Deploying Java language smart contract on xuper chain and analyzing the implementation logic of certificate of deposit contract

Time:2020-10-12

preface

In this article, we will first briefly describe the process of deploying smart contracts written in Java language by native in the latest official release version, and then discuss the code implementation logic of the deposit certificate contract. In the next article, we will talk about how to define the data structure in the contract according to our company’s business logic to realize the smart contract

Let’s talk about the slow access to documents

The official document is https://xuperchain.readthedoc…_ documents/ XuperModel.html

The document is deployed abroad, so if you visit in China, if your network is not good, it is very slow to open the web page. In this case, you can download the corresponding version of the document to your own local, so that you will not be affected by the network speed

Download address:

https://readthedocs.org/proje…

Select the appropriate version

Deploying Java language smart contract on xuper chain and analyzing the implementation logic of certificate of deposit contract

Version upgrade supports smart contracts written in Java language

Around July 13, 2020, an official version was updated to support smart contracts written in Java on the master branch

Next, let’s first pull down the latest code, and then compile and run it. This process has been described in detail in the previous articles, so I won’t repeat it here. Interested friends can have a look at these two articles

Deploy smart contract on Baidu super chain xuper and realize the function of storing certificate, and further understand the process of using Baidu super chain xuper

Deployment of Java smart contract in native mode

Java smart contract is the way of native deployment, so you need to modify the configuration file to enable native contract deployment

`VIM per node path / conf/ xchain.yaml
#Manage configuration of native contract
native:
enable: true`

Start command

`nohup ./xchain &
There is no — VM ixvm. This is the way of wasm contract deployment`

Because the configuration is modified here, you need to delete the data first, and then start me. This is built in the local environment, so the node data is not very important. If the company’s data, then you don’t need to modify the configuration easily

Compilation environment

Compiling Java SDK: Java version is no less than Java 1.8 version

Package manager: maven, MVN version 3.6+

Configure Maven environment

`vim /etc/profile
export M2_HOME=/Users/mengfanxiao/Documents/third_software/maven/apache-maven-3.6.2
export PATH=$PATH:$M2_HOME/bin
source /etc/profile
mvn -v`

Compile contract SDK

`cd xuperchain/core/contractsdk/java
mvn install -f pom.xml`

The binary file target / java-contract-sdk-0.1.0.jar is generated and automatically installed in the MVN local repository

When compiling native contracts, take the counter contract in contractsdk / Java / example as an example

`cd contractsdk/java/example/counter
mvn package -f pom.xml`

Output binary file target / counter-0.1.0-jar-with- dependencies.jar , for contract deployment

Contract deployment

./xchain-cli native deploy --account [email protected] --fee 15587517 --runtime java xuperchain/data/javacontract/counter-0.1.0-jar-with-dependencies.jar --cname javacounter

Contract call

  • Command line call

./xchain-cli native invoke --method Increase -a '{"key":"test"}' javacounter --fee 10

  • Initiate contract transaction through Java SDK

`@Test
public void invokeContract() throws Exception {
Map<String, byte[]> args = new HashMap<>();
args.put(“key”, “icexin”.getBytes());
Transaction tx = client.invokeContract(account, “native”, “javacounter”, “increase”, args);
System.out.println(“invoke txid: ” + tx.getTxid());
System.out.println(“response: ” + tx.getContractResponse().getBodyStr());
System.out.println(“gas: ” + tx.getGasUsed());
}`

Note that it must be native mode

Contract inquiry

  • Query through Java SDK

`@Test
public void queryContractJava() throws Exception {
Map<String, byte[]> args = new HashMap<>();
args.put(“key”, “icexin”.getBytes());
Transaction tx = client.queryContract(account, “native”, “javacounter”, “get”, args);
System.out.println(“response: ” + tx.getContractResponse().getBodyStr());
System.out.println(“gas: ” + tx.getGasUsed());
}`

Smart contract deployment in go language under native mode

Take the counter contract file as an example

`cd contractsdk/go/example/counter
go build counter.go`

Deployment contract document

./xchain-cli native deploy --account [email protected] -a '{"creator":"[email protected]"}' --fee 15587517 --runtime go xuperchain/data/gocontract/counter --cname golangcounter

Call contract file

  • Command line call

./xchain-cli native invoke --method Increase -a '{"key":"test"}' golangcounter --fee 10

  • Javasdk call

