FISCO BCOS Blockchain (1)

Table of contents

1. Build FISCO BCOS chain

1. Install openssl, curl dependencies

2. Install the build_chain script

2. Configure the console

1. Download java jdk

2. Install the console

3. Copy configuration files and certificates

4. Start the console

3. Java-SDK (idea version)

1. Create project

 2. Introduce Java-SDK dependency

3、sol2java.sh

4. Import the required resources

5. Create test class


Refer to FISCO BCOS official document v2.8.0

1. Build FISCO BCOS chain

1. Install openssl, curl dependencies

sudo yum install -y openssl openssl-devel

2. Install the build_chain script

Create a new fasco folder in the home directory and switch to the fisco directory

cd ~ && mkdir -p fisco && cd fisco

download script

curl -#LO https://osp-1257653870.cos.ap-guangzhou.myqcloud.com/FISCO-BCOS/FISCO-BCOS/releases/v2.8.0/build_chain.sh

Modify the build_chain script file into an executable file

chmod u+x build_chain.sh

Generate a FISCO chain with a single group of 4 nodes

bash build_chain.sh -l 127.0.0.1:4 -p 30300,20200,8545

The -p option specifies the starting port, which are p2p_port(30300), channel_port(20200), jsonrpc_port(8545)

After the execution is successful, a nodes directory is generated under the fabric path, which contains the generated nodes and certificates

start chain

bash nodes/127.0.0.1/start_all.sh

2. Configure the console

1. Download java jdk

sudo yum install -y java java-devel

2. Install the console

cd ~/fisco && curl -#LO https://gitee.com/FISCO-BCOS/console/raw/master-2.0/tools/download_console.sh

3. Copy configuration files and certificates

Note: The startup of the client requires the relevant configuration files of the fasco bcos chain and the ca generated when the chain is generated

cp -n console/conf/config-example.toml console/conf/config.toml
cp -r nodes/127.0.0.1/sdk/* console/conf/

4. Start the console

cd ~/fisco/console && bash start.sh

For relevant instructions on the console, please refer to the official documentation (including solutions to compiling solidity contracts higher than version 0.4.25 )  . Console version 2.6+ https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/console /console_of_java_sdk.html

3. Java-SDK (idea version)

Java SDK provides a Java API for accessing FISCO BCOS nodes, supporting functions such as node status query, deployment and contract calling. Blockchain applications can be developed based on Java SDK.

First ensure that a FISCO BCOS chain has been started

This example uses the built-in FISCO BCOS

1. Create project

Create a Spring Boot project under idea

 2. Introduce Java-SDK dependency

Import the Maven coordinates of Java-SDK in pom.xml

        <dependency>
            <groupId>org.fisco-bcos.java-sdk</groupId>
            <artifactId>fisco-bcos-java-sdk</artifactId>
            <version>2.8.0</version>
        </dependency>

3、sol2java.sh

Note: sol2java.sh can not only compile ABI and BIN files, but also automatically generate a contract Java class with the same name as the compiled smart contract.

Enter ~/fisco/console  and use sol2java.sh. The specific code is as follows

bash sol2java.sh -p org.fisco.bcos

The generated .java file is saved in the package path set by -p (org.fisco.bcos)

4. Import the required resources

Including: certificate, configuration file, abi, bin, JavaBean

 Among them, the certificate can be copied from ~/fisco/nodes/127.0.0.1/sdk and placed in the src/main/resources/conf directory

 The configuration file can be copiedfrom ~/fisco/console/conf and placed in the src/main/resources  directory. The IP address in the config-example.toml file needs to be changed from 127.0.0.1 to the IP address of the virtual machine.

Note: The chain code written in solidity language needs to be compiled into ABI and BIN files before it can be deployed on the blockchain network using Java-SDK

The abi and bin  files can be copied from ~/fisco/console/contracts/sdk/ path

Copy JavaBean from  ~/fisco/console/contracts/sdk/java/org/fisco/bcos/

5. Create test class

Create the corresponding package in the test directory and create BcosSDKTest.java in the package

Core code:

// 获取配置文件路径
    public final String configFile = BcosSDKTest.class.getClassLoader().getResource("config-example.toml").getPath();
    @Test
    public void testClient() throws ConfigException {
        // 初始化BcosSDK
        BcosSDK sdk =  BcosSDK.build(configFile);
        // 为群组1初始化client
        Client client = sdk.getClient(Integer.valueOf(1));
        // 向群组1部署HelloWorld合约
        CryptoKeyPair cryptoKeyPair = client.getCryptoSuite().getCryptoKeyPair();
        HelloWorld helloWorld = null;
        try {
            helloWorld = HelloWorld.deploy(client, cryptoKeyPair);
        } catch (ContractException e) {
            e.printStackTrace();
        }

You can obtain on-chain information through the client object and operate the chain code through the helloworld object.

Guess you like

Origin blog.csdn.net/m0_47233175/article/details/123748292