Class: Ciri::DevP2P::Server

Inherits:
Object
  • Object
show all
Includes:
RLPX
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

Instance Method Summary collapse

Constructor Details

#initialize(private_key:, protocol_manage:, executor:) ⇒ Server


52
53
54
55
56
57
58
# File 'lib/ciri/devp2p/server.rb', line 52

def initialize(private_key:, protocol_manage:, executor:)
  @private_key = private_key
  @name = 'ciri'
  @scheduler = Scheduler.new(self, executor: executor)
  @protocol_manage = protocol_manage
  @protocols = protocol_manage.protocols
end

Instance Attribute Details

#bootstrap_nodesObject

Returns the value of attribute bootstrap_nodes


50
51
52
# File 'lib/ciri/devp2p/server.rb', line 50

def bootstrap_nodes
  @bootstrap_nodes
end

#dialObject (readonly)

Returns the value of attribute dial


49
50
51
# File 'lib/ciri/devp2p/server.rb', line 49

def dial
  @dial
end

#handshakeObject (readonly)

Returns the value of attribute handshake


49
50
51
# File 'lib/ciri/devp2p/server.rb', line 49

def handshake
  @handshake
end

#loggerObject

Returns the value of attribute logger


50
51
52
# File 'lib/ciri/devp2p/server.rb', line 50

def logger
  @logger
end

#protocol_manageObject (readonly)

Returns the value of attribute protocol_manage


49
50
51
# File 'lib/ciri/devp2p/server.rb', line 49

def protocol_manage
  @protocol_manage
end

#protocolsObject (readonly)

Returns the value of attribute protocols


49
50
51
# File 'lib/ciri/devp2p/server.rb', line 49

def protocols
  @protocols
end

#schedulerObject (readonly)

Returns the value of attribute scheduler


49
50
51
# File 'lib/ciri/devp2p/server.rb', line 49

def scheduler
  @scheduler
end

Instance Method Details

#count_matching_protocols(protocols, caps) ⇒ Object


88
89
90
91
# File 'lib/ciri/devp2p/server.rb', line 88

def count_matching_protocols(protocols, caps)
  #TODO implement this
  1
end

#protocol_handshake_checks(handshake) ⇒ Object


82
83
84
85
86
# File 'lib/ciri/devp2p/server.rb', line 82

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


74
75
76
77
78
79
80
# File 'lib/ciri/devp2p/server.rb', line 74

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

#startObject


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ciri/devp2p/server.rb', line 60

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.executor ||= @scheduler.executor
  @protocol_manage.start
  @scheduler.start
end