Skip to content
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

Add rounds_router::simple_store::RoundMsgs::into_iter_including_me() #9

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/changelog.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

m_branch=m;
changelog_file=CHANGELOG.md;

# fetch master since we might be in a shallow clone
git fetch origin "$m_branch:$m_branch" --depth=1

changed=0;
for log in */"$changelog_file"; do
dir=$(dirname "$log");
# check if version changed
if git diff "$m_branch" -- "$dir/Cargo.toml" | grep -q "^-version = "; then
# check if changelog updated
if git diff --exit-code --no-patch "$m_branch" -- "$log"; then
echo "$dir version changed, but $log is not updated"
changed=1;
fi
fi
done

exit "$changed";
7 changes: 7 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,10 @@ jobs:
- name: Build on wasm32-unknown-unknown (no_std)
run:
(cd wasm/no_std && cargo build --target wasm32-unknown-unknown)

check-changelog:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Check changelogs
run: ./.github/changelog.sh
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions round-based/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v0.3.1
* Add `rounds_router::simple_store::RoundMsgs::into_iter_including_me()` [#9]

[#9] https://github.com/dfns/round-based/pull/9

## v0.3.0
* Add no_std and wasm support [#6]
* Add state machine wrapper that provides sync API to carry out the protocol defined as async function [#7]
Expand Down
2 changes: 1 addition & 1 deletion round-based/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "round-based"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Driver for MPC protocols"
Expand Down
2 changes: 1 addition & 1 deletion round-based/src/rounds_router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,6 @@ mod tests {
let round1 = rounds.add_round(Store);
let mut rounds = rounds.listen(incomings);

let _ = rounds.complete(round1).await.unwrap();
rounds.complete(round1).await.unwrap();
}
}
58 changes: 58 additions & 0 deletions round-based/src/rounds_router/simple_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,34 @@ impl<M> RoundMsgs<M> {
.chain(self.messages.iter().skip(usize::from(self.i)))
}

/// Returns iterator over received messages plus party's own message
pub fn into_iter_including_me(self, my_msg: M) -> impl Iterator<Item = M> {
struct InsertsAfter<T, It> {
offset: usize,
inner: It,
item: Option<T>,
}
impl<T, It: Iterator<Item = T>> Iterator for InsertsAfter<T, It> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.offset == 0 {
match self.item.take() {
Some(x) => Some(x),
None => self.inner.next(),
}
} else {
self.offset -= 1;
self.inner.next()
}
}
}
InsertsAfter {
offset: usize::from(self.i),
inner: self.messages.into_iter(),
item: Some(my_msg),
}
}

/// Returns iterator over messages with sender indexes
///
/// Iterator yields `(sender_index, msg_id, message)`
Expand Down Expand Up @@ -458,4 +486,34 @@ mod tests {
})
.unwrap();
}

#[test]
fn into_iter_including_me() {
let me = -10_isize;
let messages = alloc::vec![1, 2, 3];

let me_first = super::RoundMsgs {
i: 0,
ids: alloc::vec![1, 2, 3],
messages: messages.clone(),
};
let all = me_first.into_iter_including_me(me).collect::<Vec<_>>();
assert_eq!(all, [-10, 1, 2, 3]);

let me_second = super::RoundMsgs {
i: 1,
ids: alloc::vec![0, 2, 3],
messages: messages.clone(),
};
let all = me_second.into_iter_including_me(me).collect::<Vec<_>>();
assert_eq!(all, [1, -10, 2, 3]);

let me_last = super::RoundMsgs {
i: 3,
ids: alloc::vec![0, 1, 2],
messages: messages.clone(),
};
let all = me_last.into_iter_including_me(me).collect::<Vec<_>>();
assert_eq!(all, [1, 2, 3, -10]);
}
}
Loading