Skip to content

Commit

Permalink
Merge pull request #622 from input-output-hk/PLT-5817
Browse files Browse the repository at this point in the history
PLT-3564 PLT-6070 PLT-6071 PLT-6072 PLT-6073 PLT-5817 Safety checks for REST and CLI
  • Loading branch information
bwbush authored Jun 24, 2023
2 parents 8e15508 + 2ed9725 commit 9e412eb
Show file tree
Hide file tree
Showing 26 changed files with 830 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Control.Monad.Trans.Except (ExceptT (ExceptT), throwE)
import Data.Aeson (FromJSON, toJSON)
import qualified Data.Aeson as A
import Data.Bifunctor (bimap, first)
import qualified Data.ByteString.Char8 as BS8
import Data.List.NonEmpty (NonEmpty (..))
import Data.Map (Map)
import qualified Data.Map as Map
Expand All @@ -19,6 +20,7 @@ import qualified Data.Yaml as Yaml
import Data.Yaml.Aeson (decodeFileEither)
import GHC.Generics (Generic)
import Language.Marlowe (POSIXTime (POSIXTime))
import Language.Marlowe.Analysis.Safety.Types (SafetyError)
import Language.Marlowe.Runtime.CLI.Command.Tx (SigningMethod (Manual), TxCommand (..), txCommandParser)
import Language.Marlowe.Runtime.CLI.Monad (CLI, runCLIExceptT)
import Language.Marlowe.Runtime.CLI.Option (keyValueOption, marloweVersionParser, parseAddress)
Expand Down Expand Up @@ -52,6 +54,7 @@ import Language.Marlowe.Runtime.Transaction.Api (
)
import Options.Applicative
import Options.Applicative.NonEmpty (some1)
import System.IO (hPutStrLn, stderr)
import Text.Read (readMaybe)

