Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
74 changes: 44 additions & 30 deletions common/src/attestations/dispute_manager.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
// SPDX-License-Identifier: Apache-2.0

use std::time::Duration;

use eventuals::{timer, Eventual, EventualExt};
use graphql_client::GraphQLQuery;
use std::time::Duration;
use thegraph_core::Address;
use tokio::time::sleep;
use tokio::sync::watch::{self, Receiver};
use tokio::time::{self, sleep};
use tracing::warn;

use crate::subgraph_client::SubgraphClient;
Expand All @@ -25,27 +24,44 @@ struct DisputeManager;
pub fn dispute_manager(
network_subgraph: &'static SubgraphClient,
interval: Duration,
) -> Eventual<Address> {
timer(interval).map_with_retry(
move |_| async move {
let response = network_subgraph
.query::<DisputeManager, _>(dispute_manager::Variables {})
.await
.map_err(|e| e.to_string())?;

response.map_err(|e| e.to_string()).and_then(|data| {
data.graph_network
.map(|network| network.dispute_manager)
.ok_or_else(|| "Network 1 not found in network subgraph".to_string())
})
},
move |err: String| {
warn!("Failed to query dispute manager: {}", err);

// Sleep for a bit before we retry
sleep(interval.div_f32(2.0))
},
)
) -> Receiver<Option<Address>> {
let (tx, rx) = watch::channel(None);
tokio::spawn(async move {
let mut time_interval = time::interval(interval);
time_interval.set_missed_tick_behavior(time::MissedTickBehavior::Skip);
loop {
time_interval.tick().await;

let result = async {
let response = network_subgraph
.query::<DisputeManager, _>(dispute_manager::Variables {})
.await
.map_err(|e| e.to_string())?;

response.map_err(|e| e.to_string()).and_then(|data| {
data.graph_network
.map(|network| network.dispute_manager)
.ok_or_else(|| "Network 1 not found in network subgraph".to_string())
})
}
.await;

match result {
Ok(address) => {
if tx.send(Some(address)).is_err() {
// stopping
break;
}
}
Err(err) => {
warn!("Failed to query dispute manager for network: {}", err);
// Sleep for a bit before we retry
sleep(interval.div_f32(2.0)).await;
}
}
}
});
rx
}

#[cfg(test)]
Expand Down Expand Up @@ -100,10 +116,8 @@ mod test {
let (network_subgraph, _mock_server) = setup_mock_network_subgraph().await;

let dispute_manager = dispute_manager(network_subgraph, Duration::from_secs(60));

assert_eq!(
dispute_manager.value().await.unwrap(),
*DISPUTE_MANAGER_ADDRESS
);
sleep(Duration::from_millis(50)).await;
let result = *dispute_manager.borrow();
assert_eq!(result.unwrap(), *DISPUTE_MANAGER_ADDRESS);
}
}
119 changes: 85 additions & 34 deletions common/src/attestations/signers.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
// SPDX-License-Identifier: Apache-2.0

use eventuals::{join, Eventual, EventualExt};
use eventuals::{Eventual, EventualExt, EventualWriter};
use std::collections::HashMap;
use std::sync::Arc;
use thegraph_core::{Address, ChainId};
use tokio::sync::Mutex;
use tokio::sync::watch;
use tokio::{
select,
sync::{watch::Receiver, Mutex},
};
use tracing::warn;

