@@ -24,6 +24,7 @@ import (
24
24
"math/big"
25
25
"math/rand"
26
26
"os"
27
+ "os/exec"
27
28
"sync"
28
29
"testing"
29
30
"time"
@@ -41,6 +42,7 @@ import (
41
42
"github.com/ethereum/go-ethereum/consensus/ethash"
42
43
"github.com/ethereum/go-ethereum/core/rawdb"
43
44
"github.com/ethereum/go-ethereum/core/state"
45
+ "github.com/ethereum/go-ethereum/core/state/snapshot"
44
46
"github.com/ethereum/go-ethereum/core/types"
45
47
"github.com/ethereum/go-ethereum/core/vm"
46
48
"github.com/ethereum/go-ethereum/crypto"
@@ -4399,3 +4401,84 @@ func TestBlockChain_2000StorageUpdate(t *testing.T) {
4399
4401
t .Fatalf ("Block hash mismatches, exp %s got %s" , common.Hash {}, blockHash )
4400
4402
}
4401
4403
}
4404
+
4405
+ // This benchmark is intended to be used with mainnet data, so mainnet chaindata's directory
4406
+ // is needed to run this benchmark
4407
+ func BenchmarkManyStorageUpdate (b * testing.B ) {
4408
+ const (
4409
+ // Fill the chaindata's parent directory
4410
+ datadir = ""
4411
+ numInsert = state .ParallelInsertThreshold + 1
4412
+ )
4413
+
4414
+ var (
4415
+ diskdb ethdb.Database
4416
+ err error
4417
+ axieContract = common .HexToAddress ("0x32950db2a7164ae833121501c797d79e7b79d74c" )
4418
+ value = common .HexToHash ("0x11" )
4419
+ )
4420
+ defer func () {
4421
+ if diskdb != nil {
4422
+ diskdb .Close ()
4423
+ cmd := exec .Command ("../script/overlayfs_chaindata.sh" , "-d" , datadir , "-c" )
4424
+ if err := cmd .Run (); err != nil {
4425
+ b .Fatal (err )
4426
+ }
4427
+ }
4428
+ }()
4429
+
4430
+ keys := make ([]common.Hash , 0 , numInsert )
4431
+ for i := 0 ; i < numInsert ; i ++ {
4432
+ hash := crypto .Keccak256Hash (big .NewInt (int64 (i )).Bytes ())
4433
+ keys = append (keys , hash )
4434
+ }
4435
+
4436
+ b .StopTimer ()
4437
+ b .ResetTimer ()
4438
+ for i := 0 ; i < b .N ; i ++ {
4439
+ cmd := exec .Command ("../script/overlayfs_chaindata.sh" , "-d" , datadir )
4440
+ if err := cmd .Run (); err != nil {
4441
+ b .Fatal (err )
4442
+ }
4443
+
4444
+ diskdb , err = rawdb .NewPebbleDBDatabase (datadir + "/chaindata" , 1024 , 500000 , "" , false , false )
4445
+ if err != nil {
4446
+ b .Fatal (err )
4447
+ }
4448
+
4449
+ engine := ethash .NewFaker ()
4450
+ chain , err := NewBlockChain (diskdb , nil , params .TestChainConfig , engine , vm.Config {}, nil , nil )
4451
+ if err != nil {
4452
+ b .Fatalf ("failed to create tester chain: %v" , err )
4453
+ }
4454
+ headBlock := chain .CurrentBlock ()
4455
+
4456
+ database := state .NewDatabase (diskdb )
4457
+ snapshot , err := snapshot .New (diskdb , database .TrieDB (), 256 , headBlock .Root (), true , true , false )
4458
+ if err != nil {
4459
+ b .Fatal (err )
4460
+ }
4461
+
4462
+ statedb , err := state .New (headBlock .Root (), database , snapshot )
4463
+ if err != nil {
4464
+ b .Fatal (err )
4465
+ }
4466
+
4467
+ b .StartTimer ()
4468
+ for i := 0 ; i < numInsert ; i ++ {
4469
+ statedb .SetState (axieContract , keys [i ], value )
4470
+ }
4471
+ _ , err = statedb .Commit (true )
4472
+ if err != nil {
4473
+ b .Fatal (err )
4474
+ }
4475
+ b .StopTimer ()
4476
+
4477
+ diskdb .Close ()
4478
+ cmd = exec .Command ("../script/overlayfs_chaindata.sh" , "-d" , datadir , "-c" )
4479
+ if err := cmd .Run (); err != nil {
4480
+ b .Fatal (err )
4481
+ }
4482
+ diskdb = nil
4483
+ }
4484
+ }
0 commit comments