Class: Ciri::P2P::Server

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
RLPX, Utils::Logger
Defined in:
lib/ciri/p2p/server.rb

Overview

P2P Server maintain connection, node discovery, rlpx handshake

Constant Summary collapse

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::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:, protocols:, bootnodes: [], node_name: 'Ciri', host: '127.0.0.1', tcp_port: 33033, udp_port: 33033, max_outgoing: 10, max_incoming: 10, ping_interval_secs: 15, discovery_interval_secs: 15, dial_outgoing_interval_secs: 25) ⇒ Server

Returns a new instance of Server.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ciri/p2p/server.rb', line 56

def initialize(private_key:, protocols:, bootnodes: [],
               node_name: 'Ciri', host: '127.0.0.1',
               tcp_port: 33033, udp_port: 33033,
               max_outgoing: 10, max_incoming:10,
               ping_interval_secs: 15,
               discovery_interval_secs: 15,
               dial_outgoing_interval_secs: 25)
  @private_key = private_key
  @node_name = node_name
  # prepare handshake information
  @local_node_id = NodeID.new(@private_key)
  caps = protocols.map do |protocol|
    Cap.new(name: protocol.name, version: protocol.version)
  end
  @handshake = ProtocolHandshake.new(version: BASE_PROTOCOL_VERSION, name: @node_name, id: @local_node_id.id, caps: caps)
  @host = host
  @tcp_port = tcp_port
  @udp_port = udp_port
  @dialer = Dialer.new(private_key: private_key, handshake: @handshake)
  @peer_store = PeerStore.new
  @network_state = NetworkState.new(
    protocols: protocols,
    peer_store: @peer_store,
    local_node_id: @local_node_id,
    max_incoming: max_incoming,
    max_outgoing: max_outgoing,
    ping_interval_secs: ping_interval_secs)
  @bootnodes = bootnodes
  @discovery_interval_secs = discovery_interval_secs
  @dial_outgoing_interval_secs = dial_outgoing_interval_secs
end

Instance Attribute Details

#dial_schedulerObject (readonly)

Returns the value of attribute dial_scheduler.



52
53
54
# File 'lib/ciri/p2p/server.rb', line 52

def dial_scheduler
  @dial_scheduler
end

#dialerObject (readonly)

Returns the value of attribute dialer.



52
53
54
# File 'lib/ciri/p2p/server.rb', line 52

def dialer
  @dialer
end

#handshakeObject (readonly)

Returns the value of attribute handshake.



52
53
54
# File 'lib/ciri/p2p/server.rb', line 52

def handshake
  @handshake
end

#local_addressObject (readonly)

Returns the value of attribute local_address.



52
53
54
# File 'lib/ciri/p2p/server.rb', line 52

def local_address
  @local_address
end

#tcp_portObject (readonly)

Returns the value of attribute tcp_port.



52
53
54
# File 'lib/ciri/p2p/server.rb', line 52

def tcp_port
  @tcp_port
end

Instance Method Details

#runObject

return reactor to wait



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ciri/p2p/server.rb', line 98

def run
  # setup bootnodes
  @bootnodes.each do |node|
    @peer_store.add_bootnode(node)
  end

  # start server and services
  Async::Reactor.run do |task|
    # initialize protocols
    @network_state.initialize_protocols
    # wait sub tasks
    task.async do
      task.async do
        # Wait for server started listen
        # we use listened port to start DiscoveryService to allow 0 port
        task.sleep(0.5) until @local_address

        # start discovery service
        @discovery_service = Discovery::Service.new(
          peer_store: @peer_store,
          private_key: @private_key,
          host: @host, udp_port: @udp_port, tcp_port: @tcp_port,
          discovery_interval_secs: @discovery_interval_secs)
        task.async { @discovery_service.run }

        # start dial outgoing nodes
        @dial_scheduler = DialScheduler.new(
          @network_state,
          @dialer,
          dial_outgoing_interval_secs: @dial_outgoing_interval_secs)
        task.async {@dial_scheduler.run}
      end
      task.async {start_listen}
    end.wait
  end
end

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

start listen and accept clients



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ciri/p2p/server.rb', line 136

def start_listen(task: Async::Task.current)
  endpoint = Async::IO::Endpoint.tcp(@host, @tcp_port)
  endpoint.bind do |socket|
    @local_address = socket.local_address
    info("start accept connections -- listen on #{@local_address.getnameinfo.join(":")}")
    # update tcp_port if it is 0
    if @tcp_port.zero?
      @tcp_port = @local_address.ip_port
    end
    socket.listen(Socket::SOMAXCONN)
    loop do
      client, addrinfo = socket.accept
      c = Connection.new(client)
      c.encryption_handshake!(private_key: @private_key)
      remote_handshake = c.protocol_handshake!(handshake)
      @network_state.new_peer_connected(c, remote_handshake, direction: Peer::INCOMING)
    end
  end
end

#to_nodeObject



92
93
94
95
# File 'lib/ciri/p2p/server.rb', line 92

def to_node
  address = Address.new(ip: @host, tcp_port: tcp_port, udp_port: udp_port)
  Node.new(node_id: @local_node_id, addresses: [address])
end

#udp_portObject



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

def udp_port
  @discovery_service&.udp_port || @udp_port
end