use crate::prelude::{Allocation, AttestationSigner};
Expand All @@ -15,47 +19,92 @@ pub fn attestation_signers(
indexer_allocations: Eventual<HashMap<Address, Allocation>>,
indexer_mnemonic: String,
chain_id: ChainId,
dispute_manager: Eventual<Address>,
mut dispute_manager_rx: Receiver<Option<Address>>,
) -> Eventual<HashMap<Address, AttestationSigner>> {
let attestation_signers_map: &'static Mutex<HashMap<Address, AttestationSigner>> =
Box::leak(Box::new(Mutex::new(HashMap::new())));

let indexer_mnemonic = Arc::new(indexer_mnemonic);

// Whenever the indexer's active or recently closed allocations change, make sure
// we have attestation signers for all of them
join((indexer_allocations, dispute_manager)).map(move |(allocations, dispute_manager)| {
let indexer_mnemonic = indexer_mnemonic.clone();
async move {
let mut signers = attestation_signers_map.lock().await;

// Remove signers for allocations that are no longer active or recently closed
signers.retain(|id, _| allocations.contains_key(id));

// Create signers for new allocations
for (id, allocation) in allocations.iter() {
if !signers.contains_key(id) {
let signer = AttestationSigner::new(
&indexer_mnemonic,
allocation,
let (mut signers_writer, signers_reader) =
Eventual::<HashMap<Address, AttestationSigner>>::new();

tokio::spawn(async move {
//listening to the allocation eventual and converting them to reciever
//using pipe for updation
//for temporary pupose only
let (allocations_tx, mut allocations_rx) =
watch::channel(indexer_allocations.value().await.unwrap());
let _p1 = indexer_allocations.pipe(move |allocatons| {
let _ = allocations_tx.send(allocatons);
});

loop {
select! {
Ok(_)= allocations_rx.changed() =>{
signers_writer = modify_sigers(
Arc::new(indexer_mnemonic.clone()),
chain_id,
dispute_manager,
);
if let Err(e) = signer {
warn!(
"Failed to establish signer for allocation {}, deployment {}, createdAtEpoch {}: {}",
allocation.id, allocation.subgraph_deployment.id,
allocation.created_at_epoch, e
);
} else {
signers.insert(*id, signer.unwrap());
}
attestation_signers_map,
allocations_rx.clone(),
dispute_manager_rx.clone(),
signers_writer).await;
},
Ok(_)= dispute_manager_rx.changed() =>{
signers_writer = modify_sigers(Arc::new(indexer_mnemonic.clone()),
chain_id,
attestation_signers_map,
allocations_rx.clone(),
dispute_manager_rx.clone(),
signers_writer).await;
},
else=>{
//something is wrong
break;
}
}
}
});

signers.clone()
signers_reader
}
async fn modify_sigers(
indexer_mnemonic: Arc<String>,
chain_id: ChainId,
attestation_signers_map: &'static Mutex<HashMap<Address, AttestationSigner>>,
allocations_rx: Receiver<HashMap<Address, Allocation>>,
dispute_manager_rx: Receiver<Option<Address>>,
mut signers_writer: EventualWriter<HashMap<Address, AttestationSigner>>,
) -> EventualWriter<HashMap<Address, AttestationSigner>> {
let mut signers = attestation_signers_map.lock().await;
let allocations = allocations_rx.borrow().clone();
let dispute_manager = *dispute_manager_rx.borrow();
if dispute_manager.is_none() {
return signers_writer;
}
let dispute_manager = dispute_manager.unwrap();
// Remove signers for allocations that are no longer active or recently closed
signers.retain(|id, _| allocations.contains_key(id));

// Create signers for new allocations
for (id, allocation) in allocations.iter() {
if !signers.contains_key(id) {
let signer =
AttestationSigner::new(&indexer_mnemonic, allocation, chain_id, dispute_manager);
if let Err(e) = signer {
warn!(
"Failed to establish signer for allocation {}, deployment {}, createdAtEpoch {}: {}",
allocation.id, allocation.subgraph_deployment.id,
allocation.created_at_epoch, e
);
} else {
signers.insert(*id, signer.unwrap());
}
}
})
}

signers_writer.write(signers.clone());
signers_writer
}

#[cfg(test)]
Expand All @@ -69,9 +118,11 @@ mod tests {
#[tokio::test]
async fn test_attestation_signers_update_with_allocations() {
let (mut allocations_writer, allocations) = Eventual::<HashMap<Address, Allocation>>::new();
let (mut dispute_manager_writer, dispute_manager) = Eventual::<Address>::new();
let (dispute_manager_writer, dispute_manager) = watch::channel(None);

dispute_manager_writer.write(*DISPUTE_MANAGER_ADDRESS);
dispute_manager_writer
.send(Some(*DISPUTE_MANAGER_ADDRESS))
.unwrap();

let signers = attestation_signers(
allocations,
Expand Down
Loading