Class: BlockchainLite::Basic::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/blockchain-lite/basic/block.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, transactions, previous_hash, timestamp: nil) ⇒ Block

Returns a new instance of Block.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



14
15
16
# File 'lib/blockchain-lite/basic/block.rb', line 14

def hash
  @hash
end

#indexObject (readonly)

Returns the value of attribute index.



9
10
11
# File 'lib/blockchain-lite/basic/block.rb', line 9

def index
  @index
end

#previous_hashObject (readonly)

Returns the value of attribute previous_hash.



13
14
15
# File 'lib/blockchain-lite/basic/block.rb', line 13

def previous_hash
  @previous_hash
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



10
11
12
# File 'lib/blockchain-lite/basic/block.rb', line 10

def timestamp
  @timestamp
end

#transactionsObject (readonly)

use alias - txn - why? why not?



12
13
14
# File 'lib/blockchain-lite/basic/block.rb', line 12

def transactions
  @transactions
end

#transactions_countObject (readonly)

use alias - txn_count - why? why not?



11
12
13
# File 'lib/blockchain-lite/basic/block.rb', line 11

def transactions_count
  @transactions_count
end

Class Method Details

.first(*args) ⇒ Object

create genesis (big bang! first) block



41
42
43
44
45
46
47
48
49
50
# 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

.next(previous, *args) ⇒ Object



52
53
54
55
56
57
58
59
60
# 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

Instance Method Details

#calc_hashObject



31
32
33
34
35
36
37
# 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