Skip to content

Instantly share code, notes, and snippets.

@kingsleyh
Created April 27, 2020 18:38
Show Gist options
  • Save kingsleyh/5f7404de893d107fb175eef816a0c024 to your computer and use it in GitHub Desktop.
Save kingsleyh/5f7404de893d107fb175eef816a0c024 to your computer and use it in GitHub Desktop.
the bit that does the workers
def start_workers(difficulty, block)
@workers = MinerWorker.create(@num_processes)
@workers.each do |w|
spawn do
loop do
break unless w.can_run
nonce_found_message = w.receive.try &.to_s || "error"
debug "received nonce #{nonce_found_message} from worker"
unless nonce_found_message == "error"
nonce_with_address_json = {nonce: MinerNonce.from_json(nonce_found_message).with_address(@wallet.address)}.to_json
send(socket, M_TYPE_MINER_FOUND_NONCE, MContentMinerFoundNonce.from_json(nonce_with_address_json))
end
update(w, difficulty, block)
rescue ioe : IO::EOFError
warning "received invalid message. will be ignored"
end
end
end
update(difficulty, block)
end
def update(difficulty, block)
debug "update new workers"
@workers.each do |w|
update(w, difficulty, block)
end
end
def update(worker, difficulty, block)
worker.exec({start_nonce: Random.rand(UInt64::MAX).to_s, difficulty: difficulty, block: block}.to_json)
end
def clean_workers
debug "clean workers"
@workers.each(&.kill)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment