Skip to content

Commit

Permalink
Cardano cli chain context (#274)
Browse files Browse the repository at this point in the history
* feat: add cardano-cli chain context

* fix: allow instances of str to submit_tx_cbor

* fix: cast to int for asset amount and check for None in get_min_utxo

* test: add test for cardano-cli chain context

* Black formatting

* Fix some QA issues

* refactor: use `--out-file /dev/stdout` to get utxo data as json

* fix: remove unused offline/online mode code

* fix: remove unused fraction parser method

* fix: add docker configuration to use cardano-cli in a Docker container and network args method to use custom networks

* test: add integration tests for cardano-cli

* test: fix cardano-node container name

* Add more integration tests for cardano cli context

---------

Co-authored-by: Hareem Adderley <[email protected]>
Co-authored-by: Niels Mündler <[email protected]>
Co-authored-by: Jerry <[email protected]>
  • Loading branch information
4 people authored Feb 6, 2024
1 parent 590c49a commit 54ec756
Show file tree
Hide file tree
Showing 11 changed files with 1,656 additions and 215 deletions.
2 changes: 2 additions & 0 deletions integration-test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
entrypoint: bash
environment:
NETWORK: "${NETWORK:-local-alonzo}"
CARDANO_NODE_SOCKET_PATH: "/ipc/node.socket"
command: /code/run_node.sh

networks:
Expand All @@ -21,6 +22,7 @@ services:

volumes:
- .:/code
- /tmp:/tmp
- node-db:/data/db
- node-ipc:/ipc
ports:
Expand Down
67 changes: 67 additions & 0 deletions integration-test/test/test_cardano_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os
from pathlib import Path

from pycardano import (
CardanoCliChainContext,
CardanoCliNetwork,
GenesisParameters,
Network,
ProtocolParameters,
)
from pycardano.backend.cardano_cli import DockerConfig


class TestCardanoCli:
network_env = os.getenv("NETWORK", "local-alonzo")
host_socket = os.getenv("DOCKER_HOST", None)
network_magic = os.getenv("NETWORK_MAGIC", 42)

configs_dir = Path(__file__).parent.parent / "configs"

chain_context = CardanoCliChainContext(
binary=Path("cardano-cli"),
socket=Path("/ipc/node.socket"),
config_file=configs_dir / network_env / "config.json",
network=CardanoCliNetwork.CUSTOM,
docker_config=DockerConfig(
container_name="integration-test_cardano-node_1",
host_socket=Path(host_socket) if host_socket else None,
),
network_magic_number=int(network_magic),
)

def test_protocol_param(self):
protocol_param = self.chain_context.protocol_param

assert protocol_param is not None
assert isinstance(protocol_param, ProtocolParameters)

cost_models = protocol_param.cost_models
for _, cost_model in cost_models.items():
assert "addInteger-cpu-arguments-intercept" in cost_model
assert "addInteger-cpu-arguments-slope" in cost_model

def test_genesis_param(self):
genesis_param = self.chain_context.genesis_param

assert genesis_param is not None
assert isinstance(genesis_param, GenesisParameters)

def test_network(self):
network = self.chain_context.network

assert network is not None
assert isinstance(network, Network)

def test_epoch(self):
epoch = self.chain_context.epoch

assert epoch is not None
assert isinstance(epoch, int)
assert epoch > 0

def test_last_block_slot(self):
last_block_slot = self.chain_context.last_block_slot

assert isinstance(last_block_slot, int)
assert last_block_slot > 0
22 changes: 22 additions & 0 deletions integration-test/test/test_plutus.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import collections
import time
from typing import Dict, Union

import cbor2
import pytest
Expand All @@ -7,6 +9,7 @@
from pycardano import *

from .base import TEST_RETRIES, TestBase
from .test_cardano_cli import TestCardanoCli


class TestPlutus(TestBase):
Expand Down Expand Up @@ -371,3 +374,22 @@ class TestPlutusOgmiosOnly(TestPlutus):
@classmethod
def setup_class(cls):
cls.chain_context._kupo_url = None


def evaluate_tx(tx: Transaction) -> Dict[str, ExecutionUnits]:
redeemers = tx.transaction_witness_set.redeemer
execution_units = {}

if redeemers:
for r in redeemers:
k = f"{r.tag.name.lower()}:{r.index}"
execution_units[k] = ExecutionUnits(1000000, 1000000000)

return execution_units


class TestPlutusCardanoCLI(TestPlutus):
@classmethod
def setup_class(cls):
cls.chain_context = TestCardanoCli.chain_context
cls.chain_context.evaluate_tx = evaluate_tx
483 changes: 269 additions & 214 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pycardano/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

from .base import *
from .blockfrost import *
from .cardano_cli import *
from .ogmios import *
2 changes: 1 addition & 1 deletion pycardano/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def submit_tx(self, tx: Union[Transaction, bytes, str]):
"""
if isinstance(tx, Transaction):
return self.submit_tx_cbor(tx.to_cbor())
elif isinstance(tx, bytes):
elif isinstance(tx, bytes) or isinstance(tx, str):
return self.submit_tx_cbor(tx)
else:
raise InvalidArgumentException(
Expand Down
Loading

0 comments on commit 54ec756

Please sign in to comment.