Is there a deterministic way to get timestamp in transaction function, similar to stub.GetTxTimestamp() that can be used in Go version of Fabric's chaincode.
Asked
Active
Viewed 2,365 times
3 Answers
3
Just sharing an example that works with basic-sample-network
network:
In the model file (lib/org.acme.sample.cto
) I extended SampleAsset
definition any added new property called timestamp
of type DateTime
:
asset SampleAsset identified by assetId {
o String assetId
--> SampleParticipant owner
o String value
o DateTime timestamp
}
In the script file (lib/logic.js
), the onSampleTransaction
function to update SampleAsset's timestamp
with current transaction's timestamp:
function onSampleTransaction(sampleTransaction) {
sampleTransaction.asset.value = sampleTransaction.newValue;
sampleTransaction.asset.timestamp = sampleTransaction.timestamp;
return getAssetRegistry('org.acme.sample.SampleAsset')
.then(function (assetRegistry) {
return assetRegistry.update(sampleTransaction.asset);
});
}

Nikolay Vlasov
- 71
- 6
-
But it's possible that some malevolent or malfunctioning node could stamp a wrong timestamp? – TortelliEngineer Nov 28 '18 at 06:10
1
All transactions have a system property called timestamp
, so you can use myTransaction.timestamp
.

Dan Selman
- 2,287
- 2
- 9
- 18
-
Thanks a lot for response, Dan! Here is the sample that works for basic-sample-network (assuming that asset "SampleAsset" has the property called "timestamp" that we want to update with the following transaction "SampleTransaction"): `function onSampleTransaction(sampleTransaction) { sampleTransaction.asset.value = sampleTransaction.newValue; sampleTransaction.asset.timestamp = sampleTransaction.timestamp; return getAssetRegistry('org.acme.sample.SampleAsset') .then(function (assetRegistry) { return assetRegistry.update(sampleTransaction.asset); }); }` – Nikolay Vlasov Apr 28 '17 at 04:44
-
https://github.com/hyperledger-archives/fabric/issues/1832 we cannot use the proto from the vendor folder ... – Benjamin Fuentes Feb 08 '18 at 14:46