Skip to content

Commit

Permalink
Move ledger/node specifics to hydra-node (#1599)
Browse files Browse the repository at this point in the history
The `hydra-tx` module contained a lot of things that are not actually
required to build and test Hydra protocol transactions. This PR moves
several things that were only used by `hydra-node` back into that
package.
  • Loading branch information
ch1bo authored Sep 5, 2024
1 parent 1fdcb86 commit 49224c8
Show file tree
Hide file tree
Showing 42 changed files with 306 additions and 330 deletions.
3 changes: 3 additions & 0 deletions hydra-cardano-api/src/Hydra/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ import Cardano.Api as X hiding (
import Cardano.Api.Byron as X (
Address (..),
)
import Cardano.Api.Ledger as X (
PParams,
)
import Cardano.Api.Shelley as X (
AcquiringFailure (..),
Address (..),
Expand Down
1 change: 0 additions & 1 deletion hydra-cluster/hydra-cluster.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ library
, async
, base >=4.7 && <5
, bytestring
, cardano-api
, cardano-slotting
, containers
, contra-tracer
Expand Down
1 change: 0 additions & 1 deletion hydra-cluster/src/Hydra/Generator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Hydra.Generator where
import Hydra.Cardano.Api
import Hydra.Prelude hiding (size)

import Cardano.Api.Ledger (PParams)
import Cardano.Api.UTxO qualified as UTxO
import CardanoClient (QueryPoint (QueryTip), buildRawTransaction, buildTransaction, queryUTxOFor, sign)
import Control.Monad (foldM)
Expand Down
2 changes: 1 addition & 1 deletion hydra-cluster/test/Test/CardanoClientSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import CardanoNode (withCardanoNodeDevnet)
import Data.Aeson ((.:))
import Data.Aeson qualified as Aeson
import Hydra.Cardano.Api (GenesisParameters (..))
import Hydra.Ledger.Cardano.Configuration (readJsonFileThrow)
import Hydra.Logging (showLogsOnFailure)
import Hydra.Utils (readJsonFileThrow)
import System.FilePath ((</>))
import Test.EndToEndSpec (withClusterTempDir)

Expand Down
11 changes: 3 additions & 8 deletions hydra-cluster/test/Test/GeneratorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@ import Hydra.Generator (
Dataset (..),
generateConstantUTxODataset,
)
import Hydra.Ledger.Cardano (Tx, cardanoLedger)
import Hydra.Ledger.Cardano.Configuration (
Globals,
LedgerEnv,
newLedgerEnv,
)
import Hydra.Ledger.Ledger (applyTransactions)
import Hydra.Ledger (applyTransactions)
import Hydra.Ledger.Cardano (Globals, LedgerEnv, Tx, cardanoLedger, newLedgerEnv)
import Test.Aeson.GenericSpecs (roundtripSpecs)
import Test.Hydra.Tx.Fixture (defaultGlobals, defaultPParams)
import Test.Hydra.Node.Fixture (defaultGlobals, defaultPParams)
import Test.QuickCheck (
Positive (Positive),
Property,
Expand Down
5 changes: 2 additions & 3 deletions hydra-cluster/test/Test/Hydra/Cluster/CardanoCliSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import Data.Aeson (encodeFile)
import Data.Aeson.Lens (key, _String)
import Data.Aeson.Types (parseEither)
import Hydra.API.HTTPServer (DraftCommitTxResponse (DraftCommitTxResponse))
import Hydra.Cardano.Api (Tx)
import Hydra.Cardano.Api (LedgerEra, PParams, Tx)
import Hydra.JSONSchema (validateJSON, withJsonSpecifications)
import Hydra.Ledger.Cardano.Configuration (pparamsFromJson)
import Hydra.Logging (showLogsOnFailure)
import System.Exit (ExitCode (..))
import System.FilePath ((</>))
Expand Down Expand Up @@ -43,7 +42,7 @@ spec =
withTempDir "hydra-cluster" $ \tmpDir -> do
withCardanoNodeDevnet tracer tmpDir $ \RunningNode{nodeSocket, networkId} -> do
protocolParameters <- cliQueryProtocolParameters nodeSocket networkId
case parseEither pparamsFromJson protocolParameters of
case parseEither (parseJSON @(PParams LedgerEra)) protocolParameters of
Left e -> failure $ "Failed to decode JSON: " <> e <> "\n" <> show protocolParameters
Right _ -> pure ()

Expand Down
4 changes: 2 additions & 2 deletions hydra-node/bench/micro-bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import Hydra.Cardano.Api (
serialiseToCBOR,
)
import Hydra.Chain.ChainState (ChainSlot (ChainSlot))
import Hydra.Ledger (Ledger (applyTransactions), ValidationError)
import Hydra.Ledger.Cardano (Tx, cardanoLedger, genFixedSizeSequenceOfSimplePaymentTransactions)
import Hydra.Ledger.Ledger (Ledger (applyTransactions), ValidationError)
import Test.Hydra.Tx.Fixture (defaultGlobals, defaultLedgerEnv)
import Test.Hydra.Node.Fixture (defaultGlobals, defaultLedgerEnv)
import Test.QuickCheck (generate)

main :: IO ()
Expand Down
17 changes: 14 additions & 3 deletions hydra-node/hydra-node.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ library
Hydra.HeadLogic.Input
Hydra.HeadLogic.Outcome
Hydra.HeadLogic.State
Hydra.Ledger
Hydra.Ledger.Cardano
Hydra.Ledger.Simple
Hydra.Logging
Hydra.Logging.Messages
Hydra.Logging.Monitoring
Expand Down Expand Up @@ -143,6 +145,7 @@ library
, QuickCheck
, quickcheck-arbitrary-adt
, quickcheck-instances
, serialise
, stm
, text
, time
Expand All @@ -164,21 +167,29 @@ library testlib
import: project-config
visibility: public
hs-source-dirs: testlib
exposed-modules: Hydra.JSONSchema
exposed-modules:
Hydra.JSONSchema
Test.Hydra.Node.Fixture

other-modules: Paths_hydra_node
build-depends:
, aeson
, base
, cardano-ledger-core
, containers
, directory
, filepath
, hydra-cardano-api
, hydra-node
, hydra-prelude
, hydra-test-utils
, hydra-tx:testlib
, lens
, lens-aeson
, process
, QuickCheck
, text
, time
, versions
, yaml

Expand Down Expand Up @@ -246,9 +257,9 @@ benchmark micro
, base
, criterion
, hydra-cardano-api
, hydra-node
, hydra-node:{hydra-node, testlib}
, hydra-prelude
, hydra-tx:{hydra-tx, testlib}
, hydra-tx
, QuickCheck

ghc-options: -threaded -rtsopts
Expand Down
9 changes: 3 additions & 6 deletions hydra-node/src/Hydra/API/ServerOutput.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Hydra.API.ClientInput (ClientInput (..))
import Hydra.Chain (PostChainTx, PostTxError)
import Hydra.Chain.ChainState (IsChainState)
import Hydra.HeadLogic.State (HeadState)
import Hydra.Ledger.Ledger (ValidationError)
import Hydra.Ledger (ValidationError)
import Hydra.Network (Host, NodeId)
import Hydra.Prelude hiding (seq)
import Hydra.Tx (
Expand All @@ -26,8 +26,8 @@ import Hydra.Tx (
import Hydra.Tx qualified as Tx
import Hydra.Tx.ContestationPeriod (ContestationPeriod)
import Hydra.Tx.Crypto (MultiSignature)
import Hydra.Tx.IsTx (ArbitraryIsTx)
import Hydra.Tx.OnChainId (OnChainId)
import Test.Hydra.Tx.Gen (ArbitraryIsTx)

-- | The type of messages sent to clients by the 'Hydra.API.Server'.
data TimedServerOutput tx = TimedServerOutput
Expand Down Expand Up @@ -151,10 +151,7 @@ instance IsChainState tx => FromJSON (ServerOutput tx) where
{ omitNothingFields = True
}

instance
ArbitraryIsTx tx =>
Arbitrary (ServerOutput tx)
where
instance (ArbitraryIsTx tx, IsChainState tx) => Arbitrary (ServerOutput tx) where
arbitrary = genericArbitrary

-- NOTE: Somehow, can't use 'genericShrink' here as GHC is complaining about
Expand Down
6 changes: 3 additions & 3 deletions hydra-node/src/Hydra/Chain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import Hydra.Tx (
SnapshotVersion,
UTxOType,
)
import Hydra.Tx.IsTx (ArbitraryIsTx)
import Hydra.Tx.OnChainId (OnChainId)
import Test.Cardano.Ledger.Core.Arbitrary ()
import Test.Hydra.Tx.Gen (ArbitraryIsTx)
import Test.QuickCheck.Instances.Semigroup ()
import Test.QuickCheck.Instances.Time ()

Expand Down Expand Up @@ -177,7 +177,7 @@ deriving anyclass instance IsChainState tx => FromJSON (PostTxError tx)

instance IsChainState tx => Exception (PostTxError tx)

instance ArbitraryIsTx tx => Arbitrary (PostTxError tx) where
instance (ArbitraryIsTx tx, IsChainState tx) => Arbitrary (PostTxError tx) where
arbitrary = genericArbitrary

-- | A non empty sequence of chain states that can be rolled back.
Expand Down Expand Up @@ -271,7 +271,7 @@ deriving stock instance (IsTx tx, IsChainState tx) => Show (ChainEvent tx)
deriving anyclass instance (IsTx tx, IsChainState tx) => ToJSON (ChainEvent tx)
deriving anyclass instance (IsTx tx, IsChainState tx) => FromJSON (ChainEvent tx)

instance (ArbitraryIsTx tx, IsChainState tx, Arbitrary ChainSlot) => Arbitrary (ChainEvent tx) where
instance (ArbitraryIsTx tx, IsChainState tx) => Arbitrary (ChainEvent tx) where
arbitrary = genericArbitrary

-- | A callback indicating a 'ChainEvent tx' happened. Most importantly the
Expand Down
1 change: 0 additions & 1 deletion hydra-node/src/Hydra/Chain/CardanoClient.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Hydra.Prelude
import Hydra.Cardano.Api hiding (Block, queryCurrentEra)

import Cardano.Api.UTxO qualified as UTxO
import Cardano.Ledger.Core (PParams (..))
import Data.Aeson (eitherDecode', encode)
import Data.Set qualified as Set
import Data.Text qualified as Text
Expand Down
2 changes: 1 addition & 1 deletion hydra-node/src/Hydra/Chain/Offline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import Hydra.Chain (
)
import Hydra.Chain.ChainState (ChainSlot (ChainSlot))
import Hydra.Chain.Direct.State (initialChainState)
import Hydra.Ledger.Cardano.Configuration (readJsonFileThrow)
import Hydra.Ledger.Cardano.Time (slotNoFromUTCTime, slotNoToUTCTime)
import Hydra.Network (NodeId (nodeId))
import Hydra.Options (OfflineChainConfig (..), defaultContestationPeriod)
import Hydra.Tx (HeadId (..), HeadParameters (..), HeadSeed (..), Party)
import Hydra.Utils (readJsonFileThrow)

-- | Derived 'HeadId' of offline head.
offlineHeadId :: NodeId -> HeadId
Expand Down
4 changes: 3 additions & 1 deletion hydra-node/src/Hydra/Events.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE UndecidableInstances #-}

-- | This module defines the types and functions for creating 'EventSource' and
-- 'EventSink' instances and is intended to be used as an extension point.
--
Expand All @@ -15,7 +17,7 @@ import Hydra.Prelude

import Hydra.Chain.ChainState (IsChainState)
import Hydra.HeadLogic.Outcome (StateChanged)
import Test.Hydra.Tx.Gen (ArbitraryIsTx)
import Hydra.Tx.IsTx (ArbitraryIsTx)
import Test.QuickCheck.Arbitrary.ADT (ToADTArbitrary)

type EventId = Word64
Expand Down
2 changes: 1 addition & 1 deletion hydra-node/src/Hydra/HeadLogic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import Hydra.HeadLogic.State (
seenSnapshotNumber,
setChainState,
)
import Hydra.Ledger.Ledger (
import Hydra.Ledger (
Ledger (..),
applyTransactions,
outputsOfTx,
Expand Down
2 changes: 1 addition & 1 deletion hydra-node/src/Hydra/HeadLogic/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Hydra.Prelude

import Hydra.HeadLogic.Input (Input)
import Hydra.HeadLogic.State (HeadState)
import Hydra.Ledger.Ledger (ValidationError)
import Hydra.Ledger (ValidationError)
import Hydra.Tx (
HeadId,
IsTx (TxIdType),
Expand Down
4 changes: 3 additions & 1 deletion hydra-node/src/Hydra/HeadLogic/Input.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE UndecidableInstances #-}

module Hydra.HeadLogic.Input where

import Hydra.Prelude
Expand All @@ -6,7 +8,7 @@ import Hydra.API.ClientInput (ClientInput)
import Hydra.Chain (ChainEvent)
import Hydra.Chain.ChainState (IsChainState)
import Hydra.Network.Message (Message, NetworkEvent)
import Test.Hydra.Tx.Gen (ArbitraryIsTx)
import Hydra.Tx.IsTx (ArbitraryIsTx)

type TTL = Natural

Expand Down
8 changes: 4 additions & 4 deletions hydra-node/src/Hydra/HeadLogic/Outcome.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Hydra.Chain (PostChainTx)
import Hydra.Chain.ChainState (ChainSlot, ChainStateType, IsChainState)
import Hydra.HeadLogic.Error (LogicError)
import Hydra.HeadLogic.State (HeadState)
import Hydra.Ledger.Ledger (ValidationError)
import Hydra.Ledger (ValidationError)
import Hydra.Network.Message (Message)
import Hydra.Tx (
HeadId,
Expand All @@ -27,7 +27,7 @@ import Hydra.Tx (
)
import Hydra.Tx.Crypto (MultiSignature, Signature)
import Hydra.Tx.Environment (Environment (..))
import Test.Hydra.Tx.Gen (ArbitraryIsTx)
import Hydra.Tx.IsTx (ArbitraryIsTx)
import Test.QuickCheck (oneof)
import Test.QuickCheck.Arbitrary.ADT (ToADTArbitrary)

Expand All @@ -48,7 +48,7 @@ deriving stock instance IsChainState tx => Show (Effect tx)
deriving anyclass instance IsChainState tx => ToJSON (Effect tx)
deriving anyclass instance IsChainState tx => FromJSON (Effect tx)

instance (IsChainState tx, Test.Hydra.Tx.Gen.ArbitraryIsTx tx) => Arbitrary (Effect tx) where
instance (ArbitraryIsTx tx, IsChainState tx) => Arbitrary (Effect tx) where
arbitrary = genericArbitrary

-- | Head state changed event. These events represent all the internal state
Expand Down Expand Up @@ -104,7 +104,7 @@ instance (ArbitraryIsTx tx, IsChainState tx) => Arbitrary (StateChanged tx) wher

instance (ArbitraryIsTx tx, IsChainState tx) => ToADTArbitrary (StateChanged tx)

genStateChanged :: ArbitraryIsTx tx => Environment -> Gen (StateChanged tx)
genStateChanged :: (ArbitraryIsTx tx, IsChainState tx) => Environment -> Gen (StateChanged tx)
genStateChanged env =
oneof
[ HeadInitialized (mkHeadParameters env) <$> arbitrary <*> arbitrary <*> arbitrary
Expand Down
10 changes: 5 additions & 5 deletions hydra-node/src/Hydra/HeadLogic/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import Hydra.Tx (
Party,
)
import Hydra.Tx.Crypto (Signature)
import Hydra.Tx.IsTx (ArbitraryIsTx)
import Hydra.Tx.Snapshot (
ConfirmedSnapshot,
Snapshot (..),
SnapshotNumber,
SnapshotVersion,
)
import Test.Hydra.Tx.Gen (ArbitraryIsTx)

-- | The main state of the Hydra protocol state machine. It holds both, the
-- overall protocol state, but also the off-chain 'CoordinatedHeadState'.
Expand All @@ -44,7 +44,7 @@ data HeadState tx
| Closed (ClosedState tx)
deriving stock (Generic)

instance ArbitraryIsTx tx => Arbitrary (HeadState tx) where
instance (ArbitraryIsTx tx, Arbitrary (ChainStateType tx)) => Arbitrary (HeadState tx) where
arbitrary = genericArbitrary

deriving stock instance (IsTx tx, Eq (ChainStateType tx)) => Eq (HeadState tx)
Expand Down Expand Up @@ -100,7 +100,7 @@ deriving stock instance (IsTx tx, Show (ChainStateType tx)) => Show (InitialStat
deriving anyclass instance (IsTx tx, ToJSON (ChainStateType tx)) => ToJSON (InitialState tx)
deriving anyclass instance (IsTx tx, FromJSON (ChainStateType tx)) => FromJSON (InitialState tx)

instance ArbitraryIsTx tx => Arbitrary (InitialState tx) where
instance (ArbitraryIsTx tx, Arbitrary (ChainStateType tx)) => Arbitrary (InitialState tx) where
arbitrary = do
InitialState
<$> arbitrary
Expand Down Expand Up @@ -133,7 +133,7 @@ deriving stock instance (IsTx tx, Show (ChainStateType tx)) => Show (OpenState t
deriving anyclass instance (IsTx tx, ToJSON (ChainStateType tx)) => ToJSON (OpenState tx)
deriving anyclass instance (IsTx tx, FromJSON (ChainStateType tx)) => FromJSON (OpenState tx)

instance ArbitraryIsTx tx => Arbitrary (OpenState tx) where
instance (ArbitraryIsTx tx, Arbitrary (ChainStateType tx)) => Arbitrary (OpenState tx) where
arbitrary =
OpenState
<$> arbitrary
Expand Down Expand Up @@ -233,7 +233,7 @@ deriving stock instance (IsTx tx, Show (ChainStateType tx)) => Show (ClosedState
deriving anyclass instance (IsTx tx, ToJSON (ChainStateType tx)) => ToJSON (ClosedState tx)
deriving anyclass instance (IsTx tx, FromJSON (ChainStateType tx)) => FromJSON (ClosedState tx)

instance ArbitraryIsTx tx => Arbitrary (ClosedState tx) where
instance (ArbitraryIsTx tx, Arbitrary (ChainStateType tx)) => Arbitrary (ClosedState tx) where
arbitrary =
ClosedState
<$> arbitrary
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE TypeFamilyDependencies #-}

module Hydra.Ledger.Ledger where
module Hydra.Ledger where

import Hydra.Prelude

Expand Down
Loading

0 comments on commit 49224c8

Please sign in to comment.