Skip to content

Commit 04576b6

Browse files
committed
Fix generated txIds
1 parent 6de7ea8 commit 04576b6

File tree

7 files changed

+25
-25
lines changed

7 files changed

+25
-25
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Cardano.DbSync.Api.Types (ConsistentLevel (..), InsertOptions (..), SyncE
2323
import Cardano.DbSync.Era.Byron.Insert (insertByronBlock)
2424
import qualified Cardano.DbSync.Era.Shelley.Generic as Generic
2525
import Cardano.DbSync.Era.Universal.Block (insertBlockUniversal, prepareBlock)
26-
import Cardano.DbSync.Era.Universal.Epoch (hasEpochStartEvent) -- , hasNewEpochEvent)
26+
import Cardano.DbSync.Era.Universal.Epoch (hasEpochStartEvent)
2727
import Cardano.DbSync.Era.Universal.Insert.LedgerEvent (insertNewEpochLedgerEvents)
2828
import Cardano.DbSync.Error
2929
import Cardano.DbSync.Ledger.Types
@@ -156,14 +156,17 @@ prepareInsertBlock syncEnv (blockId, blk) applyRessultVar firstAfterRollback = d
156156
(blockDB, preparedTxs) <-
157157
liftIO $ concurrently
158158
(runOrThrowIO $ runExceptT $ DB.runDbLoggingExceptT backend tracer $ prepareBlock syncEnv blk)
159-
(mapConcurrently prepareTxWithPool (Generic.blkTxs blk))
159+
(mapConcurrently prepareTxWithPool (zip txIds $ Generic.blkTxs blk))
160160

161161
_minIds <- insertBlockGroupedData syncEnv $ mconcat (snd <$> preparedTxs)
162162
(applyResult, tookSnapshot) <- liftIO $ atomically $ readTMVar applyRessultVar
163163
insertBlockWithLedger syncEnv blockId blockDB blk (fst <$> preparedTxs) applyResult firstAfterRollback tookSnapshot
164164
where
165-
prepareTxWithPool tx = runOrThrowIO $ runSqlPoolNoTransaction (prepTx tx) (envPool syncEnv) Nothing
166-
prepTx = runExceptT . prepareTxGrouped syncEnv [] blockId
165+
txIdBase = 100000 * DB.unBlockKey blockId -- TODO: retrieve the id base from Cache
166+
txHashesIds = (\tx -> (Generic.txLedgerTxId tx, DB.TxKey (txIdBase + fromIntegral (Generic.txBlockIndex tx)))) <$> Generic.blkTxs blk
167+
txIds = snd <$> txHashesIds
168+
prepareTxWithPool (txId, tx) = runOrThrowIO $ runSqlPoolNoTransaction (prepTx txId tx) (envPool syncEnv) Nothing
169+
prepTx txId tx = runExceptT $ prepareTxGrouped syncEnv txHashesIds blockId txId tx
167170

168171
backend = envBackend syncEnv
169172
tracer = getTrace syncEnv

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ insertValidateGenesisDist syncEnv (NetworkName networkName) cfg = do
109109
, DB.blockOpCert = Nothing
110110
, DB.blockOpCertCounter = Nothing
111111
}
112-
mapM_ (insertTxOutsByron syncEnv disInOut bid) $ genesisTxos cfg
112+
mapM_ (insertTxOutsByron syncEnv disInOut bid) $ zip [0 ..] (genesisTxos cfg)
113113
liftIO . logInfo tracer $
114114
"Initial genesis distribution populated. Hash "
115115
<> renderByteArray (configGenesisHash cfg)
@@ -184,10 +184,9 @@ insertTxOutsByron ::
184184
SyncEnv ->
185185
Bool ->
186186
DB.BlockId ->
187-
(Byron.Address, Byron.Lovelace) ->
187+
(Int, (Byron.Address, Byron.Lovelace)) ->
188188
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
189-
insertTxOutsByron syncEnv disInOut blkId (address, value) = do
190-
let txId = DB.TxKey $ DB.unBlockKey blkId
189+
insertTxOutsByron syncEnv disInOut blkId (txIndex, (address, value)) = do
191190
case txHashOfAddress address of
192191
Left err -> throwError err
193192
Right val -> lift $ do
@@ -232,12 +231,13 @@ insertTxOutsByron syncEnv disInOut blkId (address, value) = do
232231
vAddress = mkVAddress addrRaw
233232
addrDetailId <- insertAddressUsingCache cache UpdateCache addrRaw vAddress
234233
void . DB.insertTxOut $
235-
DB.VTxOutW (mkVTxOut txId addrDetailId) Nothing
234+
DB.VTxOutW (mkVTxOut addrDetailId) Nothing
236235
where
237236
cache = envCache syncEnv
237+
txId = DB.TxKey $ fromIntegral txIndex + DB.unBlockKey blkId
238238

