Exploring the structure of a sample blockchain network

  1. Let's start with the name.cto model file. Model files define the assets, participants, transactions, and events in our business network. Remember that, after each step, you need to deploy the changes. Now we'll see some screenshots that will illustrate the process:

  1. Let's use the following code to create the participants, transactions, and events:
// **
* Sample business network definition.
*/
namespace org.example.basic

asset SampleAsset identified by assetId {
o String assetId
--> SampleParticipant owner
o Double value
}

participant SampleParticipant identified by participantId {
o String participantId
o String firstName
o String lastName
}

transaction SampleTransaction {
--> SampleAsset origin
--> SampleAsset target
o Double txTransferAmount
}

event SampleEvent {
--> SampleAsset origin
--> SampleAsset target
o Double txTransferAmount
}
  1. Let's create a function to transfer the assets between the participants. We'll use the name.js script file:

  1. Let's look at the code that shows the algorithm/logic used here:
 * Sample transaction processor function.
* @param {org.example.basic.SampleTransaction} tx The sample transaction instance.
* @transaction
*/
async function sampleExchange(tx) {
// Get the asset registry for the asset.
const assetRegistry = await getAssetRegistry('org.example.basic.SampleAsset');

//Ensure the balance is greather than the amount to be transfered
if(tx.origin.value > tx.txTransferAmount) {

//charge from receiver account
tx.origin.value = (tx.origin.value - tx.txTransferAmount);

//add to receiver account
tx.target.value = (tx.target.value + tx.txTransferAmount);

// Update the asset in the asset registry.
await assetRegistry.update(tx.origin);
await assetRegistry.update(tx.target);

// Emit an event for the modified asset.
let event = getFactory().newEvent('org.example.basic', 'SampleEvent');

event.origin = tx.origin;
event.target = tx.target;
event.txTransferAmount = tx.txTransferAmount;

emit(event);

} else {
throw Error(`You do not have enough balance for this transaction: Balance US$: ${tx.origin.value} Transfer Amount: ${tx.txTransferAmount}`);
}
}
  1. The Access Control List (ACL) is the feature that ensures a Hyperledger Composer blockchain network segregates access for the actions that participants can take on the assets. Now we'll create a business rule to allow the members of the blockchain network to have the right access control. The basic file gives the current participant, the network admin, full access to the business network and system-level operations:

Here we have some code that shows us how to create an access control:

/**
* Sample access control list. rule Everybody Can Read Everything and send a transaction for example
*/
rule EverybodyCanReadEverything {
description: "Allow all participants read access to all resources"
participant: "org.example.basic.SampleParticipant"
operation: READ
resource: "org.example.basic.*"
action: ALLOW
}
rule EverybodyCanSubmitTransactions {
description: "Allow all participants to submit transactions"
participant: "org.example.basic.SampleParticipant"
operation: CREATE
resource: "org.example.basic.SampleTransaction"
action: ALLOW
}
  1. Define access to the access control's assets as follows:
rule OwnerHasFullAccessToTheirAssets {
description: "Allow all participants full access to their assets"
participant(p): "org.example.basic.SampleParticipant"
operation: ALL
resource(r): "org.example.basic.SampleAsset"
condition: (r.owner.getIdentifier() === p.getIdentifier())
action: ALLOW
}
  1. Define a rule for SystemACL, whether network admin or user, as follows:

rule SystemACL {
description: "System ACL to permit all access"
participant: "org.hyperledger.composer.system.Participant"
operation: ALL
resource: "org.hyperledger.composer.system.**"
action: ALLOW
}
rule NetworkAdminUser {
description: "Grant business network administrators full access to user resources"
participant: "org.hyperledger.composer.system.NetworkAdmin"
operation: ALL
resource: "**"
action: ALLOW
}
rule NetworkAdminSystem {
description: "Grant business network administrators full access to system resources"
participant: "org.hyperledger.composer.system.NetworkAdmin"
operation: ALL
resource: "org.hyperledger.composer.system.**"
action: ALLOW
}
  1. We're now ready to test our blockchain network. Click on the Test tab at the top of your screen:

  1. Now create two participants for your blockchain network. The first participant is shown here:

 

Use the following code to create the first participant:

{
"$class": "org.example.basic.SampleParticipant",
"participantId": "1",
"firstName": "Joao",
"lastName": "Dow"
}

The second participant is shown here:

Use the following code to create the second participant:

{
"$class": "org.example.basic.SampleParticipant",
"participantId": "2",
"firstName": "Sarah",
"lastName": "Barbosa"
}
  1. Now let's create an asset for participant 1. Remember to add participantIdassetId, and value:

  1. Use the following code to create an asset for participant 1:
{
"$class": "org.example.basic.SampleAsset",
"assetId": "0744",
"owner": "resource:org.example.basic.SampleParticipant#1",
"value": 1000
}
  1. Repeat the approach used for participant 1 with participant 2:

  1. Use the following code to create an asset for participant 2:
{
"$class": "org.example.basic.SampleAsset",
"assetId": "4010",
"owner": "resource:org.example.basic.SampleParticipant#2",
"value": 1000
}
  1. We're now ready to submit a transaction between the participants. Click on the Submit button and send an amount from participant 2 to participant 1. In the following example, the value of the transaction is 300:

  1. Use the following code to transfer an amount between the participants:
{
"$class": "org.example.basic.SampleTransaction",
"origin": "resource:org.example.basic.SampleAsset#0744",
"target": "resource:org.example.basic.SampleAsset#4010",
"txTransferAmount": 300
}

Great job! You can see all of the transaction details by clicking on the records in the two following screenshots. The first one shows a list of all created assets:

This second screenshot shows the history of transactions that have run on the blockchain network:

Now that you have validated a use case, you're ready to make a new proof of concept and present the full potential of the Hyperledger blockchain for members in the business network.

..................Content has been hidden....................

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