Skip to content

Commit

Permalink
Merge pull request #221 from anoma/add-gas
Browse files Browse the repository at this point in the history
Add gas
  • Loading branch information
mateuszjasiuk authored Dec 17, 2024
2 parents d1eb8df + fbbd3bd commit 52bc198
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 41 deletions.
3 changes: 2 additions & 1 deletion chain/src/repository/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub fn insert_tokens(
#[cfg(test)]
mod tests {

use std::collections::HashSet;

use anyhow::Context;
use diesel::{
BoolExpressionMethods, ExpressionMethods, QueryDsl, SelectableHelper,
Expand All @@ -86,7 +88,6 @@ mod tests {
use shared::balance::{Amount, Balance};
use shared::id::Id;
use shared::token::IbcToken;
use std::collections::HashSet;
use test_helpers::db::TestDb;

use super::*;
Expand Down
1 change: 1 addition & 0 deletions orm/migrations/2024-12-01-170248_ibc_ack/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS ibc_ack;
2 changes: 0 additions & 2 deletions orm/migrations/2024-12-10-104502_transaction_types/down.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE ibc_ack;

DROP TYPE IBC_STATUS;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
ALTER TABLE wrapper_transactions DROP COLUMN gas_used;
2 changes: 2 additions & 0 deletions orm/migrations/2024-12-17-095036_transaction_gas_used/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Your SQL goes here
ALTER TABLE wrapper_transactions ADD COLUMN gas_used VARCHAR;
6 changes: 4 additions & 2 deletions orm/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ impl BlockInsertDb {
pub fn fake(height: i32) -> Self {
Self {
height,
hash: Some(height.to_string()), /* fake hash but ensures uniqueness
hash: Some(height.to_string()), /* fake hash but ensures
* uniqueness
* with height */
app_hash: Some("fake_app_hash".to_string()), // doesn't require uniqueness
app_hash: Some("fake_app_hash".to_string()), /* doesn't require
* uniqueness */
timestamp: Some(
chrono::DateTime::from_timestamp(0, 0).unwrap().naive_utc(),
),
Expand Down
34 changes: 1 addition & 33 deletions orm/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,6 @@ pub mod sql_types {
#[diesel(postgres_type(name = "ibc_status"))]
pub struct IbcStatus;

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

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

#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
Expand Down Expand Up @@ -263,21 +247,6 @@ diesel::table! {
}
}

diesel::table! {
use diesel::sql_types::*;
use super::sql_types::PaymentRecurrence;
use super::sql_types::PaymentKind;

public_good_funding (id) {
id -> Int4,
proposal_id -> Int4,
payment_recurrence -> PaymentRecurrence,
payment_kind -> PaymentKind,
receipient -> Varchar,
amount -> Numeric,
}
}

diesel::table! {
revealed_pk (id) {
id -> Int4,
Expand Down Expand Up @@ -340,6 +309,7 @@ diesel::table! {
block_height -> Int4,
exit_code -> TransactionResult,
atomic -> Bool,
gas_used -> Nullable<Varchar>,
}
}

Expand All @@ -350,7 +320,6 @@ diesel::joinable!(governance_votes -> governance_proposals (proposal_id));
diesel::joinable!(ibc_token -> token (address));
diesel::joinable!(inner_transactions -> wrapper_transactions (wrapper_id));
diesel::joinable!(pos_rewards -> validators (validator_id));
diesel::joinable!(public_good_funding -> governance_proposals (proposal_id));
diesel::joinable!(unbonds -> validators (validator_id));
diesel::joinable!(wrapper_transactions -> blocks (block_height));

Expand All @@ -368,7 +337,6 @@ diesel::allow_tables_to_appear_in_same_query!(
ibc_token,
inner_transactions,
pos_rewards,
public_good_funding,
revealed_pk,
token,
unbonds,
Expand Down
2 changes: 2 additions & 0 deletions orm/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub struct WrapperTransactionInsertDb {
pub fee_payer: String,
pub fee_token: String,
pub gas_limit: String,
pub gas_used: Option<String>,
pub block_height: i32,
pub exit_code: TransactionResultDb,
pub atomic: bool,
Expand All @@ -128,6 +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,
block_height: tx.block_height as i32,
exit_code: TransactionResultDb::from(tx.exit_code),
atomic: tx.atomic,
Expand Down
8 changes: 5 additions & 3 deletions shared/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@ pub struct Block {
pub hash: Id,
pub header: BlockHeader,
pub transactions: Vec<(WrapperTransaction, Vec<InnerTransaction>)>,
pub epoch: Epoch
pub epoch: Epoch,
}

impl Block {
pub fn from(
block_response: &TendermintBlockResponse,
block_results: &BlockResult,
proposer_address_namada: &Option<Id>, // Provide the namada address of the proposer, if available
proposer_address_namada: &Option<Id>, /* Provide the namada address
* of the proposer, if
* available */
checksums: Checksums,
epoch: Epoch,
block_height: BlockHeight,
Expand Down Expand Up @@ -154,7 +156,7 @@ impl Block {
app_hash: Id::from(&block_response.block.header.app_hash),
},
transactions,
epoch
epoch,
}
}

Expand Down
16 changes: 16 additions & 0 deletions shared/src/block_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,22 @@ impl BlockResult {
exit_status.unwrap_or(TransactionExitStatus::Rejected)
}

pub fn gas_used(&self, tx_hash: &Id) -> Option<String> {
self.end_events
.iter()
.filter_map(|event| {
if let Some(TxAttributesType::TxApplied(data)) =
&event.attributes
{
Some(data.clone())
} else {
None
}
})
.find(|attributes| attributes.hash.eq(tx_hash))
.map(|attributes| attributes.gas.to_string())
}

pub fn is_inner_tx_accepted(
&self,
wrapper_hash: &Id,
Expand Down
3 changes: 3 additions & 0 deletions shared/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ impl InnerTransaction {
#[derive(Debug, Clone)]
pub struct Fee {
pub gas: String,
pub gas_used: Option<String>,
pub amount_per_gas_unit: String,
pub gas_payer: Id,
pub gas_token: Id,
Expand All @@ -301,9 +302,11 @@ 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 fee = Fee {
gas: Uint::from(wrapper.gas_limit).to_string(),
gas_used,
amount_per_gas_unit: wrapper
.fee
.amount_per_gas_unit
Expand Down
2 changes: 2 additions & 0 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,8 @@ components:
type: string
gasLimit:
type: string
gasUsed:
type: string
blockHeight:
type: string
innerTransactions:
Expand Down
2 changes: 2 additions & 0 deletions webserver/src/response/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct WrapperTransaction {
pub fee_payer: String,
pub fee_token: String,
pub gas_limit: String,
pub gas_used: Option<String>,
pub block_height: u64,
pub inner_transactions: Vec<ShortInnerTransaction>,
pub exit_code: TransactionResult,
Expand Down Expand Up @@ -124,6 +125,7 @@ impl From<WrapperTransactionDb> for WrapperTransaction {
fee_payer: value.fee_payer,
fee_token: value.fee_token,
gas_limit: value.gas_limit,
gas_used: value.gas_used,
block_height: value.block_height as u64,
inner_transactions: vec![],
exit_code: TransactionResult::from(value.exit_code),
Expand Down

0 comments on commit 52bc198

Please sign in to comment.