239-
mkVTxOut :: DB.TxId -> V.AddressId -> V.TxOut
240-
mkVTxOut txId addrDetailId =
239+
mkVTxOut :: V.AddressId -> V.TxOut
240+
mkVTxOut addrDetailId =
241241
V.TxOut
242242
{ V.txOutTxId = txId
243243
, V.txOutIndex = 0

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ insertByronTx ::
240240
Word64 ->
241241
ExceptT SyncNodeError (ReaderT SqlBackend m) Word64
242242
insertByronTx syncEnv blkId tx blockIndex = do
243-
let txId = DB.TxKey $ DB.unBlockKey blkId
244243
disInOut <- liftIO $ getDisableInOutState syncEnv
245244
if disInOut
246245
then do
@@ -273,19 +272,20 @@ insertByronTx syncEnv blkId tx blockIndex = do
273272
}
274273

275274
pure 0
276-
else insertByronTx' syncEnv blkId tx blockIndex
275+
else insertByronTx' syncEnv blkId txId tx blockIndex
277276
where
278277
iopts = getInsertOptions syncEnv
278+
txId = DB.TxKey $ DB.unBlockKey blkId + fromIntegral blockIndex
279279

280280
insertByronTx' ::
281281
(MonadBaseControl IO m, MonadIO m) =>
282282
SyncEnv ->
283283
DB.BlockId ->
284+
DB.TxId ->
284285
Byron.TxAux ->
285286
Word64 ->
286287
ExceptT SyncNodeError (ReaderT SqlBackend m) Word64
287-
insertByronTx' syncEnv blkId tx blockIndex = do
288-
let txId = DB.TxKey $ fromIntegral $ fromIntegral (DB.unBlockId blkId) * 1000 + blockIndex
288+
insertByronTx' syncEnv blkId txId tx blockIndex = do
289289
resolvedInputs <- mapM (resolveTxInputs txOutTableType) (toList $ Byron.txInputs (Byron.taTx tx))
290290
valFee <- firstExceptT annotateTx $ ExceptT $ pure (calculateTxFee (Byron.taTx tx) resolvedInputs)
291291
lift $
@@ -324,7 +324,7 @@ insertByronTx' syncEnv blkId tx blockIndex = do
324324
mapM_ (insertTxIn tracer txId) resolvedInputs
325325
whenConsumeOrPruneTxOut syncEnv $
326326
lift $
327-
DB.updateListTxOutConsumedByTxId (prepUpdate txId <$> resolvedInputs)
327+
DB.updateListTxOutConsumedByTxId (prepUpdate <$> resolvedInputs)
328328
-- fees are being returned so we can sum them and put them in cache to use when updating epochs
329329
pure $ unDbLovelace $ vfFee valFee
330330
where
@@ -340,7 +340,7 @@ insertByronTx' syncEnv blkId tx blockIndex = do
340340
SNErrInvariant loc ei -> SNErrInvariant loc (annotateInvariantTx (Byron.taTx tx) ei)
341341
_other -> ee
342342

