partial removal of rethinkdb calls from tests

This commit is contained in:
ryan 2016-11-22 14:02:26 +01:00
parent 8343bab89f
commit 9ab0294bc9
5 changed files with 89 additions and 133 deletions

View File

@ -303,44 +303,24 @@ class TestBigchainApi(object):
@pytest.mark.usefixtures('inputs') @pytest.mark.usefixtures('inputs')
def test_genesis_block(self, b): def test_genesis_block(self, b):
import rethinkdb as r block = b.backend.get_genesis_block()
from bigchaindb.util import is_genesis_block
from bigchaindb.db.utils import get_conn
response = list(r.table('bigchain')
.filter(is_genesis_block)
.run(get_conn()))
assert len(response) == 1
block = response[0]
assert len(block['block']['transactions']) == 1 assert len(block['block']['transactions']) == 1
assert block['block']['transactions'][0]['transaction']['operation'] == 'GENESIS' assert block['block']['transactions'][0]['transaction']['operation'] == 'GENESIS'
assert block['block']['transactions'][0]['transaction']['fulfillments'][0]['input'] is None assert block['block']['transactions'][0]['transaction']['fulfillments'][0]['input'] is None
def test_create_genesis_block_fails_if_table_not_empty(self, b): def test_create_genesis_block_fails_if_table_not_empty(self, b):
import rethinkdb as r
from bigchaindb.common.exceptions import GenesisBlockAlreadyExistsError from bigchaindb.common.exceptions import GenesisBlockAlreadyExistsError
from bigchaindb.util import is_genesis_block
from bigchaindb.db.utils import get_conn
b.create_genesis_block() b.create_genesis_block()
with pytest.raises(GenesisBlockAlreadyExistsError): with pytest.raises(GenesisBlockAlreadyExistsError):
b.create_genesis_block() b.create_genesis_block()
genesis_blocks = list(r.table('bigchain')
.filter(is_genesis_block)
.run(get_conn()))
assert len(genesis_blocks) == 1
@pytest.mark.skipif(reason='This test may not make sense after changing the chainification mode') @pytest.mark.skipif(reason='This test may not make sense after changing the chainification mode')
def test_get_last_block(self, b): def test_get_last_block(self, b):
import rethinkdb as r
from bigchaindb.db.utils import get_conn
# get the number of blocks # get the number of blocks
num_blocks = r.table('bigchain').count().run(get_conn()) num_blocks = b.backend.count_blocks()
# get the last block # get the last block
last_block = b.get_last_block() last_block = b.get_last_block()
@ -392,15 +372,10 @@ class TestBigchainApi(object):
assert status == b.BLOCK_UNDECIDED assert status == b.BLOCK_UNDECIDED
def test_get_last_voted_block_returns_genesis_if_no_votes_has_been_casted(self, b): def test_get_last_voted_block_returns_genesis_if_no_votes_has_been_casted(self, b):
import rethinkdb as r
from bigchaindb import util
from bigchaindb.models import Block from bigchaindb.models import Block
from bigchaindb.db.utils import get_conn
b.create_genesis_block() b.create_genesis_block()
genesis = list(r.table('bigchain') genesis = b.backend.get_genesis_block()
.filter(util.is_genesis_block)
.run(get_conn()))[0]
genesis = Block.from_dict(genesis) genesis = Block.from_dict(genesis)
gb = b.get_last_voted_block() gb = b.get_last_voted_block()
assert gb == genesis assert gb == genesis
@ -463,29 +438,25 @@ class TestBigchainApi(object):
assert b.get_last_voted_block().id == block_3.id assert b.get_last_voted_block().id == block_3.id
def test_no_vote_written_if_block_already_has_vote(self, b): def test_no_vote_written_if_block_already_has_vote(self, b):
import rethinkdb as r
from bigchaindb.models import Block from bigchaindb.models import Block
from bigchaindb.db.utils import get_conn
genesis = b.create_genesis_block() genesis = b.create_genesis_block()
block_1 = dummy_block() block_1 = dummy_block()
b.write_block(block_1, durability='hard') b.write_block(block_1, durability='hard')
b.write_vote(b.vote(block_1.id, genesis.id, True)) b.write_vote(b.vote(block_1.id, genesis.id, True))
retrieved_block_1 = r.table('bigchain').get(block_1.id).run(get_conn()) retrieved_block_1 = b.get_block(block_1.id)
retrieved_block_1 = Block.from_dict(retrieved_block_1) retrieved_block_1 = Block.from_dict(retrieved_block_1)
# try to vote again on the retrieved block, should do nothing # try to vote again on the retrieved block, should do nothing
b.write_vote(b.vote(retrieved_block_1.id, genesis.id, True)) b.write_vote(b.vote(retrieved_block_1.id, genesis.id, True))
retrieved_block_2 = r.table('bigchain').get(block_1.id).run(get_conn()) retrieved_block_2 = b.get_block(block_1.id)
retrieved_block_2 = Block.from_dict(retrieved_block_2) retrieved_block_2 = Block.from_dict(retrieved_block_2)
assert retrieved_block_1 == retrieved_block_2 assert retrieved_block_1 == retrieved_block_2
def test_more_votes_than_voters(self, b): def test_more_votes_than_voters(self, b):
import rethinkdb as r
from bigchaindb.common.exceptions import MultipleVotesError from bigchaindb.common.exceptions import MultipleVotesError
from bigchaindb.db.utils import get_conn
b.create_genesis_block() b.create_genesis_block()
block_1 = dummy_block() block_1 = dummy_block()
@ -494,8 +465,8 @@ class TestBigchainApi(object):
vote_1 = b.vote(block_1.id, b.get_last_voted_block().id, True) vote_1 = b.vote(block_1.id, b.get_last_voted_block().id, True)
vote_2 = b.vote(block_1.id, b.get_last_voted_block().id, True) vote_2 = b.vote(block_1.id, b.get_last_voted_block().id, True)
vote_2['node_pubkey'] = 'aaaaaaa' vote_2['node_pubkey'] = 'aaaaaaa'
r.table('votes').insert(vote_1).run(get_conn()) b.write_vote(vote_1)
r.table('votes').insert(vote_2).run(get_conn()) b.write_vote(vote_2)
with pytest.raises(MultipleVotesError) as excinfo: with pytest.raises(MultipleVotesError) as excinfo:
b.block_election_status(block_1.id, block_1.voters) b.block_election_status(block_1.id, block_1.voters)
@ -503,16 +474,14 @@ class TestBigchainApi(object):
.format(block_id=block_1.id, n_votes=str(2), n_voters=str(1)) .format(block_id=block_1.id, n_votes=str(2), n_voters=str(1))
def test_multiple_votes_single_node(self, b): def test_multiple_votes_single_node(self, b):
import rethinkdb as r
from bigchaindb.common.exceptions import MultipleVotesError from bigchaindb.common.exceptions import MultipleVotesError
from bigchaindb.db.utils import get_conn
genesis = b.create_genesis_block() genesis = b.create_genesis_block()
block_1 = dummy_block() block_1 = dummy_block()
b.write_block(block_1, durability='hard') b.write_block(block_1, durability='hard')
# insert duplicate votes # insert duplicate votes
for i in range(2): for i in range(2):
r.table('votes').insert(b.vote(block_1.id, genesis.id, True)).run(get_conn()) b.write_vote(b.vote(block_1.id, genesis.id, True))
with pytest.raises(MultipleVotesError) as excinfo: with pytest.raises(MultipleVotesError) as excinfo:
b.block_election_status(block_1.id, block_1.voters) b.block_election_status(block_1.id, block_1.voters)
@ -525,9 +494,7 @@ class TestBigchainApi(object):
.format(block_id=block_1.id, n_votes=str(2), me=b.me) .format(block_id=block_1.id, n_votes=str(2), me=b.me)
def test_improper_vote_error(selfs, b): def test_improper_vote_error(selfs, b):
import rethinkdb as r
from bigchaindb.common.exceptions import ImproperVoteError from bigchaindb.common.exceptions import ImproperVoteError
from bigchaindb.db.utils import get_conn
b.create_genesis_block() b.create_genesis_block()
block_1 = dummy_block() block_1 = dummy_block()
@ -535,7 +502,7 @@ class TestBigchainApi(object):
vote_1 = b.vote(block_1.id, b.get_last_voted_block().id, True) vote_1 = b.vote(block_1.id, b.get_last_voted_block().id, True)
# mangle the signature # mangle the signature
vote_1['signature'] = 'a' * 87 vote_1['signature'] = 'a' * 87
r.table('votes').insert(vote_1).run(get_conn()) b.write_vote(vote_1)
with pytest.raises(ImproperVoteError) as excinfo: with pytest.raises(ImproperVoteError) as excinfo:
b.has_previous_vote(block_1.id, block_1.id) b.has_previous_vote(block_1.id, block_1.id)
assert excinfo.value.args[0] == 'Block {block_id} already has an incorrectly signed ' \ assert excinfo.value.args[0] == 'Block {block_id} already has an incorrectly signed ' \
@ -543,9 +510,7 @@ class TestBigchainApi(object):
@pytest.mark.usefixtures('inputs') @pytest.mark.usefixtures('inputs')
def test_assign_transaction_one_node(self, b, user_pk, user_sk): def test_assign_transaction_one_node(self, b, user_pk, user_sk):
import rethinkdb as r
from bigchaindb.models import Transaction from bigchaindb.models import Transaction
from bigchaindb.db.utils import get_conn
input_tx = b.get_owned_ids(user_pk).pop() input_tx = b.get_owned_ids(user_pk).pop()
input_tx = b.get_transaction(input_tx.txid) input_tx = b.get_transaction(input_tx.txid)
@ -555,17 +520,15 @@ class TestBigchainApi(object):
b.write_transaction(tx) b.write_transaction(tx)
# retrieve the transaction # retrieve the transaction
response = r.table('backlog').get(tx.id).run(get_conn()) response = list(b.backend.get_stale_transactions(0))[0]
# check if the assignee is the current node # check if the assignee is the current node
assert response['assignee'] == b.me assert response['assignee'] == b.me
@pytest.mark.usefixtures('inputs') @pytest.mark.usefixtures('inputs')
def test_assign_transaction_multiple_nodes(self, b, user_pk, user_sk): def test_assign_transaction_multiple_nodes(self, b, user_pk, user_sk):
import rethinkdb as r
from bigchaindb.common.crypto import generate_key_pair from bigchaindb.common.crypto import generate_key_pair
from bigchaindb.models import Transaction from bigchaindb.models import Transaction
from bigchaindb.db.utils import get_conn
# create 5 federation nodes # create 5 federation nodes
for _ in range(5): for _ in range(5):
@ -580,11 +543,12 @@ class TestBigchainApi(object):
tx = tx.sign([user_sk]) tx = tx.sign([user_sk])
b.write_transaction(tx) b.write_transaction(tx)
# retrieve the transaction # retrieve the transaction
response = r.table('backlog').get(tx.id).run(get_conn()) response = b.backend.get_stale_transactions(0)
# check if the assignee is one of the _other_ federation nodes # check if the assignee is one of the _other_ federation nodes
assert response['assignee'] in b.nodes_except_me for tx in response:
assert tx['assignee'] in b.nodes_except_me
@pytest.mark.usefixtures('inputs') @pytest.mark.usefixtures('inputs')

