Skip to content

Commit

Permalink
Added Consenus tests for Conway era. Also extended
Browse files Browse the repository at this point in the history
Test.Cardano.Ledger.Generic.Proof and related modules to the Conway era.
  • Loading branch information
TimSheard committed Aug 9, 2022
1 parent d4743d8 commit 64abff3
Show file tree
Hide file tree
Showing 22 changed files with 579 additions and 26 deletions.
1 change: 1 addition & 0 deletions cabal.project
Expand Up @@ -6,6 +6,7 @@ packages:
eras/babbage/impl
eras/babbage/test-suite
eras/conway/impl
eras/conway/test-suite
eras/byron/chain/executable-spec
eras/byron/ledger/executable-spec
eras/byron/ledger/impl
Expand Down
9 changes: 4 additions & 5 deletions eras/conway/impl/cardano-ledger-conway.cabal
Expand Up @@ -48,18 +48,17 @@ library
build-depends:
bytestring,
cardano-binary,
cardano-crypto-class,
-- cardano-crypto-class,
cardano-ledger-alonzo,
cardano-ledger-babbage,
cardano-ledger-core,
cardano-ledger-shelley,
cardano-ledger-shelley-ma,
cardano-slotting,
-- cardano-slotting,
containers,
microlens,
plutus-ledger-api ^>= 1.0,
plutus-tx ^>= 1.0,
-- plutus-ledger-api ^>= 1.0,
-- plutus-tx ^>= 1.0,
strict-containers,
text,
hs-source-dirs:
src
71 changes: 71 additions & 0 deletions eras/conway/test-suite/cardano-ledger-conway-test.cabal
@@ -0,0 +1,71 @@
cabal-version: 3.0

name: cardano-ledger-conway-test
version: 0.1.0.0
synopsis: Tests for Cardano ledger conway era
description:
This package builds upon the Alonzo ledger
bug-reports: https://github.com/input-output-hk/cardano-ledger/issues
license: Apache-2.0
author: IOHK Formal Methods Team
maintainer: formal.methods@iohk.io
copyright: 2020 Input Output (Hong Kong) Ltd.
category: Network
build-type: Simple

data-files:
cddl-files/conway.cddl
cddl-files/real/crypto.cddl
cddl-files/mock/extras.cddl

source-repository head
type: git
location: https://github.com/input-output-hk/cardano-ledger
subdir: eras/conway/test-suite

common base
build-depends: base >= 4.12 && < 4.15

common project-config
default-language: Haskell2010

ghc-options: -Wall
-Wcompat
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wpartial-fields
-Wredundant-constraints
-- -Wunused-packages

library
import: base, project-config

exposed-modules:
Test.Cardano.Ledger.Conway.Examples.Consensus,
build-depends:
bytestring,
cardano-binary,
cardano-ledger-alonzo,
cardano-ledger-alonzo-test,
cardano-ledger-babbage,
cardano-ledger-conway,
cardano-ledger-core,
cardano-ledger-pretty,
cardano-ledger-shelley-ma-test,
cardano-ledger-shelley-ma,
cardano-protocol-tpraos,
cardano-slotting,
containers,
data-default-class,
hashable,
plutus-tx,
plutus-ledger-api,
QuickCheck,
cardano-ledger-shelley-test,
cardano-ledger-shelley,
small-steps,
strict-containers,
text,
hs-source-dirs:
src

@@ -0,0 +1,196 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module Test.Cardano.Ledger.Conway.Examples.Consensus where

