From 405ceae2a1cb48fb000525e3773ec9887f7c1b4c Mon Sep 17 00:00:00 2001 From: Chris Ziogas Date: Fri, 31 May 2024 15:02:00 +0300 Subject: [PATCH] keep logic of removing empty fields in a single place for tech debt --- eth/tracers/internal/tracetest/supply_test.go | 27 +++++++++++++++++++ eth/tracers/live/supply.go | 27 +++++++++++++------ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/eth/tracers/internal/tracetest/supply_test.go b/eth/tracers/internal/tracetest/supply_test.go index de1a9990a0..2d4f1b0890 100644 --- a/eth/tracers/internal/tracetest/supply_test.go +++ b/eth/tracers/internal/tracetest/supply_test.go @@ -67,6 +67,33 @@ type supplyInfo struct { func emptyBlockGenerationFunc(b *core.BlockGen) {} +func TestSupplyOmittedFields(t *testing.T) { + var ( + config = *params.MergedTestChainConfig + gspec = &core.Genesis{ + Config: &config, + } + ) + + gspec.Config.TerminalTotalDifficulty = big.NewInt(0) + + out, _, 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) +} + func TestSupplyGenesisAlloc(t *testing.T) { var ( key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") diff --git a/eth/tracers/live/supply.go b/eth/tracers/live/supply.go index 8963f2ba9e..0c9141e99d 100644 --- a/eth/tracers/live/supply.go +++ b/eth/tracers/live/supply.go @@ -112,11 +112,14 @@ func newSupply(cfg json.RawMessage) (*tracing.Hooks, error) { func newSupplyInfo() supplyInfo { return supplyInfo{ Issuance: &supplyInfoIssuance{ - Reward: big.NewInt(0), - Withdrawals: big.NewInt(0), + GenesisAlloc: big.NewInt(0), + Reward: big.NewInt(0), + Withdrawals: big.NewInt(0), }, Burn: &supplyInfoBurn{ - Misc: big.NewInt(0), + EIP1559: big.NewInt(0), + Blob: big.NewInt(0), + Misc: big.NewInt(0), }, Number: 0, @@ -139,9 +142,7 @@ func (s *supply) OnBlockStart(ev tracing.BlockEvent) { // Calculate Burn for this block if ev.Block.BaseFee() != nil { burn := new(big.Int).Mul(new(big.Int).SetUint64(ev.Block.GasUsed()), ev.Block.BaseFee()) - if burn.Sign() != 0 { - s.delta.Burn.EIP1559 = burn - } + s.delta.Burn.EIP1559 = burn } // Blob burnt gas if blobGas := ev.Block.BlobGasUsed(); blobGas != nil && *blobGas > 0 && ev.Block.ExcessBlobGas() != nil { @@ -165,8 +166,6 @@ func (s *supply) OnGenesisBlock(b *types.Block, alloc types.GenesisAlloc) { s.delta.Hash = b.Hash() s.delta.ParentHash = b.ParentHash() - s.delta.Issuance.GenesisAlloc = big.NewInt(0) - // Initialize supply with total allocation in genesis block for _, account := range alloc { s.delta.Issuance.GenesisAlloc.Add(s.delta.Issuance.GenesisAlloc, account.Balance) @@ -269,6 +268,10 @@ func (s *supply) write(data any) { } // Remove empty fields + if supply.Issuance.GenesisAlloc.Sign() == 0 { + supply.Issuance.GenesisAlloc = nil + } + if supply.Issuance.Reward.Sign() == 0 { supply.Issuance.Reward = nil } @@ -281,6 +284,14 @@ func (s *supply) write(data any) { supply.Issuance = nil } + if supply.Burn.EIP1559.Sign() == 0 { + supply.Burn.EIP1559 = nil + } + + if supply.Burn.Blob.Sign() == 0 { + supply.Burn.Blob = nil + } + if supply.Burn.Misc.Sign() == 0 { supply.Burn.Misc = nil }