Skip to content

Commit 2ea48f8

Browse files
Rjectedrjl493456442fjl
authored
core: improve withdrawal index assignment in GenerateChain (#26756)
This fixes an issue where the withdrawal index was not calculated correctly for multiple withdrawals in a single block. Co-authored-by: Gary Rong <[email protected]> Co-authored-by: Felix Lange <[email protected]>
1 parent 2ad150d commit 2ea48f8

File tree

2 files changed

+126
-9
lines changed

2 files changed

+126
-9
lines changed

core/chain_makers.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,31 @@ func (b *BlockGen) AddUncle(h *types.Header) {
207207
}
208208

209209
// AddWithdrawal adds a withdrawal to the generated block.
210-
func (b *BlockGen) AddWithdrawal(w *types.Withdrawal) {
211-
// The withdrawal will be assigned the next valid index.
212-
var idx uint64
210+
// It returns the withdrawal index.
211+
func (b *BlockGen) AddWithdrawal(w *types.Withdrawal) uint64 {
212+
cpy := *w
213+
cpy.Index = b.nextWithdrawalIndex()
214+
b.withdrawals = append(b.withdrawals, &cpy)
215+
return cpy.Index
216+
}
217+
218+
// nextWithdrawalIndex computes the index of the next withdrawal.
219+
func (b *BlockGen) nextWithdrawalIndex() uint64 {
220+
if len(b.withdrawals) != 0 {
221+
return b.withdrawals[len(b.withdrawals)-1].Index + 1
222+
}
213223
for i := b.i - 1; i >= 0; i-- {
214224
if wd := b.chain[i].Withdrawals(); len(wd) != 0 {
215-
idx = wd[len(wd)-1].Index + 1
216-
break
225+
return wd[len(wd)-1].Index + 1
217226
}
218227
if i == 0 {
219-
// Correctly set the index if no parent had withdrawals
228+
// Correctly set the index if no parent had withdrawals.
220229
if wd := b.parent.Withdrawals(); len(wd) != 0 {
221-
idx = wd[len(wd)-1].Index + 1
230+
return wd[len(wd)-1].Index + 1
222231
}
223232
}
224233
}
225-
w.Index = idx
226-
b.withdrawals = append(b.withdrawals, w)
234+
return 0
227235
}
228236

229237
// PrevBlock returns a previously generated block by number. It panics if

core/chain_makers_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ package core
1919
import (
2020
"fmt"
2121
"math/big"
22+
"testing"
2223

24+
"github.com/ethereum/go-ethereum/common"
25+
"github.com/ethereum/go-ethereum/consensus/beacon"
2326
"github.com/ethereum/go-ethereum/consensus/ethash"
2427
"github.com/ethereum/go-ethereum/core/rawdb"
2528
"github.com/ethereum/go-ethereum/core/types"
@@ -28,6 +31,112 @@ import (
2831
"github.com/ethereum/go-ethereum/params"
2932
)
3033

34+
func TestGenerateWithdrawalChain(t *testing.T) {
35+
var (
36+
keyHex = "9c647b8b7c4e7c3490668fb6c11473619db80c93704c70893d3813af4090c39c"
37+
key, _ = crypto.HexToECDSA(keyHex)
38+
address = crypto.PubkeyToAddress(key.PublicKey) // 658bdf435d810c91414ec09147daa6db62406379
39+
aa = common.Address{0xaa}
40+
bb = common.Address{0xbb}
41+
funds = big.NewInt(0).Mul(big.NewInt(1337), big.NewInt(params.Ether))
42+
config = *params.AllEthashProtocolChanges
43+
gspec = &Genesis{
44+
Config: &config,
45+
Alloc: GenesisAlloc{address: {Balance: funds}},
46+
BaseFee: big.NewInt(params.InitialBaseFee),
47+
Difficulty: common.Big1,
48+
GasLimit: 5_000_000,
49+
}
50+
gendb = rawdb.NewMemoryDatabase()
51+
signer = types.LatestSigner(gspec.Config)
52+
db = rawdb.NewMemoryDatabase()
53+
)
54+
55+
config.TerminalTotalDifficultyPassed = true
56+
config.TerminalTotalDifficulty = common.Big0
57+
config.ShanghaiTime = u64(0)
58+
59+
// init 0xaa with some storage elements
60+
storage := make(map[common.Hash]common.Hash)
61+
storage[common.Hash{0x00}] = common.Hash{0x00}
62+
storage[common.Hash{0x01}] = common.Hash{0x01}
63+
storage[common.Hash{0x02}] = common.Hash{0x02}
64+
storage[common.Hash{0x03}] = common.HexToHash("0303")
65+
gspec.Alloc[aa] = GenesisAccount{
66+
Balance: common.Big1,
67+
Nonce: 1,
68+
Storage: storage,
69+
Code: common.Hex2Bytes("6042"),
70+
}
71+
gspec.Alloc[bb] = GenesisAccount{
72+
Balance: common.Big2,
73+
Nonce: 1,
74+
Storage: storage,
75+
Code: common.Hex2Bytes("600154600354"),
76+
}
77+
78+
genesis := gspec.MustCommit(gendb)
79+
80+
chain, _ := GenerateChain(gspec.Config, genesis, beacon.NewFaker(), gendb, 4, func(i int, gen *BlockGen) {
81+
tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(address), address, big.NewInt(1000), params.TxGas, new(big.Int).Add(gen.BaseFee(), common.Big1), nil), signer, key)
82+
gen.AddTx(tx)
83+
if i == 1 {
84+
gen.AddWithdrawal(&types.Withdrawal{
85+
Validator: 42,
86+
Address: common.Address{0xee},
87+
Amount: 1337,
88+
})
89+
gen.AddWithdrawal(&types.Withdrawal{
90+
Validator: 13,
91+
Address: common.Address{0xee},
92+
Amount: 1,
93+
})
94+
}
95+
if i == 3 {
96+
gen.AddWithdrawal(&types.Withdrawal{
97+
Validator: 42,
98+
Address: common.Address{0xee},
99+
Amount: 1337,
100+
})
101+
gen.AddWithdrawal(&types.Withdrawal{
102+
Validator: 13,
103+
Address: common.Address{0xee},
104+
Amount: 1,
105+
})
106+
}
107+
})
108+
109+
// Import the chain. This runs all block validation rules.
110+
blockchain, _ := NewBlockChain(db, nil, gspec, nil, beacon.NewFaker(), vm.Config{}, nil, nil)
111+
defer blockchain.Stop()
112+
113+
if i, err := blockchain.InsertChain(chain); err != nil {
114+
fmt.Printf("insert error (block %d): %v\n", chain[i].NumberU64(), err)
115+
return
116+
}
117+
118+
// enforce that withdrawal indexes are monotonically increasing from 0
119+
var (
120+
withdrawalIndex uint64
121+
head = blockchain.CurrentBlock().NumberU64()
122+
)
123+
for i := 0; i < int(head); i++ {
124+
block := blockchain.GetBlockByNumber(uint64(i))
125+
if block == nil {
126+
t.Fatalf("block %d not found", i)
127+
}
128+
if len(block.Withdrawals()) == 0 {
129+
continue
130+
}
131+
for j := 0; j < len(block.Withdrawals()); j++ {
132+
if block.Withdrawals()[j].Index != withdrawalIndex {
133+
t.Fatalf("withdrawal index %d does not equal expected index %d", block.Withdrawals()[j].Index, withdrawalIndex)
134+
}
135+
withdrawalIndex += 1
136+
}
137+
}
138+
}
139+
31140
func ExampleGenerateChain() {
32141
var (
33142
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")

0 commit comments

Comments
 (0)