This commit is contained in:
Peter Zhang 2025-09-08 16:14:44 +08:00
parent 5dbab01534
commit 4a8479e0df
5 changed files with 49 additions and 65 deletions

View File

@ -27,9 +27,7 @@ use ethereum_types::H256;
impl AppendMerkleTree<OptionalHash, Sha3Algorithm> {
/// Convert a proof of OptionalHash to a proof of H256
pub fn convert_proof_to_h256(
proof: &Proof<OptionalHash>,
) -> Result<Proof<H256>, anyhow::Error> {
pub fn convert_proof_to_h256(proof: Proof<OptionalHash>) -> Result<Proof<H256>, anyhow::Error> {
let lemma: Result<Vec<H256>, anyhow::Error> = proof
.lemma()
.iter()
@ -43,18 +41,41 @@ impl AppendMerkleTree<OptionalHash, Sha3Algorithm> {
/// Convert a range proof of OptionalHash to a range proof of H256
pub fn convert_range_proof_to_h256(
proof: &RangeProof<OptionalHash>,
proof: RangeProof<OptionalHash>,
) -> Result<RangeProof<H256>, anyhow::Error> {
Ok(RangeProof {
left_proof: Self::convert_proof_to_h256(&proof.left_proof)?,
right_proof: Self::convert_proof_to_h256(&proof.right_proof)?,
left_proof: Self::convert_proof_to_h256(proof.left_proof)?,
right_proof: Self::convert_proof_to_h256(proof.right_proof)?,
})
}
/// Convert a Proof<H256> to Proof<OptionalHash>
pub fn convert_proof_from_h256(
proof: Proof<H256>,
) -> Result<Proof<OptionalHash>, anyhow::Error> {
let lemma = proof
.lemma()
.iter()
.map(|h| OptionalHash::some(*h))
.collect();
let path = proof.path().to_vec();
Proof::new(lemma, path)
}
/// Convert a RangeProof<H256> to RangeProof<OptionalHash>
pub fn convert_range_proof_from_h256(
range_proof: RangeProof<H256>,
) -> Result<RangeProof<OptionalHash>, anyhow::Error> {
Ok(RangeProof {
left_proof: Self::convert_proof_from_h256(range_proof.left_proof)?,
right_proof: Self::convert_proof_from_h256(range_proof.right_proof)?,
})
}
/// Generate a proof and convert it to H256
pub fn gen_proof_h256(&self, leaf_index: usize) -> Result<Proof<H256>, anyhow::Error> {
let proof = self.gen_proof(leaf_index)?;
Self::convert_proof_to_h256(&proof)
Self::convert_proof_to_h256(proof)
}
/// Generate a range proof and convert it to H256
@ -64,7 +85,7 @@ impl AppendMerkleTree<OptionalHash, Sha3Algorithm> {
end_index: usize,
) -> Result<RangeProof<H256>, anyhow::Error> {
let proof = self.gen_range_proof(start_index, end_index)?;
Self::convert_range_proof_to_h256(&proof)
Self::convert_range_proof_to_h256(proof)
}
/// Get the root as H256 (unwraps the OptionalHash)

View File

@ -44,40 +44,6 @@ impl OptionalHash {
pub fn to_h256_or_zero(&self) -> H256 {
self.0.unwrap_or_else(H256::zero)
}
/// Convert a Proof<OptionalHash> to Proof<H256>
pub fn convert_proof_to_h256(proof: Proof<OptionalHash>) -> Result<Proof<H256>, anyhow::Error> {
let lemma = proof
.lemma()
.iter()
.map(|oh| oh.to_h256_or_zero())
.collect();
let path = proof.path().to_vec();
Proof::new(lemma, path)
}
/// Convert a Proof<H256> to Proof<OptionalHash>
pub fn convert_proof_from_h256(
proof: Proof<H256>,
) -> Result<Proof<OptionalHash>, anyhow::Error> {
let lemma = proof
.lemma()
.iter()
.map(|h| OptionalHash::some(*h))
.collect();
let path = proof.path().to_vec();
Proof::new(lemma, path)
}
/// Convert a RangeProof<H256> to RangeProof<OptionalHash>
pub fn convert_range_proof_from_h256(
range_proof: RangeProof<H256>,
) -> Result<RangeProof<OptionalHash>, anyhow::Error> {
Ok(RangeProof {
left_proof: Self::convert_proof_from_h256(range_proof.left_proof)?,
right_proof: Self::convert_proof_from_h256(range_proof.right_proof)?,
})
}
}
// Add From conversions for easier usage
@ -156,8 +122,6 @@ impl Decode for OptionalHash {
}
let hash = H256::from_ssz_bytes(bytes)?;
// If it's H256::zero(), treat it as None, otherwise Some
Ok(OptionalHash::some(hash))
}
}
@ -196,7 +160,7 @@ impl HashElement for H256 {
}
fn null() -> Self {
H256::zero() // Use all zeros instead of 0x0101... to avoid collision
H256::repeat_byte(0x01)
}
}

View File

@ -12,7 +12,9 @@ use crate::log_store::{
use crate::{try_option, ZgsKeyValueDB};
use any::Any;
use anyhow::{anyhow, bail, Result};
use append_merkle::{MerkleTreeRead, NodeDatabase, NodeTransaction, OptionalHash};
use append_merkle::{
AppendMerkleTree, MerkleTreeRead, NodeDatabase, NodeTransaction, OptionalHash,
};
use itertools::Itertools;
use kvdb::DBTransaction;
use parking_lot::RwLock;
@ -73,7 +75,7 @@ impl FlowStore {
)
})?;
let optional_proof = merkle.gen_proof(sector_index)?;
OptionalHash::convert_proof_to_h256(optional_proof)
AppendMerkleTree::convert_proof_to_h256(optional_proof)
}
pub fn delete_batch_list(&self, batch_list: &[u64]) -> Result<()> {

View File

@ -9,7 +9,7 @@ use crate::log_store::{
};
use crate::{try_option, ZgsKeyValueDB};
use anyhow::{anyhow, bail, Result};
use append_merkle::{Algorithm, MerkleTreeRead, OptionalHash, Sha3Algorithm};
use append_merkle::{Algorithm, AppendMerkleTree, MerkleTreeRead, OptionalHash, Sha3Algorithm};
use ethereum_types::H256;
use kvdb_rocksdb::{Database, DatabaseConfig};
use merkle_light::merkle::{log2_pow2, MerkleTree};
@ -221,7 +221,7 @@ impl LogStoreChunkWrite for LogManager {
if let Some(file_proof) = maybe_file_proof {
// Convert H256 proof to OptionalHash proof
let optional_proof = OptionalHash::convert_proof_from_h256(file_proof)?;
let optional_proof = AppendMerkleTree::convert_proof_from_h256(file_proof)?;
// Convert H256 merkle nodes to OptionalHash merkle nodes
let optional_nodes: Vec<(usize, OptionalHash)> = tx
.merkle_nodes
@ -431,7 +431,7 @@ impl LogStoreWrite for LogManager {
let mut merkle = self.merkle.write();
if valid {
merkle.pora_chunks_merkle.fill_with_range_proof(
OptionalHash::convert_range_proof_from_h256(data.proof.clone())?,
AppendMerkleTree::convert_range_proof_from_h256(data.proof.clone())?,
)?;
}
Ok(valid)
@ -893,10 +893,10 @@ impl LogManager {
let merkle = self.merkle.read_recursive();
let seg_index = sector_to_segment(flow_index);
let top_proof = match maybe_tx_seq {
None => OptionalHash::convert_proof_to_h256(
None => AppendMerkleTree::convert_proof_to_h256(
merkle.pora_chunks_merkle.gen_proof(seg_index)?,
)?,
Some(tx_seq) => OptionalHash::convert_proof_to_h256(
Some(tx_seq) => AppendMerkleTree::convert_proof_to_h256(
merkle
.pora_chunks_merkle
.at_version(tx_seq)?
@ -918,12 +918,12 @@ impl LogManager {
.gen_proof_in_batch(seg_index, flow_index as usize % PORA_CHUNK_SIZE)?
} else {
match maybe_tx_seq {
None => OptionalHash::convert_proof_to_h256(
None => AppendMerkleTree::convert_proof_to_h256(
merkle
.last_chunk_merkle
.gen_proof(flow_index as usize % PORA_CHUNK_SIZE)?,
)?,
Some(tx_version) => OptionalHash::convert_proof_to_h256(
Some(tx_version) => AppendMerkleTree::convert_proof_to_h256(
merkle
.last_chunk_merkle
.at_version(tx_version)?
@ -1346,18 +1346,13 @@ pub fn data_to_merkle_leaves(leaf_data: &[u8]) -> Result<Vec<OptionalHash>> {
Ok(r)
}
/// Convert Vec<OptionalHash> to Vec<H256> for compatibility with existing code
pub fn optional_hash_to_h256_vec(optional_hashes: Vec<OptionalHash>) -> Vec<H256> {
optional_hashes
.into_iter()
.map(|oh| oh.to_h256_or_zero())
.collect()
}
/// Convenience function that combines data_to_merkle_leaves and conversion to H256
pub fn data_to_merkle_leaves_h256(leaf_data: &[u8]) -> Result<Vec<H256>> {
let optional_hashes = data_to_merkle_leaves(leaf_data)?;
Ok(optional_hash_to_h256_vec(optional_hashes))
Ok(optional_hashes
.into_iter()
.map(|oh| oh.to_h256_or_zero())
.collect())
}
pub fn bytes_to_entries(size_bytes: u64) -> u64 {

View File

@ -94,8 +94,10 @@ fn test_put_get() {
assert_eq!(
chunk_with_proof.proof,
OptionalHash::convert_proof_to_h256(h256_merkle.gen_proof(i + start_offset).unwrap())
.unwrap()
AppendMerkleTree::convert_proof_to_h256(
h256_merkle.gen_proof(i + start_offset).unwrap()
)
.unwrap()
);
let r = chunk_with_proof.proof.validate::<Sha3Algorithm>(
&Sha3Algorithm::leaf(&chunk_with_proof.chunk.0),