@@ -19,7 +19,10 @@ package core
1919import (
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+
31140func ExampleGenerateChain () {
32141 var (
33142 key1 , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
0 commit comments