2019-07-22 04:17:27 -05:00
|
|
|
// Copyright 2018 The go-ethereum Authors
|
2019-05-26 11:15:05 -05:00
|
|
|
// 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/>.
|
|
|
|
|
2019-01-24 05:18:26 -06:00
|
|
|
package les
|
|
|
|
|
|
|
|
import (
|
2019-08-21 04:29:34 -05:00
|
|
|
"crypto/rand"
|
2019-01-24 05:18:26 -06:00
|
|
|
"fmt"
|
2018-09-24 07:57:49 -05:00
|
|
|
"net"
|
2021-08-25 04:56:25 -05:00
|
|
|
"sync/atomic"
|
2019-01-24 05:18:26 -06:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
)
|
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
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++ {
|
2022-04-08 08:48:52 -05:00
|
|
|
s, n, teardown := newTestServerPeer(t, 0, protocol, nil)
|
2019-08-21 04:29:34 -05:00
|
|
|
|
|
|
|
servers = append(servers, s)
|
|
|
|
nodes = append(nodes, n)
|
|
|
|
teardowns = append(teardowns, teardown)
|
|
|
|
ids = append(ids, n.String())
|
|
|
|
}
|
2020-02-26 03:41:24 -06:00
|
|
|
c, teardown := newTestLightPeer(t, protocol, ids, testcase.threshold)
|
2019-01-24 05:18:26 -06:00
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
// Connect all servers.
|
|
|
|
for i := 0; i < len(servers); i++ {
|
2021-08-25 04:56:25 -05:00
|
|
|
connect(servers[i].handler, nodes[i].ID(), c.handler, protocol, false)
|
2019-08-21 04:29:34 -05:00
|
|
|
}
|
|
|
|
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.Uint64()
|
|
|
|
if head != testcase.expect {
|
|
|
|
t.Fatalf("chain height mismatch, want %d, got %d", testcase.expect, head)
|
|
|
|
}
|
2019-01-24 05:18:26 -06:00
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
// Release all servers and client resources.
|
|
|
|
teardown()
|
|
|
|
for i := 0; i < len(teardowns); i++ {
|
|
|
|
teardowns[i]()
|
|
|
|
}
|
2019-01-24 05:18:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 04:56:25 -05:00
|
|
|
func connect(server *serverHandler, serverId enode.ID, client *clientHandler, protocol int, noInitAnnounce bool) (*serverPeer, *clientPeer, error) {
|
2019-01-24 05:18:26 -06:00
|
|
|
// Create a message pipe to communicate through
|
|
|
|
app, net := p2p.MsgPipe()
|
|
|
|
|
2019-08-21 04:29:34 -05:00
|
|
|
var id enode.ID
|
|
|
|
rand.Read(id[:])
|
|
|
|
|
2020-02-26 03:41:24 -06:00
|
|
|
peer1 := newServerPeer(protocol, NetworkId, true, p2p.NewPeer(serverId, "", nil), net) // Mark server as trusted
|
|
|
|
peer2 := newClientPeer(protocol, NetworkId, p2p.NewPeer(id, "", nil), app)
|
2019-01-24 05:18:26 -06:00
|
|
|
|
|
|
|
// Start the peerLight on a new thread
|
|
|
|
errc1 := make(chan error, 1)
|
|
|
|
errc2 := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
select {
|
2019-08-21 04:29:34 -05:00
|
|
|
case <-server.closeCh:
|
2019-01-24 05:18:26 -06:00
|
|
|
errc1 <- p2p.DiscQuitting
|
2019-08-21 04:29:34 -05:00
|
|
|
case errc1 <- server.handle(peer2):
|
2019-01-24 05:18:26 -06:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
select {
|
2019-08-21 04:29:34 -05:00
|
|
|
case <-client.closeCh:
|
|
|
|
errc1 <- p2p.DiscQuitting
|
2021-08-25 04:56:25 -05:00
|
|
|
case errc1 <- client.handle(peer1, noInitAnnounce):
|
2019-01-24 05:18:26 -06:00
|
|
|
}
|
|
|
|
}()
|
2021-08-25 04:56:25 -05:00
|
|
|
// 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)
|
2019-01-24 05:18:26 -06:00
|
|
|
}
|
2019-08-21 04:29:34 -05:00
|
|
|
return peer1, peer2, nil
|
2019-01-24 05:18:26 -06:00
|
|
|
}
|
|
|
|
|
2020-02-26 03:41:24 -06:00
|
|
|
// newTestServerPeer creates server peer.
|
2022-04-08 08:48:52 -05:00
|
|
|
func newTestServerPeer(t *testing.T, blocks int, protocol int, indexFn indexerCallback) (*testServer, *enode.Node, func()) {
|
2021-02-25 07:24:04 -06:00
|
|
|
netconfig := testnetConfig{
|
|
|
|
blocks: blocks,
|
|
|
|
protocol: protocol,
|
2022-04-08 08:48:52 -05:00
|
|
|
indexFn: indexFn,
|
2021-02-25 07:24:04 -06:00
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
s, _, teardown := newClientServerEnv(t, netconfig)
|
2019-01-24 05:18:26 -06:00
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("generate key err:", err)
|
|
|
|
}
|
2019-08-21 04:29:34 -05:00
|
|
|
s.handler.server.privateKey = key
|
|
|
|
n := enode.NewV4(&key.PublicKey, net.ParseIP("127.0.0.1"), 35000, 35000)
|
|
|
|
return s, n, teardown
|
2019-01-24 05:18:26 -06:00
|
|
|
}
|
|
|
|
|
2020-02-26 03:41:24 -06:00
|
|
|
// newTestLightPeer creates node with light sync mode
|
|
|
|
func newTestLightPeer(t *testing.T, protocol int, ulcServers []string, ulcFraction int) (*testClient, func()) {
|
2021-02-25 07:24:04 -06:00
|
|
|
netconfig := testnetConfig{
|
|
|
|
protocol: protocol,
|
|
|
|
ulcServers: ulcServers,
|
|
|
|
ulcFraction: ulcFraction,
|
|
|
|
nopruning: true,
|
|
|
|
}
|
|
|
|
_, c, teardown := newClientServerEnv(t, netconfig)
|
2019-08-21 04:29:34 -05:00
|
|
|
return c, teardown
|
2019-01-24 05:18:26 -06:00
|
|
|
}
|