class BlockchainLite::Basic::Block

Attributes

hash[R]
index[R]
previous_hash[R]
timestamp[R]
transactions[R]
transactions_count[R]

Public Class Methods

first( *args ) click to toggle source
# File lib/blockchain-lite/basic/block.rb, line 41
def self.first( *args )    # 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')
  Block.new( 0, transactions, '0'  )
end
new(index, transactions, previous_hash, timestamp: nil) click to toggle source
# File lib/blockchain-lite/basic/block.rb, line 16
def initialize(index, transactions, previous_hash, timestamp: nil)
  @index              = index

  ## note: use coordinated universal time (utc)
  ##    auto-add timestamp for new blocks (e.g. timestamp is nil)
  @timestamp          = timestamp ? timestamp : Time.now.utc

  ## note: assumes / expects an array for transactions
  @transactions       = transactions
  @transactions_count = transactions.size

  @previous_hash      = previous_hash
  @hash               = calc_hash
end
next( previous, *args ) click to toggle source
# File lib/blockchain-lite/basic/block.rb, line 52
def self.next( previous, *args )
  ## 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 )
end

Public Instance Methods

calc_hash() click to toggle source
# File lib/blockchain-lite/basic/block.rb, line 31
def calc_hash
  sha = Digest::SHA256.new
  sha.update( @timestamp.to_s +
              @transactions.to_s +
              @previous_hash )
  sha.hexdigest
end