MultiChain Web Demo

Follow the steps listed down in the Github repository for the same.

Github Repo: https://github.com/MultiChain/multichain-web-demo

Setting up in Local:

Starting the web server

$ cd ~/public_html
$ php -S localhost:8000

The terminal will show:

PHP 7.2.7-0ubuntu0.18.04.2 Development Server started at Thu Aug 2 17:46:31 2018
Listening on http://localhost:8000
Document root is /home/ajinkyawavare/Documents/BlockChain/MultiChain/multichain-web-demo
Press Ctrl-C to quit.
[Thu Aug 2 17:46:46 2018] 127.0.0.1:45556 [200]: /?chain=default
[Thu Aug 2 17:46:47 2018] 127.0.0.1:45574 [200]: /bootstrap.min.css
[Thu Aug 2 17:46:47 2018] 127.0.0.1:45576 [200]: /styles.css
[Thu Aug 2 17:48:43 2018] 127.0.0.1:45588 [200]: /?chain=default&page=permissions
[Thu Aug 2 17:48:43 2018] 127.0.0.1:45602 [200]: /bootstrap.min.css
[Thu Aug 2 17:48:43 2018] 127.0.0.1:45604 [200]: /styles.css

Accessing Explorer from localhost:

Accessing Explorer installed on the remote server:
Make sure that you update the network and security settings in the aws.
Also take a note of the change in the host accessible in the chain1.conf file here.
You need to install php and php curl for the web page to get served.
Also nginx or apache htt-server needs to be installed in your machine.

MultiChain Explorer

Follow the steps listed down in the Github repository for the same.

Setting up in Local:

Accessing Explorer from localhost


Accessing Explorer installed on the remote server:
Make sure that you update the network and security settings in the aws.
Also take a note of the change in the host accessible in the chain1.conf file here.

 

Github Repo: https://github.com/MultiChain/multichain-explorer

 

MultiChain Mining

In MultiChain, block creators must prove their identity by signing blocks

Block signatures

Block signatures are embedded inside the OP_RETURN metadata of the coinbase transaction, using a regular bitcoin-style signature format. The payload signed by the block creator is the full block header, with the following modifications:

  • The merkle_root is recalculated based on the txid of the coinbase transaction with the OP_RETURN removed. This prevents a dependency loop from block signature → coinbase txid → merkle root → block header → block signature.
  • The nonce field is set to 0. This avoids having to recalculate the signature for every attempt at finding a block hash to match the target difficulty. However it does lead to some malleability in that a user without mine permissions could generate a block with the same content as a valid block, but with a different nonce. Seeing as the nonce serves no purpose other than to randomize the block’s content for its hash, this should not be a source of concern.

In the case of a permissioned MultiChain blockchain, consensus is based on block signatures and a customizable round-robin consensus scheme, rather than proof-of-work as in bitcoin.

On the first server, run:

grant 15ZLxwAQU4XFrLVs2hwQz1NXW9DmudRMcyx2ZV mine

(Even though the permission is called mine note that there is no real “mining” taking place, in the sense of proof-of-work.) On the second server, check that two permitted block validators are listed:

listpermissions mine

Run this on both servers to maximize the degree of validator randomness:

setruntimeparam miningturnover 1

Now wait for a couple of minutes, so that a few blocks are added. (This assumes you left the block time on the default of 15 seconds.) On either server, check the creators of the last few blocks:

listblocks -10

The address of the validator of each block is in the miner field of each element of the response.

Snippet:
RR Mining

Node-1 Terminal | Node-2 Terminal

 

Reference: https://www.multichain.com/developers/mining-block-signatures/
https://www.multichain.com/getting-started/

MultiChain Streams

MultiChain streams enable a blockchain to be used as a general purpose append-only database, with the blockchain providing timestamping, notarization and immutability. A MultiChain blockchain can contain any number of streams, where the data published in every stream is stored by every node. If a node chooses to subscribe to a stream, it will index that stream’s contents to enable efficient retrieval in various ways.

Each stream is an ordered list of items, in which each item has the following characteristics:

  • One or more publishers who have digitally signed that item.
  • A key between 0 and 256 bytes in length.
  • Some data, which can reach many megabytes in size.
  • Information about the item’s transaction and block, including its txid, blockhash, blocktime, and so on.

