lib/bitcoin/protocol/version.rb in bitcoin-ruby-0.0.1 vs lib/bitcoin/protocol/version.rb in bitcoin-ruby-0.0.2

- old
+ new

@@ -1,14 +1,21 @@ +# encoding: ascii-8bit + module Bitcoin module Protocol + # https://en.bitcoin.it/wiki/Protocol_specification#version class Version + # services bit constants + NODE_NETWORK = (1 << 0) + attr_reader :fields + def initialize(opts={}) @fields = { - :version => Bitcoin::Protocol::VERSION, - :services => 1, + :version => Bitcoin.network[:protocol_version], + :services => NODE_NETWORK, :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}/", @@ -16,33 +23,33 @@ }.merge( opts.reject{|k,v| v == nil } ) end def to_payload payload = [ - @fields.values_at(:version, :services, :time).pack("IQQ"), + @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("I") + @fields.values_at(:last_block).pack("V") ].join end def to_pkt Bitcoin::Protocol.pkt("version", to_payload) end def parse(payload) - version, services, timestamp, to, from, nonce, payload = payload.unpack("Ia8Qa26a26Qa*") + 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("I")[0] + last_block = payload.unpack("V")[0] @fields = { :version => version, :services => services, :time => timestamp, :from => from, :to => to, :nonce => nonce, - :user_agent => user_agent, :last_block => last_block + :user_agent => user_agent.to_s, :last_block => last_block } self end def unpack_address_field(payload) @@ -60,9 +67,11 @@ end def uptime @fields[:time] - Time.now.tv_sec end + + def method_missing(*a); (@fields[a.first] rescue nil) or super(*a); end def self.parse(payload); new.parse(payload); end end end