Class: Nano::Block
- Inherits:
-
Object
- Object
- Nano::Block
- 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
-
#account ⇒ String
readonly
The account associated with the block.
-
#balance ⇒ String
readonly
The balance for the account at this block.
-
#link ⇒ String
readonly
The link for this block.
-
#previous ⇒ String
readonly
The previous block to this block.
-
#representative ⇒ String
readonly
The account representative as set by the block.
-
#signature ⇒ String?
readonly
The signature for the block.
-
#type ⇒ String
readonly
The type of the block, locked to 'state' currently.
-
#work ⇒ String?
readonly
The proof of work computed for this block.
Instance Method Summary collapse
-
#compute_work! ⇒ String
Computes the proof of work for the block, setting the internal work value and overwriting the existing value.
-
#hash ⇒ String
The computed hash for the block.
-
#initialize(params) ⇒ Block
constructor
The block initializer, requires certain parameters to be valid.
-
#link_as_account ⇒ Object
The link for the account converted to an address.
-
#sign!(secret_key) ⇒ String
This method signs the block using the secret key given.
- #to_json(options = nil) ⇒ Object
Constructor Details
#initialize(params) ⇒ Block
The block initializer, requires certain parameters to be valid.
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
#account ⇒ String (readonly)
Returns The account associated with the block.
18 19 20 |
# File 'lib/nano/block.rb', line 18 def account @account end |
#balance ⇒ String (readonly)
Returns The balance for the account at this block.
30 31 32 |
# File 'lib/nano/block.rb', line 30 def balance @balance end |
#link ⇒ String (readonly)
Returns The link for this block.
27 28 29 |
# File 'lib/nano/block.rb', line 27 def link @link end |
#previous ⇒ String (readonly)
Returns The previous block to this block.
24 25 26 |
# File 'lib/nano/block.rb', line 24 def previous @previous end |
#representative ⇒ String (readonly)
Returns The account representative as set by the block.
21 22 23 |
# File 'lib/nano/block.rb', line 21 def representative @representative end |
#signature ⇒ String? (readonly)
Returns The signature for the block.
36 37 38 |
# File 'lib/nano/block.rb', line 36 def signature @signature end |
#type ⇒ String (readonly)
Returns The type of the block, locked to 'state' currently.
15 16 17 |
# File 'lib/nano/block.rb', line 15 def type @type end |
#work ⇒ String? (readonly)
Returns 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.
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 |
#hash ⇒ String
Returns The computed hash for the block.
139 140 141 |
# File 'lib/nano/block.rb', line 139 def hash Nano::Hash.hash_block(self) end |
#link_as_account ⇒ Object
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 link_as_account 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( = nil) { type: type, account: account, previous: previous, representative: representative, balance: balance, link: link, link_as_account: link_as_account, signature: signature, work: work }.to_json() end |