Add a convenience method to determine the network a message is from

This commit is contained in:
Nick Thomas
2011-06-21 22:53:04 +01:00
parent 4359183e56
commit 5d4f44e776
2 changed files with 9 additions and 1 deletions

View File

@@ -446,10 +446,17 @@ module BtcWireProto
null_payload "null" null_payload "null"
end end
# @return[Symbol] representation of the command this message contains
def cmd_sym def cmd_sym
c = command.strip c = command.strip
c == "" ? nil : c.to_sym c == "" ? nil : c.to_sym
end 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. # version and verack messages don't have a checksum. The rest do.
# @return[Boolean] does this message header have a checksum field or not? # @return[Boolean] does this message header have a checksum field or not?
def has_checksum? def has_checksum?

View File

@@ -300,6 +300,7 @@ describe ::BtcWireProto do
m = Message::read(GOOD_VERSION_DATA) m = Message::read(GOOD_VERSION_DATA)
m.magic.should == BtcWireProto::NETWORKS[:main] m.magic.should == BtcWireProto::NETWORKS[:main]
m.network_name.should == :main
m.command.should == "version\x00\x00\x00\x00\x00" m.command.should == "version\x00\x00\x00\x00\x00"
m.payload_len.should == 85 m.payload_len.should == 85
m.has_parameter?(:checksum).should be_false m.has_parameter?(:checksum).should be_false