Class: Bitcoin::Protocol::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoin/protocol/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler = nil) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
13
# File 'lib/bitcoin/protocol/parser.rb', line 9

def initialize(handler=nil)
  @h = handler || Handler.new
  @buf = ""
  @stats = { 'total_packets' => 0, 'total_bytes' => 0, 'total_errors' => 0 }
end

Instance Attribute Details

#statsObject (readonly)

Returns the value of attribute stats.



7
8
9
# File 'lib/bitcoin/protocol/parser.rb', line 7

def stats
  @stats
end

Instance Method Details

#handle_mempool_request(payload) ⇒ Object



111
112
113
114
115
# File 'lib/bitcoin/protocol/parser.rb', line 111

def handle_mempool_request(payload)
  return unless @version.fields[:version] >= 60002           # Protocol version >= 60002
  return unless (@version.fields[:services] & Bitcoin::Protocol::Version::NODE_NETWORK) == 1 # NODE_NETWORK bit set in Services
  @h.on_mempool if @h.respond_to?(:on_mempool)
end

#handle_notfound_reply(payload) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/bitcoin/protocol/parser.rb', line 117

def handle_notfound_reply(payload)
  return unless @h.respond_to?(:on_notfound)
  count, payload = Protocol.unpack_var_int(payload)
  payload.each_byte.each_slice(36) do |i|
    hash = i[4..-1].reverse.pack("C32")
    case i[0]
    when 1; @h.on_notfound(:tx, hash)
    when 2; @h.on_notfound(:block, hash)
    else
      parse_error(:notfound, [i.pack("C*"), hash])
    end
  end
end

#handle_reject(payload) ⇒ Object



105
106
107
108
# File 'lib/bitcoin/protocol/parser.rb', line 105

def handle_reject(payload)
  return unless @h.respond_to?(:on_reject)
  @h.on_reject Bitcoin::Protocol::Reject.parse(payload)
end

#handle_stream_error(type, msg) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/bitcoin/protocol/parser.rb', line 168

def handle_stream_error(type, msg)
  case type
  when :close
    log.debug {"closing packet stream (#{msg})"}
  else
    log.debug { [type, msg] }
  end
end

#logObject



15
# File 'lib/bitcoin/protocol/parser.rb', line 15

def log; @log ||= Bitcoin::Logger.create("parser"); end

#parse(buf) ⇒ Object



131
132
133
134
135
# File 'lib/bitcoin/protocol/parser.rb', line 131

def parse(buf)
  @buf += buf
  while parse_buffer; end
  @buf
end

#parse_addr(payload) ⇒ Object



41
42
43
44
45
46
# File 'lib/bitcoin/protocol/parser.rb', line 41

def parse_addr(payload)
  count, payload = Protocol.unpack_var_int(payload)
  payload.each_byte.each_slice(30) do |i|
    @h.on_addr(Addr.new(i.pack("C*"))) rescue parse_error(:addr, i.pack("C*"))
  end
end

#parse_alert(payload) ⇒ Object



100
101
102
103
# File 'lib/bitcoin/protocol/parser.rb', line 100

def parse_alert(payload)
  return unless @h.respond_to?(:on_alert)
  @h.on_alert Bitcoin::Protocol::Alert.parse(payload)
end

#parse_bufferObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/bitcoin/protocol/parser.rb', line 137

def parse_buffer
  head_magic = Bitcoin::network[:magic_head]
  head_size  = 24
  return false if @buf.size < head_size

  magic, cmd, length, checksum = @buf.unpack("a4A12Va4")
  payload = @buf[head_size...head_size+length]

  unless magic == head_magic
    handle_stream_error(:close, "head_magic not found")
    @buf = ''
  else

    if Digest::SHA256.digest(Digest::SHA256.digest( payload ))[0...4] != checksum
      if (length < 50000) && (payload.size < length)
        size_info = [payload.size, length].join('/')
        handle_stream_error(:debug, "chunked packet stream (#{size_info})")
      else
        handle_stream_error(:close, "checksum mismatch")
      end
      return
    end
    @buf = @buf[head_size+length..-1] || ""

    process_pkt(cmd, payload)
  end

  # not empty yet? parse more.
  @buf[0] != nil
