Stanley_A
Stanley_A

Reputation: 448

How could I get unique IDs in Hyperledger Fabric

Given that Hyperledger Fabric's chaincode needs to be deterministic because it's being executed on all validating peers (Are blocks mined in HyperLedger Fabric?), how would one get a unique ID so I can 'InsertRow' with a unique value.

For example, if I execute my code to append a new record to the table, I'd need a unique key. If I get a GUID on Validating Peer 1 (vp1), it would be a different key if I got a GUID on Validating Peer 2 (vp2). The same if I used milliseconds as a key.

Is there a way I can get a deterministic unique ID in chaincode from within the chaincode rather than passing it in from the 'client'?

Upvotes: 3

Views: 2313

Answers (2)

zmanian
zmanian

Reputation: 426

If the ID should be generated by the chaincode, then a monotonically increasing counter it a good solution. If the ID should be chosen by the tx generating app, you should enforce the rule the ID's can't reused and encourage the protocol the tx generator uses to select collision resistant ids.

Upvotes: 0

Gregory Haskins
Gregory Haskins

Reputation: 311

I would be inclined to implement this as a monotonically increasing sequence variable stored in putstate along side your table. IOW, initialize something like PutState("nextsequence", 0) in your Init() function, and then RMW that any time you need a new id. The RMW mutation will be implicitly coupled to your row insert, and should be deterministic across all instances.

Upvotes: 4

Related Questions