Skip to content

Commit

Permalink
use SIGINT handler in template / docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pb99u069 committed Nov 30, 2022
1 parent b50b9e7 commit 4034d01
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -38,6 +38,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Changed

- Running plutip servers attaches on SIGINT handlers and therefore node will not exit by default. ([#1231](https://github.com/Plutonomicon/cardano-transaction-lib/pull/1231)).

### Removed

### Fixed
Expand Down
4 changes: 4 additions & 0 deletions doc/plutip-testing.md
Expand Up @@ -183,3 +183,7 @@ let
Although stake keys serve no real purpose in plutip context, they allow to use base addresses, and thus allow to have the same code for plutip testing, in-browser tests and production.

Note that CTL re-distributes tADA from payment key-only ("enterprise") addresses to base addresses, which requires a few transactions before the test can be run. Plutip can currently handle only enterprise addreses (see [this issue](https://github.com/mlabs-haskell/plutip/issues/103)).

## custom SIGINT handlers

SIGINT does not exit the node process when running a plutip server. Instead, we use a custom handler listening on SIGINT that cancels the fiber. This has the same effect in the end: The process will exit when there are no more possible events, one of them being our custom `cleanupOnSigint` that gets attached and detached to every plutip cluster. We avoid exiting on SIGINT so that multiple plutip servers with different port configs can be run in parallel.
2 changes: 2 additions & 0 deletions src/Contract/Test/Utils.purs
Expand Up @@ -632,6 +632,8 @@ checkTxHasMetadata txHash =

foreign import exitCode :: Int -> Effect Unit

-- | attaches a custom handler on SIGINt to kill the fiber.
-- | see `doc/plutip-testing#custom-SIGINT-handlers`
interruptOnSignal :: forall a. Signal -> Fiber a -> Effect Unit
interruptOnSignal signal fiber = Process.onSignal signal do
launchAff_ do
Expand Down
8 changes: 4 additions & 4 deletions templates/ctl-scaffold/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion templates/ctl-scaffold/flake.nix
Expand Up @@ -10,7 +10,7 @@
type = "github";
owner = "Plutonomicon";
repo = "cardano-transaction-lib";
rev = "a01a9409af204df835e97150bcdefe543910f2f9";
rev = "3ffc2c7f6edce0d2b838a226eb3d8ed3a8a63484";
};
# To use the same version of `nixpkgs` as we do
nixpkgs.follows = "ctl/nixpkgs";
Expand Down
2 changes: 1 addition & 1 deletion templates/ctl-scaffold/packages.dhall
Expand Up @@ -367,7 +367,7 @@ let additions =
, "variant"
]
, repo = "https://github.com/Plutonomicon/cardano-transaction-lib.git"
, version = "a01a9409af204df835e97150bcdefe543910f2f9"
, version = "3ffc2c7f6edce0d2b838a226eb3d8ed3a8a63484"
}
}

Expand Down
1 change: 1 addition & 0 deletions templates/ctl-scaffold/spago.dhall
Expand Up @@ -9,6 +9,7 @@ You can edit this file as you like.
, "cardano-transaction-lib"
, "effect"
, "ordered-collections"
, "posix-types"
, "prelude"
, "uint"
]
Expand Down
25 changes: 14 additions & 11 deletions templates/ctl-scaffold/test/Main.purs
Expand Up @@ -5,24 +5,27 @@ module Test.Scaffold.Main (main) where
import Contract.Prelude

import Contract.Config as Contract.Config
import Contract.Monad as Contract.Monad
import Contract.Test.Plutip as Contract.Test.Plutip
import Contract.Test.Utils (exitCode, interruptOnSignal)
import Contract.Wallet as Contract.Wallet
import Data.BigInt as BigInt
import Data.Posix.Signal (Signal(SIGINT))
import Data.UInt as UInt
import Effect.Aff (cancelWith, effectCanceler, launchAff)
import Scaffold as Scaffold

main :: Effect Unit
main = Contract.Monad.launchAff_ $ do
let
distribution :: Contract.Test.Plutip.InitialUTxOs
distribution =
[ BigInt.fromInt 5_000_000
, BigInt.fromInt 2_000_000_000
]
Contract.Test.Plutip.runPlutipContract config distribution \alice ->
Contract.Wallet.withKeyWallet alice $ do
Scaffold.contract
main = interruptOnSignal SIGINT =<< launchAff do
flip cancelWith (effectCanceler (exitCode 1)) do
let
distribution :: Contract.Test.Plutip.InitialUTxOs
distribution =
[ BigInt.fromInt 5_000_000
, BigInt.fromInt 2_000_000_000
]
Contract.Test.Plutip.runPlutipContract config distribution \alice ->
Contract.Wallet.withKeyWallet alice $ do
Scaffold.contract

config :: Contract.Test.Plutip.PlutipConfig
config =
Expand Down

0 comments on commit 4034d01

Please sign in to comment.