×
Community Blog Alibaba Cloud BaaS - Part III: Developing and Deploying a Chain Code

Alibaba Cloud BaaS - Part III: Developing and Deploying a Chain Code

In this article series, we will discuss the concept of blockchain as well as introduce the Alibaba Cloud Blockchain as a Service (BaaS) offering.

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.

1

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

2

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.

3

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

4

Once you do that you will be able to see this network card right in the dashboard

5

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

  • Participant: A person who can perform an action or own something, for example in context of the current fund network it is nothing but a Banker who submits the transfer request onto the system.
  • Asset: Anything which holds some value, in our case it is the request asking for a transfer for a single transaction or multiple because it holds the entire information of the transfer
  • Event: When any changes happen to the asset, we can let others know that the change is happen, it is similar to sending a notification once the transfer is successful
  • Transaction: When there is some trade which is happening on the system, that include the creation of assets also
  • CTO: The extension that is used as an extension for participants, assets, transactions and events.
  • Transaction Functions: some code that consists of a logic for a transaction.
  • Namespace: They are basically a way to organize onto groups, to keep things clean in case when the codebase grows.

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.

6

.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.

queries.qry

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)
}

Permissions.acl:

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

    • this checks whether the participant is within the transfer request
  • partyWithinBatchTransferRequest

    • this checks whether the participant is part of the batchTransferRequest or not

clearing.js

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.

7

Then once you click on install, the next is that you need to "instantiate", as below

8

But while instantiating you might find it is asking for the "Endorsement Policy", It is either-or or all of them.

9

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

10

Once synchronized our network is ready to use you can hit the URL and get the output as below.

11

12

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.

13

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.

14

The following is the screen which consists of all the transactions

15

Conclusion

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.

0 0 0
Share on

Alibaba Clouder

2,114 posts | 490 followers

You may also like

Comments