Skip to content

Commit

Permalink
Alonzo testing
Browse files Browse the repository at this point in the history
Start establishing the framework for Alonzo tests, so far just including
roundtrip tests. We include a roundtrip CBOR test for TxWitness.
  • Loading branch information
nc6 committed Dec 9, 2020
1 parent 990aaaa commit 9be566a
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 1 deletion.
48 changes: 47 additions & 1 deletion alonzo/impl/cardano-ledger-alonzo.cabal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cabal-version: 2.2
cabal-version: 3.0

name: cardano-ledger-alonzo
version: 0.1.0.0
Expand Down Expand Up @@ -39,3 +39,49 @@ library
hs-source-dirs:
src
default-language: Haskell2010

library test
if impl(ghc < 8.10)
buildable: False
visibility: public
exposed-modules:
Test.Cardano.Ledger.Alonzo.Serialisation.Generators
build-depends:
base >=4.14 && <4.15,
cardano-binary,
cardano-crypto-class,
cardano-ledger-alonzo,
cardano-ledger-shelley-ma,
cardano-slotting,
containers,
deepseq,
nothunks,
QuickCheck,
shelley-spec-ledger-test,
shelley-spec-ledger,
small-steps
hs-source-dirs:
test/lib
default-language: Haskell2010

test-suite tests
if impl(ghc < 8.10)
buildable: False
type: exitcode-stdio-1.0
main-is: Tests.hs
hs-source-dirs:
test/test
other-modules:
Test.Cardano.Ledger.Alonzo.Serialisation.Tripping
build-depends:
base >=4.14 && <4.15,
bytestring,
cardano-binary,
cardano-ledger-alonzo,
cardano-ledger-alonzo:test,
cardano-ledger-shelley-ma-test,
QuickCheck,
shelley-spec-ledger-test,
tasty-hunit,
tasty-quickcheck,
tasty
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

module Test.Cardano.Ledger.Alonzo.Serialisation.Generators where

import Cardano.Ledger.Alonzo (AlonzoEra)
import Cardano.Ledger.Alonzo.Data (Data (..))
import Cardano.Ledger.Alonzo.Scripts
import Cardano.Ledger.Alonzo.TxWitness
import qualified Cardano.Ledger.Core as Core
import qualified Cardano.Ledger.Crypto as CC
import Cardano.Ledger.Era (Crypto)
import Cardano.Ledger.Shelley.Constraints (ShelleyBased)
import Cardano.Ledger.ShelleyMA.Timelocks
import Cardano.Slotting.Slot (SlotNo (..))
import Test.QuickCheck
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (Mock)
import Test.Shelley.Spec.Ledger.Serialisation.EraIndepGenerators ()

-- TODO correct arbitrary generator for Data
instance Arbitrary (Data era) where
arbitrary = pure NotReallyData

instance Arbitrary Tag where
arbitrary = elements [Input, Mint, Cert, Wdrl]

instance Arbitrary RdmrPtr where
arbitrary = RdmrPtr <$> arbitrary <*> arbitrary

-- TODO correct arbitrary generator for Alonzo scripts
instance CC.Crypto c => Arbitrary (Timelock (AlonzoEra c)) where
arbitrary = pure $ RequireTimeStart (SlotNo 0)

instance
( ShelleyBased era,
Mock (Crypto era),
Arbitrary (Core.Script era)
) =>
Arbitrary (TxWitness era)
where
arbitrary =
TxWitness
<$> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeApplications #-}

module Test.Cardano.Ledger.Alonzo.Serialisation.Tripping where

import Cardano.Binary
import Cardano.Ledger.Alonzo
import Cardano.Ledger.Alonzo.Scripts (Script)
import Cardano.Ledger.Alonzo.TxWitness (TxWitness)
import qualified Data.ByteString.Lazy.Char8 as BSL
import Test.Cardano.Ledger.Alonzo.Serialisation.Generators ()
import Test.Cardano.Ledger.ShelleyMA.Serialisation.Coders
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes
import Test.Tasty
import Test.Tasty.QuickCheck

trippingAnn ::
( Eq t,
Show t,
ToCBOR t,
FromCBOR (Annotator t)
) =>
t ->
Property
trippingAnn x = case roundTripAnn x of
Right (remaining, y) | BSL.null remaining -> x === y
Right (remaining, _) ->
counterexample
("Unconsumed trailing bytes:\n" <> BSL.unpack remaining)
False
Left stuff ->
counterexample
("Failed to decode: " <> show stuff)
False

tests :: TestTree
tests =
testGroup
"Alonzo CBOR round-trip"
[ testProperty "alonzo/script" $
trippingAnn @(Script (AlonzoEra C_Crypto)),
testProperty "alonzo/TxWitness" $
trippingAnn @(TxWitness (AlonzoEra C_Crypto))
]
14 changes: 14 additions & 0 deletions alonzo/impl/test/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Main where

import qualified Test.Cardano.Ledger.Alonzo.Serialisation.Tripping as Tripping
import Test.Tasty

tests :: TestTree
tests =
testGroup
"Alonzo tests"
[ Tripping.tests
]

main :: IO ()
main = defaultMain tests
6 changes: 6 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ cradle:

- path: "alonzo/impl/src"
component: "lib:cardano-ledger-alonzo"

- path: "alonzo/impl/test/lib"
component: "cardano-ledger-alonzo:lib:test"

- path: "alonzo/impl/test/test"
component: "cardano-ledger-alonzo:test:tests"

0 comments on commit 9be566a

Please sign in to comment.