Skip to content

Commit

Permalink
Merge pull request #723 from input-output-hk/plt-6905-min-utxo
Browse files Browse the repository at this point in the history
PLT-6905 Marlowe Runtime fails to adjust min-UTxO when starting from low value
  • Loading branch information
jhbertra authored Sep 28, 2023
2 parents b9e32a6 + 60e8d0a commit 31b6d35
Show file tree
Hide file tree
Showing 32 changed files with 909 additions and 882 deletions.
2 changes: 1 addition & 1 deletion marlowe-apps/scaling/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ runScenario
-> [InputContent]
-> App ContractId
runScenario backend config address key contract inputs =
runWithEvents backend config address key contract (pure . NormalInput <$> inputs) 1_500_000
runWithEvents backend config address key contract (pure . NormalInput <$> inputs) Nothing

currentTime :: (MonadIO m) => m POSIXTime
currentTime = POSIXTime . floor . (* 1_000) . nominalDiffTimeToSeconds <$> liftIO P.getPOSIXTime
Expand Down
2 changes: 1 addition & 1 deletion marlowe-apps/src/Language/Marlowe/Runtime/App/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ buildCreation
:: MarloweVersion v
-> Contract v
-> M.Map TokenName Address
-> Lovelace
-> Maybe Lovelace
-> MarloweTransactionMetadata
-> [Address]
-> Address
Expand Down
8 changes: 4 additions & 4 deletions marlowe-apps/src/Language/Marlowe/Runtime/App/Transact.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ run
-> C.SigningKey C.PaymentExtendedKey
-> Contract
-> [[Input]]
-> Lovelace
-> Maybe Lovelace
-> App ContractId
run = runWithEvents unitEventBackend

Expand All @@ -64,7 +64,7 @@ runWithEvents
-> C.SigningKey C.PaymentExtendedKey
-> Contract
-> [[Input]]
-> Lovelace
-> Maybe Lovelace
-> App ContractId
runWithEvents backend config address key contract inputs minUtxo =
do
Expand All @@ -78,7 +78,7 @@ create
-> Address
-> C.SigningKey C.PaymentExtendedKey
-> Contract
-> Lovelace
-> Maybe Lovelace
-> App ContractId
create = createWithEvents unitEventBackend

