Skip to content

Commit

Permalink
Add sql impl
Browse files Browse the repository at this point in the history
  • Loading branch information
boundless-forest committed Apr 23, 2024
1 parent b37678d commit 2635f7f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
5 changes: 3 additions & 2 deletions client/db/src/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ impl<Block: BlockT, C: HeaderBackend<Block>> fc_api::Backend<Block> for Backend<
BlockNumberOrHash::Latest => Some(BlockId::Hash(self.client.info().best_hash)),
BlockNumberOrHash::Earliest => Some(BlockId::Number(Zero::zero())),
BlockNumberOrHash::Pending => None,
BlockNumberOrHash::Safe => Some(BlockId::Hash(self.client.info().finalized_hash)),
BlockNumberOrHash::Finalized => Some(BlockId::Hash(self.client.info().finalized_hash)),
BlockNumberOrHash::Safe | BlockNumberOrHash::Finalized => {
Some(BlockId::Hash(self.client.info().finalized_hash))
}
})
}

Expand Down
4 changes: 2 additions & 2 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub mod kv;
pub mod sql;

#[derive(Clone)]
pub enum Backend<Block: BlockT, C: HeaderBackend<Block>> {
KeyValue(Arc<kv::Backend<Block, C>>),
pub enum Backend<Block: BlockT, Client: HeaderBackend<Block>> {
KeyValue(Arc<kv::Backend<Block, Client>>),
#[cfg(feature = "sql")]
Sql(Arc<sql::Backend<Block>>),
}
52 changes: 48 additions & 4 deletions client/db/src/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,19 @@ where
.unwrap_or(false)
}

pub async fn latest_indexed_block(&self) -> Option<H256> {
sqlx::query(
"SELECT substrate_block_hash FROM sync_status ORDER BY block_number DESC LIMIT 1",
)
.fetch_optional(self.pool())
.await
.unwrap_or(None)
.map(|row| {
let block_hash_bytes: Vec<u8> = row.get(0);
H256::from_slice(&block_hash_bytes[..])
})
}

/// Retrieves the status if a block is indexed and if also marked as canon.
pub async fn block_indexed_and_canon_status(
&self,
Expand Down Expand Up @@ -799,13 +812,44 @@ impl<Block: BlockT<Hash = H256>> fc_api::Backend<Block> for Backend<Block> {
&self,
number_or_hash: Option<BlockNumberOrHash>,
) -> Result<Option<BlockId<Block>>, String> {
// todo!()
Ok(None)
let block_id = match number_or_hash.unwrap_or(BlockNumberOrHash::Latest) {
BlockNumberOrHash::Hash { hash, .. } => {
if let Ok(Some(substrate_hashes)) = self.block_hash(&hash).await {
for hash in substrate_hashes {
if self.is_canon(hash).await {
return Ok(Some(BlockId::Hash(hash)));
}
}
}
None
}
BlockNumberOrHash::Num(number) => Some(BlockId::Number(number.unique_saturated_into())),
BlockNumberOrHash::Latest => self.latest_indexed_block().await.map(BlockId::Hash),
BlockNumberOrHash::Earliest => Some(BlockId::Number(Zero::zero())),
BlockNumberOrHash::Pending => None,
BlockNumberOrHash::Safe | BlockNumberOrHash::Finalized => self
.get_last_indexed_canon_block()
.await
.ok()
.map(BlockId::Hash),
};
Ok(block_id)
}

async fn is_canon(&self, target_hash: Block::Hash) -> bool {
// todo!()
true
let res = sqlx::query("SELECT is_canon FROM blocks WHERE substrate_block_hash = ?")
.bind(target_hash.as_bytes())
.fetch_optional(self.pool())
.await
.ok()
.and_then(|row| {
row.and_then(|r| {
let is_canon: i32 = r.get(0);
Some(is_canon != 0)
})
});

res.unwrap_or(false)
}

async fn block_hash(
Expand Down

0 comments on commit 2635f7f

Please sign in to comment.