github.com/calmw/ethereum@v0.1.1/les/sync_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  	"fmt"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/calmw/ethereum/core"
    25  	"github.com/calmw/ethereum/core/types"
    26  	"github.com/calmw/ethereum/light"
    27  )
    28  
    29  // Test light syncing which will download all headers from genesis.
    30  func TestLightSyncingLes3(t *testing.T) { testSyncing(t, lpv3) }
    31  
    32  func testSyncing(t *testing.T, protocol int) {
    33  	config := light.TestServerIndexerConfig
    34  
    35  	waitIndexers := func(cIndexer, bIndexer, btIndexer *core.ChainIndexer) {
    36  		for {
    37  			cs, _, _ := cIndexer.Sections()
    38  			bts, _, _ := btIndexer.Sections()
    39  			if cs >= 1 && bts >= 1 {
    40  				break
    41  			}
    42  			time.Sleep(10 * time.Millisecond)
    43  		}
    44  	}
    45  	// Generate 128+1 blocks (totally 1 CHT section)
    46  	netconfig := testnetConfig{
    47  		blocks:    int(config.ChtSize + config.ChtConfirms),
    48  		protocol:  protocol,
    49  		indexFn:   waitIndexers,
    50  		nopruning: true,
    51  	}
    52  	server, client, tearDown := newClientServerEnv(t, netconfig)
    53  	defer tearDown()
    54  
    55  	expected := config.ChtSize + config.ChtConfirms
    56  
    57  	done := make(chan error)
    58  	client.handler.syncEnd = func(header *types.Header) {
    59  		if header.Number.Uint64() == expected {
    60  			done <- nil
    61  		} else {
    62  			done <- fmt.Errorf("blockchain length mismatch, want %d, got %d", expected, header.Number)
    63  		}
    64  	}
    65  
    66  	// Create connected peer pair.
    67  	peer1, peer2, err := newTestPeerPair("peer", protocol, server.handler, client.handler, false)
    68  	if err != nil {
    69  		t.Fatalf("Failed to connect testing peers %v", err)
    70  	}
    71  	defer peer1.close()
    72  	defer peer2.close()
    73  
    74  	select {
    75  	case err := <-done:
    76  		if err != nil {
    77  			t.Error("sync failed", err)
    78  		}
    79  		return
    80  	case <-time.NewTimer(10 * time.Second).C:
    81  		t.Error("checkpoint syncing timeout")
    82  	}
    83  }