end

#parse_error(*err) ⇒ Object



177
178
179
180
181
# File 'lib/bitcoin/protocol/parser.rb', line 177

def parse_error *err
  @stats['total_errors'] += 1
  return unless @h.respond_to?(:on_error)
  @h.on_error *err
end

#parse_getblocks(payload) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/bitcoin/protocol/parser.rb', line 59

def parse_getblocks(payload)
  version, payload = payload.unpack('Va*')
  count,   payload = Protocol.unpack_var_int(payload)
  buf,     payload = payload.unpack("a#{count*32}a*")
  hashes    = buf.each_byte.each_slice(32).map{|i| hash = i.reverse.pack("C32").hth }
  stop_hash = payload[0..32].reverse_hth
  [version, hashes, stop_hash]
end

#parse_headers(payload) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/bitcoin/protocol/parser.rb', line 48

def parse_headers(payload)
  return unless @h.respond_to?(:on_headers)
  buf = StringIO.new(payload)
  count = Protocol.unpack_var_int_from_io(buf)
  headers = count.times.map{
    break if buf.eof?
    b = Block.new; b.parse_data_from_io(buf, header_only=true); b
  }
  @h.on_headers(headers)
end

#parse_inv(payload, type = :put) ⇒ Object

handles inv/getdata packets



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bitcoin/protocol/parser.rb', line 18

def parse_inv(payload, type=:put)
  count, payload = Protocol.unpack_var_int(payload)
  payload.each_byte.each_slice(36).with_index do |i, idx|
    hash = i[4..-1].reverse.pack("C32")
    case i[0]
    when 1
      type == :put ? @h.on_inv_transaction(hash) : @h.on_get_transaction(hash)
    when 2
      if type == :put
        if @h.respond_to?(:on_inv_block_v2)
          @h.on_inv_block_v2(hash, idx, count)
        else
          @h.on_inv_block(hash)
        end
      else
        @h.on_get_block(hash)
      end
    else
      parse_error :parse_inv, i.pack("C*")
    end
  end
end

#parse_version(payload) ⇒ Object



95
96
97
98
# File 'lib/bitcoin/protocol/parser.rb', line 95

def parse_version(payload)
  @version = Bitcoin::Protocol::Version.parse(payload)
  @h.on_version(@version)
end

#process_pkt(command, payload) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bitcoin/protocol/parser.rb', line 68

def process_pkt(command, payload)
  @stats['total_packets'] += 1
  @stats['total_bytes'] += payload.bytesize
  @stats[command] ? (@stats[command] += 1) : @stats[command] = 1
  case command
  when 'tx';       @h.on_tx( Tx.new(payload) )
  when 'block';    @h.on_block( Block.new(payload) )
  when 'headers';  parse_headers(payload)
  when 'inv';      parse_inv(payload, :put)
  when 'getdata';  parse_inv(payload, :get)
  when 'addr';     parse_addr(payload)
  when 'getaddr';  @h.on_getaddr  if @h.respond_to?(:on_getaddr)
  when 'verack';   @h.respond_to?(:on_verack) ? @h.on_verack : (@h.respond_to?(:on_handshake_complete) ? @h.on_handshake_complete : nil)
  when 'version';  parse_version(payload)
  when 'alert';    parse_alert(payload)
  when 'ping';     @h.on_ping(payload.unpack("Q")[0])
  when 'pong';     @h.on_pong(payload.unpack("Q")[0])
  when 'getblocks';   @h.on_getblocks(*parse_getblocks(payload))  if @h.respond_to?(:on_getblocks)
  when 'getheaders';  @h.on_getheaders(*parse_getblocks(payload))  if @h.respond_to?(:on_getheaders)
  when 'mempool';  handle_mempool_request(payload)
  when 'notfound'; handle_notfound_reply(payload)
  when 'reject'; handle_reject(payload)
  else
    parse_error :unknown_packet, [command, payload.hth]
  end
end