By Sai Sarath Chandra, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud's incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.
In the previous tutorial, we have set up a blockchain network through the Alibaba Cloud console and downloaded the Blockchain as a Service (BaaS) SDK. In this article, we will continue with the rest of the setup.
Now, we need to configure the connection parameters, since we are using the NodeJS SDK we will setup the connection parameters in the "connection-profile-standard" file, but we have two variants of the same file one is .yaml format and other In .json format. I chose to update the configuration in .json as that is relatively easy for me, but you can choose to update it in .yaml format
The username and password can be added to the JSON file in the certificateAuthorities section like below.
Below is the string that you can copy and paste
"registrar": [
{
"enrollId": "username",
"enrollSecret": "password"
}
]
Before that we have to make sure we have the Node.js version 8 installed in the local system and docker installed in the local system
Since I am working on a Windows system, I am going to show the commands for windows powershell to run the Hyperledger on the local system
BAAS\HLComposer> $P=$PWD.Path.Replace("\", "/")
BAAS\HLComposer> docker run --rm -ti -v $P/composer:/home/composer/.composer -e PORT=8080 -p 8080:8080 registry.cn-hangzhou.aliyun composer:0.20.0
Once you see the output of above command as below
info: [Hyperledger-Composer] :PlaygroundAPI :createServer() Playground API started on port 8080
You can access the network on localhost:8080 to make the tweaks before deploying. Once you access you will see the popup which confirms that the everything is setup successfully
Rather than creating an entire blockchain network from scratch, we will start with a fund-clearing-network and understand every component clearly. We can click on the "Deploy a new business network" and fill the form.
There are two important fields to fill here
Business network name : fund-clearing-network
Name for admin card : DemoUser@fund-clearing-network
Model network starter template : fund-clearing-network
Once you do that you will be able to see this network card right in the dashboard
You can see the chain code downloaded into the local system, now we connect to that specific business network and understand and make some changes to the component
First, we need to understand the composer terminology
We also have the queries to run SQL like statements written to find the data. The bundling format of all the files (models, scripts, permissions, queries) will be in a .bna format(Business Network Archive)
The below diagram shows how all the components are bundled.
.cto file is written in Hyperledger Composer modelling language, it is object-oriented modelling language that is used to create the model for the Blockchain network in Hyperledger
/**
* Fund clearing business network definition.
*/
namespace org.clearing
participant BankingParticipant identified by bankingId {
o String bankingId
o String bankingName
o Currency workingCurrency default = 'USD'
o Long fundBalance default = 5000000
}
We have the namespace - > org.clearing, we have Banking participant with attributes banking id, banking name working currency defaulted to USD, fundBalance.
asset TransferRequest identified by requestId {
o String requestId
o Transfer details
o TransferRequestState fromBankState default = 'PRE_PROCESS_COMPLETE'
o TransferRequestState toBankState
o TransferRequestState state
--> BankingParticipant fromBank
--> BankingParticipant toBank
}
This is the TransferRequest asset have a unique requestId, details, fromBankState defaulted to "PRE_PROCESS_COMPLETE", toBankState, and the state which is related from fromBank, toBank.
asset BatchTransferRequest identified by batchId {
o String batchId
o Settlement settlement
o BatchState state
--> BankingParticipant[] parties
--> TransferRequest[] transferRequests
}
This is the batchTransferRequest asset have a unique batchId, settlement, state which consists of all the parties from which all the transactions are initiated from and the array of transfer requests
transaction SubmitTransferRequest {
o String transferId
o String toBank
o TransferRequestState state
o Transfer details
}
This is a transaction "SubmitTransferRequest" with transferId, toBank, state, details.
transaction CreateBatch {
o String batchId
o UsdExchangeRate[] usdRates
}
transaction MarkPreProcessComplete {
o String batchId
}
transaction CompleteSettlement {
o String batchId
o UsdExchangeRate[] usdRates
}
transaction MarkPostProcessComplete {
o String batchId
}
As per previous transaction these are self-explanatory
event BatchCreatedEvent {
o String batchId
}
This is the BatchCreatedEvent which gives the batchId whenever there is a event created
concept Settlement {
o Double amount
o Currency currency
--> BankingParticipant creditorBank
--> BankingParticipant debtorBank
}
concept UsdExchangeRate {
o String to
o Double rate
}
concept Transfer {
o Currency currency
o Double amount
o Integer fromAccount
o Integer toAccount
}
The concepts are abstract classes they are not assets, participants or transactions. They are typically contained by an asset, participant or transaction. There are settlement, UsdExhangeRate, Transfer.
enum Currency {
o EURO
o STERLING
o USD
o YEN
o CHF
o CAD
}
enum TransferRequestState {
o PENDING
o PROCESSING
o PRE_PROCESS_COMPLETE
o COMPLETE
o ERROR
}
enum BatchState {
o PENDING_PRE_PROCESS
o READY_TO_SETTLE
o PENDING_POST_PROCESS
o COMPLETE
}
Enumerated types are used to specify a type that may have 1 or N possible values. We have Currency, TransferRequestState, BatchState.
We also have queries.qry file which consists of all the queries which are going to be part of the network, below is one of the queries. It is quite similar to the SQL statements
* Queries for Fund Clearing
*/
query TransferRequestsByBankInState {
description: "Select all TransferRequests for a participating bank in a given state"
statement:
SELECT org.clearing.TransferRequest
WHERE (fromBank == _$bank)
}
This consists of all the rules of for the network we are about to create, one such rule is displayed below.
/**
* Fund clearing access control list.
*/
rule ParticipantsRestrictionOnTransferRequest {
description: "Only allow participants involved with an Asset to interact with it"
participant(p): "org.clearing.BankingParticipant"
operation: ALL
resource(r): "org.clearing.TransferRequest"
condition: (partyWithinTransferRequest(r, p))
action: ALLOW
}
All the details are pretty much self-explanatory, next we will see permissionHelper.js
The current code we discussed has nothing but two methods,
partyWithinTransferRequest
partyWithinBatchTransferRequest
I should say this file is the heart of the application, this puts back all the different pieces together so that if works perfectly, let's see a function and understand how this exactly created. Below is the function,
/**
* Determine the net transfer between a banking pair, accounting for exchange rates
* @param {TransferRequest[]} transferRequests array of TransferRequest objects
* @param {participantId} participantId string participant identity
* @param {rates[]} rates array of UsdExchangeRate objects
* @return {Double} net amount in USD
*/
function netTransfers(transferRequests, participantId, rates) {
let amount = 0;
// Determine amount in USD
for (let request of transferRequests) {
if (request.toBank.getIdentifier() === participantId) {
if (request.details.currency === 'USD') {
amount += request.details.amount;
} else {
let filteredRate = rates.filter((rate) => { return rate.to === request.details.currency; });
amount += request.details.amount / filteredRate[0].rate;
}
} else {
if (request.details.currency === 'USD') {
amount -= request.details.amount;
} else {
let filteredRate = rates.filter((rate) => { return rate.to === request.details.currency; });
amount -= request.details.amount / filteredRate[0].rate;
}
}
}
return amount;
}
This function main idea is to make the transfer between the bankers based on the exchange rates.
Now we have the entire chaincode, we need to go to the Alibaba Cloud console, naviagate to Alibaba Cloud console >> products >> Blockchain as a service >> Click organization >> chaincode
We need to upload our chaincode, once done you can see that uploaded chaincode state is uninstalled(it means that it is uninstalled form the blockchain network) as below.
Then once you click on install, the next is that you need to "instantiate", as below
But while instantiating you might find it is asking for the "Endorsement Policy", It is either-or or all of them.
If you think that the chainchode should be approved once atleast one participant is approved, You need to enter this this format.
OR('demoorganizatioMSP.peer')
If you want every one of them to do be endorse it before it Is accepted, you can do like below
AND(demoorganizationMZP.peer)
For now I only have one peer, in typical scenarios they will be separated by commas. Once it is done the next step is to synchronize like below
Once synchronized our network is ready to use you can hit the URL and get the output as below.
You can also make the test transaction right from the Hyperledger composer and check all the transactions and their statuses like below
In the below picture I am submitting a request to add a new backing participant on to the network.
You can see the entire registry of all the transactions which are mode from a specific resource and also the whole network as a whole.
The following is the screen which consists of all the transactions
If you were able to perform all the actions successfully, you should have successfully built a blockchain network. Congratulations!
In this three-part tutorial series, we have seen how easy it is to build a blockchain network using Alibaba Cloud Blockchain as a Service and the Hyperledger composer. We also discussed the key components of blockchain and the Hyperledger composer.
Alibaba Cloud BaaS - Part II: Blockchain as a Service with Hyperledger Composer
2,599 posts | 764 followers
FollowAlibaba Cloud Indonesia - October 25, 2022
Alibaba Clouder - November 6, 2019
- March 8, 2018
Alipay Technology - August 21, 2019
Alibaba Clouder - October 15, 2020
Alibaba Clouder - August 18, 2020
2,599 posts | 764 followers
FollowBaaS provides an enterprise-level platform service based on leading blockchain technologies, which helps you build a trusted cloud infrastructure.
Learn MoreAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreMore Posts by Alibaba Clouder