[JAVA] I tried to touch the asset management application using the emulator of the distributed ledger Scalar DLT

This time, previous I will try using the Scalar DLT emulator using the asset management application explained. The execution environment will continue to work on macOS 10.14.4.

Get and run the emulator

First, get the emulator. Get it with the following command.

$ git clone https://github.com/scalar-labs/scalardl-tools

After cloning, build it.

$ cd scalardl-tools/emulator
$ ./gradlew installDist

Let's run it immediately after building.

$ ./build/install/emulator/bin/emulator
Scalar DL Emulator
Type 'help' for more information
scalar>

If you wait for input, the startup is complete. It will start up quickly!

Get an asset management application

Next, let's get the asset management application. The location of the source was introduced last time, so let's clone it immediately.

$ git clone https://github.com/indetail-blockchain/getting-started-with-scalardl.git

Since two projects are used, the directory structure is as follows.

git/  ├ scalardl-tools/  └ getting-started-with-scalardl/

Build an asset management application

In the previous article, I introduced that there are 6 smart contracts available. The source is actually located in the following location.

$ ls -l getting-started-with-scalardl/src/main/java/com/scalar/am/contract
AddAssetContract.java
AddTypeContract.java
AssetHistoryContract.java
ListContract.java
ListTypeContract.java
StatusChangeContract.java

Let's build this. Build is possible by placing the source under src / main / java of the emulator.

$ cp -r getting-started-with-scalardl/src/main/java/com scalardl-tools/emulator/src/main/java/
$ rm -rf scalardl-tools/emulator/src/main/java/com/scalar/am/command
$ rm -f scalardl-tools/emulator/src/main/java/com/scalar/am/AssetManager.java
$ cd scalardl-tools/emulator/
$ ./gradlew build

Once it's successfully built, take a look at the build directory.

$ ls -l build/classes/java/main/com/scalar/am/contract
AddAssetContract.class
AddTypeContract.class
AssetHistoryContract.class
ListContract.class
ListTypeContract.class
StatusChangeContract.class

You have a class file.

Register and execute smart contracts

Now it's time to execute the smart contract. First, let's start the emulator.

$ cd scalardl-tools/emulator
$ ./build/install/emulator/bin/emulator
Scalar DL Emulator
Type 'help' for more information
scalar>

First, register the contract. Register 6 in total.

scalar> register add-type com.scalar.am.contract.AddTypeContract ../../getting-started-with-scalardl/build/com/scalar/am/contract/AddTypeContract.class {"holderId": "Admin"}
Contract 'add-type' successfully registered

scalar> register add-asset com.scalar.am.contract.AddAssetContract ../../getting-started-with-scalardl/build/com/scalar/am/contract/AddAssetContract.class {"holderId": "Admin"}
Contract 'add-asset' successfully registered

scalar> register list-type com.scalar.am.contract.ListTypeContract ../../getting-started-with-scalardl/build/com/scalar/am/contract/ListTypeContract.class {"holderId": "Admin"}
Contract 'list-type' successfully registered

scalar> register list-asset com.scalar.am.contract.ListContract ../../getting-started-with-scalardl/build/com/scalar/am/contract/ListContract.class {"holderId": "Admin"}
Contract 'list-asset' successfully registered

scalar> register status-change com.scalar.am.contract.StatusChangeContract ../../getting-started-with-scalardl/build/com/scalar/am/contract/StatusChangeContract.class {"holderId": "Admin"}
Contract 'status-change' successfully registered

scalar> register asset-history com.scalar.am.contract.AssetHistoryContract ../../getting-started-with-scalardl/build/com/scalar/am/contract/AssetHistoryContract.class {"holderId": "Admin"}
Contract 'asset-history' successfully registered

The specifications of the register command for registration are as follows.

register [Name to use at runtime] [Java class name] [Class file path] [Initial parameters]

Initial parameters can be passed when creating a smart contract, but this asset management application requires you to specify holderId. This holderId is passed only at the time of registration and has a value that does not change. By using and checking this in the smart contract, it is possible to ** prevent others from updating the asset information that you have registered **. The following is a simple image. auth.jpg Since Scalar DLT does not control the reference / rewrite authority for assets, it is possible to prevent unexpected data updates in this way.

Let's finally do it. Use ʻexecute` for execution.

execute [Registered name of smart contract] [Parameters]

First, register the asset type.

scalar> execute add-type {"name": "Tablet"}

{
   "result": "success",
   "message": "type Tablet put completed."
}
scalar> execute add-type {"name": "SmartPhone"}

{
   "result": "success",
   "message": "type SmartPhone put completed."
}

Added types called Tablet and SmartPhone. Next, let's check if it is really registered.

scalar> execute list-type {}

{
   "result": "success",
   "message": "get list completed.",
   "types": [
       {
           "type": "SmartPhone",
           "age": 1
       },
       {
           "type": "Tablet",
           "age": 0
       }
   ]
}

It is saved properly. The caveat here is that if you have no arguments, you pass an empty array {}. Please note that if you omit this, an error will occur.

Next, let's register the asset.

scalar>

scalar> execute add-asset {"type": "Tablet", "asset": "iPad", "timestamp": 20190701120000, "id": "1001"}

{
   "result": "success",
   "message": "asset iPad put completed."
}

