Smart Contract Go API

更新时间:
复制 MD 格式

ChaincodeStubInterface is the primary interface for Hyperledger Fabric Go chaincodes to interact with the distributed ledger. The interface is organized into three categories: auxiliary functions, transaction information retrieval, and ledger data operations.

For the full interface definition and source implementation, see the fabric-chaincode-go shim package.

Auxiliary functions

These methods handle chaincode invocation parameters, cross-chaincode calls, composite key management, and event emission.

Method signatureDescription
GetArgs() [][]byteReturns the arguments from the chaincode invocation request as a byte slice array. Each element is one argument.
GetStringArgs() []stringReturns the arguments from the chaincode invocation request as a string slice.
GetFunctionAndParameters() (string, []string)Returns the function name and remaining arguments from the chaincode invocation. By default, the first argument is treated as the function name.
GetArgsSlice() ([]byte, error)Returns all arguments from the chaincode invocation request joined into a single byte slice.
InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.ResponseCalls the Invoke method of another chaincode.
CreateCompositeKey(objectType string, attributes []string) (string, error)Combines an object type and a set of attributes into a composite key. Use SplitCompositeKey to decompose the resulting key.
SplitCompositeKey(compositeKey string) (string, []string, error)Splits a composite key back into its object type and attributes. See also CreateCompositeKey.
SetEvent(name string, payload []byte) errorSets a chaincode event to include in the transaction proposal response.

Transaction information

These methods expose metadata about the current transaction: its ID, channel, creator identity, timestamp, and proposal payload.

Method signatureDescription
GetTxID() stringReturns the transaction ID of the current transaction.
GetChannelID() stringReturns the name of the channel on which the transaction is executing.
GetCreator() ([]byte, error)Returns the serialized identity of the transaction submitter.
GetTransient() (map[string][]byte, error)Returns the transient map from the transaction proposal. Transient data is used for application-level communication and is not written to the ledger.
GetBinding() ([]byte, error)Returns the transaction binding, which links application-specific data to the transient data.
GetDecorations() map[string][]byteReturns additional decorations attached to the transaction proposal by the peer.
GetSignedProposal() (*pb.SignedProposal, error)Returns the complete signed transaction proposal, including all associated data.
GetTxTimestamp() (*timestamp.Timestamp, error)Returns the timestamp at which the transaction was created.

Ledger data operations

These methods read from and write to both the public ledger state and private data collections.

Public state

Method signatureDescription
GetState(key string) ([]byte, error)Returns the value of the specified key from the public ledger state.
PutState(key string, value []byte) errorAdds or updates a key-value pair in the public ledger state.
DelState(key string) errorDeletes a key-value pair from the public ledger state.
SetStateValidationParameter(key string, ep []byte) errorSets the endorsement policy for a specific key in the public ledger state.
GetStateValidationParameter(key string) ([]byte, error)Returns the endorsement policy for a specific key in the public ledger state.

Range and composite key queries

Method signatureDescription
GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error)Returns all key-value pairs with keys in a specified range.
GetStateByRangeWithPagination(startKey, endKey string, pageSize int32, bookmark string) (StateQueryIteratorInterface, *pb.QueryResponseMetadata, error)Returns key-value pairs in a specified range by page.
GetStateByPartialCompositeKey(objectType string, keys []string) (StateQueryIteratorInterface, error)Returns all key-value pairs whose composite key prefix matches the given objectType and partial key attributes. See also CreateCompositeKey and SplitCompositeKey.
GetStateByPartialCompositeKeyWithPagination(objectType string, keys []string, pageSize int32, bookmark string) (StateQueryIteratorInterface, *pb.QueryResponseMetadata, error)Paged version of GetStateByPartialCompositeKey.

Rich queries

Rich queries require a state database that supports them.

Method signatureDescription
GetQueryResult(query string) (StateQueryIteratorInterface, error)Executes a rich query against the state database. The state database must support rich queries.
GetQueryResultWithPagination(query string, pageSize int32, bookmark string) (StateQueryIteratorInterface, *pb.QueryResponseMetadata, error)Executes a rich query against the state database by page. The state database must support rich queries.

History queries

Method signatureDescription
GetHistoryForKey(key string) (HistoryQueryIteratorInterface, error)Returns all historical values of the specified key.

Private data

These methods operate on private data collections. The collection parameter identifies the target collection by name.

Method signatureDescription
GetPrivateData(collection, key string) ([]byte, error)Returns the value of the specified key from the given private data collection.
GetPrivateDataHash(collection, key string) ([]byte, error)Returns the hash of the value of the specified key in the given private data collection.
PutPrivateData(collection string, key string, value []byte) errorAdds or updates a key-value pair in the given private data collection.
DelPrivateData(collection, key string) errorDeletes the specified key from the given private data collection.
SetPrivateDataValidationParameter(collection, key string, ep []byte) errorSets the endorsement policy for a specific key in the given private data collection.
GetPrivateDataValidationParameter(collection, key string) ([]byte, error)Returns the endorsement policy for a specific key in the given private data collection.
GetPrivateDataByRange(collection, startKey, endKey string) (StateQueryIteratorInterface, error)Returns key-value pairs within a specified range from the given private data collection.
GetPrivateDataByPartialCompositeKey(collection, objectType string, keys []string) (StateQueryIteratorInterface, error)Returns all key-value pairs whose composite key prefix matches the given objectType and partial key attributes in the given private data collection. See also CreateCompositeKey and SplitCompositeKey.
GetPrivateDataQueryResult(collection, query string) (StateQueryIteratorInterface, error)Executes a rich query against the given private data collection. Requires a state database that supports rich queries.