lib/bitcoin/network/connection_handler.rb in bitcoin-ruby-0.0.5 vs lib/bitcoin/network/connection_handler.rb in bitcoin-ruby-0.0.6

- old
+ new

@@ -39,29 +39,30 @@ @port, @host = *Socket.unpack_sockaddr_in(get_peername) if get_peername @ping_nonce = nil @latency_ms = nil @lock = Monitor.new @last_getblocks = [] # the last few getblocks messages received - rescue Exception + rescue log.fatal { "Error in #initialize" } p $!; puts $@; exit end # check if connection is wanted, begin handshake if it is, disconnect if not def post_init if incoming? begin_handshake end - rescue Exception + rescue log.fatal { "Error in #post_init" } p $!; puts *$@ end # only called for outgoing connection def connection_completed + @connection_completed = true begin_handshake - rescue Exception + rescue log.fatal { "Error in #connection_completed" } p $!; puts *$@ end # receive data from peer and invoke Protocol::Parser @@ -73,12 +74,12 @@ p $!; puts *$@ end # connection closed; notify listeners and cleanup connection from node def unbind - log.info { "Disconnected" } - @node.push_notification(:connection, [:disconnected, [@host, @port]]) + log.info { (outgoing? && !@connection_completed) ? "Connection failed" : "Disconnected" } + @node.push_notification(:connection, {type: :disconnected, host: @host, port: @port}) @state = :disconnected @node.connections.delete(self) end # begin handshake @@ -88,26 +89,26 @@ return close_connection unless @node.config[:connect].include?([@host, @port.to_s]) end log.info { "Established #{@direction} connection" } @node.connections << self @state = :handshake - # incoming connections wait to receive a version - send_version if outgoing? - rescue Exception + send_version + rescue log.fatal { "Error in #begin_handshake" } p $!; puts *$@ end # complete handshake; set state, started time, notify listeners and add address to Node def complete_handshake if @state == :handshake log.debug { 'Handshake completed' } @state = :connected @started = Time.now - @node.push_notification(:connection, [:connected, info]) + @node.push_notification(:connection, info.merge(type: :connected)) @node.addrs << addr end + send_data P::Addr.pkt(@node.addr) if @node.config[:announce] end # received +inv_tx+ message for given +hash+. # add to inv_queue, unlesss maximum is reached def on_inv_transaction(hash) @@ -221,19 +222,19 @@ depth = blk[:depth] if blk log.info { ">> getblocks #{hashes[0]} (#{depth || 'unknown'})" } return unless depth && depth <= @node.store.get_depth range = (depth+1..depth+500) - blocks = @node.store.db[:blk].where(chain: 0, depth: range).select(:hash).all + - [@node.store.db[:blk].select(:hash)[chain: 0, depth: depth+502]] + blocks = @node.store.db[:blk].where(chain: 0, depth: range).order(:depth).select(:hash).all send_inv(:block, *blocks.map {|b| b[:hash].hth }) end # received +getaddr+ message. # send +addr+ message with peer addresses back. def on_getaddr - addrs = @node.addrs.select{|a| a.time > Time.now.to_i - 10800 }.shuffle[0..250] + addrs = @node.config[:announce] ? [@node.addr] : [] + addrs += @node.addrs.select{|a| a.time > Time.now.to_i - 10800 }.shuffle[0..250] log.debug { "<< addr (#{addrs.size})" } send_data P::Addr.pkt(*addrs) end # begin handshake; send +version+ message @@ -309,10 +310,10 @@ @ping_time = Time.now log.debug { "<< ping (#{@ping_nonce})" } send_data(Protocol.ping_pkt(@ping_nonce)) else # set latency to 5 seconds, terrible but this version should be obsolete now - @latency_ms = (5*1000) + @latency_ms = (5*1000) log.debug { "<< ping" } send_data(Protocol.ping_pkt) end end