Insert election records in bulk.

Otherwise, one can significantly slow nodes down by posting a whole bunch of unique elections.
This commit is contained in:
Lev Berman 2018-09-20 14:38:49 +02:00
parent 0576f3ef73
commit b8113f3e7a
4 changed files with 26 additions and 2 deletions

View File

@ -295,6 +295,13 @@ def store_election(conn, election_id, height, is_concluded):
) )
@register_query(LocalMongoDBConnection)
def store_elections(conn, elections):
return conn.run(
conn.collection('elections').insert_many(elections)
)
@register_query(LocalMongoDBConnection) @register_query(LocalMongoDBConnection)
def get_validator_set(conn, height=None): def get_validator_set(conn, height=None):
query = {} query = {}

View File

@ -358,6 +358,13 @@ def store_election(conn, election_id, height, is_concluded):
raise NotImplementedError raise NotImplementedError
@singledispatch
def store_elections(conn, elections):
"""Store election records in bulk"""
raise NotImplementedError
@singledispatch @singledispatch
def get_validator_set(conn, height): def get_validator_set(conn, height):
"""Get validator set for a given `height`, if `height` is not specified """Get validator set for a given `height`, if `height` is not specified

View File

@ -277,11 +277,15 @@ class Election(Transaction):
for other concluded elections, if it requires so, the method should for other concluded elections, if it requires so, the method should
rely on the database state. rely on the database state.
""" """
# elections placed in this block
initiated_elections = []
# elections voted for in this block and their votes
elections = OrderedDict() elections = OrderedDict()
for tx in txns: for tx in txns:
if isinstance(tx, Election): if isinstance(tx, Election):
tx.store(bigchain, new_height, is_concluded=False) initiated_elections.append({'election_id': tx.id,
'height': new_height,
'is_concluded': False})
if not isinstance(tx, Vote): if not isinstance(tx, Vote):
continue continue
election_id = tx.asset['id'] election_id = tx.asset['id']
@ -289,6 +293,9 @@ class Election(Transaction):
elections[election_id] = [] elections[election_id] = []
elections[election_id].append(tx) elections[election_id].append(tx)
if initiated_elections:
bigchain.store_elections(initiated_elections)
validator_update = None validator_update = None
for election_id, votes in elections.items(): for election_id, votes in elections.items():
election = bigchain.get_transaction(election_id) election = bigchain.get_transaction(election_id)

View File

@ -484,6 +484,9 @@ class BigchainDB(object):
return backend.query.store_election(self.connection, election_id, return backend.query.store_election(self.connection, election_id,
height, is_concluded) height, is_concluded)
def store_elections(self, elections):
return backend.query.store_elections(self.connection, elections)
Block = namedtuple('Block', ('app_hash', 'height', 'transactions')) Block = namedtuple('Block', ('app_hash', 'height', 'transactions'))