keep logic of removing empty fields in a single place for tech debt

This commit is contained in:
Chris Ziogas 2024-05-31 15:02:00 +03:00
parent 3ed1bd816b
commit 405ceae2a1
No known key found for this signature in database
GPG key ID: 2329CF479E36E2DA
2 changed files with 46 additions and 8 deletions

View file

@ -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")

View file

@ -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
}