From e54b583b1c55784c781f03d7cf08d42cc4ce0ce6 Mon Sep 17 00:00:00 2001 From: peilun-conflux <48905552+peilun-conflux@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:07:46 +0800 Subject: [PATCH] Add zgs_getSectorProof. (#112) --- node/miner/src/submitter.rs | 2 +- node/rpc/src/zgs/api.rs | 9 ++++++++- node/rpc/src/zgs/impl.rs | 16 +++++++++++++++- node/storage-async/src/lib.rs | 2 +- node/storage/src/log_store/log_manager.rs | 6 +++--- node/storage/src/log_store/mod.rs | 7 ++++++- 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/node/miner/src/submitter.rs b/node/miner/src/submitter.rs index 642bf20..efa2ae0 100644 --- a/node/miner/src/submitter.rs +++ b/node/miner/src/submitter.rs @@ -81,7 +81,7 @@ impl Submitter { let flow_proof = self .store .get_proof_at_root( - mine_answer.context_flow_root, + Some(mine_answer.context_flow_root), mine_answer.recall_position, SECTORS_PER_SEAL as u64, ) diff --git a/node/rpc/src/zgs/api.rs b/node/rpc/src/zgs/api.rs index 0c8fa68..2ed6fa3 100644 --- a/node/rpc/src/zgs/api.rs +++ b/node/rpc/src/zgs/api.rs @@ -1,7 +1,7 @@ use crate::types::{FileInfo, Segment, SegmentWithProof, Status}; use jsonrpsee::core::RpcResult; use jsonrpsee::proc_macros::rpc; -use shared_types::DataRoot; +use shared_types::{DataRoot, FlowProof}; use storage::config::ShardConfig; #[rpc(server, client, namespace = "zgs")] @@ -38,4 +38,11 @@ pub trait Rpc { #[method(name = "getShardConfig")] async fn get_shard_config(&self) -> RpcResult; + + #[method(name = "getSectorProof")] + async fn get_sector_proof( + &self, + sector_index: u64, + flow_root: Option, + ) -> RpcResult; } diff --git a/node/rpc/src/zgs/impl.rs b/node/rpc/src/zgs/impl.rs index 36ba47e..48836f0 100644 --- a/node/rpc/src/zgs/impl.rs +++ b/node/rpc/src/zgs/impl.rs @@ -5,7 +5,7 @@ use crate::Context; use chunk_pool::{FileID, SegmentInfo}; use jsonrpsee::core::async_trait; use jsonrpsee::core::RpcResult; -use shared_types::{DataRoot, Transaction, CHUNK_SIZE}; +use shared_types::{DataRoot, FlowProof, Transaction, CHUNK_SIZE}; use std::fmt::{Debug, Formatter, Result}; use storage::config::ShardConfig; use storage::try_option; @@ -157,6 +157,20 @@ impl RpcServer for RpcServerImpl { let shard_config = self.ctx.log_store.get_store().flow().get_shard_config(); Ok(shard_config) } + + async fn get_sector_proof( + &self, + sector_index: u64, + flow_root: Option, + ) -> RpcResult { + let proof = self + .ctx + .log_store + .get_proof_at_root(flow_root, sector_index, 1) + .await?; + assert_eq!(proof.left_proof, proof.right_proof); + Ok(proof.right_proof) + } } impl RpcServerImpl { diff --git a/node/storage-async/src/lib.rs b/node/storage-async/src/lib.rs index 419b380..44b2ff1 100644 --- a/node/storage-async/src/lib.rs +++ b/node/storage-async/src/lib.rs @@ -54,7 +54,7 @@ impl Store { delegate!(fn get_chunk_by_flow_index(index: u64, length: u64) -> Result>); delegate!(fn finalize_tx(tx_seq: u64) -> Result<()>); delegate!(fn finalize_tx_with_hash(tx_seq: u64, tx_hash: H256) -> Result); - delegate!(fn get_proof_at_root(root: DataRoot, index: u64, length: u64) -> Result); + delegate!(fn get_proof_at_root(root: Option, index: u64, length: u64) -> Result); delegate!(fn get_context() -> Result<(DataRoot, u64)>); pub async fn get_tx_seq_by_data_root(&self, data_root: &DataRoot) -> Result> { diff --git a/node/storage/src/log_store/log_manager.rs b/node/storage/src/log_store/log_manager.rs index f780728..e089933 100644 --- a/node/storage/src/log_store/log_manager.rs +++ b/node/storage/src/log_store/log_manager.rs @@ -540,12 +540,12 @@ impl LogStoreRead for LogManager { fn get_proof_at_root( &self, - root: DataRoot, + root: Option, index: u64, length: u64, ) -> crate::error::Result { - let left_proof = self.gen_proof(index, Some(root))?; - let right_proof = self.gen_proof(index + length - 1, Some(root))?; + let left_proof = self.gen_proof(index, root)?; + let right_proof = self.gen_proof(index + length - 1, root)?; Ok(FlowRangeProof { left_proof, right_proof, diff --git a/node/storage/src/log_store/mod.rs b/node/storage/src/log_store/mod.rs index 04ef1a6..0d3bac5 100644 --- a/node/storage/src/log_store/mod.rs +++ b/node/storage/src/log_store/mod.rs @@ -62,7 +62,12 @@ pub trait LogStoreRead: LogStoreChunkRead { fn validate_range_proof(&self, tx_seq: u64, data: &ChunkArrayWithProof) -> Result; - fn get_proof_at_root(&self, root: DataRoot, index: u64, length: u64) -> Result; + fn get_proof_at_root( + &self, + root: Option, + index: u64, + length: u64, + ) -> Result; /// Return flow root and length. fn get_context(&self) -> Result<(DataRoot, u64)>;