View File

@ -1,8 +1,6 @@
import time import time
from unittest.mock import patch from unittest.mock import patch
import rethinkdb as r
from multipipes import Pipe from multipipes import Pipe
@ -69,7 +67,7 @@ def test_write_block(b, user_pk):
block_doc = b.create_block(txs) block_doc = b.create_block(txs)
block_maker.write(block_doc) block_maker.write(block_doc)
expected = b.connection.run(r.table('bigchain').get(block_doc.id)) expected = b.backend.get_block(block_doc.id)
expected = Block.from_dict(expected) expected = Block.from_dict(expected)
assert expected == block_doc assert expected == block_doc
@ -90,18 +88,19 @@ def test_duplicate_transaction(b, user_pk):
block_maker.write(block_doc) block_maker.write(block_doc)
# block is in bigchain # block is in bigchain
assert b.connection.run(r.table('bigchain').get(block_doc.id)) == block_doc.to_dict() assert b.backend.get_block(block_doc.id) == block_doc.to_dict()
b.write_transaction(txs[0]) b.write_transaction(txs[0])
# verify tx is in the backlog # verify tx is in the backlog
assert b.connection.run(r.table('backlog').get(txs[0].id)) is not None assert b.get_transaction(txs[0].id) is not None
# try to validate a transaction that's already in the chain; should not work # try to validate a transaction that's already in the chain; should not work
assert block_maker.validate_tx(txs[0].to_dict()) is None assert block_maker.validate_tx(txs[0].to_dict()) is None
# duplicate tx should be removed from backlog # duplicate tx should be removed from backlog
assert b.connection.run(r.table('backlog').get(txs[0].id)) is None response, status = b.get_transaction(txs[0].id, include_status=True)
assert status != b.TX_IN_BACKLOG
def test_delete_tx(b, user_pk): def test_delete_tx(b, user_pk):
@ -119,9 +118,7 @@ def test_delete_tx(b, user_pk):
block_doc = block_maker.create(None, timeout=True) block_doc = block_maker.create(None, timeout=True)
for tx in block_doc.to_dict()['block']['transactions']: for tx in block_doc.to_dict()['block']['transactions']:
returned_tx = b.connection.run(r.table('backlog').get(tx['id'])) returned_tx = b.get_transaction(tx['id']).to_dict()
returned_tx.pop('assignee')
returned_tx.pop('assignment_timestamp')
assert returned_tx == tx assert returned_tx == tx
returned_block = block_maker.delete_tx(block_doc) returned_block = block_maker.delete_tx(block_doc)
@ -129,7 +126,8 @@ def test_delete_tx(b, user_pk):
assert returned_block == block_doc assert returned_block == block_doc
for tx in block_doc.to_dict()['block']['transactions']: for tx in block_doc.to_dict()['block']['transactions']:
assert b.connection.run(r.table('backlog').get(tx['id'])) is None returned_tx, status = b.get_transaction(tx['id'], include_status=True)
assert status != b.TX_IN_BACKLOG
def test_prefeed(b, user_pk): def test_prefeed(b, user_pk):
@ -165,20 +163,16 @@ def test_full_pipeline(b, user_pk):
from bigchaindb.pipelines.block import create_pipeline, get_changefeed from bigchaindb.pipelines.block import create_pipeline, get_changefeed
outpipe = Pipe() outpipe = Pipe()
# include myself here, so that some tx are actually assigned to me
count_assigned_to_me = 0 b.nodes_except_me = [b.me, 'aaa', 'bbb', 'ccc']
for i in range(100): for i in range(100):
tx = Transaction.create([b.me], [([user_pk], 1)], tx = Transaction.create([b.me], [([user_pk], 1)],
{'msg': random.random()}) {'msg': random.random()})
tx = tx.sign([b.me_private]).to_dict() tx = tx.sign([b.me_private])
assignee = random.choice([b.me, 'aaa', 'bbb', 'ccc'])
tx['assignee'] = assignee
tx['assignment_timestamp'] = time.time()
if assignee == b.me:
count_assigned_to_me += 1
b.connection.run(r.table('backlog').insert(tx, durability='hard'))
assert b.connection.run(r.table('backlog').count()) == 100 b.write_transaction(tx)
assert b.backend.count_backlog() == 100
pipeline = create_pipeline() pipeline = create_pipeline()
pipeline.setup(indata=get_changefeed(), outdata=outpipe) pipeline.setup(indata=get_changefeed(), outdata=outpipe)
@ -188,9 +182,9 @@ def test_full_pipeline(b, user_pk):
pipeline.terminate() pipeline.terminate()
block_doc = outpipe.get() block_doc = outpipe.get()
chained_block = b.connection.run(r.table('bigchain').get(block_doc.id)) chained_block = b.backend.get_block(block_doc.id)
chained_block = Block.from_dict(chained_block) chained_block = Block.from_dict(chained_block)
assert len(block_doc.transactions) == count_assigned_to_me block_len = len(block_doc.transactions)
assert chained_block == block_doc assert chained_block == block_doc
assert b.connection.run(r.table('backlog').count()) == 100 - count_assigned_to_me assert b.backend.count_backlog() == 100 - block_len

