|
| 1 | +{-# LANGUAGE FlexibleContexts #-} |
| 2 | +{-# LANGUAGE MonoLocalBinds #-} |
| 3 | +{-# LANGUAGE PartialTypeSignatures #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | +{-# LANGUAGE TypeApplications #-} |
| 6 | +{-# LANGUAGE TypeOperators #-} |
| 7 | + |
| 8 | +module Ouroboros.Consensus.Cardano.StreamingLedgerTables where |
| 9 | + |
| 10 | +import Cardano.Ledger.BaseTypes (WithOrigin (..)) |
| 11 | +import Cardano.Ledger.Binary |
| 12 | +import Cardano.Ledger.Core (eraDecoder) |
| 13 | +import qualified Cardano.Ledger.Shelley.API as SL |
| 14 | +import qualified Cardano.Ledger.Shelley.LedgerState as SL |
| 15 | +import qualified Cardano.Ledger.State as SL |
| 16 | +import Control.Monad.Except |
| 17 | +import Control.Tracer (nullTracer) |
| 18 | +import Data.ByteString (ByteString) |
| 19 | +import qualified Data.Map.Strict as Map |
| 20 | +import Data.SOP.BasicFunctors |
| 21 | +import Data.SOP.Functors |
| 22 | +import Data.SOP.Strict |
| 23 | +import qualified Data.SOP.Telescope as Telescope |
| 24 | +import Lens.Micro |
| 25 | +import Ouroboros.Consensus.Byron.Ledger |
| 26 | +import Ouroboros.Consensus.Cardano.Block |
| 27 | +import Ouroboros.Consensus.Cardano.CanHardFork (CardanoHardForkConstraints) |
| 28 | +import Ouroboros.Consensus.Cardano.Ledger |
| 29 | +import Ouroboros.Consensus.HardFork.Combinator |
| 30 | +import Ouroboros.Consensus.HardFork.Combinator.Basics (LedgerState (..)) |
| 31 | +import Ouroboros.Consensus.HardFork.Combinator.State |
| 32 | +import Ouroboros.Consensus.Ledger.Abstract |
| 33 | +import Ouroboros.Consensus.Ledger.Tables.Utils (emptyLedgerTables) |
| 34 | +import Ouroboros.Consensus.Shelley.Ledger |
| 35 | +import Ouroboros.Consensus.Shelley.Ledger.SupportsProtocol () |
| 36 | +import Ouroboros.Consensus.Storage.LedgerDB.V1.BackingStore.API |
| 37 | +import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.BackingStore.Impl.LMDB as LMDB |
| 38 | +import Ouroboros.Consensus.Util.IOLike (bracket) |
| 39 | +import Ouroboros.Consensus.Util.StreamingLedgerTables |
| 40 | +import Streaming |
| 41 | +import System.FS.API |
| 42 | +import System.FS.IO |
| 43 | + |
| 44 | +fromInMemory :: |
| 45 | + SomeHasFS IO -> |
| 46 | + FsPath -> |
| 47 | + LedgerState (CardanoBlock StandardCrypto) EmptyMK -> |
| 48 | + ( Stream |
| 49 | + ( Of |
| 50 | + ( TxIn (LedgerState (CardanoBlock StandardCrypto)) |
| 51 | + , TxOut (LedgerState (CardanoBlock StandardCrypto)) |
| 52 | + ) |
| 53 | + ) |
| 54 | + (ExceptT DeserialiseFailure IO) |
| 55 | + (Stream (Of ByteString) IO ()) -> |
| 56 | + ExceptT DeserialiseFailure IO (Stream (Of ByteString) IO ()) |
| 57 | + ) -> |
| 58 | + ExceptT DeserialiseFailure IO () |
| 59 | +fromInMemory shfs fp (HardForkLedgerState (HardForkState idx)) k = |
| 60 | + let |
| 61 | + np :: |
| 62 | + NP |
| 63 | + (Current (Flip LedgerState EmptyMK) -.-> K (ExceptT DeserialiseFailure IO ())) |
| 64 | + (CardanoEras StandardCrypto) |
| 65 | + np = |
| 66 | + ( Fn $ undefined :: |
| 67 | + (Current (Flip LedgerState EmptyMK) -.-> K (ExceptT DeserialiseFailure IO ())) ByronBlock |
| 68 | + ) |
| 69 | + :* (Fn $ K . foo ShelleyTxOut . unFlip . currentState) |
| 70 | + :* (Fn $ K . foo AllegraTxOut . unFlip . currentState) |
| 71 | + :* (Fn $ K . foo MaryTxOut . unFlip . currentState) |
| 72 | + :* (Fn $ K . foo AlonzoTxOut . unFlip . currentState) |
| 73 | + :* (Fn $ K . foo BabbageTxOut . unFlip . currentState) |
| 74 | + :* (Fn $ K . foo ConwayTxOut . unFlip . currentState) |
| 75 | + :* (Fn $ K . foo DijkstraTxOut . unFlip . currentState) |
| 76 | + :* Nil |
| 77 | + in |
| 78 | + hcollapse $ hap np $ Telescope.tip idx |
| 79 | + where |
| 80 | + foo :: |
| 81 | + forall proto era. |
| 82 | + ShelleyCompatible proto era => |
| 83 | + (TxOut (LedgerState (ShelleyBlock proto era)) -> CardanoTxOut StandardCrypto) -> |
| 84 | + LedgerState (ShelleyBlock proto era) EmptyMK -> |
| 85 | + ExceptT DeserialiseFailure IO () |
| 86 | + foo toCardanoTxOut st = |
| 87 | + let certInterns = |
| 88 | + internsFromMap $ |
| 89 | + shelleyLedgerState st |
| 90 | + ^. SL.nesEsL |
| 91 | + . SL.esLStateL |
| 92 | + . SL.lsCertStateL |
| 93 | + . SL.certDStateL |
| 94 | + . SL.accountsL |
| 95 | + . SL.accountsMapL |
| 96 | + in yieldInMemoryS |
| 97 | + shfs |
| 98 | + fp |
| 99 | + (eraDecoder @era decodeMemPack) |
| 100 | + (eraDecoder @era $ toCardanoTxOut <$> decShareCBOR certInterns) |
| 101 | + k |
| 102 | + |
| 103 | +toLMDB :: |
| 104 | + LedgerState (CardanoBlock StandardCrypto) EmptyMK -> |
| 105 | + FilePath -> |
| 106 | + Stream |
| 107 | + ( Of |
| 108 | + ( TxIn (LedgerState (CardanoBlock StandardCrypto)) |
| 109 | + , TxOut (LedgerState (CardanoBlock StandardCrypto)) |
| 110 | + ) |
| 111 | + ) |
| 112 | + (ExceptT DeserialiseFailure IO) |
| 113 | + (Stream (Of ByteString) IO ()) -> |
| 114 | + ExceptT DeserialiseFailure IO (Stream (Of ByteString) IO ()) |
| 115 | +toLMDB hint fp s = do |
| 116 | + bs <- |
| 117 | + lift $ |
| 118 | + LMDB.newLMDBBackingStore |
| 119 | + nullTracer |
| 120 | + limits |
| 121 | + (LiveLMDBFS $ SomeHasFS $ ioHasFS $ MountPoint fp) |
| 122 | + (SnapshotsFS $ SomeHasFS $ ioHasFS $ MountPoint fp) |
| 123 | + (InitFromValues (At 0) hint emptyLedgerTables) |
| 124 | + r <- sinkLmdbS @(ExceptT DeserialiseFailure IO) 1000 hint (\s' h d -> lift $ bsWrite bs s' h d) s |
| 125 | + lift $ bsClose bs |
| 126 | + pure r |
| 127 | + |
| 128 | +limits :: LMDB.LMDBLimits |
| 129 | +limits = undefined |
| 130 | + |
| 131 | +foo :: |
| 132 | + SomeHasFS IO -> |
| 133 | + FsPath -> |
| 134 | + FilePath -> |
| 135 | + LedgerState (CardanoBlock StandardCrypto) EmptyMK -> |
| 136 | + ExceptT DeserialiseFailure IO () |
| 137 | +foo shfs fpFrom fpTo st = fromInMemory shfs fpFrom st (toLMDB st fpTo) |
0 commit comments