Skip to content

Commit

Permalink
wip: improve gas estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraccaman committed Dec 20, 2024
1 parent 7db2cae commit b829015
Show file tree
Hide file tree
Showing 16 changed files with 405 additions and 61 deletions.
5 changes: 4 additions & 1 deletion orm/migrations/2024-12-20-092544_gas_eastimation/down.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
-- This file should undo anything in `up.sql`
DROP TABLE IF EXISTS gas_estimations;
DROP TABLE IF EXISTS gas_estimations;

DROP INDEX IF EXISTS wrapper_transactions_gas;
DROP INDEX IF EXISTS inner_transactions_kind;
8 changes: 7 additions & 1 deletion orm/migrations/2024-12-20-092544_gas_eastimation/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ CREATE TABLE gas_estimations (
claim_rewards INT NOT NULL,
vote_proposal INT NOT NULL,
reveal_pk INT NOT NULL,
size INT NOT NULL,
tx_size INT NOT NULL,
signatures INT NOT NULL,
CONSTRAINT fk_wrapper_id FOREIGN KEY(wrapper_id) REFERENCES wrapper_transactions(id) ON DELETE CASCADE
);

ALTER TABLE wrapper_transactions ALTER COLUMN gas_used TYPE INTEGER USING (gas_used::integer) ;

CREATE INDEX wrapper_transactions_gas ON wrapper_transactions (gas_used);

CREATE INDEX inner_transactions_kind ON inner_transactions (kind);
49 changes: 47 additions & 2 deletions orm/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::str::FromStr;

use bigdecimal::BigDecimal;
use diesel::{Insertable, Queryable, Selectable};
use shared::gas::GasPrice;
use shared::gas::{GasEstimation, GasPrice};

use crate::schema::{gas, gas_price};
use crate::schema::{gas, gas_estimations, gas_price};
use crate::transactions::TransactionKindDb;

#[derive(Clone, Queryable, Selectable)]
Expand Down Expand Up @@ -32,3 +32,48 @@ impl From<GasPrice> for GasPriceDb {
}
}
}

#[derive(Clone, Queryable, Selectable, Insertable)]
#[diesel(table_name = gas_estimations)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct GasEstimationDb {
pub wrapper_id: String,
pub transparent_transfer: i32,
pub shielded_transfer: i32,
pub shielding_transfer: i32,
pub unshielding_transfer: i32,
pub ibc_msg_transfer: i32,
pub bond: i32,
pub redelegation: i32,
pub unbond: i32,
pub withdraw: i32,
pub claim_rewards: i32,
pub vote_proposal: i32,
pub reveal_pk: i32,
pub signatures: i32,
pub tx_size: i32,
}

pub type GasEstimationInsertDb = GasEstimationDb;

impl From<GasEstimation> for GasEstimationInsertDb {
fn from(value: GasEstimation) -> Self {
Self {
wrapper_id: value.wrapper_id.to_string(),
transparent_transfer: value.transparent_transfer as i32,
shielded_transfer: value.shielded_transfer as i32,
shielding_transfer: value.shielding_transfer as i32,
unshielding_transfer: value.unshielding_transfer as i32,
ibc_msg_transfer: value.ibc_msg_transfer as i32,
bond: value.bond as i32,
redelegation: value.redelegation as i32,
unbond: value.unbond as i32,
withdraw: value.withdraw as i32,
claim_rewards: value.claim_rewards as i32,
vote_proposal: value.vote_proposal as i32,
reveal_pk: value.reveal_pk as i32,
signatures: value.signatures as i32,
tx_size: value.size as i32,
}
}
}
64 changes: 52 additions & 12 deletions orm/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,83 @@
// @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 = "ibc_status"))]
pub struct IbcStatus;

#[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 Expand Up @@ -136,7 +176,7 @@ diesel::table! {
claim_rewards -> Int4,
vote_proposal -> Int4,
reveal_pk -> Int4,
size -> Int4,
tx_size -> Int4,
signatures -> Int4,
}
}
Expand Down Expand Up @@ -291,7 +331,7 @@ diesel::table! {
block_height -> Int4,
exit_code -> TransactionResult,
atomic -> Bool,
gas_used -> Nullable<Varchar>,
gas_used -> Nullable<Int4>,
}
}

