Skip to content

Commit

Permalink
Merge pull request #4547 from IntersectMBO/td/drep-registration-expir…
Browse files Browse the repository at this point in the history
…ation-fix

Drep registration expiration fix
  • Loading branch information
lehins authored Aug 20, 2024
2 parents 87b58ea + f668f36 commit 068c7a6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 20 deletions.
3 changes: 2 additions & 1 deletion eras/conway/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Version history for `cardano-ledger-conway`

## 1.16.1.0
## 1.17.0.0

* Replace GOVCERT `updateDRepExpiry` with `computeDRepExpiry`
* Added `Eq`, `Show`, `NFData` and `Generic` instances for `CertsEnv`
* Add `delegateToDRep` and `redelegateDRep`

Expand Down
7 changes: 5 additions & 2 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Certs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import Cardano.Ledger.Conway.Governance (
)
import Cardano.Ledger.Conway.Rules.Cert (CertEnv (CertEnv), ConwayCertEvent, ConwayCertPredFailure)
import Cardano.Ledger.Conway.Rules.Deleg (ConwayDelegPredFailure)
import Cardano.Ledger.Conway.Rules.GovCert (ConwayGovCertPredFailure, updateDRepExpiry)
import Cardano.Ledger.Conway.Rules.GovCert (ConwayGovCertPredFailure, computeDRepExpiry)
import Cardano.Ledger.DRep (drepExpiryL)
import Cardano.Ledger.Shelley.API (
CertState (..),
Expand Down Expand Up @@ -236,7 +236,10 @@ conwayCertsTransition = do
Map.foldlWithKey'
( \dreps voter _ -> case voter of
DRepVoter cred ->
Map.adjust (updateDRepExpiry drepActivity currentEpoch numDormantEpochs) cred dreps
Map.adjust
(drepExpiryL .~ computeDRepExpiry drepActivity currentEpoch numDormantEpochs)
cred
dreps
_ -> dreps
)
vsDReps
Expand Down
57 changes: 41 additions & 16 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Rules/GovCert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Cardano.Ledger.Conway.Rules.GovCert (
ConwayGOVCERT,
ConwayGovCertPredFailure (..),
ConwayGovCertEnv (..),
updateDRepExpiry,
computeDRepExpiry,
)
where

Expand Down Expand Up @@ -53,6 +53,7 @@ import Cardano.Ledger.Credential (Credential)
import Cardano.Ledger.Crypto (Crypto)
import Cardano.Ledger.DRep (DRepState (..), drepAnchorL, drepDepositL, drepExpiryL)
import Cardano.Ledger.Keys (KeyRole (ColdCommitteeRole, DRepRole))
import qualified Cardano.Ledger.Shelley.HardForks as HF (bootstrapPhase)
import Cardano.Slotting.Slot (EpochInterval, binOpEpochNo)
import Control.DeepSeq (NFData)
import Control.State.Transition.Extended (
Expand Down Expand Up @@ -235,7 +236,15 @@ conwayGovCertTransition = do
{ vsDReps =
Map.insert
cred
(DRepState (addEpochInterval cgceCurrentEpoch ppDRepActivity) mAnchor ppDRepDeposit)
( DRepState
( computeDRepExpiryVersioned
cgcePParams
cgceCurrentEpoch
(vState ^. vsNumDormantEpochsL)
)
mAnchor
ppDRepDeposit
)
vsDReps
}
ConwayUnRegDRep cred refund -> do
Expand All @@ -253,11 +262,13 @@ conwayGovCertTransition = do
{ vsDReps =
Map.adjust
( \drepState ->
updateDRepExpiry
ppDRepActivity
cgceCurrentEpoch
(vState ^. vsNumDormantEpochsL)
$ drepState & drepAnchorL .~ mAnchor
drepState
& drepExpiryL
.~ computeDRepExpiry
ppDRepActivity
cgceCurrentEpoch
(vState ^. vsNumDormantEpochsL)
& drepAnchorL .~ mAnchor
)
cred
vsDReps
Expand All @@ -272,18 +283,32 @@ conwayGovCertTransition = do
UpdateCommittee _ _ newMembers _ -> Map.member coldCred newMembers
_ -> False

updateDRepExpiry ::
computeDRepExpiryVersioned ::
ConwayEraPParams era =>
PParams era ->
-- | Current epoch
EpochNo ->
-- | The count of the dormant epochs
EpochNo ->
EpochNo
computeDRepExpiryVersioned pp currentEpoch numDormantEpochs
-- Starting with version 10, we correctly take into account the number of dormant epochs
-- when registering a drep
| HF.bootstrapPhase (pp ^. ppProtocolVersionL) =
addEpochInterval currentEpoch (pp ^. ppDRepActivityL)
| otherwise =
computeDRepExpiry (pp ^. ppDRepActivityL) currentEpoch numDormantEpochs

computeDRepExpiry ::
-- | DRepActivity PParam
EpochInterval ->
-- | Current epoch
EpochNo ->
-- | The count of the dormant epochs
EpochNo ->
DRepState c ->
DRepState c
updateDRepExpiry ppDRepActivity currentEpoch numDormantEpochs =
drepExpiryL
.~ binOpEpochNo
(-)
(addEpochInterval currentEpoch ppDRepActivity)
numDormantEpochs
-- | Computed expiry
EpochNo
computeDRepExpiry ppDRepActivity currentEpoch =
binOpEpochNo
(-)
(addEpochInterval currentEpoch ppDRepActivity)
2 changes: 1 addition & 1 deletion libs/cardano-ledger-api/cardano-ledger-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ library
cardano-ledger-alonzo >=1.9 && <1.11,
cardano-ledger-babbage ^>=1.9,
cardano-ledger-binary ^>=1.3,
cardano-ledger-conway >=1.13 && <1.17,
cardano-ledger-conway >=1.13 && <1.18,
cardano-ledger-core ^>=1.14,
cardano-ledger-mary ^>=1.7,
cardano-ledger-shelley ^>=1.13,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Cardano.Ledger.Core
import Cardano.Ledger.Credential (Credential (KeyHashObj))
import Cardano.Ledger.DRep
import Cardano.Ledger.Keys (KeyRole (..))
import qualified Cardano.Ledger.Shelley.HardForks as HF
import Cardano.Ledger.Shelley.LedgerState
import Data.Default (def)
import Data.Foldable (Foldable (..))
Expand Down Expand Up @@ -70,6 +71,33 @@ spec = do
expectActualDRepExpiry drep $
addEpochInterval curEpochNo $
EpochInterval (drepActivity + fromIntegral n)

it "dRep registered when there are dormant epochs" $ do
let drepActivity = 3
modifyPParams $ ppDRepActivityL .~ EpochInterval drepActivity
let n = 2
passNEpochs n
expectNumDormantEpochs $ EpochNo (fromIntegral n)
(drep, _, _) <- setupSingleDRep 1_000_000

let expectedExpiry = do
epochNo <- getsNES nesELL
let tot = addEpochInterval epochNo (EpochInterval drepActivity)
pv <- getProtVer
pure $
if HF.bootstrapPhase pv
then binOpEpochNo (+) tot (fromIntegral n)
else tot

expectedExpiry >>= expectActualDRepExpiry drep

nes <- getsNES id
void $ submitParameterChange SNothing $ def & ppuMinFeeAL .~ SJust (Coin 3000)

expectedExpiry >>= expectDRepExpiry drep
drepState <- drepStateFromQuery drep nes
expectedExpiry >>= shouldBe (drepState ^. drepExpiryL)

it "proposals are made and numDormantEpochs are added" $ do
curEpochNo <- getsNES nesELL
let drepActivity = 3
Expand Down

0 comments on commit 068c7a6

Please sign in to comment.