Skip to content

Commit 256d0f5

Browse files
committed
Start using explocit Ids for Tx and Block
1 parent 064aaf1 commit 256d0f5

File tree

29 files changed

+264
-334
lines changed

29 files changed

+264
-334
lines changed

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import Cardano.DbSync.Era.Byron.Insert (insertByronBlock)
2121
import qualified Cardano.DbSync.Era.Shelley.Generic as Generic
2222
import Cardano.DbSync.Era.Universal.Block (insertBlockUniversal)
2323
import Cardano.DbSync.Era.Universal.Epoch (hasEpochStartEvent, hasNewEpochEvent)
24-
import Cardano.DbSync.Era.Universal.Insert.Certificate (mkAdaPots)
2524
import Cardano.DbSync.Era.Universal.Insert.LedgerEvent (insertNewEpochLedgerEvents)
2625
import Cardano.DbSync.Error
2726
import Cardano.DbSync.Ledger.State (applyBlockAndSnapshot, defaultApplyResult)
@@ -32,10 +31,9 @@ import Cardano.DbSync.Types
3231
import Cardano.DbSync.Util
3332
import Cardano.DbSync.Util.Constraint (addConstraintsIfNotExist)
3433
import qualified Cardano.Ledger.Alonzo.Scripts as Ledger
35-
import Cardano.Ledger.Shelley.AdaPots as Shelley
3634
import Cardano.Node.Configuration.Logging (Trace)
3735
import Cardano.Prelude
38-
import Cardano.Slotting.Slot (EpochNo (..), SlotNo)
36+
import Cardano.Slotting.Slot (EpochNo (..))
3937
import Control.Monad.Logger (LoggingT)
4038
import Control.Monad.Trans.Except.Extra (newExceptT)
4139
import qualified Data.ByteString.Short as SBS
@@ -86,11 +84,6 @@ applyAndInsertBlockMaybe syncEnv tracer cblk = do
8684
liftIO $ setConsistentLevel syncEnv Consistent
8785
when bl $ liftIO $ writePrefetch syncEnv cblk
8886
insertBlock syncEnv cblk applyRes True tookSnapshot
89-
Right blockId | Just (adaPots, slotNo, epochNo) <- getAdaPots applyRes -> do
90-
replaced <- lift $ DB.replaceAdaPots blockId $ mkAdaPots blockId slotNo epochNo adaPots
91-
if replaced
92-
then liftIO $ logInfo tracer $ "Fixed AdaPots for " <> textShow epochNo
93-
else liftIO $ logInfo tracer $ "Reached " <> textShow epochNo
9487
Right _
9588
| Just epochNo <- getNewEpoch applyRes ->
9689
liftIO $ logInfo tracer $ "Reached " <> textShow epochNo
@@ -104,12 +97,6 @@ applyAndInsertBlockMaybe syncEnv tracer cblk = do
10497
slotDetails <- getSlotDetailsNode nle (cardanoBlockSlotNo cblk)
10598
pure (defaultApplyResult slotDetails, False)
10699

