Search…
Tutorial

Goals

  • Build docker image for local.
  • Build javaee-api.jar for local.
  • Deploy sample java SCORE using goloop CLI binary.

Requirements

  • You need to install OpenJDK 11 version. Visit OpenJDK.net for prebuilt binaries. Or you can install a proper OpenJDK package from your OS vendors.
    In macOS:
    1
    $ brew tap AdoptOpenJDK/openjdk
    2
    $ brew install --cask adoptopenjdk11
    Copied!
    In Linux (Ubuntu 18.04):
    1
    $ sudo apt install openjdk-11-jdk
    Copied!
  • Download and Install GoLang
    In macOS:
    1
    $ brew install go
    Copied!
    In Linux (Ubuntu 18.04):
    1
    $sudo wget -c https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local
    2
    $export PATH=$PATH:/usr/local/go/bin
    Copied!
    Verify installation
    1
    go version
    Copied!
  • Download and Install Pyhton + VM
    In macOS:
    1
    brew install python3
    2
    pip3 install virtualenv setuptools wheel
    Copied!
    In Linux (Ubuntu 18.04):
    1
    $ sudo apt install python3
    2
    $ pip3 install virtualenv setuptools wheel
    Copied!
  • Download and Install rocksdb
    In macOS:
    1
    $ brew install rocksdb
    Copied!
    In Linux (Ubuntu 18.04):
    1
    git clone https://github.com/facebook/rocksdb.git
    2
    cd rocksdb
    3
    4
    DEBUG_LEVEL=0 make shared_lib install-shared
    5
    6
    export LD_LIBRARY_PATH=/usr/local/lib
    Copied!

Step 1. Source checkout

First of all, you need to checkout the gochain-local repository for executing local node.
1
$ git clone [email protected]:icon-project/gochain-local.git
2
$ GOCHAIN_LOCAL_ROOT=/path/to/gochain-local
Copied!
Then, you need to checkout the goloop repository for building docker image and new javaee-api.jar.
1
$ git clone [email protected]:icon-project/goloop.git
2
$ GOLOOP_ROOT=/path/to/goloop
Copied!
And last, you need to checkout the java-score-examples repository for sample java SCORE.
1
$ git clone [email protected]:icon-project/java-score-examples.git
2
$ JAVA_SCORE_EXAMPLES_ROOT=/path/to/java-score-examples
Copied!

Step 2. Build Docker image and goloop CLI for local

First of all, you need checkout git specific branch and run make file.
1
$ cd ${GOLOOP_ROOT}
2
$ git checkout master # use the latest stable release
3
$ make gochain-icon-image
Copied!
If the command runs successfully, it generates the docker image like the following.
1
$ docker images goloop/gochain-icon
2
3
REPOSITORY TAG IMAGE ID CREATED SIZE
4
goloop/gochain-icon latest 73927da2b1a0 20 seconds ago 512MB
Copied!
Then, you need build & copy built goloop binary file toGOCHAIN_LOCAL_ROOT.
1
$ make goloop
2
$ cp ./bin/goloop ${GOCHAIN_LOCAL_ROOT}/goloop
Copied!

Step 3. Build javaee-api.jar for local

If you want to use RLP method, you need to build javaee-api.jar for development.
1
$ cd ${GOLOOP_ROOT}/javaee
Copied!
First of all, you can make api-0.8.7-SNAPSHOT.jar by using gradle script cmd.
1
$ ./gradlew api:build
Copied!
If the command runs successfully, it generates the jar file on ./api/build/libs/.
Then copy the jar file to java-score-examples/hello-world.
1
$ cp ./api/build/libs/api-0.8.7-SNAPSHOT.jar ${JAVA_SCORE_EXAMPLES_ROOT}/hello-world/api-0.8.7-SNAPSHOT.jar
Copied!
If you want to get information about how to use RLP method, you can open local javadoc from javaee/api/build/javadoc/index.html.

Step 4. Build sample java SCORE for local

