|
| 1 | +{-# LANGUAGE BangPatterns #-} |
| 2 | +{-# LANGUAGE DeriveAnyClass #-} |
| 3 | +{-# LANGUAGE DeriveGeneric #-} |
| 4 | +{-# LANGUAGE DuplicateRecordFields #-} |
| 5 | +{-# LANGUAGE FlexibleContexts #-} |
| 6 | +{-# LANGUAGE GADTs #-} |
| 7 | +{-# LANGUAGE NamedFieldPuns #-} |
| 8 | +{-# LANGUAGE RecordWildCards #-} |
| 9 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 10 | +{-# LANGUAGE TupleSections #-} |
| 11 | +{-# LANGUAGE TypeApplications #-} |
| 12 | +{-# LANGUAGE NoFieldSelectors #-} |
| 13 | + |
| 14 | +module Diffusion |
| 15 | +where |
| 16 | + |
| 17 | +import Control.Monad |
| 18 | +import Data.Aeson |
| 19 | +import Data.Bifunctor |
| 20 | +import Data.IntMap (IntMap) |
| 21 | +import qualified Data.IntMap as IMap |
| 22 | +import qualified Data.List as List |
| 23 | +import Data.Map (Map) |
| 24 | +import qualified Data.Map.Strict as Map |
| 25 | +import Data.Maybe (fromMaybe, listToMaybe) |
| 26 | +import Data.Traversable |
| 27 | +import GHC.Generics |
| 28 | +import LeiosProtocol.Common hiding (Point) |
| 29 | +import qualified PraosProtocol.Common.Chain as Chain |
| 30 | +import PraosProtocol.ExamplesPraosP2P () |
| 31 | +import SimTypes |
| 32 | + |
| 33 | +data DiffusionEntry id = DiffusionEntry |
| 34 | + { block_id :: !id |
| 35 | + , node_id :: !Int |
| 36 | + , created :: !DiffTime |
| 37 | + , adoptions :: ![(NodeId, DiffTime)] |
| 38 | + } |
| 39 | + deriving (Generic, ToJSON, FromJSON) |
| 40 | + |
| 41 | +data DiffusionData id = DiffusionData |
| 42 | + { entries :: ![DiffusionEntry id] |
| 43 | + , latency_per_stake :: ![LatencyPerStake id] |
| 44 | + -- ^ adoption latency, counted from slot start. |
| 45 | + , average_latencies :: !(Map.Map Double DiffTime) |
| 46 | + -- ^ map from stake fraction to average adoption latency |
| 47 | + } |
| 48 | + deriving (Generic, ToJSON, FromJSON) |
| 49 | + |
| 50 | +data LatencyPerStake id = LatencyPerStake |
| 51 | + { block_id :: !id |
| 52 | + , latencies :: ![(Maybe DiffTime, Double)] |
| 53 | + } |
| 54 | + deriving (Generic, ToJSON, FromJSON) |
| 55 | + |
| 56 | +diffusionEntryToLatencyPerStake :: Map NodeId StakeFraction -> DiffusionEntry id -> LatencyPerStake id |
| 57 | +diffusionEntryToLatencyPerStake stakes DiffusionEntry{..} = |
| 58 | + LatencyPerStake |
| 59 | + { block_id |
| 60 | + , latencies = bin $ diffusionLatencyPerStakeFraction stakes slotStart adoptions |
| 61 | + } |
| 62 | + where |
| 63 | + slotStart = fromIntegral @Integer $ floor created |
| 64 | + bins = [0.50, 0.8, 0.9, 0.92, 0.94, 0.96, 0.98, 1] |
| 65 | + bin xs = map (\b -> (,b) $ fst <$> listToMaybe (dropWhile (\(_, x) -> x < b) xs)) bins |
| 66 | + |
| 67 | +diffusionLatencyPerStakeFraction :: |
| 68 | + Map NodeId StakeFraction -> |
| 69 | + DiffTime -> |
| 70 | + [(NodeId, DiffTime)] -> |
| 71 | + [(DiffTime, Double)] |
| 72 | +diffusionLatencyPerStakeFraction stakes t0 = |
| 73 | + snd |
| 74 | + . mapAccumL h 0 |
| 75 | + . map (first (stakes Map.!)) |
| 76 | + . reverse |
| 77 | + where |
| 78 | + h s (StakeFraction ns, t) = |
| 79 | + let |
| 80 | + !s' = s + ns |
| 81 | + !latency = t - t0 |
| 82 | + in |
| 83 | + (s', (latency, s')) |
| 84 | + |
| 85 | +stableChainHashes :: HasHeader a => IntMap (Chain a) -> [HeaderHash a] |
| 86 | +stableChainHashes chains = |
| 87 | + let stable_chain = fromMaybe Genesis $ do |
| 88 | + guard $ not $ IMap.null chains |
| 89 | + pure $ List.foldl1' aux (IMap.elems chains) |
| 90 | + aux c1 c2 = fromMaybe Genesis $ do |
| 91 | + p <- Chain.intersectChains c1 c2 |
| 92 | + Chain.rollback p c1 |
| 93 | + in map blockHash $ Chain.toNewestFirst stable_chain |
| 94 | + |
| 95 | +diffusionDataFromMap :: |
| 96 | + Map NodeId StakeFraction -> |
| 97 | + Map id (msg, NodeId, Time, [(NodeId, Time)]) -> |
| 98 | + DiffusionData id |
| 99 | +diffusionDataFromMap stakes arrivals = DiffusionData{..} |
| 100 | + where |
| 101 | + entries = |
| 102 | + [ DiffusionEntry{..} |
| 103 | + | (block_id, (_, NodeId node_id, Time created, ts)) <- Map.toList arrivals |
| 104 | + , let adoptions = map (second (\(Time t) -> t)) ts |
| 105 | + ] |
| 106 | + latency_per_stake = map (diffusionEntryToLatencyPerStake stakes) entries |
| 107 | + avg ts = sum ts / fromIntegral (length ts) |
| 108 | + average_latencies = |
| 109 | + Map.map avg $ |
| 110 | + Map.fromListWith |
| 111 | + (++) |
| 112 | + [ (p, [d]) |
| 113 | + | l <- latency_per_stake |
| 114 | + , (Just d, p) <- l.latencies |
| 115 | + ] |
0 commit comments