mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Problem: No queries to store pre-commit state (#2135)
Solution: Add backend logic to store and retireve pre-commit state
This commit is contained in:
parent
7f6782e31d
commit
a4986b7e71
@ -266,6 +266,22 @@ def get_unspent_outputs(conn, *, query=None):
|
||||
projection={'_id': False}))
|
||||
|
||||
|
||||
@register_query(LocalMongoDBConnection)
|
||||
def store_pre_commit_state(conn, state):
|
||||
commit_id = state['commit_id']
|
||||
return conn.run(
|
||||
conn.collection('pre_commit')
|
||||
.update({'id': commit_id}, state, upsert=True)
|
||||
)
|
||||
|
||||
|
||||
@register_query(LocalMongoDBConnection)
|
||||
def get_pre_commit_state(conn, commit_id):
|
||||
return conn.run(conn.collection('pre_commit')
|
||||
.find_one({'commit_id': commit_id},
|
||||
projection={'_id': False}))
|
||||
|
||||
|
||||
@register_query(LocalMongoDBConnection)
|
||||
def store_validator_update(conn, validator_update):
|
||||
try:
|
||||
|
@ -27,7 +27,8 @@ def create_database(conn, dbname):
|
||||
|
||||
@register_schema(LocalMongoDBConnection)
|
||||
def create_tables(conn, dbname):
|
||||
for table_name in ['transactions', 'utxos', 'assets', 'blocks', 'metadata', 'validators']:
|
||||
for table_name in ['transactions', 'utxos', 'assets', 'blocks', 'metadata',
|
||||
'validators', 'pre_commit']:
|
||||
logger.info('Create `%s` table.', table_name)
|
||||
# create the table
|
||||
# TODO: read and write concerns can be declared here
|
||||
@ -41,6 +42,7 @@ def create_indexes(conn, dbname):
|
||||
create_blocks_secondary_index(conn, dbname)
|
||||
create_metadata_secondary_index(conn, dbname)
|
||||
create_utxos_secondary_index(conn, dbname)
|
||||
create_pre_commit_secondary_index(conn, dbname)
|
||||
create_validators_secondary_index(conn, dbname)
|
||||
|
||||
|
||||
@ -113,6 +115,14 @@ def create_utxos_secondary_index(conn, dbname):
|
||||
)
|
||||
|
||||
|
||||
def create_pre_commit_secondary_index(conn, dbname):
|
||||
logger.info('Create `pre_commit` secondary index.')
|
||||
|
||||
conn.conn[dbname]['pre_commit'].create_index('commit_id',
|
||||
name='pre_commit_id',
|
||||
unique=True)
|
||||
|
||||
|
||||
def create_validators_secondary_index(conn, dbname):
|
||||
logger.info('Create `validators` secondary index.')
|
||||
|
||||
|
@ -610,6 +610,21 @@ def get_unspent_outputs(connection, *, query=None):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@singledispatch
|
||||
def store_pre_commit_state(connection, commit_id, state):
|
||||
"""Store pre-commit state in a document with `id` as `commit_id`.
|
||||
|
||||
Args:
|
||||
commit_id (string): `id` of document where `state` should be stored.
|
||||
state (dict): commit state.
|
||||
|
||||
Returns:
|
||||
The result of the operation.
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@singledispatch
|
||||
def store_validator_update(conn, validator_update):
|
||||
"""Store a update for the validator set """
|
||||
@ -617,6 +632,20 @@ def store_validator_update(conn, validator_update):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@singledispatch
|
||||
def get_pre_commit_state(connection, commit_id):
|
||||
"""Get pre-commit state where `id` is `commit_id`.
|
||||
|
||||
Args:
|
||||
commit_id (string): `id` of document where `state` should be stored.
|
||||
|
||||
Returns:
|
||||
Document with `id` as `commit_id`
|
||||
"""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@singledispatch
|
||||
def get_validator_update(conn):
|
||||
"""Get validator updates which are not synced"""
|
||||
|
@ -354,3 +354,5 @@ class BigchainDB(Bigchain):
|
||||
|
||||
|
||||
Block = namedtuple('Block', ('app_hash', 'height', 'transactions'))
|
||||
|
||||
PreCommitState = namedtuple('PreCommitState', ('commit_id', 'height', 'transactions'))
|
||||
|
@ -312,6 +312,33 @@ def test_get_unspent_outputs(db_context, utxoset):
|
||||
assert retrieved_utxoset == unspent_outputs
|
||||
|
||||
|
||||
def test_store_pre_commit_state(db_context):
|
||||
from bigchaindb.backend import query
|
||||
from bigchaindb.tendermint.lib import PreCommitState
|
||||
|
||||
state = PreCommitState(commit_id='test',
|
||||
height=3,
|
||||
transactions=[])
|
||||
|
||||
query.store_pre_commit_state(db_context.conn, state._asdict())
|
||||
cursor = db_context.conn.db.pre_commit.find({'commit_id': 'test'},
|
||||
projection={'_id': False})
|
||||
assert cursor.count() == 1
|
||||
|
||||
|
||||
def test_get_pre_commit_state(db_context):
|
||||
from bigchaindb.backend import query
|
||||
from bigchaindb.tendermint.lib import PreCommitState
|
||||
|
||||
state = PreCommitState(commit_id='test2',
|
||||
height=3,
|
||||
transactions=[])
|
||||
|
||||
db_context.conn.db.pre_commit.insert(state._asdict())
|
||||
resp = query.get_pre_commit_state(db_context.conn, 'test2')
|
||||
assert resp == state._asdict()
|
||||
|
||||
|
||||
def test_store_validator_update():
|
||||
from bigchaindb.backend import connect, query
|
||||
from bigchaindb.backend.query import VALIDATOR_UPDATE_ID
|
||||
|
@ -19,7 +19,9 @@ def test_init_creates_db_tables_and_indexes():
|
||||
|
||||
collection_names = conn.conn[dbname].collection_names()
|
||||
assert set(collection_names) == {
|
||||
'transactions', 'assets', 'metadata', 'blocks', 'utxos', 'validators'}
|
||||
'transactions', 'assets', 'metadata', 'blocks', 'utxos', 'pre_commit',
|
||||
'validators'
|
||||
}
|
||||
|
||||
indexes = conn.conn[dbname]['assets'].index_information().keys()
|
||||
assert set(indexes) == {'_id_', 'asset_id', 'text'}
|
||||
@ -34,6 +36,9 @@ def test_init_creates_db_tables_and_indexes():
|
||||
indexes = conn.conn[dbname]['utxos'].index_information().keys()
|
||||
assert set(indexes) == {'_id_', 'utxo'}
|
||||
|
||||
indexes = conn.conn[dbname]['pre_commit'].index_information().keys()
|
||||
assert set(indexes) == {'_id_', 'pre_commit_id'}
|
||||
|
||||
indexes = conn.conn[dbname]['validators'].index_information().keys()
|
||||
assert set(indexes) == {'_id_', 'update_id'}
|
||||
|
||||
@ -69,7 +74,8 @@ def test_create_tables():
|
||||
|
||||
collection_names = conn.conn[dbname].collection_names()
|
||||
assert set(collection_names) == {
|
||||
'transactions', 'assets', 'metadata', 'blocks', 'utxos', 'validators'}
|
||||
'transactions', 'assets', 'metadata', 'blocks', 'utxos', 'validators',
|
||||
'pre_commit'}
|
||||
|
||||
|
||||
def test_create_secondary_indexes():
|
||||
@ -102,6 +108,10 @@ def test_create_secondary_indexes():
|
||||
assert index_info['utxo']['key'] == [('transaction_id', 1),
|
||||
('output_index', 1)]
|
||||
|
||||
indexes = conn.conn[dbname]['pre_commit'].index_information()
|
||||
assert set(indexes.keys()) == {'_id_', 'pre_commit_id'}
|
||||
assert indexes['pre_commit_id']['unique']
|
||||
|
||||
|
||||
def test_drop(dummy_db):
|
||||
from bigchaindb import backend
|
||||
|
Loading…
x
Reference in New Issue
Block a user