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.

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)



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

def 
  @account
end

#balanceString (readonly)



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

def balance
  @balance
end


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

def link
  @link
end

#previousString (readonly)



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

def previous
  @previous
end

#representativeString (readonly)



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

def representative
  @representative
end

#signatureString? (readonly)



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

def signature
  @signature
end

#typeString (readonly)



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

def type
  @type
end

#workString? (readonly)



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.



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



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

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


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.



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