How to have monotonically increasing counter for global unique ID ? #fabric-questions


pham.qtpham@...
 

Hi,

In my app for Supply Chain Management, there is equipmentNumber, which should be a global unique ID. How can I achieve this using Javascript?

E.g. the smart contract in Javascript as below:

...

'use strict';

// Fabric smart contract classes

const { Contract, Context } = require('fabric-contract-api');

 

/**

 * Define smart contract by extending Fabric Contract class

 *

 */

class PharmaLedgerContract extends Contract {

 

    constructor() {

        // Unique namespace pcn - PharmaChainNetwork when multiple contracts per chaincode file

        super('org.pln.PharmaLedgerContract');

    }

    /**

     * Instantiate to set up ledger.

     * @param {Context} ctx the transaction context

     */

    async instantiate(ctx) {

        // No default implementation for this example

        console.log('Instantiate the PharmaLedger contract');

    }

 

    /**

     * Create pharma equipment

     *

     * @param {Context} ctx the transaction context

     * @param {String} equipment manufacturer

     * @param {String} equipmentNumber for this equipment

     * @param {String} equipment name

     * @param {String} equipment commercial name

     * @param {String} equipment composition

     * @param {String} equipment common side effects

     * @param {String} equipment cost

     * @param {String} equipment retailed cost

     * @param {String} equipment quantity

     * @param {String} name of the equipment owner

    */

   async makeEquipment(ctx, manufacturer, equipmentNumber, equipmentName, equipmentCommercialName, equipmentComposition, equipmentCommonSideEffects, equipmentCost, equipmentQuantity, equipmentRetailedCost,ownerName) {

        console.info('============= START : makeEquipment call ===========');

        let dt = new Date().toString();

        const equipment = {

            equipmentNumber,

            manufacturer,

            equipmentName,

            equipmentCommercialName,

            equipmentComposition,

            equipmentCommonSideEffects,

            equipmentCost,

            equipmentQuantity,

            equipmentRetailedCost,

            equipmentTotalCost: '0',

            previousQuantityBalance: '0',

            currentQuantityBalance: '0',

            ownerName,

            previousOwnerType: 'MANUFACTURER',

            currentOwnerType: 'MANUFACTURER',

            createDateTime: dt,

            lastUpdated: dt

        };

        await ctx.stub.putState(equipmentNumber, Buffer.from(JSON.stringify(equipment)));

        console.info('============= END : Create equipment ===========');

   }

 


Matthew White
 

Hello;
 
There are two options I'm aware of here. .. 
 
1) Generate client side a UUID that is used as the equipment number. It's not exactly what you asked for you but it widely used approach to getting an id for identifying 'things'
2) It's possible to have a single key that contains a counter that is incremented per transaction. The (big) downside of this is that only one transaction can be handled at once. If there are multiple transactions in flight that access the same keys, then this will get MVCC conflicts resulting in transactions failing.  Of course, if this is a rare case then this could be acceptable.
 
If you have multiple endorsers for the transaction this potentially gets a lot worse. Also, the exact clock time they are executed will vary.  So it's not 
a good idea to have the current timestamp accessed in chain code. 
 
Hope that helps. 
 
Regards, Matthew.
Matthew B White  IBM Blockchain Solutions Architect
 
Email me at WHITEMAT@...
Find me on StackOverflow, and generally at  calanais.me.uk
 
Note: restricted availability for meetings 14:30 to 17:00 UK Tuesday 
IBM United Kingdom Limited, Hursley Park, Winchester, Hampshire, SO21 2JN

"The wrong answers are the ones you go looking for when the right answers stare you in the face"
 
 
 
----- Original message -----
From: pham.qtpham@...
Sent by: fabric@...
To: fabric@...
Cc:
Subject: [EXTERNAL] [Hyperledger Fabric] How to have monotonically increasing counter for global unique ID ? #fabric-questions
Date: Mon, Nov 1, 2021 3:57 AM
 
Hi, In my app for Supply Chain Management, there is equipmentNumber, which should be a global unique ID. How can I achieve this using Javascript? E.g. the smart contract in Javascript as below: ... 'use strict'; // Fabric smart contract classes ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.
ZjQcmQRYFpfptBannerEnd
Hi,

In my app for Supply Chain Management, there is equipmentNumber, which should be a global unique ID. How can I achieve this using Javascript?

E.g. the smart contract in Javascript as below:

...

'use strict';

// Fabric smart contract classes

const { Contract, Context } = require('fabric-contract-api');

 

/**

 * Define smart contract by extending Fabric Contract class

 *

 */

class PharmaLedgerContract extends Contract {

 

    constructor() {

        // Unique namespace pcn - PharmaChainNetwork when multiple contracts per chaincode file

        super('org.pln.PharmaLedgerContract');

    }

    /**

     * Instantiate to set up ledger.

     * @param {Context} ctx the transaction context

     */

    async instantiate(ctx) {

        // No default implementation for this example

        console.log('Instantiate the PharmaLedger contract');

    }

 

    /**

     * Create pharma equipment

     *

     * @param {Context} ctx the transaction context

     * @param {String} equipment manufacturer

     * @param {String} equipmentNumber for this equipment

     * @param {String} equipment name

     * @param {String} equipment commercial name

     * @param {String} equipment composition

     * @param {String} equipment common side effects

     * @param {String} equipment cost

     * @param {String} equipment retailed cost

     * @param {String} equipment quantity

     * @param {String} name of the equipment owner

    */

   async makeEquipment(ctx, manufacturer, equipmentNumber, equipmentName, equipmentCommercialName, equipmentComposition, equipmentCommonSideEffects, equipmentCost, equipmentQuantity, equipmentRetailedCost,ownerName) {

        console.info('============= START : makeEquipment call ===========');

        let dt = new Date().toString();

        const equipment = {

            equipmentNumber,

            manufacturer,

            equipmentName,

            equipmentCommercialName,

            equipmentComposition,

            equipmentCommonSideEffects,

            equipmentCost,

            equipmentQuantity,

            equipmentRetailedCost,

            equipmentTotalCost: '0',

            previousQuantityBalance: '0',

            currentQuantityBalance: '0',

            ownerName,

            previousOwnerType: 'MANUFACTURER',

            currentOwnerType: 'MANUFACTURER',

            createDateTime: dt,

            lastUpdated: dt

        };

        await ctx.stub.putState(equipmentNumber, Buffer.from(JSON.stringify(equipment)));

        console.info('============= END : Create equipment ===========');

   }

 
 

Unless stated otherwise above:

IBM United Kingdom Limited - Registered in England and Wales with number 741598.

Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU