Skip to content

Commit

Permalink
disable sending requests to ulc
Browse files Browse the repository at this point in the history
  • Loading branch information
b00ris committed Apr 16, 2018
1 parent 1fb5520 commit a26c3a0
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 25 deletions.
17 changes: 10 additions & 7 deletions les/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func (f *lightFetcher) itFindBestValuesForULC() (bestHash common.Hash, bestAmoun

for p, fp := range f.peers {
for hash, n := range fp.nodeByHash {
if _, ok := f.pm.server.ulc.trusted[p.id]; ok == false {
if _, ok := f.pm.server.ulc.trusted[p.id]; !ok {
continue
}

Expand All @@ -476,18 +476,14 @@ func (f *lightFetcher) checkTrusted(hash common.Hash, minTrustedFraction int) bo
numPeers := len(f.peers)
var numAgreed int
for _, fp := range f.peers {
if _, ok := fp.nodeByHash[hash]; ok == false {
if _, ok := fp.nodeByHash[hash]; !ok {
continue
}

numAgreed = numAgreed + 1
}

if 100*numAgreed/numPeers > minTrustedFraction {
return true
}

return false
return 100*numAgreed/numPeers > minTrustedFraction
}

func (f *lightFetcher) newFetcherDistReqForSync(bestHash common.Hash) *distReq {
Expand All @@ -497,6 +493,9 @@ func (f *lightFetcher) newFetcherDistReqForSync(bestHash common.Hash) *distReq {
},
canSend: func(dp distPeer) bool {
p := dp.(*peer)
if p.allowedRequests != allRequests {
return false
}
f.lock.Lock()
defer f.lock.Unlock()
fp := f.peers[p]
Expand All @@ -523,6 +522,10 @@ func (f *lightFetcher) newFetcherDistReq(bestHash common.Hash, reqID uint64, bes
},
canSend: func(dp distPeer) bool {
p := dp.(*peer)
if p.allowedRequests != allRequests {
return false
}

f.lock.Lock()
defer f.lock.Unlock()

Expand Down
11 changes: 10 additions & 1 deletion les/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,10 @@ func (pc *peerConnection) RequestHeadersByHash(origin common.Hash, amount int, s
return peer.GetRequestCost(GetBlockHeadersMsg, amount)
},
canSend: func(dp distPeer) bool {
if dp.(*peer).allowedRequests != allRequests {
return false
}

return dp.(*peer) == pc.peer
},
request: func(dp distPeer) func() {
Expand All @@ -1224,7 +1228,12 @@ func (pc *peerConnection) RequestHeadersByNumber(origin uint64, amount int, skip
return peer.GetRequestCost(GetBlockHeadersMsg, amount)
},
canSend: func(dp distPeer) bool {
return dp.(*peer) == pc.peer
p := dp.(*peer)
if p.allowedRequests != allRequests {
return false
}

return p == pc.peer
},
request: func(dp distPeer) func() {
peer := dp.(*peer)
Expand Down
7 changes: 7 additions & 0 deletions les/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ func (p *testPeer) handshake(t *testing.T, td *big.Int, head common.Hash, headNu
expList = expList.add("flowControl/MRR", uint64(1))
expList = expList.add("flowControl/MRC", testRCL())

//var kv keyValueList
//msg, err := p.app.ReadMsg()
//t.Log(err)
//msg.Decode(&kv)
//t.Fatal(kv)
//[{protocolVersion [1]} {networkId [1]} {headTd [132 4 30 0 0]} {headHash [160 125 69 111 26 172 233 200 16 23 215 15 198 132 121 206 250 12 59 41 82 245 51 198 252 254 168 11 162 212 207 86 190]} {headNum [130 2 15]} {genesisHash [160 233 102 66 91 250 196 145 214 140 22 208 229 199 65 196 222 197 98 48 118 112 8 133 4 163 222 173 239 151 118 153 72]} {announceType [1]} {serveHeaders [128]} {serveChainSince [128]} {serveStateSince [128]} {txRelay [128]} {flowControl/BL [100]} {flowControl/MRR [1]} {flowControl/MRC [236 195 2 128 128 195 4 128 128 195 10 128 128 195 6 128 128 195 8 128 128 195 12 128 128 195 19 128 128 195 20 128 128 195 13 128 128 195 15 128 128 195 17 128 128]}]

if err := p2p.ExpectMsg(p.app, StatusMsg, expList); err != nil {
t.Fatalf("status recv: %v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion les/odr.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ func (odr *LesOdr) Retrieve(ctx context.Context, req light.OdrRequest) (err erro
},
canSend: func(dp distPeer) bool {
p := dp.(*peer)
return lreq.CanSend(p)
if p.allowedRequests == allRequests {
return lreq.CanSend(p)
}
return false
},
request: func(dp distPeer) func() {
p := dp.(*peer)
Expand Down
23 changes: 23 additions & 0 deletions les/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ const (
announceTypeSigned
)

const (
allRequests = iota
onlyAnnounceRequests
noRequests
)

type peer struct {
*p2p.Peer
pubKey *ecdsa.PublicKey
Expand Down Expand Up @@ -76,6 +82,8 @@ type peer struct {
fcServer *flowcontrol.ServerNode // nil if the peer is client only
fcServerParams *flowcontrol.ServerParams
fcCosts requestCostTable

allowedRequests uint64
}

func newPeer(version int, network uint64, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
Expand Down Expand Up @@ -414,10 +422,20 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis
list := server.fcCostStats.getCurrentList()
send = send.add("flowControl/MRC", list)
p.fcCosts = list.decode()
if server.ulc != nil {
if _, ok := server.ulc.trusted[p.ID().String()]; ok {
send = send.add("allowedRequests", uint64(onlyAnnounceRequests))
send = send.add("announceType", uint64(announceTypeSigned))
} else {
send = send.add("allowedRequests", uint64(noRequests))
send = send.add("announceType", uint64(announceTypeSimple))
}
}
} else {
p.requestAnnounceType = announceTypeSimple // set to default until "very light" client mode is implemented
send = send.add("announceType", p.requestAnnounceType)
}

recvList, err := p.sendReceiveHandshake(send)
if err != nil {
return err
Expand Down Expand Up @@ -498,6 +516,11 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis
p.fcCosts = MRC.decode()
}

//which requests could receive the peer
if recv.get("allowedRequests", &p.allowedRequests) != nil {
p.allowedRequests = allRequests
}

p.headInfo = &announceData{Td: rTd, Hash: rHash, Number: rNum}
return nil
}
Expand Down
87 changes: 73 additions & 14 deletions les/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package les

import (
"crypto/rand"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/les/flowcontrol"
Expand All @@ -25,39 +24,94 @@ var (
td = big.NewInt(123)
)

func TestPeer_Handshake_AnnounceTypeSigned_ForTrustedPeers_PeerInTrusted_Success(t *testing.T) {
//ulc connects to trusted peer and mark it
func TestPeer_Handshake_AnnounceTypeSignedAndOnlyAnnounceRequests_ForTrustedPeers_PeerInTrusted_Success(t *testing.T) {
var id discover.NodeID
rand.Read(id[:])

p := peer{
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
version: protocol_version,
rw: &rwStub{},
network: test_networkid,
}
//current ulc server
s := generateLesServer()
s.ulc = newULC(&eth.ULCConfig{
TrustedNodes: []string{id.String()},
})
s.ulc.trusted[id.String()] = struct{}{}

//peer to connect(on ulc side)
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=onlyAnnounceRequests and announceType = announceTypeSigned
recv := recvList.decode()
var a, reqType uint64
err := recv.get("allowedRequests", &a)
if err != nil {
t.Fatal(err)
}
if a != onlyAnnounceRequests {
t.Fatal("Expected onlyAnnounceRequests")
}
err = recv.get("announceType", &reqType)
if err != nil {
t.Fatal(err)
}

if reqType != announceTypeSigned {
t.Fatal("Expected announceTypeSigned")
}
},
},
network: test_networkid,
}

err := p.Handshake(td, hash, headNum, genesis, s)
if err != nil {
t.Fatalf("Handshake error: %s", err)
}

if p.announceType != announceTypeSigned {
t.Fatal("Incorrect announceType")
}
}

func TestPeer_Handshake_AnnounceTypeSigned_ForTrustedPeers_PeerNotInTrusted_Fail(t *testing.T) {
var id discover.NodeID
rand.Read(id[:])

p := peer{
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
version: protocol_version,
rw: &rwStub{},
rw: &rwStub{
WriteHook: func(recvList keyValueList) {
//checking that ulc sends to peer allowedRequests=onlyAnnounceRequests and announceType = announceTypeSigned
recv := recvList.decode()
var a, reqType uint64
err := recv.get("allowedRequests", &a)
if err != nil {
t.Fatal(err)
}

if a != noRequests {
t.Fatal("Expected noRequests")
}
err = recv.get("announceType", &reqType)
if err != nil {
t.Fatal(err)
}

if reqType == announceTypeSigned {
t.Fatal("Expected not announceTypeSigned")
}
},
},
network: test_networkid,
}

s := generateLesServer()
s.ulc = newULC(&eth.ULCConfig{
TrustedNodes: []string{},
})

err := p.Handshake(td, hash, headNum, genesis, s)
if err != nil {
Expand Down Expand Up @@ -86,7 +140,8 @@ func generateLesServer() *LesServer {
}

type rwStub struct {
WriteAssert func(m p2p.Msg)
ReadHook func(l keyValueList) keyValueList
WriteHook func(l keyValueList)
}

func (s *rwStub) ReadMsg() (p2p.Msg, error) {
Expand All @@ -105,6 +160,10 @@ func (s *rwStub) ReadMsg() (p2p.Msg, error) {
payload = payload.add("flowControl/BL", uint64(300000000))
payload = payload.add("flowControl/MRR", uint64(50000))

if s.ReadHook != nil {
payload = s.ReadHook(payload)
}

size, p, err := rlp.EncodeToReader(payload)
if err != nil {
return p2p.Msg{}, err
Expand All @@ -115,16 +174,16 @@ func (s *rwStub) ReadMsg() (p2p.Msg, error) {
Payload: p,
}, nil
}

func (s *rwStub) WriteMsg(m p2p.Msg) error {
recvList := keyValueList{}
if err := m.Decode(&recvList); err != nil {
return err
}

recv := recvList.decode()
fmt.Println("WriteMsg: ", recv)
for k, v := range recv {
fmt.Println(k, v)
if s.WriteHook != nil {
s.WriteHook(recvList)
}

return nil
}
2 changes: 1 addition & 1 deletion les/serverpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestLoadNodes_LoadTrustedNodes_Success(t *testing.T) {
rand.Read(node.ID[:])

var wg sync.WaitGroup
q := make(chan struct{}, 0)
q := make(chan struct{})
sp := newServerPool(&dbStub{}, q, &wg)
sp.server = &p2p.Server{}
sp.server.TrustedNodes = []*discover.Node{
Expand Down
5 changes: 4 additions & 1 deletion les/txrelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ func (self *LesTxRelay) send(txs types.Transactions, count int) {
return peer.GetRequestCost(SendTxMsg, len(ll))
},
canSend: func(dp distPeer) bool {
return dp.(*peer) == pp
if dp.(*peer).allowedRequests != allRequests {
return dp.(*peer) == pp
}
return false
},
request: func(dp distPeer) func() {
peer := dp.(*peer)
Expand Down

0 comments on commit a26c3a0

Please sign in to comment.