Class: StephanyCryptodemo::Blockchain
- Inherits:
-
Object
- Object
- StephanyCryptodemo::Blockchain
- Defined in:
- lib/stephany_cryptodemo.rb
Instance Attribute Summary collapse
-
#chain ⇒ Object
readonly
Returns the value of attribute chain.
Class Method Summary collapse
Instance Method Summary collapse
- #hash(block) ⇒ Object
-
#initialize ⇒ Blockchain
constructor
A new instance of Blockchain.
- #last_block ⇒ Object
- #new_block(proof_of_work, previous_hash = nil) ⇒ Object
- #new_transaction(sender, recipient, amount) ⇒ Object
- #proof_of_work(last_proof) ⇒ Object
Constructor Details
#initialize ⇒ Blockchain
Returns a new instance of Blockchain.
11 12 13 14 15 16 17 |
# File 'lib/stephany_cryptodemo.rb', line 11 def initialize @chain = [] @current_transactions = [] # Generating First Block of Blockchain (the first block is called 'Genesis Block') new_block(100, 1) end |
Instance Attribute Details
#chain ⇒ Object (readonly)
Returns the value of attribute chain.
9 10 11 |
# File 'lib/stephany_cryptodemo.rb', line 9 def chain @chain end |
Class Method Details
.hash(block) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/stephany_cryptodemo.rb', line 92 def self.hash(block) block_string = block.to_json # return SHA256 for the given block Digest::SHA256.hexdigest(block_string) end |
.valid_proof(last_proof, new_proof) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/stephany_cryptodemo.rb', line 101 def self.valid_proof(last_proof, new_proof) # return true if the resulting hash has 4 zeros at the end guess = "#{last_proof}#{new_proof}" guess_hash = Digest::SHA256.hexdigest(guess) guess_hash.to_s[-4..-1] == '0000' end |
Instance Method Details
#hash(block) ⇒ Object
86 87 88 89 |
# File 'lib/stephany_cryptodemo.rb', line 86 def hash(block) # If we need to create a hash for the block Blockchain.hash(block) end |
#last_block ⇒ Object
81 82 83 |
# File 'lib/stephany_cryptodemo.rb', line 81 def last_block @chain[-1] end |
#new_block(proof_of_work, previous_hash = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/stephany_cryptodemo.rb', line 40 def new_block(proof_of_work, previous_hash = nil) # To generate a new block, we need: # - proof of work # - the previous hash to control the blocks chain creation previous_hash ||= self.hash(@chain[-1]) block = { :index => @chain.count + 1, :timestamp => Time.now.to_i, :transactions => @current_transactions, :proof => proof_of_work, :previous_hash => previous_hash, } # The chain must restart the transactions being stored # to store the transactions coming for the new block @current_transactions = [] @chain.push(block) block end |
#new_transaction(sender, recipient, amount) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/stephany_cryptodemo.rb', line 64 def new_transaction(sender, recipient, amount) # Just like a Bank Transaction # You need a sender, a recipient and the amount to transfer transaction = { :sender => sender, :recipient => recipient, :amount => amount } @current_transactions.push(transaction) @chain.index(last_block) end |
#proof_of_work(last_proof) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/stephany_cryptodemo.rb', line 19 def proof_of_work(last_proof) # Piece of data hard to generate but easy to validate # Generate it, could take some time (X hours to generate a new block) # It allow us to validate if the person using this gem is actually spending time to mining a new block # It will calculate a number that concatenated to the last_proof # will return a hash with 4 zeros at the end # Ex: # efc26373838..0000 proof = 0 while !Blockchain.valid_proof(last_proof, proof) do proof += 1 end proof end |