added update_utxoset, removed deprecated test utils

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2023-04-07 13:55:57 +02:00
parent b69a217289
commit ea8ef5395a
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
5 changed files with 20 additions and 34 deletions

View File

@ -208,7 +208,7 @@ def get_block_with_transaction(connection, txid):
@singledispatch
def store_transaction_outputs(connection, output: Output, index: int):
def store_transaction_outputs(connection, output: Output, index: int, table: str):
"""Store the transaction outputs.
Args:

View File

@ -126,12 +126,12 @@ def get_transactions_by_metadata(connection, metadata: str, limit: int = 1000) -
tx_ids = [tx[0] for tx in txs]
return get_complete_transactions_by_ids(connection, tx_ids)
@register_query(TarantoolDBConnection)
@catch_db_exception
def store_transaction_outputs(connection, output: Output, index: int) -> str:
def store_transaction_outputs(connection, output: Output, index: int, table=TARANT_TABLE_OUTPUT) -> str:
output_id = uuid4().hex
connection.connect().insert(
TARANT_TABLE_OUTPUT,
table,
(
output_id,
int(output.amount),
@ -363,7 +363,7 @@ def store_unspent_outputs(connection, *unspent_outputs: list):
@register_query(TarantoolDBConnection)
@catch_db_exception
def delete_unspent_outputs(connection, *unspent_outputs: list):
def delete_unspent_outputs(connection, unspent_outputs: list):
result = []
if unspent_outputs:
for utxo in unspent_outputs:

View File

@ -11,7 +11,7 @@ from planetmint.const import GOVERNANCE_TRANSACTION_TYPES
from planetmint.model.fastquery import FastQuery
from planetmint.abci.utils import key_from_base64
from planetmint.backend.connection import Connection
from planetmint.backend.tarantool.const import TARANT_TABLE_TRANSACTION, TARANT_TABLE_GOVERNANCE
from planetmint.backend.tarantool.const import TARANT_TABLE_TRANSACTION, TARANT_TABLE_GOVERNANCE, TARANT_TABLE_UTXOS, TARANT_TABLE_OUTPUT
from planetmint.backend.models.block import Block
from planetmint.backend.models.output import Output
from planetmint.backend.models.asset import Asset
@ -37,6 +37,7 @@ class DataAccessor:
backend.query.store_transactions(self.connection, txns, TARANT_TABLE_TRANSACTION)
backend.query.store_transactions(self.connection, gov_txns, TARANT_TABLE_GOVERNANCE)
[self.update_utxoset(t) for t in txns + gov_txns]
def delete_transactions(self, txs):
return backend.query.delete_transactions(self.connection, txs)
@ -276,6 +277,18 @@ class DataAccessor:
def get_asset_tokens_for_public_key(self, transaction_id, election_pk):
txns = backend.query.get_asset_tokens_for_public_key(self.connection, transaction_id, election_pk)
return txns
def update_utxoset(self, transaction):
spent_outputs = [
{"output_index": input["fulfills"]["output_index"], "transaction_id": input["fulfills"]["transaction_id"]}
for input in transaction["inputs"] if input["fulfills"] != None]
if spent_outputs:
backend.query.delete_unspent_outputs(self.connection, spent_outputs)
[
backend.query.store_transaction_outputs(self.connection, Output.outputs_dict(output, transaction["id"]), index, TARANT_TABLE_UTXOS)
for index, output in enumerate(transaction["outputs"])
]
@property
def fastquery(self):

View File

@ -22,7 +22,7 @@ from ipld import marshal, multihash
from uuid import uuid4
from planetmint.abci.rpc import MODE_COMMIT, MODE_LIST
from tests.utils import delete_unspent_outputs, get_utxoset_merkle_root, store_unspent_outputs, update_utxoset
from tests.utils import delete_unspent_outputs, get_utxoset_merkle_root
@pytest.mark.bdb

View File

@ -171,33 +171,6 @@ def get_utxoset_merkle_root(connection):
return merkleroot(sorted(hashes))
def store_unspent_outputs(connection, *unspent_outputs):
"""Store the given ``unspent_outputs`` (utxos).
Args:
*unspent_outputs (:obj:`tuple` of :obj:`dict`): Variable
length tuple or list of unspent outputs.
"""
if unspent_outputs:
return backend.query.store_unspent_outputs(connection, *unspent_outputs)
def update_utxoset(connection, transaction):
"""
Update the UTXO set given ``transaction``. That is, remove
the outputs that the given ``transaction`` spends, and add the
outputs that the given ``transaction`` creates.
Args:
transaction (:obj:`~planetmint.models.Transaction`): A new
transaction incoming into the system for which the UTXOF
set needs to be updated.
"""
spent_outputs = [spent_output for spent_output in transaction.spent_outputs]
if spent_outputs:
delete_unspent_outputs(connection, *spent_outputs)
store_unspent_outputs(connection, *[utxo._asdict() for utxo in transaction.unspent_outputs])
class ProcessGroup(object):
def __init__(self, concurrency=None, group=None, target=None, name=None, args=None, kwargs=None, daemon=None):