2019-01-24 05:18:26 -06:00
|
|
|
package les
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-02-26 05:32:48 -06:00
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
2019-01-24 05:18:26 -06:00
|
|
|
"github.com/ethereum/go-ethereum/les/flowcontrol"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
test_networkid = 10
|
2019-05-13 06:26:47 -05:00
|
|
|
protocol_version = lpv2
|
2019-01-24 05:18:26 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
hash = common.HexToHash("some string")
|
|
|
|
genesis = common.HexToHash("genesis hash")
|
|
|
|
headNum = uint64(1234)
|
|
|
|
td = big.NewInt(123)
|
|
|
|
)
|
|
|
|
|
|
|
|
//ulc connects to trusted peer and send announceType=announceTypeSigned
|
|
|
|
func TestPeerHandshakeSetAnnounceTypeToAnnounceTypeSignedForTrustedPeer(t *testing.T) {
|
2019-02-25 04:49:49 -06:00
|
|
|
id := newNodeID(t).ID()
|
2019-01-24 05:18:26 -06:00
|
|
|
|
|
|
|
//peer to connect(on ulc side)
|
|
|
|
p := peer{
|
2019-07-09 12:30:24 -05:00
|
|
|
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
|
|
|
version: protocol_version,
|
|
|
|
trusted: true,
|
2019-01-24 05:18:26 -06:00
|
|
|
rw: &rwStub{
|
|
|
|
WriteHook: func(recvList keyValueList) {
|
|
|
|
//checking that ulc sends to peer allowedRequests=onlyAnnounceRequests and announceType = announceTypeSigned
|
2019-02-26 05:32:48 -06:00
|
|
|
recv, _ := recvList.decode()
|
2019-01-24 05:18:26 -06: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 13:51:13 -05:00
|
|
|
l = l.add("flowControl/MRC", testCostList(0))
|
2019-01-24 05:18:26 -06:00
|
|
|
|
|
|
|
return l
|
|
|
|
},
|
|
|
|
},
|
|
|
|
network: test_networkid,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 04:49:49 -06:00
|
|
|
id := newNodeID(t).ID()
|
2019-01-24 05:18:26 -06:00
|
|
|
p := peer{
|
|
|
|
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
|
|
|
version: protocol_version,
|
|
|
|
rw: &rwStub{
|
|
|
|
WriteHook: func(recvList keyValueList) {
|
|
|
|
//checking that ulc sends to peer allowedRequests=noRequests and announceType != announceTypeSigned
|
2019-02-26 05:32:48 -06:00
|
|
|
recv, _ := recvList.decode()
|
2019-01-24 05:18:26 -06: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 13:51:13 -05:00
|
|
|
l = l.add("flowControl/MRC", testCostList(0))
|
2019-01-24 05:18:26 -06:00
|
|
|
|
|
|
|
return l
|
|
|
|
},
|
|
|
|
},
|
|
|
|
network: test_networkid,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 04:49:49 -06:00
|
|
|
id := newNodeID(t).ID()
|
2019-01-24 05:18:26 -06:00
|
|
|
|
|
|
|
s := generateLesServer()
|
|
|
|
|
|
|
|
p := peer{
|
|
|
|
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
|
|
|
version: protocol_version,
|
|
|
|
rw: &rwStub{
|
|
|
|
ReadHook: func(l keyValueList) keyValueList {
|
|
|
|
l = l.add("announceType", uint64(announceTypeSigned))
|
|
|
|
l = l.add("allowedRequests", uint64(0))
|
|
|
|
|
|
|
|
return l
|
|
|
|
},
|
|
|
|
},
|
|
|
|
network: test_networkid,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := p.Handshake(td, hash, headNum, genesis, s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-07-09 12:30:24 -05:00
|
|
|
if p.onlyAnnounce {
|
2019-01-24 05:18:26 -06:00
|
|
|
t.Fatal("Incorrect announceType")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerHandshakeServerSendOnlyAnnounceRequestsHeaders(t *testing.T) {
|
2019-02-25 04:49:49 -06:00
|
|
|
id := newNodeID(t).ID()
|
2019-01-24 05:18:26 -06:00
|
|
|
|
|
|
|
s := generateLesServer()
|
|
|
|
s.onlyAnnounce = true
|
|
|
|
|
|
|
|
p := peer{
|
|
|
|
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
|
|
|
version: protocol_version,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
network: test_networkid,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := p.Handshake(td, hash, headNum, genesis, s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestPeerHandshakeClientReceiveOnlyAnnounceRequestsHeaders(t *testing.T) {
|
2019-02-25 04:49:49 -06:00
|
|
|
id := newNodeID(t).ID()
|
2019-01-24 05:18:26 -06:00
|
|
|
|
|
|
|
p := peer{
|
|
|
|
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
|
|
|
version: protocol_version,
|
|
|
|
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-07-09 12:30:24 -05:00
|
|
|
network: test_networkid,
|
|
|
|
trusted: true,
|
2019-01-24 05:18:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := p.Handshake(td, hash, headNum, genesis, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-07-09 12:30:24 -05:00
|
|
|
if !p.onlyAnnounce {
|
|
|
|
t.Fatal("onlyAnnounce must be true")
|
2019-01-24 05:18:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerHandshakeClientReturnErrorOnUselessPeer(t *testing.T) {
|
2019-02-25 04:49:49 -06:00
|
|
|
id := newNodeID(t).ID()
|
2019-01-24 05:18:26 -06:00
|
|
|
|
|
|
|
p := peer{
|
|
|
|
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
|
|
|
version: protocol_version,
|
|
|
|
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
|
|
|
|
},
|
|
|
|
},
|
|
|
|
network: test_networkid,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := p.Handshake(td, hash, headNum, genesis, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateLesServer() *LesServer {
|
|
|
|
s := &LesServer{
|
2019-02-26 05:32:48 -06:00
|
|
|
defParams: flowcontrol.ServerParams{
|
2019-01-24 05:18:26 -06:00
|
|
|
BufLimit: uint64(300000000),
|
|
|
|
MinRecharge: uint64(50000),
|
|
|
|
},
|
2019-02-26 05:32:48 -06:00
|
|
|
fcManager: flowcontrol.NewClientManager(nil, &mclock.System{}),
|
2019-01-24 05:18:26 -06:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type rwStub struct {
|
|
|
|
ReadHook func(l keyValueList) keyValueList
|
|
|
|
WriteHook func(l keyValueList)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rwStub) ReadMsg() (p2p.Msg, error) {
|
|
|
|
payload := keyValueList{}
|
|
|
|
payload = payload.add("protocolVersion", uint64(protocol_version))
|
|
|
|
payload = payload.add("networkId", uint64(test_networkid))
|
|
|
|
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
|
|
|
|
}
|