Skip to content

Commit

Permalink
db: Add a field to the Tx table which holds the block index
Browse files Browse the repository at this point in the history
This new field is the index of the transaction within a block. There
is no easy way to write a migration for this, so the database needs to
be dropped and recreated.

Closes: #91
  • Loading branch information
erikd committed May 18, 2020
1 parent ff2a3c6 commit 2cbd621
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions cardano-db-sync/src/Cardano/DbSync/Genesis.hs
Expand Up @@ -172,6 +172,7 @@ insertTxOuts blkId (address, value) = do
DB.Tx
{ DB.txHash = unTxHash $ txHashOfAddress address
, DB.txBlock = blkId
, DB.txBlockIndex = 0
, DB.txOutSum = Ledger.unsafeGetLovelace value
, DB.txFee = 0
, DB.txSize = 0 -- Genesis distribution address to not have a size.
Expand Down
17 changes: 11 additions & 6 deletions cardano-db-sync/src/Cardano/DbSync/Plugin/Default/Insert.hs
Expand Up @@ -120,17 +120,21 @@ insertABlock tracer blk tip = do
, DB.blockTxCount = fromIntegral $ length (blockPayload blk)
}

mapMVExceptT (insertTx tracer blkId) $ blockPayload blk
mapMVExceptT (insertTx tracer blkId) $ zip (blockPayload blk) [ 0 .. ]

liftIO $ do
let followingClosely = withOrigin 0 unBlockNo (getTipBlockNo tip) - blockNumber blk < 20
(epoch, slotWithin) = slotNumber blk `divMod` slotsPerEpoch
when (followingClosely && slotWithin /= 0 && slotNumber blk > 0 && slotNumber blk `mod` 20 == 0) $ do
(epoch, slotWithinEpoch) = slotNumber blk `divMod` slotsPerEpoch
when (followingClosely && slotWithinEpoch /= 0 && slotNumber blk > 0 && slotNumber blk `mod` 20 == 0) $ do
logInfo tracer $
mconcat
[ "insertABlock: continuing epoch ", textShow epoch
, " (slot ", textShow slotWithin, ")"
, " (slot ", textShow slotWithinEpoch, ")"
]

when (blockNumber blk `mod` 100 == 0) $
logInfo tracer $ textShow (withOrigin 0 unBlockNo (getTipBlockNo tip), slotNumber blk, blockNumber blk)

logger tracer $ mconcat
[ "insertABlock: slot ", textShow (slotNumber blk)
, ", block ", textShow (blockNumber blk)
Expand All @@ -146,14 +150,15 @@ insertABlock tracer blk tip = do

insertTx
:: MonadIO m
=> Trace IO Text -> DB.BlockId -> Ledger.TxAux
=> Trace IO Text -> DB.BlockId -> (Ledger.TxAux, Word64)
-> ExceptT DbSyncNodeError (ReaderT SqlBackend m) ()
insertTx tracer blkId tx = do
insertTx tracer blkId (tx, blockIndex) = do
valFee <- firstExceptT annotateTx $ newExceptT (calculateTxFee $ Ledger.taTx tx)
txId <- lift . DB.insertTx $
DB.Tx
{ DB.txHash = unTxHash $ Crypto.serializeCborHash (Ledger.taTx tx)
, DB.txBlock = blkId
, DB.txBlockIndex = blockIndex
, DB.txOutSum = vfValue valFee
, DB.txFee = vfFee valFee
-- Would be really nice to have a way to get the transaction size
Expand Down
1 change: 1 addition & 0 deletions cardano-db/src/Cardano/Db/Schema.hs
Expand Up @@ -77,6 +77,7 @@ share
Tx
hash ByteString sqltype=hash32type
block BlockId -- This type is the primary key for the 'block' table.
blockIndex Word64 sqltype=uinteger -- The index of this transaction within the block.
outSum Word64 sqltype=lovelace
fee Word64 sqltype=lovelace
size Word64 sqltype=uinteger
Expand Down
2 changes: 1 addition & 1 deletion cardano-db/test/Test/IO/Cardano/Db/Rollback.hs
Expand Up @@ -96,7 +96,7 @@ createAndInsertBlocks blockCount =
newMTxOutId <- if indx /= 0
then pure mTxOutId
else do
txId <- insertTx $ Tx (mkTxHash blkId 0) blkId 0 0 12
txId <- insertTx $ Tx (mkTxHash blkId 0) blkId 0 0 0 12
void $ insertTxOut (mkTxOut blkId txId)
pure $ Just txId
case (indx, mTxOutId) of
Expand Down
2 changes: 1 addition & 1 deletion cardano-db/test/Test/IO/Cardano/Db/TotalSupply.hs
Expand Up @@ -36,7 +36,7 @@ initialSupplyTest =

-- Spend from the Utxo set.
bid1 <- insertBlock (mkBlock 1 slid)
tx1Id <- insertTx (Tx (mkTxHash bid1 1) bid1 500000000 100 123)
tx1Id <- insertTx (Tx (mkTxHash bid1 1) bid1 0 500000000 100 123)
_ <- insertTxIn (TxIn tx1Id (head tx0Ids) 0)
_ <- insertTxOut $ TxOut tx1Id 0 (mkAddressHash bid1 tx1Id) 500000000
supply1 <- queryTotalSupply
Expand Down
2 changes: 1 addition & 1 deletion cardano-db/test/Test/IO/Cardano/Db/Util.hs
Expand Up @@ -72,7 +72,7 @@ mkTxs :: BlockId -> Word -> [Tx]
mkTxs blkId count =
take (fromIntegral count) $ map create [ 0 .. ]
where
create w = Tx (mkTxHash blkId w) blkId 2 1 12
create w = Tx (mkTxHash blkId w) blkId 0 2 1 12

testSlotLeader :: SlotLeader
testSlotLeader =
Expand Down
19 changes: 19 additions & 0 deletions schema/migration-2-0013-20200517.sql
@@ -0,0 +1,19 @@
-- Persistent generated migration.

CREATE FUNCTION migrate() RETURNS void AS $$
DECLARE
next_version int ;
BEGIN
SELECT stage_two + 1 INTO next_version FROM schema_version ;
IF next_version = 13 THEN
EXECUTE 'ALTER TABLE "tx" ADD COLUMN "block_index" INT4 NOT NULL' ;
-- Hand written SQL statements can be added here.
UPDATE schema_version SET stage_two = 13 ;
RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ;
END IF ;
END ;
$$ LANGUAGE plpgsql ;

SELECT migrate() ;

DROP FUNCTION migrate() ;

0 comments on commit 2cbd621

Please sign in to comment.