You have registered an iPad with an ID of 1001 that is classified as a Tablet. I am registering a UNIX timestamp for timestamp, but I have specified a temporary value for easy understanding. I will also check if the data is registered.

scalar> execute list-asset {"type": "Tablet"}

{
   "result": "success",
   "message": "get list completed.",
   "Tablet": [
       {
           "id": "1001",
           "name": "iPad",
           "timestamp": 20190701120000,
           "status": "in-stock"
       }
   ]
}

Certainly the iPad is registered. The status is in-stock, so let's borrow it right away.

scalar> execute status-change {"asset_id": "1001", "status": "on-loan", "timestamp": 20190701130000}

{
   "result": "success",
   "message": "Borrowed"
}

scalar> execute list-asset {"type": "Tablet"}

{
   "result": "success",
   "message": "get list completed.",
   "Tablet": [
       {
           "id": "1001",
           "name": "iPad",
           "timestamp": 20190701130000,
           "status": "on-loan",
           "holderId": "Admin"
       }
   ]
}

I was able to borrow it safely. The timestamp has also been updated and the status has changed to on-loan. Next, let's return it.

scalar> execute status-change {"asset_id": "1001", "status": "in-stock", "timestamp": 20190701140000}

{
   "result": "success",
   "message": "Returned"
}

scalar> execute list-asset {"type": "Tablet"}

{
   "result": "success",
   "message": "get list completed.",
   "Tablet": [
       {
           "id": "1001",
           "name": "iPad",
           "timestamp": 20190701140000,
           "status": "in-stock"
       }
   ]
}

This was also successful. Finally, let's check the rental history.

scalar> execute asset-history {"id": "1001"}

{
   "result": "success",
   "message": "get history complete.",
   "history": [
       {
           "timestamp": 20190701140000,
           "status": "in-stock",
           "age": 2
       },
       {
           "timestamp": 20190701130000,
           "status": "on-loan",
           "age": 1,
           "holderId": "Admin"
       },
       {
           "timestamp": 20190701120000,
           "status": "in-stock",
           "age": 0
       }
   ]
}

Registration, lending, returning and history remain.

Summary

That's why I tried running an asset management application using an emulator. The emulator starts up quickly and operates quickly, so work progresses. Since I used the emulator this time, the contract and data will be lost if I exit. So next, I would like to follow the actual registration to the Sandbox environment and its call.

Recommended Posts

I tried to touch the asset management application using the emulator of the distributed ledger Scalar DLT
I tried using the cache function of Application Container Cloud Service
I investigated how to write smart contracts for distributed ledger Scalar DLT
I tried using the profiler of IntelliJ IDEA
I investigated the recently talked about distributed ledger Scalar DLT (smart contract execution)
I tried using the Server Push function of Servlet 4.0
I tried to summarize the state transition of docker
I tried to reduce the capacity of Spring Boot
I tried using the Migration Toolkit for Application Binaries
I tried to investigate the mechanism of Emscripten by using it with the Sudoku solver
I tried to summarize the basics of kotlin and java
[Swift] I tried to implement the function of the vending machine
I tried to make the sample application into a microservice according to the idea of the book "Microservice Architecture".
I tried to summarize the basic grammar of Ruby briefly
I tried to build the environment little by little using docker
I tried to build the environment of WSL2 + Docker + VSCode
I tried to develop the cache function of Application Container Cloud Service in the local environment
I tried to make the "Select File" button of the sample application created in the Rails tutorial cool
After learning Progate, I tried to make an SNS application using Rails in the local environment
[Metal] I tried to figure out the flow until rendering using Metal
I tried to build the environment of PlantUML Server with Docker
I tried to build a simple application using Dockder + Rails Scaffold
I tried to check the operation of gRPC server with grpcurl
I tried to summarize the methods of Java String and StringBuilder
I tried to display the calendar on the Eclipse console using Java.
I tried to solve the problem of Google Tech Dev Guide
I tried to explain the method
[Rails] I tried deleting the application
I tried using GoogleHttpClient of Java
I tried to make a sample program using the problem of database specialist in Domain Driven Design
I want to display the number of orders for today using datetime.
I tried to summarize the stumbling points when developing an Android application
I tried to summarize the key points of gRPC design and development
I tried to make full use of the CPU core in Ruby
I tried to visualize the access of Lambda → Athena with AWS X-Ray
I tried to make a talk application in Java using AI "A3RT"
I tried to measure and compare the speed of GraalVM with JMH
I tried to publish the reflex measurement application on the Google Play store
I tried to summarize the methods used
I tried using Scalar DL with Docker
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried to touch JavaScript Part.2 Object-oriented
I tried to compare the infrastructure technology of engineers these days with cooking.
I tried to clone a web application full of bugs with Spring Boot
[Beginner] I tried to decorate the bar after displaying the details of the hamburger menu
[JDBC ③] I tried to input from the main method using placeholders and arguments.
I tried using Docker for the first time
I want to output the day of the week
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
I tried to develop a man-hour management tool
I tried to chew C # (basic of encapsulation)
I want to var_dump the contents of the intent
[API] I tried using the zip code search API
I tried to set tomcat to run the Servlet.
I tried to develop an application in 2 languages
I tried to implement a server using Netty
I tried to summarize the points to consider when acquiring location information with the iOS application ③
I tried to check the operation of http request (Put) with Talented API Tester
I tried to create a log reproduction script at the time of apt install