mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 22:26:42 +00:00
p2p: implement naive version of discv4 forkid filtering
Simple version that does the filtering, but misses pipelining, waiting for ENRs to be retrieved one-by-one. Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
parent
2a1784baef
commit
721b494854
3 changed files with 40 additions and 1 deletions
|
|
@ -487,6 +487,15 @@ func (s *Ethereum) setupDiscovery() error {
|
||||||
s.discmix.AddSource(iter)
|
s.discmix.AddSource(iter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add DHT nodes from discv4.
|
||||||
|
if s.p2pServer.DiscoveryV4() != nil {
|
||||||
|
asyncFilter := s.p2pServer.DiscoveryV4().RequestENR
|
||||||
|
filter := eth.NewNodeFilter(s.blockchain)
|
||||||
|
iter := enode.AsyncFilter(s.p2pServer.DiscoveryV4().RandomNodes(), asyncFilter)
|
||||||
|
iter = enode.Filter(iter, filter)
|
||||||
|
s.discmix.AddSource(iter)
|
||||||
|
}
|
||||||
|
|
||||||
// Add DHT nodes from discv5.
|
// Add DHT nodes from discv5.
|
||||||
if s.p2pServer.DiscoveryV5() != nil {
|
if s.p2pServer.DiscoveryV5() != nil {
|
||||||
filter := eth.NewNodeFilter(s.blockchain)
|
filter := eth.NewNodeFilter(s.blockchain)
|
||||||
|
|
|
||||||
|
|
@ -152,6 +152,37 @@ func (f *filterIter) Next() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AsyncFilter wraps an iterator such that Next only returns nodes for which
|
||||||
|
// the 'check' function returns a (possibly modified) node.
|
||||||
|
func AsyncFilter(it Iterator, check func(*Node) (*Node, error)) Iterator {
|
||||||
|
return &AsyncFilterIter{it, nil, check}
|
||||||
|
}
|
||||||
|
|
||||||
|
type AsyncFilterIter struct {
|
||||||
|
it Iterator
|
||||||
|
buffer *Node
|
||||||
|
check func(*Node) (*Node, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *AsyncFilterIter) Next() bool {
|
||||||
|
for f.it.Next() {
|
||||||
|
nn, err := f.check(f.it.Node())
|
||||||
|
if err == nil {
|
||||||
|
f.buffer = nn
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *AsyncFilterIter) Node() *Node {
|
||||||
|
return f.buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *AsyncFilterIter) Close() {
|
||||||
|
f.it.Close()
|
||||||
|
}
|
||||||
|
|
||||||
// FairMix aggregates multiple node iterators. The mixer itself is an iterator which ends
|
// FairMix aggregates multiple node iterators. The mixer itself is an iterator which ends
|
||||||
// only when Close is called. Source iterators added via AddSource are removed from the
|
// only when Close is called. Source iterators added via AddSource are removed from the
|
||||||
// mix when they end.
|
// mix when they end.
|
||||||
|
|
|
||||||
|
|
@ -483,7 +483,6 @@ func (srv *Server) setupDiscovery() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
srv.discv4 = ntab
|
srv.discv4 = ntab
|
||||||
srv.discmix.AddSource(ntab.RandomNodes())
|
|
||||||
}
|
}
|
||||||
if srv.Config.DiscoveryV5 {
|
if srv.Config.DiscoveryV5 {
|
||||||
cfg := discover.Config{
|
cfg := discover.Config{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue