datastructures/blockchain.rb
# Add a block to the chain. The hash is optional, this method will calculate it when it is nil
Add a block to the chain. The hash is optional, this method will calculate it when it is nil
Add a block to the chain . The hash is optional this method will calculate it when it is nil
def new_block(previous_hash=nil)
    start = Time.now.utc
    merkle_tree = MerkleTree.new(@current_transactions)
    merkle_root = merkle_tree.root.hash
    block = {
      :index => @chain.length,
      :timestamp => Time.now.utc.to_i,
      :transactions => @current_transactions,
      :previous_hash => (previous_hash or self.class.hash(last_block)),
      :merkle_root => merkle_root
    }
    proof = proof_of_work(block)
    block[:proof] = proof
    @current_transactions = []
    @chain.push(block)
    save
    puts 'Block created in: ' + (Time.now - start).to_s + ' seconds'
    return block
  end
def new_block ( previous_hash = nil ) start = Time . now . utc merkle_tree = MerkleTree . new ( @current_transactions ) merkle_root = merkle_tree . root . hash block = { :index => @chain . length , :timestamp => Time . now . utc . to_i , :transactions => @current_transactions , :previous_hash => ( previous_hash or self . class . hash ( last_block ) ) , :merkle_root => merkle_root } proof = proof_of_work ( block ) block [ :proof ] = proof @current_transactions = [ ] @chain . push ( block ) save puts 'Block created in: ' + ( Time . now - start ) . to_s + ' seconds' return block end
Add a block to the chain.
Add a block to the chain .
# Reset the current list of transactions # Add the block to the chain and save it
previous_hash