Skip to content

Commit 78fafbc

Browse files
committed
New query GetPoolDistr using more performant calculatePoolDistr' function
1 parent 8e35359 commit 78fafbc

File tree

9 files changed

+45
-11
lines changed

9 files changed

+45
-11
lines changed

cabal.project

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ source-repository-package
222222
source-repository-package
223223
type: git
224224
location: https://github.com/input-output-hk/cardano-ledger
225-
tag: b8a3ea89dd33b02f11c68393b67c425dbef13acd
226-
--sha256: 10bhq9zl1rjrbci26cxp8n6n9lx6jy93yqp6r3w31snfirqq4ihc
225+
tag: 7f7a8ae757a5f60ed759efed8b0bc721277b95f7
226+
--sha256: 1pzgcmlgrw8xa78mmw2sgfdawhkq9d42hr5bdhycm770053jr8h7
227227
subdir:
228228
eras/alonzo/impl
229229
eras/alonzo/test-suite

ouroboros-consensus-cardano-test/src/Test/ThreadNet/TxGen/Cardano.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ import qualified Cardano.Chain.Common as Byron
3636
import Cardano.Chain.Genesis (GeneratedSecrets (..))
3737

3838
import qualified Cardano.Ledger.Address as SL (BootstrapAddress (..))
39+
import qualified Cardano.Ledger.Core as SL (TxBody, TxOut)
3940
import qualified Cardano.Ledger.Hashes as SL
41+
import qualified Cardano.Ledger.Keys.Bootstrap as SL (makeBootstrapWitness)
4042
import qualified Cardano.Ledger.SafeHash as SL
41-
import qualified Cardano.Ledger.Core as SL (TxBody, TxOut)
4243
import qualified Cardano.Ledger.Shelley.API as SL hiding (TxBody, TxOut)
43-
import qualified Cardano.Ledger.Keys.Bootstrap as SL (makeBootstrapWitness)
4444
import qualified Cardano.Ledger.Shelley.Tx as SL (WitnessSetHKD (..))
4545
import qualified Cardano.Ledger.Shelley.UTxO as SL (makeWitnessVKey)
4646
import Cardano.Ledger.Val ((<->))

ouroboros-consensus-shelley/src/Ouroboros/Consensus/Shelley/Eras.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ import Cardano.Ledger.Mary (MaryEra)
6464
import Cardano.Ledger.Mary.Translation ()
6565
import Cardano.Ledger.Shelley (ShelleyEra)
6666
import qualified Cardano.Ledger.Shelley.API as SL
67+
import qualified Cardano.Ledger.Shelley.LedgerState as SL
6768
import qualified Cardano.Ledger.Shelley.Rules.Ledger as SL
6869
import qualified Cardano.Ledger.Shelley.Rules.Utxow as SL
69-
import qualified Cardano.Ledger.Shelley.LedgerState as SL
7070
import Cardano.Ledger.ShelleyMA ()
7171
import qualified Cardano.Protocol.TPraos.API as SL
72-
import Control.State.Transition (State, PredicateFailure)
72+
import Control.State.Transition (PredicateFailure, State)
7373
import Data.Data (Proxy (Proxy))
7474
import Ouroboros.Consensus.Ledger.SupportsMempool
7575
(WhetherToIntervene (..))

