Skip to content

Commit

Permalink
Fix reference scripts fetched from ogmios (Python-Cardano#254)
Browse files Browse the repository at this point in the history
* Test transaction chaining

* Fix reference scripts fetched from ogmios

* Add tests

* Remove unused import

---------

Co-authored-by: Jerry <[email protected]>
  • Loading branch information
nielstron and cffls authored Sep 3, 2023
1 parent 0d5b3d7 commit 0a95536
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
60 changes: 60 additions & 0 deletions integration-test/test/test_plutus.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,63 @@ def test_plutus_v2_ref_script(self):
self.chain_context.submit_tx(signed_tx)

self.assert_output(taker_address, take_output)

@retry(tries=TEST_RETRIES, backoff=1.5, delay=6, jitter=(0, 4))
@pytest.mark.post_alonzo
def test_transaction_chaining(self):
giver_address = Address(self.payment_vkey.hash(), network=self.NETWORK)
builder = TransactionBuilder(self.chain_context)
builder.add_input_address(giver_address)
builder.add_output(TransactionOutput(giver_address, 50000000))
tx1 = builder.build_and_sign([self.payment_skey], giver_address)

utxo_to_spend = UTxO(
TransactionInput(tx1.id, 0), tx1.transaction_body.outputs[0]
)

builder = TransactionBuilder(self.chain_context)
builder.add_input(utxo_to_spend)
builder.add_output(TransactionOutput(giver_address, 25000000))
tx2 = builder.build_and_sign([self.payment_skey], giver_address)

self.chain_context.submit_tx(tx1)
self.chain_context.submit_tx(tx2)

@retry(tries=TEST_RETRIES, backoff=1.5, delay=6, jitter=(0, 4))
@pytest.mark.post_alonzo
def test_get_plutus_script(self):
# ----------- Giver give ---------------
with open("./plutus_scripts/fortytwoV2.plutus", "r") as f:
script_hex = f.read()
forty_two_script = PlutusV2Script(cbor2.loads(bytes.fromhex(script_hex)))

script_hash = plutus_script_hash(forty_two_script)

script_address = Address(script_hash, network=self.NETWORK)

giver_address = Address(self.payment_vkey.hash(), network=self.NETWORK)

builder = TransactionBuilder(self.chain_context)
builder.add_input_address(giver_address)
builder.add_output(
TransactionOutput(script_address, 50000000, script=forty_two_script)
)

signed_tx = builder.build_and_sign([self.payment_skey], giver_address)

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)
time.sleep(3)

utxos = self.chain_context.utxos(script_address)

assert utxos[0].output.script == forty_two_script


class TestPlutusOgmiosOnly(TestPlutus):
@classmethod
def setup_class(cls):
cls.chain_context._kupo_url = None
5 changes: 2 additions & 3 deletions pycardano/backend/ogmios.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from enum import Enum
from typing import Any, Dict, List, Optional, Tuple, Union

import cbor2
import requests
import websocket
from cachetools import Cache, LRUCache, TTLCache, func
Expand Down Expand Up @@ -466,9 +465,9 @@ def _utxo_from_ogmios_result(self, result) -> UTxO:
script = output.get("script", None)
if script:
if "plutus:v2" in script:
script = PlutusV2Script(cbor2.loads(bytes.fromhex(script["plutus:v2"])))
script = PlutusV2Script(bytes.fromhex(script["plutus:v2"]))
elif "plutus:v1" in script:
script = PlutusV1Script(cbor2.loads(bytes.fromhex(script["plutus:v1"])))
script = PlutusV1Script(bytes.fromhex(script["plutus:v1"]))
else:
raise ValueError("Unknown plutus script type")
datum_hash = (
Expand Down

0 comments on commit 0a95536

Please sign in to comment.