Class: Bitcoin::Protocol::Version

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoin/protocol/version.rb

Overview

Constant Summary collapse

NODE_NETWORK =

services bit constants

(1 << 0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Version

Returns a new instance of Version.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bitcoin/protocol/version.rb', line 13

def initialize(opts={})
  @fields = {
    :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}/",
    :last_block => 0 # 188617
  }.merge( opts.reject{|k,v| v == nil } )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*a) ⇒ Object



73
# File 'lib/bitcoin/protocol/version.rb', line 73

def method_missing(*a); (@fields[a.first] rescue nil) or super(*a); end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



11
12
13
# File 'lib/bitcoin/protocol/version.rb', line 11

def fields
  @fields
end

Class Method Details

.parse(payload) ⇒ Object



75
# File 'lib/bitcoin/protocol/version.rb', line 75

def self.parse(payload); new.parse(payload); end

Instance Method Details

#pack_address_field(addr_str) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/bitcoin/protocol/version.rb', line 60

def pack_address_field(addr_str)
  host, port = addr_str.split(":")
  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

#parse(payload) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bitcoin/protocol/version.rb', line 41

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]

  @fields = {
   :version => version, :services => services, :time => timestamp,
   :from => from, :to => to, :nonce => nonce,
   :user_agent => user_agent.to_s, :last_block => last_block
  }
  self
end

#to_payloadObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/bitcoin/protocol/version.rb', line 26

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")
  ].join
end

#to_pktObject



37
38
39
# File 'lib/bitcoin/protocol/version.rb', line 37

def to_pkt
  Bitcoin::Protocol.pkt("version", to_payload)
end

#unpack_address_field(payload) ⇒ Object



55
56
57
58
# File 'lib/bitcoin/protocol/version.rb', line 55

def unpack_address_field(payload)
  ip, port = payload.unpack("x8x12a4n")
  "#{ip.unpack("C*").join(".")}:#{port}"
end

#uptimeObject



69
70
71
# File 'lib/bitcoin/protocol/version.rb', line 69

def uptime
  @fields[:time] - Time.now.tv_sec
end