lib/bitcoin/protocol/version.rb in bitcoin-ruby-0.0.4 vs lib/bitcoin/protocol/version.rb in bitcoin-ruby-0.0.5

- old
+ new

@@ -17,22 +17,24 @@ :time => Time.now.tv_sec, :from => "127.0.0.1:8333", :to => "127.0.0.1:8333", :nonce => Bitcoin::Protocol::Uniq, :user_agent => "/bitcoin-ruby:#{Bitcoin::VERSION}/", - :last_block => 0 # 188617 + :last_block => 0, # 188617 + :relay => true # BIP0037 }.merge( opts.reject{|k,v| v == nil } ) end def to_payload payload = [ @fields.values_at(:version, :services, :time).pack("VQQ"), pack_address_field(@fields[:from]), pack_address_field(@fields[:to]), @fields.values_at(:nonce).pack("Q"), Protocol.pack_var_string(@fields[:user_agent]), - @fields.values_at(:last_block).pack("V") + @fields.values_at(:last_block).pack("V"), + Protocol.pack_boolean(@fields[:relay]) # Satoshi 0.8.6 doesn't send this but it does respect it ].join end def to_pkt Bitcoin::Protocol.pkt("version", to_payload) @@ -40,16 +42,17 @@ def parse(payload) version, services, timestamp, to, from, nonce, payload = payload.unpack("VQQa26a26Qa*") to, from = unpack_address_field(to), unpack_address_field(from) user_agent, payload = Protocol.unpack_var_string(payload) - last_block = payload.unpack("V")[0] + last_block, payload = payload.unpack("Va*") + relay, payload = unpack_relay_field(version, payload) @fields = { :version => version, :services => services, :time => timestamp, :from => from, :to => to, :nonce => nonce, - :user_agent => user_agent.to_s, :last_block => last_block + :user_agent => user_agent.to_s, :last_block => last_block, :relay => relay } self end def unpack_address_field(payload) @@ -62,9 +65,14 @@ port = port ? port.to_i : 8333 sockaddr = Socket.pack_sockaddr_in(port, host) #raise "invalid IPv4 Address: #{addr}" unless sockaddr[0...2] == "\x02\x00" port, host = sockaddr[2...4], sockaddr[4...8] [[1].pack("Q"), "\x00"*10, "\xFF\xFF", host, port].join + end + + # BIP0037: this field starts with version 70001 and is allowed to be missing, defaults to true + def unpack_relay_field(version, payload) + ( version >= 70001 and payload ) ? Protocol.unpack_boolean(payload) : [ true, nil ] end def uptime @fields[:time] - Time.now.tv_sec end