import Cardano.Ledger.Alonzo.Data
( AlonzoAuxiliaryData (..),
AuxiliaryDataHash (..),
Data (..),
dataToBinaryData,
hashData,
)
import Cardano.Ledger.Alonzo.Language (Language (..))
import Cardano.Ledger.Alonzo.Scripts (AlonzoScript (..), ExUnits (..))
import qualified Cardano.Ledger.Alonzo.Scripts as Tag (Tag (..))
import Cardano.Ledger.Alonzo.Tx (IsValid (..))
import Cardano.Ledger.Alonzo.TxWitness (RdmrPtr (..), Redeemers (..), TxDats (..))
import Cardano.Ledger.Babbage.TxBody (Datum (..))
import Cardano.Ledger.BaseTypes (StrictMaybe (..))
import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Conway (ConwayEra)
import Cardano.Ledger.Conway.Genesis (ConwayGenesis (..))
import Cardano.Ledger.Conway.PParams (BabbagePParamsHKD (..), emptyPParams, emptyPParamsUpdate)
import Cardano.Ledger.Conway.Translation ()
import Cardano.Ledger.Conway.Tx (AlonzoTx (..))
import Cardano.Ledger.Conway.TxBody (BabbageTxBody (..), BabbageTxOut (..))
import Cardano.Ledger.Conway.TxWits (TxWitness (..))
import Cardano.Ledger.Core (EraScript (hashScript), TxBody)
import Cardano.Ledger.Credential (Credential (KeyHashObj, ScriptHashObj))
import Cardano.Ledger.Crypto (StandardCrypto)
import Cardano.Ledger.Keys (GenDelegs (..), asWitness)
import Cardano.Ledger.Mary.Value (MaryValue (..))
import Cardano.Ledger.SafeHash (hashAnnotated)
import Cardano.Ledger.Serialization (mkSized)
import Cardano.Ledger.Shelley.API (ApplyTxError (..), Network (..), NewEpochState (..), ProposedPPUpdates (..), RewardAcnt (..), TxId (..), Update (..), Wdrl (..))
import Cardano.Ledger.Shelley.Rules.Delegs (DelegsPredicateFailure (..))
import Cardano.Ledger.Shelley.Rules.Ledger (LedgerPredicateFailure (DelegsFailure))
import Cardano.Ledger.Shelley.Tx (ShelleyTx (..))
import Cardano.Ledger.Shelley.UTxO (makeWitnessesVKey)
import Cardano.Ledger.ShelleyMA.Timelocks (Timelock (..), ValidityInterval (..))
import Cardano.Ledger.TxIn (mkTxInPartial)
import Cardano.Slotting.Slot (EpochNo (..), SlotNo (..))
import Data.Default.Class (Default (def))
import qualified Data.Map.Strict as Map
import Data.Proxy (Proxy (..))
import qualified Data.Sequence.Strict as StrictSeq
import qualified Data.Set as Set
import qualified PlutusTx as Plutus
import Test.Cardano.Ledger.Alonzo.Scripts (alwaysFails, alwaysSucceeds)
import qualified Test.Cardano.Ledger.Mary.Examples.Consensus as MarySLE
import qualified Test.Cardano.Ledger.Shelley.Examples.Consensus as SLE
import Test.Cardano.Ledger.Shelley.Orphans ()
import Test.Cardano.Ledger.Shelley.Utils (mkAddr)

-- ==============================================================

type StandardConway = ConwayEra StandardCrypto

-- | ShelleyLedgerExamples for Conway era
ledgerExamplesConway :: SLE.ShelleyLedgerExamples StandardConway
ledgerExamplesConway =
SLE.ShelleyLedgerExamples
{ SLE.sleBlock = SLE.exampleShelleyLedgerBlock exampleTransactionInBlock,
SLE.sleHashHeader = SLE.exampleHashHeader (Proxy @StandardConway),
SLE.sleTx = exampleTransactionInBlock,
SLE.sleApplyTxError =
ApplyTxError $
pure $
DelegsFailure $
DelegateeNotRegisteredDELEG @StandardConway (SLE.mkKeyHash 1),
SLE.sleRewardsCredentials =
Set.fromList
[ Left (Coin 100),
Right (ScriptHashObj (SLE.mkScriptHash 1)),
Right (KeyHashObj (SLE.mkKeyHash 2))
],
SLE.sleResultExamples = resultExamples,
SLE.sleNewEpochState = exampleConwayNewEpochState,
SLE.sleChainDepState = SLE.exampleLedgerChainDepState 1,
SLE.sleTranslationContext = exampleConwayGenesis
}
where
resultExamples =
SLE.ShelleyResultExamples
{ SLE.srePParams = def,
SLE.sreProposedPPUpdates = examplePPPU,
SLE.srePoolDistr = SLE.examplePoolDistr,
SLE.sreNonMyopicRewards = SLE.exampleNonMyopicRewards,
SLE.sreShelleyGenesis = SLE.testShelleyGenesis
}
examplePPPU =
ProposedPPUpdates $
Map.singleton
(SLE.mkKeyHash 0)
(emptyPParamsUpdate {_collateralPercentage = SJust 150})

