From 5d4f44e7763c4038c997f778000448d4473eff2b Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Tue, 21 Jun 2011 22:53:04 +0100 Subject: [PATCH] Add a convenience method to determine the network a message is from --- lib/btc_wire_proto.rb | 9 ++++++++- spec/btc_wire_proto_spec.rb | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/btc_wire_proto.rb b/lib/btc_wire_proto.rb index 630f727..44fb2d5 100644 --- a/lib/btc_wire_proto.rb +++ b/lib/btc_wire_proto.rb @@ -428,7 +428,7 @@ module BtcWireProto string :command, :length => 12 uint32 :payload_len, :value => lambda { payload.num_bytes } uint32 :checksum, :onlyif => :has_checksum? - + choice :payload, :selection => :payload_choice do version "version" addr_pre31402 "addr_pre31402" @@ -446,10 +446,17 @@ module BtcWireProto null_payload "null" end + # @return[Symbol] representation of the command this message contains def cmd_sym c = command.strip c == "" ? nil : c.to_sym end + + # @return[Symbol] representation of the network this message belongs to + def network_name + NETWORKS[magic] + end + # version and verack messages don't have a checksum. The rest do. # @return[Boolean] does this message header have a checksum field or not? def has_checksum? diff --git a/spec/btc_wire_proto_spec.rb b/spec/btc_wire_proto_spec.rb index fc18108..dcc0d0b 100644 --- a/spec/btc_wire_proto_spec.rb +++ b/spec/btc_wire_proto_spec.rb @@ -300,6 +300,7 @@ describe ::BtcWireProto do m = Message::read(GOOD_VERSION_DATA) m.magic.should == BtcWireProto::NETWORKS[:main] + m.network_name.should == :main m.command.should == "version\x00\x00\x00\x00\x00" m.payload_len.should == 85 m.has_parameter?(:checksum).should be_false