2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2015-06-25 00:04:50 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-26 21:21:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2017-04-04 22:16:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2018-05-07 11:35:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2015-06-25 00:04:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-01-17 11:19:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2015-06-25 00:04:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkInsertChain_empty_memdb(b *testing.B) {
|
|
|
|
benchInsertChain(b, false, nil)
|
|
|
|
}
|
|
|
|
func BenchmarkInsertChain_empty_diskdb(b *testing.B) {
|
|
|
|
benchInsertChain(b, true, nil)
|
|
|
|
}
|
|
|
|
func BenchmarkInsertChain_valueTx_memdb(b *testing.B) {
|
|
|
|
benchInsertChain(b, false, genValueTx(0))
|
|
|
|
}
|
|
|
|
func BenchmarkInsertChain_valueTx_diskdb(b *testing.B) {
|
|
|
|
benchInsertChain(b, true, genValueTx(0))
|
|
|
|
}
|
|
|
|
func BenchmarkInsertChain_valueTx_100kB_memdb(b *testing.B) {
|
|
|
|
benchInsertChain(b, false, genValueTx(100*1024))
|
|
|
|
}
|
|
|
|
func BenchmarkInsertChain_valueTx_100kB_diskdb(b *testing.B) {
|
|
|
|
benchInsertChain(b, true, genValueTx(100*1024))
|
|
|
|
}
|
|
|
|
func BenchmarkInsertChain_uncles_memdb(b *testing.B) {
|
|
|
|
benchInsertChain(b, false, genUncles)
|
|
|
|
}
|
|
|
|
func BenchmarkInsertChain_uncles_diskdb(b *testing.B) {
|
|
|
|
benchInsertChain(b, true, genUncles)
|
|
|
|
}
|
|
|
|
func BenchmarkInsertChain_ring200_memdb(b *testing.B) {
|
|
|
|
benchInsertChain(b, false, genTxRing(200))
|
|
|
|
}
|
|
|
|
func BenchmarkInsertChain_ring200_diskdb(b *testing.B) {
|
|
|
|
benchInsertChain(b, true, genTxRing(200))
|
|
|
|
}
|
|
|
|
func BenchmarkInsertChain_ring1000_memdb(b *testing.B) {
|
|
|
|
benchInsertChain(b, false, genTxRing(1000))
|
|
|
|
}
|
|
|
|
func BenchmarkInsertChain_ring1000_diskdb(b *testing.B) {
|
|
|
|
benchInsertChain(b, true, genTxRing(1000))
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// This is the content of the genesis block used by the benchmarks.
|
|
|
|
benchRootKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
|
|
|
benchRootAddr = crypto.PubkeyToAddress(benchRootKey.PublicKey)
|
2021-10-27 11:08:51 +00:00
|
|
|
benchRootFunds = math.BigPow(2, 200)
|
2015-06-25 00:04:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// genValueTx returns a block generator that includes a single
|
|
|
|
// value-transfer transaction with n bytes of extra data in each
|
|
|
|
// block.
|
|
|
|
func genValueTx(nbytes int) func(int, *BlockGen) {
|
|
|
|
return func(i int, gen *BlockGen) {
|
|
|
|
toaddr := common.Address{}
|
|
|
|
data := make([]byte, nbytes)
|
2023-01-11 09:05:47 +00:00
|
|
|
gas, _ := IntrinsicGas(data, nil, false, false, false, false)
|
2023-10-31 11:39:25 +00:00
|
|
|
signer := gen.Signer()
|
2021-10-27 11:08:51 +00:00
|
|
|
gasPrice := big.NewInt(0)
|
|
|
|
if gen.header.BaseFee != nil {
|
|
|
|
gasPrice = gen.header.BaseFee
|
|
|
|
}
|
|
|
|
tx, _ := types.SignNewTx(benchRootKey, signer, &types.LegacyTx{
|
|
|
|
Nonce: gen.TxNonce(benchRootAddr),
|
|
|
|
To: &toaddr,
|
|
|
|
Value: big.NewInt(1),
|
|
|
|
Gas: gas,
|
|
|
|
Data: data,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
})
|
2015-06-25 00:04:50 +00:00
|
|
|
gen.AddTx(tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 12:47:56 +00:00
|
|
|
var (
|
|
|
|
ringKeys = make([]*ecdsa.PrivateKey, 1000)
|
|
|
|
ringAddrs = make([]common.Address, len(ringKeys))
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
ringKeys[0] = benchRootKey
|
|
|
|
ringAddrs[0] = benchRootAddr
|
|
|
|
for i := 1; i < len(ringKeys); i++ {
|
|
|
|
ringKeys[i], _ = crypto.GenerateKey()
|
|
|
|
ringAddrs[i] = crypto.PubkeyToAddress(ringKeys[i].PublicKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 00:04:50 +00:00
|
|
|
// genTxRing returns a block generator that sends ether in a ring
|
|
|
|
// among n accounts. This is creates n entries in the state database
|
|
|
|
// and fills the blocks with many small transactions.
|
|
|
|
func genTxRing(naccounts int) func(int, *BlockGen) {
|
|
|
|
from := 0
|
2021-10-27 11:08:51 +00:00
|
|
|
availableFunds := new(big.Int).Set(benchRootFunds)
|
2015-06-25 00:04:50 +00:00
|
|
|
return func(i int, gen *BlockGen) {
|
2018-08-29 09:21:12 +00:00
|
|
|
block := gen.PrevBlock(i - 1)
|
2021-05-21 07:59:26 +00:00
|
|
|
gas := block.GasLimit()
|
2021-10-27 11:08:51 +00:00
|
|
|
gasPrice := big.NewInt(0)
|
|
|
|
if gen.header.BaseFee != nil {
|
|
|
|
gasPrice = gen.header.BaseFee
|
|
|
|
}
|
2023-10-31 11:39:25 +00:00
|
|
|
signer := gen.Signer()
|
2015-06-25 00:04:50 +00:00
|
|
|
for {
|
2017-11-13 11:47:27 +00:00
|
|
|
gas -= params.TxGas
|
|
|
|
if gas < params.TxGas {
|
2015-06-25 00:04:50 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
to := (from + 1) % naccounts
|
2021-10-27 11:08:51 +00:00
|
|
|
burn := new(big.Int).SetUint64(params.TxGas)
|
|
|
|
burn.Mul(burn, gen.header.BaseFee)
|
|
|
|
availableFunds.Sub(availableFunds, burn)
|
|
|
|
if availableFunds.Cmp(big.NewInt(1)) < 0 {
|
|
|
|
panic("not enough funds")
|
|
|
|
}
|
|
|
|
tx, err := types.SignNewTx(ringKeys[from], signer,
|
|
|
|
&types.LegacyTx{
|
|
|
|
Nonce: gen.TxNonce(ringAddrs[from]),
|
|
|
|
To: &ringAddrs[to],
|
|
|
|
Value: availableFunds,
|
|
|
|
Gas: params.TxGas,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-06-25 00:04:50 +00:00
|
|
|
gen.AddTx(tx)
|
|
|
|
from = to
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// genUncles generates blocks with two uncle headers.
|
|
|
|
func genUncles(i int, gen *BlockGen) {
|
2022-04-12 08:34:07 +00:00
|
|
|
if i >= 7 {
|
2015-06-25 00:04:50 +00:00
|
|
|
b2 := gen.PrevBlock(i - 6).Header()
|
|
|
|
b2.Extra = []byte("foo")
|
|
|
|
gen.AddUncle(b2)
|
|
|
|
b3 := gen.PrevBlock(i - 6).Header()
|
|
|
|
b3.Extra = []byte("bar")
|
|
|
|
gen.AddUncle(b3)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
|
|
|
|
// Create the database in memory or in a temporary directory.
|
2015-09-14 07:35:57 +00:00
|
|
|
var db ethdb.Database
|
2022-04-08 13:44:55 +00:00
|
|
|
var err error
|
2015-06-25 00:04:50 +00:00
|
|
|
if !disk {
|
2018-09-24 12:57:49 +00:00
|
|
|
db = rawdb.NewMemoryDatabase()
|
2015-06-25 00:04:50 +00:00
|
|
|
} else {
|
2022-04-08 13:44:55 +00:00
|
|
|
dir := b.TempDir()
|
2021-03-22 18:06:30 +00:00
|
|
|
db, err = rawdb.NewLevelDBDatabase(dir, 128, 128, "", false)
|
2015-06-25 00:04:50 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("cannot create temporary database: %v", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a chain of b.N blocks using the supplied block
|
|
|
|
// generator function.
|
2022-09-07 18:21:59 +00:00
|
|
|
gspec := &Genesis{
|
2017-03-02 13:03:33 +00:00
|
|
|
Config: params.TestChainConfig,
|
2024-02-16 18:05:33 +00:00
|
|
|
Alloc: types.GenesisAlloc{benchRootAddr: {Balance: benchRootFunds}},
|
2017-03-02 13:03:33 +00:00
|
|
|
}
|
2022-09-07 18:21:59 +00:00
|
|
|
_, chain, _ := GenerateChainWithGenesis(gspec, ethash.NewFaker(), b.N, gen)
|
2015-06-25 00:04:50 +00:00
|
|
|
|
|
|
|
// Time the insertion of the new chain.
|
|
|
|
// State and blocks are stored in the same DB.
|
2022-09-07 18:21:59 +00:00
|
|
|
chainman, _ := NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
|
2015-06-25 00:04:50 +00:00
|
|
|
defer chainman.Stop()
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
if i, err := chainman.InsertChain(chain); err != nil {
|
|
|
|
b.Fatalf("insert error (block %d): %v\n", i, err)
|
|
|
|
}
|
|
|
|
}
|
2016-04-25 16:12:51 +00:00
|
|
|
|
|
|
|
func BenchmarkChainRead_header_10k(b *testing.B) {
|
|
|
|
benchReadChain(b, false, 10000)
|
|
|
|
}
|
|
|
|
func BenchmarkChainRead_full_10k(b *testing.B) {
|
|
|
|
benchReadChain(b, true, 10000)
|
|
|
|
}
|
|
|
|
func BenchmarkChainRead_header_100k(b *testing.B) {
|
|
|
|
benchReadChain(b, false, 100000)
|
|
|
|
}
|
|
|
|
func BenchmarkChainRead_full_100k(b *testing.B) {
|
|
|
|
benchReadChain(b, true, 100000)
|
|
|
|
}
|
|
|
|
func BenchmarkChainRead_header_500k(b *testing.B) {
|
|
|
|
benchReadChain(b, false, 500000)
|
|
|
|
}
|
|
|
|
func BenchmarkChainRead_full_500k(b *testing.B) {
|
|
|
|
benchReadChain(b, true, 500000)
|
|
|
|
}
|
|
|
|
func BenchmarkChainWrite_header_10k(b *testing.B) {
|
|
|
|
benchWriteChain(b, false, 10000)
|
|
|
|
}
|
|
|
|
func BenchmarkChainWrite_full_10k(b *testing.B) {
|
|
|
|
benchWriteChain(b, true, 10000)
|
|
|
|
}
|
|
|
|
func BenchmarkChainWrite_header_100k(b *testing.B) {
|
|
|
|
benchWriteChain(b, false, 100000)
|
|
|
|
}
|
|
|
|
func BenchmarkChainWrite_full_100k(b *testing.B) {
|
|
|
|
benchWriteChain(b, true, 100000)
|
|
|
|
}
|
|
|
|
func BenchmarkChainWrite_header_500k(b *testing.B) {
|
|
|
|
benchWriteChain(b, false, 500000)
|
|
|
|
}
|
|
|
|
func BenchmarkChainWrite_full_500k(b *testing.B) {
|
|
|
|
benchWriteChain(b, true, 500000)
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeChainForBench writes a given number of headers or empty blocks/receipts
|
|
|
|
// into a database.
|
2024-01-23 13:46:34 +00:00
|
|
|
func makeChainForBench(db ethdb.Database, genesis *Genesis, full bool, count uint64) {
|
2016-04-25 16:12:51 +00:00
|
|
|
var hash common.Hash
|
|
|
|
for n := uint64(0); n < count; n++ {
|
|
|
|
header := &types.Header{
|
|
|
|
Coinbase: common.Address{},
|
|
|
|
Number: big.NewInt(int64(n)),
|
|
|
|
ParentHash: hash,
|
|
|
|
Difficulty: big.NewInt(1),
|
|
|
|
UncleHash: types.EmptyUncleHash,
|
2023-02-21 11:12:27 +00:00
|
|
|
TxHash: types.EmptyTxsHash,
|
|
|
|
ReceiptHash: types.EmptyReceiptsHash,
|
2016-04-25 16:12:51 +00:00
|
|
|
}
|
2024-01-23 13:46:34 +00:00
|
|
|
if n == 0 {
|
|
|
|
header = genesis.ToBlock().Header()
|
|
|
|
}
|
2016-04-25 16:12:51 +00:00
|
|
|
hash = header.Hash()
|
2018-05-07 11:35:06 +00:00
|
|
|
|
|
|
|
rawdb.WriteHeader(db, header)
|
|
|
|
rawdb.WriteCanonicalHash(db, hash, n)
|
|
|
|
rawdb.WriteTd(db, hash, n, big.NewInt(int64(n+1)))
|
|
|
|
|
2022-08-30 16:22:28 +00:00
|
|
|
if n == 0 {
|
2024-01-23 13:46:34 +00:00
|
|
|
rawdb.WriteChainConfig(db, hash, genesis.Config)
|
2022-08-30 16:22:28 +00:00
|
|
|
}
|
|
|
|
rawdb.WriteHeadHeaderHash(db, hash)
|
|
|
|
|
2016-04-25 16:12:51 +00:00
|
|
|
if full || n == 0 {
|
|
|
|
block := types.NewBlockWithHeader(header)
|
2018-05-07 11:35:06 +00:00
|
|
|
rawdb.WriteBody(db, hash, n, block.Body())
|
|
|
|
rawdb.WriteReceipts(db, hash, n, nil)
|
2021-10-27 11:08:51 +00:00
|
|
|
rawdb.WriteHeadBlockHash(db, hash)
|
2016-04-25 16:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchWriteChain(b *testing.B, full bool, count uint64) {
|
2024-01-23 13:46:34 +00:00
|
|
|
genesis := &Genesis{Config: params.AllEthashProtocolChanges}
|
2016-04-25 16:12:51 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2022-04-08 13:44:55 +00:00
|
|
|
dir := b.TempDir()
|
2021-03-22 18:06:30 +00:00
|
|
|
db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false)
|
2016-04-25 16:12:51 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("error opening database at %v: %v", dir, err)
|
|
|
|
}
|
2024-01-23 13:46:34 +00:00
|
|
|
makeChainForBench(db, genesis, full, count)
|
2016-04-25 16:12:51 +00:00
|
|
|
db.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchReadChain(b *testing.B, full bool, count uint64) {
|
2022-04-08 13:44:55 +00:00
|
|
|
dir := b.TempDir()
|
2016-04-25 16:12:51 +00:00
|
|
|
|
2021-03-22 18:06:30 +00:00
|
|
|
db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false)
|
2016-04-25 16:12:51 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("error opening database at %v: %v", dir, err)
|
|
|
|
}
|
2024-01-23 13:46:34 +00:00
|
|
|
genesis := &Genesis{Config: params.AllEthashProtocolChanges}
|
|
|
|
makeChainForBench(db, genesis, full, count)
|
2016-04-25 16:12:51 +00:00
|
|
|
db.Close()
|
2021-10-27 11:08:51 +00:00
|
|
|
cacheConfig := *defaultCacheConfig
|
|
|
|
cacheConfig.TrieDirtyDisabled = true
|
2016-04-25 16:12:51 +00:00
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2021-03-22 18:06:30 +00:00
|
|
|
db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false)
|
2016-04-25 16:12:51 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("error opening database at %v: %v", dir, err)
|
|
|
|
}
|
2024-01-23 13:46:34 +00:00
|
|
|
chain, err := NewBlockChain(db, &cacheConfig, genesis, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
|
2016-04-25 16:12:51 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("error creating chain: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for n := uint64(0); n < count; n++ {
|
|
|
|
header := chain.GetHeaderByNumber(n)
|
|
|
|
if full {
|
|
|
|
hash := header.Hash()
|
2018-05-07 11:35:06 +00:00
|
|
|
rawdb.ReadBody(db, hash, n)
|
2023-04-21 09:52:02 +00:00
|
|
|
rawdb.ReadReceipts(db, hash, n, header.Time, chain.Config())
|
2016-04-25 16:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-07 12:47:25 +00:00
|
|
|
chain.Stop()
|
2016-04-25 16:12:51 +00:00
|
|
|
db.Close()
|
|
|
|
}
|
|
|
|
}
|