Skip to content

Commit

Permalink
TxID compute with nonce + creator
Browse files Browse the repository at this point in the history
Chain.js needs to build the transaction id
using nonce and creator to match fabric
change 5945.

Patch set 1:  Only implemented for
sendInstallProposal used by end-to-end.js step1.
Patch set 2:  Implemented all txId's, but could
not test.  See 'to do' line 1059 - need to get
nonce from transaction id?
Patch set 3: TxId is working with fix from Angelo.
Tested successfully with end-to-end.js step1.

Change-Id: Ie9ec9f68a1973269d10fa29fd98578bc0d8d0902
Signed-off-by: cdaughtr <cdaughtr@us.ibm.com>
  • Loading branch information
cdaughtr authored and jimthematrix committed Feb 19, 2017
1 parent 6ceccd0 commit 2b5907c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 41 deletions.
121 changes: 87 additions & 34 deletions fabric-client/lib/Chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,16 +871,21 @@ var Chain = class {
*/
queryInfo() {
logger.debug('queryInfo - start');
var request = {
targets: [this.getPrimaryPeer()],
chaincodeId : 'qscc',
chainId: '',
txId: utils.buildTransactionID(),
nonce: utils.getNonce(),
fcn : 'GetChainInfo',
args: [ this._name]
};
return this.sendTransactionProposal(request)
var self = this;
var nonce = utils.getNonce();
return this.buildTransactionID_getUserContext(nonce)
.then(function(txId) {
var request = {
targets: [self.getPrimaryPeer()],
chaincodeId : 'qscc',
chainId: '',
txId: txId,
nonce: nonce,
fcn : 'GetChainInfo',
args: [ self._name]
};
return self.sendTransactionProposal(request);
})
.then(
function(results) {
var responses = results[0];
Expand Down Expand Up @@ -923,17 +928,22 @@ var Chain = class {
if(!blockHash) {
return Promise.reject( new Error('Blockhash bytes are required'));
}
var request = {
targets: [this.getPrimaryPeer()],
chaincodeId : 'qscc',
chainId: '',
txId: utils.buildTransactionID(),
nonce: utils.getNonce(),
fcn : 'GetBlockByHash',
args: [ this._name],
argbytes : blockHash
};
return this.sendTransactionProposal(request)
var self = this;
var nonce = utils.getNonce();
return this.buildTransactionID_getUserContext(nonce)
.then(function(txId) {
var request = {
targets: [self.getPrimaryPeer()],
chaincodeId : 'qscc',
chainId: '',
txId: txId,
nonce: nonce,
fcn : 'GetBlockByHash',
args: [ self._name],
argbytes : blockHash
};
return self.sendTransactionProposal(request);
})
.then(
function(results) {
var responses = results[0];
Expand Down Expand Up @@ -980,16 +990,21 @@ var Chain = class {
} else {
return Promise.reject( new Error('Block number must be a postive integer'));
}
var request = {
targets: [this.getPrimaryPeer()],
chaincodeId : 'qscc',
chainId: '',
txId: utils.buildTransactionID(),
nonce: utils.getNonce(),
fcn : 'GetBlockByNumber',
args: [ this._name, block_number]
};
return this.sendTransactionProposal(request)
var self = this;
var nonce = utils.getNonce();
return this.buildTransactionID_getUserContext(nonce)
.then(function(txId) {
var request = {
targets: [self.getPrimaryPeer()],
chaincodeId : 'qscc',
chainId: '',
txId: txId,
nonce: nonce,
fcn : 'GetBlockByNumber',
args: [ self._name, block_number]
};
return self.sendTransactionProposal(request);
})
.then(
function(results) {
var responses = results[0];
Expand Down Expand Up @@ -1040,8 +1055,8 @@ var Chain = class {
targets: [this.getPrimaryPeer()],
chaincodeId : 'qscc',
chainId: '',
txId: utils.buildTransactionID(),
nonce: utils.getNonce(),
txId: transactionID,
nonce: utils.getNonce(),//to do - get nonce from transaction id
fcn : 'GetTransactionByID',
args: [ this._name, transaction_id]
};
Expand Down Expand Up @@ -1190,10 +1205,11 @@ var Chain = class {
return self._clientContext.getUserContext()
.then(
function(userContext) {
var txId = self.buildTransactionID(request.nonce, userContext);
var channelHeader = buildChannelHeader(
_commonProto.HeaderType.ENDORSER_TRANSACTION,
'', //install does not target a channel
request.txId,
txId,
null,
'lccc'
);
Expand Down Expand Up @@ -1708,6 +1724,43 @@ var Chain = class {
return errorMsg;
}

/**
* Utility method to build an unique transaction id
* based on a nonce and this chain's user.
* @param {int} nonce - a one time use number
* @param {User} userContext - the user context
* @returns {string} An unique string
*/
buildTransactionID(nonce, userContext) {
logger.debug('buildTransactionID - start');
var creator_bytes = userContext.getIdentity().serialize();//same as signatureHeader.Creator
var nonce_bytes = nonce;//nonce is already in bytes
var trans_bytes = Buffer.concat([nonce_bytes, creator_bytes]);
var trans_hash = this.cryptoPrimitives.hash(trans_bytes);
var transaction_id = Buffer.from(trans_hash).toString();
logger.debug('buildTransactionID - transaction_id %s',transaction_id);
return transaction_id;
}

/**
* Utility method to build an unique transaction id
* based on a nonce and this chain's user.
* Gets the user context.
* @param {int} nonce - a one time use number
* @returns {Promise} A promise for the transaction id
*/
buildTransactionID_getUserContext(nonce) {
return this._clientContext.getUserContext()
.then((userContext) => {
logger.debug('buildTransactionID_getUserContext - got userContext');
return this.buildTransactionID(nonce, userContext);
})
.catch(function(error) {
logger.debug('buildTransactionID_getUserContext - caught error ::' + error.stack ? error.stack : error);
return Promise.reject(new Error(error));
});
}

/**
* return a printable representation of this object
*/
Expand Down
2 changes: 1 addition & 1 deletion test/integration/chain-fabriccop-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var utils = require('fabric-client/lib/utils.js');
var User = require('fabric-client/lib/User.js');
var Client = require('fabric-client/lib/Client.js');

var testUtil = require('./util.js');
var testUtil = require('../unit/util.js');

var keyValStorePath = testUtil.KVS;

Expand Down
18 changes: 13 additions & 5 deletions test/integration/end-to-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ test('End-to-end flow of chaincode install, deploy, transaction invocation, and
t.end();
}).then((nothing) => {
t.pass('Successfully waited on timer');
if (useSteps) t.end();
},
(err) => {
t.fail('Failed to wait on timer: ' + err.stack ? err.stack : err);
Expand Down Expand Up @@ -332,7 +333,7 @@ test('End-to-end flow of chaincode install, deploy, transaction invocation, and
t.fail('Failed to send transaction proposal due to error: ' + err.stack ? err.stack : err);
t.end();
}).then((response) => {
if (response.status === 'SUCCESS') {
if (response && response.status === 'SUCCESS') {
t.pass('Successfully ordered endorsement transaction.');
} else {
t.fail('Failed to order the endorsement of the transaction. Error code: ' + response.status);
Expand Down Expand Up @@ -363,13 +364,19 @@ test('End-to-end flow of chaincode install, deploy, transaction invocation, and
return chain.queryByChaincode(request);
},
(err) => {
t.fail('Failed to get transaction notification within the timeout period');
t.comment('Failed to get transaction notification within the timeout period. exiting...');
t.fail('Error: ' + err.stack ? err.stack : err );
t.end();
}).then((response_payloads) => {
for(let i = 0; i < response_payloads.length; i++) {
t.equal(response_payloads[i].toString('utf8'),'300','checking query results are correct that user b has 300 now after the move');
if (response_payloads) {
for(let i = 0; i < response_payloads.length; i++) {
t.equal(response_payloads[i].toString('utf8'),'300','checking query results are correct that user b has 300 now after the move');
}
t.end();
} else {
t.fail('response_payloads is null');
t.end();
}
t.end();
},
(err) => {
t.fail('Failed to send query due to error: ' + err.stack ? err.stack : err);
Expand All @@ -380,6 +387,7 @@ test('End-to-end flow of chaincode install, deploy, transaction invocation, and
});
}
});
t.end();
});

function sleep(ms) {
Expand Down
1 change: 0 additions & 1 deletion test/unit/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,6 @@ test('\n\n ** Chain sendInstallProposal() tests **\n\n', function (t) {
chaincodePath: 'blah',
chaincodeId: 'blah',
chaincodeVersion: 'blah',
chainId: 'blah',
txId: 'blah',
nonce: 'blah'
}).then(function () {
Expand Down

0 comments on commit 2b5907c

Please sign in to comment.