Skip to content

Commit

Permalink
change announce only direction
Browse files Browse the repository at this point in the history
  • Loading branch information
b00ris committed May 3, 2018
1 parent 3db20a8 commit c535734
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 43 deletions.
4 changes: 2 additions & 2 deletions les/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func (f *lightFetcher) newFetcherDistReqForSync(bestHash common.Hash) *distReq {
},
canSend: func(dp distPeer) bool {
p := dp.(*peer)
if p.allowedRequests != allRequests {
if p.isOnlyAnnounce {
return false
}
f.lock.Lock()
Expand Down Expand Up @@ -535,7 +535,7 @@ func (f *lightFetcher) newFetcherDistReq(bestHash common.Hash, reqID uint64, bes
},
canSend: func(dp distPeer) bool {
p := dp.(*peer)
if p.allowedRequests != allRequests {
if p.isOnlyAnnounce {
return false
}

Expand Down
8 changes: 0 additions & 8 deletions les/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,10 +1226,6 @@ 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 @@ -1255,10 +1251,6 @@ func (pc *peerConnection) RequestHeadersByNumber(origin uint64, amount int, skip
},
canSend: func(dp distPeer) bool {
p := dp.(*peer)
if p.allowedRequests != allRequests {
return false
}

return p == pc.peer
},
request: func(dp distPeer) func() {
Expand Down
2 changes: 1 addition & 1 deletion les/odr.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (odr *LesOdr) Retrieve(ctx context.Context, req light.OdrRequest) (err erro
},
canSend: func(dp distPeer) bool {
p := dp.(*peer)
if p.allowedRequests == allRequests {
if !p.isOnlyAnnounce {
return lreq.CanSend(p)
}
return false
Expand Down
34 changes: 13 additions & 21 deletions les/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ const (
announceTypeSigned
)

const (
allRequests = iota
onlyAnnounceRequests
noRequests
)

type peer struct {
*p2p.Peer
pubKey *ecdsa.PublicKey
Expand Down Expand Up @@ -83,8 +77,8 @@ type peer struct {
fcServerParams *flowcontrol.ServerParams
fcCosts requestCostTable

isTrusted bool
allowedRequests uint64
isTrusted bool
isOnlyAnnounce bool
}

func newPeer(version int, network uint64, isTrusted bool, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
Expand Down Expand Up @@ -415,10 +409,13 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis
send = send.add("headNum", headNum)
send = send.add("genesisHash", genesis)
if server != nil {
send = send.add("serveHeaders", nil)
send = send.add("serveChainSince", uint64(0))
send = send.add("serveStateSince", uint64(0))
send = send.add("txRelay", nil)
if !server.onlyAnnounse {
//only announce server. It sends only announse requests
send = send.add("serveHeaders", nil)
send = send.add("serveChainSince", uint64(0))
send = send.add("serveStateSince", uint64(0))
send = send.add("txRelay", nil)
}
send = send.add("flowControl/BL", server.defParams.BufLimit)
send = send.add("flowControl/MRR", server.defParams.MinRecharge)
list := server.fcCostStats.getCurrentList()
Expand All @@ -428,7 +425,6 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis
//on client node
p.announceType = announceTypeSimple
if p.isTrusted {
send = send.add("allowedRequests", uint64(onlyAnnounceRequests))
p.announceType = uint64(announceTypeSigned)
}
send = send.add("announceType", p.announceType)
Expand Down Expand Up @@ -484,20 +480,16 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis
p.announceType = announceTypeSimple
}
p.fcClient = flowcontrol.NewClientNode(server.fcManager, server.defParams)

//which requests could receive the peer
if recv.get("allowedRequests", &p.allowedRequests) != nil {
p.allowedRequests = allRequests
}
} else {
//mark OnlyAnnounce server if "serveHeaders", "serveChainSince", "serveStateSince" or "txRelay" fields don't exist
if recv.get("serveChainSince", nil) != nil {
return errResp(ErrUselessPeer, "peer cannot serve chain")
p.isOnlyAnnounce = true
}
if recv.get("serveStateSince", nil) != nil {
return errResp(ErrUselessPeer, "peer cannot serve state")
p.isOnlyAnnounce = true
}
if recv.get("txRelay", nil) != nil {
return errResp(ErrUselessPeer, "peer cannot relay transactions")
p.isOnlyAnnounce = true
}
params := &flowcontrol.ServerParams{}
if err := recv.get("flowControl/BL", &params.BufLimit); err != nil {
Expand Down
84 changes: 74 additions & 10 deletions les/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,9 @@ func TestPeerHandshakeSetAnnounceTypeToAnnounceTypeSignedForTrustedPeer(t *testi
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)
var reqType uint64

err := recv.get("announceType", &reqType)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -148,11 +142,81 @@ func TestPeerHandshakeDefaultAllRequests(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if p.allowedRequests != allRequests {
if p.isOnlyAnnounce != false {
t.Fatal("Incorrect announceType")
}
}

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

s := generateLesServer()
s.onlyAnnounse = 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) {
var id discover.NodeID
rand.Read(id[:])

p := peer{
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
version: protocol_version,
rw: &rwStub{
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))
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.Fatal(err)
}

if p.isOnlyAnnounce == false {
t.Fatal("isOnlyAnnounce must be true")
}
}

func generateLesServer() *LesServer {
s := &LesServer{
defParams: &flowcontrol.ServerParams{
Expand Down
1 change: 1 addition & 0 deletions les/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type LesServer struct {
lesTopics []discv5.Topic
privateKey *ecdsa.PrivateKey
quitSync chan struct{}
onlyAnnounse bool

chtIndexer, bloomTrieIndexer *core.ChainIndexer
}
Expand Down
2 changes: 1 addition & 1 deletion les/txrelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (self *LesTxRelay) send(txs types.Transactions, count int) {
return peer.GetRequestCost(SendTxMsg, len(ll))
},
canSend: func(dp distPeer) bool {
if dp.(*peer).allowedRequests != allRequests {
if !dp.(*peer).isOnlyAnnounce {
return dp.(*peer) == pp
}
return false
Expand Down

0 comments on commit c535734

Please sign in to comment.