2019-07-22 09:17:27 +00:00
|
|
|
// 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/>.
|
|
|
|
|
2019-01-24 11:18:26 +00:00
|
|
|
package les
|
|
|
|
|
|
|
|
import (
|
2020-02-26 09:41:24 +00:00
|
|
|
"crypto/rand"
|
2020-05-01 11:48:52 +00:00
|
|
|
"errors"
|
2019-01-24 11:18:26 +00:00
|
|
|
"math/big"
|
2020-02-26 09:41:24 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2019-01-24 11:18:26 +00:00
|
|
|
"testing"
|
2020-02-26 09:41:24 +00:00
|
|
|
"time"
|
2019-01-24 11:18:26 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-12-10 16:20:55 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/forkid"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2019-01-24 11:18:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2019-08-21 09:29:34 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2020-12-10 16:20:55 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2019-01-24 11:18:26 +00:00
|
|
|
)
|
|
|
|
|
2020-02-26 09:41:24 +00:00
|
|
|
type testServerPeerSub struct {
|
|
|
|
regCh chan *serverPeer
|
|
|
|
unregCh chan *serverPeer
|
2019-01-24 11:18:26 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 09:41:24 +00:00
|
|
|
func newTestServerPeerSub() *testServerPeerSub {
|
|
|
|
return &testServerPeerSub{
|
|
|
|
regCh: make(chan *serverPeer, 1),
|
|
|
|
unregCh: make(chan *serverPeer, 1),
|
2019-01-24 11:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 09:41:24 +00:00
|
|
|
func (t *testServerPeerSub) registerPeer(p *serverPeer) { t.regCh <- p }
|
|
|
|
func (t *testServerPeerSub) unregisterPeer(p *serverPeer) { t.unregCh <- p }
|
|
|
|
|
|
|
|
func TestPeerSubscription(t *testing.T) {
|
|
|
|
peers := newServerPeerSet()
|
|
|
|
defer peers.close()
|
|
|
|
|
|
|
|
checkIds := func(expect []string) {
|
|
|
|
given := peers.ids()
|
|
|
|
if len(given) == 0 && len(expect) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sort.Strings(given)
|
|
|
|
sort.Strings(expect)
|
|
|
|
if !reflect.DeepEqual(given, expect) {
|
|
|
|
t.Fatalf("all peer ids mismatch, want %v, given %v", expect, given)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkPeers := func(peerCh chan *serverPeer) {
|
|
|
|
select {
|
|
|
|
case <-peerCh:
|
|
|
|
case <-time.NewTimer(100 * time.Millisecond).C:
|
|
|
|
t.Fatalf("timeout, no event received")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-peerCh:
|
|
|
|
t.Fatalf("unexpected event received")
|
|
|
|
case <-time.NewTimer(10 * time.Millisecond).C:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkIds([]string{})
|
|
|
|
|
|
|
|
sub := newTestServerPeerSub()
|
|
|
|
peers.subscribe(sub)
|
|
|
|
|
|
|
|
// Generate a random id and create the peer
|
|
|
|
var id enode.ID
|
|
|
|
rand.Read(id[:])
|
|
|
|
peer := newServerPeer(2, NetworkId, false, p2p.NewPeer(id, "name", nil), nil)
|
|
|
|
peers.register(peer)
|
|
|
|
|
|
|
|
checkIds([]string{peer.id})
|
|
|
|
checkPeers(sub.regCh)
|
|
|
|
|
|
|
|
peers.unregister(peer.id)
|
|
|
|
checkIds([]string{})
|
|
|
|
checkPeers(sub.unregCh)
|
2019-01-24 11:18:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 16:20:55 +00:00
|
|
|
type fakeChain struct{}
|
|
|
|
|
|
|
|
func (f *fakeChain) Config() *params.ChainConfig { return params.MainnetChainConfig }
|
|
|
|
func (f *fakeChain) Genesis() *types.Block {
|
2022-08-09 09:44:39 +00:00
|
|
|
return core.DefaultGenesisBlock().ToBlock()
|
2020-12-10 16:20:55 +00:00
|
|
|
}
|
|
|
|
func (f *fakeChain) CurrentHeader() *types.Header { return &types.Header{Number: big.NewInt(10000000)} }
|
|
|
|
|
2020-02-26 09:41:24 +00:00
|
|
|
func TestHandshake(t *testing.T) {
|
|
|
|
// Create a message pipe to communicate through
|
|
|
|
app, net := p2p.MsgPipe()
|
|
|
|
|
|
|
|
// Generate a random id and create the peer
|
|
|
|
var id enode.ID
|
|
|
|
rand.Read(id[:])
|
|
|
|
|
|
|
|
peer1 := newClientPeer(2, NetworkId, p2p.NewPeer(id, "name", nil), net)
|
|
|
|
peer2 := newServerPeer(2, NetworkId, true, p2p.NewPeer(id, "name", nil), app)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errCh1 = make(chan error, 1)
|
|
|
|
errCh2 = make(chan error, 1)
|
|
|
|
|
|
|
|
td = big.NewInt(100)
|
|
|
|
head = common.HexToHash("deadbeef")
|
|
|
|
headNum = uint64(10)
|
|
|
|
genesis = common.HexToHash("cafebabe")
|
2020-12-10 16:20:55 +00:00
|
|
|
|
|
|
|
chain1, chain2 = &fakeChain{}, &fakeChain{}
|
2023-08-16 21:31:02 +00:00
|
|
|
forkID1 = forkid.NewID(chain1.Config(), chain1.Genesis(), chain1.CurrentHeader().Number.Uint64(), chain1.CurrentHeader().Time)
|
|
|
|
forkID2 = forkid.NewID(chain2.Config(), chain2.Genesis(), chain2.CurrentHeader().Number.Uint64(), chain2.CurrentHeader().Time)
|
2020-12-10 16:20:55 +00:00
|
|
|
filter1, filter2 = forkid.NewFilter(chain1), forkid.NewFilter(chain2)
|
2020-02-26 09:41:24 +00:00
|
|
|
)
|
2020-12-10 16:20:55 +00:00
|
|
|
|
2020-02-26 09:41:24 +00:00
|
|
|
go func() {
|
2020-12-10 16:20:55 +00:00
|
|
|
errCh1 <- peer1.handshake(td, head, headNum, genesis, forkID1, filter1, func(list *keyValueList) {
|
2020-02-26 09:41:24 +00:00
|
|
|
var announceType uint64 = announceTypeSigned
|
|
|
|
*list = (*list).add("announceType", announceType)
|
|
|
|
}, nil)
|
|
|
|
}()
|
|
|
|
go func() {
|
2020-12-10 16:20:55 +00:00
|
|
|
errCh2 <- peer2.handshake(td, head, headNum, genesis, forkID2, filter2, nil, func(recv keyValueMap) error {
|
2020-02-26 09:41:24 +00:00
|
|
|
var reqType uint64
|
|
|
|
err := recv.get("announceType", &reqType)
|
|
|
|
if err != nil {
|
2020-05-01 11:48:52 +00:00
|
|
|
return err
|
2020-02-26 09:41:24 +00:00
|
|
|
}
|
|
|
|
if reqType != announceTypeSigned {
|
2023-11-15 12:36:57 +00:00
|
|
|
return errors.New("expected announceTypeSigned")
|
2020-02-26 09:41:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
select {
|
|
|
|
case err := <-errCh1:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("handshake failed, %v", err)
|
|
|
|
}
|
|
|
|
case err := <-errCh2:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("handshake failed, %v", err)
|
|
|
|
}
|
2020-04-07 06:16:21 +00:00
|
|
|
case <-time.After(time.Second):
|
2020-02-26 09:41:24 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
2019-01-24 11:18:26 +00:00
|
|
|
}
|
|
|
|
}
|