Tutorial: UB Smart Contracts and Hybrid Consensus

UnitedBitcoin
5 min readJul 18, 2018

--

Smart contracts and hybrid consensus are two exciting features that we have launched on the UB test net. Before launching it on the main net, we are conducting tests with the development team and we invite you to test it for yourselves.

In this tutorial, we will break down the process of deploying a smart contract on the UBTC test net.

UB smart contracts supports popular programming languages C#, Java, LUA, and Kotlin; unlike other smart contract features on the market, you will not need to learn a new programming language to use UB smart contracts. We will use C# for the purposes of these example instructions.

Before getting started, please ensure you install all required software. We will use Visual Studio 2017, Python 3.5.2 and Ubuntu 16.04 LTS (you must use Ubuntu to connect to UBTC test net).

Step 1

Go to UBTC Github page:

https://github.com/UnitedBitcoin/UnitedBitcoin/wiki/UnitedBitcoin-Smart-Contract

Step 3

Unzip the gsharpc file. Click on the DemoContract1 folder, open the DemoContract1.csproj

Step 4

Jump into the visual studio interface.

On the solution Explorer, select DemoContract1.cs. This is the demo contract. You create you own contract by using this format. More information is available in UBTC Github.

Step 5

Choose Build , Build Solution. And then, check the output dialog box.

Step 6

Just like that, the solution is built. The warning message is normal.

Step 7

Copy all files from C:\gsharpc-master\gsharpc\bin\Debug to C:\gsharpc-master\gsharpc.

Step 8

Copy all files from C:\gsharpc-master\DemoContract1\bin\Debug to C:\gsharpc-master\gsharpc.

Step 9

Double check that all files are available like this screenshot.

Step 10

Compile the contract file into UVM binary format, then convert to *.gpc format.

Open the command prompt then run these commands:-

cd desktopcd gsharpc-mastercd gsharpcgsharpc —gpc C:\yourcomputername\desktop\gsharpc-master\gsharpc\DemoContract1.dll

Step 12

The contract file is generated.

Step 13

Install packages for deploying smart contract on the public testnet.

For this step, we need Linux. While the final node will run on both Linux and Windows, the test node currently only runs on Linux. This tutorial assumes you use Ubuntu.

On your Ubuntu machine, open a Terminal. Run these commands:-

pip3 install requestswget http://p728vdxow.bkt.gdipper.com/ubc-cli-test-v13

( this package will be updated from time to time, please check the download link if you get an error message )

wget http://p728vdxow.bkt.gdipper.com/ubcd-test-v13

( this package will be updated from time to time, please check the download link if you get an error message )

git clone https://github.com/UnitedBitcoin/ub_contract_test_tools.gitmkdir ./testdata

Step 14

Hybrid Consensus. POS or POW ?

After you have done all above steps. You can choose either one of the consensus methods : Proof of Work or Proof of stake. However, there have some points and you should know:-

1) Your wallet need at least 500 coins to connect POS algorithm

2) Once your wallet contain 500 coins, staking will be started after 4800 blocks

3) POS is base on chance to generate blocks, if you have more coins then you will have more chance

You learnt it, great! Then we can run this command to join the public test net:-

POW method

./ubcd-test -datadir=./testdata -rpcuser=user -rpcpassword=password -rpcport=60000 -rpcallowip=127.0.0.1/32 -port=18333 -testnet -txindex -server -contractprint

POS method

./ubcd-test -datadir=./testdata3 -rpcuser=user -rpcpassword=password -rpcport=60000 -rpcallowip=127.0.0.1/32 -port=18333 -testnet -txindex -server -contractprint -rpcserialversion -prematurewitness -staking

Step 15

Connect our wallet to the nodes and check the current block height.

Run these command then you will connect to 3 different nodes:-

./ubc-cli-test -rpcuser=user -rpcpassword=password -rpcport=60000 addnode 13.251.64.171:18333 add

./ubc-cli-test -rpcuser=user -rpcpassword=password -rpcport=60000 addnode 13.209.29.62:18333 add

./ubc-cli-test -rpcuser=user -rpcpassword=password -rpcport=60000 addnode 18.182.0.202:18333 add

wait for few minutes then run the command:-

./ubc-cli-test -rpcuser=user -rpcpassword=password -rpcport=60000 getblockcount

If you can see the existing block height, that means you join the public testnet. You can check the existing block height and request some test coins at http://13.251.64.171:3000/

Step 16

How to generate blocks by POW or POS?

Once the testing procedure has completed, you can use the same way to generate coin on main test. Firstly, you can run this command to check the existing difficulty:-

./ubc-cli-test -rpcuser=user -rpcpassword=password -rpcport=60000 getdifficulty

Then, create two new accounts to your wallet by this command:-

./ubc-cli-test -rpcuser=user -rpcpassword=password -rpcport=60000 getnewaddress “”

./ubc-cli-test -rpcuser=user -rpcpassword=password -rpcport=60000 getnewaddress “a”

Two new bitcoin addresses will be generated. Please write it down or you can check all created addresses by this command:-

./ubc-cli-test -rpcuser=user -rpcpassword=password -rpcport=60000 listunspent

And then, you can generate blocks by this command:-

If you connect to POW test net

./ubc-cli-test -rpcuser=user -rpcpassword=password -rpcport=60000 generatetoaddress 200 YOURADDRESS 1000000 You can change hashing range.

And then, you can check your balance by this command if you get block reward:-

./ubc-cli-test -rpcuser=user -rpcpassword=password -rpcport=60000 getwalletinfo

If you connect to POS test net

Blocks will be generated automatically by chance when you meet the requirements

Step 17

How to upload contract file and deploy it?

Do you remember we downloaded a file called “ub_contract_test_tools”? Run the command:-

cd ub_contract_test_tools

There have some sample contract files and you can try it by yourself. However, we will test that .gpc file only.

Run the command nano ubtc_tests.py

Then, press ctrl — x , select save and then press enter

Run the command python3 –m ubtc_random_tests

Then, you will see the terminal print a message “ Lucky draw result will post at block height 15000”.

We hope you enjoy this tutorial. Thank you for reading!

--

--