Supply delta live tracer (#29347)

Introduces the first built-in live tracer. The supply tracer tracks ETH supply changes across blocks
and writes the output to disk. This will need to be enabled through CLI using the `--vmtrace supply` flag.

Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>
pull/29918/head
Chris Ziogas 4 months ago committed by GitHub
parent d4b81f0e08
commit c9e0b3105b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 37
      core/tracing/gen_balance_change_reason_stringer.go
  2. 2
      core/tracing/hooks.go
  3. 613
      eth/tracers/internal/tracetest/supply_test.go
  4. 49
      eth/tracers/live/gen_supplyinfoburn.go
  5. 49
      eth/tracers/live/gen_supplyinfoissuance.go
  6. 310
      eth/tracers/live/supply.go

@ -0,0 +1,37 @@
// Code generated by "stringer -type=BalanceChangeReason -output gen_balance_change_reason_stringer.go"; DO NOT EDIT.
package tracing
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[BalanceChangeUnspecified-0]
_ = x[BalanceIncreaseRewardMineUncle-1]
_ = x[BalanceIncreaseRewardMineBlock-2]
_ = x[BalanceIncreaseWithdrawal-3]
_ = x[BalanceIncreaseGenesisBalance-4]
_ = x[BalanceIncreaseRewardTransactionFee-5]
_ = x[BalanceDecreaseGasBuy-6]
_ = x[BalanceIncreaseGasReturn-7]
_ = x[BalanceIncreaseDaoContract-8]
_ = x[BalanceDecreaseDaoAccount-9]
_ = x[BalanceChangeTransfer-10]
_ = x[BalanceChangeTouchAccount-11]
_ = x[BalanceIncreaseSelfdestruct-12]
_ = x[BalanceDecreaseSelfdestruct-13]
_ = x[BalanceDecreaseSelfdestructBurn-14]
}
const _BalanceChangeReason_name = "BalanceChangeUnspecifiedBalanceIncreaseRewardMineUncleBalanceIncreaseRewardMineBlockBalanceIncreaseWithdrawalBalanceIncreaseGenesisBalanceBalanceIncreaseRewardTransactionFeeBalanceDecreaseGasBuyBalanceIncreaseGasReturnBalanceIncreaseDaoContractBalanceDecreaseDaoAccountBalanceChangeTransferBalanceChangeTouchAccountBalanceIncreaseSelfdestructBalanceDecreaseSelfdestructBalanceDecreaseSelfdestructBurn"
var _BalanceChangeReason_index = [...]uint16{0, 24, 54, 84, 109, 138, 173, 194, 218, 244, 269, 290, 315, 342, 369, 400}
func (i BalanceChangeReason) String() string {
if i >= BalanceChangeReason(len(_BalanceChangeReason_index)-1) {
return "BalanceChangeReason(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _BalanceChangeReason_name[_BalanceChangeReason_index[i]:_BalanceChangeReason_index[i+1]]
}

@ -199,6 +199,8 @@ type Hooks struct {
// for tracing and reporting.
type BalanceChangeReason byte
//go:generate stringer -type=BalanceChangeReason -output gen_balance_change_reason_stringer.go
const (
BalanceChangeUnspecified BalanceChangeReason = 0

@ -0,0 +1,613 @@
// Copyright 2021 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 <http://www.gnu.org/licenses/>.
package tracetest
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"math/big"
"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"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"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/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) {
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")
key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
addr2 = crypto.PubkeyToAddress(key2.PublicKey)
eth1 = new(big.Int).Mul(common.Big1, big.NewInt(params.Ether))
config = *params.AllEthashProtocolChanges
gspec = &core.Genesis{
Config: &config,
Alloc: types.GenesisAlloc{
addr1: {Balance: eth1},
addr2: {Balance: eth1},
},
}
)
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)
if err != nil {
t.Fatalf("failed to test supply tracer: %v", err)
}
actual := out[expected.Number]
compareAsJSON(t, expected, actual)
}
func TestSupplyEip1559Burn(t *testing.T) {
var (
config = *params.AllEthashProtocolChanges
aa = common.HexToAddress("0x000000000000000000000000000000000000aaaa")
// A sender who makes transactions, has some eth1
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
gwei5 = new(big.Int).Mul(big.NewInt(5), big.NewInt(params.GWei))
eth1 = new(big.Int).Mul(common.Big1, big.NewInt(params.Ether))
gspec = &core.Genesis{
Config: &config,
BaseFee: big.NewInt(params.InitialBaseFee),
Alloc: types.GenesisAlloc{
addr1: {Balance: eth1},
},
}
)
signer := types.LatestSigner(gspec.Config)
eip1559BlockGenerationFunc := func(b *core.BlockGen) {
txdata := &types.DynamicFeeTx{
ChainID: gspec.Config.ChainID,
Nonce: 0,
To: &aa,
Gas: 21000,
GasFeeCap: gwei5,
GasTipCap: big.NewInt(2),
}
tx := types.NewTx(txdata)
tx, _ = types.SignTx(tx, signer, key1)
b.AddTx(tx)
}
out, chain, err := testSupplyTracer(t, gspec, eip1559BlockGenerationFunc)
if err != nil {
t.Fatalf("failed to test supply tracer: %v", err)
}
var (
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{
Issuance: &supplyInfoIssuance{
Reward: (*hexutil.Big)(reward),
},
Burn: &supplyInfoBurn{
EIP1559: (*hexutil.Big)(burn),
},
Number: 1,
Hash: head.Hash(),
ParentHash: head.ParentHash,
}
)
actual := out[expected.Number]
compareAsJSON(t, expected, actual)
}
func TestSupplyWithdrawals(t *testing.T) {
var (
config = *params.MergedTestChainConfig
gspec = &core.Genesis{
Config: &config,
}
)
withdrawalsBlockGenerationFunc := func(b *core.BlockGen) {
b.SetPoS()
b.AddWithdrawal(&types.Withdrawal{
Validator: 42,
Address: common.Address{0xee},
Amount: 1337,
})
}
out, chain, err := testSupplyTracer(t, gspec, withdrawalsBlockGenerationFunc)
if err != nil {
t.Fatalf("failed to test supply tracer: %v", err)
}
var (
head = chain.CurrentBlock()
expected = supplyInfo{
Issuance: &supplyInfoIssuance{
Withdrawals: (*hexutil.Big)(big.NewInt(1337000000000)),
},
Number: 1,
Hash: head.Hash(),
ParentHash: head.ParentHash,
}
actual = out[expected.Number]
)
compareAsJSON(t, expected, actual)
}
// Tests fund retrieval after contract's selfdestruct.
// Contract A calls contract B which selfdestructs, but B receives eth1
// 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) {
var (
config = *params.TestChainConfig
aa = common.HexToAddress("0x1111111111111111111111111111111111111111")
bb = common.HexToAddress("0x2222222222222222222222222222222222222222")
dad = common.HexToAddress("0x0000000000000000000000000000000000000dad")
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
gwei5 = new(big.Int).Mul(big.NewInt(5), big.NewInt(params.GWei))
eth1 = new(big.Int).Mul(common.Big1, big.NewInt(params.Ether))
gspec = &core.Genesis{
Config: &config,
BaseFee: big.NewInt(params.InitialBaseFee),
Alloc: types.GenesisAlloc{
addr1: {Balance: eth1},
aa: {
Code: common.FromHex("0x61face60f01b6000527322222222222222222222222222222222222222226000806002600080855af160008103603457600080fd5b60008060008034865af1905060008103604c57600080fd5b5050"),
// Nonce: 0,
Balance: big.NewInt(0),
},
bb: {
Code: common.FromHex("0x6000357fface000000000000000000000000000000000000000000000000000000000000808203602f57610dad80ff5b5050"),
Nonce: 0,
Balance: eth1,
},
},
}
)
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{},
}
tx := types.NewTx(txdata)
tx, _ = types.SignTx(tx, signer, key1)
b.AddTx(tx)
}
// 1. Test pre Cancun
preCancunOutput, preCancunChain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc)
if err != nil {
t.Fatalf("Pre-cancun failed to test supply tracer: %v", err)
}
// Check balance at state:
// 1. 0x0000...000dad has 1 ether
// 2. A has 0 ether
// 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)
}
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)
}
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)
}
head := preCancunChain.CurrentBlock()
// Check live trace output
expected := supplyInfo{
Burn: &supplyInfoBurn{
EIP1559: (*hexutil.Big)(big.NewInt(55289500000000)),
Misc: (*hexutil.Big)(big.NewInt(5000000000)),
},
Number: 1,
Hash: head.Hash(),
ParentHash: head.ParentHash,
}
actual := preCancunOutput[expected.Number]
compareAsJSON(t, expected, actual)
// 2. Test post Cancun
cancunTime := uint64(0)
gspec.Config.ShanghaiTime = &cancunTime
gspec.Config.CancunTime = &cancunTime
postCancunOutput, postCancunChain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc)
if err != nil {
t.Fatalf("Post-cancun failed to test supply tracer: %v", err)
}
// Check balance at state:
// 1. 0x0000...000dad has 1 ether
// 3. A has 0 ether
// 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)
}
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)
}
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)
}
// Check live trace output
head = postCancunChain.CurrentBlock()
expected = supplyInfo{
Burn: &supplyInfoBurn{
EIP1559: (*hexutil.Big)(big.NewInt(55289500000000)),
},
Number: 1,
Hash: head.Hash(),
ParentHash: head.ParentHash,
}
actual = postCancunOutput[expected.Number]
compareAsJSON(t, expected, actual)
}
// Tests selfdestructing contract to send its balance to itself (burn).
// It tests both cases of selfdestructing succeding and being reverted.
// - Contract A calls B and D.
// - Contract B selfdestructs and sends the eth1 to itself (Burn amount to be counted).
// - 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) {
var (
config = *params.TestChainConfig
aa = common.HexToAddress("0x1111111111111111111111111111111111111111")
bb = common.HexToAddress("0x2222222222222222222222222222222222222222")
cc = common.HexToAddress("0x3333333333333333333333333333333333333333")
dd = common.HexToAddress("0x4444444444444444444444444444444444444444")
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
gwei5 = new(big.Int).Mul(big.NewInt(5), big.NewInt(params.GWei))
eth1 = new(big.Int).Mul(common.Big1, big.NewInt(params.Ether))
eth2 = new(big.Int).Mul(common.Big2, big.NewInt(params.Ether))
eth5 = new(big.Int).Mul(big.NewInt(5), big.NewInt(params.Ether))
gspec = &core.Genesis{
Config: &config,
// BaseFee: big.NewInt(params.InitialBaseFee),
Alloc: types.GenesisAlloc{
addr1: {Balance: eth1},
aa: {
// Contract code in YUL:
//
// object "ContractA" {
// code {
// let B := 0x2222222222222222222222222222222222222222
// let D := 0x4444444444444444444444444444444444444444
// // Call to Contract B
// let resB:= call(gas(), B, 0, 0x0, 0x0, 0, 0)
// // Call to Contract D
// let resD := call(gas(), D, 0, 0x0, 0x0, 0, 0)
// }
// }
Code: common.FromHex("0x73222222222222222222222222222222222222222273444444444444444444444444444444444444444460006000600060006000865af160006000600060006000865af150505050"),
Balance: common.Big0,
},
bb: {
// Contract code in YUL:
//
// object "ContractB" {
// code {
// let self := address()
// selfdestruct(self)
// }
// }
Code: common.FromHex("0x3080ff50"),
Balance: eth5,
},
cc: {
Code: common.FromHex("0x3080ff50"),
Balance: eth1,
},
dd: {
// Contract code in YUL:
//
// object "ContractD" {
// code {
// let C := 0x3333333333333333333333333333333333333333
// // Call to Contract C
// let resC := call(gas(), C, 0, 0x0, 0x0, 0, 0)
// // Revert
// revert(0, 0)
// }
// }
Code: common.FromHex("0x73333333333333333333333333333333333333333360006000600060006000855af160006000fd5050"),
Balance: eth2,
},
},
}
)
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: common.Big0,
Gas: 150000,
GasPrice: gwei5,
Data: []byte{},
}
tx := types.NewTx(txdata)
tx, _ = types.SignTx(tx, signer, key1)
b.AddTx(tx)
}
output, chain, err := testSupplyTracer(t, gspec, testBlockGenerationFunc)
if err != nil {
t.Fatalf("failed to test supply tracer: %v", err)
}
// Check balance at state:
// 1. A has 0 ether
// 2. B has 0 ether, burned
// 3. C has 2 ether, selfdestructed but parent D reverted
// 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)
}
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)
}
if got, exp := statedb.GetBalance(cc), eth1; got.CmpBig(exp) != 0 {
t.Fatalf("address \"%v\" balance, got %v exp %v\n", bb, 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)
}
// Check live trace output
block := chain.GetBlockByNumber(1)
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
},
Number: 1,
Hash: block.Hash(),
ParentHash: block.ParentHash(),
}
actual := output[expected.Number]
compareAsJSON(t, expected, actual)
}
func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockGen)) ([]supplyInfo, *core.BlockChain, error) {
var (
engine = beacon.New(ethash.NewFaker())
)
traceOutputPath := filepath.ToSlash(t.TempDir())
traceOutputFilename := path.Join(traceOutputPath, "supply.jsonl")
// 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)
}
chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), 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)
}
defer chain.Stop()
_, blocks, _ := core.GenerateChainWithGenesis(genesis, engine, 1, func(i int, b *core.BlockGen) {
b.SetCoinbase(common.Address{1})
gen(b)
})
if n, err := chain.InsertChain(blocks); err != nil {
return 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)
}
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 {
return nil, chain, fmt.Errorf("failed to unmarshal result: %v", err)
}
output = append(output, info)
}
return output, chain, nil
}
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: expected %s, got %s", string(want), string(have))
}
}

