github.com/calmw/ethereum@v0.1.1/les/fetcher_test.go (about)

     1  // Copyright 2019 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  import (
    20  	"math/big"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/calmw/ethereum/consensus/ethash"
    25  	"github.com/calmw/ethereum/core"
    26  	"github.com/calmw/ethereum/core/rawdb"
    27  	"github.com/calmw/ethereum/core/types"
    28  	"github.com/calmw/ethereum/params"
    29  )
    30  
    31  // verifyImportEvent verifies that one single event arrive on an import channel.
    32  func verifyImportEvent(t *testing.T, imported chan interface{}, arrive bool) {
    33  	if arrive {
    34  		select {
    35  		case <-imported:
    36  		case <-time.After(time.Second):
    37  			t.Fatalf("import timeout")
    38  		}
    39  	} else {
    40  		select {
    41  		case <-imported:
    42  			t.Fatalf("import invoked")
    43  		case <-time.After(20 * time.Millisecond):
    44  		}
    45  	}
    46  }
    47  
    48  // verifyImportDone verifies that no more events are arriving on an import channel.
    49  func verifyImportDone(t *testing.T, imported chan interface{}) {
    50  	select {
    51  	case <-imported:
    52  		t.Fatalf("extra block imported")
    53  	case <-time.After(50 * time.Millisecond):
    54  	}
    55  }
    56  
    57  // verifyChainHeight verifies the chain height is as expected.
    58  func verifyChainHeight(t *testing.T, fetcher *lightFetcher, height uint64) {
    59  	local := fetcher.chain.CurrentHeader().Number.Uint64()
    60  	if local != height {
    61  		t.Fatalf("chain height mismatch, got %d, want %d", local, height)
    62  	}
    63  }
    64  
    65  func TestSequentialAnnouncementsLes2(t *testing.T) { testSequentialAnnouncements(t, 2) }
    66  func TestSequentialAnnouncementsLes3(t *testing.T) { testSequentialAnnouncements(t, 3) }
    67  
    68  func testSequentialAnnouncements(t *testing.T, protocol int) {
    69  	netconfig := testnetConfig{
    70  		blocks:    4,
    71  		protocol:  protocol,
    72  		nopruning: true,
    73  	}
    74  	s, c, teardown := newClientServerEnv(t, netconfig)
    75  	defer teardown()
    76  
    77  	// Create connected peer pair, the initial signal from LES server
    78  	// is discarded to prevent syncing.
    79  	p1, _, err := newTestPeerPair("peer", protocol, s.handler, c.handler, true)
    80  	if err != nil {
    81  		t.Fatalf("Failed to create peer pair %v", err)
    82  	}
    83  	importCh := make(chan interface{})
    84  	c.handler.fetcher.newHeadHook = func(header *types.Header) {
    85  		importCh <- header
    86  	}
    87  	for i := uint64(1); i <= s.backend.Blockchain().CurrentHeader().Number.Uint64(); i++ {
    88  		header := s.backend.Blockchain().GetHeaderByNumber(i)
    89  		hash, number := header.Hash(), header.Number.Uint64()
    90  		td := rawdb.ReadTd(s.db, hash, number)
    91  
    92  		announce := announceData{hash, number, td, 0, nil}
    93  		if p1.cpeer.announceType == announceTypeSigned {
    94  			announce.sign(s.handler.server.privateKey)
    95  		}
    96  		p1.cpeer.sendAnnounce(announce)
    97  		verifyImportEvent(t, importCh, true)
    98  	}
    99  	verifyImportDone(t, importCh)
   100  	verifyChainHeight(t, c.handler.fetcher, 4)
   101  }
   102  
   103  func TestGappedAnnouncementsLes2(t *testing.T) { testGappedAnnouncements(t, 2) }
   104  func TestGappedAnnouncementsLes3(t *testing.T) { testGappedAnnouncements(t, 3) }
   105  
   106  func testGappedAnnouncements(t *testing.T, protocol int) {
   107  	netconfig := testnetConfig{
   108  		blocks:    4,
   109  		protocol:  protocol,
   110  		nopruning: true,
   111  	}
   112  	s, c, teardown := newClientServerEnv(t, netconfig)
   113  	defer teardown()
   114  
   115  	// Create connected peer pair, the initial signal from LES server
   116  	// is discarded to prevent syncing.
   117  	peer, _, err := newTestPeerPair("peer", protocol, s.handler, c.handler, true)
   118  	if err != nil {
   119  		t.Fatalf("Failed to create peer pair %v", err)
   120  	}
   121  	done := make(chan *types.Header, 1)
   122  	c.handler.fetcher.newHeadHook = func(header *types.Header) { done <- header }
   123  
   124  	// Prepare announcement by latest header.
   125  	latest := s.backend.Blockchain().CurrentHeader()
   126  	hash, number := latest.Hash(), latest.Number.Uint64()
   127  	td := rawdb.ReadTd(s.db, hash, number)
   128  
   129  	// Sign the announcement if necessary.
   130  	announce := announceData{hash, number, td, 0, nil}
   131  	if peer.cpeer.announceType == announceTypeSigned {
   132  		announce.sign(s.handler.server.privateKey)
   133  	}
   134  	peer.cpeer.sendAnnounce(announce)
   135  
   136  	<-done // Wait syncing
   137  	verifyChainHeight(t, c.handler.fetcher, 4)
   138  
   139  	// Send a reorged announcement
   140  	blocks, _ := core.GenerateChain(rawdb.ReadChainConfig(s.db, s.backend.Blockchain().Genesis().Hash()), s.backend.Blockchain().GetBlockByNumber(3),
   141  		ethash.NewFaker(), s.db, 2, func(i int, gen *core.BlockGen) {
   142  			gen.OffsetTime(-9) // higher block difficulty
   143  		})
   144  	s.backend.Blockchain().InsertChain(blocks)
   145  
   146  	<-done // Wait syncing
   147  	verifyChainHeight(t, c.handler.fetcher, 5)
   148  }
   149  
   150  func TestInvalidAnnouncesLES2(t *testing.T) { testInvalidAnnounces(t, lpv2) }
   151  func TestInvalidAnnouncesLES3(t *testing.T) { testInvalidAnnounces(t, lpv3) }
   152  func TestInvalidAnnouncesLES4(t *testing.T) { testInvalidAnnounces(t, lpv4) }
   153  
   154  func testInvalidAnnounces(t *testing.T, protocol int) {
   155  	netconfig := testnetConfig{
   156  		blocks:    4,
   157  		protocol:  protocol,
   158  		nopruning: true,
   159  	}
   160  	s, c, teardown := newClientServerEnv(t, netconfig)
   161  	defer teardown()
   162  
   163  	// Create connected peer pair, the initial signal from LES server
   164  	// is discarded to prevent syncing.
   165  	peer, _, err := newTestPeerPair("peer", lpv3, s.handler, c.handler, true)
   166  	if err != nil {
   167  		t.Fatalf("Failed to create peer pair %v", err)
   168  	}
   169  	done := make(chan *types.Header, 1)
   170  	c.handler.fetcher.newHeadHook = func(header *types.Header) { done <- header }
   171  
   172  	// Prepare announcement by latest header.
   173  	headerOne := s.backend.Blockchain().GetHeaderByNumber(1)
   174  	hash, number := headerOne.Hash(), headerOne.Number.Uint64()
   175  	td := big.NewInt(params.GenesisDifficulty.Int64() + 200) // bad td
   176  
   177  	// Sign the announcement if necessary.
   178  	announce := announceData{hash, number, td, 0, nil}
   179  	if peer.cpeer.announceType == announceTypeSigned {
   180  		announce.sign(s.handler.server.privateKey)
   181  	}
   182  	peer.cpeer.sendAnnounce(announce)
   183  	<-done // Wait syncing
   184  
   185  	// Ensure the bad peer is evicted
   186  	if c.handler.backend.peers.len() != 0 {
   187  		t.Fatalf("Failed to evict invalid peer")
   188  	}
   189  }