Class: Ciri::DevP2P::Server
- Inherits:
-
Object
- Object
- Ciri::DevP2P::Server
- Includes:
- RLPX, Utils::Logger
- Defined in:
- lib/ciri/devp2p/server.rb
Overview
DevP2P Server maintain connection, node discovery, rlpx handshake and protocols
Defined Under Namespace
Classes: Dial, Error, Scheduler, Task, UselessPeerError
Constant Summary collapse
- MAX_ACTIVE_DIAL_TASKS =
16- DEFAULT_MAX_PENDING_PEERS =
50- DEFAULT_DIAL_RATIO =
3
Constants included from RLPX
RLPX::AUTH_MSG_LENGTH, RLPX::AUTH_RESP_MSG_LENGTH, RLPX::BASE_PROTOCOL_LENGTH, RLPX::BASE_PROTOCOL_MAX_MSG_SIZE, RLPX::BASE_PROTOCOL_VERSION, RLPX::ECIES_OVERHEAD, RLPX::ENC_AUTH_MSG_LENGTH, RLPX::ENC_AUTH_RESP_MSG_LENGTH, RLPX::HANDSHAKE_TIMEOUT, RLPX::MESSAGES, RLPX::PUBLIC_KEY_LENGTH, RLPX::SHA_LENGTH, RLPX::SIGNATURE_LENGTH, RLPX::SNAPPY_PROTOCOL_VERSION
Instance Attribute Summary collapse
-
#bootstrap_nodes ⇒ Object
Returns the value of attribute bootstrap_nodes.
-
#dial ⇒ Object
readonly
Returns the value of attribute dial.
-
#handshake ⇒ Object
readonly
Returns the value of attribute handshake.
-
#protocol_manage ⇒ Object
readonly
Returns the value of attribute protocol_manage.
-
#protocols ⇒ Object
readonly
Returns the value of attribute protocols.
-
#scheduler ⇒ Object
readonly
Returns the value of attribute scheduler.
Instance Method Summary collapse
- #count_matching_protocols(protocols, caps) ⇒ Object
-
#initialize(private_key:, protocol_manage:) ⇒ Server
constructor
A new instance of Server.
- #protocol_handshake_checks(handshake) ⇒ Object
- #setup_connection(node) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(private_key:, protocol_manage:) ⇒ Server
Returns a new instance of Server.
48 49 50 51 52 53 54 |
# File 'lib/ciri/devp2p/server.rb', line 48 def initialize(private_key:, protocol_manage:) @private_key = private_key @name = 'ciri' @scheduler = Scheduler.new(self) @protocol_manage = protocol_manage @protocols = protocol_manage.protocols end |
Instance Attribute Details
#bootstrap_nodes ⇒ Object
Returns the value of attribute bootstrap_nodes.
46 47 48 |
# File 'lib/ciri/devp2p/server.rb', line 46 def bootstrap_nodes @bootstrap_nodes end |
#dial ⇒ Object (readonly)
Returns the value of attribute dial.
45 46 47 |
# File 'lib/ciri/devp2p/server.rb', line 45 def dial @dial end |
#handshake ⇒ Object (readonly)
Returns the value of attribute handshake.
45 46 47 |
# File 'lib/ciri/devp2p/server.rb', line 45 def handshake @handshake end |
#protocol_manage ⇒ Object (readonly)
Returns the value of attribute protocol_manage.
45 46 47 |
# File 'lib/ciri/devp2p/server.rb', line 45 def protocol_manage @protocol_manage end |
#protocols ⇒ Object (readonly)
Returns the value of attribute protocols.
45 46 47 |
# File 'lib/ciri/devp2p/server.rb', line 45 def protocols @protocols end |
#scheduler ⇒ Object (readonly)
Returns the value of attribute scheduler.
45 46 47 |
# File 'lib/ciri/devp2p/server.rb', line 45 def scheduler @scheduler end |
Instance Method Details
#count_matching_protocols(protocols, caps) ⇒ Object
83 84 85 86 |
# File 'lib/ciri/devp2p/server.rb', line 83 def count_matching_protocols(protocols, caps) #TODO implement this 1 end |
#protocol_handshake_checks(handshake) ⇒ Object
77 78 79 80 81 |
# File 'lib/ciri/devp2p/server.rb', line 77 def protocol_handshake_checks(handshake) if !protocols.empty? && count_matching_protocols(protocols, handshake.caps) == 0 raise UselessPeerError.new('discovery useless peer') end end |
#setup_connection(node) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/ciri/devp2p/server.rb', line 69 def setup_connection(node) socket = TCPSocket.new(node.ip, node.tcp_port) c = Connection.new(socket) c.encryption_handshake!(private_key: @private_key, node_id: node.node_id) remote_handshake = c.protocol_handshake!(handshake) scheduler << [:add_peer, c, remote_handshake] end |
#start ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ciri/devp2p/server.rb', line 56 def start #TODO start dialer, discovery nodes and connect to them #TODO listen udp, for discovery protocol server_node_id = NodeID.new(@private_key) caps = [Cap.new(name: 'eth', version: 63)] @handshake = ProtocolHandshake.new(version: BASE_PROTOCOL_VERSION, name: @name, id: server_node_id.id, caps: caps) # start listen tcp @dial = Dial.new(self) @protocol_manage.start @scheduler.start end |