class Blockchain
Public Class Methods
from_json( data )
click to toggle source
# File lib/centralbank/blockchain.rb, line 40 def self.from_json( data ) ## note: assumes data is an array of block records/objects in json chain = data.map { |h| Block.from_h( h ) } self.new( chain ) end
new( chain=[] )
click to toggle source
# File lib/centralbank/blockchain.rb, line 9 def initialize( chain=[] ) @chain = chain end
Public Instance Methods
<<( txs )
click to toggle source
# File lib/centralbank/blockchain.rb, line 13 def <<( txs ) ## todo: check if is block or array ## if array (of transactions) - auto-add (build) block ## allow block - why? why not? ## for now just use transactions (keep it simple :-) if @chain.size == 0 block = Block.first( txs ) else block = Block.next( @chain.last, txs ) end @chain << block end
as_json()
click to toggle source
# File lib/centralbank/blockchain.rb, line 29 def as_json @chain.map { |block| block.to_h } end
transactions()
click to toggle source
# File lib/centralbank/blockchain.rb, line 33 def transactions ## "accumulate" get all transactions from all blocks "reduced" into a single array @chain.reduce( [] ) { |acc, block| acc + block.transactions } end