Skip to content

Commit 2858da5

Browse files
committed
improve block response
1 parent dae72e9 commit 2858da5

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

swagger.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,12 @@ components:
12951295
type: string
12961296
proposer:
12971297
type: string
1298+
transactions:
1299+
type: array
1300+
items:
1301+
type: string
1302+
parentHash:
1303+
type: string
12981304
epoch:
12991305
type: string
13001306
TransactionHistory:

webserver/src/repository/tranasaction.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ pub trait TransactionRepositoryTrait {
4141
PaginatedResponseDb<(TransactionHistoryDb, InnerTransactionDb, i32)>,
4242
String,
4343
>;
44+
async fn find_txs_by_block_height(
45+
&self,
46+
block_height: i32,
47+
) -> Result<Vec<WrapperTransactionDb>, String>;
4448
}
4549

4650
#[async_trait]
@@ -124,4 +128,23 @@ impl TransactionRepositoryTrait for TransactionRepository {
124128
.map_err(|e| e.to_string())?
125129
.map_err(|e| e.to_string())
126130
}
131+
132+
async fn find_txs_by_block_height(
133+
&self,
134+
block_height: i32,
135+
) -> Result<Vec<WrapperTransactionDb>, String> {
136+
let conn = self.app_state.get_db_connection().await;
137+
138+
conn.interact(move |conn| {
139+
wrapper_transactions::table
140+
.filter(
141+
wrapper_transactions::dsl::block_height.eq(block_height),
142+
)
143+
.select(WrapperTransactionDb::as_select())
144+
.get_results(conn)
145+
})
146+
.await
147+
.map_err(|e| e.to_string())?
148+
.map_err(|e| e.to_string())
149+
}
127150
}

webserver/src/response/block.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use orm::blocks::BlockDb;
2+
use orm::transactions::WrapperTransactionDb;
23
use serde::{Deserialize, Serialize};
34

45
#[derive(Clone, Debug, Deserialize, Serialize)]
@@ -9,11 +10,17 @@ pub struct Block {
910
pub app_hash: Option<String>,
1011
pub timestamp: Option<String>,
1112
pub proposer: Option<String>,
13+
pub transactions: Vec<String>,
14+
pub parent_hash: Option<String>,
1215
pub epoch: Option<String>,
1316
}
1417

15-
impl From<BlockDb> for Block {
16-
fn from(block_db: BlockDb) -> Self {
18+
impl Block {
19+
pub fn from(
20+
block_db: BlockDb,
21+
prev_block_db: Option<BlockDb>,
22+
transactions: Vec<WrapperTransactionDb>,
23+
) -> Self {
1724
Self {
1825
height: block_db.height,
1926
hash: block_db.hash,
@@ -22,6 +29,13 @@ impl From<BlockDb> for Block {
2229
.timestamp
2330
.map(|t| t.and_utc().timestamp().to_string()),
2431
proposer: block_db.proposer,
32+
transactions: transactions
33+
.into_iter()
34+
.map(|wrapper| wrapper.id.to_lowercase())
35+
.collect(),
36+
parent_hash: prev_block_db
37+
.map(|block| block.app_hash)
38+
.unwrap_or(None),
2539
epoch: block_db.epoch.map(|e| e.to_string()),
2640
}
2741
}

webserver/src/service/block.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
use crate::appstate::AppState;
22
use crate::error::block::BlockError;
33
use crate::repository::block::{BlockRepository, BlockRepositoryTrait};
4+
use crate::repository::tranasaction::{
5+
TransactionRepository, TransactionRepositoryTrait,
6+
};
47
use crate::response::block::Block;
58

69
#[derive(Clone)]
710
pub struct BlockService {
811
block_repo: BlockRepository,
12+
transaction_repo: TransactionRepository,
913
}
1014

1115
impl BlockService {
1216
pub fn new(app_state: AppState) -> Self {
1317
Self {
14-
block_repo: BlockRepository::new(app_state),
18+
block_repo: BlockRepository::new(app_state.clone()),
19+
transaction_repo: TransactionRepository::new(app_state),
1520
}
1621
}
1722

@@ -28,8 +33,23 @@ impl BlockService {
2833
"height".to_string(),
2934
height.to_string(),
3035
))?;
36+
let prev_block = if let Some(block_height) = block.height.checked_sub(1)
37+
{
38+
self.block_repo
39+
.find_block_by_height(block_height)
40+
.await
41+
.map_err(BlockError::Database)?
42+
} else {
43+
None
44+
};
3145

32-
Ok(Block::from(block))
46+
let transactions = self
47+
.transaction_repo
48+
.find_txs_by_block_height(block.height)
49+
.await
50+
.map_err(BlockError::Database)?;
51+
52+
Ok(Block::from(block, prev_block, transactions))
3353
}
3454

3555
pub async fn get_block_by_timestamp(
@@ -46,7 +66,22 @@ impl BlockService {
4666
"timestamp".to_string(),
4767
timestamp.to_string(),
4868
))?;
69+
let prev_block = if let Some(block_height) = block.height.checked_sub(1)
70+
{
71+
self.block_repo
72+
.find_block_by_height(block_height)
73+
.await
74+
.map_err(BlockError::Database)?
75+
} else {
76+
None
77+
};
78+
79+
let transactions = self
80+
.transaction_repo
81+
.find_txs_by_block_height(block.height)
82+
.await
83+
.map_err(BlockError::Database)?;
4984

50-
Ok(Block::from(block))
85+
Ok(Block::from(block, prev_block, transactions))
5186
}
5287
}

0 commit comments

Comments
 (0)