@@ -33,14 +33,15 @@ module Cardano.Ledger.Shelley.Rules.Ledger (
3333 shelleyLedgerAssertions ,
3434) where
3535
36- import Cardano.Ledger.BaseTypes (ShelleyBase , TxIx , invalidKey )
36+ import Cardano.Ledger.BaseTypes (ShelleyBase , TxIx , invalidKey , networkId )
3737import Cardano.Ledger.Binary (
3838 DecCBOR (.. ),
3939 EncCBOR (.. ),
4040 decodeRecordSum ,
4141 encodeListLen ,
4242 )
4343import Cardano.Ledger.Binary.Coders (Encode (.. ), encode , (!>) )
44+ import Control.Monad.Trans.Reader (asks )
4445import Cardano.Ledger.Shelley.AdaPots (consumedTxBody , producedTxBody )
4546import Cardano.Ledger.Shelley.Core
4647import Cardano.Ledger.Shelley.Era (ShelleyEra , ShelleyLEDGER )
@@ -65,7 +66,7 @@ import Cardano.Ledger.Shelley.Rules.Reports (showTxCerts)
6566import Cardano.Ledger.Shelley.Rules.Utxo (ShelleyUtxoPredFailure (.. ), UtxoEnv (.. ))
6667import Cardano.Ledger.Shelley.Rules.Utxow (ShelleyUTXOW , ShelleyUtxowPredFailure )
6768import Cardano.Ledger.Slot (EpochNo (.. ), SlotNo , epochFromSlot )
68- import Cardano.Ledger.State (EraCertState (.. ))
69+ import Cardano.Ledger.State (EraCertState (.. ), withdrawalsThatDoNotDrainAccounts , accountsL , drainAccounts )
6970import Control.DeepSeq (NFData (.. ))
7071import Control.State.Transition (
7172 Assertion (PostCondition ),
@@ -77,6 +78,7 @@ import Control.State.Transition (
7778 judgmentContext ,
7879 liftSTS ,
7980 trans ,
81+ failOnJust ,
8082 )
8183import Data.Sequence (Seq )
8284import qualified Data.Sequence.Strict as StrictSeq
@@ -116,6 +118,8 @@ instance EraPParams era => EncCBOR (LedgerEnv era) where
116118data ShelleyLedgerPredFailure era
117119 = UtxowFailure (PredicateFailure (EraRule " UTXOW" era )) -- Subtransition Failures
118120 | DelegsFailure (PredicateFailure (EraRule " DELEGS" era )) -- Subtransition Failures
121+ | ShelleyWithdrawalsMissingAccounts Withdrawals
122+ | ShelleyIncompleteWithdrawals Withdrawals
119123 deriving (Generic )
120124
121125ledgerSlotNoL :: Lens' (LedgerEnv era ) SlotNo
@@ -213,8 +217,10 @@ instance
213217 EncCBOR (ShelleyLedgerPredFailure era )
214218 where
215219 encCBOR = \ case
216- (UtxowFailure a) -> encodeListLen 2 <> encCBOR (0 :: Word8 ) <> encCBOR a
217- (DelegsFailure a) -> encodeListLen 2 <> encCBOR (1 :: Word8 ) <> encCBOR a
220+ UtxowFailure a -> encodeListLen 2 <> encCBOR (0 :: Word8 ) <> encCBOR a
221+ DelegsFailure a -> encodeListLen 2 <> encCBOR (1 :: Word8 ) <> encCBOR a
222+ ShelleyWithdrawalsMissingAccounts w -> encodeListLen 2 <> encCBOR (2 :: Word8 ) <> encCBOR w
223+ ShelleyIncompleteWithdrawals w -> encodeListLen 2 <> encCBOR (3 :: Word8 ) <> encCBOR w
218224
219225instance
220226 ( DecCBOR (PredicateFailure (EraRule " DELEGS" era ))
@@ -232,6 +238,12 @@ instance
232238 1 -> do
233239 a <- decCBOR
234240 pure (2 , DelegsFailure a)
241+ 2 -> do
242+ w <- decCBOR
243+ pure (2 , ShelleyWithdrawalsMissingAccounts w)
244+ 3 -> do
245+ w <- decCBOR
246+ pure (2 , ShelleyIncompleteWithdrawals w)
235247 k -> invalidKey k
236248
237249shelleyLedgerAssertions ::
@@ -281,6 +293,7 @@ instance
281293ledgerTransition ::
282294 forall era .
283295 ( EraTx era
296+ , EraCertState era
284297 , STS (ShelleyLEDGER era )
285298 , Embed (EraRule " DELEGS" era ) (ShelleyLEDGER era )
286299 , Environment (EraRule " DELEGS" era ) ~ DelegsEnv era
@@ -296,11 +309,22 @@ ledgerTransition = do
296309 TRC (LedgerEnv slot mbCurEpochNo txIx pp account, LedgerState utxoSt certState, tx) <-
297310 judgmentContext
298311 curEpochNo <- maybe (liftSTS $ epochFromSlot slot) pure mbCurEpochNo
312+ network <- liftSTS $ asks networkId
313+ let withdrawals = tx ^. bodyTxL . withdrawalsTxBodyL
314+ (invalidWithdrawals, incompleteWithdrawals) =
315+ case withdrawalsThatDoNotDrainAccounts withdrawals network $ certState ^. certDStateL . accountsL of
316+ Nothing -> (Nothing , Nothing )
317+ Just (invalid, incomplete) ->
318+ ( if null (unWithdrawals invalid) then Nothing else Just invalid
319+ , if null (unWithdrawals incomplete) then Nothing else Just incomplete
320+ )
321+ failOnJust invalidWithdrawals ShelleyWithdrawalsMissingAccounts
322+ failOnJust incompleteWithdrawals ShelleyIncompleteWithdrawals
299323 certState' <-
300324 trans @ (EraRule " DELEGS" era ) $
301325 TRC
302326 ( DelegsEnv slot curEpochNo txIx pp tx account
303- , certState
327+ , certState & certDStateL . accountsL %~ drainAccounts withdrawals
304328 , StrictSeq. fromStrict $ tx ^. bodyTxL . certsTxBodyL
305329 )
306330
0 commit comments