Expand All @@ -88,7 +88,7 @@ createWithEvents
-> Address
-> C.SigningKey C.PaymentExtendedKey
-> Contract
-> Lovelace
-> Maybe Lovelace
-> App ContractId
createWithEvents backend config address key contract minUtxo =
transactWithEvents' backend config key $
Expand Down
6 changes: 3 additions & 3 deletions marlowe-apps/src/Language/Marlowe/Runtime/App/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ data MarloweRequest v
| Create
{ reqContract :: Contract v
, reqRoles :: M.Map TokenName Address
, reqMinUtxo :: Lovelace
, reqMinUtxo :: Maybe Lovelace
, -- , reqStakeAddress :: Maybe StakeCredential
reqMetadata :: TransactionMetadata
, reqAddresses :: [Address]
Expand Down Expand Up @@ -244,7 +244,7 @@ instance A.FromJSON (MarloweRequest 'V1) where
"create" -> do
reqContract <- o A..: "contract"
reqRoles <- M.mapKeys fromString . M.map fromString <$> (o A..: "roles" :: A.Parser (M.Map String String))
reqMinUtxo <- Lovelace <$> o A..: "minUtxo"
reqMinUtxo <- fmap Lovelace <$> o A..:? "minUtxo"
reqMetadata <- metadataFromJSON =<< o A..: "metadata"
reqAddresses <- mapM addressFromJSON =<< o A..: "addresses"
reqChange <- addressFromJSON =<< o A..: "change"
Expand Down Expand Up @@ -328,7 +328,7 @@ instance A.ToJSON (MarloweRequest 'V1) where
[ "request" A..= ("create" :: String)
, "contract" A..= reqContract
, "roles" A..= M.mapKeys show reqRoles
, "minUtxo" A..= unLovelace reqMinUtxo
, "minUtxo" A..= fmap unLovelace reqMinUtxo
, "metadata" A..= reqMetadata
, "addresses" A..= fmap addressToJSON reqAddresses
, "change" A..= addressToJSON reqChange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ interpret ro@RuntimeCreateContract{..} = do
in (c, Just cnt)

result <- liftIO $ flip runMarloweT connector do
let minLovelace = ChainSync.Lovelace roMinLovelace
let minLovelace = ChainSync.Lovelace <$> roMinLovelace
walletAddresses =
WalletAddresses
{ changeAddress = changeAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ data RuntimeOperation
{ roContractNickname :: Maybe ContractNickname
, roSubmitter :: Maybe WalletNickname
-- ^ A wallet which gonna submit the initial transaction.
, roMinLovelace :: Word64
, roMinLovelace :: Maybe Word64
, roRoleCurrency :: Maybe CurrencyNickname
-- ^ If contract uses roles then currency is required.
, roContractSource :: Contract.Source
Expand Down
2 changes: 1 addition & 1 deletion marlowe-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ main = connectToMarloweRuntime "localhost" 3700 do
_ <- runMarloweQueryClient $ getContractHeaders Nothing

-- Interact with contracts
contractCreated <- createContract Nothing MarloweV1 wallet roleTokens mempty minAdaDeposit myContract
contractCreated <- createContract Nothing MarloweV1 wallet roleTokens mempty Nothing myContract
-- Sign with method of choice
submitAndWait $ signTx $ case contractCreated of ContractCreated{..} -> txBody

Expand Down
4 changes: 2 additions & 2 deletions marlowe-client/src/Control/Monad/Trans/Marlowe/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ createContract
-- ^ How to initialize role tokens
-> MarloweTransactionMetadata
-- ^ Optional metadata to attach to the transaction
-> Lovelace
-- ^ Min Lovelace which should be used for the contract output.
-> Maybe Lovelace
-- ^ Optional min Lovelace deposit which should be stored in the contract's accounts.
-> Either (Contract v) DatumHash
-- ^ The contract to run, or the hash of the contract to look up in the store.
-> m (Either CreateError (ContractCreated v))
Expand Down
19 changes: 11 additions & 8 deletions marlowe-integration-tests/test/Language/Marlowe/Runtime/CliSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Data.Foldable (for_)
import Data.Function (on)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (maybeToList)
import qualified Data.Maybe as Maybe
import qualified Data.Set as Set
import Data.String (fromString)
Expand Down Expand Up @@ -117,10 +118,12 @@ serializeAddress = Text.unpack . Maybe.fromJust . ChainSync.Api.toBech32

toCliArgs :: MarloweTxCommand Void err result -> [String]
toCliArgs = \case
Create _ MarloweV1 WalletAddresses{changeAddress, extraAddresses} _ _ (ChainSync.Api.Lovelace minUTXO) _ ->
Create _ MarloweV1 WalletAddresses{changeAddress, extraAddresses} _ _ minAdaDeposit _ ->
["create", "--change-address", serializeAddress changeAddress]
<> do address <- Set.toList extraAddresses; ["--address", serializeAddress address]
<> ["--min-utxo", show minUTXO]
<> do
minAda <- maybeToList minAdaDeposit
["--min-utxo", show minAda]
ApplyInputs MarloweV1 WalletAddresses{changeAddress, extraAddresses} contractId _metadata _ _ inputs ->
let tokenNotAda :: V1.Token -> Maybe (PlutusLedgerApi.V2.CurrencySymbol, PlutusLedgerApi.V2.TokenName)
tokenNotAda = \case V1.Token "" "" -> Nothing; V1.Token a b -> Just (a, b)
Expand Down Expand Up @@ -267,7 +270,7 @@ createSpec = describe "create" $
addresses
Runtime.Transaction.Api.RoleTokensNone
md
(ChainSync.Api.Lovelace 2_000_000)
Nothing
(Left contract)

expectSameResultFromCLIAndJobClient "create-tx-body.json" extraCliArgs extractCreateTxBody creationCommand
Expand Down Expand Up @@ -301,7 +304,7 @@ depositSpec = describe "deposit" $
pure ("Party A", (changeAddress $ addresses partyAWallet, Nothing))
)
(standardMetadata tags)
2_000_000
Nothing
(Left contract)

_ <- Runtime.Integration.Common.submit partyAWallet era txBody
Expand Down Expand Up @@ -356,7 +359,7 @@ chooseSpec = describe "choose" $
pure ("Party A", (changeAddress $ addresses partyAWallet, Nothing))
)
(standardMetadata tags)
2_000_000
Nothing
(Left contract)

_ <- Runtime.Integration.Common.submit partyAWallet era txBody
Expand Down Expand Up @@ -408,7 +411,7 @@ notifySpec = describe "notify" $
pure ("Party A", (changeAddress $ addresses partyAWallet, Nothing))
)
(standardMetadata tags)
2_000_000
Nothing
(Left contract)

_ <- Runtime.Integration.Common.submit partyAWallet era txBody
Expand Down Expand Up @@ -483,7 +486,7 @@ applySpec = describe "apply" $
pure ("Party A", (changeAddress $ addresses partyAWallet, Nothing))
)
(standardMetadata tags)
2_000_000
Nothing
(Left contract)

_ <- Runtime.Integration.Common.submit partyAWallet era txBody
Expand Down Expand Up @@ -542,7 +545,7 @@ withdrawSpec = describe "withdraw" $
pure ("Party A", (changeAddress $ addresses partyAWallet, Nothing))
)
(standardMetadata tags)
2_000_000
Nothing
(Left contract)

_ <- Runtime.Integration.Common.submit partyAWallet era0 txBody
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ closedSpec = parallel $ describe "Closed contract" $ aroundAll setup do
(addresses wallet)
RoleTokensNone
emptyMarloweTransactionMetadata
2_000_000
Nothing
(Left Close)
_ <- submit wallet era0 createBody
applyInputs MarloweV1 (addresses wallet) contractId emptyMarloweTransactionMetadata []
Expand Down Expand Up @@ -136,7 +136,7 @@ closeSpec = parallel $ describe "Close contract" $ aroundAll setup do
(addresses wallet)
RoleTokensNone
emptyMarloweTransactionMetadata
2_000_000
Nothing
(Left Close)
>>= expectRight "Failed to create contract"
>>= \created@(ContractCreated era0 ContractCreatedInEra{txBody = createBody, contractId}) -> do
Expand Down Expand Up @@ -281,7 +281,7 @@ paySpec = parallel $ describe "Pay contracts" $ aroundAll setup do
(addresses wallet1)
(mkRoleTokens [("Role", wallet2)])
emptyMarloweTransactionMetadata
2_000_000
(Just 2_000_000)
(Left $ mkPay (Account $ Role "Role") ada (Constant 2_000_000) Close)
submitCreate wallet1 payRoleAccountCreated
payAddressAccountCreated <-
Expand All @@ -292,7 +292,7 @@ paySpec = parallel $ describe "Pay contracts" $ aroundAll setup do
(addresses wallet1)
RoleTokensNone
emptyMarloweTransactionMetadata
2_000_000
(Just 2_000_000)
(Left $ mkPay (Account $ walletParty wallet2) ada (Constant 2_000_000) Close)
submitCreate wallet1 payAddressAccountCreated
payRolePartyCreated <-
Expand All @@ -303,7 +303,7 @@ paySpec = parallel $ describe "Pay contracts" $ aroundAll setup do
(addresses wallet1)
(mkRoleTokens [("Role", wallet2)])
emptyMarloweTransactionMetadata
2_000_000
(Just 2_000_000)
(Left $ mkPay (Party $ Role "Role") ada (Constant 2_000_000) Close)
submitCreate wallet1 payRolePartyCreated
payAddressPartyCreated <-
Expand All @@ -314,7 +314,7 @@ paySpec = parallel $ describe "Pay contracts" $ aroundAll setup do
(addresses wallet1)
RoleTokensNone
emptyMarloweTransactionMetadata
2_000_000
(Just 2_000_000)
(Left $ mkPay (Party $ walletParty wallet2) ada (Constant 2_000_000) Close)
submitCreate wallet1 payAddressPartyCreated
payDepth1Created <-
Expand All @@ -325,7 +325,7 @@ paySpec = parallel $ describe "Pay contracts" $ aroundAll setup do
(addresses wallet1)
(mkRoleTokens [("Role", wallet2)])
emptyMarloweTransactionMetadata
2_000_000
(Just 2_000_000)
( Left $
mkPay (Account $ Role "Role") ada (Constant 2_000_000) $
When
Expand All @@ -343,7 +343,7 @@ paySpec = parallel $ describe "Pay contracts" $ aroundAll setup do
(addresses wallet1)
(mkRoleTokens [("Role", wallet2)])
emptyMarloweTransactionMetadata
10_000_000
(Just 10_000_000)
( Left $
mkPay (Account $ Role "Role") ada (Constant 2_000_000) $
When
Expand All @@ -366,7 +366,7 @@ paySpec = parallel $ describe "Pay contracts" $ aroundAll setup do
(addresses wallet1)
(mkRoleTokens [("Role", wallet2)])
emptyMarloweTransactionMetadata
10_000_000
(Just 10_000_000)
( Left $
mkPay (Party $ Role "Role") ada (Constant 2_000_000) $
When
Expand Down Expand Up @@ -511,7 +511,7 @@ whenTimeoutSpec = parallel $ describe "Timed out contracts" $ aroundAll setup do
(addresses wallet)
RoleTokensNone
emptyMarloweTransactionMetadata
2_000_000
Nothing
(Left $ When [Case (Notify TrueObs) Close] (utcTimeToPOSIXTime startTime) Close)
submitCreate wallet depth1Created
depth2InnerTimeoutCreated <-
Expand All @@ -522,7 +522,7 @@ whenTimeoutSpec = parallel $ describe "Timed out contracts" $ aroundAll setup do
(addresses wallet)
RoleTokensNone
emptyMarloweTransactionMetadata
2_000_000
Nothing
( Left $
When [Case (Notify TrueObs) Close] (utcTimeToPOSIXTime startTime) $
When [] (utcTimeToPOSIXTime startTime) Close
Expand All @@ -536,7 +536,7 @@ whenTimeoutSpec = parallel $ describe "Timed out contracts" $ aroundAll setup do
(addresses wallet)
RoleTokensNone
emptyMarloweTransactionMetadata
2_000_000
Nothing
( Left $
When [Case (Notify TrueObs) Close] (utcTimeToPOSIXTime startTime) $
When [] (utcTimeToPOSIXTime $ addUTCTime (secondsToNominalDiffTime 200) startTime) Close
Expand Down Expand Up @@ -632,7 +632,7 @@ whenEmptySpec = parallel $ describe "Empty When contracts" $ aroundAll setup do
(addresses wallet)
RoleTokensNone
emptyMarloweTransactionMetadata
2_000_000
Nothing
(Left $ When [] (utcTimeToPOSIXTime $ addUTCTime (secondsToNominalDiffTime 200) startTime) Close)
submitCreate wallet $ ContractCreated era ContractCreatedInEra{..}
runtime <- ask
Expand Down Expand Up @@ -900,7 +900,7 @@ whenNonEmptySpec = parallel $ describe "Non-Empty When contracts" $ aroundAll se
(addresses wallet1)
(mkRoleTokens [("Role1", wallet1), ("Role2", wallet2)])
emptyMarloweTransactionMetadata
2_000_000
Nothing
(Left $ When cases (utcTimeToPOSIXTime $ addUTCTime (secondsToNominalDiffTime 100) startTime) Close)
submitCreate wallet1 contract
runtime <- ask
Expand Down Expand Up @@ -956,7 +956,7 @@ merkleizedSpec = parallel $ describe "Merkleized contracts" $ aroundAll setup do
(addresses wallet)
RoleTokensNone
emptyMarloweTransactionMetadata
2_000_000
Nothing
( Left $
When
[MerkleizedCase (Notify TrueObs) hash]
Expand Down Expand Up @@ -1021,7 +1021,7 @@ multiInputsSpec = parallel $ describe "Multi inputs" $ aroundAll setup do
(addresses wallet)
(mkRoleTokens [("role", wallet)])
emptyMarloweTransactionMetadata
2_000_000
Nothing
( Left $
When
[Case action1 $ When [Case action2 Close] timeout Close, Case action2 $ When [Case action1 Close] timeout Close]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ spec = describe "Basic scenarios" do
(wallet.addresses)
(RoleTokensUsePolicy "")
emptyMarloweTransactionMetadata
2_000_000
Nothing
(Left contract)
_ <- submit wallet era0 created.txBody
InputsApplied era1 applied <-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ contractCreatedToCreateStep (ContractCreated _ ContractCreatedInEra{..}) =
{ createOutput =
TransactionScriptOutput
{ address = marloweScriptAddress
, assets = Assets 2_000_000 mempty
, assets
, utxo = unContractId contractId
, datum
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ addNFTMetadata ContractCreatedInEra{..} = \case

minLovelaceSpec :: MinLovelaceCase -> Maybe (SpecWith (TestData, ContractCreated v))
minLovelaceSpec = \case
MinLovelaceOmitted -> Just $ pure ()
MinLovelaceSufficient -> Just $ pure ()
MinLovelaceInsufficient -> Nothing

Expand Down Expand Up @@ -424,10 +425,11 @@ mkExtraMetadata (ExtraMetadataCase extraRandomMetadata extraMarloweMetadata extr
, guard extraNFTMetadata $> (721, MetadataNumber 721)
]

mkMinLovelace :: MinLovelaceCase -> Lovelace
mkMinLovelace :: MinLovelaceCase -> Maybe Lovelace
mkMinLovelace = \case
MinLovelaceSufficient -> 5_000_000
MinLovelaceInsufficient -> 500_000
MinLovelaceOmitted -> Nothing
MinLovelaceSufficient -> Just 5_000_000
MinLovelaceInsufficient -> Just 500_000

mkContract :: RoleTokenCase -> V1.Contract
mkContract = \case
Expand Down Expand Up @@ -503,6 +505,7 @@ data ExtraMetadataCase = ExtraMetadataCase Bool Bool Bool
deriving (Show, Eq)

data MinLovelaceCase
= MinLovelaceSufficient
= MinLovelaceOmitted
| MinLovelaceSufficient
| MinLovelaceInsufficient
deriving (Show, Eq, Enum, Bounded)
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ createStandardContractWithTags tags partyAWallet partyBWallet = do
}
}
)
2_000_000
Nothing
(Right contractHash)
contractCreated@(ContractCreated era0 ContractCreatedInEra{contractId, txBody = createTxBody}) <-
expectRight "failed to create standard contract" result
Expand Down
Loading

0 comments on commit 31b6d35

Please sign in to comment.