Running Smart Contracts in ARK.io via Hyperledger Fabric

in #hyperledger6 years ago (edited)

ark-plus-hyper.png

The approaching ARK.io V2 main net release is bringing in some of the most anticipated features including modular core architecture based on plugins. I am especially excited about plugins because they make my job easier :)

Here at Ark Labs, we've been actively participating in the development of decentralized solutions enriching Ark ecosystem. Ark's "Point. Click. Blockchain." concept enables us launching custom Ark-based blockchains oriented at consumer needs. And, Ark V2 plugins will make it easier than ever to customize Ark blockchain by embedding any kind of business logic as a plugin.

In this post I'll show you how to enhance Ark blockchain with sample (auto, life, home...) insurance smart contracts.

But how to run Smart Contracts in Ark if it does not have a VM ?

But why would you want to burden Ark's main net with running each and every smart contract? Recall how Crypto Kitties brought Ethereum down to its knees. Instead, we'll follow a different strategy and run our smart contracts in a sidechain based on Hyperledger Fabric.

The solution

Let's look at what we are going to build:

Ark - Hyperledger-Page-1.png

First, we'll write V2 plugin which will be listening to Ark devnet transactions. Our plugin will have a TX Listener which will be filtering out specific transactions and then using Hyperledger Client to call correspondent Smart Contracts deployed on Hyperledger Fabric. The plugin will be deployed within a standard Ark V2 Relay connected to V2 devnet.

Then, we'll launch our own Hyperledger Fabric blockchain and deploy our sample insurance smart contracts.

Finally, we'll show how to use Ark SmartBridge technology to trigger smart contracts on Hyperledger blockchain. Our examples will demonstrate two ways to construct a transaction containing encoded SmartBridge data: 1) manually, via ARK Desktop Client 2) programmatically through ARK JavaScript SDK

Hyperledger Plugin

We'll clone the sample plugin from https://github.com/Ark-Labs/ark-hyperledger-plugin. But before, let's examine it.

The plugin repo has several common boilerplate files. But there are only 3 relevant files which have been written for our example:

  • Plugin Registration
  • TX Listener
  • Hyperledger Client

Plugin Registration

Here we register our plugin by simply instantiating the TX Listener.

lib/index.js
Screen Shot 2018-10-21 at 2.45.15 PM.png

TX Listener

Transaction Listener is responsible for intercepting all transactions via listening to "transaction.applied" event. If vendor field matches out transaction pattern (starts with "sm:"), the listener delegates transaction execution to Hyperledger Client.

lib/listener.js
Screen Shot 2018-10-21 at 2.45.42 PM.png

Hyperledger Client

Hyperledger Client parses the transaction and executes corresponding Hyperledger Smart Contract method via HTTP POST request.

NOTE: you have to change IP address to match your Hyperledger instance.

lib/hyperledger-client.js
Screen Shot 2018-10-21 at 2.46.52 PM.png

Hyperledger Plugin Installation

Prerequisites

Install Ark V2 Relay by following this post: How to install Ark 2.0 blockchain node on a cloud server

Install Hyperledger Composer and deploy sample smart contracts as described in: Launch your own blockchain with Hyperledger Composer

Make sure Ark V2 Relay, Hyperledger Composer, and Hyperledger REST Server are up and running before you proceed further.

Step-by-step Instructions to Install Ark Hyperledger Plugin

Login into your Ark V2 node:

    ssh [email protected]
    su - ark
    cd ~/ark-core/packages 


Clone the plugin:

git clone https://github.com/vaustymenko/ark-hyperledger-plugin.git


Install:

cd ark-hyperledger-plugin/
yarn install


Register:

vi ~/.ark/config/plugins.js


Add

,
 '@vadymus/ark-hyperledger-plugin': {  
} 


So the end of the file looks like:
...
Screen Shot 2018-10-21 at 3.25.46 PM.png

Restart the relay and start watching logs

pm2 restart ark-core-relay && pm2 logs ark-core-relay


At this point we have completed installation of ark-hyperledger-plugin

Demo Time!

Trigger Smart Contract via Ark V2 Desktop Wallet

