Class: Tapyrus::Message::Version

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

Overview

Constant Summary collapse

COMMAND =
"version"

Constants included from Util

Util::DIGEST_NAME_SHA256

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#to_pkt

Methods included from Util

#byte_to_bit, #calc_checksum, #decode_base58_address, #double_sha256, #encode_base58_address, #hash160, #hmac_sha256, #pack_boolean, #pack_var_int, #pack_var_string, #padding_zero, #sha256, #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.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tapyrus/message/version.rb', line 19

def initialize(opts = {})
  @version = Tapyrus.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 = Tapyrus::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.



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

def local_addr
  @local_addr
end

#nonceObject

Returns the value of attribute nonce.



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

def nonce
  @nonce
end

#relayObject

Returns the value of attribute relay.



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

def relay
  @relay
end

#remote_addrObject

Returns the value of attribute remote_addr.



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

def remote_addr
  @remote_addr
end

#servicesObject

Returns the value of attribute services.



10
11
12
# File 'lib/tapyrus/message/version.rb', line 10

def services
  @services
end

#start_heightObject

Returns the value of attribute start_height.



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

def start_height
  @start_height
end

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

#user_agentObject

Returns the value of attribute user_agent.



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

def user_agent
  @user_agent
end

#versionObject

Returns the value of attribute version.



9
10
11
# File 'lib/tapyrus/message/version.rb', line 9

def version
  @version
end

Class Method Details

.parse_from_payload(payload) ⇒ Object



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

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

#to_payloadObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tapyrus/message/version.rb', line 49

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



61
62
63
# File 'lib/tapyrus/message/version.rb', line 61

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