From 4e3a66e2ebae4a59166005fd4eb4f438494fb778 Mon Sep 17 00:00:00 2001 From: Jude Nelson Date: Tue, 21 Jun 2016 01:48:28 -0400 Subject: [PATCH] Optimization: keep the database in RAM, except when indexing. Be sure to invalidate when finished indexing. The only thing clients will pull from the db is the current consensus hash. --- blockstack/blockstackd.py | 18 +++++++++++++++--- blockstack/lib/nameset/__init__.py | 2 +- blockstack/lib/nameset/virtualchain_hooks.py | 9 +++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/blockstack/blockstackd.py b/blockstack/blockstackd.py index 69fc77e58..5fc67c575 100644 --- a/blockstack/blockstackd.py +++ b/blockstack/blockstackd.py @@ -64,7 +64,7 @@ from ConfigParser import SafeConfigParser import pybitcoin from lib import nameset as blockstack_state_engine -from lib import get_db_state +from lib import get_db_state, invalidate_db_state from lib.config import REINDEX_FREQUENCY, DEFAULT_DUST_FEE from lib import * from lib.storage import * @@ -319,7 +319,7 @@ class BlockstackdRPC(SimpleXMLRPCServer): def rpc_get_name_blockchain_record(self, name): """ - Lookup the blockchain-derived profile for a name. + Lookup the blockchain-derived whois info for a name. """ db = get_state_engine() @@ -331,6 +331,9 @@ class BlockstackdRPC(SimpleXMLRPCServer): name_record = db.get_name(str(name)) + namespace_id = get_namespace_from_name(name) + namespace_record = db.get_namespace(namespace_id) + if name_record is None: if is_indexing(): return {"error": "Indexing blockchain"} @@ -338,8 +341,14 @@ class BlockstackdRPC(SimpleXMLRPCServer): return {"error": "Not found."} else: + + # when does this name expire (if it expires)? + if namespace_record['lifetime'] != NAMESPACE_LIFE_INFINITE: + name_record['expire_block'] = namespace_record['lifetime'] + name_record['last_renewed'] + return name_record + def rpc_get_name_blockchain_history( self, name, start_block, end_block ): """ Get the sequence of name operations processed for a given name. @@ -961,11 +970,14 @@ def index_blockchain(): # bring us up to speed log.debug("Begin indexing (up to %s)" % current_block) set_indexing( True ) - db = virtualchain_hooks.get_db_state() + db = get_state_engine() virtualchain.sync_virtualchain( bt_opts, current_block, db ) set_indexing( False ) log.debug("End indexing (up to %s)" % current_block) + # invalidate in-RAM copy, so we re-load it on next get_state_engine() + invalidate_db_state() + def blockstack_exit(): """ diff --git a/blockstack/lib/nameset/__init__.py b/blockstack/lib/nameset/__init__.py index 66b0b07f4..d37825d24 100644 --- a/blockstack/lib/nameset/__init__.py +++ b/blockstack/lib/nameset/__init__.py @@ -51,4 +51,4 @@ import virtualchain_hooks from .namedb import BlockstackDB, get_namespace_from_name, price_name, \ get_name_from_fq_name, price_namespace, DISPOSITION_RW, DISPOSITION_RO from .virtualchain_hooks import get_virtual_chain_name, get_virtual_chain_version, get_first_block_id, get_opcodes, \ - get_op_processing_order, get_magic_bytes, get_db_state, db_parse, db_check, db_commit, db_save + get_op_processing_order, get_magic_bytes, get_db_state, invalidate_db_state, db_parse, db_check, db_commit, db_save diff --git a/blockstack/lib/nameset/virtualchain_hooks.py b/blockstack/lib/nameset/virtualchain_hooks.py index 08029e281..00c31f2c6 100644 --- a/blockstack/lib/nameset/virtualchain_hooks.py +++ b/blockstack/lib/nameset/virtualchain_hooks.py @@ -304,6 +304,15 @@ def get_db_state(disposition=None): return blockstack_db +def invalidate_db_state(): + """ + Clear out in-RAM cached db state + """ + log.info("Invalidating cached blockstack state") + global blockstack_db + blockstack_db = None + + def db_parse( block_id, txid, vtxindex, opcode, data, senders, inputs, outputs, fee, db_state=None ): """ (required by virtualchain state engine)