live: fix supply tracer uncle accounting

This commit is contained in:
Shane Bammel 2025-08-06 19:19:32 -05:00
parent 792de5d2e3
commit 54c2d4f0a3
2 changed files with 56 additions and 4 deletions

View file

@ -158,6 +158,54 @@ func TestSupplyRewards(t *testing.T) {
compareAsJSON(t, expected, actual)
}
func TestSupplyRewardsWithUncle(t *testing.T) {
var (
config = *params.AllEthashProtocolChanges
gspec = &core.Genesis{
Config: &config,
}
)
// Base reward for the miner
baseReward := ethash.ConstantinopleBlockReward.ToBig()
// Miner reward for uncle inclusion is 1/32 of the base reward
uncleInclusionReward := new(big.Int).Rsh(baseReward, 5)
// Uncle miner reward for an uncle that is 1 block behind is 7/8 of the base reward
uncleReward := big.NewInt(7)
uncleReward.Mul(uncleReward, baseReward).Rsh(uncleReward, 3)
totalReward := baseReward.Add(baseReward, uncleInclusionReward).Add(baseReward, uncleReward)
expected := supplyInfo{
Issuance: &supplyInfoIssuance{
Reward: (*hexutil.Big)(totalReward),
},
Number: 3,
Hash: common.HexToHash("0x0737d31f8671c18d32b5143833cfa600e4264df62324c9de569668c6de9eed6d"),
ParentHash: common.HexToHash("0x45af6557df87719cb3c7e6f8a98b61508ea74a797733191aececb4c2ec802447"),
}
// Generate a new chain where block 3 includes an uncle
uncleGenerationFunc := func(b *core.BlockGen) {
if b.Number().Uint64() == 3 {
prevBlock := b.PrevBlock(1) // Block 2
uncle := types.CopyHeader(prevBlock.Header())
uncle.Extra = []byte("uncle!")
b.AddUncle(uncle)
}
}
out, _, err := testSupplyTracer(t, gspec, uncleGenerationFunc, 3)
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
@ -542,7 +590,7 @@ func TestSupplySelfdestructItselfAndRevert(t *testing.T) {
compareAsJSON(t, expected, actual)
}
func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockGen)) ([]supplyInfo, *core.BlockChain, error) {
func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(b *core.BlockGen), numBlocks ...int) ([]supplyInfo, *core.BlockChain, error) {
engine := beacon.New(ethash.NewFaker())
traceOutputPath := filepath.ToSlash(t.TempDir())
@ -562,7 +610,12 @@ func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockG
}
defer chain.Stop()
_, blocks, _ := core.GenerateChainWithGenesis(genesis, engine, 1, func(i int, b *core.BlockGen) {
blockCount := 1
if len(numBlocks) > 0 {
blockCount = numBlocks[0]
}
_, blocks, _ := core.GenerateChainWithGenesis(genesis, engine, blockCount, func(i int, b *core.BlockGen) {
b.SetCoinbase(common.Address{1})
gen(b)
})

View file

@ -199,8 +199,7 @@ func (s *supplyTracer) onBalanceChange(a common.Address, prevBalance, newBalance
// NOTE: don't handle "BalanceIncreaseGenesisBalance" because it is handled in OnGenesisBlock
switch reason {
case tracing.BalanceIncreaseRewardMineUncle:
case tracing.BalanceIncreaseRewardMineBlock:
case tracing.BalanceIncreaseRewardMineBlock, tracing.BalanceIncreaseRewardMineUncle:
s.delta.Issuance.Reward.Add(s.delta.Issuance.Reward, diff)
case tracing.BalanceIncreaseWithdrawal:
s.delta.Issuance.Withdrawals.Add(s.delta.Issuance.Withdrawals, diff)