温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Call contract form another contract in chaincode in HYPERLEDGER Fabric
hyperledger hyperledger-fabric smartcontracts

其他 - 在HYPERLEDGER Fabric中以链码形式从另一个合同中调用合同

发布于 2020-04-15 11:56:39

我正在尝试将合同从一个渠道中的另一个合同中调用并部署在一个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

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

查看更多

提问者
GeorgeMA
被浏览
100
Gari Singh 2020-02-03 22:42

TL; DR

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不完全一样的东西。

在您的情况下,您已Contracts在一个链码中实现了两个A Contract是更高级别的抽象,Contracts可以将一个或多个打包在同一抽象中chaincode

因此,您实际上希望a 在相同的链码中而不是在两个不同的链码中Contract调用a Contract