Class: Nano::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/nano/block.rb

Overview

A class representing a state block in the Nano network. Can be initialized as an existing block, with a work and signature or as an incomplete block.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Block

The block initializer, requires certain parameters to be valid.

Parameters:

  • params (Hash)

    The block parameters to construct the block with.\n :previous - The previous block hash as a string\n :account - The associated account address as a string\n :representative - The account representative as a string\n :balance - The account balance after this block in raw unit\n :link - The link hash associated with this block.\n :work - The proof of work for the block (optional)\n :signature - The signature for this block (optional)

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nano/block.rb', line 48

def initialize(params)
  @type = "state"

  @previous = params[:previous]

  raise ArgumentError, "Missing data for previous" if @previous.nil?
  raise(
    ArgumentError, "Invalid previous hash #{@previous}"
  ) unless Nano::Check.is_valid_hash? @previous

  @account = params[:account]
  raise ArgumentError, "Missing data for account" if @account.nil?
  raise(
    ArgumentError, "Invalid account #{@account}"
  ) unless Nano::Check.is_valid_account? @account

  @representative = params[:representative]
  raise(
    ArgumentError, "Missing data for representative"
  ) if @representative.nil?
  raise(
    ArgumentError, "Invalid representative #{@representative}"
  ) unless Nano::Check.is_valid_account? @representative

  @balance = params[:balance]
  raise(
    ArgumentError, "Missing data for balance"
  ) if @balance.nil?
  raise(
    ArgumentError, "Invalid balance #{@balance}"
  ) unless Nano::Check.is_balance_valid? @balance

  @link = params[:link]
  raise(
    ArgumentError, "Missing data for link"
  ) if @link.nil?
  raise(
    ArgumentError, "Invalid link #{@link}"
  ) unless Nano::Check.is_hash_valid? @link

  @work = params[:work]
  @signature = params[:signature]
end

Instance Attribute Details

#accountString (readonly)

Returns The account associated with the block.

Returns:

  • (String)

    The account associated with the block.



18
19
20
# File 'lib/nano/block.rb', line 18

def 
  @account
end

#balanceString (readonly)

Returns The balance for the account at this block.

Returns:

  • (String)

    The balance for the account at this block



30
31
32
# File 'lib/nano/block.rb', line 30

def balance
  @balance
end

Returns The link for this block.

Returns:

  • (String)

    The link for this block



27
28
29
# File 'lib/nano/block.rb', line 27

def link
  @link
end

#previousString (readonly)

Returns The previous block to this block.

Returns:

  • (String)

    The previous block to this block



24
25
26
# File 'lib/nano/block.rb', line 24

def previous
  @previous
end

#representativeString (readonly)

Returns The account representative as set by the block.

Returns:

  • (String)

    The account representative as set by the block



21
22
23
# File 'lib/nano/block.rb', line 21

def representative
  @representative
end

#signatureString? (readonly)

Returns The signature for the block.

Returns:

  • (String?)

    The signature for the block



36
37
38
# File 'lib/nano/block.rb', line 36

def signature
  @signature
end

#typeString (readonly)

Returns The type of the block, locked to 'state' currently.

Returns:

  • (String)

    The type of the block, locked to 'state' currently



15
16
17
# File 'lib/nano/block.rb', line 15

def type
  @type
end

#workString? (readonly)

Returns The proof of work computed for this block.

Returns:

  • (String?)

    The proof of work computed for this block



33
34
35
# File 'lib/nano/block.rb', line 33

def work
  @work
end

Instance Method Details

#compute_work!String

Computes the proof of work for the block, setting the internal work value and overwriting the existing value. The method uses a native extension to improve the performance.

Returns:

  • (String)

    The computed work value, also setting it internally.



129
130
131
132
133
134
135
# File 'lib/nano/block.rb', line 129

def compute_work!
  base_prev = "".rjust(64, "0")
  is_first = previous == base_prev
  hash = is_first ? Nano::Key.derive_public_key(@account) : previous
  @work = Nano::Work.compute_work(hash)
  @work
end

#hashString

Returns The computed hash for the block.

Returns:

  • (String)

    The computed hash for the block.



139
140
141
# File 'lib/nano/block.rb', line 139

def hash
  Nano::Hash.hash_block(self)
end

Returns The link for the account converted to an address. May not be valid in the case the link is a block hash.

Returns:

  • The link for the account converted to an address. May not be valid in the case the link is a block hash



94
95
96
# File 'lib/nano/block.rb', line 94

def 
  Nano::Key.derive_address(@link)
end

#sign!(secret_key) ⇒ String

This method signs the block using the secret key given. This method will fail if the key is incorrect. If this method succeeds, the block may still be invalid if the key does not belong to the block account. This method modifies the block's existing signature.

Parameters:

  • secret_key (String)

    The hexidecimal representation of the secret key. Should match the account but does not perform a check, currently.

Returns:

  • (String)

    Returns the signature for the block, whilst also setting the signature internally.



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/nano/block.rb', line 110

def sign!(secret_key)
  throw ArgumentError, "Invalid key" unless Nano::Check.is_key?(secret_key)

  hash_bin = Nano::Utils.hex_to_bin(hash)
  secret_bin = Nano::Utils.hex_to_bin(secret_key)

  @signature = Nano::Utils.bin_to_hex(
    NanocurrencyExt.sign(hash_bin, secret_bin)
  ).upcase

  @signature
end

#to_json(options = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/nano/block.rb', line 143

def to_json(options = nil)
  {
    type: type,
    account: ,
    previous: previous,
    representative: representative,
    balance: balance,
    link: link,
    link_as_account: ,
    signature: signature,
    work: work
  }.to_json(options)
end