2

I have followed the basic guide for getting the HyperLedger fabric-starter-kit up and running which works perfectly. I cannot figure out how to successfully change the development directory of the app.js without causing an "invalid ELF header" error:

root@104efc36f09e:/user/env# node app
module.js:355
   Module._extensions[extension](this, filename);
                           ^
Error: /user/env/node_modules/grpc/src/node/extension_binary/grpc_node.node: invalid ELF header
   at Error (native)
   at Module.load (module.js:355:32)
   at Function.Module._load (module.js:310:12)
   at Module.require (module.js:365:17)
   at require (module.js:384:17)
   at Object.<anonymous> (/user/env/node_modules/grpc/src/node/src/grpc_extension.js:38:15)
   at Module._compile (module.js:460:26)
   at Object.Module._extensions..js (module.js:478:10)
   at Module.load (module.js:355:32)
   at Function.Module._load (module.js:310:12)
root@104efc36f09e:/user/env#

Dockerfile (unchanged):

FROM hyperledger/fabric-peer:latest
WORKDIR $GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
RUN go build
WORKDIR $GOPATH/src/github.com/hyperledger/fabric/examples/sdk/node
RUN npm install hfc`

docker-compose.yaml (changed volume to local workdir: ~/Documents/Work/Blockchain/env):

membersrvc:
  container_name: membersrvc
  image: hyperledger/fabric-membersrvc
  command: membersrvc

peer:
  container_name: peer
  image: hyperledger/fabric-peer
  environment:
    - CORE_PEER_ADDRESSAUTODETECT=true
    - CORE_VM_ENDPOINT=unix:///var/run/docker.sock
    - CORE_LOGGING_LEVEL=DEBUG
    - CORE_PEER_ID=vp0
    - CORE_SECURITY_ENABLED=true
    - CORE_PEER_PKI_ECA_PADDR=membersrvc:7054
    - CORE_PEER_PKI_TCA_PADDR=membersrvc:7054
    - CORE_PEER_PKI_TLSCA_PADDR=membersrvc:7054
    - CORE_PEER_VALIDATOR_CONSENSUS_PLUGIN=noops
  # this gives access to the docker host daemon to deploy chain code in network mode
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
  # have the peer wait 10 sec for membersrvc to start
  #  the following is to run the peer in Developer mode - also set sample DEPLOY_MODE=dev
  command: sh -c "sleep 10; peer node start --peer-chaincodedev"
  #command: sh -c "sleep 10; peer node start"
  links:
    - membersrvc

starter:
  container_name: starter
  image: hyperledger/fabric-starter-kit
  volumes:
    - ~/Documents/Work/Blockchain/env:/user/env
  environment:
    - MEMBERSRVC_ADDRESS=membersrvc:7054
    - PEER_ADDRESS=peer:7051
    - KEY_VALUE_STORE=/tmp/hl_sdk_node_key_value_store
    # set to following to 'dev' if peer running in Developer mode
    - DEPLOY_MODE=dev
    - CORE_CHAINCODE_ID_NAME=mycc
    - CORE_PEER_ADDRESS=peer:7051
  # the following command will start the chain code when this container starts and ready it for deployment by the app
  command: sh -c "sleep 20; /opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02/chaincode_example02"
  stdin_open: true
  tty: true
  links:
    - membersrvc
    - peer

app.js (unchanged):

/*
 * A simple application utilizing the Node.js Client SDK to:
 * 1) Enroll a user
 * 2) User deploys chaincode
 * 3) User queries chaincode
 */
// "HFC" stands for "Hyperledger Fabric Client"
var hfc = require("hfc");

console.log(" **** STARTING APP.JS ****");

// get the addresses from the docker-compose environment
var PEER_ADDRESS         = process.env.CORE_PEER_ADDRESS;
var MEMBERSRVC_ADDRESS   = process.env.MEMBERSRVC_ADDRESS;

var chain, chaincodeID;

// Create a chain object used to interact with the chain.
// You can name it anything you want as it is only used by client.
chain = hfc.newChain("mychain");

// Initialize the place to store sensitive private key information
chain.setKeyValStore( hfc.newFileKeyValStore('/tmp/keyValStore') );

// Set the URL to membership services and to the peer
console.log("member services address ="+MEMBERSRVC_ADDRESS);
console.log("peer address ="+PEER_ADDRESS);
chain.setMemberServicesUrl("grpc://"+MEMBERSRVC_ADDRESS);
chain.addPeer("grpc://"+PEER_ADDRESS);

// The following is required when the peer is started in dev mode
// (i.e. with the '--peer-chaincodedev' option)
var mode =  process.env['DEPLOY_MODE'];
console.log("DEPLOY_MODE=" + mode);
if (mode === 'dev') {
    chain.setDevMode(true);xs
    //Deploy will not take long as the chain should already be running
    chain.setDeployWaitTime(10);
} else {
    chain.setDevMode(false);
    //Deploy will take much longer in network mode
    chain.setDeployWaitTime(120);
}


chain.setInvokeWaitTime(10);

// Begin by enrolling the user
enroll();

// Enroll a user.
function enroll() {
   console.log("enrolling user admin ...");
   // Enroll "admin" which is preregistered in the membersrvc.yaml
   chain.enroll("admin", "Xurw3yU9zI0l", function(err, admin) {
      if (err) {
         console.log("ERROR: failed to register admin: %s",err);
         process.exit(1);
      }
      // Set this user as the chain's registrar which is authorized to register other users.
      chain.setRegistrar(admin);

      var userName = "JohnDoe";
      // registrationRequest
      var registrationRequest = {
          enrollmentID: userName,
          affiliation: "bank_a"
      };
      chain.registerAndEnroll(registrationRequest, function(error, user) {
          if (error) throw Error(" Failed to register and enroll " + userName + ": " + error);
          console.log("Enrolled %s successfully\n", userName);
          deploy(user);
      });      
   });
}

// Deploy chaincode
function deploy(user) {
   console.log("deploying chaincode; please wait ...");
   // Construct the deploy request
   var deployRequest = {
       chaincodeName: process.env.CORE_CHAINCODE_ID_NAME,
       fcn: "init",
       args: ["a", "100", "b", "200"]
   };
   // where is the chain code, ignored in dev mode
   deployRequest.chaincodePath = "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02";

   // Issue the deploy request and listen for events
   var tx = user.deploy(deployRequest);
   tx.on('complete', function(results) {
       // Deploy request completed successfully
       console.log("deploy complete; results: %j",results);
       // Set the testChaincodeID for subsequent tests
       chaincodeID = results.chaincodeID;
       invoke(user);
   });
   tx.on('error', function(error) {
       console.log("Failed to deploy chaincode: request=%j, error=%k",deployRequest,error);
       process.exit(1);
   });

}

// Query chaincode
function query(user) {
   console.log("querying chaincode ...");
   // Construct a query request
   var queryRequest = {
      chaincodeID: chaincodeID,
      fcn: "query",
      args: ["a"]
   };
   // Issue the query request and listen for events
   var tx = user.query(queryRequest);
   tx.on('complete', function (results) {
      console.log("query completed successfully; results=%j",results);
      process.exit(0);
   });
   tx.on('error', function (error) {
      console.log("Failed to query chaincode: request=%j, error=%k",queryRequest,error);
      process.exit(1);
   });
}

//Invoke chaincode
function invoke(user) {
   console.log("invoke chaincode ...");
   // Construct a query request
   var invokeRequest = {
      chaincodeID: chaincodeID,
      fcn: "invoke",
      args: ["a", "b", "1"]
   };
   // Issue the invoke request and listen for events
   var tx = user.invoke(invokeRequest);
   tx.on('submitted', function (results) {
          console.log("invoke submitted successfully; results=%j",results);       
       });   
   tx.on('complete', function (results) {
      console.log("invoke completed successfully; results=%j",results);
      query(user);      
   });
   tx.on('error', function (error) {
      console.log("Failed to invoke chaincode: request=%j, error=%k",invokeRequest,error);
      process.exit(1);
   });
}

My goal is to create an authentication service using the HFC so that an Android app invoke a transaction. Any help would be greatly appreciated.

  • are you switching between the platforms or the architecture for the development ? make sure that `npm` modules are built on the platform you are executing it. Try re-installing your node modules. Please read this http://stackoverflow.com/questions/32618976/nodejs-google-compute-engine-invalid-elf-header-when-using-gcloud-module and this as well http://stackoverflow.com/questions/29994411/invalid-elf-header-when-using-the-nodejs-ref-module-on-aws-lambda . let me know if it helps – Sufiyan Ghori Nov 08 '16 at 01:54
  • 1
    @SufiyanGhori Great help! I deleted the node_modules folder in my work directory and ran `npm install hfc@0.6.x` from inside the `starter` docker image which worked. Thanks! – Callum Hyland Nov 08 '16 at 11:25
  • try to use this boilerplate https://github.com/IBM-Blockchain/fabric-boilerplate – Sibelius Seraphini Jan 07 '17 at 17:24

2 Answers2

1

you installed node modules in your mac and used them in your Linux docker image. This is what causing the problem.

Make sure that npm modules are built on the platform you are executing it. Re-install your node modules in your linux environment by first deleting node_modules and running npm install from inside starter docker image.

Please consult these questions as well,

NodeJs Google Compute Engine Invalid ELF Header when using 'gcloud' module

"invalid ELF header" when using the nodejs "ref" module on AWS Lambda

Community
  • 1
  • 1
Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
0

Credit to Sufiyan Ghori for pointing this out - the issue was that the node modules were installed in my host (mac) and therefore weren't compatible with the linux docker image I was trying to execute code within.

SOLUTION:

  • Delete node_modules folder from work directory.
  • Run npm install hfc@0.6.x from inside the starter docker image.