Skip to content

Commit

Permalink
feat: unbonds withdraws improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Jun 26, 2024
1 parent 45285ea commit 6660703
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 50 deletions.
7 changes: 7 additions & 0 deletions chain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ async fn crawling_fn(
.into_rpc_error()?;
tracing::info!("Updating unbonds for {} addresses", unbonds.len());

let withdraw_addreses = block.withdraw_addresses();

let revealed_pks = block.revealed_pks();
tracing::info!(
"Updating revealed pks for {} addresses",
Expand Down Expand Up @@ -216,6 +218,11 @@ async fn crawling_fn(

repository::pos::insert_bonds(transaction_conn, bonds)?;
repository::pos::insert_unbonds(transaction_conn, unbonds)?;
repository::pos::remove_withdraws(
transaction_conn,
epoch,
withdraw_addreses,
)?;

repository::pos::delete_claimed_rewards(
transaction_conn,
Expand Down
39 changes: 37 additions & 2 deletions chain/src/repository/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ use std::collections::HashSet;
use anyhow::Context;
use diesel::upsert::excluded;
use diesel::{
ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, SelectableHelper,
BoolExpressionMethods, ExpressionMethods, PgConnection, QueryDsl,
RunQueryDsl, SelectableHelper,
};
use orm::bond::BondInsertDb;
use orm::schema::{bonds, pos_rewards, unbonds, validators};
use orm::unbond::UnbondInsertDb;
use orm::validators::{ValidatorDb, ValidatorUpdateMetadataDb};
use shared::block::Epoch;
use shared::bond::Bonds;
use shared::id::Id;
use shared::unbond::Unbonds;
use shared::unbond::{UnbondAddresses, Unbonds};
use shared::validator::ValidatorMetadataChange;

pub fn insert_bonds(
Expand Down Expand Up @@ -91,6 +93,39 @@ pub fn insert_unbonds(
anyhow::Ok(())
}

pub fn remove_withdraws(
transaction_conn: &mut PgConnection,
current_epoch: Epoch,
unbond_addresses: Vec<UnbondAddresses>,
) -> anyhow::Result<()> {
let sources = unbond_addresses
.iter()
.map(|unbond| unbond.source.to_string())
.collect::<Vec<String>>();

let validators = unbond_addresses
.iter()
.map(|unbond| unbond.validator.to_string())
.collect::<Vec<String>>();

diesel::delete(
unbonds::table.filter(
unbonds::columns::address
.eq_any(sources)
.and(unbonds::columns::validator_id.eq_any(
validators::table.select(validators::columns::id).filter(
validators::columns::namada_address.eq_any(validators),
),
))
.and(unbonds::columns::withdraw_epoch.le(current_epoch as i32)),
),
)
.execute(transaction_conn)
.context("Failed to remove withdraws from db")?;

anyhow::Ok(())
}

pub fn delete_claimed_rewards(
transaction_conn: &mut PgConnection,
reward_claimers: HashSet<Id>,
Expand Down
5 changes: 3 additions & 2 deletions chain/src/services/namada.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,9 @@ pub async fn query_unbonds(
withdraw_epoch.0 as Epoch,
));

// We have to merge the unbonds with the same withdraw epoch into one
// otherwise we can't insert them into the db
// We have to merge the unbonds with the same withdraw
// epoch into one otherwise we can't
// insert them into the db
match record {
Some(r) => {
*r = r.checked_add(&Amount::from(amount)).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion orm/src/balances.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::str::FromStr;

use bigdecimal::BigDecimal;
use diesel::{Insertable, Queryable, Selectable};
use shared::balance::Balance;
use std::str::FromStr;

use crate::schema::balances;

Expand Down
7 changes: 3 additions & 4 deletions orm/src/group_by_macros.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use diesel::allow_columns_to_appear_in_same_group_by_clause;
use diesel::expression::{SqlLiteral, ValidGrouping};

use crate::schema::{bonds, unbonds, validators};
use diesel::{
allow_columns_to_appear_in_same_group_by_clause,
expression::{SqlLiteral, ValidGrouping},
};

allow_columns_to_appear_in_same_group_by_clause!(
bonds::address,
Expand Down
42 changes: 35 additions & 7 deletions orm/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
// @generated automatically by Diesel CLI.

pub mod sql_types {
#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[diesel(postgres_type(name = "governance_kind"))]
pub struct GovernanceKind;

#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[diesel(postgres_type(name = "governance_result"))]
pub struct GovernanceResult;

#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[diesel(postgres_type(name = "governance_tally_type"))]
pub struct GovernanceTallyType;

#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[diesel(postgres_type(name = "transaction_kind"))]
pub struct TransactionKind;

#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[diesel(postgres_type(name = "transaction_result"))]
pub struct TransactionResult;

#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[diesel(postgres_type(name = "validator_state"))]
pub struct ValidatorState;

#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[diesel(postgres_type(name = "vote_kind"))]
pub struct VoteKind;
}
Expand Down
5 changes: 2 additions & 3 deletions orm/src/validators.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::str::FromStr;

use diesel::{
sql_types::Nullable, AsChangeset, Insertable, Queryable, Selectable,
};
use diesel::sql_types::Nullable;
use diesel::{AsChangeset, Insertable, Queryable, Selectable};
use serde::{Deserialize, Serialize};
use shared::validator::{Validator, ValidatorState};

Expand Down
32 changes: 19 additions & 13 deletions parameters/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use orm::parameters::ParametersInsertDb;
use orm::schema::{chain_parameters, gas_price};
use parameters::app_state::AppState;
use parameters::config::AppConfig;
use parameters::services::namada as namada_service;
use parameters::services::tendermint as tendermint_service;
use parameters::services::{
namada as namada_service, tendermint as tendermint_service,
};
use shared::error::{AsDbError, AsRpcError, ContextDbInteractError, MainError};
use tendermint_rpc::HttpClient;
use tokio::signal;
Expand Down Expand Up @@ -92,34 +93,39 @@ async fn main() -> anyhow::Result<()> {
conn.build_transaction().read_write().run(
|transaction_conn| {
diesel::insert_into(chain_parameters::table)
.values(ParametersInsertDb::from((parameters, genesis, EPOCH_SWITCH_BLOCKS_DELAY)))
.on_conflict(
chain_parameters::chain_id,
)
.values(ParametersInsertDb::from((
parameters,
genesis,
EPOCH_SWITCH_BLOCKS_DELAY,
)))
.on_conflict(chain_parameters::chain_id)
.do_update()
.set(
chain_parameters::apr
.eq(excluded(chain_parameters::apr)),
)
.execute(transaction_conn)
.context(
"Failed to update chain_parameters state in db",
"Failed to update chain_parameters state \
in db",
)?;

diesel::insert_into(gas_price::table)
.values(gas_price.iter().cloned().map(GasPriceDb::from).collect::<Vec<GasPriceDb>>())
.on_conflict(
gas_price::token,
.values(
gas_price
.iter()
.cloned()
.map(GasPriceDb::from)
.collect::<Vec<GasPriceDb>>(),
)
.on_conflict(gas_price::token)
.do_update()
.set(
gas_price::amount
.eq(excluded(gas_price::amount)),
)
.execute(transaction_conn)
.context(
"Failed to update gas price in db",
)?;
.context("Failed to update gas price in db")?;

anyhow::Ok(())
},
Expand Down
2 changes: 1 addition & 1 deletion parameters/src/services/tendermint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use shared::genesis::{Genesis, GenesisParams, GenesisRequest};
pub async fn query_genesis(client: &HttpClient) -> anyhow::Result<Genesis> {
let genesis_params: GenesisParams =
client.perform(GenesisRequest).await?.genesis;

Ok(Genesis::from(genesis_params))
}
4 changes: 2 additions & 2 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.78.0"
channel = "1.78.1"
components = ["rustc", "cargo", "rust-std", "rust-docs", "rls", "rust-src", "rust-analysis", "clippy", "rustfmt"]
targets = []
targets = []
4 changes: 2 additions & 2 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ remove_nested_parens = true
reorder_impl_items = true
reorder_imports = true
reorder_modules = true
required_version = "1.7.0"
required_version = "1.7.1"
skip_children = false
space_after_colon = true
space_before_colon = false
Expand All @@ -62,4 +62,4 @@ use_small_heuristics = "Default"
use_try_shorthand = false
version = "Two"
where_single_line = false
wrap_comments = true
wrap_comments = true
10 changes: 10 additions & 0 deletions shared/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,16 @@ impl Block {
validator: Id::from(validator_address),
})
}
_ => None,
})
.collect()
}

pub fn withdraw_addresses(&self) -> Vec<UnbondAddresses> {
self.transactions
.iter()
.flat_map(|(_, txs)| txs)
.filter_map(|tx| match &tx.kind {
TransactionKind::Withdraw(data) => {
let withdraw_data = data.clone();

Expand Down
5 changes: 3 additions & 2 deletions shared/src/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use serde::{Deserialize, Serialize};
use tendermint::{chain, consensus, validator, AppHash, Time};
use tendermint_rpc::dialect::LatestDialect;
use tendermint_rpc::request::RequestMessage;
use tendermint_rpc::{
dialect::LatestDialect, request::RequestMessage, Method,
Request as TendermintRequest, Response as TendermintResponse,
Method, Request as TendermintRequest, Response as TendermintResponse,
SimpleRequest,
};

Expand Down
13 changes: 6 additions & 7 deletions shared/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ use std::collections::HashMap;
use std::fmt::Display;

use namada_governance::{InitProposalData, VoteProposalData};
use namada_sdk::borsh::BorshDeserialize;
use namada_sdk::key::common::PublicKey;
use namada_sdk::masp::ShieldedTransfer;
use namada_sdk::token::TransparentTransfer;
use namada_sdk::uint::Uint;
use namada_sdk::{borsh::BorshDeserialize, key::common::PublicKey};
use namada_tx::data::{
pos::{
Bond, ClaimRewards, CommissionChange, MetaDataChange, Redelegation,
Unbond, Withdraw,
},
TxType,
use namada_tx::data::pos::{
Bond, ClaimRewards, CommissionChange, MetaDataChange, Redelegation, Unbond,
Withdraw,
};
use namada_tx::data::TxType;
use namada_tx::{Section, Tx};
use serde::Serialize;

Expand Down
4 changes: 3 additions & 1 deletion swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ components:
type: string
Unbond:
type: object
required: [validator, amount, withdrawEpoch, withdrawTime]
required: [validator, amount, withdrawEpoch, withdrawTime, currentTime]
properties:
validator:
$ref: '#/components/schemas/Validator'
Expand All @@ -655,6 +655,8 @@ components:
type: string
withdrawTime:
type: string
currentTime:
type: string
Withdraw:
type: object
required: [amount, withdrawEpoch]
Expand Down
4 changes: 2 additions & 2 deletions webserver/src/repository/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ impl PosRepositoryTrait for PosRepository {
validators::table
.inner_join(unbonds::table)
.filter(unbonds::dsl::address.eq(address))
.group_by((unbonds::address, validators::id, sql::<Integer>(&format!("CASE WHEN withdraw_epoch < {} THEN 0 ELSE withdraw_epoch END", current_epoch))))
.group_by((unbonds::address, validators::id, sql::<Integer>(&format!("CASE WHEN withdraw_epoch <= {} THEN 0 ELSE withdraw_epoch END", current_epoch))))
.select((
unbonds::address,
validators::all_columns,
sum(unbonds::raw_amount),
sql::<Integer>(&format!("CASE WHEN MIN(withdraw_epoch) < {} THEN 0 ELSE MAX(withdraw_epoch) END AS withdraw_epoch", current_epoch))))
sql::<Integer>(&format!("CASE WHEN MIN(withdraw_epoch) <= {} THEN 0 ELSE MAX(withdraw_epoch) END AS withdraw_epoch", current_epoch))))
.paginate(page)
.load_and_count_pages::<(String, ValidatorDb, Option<BigDecimal>, i32)>(conn)

Expand Down
2 changes: 2 additions & 0 deletions webserver/src/response/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub struct Unbond {
pub validator: ValidatorWithId,
pub withdraw_epoch: String,
pub withdraw_time: String,
pub current_time: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -196,6 +197,7 @@ impl Unbond {
validator: ValidatorWithId::from(db_validator),
withdraw_epoch: withdraw_epoch.to_string(),
withdraw_time: withdraw_time.to_string(),
current_time: time_now.to_string(),
}
}
}
Expand Down
Loading

0 comments on commit 6660703

Please sign in to comment.