{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TypeFamilies #-}
module Test.Cardano.Ledger.Mary.Imp.UtxoSpec (spec) where
import Cardano.Ledger.Allegra.Scripts
import Cardano.Ledger.BaseTypes (Mismatch (..))
import Cardano.Ledger.Mary.Core
import Cardano.Ledger.Mary.Value
import Cardano.Ledger.Shelley.Rules (ShelleyUtxoPredFailure (..))
import Cardano.Ledger.Shelley.Scripts (
pattern RequireSignature,
)
import qualified Data.Map.Strict as Map
import Data.Sequence.Strict (StrictSeq (..))
import Lens.Micro
import Test.Cardano.Ledger.Core.Utils (txInAt)
import Test.Cardano.Ledger.Imp.Common
import Test.Cardano.Ledger.Mary.ImpTest
mintBasicToken ::
(HasCallStack, AllegraEraScript era, MaryEraImp era) => ImpTestM era (Tx era)
mintBasicToken :: forall era.
(HasCallStack, AllegraEraScript era, MaryEraImp era) =>
ImpTestM era (Tx era)
mintBasicToken = do
Addr
addr <- forall s (m :: * -> *) g.
(HasKeyPairs s, MonadState s m, HasStatefulGen g m) =>
m Addr
freshKeyAddr_
KeyHash 'Witness
keyHash <- forall s (m :: * -> *) g (r :: KeyRole).
(HasKeyPairs s, MonadState s m, HasStatefulGen g m) =>
m (KeyHash r)
freshKeyHash
ScriptHash
scriptHash <- forall era.
EraScript era =>
NativeScript era -> ImpTestM era ScriptHash
impAddNativeScript forall a b. (a -> b) -> a -> b
$ forall era.
ShelleyEraScript era =>
KeyHash 'Witness -> NativeScript era
RequireSignature KeyHash 'Witness
keyHash
Positive Integer
amount <- forall a (m :: * -> *). (Arbitrary a, MonadGen m) => m a
arbitrary
let txAsset :: MultiAsset
txAsset = Map PolicyID (Map AssetName Integer) -> MultiAsset
MultiAsset forall a b. (a -> b) -> a -> b
$ forall k a. k -> a -> Map k a
Map.singleton (ScriptHash -> PolicyID
PolicyID ScriptHash
scriptHash) forall a b. (a -> b) -> a -> b
$ forall k a. k -> a -> Map k a
Map.singleton (ShortByteString -> AssetName
AssetName ShortByteString
"testAsset") Integer
amount
txValue :: MaryValue
txValue :: MaryValue
txValue = Coin -> MultiAsset -> MaryValue
MaryValue forall a. Monoid a => a
mempty MultiAsset
txAsset
txBody :: TxBody era
txBody =
forall era. EraTxBody era => TxBody era
mkBasicTxBody
forall a b. a -> (a -> b) -> b
& forall era.
EraTxBody era =>
Lens' (TxBody era) (StrictSeq (TxOut era))
outputsTxBodyL forall s t a b. ASetter s t a b -> b -> s -> t
.~ [forall era.
(EraTxOut era, HasCallStack) =>
Addr -> Value era -> TxOut era
mkBasicTxOut Addr
addr MaryValue
txValue]
forall a b. a -> (a -> b) -> b
& forall era. MaryEraTxBody era => Lens' (TxBody era) MultiAsset
mintTxBodyL forall s t a b. ASetter s t a b -> b -> s -> t
.~ MultiAsset
txAsset
forall era.
(HasCallStack, ShelleyEraImp era) =>
Tx era -> ImpTestM era (Tx era)
submitTx forall a b. (a -> b) -> a -> b
$ forall era. EraTx era => TxBody era -> Tx era
mkBasicTx TxBody era
txBody
spec ::
( HasCallStack
, MaryEraImp era
, AllegraEraScript era
, InjectRuleFailure "LEDGER" ShelleyUtxoPredFailure era
) =>
SpecWith (ImpInit (LedgerSpec era))
spec :: forall era.
(HasCallStack, MaryEraImp era, AllegraEraScript era,
InjectRuleFailure "LEDGER" ShelleyUtxoPredFailure era) =>
SpecWith (ImpInit (LedgerSpec era))
spec = forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"UTXO" forall a b. (a -> b) -> a -> b
$ do
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"Mint a Token" forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Functor f => f a -> f ()
void forall era.
(HasCallStack, AllegraEraScript era, MaryEraImp era) =>
ImpTestM era (Tx era)
mintBasicToken
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"ShelleyUtxoPredFailure" forall a b. (a -> b) -> a -> b
$ do
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"ValueNotConservedUTxO" forall a b. (a -> b) -> a -> b
$ do
Positive Integer
tooMuch <- forall a (m :: * -> *). (Arbitrary a, MonadGen m) => m a
arbitrary
Tx era
txMinted <- forall era.
(HasCallStack, AllegraEraScript era, MaryEraImp era) =>
ImpTestM era (Tx era)
mintBasicToken
let MaryValue Coin
c (MultiAsset Map PolicyID (Map AssetName Integer)
mintedMultiAsset) =
case Tx era
txMinted forall s a. s -> Getting a s a -> a
^. forall era. EraTx era => Lens' (Tx era) (TxBody era)
bodyTxL forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall era.
EraTxBody era =>
Lens' (TxBody era) (StrictSeq (TxOut era))
outputsTxBodyL of
StrictSeq (TxOut era)
Empty -> forall a. HasCallStack => String -> a
error String
"Empty outputs was unexpected"
TxOut era
txOut :<| StrictSeq (TxOut era)
_ -> TxOut era
txOut forall s a. s -> Getting a s a -> a
^. forall era. EraTxOut era => Lens' (TxOut era) (Value era)
valueTxOutL
burnTooMuchMultiAsset :: MultiAsset
burnTooMuchMultiAsset@(MultiAsset Map PolicyID (Map AssetName Integer)
burnTooMuch) =
Map PolicyID (Map AssetName Integer) -> MultiAsset
MultiAsset (forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (forall a. Num a => a -> a -> a
subtract Integer
tooMuch forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
negate)) Map PolicyID (Map AssetName Integer)
mintedMultiAsset)
burnTooMuchProducedMultiAsset :: MultiAsset
burnTooMuchProducedMultiAsset = Map PolicyID (Map AssetName Integer) -> MultiAsset
MultiAsset (forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (forall a b k. (a -> b) -> Map k a -> Map k b
Map.map forall a. Num a => a -> a
negate) Map PolicyID (Map AssetName Integer)
burnTooMuch)
txBody :: TxBody era
txBody =
forall era. EraTxBody era => TxBody era
mkBasicTxBody
forall a b. a -> (a -> b) -> b
& forall era. EraTxBody era => Lens' (TxBody era) (Set TxIn)
inputsTxBodyL forall s t a b. ASetter s t a b -> b -> s -> t
.~ [forall i era.
(HasCallStack, Integral i, EraTx era) =>
i -> Tx era -> TxIn
txInAt (Int
0 :: Int) Tx era
txMinted]
forall a b. a -> (a -> b) -> b
& forall era. MaryEraTxBody era => Lens' (TxBody era) MultiAsset
mintTxBodyL forall s t a b. ASetter s t a b -> b -> s -> t
.~ MultiAsset
burnTooMuchMultiAsset
(TxIn
_, TxOut era
rootTxOut) <- forall era. ImpTestM era (TxIn, TxOut era)
lookupImpRootTxOut
let rootTxOutValue :: MaryValue
rootTxOutValue = TxOut era
rootTxOut forall s a. s -> Getting a s a -> a
^. forall era. EraTxOut era => Lens' (TxOut era) (Value era)
valueTxOutL
forall era.
(HasCallStack, ShelleyEraImp era) =>
Tx era
-> NonEmpty (PredicateFailure (EraRule "LEDGER" era))
-> ImpTestM era ()
submitFailingTx
(forall era. EraTx era => TxBody era -> Tx era
mkBasicTx TxBody era
txBody)
[ forall (rule :: Symbol) (t :: * -> *) era.
InjectRuleFailure rule t era =>
t era -> EraRuleFailure rule era
injectFailure forall a b. (a -> b) -> a -> b
$
forall era.
Mismatch 'RelEQ (Value era) -> ShelleyUtxoPredFailure era
ValueNotConservedUTxO forall a b. (a -> b) -> a -> b
$
forall (r :: Relation) a. a -> a -> Mismatch r a
Mismatch
(MaryValue
rootTxOutValue forall a. Semigroup a => a -> a -> a
<> Coin -> MultiAsset -> MaryValue
MaryValue Coin
c (Map PolicyID (Map AssetName Integer) -> MultiAsset
MultiAsset Map PolicyID (Map AssetName Integer)
mintedMultiAsset))
(MaryValue
rootTxOutValue forall a. Semigroup a => a -> a -> a
<> Coin -> MultiAsset -> MaryValue
MaryValue Coin
c MultiAsset
burnTooMuchProducedMultiAsset)
]