diff --git a/chain/src/main.rs b/chain/src/main.rs index 6b48fad6..ff4a190f 100644 --- a/chain/src/main.rs +++ b/chain/src/main.rs @@ -185,17 +185,18 @@ async fn crawling_fn( let bonds_updates = bonds .iter() .cloned() - .filter_map(|(_, bond)| bond) + .filter_map(|(_, _, bond)| bond) .collect::>(); let removed_bonds_addresses = bonds .iter() .cloned() - .filter_map(|(addr, bond)| match bond { + .filter_map(|(source, validator, bond)| match bond { Some(_) => None, - None => Some(addr), + None => Some((source, validator)), }) - .collect::>(); + .collect::>(); + let addresses = block.unbond_addresses(); let unbonds = namada_service::query_unbonds(&client, addresses) .await diff --git a/chain/src/repository/pos.rs b/chain/src/repository/pos.rs index 079d0f81..eb63489e 100644 --- a/chain/src/repository/pos.rs +++ b/chain/src/repository/pos.rs @@ -18,15 +18,21 @@ use shared::validator::ValidatorMetadataChange; pub fn clear_bonds( transaction_conn: &mut PgConnection, - addresses: Vec, + addresses: Vec<(Id, Id)>, ) -> anyhow::Result<()> { - let addresses = addresses + let (sources, validators): (Vec, Vec) = addresses .into_iter() - .map(|a| a.to_string()) - .collect::>(); + .map(|(source, validator)| (source.to_string(), validator.to_string())) + .unzip(); diesel::delete(bonds::table) - .filter(bonds::address.eq_any(addresses)) + .filter(bonds::address.eq_any(sources).and( + bonds::validator_id.eq_any( + validators::table.select(validators::columns::id).filter( + validators::columns::namada_address.eq_any(validators), + ), + ), + )) .execute(transaction_conn) .context("Failed to remove bonds from db")?; diff --git a/chain/src/services/namada.rs b/chain/src/services/namada.rs index 1acaf4d3..5c57d59d 100644 --- a/chain/src/services/namada.rs +++ b/chain/src/services/namada.rs @@ -329,14 +329,14 @@ pub async fn query_next_governance_id( pub async fn query_bonds( client: &HttpClient, addresses: HashSet, -) -> anyhow::Result)>> { +) -> anyhow::Result)>> { let nested_bonds = futures::stream::iter(addresses) .filter_map(|BondAddresses { source, target }| async move { // TODO: if this is too slow do not use query_all_bonds_and_unbonds let (bonds_res, _) = query_all_bonds_and_unbonds( client, Some(source.clone()), - Some(target), + Some(target.clone()), ) .await .context("Failed to query all bonds and unbonds") @@ -345,10 +345,10 @@ pub async fn query_bonds( let bonds = if !bonds_res.is_empty() { bonds_res .into_iter() - .map(|bond| (source.clone(), Some(bond))) + .map(|bond| (source.clone(), target.clone(), Some(bond))) .collect::>() } else { - vec![(source, None)] + vec![(source, target, None)] }; Some(bonds)