Sha256: d482a2517158dbcd424bc7c5891e0f24d59edb453c56f2d472f6c77641cede62

Contents?: true

Size: 899 Bytes

Versions: 5

Compression:

Stored size: 899 Bytes

Contents

module Dcha
  # :nodoc:
  class Block
    attr_accessor :index, :hash, :root_hash, :parent_hash, :time, :proof

    def initialize(options = {})
      @time = Time.now.to_i
      @proof = ''
      options.each { |k, v| send("#{k}=", v) }
    end

    def valid_after?(previous_block)
      (previous_block.hash == parent_hash) &&
        (hash == Digest::SHA256.hexdigest(body.join)) &&
        valid_proof?
    end

    def body
      [index, time, root_hash, parent_hash]
    end

    def valid_proof?
      Digest::SHA256.hexdigest((body + [proof]).join).start_with?('abc')
    end

    def make_proof
      @hash = Digest::SHA256.hexdigest(body.join)
      letters = ('a'..'z').to_a
      @proof << letters.sample until valid_proof?
      self
    end

    # rubocop:disable Metrics/LineLength
    GENESIS = Block.new(index: 0, parent_hash: 0, time: 0, root_hash: "\0", proof: 'lvew')
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dcha-0.1.4 lib/dcha/block.rb
dcha-0.1.3 lib/dcha/block.rb
dcha-0.1.2 lib/dcha/block.rb
dcha-0.1.1 lib/dcha/block.rb
dcha-0.1.0 lib/dcha/block.rb