View File

@ -2,7 +2,6 @@ import time
from unittest.mock import patch from unittest.mock import patch
from bigchaindb.common import crypto from bigchaindb.common import crypto
import rethinkdb as r
from multipipes import Pipe, Pipeline from multipipes import Pipe, Pipeline
from bigchaindb import Bigchain from bigchaindb import Bigchain
@ -33,7 +32,8 @@ def test_check_for_quorum_invalid(b, user_pk):
[member.vote(test_block.id, 'abc', False) for member in test_federation[2:]] [member.vote(test_block.id, 'abc', False) for member in test_federation[2:]]
# cast votes # cast votes
b.connection.run(r.table('votes').insert(votes, durability='hard')) for vote in votes:
b.write_vote(vote)
# since this block is now invalid, should pass to the next process # since this block is now invalid, should pass to the next process
assert e.check_for_quorum(votes[-1]) == test_block assert e.check_for_quorum(votes[-1]) == test_block
@ -62,7 +62,8 @@ def test_check_for_quorum_invalid_prev_node(b, user_pk):
[member.vote(test_block.id, 'def', True) for member in test_federation[2:]] [member.vote(test_block.id, 'def', True) for member in test_federation[2:]]
# cast votes # cast votes
b.connection.run(r.table('votes').insert(votes, durability='hard')) for vote in votes:
b.write_vote(vote)
# since nodes cannot agree on prev block, the block is invalid # since nodes cannot agree on prev block, the block is invalid
assert e.check_for_quorum(votes[-1]) == test_block assert e.check_for_quorum(votes[-1]) == test_block
@ -91,7 +92,8 @@ def test_check_for_quorum_valid(b, user_pk):
votes = [member.vote(test_block.id, 'abc', True) votes = [member.vote(test_block.id, 'abc', True)
for member in test_federation] for member in test_federation]
# cast votes # cast votes
b.connection.run(r.table('votes').insert(votes, durability='hard')) for vote in votes:
b.write_vote(vote)
# since this block is valid, should go nowhere # since this block is valid, should go nowhere
assert e.check_for_quorum(votes[-1]) is None assert e.check_for_quorum(votes[-1]) is None
@ -107,10 +109,12 @@ def test_check_requeue_transaction(b, user_pk):
test_block = b.create_block([tx1]) test_block = b.create_block([tx1])
e.requeue_transactions(test_block) e.requeue_transactions(test_block)
backlog_tx = b.connection.run(r.table('backlog').get(tx1.id))
backlog_tx.pop('assignee') backlog_tx, status = b.get_transaction(tx1.id, include_status=True)
backlog_tx.pop('assignment_timestamp') #backlog_tx = b.connection.run(r.table('backlog').get(tx1.id))
assert backlog_tx == tx1.to_dict() assert status == b.TX_IN_BACKLOG
assert backlog_tx == tx1
@patch.object(Pipeline, 'start') @patch.object(Pipeline, 'start')
@ -157,16 +161,16 @@ def test_full_pipeline(b, user_pk):
vote_valid = b.vote(valid_block.id, 'abc', True) vote_valid = b.vote(valid_block.id, 'abc', True)
vote_invalid = b.vote(invalid_block.id, 'abc', False) vote_invalid = b.vote(invalid_block.id, 'abc', False)
b.connection.run(r.table('votes').insert(vote_valid, durability='hard')) b.write_vote(vote_valid)
b.connection.run(r.table('votes').insert(vote_invalid, durability='hard')) b.write_vote(vote_invalid)
outpipe.get() outpipe.get()
pipeline.terminate() pipeline.terminate()
# only transactions from the invalid block should be returned to # only transactions from the invalid block should be returned to
# the backlog # the backlog
assert b.connection.run(r.table('backlog').count()) == 100 assert b.backend.count_backlog() == 100
# NOTE: I'm still, I'm still tx from the block. # NOTE: I'm still, I'm still tx from the block.
tx_from_block = set([tx.id for tx in invalid_block.transactions]) tx_from_block = set([tx.id for tx in invalid_block.transactions])
tx_from_backlog = set([tx['id'] for tx in list(b.connection.run(r.table('backlog')))]) tx_from_backlog = set([tx['id'] for tx in list(b.backend.get_stale_transactions(0))])
assert tx_from_block == tx_from_backlog assert tx_from_block == tx_from_backlog

