working tests

This commit is contained in:
Jürgen Eckel 2022-02-03 01:07:19 +01:00
parent bd495e1a2f
commit 5fb0913b5f
4 changed files with 96 additions and 99 deletions

View File

@ -31,6 +31,7 @@ from planetmint.common.crypto import (key_pair_from_ed25519_key,
from planetmint.common.exceptions import DatabaseDoesNotExist
from planetmint.lib import Block
from tests.utils import gen_vote
import tests.tendermint.conftest
TEST_DB_NAME = 'planetmint_test'
@ -235,9 +236,10 @@ def merlin():
@pytest.fixture
def a():
from tendermint.abci import types_pb2 as types_v0_34_11
return types_v0_34_11
#def a():
def abci_fixture():
from tendermint.abci import types_pb2
return types_pb2
@pytest.fixture
@ -245,6 +247,12 @@ def b():
from planetmint import Planetmint
return Planetmint()
@pytest.fixture
def eventqueue_fixture():
from multiprocessing import Queue
return Queue()
@pytest.fixture
def b_mock(b, network_validators):

View File

@ -51,9 +51,9 @@ def generate_init_chain_request(chain_id, vals=None):
return types.RequestInitChain(validators=vals, chain_id=chain_id)
def test_init_chain_successfully_registers_chain(a, b):
def test_init_chain_successfully_registers_chain(b):
request = generate_init_chain_request('chain-XYZ')
res = App(b, a).init_chain(request)
res = App(b).init_chain(request)
assert res == types.ResponseInitChain()
chain = query.get_latest_abci_chain(b.connection)
assert chain == {'height': 0, 'chain_id': 'chain-XYZ', 'is_synced': True}
@ -64,10 +64,10 @@ def test_init_chain_successfully_registers_chain(a, b):
}
def test_init_chain_ignores_invalid_init_chain_requests(a, b):
def test_init_chain_ignores_invalid_init_chain_requests(b):
validators = [generate_validator()]
request = generate_init_chain_request('chain-XYZ', validators)
res = App(b, a).init_chain(request)
res = App(b).init_chain(request)
assert res == types.ResponseInitChain()
validator_set = query.get_validator_set(b.connection)
@ -81,7 +81,7 @@ def test_init_chain_ignores_invalid_init_chain_requests(a, b):
]
for r in invalid_requests:
with pytest.raises(SystemExit):
App(b, a).init_chain(r)
App(b).init_chain(r)
# assert nothing changed - neither validator set, nor chain ID
new_validator_set = query.get_validator_set(b.connection)
assert new_validator_set == validator_set
@ -94,10 +94,10 @@ def test_init_chain_ignores_invalid_init_chain_requests(a, b):
}
def test_init_chain_recognizes_new_chain_after_migration(a, b):
def test_init_chain_recognizes_new_chain_after_migration(b):
validators = [generate_validator()]
request = generate_init_chain_request('chain-XYZ', validators)
res = App(b, a).init_chain(request)
res = App(b).init_chain(request)
assert res == types.ResponseInitChain()
validator_set = query.get_validator_set(b.connection)['validators']
@ -116,7 +116,7 @@ def test_init_chain_recognizes_new_chain_after_migration(a, b):
]
for r in invalid_requests:
with pytest.raises(SystemExit):
App(b, a).init_chain(r)
App(b).init_chain(r)
assert query.get_latest_abci_chain(b.connection) == {
'chain_id': 'chain-XYZ-migrated-at-height-1',
'is_synced': False,
@ -129,7 +129,7 @@ def test_init_chain_recognizes_new_chain_after_migration(a, b):
# completes the migration
request = generate_init_chain_request('chain-XYZ-migrated-at-height-1',
validators)
res = App(b, a).init_chain(request)
res = App(b).init_chain(request)
assert res == types.ResponseInitChain()
assert query.get_latest_abci_chain(b.connection) == {
'chain_id': 'chain-XYZ-migrated-at-height-1',
@ -150,7 +150,7 @@ def test_init_chain_recognizes_new_chain_after_migration(a, b):
]
for r in invalid_requests:
with pytest.raises(SystemExit):
App(b, a).init_chain(r)
App(b).init_chain(r)
assert query.get_latest_abci_chain(b.connection) == {
'chain_id': 'chain-XYZ-migrated-at-height-1',
'is_synced': True,
@ -165,9 +165,9 @@ def test_init_chain_recognizes_new_chain_after_migration(a, b):
}
def test_info(a, b):
def test_info(b):
r = types.RequestInfo(version=__tm_supported_versions__[0])
app = App(b, a)
app = App(b)
res = app.info(r)
assert res.last_block_height == 0
@ -180,7 +180,7 @@ def test_info(a, b):
# simulate a migration and assert the height is shifted
b.store_abci_chain(2, 'chain-XYZ')
app = App(b, a)
app = App(b)
b.store_block(Block(app_hash='2', height=2, transactions=[])._asdict())
res = app.info(r)
assert res.last_block_height == 0
@ -193,14 +193,14 @@ def test_info(a, b):
# it's always the latest migration that is taken into account
b.store_abci_chain(4, 'chain-XYZ-new')
app = App(b, a)
app = App(b)
b.store_block(Block(app_hash='4', height=4, transactions=[])._asdict())
res = app.info(r)
assert res.last_block_height == 0
assert res.last_block_app_hash == b'4'
def test_check_tx__signed_create_is_ok(a, b):
def test_check_tx__signed_create_is_ok(b):
from planetmint import App
from planetmint.models import Transaction
from planetmint.common.crypto import generate_key_pair
@ -212,12 +212,12 @@ def test_check_tx__signed_create_is_ok(a, b):
[([bob.public_key], 1)])\
.sign([alice.private_key])
app = App(b, a)
app = App(b)
result = app.check_tx(encode_tx_to_bytes(tx))
assert result.code == OkCode
def test_check_tx__unsigned_create_is_error(a, b):
def test_check_tx__unsigned_create_is_error(b):
from planetmint import App
from planetmint.models import Transaction
from planetmint.common.crypto import generate_key_pair
@ -228,12 +228,12 @@ def test_check_tx__unsigned_create_is_error(a, b):
tx = Transaction.create([alice.public_key],
[([bob.public_key], 1)])
app = App(b, a)
app = App(b)
result = app.check_tx(encode_tx_to_bytes(tx))
assert result.code == CodeTypeError
def test_deliver_tx__valid_create_updates_db_and_emits_event(a, b, init_chain_request):
def test_deliver_tx__valid_create_updates_db_and_emits_event(b, init_chain_request):
import multiprocessing as mp
from planetmint import App
from planetmint.models import Transaction
@ -247,7 +247,6 @@ def test_deliver_tx__valid_create_updates_db_and_emits_event(a, b, init_chain_re
[([bob.public_key], 1)])\
.sign([alice.private_key])
# app = App(b, a, events)
app = App(b, events)
app.init_chain(init_chain_request)
@ -272,7 +271,7 @@ def test_deliver_tx__valid_create_updates_db_and_emits_event(a, b, init_chain_re
# next(unspent_outputs)
def test_deliver_tx__double_spend_fails(a, b, init_chain_request):
def test_deliver_tx__double_spend_fails(b, init_chain_request):
from planetmint import App
from planetmint.models import Transaction
from planetmint.common.crypto import generate_key_pair
@ -284,7 +283,7 @@ def test_deliver_tx__double_spend_fails(a, b, init_chain_request):
[([bob.public_key], 1)])\
.sign([alice.private_key])
app = App(b, a)
app = App(b)
app.init_chain(init_chain_request)
begin_block = types.RequestBeginBlock()
@ -301,12 +300,12 @@ def test_deliver_tx__double_spend_fails(a, b, init_chain_request):
assert result.code == CodeTypeError
def test_deliver_transfer_tx__double_spend_fails(a, b, init_chain_request):
def test_deliver_transfer_tx__double_spend_fails(b, init_chain_request):
from planetmint import App
from planetmint.models import Transaction
from planetmint.common.crypto import generate_key_pair
app = App(b, a)
app = App(b)
app.init_chain(init_chain_request)
begin_block = types.RequestBeginBlock()
@ -345,8 +344,8 @@ def test_deliver_transfer_tx__double_spend_fails(a, b, init_chain_request):
assert result.code == CodeTypeError
def test_end_block_return_validator_updates(a, b, init_chain_request):
app = App(b, a)
def test_end_block_return_validator_updates(b, init_chain_request):
app = App(b)
app.init_chain(init_chain_request)
begin_block = types.RequestBeginBlock()
@ -380,7 +379,7 @@ def test_end_block_return_validator_updates(a, b, init_chain_request):
assert expected == resp.validator_updates[0].pub_key.ed25519
def test_store_pre_commit_state_in_end_block(a, b, alice, init_chain_request):
def test_store_pre_commit_state_in_end_block(b, alice, init_chain_request):
from planetmint import App
from planetmint.backend import query
from planetmint.models import Transaction
@ -390,7 +389,7 @@ def test_store_pre_commit_state_in_end_block(a, b, alice, init_chain_request):
asset={'msg': 'live long and prosper'})\
.sign([alice.private_key])
app = App(b, a)
app = App(b)
app.init_chain(init_chain_request)
begin_block = types.RequestBeginBlock()
@ -411,7 +410,7 @@ def test_store_pre_commit_state_in_end_block(a, b, alice, init_chain_request):
# simulate a chain migration and assert the height is shifted
b.store_abci_chain(100, 'new-chain')
app = App(b, a)
app = App(b)
app.begin_block(begin_block)
app.deliver_tx(encode_tx_to_bytes(tx))
app.end_block(types.RequestEndBlock(height=1))
@ -504,43 +503,43 @@ def test_new_validator_set(b):
assert updated_validator_set == updated_validators
def test_info_aborts_if_chain_is_not_synced(a, b):
def test_info_aborts_if_chain_is_not_synced(b):
b.store_abci_chain(0, 'chain-XYZ', False)
with pytest.raises(SystemExit):
App(b, a).info(types.RequestInfo())
App(b).info(types.RequestInfo())
def test_check_tx_aborts_if_chain_is_not_synced(a, b):
def test_check_tx_aborts_if_chain_is_not_synced(b):
b.store_abci_chain(0, 'chain-XYZ', False)
with pytest.raises(SystemExit):
App(b, a).check_tx('some bytes')
App(b).check_tx('some bytes')
def test_begin_aborts_if_chain_is_not_synced(a, b):
def test_begin_aborts_if_chain_is_not_synced(b):
b.store_abci_chain(0, 'chain-XYZ', False)
with pytest.raises(SystemExit):
App(b, a).info(types.RequestBeginBlock())
App(b).info(types.RequestBeginBlock())
def test_deliver_tx_aborts_if_chain_is_not_synced(a, b):
def test_deliver_tx_aborts_if_chain_is_not_synced(b):
b.store_abci_chain(0, 'chain-XYZ', False)
with pytest.raises(SystemExit):
App(b, a).deliver_tx('some bytes')
App(b).deliver_tx('some bytes')
def test_end_block_aborts_if_chain_is_not_synced(a, b):
def test_end_block_aborts_if_chain_is_not_synced(b):
b.store_abci_chain(0, 'chain-XYZ', False)
with pytest.raises(SystemExit):
App(b, a).info(types.RequestEndBlock())
App(b).info(types.RequestEndBlock())
def test_commit_aborts_if_chain_is_not_synced(a, b):
def test_commit_aborts_if_chain_is_not_synced(b):
b.store_abci_chain(0, 'chain-XYZ', False)
with pytest.raises(SystemExit):
App(b, a).commit()
App(b).commit()

