Warm tip: This article is reproduced from stackoverflow.com, please click
hyperledger hyperledger-fabric smartcontracts

Call contract form another contract in chaincode in HYPERLEDGER Fabric

发布于 2020-04-15 10:16:17

I'm trying to call contract form another contract inside one channel and deployed on one Chaincode('cc'). Version of 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
  }
}

And getting error:

[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

Everythoing else working as expected(calling methods from client, saving sate, etc). What I'm doing wrong? Is it impossible to call method from one contract in another in same chaincode???

Questioner
GeorgeMA
Viewed
86
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 is designed to actually call another chaincode. Part of the confusion is that Contract and chaincode are not exactly the same thing.

In your case, you've implemented two Contracts within a single chaincode. A Contract is a higher level abstraction and one or more Contracts can be packaged within the same chaincode.

So you actually want a Contract to call a Contract within the same chaincode, not across two different chaincodes.