-
Notifications
You must be signed in to change notification settings - Fork 1
feat(rust/signed-doc): Add chain metadata field in the Signed Doc header
#569
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ba2eaba
feat: initial chain
apskhem 6ef91c7
feat: chain body
apskhem c43d297
feat: serde template
apskhem 01b1aa7
feat: cbor decoder
apskhem b4da2d7
feat: encoder
apskhem a006a70
feat: proper serde for doc_refs
apskhem 0c6c281
fix: deserializer
apskhem d2ec255
chore: lintfix
apskhem 227a517
Merge branch 'main' into feat/chain-metadata-field
apskhem 37271dd
feat: accessor
apskhem 82ff5b4
test: initial
apskhem 54f3e52
Merge branch 'main' into feat/chain-metadata-field
apskhem 9ae8e57
chore: minor
apskhem 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
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 |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| //! Document Payload Chain. | ||
| //! | ||
| //! ref: <https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/signed_doc/metadata/#chain-link> | ||
|
|
||
| use std::{fmt::Display, hash::Hash}; | ||
|
|
||
| use cbork_utils::{array::Array, decode_context::DecodeCtx}; | ||
|
|
||
| use crate::DocumentRef; | ||
|
|
||
| /// Reference to the previous Signed Document in a sequence. | ||
| #[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Deserialize, serde::Serialize)] | ||
| pub struct Chain { | ||
| /// The consecutive sequence number of the current document | ||
| /// in the chain. | ||
| /// The very first document in a sequence is numbered `0` and it | ||
| /// *MUST ONLY* increment by one for each successive document in | ||
| /// the sequence. | ||
| /// | ||
| /// The FINAL sequence number is encoded with the current height | ||
| /// sequence value, negated. | ||
| /// | ||
| /// For example the following values for height define a chain | ||
| /// that has 5 documents in the sequence 0-4, the final height | ||
| /// is negated to indicate the end of the chain: | ||
| /// `0, 1, 2, 3, -4` | ||
| /// | ||
| /// No subsequent document can be chained to a sequence that has | ||
| /// a final chain height. | ||
| height: i32, | ||
| /// Reference to a single Signed Document. | ||
| /// | ||
| /// Can be *ONLY* omitted in the very first document in a sequence. | ||
| document_ref: Option<DocumentRef>, | ||
| } | ||
|
|
||
| impl Display for Chain { | ||
| fn fmt( | ||
| &self, | ||
| f: &mut std::fmt::Formatter<'_>, | ||
| ) -> std::fmt::Result { | ||
| if let Some(document_ref) = &self.document_ref { | ||
| write!(f, "height: {}, document_ref: {}", self.height, document_ref) | ||
| } else { | ||
| write!(f, "height: {}", self.height) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl minicbor::Encode<()> for Chain { | ||
| fn encode<W: minicbor::encode::Write>( | ||
| &self, | ||
| e: &mut minicbor::Encoder<W>, | ||
| _ctx: &mut (), | ||
| ) -> Result<(), minicbor::encode::Error<W::Error>> { | ||
| e.array(if self.document_ref.is_some() { 2 } else { 1 })?; | ||
| self.height.encode(e, &mut ())?; | ||
| if let Some(doc_ref) = &self.document_ref { | ||
| doc_ref.encode(e, &mut ())?; | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl minicbor::Decode<'_, ()> for Chain { | ||
| fn decode( | ||
| d: &mut minicbor::Decoder<'_>, | ||
| _ctx: &mut (), | ||
| ) -> Result<Self, minicbor::decode::Error> { | ||
| const CONTEXT: &str = "Chain decoding"; | ||
|
|
||
| let arr = Array::decode(d, &mut DecodeCtx::Deterministic)?; | ||
|
|
||
| let Some(height_bytes) = arr.first() else { | ||
| return Err(minicbor::decode::Error::message(format!( | ||
| "{CONTEXT}: expected [height, ? document_ref], found empty array" | ||
| ))); | ||
| }; | ||
|
|
||
| let height = minicbor::Decoder::new(height_bytes).int()?; | ||
| let height = height.try_into().map_err(minicbor::decode::Error::custom)?; | ||
|
|
||
| let document_ref = match arr.get(1) { | ||
| Some(bytes) => { | ||
| let mut d = minicbor::Decoder::new(bytes); | ||
| Some(DocumentRef::decode(&mut d, &mut ())?) | ||
| }, | ||
| None => None, | ||
| }; | ||
|
|
||
| Ok(Self { | ||
| height, | ||
| document_ref, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use catalyst_types::uuid::UuidV7; | ||
| use minicbor::{Decode, Decoder, Encode, Encoder}; | ||
|
|
||
| use super::*; | ||
| use crate::DocLocator; | ||
|
|
||
| #[test] | ||
| fn test_chain_encode_decode_without_doc_ref() { | ||
| let chain = Chain { | ||
| height: 0, | ||
| document_ref: None, | ||
| }; | ||
|
|
||
| let mut buf = Vec::new(); | ||
| let mut enc = Encoder::new(&mut buf); | ||
| chain.encode(&mut enc, &mut ()).unwrap(); | ||
|
|
||
| let mut dec = Decoder::new(&buf); | ||
| let decoded = Chain::decode(&mut dec, &mut ()).unwrap(); | ||
|
|
||
| assert_eq!(decoded, chain); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_chain_encode_decode_with_doc_ref() { | ||
| let id = UuidV7::new(); | ||
| let ver = UuidV7::new(); | ||
|
|
||
| let chain = Chain { | ||
| height: 3, | ||
| document_ref: Some(DocumentRef::new(id, ver, DocLocator::default())), | ||
| }; | ||
|
|
||
| let mut buf = Vec::new(); | ||
| let mut enc = Encoder::new(&mut buf); | ||
| chain.encode(&mut enc, &mut ()).unwrap(); | ||
|
|
||
| let mut dec = Decoder::new(&buf); | ||
| let decoded = Chain::decode(&mut dec, &mut ()).unwrap(); | ||
|
|
||
| assert_eq!(decoded, chain); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.