def dbcall(): with self.node.db.open_session() as sess: tlocked = False batch = [] added = [] for peer in peers: assert type(peer) is Peer if not check_address(peer.address): continue if not tlocked: self.node.db.lock_table(sess, Peer) tlocked = True q = sess.query(func.count("*")) if peer.pubkey: assert peer.node_id is None peer.node_id = enc.generate_ID(peer.pubkey) peer.distance, peer.direction = calc_log_distance(self.node_id, peer.node_id) q = q.filter(Peer.node_id == peer.node_id) elif peer.address: assert peer.node_id is None q = q.filter(Peer.address == peer.address) if q.scalar() > 0: if log.isEnabledFor(logging.DEBUG): log.debug("Peer [{}] already in list.".format(peer.address)) continue peer.connected = False if log.isEnabledFor(logging.INFO): log.info("Adding Peer [{}].".format(peer.address)) sess.add(peer) batch.append(peer) if len(batch) == 10: sess.commit() for dbpeer in batch: fetch_id_in_thread = dbpeer.id added.extend(batch) batch.clear() sess.expunge_all() tlocked = False if batch and tlocked: sess.commit() for dbpeer in batch: fetch_id_in_thread = dbpeer.id added.extend(batch) sess.expunge_all() return added
def __find_noonce(rp): # log.debug("Worker running.") wid, prefix, nbits, data, noonce_offset, noonce_size = rp.recv() max_dist = HASH_BITS - nbits nbytes = int(nbits / 8) nbytes += 4 # Extra bytes to increase probability of enough possibilities. nbytes = min(nbytes, noonce_size) ne = noonce_offset + noonce_size noonce_offset = ne - nbytes noonce = wid while True: noonce_bytes = noonce.to_bytes(nbytes, "big") data[noonce_offset:ne] = noonce_bytes h = enc.generate_ID(data) try: dist, direction = mutil.calc_log_distance(h, prefix) match = dist <= max_dist and direction == -1 except IndexError: # log.debug("Exactly matched prefix.") match = True if match: # if log.isEnabledFor(logging.INFO): # log.info("noonce_bytes=[{}]."\ # .format(mutil.hex_string(noonce_bytes))) # if log.isEnabledFor(logging.DEBUG): # log.debug("resulting block=[\n{}]."\ # .format(mutil.hex_dump(data))) rp.send(noonce_bytes) return noonce += WORKERS
def update_distance(self): self.distance, self.direction =\ mutil.calc_log_distance(self.engine.node_id, self.node_id)
def dbcall(): with self.node.db.open_session() as sess: tlocked = False batch = [] added = [] for peer in peers: assert type(peer) is Peer if not check_address(peer.address): continue if not tlocked: self.node.db.lock_table(sess, Peer) tlocked = True q = sess.query(func.count("*")).select_from(Peer) if peer.pubkey: assert peer.node_id is None peer.node_id = enc.generate_ID(peer.pubkey) peer.distance, peer.direction =\ calc_log_distance(\ self.node_id,\ peer.node_id) q = q.filter(Peer.node_id == peer.node_id) elif peer.address: assert peer.node_id is None q = q.filter(Peer.address == peer.address) if q.scalar() > 0: if log.isEnabledFor(logging.DEBUG): log.debug("Peer [{}] already in list."\ .format(peer.address)) continue peer.connected = False if log.isEnabledFor(logging.INFO): log.info("Adding Peer [{}].".format(peer.address)) sess.add(peer) batch.append(peer) if len(batch) == 10: sess.commit() for dbpeer in batch: fetch_id_in_thread = dbpeer.id added.extend(batch) batch.clear() sess.expunge_all() tlocked = False if batch and tlocked: sess.commit() for dbpeer in batch: fetch_id_in_thread = dbpeer.id added.extend(batch) sess.expunge_all() return added