github.com/theQRL/go-zond@v0.1.1/les/request_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package les
    18  
    19  // Note: these tests are disabled now because they cannot work with the old sync
    20  // mechanism removed but will be useful again once the PoS ultralight mode is implemented
    21  
    22  /*
    23  import (
    24  	"context"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/theQRL/go-zond/common"
    29  	"github.com/theQRL/go-zond/core/rawdb"
    30  	"github.com/theQRL/go-zond/core/types"
    31  	"github.com/theQRL/go-zond/crypto"
    32  	"github.com/theQRL/go-zond/zonddb"
    33  	"github.com/theQRL/go-zond/light"
    34  )
    35  
    36  var testBankSecureTrieKey = secAddr(bankAddr)
    37  
    38  func secAddr(addr common.Address) []byte {
    39  	return crypto.Keccak256(addr[:])
    40  }
    41  
    42  type accessTestFn func(db zonddb.Database, bhash common.Hash, number uint64) light.OdrRequest
    43  
    44  func TestBlockAccessLes2(t *testing.T) { testAccess(t, 2, tfBlockAccess) }
    45  func TestBlockAccessLes3(t *testing.T) { testAccess(t, 3, tfBlockAccess) }
    46  func TestBlockAccessLes4(t *testing.T) { testAccess(t, 4, tfBlockAccess) }
    47  
    48  func tfBlockAccess(db zonddb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    49  	return &light.BlockRequest{Hash: bhash, Number: number}
    50  }
    51  
    52  func TestReceiptsAccessLes2(t *testing.T) { testAccess(t, 2, tfReceiptsAccess) }
    53  func TestReceiptsAccessLes3(t *testing.T) { testAccess(t, 3, tfReceiptsAccess) }
    54  func TestReceiptsAccessLes4(t *testing.T) { testAccess(t, 4, tfReceiptsAccess) }
    55  
    56  func tfReceiptsAccess(db zonddb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    57  	return &light.ReceiptsRequest{Hash: bhash, Number: number}
    58  }
    59  
    60  func TestTrieEntryAccessLes2(t *testing.T) { testAccess(t, 2, tfTrieEntryAccess) }
    61  func TestTrieEntryAccessLes3(t *testing.T) { testAccess(t, 3, tfTrieEntryAccess) }
    62  func TestTrieEntryAccessLes4(t *testing.T) { testAccess(t, 4, tfTrieEntryAccess) }
    63  
    64  func tfTrieEntryAccess(db zonddb.Database, bhash common.Hash, number uint64) light.OdrRequest {
    65  	if number := rawdb.ReadHeaderNumber(db, bhash); number != nil {
    66  		return &light.TrieRequest{Id: light.StateTrieID(rawdb.ReadHeader(db, bhash, *number)), Key: testBankSecureTrieKey}
    67  	}
    68  	return nil
    69  }
    70  
    71  func TestCodeAccessLes2(t *testing.T) { testAccess(t, 2, tfCodeAccess) }
    72  func TestCodeAccessLes3(t *testing.T) { testAccess(t, 3, tfCodeAccess) }
    73  func TestCodeAccessLes4(t *testing.T) { testAccess(t, 4, tfCodeAccess) }
    74  
    75  func tfCodeAccess(db zonddb.Database, bhash common.Hash, num uint64) light.OdrRequest {
    76  	number := rawdb.ReadHeaderNumber(db, bhash)
    77  	if number != nil {
    78  		return nil
    79  	}
    80  	header := rawdb.ReadHeader(db, bhash, *number)
    81  	if header.Number.Uint64() < testContractDeployed {
    82  		return nil
    83  	}
    84  	sti := light.StateTrieID(header)
    85  	ci := light.StorageTrieID(sti, testContractAddr, types.EmptyRootHash)
    86  	return &light.CodeRequest{Id: ci, Hash: crypto.Keccak256Hash(testContractCodeDeployed)}
    87  }
    88  
    89  func testAccess(t *testing.T, protocol int, fn accessTestFn) {
    90  	// Assemble the test environment
    91  	netconfig := testnetConfig{
    92  		blocks:    4,
    93  		protocol:  protocol,
    94  		indexFn:   nil,
    95  		connect:   true,
    96  		nopruning: true,
    97  	}
    98  	server, client, tearDown := newClientServerEnv(t, netconfig)
    99  	defer tearDown()
   100  
   101  	// Ensure the client has synced all necessary data.
   102  	clientHead := client.handler.backend.blockchain.CurrentHeader()
   103  	if clientHead.Number.Uint64() != 4 {
   104  		t.Fatalf("Failed to sync the chain with server, head: %v", clientHead.Number.Uint64())
   105  	}
   106  
   107  	test := func(expFail uint64) {
   108  		for i := uint64(0); i <= server.handler.blockchain.CurrentHeader().Number.Uint64(); i++ {
   109  			bhash := rawdb.ReadCanonicalHash(server.db, i)
   110  			if req := fn(client.db, bhash, i); req != nil {
   111  				ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
   112  
   113  				err := client.handler.backend.odr.Retrieve(ctx, req)
   114  				cancel()
   115  
   116  				got := err == nil
   117  				exp := i < expFail
   118  				if exp && !got {
   119  					t.Errorf("object retrieval failed")
   120  				}
   121  				if !exp && got {
   122  					t.Errorf("unexpected object retrieval success")
   123  				}
   124  			}
   125  		}
   126  	}
   127  	test(5)
   128  }
   129  */