Class: Dcha::Block
- Inherits:
-
Object
- Object
- Dcha::Block
- Defined in:
- lib/dcha/block.rb
Overview
:nodoc:
Constant Summary collapse
- GENESIS =
rubocop:disable Metrics/LineLength
Block.new(index: 0, parent_hash: 0, time: 0, root_hash: "\0", proof: 'lvew')
Instance Attribute Summary collapse
-
#hash ⇒ Object
Returns the value of attribute hash.
-
#index ⇒ Object
Returns the value of attribute index.
-
#parent_hash ⇒ Object
Returns the value of attribute parent_hash.
-
#proof ⇒ Object
Returns the value of attribute proof.
-
#root_hash ⇒ Object
Returns the value of attribute root_hash.
-
#time ⇒ Object
Returns the value of attribute time.
Instance Method Summary collapse
- #body ⇒ Object
-
#initialize(options = {}) ⇒ Block
constructor
A new instance of Block.
- #make_proof ⇒ Object
- #valid_after?(previous_block) ⇒ Boolean
- #valid_proof? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Block
Returns a new instance of Block.
6 7 8 9 10 |
# File 'lib/dcha/block.rb', line 6 def initialize( = {}) @time = Time.now.to_i @proof = '' .each { |k, v| send("#{k}=", v) } end |
Instance Attribute Details
#hash ⇒ Object
Returns the value of attribute hash.
4 5 6 |
# File 'lib/dcha/block.rb', line 4 def hash @hash end |
#index ⇒ Object
Returns the value of attribute index.
4 5 6 |
# File 'lib/dcha/block.rb', line 4 def index @index end |
#parent_hash ⇒ Object
Returns the value of attribute parent_hash.
4 5 6 |
# File 'lib/dcha/block.rb', line 4 def parent_hash @parent_hash end |
#proof ⇒ Object
Returns the value of attribute proof.
4 5 6 |
# File 'lib/dcha/block.rb', line 4 def proof @proof end |
#root_hash ⇒ Object
Returns the value of attribute root_hash.
4 5 6 |
# File 'lib/dcha/block.rb', line 4 def root_hash @root_hash end |
#time ⇒ Object
Returns the value of attribute time.
4 5 6 |
# File 'lib/dcha/block.rb', line 4 def time @time end |
Instance Method Details
#body ⇒ Object
18 19 20 |
# File 'lib/dcha/block.rb', line 18 def body [index, time, root_hash, parent_hash] end |
#make_proof ⇒ Object
26 27 28 29 30 31 |
# File 'lib/dcha/block.rb', line 26 def make_proof @hash = Digest::SHA256.hexdigest(body.join) letters = ('a'..'z').to_a @proof << letters.sample until valid_proof? self end |
#valid_after?(previous_block) ⇒ Boolean
12 13 14 15 16 |
# File 'lib/dcha/block.rb', line 12 def valid_after?(previous_block) (previous_block.hash == parent_hash) && (hash == Digest::SHA256.hexdigest(body.join)) && valid_proof? end |
#valid_proof? ⇒ Boolean
22 23 24 |
# File 'lib/dcha/block.rb', line 22 def valid_proof? Digest::SHA256.hexdigest((body + [proof]).join).start_with?('abc') end |