Class: Bitcoin::Message::Version

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

Overview

Constant Summary collapse

COMMAND =
'version'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

from_pkt, #to_pkt

Methods included from Util

#byte_to_bit, #calc_checksum, #decode_base58_address, #double_sha256, #encode_base58_address, #hash160, #hkdf_sha256, #hmac_sha256, #pack_boolean, #pack_var_int, #pack_var_string, #padding_zero, #sha256, #tagged_hash, #unpack_boolean, #unpack_var_int, #unpack_var_int_from_io, #unpack_var_string, #valid_address?

Methods included from HexConverter

#to_hex

Constructor Details

#initialize(opts = {}) ⇒ Version

Returns a new instance of Version.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bitcoin/message/version.rb', line 21

def initialize(opts = {})
  @version = Bitcoin.chain_params.protocol_version
  @services = DEFAULT_SERVICE_FLAGS
  @timestamp = Time.now.to_i
  @local_addr = NetworkAddr.local_addr
  @remote_addr = NetworkAddr.local_addr
  @nonce = SecureRandom.random_number(0xffffffffffffffff)
  @user_agent = Bitcoin::Message::USER_AGENT
  @start_height = 0
  opts.each { |k, v| send "#{k}=", v }
  @relay = opts[:relay] || false
end

Instance Attribute Details

#local_addrObject

Returns the value of attribute local_addr.



14
15
16
# File 'lib/bitcoin/message/version.rb', line 14

def local_addr
  @local_addr
end

#nonceObject

Returns the value of attribute nonce.



16
17
18
# File 'lib/bitcoin/message/version.rb', line 16

def nonce
  @nonce
end

#relayObject

Returns the value of attribute relay.



19
20
21
# File 'lib/bitcoin/message/version.rb', line 19

def relay
  @relay
end

#remote_addrObject

Returns the value of attribute remote_addr.



15
16
17
# File 'lib/bitcoin/message/version.rb', line 15

def remote_addr
  @remote_addr
end

#servicesObject

Returns the value of attribute services.



12
13
14
# File 'lib/bitcoin/message/version.rb', line 12

def services
  @services
end

#start_heightObject

Returns the value of attribute start_height.



18
19
20
# File 'lib/bitcoin/message/version.rb', line 18

def start_height
  @start_height
end

#timestampObject

Returns the value of attribute timestamp.



13
14
15
# File 'lib/bitcoin/message/version.rb', line 13

def timestamp
  @timestamp
end

#user_agentObject

Returns the value of attribute user_agent.



17
18
19
# File 'lib/bitcoin/message/version.rb', line 17

def user_agent
  @user_agent
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.parse_from_payload(payload) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bitcoin/message/version.rb', line 34

def self.parse_from_payload(payload)
  version, services, timestamp, local_addr, remote_addr, nonce, rest = payload.unpack('VQQa26a26Qa*')
  v = new
  v.version = version
  v.services = services
  v.timestamp = timestamp
  v.local_addr = NetworkAddr.parse_from_payload(local_addr)
  v.remote_addr = NetworkAddr.parse_from_payload(remote_addr)
  v.nonce = nonce
  user_agent, rest = unpack_var_string(rest)
  start_height, rest = rest.unpack('Va*')
  v.user_agent = user_agent
  v.start_height = start_height
  v.relay = v.unpack_relay_field(rest).first
  v
end

Instance Method Details

#support?(service_flag) ⇒ Boolean

Check whether service_flag support this version.

Parameters:

  • service_flag (Integer)

    the service flags.

Returns:

  • (Boolean)

    whether support service_flag



70
71
72
# File 'lib/bitcoin/message/version.rb', line 70

def support?(service_flag)
  (services & service_flag) != 0
end

#to_payloadObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bitcoin/message/version.rb', line 51

def to_payload
  [
    [version, services, timestamp].pack('VQQ'),
    local_addr.to_payload(true),
    remote_addr.to_payload(true),
    [nonce].pack('Q'),
    pack_var_string(user_agent),
    [start_height].pack('V'),
    pack_boolean(relay)
  ].join
end

#unpack_relay_field(payload) ⇒ Object



63
64
65
# File 'lib/bitcoin/message/version.rb', line 63

def unpack_relay_field(payload)
  ( version >= 70001 && payload ) ? unpack_boolean(payload) : [ true, nil ]
end