View File

@ -19,13 +19,13 @@ from io import BytesIO
@pytest.mark.bdb
def test_app(a, b, init_chain_request):
def test_app(b, eventqueue_fixture, init_chain_request):
from planetmint import App
from planetmint.tendermint_utils import calculate_hash
from planetmint.common.crypto import generate_key_pair
from planetmint.models import Transaction
app = App(b, a)
app = App(b, eventqueue_fixture)
p = ProtocolHandler(app)
data = p.process('info',
@ -144,14 +144,3 @@ def test_post_transaction_responses(tendermint_ws_url, b):
code, message = b.write_transaction(double_spend, mode)
assert code == 500
assert message == 'Transaction validation failed'
@pytest.mark.bdb
def test_exit_when_tm_ver_not_supported(a, b):
from planetmint import App
app = App(b, a)
p = ProtocolHandler(app)
with pytest.raises(SystemExit):
p.process('info', types.Request(info=types.RequestInfo(version='2')))

View File

@ -25,6 +25,7 @@ from planetmint.tendermint_utils import public_key_to_base64
from planetmint.version import __tm_supported_versions__
from tests.utils import generate_election, generate_validators
from tests.tendermint.conftest import init_chain_request
pytestmark = pytest.mark.bdb
@ -51,9 +52,9 @@ def generate_init_chain_request(chain_id, vals=None):
return types.RequestInitChain(validators=vals, chain_id=chain_id)
def test_init_chain_successfully_registers_chain(a, b):
def test_init_chain_successfully_registers_chain(b):
request = generate_init_chain_request('chain-XYZ')
res = App(b, a).init_chain(request)
res = App(b).init_chain(request)
assert res == types.ResponseInitChain()
chain = query.get_latest_abci_chain(b.connection)
assert chain == {'height': 0, 'chain_id': 'chain-XYZ', 'is_synced': True}
@ -64,10 +65,10 @@ def test_init_chain_successfully_registers_chain(a, b):
}
def test_init_chain_ignores_invalid_init_chain_requests(a, b):
def test_init_chain_ignores_invalid_init_chain_requests(b):
validators = [generate_validator()]
request = generate_init_chain_request('chain-XYZ', validators)
res = App(b, a).init_chain(request)
res = App(b).init_chain(request)
assert res == types.ResponseInitChain()
validator_set = query.get_validator_set(b.connection)
@ -81,7 +82,7 @@ def test_init_chain_ignores_invalid_init_chain_requests(a, b):
]
for r in invalid_requests:
with pytest.raises(SystemExit):
App(b, a).init_chain(r)
App(b).init_chain(r)
# assert nothing changed - neither validator set, nor chain ID
new_validator_set = query.get_validator_set(b.connection)
assert new_validator_set == validator_set
@ -94,10 +95,10 @@ def test_init_chain_ignores_invalid_init_chain_requests(a, b):
}
def test_init_chain_recognizes_new_chain_after_migration(a, b):
def test_init_chain_recognizes_new_chain_after_migration(b):
validators = [generate_validator()]
request = generate_init_chain_request('chain-XYZ', validators)
res = App(b, a).init_chain(request)
res = App(b).init_chain(request)
assert res == types.ResponseInitChain()
validator_set = query.get_validator_set(b.connection)['validators']
@ -116,7 +117,7 @@ def test_init_chain_recognizes_new_chain_after_migration(a, b):
]
for r in invalid_requests:
with pytest.raises(SystemExit):
App(b, a).init_chain(r)
App(b).init_chain(r)
assert query.get_latest_abci_chain(b.connection) == {
'chain_id': 'chain-XYZ-migrated-at-height-1',
'is_synced': False,
@ -129,7 +130,7 @@ def test_init_chain_recognizes_new_chain_after_migration(a, b):
# completes the migration
request = generate_init_chain_request('chain-XYZ-migrated-at-height-1',
validators)
res = App(b, a).init_chain(request)
res = App(b).init_chain(request)
assert res == types.ResponseInitChain()
assert query.get_latest_abci_chain(b.connection) == {
'chain_id': 'chain-XYZ-migrated-at-height-1',
@ -150,7 +151,7 @@ def test_init_chain_recognizes_new_chain_after_migration(a, b):
]
for r in invalid_requests:
with pytest.raises(SystemExit):
App(b, a).init_chain(r)
App(b).init_chain(r)
assert query.get_latest_abci_chain(b.connection) == {
'chain_id': 'chain-XYZ-migrated-at-height-1',
'is_synced': True,
@ -165,9 +166,9 @@ def test_init_chain_recognizes_new_chain_after_migration(a, b):
}
def test_info(a, b):
def test_info(b):
r = types.RequestInfo(version=__tm_supported_versions__[0])
app = App(b, a)
app = App(b)
res = app.info(r)
assert res.last_block_height == 0
@ -180,7 +181,7 @@ def test_info(a, b):
# simulate a migration and assert the height is shifted
b.store_abci_chain(2, 'chain-XYZ')
app = App(b, a)
app = App(b)
b.store_block(Block(app_hash='2', height=2, transactions=[])._asdict())
res = app.info(r)
assert res.last_block_height == 0
@ -193,14 +194,14 @@ def test_info(a, b):
# it's always the latest migration that is taken into account
b.store_abci_chain(4, 'chain-XYZ-new')
app = App(b, a)
app = App(b)
b.store_block(Block(app_hash='4', height=4, transactions=[])._asdict())
res = app.info(r)
assert res.last_block_height == 0
assert res.last_block_app_hash == b'4'
def test_check_tx__signed_create_is_ok(a, b):
def test_check_tx__signed_create_is_ok(b):
from planetmint import App
from planetmint.models import Transaction
from planetmint.common.crypto import generate_key_pair
@ -212,12 +213,12 @@ def test_check_tx__signed_create_is_ok(a, b):
[([bob.public_key], 1)])\
.sign([alice.private_key])
app = App(b, a)
app = App(b)
result = app.check_tx(encode_tx_to_bytes(tx))
assert result.code == OkCode
def test_check_tx__unsigned_create_is_error(a, b):
def test_check_tx__unsigned_create_is_error(b):
from planetmint import App
from planetmint.models import Transaction
from planetmint.common.crypto import generate_key_pair
@ -228,12 +229,12 @@ def test_check_tx__unsigned_create_is_error(a, b):
tx = Transaction.create([alice.public_key],
[([bob.public_key], 1)])
app = App(b, a)
app = App(b)
result = app.check_tx(encode_tx_to_bytes(tx))
assert result.code == CodeTypeError
def test_deliver_tx__valid_create_updates_db_and_emits_event(a, b, init_chain_request):
def test_deliver_tx__valid_create_updates_db_and_emits_event(b, init_chain_request):
import multiprocessing as mp
from planetmint import App
from planetmint.models import Transaction
@ -247,7 +248,7 @@ def test_deliver_tx__valid_create_updates_db_and_emits_event(a, b, init_chain_re
[([bob.public_key], 1)])\
.sign([alice.private_key])
app = App( a, events)
app = App(b, events)
app.init_chain(init_chain_request)
@ -271,7 +272,7 @@ def test_deliver_tx__valid_create_updates_db_and_emits_event(a, b, init_chain_re
# next(unspent_outputs)
def test_deliver_tx__double_spend_fails(a, b, init_chain_request):
def test_deliver_tx__double_spend_fails(b, eventqueue_fixture, init_chain_request):
from planetmint import App
from planetmint.models import Transaction
from planetmint.common.crypto import generate_key_pair
@ -283,7 +284,7 @@ def test_deliver_tx__double_spend_fails(a, b, init_chain_request):
[([bob.public_key], 1)])\
.sign([alice.private_key])
app = App(b, a)
app = App(b, eventqueue_fixture)
app.init_chain(init_chain_request)
begin_block = types.RequestBeginBlock()
@ -300,12 +301,12 @@ def test_deliver_tx__double_spend_fails(a, b, init_chain_request):
assert result.code == CodeTypeError
def test_deliver_transfer_tx__double_spend_fails(a, b, init_chain_request):
def test_deliver_transfer_tx__double_spend_fails(b, init_chain_request):
from planetmint import App
from planetmint.models import Transaction
from planetmint.common.crypto import generate_key_pair
app = App(b, a)
app = App(b)
app.init_chain(init_chain_request)
begin_block = types.RequestBeginBlock()
@ -344,8 +345,8 @@ def test_deliver_transfer_tx__double_spend_fails(a, b, init_chain_request):
assert result.code == CodeTypeError
def test_end_block_return_validator_updates(a, b, init_chain_request):
app = App(b, a)
def test_end_block_return_validator_updates(b, init_chain_request):
app = App(b)
app.init_chain(init_chain_request)
begin_block = types.RequestBeginBlock()
@ -376,10 +377,10 @@ def test_end_block_return_validator_updates(a, b, init_chain_request):
resp = app.end_block(types.RequestEndBlock(height=2))
assert resp.validator_updates[0].power == new_validator['election']['power']
expected = bytes.fromhex(new_validator['election']['public_key']['value'])
assert expected == resp.validator_updates[0].pub_key.data
assert expected == resp.validator_updates[0].pub_key.ed25519
def test_store_pre_commit_state_in_end_block(a, b, alice, init_chain_request):
def test_store_pre_commit_state_in_end_block(b, alice, init_chain_request):
from planetmint import App
from planetmint.backend import query
from planetmint.models import Transaction
@ -389,7 +390,7 @@ def test_store_pre_commit_state_in_end_block(a, b, alice, init_chain_request):
asset={'msg': 'live long and prosper'})\
.sign([alice.private_key])
app = App(b, a)
app = App(b)
app.init_chain(init_chain_request)
begin_block = types.RequestBeginBlock()
@ -410,7 +411,7 @@ def test_store_pre_commit_state_in_end_block(a, b, alice, init_chain_request):
# simulate a chain migration and assert the height is shifted
b.store_abci_chain(100, 'new-chain')
app = App(b, a)
app = App(b)
app.begin_block(begin_block)
app.deliver_tx(encode_tx_to_bytes(tx))
app.end_block(types.RequestEndBlock(height=1))
@ -503,43 +504,43 @@ def test_new_validator_set(b):
assert updated_validator_set == updated_validators
def test_info_aborts_if_chain_is_not_synced(a, b):
def test_info_aborts_if_chain_is_not_synced(b):
b.store_abci_chain(0, 'chain-XYZ', False)
with pytest.raises(SystemExit):
App(b, a).info(types.RequestInfo())
App(b).info(types.RequestInfo())
def test_check_tx_aborts_if_chain_is_not_synced(a, b):
def test_check_tx_aborts_if_chain_is_not_synced(b):
b.store_abci_chain(0, 'chain-XYZ', False)
with pytest.raises(SystemExit):
App(b, a).check_tx('some bytes')
App(b).check_tx('some bytes')
def test_begin_aborts_if_chain_is_not_synced(a, b):
def test_begin_aborts_if_chain_is_not_synced(b):
b.store_abci_chain(0, 'chain-XYZ', False)
with pytest.raises(SystemExit):
App(b, a).info(types.RequestBeginBlock())
App(b).info(types.RequestBeginBlock())
def test_deliver_tx_aborts_if_chain_is_not_synced(a, b):
def test_deliver_tx_aborts_if_chain_is_not_synced(b):
b.store_abci_chain(0, 'chain-XYZ', False)
with pytest.raises(SystemExit):
App(b, a).deliver_tx('some bytes')
App(b).deliver_tx('some bytes')
def test_end_block_aborts_if_chain_is_not_synced(a, b):
def test_end_block_aborts_if_chain_is_not_synced(b):
b.store_abci_chain(0, 'chain-XYZ', False)
with pytest.raises(SystemExit):
App(b, a).info(types.RequestEndBlock())
App(b).info(types.RequestEndBlock())
def test_commit_aborts_if_chain_is_not_synced(a, b):
def test_commit_aborts_if_chain_is_not_synced(b):
b.store_abci_chain(0, 'chain-XYZ', False)
with pytest.raises(SystemExit):
App(b, a).commit()
App(b).commit()