Module: Bitcoin::ConnectionHandler

Included in:
Connection
Defined in:
lib/bitcoin/connection.rb

Overview

Handle messages received from node

Instance Method Summary collapse

Instance Method Details

#on_addr(addr) ⇒ Object



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

def on_addr(addr)
  p ['addr', addr, addr.alive?]
end

#on_block(block) ⇒ Object



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

def on_block(block)
  p ['block', block.hash]
  # p block.payload.each_byte.map{|i| "%02x" % [i] }.join(" ")
  # puts block.to_json
end

#on_get_block(hash) ⇒ Object



27
28
29
# File 'lib/bitcoin/connection.rb', line 27

def on_get_block(hash)
  p ['get block', hash.hth]
end

#on_get_transaction(hash) ⇒ Object



23
24
25
# File 'lib/bitcoin/connection.rb', line 23

def on_get_transaction(hash)
  p ['get transaction', hash.hth]
end

#on_handshake_beginObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/bitcoin/connection.rb', line 68

def on_handshake_begin
  block   = 127_953
  from    = '127.0.0.1:8333'
  from_id = Bitcoin::Protocol::Uniq
  to      = @sockaddr.reverse.join(':')
  # p "==", from_id, from, to, block
  pkt = Protocol.version_pkt(from_id, from, to, block)
  p ['sending version pkt', pkt]
  send_data(pkt)
end

#on_handshake_completeObject



54
55
56
57
58
59
# File 'lib/bitcoin/connection.rb', line 54

def on_handshake_complete
  p [@sockaddr, 'handshake complete']
  @connected = true

  query_blocks
end

#on_inv_block(hash) ⇒ Object



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

def on_inv_block(hash)
  p ['inv block', hash.hth]
  pkt = Protocol.getdata_pkt(:block, [hash])
  send_data(pkt)
end

#on_inv_transaction(hash) ⇒ Object



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

def on_inv_transaction(hash)
  p ['inv transaction', hash.hth]
  pkt = Protocol.getdata_pkt(:tx, [hash])
  send_data(pkt)
end

#on_tx(tx) ⇒ Object



35
36
37
# File 'lib/bitcoin/connection.rb', line 35

def on_tx(tx)
  p ['tx', tx.hash]
end

#on_verackObject



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

def on_verack
  on_handshake_complete
end

#on_version(version) ⇒ Object



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

def on_version(version)
  p [@sockaddr, 'version', version, version.time - Time.now.to_i]
  send_data(Protocol.verack_pkt)
end

#query_blocksObject



61
62
63
64
65
66
# File 'lib/bitcoin/connection.rb', line 61

def query_blocks
  start = ("\x00" * 32)
  stop  = ("\x00" * 32)
  pkt = Protocol.pkt('getblocks', "\x00" + start + stop)
  send_data(pkt)
end