Like native assets, MultiChain streams can be referred to in any of three ways:

  • An optional stream name, chosen at the time of stream creation. If used, the name must be unique on a blockchain, between both assets and streams. Stream names are stored as UTF-8 encoded strings up to 32 bytes in size and are case insensitive.
  • A createtxid, containing the txid of the transaction in which the stream was created.
  • A streamref which encodes the block number and byte offset of the stream creation transaction, along with the first two bytes of its txid.

If root-stream-name in the blockchain parameters is a non-empty string, it defines a stream which is created with the blockchain and can be written to immediately. The root stream’s createtxid is the txid of the coinbase of the genesis block, and its streamref is 0-0-0.

Create a stream, which can be used for general data storage and retrieval. On the first server:

create stream stream1 false

The false means the stream can only be written to by those with explicit permissions. Let’s see its permissions:

listpermissions stream1.*

So for now, only the first server has the ability to write to the stream, as well as administrate it. Let’s publish something to it, with key key1:

publish stream1 key1 73747265616d2064617461

The txid of the stream item is returned. Now let’s see that the stream is visible on another node. On the second server:

liststreams

(The root stream was in the blockchain by default.) Now we want the second server to subscribe to the stream, then view its contents:

subscribe stream1
liststreamitems stream1

Now we want the second server to be allowed to publish to the stream. On the first server:

grant 15ZLxwAQU4XFrLVs2hwQz1NXW9DmudRMcyx2ZV send
grant 15ZLxwAQU4XFrLVs2hwQz1NXW9DmudRMcyx2ZV stream1.write

Note that the address needs both general send permissions for the blockchain, as well as permission to write to this specific stream. Now let’s publish a couple of items on the second server:

publish stream1 key1 736f6d65206f746865722064617461
publish stream1 key2 53747265616d732052756c6521

Now let’s query the stream’s contents in many different ways. Back on the first server:

subscribe stream1
liststreamitems stream1 (should show 3 items)
liststreamkeys stream1 (2 keys)
liststreamkeyitems stream1 key1 (2 items with this key)
liststreampublishers stream1 (2 publishers)
liststreampublisheritems stream1 15ZLxwAQU4XFrLVs2hwQz1NXW9DmudRMcyx2ZV

Snippets:

Streams -1 (node 1 and node 2)

Streams – 2

Reference: https://www.multichain.com/developers/data-streams/
https://www.multichain.com/getting-started/

 

MultiChain Native Assets

MultiChain allows the creation & tracking of assets at the network level.

A use of  Blockchain to have defined limited native assets & let peers or nodes transact them among each other in an atomic or non-atomic manner. Anything can be defined as an asset.

Using native assets:

MultiChain allows the creation and tracking of assets at the network level

Assets in MultiChain

MultiChain supports assets natively at the blockchain level. The identifiers and quantities of assets are encoded within each transaction output, alongside the quantity of the blockchain’s native currency (which may be zero if it is not being used). Every MultiChain node tracks and verifies the quantity of assets in transactions, just as it is does with the native currency. Specifically, it checks that the total quantities of all assets in a transaction’s outputs are exactly matched by the total in its inputs. MultiChain allows each transaction output can contain any number of different assets.

In MultiChain, assets can be referred to in any of three ways:

  • An optional asset name, chosen at the time of issuance. If used, the name must be unique on a blockchain, between both assets and streams. Asset names are stored as UTF-8 encoded strings up to 32 bytes in size and are case insensitive.
  • An issuetxid, containing the txid of the transaction in which the asset was issued.
  • An assetref which encodes the block number and byte offset of the issuance transaction, along with the first two bytes of its txid.Common Commands:
    
    listpermissions issue (Check whether you have the permission to issue or not)
    issue 16bNJqp4d8JX3uL7UYr5hACWLnK2LU4f2Ep9FD aj007 100000 0.000001 (Address which has the permission to issue. Here aj007 is the asset issued. This has precison till 6 as specified in the last param)
    listassets (list the assets on the chain)
    gettotalbalances (get the total balance on the server)
    send 15ZLxwAQU4XFrLVs2hwQz1NXW9DmudRMcyx2ZV aj007 5000 (send the assets to the given address on node - 2)
    listwallettransactions (get the total transactions which have occured in the given wallet)
    
    sendwithdata 15ZLxwAQU4XFrLVs2hwQz1NXW9DmudRMcyx2ZV '{"aj007":500}' data (Sending metadata in transaction)


    Reference: https://www.multichain.com/developers/native-assets/

