-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LedgerSupportsPeerSelection: return peers registered in ledger state
Fixes #2535. For Shelley, this returns the stake pool relays ordered by descending stake.
- Loading branch information
Showing
16 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
ouroboros-consensus-shelley/src/Ouroboros/Consensus/Shelley/Ledger/PeerSelection.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
module Ouroboros.Consensus.Shelley.Ledger.PeerSelection () where | ||
|
||
import Data.Bifunctor (second) | ||
import Data.Foldable (toList) | ||
import Data.List (sortOn) | ||
import Data.Map.Strict (Map) | ||
import qualified Data.Map.Strict as Map | ||
import Data.Ord (Down (..)) | ||
|
||
import Ouroboros.Consensus.Ledger.SupportsPeerSelection | ||
|
||
import qualified Shelley.Spec.Ledger.Delegation.Certificates as SL | ||
import qualified Shelley.Spec.Ledger.Keys as SL | ||
import qualified Shelley.Spec.Ledger.LedgerState as SL | ||
import qualified Shelley.Spec.Ledger.TxData as SL | ||
|
||
import Ouroboros.Consensus.Shelley.Ledger.Block | ||
import Ouroboros.Consensus.Shelley.Ledger.Ledger | ||
|
||
instance LedgerSupportsPeerSelection (ShelleyBlock c) where | ||
getPeers ShelleyLedgerState { shelleyState } = concat | ||
[ Map.findWithDefault [] stakePool poolDomainAddresses | ||
| stakePool <- orderByStake poolDistr | ||
] | ||
where | ||
poolDistr :: SL.PoolDistr c | ||
poolDistr = SL.nesPd shelleyState | ||
|
||
-- | Sort stake pools by descending stake | ||
orderByStake :: SL.PoolDistr c -> [SL.KeyHash 'SL.StakePool c] | ||
orderByStake = | ||
map fst | ||
. sortOn (Down . snd) | ||
. map (second SL.individualPoolStake) | ||
. Map.toList | ||
. SL.unPoolDistr | ||
|
||
-- | Note that a stake pool can have multiple registered relays | ||
poolDomainAddresses :: Map (SL.KeyHash 'SL.StakePool c) [DomainAddress] | ||
poolDomainAddresses = | ||
Map.map (map relayToDomainAddress . toList . SL._poolRelays) | ||
. SL._pParams | ||
. SL._pstate | ||
. SL._delegationState | ||
. SL.esLState | ||
. SL.nesEs | ||
$ shelleyState | ||
|
||
relayToDomainAddress :: SL.StakePoolRelay -> DomainAddress | ||
relayToDomainAddress = undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
ouroboros-consensus/src/Ouroboros/Consensus/HardFork/Combinator/Ledger/PeerSelection.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
module Ouroboros.Consensus.HardFork.Combinator.Ledger.PeerSelection () where | ||
|
||
import Data.SOP.Strict | ||
|
||
import Ouroboros.Consensus.Ledger.SupportsPeerSelection | ||
|
||
import Ouroboros.Consensus.HardFork.Combinator.Abstract | ||
import Ouroboros.Consensus.HardFork.Combinator.Basics | ||
import Ouroboros.Consensus.HardFork.Combinator.Ledger () | ||
import qualified Ouroboros.Consensus.HardFork.Combinator.State as State | ||
|
||
instance CanHardFork xs => LedgerSupportsPeerSelection (HardForkBlock xs) where | ||
getPeers = | ||
hcollapse | ||
. hcmap proxySingle (K . getPeers) | ||
. State.tip | ||
. hardForkLedgerStatePerEra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
ouroboros-consensus/src/Ouroboros/Consensus/Ledger/SupportsPeerSelection.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Ouroboros.Consensus.Ledger.SupportsPeerSelection ( | ||
LedgerSupportsPeerSelection (..) | ||
-- * Re-exports for convenience | ||
, DomainAddress (..) | ||
, Domain | ||
, PortNumber | ||
) where | ||
|
||
import Ouroboros.Network.PeerSelection.RootPeersDNS (Domain, | ||
DomainAddress (..), PortNumber) | ||
|
||
import Ouroboros.Consensus.Ledger.Abstract (LedgerState) | ||
|
||
class LedgerSupportsPeerSelection blk where | ||
-- | Return peers registered in the ledger ordered by descending some | ||
-- /preference/. | ||
-- | ||
-- For example, for Shelley, this should return the stake pool relays that | ||
-- have been registered. The /preference/ should be the /stake/, i.e., the | ||
-- stake pools with the most stake will come first in the list. | ||
getPeers :: LedgerState blk -> [DomainAddress] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters