Skip to content

Commit

Permalink
Use block download when ranking peers
Browse files Browse the repository at this point in the history
Use both who ever gave us a header first, and who ever provided us with
the block first when ranking peers in deadline mode. For larger blocks
forged far away it is likely that we learned of the header
from a node on another continent, but the node from which we first
downloaded the block is much closer. This change gives both nodes
credit.
  • Loading branch information
karknu committed Nov 30, 2021
1 parent b9e0b9d commit 863dc39
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
9 changes: 6 additions & 3 deletions ouroboros-network/src/Ouroboros/Network/Diffusion/Policies.hs
Expand Up @@ -77,10 +77,13 @@ simplePeerSelectionPolicy rngVar getChurnMode metrics = PeerSelectionPolicy {
hotDemotionPolicy _ _ _ available pickNum = do
mode <- getChurnMode
scores <- case mode of
ChurnModeNormal ->
upstreamyness <$> getHeaderMetrics metrics
ChurnModeNormal -> do
hup <- upstreamyness <$> getHeaderMetrics metrics
bup <- fetchynessBlocks <$> getFetchedMetrics metrics
return $ Map.unionWith (+) hup bup

ChurnModeBulkSync ->
fetchyness <$> getFetchedMetrics metrics
fetchynessBytes <$> getFetchedMetrics metrics
available' <- addRand available (,)
return $ Set.fromList
. map fst
Expand Down
Expand Up @@ -145,11 +145,11 @@ upstreamyness = Pq.fold' count Map.empty

-- Returns a Map which counts the number of bytes downloaded
-- for a given peer.
fetchyness
fetchynessBytes
:: forall p. ( Ord p )
=> SlotMetric (p, SizeInBytes)
-> Map p Int
fetchyness = Pq.fold' count Map.empty
fetchynessBytes = Pq.fold' count Map.empty
where
count :: Int
-> SlotNo
Expand All @@ -163,5 +163,24 @@ fetchyness = Pq.fold' count Map.empty
fn Nothing = Just $ fromIntegral bytes
fn (Just oldBytes) = Just $! oldBytes + fromIntegral bytes

-- Returns a Map which counts the number of times a given peer
-- was the first we downloaded a block from.
fetchynessBlocks
:: forall p. ( Ord p )
=> SlotMetric (p, SizeInBytes)
-> Map p Int
fetchynessBlocks = Pq.fold' count Map.empty
where
count :: Int
-> SlotNo
-> ((p, SizeInBytes), Time)
-> Map p Int
-> Map p Int
count _ _ ((peer, _),_) m =
Map.alter fn peer m
where
fn :: Maybe Int -> Maybe Int
fn Nothing = Just 1
fn (Just c) = Just $! c + 1


Expand Up @@ -182,9 +182,11 @@ prop_hotToWarmM ArbitraryPolicyArguments{..} seed = do
-> m Property
noneWorse metrics pickedSet = do
scores <- atomically $ case apaChurnMode of
ChurnModeNormal -> upstreamyness <$>
getHeaderMetrics metrics
ChurnModeBulkSync -> fetchyness <$>
ChurnModeNormal -> do
hup <- upstreamyness <$> getHeaderMetrics metrics
bup <- fetchynessBlocks <$> getFetchedMetrics metrics
return $ Map.unionWith (+) hup bup
ChurnModeBulkSync -> fetchynessBytes <$>
getFetchedMetrics metrics
let (picked, notPicked) = Map.partitionWithKey fn scores
maxPicked = maximum $ Map.elems picked
Expand Down

0 comments on commit 863dc39

Please sign in to comment.