Skip to content

Commit

Permalink
check reference inputs txins on dbsync
Browse files Browse the repository at this point in the history
  • Loading branch information
saratomaz committed May 16, 2023
1 parent 1abc060 commit 69961cb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
31 changes: 15 additions & 16 deletions cardano_node_tests/tests/test_scripts.py
Expand Up @@ -2167,7 +2167,7 @@ def test_script_reference_utxo(
fund_amount = 4_500_000
amount = 2_000_000

# create multisig script
# Create multisig script
if script_version == "simple_v1":
invalid_before = None
invalid_hereafter = None
Expand Down Expand Up @@ -2199,7 +2199,7 @@ def test_script_reference_utxo(
slot_type_arg=clusterlib.MultiSlotTypeArgs.AFTER,
)

# create reference UTxO
# Create reference UTxO
reference_utxo, tx_out_reference = clusterlib_utils.create_reference_utxo(
temp_template=temp_template,
cluster_obj=cluster,
Expand All @@ -2210,12 +2210,12 @@ def test_script_reference_utxo(
)
assert reference_utxo.reference_script

# create script address
# Create script address
script_address = cluster.g_address.gen_payment_addr(
addr_name=temp_template, payment_script_file=multisig_script
)

# send funds to script address
# Send funds to script address
tx_out_to = multisig_tx(
cluster_obj=cluster,
temp_template=f"{temp_template}_to",
Expand All @@ -2226,12 +2226,12 @@ def test_script_reference_utxo(
use_build_cmd=use_build_cmd,
)

# send funds from script address
# Send funds from script address
destinations = [clusterlib.TxOut(address=dst_addr.address, amount=amount)]
tx_files = clusterlib.TxFiles(
signing_key_files=[dst_addr.skey_file],
)
# empty `txins` means Tx inputs will be selected automatically by ClusterLib magic
# Empty `txins` means Tx inputs will be selected automatically by ClusterLib magic
script_txins = [
clusterlib.ScriptTxIn(
txins=[],
Expand Down Expand Up @@ -2269,7 +2269,7 @@ def test_script_reference_utxo(
invalid_before=invalid_before,
)

# check final balances
# Check final balances
out_utxos = cluster.g_query.get_utxo(tx_raw_output=tx_out_from)
assert (
clusterlib.filter_utxos(utxos=out_utxos, address=script_address)[0].amount
Expand All @@ -2284,15 +2284,7 @@ def test_script_reference_utxo(

dbsync_utils.check_tx(cluster_obj=cluster, tx_raw_output=tx_out_reference)
dbsync_utils.check_tx(cluster_obj=cluster, tx_raw_output=tx_out_to)
# TODO: check reference script in db-sync (the `tx_out_from`)

# check expected script type
# TODO: moved the check to the end of the test because of XFAIL
if (
script_type_str == "SimpleScriptV1"
and reference_utxo.reference_script["script"]["type"] == "SimpleScriptV2"
):
pytest.xfail("Reported 'SimpleScriptV2', see node issue #4261")
dbsync_utils.check_tx(cluster_obj=cluster, tx_raw_output=tx_out_from)

# In node >= 1.36.0 it is not necessary to distinguish between MultiSig and Timelock
# scripts, both now report as "SimpleScript".
Expand All @@ -2301,6 +2293,13 @@ def test_script_reference_utxo(
"SimpleScript",
)

# Check expected script type
if (
script_type_str == "SimpleScriptV1"
and reference_utxo.reference_script["script"]["type"] == "SimpleScriptV2"
):
pytest.fail("Regression -> Reported 'SimpleScriptV2', see node issue #4261")

@allure.link(helpers.get_vcs_link())
@common.PARAM_USE_BUILD_CMD
@pytest.mark.parametrize("script_version", ("simple_v1", "simple_v2"))
Expand Down
41 changes: 40 additions & 1 deletion cardano_node_tests/utils/dbsync_utils.py
Expand Up @@ -4,6 +4,7 @@
import json
import logging
import time
from pathlib import Path
from typing import Any
from typing import Callable
from typing import Dict
Expand All @@ -17,6 +18,7 @@
from cardano_node_tests.utils import clusterlib_utils
from cardano_node_tests.utils import configuration
from cardano_node_tests.utils import dbsync_queries
from cardano_node_tests.utils import helpers

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -153,6 +155,7 @@ class TxRecord(NamedTuple):
collateral_outputs: List[clusterlib.UTXOData]
reference_inputs: List[clusterlib.UTXOData]
scripts: List[ScriptRecord]
script_txins: List[ScriptRecord]
redeemers: List[RedeemerRecord]
metadata: List[MetadataRecord]
reserve: List[ADAStashRecord]
Expand Down Expand Up @@ -555,6 +558,18 @@ def get_tx_record(txhash: str) -> TxRecord: # noqa: C901
for r in dbsync_queries.query_scripts(txhash=txhash)
]

script_txins = []
if reference_inputs:
for reference_input in reference_inputs:
script_txins += [
ScriptRecord(
hash=r.hash.hex(),
type=str(r.type),
serialised_size=int(r.serialised_size) if r.serialised_size else 0,
)
for r in dbsync_queries.query_scripts(txhash=reference_input.utxo_hash)
]

redeemers = []
if txdata.last_row.redeemer_count:
redeemers = [
Expand Down Expand Up @@ -598,6 +613,7 @@ def get_tx_record(txhash: str) -> TxRecord: # noqa: C901
collateral_outputs=collateral_outputs,
reference_inputs=reference_inputs,
scripts=scripts,
script_txins=script_txins,
redeemers=redeemers,
metadata=metadata,
reserve=reserve,
Expand Down Expand Up @@ -829,7 +845,7 @@ def _txout_has_inline_datum(txout: clusterlib.TxOut) -> bool:
return False


def check_tx(
def check_tx( # noqa: C901
cluster_obj: clusterlib.ClusterLib, tx_raw_output: clusterlib.TxRawOutput, retry_num: int = 3
) -> Optional[TxRecord]:
"""Check a transaction in db-sync."""
Expand Down Expand Up @@ -1060,6 +1076,29 @@ def check_tx(
f"({tx_raw_output.required_signer_hashes} != {db_required_signer_hashes})"
)

# Check reference scripts txins
txins_script_hashes = []

for r in tx_raw_output.script_txins:
if r.reference_txin and r.reference_txin.reference_script:
script_file = Path(f"{helpers.get_timestamped_rand_str()}.script")
with open(script_file, "w", encoding="utf-8") as outfile:
json.dump(r.reference_txin.reference_script["script"], outfile)

txins_script_hashes.append(
cluster_obj.g_transaction.get_policyid(script_file=script_file)
)

db_txins_script_hashes = {r.hash for r in response.script_txins if r.hash}

# A script is added to `script` table only the first time it is seen, so the record
# can be empty for the current transaction
if db_txins_script_hashes:
assert set(txins_script_hashes).issubset(db_txins_script_hashes), (
"Reference scripts txins don't match "
f"({set(txins_script_hashes)} != {db_txins_script_hashes})"
)

return response


Expand Down

0 comments on commit 69961cb

Please sign in to comment.