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`) (#741)
  • Loading branch information
koslambrou authored Oct 7, 2022
1 parent 4c832ce commit 9be8ea1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 59 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
32 changes: 13 additions & 19 deletions plutus-ledger-constraints/src/Ledger/Constraints/OffChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ module Ledger.Constraints.OffChain(
, tx
, requiredSignatories
, utxoIndex
, validityTimeRange
, emptyUnbalancedTx
, adjustUnbalancedTx
, adjustTxOut
Expand Down Expand Up @@ -105,7 +104,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 All @@ -115,14 +115,13 @@ import Ledger.Typed.Scripts (Any, ConnectionError (UnknownRef), TypedValidator (
import Ledger.Validation (evaluateMinLovelaceOutput, fromPlutusTxOut)
import Plutus.Script.Utils.Scripts qualified as P
import Plutus.Script.Utils.V2.Typed.Scripts qualified as Typed
import Plutus.V1.Ledger.Api (Datum (Datum), DatumHash, POSIXTimeRange, Validator (getValidator), Value,
getMintingPolicy)
import Plutus.V1.Ledger.Api (Datum (Datum), DatumHash, Validator (getValidator), Value, getMintingPolicy)
import Plutus.V1.Ledger.Scripts (MintingPolicy (MintingPolicy), MintingPolicyHash (MintingPolicyHash), Script,
ScriptHash (ScriptHash), Validator (Validator), ValidatorHash (ValidatorHash))
import Plutus.V1.Ledger.Value qualified as Value
import Plutus.V2.Ledger.Tx qualified as PV2
import PlutusTx (FromData, ToData (toBuiltinData))
import PlutusTx.Lattice (BoundedMeetSemiLattice (top), JoinSemiLattice ((\/)), MeetSemiLattice ((/\)))
import PlutusTx.Lattice (JoinSemiLattice ((\/)), MeetSemiLattice ((/\)))
import PlutusTx.Numeric qualified as N
import Prettyprinter (Pretty (pretty), colon, hang, vsep, (<+>))

Expand Down Expand Up @@ -269,14 +268,6 @@ data UnbalancedTx
, unBalancedTxUtxoIndex :: Map TxOutRef TxOut
-- ^ Utxo lookups that are used for adding inputs to the 'UnbalancedTx'.
-- Simply refers to 'slTxOutputs' of 'ScriptLookups'.
, unBalancedTxValidityTimeRange :: POSIXTimeRange
-- ^ The reason this is a separate field instead of setting the
-- 'Plutus.txValidRange' of 'Plutus.Tx' is because the 'Plutus.txValidRange' is
-- specified as a 'SlotRange', but the user must specify the validity
-- range in terms of 'POSIXTimeRange' instead. Thus, before submitting
-- this transaction to the blockchain, we must convert this
-- 'POSIXTimeRange' to 'SlotRange' using a 'SlotConfig'. See
-- 'Plutus.Contract.Wallet.finalize'.
}
| UnbalancedCardanoTx
{ unBalancedCardanoBuildTx :: C.CardanoBuildTx
Expand All @@ -297,23 +288,21 @@ makeLensesFor
, ("unBalancedCardanoBuildTx", "cardanoTx")
, ("unBalancedTxRequiredSignatories", "requiredSignatories")
, ("unBalancedTxUtxoIndex", "utxoIndex")
, ("unBalancedTxValidityTimeRange", "validityTimeRange")
] ''UnbalancedTx

unBalancedTxTx :: UnbalancedTx -> Either C.CardanoBuildTx Tx.Tx
unBalancedTxTx UnbalancedEmulatorTx{unBalancedEmulatorTx} = Right unBalancedEmulatorTx
unBalancedTxTx UnbalancedCardanoTx{unBalancedCardanoBuildTx} = Left unBalancedCardanoBuildTx

emptyUnbalancedTx :: UnbalancedTx
emptyUnbalancedTx = UnbalancedEmulatorTx mempty mempty mempty top
emptyUnbalancedTx = UnbalancedEmulatorTx mempty mempty mempty

instance Pretty UnbalancedTx where
pretty (UnbalancedEmulatorTx utx rs utxo vr) =
pretty (UnbalancedEmulatorTx utx rs utxo) =
vsep
[ hang 2 $ vsep ["Tx:", pretty utx]
, hang 2 $ vsep $ "Requires signatures:" : (pretty <$> Set.toList rs)
, hang 2 $ vsep $ "Utxo index:" : (pretty <$> Map.toList utxo)
, hang 2 $ vsep ["Validity range:", pretty vr]
]
pretty (UnbalancedCardanoTx utx rs utxo) =
vsep
Expand Down Expand Up @@ -702,8 +691,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
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ module Ledger.Tx.Constraints.OffChain(
, txOuts
, P.requiredSignatories
, P.utxoIndex
, P.validityTimeRange
, emptyUnbalancedTx
, P.adjustUnbalancedTx
, MkTxError(..)
Expand Down
16 changes: 4 additions & 12 deletions plutus-use-cases/test/Spec/crowdfundingEmulatorTestOutput.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ 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 +91,12 @@ 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 +144,12 @@ 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 +202,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 All @@ -235,8 +229,6 @@ Slot 20: W[1]: Balancing an unbalanced transaction:
ScriptCredential: 8ff252c8c5423de7925e0c556871c90ed904ec3beb3dab519e9bfd9f (no staking credential)
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 9be8ea1

Please sign in to comment.