Skip to content

Commit dbbb867

Browse files
committed
Construct EraHistory correctly
Signed-off-by: Sasha Bogicevic <[email protected]>
1 parent 384bbc3 commit dbbb867

File tree

4 files changed

+43
-37
lines changed

4 files changed

+43
-37
lines changed

hydra-cardano-api/hydra-cardano-api.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ library
7575
-- dependencies on cardano-ledger* and plutus-ledger-api follow.
7676
build-depends:
7777
, aeson >=2
78-
, base >=4.16
78+
, base >=4.14
7979
, bytestring
8080
, cardano-api ^>=10.13
8181
, cardano-api:gen

hydra-node/src/Hydra/Chain/Blockfrost.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ mkTinyWallet ::
3939
mkTinyWallet tracer config = do
4040
keyPair@(_, sk) <- readKeyPair cardanoSigningKey
4141
prj <- Blockfrost.projectFromFile projectPath
42-
genesis@Blockfrost.Genesis{_genesisSystemStart, _genesisNetworkMagic} <- runBlockfrostM prj queryGenesis
42+
Blockfrost.Genesis{_genesisSystemStart, _genesisNetworkMagic} <- runBlockfrostM prj queryGenesis
4343
let networkId = toCardanoNetworkId _genesisNetworkMagic
44-
let queryEpochInfo = pure $ toEpochInfo $ mkEraHistory genesis
44+
let queryEpochInfo = toEpochInfo <$> runBlockfrostM prj mkEraHistory
4545
-- NOTE: we don't need to provide address here since it is derived from the
4646
-- keypair but we still want to keep the same wallet api.
4747
let queryWalletInfo queryPoint _address = runBlockfrostM prj $ do

hydra-node/src/Hydra/Chain/Blockfrost/Client.hs

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ import Cardano.Ledger.Conway.PParams (ppMinFeeRefScriptCostPerByteL)
3333
import Cardano.Ledger.Plutus (ExUnits (..), Language (..), Prices (..))
3434
import Cardano.Ledger.Plutus.CostModels (CostModels, mkCostModel, mkCostModels)
3535
import Cardano.Ledger.Shelley.API (ProtVer (..))
36-
import Cardano.Slotting.Time (mkSlotLength)
36+
import Cardano.Slotting.Time (RelativeTime (..), mkSlotLength)
3737
import Control.Lens ((.~), (^.))
3838
import Data.Default (def)
39-
import Data.SOP.NonEmpty (NonEmpty (..))
39+
import Data.SOP.NonEmpty (nonEmptyFromList)
4040
import Data.Set qualified as Set
4141
import Data.Text qualified as T
4242
import Hydra.Cardano.Api.Prelude (StakePoolKey, fromNetworkMagic)
@@ -45,8 +45,7 @@ import Hydra.Chain.ScriptRegistry (buildScriptPublishingTxs)
4545
import Hydra.Tx (txId)
4646
import Money qualified
4747
import Ouroboros.Consensus.Block (GenesisWindow (..))
48-
import Ouroboros.Consensus.Cardano.Block (CardanoEras, StandardCrypto)
49-
import Ouroboros.Consensus.HardFork.History (EraEnd (..), EraParams (..), EraSummary (..), SafeZone (..), Summary (..), initBound, mkInterpreter)
48+
import Ouroboros.Consensus.HardFork.History (Bound (..), EraEnd (..), EraParams (..), EraSummary (..), SafeZone (..), Summary (..), mkInterpreter)
5049

5150
data APIBlockfrostError
5251
= BlockfrostError Text
@@ -73,7 +72,7 @@ publishHydraScripts ::
7372
publishHydraScripts projectPath sk = do
7473
prj <- Blockfrost.projectFromFile projectPath
7574
runBlockfrostM prj $ do
76-
genesis@Blockfrost.Genesis
75+
Blockfrost.Genesis
7776
{ _genesisNetworkMagic = networkMagic
7877
, _genesisSystemStart = systemStart'
7978
} <-
@@ -85,7 +84,7 @@ publishHydraScripts projectPath sk = do
8584
stakePools' <- Blockfrost.listPools
8685
let stakePools = Set.fromList (toCardanoPoolId <$> stakePools')
8786
let systemStart = SystemStart $ posixSecondsToUTCTime systemStart'
88-
let eraHistory = mkEraHistory genesis
87+
eraHistory <- mkEraHistory
8988
utxo <- Blockfrost.getAddressUtxos address
9089
let cardanoUTxO = toCardanoUTxO utxo changeAddress
9190

@@ -323,32 +322,40 @@ toCardanoGenesisParameters bfGenesis =
323322
, _genesisSecurityParam
324323
} = bfGenesis
325324

