Skip to content

Commit

Permalink
Modify SQLite function selectTxs to return tokens with tx outputs f…
Browse files Browse the repository at this point in the history
…or resolved inputs.
  • Loading branch information
jonathanknowles committed Dec 2, 2020
1 parent ec7ea22 commit 15d909e
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Expand Up @@ -1278,7 +1278,7 @@ txHistoryFromEntity
=> TimeInterpreter m
-> W.BlockHeader
-> [TxMeta]
-> [(TxIn, Maybe TxOut)]
-> [(TxIn, Maybe (TxOut, [TxOutToken]))]
-> [(TxOut, [TxOutToken])]
-> [TxWithdrawal]
-> m [W.TransactionInfo]
Expand All @@ -1293,7 +1293,9 @@ txHistoryFromEntity ti tip metas ins outs ws =
{ W.txInfoId =
getTxId txid
, W.txInfoInputs =
map mkTxIn $ filter ((== txid) . txInputTxId . fst) ins
map mkTxIn
$ filter ((== txid) . txInputTxId . fst)
$ fmap (fmap (fmap fst)) ins
, W.txInfoOutputs =
map mkTxOut
$ filter ((== txid) . txOutputTxId)
Expand Down Expand Up @@ -1509,7 +1511,7 @@ selectUTxO cp = fmap entityVal <$>
selectTxs
:: [TxId]
-> SqlPersistT IO
( [(TxIn, Maybe (TxOut))]
( [(TxIn, Maybe (TxOut, [TxOutToken]))]
, [(TxOut, [TxOutToken])]
, [TxWithdrawal]
)
Expand All @@ -1520,10 +1522,17 @@ selectTxs = fmap concatUnzip . mapM select . chunksOf chunkSize
[TxInputTxId <-. txids]
[Asc TxInputTxId, Asc TxInputOrder]

resolvedInputs <- toOutputMap . fmap entityVal <$>
combineChunked inputs (\inputsChunk -> selectList
[TxOutputTxId <-. (txInputSourceTxId <$> inputsChunk)]
[Asc TxOutputTxId, Asc TxOutputIndex])
resolvedInputs <- fmap toOutputMap $
combineChunked inputs $ \inputsChunk -> do
outs <- fmap entityVal <$> selectList
[TxOutputTxId <-. (txInputSourceTxId <$> inputsChunk)]
[Asc TxOutputTxId, Asc TxOutputIndex]
forM outs $ \out ->
(out,) . fmap entityVal <$> selectList
[ TxOutTokenTxId ==. txOutputTxId out
, TxOutTokenTxIndex ==. txOutputIndex out
]
[]

outputs <- do
outs <- fmap entityVal <$> selectList
Expand All @@ -1546,11 +1555,16 @@ selectTxs = fmap concatUnzip . mapM select . chunksOf chunkSize
, withdrawals
)

toOutputMap :: [TxOut] -> Map (TxId, Word32) TxOut
toOutputMap = Map.fromList . fmap
(\out -> let key = (txOutputTxId out, txOutputIndex out) in (key, out))
toOutputMap
:: [(TxOut, [TxOutToken])]
-> Map (TxId, Word32) (TxOut, [TxOutToken])
toOutputMap = Map.fromList . fmap toEntry
where
toEntry (out, tokens) = (key, (out, tokens))
where
key = (txOutputTxId out, txOutputIndex out)

resolveWith :: [TxIn] -> Map (TxId, Word32) TxOut -> [(TxIn, Maybe TxOut)]
resolveWith :: [TxIn] -> Map (TxId, Word32) txOut -> [(TxIn, Maybe txOut)]
resolveWith inputs resolvedInputs =
[ (i, Map.lookup key resolvedInputs)
| i <- inputs
Expand Down

0 comments on commit 15d909e

Please sign in to comment.