Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions libraries/fabric-shim/lib/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {msp, peer, common} = require('@hyperledger/fabric-protos');
const util = require('util');
const crypto = require('crypto');
const {ChaincodeEvent} = require('@hyperledger/fabric-protos/lib/peer');
const Long = require('long');

const logger = require('./logger').getLogger('lib/stub.js');

Expand Down Expand Up @@ -416,7 +417,10 @@ class ChaincodeStub {
* Object returned: { seconds: [Long] { low: [int32], high: [int32], unsigned: [bool] }, nanos: [int32] }
*/
getTxTimestamp() {
return this.txTimestamp;
return {
nanos: this.txTimestamp.getNanos(),
seconds: Long.fromNumber(this.txTimestamp.getSeconds(), true),
};
}

/**
Expand All @@ -425,8 +429,7 @@ class ChaincodeStub {
* client's date, and will have the same value across all endorsers.
*/
getDateTimestamp() {
const date = new Date(this.txTimestamp.seconds * 1e3 + this.txTimestamp.nanos / 1e6);
return date;
return this.txTimestamp.toDate();
}

/**
Expand Down
1 change: 1 addition & 0 deletions libraries/fabric-shim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"fabric-shim-api": "2.5.3",
"fast-safe-stringify": "^2.1.1",
"fs-extra": "^10.0.1",
"long": "^5.2.3",
"reflect-metadata": "^0.1.13",
"winston": "^3.7.2",
"yargs": "^17.4.0",
Expand Down
26 changes: 20 additions & 6 deletions libraries/fabric-shim/test/unit/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
const sinon = require('sinon');
const chai = require('chai');
chai.use(require('chai-as-promised'));
const { Timestamp } = require('google-protobuf/google/protobuf/timestamp_pb');
const Long = require('long');
// chai.config.truncateThreshold = 0;
const expect = chai.expect;
const rewire = require('rewire');
Expand Down Expand Up @@ -556,19 +558,31 @@ describe('Stub', () => {
describe('getTxTimestamp', () => {
it ('should return transaction timestamp', () => {
const stub = new Stub('dummyClient', 'dummyChannelId', 'dummyTxid', chaincodeInput);

stub.txTimestamp = 'some timestamp';

expect(stub.getTxTimestamp()).to.deep.equal('some timestamp');
const millis = Date.now();
const seconds = Math.trunc(millis / 1000);
const nanos = (millis - (seconds * 1000)) * 1e6;
const timestamp = new Timestamp();
timestamp.setSeconds(seconds);
timestamp.setNanos(nanos);
stub.txTimestamp = timestamp;

const actual = stub.getTxTimestamp();

expect(actual).to.deep.include({
nanos,
seconds: Long.fromNumber(seconds, true),
});
});
});

describe('getDateTimestamp', () => {
it ('should return transaction date as Node.js Date object', () => {
const stub = new Stub('dummyClient', 'dummyChannelId', 'dummyTxid', chaincodeInput);
stub.txTimestamp = {seconds: 1606233385, nanos: 54000000};
const now = new Date();
const timestamp = Timestamp.fromDate(now);
stub.txTimestamp = timestamp;

expect(stub.getDateTimestamp()).to.deep.equal(new Date(1606233385054));
expect(stub.getDateTimestamp().toISOString()).to.equal(now.toISOString());
});
});

Expand Down