View File

@ -1,10 +1,8 @@
import rethinkdb as r
from bigchaindb import Bigchain from bigchaindb import Bigchain
from bigchaindb.pipelines import stale from bigchaindb.pipelines import stale
from multipipes import Pipe, Pipeline from multipipes import Pipe, Pipeline
from unittest.mock import patch from unittest.mock import patch
from bigchaindb import config_utils from bigchaindb import config_utils
import time
import os import os
@ -43,23 +41,23 @@ def test_reassign_transactions(b, user_pk):
stm = stale.StaleTransactionMonitor(timeout=0.001, stm = stale.StaleTransactionMonitor(timeout=0.001,
backlog_reassign_delay=0.001) backlog_reassign_delay=0.001)
stm.bigchain.nodes_except_me = ['aaa', 'bbb', 'ccc'] stm.bigchain.nodes_except_me = ['aaa', 'bbb', 'ccc']
tx = list(b.connection.run(r.table('backlog')))[0] tx = list(b.backend.get_stale_transactions(0))[0]
stm.reassign_transactions(tx) stm.reassign_transactions(tx)
reassigned_tx = b.connection.run(r.table('backlog').get(tx['id'])) reassigned_tx = list(b.backend.get_stale_transactions(0))[0]
assert reassigned_tx['assignment_timestamp'] > tx['assignment_timestamp'] assert reassigned_tx['assignment_timestamp'] > tx['assignment_timestamp']
assert reassigned_tx['assignee'] != tx['assignee'] assert reassigned_tx['assignee'] != tx['assignee']
# test with node not in federation # test with node not in federation
tx = Transaction.create([b.me], [([user_pk], 1)]) tx = Transaction.create([b.me], [([user_pk], 1)])
tx = tx.sign([b.me_private]).to_dict() tx = tx.sign([b.me_private])
tx.update({'assignee': 'lol'}) stm.bigchain.nodes_except_me = ['lol']
tx.update({'assignment_timestamp': time.time()}) b.write_transaction(tx, durability='hard')
b.connection.run(r.table('backlog').insert(tx, durability='hard')) stm.bigchain.nodes_except_me = None
tx = list(b.connection.run(r.table('backlog')))[0] tx = list(b.backend.get_stale_transactions(0))[0]
stm.reassign_transactions(tx) stm.reassign_transactions(tx)
assert b.connection.run(r.table('backlog').get(tx['id']))['assignee'] != 'lol' assert tx['assignee'] != 'lol'
def test_full_pipeline(monkeypatch, user_pk): def test_full_pipeline(monkeypatch, user_pk):
@ -90,9 +88,10 @@ def test_full_pipeline(monkeypatch, user_pk):
original_txc.append(tx.to_dict()) original_txc.append(tx.to_dict())
b.write_transaction(tx) b.write_transaction(tx)
original_txs[tx.id] = b.connection.run(r.table('backlog').get(tx.id)) original_txs = list(b.backend.get_stale_transactions(0))
original_txs = {tx['id']: tx for tx in original_txs}
assert b.connection.run(r.table('backlog').count()) == 100 assert len(original_txs) == 100
monkeypatch.undo() monkeypatch.undo()
@ -107,8 +106,8 @@ def test_full_pipeline(monkeypatch, user_pk):
pipeline.terminate() pipeline.terminate()
assert b.connection.run(r.table('backlog').count()) == 100 assert len(list(b.backend.get_stale_transactions(0))) == 100
reassigned_txs = list(b.connection.run(r.table('backlog'))) reassigned_txs= list(b.backend.get_stale_transactions(0))
# check that every assignment timestamp has increased, and every tx has a new assignee # check that every assignment timestamp has increased, and every tx has a new assignee
for reassigned_tx in reassigned_txs: for reassigned_tx in reassigned_txs:

