Skip to content

Commit

Permalink
fix: add new validators on become validator
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Oct 9, 2024
1 parent 355b181 commit b0e0dbd
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 35 deletions.
28 changes: 16 additions & 12 deletions chain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use clap_verbosity_flag::LevelFilter;
use deadpool_diesel::postgres::Object;
use namada_sdk::time::DateTimeUtc;
use orm::migrations::run_migrations;
use orm::validators::ValidatorInsertDb;
use shared::block::Block;
use shared::block_result::BlockResult;
use shared::checksums::Checksums;
Expand All @@ -28,6 +27,7 @@ use shared::crawler_state::ChainCrawlerState;
use shared::error::{AsDbError, AsRpcError, ContextDbInteractError, MainError};
use shared::id::Id;
use shared::token::Token;
use shared::validator::ValidatorSet;
use tendermint_rpc::HttpClient;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;
Expand Down Expand Up @@ -180,6 +180,12 @@ async fn crawling_fn(
let proposals_votes = block.governance_votes();
tracing::info!("Creating {} governance votes...", proposals_votes.len());

let validators = block.validators();
let validator_set = ValidatorSet {
validators: validators.clone(),
epoch,
};

let addresses = block.bond_addresses();
let bonds = query_bonds(&client, addresses).await.into_rpc_error()?;
tracing::info!("Updating bonds for {} addresses", bonds.len());
Expand Down Expand Up @@ -246,6 +252,11 @@ async fn crawling_fn(
proposals_votes,
)?;

repository::pos::upsert_validators(
transaction_conn,
validator_set,
)?;

// We first remove all the bonds and then insert the new ones
repository::pos::clear_bonds(
transaction_conn,
Expand Down Expand Up @@ -312,16 +323,15 @@ async fn initial_query(
let pipeline_length = namada_service::query_pipeline_length(client)
.await
.into_rpc_error()?;
// We need to add pipeline_length to the epoch as it is possible to bond in advance
let validators_set = namada_service::get_validator_set_at_epoch(
// We need to add pipeline_length to the epoch as it is possible to bond in
// advance
let validator_set = namada_service::get_validator_set_at_epoch(
client,
epoch + pipeline_length as u32,
)
.await
.into_rpc_error()?;

tracing::info!("Validator set length: {}", validators_set.validators.len());

tracing::info!("Querying bonds and unbonds...");
let (bonds, unbonds) = query_all_bonds_and_unbonds(client, None, None)
.await
Expand Down Expand Up @@ -373,15 +383,9 @@ async fn initial_query(
proposals_votes,
)?;

let validators_dbo = &validators_set
.validators
.into_iter()
.map(ValidatorInsertDb::from_validator)
.collect::<Vec<_>>();

repository::pos::upsert_validators(
transaction_conn,
validators_dbo,
validator_set,
)?;

repository::pos::insert_bonds(transaction_conn, bonds)?;
Expand Down
10 changes: 8 additions & 2 deletions chain/src/repository/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use shared::block::Epoch;
use shared::bond::Bonds;
use shared::id::Id;
use shared::unbond::{UnbondAddresses, Unbonds};
use shared::validator::ValidatorMetadataChange;
use shared::validator::{ValidatorMetadataChange, ValidatorSet};

pub fn clear_bonds(
transaction_conn: &mut PgConnection,
Expand Down Expand Up @@ -202,8 +202,14 @@ pub fn update_validator_metadata(

pub fn upsert_validators(
transaction_conn: &mut PgConnection,
validators_db: &Vec<ValidatorInsertDb>,
validators_set: ValidatorSet,
) -> anyhow::Result<()> {
let validators_db = &validators_set
.validators
.into_iter()
.map(ValidatorInsertDb::from_validator)
.collect::<Vec<_>>();

diesel::insert_into(validators::table)
.values::<&Vec<ValidatorInsertDb>>(validators_db)
.on_conflict(validators::columns::namada_address)
Expand Down
2 changes: 1 addition & 1 deletion chain/src/services/namada.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ pub async fn get_validator_set_at_epoch(
})
})
.buffer_unordered(100)
.try_collect::<Vec<_>>()
.try_collect::<HashSet<_>>()
.await?;

Ok(ValidatorSet { validators, epoch })
Expand Down
54 changes: 45 additions & 9 deletions orm/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,75 @@
// @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 = "crawler_name"))]
pub struct CrawlerName;

#[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 = "token_type"))]
pub struct TokenType;

#[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
4 changes: 4 additions & 0 deletions orm/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum TransactionKindDb {
ChangeMetadata,
ChangeCommission,
RevealPk,
BecomeValidator,
Unknown,
}

Expand Down Expand Up @@ -54,6 +55,9 @@ impl From<TransactionKind> for TransactionKindDb {
TransactionKindDb::ChangeCommission
}
TransactionKind::RevealPk(_) => TransactionKindDb::RevealPk,
TransactionKind::BecomeValidator(_) => {
TransactionKindDb::BecomeValidator
}
TransactionKind::Unknown => TransactionKindDb::Unknown,
}
}
Expand Down
4 changes: 3 additions & 1 deletion pos/src/services/namada.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use anyhow::Context;
use futures::{StreamExt, TryStreamExt};
use namada_core::chain::Epoch as NamadaSdkEpoch;
Expand Down Expand Up @@ -83,7 +85,7 @@ pub async fn get_validator_set_at_epoch(
})
})
.buffer_unordered(100)
.try_collect::<Vec<_>>()
.try_collect::<HashSet<_>>()
.await?;