MultiChain

Installing MultiChain on Linux

su (enter root password)
cd /tmp
wget https://www.multichain.com/download/multichain-1.0.5.tar.gz
tar -xvzf multichain-1.0.5.tar.gz
cd multichain-1.0.5
mv multichaind multichain-cli multichain-util /usr/local/bin (to make easily accessible on the command line) exit (to return to your regular user)

We need two servers with multichain installed on them.
Try Amazon EC2.
After installing multichain and initializing the blockchain, make sure you modify the Network and security settings port numbers to allow the two nodes to communicate with each other.

1. Creating a blockchain

First we will create a new blockchain named chain1. On the first server, run this command:

multichain-util create chain1

View the blockchain’s default settings:

cat ~/.multichain/chain1/params.dat

Initialize the blockchain, including creating the genesis block:

multichaind chain1 -daemon

2. Connecting to a blockchain

Now we’ll connect to this blockchain from elsewhere. On the second server, run the following:

multichaind multichaind chain1@172.31.29.15:6461

You should be told that the blockchain was successfully initialized, but you do not have permission to connect. You should also be shown a message containing an address in this node’s wallet.

Back on the first server, add connection permissions for this address:

multichain-cli chain1 grant 15ZLxwAQU4XFrLVs2hwQz1NXW9DmudRMcyx2ZV connect,send,receive

Now try reconnecting again from the second server:

multichaind chain1 -daemon

3. Some commands in interactive mode

Before we proceed, let’s enter interactive mode so we can issue commands without typing multichain-cli chain1 every time. On both servers:

multichain-cli chain1

If you are using Windows, interactive mode is not yet available, so all commands in this guide should be preceded by multichain-cli chain1. You will also need to open another DOS command line in the directory where you installed the MultiChain executables.

Now that the blockchain is working on two nodes, you can run the commands in this section on either or both. To get general information:

getinfo

See a list of all available commands:

help

Show all permissions currently assigned:

listpermissions

Create a new address in the wallet:

getnewaddress

List all addresses in the wallet:

getaddresses

Get the parameters of this blockchain (based on params.dat file):

getblockchainparams

For each node, get a list of connected peers:

getpeerinfo

4. Streams

Now let’s create a stream, which can be used for general data storage and retrieval. On the first server:

create stream stream1 false

The false means the stream can only be written to by those with explicit permissions. Let’s see its permissions:

listpermissions stream1.*

So for now, only the first server has the ability to write to the stream, as well as administrate it. Let’s publish something to it, with key key1:

publish stream1 key1 73747265616d20646174358

The txid of the stream item is returned. Now let’s see that the stream is visible on another node. On the second server:

liststreams

(The root stream was in the blockchain by default.) Now we want the second server to subscribe to the stream, then view its contents:

subscribe stream1
liststreamitems stream1

Now we want the second server to be allowed to publish to the stream. On the first server:

grant 15ZLxwAQU4XFrLVs2hwQz1NXW9DmudRMcyx2ZV send
grant 15ZLxwAQU4XFrLVs2hwQz1NXW9DmudRMcyx2ZV stream1.write

Note that the address needs both general send permissions for the blockchain, as well as permission to write to this specific stream. Now let’s publish a couple of items on the second server:

publish stream1 key1 736f6d65206f746865722064617461
publish stream1 key2 53747265616d732052756c6521

Now let’s query the stream’s contents in many different ways. Back on the first server:

subscribe stream1
liststreamitems stream1 (should show 3 items)
liststreamkeys stream1 (2 keys)
liststreamkeyitems stream1 key1 (2 items with this key)
liststreampublishers stream1 (2 publishers)
liststreampublisheritems stream1 1... (2 items by this publisher)

Reference: https://www.multichain.com/getting-started/