Test out the Version message

This commit is contained in:
Nick Thomas
2011-05-19 21:40:26 +01:00
parent 7e31f3976d
commit dd2c02a206
2 changed files with 39 additions and 19 deletions

View File

@@ -44,11 +44,11 @@ module BtcWireProto
# Comprehensive list of known networks. The hex values are what you see in
# MessageHdr#magic and the symbols are their known friendly names.
NETWORKS = {
:main => 0xF9BEB4D9,
0xF9BEB4D9 => :main,
:testnet => 0xDAB5BFFA,
0xDAB5BFFA => :testnet,
:testnet => 0xFABFB5DA,
0xFABFB5DA => :testnet
:main => 0xD9B4BEF9,
0xD9B4BEF9 => :main
}
# Comprehensive list of known inventory vector types.
@@ -392,7 +392,7 @@ module BtcWireProto
# 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?
command != "version" && command != "verack"
!%w|version verack|.include?(command.strip)
end
end
@@ -430,26 +430,25 @@ module BtcWireProto
# Works out what the payload looks like based on the MessageHdr struct
# and (potentially) the version
def payload_choice
puts header.command
return header.command if %w{
cmd = header.command.to_s.strip
return cmd if %w{
version inv getdata getblocks getheaders tx block headers alert
}.include?(header.command)
}.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 #{header.command}"
) if %w|checkorder submitorder|.include?(header.command)
"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?(header.command) ||
header.command == ""
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
header.command == "addr"
cmd == "addr"
raise NotImplementedError.new("Unknown command: #{header.command.inspect}")
raise NotImplementedError.new("Unknown command: #{cmd}")
end
end

View File

@@ -231,11 +231,32 @@ describe ::BtcWireProto do
describe Message do
context "Version message" do
it "should have a Version payload" do
m = Message::new
puts m.inspect
puts m.header.inspect
m.header.command = "version"
v.payload.class.should == Version
m = Message::new(:header => {:command => 'version'})
m.payload.selection.should == "version"
end
it "should parse binary data correctly" do
m = Message::read(binary(%w{
F9 BE B4 D9 76 65 72 73 69 6F 6E 00 00 00 00 00 55 00 00
00 9C 7C 00 00 01 00 00 00 00 00 00 00 E6 15 10 4D 00 00
00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 FF FF 0A 00 00 01 DA F6 01 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 FF FF 0A 00 00 02 20 8D DD 9D 20
2C 3A B4 57 13 00 55 81 01 00
}))
m.header.magic.should == BtcWireProto::NETWORKS[:main]
m.header.command.should == "version\x00\x00\x00\x00\x00"
m.header.payload_len.should == 85
m.header.has_parameter?(:checksum).should be_false
m.payload.version.should == 31900
m.payload.services.node_network.should == 1
m.payload.timestamp.should == 1292899814
m.payload.addr_me.class.should == NetAddr
m.payload.addr_you.class.should == NetAddr
m.payload.nonce.should == 0x1357B43A2C209DDD
m.payload.sub_version.should == ""
m.payload.start_height.should == 98645
end
end
end