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 1 commit
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
Prev Previous commit
Next Next commit
if cache did't hit, re-fetch spv instance from network.
  • Loading branch information
EthanYuan committed May 20, 2024
commit 0b90ec9b41f6514f45fe5eaccb043b7df4e54d00
68 changes: 53 additions & 15 deletions src/components/api_service/mod.rs
Original file line number Diff line number Diff line change
@@ -244,30 +244,32 @@ 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 (mut spv_instance, from_cache) = if let Some(spv_instance) = self.load_spv_instance() {
log::debug!(">>> the cached SPV instance is {spv_instance}");
spv_instance
(spv_instance, true)
} else {
log::debug!(">>> fetch SPV instance from remote since cached is not satisfied");
let spv_instance = tokio::task::block_in_place(|| -> RpcResult<SpvInstance> {
spv.ckb_cli.find_spv_cells(spv_type_script).map_err(|err| {
let message = format!(
"failed to get SPV cell base on height {stg_tip_height} from chain"
);
log::error!("{message} since {err}");
RpcError {
code: RpcErrorCode::InternalError,
message,
data: None,
}
})
spv.ckb_cli
.find_spv_cells(spv_type_script.clone())
.map_err(|err| {
let message = format!(
"failed to get SPV cell base on height {stg_tip_height} from chain"
);
log::error!("{message} since {err}");
RpcError {
code: RpcErrorCode::InternalError,
message,
data: None,
}
})
})?;
log::debug!(">>> the fetched SPV instance is {spv_instance}");
self.update_spv_instance(spv_instance.clone());
spv_instance
(spv_instance, false)
};

let spv_client_cell = spv_instance
let mut spv_client_cell = spv_instance
.find_best_spv_client_not_greater_than_height(stg_tip_height)
.map_err(|err| {
let message = format!(
@@ -284,7 +286,43 @@ impl SpvRpc for SpvRpcImpl {
log::debug!(">>> the best SPV client is {}", spv_client_cell.client);

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_height + confirmations && from_cache {
// Fetch SPV instance from network again
spv_instance = tokio::task::block_in_place(|| -> RpcResult<SpvInstance> {
spv.ckb_cli.find_spv_cells(spv_type_script).map_err(|err| {
let message = format!(
"failed to get SPV cell base on height {stg_tip_height} from chain"
);
log::error!("{message} since {err}");
RpcError {
code: RpcErrorCode::InternalError,
message,
data: None,
}
})
})?;
log::debug!(">>> the fetched SPV instance is {spv_instance}");
self.update_spv_instance(spv_instance.clone());

spv_client_cell = spv_instance
.find_best_spv_client_not_greater_than_height(stg_tip_height)
.map_err(|err| {
let message = format!(
"failed to get SPV cell base on height {stg_tip_height} from fetched data"
);
log::error!("{message} since {err}");
RpcError {
code: RpcErrorCode::InternalError,
message,
data: None,
}
})?;

log::debug!(">>> the best SPV client is {}", spv_client_cell.client);
}

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_height + confirmations {
let desc = format!(