Hyperledger Caliper Besu踩坑总结(2)测试自己写的合约

Abstract

踩坑第一篇写了官网给出的例子,这篇记录怎么测试自己的Contract
Caliper版本为v0.4.2

Repository

  • 本文还是继续用官网的Github Repo,合约新建,为了简化还是用Sample改了一下,合同名和函数名都换掉,Storage和Logic一样

  • Github上开源
    https://github.com/hyperledger/caliper-benchmarks

Table of contents

  1. Chapter1:安装Caliper
  2. Chapter2:撰写合约&生成abi.json文件
  3. Chapter3:网络配置和测试任务配置
  4. Reference

Chapter1

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

这里进到项目要init一下,不然后面步骤都会出错

cd caliper-benchmarks
npm init

安装caliper
绑定到最新版本的besu,这里注意可以选择绑定版本,但是后续的一系列设置都要向这个版本看齐

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

Chapter2

新路径下新建合约/src/candy/candy.sol(路径其实可以随意,调用时候指定好地方就OK)
添加新合约内容

  • 这里需要注意合约的Solidity版本,要与之后编译器版本一致
// 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需要的是abi.json文件,这里用到solcjs工具生成内部数据,经常用的小伙伴可以加-g装到全局

npm install solc  

会在当前路径生成文件

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

如果出现以下异常,需要更改合约Solidity版本

扫描二维码关注公众号,回复: 16913224 查看本文章
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对abi.json文件内容有要求,Contract definition file中要求有以下四个关键字

  • Name :随意,我这里取Candy
  • ABI :上面-abi指令生成的内容
  • Byetcode :上面-bin指令生成的内容
  • Gas:一定要填写,可以在Remix上Deploy试一下看看花多少,或者用Simulation工具估算下,Besu网络,这个影响不大,不要太小就可以

然后就可以新建一个json文件取名candy.json,把内容拼接起来
注意bytecode是-bin生成的一串数字,前面要加0x前缀

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

如果出现以下异常,是合约部署的gas不够,把上面的json文件里的gas改大一点

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

每个合约中的待测试函数分别在open.js, query.js, transfer.js中设置
将实际的自己的函数名换掉原来给的Sample

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

各个测试函数都是utils/operation-base.js继承,在utils/simple-state.js里Sample代码会根据乱数生成账户和相应的初始余额。
初始余额通过在config.yaml的参数initialMoney设置

在config.yaml里面除了参数的设置还会进行测试任务的设置
下面是一部分代码,需要修改的是workload的module路径,tps可以自己设置

  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

这样就可以部署测试自己的合约了

但是除此之外,根据合约的函数,有些测试任务的参数传递,账户的生成等,还是需要继续深入了解benchmarks里的scenario, 下篇将记录怎么写自己的simple-state.js和operation-base.js

Reference

猜你喜欢

转载自blog.csdn.net/weixin_44565484/article/details/123062975