collateralOutput :: BabbageTxOut StandardConway
collateralOutput =
BabbageTxOut
(mkAddr (SLE.examplePayKey, SLE.exampleStakeKey))
(MaryValue 8675309 mempty)
NoDatum
SNothing

exampleTxBodyConway :: TxBody StandardConway
exampleTxBodyConway =
BabbageTxBody
(Set.fromList [mkTxInPartial (TxId (SLE.mkDummySafeHash Proxy 1)) 0]) -- spending inputs
(Set.fromList [mkTxInPartial (TxId (SLE.mkDummySafeHash Proxy 2)) 1]) -- collateral inputs
(Set.fromList [mkTxInPartial (TxId (SLE.mkDummySafeHash Proxy 1)) 3]) -- reference inputs
( StrictSeq.fromList
[ mkSized $
BabbageTxOut
(mkAddr (SLE.examplePayKey, SLE.exampleStakeKey))
(MarySLE.exampleMultiAssetValue 2)
(Datum $ dataToBinaryData datumExample) -- inline datum
(SJust $ alwaysSucceeds PlutusV2 3) -- reference script
]
)
(SJust $ mkSized collateralOutput) -- collateral return
(SJust $ Coin 8675309) -- collateral tot
SLE.exampleCerts -- txcerts
( Wdrl $
Map.singleton
(RewardAcnt Testnet (SLE.keyToCredential SLE.exampleStakeKey))
(Coin 100) -- txwdrls
)
(Coin 999) -- txfee
(ValidityInterval (SJust (SlotNo 2)) (SJust (SlotNo 4))) -- txvldt
( SJust $
Update
( ProposedPPUpdates $
Map.singleton
(SLE.mkKeyHash 1)
(emptyPParamsUpdate {_maxBHSize = SJust 4000})
)
(EpochNo 0)
) -- txUpdates
(Set.singleton $ SLE.mkKeyHash 212) -- reqSignerHashes
(MarySLE.exampleMultiAssetValue 3) -- mint
(SJust $ SLE.mkDummySafeHash (Proxy @StandardCrypto) 42) -- scriptIntegrityHash
(SJust . AuxiliaryDataHash $ SLE.mkDummySafeHash (Proxy @StandardCrypto) 42) -- adHash
(SJust Mainnet) -- txnetworkid

datumExample :: Data StandardConway
datumExample = Data (Plutus.I 191)

redeemerExample :: Data StandardConway
redeemerExample = Data (Plutus.I 919)

exampleTx :: ShelleyTx StandardConway
exampleTx =
ShelleyTx
exampleTxBodyConway
( TxWitness
(makeWitnessesVKey (hashAnnotated exampleTxBodyConway) [asWitness SLE.examplePayKey]) -- vkey
mempty -- bootstrap
( Map.singleton
(hashScript @StandardConway $ alwaysSucceeds PlutusV1 3)
(alwaysSucceeds PlutusV1 3) -- txscripts
)
(TxDats $ Map.singleton (hashData datumExample) datumExample)
( Redeemers $
Map.singleton (RdmrPtr Tag.Spend 0) (redeemerExample, ExUnits 5000 5000)
) -- redeemers
)
( SJust $
AlonzoAuxiliaryData
SLE.exampleMetadataMap -- metadata
( StrictSeq.fromList
[alwaysFails PlutusV1 2, TimelockScript $ RequireAllOf mempty] -- Scripts
)
)

