2017-07-01 17:05:39 +02:00
|
|
|
import base64
|
2017-07-01 15:05:06 +02:00
|
|
|
import hashlib
|
2017-07-01 16:14:31 +02:00
|
|
|
import logging
|
2017-07-01 15:05:06 +02:00
|
|
|
import multiprocessing
|
|
|
|
import shared
|
|
|
|
import struct
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
|
|
|
import structure
|
|
|
|
|
|
|
|
|
|
|
|
def _pow_worker(target, initial_hash, q):
|
|
|
|
nonce = 0
|
2017-07-01 17:21:02 +02:00
|
|
|
logging.debug("target: {}, initial_hash: {}".format(target, base64.b16encode(initial_hash).decode()))
|
2017-07-01 15:05:06 +02:00
|
|
|
trial_value = target + 1
|
|
|
|
|
|
|
|
while trial_value > target:
|
|
|
|
nonce += 1
|
|
|
|
trial_value = struct.unpack('>Q', hashlib.sha512(hashlib.sha512(struct.pack('>Q', nonce) + initial_hash).digest()).digest()[:8])[0]
|
|
|
|
|
|
|
|
q.put(struct.pack('>Q', nonce))
|
|
|
|
|
|
|
|
|
|
|
|
def _worker(obj):
|
|
|
|
q = multiprocessing.Queue()
|
|
|
|
p = multiprocessing.Process(target=_pow_worker, args=(obj.pow_target(), obj.pow_initial_hash(), q))
|
|
|
|
|
2017-07-01 16:14:31 +02:00
|
|
|
logging.debug("Starting POW process")
|
2017-07-01 15:05:06 +02:00
|
|
|
t = time.time()
|
|
|
|
p.start()
|
|
|
|
nonce = q.get()
|
|
|
|
p.join()
|
|
|
|
|
2017-07-01 16:14:31 +02:00
|
|
|
logging.debug("Finished doing POW, nonce: {}, time: {}s".format(nonce, time.time() - t))
|
2017-07-01 15:05:06 +02:00
|
|
|
obj = structure.Object(nonce, obj.expires_time, obj.object_type, obj.version, obj.stream_number, obj.object_payload)
|
2017-07-01 17:05:39 +02:00
|
|
|
logging.debug("Object vector is {}".format(base64.b16encode(obj.vector).decode()))
|
2017-07-01 16:14:31 +02:00
|
|
|
|
2017-07-01 15:05:06 +02:00
|
|
|
with shared.objects_lock:
|
|
|
|
shared.objects[obj.vector] = obj
|
|
|
|
shared.vector_advertise_queue.put(obj.vector)
|
|
|
|
|
|
|
|
|
|
|
|
def do_pow_and_publish(obj):
|
|
|
|
t = threading.Thread(target=_worker, args=(obj, ))
|
|
|
|
t.start()
|