Summary of Hyperledger Caliper Besu pitfalls (2) Testing the contract you wrote

Abstract

In the first article of Trapping the Pit, I wrote an example given on the official website. This article records how to test your own Contract
Caliper version v0.4.2.

Repository

  • This article continues to use the Github Repo on the official website to create a new contract. To simplify it, I use Sample to change it. The contract name and function name are replaced. Storage is the same as Logic.

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

Table of contents

  1. Chapter1: Install Caliper
  2. Chapter2: Write contract & generate abi.json file
  3. Chapter3: Network configuration and test task configuration
  4. Reference

Chapter1

git clone [email protected]:hyperledger/caliper-benchmarks.git

You need to init it before entering the project, otherwise the following steps will go wrong.

cd caliper-benchmarks
npm init

Install caliper
and bind it to the latest version of besu. Note here that you can choose the binding version, but a series of subsequent settings must be consistent with this version.

npm install --only=prod @hyperledger/[email protected]
npx caliper bind --caliper-bind-sut besu:latest

Chapter2

Create a new contract under the new path /src/candy/candy.sol (the path can be arbitrary, just specify the place when calling) and
add the new contract content

  • Here you need to pay attention to the Solidity version of the contract, which must be consistent with the subsequent 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;
    }
}

What Caliper needs is the abi.json file. Here, solcjstools are used to generate internal data. Friends who use it frequently can -ginstall it globally.

npm install solc  

The file will be generated in the current path

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

If the following exception occurs, 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 has requirements for the content of the abi.json file. The following four keywords are required in the Contract definition file.

  • Name: Whatever, I’ll take Candy here
  • ABI: Content generated by the -abi directive above
  • Byetcode: the content generated by the above -bin command
  • Gas: must be filled in. You can try Deploy on Remix to see how much it costs, or use the Simulation tool to estimate the Besu network. This has little impact, just don’t make it too small.

Then you can create a new json file candy.json, name it, and splice the contents together
. Note that bytecode is a string of numbers generated by -bin, and must be preceded by 0xa prefix .

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

If the following exception occurs, it means that the gas deployed by the contract is not enough. Change the gas in the above json file to be 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 set in open.js, query.js, respectively . Replace the original Sample with your actual function name.transfer.js

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

Each test function is inherited from utils/operation-base.js. In utils/simple-state.js, the Sample code will generate an account and corresponding initial balance based on random numbers.
The initial balance is set by the parameter initialMoney in config.yaml

In config.yaml, in addition to parameter settings, test tasks are also set. The
following is part of the code. What needs to be modified is the module path of the workload. TPS can be set by yourself.

  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

In this way, you can deploy and test your own contract

But in addition, according to the functions of the contract, the parameter transfer of some test tasks, the generation of accounts, etc., you still need to continue to have a deeper understanding of the scenarios in benchmarks. The next article will record how to write your own simple-state.js and operation-base. .js

Reference

Guess you like

Origin blog.csdn.net/weixin_44565484/article/details/123062975
Recommended