Test out the Version message
This commit is contained in:
@@ -44,11 +44,11 @@ module BtcWireProto
|
|||||||
# Comprehensive list of known networks. The hex values are what you see in
|
# Comprehensive list of known networks. The hex values are what you see in
|
||||||
# MessageHdr#magic and the symbols are their known friendly names.
|
# MessageHdr#magic and the symbols are their known friendly names.
|
||||||
NETWORKS = {
|
NETWORKS = {
|
||||||
:main => 0xF9BEB4D9,
|
:testnet => 0xDAB5BFFA,
|
||||||
0xF9BEB4D9 => :main,
|
0xDAB5BFFA => :testnet,
|
||||||
|
|
||||||
:testnet => 0xFABFB5DA,
|
:main => 0xD9B4BEF9,
|
||||||
0xFABFB5DA => :testnet
|
0xD9B4BEF9 => :main
|
||||||
}
|
}
|
||||||
|
|
||||||
# Comprehensive list of known inventory vector types.
|
# 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.
|
# 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?
|
||||||
command != "version" && command != "verack"
|
!%w|version verack|.include?(command.strip)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -430,26 +430,25 @@ module BtcWireProto
|
|||||||
# Works out what the payload looks like based on the MessageHdr struct
|
# Works out what the payload looks like based on the MessageHdr struct
|
||||||
# and (potentially) the version
|
# and (potentially) the version
|
||||||
def payload_choice
|
def payload_choice
|
||||||
puts header.command
|
cmd = header.command.to_s.strip
|
||||||
return header.command if %w{
|
return cmd if %w{
|
||||||
version inv getdata getblocks getheaders tx block headers alert
|
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
|
# 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
|
# next message starts. So all we can do is throw an error
|
||||||
raise NotImplementedError.new(
|
raise NotImplementedError.new(
|
||||||
"Received unsupported command #{header.command}"
|
"Received unsupported command #{cmd}"
|
||||||
) if %w|checkorder submitorder|.include?(header.command)
|
) if %w|checkorder submitorder|.include?(cmd)
|
||||||
|
|
||||||
# These commands don't have any payloads
|
# These commands don't have any payloads
|
||||||
return "null" if %w|verack getaddr ping|.include?(header.command) ||
|
return "null" if %w|verack getaddr ping|.include?(cmd) || cmd == ""
|
||||||
header.command == ""
|
|
||||||
|
|
||||||
# Payload has two forms, depending on protocol version. Ugh.
|
# Payload has two forms, depending on protocol version. Ugh.
|
||||||
return (@version < 31402 ? "addr_pre31402" : "addr_from31402") if
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@@ -231,11 +231,32 @@ describe ::BtcWireProto do
|
|||||||
describe Message do
|
describe Message do
|
||||||
context "Version message" do
|
context "Version message" do
|
||||||
it "should have a Version payload" do
|
it "should have a Version payload" do
|
||||||
m = Message::new
|
m = Message::new(:header => {:command => 'version'})
|
||||||
puts m.inspect
|
m.payload.selection.should == "version"
|
||||||
puts m.header.inspect
|
end
|
||||||
m.header.command = "version"
|
|
||||||
v.payload.class.should == Version
|
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
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user