First Attempt on BSN: Deploy a Sample Fabric Chaincode in BSN Testnet
BSN (Blockchain-based Service Network) was first announced last year. Since then, they keep deploying nodes in China and across the world, and launched international portal for services on a variety of blockchain frameworks. Here is my first experience on their permissioned services on Hyperledger Fabric through their Testnet. In this article I first provided a very brief introduction about BSN, and made a quick demonstration on how to deploy a standard smart contract package (chaincode) on the Hyperledger Fabric framework integrated into the BSN.
Quick Overview of BSN
To understand more about BSN, the best source of information is from BSN. Here you can find the official information. Here is a quick overview on it.
BSN is a global infrastructure for blockchain application deployment. BSN supports a large amount of blockchain frameworks, including both permissionless and permissioned blockchains. BSN currently runs as two networks: BSN China and BSN International. In this article the demonstration is done on BSN International.
Several types of blockchain service are currently offered in BSN International: they are permissionless services, permissioned services and interchain services. For public permissionless services BSN is serving as gateways of most well-known blockchain networks, such as Ethereum, EOSIO, Algorand, etc, both mainnet and some testnets. For permissioned blockchain frameworks BSN currently supports Hyperledger Fabric and FISCO BCOS. BSN also recently launched interchain services for testing. In this article we focus on the permissioned blockchain services.
Unlike the way we are used to working with permissioned blockchain frameworks, BSN takes a different approach.
First, the blockchain frameworks are adapted and integrated into a BSN delivery platform. Take Hyperledger Fabric as an example. The infrastructure part (fabric network) is predesigned and prebuilt by BSN. Application owners focus on the development of smart contracts (i.e. chaincode). BSN will handle the deployment of chaincode (i.e. installing chaincode to the peers and instantiating it in the channel). On one side application owners skip the tedious process of bringing up the network, but on the other side they are unable to alter the network setup at all.
Secondly, accessing the application deployed in BSN is now through BSN PCN (public city node) gateway through a new API set. This new API set is used for all permissioned blockchain frameworks, which means that we cannot access the application through framework-native API or SDK any more. As an example, for the fabric application deployed in BSN, one needs to use this BSN PCN API to invoke chaincode functions defined in our chaincode package. Note that this PCN API is used for all permissioned blockchain adapted into BSN. The framework-specific items are included in the body in the requests and responses.
Finally, BSN enforces access control on users accessing the applications deployed in BSN. Access control is done first at PCN Gateway level, and then at framework level. For fabric application, two sets of credentials are required when using the application: one set for PCN Gateway, and one set for Fabric MSP requirement.
This is the flow of using the application deployed in BSN. Off-BSN System is where outside BSN, and can be considered as the external world accessing the applications deployed in BSN.
Here we will demonstrate how to deploy a sample chaincode on BSN Testnet, and how to access (invoke) chaincode functions using PCN Gateway API/SDK.
We need a user account before using BSN. Simply register a new account in BSN Global site (https://www.bsnbase.io). With this account you can test several services free of charge, and here we are using Permissioned Service Testnet.
In real life, BSN permissioned services allow developer to select the framework (e.g. Hyperledger Fabric or FISCO BCOS), sites and the number of peers in each site. Application owners are then charged by the site locations, quantity of peer, storage and data volume, on a monthly basis.
In October BSN launched a Testnet, in which we can test our chaincode running in BSN free of charge. And in this demonstration we are using the Testnet.
We are using a sample chaincode for our demonstration. This chaincode is adapted from the Simple Asset Chaincode (SACC) provided in
fabric-samples/chaincode/. This chaincode comes with two functions,
- set(): store a key/value pair into ledger, when calling we need two arguments: key and value
- get(): retrieve the value from the ledger based on the given key
Note that in the original chaincode there are initial values (a key/value) required. For sake of simplicity we remove them.
Here is the chaincode we are going to deploy:
To meet the requirement of chaincode packaging, we also need to change the file name from
BSN PCN Gateway SDK
After the chaincode is deployed in BSN, credentials for the Testnet are downloaded, and we can build our application accessing this chaincode, that is, invoking chaincode functions. Currently PCN Gateway supports Python, Java, Go and CSharp. In this demonstration we are using Python SDK. Python 3 is needed for the demonstration, and the SDK setup will load other packages required.
Prepare Chaincode Package
Assuming you have
fabric-samples/ well downloaded in your computer and a proper Go environment is set.
cp -r sacc testBSNcd testBSN
testBSN/, remove the
sacc.go and save the chaincode above as
main.go (or you simply change name
main.go and remove the lines in Init.)
Then zip the directory content
zip -r testBSN.zip testBSN/
testBSN.zip is to be uploaded in the next step.
Upload and Deploy Chaincode Package
Login BSN Global (https://www.bsnbase.io). In the left list choose Permissioned Services and Testnet Services.
Select Create a Test Service and choose platform Fabric-1.4.3-secp256r1.
Upload Chaincode Package: choose GO and upload the file
testBSN.zip. Specify testBSN as the main path.
After that, select Start Deploying.
It takes some time. Upon completion, you can see the detail of the deployment.
We see the Status: Deployment successful. Besides, there are several pieces of information we need in order to access the application (i.e. use the chaincode). They are
- My Certificate: This file contains all the credential material needed to access the application through the BSN PCN Gateway. Simply download it, and we will refer to some files later.
- Access URL: This is the BSN PCN Gateway to access the Testnet.
- UserCode and APPCode: We specify both the user code and application code in our application.
- Chaincode deployment name: This refers to the deployment of our chaincode package.
Access Application / Invoking Chaincode Functions with Python SDK
First, take a look at the credential material downloaded and unzipped. We place it inside the
We use the PCN Gateway Python SDK. More detail can be found here: https://github.com/BSNDA/PCNGateway-PY-SDK.
We access python console and start working on the SDK.
from bsn_sdk_py.client.config import Config
from bsn_sdk_py.client.fabric_client import FabricClientnodeApi = "http://220.127.116.11:4004"
user_code = "USER0003202009052048235246180"
app_code = "app003eaab3197d3f342fdb217563493efd3c8"
chaincode = "cc_8b6c2a8007904790a020accf8bbb19f7"app_public_cert_path = "/home/ubuntu/testcert/gatewayCert/gateway_public_cert_secp256r1.pem"
user_private_cert_path = "/home/ubuntu/testcert/userAppCert/secp256r1/private_key.pem"
mspDir = "/home/ubuntu/testcert/fabricMsp"
httpcert = ""c = Config(user_code, app_code, nodeApi, mspDir, httpcert, app_public_cert_path, user_private_cert_path)client = FabricClient()
Now we first invoke the set function, with key/value as name/Peter.
result = client.req_chain_code(chaincode,'set','',['name','Peter'])
We see the invoking chaincode function is successful. Note the chaincode response (ccRes) inside the body is the returned result of the chaincode function set.
For information and comparison: this is how it looks like when we are NOT using BSN: using peer command natively onto Fabric Network, invoking the same function. We can see the result, identical to what we see through BSN.
We check the value of name by invoking get function.
result = client.req_chain_code(chaincode,'get','',['name'])
And the result is Peter, which was what we have set before.
Similarly, for information and comparison, here is how we use peer command natively on the fabric network. We get back the same result.
We can also inspect the ledger information between invoking chaincode functions:
We see the ledger grows by one block after a chaincode function is invoked. This transaction is included in a block, committed into the ledger.
This is just a demonstration on how the permissioned blockchain like Hyperledger Fabric is adapted into the BSN environment. As you can see I do not need to care the fabric network setup: all are predefined and done by BSN. I simply upload the chaincode and it can run in BSN. You also notice that BSN has adapted and integrated these permissioned blockchain frameworks into their environment, and provides a unified way when accessing the applications deployed in BSN. There are other interesting services in BSN and I will share with you once I have done some more tests.