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 (
|
|
|
|
"math/big"
|
2019-08-21 09:29:34 +00:00
|
|
|
"net"
|
2019-01-24 11:18:26 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-02-26 11:32:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
2019-08-21 09:29:34 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
2019-01-24 11:18:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/les/flowcontrol"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2019-08-21 09:29:34 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2019-01-24 11:18:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2019-08-21 09:29:34 +00:00
|
|
|
const protocolVersion = lpv2
|
2019-01-24 11:18:26 +00:00
|
|
|
|
|
|
|
var (
|
2019-08-21 09:29:34 +00:00
|
|
|
hash = common.HexToHash("deadbeef")
|
|
|
|
genesis = common.HexToHash("cafebabe")
|
2019-01-24 11:18:26 +00:00
|
|
|
headNum = uint64(1234)
|
|
|
|
td = big.NewInt(123)
|
|
|
|
)
|
|
|
|
|
2019-08-21 09:29:34 +00:00
|
|
|
func newNodeID(t *testing.T) *enode.Node {
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("generate key err:", err)
|
|
|
|
}
|
|
|
|
return enode.NewV4(&key.PublicKey, net.IP{}, 35000, 35000)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ulc connects to trusted peer and send announceType=announceTypeSigned
|
2019-01-24 11:18:26 +00:00
|
|
|
func TestPeerHandshakeSetAnnounceTypeToAnnounceTypeSignedForTrustedPeer(t *testing.T) {
|
2019-02-25 10:49:49 +00:00
|
|
|
id := newNodeID(t).ID()
|
2019-01-24 11:18:26 +00:00
|
|
|
|
2019-08-21 09:29:34 +00:00
|
|
|
// peer to connect(on ulc side)
|
2019-01-24 11:18:26 +00:00
|
|
|
p := peer{
|
2019-07-09 17:30:24 +00:00
|
|
|
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
2019-08-21 09:29:34 +00:00
|
|
|
version: protocolVersion,
|
2019-07-09 17:30:24 +00:00
|
|
|
trusted: true,
|
2019-01-24 11:18:26 +00:00
|
|
|
rw: &rwStub{
|
|
|
|
WriteHook: func(recvList keyValueList) {
|
2019-02-26 11:32:48 +00:00
|
|
|
recv, _ := recvList.decode()
|
2019-01-24 11:18:26 +00:00
|
|
|
var reqType uint64
|
|
|
|
err := recv.get("announceType", &reqType)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if reqType != announceTypeSigned {
|
|
|
|
t.Fatal("Expected announceTypeSigned")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ReadHook: func(l keyValueList) keyValueList {
|
|
|
|
l = l.add("serveHeaders", nil)
|
|
|
|
l = l.add("serveChainSince", uint64(0))
|
|
|
|
l = l.add("serveStateSince", uint64(0))
|
|
|
|
l = l.add("txRelay", nil)
|
|
|
|
l = l.add("flowControl/BL", uint64(0))
|
|
|
|
l = l.add("flowControl/MRR", uint64(0))
|
2019-05-30 18:51:13 +00:00
|
|
|
l = l.add("flowControl/MRC", testCostList(0))
|
2019-01-24 11:18:26 +00:00
|
|
|
return l
|
|
|
|
},
|
|
|
|
},
|
2019-08-21 09:29:34 +00:00
|
|
|
network: NetworkId,
|
2019-01-24 11:18:26 +00:00
|
|
|
}
|
|
|
|
err := p.Handshake(td, hash, headNum, genesis, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Handshake error: %s", err)
|
|
|
|
}
|
|
|
|
if p.announceType != announceTypeSigned {
|
|
|
|
t.Fatal("Incorrect announceType")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerHandshakeAnnounceTypeSignedForTrustedPeersPeerNotInTrusted(t *testing.T) {
|
2019-02-25 10:49:49 +00:00
|
|
|
id := newNodeID(t).ID()
|
2019-01-24 11:18:26 +00:00
|
|
|
p := peer{
|
|
|
|
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
2019-08-21 09:29:34 +00:00
|
|
|
version: protocolVersion,
|
2019-01-24 11:18:26 +00:00
|
|
|
rw: &rwStub{
|
|
|
|
WriteHook: func(recvList keyValueList) {
|
2019-08-21 09:29:34 +00:00
|
|
|
// checking that ulc sends to peer allowedRequests=noRequests and announceType != announceTypeSigned
|
2019-02-26 11:32:48 +00:00
|
|
|
recv, _ := recvList.decode()
|
2019-01-24 11:18:26 +00:00
|
|
|
var reqType uint64
|
|
|
|
err := recv.get("announceType", &reqType)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if reqType == announceTypeSigned {
|
|
|
|
t.Fatal("Expected not announceTypeSigned")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ReadHook: func(l keyValueList) keyValueList {
|
|
|
|
l = l.add("serveHeaders", nil)
|
|
|
|
l = l.add("serveChainSince", uint64(0))
|
|
|
|
l = l.add("serveStateSince", uint64(0))
|
|
|
|
l = l.add("txRelay", nil)
|
|
|
|
l = l.add("flowControl/BL", uint64(0))
|
|
|
|
l = l.add("flowControl/MRR", uint64(0))
|
2019-05-30 18:51:13 +00:00
|
|
|
l = l.add("flowControl/MRC", testCostList(0))
|
2019-01-24 11:18:26 +00:00
|
|
|
return l
|
|
|
|
},
|
|
|
|
},
|
2019-08-21 09:29:34 +00:00
|
|
|
network: NetworkId,
|
2019-01-24 11:18:26 +00:00
|
|
|
}
|
|
|
|
err := p.Handshake(td, hash, headNum, genesis, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if p.announceType == announceTypeSigned {
|
|
|
|
t.Fatal("Incorrect announceType")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerHandshakeDefaultAllRequests(t *testing.T) {
|
2019-02-25 10:49:49 +00:00
|
|
|
id := newNodeID(t).ID()
|
2019-01-24 11:18:26 +00:00
|
|
|
|
|
|
|
s := generateLesServer()
|
|
|
|
|
|
|
|
p := peer{
|
|
|
|
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
2019-08-21 09:29:34 +00:00
|
|
|
version: protocolVersion,
|
2019-01-24 11:18:26 +00:00
|
|
|
rw: &rwStub{
|
|
|
|
ReadHook: func(l keyValueList) keyValueList {
|
|
|
|
l = l.add("announceType", uint64(announceTypeSigned))
|
|
|
|
l = l.add("allowedRequests", uint64(0))
|
|
|
|
return l
|
|
|
|
},
|
|
|
|
},
|
2019-08-21 09:29:34 +00:00
|
|
|
network: NetworkId,
|
2019-01-24 11:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := p.Handshake(td, hash, headNum, genesis, s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-07-09 17:30:24 +00:00
|
|
|
if p.onlyAnnounce {
|
2019-01-24 11:18:26 +00:00
|
|
|
t.Fatal("Incorrect announceType")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerHandshakeServerSendOnlyAnnounceRequestsHeaders(t *testing.T) {
|
2019-02-25 10:49:49 +00:00
|
|
|
id := newNodeID(t).ID()
|
2019-01-24 11:18:26 +00:00
|
|
|
|
|
|
|
s := generateLesServer()
|
2019-08-21 09:29:34 +00:00
|
|
|
s.config.UltraLightOnlyAnnounce = true
|
2019-01-24 11:18:26 +00:00
|
|
|
|
|
|
|
p := peer{
|
|
|
|
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
2019-08-21 09:29:34 +00:00
|
|
|
version: protocolVersion,
|
2019-01-24 11:18:26 +00:00
|
|
|
rw: &rwStub{
|
|
|
|
ReadHook: func(l keyValueList) keyValueList {
|
|
|
|
l = l.add("announceType", uint64(announceTypeSigned))
|
|
|
|
return l
|
|
|
|
},
|
|
|
|
WriteHook: func(l keyValueList) {
|
|
|
|
for _, v := range l {
|
|
|
|
if v.Key == "serveHeaders" ||
|
|
|
|
v.Key == "serveChainSince" ||
|
|
|
|
v.Key == "serveStateSince" ||
|
|
|
|
v.Key == "txRelay" {
|
|
|
|
t.Fatalf("%v exists", v.Key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2019-08-21 09:29:34 +00:00
|
|
|
network: NetworkId,
|
2019-01-24 11:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := p.Handshake(td, hash, headNum, genesis, s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestPeerHandshakeClientReceiveOnlyAnnounceRequestsHeaders(t *testing.T) {
|
2019-02-25 10:49:49 +00:00
|
|
|
id := newNodeID(t).ID()
|
2019-01-24 11:18:26 +00:00
|
|
|
|
|
|
|
p := peer{
|
|
|
|
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
2019-08-21 09:29:34 +00:00
|
|
|
version: protocolVersion,
|
2019-01-24 11:18:26 +00:00
|
|
|
rw: &rwStub{
|
|
|
|
ReadHook: func(l keyValueList) keyValueList {
|
|
|
|
l = l.add("flowControl/BL", uint64(0))
|
|
|
|
l = l.add("flowControl/MRR", uint64(0))
|
|
|
|
l = l.add("flowControl/MRC", RequestCostList{})
|
|
|
|
|
|
|
|
l = l.add("announceType", uint64(announceTypeSigned))
|
|
|
|
|
|
|
|
return l
|
|
|
|
},
|
|
|
|
},
|
2019-08-21 09:29:34 +00:00
|
|
|
network: NetworkId,
|
2019-07-09 17:30:24 +00:00
|
|
|
trusted: true,
|
2019-01-24 11:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := p.Handshake(td, hash, headNum, genesis, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-07-09 17:30:24 +00:00
|
|
|
if !p.onlyAnnounce {
|
|
|
|
t.Fatal("onlyAnnounce must be true")
|
2019-01-24 11:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerHandshakeClientReturnErrorOnUselessPeer(t *testing.T) {
|
2019-02-25 10:49:49 +00:00
|
|
|
id := newNodeID(t).ID()
|
2019-01-24 11:18:26 +00:00
|
|
|
|
|
|
|
p := peer{
|
|
|
|
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
2019-08-21 09:29:34 +00:00
|
|
|
version: protocolVersion,
|
2019-01-24 11:18:26 +00:00
|
|
|
rw: &rwStub{
|
|
|
|
ReadHook: func(l keyValueList) keyValueList {
|
|
|
|
l = l.add("flowControl/BL", uint64(0))
|
|
|
|
l = l.add("flowControl/MRR", uint64(0))
|
|
|
|
l = l.add("flowControl/MRC", RequestCostList{})
|
|
|
|
l = l.add("announceType", uint64(announceTypeSigned))
|
|
|
|
return l
|
|
|
|
},
|
|
|
|
},
|
2019-08-21 09:29:34 +00:00
|
|
|
network: NetworkId,
|
2019-01-24 11:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := p.Handshake(td, hash, headNum, genesis, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateLesServer() *LesServer {
|
|
|
|
s := &LesServer{
|
2019-08-21 09:29:34 +00:00
|
|
|
lesCommons: lesCommons{
|
|
|
|
config: ð.Config{UltraLightOnlyAnnounce: true},
|
|
|
|
},
|
2019-02-26 11:32:48 +00:00
|
|
|
defParams: flowcontrol.ServerParams{
|
2019-01-24 11:18:26 +00:00
|
|
|
BufLimit: uint64(300000000),
|
|
|
|
MinRecharge: uint64(50000),
|
|
|
|
},
|
2019-02-26 11:32:48 +00:00
|
|
|
fcManager: flowcontrol.NewClientManager(nil, &mclock.System{}),
|
2019-01-24 11:18:26 +00:00
|
|
|
}
|
2019-08-21 09:29:34 +00:00
|
|
|
s.costTracker, _ = newCostTracker(rawdb.NewMemoryDatabase(), s.config)
|
2019-01-24 11:18:26 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type rwStub struct {
|
|
|
|
ReadHook func(l keyValueList) keyValueList
|
|
|
|
WriteHook func(l keyValueList)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rwStub) ReadMsg() (p2p.Msg, error) {
|
|
|
|
payload := keyValueList{}
|
2019-08-21 09:29:34 +00:00
|
|
|
payload = payload.add("protocolVersion", uint64(protocolVersion))
|
|
|
|
payload = payload.add("networkId", uint64(NetworkId))
|
2019-01-24 11:18:26 +00:00
|
|
|
payload = payload.add("headTd", td)
|
|
|
|
payload = payload.add("headHash", hash)
|
|
|
|
payload = payload.add("headNum", headNum)
|
|
|
|
payload = payload.add("genesisHash", genesis)
|
|
|
|
|
|
|
|
if s.ReadHook != nil {
|
|
|
|
payload = s.ReadHook(payload)
|
|
|
|
}
|
|
|
|
size, p, err := rlp.EncodeToReader(payload)
|
|
|
|
if err != nil {
|
|
|
|
return p2p.Msg{}, err
|
|
|
|
}
|
|
|
|
return p2p.Msg{
|
|
|
|
Size: uint32(size),
|
|
|
|
Payload: p,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rwStub) WriteMsg(m p2p.Msg) error {
|
|
|
|
recvList := keyValueList{}
|
|
|
|
if err := m.Decode(&recvList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if s.WriteHook != nil {
|
|
|
|
s.WriteHook(recvList)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|