Skip to content

Commit 8983b7b

Browse files
committed
Test flipped serialization for TxIn
1 parent e853d1a commit 8983b7b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

ouroboros-consensus-cardano/ouroboros-consensus-cardano.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ test-suite shelley-test
375375
containers,
376376
filepath,
377377
measures,
378+
mempack,
378379
microlens,
379380
ouroboros-consensus:{ouroboros-consensus, unstable-consensus-testlib},
380381
ouroboros-consensus-cardano,

ouroboros-consensus-cardano/test/shelley-test/Test/Consensus/Shelley/LedgerTables.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
{-# LANGUAGE DerivingStrategies #-}
12
{-# LANGUAGE FlexibleContexts #-}
23
{-# LANGUAGE FlexibleInstances #-}
4+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
35
{-# LANGUAGE ScopedTypeVariables #-}
6+
{-# LANGUAGE StandaloneDeriving #-}
47
{-# LANGUAGE TypeApplications #-}
58
{-# LANGUAGE TypeFamilies #-}
69
{-# LANGUAGE TypeOperators #-}
@@ -11,6 +14,9 @@
1114
module Test.Consensus.Shelley.LedgerTables (tests) where
1215

1316
import qualified Cardano.Ledger.Api.Era as L
17+
import qualified Cardano.Ledger.BaseTypes as L
18+
import qualified Cardano.Ledger.Shelley.API.Types as L
19+
import Data.MemPack
1420
import Data.Proxy
1521
import Data.SOP.BasicFunctors
1622
import Data.SOP.Constraint
@@ -29,12 +35,15 @@ import Test.Cardano.Ledger.Dijkstra.Arbitrary ()
2935
import Test.Consensus.Shelley.Generators ()
3036
import Test.Consensus.Shelley.MockCrypto (CanMock)
3137
import Test.LedgerTables
38+
import Test.QuickCheck
3239
import Test.Tasty
3340
import Test.Tasty.QuickCheck
3441

3542
tests :: TestTree
3643
tests =
3744
testGroup "LedgerTables"
45+
. (testProperty "Serializing BigEndianTxIn preserves order" testBigEndianTxInPreservesOrder :)
46+
. (testProperty "Serializing TxIn fails to preserve order" (expectFailure testTxInPreservesOrder) :)
3847
. hcollapse
3948
. hcmap (Proxy @TestLedgerTables) (K . f)
4049
$ (hpure Proxy :: NP Proxy (CardanoShelleyEras StandardCrypto))
@@ -74,3 +83,15 @@ instance
7483
Arbitrary (LedgerTables (LedgerState (ShelleyBlock proto era)) ValuesMK)
7584
where
7685
arbitrary = projectLedgerTables . unstowLedgerTables <$> arbitrary
86+
87+
testBigEndianTxInPreservesOrder :: L.TxId -> L.TxIx -> L.TxIx -> Property
88+
testBigEndianTxInPreservesOrder txid txix1 txix2 =
89+
let b1 = packByteString (BigEndianTxIn $ L.TxIn txid txix1)
90+
b2 = packByteString (BigEndianTxIn $ L.TxIn txid txix2)
91+
in counterexample (show b1 <> " " <> show b2) $ compare b1 b2 === compare txix1 txix2
92+
93+
testTxInPreservesOrder :: L.TxId -> L.TxIx -> L.TxIx -> Property
94+
testTxInPreservesOrder txid txix1 txix2 =
95+
let b1 = packByteString (L.TxIn txid txix1)
96+
b2 = packByteString (L.TxIn txid txix2)
97+
in counterexample (show b1 <> " " <> show b2) $ compare b1 b2 === compare txix1 txix2

0 commit comments

Comments
 (0)