omitempty fields from supply tracer output

This commit is contained in:
Chris Ziogas 2024-05-31 15:00:17 +03:00
parent b12c669a09
commit 3ed1bd816b
No known key found for this signature in database
GPG key ID: 2329CF479E36E2DA
4 changed files with 59 additions and 70 deletions

View file

@ -45,19 +45,19 @@ import (
type supplyInfoIssuance struct { type supplyInfoIssuance struct {
GenesisAlloc *hexutil.Big `json:"genesisAlloc,omitempty"` GenesisAlloc *hexutil.Big `json:"genesisAlloc,omitempty"`
Reward *hexutil.Big `json:"reward"` Reward *hexutil.Big `json:"reward,omitempty"`
Withdrawals *hexutil.Big `json:"withdrawals"` Withdrawals *hexutil.Big `json:"withdrawals,omitempty"`
} }
type supplyInfoBurn struct { type supplyInfoBurn struct {
EIP1559 *hexutil.Big `json:"1559"` EIP1559 *hexutil.Big `json:"1559,omitempty"`
Blob *hexutil.Big `json:"blob"` Blob *hexutil.Big `json:"blob,omitempty"`
Misc *hexutil.Big `json:"misc"` Misc *hexutil.Big `json:"misc,omitempty"`
} }
type supplyInfo struct { type supplyInfo struct {
Issuance *supplyInfoIssuance `json:"issuance"` Issuance *supplyInfoIssuance `json:"issuance,omitempty"`
Burn *supplyInfoBurn `json:"burn"` Burn *supplyInfoBurn `json:"burn,omitempty"`
// Block info // Block info
Number uint64 `json:"blockNumber"` Number uint64 `json:"blockNumber"`
@ -89,13 +89,6 @@ func TestSupplyGenesisAlloc(t *testing.T) {
expected := supplyInfo{ expected := supplyInfo{
Issuance: &supplyInfoIssuance{ Issuance: &supplyInfoIssuance{
GenesisAlloc: (*hexutil.Big)(new(big.Int).Mul(common.Big2, big.NewInt(params.Ether))), GenesisAlloc: (*hexutil.Big)(new(big.Int).Mul(common.Big2, big.NewInt(params.Ether))),
Reward: (*hexutil.Big)(common.Big0),
Withdrawals: (*hexutil.Big)(common.Big0),
},
Burn: &supplyInfoBurn{
EIP1559: (*hexutil.Big)(common.Big0),
Blob: (*hexutil.Big)(common.Big0),
Misc: (*hexutil.Big)(common.Big0),
}, },
Number: 0, Number: 0,
Hash: common.HexToHash("0xbcc9466e9fc6a8b56f4b29ca353a421ff8b51a0c1a58ca4743b427605b08f2ca"), Hash: common.HexToHash("0xbcc9466e9fc6a8b56f4b29ca353a421ff8b51a0c1a58ca4743b427605b08f2ca"),
@ -124,12 +117,6 @@ func TestSupplyRewards(t *testing.T) {
expected := supplyInfo{ expected := supplyInfo{
Issuance: &supplyInfoIssuance{ Issuance: &supplyInfoIssuance{
Reward: (*hexutil.Big)(new(big.Int).Mul(common.Big2, big.NewInt(params.Ether))), Reward: (*hexutil.Big)(new(big.Int).Mul(common.Big2, big.NewInt(params.Ether))),
Withdrawals: (*hexutil.Big)(common.Big0),
},
Burn: &supplyInfoBurn{
EIP1559: (*hexutil.Big)(common.Big0),
Blob: (*hexutil.Big)(common.Big0),
Misc: (*hexutil.Big)(common.Big0),
}, },
Number: 1, Number: 1,
Hash: common.HexToHash("0xcbb08370505be503dafedc4e96d139ea27aba3cbc580148568b8a307b3f51052"), Hash: common.HexToHash("0xcbb08370505be503dafedc4e96d139ea27aba3cbc580148568b8a307b3f51052"),
@ -194,12 +181,9 @@ func TestSupplyEip1559Burn(t *testing.T) {
expected = supplyInfo{ expected = supplyInfo{
Issuance: &supplyInfoIssuance{ Issuance: &supplyInfoIssuance{
Reward: (*hexutil.Big)(reward), Reward: (*hexutil.Big)(reward),
Withdrawals: (*hexutil.Big)(common.Big0),
}, },
Burn: &supplyInfoBurn{ Burn: &supplyInfoBurn{
EIP1559: (*hexutil.Big)(burn), EIP1559: (*hexutil.Big)(burn),
Blob: (*hexutil.Big)(common.Big0),
Misc: (*hexutil.Big)(common.Big0),
}, },
Number: 1, Number: 1,
Hash: head.Hash(), Hash: head.Hash(),
@ -238,14 +222,8 @@ func TestSupplyWithdrawals(t *testing.T) {
head = chain.CurrentBlock() head = chain.CurrentBlock()
expected = supplyInfo{ expected = supplyInfo{
Issuance: &supplyInfoIssuance{ Issuance: &supplyInfoIssuance{
Reward: (*hexutil.Big)(common.Big0),
Withdrawals: (*hexutil.Big)(big.NewInt(1337000000000)), Withdrawals: (*hexutil.Big)(big.NewInt(1337000000000)),
}, },
Burn: &supplyInfoBurn{
EIP1559: (*hexutil.Big)(common.Big0),
Blob: (*hexutil.Big)(common.Big0),
Misc: (*hexutil.Big)(common.Big0),
},
Number: 1, Number: 1,
Hash: head.Hash(), Hash: head.Hash(),
ParentHash: head.ParentHash, ParentHash: head.ParentHash,
@ -338,13 +316,8 @@ func TestSupplySelfdestruct(t *testing.T) {
head := preCancunChain.CurrentBlock() head := preCancunChain.CurrentBlock()
// Check live trace output // Check live trace output
expected := supplyInfo{ expected := supplyInfo{
Issuance: &supplyInfoIssuance{
Reward: (*hexutil.Big)(common.Big0),
Withdrawals: (*hexutil.Big)(common.Big0),
},
Burn: &supplyInfoBurn{ Burn: &supplyInfoBurn{
EIP1559: (*hexutil.Big)(big.NewInt(55289500000000)), EIP1559: (*hexutil.Big)(big.NewInt(55289500000000)),
Blob: (*hexutil.Big)(common.Big0),
Misc: (*hexutil.Big)(big.NewInt(5000000000)), Misc: (*hexutil.Big)(big.NewInt(5000000000)),
}, },
Number: 1, Number: 1,
@ -384,14 +357,8 @@ func TestSupplySelfdestruct(t *testing.T) {
// Check live trace output // Check live trace output
head = postCancunChain.CurrentBlock() head = postCancunChain.CurrentBlock()
expected = supplyInfo{ expected = supplyInfo{
Issuance: &supplyInfoIssuance{
Reward: (*hexutil.Big)(common.Big0),
Withdrawals: (*hexutil.Big)(common.Big0),
},
Burn: &supplyInfoBurn{ Burn: &supplyInfoBurn{
EIP1559: (*hexutil.Big)(big.NewInt(55289500000000)), EIP1559: (*hexutil.Big)(big.NewInt(55289500000000)),
Blob: (*hexutil.Big)(common.Big0),
Misc: (*hexutil.Big)(common.Big0),
}, },
Number: 1, Number: 1,
Hash: head.Hash(), Hash: head.Hash(),
@ -535,13 +502,8 @@ func TestSupplySelfdestructItselfAndRevert(t *testing.T) {
block := chain.GetBlockByNumber(1) block := chain.GetBlockByNumber(1)
expected := supplyInfo{ expected := supplyInfo{
Issuance: &supplyInfoIssuance{
Reward: (*hexutil.Big)(common.Big0),
Withdrawals: (*hexutil.Big)(common.Big0),
},
Burn: &supplyInfoBurn{ Burn: &supplyInfoBurn{
EIP1559: (*hexutil.Big)(new(big.Int).Mul(block.BaseFee(), big.NewInt(int64(block.GasUsed())))), EIP1559: (*hexutil.Big)(new(big.Int).Mul(block.BaseFee(), big.NewInt(int64(block.GasUsed())))),
Blob: (*hexutil.Big)(common.Big0),
Misc: (*hexutil.Big)(eth5), // 5ETH burned from contract B Misc: (*hexutil.Big)(eth5), // 5ETH burned from contract B
}, },
Number: 1, Number: 1,

View file

@ -14,9 +14,9 @@ var _ = (*supplyInfoBurnMarshaling)(nil)
// MarshalJSON marshals as JSON. // MarshalJSON marshals as JSON.
func (s supplyInfoBurn) MarshalJSON() ([]byte, error) { func (s supplyInfoBurn) MarshalJSON() ([]byte, error) {
type supplyInfoBurn struct { type supplyInfoBurn struct {
EIP1559 *hexutil.Big `json:"1559"` EIP1559 *hexutil.Big `json:"1559,omitempty"`
Blob *hexutil.Big `json:"blob"` Blob *hexutil.Big `json:"blob,omitempty"`
Misc *hexutil.Big `json:"misc"` Misc *hexutil.Big `json:"misc,omitempty"`
} }
var enc supplyInfoBurn var enc supplyInfoBurn
enc.EIP1559 = (*hexutil.Big)(s.EIP1559) enc.EIP1559 = (*hexutil.Big)(s.EIP1559)
@ -28,9 +28,9 @@ func (s supplyInfoBurn) MarshalJSON() ([]byte, error) {
// UnmarshalJSON unmarshals from JSON. // UnmarshalJSON unmarshals from JSON.
func (s *supplyInfoBurn) UnmarshalJSON(input []byte) error { func (s *supplyInfoBurn) UnmarshalJSON(input []byte) error {
type supplyInfoBurn struct { type supplyInfoBurn struct {
EIP1559 *hexutil.Big `json:"1559"` EIP1559 *hexutil.Big `json:"1559,omitempty"`
Blob *hexutil.Big `json:"blob"` Blob *hexutil.Big `json:"blob,omitempty"`
Misc *hexutil.Big `json:"misc"` Misc *hexutil.Big `json:"misc,omitempty"`
} }
var dec supplyInfoBurn var dec supplyInfoBurn
if err := json.Unmarshal(input, &dec); err != nil { if err := json.Unmarshal(input, &dec); err != nil {

View file

@ -15,8 +15,8 @@ var _ = (*supplyInfoIssuanceMarshaling)(nil)
func (s supplyInfoIssuance) MarshalJSON() ([]byte, error) { func (s supplyInfoIssuance) MarshalJSON() ([]byte, error) {
type supplyInfoIssuance struct { type supplyInfoIssuance struct {
GenesisAlloc *hexutil.Big `json:"genesisAlloc,omitempty"` GenesisAlloc *hexutil.Big `json:"genesisAlloc,omitempty"`
Reward *hexutil.Big `json:"reward"` Reward *hexutil.Big `json:"reward,omitempty"`
Withdrawals *hexutil.Big `json:"withdrawals"` Withdrawals *hexutil.Big `json:"withdrawals,omitempty"`
} }
var enc supplyInfoIssuance var enc supplyInfoIssuance
enc.GenesisAlloc = (*hexutil.Big)(s.GenesisAlloc) enc.GenesisAlloc = (*hexutil.Big)(s.GenesisAlloc)
@ -29,8 +29,8 @@ func (s supplyInfoIssuance) MarshalJSON() ([]byte, error) {
func (s *supplyInfoIssuance) UnmarshalJSON(input []byte) error { func (s *supplyInfoIssuance) UnmarshalJSON(input []byte) error {
type supplyInfoIssuance struct { type supplyInfoIssuance struct {
GenesisAlloc *hexutil.Big `json:"genesisAlloc,omitempty"` GenesisAlloc *hexutil.Big `json:"genesisAlloc,omitempty"`
Reward *hexutil.Big `json:"reward"` Reward *hexutil.Big `json:"reward,omitempty"`
Withdrawals *hexutil.Big `json:"withdrawals"` Withdrawals *hexutil.Big `json:"withdrawals,omitempty"`
} }
var dec supplyInfoIssuance var dec supplyInfoIssuance
if err := json.Unmarshal(input, &dec); err != nil { if err := json.Unmarshal(input, &dec); err != nil {

View file

@ -24,8 +24,8 @@ func init() {
type supplyInfoIssuance struct { type supplyInfoIssuance struct {
GenesisAlloc *big.Int `json:"genesisAlloc,omitempty"` GenesisAlloc *big.Int `json:"genesisAlloc,omitempty"`
Reward *big.Int `json:"reward"` Reward *big.Int `json:"reward,omitempty"`
Withdrawals *big.Int `json:"withdrawals"` Withdrawals *big.Int `json:"withdrawals,omitempty"`
} }
//go:generate go run github.com/fjl/gencodec -type supplyInfoIssuance -field-override supplyInfoIssuanceMarshaling -out gen_supplyinfoissuance.go //go:generate go run github.com/fjl/gencodec -type supplyInfoIssuance -field-override supplyInfoIssuanceMarshaling -out gen_supplyinfoissuance.go
@ -36,9 +36,9 @@ type supplyInfoIssuanceMarshaling struct {
} }
type supplyInfoBurn struct { type supplyInfoBurn struct {
EIP1559 *big.Int `json:"1559"` EIP1559 *big.Int `json:"1559,omitempty"`
Blob *big.Int `json:"blob"` Blob *big.Int `json:"blob,omitempty"`
Misc *big.Int `json:"misc"` Misc *big.Int `json:"misc,omitempty"`
} }
//go:generate go run github.com/fjl/gencodec -type supplyInfoBurn -field-override supplyInfoBurnMarshaling -out gen_supplyinfoburn.go //go:generate go run github.com/fjl/gencodec -type supplyInfoBurn -field-override supplyInfoBurnMarshaling -out gen_supplyinfoburn.go
@ -49,8 +49,8 @@ type supplyInfoBurnMarshaling struct {
} }
type supplyInfo struct { type supplyInfo struct {
Issuance *supplyInfoIssuance `json:"issuance"` Issuance *supplyInfoIssuance `json:"issuance,omitempty"`
Burn *supplyInfoBurn `json:"burn"` Burn *supplyInfoBurn `json:"burn,omitempty"`
// Block info // Block info
Number uint64 `json:"blockNumber"` Number uint64 `json:"blockNumber"`
@ -116,8 +116,6 @@ func newSupplyInfo() supplyInfo {
Withdrawals: big.NewInt(0), Withdrawals: big.NewInt(0),
}, },
Burn: &supplyInfoBurn{ Burn: &supplyInfoBurn{
EIP1559: big.NewInt(0),
Blob: big.NewInt(0),
Misc: big.NewInt(0), Misc: big.NewInt(0),
}, },
@ -141,7 +139,9 @@ func (s *supply) OnBlockStart(ev tracing.BlockEvent) {
// Calculate Burn for this block // Calculate Burn for this block
if ev.Block.BaseFee() != nil { if ev.Block.BaseFee() != nil {
burn := new(big.Int).Mul(new(big.Int).SetUint64(ev.Block.GasUsed()), ev.Block.BaseFee()) burn := new(big.Int).Mul(new(big.Int).SetUint64(ev.Block.GasUsed()), ev.Block.BaseFee())
s.delta.Burn.EIP1559.Add(s.delta.Burn.EIP1559, burn) if burn.Sign() != 0 {
s.delta.Burn.EIP1559 = burn
}
} }
// Blob burnt gas // Blob burnt gas
if blobGas := ev.Block.BlobGasUsed(); blobGas != nil && *blobGas > 0 && ev.Block.ExcessBlobGas() != nil { if blobGas := ev.Block.BlobGasUsed(); blobGas != nil && *blobGas > 0 && ev.Block.ExcessBlobGas() != nil {
@ -150,7 +150,7 @@ func (s *supply) OnBlockStart(ev tracing.BlockEvent) {
baseFee = eip4844.CalcBlobFee(excess) baseFee = eip4844.CalcBlobFee(excess)
burn = new(big.Int).Mul(new(big.Int).SetUint64(*blobGas), baseFee) burn = new(big.Int).Mul(new(big.Int).SetUint64(*blobGas), baseFee)
) )
s.delta.Burn.Blob.Add(s.delta.Burn.Blob, burn) s.delta.Burn.Blob = burn
} }
} }
@ -262,7 +262,34 @@ func (s *supply) OnClose() {
} }
func (s *supply) write(data any) { func (s *supply) write(data any) {
out, _ := json.Marshal(data) supply, ok := data.(supplyInfo)
if !ok {
log.Warn("failed to cast supply tracer data on write to log file")
return
}
// Remove empty fields
if supply.Issuance.Reward.Sign() == 0 {
supply.Issuance.Reward = nil
}
if supply.Issuance.Withdrawals.Sign() == 0 {
supply.Issuance.Withdrawals = nil
}
if supply.Issuance.GenesisAlloc == nil && supply.Issuance.Reward == nil && supply.Issuance.Withdrawals == nil {
supply.Issuance = nil
}
if supply.Burn.Misc.Sign() == 0 {
supply.Burn.Misc = nil
}
if supply.Burn.EIP1559 == nil && supply.Burn.Blob == nil && supply.Burn.Misc == nil {
supply.Burn = nil
}
out, _ := json.Marshal(supply)
if _, err := s.logger.Write(out); err != nil { if _, err := s.logger.Write(out); err != nil {
log.Warn("failed to write to supply tracer log file", "error", err) log.Warn("failed to write to supply tracer log file", "error", err)
} }