Skip to content

Commit

Permalink
Split up the MaybeInRecentEra type into two disjoint sum types.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed May 31, 2023
1 parent 58b9c1c commit ac6c279
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/wallet/src/Cardano/Wallet/Byron/Compatibility.hs
Expand Up @@ -151,7 +151,7 @@ mainnetNetworkParameters = W.NetworkParameters
, maximumCollateralInputCount = 0
, minimumCollateralPercentage = 0
, executionUnitPrices = Nothing
, currentLedgerProtocolParameters = Write.InNonRecentEraByron
, currentLedgerProtocolParameters = Write.InBygoneEra Write.InEraByron
}
}

Expand Down Expand Up @@ -333,7 +333,7 @@ protocolParametersFromPP eraInfo pp =
, maximumCollateralInputCount = 0
, minimumCollateralPercentage = 0
, executionUnitPrices = Nothing
, currentLedgerProtocolParameters = Write.InNonRecentEraByron
, currentLedgerProtocolParameters = Write.InBygoneEra Write.InEraByron
}
where
fromBound (Bound _relTime _slotNo (O.EpochNo e)) =
Expand Down
16 changes: 10 additions & 6 deletions lib/wallet/src/Cardano/Wallet/Shelley/Compatibility.hs
Expand Up @@ -893,7 +893,8 @@ fromShelleyPParams eraInfo pp =
, maximumCollateralInputCount = 0
, minimumCollateralPercentage = 0
, executionUnitPrices = Nothing
, currentLedgerProtocolParameters = Write.InNonRecentEraShelley
, currentLedgerProtocolParameters =
Write.InBygoneEra Write.InEraShelley
}

fromAllegraPParams
Expand All @@ -917,7 +918,8 @@ fromAllegraPParams eraInfo pp =
, maximumCollateralInputCount = 0
, minimumCollateralPercentage = 0
, executionUnitPrices = Nothing
, currentLedgerProtocolParameters = Write.InNonRecentEraAllegra
, currentLedgerProtocolParameters =
Write.InBygoneEra Write.InEraAllegra
}

fromMaryPParams
Expand All @@ -941,7 +943,8 @@ fromMaryPParams eraInfo pp =
, maximumCollateralInputCount = 0
, minimumCollateralPercentage = 0
, executionUnitPrices = Nothing
, currentLedgerProtocolParameters = Write.InNonRecentEraMary
, currentLedgerProtocolParameters =
Write.InBygoneEra Write.InEraMary
}

fromBoundToEpochNo :: Bound -> W.EpochNo
Expand Down Expand Up @@ -973,7 +976,8 @@ fromAlonzoPParams eraInfo pp =
Alonzo._collateralPercentage pp
, executionUnitPrices =
Just $ executionUnitPricesFromPParams pp
, currentLedgerProtocolParameters = Write.InNonRecentEraAlonzo
, currentLedgerProtocolParameters =
Write.InBygoneEra Write.InEraAlonzo
}

fromBabbagePParams
Expand Down Expand Up @@ -1002,7 +1006,7 @@ fromBabbagePParams eraInfo pp =
, executionUnitPrices =
Just $ executionUnitPricesFromPParams pp
, currentLedgerProtocolParameters =
Write.InRecentEraBabbage $ Write.ProtocolParameters pp
Write.InRecentEra $ Write.InEraBabbage $ Write.ProtocolParameters pp
}

fromConwayPParams
Expand All @@ -1026,7 +1030,7 @@ fromConwayPParams eraInfo pp =
, minimumCollateralPercentage = Conway._collateralPercentage pp
, executionUnitPrices = Just $ executionUnitPricesFromPParams pp
, currentLedgerProtocolParameters =
Write.InRecentEraConway $ Write.ProtocolParameters pp
Write.InRecentEra $ Write.InEraConway $ Write.ProtocolParameters pp
}

-- | Extract the current network decentralization level from the given set of
Expand Down
47 changes: 33 additions & 14 deletions lib/wallet/src/Cardano/Wallet/Write/Tx.hs
Expand Up @@ -37,6 +37,8 @@ module Cardano.Wallet.Write.Tx
, toRecentEra
, fromRecentEra
, MaybeInRecentEra (..)
, InRecentEra (..)
, InBygoneEra (..)
, toRecentEraGADT
, LatestLedgerEra
, LatestEra
Expand Down Expand Up @@ -359,30 +361,47 @@ shelleyBasedEra :: forall era. IsRecentEra era => Cardano.ShelleyBasedEra era
shelleyBasedEra = shelleyBasedEraFromRecentEra $ recentEra @era

