Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Rename txOutSpent/txOutUnspent to txOutSpentP/txOutUnspentP
Browse files Browse the repository at this point in the history
The 'P' indicates that ies a predicate. Also add txOutSpentB which
returns a 'Bool' value.
  • Loading branch information
erikd committed Oct 19, 2019
1 parent e4f784a commit 9e6d169
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
40 changes: 25 additions & 15 deletions cardano-explorer-db/src/Explorer/DB/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ module Explorer.DB.Query
, renderLookupFail
, slotPosixTime
, slotUtcTime
, txOutUnspent
, txOutSpent
, txOutSpentB
, txOutSpentP
, txOutUnspentP
, unValueSumAda
, unBlockId
, unValue2
Expand Down Expand Up @@ -270,7 +271,7 @@ querySlotUtcTime slotNo =
queryTotalSupply :: MonadIO m => ReaderT SqlBackend m Ada
queryTotalSupply = do
res <- select . from $ \ txOut -> do
txOutUnspent txOut
txOutUnspentP txOut
pure $ sum_ (txOut ^. TxOutValue)
pure $ unValueSumAda (listToMaybe res)

Expand Down Expand Up @@ -356,23 +357,32 @@ queryUtxoAtSlotNo slotNo = do
isJust :: PersistField a => SqlExpr (Value (Maybe a)) -> SqlExpr (Value Bool)
isJust = not_ . isNothing

-- Returns True if the TxOut has been spent.
{-# INLINABLE txOutSpentB #-}
txOutSpentB :: SqlExpr (Entity TxOut) -> SqlExpr (Value Bool)
txOutSpentB txOut =
exists . from $ \ txIn ->
where_ (txOut ^. TxOutTxId ==. txIn ^. TxInTxOutId
&&. txOut ^. TxOutIndex ==. txIn ^. TxInTxOutIndex
)

-- A predicate that filters out unspent 'TxOut' entries.
{-# INLINABLE txOutSpent #-}
txOutSpent :: SqlExpr (Entity TxOut) -> SqlQuery ()
txOutSpent txOut =
{-# INLINABLE txOutSpentP #-}
txOutSpentP :: SqlExpr (Entity TxOut) -> SqlQuery ()
txOutSpentP txOut =
where_ . exists . from $ \ txIn -> do
where_ (txOut ^. TxOutTxId ==. txIn ^. TxInTxOutId
&&. txOut ^. TxOutIndex ==. txIn ^. TxInTxOutIndex
)
where_ (txOut ^. TxOutTxId ==. txIn ^. TxInTxOutId
&&. txOut ^. TxOutIndex ==. txIn ^. TxInTxOutIndex
)

-- A predicate that filters out spent 'TxOut' entries.
{-# INLINABLE txOutUnspent #-}
txOutUnspent :: SqlExpr (Entity TxOut) -> SqlQuery ()
txOutUnspent txOut =
{-# INLINABLE txOutUnspentP #-}
txOutUnspentP :: SqlExpr (Entity TxOut) -> SqlQuery ()
txOutUnspentP txOut =
where_ . notExists . from $ \ txIn -> do
where_ (txOut ^. TxOutTxId ==. txIn ^. TxInTxOutId
&&. txOut ^. TxOutIndex ==. txIn ^. TxInTxOutIndex
)
where_ (txOut ^. TxOutTxId ==. txIn ^. TxInTxOutId
&&. txOut ^. TxOutIndex ==. txIn ^. TxInTxOutIndex
)

-- every tx made before or at the snapshot time
txLessEqual :: BlockId -> SqlExpr (ValueList TxId)
Expand Down
6 changes: 3 additions & 3 deletions cardano-explorer/src/Explorer/Web/Server/GenesisPages.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Database.Esqueleto (InnerJoin (..), Value,
(^.), (==.), countRows, from, on, select, unValue, val, where_)
import Database.Persist.Sql (SqlBackend)

import Explorer.DB (EntityField (..), listToMaybe, txOutSpent, txOutUnspent)
import Explorer.DB (EntityField (..), listToMaybe, txOutSpentP, txOutUnspentP)

import Explorer.Web.ClientTypes (CAddressesFilter (..))
import Explorer.Web.Error (ExplorerError (..))
Expand Down Expand Up @@ -47,7 +47,7 @@ queryRedeemedGenesisAddressCount pageSize = do
res <- select . from $ \ (blk `InnerJoin` tx `InnerJoin` txOut) -> do
on (tx ^. TxId ==. txOut ^. TxOutTxId)
on (blk ^. BlockId ==. tx ^. TxBlock)
txOutSpent txOut
txOutSpentP txOut
-- Only the initial genesis block has a size of 0.
where_ (blk ^. BlockSize ==. val 0)
pure countRows
Expand All @@ -58,7 +58,7 @@ queryUnRedeemedGenesisAddressCount pageSize = do
res <- select . from $ \ (blk `InnerJoin` tx `InnerJoin` txOut) -> do
on (tx ^. TxId ==. txOut ^. TxOutTxId)
on (blk ^. BlockId ==. tx ^. TxBlock)
txOutUnspent txOut
txOutUnspentP txOut
-- Only the initial genesis block has a size of 0.
where_ (blk ^. BlockSize ==. val 0)
pure countRows
Expand Down
4 changes: 2 additions & 2 deletions cardano-explorer/src/Explorer/Web/Server/GenesisSummary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Database.Esqueleto (InnerJoin (..), Value,
val, where_)
import Database.Persist.Sql (SqlBackend)

import Explorer.DB (EntityField (..), listToMaybe, txOutSpent)
import Explorer.DB (EntityField (..), listToMaybe, txOutSpentP)

import Explorer.Web.ClientTypes (CGenesisSummary (..), mkCCoin)
import Explorer.Web.Error (ExplorerError (..))
Expand Down Expand Up @@ -52,7 +52,7 @@ queryGenesisRedeemed = do
res <- select . from $ \ (blk `InnerJoin` tx `InnerJoin` txOut) -> do
on (tx ^. TxId ==. txOut ^. TxOutTxId)
on (blk ^. BlockId ==. tx ^. TxBlock)
txOutSpent txOut
txOutSpentP txOut
-- Only the initial genesis block has a size of 0.
where_ (blk ^. BlockSize ==. val 0)
pure (countRows, sum_ (txOut ^. TxOutValue))
Expand Down

0 comments on commit 9e6d169

Please sign in to comment.