合约相关的API contractapi.ContractStub提供如下API接口,可以在合约文件中进行调用。这些API按照功能可以划分为: 表1 辅助功能 接口 说明 FuncName() string 获取智能合约请求中指定的智能合约函数名称。 Parameters() [][]byte 获取请求参数。 ChainID() string 获取智能合约所在链ID。 ContractName() string 获取智能合约名称。 表2 账本数据操作 接口 说明 GetKV(key string) ([]byte, error) 获取某个键对应的值。 PutKV(key string, value []byte) error 添加或更新一对键值。 PutKVCommon(key string, value interface{}) error 添加或更新一对键值。其中值为结构体,需要实现Marshal() ([]byte, error)。 DelKV(key string) error 删除一对键值。 GetIterator(startKey, endKey string) (Iterator, error) 查询指定范围内的键值,查询范围是左闭右开的。 GetKeyHistoryIterator(key string) (HistoryIterator, error) 查询某个键的所有历史值。 SaveComIndex(indexName string, attributes []string, objectKey string) error 创建一个复合键。 例如为"zhangsan":{sex="male", height=175}创建用于查询性别的复合键,可调用SaveComIndex("sex", []string{"male"}, "zhangsan"),如创建用于查询性别和身高的复合键,可调用SaveComIndex("sex/height", []string{"male", "175"}, "zhangsan")。 GetKVByComIndex(indexName string, attributes []string) (Iterator, error) 通过复合键查找满足某种查询条件的键值对。 DelComIndexOneRow(indexName string, attributes []string, objectKey string) error 删除某个复合键。 父主题: Go合约开发