diff --git a/README.md b/README.md index 2186d99..fa6f1b2 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ hash so that its existence can be later proved through transaction queries. * node - v6.9.5 * Git client * Windows needs OpenSSL installed into C:/OpenSSL-Win64 - * Goto https://slproweb.com/products/Win32OpenSSL.html and pick the full Win64 package + * Goto https://slproweb.com/products/Win32OpenSSL.html and pick the full Win64 package ## Installation @@ -24,17 +24,6 @@ npm install -g bower npm install ``` -> **Windows note:** To avoid a problem with the windows style '\\' during -> the chaincode install resulting in the error: -> -> `Promise is rejected: Error: Illegal file detected in payload: "src\github.com\chaincode\poe_chaincode.go"` -> -> per FAB-3975, -> add `.split('\\').join('/')` to the end of line 98 of -> node_modules/fabric-client/lib/packager/Golang.js like this: -> -> `name: path.relative(goPath, entry.path).split('\\').join('/'),` - ## Setting up a test blockchain using Docker A Hyperledger Fabric blockchain network can be run on your local machine @@ -48,7 +37,7 @@ The instructions below assume that you already have docker installed and running Pull the fabric chaincode environment image using the x86_64-1.0.0-alpha tag. ``` -docker pull hyperledger/fabric-ccenv:x86_64-1.0.0-alpha2 +docker pull hyperledger/fabric-ccenv:x86_64-1.0.0-beta ``` Next give the fabric-ccenv the 'latest' tag so that software can use it. Replace the image ID (4ac07a26ca7a) in the example command with the image ID diff --git a/api.js b/api.js index 7fe3a92..d8f990f 100644 --- a/api.js +++ b/api.js @@ -59,7 +59,6 @@ debug(process.env.GOPATH); var confidentialSetting = config.confidentialSetting; // Store the exports from initialize.js in global variables -var chain = init.chain; var GlobalAppUser = init.GlobalAppUser; var cred = config.cred; @@ -116,7 +115,7 @@ app.get('/chain/transactions/:id', function(req, res) { init.client.setUserContext(appUser.hfcUser, true) .then(function(user) { console.log('user is ', user.getName()); - return init.chain.queryTransaction(req.params.id); + return init.channel.queryTransaction(req.params.id); }) .then(function(result) { debug(result); diff --git a/createChannel.js b/createChannel.js index 502b23c..0ea0b31 100644 --- a/createChannel.js +++ b/createChannel.js @@ -47,12 +47,14 @@ let opts = {path: config.keyPath}; debug(opts); debug('============================================================='); -var cryptoSuite = client.newCryptoSuite(opts); +var cryptoSuite = HFC.newCryptoSuite(); +cryptoSuite.setCryptoKeyStore(HFC.newCryptoKeyStore(opts)); +client.setCryptoSuite(cryptoSuite); debug(cryptoSuite); debug('============================================================='); var ca = new FabricCAServices(cred.cas[0].api_url, tlsOptions, '', cryptoSuite); // Create a new CA var targets = []; -var chain = {}; +var channel = {}; var eventhub; var ehtxid; @@ -187,10 +189,8 @@ HFC.newDefaultKeyValueStore({ var signature = client.signChannelConfig(channelConfig); var signatures = []; signatures.push(signature); - signatures.push(signature); // this second signature is from the sdk test code. Appears to work around a problem somewhere - let nonce = utils.getNonce(); - let txId = HFC.buildTransactionID(nonce, peerorg1Admin); - ehtxid = txId.toString(); + let txId = client.newTransactionID(); + ehtxid = txId.getTransactionID(); // jscs:disable requireCamelCaseOrUpperCaseIdentifiers // set up the event hub @@ -208,7 +208,7 @@ HFC.newDefaultKeyValueStore({ let ehPromise = new Promise(function(resolve, reject) { let handle = setTimeout(function() { - if (!config.windows) {eventhub.unregisterTxEvent(ehtxid);} + eventhub.unregisterTxEvent(ehtxid); reject(new Error('Event hub timed out.')); }, 10000); debug('registering for the Tx event'); @@ -216,7 +216,7 @@ HFC.newDefaultKeyValueStore({ // Setup event hug to listen for results eventhub.registerTxEvent(ehtxid, function(txid, code) { clearTimeout(handle); - if (!config.windows) {eventhub.unregisterTxEvent(txid);} + eventhub.unregisterTxEvent(txid); if (code !== 'VALID') { debug('Transaction failed event hub reported:', code); @@ -230,12 +230,11 @@ HFC.newDefaultKeyValueStore({ // Create the new form for request... request = { - name: 'mychannel', + name: config.channelId, orderer: orderer, config: channelConfig, signatures: signatures, txId: txId, - nonce: nonce }; debug('============ calling createChannel ====================='); @@ -244,27 +243,27 @@ HFC.newDefaultKeyValueStore({ .then(function(result) { debug('--------------------------------------------'); debug('Channel created ', result); - if (!config.windows) {eventhub.disconnect();} + eventhub.disconnect(); - chain = client.newChain('mychannel'); - chain.addOrderer(orderer); + channel = client.newChannel(config.channelId); + channel.addOrderer(orderer); // see if the channel has been created - debug(chain); - debug('chain name', chain.getName()); - debug('orderers', chain.getOrderers()); + debug(channel); + debug('channel name', channel.getName()); + debug('orderers', channel.getOrderers()); debug('----------calling getChannelConfig after createChannel succeeded -----------'); - return chain.getChannelConfig(); + return channel.getChannelConfig(); }, function(err) { debug('Error creating channel, maybe it is already created, try building it'); debug(err); - if (!config.windows) {eventhub.disconnect();} + eventhub.disconnect(); - chain = client.newChain('mychannel'); - chain.addOrderer(orderer); + channel = client.newChannel(config.channelId); + channel.addOrderer(orderer); debug('----------calling getChannelConfig after createChannel fails -----------'); - return chain.getChannelConfig(); + return channel.getChannelConfig(); }) .then(function(result) { debug('channel config', result); @@ -291,13 +290,11 @@ HFC.newDefaultKeyValueStore({ debug('finished enrolling orderer admin'); debug(admin); - let nonce = utils.getNonce(); - let txId = HFC.buildTransactionID(nonce, admin); + let txId = client.newTransactionID(); let request = { txId: txId, - nonce: nonce }; - return chain.getGenesisBlock(request); + return channel.getGenesisBlock(request); }) .then(function(block) { genisisBlock = block; @@ -308,15 +305,13 @@ HFC.newDefaultKeyValueStore({ // join the peers to the channel debug('join the peers to the channel'); - let nonce = utils.getNonce(); - let txId = HFC.buildTransactionID(nonce, user); + let txId = client.newTransactionID(); var request = { targets: targets, block: genisisBlock, txId: txId, - nonce: nonce }; - return chain.joinChannel(request); + return channel.joinChannel(request); }) .then(function(result) { debug('-------- join channel results ---------'); diff --git a/initialize.js b/initialize.js index 122ced9..0274b1d 100644 --- a/initialize.js +++ b/initialize.js @@ -38,7 +38,7 @@ var cred = config.cred; // Configuration file for the test network from the fab var client = new HFC(); // jscs:disable requireCamelCaseOrUpperCaseIdentifiers -var chain = client.newChain(config.channelId); +var channel = client.newChannel(config.channelId); var targets = []; var tlsOptions = { @@ -48,7 +48,9 @@ var tlsOptions = { debug('ca tls cert', cred.cas[0].tls_cacerts); -var cryptoSuite = client.newCryptoSuite({path: config.keyPath}); +var cryptoSuite = HFC.newCryptoSuite(); +cryptoSuite.setCryptoKeyStore(HFC.newCryptoKeyStore({path: config.keyPath})); +client.setCryptoSuite(cryptoSuite); // It may not be necessary to set the cryptoSuite here var ca = new FabricCAServices(cred.cas[0].api_url, tlsOptions, '', cryptoSuite); var admin; var member; @@ -67,7 +69,7 @@ for (var i = 0; i < cred.peers.length; i++) { } for (var i = 0; i < peerList.length; i++) { targets.push(peerList[i]); - chain.addPeer(peerList[i]); + channel.addPeer(peerList[i]); } // Add orderer @@ -78,7 +80,7 @@ var orderer = client.newOrderer( 'ssl-target-name-override': cred.orderers[0].common_name } ); -chain.addOrderer(orderer); +channel.addOrderer(orderer); // Configure the KeyValStore which is used to store sensitive keys // check that the ./tmp directory existsSync @@ -183,11 +185,13 @@ HFC.newDefaultKeyValueStore({ debug('Finished initializing users'); debug(results); - return client.setUserContext(member); + return client.getUserContext('peerorg1Admin', true); }) -.then(function() { +.then(function(user) { debug('Channel information ---------------------'); + debug('User name ', user.getName()); + debug(user.getIdentity()); let promises = []; targets.forEach(function(target) { promises.push(client.queryChannels(target)); @@ -201,8 +205,8 @@ HFC.newDefaultKeyValueStore({ }); debug('-----------------------------------------'); - debug('calling chain initialize'); - return chain.initialize(); + debug('calling channel initialize'); + return channel.initialize(); }) .then(function(result) { debug('initialize success', result); @@ -224,7 +228,7 @@ HFC.newDefaultKeyValueStore({ debug('Failed registration or initialization', err); }); -exports.chain = chain; +exports.channel = channel; exports.GlobalAppUser = GlobalAppUser; exports.poeChaincode = poeChaincode; exports.setPoeChaincode = setPoeChaincode; diff --git a/package.json b/package.json index d328b0b..0930c5c 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,8 @@ "debug": "^2.2.0", "express": "^4.13.4", "express-session": "^1.15.1", - "fabric-ca-client": "1.0.0-alpha2", - "fabric-client": "1.0.0-alpha2", + "fabric-ca-client": "1.0.0-beta", + "fabric-client": "1.0.0-beta", "grpc": ">=1.1.2 <1.3.0", "js-sha256": "^0.5.0", "morgan": "^1.7.0", diff --git a/sdkInterface.js b/sdkInterface.js index 389f644..a66ca5e 100644 --- a/sdkInterface.js +++ b/sdkInterface.js @@ -40,11 +40,11 @@ function deploy(req, res) { var ccName = 'poe' + '-' + Math.floor(Math.random() * 1000); var deployUser; var client = init.client; - var chain = init.chain; + var channel = init.channel; var cred = init.cred; - var targets = chain.getPeers(); + var targets = channel.getPeers(); var store = client.getStateStore(); - debug('primary peer is ', chain.getPrimaryPeer()); + debug('The first peer is ', targets[0]); client.getUserContext('peerorg1Admin', true) .then(function(user) { @@ -52,17 +52,12 @@ function deploy(req, res) { debug('set the client user context'); debug(user); - let nonce = utils.getNonce(); - let txId = hfc.buildTransactionID(nonce, deployUser); - // send proposal to endorser let request = { targets: targets, chaincodePath: chaincodePath, chaincodeId: ccName, chaincodeVersion: ccVersion, - txId: txId, - nonce: nonce }; debug('install chaincode request'); debug(request); @@ -83,29 +78,25 @@ function deploy(req, res) { } } - return chain.initialize(); + return channel.initialize(); }) .then(function(results) { - console.log('chain initialize result ', results); + console.log('channel initialize result ', results); - let nonce = utils.getNonce(); - let txId = hfc.buildTransactionID(nonce, deployUser); + let txId = client.newTransactionID(); // send proposal to endorser let request = { targets: targets, - chaincodePath: chaincodePath, chaincodeId: ccName, chaincodeVersion: ccVersion, fcn: 'init', args: [], - chainId: config.channelId, txId: txId, - nonce: nonce }; debug('====== Instantiate Proposal request ==========='); debug(request); - return chain.sendInstantiateProposal(request); + return channel.sendInstantiateProposal(request); }) .then(function(results) { let proposalResponses = results[0]; @@ -123,7 +114,7 @@ function deploy(req, res) { proposal: proposal, header: header }; - return chain.sendTransaction(request); + return channel.sendTransaction(request); }) .then(function(results) { debug('====== sendTransaction results ==========='); @@ -154,11 +145,11 @@ function deploy(req, res) { // create a promise for the chaincode Invoke so it can be easily retried function sdkInvoke(user, invokeRequest) { var client = init.client; - var chain = init.chain; + var channel = init.channel; var cred = init.cred; var eventhub; var ehtxid; - var targets = chain.getPeers(); + var targets = channel.getPeers(); var store = client.getStateStore(); debug(invokeRequest); @@ -179,9 +170,8 @@ function sdkInvoke(user, invokeRequest) { eventhub.connect(); // jscs:enable requireCamelCaseOrUpperCaseIdentifiers - let nonce = utils.getNonce(); - let txId = hfc.buildTransactionID(nonce, user); - ehtxid = txId.toString(); + let txId = client.newTransactionID(); + ehtxid = txId.getTransactionID(); // debug('the user context is ', client.getUserContext()); let ccID = {}; debug(invokeRequest); @@ -194,10 +184,9 @@ function sdkInvoke(user, invokeRequest) { args: invokeRequest.args, chainId: config.channelId, txId: txId, - nonce: nonce }; debug(request); - return chain.sendTransactionProposal(request); + return channel.sendTransactionProposal(request); }) .then(function(results) { let proposalResponses = results[0]; @@ -216,14 +205,14 @@ function sdkInvoke(user, invokeRequest) { let ehPromise = new Promise(function(resolve, reject) { let handle = setTimeout(function() { - if (!config.windows) {eventhub.unregisterTxEvent(ehtxid);} + eventhub.unregisterTxEvent(ehtxid); reject(new Error('Event hub timed out.')); }, 30000); debug('registering for the Tx event'); eventhub.registerTxEvent(ehtxid, function(txid, code) { clearTimeout(handle); - if (!config.windows) {eventhub.unregisterTxEvent(txid);} + eventhub.unregisterTxEvent(txid); if (code !== 'VALID') { debug('Transaction failed event hub reported:', code); @@ -241,7 +230,7 @@ function sdkInvoke(user, invokeRequest) { header: header }; debug('====== sending transaction ========='); - return Q.allSettled([chain.sendTransaction(request), ehPromise]); + return Q.allSettled([channel.sendTransaction(request), ehPromise]); }) .then(function(results) { debug('====== sendTransaction results ==========='); @@ -251,13 +240,13 @@ function sdkInvoke(user, invokeRequest) { return Q.reject(results[i].reason); } } - if (!config.windows) {eventhub.disconnect();} + eventhub.disconnect(); return Q.resolve(results); }) .catch(function(err) { debug('====== invoke failed ==========='); debug(err); - if (!config.windows) {eventhub.disconnect();} + eventhub.disconnect(); return Q.reject(err); }); } @@ -283,31 +272,26 @@ function retrySdkInvoke(user, invokeRequest, maxRetries) { // create a promise for the chaincode query so it can be easily retried function sdkQuery(user, queryRequest, maxRetries) { var client = init.client; - var chain = init.chain; - var target = chain.getPrimaryPeer(); + var channel = init.channel; var store = client.getStateStore(); + var targets = channel.getPeers(); return client.setUserContext(user, true) .then(function(user) { - let nonce = utils.getNonce(); - let txId = hfc.buildTransactionID(nonce, user); let ccID = {}; ccID = queryRequest.chaincodeID; let request = { - targets: target, + targets: targets[0], chaincodeId: ccID.chaincodeId, - chaincodeVersion: ccID.chaincodeVersion, fcn: queryRequest.fcn, args: queryRequest.args, - chainId: config.channelId, - txId: txId, - nonce: nonce + channelId: config.channelId, }; debug(request); - return chain.queryByChaincode(request); + return channel.queryByChaincode(request); }) .then(function(results) { debug('====== queryByChaincode results =========='); diff --git a/test/fixtures-V1/docker-compose-template.yaml b/test/fixtures-V1/docker-compose-template.yaml index ba1a1bc..446c4c8 100644 --- a/test/fixtures-V1/docker-compose-template.yaml +++ b/test/fixtures-V1/docker-compose-template.yaml @@ -2,7 +2,7 @@ version: '2' services: ca.blocledger.com: - image: hyperledger/fabric-ca:x86_64-1.0.0-alpha2 + image: hyperledger/fabric-ca:x86_64-1.0.0-beta environment: - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server - FABRIC_CA_SERVER_TLS_ENABLED=true @@ -19,7 +19,7 @@ services: orderer.blocledger.com: container_name: orderer.blocledger.com - image: hyperledger/fabric-orderer:x86_64-1.0.0-alpha2 + image: hyperledger/fabric-orderer:x86_64-1.0.0-beta environment: - ORDERER_GENERAL_LOGLEVEL=debug - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0 @@ -45,7 +45,7 @@ services: peer0.org1.blocledger.com: container_name: peer0.org1.blocledger.com - image: hyperledger/fabric-peer:x86_64-1.0.0-alpha2 + image: hyperledger/fabric-peer:x86_64-1.0.0-beta environment: - CORE_PEER_ID=peer0.org1.blocledger.com - CORE_PEER_ADDRESS=peer0.org1.blocledger.com:7051 @@ -83,7 +83,7 @@ services: peer1.org1.blocledger.com: container_name: peer1.org1.blocledger.com - image: hyperledger/fabric-peer:x86_64-1.0.0-alpha2 + image: hyperledger/fabric-peer:x86_64-1.0.0-beta environment: - CORE_PEER_ID=peer1.org1.blocledger.com - CORE_PEER_ADDRESS=peer1.org1.blocledger.com:7051 diff --git a/util.js b/util.js index 1f228c2..2325998 100644 --- a/util.js +++ b/util.js @@ -22,6 +22,7 @@ var util = require('util'); var config = require('./configuration.js'); var User = require('fabric-client/lib/User.js'); +var HFC = require('fabric-client'); var init = require('./initialize.js'); // jscs:disable requireCamelCaseOrUpperCaseIdentifiers @@ -68,7 +69,8 @@ function newUser(appUser) { debug('user enrollment ', enrollment); user = new User(appUser.userName, client); - var cryptoSuite = client.newCryptoSuite({path: config.keyPath}); + var cryptoSuite = HFC.newCryptoSuite(); + cryptoSuite.setCryptoKeyStore(HFC.newCryptoKeyStore({path: config.keyPath})); user.setCryptoSuite(cryptoSuite); return user.setEnrollment(enrollment.key, enrollment.certificate, mspid); })