def fast_getlastblock( impl=None ): """ Fast way to get the last block processed, without loading the db. """ lastblock_path = virtualchain.get_lastblock_filename( impl=impl ) try: with open(lastblock_path, "r") as f: data = f.read().strip() return int(data) except: log.exception("Failed to read: %s" % lastblock_path) return None
def get_db_state(disposition=DISPOSITION_RO): """ (required by virtualchain state engine) Callback to the virtual chain state engine. Get a handle to our state engine implementation (i.e. our name database). Note that in this implementation, the database handle returned will only support read-only operations by default. NO COMMITS WILL BE ALLOWED. """ # make this usable even if we haven't explicitly configured virtualchain impl = virtualchain.get_implementation() if impl is None: impl = sys.modules[__name__] db_filename = virtualchain.get_db_filename(impl=impl) lastblock_filename = virtualchain.get_lastblock_filename(impl=impl) lastblock = None firstcheck = True for path in [db_filename, lastblock_filename]: if os.path.exists(path): # have already created the db firstcheck = False if not firstcheck and not os.path.exists(lastblock_filename): # this can't ever happen log.error("FATAL: no such file or directory: %s" % lastblock_filename) os.abort() # verify that it is well-formed, if it exists elif os.path.exists(lastblock_filename): try: with open(lastblock_filename, "r") as f: lastblock = int(f.read().strip()) except Exception, e: # this can't ever happen log.error("FATAL: failed to parse: %s" % lastblock_filename) log.exception(e) os.abort()
def get_db_state( disposition=DISPOSITION_RO ): """ (required by virtualchain state engine) Callback to the virtual chain state engine. Get a handle to our state engine implementation (i.e. our name database). Note that in this implementation, the database handle returned will only support read-only operations by default. NO COMMITS WILL BE ALLOWED. """ # make this usable even if we haven't explicitly configured virtualchain impl = virtualchain.get_implementation() if impl is None: impl = sys.modules[__name__] db_filename = virtualchain.get_db_filename(impl=impl) lastblock_filename = virtualchain.get_lastblock_filename(impl=impl) lastblock = None firstcheck = True for path in [db_filename, lastblock_filename]: if os.path.exists( path ): # have already created the db firstcheck = False if not firstcheck and not os.path.exists( lastblock_filename ): # this can't ever happen log.error("FATAL: no such file or directory: %s" % lastblock_filename ) os.abort() # verify that it is well-formed, if it exists elif os.path.exists( lastblock_filename ): try: with open(lastblock_filename, "r") as f: lastblock = int( f.read().strip() ) except Exception, e: # this can't ever happen log.error("FATAL: failed to parse: %s" % lastblock_filename) log.exception(e) os.abort()
def clean( confirm=True ): """ Remove blockstore's db, lastblock, and snapshot files. Prompt for confirmation """ delete = False exit_status = 0 if confirm: warning = "WARNING: THIS WILL DELETE YOUR BLOCKSTORE DATABASE!\n" warning+= "Are you sure you want to proceed?\n" warning+= "Type 'YES' if so: " value = raw_input( warning ) if value != "YES": sys.exit(exit_status) else: delete = True else: delete = True if delete: print "Deleting..." db_filename = virtualchain.get_db_filename() lastblock_filename = virtualchain.get_lastblock_filename() snapshots_filename = virtualchain.get_snapshots_filename() for path in [db_filename, lastblock_filename, snapshots_filename]: try: os.unlink( path ) except: log.warning("Unable to delete '%s'" % path) exit_status = 1 sys.exit(exit_status)
def get_last_block(): """ Get the last block processed Return the integer on success Return None on error """ # make this usable even if we haven't explicitly configured virtualchain impl = virtualchain.get_implementation() if impl is None: impl = sys.modules[__name__] lastblock_filename = virtualchain.get_lastblock_filename(impl=impl) if os.path.exists(lastblock_filename): try: with open(lastblock_filename, "r") as f: lastblock = int(f.read().strip()) return lastblock except Exception, e: # this can't ever happen log.exception(e) return None