Class: BtcWireProto::Message

Inherits:
BinData::Record
  • Object
show all
Defined in:
lib/btc_wire_proto.rb

Overview

Everything on the wire is a Message.

Author:

Instance Method Summary (collapse)

Instance Method Details

- (Object) initialize_instance(v = nil)

@param version The protocol version. Setting this affects the layout of various fields.



417
418
419
420
# File 'lib/btc_wire_proto.rb', line 417

def initialize_instance(v = nil)
  super()
  @version = v || ::BtcWireProto::CURRENT_VERSION(:main)
end

- (Object) payload_choice

Works out what the payload looks like based on the MessageHdr struct and (potentially) the version

Raises:

  • (NotImplementedError)


443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/btc_wire_proto.rb', line 443

def payload_choice
  cmd = header.command.to_s.strip
  return cmd if %w{
    version inv getdata getblocks getheaders tx block headers alert
  }.include?(cmd)
  
  # We can't parse these yet, and so we don't know where in the stream the
  # next message starts. So all we can do is throw an error
  raise NotImplementedError.new(
    "Received unsupported command #{cmd}"
  ) if %w|checkorder submitorder|.include?(cmd)
  
  # These commands don't have any payloads
  return "null" if %w|verack getaddr ping|.include?(cmd) || cmd == ""
  
  # Payload has two forms, depending on protocol version. Ugh.
  return (@version < 31402 ? "addr_pre31402" : "addr_from31402") if
    cmd == "addr"

  raise NotImplementedError.new("Unknown command: #{cmd}")  
end