Skip to content

Commit

Permalink
sketch for auto suspension
Browse files Browse the repository at this point in the history
  • Loading branch information
drsk committed Nov 26, 2024
1 parent 4a2e7b8 commit 6f62054
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 27 deletions.
4 changes: 4 additions & 0 deletions concordium-consensus/src/Concordium/GlobalState/BlockState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,8 @@ class (BlockStateQuery m) => BlockStateOperations m where
-- round did timeout.
bsoUpdateMissedRounds :: (PVSupportsDelegation (MPV m), PVSupportsValidatorSuspension (MPV m)) => UpdatableBlockState m -> Map.Map BakerId Word64 -> m (UpdatableBlockState m)

bsoPrimeForSuspension :: (PVSupportsDelegation (MPV m), PVSupportsValidatorSuspension (MPV m)) => UpdatableBlockState m -> [BakerId] -> m (UpdatableBlockState m)

-- | A snapshot of the block state that can be used to roll back to a previous state.
type StateSnapshot m

Expand Down Expand Up @@ -1830,6 +1832,7 @@ instance (Monad (t m), MonadTrans t, BlockStateOperations m) => BlockStateOperat
bsoSetRewardAccounts s = lift . bsoSetRewardAccounts s
bsoIsProtocolUpdateEffective = lift . bsoIsProtocolUpdateEffective
bsoUpdateMissedRounds s = lift . bsoUpdateMissedRounds s
bsoPrimeForSuspension s = lift . bsoPrimeForSuspension s
type StateSnapshot (MGSTrans t m) = StateSnapshot m
bsoSnapshotState = lift . bsoSnapshotState
bsoRollback s = lift . bsoRollback s
Expand Down Expand Up @@ -1887,6 +1890,7 @@ instance (Monad (t m), MonadTrans t, BlockStateOperations m) => BlockStateOperat
{-# INLINE bsoGetCurrentEpochBakers #-}
{-# INLINE bsoIsProtocolUpdateEffective #-}
{-# INLINE bsoUpdateMissedRounds #-}
{-# INLINE bsoPrimeForSuspension #-}
{-# INLINE bsoSnapshotState #-}
{-# INLINE bsoRollback #-}

Expand Down
20 changes: 16 additions & 4 deletions concordium-consensus/src/Concordium/GlobalState/DummyData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ dummyFinalizationCommitteeParameters =
_fcpFinalizerRelativeStakeThreshold = PartsPerHundredThousands 10000
}

-- | Validator score parameters for the second consensus protocol.
dummyValidatorScoreParameters :: ValidatorScoreParameters
dummyValidatorScoreParameters =
ValidatorScoreParameters
{ -- Maximal number of missed rounds before a validator gets suspended.
_vspMaxMissedRounds = 1
}

dummyChainParameters :: forall cpv. (IsChainParametersVersion cpv) => ChainParameters' cpv
dummyChainParameters = case chainParametersVersion @cpv of
SChainParametersV0 ->
Expand All @@ -382,7 +390,8 @@ dummyChainParameters = case chainParametersVersion @cpv of
PoolParametersV0
{ _ppBakerStakeThreshold = 300000000000
},
_cpFinalizationCommitteeParameters = NoParam
_cpFinalizationCommitteeParameters = NoParam,
_cpValidatorScoreParameters = NoParam
}
SChainParametersV1 ->
ChainParameters
Expand Down Expand Up @@ -420,7 +429,8 @@ dummyChainParameters = case chainParametersVersion @cpv of
_transactionCommissionRange = fullRange
}
},
_cpFinalizationCommitteeParameters = NoParam
_cpFinalizationCommitteeParameters = NoParam,
_cpValidatorScoreParameters = NoParam
}
SChainParametersV2 ->
ChainParameters
Expand Down Expand Up @@ -458,7 +468,8 @@ dummyChainParameters = case chainParametersVersion @cpv of
_transactionCommissionRange = fullRange
}
},
_cpFinalizationCommitteeParameters = SomeParam dummyFinalizationCommitteeParameters
_cpFinalizationCommitteeParameters = SomeParam dummyFinalizationCommitteeParameters,
_cpValidatorScoreParameters = NoParam
}
SChainParametersV3 ->
ChainParameters
Expand Down Expand Up @@ -496,7 +507,8 @@ dummyChainParameters = case chainParametersVersion @cpv of
_transactionCommissionRange = fullRange
}
},
_cpFinalizationCommitteeParameters = SomeParam dummyFinalizationCommitteeParameters
_cpFinalizationCommitteeParameters = SomeParam dummyFinalizationCommitteeParameters,
_cpValidatorScoreParameters = SomeParam dummyValidatorScoreParameters
}
where
fullRange = InclusiveRange (makeAmountFraction 0) (makeAmountFraction 100000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,7 @@ instance (MonadBlobStore m, IsCooldownParametersVersion cpv) => BlobStorable m (
instance (MonadBlobStore m) => BlobStorable m Parameters.TimeParameters
instance (MonadBlobStore m) => BlobStorable m Parameters.TimeoutParameters
instance (MonadBlobStore m) => BlobStorable m Parameters.FinalizationCommitteeParameters
instance (MonadBlobStore m) => BlobStorable m Parameters.ValidatorScoreParameters
instance (MonadBlobStore m) => BlobStorable m Duration
instance (MonadBlobStore m) => BlobStorable m Energy
instance (MonadBlobStore m) => BlobStorable m (Map AccountAddress Timestamp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3498,6 +3498,28 @@ doUpdateMissedRounds pbs rds = do
(Map.toList rds)
storePBS pbs bsp'

doPrimeForSuspension ::
( PVSupportsDelegation pv,
SupportsPersistentState pv m
) =>
PersistentBlockState pv ->
[BakerId] ->
m (PersistentBlockState pv)
doPrimeForSuspension pbs bids = do
bsp <- loadPBS pbs
bsp' <-
foldM
( \bsp0 bId ->
modifyBakerPoolRewardDetailsInPoolRewards
bsp0
bId
(\bprd -> bprd{suspensionPrimed = True <$ suspensionPrimed bprd})
)
bsp
bids

storePBS pbs bsp'

doProcessUpdateQueues ::
forall pv m.
(SupportsPersistentState pv m) =>
Expand Down Expand Up @@ -4387,6 +4409,7 @@ instance (IsProtocolVersion pv, PersistentState av pv r m) => BlockStateOperatio
bsoSetRewardAccounts = doSetRewardAccounts
bsoIsProtocolUpdateEffective = doIsProtocolUpdateEffective
bsoUpdateMissedRounds = doUpdateMissedRounds
bsoPrimeForSuspension = doPrimeForSuspension
type StateSnapshot (PersistentBlockStateMonad pv r m) = BlockStatePointers pv
bsoSnapshotState = loadPBS
bsoRollback = storePBS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ data PendingUpdates (cpv :: ChainParametersVersion) = PendingUpdates
-- | Block energy limit (CPV2 onwards).
pBlockEnergyLimitQueue :: !(HashedBufferedRefO 'PTBlockEnergyLimit cpv (UpdateQueue Energy)),
-- | Finalization committee parameters (CPV2 onwards).
pFinalizationCommitteeParametersQueue :: !(HashedBufferedRefO 'PTFinalizationCommitteeParameters cpv (UpdateQueue FinalizationCommitteeParameters))
pFinalizationCommitteeParametersQueue :: !(HashedBufferedRefO 'PTFinalizationCommitteeParameters cpv (UpdateQueue FinalizationCommitteeParameters)),
-- | Validators score parameters (CPV3 onwards).
pValidatorScoreParametersQueue :: !(HashedBufferedRefO 'PTValidatorScoreParameters cpv (UpdateQueue ValidatorScoreParameters))
}

-- | See documentation of @migratePersistentBlockState@.
Expand Down Expand Up @@ -422,6 +424,25 @@ migratePendingUpdates migration PendingUpdates{..} = withCPVConstraints (chainPa
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
StateMigrationParametersP7ToP8{} -> case pFinalizationCommitteeParametersQueue of
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
newValidatorScoreParametersQueue <- case migration of
StateMigrationParametersTrivial -> case pValidatorScoreParametersQueue of
NoParam -> return NoParam
SomeParam hbr -> SomeParam <$> migrateHashedBufferedRef (migrateUpdateQueue id) hbr
StateMigrationParametersP1P2 -> case pValidatorScoreParametersQueue of
NoParam -> return NoParam
StateMigrationParametersP2P3 -> case pValidatorScoreParametersQueue of
NoParam -> return NoParam
StateMigrationParametersP3ToP4{} -> case pValidatorScoreParametersQueue of
NoParam -> return NoParam
StateMigrationParametersP4ToP5{} -> case pValidatorScoreParametersQueue of
NoParam -> return NoParam
StateMigrationParametersP5ToP6{} -> case pValidatorScoreParametersQueue of
NoParam -> return NoParam
StateMigrationParametersP6ToP7{} -> case pValidatorScoreParametersQueue of
NoParam -> return NoParam
StateMigrationParametersP7ToP8{} -> do
(!hbr, _) <- refFlush =<< refMake emptyUpdateQueue
return (SomeParam hbr)
return $!
PendingUpdates
{ pRootKeysUpdateQueue = newRootKeys,
Expand All @@ -443,7 +464,8 @@ migratePendingUpdates migration PendingUpdates{..} = withCPVConstraints (chainPa
pTimeoutParametersQueue = newTimeoutParameters,
pMinBlockTimeQueue = newMinBlockTimeQueue,
pBlockEnergyLimitQueue = newBlockEnergyLimitQueue,
pFinalizationCommitteeParametersQueue = newFinalizationCommitteeParametersQueue
pFinalizationCommitteeParametersQueue = newFinalizationCommitteeParametersQueue,
pValidatorScoreParametersQueue = newValidatorScoreParametersQueue
}

instance
Expand Down Expand Up @@ -522,6 +544,7 @@ instance
(putMinBlockTimeQueue, newMinBlockTimeQueue) <- storeUpdate pMinBlockTimeQueue
(putBlockEnergyLimitQueue, newBlockEnergyLimitQueue) <- storeUpdate pBlockEnergyLimitQueue
(putFinalizationCommitteeParametersQueue, newFinalizationCommitteeParametersQueue) <- storeUpdate pFinalizationCommitteeParametersQueue
(putValidatorScoreParametersQueue, newValidatorScoreParametersQueue) <- storeUpdate pValidatorScoreParametersQueue
let newPU =
PendingUpdates
{ pRootKeysUpdateQueue = rkQ,
Expand All @@ -543,7 +566,8 @@ instance
pTimeoutParametersQueue = newTimeoutParametersQueue,
pMinBlockTimeQueue = newMinBlockTimeQueue,
pBlockEnergyLimitQueue = newBlockEnergyLimitQueue,
pFinalizationCommitteeParametersQueue = newFinalizationCommitteeParametersQueue
pFinalizationCommitteeParametersQueue = newFinalizationCommitteeParametersQueue,
pValidatorScoreParametersQueue = newValidatorScoreParametersQueue
}
let putPU =
pRKQ
Expand All @@ -566,6 +590,7 @@ instance
>> putMinBlockTimeQueue
>> putBlockEnergyLimitQueue
>> putFinalizationCommitteeParametersQueue
>> putValidatorScoreParametersQueue
return (putPU, newPU)
load = withCPVConstraints (chainParametersVersion @cpv) $ do
mRKQ <- label "Root keys update queue" load
Expand All @@ -588,6 +613,7 @@ instance
mMinBlockTimeQueue <- label "Minimum block time update queue" load
mBlockEnergyLimitQueue <- label "Block energy limit update queue" load
mFinalizationCommitteeParametersQueue <- label "Finalization committee parameters update queue" load
mValidatorScoreParametersQueue <- label "Validator score parameters update queue" load
return $! do
pRootKeysUpdateQueue <- mRKQ
pLevel1KeysUpdateQueue <- mL1KQ
Expand All @@ -609,6 +635,7 @@ instance
pMinBlockTimeQueue <- mMinBlockTimeQueue
pBlockEnergyLimitQueue <- mBlockEnergyLimitQueue
pFinalizationCommitteeParametersQueue <- mFinalizationCommitteeParametersQueue
pValidatorScoreParametersQueue <- mValidatorScoreParametersQueue
return PendingUpdates{..}

instance
Expand Down Expand Up @@ -638,6 +665,7 @@ instance
<*> cache pMinBlockTimeQueue
<*> cache pBlockEnergyLimitQueue
<*> cache pFinalizationCommitteeParametersQueue
<*> cache pValidatorScoreParametersQueue
where
cpv = chainParametersVersion @cpv

Expand All @@ -646,7 +674,7 @@ emptyPendingUpdates ::
forall m cpv.
(MonadBlobStore m, IsChainParametersVersion cpv) =>
m (PendingUpdates cpv)
emptyPendingUpdates = PendingUpdates <$> e <*> e <*> e <*> e <*> whenSupportedA e <*> e <*> e <*> e <*> e <*> e <*> e <*> e <*> e <*> e <*> whenSupportedA e <*> whenSupportedA e <*> whenSupportedA e <*> whenSupportedA e <*> whenSupportedA e <*> whenSupportedA e
emptyPendingUpdates = PendingUpdates <$> e <*> e <*> e <*> e <*> whenSupportedA e <*> e <*> e <*> e <*> e <*> e <*> e <*> e <*> e <*> e <*> whenSupportedA e <*> whenSupportedA e <*> whenSupportedA e <*> whenSupportedA e <*> whenSupportedA e <*> whenSupportedA e <*> whenSupportedA e
where
e :: m (HashedBufferedRef (UpdateQueue a))
e = makeHashedBufferedRef emptyUpdateQueue
Expand Down Expand Up @@ -678,6 +706,7 @@ makePersistentPendingUpdates UQ.PendingUpdates{..} = withCPVConstraints (chainPa
pMinBlockTimeQueue <- mapM (refMake <=< makePersistentUpdateQueue) _pMinBlockTimeQueue
pBlockEnergyLimitQueue <- mapM (refMake <=< makePersistentUpdateQueue) _pBlockEnergyLimitQueue
pFinalizationCommitteeParametersQueue <- mapM (refMake <=< makePersistentUpdateQueue) _pFinalizationCommitteeParametersQueue
pValidatorScoreParametersQueue <- mapM (refMake <=< makePersistentUpdateQueue) _pValidatorScoreParametersQueue
return PendingUpdates{..}

-- | Convert a persistent 'PendingUpdates' to an in-memory 'UQ.PendingUpdates'.
Expand Down Expand Up @@ -707,6 +736,7 @@ makeBasicPendingUpdates PendingUpdates{..} = withCPVConstraints (chainParameters
_pMinBlockTimeQueue <- mapM (makeBasicUpdateQueue <=< refLoad) pMinBlockTimeQueue
_pBlockEnergyLimitQueue <- mapM (makeBasicUpdateQueue <=< refLoad) pBlockEnergyLimitQueue
_pFinalizationCommitteeParametersQueue <- mapM (makeBasicUpdateQueue <=< refLoad) pFinalizationCommitteeParametersQueue
_pValidatorScoreParametersQueue <- mapM (makeBasicUpdateQueue <=< refLoad) pValidatorScoreParametersQueue
return UQ.PendingUpdates{..}

-- | Current state of updatable parameters and update queues.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ migratePoolRewardsP1 curBakers nextBakers blockCounts npEpoch npMintRate = do
{ blockCount = Map.findWithDefault 0 bid blockCounts,
transactionFeesAccrued = 0,
finalizationAwake = False,
missedRounds = conditionally hasValidatorSuspension 0
missedRounds = conditionally hasValidatorSuspension 0,
suspensionPrimed = conditionally hasValidatorSuspension False
}
(!newRef, _) <- refFlush =<< refMake bprd
return newRef
Expand Down
13 changes: 11 additions & 2 deletions concordium-consensus/src/Concordium/GlobalState/PoolRewards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ data BakerPoolRewardDetails (av :: AccountVersion) = BakerPoolRewardDetails
-- | Whether the pool contributed to a finalization proof in the reward period
finalizationAwake :: !Bool,
-- | The number of missed rounds in the reward period
missedRounds :: !(Conditionally (SupportsValidatorSuspension av) Word64)
missedRounds :: !(Conditionally (SupportsValidatorSuspension av) Word64),
-- | A flag indicating whether suspension can be triggered the next snapshot epoch.
suspensionPrimed :: !(Conditionally (SupportsValidatorSuspension av) Bool)
}
deriving (Eq, Show)

Expand All @@ -35,13 +37,15 @@ instance forall av. (IsAccountVersion av) => Serialize (BakerPoolRewardDetails a
put transactionFeesAccrued
putBool finalizationAwake
mapM_ putWord64be missedRounds
mapM_ putBool suspensionPrimed

get =
BakerPoolRewardDetails
<$> getWord64be
<*> get
<*> getBool
<*> conditionallyA (sSupportsValidatorSuspension (accountVersion @av)) get
<*> conditionallyA (sSupportsValidatorSuspension (accountVersion @av)) getBool

instance forall av. (IsAccountVersion av) => HashableTo Hash.Hash (BakerPoolRewardDetails av) where
getHash = Hash.hash . encode
Expand All @@ -55,7 +59,8 @@ emptyBakerPoolRewardDetails =
{ blockCount = 0,
transactionFeesAccrued = 0,
finalizationAwake = False,
missedRounds = conditionally (sSupportsValidatorSuspension (accountVersion @av)) 0
missedRounds = conditionally (sSupportsValidatorSuspension (accountVersion @av)) 0,
suspensionPrimed = conditionally (sSupportsValidatorSuspension (accountVersion @av)) False
}

-- | Migrate BakerPoolRewardDetails with different account versions.
Expand All @@ -70,5 +75,9 @@ migrateBakerPoolRewardDetails BakerPoolRewardDetails{..} =
conditionally
(sSupportsValidatorSuspension (accountVersion @av1))
(fromCondDef missedRounds 0),
suspensionPrimed =
conditionally
(sSupportsValidatorSuspension (accountVersion @av1))
(fromCondDef suspensionPrimed False),
..
}
Loading

0 comments on commit 6f62054

Please sign in to comment.