Skip to content

Commit 6600f87

Browse files
committed
merge master and fix tests
2 parents b68859a + afbafe3 commit 6600f87

File tree

9 files changed

+61
-56
lines changed

9 files changed

+61
-56
lines changed

bot-plutus-interface.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ test-suite bot-plutus-interface-test
220220
, plutus-pab
221221
, plutus-tx
222222
, plutus-tx-plugin
223+
, pretty-diff
223224
, prettyprinter
224225
, QuickCheck
225226
, quickcheck-instances

src/BotPlutusInterface/Balance.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import BotPlutusInterface.Effects (
2525
import BotPlutusInterface.Files (DummyPrivKey, unDummyPrivateKey)
2626
import BotPlutusInterface.Files qualified as Files
2727
import BotPlutusInterface.Types (
28-
CollateralUtxo,
28+
CollateralUtxo (collateralTxOutRef),
2929
LogLevel (Debug),
3030
LogType (TxBalancingLog),
3131
PABConfig,

src/BotPlutusInterface/CoinSelection.hs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ greedySearch stopSearch outVec txInsVec utxosVec
133133
zip [0 .. length utxosVec - 1] utxosDist
134134

135135
newEitherT $ loop sortedDist txInsVec
136-
137136
where
138-
139137
loop :: [(Int, Float)] -> Vector Integer -> Eff effs (Either Text [Int])
140138
loop [] _ = return $ Right mempty
141139
loop ((idx, _) : remSortedDist) newTxInsVec =
@@ -176,27 +174,24 @@ greedyPruning stopSearch outVec txInsVec utxosVec
176174
runEitherT $ do
177175
selectedUtxosIdx <- newEitherT $ greedySearch @w stopSearch outVec txInsVec utxosVec
178176

179-
let revSelectedUtxosVec :: [Vector Integer]
177+
let revSelectedUtxosVec :: [Vector Integer]
180178
revSelectedUtxosVec = List.reverse $ mapMaybe (\idx -> utxosVec ^? ix idx) selectedUtxosIdx
181179

182180
revSelectedUtxosIdx :: [Int]
183181
revSelectedUtxosIdx = List.reverse selectedUtxosIdx
184182

185183
hoistEither $ loop txInsVec revSelectedUtxosIdx revSelectedUtxosVec
186-
187184
where
188-
189185
loop :: Vector Integer -> [Int] -> [Vector Integer] -> Either Text [Int]
190-
loop newTxInsVec (idx:idxs) (vec:vecs) = do
191-
newTxInsVec' <- addVec newTxInsVec vec
192-
changeVec <- subVec outVec newTxInsVec
193-
changeVec' <- subVec outVec newTxInsVec'
194-
195-
case l2norm outVec changeVec' < l2norm outVec changeVec of
196-
True -> (idx :) <$> loop newTxInsVec' idxs vecs
197-
False | stopSearch newTxInsVec -> Right mempty
198-
False -> (idx :) <$> loop newTxInsVec' idxs vecs
199-
186+
loop newTxInsVec (idx : idxs) (vec : vecs) = do
187+
newTxInsVec' <- addVec newTxInsVec vec
188+
changeVec <- subVec outVec newTxInsVec
189+
changeVec' <- subVec outVec newTxInsVec'
190+
191+
case l2norm outVec changeVec' < l2norm outVec changeVec of
192+
True -> (idx :) <$> loop newTxInsVec' idxs vecs
193+
False | stopSearch newTxInsVec -> Right mempty
194+
False -> (idx :) <$> loop newTxInsVec' idxs vecs
200195
loop _newTxInsVec [] [] = pure mempty
201196
loop _newTxInsVec _idxs _vecs = Left "Length of idxs and list of vecs are not same."
202197

src/BotPlutusInterface/Contract.hs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module BotPlutusInterface.Contract (runContract, handleContract) where
66

7-
import BotPlutusInterface.Balance qualified as PreBalance
7+
import BotPlutusInterface.Balance qualified as Balance
88
import BotPlutusInterface.BodyBuilder qualified as BodyBuilder
99
import BotPlutusInterface.CardanoCLI qualified as CardanoCLI
1010
import BotPlutusInterface.Collateral qualified as Collateral
@@ -240,7 +240,7 @@ awaitTxStatusChange contractEnv txId = do
240240
txStatus <- getStatus
241241
case (txStatus, currBlock > cutOffBlock) of
242242
(status, True) -> do
243-
helperLog (Debug [PABLog]) . mconcat . fmap mconcat $
243+
helperLog . mconcat . fmap mconcat $
244244
[ ["Timeout for waiting `TxId ", show txId, "` status change reached"]
245245
, [" - waited ", show pollTimeout, " blocks."]
246246
, [" Current status: ", show status]
@@ -255,17 +255,17 @@ awaitTxStatusChange contractEnv txId = do
255255
mTx <- queryChainIndexForTxState
256256
case mTx of
257257
Nothing -> do
258-
helperLog (Debug [PABLog]) $ "TxId " ++ show txId ++ " not found in index"
258+
helperLog $ "TxId " ++ show txId ++ " not found in index"
259259
pure Unknown
260260
Just txState -> do
261-
helperLog (Debug [PABLog]) $ "TxId " ++ show txId ++ " found in index, checking status"
261+
helperLog $ "TxId " ++ show txId ++ " found in index, checking status"
262262
blk <- fromInteger <$> currentBlock contractEnv
263263
case transactionStatus blk txState txId of
264264
Left e -> do
265-
helperLog (Debug [PABLog]) $ "Status check for TxId " ++ show txId ++ " failed with " ++ show e
265+
helperLog $ "Status check for TxId " ++ show txId ++ " failed with " ++ show e
266266
pure Unknown
267267
Right st -> do
268-
helperLog (Debug [PABLog]) $ "Status for TxId " ++ show txId ++ " is " ++ show st
268+
helperLog $ "Status for TxId " ++ show txId ++ " is " ++ show st
269269
pure st
270270

271271
queryChainIndexForTxState :: Eff effs (Maybe TxIdState)
@@ -277,8 +277,7 @@ awaitTxStatusChange contractEnv txId = do
277277
pure . Just $ fromTx blk tx
278278
Nothing -> pure Nothing
279279

280-
helperLog :: LogLevel -> String -> Eff effs ()
281-
helperLog (Debug a) = printBpiLog @w (Debug a) . pretty
280+
helperLog = printBpiLog @w (Debug [CollateralLog]) . pretty
282281

283282
-- | This will FULLY balance a transaction
284283
balanceTx ::
@@ -296,21 +295,16 @@ balanceTx contractEnv unbalancedTx = do
296295
Left e -> pure $ BalanceTxFailed (OtherError e)
297296
_ -> do
298297
uploadDir @w pabConf.pcSigningKeyFileDir
299-
eitherPreBalancedTx <-
300-
if PreBalance.txUsesScripts (unBalancedTxTx unbalancedTx)
301-
then
302-
PreBalance.balanceTxIO' @w
303-
PreBalance.defaultBalanceConfig {PreBalance.bcHasScripts = True}
304-
pabConf
305-
pabConf.pcOwnPubKeyHash
306-
unbalancedTx
307-
else
308-
PreBalance.balanceTxIO @w
309-
pabConf
310-
pabConf.pcOwnPubKeyHash
311-
unbalancedTx
312-
313-
pure $ either (BalanceTxFailed . InsufficientFunds) (BalanceTxSuccess . Right) eitherPreBalancedTx
298+
eitherBalancedTx <-
299+
Balance.balanceTxIO' @w
300+
Balance.defaultBalanceConfig
301+
{ Balance.bcHasScripts = Balance.txUsesScripts (unBalancedTxTx unbalancedTx)
302+
}
303+
pabConf
304+
pabConf.pcOwnPubKeyHash
305+
unbalancedTx
306+
307+
pure $ either (BalanceTxFailed . InsufficientFunds) (BalanceTxSuccess . Right) eitherBalancedTx
314308

315309
-- | This step would build tx files, write them to disk and submit them to the chain
316310
writeBalancedTx ::
@@ -485,7 +479,7 @@ handleCollateral cEnv = do
485479
case result of
486480
Right collteralUtxo ->
487481
setInMemCollateral @w collteralUtxo
488-
>> Right <$> printBpiLog @w (Debug [PABLog]) "successfully set the collateral utxo in env."
482+
>> Right <$> printBpiLog @w (Debug [CollateralLog]) "successfully set the collateral utxo in env."
489483
Left err -> pure $ Left $ "Failed to make collateral: " <> err
490484
where
491485
--
@@ -510,8 +504,8 @@ makeCollateral cEnv = runEitherT $ do
510504

511505
balancedTx <-
512506
newEitherT $
513-
PreBalance.balanceTxIO' @w
514-
PreBalance.defaultBalanceConfig {PreBalance.bcHasScripts = False, PreBalance.bcSeparateChange = True}
507+
Balance.balanceTxIO' @w
508+
Balance.defaultBalanceConfig {Balance.bcHasScripts = False, Balance.bcSeparateChange = True}
515509
pabConf
516510
pabConf.pcOwnPubKeyHash unbalancedTx
517511

src/BotPlutusInterface/Effects.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import BotPlutusInterface.Types (
4444
ContractState (ContractState),
4545
LogContext (BpiLog, ContractLog),
4646
LogLevel (..),
47-
LogType (AnyLog),
47+
LogType (..),
4848
LogsList (LogsList),
4949
TxBudget,
5050
TxFile,
@@ -64,8 +64,8 @@ import Data.Aeson (ToJSON)
6464
import Data.Aeson qualified as JSON
6565
import Data.Bifunctor (second)
6666
import Data.ByteString qualified as ByteString
67-
import Data.List (intersect)
6867
import Data.Kind (Type)
68+
import Data.List (intersect)
6969
import Data.Maybe (catMaybes)
7070
import Data.String (IsString, fromString)
7171
import Data.Text (Text)
@@ -194,16 +194,16 @@ handlePABEffect contractEnv =
194194
printLog' :: LogLevel -> LogContext -> LogLevel -> PP.Doc () -> IO ()
195195
printLog' logLevelSetting msgCtx msgLogLvl msg =
196196
when
197-
(logLevelSetting {ltLogTypes = mempty} >= msgLogLvl {ltLogTypes = mempty}
198-
&& not (null intersectLogTypes))
197+
( logLevelSetting {ltLogTypes = mempty} >= msgLogLvl {ltLogTypes = mempty}
198+
&& not (null intersectLogTypes)
199+
)
199200
$ putStrLn target
200201
where
201202
target =
202203
Render.renderString . layoutPretty defaultLayoutOptions $
203204
prettyLog msgCtx msgLogLvl msg
204205

205206
intersectLogTypes = ltLogTypes logLevelSetting `intersect` (ltLogTypes msgLogLvl <> [AnyLog])
206-
207207

208208
prettyLog :: LogContext -> LogLevel -> PP.Doc () -> PP.Doc ()
209209
prettyLog msgCtx msgLogLvl msg = pretty msgCtx <+> pretty msgLogLvl <+> msg

src/BotPlutusInterface/Types.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ module BotPlutusInterface.Types (
66
PABConfig (..),
77
CLILocation (..),
88
AppState (AppState),
9-
LogLevel (..),
109
LogContext (..),
10+
LogLevel (..),
11+
LogType (..),
1112
ContractEnvironment (..),
1213
Tip (Tip, epoch, hash, slot, block, era, syncProgress),
1314
ContractState (..),
@@ -261,11 +262,12 @@ instance Pretty LogType where
261262
pretty PABLog = "PABLog"
262263
pretty AnyLog = "Any"
263264

264-
data LogLevel = Error { ltLogTypes :: [LogType] }
265-
| Warn { ltLogTypes :: [LogType] }
266-
| Notice { ltLogTypes :: [LogType] }
267-
| Info { ltLogTypes :: [LogType] }
268-
| Debug { ltLogTypes :: [LogType] }
265+
data LogLevel
266+
= Error {ltLogTypes :: [LogType]}
267+
| Warn {ltLogTypes :: [LogType]}
268+
| Notice {ltLogTypes :: [LogType]}
269+
| Info {ltLogTypes :: [LogType]}
270+
| Debug {ltLogTypes :: [LogType]}
269271
deriving stock (Eq, Ord, Show)
270272

271273
instance Pretty LogLevel where

src/PlutusConfig/Cardano/Api/Shelley.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Config.Schema (
2020
naturalSpec,
2121
sectionsSpec,
2222
)
23+
import Control.Exception (IOException, catch)
2324
import Data.Aeson qualified as JSON
2425
import Data.ByteString.Lazy qualified as LazyByteString
2526
import Data.Default (def)
@@ -276,7 +277,7 @@ instance HasSpec ProtocolParameters where
276277
pure ProtocolParameters {..}
277278

278279
readProtocolParametersJSON :: FilePath -> IO (Either String ProtocolParameters)
279-
readProtocolParametersJSON fn = JSON.eitherDecode <$> LazyByteString.readFile fn
280+
readProtocolParametersJSON fn = (JSON.eitherDecode <$> LazyByteString.readFile fn) `catch` (\(e :: IOException) -> pure $ Left (show e))
280281

281282
writeProtocolParametersJSON :: FilePath -> ProtocolParameters -> IO ()
282283
writeProtocolParametersJSON fn params =

test/Spec/BotPlutusInterface/Balance.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ addUtxosForNativeTokens = do
8888

8989
case ebalancedTx of
9090
Left e -> assertFailure (Text.unpack e)
91-
Right balancedTx -> txInputs <$> balancedTx @?= Right (Set.fromList [txIn1, txIn2, txIn3, txIn4])
91+
Right balancedTx -> txInputs <$> balancedTx @?= Right (Set.fromList [txIn3, txIn4])
9292

9393
addUtxosForChange :: Assertion
9494
addUtxosForChange = do

test/Spec/BotPlutusInterface/Contract.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ import Plutus.Contract (
4545
)
4646
import PlutusTx qualified
4747
import PlutusTx.Builtins (fromBuiltin)
48+
import Pretty.Diff (
49+
MultilineContext (FullContext),
50+
Wrapping (Wrap),
51+
)
52+
import Pretty.Diff qualified as Diff
4853
import Spec.MockContract (
4954
MockContractState (..),
5055
addr1,
@@ -903,7 +908,14 @@ assertCommandHistory state =
903908
assertCommandEqual :: String -> Text -> Text -> Assertion
904909
assertCommandEqual err expected actual
905910
| commandEqual expected actual = pure ()
906-
| otherwise = assertFailure $ err ++ "\nExpected:\n" ++ show expected ++ "\nGot:\n" ++ show actual
911+
| otherwise = assertFailure $ err ++ "\n" ++ prettyPrintDiff expected actual
912+
913+
prettyPrintDiff :: Text -> Text -> String
914+
prettyPrintDiff expected actual =
915+
"\nExpected:\n"
916+
++ Text.unpack (Diff.above (Wrap 80) FullContext (Text.replace "\n" " " expected) actual)
917+
++ "\nGot:\n"
918+
++ Text.unpack (Diff.below (Wrap 80) FullContext (Text.replace "\n" " " expected) actual)
907919

908920
{- | Checks if a command matches an expected command pattern
909921
Where a command pattern may use new lines in place of spaces, and use the wildcard `?` to match up to the next space

0 commit comments

Comments
 (0)