Skip to content

Commit 6214336

Browse files
qnikstCopilot
andauthored
Extend information for the known Namespaces (#105)
* Extend information for the known Namespaces We introduce a structure for the information about the namespaces NamespaceInfo, that keeps an information and configuration for the namespaces. Currently there are only two fields, but they can be excended later: 1. namespaceSpec - Huddle specification for the namespace 2. namespaceKeySize - size of the key We need the latter because in the CIP we propose that all the keys in the namespaces have a statically known size. This way we can pass this information. We use GHC Natural because later we will need that info to pass the values on the type level. * Update scls-cddl/cddl-src/Cardano/SCLS/CDDL.hs Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 726ee38 commit 6214336

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1-
module Cardano.SCLS.CDDL where
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
module Cardano.SCLS.CDDL (
4+
NamespaceInfo (..),
5+
namespaces,
6+
) where
27

38
import Cardano.SCLS.Namespace.Blocks qualified as Blocks
49
import Cardano.SCLS.Namespace.UTxO qualified as UTxO
510
import Codec.CBOR.Cuddle.Huddle (Huddle, HuddleItem (HIRule), collectFromInit)
611
import Data.Map.Strict qualified as Map
12+
import Data.Text (Text)
13+
import Numeric.Natural (Natural)
14+
15+
-- | Various information about supported namespaces.
16+
data NamespaceInfo = NamespaceInfo
17+
{ namespaceSpec :: !Huddle
18+
-- ^ Specification for the namespace entries.
19+
, namespaceKeySize :: !Natural
20+
-- ^ Size of the keys in the namespace.
21+
}
722

8-
namespaces :: Map.Map String Huddle
23+
-- | List of the namespaces known to the SCLS utilities.
24+
namespaces :: Map.Map Text NamespaceInfo
925
namespaces =
1026
Map.fromList
11-
[ ("utxo/v0", collectFromInit [HIRule UTxO.record_entry]),
12-
("blocks/v0", collectFromInit [HIRule Blocks.record_entry])
27+
[ ("utxo/v0", NamespaceInfo (collectFromInit [HIRule UTxO.record_entry]) 32)
28+
, ("blocks/v0", NamespaceInfo (collectFromInit [HIRule Blocks.record_entry]) 32)
1329
]

scls-cddl/gen-cddl/Main.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
module Main where
44

5-
import Cardano.SCLS.CDDL (namespaces)
5+
import Cardano.SCLS.CDDL (NamespaceInfo (..), namespaces)
66

77
import Codec.CBOR.Cuddle.Huddle qualified as Cuddle
88
import Codec.CBOR.Cuddle.Pretty ()
99
import Control.Monad (forM_)
1010
import Data.Map.Strict qualified as Map
11+
import Data.Text qualified as T
1112
import Prettyprinter (pretty)
1213
import Prettyprinter.Render.Text (hPutDoc)
1314
import System.Environment (getArgs)
@@ -17,8 +18,8 @@ import System.IO
1718
main :: IO ()
1819
main =
1920
getArgs >>= \case
20-
[dir] -> forM_ (Map.toList namespaces) $ \(ns, cddl) -> do
21-
writeSpec cddl (dir </> ns <.> "cddl")
21+
[dir] -> forM_ (Map.toList namespaces) $ \(ns, NamespaceInfo{namespaceSpec = cddl}) -> do
22+
writeSpec cddl (dir </> T.unpack ns <.> "cddl")
2223
_ -> error "Usage: gen-cddl directory"
2324

2425
writeSpec :: Cuddle.Huddle -> FilePath -> IO ()

scls-cddl/scls-cddl.cabal

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ library
3535
base >=4.18 && <5,
3636
containers >=0.6,
3737
cuddle >=0.5,
38-
heredoc >=0.2
38+
heredoc >=0.2,
39+
text
3940

4041
hs-source-dirs:
4142
cddl-src
@@ -53,4 +54,5 @@ executable gen-cddl
5354
cuddle >=0.5,
5455
filepath >=1.4,
5556
prettyprinter,
56-
scls-cddl
57+
scls-cddl,
58+
text

scls-format/test/Roundtrip.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Roundtrip (
55
tests,
66
) where
77

8-
import Cardano.SCLS.CDDL (namespaces)
8+
import Cardano.SCLS.CDDL (NamespaceInfo (..), namespaces)
99
import Cardano.SCLS.Internal.Entry
1010
import Cardano.SCLS.Internal.Hash (Digest (..))
1111
import Cardano.SCLS.Internal.Reader (extractRootHash, withHeader, withNamespacedData, withRecordData)
@@ -36,7 +36,6 @@ import Data.Function ((&))
3636
import Data.List (sort)
3737
import Data.Map.Strict qualified as Map
3838
import Data.MemPack
39-
import Data.String
4039
import Data.Text qualified as T
4140
import Data.Word (Word32, Word64)
4241
import Streaming.Prelude qualified as S
@@ -73,7 +72,7 @@ mkRoundtripTestsFor groupName serialize =
7372
describe groupName $ do
7473
sequence_
7574
[ context (Namespace.asString n) $ it "should succeed with stream roundtrip" $ roundtrip n (toCDDL huddle)
76-
| (fromString -> n, huddle) <- Map.toList namespaces
75+
| (Namespace.fromText -> n, namespaceSpec -> huddle) <- Map.toList namespaces
7776
]
7877
where
7978
roundtrip namespace cddl = do

0 commit comments

Comments
 (0)