Skip to content

Commit

Permalink
Fix everything up to plutus-use-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mueller committed Apr 16, 2021
1 parent fed67c3 commit 68339b8
Show file tree
Hide file tree
Showing 48 changed files with 20,702 additions and 19,067 deletions.
8 changes: 4 additions & 4 deletions doc/plutus/tutorials/BasicApps.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ PlutusTx.makeLift ''SplitData

-- BLOCK2

validateSplit :: SplitData -> () -> ValidatorCtx -> Bool
validateSplit SplitData{recipient1, recipient2, amount} _ ValidatorCtx{valCtxTxInfo} =
validateSplit :: SplitData -> () -> ScriptContext -> Bool
validateSplit SplitData{recipient1, recipient2, amount} _ ScriptContext{scriptContextTxInfo} =
let half = Ada.divide amount 2 in
Ada.fromValue (valuePaidTo valCtxTxInfo recipient1) >= half &&
Ada.fromValue (valuePaidTo valCtxTxInfo recipient2) >= (amount - half)
Ada.fromValue (valuePaidTo scriptContextTxInfo recipient1) >= half &&
Ada.fromValue (valuePaidTo scriptContextTxInfo recipient2) >= (amount - half)

-- BLOCK3

Expand Down
4 changes: 2 additions & 2 deletions doc/plutus/tutorials/BasicValidators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ validatePayment _ _ ctx = check $ case fromData ctx of
Just valCtx ->
-- The 'TxInfo' in the validation context is the representation of the
-- transaction being validated
let txinfo = valCtxTxInfo valCtx
let txinfo = scriptContextTxInfo valCtx
-- 'pubKeyOutputsAt' collects the 'Value' at all outputs which pay to
-- the given public key hash
values = pubKeyOutputsAt myKeyHash txinfo
Expand All @@ -77,6 +77,6 @@ validatePayment _ _ ctx = check $ case fromData ctx of
validateDate' :: Data -> Data -> Data -> ()
validateDate' = wrapValidator validateDateTyped
where
validateDateTyped :: EndDate -> Date -> ValidatorCtx -> Bool
validateDateTyped :: EndDate -> Date -> ScriptContext -> Bool
validateDateTyped endDate date _ = beforeEnd date endDate
-- BLOCK6
8 changes: 4 additions & 4 deletions marlowe/src/Language/Marlowe/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import GHC.Generics (Generic)
import Language.Marlowe.Semantics hiding (Contract)
import qualified Language.Marlowe.Semantics as Marlowe
import Language.Marlowe.Util (extractContractRoles)
import Ledger (Address (..), CurrencySymbol, Datum (..), PubKeyHash, Slot (..),
TokenName, TxOut (..), TxOutTx (..), TxOutType (..), ValidatorCtx (..),
import Ledger (Address (..), CurrencySymbol, Datum (..), PubKeyHash, ScriptContext (..),
Slot (..), TokenName, TxOut (..), TxOutTx (..), TxOutType (..),
ValidatorHash, mkValidatorScript, pubKeyHash, txOutDatum, txOutValue,
txOutputs, validatorHash, valueSpent)
import Ledger.Ada (adaSymbol, adaValueOf)
Expand Down Expand Up @@ -347,9 +347,9 @@ rolePayoutScript symbol = mkValidatorScript ($$(PlutusTx.compile [|| wrapped ||]


{-# INLINABLE rolePayoutValidator #-}
rolePayoutValidator :: CurrencySymbol -> TokenName -> () -> ValidatorCtx -> Bool
rolePayoutValidator :: CurrencySymbol -> TokenName -> () -> ScriptContext -> Bool
rolePayoutValidator currency role _ ctx =
Val.valueOf (valueSpent (valCtxTxInfo ctx)) currency role P.> 0
Val.valueOf (valueSpent (scriptContextTxInfo ctx)) currency role P.> 0


mkRolePayoutValidatorHash :: CurrencySymbol -> ValidatorHash
Expand Down
8 changes: 6 additions & 2 deletions playground-common/src/PSGenerator/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import Language.PureScript.Bridge.PSTypes (psArray, psInt, psNu
import Language.PureScript.Bridge.TypeParameters (A)
import Ledger (Address, Datum, DatumHash, MonetaryPolicy, PubKey,
PubKeyHash, Redeemer, Signature, Tx, TxId, TxIn, TxInType,
TxOut, TxOutRef, TxOutTx, TxOutType, UtxoIndex, Validator)
TxOut, TxOutRef, TxOutTx, UtxoIndex, Validator)
import Ledger.Ada (Ada)
import Ledger.Constraints.OffChain (MkTxError)
import Ledger.Credential (Credential, StakingCredential)
import Ledger.DCert (DCert)
import Ledger.Index (ScriptType, ScriptValidationEvent, ValidationError)
import Ledger.Interval (Extended, Interval, LowerBound, UpperBound)
import Ledger.Scripts (ScriptError)
Expand Down Expand Up @@ -257,7 +259,9 @@ ledgerTypes =
, (order <*> (genericShow <*> mkSumType)) (Proxy @DatumHash)
, (order <*> (genericShow <*> mkSumType)) (Proxy @PubKey)
, (order <*> (genericShow <*> mkSumType)) (Proxy @PubKeyHash)
, (order <*> (genericShow <*> mkSumType)) (Proxy @TxOutType)
, (order <*> (genericShow <*> mkSumType)) (Proxy @Credential)
, (order <*> (genericShow <*> mkSumType)) (Proxy @StakingCredential)
, (order <*> (genericShow <*> mkSumType)) (Proxy @DCert)
, (equal <*> (genericShow <*> mkSumType)) (Proxy @MkTxError)
, (equal <*> (genericShow <*> mkSumType)) (Proxy @ContractError)
, (equal <*> (genericShow <*> mkSumType)) (Proxy @ConnectionError)
Expand Down
6 changes: 3 additions & 3 deletions plutus-contract/src/Plutus/Contract/Effects/UtxoAt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import qualified Data.Map as Map
import Data.Row
import Data.Text.Prettyprint.Doc
import GHC.Generics (Generic)
import Ledger (Address, Slot, TxOut (..), TxOutTx (..))
import Ledger (Address, Slot, TxOutTx (..))
import Ledger.AddressMap (UtxoMap)

import Plutus.Contract.Effects.AwaitSlot (HasAwaitSlot, awaitSlot)
Expand All @@ -43,8 +43,8 @@ data UtxoAtAddress =
instance Pretty UtxoAtAddress where
pretty UtxoAtAddress{address, utxo} =
let
prettyTxOutPair (txoutref, TxOutTx _ TxOut{txOutValue, txOutType}) =
pretty txoutref <> colon <+> pretty txOutType <+> viaShow txOutValue
prettyTxOutPair (txoutref, TxOutTx{txOutTxOut}) =
pretty txoutref <> colon <+> pretty txOutTxOut
utxos = vsep $ fmap prettyTxOutPair (Map.toList utxo)
in vsep ["Utxo at" <+> pretty address <+> "=", indent 2 utxos]

Expand Down
13 changes: 7 additions & 6 deletions plutus-contract/src/Plutus/Contract/StateMachine/OnChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import qualified PlutusTx as PlutusTx
import PlutusTx.Prelude hiding (check)

import Ledger (Address, Value)
import Ledger.Contexts (TxInInfo (..), ValidatorCtx (..), findOwnInput)
import Ledger.Contexts (ScriptContext (..), TxInInfo (..), findOwnInput)
import Ledger.Tx (TxOut (..))
import Ledger.Typed.Scripts
import Ledger.Value (isZero)
import qualified Prelude as Haskell
Expand All @@ -58,11 +59,11 @@ data StateMachine s i = StateMachine {
-- checks on the pending transaction that aren't covered by the
-- constraints. 'smCheck' is always run in addition to checking the
-- constraints, so the default implementation always returns true.
smCheck :: s -> i -> ValidatorCtx -> Bool
smCheck :: s -> i -> ScriptContext -> Bool
}

-- | A state machine that does not perform any additional checks on the
-- 'ValidatorCtx' (beyond enforcing the constraints)
-- 'ScriptContext' (beyond enforcing the constraints)
mkStateMachine
:: (State s -> i -> Maybe (TxConstraints Void Void, State s))
-> (s -> Bool)
Expand Down Expand Up @@ -92,20 +93,20 @@ machineAddress = scriptAddress . validatorInstance
-- | Turn a state machine into a validator script.
mkValidator :: forall s i. (PlutusTx.IsData s) => StateMachine s i -> ValidatorType (StateMachine s i)
mkValidator (StateMachine step isFinal check) currentState input ptx =
let vl = txInInfoValue (findOwnInput ptx)
let vl = maybe (error ()) (txOutValue . txInInfoResolved) (findOwnInput ptx)
checkOk = traceIfFalse "State transition invalid - checks failed" (check currentState input ptx)
oldState = State{stateData=currentState, stateValue=vl}
stateAndOutputsOk = case step oldState input of
Just (newConstraints, State{stateData=newData, stateValue=newValue})
| isFinal newData ->
traceIfFalse "Non-zero value allocated in final state" (isZero newValue)
&& traceIfFalse "State transition invalid - constraints not satisfied by ValidatorCtx" (checkValidatorCtx newConstraints ptx)
&& traceIfFalse "State transition invalid - constraints not satisfied by ScriptContext" (checkScriptContext newConstraints ptx)
| otherwise ->
let txc =
newConstraints
{ txOwnOutputs=
[ OutputConstraint{ocDatum=newData, ocValue= newValue} ]
}
in traceIfFalse "State transition invalid - constraints not satisfied by ValidatorCtx" (checkValidatorCtx @_ @s txc ptx)
in traceIfFalse "State transition invalid - constraints not satisfied by ScriptContext" (checkScriptContext @_ @s txc ptx)
Nothing -> trace "State transition invalid - input is not a valid transition at the current state" False
in checkOk && stateAndOutputsOk
10 changes: 6 additions & 4 deletions plutus-contract/src/Wallet/Emulator/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import GHC.Generics (Generic)
import Ledger
import qualified Ledger.Ada as Ada
import qualified Ledger.AddressMap as AM
import Ledger.Credential (Credential (..))
import qualified Ledger.Crypto as Crypto
import qualified Ledger.Value as Value
import Plutus.Contract.Checkpoint (CheckpointLogMsg)
Expand Down Expand Up @@ -327,10 +328,11 @@ balances :: ChainState -> WalletSet -> Map.Map Entity Value
balances state wallets = foldl' f Map.empty . getIndex . _index $ state
where
toEntity :: Address -> Entity
toEntity (PubKeyAddress h) = case Map.lookup h ws of
Nothing -> PubKeyHashEntity h
Just w -> WalletEntity w
toEntity (ScriptAddress h) = ScriptEntity h
toEntity a = case addressCredential a of
PubKeyCredential h -> case Map.lookup h ws of
Nothing -> PubKeyHashEntity h
Just w -> WalletEntity w
ScriptCredential h -> ScriptEntity h

ws :: Map.Map PubKeyHash Wallet
ws = walletPubKeyHashes wallets
Expand Down
13 changes: 7 additions & 6 deletions plutus-contract/src/Wallet/Graph.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}
Expand Down Expand Up @@ -27,6 +28,7 @@ import GHC.Generics (Generic)
import qualified Ledger.Ada as Ada
import Ledger.Address
import Ledger.Blockchain
import Ledger.Credential (Credential (..))
import Ledger.Crypto
import Ledger.Tx
import Ledger.TxId
Expand All @@ -43,13 +45,12 @@ data UtxOwner

-- | Given a set of known public keys, compute the owner of a given transaction output.
owner :: Set.Set PubKey -> TxOut -> UtxOwner
owner keys TxOut {..} =
owner keys TxOut {txOutAddress=Address{addressCredential}} =
let hashMap = foldMap (\pk -> Map.singleton (pubKeyHash pk) pk) keys
in case (txOutType, txOutAddress) of
(PayToScript _, ScriptAddress _) -> ScriptOwner
(PayToPubKey, PubKeyAddress pkh)
| Just pk <- Map.lookup pkh hashMap -> PubKeyOwner pk
_ -> OtherOwner
in case addressCredential of
ScriptCredential{} -> ScriptOwner
PubKeyCredential pkh | Just pk <- Map.lookup pkh hashMap -> PubKeyOwner pk
_ -> OtherOwner

-- | A wrapper around the first 8 digits of a 'TxId'.
newtype TxRef =
Expand Down
9 changes: 5 additions & 4 deletions plutus-contract/src/Wallet/Rollup/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Data.Map (Map)
import Data.Text.Prettyprint.Doc (Pretty, pretty, viaShow)
import GHC.Generics
import Ledger
import Ledger.Credential (Credential (..))

data TxKey =
TxKey
Expand Down Expand Up @@ -61,10 +62,10 @@ data BeneficialOwner
deriving anyclass (FromJSON, ToJSON, FromJSONKey, ToJSONKey)

toBeneficialOwner :: TxOut -> BeneficialOwner
toBeneficialOwner TxOut {txOutAddress} =
case txOutAddress of
PubKeyAddress pkh -> OwnedByPubKey pkh
ScriptAddress vh -> OwnedByScript vh
toBeneficialOwner TxOut {txOutAddress=Address{addressCredential}} =
case addressCredential of
PubKeyCredential pkh -> OwnedByPubKey pkh
ScriptCredential vh -> OwnedByScript vh

data AnnotatedTx =
AnnotatedTx
Expand Down
2 changes: 1 addition & 1 deletion plutus-contract/test/Spec/Emulator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ invalidScript = property $ do
where
failValidator :: Validator
failValidator = mkValidatorScript $$(PlutusTx.compile [|| wrapValidator validator ||])
validator :: () -> () -> ValidatorCtx -> Bool
validator :: () -> () -> ScriptContext -> Bool
validator _ _ _ = PlutusTx.traceError "I always fail everything"


Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Slot 00001: TxnValidate 4febabe136e65d5fb4683b378570e6f43f92056e489281ad2e6302a9fa127874
Slot 00001: TxnValidate af5e6d25b5ecb26185289a03d50786b7ac4425b21849143ed7e18bcd70dc4db8
Slot 00001: SlotAdd Slot 1
Slot 00001: W1: TxSubmit: 1ddbdaa7c4750c268b3437171d8f0151f2bcb9e375e0de1b6a27744823ee4daf
Slot 00002: TxnValidate 1ddbdaa7c4750c268b3437171d8f0151f2bcb9e375e0de1b6a27744823ee4daf
Slot 00001: W1: TxSubmit: b88eef8f6e6ecc1005a17b2f80fa8173e108457bb861ebf287a882f2517374a1
Slot 00002: TxnValidate b88eef8f6e6ecc1005a17b2f80fa8173e108457bb861ebf287a882f2517374a1
Slot 00002: SlotAdd Slot 2
Slot 00002: W2: TxSubmit: 69ea3fa973772088abeb2fe7d64ab63d3ef556f23292051428847fa2d046f78d
Slot 00003: TxnValidate 69ea3fa973772088abeb2fe7d64ab63d3ef556f23292051428847fa2d046f78d
Slot 00002: W2: TxSubmit: 3c2b8dffba39e446e7f0920c9fa0684addedd68a09f5a538b0617606386b01e1
Slot 00003: TxnValidate 3c2b8dffba39e446e7f0920c9fa0684addedd68a09f5a538b0617606386b01e1
Slot 00003: SlotAdd Slot 3
Slot 00003: W3: TxSubmit: dff447b2f637c58a54a587296f58cede2dc20ed4bcb117faa624776ccf5e4e1f
Slot 00004: TxnValidate dff447b2f637c58a54a587296f58cede2dc20ed4bcb117faa624776ccf5e4e1f
Slot 00003: W3: TxSubmit: 9b9bbd27ffb2c5ad5c742f2829de9db2b5c025dc3918652450a82c06519bc28c
Slot 00004: TxnValidate 9b9bbd27ffb2c5ad5c742f2829de9db2b5c025dc3918652450a82c06519bc28c
Slot 00004: SlotAdd Slot 4
Slot 00005: SlotAdd Slot 5
Final balances
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Slot 00001: TxnValidate 4febabe136e65d5fb4683b378570e6f43f92056e489281ad2e6302a9fa127874
Slot 00001: TxnValidate af5e6d25b5ecb26185289a03d50786b7ac4425b21849143ed7e18bcd70dc4db8
Slot 00001: SlotAdd Slot 1
Slot 00001: W1: TxSubmit: 227175d9c54eab2d6168c85b9f0158f25df4d692477612d778aa26fcd95576dd
Slot 00002: TxnValidate 227175d9c54eab2d6168c85b9f0158f25df4d692477612d778aa26fcd95576dd
Slot 00001: W1: TxSubmit: a7f674beb0b2b728d80cdf60ad2945556d0a91add3a0209ac60bc411871da826
Slot 00002: TxnValidate a7f674beb0b2b728d80cdf60ad2945556d0a91add3a0209ac60bc411871da826
Slot 00002: SlotAdd Slot 2
Slot 00002: W2: TxSubmit: c8e9420b110b561f4740497150ccc00a81f406a821088bc8c366a2507241a51b
Slot 00003: TxnValidate c8e9420b110b561f4740497150ccc00a81f406a821088bc8c366a2507241a51b
Slot 00002: W2: TxSubmit: 4fa93f6f251ef383a974ba52920cf85850c28ff33a9550fee88d92b3f8a9d81a
Slot 00003: TxnValidate 4fa93f6f251ef383a974ba52920cf85850c28ff33a9550fee88d92b3f8a9d81a
Slot 00003: SlotAdd Slot 3
Slot 00003: W3: TxSubmit: d833a08936c48a0807278d6e301cef8a99e9c4bc5c60b06bb9d59d998ff2ab31
Slot 00004: TxnValidate d833a08936c48a0807278d6e301cef8a99e9c4bc5c60b06bb9d59d998ff2ab31
Slot 00003: W3: TxSubmit: 087caad8d00f3b1201171cffcda723fc5079380828f39c63f3dc3e5a48b71752
Slot 00004: TxnValidate 087caad8d00f3b1201171cffcda723fc5079380828f39c63f3dc3e5a48b71752
Slot 00004: SlotAdd Slot 4
Slot 00004: W1: TxSubmit: f4dafd03d99c47c3cbdef59b76fe190f0e23efc8fe9016c61e55d2e688edae4a
Slot 00005: TxnValidate f4dafd03d99c47c3cbdef59b76fe190f0e23efc8fe9016c61e55d2e688edae4a
Slot 00004: W1: TxSubmit: 9933fe032fd1ede9744b248db221103a29e83f24ebb8274e8df19ff70b2ae541
Slot 00005: TxnValidate 9933fe032fd1ede9744b248db221103a29e83f24ebb8274e8df19ff70b2ae541
Slot 00005: SlotAdd Slot 5
Slot 00006: SlotAdd Slot 6
Final balances
Expand Down
2 changes: 1 addition & 1 deletion plutus-contract/test/Spec/golden/traceOutput - wait1.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Slot 00001: TxnValidate 4febabe136e65d5fb4683b378570e6f43f92056e489281ad2e6302a9fa127874
Slot 00001: TxnValidate af5e6d25b5ecb26185289a03d50786b7ac4425b21849143ed7e18bcd70dc4db8
Slot 00001: SlotAdd Slot 1
Slot 00002: SlotAdd Slot 2
Slot 00003: SlotAdd Slot 3
Expand Down
2 changes: 2 additions & 0 deletions plutus-ledger/plutus-ledger.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ library
Plutus.V1.Ledger.Ada as Ledger.Ada,
Plutus.V1.Ledger.Bytes as Ledger.Bytes,
Plutus.V1.Ledger.Contexts as Ledger.Contexts,
Plutus.V1.Ledger.Credential as Ledger.Credential,
Plutus.V1.Ledger.DCert as Ledger.DCert,
Plutus.V1.Ledger.Crypto as Ledger.Crypto,
Plutus.V1.Ledger.Interval as Ledger.Interval,
Plutus.V1.Ledger.Scripts as Ledger.Scripts,
Expand Down
6 changes: 3 additions & 3 deletions plutus-ledger/src/Ledger/Constraints.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Ledger.Constraints(
, modifiesUtxoSet
, isSatisfiable
-- * Checking
, checkValidatorCtx
, checkScriptContext
-- * Generating transactions
, ScriptLookups(..)
, MkTxError(..)
Expand All @@ -40,7 +40,7 @@ module Ledger.Constraints(
import Ledger.Constraints.OffChain (MkTxError (..), ScriptLookups (..), SomeLookupsAndConstraints (..),
UnbalancedTx, mkSomeTx, mkTx, monetaryPolicy, otherData, otherScript,
ownPubKeyHash, scriptInstanceLookups, unspentOutputs)
import Ledger.Constraints.OnChain (checkValidatorCtx)
import Ledger.Constraints.OnChain (checkScriptContext)
import Ledger.Constraints.TxConstraints

-- $constraints
Expand All @@ -50,4 +50,4 @@ import Ledger.Constraints.TxConstraints
-- 'TxConstraints' value it can be used both to generate a transaction that
-- satisfies the constraints (off-chain, using 'mkTx') and to check whether
-- a given pending transaction meets the constraints (on-chain, using
-- 'checkValidatorCtx').
-- 'checkScriptContext').

0 comments on commit 68339b8

Please sign in to comment.