Skip to content

Commit

Permalink
Merge pull request #166 from input-output-hk/erikd/tx-depost-refund
Browse files Browse the repository at this point in the history
Shelley: Add a deposit column to tx table
  • Loading branch information
erikd committed Jul 3, 2020
2 parents 150ee4b + a7ba1dd commit 00b72af
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions cardano-db-sync/src/Cardano/DbSync/Era/Byron/Genesis.hs
Expand Up @@ -182,6 +182,7 @@ insertTxOuts blkId (address, value) = do
, DB.txBlockIndex = 0
, DB.txOutSum = Byron.unsafeGetLovelace value
, DB.txFee = 0
, DB.txDeposit = 0
, DB.txSize = 0 -- Genesis distribution address to not have a size.
}
void . DB.insertTxOut $
Expand Down
1 change: 1 addition & 0 deletions cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Genesis.hs
Expand Up @@ -198,6 +198,7 @@ insertTxOuts blkId (Shelley.TxIn txInId _, txOut) = do
, DB.txBlockIndex = 0
, DB.txOutSum = unCoin (txOutCoin txOut)
, DB.txFee = 0
, DB.txDeposit = 0
, DB.txSize = 0 -- Genesis distribution address to not have a size.
}
void . DB.insertTxOut $
Expand Down
Expand Up @@ -169,6 +169,7 @@ insertTx tracer blkId tx blockIndex = do
, DB.txBlockIndex = blockIndex
, DB.txOutSum = vfValue valFee
, DB.txFee = vfFee valFee
, DB.txDeposit = 0 -- Byron does not have deposits/refunds
-- Would be really nice to have a way to get the transaction size
-- without re-serializing it.
, DB.txSize = fromIntegral $ BS.length (serialize' $ Byron.taTx tx)
Expand Down
Expand Up @@ -103,13 +103,17 @@ insertTx
-> ExceptT DbSyncNodeError (ReaderT SqlBackend m) ()
insertTx tracer env blkId blockIndex tx = do
-- Insert transaction and get txId from the DB.
let outSum = Shelley.txOutputSum tx
fees = Shelley.txFee tx
inSum <- lift $ queryTxInputSum (Shelley.txInputList tx)
txId <- lift . DB.insertTx $
DB.Tx
{ DB.txHash = Shelley.txHash tx
, DB.txBlock = blkId
, DB.txBlockIndex = blockIndex
, DB.txOutSum = Shelley.txOutputSum tx
, DB.txFee = Shelley.txFee tx
, DB.txOutSum = outSum
, DB.txFee = fees
, DB.txDeposit = fromIntegral inSum - fromIntegral (outSum + fees)
, DB.txSize = fromIntegral $ LBS.length (Shelley.txFullBytes tx)
}

Expand Down
13 changes: 13 additions & 0 deletions cardano-db-sync/src/Cardano/DbSync/Plugin/Default/Shelley/Query.hs
Expand Up @@ -6,9 +6,11 @@
module Cardano.DbSync.Plugin.Default.Shelley.Query
( queryStakeAddress
, queryStakePoolKeyHash
, queryTxInputSum
) where


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

import Data.ByteString.Char8 (ByteString)
import Data.Either (fromRight)
import Data.Maybe (listToMaybe)
import Data.Word (Word64)

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

import qualified Shelley.Spec.Ledger.TxData as Shelley


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

queryTxInputSum :: MonadIO m => [ShelleyTxIn] -> ReaderT SqlBackend m Word64
queryTxInputSum txins =
sum <$> mapM queryTxInputValue txins
where
queryTxInputValue :: MonadIO m => ShelleyTxIn -> ReaderT SqlBackend m Word64
queryTxInputValue (Shelley.TxIn (Shelley.TxId hash) index) =
fromRight 0 <$> queryTxOutValue (Crypto.getHash hash, fromIntegral index)
14 changes: 8 additions & 6 deletions cardano-db/app/Cardano/Db/App/Validate/TotalSupply.hs
Expand Up @@ -17,14 +17,16 @@ validateTotalSupplyDecreasing :: IO ()
validateTotalSupplyDecreasing = do
test <- genTestParameters

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

putStrF $ "Validate total supply decreasing from block " ++ show (testFirstBlockNo test)
++ " to block " ++ show (testSecondBlockNo test) ++ ": "
Expand Down
11 changes: 11 additions & 0 deletions cardano-db/src/Cardano/Db/Query.hs
Expand Up @@ -15,6 +15,7 @@ module Cardano.Db.Query
, queryBlockTxCount
, queryCalcEpochEntry
, queryCheckPoints
, queryDepositUpToBlockNo
, queryEpochEntry
, queryEpochNo
, queryFeesUpToBlockNo
Expand Down Expand Up @@ -252,6 +253,16 @@ queryCheckPoints limitCount = do
then [ end, end - end `div` limitCount .. 1 ]
else [ end, end - 2 .. 1 ]

queryDepositUpToBlockNo :: MonadIO m => Word64 -> ReaderT SqlBackend m Ada
queryDepositUpToBlockNo slotNo = do
res <- select . from $ \ (tx `InnerJoin` blk) -> do
on (tx ^. TxBlock ==. blk ^. BlockId)
where_ (isJust $ blk ^. BlockSlotNo)
where_ (blk ^. BlockSlotNo <=. just (val slotNo))
pure $ sum_ (tx ^. TxDeposit)
pure $ unValueSumAda (listToMaybe res)


queryEpochEntry :: MonadIO m => Word64 -> ReaderT SqlBackend m (Either LookupFail Epoch)
queryEpochEntry epochNum = do
res <- select . from $ \ epoch -> do
Expand Down
2 changes: 2 additions & 0 deletions cardano-db/src/Cardano/Db/Schema.hs
Expand Up @@ -17,6 +17,7 @@
module Cardano.Db.Schema where

import Data.ByteString.Char8 (ByteString)
import Data.Int (Int64)
import Data.Text (Text)
import Data.Time.Clock (UTCTime)
import Data.Word (Word16, Word64)
Expand Down Expand Up @@ -86,6 +87,7 @@ share
blockIndex Word64 sqltype=uinteger -- The index of this transaction within the block.
outSum Word64 sqltype=lovelace
fee Word64 sqltype=lovelace
deposit Int64
size Word64 sqltype=uinteger
UniqueTx hash

Expand Down
2 changes: 1 addition & 1 deletion cardano-db/test/Test/IO/Cardano/Db/Rollback.hs
Expand Up @@ -99,7 +99,7 @@ createAndInsertBlocks blockCount =
newMTxOutId <- if indx /= 0
then pure mTxOutId
else do
txId <- insertTx $ Tx (mkTxHash blkId 0) blkId 0 0 0 12
txId <- insertTx $ Tx (mkTxHash blkId 0) blkId 0 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 0 500000000 100 123)
tx1Id <- insertTx (Tx (mkTxHash bid1 1) bid1 0 500000000 100 0 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 @@ -73,7 +73,7 @@ mkTxs :: BlockId -> Word -> [Tx]
mkTxs blkId count =
take (fromIntegral count) $ map create [ 0 .. ]
where
create w = Tx (mkTxHash blkId w) blkId 0 2 1 12
create w = Tx (mkTxHash blkId w) blkId 0 2 1 0 12

testSlotLeader :: SlotLeader
testSlotLeader =
Expand Down
Expand Up @@ -10,6 +10,7 @@ BEGIN
EXECUTE 'ALTER TABLE "block" ADD COLUMN "vrf_key" hash32type NULL' ;
EXECUTE 'ALTER TABLE "block" ADD COLUMN "op_cert" hash32type NULL' ;
EXECUTE 'ALTER TABLE "block" ADD COLUMN "proto_version" VARCHAR NULL' ;
EXECUTE 'ALTER TABLE "tx" ADD COLUMN "deposit" INT8 NOT NULL' ;
EXECUTE 'CREATe TABLE "stake_address"("id" SERIAL8 PRIMARY KEY UNIQUE,"hash" addr29type NOT NULL)' ;
EXECUTE 'ALTER TABLE "stake_address" ADD CONSTRAINT "unique_stake_address" UNIQUE("hash")' ;
EXECUTE 'CREATe TABLE "pool_meta_data"("id" SERIAL8 PRIMARY KEY UNIQUE,"url" VARCHAR NOT NULL,"hash" hash32type NOT NULL,"tx_id" INT8 NOT NULL)' ;
Expand Down

0 comments on commit 00b72af

Please sign in to comment.