Class: Bitcoin::Network::Connection

Inherits:
EM::Connection
  • Object
show all
Includes:
MessageHandler
Defined in:
lib/bitcoin/network/connection.rb

Overview

Basic Bitcoin P2P connection class

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MessageHandler

#defer_handle_command, #handle, #handle_command, #handshake_done, #on_addr, #on_block, #on_cmpct_block, #on_fee_filter, #on_get_addr, #on_get_data, #on_get_headers, #on_headers, #on_inv, #on_mem_pool, #on_merkle_block, #on_not_found, #on_ping, #on_pong, #on_reject, #on_send_cmpct, #on_send_headers, #on_tx, #on_ver_ack, #on_version, #parse, #parse_header, #send_message

Constructor Details

#initialize(peer) ⇒ Connection

Returns a new instance of Connection.



21
22
23
24
25
26
27
28
# File 'lib/bitcoin/network/connection.rb', line 21

def initialize(peer)
  @peer = peer
  @logger = peer.logger
  @sendheaders = false
  @attr_accessor = 0
  @message = ''
  self.pending_connect_timeout = 5.0
end

Instance Attribute Details

#fee_rateObject

minimum fee(in satoshis per kilobyte) for relay tx



19
20
21
# File 'lib/bitcoin/network/connection.rb', line 19

def fee_rate
  @fee_rate
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/bitcoin/network/connection.rb', line 10

def logger
  @logger
end

#peerObject (readonly)

Returns the value of attribute peer.



10
11
12
# File 'lib/bitcoin/network/connection.rb', line 10

def peer
  @peer
end

#sendheadersObject

if true, this peer send new block announcements using a headers message rather than an inv message.



16
17
18
# File 'lib/bitcoin/network/connection.rb', line 16

def sendheaders
  @sendheaders
end

#versionObject

remote peer version.



13
14
15
# File 'lib/bitcoin/network/connection.rb', line 13

def version
  @version
end

Instance Method Details

#addrObject



45
46
47
# File 'lib/bitcoin/network/connection.rb', line 45

def addr
  peer.addr
end

#close(msg = '') ⇒ Object

close network connection.



50
51
52
53
# File 'lib/bitcoin/network/connection.rb', line 50

def close(msg = '')
  logger.info "close connection with #{addr}. #{msg}"
  close_connection_after_writing
end

#handle_error(e) ⇒ Object



55
56
57
# File 'lib/bitcoin/network/connection.rb', line 55

def handle_error(e)
  peer.handle_error(e)
end

#post_handshakeObject



41
42
43
# File 'lib/bitcoin/network/connection.rb', line 41

def post_handshake
  peer.post_handshake
end

#post_initObject



30
31
32
33
34
# File 'lib/bitcoin/network/connection.rb', line 30

def post_init
  logger.info "connected. #{addr}"
  peer.conn_time = Time.now.to_i
  begin_handshake
end

#receive_data(data) ⇒ Object

handle receiving data from remote node.



37
38
39
# File 'lib/bitcoin/network/connection.rb', line 37

def receive_data(data)
  handle(data)
end

#unbindObject



59
60
61
62
# File 'lib/bitcoin/network/connection.rb', line 59

def unbind
  logger.info "unbind. #{addr}"
  peer.unbind
end