Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: spv client cache #37

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/components/api_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,30 @@ impl SpvRpcImpl {
}
}

fn load_spv_instance(&self) -> Option<SpvInstance> {
fn load_spv_instance(
&self,
stg_tip_height: u32,
target_confirmed_height: u32,
) -> Result<Option<SpvInstance>> {
let spv_instance = match self.load_cache_spv_instance() {
Some(instance) => instance,
None => return Ok(None),
};

let spv_client_cell =
spv_instance.find_best_spv_client_not_greater_than_height(stg_tip_height)?;

let spv_header_root = &spv_client_cell.client.headers_mmr_root;
let spv_best_height = spv_header_root.max_height;

if spv_best_height < target_confirmed_height {
return Ok(None);
}

Ok(Some(spv_instance))
}

fn load_cache_spv_instance(&self) -> Option<SpvInstance> {
if let Some(cached) = self
.cached_spv_instance
.read()
Expand Down Expand Up @@ -244,7 +267,9 @@ impl SpvRpc for SpvRpcImpl {

log::debug!(">>> try the cached SPV instance at first");

let spv_instance = if let Some(spv_instance) = self.load_spv_instance() {
let spv_instance = if let Ok(Some(spv_instance)) =
self.load_spv_instance(stg_tip_height, target_height + confirmations)
{
log::debug!(">>> the cached SPV instance is {spv_instance}");
spv_instance
} else {
Expand Down Expand Up @@ -314,7 +339,7 @@ impl SpvRpc for SpvRpcImpl {
log::warn!("[onchain] header#{spv_best_height}; mmr-root {spv_header_root}");
let stg_header_root = packed_stg_header_root.unpack();
log::warn!("[storage] header#{spv_best_height}; mmr-root {stg_header_root}");
let desc = "the SPV instance on chain is not unknown, reorg is required";
let desc = "the SPV instance on chain is unknown, reorg is required";
return Err(ApiErrorCode::OnchainReorgRequired.with_desc(desc));
}

Expand Down