diff --git a/bigchaindb/backend/localmongodb/query.py b/bigchaindb/backend/localmongodb/query.py index 5c60eabc..aea34bd9 100644 --- a/bigchaindb/backend/localmongodb/query.py +++ b/bigchaindb/backend/localmongodb/query.py @@ -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) def get_validator_set(conn, height=None): query = {} diff --git a/bigchaindb/backend/query.py b/bigchaindb/backend/query.py index d71412fe..0b5bbade 100644 --- a/bigchaindb/backend/query.py +++ b/bigchaindb/backend/query.py @@ -358,6 +358,13 @@ def store_election(conn, election_id, height, is_concluded): raise NotImplementedError +@singledispatch +def store_elections(conn, elections): + """Store election records in bulk""" + + raise NotImplementedError + + @singledispatch def get_validator_set(conn, height): """Get validator set for a given `height`, if `height` is not specified diff --git a/bigchaindb/elections/election.py b/bigchaindb/elections/election.py index df122f64..bcab8412 100644 --- a/bigchaindb/elections/election.py +++ b/bigchaindb/elections/election.py @@ -277,11 +277,15 @@ class Election(Transaction): for other concluded elections, if it requires so, the method should rely on the database state. """ + # elections placed in this block + initiated_elections = [] + # elections voted for in this block and their votes elections = OrderedDict() for tx in txns: 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): continue election_id = tx.asset['id'] @@ -289,6 +293,9 @@ class Election(Transaction): elections[election_id] = [] elections[election_id].append(tx) + if initiated_elections: + bigchain.store_elections(initiated_elections) + validator_update = None for election_id, votes in elections.items(): election = bigchain.get_transaction(election_id) diff --git a/bigchaindb/lib.py b/bigchaindb/lib.py index a23a69a1..5bf5ecb7 100644 --- a/bigchaindb/lib.py +++ b/bigchaindb/lib.py @@ -484,6 +484,9 @@ class BigchainDB(object): return backend.query.store_election(self.connection, election_id, height, is_concluded) + def store_elections(self, elections): + return backend.query.store_elections(self.connection, elections) + Block = namedtuple('Block', ('app_hash', 'height', 'transactions'))