Class: Lapidar::Miner

Inherits:
Object
  • Object
show all
Defined in:
lib/lapidar/miner.rb

Instance Method Summary collapse

Constructor Details

#initializeMiner

Returns a new instance of Miner.



5
6
7
# File 'lib/lapidar/miner.rb', line 5

def initialize
  @stomach = Digest::SHA2.new(256)
end

Instance Method Details

#mine(base_block, data = "") ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# 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