Skip to content

Commit

Permalink
Drop duplicate TxBody serialisation tests.
Browse files Browse the repository at this point in the history
These are already covered by the ShelleyMA/Serialisation tests.
  • Loading branch information
nc6 committed Feb 25, 2021
1 parent a9214cf commit a5bfe35
Showing 1 changed file with 2 additions and 113 deletions.
115 changes: 2 additions & 113 deletions shelley-ma/shelley-ma-test/src/Test/Cardano/Ledger/ShelleyMA/TxBody.hs
Expand Up @@ -13,47 +13,22 @@
module Test.Cardano.Ledger.ShelleyMA.TxBody
( txBodyTest,
TestEra,
genShelleyBody,
genMaryBody,
)
where

import Cardano.Binary (ToCBOR (..))
-- Arbitrary instances
-- Arbitrary instances

import Cardano.Ledger.Mary (MaryEra)
import Cardano.Ledger.Mary.Value
( AssetName (..),
PolicyID (..),
Value (..),
)
import Cardano.Ledger.Shelley.Constraints
( UsesAuxiliary,
UsesPParams,
UsesScript,
UsesValue,
)
import Cardano.Ledger.ShelleyMA.Timelocks (Timelock (..), ValidityInterval (..))
import Cardano.Ledger.ShelleyMA.TxBody
( TxBodyRaw (..),
bodyFields,
initial,
txSparse,
)
import qualified Cardano.Ledger.ShelleyMA.TxBody as Mary
import Cardano.Slotting.Slot (SlotNo (..))
import Data.ByteString.Short (ShortByteString)
import qualified Data.ByteString.Short as Short
import Data.Coders
( Decode (..),
Density (..),
Wrapped (..),
decode,
encode,
)
import qualified Data.Map.Strict as Map
import Data.MemoBytes (MemoBytes (Memo), roundTripMemo)
import Data.MemoBytes (MemoBytes (Memo))
import Data.Sequence.Strict (fromList)
import qualified Data.Sequence.Strict as StrictSeq
import Data.Set (empty)
Expand All @@ -63,19 +38,10 @@ import Shelley.Spec.Ledger.BaseTypes (StrictMaybe (SJust, SNothing))
import Shelley.Spec.Ledger.Coin (Coin (..))
import Shelley.Spec.Ledger.Tx (hashScript)
import Shelley.Spec.Ledger.TxBody (Wdrl (..))
import qualified Shelley.Spec.Ledger.TxBody as Shelley
import Test.Cardano.Ledger.EraBuffet (TestCrypto)
import Test.Cardano.Ledger.ShelleyMA.Serialisation.Coders
( RoundTripResult,
embedTrip',
roundTrip',
roundTripAnn,
)
import Test.Cardano.Ledger.ShelleyMA.Serialisation.Generators (genMintValues)
import Test.Shelley.Spec.Ledger.Serialisation.Generators ()
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck

-- ============================================================================================
-- make an example
Expand Down Expand Up @@ -139,87 +105,10 @@ fieldTests =
testCase "mint" (assertEqual "mint" (getField @"mint" txM) testmint)
]

roundtrip :: Mary.TxBody TestEra -> Assertion
roundtrip (Mary.TxBodyConstr memo) =
case roundTripMemo memo of
Right ("", new) -> new @?= memo
Right (extra, _new) -> error ("extra bytes: " <> show extra)
Left s -> error (show s)

-- =====================================================================
-- Now some random property tests

checkSparse :: TxBodyRaw TestEra -> Bool
checkSparse tx = case oldStyleRoundTrip tx of
Right ("", _) -> True
Right (left, _) -> error ("left over input: " ++ show left)
Left s -> error (show s)

embedTest :: Gen Bool
embedTest = do
shelleybody <- genShelleyBody
case embedTrip' toCBOR (decode (getTxSparse @TestEra)) shelleybody of
Right ("", _) -> pure True
Right (left, _) -> error ("left over input: " ++ show left)
Left s -> error (show s)

getTxSparse ::
(UsesValue era, UsesPParams era, UsesScript era, UsesAuxiliary era) =>
Decode ('Closed 'Dense) (TxBodyRaw era)
getTxSparse =
SparseKeyed
"TxBodyRaw"
initial
bodyFields
[(0, "inputs"), (1, "outputs"), (2, "txfee")]

oldStyleRoundTrip :: TxBodyRaw TestEra -> RoundTripResult (TxBodyRaw TestEra)
oldStyleRoundTrip = roundTrip' (encode . txSparse) (decode getTxSparse)

genShelleyBody :: Gen (Shelley.TxBody TestEra)
genShelleyBody =
Shelley.TxBody
<$> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary

genMaryBody :: Gen (TxBodyRaw TestEra)
genMaryBody =
TxBodyRaw
<$> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> arbitrary
<*> genMintValues

instance Arbitrary (TxBodyRaw TestEra) where
arbitrary = genMaryBody

checkSparseAnn :: Mary.TxBody TestEra -> Bool
checkSparseAnn tx = case roundTripAnn tx of
Right ("", _) -> True
Right (left, _) -> error ("left over input: " ++ show left)
Left s -> error (show s)

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

txBodyTest :: TestTree
txBodyTest =
testGroup
"TxBody"
[ fieldTests,
testCase "length" (assertEqual "length" 36 (Short.length (bytes txM))),
testCase "roundtrip txM" (roundtrip txM),
testProperty "roundtrip sparse TxBodyRaw" checkSparse,
testProperty "embed Shelley sparse TxBodyRaw" embedTest,
testProperty "roundtrip sparse TxBody" checkSparseAnn
testCase "length" (assertEqual "length" 36 (Short.length (bytes txM)))
]

0 comments on commit a5bfe35

Please sign in to comment.