Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
PLT-990 Removed Plutus.Contract.Wallet.finalize as we instead set the…
Browse files Browse the repository at this point in the history
… validity range of a transaction directly in `plutus-ledger-constraints` (since we now have access to the `SlotConfig`)
  • Loading branch information
koslambrou committed Oct 7, 2022
1 parent 4c832ce commit 0ef7f55
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
27 changes: 6 additions & 21 deletions plutus-contract/src/Plutus/Contract/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ module Plutus.Contract.Wallet(
, ExportTxInput(..)
, ExportTxRedeemer(..)
, export
, finalize
) where

import Cardano.Api qualified as C
import Control.Applicative ((<|>))
import Control.Lens ((&), (.~))
import Control.Monad ((>=>))
import Control.Monad.Error.Lens (throwing)
import Control.Monad.Freer (Eff, Member)
Expand All @@ -50,10 +48,8 @@ import Ledger qualified (ScriptPurpose (..))
import Ledger qualified as P
import Ledger.Ada qualified as Ada
import Ledger.Constraints (mustPayToPubKey)
import Ledger.Constraints.OffChain (UnbalancedTx (UnbalancedCardanoTx, UnbalancedEmulatorTx, unBalancedTxRequiredSignatories, unBalancedTxUtxoIndex, unBalancedTxValidityTimeRange),
mkTx, unBalancedTxTx)
import Ledger.Constraints.OffChain qualified as U
import Ledger.TimeSlot (SlotConfig, posixTimeRangeToContainedSlotRange)
import Ledger.Constraints.OffChain (UnbalancedTx (unBalancedTxRequiredSignatories, unBalancedTxUtxoIndex), mkTx,
unBalancedTxTx)
import Ledger.Tx (CardanoTx, TxId (TxId), TxIn (..), TxOutRef, getCardanoTxInputs, txInRef)
import Ledger.Validation (CardanoLedgerError, fromPlutusIndex, makeTransactionBody)
import Ledger.Value (currencyMPSHash)
Expand Down Expand Up @@ -258,30 +254,19 @@ export
-> UnbalancedTx
-> Either CardanoLedgerError ExportTx
export params utx =
let utxFinal = finalize (P.pSlotConfig params) utx
requiredSigners = Set.toList (unBalancedTxRequiredSignatories utxFinal)
let requiredSigners = Set.toList (unBalancedTxRequiredSignatories utx)
fromCardanoTx ctx = do
utxo <- fromPlutusIndex $ P.UtxoIndex (unBalancedTxUtxoIndex utxFinal)
utxo <- fromPlutusIndex $ P.UtxoIndex (unBalancedTxUtxoIndex utx)
makeTransactionBody params utxo ctx
in ExportTx
<$> fmap (C.makeSignedTransaction [])
(either
fromCardanoTx
(first Right . CardanoAPI.toCardanoTxBody params requiredSigners)
(unBalancedTxTx utxFinal))
<*> first Right (mkInputs (P.pNetworkId params) (unBalancedTxUtxoIndex utxFinal))
(unBalancedTxTx utx))
<*> first Right (mkInputs (P.pNetworkId params) (unBalancedTxUtxoIndex utx))
<*> either (const $ Right []) (Right . mkRedeemers) (unBalancedTxTx utx)

-- | when we use UnbalancedEmulatorTx, finalize computes the final validityRange and set it into the Tx.
-- In the case of a UnbalancedCardanoTx, there's nothing to do here as the validityRange of the Tx is set when we process the
-- constraints.
finalize :: SlotConfig -> UnbalancedTx -> UnbalancedTx
finalize slotConfig utx@UnbalancedEmulatorTx{unBalancedTxValidityTimeRange} =
utx & U.tx
. P.validRange
.~ posixTimeRangeToContainedSlotRange slotConfig unBalancedTxValidityTimeRange
finalize _ utx@UnbalancedCardanoTx{} = utx

mkInputs :: C.NetworkId -> Map Plutus.TxOutRef P.TxOut -> Either CardanoAPI.ToCardanoError [ExportTxInput]
mkInputs networkId = traverse (uncurry (toExportTxInput networkId)) . Map.toList