exampleTransactionInBlock :: AlonzoTx StandardConway
exampleTransactionInBlock = AlonzoTx b w (IsValid True) a
where
(ShelleyTx b w a) = exampleTx

exampleConwayNewEpochState :: NewEpochState StandardConway
exampleConwayNewEpochState =
SLE.exampleNewEpochState
(MarySLE.exampleMultiAssetValue 1)
emptyPParams
(emptyPParams {_coinsPerUTxOByte = Coin 1})

exampleConwayGenesis :: ConwayGenesis crypto
exampleConwayGenesis =
ConwayGenesis (GenDelegs Map.empty)
Expand Up @@ -24,7 +24,6 @@ import qualified Cardano.Ledger.Era as Era (Crypto)
import Cardano.Ledger.Keys
import Cardano.Ledger.PoolDistr
import Cardano.Ledger.SafeHash
import Cardano.Ledger.Serialization
import Cardano.Ledger.Shelley (ShelleyEra)
import Cardano.Ledger.Shelley.API
import Cardano.Ledger.Shelley.EpochBoundary
Expand Down Expand Up @@ -96,9 +95,7 @@ data ShelleyLedgerExamples era = ShelleyLedgerExamples
-------------------------------------------------------------------------------}

type ShelleyBasedEra' era =
( ToCBORGroup (TxSeq era),
ToCBOR (Core.Witnesses era),
Default (State (Core.EraRule "PPUP" era)),
( Default (State (Core.EraRule "PPUP" era)),
PraosCrypto (Cardano.Ledger.Era.Crypto era)
)

Expand Down Expand Up @@ -251,7 +248,7 @@ examplePoolDistr =

exampleNonMyopicRewards ::
forall c.
PraosCrypto c =>
CC.Crypto c =>
Map
(Either Coin (Credential 'Staking c))
(Map (KeyHash 'StakePool c) Coin)
Expand Down Expand Up @@ -363,7 +360,7 @@ exampleNewEpochState value ppp pp =
(activeSlotCoeff testGlobals)
10

exampleLedgerChainDepState :: forall c. PraosCrypto c => Word64 -> ChainDepState c
exampleLedgerChainDepState :: forall c. CC.Crypto c => Word64 -> ChainDepState c
exampleLedgerChainDepState seed =
ChainDepState
{ csProtocol =
Expand Down
1 change: 1 addition & 0 deletions libs/cardano-ledger-test/cardano-ledger-test.cabal
Expand Up @@ -107,6 +107,7 @@ library
cardano-ledger-alonzo,
cardano-ledger-alonzo-test,
cardano-ledger-babbage,
cardano-ledger-conway,
cardano-ledger-core,
cardano-ledger-pretty,
cardano-ledger-shelley-ma,
Expand Down
Expand Up @@ -130,6 +130,7 @@ evenData3ArgsScript proof =
Allegra _ -> error unsupported
Alonzo _ -> evenData3ArgsLang PlutusV1
Babbage _ -> evenData3ArgsLang PlutusV2
Conway _ -> evenData3ArgsLang PlutusV2
where
unsupported = "Plutus scripts are not supported in:" ++ show proof
evenData3ArgsLang lang =
Expand Down Expand Up @@ -696,6 +697,7 @@ inlineDatumWithPlutusV1Script pf =

malformedScript :: forall era. Proof era -> ShortByteString -> Script era
malformedScript pf s = case pf of
Conway {} -> ms
Babbage {} -> ms
Alonzo {} -> ms
x@Shelley {} -> er x
Expand Down

0 comments on commit 64abff3

Please sign in to comment.