github.com/hardtosaygoodbye/go-ethereum@v1.10.16-0.20220122011429-97003b9e6c15/les/fetcher_test.go (about)

     1  // Copyright 2020 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/hardtosaygoodbye/go-ethereum/consensus/ethash"
    25  	"github.com/hardtosaygoodbye/go-ethereum/core"
    26  	"github.com/hardtosaygoodbye/go-ethereum/core/rawdb"
    27  	"github.com/hardtosaygoodbye/go-ethereum/core/types"
    28  	"github.com/hardtosaygoodbye/go-ethereum/p2p/enode"
    29  	"github.com/hardtosaygoodbye/go-ethereum/params"
    30  )
    31  
    32  // verifyImportEvent verifies that one single event arrive on an import channel.
    33  func verifyImportEvent(t *testing.T, imported chan interface{}, arrive bool) {
    34  	if arrive {
    35  		select {
    36  		case <-imported:
    37  		case <-time.After(time.Second):
    38  			t.Fatalf("import timeout")
    39  		}
    40  	} else {
    41  		select {
    42  		case <-imported:
    43  			t.Fatalf("import invoked")
    44  		case <-time.After(20 * time.Millisecond):
    45  		}
    46  	}
    47  }
    48  
    49  // verifyImportDone verifies that no more events are arriving on an import channel.
    50  func verifyImportDone(t *testing.T, imported chan interface{}) {
    51  	select {
    52  	case <-imported:
    53  		t.Fatalf("extra block imported")
    54  	case <-time.After(50 * time.Millisecond):
    55  	}
    56  }
    57  
    58  // verifyChainHeight verifies the chain height is as expected.
    59  func verifyChainHeight(t *testing.T, fetcher *lightFetcher, height uint64) {
    60  	local := fetcher.chain.CurrentHeader().Number.Uint64()
    61  	if local != height {
    62  		t.Fatalf("chain height mismatch, got %d, want %d", local, height)
    63  	}
    64  }
    65  
    66  func TestSequentialAnnouncementsLes2(t *testing.T) { testSequentialAnnouncements(t, 2) }
    67  func TestSequentialAnnouncementsLes3(t *testing.T) { testSequentialAnnouncements(t, 3) }
    68  
    69  func testSequentialAnnouncements(t *testing.T, protocol int) {
    70  	netconfig := testnetConfig{
    71  		blocks:    4,
    72  		protocol:  protocol,
    73  		nopruning: true,
    74  	}
    75  	s, c, teardown := newClientServerEnv(t, netconfig)
    76  	defer teardown()
    77  
    78  	// Create connected peer pair, the initial signal from LES server
    79  	// is discarded to prevent syncing.
    80  	p1, _, err := newTestPeerPair("peer", protocol, s.handler, c.handler, true)
    81  	if err != nil {
    82  		t.Fatalf("Failed to create peer pair %v", err)
    83  	}
    84  	importCh := make(chan interface{})
    85  	c.handler.fetcher.newHeadHook = func(header *types.Header) {
    86  		importCh <- header
    87  	}
    88  	for i := uint64(1); i <= s.backend.Blockchain().CurrentHeader().Number.Uint64(); i++ {
    89  		header := s.backend.Blockchain().GetHeaderByNumber(i)
    90  		hash, number := header.Hash(), header.Number.Uint64()
    91  		td := rawdb.ReadTd(s.db, hash, number)
    92  
    93  		announce := announceData{hash, number, td, 0, nil}
    94  		if p1.cpeer.announceType == announceTypeSigned {
    95  			announce.sign(s.handler.server.privateKey)
    96  		}
    97  		p1.cpeer.sendAnnounce(announce)
    98  		verifyImportEvent(t, importCh, true)
    99  	}
   100  	verifyImportDone(t, importCh)
   101  	verifyChainHeight(t, c.handler.fetcher, 4)
   102  }
   103  
   104  func TestGappedAnnouncementsLes2(t *testing.T) { testGappedAnnouncements(t, 2) }
   105  func TestGappedAnnouncementsLes3(t *testing.T) { testGappedAnnouncements(t, 3) }
   106  
   107  func testGappedAnnouncements(t *testing.T, protocol int) {
   108  	netconfig := testnetConfig{
   109  		blocks:    4,
   110  		protocol:  protocol,
   111  		nopruning: true,
   112  	}
   113  	s, c, teardown := newClientServerEnv(t, netconfig)
   114  	defer teardown()
   115  
   116  	// Create connected peer pair, the initial signal from LES server
   117  	// is discarded to prevent syncing.
   118  	peer, _, err := newTestPeerPair("peer", protocol, s.handler, c.handler, true)
   119  	if err != nil {
   120  		t.Fatalf("Failed to create peer pair %v", err)
   121  	}
   122  	done := make(chan *types.Header, 1)
   123  	c.handler.fetcher.newHeadHook = func(header *types.Header) { done <- header }
   124  
   125  	// Prepare announcement by latest header.
   126  	latest := s.backend.Blockchain().CurrentHeader()
   127  	hash, number := latest.Hash(), latest.Number.Uint64()
   128  	td := rawdb.ReadTd(s.db, hash, number)
   129  
   130  	// Sign the announcement if necessary.
   131  	announce := announceData{hash, number, td, 0, nil}
   132  	if peer.cpeer.announceType == announceTypeSigned {
   133  		announce.sign(s.handler.server.privateKey)
   134  	}
   135  	peer.cpeer.sendAnnounce(announce)
   136  
   137  	<-done // Wait syncing
   138  	verifyChainHeight(t, c.handler.fetcher, 4)
   139  
   140  	// Send a reorged announcement
   141  	blocks, _ := core.GenerateChain(rawdb.ReadChainConfig(s.db, s.backend.Blockchain().Genesis().Hash()), s.backend.Blockchain().GetBlockByNumber(3),
   142  		ethash.NewFaker(), s.db, 2, func(i int, gen *core.BlockGen) {
   143  			gen.OffsetTime(-9) // higher block difficulty
   144  		})
   145  	s.backend.Blockchain().InsertChain(blocks)
   146  
   147  	<-done // Wait syncing
   148  	verifyChainHeight(t, c.handler.fetcher, 5)
   149  }
   150  
   151  func TestTrustedAnnouncementsLes2(t *testing.T) { testTrustedAnnouncement(t, 2) }
   152  func TestTrustedAnnouncementsLes3(t *testing.T) { testTrustedAnnouncement(t, 3) }
   153  
   154  func testTrustedAnnouncement(t *testing.T, protocol int) {
   155  	var (
   156  		servers   []*testServer
   157  		teardowns []func()
   158  		nodes     []*enode.Node
   159  		ids       []string
   160  		cpeers    []*clientPeer
   161  		speers    []*serverPeer
   162  	)
   163  	for i := 0; i < 10; i++ {
   164  		s, n, teardown := newTestServerPeer(t, 10, protocol)
   165  
   166  		servers = append(servers, s)
   167  		nodes = append(nodes, n)
   168  		teardowns = append(teardowns, teardown)
   169  
   170  		// A half of them are trusted servers.
   171  		if i < 5 {
   172  			ids = append(ids, n.String())
   173  		}
   174  	}
   175  	netconfig := testnetConfig{
   176  		protocol:    protocol,
   177  		nopruning:   true,
   178  		ulcServers:  ids,
   179  		ulcFraction: 60,
   180  	}
   181  	_, c, teardown := newClientServerEnv(t, netconfig)
   182  	defer teardown()
   183  	defer func() {
   184  		for i := 0; i < len(teardowns); i++ {
   185  			teardowns[i]()
   186  		}
   187  	}()
   188  	// Connect all server instances.
   189  	for i := 0; i < len(servers); i++ {
   190  		sp, cp, err := connect(servers[i].handler, nodes[i].ID(), c.handler, protocol, true)
   191  		if err != nil {
   192  			t.Fatalf("connect server and client failed, err %s", err)
   193  		}
   194  		cpeers = append(cpeers, cp)
   195  		speers = append(speers, sp)
   196  	}
   197  	newHead := make(chan *types.Header, 1)
   198  	c.handler.fetcher.newHeadHook = func(header *types.Header) { newHead <- header }
   199  
   200  	check := func(height []uint64, expected uint64, callback func()) {
   201  		for i := 0; i < len(height); i++ {
   202  			for j := 0; j < len(servers); j++ {
   203  				h := servers[j].backend.Blockchain().GetHeaderByNumber(height[i])
   204  				hash, number := h.Hash(), h.Number.Uint64()
   205  				td := rawdb.ReadTd(servers[j].db, hash, number)
   206  
   207  				// Sign the announcement if necessary.
   208  				announce := announceData{hash, number, td, 0, nil}
   209  				p := cpeers[j]
   210  				if p.announceType == announceTypeSigned {
   211  					announce.sign(servers[j].handler.server.privateKey)
   212  				}
   213  				p.sendAnnounce(announce)
   214  			}
   215  		}
   216  		if callback != nil {
   217  			callback()
   218  		}
   219  		verifyChainHeight(t, c.handler.fetcher, expected)
   220  	}
   221  	check([]uint64{1}, 1, func() { <-newHead })   // Sequential announcements
   222  	check([]uint64{4}, 4, func() { <-newHead })   // ULC-style light syncing, rollback untrusted headers
   223  	check([]uint64{10}, 10, func() { <-newHead }) // Sync the whole chain.
   224  }
   225  
   226  func TestInvalidAnnouncesLES2(t *testing.T) { testInvalidAnnounces(t, lpv2) }
   227  func TestInvalidAnnouncesLES3(t *testing.T) { testInvalidAnnounces(t, lpv3) }
   228  func TestInvalidAnnouncesLES4(t *testing.T) { testInvalidAnnounces(t, lpv4) }
   229  
   230  func testInvalidAnnounces(t *testing.T, protocol int) {
   231  	netconfig := testnetConfig{
   232  		blocks:    4,
   233  		protocol:  protocol,
   234  		nopruning: true,
   235  	}
   236  	s, c, teardown := newClientServerEnv(t, netconfig)
   237  	defer teardown()
   238  
   239  	// Create connected peer pair, the initial signal from LES server
   240  	// is discarded to prevent syncing.
   241  	peer, _, err := newTestPeerPair("peer", lpv3, s.handler, c.handler, true)
   242  	if err != nil {
   243  		t.Fatalf("Failed to create peer pair %v", err)
   244  	}
   245  	done := make(chan *types.Header, 1)
   246  	c.handler.fetcher.newHeadHook = func(header *types.Header) { done <- header }
   247  
   248  	// Prepare announcement by latest header.
   249  	headerOne := s.backend.Blockchain().GetHeaderByNumber(1)
   250  	hash, number := headerOne.Hash(), headerOne.Number.Uint64()
   251  	td := big.NewInt(params.GenesisDifficulty.Int64() + 200) // bad td
   252  
   253  	// Sign the announcement if necessary.
   254  	announce := announceData{hash, number, td, 0, nil}
   255  	if peer.cpeer.announceType == announceTypeSigned {
   256  		announce.sign(s.handler.server.privateKey)
   257  	}
   258  	peer.cpeer.sendAnnounce(announce)
   259  	<-done // Wait syncing
   260  
   261  	// Ensure the bad peer is evicited
   262  	if c.handler.backend.peers.len() != 0 {
   263  		t.Fatalf("Failed to evict invalid peer")
   264  	}
   265  }