Skip to content

Commit

Permalink
Introduce hydra-tx library (#1587)
Browse files Browse the repository at this point in the history
Introduces hydra-tx library.

* Introduces `hydra-tx:testlib` with Arbitrary instances and generators
namespaced away from the main library.
* Moves many things to `Hydra.Tx` module namespace.

Next:
* Move observation code to `Hydra.Tx`
* Standardise generators. Stop using a mix of generators and Arbitrary
* Remove `IsTx` constraints on `Hydra.Tx` types and decouple ledger code
from `Snapshot` types.
  • Loading branch information
locallycompact authored Sep 3, 2024
1 parent e3d9420 commit 0062556
Show file tree
Hide file tree
Showing 145 changed files with 3,275 additions and 2,262 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-nix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
- package: hydra-plutus
- package: hydra-tui
- package: hydra-node
- package: hydra-tx
- package: hydra-cluster
steps:
- name: 📥 Checkout repository
Expand Down
1 change: 1 addition & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ packages:
hydra-plutus
hydra-node
hydra-cluster
hydra-tx
hydra-tui
hydraw
hydra-chain-observer
Expand Down
2 changes: 1 addition & 1 deletion docs/benchmarks/profiling.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ The `collectCom` transaction utilizes the `vCommit` and `vHead` validator script
## Acquiring an executable script

You can achieve this using
[`prepareTxScripts`](/haddock/hydra-node/Hydra-Ledger-Cardano-Evaluate.html#v:prepareTxScripts).
[`prepareTxScripts`](/haddock/hydra-tx/Hydra-Ledger-Cardano-Evaluate.html#v:prepareTxScripts).
To acquire and save the fully applied scripts from the transaction onto disk, run:

```haskell
Expand Down
2 changes: 0 additions & 2 deletions docs/standalone/audit-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ The outcome of the review could also include, without being limited to:
* a validation that the Hydra plutus scripts validators do check the transaction constraints defined in the Hydra Head specification;
* a review and comment on the mutation-based tests applied to the Hydra plutus scripts and, in particular, any adversarial situation that would not be covered by them but should be.

See the documentation of our [Mutation-Based tests](/haddock/hydra-node/tests/Hydra-Chain-Direct-Contract-Mutation.html).

See [Common Weaknesses](https://plutus.readthedocs.io/en/latest/reference/writing-scripts/common-weaknesses/index.html) and [Vulnerabilities](https://github.com/Plutonomicon/plutonomicon/blob/main/vulnerabilities.md).

### Hydra node produces consistent transactions
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
"hydra-cluster"
"hydra-explorer"
"hydra-node"
"hydra-tx"
"hydra-plutus"
"hydra-plutus-extras"
"hydra-test-utils"
Expand Down
4 changes: 3 additions & 1 deletion hydra-chain-observer/hydra-chain-observer.cabal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cabal-version: 2.2
cabal-version: 3.0
name: hydra-chain-observer
version: 0.18.1
synopsis: Hydra Chain Observer
Expand Down Expand Up @@ -69,6 +69,7 @@ library
, hydra-node
, hydra-plutus
, hydra-prelude
, hydra-tx
, optparse-applicative
, ouroboros-network-protocols

Expand Down Expand Up @@ -96,6 +97,7 @@ test-suite tests
, hydra-node
, hydra-prelude
, hydra-test-utils
, hydra-tx:testlib
, QuickCheck

other-modules:
Expand Down
2 changes: 1 addition & 1 deletion hydra-chain-observer/src/Hydra/ChainObserver.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ import Hydra.Chain.Direct.Tx (
import Hydra.ChainObserver.Options (Options (..), hydraChainObserverOptions)
import Hydra.Contract (ScriptInfo)
import Hydra.Contract qualified as Contract
import Hydra.HeadId (HeadId (..))
import Hydra.Ledger.Cardano (adjustUTxO)
import Hydra.Logging (Tracer, Verbosity (..), traceWith, withTracer)
import Hydra.Tx.HeadId (HeadId (..))
import Options.Applicative (execParser)
import Ouroboros.Network.Protocol.ChainSync.Client (
ChainSyncClient (..),
Expand Down
2 changes: 1 addition & 1 deletion hydra-chain-observer/test/Hydra/ChainObserverSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module Hydra.ChainObserverSpec where
import Hydra.Prelude
import Test.Hydra.Prelude

import Hydra.Chain.Direct.Fixture (testNetworkId)
import Hydra.Chain.Direct.State (HasKnownUTxO (getKnownUTxO), genChainStateWithTx)
import Hydra.Chain.Direct.State qualified as Transition
import Hydra.Chain.Direct.Tx (HeadObservation (..))
import Hydra.ChainObserver (observeAll, observeTx)
import Hydra.Ledger.Cardano (genSequenceOfSimplePaymentTransactions)
import Test.Hydra.Tx.Fixture (testNetworkId)
import Test.QuickCheck (counterexample, forAll, forAllBlind, property, (=/=), (===))
import Test.QuickCheck.Property (checkCoverage)

Expand Down
30 changes: 23 additions & 7 deletions hydra-cluster/bench/Bench/EndToEnd.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,31 @@ import Hydra.Cluster.Scenarios (
EndToEndLog (..),
headIsInitializingWith,
)
import Hydra.ContestationPeriod (ContestationPeriod (UnsafeContestationPeriod))
import Hydra.Crypto (generateSigningKey)
import Hydra.Generator (ClientDataset (..), ClientKeys (..), Dataset (..))
import Hydra.HeadId (HeadId)
import Hydra.Ledger (txId)
import Hydra.Logging (Tracer, traceWith, withTracerOutputTo)
import Hydra.Logging (
Tracer,
traceWith,
withTracerOutputTo,
)
import Hydra.Network (Host)
import Hydra.Party (Party, deriveParty)
import HydraNode (HydraClient, HydraNodeLog, hydraNodeId, input, output, requestCommitTx, send, waitFor, waitForAllMatch, waitForNodesConnected, waitMatch, withConnectionToNodeHost, withHydraCluster)
import Hydra.Tx (HeadId, Party, deriveParty, txId)
import Hydra.Tx.ContestationPeriod (ContestationPeriod (UnsafeContestationPeriod))
import Hydra.Tx.Crypto (generateSigningKey)
import HydraNode (
HydraClient,
HydraNodeLog,
hydraNodeId,
input,
output,
requestCommitTx,
send,
waitFor,
waitForAllMatch,
waitForNodesConnected,
waitMatch,
withConnectionToNodeHost,
withHydraCluster,
)
import System.Directory (findExecutable)
import System.FilePath ((</>))
import System.IO (hGetLine, hPutStrLn)
Expand Down
5 changes: 4 additions & 1 deletion hydra-cluster/hydra-cluster.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ library
build-depends:
, aeson
, async
, base >=4.7 && <5
, base >=4.7 && <5
, bytestring
, cardano-api
, cardano-slotting
Expand All @@ -99,6 +99,7 @@ library
, hydra-node
, hydra-prelude
, hydra-test-utils
, hydra-tx:{hydra-tx, testlib}
, io-classes
, iohk-monitoring
, lens
Expand Down Expand Up @@ -173,6 +174,7 @@ test-suite tests
, hydra-node:{hydra-node, testlib}
, hydra-prelude
, hydra-test-utils
, hydra-tx:{hydra-tx, testlib}
, io-classes
, lens
, lens-aeson
Expand Down Expand Up @@ -212,6 +214,7 @@ benchmark bench-e2e
, hydra-node
, hydra-prelude
, hydra-test-utils
, hydra-tx
, io-classes
, lens
, lens-aeson
Expand Down
4 changes: 2 additions & 2 deletions hydra-cluster/src/Hydra/Cluster/Faucet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import Control.Monad.Class.MonadThrow (Handler (Handler), catches)
import Control.Tracer (Tracer, traceWith)
import GHC.IO.Exception (IOErrorType (ResourceExhausted), IOException (ioe_type))
import Hydra.Chain.CardanoClient (queryProtocolParameters)
import Hydra.Chain.Direct.ScriptRegistry (
import Hydra.Chain.ScriptRegistry (
publishHydraScripts,
)
import Hydra.Cluster.Fixture (Actor (Faucet))
import Hydra.Cluster.Util (keysFor)
import Hydra.Ledger (balance)
import Hydra.Ledger.Cardano ()
import Hydra.Tx (balance)

data FaucetException
= FaucetHasNotEnoughFunds {faucetUTxO :: UTxO}
Expand Down
6 changes: 3 additions & 3 deletions hydra-cluster/src/Hydra/Cluster/Fixture.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import Hydra.Prelude

import Hydra.Cardano.Api (NetworkId)
import Hydra.Cardano.Api qualified as Api
import Hydra.ContestationPeriod (ContestationPeriod (..))
import Hydra.Crypto (HydraKey, SigningKey, VerificationKey, generateSigningKey, getVerificationKey)
import Hydra.Party (Party, deriveParty)
import Hydra.Tx (Party, deriveParty)
import Hydra.Tx.ContestationPeriod (ContestationPeriod (..))
import Hydra.Tx.Crypto (HydraKey, SigningKey, VerificationKey, generateSigningKey, getVerificationKey)

alice, bob, carol :: Party
alice = deriveParty aliceSk
Expand Down
11 changes: 5 additions & 6 deletions hydra-cluster/src/Hydra/Cluster/Scenarios.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,18 @@ import Hydra.Cardano.Api (
pattern TxOut,
pattern TxOutDatumNone,
)
import Hydra.Chain.Direct.Tx (verificationKeyToOnChainId)
import Hydra.Cluster.Faucet (FaucetLog, seedFromFaucet, seedFromFaucet_)
import Hydra.Cluster.Faucet qualified as Faucet
import Hydra.Cluster.Fixture (Actor (..), actorName, alice, aliceSk, aliceVk, bob, bobSk, bobVk, carol, carolSk)
import Hydra.Cluster.Mithril (MithrilLog)
import Hydra.Cluster.Options (Options)
import Hydra.Cluster.Util (chainConfigFor, keysFor, modifyConfig, setNetworkId)
import Hydra.ContestationPeriod (ContestationPeriod (UnsafeContestationPeriod), fromNominalDiffTime)
import Hydra.HeadId (HeadId)
import Hydra.Ledger (IsTx (balance), txId)
import Hydra.Ledger.Cardano (genKeyPair, mkSimpleTx)
import Hydra.Ledger.Cardano (mkSimpleTx)
import Hydra.Logging (Tracer, traceWith)
import Hydra.Options (DirectChainConfig (..), networkId, startChainFrom)
import Hydra.Party (Party)
import Hydra.Tx (HeadId, IsTx (balance), Party, txId)
import Hydra.Tx.ContestationPeriod (ContestationPeriod (UnsafeContestationPeriod), fromNominalDiffTime)
import Hydra.Tx.Utils (verificationKeyToOnChainId)
import HydraNode (
HydraClient (..),
HydraNodeLog,
Expand Down Expand Up @@ -101,6 +99,7 @@ import Network.HTTP.Req (
)
import System.Directory (removeDirectoryRecursive)
import System.FilePath ((</>))
import Test.Hydra.Tx.Gen (genKeyPair)
import Test.QuickCheck (choose, elements, generate)

data EndToEndLog
Expand Down
4 changes: 2 additions & 2 deletions hydra-cluster/src/Hydra/Cluster/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import Hydra.Cardano.Api (
textEnvelopeToJSON,
)
import Hydra.Cluster.Fixture (Actor, actorName, fundsOf)
import Hydra.ContestationPeriod (ContestationPeriod)
import Hydra.Ledger.Cardano (genSigningKey)
import Hydra.Options (ChainConfig (..), DirectChainConfig (..), defaultDirectChainConfig)
import Hydra.Tx.ContestationPeriod (ContestationPeriod)
import Paths_hydra_cluster qualified as Pkg
import System.FilePath ((<.>), (</>))
import Test.Hydra.Prelude (failure)
import Test.Hydra.Tx.Gen (genSigningKey)
import Test.QuickCheck (generate)

-- | Lookup a config file similar reading a file from disk.
Expand Down
10 changes: 8 additions & 2 deletions hydra-cluster/src/Hydra/Generator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ import Data.Default (def)
import Hydra.Cluster.Faucet (FaucetException (..))
import Hydra.Cluster.Fixture (Actor (..), availableInitialFunds)
import Hydra.Cluster.Util (keysFor)
import Hydra.Ledger (balance)
import Hydra.Ledger.Cardano (genSigningKey, generateOneRandomTransfer, generateOneSelfTransfer)
import Hydra.Ledger.Cardano (
generateOneRandomTransfer,
generateOneSelfTransfer,
)
import Hydra.Tx (balance)
import Test.Hydra.Tx.Gen (
genSigningKey,
)
import Test.QuickCheck (choose, generate, sized)

networkId :: NetworkId
Expand Down
4 changes: 2 additions & 2 deletions hydra-cluster/src/HydraNode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import Data.Text (pack)
import Data.Text qualified as T
import Hydra.API.HTTPServer (DraftCommitTxRequest (..), DraftCommitTxResponse (..))
import Hydra.Cluster.Util (readConfigFile)
import Hydra.ContestationPeriod (ContestationPeriod)
import Hydra.Crypto (HydraKey)
import Hydra.Logging (Tracer, Verbosity (..), traceWith)
import Hydra.Network (Host (Host), NodeId (NodeId))
import Hydra.Network qualified as Network
import Hydra.Options (ChainConfig (..), DirectChainConfig (..), LedgerConfig (..), RunOptions (..), defaultDirectChainConfig, toArgs)
import Hydra.Tx.ContestationPeriod (ContestationPeriod)
import Hydra.Tx.Crypto (HydraKey)
import Network.HTTP.Conduit (parseUrlThrow)
import Network.HTTP.Req (GET (..), HttpException, JsonResponse, NoReqBody (..), POST (..), ReqBodyJson (..), defaultHttpConfig, responseBody, runReq, (/:))
import Network.HTTP.Req qualified as Req
Expand Down
8 changes: 5 additions & 3 deletions hydra-cluster/test/Test/ChainObserverSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ import Data.List qualified as List
import Data.Text qualified as T
import Hydra.Cardano.Api (NetworkId (..), NetworkMagic (..), lovelaceToValue, mkVkAddress, signTx, unFile)
import Hydra.Cluster.Faucet (FaucetLog, publishHydraScriptsAs, seedFromFaucet, seedFromFaucet_)
import Hydra.Cluster.Fixture (Actor (..), aliceSk, cperiod)
import Hydra.Cluster.Fixture (Actor (..))
import Hydra.Cluster.Util (chainConfigFor, keysFor)
import Hydra.Ledger (txId)
import Hydra.Ledger.Cardano (genKeyPair, mkSimpleTx)
import Hydra.Ledger.Cardano (mkSimpleTx)
import Hydra.Logging (showLogsOnFailure)
import Hydra.Tx.IsTx (txId)
import HydraNode (HydraNodeLog, input, output, requestCommitTx, send, waitFor, waitMatch, withHydraNode)
import System.IO.Error (isEOFError, isIllegalOperation)
import System.Process (CreateProcess (std_out), StdStream (..), proc, withCreateProcess)
import Test.Hydra.Tx.Fixture (aliceSk, cperiod)
import Test.Hydra.Tx.Gen (genKeyPair)
import Test.QuickCheck (generate)

spec :: Spec
Expand Down
28 changes: 16 additions & 12 deletions hydra-cluster/test/Test/DirectChainSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ import Hydra.Cardano.Api (
import Hydra.Chain (
Chain (Chain, draftCommitTx, postTx),
ChainEvent (..),
CommitBlueprintTx (..),
HeadParameters (..),
OnChainTx (..),
PostChainTx (..),
PostTxError (..),
Expand All @@ -53,9 +51,8 @@ import Hydra.Chain.Direct (
withDirectChain,
)
import Hydra.Chain.Direct.Handlers (DirectChainLog)
import Hydra.Chain.Direct.ScriptRegistry (queryScriptRegistry)
import Hydra.Chain.Direct.State (initialChainState, splitUTxO)
import Hydra.Chain.Direct.Tx (verificationKeyToOnChainId)
import Hydra.Chain.Direct.State (initialChainState)
import Hydra.Chain.ScriptRegistry (queryScriptRegistry)
import Hydra.Cluster.Faucet (
FaucetLog,
publishHydraScriptsAs,
Expand All @@ -71,21 +68,28 @@ import Hydra.Cluster.Fixture (
cperiod,
)
import Hydra.Cluster.Util (chainConfigFor, keysFor, modifyConfig, readConfigFile)
import Hydra.Crypto (aggregate, sign)
import Hydra.HeadId (HeadId, HeadSeed (..))
import Hydra.Ledger (IsTx (..))
import Hydra.Ledger.Cardano (Tx, genKeyPair)
import Hydra.Ledger.Cardano (Tx)
import Hydra.Logging (Tracer, nullTracer, showLogsOnFailure)
import Hydra.OnChainId (OnChainId)
import Hydra.Options (
ChainConfig (..),
DirectChainConfig (..),
toArgNetworkId,
)
import Hydra.Party (Party)
import Hydra.Snapshot (ConfirmedSnapshot (..), Snapshot (..))
import Hydra.Tx.BlueprintTx (CommitBlueprintTx (..))
import Hydra.Tx.Crypto (aggregate, sign)
import Hydra.Tx.HeadId (HeadId, HeadSeed (..))
import Hydra.Tx.HeadParameters (HeadParameters (..))
import Hydra.Tx.IsTx (IsTx (..))
import Hydra.Tx.OnChainId (OnChainId)
import Hydra.Tx.Party (Party)
import Hydra.Tx.Snapshot (ConfirmedSnapshot (..), Snapshot (..))
import Hydra.Tx.Utils (
splitUTxO,
verificationKeyToOnChainId,
)
import System.FilePath ((</>))
import System.Process (proc, readCreateProcess)
import Test.Hydra.Tx.Gen (genKeyPair)
import Test.QuickCheck (choose, generate)

spec :: Spec
Expand Down
9 changes: 5 additions & 4 deletions hydra-cluster/test/Test/EndToEndSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import Data.Set qualified as Set
import Data.Text qualified as Text
import Data.Time (secondsToDiffTime)
import Hydra.Cardano.Api hiding (Value, cardanoEra, queryGenesisParameters)
import Hydra.Chain.Direct.Fixture (testNetworkId)
import Hydra.Chain.Direct.State ()
import Hydra.Cluster.Faucet (
publishHydraScriptsAs,
Expand Down Expand Up @@ -77,11 +76,11 @@ import Hydra.Cluster.Scenarios (
threeNodesNoErrorsOnOpen,
)
import Hydra.Cluster.Util (chainConfigFor, keysFor, modifyConfig)
import Hydra.ContestationPeriod (ContestationPeriod (UnsafeContestationPeriod))
import Hydra.Ledger (txId)
import Hydra.Ledger.Cardano (genKeyPair, genUTxOFor, mkRangedTx, mkSimpleTx)
import Hydra.Ledger.Cardano (mkRangedTx, mkSimpleTx)
import Hydra.Logging (Tracer, showLogsOnFailure)
import Hydra.Options
import Hydra.Tx.ContestationPeriod (ContestationPeriod (UnsafeContestationPeriod))
import Hydra.Tx.IsTx (txId)
import HydraNode (
HydraClient (..),
getMetrics,
Expand All @@ -107,6 +106,8 @@ import System.IO (
)
import System.IO.Error (isEOFError)
import System.Process (waitForProcess)
import Test.Hydra.Tx.Fixture (testNetworkId)
import Test.Hydra.Tx.Gen (genKeyPair, genUTxOFor)
import Test.QuickCheck (generate)
import Prelude qualified

Expand Down
Loading

0 comments on commit 0062556

Please sign in to comment.