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

bug: bonds do not clear when unbonding whole amount #97

Merged
merged 1 commit into from
Aug 9, 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
24 changes: 23 additions & 1 deletion chain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use shared::checksums::Checksums;
use shared::crawler::crawl;
use shared::crawler_state::ChainCrawlerState;
use shared::error::{AsDbError, AsRpcError, ContextDbInteractError, MainError};
use shared::id::Id;
use tendermint_rpc::HttpClient;
use tokio::time::sleep;
use tracing::Level;
Expand Down Expand Up @@ -181,6 +182,20 @@ async fn crawling_fn(
let bonds = query_bonds(&client, addresses).await.into_rpc_error()?;
tracing::info!("Updating bonds for {} addresses", bonds.len());

let bonds_updates = bonds
.iter()
.cloned()
.filter_map(|(_, bond)| bond)
.collect::<Vec<_>>();

let removed_bonds_addresses = bonds
.iter()
.cloned()
.filter_map(|(addr, bond)| match bond {
Some(_) => None,
None => Some(addr),
})
.collect::<Vec<Id>>();
let addresses = block.unbond_addresses();
let unbonds = namada_service::query_unbonds(&client, addresses)
.await
Expand Down Expand Up @@ -226,7 +241,14 @@ async fn crawling_fn(
proposals_votes,
)?;

repository::pos::insert_bonds(transaction_conn, bonds)?;
// We first remove all the bonds that are not in the storage
// anymore
repository::pos::clear_bonds(
transaction_conn,
removed_bonds_addresses,
)?;
repository::pos::insert_bonds(transaction_conn, bonds_updates)?;

repository::pos::insert_unbonds(transaction_conn, unbonds)?;
repository::pos::remove_withdraws(
transaction_conn,
Expand Down
17 changes: 17 additions & 0 deletions chain/src/repository/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ use shared::id::Id;
use shared::unbond::{UnbondAddresses, Unbonds};
use shared::validator::ValidatorMetadataChange;

pub fn clear_bonds(
transaction_conn: &mut PgConnection,
addresses: Vec<Id>,
) -> anyhow::Result<()> {
let addresses = addresses
.into_iter()
.map(|a| a.to_string())
.collect::<Vec<_>>();

diesel::delete(bonds::table)
.filter(bonds::address.eq_any(addresses))
.execute(transaction_conn)
.context("Failed to remove bonds from db")?;

anyhow::Ok(())
}

pub fn insert_bonds(
transaction_conn: &mut PgConnection,
bonds: Bonds,
Expand Down
24 changes: 18 additions & 6 deletions chain/src/services/namada.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,27 @@ pub async fn query_next_governance_id(
pub async fn query_bonds(
client: &HttpClient,
addresses: HashSet<BondAddresses>,
) -> anyhow::Result<Bonds> {
) -> anyhow::Result<Vec<(Id, Option<Bond>)>> {
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, _) =
query_all_bonds_and_unbonds(client, Some(source), Some(target))
.await
.context("Failed to query all bonds and unbonds")
.ok()?;
let (bonds_res, _) = query_all_bonds_and_unbonds(
client,
Some(source.clone()),
Some(target),
)
.await
.context("Failed to query all bonds and unbonds")
.ok()?;

let bonds = if !bonds_res.is_empty() {
bonds_res
.into_iter()
.map(|bond| (source.clone(), Some(bond)))
.collect::<Vec<_>>()
} else {
vec![(source, None)]
};

Some(bonds)
})
Expand Down
Loading