简体   繁体   中英

Should each asset have its own chaincode in Fabric?

The Chaincode for Developers tutorial uses only one chaincode.

I know multiple chaincode is supported as well, and the aforementioned tutorial uses a chaincode called SimpleAsset .

Does it imply that there is an expectation to have 1 chaincode for 1 asset? If my application involves several asset types (eg. Widgets and Gadgets), is the best practice to have a separate chaincode managing the lifecycle of each asset type? Or a single chaincode?

It is possible to have one single chaincode to manage multiple assets. There are no fixed rule on how to map assets to chaincode, but some guidelines I found useful are:

  • If the assets are tightly coupled I prefer to have a single chaincode to manage them, specially if the life cycle of one asset depends on the lyfe cycle of another asset.

  • If you expect for a particular asset changes in its logic and life cycle then it is best to isolate it in its own chaincode. The idea is to keep assets that rarely will have its behavior changed separated from assets that would require changes in its chaincode from time to time.

  • Try to keep your chaincodes small. This one is mostly a personal preference but it is also related to the previous point.

  • If you have a small number of assets with not too much logic it might make sense to have a single chaincode.

Hope you find this useful.

There is one thing which need to keep in mind. Once you are going to maintain all you assets within single chaincode namespace, you need to be able to distinguish between different type of assets in some way, such that you will be able to search for specific type of asset given its ID. This basically will eventually force you to extend your document model to include type information or to incorporate that knowledge into the key, making it easier to differentiate between types of assets/documents.

Now, thinking on this, this is in a sense has same effect as splitting assets management into separate chaincodes. Moreover splitting management or maintenance of different type of assets with different chaicodes will facilitate better separation of concerns which in general better design practice.

Doing small PoC or playing around with platform it make a lot of sense to put everything into one chaincode, while speaking long term and keeping in mind that you need eventually to support your own chaincode code base, I'd suggest to split up chaincodes whenever it make sense and I think managing several assets is a good candidate.

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