5

Right now I have a sequence if ids used for mapping in contract:

mapping (uint => Document) documents;
uint documentsCount;

which is incremented for every new document:

function createDocument() returns (uint docId) {
    docId = documentsCount++;
    documents[docId] = Document(...);
    Created(msg.sender, docId);
}

When I make this call I receive a transaction id (tx_id), which is uniq and finalized if I understand correctly. Also I can get docId if I listen for Created events.

This docId depends on execution order, and in distributed systems such as Ethereum, we should wait until it's finalized in blockchain.

I wonder what should I do if I want to use it as a reference in further transactions? Say I have addSignature(uint docId, bytes sign) transaction next to it. Or simple read, like getState(unit docId).

If I call it just after first transaction, it still possible that in blockchain it will be executed against different block and wrong document. Or what if pass this docId to a different machine which can have slightly different order or transactions at this moment.

What is a best practice to generate a reference id at this case?

Igor Artamonov
  • 679
  • 1
  • 6
  • 10

1 Answers1

2

One idea could be to use sha3 (of the document) for docId.

The mapping would become: mapping (bytes32 => Document) documents;

sha3 is the cheapest hash function.

eth
  • 85,679
  • 53
  • 285
  • 406