Expand Down
4 changes: 2 additions & 2 deletions orm/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub struct WrapperTransactionInsertDb {
pub fee_payer: String,
pub fee_token: String,
pub gas_limit: String,
pub gas_used: Option<String>,
pub gas_used: Option<i32>,
pub block_height: i32,
pub exit_code: TransactionResultDb,
pub atomic: bool,
Expand All @@ -129,7 +129,7 @@ impl WrapperTransactionInsertDb {
fee_payer: tx.fee.gas_payer.to_string(),
fee_token: tx.fee.gas_token.to_string(),
gas_limit: tx.fee.gas,
gas_used: tx.fee.gas_used,
gas_used: tx.fee.gas_used.map(|gas| gas as i32),
block_height: tx.block_height as i32,
exit_code: TransactionResultDb::from(tx.exit_code),
atomic: tx.atomic,
Expand Down
42 changes: 42 additions & 0 deletions shared/src/gas.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,49 @@
use crate::balance::Amount;
use crate::id::Id;

#[derive(Clone, Debug)]
pub struct GasPrice {
pub token: String,
pub amount: Amount,
}

#[derive(Clone, Debug)]
pub struct GasEstimation {
pub wrapper_id: Id,
pub transparent_transfer: u64,
pub shielded_transfer: u64,
pub shielding_transfer: u64,
pub unshielding_transfer: u64,
pub ibc_msg_transfer: u64,
pub bond: u64,
pub redelegation: u64,
pub unbond: u64,
pub withdraw: u64,
pub claim_rewards: u64,
pub vote_proposal: u64,
pub reveal_pk: u64,
pub size: u64,
pub signatures: u64,
}

impl GasEstimation {
pub fn new(tx_id: Id) -> Self {
Self {
wrapper_id: tx_id,
transparent_transfer: 0,
shielded_transfer: 0,
shielding_transfer: 0,
unshielding_transfer: 0,
ibc_msg_transfer: 0,
bond: 0,
redelegation: 0,
unbond: 0,
withdraw: 0,
claim_rewards: 0,
vote_proposal: 0,
reveal_pk: 0,
size: 0,
signatures: 0,
}
}
}
10 changes: 8 additions & 2 deletions shared/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,16 @@ impl InnerTransaction {
pub fn get_section_data_by_id(&self, section_id: Id) -> Option<Vec<u8>> {
self.extra_sections.get(&section_id).cloned()
}

pub fn was_successful(&self) -> bool {
self.exit_code == TransactionExitStatus::Applied
}
}

#[derive(Debug, Clone)]
pub struct Fee {
pub gas: String,
pub gas_used: Option<String>,
pub gas_used: Option<u64>,
pub amount_per_gas_unit: String,
pub gas_payer: Id,
pub gas_token: Id,
Expand All @@ -302,7 +306,9 @@ impl Transaction {
let wrapper_tx_id = Id::from(transaction.header_hash());
let wrapper_tx_status =
block_results.is_wrapper_tx_applied(&wrapper_tx_id);
let gas_used = block_results.gas_used(&wrapper_tx_id);
let gas_used = block_results
.gas_used(&wrapper_tx_id)
.map(|gas| gas.parse::<u64>().unwrap());

let fee = Fee {
gas: Uint::from(wrapper.gas_limit).to_string(),
Expand Down
6 changes: 6 additions & 0 deletions transactions/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ async fn crawling_fn(

let inner_txs = block.inner_txs();
let wrapper_txs = block.wrapper_txs();
let gas_estimates = tx_service::get_gas_estimates(&inner_txs, &wrapper_txs);

let ibc_sequence_packet =
tx_service::get_ibc_packets(&block_results, &inner_txs);
Expand Down Expand Up @@ -203,6 +204,11 @@ async fn crawling_fn(
ibc_ack_packet,
)?;

transaction_repo::insert_gas_estimates(
transaction_conn,
gas_estimates,
)?;

anyhow::Ok(())
})
})
Expand Down
23 changes: 22 additions & 1 deletion transactions/src/repository/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ use diesel::{
RunQueryDsl,
};
use orm::crawler_state::{BlockStateInsertDb, CrawlerNameDb};
use orm::gas::GasEstimationInsertDb;
use orm::ibc::{IbcAckInsertDb, IbcAckStatusDb, IbcSequencekStatusUpdateDb};
use orm::schema::{
crawler_state, ibc_ack, inner_transactions, wrapper_transactions,
crawler_state, gas_estimations, ibc_ack, inner_transactions,
wrapper_transactions,
};
use orm::transactions::{InnerTransactionInsertDb, WrapperTransactionInsertDb};
use shared::crawler_state::{BlockCrawlerState, CrawlerName};
use shared::gas::GasEstimation;
use shared::transaction::{
IbcAck, IbcSequence, InnerTransaction, WrapperTransaction,
};
Expand Down Expand Up @@ -119,3 +122,21 @@ pub fn update_ibc_sequence(
}
anyhow::Ok(())
}

pub fn insert_gas_estimates(
transaction_conn: &mut PgConnection,
gas_estimates: Vec<GasEstimation>,
) -> anyhow::Result<()> {
diesel::insert_into(gas_estimations::table)
.values::<Vec<GasEstimationInsertDb>>(
gas_estimates
.into_iter()
.map(GasEstimationInsertDb::from)
.collect(),
)
.on_conflict_do_nothing()
.execute(transaction_conn)
.context("Failed to update gas estimates in db")?;

anyhow::Ok(())
}
Loading

0 comments on commit b829015

Please sign in to comment.