Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
apply same logic for get block by height and id
Browse files Browse the repository at this point in the history
  • Loading branch information
rllola committed Apr 22, 2024
1 parent 755dc9b commit 227fb70
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
20 changes: 12 additions & 8 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,10 +1085,12 @@ impl Database {
#[instrument(skip(self, block_id))]
pub async fn block_by_id(&self, block_id: &[u8]) -> Result<Option<Row>, Error> {
// query for the block if it exists
let str = format!(
"SELECT * FROM {}.{BLOCKS_TABLE_NAME} WHERE block_id=$1",
self.network
);
// let str = format!(
// "SELECT * FROM {}.{BLOCKS_TABLE_NAME} WHERE block_id=$1",
// self.network
// );
let str = format!("SELECT b.*, txs FROM {0}.blocks b LEFT JOIN (SELECT block_id, JSON_AGG(JSON_BUILD_OBJECT('hash_id', encode(t.hash, 'hex'), 'tx_type', t.tx_type)) AS txs FROM {0}.transactions t GROUP BY t.block_id) t ON b.block_id = t.block_id WHERE b.block_id = $1;", self.network);

query(&str)
.bind(block_id)
.fetch_optional(&*self.pool)
Expand All @@ -1099,10 +1101,12 @@ impl Database {
/// Returns the block at `block_height` if present, otherwise returns an Error.
#[instrument(skip(self))]
pub async fn block_by_height(&self, block_height: u32) -> Result<Option<Row>, Error> {
let str = format!(
"SELECT * FROM {}.{BLOCKS_TABLE_NAME} WHERE header_height={block_height}",
self.network
);
// let str = format!(
// "SELECT * FROM {}.{BLOCKS_TABLE_NAME} WHERE header_height={block_height}",
// self.network
// );
let str = format!("SELECT b.*, txs FROM {0}.blocks b LEFT JOIN (SELECT block_id, JSON_AGG(JSON_BUILD_OBJECT('hash_id', encode(t.hash, 'hex'), 'tx_type', t.tx_type)) AS txs FROM {0}.transactions t GROUP BY t.block_id) t ON b.block_id = t.block_id WHERE b.header_height = {block_height};", self.network);


query(&str)
.fetch_optional(&*self.pool)
Expand Down
6 changes: 0 additions & 6 deletions src/server/endpoints/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ pub async fn get_block_by_hash(
};
let mut block = BlockInfo::try_from(&row)?;

let block_id: Vec<u8> = row.try_get("block_id")?;
get_tx_hashes(&state, &mut block, &block_id).await?;

Ok(Json(Some(block)))
}

Expand All @@ -76,9 +73,6 @@ pub async fn get_block_by_height(

let mut block = BlockInfo::try_from(&row)?;

let block_id: Vec<u8> = row.try_get("block_id")?;
get_tx_hashes(&state, &mut block, &block_id).await?;

Ok(Json(Some(block)))
}

Expand Down

0 comments on commit 227fb70

Please sign in to comment.