Skip to content

Commit

Permalink
Fix/disallow attach inline datum (Python-Cardano#325)
Browse files Browse the repository at this point in the history
* Raise an error when trying to supply a datum when inline datums are
present

* Add a test to check the inline datum issue

* Allow datum when datum hash was attached to the target utxo

* Fix linter

* Always keep datum_hash in TransactionOutput if the datum isn't inline

---------

Co-authored-by: Jerry <jerrycgh@gmail.com>
  • Loading branch information
nielstron and cffls authored Mar 4, 2024
1 parent 7db66cf commit 0fec24a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 0 additions & 2 deletions pycardano/backend/ogmios.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,6 @@ def _utxos_kupo(self, address: str) -> List[UTxO]:
)
if datum_hash and result.get("datum_type", "inline"):
datum = self._get_datum_from_kupo(result["datum_hash"])
if datum is not None:
datum_hash = None

if not result["value"]["assets"]:
tx_out = TransactionOutput(
Expand Down
9 changes: 9 additions & 0 deletions pycardano/txbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ def add_script_input(
f"Datum hash in transaction output is {utxo.output.datum_hash}, "
f"but actual datum hash from input datum is {datum_hash(datum)}."
)
if (
datum is not None
and utxo.output.datum_hash is None
and utxo.output.datum is not None
):
raise InvalidArgumentException(
f"Inline Datum found in transaction output {utxo.input}, "
"so attaching a Datum to the transaction input manually is not allowed."
)

if datum is not None:
self.datums[datum_hash(datum)] = datum
Expand Down
24 changes: 24 additions & 0 deletions test/pycardano/test_txbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,30 @@ def test_estimate_execution_unit(chain_context):
assert [plutus_script] == witness.plutus_v1_script


def test_add_script_input_inline_datum_extra(chain_context):
tx_builder = TransactionBuilder(chain_context)
tx_in1 = TransactionInput.from_primitive(
["18cbe6cadecd3f89b60e08e68e5e6c7d72d730aaa1ad21431590f7e6643438ef", 0]
)
tx_in2 = TransactionInput.from_primitive(
["18cbe6cadecd3f89b60e08e68e5e6c7d72d730aaa1ad21431590f7e6643438ef", 1]
)
plutus_script = PlutusV1Script(b"dummy test script")
script_hash = plutus_script_hash(plutus_script)
script_address = Address(script_hash)
datum = PlutusData()
utxo1 = UTxO(tx_in1, TransactionOutput(script_address, 10000000, datum=datum))
redeemer1 = Redeemer(PlutusData(), ExecutionUnits(1000000, 1000000))
pytest.raises(
InvalidArgumentException,
tx_builder.add_script_input,
utxo1,
plutus_script,
datum,
redeemer1,
)


def test_tx_builder_exact_fee_no_change(chain_context):
tx_builder = TransactionBuilder(chain_context)
sender = "addr_test1vrm9x2zsux7va6w892g38tvchnzahvcd9tykqf3ygnmwtaqyfg52x"
Expand Down

0 comments on commit 0fec24a

Please sign in to comment.