1+ {-# LANGUAGE DerivingStrategies #-}
12{-# LANGUAGE FlexibleContexts #-}
23{-# LANGUAGE FlexibleInstances #-}
34{-# LANGUAGE ScopedTypeVariables #-}
1112module Test.Consensus.Shelley.LedgerTables (tests ) where
1213
1314import qualified Cardano.Ledger.Api.Era as L
15+ import qualified Cardano.Ledger.BaseTypes as L
16+ import qualified Cardano.Ledger.Shelley.API.Types as L
17+ import Data.MemPack
1418import Data.Proxy
1519import Data.SOP.BasicFunctors
1620import Data.SOP.Constraint
@@ -29,12 +33,15 @@ import Test.Cardano.Ledger.Dijkstra.Arbitrary ()
2933import Test.Consensus.Shelley.Generators ()
3034import Test.Consensus.Shelley.MockCrypto (CanMock )
3135import Test.LedgerTables
36+ import Test.QuickCheck
3237import Test.Tasty
3338import Test.Tasty.QuickCheck
3439
3540tests :: TestTree
3641tests =
3742 testGroup " LedgerTables"
43+ . (testProperty " Serializing BigEndianTxIn preserves order" testBigEndianTxInPreservesOrder : )
44+ . (testProperty " Serializing TxIn fails to preserve order" (expectFailure testTxInPreservesOrder) : )
3845 . hcollapse
3946 . hcmap (Proxy @ TestLedgerTables ) (K . f)
4047 $ (hpure Proxy :: NP Proxy (CardanoShelleyEras StandardCrypto ))
@@ -74,3 +81,15 @@ instance
7481 Arbitrary (LedgerTables (LedgerState (ShelleyBlock proto era )) ValuesMK )
7582 where
7683 arbitrary = projectLedgerTables . unstowLedgerTables <$> arbitrary
84+
85+ testBigEndianTxInPreservesOrder :: L. TxId -> L. TxIx -> L. TxIx -> Property
86+ testBigEndianTxInPreservesOrder txid txix1 txix2 =
87+ let b1 = packByteString (BigEndianTxIn $ L. TxIn txid txix1)
88+ b2 = packByteString (BigEndianTxIn $ L. TxIn txid txix2)
89+ in counterexample (show b1 <> " " <> show b2) $ compare b1 b2 === compare txix1 txix2
90+
91+ testTxInPreservesOrder :: L. TxId -> L. TxIx -> L. TxIx -> Property
92+ testTxInPreservesOrder txid txix1 txix2 =
93+ let b1 = packByteString (L. TxIn txid txix1)
94+ b2 = packByteString (L. TxIn txid txix2)
95+ in counterexample (show b1 <> " " <> show b2) $ compare b1 b2 === compare txix1 txix2
0 commit comments