Skip to content

Commit 3ac74bd

Browse files
committed
supply tracer to exports hex values
1 parent a2639b2 commit 3ac74bd

File tree

3 files changed

+170
-60
lines changed

3 files changed

+170
-60
lines changed

eth/tracers/internal/tracetest/supply_test.go

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ package tracetest
1818

1919
import (
2020
"bufio"
21+
"bytes"
2122
"encoding/json"
2223
"fmt"
2324
"math/big"
2425
"os"
2526
"path"
2627
"path/filepath"
27-
"reflect"
2828
"testing"
2929

3030
"github.com/ethereum/go-ethereum/common"
31+
"github.com/ethereum/go-ethereum/common/hexutil"
3132
"github.com/ethereum/go-ethereum/consensus/beacon"
3233
"github.com/ethereum/go-ethereum/consensus/ethash"
3334
"github.com/ethereum/go-ethereum/core"
@@ -43,10 +44,11 @@ import (
4344
)
4445

4546
type supplyInfo struct {
46-
Delta *big.Int `json:"delta"`
47-
Reward *big.Int `json:"reward"`
48-
Withdrawals *big.Int `json:"withdrawals"`
49-
Burn *big.Int `json:"burn"`
47+
Delta *hexutil.Big `json:"delta"`
48+
DeltaSign string `json:"deltaSign"`
49+
Reward *hexutil.Big `json:"reward"`
50+
Withdrawals *hexutil.Big `json:"withdrawals"`
51+
Burn *hexutil.Big `json:"burn"`
5052

5153
// Block info
5254
Number uint64 `json:"blockNumber"`
@@ -76,10 +78,11 @@ func TestSupplyGenesisAlloc(t *testing.T) {
7678
)
7779

7880
expected := supplyInfo{
79-
Delta: new(big.Int).Mul(common.Big2, big.NewInt(params.Ether)),
80-
Reward: common.Big0,
81-
Withdrawals: common.Big0,
82-
Burn: common.Big0,
81+
Delta: (*hexutil.Big)(new(big.Int).Mul(common.Big2, big.NewInt(params.Ether))),
82+
DeltaSign: "+",
83+
Reward: (*hexutil.Big)(common.Big0),
84+
Withdrawals: (*hexutil.Big)(common.Big0),
85+
Burn: (*hexutil.Big)(common.Big0),
8386
Number: 0,
8487
Hash: common.HexToHash("0xbcc9466e9fc6a8b56f4b29ca353a421ff8b51a0c1a58ca4743b427605b08f2ca"),
8588
ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
@@ -91,10 +94,7 @@ func TestSupplyGenesisAlloc(t *testing.T) {
9194
}
9295

9396
actual := out[expected.Number]
94-
95-
if !reflect.DeepEqual(actual, expected) {
96-
t.Fatalf("incorrect supply info: expected %+v, got %+v", expected, actual)
97-
}
97+
compareAsJSON(t, expected, actual)
9898
}
9999

100100
func TestSupplyRewards(t *testing.T) {
@@ -107,10 +107,11 @@ func TestSupplyRewards(t *testing.T) {
107107
)
108108

109109
expected := supplyInfo{
110-
Delta: new(big.Int).Mul(common.Big2, big.NewInt(params.Ether)),
111-
Reward: new(big.Int).Mul(common.Big2, big.NewInt(params.Ether)),
112-
Withdrawals: common.Big0,
113-
Burn: common.Big0,
110+
Delta: (*hexutil.Big)(new(big.Int).Mul(common.Big2, big.NewInt(params.Ether))),
111+
DeltaSign: "+",
112+
Reward: (*hexutil.Big)(new(big.Int).Mul(common.Big2, big.NewInt(params.Ether))),
113+
Withdrawals: (*hexutil.Big)(common.Big0),
114+
Burn: (*hexutil.Big)(common.Big0),
114115
Number: 1,
115116
Hash: common.HexToHash("0xcbb08370505be503dafedc4e96d139ea27aba3cbc580148568b8a307b3f51052"),
116117
ParentHash: common.HexToHash("0xadeda0a83e337b6c073e3f0e9a17531a04009b397a9588c093b628f21b8bc5a3"),
@@ -122,10 +123,7 @@ func TestSupplyRewards(t *testing.T) {
122123
}
123124

124125
actual := out[expected.Number]
125-
126-
if !reflect.DeepEqual(actual, expected) {
127-
t.Fatalf("incorrect supply info: expected %+v, got %+v", expected, actual)
128-
}
126+
compareAsJSON(t, expected, actual)
129127
}
130128

131129
func TestSupplyEip1559Burn(t *testing.T) {
@@ -174,20 +172,19 @@ func TestSupplyEip1559Burn(t *testing.T) {
174172
reward = new(big.Int).Mul(common.Big2, big.NewInt(params.Ether))
175173
burn = new(big.Int).Mul(big.NewInt(21000), head.BaseFee)
176174
expected = supplyInfo{
177-
Delta: new(big.Int).Sub(reward, burn),
178-
Reward: reward,
179-
Withdrawals: common.Big0,
180-
Burn: burn,
175+
Delta: (*hexutil.Big)(new(big.Int).Sub(reward, burn)),
176+
DeltaSign: "+",
177+
Reward: (*hexutil.Big)(reward),
178+
Withdrawals: (*hexutil.Big)(common.Big0),
179+
Burn: (*hexutil.Big)(burn),
181180
Number: 1,
182181
Hash: head.Hash(),
183182
ParentHash: head.ParentHash,
184183
}
185184
)
186185

187186
actual := out[expected.Number]
188-
if !reflect.DeepEqual(actual, expected) {
189-
t.Fatalf("incorrect supply info: expected %+v, got %+v", expected, actual)
190-
}
187+
compareAsJSON(t, expected, actual)
191188
}
192189

193190
func TestSupplyWithdrawals(t *testing.T) {
@@ -216,20 +213,18 @@ func TestSupplyWithdrawals(t *testing.T) {
216213
var (
217214
head = chain.CurrentBlock()
218215
expected = supplyInfo{
219-
Delta: big.NewInt(1337000000000),
220-
Reward: common.Big0,
221-
Withdrawals: big.NewInt(1337000000000),
222-
Burn: common.Big0,
216+
Delta: (*hexutil.Big)(big.NewInt(1337000000000)),
217+
DeltaSign: "+",
218+
Reward: (*hexutil.Big)(common.Big0),
219+
Withdrawals: (*hexutil.Big)(big.NewInt(1337000000000)),
220+
Burn: (*hexutil.Big)(common.Big0),
223221
Number: 1,
224222
Hash: head.Hash(),
225223
ParentHash: head.ParentHash,
226224
}
227225
actual = out[expected.Number]
228226
)
229-
230-
if !reflect.DeepEqual(actual, expected) {
231-
t.Fatalf("incorrect supply info: expected %+v, got %+v", expected, actual)
232-
}
227+
compareAsJSON(t, expected, actual)
233228
}
234229

235230
// Tests fund retrieval after contract's selfdestruct.
@@ -314,20 +309,18 @@ func TestSupplySelfdestruct(t *testing.T) {
314309
head := preCancunChain.CurrentBlock()
315310
// Check live trace output
316311
expected := supplyInfo{
317-
Delta: big.NewInt(-55294500000000),
318-
Reward: common.Big0,
319-
Withdrawals: common.Big0,
320-
Burn: big.NewInt(55294500000000),
312+
Delta: (*hexutil.Big)(big.NewInt(55294500000000)),
313+
DeltaSign: "-",
314+
Reward: (*hexutil.Big)(common.Big0),
315+
Withdrawals: (*hexutil.Big)(common.Big0),
316+
Burn: (*hexutil.Big)(big.NewInt(55294500000000)),
321317
Number: 1,
322318
Hash: head.Hash(),
323319
ParentHash: head.ParentHash,
324320
}
325321

326322
actual := preCancunOutput[expected.Number]
327-
328-
if !reflect.DeepEqual(actual, expected) {
329-
t.Fatalf("Pre-cancun incorrect supply info: expected %+v, got %+v", expected, actual)
330-
}
323+
compareAsJSON(t, expected, actual)
331324

332325
// 2. Test post Cancun
333326
cancunTime := uint64(0)
@@ -357,20 +350,18 @@ func TestSupplySelfdestruct(t *testing.T) {
357350
// Check live trace output
358351
head = postCancunChain.CurrentBlock()
359352
expected = supplyInfo{
360-
Delta: big.NewInt(-55289500000000),
361-
Reward: common.Big0,
362-
Withdrawals: common.Big0,
363-
Burn: big.NewInt(55289500000000),
353+
Delta: (*hexutil.Big)(big.NewInt(55289500000000)),
354+
DeltaSign: "-",
355+
Reward: (*hexutil.Big)(common.Big0),
356+
Withdrawals: (*hexutil.Big)(common.Big0),
357+
Burn: (*hexutil.Big)(big.NewInt(55289500000000)),
364358
Number: 1,
365359
Hash: head.Hash(),
366360
ParentHash: head.ParentHash,
367361
}
368362

369363
actual = postCancunOutput[expected.Number]
370-
371-
if !reflect.DeepEqual(actual, expected) {
372-
t.Fatalf("Post-cancun incorrect supply info: expected %+v, got %+v", expected, actual)
373-
}
364+
compareAsJSON(t, expected, actual)
374365
}
375366

376367
// Tests selfdestructing contract to send its balance to itself (burn).
@@ -507,20 +498,18 @@ func TestSupplySelfdestructItselfAndRevert(t *testing.T) {
507498
totalBurn := new(big.Int).Add(blockBurn, eth5) // 5ETH burned from contract B
508499

509500
expected := supplyInfo{
510-
Delta: new(big.Int).Neg(totalBurn),
511-
Reward: common.Big0,
512-
Withdrawals: common.Big0,
513-
Burn: totalBurn,
501+
Delta: (*hexutil.Big)(totalBurn),
502+
DeltaSign: "-",
503+
Reward: (*hexutil.Big)(common.Big0),
504+
Withdrawals: (*hexutil.Big)(common.Big0),
505+
Burn: (*hexutil.Big)(totalBurn),
514506
Number: 1,
515507
Hash: block.Hash(),
516508
ParentHash: block.ParentHash(),
517509
}
518510

519511
actual := output[expected.Number]
520-
521-
if !reflect.DeepEqual(actual, expected) {
522-
t.Fatalf("incorrect supply info: expected %+v, got %+v", expected, actual)
523-
}
512+
compareAsJSON(t, expected, actual)
524513
}
525514

526515
func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockGen)) ([]supplyInfo, *core.BlockChain, error) {
@@ -575,3 +564,19 @@ func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockG
575564

576565
return output, chain, nil
577566
}
567+
568+
func compareAsJSON(t *testing.T, expected interface{}, actual interface{}) {
569+
want, err := json.Marshal(expected)
570+
if err != nil {
571+
t.Fatalf("failed to marshal expected value to JSON: %v", err)
572+
}
573+
574+
have, err := json.Marshal(actual)
575+
if err != nil {
576+
t.Fatalf("failed to marshal actual value to JSON: %v", err)
577+
}
578+
579+
if !bytes.Equal(want, have) {
580+
t.Fatalf("incorrect supply info: expected %s, got %s", string(want), string(have))
581+
}
582+
}

eth/tracers/live/gen_supplyinfo.go

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eth/tracers/live/supply.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99

1010
"github.com/ethereum/go-ethereum/common"
11+
"github.com/ethereum/go-ethereum/common/hexutil"
1112
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
1213
"github.com/ethereum/go-ethereum/core/tracing"
1314
"github.com/ethereum/go-ethereum/core/types"
@@ -22,6 +23,7 @@ func init() {
2223
}
2324

2425
type supplyInfo struct {
26+
// DeltaSign string `json:"deltaSign"`
2527
Delta *big.Int `json:"delta"`
2628
Reward *big.Int `json:"reward"`
2729
Withdrawals *big.Int `json:"withdrawals"`
@@ -33,6 +35,33 @@ type supplyInfo struct {
3335
ParentHash common.Hash `json:"parentHash"`
3436
}
3537

38+
//go:generate go run github.com/fjl/gencodec -type supplyInfo -field-override supplyInfoMarshaling -out gen_supplyinfo.go
39+
40+
// AbsBigInt is a wrapper around big.Int that marshals to a positive hexutil.Big
41+
type AbsBigInt big.Int
42+
43+
func (b AbsBigInt) MarshalJSON() ([]byte, error) {
44+
absVal := new(big.Int).Abs((*big.Int)(&b))
45+
hexBig := hexutil.Big(*absVal)
46+
return json.Marshal(&hexBig)
47+
}
48+
49+
// DeltaSign returns the sign of the delta as a string
50+
func (s *supplyInfo) DeltaSign() string {
51+
if s.Delta.Sign() == -1 {
52+
return "-"
53+
}
54+
return "+"
55+
}
56+
57+
type supplyInfoMarshaling struct {
58+
Delta *AbsBigInt
59+
DeltaSign string `json:"deltaSign"`
60+
Reward *hexutil.Big
61+
Withdrawals *hexutil.Big
62+
Burn *hexutil.Big
63+
}
64+
3665
func (s *supplyInfo) burn(amount *big.Int) {
3766
s.Burn.Add(s.Burn, amount)
3867
s.Delta.Sub(s.Delta, amount)

0 commit comments

Comments
 (0)