-
Notifications
You must be signed in to change notification settings - Fork 74
simple timestamp sort #2676
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
simple timestamp sort #2676
Changes from all commits
Commits
Show all changes
2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
7 changes: 7 additions & 0 deletions
7
xmtp_api_d14n/proptest-regressions/protocol/sort/timestamp.txt
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,7 @@ | ||
| # Seeds for failure cases proptest has generated in the past. It is | ||
| # automatically read and these particular cases re-run before any | ||
| # novel cases are generated. | ||
| # | ||
| # It is recommended to check this file in to source control so that | ||
| # everyone who runs the test benefits from these saved cases. | ||
| cc 783ebaf4bd78f8d0bb95fa2dbd7c798aee6abfb3d504dbc7ac1f7724fd1bb326 # shrinks to mut envelopes = [TestEnvelope { time: Some(0) }, TestEnvelope { time: Some(-1) }] |
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,64 @@ | ||
| //! Extractor for a envelope [`Cursor`](xmtp_proto::types::Cursor) | ||
| //! useful for verifing a message has been read or maybe duplicates. | ||
| use xmtp_proto::ConversionError; | ||
| use xmtp_proto::mls_v1::welcome_message::WelcomePointer as V3WelcomePointer; | ||
| use xmtp_proto::types::Cursor; | ||
| use xmtp_proto::xmtp::mls::api::v1::{ | ||
| group_message::V1 as V3GroupMessage, welcome_message::V1 as V3WelcomeMessage, | ||
| }; | ||
| use xmtp_proto::xmtp::xmtpv4::envelopes::UnsignedOriginatorEnvelope; | ||
|
|
||
| use crate::protocol::{EnvelopeVisitor, Extractor}; | ||
|
|
||
| /// Extract Cursor from Envelopes | ||
| #[derive(Default, Clone, Debug)] | ||
| pub struct CursorExtractor { | ||
| cursor: Option<Cursor>, | ||
| } | ||
|
|
||
| impl CursorExtractor { | ||
| pub fn new() -> Self { | ||
| Default::default() | ||
| } | ||
| } | ||
|
|
||
| impl Extractor for CursorExtractor { | ||
| type Output = Result<Cursor, ConversionError>; | ||
|
|
||
| fn get(self) -> Self::Output { | ||
| self.cursor.ok_or(ConversionError::Missing { | ||
| item: "cursor", | ||
| r#type: std::any::type_name::<Cursor>(), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl EnvelopeVisitor<'_> for CursorExtractor { | ||
| type Error = ConversionError; | ||
|
|
||
| fn visit_unsigned_originator( | ||
| &mut self, | ||
| e: &UnsignedOriginatorEnvelope, | ||
| ) -> Result<(), Self::Error> { | ||
| self.cursor = Some(Cursor { | ||
| sequence_id: e.originator_sequence_id, | ||
| originator_id: e.originator_node_id, | ||
| }); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn visit_v3_group_message(&mut self, m: &V3GroupMessage) -> Result<(), Self::Error> { | ||
| self.cursor = Some(Cursor::v3_messages(m.id)); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn visit_v3_welcome_message(&mut self, m: &V3WelcomeMessage) -> Result<(), Self::Error> { | ||
| self.cursor = Some(Cursor::v3_welcomes(m.id)); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn visit_v3_welcome_pointer(&mut self, m: &V3WelcomePointer) -> Result<(), Self::Error> { | ||
| self.cursor = Some(Cursor::v3_welcomes(m.id)); | ||
| Ok(()) | ||
| } | ||
| } |
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,57 @@ | ||
| //! Extractor for an MLS Data field | ||
| //! useful for verifing a message has been read or maybe duplicates. | ||
| use chrono::Utc; | ||
| use xmtp_proto::ConversionError; | ||
| use xmtp_proto::mls_v1::welcome_message::WelcomePointer; | ||
| use xmtp_proto::xmtp::mls::api::v1::{ | ||
| group_message::V1 as V3GroupMessage, welcome_message::V1 as V3WelcomeMessage, | ||
| }; | ||
| use xmtp_proto::xmtp::xmtpv4::envelopes::UnsignedOriginatorEnvelope; | ||
|
|
||
| use crate::protocol::{EnvelopeVisitor, Extractor}; | ||
|
|
||
| /// Extract Mls Data from Envelopes | ||
| #[derive(Default, Clone, Debug)] | ||
| pub struct TimestampExtractor { | ||
| time: Option<i64>, | ||
| } | ||
|
|
||
| impl TimestampExtractor { | ||
| pub fn new() -> Self { | ||
| Default::default() | ||
| } | ||
| } | ||
|
|
||
| impl Extractor for TimestampExtractor { | ||
| type Output = Option<chrono::DateTime<Utc>>; | ||
|
|
||
| fn get(self) -> Self::Output { | ||
| self.time.map(chrono::DateTime::from_timestamp_nanos) | ||
| } | ||
| } | ||
|
|
||
| impl EnvelopeVisitor<'_> for TimestampExtractor { | ||
| type Error = ConversionError; | ||
|
|
||
| fn visit_unsigned_originator( | ||
| &mut self, | ||
| e: &UnsignedOriginatorEnvelope, | ||
| ) -> Result<(), Self::Error> { | ||
| self.time = Some(e.originator_ns); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn visit_v3_group_message(&mut self, message: &V3GroupMessage) -> Result<(), Self::Error> { | ||
| self.time = Some(message.created_ns as i64); | ||
macroscopeapp[bot] marked this conversation as resolved.
Show resolved
Hide resolved
insipx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn visit_v3_welcome_message(&mut self, message: &V3WelcomeMessage) -> Result<(), Self::Error> { | ||
| self.time = Some(message.created_ns as i64); | ||
| Ok(()) | ||
| } | ||
| fn visit_v3_welcome_pointer(&mut self, ptr: &WelcomePointer) -> Result<(), Self::Error> { | ||
| self.time = Some(ptr.created_ns as i64); | ||
| Ok(()) | ||
| } | ||
| } | ||
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,10 @@ | ||
| //! Implementations for Dependency Resolution strategies [XIP](https://github.com/xmtp/XIPs/blob/main/XIPs/xip-49-decentralized-backend.md#335-cross-originator-message-ordering) | ||
| //! | ||
| //! Possible Implementation of Dependency Resolution Strategies: | ||
| //! - keep retrying same query and error forever after and finish after some backoff | ||
| //! - query the originator that the mesasge is stored on. | ||
| //! - file misbehavior report if originator message came from is unresponsive | ||
| //! - dont resolve dependencies at all | ||
| //! - query random originators for the dependency | ||
| //! - round robin query for dependency | ||
| //! - etc. |
Empty file.
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,5 @@ | ||
| //! Sorting Implelementations on Envelope Collections [XIP](https://github.com/xmtp/XIPs/blob/main/XIPs/xip-49-decentralized-backend.md#335-cross-originator-message-ordering) | ||
| // mod causal; | ||
| // pub use causal::*; | ||
| mod timestamp; | ||
| pub use timestamp::*; |
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,125 @@ | ||
| use crate::protocol::{Envelope, Sort}; | ||
|
|
||
| pub struct TimestampSort<'a, E> { | ||
| envelopes: &'a mut [E], | ||
| } | ||
|
|
||
| impl<'b, 'a, E> Sort for TimestampSort<'b, E> | ||
| where | ||
| E: Envelope<'a>, | ||
| { | ||
| fn sort(mut self) { | ||
| let envelopes = &mut self.envelopes; | ||
| // we can only sort envelopes which have a timestamp | ||
| envelopes.sort_unstable_by_key(|e| e.timestamp()); | ||
| } | ||
| } | ||
|
|
||
| /// Sorts Envelopes by server-side Timestamp in ascending order | ||
| /// * for d14n this will sort envelopes by | ||
| /// [`originator_ns`](xmtp_proto::xmtp::xmtpv4::envelopes::UnsignedOriginatorEnvelope::originator_ns) | ||
| /// * for v3 this will sort by created_ns on GroupMessage, WelcomeMessage, or WelcomePointer | ||
| /// overall, sorts according to the timestamp extracted by | ||
| /// [`TimestampExtractor`](crate::protocol::TimestampExtractor) | ||
| /// | ||
| /// If a timestamp does not have a cursor (extractor return [`Option::None`]) it is | ||
| /// sorted according to [`Ord`], [impl](https://doc.rust-lang.org/src/core/option.rs.html#2341) | ||
| /// This sort will never return any missing envelopes. | ||
| pub fn timestamp<'b, 'a: 'b, E: Envelope<'a>>(envelopes: &'b mut [E]) -> impl Sort { | ||
| TimestampSort { envelopes } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::protocol::sort; | ||
|
Contributor
Author
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. these tests are a bit extra since this sort is exactly one call to |
||
| use chrono::Utc; | ||
| use proptest::prelude::*; | ||
| use xmtp_common::Generate; | ||
| use xmtp_proto::xmtp::xmtpv4::envelopes::ClientEnvelope; | ||
| use xmtp_proto::xmtp::xmtpv4::envelopes::client_envelope::Payload; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[derive(Debug)] | ||
| struct TestEnvelope { | ||
| time: Option<chrono::DateTime<Utc>>, | ||
| } | ||
|
|
||
| impl TestEnvelope { | ||
| fn new(time: i64) -> Self { | ||
| Self { | ||
| time: Some(chrono::DateTime::from_timestamp_nanos(time)), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Generate for TestEnvelope { | ||
| fn generate() -> Self { | ||
| TestEnvelope { | ||
| time: Some(chrono::DateTime::from_timestamp_nanos( | ||
| xmtp_common::rand_i64(), | ||
| )), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Envelope<'_> for TestEnvelope { | ||
| fn topic(&self) -> Result<xmtp_proto::types::Topic, crate::protocol::EnvelopeError> { | ||
| unreachable!() | ||
| } | ||
|
|
||
| fn payload(&self) -> Result<Payload, crate::protocol::EnvelopeError> { | ||
| unreachable!() | ||
| } | ||
|
|
||
| fn timestamp(&self) -> Option<chrono::DateTime<Utc>> { | ||
| self.time | ||
| } | ||
|
|
||
| fn client_envelope(&self) -> Result<ClientEnvelope, crate::protocol::EnvelopeError> { | ||
| unreachable!() | ||
| } | ||
|
|
||
| fn group_message( | ||
| &self, | ||
| ) -> Result<Option<xmtp_proto::types::GroupMessage>, crate::protocol::EnvelopeError> | ||
| { | ||
| unreachable!() | ||
| } | ||
|
|
||
| fn welcome_message( | ||
| &self, | ||
| ) -> Result<Option<xmtp_proto::types::WelcomeMessage>, crate::protocol::EnvelopeError> | ||
| { | ||
| unreachable!() | ||
| } | ||
|
|
||
| fn consume<E>(&self, _extractor: E) -> Result<E::Output, crate::protocol::EnvelopeError> | ||
| where | ||
| Self: Sized, | ||
| for<'a> crate::protocol::EnvelopeError: | ||
| From<<E as crate::protocol::EnvelopeVisitor<'a>>::Error>, | ||
| for<'a> E: crate::protocol::EnvelopeVisitor<'a> + crate::protocol::Extractor, | ||
| { | ||
| unreachable!() | ||
| } | ||
| } | ||
|
|
||
| prop_compose! { | ||
| fn envelope_all_some()(id in 1..u32::MAX) -> TestEnvelope { | ||
| TestEnvelope::new(id as i64) | ||
| } | ||
| } | ||
|
|
||
| fn is_sorted(sorted: &[TestEnvelope]) -> bool { | ||
| sorted.is_sorted_by_key(|e| e.time) | ||
| } | ||
|
|
||
| #[xmtp_common::test] | ||
| fn sorts_by_timestamp() { | ||
| proptest!(|(mut envelopes in prop::collection::vec(envelope_all_some(), 0 .. 100))| { | ||
| sort::timestamp(&mut envelopes).sort(); | ||
| assert!(is_sorted(&envelopes), "envelopes were not sorted") | ||
| }); | ||
| } | ||
| } | ||
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
30 changes: 30 additions & 0 deletions
30
xmtp_api_d14n/src/protocol/traits/dependency_resolution.rs
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,30 @@ | ||
| use xmtp_common::{MaybeSend, MaybeSync}; | ||
|
|
||
| use crate::protocol::{Envelope, EnvelopeError, types::MissingEnvelope}; | ||
|
|
||
| #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] | ||
| #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] | ||
| pub trait ResolveDependencies: MaybeSend + MaybeSync { | ||
| type ResolvedEnvelope: Envelope<'static> + MaybeSend + MaybeSync; | ||
| /// Resolve dependencies, starting with a list of dependencies. Should try to resolve | ||
| /// all dependents after `dependency`, if `Dependency` is missing as well. | ||
| /// * Once resolved, these dependencies may have missing dependencies of their own. | ||
| /// # Returns | ||
| /// * `Vec<Self::ResolvedEnvelope>`: The list of envelopes which were resolved. | ||
| async fn resolve( | ||
| &mut self, | ||
| missing: Vec<MissingEnvelope>, | ||
| ) -> Result<Vec<Self::ResolvedEnvelope>, EnvelopeError>; | ||
| } | ||
|
|
||
| /// A resolver that does not even attempt to try and get dependencies | ||
| pub struct NoopResolver; | ||
|
|
||
| #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] | ||
| #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] | ||
| impl ResolveDependencies for NoopResolver { | ||
| type ResolvedEnvelope = (); | ||
| async fn resolve(&mut self, _: Vec<MissingEnvelope>) -> Result<Vec<()>, EnvelopeError> { | ||
| Ok(vec![]) | ||
| } | ||
| } |
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.
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.
why can't we proptest in wasm?
Uh oh!
There was an error while loading. Please reload this page.
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.
proptest exists in wasm, but they dont support the fork feature for wasm so had to turn it off