hyperledger-fabric - 在 HYPERLEDGER Fabric 中的链代码中调用合约形成另一个合约

标签 hyperledger-fabric hyperledger smartcontracts

我正在尝试从一个 channel 内的另一个合约调用合约并部署在一个 Chaincode('cc') 上。 HLF 1.4版本

class Contract1  extends Contract {
  constructor() {
    super('Contract1');
  }

  async testContract2(ctx) {
    const res = await ctx.stub.invokeChaincode('cc', ['test']);
    return JSON.stringify(res);
  }
}

class Contract2  extends Contract {
  constructor() {
    super('Contract2');
  }

  async test(ctx) {
    // some logic
  }
}

并出现错误:

[Query]: evaluate: Query ID "[object Object]" of peer "peer0.org1.example.com:7051" failed:
 message=transaction returned with failure: Error: INVOKE_CHAINCODE failed: 
transaction ID: 82c10988ad2c7cef468cae937d2e0d0bfe649f7f9c5406498733d7ef20d387f7: 
execute failed: error sending: txid: 82c10988ad2c7cef468cae937d2e0d0bfe649f7f9c5406498733d7ef20d387f7(some-channel) exists,
 stack=Error: transaction returned with failure: Error: INVOKE_CHAINCODE failed: transaction ID:
 82c10988ad2c7cef468cae937d2e0d0bfe649f7f9c5406498733d7ef20d387f7: 
execute failed: error sending: txid: 82c10988ad2c7cef468cae937d2e0d0bfe649f7f9c5406498733d7ef20d387f7(some-channel) exists


(/hyperledger-fabric/javascript/node_modules/grpc/src/client_interceptors.js:845:24),status=500,
url=grpcs://localhost:7051, name=peer0.org1.example.com:7051,
grpc.max_receive_message_length=-1, grpc.max_send_message_length=-1, 
grpc.keepalive_time_ms=120000, grpc.http2.min_time_between_pings_ms=120000, 
grpc.keepalive_timeout_ms=20000, grpc.http2.max_pings_without_data=0, 
grpc.keepalive_permit_without_calls=1, name=peer0.org1.example.com:7051, 
grpc.ssl_target_name_override=peer0.org1.example.com, 
grpc.default_authority=peer0.org1.example.com, isProposalResponse=true

Failed to evaluate transaction: Error: transaction returned with failure: Error: 
INVOKE_CHAINCODE failed: transaction ID: 
82c10988ad2c7cef468cae937d2e0d0bfe649f7f9c5406498733d7ef20d387f7: execute failed: error sending: 
txid: 82c10988ad2c7cef468cae937d2e0d0bfe649f7f9c5406498733d7ef20d387f7(some-channel) exists

其他一切都按预期工作(从客户端调用方法、保存状态等)。我做错了什么?是否不可能在同一链代码中从一个合约调用另一个合约中的方法???

最佳答案

TL;博士

class Contract1  extends Contract {
  constructor() {
    super('Contract1');
  }

  async testContract2(ctx) {
    let contract2 = new Contract2();
    const res = await contract2.test(ctx)
    .....
  }
}

class Contract2  extends Contract {
  constructor() {
    super('Contract2');
  }

  async test(ctx) {
    // some logic
  }
}

ctx.stub.invokeChaincode 旨在实际调用另一个链码。造成混淆的部分原因是,Contractchaincode 并不完全相同。

就您而言,您已在单个链代码中实现了两个合约合约是一种更高级别的抽象,一个或多个合约可以打包在同一个链码中。

因此,您实际上希望一个Contract在同一链代码中调用一个Contract,而不是跨两个不同的链代码。

关于hyperledger-fabric - 在 HYPERLEDGER Fabric 中的链代码中调用合约形成另一个合约,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60037794/

相关文章:

hyperledger-fabric - 在 Hyperledger Fabric 中尝试调用时,出现以下错误 : chaincode definition for 'fabcar' exists, 但未安装链码

go - 通过值获取世界状态

hyperledger-fabric - 错误 : Cannot find module './api' (Hyperledger composer)

solidity - 如何通过HardHat与接口(interface)交互?

linux - 第一个在 REST 协议(protocol)上运行、部署和调用的网络样本

hyperledger-fabric - Hyperledger Fabric 1.0 中的读集和写集是什么?

docker - 测试从Docker容器到验证程序的连接时遇到的问题。 curl http://rest-api:8008/blocks

hyperledger - 修改 Hyperledger Composer Web 应用程序

blockchain - 智能合约——合约在哪里?

python - 如何将Hyperledger智能合约连接到python脚本