Skip to content

Commit

Permalink
added handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraccaman committed Dec 13, 2024
1 parent 158bad0 commit bdeae36
Show file tree
Hide file tree
Showing 21 changed files with 208 additions and 96 deletions.
16 changes: 3 additions & 13 deletions chain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,19 +209,9 @@ async fn crawling_fn(
.await
.into_rpc_error()?;

let block = Block::from(
tm_block_response,
&block_results,
checksums,
epoch,
block_height,
);
tracing::debug!(
block = block_height,
txs = block.transactions.len(),
"Deserialized {} txs...",
block.transactions.len()
);
let block =
Block::from(tm_block_response, &block_results, checksums, block_height);
tracing::info!("Deserialized {} txs...", block.transactions.len());

let native_token = namada_service::get_native_token(&client)
.await
Expand Down
4 changes: 2 additions & 2 deletions orm/migrations/2024-12-01-170248_ibc_ack/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ CREATE TYPE IBC_STATUS AS ENUM ('fail', 'success', 'timeout', 'unknown');
CREATE TABLE ibc_ack (
id VARCHAR PRIMARY KEY,
tx_hash VARCHAR NOT NULL,
timeout INT NOT NULL,
status IBC_STATUS
timeout BIGINT NOT NULL,
status IBC_STATUS NOT NULL
);
9 changes: 5 additions & 4 deletions orm/src/ibc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use diesel::{AsChangeset, Insertable};
use diesel::prelude::Queryable;
use diesel::{AsChangeset, Insertable, Selectable};
use serde::{Deserialize, Serialize};
use shared::transaction::{IbcAckStatus, IbcSequence};

Expand All @@ -24,13 +25,13 @@ impl From<IbcAckStatus> for IbcAckStatusDb {
}
}

#[derive(Serialize, Insertable, AsChangeset, Clone)]
#[derive(Serialize, Queryable, Insertable, Selectable, Clone, Debug)]
#[diesel(table_name = ibc_ack)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct IbcAckDb {
pub id: String,
pub tx_hash: String,
pub timeout: i32,
pub timeout: i64,
pub status: IbcAckStatusDb,
}

Expand All @@ -41,7 +42,7 @@ impl From<IbcSequence> for IbcAckInsertDb {
Self {
id: value.id(),
tx_hash: value.tx_id.to_string(),
timeout: value.timeout as i32,
timeout: value.timeout as i64,
status: IbcAckStatusDb::Unknown,
}
}
Expand Down
80 changes: 14 additions & 66 deletions orm/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,99 +1,51 @@
// @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 = "payment_kind"))]
pub struct PaymentKind;

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

#[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 @@ -211,8 +163,8 @@ diesel::table! {
ibc_ack (id) {
id -> Varchar,
tx_hash -> Varchar,
timeout -> Int4,
status -> Nullable<IbcStatus>,
timeout -> Int8,
status -> IbcStatus,
}
}

Expand Down Expand Up @@ -262,10 +214,6 @@ diesel::table! {
payment_kind -> PaymentKind,
receipient -> Varchar,
amount -> Numeric,
<<<<<<< HEAD
=======
last_paid_epoch -> Int4,
>>>>>>> 3e415012 (parsing ack tx)
}
}

Expand Down
3 changes: 0 additions & 3 deletions shared/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,13 @@ pub struct Block {
pub hash: Id,
pub header: BlockHeader,
pub transactions: Vec<(WrapperTransaction, Vec<InnerTransaction>)>,
pub epoch: Epoch,
}

impl Block {
pub fn from(
block_response: TendermintBlockResponse,
block_results: &BlockResult,
checksums: Checksums,
epoch: Epoch,
block_height: BlockHeight,
) -> Self {
let transactions = block_response
Expand Down Expand Up @@ -148,7 +146,6 @@ impl Block {
app_hash: Id::from(block_response.block.header.app_hash),
},
transactions,
epoch,
}
}

