def __loop(self): logging.info("Starting stamper loop") journal = Journal(self.calendar.path + '/journal') try: with open(self.calendar.path + '/journal.known-good', 'r') as known_good_fd: idx = int(known_good_fd.read().strip()) except FileNotFoundError as exp: idx = 0 startup = True while not self.exit_event.is_set(): if not startup: self.__do_ethereum() try: commitment = journal[idx] except KeyError: startup = False self.exit_event.wait(1) continue # Is this commitment already stamped? if commitment in self.calendar: logging.debug('Commitment %s (idx %d) already stamped' % (bytes.hex(commitment), idx)) idx += 1 continue self.pending_commitments.add(commitment) logging.info('Added %s (idx %d) to pending commitments; %d total' % (bytes.hex(commitment), idx, len(self.pending_commitments))) idx += 1
def is_pending(self, commitment): """Return whether or not a commitment is waiting to be stamped Returns False if not, or str reason if it is """ if commitment in self.pending_commitments: return "Pending confirmation in Bitcoin blockchain" else: journal = Journal(self.calendar.path + '/journal') idx = self.journal_cursor while idx is not None: # cursor is None when stamper loop never executed once try: recent_commitment = journal[idx] except KeyError: break if recent_commitment == commitment: return "Pending confirmation in Bitcoin blockchain" idx += 1 for height, ttx in self.txs_waiting_for_confirmation.items(): for commitment_timestamp in ttx.commitment_timestamps: if commitment == commitment_timestamp.msg: return "Timestamped by transaction %s; waiting for %d confirmations"\ % (b2lx(ttx.tx.GetTxid()), self.min_confirmations-1) return False
def __loop(self): logging.info("Starting stamper loop") journal = Journal(self.calendar.path + '/journal') try: with open(self.calendar.path + '/journal.known-good', 'r') as known_good_fd: idx = int(known_good_fd.read().strip()) except FileNotFoundError as exp: idx = 0 while not self.exit_event.is_set(): # Get all pending commitments while len(self.pending_commitments) < self.max_pending: try: commitment = journal[idx] except KeyError: break # Is this commitment already stamped? if commitment not in self.calendar: self.pending_commitments.add(commitment) if idx % 100 == 0: logging.debug('Added %s (idx %d) to pending commitments; %d total' % (b2x(commitment), idx, len(self.pending_commitments))) else: if idx % 10000 == 0: logging.debug('Commitment at idx %d already stamped' % idx) idx += 1 try: self.__do_bitcoin() except bitcoin.rpc.InWarmupError as warmuperr: logging.info("Bitcoincore is warming up: %r" % warmuperr) time.sleep(5) except ValueError as err: # If not caused by misconfiguration this error in bitcoinlib # usually occurs when bitcoincore is not started if str(err).startswith('Cookie file unusable'): logging.error("Proxy Authentication Error: Is bitcoincore running?: %r" % err) time.sleep(5) else: logging.error("__do_bitcoin() failed: %r" % exp, exc_info=True) except Exception as exp: # !@#$ Python. # # Just logging errors like this is garbage, but we don't really # know all the ways that __do_bitcoin() will raise an exception # so easiest just to ignore and continue onwards. # # Mainly Bitcoin Core has been hanging up on our RPC # connection, and python-bitcoinlib doesn't have great handling # of that. In our case we should be safe to just retry as # __do_bitcoin() is fairly self-contained. logging.error("__do_bitcoin() failed: %r" % exp, exc_info=True) self.exit_event.wait(1)
def __init__(self, server_address, aggregator, calendar): class rpc_request_handler(RPCRequestHandler): pass rpc_request_handler.aggregator = aggregator rpc_request_handler.calendar = calendar journal = Journal(calendar.path + '/journal') rpc_request_handler.backup = Backup(journal, calendar, calendar.path + '/backup_cache') super().__init__(server_address, rpc_request_handler)
def __loop(self): logging.info("Starting stamper loop") journal = Journal(self.calendar.path + '/journal') try: with open(self.calendar.path + '/journal.known-good', 'r') as known_good_fd: idx = int(known_good_fd.read().strip()) except FileNotFoundError as exp: idx = 0 while not self.exit_event.is_set(): # Get all pending commitments while len(self.pending_commitments) < self.max_pending: try: commitment = journal[idx] except KeyError: break # Is this commitment already stamped? if commitment not in self.calendar: self.pending_commitments.add(commitment) logging.debug( 'Added %s (idx %d) to pending commitments; %d total' % (b2x(commitment), idx, len(self.pending_commitments))) else: if idx % 1000 == 0: logging.debug('Commitment at idx %d already stamped' % idx) idx += 1 try: self.__do_bitcoin() except Exception as exp: # !@#$ Python. # # Just logging errors like this is garbage, but we don't really # know all the ways that __do_bitcoin() will raise an exception # so easiest just to ignore and continue onwards. # # Mainly Bitcoin Core has been hanging up on our RPC # connection, and python-bitcoinlib doesn't have great handling # of that. In our case we should be safe to just retry as # __do_bitcoin() is fairly self-contained. logging.error("__do_bitcoin() failed: %r" % exp, exc_info=True) self.exit_event.wait(self.stamper_interval)
from otsserver.calendar import Journal journal = Journal( '/Users/casatta/.otsd/eth-calendar/journal') for idx in range(0, 1000): try: commitment = journal[idx] print(str(idx) + ":" + str(len(commitment))) print(commitment) except KeyError: break print (idx)