diff options
author | Nick Johnson <arachnid@notdot.net> | 2017-06-28 20:25:08 +0800 |
---|---|---|
committer | Nick Johnson <arachnid@notdot.net> | 2017-06-29 19:49:18 +0800 |
commit | ae11545bc5fc0fcf4340d9f33d488eaf81d8bb2d (patch) | |
tree | 6ed575d314c8781e8e258a63a8c2d88753f896a1 /les/handler.go | |
parent | 055095798911e3c6f2e210220cb7ece1b71f6517 (diff) | |
download | dexon-ae11545bc5fc0fcf4340d9f33d488eaf81d8bb2d.tar.gz dexon-ae11545bc5fc0fcf4340d9f33d488eaf81d8bb2d.tar.zst dexon-ae11545bc5fc0fcf4340d9f33d488eaf81d8bb2d.zip |
eth, les: Refactor downloader peer to use structs
Diffstat (limited to 'les/handler.go')
-rw-r--r-- | les/handler.go | 120 |
1 files changed, 73 insertions, 47 deletions
diff --git a/les/handler.go b/les/handler.go index afd4356ef..967b31521 100644 --- a/les/handler.go +++ b/les/handler.go @@ -838,57 +838,83 @@ func (self *ProtocolManager) NodeInfo() *eth.EthNodeInfo { // downloaderPeerNotify implements peerSetNotify type downloaderPeerNotify ProtocolManager -func (d *downloaderPeerNotify) registerPeer(p *peer) { - pm := (*ProtocolManager)(d) +type peerConnection struct { + manager *ProtocolManager + peer *peer +} - requestHeadersByHash := func(origin common.Hash, amount int, skip int, reverse bool) error { - reqID := genReqID() - rq := &distReq{ - getCost: func(dp distPeer) uint64 { - peer := dp.(*peer) - return peer.GetRequestCost(GetBlockHeadersMsg, amount) - }, - canSend: func(dp distPeer) bool { - return dp.(*peer) == p - }, - request: func(dp distPeer) func() { - peer := dp.(*peer) - cost := peer.GetRequestCost(GetBlockHeadersMsg, amount) - peer.fcServer.QueueRequest(reqID, cost) - return func() { peer.RequestHeadersByHash(reqID, cost, origin, amount, skip, reverse) } - }, - } - _, ok := <-pm.reqDist.queue(rq) - if !ok { - return ErrNoPeers - } - return nil +func (pc *peerConnection) Head() (common.Hash, *big.Int) { + return pc.peer.HeadAndTd() +} + +func (pc *peerConnection) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error { + reqID := genReqID() + rq := &distReq{ + getCost: func(dp distPeer) uint64 { + peer := dp.(*peer) + return peer.GetRequestCost(GetBlockHeadersMsg, amount) + }, + canSend: func(dp distPeer) bool { + return dp.(*peer) == pc.peer + }, + request: func(dp distPeer) func() { + peer := dp.(*peer) + cost := peer.GetRequestCost(GetBlockHeadersMsg, amount) + peer.fcServer.QueueRequest(reqID, cost) + return func() { peer.RequestHeadersByHash(reqID, cost, origin, amount, skip, reverse) } + }, } - requestHeadersByNumber := func(origin uint64, amount int, skip int, reverse bool) error { - reqID := genReqID() - rq := &distReq{ - getCost: func(dp distPeer) uint64 { - peer := dp.(*peer) - return peer.GetRequestCost(GetBlockHeadersMsg, amount) - }, - canSend: func(dp distPeer) bool { - return dp.(*peer) == p - }, - request: func(dp distPeer) func() { - peer := dp.(*peer) - cost := peer.GetRequestCost(GetBlockHeadersMsg, amount) - peer.fcServer.QueueRequest(reqID, cost) - return func() { peer.RequestHeadersByNumber(reqID, cost, origin, amount, skip, reverse) } - }, - } - _, ok := <-pm.reqDist.queue(rq) - if !ok { - return ErrNoPeers - } - return nil + _, ok := <-pc.manager.reqDist.queue(rq) + if !ok { + return ErrNoPeers + } + return nil +} + +func (pc *peerConnection) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool) error { + reqID := genReqID() + rq := &distReq{ + getCost: func(dp distPeer) uint64 { + peer := dp.(*peer) + return peer.GetRequestCost(GetBlockHeadersMsg, amount) + }, + canSend: func(dp distPeer) bool { + return dp.(*peer) == pc.peer + }, + request: func(dp distPeer) func() { + peer := dp.(*peer) + cost := peer.GetRequestCost(GetBlockHeadersMsg, amount) + peer.fcServer.QueueRequest(reqID, cost) + return func() { peer.RequestHeadersByNumber(reqID, cost, origin, amount, skip, reverse) } + }, + } + _, ok := <-pc.manager.reqDist.queue(rq) + if !ok { + return ErrNoPeers } + return nil +} + +func (pc *peerConnection) RequestBodies(hashes []common.Hash) error { + panic("RequestBodies not supported in light client mode sync") + +} +func (pc *peerConnection) RequestReceipts(hashes []common.Hash) error { + panic("RequestReceipts not supported in light client mode sync") + +} + +func (pc *peerConnection) RequestNodeData(hashes []common.Hash) error { + panic("RequestNodeData not supported in light client mode sync") +} - pm.downloader.RegisterPeer(p.id, ethVersion, p.HeadAndTd, requestHeadersByHash, requestHeadersByNumber, nil, nil, nil) +func (d *downloaderPeerNotify) registerPeer(p *peer) { + pm := (*ProtocolManager)(d) + pc := &peerConnection{ + manager: pm, + peer: p, + } + pm.downloader.RegisterPeer(p.id, ethVersion, pc) } func (d *downloaderPeerNotify) unregisterPeer(p *peer) { |