-
Notifications
You must be signed in to change notification settings - Fork 66
A0-1872: Network data for sync #879
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
timorl
merged 4 commits into
Cardinal-Cryptography:main
from
timorl:A0-1872-ireland-2024
Jan 23, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| use std::mem::size_of; | ||
|
|
||
| use aleph_primitives::MAX_BLOCK_SIZE; | ||
| use codec::{Decode, Encode, Error as CodecError, Input as CodecInput}; | ||
| use log::warn; | ||
|
|
||
| use crate::{sync::Justification, Version}; | ||
|
|
||
| /// The representation of the database state to be sent to other nodes. | ||
| /// In the first version this only contains the top justification. | ||
| #[derive(Clone, Debug, Encode, Decode)] | ||
| pub struct State<J: Justification> { | ||
| top_justification: J::Unverified, | ||
| } | ||
|
|
||
| /// Data to be sent over the network. | ||
| #[derive(Clone, Debug, Encode, Decode)] | ||
| pub enum NetworkData<J: Justification> { | ||
| /// A periodic state broadcast, so that neighbouring nodes can request what they are missing, | ||
| /// send what we are missing, and sometines just use the justifications to update their own | ||
| /// state. | ||
| StateBroadcast(State<J>), | ||
| /// A series of justifications, sent to a node that is clearly behind. | ||
| Justifications(Vec<J::Unverified>, State<J>), | ||
| } | ||
|
|
||
| /// Version wrapper around the network data. | ||
| #[derive(Clone, Debug)] | ||
| pub enum VersionedNetworkData<J: Justification> { | ||
| // Most likely from the future. | ||
| Other(Version, Vec<u8>), | ||
| V1(NetworkData<J>), | ||
| } | ||
|
|
||
| // We need 32 bits, since blocks can be quite sizeable. | ||
| type ByteCount = u32; | ||
|
|
||
| // We want to be able to safely send at least 10 blocks at once, so this gives uss a bit of wiggle | ||
| // room. | ||
| const MAX_SYNC_MESSAGE_SIZE: u32 = MAX_BLOCK_SIZE * 11; | ||
|
|
||
| fn encode_with_version(version: Version, payload: &[u8]) -> Vec<u8> { | ||
| let size = payload.len().try_into().unwrap_or(ByteCount::MAX); | ||
|
|
||
| if size > MAX_SYNC_MESSAGE_SIZE { | ||
| warn!( | ||
| "Versioned sync message v{:?} too big during Encode. Size is {:?}. Should be {:?} at max.", | ||
| version, | ||
| payload.len(), | ||
| MAX_SYNC_MESSAGE_SIZE | ||
| ); | ||
| } | ||
|
|
||
| let mut result = Vec::with_capacity(version.size_hint() + size.size_hint() + payload.len()); | ||
|
|
||
| version.encode_to(&mut result); | ||
| size.encode_to(&mut result); | ||
| result.extend_from_slice(payload); | ||
|
|
||
| result | ||
| } | ||
|
|
||
| impl<J: Justification> Encode for VersionedNetworkData<J> { | ||
| fn size_hint(&self) -> usize { | ||
| use VersionedNetworkData::*; | ||
| let version_size = size_of::<Version>(); | ||
| let byte_count_size = size_of::<ByteCount>(); | ||
| version_size | ||
| + byte_count_size | ||
| + match self { | ||
| Other(_, payload) => payload.len(), | ||
| V1(data) => data.size_hint(), | ||
| } | ||
| } | ||
|
|
||
| fn encode(&self) -> Vec<u8> { | ||
| use VersionedNetworkData::*; | ||
| match self { | ||
| Other(version, payload) => encode_with_version(*version, payload), | ||
| V1(data) => encode_with_version(Version(1), &data.encode()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<J: Justification> Decode for VersionedNetworkData<J> { | ||
| fn decode<I: CodecInput>(input: &mut I) -> Result<Self, CodecError> { | ||
| use VersionedNetworkData::*; | ||
| let version = Version::decode(input)?; | ||
| let num_bytes = ByteCount::decode(input)?; | ||
| match version { | ||
| Version(1) => Ok(V1(NetworkData::decode(input)?)), | ||
| _ => { | ||
| if num_bytes > MAX_SYNC_MESSAGE_SIZE { | ||
| Err("Sync message has unknown version and is encoded as more than the maximum size.")?; | ||
| }; | ||
| let mut payload = vec![0; num_bytes as usize]; | ||
| input.read(payload.as_mut_slice())?; | ||
| Ok(Other(version, payload)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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.