Skip to content

Commit

Permalink
Replace TxCertPool in core TxCert with specialized patterns
Browse files Browse the repository at this point in the history
`RegPoolTxCert` and `RetirePoolTxCert`
  • Loading branch information
teodanciu committed May 30, 2023
1 parent c160399 commit 44c713f
Show file tree
Hide file tree
Showing 27 changed files with 135 additions and 96 deletions.
11 changes: 8 additions & 3 deletions eras/allegra/impl/src/Cardano/Ledger/Allegra/TxCert.hs
Expand Up @@ -16,10 +16,15 @@ instance Crypto c => EraTxCert (AllegraEra c) where

getScriptWitnessTxCert = getScriptWitnessShelleyTxCert

mkTxCertPool = ShelleyTxCertPool
mkRegPoolTxCert = ShelleyTxCertPool . RegPool

getTxCertPool (ShelleyTxCertPool c) = Just c
getTxCertPool _ = Nothing
getRegPoolTxCert (ShelleyTxCertPool (RegPool poolParams)) = Just poolParams
getRegPoolTxCert _ = Nothing

mkRetirePoolTxCert poolId epochNo = ShelleyTxCertPool $ RetirePool poolId epochNo

getRetirePoolTxCert (ShelleyTxCertPool (RetirePool poolId epochNo)) = Just (poolId, epochNo)
getRetirePoolTxCert _ = Nothing

