@@ -21,6 +21,8 @@ import Data.Aeson.Encoding (pairs)
21
21
import Data.Aeson.Types (Encoding , FromJSON (.. ), KeyValue ((.=) ), Options (constructorTagModifier ), Parser , ToJSON (.. ), Value (.. ), genericParseJSON , object , typeMismatch , withObject , (.:) )
22
22
import Data.Default (Default (.. ))
23
23
import Data.Maybe (catMaybes )
24
+ import Data.Set (Set )
25
+ import qualified Data.Set as Set
24
26
import Data.Text (Text )
25
27
import Data.Word
26
28
import Data.Yaml (ParseException )
@@ -59,11 +61,36 @@ data RelayStrategy
59
61
| RequestFromAll
60
62
deriving (Show , Eq , Generic )
61
63
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
+
62
88
data Config = Config
63
89
{ relayStrategy :: RelayStrategy
64
90
, tcpCongestionControl :: Bool
65
91
, multiplexMiniProtocols :: Bool
66
92
, treatBlocksAsFull :: Bool
93
+ , cleanupPolicies :: CleanupPolicies
67
94
, leiosStageLengthSlots :: Word
68
95
, leiosStageActiveVotingSlots :: Word
69
96
, leiosVoteSendRecvStages :: Bool
@@ -129,6 +156,7 @@ instance Default Config where
129
156
, tcpCongestionControl = True
130
157
, multiplexMiniProtocols = True
131
158
, treatBlocksAsFull = False
159
+ , cleanupPolicies = def
132
160
, leiosStageLengthSlots = 20
133
161
, leiosStageActiveVotingSlots = 1
134
162
, leiosVoteSendRecvStages = False
@@ -198,6 +226,7 @@ configToKVsWith getter cfg =
198
226
, get @ " tcpCongestionControl" getter cfg
199
227
, get @ " multiplexMiniProtocols" getter cfg
200
228
, get @ " treatBlocksAsFull" getter cfg
229
+ , get @ " cleanupPolicies" getter cfg
201
230
, get @ " leiosStageLengthSlots" getter cfg
202
231
, get @ " leiosStageActiveVotingSlots" getter cfg
203
232
, get @ " leiosVoteSendRecvStages" getter cfg
@@ -277,6 +306,7 @@ instance FromJSON Config where
277
306
tcpCongestionControl <- parseFieldOrDefault @ Config @ " tcpCongestionControl" obj
278
307
multiplexMiniProtocols <- parseFieldOrDefault @ Config @ " multiplexMiniProtocols" obj
279
308
treatBlocksAsFull <- parseFieldOrDefault @ Config @ " treatBlocksAsFull" obj
309
+ cleanupPolicies <- parseFieldOrDefault @ Config @ " cleanupPolicies" obj
280
310
leiosStageLengthSlots <- parseFieldOrDefault @ Config @ " leiosStageLengthSlots" obj
281
311
leiosStageActiveVotingSlots <- parseFieldOrDefault @ Config @ " leiosStageActiveVotingSlots" obj
282
312
leiosVoteSendRecvStages <- parseFieldOrDefault @ Config @ " leiosVoteSendRecvStages" obj
@@ -383,13 +413,32 @@ instance FromJSON Distribution where
383
413
where
384
414
orUnknown k v = k v <|> pure (Unknown v)
385
415
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
+
386
428
defaultEnumOptions :: Options
387
429
defaultEnumOptions =
388
430
defaultOptions
389
431
{ constructorTagModifier = camelToKebab
390
432
, allNullaryToStringTag = True
391
433
}
392
434
435
+ instance FromJSON CleanupPolicy where
436
+ parseJSON = genericParseJSON defaultEnumOptions
437
+
438
+ instance ToJSON CleanupPolicy where
439
+ toJSON = genericToJSON defaultEnumOptions
440
+ toEncoding = genericToEncoding defaultEnumOptions
441
+
393
442
instance FromJSON DiffusionStrategy where
394
443
parseJSON = genericParseJSON defaultEnumOptions
395
444
0 commit comments