`@Test
public void invokeContractG0() throws Exception {
Map<String, byte[]> args = new HashMap<>();
args.put(“key”, “icexin”.getBytes());
Transaction tx = client.invokeContract(account, “native”, “golangcounter”, “increase”, args);
System.out.println(“invoke txid: ” + tx.getTxid());
System.out.println(“response: ” + tx.getContractResponse().getBodyStr());
System.out.println(“gas: ” + tx.getGasUsed());
}`

Inquiry of contract documents

Query through javasdk

`@Test
public void queryContractGo() throws Exception {
Map<String, byte[]> args = new HashMap<>();
args.put(“key”, “icexin”.getBytes());
Transaction tx = client.queryContract(account, “native”, “golangcounter”, “get”, args);
System.out.println(“response: ” + tx.getContractResponse().getBodyStr());
System.out.println(“gas: ” + tx.getGasUsed());
}`

After deploying as like as two peas of Java and go, we actually know that the process is the same as the counter contract.

Possible abnormal information

  • If contract type native not found is reported, the native contract configuration is not enabled
  • If context deadline exceeded is reported, it indicates that you may be using an old version of the contract file, so you need to recompile the contract source code of the new version to generate the contract file
    • *

The above briefly describes the deployment process based on the latest version as of July 15, and the process of deploying smart contracts written in go and Java through native contract deployment. The following describes the implementation logic of the smart contract with the company’s business

There is no contract template written in Java language in the current version of CD smart contract, which will be available in about two weeks

Let’s analyze the logic of the deposit contract in go language

Let’s talk about the data structure

`Data structure of user object
{
“Owner”: “Xiaoming”, which means the user name
“User files”: {this is a map collection
“Filehash”: {# the key of the map collection is a file hash
“Timestamp”: “ා timestamp
“Hashval”: “# [] byte (filehash) is also a hash value
}
}
}`

Data initialization during contract deployment

Deploying Java language smart contract on xuper chain and analyzing the implementation logic of certificate of deposit contract

Deploying Java language smart contract on xuper chain and analyzing the implementation logic of certificate of deposit contract

The initialize method is called when the contract is deployed

First, get the owner parameter in the deployment command to indicate the user name and then call the method GetObject on the chain to query the value corresponding to the user name.

There are only two methods on the chain called by the smart contract. One is putobject, which saves the key value on the chain

The other is GetObject to get the corresponding value according to the key

If the value is empty, an empty user object is initialized and converted to a JSON string and saved in the chain

The method of contract on chain transaction

Deploying Java language smart contract on xuper chain and analyzing the implementation logic of certificate of deposit contract

This method first queries the corresponding value on the chain according to the owner (representing the user name). According to the above analysis, the value holds a user object. When the first call is made, the user object is not initialized inside the attribute, then the putFile method is called.

Deploying Java language smart contract on xuper chain and analyzing the implementation logic of certificate of deposit contract

First, initialize a userfile object, which stores the information in the chain

Save the object as an element to the userfiles in the user object. In the map collection, key is filehash value is userfile

The user object is then converted into a JSON string and saved on the chain

Contract transaction inquiry

Deploying Java language smart contract on xuper chain and analyzing the implementation logic of certificate of deposit contract

The user object is found in the chain through the user name

Deploying Java language smart contract on xuper chain and analyzing the implementation logic of certificate of deposit contract

According to the filehash, traverse the userfiles map collection of the user object to find the corresponding userfile element

As of here, have you found the logic of the template of the deposit contract actually very simple

Summarize the above logic

Two methods in smart contract call chain

  • Putobject saves K-V in chain
  • GetObject queries V through K

A data structure

  • There is a map collection in a user object, and the elements in the set are the data of each link. You can locate the data in the chain through the key of the map

Problem finding

Filehash repeat

Have you seen the above code and found a problem

Deploying Java language smart contract on xuper chain and analyzing the implementation logic of certificate of deposit contract

The red area is to put the single uplink data userfile into the map set, and the key is filehash. If the filehash uploaded twice is the same, the value corresponding to the previous filehash will be replaced

However, the same uplink data returns different transaction IDs. What is the internal implementation mechanism? And how to define the data structure according to the company’s business to realize the smart contract.

This paper uses mdnice typesetting