Skip to content

Commit 00b72af

Browse files
authored
Merge pull request #166 from input-output-hk/erikd/tx-depost-refund
Shelley: Add a deposit column to tx table
2 parents 150ee4b + a7ba1dd commit 00b72af

File tree

12 files changed

+47
-11
lines changed

12 files changed

+47
-11
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ insertTxOuts blkId (address, value) = do
182182
, DB.txBlockIndex = 0
183183
, DB.txOutSum = Byron.unsafeGetLovelace value
184184
, DB.txFee = 0
185+
, DB.txDeposit = 0
185186
, DB.txSize = 0 -- Genesis distribution address to not have a size.
186187
}
187188
void . DB.insertTxOut $

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ insertTxOuts blkId (Shelley.TxIn txInId _, txOut) = do
198198
, DB.txBlockIndex = 0
199199
, DB.txOutSum = unCoin (txOutCoin txOut)
200200
, DB.txFee = 0
201+
, DB.txDeposit = 0
201202
, DB.txSize = 0 -- Genesis distribution address to not have a size.
202203
}
203204
void . DB.insertTxOut $

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ insertTx tracer blkId tx blockIndex = do
169169
, DB.txBlockIndex = blockIndex
170170
, DB.txOutSum = vfValue valFee
171171
, DB.txFee = vfFee valFee
172+
, DB.txDeposit = 0 -- Byron does not have deposits/refunds
172173
-- Would be really nice to have a way to get the transaction size
173174
-- without re-serializing it.
174175
, DB.txSize = fromIntegral $ BS.length (serialize' $ Byron.taTx tx)

cardano-db-sync/src/Cardano/DbSync/Plugin/Default/Shelley/Insert.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,17 @@ insertTx
103103
-> ExceptT DbSyncNodeError (ReaderT SqlBackend m) ()
104104
insertTx tracer env blkId blockIndex tx = do
105105
-- Insert transaction and get txId from the DB.
106+
let outSum = Shelley.txOutputSum tx
107+
fees = Shelley.txFee tx
108+
inSum <- lift $ queryTxInputSum (Shelley.txInputList tx)
106109
txId <- lift . DB.insertTx $
107110
DB.Tx
108111
{ DB.txHash = Shelley.txHash tx
109112
, DB.txBlock = blkId
110113
, DB.txBlockIndex = blockIndex
111-
, DB.txOutSum = Shelley.txOutputSum tx
112-
, DB.txFee = Shelley.txFee tx
114+
, DB.txOutSum = outSum
115+
, DB.txFee = fees
116+
, DB.txDeposit = fromIntegral inSum - fromIntegral (outSum + fees)
113117
, DB.txSize = fromIntegral $ LBS.length (Shelley.txFullBytes tx)
114118
}
115119

cardano-db-sync/src/Cardano/DbSync/Plugin/Default/Shelley/Query.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
module Cardano.DbSync.Plugin.Default.Shelley.Query
77
( queryStakeAddress
88
, queryStakePoolKeyHash
9+
, queryTxInputSum
910
) where
1011

1112

13+
import qualified Cardano.Crypto.Hash as Crypto
1214
import Cardano.Db
1315
import Cardano.DbSync.Types
1416
import Cardano.DbSync.Era.Shelley.Util (unKeyHashBS)
@@ -17,11 +19,15 @@ import Control.Monad.IO.Class (MonadIO)
1719
import Control.Monad.Trans.Reader (ReaderT)
1820

1921
import Data.ByteString.Char8 (ByteString)
22+
import Data.Either (fromRight)
2023
import Data.Maybe (listToMaybe)
24+
import Data.Word (Word64)
2125

2226
import Database.Esqueleto (Value (..), (^.), (==.), from, select, val, where_)
2327
import Database.Persist.Sql (SqlBackend)
2428

29+
import qualified Shelley.Spec.Ledger.TxData as Shelley
30+
2531

2632
queryStakeAddress :: MonadIO m => ByteString -> ReaderT SqlBackend m (Either LookupFail StakeAddressId)
2733
queryStakeAddress addr = do
@@ -37,3 +43,10 @@ queryStakePoolKeyHash kh = do
3743
pure (pool ^. PoolId)
3844
pure $ maybeToEither (DbLookupMessage "StakePoolKeyHash") unValue (listToMaybe res)
3945

46+
queryTxInputSum :: MonadIO m => [ShelleyTxIn] -> ReaderT SqlBackend m Word64
47+
queryTxInputSum txins =
48+
sum <$> mapM queryTxInputValue txins
49+
where
50+
queryTxInputValue :: MonadIO m => ShelleyTxIn -> ReaderT SqlBackend m Word64
51+
queryTxInputValue (Shelley.TxIn (Shelley.TxId hash) index) =
52+
fromRight 0 <$> queryTxOutValue (Crypto.getHash hash, fromIntegral index)

cardano-db/app/Cardano/Db/App/Validate/TotalSupply.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ validateTotalSupplyDecreasing :: IO ()
1717
validateTotalSupplyDecreasing = do
1818
test <- genTestParameters
1919

20-
putStrF $ "Total supply plus fees at block " ++ show (testFirstBlockNo test)
20+
putStrF $ "Total supply + fees + deposit at block " ++ show (testFirstBlockNo test)
2121
++ " is same as genesis supply: "
22-
(fee1, supply1) <- runDbNoLogging $ do
23-
(,) <$> queryFeesUpToBlockNo (testFirstBlockNo test)
24-
<*> fmap2 utxoSetSum queryUtxoAtBlockNo (testFirstBlockNo test)
25-
if genesisSupply test == supply1 + fee1
22+
(fee1, depost1, supply1)
23+
<- runDbNoLogging $ do
24+
(,,) <$> queryFeesUpToBlockNo (testFirstBlockNo test)
25+
<*> queryDepositUpToBlockNo (testFirstBlockNo test)
26+
<*> fmap2 utxoSetSum queryUtxoAtBlockNo (testFirstBlockNo test)
27+
if genesisSupply test == supply1 + fee1 + depost1
2628
then putStrLn $ greenText "ok"
27-
else error $ redText (show (genesisSupply test) ++ " /= " ++ show (supply1 + fee1))
29+
else error $ redText (show (genesisSupply test) ++ " /= " ++ show (supply1 + fee1 + depost1))
2830

2931
putStrF $ "Validate total supply decreasing from block " ++ show (testFirstBlockNo test)
3032
++ " to block " ++ show (testSecondBlockNo test) ++ ": "

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Cardano.Db.Query
1515
, queryBlockTxCount
1616
, queryCalcEpochEntry
1717
, queryCheckPoints
18+
, queryDepositUpToBlockNo
1819
, queryEpochEntry
1920
, queryEpochNo
2021
, queryFeesUpToBlockNo
@@ -252,6 +253,16 @@ queryCheckPoints limitCount = do
252253
then [ end, end - end `div` limitCount .. 1 ]
253254
else [ end, end - 2 .. 1 ]
254255

256+
queryDepositUpToBlockNo :: MonadIO m => Word64 -> ReaderT SqlBackend m Ada
257+
queryDepositUpToBlockNo slotNo = do
258+
res <- select . from $ \ (tx `InnerJoin` blk) -> do
259+
on (tx ^. TxBlock ==. blk ^. BlockId)
260+
where_ (isJust $ blk ^. BlockSlotNo)
261+
where_ (blk ^. BlockSlotNo <=. just (val slotNo))
262+
pure $ sum_ (tx ^. TxDeposit)
263+
pure $ unValueSumAda (listToMaybe res)
264+
265+
255266
queryEpochEntry :: MonadIO m => Word64 -> ReaderT SqlBackend m (Either LookupFail Epoch)
256267
queryEpochEntry epochNum = do
257268
res <- select . from $ \ epoch -> do

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
module Cardano.Db.Schema where
1818

1919
import Data.ByteString.Char8 (ByteString)
20+
import Data.Int (Int64)
2021
import Data.Text (Text)
2122
import Data.Time.Clock (UTCTime)
2223
import Data.Word (Word16, Word64)
@@ -86,6 +87,7 @@ share
8687
blockIndex Word64 sqltype=uinteger -- The index of this transaction within the block.
8788
outSum Word64 sqltype=lovelace
8889
fee Word64 sqltype=lovelace
90+
deposit Int64
8991
size Word64 sqltype=uinteger
9092
UniqueTx hash
9193

cardano-db/test/Test/IO/Cardano/Db/Rollback.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ createAndInsertBlocks blockCount =
9999
newMTxOutId <- if indx /= 0
100100
then pure mTxOutId
101101
else do
102-
txId <- insertTx $ Tx (mkTxHash blkId 0) blkId 0 0 0 12
102+
txId <- insertTx $ Tx (mkTxHash blkId 0) blkId 0 0 0 0 12
103103
void $ insertTxOut (mkTxOut blkId txId)
104104
pure $ Just txId
105105
case (indx, mTxOutId) of

cardano-db/test/Test/IO/Cardano/Db/TotalSupply.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ initialSupplyTest =
3636

3737
-- Spend from the Utxo set.
3838
bid1 <- insertBlock (mkBlock 1 slid)
39-
tx1Id <- insertTx (Tx (mkTxHash bid1 1) bid1 0 500000000 100 123)
39+
tx1Id <- insertTx (Tx (mkTxHash bid1 1) bid1 0 500000000 100 0 123)
4040
_ <- insertTxIn (TxIn tx1Id (head tx0Ids) 0)
4141
_ <- insertTxOut $ TxOut tx1Id 0 (mkAddressHash bid1 tx1Id) 500000000
4242
supply1 <- queryTotalSupply

cardano-db/test/Test/IO/Cardano/Db/Util.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ mkTxs :: BlockId -> Word -> [Tx]
7373
mkTxs blkId count =
7474
take (fromIntegral count) $ map create [ 0 .. ]
7575
where
76-
create w = Tx (mkTxHash blkId w) blkId 0 2 1 12
76+
create w = Tx (mkTxHash blkId w) blkId 0 2 1 0 12
7777

7878
testSlotLeader :: SlotLeader
7979
testSlotLeader =

schema/migration-2-0003-20200630.sql renamed to schema/migration-2-0003-20200702.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ BEGIN
1010
EXECUTE 'ALTER TABLE "block" ADD COLUMN "vrf_key" hash32type NULL' ;
1111
EXECUTE 'ALTER TABLE "block" ADD COLUMN "op_cert" hash32type NULL' ;
1212
EXECUTE 'ALTER TABLE "block" ADD COLUMN "proto_version" VARCHAR NULL' ;
13+
EXECUTE 'ALTER TABLE "tx" ADD COLUMN "deposit" INT8 NOT NULL' ;
1314
EXECUTE 'CREATe TABLE "stake_address"("id" SERIAL8 PRIMARY KEY UNIQUE,"hash" addr29type NOT NULL)' ;
1415
EXECUTE 'ALTER TABLE "stake_address" ADD CONSTRAINT "unique_stake_address" UNIQUE("hash")' ;
1516
EXECUTE 'CREATe TABLE "pool_meta_data"("id" SERIAL8 PRIMARY KEY UNIQUE,"url" VARCHAR NOT NULL,"hash" hash32type NOT NULL,"tx_id" INT8 NOT NULL)' ;

0 commit comments

Comments
 (0)