View File

@ -2,7 +2,6 @@ import time
from unittest.mock import patch from unittest.mock import patch
import rethinkdb as r
from multipipes import Pipe, Pipeline from multipipes import Pipe, Pipeline
@ -166,7 +165,7 @@ def test_valid_block_voting_sequential(b, monkeypatch):
last_vote = vote_obj.vote(*vote_obj.validate_tx(tx, block_id, num_tx)) last_vote = vote_obj.vote(*vote_obj.validate_tx(tx, block_id, num_tx))
vote_obj.write_vote(last_vote) vote_obj.write_vote(last_vote)
vote_rs = b.connection.run(r.table('votes').get_all([block.id, b.me], index='block_and_voter')) vote_rs = b.backend.get_votes_by_block_id_and_voter(block_id, b.me)
vote_doc = vote_rs.next() vote_doc = vote_rs.next()
assert vote_doc['vote'] == {'voting_for_block': block.id, assert vote_doc['vote'] == {'voting_for_block': block.id,
@ -200,7 +199,7 @@ def test_valid_block_voting_multiprocessing(b, monkeypatch):
vote_out = outpipe.get() vote_out = outpipe.get()
vote_pipeline.terminate() vote_pipeline.terminate()
vote_rs = b.connection.run(r.table('votes').get_all([block.id, b.me], index='block_and_voter')) vote_rs = b.backend.get_votes_by_block_id_and_voter(block.id, b.me)
vote_doc = vote_rs.next() vote_doc = vote_rs.next()
assert vote_out['vote'] == vote_doc['vote'] assert vote_out['vote'] == vote_doc['vote']
assert vote_doc['vote'] == {'voting_for_block': block.id, assert vote_doc['vote'] == {'voting_for_block': block.id,
@ -241,7 +240,7 @@ def test_valid_block_voting_with_create_transaction(b, monkeypatch):
vote_out = outpipe.get() vote_out = outpipe.get()
vote_pipeline.terminate() vote_pipeline.terminate()
vote_rs = b.connection.run(r.table('votes').get_all([block.id, b.me], index='block_and_voter')) vote_rs = b.backend.get_votes_by_block_id_and_voter(block.id, b.me)
vote_doc = vote_rs.next() vote_doc = vote_rs.next()
assert vote_out['vote'] == vote_doc['vote'] assert vote_out['vote'] == vote_doc['vote']
assert vote_doc['vote'] == {'voting_for_block': block.id, assert vote_doc['vote'] == {'voting_for_block': block.id,
@ -296,7 +295,7 @@ def test_valid_block_voting_with_transfer_transactions(monkeypatch, b):
vote2_out = outpipe.get() vote2_out = outpipe.get()
vote_pipeline.terminate() vote_pipeline.terminate()
vote_rs = b.connection.run(r.table('votes').get_all([block.id, b.me], index='block_and_voter')) vote_rs = b.backend.get_votes_by_block_id_and_voter(block.id, b.me)
vote_doc = vote_rs.next() vote_doc = vote_rs.next()
assert vote_out['vote'] == vote_doc['vote'] assert vote_out['vote'] == vote_doc['vote']
assert vote_doc['vote'] == {'voting_for_block': block.id, assert vote_doc['vote'] == {'voting_for_block': block.id,
@ -310,7 +309,7 @@ def test_valid_block_voting_with_transfer_transactions(monkeypatch, b):
assert crypto.PublicKey(b.me).verify(serialized_vote, assert crypto.PublicKey(b.me).verify(serialized_vote,
vote_doc['signature']) is True vote_doc['signature']) is True
vote2_rs = b.connection.run(r.table('votes').get_all([block2.id, b.me], index='block_and_voter')) vote2_rs = b.backend.get_votes_by_block_id_and_voter(block2.id, b.me)
vote2_doc = vote2_rs.next() vote2_doc = vote2_rs.next()
assert vote2_out['vote'] == vote2_doc['vote'] assert vote2_out['vote'] == vote2_doc['vote']
assert vote2_doc['vote'] == {'voting_for_block': block2.id, assert vote2_doc['vote'] == {'voting_for_block': block2.id,
@ -347,7 +346,7 @@ def test_unsigned_tx_in_block_voting(monkeypatch, b, user_pk):
vote_out = outpipe.get() vote_out = outpipe.get()
vote_pipeline.terminate() vote_pipeline.terminate()
vote_rs = b.connection.run(r.table('votes').get_all([block.id, b.me], index='block_and_voter')) vote_rs = b.backend.get_votes_by_block_id_and_voter(block.id, b.me)
vote_doc = vote_rs.next() vote_doc = vote_rs.next()
assert vote_out['vote'] == vote_doc['vote'] assert vote_out['vote'] == vote_doc['vote']
assert vote_doc['vote'] == {'voting_for_block': block.id, assert vote_doc['vote'] == {'voting_for_block': block.id,
@ -386,7 +385,7 @@ def test_invalid_id_tx_in_block_voting(monkeypatch, b, user_pk):
vote_out = outpipe.get() vote_out = outpipe.get()
vote_pipeline.terminate() vote_pipeline.terminate()
vote_rs = b.connection.run(r.table('votes').get_all([block['id'], b.me], index='block_and_voter')) vote_rs = b.backend.get_votes_by_block_id_and_voter(block['id'], b.me)
vote_doc = vote_rs.next() vote_doc = vote_rs.next()
assert vote_out['vote'] == vote_doc['vote'] assert vote_out['vote'] == vote_doc['vote']
assert vote_doc['vote'] == {'voting_for_block': block['id'], assert vote_doc['vote'] == {'voting_for_block': block['id'],
@ -425,7 +424,7 @@ def test_invalid_content_in_tx_in_block_voting(monkeypatch, b, user_pk):
vote_out = outpipe.get() vote_out = outpipe.get()
vote_pipeline.terminate() vote_pipeline.terminate()
vote_rs = b.connection.run(r.table('votes').get_all([block['id'], b.me], index='block_and_voter')) vote_rs = b.backend.get_votes_by_block_id_and_voter(block['id'], b.me)
vote_doc = vote_rs.next() vote_doc = vote_rs.next()
assert vote_out['vote'] == vote_doc['vote'] assert vote_out['vote'] == vote_doc['vote']
assert vote_doc['vote'] == {'voting_for_block': block['id'], assert vote_doc['vote'] == {'voting_for_block': block['id'],
@ -460,7 +459,7 @@ def test_invalid_block_voting(monkeypatch, b, user_pk):
vote_out = outpipe.get() vote_out = outpipe.get()
vote_pipeline.terminate() vote_pipeline.terminate()
vote_rs = b.connection.run(r.table('votes').get_all([block['id'], b.me], index='block_and_voter')) vote_rs = b.backend.get_votes_by_block_id_and_voter(block['id'], b.me)
vote_doc = vote_rs.next() vote_doc = vote_rs.next()
assert vote_out['vote'] == vote_doc['vote'] assert vote_out['vote'] == vote_doc['vote']
assert vote_doc['vote'] == {'voting_for_block': block['id'], assert vote_doc['vote'] == {'voting_for_block': block['id'],
@ -483,13 +482,16 @@ def test_voter_considers_unvoted_blocks_when_single_node(monkeypatch, b):
monkeypatch.setattr('time.time', lambda: 1) monkeypatch.setattr('time.time', lambda: 1)
b.create_genesis_block() b.create_genesis_block()
block_ids = []
# insert blocks in the database while the voter process is not listening # insert blocks in the database while the voter process is not listening
# (these blocks won't appear in the changefeed) # (these blocks won't appear in the changefeed)
monkeypatch.setattr('time.time', lambda: 2) monkeypatch.setattr('time.time', lambda: 2)
block_1 = dummy_block(b) block_1 = dummy_block(b)
block_ids.append(block_1.id)
b.write_block(block_1, durability='hard') b.write_block(block_1, durability='hard')
monkeypatch.setattr('time.time', lambda: 3) monkeypatch.setattr('time.time', lambda: 3)
block_2 = dummy_block(b) block_2 = dummy_block(b)
block_ids.append(block_2.id)
b.write_block(block_2, durability='hard') b.write_block(block_2, durability='hard')
vote_pipeline = vote.create_pipeline() vote_pipeline = vote.create_pipeline()
@ -504,6 +506,7 @@ def test_voter_considers_unvoted_blocks_when_single_node(monkeypatch, b):
# create a new block that will appear in the changefeed # create a new block that will appear in the changefeed
monkeypatch.setattr('time.time', lambda: 4) monkeypatch.setattr('time.time', lambda: 4)
block_3 = dummy_block(b) block_3 = dummy_block(b)
block_ids.append(block_3.id)
b.write_block(block_3, durability='hard') b.write_block(block_3, durability='hard')
# Same as before with the two `get`s # Same as before with the two `get`s
@ -511,19 +514,9 @@ def test_voter_considers_unvoted_blocks_when_single_node(monkeypatch, b):
vote_pipeline.terminate() vote_pipeline.terminate()
# retrieve blocks from bigchain
blocks = list(b.connection.run(
r.table('bigchain')
.order_by(r.asc((r.row['block']['timestamp'])))))
# FIXME: remove genesis block, we don't vote on it
# (might change in the future)
blocks.pop(0)
vote_pipeline.terminate()
# retrieve vote # retrieve vote
votes = b.connection.run(r.table('votes')) votes = [list(b.backend.get_votes_by_block_id(_id))[0]
votes = list(votes) for _id in block_ids]
assert all(vote['node_pubkey'] == b.me for vote in votes) assert all(vote['node_pubkey'] == b.me for vote in votes)
@ -536,12 +529,15 @@ def test_voter_chains_blocks_with_the_previous_ones(monkeypatch, b):
monkeypatch.setattr('time.time', lambda: 1) monkeypatch.setattr('time.time', lambda: 1)
b.create_genesis_block() b.create_genesis_block()
block_ids = []
monkeypatch.setattr('time.time', lambda: 2) monkeypatch.setattr('time.time', lambda: 2)
block_1 = dummy_block(b) block_1 = dummy_block(b)
block_ids.append(block_1.id)
b.write_block(block_1, durability='hard') b.write_block(block_1, durability='hard')
monkeypatch.setattr('time.time', lambda: 3) monkeypatch.setattr('time.time', lambda: 3)
block_2 = dummy_block(b) block_2 = dummy_block(b)
block_ids.append(block_2.id)
b.write_block(block_2, durability='hard') b.write_block(block_2, durability='hard')
vote_pipeline = vote.create_pipeline() vote_pipeline = vote.create_pipeline()
@ -555,15 +551,13 @@ def test_voter_chains_blocks_with_the_previous_ones(monkeypatch, b):
vote_pipeline.terminate() vote_pipeline.terminate()
# retrive blocks from bigchain # retrive blocks from bigchain
blocks = list(b.connection.run( blocks = [b.get_block(_id) for _id in block_ids]
r.table('bigchain')
.order_by(r.asc((r.row['block']['timestamp'])))))
# retrieve votes # retrieve votes
votes = list(b.connection.run(r.table('votes'))) votes = [list(b.backend.get_votes_by_block_id(_id))[0]
for _id in block_ids]
assert votes[0]['vote']['voting_for_block'] in (blocks[1]['id'], blocks[2]['id']) assert votes[0]['vote']['voting_for_block'] in (blocks[0]['id'], blocks[1]['id'])
assert votes[1]['vote']['voting_for_block'] in (blocks[1]['id'], blocks[2]['id']) assert votes[1]['vote']['voting_for_block'] in (blocks[0]['id'], blocks[1]['id'])
def test_voter_checks_for_previous_vote(monkeypatch, b): def test_voter_checks_for_previous_vote(monkeypatch, b):
@ -578,8 +572,7 @@ def test_voter_checks_for_previous_vote(monkeypatch, b):
monkeypatch.setattr('time.time', lambda: 2) monkeypatch.setattr('time.time', lambda: 2)
block_1 = dummy_block(b) block_1 = dummy_block(b)
inpipe.put(block_1.to_dict()) inpipe.put(block_1.to_dict())
assert len(list(b.backend.get_votes_by_block_id(block_1.id))) == 0
assert b.connection.run(r.table('votes').count()) == 0
vote_pipeline = vote.create_pipeline() vote_pipeline = vote.create_pipeline()
vote_pipeline.setup(indata=inpipe, outdata=outpipe) vote_pipeline.setup(indata=inpipe, outdata=outpipe)
@ -594,14 +587,16 @@ def test_voter_checks_for_previous_vote(monkeypatch, b):
# queue another block # queue another block
monkeypatch.setattr('time.time', lambda: 4) monkeypatch.setattr('time.time', lambda: 4)
inpipe.put(dummy_block(b).to_dict()) block_2 = dummy_block(b)
inpipe.put(block_2.to_dict())
# wait for the result of the new block # wait for the result of the new block
outpipe.get() outpipe.get()
vote_pipeline.terminate() vote_pipeline.terminate()
assert b.connection.run(r.table('votes').count()) == 2 assert len(list(b.backend.get_votes_by_block_id(block_1.id))) == 1
assert len(list(b.backend.get_votes_by_block_id(block_2.id))) == 1
@patch.object(Pipeline, 'start') @patch.object(Pipeline, 'start')