Commit dd1d7b35d904f95e3157f0f39fbb0ff97e220d1f

Authored by Marius Hanne
1 parent cd701b7782
Exists in bip33

use services bitmask correctly

Showing 2 changed files with 7 additions and 9 deletions Side-by-side Diff

lib/bitcoin/protocol/version.rb
... ... @@ -10,10 +10,8 @@
10 10 # parse packet
11 11 #
12 12 def self.parse(payload)
13   - version, s, timestamp, to, from, nonce, payload = payload.unpack("Ia8Qa26a26Qa*")
14   - services = []
15   - s.unpack("c*").each_with_index {|s, i|
16   - services << SERVICES[i] if s == 1 }
  13 + version, services, timestamp, to, from, nonce, payload = payload.unpack("IQQa26a26Qa*")
  14 + services = SERVICES.select.with_index {|s, i| (services & (1 << i)) != 0 }
17 15 user_agent, payload = Protocol.unpack_var_string(payload)
18 16 block = payload.unpack("I")[0]
19 17 to, from = parse_ip(to), parse_ip(from)
20 18  
... ... @@ -41,9 +39,9 @@
41 39 opts = {:version => Bitcoin::Protocol::VERSION, :services => [],
42 40 :block => 0, :time => Time.now.tv_sec,
43 41 :user_agent => "/bitcoin-ruby:#{Bitcoin::VERSION}/"}.merge(opts)
44   - services = "\x00"*8; opts[:services].each {|s| services[SERVICES.index(s)] = "\x01"}
  42 + services = opts[:services].map{|i| (1 << SERVICES.index(i)) }.inject(:|)
45 43 payload = [
46   - [opts[:version]].pack("I"), services, [opts[:time]].pack("Q"),
  44 + [opts[:version], services, opts[:time]].pack("IQQ"),
47 45 build_address(from), build_address(to),
48 46 [from_id].pack("Q"),
49 47 Protocol.pack_var_string(opts[:user_agent]),
spec/bitcoin/protocol/version_spec.rb
... ... @@ -11,14 +11,14 @@
11 11  
12 12 it 'parses version' do
13 13 pkt = Bitcoin::Protocol.pkt("version",
14   - ["60ea00000100000000000000b3c1424f00000000010000000000000000000000000000000000ffff7f000001e1ca010000000000000000000000000000000000ffff7f000001479d9525d0c7b30688ae122f626974636f696e2d71743a302e362e302f82b60000"].pack("H*"))
  14 + ["60ea00000300000000000000b3c1424f00000000010000000000000000000000000000000000ffff7f000001e1ca010000000000000000000000000000000000ffff7f000001479d9525d0c7b30688ae122f626974636f696e2d71743a302e362e302f82b60000"].pack("H*"))
15 15  
16 16 parser = Bitcoin::Protocol::Parser.new( handler = Version_Handler.new )
17 17 parser.parse(pkt + "AAAA").should == "AAAA"
18 18  
19 19 pkt = handler.pkt
20 20 pkt.version.should == 60000
21   - pkt.services.should == [:network]
  21 + pkt.services.should == [:network, :service]
22 22 pkt.timestamp.should == 1329775027
23 23 pkt.block.should == 46722
24 24 pkt.from.should == { :service => 1, :ip => [127, 0, 0, 1], :port => 18333 }
... ... @@ -28,7 +28,7 @@
28 28  
29 29 it 'parses version' do
30 30 pkt = Bitcoin::Protocol.pkt("version",
31   - ["60ea00000101010000000000b3c1424f00000000010000000000000000000000000000000000ffff7f000001e1ca010000000000000000000000000000000000ffff7f000001479d9525d0c7b30688ae122f626974636f696e2d71743a302e362e302f82b60000"].pack("H*"))
  31 + ["60ea00000700000000000000b3c1424f00000000010000000000000000000000000000000000ffff7f000001e1ca010000000000000000000000000000000000ffff7f000001479d9525d0c7b30688ae122f626974636f696e2d71743a302e362e302f82b60000"].pack("H*"))
32 32  
33 33 parser = Bitcoin::Protocol::Parser.new( handler = Version_Handler.new )
34 34 parser.parse(pkt + "AAAA").should == "AAAA"