The ChaincodeStubImpl class provides the Java API for Hyperledger Fabric chaincode. Use these methods in your chaincode to read and write ledger state, query transaction metadata, and interact with other chaincodes.
For the full source implementation, see ChaincodeStubImpl on GitHub.
The API is organized into three groups:
Utility methods — parse call arguments, manage composite keys, set events, and invoke other chaincodes
Transaction information — retrieve metadata about the current transaction
Ledger data operations — read, write, and query state and private data
Utility methods
| Method | Description |
|---|---|
public List getArgs() | Returns all arguments in the chaincode invocation request as raw bytes. |
public List getStringArgs() | Returns all arguments in the chaincode invocation request as strings. |
public String getFunction() | Returns the function name from the chaincode call. By default, the first argument is treated as the function name. |
public List getParameters() | Returns the call arguments in the chaincode invocation request, excluding the function name. |
public void setEvent(String name, byte[] payload) | Sets an event to emit. |
public ChaincodeEvent getEvent() | Returns the event set by setEvent. |
public CompositeKey createCompositeKey(String objectType, String… attributes) | Combines an object type and a list of attributes into a composite key. |
public CompositeKey splitCompositeKey(String compositeKey) | Splits a composite key back into its component attributes. |
public Response invokeChaincode(final String chaincodeName, final List args, final String channel) | Invokes another chaincode's Invoke method. |
Transaction information
| Method | Description |
|---|---|
public String getChannelId() | Returns the name of the current channel. |
public String getTxId() | Returns the transaction ID. |
public SignedProposal getSignedProposal() | Returns all data associated with the signed transaction proposal. |
public Instant getTxTimestamp() | Returns the transaction timestamp. |
public byte[] getCreator() | Returns the identity of the transaction submitter. |
public Map getTransient() | Returns transient data from the transaction. Transient data is used at the application level and is not written to the ledger. |
public byte[] getBinding() | Returns the binding information of the transaction, which is mainly used to force the link between application data and temporary information. |
Ledger data operations
Public state
| Method | Description |
|---|---|
public byte[] getState(String key) | Returns the current value of a key from the ledger. |
public void putState(String key, byte[] value) | Adds or updates a key-value pair in the ledger. |
public void delState(String key) | Deletes a key-value pair from the ledger. |
public byte[] getStateValidationParameter(String key) | Returns the endorsement policy for a specific key. |
public void setStateValidationParameter(String key, byte[] value) | Sets the endorsement policy for a specific key. |
public QueryResultsIterator getStateByRange(String startKey, String endKey) | Returns all key-value pairs within a specified key range. |
public QueryResultsIteratorWithMetadata getStateByRangeWithPagination(String startKey, String endKey, int pageSize, String bookmark) | Returns key-value pairs within a specified key range, paginated by pageSize. |
public QueryResultsIterator getStateByPartialCompositeKey(String compositeKey) | Returns all key-value pairs whose composite keys begin with the specified prefix (string form). |
public QueryResultsIterator getStateByPartialCompositeKey(String objectType, String… attributes) | Returns all key-value pairs whose composite keys begin with the specified object type and attributes. |
public QueryResultsIterator getStateByPartialCompositeKey(CompositeKey compositeKey) | Returns all key-value pairs whose composite keys begin with the specified prefix (CompositeKey form). |
public QueryResultsIteratorWithMetadata getStateByPartialCompositeKeyWithPagination(CompositeKey compositeKey, int pageSize, String bookmark) | Returns key-value pairs matching a partial composite key, paginated by pageSize. |
public QueryResultsIterator getQueryResult(String query) | Runs a rich query against the state database. Requires a state database that supports rich queries, such as CouchDB. |
public QueryResultsIteratorWithMetadata getQueryResultWithPagination(String query, int pageSize, String bookmark) | Runs a rich query with pagination against the state database. Returns up to pageSize results. Requires a state database that supports rich queries, such as CouchDB. |
public QueryResultsIterator getHistoryForKey(String key) | Returns all historical values for a given key. |
Private data
| Method | Description |
|---|---|
public byte[] getPrivateData(String collection, String key) | Returns the value of a key from the specified private data collection. |
public byte[] getPrivateDataHash(String collection, String key) | Returns the hash of the value of a key in the specified private data collection. |
public byte[] getPrivateDataValidationParameter(String collection, String key) | Returns the endorsement policy for a key in the specified private data collection. |
public void putPrivateData(String collection, String key, byte[] value) | Sets the value of a key in the specified private data collection. |
public void delPrivateData(String collection, String key) | Deletes a key from the specified private data collection. |
public void setPrivateDataValidationParameter(String collection, String key, byte[] value) | Sets the endorsement policy for a key in the specified private data collection. |
public QueryResultsIterator getPrivateDataByRange(String collection, String startKey, String endKey) | Returns all key-value pairs in the specified private data collection within a specified key range. |
public QueryResultsIterator getPrivateDataByPartialCompositeKey(String collection, String compositeKey) | Returns all key-value pairs in the specified private data collection whose composite keys begin with the given prefix (string form). |
public QueryResultsIterator getPrivateDataByPartialCompositeKey(String collection, CompositeKey compositeKey) | Returns all key-value pairs in the specified private data collection whose composite keys begin with the given prefix (CompositeKey form). |
public QueryResultsIterator getPrivateDataByPartialCompositeKey(String collection, String objectType, String… attributes) | Returns all key-value pairs in the specified private data collection whose composite keys begin with the given object type and attributes. |
public QueryResultsIterator getPrivateDataQueryResult(String collection, String query) | Runs a rich query against a private data collection. Requires a state database that supports rich queries, such as CouchDB. |
该文章对您有帮助吗?