def test_end_block_return_validator_updates(a, b, init_chain_request): app = App(a, b) app.init_chain(init_chain_request) begin_block = types.RequestBeginBlock() app.begin_block(begin_block) # generate a block containing a concluded validator election validators = generate_validators([1] * 4) b.store_validator_set(1, [v['storage'] for v in validators]) new_validator = generate_validators([1])[0] public_key = validators[0]['public_key'] private_key = validators[0]['private_key'] voter_keys = [v['private_key'] for v in validators] election, votes = generate_election(b, ValidatorElection, public_key, private_key, new_validator['election'], voter_keys) b.store_block(Block(height=1, transactions=[election.id], app_hash='')._asdict()) b.store_bulk_transactions([election]) Election.process_block(b, 1, [election]) app.block_transactions = votes 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
def test_deliver_tx__double_spend_fails(a, b, init_chain_request): from bigchaindb import App from bigchaindb.models import Transaction from bigchaindb.common.crypto import generate_key_pair alice = generate_key_pair() bob = generate_key_pair() tx = Transaction.create([alice.public_key], [([bob.public_key], 1)])\ .sign([alice.private_key]) app = App(a, b) app.init_chain(init_chain_request) begin_block = types.RequestBeginBlock() app.begin_block(begin_block) result = app.deliver_tx(encode_tx_to_bytes(tx)) assert result.code == CodeTypeOk app.end_block(types.RequestEndBlock(height=99)) app.commit() assert b.get_transaction(tx.id).id == tx.id result = app.deliver_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): import multiprocessing as mp from bigchaindb import App from bigchaindb.models import Transaction from bigchaindb.common.crypto import generate_key_pair alice = generate_key_pair() bob = generate_key_pair() events = mp.Queue() tx = Transaction.create([alice.public_key], [([bob.public_key], 1)])\ .sign([alice.private_key]) app = App(a, b, events) app.init_chain(init_chain_request) begin_block = types.RequestBeginBlock() app.begin_block(begin_block) result = app.deliver_tx(encode_tx_to_bytes(tx)) assert result.code == CodeTypeOk app.end_block(types.RequestEndBlock(height=99)) app.commit() assert b.get_transaction(tx.id).id == tx.id block_event = events.get() assert block_event.data['transactions'] == [tx]
def test_store_pre_commit_state_in_end_block(a, b, alice, init_chain_request): from bigchaindb import App from bigchaindb.backend import query from bigchaindb.models import Transaction tx = Transaction.create([alice.public_key], [([alice.public_key], 1)], asset={'msg': 'live long and prosper'})\ .sign([alice.private_key]) app = App(a, b) app.init_chain(init_chain_request) begin_block = types.RequestBeginBlock() app.begin_block(begin_block) app.deliver_tx(encode_tx_to_bytes(tx)) app.end_block(types.RequestEndBlock(height=99)) resp = query.get_pre_commit_state(b.connection) assert resp['height'] == 99 assert resp['transactions'] == [tx.id] app.begin_block(begin_block) app.deliver_tx(encode_tx_to_bytes(tx)) app.end_block(types.RequestEndBlock(height=100)) resp = query.get_pre_commit_state(b.connection) assert resp['height'] == 100 assert resp['transactions'] == [tx.id] # simulate a chain migration and assert the height is shifted b.store_abci_chain(100, 'new-chain') app = App(a, b) app.begin_block(begin_block) app.deliver_tx(encode_tx_to_bytes(tx)) app.end_block(types.RequestEndBlock(height=1)) resp = query.get_pre_commit_state(b.connection) assert resp['height'] == 101 assert resp['transactions'] == [tx.id]
def test_end_block_aborts_if_chain_is_not_synced(a, b): b.store_abci_chain(0, 'chain-XYZ', False) with pytest.raises(SystemExit): App(a, b).info(types.RequestEndBlock())
def test_app(a, b, init_chain_request): from bigchaindb import App from bigchaindb.tendermint_utils import calculate_hash from bigchaindb.common.crypto import generate_key_pair from bigchaindb.models import Transaction app = App(a, b) p = ProtocolHandler(app) data = p.process( 'info', types.Request(info=types.RequestInfo( version=__tm_supported_versions__[0]))) res = next(read_messages(BytesIO(data), types.Response)) assert res assert res.info.last_block_app_hash == b'' assert res.info.last_block_height == 0 assert not b.get_latest_block() p.process('init_chain', types.Request(init_chain=init_chain_request)) block0 = b.get_latest_block() assert block0 assert block0['height'] == 0 assert block0['app_hash'] == '' pk = codecs.encode(init_chain_request.validators[0].pub_key.data, 'base64').decode().strip('\n') [validator] = b.get_validators(height=1) assert validator['public_key']['value'] == pk assert validator['voting_power'] == 10 alice = generate_key_pair() bob = generate_key_pair() tx = Transaction.create([alice.public_key], [([bob.public_key], 1)])\ .sign([alice.private_key]) etxn = json.dumps(tx.to_dict()).encode('utf8') r = types.Request(check_tx=types.RequestCheckTx(tx=etxn)) data = p.process('check_tx', r) res = next(read_messages(BytesIO(data), types.Response)) assert res assert res.check_tx.code == 0 r = types.Request() r.begin_block.hash = b'' p.process('begin_block', r) r = types.Request(deliver_tx=types.RequestDeliverTx(tx=etxn)) data = p.process('deliver_tx', r) res = next(read_messages(BytesIO(data), types.Response)) assert res assert res.deliver_tx.code == 0 new_block_txn_hash = calculate_hash([tx.id]) r = types.Request(end_block=types.RequestEndBlock(height=1)) data = p.process('end_block', r) res = next(read_messages(BytesIO(data), types.Response)) assert res assert 'end_block' == res.WhichOneof('value') new_block_hash = calculate_hash([block0['app_hash'], new_block_txn_hash]) data = p.process('commit', None) res = next(read_messages(BytesIO(data), types.Response)) assert res.commit.data == new_block_hash.encode('utf-8') assert b.get_transaction(tx.id).id == tx.id block0 = b.get_latest_block() assert block0 assert block0['height'] == 1 assert block0['app_hash'] == new_block_hash # empty block should not update height r = types.Request() r.begin_block.hash = new_block_hash.encode('utf-8') p.process('begin_block', r) r = types.Request() r.end_block.height = 2 p.process('end_block', r) data = p.process('commit', None) res = next(read_messages(BytesIO(data), types.Response)) assert res.commit.data == new_block_hash.encode('utf-8') block0 = b.get_latest_block() assert block0 assert block0['height'] == 2 # when empty block is generated hash of previous block should be returned assert block0['app_hash'] == new_block_hash