From 2779ff86f828af847dfecd754f1cf0f2f827a4b1 Mon Sep 17 00:00:00 2001 From: Matias Romeo Date: Mon, 27 Mar 2023 19:01:46 -0300 Subject: [PATCH 1/5] Add utilities to measure contract execution time --- contract/src/actions.cpp | 1 + tests/leap/nodeos_trust_evm_server.py | 9 ++ .../contracts/Recursive.sol | 18 ++++ .../contracts/Token.sol | 3 +- .../extract-logtime-cmd.sh | 3 + .../nodeos_trust_evm_server/hardhat.config.js | 92 +++++++++++++++---- .../scripts/deploy-recursive.js | 25 +++++ .../scripts/deploy-token.js | 2 +- .../scripts/deploy-uniswap.js | 47 +++++++++- 9 files changed, 174 insertions(+), 26 deletions(-) create mode 100644 tests/leap/nodeos_trust_evm_server/contracts/Recursive.sol create mode 100755 tests/leap/nodeos_trust_evm_server/extract-logtime-cmd.sh create mode 100644 tests/leap/nodeos_trust_evm_server/scripts/deploy-recursive.js diff --git a/contract/src/actions.cpp b/contract/src/actions.cpp index 618a175a..3a9b8532 100644 --- a/contract/src/actions.cpp +++ b/contract/src/actions.cpp @@ -328,6 +328,7 @@ void evm_contract::pushtx( eosio::name miner, const bytes& rlptx ) { engine.finalize(ep.state(), ep.evm().block(), ep.evm().revision()); ep.state().write_to_db(ep.evm().block().header.number); + LOGTIME("EVM END"); } void evm_contract::open(eosio::name owner) { diff --git a/tests/leap/nodeos_trust_evm_server.py b/tests/leap/nodeos_trust_evm_server.py index b2083cf3..4c906b7b 100755 --- a/tests/leap/nodeos_trust_evm_server.py +++ b/tests/leap/nodeos_trust_evm_server.py @@ -410,6 +410,15 @@ def forward_request(req): "jsonrpc": "2.0", "result": '0x'+keccak(unhexlify(req['params'][0][2:])).hex() } + + if req['method'] == "eth_gasPrice": + gas_price=int(prodNode1.getTable(evmAcc.name, evmAcc.name, "config")['rows'][0]['gas_price']) + return { + "id": req['id'], + "jsonrpc": "2.0", + "result": f'{gas_price:#0x}' + } + return requests.post(readEndpoint, json.dumps(req), headers={"Content-Type":"application/json"}).json() request_data = request.get_json() diff --git a/tests/leap/nodeos_trust_evm_server/contracts/Recursive.sol b/tests/leap/nodeos_trust_evm_server/contracts/Recursive.sol new file mode 100644 index 00000000..97a1ba22 --- /dev/null +++ b/tests/leap/nodeos_trust_evm_server/contracts/Recursive.sol @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity >=0.7.0 <0.9.0; + +contract Recursive { + + event Call(uint256 _value); + + function start(uint256 _depth) public { + Recursive(this).doit(_depth); + } + + function doit(uint256 _i) public { + emit Call(_i); + if( _i == 0 ) + return; + Recursive(this).doit(_i-1); + } +} \ No newline at end of file diff --git a/tests/leap/nodeos_trust_evm_server/contracts/Token.sol b/tests/leap/nodeos_trust_evm_server/contracts/Token.sol index e36f7ea4..1d8b6271 100644 --- a/tests/leap/nodeos_trust_evm_server/contracts/Token.sol +++ b/tests/leap/nodeos_trust_evm_server/contracts/Token.sol @@ -5,8 +5,7 @@ pragma solidity >=0.7.0 <0.9.0; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract Token is ERC20 { - - constructor () ERC20("Yuniper", "YUN") { + constructor (string memory _name, string memory _symbol) ERC20(_name, _symbol) { _mint(msg.sender, 1000000 * (10 ** uint256(decimals()))); } } diff --git a/tests/leap/nodeos_trust_evm_server/extract-logtime-cmd.sh b/tests/leap/nodeos_trust_evm_server/extract-logtime-cmd.sh new file mode 100755 index 00000000..879040ba --- /dev/null +++ b/tests/leap/nodeos_trust_evm_server/extract-logtime-cmd.sh @@ -0,0 +1,3 @@ +#!/bin/bash +echo INIT,DECODE,RECOVER,EXECUTE,SAVE,CLEANUP,TOTAL +tail -f $1 | grep --line-buffered -Po "tx: \K\{(.*)\}" | jq -r '[.timelogs[0][1], .timelogs[1][1]-.timelogs[0][1], .timelogs[2][1]-.timelogs[1][1], .timelogs[3][1]-.timelogs[2][1], .timelogs[4][1]-.timelogs[3][1], .elapsed-.timelogs[4][1], .elapsed] | join(",")' diff --git a/tests/leap/nodeos_trust_evm_server/hardhat.config.js b/tests/leap/nodeos_trust_evm_server/hardhat.config.js index 6ba8c1c6..bb97f92d 100644 --- a/tests/leap/nodeos_trust_evm_server/hardhat.config.js +++ b/tests/leap/nodeos_trust_evm_server/hardhat.config.js @@ -40,8 +40,8 @@ task("transfer", "Send ERC20 tokens") .setAction(async (taskArgs) => { const Token = await ethers.getContractFactory('Token') const token = Token.attach(taskArgs.contract) - const res = await token.connect(await ethers.getSigner(taskArgs.from)).transfer(taskArgs.to, ethers.utils.parseEther(taskArgs.amount.toString()),{gasLimit:50000}); - console.log(res); + const res = await token.connect(await ethers.getSigner(taskArgs.from)).transfer(taskArgs.to, ethers.utils.parseEther(taskArgs.amount.toString())); + console.log(res.hash); }); task("send-loop", "Send ERC20 token in a loop") @@ -172,8 +172,8 @@ task("swap4eth", "Swap exact tokens for ETH") .addParam("weth9", "The weth9 contract address") .addParam("router", "The router contract address") .setAction(async (taskArgs) => { + const signer = await ethers.getSigner(0); - //console.log(signer); const Token = await ethers.getContractFactory('Token') const token = Token.attach(taskArgs.erc20) @@ -181,31 +181,72 @@ task("swap4eth", "Swap exact tokens for ETH") const ROUTER = require("@uniswap/v2-periphery/build/UniswapV2Router02.json"); const WETH9 = require("@uniswap/v2-periphery/build/WETH9.json"); - //await ethers.getSigner const Router = new ethers.ContractFactory(ROUTER.abi, ROUTER.bytecode); const router = Router.attach(taskArgs.router).connect(signer); const Weth9 = new ethers.ContractFactory(WETH9.abi, WETH9.bytecode); const weth9 = Weth9.attach(taskArgs.weth9).connect(signer); - const AMOUNT_WETH9 = eth(1000); - const AMOUNT_TOKEN = eth(1000); - - await weth9.approve(router.address, AMOUNT_WETH9); - await token.approve(router.address, AMOUNT_TOKEN); + //TODO: check allowance + await token.approve(router.address, eth(1)); - const receipt = await router.addLiquidityETH( - token.address, - eth(1000), - eth(1000), - eth(100), - signer.address, - ethers.constants.MaxUint256, - { value: eth(1000) } + const receipt = await router.swapExactTokensForETH( + eth(1), //amountIn + 0, //amountOutMin + [token.address, weth9.address], //path + signer.address, //to + Date.now() + 1000*60*10, ); - console.log(receipt); + console.log(receipt.hash); +}); + +task("approve", "") +.addParam("erc20", "erc20 contract") +.addParam("router", "The router contract address") +.addParam("amount", "amount to approve") +.setAction(async (taskArgs) => { + const signer = await ethers.getSigner(0); + const Token = await ethers.getContractFactory('Token') + const token = Token.attach(taskArgs.erc20); + + const ROUTER = require("@uniswap/v2-periphery/build/UniswapV2Router02.json"); + const Router = new ethers.ContractFactory(ROUTER.abi, ROUTER.bytecode); + const router = Router.attach(taskArgs.router).connect(signer); + + const receipt = await token.approve(router.address, eth(taskArgs.amount)); + console.log(receipt.hash); +}); + +task("swap", "swap two erc20") +.addParam("path", "erc20 contract address path") +.addParam("router", "The router contract address") +.addParam("amount", "Amount-in") +.setAction(async (taskArgs) => { + const signer = await ethers.getSigner(0); + + const Token = await ethers.getContractFactory('Token') + + var tokens = []; + taskArgs.path.split(',').forEach((address) => { + tokens.push({token:Token.attach(address)}) + }); + + const ROUTER = require("@uniswap/v2-periphery/build/UniswapV2Router02.json"); + const WETH9 = require("@uniswap/v2-periphery/build/WETH9.json"); + + const Router = new ethers.ContractFactory(ROUTER.abi, ROUTER.bytecode); + const router = Router.attach(taskArgs.router).connect(signer); + + const receipt = await router.swapExactTokensForTokens( + eth(taskArgs.amount), //amountIn + 0, //amountOutMin + taskArgs.path.split(','), //path + signer.address, //to + Date.now() + 1000*60*10, + ); + console.log(receipt.hash); }); @@ -246,10 +287,23 @@ task("add-liquidity", "Adds liquidity to an ERC-20⇄WETH pool with ETH") { value: eth(1000) } ); - console.log(receipt); + console.log(receipt.hash); }); +task("call-recursive", "Call recursive contract") + .addParam("contract", "The contract address") + .addParam("depth", "Call depth") + .setAction(async (taskArgs) => { + const signer = await ethers.getSigner(0); + //console.log(signer); + + const Recursive = await ethers.getContractFactory('Recursive') + const recursive = Recursive.attach(taskArgs.contract) + + const receipt = await recursive.start(taskArgs.depth); + console.log(receipt.hash); +}); diff --git a/tests/leap/nodeos_trust_evm_server/scripts/deploy-recursive.js b/tests/leap/nodeos_trust_evm_server/scripts/deploy-recursive.js new file mode 100644 index 00000000..9b4fe7c0 --- /dev/null +++ b/tests/leap/nodeos_trust_evm_server/scripts/deploy-recursive.js @@ -0,0 +1,25 @@ +// We require the Hardhat Runtime Environment explicitly here. This is optional +// but useful for running the script in a standalone fashion through `node