Skip to content

Instantly share code, notes, and snippets.

@bradford-hamilton
Created May 25, 2018 01:11
Show Gist options
  • Save bradford-hamilton/42e4afb608c2a3da65b61c7efa9a0a1e to your computer and use it in GitHub Desktop.
Save bradford-hamilton/42e4afb608c2a3da65b61c7efa9a0a1e to your computer and use it in GitHub Desktop.
def generate(last_block, data)
new_block = self.create(
last_block[:index] + 1,
Time.now.to_s,
data,
last_block[:hash]
)
i = 0
loop do
hex = i.to_s(16)
new_block = new_block.merge({ nonce: hex })
if !self.is_hash_valid?(self.calculate_hash(new_block), new_block[:difficulty])
puts "Mining: trying another nonce... #{self.calculate_hash(new_block)}"
i += 1
# sleep 25.milliseconds
next
else
puts "\nMining complete! Nonce for this block is #{new_block[:nonce]}."
new_block = new_block.merge({ hash: self.calculate_hash(new_block) })
break
end
end
new_block
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment