aboutsummaryrefslogtreecommitdiffstats
path: root/les/sync_test.go
blob: 3a75d6856dd5b6192a3c05c5f218299303fb7b57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package les

import (
    "fmt"
    "math/big"
    "testing"
    "time"

    "github.com/ethereum/go-ethereum/accounts/abi/bind"
    "github.com/ethereum/go-ethereum/core"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/light"
    "github.com/ethereum/go-ethereum/params"
)

// Test light syncing which will download all headers from genesis.
func TestLightSyncingLes2(t *testing.T) { testCheckpointSyncing(t, 2, 0) }
func TestLightSyncingLes3(t *testing.T) { testCheckpointSyncing(t, 3, 0) }

// Test legacy checkpoint syncing which will download tail headers
// based on a hardcoded checkpoint.
func TestLegacyCheckpointSyncingLes2(t *testing.T) { testCheckpointSyncing(t, 2, 1) }
func TestLegacyCheckpointSyncingLes3(t *testing.T) { testCheckpointSyncing(t, 3, 1) }

// Test checkpoint syncing which will download tail headers based
// on a verified checkpoint.
func TestCheckpointSyncingLes2(t *testing.T) { testCheckpointSyncing(t, 2, 2) }
func TestCheckpointSyncingLes3(t *testing.T) { testCheckpointSyncing(t, 3, 2) }

func testCheckpointSyncing(t *testing.T, protocol int, syncMode int) {
    config := light.TestServerIndexerConfig

    waitIndexers := func(cIndexer, bIndexer, btIndexer *core.ChainIndexer) {
        for {
            cs, _, _ := cIndexer.Sections()
            bts, _, _ := btIndexer.Sections()
            if cs >= 1 && bts >= 1 {
                break
            }
            time.Sleep(10 * time.Millisecond)
        }
    }
    // Generate 512+4 blocks (totally 1 CHT sections)
    server, client, tearDown := newClientServerEnv(t, int(config.ChtSize+config.ChtConfirms), protocol, waitIndexers, false)
    defer tearDown()

    expected := config.ChtSize + config.ChtConfirms

    // Checkpoint syncing or legacy checkpoint syncing.
    if syncMode == 1 || syncMode == 2 {
        // Assemble checkpoint 0
        s, _, head := server.chtIndexer.Sections()
        cp := &params.TrustedCheckpoint{
            SectionIndex: 0,
            SectionHead:  head,
            CHTRoot:      light.GetChtRoot(server.db, s-1, head),
            BloomRoot:    light.GetBloomTrieRoot(server.db, s-1, head),
        }
        if syncMode == 1 {
            // Register the assembled checkpoint as hardcoded one.
            client.pm.checkpoint = cp
            client.pm.blockchain.(*light.LightChain).AddTrustedCheckpoint(cp)
        } else {
            // Register the assembled checkpoint into oracle.
            header := server.backend.Blockchain().CurrentHeader()

            data := append([]byte{0x19, 0x00}, append(registrarAddr.Bytes(), append([]byte{0, 0, 0, 0, 0, 0, 0, 0}, cp.Hash().Bytes()...)...)...)
            sig, _ := crypto.Sign(crypto.Keccak256(data), signerKey)
            sig[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
            if _, err := server.pm.reg.contract.RegisterCheckpoint(bind.NewKeyedTransactor(signerKey), cp.SectionIndex, cp.Hash().Bytes(), new(big.Int).Sub(header.Number, big.NewInt(1)), header.ParentHash, [][]byte{sig}); err != nil {
                t.Error("register checkpoint failed", err)
            }
            server.backend.Commit()

            // Wait for the checkpoint registration
            for {
                _, hash, _, err := server.pm.reg.contract.Contract().GetLatestCheckpoint(nil)
                if err != nil || hash == [32]byte{} {
                    time.Sleep(100 * time.Millisecond)
                    continue
                }
                break
            }
            expected += 1
        }
    }

    done := make(chan error)
    client.pm.reg.syncDoneHook = func() {
        header := client.pm.blockchain.CurrentHeader()
        if header.Number.Uint64() == expected {
            done <- nil
        } else {
            done <- fmt.Errorf("blockchain length mismatch, want %d, got %d", expected, header.Number)
        }
    }

    // Create connected peer pair.
    peer, err1, lPeer, err2 := newTestPeerPair("peer", protocol, server.pm, client.pm)
    select {
    case <-time.After(time.Millisecond * 100):
    case err := <-err1:
        t.Fatalf("peer 1 handshake error: %v", err)
    case err := <-err2:
        t.Fatalf("peer 2 handshake error: %v", err)
    }
    server.rPeer, client.rPeer = peer, lPeer

    select {
    case err := <-done:
        if err != nil {
            t.Error("sync failed", err)
        }
        return
    case <-time.NewTimer(10 * time.Second).C:
        t.Error("checkpoint syncing timeout")
    }
}