Skip to content

Commit 834c249

Browse files
Remove chainindex timeout, order ScriptContext
1 parent 91a0465 commit 834c249

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/BotPlutusInterface/CardanoCLI.hs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import Data.Either (fromRight)
4343
import Data.Either.Combinators (mapLeft, maybeToRight)
4444
import Data.Hex (hex)
4545
import Data.Kind (Type)
46-
import Data.List (nub, sort)
46+
import Data.List (nub, sort, sortOn)
4747
import Data.Map (Map)
4848
import Data.Map qualified as Map
4949
import Data.Maybe (fromMaybe)
@@ -97,7 +97,9 @@ import Plutus.V1.Ledger.Api (
9797
TxInfo (TxInfo),
9898
)
9999
import Plutus.V1.Ledger.Api qualified as Plutus
100+
import PlutusTx.AssocMap qualified as AMap
100101
import PlutusTx.Builtins (fromBuiltin)
102+
import PlutusTx.Prelude qualified as PPrelude
101103
import Prelude
102104

103105
-- | Getting information of the latest block
@@ -192,8 +194,11 @@ queryTxOuts ::
192194
queryTxOuts txIds = do
193195
res <- queryChainIndex @w $ TxsFromTxIds txIds
194196
return $ case res of
195-
TxIdsResponse chainTxs -> Right $ foldMap (fmap fst . txOutRefMap) chainTxs
197+
TxIdsResponse chainTxs -> Right $ foldMap (fmap (sortTxOut . fst) . txOutRefMap) chainTxs
196198
_ -> Left "Wrong PAB response"
199+
where
200+
sortTxOut :: TxOut -> TxOut
201+
sortTxOut txOut = txOut {txOutValue = sortValue $ txOutValue txOut}
197202

198203
-- There is no match txOutRefs request, and we don't want a separate PAB query per input.
199204
-- So, for efficiency, we're going to query the transactions for all inputs combined,
@@ -220,23 +225,30 @@ buildTxInfo ::
220225
Tx ->
221226
Eff effs (Either Text TxInfo)
222227
buildTxInfo pabConf tx = do
223-
let txOutRefs = txInRef <$> Set.toList (txInputs tx)
228+
let txOutRefs = txInRef <$> Set.toList (txInputs tx) -- This will already be in order, for Sets listify acsending
224229
eTxInInfos <- getTxInInfos @w txOutRefs
225230
return $
226231
(`second` eTxInInfos) $ \txInInfos ->
227232
TxInfo
228233
{ txInfoInputs = txInInfos
229234
, txInfoOutputs = txOutputs tx
230-
, txInfoFee = txFee tx
231-
, txInfoMint = txMint tx
235+
, txInfoFee = sortValue $ txFee tx
236+
, txInfoMint = sortValue $ txMint tx
232237
, txInfoDCert = [] -- We don't support staking or stake redeeming at this time
233238
, txInfoWdrl = []
234239
, txInfoValidRange = slotRangeToPOSIXTimeRange (pcSlotConfig pabConf) $ txValidRange tx
235-
, txInfoSignatories = Ledger.pubKeyHash <$> Map.keys (txSignatures tx)
236-
, txInfoData = Map.toList $ txData tx
240+
, txInfoSignatories = sort $ Ledger.pubKeyHash <$> Map.keys (txSignatures tx)
241+
, txInfoData = sortOn fst $ Map.toList $ txData tx
237242
, txInfoId = Ledger.txId tx
238243
}
239244

245+
-- | Sorts the internal maps
246+
sortValue :: Value -> Value
247+
sortValue (Value.Value m) = Value.Value $ sortMap $ sortMap PPrelude.<$> m
248+
where
249+
sortMap :: forall (k :: Type) (v :: Type). Ord k => AMap.Map k v -> AMap.Map k v
250+
sortMap = AMap.fromList . sortOn fst . AMap.toList
251+
240252
-- | Build a tx body and write it to disk
241253
buildTx ::
242254
forall (w :: Type) (effs :: [Type -> Type]).
@@ -476,19 +488,19 @@ unsafeSerialiseAddress network address =
476488
Right a -> a
477489
Left _ -> error "Couldn't create address"
478490

479-
-- calculateExBudget :: Script -> [BuiltinData] -> Either Text ExBudget
480-
-- calculateExBudget script builtinData = do
481-
-- -- TODO, pull this from the protocol, they're the same for now but may not always be
482-
-- modelParams <- maybeToRight "Cost model params invalid." Plutus.defaultCostModelParams
483-
-- let serialisedScript = ShortByteString.toShort $ LazyByteString.toStrict $ Codec.serialise script
484-
-- pData = map Plutus.builtinDataToData builtinData
485-
-- mapLeft showText $
486-
-- snd $
487-
-- Plutus.evaluateScriptCounting Plutus.Verbose modelParams serialisedScript pData
488-
489491
calculateExBudget :: Script -> [BuiltinData] -> Either Text ExBudget
490492
calculateExBudget script builtinData = do
491-
mapLeft showText $ fst <$> Scripts.evaluateScript (Scripts.applyArguments script $ Plutus.builtinDataToData <$> builtinData)
493+
-- TODO, pull this from the protocol, they're the same for now but may not always be
494+
modelParams <- maybeToRight "Cost model params invalid." Plutus.defaultCostModelParams
495+
let serialisedScript = ShortByteString.toShort $ LazyByteString.toStrict $ Codec.serialise script
496+
pData = map Plutus.builtinDataToData builtinData
497+
mapLeft showText $
498+
snd $
499+
Plutus.evaluateScriptCounting Plutus.Verbose modelParams serialisedScript pData
500+
501+
-- calculateExBudget :: Script -> [BuiltinData] -> Either Text ExBudget
502+
-- calculateExBudget script builtinData = do
503+
-- mapLeft showText $ fst <$> Scripts.evaluateScript (Scripts.applyArguments script $ Plutus.builtinDataToData <$> builtinData)
492504

493505
exBudgetToCliArg :: ExBudget -> Text
494506
exBudgetToCliArg (ExBudget (ExCPU steps) (ExMemory memory)) =

src/BotPlutusInterface/ChainIndex.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module BotPlutusInterface.ChainIndex (handleChainIndexReq) where
44

55
import BotPlutusInterface.Types (PABConfig)
66
import Data.Kind (Type)
7-
import Network.HTTP.Client (defaultManagerSettings, newManager)
7+
import Network.HTTP.Client (ManagerSettings (managerResponseTimeout), defaultManagerSettings, newManager, responseTimeoutNone)
88
import Network.HTTP.Types (Status (statusCode))
99
import Plutus.ChainIndex.Api (
1010
TxoAtAddressRequest (TxoAtAddressRequest),
@@ -65,7 +65,7 @@ handleChainIndexReq pabConf = \case
6565

6666
chainIndexQuery' :: forall (a :: Type). PABConfig -> ClientM a -> IO (Either ClientError a)
6767
chainIndexQuery' pabConf endpoint = do
68-
manager' <- newManager defaultManagerSettings
68+
manager' <- newManager defaultManagerSettings {managerResponseTimeout = responseTimeoutNone}
6969
runClientM endpoint $ mkClientEnv manager' pabConf.pcChainIndexUrl
7070

7171
chainIndexQueryMany :: forall (a :: Type). PABConfig -> ClientM a -> IO a

0 commit comments

Comments
 (0)