Expand Down
7 changes: 5 additions & 2 deletions shared/src/block_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,13 @@ impl TxAttributesType {
attributes.get("packet_sequence").unwrap().to_owned();
let timeout_timestamp = attributes
.get("packet_timeout_timestamp")
.unwrap()
.unwrap_or(&"0".to_string())
.parse::<u64>()
.unwrap()
.unwrap_or_default()
.to_owned();

tracing::error!("{}", timeout_timestamp);

Some(Self::SendPacket(SendPacket {
source_port,
dest_port,
Expand Down
17 changes: 14 additions & 3 deletions shared/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use std::str::FromStr;

use namada_core::address::Address;
use namada_core::masp::MaspTxId;
use namada_sdk::borsh::BorshSerializeExt;
use namada_sdk::ibc::IbcMessage as NamadaIbcMessage;
use namada_sdk::token::{
Account as NamadaAccount, DenominatedAmount as NamadaDenominatedAmount,
Transfer as NamadaTransfer,
};
use serde::ser::SerializeStruct;
use serde::{Deserialize, Serialize};
use subtle_encoding::hex;

#[derive(Debug, Clone)]
pub struct AccountsMap(pub BTreeMap<NamadaAccount, NamadaDenominatedAmount>);
Expand Down Expand Up @@ -120,10 +122,19 @@ impl Serialize for IbcMessage<NamadaTransfer> {

state.end()
}
NamadaIbcMessage::Envelope(_) => {
let state = serializer.serialize_struct("IbcEnvelope", 0)?;
NamadaIbcMessage::Envelope(data) => {
let mut state =
serializer.serialize_struct("IbcEnvelope", 1)?;

// todo: implement this bs :(

// TODO: serialize envelope message correctly
state.serialize_field(
"data",
&String::from_utf8_lossy(&hex::encode(
data.serialize_to_vec(),
))
.into_owned(),
)?;

state.end()
}
Expand Down
1 change: 0 additions & 1 deletion transactions/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ async fn crawling_fn(
tm_block_response.clone(),
&block_results,
checksums,
1_u32,
block_height,
);

Expand Down
5 changes: 3 additions & 2 deletions webserver/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::config::AppConfig;
use crate::handler::{
balance as balance_handlers, chain as chain_handlers,
crawler_state as crawler_state_handlers, gas as gas_handlers,
governance as gov_handlers, pgf as pgf_service, pk as pk_handlers,
pos as pos_handlers, transaction as transaction_handlers,
governance as gov_handlers, ibc as ibc_handler, pk as pk_handlers,
pos as pos_handlers, transaction as transaction_handlers, pgf as pgf_service
};
use crate::state::common::CommonState;

Expand Down Expand Up @@ -133,6 +133,7 @@ impl ApplicationServer {
"/chain/epoch/latest",
get(chain_handlers::get_last_processed_epoch),
)
.route("/ibc/:tx_id/status", get(ibc_handler::get_ibc_status))
.route(
"/pgf/payments",
get(pgf_service::get_pgf_continuous_payments),
Expand Down
4 changes: 4 additions & 0 deletions webserver/src/error/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::crawler_state::CrawlerStateError;
use super::gas::GasError;
use super::governance::GovernanceError;
use super::pgf::PgfError;
use super::ibc::IbcError;
use super::pos::PoSError;
use super::revealed_pk::RevealedPkError;
use super::transaction::TransactionError;
Expand All @@ -30,6 +31,8 @@ pub enum ApiError {
#[error(transparent)]
PgfError(#[from] PgfError),
#[error(transparent)]
IbcError(#[from] IbcError),
#[error(transparent)]
CrawlerStateError(#[from] CrawlerStateError),
}

Expand All @@ -44,6 +47,7 @@ impl IntoResponse for ApiError {
ApiError::RevealedPkError(error) => error.into_response(),
ApiError::GasError(error) => error.into_response(),
ApiError::PgfError(error) => error.into_response(),
ApiError::IbcError(error) => error.into_response(),
ApiError::CrawlerStateError(error) => error.into_response(),
}
}
Expand Down
28 changes: 28 additions & 0 deletions webserver/src/error/ibc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use axum::http::StatusCode;
use axum::response::IntoResponse;
use thiserror::Error;

use crate::response::api::ApiErrorResponse;

#[derive(Error, Debug)]
pub enum IbcError {
#[error("Revealed public key {0} not found")]
NotFound(u64),
#[error("Database error: {0}")]
Database(String),
#[error("Unknown error: {0}")]
Unknown(String),
}

impl IntoResponse for IbcError {
fn into_response(self) -> axum::response::Response {
let status_code = match self {
IbcError::NotFound(_) => StatusCode::NOT_FOUND,
IbcError::Unknown(_) | IbcError::Database(_) => {
StatusCode::INTERNAL_SERVER_ERROR
}
};

ApiErrorResponse::send(status_code.as_u16(), Some(self.to_string()))
}
}
1 change: 1 addition & 0 deletions webserver/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod crawler_state;
pub mod gas;
pub mod governance;
pub mod pgf;
pub mod ibc;
pub mod pos;
pub mod revealed_pk;
pub mod transaction;
19 changes: 19 additions & 0 deletions webserver/src/handler/ibc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use axum::extract::{Path, State};
use axum::http::HeaderMap;
use axum::Json;
use axum_macros::debug_handler;

use crate::error::api::ApiError;
use crate::response::ibc::IbcAck;
use crate::state::common::CommonState;

#[debug_handler]
pub async fn get_ibc_status(
_headers: HeaderMap,
Path(tx_id): Path<String>,
State(state): State<CommonState>,
) -> Result<Json<IbcAck>, ApiError> {
let ibc_ack_status = state.ibc_service.get_ack_by_tx_id(tx_id).await?;

Ok(Json(ibc_ack_status))
}
1 change: 1 addition & 0 deletions webserver/src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod crawler_state;
pub mod gas;
pub mod governance;
pub mod pgf;
pub mod ibc;
pub mod pk;
pub mod pos;
pub mod transaction;
Loading

0 comments on commit bdeae36

Please sign in to comment.