107-
getAdaPots :: ApplyResult -> Maybe (Shelley.AdaPots, SlotNo, EpochNo)
108-
getAdaPots appRes = do
109-
newEpoch <- maybeFromStrict $ apNewEpoch appRes
110-
adaPots <- maybeFromStrict $ Generic.neAdaPots newEpoch
111-
pure (adaPots, sdSlotNo $ apSlotDetails appRes, sdEpochNo $ apSlotDetails appRes)
112-
113100
getNewEpoch :: ApplyResult -> Maybe EpochNo
114101
getNewEpoch appRes =
115102
Generic.neEpoch <$> maybeFromStrict (apNewEpoch appRes)

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import Cardano.DbSync.Era.Util
4343
import Cardano.DbSync.Error
4444
import Cardano.DbSync.Types
4545
import Cardano.Ledger.Mary.Value
46-
import qualified Cardano.Ledger.TxIn as Ledger
4746
import Cardano.Prelude
4847
import Control.Concurrent.Class.MonadSTM.Strict (
4948
StrictTVar,
@@ -335,7 +334,7 @@ queryPrevBlockWithCache msg cache hsh =
335334
queryTxIdWithCache ::
336335
MonadIO m =>
337336
CacheStatus ->
338-
Ledger.TxId StandardCrypto ->
337+
TxIdLedger ->
339338
ReaderT SqlBackend m (Either DB.LookupFail DB.TxId)
340339
queryTxIdWithCache cache txIdLedger = do
341340
case cache of
@@ -370,7 +369,7 @@ queryTxIdWithCache cache txIdLedger = do
370369
tryUpdateCacheTx ::
371370
MonadIO m =>
372371
CacheStatus ->
373-
Ledger.TxId StandardCrypto ->
372+
TxIdLedger ->
374373
DB.TxId ->
375374
m ()
376375
tryUpdateCacheTx (ActiveCache ci) ledgerTxId txId =
@@ -380,20 +379,19 @@ tryUpdateCacheTx _ _ _ = pure ()
380379
insertBlockAndCache ::
381380
(MonadIO m, MonadBaseControl IO m) =>
382381
CacheStatus ->
382+
DB.BlockId ->
383383
DB.Block ->
384-
ReaderT SqlBackend m DB.BlockId
385-
insertBlockAndCache cache block =
384+
ReaderT SqlBackend m ()
385+
insertBlockAndCache cache k block =
386386
case cache of
387387
NoCache -> insBlck
388388
ActiveCache ci ->
389389
withCacheOptimisationCheck ci insBlck $ do
390-
bid <- insBlck
391-
liftIO $ do
392-
missPrevBlock (cStats ci)
393-
atomically $ writeTVar (cPrevBlock ci) $ Just (bid, DB.blockHash block)
394-
pure bid
390+
insBlck
391+
liftIO $
392+
atomically $ writeTVar (cPrevBlock ci) $ Just (k, DB.blockHash block)
395393
where
396-
insBlck = DB.insertBlock block
394+
insBlck = DB.insertBlock k block
397395

398396
queryDatum ::
399397
MonadIO m =>

cardano-db-sync/src/Cardano/DbSync/Cache/Types.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ import Cardano.DbSync.Cache.FIFO (FIFOCache)
3939
import qualified Cardano.DbSync.Cache.FIFO as FIFO
4040
import Cardano.DbSync.Cache.LRU (LRUCache)
4141
import qualified Cardano.DbSync.Cache.LRU as LRU
42-
import Cardano.DbSync.Types (CardanoBlock, DataHash, PoolKeyHash, RewAccount, StakeCred)
42+
import Cardano.DbSync.Types (CardanoBlock, DataHash, PoolKeyHash, RewAccount, StakeCred, TxIdLedger)
4343
import Cardano.Ledger.Mary.Value (AssetName, PolicyID)
44-
import qualified Cardano.Ledger.TxIn as Ledger
4544
import Cardano.Prelude
4645
import Control.Concurrent.Class.MonadSTM.Strict (
4746
StrictTMVar,
@@ -90,7 +89,7 @@ data CacheInternal = CacheInternal
9089
, cStats :: !(StrictTVar IO CacheStatistics)
9190
, cEpoch :: !(StrictTVar IO CacheEpoch)
9291
, cAddress :: !(StrictTVar IO (LRUCache ByteString V.AddressId))
93-
, cTxIds :: !(StrictTVar IO (FIFOCache (Ledger.TxId StandardCrypto) DB.TxId))
92+
, cTxIds :: !(StrictTVar IO (FIFOCache TxIdLedger DB.TxId))
9493
}
9594

9695
data CacheStatistics = CacheStatistics

cardano-db-sync/src/Cardano/DbSync/Era/Byron/Genesis.hs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ insertValidateGenesisDist syncEnv (NetworkName networkName) cfg = do
8888
, DB.slotLeaderPoolHashId = Nothing
8989
, DB.slotLeaderDescription = "Genesis slot leader"
9090
}
91-
bid <-
92-
lift . DB.insertBlock $
91+
let bid = DB.BlockKey 0
92+
lift $ DB.insertBlock bid $
9393
DB.Block
9494
{ DB.blockHash = configGenesisHash cfg
9595
, DB.blockEpochNo = Nothing
@@ -187,27 +187,27 @@ insertTxOutsByron ::
187187
(Byron.Address, Byron.Lovelace) ->
188188
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
189189
insertTxOutsByron syncEnv disInOut blkId (address, value) = do
190+
let txId = DB.TxKey $ DB.unBlockKey blkId
190191
case txHashOfAddress address of
191192
Left err -> throwError err
192193
Right val -> lift $ do
193194
-- Each address/value pair of the initial coin distribution comes from an artifical transaction
194195
-- with a hash generated by hashing the address.
195-
txId <- do
196-
DB.insertTx $
197-
DB.Tx
198-
{ DB.txHash = Byron.unTxHash val
199-
, DB.txBlockId = blkId
200-
, DB.txBlockIndex = 0
201-
, DB.txOutSum = DB.DbLovelace (Byron.unsafeGetLovelace value)
202-
, DB.txFee = DB.DbLovelace 0
203-
, DB.txDeposit = Just 0
204-
, DB.txSize = 0 -- Genesis distribution address to not have a size.
205-
, DB.txInvalidHereafter = Nothing
206-
, DB.txInvalidBefore = Nothing
207-
, DB.txValidContract = True
208-
, DB.txScriptSize = 0
209-
, DB.txTreasuryDonation = DB.DbLovelace 0
210-
}
196+
DB.insertTx txId $
197+
DB.Tx
198+
{ DB.txHash = Byron.unTxHash val
199+
, DB.txBlockId = blkId
200+
, DB.txBlockIndex = 0
201+
, DB.txOutSum = DB.DbLovelace (Byron.unsafeGetLovelace value)
202+
, DB.txFee = DB.DbLovelace 0
203+
, DB.txDeposit = Just 0
204+
, DB.txSize = 0 -- Genesis distribution address to not have a size.
205+
, DB.txInvalidHereafter = Nothing
206+
, DB.txInvalidBefore = Nothing
207+
, DB.txValidContract = True
208+
, DB.txScriptSize = 0
209+
, DB.txTreasuryDonation = DB.DbLovelace 0
210+
}
211211
--
212212
unless disInOut $
213213
case getTxOutTableType syncEnv of

cardano-db-sync/src/Cardano/DbSync/Era/Byron/Insert.hs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ insertABOBBoundary syncEnv blk details = do
8282
cache = envCache syncEnv
8383
-- Will not get called in the OBFT part of the Byron era.
8484
pbid <- queryPrevBlockWithCache "insertABOBBoundary" cache (Byron.ebbPrevHash blk)
85+
let blkId = DB.BlockKey $ 1 + DB.unBlockKey pbid
8586
let epochNo = unEpochNo $ sdEpochNo details
8687
slid <-
8788
lift . DB.insertSlotLeader $
@@ -90,8 +91,8 @@ insertABOBBoundary syncEnv blk details = do
9091
, DB.slotLeaderPoolHashId = Nothing
9192
, DB.slotLeaderDescription = "Epoch boundary slot leader"
9293
}
93-
blkId <-
94-
lift . insertBlockAndCache cache $
94+
lift $
95+
insertBlockAndCache cache blkId $
9596
DB.Block
9697
{ DB.blockHash = Byron.unHeaderHash $ Byron.boundaryHashAnnotated blk
9798
, DB.blockEpochNo = Just epochNo
@@ -146,10 +147,11 @@ insertABlock ::
146147
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
147148
insertABlock syncEnv firstBlockOfEpoch blk details = do
148149
pbid <- queryPrevBlockWithCache "insertABlock" cache (Byron.blockPreviousHash blk)
150+
let blkId = DB.BlockKey $ 1 + DB.unBlockKey pbid
149151
slid <- lift . DB.insertSlotLeader $ Byron.mkSlotLeader blk
150152
let txs = Byron.blockPayload blk
151-
blkId <-
152-
lift . insertBlockAndCache cache $
153+
lift $
154+
insertBlockAndCache cache blkId $
153155
DB.Block
154156
{ DB.blockHash = Byron.blockHash blk
155157
, DB.blockEpochNo = Just $ unEpochNo (sdEpochNo details)
@@ -238,11 +240,12 @@ insertByronTx ::
238240
Word64 ->
239241
ExceptT SyncNodeError (ReaderT SqlBackend m) Word64
240242
insertByronTx syncEnv blkId tx blockIndex = do
243+
let txId = DB.TxKey $ DB.unBlockKey blkId
241244
disInOut <- liftIO $ getDisableInOutState syncEnv
242245
if disInOut
243246
then do
244-
txId <-
245-
lift . DB.insertTx $
247+
lift $
248+
DB.insertTx txId $
246249
DB.Tx
247250
{ DB.txHash = Byron.unTxHash $ Crypto.serializeCborHash (Byron.taTx tx)
248251
, DB.txBlockId = blkId
@@ -282,10 +285,11 @@ insertByronTx' ::
282285
Word64 ->
283286
ExceptT SyncNodeError (ReaderT SqlBackend m) Word64
284287
insertByronTx' syncEnv blkId tx blockIndex = do
288+
let txId = DB.TxKey $ fromIntegral $ fromIntegral (DB.unBlockId blkId) * 1000 + blockIndex
285289
resolvedInputs <- mapM (resolveTxInputs txOutTableType) (toList $ Byron.txInputs (Byron.taTx tx))
286290
valFee <- firstExceptT annotateTx $ ExceptT $ pure (calculateTxFee (Byron.taTx tx) resolvedInputs)
287-
txId <-
288-
lift . DB.insertTx $
291+
lift $
292+
DB.insertTx txId $
289293
DB.Tx
290294
{ DB.txHash = Byron.unTxHash $ Crypto.serializeCborHash (Byron.taTx tx)
291295
, DB.txBlockId = blkId

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Conway.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import Cardano.Ledger.BaseTypes
2121
import Cardano.Ledger.Conway.Governance
2222
import Cardano.Ledger.Conway.TxBody
2323
import qualified Cardano.Ledger.Core as Core
24-
import Cardano.Ledger.TxIn
2524
import Cardano.Prelude
2625
import qualified Data.Map.Strict as Map
2726
import Lens.Micro
2827
import Ouroboros.Consensus.Cardano.Block (StandardConway, StandardCrypto)
28+
import Cardano.DbSync.Types
2929

3030
fromConwayTx :: Bool -> Maybe Alonzo.Prices -> (Word64, Core.Tx StandardConway) -> Tx
3131
fromConwayTx ioExtraPlutus mprices (blkIndex, tx) =
@@ -77,7 +77,7 @@ fromConwayTx ioExtraPlutus mprices (blkIndex, tx) =
7777
txBody :: Core.TxBody StandardConway
7878
txBody = tx ^. Core.bodyTxL
7979

80-
txId :: TxId StandardCrypto
80+
txId :: TxIdLedger
8181
txId = mkTxId tx
8282

8383
outputs :: [TxOut]

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Shelley.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import Cardano.DbSync.Era.Shelley.Generic.Script (fromMultiSig)
3636
import Cardano.DbSync.Era.Shelley.Generic.Tx.Types
3737
import Cardano.DbSync.Era.Shelley.Generic.Util
3838
import Cardano.DbSync.Era.Shelley.Generic.Witness
39-
import Cardano.DbSync.Types (RewAccount)
39+
import Cardano.DbSync.Types (RewAccount, TxIdLedger)
4040
import Cardano.Ledger.BaseTypes (TxIx (..), strictMaybeToMaybe)
4141
import Cardano.Ledger.Coin (Coin (..))
4242
import qualified Cardano.Ledger.Core as Core
@@ -140,7 +140,7 @@ txHashId = safeHashToByteString . txSafeHash
140140
txSafeHash :: (EraCrypto era ~ StandardCrypto, Core.EraTx era) => Core.Tx era -> Ledger.SafeHash StandardCrypto Core.EraIndependentTxBody
141141
txSafeHash tx = Ledger.hashAnnotated (tx ^. Core.bodyTxL)
142142

143-
mkTxId :: (EraCrypto era ~ StandardCrypto, Core.EraTx era) => Core.Tx era -> Ledger.TxId StandardCrypto
143+
mkTxId :: (EraCrypto era ~ StandardCrypto, Core.EraTx era) => Core.Tx era -> TxIdLedger
144144
mkTxId = Ledger.TxId . txSafeHash
145145

146146
txHashFromSafe :: Ledger.SafeHash StandardCrypto Core.EraIndependentTxBody -> ByteString

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Types.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@ import Cardano.Ledger.Conway.TxCert (ConwayTxCert)
4545
import Cardano.Ledger.Core (TxBody)
4646
import Cardano.Ledger.Mary.Value (AssetName, MultiAsset, PolicyID)
4747
import Cardano.Ledger.Shelley.TxCert
48-
import qualified Cardano.Ledger.TxIn as Ledger
4948
import Cardano.Prelude
5049
import Cardano.Slotting.Slot (SlotNo (..))
5150
import Ouroboros.Consensus.Cardano.Block (StandardAlonzo, StandardBabbage, StandardConway, StandardCrypto, StandardShelley)
5251

5352
data Tx = Tx
5453
{ txHash :: !ByteString
55-
, txLedgerTxId :: !(Ledger.TxId StandardCrypto)
54+
, txLedgerTxId :: !TxIdLedger
5655
, txBlockIndex :: !Word64
5756
, txCBOR :: ByteString
5857
, txSize :: !Word64
@@ -99,7 +98,7 @@ data TxWithdrawal = TxWithdrawal
9998
}
10099

101100
data TxInKey = TxInKey
102-
{ txInTxId :: !(Ledger.TxId StandardCrypto)
101+
{ txInTxId :: !TxIdLedger
103102
, txInIndex :: !Word64
104103
}
105104
deriving (Eq, Show, Ord)

0 commit comments

Comments
 (0)