Commit e91958e

bryfry <bryon.fryer@gmail.com>
2014-01-25 00:32:04
friday
1 parent 6022cc6
.gitcoin.py.swp
Binary file
gitcoin.py
@@ -1,9 +1,50 @@
 #!/usr/bin/python
+
 from __future__ import print_function
-import sys
-import time
-#from random import random
+import pygit2
+#   brew install libgit2
+#   pip install pygit2
+from tempfile import mkstemp
+from os import remove, close, path, stat, chmod
+from shutil import move
 from hashlib import sha1
+from random import randint
+from time import time
+import sys # print to stderr
+
+def update_ledger(ledger_path):
+  coin_added = False
+  ledger = open(ledger_path, "r")
+  fh, abs_path = mkstemp()
+  new_ledger = open(abs_path, "w")
+
+  for line in ledger:
+    if username in line:
+      new_ledger.write(username+": "+str(int(line.split(": ")[1])+1)+"\n")
+      coin_added = True
+    else:
+      new_ledger.write(line)
+
+  if not coin_added:
+    new_ledger.write(username+": 1\n")
+
+  new_ledger.close()
+  close(fh)
+  ledger.close()
+  remove(ledger_path)
+  move(abs_path, ledger_path)
+  chmod(ledger_path, 0644)
+
+def add_to_repo(repo, repo_path, filename):
+  "add filename to repository located at path and return the tree object id"
+  blob = repo.create_blob_fromworkdir(filename)
+  treebuilder = repo.TreeBuilder()
+  treebuilder.insert(filename, blob, stat(path.join(repo_path,filename)).st_mode)
+  tree_oid = treebuilder.write()
+  return tree_oid
+
+def reset_repo(repo):
+  pass
 
 def githash(data):
   s = sha1()
@@ -11,21 +52,43 @@ def githash(data):
   s.update(data)
   return s.hexdigest()
 
-if __name__ == "__main__":
-  counter = 0
-  body = sys.stdin.read()
-  difficulty = open("./difficulty.txt","r").read().split()[0]
+def mine_gitcoin(repo, repo_path):
+  difficulty_path = path.join(repo_path,"difficulty.txt")
+  difficulty = open(difficulty_path,"r").read().split()[0]
+  sig = pygit2.Signature(name='gitcoinminer', email='git@coin.co', time=time())
+  tree = add_to_repo(repo, repo_path, ledger_filename)
+  hex_head = [] if repo.is_empty else [repo.head.target.hex]
+
+  count = randint(0, sys.maxint/2)
+  start = time()
   while True:
-    start = time.time()
-    #print(counter%1000, file=sys.stderr)
-    #nonce = sha1(str(random())).hexdigest()
-    counter += 1
-    commit = body+str(counter)
-    #if githash(body+str(counter)) < difficulty:
-    print(commit)
-    print("bodyhash "+githash(body), file=sys.stderr)
-    print("githash "+githash(commit), file=sys.stderr)
+
+    nonce = str(count) #sha1(str(count)).hexdigest()
+    c = repo.create_commit(repo.head.name, sig, sig, nonce, tree, hex_head) 
+
+    count += 1
+    if c.hex < difficulty: break
+    if (count % 1024) == 1: 
+      end = time()
+      print("%0.3f Kh/s ... %d" % (1/(end-start), count), end='\n', file=sys.stderr)
+      start = time()
     break
-    end = time.time()
-    print("%g Kh/s ... %d " % (1/(end-start)/1024, counter), end='', file=sys.stderr)
-    
+
+  print (c.hex)
+
+username = "user-zueglyh2"
+repo_path = "/Users/bryon/r-bryfry/level1/"
+
+ledger_filename = "LEDGER.txt"
+ledger_path = path.join(repo_path,ledger_filename)
+
+repo = pygit2.Repository(repo_path)
+reset_repo(repo)
+update_ledger(ledger_path)
+mine_gitcoin(repo, repo_path)
+
+
+#write to disk
+#repo.index.read()
+#repo.index.add(filename)
+#repo.index.write()
old.py
@@ -0,0 +1,31 @@
+#!/usr/bin/python
+from __future__ import print_function
+import sys
+import time
+#from random import random
+from hashlib import sha1
+
+def githash(data):
+  s = sha1()
+  s.update("commit %u\0" % len(data))
+  s.update(data)
+  return s.hexdigest()
+
+if __name__ == "__main__":
+  counter = 0
+  body = sys.stdin.read()
+  difficulty = open("./difficulty.txt","r").read().split()[0]
+  while True:
+    start = time.time()
+    #print(counter%1000, file=sys.stderr)
+    #nonce = sha1(str(random())).hexdigest()
+    counter += 1
+    commit = body+str(counter)
+    #if githash(body+str(counter)) < difficulty:
+    print(commit)
+    print("bodyhash "+githash(body), file=sys.stderr)
+    print("githash "+githash(commit), file=sys.stderr)
+    break
+    end = time.time()
+    print("%g Kh/s ... %d " % (1/(end-start)/1024, counter), end='', file=sys.stderr)
+