Skip to content

Commit

Permalink
fix: fix get metadata by number (#1483)
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo authored Oct 18, 2023
1 parent 31d64de commit a2d8e04
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions core/api/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ where
block_number: Option<u64>,
) -> ProtocolResult<Metadata> {
if let Some(num) = block_number {
return MetadataHandle::new(self.get_metadata_root(ctx).await?)
return MetadataHandle::new(self.get_metadata_root(ctx, Some(num)).await?)
.get_metadata_by_block_number(num);
}

Expand All @@ -255,11 +255,12 @@ where
.get_latest_block_header(ctx.clone())
.await?
.number;
MetadataHandle::new(self.get_metadata_root(ctx).await?).get_metadata_by_block_number(num)
MetadataHandle::new(self.get_metadata_root(ctx, None).await?)
.get_metadata_by_block_number(num)
}

async fn get_ckb_related_info(&self, ctx: Context) -> ProtocolResult<CkbRelatedInfo> {
MetadataHandle::new(self.get_metadata_root(ctx).await?).get_ckb_related_info()
MetadataHandle::new(self.get_metadata_root(ctx, None).await?).get_ckb_related_info()
}

async fn get_image_cell_root(&self, ctx: Context) -> ProtocolResult<H256> {
Expand All @@ -274,8 +275,17 @@ where
.get_image_cell_root())
}

async fn get_metadata_root(&self, ctx: Context) -> ProtocolResult<H256> {
let state_root = self.storage.get_latest_block_header(ctx).await?.state_root;
async fn get_metadata_root(&self, ctx: Context, number: Option<u64>) -> ProtocolResult<H256> {
let state_root = match number {
Some(n) => {
self.storage
.get_block_header(ctx, n)
.await?
.ok_or_else(|| APIError::RequestPayload("Not found number".to_string()))?
.state_root
}
None => self.storage.get_latest_block_header(ctx).await?.state_root,
};

Ok(AxonExecutorReadOnlyAdapter::from_root(
state_root,
Expand All @@ -287,7 +297,7 @@ where
}

async fn hardfork_info(&self, ctx: Context) -> ProtocolResult<HardforkInfo> {
MetadataHandle::new(self.get_metadata_root(ctx).await?).hardfork_infos()
MetadataHandle::new(self.get_metadata_root(ctx, None).await?).hardfork_infos()
}

async fn hardfork_proposal(&self, ctx: Context) -> ProtocolResult<Option<HardforkInfoInner>> {
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/traits/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub trait APIAdapter: Send + Sync {

async fn get_image_cell_root(&self, ctx: Context) -> ProtocolResult<H256>;

async fn get_metadata_root(&self, ctx: Context) -> ProtocolResult<H256>;
async fn get_metadata_root(&self, ctx: Context, number: Option<u64>) -> ProtocolResult<H256>;

async fn hardfork_info(&self, ctx: Context) -> ProtocolResult<HardforkInfo>;

Expand Down

0 comments on commit a2d8e04

Please sign in to comment.