Skip to content

Commit c720514

Browse files
committed
budget estimation refactoring and cleanup
1 parent 73f8070 commit c720514

File tree

9 files changed

+238
-165
lines changed

9 files changed

+238
-165
lines changed

examples/ex-units/app/Main.hs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,31 @@ import Data.Text qualified as Text
1414
import Data.UUID.V4 qualified as UUID
1515
import Ledger (PubKeyHash)
1616
import LockSpend (lockThenSpend)
17+
1718
-- import LockSpendSingle (lockThenSpendSingle)
1819
import Plutus.PAB.Core.ContractInstance.STM (Activity (Active))
1920
import Servant.Client (BaseUrl (BaseUrl), Scheme (Http))
2021
import System.Directory (listDirectory)
21-
import System.Environment (getArgs, setEnv, getEnv)
22+
import System.Environment (getArgs, getEnv, setEnv)
2223
import System.FilePath ((</>))
2324
import Wallet.Types (ContractInstanceId (ContractInstanceId))
2425
import Prelude
2526

27+
{- | For running fast live tests using Plutip's local cluster,
28+
needed only for debugging period
29+
-}
2630
main :: IO ()
2731
main = do
28-
-- TODO: export PATH=$PATH:/home/mike/dev/mlabs/local-cluster/node-bins
29-
let clusterDir = "/home/mike/dev/mlabs/local-cluster/data"
30-
[sockPath] <- getArgs
32+
[sockPath, clusterDir, cliDir] <- getArgs
3133
setEnv "CARDANO_NODE_SOCKET_PATH" sockPath
32-
getEnv "PATH" >>= \p -> setEnv "PATH" (p ++ ":/home/mike/dev/mlabs/local-cluster/node-bins")
34+
getEnv "PATH" >>= \p -> setEnv "PATH" (p ++ ":" ++ cliDir)
3335
let nodeInfo = BPI.NodeInfo Mainnet sockPath
3436

3537
cEnv <- mkContractEnv nodeInfo clusterDir
36-
res <- BPI.runContract cEnv lockThenSpend
37-
-- res <- BPI.runContract cEnv lockThenSpendSingle
38+
res <- BPI.runContract cEnv lockThenSpend
3839
putStrLn $ case res of
39-
Right _ -> "=== OK ==="
40-
Left e -> "=== FAILED ===\n" ++ show e
40+
Right _ -> "=== OK ==="
41+
Left e -> "=== FAILED ===\n" ++ show e
4142

