Module: Bitcoin::ConnectionHandler

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

Instance Method Summary collapse

Instance Method Details

#on_addr(addr) ⇒ Object



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

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

#on_block(block) ⇒ Object



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

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



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

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

#on_get_transaction(hash) ⇒ Object



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

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

#on_handshake_beginObject



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

def on_handshake_begin
  block   = 127953
  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



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

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

  query_blocks
end

#on_inv_block(hash) ⇒ Object



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

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



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

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

#on_tx(tx) ⇒ Object



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

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

#on_verackObject



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

def on_verack
  on_handshake_complete
end

#on_version(version) ⇒ Object



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

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

#query_blocksObject



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

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