Module: EventMachine::Bitcoin::ConnectionHandler

Included in:
Connection
Defined in:
lib/em_bitcoin.rb

Instance Method Summary collapse

Instance Method Details

#on_addr(addr) ⇒ Object



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

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

#on_block(block) ⇒ Object



39
40
41
# File 'lib/em_bitcoin.rb', line 39

def on_block(block)
  p ['block', block.hash]
end

#on_get_block(hash) ⇒ Object



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

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

#on_get_transaction(hash) ⇒ Object



19
20
21
# File 'lib/em_bitcoin.rb', line 19

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

#on_handshake_beginObject



66
67
68
69
70
71
72
73
# File 'lib/em_bitcoin.rb', line 66

def on_handshake_begin
  pkt = Bitcoin::Protocol::Version.new(
    from:       "127.0.0.1:8333",
    to:         @sockaddr.join(":"),
  ).to_pkt
  p ['sending version pkt', pkt]
  send_data(pkt)
end

#on_handshake_completeObject



52
53
54
55
56
57
# File 'lib/em_bitcoin.rb', line 52

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

  query_blocks
end

#on_inv_block(hash) ⇒ Object



13
14
15
16
17
# File 'lib/em_bitcoin.rb', line 13

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

#on_inv_transaction(hash) ⇒ Object



7
8
9
10
11
# File 'lib/em_bitcoin.rb', line 7

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

#on_ping(nonce) ⇒ Object



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

def on_ping(nonce)
  send_data(Bitcoin::Protocol.pong_pkt(nonce))
end

#on_tx(tx) ⇒ Object



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

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

#on_verackObject



48
49
50
# File 'lib/em_bitcoin.rb', line 48

def on_verack
  on_handshake_complete
end

#on_version(version) ⇒ Object



43
44
45
46
# File 'lib/em_bitcoin.rb', line 43

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

#query_blocksObject



59
60
61
62
63
64
# File 'lib/em_bitcoin.rb', line 59

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