Expand Down
10 changes: 4 additions & 6 deletions plutus-contract/src/Wallet/Emulator/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import Ledger.Constraints.OffChain qualified as U
import Ledger.Credential (Credential (PubKeyCredential, ScriptCredential))
import Ledger.Fee (estimateTransactionFee, makeAutoBalancedTransaction)
import Ledger.Index.Internal (UtxoIndex (UtxoIndex, getIndex))
import Ledger.Params (Params (Params, pNetworkId, pProtocolParams, pSlotConfig))
import Ledger.Params (Params (Params, pNetworkId, pProtocolParams))
import Ledger.Tx (CardanoTx, ChainIndexTxOut, SomeCardanoApiTx, Tx (txFee, txMint), TxOut (TxOut))
import Ledger.Tx qualified as Tx
import Ledger.Tx.CardanoAPI.Internal (makeTransactionBody, toCardanoTxOut, toCardanoTxOutDatum)
Expand All @@ -69,7 +69,6 @@ import Plutus.ChainIndex qualified as ChainIndex
import Plutus.ChainIndex.Api (UtxosResponse (page))
import Plutus.ChainIndex.Emulator (ChainIndexEmulatorState, ChainIndexQueryEffect)
import Plutus.Contract.Checkpoint (CheckpointLogMsg)
import Plutus.Contract.Wallet (finalize)
import Plutus.V1.Ledger.Api (PubKeyHash, TxOutRef, ValidatorHash, Value)
import PlutusTx.Prelude qualified as PlutusTx
import Prettyprinter (Pretty (pretty))
Expand Down Expand Up @@ -317,11 +316,10 @@ handleBalance ::
)
=> UnbalancedTx
-> Eff effs CardanoTx
handleBalance utx' = do
handleBalance utx = do
utxo <- get >>= ownOutputs
params@Params { pSlotConfig, pNetworkId } <- WAPI.getClientParams
let utx = finalize pSlotConfig utx'
requiredSigners = Set.toList (U.unBalancedTxRequiredSignatories utx)
params@Params { pNetworkId } <- WAPI.getClientParams
let requiredSigners = Set.toList (U.unBalancedTxRequiredSignatories utx)
eitherTx = U.unBalancedTxTx utx
plUtxo = traverse (Tx.toTxOut pNetworkId) utxo
mappedUtxo <- either (throwError . WAPI.ToCardanoError) pure plUtxo
Expand Down
12 changes: 9 additions & 3 deletions plutus-ledger-constraints/src/Ledger/Constraints/OffChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ import Ledger.Constraints.TxConstraints (ScriptInputConstraint (ScriptInputConst
import Ledger.Crypto (pubKeyHash)
import Ledger.Index (minAdaTxOut)
import Ledger.Orphans ()
import Ledger.Params (Params (pNetworkId))
import Ledger.Params (Params (pNetworkId, pSlotConfig))
import Ledger.TimeSlot (posixTimeRangeToContainedSlotRange)
import Ledger.Tx (ChainIndexTxOut (_ciTxOutReferenceScript), Language (PlutusV1, PlutusV2), ReferenceScript,
TxOut (TxOut), TxOutRef, Versioned (Versioned), txOutValue)
import Ledger.Tx qualified as Tx
Expand Down Expand Up @@ -702,8 +703,13 @@ processConstraint = \case
let dvHash = P.datumHash dv
unless (dvHash `elem` Map.keys datums)
(throwError $ DatumNotFoundInTx dvHash)
MustValidateIn timeRange ->
unbalancedTx . validityTimeRange %= (timeRange /\)
MustValidateIn timeRange -> do
slotRange <-
gets ( flip posixTimeRangeToContainedSlotRange timeRange
. pSlotConfig
. cpsParams
)
unbalancedTx . tx . Tx.validRange %= (slotRange /\)
MustBeSignedBy pk ->
unbalancedTx . requiredSignatories <>= Set.singleton pk
MustSpendAtLeast vl -> valueSpentInputs <>= required vl
Expand Down
16 changes: 8 additions & 8 deletions plutus-use-cases/test/Spec/crowdfundingEmulatorTestOutput.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ Slot 1: W[2]: Balancing an unbalanced transaction:
fee: Value (Map [])
mps:
signatures:
validity range: Interval {ivFrom = LowerBound NegInf True, ivTo = UpperBound PosInf True}
validity range: Interval {ivFrom = LowerBound NegInf True, ivTo = UpperBound (Finite (Slot {getSlot = 20})) False}
data:
( 77ab184b7537cd4b1dc3730f6a8a76a3d3aad1642fae9d769aa5dae40be38b51
, "\128\164\244[V\184\141\DC19\218#\188L<u\236m2\148<\b\DEL%\v\134\EM<\167" )}
Requires signatures:
Utxo index:
Validity range:
(-∞ , POSIXTime 1596059111000 ]
(-∞ , +∞)
Slot 1: W[2]: Finished balancing:
Tx a461d756dcb403971b1cad49aa06db9ded2ae9913c3a4f4ab9693814ed8f5f42:
{inputs:
Expand Down Expand Up @@ -93,14 +93,14 @@ Slot 1: W[3]: Balancing an unbalanced transaction:
fee: Value (Map [])
mps:
signatures:
validity range: Interval {ivFrom = LowerBound NegInf True, ivTo = UpperBound PosInf True}
validity range: Interval {ivFrom = LowerBound NegInf True, ivTo = UpperBound (Finite (Slot {getSlot = 20})) False}
data:
( 2cc2afd267462229babbc139837611310e4307bd6c7e870049c22fb02c2ad122
, ".\n\214\f2\a$\140\236\212}\189\227\215R\224\170\209A\214\184\248\SUB\194\198\236\162|" )}
Requires signatures:
Utxo index:
Validity range:
(-∞ , POSIXTime 1596059111000 ]
(-∞ , +∞)
Slot 1: W[3]: Finished balancing:
Tx 4f3549b97ce27589661068a58674751f01484a0035f20c7704728dc591fc7a54:
{inputs:
Expand Down Expand Up @@ -148,14 +148,14 @@ Slot 1: W[4]: Balancing an unbalanced transaction:
fee: Value (Map [])
mps:
signatures:
validity range: Interval {ivFrom = LowerBound NegInf True, ivTo = UpperBound PosInf True}
validity range: Interval {ivFrom = LowerBound NegInf True, ivTo = UpperBound (Finite (Slot {getSlot = 20})) False}
data:
( 63f4305deedb48449f218150b39eceb8d5951aa680e28a414024bc4c04758969
, "U}#\192\165\&3\180\210\149\172-\193Kx:~\252);\194>\222\136\166\254\253 =" )}
Requires signatures:
Utxo index:
Validity range:
(-∞ , POSIXTime 1596059111000 ]
(-∞ , +∞)
Slot 1: W[4]: Finished balancing:
Tx 1392dd20a08de6c8f41ed6a1418bd6092c75f95ecbf5e3507e2039b601fc00ba:
{inputs:
Expand Down Expand Up @@ -208,7 +208,7 @@ Slot 20: W[1]: Balancing an unbalanced transaction:
fee: Value (Map [])
mps:
signatures:
validity range: Interval {ivFrom = LowerBound NegInf True, ivTo = UpperBound PosInf True}
validity range: Interval {ivFrom = LowerBound (Finite (Slot {getSlot = 20})) True, ivTo = UpperBound (Finite (Slot {getSlot = 29})) True}
data:
( 2cc2afd267462229babbc139837611310e4307bd6c7e870049c22fb02c2ad122
, ".\n\214\f2\a$\140\236\212}\189\227\215R\224\170\209A\214\184\248\SUB\194\198\236\162|" )
Expand Down Expand Up @@ -236,7 +236,7 @@ Slot 20: W[1]: Balancing an unbalanced transaction:
with datum hash 77ab184b7537cd4b1dc3730f6a8a76a3d3aad1642fae9d769aa5dae40be38b51
and with no reference script )
Validity range:
[ POSIXTime 1596059111000 , POSIXTime 1596059120999 ]
(-∞ , +∞)
Slot 20: W[1]: Finished balancing:
Tx 0ef047e168d9ca4da973ed6010146ebc2bf02498a8e51f3058c958f8b1367ec3:
{inputs:
Expand Down

0 comments on commit 0ef7f55

Please sign in to comment.