@ -0,0 +1,49 @@
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
package live
import (
"encoding/json"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
)
var _ = (*supplyInfoBurnMarshaling)(nil)
// MarshalJSON marshals as JSON.
func (s supplyInfoBurn) MarshalJSON() ([]byte, error) {
type supplyInfoBurn struct {
EIP1559 *hexutil.Big `json:"1559,omitempty"`
Blob *hexutil.Big `json:"blob,omitempty"`
Misc *hexutil.Big `json:"misc,omitempty"`
}
var enc supplyInfoBurn
enc.EIP1559 = (*hexutil.Big)(s.EIP1559)
enc.Blob = (*hexutil.Big)(s.Blob)
enc.Misc = (*hexutil.Big)(s.Misc)
return json.Marshal(&enc)
}
// UnmarshalJSON unmarshals from JSON.
func (s *supplyInfoBurn) UnmarshalJSON(input []byte) error {
type supplyInfoBurn struct {
EIP1559 *hexutil.Big `json:"1559,omitempty"`
Blob *hexutil.Big `json:"blob,omitempty"`
Misc *hexutil.Big `json:"misc,omitempty"`
}
var dec supplyInfoBurn
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
if dec.EIP1559 != nil {
s.EIP1559 = (*big.Int)(dec.EIP1559)
}
if dec.Blob != nil {
s.Blob = (*big.Int)(dec.Blob)
}
if dec.Misc != nil {
s.Misc = (*big.Int)(dec.Misc)
}
return nil
}

