Manipulating assets via transactions in the blockchain

Transactions are atomic operations that are performed on objects inside a Hyperledger Composer-defined business network. They run on the scope of the Hyperledger Fabric environment and the defined business network.

In the use case that's demonstrated here, the transaction that we've created will update both the pallet and the nested food boxes with the information provided by the IoT device.

It is composed of two structures. The first one is the definition of the transaction and is created in the business network definition model (the .cto file):

transaction updateTransportationData {
--> FoodBoxPallet pallet
o Location locationInformation
o Measurement measurementInformation
}

The next structure is the function that implements the transaction that was defined previously and is created in a JavaScript ES5 compliant script (a .js file):

/**
* Update pallets and boxes with measurements function.
* @param {com.packtpublishing.businessnetwork.foodsafety.UpdateTransportationData} tx Update pallets and boxes with measurements.
* @transaction
*/
async function updateTransportationData(tx) {

// Get transaction parametes
let newValue = tx.asset;
let location = tx.locationInformation;
let measurement = tx.measurementInformation;

// Update Pallet data with measurements
if( !newValue.assetTrackingInformation || newValue.assetTrackingInformation == undefined)
newValue.assetTrackingInformation = [];
if ( !newValue.measureTrackingInformation || newValue.measureTrackingInformation == undefined)
newValue.measureTrackingInformation = [];

newValue.assetTrackingInformation.push(location);
newValue.measureTrackingInformation.push(measurement);

// Update Boxes data with measurements
let foodBox = newValue.foodBoxInPallet;
if( !foodBox.assetTrackingInformation || foodBox.assetTrackingInformation == undefined)
foodBox.assetTrackingInformation = [];

if ( ! foodBox.measureTrackingInformation || foodBox.measureTrackingInformation == undefined)
foodBox.measureTrackingInformation = [];

foodBox.assetTrackingInformation.push(location);
foodBox.measureTrackingInformation.push(measurement);

// Get the asset registry for both assets.
let assetRegistryFoodBoxPallet = await getAssetRegistry('com.packtpublishing.businessnetwork.foodsafety.FoodBoxPallet');
let assetRegistryFoodBox = await getAssetRegistry('com.packtpublishing.businessnetwork.foodsafety.FoodBox');

// Update the assets in the asset registry.
await assetRegistryFoodBoxPallet.update(newValue);
await assetRegistryFoodBox.update(foodBox);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.35.77