Class: Ciri::P2P::Discovery::Service

Inherits:
Object
  • Object
show all
Includes:
Protocol, Utils::Logger
Defined in:
lib/ciri/p2p/discovery/service.rb

Overview

Implement the DiscV4 protocol github.com/ethereum/devp2p/blob/master/discv4.md notice difference between PeerStore and Kad, we use PeerStore to store all peers we known(upon 8192), and use Kad to store our neighbours for discovery query.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(peer_store:, host:, udp_port:, tcp_port:, private_key:, discovery_interval_secs: 15) ⇒ Service

we should consider search from peer_store instead connect to bootnodes everytime



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ciri/p2p/discovery/service.rb', line 60

def initialize(peer_store:, host:, udp_port:, tcp_port:, private_key:, discovery_interval_secs: 15)
  @discovery_interval_secs = discovery_interval_secs
  @cache = Set.new
  @host = host
  @udp_port = udp_port
  @tcp_port = tcp_port
  @peer_store = peer_store
  @private_key = private_key
  @local_node_id = NodeID.new(private_key)
  @kad_table = Kad::RoutingTable.new(local_node: Kad::Node.new(@local_node_id.to_bytes))
  setup_kad_table
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



57
58
59
# File 'lib/ciri/p2p/discovery/service.rb', line 57

def host
  @host
end

#local_node_idObject (readonly)

Returns the value of attribute local_node_id.



57
58
59
# File 'lib/ciri/p2p/discovery/service.rb', line 57

def local_node_id
  @local_node_id
end

#peer_storeObject (readonly)

Returns the value of attribute peer_store.



57
58
59
# File 'lib/ciri/p2p/discovery/service.rb', line 57

def peer_store
  @peer_store
end

#tcp_portObject (readonly)

Returns the value of attribute tcp_port.



57
58
59
# File 'lib/ciri/p2p/discovery/service.rb', line 57

def tcp_port
  @tcp_port
end

#udp_portObject (readonly)

Returns the value of attribute udp_port.



57
58
59
# File 'lib/ciri/p2p/discovery/service.rb', line 57

def udp_port
  @udp_port
end

Instance Method Details

#run(task: Async::Task.current) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ciri/p2p/discovery/service.rb', line 73

def run(task: Async::Task.current)
  # start listening
  task.async do
    start_listen
  end
  # search peers every x seconds
  task.reactor.every(@discovery_interval_secs) do
    task.async do
      perform_discovery
    end
  end
end