diff --git a/planetmint/backend/models/asset.py b/planetmint/backend/models/asset.py index f2f18c5..2071954 100644 --- a/planetmint/backend/models/asset.py +++ b/planetmint/backend/models/asset.py @@ -12,8 +12,8 @@ from dataclasses import dataclass class Asset: id: str = "" tx_id: str = "" - data: str = "" + data: dict = "" @staticmethod def from_tuple(asset_tuple: tuple) -> Asset: - return Asset(asset_tuple[2], asset_tuple[1], json.loads(asset_tuple[0])) + return Asset(asset_tuple[2], asset_tuple[1], json.loads(asset_tuple[0])["data"]) diff --git a/planetmint/backend/models/output.py b/planetmint/backend/models/output.py index cbd75fb..c8a8067 100644 --- a/planetmint/backend/models/output.py +++ b/planetmint/backend/models/output.py @@ -37,8 +37,9 @@ class Condition: @dataclass class Output: + id: str = "" tx_id: str = "" - amount: int = 0 + amount: str = '0' public_keys: List[str] = field(default_factory=list) condition: Condition = field(default_factory=Condition) @@ -54,6 +55,7 @@ class Output: @staticmethod def from_tuple(output: tuple) -> Output: return Output( + id=output[5], tx_id=output[0], amount=output[1], condition=Condition( diff --git a/planetmint/backend/tarantool/query.py b/planetmint/backend/tarantool/query.py index 7d34bf0..1a3296d 100644 --- a/planetmint/backend/tarantool/query.py +++ b/planetmint/backend/tarantool/query.py @@ -31,7 +31,7 @@ register_query = module_dispatch_registrar(query) @register_query(TarantoolDBConnection) -def _group_transaction_by_ids(connection, txids: list): +def _group_transaction_by_ids(connection, txids: list) -> list[Transaction]: _transactions = [] for txid in txids: tx = get_transaction_space_by_id(connection, txid) @@ -39,8 +39,9 @@ def _group_transaction_by_ids(connection, txids: list): continue tx.inputs = get_inputs_by_tx_id(connection, txid) - tx.outputs = get_outputs_by_tx_id(connection, txid) - tx.keys = get_keys_by_tx_id(connection, txid) + _output = get_outputs_by_tx_id(connection, txid) + _keys = get_keys_by_tx_id(connection, txid) + tx.outputs = [_enricht_output_with_public_keys(_keys, output) for output in _output] tx.assets = get_assets_by_tx_id(connection, txid) tx.metadata = get_metadata_by_tx_id(connection, txid) tx.script = get_script_by_tx_id(connection, txid) @@ -49,6 +50,10 @@ def _group_transaction_by_ids(connection, txids: list): return _transactions +def _enricht_output_with_public_keys(keys: list[Keys], output: Output) -> Output: + output.public_keys = [key.public_keys for key in keys if key.output_id == output.id] + return output + @register_query(TarantoolDBConnection) def get_inputs_by_tx_id(connection, tx_id: str) -> list[Input]: _inputs = connection.run(connection.space(TARANT_TABLE_INPUT).select(tx_id, index=TARANT_ID_SEARCH)) @@ -183,7 +188,7 @@ def get_transaction_space_by_id(connection, transaction_id): @register_query(TarantoolDBConnection) def get_transaction_single(connection, transaction_id): - return _group_transaction_by_ids(txids=[transaction_id], connection=connection) + return _group_transaction_by_ids(txids=[transaction_id], connection=connection)[0] @register_query(TarantoolDBConnection) diff --git a/planetmint/lib.py b/planetmint/lib.py index 397ded1..da69bf4 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -224,11 +224,11 @@ class Planetmint(object): return backend.query.delete_unspent_outputs(self.connection, *unspent_outputs) def is_committed(self, transaction_id): - transaction = backend.query.get_transaction(self.connection, transaction_id) + transaction = backend.query.get_transaction_space_by_id(self.connection, transaction_id) return bool(transaction) def get_transaction(self, transaction_id): - return backend.query.get_transaction(self.connection, transaction_id) + return backend.query.get_transaction_single(self.connection, transaction_id) def get_transactions(self, txn_ids): return backend.query.get_transactions(self.connection, txn_ids) @@ -278,7 +278,7 @@ class Planetmint(object): raise DoubleSpend('tx "{}" spends inputs twice'.format(txid)) elif transactions: tx_id = transactions[0]["transactions"].id - tx = backend.query.get_transaction(self.connection, tx_id) + tx = backend.query.get_transaction_single(self.connection, tx_id) assets = backend.query.get_assets_by_tx_id(self.connection, tx_id) transaction = {"transactions": tx} | {"assets": [asset.data for asset in assets]} elif current_spent_transactions: @@ -693,7 +693,7 @@ class Planetmint(object): return recipients def show_election_status(self, transaction): - data = transaction.assets[0]["data"] + data = transaction.assets[0] if "public_key" in data.keys(): data["public_key"] = public_key_to_base64(data["public_key"]["value"]) response = "" @@ -757,7 +757,7 @@ class Planetmint(object): def get_commited_votes(self, transaction, election_pk=None): # TODO: move somewhere else if election_pk is None: election_pk = election_id_to_public_key(transaction.id) - txns = list(backend.query.get_asset_tokens_for_public_key(self.connection, transaction.id, election_pk)) + txns = backend.query.get_asset_tokens_for_public_key(self.connection, transaction.id, election_pk) return self.count_votes(election_pk, txns) def _get_initiated_elections(self, height, txns): # TODO: move somewhere else @@ -851,7 +851,7 @@ class Planetmint(object): votes_committed = self.get_commited_votes(transaction, election_pk) votes_current = self.count_votes(election_pk, current_votes) - total_votes = sum(output.amount for output in transaction.outputs) + total_votes = sum(int(output.amount) for output in transaction.outputs) if (votes_committed < (2 / 3) * total_votes) and (votes_committed + votes_current >= (2 / 3) * total_votes): return True @@ -909,7 +909,7 @@ class Planetmint(object): if election.operation == CHAIN_MIGRATION_ELECTION: self.migrate_abci_chain() if election.operation == VALIDATOR_ELECTION: - validator_updates = [election.assets[0]["data"]] + validator_updates = [election.assets[0].data] curr_validator_set = self.get_validators(new_height) updated_validator_set = new_validator_set(curr_validator_set, validator_updates) @@ -917,7 +917,7 @@ class Planetmint(object): # TODO change to `new_height + 2` when upgrading to Tendermint 0.24.0. self.store_validator_set(new_height + 1, updated_validator_set) - return encode_validator(election.assets[0]["data"]) + return encode_validator(election.assets[0].data) Block = namedtuple("Block", ("app_hash", "height", "transactions")) diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index 0b9186a..ab2762a 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -320,7 +320,7 @@ def test_election_new_upsert_validator_with_tendermint(b, priv_validator_path, u election_id = run_election_new_upsert_validator(new_args, b) - assert b.get_transaction_space_by_id(election_id) + assert b.get_transaction(election_id) @pytest.mark.bdb @@ -347,7 +347,7 @@ def test_election_new_upsert_validator_without_tendermint(caplog, b, priv_valida with caplog.at_level(logging.INFO): election_id = run_election_new_upsert_validator(args, b) assert caplog.records[0].msg == "[SUCCESS] Submitted proposal with id: " + election_id - assert b.get_transaction_space_by_id(election_id) + assert b.get_transaction(election_id) @pytest.mark.abci @@ -358,7 +358,7 @@ def test_election_new_chain_migration_with_tendermint(b, priv_validator_path, us election_id = run_election_new_chain_migration(new_args, b) - assert b.get_transaction_space_by_id(election_id) + assert b.get_transaction(election_id) @pytest.mark.bdb @@ -377,7 +377,7 @@ def test_election_new_chain_migration_without_tendermint(caplog, b, priv_validat with caplog.at_level(logging.INFO): election_id = run_election_new_chain_migration(args, b) assert caplog.records[0].msg == "[SUCCESS] Submitted proposal with id: " + election_id - assert b.get_transaction_space_by_id(election_id) + assert b.get_transaction(election_id) @pytest.mark.bdb @@ -446,7 +446,7 @@ def test_election_approve_with_tendermint(b, priv_validator_path, user_sk, valid args = Namespace(action="approve", election_id=election_id, sk=priv_validator_path, config={}) approve = run_election_approve(args, b) - assert b.get_transaction_space_by_id(approve) + assert b.get_transaction(approve) @pytest.mark.bdb @@ -463,7 +463,7 @@ def test_election_approve_without_tendermint(caplog, b, priv_validator_path, new with caplog.at_level(logging.INFO): approval_id = run_election_approve(args, b) assert caplog.records[0].msg == "[SUCCESS] Your vote has been submitted" - assert b.get_transaction_space_by_id(approval_id) + assert b.get_transaction(approval_id) @pytest.mark.bdb diff --git a/tests/db/test_planetmint_api.py b/tests/db/test_planetmint_api.py index 301507e..2c73339 100644 --- a/tests/db/test_planetmint_api.py +++ b/tests/db/test_planetmint_api.py @@ -105,12 +105,12 @@ class TestBigchainApi(object): tx = Create.generate([alice.public_key], [([alice.public_key], 1)], assets=[asset1]).sign([alice.private_key]) b.store_bulk_transactions([tx]) - tx_from_db = b.get_transaction_space_by_id(tx.id) + tx_from_db = b.get_transaction(tx.id) before = tx.to_dict() after = tx_from_db.to_dict() - assert before["assets"][0]["data"] == after["transaction"]["assets"][0]["data"] + assert before["assets"][0] == after["transaction"]["assets"][0] before.pop("asset", None) after["transaction"].pop("asset", None) assert before == after["transaction"] @@ -131,7 +131,7 @@ class TestTransactionValidation(object): from transactions.common.exceptions import InvalidSignature input_tx = b.fastquery.get_outputs_by_public_key(user_pk).pop() - input_transaction = b.get_transaction_space_by_id(input_tx.txid) + input_transaction = b.get_transaction(input_tx.txid) sk, pk = generate_key_pair() tx = Create.generate([pk], [([user_pk], 1)]) tx.operation = "TRANSFER" @@ -158,7 +158,7 @@ class TestMultipleInputs(object): user2_sk, user2_pk = crypto.generate_key_pair() tx_link = b.fastquery.get_outputs_by_public_key(user_pk).pop() - input_tx = b.get_transaction_space_by_id(tx_link.txid) + input_tx = b.get_transaction(tx_link.txid) inputs = input_tx.to_inputs() tx = Transfer.generate(inputs, [([user2_pk], 1)], asset_ids=[input_tx.id]) tx = tx.sign([user_sk]) @@ -175,7 +175,7 @@ class TestMultipleInputs(object): user3_sk, user3_pk = crypto.generate_key_pair() tx_link = b.fastquery.get_outputs_by_public_key(user_pk).pop() - input_tx = b.get_transaction_space_by_id(tx_link.txid) + input_tx = b.get_transaction(tx_link.txid) tx = Transfer.generate(input_tx.to_inputs(), [([user2_pk, user3_pk], 1)], asset_ids=[input_tx.id]) tx = tx.sign([user_sk]) @@ -195,7 +195,7 @@ class TestMultipleInputs(object): b.store_bulk_transactions([tx]) owned_input = b.fastquery.get_outputs_by_public_key(user_pk).pop() - input_tx = b.get_transaction_space_by_id(owned_input.txid) + input_tx = b.get_transaction(owned_input.txid) inputs = input_tx.to_inputs() transfer_tx = Transfer.generate(inputs, [([user3_pk], 1)], asset_ids=[input_tx.id]) @@ -220,7 +220,7 @@ class TestMultipleInputs(object): # get input tx_link = b.fastquery.get_outputs_by_public_key(user_pk).pop() - tx_input = b.get_transaction_space_by_id(tx_link.txid) + tx_input = b.get_transaction(tx_link.txid) tx = Transfer.generate(tx_input.to_inputs(), [([user3_pk, user4_pk], 1)], asset_ids=[tx_input.id]) tx = tx.sign([user_sk, user2_sk]) diff --git a/tests/tendermint/test_core.py b/tests/tendermint/test_core.py index 44d4dc1..d036a1b 100644 --- a/tests/tendermint/test_core.py +++ b/tests/tendermint/test_core.py @@ -235,7 +235,7 @@ def test_deliver_tx__valid_create_updates_db_and_emits_event(b, init_chain_reque app.end_block(types.RequestEndBlock(height=99)) app.commit() - assert b.get_transaction_space_by_id(tx.id).id == tx.id + assert b.get_transaction(tx.id).id == tx.id block_event = events.get() assert block_event.data["transactions"] == [tx] @@ -264,7 +264,7 @@ def test_deliver_tx__double_spend_fails(b, init_chain_request): app.end_block(types.RequestEndBlock(height=99)) app.commit() - assert b.get_transaction_space_by_id(tx.id).id == tx.id + assert b.get_transaction(tx.id).id == tx.id result = app.deliver_tx(encode_tx_to_bytes(tx)) assert result.code == CodeTypeError @@ -410,7 +410,7 @@ def test_rollback_pre_commit_state_after_crash(b): rollback(b) for tx in txs: - assert b.get_transaction_space_by_id(tx.id) + assert b.get_transaction(tx.id) assert b.get_latest_abci_chain() assert len(b.get_validator_set()["validators"]) == 1 assert b.get_election(migration_election.id) @@ -421,7 +421,7 @@ def test_rollback_pre_commit_state_after_crash(b): rollback(b) for tx in txs: - assert not b.get_transaction_space_by_id(tx.id) + assert not b.get_transaction(tx.id) assert not b.get_latest_abci_chain() assert len(b.get_validator_set()["validators"]) == 4 assert len(b.get_validator_set(2)["validators"]) == 4 diff --git a/tests/tendermint/test_integration.py b/tests/tendermint/test_integration.py index 6a36d74..341ef09 100644 --- a/tests/tendermint/test_integration.py +++ b/tests/tendermint/test_integration.py @@ -78,7 +78,7 @@ def test_app(b, eventqueue_fixture, init_chain_request): 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_space_by_id(tx.id).id == tx.id + assert b.get_transaction(tx.id).id == tx.id block0 = b.get_latest_block() assert block0 diff --git a/tests/tendermint/test_lib.py b/tests/tendermint/test_lib.py index cd5b0b7..7eef6af 100644 --- a/tests/tendermint/test_lib.py +++ b/tests/tendermint/test_lib.py @@ -67,9 +67,9 @@ def test_asset_is_separated_from_transaciton(b): tx_dict = copy.deepcopy(tx.to_dict()) b.store_bulk_transactions([tx]) - assert "asset" not in backend.query.get_transaction(b.connection, tx.id) - assert backend.query.get_asset(b.connection, tx.id)["data"] == assets[0] - assert b.get_transaction_space_by_id(tx.id).to_dict() == tx_dict + assert "asset" not in backend.query.get_transaction_single(b.connection, tx.id) + assert backend.query.get_asset(b.connection, tx.id).data == assets[0] + assert b.get_transaction(tx.id).to_dict() == tx_dict @pytest.mark.bdb diff --git a/tests/upsert_validator/test_upsert_validator_vote.py b/tests/upsert_validator/test_upsert_validator_vote.py index 86d045d..b0e649e 100644 --- a/tests/upsert_validator/test_upsert_validator_vote.py +++ b/tests/upsert_validator/test_upsert_validator_vote.py @@ -246,7 +246,7 @@ def test_upsert_validator(b, node_key, node_keys, ed25519_node_keys): ) code, message = b.write_transaction(election, BROADCAST_TX_COMMIT) assert code == 202 - assert b.get_transaction_space_by_id(election.id) + assert b.get_transaction(election.id) tx_vote = gen_vote(election, 0, ed25519_node_keys) assert b.validate_transaction(tx_vote)