-
Notifications
You must be signed in to change notification settings - Fork 0
[DRAFT] Implement Metadata record support #58
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
Closed
Closed
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b716414
Refactor roundtrip test to use HSpec
joaosreis a0b00f3
Move rountrip test to its own module
joaosreis e3d0b06
Fix typo
joaosreis 9608bae
Initial Metadata structure definition
joaosreis a77a691
Implement serialization for metadata
joaosreis a73a9c1
Implement generic frame reader
joaosreis 6eacbd2
Define sorted_encoded_data
joaosreis 2596dc6
Add roundtrip test for metadata
joaosreis 1169238
Update cabal file
joaosreis bcc39ca
Introduce dump configuration using builder pattern
joaosreis f256d05
Refactor serialize functions and tests to use dump config
joaosreis ada8c38
Merge remote-tracking branch 'origin/main' into 36-support-meta-blocks
joaosreis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
scls-format/src/Cardano/SCLS/Internal/Record/Metadata.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE LambdaCase #-} | ||
| {-# LANGUAGE RecordWildCards #-} | ||
|
|
||
| {- | | ||
| Record to hold metadata information. | ||
| -} | ||
| module Cardano.SCLS.Internal.Record.Metadata (Metadata, mkMetadata) where | ||
|
|
||
| import Cardano.SCLS.Internal.Hash (Digest, digest) | ||
| import Cardano.SCLS.Internal.Record.Internal.Class | ||
| import Data.Binary (Binary (get), Word32, put) | ||
| import Data.Binary.Get (getByteString, getWord32be, getWord64be, getWord8) | ||
| import Data.Binary.Put (putByteString, putWord32be, putWord64be) | ||
| import Data.ByteString qualified as BS | ||
| import Data.Word (Word64) | ||
|
|
||
| -- TODO: reintroduce MetadataEntry ? | ||
| -- data MetadataEntry = MetadataEntry | ||
| -- { subject :: URI | ||
| -- , value :: BS.ByteString -- CBOR | ||
| -- } | ||
| -- deriving (Show) | ||
|
|
||
| data MetadataFooter = MetadataFooter | ||
| { totalEntries :: Word64 | ||
| , entriesHash :: Digest | ||
| } | ||
| deriving (Show, Eq) | ||
|
|
||
| data Metadata = Metadata | ||
| { entries :: BS.ByteString -- TODO: reintroduce [MetadataEntry] ? | ||
|
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. I'd reintroduce 'MetadataEntry' here, because otherwise it will likely be not very readable by the other clients. |
||
| , footer :: MetadataFooter | ||
| } | ||
| deriving (Show, Eq) | ||
|
|
||
| instance IsFrameRecord 0x31 Metadata where | ||
| encodeRecordContents Metadata{..} = do | ||
| putWord32be (fromIntegral (BS.length entries) :: Word32) | ||
| putByteString entries | ||
| -- putWord64be 0 -- end of entries | ||
| putFooter footer | ||
| where | ||
| -- putEntry MetadataEntry{..} = do | ||
| -- let subjectBytes = serializeURIRef' subject | ||
| -- subjectLen = BS.length subjectBytes | ||
| -- valueLen = BS.length value | ||
|
|
||
| -- putWord64be (fromIntegral subjectLen) | ||
| -- putByteString subjectBytes | ||
| -- putWord64be (fromIntegral valueLen) | ||
| -- putByteString value | ||
|
|
||
| putFooter MetadataFooter{..} = do | ||
| putWord64be totalEntries | ||
| put entriesHash | ||
|
|
||
| decodeRecordContents = do | ||
| _ <- getWord8 -- type offset: maintain consistency with Chunk pattern | ||
joaosreis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| entriesSize <- getWord32be | ||
| entries <- getByteString (fromIntegral entriesSize) | ||
| -- _ <- getWord64be -- end of entries marker | ||
| footer <- getFooter | ||
| pure Metadata{..} | ||
| where | ||
| -- getEntry = do | ||
| -- len <- getWord64be | ||
| -- if len == 0 | ||
| -- then return Nothing | ||
| -- else do | ||
| -- subjectLen <- getWord64be | ||
| -- subjectBytes <- getByteString (fromIntegral subjectLen) | ||
| -- let subject = case parseURI strictURIParserOptions subjectBytes of | ||
| -- Left err -> error $ "Failed to parse URI: " ++ show err | ||
| -- Right uri -> uri | ||
| -- valueLen <- getWord64be | ||
| -- value <- getByteString (fromIntegral valueLen) | ||
| -- pure (Just MetadataEntry{..}) | ||
|
|
||
| getFooter = do | ||
| totalEntries <- getWord64be | ||
| entriesHash <- get | ||
| pure MetadataFooter{..} | ||
|
|
||
| mkMetadata :: BS.ByteString -> Word64 -> Metadata | ||
| mkMetadata entries totalEntries = | ||
| let | ||
| -- entries = [MetadataEntry subject value | (subject, value) <- metadataEntries] | ||
| -- totalEntries = fromIntegral $ length entries | ||
| entriesHash = digest entries | ||
| footer = MetadataFooter{..} | ||
| in | ||
| Metadata{..} | ||
| where | ||
joaosreis marked this conversation as resolved.
Show resolved
Hide resolved
joaosreis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| -- digestEntries entries = | ||
| -- digest $ mconcat $ map entryDigestInput entries | ||
| -- where | ||
| -- entryDigestInput MetadataEntry{..} = | ||
| -- BS.singleton 0x01 <> serializeURIRef' subject <> value | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,100 +1,14 @@ | ||
| module Main (main) where | ||
|
|
||
| import Cardano.SCLS.CDDL (namespaces) | ||
| import Cardano.SCLS.Internal.Hash (Digest (..)) | ||
| import Cardano.SCLS.Internal.Reader (extractRootHash, withNamespacedData) | ||
| import Cardano.SCLS.Internal.Serializer.External.Impl qualified as External (serialize) | ||
| import Cardano.SCLS.Internal.Serializer.MemPack | ||
| import Cardano.SCLS.Internal.Serializer.Reference.Impl (InputChunk) | ||
| import Cardano.SCLS.Internal.Serializer.Reference.Impl qualified as Reference (serialize) | ||
| import Cardano.Types.Network (NetworkId (..)) | ||
| import Cardano.Types.SlotNo (SlotNo (..)) | ||
| import ChunksBuilderSpec (chunksBuilderTests) | ||
| import Codec.CBOR.Cuddle.CBOR.Gen (generateCBORTerm') | ||
| import Codec.CBOR.Cuddle.CDDL (CDDL, Name (..)) | ||
| import Codec.CBOR.Cuddle.CDDL.Resolve ( | ||
| asMap, | ||
| buildMonoCTree, | ||
| buildRefCTree, | ||
| buildResolvedCTree, | ||
| ) | ||
| import Codec.CBOR.Cuddle.Huddle | ||
| import Codec.CBOR.Term (encodeTerm) | ||
| import Codec.CBOR.Write (toStrictByteString) | ||
| import Control.Monad (replicateM) | ||
| import Crypto.Hash.MerkleTree.Incremental qualified as MT | ||
| import Data.Function ((&)) | ||
| import Data.List (sort) | ||
| import Data.Map.Strict qualified as Map | ||
| import Data.Text (Text) | ||
| import Data.Text qualified as T | ||
| import Streaming.Prelude qualified as S | ||
| import System.FilePath ((</>)) | ||
| import System.IO.Temp (withSystemTempDirectory) | ||
| import System.Random.Stateful (applyAtomicGen, globalStdGen) | ||
| import Test.HUnit | ||
| import Test.Hspec | ||
| import Test.Hspec.Contrib.HUnit | ||
|
|
||
| import MultiNamespace qualified (tests) | ||
| import Roundtrip qualified (tests) | ||
|
|
||
| type SerializeF = FilePath -> NetworkId -> SlotNo -> S.Stream (S.Of (InputChunk RawBytes)) IO () -> IO () | ||
| import Test.Hspec | ||
|
|
||
| main :: IO () | ||
| main = do | ||
| hspec $ do | ||
| fromHUnitTest tests | ||
| Roundtrip.tests | ||
| chunksBuilderTests | ||
| MultiNamespace.tests | ||
| where | ||
| tests = | ||
| TestList | ||
| [ roundTriptests | ||
| ] | ||
| roundTriptests = | ||
| TestLabel "Roundtrip tests" $ | ||
| TestList | ||
| [ mkRountripTestsFor "Reference" Reference.serialize | ||
| , mkRountripTestsFor "External" External.serialize | ||
| ] | ||
| mkRountripTestsFor :: String -> SerializeF -> Test | ||
| mkRountripTestsFor groupName serialize = | ||
| TestLabel groupName $ | ||
| TestList | ||
| [ TestLabel n $ TestCase $ roundtrip (T.pack n) (toCDDL huddle) serialize | ||
| | (n, huddle) <- Map.toList namespaces | ||
| ] | ||
| roundtrip :: Text -> CDDL -> SerializeF -> Assertion | ||
| roundtrip namespace cddl serialize = do | ||
| case buildMonoCTree =<< buildResolvedCTree (buildRefCTree $ asMap cddl) of | ||
| Left err -> assertFailure $ "Failed to build CTree: " ++ show err | ||
| Right mt -> withSystemTempDirectory "scls-format-test-XXXXXX" $ \fn -> do | ||
| data_ <- | ||
| replicateM 1024 $ | ||
| applyAtomicGen (generateCBORTerm' mt (Name (T.pack "record_entry") mempty)) globalStdGen | ||
| let encoded_data = [toStrictByteString (encodeTerm term) | term <- data_] | ||
| let fileName = (fn </> "data.scls") | ||
| serialize | ||
| fileName | ||
| Mainnet | ||
| (SlotNo 1) | ||
| (S.each [(namespace S.:> (S.each encoded_data & S.map RawBytes))]) | ||
| withNamespacedData | ||
| fileName | ||
| namespace | ||
| ( \stream -> do | ||
| decoded_data <- S.toList_ stream | ||
| assertEqual | ||
| "Stream roundtrip successful" | ||
| [b | RawBytes b <- decoded_data] | ||
| (sort encoded_data) | ||
| ) | ||
| -- Check roundtrip of root hash | ||
| file_digest <- extractRootHash fileName | ||
| expected_digest <- | ||
| S.each (sort encoded_data) | ||
| & S.fold_ MT.add (MT.empty undefined) (Digest . MT.merkleRootHash . MT.finalize) | ||
| assertEqual | ||
| "Root hash roundtrip successful" | ||
| file_digest | ||
| (Digest $ MT.merkleRootHash $ MT.finalize $ MT.add (MT.empty undefined) expected_digest) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think we need to add strictness here, just to be sure