Trie stress test (#981)

* Add TrieStreeTest

This smart contract will append N new elements to a vector, this is a stress
test to our implementation of the trie structure

* Expose internal database?

* Rewrite bench

Make bench more generic and simpler to bootstrap

* Reuse functions

* Deploy contract once and reuse it

* Write full blocks

* Allow callee to deploy N contracts

Remove gas calculation from benchmarks, gas is still calculated to make sure
all contracts are deployed.

stressTestTrieDb takes 3 arguments. The number of contracts to deploy, the
calls for each block and the numbers of elements to append to the vector on
each call

* Remove all gas control

* Added AddTxOrFail()

This function will treat execution errors as errors as well, useful for
testing.

ApplyTransaction has been modified to return the ExecutionResult

* Move changes to the test

Breaking API changes should be avoided

* Improve error handling in benchmark

* Call many contracts per block

* Removed duplicate code

Use existing code instead, reading the receipt from the last execution instead
of returning the exceutionresult struct

* Use NewTx instead of NewTransaction

* Better organization for files

* rename & move files

* Update core/trie_stress_bench_test.go

Co-authored-by: aaronbuchwald <[email protected]>
Signed-off-by: Cesar <[email protected]>

---------

Signed-off-by: Cesar <[email protected]>
Co-authored-by: Ceyhun Onur <[email protected]>
Co-authored-by: aaronbuchwald <[email protected]>
This commit is contained in:
Cesar
2023-11-29 11:37:30 -05:00
committed by GitHub
co-authored by Ceyhun Onur aaronbuchwald
parent 67cf00f67b
commit 7e0b73c670
4 changed files with 139 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"uint256","name":"index","type":"uint256"}],"name":"getData","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"value","type":"uint256"}],"name":"writeValues","outputs":[],"stateMutability":"nonpayable","type":"function"}]
+1
View File
@@ -0,0 +1 @@
608060405234801561000f575f80fd5b506103938061001d5f395ff3fe608060405234801561000f575f80fd5b506004361061003f575f3560e01c80630178fe3f14610043578063514a19d614610073578063be1c766b1461008f575b5f80fd5b61005d600480360381019061005891906101c0565b6100ad565b60405161006a9190610203565b60405180910390f35b61008d600480360381019061008891906101c0565b610117565b005b61009761017e565b6040516100a4919061022b565b60405180910390f35b5f808054905082106100f4576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016100eb9061029e565b60405180910390fd5b5f8281548110610107576101066102bc565b5b905f5260205f2001549050919050565b5f60603373ffffffffffffffffffffffffffffffffffffffff16901b5f1b90505f5b82811015610179575f82908060018154018082558091505060019003905f5260205f20015f9091909190915055808061017190610316565b915050610139565b505050565b5f8080549050905090565b5f80fd5b5f819050919050565b61019f8161018d565b81146101a9575f80fd5b50565b5f813590506101ba81610196565b92915050565b5f602082840312156101d5576101d4610189565b5b5f6101e2848285016101ac565b91505092915050565b5f819050919050565b6101fd816101eb565b82525050565b5f6020820190506102165f8301846101f4565b92915050565b6102258161018d565b82525050565b5f60208201905061023e5f83018461021c565b92915050565b5f82825260208201905092915050565b7f496e646578206f7574206f6620626f756e6400000000000000000000000000005f82015250565b5f610288601283610244565b915061029382610254565b602082019050919050565b5f6020820190508181035f8301526102b58161027c565b9050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52603260045260245ffd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f6103208261018d565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610352576103516102e9565b5b60018201905091905056fea26469706673582212206a77a7e1e9aaf41a68d7c07cec79019e175a15c152db425e1c143d7cfd7da65e64736f6c63430008150033
+22
View File
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract TrieStressTest {
bytes32[] private data;
function writeValues(uint value) public {
bytes32 dataToPush = bytes32(uint256(uint160(msg.sender)) << 96);
for (uint i = 0; i < value; i++) {
data.push(dataToPush);
}
}
function getLength() public view returns (uint) {
return data.length;
}
function getData(uint index) public view returns (bytes32) {
require(index < data.length, "Index out of bound");
return data[index];
}
}
+115
View File
@@ -0,0 +1,115 @@
// (c) 2020-2021, Ava Labs, Inc.
//
// This file is a derived work, based on the go-ethereum library whose original
// notices appear below.
//
// It is distributed under a license compatible with the licensing terms of the
// original code from which it is derived.
//
// Much love to the original authors for their work.
// **********
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package core
import (
_ "embed"
"math/big"
"testing"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/precompile/contract"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
)
var (
//go:embed TrieStressTest.bin
stressBinStr string
//go:embed TrieStressTest.abi
stressABIStr string
)
func BenchmarkTrie(t *testing.B) {
benchInsertChain(t, true, stressTestTrieDb(t, 100, 6, 50, 1202102))
}
func stressTestTrieDb(t *testing.B, numContracts int, callsPerBlock int, elements int64, gasTxLimit uint64) func(int, *BlockGen) {
require := require.New(t)
contractAddr := make([]common.Address, numContracts)
contractTxs := make([]*types.Transaction, numContracts)
gasPrice := big.NewInt(225000000000)
gasCreation := uint64(258000)
deployedContracts := 0
for i := 0; i < numContracts; i++ {
nonce := uint64(i)
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
Nonce: nonce,
Value: big.NewInt(0),
Gas: gasCreation,
GasPrice: gasPrice,
Data: common.FromHex(stressBinStr),
}), signer, testKey)
sender, _ := types.Sender(signer, tx)
contractTxs[i] = tx
contractAddr[i] = crypto.CreateAddress(sender, nonce)
}
stressABI := contract.ParseABI(stressABIStr)
txPayload, _ := stressABI.Pack(
"writeValues",
big.NewInt(elements),
)
return func(i int, gen *BlockGen) {
if len(contractTxs) != deployedContracts {
// This is the first stage of the bench, the preparation stage, at
// this state the requested amount of smart contract will be
// deployed to be used in the second stage
//
// This block will be executed until all contracts are deployed, the
// code will make sure that enough instances are deployed per block
block := gen.PrevBlock(i - 1)
gas := block.GasLimit()
for ; deployedContracts < len(contractTxs) && gasCreation < gas; deployedContracts++ {
gen.AddTx(contractTxs[deployedContracts])
require.Equal(gen.receipts[len(gen.receipts)-1].Status, types.ReceiptStatusSuccessful, "Execution of last transaction failed")
gas -= gasCreation
}
return
}
// Benchmark itself, this is the second stage
for e := 0; e < callsPerBlock; e++ {
contractId := (i + e) % deployedContracts
tx, err := types.SignTx(types.NewTx(&types.LegacyTx{
Nonce: gen.TxNonce(benchRootAddr),
To: &contractAddr[contractId],
Value: big.NewInt(0),
Gas: gasTxLimit,
GasPrice: gasPrice,
Data: txPayload,
}), signer, testKey)
require.NoError(err)
gen.AddTx(tx)
require.Equal(gen.receipts[len(gen.receipts)-1].Status, types.ReceiptStatusSuccessful, "Execution of last transaction failed")
}
}
}