From 46040cc5444e4d0f7fb6bcda68b98882123d664d Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 16 Sep 2024 17:24:08 +0700 Subject: [PATCH] add used gas --- cmd/evm/staterunner.go | 7 +++++-- tests/state_test_util.go | 16 ++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/cmd/evm/staterunner.go b/cmd/evm/staterunner.go index d5cb1b6056..24ecf5574f 100644 --- a/cmd/evm/staterunner.go +++ b/cmd/evm/staterunner.go @@ -183,11 +183,14 @@ func runStateTest(ctx *cli.Context, fname string, cfg vm.Config, dump bool, benc if !bench { return nil + } else if len(matchingTests) != 1 { + return fmt.Errorf("can only benchmark single state test case (more than one matching params)") } + var gasUsed uint64 result := testing.Benchmark(func(b *testing.B) { for i := 0; i < b.N; i++ { for _, test := range matchingTests { - test.test.Run(test.st, cfg, false, rawdb.HashScheme, func(err error, tstate *tests.StateTestState) {}) + _, _, gasUsed, _ = test.test.RunNoVerify(test.st, cfg, false, rawdb.HashScheme) } } }) @@ -201,6 +204,6 @@ func runStateTest(ctx *cli.Context, fname string, cfg vm.Config, dump bool, benc execution time: %v allocations: %d allocated bytes: %d -`, 0, stats.time, stats.allocs, stats.bytesAllocated) +`, gasUsed, stats.time, stats.allocs, stats.bytesAllocated) return nil } diff --git a/tests/state_test_util.go b/tests/state_test_util.go index cf0ce9777f..e6d180ab3b 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -196,7 +196,7 @@ func (t *StateTest) checkError(subtest StateSubtest, err error) error { // Run executes a specific subtest and verifies the post-state and logs func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config, snapshotter bool, scheme string, postCheck func(err error, st *StateTestState)) (result error) { - st, root, err := t.RunNoVerify(subtest, vmconfig, snapshotter, scheme) + st, root, _, err := t.RunNoVerify(subtest, vmconfig, snapshotter, scheme) // Invoke the callback at the end of function for further analysis. defer func() { postCheck(result, &st) @@ -228,10 +228,10 @@ func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config, snapshotter bo // RunNoVerify runs a specific subtest and returns the statedb and post-state root. // Remember to call state.Close after verifying the test result! -func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapshotter bool, scheme string) (st StateTestState, root common.Hash, err error) { +func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapshotter bool, scheme string) (st StateTestState, root common.Hash, gasUsed uint64, err error) { config, eips, err := GetChainConfig(subtest.Fork) if err != nil { - return st, common.Hash{}, UnsupportedForkError{subtest.Fork} + return st, common.Hash{}, 0, UnsupportedForkError{subtest.Fork} } vmconfig.ExtraEips = eips @@ -250,7 +250,7 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh post := t.json.Post[subtest.Fork][subtest.Index] msg, err := t.json.Tx.toMessage(post, baseFee) if err != nil { - return st, common.Hash{}, err + return st, common.Hash{}, 0, err } { // Blob transactions may be present after the Cancun fork. @@ -260,7 +260,7 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh // Here, we just do this shortcut smaller fix, since state tests do not // utilize those codepaths if len(msg.BlobHashes)*params.BlobTxBlobGasPerBlob > params.MaxBlobGasPerBlock { - return st, common.Hash{}, errors.New("blob gas exceeds maximum") + return st, common.Hash{}, 0, errors.New("blob gas exceeds maximum") } } @@ -269,10 +269,10 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh var ttx types.Transaction err := ttx.UnmarshalBinary(post.TxBytes) if err != nil { - return st, common.Hash{}, err + return st, common.Hash{}, 0, err } if _, err := types.Sender(types.LatestSigner(config), &ttx); err != nil { - return st, common.Hash{}, err + return st, common.Hash{}, 0, err } } @@ -322,7 +322,7 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh receipt := &types.Receipt{GasUsed: vmRet.UsedGas} tracer.OnTxEnd(receipt, nil) } - return st, root, err + return st, root, vmRet.UsedGas, err } func (t *StateTest) gasLimit(subtest StateSubtest) uint64 {