326-
mkEraHistory :: Blockfrost.Genesis -> EraHistory
327-
mkEraHistory genesis = EraHistory (mkInterpreter summary)
325+
mkEraHistory :: BlockfrostClientT IO EraHistory
326+
mkEraHistory = do
327+
eras' <- Blockfrost.getNetworkEras
328+
let eras = filter withoutEmptyEra eras'
329+
let summary = mkEra <$> eras
330+
case nonEmptyFromList summary of
331+
Nothing ->
332+
liftIO $ throwIO $ BlockfrostError "Failed to create EraHistory."
333+
Just s -> pure $ EraHistory (mkInterpreter $ Summary s)
328334
where
329-
Blockfrost.Genesis
330-
{ _genesisNetworkMagic
331-
, _genesisSystemStart
332-
, _genesisSlotLength
333-
, _genesisEpochLength
334-
} = genesis
335-
336-
summary :: Summary (CardanoEras StandardCrypto)
337-
summary =
338-
Summary . NonEmptyOne $
339-
EraSummary
340-
{ eraStart = initBound
341-
, eraEnd = EraUnbounded
342-
, eraParams
343-
}
344-
345-
eraParams =
335+
mkBound Blockfrost.NetworkEraBound{_boundEpoch, _boundSlot, _boundTime} =
336+
Bound
337+
{ boundTime = RelativeTime _boundTime
338+
, boundSlot = SlotNo $ fromIntegral _boundSlot
339+
, boundEpoch = EpochNo $ fromIntegral _boundEpoch
340+
}
341+
mkEraParams Blockfrost.NetworkEraParameters{_parametersEpochLength, _parametersSlotLength, _parametersSafeZone} =
346342
EraParams
347-
{ eraEpochSize = EpochSize $ fromIntegral _genesisEpochLength
348-
, eraSlotLength = mkSlotLength $ fromIntegral _genesisSlotLength
349-
, eraSafeZone = UnsafeIndefiniteSafeZone
350-
, eraGenesisWin = GenesisWindow 1
343+
{ eraEpochSize = EpochSize $ fromIntegral _parametersEpochLength
344+
, eraSlotLength = mkSlotLength _parametersSlotLength
345+
, eraSafeZone = StandardSafeZone _parametersSafeZone
346+
, eraGenesisWin = GenesisWindow _parametersSafeZone
347+
}
348+
mkEra Blockfrost.NetworkEraSummary{_networkEraStart, _networkEraEnd, _networkEraParameters} =
349+
EraSummary
350+
{ eraStart = mkBound _networkEraStart
351+
, eraEnd = EraEnd $ mkBound _networkEraEnd
352+
, eraParams = mkEraParams _networkEraParameters
351353
}
354+
withoutEmptyEra
355+
Blockfrost.NetworkEraSummary
356+
{ _networkEraStart = Blockfrost.NetworkEraBound{_boundTime = boundStart}
357+
, _networkEraEnd = Blockfrost.NetworkEraBound{_boundTime = boundEnd}
358+
} = boundStart == 0 && boundEnd == 0
352359

353360
----------------
354361
-- Wallet API --

hydra-node/src/Hydra/Chain/Direct/Wallet.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import Hydra.Prelude
88

99
import Cardano.Api.UTxO (UTxO)
1010
import Cardano.Ledger.Address qualified as Ledger
11-
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusContext, ContextError, EraPlutusContext)
12-
import Cardano.Crypto.Hash.Class
11+
import Cardano.Ledger.Alonzo.Plutus.Context (ContextError, EraPlutusContext)
1312
import Cardano.Ledger.Alonzo.Scripts (
1413
AlonzoEraScript (..),
1514
AsIx (..),
@@ -101,8 +100,8 @@ import Hydra.Chain.CardanoClient (QueryPoint (..))
101100
import Hydra.Ledger.Cardano ()
102101
import Hydra.Logging (Tracer, traceWith)
103102

104-
type Address = Ledger.Addr StandardCrypto
105-
type TxIn = Ledger.TxIn StandardCrypto
103+
type Address = Ledger.Addr
104+
type TxIn = Ledger.TxIn
106105
type TxOut = Ledger.TxOut LedgerEra
107106

108107
-- | A 'TinyWallet' is a small abstraction of a wallet with basic UTXO
@@ -236,7 +235,7 @@ data ErrCoverFee
236235
| ErrUnknownInput {input :: TxIn}
237236
| ErrScriptExecutionFailed {redeemerPointer :: Text, scriptFailure :: Text}
238237
| ErrTranslationError (ContextError LedgerEra)
239-
| ErrConwayUpgradeError (TxUpgradeError Conway)
238+
| ErrConwayUpgradeError (TxUpgradeError ConwayEra)
240239
deriving stock (Show)
241240

242241
data ChangeError = ChangeError {inputBalance :: Coin, outputBalance :: Coin}

0 commit comments

Comments
 (0)