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 24, 2024
1 parent c1fbdc1 commit 9e31f00
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 22 deletions.
48 changes: 48 additions & 0 deletions shared/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,52 @@ impl GasEstimation {
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
}
}
11 changes: 11 additions & 0 deletions shared/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ pub struct WrapperTransaction {
pub atomic: bool,
pub block_height: BlockHeight,
pub exit_code: TransactionExitStatus,
pub total_signatures: u64,
pub size: u64,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -300,6 +302,13 @@ impl Transaction {
) -> Result<(WrapperTransaction, Vec<InnerTransaction>), String> {
let transaction =
Tx::try_from(raw_tx_bytes).map_err(|e| e.to_string())?;
let total_signatures = transaction
.clone()
.sections
.iter()
.filter(|section| section.signature().is_some())
.count() as u64;
let tx_size = raw_tx_bytes.len() as u64;

match transaction.header().tx_type {
TxType::Wrapper(wrapper) => {
Expand Down Expand Up @@ -330,6 +339,8 @@ impl Transaction {
atomic,
block_height,
exit_code: wrapper_tx_status,
total_signatures,
size: tx_size,
};

let mut inner_txs = vec![];
Expand Down
106 changes: 106 additions & 0 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,100 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/GasPriceTable"
/api/v1/gas/estimate:
get:
summary: Get an estimate for a transaction
parameters:
- in: query
name: bond
schema:
type: integer
minimum: 1
maximum: 100
- in: query
name: claim_rewards
schema:
type: integer
minimum: 1
maximum: 100
- in: query
name: unbond
schema:
type: integer
minimum: 1
maximum: 100
- in: query
name: transparent_transfer
schema:
type: integer
minimum: 1
maximum: 100
- in: query
name: shielded_transfer
schema:
type: integer
minimum: 1
maximum: 100
- in: query
name: shielding_transfer
schema:
type: integer
minimum: 1
maximum: 100
- in: query
name: unshielding_transfer
schema:
type: integer
minimum: 1
maximum: 100
- in: query
name: vote
schema:
type: integer
minimum: 1
maximum: 100
- in: query
name: ibc
schema:
type: integer
minimum: 1
maximum: 100
- in: query
name: withdraw
schema:
type: integer
minimum: 1
maximum: 100
- in: query
name: reveal_pk
schema:
type: integer
minimum: 1
maximum: 100
- in: query
name: redelegate
schema:
type: integer
minimum: 1
maximum: 100
- in: query
name: signatures
schema:
type: integer
minimum: 1
maximum: 20
- in: query
name: tx_size
schema:
type: integer
minimum: 1
responses:
'200':
description: A gas estimate.
content:
application/json:
schema:
$ref: '#/components/schemas/GasEstimate'
/api/v1/chain/token:
get:
summary: Get chain tokens
Expand Down Expand Up @@ -998,6 +1092,18 @@ components:
type: string
minDenomAmount:
type: string
GasEstimate:
type: object
required: [min, max, avg, totalEstimates]
properties:
min:
type: number
max:
type: number
avg:
type: number
totalEstimates:
type: number
NativeToken:
type: object
required: [address]
Expand Down
29 changes: 19 additions & 10 deletions transactions/src/services/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ pub fn get_gas_estimates(
.iter()
.map(|wrapper_tx| {
let mut gas_estimate = GasEstimation::new(wrapper_tx.tx_id.clone());
gas_estimate.signatures = wrapper_tx.total_signatures;
gas_estimate.size = wrapper_tx.size;

inner_txs
.iter()
.filter(|inner_tx| {
Expand All @@ -131,27 +134,33 @@ pub fn get_gas_estimates(
})
.for_each(|tx| match tx.kind {
TransactionKind::TransparentTransfer(_) => {
gas_estimate.transparent_transfer += 1
gas_estimate.increase_transparent_transfer()
}
TransactionKind::ShieldedTransfer(_) => {
gas_estimate.shielded_transfer += 1
gas_estimate.increase_shielded_transfer()
}
TransactionKind::IbcMsgTransfer(_) => {
gas_estimate.ibc_msg_transfer += 1
gas_estimate.increase_ibc_msg_transfer()
}
TransactionKind::Bond(_) => gas_estimate.bond += 1,
TransactionKind::Bond(_) => gas_estimate.increase_bond(),
TransactionKind::Redelegation(_) => {
gas_estimate.transparent_transfer += 1
gas_estimate.increase_redelegation()
}
TransactionKind::Unbond(_) => {
gas_estimate.increase_unbond()
}
TransactionKind::Withdraw(_) => {
gas_estimate.increase_withdraw()
}
TransactionKind::Unbond(_) => gas_estimate.unbond += 1,
TransactionKind::Withdraw(_) => gas_estimate.withdraw += 1,
TransactionKind::ClaimRewards(_) => {
gas_estimate.claim_rewards += 1
gas_estimate.increase_claim_rewards()
}
TransactionKind::ProposalVote(_) => {
gas_estimate.vote_proposal += 1
gas_estimate.increase_vote()
}
TransactionKind::RevealPk(_) => {
gas_estimate.increase_reveal_pk()
}
TransactionKind::RevealPk(_) => gas_estimate.reveal_pk += 1,
_ => (),
});
gas_estimate
Expand Down
37 changes: 35 additions & 2 deletions webserver/src/dto/gas.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use serde::{Deserialize, Serialize};
use validator::Validate;

use crate::error::gas::GasError;

#[derive(Clone, Serialize, Deserialize, Validate)]
pub struct GasEstimateQuery {
#[validate(range(min = 1, max = 100))]
pub page: Option<u64>,
#[validate(range(min = 1, max = 100))]
pub bond: Option<u64>,
#[validate(range(min = 1, max = 100))]
Expand All @@ -27,4 +27,37 @@ pub struct GasEstimateQuery {
pub withdraw: Option<u64>,
#[validate(range(min = 1, max = 100))]
pub reveal_pk: Option<u64>,
#[validate(range(min = 1, max = 100))]
pub redelegate: Option<u64>,
#[validate(range(min = 1, max = 20))]
pub signatures: Option<u64>,
#[validate(range(min = 1, max = 100000))]
pub tx_size: Option<u64>,
}

impl GasEstimateQuery {
pub fn is_valid(&self) -> Result<(), GasError> {
let res = [
self.bond,
self.claim_rewards,
self.unbond,
self.transparent_transfer,
self.shielded_transfer,
self.shielding_transfer,
self.unshielding_transfer,
self.vote,
self.withdraw,
self.ibc,
self.reveal_pk,
self.redelegate,
]
.iter()
.any(|field| field.is_some());

if res {
Ok(())
} else {
Err(GasError::InvalidQueryParams)
}
}
}
3 changes: 3 additions & 0 deletions webserver/src/error/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::response::api::ApiErrorResponse;

#[derive(Error, Debug)]
pub enum GasError {
#[error("Invalid query parameters")]
InvalidQueryParams,
#[error("Database error: {0}")]
Database(String),
#[error("Unknown error: {0}")]
Expand All @@ -15,6 +17,7 @@ pub enum GasError {
impl IntoResponse for GasError {
fn into_response(self) -> Response {
let status_code = match self {
GasError::InvalidQueryParams => StatusCode::BAD_GATEWAY,
GasError::Unknown(_) | GasError::Database(_) => {
StatusCode::INTERNAL_SERVER_ERROR
}
Expand Down
5 changes: 5 additions & 0 deletions webserver/src/handler/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ pub async fn get_gas_estimate(
Query(query): Query<GasEstimateQuery>,
State(state): State<CommonState>,
) -> Result<Json<GasEstimate>, ApiError> {
query.is_valid()?;

let gas = state
.gas_service
.estimate_gas(
query.bond.unwrap_or(0),
query.redelegate.unwrap_or(0),
query.claim_rewards.unwrap_or(0),
query.unbond.unwrap_or(0),
query.transparent_transfer.unwrap_or(0),
Expand All @@ -59,6 +62,8 @@ pub async fn get_gas_estimate(
query.ibc.unwrap_or(0),
query.withdraw.unwrap_or(0),
query.reveal_pk.unwrap_or(0),
query.signatures.unwrap_or(2),
query.tx_size.unwrap_or(0),
)
.await?;

Expand Down
Loading

0 comments on commit 9e31f00

Please sign in to comment.