[JAVA] I investigated the recently talked about distributed ledger Scalar DLT (smart contract execution)

Last time, I examined Scalar DLT, a distributed ledger software developed by Scalar, Inc..

Previous article: Recently talked about distributed ledger Scalar DLT (basic edition)

This time I will actually touch the smart contract.

How to get started with Scalar DLT

As I introduced last time, Scalar DLT has software called Scalar DL and Scalar DB. It is possible to use Scalar DB alone, but if you want, try Scalar DL, which can use smart contracts. Currently, Sandbox is provided, so we will use it.

Sandbox

Some people have already written about Sandbox, so I will refer to the following.

I tried the sandbox environment of the distributed ledger software Scalar DL

In addition, although there was the following description in the article, now I can download the necessary files as soon as I sign in with my GitHub account.

Request access to use the sandbox from the URL above. Shortly after the request, you will receive a registration completion email, and you will receive the key pair, certificate, configuration file, etc. required for use (I received it in tens of minutes).

Smart contract

Now let's run the sample contract. This time, we will work in the environment of macOS 10.14.4. Basically, you can do it by following the page above, but I will explain each command step by step.

Java installation

First of all, JAVA is required to run scalar l-client-sdk. So let's install JAVA.

Homebrew installation
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

homebrew-cask-Introduction of versions
$ brew tap homebrew/cask-versions

Java 8 installation
$ brew cask install adoptopenjdk8

Sandbox usage application

Next, apply for Sandbox usage from the following page.

Scalar DL Sandbox

Please visit this page and log in with your GitHub account. Then, the following page will be displayed. Click the "Download" button to download the necessary files. sandbox01.jpg

setup

When you unzip the downloaded file, the following files will be extracted.

$ unzip ~/Downloads/scalar-sandbox-client-{username}.zip
$ ls ~/Downloads/scalar-sandbox-client-{username}
client.properties {username}-key.pem {username}.pem

A brief description of the file is as follows.

Next, we will clone the repository.

$ git clone https://github.com/scalar-labs/scalardl-client-sdk.git
$ cd scalardl-client-sdk

After cloning the repository, let's copy the files downloaded earlier.

$ cp -r ~/Downloads/scalar-sandbox-client-{username}/* ./

Certificate registration

Next, register the certificate you copied earlier to join the Scalar DL Network.

$ client/bin/register-cert -properties client.properties

Contract registration

We will finally register the contract. The source of the contract exists in the cloned repository, but in order to register it, you need to compile it with the following command.

$ ./gradlew assemble

Now let's register the contract. This time, we will use StateUpdater to update the status and StateReader to check the status.

$ client/bin/register-contract -properties client.properties -contract-id {username}-StateUpdater -contract-binary-name com.org1.contract.StateUpdater -contract-class-file build/classes/java/main/com/org1/contract/StateUpdater.class

$ client/bin/register-contract -properties client.properties -contract-id {username}-StateReader -contract-binary-name com.org1.contract.Reader -contract-class-file build/classes/java/main/com/org1/contract/StateReader.class

The execution is simple, but the options are explained below.

Execution of contract

Once you get here, all you have to do is execute it. Let's use StateUpdater to set a value for a specific asset.

$ client/bin/execute-contract -properties client.properties -contract-id {username}-StateUpdater -contract-argument '{"asset_id": "{username}-myasset", "state": 3}'

status: 200

This is also very simple. If status: 200 is returned, the execution is successful. There is only one option that is different from the previous one.

Now that we've hit Updater, let's check the value with Reader.

client/bin/execute-contract -properties client.properties -contract-id {username}-StateReader -contract-argument '{"asset_id": "{username}-myasset"}'
status: 200
{"age":0,"input":{},"output":{"state":3},"contract_id":"{username}-StateUpdater","argument":{"asset_id":"{username}-myasset","state":3,"nonce":"b3a59ed7-e69a-49ea-83c4-7237296bf119"},"signature":"MEUCIBqkUbPI3l5TGx+MqjNFsUmv8UWJ0DYyjzeYgOauoB83AiEAuMwofkeQEJnTn/6W8sjilfawYcryPt3wZIgPfEprjTo=","hash":"fkXewkXuiof/PO87DKb6vfxdAMAisJ9WzXOvIyzXQoY=","prev_hash":""}

It's hard to see, so I'll format the output.

{
  "age": 0,
  "input": {},
  "output": {
    "state": 3
  },
  "contract_id": "{username}-StateUpdater",
  "argument": {
    "asset_id": "{username}-myasset",
    "state": 3,
    "nonce": "b3a59ed7-e69a-49ea-83c4-7237296bf119"
  },
  "signature": "MEUCIBqkUbPI3l5TGx+MqjNFsUmv8UWJ0DYyjzeYgOauoB83AiEAuMwofkeQEJnTn/6W8sjilfawYcryPt3wZIgPfEprjTo=",
  "hash": "fkXewkXuiof/PO87DKb6vfxdAMAisJ9WzXOvIyzXQoY=",
  "prev_hash": ""
}

If you pay attention to output, can you see that the value 3 set earlier is registered? And since this asset is the first registration, input and prev_hash are empty.

Finally, let's set different values for the same asset.

$ client/bin/execute-contract -properties client.properties -contract-id {username}-StateUpdater -contract-argument '{"asset_id": "{username}-myasset", "state": 100}'

status: 200

Let's check the value with StateReader in the same way as before.

client/bin/execute-contract -properties client.properties -contract-id {username}-StateReader -contract-argument '{"asset_id": "{username}-myasset"}'
{
  "age": 1,
  "input": {
    "{username}-myasset": {
      "age": 0,
      "data": {
        "state": 3
      }
    }
  },
  "output": {
    "state": 100
  },
  "contract_id": "{username}-StateUpdater",
  "argument": {
    "asset_id": "{username}-myasset",
    "state": 100,
    "nonce": "31f3c3d5-d7ab-4fd5-acae-fe0d0b95038c"},
  "signature": "MEUCIQC+pcBXSsAwmzsfX0xjXLEoANU47ONCA1e1xrQYsa+5pgIgToOUMKvv37nvZzAAFDR/EDKXe7fZzmfDWubM8TEMuTM=",
  "hash": "McnMW2rvOvLjeC4g6zc0yRd/ajX1Z3aIqeI9bQAc+ko=",
  "prev_hash": "fkXewkXuiof/PO87DKb6vfxdAMAisJ9WzXOvIyzXQoY="
}

The state value of output has been updated, but the difference from the return value of the first StateReader is that input and prev_hash are noticeable. The input contains the state of the previous asset, and prev_hash contains the hash ID of the previous asset. Hash chains are formed in this way.

Summary

So it's easy, but I tried running a sample contract. It's easy to use that the contract can be written in pure Java. Considering that Ethereum is Solidity, which is a proprietary language (although it is similar to JavaScript), and Hyperledger Fabric is mainly Go language, isn't it easy to introduce?

Currently, Hyperledger Fabric can be written in Node.js and Java in addition to Go language, so I would like to compare how to write and deploy code from the next time onwards.

Recommended Posts

I investigated the recently talked about distributed ledger Scalar DLT (smart contract execution)
I investigated how to write smart contracts for distributed ledger Scalar DLT
I tried to touch the asset management application using the emulator of the distributed ledger Scalar DLT