4243
mkContractEnv :: Monoid w => BPI.NodeInfo -> FilePath -> IO (ContractEnvironment w)
4344
mkContractEnv nodeInfo clusterDir = do
@@ -47,36 +48,36 @@ mkContractEnv nodeInfo clusterDir = do
4748
pkhs <- getPkhs clusterDir
4849
return $
4950
ContractEnvironment
50-
{ cePABConfig = mkPabConf pparams (Text.pack paramsFile) clusterDir (head pkhs),
51-
ceContractState = contractState,
52-
ceContractInstanceId = contractInstanceID
51+
{ cePABConfig = mkPabConf pparams (Text.pack paramsFile) clusterDir (head pkhs)
52+
, ceContractState = contractState
53+
, ceContractInstanceId = contractInstanceID
5354
}
5455

5556
getPparams :: BPI.NodeInfo -> FilePath -> IO (ProtocolParameters, FilePath)
5657
getPparams nodeInfo clusterDir = do
57-
pparams :: ProtocolParameters <- getOrFailM $ BPI.protocolParams nodeInfo
58+
pparams :: ProtocolParameters <- getOrFailM $ BPI.queryProtocolParams nodeInfo
5859
let ppath = clusterDir </> "pparams.json"
5960
JSON.encodeFile ppath pparams
6061
return (pparams, ppath)
6162

6263
mkPabConf :: ProtocolParameters -> Text -> FilePath -> PubKeyHash -> PABConfig
6364
mkPabConf pparams pparamsFile clusterDir ownPkh =
6465
PABConfig
65-
{ pcCliLocation = Local,
66-
pcNetwork = Mainnet,
67-
pcChainIndexUrl = BaseUrl Http "localhost" 9083 "",
68-
pcPort = 9080,
69-
pcProtocolParams = pparams,
70-
pcTipPollingInterval = 1_000_000,
71-
pcSlotConfig = def,
72-
pcOwnPubKeyHash = ownPkh,
73-
pcScriptFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/scripts",
74-
pcSigningKeyFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/signing-keys",
75-
pcTxFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/txs",
76-
pcDryRun = False,
77-
pcLogLevel = Error,
78-
pcProtocolParamsFile = pparamsFile,
79-
pcEnableTxEndpoint = True
66+
{ pcCliLocation = Local
67+
, pcNetwork = Mainnet
68+
, pcChainIndexUrl = BaseUrl Http "localhost" 9083 ""
69+
, pcPort = 9080
70+
, pcProtocolParams = pparams
71+
, pcTipPollingInterval = 1_000_000
72+
, pcSlotConfig = def
73+
, pcOwnPubKeyHash = ownPkh
74+
, pcScriptFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/scripts"
75+
, pcSigningKeyFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/signing-keys"
76+
, pcTxFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/txs"
77+
, pcDryRun = False
78+
, pcLogLevel = Error
79+
, pcProtocolParamsFile = pparamsFile
80+
, pcEnableTxEndpoint = True
8081
}
8182

8283
getPkhs :: FilePath -> IO [PubKeyHash]

examples/ex-units/src/LockSpendSingle.hs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,35 @@ import Prelude
44

55
import Data.Map qualified as Map
66
import Data.Text (Text)
7-
import Ledger
8-
( CardanoTx,
9-
Address,
10-
ScriptContext,
11-
TxId,
12-
Validator,
13-
getCardanoTxId,
14-
scriptAddress,
15-
unitDatum,
16-
unitRedeemer,
17-
validatorHash )
7+
import Ledger (
8+
Address,
9+
CardanoTx,
10+
ScriptContext,
11+
TxId,
12+
Validator,
13+
getCardanoTxId,
14+
scriptAddress,
15+
unitDatum,
16+
unitRedeemer,
17+
validatorHash,
18+
)
1819
import Ledger.Constraints qualified as Constraints
19-
import Plutus.Contract
20-
( Contract, submitTx, awaitTxConfirmed, submitTxConstraintsWith )
20+
import Ledger.Typed.Scripts.Validators qualified as Validators
21+
import Plutus.Contract (
22+
Contract,
23+
awaitTxConfirmed,
24+
submitTx,
25+
submitTxConstraintsWith,
26+
)
2127
import Plutus.Contract qualified as Contract
2228
import Plutus.PAB.Effects.Contract.Builtin (EmptySchema)
23-
import Ledger.Typed.Scripts.Validators qualified as Validators
2429
import Plutus.V1.Ledger.Ada qualified as Value
2530
import PlutusTx qualified
2631

2732
lockThenSpendSingle :: Contract () EmptySchema Text (TxId, CardanoTx)
2833
lockThenSpendSingle =
2934
lockAtScript >> Contract.waitNSlots 1 >> spendFromScript
3035

31-
3236
lockAtScript :: Contract () EmptySchema Text (TxId, CardanoTx)
3337
lockAtScript = do
3438
let constr =
@@ -56,7 +60,6 @@ spendFromScript = do
5660
awaitTxConfirmed $ getCardanoTxId tx
5761
pure (getCardanoTxId tx, tx)
5862

59-
6063
-- Always true Script and spending contract
6164

6265
{-# INLINEABLE mkValidator #-}

src/BotPlutusInterface/BodyBuilder.hs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
2+
3+
{- | Module provides the way of building ".raw" transactions with execution budget
4+
estimated with `Cardano.Api` tools.
5+
-}
26
module BotPlutusInterface.BodyBuilder (buildRaw) where
37

48
import BotPlutusInterface.CardanoCLI qualified as CardanoCLI
59
import BotPlutusInterface.Effects (PABEffect, estimateBudget)
6-
import BotPlutusInterface.Files
7-
( DummyPrivKey,
8-
)
9-
import BotPlutusInterface.Estimate (TxFile (Raw), getMaxBudgets)
10-
import BotPlutusInterface.Types (PABConfig)
10+
import BotPlutusInterface.Files (
11+
DummyPrivKey,
12+
)
13+
import BotPlutusInterface.Types (PABConfig, TxFile (Raw))
1114
import Control.Monad.Freer (Eff, Member)
1215
import Control.Monad.Trans.Either (firstEitherT, newEitherT, runEitherT)
1316
import Data.Kind (Type)
@@ -17,8 +20,13 @@ import Data.Text qualified as Text
1720
import Ledger (ExBudget, Tx)
1821
import Ledger.Crypto (PubKeyHash)
1922
import Prelude
23+
import BotPlutusInterface.Estimate (getMaxBudgets)
2024

21-
25+
{- | Build and save raw transaction (transaction body) with estimated execution budgets using `CardanoCLI`.
26+
It builds first transaction body with 0 budget for all spending inputs and minting policies,
27+
then uses body of this transaction to estimate execution budget
28+
and build final body with budget set.
29+
-}
2230
buildRaw ::
2331
forall (w :: Type) (effs :: [Type -> Type]).
2432
Member (PABEffect w) effs =>
@@ -27,16 +35,16 @@ buildRaw ::
2735
Tx ->
2836
Eff effs (Either Text ExBudget)
2937
buildRaw pabConf privKeys tx = runEitherT $ do
30-
buildDraftTxBody
31-
>>= estimateBudgetByDraftBody
38+
buildDraftTxBody
39+
>>= estimateBudgetByDraftBody
3240
>>= buildBodyUsingEstimatedBudget
3341
where
3442
buildDraftTxBody = newEitherT $ CardanoCLI.buildDraftTx @w pabConf privKeys tx
3543

36-
estimateBudgetByDraftBody path =
44+
estimateBudgetByDraftBody path =
3745
firstEitherT toText . newEitherT $ estimateBudget @w (Raw path)
3846

39-
buildBodyUsingEstimatedBudget exBudget =
47+
buildBodyUsingEstimatedBudget exBudget =
4048
newEitherT $
4149
CardanoCLI.buildTx @w
4250
pabConf

src/BotPlutusInterface/Effects.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ module BotPlutusInterface.Effects (
2323
) where
2424

2525
import BotPlutusInterface.ChainIndex (handleChainIndexReq)
26-
import BotPlutusInterface.Estimate (TxFile)
2726
import BotPlutusInterface.Estimate qualified as Estimate
2827
import BotPlutusInterface.Types (
28+
BudgetEstimationError,
2929
CLILocation (..),
3030
ContractEnvironment,
3131
ContractState (ContractState),
3232
LogLevel (..),
33+
TxBudget,
34+
TxFile,
3335
)
3436
import Cardano.Api (AsType, FileError, HasTextEnvelope, TextEnvelopeDescr, TextEnvelopeError)
3537
import Cardano.Api qualified
@@ -85,7 +87,7 @@ data PABEffect (w :: Type) (r :: Type) where
8587
ListDirectory :: FilePath -> PABEffect w [FilePath]
8688
UploadDir :: Text -> PABEffect w ()
8789
QueryChainIndex :: ChainIndexQuery -> PABEffect w ChainIndexResponse
88-
EstimateBudget :: TxFile -> PABEffect w (Either Estimate.BudgetEstimationError Estimate.TxBudgets)
90+
EstimateBudget :: TxFile -> PABEffect w (Either BudgetEstimationError TxBudget)
8991

9092
handlePABEffect ::
9193
forall (w :: Type) (effs :: [Type -> Type]).
@@ -129,7 +131,7 @@ handlePABEffect contractEnv =
129131
QueryChainIndex query ->
130132
handleChainIndexReq contractEnv.cePABConfig query
131133
EstimateBudget txPath ->
132-
Estimate.budgetByFile txPath
134+
Estimate.estimateBudget contractEnv.cePABConfig txPath
133135
)
134136

135137
printLog' :: LogLevel -> LogLevel -> String -> IO ()
@@ -180,7 +182,7 @@ estimateBudget ::
180182
forall (w :: Type) (effs :: [Type -> Type]).
181183
Member (PABEffect w) effs =>
182184
TxFile ->
183-
Eff effs (Either Estimate.BudgetEstimationError Estimate.TxBudgets)
185+
Eff effs (Either BudgetEstimationError TxBudget)
184186
estimateBudget = send @(PABEffect w) . EstimateBudget
185187

186188
createDirectoryIfMissing ::

0 commit comments

Comments
 (0)