@ -0,0 +1,49 @@
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
package live
import (
"encoding/json"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
)
var _ = (*supplyInfoIssuanceMarshaling)(nil)
// MarshalJSON marshals as JSON.
func (s supplyInfoIssuance) MarshalJSON() ([]byte, error) {
type supplyInfoIssuance struct {
GenesisAlloc *hexutil.Big `json:"genesisAlloc,omitempty"`
Reward *hexutil.Big `json:"reward,omitempty"`
Withdrawals *hexutil.Big `json:"withdrawals,omitempty"`
}
var enc supplyInfoIssuance
enc.GenesisAlloc = (*hexutil.Big)(s.GenesisAlloc)
enc.Reward = (*hexutil.Big)(s.Reward)
enc.Withdrawals = (*hexutil.Big)(s.Withdrawals)
return json.Marshal(&enc)
}
// UnmarshalJSON unmarshals from JSON.
func (s *supplyInfoIssuance) UnmarshalJSON(input []byte) error {
type supplyInfoIssuance struct {
GenesisAlloc *hexutil.Big `json:"genesisAlloc,omitempty"`
Reward *hexutil.Big `json:"reward,omitempty"`
Withdrawals *hexutil.Big `json:"withdrawals,omitempty"`
}
var dec supplyInfoIssuance
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
if dec.GenesisAlloc != nil {
s.GenesisAlloc = (*big.Int)(dec.GenesisAlloc)
}
if dec.Reward != nil {
s.Reward = (*big.Int)(dec.Reward)
}
if dec.Withdrawals != nil {
s.Withdrawals = (*big.Int)(dec.Withdrawals)
}
return nil
}

