-
Notifications
You must be signed in to change notification settings - Fork 24
refactor: use tokio::watch for allocations #393
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 1 commit
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
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 |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| // Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| use eventuals::{Eventual, EventualExt}; | ||
| use std::collections::HashMap; | ||
| use std::sync::Arc; | ||
| use thegraph_core::{Address, ChainId}; | ||
|
|
@@ -18,31 +17,19 @@ use crate::prelude::{Allocation, AttestationSigner}; | |
|
|
||
| /// An always up-to-date list of attestation signers, one for each of the indexer's allocations. | ||
| pub async fn attestation_signers( | ||
| indexer_allocations: Eventual<HashMap<Address, Allocation>>, | ||
| mut indexer_allocations_rx: Receiver<HashMap<Address, Allocation>>, | ||
| indexer_mnemonic: String, | ||
| chain_id: ChainId, | ||
| mut dispute_manager_rx: Receiver<Option<Address>>, | ||
| ) -> Receiver<HashMap<Address, AttestationSigner>> { | ||
| let attestation_signers_map: &'static Mutex<HashMap<Address, AttestationSigner>> = | ||
| Box::leak(Box::new(Mutex::new(HashMap::new()))); | ||
|
|
||
| // Actively listening to indexer_allocations to update allocations channel | ||
| // Temporary fix until the indexer_allocations is migrated to tokio watch | ||
| let (allocations_tx, mut allocations_rx) = | ||
| watch::channel(indexer_allocations.value_immediate().unwrap_or_default()); | ||
| indexer_allocations | ||
| .pipe(move |allocatons| { | ||
| allocations_tx | ||
| .send(allocatons) | ||
| .expect("Failed to update allocations channel"); | ||
| }) | ||
| .forever(); | ||
|
|
||
| let starter_signers_map = modify_sigers( | ||
| Arc::new(indexer_mnemonic.clone()), | ||
| chain_id, | ||
| attestation_signers_map, | ||
| allocations_rx.clone(), | ||
| indexer_allocations_rx.clone(), | ||
| dispute_manager_rx.clone(), | ||
| ) | ||
| .await; | ||
|
|
@@ -53,12 +40,12 @@ pub async fn attestation_signers( | |
| tokio::spawn(async move { | ||
| loop { | ||
| let updated_signers = select! { | ||
| Ok(())= allocations_rx.changed() =>{ | ||
| Ok(())= indexer_allocations_rx.changed() =>{ | ||
| modify_sigers( | ||
| Arc::new(indexer_mnemonic.clone()), | ||
| chain_id, | ||
| attestation_signers_map, | ||
| allocations_rx.clone(), | ||
| indexer_allocations_rx.clone(), | ||
| dispute_manager_rx.clone(), | ||
| ).await | ||
| }, | ||
|
|
@@ -67,7 +54,7 @@ pub async fn attestation_signers( | |
| Arc::new(indexer_mnemonic.clone()), | ||
| chain_id, | ||
| attestation_signers_map, | ||
| allocations_rx.clone(), | ||
| indexer_allocations_rx.clone(), | ||
| dispute_manager_rx.clone() | ||
| ).await | ||
| }, | ||
|
|
@@ -129,27 +116,27 @@ mod tests { | |
|
|
||
| #[tokio::test] | ||
| async fn test_attestation_signers_update_with_allocations() { | ||
| let (mut allocations_writer, allocations) = Eventual::<HashMap<Address, Allocation>>::new(); | ||
| let (allocations_tx, allcoations_rx) = watch::channel(HashMap::new()); | ||
|
||
| let (dispute_manager_tx, dispute_manager_rx) = watch::channel(None); | ||
| dispute_manager_tx | ||
| .send(Some(*DISPUTE_MANAGER_ADDRESS)) | ||
| .unwrap(); | ||
| let mut signers = attestation_signers( | ||
| allocations, | ||
| allcoations_rx, | ||
| (*INDEXER_OPERATOR_MNEMONIC).to_string(), | ||
| 1, | ||
| dispute_manager_rx, | ||
| ) | ||
| .await; | ||
|
|
||
| // Test that an empty set of allocations leads to an empty set of signers | ||
| allocations_writer.write(HashMap::new()); | ||
| allocations_tx.send(HashMap::new()).unwrap(); | ||
| signers.changed().await.unwrap(); | ||
| let latest_signers = signers.borrow().clone(); | ||
| assert_eq!(latest_signers, HashMap::new()); | ||
|
|
||
| // Test that writing our set of test allocations results in corresponding signers for all of them | ||
| allocations_writer.write((*INDEXER_ALLOCATIONS).clone()); | ||
| allocations_tx.send((*INDEXER_ALLOCATIONS).clone()).unwrap(); | ||
| signers.changed().await.unwrap(); | ||
| let latest_signers = signers.borrow().clone(); | ||
| assert_eq!(latest_signers.len(), INDEXER_ALLOCATIONS.len()); | ||
|
|
||
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
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.
You dont need to map to strings here.