-
Notifications
You must be signed in to change notification settings - Fork 32
Add NodeToNode Codec for PerasCertDiffusion #1658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: peras-staging
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
@@ -16,6 +17,9 @@ module Ouroboros.Consensus.Block.SupportsPeras | |
, PerasCert (..) | ||
) where | ||
|
||
import Codec.Serialise (Serialise (..)) | ||
import Codec.Serialise.Decoding (decodeListLenOf) | ||
import Codec.Serialise.Encoding (encodeListLen) | ||
import Data.Monoid (Sum (..)) | ||
import Data.Word (Word64) | ||
import GHC.Generics (Generic) | ||
|
@@ -27,7 +31,7 @@ import Quiet (Quiet (..)) | |
newtype PerasRoundNo = PerasRoundNo {unPerasRoundNo :: Word64} | ||
deriving Show via Quiet PerasRoundNo | ||
deriving stock Generic | ||
deriving newtype (Eq, Ord, NoThunks) | ||
deriving newtype (Eq, Ord, NoThunks, Serialise) | ||
|
||
instance Condense PerasRoundNo where | ||
condense = show . unPerasRoundNo | ||
|
@@ -66,3 +70,14 @@ instance StandardHash blk => BlockSupportsPeras blk where | |
|
||
perasCertRound = pcCertRound | ||
perasCertBoostedBlock = pcCertBoostedBlock | ||
|
||
instance Serialise (HeaderHash blk) => Serialise (PerasCert blk) where | ||
encode PerasCert{..} = | ||
encodeListLen 2 | ||
<> encode pcCertRound | ||
<> encode pcCertBoostedBlock | ||
decode = do | ||
decodeListLenOf 2 | ||
pcCertRound <- decode | ||
pcCertBoostedBlock <- decode | ||
pure $ PerasCert pcCertRound pcCertBoostedBlock | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: I think it is somewhat preferable to use |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,8 @@ | |||||
{-# LANGUAGE MultiParamTypeClasses #-} | ||||||
{-# LANGUAGE PolyKinds #-} | ||||||
{-# LANGUAGE RankNTypes #-} | ||||||
{-# LANGUAGE RecordWildCards #-} | ||||||
{-# LANGUAGE ScopedTypeVariables #-} | ||||||
{-# LANGUAGE StandaloneDeriving #-} | ||||||
{-# LANGUAGE StandaloneKindSignatures #-} | ||||||
{-# LANGUAGE UndecidableInstances #-} | ||||||
|
@@ -33,8 +35,8 @@ module Ouroboros.Consensus.Node.Serialisation | |||||
, Some (..) | ||||||
) where | ||||||
|
||||||
import Codec.CBOR.Decoding (Decoder) | ||||||
import Codec.CBOR.Encoding (Encoding) | ||||||
import Codec.CBOR.Decoding (Decoder, decodeListLenOf) | ||||||
import Codec.CBOR.Encoding (Encoding, encodeListLen) | ||||||
import Codec.Serialise (Serialise (decode, encode)) | ||||||
import Data.Kind | ||||||
import Data.SOP.BasicFunctors | ||||||
|
@@ -47,7 +49,15 @@ import Ouroboros.Consensus.Ledger.SupportsMempool | |||||
import Ouroboros.Consensus.Node.NetworkProtocolVersion | ||||||
import Ouroboros.Consensus.TypeFamilyWrappers | ||||||
import Ouroboros.Consensus.Util (Some (..)) | ||||||
import Ouroboros.Network.Block (unwrapCBORinCBOR, wrapCBORinCBOR) | ||||||
import Ouroboros.Network.Block | ||||||
( Tip | ||||||
, decodePoint | ||||||
, decodeTip | ||||||
, encodePoint | ||||||
, encodeTip | ||||||
, unwrapCBORinCBOR | ||||||
, wrapCBORinCBOR | ||||||
) | ||||||
|
||||||
{------------------------------------------------------------------------------- | ||||||
NodeToNode | ||||||
|
@@ -173,6 +183,30 @@ deriving newtype instance | |||||
SerialiseNodeToNode blk (GenTxId blk) => | ||||||
SerialiseNodeToNode blk (WrapGenTxId blk) | ||||||
|
||||||
-- TODO: move these orphan instances elsewhere | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, if they are defined here, they actually aren't orphan instances (note that the orphan warning isn't disabled in this file), so this can be removed. |
||||||
instance ConvertRawHash blk => SerialiseNodeToNode blk (Point blk) where | ||||||
encodeNodeToNode _ccfg _version = encodePoint $ encodeRawHash (Proxy :: Proxy blk) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick:
Suggested change
Also search for " |
||||||
decodeNodeToNode _ccfg _version = decodePoint $ decodeRawHash (Proxy :: Proxy blk) | ||||||
|
||||||
instance ConvertRawHash blk => SerialiseNodeToNode blk (Tip blk) where | ||||||
encodeNodeToNode _ccfg _version = encodeTip $ encodeRawHash (Proxy :: Proxy blk) | ||||||
decodeNodeToNode _ccfg _version = decodeTip $ decodeRawHash (Proxy :: Proxy blk) | ||||||
|
||||||
instance SerialiseNodeToNode blk PerasRoundNo where | ||||||
encodeNodeToNode _ccfg _version = encode | ||||||
decodeNodeToNode _ccfg _version = decode | ||||||
instance ConvertRawHash blk => SerialiseNodeToNode blk (PerasCert blk) where | ||||||
-- Consistent with the 'Serialise' instance for 'PerasCert' defined in Ouroboros.Consensus.Block.SupportsPeras | ||||||
encodeNodeToNode ccfg version PerasCert{..} = | ||||||
encodeListLen 2 | ||||||
<> encodeNodeToNode ccfg version pcCertRound | ||||||
<> encodeNodeToNode ccfg version pcCertBoostedBlock | ||||||
decodeNodeToNode ccfg version = do | ||||||
decodeListLenOf 2 | ||||||
pcCertRound <- decodeNodeToNode ccfg version | ||||||
pcCertBoostedBlock <- decodeNodeToNode ccfg version | ||||||
pure $ PerasCert pcCertRound pcCertBoostedBlock | ||||||
Comment on lines
+198
to
+208
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also just defer to |
||||||
|
||||||
deriving newtype instance | ||||||
SerialiseNodeToClient blk (GenTxId blk) => | ||||||
SerialiseNodeToClient blk (WrapGenTxId blk) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: The style guide asks us to not use
RecordWildCards
. (I personally like them though 😅 And there are still lots of usages left from before this rule existed.)