class Lapidar::Miner

Public Class Methods

new() click to toggle source
# File lib/lapidar/miner.rb, line 5
def initialize
  @stomach = Digest::SHA2.new(256)
end

Public Instance Methods

mine(base_block, data = "") click to toggle source
# File lib/lapidar/miner.rb, line 9
def mine(base_block, data = "")
  base_block ||= god
  nonce = 0

  until meets_difficulty?(digest(base_block, nonce, data))
    nonce += 1

    # Let others do work as well (TODO: nicer solution without thread context in the miner?)
    Thread.pass if (nonce % 1000).zero?
  end

  Block.new(number: base_block.number + 1, hash: digest(base_block, nonce, data), nonce: nonce, data: data)
end

Private Instance Methods

digest(base_block, nonce, data) click to toggle source
# File lib/lapidar/miner.rb, line 25
def digest(base_block, nonce, data)
  @stomach.hexdigest("#{base_block.hash}-#{nonce}-#{data}")
end
god() click to toggle source
# File lib/lapidar/miner.rb, line 33
def god
  # Virtual block descending from Heaven to create the genesis block
  Block.new(number: -1, hash: nil, nonce: nil)
end
meets_difficulty?(digest) click to toggle source
# File lib/lapidar/miner.rb, line 29
def meets_difficulty?(digest)
  digest.start_with?("0000")
end