Skip to content

Commit 551ba54

Browse files
committed
wip
1 parent f776625 commit 551ba54

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Delegs.hs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
{-# LANGUAGE LambdaCase #-}
88
{-# LANGUAGE MultiParamTypeClasses #-}
99
{-# LANGUAGE OverloadedStrings #-}
10-
{-# LANGUAGE PatternSynonyms #-}
1110
{-# LANGUAGE ScopedTypeVariables #-}
1211
{-# LANGUAGE StandaloneDeriving #-}
1312
{-# LANGUAGE TypeApplications #-}
@@ -31,7 +30,6 @@ import Cardano.Ledger.BaseTypes (
3130
ShelleyBase,
3231
TxIx (..),
3332
invalidKey,
34-
networkId,
3533
)
3634
import Cardano.Ledger.Binary (
3735
DecCBOR (..),
@@ -55,16 +53,13 @@ import Cardano.Ledger.Shelley.Rules.Pool (ShelleyPoolPredFailure)
5553
import Cardano.Ledger.Shelley.State
5654
import Cardano.Ledger.Slot (SlotNo (..))
5755
import Control.DeepSeq
58-
import Control.Monad.Trans.Reader (asks)
5956
import Control.SetAlgebra (dom, eval, (∈))
6057
import Control.State.Transition (
6158
Embed (..),
6259
STS (..),
6360
TRC (..),
6461
TransitionRule,
65-
failOnJust,
6662
judgmentContext,
67-
liftSTS,
6863
trans,
6964
validateTrans,
7065
)
@@ -217,21 +212,10 @@ delegsTransition ::
217212
TransitionRule (ShelleyDELEGS era)
218213
delegsTransition = do
219214
TRC
220-
(env@(DelegsEnv slot@(SlotNo slot64) epochNo txIx pp tx chainAccountState), certState, certificates) <-
215+
(env@(DelegsEnv slot@(SlotNo slot64) epochNo txIx pp _tx chainAccountState), certState, certificates) <-
221216
judgmentContext
222-
network <- liftSTS $ asks networkId
223-
224217
case certificates of
225-
Empty -> do
226-
let dState = certState ^. certDStateL
227-
withdrawals = tx ^. bodyTxL . withdrawalsTxBodyL
228-
accounts = dState ^. accountsL
229-
failOnJust
230-
(withdrawalsThatDoNotDrainAccounts withdrawals network accounts)
231-
( \(invalid, incomplete) ->
232-
WithdrawalsNotInRewardsDELEGS $ Withdrawals $ unWithdrawals invalid <> unWithdrawals incomplete
233-
)
234-
pure $ certState & certDStateL . accountsL %~ drainAccounts withdrawals
218+
Empty -> pure certState
235219
gamma :|> txCert -> do
236220
certState' <-
237221
trans @(ShelleyDELEGS era) $ TRC (env, certState, gamma)

eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Ledger.hs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
3737
import Cardano.Ledger.Binary (
3838
DecCBOR (..),
3939
EncCBOR (..),
4040
decodeRecordSum,
4141
encodeListLen,
4242
)
4343
import Cardano.Ledger.Binary.Coders (Encode (..), encode, (!>))
44+
import Control.Monad.Trans.Reader (asks)
4445
import Cardano.Ledger.Shelley.AdaPots (consumedTxBody, producedTxBody)
4546
import Cardano.Ledger.Shelley.Core
4647
import Cardano.Ledger.Shelley.Era (ShelleyEra, ShelleyLEDGER)
@@ -65,7 +66,7 @@ import Cardano.Ledger.Shelley.Rules.Reports (showTxCerts)
6566
import Cardano.Ledger.Shelley.Rules.Utxo (ShelleyUtxoPredFailure (..), UtxoEnv (..))
6667
import Cardano.Ledger.Shelley.Rules.Utxow (ShelleyUTXOW, ShelleyUtxowPredFailure)
6768
import Cardano.Ledger.Slot (EpochNo (..), SlotNo, epochFromSlot)
68-
import Cardano.Ledger.State (EraCertState (..))
69+
import Cardano.Ledger.State (EraCertState (..), withdrawalsThatDoNotDrainAccounts, accountsL, drainAccounts)
6970
import Control.DeepSeq (NFData (..))
7071
import Control.State.Transition (
7172
Assertion (PostCondition),
@@ -77,6 +78,7 @@ import Control.State.Transition (
7778
judgmentContext,
7879
liftSTS,
7980
trans,
81+
failOnJust,
8082
)
8183
import Data.Sequence (Seq)
8284
import qualified Data.Sequence.Strict as StrictSeq
@@ -116,6 +118,8 @@ instance EraPParams era => EncCBOR (LedgerEnv era) where
116118
data 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

121125
ledgerSlotNoL :: 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

219225
instance
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

237249
shelleyLedgerAssertions ::
@@ -281,6 +293,7 @@ instance
281293
ledgerTransition ::
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

Comments
 (0)