@@ -278,6 +278,31 @@ func testGetCode(t *testing.T, protocol int) {
278278 }
279279}
280280
281+ // Tests that the stale contract codes can't be retrieved based on account addresses.
282+ func TestGetStaleCodeLes2 (t * testing.T ) { testGetStaleCode (t , 2 ) }
283+ func TestGetStaleCodeLes3 (t * testing.T ) { testGetStaleCode (t , 3 ) }
284+
285+ func testGetStaleCode (t * testing.T , protocol int ) {
286+ server , tearDown := newServerEnv (t , core .TriesInMemory + 4 , protocol , nil )
287+ defer tearDown ()
288+ bc := server .pm .blockchain .(* core.BlockChain )
289+
290+ check := func (number uint64 , expected [][]byte ) {
291+ req := & CodeReq {
292+ BHash : bc .GetHeaderByNumber (number ).Hash (),
293+ AccKey : crypto .Keccak256 (testContractAddr [:]),
294+ }
295+ cost := server .tPeer .GetRequestCost (GetCodeMsg , 1 )
296+ sendRequest (server .tPeer .app , GetCodeMsg , 42 , cost , []* CodeReq {req })
297+ if err := expectResponse (server .tPeer .app , CodeMsg , 42 , testBufLimit , expected ); err != nil {
298+ t .Errorf ("codes mismatch: %v" , err )
299+ }
300+ }
301+ check (0 , [][]byte {}) // Non-exist contract
302+ check (testContractDeployed , [][]byte {}) // Stale contract
303+ check (bc .CurrentHeader ().Number .Uint64 (), [][]byte {testContractCodeDeployed }) // Fresh contract
304+ }
305+
281306// Tests that the transaction receipts can be retrieved based on hashes.
282307func TestGetReceiptLes2 (t * testing.T ) { testGetReceipt (t , 2 ) }
283308
@@ -338,6 +363,43 @@ func testGetProofs(t *testing.T, protocol int) {
338363 }
339364}
340365
366+ // Tests that the stale contract codes can't be retrieved based on account addresses.
367+ func TestGetStaleProofLes2 (t * testing.T ) { testGetStaleProof (t , 2 ) }
368+ func TestGetStaleProofLes3 (t * testing.T ) { testGetStaleProof (t , 3 ) }
369+
370+ func testGetStaleProof (t * testing.T , protocol int ) {
371+ server , tearDown := newServerEnv (t , core .TriesInMemory + 4 , protocol , nil )
372+ defer tearDown ()
373+ bc := server .pm .blockchain .(* core.BlockChain )
374+
375+ check := func (number uint64 , wantOK bool ) {
376+ var (
377+ header = bc .GetHeaderByNumber (number )
378+ account = crypto .Keccak256 (testBankAddress .Bytes ())
379+ )
380+ req := & ProofReq {
381+ BHash : header .Hash (),
382+ Key : account ,
383+ }
384+ cost := server .tPeer .GetRequestCost (GetProofsV2Msg , 1 )
385+ sendRequest (server .tPeer .app , GetProofsV2Msg , 42 , cost , []* ProofReq {req })
386+
387+ var expected []rlp.RawValue
388+ if wantOK {
389+ proofsV2 := light .NewNodeSet ()
390+ t , _ := trie .New (header .Root , trie .NewDatabase (server .db ))
391+ t .Prove (crypto .Keccak256 (account ), 0 , proofsV2 )
392+ expected = proofsV2 .NodeList ()
393+ }
394+ if err := expectResponse (server .tPeer .app , ProofsV2Msg , 42 , testBufLimit , expected ); err != nil {
395+ t .Errorf ("codes mismatch: %v" , err )
396+ }
397+ }
398+ check (0 , false ) // Non-exist proof
399+ check (2 , false ) // Stale proof
400+ check (bc .CurrentHeader ().Number .Uint64 (), true ) // Fresh proof
401+ }
402+
341403// Tests that CHT proofs can be correctly retrieved.
342404func TestGetCHTProofsLes2 (t * testing.T ) { testGetCHTProofs (t , 2 ) }
343405
0 commit comments