Class: BlockchainLite::ProofOfWork::Block
- Inherits:
-
Object
- Object
- BlockchainLite::ProofOfWork::Block
- Defined in:
- lib/blockchain-lite/proof_of_work/block.rb
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#nonce ⇒ Object
readonly
(“lucky” number used once) - proof of work if hash starts with leading zeros (00).
-
#previous_hash ⇒ Object
readonly
Returns the value of attribute previous_hash.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#transactions ⇒ Object
readonly
use alias - txn - why? why not?.
-
#transactions_count ⇒ Object
readonly
use alias - txn_count - why? why not?.
-
#transactions_hash ⇒ Object
readonly
use alias - merkle_root - why? why not?.
Class Method Summary collapse
-
.first(*args, **opts) ⇒ Object
create genesis (big bang! first) block.
- .next(previous, *args, **opts) ⇒ Object
Instance Method Summary collapse
- #calc_hash ⇒ Object
-
#initialize(index, transactions, previous_hash, timestamp: nil, nonce: nil) ⇒ Block
constructor
A new instance of Block.
Constructor Details
#initialize(index, transactions, previous_hash, timestamp: nil, nonce: nil) ⇒ Block
Returns a new instance of Block.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/blockchain-lite/proof_of_work/block.rb', line 18 def initialize(index, transactions, previous_hash, timestamp: nil, nonce: nil) @index = index ## note: assumes / expects an array for transactions @transactions = transactions @transactions_count = transactions.size ## todo: add empty array check to merkletree.compute why? why not? @transactions_hash = transactions.empty? ? '0' : MerkleTree.compute_root_for( transactions ) @previous_hash = previous_hash ## note: use coordinated universal time (utc) @timestamp = ? : Time.now.utc if nonce ## restore pre-computed/mined block (from disk/cache/db/etc.) ## todo: check timestamp MUST NOT be nil @nonce = nonce @hash = calc_hash else ## new block (mine! e.g. find nonce - "lucky" number used once) @nonce, @hash = compute_hash_with_proof_of_work end end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
16 17 18 |
# File 'lib/blockchain-lite/proof_of_work/block.rb', line 16 def hash @hash end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
9 10 11 |
# File 'lib/blockchain-lite/proof_of_work/block.rb', line 9 def index @index end |
#nonce ⇒ Object (readonly)
(“lucky” number used once) - proof of work if hash starts with leading zeros (00)
15 16 17 |
# File 'lib/blockchain-lite/proof_of_work/block.rb', line 15 def nonce @nonce end |
#previous_hash ⇒ Object (readonly)
Returns the value of attribute previous_hash.
14 15 16 |
# File 'lib/blockchain-lite/proof_of_work/block.rb', line 14 def previous_hash @previous_hash end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
10 11 12 |
# File 'lib/blockchain-lite/proof_of_work/block.rb', line 10 def @timestamp end |
#transactions ⇒ Object (readonly)
use alias - txn - why? why not?
12 13 14 |
# File 'lib/blockchain-lite/proof_of_work/block.rb', line 12 def transactions @transactions end |
#transactions_count ⇒ Object (readonly)
use alias - txn_count - why? why not?
11 12 13 |
# File 'lib/blockchain-lite/proof_of_work/block.rb', line 11 def transactions_count @transactions_count end |
#transactions_hash ⇒ Object (readonly)
use alias - merkle_root - why? why not?
13 14 15 |
# File 'lib/blockchain-lite/proof_of_work/block.rb', line 13 def transactions_hash @transactions_hash end |
Class Method Details
.first(*args, **opts) ⇒ Object
create genesis (big bang! first) block
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/blockchain-lite/proof_of_work/block.rb', line 48 def self.first( *args, **opts ) # create genesis (big bang! first) block ## note: allow/support splat-* for now for convenience (auto-wraps args into array) if args.size == 1 && args[0].is_a?( Array ) transactions = args[0] ## "unwrap" array in array else transactions = args ## use "auto-wrapped" splat array end ## uses index zero (0) and arbitrary previous_hash ('0') ## note: pass along (optional) custom timestamp (e.g. used for 1637 etc.) Block.new( 0, transactions, '0', timestamp: opts[:timestamp] ) end |
.next(previous, *args, **opts) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/blockchain-lite/proof_of_work/block.rb', line 60 def self.next( previous, *args, **opts ) ## note: allow/support splat-* for now for convenience (auto-wraps args into array) if args.size == 1 && args[0].is_a?( Array ) transactions = args[0] ## "unwrap" array in array else transactions = args ## use "auto-wrapped" splat array end Block.new( previous.index+1, transactions, previous.hash, timestamp: opts[:timestamp] ) end |
Instance Method Details
#calc_hash ⇒ Object
42 43 44 |
# File 'lib/blockchain-lite/proof_of_work/block.rb', line 42 def calc_hash calc_hash_with_nonce( @nonce ) end |