Ok(ValidatorSet { validators, epoch })
Expand Down
34 changes: 33 additions & 1 deletion shared/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::transaction::{
};
use crate::unbond::UnbondAddresses;
use crate::utils::BalanceChange;
use crate::validator::ValidatorMetadataChange;
use crate::validator::{Validator, ValidatorMetadataChange, ValidatorState};
use crate::vote::GovernanceVote;

pub type Epoch = u32;
Expand Down Expand Up @@ -490,6 +490,38 @@ impl Block {
Some(recv_msg)
}

pub fn validators(&self) -> HashSet<Validator> {
self.transactions
.iter()
.flat_map(|(_, txs)| txs)
.filter(|tx| {
tx.data.is_some()
&& tx.exit_code == TransactionExitStatus::Applied
})
.filter_map(|tx| match &tx.kind {
TransactionKind::BecomeValidator(data) => {
let data = data.clone().unwrap();
Some(Validator {
address: Id::from(data.address),
voting_power: "0".to_string(),
max_commission: data
.max_commission_rate_change
.to_string(),
commission: data.commission_rate.to_string(),
name: data.name,
email: Some(data.email),
description: data.description,
website: data.website,
discord_handler: data.discord_handle,
avatar: data.avatar,
state: ValidatorState::Inactive,
})
}
_ => None,
})
.collect()
}

pub fn bond_addresses(&self) -> HashSet<BondAddresses> {
self.transactions
.iter()
Expand Down
9 changes: 5 additions & 4 deletions shared/src/checksums.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bimap::BiMap;
use namada_sdk::tx::{
TX_BOND_WASM, TX_CHANGE_COMMISSION_WASM, TX_CHANGE_METADATA_WASM,
TX_CLAIM_REWARDS_WASM, TX_IBC_WASM, TX_INIT_PROPOSAL, TX_REDELEGATE_WASM,
TX_REVEAL_PK, TX_TRANSFER_WASM, TX_UNBOND_WASM, TX_VOTE_PROPOSAL,
TX_WITHDRAW_WASM,
TX_BECOME_VALIDATOR_WASM, TX_BOND_WASM, TX_CHANGE_COMMISSION_WASM,
TX_CHANGE_METADATA_WASM, TX_CLAIM_REWARDS_WASM, TX_IBC_WASM,
TX_INIT_PROPOSAL, TX_REDELEGATE_WASM, TX_REVEAL_PK, TX_TRANSFER_WASM,
TX_UNBOND_WASM, TX_VOTE_PROPOSAL, TX_WITHDRAW_WASM,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -43,6 +43,7 @@ impl Checksums {
TX_CHANGE_METADATA_WASM.to_string(),
TX_CHANGE_COMMISSION_WASM.to_string(),
TX_IBC_WASM.to_string(),
TX_BECOME_VALIDATOR_WASM.to_string(),
]
}
}
14 changes: 12 additions & 2 deletions shared/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use namada_sdk::masp::ShieldedTransfer;
use namada_sdk::token::Transfer;
use namada_sdk::uint::Uint;
use namada_tx::data::pos::{
Bond, ClaimRewards, CommissionChange, MetaDataChange, Redelegation, Unbond,
Withdraw,
BecomeValidator, Bond, ClaimRewards, CommissionChange, MetaDataChange,
Redelegation, Unbond, Withdraw,
};
use namada_tx::data::{compute_inner_tx_hash, TxType};
use namada_tx::either::Either;
Expand Down Expand Up @@ -47,6 +47,7 @@ pub enum TransactionKind {
MetadataChange(Option<MetaDataChange>),
CommissionChange(Option<CommissionChange>),
RevealPk(Option<RevealPkData>),
BecomeValidator(Option<BecomeValidator>),
Unknown,
}

Expand Down Expand Up @@ -162,6 +163,15 @@ impl TransactionKind {
};
TransactionKind::IbcMsgTransfer(data.map(IbcMessage))
}
"tx_become_validator" => {
let data =
if let Ok(data) = BecomeValidator::try_from_slice(data) {
Some(data)
} else {
None
};
TransactionKind::BecomeValidator(data)
}
_ => {
tracing::warn!("Unknown transaction kind: {}", tx_kind_name);
TransactionKind::Unknown
Expand Down
8 changes: 5 additions & 3 deletions shared/src/validator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use fake::faker::company::en::{CatchPhrase, CompanyName};
use fake::faker::internet::en::{DomainSuffix, SafeEmail, Username};
use fake::Fake;
Expand All @@ -9,7 +11,7 @@ use crate::id::Id;

pub type VotingPower = String;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum ValidatorState {
Consensus,
BelowCapacity,
Expand Down Expand Up @@ -37,11 +39,11 @@ impl From<NamadaValidatorState> for ValidatorState {

#[derive(Debug, Clone)]
pub struct ValidatorSet {
pub validators: Vec<Validator>,
pub validators: HashSet<Validator>,
pub epoch: Epoch,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Validator {
pub address: Id,
pub voting_power: VotingPower,
Expand Down
4 changes: 4 additions & 0 deletions webserver/src/response/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum TransactionKind {
ChangeCommission,
RevealPk,
IbcMsgTransfer,
BecomeValidator,
Unknown,
}

Expand Down Expand Up @@ -119,6 +120,9 @@ impl From<TransactionKindDb> for TransactionKind {
TransactionKindDb::IbcMsgTransfer => {
TransactionKind::IbcMsgTransfer
}
TransactionKindDb::BecomeValidator => {
TransactionKind::BecomeValidator
}
}
}
}
Expand Down

0 comments on commit b0e0dbd

Please sign in to comment.