Skip to content

Commit 55e1e59

Browse files
committed
add logging and more bug fixing
1 parent f0b99a1 commit 55e1e59

File tree

2 files changed

+43
-70
lines changed

2 files changed

+43
-70
lines changed

src/BotPlutusInterface/Balance.hs

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -320,56 +320,6 @@ getAdaChange utxos = lovelaceValue . getChange utxos
320320
getNonAdaChange :: Map TxOutRef TxOut -> Tx -> Value
321321
getNonAdaChange utxos = Ledger.noAdaValue . getChange utxos
322322

323-
{- | Getting the necessary utxos to cover the fees for the transaction
324-
collectTxIns :: Set TxIn -> Map TxOutRef TxOut -> Value -> Either Text (Set TxIn)
325-
collectTxIns originalTxIns utxos value = do
326-
updatedInputs <- selectTxInStep originalTxIns utxos value
327-
if isSufficient updatedInputs
328-
then pure updatedInputs
329-
else Left $
330-
Text.unlines
331-
[ "Insufficient tx inputs, needed: "
332-
, showText (Value.flattenValue value)
333-
, "got:"
334-
, showText (Value.flattenValue (txInsValue updatedInputs))
335-
]
336-
where
337-
338-
selectTxInStep ins utxoIndex outValue = do
339-
let txInRefs :: [TxOutRef]
340-
txInRefs = map txInRef $ Set.toList originalTxIns
341-
342-
diffUtxos :: [(TxOutRef, TxOut)]
343-
diffUtxos = Map.toList $ Map.filterWithKey (\k _ -> k `notElem` txInRefs) utxoIndex
344-
345-
case null diffUtxos of
346-
True -> return ins
347-
False -> do
348-
newIns <- selectTxIn ins utxoIndex outValue
349-
350-
if isSufficient newIns
351-
then return newIns
352-
else selectTxInStep newIns utxoIndex outValue
353-
-}
354-
355-
-- updatedInputs =
356-
-- foldl
357-
-- ( \acc txIn ->
358-
-- if isSufficient acc
359-
-- then acc
360-
-- else Set.insert txIn acc
361-
-- )
362-
-- originalTxIns
363-
-- $ mapMaybe (rightToMaybe . txOutToTxIn) $ Map.toList utxos
364-
365-
-- isSufficient :: Set TxIn -> Bool
366-
-- isSufficient txIns' =
367-
-- not (Set.null txIns') && txInsValue txIns' `Value.geq` value
368-
--
369-
-- txInsValue :: Set TxIn -> Value
370-
-- txInsValue txIns' =
371-
-- mconcat $ map Tx.txOutValue $ mapMaybe ((`Map.lookup` utxos) . Tx.txInRef) $ Set.toList txIns'
372-
373323
-- | Add min lovelaces to each tx output
374324
addLovelaces :: [(TxOut, Integer)] -> Tx -> Tx
375325
addLovelaces minLovelaces tx =

src/BotPlutusInterface/CoinSelection.hs

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import Control.Lens (Cons, cons, ix, uncons, (^?))
66
import Control.Monad.Freer (Eff, Member)
77

88
import Control.Monad.Trans.Class (lift)
9-
import Control.Monad.Trans.Either (hoistEither, runEitherT)
9+
import Control.Monad.Trans.Either (hoistEither, runEitherT, newEitherT)
1010
import Data.Either.Combinators (isRight, maybeToRight)
1111
import Data.Kind (Type)
12+
import Data.List qualified as List
1213
import Data.Map (Map)
1314
import Data.Map qualified as Map
1415
import Data.Maybe (mapMaybe)
@@ -26,7 +27,7 @@ import Plutus.V1.Ledger.Api (
2627
)
2728

2829
import BotPlutusInterface.Effects (PABEffect, printBpiLog)
29-
import BotPlutusInterface.Types (LogLevel (Notice))
30+
import BotPlutusInterface.Types (LogLevel (Debug))
3031

3132
import Prettyprinter (pretty, (<+>))
3233
import Prelude
@@ -43,8 +44,7 @@ selectTxIns ::
4344
Eff effs (Either Text (Set TxIn))
4445
selectTxIns originalTxIns utxosIndex outValue =
4546
runEitherT $ do
46-
lift $ printBpiLog @w Notice $ pretty (Map.toList utxosIndex)
47-
47+
4848
let txInsValue :: Value
4949
txInsValue =
5050
mconcat $ map txOutValue $ mapMaybe ((`Map.lookup` utxosIndex) . txInRef) $ Set.toList originalTxIns
@@ -63,7 +63,7 @@ selectTxIns originalTxIns utxosIndex outValue =
6363
(\k v -> k `notElem` txInRefs && isRight (txOutToTxIn (k, v)))
6464
utxosIndex
6565

66-
lift $ printBpiLog @w Notice $ "\n\n Remaining UTxOs: " <+> pretty remainingUtxos <+> "\n\n"
66+
lift $ printBpiLog @w Debug $ "Remaining UTxOs: " <+> pretty remainingUtxos <+> "\n\n"
6767

6868
txInsVec <-
6969
hoistEither $
@@ -73,20 +73,18 @@ selectTxIns originalTxIns utxosIndex outValue =
7373

7474
outVec <- hoistEither $ valueToVec allAssetClasses outValue
7575

76-
lift $ printBpiLog @w Notice $ "IsSufficient: " <+> pretty (isSufficient outVec txInsVec) <+> "\n\n"
77-
7876
remainingUtxosVec <- hoistEither $ mapM (valueToVec allAssetClasses . txOutValue . snd) remainingUtxos
7977

80-
selectedUtxosIdxs <- hoistEither $ selectTxIns' Greedy (isSufficient outVec) outVec txInsVec remainingUtxosVec
78+
selectedUtxosIdxs <- newEitherT $ selectTxIns' @w Greedy (isSufficient outVec) outVec txInsVec remainingUtxosVec
8179

82-
lift $ printBpiLog @w Notice $ "\n\n" <+> "Selected UTxOs Index: " <+> pretty selectedUtxosIdxs <+> "\n\n"
80+
lift $ printBpiLog @w Debug $ "" <+> "Selected UTxOs Index: " <+> pretty selectedUtxosIdxs <+> "\n\n"
8381

8482
let selectedUtxos :: [(TxOutRef, TxOut)]
8583
selectedUtxos = mapMaybe (\idx -> remainingUtxos ^? ix (fromInteger idx)) selectedUtxosIdxs
8684

8785
selectedTxIns <- hoistEither $ mapM txOutToTxIn selectedUtxos
8886

89-
lift $ printBpiLog @w Notice $ "Selected TxIns: " <+> pretty selectedTxIns <+> "\n\n"
87+
lift $ printBpiLog @w Debug $ "Selected TxIns: " <+> pretty selectedTxIns <+> "\n\n"
9088

9189
return $ originalTxIns <> Set.fromList selectedTxIns
9290
where
@@ -95,26 +93,51 @@ selectTxIns originalTxIns utxosIndex outValue =
9593
&& txInsVec /= zeroVec (toInteger $ length txInsVec)
9694

9795
selectTxIns' ::
96+
forall (w :: Type) (effs :: [Type -> Type]).
97+
Member (PABEffect w) effs =>
9898
Search ->
9999
(Vector Integer -> Bool) ->
100100
Vector Integer ->
101101
Vector Integer ->
102102
[Vector Integer] ->
103-
Either Text [Integer]
103+
Eff effs (Either Text [Integer])
104104
selectTxIns' Greedy stopSearch outVec txInsVec utxosVec
105-
| null utxosVec || stopSearch txInsVec = Right mempty
105+
| null utxosVec = printBpiLog @w Debug "The list of remanining UTxO vectors in null.\n\n"
106+
>> return (Right mempty)
107+
108+
| stopSearch txInsVec = printBpiLog @w Debug "Stopping search early.\n\n" >>
109+
return (Right mempty)
106110
| otherwise =
107-
do
108-
utxosDist <- Vec.fromList . map (l2norm outVec) <$> mapM (addVec txInsVec) utxosVec
109-
let minIndex = toInteger $ Vec.minIndex utxosDist
110-
111-
(selectedUtxoVec, remainingUtxosVec) <- pop utxosVec minIndex
111+
runEitherT $ do
112+
113+
x <- hoistEither $ mapM (addVec txInsVec) utxosVec
114+
utxosDist <- hoistEither $ mapM (l2norm outVec) x
115+
116+
let sortedDist :: [(Integer, Float)]
117+
sortedDist = List.sortBy (\a b -> compare (snd a) (snd b))
118+
$ zip [0 .. toInteger (length utxosVec) - 1] utxosDist
112119

113-
newTxInsVec <- addVec txInsVec selectedUtxoVec
120+
newEitherT $ loop sortedDist txInsVec
114121

122+
where
123+
124+
loop :: [(Integer, Float)] -> Vector Integer -> Eff effs (Either Text [Integer])
125+
loop [] _ = return $ Right mempty
126+
loop ((idx,_):remSortedDist) newTxInsVec =
115127
if stopSearch newTxInsVec
116-
then return [minIndex]
117-
else (minIndex :) <$> selectTxIns' Greedy stopSearch outVec newTxInsVec remainingUtxosVec
128+
then return $ Right mempty
129+
else
130+
runEitherT $ do
131+
selectedUtxoVec <- hoistEither $ maybeToRight "Out of bounds"
132+
(utxosVec ^? ix (fromInteger idx))
133+
newTxInsVec' <- hoistEither $ addVec newTxInsVec selectedUtxoVec
134+
135+
lift $ printBpiLog @w Debug
136+
$ "Loop Info: Stop search -> " <+> pretty (stopSearch newTxInsVec')
137+
<+> "Selected UTxo Idx : " <+> pretty idx
138+
<+> "\n\n"
139+
140+
(idx:) <$> newEitherT (loop remSortedDist newTxInsVec')
118141

119142
l2norm :: Vector Integer -> Vector Integer -> Either Text Float
120143
l2norm v1 v2

0 commit comments

Comments
 (0)