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

feat: improve gas estimation #231

Merged
merged 4 commits into from
Dec 24, 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
5 changes: 5 additions & 0 deletions orm/migrations/2024-12-20-092544_gas_eastimation/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- This file should undo anything in `up.sql`
DROP TABLE IF EXISTS gas_estimations;

DROP INDEX IF EXISTS wrapper_transactions_gas;
DROP INDEX IF EXISTS inner_transactions_kind;
26 changes: 26 additions & 0 deletions orm/migrations/2024-12-20-092544_gas_eastimation/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Your SQL goes here
CREATE TABLE gas_estimations (
id SERIAL PRIMARY KEY,
wrapper_id VARCHAR(64) NOT NULL,
transparent_transfer INT NOT NULL,
shielded_transfer INT NOT NULL,
shielding_transfer INT NOT NULL,
unshielding_transfer INT NOT NULL,
ibc_msg_transfer INT NOT NULL,
bond INT NOT NULL,
redelegation INT NOT NULL,
unbond INT NOT NULL,
withdraw INT NOT NULL,
claim_rewards INT NOT NULL,
vote_proposal INT NOT NULL,
reveal_pk 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) ;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda weird but I messed up in a previous PR


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,
}
}
}
92 changes: 36 additions & 56 deletions orm/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,91 +1,47 @@
// @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 = "history_kind"))]
pub struct HistoryKind;

#[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 @@ -167,6 +123,28 @@ diesel::table! {
}
}

diesel::table! {
gas_estimations (id) {
id -> Int4,
#[max_length = 64]
wrapper_id -> Varchar,
transparent_transfer -> Int4,
shielded_transfer -> Int4,
shielding_transfer -> Int4,
unshielding_transfer -> Int4,
ibc_msg_transfer -> Int4,
bond -> Int4,
redelegation -> Int4,
unbond -> Int4,
withdraw -> Int4,
claim_rewards -> Int4,
vote_proposal -> Int4,
reveal_pk -> Int4,
tx_size -> Int4,
signatures -> Int4,
}
}

diesel::table! {
gas_price (token) {
token -> Varchar,
Expand Down Expand Up @@ -330,13 +308,14 @@ diesel::table! {
block_height -> Int4,
exit_code -> TransactionResult,
atomic -> Bool,
gas_used -> Nullable<Varchar>,
gas_used -> Nullable<Int4>,
}
}

diesel::joinable!(balance_changes -> blocks (height));
diesel::joinable!(balance_changes -> token (token));
diesel::joinable!(bonds -> validators (validator_id));
diesel::joinable!(gas_estimations -> wrapper_transactions (wrapper_id));
diesel::joinable!(governance_votes -> governance_proposals (proposal_id));
diesel::joinable!(ibc_token -> token (address));
diesel::joinable!(inner_transactions -> wrapper_transactions (wrapper_id));
Expand All @@ -352,6 +331,7 @@ diesel::allow_tables_to_appear_in_same_query!(
chain_parameters,
crawler_state,
gas,
gas_estimations,
gas_price,
governance_proposals,
governance_votes,
Expand Down
4 changes: 2 additions & 2 deletions orm/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub struct WrapperTransactionDb {
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 @@ -131,7 +131,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
90 changes: 90 additions & 0 deletions shared/src/gas.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,97 @@
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,
}
}

pub fn increase_transparent_transfer(&mut self) {
self.transparent_transfer += 1
}

pub fn increase_shielded_transfer(&mut self) {
self.shielded_transfer += 1
}

pub fn increase_shielding_transfer(&mut self) {
self.shielding_transfer += 1
}

pub fn increase_unshielding_transfer(&mut self) {
self.unshielding_transfer += 1
}

pub fn increase_ibc_msg_transfer(&mut self) {
self.ibc_msg_transfer += 1
}

pub fn increase_bond(&mut self) {
self.bond += 1
}

pub fn increase_redelegation(&mut self) {
self.redelegation += 1
}

pub fn increase_unbond(&mut self) {
self.unbond += 1
}

pub fn increase_withdraw(&mut self) {
self.withdraw += 1
}

pub fn increase_claim_rewards(&mut self) {
self.claim_rewards += 1
}

pub fn increase_vote(&mut self) {
self.vote_proposal += 1
}

pub fn increase_reveal_pk(&mut self) {
self.reveal_pk += 1
}
}
Loading
Loading