Skip to content

Commit

Permalink
Merge branch 'dshuiski/refactor' into dshuiski/balancer-constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
errfrom committed Sep 5, 2024
2 parents 4328687 + a1cea53 commit c70d485
Show file tree
Hide file tree
Showing 13 changed files with 902 additions and 1,327 deletions.
271 changes: 175 additions & 96 deletions flake.lock

Large diffs are not rendered by default.

48 changes: 26 additions & 22 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@
flake = false;
};

cardano-node.url = "github:input-output-hk/cardano-node/4f4e372a1641ac68cd09fb0339e6f55bef1ab85d";
# The changes introduced in the PRs listed below have not yet been included
# in any cardano-node release. These updates are necessary to run
# cardano-testnet in the Conway era and be able to adjust max Lovelace
# supply.
# https://github.com/IntersectMBO/cardano-node/pull/5936
# https://github.com/IntersectMBO/cardano-node/pull/5960
cardano-node.url = "github:input-output-hk/cardano-node/d7abccd4e90c38ff5cd4d6a7839689d888332056";

# Repository with network parameters
# NOTE(bladyjoker): Cardano configurations (yaml/json) often change format and break, that's why we pin to a specific known version.
Expand Down Expand Up @@ -75,12 +81,9 @@
, ...
}@inputs:
let
supportedSystems = [
"x86_64-linux"
"x86_64-darwin"
"aarch64-linux"
"aarch64-darwin"
];
linuxSystems = [ "x86_64-linux" "aarch64-linux" ];
darwinSystems = [ "x86_64-darwin" "aarch64-darwin" ];
supportedSystems = linuxSystems ++ darwinSystems;

ogmiosVersion = "6.5.0";
kupoVersion = "2.9.0";
Expand Down Expand Up @@ -140,7 +143,7 @@
cp -rT ogmios $out
'';

psProjectFor = pkgs:
psProjectFor = pkgs: system:
let
projectName = "cardano-transaction-lib";
# `filterSource` will still trigger rebuilds with flakes, even if a
Expand All @@ -161,18 +164,19 @@
packageJson = ./package.json;
packageLock = ./package-lock.json;
shell = {
withRuntime = true;
withRuntime = system == "x86_64-linux";
shellHook = exportOgmiosFixtures;
packageLockOnly = true;
packages = with pkgs; [
arion
fd
psmisc
nixpkgs-fmt
nodePackages.eslint
nodePackages.prettier
blockfrost-backend-ryo
];
packages = with pkgs;
(if (builtins.elem system linuxSystems) then [ psmisc ] else [ ]) ++
[
arion
fd
nixpkgs-fmt
nodePackages.eslint
nodePackages.prettier
blockfrost-backend-ryo
];
};
};
exportOgmiosFixtures =
Expand Down Expand Up @@ -297,18 +301,18 @@
devShells = perSystem (system: {
# This is the default `devShell` and can be run without specifying
# it (i.e. `nix develop`)
default = (psProjectFor (nixpkgsFor system)).devShell;
default = (psProjectFor (nixpkgsFor system) system).devShell;
});

packages = perSystem (system:
(psProjectFor (nixpkgsFor system)).packages
(psProjectFor (nixpkgsFor system) system).packages
);

