Skip to content

Commit

Permalink
Merge pull request #250 from anoma/improve-block-response
Browse files Browse the repository at this point in the history
improve block response
  • Loading branch information
Fraccaman authored Jan 15, 2025
2 parents dae72e9 + 2858da5 commit 4459f92
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
6 changes: 6 additions & 0 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,12 @@ components:
type: string
proposer:
type: string
transactions:
type: array
items:
type: string
parentHash:
type: string
epoch:
type: string
TransactionHistory:
Expand Down
23 changes: 23 additions & 0 deletions webserver/src/repository/tranasaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ pub trait TransactionRepositoryTrait {
PaginatedResponseDb<(TransactionHistoryDb, InnerTransactionDb, i32)>,
String,
>;
async fn find_txs_by_block_height(
&self,
block_height: i32,
) -> Result<Vec<WrapperTransactionDb>, String>;
}

#[async_trait]
Expand Down Expand Up @@ -124,4 +128,23 @@ impl TransactionRepositoryTrait for TransactionRepository {
.map_err(|e| e.to_string())?
.map_err(|e| e.to_string())
}

async fn find_txs_by_block_height(
&self,
block_height: i32,
) -> Result<Vec<WrapperTransactionDb>, String> {
let conn = self.app_state.get_db_connection().await;

conn.interact(move |conn| {
wrapper_transactions::table
.filter(
wrapper_transactions::dsl::block_height.eq(block_height),
)
.select(WrapperTransactionDb::as_select())
.get_results(conn)
})
.await
.map_err(|e| e.to_string())?
.map_err(|e| e.to_string())
}
}
18 changes: 16 additions & 2 deletions webserver/src/response/block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use orm::blocks::BlockDb;
use orm::transactions::WrapperTransactionDb;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand All @@ -9,11 +10,17 @@ pub struct Block {
pub app_hash: Option<String>,
pub timestamp: Option<String>,
pub proposer: Option<String>,
pub transactions: Vec<String>,
pub parent_hash: Option<String>,
pub epoch: Option<String>,
}

impl From<BlockDb> for Block {
fn from(block_db: BlockDb) -> Self {
impl Block {
pub fn from(
block_db: BlockDb,
prev_block_db: Option<BlockDb>,
transactions: Vec<WrapperTransactionDb>,
) -> Self {
Self {
height: block_db.height,
hash: block_db.hash,
Expand All @@ -22,6 +29,13 @@ impl From<BlockDb> for Block {
.timestamp
.map(|t| t.and_utc().timestamp().to_string()),
proposer: block_db.proposer,
transactions: transactions
.into_iter()
.map(|wrapper| wrapper.id.to_lowercase())
.collect(),
parent_hash: prev_block_db
.map(|block| block.app_hash)
.unwrap_or(None),
epoch: block_db.epoch.map(|e| e.to_string()),
}
}
Expand Down
41 changes: 38 additions & 3 deletions webserver/src/service/block.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
use crate::appstate::AppState;
use crate::error::block::BlockError;
use crate::repository::block::{BlockRepository, BlockRepositoryTrait};
use crate::repository::tranasaction::{
TransactionRepository, TransactionRepositoryTrait,
};
use crate::response::block::Block;

#[derive(Clone)]
pub struct BlockService {
block_repo: BlockRepository,
transaction_repo: TransactionRepository,
}

impl BlockService {
pub fn new(app_state: AppState) -> Self {
Self {
block_repo: BlockRepository::new(app_state),
block_repo: BlockRepository::new(app_state.clone()),
transaction_repo: TransactionRepository::new(app_state),
}
}

Expand All @@ -28,8 +33,23 @@ impl BlockService {
"height".to_string(),
height.to_string(),
))?;
let prev_block = if let Some(block_height) = block.height.checked_sub(1)
{
self.block_repo
.find_block_by_height(block_height)
.await
.map_err(BlockError::Database)?
} else {
None
};

Ok(Block::from(block))
let transactions = self
.transaction_repo
.find_txs_by_block_height(block.height)
.await
.map_err(BlockError::Database)?;

Ok(Block::from(block, prev_block, transactions))
}

pub async fn get_block_by_timestamp(
Expand All @@ -46,7 +66,22 @@ impl BlockService {
"timestamp".to_string(),
timestamp.to_string(),
))?;
let prev_block = if let Some(block_height) = block.height.checked_sub(1)
{
self.block_repo
.find_block_by_height(block_height)
.await
.map_err(BlockError::Database)?
} else {
None
};

let transactions = self
.transaction_repo
.find_txs_by_block_height(block.height)
.await
.map_err(BlockError::Database)?;

Ok(Block::from(block))
Ok(Block::from(block, prev_block, transactions))
}
}

0 comments on commit 4459f92

Please sign in to comment.