Log into your Hyperledger Composer, switch to "Test" mode, select "Subscriber" on the left, and click "Create a New Participant":

Screen Shot 2018-10-21 at 4.11.28 PM.png

Enter details and click "Create New":

Screen Shot 2018-10-21 at 4.12.04 PM.png

You should see the newly created subscriber:

Screen Shot 2018-10-21 at 4.12.19 PM.png

Then, in the same way add an InsurancePolicy:

Screen Shot 2018-10-21 at 4.13.00 PM.png

Notice, that "amountPaid" and "arkTransaction" fields are empty for the new policy. Now, we'll broadcast Ark transaction, which will be intercepted by our plugin relay, which in turn will call "PolicyPaymentTransaction" to update the payment field.

Open your Ark Desktop client and submit a new transaction:

Screen Shot 2018-10-22 at 6.37.38 PM.png

Screen Shot 2018-10-22 at 6.37.50 PM.png

In a few second, you'll notice your transaction details captured by the ark-hyperledger plugin in the relay logs:

Screen Shot 2018-10-22 at 6.38.19 PM.png

Then, refresh "All Transactions" section in Composer UI:

Screen Shot 2018-10-22 at 6.38.46 PM.png

, and notice your transaction:

Screen Shot 2018-10-22 at 6.38.58 PM.png

You can look at transaction details:

Screen Shot 2018-10-22 at 6.39.10 PM.png

As a last step, switch to "InsurancePolicy" section on the left, and notice how policy with id "0123" now has it's "paidAmount" and "arkTransaction" fields populated by the last transaction details:

Screen Shot 2018-10-22 at 6.39.34 PM.png

Trigger Smart Contract via Ark V2 SDK Client

The second way to trigger a smart contract is programmatically, through Ark V2 SDK. Sample client is located under https://github.com/Ark-Labs/ark-hyperledger-plugin/tree/master/client:

Screen Shot 2018-10-22 at 7.15.28 PM.png

Pull the code, open client/app.js and update variables:

Screen Shot 2018-10-22 at 7.19.01 PM.png

client/ark-client.js does all the work:

Screen Shot 2018-10-22 at 7.20.31 PM.png

Run the test client via:

node app.js


Observer your transaction, first, in the relay logs, and then in the Hyperledger Composer UI.

Summary

In this post, we have implemented Ark V2 plugin which acts as a connector to effectively turn Hyperledger Fabric into a sidechain driven by Ark blockchain transactions. This solution enables anybody to run smart contracts triggered by Ark transactions without overloading Ark's main blockchain.

On a large scale, we have connected public/decentralized (Ark) and private/centralized (Hyperledger) blockchains. While I'm personally all for decentralization of governance, private blockchains do have their own use cases: supply chains, medical applications with sensitive patient data (Hyperledger can open "private" channels), and any application with limited number of participants which do not trust each other.

Where to go from here...

The trivial example shared in this post is merely a starting point and can be immensely improved. Here are some ideas for the reader:

  • map Vendor Field "sm:ins:" to various installations of Hyperledger / other sidechains
  • implement plugin in a way where it can be safely run on multiple relays for redundancy
  • add authentication / authorization, which were skipped for the sake of simplicity
  • make the code production quality so that it could be submitted as AIP proposal to be included as Ark default plugin

In conclusion, this does not have to be a centralized / private sidechain only. If you can introduce reasonable transaction fees, then people will have incentives to run these relays and Hyperledger Peers, thus converting the entire solution into a decentralized sidechain!

References

Setting Up New Plugins in ARK Core v2: https://blog.ark.io/setting-up-new-plugins-in-ark-core-v2-example-7fac69993a73)
Hyperledger Fabric: https://www.hyperledger.org/projects/fabric
Hyperledger Composer: https://hyperledger.github.io/composer/latest/
Swagger: https://swagger.io/

Sort:  

Congratulations @vadymus! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You got your First payout

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

SteemitBoard Ranking update - Resteem and Resteemed added

Support SteemitBoard's project! Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.19
TRX 0.12
JST 0.028
BTC 64833.23
ETH 3558.77
USDT 1.00
SBD 2.35