apps = perSystem (system:
let
pkgs = nixpkgsFor system;
in
(psProjectFor pkgs).apps // {
(psProjectFor pkgs system).apps // {
ctl-runtime = pkgs.launchCtlRuntime { };
ctl-runtime-blockfrost = pkgs.launchCtlRuntime { blockfrost.enable = true; };
default = self.apps.${system}.ctl-runtime;
Expand All @@ -326,7 +330,7 @@
checks = perSystem (system:
let
pkgs = nixpkgsFor system;
psProject = psProjectFor pkgs;
psProject = psProjectFor pkgs system;
in
psProject.checks
// {
Expand Down
104 changes: 104 additions & 0 deletions src/Internal/CardanoCli/QueryHandle.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
module Internal.CardanoCli.QueryHandle
( withCardanoCliCompletion
) where

import Prelude

import Cardano.Types.Address (Address)
import Cardano.Types.TransactionInput (TransactionInput)
import Cardano.Types.TransactionOutput (TransactionOutput)
import Cardano.Types.UtxoMap (UtxoMap)
import Control.Alt ((<|>))
import Control.Monad.Error.Class (try)
import Control.Monad.Except (ExceptT(ExceptT), runExceptT)
import Control.Monad.Reader (local)
import Ctl.Internal.CardanoCli
( CardanoCliTxOutInfo
, CardanoNodeInstance
, cardanoCliTxOutInfoToUtxo
, queryUtxosViaCardanoCli
) as CardanoCli
import Ctl.Internal.Contract.Monad (Contract, ContractEnv)
import Ctl.Internal.Service.Error (ClientError(ClientOtherError))
import Data.Bifunctor (bimap)
import Data.Either (Either)
import Data.Lens (Lens', (%~))
import Data.Lens.Record (prop)
import Data.Map as Map
import Data.Maybe (Maybe)
import Effect.Aff (Aff)
import Effect.Exception (Error)
import Effect.Exception (message) as Error
import Type.Proxy (Proxy(Proxy))

type UtxosAtQuery = Address -> Aff (Either ClientError UtxoMap)

type GetUtxoByOrefQuery =
TransactionInput -> Aff (Either ClientError (Maybe TransactionOutput))

utxosAtLens :: Lens' ContractEnv UtxosAtQuery
utxosAtLens =
prop (Proxy :: _ "handle")
<<< prop (Proxy :: _ "utxosAt")

getUtxoByOrefLens :: Lens' ContractEnv GetUtxoByOrefQuery
getUtxoByOrefLens =
prop (Proxy :: _ "handle")
<<< prop (Proxy :: _ "getUtxoByOref")

withCardanoCliCompletion
:: forall a
. CardanoCli.CardanoNodeInstance
-> Address
-> Contract a
-> Contract a
withCardanoCliCompletion node genesisAddr =
local $ (utxosAtLens %~ completeUtxosAt node) >>>
(getUtxoByOrefLens %~ completeGetUtxoByOref node genesisAddr)

-- | Complements the `utxosAt` result with utxos found via cardano-cli.
-- | In case of overlapping results, the utxos from the query layer are given
-- | preference.
-- |
-- | NOTE: It is assumed that utxos retrieved via cardano-cli do not include
-- | datum or reference scripts.
completeUtxosAt
:: CardanoCli.CardanoNodeInstance -> UtxosAtQuery -> UtxosAtQuery
completeUtxosAt node utxosAt address =
runExceptT do
queryLayerUtxos <- ExceptT $ utxosAt address
cardanoCliUtxos <- ExceptT $ queryUtxosViaCardanoCli node address
pure $ Map.union queryLayerUtxos cardanoCliUtxos

-- | Complements the `getUtxoByOref` search space with utxos found via
-- | cardano-cli. If no utxo is found in the initial search, the lookup will be
-- | performed using utxos from cardano-cli.
-- |
-- | NOTE: It is assumed that utxos retrieved via cardano-cli do not include
-- | datum or reference scripts.
completeGetUtxoByOref
:: CardanoCli.CardanoNodeInstance
-> Address
-> GetUtxoByOrefQuery
-> GetUtxoByOrefQuery
completeGetUtxoByOref node address getUtxoByOref oref =
runExceptT do
mbUtxo <- ExceptT $ getUtxoByOref oref
cardanoCliUtxos <- ExceptT $ queryUtxosViaCardanoCli node address
pure $ mbUtxo <|> Map.lookup oref cardanoCliUtxos

queryUtxosViaCardanoCli
:: CardanoCli.CardanoNodeInstance
-> Address
-> Aff (Either ClientError UtxoMap)
queryUtxosViaCardanoCli node address =
bimap toClientError toUtxoMap <$>
try (CardanoCli.queryUtxosViaCardanoCli node address)
where
toClientError :: Error -> ClientError
toClientError = ClientOtherError <<< Error.message

toUtxoMap :: Array CardanoCli.CardanoCliTxOutInfo -> UtxoMap
toUtxoMap =
Map.fromFoldable
<<< map (CardanoCli.cardanoCliTxOutInfoToUtxo address)
93 changes: 0 additions & 93 deletions src/Internal/CardanoCli/QueryHandler.purs

This file was deleted.

12 changes: 10 additions & 2 deletions src/Internal/Spawn.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Ctl.Internal.Spawn
, spawn
, exec
, stop
, stopProcessWithChildren
, waitForStop
, cleanupTmpDir
, cleanupOnSigint
Expand Down Expand Up @@ -176,6 +177,13 @@ stop (ManagedProcess _ child closedAVar) = do
isAlive <- AVar.isEmpty <$> AVar.status closedAVar
when isAlive $ liftEffect $ kill SIGINT child

stopProcessWithChildren :: ManagedProcess -> Aff Unit
stopProcessWithChildren managedProc@(ManagedProcess _ proc _) = do
void $ liftEffect $ Node.ChildProcess.execSync
("pkill -TERM -P " <> show (unwrap $ Node.ChildProcess.pid proc))
Node.ChildProcess.defaultExecSyncOptions
stop managedProc

-- | Waits until the process has cleanly stopped.
waitForStop :: ManagedProcess -> Aff Unit
waitForStop (ManagedProcess cmd _ closedAVar) = do
Expand All @@ -195,12 +203,12 @@ onSignal :: Signal -> Effect Unit -> Effect OnSignalRef
onSignal sig = onSignalImpl (Signal.toString sig)

-- | Just as onSignal, but Aff.
waitForSignal :: Signal -> Aff Unit
waitForSignal :: Signal -> Aff Signal
waitForSignal signal = makeAff \cont -> do
isCanceledRef <- Ref.new false
onSignalRef <- onSignal signal
$ Ref.read isCanceledRef
>>= flip unless (cont $ Right unit)
>>= flip unless (cont $ Right signal)
pure $ Canceler \err -> liftEffect do
Ref.write true isCanceledRef
removeOnSignal onSignalRef
Expand Down
Loading

0 comments on commit c70d485

Please sign in to comment.