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

Commit

Permalink
refactoring: query_tip
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanYuan committed Jun 20, 2022
1 parent 7646951 commit 97e2082
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
29 changes: 17 additions & 12 deletions core/storage/src/relational/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,23 @@ macro_rules! build_next_cursor {

impl RelationalStorage {
pub(crate) async fn query_tip(&self) -> Result<Option<(BlockNumber, H256)>> {
let w = self
.pool
.wrapper()
.order_by(false, &["block_number"])
.limit(1);
let res: Option<CanonicalChainTable> = self.pool.fetch_by_wrapper(w).await?;
Ok(res.map(|t| {
(
t.block_number,
H256::from_slice(&t.block_hash.inner[0..32]).expect("get block hash h256"),
)
}))
let query = sqlx::query(
r#"
SELECT * FROM mercury_canonical_chain
ORDER BY block_number
DESC
LIMIT 1
"#,
);
let res = self.sqlx_pool.fetch_all(query).await?;
if res.is_empty() {
Ok(None)
} else {
Ok(Some((
res[0].get::<i32, _>("block_number") as u64,
H256::from_slice(&res[0].get::<Vec<u8>, _>("block_hash")[0..32])?,
)))
}
}

pub(crate) async fn get_block_by_number(
Expand Down
15 changes: 15 additions & 0 deletions core/storage/src/relational/tests/fetch_mod_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,18 @@ async fn test_query_transactions() {
assert_eq!(3, ret.response.len());
assert_eq!(None, ret.count);
}

#[tokio::test]
async fn test_query_tip() {
let pool = connect_and_create_tables().await;
let res = pool.query_tip().await.unwrap();
assert!(res.is_none());

let pool = connect_and_insert_blocks().await;
let (block_number, block_hash) = pool.query_tip().await.unwrap().unwrap();
assert_eq!(9, block_number);
assert_eq!(
"953761d56c03bfedf5e70dde0583470383184c41331f709df55d4acab5358640".to_string(),
block_hash.to_string()
);
}
15 changes: 11 additions & 4 deletions core/storage/src/relational/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ async fn connect_sqlite() -> RelationalStorage {
pool
}

pub fn read_block_view(number: u64, dir_path: String) -> JsonBlockView {
let file_name = number.to_string() + ".json";
let path = dir_path + file_name.as_str();
serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap()
async fn connect_and_create_tables() -> RelationalStorage {
let pool = connect_sqlite().await;
let mut tx = pool.pool.transaction().await.unwrap();
xsql_test::create_tables(&mut tx).await.unwrap();
pool
}

async fn connect_and_insert_blocks() -> RelationalStorage {
Expand All @@ -75,6 +76,12 @@ async fn connect_and_insert_blocks() -> RelationalStorage {
pool
}

pub fn read_block_view(number: u64, dir_path: String) -> JsonBlockView {
let file_name = number.to_string() + ".json";
let path = dir_path + file_name.as_str();
serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap()
}

fn caculate_lock_hash(code_hash: &str, args: &str, script_hash_type: ScriptHashType) -> H256 {
let code_hash = H256::from_str(code_hash).unwrap();
let args = H160::from_str(args).unwrap();
Expand Down
9 changes: 9 additions & 0 deletions db/db-sqlx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ impl SQLXPool {
Ok(r)
}

pub async fn fetch_all<'a, T>(&self, query: Query<'a, Any, T>) -> Result<Vec<AnyRow>>
where
T: Send + IntoArguments<'a, Any> + 'a,
{
let pool = self.get_pool()?;
let r = query.fetch_all(pool).await?;
Ok(r)
}

pub async fn fetch_one_by_query_as<T>(
&self,
query: QueryAs<'static, Any, T, AnyArguments<'static>>,
Expand Down

0 comments on commit 97e2082

Please sign in to comment.