Top 6 Hyperledger Fabric Instantiate Errors - For Programmers

Following codes seems to be very basic silly mistakes any hyperledger developer (you) making. As i already spend some time on it, here are some ways to make mistakes and avoid it. (Not well documented officially anywhere)

This Fabric Instantiate will not work.

This wont work

  // Initialize the chaincode
  async Init(stub) {
    let ret = stub.getFunctionAndParameters();
    let args = ret.params;
    let A = args[0];
    let B = args[1];
    let Aval = "1";
    let Bval = "4";
    await stub.putState(A, Buffer.from(Aval.toString()));
    await stub.putState(B, Buffer.from(Bval.toString()));
    return stub.success(Buffer.from("Init Successfully"));
  }

The Last line stub.success(Buffer.from("Init Successfully")); is supposed to be shim.success(Buffer.from("Init Successfully"));
Incase of error following error will be thrown by hyperledger fabric-node-sdk


[2018-07-09 10:24:10.973] [ERROR] instantiate-chaincode - instantiate proposal was bad
[2018-07-09 10:24:10.973] [ERROR] instantiate-chaincode - instantiate proposal was bad
[2018-07-09 10:24:10.973] [DEBUG] instantiate-chaincode - Failed to send Proposal and receive all good ProposalResponse
[2018-07-09 10:24:10.973] [ERROR] instantiate-chaincode - Failed to instantiate. cause:Failed to send Proposal and receive all good ProposalResponse
(node:4435) UnhandledPromiseRejectionWarning: Error: Failed to instantiate. cause:Failed to send Proposal and receive all good ProposalResponse
    at Object.instantiateChaincode (/home/rashid/Documents/hyperledger/fabric-samples/balance-transfer/app/instantiate-chaincode.js:190:9)
    at <anonymous>
(node:4435) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
This wont work
  // Initialize the chaincode
  async Init(stub) {
    let ret = stub.getFunctionAndParameters();
    let args = ret.params;
    let A = args[0];
    let B = args[1];
    let Aval = "1";
    let Bval = "3";
    await stub.putState(A, Buffer.from(Aval.toString()));
    await stub.putState(B, Buffer.from(Bval.toString()));
    shim.success();
  }
Chaincode args: "args":["A","B"]
Because the last line i am not returning anything , it should be return shim.success()


This wont work

  // Initialize the chaincode
  async Init(stub) {
    let ret = stub.getFunctionAndParameters();
    let args = ret.params;
    let A = args[0];
    let B = args[1];
    let Aval = "1";
    let Bval = "3";
    await stub.putState(A, Buffer.from(Aval.toString()));
    await stub.putState(B, Buffer.from(Bval.toString()));
    return shim.success("Success");
  }
Chaincode args: "args":["A","B"]
Because the last line should be 
return shim.success(Buffer.from("Success"))        or 
return shim.success() [With empty params]

This works

  // Initialize the chaincode
  async Init(stub) {
    let ret = stub.getFunctionAndParameters();
    let args = ret.params;
    let A = args[0];
    let Aval = "1";
    await stub.putState(A, Buffer.from(Aval.toString()));
    return shim.success();
  }
Chaincode args: "args":["A"]

So following also works, with Multiple params also

  // Initialize the chaincode
  async Init(stub) {
    let ret = stub.getFunctionAndParameters();
    let args = ret.params;
    let A = args[0];
    let Aval = "1";
    let B = args[1];
    let Bval = "156";
await stub.putState(A, Buffer.from(Aval.toString()));
    await stub.putState(B, Buffer.from(Bval.toString()));
return shim.success(); }
Chaincode args: "args":["A","B"]






But the Proper way to write Instantiate code is inclusing try and catch because async await promises might return error after execution.

  // Initialize the chaincode
  async Init(stub) {
    let ret = stub.getFunctionAndParameters();
    let args = ret.params;
    let A = args[0];
    let Aval = "1";
    try {
      await stub.putState(A, Buffer.from(Aval.toString()));
    } catch (err) {
      shim.error(Buffer.from("Error"));
    }
    return shim.success(Buffer.from("Working fine"));
  }



Have a look at Fabric Node chaincode documentation (Fabric-SHIM)

shim.success and shim.error both requires Buffer object, 
which can be created using nodejs Buffer.from("Some Text here") 

1 Comments

  1. https://mycodde.blogspot.com/2018/07/top-hyperledger-fabric-instantiate.html?showComment=1535785804314#c5014723426602275332

    ReplyDelete
Previous Post Next Post