@@ -36,6 +36,7 @@ import (
3636
3737type LesOdrRequest interface {
3838 GetCost (* peer ) uint64
39+ CanSend (* peer ) bool
3940 Request (uint64 , * peer ) error
4041 Valid (ethdb.Database , * Msg ) bool // if true, keeps the retrieved object
4142}
@@ -66,6 +67,11 @@ func (self *BlockRequest) GetCost(peer *peer) uint64 {
6667 return peer .GetRequestCost (GetBlockBodiesMsg , 1 )
6768}
6869
70+ // CanSend tells if a certain peer is suitable for serving the given request
71+ func (self * BlockRequest ) CanSend (peer * peer ) bool {
72+ return peer .HasBlock (self .Hash , self .Number )
73+ }
74+
6975// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
7076func (self * BlockRequest ) Request (reqID uint64 , peer * peer ) error {
7177 glog .V (logger .Debug ).Infof ("ODR: requesting body of block %08x from peer %v" , self .Hash [:4 ], peer .id )
@@ -121,6 +127,11 @@ func (self *ReceiptsRequest) GetCost(peer *peer) uint64 {
121127 return peer .GetRequestCost (GetReceiptsMsg , 1 )
122128}
123129
130+ // CanSend tells if a certain peer is suitable for serving the given request
131+ func (self * ReceiptsRequest ) CanSend (peer * peer ) bool {
132+ return peer .HasBlock (self .Hash , self .Number )
133+ }
134+
124135// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
125136func (self * ReceiptsRequest ) Request (reqID uint64 , peer * peer ) error {
126137 glog .V (logger .Debug ).Infof ("ODR: requesting receipts for block %08x from peer %v" , self .Hash [:4 ], peer .id )
@@ -171,6 +182,11 @@ func (self *TrieRequest) GetCost(peer *peer) uint64 {
171182 return peer .GetRequestCost (GetProofsMsg , 1 )
172183}
173184
185+ // CanSend tells if a certain peer is suitable for serving the given request
186+ func (self * TrieRequest ) CanSend (peer * peer ) bool {
187+ return peer .HasBlock (self .Id .BlockHash , self .Id .BlockNumber )
188+ }
189+
174190// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
175191func (self * TrieRequest ) Request (reqID uint64 , peer * peer ) error {
176192 glog .V (logger .Debug ).Infof ("ODR: requesting trie root %08x key %08x from peer %v" , self .Id .Root [:4 ], self .Key [:4 ], peer .id )
@@ -221,6 +237,11 @@ func (self *CodeRequest) GetCost(peer *peer) uint64 {
221237 return peer .GetRequestCost (GetCodeMsg , 1 )
222238}
223239
240+ // CanSend tells if a certain peer is suitable for serving the given request
241+ func (self * CodeRequest ) CanSend (peer * peer ) bool {
242+ return peer .HasBlock (self .Id .BlockHash , self .Id .BlockNumber )
243+ }
244+
224245// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
225246func (self * CodeRequest ) Request (reqID uint64 , peer * peer ) error {
226247 glog .V (logger .Debug ).Infof ("ODR: requesting node data for hash %08x from peer %v" , self .Hash [:4 ], peer .id )
@@ -274,6 +295,14 @@ func (self *ChtRequest) GetCost(peer *peer) uint64 {
274295 return peer .GetRequestCost (GetHeaderProofsMsg , 1 )
275296}
276297
298+ // CanSend tells if a certain peer is suitable for serving the given request
299+ func (self * ChtRequest ) CanSend (peer * peer ) bool {
300+ peer .lock .RLock ()
301+ defer peer .lock .RUnlock ()
302+
303+ return self .ChtNum <= (peer .headInfo .Number - light .ChtConfirmations )/ light .ChtFrequency
304+ }
305+
277306// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
278307func (self * ChtRequest ) Request (reqID uint64 , peer * peer ) error {
279308 glog .V (logger .Debug ).Infof ("ODR: requesting CHT #%d block #%d from peer %v" , self .ChtNum , self .BlockNum , peer .id )
0 commit comments