From d91a2abc9e155f1e17b68ada61a1c9d358f1cd12 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Fri, 30 Aug 2024 09:00:15 +0200 Subject: [PATCH 01/12] internal/ethapi: add geth_fork method --- eth/catalyst/api.go | 8 +++--- internal/ethapi/api.go | 19 ++++++++++++++ internal/ethapi/backend.go | 3 +++ internal/web3ext/web3ext.go | 15 +++++++++++ params/config.go | 52 +++++++++++++++++++++++++++++++++++-- params/forks/forks.go | 45 ++++++++++++++++++++++++++++++++ 6 files changed, 136 insertions(+), 6 deletions(-) diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index 48bfb021b948..badc9b5d0c60 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -194,7 +194,7 @@ func (api *ConsensusAPI) ForkchoiceUpdatedV2(update engine.ForkchoiceStateV1, pa if params.BeaconRoot != nil { return engine.STATUS_INVALID, engine.InvalidPayloadAttributes.With(errors.New("unexpected beacon root")) } - switch api.eth.BlockChain().Config().LatestFork(params.Timestamp) { + switch api.eth.BlockChain().Config().LatestPostLondonFork(params.Timestamp) { case forks.Paris: if params.Withdrawals != nil { return engine.STATUS_INVALID, engine.InvalidPayloadAttributes.With(errors.New("withdrawals before shanghai")) @@ -220,7 +220,7 @@ func (api *ConsensusAPI) ForkchoiceUpdatedV3(update engine.ForkchoiceStateV1, pa if params.BeaconRoot == nil { return engine.STATUS_INVALID, engine.InvalidPayloadAttributes.With(errors.New("missing beacon root")) } - if api.eth.BlockChain().Config().LatestFork(params.Timestamp) != forks.Cancun { + if api.eth.BlockChain().Config().LatestPostLondonFork(params.Timestamp) != forks.Cancun { return engine.STATUS_INVALID, engine.UnsupportedFork.With(errors.New("forkchoiceUpdatedV3 must only be called for cancun payloads")) } } @@ -465,7 +465,7 @@ func (api *ConsensusAPI) NewPayloadV2(params engine.ExecutableData) (engine.Payl if api.eth.BlockChain().Config().IsCancun(api.eth.BlockChain().Config().LondonBlock, params.Timestamp) { return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(errors.New("can't use newPayloadV2 post-cancun")) } - if api.eth.BlockChain().Config().LatestFork(params.Timestamp) == forks.Shanghai { + if api.eth.BlockChain().Config().LatestPostLondonFork(params.Timestamp) == forks.Shanghai { if params.Withdrawals == nil { return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(errors.New("nil withdrawals post-shanghai")) } @@ -502,7 +502,7 @@ func (api *ConsensusAPI) NewPayloadV3(params engine.ExecutableData, versionedHas return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(errors.New("nil beaconRoot post-cancun")) } - if api.eth.BlockChain().Config().LatestFork(params.Timestamp) != forks.Cancun { + if api.eth.BlockChain().Config().LatestPostLondonFork(params.Timestamp) != forks.Cancun { return engine.PayloadStatusV1{Status: engine.INVALID}, engine.UnsupportedFork.With(errors.New("newPayloadV3 must only be called for cancun payloads")) } return api.newPayload(params, versionedHashes, beaconRoot) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 1c3cb4adf936..a127f4f2bed4 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -2164,6 +2164,25 @@ func (api *NetAPI) Version() string { return fmt.Sprintf("%d", api.networkVersion) } +// GethAPI offers geth-specific API methods. +type GethAPI struct { + b Backend +} + +// NewGethAPI creates a new net API instance. +func NewGethAPI(b Backend) *GethAPI { + return &GethAPI{b: b} +} + +// Fork returns the latest enabled fork as of the given block. +func (api *GethAPI) Fork(ctx context.Context, num rpc.BlockNumber) (string, error) { + b, err := api.b.BlockByNumber(ctx, num) + if err != nil { + return "", fmt.Errorf("block #%d not found", num) + } + return api.b.ChainConfig().LatestFork(b.Number(), b.Time()).String(), nil +} + // checkTxFee is an internal function used to check whether the fee of // the given transaction is _reasonable_(under the cap). func checkTxFee(gasPrice *big.Int, gas uint64, cap float64) error { diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index 2a45ba09210f..55cbe6b67c8b 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -123,6 +123,9 @@ func GetAPIs(apiBackend Backend) []rpc.API { }, { Namespace: "personal", Service: NewPersonalAccountAPI(apiBackend, nonceLock), + }, { + Namespace: "geth", + Service: NewGethAPI(apiBackend), }, } } diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 4a1a37d722d6..a9e3b45f3247 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -31,6 +31,7 @@ var Modules = map[string]string{ "les": LESJs, "vflux": VfluxJs, "dev": DevJs, + "geth": GethJs, } const CliqueJs = ` @@ -887,3 +888,17 @@ web3._extend({ ], }); ` + +const GethJs = ` +web3._extend({ + property: 'geth', + methods: + [ + new web3._extend.Method({ + name: 'fork', + call: 'geth_fork', + params: 1 + }), + ], +}); +` diff --git a/params/config.go b/params/config.go index c4e6ba6948cf..17a57a1ccd98 100644 --- a/params/config.go +++ b/params/config.go @@ -533,6 +533,13 @@ func (c *ChainConfig) IsTerminalPoWBlock(parentTotalDiff *big.Int, totalDiff *bi return parentTotalDiff.Cmp(c.TerminalTotalDifficulty) < 0 && totalDiff.Cmp(c.TerminalTotalDifficulty) >= 0 } +// IsParis returns whether the given block is either equal to the Paris (merge) +// fork or greater. +// Note: Only usable for post-merge networks where MergeNetsplitBlock has been configured. +func (c *ChainConfig) IsParis(num *big.Int) bool { + return c.IsLondon(num) && isBlockForked(c.MergeNetsplitBlock, num) +} + // IsShanghai returns whether time is either equal to the Shanghai fork time or greater. func (c *ChainConfig) IsShanghai(num *big.Int, time uint64) bool { return c.IsLondon(num) && isTimestampForked(c.ShanghaiTime, time) @@ -732,8 +739,8 @@ func (c *ChainConfig) ElasticityMultiplier() uint64 { return DefaultElasticityMultiplier } -// LatestFork returns the latest time-based fork that would be active for the given time. -func (c *ChainConfig) LatestFork(time uint64) forks.Fork { +// LatestPostLondonFork returns the latest time-based fork that would be active for the given time. +func (c *ChainConfig) LatestPostLondonFork(time uint64) forks.Fork { // Assume last non-time-based fork has passed. london := c.LondonBlock @@ -749,6 +756,47 @@ func (c *ChainConfig) LatestFork(time uint64) forks.Fork { } } +func (c *ChainConfig) LatestFork(num *big.Int, time uint64) forks.Fork { + switch { + case c.IsPrague(num, time): + return forks.Prague + case c.IsCancun(num, time): + return forks.Cancun + case c.IsShanghai(num, time): + return forks.Shanghai + case c.IsParis(num): + return forks.Paris + case c.IsGrayGlacier(num): + return forks.GrayGlacier + case c.IsArrowGlacier(num): + return forks.ArrowGlacier + case c.IsLondon(num): + return forks.London + case c.IsBerlin(num): + return forks.Berlin + case c.IsMuirGlacier(num): + return forks.MuirGlacier + case c.IsIstanbul(num): + return forks.Istanbul + case c.IsPetersburg(num): + return forks.Petersburg + case c.IsConstantinople(num): + return forks.Constantinople + case c.IsByzantium(num): + return forks.Byzantium + case c.IsEIP158(num): + return forks.SpuriousDragon + case c.IsEIP155(num): + return forks.TangerineWhistle + case c.IsEIP150(num): + return forks.DAO + case c.IsHomestead(num): + return forks.Homestead + default: + return forks.Frontier + } +} + // isForkBlockIncompatible returns true if a fork scheduled at block s1 cannot be // rescheduled to block s2 because head is already past the fork. func isForkBlockIncompatible(s1, s2, head *big.Int) bool { diff --git a/params/forks/forks.go b/params/forks/forks.go index 4f50ff5aedbe..659f144a41b6 100644 --- a/params/forks/forks.go +++ b/params/forks/forks.go @@ -40,3 +40,48 @@ const ( Cancun Prague ) + +func (f Fork) String() string { + switch f { + case Prague: + return "prague" + case Cancun: + return "cancun" + case Shanghai: + return "shanghai" + case Paris: + return "paris" + case GrayGlacier: + return "grayGlacier" + case ArrowGlacier: + return "arrowGlacier" + case London: + return "london" + case Berlin: + return "berlin" + case MuirGlacier: + return "muirGlacier" + case Istanbul: + return "istanbul" + case Petersburg: + return "petersburg" + case Constantinople: + return "constantinople" + case Byzantium: + return "byzantium" + case SpuriousDragon: + return "spuriousDragon" + case TangerineWhistle: + return "tangerineWhistle" + case DAO: + return "dao" + case Homestead: + return "homestead" + case FrontierThawing: + return "frontierThawing" + case Frontier: + return "frontier" + default: + panic("unknown fork") + } +} From 4fe7871f00ca7a4d06a02e890a4b5dfa8f70defa Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Fri, 30 Aug 2024 09:12:58 +0200 Subject: [PATCH 02/12] eth/tracers: add test runner for supply tracer --- eth/tracers/live/supply_test.go | 83 +++++++++++++++++++++++++++++++++ tests/block_test_util.go | 8 ++++ 2 files changed, 91 insertions(+) create mode 100644 eth/tracers/live/supply_test.go diff --git a/eth/tracers/live/supply_test.go b/eth/tracers/live/supply_test.go new file mode 100644 index 000000000000..18c4b94d9c39 --- /dev/null +++ b/eth/tracers/live/supply_test.go @@ -0,0 +1,83 @@ +package live + +import ( + "bufio" + "encoding/json" + "fmt" + "os" + "path" + "path/filepath" + "strings" + "testing" + "unicode" + + "github.com/ethereum/go-ethereum/tests" +) + +func TestSupplyTracerBlockchain(t *testing.T) { + dirPath := "supply" + files, err := os.ReadDir(filepath.Join("testdata", dirPath)) + if err != nil { + t.Fatalf("failed to retrieve tracer test suite: %v", err) + } + for _, file := range files { + if !strings.HasSuffix(file.Name(), ".json") { + continue + } + file := file // capture range variable + var testcases map[string]*tests.BlockTest + var blob []byte + // Call tracer test found, read if from disk + if blob, err = os.ReadFile(filepath.Join("testdata", dirPath, file.Name())); err != nil { + t.Fatalf("failed to read testcase: %v", err) + } + if err := json.Unmarshal(blob, &testcases); err != nil { + t.Fatalf("failed to parse testcase: %v", err) + } + for testname, test := range testcases { + t.Run(fmt.Sprintf("%s/%s", camel(strings.TrimSuffix(file.Name(), ".json")), testname), func(t *testing.T) { + t.Parallel() + + traceOutputPath := filepath.ToSlash(t.TempDir()) + traceOutputFilename := path.Join(traceOutputPath, "supply.jsonl") + // Load supply tracer + tracer, err := newSupply(json.RawMessage(fmt.Sprintf(`{"path":"%s"}`, traceOutputPath))) + if err != nil { + t.Fatalf("failed to create call tracer: %v", err) + } + if err := test.Run(false, "path", false, tracer, nil); err != nil { + t.Errorf("failed to run test: %v\n", err) + } + // Check and compare the results + file, err := os.OpenFile(traceOutputFilename, os.O_RDONLY, 0666) + if err != nil { + t.Fatalf("failed to open output file: %v", err) + } + defer file.Close() + var output []supplyInfo + scanner := bufio.NewScanner(file) + + for scanner.Scan() { + blockBytes := scanner.Bytes() + + var info supplyInfo + if err := json.Unmarshal(blockBytes, &info); err != nil { + t.Fatalf("failed to unmarshal result: %v", err) + } + + output = append(output, info) + } + fmt.Printf("output: %v\n", output) + }) + } + } +} + +// camel converts a snake cased input string into a camel cased output. +func camel(str string) string { + pieces := strings.Split(str, "_") + for i := 1; i < len(pieces); i++ { + pieces[i] = string(unicode.ToUpper(rune(pieces[i][0]))) + pieces[i][1:] + } + return strings.Join(pieces, "") +} diff --git a/tests/block_test_util.go b/tests/block_test_util.go index 62aa582c828e..3613c98f55c9 100644 --- a/tests/block_test_util.go +++ b/tests/block_test_util.go @@ -55,6 +55,14 @@ func (t *BlockTest) UnmarshalJSON(in []byte) error { return json.Unmarshal(in, &t.json) } +func (t *BlockTest) FromChain(chain *core.BlockChain, start, end uint64) (BlockTest, error) { + bt := BlockTest{} + if head := chain.CurrentHeader().Number.Uint64(); head < end { + return bt, fmt.Errorf("Chain is shorter than requested segment: %d < %d", head, end) + } + return bt, nil +} + type btJSON struct { Blocks []btBlock `json:"blocks"` Genesis btHeader `json:"genesisBlockHeader"` From 55dcd9623fc7915018f1be40873deb48c7e8d566 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Fri, 30 Aug 2024 17:00:03 +0200 Subject: [PATCH 03/12] Fill half supply tests in blockchain format, fix runner --- core/blockchain.go | 2 +- core/genesis.go | 2 +- .../supply_fill_test.go} | 198 ++++++++---------- eth/tracers/live/supply_test.go | 22 +- eth/tracers/live/supply_test_util.go | 50 +++++ .../live/testdata/supply/eip1559_burn.json | 87 ++++++++ .../live/testdata/supply/genesis_alloc.json | 87 ++++++++ .../live/testdata/supply/omitted_fields.json | 74 +++++++ tests/block_test_util.go | 87 +++++++- 9 files changed, 480 insertions(+), 129 deletions(-) rename eth/tracers/{internal/tracetest/supply_test.go => live/supply_fill_test.go} (77%) create mode 100644 eth/tracers/live/supply_test_util.go create mode 100644 eth/tracers/live/testdata/supply/eip1559_burn.json create mode 100644 eth/tracers/live/testdata/supply/genesis_alloc.json create mode 100644 eth/tracers/live/testdata/supply/omitted_fields.json diff --git a/core/blockchain.go b/core/blockchain.go index 733525bc706f..5d21cc8d0152 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -420,7 +420,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis } if bc.logger != nil && bc.logger.OnGenesisBlock != nil { if block := bc.CurrentBlock(); block.Number.Uint64() == 0 { - alloc, err := getGenesisState(bc.db, block.Hash()) + alloc, err := GetGenesisState(bc.db, block.Hash()) if err != nil { return nil, fmt.Errorf("failed to get genesis state: %w", err) } diff --git a/core/genesis.go b/core/genesis.go index d83aab0baf89..a819f9b88247 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -177,7 +177,7 @@ func flushAlloc(ga *types.GenesisAlloc, db ethdb.Database, triedb *triedb.Databa return root, nil } -func getGenesisState(db ethdb.Database, blockhash common.Hash) (alloc types.GenesisAlloc, err error) { +func GetGenesisState(db ethdb.Database, blockhash common.Hash) (alloc types.GenesisAlloc, err error) { blob := rawdb.ReadGenesisStateSpec(db, blockhash) if len(blob) != 0 { if err := alloc.UnmarshalJSON(blob); err != nil { diff --git a/eth/tracers/internal/tracetest/supply_test.go b/eth/tracers/live/supply_fill_test.go similarity index 77% rename from eth/tracers/internal/tracetest/supply_test.go rename to eth/tracers/live/supply_fill_test.go index d608b1e002af..79003b780d09 100644 --- a/eth/tracers/internal/tracetest/supply_test.go +++ b/eth/tracers/live/supply_fill_test.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package tracetest +package live import ( "bufio" @@ -28,7 +28,6 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/consensus/beacon" "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/core" @@ -37,34 +36,10 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/eth/tracers" + "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/params" - - // Force-load live packages, to trigger registration - _ "github.com/ethereum/go-ethereum/eth/tracers/live" ) -type supplyInfoIssuance struct { - GenesisAlloc *hexutil.Big `json:"genesisAlloc,omitempty"` - Reward *hexutil.Big `json:"reward,omitempty"` - Withdrawals *hexutil.Big `json:"withdrawals,omitempty"` -} - -type supplyInfoBurn struct { - EIP1559 *hexutil.Big `json:"1559,omitempty"` - Blob *hexutil.Big `json:"blob,omitempty"` - Misc *hexutil.Big `json:"misc,omitempty"` -} - -type supplyInfo struct { - Issuance *supplyInfoIssuance `json:"issuance,omitempty"` - Burn *supplyInfoBurn `json:"burn,omitempty"` - - // Block info - Number uint64 `json:"blockNumber"` - Hash common.Hash `json:"hash"` - ParentHash common.Hash `json:"parentHash"` -} - func emptyBlockGenerationFunc(b *core.BlockGen) {} func TestSupplyOmittedFields(t *testing.T) { @@ -73,25 +48,25 @@ func TestSupplyOmittedFields(t *testing.T) { gspec = &core.Genesis{ Config: &config, } + expected = []supplyInfo{{ + Number: 0, + Hash: common.HexToHash("0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703"), + ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), + }, { + Number: 1, + Hash: common.HexToHash("0xe430cdf604a88b9d713d4f89fd100ddddf38c1cc6b049e3d5df563c7bfd320fc"), + ParentHash: common.HexToHash("0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703"), + }} ) - gspec.Config.TerminalTotalDifficulty = big.NewInt(0) - - out, _, err := testSupplyTracer(t, gspec, func(b *core.BlockGen) { + out, db, chain, err := testSupplyTracer(t, gspec, func(b *core.BlockGen) { b.SetPoS() }) if err != nil { t.Fatalf("failed to test supply tracer: %v", err) } - - expected := supplyInfo{ - Number: 0, - Hash: common.HexToHash("0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703"), - ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), - } - actual := out[expected.Number] - - compareAsJSON(t, expected, actual) + compareAsJSON(t, expected, out) + writeArtifact(t, "omitted_fields_cancun", db, chain, expected) } func TestSupplyGenesisAlloc(t *testing.T) { @@ -103,61 +78,36 @@ func TestSupplyGenesisAlloc(t *testing.T) { eth1 = new(big.Int).Mul(common.Big1, big.NewInt(params.Ether)) config = *params.AllEthashProtocolChanges - - gspec = &core.Genesis{ + gspec = &core.Genesis{ Config: &config, Alloc: types.GenesisAlloc{ addr1: {Balance: eth1}, addr2: {Balance: eth1}, }, } + expected = []supplyInfo{{ + Issuance: &supplyInfoIssuance{ + GenesisAlloc: new(big.Int).Mul(common.Big2, big.NewInt(params.Ether)), + }, + Number: 0, + Hash: common.HexToHash("0xbcc9466e9fc6a8b56f4b29ca353a421ff8b51a0c1a58ca4743b427605b08f2ca"), + ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), + }, { + Issuance: &supplyInfoIssuance{ + Reward: new(big.Int).Mul(common.Big2, big.NewInt(params.Ether)), + }, + Number: 1, + Hash: common.HexToHash("0x37bb7e9b45f4fb7b311abb5f815e3e00d3382d83a2c39b9b0bd22b717566cd04"), + ParentHash: common.HexToHash("0xbcc9466e9fc6a8b56f4b29ca353a421ff8b51a0c1a58ca4743b427605b08f2ca"), + }} ) - expected := supplyInfo{ - Issuance: &supplyInfoIssuance{ - GenesisAlloc: (*hexutil.Big)(new(big.Int).Mul(common.Big2, big.NewInt(params.Ether))), - }, - Number: 0, - Hash: common.HexToHash("0xbcc9466e9fc6a8b56f4b29ca353a421ff8b51a0c1a58ca4743b427605b08f2ca"), - ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), - } - - out, _, err := testSupplyTracer(t, gspec, emptyBlockGenerationFunc) - if err != nil { - t.Fatalf("failed to test supply tracer: %v", err) - } - - actual := out[expected.Number] - - compareAsJSON(t, expected, actual) -} - -func TestSupplyRewards(t *testing.T) { - var ( - config = *params.AllEthashProtocolChanges - - gspec = &core.Genesis{ - Config: &config, - } - ) - - expected := supplyInfo{ - Issuance: &supplyInfoIssuance{ - Reward: (*hexutil.Big)(new(big.Int).Mul(common.Big2, big.NewInt(params.Ether))), - }, - Number: 1, - Hash: common.HexToHash("0xcbb08370505be503dafedc4e96d139ea27aba3cbc580148568b8a307b3f51052"), - ParentHash: common.HexToHash("0xadeda0a83e337b6c073e3f0e9a17531a04009b397a9588c093b628f21b8bc5a3"), - } - - out, _, err := testSupplyTracer(t, gspec, emptyBlockGenerationFunc) + out, db, chain, err := testSupplyTracer(t, gspec, emptyBlockGenerationFunc) if err != nil { t.Fatalf("failed to test supply tracer: %v", err) } - - actual := out[expected.Number] - - compareAsJSON(t, expected, actual) + compareAsJSON(t, expected, out) + writeArtifact(t, "genesis_alloc_grayGlacier", db, chain, expected) } func TestSupplyEip1559Burn(t *testing.T) { @@ -179,9 +129,8 @@ func TestSupplyEip1559Burn(t *testing.T) { }, } ) - - signer := types.LatestSigner(gspec.Config) - + config.ChainID = big.NewInt(1) + signer := types.LatestSigner(&config) eip1559BlockGenerationFunc := func(b *core.BlockGen) { txdata := &types.DynamicFeeTx{ ChainID: gspec.Config.ChainID, @@ -197,7 +146,7 @@ func TestSupplyEip1559Burn(t *testing.T) { b.AddTx(tx) } - out, chain, err := testSupplyTracer(t, gspec, eip1559BlockGenerationFunc) + out, db, chain, err := testSupplyTracer(t, gspec, eip1559BlockGenerationFunc) if err != nil { t.Fatalf("failed to test supply tracer: %v", err) } @@ -205,21 +154,27 @@ func TestSupplyEip1559Burn(t *testing.T) { head = chain.CurrentBlock() reward = new(big.Int).Mul(common.Big2, big.NewInt(params.Ether)) burn = new(big.Int).Mul(big.NewInt(21000), head.BaseFee) - expected = supplyInfo{ + expected = []supplyInfo{{ Issuance: &supplyInfoIssuance{ - Reward: (*hexutil.Big)(reward), + GenesisAlloc: eth1, + }, + Number: 0, + Hash: common.HexToHash("0xc4265421181cafc43e4b97ae4f21530e37e00320f219a13311482c9c552bcdc7"), + ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), + }, { + Issuance: &supplyInfoIssuance{ + Reward: reward, }, Burn: &supplyInfoBurn{ - EIP1559: (*hexutil.Big)(burn), + EIP1559: burn, }, Number: 1, Hash: head.Hash(), ParentHash: head.ParentHash, - } + }} ) - - actual := out[expected.Number] - compareAsJSON(t, expected, actual) + compareAsJSON(t, expected, out) + writeArtifact(t, "eip1559_burn_grayGlacier", db, chain, expected) } func TestSupplyWithdrawals(t *testing.T) { @@ -240,7 +195,7 @@ func TestSupplyWithdrawals(t *testing.T) { }) } - out, chain, err := testSupplyTracer(t, gspec, withdrawalsBlockGenerationFunc) + out, _, chain, err := testSupplyTracer(t, gspec, withdrawalsBlockGenerationFunc) if err != nil { t.Fatalf("failed to test supply tracer: %v", err) } @@ -249,7 +204,7 @@ func TestSupplyWithdrawals(t *testing.T) { head = chain.CurrentBlock() expected = supplyInfo{ Issuance: &supplyInfoIssuance{ - Withdrawals: (*hexutil.Big)(big.NewInt(1337000000000)), + Withdrawals: big.NewInt(1337000000000), }, Number: 1, Hash: head.Hash(), @@ -320,7 +275,7 @@ func TestSupplySelfdestruct(t *testing.T) { } // 1. Test pre Cancun - preCancunOutput, preCancunChain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc) + preCancunOutput, _, preCancunChain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc) if err != nil { t.Fatalf("Pre-cancun failed to test supply tracer: %v", err) } @@ -344,8 +299,8 @@ func TestSupplySelfdestruct(t *testing.T) { // Check live trace output expected := supplyInfo{ Burn: &supplyInfoBurn{ - EIP1559: (*hexutil.Big)(big.NewInt(55289500000000)), - Misc: (*hexutil.Big)(big.NewInt(5000000000)), + EIP1559: big.NewInt(55289500000000), + Misc: big.NewInt(5000000000), }, Number: 1, Hash: head.Hash(), @@ -361,7 +316,7 @@ func TestSupplySelfdestruct(t *testing.T) { gspec.Config.ShanghaiTime = &cancunTime gspec.Config.CancunTime = &cancunTime - postCancunOutput, postCancunChain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc) + postCancunOutput, _, postCancunChain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc) if err != nil { t.Fatalf("Post-cancun failed to test supply tracer: %v", err) } @@ -385,7 +340,7 @@ func TestSupplySelfdestruct(t *testing.T) { head = postCancunChain.CurrentBlock() expected = supplyInfo{ Burn: &supplyInfoBurn{ - EIP1559: (*hexutil.Big)(big.NewInt(55289500000000)), + EIP1559: big.NewInt(55289500000000), }, Number: 1, Hash: head.Hash(), @@ -501,7 +456,7 @@ func TestSupplySelfdestructItselfAndRevert(t *testing.T) { b.AddTx(tx) } - output, chain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc) + output, _, chain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc) if err != nil { t.Fatalf("failed to test supply tracer: %v", err) } @@ -530,8 +485,8 @@ func TestSupplySelfdestructItselfAndRevert(t *testing.T) { expected := supplyInfo{ Burn: &supplyInfoBurn{ - EIP1559: (*hexutil.Big)(new(big.Int).Mul(block.BaseFee(), big.NewInt(int64(block.GasUsed())))), - Misc: (*hexutil.Big)(eth5), // 5ETH burned from contract B + EIP1559: new(big.Int).Mul(block.BaseFee(), big.NewInt(int64(block.GasUsed()))), + Misc: eth5, // 5ETH burned from contract B }, Number: 1, Hash: block.Hash(), @@ -543,7 +498,7 @@ func TestSupplySelfdestructItselfAndRevert(t *testing.T) { compareAsJSON(t, expected, actual) } -func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockGen)) ([]supplyInfo, *core.BlockChain, error) { +func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockGen)) ([]supplyInfo, ethdb.Database, *core.BlockChain, error) { var ( engine = beacon.New(ethash.NewFaker()) ) @@ -554,12 +509,13 @@ func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockG // Load supply tracer tracer, err := tracers.LiveDirectory.New("supply", json.RawMessage(fmt.Sprintf(`{"path":"%s"}`, traceOutputPath))) if err != nil { - return nil, nil, fmt.Errorf("failed to create call tracer: %v", err) + return nil, nil, nil, fmt.Errorf("failed to create call tracer: %v", err) } - chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), core.DefaultCacheConfigWithScheme(rawdb.PathScheme), genesis, nil, engine, vm.Config{Tracer: tracer}, nil, nil) + db := rawdb.NewMemoryDatabase() + chain, err := core.NewBlockChain(db, core.DefaultCacheConfigWithScheme(rawdb.PathScheme), genesis, nil, engine, vm.Config{Tracer: tracer}, nil, nil) if err != nil { - return nil, nil, fmt.Errorf("failed to create tester chain: %v", err) + return nil, nil, nil, fmt.Errorf("failed to create tester chain: %v", err) } defer chain.Stop() @@ -568,14 +524,15 @@ func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockG gen(b) }) + fmt.Printf("blocks: %v\n", blocks) if n, err := chain.InsertChain(blocks); err != nil { - return nil, chain, fmt.Errorf("block %d: failed to insert into chain: %v", n, err) + return nil, nil, chain, fmt.Errorf("block %d: failed to insert into chain: %v", n, err) } // Check and compare the results file, err := os.OpenFile(traceOutputFilename, os.O_RDONLY, 0666) if err != nil { - return nil, chain, fmt.Errorf("failed to open output file: %v", err) + return nil, nil, chain, fmt.Errorf("failed to open output file: %v", err) } defer file.Close() @@ -587,13 +544,13 @@ func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockG var info supplyInfo if err := json.Unmarshal(blockBytes, &info); err != nil { - return nil, chain, fmt.Errorf("failed to unmarshal result: %v", err) + return nil, nil, chain, fmt.Errorf("failed to unmarshal result: %v", err) } output = append(output, info) } - return output, chain, nil + return output, db, chain, nil } func compareAsJSON(t *testing.T, expected interface{}, actual interface{}) { @@ -601,13 +558,26 @@ func compareAsJSON(t *testing.T, expected interface{}, actual interface{}) { if err != nil { t.Fatalf("failed to marshal expected value to JSON: %v", err) } - have, err := json.Marshal(actual) if err != nil { t.Fatalf("failed to marshal actual value to JSON: %v", err) } - if !bytes.Equal(want, have) { t.Fatalf("incorrect supply info: expected %s, got %s", string(want), string(have)) } } + +func writeArtifact(t *testing.T, name string, db ethdb.Database, chain *core.BlockChain, expected []supplyInfo) { + bt, err := btFromChain(db, chain) + if err != nil { + t.Fatalf("failed to fill tests from chain: %v", err) + } + bt.Expected = expected + result := make(map[string]any) + result[name] = bt + enc, err := json.MarshalIndent(&result, "", " ") + if err != nil { + t.Fatalf("failed to marshal tests: %v", err) + } + t.Logf("Tests: %s", enc) +} diff --git a/eth/tracers/live/supply_test.go b/eth/tracers/live/supply_test.go index 18c4b94d9c39..aa08b68cf47b 100644 --- a/eth/tracers/live/supply_test.go +++ b/eth/tracers/live/supply_test.go @@ -10,8 +10,6 @@ import ( "strings" "testing" "unicode" - - "github.com/ethereum/go-ethereum/tests" ) func TestSupplyTracerBlockchain(t *testing.T) { @@ -25,7 +23,7 @@ func TestSupplyTracerBlockchain(t *testing.T) { continue } file := file // capture range variable - var testcases map[string]*tests.BlockTest + var testcases map[string]*BlockTest var blob []byte // Call tracer test found, read if from disk if blob, err = os.ReadFile(filepath.Join("testdata", dirPath, file.Name())); err != nil { @@ -45,7 +43,7 @@ func TestSupplyTracerBlockchain(t *testing.T) { if err != nil { t.Fatalf("failed to create call tracer: %v", err) } - if err := test.Run(false, "path", false, tracer, nil); err != nil { + if err := test.bt.Run(false, "path", false, tracer, nil); err != nil { t.Errorf("failed to run test: %v\n", err) } // Check and compare the results @@ -54,20 +52,26 @@ func TestSupplyTracerBlockchain(t *testing.T) { t.Fatalf("failed to open output file: %v", err) } defer file.Close() - var output []supplyInfo - scanner := bufio.NewScanner(file) + var ( + output []supplyInfo + scanner = bufio.NewScanner(file) + ) for scanner.Scan() { blockBytes := scanner.Bytes() - var info supplyInfo if err := json.Unmarshal(blockBytes, &info); err != nil { t.Fatalf("failed to unmarshal result: %v", err) } - output = append(output, info) } - fmt.Printf("output: %v\n", output) + if len(output) != len(test.Expected) { + fmt.Printf("output: %v\n", output) + t.Fatalf("expected %d supply infos, got %d", len(test.Expected), len(output)) + } + for i, expected := range test.Expected { + compareAsJSON(t, expected, output[i]) + } }) } } diff --git a/eth/tracers/live/supply_test_util.go b/eth/tracers/live/supply_test_util.go new file mode 100644 index 000000000000..ca04ad934bcf --- /dev/null +++ b/eth/tracers/live/supply_test_util.go @@ -0,0 +1,50 @@ +package live + +import ( + "encoding/json" + + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/tests" +) + +type BlockTest struct { + bt *tests.BlockTest + Expected []supplyInfo `json:"expected"` +} + +func btFromChain(db ethdb.Database, chain *core.BlockChain) (*BlockTest, error) { + bt, err := tests.FromChain(db, chain) + if err != nil { + return nil, err + } + return &BlockTest{bt: &bt}, nil +} + +func (bt *BlockTest) UnmarshalJSON(data []byte) error { + tmp := make(map[string]json.RawMessage) + if err := json.Unmarshal(data, &tmp); err != nil { + return err + } + if err := json.Unmarshal(tmp["expected"], &bt.Expected); err != nil { + return err + } + if err := json.Unmarshal(data, &bt.bt); err != nil { + return err + } + return nil +} + +func (bt *BlockTest) MarshalJSON() ([]byte, error) { + enc, err := json.Marshal(bt.bt) + if err != nil { + return nil, err + } + // Insert the expected supply info + result := make(map[string]any) + if err := json.Unmarshal(enc, &result); err != nil { + return nil, err + } + result["expected"] = bt.Expected + return json.Marshal(result) +} diff --git a/eth/tracers/live/testdata/supply/eip1559_burn.json b/eth/tracers/live/testdata/supply/eip1559_burn.json new file mode 100644 index 000000000000..44fe0a328e77 --- /dev/null +++ b/eth/tracers/live/testdata/supply/eip1559_burn.json @@ -0,0 +1,87 @@ +{ + "eip1559_burn_grayGlacier": { + "blocks": [ + { + "BlockHeader": { + "BaseFeePerGas": "0x342770c0", + "BlobGasUsed": null, + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0100000000000000000000000000000000000000", + "Difficulty": "0x20000", + "ExcessBlobGas": null, + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0x5208", + "Hash": "0x7891c11e0cc121c578c62c4b42622b909f4ded1a66fea03336c303e6bc8d6f88", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x1", + "ParentBeaconBlockRoot": null, + "ParentHash": "0xc4265421181cafc43e4b97ae4f21530e37e00320f219a13311482c9c552bcdc7", + "ReceiptTrie": "0xf78dfb743fbd92ade140711c8bbc542b5e307f0ab7984eff35d751969fe57efa", + "StateRoot": "0x208dc296c4dc37b673a99aed4837174a2ed7f5380ad802d5aed0295e6795241d", + "Timestamp": "0xa", + "TransactionsTrie": "0x4ff793bd96fb3d8d186476e59a5118de9f1813b3f22bd8a64875a535422ac62f", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": null + }, + "ExpectException": "", + "Rlp": "0xf9026cf901faa0c4265421181cafc43e4b97ae4f21530e37e00320f219a13311482c9c552bcdc7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940100000000000000000000000000000000000000a0208dc296c4dc37b673a99aed4837174a2ed7f5380ad802d5aed0295e6795241da04ff793bd96fb3d8d186476e59a5118de9f1813b3f22bd8a64875a535422ac62fa0f78dfb743fbd92ade140711c8bbc542b5e307f0ab7984eff35d751969fe57efab901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000018347e7c48252080a80a0000000000000000000000000000000000000000000000000000000000000000088000000000000000084342770c0f86cb86a02f86701800285012a05f20082520894000000000000000000000000000000000000aaaa8080c001a083b46f9cdfcb2c087934d9ae79eedeaa53df39b73c58b7cba5c77d69039bdc64a05af6144d3123f5b9850f9c40fc4b8ce02bf4782f6d6bca3f88e553689d66480bc0", + "UncleHeaders": null + } + ], + "expected": [ + { + "issuance": { + "genesisAlloc": "0xde0b6b3a7640000" + }, + "blockNumber": 0, + "hash": "0xc4265421181cafc43e4b97ae4f21530e37e00320f219a13311482c9c552bcdc7", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "issuance": { + "reward": "0x1bc16d674ec80000" + }, + "burn": { + "1559": "0x10b643590600" + }, + "blockNumber": 1, + "hash": "0x7891c11e0cc121c578c62c4b42622b909f4ded1a66fea03336c303e6bc8d6f88", + "parentHash": "0xc4265421181cafc43e4b97ae4f21530e37e00320f219a13311482c9c552bcdc7" + } + ], + "genesisBlockHeader": { + "BaseFeePerGas": "0x3b9aca00", + "BlobGasUsed": null, + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0000000000000000000000000000000000000000", + "Difficulty": "0x20000", + "ExcessBlobGas": null, + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0x0", + "Hash": "0xc4265421181cafc43e4b97ae4f21530e37e00320f219a13311482c9c552bcdc7", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x0", + "ParentBeaconBlockRoot": null, + "ParentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ReceiptTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "StateRoot": "0x9f88be00eee1114edfd9372f52560aab3980a142efe8b5b39a09644075084275", + "Timestamp": "0x0", + "TransactionsTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": null + }, + "lastblockhash": "7891c11e0cc121c578c62c4b42622b909f4ded1a66fea03336c303e6bc8d6f88", + "network": "GrayGlacier", + "postState": {}, + "pre": { + "0x71562b71999873db5b286df957af199ec94617f7": { + "balance": "0xde0b6b3a7640000" + } + }, + "sealEngine": "" + } +} \ No newline at end of file diff --git a/eth/tracers/live/testdata/supply/genesis_alloc.json b/eth/tracers/live/testdata/supply/genesis_alloc.json new file mode 100644 index 000000000000..940ef6735ddb --- /dev/null +++ b/eth/tracers/live/testdata/supply/genesis_alloc.json @@ -0,0 +1,87 @@ +{ + "genesis_alloc_grayGlacier": { + "blocks": [ + { + "BlockHeader": { + "BaseFeePerGas": "0x342770c0", + "BlobGasUsed": null, + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0100000000000000000000000000000000000000", + "Difficulty": "0x20000", + "ExcessBlobGas": null, + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0x0", + "Hash": "0x37bb7e9b45f4fb7b311abb5f815e3e00d3382d83a2c39b9b0bd22b717566cd04", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x1", + "ParentBeaconBlockRoot": null, + "ParentHash": "0xbcc9466e9fc6a8b56f4b29ca353a421ff8b51a0c1a58ca4743b427605b08f2ca", + "ReceiptTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "StateRoot": "0x98d03dd72cdfee5c55acc619a1f1bcb1be6d1a10b25b512e7c4e4c4413357940", + "Timestamp": "0xa", + "TransactionsTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": null + }, + "ExpectException": "", + "Rlp": "0xf901fdf901f8a0bcc9466e9fc6a8b56f4b29ca353a421ff8b51a0c1a58ca4743b427605b08f2caa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940100000000000000000000000000000000000000a098d03dd72cdfee5c55acc619a1f1bcb1be6d1a10b25b512e7c4e4c4413357940a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000018347e7c4800a80a0000000000000000000000000000000000000000000000000000000000000000088000000000000000084342770c0c0c0", + "UncleHeaders": null + } + ], + "expected": [ + { + "issuance": { + "genesisAlloc": "0x1bc16d674ec80000" + }, + "blockNumber": 0, + "hash": "0xbcc9466e9fc6a8b56f4b29ca353a421ff8b51a0c1a58ca4743b427605b08f2ca", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "issuance": { + "reward": "0x1bc16d674ec80000" + }, + "blockNumber": 1, + "hash": "0x37bb7e9b45f4fb7b311abb5f815e3e00d3382d83a2c39b9b0bd22b717566cd04", + "parentHash": "0xbcc9466e9fc6a8b56f4b29ca353a421ff8b51a0c1a58ca4743b427605b08f2ca" + } + ], + "genesisBlockHeader": { + "BaseFeePerGas": "0x3b9aca00", + "BlobGasUsed": null, + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0000000000000000000000000000000000000000", + "Difficulty": "0x20000", + "ExcessBlobGas": null, + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0x0", + "Hash": "0xbcc9466e9fc6a8b56f4b29ca353a421ff8b51a0c1a58ca4743b427605b08f2ca", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x0", + "ParentBeaconBlockRoot": null, + "ParentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ReceiptTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "StateRoot": "0x4eaed1ec95373a6cad785d6b2506606a2c19e2e7c6d7faf4bab3472c21861b27", + "Timestamp": "0x0", + "TransactionsTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": null + }, + "lastblockhash": "37bb7e9b45f4fb7b311abb5f815e3e00d3382d83a2c39b9b0bd22b717566cd04", + "network": "GrayGlacier", + "postState": {}, + "pre": { + "0x703c4b2bd70c169f5717101caee543299fc946c7": { + "balance": "0xde0b6b3a7640000" + }, + "0x71562b71999873db5b286df957af199ec94617f7": { + "balance": "0xde0b6b3a7640000" + } + }, + "sealEngine": "" + } +} \ No newline at end of file diff --git a/eth/tracers/live/testdata/supply/omitted_fields.json b/eth/tracers/live/testdata/supply/omitted_fields.json new file mode 100644 index 000000000000..2ffbfe81fdd2 --- /dev/null +++ b/eth/tracers/live/testdata/supply/omitted_fields.json @@ -0,0 +1,74 @@ +{ + "omitted_fields_cancun": { + "expected": [ + { + "blockNumber": 0, + "hash": "0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "blockNumber": 1, + "hash": "0xe430cdf604a88b9d713d4f89fd100ddddf38c1cc6b049e3d5df563c7bfd320fc", + "parentHash": "0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703" + } + ], + "blocks": [ + { + "BlockHeader": { + "BaseFeePerGas": "0x342770c0", + "BlobGasUsed": "0x0", + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0100000000000000000000000000000000000000", + "Difficulty": "0x0", + "ExcessBlobGas": "0x0", + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0x0", + "Hash": "0xe430cdf604a88b9d713d4f89fd100ddddf38c1cc6b049e3d5df563c7bfd320fc", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x1", + "ParentBeaconBlockRoot": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ParentHash": "0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703", + "ReceiptTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "StateRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "Timestamp": "0xa", + "TransactionsTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" + }, + "ExpectException": "", + "Rlp": "0xf9023ff90239a052f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940100000000000000000000000000000000000000a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080018347e7c4800a80a0000000000000000000000000000000000000000000000000000000000000000088000000000000000084342770c0a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b4218080a00000000000000000000000000000000000000000000000000000000000000000c0c0c0", + "UncleHeaders": null + } + ], + "genesisBlockHeader": { + "BaseFeePerGas": "0x3b9aca00", + "BlobGasUsed": "0x0", + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0000000000000000000000000000000000000000", + "Difficulty": "0x20000", + "ExcessBlobGas": "0x0", + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0x0", + "Hash": "0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x0", + "ParentBeaconBlockRoot": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ParentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ReceiptTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "StateRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "Timestamp": "0x0", + "TransactionsTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" + }, + "lastblockhash": "e430cdf604a88b9d713d4f89fd100ddddf38c1cc6b049e3d5df563c7bfd320fc", + "network": "Cancun", + "postState": {}, + "pre": {}, + "sealEngine": "" + } +} \ No newline at end of file diff --git a/tests/block_test_util.go b/tests/block_test_util.go index 3613c98f55c9..edd9634efcb5 100644 --- a/tests/block_test_util.go +++ b/tests/block_test_util.go @@ -25,6 +25,7 @@ import ( "math/big" "os" "reflect" + "strings" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -37,6 +38,7 @@ import ( "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" @@ -55,10 +57,41 @@ func (t *BlockTest) UnmarshalJSON(in []byte) error { return json.Unmarshal(in, &t.json) } -func (t *BlockTest) FromChain(chain *core.BlockChain, start, end uint64) (BlockTest, error) { - bt := BlockTest{} - if head := chain.CurrentHeader().Number.Uint64(); head < end { - return bt, fmt.Errorf("Chain is shorter than requested segment: %d < %d", head, end) +// MarshalJSON implements json.Marshaler interface. +func (t *BlockTest) MarshalJSON() ([]byte, error) { + return json.Marshal(t.json) +} + +// FromChain creates a BlockTest from the history of the given chain. +func FromChain(db ethdb.Database, chain *core.BlockChain) (BlockTest, error) { + var ( + bt = BlockTest{} + head = chain.CurrentHeader() + blocks = make([]btBlock, head.Number.Uint64()) + ) + for i := 1; i <= int(head.Number.Uint64()); i++ { + block := chain.GetBlockByNumber(uint64(i)) + fmt.Printf("block: %v\n", block) + if block == nil { + return bt, fmt.Errorf("block %d not found", i) + } + btBlock, err := FromBlock(block) + if err != nil { + return bt, err + } + blocks[i-1] = btBlock + } + alloc, err := core.GetGenesisState(db, chain.Genesis().Hash()) + if err != nil { + return bt, fmt.Errorf("failed to get genesis alloc: %v", err) + } + bt.json = btJSON{ + Network: capitalize(chain.Config().LatestFork(head.Number, head.Time).String()), + Blocks: blocks, + Genesis: FromHeader(chain.Genesis().Header()), + Pre: alloc, + Post: make(types.GenesisAlloc), + BestBlock: common.UnprefixedHash(head.Hash()), } return bt, nil } @@ -80,6 +113,18 @@ type btBlock struct { UncleHeaders []*btHeader } +func FromBlock(block *types.Block) (btBlock, error) { + header := FromHeader(block.Header()) + enc, err := rlp.EncodeToBytes(block) + if err != nil { + return btBlock{}, err + } + return btBlock{ + BlockHeader: &header, + Rlp: "0x" + common.Bytes2Hex(enc), + }, nil +} + //go:generate go run github.com/fjl/gencodec -type btHeader -field-override btHeaderMarshaling -out gen_btheader.go type btHeader struct { @@ -106,6 +151,32 @@ type btHeader struct { ParentBeaconBlockRoot *common.Hash } +func FromHeader(header *types.Header) btHeader { + return btHeader{ + Bloom: header.Bloom, + Coinbase: header.Coinbase, + MixHash: header.MixDigest, + Nonce: header.Nonce, + Number: header.Number, + Hash: header.Hash(), + ParentHash: header.ParentHash, + ReceiptTrie: header.ReceiptHash, + StateRoot: header.Root, + TransactionsTrie: header.TxHash, + UncleHash: header.UncleHash, + ExtraData: header.Extra, + Difficulty: header.Difficulty, + GasLimit: header.GasLimit, + GasUsed: header.GasUsed, + Timestamp: header.Time, + BaseFeePerGas: header.BaseFee, + WithdrawalsRoot: header.WithdrawalsHash, + BlobGasUsed: header.BlobGasUsed, + ExcessBlobGas: header.ExcessBlobGas, + ParentBeaconBlockRoot: header.ParentBeaconRoot, + } +} + type btHeaderMarshaling struct { ExtraData hexutil.Bytes Number *math.HexOrDecimal256 @@ -387,3 +458,11 @@ func (bb *btBlock) decode() (*types.Block, error) { err = rlp.DecodeBytes(data, &b) return &b, err } + +func capitalize(s string) string { + // Check if the string is empty + if len(s) == 0 { + return s + } + return strings.ToUpper(string(s[0])) + s[1:] +} From eee0d4fc0b754bcf5479a13758758ba1546d883d Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Wed, 4 Sep 2024 13:07:40 +0200 Subject: [PATCH 04/12] fix supply withdrawals --- eth/tracers/live/supply_fill_test.go | 15 +++++++++------ eth/tracers/live/supply_test.go | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/eth/tracers/live/supply_fill_test.go b/eth/tracers/live/supply_fill_test.go index 79003b780d09..93e5bdca33d8 100644 --- a/eth/tracers/live/supply_fill_test.go +++ b/eth/tracers/live/supply_fill_test.go @@ -195,25 +195,28 @@ func TestSupplyWithdrawals(t *testing.T) { }) } - out, _, chain, err := testSupplyTracer(t, gspec, withdrawalsBlockGenerationFunc) + out, db, chain, err := testSupplyTracer(t, gspec, withdrawalsBlockGenerationFunc) if err != nil { t.Fatalf("failed to test supply tracer: %v", err) } var ( head = chain.CurrentBlock() - expected = supplyInfo{ + expected = []supplyInfo{{ + Number: 0, + Hash: common.HexToHash("0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703"), + ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), + }, { Issuance: &supplyInfoIssuance{ Withdrawals: big.NewInt(1337000000000), }, Number: 1, Hash: head.Hash(), ParentHash: head.ParentHash, - } - actual = out[expected.Number] + }} ) - - compareAsJSON(t, expected, actual) + compareAsJSON(t, expected, out) + writeArtifact(t, "withdrawals_cancun", db, chain, expected) } // Tests fund retrieval after contract's selfdestruct. diff --git a/eth/tracers/live/supply_test.go b/eth/tracers/live/supply_test.go index aa08b68cf47b..f7fa754d4483 100644 --- a/eth/tracers/live/supply_test.go +++ b/eth/tracers/live/supply_test.go @@ -30,7 +30,7 @@ func TestSupplyTracerBlockchain(t *testing.T) { t.Fatalf("failed to read testcase: %v", err) } if err := json.Unmarshal(blob, &testcases); err != nil { - t.Fatalf("failed to parse testcase: %v", err) + t.Fatalf("failed to parse testcase %s: %v", file.Name(), err) } for testname, test := range testcases { t.Run(fmt.Sprintf("%s/%s", camel(strings.TrimSuffix(file.Name(), ".json")), testname), func(t *testing.T) { From 4c0ae0388979f2f7595d17bf4bf69237096c0d51 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Wed, 4 Sep 2024 16:26:34 +0200 Subject: [PATCH 05/12] add boilerplate post state --- eth/tracers/live/supply_fill_test.go | 41 +++++++++++++++------------- eth/tracers/live/supply_test_util.go | 5 ++-- tests/block_test_util.go | 7 +++-- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/eth/tracers/live/supply_fill_test.go b/eth/tracers/live/supply_fill_test.go index 93e5bdca33d8..4890eb6f441e 100644 --- a/eth/tracers/live/supply_fill_test.go +++ b/eth/tracers/live/supply_fill_test.go @@ -66,7 +66,7 @@ func TestSupplyOmittedFields(t *testing.T) { t.Fatalf("failed to test supply tracer: %v", err) } compareAsJSON(t, expected, out) - writeArtifact(t, "omitted_fields_cancun", db, chain, expected) + writeArtifact(t, "omitted_fields_cancun", db, chain, expected, nil) } func TestSupplyGenesisAlloc(t *testing.T) { @@ -107,7 +107,7 @@ func TestSupplyGenesisAlloc(t *testing.T) { t.Fatalf("failed to test supply tracer: %v", err) } compareAsJSON(t, expected, out) - writeArtifact(t, "genesis_alloc_grayGlacier", db, chain, expected) + writeArtifact(t, "genesis_alloc_grayGlacier", db, chain, expected, nil) } func TestSupplyEip1559Burn(t *testing.T) { @@ -174,7 +174,7 @@ func TestSupplyEip1559Burn(t *testing.T) { }} ) compareAsJSON(t, expected, out) - writeArtifact(t, "eip1559_burn_grayGlacier", db, chain, expected) + writeArtifact(t, "eip1559_burn_grayGlacier", db, chain, expected, nil) } func TestSupplyWithdrawals(t *testing.T) { @@ -216,7 +216,7 @@ func TestSupplyWithdrawals(t *testing.T) { }} ) compareAsJSON(t, expected, out) - writeArtifact(t, "withdrawals_cancun", db, chain, expected) + writeArtifact(t, "withdrawals_cancun", db, chain, expected, nil) } // Tests fund retrieval after contract's selfdestruct. @@ -437,13 +437,8 @@ func TestSupplySelfdestructItselfAndRevert(t *testing.T) { } ) - gspec.Config.TerminalTotalDifficulty = big.NewInt(0) - signer := types.LatestSigner(gspec.Config) - testBlockGenerationFunc := func(b *core.BlockGen) { - b.SetPoS() - txdata := &types.LegacyTx{ Nonce: 0, To: &aa, @@ -459,7 +454,7 @@ func TestSupplySelfdestructItselfAndRevert(t *testing.T) { b.AddTx(tx) } - output, _, chain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc) + output, db, chain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc) if err != nil { t.Fatalf("failed to test supply tracer: %v", err) } @@ -485,20 +480,28 @@ func TestSupplySelfdestructItselfAndRevert(t *testing.T) { // Check live trace output block := chain.GetBlockByNumber(1) - - expected := supplyInfo{ + expected := []supplyInfo{{ + Issuance: &supplyInfoIssuance{ + GenesisAlloc: new(big.Int).Mul(big.NewInt(9), big.NewInt(params.Ether)), + }, + Number: 0, + Hash: common.HexToHash("0xaf41e72f748de317965454508c749f7e14dc4fe444cd07bca4c981c7e952364d"), + ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), + }, { Burn: &supplyInfoBurn{ EIP1559: new(big.Int).Mul(block.BaseFee(), big.NewInt(int64(block.GasUsed()))), Misc: eth5, // 5ETH burned from contract B }, + Issuance: &supplyInfoIssuance{ + Reward: eth2, + }, Number: 1, Hash: block.Hash(), ParentHash: block.ParentHash(), - } - - actual := output[expected.Number] + }} - compareAsJSON(t, expected, actual) + compareAsJSON(t, expected, output) + writeArtifact(t, "selfdestruct_itself_and_revert_grayGlacier", db, chain, expected, nil) } func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockGen)) ([]supplyInfo, ethdb.Database, *core.BlockChain, error) { @@ -566,12 +569,12 @@ func compareAsJSON(t *testing.T, expected interface{}, actual interface{}) { t.Fatalf("failed to marshal actual value to JSON: %v", err) } if !bytes.Equal(want, have) { - t.Fatalf("incorrect supply info: expected %s, got %s", string(want), string(have)) + t.Fatalf("incorrect supply info:\nexpected:\n%s\ngot:\n%s", string(want), string(have)) } } -func writeArtifact(t *testing.T, name string, db ethdb.Database, chain *core.BlockChain, expected []supplyInfo) { - bt, err := btFromChain(db, chain) +func writeArtifact(t *testing.T, name string, db ethdb.Database, chain *core.BlockChain, expected []supplyInfo, post *types.GenesisAlloc) { + bt, err := btFromChain(db, chain, post) if err != nil { t.Fatalf("failed to fill tests from chain: %v", err) } diff --git a/eth/tracers/live/supply_test_util.go b/eth/tracers/live/supply_test_util.go index ca04ad934bcf..a5387463a0c1 100644 --- a/eth/tracers/live/supply_test_util.go +++ b/eth/tracers/live/supply_test_util.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/tests" ) @@ -13,8 +14,8 @@ type BlockTest struct { Expected []supplyInfo `json:"expected"` } -func btFromChain(db ethdb.Database, chain *core.BlockChain) (*BlockTest, error) { - bt, err := tests.FromChain(db, chain) +func btFromChain(db ethdb.Database, chain *core.BlockChain, post *types.GenesisAlloc) (*BlockTest, error) { + bt, err := tests.FromChain(db, chain, post) if err != nil { return nil, err } diff --git a/tests/block_test_util.go b/tests/block_test_util.go index edd9634efcb5..58fbab9a3a30 100644 --- a/tests/block_test_util.go +++ b/tests/block_test_util.go @@ -63,7 +63,7 @@ func (t *BlockTest) MarshalJSON() ([]byte, error) { } // FromChain creates a BlockTest from the history of the given chain. -func FromChain(db ethdb.Database, chain *core.BlockChain) (BlockTest, error) { +func FromChain(db ethdb.Database, chain *core.BlockChain, post *types.GenesisAlloc) (BlockTest, error) { var ( bt = BlockTest{} head = chain.CurrentHeader() @@ -85,12 +85,15 @@ func FromChain(db ethdb.Database, chain *core.BlockChain) (BlockTest, error) { if err != nil { return bt, fmt.Errorf("failed to get genesis alloc: %v", err) } + if post == nil { + post = &types.GenesisAlloc{} + } bt.json = btJSON{ Network: capitalize(chain.Config().LatestFork(head.Number, head.Time).String()), Blocks: blocks, Genesis: FromHeader(chain.Genesis().Header()), Pre: alloc, - Post: make(types.GenesisAlloc), + Post: *post, BestBlock: common.UnprefixedHash(head.Hash()), } return bt, nil From 4eb0e403f5226934c832ae697e4936b0e6bfef62 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Mon, 9 Sep 2024 17:12:12 +0200 Subject: [PATCH 06/12] forgot artifacts --- .../selfdestruct_itself_and_revert.json | 104 ++++++++++++++++++ .../live/testdata/supply/withdrawals.json | 77 +++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 eth/tracers/live/testdata/supply/selfdestruct_itself_and_revert.json create mode 100644 eth/tracers/live/testdata/supply/withdrawals.json diff --git a/eth/tracers/live/testdata/supply/selfdestruct_itself_and_revert.json b/eth/tracers/live/testdata/supply/selfdestruct_itself_and_revert.json new file mode 100644 index 000000000000..05336e81a83f --- /dev/null +++ b/eth/tracers/live/testdata/supply/selfdestruct_itself_and_revert.json @@ -0,0 +1,104 @@ +{ + "selfdestruct_itself_and_revert_grayGlacier": { + "blocks": [ + { + "BlockHeader": { + "BaseFeePerGas": "0x342770c0", + "BlobGasUsed": null, + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0100000000000000000000000000000000000000", + "Difficulty": "0x20000", + "ExcessBlobGas": null, + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0x97ed", + "Hash": "0x470d8e7af7bebd6c7515f35d2c77c25d003fe814b596557d5943f700b6ede5aa", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x1", + "ParentBeaconBlockRoot": null, + "ParentHash": "0xaf41e72f748de317965454508c749f7e14dc4fe444cd07bca4c981c7e952364d", + "ReceiptTrie": "0x0002d836009201e474b8e616daff83625eb48821fa40b62a5324828c86ccf656", + "StateRoot": "0xe10489118b2484cae44ccf08dbf8312047747a905e1b3888c3569d0b203062e5", + "Timestamp": "0xa", + "TransactionsTrie": "0x35f9144ebabc94d93ed27a6d644cfc8d1e08b5112c85a085fe92f37c273db18b", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": null + }, + "ExpectException": "", + "Rlp": "0xf90267f901faa0af41e72f748de317965454508c749f7e14dc4fe444cd07bca4c981c7e952364da01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940100000000000000000000000000000000000000a0e10489118b2484cae44ccf08dbf8312047747a905e1b3888c3569d0b203062e5a035f9144ebabc94d93ed27a6d644cfc8d1e08b5112c85a085fe92f37c273db18ba00002d836009201e474b8e616daff83625eb48821fa40b62a5324828c86ccf656b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000018347e7c48297ed0a80a0000000000000000000000000000000000000000000000000000000000000000088000000000000000084342770c0f867f8658085012a05f200830249f0941111111111111111111111111111111111111111808025a01244162a9c8a5aadf0c178eb0fca4b1ef8d0e35506bb2a2c9523222b0241c543a00b2c673143c87b44ab4cf52cacd0ef168d579ff1544c694e31e1efa8b00f5563c0", + "UncleHeaders": null + } + ], + "expected": [ + { + "issuance": { + "genesisAlloc": "0x7ce66c50e2840000" + }, + "blockNumber": 0, + "hash": "0xaf41e72f748de317965454508c749f7e14dc4fe444cd07bca4c981c7e952364d", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "issuance": { + "reward": "0x1bc16d674ec80000" + }, + "burn": { + "1559": "0x1ef38c04a1c0", + "misc": "0x4563918244f40000" + }, + "blockNumber": 1, + "hash": "0x470d8e7af7bebd6c7515f35d2c77c25d003fe814b596557d5943f700b6ede5aa", + "parentHash": "0xaf41e72f748de317965454508c749f7e14dc4fe444cd07bca4c981c7e952364d" + } + ], + "genesisBlockHeader": { + "BaseFeePerGas": "0x3b9aca00", + "BlobGasUsed": null, + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0000000000000000000000000000000000000000", + "Difficulty": "0x20000", + "ExcessBlobGas": null, + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0x0", + "Hash": "0xaf41e72f748de317965454508c749f7e14dc4fe444cd07bca4c981c7e952364d", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x0", + "ParentBeaconBlockRoot": null, + "ParentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ReceiptTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "StateRoot": "0x65dc2cb11746f33989e47548a362f38b6d895960cd11b0c2b950583f276933df", + "Timestamp": "0x0", + "TransactionsTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": null + }, + "lastblockhash": "470d8e7af7bebd6c7515f35d2c77c25d003fe814b596557d5943f700b6ede5aa", + "network": "GrayGlacier", + "postState": {}, + "pre": { + "0x1111111111111111111111111111111111111111": { + "balance": "0x0", + "code": "0x73222222222222222222222222222222222222222273444444444444444444444444444444444444444460006000600060006000865af160006000600060006000865af150505050" + }, + "0x2222222222222222222222222222222222222222": { + "balance": "0x4563918244f40000", + "code": "0x3080ff50" + }, + "0x3333333333333333333333333333333333333333": { + "balance": "0xde0b6b3a7640000", + "code": "0x3080ff50" + }, + "0x4444444444444444444444444444444444444444": { + "balance": "0x1bc16d674ec80000", + "code": "0x73333333333333333333333333333333333333333360006000600060006000855af160006000fd5050" + }, + "0x71562b71999873db5b286df957af199ec94617f7": { + "balance": "0xde0b6b3a7640000" + } + }, + "sealEngine": "" + } +} \ No newline at end of file diff --git a/eth/tracers/live/testdata/supply/withdrawals.json b/eth/tracers/live/testdata/supply/withdrawals.json new file mode 100644 index 000000000000..c94bc6465497 --- /dev/null +++ b/eth/tracers/live/testdata/supply/withdrawals.json @@ -0,0 +1,77 @@ +{ + "withdrawals_cancun": { + "blocks": [ + { + "BlockHeader": { + "BaseFeePerGas": "0x342770c0", + "BlobGasUsed": "0x0", + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0100000000000000000000000000000000000000", + "Difficulty": "0x0", + "ExcessBlobGas": "0x0", + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0x0", + "Hash": "0x273c5dc5beed8249b8103b9a168b1c693c17f371be39d675dec50120f4d93402", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x1", + "ParentBeaconBlockRoot": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ParentHash": "0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703", + "ReceiptTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "StateRoot": "0x6e2ae8cee635793fd04490934e7b7d8f727c74534c5a01e42051116e12ba6a1a", + "Timestamp": "0xa", + "TransactionsTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": "0xad2dcd8b054415c239ed4cdfff00f865a00f28676fc12e33fec0ce91e4be10d6" + }, + "ExpectException": "", + "Rlp": "0xf9025af90239a052f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940100000000000000000000000000000000000000a06e2ae8cee635793fd04490934e7b7d8f727c74534c5a01e42051116e12ba6a1aa056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080018347e7c4800a80a0000000000000000000000000000000000000000000000000000000000000000088000000000000000084342770c0a0ad2dcd8b054415c239ed4cdfff00f865a00f28676fc12e33fec0ce91e4be10d68080a00000000000000000000000000000000000000000000000000000000000000000c0c0dbda802a94ee00000000000000000000000000000000000000820539", + "UncleHeaders": null + } + ], + "expected": [ + { + "blockNumber": 0, + "hash": "0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "issuance": { + "withdrawals": "0x1374b68fa00" + }, + "blockNumber": 1, + "hash": "0x273c5dc5beed8249b8103b9a168b1c693c17f371be39d675dec50120f4d93402", + "parentHash": "0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703" + } + ], + "genesisBlockHeader": { + "BaseFeePerGas": "0x3b9aca00", + "BlobGasUsed": "0x0", + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0000000000000000000000000000000000000000", + "Difficulty": "0x20000", + "ExcessBlobGas": "0x0", + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0x0", + "Hash": "0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x0", + "ParentBeaconBlockRoot": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ParentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ReceiptTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "StateRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "Timestamp": "0x0", + "TransactionsTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" + }, + "lastblockhash": "273c5dc5beed8249b8103b9a168b1c693c17f371be39d675dec50120f4d93402", + "network": "Cancun", + "postState": {}, + "pre": {}, + "sealEngine": "" + } +} \ No newline at end of file From ee0bd810e3d1efbb068a07085aa112e47dce580d Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 10 Sep 2024 17:57:24 +0200 Subject: [PATCH 07/12] add selfdestruct test case --- eth/tracers/live/supply_fill_test.go | 124 +++++++----- .../live/testdata/supply/selfdestruct.json | 186 ++++++++++++++++++ 2 files changed, 262 insertions(+), 48 deletions(-) create mode 100644 eth/tracers/live/testdata/supply/selfdestruct.json diff --git a/eth/tracers/live/supply_fill_test.go b/eth/tracers/live/supply_fill_test.go index 4890eb6f441e..6bee77b50898 100644 --- a/eth/tracers/live/supply_fill_test.go +++ b/eth/tracers/live/supply_fill_test.go @@ -253,32 +253,27 @@ func TestSupplySelfdestruct(t *testing.T) { }, }, } - ) - - gspec.Config.TerminalTotalDifficulty = big.NewInt(0) - - signer := types.LatestSigner(gspec.Config) - - testBlockGenerationFunc := func(b *core.BlockGen) { - b.SetPoS() - - txdata := &types.LegacyTx{ - Nonce: 0, - To: &aa, - Value: gwei5, - Gas: 150000, - GasPrice: gwei5, - Data: []byte{}, + signer = types.LatestSigner(gspec.Config) + + testBlockGenerationFunc = func(b *core.BlockGen) { + txdata := &types.LegacyTx{ + Nonce: 0, + To: &aa, + Value: gwei5, + Gas: 150000, + GasPrice: gwei5, + Data: []byte{}, + } + + tx := types.NewTx(txdata) + tx, _ = types.SignTx(tx, signer, key1) + + b.AddTx(tx) } - - tx := types.NewTx(txdata) - tx, _ = types.SignTx(tx, signer, key1) - - b.AddTx(tx) - } + ) // 1. Test pre Cancun - preCancunOutput, _, preCancunChain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc) + preCancunOutput, preCancunDB, preCancunChain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc) if err != nil { t.Fatalf("Pre-cancun failed to test supply tracer: %v", err) } @@ -298,28 +293,49 @@ func TestSupplySelfdestruct(t *testing.T) { t.Fatalf("Pre-cancun address \"%v\" balance, got %v exp %v\n", bb, got, exp) } - head := preCancunChain.CurrentBlock() - // Check live trace output - expected := supplyInfo{ - Burn: &supplyInfoBurn{ - EIP1559: big.NewInt(55289500000000), - Misc: big.NewInt(5000000000), - }, - Number: 1, - Hash: head.Hash(), - ParentHash: head.ParentHash, - } - - actual := preCancunOutput[expected.Number] + var ( + head = preCancunChain.CurrentBlock() + // Check live trace output + expected = []supplyInfo{{ + Issuance: &supplyInfoIssuance{ + GenesisAlloc: new(big.Int).Mul(big.NewInt(2), big.NewInt(params.Ether)), + }, + Number: 0, + Hash: common.HexToHash("0xdd9fbe877f0b43987d2f0cda0df176b7939be14f33eb5137f16e6eddf4562706"), + ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), + }, { + Issuance: &supplyInfoIssuance{ + Reward: new(big.Int).Mul(big.NewInt(2), big.NewInt(params.Ether)), + }, + Burn: &supplyInfoBurn{ + EIP1559: big.NewInt(55289500000000), + Misc: big.NewInt(5000000000), + }, + Number: 1, + Hash: head.Hash(), + ParentHash: head.ParentHash, + }} + ) - compareAsJSON(t, expected, actual) + compareAsJSON(t, expected, preCancunOutput) + preCancunTest, err := btFromChain(preCancunDB, preCancunChain, nil) + if err != nil { + t.Fatalf("failed to fill tests from chain: %v", err) + } + preCancunTest.Expected = expected // 2. Test post Cancun cancunTime := uint64(0) + gspec.Config = params.MergedTestChainConfig gspec.Config.ShanghaiTime = &cancunTime gspec.Config.CancunTime = &cancunTime - - postCancunOutput, _, postCancunChain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc) + gspec.Config.TerminalTotalDifficulty = big.NewInt(0) + signer = types.LatestSigner(gspec.Config) + posTestBlockGenerationFunc := func(b *core.BlockGen) { + b.SetPoS() + testBlockGenerationFunc(b) + } + postCancunOutput, postCancunDB, postCancunChain, err := testSupplyTracer(t, gspec, posTestBlockGenerationFunc) if err != nil { t.Fatalf("Post-cancun failed to test supply tracer: %v", err) } @@ -341,18 +357,29 @@ func TestSupplySelfdestruct(t *testing.T) { // Check live trace output head = postCancunChain.CurrentBlock() - expected = supplyInfo{ + expected = []supplyInfo{{ + Issuance: &supplyInfoIssuance{ + GenesisAlloc: new(big.Int).Mul(big.NewInt(2), big.NewInt(params.Ether)), + }, + Number: 0, + Hash: common.HexToHash("0x16d2bb0b366d3963bf2d8d75cb4b3bc0f233047c948fa746cbd38ac82bf9cfe9"), + ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), + }, { Burn: &supplyInfoBurn{ EIP1559: big.NewInt(55289500000000), }, Number: 1, Hash: head.Hash(), ParentHash: head.ParentHash, - } - - actual = postCancunOutput[expected.Number] + }} - compareAsJSON(t, expected, actual) + compareAsJSON(t, expected, postCancunOutput) + postCancunTest, err := btFromChain(postCancunDB, postCancunChain, nil) + if err != nil { + t.Fatalf("failed to fill tests from chain: %v", err) + } + postCancunTest.Expected = expected + writeBTs(t, map[string]*BlockTest{"selfdestruct_grayGlacier": preCancunTest, "selfdestruct_cancun": postCancunTest}) } // Tests selfdestructing contract to send its balance to itself (burn). @@ -530,7 +557,6 @@ func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockG gen(b) }) - fmt.Printf("blocks: %v\n", blocks) if n, err := chain.InsertChain(blocks); err != nil { return nil, nil, chain, fmt.Errorf("block %d: failed to insert into chain: %v", n, err) } @@ -579,9 +605,11 @@ func writeArtifact(t *testing.T, name string, db ethdb.Database, chain *core.Blo t.Fatalf("failed to fill tests from chain: %v", err) } bt.Expected = expected - result := make(map[string]any) - result[name] = bt - enc, err := json.MarshalIndent(&result, "", " ") + writeBTs(t, map[string]*BlockTest{name: bt}) +} + +func writeBTs(t *testing.T, tests map[string]*BlockTest) { + enc, err := json.MarshalIndent(&tests, "", " ") if err != nil { t.Fatalf("failed to marshal tests: %v", err) } diff --git a/eth/tracers/live/testdata/supply/selfdestruct.json b/eth/tracers/live/testdata/supply/selfdestruct.json new file mode 100644 index 000000000000..717041be2d09 --- /dev/null +++ b/eth/tracers/live/testdata/supply/selfdestruct.json @@ -0,0 +1,186 @@ +{ + "selfdestruct_cancun": { + "blocks": [ + { + "BlockHeader": { + "BaseFeePerGas": "0x342770c0", + "BlobGasUsed": "0x0", + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0100000000000000000000000000000000000000", + "Difficulty": "0x0", + "ExcessBlobGas": "0x0", + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0xf6d4", + "Hash": "0xd5a0d7e4b5fa687dd355938a24e34229db3c01cd2a999e5023369016371a98ac", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x1", + "ParentBeaconBlockRoot": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ParentHash": "0x16d2bb0b366d3963bf2d8d75cb4b3bc0f233047c948fa746cbd38ac82bf9cfe9", + "ReceiptTrie": "0x6bd5a500652764abb50a6bfd441a2f52f9dc2a38644170adb7951804998e0176", + "StateRoot": "0x90899467081bda450d3ef7a6fb0f0c37ddfefea28c810a38323af55f79122c50", + "Timestamp": "0xa", + "TransactionsTrie": "0x8cfce1fa6bf3370d316650ab9ac77c2cfd69c9b3fd4e3def07b9e650769836e0", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" + }, + "ExpectException": "", + "Rlp": "0xf902aef9023ba016d2bb0b366d3963bf2d8d75cb4b3bc0f233047c948fa746cbd38ac82bf9cfe9a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940100000000000000000000000000000000000000a090899467081bda450d3ef7a6fb0f0c37ddfefea28c810a38323af55f79122c50a08cfce1fa6bf3370d316650ab9ac77c2cfd69c9b3fd4e3def07b9e650769836e0a06bd5a500652764abb50a6bfd441a2f52f9dc2a38644170adb7951804998e0176b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080018347e7c482f6d40a80a0000000000000000000000000000000000000000000000000000000000000000088000000000000000084342770c0a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b4218080a00000000000000000000000000000000000000000000000000000000000000000f86cf86a8085012a05f200830249f094111111111111111111111111111111111111111185012a05f2008026a03c8f16fd94b4c5e56c1c2ff249685a19b2afedb5d6529fc2950a79c3f9c8d599a01a7882a80e333850532a917631347435237f751c9f7a0b810aae8098f5a6225ec0c0", + "UncleHeaders": null + } + ], + "expected": [ + { + "issuance": { + "genesisAlloc": "0x1bc16d674ec80000" + }, + "blockNumber": 0, + "hash": "0x16d2bb0b366d3963bf2d8d75cb4b3bc0f233047c948fa746cbd38ac82bf9cfe9", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "burn": { + "1559": "0x32491701df00" + }, + "blockNumber": 1, + "hash": "0xd5a0d7e4b5fa687dd355938a24e34229db3c01cd2a999e5023369016371a98ac", + "parentHash": "0x16d2bb0b366d3963bf2d8d75cb4b3bc0f233047c948fa746cbd38ac82bf9cfe9" + } + ], + "genesisBlockHeader": { + "BaseFeePerGas": "0x3b9aca00", + "BlobGasUsed": "0x0", + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0000000000000000000000000000000000000000", + "Difficulty": "0x20000", + "ExcessBlobGas": "0x0", + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0x0", + "Hash": "0x16d2bb0b366d3963bf2d8d75cb4b3bc0f233047c948fa746cbd38ac82bf9cfe9", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x0", + "ParentBeaconBlockRoot": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ParentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ReceiptTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "StateRoot": "0xb4187a1038a0611c5ca076f45fdbcf3d741c3e126a57b966edd44f844323a72f", + "Timestamp": "0x0", + "TransactionsTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" + }, + "lastblockhash": "d5a0d7e4b5fa687dd355938a24e34229db3c01cd2a999e5023369016371a98ac", + "network": "Cancun", + "postState": {}, + "pre": { + "0x1111111111111111111111111111111111111111": { + "balance": "0x0", + "code": "0x61face60f01b6000527322222222222222222222222222222222222222226000806002600080855af160008103603457600080fd5b60008060008034865af1905060008103604c57600080fd5b5050" + }, + "0x2222222222222222222222222222222222222222": { + "balance": "0xde0b6b3a7640000", + "code": "0x6000357fface000000000000000000000000000000000000000000000000000000000000808203602f57610dad80ff5b5050" + }, + "0x71562b71999873db5b286df957af199ec94617f7": { + "balance": "0xde0b6b3a7640000" + } + }, + "sealEngine": "" + }, + "selfdestruct_grayGlacier": { + "blocks": [ + { + "BlockHeader": { + "BaseFeePerGas": "0x342770c0", + "BlobGasUsed": null, + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0100000000000000000000000000000000000000", + "Difficulty": "0x20000", + "ExcessBlobGas": null, + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0xf6d4", + "Hash": "0xf7f68f34d07d7acc5dad5ba9bbde99c82347a74471801e7c0c22e5b2ed6d0bc6", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x1", + "ParentBeaconBlockRoot": null, + "ParentHash": "0xdd9fbe877f0b43987d2f0cda0df176b7939be14f33eb5137f16e6eddf4562706", + "ReceiptTrie": "0x6bd5a500652764abb50a6bfd441a2f52f9dc2a38644170adb7951804998e0176", + "StateRoot": "0xbd0a2414c9584c14678caa39773fa17db8aab8667f897b251bb3fc025822e449", + "Timestamp": "0xa", + "TransactionsTrie": "0x8cfce1fa6bf3370d316650ab9ac77c2cfd69c9b3fd4e3def07b9e650769836e0", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": null + }, + "ExpectException": "", + "Rlp": "0xf9026cf901faa0dd9fbe877f0b43987d2f0cda0df176b7939be14f33eb5137f16e6eddf4562706a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940100000000000000000000000000000000000000a0bd0a2414c9584c14678caa39773fa17db8aab8667f897b251bb3fc025822e449a08cfce1fa6bf3370d316650ab9ac77c2cfd69c9b3fd4e3def07b9e650769836e0a06bd5a500652764abb50a6bfd441a2f52f9dc2a38644170adb7951804998e0176b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000018347e7c482f6d40a80a0000000000000000000000000000000000000000000000000000000000000000088000000000000000084342770c0f86cf86a8085012a05f200830249f094111111111111111111111111111111111111111185012a05f2008026a03c8f16fd94b4c5e56c1c2ff249685a19b2afedb5d6529fc2950a79c3f9c8d599a01a7882a80e333850532a917631347435237f751c9f7a0b810aae8098f5a6225ec0", + "UncleHeaders": null + } + ], + "expected": [ + { + "issuance": { + "genesisAlloc": "0x1bc16d674ec80000" + }, + "blockNumber": 0, + "hash": "0xdd9fbe877f0b43987d2f0cda0df176b7939be14f33eb5137f16e6eddf4562706", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "issuance": { + "reward": "0x1bc16d674ec80000" + }, + "burn": { + "1559": "0x32491701df00", + "misc": "0x12a05f200" + }, + "blockNumber": 1, + "hash": "0xf7f68f34d07d7acc5dad5ba9bbde99c82347a74471801e7c0c22e5b2ed6d0bc6", + "parentHash": "0xdd9fbe877f0b43987d2f0cda0df176b7939be14f33eb5137f16e6eddf4562706" + } + ], + "genesisBlockHeader": { + "BaseFeePerGas": "0x3b9aca00", + "BlobGasUsed": null, + "Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "Coinbase": "0x0000000000000000000000000000000000000000", + "Difficulty": "0x20000", + "ExcessBlobGas": null, + "ExtraData": "0x", + "GasLimit": "0x47e7c4", + "GasUsed": "0x0", + "Hash": "0xdd9fbe877f0b43987d2f0cda0df176b7939be14f33eb5137f16e6eddf4562706", + "MixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "Nonce": "0x0000000000000000", + "Number": "0x0", + "ParentBeaconBlockRoot": null, + "ParentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "ReceiptTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "StateRoot": "0xb4187a1038a0611c5ca076f45fdbcf3d741c3e126a57b966edd44f844323a72f", + "Timestamp": "0x0", + "TransactionsTrie": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "UncleHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "WithdrawalsRoot": null + }, + "lastblockhash": "f7f68f34d07d7acc5dad5ba9bbde99c82347a74471801e7c0c22e5b2ed6d0bc6", + "network": "GrayGlacier", + "postState": {}, + "pre": { + "0x1111111111111111111111111111111111111111": { + "balance": "0x0", + "code": "0x61face60f01b6000527322222222222222222222222222222222222222226000806002600080855af160008103603457600080fd5b60008060008034865af1905060008103604c57600080fd5b5050" + }, + "0x2222222222222222222222222222222222222222": { + "balance": "0xde0b6b3a7640000", + "code": "0x6000357fface000000000000000000000000000000000000000000000000000000000000808203602f57610dad80ff5b5050" + }, + "0x71562b71999873db5b286df957af199ec94617f7": { + "balance": "0xde0b6b3a7640000" + } + }, + "sealEngine": "" + } +} \ No newline at end of file From 4abb9c495c0be246ef41dbc5d3a26770d3d7ee86 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 10 Sep 2024 18:16:57 +0200 Subject: [PATCH 08/12] Add post state to selfdestruct test --- eth/tracers/live/supply_fill_test.go | 14 ++++++++-- .../live/testdata/supply/selfdestruct.json | 27 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/eth/tracers/live/supply_fill_test.go b/eth/tracers/live/supply_fill_test.go index 6bee77b50898..e689d2bd2869 100644 --- a/eth/tracers/live/supply_fill_test.go +++ b/eth/tracers/live/supply_fill_test.go @@ -315,10 +315,15 @@ func TestSupplySelfdestruct(t *testing.T) { Hash: head.Hash(), ParentHash: head.ParentHash, }} + post = &types.GenesisAlloc{ + dad: {Balance: eth1}, + aa: {Balance: big.NewInt(0), Code: gspec.Alloc[aa].Code}, + bb: {Balance: big.NewInt(0)}, + } ) compareAsJSON(t, expected, preCancunOutput) - preCancunTest, err := btFromChain(preCancunDB, preCancunChain, nil) + preCancunTest, err := btFromChain(preCancunDB, preCancunChain, post) if err != nil { t.Fatalf("failed to fill tests from chain: %v", err) } @@ -372,9 +377,14 @@ func TestSupplySelfdestruct(t *testing.T) { Hash: head.Hash(), ParentHash: head.ParentHash, }} + post = &types.GenesisAlloc{ + dad: {Balance: eth1}, + aa: {Balance: big.NewInt(0), Code: gspec.Alloc[aa].Code}, + bb: {Balance: gwei5, Code: gspec.Alloc[bb].Code}, + } compareAsJSON(t, expected, postCancunOutput) - postCancunTest, err := btFromChain(postCancunDB, postCancunChain, nil) + postCancunTest, err := btFromChain(postCancunDB, postCancunChain, post) if err != nil { t.Fatalf("failed to fill tests from chain: %v", err) } diff --git a/eth/tracers/live/testdata/supply/selfdestruct.json b/eth/tracers/live/testdata/supply/selfdestruct.json index 717041be2d09..ae3dff06c053 100644 --- a/eth/tracers/live/testdata/supply/selfdestruct.json +++ b/eth/tracers/live/testdata/supply/selfdestruct.json @@ -73,7 +73,19 @@ }, "lastblockhash": "d5a0d7e4b5fa687dd355938a24e34229db3c01cd2a999e5023369016371a98ac", "network": "Cancun", - "postState": {}, + "postState": { + "0x0000000000000000000000000000000000000dad": { + "balance": "0xde0b6b3a7640000" + }, + "0x1111111111111111111111111111111111111111": { + "balance": "0x0", + "code": "0x61face60f01b6000527322222222222222222222222222222222222222226000806002600080855af160008103603457600080fd5b60008060008034865af1905060008103604c57600080fd5b5050" + }, + "0x2222222222222222222222222222222222222222": { + "balance": "0x12a05f200", + "code": "0x6000357fface000000000000000000000000000000000000000000000000000000000000808203602f57610dad80ff5b5050" + } + }, "pre": { "0x1111111111111111111111111111111111111111": { "balance": "0x0", @@ -167,7 +179,18 @@ }, "lastblockhash": "f7f68f34d07d7acc5dad5ba9bbde99c82347a74471801e7c0c22e5b2ed6d0bc6", "network": "GrayGlacier", - "postState": {}, + "postState": { + "0x0000000000000000000000000000000000000dad": { + "balance": "0xde0b6b3a7640000" + }, + "0x1111111111111111111111111111111111111111": { + "balance": "0x0", + "code": "0x61face60f01b6000527322222222222222222222222222222222222222226000806002600080855af160008103603457600080fd5b60008060008034865af1905060008103604c57600080fd5b5050" + }, + "0x2222222222222222222222222222222222222222": { + "balance": "0x0" + } + }, "pre": { "0x1111111111111111111111111111111111111111": { "balance": "0x0", From 0853f3b890b1048c3ebb5cd5aca6acfabf89bbd1 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 17 Sep 2024 18:50:21 +0200 Subject: [PATCH 09/12] make filler an executable module --- eth/tracers/live/supply_test.go | 40 ++- eth/tracers/live/supply_test_util.go | 51 --- .../live/testdata/supply/omitted_fields.json | 24 +- .../supply_filler.go} | 290 +++++++++++++----- tests/block_test_util.go | 1 - 5 files changed, 265 insertions(+), 141 deletions(-) delete mode 100644 eth/tracers/live/supply_test_util.go rename eth/tracers/live/{supply_fill_test.go => testdata/supply_filler.go} (65%) diff --git a/eth/tracers/live/supply_test.go b/eth/tracers/live/supply_test.go index f7fa754d4483..4f4dd6e749f0 100644 --- a/eth/tracers/live/supply_test.go +++ b/eth/tracers/live/supply_test.go @@ -2,6 +2,7 @@ package live import ( "bufio" + "bytes" "encoding/json" "fmt" "os" @@ -10,8 +11,31 @@ import ( "strings" "testing" "unicode" + + "github.com/ethereum/go-ethereum/tests" ) +type blockTest struct { + bt *tests.BlockTest + Expected []supplyInfo `json:"expected"` +} + +func (bt *blockTest) UnmarshalJSON(data []byte) error { + tmp := make(map[string]json.RawMessage) + if err := json.Unmarshal(data, &tmp); err != nil { + return err + } + if err := json.Unmarshal(tmp["expected"], &bt.Expected); err != nil { + return err + } + if err := json.Unmarshal(data, &bt.bt); err != nil { + return err + } + return nil +} + +// The tests have been filled using the executable at +// eth/tracers/live/tracetest/supply_filler.go. func TestSupplyTracerBlockchain(t *testing.T) { dirPath := "supply" files, err := os.ReadDir(filepath.Join("testdata", dirPath)) @@ -23,7 +47,7 @@ func TestSupplyTracerBlockchain(t *testing.T) { continue } file := file // capture range variable - var testcases map[string]*BlockTest + var testcases map[string]*blockTest var blob []byte // Call tracer test found, read if from disk if blob, err = os.ReadFile(filepath.Join("testdata", dirPath, file.Name())); err != nil { @@ -85,3 +109,17 @@ func camel(str string) string { } return strings.Join(pieces, "") } + +func compareAsJSON(t *testing.T, expected interface{}, actual interface{}) { + want, err := json.Marshal(expected) + if err != nil { + t.Fatalf("failed to marshal expected value to JSON: %v", err) + } + have, err := json.Marshal(actual) + if err != nil { + t.Fatalf("failed to marshal actual value to JSON: %v", err) + } + if !bytes.Equal(want, have) { + t.Fatalf("incorrect supply info:\nexpected:\n%s\ngot:\n%s", string(want), string(have)) + } +} diff --git a/eth/tracers/live/supply_test_util.go b/eth/tracers/live/supply_test_util.go deleted file mode 100644 index a5387463a0c1..000000000000 --- a/eth/tracers/live/supply_test_util.go +++ /dev/null @@ -1,51 +0,0 @@ -package live - -import ( - "encoding/json" - - "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/tests" -) - -type BlockTest struct { - bt *tests.BlockTest - Expected []supplyInfo `json:"expected"` -} - -func btFromChain(db ethdb.Database, chain *core.BlockChain, post *types.GenesisAlloc) (*BlockTest, error) { - bt, err := tests.FromChain(db, chain, post) - if err != nil { - return nil, err - } - return &BlockTest{bt: &bt}, nil -} - -func (bt *BlockTest) UnmarshalJSON(data []byte) error { - tmp := make(map[string]json.RawMessage) - if err := json.Unmarshal(data, &tmp); err != nil { - return err - } - if err := json.Unmarshal(tmp["expected"], &bt.Expected); err != nil { - return err - } - if err := json.Unmarshal(data, &bt.bt); err != nil { - return err - } - return nil -} - -func (bt *BlockTest) MarshalJSON() ([]byte, error) { - enc, err := json.Marshal(bt.bt) - if err != nil { - return nil, err - } - // Insert the expected supply info - result := make(map[string]any) - if err := json.Unmarshal(enc, &result); err != nil { - return nil, err - } - result["expected"] = bt.Expected - return json.Marshal(result) -} diff --git a/eth/tracers/live/testdata/supply/omitted_fields.json b/eth/tracers/live/testdata/supply/omitted_fields.json index 2ffbfe81fdd2..e5d4a81ab053 100644 --- a/eth/tracers/live/testdata/supply/omitted_fields.json +++ b/eth/tracers/live/testdata/supply/omitted_fields.json @@ -1,17 +1,5 @@ { "omitted_fields_cancun": { - "expected": [ - { - "blockNumber": 0, - "hash": "0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703", - "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000" - }, - { - "blockNumber": 1, - "hash": "0xe430cdf604a88b9d713d4f89fd100ddddf38c1cc6b049e3d5df563c7bfd320fc", - "parentHash": "0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703" - } - ], "blocks": [ { "BlockHeader": { @@ -42,6 +30,18 @@ "UncleHeaders": null } ], + "expected": [ + { + "blockNumber": 0, + "hash": "0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000" + }, + { + "blockNumber": 1, + "hash": "0xe430cdf604a88b9d713d4f89fd100ddddf38c1cc6b049e3d5df563c7bfd320fc", + "parentHash": "0x52f276d96f0afaaf2c3cb358868bdc2779c4b0cb8de3e7e5302e247c0b66a703" + } + ], "genesisBlockHeader": { "BaseFeePerGas": "0x3b9aca00", "BlobGasUsed": "0x0", diff --git a/eth/tracers/live/supply_fill_test.go b/eth/tracers/live/testdata/supply_filler.go similarity index 65% rename from eth/tracers/live/supply_fill_test.go rename to eth/tracers/live/testdata/supply_filler.go index e689d2bd2869..e14380a61380 100644 --- a/eth/tracers/live/supply_fill_test.go +++ b/eth/tracers/live/testdata/supply_filler.go @@ -1,4 +1,4 @@ -// Copyright 2021 The go-ethereum Authors +// Copyright 2024 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 @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package live +package main import ( "bufio" @@ -25,9 +25,9 @@ import ( "os" "path" "path/filepath" - "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/consensus/beacon" "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/core" @@ -38,11 +38,80 @@ import ( "github.com/ethereum/go-ethereum/eth/tracers" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/tests" + + _ "github.com/ethereum/go-ethereum/eth/tracers/live" ) +type supplyInfoIssuance struct { + GenesisAlloc *hexutil.Big `json:"genesisAlloc,omitempty"` + Reward *hexutil.Big `json:"reward,omitempty"` + Withdrawals *hexutil.Big `json:"withdrawals,omitempty"` +} + +type supplyInfoBurn struct { + EIP1559 *hexutil.Big `json:"1559,omitempty"` + Blob *hexutil.Big `json:"blob,omitempty"` + Misc *hexutil.Big `json:"misc,omitempty"` +} + +type supplyInfo struct { + Issuance *supplyInfoIssuance `json:"issuance,omitempty"` + Burn *supplyInfoBurn `json:"burn,omitempty"` + + // Block info + Number uint64 `json:"blockNumber"` + Hash common.Hash `json:"hash"` + ParentHash common.Hash `json:"parentHash"` +} + +func main() { + // Takes a path where the filled tests will be written. + if len(os.Args) < 2 { + fmt.Println("Please provide a path as a command-line argument") + os.Exit(1) + } + + path, err := filepath.Abs(os.Args[1]) + if err != nil { + fmt.Printf("Error resolving path: %v\n", err) + os.Exit(1) + } + + // Create all directories in the path if they don't exist + if err := os.MkdirAll(path, 0755); err != nil { + fmt.Printf("failed to create directory: %v\n", err) + os.Exit(1) + } + if err := fillSupplyOmittedFields(path); err != nil { + fmt.Printf("fillSupplyOmittedFields failed: %v\n", err) + os.Exit(1) + } + if err := fillSupplyGenesisAlloc(path); err != nil { + fmt.Printf("fillSupplyGenesisAlloc failed: %v\n", err) + os.Exit(1) + } + if err := fillSupplyEip1559Burn(path); err != nil { + fmt.Printf("fillSupplyEip1559Burn failed: %v\n") + os.Exit(1) + } + if err := fillSupplyWithdrawals(path); err != nil { + fmt.Printf("fillSupplyWithdrawals failed: %v\n", err) + os.Exit(1) + } + if err := fillSupplySelfdestruct(path); err != nil { + fmt.Printf("fillSupplySelfdestruct failed: %v\n", err) + os.Exit(1) + } + if err := fillSupplySelfdestructItselfAndRevert(path); err != nil { + fmt.Printf("fillSupplySelfdestructItselfAndRevert failed: %v\n", err) + os.Exit(1) + } +} + func emptyBlockGenerationFunc(b *core.BlockGen) {} -func TestSupplyOmittedFields(t *testing.T) { +func fillSupplyOmittedFields(path string) error { var ( config = *params.MergedTestChainConfig gspec = &core.Genesis{ @@ -59,17 +128,22 @@ func TestSupplyOmittedFields(t *testing.T) { }} ) gspec.Config.TerminalTotalDifficulty = big.NewInt(0) - out, db, chain, err := testSupplyTracer(t, gspec, func(b *core.BlockGen) { + out, db, chain, err := testSupplyTracer(gspec, func(b *core.BlockGen) { b.SetPoS() }) if err != nil { - t.Fatalf("failed to test supply tracer: %v", err) + return fmt.Errorf("failed to test supply tracer: %v", err) } - compareAsJSON(t, expected, out) - writeArtifact(t, "omitted_fields_cancun", db, chain, expected, nil) + if err := compareAsJSON(expected, out); err != nil { + return err + } + if err := writeArtifact(filepath.Join(path, "omitted_fields.json"), "omitted_fields_cancun", db, chain, expected, nil); err != nil { + return err + } + return nil } -func TestSupplyGenesisAlloc(t *testing.T) { +func fillSupplyGenesisAlloc(path string) error { var ( key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") @@ -87,14 +161,14 @@ func TestSupplyGenesisAlloc(t *testing.T) { } expected = []supplyInfo{{ Issuance: &supplyInfoIssuance{ - GenesisAlloc: new(big.Int).Mul(common.Big2, big.NewInt(params.Ether)), + GenesisAlloc: (*hexutil.Big)(new(big.Int).Mul(common.Big2, big.NewInt(params.Ether))), }, Number: 0, Hash: common.HexToHash("0xbcc9466e9fc6a8b56f4b29ca353a421ff8b51a0c1a58ca4743b427605b08f2ca"), ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), }, { Issuance: &supplyInfoIssuance{ - Reward: new(big.Int).Mul(common.Big2, big.NewInt(params.Ether)), + Reward: (*hexutil.Big)(new(big.Int).Mul(common.Big2, big.NewInt(params.Ether))), }, Number: 1, Hash: common.HexToHash("0x37bb7e9b45f4fb7b311abb5f815e3e00d3382d83a2c39b9b0bd22b717566cd04"), @@ -102,15 +176,20 @@ func TestSupplyGenesisAlloc(t *testing.T) { }} ) - out, db, chain, err := testSupplyTracer(t, gspec, emptyBlockGenerationFunc) + out, db, chain, err := testSupplyTracer(gspec, emptyBlockGenerationFunc) if err != nil { - t.Fatalf("failed to test supply tracer: %v", err) + return fmt.Errorf("failed to test supply tracer: %v", err) + } + if err := compareAsJSON(expected, out); err != nil { + return err } - compareAsJSON(t, expected, out) - writeArtifact(t, "genesis_alloc_grayGlacier", db, chain, expected, nil) + if err := writeArtifact(filepath.Join(path, "genesis_alloc.json"), "genesis_alloc_grayGlacier", db, chain, expected, nil); err != nil { + return err + } + return nil } -func TestSupplyEip1559Burn(t *testing.T) { +func fillSupplyEip1559Burn(path string) error { var ( config = *params.AllEthashProtocolChanges @@ -146,9 +225,9 @@ func TestSupplyEip1559Burn(t *testing.T) { b.AddTx(tx) } - out, db, chain, err := testSupplyTracer(t, gspec, eip1559BlockGenerationFunc) + out, db, chain, err := testSupplyTracer(gspec, eip1559BlockGenerationFunc) if err != nil { - t.Fatalf("failed to test supply tracer: %v", err) + return fmt.Errorf("failed to test supply tracer: %v", err) } var ( head = chain.CurrentBlock() @@ -156,28 +235,33 @@ func TestSupplyEip1559Burn(t *testing.T) { burn = new(big.Int).Mul(big.NewInt(21000), head.BaseFee) expected = []supplyInfo{{ Issuance: &supplyInfoIssuance{ - GenesisAlloc: eth1, + GenesisAlloc: (*hexutil.Big)(eth1), }, Number: 0, Hash: common.HexToHash("0xc4265421181cafc43e4b97ae4f21530e37e00320f219a13311482c9c552bcdc7"), ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), }, { Issuance: &supplyInfoIssuance{ - Reward: reward, + Reward: (*hexutil.Big)(reward), }, Burn: &supplyInfoBurn{ - EIP1559: burn, + EIP1559: (*hexutil.Big)(burn), }, Number: 1, Hash: head.Hash(), ParentHash: head.ParentHash, }} ) - compareAsJSON(t, expected, out) - writeArtifact(t, "eip1559_burn_grayGlacier", db, chain, expected, nil) + if err := compareAsJSON(expected, out); err != nil { + return err + } + if err := writeArtifact(filepath.Join(path, "eip1559_burn.json"), "eip1559_burn_grayGlacier", db, chain, expected, nil); err != nil { + return err + } + return nil } -func TestSupplyWithdrawals(t *testing.T) { +func fillSupplyWithdrawals(path string) error { var ( config = *params.MergedTestChainConfig gspec = &core.Genesis{ @@ -195,9 +279,9 @@ func TestSupplyWithdrawals(t *testing.T) { }) } - out, db, chain, err := testSupplyTracer(t, gspec, withdrawalsBlockGenerationFunc) + out, db, chain, err := testSupplyTracer(gspec, withdrawalsBlockGenerationFunc) if err != nil { - t.Fatalf("failed to test supply tracer: %v", err) + return fmt.Errorf("failed to test supply tracer: %v", err) } var ( @@ -208,15 +292,20 @@ func TestSupplyWithdrawals(t *testing.T) { ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), }, { Issuance: &supplyInfoIssuance{ - Withdrawals: big.NewInt(1337000000000), + Withdrawals: (*hexutil.Big)(big.NewInt(1337000000000)), }, Number: 1, Hash: head.Hash(), ParentHash: head.ParentHash, }} ) - compareAsJSON(t, expected, out) - writeArtifact(t, "withdrawals_cancun", db, chain, expected, nil) + if err := compareAsJSON(expected, out); err != nil { + return err + } + if err := writeArtifact(filepath.Join(path, "withdrawals.json"), "withdrawals_cancun", db, chain, expected, nil); err != nil { + return err + } + return nil } // Tests fund retrieval after contract's selfdestruct. @@ -224,7 +313,7 @@ func TestSupplyWithdrawals(t *testing.T) { // after the selfdestruct opcode executes from Contract A. // Because Contract B is removed only at the end of the transaction // the ether sent in between is burnt before Cancun hard fork. -func TestSupplySelfdestruct(t *testing.T) { +func fillSupplySelfdestruct(path string) error { var ( config = *params.TestChainConfig @@ -273,9 +362,9 @@ func TestSupplySelfdestruct(t *testing.T) { ) // 1. Test pre Cancun - preCancunOutput, preCancunDB, preCancunChain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc) + preCancunOutput, preCancunDB, preCancunChain, err := testSupplyTracer(gspec, testBlockGenerationFunc) if err != nil { - t.Fatalf("Pre-cancun failed to test supply tracer: %v", err) + return fmt.Errorf("failed to test supply tracer: %v", err) } // Check balance at state: @@ -284,13 +373,13 @@ func TestSupplySelfdestruct(t *testing.T) { // 3. B has 0 ether statedb, _ := preCancunChain.State() if got, exp := statedb.GetBalance(dad), eth1; got.CmpBig(exp) != 0 { - t.Fatalf("Pre-cancun address \"%v\" balance, got %v exp %v\n", dad, got, exp) + return fmt.Errorf("Pre-cancun address \"%v\" balance, got %v exp %v\n", dad, got, exp) } if got, exp := statedb.GetBalance(aa), big.NewInt(0); got.CmpBig(exp) != 0 { - t.Fatalf("Pre-cancun address \"%v\" balance, got %v exp %v\n", aa, got, exp) + return fmt.Errorf("Pre-cancun address \"%v\" balance, got %v exp %v\n", aa, got, exp) } if got, exp := statedb.GetBalance(bb), big.NewInt(0); got.CmpBig(exp) != 0 { - t.Fatalf("Pre-cancun address \"%v\" balance, got %v exp %v\n", bb, got, exp) + return fmt.Errorf("Pre-cancun address \"%v\" balance, got %v exp %v\n", bb, got, exp) } var ( @@ -298,18 +387,18 @@ func TestSupplySelfdestruct(t *testing.T) { // Check live trace output expected = []supplyInfo{{ Issuance: &supplyInfoIssuance{ - GenesisAlloc: new(big.Int).Mul(big.NewInt(2), big.NewInt(params.Ether)), + GenesisAlloc: (*hexutil.Big)(new(big.Int).Mul(big.NewInt(2), big.NewInt(params.Ether))), }, Number: 0, Hash: common.HexToHash("0xdd9fbe877f0b43987d2f0cda0df176b7939be14f33eb5137f16e6eddf4562706"), ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), }, { Issuance: &supplyInfoIssuance{ - Reward: new(big.Int).Mul(big.NewInt(2), big.NewInt(params.Ether)), + Reward: (*hexutil.Big)(new(big.Int).Mul(big.NewInt(2), big.NewInt(params.Ether))), }, Burn: &supplyInfoBurn{ - EIP1559: big.NewInt(55289500000000), - Misc: big.NewInt(5000000000), + EIP1559: (*hexutil.Big)(big.NewInt(55289500000000)), + Misc: (*hexutil.Big)(big.NewInt(5000000000)), }, Number: 1, Hash: head.Hash(), @@ -322,10 +411,12 @@ func TestSupplySelfdestruct(t *testing.T) { } ) - compareAsJSON(t, expected, preCancunOutput) + if err := compareAsJSON(expected, preCancunOutput); err != nil { + return err + } preCancunTest, err := btFromChain(preCancunDB, preCancunChain, post) if err != nil { - t.Fatalf("failed to fill tests from chain: %v", err) + return fmt.Errorf("failed to fill tests from chain: %v", err) } preCancunTest.Expected = expected @@ -340,9 +431,9 @@ func TestSupplySelfdestruct(t *testing.T) { b.SetPoS() testBlockGenerationFunc(b) } - postCancunOutput, postCancunDB, postCancunChain, err := testSupplyTracer(t, gspec, posTestBlockGenerationFunc) + postCancunOutput, postCancunDB, postCancunChain, err := testSupplyTracer(gspec, posTestBlockGenerationFunc) if err != nil { - t.Fatalf("Post-cancun failed to test supply tracer: %v", err) + return fmt.Errorf("Post-cancun failed to test supply tracer: %v", err) } // Check balance at state: @@ -351,27 +442,27 @@ func TestSupplySelfdestruct(t *testing.T) { // 3. B has 5 gwei statedb, _ = postCancunChain.State() if got, exp := statedb.GetBalance(dad), eth1; got.CmpBig(exp) != 0 { - t.Fatalf("Post-shanghai address \"%v\" balance, got %v exp %v\n", dad, got, exp) + return fmt.Errorf("Post-shanghai address \"%v\" balance, got %v exp %v\n", dad, got, exp) } if got, exp := statedb.GetBalance(aa), big.NewInt(0); got.CmpBig(exp) != 0 { - t.Fatalf("Post-shanghai address \"%v\" balance, got %v exp %v\n", aa, got, exp) + return fmt.Errorf("Post-shanghai address \"%v\" balance, got %v exp %v\n", aa, got, exp) } if got, exp := statedb.GetBalance(bb), gwei5; got.CmpBig(exp) != 0 { - t.Fatalf("Post-shanghai address \"%v\" balance, got %v exp %v\n", bb, got, exp) + return fmt.Errorf("Post-shanghai address \"%v\" balance, got %v exp %v\n", bb, got, exp) } // Check live trace output head = postCancunChain.CurrentBlock() expected = []supplyInfo{{ Issuance: &supplyInfoIssuance{ - GenesisAlloc: new(big.Int).Mul(big.NewInt(2), big.NewInt(params.Ether)), + GenesisAlloc: (*hexutil.Big)(new(big.Int).Mul(big.NewInt(2), big.NewInt(params.Ether))), }, Number: 0, Hash: common.HexToHash("0x16d2bb0b366d3963bf2d8d75cb4b3bc0f233047c948fa746cbd38ac82bf9cfe9"), ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), }, { Burn: &supplyInfoBurn{ - EIP1559: big.NewInt(55289500000000), + EIP1559: (*hexutil.Big)(big.NewInt(55289500000000)), }, Number: 1, Hash: head.Hash(), @@ -383,13 +474,18 @@ func TestSupplySelfdestruct(t *testing.T) { bb: {Balance: gwei5, Code: gspec.Alloc[bb].Code}, } - compareAsJSON(t, expected, postCancunOutput) + if err := compareAsJSON(expected, postCancunOutput); err != nil { + return err + } postCancunTest, err := btFromChain(postCancunDB, postCancunChain, post) if err != nil { - t.Fatalf("failed to fill tests from chain: %v", err) + return fmt.Errorf("failed to fill tests from chain: %v", err) } postCancunTest.Expected = expected - writeBTs(t, map[string]*BlockTest{"selfdestruct_grayGlacier": preCancunTest, "selfdestruct_cancun": postCancunTest}) + if err := writeBTs(filepath.Join(path, "selfdestruct.json"), map[string]*blockTest{"selfdestruct_grayGlacier": preCancunTest, "selfdestruct_cancun": postCancunTest}); err != nil { + return err + } + return nil } // Tests selfdestructing contract to send its balance to itself (burn). @@ -399,7 +495,7 @@ func TestSupplySelfdestruct(t *testing.T) { // - Contract C selfdestructs and sends the eth1 to itself. // - Contract D calls C and reverts (Burn amount of C // has to be reverted as well). -func TestSupplySelfdestructItselfAndRevert(t *testing.T) { +func fillSupplySelfdestructItselfAndRevert(path string) error { var ( config = *params.TestChainConfig @@ -491,9 +587,9 @@ func TestSupplySelfdestructItselfAndRevert(t *testing.T) { b.AddTx(tx) } - output, db, chain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc) + output, db, chain, err := testSupplyTracer(gspec, testBlockGenerationFunc) if err != nil { - t.Fatalf("failed to test supply tracer: %v", err) + return fmt.Errorf("failed to test supply tracer: %v", err) } // Check balance at state: @@ -503,50 +599,61 @@ func TestSupplySelfdestructItselfAndRevert(t *testing.T) { // 4. D has 1 ether, reverted statedb, _ := chain.State() if got, exp := statedb.GetBalance(aa), common.Big0; got.CmpBig(exp) != 0 { - t.Fatalf("address \"%v\" balance, got %v exp %v\n", aa, got, exp) + return fmt.Errorf("address \"%v\" balance, got %v exp %v\n", aa, got, exp) } if got, exp := statedb.GetBalance(bb), common.Big0; got.CmpBig(exp) != 0 { - t.Fatalf("address \"%v\" balance, got %v exp %v\n", bb, got, exp) + return fmt.Errorf("address \"%v\" balance, got %v exp %v\n", bb, got, exp) } if got, exp := statedb.GetBalance(cc), eth1; got.CmpBig(exp) != 0 { - t.Fatalf("address \"%v\" balance, got %v exp %v\n", bb, got, exp) + return fmt.Errorf("address \"%v\" balance, got %v exp %v\n", cc, got, exp) } if got, exp := statedb.GetBalance(dd), eth2; got.CmpBig(exp) != 0 { - t.Fatalf("address \"%v\" balance, got %v exp %v\n", bb, got, exp) + return fmt.Errorf("address \"%v\" balance, got %v exp %v\n", dd, got, exp) } // Check live trace output block := chain.GetBlockByNumber(1) expected := []supplyInfo{{ Issuance: &supplyInfoIssuance{ - GenesisAlloc: new(big.Int).Mul(big.NewInt(9), big.NewInt(params.Ether)), + GenesisAlloc: (*hexutil.Big)(new(big.Int).Mul(big.NewInt(9), big.NewInt(params.Ether))), }, Number: 0, Hash: common.HexToHash("0xaf41e72f748de317965454508c749f7e14dc4fe444cd07bca4c981c7e952364d"), ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), }, { Burn: &supplyInfoBurn{ - EIP1559: new(big.Int).Mul(block.BaseFee(), big.NewInt(int64(block.GasUsed()))), - Misc: eth5, // 5ETH burned from contract B + EIP1559: (*hexutil.Big)(new(big.Int).Mul(block.BaseFee(), big.NewInt(int64(block.GasUsed())))), + Misc: (*hexutil.Big)(eth5), // 5ETH burned from contract B }, Issuance: &supplyInfoIssuance{ - Reward: eth2, + Reward: (*hexutil.Big)(eth2), }, Number: 1, Hash: block.Hash(), ParentHash: block.ParentHash(), }} - compareAsJSON(t, expected, output) - writeArtifact(t, "selfdestruct_itself_and_revert_grayGlacier", db, chain, expected, nil) + if err := compareAsJSON(expected, output); err != nil { + return err + } + if err := writeArtifact(filepath.Join(path, "selfdestruct_itself_and_revert.json"), "selfdestruct_itself_and_revert_grayGlacier", db, chain, expected, nil); err != nil { + return err + } + return nil } -func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockGen)) ([]supplyInfo, ethdb.Database, *core.BlockChain, error) { +func testSupplyTracer(genesis *core.Genesis, gen func(*core.BlockGen)) ([]supplyInfo, ethdb.Database, *core.BlockChain, error) { var ( engine = beacon.New(ethash.NewFaker()) ) - traceOutputPath := filepath.ToSlash(t.TempDir()) + tempDir, err := os.MkdirTemp("", "supply-filler-") + if err != nil { + return nil, nil, nil, fmt.Errorf("failed to generate directory for tracer outputs: %v", err) + } + defer os.RemoveAll(tempDir) // Clean up + + traceOutputPath := filepath.ToSlash(tempDir) traceOutputFilename := path.Join(traceOutputPath, "supply.jsonl") // Load supply tracer @@ -595,33 +702,64 @@ func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockG return output, db, chain, nil } -func compareAsJSON(t *testing.T, expected interface{}, actual interface{}) { +func compareAsJSON(expected interface{}, actual interface{}) error { want, err := json.Marshal(expected) if err != nil { - t.Fatalf("failed to marshal expected value to JSON: %v", err) + return fmt.Errorf("failed to marshal expected value to JSON: %v", err) } have, err := json.Marshal(actual) if err != nil { - t.Fatalf("failed to marshal actual value to JSON: %v", err) + return fmt.Errorf("failed to marshal actual value to JSON: %v", err) } if !bytes.Equal(want, have) { - t.Fatalf("incorrect supply info:\nexpected:\n%s\ngot:\n%s", string(want), string(have)) + return fmt.Errorf("incorrect supply info:\nexpected:\n%s\ngot:\n%s", string(want), string(have)) } + return nil } -func writeArtifact(t *testing.T, name string, db ethdb.Database, chain *core.BlockChain, expected []supplyInfo, post *types.GenesisAlloc) { +func writeArtifact(path, name string, db ethdb.Database, chain *core.BlockChain, expected []supplyInfo, post *types.GenesisAlloc) error { bt, err := btFromChain(db, chain, post) if err != nil { - t.Fatalf("failed to fill tests from chain: %v", err) + return fmt.Errorf("failed to fill tests from chain: %v", err) } bt.Expected = expected - writeBTs(t, map[string]*BlockTest{name: bt}) + return writeBTs(path, map[string]*blockTest{name: bt}) } -func writeBTs(t *testing.T, tests map[string]*BlockTest) { +type blockTest struct { + bt *tests.BlockTest + Expected []supplyInfo `json:"expected"` +} + +func writeBTs(path string, tests map[string]*blockTest) error { enc, err := json.MarshalIndent(&tests, "", " ") if err != nil { - t.Fatalf("failed to marshal tests: %v", err) + return fmt.Errorf("failed to marshal tests: %v", err) + } + if err := os.WriteFile(path, enc, 0644); err != nil { + return fmt.Errorf("failed to write test to file: %v", err) + } + return nil +} + +func btFromChain(db ethdb.Database, chain *core.BlockChain, post *types.GenesisAlloc) (*blockTest, error) { + bt, err := tests.FromChain(db, chain, post) + if err != nil { + return nil, err + } + return &blockTest{bt: &bt}, nil +} + +func (bt *blockTest) MarshalJSON() ([]byte, error) { + enc, err := json.Marshal(bt.bt) + if err != nil { + return nil, err + } + // Insert the expected supply info + result := make(map[string]any) + if err := json.Unmarshal(enc, &result); err != nil { + return nil, err } - t.Logf("Tests: %s", enc) + result["expected"] = bt.Expected + return json.Marshal(result) } diff --git a/tests/block_test_util.go b/tests/block_test_util.go index 58fbab9a3a30..5b8ba4610ea2 100644 --- a/tests/block_test_util.go +++ b/tests/block_test_util.go @@ -71,7 +71,6 @@ func FromChain(db ethdb.Database, chain *core.BlockChain, post *types.GenesisAll ) for i := 1; i <= int(head.Number.Uint64()); i++ { block := chain.GetBlockByNumber(uint64(i)) - fmt.Printf("block: %v\n", block) if block == nil { return bt, fmt.Errorf("block %d not found", i) } From 5e6e1f2b97e4aa4630242fd067a9380a44c05748 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 17 Sep 2024 19:06:00 +0200 Subject: [PATCH 10/12] rename dir to tests --- eth/tracers/live/supply_test.go | 12 ++++++------ .../{testdata => tests}/supply/eip1559_burn.json | 0 .../{testdata => tests}/supply/genesis_alloc.json | 0 .../{testdata => tests}/supply/omitted_fields.json | 0 .../{testdata => tests}/supply/selfdestruct.json | 0 .../supply/selfdestruct_itself_and_revert.json | 0 .../live/{testdata => tests}/supply/withdrawals.json | 0 .../live/{testdata => tests}/supply_filler.go | 2 +- 8 files changed, 7 insertions(+), 7 deletions(-) rename eth/tracers/live/{testdata => tests}/supply/eip1559_burn.json (100%) rename eth/tracers/live/{testdata => tests}/supply/genesis_alloc.json (100%) rename eth/tracers/live/{testdata => tests}/supply/omitted_fields.json (100%) rename eth/tracers/live/{testdata => tests}/supply/selfdestruct.json (100%) rename eth/tracers/live/{testdata => tests}/supply/selfdestruct_itself_and_revert.json (100%) rename eth/tracers/live/{testdata => tests}/supply/withdrawals.json (100%) rename eth/tracers/live/{testdata => tests}/supply_filler.go (99%) diff --git a/eth/tracers/live/supply_test.go b/eth/tracers/live/supply_test.go index 4f4dd6e749f0..b6bbb292ec62 100644 --- a/eth/tracers/live/supply_test.go +++ b/eth/tracers/live/supply_test.go @@ -35,10 +35,10 @@ func (bt *blockTest) UnmarshalJSON(data []byte) error { } // The tests have been filled using the executable at -// eth/tracers/live/tracetest/supply_filler.go. +// eth/tracers/live/tests/supply_filler.go. func TestSupplyTracerBlockchain(t *testing.T) { - dirPath := "supply" - files, err := os.ReadDir(filepath.Join("testdata", dirPath)) + dirPath := filepath.Join("tests", "supply") + files, err := os.ReadDir(dirPath) if err != nil { t.Fatalf("failed to retrieve tracer test suite: %v", err) } @@ -49,8 +49,8 @@ func TestSupplyTracerBlockchain(t *testing.T) { file := file // capture range variable var testcases map[string]*blockTest var blob []byte - // Call tracer test found, read if from disk - if blob, err = os.ReadFile(filepath.Join("testdata", dirPath, file.Name())); err != nil { + // Tracer test found, read if from disk + if blob, err = os.ReadFile(filepath.Join(dirPath, file.Name())); err != nil { t.Fatalf("failed to read testcase: %v", err) } if err := json.Unmarshal(blob, &testcases); err != nil { @@ -65,7 +65,7 @@ func TestSupplyTracerBlockchain(t *testing.T) { // Load supply tracer tracer, err := newSupply(json.RawMessage(fmt.Sprintf(`{"path":"%s"}`, traceOutputPath))) if err != nil { - t.Fatalf("failed to create call tracer: %v", err) + t.Fatalf("failed to create tracer: %v", err) } if err := test.bt.Run(false, "path", false, tracer, nil); err != nil { t.Errorf("failed to run test: %v\n", err) diff --git a/eth/tracers/live/testdata/supply/eip1559_burn.json b/eth/tracers/live/tests/supply/eip1559_burn.json similarity index 100% rename from eth/tracers/live/testdata/supply/eip1559_burn.json rename to eth/tracers/live/tests/supply/eip1559_burn.json diff --git a/eth/tracers/live/testdata/supply/genesis_alloc.json b/eth/tracers/live/tests/supply/genesis_alloc.json similarity index 100% rename from eth/tracers/live/testdata/supply/genesis_alloc.json rename to eth/tracers/live/tests/supply/genesis_alloc.json diff --git a/eth/tracers/live/testdata/supply/omitted_fields.json b/eth/tracers/live/tests/supply/omitted_fields.json similarity index 100% rename from eth/tracers/live/testdata/supply/omitted_fields.json rename to eth/tracers/live/tests/supply/omitted_fields.json diff --git a/eth/tracers/live/testdata/supply/selfdestruct.json b/eth/tracers/live/tests/supply/selfdestruct.json similarity index 100% rename from eth/tracers/live/testdata/supply/selfdestruct.json rename to eth/tracers/live/tests/supply/selfdestruct.json diff --git a/eth/tracers/live/testdata/supply/selfdestruct_itself_and_revert.json b/eth/tracers/live/tests/supply/selfdestruct_itself_and_revert.json similarity index 100% rename from eth/tracers/live/testdata/supply/selfdestruct_itself_and_revert.json rename to eth/tracers/live/tests/supply/selfdestruct_itself_and_revert.json diff --git a/eth/tracers/live/testdata/supply/withdrawals.json b/eth/tracers/live/tests/supply/withdrawals.json similarity index 100% rename from eth/tracers/live/testdata/supply/withdrawals.json rename to eth/tracers/live/tests/supply/withdrawals.json diff --git a/eth/tracers/live/testdata/supply_filler.go b/eth/tracers/live/tests/supply_filler.go similarity index 99% rename from eth/tracers/live/testdata/supply_filler.go rename to eth/tracers/live/tests/supply_filler.go index e14380a61380..496e23d01e91 100644 --- a/eth/tracers/live/testdata/supply_filler.go +++ b/eth/tracers/live/tests/supply_filler.go @@ -659,7 +659,7 @@ func testSupplyTracer(genesis *core.Genesis, gen func(*core.BlockGen)) ([]supply // Load supply tracer tracer, err := tracers.LiveDirectory.New("supply", json.RawMessage(fmt.Sprintf(`{"path":"%s"}`, traceOutputPath))) if err != nil { - return nil, nil, nil, fmt.Errorf("failed to create call tracer: %v", err) + return nil, nil, nil, fmt.Errorf("failed to create tracer: %v", err) } db := rawdb.NewMemoryDatabase() From 358c674c704e73ad3651acf966fc5c49777e943c Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 17 Sep 2024 19:36:33 +0200 Subject: [PATCH 11/12] rm unused geth_fork api method --- internal/ethapi/api.go | 19 ------------------- internal/ethapi/backend.go | 3 --- internal/web3ext/web3ext.go | 15 --------------- 3 files changed, 37 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index a127f4f2bed4..1c3cb4adf936 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -2164,25 +2164,6 @@ func (api *NetAPI) Version() string { return fmt.Sprintf("%d", api.networkVersion) } -// GethAPI offers geth-specific API methods. -type GethAPI struct { - b Backend -} - -// NewGethAPI creates a new net API instance. -func NewGethAPI(b Backend) *GethAPI { - return &GethAPI{b: b} -} - -// Fork returns the latest enabled fork as of the given block. -func (api *GethAPI) Fork(ctx context.Context, num rpc.BlockNumber) (string, error) { - b, err := api.b.BlockByNumber(ctx, num) - if err != nil { - return "", fmt.Errorf("block #%d not found", num) - } - return api.b.ChainConfig().LatestFork(b.Number(), b.Time()).String(), nil -} - // checkTxFee is an internal function used to check whether the fee of // the given transaction is _reasonable_(under the cap). func checkTxFee(gasPrice *big.Int, gas uint64, cap float64) error { diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index 55cbe6b67c8b..2a45ba09210f 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -123,9 +123,6 @@ func GetAPIs(apiBackend Backend) []rpc.API { }, { Namespace: "personal", Service: NewPersonalAccountAPI(apiBackend, nonceLock), - }, { - Namespace: "geth", - Service: NewGethAPI(apiBackend), }, } } diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index a9e3b45f3247..4a1a37d722d6 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -31,7 +31,6 @@ var Modules = map[string]string{ "les": LESJs, "vflux": VfluxJs, "dev": DevJs, - "geth": GethJs, } const CliqueJs = ` @@ -888,17 +887,3 @@ web3._extend({ ], }); ` - -const GethJs = ` -web3._extend({ - property: 'geth', - methods: - [ - new web3._extend.Method({ - name: 'fork', - call: 'geth_fork', - params: 1 - }), - ], -}); -` From e6463e2b9fe0c078e41416c3d8f39188dc2d78ce Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 17 Sep 2024 19:41:58 +0200 Subject: [PATCH 12/12] add license --- eth/tracers/live/supply_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/eth/tracers/live/supply_test.go b/eth/tracers/live/supply_test.go index b6bbb292ec62..b06c38db307e 100644 --- a/eth/tracers/live/supply_test.go +++ b/eth/tracers/live/supply_test.go @@ -1,3 +1,19 @@ +// Copyright 2024 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 . + package live import (