@@ -35,6 +35,7 @@ module Cardano.DbSync.Ledger.State (
3535 getStakeSlice ,
3636 getSliceMeta ,
3737 findProposedCommittee ,
38+ trimLedgerState ,
3839) where
3940
4041import Cardano.BM.Trace (Trace , logInfo , logWarning )
@@ -50,9 +51,15 @@ import Cardano.DbSync.Types
5051import Cardano.DbSync.Util
5152import qualified Cardano.Ledger.Alonzo.PParams as Alonzo
5253import Cardano.Ledger.Alonzo.Scripts
54+ import Cardano.Ledger.Alonzo.TxOut (AlonzoTxOut (.. ))
55+ import Cardano.Ledger.Babbage.TxOut (BabbageTxOut (.. ))
5356import qualified Cardano.Ledger.BaseTypes as Ledger
57+ import Cardano.Ledger.Crypto (Crypto )
58+ import Cardano.Ledger.Mary.Value (MaryValue (.. ))
5459import Cardano.Ledger.Shelley.AdaPots (AdaPots )
5560import qualified Cardano.Ledger.Shelley.LedgerState as Shelley
61+ import Cardano.Ledger.Shelley.TxOut (ShelleyTxOut (.. ))
62+ import Cardano.Ledger.UTxO (UTxO (.. ))
5663import Cardano.Prelude hiding (atomically )
5764import Cardano.Slotting.Block (BlockNo (.. ))
5865import Cardano.Slotting.EpochInfo (EpochInfo , epochInfoEpoch )
@@ -73,6 +80,7 @@ import Control.Concurrent.STM.TBQueue (TBQueue, newTBQueueIO, readTBQueue, write
7380import qualified Control.Exception as Exception
7481
7582import qualified Data.ByteString.Base16 as Base16
83+ import Data.SOP.Strict (NP (.. ), fn )
7684
7785import Cardano.DbSync.Api.Types (InsertOptions (.. ), LedgerEnv (.. ), SyncOptions (.. ))
7886import Cardano.DbSync.Error (SyncNodeError (.. ), fromEitherSTM )
@@ -104,6 +112,7 @@ import Ouroboros.Consensus.Block (
104112import Ouroboros.Consensus.Block.Abstract (ConvertRawHash (.. ))
105113import Ouroboros.Consensus.BlockchainTime.WallClock.Types (SystemStart (.. ))
106114import Ouroboros.Consensus.Cardano.Block (LedgerState (.. ), StandardConway , StandardCrypto )
115+ import qualified Ouroboros.Consensus.Cardano.Block as Consensus
107116import Ouroboros.Consensus.Cardano.CanHardFork ()
108117import Ouroboros.Consensus.Config (TopLevelConfig (.. ), configCodec , configLedger )
109118import Ouroboros.Consensus.HardFork.Abstract
@@ -217,6 +226,7 @@ readStateUnsafe env = do
217226applyBlockAndSnapshot :: HasLedgerEnv -> CardanoBlock -> Bool -> IO (ApplyResult , Bool )
218227applyBlockAndSnapshot ledgerEnv blk isCons = do
219228 (oldState, appResult) <- applyBlock ledgerEnv blk
229+
220230 tookSnapshot <- storeSnapshotAndCleanupMaybe ledgerEnv oldState appResult (blockNo blk) isCons (isSyncedWithinSeconds (apSlotDetails appResult) 600 )
221231 pure (appResult, tookSnapshot)
222232
@@ -233,11 +243,13 @@ applyBlock env blk = do
233243 let ledgerEventsFull = mapMaybe (convertAuxLedgerEvent (leHasRewards env)) (lrEvents result)
234244 let (ledgerEvents, deposits) = splitDeposits ledgerEventsFull
235245 let ! newLedgerState = finaliseDrepDistr (lrResult result)
246+
236247 ! details <- getSlotDetails env (ledgerState newLedgerState) time (cardanoBlockSlotNo blk)
237248 ! newEpoch <- fromEitherSTM $ mkOnNewEpoch (clsState oldState) newLedgerState (findAdaPots ledgerEvents)
238249 let ! newEpochBlockNo = applyToEpochBlockNo (isJust $ blockIsEBB blk) (isJust newEpoch) (clsEpochBlockNo oldState)
239250 let ! newState = CardanoLedgerState newLedgerState newEpochBlockNo
240- let ! ledgerDB' = pushLedgerDB ledgerDB newState
251+ let ! newState' = maybe newState (trimOnNewEpoch newState) newEpoch
252+ let ! ledgerDB' = pushLedgerDB ledgerDB newState'
241253 writeTVar (leStateVar env) (Strict. Just ledgerDB')
242254 let ! appResult =
243255 if leUseLedger env
@@ -299,6 +311,9 @@ applyBlock env blk = do
299311 finaliseDrepDistr ledger =
300312 ledger & newEpochStateT %~ forceDRepPulsingState @ StandardConway
301313
314+ trimOnNewEpoch :: CardanoLedgerState -> Generic. NewEpoch -> CardanoLedgerState
315+ trimOnNewEpoch ls ! _ = trimLedgerState ls
316+
302317getGovState :: ExtLedgerState CardanoBlock -> Maybe (ConwayGovState StandardConway )
303318getGovState ls = case ledgerState ls of
304319 LedgerStateConway cls ->
@@ -889,3 +904,56 @@ findProposedCommittee gaId cgs = do
889904 UpdateCommittee _ toRemove toAdd q -> Right $ Ledger. SJust $ updatedCommittee toRemove toAdd q scommittee
890905 _ -> Left " Unexpected gov action." -- Should never happen since the accumulator only includes UpdateCommittee
891906 fromNothing err = maybe (Left err) Right
907+
908+ trimLedgerState :: CardanoLedgerState -> CardanoLedgerState
909+ trimLedgerState (CardanoLedgerState extLedger epochBlockNo) =
910+ CardanoLedgerState extLedger' epochBlockNo
911+ where
912+ extLedger' = trimExtLedgerState extLedger
913+
914+ trimExtLedgerState :: ExtLedgerState CardanoBlock -> ExtLedgerState CardanoBlock
915+ trimExtLedgerState =
916+ hApplyExtLedgerState $
917+ fn id
918+ :* fn id
919+ :* fn (overUTxO trimMaryTxOut)
920+ :* fn (overUTxO trimAlonzoTxOut)
921+ :* fn (overUTxO trimBabbageTxOut)
922+ :* fn (overUTxO trimBabbageTxOut)
923+ :* Nil
924+
925+ overUTxO ::
926+ (TxOut era -> TxOut era ) ->
927+ LedgerState (ShelleyBlock proto era ) ->
928+ LedgerState (ShelleyBlock proto era )
929+ overUTxO f ledger = ledger {Consensus. shelleyLedgerState = newEpochState'}
930+ where
931+ newEpochState = Consensus. shelleyLedgerState ledger
932+ newEpochState' = newEpochState & utxosL %~ mapUTxO
933+ utxosL = Shelley. nesEpochStateL . Shelley. esLStateL . Shelley. lsUTxOStateL . Shelley. utxosUtxoL
934+ mapUTxO (UTxO utxos) = UTxO (Map. map f utxos)
935+
936+ trimMaryTxOut ::
937+ ShelleyTxOut Consensus. StandardMary ->
938+ ShelleyTxOut Consensus. StandardMary
939+ trimMaryTxOut (ShelleyTxOut addr val) = ShelleyTxOut addr val'
940+ where
941+ val' = trimMultiAsset val
942+
943+ trimAlonzoTxOut ::
944+ AlonzoTxOut Consensus. StandardAlonzo ->
945+ AlonzoTxOut Consensus. StandardAlonzo
946+ trimAlonzoTxOut (AlonzoTxOut addr val hashes) = AlonzoTxOut addr val' hashes
947+ where
948+ val' = trimMultiAsset val
949+
950+ trimBabbageTxOut ::
951+ (Crypto c , Era era , Value era ~ MaryValue c ) =>
952+ BabbageTxOut era ->
953+ BabbageTxOut era
954+ trimBabbageTxOut (BabbageTxOut addr val datums refs) = BabbageTxOut addr val' datums refs
955+ where
956+ val' = trimMultiAsset val
957+
958+ trimMultiAsset :: MaryValue c -> MaryValue c
959+ trimMultiAsset (MaryValue coin _) = MaryValue coin mempty
0 commit comments