343-
prepUpdate txId (_, _, txOutId, _) = (txOutId, txId)
343+
prepUpdate (_, _, txOutId, _) = (txOutId, txId)
344344

345345
insertTxOutByron ::
346346
(MonadBaseControl IO m, MonadIO m) =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ insertTxOuts syncEnv blkId (txIndex, (TxIn txInId _, txOut)) = do
269269
addrDetailId <- insertAddressUsingCache cache UpdateCache addrRaw vAddress
270270
void . DB.insertTxOut $ DB.VTxOutW (makeVTxOut addrDetailId) Nothing
271271
where
272-
txId = DB.TxKey $ fromIntegral txIndex + 1000 * DB.unBlockKey blkId
272+
txId = DB.TxKey $ fromIntegral txIndex + 20000 * DB.unBlockKey blkId -- On mainnet genesis produces ~14000 txs
273273
addr = txOut ^. Core.addrTxOutL
274274
cache = envCache syncEnv
275275
hasScript = maybe False Generic.hasCredScript (Generic.getPaymentCred addr)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ insertBlockUniversal ::
9191
Bool ->
9292
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
9393
insertBlockUniversal syncEnv blkId genericBlock blk applyResult shouldLog = do
94-
when (isSyncedWithintwoMinutes details) $ lift $ optimiseCaches $ envCache syncEnv
94+
-- when (isSyncedWithintwoMinutes details) $ lift $ optimiseCaches $ envCache syncEnv
9595
lift $
9696
insertBlockAndCache cache blkId $
9797
blk

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ prepareTxGrouped ::
7171
SyncEnv ->
7272
[(TxIdLedger, DB.TxId)] ->
7373
DB.BlockId ->
74+
DB.TxId ->
7475
Generic.Tx ->
7576
ExceptT SyncNodeError (ReaderT SqlBackend m) ((DB.TxId, DB.Tx, Generic.Tx), BlockGroupedData)
76-
prepareTxGrouped syncEnv txHashes blkId tx = do
77+
prepareTxGrouped syncEnv txHashes blkId txId tx = do
7778
disInOut <- liftIO $ getDisableInOutState syncEnv
7879
txDb <- lift $ prepareTx syncEnv blkId tx
7980
txIns <- whenTrueMempty disInOut $ mapM (prepareTxIn syncEnv txHashes txId) (Generic.txInputs tx)
@@ -91,8 +92,6 @@ prepareTxGrouped syncEnv txHashes blkId tx = do
9192
Generic.txMint tx
9293
pure ((txId, txDb, tx), BlockGroupedData txIns txOuts txMetadata maTxMint 0 outSum)
9394
where
94-
blockIndex = Generic.txBlockIndex tx
95-
txId = DB.TxKey $ fromIntegral $ fromIntegral (DB.unBlockId blkId) * 1000 + blockIndex
9695
tracer = getTrace syncEnv
9796
cache = envCache syncEnv
9897
iopts = getInsertOptions syncEnv
@@ -150,8 +149,6 @@ prepareTxIn syncEnv txHashes txId txIn = do
150149
, DB.txInRedeemerId = Nothing -- Remove or fix later https://github.com/IntersectMBO/cardano-db-sync/issues/1746
151150
}
152151

153-
-- let txId = DB.TxKey $ fromIntegral $ fromIntegral (DB.unBlockId blkId) * 1000 + blockIndex
154-
155152
insertTxRest ::
156153
(MonadBaseControl IO m, MonadIO m) =>
157154
SyncEnv ->

cardano-db/src/Cardano/Db/Util.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ import Cardano.Db.Schema.BaseSchema
44
import Data.Word (Word64)
55

66
toTxId :: BlockId -> Word64 -> TxId
7-
toTxId blockId blockIndex = TxKey $ 1000 * unBlockKey blockId + fromIntegral blockIndex
7+
toTxId blockId blockIndex = TxKey $ 100000 * unBlockKey blockId + fromIntegral blockIndex

0 commit comments

Comments
 (0)