packages feed

raft-consensus-serialise-instances (empty) → 0.2.0.0

raw patch · 11 files changed

+760/−0 lines, 11 filesdep +QuickCheckdep +basedep +bytestring

Dependencies added: QuickCheck, base, bytestring, raft-consensus, raft-consensus-serialise-instances, serialise, tasty, tasty-quickcheck

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for raft-consensus-serialise-instances++## 0.2.0.0++* First version is 0.2.0.0 since it supports `raft-consensus-0.2.0.0`
+ LICENSE view
@@ -0,0 +1,29 @@+Copyright (c) 2026, Laurent P. René de Cotret+++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of the copyright holder nor the names of its+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,7 @@+# raft-consensus-serialise-instances++`Serialise` instances, from the [`serialise`](https://hackage.haskell.org/package/serialise) package, for the types of the `raft-consensus` library.++```haskell+import Distributed.Consensus.Raft.Serialise.Instances ()+```
+ raft-consensus-serialise-instances.cabal view
@@ -0,0 +1,76 @@+cabal-version:      3.4+name:               raft-consensus-serialise-instances+-- Matching the major version of the supported 'raft-consnsus'+version:            0.2.0.0+synopsis:           Serialise instances for the raft-consensus library+-- description:+homepage:           https://github.com/LaurentRDC/distributed-consensus+license:            BSD-3-Clause+license-file:       LICENSE+author:             Laurent P. René de Cotret+maintainer:         laurent.decotret@outlook.com+copyright:          Laurent P. René de Cotret+category:           Distributed Systems, Consensus+build-type:         Simple+extra-doc-files:    CHANGELOG.md+                    README.md++source-repository head+    type: git+    location: https://github.com/LaurentRDC/distributed-consensus+    subdir: raft-consensus-serialise-instances++common warnings+    ghc-options: -Wall+                 -Wcompat+                 -Widentities+                 -Wincomplete-uni-patterns+                 -Wincomplete-record-updates+                 -Wredundant-constraints+                 -fhide-source-paths+                 -Wpartial-fields+                 -Wunused-packages++library+    import:           warnings+    exposed-modules:  Distributed.Consensus.Raft.Serialise.Instances+    hs-source-dirs:   src+    default-language: Haskell2010+    build-depends:    base >= 4.18 && <4.23+                    , raft-consensus ^>=0.2+                    , serialise ^>=0.2++-- The following "test suite" only exists to ensure that all types+-- required to implement a server and clients are exported.+--+-- If it compiles, we're good+test-suite rcsi-interface-test+    import:           warnings+    default-language: Haskell2010+    type:             exitcode-stdio-1.0+    ghc-options:      -rtsopts+    hs-source-dirs:   tests+    main-is:          Interface/Main.hs+    other-modules:    Interface.Admin+                      Interface.Client+                      Interface.Server+    build-depends:    base >= 4.18 && <4.23+                    , bytestring+                    , raft-consensus+                    , raft-consensus-serialise-instances+                    , serialise ^>=0.2+test-suite rcsi-roundtrip-test+    import:           warnings+    default-language: Haskell2010+    type:             exitcode-stdio-1.0+    ghc-options:      -rtsopts+    hs-source-dirs:   tests+    main-is:          RoundTrip/Main.hs+    other-modules:    RoundTrip.Arbitrary+    build-depends:    base >= 4.18 && <4.23+                    , QuickCheck+                    , raft-consensus+                    , raft-consensus-serialise-instances+                    , serialise ^>=0.2+                    , tasty ^>=1.5+                    , tasty-quickcheck ^>=0.11
+ src/Distributed/Consensus/Raft/Serialise/Instances.hs view
@@ -0,0 +1,321 @@+{-# LANGUAGE LambdaCase #-}+{-# OPTIONS_GHC -Wno-orphans #-}++-- |+-- Module:      $module+-- Copyright:   (c) Laurent P. René de Cotret+-- License:     BSD 3-Clause+--+-- 'Serialise' instances for the types of "Distributed.Consensus.Raft".+--+-- This module exports nothing but instances; import it with @import Distributed.Consensus.Raft.Serialise.Instances ()@.+--+-- Encodings are written by hand rather than derived through 'GHC.Generics.Generic',+-- so that they do not change when constructors or fields are reordered.+module Distributed.Consensus.Raft.Serialise.Instances () where++import Codec.Serialise (Serialise (..))+import Codec.Serialise.Decoding (Decoder, decodeListLen, decodeListLenOf, decodeWord, decodeWord64)+import Codec.Serialise.Encoding (Encoding, encodeListLen, encodeWord, encodeWord64)+import Distributed.Consensus.Raft+  ( AdminCommand (..),+    AdminCommandResult (..),+    AdminRequestId (..),+    AppendEntries (..),+    AppendEntriesResult (..),+    ClientRequestId (..),+    ClusterConfiguration (..),+    ClusterMembershipError (..),+    ClusterMembershipRequest (..),+    ClusterMembershipResult (..),+    Command (..),+    CommandResponse (..),+    InstallSnapshot (..),+    InstallSnapshotResult (..),+    LogEntry (..),+    LogIndex,+    RPC (..),+    RPCResult (..),+    ReadOnlySequenceNum,+    Request (..),+    RequestPayload (..),+    RequestVote (..),+    RequestVoteResult (..),+    Response (..),+    ResponsePayload (..),+    Role (..),+    SequenceNum,+    SessionId (..),+    Snapshot (..),+    SnapshotMetadata (..),+    Term,+  )++instance Serialise Term where+  encode = encodeIntegral+  decode = decodeIntegral++instance Serialise LogIndex where+  encode = encodeIntegral+  decode = decodeIntegral++instance Serialise SequenceNum where+  encode = encodeIntegral+  decode = decodeIntegral++instance Serialise ReadOnlySequenceNum where+  encode = encodeIntegral+  decode = decodeIntegral++instance Serialise Role where+  encode = \case+    Leader -> variant 0 []+    Follower -> variant 1 []+    Candidate -> variant 2 []+    NonMember -> variant 3 []+  decode = decodeVariant $ \case+    0 -> pure Leader+    1 -> pure Follower+    2 -> pure Candidate+    3 -> pure NonMember+    t -> unknownTag t++instance Serialise SessionId where+  encode (SessionId hi lo) = fields [encodeWord64 hi, encodeWord64 lo]+  decode = decodeFields 2 (SessionId <$> decodeWord64 <*> decodeWord64)++instance Serialise ClientRequestId where+  encode (ClientRequestId s n) = fields [encode s, encode n]+  decode = decodeFields 2 (ClientRequestId <$> decode <*> decode)++instance Serialise AdminRequestId where+  encode (AdminRequestId s n) = fields [encode s, encode n]+  decode = decodeFields 2 (AdminRequestId <$> decode <*> decode)++instance (Serialise p) => Serialise (RequestPayload p) where+  encode = \case+    Heartbeat -> variant 0 []+    RequestPayload p -> variant 1 [encode p]+  decode = decodeVariant $ \case+    0 -> pure Heartbeat+    1 -> RequestPayload <$> decode+    t -> unknownTag t++instance (Serialise r) => Serialise (ResponsePayload r) where+  encode = \case+    HeartbeatEcho -> variant 0 []+    NotLeader -> variant 1 []+    SessionExpired -> variant 2 []+    ResponsePayload r -> variant 3 [encode r]+  decode = decodeVariant $ \case+    0 -> pure HeartbeatEcho+    1 -> pure NotLeader+    2 -> pure SessionExpired+    3 -> ResponsePayload <$> decode+    t -> unknownTag t++instance (Serialise i, Serialise n, Serialise p) => Serialise (Request i n p) where+  encode r =+    fields+      [ encode (requestId r),+        encode (requestOriginator r),+        encode (requestOldestOutstanding r),+        encode (requestPayload r)+      ]+  decode = decodeFields 4 (Request <$> decode <*> decode <*> decode <*> decode)++instance (Serialise i, Serialise n, Serialise r) => Serialise (Response i n r) where+  encode r =+    fields+      [ encode (responseRequestId r),+        encode (responseClusterLeader r),+        encode (responsePayload r)+      ]+  decode = decodeFields 3 (Response <$> decode <*> decode <*> decode)++instance (Serialise node) => Serialise (AdminCommand node) where+  encode = \case+    JoinCluster n -> variant 0 [encode n]+    LeaveCluster -> variant 1 []+    GetClusterConfiguration -> variant 2 []+    ShutDown -> variant 3 []+  decode = decodeVariant $ \case+    0 -> JoinCluster <$> decode+    1 -> pure LeaveCluster+    2 -> pure GetClusterConfiguration+    3 -> pure ShutDown+    t -> unknownTag t++instance (Ord node, Serialise node) => Serialise (AdminCommandResult node) where+  encode = \case+    JoinInitiated -> variant 0 []+    LeaveInitiated -> variant 1 []+    ShutdownInitiated -> variant 2 []+    ClusterConfigurationIs c -> variant 3 [encode c]+  decode = decodeVariant $ \case+    0 -> pure JoinInitiated+    1 -> pure LeaveInitiated+    2 -> pure ShutdownInitiated+    3 -> ClusterConfigurationIs <$> decode+    t -> unknownTag t++instance (Ord node, Serialise node) => Serialise (ClusterConfiguration node) where+  encode = \case+    Simple s -> variant 0 [encode s]+    Joint a b -> variant 1 [encode a, encode b]+  decode = decodeVariant $ \case+    0 -> Simple <$> decode+    1 -> Joint <$> decode <*> decode+    t -> unknownTag t++instance Serialise SnapshotMetadata where+  encode m = fields [encode (smLastIncludedIndex m), encode (smLastIncludedTerm m)]+  decode = decodeFields 2 (SnapshotMetadata <$> decode <*> decode)++instance (Ord node, Serialise node, Serialise state) => Serialise (Snapshot node state) where+  encode s = fields [encode (sMetadata s), encode (sData s), encode (sCluster s)]+  decode = decodeFields 3 (Snapshot <$> decode <*> decode <*> decode)++instance (Serialise entry) => Serialise (Command entry) where+  encode (Command rid e) = fields [encode rid, encode e]+  decode = decodeFields 2 (Command <$> decode <*> decode)++instance (Serialise result) => Serialise (CommandResponse node result) where+  encode (CommandResponse r rid) = fields [encode r, encode rid]+  decode = decodeFields 2 (CommandResponse <$> decode <*> decode)++instance (Ord node, Serialise node, Serialise entry) => Serialise (LogEntry entry node) where+  encode = \case+    LogEntryCommand c -> variant 0 [encode c]+    LogEntryLeaderChange -> variant 1 []+    LogEntryMembershipChange c -> variant 2 [encode c]+  decode = decodeVariant $ \case+    0 -> LogEntryCommand <$> decode+    1 -> pure LogEntryLeaderChange+    2 -> LogEntryMembershipChange <$> decode+    t -> unknownTag t++instance (Ord node, Serialise node, Serialise entry) => Serialise (AppendEntries entry node) where+  encode m =+    fields+      [ encode (aeLeaderTerm m),+        encode (aeLeaderNode m),+        encode (aePreviousLogIndex m),+        encode (aePreviousLogTerm m),+        encode (aeEntries m),+        encode (aeCommitIndex m),+        encode (aeReadOnlySeqNum m)+      ]+  decode =+    decodeFields 7 $+      AppendEntries <$> decode <*> decode <*> decode <*> decode <*> decode <*> decode <*> decode++instance (Serialise node) => Serialise (AppendEntriesResult node result) where+  encode m =+    fields+      [ encode (aerCurrentTerm m),+        encode (aerNode m),+        encode (aerMatch m),+        encode (aerNewEntryLogIndex m),+        encode (aerPrevLogIndex m),+        encode (aerReadOnlySeqNum m)+      ]+  decode =+    decodeFields 6 $+      AppendEntriesResult <$> decode <*> decode <*> decode <*> decode <*> decode <*> decode++instance (Ord node, Serialise node, Serialise state) => Serialise (InstallSnapshot node state) where+  encode m = fields [encode (isLeaderTerm m), encode (isLeaderNode m), encode (isSnapshot m)]+  decode = decodeFields 3 (InstallSnapshot <$> decode <*> decode <*> decode)++instance (Serialise node) => Serialise (InstallSnapshotResult node) where+  encode m = fields [encode (isrTerm m), encode (isrNode m), encode (isrSnapshotMetadata m)]+  decode = decodeFields 3 (InstallSnapshotResult <$> decode <*> decode <*> decode)++instance (Serialise node) => Serialise (ClusterMembershipRequest node) where+  encode = \case+    ClusterMembershipJoinRequest n -> variant 0 [encode n]+    ClusterMembershipLeaveRequest n -> variant 1 [encode n]+  decode = decodeVariant $ \case+    0 -> ClusterMembershipJoinRequest <$> decode+    1 -> ClusterMembershipLeaveRequest <$> decode+    t -> unknownTag t++instance (Serialise node) => Serialise (ClusterMembershipError node) where+  encode = \case+    ClusterMembershipNoKnownLeader n -> variant 0 [encode n]+    OngoingClusterMembershipChange n -> variant 1 [encode n]+  decode = decodeVariant $ \case+    0 -> ClusterMembershipNoKnownLeader <$> decode+    1 -> OngoingClusterMembershipChange <$> decode+    t -> unknownTag t++instance (Serialise node) => Serialise (ClusterMembershipResult node) where+  encode = \case+    ClusterMembershipJoinResult r -> variant 0 [encode r]+    ClusterMembershipLeaveResult r -> variant 1 [encode r]+  decode = decodeVariant $ \case+    0 -> ClusterMembershipJoinResult <$> decode+    1 -> ClusterMembershipLeaveResult <$> decode+    t -> unknownTag t++instance (Serialise node) => Serialise (RequestVote node) where+  encode m =+    fields+      [ encode (rvCandidateTerm m),+        encode (rvCandidateNode m),+        encode (rvCandidateLastLogEntryIndex m),+        encode (rvCandidateLastLogEntryTerm m)+      ]+  decode = decodeFields 4 (RequestVote <$> decode <*> decode <*> decode <*> decode)++instance (Serialise node) => Serialise (RequestVoteResult node) where+  encode m = fields [encode (rvrVoterNode m), encode (rvrVoterTerm m), encode (rvrVoteGranted m)]+  decode = decodeFields 3 (RequestVoteResult <$> decode <*> decode <*> decode)++instance (Ord node, Serialise node, Serialise entry, Serialise state) => Serialise (RPC entry node state) where+  encode = \case+    AE m -> variant 0 [encode m]+    IS m -> variant 1 [encode m]+    CM m -> variant 2 [encode m]+    RV m -> variant 3 [encode m]+  decode = decodeVariant $ \case+    0 -> AE <$> decode+    1 -> IS <$> decode+    2 -> CM <$> decode+    3 -> RV <$> decode+    t -> unknownTag t++instance (Serialise node) => Serialise (RPCResult node result) where+  encode = \case+    AER m -> variant 0 [encode m]+    ISR m -> variant 1 [encode m]+    CMR m -> variant 2 [encode m]+    RVR m -> variant 3 [encode m]+  decode = decodeVariant $ \case+    0 -> AER <$> decode+    1 -> ISR <$> decode+    2 -> CMR <$> decode+    3 -> RVR <$> decode+    t -> unknownTag t++fields :: [Encoding] -> Encoding+fields xs = encodeListLen (fromIntegral (length xs)) <> mconcat xs++decodeFields :: Int -> Decoder s a -> Decoder s a+decodeFields n d = decodeListLenOf n *> d++variant :: Word -> [Encoding] -> Encoding+variant tag xs = fields (encodeWord tag : xs)++decodeVariant :: (Word -> Decoder s a) -> Decoder s a+decodeVariant f = decodeListLen *> decodeWord >>= f++unknownTag :: Word -> Decoder s a+unknownTag tag = fail ("unknown tag " <> show tag)++encodeIntegral :: (Integral a) => a -> Encoding+encodeIntegral = encodeWord64 . fromIntegral++decodeIntegral :: (Integral a) => Decoder s a+decodeIntegral = fromIntegral <$> decodeWord64
+ tests/Interface/Admin.hs view
@@ -0,0 +1,18 @@+module Interface.Admin+  ( implementation,+  )+where++import Codec.Serialise (deserialise, serialise)+import qualified Data.ByteString.Lazy as BSL+import Data.Functor ((<&>))+import Distributed.Consensus.Raft.Admin (AdminImplementation (..), randomSessionId)+import Interface.Server (Node)++implementation :: AdminImplementation Node IO+implementation =+  AdminImplementation+    { sendAdminRequest = \_ -> BSL.putStr . serialise,+      receiveAdminResponse = BSL.getContents <&> deserialise,+      generateAdminSessionId = randomSessionId+    }
+ tests/Interface/Client.hs view
@@ -0,0 +1,18 @@+module Interface.Client+  ( implementation,+  )+where++import Codec.Serialise (deserialise, serialise)+import qualified Data.ByteString.Lazy as BSL+import Data.Functor ((<&>))+import Distributed.Consensus.Raft.Client (ClientImplementation (..), randomSessionId)+import Interface.Server (CounterOp, Node, Result)++implementation :: ClientImplementation CounterOp Node Result IO+implementation =+  ClientImplementation+    { sendClientRequest = \_ -> BSL.putStr . serialise,+      receiveClientResponse = BSL.getContents <&> deserialise,+      generateClientSessionId = randomSessionId+    }
+ tests/Interface/Main.hs view
@@ -0,0 +1,5 @@+-- | This suite only needs to compile: see the other modules in this directory.+module Main (main) where++main :: IO ()+main = pure ()
+ tests/Interface/Server.hs view
@@ -0,0 +1,75 @@+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE LambdaCase #-}++-- This module exists for a single purpose: to show that all of the+-- types required to write an 'Implementation' are exported by+-- "Distributed.Consensus.Raft".+--+-- To ensure this, we implement de/serialisation via the+-- @serialise@ package, using the instances from this package.+module Interface.Server+  ( implementation,+    CounterOp (..),+    Node,+    Result,+  )+where++import Codec.Serialise (Serialise, deserialise, serialise)+import qualified Data.ByteString.Lazy as BSL+import Data.Functor ((<&>))+import Distributed.Consensus.Raft+  ( Implementation (..),+    Networking (..),+    Persistence (..),+    StateMachineStep (..),+    nullTracer,+  )+import Distributed.Consensus.Raft.Serialise.Instances ()+import GHC.Generics (Generic)++type State = Int++type Result = Int++type Node = Int++data CounterOp = ReadCounter | Increment | Decrement+  deriving (Generic)++instance Serialise CounterOp++implementation :: Implementation CounterOp Node Result State IO+implementation =+  Implementation+    { persistence =+        Persistence+          { readLogEntriesFrom = \_ _ -> BSL.getContents <&> deserialise,+            writeLogEntry = \_ -> BSL.putStr . serialise,+            truncateLogEntriesFrom = \_ _ -> pure (),+            readTerm = \_ -> BSL.getContents <&> deserialise,+            writeTerm = \_ -> BSL.putStr . serialise,+            readVotedFor = \_ _ -> BSL.getContents <&> deserialise,+            writeVotedFor = \_ _ _ -> pure (),+            readSnapshot = \_ -> BSL.getContents <&> deserialise,+            writeSnapshot = \_ -> BSL.putStr . serialise,+            readCommitIndex = \_ -> BSL.getContents <&> deserialise,+            writeCommitIndex = \_ _ -> pure ()+          },+      stateMachine = \case+        ReadCounter -> Read id+        Increment -> Transition $ \st -> (succ st, succ st)+        Decrement -> Transition $ \st -> (pred st, pred st),+      networking =+        Networking+          { sendRPC = \_ -> BSL.putStr . serialise,+            sendRPCResult = \_ -> BSL.putStr . serialise,+            sendClientResponse = \_ -> BSL.putStr . serialise,+            sendAdminResponse = \_ -> BSL.putStr . serialise,+            receiveRPC = BSL.getContents <&> deserialise,+            receiveRPCResult = BSL.getContents <&> deserialise,+            receiveClientRequests = BSL.getContents <&> deserialise,+            receiveAdminRequest = BSL.getContents <&> deserialise+          },+      tracer = nullTracer+    }
+ tests/RoundTrip/Arbitrary.hs view
@@ -0,0 +1,149 @@+{-# LANGUAGE DerivingVia #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE StandaloneDeriving #-}+{-# LANGUAGE TypeApplications #-}+{-# LANGUAGE TypeOperators #-}+{-# LANGUAGE UndecidableInstances #-}+{-# OPTIONS_GHC -Wno-orphans #-}++module RoundTrip.Arbitrary () where++import Data.Proxy (Proxy (..))+import Data.Word (Word64)+import Distributed.Consensus.Raft+  ( AdminCommand (..),+    AdminCommandResult (..),+    AdminRequestId (..),+    AppendEntries (..),+    AppendEntriesResult (..),+    ClientRequestId (..),+    ClusterConfiguration (..),+    ClusterMembershipError (..),+    ClusterMembershipRequest (..),+    ClusterMembershipResult (..),+    Command (..),+    CommandResponse (..),+    InstallSnapshot (..),+    InstallSnapshotResult (..),+    LogEntry (..),+    LogIndex,+    RPC (..),+    RPCResult (..),+    ReadOnlySequenceNum,+    Request (..),+    RequestPayload (..),+    RequestVote (..),+    RequestVoteResult (..),+    Response (..),+    ResponsePayload (..),+    Role (..),+    SequenceNum,+    SessionId (..),+    Snapshot (..),+    SnapshotMetadata (..),+    Term,+  )+import GHC.Generics (Generic, K1 (..), M1 (..), Rep, U1 (..), to, (:*:) (..), (:+:) (..))+import Test.QuickCheck (Arbitrary (..), Gen, frequency)++word :: (Num a) => Gen a+word = fromIntegral <$> (arbitrary :: Gen Word64)++-- Why can't QuickCheck provide this already ?!+newtype GenericArbitrary a = GenericArbitrary a++instance (Generic a, GArbitrary (Rep a)) => Arbitrary (GenericArbitrary a) where+  arbitrary = GenericArbitrary . to <$> garbitrary++class GArbitrary f where+  garbitrary :: Gen (f p)+  constructors :: Proxy f -> Int++instance GArbitrary U1 where+  garbitrary = pure U1+  constructors _ = 1++instance (Arbitrary c) => GArbitrary (K1 i c) where+  garbitrary = K1 <$> arbitrary+  constructors _ = 1++instance (GArbitrary f) => GArbitrary (M1 i t f) where+  garbitrary = M1 <$> garbitrary+  constructors _ = constructors (Proxy @f)++instance (GArbitrary f, GArbitrary g) => GArbitrary (f :*: g) where+  garbitrary = (:*:) <$> garbitrary <*> garbitrary+  constructors _ = 1++instance (GArbitrary f, GArbitrary g) => GArbitrary (f :+: g) where+  garbitrary =+    frequency+      [ (constructors (Proxy @f), L1 <$> garbitrary),+        (constructors (Proxy @g), R1 <$> garbitrary)+      ]+  constructors _ = constructors (Proxy @f) + constructors (Proxy @g)++deriving via GenericArbitrary Term instance Arbitrary Term++deriving via GenericArbitrary LogIndex instance Arbitrary LogIndex++instance Arbitrary SequenceNum where+  arbitrary = word++instance Arbitrary ReadOnlySequenceNum where+  arbitrary = word++deriving via GenericArbitrary Role instance Arbitrary Role++deriving via GenericArbitrary SessionId instance Arbitrary SessionId++deriving via GenericArbitrary ClientRequestId instance Arbitrary ClientRequestId++deriving via GenericArbitrary AdminRequestId instance Arbitrary AdminRequestId++deriving via GenericArbitrary (RequestPayload p) instance (Arbitrary p) => Arbitrary (RequestPayload p)++deriving via GenericArbitrary (ResponsePayload r) instance (Arbitrary r) => Arbitrary (ResponsePayload r)++deriving via GenericArbitrary (Request i n p) instance (Arbitrary i, Arbitrary n, Arbitrary p) => Arbitrary (Request i n p)++deriving via GenericArbitrary (Response i n r) instance (Arbitrary i, Arbitrary n, Arbitrary r) => Arbitrary (Response i n r)++deriving via GenericArbitrary (AdminCommand node) instance (Arbitrary node) => Arbitrary (AdminCommand node)++deriving via GenericArbitrary (AdminCommandResult node) instance (Ord node, Arbitrary node) => Arbitrary (AdminCommandResult node)++deriving via GenericArbitrary (ClusterConfiguration node) instance (Ord node, Arbitrary node) => Arbitrary (ClusterConfiguration node)++deriving via GenericArbitrary SnapshotMetadata instance Arbitrary SnapshotMetadata++deriving via GenericArbitrary (Snapshot node state) instance (Ord node, Arbitrary node, Arbitrary state) => Arbitrary (Snapshot node state)++deriving via GenericArbitrary (Command entry) instance (Arbitrary entry) => Arbitrary (Command entry)++deriving via GenericArbitrary (CommandResponse node result) instance (Arbitrary result) => Arbitrary (CommandResponse node result)++deriving via GenericArbitrary (LogEntry entry node) instance (Ord node, Arbitrary node, Arbitrary entry) => Arbitrary (LogEntry entry node)++deriving via GenericArbitrary (AppendEntries entry node) instance (Ord node, Arbitrary node, Arbitrary entry) => Arbitrary (AppendEntries entry node)++deriving via GenericArbitrary (AppendEntriesResult node result) instance (Arbitrary node) => Arbitrary (AppendEntriesResult node result)++deriving via GenericArbitrary (InstallSnapshot node state) instance (Ord node, Arbitrary node, Arbitrary state) => Arbitrary (InstallSnapshot node state)++deriving via GenericArbitrary (InstallSnapshotResult node) instance (Arbitrary node) => Arbitrary (InstallSnapshotResult node)++deriving via GenericArbitrary (ClusterMembershipRequest node) instance (Arbitrary node) => Arbitrary (ClusterMembershipRequest node)++deriving via GenericArbitrary (ClusterMembershipError node) instance (Arbitrary node) => Arbitrary (ClusterMembershipError node)++deriving via GenericArbitrary (ClusterMembershipResult node) instance (Arbitrary node) => Arbitrary (ClusterMembershipResult node)++deriving via GenericArbitrary (RequestVote node) instance (Arbitrary node) => Arbitrary (RequestVote node)++deriving via GenericArbitrary (RequestVoteResult node) instance (Arbitrary node) => Arbitrary (RequestVoteResult node)++deriving via GenericArbitrary (RPC entry node state) instance (Ord node, Arbitrary node, Arbitrary entry, Arbitrary state) => Arbitrary (RPC entry node state)++deriving via GenericArbitrary (RPCResult node result) instance (Arbitrary node) => Arbitrary (RPCResult node result)
+ tests/RoundTrip/Main.hs view
@@ -0,0 +1,57 @@+{-# LANGUAGE AllowAmbiguousTypes #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeApplications #-}++module Main (main) where++import Codec.Serialise (Serialise, deserialiseOrFail, serialise)+import Data.Bifunctor (first)+import Distributed.Consensus.Raft+import Distributed.Consensus.Raft.Serialise.Instances ()+import RoundTrip.Arbitrary ()+import Test.Tasty (TestTree, defaultMain, testGroup)+import Test.Tasty.QuickCheck (Arbitrary, testProperty, (===))++type N = Int++main :: IO ()+main =+  defaultMain $+    testGroup+      "Serialise round-trips"+      [ roundTrip @Term "Term",+        roundTrip @LogIndex "LogIndex",+        roundTrip @SequenceNum "SequenceNum",+        roundTrip @ReadOnlySequenceNum "ReadOnlySequenceNum",+        roundTrip @Role "Role",+        roundTrip @SessionId "SessionId",+        roundTrip @ClientRequestId "ClientRequestId",+        roundTrip @AdminRequestId "AdminRequestId",+        roundTrip @(RequestPayload Int) "RequestPayload",+        roundTrip @(ResponsePayload Int) "ResponsePayload",+        roundTrip @(Request ClientRequestId N Int) "Request",+        roundTrip @(Response ClientRequestId N Int) "Response",+        roundTrip @(AdminCommand N) "AdminCommand",+        roundTrip @(AdminCommandResult N) "AdminCommandResult",+        roundTrip @(ClusterConfiguration N) "ClusterConfiguration",+        roundTrip @SnapshotMetadata "SnapshotMetadata",+        roundTrip @(Snapshot N Int) "Snapshot",+        roundTrip @(Command Int) "Command",+        roundTrip @(CommandResponse N Int) "CommandResponse",+        roundTrip @(LogEntry Int N) "LogEntry",+        roundTrip @(AppendEntries Int N) "AppendEntries",+        roundTrip @(AppendEntriesResult N Int) "AppendEntriesResult",+        roundTrip @(InstallSnapshot N Int) "InstallSnapshot",+        roundTrip @(InstallSnapshotResult N) "InstallSnapshotResult",+        roundTrip @(ClusterMembershipRequest N) "ClusterMembershipRequest",+        roundTrip @(ClusterMembershipError N) "ClusterMembershipError",+        roundTrip @(ClusterMembershipResult N) "ClusterMembershipResult",+        roundTrip @(RequestVote N) "RequestVote",+        roundTrip @(RequestVoteResult N) "RequestVoteResult",+        roundTrip @(RPC Int N Int) "RPC",+        roundTrip @(RPCResult N Int) "RPCResult"+      ]++roundTrip :: forall a. (Arbitrary a, Eq a, Show a, Serialise a) => String -> TestTree+roundTrip name = testProperty name $ \(x :: a) ->+  first show (deserialiseOrFail (serialise x)) === Right x