data CreateCommand = CreateCommand
Expand Down Expand Up @@ -215,7 +218,13 @@ runCreateCommand TxCommand{walletAddresses, signingMethod, tagsFile, metadataFil
case Map.toList configMap of
[] -> throwE $ RolesConfigFileDecodingError "Empty role token config"
(x : xs) -> pure $ RoleTokensMint $ mkMint $ fmap (\RoleConfig{..} -> (address, Just metadata)) <$> x :| xs
ContractId contractId <- run MarloweV1 minting'
(ContractId contractId, safetyErrors) <- run MarloweV1 minting'
liftIO $
if null safetyErrors
then hPutStrLn stderr "Safety analysis found no errors in the contract."
else do
hPutStrLn stderr "Safety analysis found the following errors in the contract:"
BS8.hPutStrLn stderr $ Yaml.encode safetyErrors
liftIO . print $ A.encode (A.object [("contractId", toJSON . renderTxOutRef $ contractId)])
where
readContract :: MarloweVersion v -> ExceptT (CreateCommandError v) CLI (Either (Contract v) DatumHash)
Expand Down Expand Up @@ -243,14 +252,14 @@ runCreateCommand TxCommand{walletAddresses, signingMethod, tagsFile, metadataFil
pure $ MarloweMetadata tags Nothing
Nothing -> pure Nothing

run :: MarloweVersion v -> RoleTokensConfig -> ExceptT (CreateCommandError v) CLI ContractId
run :: MarloweVersion v -> RoleTokensConfig -> ExceptT (CreateCommandError v) CLI (ContractId, [SafetyError])
run version rolesDistribution = do
contract <- readContract version
metadata <- MarloweTransactionMetadata <$> readTags <*> readMetadata
ContractCreated{contractId, txBody} <-
ContractCreated{contractId, txBody, safetyErrors} <-
ExceptT $
first CreateFailed <$> createContract Nothing version walletAddresses rolesDistribution metadata minUTxO contract
case signingMethod of
Manual outputFile -> do
ExceptT $ liftIO $ first TransactionFileWriteFailed <$> C.writeFileTextEnvelope outputFile Nothing txBody
pure contractId
pure (contractId, safetyErrors)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Added

- Results of contract's safety analysis reported in create response.
1 change: 1 addition & 0 deletions marlowe-runtime-web/marlowe-runtime-web.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ library
, network-uri >=2.6 && <3
, openapi3 >=3.2 && <4
, parsec ^>=3.1.14
, plutus-ledger-api
, servant ^>=0.19
, servant-client ^>=0.19
, servant-client-core ^>=0.19
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import qualified Data.Map as Map
import Data.Maybe (fromMaybe)
import qualified Data.Set as Set
import Data.Text (Text)
import Language.Marlowe.Analysis.Safety.Types (SafetyError)
import Language.Marlowe.Protocol.Query.Types (ContractFilter (..), Page (..))
import Language.Marlowe.Runtime.ChainSync.Api (Lovelace (..))
import Language.Marlowe.Runtime.Core.Api (
Expand Down Expand Up @@ -58,7 +59,7 @@ postCreateTxBody
-> Address
-> Maybe (CommaList Address)
-> Maybe (CommaList TxOutRef)
-> ServerM (ContractId, TxBody BabbageEra)
-> ServerM (ContractId, TxBody BabbageEra, [SafetyError])
postCreateTxBody PostContractsRequest{..} changeAddressDTO mAddresses mCollateralUtxos = do
SomeMarloweVersion v@MarloweV1 <- fromDTOThrow (badRequest' "Unsupported Marlowe version") version
changeAddress <- fromDTOThrow (badRequest' "Invalid change address value") changeAddressDTO
Expand All @@ -75,7 +76,7 @@ postCreateTxBody PostContractsRequest{..} changeAddressDTO mAddresses mCollatera
if Map.null tags then Nothing else Just (tags, Nothing)
createContract Nothing v WalletAddresses{..} roles' MarloweTransactionMetadata{..} (Lovelace minUTxODeposit) contract >>= \case
Left err -> throwDTOError err
Right ContractCreated{contractId, txBody} -> pure (contractId, txBody)
Right ContractCreated{contractId, txBody, safetyErrors} -> pure (contractId, txBody, safetyErrors)

postCreateTxBodyResponse
:: PostContractsRequest
Expand All @@ -84,9 +85,9 @@ postCreateTxBodyResponse
-> Maybe (CommaList TxOutRef)
-> ServerM (PostContractsResponse CardanoTxBody)
postCreateTxBodyResponse req changeAddressDTO mAddresses mCollateralUtxos = do
res <- postCreateTxBody req changeAddressDTO mAddresses mCollateralUtxos
let (contractId', txBody') = toDTO res
let body = CreateTxEnvelope contractId' txBody'
(contractId, txBody, safetyErrors) <- postCreateTxBody req changeAddressDTO mAddresses mCollateralUtxos
let (contractId', txBody') = toDTO (contractId, txBody)
let body = CreateTxEnvelope contractId' txBody' safetyErrors
pure $ IncludeLink (Proxy @"contract") body

postCreateTxResponse
Expand All @@ -96,10 +97,10 @@ postCreateTxResponse
-> Maybe (CommaList TxOutRef)
-> ServerM (PostContractsResponse CardanoTx)
postCreateTxResponse req changeAddressDTO mAddresses mCollateralUtxos = do
(contractId, txBody) <- postCreateTxBody req changeAddressDTO mAddresses mCollateralUtxos
(contractId, txBody, safetyErrors) <- postCreateTxBody req changeAddressDTO mAddresses mCollateralUtxos
let tx = makeSignedTransaction [] txBody
let (contractId', tx') = toDTO (contractId, tx)
let body = CreateTxEnvelope contractId' tx'
let body = CreateTxEnvelope contractId' tx' safetyErrors
pure $ IncludeLink (Proxy @"contract") body

get
Expand Down
Loading

0 comments on commit 9e412eb

Please sign in to comment.