简体   繁体   中英

How to generate new Id in hyperledger composer chaincode?

There is a method in a factory newResource . The third parameter of this method is id . Are there any ways or workarounds to generate id ?

You control what the id is generated as (deterministically) when you create the new Resource in the Resource registry (whether its an AssetRegistry or ParticipantRegistry etc) and depends on the id field type that identifies the asset/participant.

See sample networks for examples. Eg Perishables network:

https://github.com/hyperledger/composer-sample-networks/blob/v0.16.x/packages/perishable-network/lib/logic.js#L133

Here, Grower is created as a Resource using an email id as the identifier of the Resource.

see model here -> https://github.com/hyperledger/composer-sample-networks/blob/v0.16.x/packages/perishable-network/models/perishable.cto

here - the Participant Grower is extended from a generic Business participant that is identified by an email id (as it inherits that field from the supertype).

There was a similar question if you mean to auto-increment the identifier: Auto Increment field in Composer

It is not recommended by the devs, because different peers may have calculated the IDs at the same time for different new assets/participants and disagree. Disagreements would force all records to roll back.

Source: https://github.com/hyperledger/composer/issues/2931

This is a real problem and the Hyperledger documentation is not clear about which is the right approach.

When you create a new resource the third parameter id is mandatory and the transaction is rejected if it already exists another asset with the same id. So, how to fill the parameter id of newResource?

factory.newResource(ns, type, id) 

A solution could be counting the assets of the same type and generating a pseudo-incremental ID . But this should be used only for assets that are kinda unique on the blockchain. You should use this approach if you don't care of the rejection of the transaction by the peers; or maybe if the rejection is wanted behavior, because a deterministic unique id could work like a unique constraint (like in sql). A big flaw of "counting the assets" is the hyperledger query language doesn't do that, so you you have to use getAll method of the AssetRegistry and then get length of the array, which is not scalable for big arrays:

  let bananas_registry = await getAssetRegistry("org.exampleblockchain.Banana");
  let all_bananas = await bananas_registry.getAll();
  let new_banana_id = String(all_bananas.length + 1);

Another way to get unique ID is concatenate ID of parent assets .

But sometimes I also need something like a real random ID (not incremental). I do not understand why the is not the default for the parameter Id. Anyway, when I will need a real random ID, I will generate a long random string so the risk of collision with other peers will be low. I will let you know!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM