Skip to content

Commit ff3a4f1

Browse files
committed
Get AdaPots from leger events
Instead of using the ledger. This changes semantically the values of the DB, so it's a c level breaking change. A migration is impossible to cover these changes. Fixes #1258.
1 parent f73d541 commit ff3a4f1

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

cardano-db-sync/src/Cardano/DbSync/Default.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ insertLedgerEvents syncEnv currentEpochNo@(EpochNo curEpoch) =
238238
lift $ adjustEpochRewards tracer ntw cache e rwd creds
239239
LedgerTotalRewards _e rwd -> do
240240
lift $ validateEpochRewards tracer ntw (subFromCurrentEpoch 2) currentEpochNo rwd
241+
LedgerAdaPots _ ->
242+
pure () -- These are handled separately by insertBlock
241243
LedgerMirDist rwd -> do
242244
unless (Map.null rwd) $ do
243245
let rewards = Map.toList rwd

cardano-db-sync/src/Cardano/DbSync/LedgerEvent.hs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ module Cardano.DbSync.LedgerEvent (
1616
ledgerEventName,
1717
) where
1818

19-
import Cardano.Db hiding (EpochNo, SyncState, epochNo)
19+
import Cardano.Db hiding (AdaPots, EpochNo, SyncState, epochNo)
2020
import qualified Cardano.DbSync.Era.Shelley.Generic as Generic
2121
import Cardano.DbSync.Types
2222
import Cardano.DbSync.Util
2323
import Cardano.Ledger.Coin (Coin (..), DeltaCoin (..))
2424
import qualified Cardano.Ledger.Core as Ledger
25-
import Cardano.Ledger.Shelley.API (InstantaneousRewards (..))
25+
-- import Cardano.Ledger.Crypto (StandardCrypto)
26+
-- import Cardano.Ledger.Era (Crypto)
27+
-- import Cardano.Ledger.Shelley.AdaPots
28+
import Cardano.Ledger.Shelley.API (AdaPots, InstantaneousRewards (..))
2629
import Cardano.Ledger.Shelley.Rules (
2730
RupdEvent (RupdEvent),
2831
ShelleyEpochEvent (PoolReapEvent),
@@ -55,6 +58,7 @@ data LedgerEvent
5558
| LedgerDeltaRewards !EpochNo !Generic.Rewards
5659
| LedgerRestrainedRewards !EpochNo !Generic.Rewards !(Set StakeCred)
5760
| LedgerTotalRewards !EpochNo !(Map StakeCred (Set (Ledger.Reward StandardCrypto)))
61+
| LedgerAdaPots !AdaPots
5862
| LedgerStartAtEpoch !EpochNo
5963
| LedgerNewEpoch !EpochNo !SyncState
6064
deriving (Eq)
@@ -70,8 +74,9 @@ toOrdering ev = case ev of
7074
LedgerDeltaRewards {} -> 3
7175
LedgerRestrainedRewards {} -> 4
7276
LedgerTotalRewards {} -> 5
73-
LedgerStartAtEpoch {} -> 6
74-
LedgerNewEpoch {} -> 7
77+
LedgerAdaPots {} -> 6
78+
LedgerStartAtEpoch {} -> 7
79+
LedgerNewEpoch {} -> 8
7580

7681
convertAuxLedgerEvent :: OneEraLedgerEvent (CardanoEras StandardCrypto) -> Maybe LedgerEvent
7782
convertAuxLedgerEvent = toLedgerEvent . wrappedAuxLedgerEvent
@@ -85,6 +90,7 @@ ledgerEventName le =
8590
LedgerDeltaRewards {} -> "LedgerDeltaRewards"
8691
LedgerRestrainedRewards {} -> "LedgerRestrainedRewards"
8792
LedgerTotalRewards {} -> "LedgerTotalRewards"
93+
LedgerAdaPots {} -> "LedgerAdaPots"
8894
LedgerStartAtEpoch {} -> "LedgerStartAtEpoch"
8995
LedgerNewEpoch {} -> "LedgerNewEpoch"
9096

@@ -140,6 +146,7 @@ toLedgerEventShelley evt =
140146
Just $ LedgerIncrementalRewards e (convertPoolRewards m)
141147
LEMirTransfer rp tp _rtt _ttr -> Just $ LedgerMirDist (convertMirRewards rp tp)
142148
LERetiredPools r _u en -> Just $ LedgerPoolReap en (convertPoolDepositRefunds r)
149+
LEAdaPots p -> Just $ LedgerAdaPots p
143150
ShelleyLedgerEventBBODY {} -> Nothing
144151
ShelleyLedgerEventTICK {} -> Nothing
145152

@@ -298,3 +305,17 @@ pattern LERetiredPools r u e <-
298305
)
299306
)
300307
)
308+
309+
pattern LEAdaPots ::
310+
( EraCrypto ledgerera ~ StandardCrypto
311+
, Event (Ledger.EraRule "TICK" ledgerera) ~ ShelleyTickEvent ledgerera
312+
, Event (Ledger.EraRule "NEWEPOCH" ledgerera) ~ ShelleyNewEpochEvent ledgerera
313+
, Event (Ledger.EraRule "MIR" ledgerera) ~ ShelleyMirEvent ledgerera
314+
) =>
315+
AdaPots ->
316+
AuxLedgerEvent (LedgerState (ShelleyBlock p ledgerera))
317+
pattern LEAdaPots pots <-
318+
ShelleyLedgerEventTICK
319+
( TickNewEpochEvent
320+
(TotalAdaPotsEvent pots)
321+
)

cardano-db-sync/src/Cardano/DbSync/LedgerState.hs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ import Cardano.DbSync.Util
4343
import qualified Cardano.Ledger.Alonzo.PParams as Alonzo
4444
import Cardano.Ledger.Alonzo.Scripts
4545
import qualified Cardano.Ledger.BaseTypes as Ledger
46-
import qualified Cardano.Ledger.Core as Core
4746
import Cardano.Ledger.Era (EraCrypto)
48-
import qualified Cardano.Ledger.Shelley.API.Wallet as Shelley
47+
import Cardano.Ledger.Shelley.AdaPots (AdaPots)
4948
import Cardano.Ledger.Shelley.LedgerState (EpochState (..))
5049
import qualified Cardano.Ledger.Shelley.LedgerState as Shelley
5150
import Cardano.Prelude hiding (atomically)
@@ -307,14 +306,14 @@ applyBlock env blk = do
307306
!ledgerDB <- readStateUnsafe env
308307
let oldState = ledgerDbCurrent ledgerDB
309308
let !result = applyBlk (ExtLedgerCfg (getTopLevelconfigHasLedger env)) blk (clsState oldState)
309+
let !ledgerEvents = mapMaybe convertAuxLedgerEvent (lrEvents result)
310310
let !newLedgerState = lrResult result
311311
!details <- getSlotDetails env (ledgerState newLedgerState) time (cardanoBlockSlotNo blk)
312-
let !newEpoch = mkNewEpoch (clsState oldState) newLedgerState
312+
let !newEpoch = mkNewEpoch (clsState oldState) newLedgerState (findAdaPots ledgerEvents)
313313
let !newEpochBlockNo = applyToEpochBlockNo (isJust $ blockIsEBB blk) (isJust newEpoch) (clsEpochBlockNo oldState)
314314
let !newState = CardanoLedgerState newLedgerState newEpochBlockNo
315315
let !ledgerDB' = pushLedgerDB ledgerDB newState
316316
writeTVar (leStateVar env) (Strict.Just ledgerDB')
317-
let !ledgerEvents = mapMaybe convertAuxLedgerEvent (lrEvents result)
318317
let !appResult =
319318
ApplyResult
320319
{ apPrices = getPrices newState
@@ -336,16 +335,16 @@ applyBlock env blk = do
336335
Left err -> panic err
337336
Right result -> result
338337

339-
mkNewEpoch :: ExtLedgerState CardanoBlock -> ExtLedgerState CardanoBlock -> Maybe Generic.NewEpoch
340-
mkNewEpoch oldState newState =
338+
mkNewEpoch :: ExtLedgerState CardanoBlock -> ExtLedgerState CardanoBlock -> Maybe AdaPots -> Maybe Generic.NewEpoch
339+
mkNewEpoch oldState newState mPots =
341340
if ledgerEpochNo env newState /= ledgerEpochNo env oldState + 1
342341
then Nothing
343342
else
344343
Just $
345344
Generic.NewEpoch
346345
{ Generic.neEpoch = ledgerEpochNo env newState
347346
, Generic.neIsEBB = isJust $ blockIsEBB blk
348-
, Generic.neAdaPots = maybeToStrict $ getAdaPots newState
347+
, Generic.neAdaPots = maybeToStrict mPots
349348
, Generic.neEpochUpdate = Generic.epochUpdate newState
350349
}
351350

@@ -791,19 +790,6 @@ getRegisteredPoolShelley lState =
791790
Shelley.nesEs $
792791
Consensus.shelleyLedgerState lState
793792

794-
-- We only compute 'AdaPots' for later eras. This is a time consuming
795-
-- function and we only want to run it on epoch boundaries.
796-
getAdaPots :: ExtLedgerState CardanoBlock -> Maybe Shelley.AdaPots
797-
getAdaPots st =
798-
case ledgerState st of
799-
LedgerStateByron _ -> Nothing
800-
LedgerStateShelley sts -> Just $ totalAdaPots sts
801-
LedgerStateAllegra sta -> Just $ totalAdaPots sta
802-
LedgerStateMary stm -> Just $ totalAdaPots stm
803-
LedgerStateAlonzo sta -> Just $ totalAdaPots sta
804-
LedgerStateBabbage stb -> Just $ totalAdaPots stb
805-
LedgerStateConway _stc -> panic "TODO: Conway 3"
806-
807793
ledgerEpochNo :: HasLedgerEnv -> ExtLedgerState CardanoBlock -> EpochNo
808794
ledgerEpochNo env cls =
809795
case ledgerTipSlot (ledgerState cls) of
@@ -840,13 +826,6 @@ tickThenReapplyCheckHash cfg block lsb =
840826
, "."
841827
]
842828

843-
totalAdaPots ::
844-
forall p era.
845-
Core.EraTxOut era =>
846-
LedgerState (ShelleyBlock p era) ->
847-
Shelley.AdaPots
848-
totalAdaPots = Shelley.totalAdaPotsES . Shelley.nesEs . Consensus.shelleyLedgerState
849-
850829
getHeaderHash :: HeaderHash CardanoBlock -> ByteString
851830
getHeaderHash bh = SBS.fromShort (Consensus.getOneEraHash bh)
852831

@@ -894,3 +873,10 @@ getPrices st = case ledgerState $ clsState st of
894873
^. Alonzo.ppPricesL
895874
)
896875
_ -> Strict.Nothing
876+
877+
findAdaPots :: [LedgerEvent] -> Maybe AdaPots
878+
findAdaPots = go
879+
where
880+
go [] = Nothing
881+
go (LedgerAdaPots p: _) = Just p
882+
go (_ : rest) = go rest

0 commit comments

Comments
 (0)