Skip to content

Commit

Permalink
add Alonzo tripping test for Block and Tx
Browse files Browse the repository at this point in the history
Additionally, clean up the arbitrary Block instance to work in Alonzo.
  • Loading branch information
Jared Corduan committed May 7, 2021
1 parent 9ac75ad commit 7447dd7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import Cardano.Ledger.Alonzo.Scripts (Script)
import Cardano.Ledger.Alonzo.Tx (CostModel, WitnessPPData)
import Cardano.Ledger.Alonzo.TxBody (TxBody)
import Cardano.Ledger.Alonzo.TxWitness
import qualified Cardano.Ledger.Tx as LTX
import qualified Data.ByteString as BS (ByteString)
import qualified Data.ByteString.Base16.Lazy as Base16
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified PlutusTx as Plutus
import Shelley.Spec.Ledger.BlockChain (Block)
import Shelley.Spec.Ledger.Metadata (Metadata)
import Test.Cardano.Ledger.Alonzo.Serialisation.Generators ()
import Test.Cardano.Ledger.ShelleyMA.Serialisation.Coders (roundTrip, roundTripAnn)
Expand Down Expand Up @@ -111,5 +113,9 @@ tests =
testProperty "Script" $
trippingAnn @(Script (AlonzoEra C_Crypto)),
testProperty "WitnessPPData" $
trippingAnn @(WitnessPPData (AlonzoEra C_Crypto))
trippingAnn @(WitnessPPData (AlonzoEra C_Crypto)),
testProperty "alonzo/Tx" $
trippingAnn @(LTX.Tx (AlonzoEra C_Crypto)),
testProperty "alonzo/Block" $
trippingAnn @(Block (AlonzoEra C_Crypto))
]
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ import Cardano.Ledger.Coin (DeltaCoin (..))
import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Crypto (DSIGN)
import qualified Cardano.Ledger.Crypto as CC (Crypto)
import Cardano.Ledger.Era (Crypto, Era, ValidateScript)
import Cardano.Ledger.Era (Crypto, Era, SupportsSegWit (..), ValidateScript)
import Cardano.Ledger.SafeHash (HasAlgorithm, SafeHash, unsafeMakeSafeHash)
import Cardano.Ledger.Shelley.Constraints
( UsesAuxiliary,
UsesScript,
( UsesScript,
UsesTxBody,
UsesTxOut,
UsesValue,
Expand Down Expand Up @@ -125,23 +124,18 @@ import qualified Shelley.Spec.Ledger.STS.Ppup as STS
import qualified Shelley.Spec.Ledger.STS.Prtcl as STS (PrtclState)
import qualified Shelley.Spec.Ledger.STS.Tickn as STS
import qualified Shelley.Spec.Ledger.STS.Utxow as STS
import Shelley.Spec.Ledger.Serialization (ToCBORGroup)
import Shelley.Spec.Ledger.Tx (WitnessSetHKD (WitnessSet), hashScript)
import Test.QuickCheck (Arbitrary, arbitrary, genericShrink, listOf, oneof, recursivelyShrink, resize, shrink, vectorOf)
import Test.QuickCheck.Gen (chooseAny)
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (Mock)
import Test.Shelley.Spec.Ledger.Generator.Constants (defaultConstants)
import Test.Shelley.Spec.Ledger.Generator.Core
( KeySpace (KeySpace_),
PreAlonzo,
geKeySpace,
ksCoreNodes,
mkBlock,
mkBlockHeader,
( mkBlockHeader,
mkOCert,
)
import Test.Shelley.Spec.Ledger.Generator.EraGen (EraGen)
import Test.Shelley.Spec.Ledger.Generator.Presets (coreNodeKeys, genEnv)
import Test.Shelley.Spec.Ledger.Generator.ScriptClass (ScriptClass)
import Test.Shelley.Spec.Ledger.Generator.Presets (coreNodeKeys)
import Test.Shelley.Spec.Ledger.Serialisation.Generators.Bootstrap
( genBootstrapAddress,
genSignature,
Expand Down Expand Up @@ -772,12 +766,13 @@ instance
shrink _ = []

genTx ::
( UsesTxBody era,
UsesAuxiliary era,
( Era era,
Arbitrary (Core.TxBody era),
Arbitrary (Core.AuxiliaryData era),
ToCBOR (Core.Witnesses era),
Arbitrary (Core.Witnesses era)
Arbitrary (Core.Witnesses era),
ToCBOR (Core.AuxiliaryData era), -- for Tx Pattern
ToCBOR (Core.TxBody era), -- for Tx Pattern
ToCBOR (Core.Witnesses era) -- for Tx Pattern
) =>
Gen (Tx era)
genTx =
Expand All @@ -788,69 +783,35 @@ genTx =

genBlock ::
forall era.
( UsesTxBody era,
UsesAuxiliary era,
PreAlonzo era,
ScriptClass era,
( Era era,
ToCBORGroup (TxSeq era),
Mock (Crypto era),
Arbitrary (Core.Witnesses era),
Arbitrary (Core.TxBody era),
Arbitrary (Core.AuxiliaryData era)
Arbitrary (TxInBlock era)
) =>
Gen (Block era)
genBlock = do
let KeySpace_ {ksCoreNodes} = geKeySpace (genEnv p)
prevHash <- arbitrary :: Gen (HashHeader (Crypto era))
allPoolKeys <- elements (map snd ksCoreNodes)
txs <- listOf (genTx @era)
curSlotNo <- SlotNo <$> choose (0, 10)
curBlockNo <- BlockNo <$> choose (0, 100)
epochNonce <- arbitrary :: Gen Nonce
let kesPeriod = 1
keyRegKesPeriod = 1
ocert = mkOCert allPoolKeys 1 (KESPeriod kesPeriod)
return $
mkBlock
prevHash
allPoolKeys
txs
curSlotNo
curBlockNo
epochNonce
kesPeriod
keyRegKesPeriod
ocert
where
p :: Proxy era
p = Proxy
genBlock = Block <$> arbitrary <*> (toTxSeq @era <$> arbitrary)

instance
( UsesTxBody era,
UsesScript era,
UsesAuxiliary era,
PreAlonzo era,
Mock (Crypto era),
ValidateScript era,
( Era era,
Arbitrary (Core.TxBody era),
Arbitrary (Core.Value era),
Arbitrary (Core.AuxiliaryData era),
Arbitrary (Core.Script era)
Arbitrary (Core.Script era),
Arbitrary (Core.Witnesses era),
ToCBOR (Core.AuxiliaryData era), -- for Tx Pattern
ToCBOR (Core.TxBody era), -- for Tx Pattern
ToCBOR (Core.Witnesses era) -- for Tx Pattern
) =>
Arbitrary (Tx era)
where
arbitrary = genTx

instance
( UsesTxBody era,
UsesAuxiliary era,
PreAlonzo era,
EraGen era,
ToCBORGroup (TxSeq era),
SupportsSegWit era,
Mock (Crypto era),
ValidateScript era,
Arbitrary (Core.TxBody era),
Arbitrary (Core.Value era),
Arbitrary (Core.AuxiliaryData era),
Arbitrary (Core.Script era)
Arbitrary (TxInBlock era)
) =>
Arbitrary (Block era)
where
Expand Down

0 comments on commit 7447dd7

Please sign in to comment.