@@ -35,6 +35,7 @@ module Cardano.DbSync.Ledger.State (
35
35
getStakeSlice ,
36
36
getSliceMeta ,
37
37
findProposedCommittee ,
38
+ trimLedgerState ,
38
39
) where
39
40
40
41
import Cardano.BM.Trace (Trace , logInfo , logWarning )
@@ -50,9 +51,15 @@ import Cardano.DbSync.Types
50
51
import Cardano.DbSync.Util
51
52
import qualified Cardano.Ledger.Alonzo.PParams as Alonzo
52
53
import Cardano.Ledger.Alonzo.Scripts
54
+ import Cardano.Ledger.Alonzo.TxOut (AlonzoTxOut (.. ))
55
+ import Cardano.Ledger.Babbage.TxOut (BabbageTxOut (.. ))
53
56
import qualified Cardano.Ledger.BaseTypes as Ledger
57
+ import Cardano.Ledger.Crypto (Crypto )
58
+ import Cardano.Ledger.Mary.Value (MaryValue (.. ))
54
59
import Cardano.Ledger.Shelley.AdaPots (AdaPots )
55
60
import qualified Cardano.Ledger.Shelley.LedgerState as Shelley
61
+ import Cardano.Ledger.Shelley.TxOut (ShelleyTxOut (.. ))
62
+ import Cardano.Ledger.UTxO (UTxO (.. ))
56
63
import Cardano.Prelude hiding (atomically )
57
64
import Cardano.Slotting.Block (BlockNo (.. ))
58
65
import Cardano.Slotting.EpochInfo (EpochInfo , epochInfoEpoch )
@@ -73,6 +80,7 @@ import Control.Concurrent.STM.TBQueue (TBQueue, newTBQueueIO, readTBQueue, write
73
80
import qualified Control.Exception as Exception
74
81
75
82
import qualified Data.ByteString.Base16 as Base16
83
+ import Data.SOP.Strict (NP (.. ), fn )
76
84
77
85
import Cardano.DbSync.Api.Types (InsertOptions (.. ), LedgerEnv (.. ), SyncOptions (.. ))
78
86
import Cardano.DbSync.Error (SyncNodeError (.. ), fromEitherSTM )
@@ -104,6 +112,7 @@ import Ouroboros.Consensus.Block (
104
112
import Ouroboros.Consensus.Block.Abstract (ConvertRawHash (.. ))
105
113
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (SystemStart (.. ))
106
114
import Ouroboros.Consensus.Cardano.Block (LedgerState (.. ), StandardConway , StandardCrypto )
115
+ import qualified Ouroboros.Consensus.Cardano.Block as Consensus
107
116
import Ouroboros.Consensus.Cardano.CanHardFork ()
108
117
import Ouroboros.Consensus.Config (TopLevelConfig (.. ), configCodec , configLedger )
109
118
import Ouroboros.Consensus.HardFork.Abstract
@@ -217,6 +226,7 @@ readStateUnsafe env = do
217
226
applyBlockAndSnapshot :: HasLedgerEnv -> CardanoBlock -> Bool -> IO (ApplyResult , Bool )
218
227
applyBlockAndSnapshot ledgerEnv blk isCons = do
219
228
(oldState, appResult) <- applyBlock ledgerEnv blk
229
+
220
230
tookSnapshot <- storeSnapshotAndCleanupMaybe ledgerEnv oldState appResult (blockNo blk) isCons (isSyncedWithinSeconds (apSlotDetails appResult) 600 )
221
231
pure (appResult, tookSnapshot)
222
232
@@ -233,11 +243,13 @@ applyBlock env blk = do
233
243
let ledgerEventsFull = mapMaybe (convertAuxLedgerEvent (leHasRewards env)) (lrEvents result)
234
244
let (ledgerEvents, deposits) = splitDeposits ledgerEventsFull
235
245
let ! newLedgerState = finaliseDrepDistr (lrResult result)
246
+
236
247
! details <- getSlotDetails env (ledgerState newLedgerState) time (cardanoBlockSlotNo blk)
237
248
! newEpoch <- fromEitherSTM $ mkOnNewEpoch (clsState oldState) newLedgerState (findAdaPots ledgerEvents)
238
249
let ! newEpochBlockNo = applyToEpochBlockNo (isJust $ blockIsEBB blk) (isJust newEpoch) (clsEpochBlockNo oldState)
239
250
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'
241
253
writeTVar (leStateVar env) (Strict. Just ledgerDB')
242
254
let ! appResult =
243
255
if leUseLedger env
@@ -299,6 +311,9 @@ applyBlock env blk = do
299
311
finaliseDrepDistr ledger =
300
312
ledger & newEpochStateT %~ forceDRepPulsingState @ StandardConway
301
313
314
+ trimOnNewEpoch :: CardanoLedgerState -> Generic. NewEpoch -> CardanoLedgerState
315
+ trimOnNewEpoch ls ! _ = trimLedgerState ls
316
+
302
317
getGovState :: ExtLedgerState CardanoBlock -> Maybe (ConwayGovState StandardConway )
303
318
getGovState ls = case ledgerState ls of
304
319
LedgerStateConway cls ->
@@ -889,3 +904,56 @@ findProposedCommittee gaId cgs = do
889
904
UpdateCommittee _ toRemove toAdd q -> Right $ Ledger. SJust $ updatedCommittee toRemove toAdd q scommittee
890
905
_ -> Left " Unexpected gov action." -- Should never happen since the accumulator only includes UpdateCommittee
891
906
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