Skip to content

Commit

Permalink
Use any in cddl spec
Browse files Browse the repository at this point in the history
`cddl` comes with `any` definition, it can be used to indicate that the
data structures in our spec are not specified in `ouroboros-network`.

This patch only introduces it in `TxSubmission` mini-protocol, but we
should do that for other mini-protocols too.

In this patch we modified the definition of `Tx` and `TxId` used in
`ouroboros-network-protocols:test` as well as
`ouroboros-network-protocols:cddl` components.
  • Loading branch information
coot committed Jun 13, 2023
1 parent 367755f commit 43586d7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
8 changes: 8 additions & 0 deletions ouroboros-network-protocols/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Revision history for ouroboros-network-protocols

## next version

### Breaking changes

* Definition of `TxId` has changed, it's now a newtype wrapper for
`Codec.CBOR.Term.Term` type, which indicates that `ouroboros-network` does
not specify what `TxId` or `Tx` types are.

## 0.5.0.3 -- 2023-05-26

* `ghc-9.6` compatibility
Expand Down
9 changes: 5 additions & 4 deletions ouroboros-network-protocols/test-cddl/specs/common.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ origin = []
blockHeaderHash = [slotNo, int]
slotNo = word64

; In `ouroboros-network` we don't know what are tx (and txId), we mock them
; with `int`s, that's because `ouroboros-network` is polymorphic over them.
txId = int
tx = int
; In this spec we don't specify what transaction idenfiers (txId) or
; transactions (tx) are themselves, `ouroboros-network` is polymorphic over
; them.
txId = any
tx = any
rejectReason = int

word16 = 0..65535
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Control.Monad.IOSim
import Control.Monad.ST (runST)
import Control.Tracer (Tracer (..), nullTracer)

import qualified Codec.CBOR.Term as CBOR
import Codec.Serialise (DeserialiseFailure, Serialise)
import qualified Codec.Serialise as Serialise (decode, encode)

Expand All @@ -52,6 +53,7 @@ import Test.Ouroboros.Network.Testing.Utils (prop_codec_cborM,
prop_codec_valid_cbor_encoding, splits2, splits3)

import Test.QuickCheck as QC
import Test.QuickCheck.Instances.ByteString ()
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck (testProperty)

Expand Down Expand Up @@ -94,8 +96,22 @@ instance ShowProxy Tx where
txId :: Tx -> TxId
txId (Tx txid) = txid

newtype TxId = TxId Int
deriving (Eq, Ord, Show, Arbitrary, Serialise)
-- | We use any `CBOR.Term`. This allows us to use `any` in cddl specs.
--
newtype TxId = TxId CBOR.Term
deriving (Eq, Ord, Show, Serialise)

instance Arbitrary TxId where
arbitrary = oneof [ TxId . CBOR.TInt <$> arbitrary
, TxId . CBOR.TBytes <$> arbitrary
, TxId . CBOR.TBool <$> arbitrary
]
shrink (TxId term) =
case term of
CBOR.TInt a -> TxId . CBOR.TInt <$> shrink a
CBOR.TBytes a -> TxId . CBOR.TBytes <$> shrink a
CBOR.TBool a -> TxId . CBOR.TBool <$> shrink a
_ -> []

instance ShowProxy TxId where
showProxy _ = "TxId"
Expand Down

0 comments on commit 43586d7

Please sign in to comment.