如何使用Hyperledger getNative API?Composer中链码调用问询
Great question! Let's dive into how the getNative API works and how you can use it to interact with your custom chaincode from Hyperledger Composer.
getNative API Works The getNative API in Hyperledger Composer acts as a bridge between Composer's high-level abstraction layer and the underlying Hyperledger Fabric chaincode environment. Here's a breakdown of its core mechanics:
- It exposes the Fabric ChaincodeStub instance running your Composer business network. This stub is the same one native Fabric chaincodes use to interact with the ledger.
- By accessing this stub, you bypass Composer's data model and transaction logic directly, gaining low-level access to Fabric's native chaincode operations (like reading/writing ledger state, invoking other chaincodes, etc.).
- You can call
getNative()within any Composer transaction function, transaction processor, or script to retrieve this stub instance.
Absolutely! You can use the getNative API to invoke getter and setter methods defined in your standalone Fabric chaincode. Here's a practical example of how to implement this in your Composer JavaScript code:
Example: Invoking a Getter Method
Suppose your custom chaincode has a getter method getAsset that takes an asset ID and returns its data. In your Composer transaction function:
/** * Transaction function to call a custom chaincode getter * @param {org.example.FetchAsset} tx * @transaction */ async function callCustomGetter(tx) { // Retrieve the native Fabric ChaincodeStub const stub = getNativeAPI(); // Invoke the custom chaincode's getter method // Parameters: chaincode name, method name, arguments array const getterResponse = await stub.invokeChaincode('my-custom-chaincode', ['getAsset', tx.assetId]); // Parse the response (Fabric returns a Buffer, so convert to JSON) const assetData = JSON.parse(getterResponse.payload.toString()); // Use the fetched asset data in your Composer logic console.log('Fetched asset details:', assetData); }
Example: Invoking a Setter Method
For a setter method setAssetValue that updates an asset's value, the approach is similar:
/** * Transaction function to call a custom chaincode setter * @param {org.example.UpdateAssetValue} tx * @transaction */ async function callCustomSetter(tx) { const stub = getNativeAPI(); // Prepare arguments for the setter (asset ID, new value) const setterArgs = ['setAssetValue', tx.assetId, tx.newValue]; // Invoke the setter method await stub.invokeChaincode('my-custom-chaincode', setterArgs); // Optional: Emit an event to track the update const updateEvent = getFactory().newEvent('org.example', 'AssetValueUpdated'); updateEvent.assetId = tx.assetId; updateEvent.newValue = tx.newValue; emit(updateEvent); }
Key Notes
- Ensure your custom chaincode is installed and instantiated on the same Fabric channel as your Composer business network.
- When using
invokeChaincode, you need to know the exact chaincode name and method signature (argument order, data types) to pass valid parameters. - Responses from
invokeChaincodeare returned as Buffers, so you'll need to convert them to strings or JSON to work with in your Composer logic.
内容的提问来源于stack exchange,提问作者user1274878