ouroboros-consensus-shelley/src/Ouroboros/Consensus/Shelley/Ledger/Block.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE DeriveAnyClass #-}
23
{-# LANGUAGE DeriveGeneric #-}
34
{-# LANGUAGE DerivingVia #-}
@@ -12,6 +13,7 @@
1213
{-# LANGUAGE TypeApplications #-}
1314
{-# LANGUAGE TypeFamilyDependencies #-}
1415
{-# LANGUAGE UndecidableSuperClasses #-}
16+
1517
module Ouroboros.Consensus.Shelley.Ledger.Block (
1618
GetHeader (..)
1719
, Header (..)
@@ -38,6 +40,7 @@ import Codec.CBOR.Decoding (Decoder)
3840
import Codec.CBOR.Encoding (Encoding)
3941
import qualified Data.ByteString.Lazy as Lazy
4042
import Data.Coerce (coerce)
43+
import Data.Sharing (FromSharedCBOR, Interns, Share)
4144
import Data.Typeable (Typeable)
4245
import GHC.Generics (Generic)
4346
import NoThunks.Class (NoThunks (..))
@@ -52,6 +55,7 @@ import Ouroboros.Consensus.Storage.Common (BinaryBlockInfo (..))
5255
import Ouroboros.Consensus.Util (ShowProxy (..), hashFromBytesShortE)
5356
import Ouroboros.Consensus.Util.Condense
5457

58+
import qualified Cardano.Ledger.Core as Core
5559
import Cardano.Ledger.Crypto (HASH)
5660
import qualified Cardano.Ledger.Era as SL (hashTxSeq)
5761
import qualified Cardano.Ledger.Shelley.API as SL
@@ -99,6 +103,8 @@ class
99103
-- Hard-fork related constraints
100104
, HasPartialConsensusConfig proto
101105
, FromCBOR (SL.PState (ProtoCrypto proto))
106+
, Share (Core.TxOut era) ~ Interns (SL.Credential 'SL.Staking (ProtoCrypto proto))
107+
, FromSharedCBOR (Core.TxOut era)
102108
) => ShelleyCompatible proto era
103109

104110
instance ShelleyCompatible proto era => ConvertRawHash (ShelleyBlock proto era) where

ouroboros-consensus-shelley/src/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ import Data.Typeable (Typeable)
4141
import GHC.Generics (Generic)
4242
import GHC.Natural (Natural)
4343
import GHC.Records
44-
import NoThunks.Class (NoThunks (..))
4544
import Lens.Micro ((^.))
45+
import NoThunks.Class (NoThunks (..))
4646

4747
import Cardano.Binary (Annotator (..), FromCBOR (..),
4848
FullByteString (..), ToCBOR (..))
@@ -64,9 +64,9 @@ import Ouroboros.Consensus.Util.Condense
6464
import Cardano.Ledger.Alonzo.PParams
6565
import Cardano.Ledger.Alonzo.Tx (totExUnits)
6666
import Cardano.Ledger.Babbage.PParams
67-
import Cardano.Ledger.Core (Tx, TxSeq, bodyTxL, sizeTxF, fromTxSeq)
68-
import qualified Cardano.Ledger.Shelley.API as SL
6967
import qualified Cardano.Ledger.Block as SL (txid)
68+
import Cardano.Ledger.Core (Tx, TxSeq, bodyTxL, fromTxSeq, sizeTxF)
69+
import qualified Cardano.Ledger.Shelley.API as SL
7070

7171
import Cardano.Ledger.Crypto (Crypto)
7272
import Ouroboros.Consensus.Shelley.Eras

ouroboros-consensus-shelley/src/Ouroboros/Consensus/Shelley/Ledger/NetworkProtocolVersion.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ data ShelleyNodeToClientVersion =
3131
-- | New queries introduced: GetRewardInfoPools
3232
| ShelleyNodeToClientVersion5
3333

34-
-- | New queries introduced: GetPoolState, GetStakeSnapshots
34+
-- | New queries introduced: GetPoolDistr, GetPoolState, GetStakeSnapshots
3535
| ShelleyNodeToClientVersion6
3636
deriving (Show, Eq, Ord, Enum, Bounded)
3737

ouroboros-consensus-shelley/src/Ouroboros/Consensus/Shelley/Ledger/Query.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ data instance BlockQuery (ShelleyBlock proto era) :: Type -> Type where
210210
-> BlockQuery (ShelleyBlock proto era)
211211
(StakeSnapshots (EraCrypto era))
212212

213+
GetPoolDistr
214+
:: Maybe (Set (SL.KeyHash 'SL.StakePool (EraCrypto era)))
215+
-> BlockQuery (ShelleyBlock proto era)
216+
(SL.PoolDistr (EraCrypto era))
217+
213218
-- WARNING: please add new queries to the end of the list and stick to this
214219
-- order in all other pattern matches on queries. This helps in particular
215220
-- with the en/decoders, as we want the CBOR tags to be ordered.
@@ -330,6 +335,9 @@ instance (ShelleyCompatible proto era, ProtoCrypto proto ~ crypto) => QueryLedge
330335
, ssSetTotal = getAllStake _pstakeSet
331336
, ssGoTotal = getAllStake _pstakeGo
332337
}
338+
GetPoolDistr mPoolIds ->
339+
let stakeSet = SL._pstakeSet . SL.esSnapshots $ getEpochState st in
340+
SL.calculatePoolDistr' (maybe (const True) (flip Set.member) mPoolIds) stakeSet
333341
where
334342
lcfg = configLedger $ getExtLedgerCfg cfg
335343
globals = shelleyLedgerGlobals lcfg
@@ -451,6 +459,13 @@ instance SameDepIndex (BlockQuery (ShelleyBlock proto era)) where
451459
= Nothing
452460
sameDepIndex (GetStakeSnapshots _) _
453461
= Nothing
462+
sameDepIndex (GetPoolDistr poolids) (GetPoolDistr poolids')
463+
| poolids == poolids'
464+
= Just Refl
465+
| otherwise
466+
= Nothing
467+
sameDepIndex (GetPoolDistr _) _
468+
= Nothing
454469

455470
deriving instance Eq (BlockQuery (ShelleyBlock proto era) result)
456471
deriving instance Show (BlockQuery (ShelleyBlock proto era) result)
@@ -478,6 +493,7 @@ instance ShelleyCompatible proto era => ShowQuery (BlockQuery (ShelleyBlock prot
478493
GetRewardInfoPools -> show
479494
GetPoolState {} -> show
480495
GetStakeSnapshots {} -> show
496+
GetPoolDistr {} -> show
481497

482498
-- | Is the given query supported by the given 'ShelleyNodeToClientVersion'?
483499
querySupportedVersion :: BlockQuery (ShelleyBlock proto era) result -> ShelleyNodeToClientVersion -> Bool
@@ -503,6 +519,7 @@ querySupportedVersion = \case
503519
GetRewardInfoPools -> (>= v5)
504520
GetPoolState {} -> (>= v6)
505521
GetStakeSnapshots {} -> (>= v6)
522+
GetPoolDistr {} -> (>= v6)
506523
-- WARNING: when adding a new query, a new @ShelleyNodeToClientVersionX@
507524
-- must be added. See #2830 for a template on how to do this.
508525
where
@@ -592,6 +609,8 @@ encodeShelleyQuery query = case query of
592609
CBOR.encodeListLen 2 <> CBOR.encodeWord8 19 <> toCBOR poolids
593610
GetStakeSnapshots poolId ->
594611
CBOR.encodeListLen 2 <> CBOR.encodeWord8 20 <> toCBOR poolId
612+
GetPoolDistr poolids ->
613+
CBOR.encodeListLen 2 <> CBOR.encodeWord8 21 <> toCBOR poolids
595614

596615
decodeShelleyQuery ::
597616
ShelleyBasedEra era
@@ -621,6 +640,7 @@ decodeShelleyQuery = do
621640
(1, 18) -> return $ SomeSecond GetRewardInfoPools
622641
(2, 19) -> SomeSecond . GetPoolState <$> fromCBOR
623642
(2, 20) -> SomeSecond . GetStakeSnapshots <$> fromCBOR
643+
(2, 21) -> SomeSecond . GetPoolDistr <$> fromCBOR
624644
_ -> fail $
625645
"decodeShelleyQuery: invalid (len, tag): (" <>
626646
show len <> ", " <> show tag <> ")"
@@ -650,6 +670,7 @@ encodeShelleyResult query = case query of
650670
GetRewardInfoPools -> toCBOR
651671
GetPoolState {} -> toCBOR
652672
GetStakeSnapshots {} -> toCBOR
673+
GetPoolDistr {} -> toCBOR
653674

654675
decodeShelleyResult ::
655676
ShelleyCompatible proto era
@@ -677,6 +698,7 @@ decodeShelleyResult query = case query of
677698
GetRewardInfoPools -> fromCBOR
678699
GetPoolState {} -> fromCBOR
679700
GetStakeSnapshots {} -> fromCBOR
701+
GetPoolDistr {} -> fromCBOR
680702

681703
-- | The stake snapshot returns information about the mark, set, go ledger snapshots for a pool,
682704
-- plus the total active stake for each snapshot that can be used in a 'sigma' calculation.

ouroboros-consensus/docs/interface-CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ may appear out of chronological order.
5656
The internals of each entry are organized similar to
5757
https://keepachangelog.com/en/1.1.0/, adapted to our plan explained above.
5858

59+
## Circa 2022-08-08
60+
61+
### Added
62+
63+
- `GetPoolDistr`: Get the pool distribution for the given stake pool ids
64+
5965
## Circa 2022-07-26
6066

6167
### Added

ouroboros-network/src/Ouroboros/Network/NodeToClient/Version.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ data NodeToClientVersion
3535
| NodeToClientV_13
3636
-- ^ enabled @CardanoNodeToClientVersion9@, i.e., Babbage
3737
| NodeToClientV_14
38-
-- ^ added @GetPoolState, @GetSnapshots
38+
-- ^ added @GetPoolDistr, @GetPoolState, @GetSnapshots
3939
deriving (Eq, Ord, Enum, Bounded, Show, Typeable)
4040

4141
-- | We set 16ths bit to distinguish `NodeToNodeVersion` and

0 commit comments

Comments
 (0)