Determine current chain in __init__ for efficiency.

This commit is contained in:
Lev Berman 2018-08-30 18:08:25 +02:00
parent cef559cdb0
commit 07b9802194
2 changed files with 18 additions and 18 deletions

View File

@ -48,18 +48,19 @@ class App(BaseApplication):
self.block_transactions = [] self.block_transactions = []
self.validators = None self.validators = None
self.new_height = None self.new_height = None
self.chain = self.bigchaindb.get_latest_abci_chain()
def log_abci_migration_error(self, chain_id, validators): def log_abci_migration_error(self, chain_id, validators):
logger.error(f'An ABCI chain migration is in process. ' + logger.error(f'An ABCI chain migration is in process. ' +
'Download the new ABCI client and configure it with ' + 'Download the new ABCI client and configure it with ' +
'chain_id={chain_id} and validators={validators}.') 'chain_id={chain_id} and validators={validators}.')
def abort_if_abci_chain_is_not_synced(self, chain): def abort_if_abci_chain_is_not_synced(self):
if chain is None or chain['is_synced']: if self.chain is None or self.chain['is_synced']:
return return
validators = self.bigchaindb.get_validators() validators = self.bigchaindb.get_validators()
self.log_abci_migration_error(chain['chain_id'], validators) self.log_abci_migration_error(self.chain['chain_id'], validators)
sys.exit(1) sys.exit(1)
def init_chain(self, genesis): def init_chain(self, genesis):
@ -103,18 +104,19 @@ class App(BaseApplication):
abci_chain_height = 0 if known_chain is None else known_chain['height'] abci_chain_height = 0 if known_chain is None else known_chain['height']
self.bigchaindb.store_abci_chain(abci_chain_height, self.bigchaindb.store_abci_chain(abci_chain_height,
genesis.chain_id, True) genesis.chain_id, True)
self.chain = {'height': abci_chain_height, 'is_synced': True,
'chain_id': genesis.chain_id}
return ResponseInitChain() return ResponseInitChain()
def info(self, request): def info(self, request):
"""Return height of the latest committed block.""" """Return height of the latest committed block."""
chain = self.bigchaindb.get_latest_abci_chain() self.abort_if_abci_chain_is_not_synced()
self.abort_if_abci_chain_is_not_synced(chain)
r = ResponseInfo() r = ResponseInfo()
block = self.bigchaindb.get_latest_block() block = self.bigchaindb.get_latest_block()
if block: if block:
chain_shift = 0 if chain is None else chain['height'] chain_shift = 0 if self.chain is None else self.chain['height']
r.last_block_height = block['height'] - chain_shift r.last_block_height = block['height'] - chain_shift
r.last_block_app_hash = block['app_hash'].encode('utf-8') r.last_block_app_hash = block['app_hash'].encode('utf-8')
else: else:
@ -130,8 +132,7 @@ class App(BaseApplication):
raw_tx: a raw string (in bytes) transaction. raw_tx: a raw string (in bytes) transaction.
""" """
chain = self.bigchaindb.get_latest_abci_chain() self.abort_if_abci_chain_is_not_synced()
self.abort_if_abci_chain_is_not_synced(chain)
logger.benchmark('CHECK_TX_INIT') logger.benchmark('CHECK_TX_INIT')
logger.debug('check_tx: %s', raw_transaction) logger.debug('check_tx: %s', raw_transaction)
@ -151,10 +152,9 @@ class App(BaseApplication):
req_begin_block: block object which contains block header req_begin_block: block object which contains block header
and block hash. and block hash.
""" """
chain = self.bigchaindb.get_latest_abci_chain() self.abort_if_abci_chain_is_not_synced()
self.abort_if_abci_chain_is_not_synced(chain)
chain_shift = 0 if chain is None else chain['height'] chain_shift = 0 if self.chain is None else self.chain['height']
logger.benchmark('BEGIN BLOCK, height:%s, num_txs:%s', logger.benchmark('BEGIN BLOCK, height:%s, num_txs:%s',
req_begin_block.header.height + chain_shift, req_begin_block.header.height + chain_shift,
req_begin_block.header.num_txs) req_begin_block.header.num_txs)
@ -170,8 +170,7 @@ class App(BaseApplication):
raw_tx: a raw string (in bytes) transaction. raw_tx: a raw string (in bytes) transaction.
""" """
chain = self.bigchaindb.get_latest_abci_chain() self.abort_if_abci_chain_is_not_synced()
self.abort_if_abci_chain_is_not_synced(chain)
logger.debug('deliver_tx: %s', raw_transaction) logger.debug('deliver_tx: %s', raw_transaction)
transaction = self.bigchaindb.is_valid_transaction( transaction = self.bigchaindb.is_valid_transaction(
@ -194,10 +193,9 @@ class App(BaseApplication):
height (int): new height of the chain. height (int): new height of the chain.
""" """
chain = self.bigchaindb.get_latest_abci_chain() self.abort_if_abci_chain_is_not_synced()
self.abort_if_abci_chain_is_not_synced(chain)
chain_shift = 0 if chain is None else chain['height'] chain_shift = 0 if self.chain is None else self.chain['height']
height = request_end_block.height + chain_shift height = request_end_block.height + chain_shift
self.new_height = height self.new_height = height
@ -227,8 +225,7 @@ class App(BaseApplication):
def commit(self): def commit(self):
"""Store the new height and along with block hash.""" """Store the new height and along with block hash."""
chain = self.bigchaindb.get_latest_abci_chain() self.abort_if_abci_chain_is_not_synced()
self.abort_if_abci_chain_is_not_synced(chain)
data = self.block_txn_hash.encode('utf-8') data = self.block_txn_hash.encode('utf-8')

View File

@ -182,6 +182,7 @@ def test_info(b):
# simulate a migration and assert the height is shifted # simulate a migration and assert the height is shifted
b.store_abci_chain(2, 'chain-XYZ') b.store_abci_chain(2, 'chain-XYZ')
app = App(b)
b.store_block(Block(app_hash='2', height=2, transactions=[])._asdict()) b.store_block(Block(app_hash='2', height=2, transactions=[])._asdict())
res = app.info(r) res = app.info(r)
assert res.last_block_height == 0 assert res.last_block_height == 0
@ -194,6 +195,7 @@ def test_info(b):
# it's always the latest migration that is taken into account # it's always the latest migration that is taken into account
b.store_abci_chain(4, 'chain-XYZ-new') b.store_abci_chain(4, 'chain-XYZ-new')
app = App(b)
b.store_block(Block(app_hash='4', height=4, transactions=[])._asdict()) b.store_block(Block(app_hash='4', height=4, transactions=[])._asdict())
res = app.info(r) res = app.info(r)
assert res.last_block_height == 0 assert res.last_block_height == 0
@ -402,6 +404,7 @@ def test_store_pre_commit_state_in_end_block(b, alice, init_chain_request):
# simulate a chain migration and assert the height is shifted # simulate a chain migration and assert the height is shifted
b.store_abci_chain(100, 'new-chain') b.store_abci_chain(100, 'new-chain')
app = App(b)
app.begin_block(begin_block) app.begin_block(begin_block)
app.deliver_tx(encode_tx_to_bytes(tx)) app.deliver_tx(encode_tx_to_bytes(tx))
app.end_block(RequestEndBlock(height=1)) app.end_block(RequestEndBlock(height=1))