Merge pull request #1 from vladzamfir/patch-1
Added comments and more detailed reporting
This commit is contained in:
commit
82a17a9db4
61
mining.py
61
mining.py
|
@ -2,6 +2,7 @@ import pyethereum
|
||||||
u = pyethereum.utils
|
u = pyethereum.utils
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
#These are the operations that will end up in the tape
|
||||||
ops = [
|
ops = [
|
||||||
lambda x, y: x+y % 2**256,
|
lambda x, y: x+y % 2**256,
|
||||||
lambda x, y: x*y % 2**256,
|
lambda x, y: x*y % 2**256,
|
||||||
|
@ -12,9 +13,15 @@ ops = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def gen(seed, w, d):
|
'''
|
||||||
|
the tape will be 'w' wide and 'd' operations deep
|
||||||
|
it is a list of triples [i, j, op], later used
|
||||||
|
in the tape's execution: xi = xi op xj
|
||||||
|
'''
|
||||||
|
def gen_tape(seed, w, d):
|
||||||
tape = []
|
tape = []
|
||||||
h = 0
|
h = 0
|
||||||
|
#Getting as much entropy out of a hash as possible
|
||||||
for i in range(d):
|
for i in range(d):
|
||||||
if h < 2**32:
|
if h < 2**32:
|
||||||
h = u.big_endian_to_int(u.sha3(seed+str(i)))
|
h = u.big_endian_to_int(u.sha3(seed+str(i)))
|
||||||
|
@ -31,8 +38,9 @@ def gen(seed, w, d):
|
||||||
def lshift(n):
|
def lshift(n):
|
||||||
return 2**255 * (n % 2) + (n / 2)
|
return 2**255 * (n % 2) + (n / 2)
|
||||||
|
|
||||||
|
#Generates the inputs to and evaluates the tape, the mining nonce can be taken to be in the seed
|
||||||
def run(seed, w, tape):
|
def gen_inputs(seed,w):
|
||||||
|
#generating the tape's inputs
|
||||||
v = []
|
v = []
|
||||||
h = 0
|
h = 0
|
||||||
for i in range(w):
|
for i in range(w):
|
||||||
|
@ -41,19 +49,44 @@ def run(seed, w, tape):
|
||||||
else:
|
else:
|
||||||
h = lshift(h)
|
h = lshift(h)
|
||||||
v.append(h)
|
v.append(h)
|
||||||
|
return v
|
||||||
|
|
||||||
|
#Evaluate tape on inputs (incorrect dimension of v is an unhandled exception)
|
||||||
|
def run_tape(v, tape):
|
||||||
for t in tape:
|
for t in tape:
|
||||||
v[t[0]] = t[2](v[t[0]], v[t[1]])
|
v[t[0]] = t[2](v[t[0]], v[t[1]])
|
||||||
return u.sha3(str(v))
|
#Implemented in a blockchain, any additional hashes or timestamp would be added in the sha
|
||||||
|
return str(v)
|
||||||
|
|
||||||
|
def sha_cap(s):
|
||||||
|
return u.sha3(s)
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test(num_iterations, num_tape_evals, tape_w = 100, tape_d = 1000):
|
||||||
t1 = time.time()
|
time_generating_tape = 0.
|
||||||
for i in range(10):
|
time_generating_inputs = 0.
|
||||||
tape = gen(str(i), 1000, 1000)
|
time_evaluating_tape = 0.
|
||||||
print time.time() - t1
|
time_sha_capping = 0.
|
||||||
|
for i in range(num_iterations):
|
||||||
|
t = time.time()
|
||||||
|
tape = gen_tape(str(i), tape_w, tape_d)
|
||||||
|
time_generating_tape += time.time() - t
|
||||||
|
|
||||||
|
for i in xrange(num_tape_evals):
|
||||||
|
t = time.time()
|
||||||
|
v = gen_inputs(str(i), tape_w)
|
||||||
|
time_generating_inputs += time.time() - t
|
||||||
|
|
||||||
|
t = time.time()
|
||||||
|
x = run_tape(v,tape)
|
||||||
|
time_evaluating_tape += time.time() - t
|
||||||
|
|
||||||
|
t = time.time()
|
||||||
|
h = sha_cap(x)
|
||||||
|
time_sha_capping += time.time() - t
|
||||||
|
|
||||||
t2 = time.time()
|
total_time = time_generating_tape + time_generating_inputs + time_evaluating_tape + time_sha_capping
|
||||||
for i in range(10):
|
print "% of time generating tape:", time_generating_tape/total_time
|
||||||
p = run(str(i), 1000, tape)
|
print "% of time generating inputs:", time_generating_inputs/total_time
|
||||||
print time.time() - t2
|
print "% of time evaluating tape:", time_evaluating_tape/total_time
|
||||||
p = p
|
print "% of time sha-capping:", time_sha_capping/total_time
|
||||||
|
|
Loading…
Reference in New Issue