-
Notifications
You must be signed in to change notification settings - Fork 14
wip Contract event stream #495
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
Draft
alysiahuggins
wants to merge
11
commits into
main
Choose a base branch
from
contract-event-stream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
72d3fe9
add key manager eventmonitoring utilities with unit tests
alysiahuggins 3d45813
wip event monitor
alysiahuggins 9d96c5c
Merge remote-tracking branch 'origin/main' into contract-event-stream
alxiong 0bb03e5
fix compiler err
alxiong f05c7aa
Merge remote-tracking branch 'origin/main' into contract-event-stream
alxiong 7a58666
optimize provider usage and used Arc for shared ownership
alysiahuggins bd03faa
refactor Arc usage
alysiahuggins 8fcda1b
lint
alysiahuggins 1d00f64
remove _arc in naming
alysiahuggins 334f51c
removed redundant implementation
alysiahuggins 4f962ab
provided a getter for the enabled field
alysiahuggins 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
//! Committee management and querying utilities | ||
use crate::bindings::keymanager::KeyManager; | ||
use alloy::{primitives::Address, providers::Provider}; | ||
use anyhow::Result; | ||
|
||
pub struct CommitteeManager<P> { | ||
provider: P, | ||
contract_addr: Address, | ||
} | ||
|
||
impl<P: Provider> CommitteeManager<P> { | ||
pub fn new(provider: P, contract_addr: Address) -> Self { | ||
Self { | ||
provider, | ||
contract_addr, | ||
} | ||
} | ||
|
||
pub async fn get_committees_for_startup( | ||
&self, | ||
current_id: u64, | ||
previous_id: Option<u64>, | ||
) -> Result<(KeyManager::Committee, Option<KeyManager::Committee>)> { | ||
let contract = KeyManager::new(self.contract_addr, &self.provider); | ||
|
||
let current = contract.getCommitteeById(current_id).call().await?; | ||
let previous = if let Some(prev_id) = previous_id { | ||
Some(contract.getCommitteeById(prev_id).call().await?) | ||
} else { | ||
None | ||
}; | ||
|
||
Ok((current, previous)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{CommitteeMemberSol, KeyManager}; | ||
use rand::prelude::*; | ||
|
||
#[tokio::test] | ||
async fn test_get_committees_for_startup_current_only() { | ||
let (provider, contract_addr) = crate::init_test_chain().await.unwrap(); | ||
let contract = KeyManager::new(contract_addr, &provider); | ||
|
||
let manager = CommitteeManager::new(&provider, contract_addr); | ||
|
||
let rng = &mut rand::rng(); | ||
let members = (0..3) | ||
.map(|_| CommitteeMemberSol::random()) | ||
.collect::<Vec<_>>(); | ||
let timestamp = rng.random::<u64>(); | ||
|
||
// create the committee | ||
contract | ||
.setNextCommittee(timestamp, members.clone()) | ||
.send() | ||
.await | ||
.unwrap() | ||
.get_receipt() | ||
.await | ||
.unwrap(); | ||
|
||
let (current, previous) = manager.get_committees_for_startup(0, None).await.unwrap(); | ||
|
||
// verify current committee | ||
assert_eq!(current.id, 0); | ||
assert_eq!(current.effectiveTimestamp, timestamp); | ||
assert_eq!(current.members.len(), 3); | ||
|
||
// verify no previous committee | ||
assert!(previous.is_none()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_get_committees_for_startup_with_previous() { | ||
// setup test chain and deploy contract | ||
let (provider, contract_addr) = crate::init_test_chain().await.unwrap(); | ||
let contract = KeyManager::new(contract_addr, &provider); | ||
|
||
let manager = CommitteeManager::new(&provider, contract_addr); | ||
|
||
let rng = &mut rand::rng(); | ||
let first_members = (0..2) | ||
.map(|_| CommitteeMemberSol::random()) | ||
.collect::<Vec<_>>(); | ||
let first_timestamp = rng.random::<u64>(); | ||
|
||
contract | ||
.setNextCommittee(first_timestamp, first_members.clone()) | ||
.send() | ||
.await | ||
.unwrap() | ||
.get_receipt() | ||
.await | ||
.unwrap(); | ||
|
||
// create second committee (will be current) | ||
let second_members = (0..3) | ||
.map(|_| CommitteeMemberSol::random()) | ||
.collect::<Vec<_>>(); | ||
let second_timestamp = first_timestamp + 1000; // Ensure different timestamp | ||
|
||
contract | ||
.setNextCommittee(second_timestamp, second_members.clone()) | ||
.send() | ||
.await | ||
.unwrap() | ||
.get_receipt() | ||
.await | ||
.unwrap(); | ||
|
||
// test getting both current and previous committees | ||
let (current, previous) = manager | ||
.get_committees_for_startup(1, Some(0)) | ||
.await | ||
.unwrap(); | ||
|
||
// verify current committee (id=1) | ||
assert_eq!(current.id, 1); | ||
assert_eq!(current.effectiveTimestamp, second_timestamp); | ||
assert_eq!(current.members.len(), 3); | ||
|
||
// verify previous committee (id=0) | ||
assert!(previous.is_some()); | ||
let prev = previous.unwrap(); | ||
assert_eq!(prev.id, 0); | ||
assert_eq!(prev.effectiveTimestamp, first_timestamp); | ||
assert_eq!(prev.members.len(), 2); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_get_committees_for_startup_nonexistent_committee() { | ||
// setup test chain and deploy contract | ||
let (provider, contract_addr) = crate::init_test_chain().await.unwrap(); | ||
|
||
let manager = CommitteeManager::new(&provider, contract_addr); | ||
|
||
let result = manager.get_committees_for_startup(999, None).await; | ||
|
||
// should return an error | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_get_committees_for_startup_nonexistent_previous() { | ||
// setup test chain and deploy contract | ||
let (provider, contract_addr) = crate::init_test_chain().await.unwrap(); | ||
let contract = KeyManager::new(contract_addr, &provider); | ||
|
||
let manager = CommitteeManager::new(&provider, contract_addr); | ||
|
||
let rng = &mut rand::rng(); | ||
let members = (0..2) | ||
.map(|_| CommitteeMemberSol::random()) | ||
.collect::<Vec<_>>(); | ||
let timestamp = rng.random::<u64>(); | ||
|
||
contract | ||
.setNextCommittee(timestamp, members) | ||
.send() | ||
.await | ||
.unwrap() | ||
.get_receipt() | ||
.await | ||
.unwrap(); | ||
|
||
// try to get current committee with non-existent previous | ||
let result = manager.get_committees_for_startup(0, Some(999)).await; | ||
|
||
// should return an error because previous committee doesn't exist | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_get_committees_for_startup_multiple_committees() { | ||
// setup test chain and deploy contract | ||
let (provider, contract_addr) = crate::init_test_chain().await.unwrap(); | ||
let contract = KeyManager::new(contract_addr, &provider); | ||
|
||
let manager = CommitteeManager::new(&provider, contract_addr); | ||
|
||
// create multiple committees | ||
let rng = &mut rand::rng(); | ||
let base_timestamp = rng.random::<u64>(); | ||
|
||
for i in 0..5 { | ||
let members = (0..2) | ||
.map(|_| CommitteeMemberSol::random()) | ||
.collect::<Vec<_>>(); | ||
let timestamp = base_timestamp + (i as u64 * 1000); | ||
|
||
contract | ||
.setNextCommittee(timestamp, members) | ||
.send() | ||
.await | ||
.unwrap() | ||
.get_receipt() | ||
.await | ||
.unwrap(); | ||
} | ||
|
||
// test getting committee 3 with previous committee 2 | ||
let (current, previous) = manager | ||
.get_committees_for_startup(3, Some(2)) | ||
.await | ||
.unwrap(); | ||
|
||
// verify current committee (id=3) | ||
assert_eq!(current.id, 3); | ||
assert_eq!(current.effectiveTimestamp, base_timestamp + 3000); | ||
assert_eq!(current.members.len(), 2); | ||
|
||
// verify previous committee (id=2) | ||
assert!(previous.is_some()); | ||
let prev = previous.unwrap(); | ||
assert_eq!(prev.id, 2); | ||
assert_eq!(prev.effectiveTimestamp, base_timestamp + 2000); | ||
assert_eq!(prev.members.len(), 2); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_committee_manager_creation() { | ||
// setup test chain and deploy contract | ||
let (provider, contract_addr) = crate::init_test_chain().await.unwrap(); | ||
|
||
let manager = CommitteeManager::new(&provider, contract_addr); | ||
|
||
// manager should be created successfully | ||
assert_eq!(manager.contract_addr, contract_addr); | ||
} | ||
} |
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 find
struct CommitteeManager
an overkill, and i also find this function a bit unnecessary.usually wherever i need to fetch committee info, i simply have two lines, one line to get a
KeyManager
contract instance, another line to.getCommitteeById()
on it. that's it.if we use this struct, we first need to
CommitteManager::new()
, then call thisfor_startup()
.imo, the former is clearer and less indirection.
The decision to fetch "which committee" during startup could be logic that resides in
timeboost
code, not here.I vote for dropping this file.