1
$ cd ${JAVA_SCORE_EXAMPLES_ROOT}
Copied!
First of all, edit java-score-example/hello-world/build.gradle as below.
1
...
2
dependencies {
3
# use the local api jar for testing
4
compile files('api-0.8.7-SNAPSHOT.jar')
5
6
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
7
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
8
}
9
...
Copied!
Prepare hello-world/src/main/java/com/iconloop/score/example/HelloWorld.java file as below.
1
/*
2
* Copyright 2021 ICONLOOP Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
package com.iconloop.score.example;
18
19
import score.Context;
20
import score.ObjectReader;
21
import score.ByteArrayObjectWriter;
22
import score.annotation.External;
23
import score.annotation.Payable;
24
25
public class HelloWorld {
26
private final String name;
27
28
public HelloWorld(String name) {
29
this.name = name;
30
}
31
32
@External(readonly=true)
33
public String name() {
34
return name;
35
}
36
37
@External(readonly=true)
38
public String getGreeting() {
39
String msg = "Hello " + name + "!";
40
Context.println(msg);
41
return msg;
42
}
43
44
@Payable
45
public void fallback() {
46
// just receive incoming funds
47
}
48
49
@External
50
public void testRlp() {
51
var codec = "RLPn";
52
var msg = "testRLP";
53
54
ByteArrayObjectWriter w = Context.newByteArrayObjectWriter(codec);
55
w.write(msg.getBytes());
56
ObjectReader r = Context.newByteArrayObjectReader(codec, msg.getBytes());
57
}
58
}
Copied!

Build the SCORE

1
$ ./gradlew build
Copied!
The compiled jar bundle will be generated at ./hello-world/build/libs/hello-world-0.1.0.jar.

Optimize the jar

You need to optimize your jar bundle before you deploy it to local or ICON networks. This involves some pre-processing to ensure the actual deployment successful.
gradle-javaee-plugin is a Gradle plugin to automate the process of generating the optimized jar bundle. Run the optimizedJar task to generate the optimized jar bundle.
1
$ ./gradlew optimizedJar
Copied!
The output jar will be located at ./hello-world/build/libs/hello-world-0.1.0-optimized.jar.

Copy example SCORE to GOCHAIN_LOCAL_ROOT

1
$ cp ./hello-world/build/libs/hello-world-0.1.0-optimized.jar ${GOCHAIN_LOCAL_ROOT}
Copied!

Step 5. Start gochain docker container

1
$ cd ${GOCHAIN_LOCAL_ROOT}
Copied!
Prepare run_gochain-icon.sh file as below.
1
#!/bin/bash
2
3
usage() {
4
echo "Usage: $0 [start|stop] (docker-tag)"
5
exit 1
6
}
7
8
if [ $# -eq 1 ]; then
9
CMD=$1
10
TAG=latest
11
elif [ $# -eq 2 ]; then
12
CMD=$1
13
TAG=$2
14
else
15
usage
16
fi
17
18
startDocker() {
19
local dockerEnv=$1
20
local port=$2
21
echo ">>> START $dockerEnv $port $TAG"
22
docker run -dit -v $PWD:/testsuite -p $port:$port \
23
--env-file data/dockerenv/$dockerEnv \
24
--name gochain-$dockerEnv \
25
goloop/gochain-icon:$TAG
26
}
27
28
stopDocker() {
29
echo ">>> STOP gochain-$1"
30
docker stop gochain-$1
31
docker rm gochain-$1
32
}
33
34
DOCKER_ENV=iconee
35
PORT=9082
36
37
case "$CMD" in
38
start )
39
startDocker $DOCKER_ENV $PORT
40
;;
41
stop )
42
stopDocker $DOCKER_ENV
43
;;
44
* )
45
echo "Error: unknown command: $CMD"
46
usage
47
esac
Copied!
Start gochain-icon container.
1
$ ./run_gochain-icon.sh start
2
>>> START iconee 9082 latest
3
48e4c66fec68d01e767da91cbbb043c03f595b33cac69c8cdf94f39eaa03b34e
4
5
$ docker ps
6
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7
48e4c66fec68 goloop/gochain-icon:latest "/entrypoint /bin/sh…" 9 seconds ago Up 8 seconds 8080/tcp, 9080/tcp, 0.0.0.0:9082->9082/tcp gochain-iconee
Copied!
Note that log messages will be generated at ./chain/iconee.log.
1
$ head ./chain/iconee.log
2
I|20210125-05:41:05.997850|b6b5|-|main|main.go:431 ____ ___ ____ _ _ _ ___ _ _
3
I|20210125-05:41:05.997953|b6b5|-|main|main.go:431 / ___|/ _ \ / ___| | | | / \ |_ _| \ | |
4
I|20210125-05:41:05.997964|b6b5|-|main|main.go:431 | | _| | | | | | |_| | / _ \ | || \| |
5
I|20210125-05:41:05.997973|b6b5|-|main|main.go:431 | |_| | |_| | |___| _ |/ ___ \ | || |\ |
6
I|20210125-05:41:05.997990|b6b5|-|main|main.go:431 \____|\___/ \____|_| |_/_/ \_\___|_| \_|
7
I|20210125-05:41:05.998006|b6b5|-|main|main.go:433 Version : v0.1.15-1039-g9f22c115
8
I|20210125-05:41:05.998057|b6b5|-|main|main.go:434 Build : linux/amd64 tags()-2021-01-25-04:23:23
9
I|20210125-05:41:05.998094|b6b5|-|metric|metric.go:150 Initialize rootMetricCtx
10
T|20210125-05:41:05.998278|b6b5|-|TP|transport.go:383 registerPeerHandler &{0xc0001e6750 0xc0001e66f0 map[] {{0 0} 0 0 0 0} 0xc0001e67b0} true
11
T|20210125-05:41:05.998304|b6b5|-|TP|transport.go:383 registerPeerHandler &{0xc0001e66c0 :8080} true
Copied!

Stop the container (if test done)

1
$ ./run_gochain-icon.sh stop
2
>>> STOP gochain-iconee
3
gochain-iconee
4
gochain-iconee
Copied!

Step 6. Deploy the optimized jar (hello-world SCORE with RLP)

You can deploy the optimized jar by using goloop CLI binary.
1
$ cd ${GOCHAIN_LOCAL_ROOT}
2
$ ./goloop rpc sendtx deploy ./hello-world-0.1.0-optimized.jar \
3
--uri http://localhost:9082/api/v3 \
4
--key_store ./data/godWallet.json --key_password gochain \
5
--nid 3 --step_limit 10000000000 \
6
--content_type application/java \
7
--param name=GoLoop
8
"0xfee1e31e3ecb88106e785a6cb8b0b957e42f5f908a7c2c66a0c19aebd659f7ef"
Copied!
Check the deployed SCORE address first using the txresult command.
1
$ ./goloop rpc txresult 0xfee1e31e3ecb88106e785a6cb8b0b957e42f5f908a7c2c66a0c19aebd659f7ef \
2
--uri http://localhost:9082/api/v3
3
{
4
"to": "cx0000000000000000000000000000000000000000",
5
"cumulativeStepUsed": "0x3d70a5c3",
6
"stepUsed": "0x3d70a5c3",
7
"stepPrice": "0x2e90edd00",
8
"eventLogs": [],
9
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
10
"status": "0x1",
11
"scoreAddress": "cxd1f5d12e92459a4fcdf2678a14b572687471a70e",
12
"blockHash": "0xe678a4a19d43c54c709c16b4d794e59a3214af64d9efd860eb8f97fc6bdece7d",
13
"blockHeight": "0x1b6",
14
"txIndex": "0x0",
15
"txHash": "0xfee1e31e3ecb88106e785a6cb8b0b957e42f5f908a7c2c66a0c19aebd659f7ef"
16
}
Copied!
Then you can query getGreeting method via the following call command.
1
$ ./goloop rpc call --to cxd1f5d12e92459a4fcdf2678a14b572687471a70e \
2
--method getGreeting \
3
--uri http://localhost:9082/api/v3
4
"Hello GoLoop!"
Copied!
And you can invoke testRlp method via the following sendtx command.
1
$ ./goloop rpc sendtx call --to cxd1f5d12e92459a4fcdf2678a14b572687471a70e \
2
--method testRlp \
3
--uri http://localhost:9082/api/v3 \
4
--key_store ./data/godWallet.json --key_password gochain \
5
--nid 3 --step_limit 10000000000
6
"0x07868af25c42e0d201073eae9d490d895e0922a431918199aa3bd461d6d9e65f"
Copied!
Check the called SCORE address using the txresult command.
1
$ ./goloop rpc txresult 0x07868af25c42e0d201073eae9d490d895e0922a431918199aa3bd461d6d9e65f \
2
--uri http://localhost:9082/api/v3
3
{
4
"to": "cxd1f5d12e92459a4fcdf2678a14b572687471a70e",
5
"cumulativeStepUsed": "0x1fe85",
6
"stepUsed": "0x1fe85",
7
"stepPrice": "0x2e90edd00",
8
"eventLogs": [],
9
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
10
"status": "0x1",
11
"blockHash": "0xae2be6e87b715f786c2ba599f5f570f9a4f018a20338cfb1d055ae3463d65571",
12
"blockHeight": "0x2bc",
13
"txIndex": "0x0",
14
"txHash": "0x07868af25c42e0d201073eae9d490d895e0922a431918199aa3bd461d6d9e65f"
15
}
Copied!
Last modified 13d ago