How can i get timestamp difference in DAML choice execution?

102 views Asked by At

I am trying to execute 2 choices one after the another. Both are executing so fast, that they have same timestamp.

timestamp = 1607079031453, Thus making it difficult to arrange via ascending order in a table.

Canyou suggest any work aroud for this?

2

There are 2 answers

0
bame On

getTime in DAML does not give you "system time", as there is no notion of system time on a distributed system. It gives you something called "Ledger Time" documented here: https://docs.daml.com/concepts/time.html

Ledger Time is specified by the submitting node, and a property of the entire transaction. That means all calls to getTime within a single transaction will return the same time.

If you create two identical contracts in a single transaction, there are only two ways to distinguish them:

  1. Position in the transaction tree
  2. Contract Id

Contract Id is a hash so gives you no useful ordering properties other than some value to order by stably. If you want to order by the order in which contracts were created, you need to use the position in the transaction tree.

I don't know where you store your data, or which API you use to store it there, but suppose you used a subscription to the Transaction Service, which returns Create events in order, and stored it to an SQL database, you can just put an auto-incrementing integer column on your table and use that integer to sort by.

0
stefanobaghino On

@bame's answer is mostly geared towards the DAML language, I'll explore it from the point of view of the Ledger API.

If your objective is to assess that one choice effectively occurred after the other and the two choices occur as part of different transactions you could use offsets for it.

Offsets are effectively an opaque binary blob from the client perspective, but they must be lexicographically comparable: take the two offsets and the lowest one will have occurred before the one with the higher offset.

Note that this only applies if the two choices were taken as part of two different transactions. If they occurred in the same transaction, the choice that occurred before will appear before as you traverse the transaction tree in preorder.