class Cryptoruby::Blockchain::Block
Attributes
blockchain[R]
data[R]
difficult[R]
hash[R]
index[R]
nonce[R]
previous_hash[R]
timestamp[R]
Public Class Methods
new(args)
click to toggle source
# File lib/cryptoruby/block.rb, line 8 def initialize(args) setup_args(args) @timestamp = Time.now.to_i @difficult = difficult @nonce = 0 mine end
Public Instance Methods
digest_hash()
click to toggle source
# File lib/cryptoruby/block.rb, line 16 def digest_hash OpenSSL::Digest::SHA512.hexdigest "#{index}#{previous_hash}#{stringfied_data}#{timestamp.to_s}#{nonce}" end
Private Instance Methods
mine()
click to toggle source
# File lib/cryptoruby/block.rb, line 22 def mine loop do @nonce += 1 @hash = digest_hash break if @hash[0..@difficult] =~ /^0*$/ || @difficult.zero? end end
setup_args(args)
click to toggle source
# File lib/cryptoruby/block.rb, line 43 def setup_args(args) @index = args[:index] || 0 @previous_hash = args[:previous_hash] || nil @data = args[:data] || 'Genesis Block' @difficult = args[:difficult] || 0 @blockchain = args[:blockchain] end
stringfied_data()
click to toggle source
# File lib/cryptoruby/block.rb, line 30 def stringfied_data case @data when String @data when Hash @data.to_json when Array @data.to_json else raise 'Invalid Data' end end