Skip to content

Commit

Permalink
New node to client version
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Sep 13, 2021
1 parent 359210b commit 37f1757
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 55 deletions.
21 changes: 11 additions & 10 deletions ouroboros-consensus-cardano/src/Ouroboros/Consensus/Cardano/Node.hs
Expand Up @@ -374,18 +374,19 @@ instance CardanoHardForkConstraints c
]

supportedNodeToClientVersions _ = Map.fromList $
[ (NodeToClientV_1, CardanoNodeToClientVersion1)
, (NodeToClientV_2, CardanoNodeToClientVersion1)
, (NodeToClientV_3, CardanoNodeToClientVersion2)
, (NodeToClientV_4, CardanoNodeToClientVersion3)
, (NodeToClientV_5, CardanoNodeToClientVersion4)
, (NodeToClientV_6, CardanoNodeToClientVersion5)
, (NodeToClientV_7, CardanoNodeToClientVersion6)
, (NodeToClientV_8, CardanoNodeToClientVersion6)
, (NodeToClientV_9, CardanoNodeToClientVersion7)
[ (NodeToClientV_1 , CardanoNodeToClientVersion1)
, (NodeToClientV_2 , CardanoNodeToClientVersion1)
, (NodeToClientV_3 , CardanoNodeToClientVersion2)
, (NodeToClientV_4 , CardanoNodeToClientVersion3)
, (NodeToClientV_5 , CardanoNodeToClientVersion4)
, (NodeToClientV_6 , CardanoNodeToClientVersion5)
, (NodeToClientV_7 , CardanoNodeToClientVersion6)
, (NodeToClientV_8 , CardanoNodeToClientVersion6)
, (NodeToClientV_9 , CardanoNodeToClientVersion7)
, (NodeToClientV_10, CardanoNodeToClientVersion7)
]

latestReleasedNodeVersion _prx = (Just NodeToNodeV_7, Just NodeToClientV_9)
latestReleasedNodeVersion _prx = (Just NodeToNodeV_7, Just NodeToClientV_10)

{-------------------------------------------------------------------------------
ProtocolInfo
Expand Down
27 changes: 18 additions & 9 deletions ouroboros-consensus-test/src/Test/Util/Serialisation/Roundtrip.hs
Expand Up @@ -231,17 +231,26 @@ instance ( blockVersion ~ BlockNodeToClientVersion blk
Query.TopLevelQueryDisabled ->
arbitraryBlockQuery queryVersion

Query.QueryVersion1 ->
Query.QueryVersion1 -> genTopLevelQuery queryVersion
Query.QueryVersion2 -> genTopLevelQuery queryVersion
where
genTopLevelQuery queryVersion =
frequency
[ (15, arbitraryBlockQuery queryVersion)
, (1, do blockV <- arbitrary
return (WithVersion (queryVersion, blockV)
(SomeSecond GetSystemStart)))
, (1, do blockV <- arbitrary
return (WithVersion (queryVersion, blockV)
(SomeSecond GetHeaderStateTip)))
[ queryFrequencyFor Query.TopLevelQueryDisabled 15 $
arbitraryBlockQuery queryVersion
, queryFrequencyFor Query.QueryVersion1 1 $ do
blockV <- arbitrary
return (WithVersion (queryVersion, blockV) (SomeSecond GetSystemStart))
, queryFrequencyFor Query.QueryVersion2 1 $ do
blockV <- arbitrary
return (WithVersion (queryVersion, blockV) (SomeSecond GetHeaderStateTip))
]
where
where
queryFrequencyFor expectedVersion n f =
if queryVersion >= expectedVersion
then (n, f)
else (0, f)

arbitraryBlockQuery :: QueryVersion
-> Gen (WithVersion (QueryVersion, blockVersion)
(SomeSecond Query blk))
Expand Down
55 changes: 41 additions & 14 deletions ouroboros-consensus/src/Ouroboros/Consensus/Ledger/Query.hs
Expand Up @@ -81,7 +81,7 @@ data Query blk result where

-- | Get the 'GetHeaderStateTip' time.
--
-- Supported by 'QueryVersion' >= 'QueryVersionX'. -- TODO jky what version?
-- Supported by 'QueryVersion' >= 'QueryVersion2'.
GetHeaderStateTip :: Query blk (WithOrigin (HeaderStateTip blk))


Expand Down Expand Up @@ -120,6 +120,7 @@ data QueryEncoderException blk =
deriving instance Show (SomeSecond BlockQuery blk) => Show (QueryEncoderException blk)
instance (Typeable blk, Show (SomeSecond BlockQuery blk)) => Exception (QueryEncoderException blk)


queryEncodeNodeToClient ::
forall blk.
Typeable blk
Expand All @@ -143,19 +144,34 @@ queryEncodeNodeToClient codecConfig queryVersion blockVersion (SomeSecond query)
throw $ QueryEncoderUnsupportedQuery (SomeSecond query) queryVersion

-- From version 1 onwards, we use normal constructor tags
QueryVersion1 ->
_ ->
case query of
BlockQuery blockQuery ->
encodeListLen 2
<> encodeWord8 0
<> encodeBlockQuery blockQuery
requireVersion QueryVersion1 queryVersion $ mconcat
[ encodeListLen 2
, encodeWord8 0
, encodeBlockQuery blockQuery
]

GetSystemStart ->
encodeListLen 1
<> encodeWord8 1
requireVersion QueryVersion1 queryVersion $ mconcat
[ encodeListLen 1
, encodeWord8 1
]

GetHeaderStateTip ->
encodeListLen 1
<> encodeWord8 2
requireVersion QueryVersion2 queryVersion $ mconcat
[ encodeListLen 1
, encodeWord8 2
]

where
requireVersion :: QueryVersion -> QueryVersion -> a -> a
requireVersion expectedVersion actualVersion a =
if actualVersion >= expectedVersion
then a
else throw $ QueryEncoderUnsupportedQuery (SomeSecond query) actualVersion

encodeBlockQuery blockQuery =
encodeNodeToClient
@blk
Expand All @@ -174,15 +190,26 @@ queryDecodeNodeToClient ::
queryDecodeNodeToClient codecConfig queryVersion blockVersion
= case queryVersion of
TopLevelQueryDisabled -> decodeBlockQuery
QueryVersion1 -> do
QueryVersion1 -> handleTopLevelQuery
QueryVersion2 -> handleTopLevelQuery
where
handleTopLevelQuery :: Decoder s (SomeSecond Query blk)
handleTopLevelQuery = do
size <- decodeListLen
tag <- decodeWord8
case (size, tag) of
(2, 0) -> decodeBlockQuery
(1, 1) -> return (SomeSecond GetSystemStart)
(1, 2) -> return (SomeSecond GetHeaderStateTip)
(2, 0) -> requireVersion "BlockQuery" QueryVersion1 decodeBlockQuery
(1, 1) -> requireVersion "GetSystemStart" QueryVersion1 $ return (SomeSecond GetSystemStart)
(1, 2) -> requireVersion "GetHeaderStateTip" QueryVersion2 $ return (SomeSecond GetHeaderStateTip)
_ -> fail $ "Query: invalid size and tag" <> show (size, tag)
where

requireVersion :: String -> QueryVersion -> Decoder s (SomeSecond Query blk) -> Decoder s (SomeSecond Query blk)
requireVersion name expectedVersion f =
if queryVersion >= expectedVersion
then f
else fail $ "Query: " <> name <> " requires at least " <> show expectedVersion

decodeBlockQuery :: Decoder s (SomeSecond Query blk)
decodeBlockQuery = do
SomeSecond blockQuery <- decodeNodeToClient
@blk
Expand Down
29 changes: 17 additions & 12 deletions ouroboros-consensus/src/Ouroboros/Consensus/Ledger/Query/Version.hs
Expand Up @@ -7,27 +7,32 @@ module Ouroboros.Consensus.Ledger.Query.Version (
import Ouroboros.Network.NodeToClient.Version

-- | Version of the `Query blk` type.
--
-- Multiple top level queries are now supported. The encoding now has
-- constructor tags for the different top level queries for QueryVersion1 onwards.
data QueryVersion
-- | Only the 'BlockQuery' constructor of 'Query' is supported. The binary
-- encoding is backwards compatible: it does not introduce any constructor
-- tag around the 'BlockQuery'.
= TopLevelQueryDisabled

-- | Multiple top level queries are now supported. The encoding now has
-- constructor tags for the different top level queries. Specifically V1
-- adds support for 'GetSystemStart'.
-- Adds support for 'GetSystemStart'.
| QueryVersion1

-- Adds support for 'GetHeaderStateTip'.
| QueryVersion2
deriving (Eq, Ord, Enum, Bounded, Show)

-- | Get the @QueryVersion@ supported by this @NodeToClientVersion@.
nodeToClientVersionToQueryVersion :: NodeToClientVersion -> QueryVersion
nodeToClientVersionToQueryVersion x = case x of
NodeToClientV_1 -> TopLevelQueryDisabled
NodeToClientV_2 -> TopLevelQueryDisabled
NodeToClientV_3 -> TopLevelQueryDisabled
NodeToClientV_4 -> TopLevelQueryDisabled
NodeToClientV_5 -> TopLevelQueryDisabled
NodeToClientV_6 -> TopLevelQueryDisabled
NodeToClientV_7 -> TopLevelQueryDisabled
NodeToClientV_8 -> TopLevelQueryDisabled
NodeToClientV_9 -> QueryVersion1
NodeToClientV_1 -> TopLevelQueryDisabled
NodeToClientV_2 -> TopLevelQueryDisabled
NodeToClientV_3 -> TopLevelQueryDisabled
NodeToClientV_4 -> TopLevelQueryDisabled
NodeToClientV_5 -> TopLevelQueryDisabled
NodeToClientV_6 -> TopLevelQueryDisabled
NodeToClientV_7 -> TopLevelQueryDisabled
NodeToClientV_8 -> TopLevelQueryDisabled
NodeToClientV_9 -> QueryVersion1
NodeToClientV_10 -> QueryVersion2
22 changes: 13 additions & 9 deletions ouroboros-network/src/Ouroboros/Network/NodeToClient/Version.hs
Expand Up @@ -41,6 +41,8 @@ data NodeToClientVersion
-- ^ 'LocalStateQuery' protocol codec change, allows to acquire tip point.
| NodeToClientV_9
-- ^ enabled @CardanoNodeToClientVersion7@, i.e., Alonzo
| NodeToClientV_10
-- ^ enabled @CardanoNodeToClientVersion7@, i.e., Alonzo
deriving (Eq, Ord, Enum, Bounded, Show, Typeable)

-- | We set 16ths bit to distinguish `NodeToNodeVersion` and
Expand All @@ -53,15 +55,16 @@ data NodeToClientVersion
nodeToClientVersionCodec :: CodecCBORTerm (Text, Maybe Int) NodeToClientVersion
nodeToClientVersionCodec = CodecCBORTerm { encodeTerm, decodeTerm }
where
encodeTerm NodeToClientV_1 = CBOR.TInt 1
encodeTerm NodeToClientV_2 = CBOR.TInt (2 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_3 = CBOR.TInt (3 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_4 = CBOR.TInt (4 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_5 = CBOR.TInt (5 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_6 = CBOR.TInt (6 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_7 = CBOR.TInt (7 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_8 = CBOR.TInt (8 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_9 = CBOR.TInt (9 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_1 = CBOR.TInt 1
encodeTerm NodeToClientV_2 = CBOR.TInt (2 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_3 = CBOR.TInt (3 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_4 = CBOR.TInt (4 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_5 = CBOR.TInt (5 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_6 = CBOR.TInt (6 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_7 = CBOR.TInt (7 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_8 = CBOR.TInt (8 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_9 = CBOR.TInt (9 `setBit` nodeToClientVersionBit)
encodeTerm NodeToClientV_10 = CBOR.TInt (10 `setBit` nodeToClientVersionBit)

decodeTerm (CBOR.TInt tag) =
case ( tag `clearBit` nodeToClientVersionBit
Expand All @@ -76,6 +79,7 @@ nodeToClientVersionCodec = CodecCBORTerm { encodeTerm, decodeTerm }
(7, True) -> Right NodeToClientV_7
(8, True) -> Right NodeToClientV_8
(9, True) -> Right NodeToClientV_9
(10, True) -> Right NodeToClientV_10
(n, _) -> Left ( T.pack "decode NodeToClientVersion: unknown tag: " <> T.pack (show tag)
, Just n)
decodeTerm _ = Left ( T.pack "decode NodeToClientVersion: unexpected term"
Expand Down
Expand Up @@ -14,7 +14,7 @@ msgRefuse = [2, refuseReason]
versionTable = { * versionNumber => nodeToClientVersionData }

; from version 2 we set 15th bit to 1
versionNumber = 1 / 32770 / 32771 / 32772 / 32773 / 32774 / 32775 / 32776 / 32777
versionNumber = 1 / 32770 / 32771 / 32772 / 32773 / 32774 / 32775 / 32776 / 32777 / 32778

nodeToClientVersionData = networkMagic

Expand Down

0 comments on commit 37f1757

Please sign in to comment.