Last active
January 4, 2016 04:58
-
-
Save worace/8571597 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest/sha1' | |
class Miner | |
attr_reader :tree, :parent, :timestamp | |
def initialize | |
@tree=`git write-tree`.chomp | |
@parent=`git rev-parse HEAD`.chomp | |
@timestamp=12345 | |
prepare_index | |
end | |
def prepare_index | |
user_name = "user-7vxutq41" | |
`perl -i -pe 's/(#{user_name}: )(\d+)/$1 . ($2+1)/e' LEDGER.txt` | |
`grep -q "#{user_name}" LEDGER.txt || echo "#{user_name}: 1" >> LEDGER.txt` | |
`git add LEDGER.txt` | |
`git update-index --add 100644` | |
end | |
def gitify_body_with_header(content) | |
"commit #{content.length}\0#{content}" | |
end | |
def commit_body_with_git(content) | |
`git hash-object -t commit -w --stdin <<< "#{content.chomp}"` | |
end | |
def git_sha(content) | |
Digest::SHA1.hexdigest(gitify_body_with_header(content)) | |
end | |
def difficulty | |
"0001" | |
end | |
def commit_body(counter) | |
body="tree #{tree} | |
parent #{parent} | |
author CTF user <me@example.com> #{timestamp} +0000 | |
committer CTF user <me@example.com> #{timestamp} +0000 | |
Give me a Gitcoin | |
#{counter} | |
" | |
end | |
def generate_sha(counter) | |
git_sha(commit_body(counter)) | |
end | |
def run | |
threads = [] | |
10.times do |i| | |
threads << Thread.new do | |
start_time = Time.now | |
counter = i * 1000000 | |
while true | |
sha = generate_sha(counter) | |
counter += 1 | |
if sha < difficulty | |
#puts "THREAD #{i} got a hit!" | |
#puts "total time: #{Time.now.to_f - start_time.to_f}" | |
puts "sha: #{sha}" | |
#puts "counter: #{counter}" | |
#puts "difficulty: #{difficulty}" | |
puts "committing #{commit_body_with_git(commit_body(counter))}" | |
puts commit_body(counter) | |
end | |
if counter > (i+1) * 10000000 | |
break | |
end | |
end | |
end | |
end | |
threads.map(&:join) | |
puts "done" | |
end | |
end | |
Miner.new.run |
wengzilla
commented
Jan 23, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment