github.com/core-coin/go-core/v2@v2.1.9/les/request_test.go (about)

     1  // Copyright 2016 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-core library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package les
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/core-coin/go-core/v2/xcbdb"
    25  
    26  	"github.com/core-coin/go-core/v2/common"
    27  	"github.com/core-coin/go-core/v2/core/rawdb"
    28  	"github.com/core-coin/go-core/v2/crypto"
    29  	"github.com/core-coin/go-core/v2/light"
    30  )
    31  
    32  var testBankSecureTrieKey = secAddr(bankKey.Address())
    33  
    34  func secAddr(addr common.Address) []byte {
    35  	return crypto.SHA3(addr[:])
    36  }
    37  
    38  type accessTestFn func(db xcbdb.Database, bhash common.Hash, number uint64) light.OdrRequest
    39  
    40  func TestBlockAccessLes2(t *testing.T) { testAccess(t, 2, tfBlockAccess) }
    41  func TestBlockAccessLes3(t *testing.T) { testAccess(t, 3, tfBlockAccess) }
    42  
    43  func tfBlockAccess(db xcbdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    44  	return &light.BlockRequest{Hash: bhash, Number: number}
    45  }
    46  
    47  func TestReceiptsAccessLes2(t *testing.T) { testAccess(t, 2, tfReceiptsAccess) }
    48  func TestReceiptsAccessLes3(t *testing.T) { testAccess(t, 3, tfReceiptsAccess) }
    49  
    50  func tfReceiptsAccess(db xcbdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    51  	return &light.ReceiptsRequest{Hash: bhash, Number: number}
    52  }
    53  
    54  func TestTrieEntryAccessLes2(t *testing.T) { testAccess(t, 2, tfTrieEntryAccess) }
    55  func TestTrieEntryAccessLes3(t *testing.T) { testAccess(t, 3, tfTrieEntryAccess) }
    56  
    57  func tfTrieEntryAccess(db xcbdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    58  	if number := rawdb.ReadHeaderNumber(db, bhash); number != nil {
    59  		return &light.TrieRequest{Id: light.StateTrieID(rawdb.ReadHeader(db, bhash, *number)), Key: testBankSecureTrieKey}
    60  	}
    61  	return nil
    62  }
    63  
    64  func TestCodeAccessLes2(t *testing.T) { testAccess(t, 2, tfCodeAccess) }
    65  func TestCodeAccessLes3(t *testing.T) { testAccess(t, 3, tfCodeAccess) }
    66  
    67  func tfCodeAccess(db xcbdb.Database, bhash common.Hash, num uint64) light.OdrRequest {
    68  	number := rawdb.ReadHeaderNumber(db, bhash)
    69  	if number != nil {
    70  		return nil
    71  	}
    72  	header := rawdb.ReadHeader(db, bhash, *number)
    73  	if header.Number.Uint64() < testContractDeployed {
    74  		return nil
    75  	}
    76  	sti := light.StateTrieID(header)
    77  	ci := light.StorageTrieID(sti, crypto.SHA3Hash(testContractAddr[:]), common.Hash{})
    78  	return &light.CodeRequest{Id: ci, Hash: crypto.SHA3Hash(testContractCodeDeployed)}
    79  }
    80  
    81  func testAccess(t *testing.T, protocol int, fn accessTestFn) {
    82  	// Assemble the test environment
    83  	server, client, tearDown := newClientServerEnv(t, 4, protocol, nil, nil, 0, false, true, true)
    84  	defer tearDown()
    85  
    86  	// Ensure the client has synced all necessary data.
    87  	clientHead := client.handler.backend.blockchain.CurrentHeader()
    88  	if clientHead.Number.Uint64() != 4 {
    89  		t.Fatalf("Failed to sync the chain with server, head: %v", clientHead.Number.Uint64())
    90  	}
    91  
    92  	test := func(expFail uint64) {
    93  		for i := uint64(0); i <= server.handler.blockchain.CurrentHeader().Number.Uint64(); i++ {
    94  			bhash := rawdb.ReadCanonicalHash(server.db, i)
    95  			if req := fn(client.db, bhash, i); req != nil {
    96  				ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
    97  				err := client.handler.backend.odr.Retrieve(ctx, req)
    98  				cancel()
    99  
   100  				got := err == nil
   101  				exp := i < expFail
   102  				if exp && !got {
   103  					t.Errorf("object retrieval failed")
   104  				}
   105  				if !exp && got {
   106  					t.Errorf("unexpected object retrieval success")
   107  				}
   108  			}
   109  		}
   110  	}
   111  	test(5)
   112  }