add supply delta live tracer

This commit is contained in:
Chris Ziogas 2024-03-26 11:30:50 +02:00
parent c686e305a3
commit a4117a8337
No known key found for this signature in database
GPG key ID: 2329CF479E36E2DA
2 changed files with 806 additions and 0 deletions

View file

@ -0,0 +1,567 @@
// 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"
"encoding/json"
"fmt"
"math/big"
"os"
"path"
"reflect"
"testing"
"github.com/ethereum/go-ethereum/common"
"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/eth/tracers/live"
"github.com/ethereum/go-ethereum/params"
// Force-load live packages, to trigger registration
_ "github.com/ethereum/go-ethereum/eth/tracers/live"
)
func emptyBlockGenerationFunc(b *core.BlockGen) {}
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 := live.SupplyInfo{
Delta: new(big.Int).Mul(common.Big2, big.NewInt(params.Ether)),
Reward: common.Big0,
Withdrawals: common.Big0,
Burn: common.Big0,
Number: 0,
Hash: common.HexToHash("0xbcc9466e9fc6a8b56f4b29ca353a421ff8b51a0c1a58ca4743b427605b08f2ca"),
ParentHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
}
out, _, err := testSupplyTracer(gspec, emptyBlockGenerationFunc)
if err != nil {
t.Fatalf("failed to test supply tracer: %v", err)
}
actual := out[expected.Number]
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("incorrect supply info: expected %+v, got %+v", expected, actual)
}
}
func TestSupplyRewards(t *testing.T) {
var (
config = *params.AllEthashProtocolChanges
gspec = &core.Genesis{
Config: &config,
}
)
expected := live.SupplyInfo{
Delta: new(big.Int).Mul(common.Big2, big.NewInt(params.Ether)),
Reward: new(big.Int).Mul(common.Big2, big.NewInt(params.Ether)),
Withdrawals: common.Big0,
Burn: common.Big0,
Number: 1,
Hash: common.HexToHash("0xcbb08370505be503dafedc4e96d139ea27aba3cbc580148568b8a307b3f51052"),
ParentHash: common.HexToHash("0xadeda0a83e337b6c073e3f0e9a17531a04009b397a9588c093b628f21b8bc5a3"),
}
out, _, err := testSupplyTracer(gspec, emptyBlockGenerationFunc)
if err != nil {
t.Fatalf("failed to test supply tracer: %v", err)
}
actual := out[expected.Number]
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("incorrect supply info: expected %+v, got %+v", 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(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 = live.SupplyInfo{
Delta: new(big.Int).Sub(reward, burn),
Reward: reward,
Withdrawals: common.Big0,
Burn: burn,
Number: 1,
Hash: head.Hash(),
ParentHash: head.ParentHash,
}
)
actual := out[expected.Number]
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("incorrect supply info: expected %+v, got %+v", 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(gspec, withdrawalsBlockGenerationFunc)
if err != nil {
t.Fatalf("failed to test supply tracer: %v", err)
}
var (
head = chain.CurrentBlock()
expected = live.SupplyInfo{
Delta: big.NewInt(1337000000000),
Reward: common.Big0,
Withdrawals: big.NewInt(1337000000000),
Burn: common.Big0,
Number: 1,
Hash: head.Hash(),
ParentHash: head.ParentHash,
}
actual = out[expected.Number]
)
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("incorrect supply info: expected %+v, got %+v", 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(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 := live.SupplyInfo{
Delta: big.NewInt(-55294500000000),
Reward: common.Big0,
Withdrawals: common.Big0,
Burn: big.NewInt(55294500000000),
Number: 1,
Hash: head.Hash(),
ParentHash: head.ParentHash,
}
actual := preCancunOutput[expected.Number]
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Pre-cancun incorrect supply info: expected %+v, got %+v", expected, actual)
}
// 2. Test post Cancun
cancunTime := uint64(0)
gspec.Config.ShanghaiTime = &cancunTime
gspec.Config.CancunTime = &cancunTime
postCancunOutput, postCancunChain, err := testSupplyTracer(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 = live.SupplyInfo{
Delta: big.NewInt(-55289500000000),
Reward: common.Big0,
Withdrawals: common.Big0,
Burn: big.NewInt(55289500000000),
Number: 1,
Hash: head.Hash(),
ParentHash: head.ParentHash,
}
actual = postCancunOutput[expected.Number]
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Post-cancun incorrect supply info: expected %+v, got %+v", 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(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)
blockBurn := new(big.Int).Mul(block.BaseFee(), big.NewInt(int64(block.GasUsed())))
totalBurn := new(big.Int).Add(blockBurn, eth5) // 5ETH burned from contract B
expected := live.SupplyInfo{
Delta: new(big.Int).Neg(totalBurn),
Reward: common.Big0,
Withdrawals: common.Big0,
Burn: totalBurn,
Number: 1,
Hash: block.Hash(),
ParentHash: block.ParentHash(),
}
actual := output[expected.Number]
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("incorrect supply info: expected %+v, got %+v", expected, actual)
}
}
func testSupplyTracer(genesis *core.Genesis, gen func(*core.BlockGen)) ([]live.SupplyInfo, *core.BlockChain, error) {
var (
engine = beacon.New(ethash.NewFaker())
)
traceOutputPath := os.TempDir()
traceOutputFilename := path.Join(traceOutputPath, "supply.jsonl")
defer os.Remove(traceOutputFilename)
// Load supply tracer
tracer, err := tracers.LiveDirectory.New("supply", []byte(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
// TODO: replace file to pass 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 []live.SupplyInfo
scanner := bufio.NewScanner(file)
for scanner.Scan() {
blockBytes := scanner.Bytes()
var info live.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
}

239
eth/tracers/live/supply.go Normal file
View file

@ -0,0 +1,239 @@
package live
import (
"encoding/json"
"errors"
"fmt"
"log"
"math/big"
"path/filepath"
"github.com/ethereum/go-ethereum/common"
"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"
"gopkg.in/natefinch/lumberjack.v2"
)
func init() {
tracers.LiveDirectory.Register("supply", newSupply)
}
type SupplyInfo struct {
Delta *big.Int `json:"delta"`
Reward *big.Int `json:"reward"`
Withdrawals *big.Int `json:"withdrawals"`
Burn *big.Int `json:"burn"`
// Block info
Number uint64 `json:"blockNumber"`
Hash common.Hash `json:"hash"`
ParentHash common.Hash `json:"parentHash"`
}
func (s *SupplyInfo) burn(amount *big.Int) {
s.Burn.Add(s.Burn, amount)
s.Delta.Sub(s.Delta, amount)
}
type supplyTxCallstack struct {
calls []supplyTxCallstack
burn *big.Int
}
type Supply struct {
delta SupplyInfo
txCallstack []supplyTxCallstack // Callstack for current transaction
logger *log.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
loggerOutput := &lumberjack.Logger{
Filename: filepath.Join(config.Path, "supply.jsonl"),
}
if config.MaxSize > 0 {
loggerOutput.MaxSize = config.MaxSize
}
logger := log.New(loggerOutput, "", 0)
supplyInfo := newSupplyInfo()
t := &Supply{
delta: supplyInfo,
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,
}, nil
}
func newSupplyInfo() SupplyInfo {
return SupplyInfo{
Delta: big.NewInt(0),
Reward: big.NewInt(0),
Withdrawals: big.NewInt(0),
Burn: 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(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(burn)
}
}
func (s *Supply) OnBlockEnd(err error) {
out, _ := json.Marshal(s.delta)
s.logger.Println(string(out))
}
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.Delta.Add(s.delta.Delta, account.Balance)
}
out, _ := json.Marshal(s.delta)
s.logger.Println(string(out))
}
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.Reward.Add(s.delta.Reward, diff)
case tracing.BalanceIncreaseWithdrawal:
s.delta.Withdrawals.Add(s.delta.Withdrawals, diff)
case tracing.BalanceDecreaseSelfdestructBurn:
// BalanceDecreaseSelfdestructBurn is non-reversible as it happens
// at the end of the transaction.
s.delta.Burn.Sub(s.delta.Burn, diff)
default:
// fmt.Printf("~~\tNo need to take action. Change reason: %v\n\n", reason)
return
}
s.delta.Delta.Add(s.delta.Delta, diff)
}
func (s *Supply) OnTxStart(vm *tracing.VMContext, tx *types.Transaction, from common.Address) {
s.txCallstack = make([]supplyTxCallstack, 0, 1)
}
// interalTxsHandler handles internal transactions burned amount
func (s *Supply) interalTxsHandler(call *supplyTxCallstack) {
// Handle Burned amount
if call.burn != nil {
s.delta.burn(call.burn)
}
if len(call.calls) > 0 {
// Recursivelly handle internal calls
for _, call := range call.calls {
callCopy := call
s.interalTxsHandler(&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.interalTxsHandler(&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)
}