forked from dominant-strategies/go-quai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ulc_test.go
162 lines (148 loc) · 4.87 KB
/
ulc_test.go
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2018 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 (
"crypto/rand"
"fmt"
"net"
"sync/atomic"
"testing"
"time"
"github.com/spruce-solutions/go-quai/core/types"
"github.com/spruce-solutions/go-quai/crypto"
"github.com/spruce-solutions/go-quai/p2p"
"github.com/spruce-solutions/go-quai/p2p/enode"
)
func TestULCAnnounceThresholdLes2(t *testing.T) { testULCAnnounceThreshold(t, 2) }
func TestULCAnnounceThresholdLes3(t *testing.T) { testULCAnnounceThreshold(t, 3) }
func testULCAnnounceThreshold(t *testing.T, protocol int) {
// todo figure out why it takes fetcher so longer to fetcher the announced header.
t.Skip("Sometimes it can failed")
var cases = []struct {
height []int
threshold int
expect uint64
}{
{[]int{1}, 100, 1},
{[]int{0, 0, 0}, 100, 0},
{[]int{1, 2, 3}, 30, 3},
{[]int{1, 2, 3}, 60, 2},
{[]int{3, 2, 1}, 67, 1},
{[]int{3, 2, 1}, 100, 1},
}
for _, testcase := range cases {
var (
servers []*testServer
teardowns []func()
nodes []*enode.Node
ids []string
)
for i := 0; i < len(testcase.height); i++ {
s, n, teardown := newTestServerPeer(t, 0, protocol)
servers = append(servers, s)
nodes = append(nodes, n)
teardowns = append(teardowns, teardown)
ids = append(ids, n.String())
}
c, teardown := newTestLightPeer(t, protocol, ids, testcase.threshold)
// Connect all servers.
for i := 0; i < len(servers); i++ {
connect(servers[i].handler, nodes[i].ID(), c.handler, protocol, false)
}
for i := 0; i < len(servers); i++ {
for j := 0; j < testcase.height[i]; j++ {
servers[i].backend.Commit()
}
}
time.Sleep(1500 * time.Millisecond) // Ensure the fetcher has done its work.
head := c.handler.backend.blockchain.CurrentHeader().Number[types.QuaiNetworkContext].Uint64()
if head != testcase.expect {
t.Fatalf("chain height mismatch, want %d, got %d", testcase.expect, head)
}
// Release all servers and client resources.
teardown()
for i := 0; i < len(teardowns); i++ {
teardowns[i]()
}
}
}
func connect(server *serverHandler, serverId enode.ID, client *clientHandler, protocol int, noInitAnnounce bool) (*serverPeer, *clientPeer, error) {
// Create a message pipe to communicate through
app, net := p2p.MsgPipe()
var id enode.ID
rand.Read(id[:])
peer1 := newServerPeer(protocol, NetworkId, true, p2p.NewPeer(serverId, "", nil), net) // Mark server as trusted
peer2 := newClientPeer(protocol, NetworkId, p2p.NewPeer(id, "", nil), app)
// Start the peerLight on a new thread
errc1 := make(chan error, 1)
errc2 := make(chan error, 1)
go func() {
select {
case <-server.closeCh:
errc1 <- p2p.DiscQuitting
case errc1 <- server.handle(peer2):
}
}()
go func() {
select {
case <-client.closeCh:
errc1 <- p2p.DiscQuitting
case errc1 <- client.handle(peer1, noInitAnnounce):
}
}()
// Ensure the connection is established or exits when any error occurs
for {
select {
case err := <-errc1:
return nil, nil, fmt.Errorf("failed to establish protocol connection %v", err)
case err := <-errc2:
return nil, nil, fmt.Errorf("failed to establish protocol connection %v", err)
default:
}
if atomic.LoadUint32(&peer1.serving) == 1 && atomic.LoadUint32(&peer2.serving) == 1 {
break
}
time.Sleep(50 * time.Millisecond)
}
return peer1, peer2, nil
}
// newTestServerPeer creates server peer.
func newTestServerPeer(t *testing.T, blocks int, protocol int) (*testServer, *enode.Node, func()) {
netconfig := testnetConfig{
blocks: blocks,
protocol: protocol,
nopruning: true,
}
s, _, teardown := newClientServerEnv(t, netconfig)
key, err := crypto.GenerateKey()
if err != nil {
t.Fatal("generate key err:", err)
}
s.handler.server.privateKey = key
n := enode.NewV4(&key.PublicKey, net.ParseIP("127.0.0.1"), 35000, 35000)
return s, n, teardown
}
// newTestLightPeer creates node with light sync mode
func newTestLightPeer(t *testing.T, protocol int, ulcServers []string, ulcFraction int) (*testClient, func()) {
netconfig := testnetConfig{
protocol: protocol,
ulcServers: ulcServers,
ulcFraction: ulcFraction,
nopruning: true,
}
_, c, teardown := newClientServerEnv(t, netconfig)
return c, teardown
}