Hyperledger Caliper Besu stepped on the pit to summarize and test the contract he wrote

Posted by trg on Tue, 22 Feb 2022 10:25:07 +0100

Abstract

The first article of stepping on the pit writes the example given on the official website, which records how to test your Contract
Caliper version is v0 four point two

Repository

  • In this article, we continue to use Github Repo on the official website to create a new contract. In order to simplify, we still use Sample to change the contract name and function name. Storage is the same as Logic

  • Open source on Github
    https://github.com/hyperledger/caliper-benchmarks

Table of contents

  1. Chapter1: installing Caliper
  2. Chapter 2: writing contracts & generating abi JSON file
  3. Chapter 3: network configuration and test task configuration
  4. Reference

Chapter1

git clone git@github.com:hyperledger/caliper-benchmarks.git

You need to init the project here, otherwise the following steps will make mistakes

cd caliper-benchmarks
npm init

Install caliper
Bind to the latest version of besu. Note that the binding version can be selected here, but the subsequent series of settings should be in line with this version

npm install --only=prod @hyperledger/caliper-cli@0.4.2
npx caliper bind --caliper-bind-sut besu:latest

Chapter2

New contract under new path / SRC / candy / candy Sol (in fact, the path can be arbitrary. It's OK to specify a good place when calling)
Add new contract content

  • Here, we need to pay attention to the solid version of the contract, which should be consistent with the later compiler version
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;

contract candy {
    mapping(string => int) private accounts;

    function open_candy(string memory acc_id, int amount) public {
        accounts[acc_id] = amount;
    }

    function query_candy(string memory acc_id) public view returns (int amount) {
        amount = accounts[acc_id];
    }

    function transfer_candy(string memory acc_from, string memory acc_to, int amount) public {
        accounts[acc_from] -= amount;
        accounts[acc_to] += amount;
    }
}

Caliper needs abi JSON file, where solcjs tool is used to generate internal data. Frequently used small partners can add - g to the global

npm install solc  

The file is generated in the current path

cd caliper/src/ethereum/candy 
solcjs --abi ./candy.sol
solcjs --bin ./candy.sol

If the following exceptions occur, the contract Solidity version needs to be changed

ParserError: Source file requires different compiler version (current compiler is 0.8.11+commit.d7f03943.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version
 --> candy.sol:1:1:
  |
1 | pragma solidity >=0.4.22 <0.6.0;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Caliper vs abi JSON file content requirements, Contract definition file The following four keywords are required in

  • Name: whatever. I'll take Candy here
  • abi: the content generated by the above - abi instruction
  • Byetcode: the content generated by the - bin instruction above
  • Gas: it must be filled in. You can try deploying on Remix to see how much it costs, or use the Simulation tool to estimate the Besu network. This has little impact, not too small

Then you can create a new json file named candy json, put the content together
Note that bytecode is a string of numbers generated by - bin, preceded by 0x

{
    "name": "Candy",
    "abi": [{"constant":true,"inputs":[{"nam......ype":"function"}],
    "bytecode": "0x608060405234801.........0033",
    "gas": 4700000
}

If the following exception occurs, the gas deployed in the contract is not enough. Change the gas in the json file above a little larger

2022.02.22-13:59:30.074 error [caliper] [caliper-engine]        Error while performing "install" step: Error: The contract code couldn't be stored, please check your gas limit.
<a id="#Chapter3"></a>

Chapter3

The functions to be tested in each contract are in open js, query. js, transfer. JS
Replace the actual function name with the original Sample

    async submitTransaction() {
        const queryArgs = this.simpleState.getQueryArguments();
        await this.sutAdapter.sendRequests(this.createConnectorRequest('query_candy', queryArgs));
    }

Each test function is utils / operation base JS inheritance, in utils / simple state The Sample code in JS will generate the account and the corresponding initial balance according to the random number.
Initial balance through config Parameter initialMoney setting of yaml

In config In addition to setting parameters, yaml also sets test tasks
The following is part of the code. What needs to be modified is the module path of workload. tps can be set by itself

  rounds:
    - label: open
      description: >-
        Test description for the opening of an account through the deployed
        contract.
      txNumber: *number-of-accounts
      rateControl:
        type: fixed-rate
        opts:
          tps: 50
      workload:
        module: benchmarks/scenario/candy/open.js
        arguments: *simple-args

So you can deploy and test your contract

However, in addition, according to the contract functions, parameter transfer of some test tasks, account generation, etc., we still need to continue to have an in-depth understanding of the scenario in benchmarks. The next article will record how to write your own simple state JS and operation base js

Reference

Topics: Blockchain Ethereum tools Digital Currency hyperledger