@ -0,0 +1,310 @@
package live
import (
"encoding/json"
"errors"
"fmt"
"math/big"
"path/filepath"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
"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/eth/tracers"
"github.com/ethereum/go-ethereum/log"
"gopkg.in/natefinch/lumberjack.v2"
)
func init() {
tracers.LiveDirectory.Register("supply", newSupply)
}
type supplyInfoIssuance struct {
GenesisAlloc *big.Int `json:"genesisAlloc,omitempty"`
Reward *big.Int `json:"reward,omitempty"`
Withdrawals *big.Int `json:"withdrawals,omitempty"`
}
//go:generate go run github.com/fjl/gencodec -type supplyInfoIssuance -field-override supplyInfoIssuanceMarshaling -out gen_supplyinfoissuance.go
type supplyInfoIssuanceMarshaling struct {
GenesisAlloc *hexutil.Big
Reward *hexutil.Big
Withdrawals *hexutil.Big
}
type supplyInfoBurn struct {
EIP1559 *big.Int `json:"1559,omitempty"`
Blob *big.Int `json:"blob,omitempty"`
Misc *big.Int `json:"misc,omitempty"`
}
//go:generate go run github.com/fjl/gencodec -type supplyInfoBurn -field-override supplyInfoBurnMarshaling -out gen_supplyinfoburn.go
type supplyInfoBurnMarshaling struct {
EIP1559 *hexutil.Big
Blob *hexutil.Big
Misc *hexutil.Big
}
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"`
}
type supplyTxCallstack struct {
calls []supplyTxCallstack
burn *big.Int
}
type supply struct {
delta supplyInfo
txCallstack []supplyTxCallstack // Callstack for current transaction
logger *lumberjack.Logger
}
type supplyTracerConfig struct {
Path string `json:"path"` // Path to the directory where the tracer logs will be stored
MaxSize int `json:"maxSize"` // MaxSize is the maximum size in megabytes of the tracer log file before it gets rotated. It defaults to 100 megabytes.
}
func newSupply(cfg json.RawMessage) (*tracing.Hooks, error) {
var config supplyTracerConfig
if cfg != nil {
if err := json.Unmarshal(cfg, &config); err != nil {
return nil, fmt.Errorf("failed to parse config: %v", err)
}
}
if config.Path == "" {
return nil, errors.New("supply tracer output path is required")
}
// Store traces in a rotating file
logger := &lumberjack.Logger{
Filename: filepath.Join(config.Path, "supply.jsonl"),
}
if config.MaxSize > 0 {
logger.MaxSize = config.MaxSize
}
t := &supply{
delta: newSupplyInfo(),
logger: logger,
}
return &tracing.Hooks{
OnBlockStart: t.OnBlockStart,
OnBlockEnd: t.OnBlockEnd,
OnGenesisBlock: t.OnGenesisBlock,
OnTxStart: t.OnTxStart,
OnBalanceChange: t.OnBalanceChange,
OnEnter: t.OnEnter,
OnExit: t.OnExit,
OnClose: t.OnClose,
}, nil
}
func newSupplyInfo() supplyInfo {
return supplyInfo{
Issuance: &supplyInfoIssuance{
GenesisAlloc: big.NewInt(0),
Reward: big.NewInt(0),
Withdrawals: big.NewInt(0),
},
Burn: &supplyInfoBurn{
EIP1559: big.NewInt(0),
Blob: big.NewInt(0),
Misc: big.NewInt(0),
},
Number: 0,
Hash: common.Hash{},
ParentHash: common.Hash{},
}
}
func (s *supply) resetDelta() {
s.delta = newSupplyInfo()
}
func (s *supply) OnBlockStart(ev tracing.BlockEvent) {
s.resetDelta()
s.delta.Number = ev.Block.NumberU64()
s.delta.Hash = ev.Block.Hash()
s.delta.ParentHash = ev.Block.ParentHash()
// 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())
s.delta.Burn.EIP1559 = burn
}
// Blob burnt gas
if blobGas := ev.Block.BlobGasUsed(); blobGas != nil && *blobGas > 0 && ev.Block.ExcessBlobGas() != nil {
var (
excess = *ev.Block.ExcessBlobGas()
baseFee = eip4844.CalcBlobFee(excess)
burn = new(big.Int).Mul(new(big.Int).SetUint64(*blobGas), baseFee)
)
s.delta.Burn.Blob = burn
}
}
func (s *supply) OnBlockEnd(err error) {
s.write(s.delta)
}
func (s *supply) OnGenesisBlock(b *types.Block, alloc types.GenesisAlloc) {
s.resetDelta()
s.delta.Number = b.NumberU64()
s.delta.Hash = b.Hash()
s.delta.ParentHash = b.ParentHash()
// Initialize supply with total allocation in genesis block
for _, account := range alloc {
s.delta.Issuance.GenesisAlloc.Add(s.delta.Issuance.GenesisAlloc, account.Balance)
}
s.write(s.delta)
}
func (s *supply) OnBalanceChange(a common.Address, prevBalance, newBalance *big.Int, reason tracing.BalanceChangeReason) {
diff := new(big.Int).Sub(newBalance, prevBalance)
// NOTE: don't handle "BalanceIncreaseGenesisBalance" because it is handled in OnGenesisBlock
switch reason {
case tracing.BalanceIncreaseRewardMineUncle:
case tracing.BalanceIncreaseRewardMineBlock:
s.delta.Issuance.Reward.Add(s.delta.Issuance.Reward, diff)
case tracing.BalanceIncreaseWithdrawal:
s.delta.Issuance.Withdrawals.Add(s.delta.Issuance.Withdrawals, diff)
case tracing.BalanceDecreaseSelfdestructBurn:
// BalanceDecreaseSelfdestructBurn is non-reversible as it happens
// at the end of the transaction.
s.delta.Burn.Misc.Sub(s.delta.Burn.Misc, diff)
default:
return
}
}
func (s *supply) OnTxStart(vm *tracing.VMContext, tx *types.Transaction, from common.Address) {
s.txCallstack = make([]supplyTxCallstack, 0, 1)
}
// internalTxsHandler handles internal transactions burned amount
func (s *supply) internalTxsHandler(call *supplyTxCallstack) {
// Handle Burned amount
if call.burn != nil {
s.delta.Burn.Misc.Add(s.delta.Burn.Misc, call.burn)
}
if len(call.calls) > 0 {
// Recursivelly handle internal calls
for _, call := range call.calls {
callCopy := call
s.internalTxsHandler(&callCopy)
}
}
}
func (s *supply) OnEnter(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
call := supplyTxCallstack{
calls: make([]supplyTxCallstack, 0),
}
// This is a special case of burned amount which has to be handled here
// which happens when type == selfdestruct and from == to.
if vm.OpCode(typ) == vm.SELFDESTRUCT && from == to && value.Cmp(common.Big0) == 1 {
call.burn = value
}
// Append call to the callstack, so we can fill the details in CaptureExit
s.txCallstack = append(s.txCallstack, call)
}
func (s *supply) OnExit(depth int, output []byte, gasUsed uint64, err error, reverted bool) {
if depth == 0 {
// No need to handle Burned amount if transaction is reverted
if !reverted {
s.internalTxsHandler(&s.txCallstack[0])
}
return
}
size := len(s.txCallstack)
if size <= 1 {
return
}
// Pop call
call := s.txCallstack[size-1]
s.txCallstack = s.txCallstack[:size-1]
size -= 1
// In case of a revert, we can drop the call and all its subcalls.
// Caution, that this has to happen after popping the call from the stack.
if reverted {
return
}
s.txCallstack[size-1].calls = append(s.txCallstack[size-1].calls, call)
}
func (s *supply) OnClose() {
if err := s.logger.Close(); err != nil {
log.Warn("failed to close supply tracer log file", "error", err)
}
}
func (s *supply) write(data any) {
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.GenesisAlloc.Sign() == 0 {
supply.Issuance.GenesisAlloc = nil
}
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.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
}
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 {
log.Warn("failed to write to supply tracer log file", "error", err)
}
if _, err := s.logger.Write([]byte{'\n'}); err != nil {
log.Warn("failed to write to supply tracer log file", "error", err)
}
}
Loading…
Cancel
Save