@@ -39,24 +39,37 @@ func TestGetBalance(t *testing.T) {
3939 }
4040}
4141
42- func TestGetBalanceRejectsHistoricalState (t * testing.T ) {
43- backend := & testBackend {
42+ // A numbered block, the head's own included, is read at that block rather than from the latest state.
43+ func TestGetBalanceReadsNumberedBlocksFromHistory (t * testing.T ) {
44+ address := common.Address {1 }
45+ api := & stateAPI {backend : & testBackend {
46+ blockNumber : func () uint64 { return 9 },
4447 balance : func (common.Address ) uint256.Int {
45- t .Fatal ("historical request read the current balance " )
48+ t .Fatal ("a numbered block read the latest state " )
4649 return uint256.Int {}
4750 },
51+ balanceAt : func (got common.Address , height int64 ) (uint256.Int , error ) {
52+ require .Equal (t , address , got )
53+ return * uint256 .NewInt (uint64 (height ) * 100 ), nil //nolint:gosec // test heights are small
54+ },
55+ }}
56+
57+ for height , want := range map [ethrpc.BlockNumber ]int64 {ethrpc .EarliestBlockNumber : 0 , 7 : 700 , 9 : 900 } {
58+ got , err := api .GetBalance (t .Context (), address , ethrpc .BlockNumberOrHashWithNumber (height ))
59+ require .NoError (t , err )
60+ require .Equal (t , want , got .ToInt ().Int64 (), "block %d" , height )
4861 }
49- api := & stateAPI {backend : backend }
50- address := common.Address {1 }
62+ _ , err := api .GetBalance (t .Context (), address , ethrpc .BlockNumberOrHashWithNumber (10 ))
63+ require .ErrorIs (t , err , errBlockAboveHead )
64+ _ , err = api .GetBalance (t .Context (), address , ethrpc .BlockNumberOrHashWithNumber (- 6 ))
65+ require .ErrorIs (t , err , errBlockTagUnsupported )
66+ }
5167
52- for _ , block := range []ethrpc.BlockNumberOrHash {
53- ethrpc .BlockNumberOrHashWithNumber (ethrpc .EarliestBlockNumber ),
54- ethrpc .BlockNumberOrHashWithNumber (7 ),
55- ethrpc .BlockNumberOrHashWithHash (common.Hash {2 }, true ),
56- {},
57- } {
58- got , err := api .GetBalance (t .Context (), address , block )
59- require .ErrorIs (t , err , errHistoricalStateUnsupported )
68+ func TestGetBalanceRejectsABlockHash (t * testing.T ) {
69+ api := & stateAPI {backend : & testBackend {}}
70+ for _ , block := range []ethrpc.BlockNumberOrHash {ethrpc .BlockNumberOrHashWithHash (common.Hash {2 }, true ), {}} {
71+ got , err := api .GetBalance (t .Context (), common.Address {1 }, block )
72+ require .ErrorIs (t , err , errBlockHashUnsupported )
6073 require .Nil (t , got )
6174 }
6275}
@@ -105,24 +118,24 @@ func TestGetCode(t *testing.T) {
105118 }
106119}
107120
108- func TestGetCodeRejectsHistoricalState (t * testing.T ) {
109- backend := & testBackend {
110- code : func (common.Address ) ([]byte , error ) {
111- t .Fatal ("historical request read the current code" )
112- return nil , nil
113- },
114- }
115- api := & stateAPI {backend : backend }
121+ func TestGetCodeReadsNumberedBlocksFromHistory (t * testing.T ) {
116122 address := common.Address {1 }
123+ api := & stateAPI {backend : & testBackend {
124+ blockNumber : func () uint64 { return 9 },
125+ codeAt : func (got common.Address , height int64 ) ([]byte , error ) {
126+ require .Equal (t , address , got )
127+ require .Equal (t , int64 (7 ), height )
128+ return []byte {0xfe }, nil
129+ },
130+ }}
117131
118- for _ , block := range []ethrpc.BlockNumberOrHash {
119- ethrpc .BlockNumberOrHashWithNumber (ethrpc .EarliestBlockNumber ),
120- ethrpc .BlockNumberOrHashWithNumber (7 ),
121- ethrpc .BlockNumberOrHashWithHash (common.Hash {2 }, true ),
122- {},
123- } {
132+ got , err := api .GetCode (t .Context (), address , ethrpc .BlockNumberOrHashWithNumber (7 ))
133+ require .NoError (t , err )
134+ require .Equal (t , hexutil.Bytes {0xfe }, got )
135+
136+ for _ , block := range []ethrpc.BlockNumberOrHash {ethrpc .BlockNumberOrHashWithHash (common.Hash {2 }, true ), {}} {
124137 got , err := api .GetCode (t .Context (), address , block )
125- require .ErrorIs (t , err , errHistoricalStateUnsupported )
138+ require .ErrorIs (t , err , errBlockHashUnsupported )
126139 require .Nil (t , got )
127140 }
128141}
@@ -148,6 +161,8 @@ func TestHandlerServesGetCode(t *testing.T) {
148161 }
149162 return nil , nil
150163 },
164+ blockNumber : func () uint64 { return 5 },
165+ codeAt : func (common.Address , int64 ) ([]byte , error ) { return nil , nil },
151166 }
152167 handler , err := newHandler (backend , evmonly .NewMemoryReceiptStore ())
153168 require .NoError (t , err )
@@ -167,8 +182,9 @@ func TestHandlerServesGetCode(t *testing.T) {
167182 require .NoError (t , client .CallContext (t .Context (), & raw , "eth_getCode" , common.Address {9 }, "latest" ))
168183 require .JSONEq (t , `"0x"` , string (raw ))
169184
170- err = client .CallContext (t .Context (), & got , "eth_getCode" , contract , "0x1" )
171- require .ErrorContains (t , err , errHistoricalStateUnsupported .Error ())
185+ // Before the contract was deployed.
186+ require .NoError (t , client .CallContext (t .Context (), & raw , "eth_getCode" , contract , "0x1" ))
187+ require .JSONEq (t , `"0x"` , string (raw ))
172188}
173189
174190func TestGetStorageAt (t * testing.T ) {
@@ -196,11 +212,9 @@ func TestGetStorageAt(t *testing.T) {
196212 {name : "unprefixed key" , key : "ff" , block : latest , wantKey : common .BigToHash (big .NewInt (0xff ))},
197213 {name : "uppercase prefix" , key : "0X1" , block : latest , wantKey : common .BigToHash (big .NewInt (1 ))},
198214 {name : "empty key is slot zero" , key : "0x" , block : latest },
199- // Historical state, malformed keys, and backend failures are errors.
200- {name : "earliest" , key : "0x1" , block : ethrpc .BlockNumberOrHashWithNumber (ethrpc .EarliestBlockNumber ), wantErr : errHistoricalStateUnsupported },
201- {name : "explicit height" , key : "0x1" , block : ethrpc .BlockNumberOrHashWithNumber (7 ), wantErr : errHistoricalStateUnsupported },
202- {name : "block hash" , key : "0x1" , block : ethrpc .BlockNumberOrHashWithHash (common.Hash {2 }, true ), wantErr : errHistoricalStateUnsupported },
203- {name : "neither number nor hash" , key : "0x1" , wantErr : errHistoricalStateUnsupported },
215+ // Block hashes, malformed keys, and backend failures are errors.
216+ {name : "block hash" , key : "0x1" , block : ethrpc .BlockNumberOrHashWithHash (common.Hash {2 }, true ), wantErr : errBlockHashUnsupported },
217+ {name : "neither number nor hash" , key : "0x1" , wantErr : errBlockHashUnsupported },
204218 {name : "non-hex key" , key : "0xzz" , block : latest , wantErr : errStorageKeyInvalid },
205219 {name : "doubled prefix" , key : "0x0X1" , block : latest , wantErr : errStorageKeyInvalid },
206220 {name : "key longer than 32 bytes" , key : "0x01" + common.Hash {}.Hex ()[2 :], block : latest , wantErr : errStorageKeyTooLong },
@@ -232,16 +246,49 @@ func TestGetStorageAt(t *testing.T) {
232246 }
233247}
234248
249+ // A numbered block, the head's own included, is read at that block rather than from the latest state.
250+ func TestGetStorageAtReadsNumberedBlocksFromHistory (t * testing.T ) {
251+ address := common.Address {1 }
252+ slot := common .BigToHash (big .NewInt (1 ))
253+ api := & stateAPI {backend : & testBackend {
254+ blockNumber : func () uint64 { return 9 },
255+ storage : func (common.Address , common.Hash ) (common.Hash , error ) {
256+ t .Fatal ("a numbered block read the latest state" )
257+ return common.Hash {}, nil
258+ },
259+ storageAt : func (gotAddress common.Address , gotKey common.Hash , height int64 ) (common.Hash , error ) {
260+ require .Equal (t , address , gotAddress )
261+ require .Equal (t , slot , gotKey )
262+ return common.Hash {byte (height )}, nil //nolint:gosec // test heights are small
263+ },
264+ }}
265+
266+ for number , want := range map [ethrpc.BlockNumber ]common.Hash {ethrpc .EarliestBlockNumber : {0 }, 7 : {7 }, 9 : {9 }} {
267+ got , err := api .GetStorageAt (t .Context (), address , "0x1" , ethrpc .BlockNumberOrHashWithNumber (number ))
268+ require .NoError (t , err )
269+ require .Equal (t , hexutil .Bytes (want [:]), got , "block %d" , number )
270+ }
271+ _ , err := api .GetStorageAt (t .Context (), address , "0x1" , ethrpc .BlockNumberOrHashWithNumber (10 ))
272+ require .ErrorIs (t , err , errBlockAboveHead )
273+ }
274+
235275func TestHandlerServesGetStorageAt (t * testing.T ) {
236276 contract := common .HexToAddress ("0x6000000000000000000000000000000000000006" )
237277 slot := common .BigToHash (big .NewInt (1 ))
238278 backend := & testBackend {
279+ blockNumber : func () uint64 { return 5 },
239280 storage : func (address common.Address , key common.Hash ) (common.Hash , error ) {
240281 if address == contract && key == slot {
241282 return common .BigToHash (big .NewInt (0x2a )), nil
242283 }
243284 return common.Hash {}, nil
244285 },
286+ storageAt : func (address common.Address , key common.Hash , height int64 ) (common.Hash , error ) {
287+ if address == contract && key == slot && height == 3 {
288+ return common .BigToHash (big .NewInt (0x11 )), nil
289+ }
290+ return common.Hash {}, nil
291+ },
245292 }
246293 handler , err := newHandler (backend , evmonly .NewMemoryReceiptStore ())
247294 require .NoError (t , err )
@@ -260,6 +307,9 @@ func TestHandlerServesGetStorageAt(t *testing.T) {
260307 require .NoError (t , client .CallContext (t .Context (), & got , "eth_getStorageAt" , contract , "0x2" , "latest" ))
261308 require .Equal (t , common.Hash {}.Hex (), got .String ())
262309
263- err = client .CallContext (t .Context (), & got , "eth_getStorageAt" , contract , "0x1" , "0x1" )
264- require .ErrorContains (t , err , errHistoricalStateUnsupported .Error ())
310+ // A numbered block below the head reads that block's history.
311+ require .NoError (t , client .CallContext (t .Context (), & got , "eth_getStorageAt" , contract , "0x1" , "0x3" ))
312+ require .Equal (t , common .BigToHash (big .NewInt (0x11 )).Hex (), got .String ())
313+ require .NoError (t , client .CallContext (t .Context (), & got , "eth_getStorageAt" , contract , "0x1" , "0x2" ))
314+ require .Equal (t , common.Hash {}.Hex (), got .String ())
265315}
0 commit comments