Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRep undelegation fix #4773

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Deleg.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Cardano.Ledger.Conway.Rules.Deleg (
processDelegation,
) where

import Cardano.Ledger.BaseTypes (ShelleyBase, StrictMaybe (..))
import Cardano.Ledger.BaseTypes (ProtVer (..), ShelleyBase, StrictMaybe (..), natVersion)
import Cardano.Ledger.Binary (DecCBOR (..), EncCBOR (..))
import Cardano.Ledger.Binary.Coders (
Decode (From, Invalid, SumD, Summands),
Expand Down Expand Up @@ -174,6 +174,7 @@ conwayDelegTransition = do
judgmentContext
let
ppKeyDeposit = pp ^. ppKeyDepositL
pv = pp ^. ppProtocolVersionL
checkDepositAgainstPParams deposit =
deposit == ppKeyDeposit ?! IncorrectDepositDELEG deposit
registerStakeCredential stakeCred =
Expand Down Expand Up @@ -228,13 +229,14 @@ conwayDelegTransition = do
ConwayDelegCert stakeCred delegatee -> do
mCurDelegatee <- checkStakeKeyIsRegistered stakeCred
checkStakeDelegateeRegistered delegatee
pure $ processDelegationInternal stakeCred mCurDelegatee delegatee certState
pure $
processDelegationInternal (pvMajor pv < natVersion @10) stakeCred mCurDelegatee delegatee certState
ConwayRegDelegCert stakeCred delegatee deposit -> do
checkDepositAgainstPParams deposit
checkStakeKeyNotRegistered stakeCred
checkStakeDelegateeRegistered delegatee
pure $
processDelegationInternal stakeCred Nothing delegatee $
processDelegationInternal (pvMajor pv < natVersion @10) stakeCred Nothing delegatee $
certState & certDStateL . dsUnifiedL .~ registerStakeCredential stakeCred

-- | Apply new delegation, while properly cleaning up older delegations. This function
Expand All @@ -248,13 +250,15 @@ processDelegation ::
CertState era
processDelegation stakeCred newDelegatee !certState = certState'
where
!certState' = processDelegationInternal stakeCred mCurDelegatee newDelegatee certState
!certState' = processDelegationInternal False stakeCred mCurDelegatee newDelegatee certState
mUMElem = Map.lookup stakeCred (UM.umElems (dsUnified (certDState certState)))
mCurDelegatee = mUMElem >>= umElemToDelegatee

-- | Same as `processDelegation`, except it expects the current delegation supplied as an
-- argument, because in ledger rules we already have it readily available.
processDelegationInternal ::
-- | Preserve the buggy behavior where DRep delegations are not updated correctly (See #4772)
Bool ->
-- | Delegator
Credential 'Staking (EraCrypto era) ->
-- | Current delegatee for the above stake credential that needs to be cleaned up.
Expand All @@ -263,7 +267,7 @@ processDelegationInternal ::
Delegatee (EraCrypto era) ->
CertState era ->
CertState era
processDelegationInternal stakeCred mCurDelegatee newDelegatee =
processDelegationInternal preserveIncorrectDelegation stakeCred mCurDelegatee newDelegatee =
case newDelegatee of
DelegStake sPool -> delegStake sPool
DelegVote dRep -> delegVote dRep
Expand All @@ -278,7 +282,9 @@ processDelegationInternal stakeCred mCurDelegatee newDelegatee =
processDRepUnDelegation stakeCred mCurDelegatee cState
& certDStateL . dsUnifiedL %~ \umap ->
UM.DRepUView umap UM.⨃ Map.singleton stakeCred dRep
dReps = vsDReps (certVState cState)
dReps
| preserveIncorrectDelegation = vsDReps (certVState cState)
| otherwise = vsDReps (certVState cState')
in case dRep of
DRepCredential targetDRep
| Just dRepState <- Map.lookup targetDRep dReps ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ hardforkTransition = do
epochState
& esLStateL . lsCertStateL %~ \certState ->
let umap = certState ^. certDStateL . dsUnifiedL
dReps = certState ^. certVStateL . vsDRepsL
dReps =
-- Reset all delegations in order to remove any inconsistencies
-- Delegations will be reset accordingly below.
Map.map (\dRepState -> dRepState {drepDelegs = Set.empty}) $
certState ^. certVStateL . vsDRepsL
lehins marked this conversation as resolved.
Show resolved Hide resolved
(dRepsWithDelegations, elemsWithoutUnknownDRepDelegations) =
Map.mapAccumWithKey adjustDelegations dReps (UM.umElems umap)
adjustDelegations ds stakeCred umElem@(UM.UMElem rd ptr stakePool mDrep) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,11 @@ spec = do

expectDelegatedVote cred (DRepCredential drepCred2)

impAnn "Check that unregistration of previous delegation does not affect current delegation" $ do
unRegisterDRep drepCred
-- we need to preserve the buggy behavior until the boostrap phase is over.
ifBootstrap (expectNotDelegatedVote cred) (expectDelegatedVote cred (DRepCredential drepCred2))

it "Delegate vote and unregister stake credentials" $ do
expectedDeposit <- getsNES $ nesEsL . curPParamsEpochStateL . ppKeyDepositL
cred <- KeyHashObj <$> freshKeyHash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ library
cardano-ledger-binary,
cardano-ledger-core:{cardano-ledger-core, testlib},
cardano-ledger-conway:{cardano-ledger-conway, testlib},
cardano-ledger-shelley,
cardano-ledger-allegra,
cardano-ledger-mary,
cardano-ledger-alonzo,
cardano-ledger-babbage,
cardano-ledger-conformance,
cardano-ledger-test,
constrained-generators,
Expand Down
6 changes: 6 additions & 0 deletions libs/cardano-ledger-repl-environment/src/ReplEnvironment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@
-- the REPL should load this module automatically.
module ReplEnvironment where

import Cardano.Ledger.Allegra
import Cardano.Ledger.Alonzo
import Cardano.Ledger.Api
import Cardano.Ledger.Babbage
import Cardano.Ledger.Binary
import Cardano.Ledger.CertState
import Cardano.Ledger.Coin
import Cardano.Ledger.Conway
import Cardano.Ledger.Conway.Core
import Cardano.Ledger.Conway.Governance
import Cardano.Ledger.Crypto
import Cardano.Ledger.Mary
import Cardano.Ledger.PoolDistr
import Cardano.Ledger.Shelley
import Cardano.Ledger.Shelley.LedgerState
import Cardano.Ledger.Val
import Test.Cardano.Ledger.Api.DebugTools

Expand Down