Skip to content

Commit

Permalink
Move Params and TimeSlot modules to cardano-node-emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
ak3n committed Nov 30, 2022
1 parent 13e4027 commit 9e707c0
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 61 deletions.
8 changes: 8 additions & 0 deletions cardano-node-emulator/cardano-node-emulator.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ library
Cardano.Node.Emulator.Effects
Cardano.Node.Emulator.Fee
Cardano.Node.Emulator.Generators
Cardano.Node.Emulator.Params
Cardano.Node.Emulator.TimeSlot
Cardano.Node.Emulator.Validation

--------------------
Expand All @@ -55,6 +57,8 @@ library
, cardano-ledger-shelley
, cardano-ledger-shelley-ma
, cardano-slotting
, ouroboros-consensus
, plutus-core >=1.0.0
, plutus-ledger-api >=1.0.0
, plutus-tx >=1.0.0

Expand All @@ -68,14 +72,18 @@ library
, bytestring
, containers
, data-default
, deepseq
, either
, freer-simple
, hedgehog
, lens
, mtl
, openapi3
, prettyprinter >=1.1.0.1
, serialise
, strict-containers
, text
, time

test-suite cardano-node-emulator-test
import: lang
Expand Down
2 changes: 2 additions & 0 deletions cardano-node-emulator/src/Cardano/Node/Emulator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ import Cardano.Node.Emulator.Chain as Export
import Cardano.Node.Emulator.Effects as Export
import Cardano.Node.Emulator.Fee as Export
import Cardano.Node.Emulator.Generators as Export
import Cardano.Node.Emulator.Params as Export
import Cardano.Node.Emulator.TimeSlot as Export
import Cardano.Node.Emulator.Validation as Export
11 changes: 6 additions & 5 deletions cardano-node-emulator/src/Cardano/Node/Emulator/Chain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

module Cardano.Node.Emulator.Chain where

