Skip to content

Commit 3d4c098

Browse files
Wen Kokkewenkokke
Wen Kokke
authored andcommitted
feat(config,simulation): add cleanup-policies configuration
1 parent c2592ed commit 3d4c098

File tree

7 files changed

+113
-11
lines changed

7 files changed

+113
-11
lines changed

data/simulation/config.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export interface Config {
2020
* - EBs reference `ceil (ib-generation-probability * leios-stage-length-slots)` IBs.
2121
* Only supported by Haskell simulation. */
2222
"treat-blocks-as-full": boolean;
23+
/** Only supported by Haskell simulation. */
24+
"cleanup-policies": CleanupPolicies;
2325

2426
// Leios Protocol Configuration
2527
"leios-stage-length-slots": bigint;
@@ -118,6 +120,15 @@ export interface Config {
118120
"cert-size-bytes-per-node": bigint;
119121
}
120122

123+
export type CleanupPolicies = "all" | CleanupPolicy[];
124+
125+
export type CleanupPolicy =
126+
| "cleanup-expired-ib"
127+
| "cleanup-expired-uncertified-eb"
128+
| "cleanup-expired-unadopted-eb"
129+
| "cleanup-expired-vote"
130+
| "cleanup-expired-certificate";
131+
121132
export type Distribution =
122133
| NormalDistribution
123134
| ExpDistribution

data/simulation/config.default.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tcp-congestion-control: true
1616
multiplex-mini-protocols: true
1717
simulate-transactions: true
1818
treat-blocks-as-full: false
19+
cleanup-policies: ["cleanup-expired-vote"]
1920

2021
################################################################################
2122
# Leios Protocol Configuration

data/simulation/config.schema.json

+28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"definitions": {
4+
"CleanupPolicies": {
5+
"anyOf": [
6+
{
7+
"items": {
8+
"$ref": "#/definitions/CleanupPolicy"
9+
},
10+
"type": "array"
11+
},
12+
{
13+
"const": "all",
14+
"type": "string"
15+
}
16+
]
17+
},
18+
"CleanupPolicy": {
19+
"enum": [
20+
"cleanup-expired-certificate",
21+
"cleanup-expired-ib",
22+
"cleanup-expired-unadopted-eb",
23+
"cleanup-expired-uncertified-eb",
24+
"cleanup-expired-vote"
25+
],
26+
"type": "string"
27+
},
428
"ConstantDistribution": {
529
"properties": {
630
"distribution": {
@@ -107,6 +131,10 @@
107131
"cert-validation-cpu-time-ms-per-node": {
108132
"type": "number"
109133
},
134+
"cleanup-policies": {
135+
"$ref": "#/definitions/CleanupPolicies",
136+
"description": "Only supported by Haskell simulation."
137+
},
110138
"eb-diffusion-max-bodies-to-request": {
111139
"additionalProperties": false,
112140
"description": "Only supported by Haskell simulation.",

simulation/src/LeiosProtocol/Config.hs

+49
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import Data.Aeson.Encoding (pairs)
2121
import Data.Aeson.Types (Encoding, FromJSON (..), KeyValue ((.=)), Options (constructorTagModifier), Parser, ToJSON (..), Value (..), genericParseJSON, object, typeMismatch, withObject, (.:))
2222
import Data.Default (Default (..))
2323
import Data.Maybe (catMaybes)
24+
import Data.Set (Set)
25+
import qualified Data.Set as Set
2426
import Data.Text (Text)
2527
import Data.Word
2628
import Data.Yaml (ParseException)
@@ -59,11 +61,36 @@ data RelayStrategy
5961
| RequestFromAll
6062
deriving (Show, Eq, Generic)
6163

64+
-- | Data expiration.
65+
newtype CleanupPolicies = CleanupPolicies (Set CleanupPolicy)
66+
deriving (Show, Eq, Ord, Monoid, Semigroup)
67+
68+
-- | Data types for expiration policy.
69+
data CleanupPolicy
70+
= CleanupExpiredIb
71+
| CleanupExpiredUncertifiedEb
72+
| CleanupExpiredUnadoptedEb
73+
| CleanupExpiredVote
74+
| CleanupExpiredCertificate
75+
deriving (Show, Eq, Ord, Generic, Bounded, Enum)
76+
77+
isEnabledIn :: CleanupPolicy -> CleanupPolicies -> Bool
78+
isEnabledIn cleanupPolicy (CleanupPolicies cleanupPolicies) =
79+
cleanupPolicy `Set.member` cleanupPolicies
80+
81+
instance Default CleanupPolicies where
82+
def :: CleanupPolicies
83+
def = CleanupPolicies $ Set.fromList [CleanupExpiredVote]
84+
85+
allCleanupPolicies :: CleanupPolicies
86+
allCleanupPolicies = CleanupPolicies $ Set.fromList [minBound .. maxBound]
87+
6288
data Config = Config
6389
{ relayStrategy :: RelayStrategy
6490
, tcpCongestionControl :: Bool
6591
, multiplexMiniProtocols :: Bool
6692
, treatBlocksAsFull :: Bool
93+
, cleanupPolicies :: CleanupPolicies
6794
, leiosStageLengthSlots :: Word
6895
, leiosStageActiveVotingSlots :: Word
6996
, leiosVoteSendRecvStages :: Bool
@@ -129,6 +156,7 @@ instance Default Config where
129156
, tcpCongestionControl = True
130157
, multiplexMiniProtocols = True
131158
, treatBlocksAsFull = False
159+
, cleanupPolicies = def
132160
, leiosStageLengthSlots = 20
133161
, leiosStageActiveVotingSlots = 1
134162
, leiosVoteSendRecvStages = False
@@ -198,6 +226,7 @@ configToKVsWith getter cfg =
198226
, get @"tcpCongestionControl" getter cfg
199227
, get @"multiplexMiniProtocols" getter cfg
200228
, get @"treatBlocksAsFull" getter cfg
229+
, get @"cleanupPolicies" getter cfg
201230
, get @"leiosStageLengthSlots" getter cfg
202231
, get @"leiosStageActiveVotingSlots" getter cfg
203232
, get @"leiosVoteSendRecvStages" getter cfg
@@ -277,6 +306,7 @@ instance FromJSON Config where
277306
tcpCongestionControl <- parseFieldOrDefault @Config @"tcpCongestionControl" obj
278307
multiplexMiniProtocols <- parseFieldOrDefault @Config @"multiplexMiniProtocols" obj
279308
treatBlocksAsFull <- parseFieldOrDefault @Config @"treatBlocksAsFull" obj
309+
cleanupPolicies <- parseFieldOrDefault @Config @"cleanupPolicies" obj
280310
leiosStageLengthSlots <- parseFieldOrDefault @Config @"leiosStageLengthSlots" obj
281311
leiosStageActiveVotingSlots <- parseFieldOrDefault @Config @"leiosStageActiveVotingSlots" obj
282312
leiosVoteSendRecvStages <- parseFieldOrDefault @Config @"leiosVoteSendRecvStages" obj
@@ -383,13 +413,32 @@ instance FromJSON Distribution where
383413
where
384414
orUnknown k v = k v <|> pure (Unknown v)
385415

416+
instance FromJSON CleanupPolicies where
417+
parseJSON cleanupPolicies@(Array _) =
418+
CleanupPolicies . Set.fromList <$> parseJSONList cleanupPolicies
419+
parseJSON (String cleanupPolicies)
420+
| cleanupPolicies == "all" = pure allCleanupPolicies
421+
parseJSON v = typeMismatch "CleanupPolicies" v
422+
423+
instance ToJSON CleanupPolicies where
424+
toJSON cleanupPolicies@(CleanupPolicies cleanupPolicySet)
425+
| cleanupPolicies == allCleanupPolicies = String "all"
426+
| otherwise = toJSONList $ Set.toAscList cleanupPolicySet
427+
386428
defaultEnumOptions :: Options
387429
defaultEnumOptions =
388430
defaultOptions
389431
{ constructorTagModifier = camelToKebab
390432
, allNullaryToStringTag = True
391433
}
392434

435+
instance FromJSON CleanupPolicy where
436+
parseJSON = genericParseJSON defaultEnumOptions
437+
438+
instance ToJSON CleanupPolicy where
439+
toJSON = genericToJSON defaultEnumOptions
440+
toEncoding = genericToEncoding defaultEnumOptions
441+
393442
instance FromJSON DiffusionStrategy where
394443
parseJSON = genericParseJSON defaultEnumOptions
395444

simulation/src/LeiosProtocol/Short.hs

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ data LeiosConfig = forall p. IsPipeline p => LeiosConfig
101101
-- ^ prefix of the voting stage where new votes are generated, <= sliceLength.
102102
, maxEndorseBlockAgeSlots :: Int
103103
-- ^ maximum age of a certified endorsement block before it expires
104+
, cleanupPolicies :: CleanupPolicies
105+
-- ^ active cleanup policies
104106
, votingFrequencyPerStage :: Double
105107
, voteSendStage :: Stage p
106108
, votesForCertificate :: Int
@@ -130,6 +132,7 @@ convertConfig disk =
130132
, inputBlockFrequencyPerSlot = disk.ibGenerationProbability
131133
, endorseBlockFrequencyPerStage = disk.ebGenerationProbability
132134
, maxEndorseBlockAgeSlots = fromIntegral disk.ebMaxAgeSlots
135+
, cleanupPolicies = disk.cleanupPolicies
133136
, activeVotingStageLength = fromIntegral disk.leiosStageActiveVotingSlots
134137
, votingFrequencyPerStage = disk.voteGenerationProbability
135138
, votesForCertificate = fromIntegral disk.voteThreshold
@@ -257,6 +260,7 @@ delaysAndSizesAsFull cfg@LeiosConfig{pipeline, voteSendStage} =
257260
, inputBlockFrequencyPerSlot = cfg.inputBlockFrequencyPerSlot
258261
, endorseBlockFrequencyPerStage = cfg.endorseBlockFrequencyPerStage
259262
, maxEndorseBlockAgeSlots = cfg.maxEndorseBlockAgeSlots
263+
, cleanupPolicies = cfg.cleanupPolicies
260264
, activeVotingStageLength = cfg.activeVotingStageLength
261265
, votingFrequencyPerStage = cfg.votingFrequencyPerStage
262266
, voteSendStage = voteSendStage

simulation/src/LeiosProtocol/Short/Node.hs

+12-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import PraosProtocol.BlockFetch (
5454
import qualified PraosProtocol.Common.Chain as Chain
5555
import qualified PraosProtocol.PraosNode as PraosNode
5656
import STMCompat
57-
import SimTypes (NodeId (..), cpuTask)
57+
import SimTypes (cpuTask)
5858
import System.Random
5959
import Text.Printf (printf)
6060

@@ -441,10 +441,17 @@ leiosNode tracer cfg followers peers = do
441441
-- TODO: expiration times to be decided. At least need EB/IBs to be
442442
-- around long enough to compute ledger state if they end in RB.
443443
let pruningThreads =
444-
[ pruneExpiredVotes tracer cfg leiosState
445-
, pruneExpiredUnadoptedEBs tracer cfg leiosState
446-
, pruneExpiredUncertifiedEBs tracer cfg leiosState
447-
]
444+
concat
445+
[ [ pruneExpiredVotes tracer cfg leiosState
446+
| CleanupExpiredVote `isEnabledIn` cfg.leios.cleanupPolicies
447+
]
448+
, [ pruneExpiredUncertifiedEBs tracer cfg leiosState
449+
| CleanupExpiredUncertifiedEb `isEnabledIn` cfg.leios.cleanupPolicies
450+
]
451+
, [ pruneExpiredUnadoptedEBs tracer cfg leiosState
452+
| CleanupExpiredUnadoptedEb `isEnabledIn` cfg.leios.cleanupPolicies
453+
]
454+
]
448455

449456
return $
450457
concat

simulation/src/LeiosProtocol/Short/Sim.hs

+8-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Control.Tracer as Tracer (
2929
import Data.Aeson
3030
import Data.Aeson.Encoding (pair)
3131
import Data.Coerce
32+
import Data.Default (Default (..))
3233
import Data.Map.Strict (Map)
3334
import qualified Data.Map.Strict as Map
3435
import Data.Maybe
@@ -331,13 +332,14 @@ traceRelayLink1 connectionOptions =
331332
let leiosConfig =
332333
LeiosConfig
333334
{ praos = praosConfig
334-
, sliceLength = 5 -- matching the interval between RBs
335-
, -- \^ measured in slots, also stage length in Short leios.
336-
inputBlockFrequencyPerSlot = 5
337-
, -- \^ expected InputBlock generation rate per slot.
335+
, -- measured in slots, also stage length in Short leios.
336+
sliceLength = 5 -- matching the interval between RBs
337+
-- expected InputBlock generation rate per slot.
338+
, inputBlockFrequencyPerSlot = 5
339+
, -- expected EndorseBlock generation rate per stage, at most one per _node_ in each (pipeline, stage).
338340
endorseBlockFrequencyPerStage = 4
339-
, -- \^ expected EndorseBlock generation rate per stage, at most one per _node_ in each (pipeline, stage).
340-
activeVotingStageLength = 1
341+
, cleanupPolicies = def
342+
, activeVotingStageLength = 1
341343
, pipeline = SingSingleVote
342344
, voteSendStage = Vote
343345
, votingFrequencyPerStage = 4

0 commit comments

Comments
 (0)