data MaybeInRecentEra (thing :: Type -> Type)
= InNonRecentEraByron
| InNonRecentEraShelley
| InNonRecentEraAllegra
| InNonRecentEraMary
| InNonRecentEraAlonzo
| InRecentEraBabbage (thing BabbageEra)
| InRecentEraConway (thing ConwayEra)
= InBygoneEra !(InBygoneEra)
| InRecentEra !(InRecentEra thing)

data InBygoneEra
= InEraByron
| InEraShelley
| InEraAllegra
| InEraMary
| InEraAlonzo
deriving (Eq, Show)

data InRecentEra (thing :: Type -> Type)
= InEraBabbage !(thing BabbageEra)
| InEraConway !(thing ConwayEra)

deriving instance (Eq (a BabbageEra), (Eq (a ConwayEra)))
=> Eq (MaybeInRecentEra a)
deriving instance (Show (a BabbageEra), (Show (a ConwayEra)))
=> Show (MaybeInRecentEra a)

deriving instance (Eq (a BabbageEra), (Eq (a ConwayEra)))
=> Eq (InRecentEra a)
deriving instance (Show (a BabbageEra), (Show (a ConwayEra)))
=> Show (InRecentEra a)

toRecentEraGADT
:: MaybeInRecentEra a
-> Either Cardano.AnyCardanoEra (InAnyRecentEra a)
toRecentEraGADT = \case
InNonRecentEraByron -> Left $ Cardano.AnyCardanoEra Cardano.ByronEra
InNonRecentEraShelley -> Left $ Cardano.AnyCardanoEra Cardano.ShelleyEra
InNonRecentEraAllegra -> Left $ Cardano.AnyCardanoEra Cardano.AllegraEra
InNonRecentEraMary -> Left $ Cardano.AnyCardanoEra Cardano.MaryEra
InNonRecentEraAlonzo -> Left $ Cardano.AnyCardanoEra Cardano.AlonzoEra
InRecentEraBabbage a -> Right $ InAnyRecentEra recentEra a
InRecentEraConway a -> Right $ InAnyRecentEra recentEra a
InBygoneEra e -> Left $ inBygoneEra e
InRecentEra e -> Right $ inRecentEra e
where
inBygoneEra = \case
InEraByron -> Cardano.AnyCardanoEra Cardano.ByronEra
InEraShelley -> Cardano.AnyCardanoEra Cardano.ShelleyEra
InEraAllegra -> Cardano.AnyCardanoEra Cardano.AllegraEra
InEraMary -> Cardano.AnyCardanoEra Cardano.MaryEra
InEraAlonzo -> Cardano.AnyCardanoEra Cardano.AlonzoEra
inRecentEra = \case
InEraBabbage a -> InAnyRecentEra recentEra a
InEraConway a -> InAnyRecentEra recentEra a

data InAnyRecentEra thing where
InAnyRecentEra
Expand Down
Expand Up @@ -148,7 +148,8 @@ dummyProtocolParameters = ProtocolParameters
, pricePerMemoryUnit = 0.0577
}
, currentLedgerProtocolParameters =
Write.InRecentEraBabbage
Write.InRecentEra
$ Write.InEraBabbage
$ Write.ProtocolParameters
$ C.toLedgerPParams C.ShelleyBasedEraBabbage dummyNodeProtocolParameters
}
Expand Down
2 changes: 1 addition & 1 deletion lib/wallet/test/unit/Cardano/Wallet/DB/Arbitrary.hs
Expand Up @@ -724,7 +724,7 @@ instance Arbitrary ProtocolParameters where
<*> genMaximumCollateralInputCount
<*> genMinimumCollateralPercentage
<*> arbitrary
<*> pure Write.InNonRecentEraAlonzo
<*> pure (Write.InBygoneEra Write.InEraAlonzo)
where
genMaximumCollateralInputCount :: Gen Word16
genMaximumCollateralInputCount = arbitrarySizedNatural
Expand Down

0 comments on commit ac6c279

Please sign in to comment.