import Cardano.Node.Emulator.Params (Params (..))
import Cardano.Node.Emulator.Validation qualified as Validation
import Control.Lens hiding (index)
import Control.Monad.Freer
Expand All @@ -33,7 +34,7 @@ import Data.Monoid (Ap (Ap))
import Data.Text (Text)
import Data.Traversable (for)
import GHC.Generics (Generic)
import Ledger (Block, Blockchain, CardanoTx (..), OnChainTx (..), Params (..), Slot (..), TxId, TxIn (txInRef), Value,
import Ledger (Block, Blockchain, CardanoTx (..), OnChainTx (..), Slot (..), TxId, TxIn (txInRef), Value,
getCardanoTxCollateralInputs, getCardanoTxFee, getCardanoTxId, getCardanoTxTotalCollateral,
getCardanoTxValidityRange, txOutValue, unOnChain)
import Ledger.Index qualified as Index
Expand Down Expand Up @@ -64,7 +65,7 @@ data ChainState = ChainState {
_chainNewestFirst :: Blockchain, -- ^ The current chain, with the newest transactions first in the list.
_txPool :: TxPool, -- ^ The pool of pending transactions.
_index :: Index.UtxoIndex, -- ^ The UTxO index, used for validation.
_currentSlot :: Slot -- ^ The current slot number
_chainCurrentSlot :: Slot -- ^ The current slot number
} deriving (Show, Generic)

emptyChainState :: ChainState
Expand Down Expand Up @@ -104,7 +105,7 @@ handleControlChain :: Members ChainEffs effs => Params -> ChainControlEffect ~>
handleControlChain params = \case
ProcessBlock -> do
pool <- gets $ view txPool
slot <- gets $ view currentSlot
slot <- gets $ view chainCurrentSlot
idx <- gets $ view index

let ValidatedBlock block events idx' =
Expand All @@ -117,7 +118,7 @@ handleControlChain params = \case
traverse_ logEvent events
pure block

ModifySlot f -> modify @ChainState (over currentSlot f) >> gets (view currentSlot)
ModifySlot f -> modify @ChainState (over chainCurrentSlot f) >> gets (view chainCurrentSlot)

logEvent :: Member (LogMsg ChainEvent) effs => ChainEvent -> Eff effs ()
logEvent e = case e of
Expand All @@ -128,7 +129,7 @@ logEvent e = case e of
handleChain :: (Members ChainEffs effs) => Params -> ChainEffect ~> Eff effs
handleChain params = \case
QueueTx tx -> modify $ over txPool (addTxToPool tx)
GetCurrentSlot -> gets _currentSlot
GetCurrentSlot -> gets _chainCurrentSlot
GetParams -> pure params

-- | The result of validating a block.
Expand Down
3 changes: 2 additions & 1 deletion cardano-node-emulator/src/Cardano/Node/Emulator/Effects.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ module Cardano.Node.Emulator.Effects(
, getClientParams
) where

import Cardano.Node.Emulator.Params (Params)
import Control.Monad.Freer.TH (makeEffect)
import Ledger (CardanoTx, Params, Slot)
import Ledger (CardanoTx, Slot)

data NodeClientEffect r where
PublishTx :: CardanoTx -> NodeClientEffect ()
Expand Down
6 changes: 3 additions & 3 deletions cardano-node-emulator/src/Cardano/Node/Emulator/Fee.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import Cardano.Api.Shelley qualified as C.Api
import Cardano.Ledger.BaseTypes (Globals (systemStart))
import Cardano.Ledger.Core qualified as C.Ledger (Tx)
import Cardano.Ledger.Shelley.API qualified as C.Ledger hiding (Tx)
import Cardano.Node.Emulator.Params (EmulatorEra, PParams, Params (emulatorPParams, pNetworkId), emulatorEraHistory,
emulatorGlobals, pProtocolParams)
import Cardano.Node.Emulator.Validation (CardanoLedgerError, UTxO (..), makeTransactionBody)
import Data.Bifunctor (bimap, first)
import Data.Map qualified as Map
import Ledger.Ada (lovelaceValueOf)
import Ledger.Address (Address, PaymentPubKeyHash)
import Ledger.Params (EmulatorEra, PParams, Params (emulatorPParams, pNetworkId), emulatorEraHistory, emulatorGlobals,
pProtocolParams)
import Ledger.Tx (ToCardanoError (TxBodyError), Tx)
import Ledger.Tx.CardanoAPI (CardanoBuildTx (..), getCardanoBuildTx, toCardanoAddressInEra, toCardanoTxBodyContent)
import Ledger.Value (Value)
Expand All @@ -28,7 +28,7 @@ estimateTransactionFee
-> Tx
-> Either CardanoLedgerError Value
estimateTransactionFee params utxo requiredSigners tx = do
txBodyContent <- first Right $ toCardanoTxBodyContent params requiredSigners tx
txBodyContent <- first Right $ toCardanoTxBodyContent (pNetworkId params) (emulatorPParams params) requiredSigners tx
let nkeys = C.Api.estimateTransactionKeyWitnessCount (getCardanoBuildTx txBodyContent)
txBody <- makeTransactionBody params utxo txBodyContent
case evaluateTransactionFee (emulatorPParams params) txBody nkeys of
Expand Down
6 changes: 3 additions & 3 deletions cardano-node-emulator/src/Cardano/Node/Emulator/Generators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ import Hedgehog.Range qualified as Range
import Cardano.Api qualified as C
import Cardano.Api.Shelley (ProtocolParameters (..))
import Cardano.Crypto.Wallet qualified as Crypto
import Cardano.Node.Emulator.Params (Params (pSlotConfig))
import Cardano.Node.Emulator.TimeSlot (SlotConfig)
import Cardano.Node.Emulator.TimeSlot qualified as TimeSlot
import Cardano.Node.Emulator.Validation (fromPlutusIndex, fromPlutusTxSigned, validateCardanoTx)
import Ledger (Ada, AssetClass, CardanoTx (EmulatorTx), CurrencySymbol, Datum, Interval, Language (PlutusV1),
POSIXTime (POSIXTime, getPOSIXTime), POSIXTimeRange, Passphrase (Passphrase),
Expand All @@ -94,9 +97,6 @@ import Ledger (Ada, AssetClass, CardanoTx (EmulatorTx), CurrencySymbol, Datum, I
import Ledger.Ada qualified as Ada
import Ledger.CardanoWallet qualified as CW
import Ledger.Index.Internal qualified as Index (UtxoIndex (UtxoIndex))
import Ledger.Params (Params (pSlotConfig))
import Ledger.TimeSlot (SlotConfig)
import Ledger.TimeSlot qualified as TimeSlot
import Ledger.Tx qualified as Tx
import Ledger.Value qualified as Value
import Numeric.Natural (Natural)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -Wno-orphans #-}
-- | The set of parameters, like protocol parameters and slot configuration.
module Ledger.Params(
module Cardano.Node.Emulator.Params(
Params(..),
paramsWithProtocolsParameters,
slotConfigL,
Expand Down Expand Up @@ -39,6 +39,7 @@ import Cardano.Ledger.Crypto (StandardCrypto)
import Cardano.Ledger.Shelley.API (Coin (..), Globals, ShelleyGenesis (..), mkShelleyGlobals)
import Cardano.Ledger.Shelley.API qualified as C.Ledger
import Cardano.Ledger.Slot (EpochSize (..))
import Cardano.Node.Emulator.TimeSlot (SlotConfig (..), posixTimeToNominalDiffTime, posixTimeToUTCTime)
import Cardano.Slotting.EpochInfo (fixedEpochInfo)
import Cardano.Slotting.Time (SlotLength, mkSlotLength)
import Control.Lens (Lens', lens, makeLensesFor, over, (&), (.~))
Expand All @@ -50,7 +51,6 @@ import Data.Maybe (fromMaybe)
import Data.Ratio ((%))
import Data.SOP.Strict (K (K), NP (..))
import GHC.Generics (Generic)
import Ledger.TimeSlot (SlotConfig (..), posixTimeToNominalDiffTime, posixTimeToUTCTime)
import Ouroboros.Consensus.HardFork.History qualified as Ouroboros
import Ouroboros.Consensus.Util.Counting qualified as Ouroboros
import Plutus.V1.Ledger.Api (POSIXTime (..))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

{-# OPTIONS_GHC -Wno-orphans #-}

module Ledger.TimeSlot(
module Cardano.Node.Emulator.TimeSlot(
SlotConfig(..)
, SlotConversionError(..)
, slotRangeToPOSIXTimeRange
Expand Down
29 changes: 15 additions & 14 deletions cardano-node-emulator/src/Cardano/Node/Emulator/Validation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ import Cardano.Ledger.Shelley.Rules.Utxo (UtxoEnv (..))
import Cardano.Ledger.Shelley.TxBody (DCert, Wdrl)
import Cardano.Ledger.ShelleyMA.Timelocks (ValidityInterval)
import Cardano.Ledger.TxIn (TxIn (..))
import Cardano.Node.Emulator.Params (EmulatorEra, Params (emulatorPParams, pNetworkId), emulatorGlobals,
emulatorPParams)
import Cardano.Slotting.Slot (SlotNo (..))
import Control.Lens (makeLenses, over, (&), (.~), (^.))
import Control.Monad.Except (MonadError (throwError))
Expand All @@ -88,8 +90,7 @@ import Ledger.Ada qualified as P
import Ledger.Address qualified as P
import Ledger.Crypto qualified as Crypto
import Ledger.Index.Internal qualified as P
import Ledger.Params (EmulatorEra, emulatorGlobals, emulatorPParams)
import Ledger.Params qualified as P
-- import Ledger.Params qualified as P
import Ledger.Slot (Slot)
import Ledger.Tx (CardanoTx (CardanoApiTx), SomeCardanoApiTx (CardanoApiEmulatorEraTx, SomeTx), addCardanoTxSignature,
onCardanoTx)
Expand Down Expand Up @@ -181,7 +182,7 @@ makeBlock state =

{-| Initial ledger state for a distribution
-}
initialState :: P.Params -> EmulatedLedgerState
initialState :: Params -> EmulatedLedgerState
initialState params = EmulatedLedgerState
{ _ledgerEnv = C.Ledger.LedgerEnv
{ C.Ledger.ledgerSlotNo = 0
Expand All @@ -198,11 +199,11 @@ initialState params = EmulatedLedgerState
}


utxoEnv :: P.Params -> SlotNo -> C.Ledger.UtxoEnv EmulatorEra
utxoEnv :: Params -> SlotNo -> C.Ledger.UtxoEnv EmulatorEra
utxoEnv params slotNo = C.Ledger.UtxoEnv slotNo (emulatorPParams params) mempty (C.Ledger.GenDelegs mempty)

applyTx ::
P.Params ->
Params ->
EmulatedLedgerState ->
Tx EmulatorEra ->
Either P.ValidationError (EmulatedLedgerState, Validated (Tx EmulatorEra))
Expand All @@ -211,7 +212,7 @@ applyTx params oldState@EmulatedLedgerState{_ledgerEnv, _memPoolState} tx = do
return (oldState & memPoolState .~ newMempool & over currentBlock ((:) vtx), vtx)


hasValidationErrors :: P.Params -> SlotNo -> UTxO EmulatorEra -> C.Api.Tx C.Api.BabbageEra -> Either P.ValidationErrorInPhase P.ValidationSuccess
hasValidationErrors :: Params -> SlotNo -> UTxO EmulatorEra -> C.Api.Tx C.Api.BabbageEra -> Either P.ValidationErrorInPhase P.ValidationSuccess
hasValidationErrors params slotNo utxo tx'@(C.Api.ShelleyTx _ tx) =
case res of
Left e -> Left (P.Phase1, e)
Expand Down Expand Up @@ -270,7 +271,7 @@ constructValidated globals (UtxoEnv _ pp _ _) st tx =
lift (Fails _ _) = False

validateCardanoTx
:: P.Params
:: Params
-> Slot
-> UTxO EmulatorEra
-> CardanoTx
Expand All @@ -281,7 +282,7 @@ validateCardanoTx params slot utxo@(UTxO utxoMap) =
(\(CardanoApiEmulatorEraTx tx) -> if Map.null utxoMap then Right Map.empty else
hasValidationErrors params (fromIntegral slot) utxo tx)

getTxExUnitsWithLogs :: P.Params -> UTxO EmulatorEra -> C.Api.Tx C.Api.BabbageEra -> Either P.ValidationErrorInPhase P.ValidationSuccess
getTxExUnitsWithLogs :: Params -> UTxO EmulatorEra -> C.Api.Tx C.Api.BabbageEra -> Either P.ValidationErrorInPhase P.ValidationSuccess
getTxExUnitsWithLogs params utxo (C.Api.ShelleyTx _ tx) =
case C.Ledger.evaluateTransactionExecutionUnitsWithLogs (emulatorPParams params) tx utxo ei ss costmdls of
Left e -> Left . (P.Phase1,) . P.CardanoLedgerValidationError . Text.pack . show $ e
Expand All @@ -298,7 +299,7 @@ getTxExUnitsWithLogs params utxo (C.Api.ShelleyTx _ tx) =
toCardanoLedgerError e = Left (P.Phase2, P.CardanoLedgerValidationError $ Text.pack $ show e)

makeTransactionBody
:: P.Params
:: Params
-> UTxO EmulatorEra
-> P.CardanoBuildTx
-> Either CardanoLedgerError (C.Api.TxBody C.Api.BabbageEra)
Expand All @@ -308,14 +309,14 @@ makeTransactionBody params utxo txBodyContent = do
first Right $ P.makeTransactionBody (Just $ emulatorPParams params) exUnits txBodyContent


evaluateMinLovelaceOutput :: P.Params -> TxOut EmulatorEra -> P.Ada
evaluateMinLovelaceOutput :: Params -> TxOut EmulatorEra -> P.Ada
evaluateMinLovelaceOutput params = toPlutusValue . C.Ledger.evaluateMinLovelaceOutput (emulatorPParams params)
where
toPlutusValue :: Coin -> P.Ada
toPlutusValue (Coin c) = P.lovelaceOf c

fromPlutusTxSigned'
:: P.Params
:: Params
-> UTxO EmulatorEra
-> P.Tx
-> Map.Map P.PaymentPubKey P.PaymentPrivateKey
Expand All @@ -333,7 +334,7 @@ fromPlutusTxSigned' params utxo tx knownPaymentKeys =
signTx . CardanoApiTx <$> convertTx tx

fromPlutusTxSigned
:: P.Params
:: Params
-> UTxO EmulatorEra
-> P.Tx
-> Map.Map P.PaymentPubKey P.PaymentPrivateKey
Expand All @@ -343,13 +344,13 @@ fromPlutusTxSigned params utxo tx knownPaymentKeys = case fromPlutusTxSigned' pa
Right t -> t

fromPlutusTx
:: P.Params
:: Params
-> UTxO EmulatorEra
-> [P.PaymentPubKeyHash]
-> P.Tx
-> Either CardanoLedgerError (C.Api.Tx C.Api.BabbageEra)
fromPlutusTx params utxo requiredSigners tx = do
txBodyContent <- first Right $ P.toCardanoTxBodyContent params requiredSigners tx
txBodyContent <- first Right $ P.toCardanoTxBodyContent (pNetworkId params) (emulatorPParams params) requiredSigners tx
makeSignedTransaction [] <$> makeTransactionBody params utxo txBodyContent

getRequiredSigners :: C.Api.Tx C.Api.BabbageEra -> [P.PaymentPubKeyHash]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import Test.Tasty (TestTree, testGroup)
import Test.Tasty.Hedgehog (testPropertyNamed)

import Cardano.Node.Emulator.Generators qualified as Gen
import Cardano.Node.Emulator.TimeSlot (SlotConfig (scSlotLength))
import Cardano.Node.Emulator.TimeSlot qualified as TimeSlot
import Data.Aeson qualified as JSON
import Data.Aeson.Internal qualified as Aeson
import Hedgehog qualified
Expand All @@ -24,8 +26,6 @@ import Ledger qualified
import Ledger.Ada qualified as Ada
import Ledger.Bytes qualified as Bytes
import Ledger.Interval qualified as Interval
import Ledger.TimeSlot (SlotConfig (scSlotLength))
import Ledger.TimeSlot qualified as TimeSlot
import Ledger.Value qualified as Value
import PlutusTx.Prelude qualified as PlutusTx

Expand Down
2 changes: 0 additions & 2 deletions plutus-ledger/plutus-ledger.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,10 @@ library
Ledger.Index
Ledger.Index.Internal
Ledger.Orphans
Ledger.Params
Ledger.Scripts
Ledger.Scripts.Orphans
Ledger.Slot
Ledger.Test
Ledger.TimeSlot
Ledger.Tokens
Ledger.Tx
Ledger.Tx.CardanoAPI
Expand Down
1 change: 0 additions & 1 deletion plutus-ledger/src/Ledger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import Ledger.Blockchain as Export
import Ledger.Crypto as Export
import Ledger.Index as Export
import Ledger.Orphans ()
import Ledger.Params as Export
import Ledger.Scripts as Export
import Ledger.Slot as Export
import Ledger.Tx as Export
Expand Down

0 comments on commit 9e707c0

Please sign in to comment.