Hyperledger fabric的链码接口整理

本文档详细梳理了Hyperledger Fabric的链码接口,强调了所有链上代码必须实现的接口,特别是如何通过这些接口处理交易。还介绍了StateRangeQueryIteratorInterface,它用于在状态数据库中进行键值范围查询,以及chaincode_example02的解析,作为理解链码功能的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Chaincode接口必须被所有的链上代码实现,fabric运行交易通过调用这些指定的函数

 type Chaincode interface{ 
        
      // 在容器建立连接之后再部署交易期间调用Init函数,准许链上代码初始化内部数据
    Init(stub ChaincodeStubInterface, function string, args []string) ([]byte, error)
    // 每次调用交易都会调用Invoke接口. 链上代码可能会改变状态变量
    Invoke(stub ChaincodeStubInterface, function string, args []string) ([]byte, error)
    // 查询交易时调用Query接口. 链上代码仅仅读(而不是修改)它的状态变量并其返回结果
    Query(stub ChaincodeStubInterface, function string, args []string) ([]byte, error)
}
2. ChaincodeStubInterface用来部署链上代码apps来进入和修改他们的账本

type ChaincodeStubInterface interface {
  
  
    // Get the arguments to the stub call as a 2D byte array
    // 获取stub调用的参数来作为一个2维字节数组
    GetArgs() [][]byte
 
    // Get the arguments to the stub call as a string array
    // 获取stub调用的参数来作为一个字符数组
    GetStringArgs() []string
 
    // 获取交易的ID
    GetTxID() string
 
    // InvokeChaincode 本地调用指定的链上代码,`Invoke`使用相同的交易,也就说链上代码调用
    // 链上代码不会创建一个新的交易消息
    InvokeChaincode(chaincodeName string, args [][]byte) ([]byte, error)
 
    // InvokeChaincode 本地调用指定的链上代码,`Query`使用相同的交易,也就说链上代码调用
    // 链上代码不会创建一个新的交易消息
    QueryChaincode(chaincodeName string, args [][]byte) ([]byte, error)
 
    // GetState通过Key来返回数组的特定值
    GetState(key string) ([]byte, error)
 
    // PutState向账本中写入特定的键和值
    PutState(key string, value []byte) error
 
    // DelState从账本中移除指定的键和值
    DelState(key string) error
 
    // RangeQueryState函数可以通过chaincode调用来查询状态范围内的键。假设startKey和endKey
    // 在词典中,将返回一个迭代器,它可以用来遍历startKey和endKey之间的所有键。
    // 迭代器返回键的顺序是随机的。
    RangeQueryState(startKey, endKey string) (StateRangeQueryIteratorInterface, error)
 
    // CreateTable创建一张新表,给出表名和列定义
    CreateTable(name string, columnDefinitions []*ColumnDefinition) error
 
    // GetTable如果表存在,返回指定的一张表,如果表不存在ErrTableNotFound错误
    GetTable(tableName string) (*Table, error)
 
    // DeleteTable删除表和实体相关的所有行
    DeleteTable(tableName string) error
 
    // InsertRow 插入一个新行进入指定的表
    // 返回 -
    // 如果行成功插入返回true和no error
    // 如果已经存在给定的行就返回false和no error
    // 如果指定的表名不存在返回false和TableNotFoundError
    // 如果出现一个没有预料到的错误条件返回false和error
    InsertRow(tableName string, row Row) (bool, error)
 
    // ReplaceRow 在指定的表中更新行.
    // 返回 -
    // 如果行成功更新就返回false和no error
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

The_Web3_社区

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值