instance Crypto c => ShelleyEraTxCert (AllegraEra c) where
{-# SPECIALIZE instance ShelleyEraTxCert (AllegraEra StandardCrypto) #-}
Expand Down
11 changes: 8 additions & 3 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxCert.hs
Expand Up @@ -16,10 +16,15 @@ instance Crypto c => EraTxCert (AlonzoEra c) where

getScriptWitnessTxCert = getScriptWitnessShelleyTxCert

mkTxCertPool = ShelleyTxCertPool
mkRegPoolTxCert = ShelleyTxCertPool . RegPool

getTxCertPool (ShelleyTxCertPool c) = Just c
getTxCertPool _ = Nothing
getRegPoolTxCert (ShelleyTxCertPool (RegPool poolParams)) = Just poolParams
getRegPoolTxCert _ = Nothing

mkRetirePoolTxCert poolId epochNo = ShelleyTxCertPool $ RetirePool poolId epochNo

getRetirePoolTxCert (ShelleyTxCertPool (RetirePool poolId epochNo)) = Just (poolId, epochNo)
getRetirePoolTxCert _ = Nothing

instance Crypto c => ShelleyEraTxCert (AlonzoEra c) where
{-# SPECIALIZE instance ShelleyEraTxCert (AlonzoEra StandardCrypto) #-}
Expand Down
11 changes: 8 additions & 3 deletions eras/babbage/impl/src/Cardano/Ledger/Babbage/TxCert.hs
Expand Up @@ -16,10 +16,15 @@ instance Crypto c => EraTxCert (BabbageEra c) where

getScriptWitnessTxCert = getScriptWitnessShelleyTxCert

mkTxCertPool = ShelleyTxCertPool
mkRegPoolTxCert = ShelleyTxCertPool . RegPool

getTxCertPool (ShelleyTxCertPool c) = Just c
getTxCertPool _ = Nothing
getRegPoolTxCert (ShelleyTxCertPool (RegPool poolParams)) = Just poolParams
getRegPoolTxCert _ = Nothing

mkRetirePoolTxCert poolId epochNo = ShelleyTxCertPool $ RetirePool poolId epochNo

getRetirePoolTxCert (ShelleyTxCertPool (RetirePool poolId epochNo)) = Just (poolId, epochNo)
getRetirePoolTxCert _ = Nothing

instance Crypto c => ShelleyEraTxCert (BabbageEra c) where
{-# SPECIALIZE instance ShelleyEraTxCert (BabbageEra StandardCrypto) #-}
Expand Down
13 changes: 10 additions & 3 deletions eras/conway/impl/src/Cardano/Ledger/Conway/TxCert.hs
Expand Up @@ -51,6 +51,7 @@ import Cardano.Ledger.Credential (Credential, StakeCredential, credKeyHashWitnes
import Cardano.Ledger.Crypto
import Cardano.Ledger.Keys (KeyHash, KeyRole (..), asWitness)
import Cardano.Ledger.Shelley.TxCert (
PoolCert (..),
ShelleyDelegCert (..),
ShelleyEraTxCert (..),
encodePoolCert,
Expand All @@ -70,9 +71,15 @@ instance Crypto c => EraTxCert (ConwayEra c) where

getScriptWitnessTxCert = getScriptWitnessConwayTxCert

mkTxCertPool = ConwayTxCertPool
getTxCertPool (ConwayTxCertPool x) = Just x
getTxCertPool _ = Nothing
mkRegPoolTxCert = ConwayTxCertPool . RegPool

getRegPoolTxCert (ConwayTxCertPool (RegPool poolParams)) = Just poolParams
getRegPoolTxCert _ = Nothing

mkRetirePoolTxCert poolId epochNo = ConwayTxCertPool $ RetirePool poolId epochNo

getRetirePoolTxCert (ConwayTxCertPool (RetirePool poolId epochNo)) = Just (poolId, epochNo)
getRetirePoolTxCert _ = Nothing

instance Crypto c => ShelleyEraTxCert (ConwayEra c) where
mkShelleyTxCertDeleg = ConwayTxCertDeleg . fromShelleyDelegCert
Expand Down
12 changes: 9 additions & 3 deletions eras/mary/impl/src/Cardano/Ledger/Mary/TxCert.hs
Expand Up @@ -7,6 +7,7 @@ import Cardano.Ledger.Crypto (Crypto, StandardCrypto)
import Cardano.Ledger.Mary.Era (MaryEra)
import Cardano.Ledger.Shelley.TxCert (
EraTxCert (..),
PoolCert (..),
ShelleyEraTxCert (..),
ShelleyTxCert (..),
getScriptWitnessShelleyTxCert,
Expand All @@ -22,10 +23,15 @@ instance Crypto c => EraTxCert (MaryEra c) where

getScriptWitnessTxCert = getScriptWitnessShelleyTxCert

mkTxCertPool = ShelleyTxCertPool
mkRegPoolTxCert = ShelleyTxCertPool . RegPool

getTxCertPool (ShelleyTxCertPool c) = Just c
getTxCertPool _ = Nothing
getRegPoolTxCert (ShelleyTxCertPool (RegPool poolParams)) = Just poolParams
getRegPoolTxCert _ = Nothing

mkRetirePoolTxCert poolId epochNo = ShelleyTxCertPool $ RetirePool poolId epochNo

getRetirePoolTxCert (ShelleyTxCertPool (RetirePool poolId epochNo)) = Just (poolId, epochNo)
getRetirePoolTxCert _ = Nothing

instance Crypto c => ShelleyEraTxCert (MaryEra c) where
{-# SPECIALIZE instance ShelleyEraTxCert (MaryEra StandardCrypto) #-}
Expand Down
Expand Up @@ -61,7 +61,7 @@ totalCertsDeposits pp isRegPool certs =
numKeys = getSum @Int $ foldMap' (\x -> if isRegKey x then 1 else 0) certs
numNewRegPoolCerts = Set.size (foldl' addNewPoolIds Set.empty certs)
addNewPoolIds regPoolIds = \case
TxCertPool (RegPool (PoolParams {ppId}))
RegPoolTxCert (PoolParams {ppId})
-- We don't pay a deposit on a pool that is already registered or duplicated in the certs
| not (isRegPool ppId || Set.member ppId regPoolIds) -> Set.insert ppId regPoolIds
_ -> regPoolIds
Expand Down
Expand Up @@ -324,7 +324,7 @@ delegationTransition = do
{ dsFutureGenDelegs =
eval (dsFutureGenDelegs ds singleton (FutureGenDeleg s' gkh) (GenDelegPair vkh vrf))
}
TxCertPool _ -> do
RegPoolTxCert _ -> do
failBecause WrongCertificateTypeDELEG -- this always fails
pure ds
_ | Just (MIRCert targetPot mirTarget) <- getTxCertMir c -> do
Expand Down
4 changes: 2 additions & 2 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Pool.hs
Expand Up @@ -178,7 +178,7 @@ poolDelegationTransition = do
let stpools = psStakePoolParams ps
let pv = pp ^. ppProtocolVersionL
case c of
TxCertPool (RegPool poolParam) -> do
RegPoolTxCert poolParam -> do
-- note that pattern match is used instead of cwitness, as in the spec

when (HardForks.validatePoolRewardAccountNetID pv) $ do
Expand Down Expand Up @@ -223,7 +223,7 @@ poolDelegationTransition = do
{ psFutureStakePoolParams = eval (psFutureStakePoolParams ps singleton hk poolParam)
, psRetiring = eval (setSingleton hk psRetiring ps)
}
TxCertPool (RetirePool hk e) -> do
RetirePoolTxCert hk e -> do
-- note that pattern match is used instead of cwitness, as in the spec
eval (hk dom stpools) ?! StakePoolNotRegisteredOnKeyPOOL hk
cepoch <- liftSTS $ do
Expand Down
4 changes: 2 additions & 2 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Reports.hs
Expand Up @@ -54,8 +54,8 @@ synopsisCert x = case x of
ShelleyTxCertDeleg (ShelleyRegCert cred) -> "ShelleyRegCert " ++ take 10 (showCred cred)
ShelleyTxCertDeleg (ShelleyUnRegCert cred) -> "ShelleyUnRegCert " ++ take 10 (showCred cred)
ShelleyTxCertDeleg (ShelleyDelegCert cred _) -> "ShelleyDelegCert" ++ take 10 (showCred cred)
TxCertPool (RegPool pool) -> let KeyHash hash = ppId pool in "RegPool " ++ take 10 (show hash)
TxCertPool (RetirePool khash e) -> "RetirePool " ++ showKeyHash khash ++ " " ++ show e
RegPoolTxCert pool -> let KeyHash hash = ppId pool in "RegPool " ++ take 10 (show hash)
RetirePoolTxCert khash e -> "RetirePool " ++ showKeyHash khash ++ " " ++ show e
TxCertGenesisDeleg _ -> "GenesisCert"
_ | isInstantaneousRewards x -> "MirCert"
_ -> error "Impossible"
Expand Down
Expand Up @@ -501,7 +501,7 @@ witsVKeyNeeded utxo' tx genDelegs =
owners :: Set (KeyHash 'Witness (EraCrypto era))
owners = foldr' accum Set.empty (txBody ^. certsTxBodyL)
where
accum (TxCertPool (RegPool pool)) !ans =
accum (RegPoolTxCert pool) !ans =
Set.union
(Set.map asWitness (ppOwners pool))
ans
Expand Down
20 changes: 12 additions & 8 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/TxCert.hs
Expand Up @@ -62,7 +62,8 @@ module Cardano.Ledger.Shelley.TxCert (

-- * Re-exports
EraTxCert (..),
pattern TxCertPool,
pattern RegPoolTxCert,
pattern RetirePoolTxCert,
Delegation (..),
PoolCert (..),
poolCWitness,
Expand Down Expand Up @@ -109,10 +110,13 @@ instance Crypto c => EraTxCert (ShelleyEra c) where

getScriptWitnessTxCert = getScriptWitnessShelleyTxCert

mkTxCertPool = ShelleyTxCertPool
mkRegPoolTxCert = ShelleyTxCertPool . RegPool
getRegPoolTxCert (ShelleyTxCertPool (RegPool poolParams)) = Just poolParams
getRegPoolTxCert _ = Nothing

getTxCertPool (ShelleyTxCertPool c) = Just c
getTxCertPool _ = Nothing
mkRetirePoolTxCert poolId epochNo = ShelleyTxCertPool $ RetirePool poolId epochNo
getRetirePoolTxCert (ShelleyTxCertPool (RetirePool poolId epochNo)) = Just (poolId, epochNo)
getRetirePoolTxCert _ = Nothing

class EraTxCert era => ShelleyEraTxCert era where
mkShelleyTxCertDeleg :: ShelleyDelegCert (EraCrypto era) -> TxCert era
Expand Down Expand Up @@ -339,11 +343,11 @@ poolTxCertDecoder :: EraTxCert era => Word -> Decoder s (Int, TxCert era)
poolTxCertDecoder = \case
3 -> do
group <- decCBORGroup
pure (1 + listLenInt group, TxCertPool (RegPool group))
pure (1 + listLenInt group, RegPoolTxCert group)
4 -> do
a <- decCBOR
b <- decCBOR
pure (3, TxCertPool $ RetirePool a b)
pure (3, RetirePoolTxCert a b)
k -> invalidKey k
{-# INLINE poolTxCertDecoder #-}

Expand Down Expand Up @@ -409,12 +413,12 @@ isGenesisDelegation = isJust . getTxCertGenesisDeleg

-- | Check for 'RegPool' constructor
isRegPool :: EraTxCert era => TxCert era -> Bool
isRegPool (TxCertPool (RegPool _)) = True
isRegPool (RegPoolTxCert _) = True
isRegPool _ = False

-- | Check for 'RetirePool' constructor
isRetirePool :: EraTxCert era => TxCert era -> Bool
isRetirePool (TxCertPool (RetirePool _ _)) = True
isRetirePool (RetirePoolTxCert _ _) = True
isRetirePool _ = False

isInstantaneousRewards :: ShelleyEraTxCert era => TxCert era -> Bool
Expand Down
Expand Up @@ -411,7 +411,7 @@ mkPoolParameters keys =

-- Create stake pool registration certs
poolRegCerts :: [KeyPair 'StakePool B_Crypto] -> StrictSeq (TxCert B)
poolRegCerts = StrictSeq.fromList . fmap (TxCertPool . RegPool . mkPoolParameters)
poolRegCerts = StrictSeq.fromList . fmap (RegPoolTxCert . mkPoolParameters)

-- Create a transaction that registers stake pools.
txRegStakePools :: TxIx -> [KeyPair 'StakePool B_Crypto] -> ShelleyTx B
Expand Down Expand Up @@ -467,7 +467,7 @@ txbRetireStakePool x y =
(StrictSeq.fromList [ShelleyTxOut aliceAddr (inject $ Coin 100)])
( StrictSeq.fromList $
fmap
(\ks -> TxCertPool $ RetirePool (mkPoolKeyHash ks) (EpochNo 1))
(\ks -> RetirePoolTxCert (mkPoolKeyHash ks) (EpochNo 1))
(poolColdKeys x y)
)
(Withdrawals Map.empty)
Expand Down
Expand Up @@ -471,7 +471,7 @@ exampleCerts :: (ShelleyEraTxCert era, ProtVerAtMost era 8) => StrictSeq (TxCert
exampleCerts =
StrictSeq.fromList
[ ShelleyTxCertDeleg (ShelleyRegCert (keyToCredential exampleStakeKey))
, TxCertPool (RegPool examplePoolParams)
, RegPoolTxCert examplePoolParams
, TxCertMir $
MIRCert ReservesMIR $
StakeAddressesMIR $
Expand Down
Expand Up @@ -415,7 +415,7 @@ genRegPool ::
Gen (Maybe (TxCert era, CertCred era))
genRegPool poolKeys keyPairs minPoolCost = do
(pps, poolKey) <- genStakePool poolKeys keyPairs minPoolCost
pure $ Just (TxCertPool (RegPool pps), PoolCred poolKey)
pure $ Just (RegPoolTxCert pps, PoolCred poolKey)

-- | Generate a RetirePool along with the keypair which registered it.
--
Expand All @@ -437,7 +437,7 @@ genRetirePool _pp poolKeys pState slot =
else
( \keyHash epoch ->
Just
( TxCertPool (RetirePool keyHash epoch)
( RetirePoolTxCert keyHash epoch
, PoolCred (aikCold $ lookupHash keyHash)
)
)
Expand Down
Expand Up @@ -135,7 +135,7 @@ poolStateIsInternallyConsistent (SourceSignalTarget {source = chainSt, signal =
poolRegistrationProp :: EraTxCert era => SourceSignalTarget (ShelleyPOOL era) -> Property
poolRegistrationProp
SourceSignalTarget
{ signal = (TxCertPool (RegPool poolParams))
{ signal = (RegPoolTxCert poolParams)
, source = sourceSt
, target = targetSt
} =
Expand Down Expand Up @@ -173,7 +173,7 @@ poolRetirementProp :: EraTxCert era => EpochNo -> EpochNo -> SourceSignalTarget
poolRetirementProp
currentEpoch@(EpochNo ce)
(EpochNo maxEpoch)
SourceSignalTarget {source = sourceSt, target = targetSt, signal = (TxCertPool (RetirePool hk e))} =
SourceSignalTarget {source = sourceSt, target = targetSt, signal = (RetirePoolTxCert hk e)} =
conjoin
[ counterexample
("epoch must be well formed " <> show ce <> " " <> show e <> " " <> show maxEpoch)
Expand Down
Expand Up @@ -199,7 +199,8 @@ poolTraceFromBlock chainSt block =
in PoolEnv s pp
poolSt0 =
certPState (lsCertState ledgerSt0)
poolCert (TxCertPool _) = True
poolCert (RegPoolTxCert _) = True
poolCert (RetirePoolTxCert _ _) = True
poolCert _ = False

-- | Reconstruct a DELEG trace from all the transaction certificates in a Block
Expand Down
Expand Up @@ -49,7 +49,7 @@ testPoolNetworkID pv poolParams e = do
( TRC
( PoolEnv (SlotNo 0) $ emptyPParams & ppProtocolVersionL .~ pv
, def
, TxCertPool (RegPool poolParams)
, RegPoolTxCert poolParams
)
)
case (st, e) of
Expand Down
Expand Up @@ -198,7 +198,7 @@ txbodyEx1 =
( [ ShelleyTxCertDeleg (ShelleyRegCert Cast.aliceSHK)
, ShelleyTxCertDeleg (ShelleyRegCert Cast.bobSHK)
, ShelleyTxCertDeleg (ShelleyRegCert Cast.carlSHK)
, TxCertPool (RegPool Cast.alicePoolParams)
, RegPoolTxCert Cast.alicePoolParams
]
++ [ ShelleyTxCertMir
( MIRCert
Expand Down Expand Up @@ -931,7 +931,7 @@ txbodyEx11 =
ShelleyTxBody
(Set.fromList [TxIn (txid txbodyEx4) minBound])
(StrictSeq.singleton $ ShelleyTxOut Cast.alicePtrAddr (Val.inject aliceCoinEx11Ptr))
(StrictSeq.fromList [TxCertPool (RetirePool (aikColdKeyHash Cast.alicePoolKeys) aliceRetireEpoch)])
(StrictSeq.fromList [RetirePoolTxCert (aikColdKeyHash Cast.alicePoolKeys) aliceRetireEpoch])
(Withdrawals Map.empty)
feeTx11
(SlotNo 500)
Expand Down
Expand Up @@ -106,7 +106,7 @@ txbodyEx1 =
ShelleyTxBody
(Set.fromList [TxIn genesisId minBound])
(StrictSeq.fromList [ShelleyTxOut Cast.aliceAddr (Val.inject aliceCoinEx1)])
(StrictSeq.fromList [TxCertPool (RegPool Cast.alicePoolParams)])
(StrictSeq.fromList [RegPoolTxCert Cast.alicePoolParams])
(Withdrawals Map.empty)
feeTx1
(SlotNo 10)
Expand Down Expand Up @@ -183,7 +183,7 @@ txbodyEx2 =
(Set.fromList [TxIn (txid txbodyEx1) minBound])
(StrictSeq.fromList [ShelleyTxOut Cast.aliceAddr (Val.inject aliceCoinEx2)])
( StrictSeq.fromList
( [ TxCertPool (RegPool newPoolParams)
( [ RegPoolTxCert newPoolParams
]
)
)
Expand Down
Expand Up @@ -193,8 +193,8 @@ txbodyEx1 =
[ ShelleyTxCertDeleg (ShelleyRegCert Cast.aliceSHK)
, ShelleyTxCertDeleg (ShelleyRegCert Cast.bobSHK)
, ShelleyTxCertDeleg (ShelleyRegCert Cast.carlSHK)
, TxCertPool (RegPool alicePoolParams')
, TxCertPool (RegPool bobPoolParams')
, RegPoolTxCert alicePoolParams'
, RegPoolTxCert bobPoolParams'
, ShelleyTxCertDeleg (ShelleyDelegCert Cast.aliceSHK (aikColdKeyHash Cast.alicePoolKeys))
, ShelleyTxCertDeleg (ShelleyDelegCert Cast.bobSHK (aikColdKeyHash Cast.bobPoolKeys))
, ShelleyTxCertDeleg (ShelleyDelegCert Cast.carlSHK (aikColdKeyHash Cast.alicePoolKeys))
Expand Down
Expand Up @@ -332,7 +332,7 @@ txbRegisterPool =
ShelleyTxBody
{ stbInputs = Set.fromList [TxIn genesisId minBound]
, stbOutputs = StrictSeq.fromList [ShelleyTxOut aliceAddr (Val.inject $ Coin 10)]
, stbCerts = StrictSeq.fromList [TxCertPool (RegPool alicePoolParams)]
, stbCerts = StrictSeq.fromList [RegPoolTxCert alicePoolParams]
, stbWithdrawals = Withdrawals Map.empty
, stbTxFee = Coin 94
, stbTTL = SlotNo 10
Expand Down Expand Up @@ -360,7 +360,7 @@ txbRetirePool =
ShelleyTxBody
{ stbInputs = Set.fromList [TxIn genesisId minBound]
, stbOutputs = StrictSeq.fromList [ShelleyTxOut aliceAddr (Val.inject $ Coin 10)]
, stbCerts = StrictSeq.fromList [TxCertPool (RetirePool alicePoolKH (EpochNo 5))]
, stbCerts = StrictSeq.fromList [RetirePoolTxCert alicePoolKH (EpochNo 5)]
, stbWithdrawals = Withdrawals Map.empty
, stbTxFee = Coin 94
, stbTTL = SlotNo 10
Expand Down

0 comments on commit 44c713f

Please sign in to comment.