mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
pallet-mmr: RPC API and Runtime API work with block numbers (#12345)
* pallet-mmr: RPC API works with block_numbers * fixes * update rpc * fmt * final touches in the rpc * temporary fix * fix * fmt * docs * Update lib.rs * use NumberFor * validate input * update runtime * convert block_number to u64 * small edit * update runtime api * test fix * runtime fix * update test function * fmt * fix nits * remove block_num_to_leaf_index from runtime api * Update frame/merkle-mountain-range/src/lib.rs Co-authored-by: Robert Hambrock <roberthambrock@gmail.com> * fix tests * get the code to compile after merge * get the tests to compile * fix in tests? * fix test * Update frame/merkle-mountain-range/src/tests.rs Co-authored-by: Adrian Catangiu <adrian@parity.io> * Update frame/merkle-mountain-range/src/lib.rs Co-authored-by: Adrian Catangiu <adrian@parity.io> * Update primitives/merkle-mountain-range/src/lib.rs Co-authored-by: Adrian Catangiu <adrian@parity.io> * fix errors & nits * change block_num_to_leaf_index * don't make any assumptions * Update frame/merkle-mountain-range/src/tests.rs Co-authored-by: Adrian Catangiu <adrian@parity.io> * Update frame/merkle-mountain-range/src/tests.rs Co-authored-by: Adrian Catangiu <adrian@parity.io> * Update frame/merkle-mountain-range/src/tests.rs Co-authored-by: Adrian Catangiu <adrian@parity.io> * fix * small fix * use best_known_block_number * best_known_block_number instead of leaves_count * more readable? * remove warning * Update frame/merkle-mountain-range/src/lib.rs Co-authored-by: Robert Hambrock <roberthambrock@gmail.com> * simplify * update docs * nits * fmt & fix * merge fixes * fix * small fix * docs & nit fixes * Nit fixes * remove leaf_indices_to_block_numbers() * fmt Co-authored-by: Robert Hambrock <roberthambrock@gmail.com> Co-authored-by: Adrian Catangiu <adrian@parity.io>
This commit is contained in:
@@ -30,10 +30,10 @@ use jsonrpsee::{
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use sp_api::ProvideRuntimeApi;
|
||||
use sp_api::{NumberFor, ProvideRuntimeApi};
|
||||
use sp_blockchain::HeaderBackend;
|
||||
use sp_core::Bytes;
|
||||
use sp_mmr_primitives::{BatchProof, Error as MmrError, LeafIndex, Proof};
|
||||
use sp_mmr_primitives::{BatchProof, Error as MmrError, Proof};
|
||||
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
|
||||
|
||||
pub use sp_mmr_primitives::MmrApi as MmrRuntimeApi;
|
||||
@@ -96,11 +96,11 @@ impl<BlockHash> LeafBatchProof<BlockHash> {
|
||||
|
||||
/// MMR RPC methods.
|
||||
#[rpc(client, server)]
|
||||
pub trait MmrApi<BlockHash> {
|
||||
/// Generate MMR proof for given leaf index.
|
||||
pub trait MmrApi<BlockHash, BlockNumber> {
|
||||
/// Generate MMR proof for given block number.
|
||||
///
|
||||
/// This method calls into a runtime with MMR pallet included and attempts to generate
|
||||
/// MMR proof for leaf at given `leaf_index`.
|
||||
/// MMR proof for a block with a specified `block_number`.
|
||||
/// Optionally, a block hash at which the runtime should be queried can be specified.
|
||||
///
|
||||
/// Returns the (full) leaf itself and a proof for this leaf (compact encoding, i.e. hash of
|
||||
@@ -108,49 +108,49 @@ pub trait MmrApi<BlockHash> {
|
||||
#[method(name = "mmr_generateProof")]
|
||||
fn generate_proof(
|
||||
&self,
|
||||
leaf_index: LeafIndex,
|
||||
block_number: BlockNumber,
|
||||
at: Option<BlockHash>,
|
||||
) -> RpcResult<LeafProof<BlockHash>>;
|
||||
|
||||
/// Generate MMR proof for the given leaf indices.
|
||||
/// Generate MMR proof for the given block numbers.
|
||||
///
|
||||
/// This method calls into a runtime with MMR pallet included and attempts to generate
|
||||
/// MMR proof for a set of leaves at the given `leaf_indices`.
|
||||
/// MMR proof for a set of blocks with the specific `block_numbers`.
|
||||
/// Optionally, a block hash at which the runtime should be queried can be specified.
|
||||
///
|
||||
/// Returns the leaves and a proof for these leaves (compact encoding, i.e. hash of
|
||||
/// the leaves). Both parameters are SCALE-encoded.
|
||||
/// The order of entries in the `leaves` field of the returned struct
|
||||
/// is the same as the order of the entries in `leaf_indices` supplied
|
||||
/// is the same as the order of the entries in `block_numbers` supplied
|
||||
#[method(name = "mmr_generateBatchProof")]
|
||||
fn generate_batch_proof(
|
||||
&self,
|
||||
leaf_indices: Vec<LeafIndex>,
|
||||
block_numbers: Vec<BlockNumber>,
|
||||
at: Option<BlockHash>,
|
||||
) -> RpcResult<LeafBatchProof<BlockHash>>;
|
||||
|
||||
/// Generate a MMR proof for the given `leaf_indices` of the MMR that had `leaves_count` leaves.
|
||||
/// Generate a MMR proof for the given `block_numbers` given the `best_known_block_number`.
|
||||
///
|
||||
/// This method calls into a runtime with MMR pallet included and attempts to generate
|
||||
/// a MMR proof for the set of leaves at the given `leaf_indices` with MMR fixed to the state
|
||||
/// with exactly `leaves_count` leaves. `leaves_count` must be larger than all `leaf_indices`
|
||||
/// for the function to succeed.
|
||||
/// a MMR proof for the set of blocks that have the given `block_numbers` with MMR given the
|
||||
/// `best_known_block_number`. `best_known_block_number` must be larger than all the
|
||||
/// `block_numbers` for the function to succeed.
|
||||
///
|
||||
/// Optionally, a block hash at which the runtime should be queried can be specified.
|
||||
/// Note that specifying the block hash isn't super-useful here, unless you're generating
|
||||
/// proof using non-finalized blocks where there are several competing forks. That's because
|
||||
/// MMR state will be fixed to the state with `leaves_count`, which already points to some
|
||||
/// historical block.
|
||||
/// MMR state will be fixed to the state with `best_known_block_number`, which already points to
|
||||
/// some historical block.
|
||||
///
|
||||
/// Returns the leaves and a proof for these leaves (compact encoding, i.e. hash of
|
||||
/// the leaves). Both parameters are SCALE-encoded.
|
||||
/// The order of entries in the `leaves` field of the returned struct
|
||||
/// is the same as the order of the entries in `leaf_indices` supplied
|
||||
/// is the same as the order of the entries in `block_numbers` supplied
|
||||
#[method(name = "mmr_generateHistoricalBatchProof")]
|
||||
fn generate_historical_batch_proof(
|
||||
&self,
|
||||
leaf_indices: Vec<LeafIndex>,
|
||||
leaves_count: LeafIndex,
|
||||
block_numbers: Vec<BlockNumber>,
|
||||
best_known_block_number: BlockNumber,
|
||||
at: Option<BlockHash>,
|
||||
) -> RpcResult<LeafBatchProof<BlockHash>>;
|
||||
}
|
||||
@@ -169,16 +169,17 @@ impl<C, B> Mmr<C, B> {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<Client, Block, MmrHash> MmrApiServer<<Block as BlockT>::Hash> for Mmr<Client, (Block, MmrHash)>
|
||||
impl<Client, Block, MmrHash> MmrApiServer<<Block as BlockT>::Hash, NumberFor<Block>>
|
||||
for Mmr<Client, (Block, MmrHash)>
|
||||
where
|
||||
Block: BlockT,
|
||||
Client: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
|
||||
Client::Api: MmrRuntimeApi<Block, MmrHash>,
|
||||
Client::Api: MmrRuntimeApi<Block, MmrHash, NumberFor<Block>>,
|
||||
MmrHash: Codec + Send + Sync + 'static,
|
||||
{
|
||||
fn generate_proof(
|
||||
&self,
|
||||
leaf_index: LeafIndex,
|
||||
block_number: NumberFor<Block>,
|
||||
at: Option<<Block as BlockT>::Hash>,
|
||||
) -> RpcResult<LeafProof<Block::Hash>> {
|
||||
let api = self.client.runtime_api();
|
||||
@@ -188,7 +189,7 @@ where
|
||||
.generate_proof_with_context(
|
||||
&BlockId::hash(block_hash),
|
||||
sp_core::ExecutionContext::OffchainCall(None),
|
||||
leaf_index,
|
||||
block_number,
|
||||
)
|
||||
.map_err(runtime_error_into_rpc_error)?
|
||||
.map_err(mmr_error_into_rpc_error)?;
|
||||
@@ -198,7 +199,7 @@ where
|
||||
|
||||
fn generate_batch_proof(
|
||||
&self,
|
||||
leaf_indices: Vec<LeafIndex>,
|
||||
block_numbers: Vec<NumberFor<Block>>,
|
||||
at: Option<<Block as BlockT>::Hash>,
|
||||
) -> RpcResult<LeafBatchProof<<Block as BlockT>::Hash>> {
|
||||
let api = self.client.runtime_api();
|
||||
@@ -210,7 +211,7 @@ where
|
||||
.generate_batch_proof_with_context(
|
||||
&BlockId::hash(block_hash),
|
||||
sp_core::ExecutionContext::OffchainCall(None),
|
||||
leaf_indices,
|
||||
block_numbers,
|
||||
)
|
||||
.map_err(runtime_error_into_rpc_error)?
|
||||
.map_err(mmr_error_into_rpc_error)?;
|
||||
@@ -220,8 +221,8 @@ where
|
||||
|
||||
fn generate_historical_batch_proof(
|
||||
&self,
|
||||
leaf_indices: Vec<LeafIndex>,
|
||||
leaves_count: LeafIndex,
|
||||
block_numbers: Vec<NumberFor<Block>>,
|
||||
best_known_block_number: NumberFor<Block>,
|
||||
at: Option<<Block as BlockT>::Hash>,
|
||||
) -> RpcResult<LeafBatchProof<<Block as BlockT>::Hash>> {
|
||||
let api = self.client.runtime_api();
|
||||
@@ -233,8 +234,8 @@ where
|
||||
.generate_historical_batch_proof_with_context(
|
||||
&BlockId::hash(block_hash),
|
||||
sp_core::ExecutionContext::OffchainCall(None),
|
||||
leaf_indices,
|
||||
leaves_count,
|
||||
block_numbers,
|
||||
best_known_block_number,
|
||||
)
|
||||
.map_err(runtime_error_into_rpc_error)?
|
||||
.map_err(mmr_error_into_rpc_error)?;
|
||||
|
||||
@@ -59,7 +59,7 @@
|
||||
use codec::Encode;
|
||||
use frame_support::weights::Weight;
|
||||
use sp_runtime::{
|
||||
traits::{self, One, Saturating},
|
||||
traits::{self, CheckedSub, One, Saturating},
|
||||
SaturatedConversion,
|
||||
};
|
||||
|
||||
@@ -318,37 +318,73 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
.saturating_add(leaf_index.saturated_into())
|
||||
}
|
||||
|
||||
/// Generate a MMR proof for the given `leaf_indices`.
|
||||
/// Convert a `block_num` into a leaf index.
|
||||
fn block_num_to_leaf_index(block_num: T::BlockNumber) -> Result<LeafIndex, primitives::Error>
|
||||
where
|
||||
T: frame_system::Config,
|
||||
{
|
||||
// leaf_idx = (leaves_count - 1) - (current_block_num - block_num);
|
||||
let best_block_num = <frame_system::Pallet<T>>::block_number();
|
||||
let blocks_diff = best_block_num.checked_sub(&block_num).ok_or_else(|| {
|
||||
primitives::Error::BlockNumToLeafIndex
|
||||
.log_debug("The provided block_number is greater than the best block number.")
|
||||
})?;
|
||||
let blocks_diff_as_leaf_idx = blocks_diff.try_into().map_err(|_| {
|
||||
primitives::Error::BlockNumToLeafIndex
|
||||
.log_debug("The `blocks_diff` couldn't be converted to `LeafIndex`.")
|
||||
})?;
|
||||
|
||||
let leaf_idx = Self::mmr_leaves()
|
||||
.checked_sub(1)
|
||||
.and_then(|last_leaf_idx| last_leaf_idx.checked_sub(blocks_diff_as_leaf_idx))
|
||||
.ok_or_else(|| {
|
||||
primitives::Error::BlockNumToLeafIndex
|
||||
.log_debug("There aren't enough leaves in the chain.")
|
||||
})?;
|
||||
Ok(leaf_idx)
|
||||
}
|
||||
|
||||
/// Generate a MMR proof for the given `block_numbers`.
|
||||
///
|
||||
/// Note this method can only be used from an off-chain context
|
||||
/// (Offchain Worker or Runtime API call), since it requires
|
||||
/// all the leaves to be present.
|
||||
/// It may return an error or panic if used incorrectly.
|
||||
pub fn generate_batch_proof(
|
||||
leaf_indices: Vec<LeafIndex>,
|
||||
block_numbers: Vec<T::BlockNumber>,
|
||||
) -> Result<
|
||||
(Vec<LeafOf<T, I>>, primitives::BatchProof<<T as Config<I>>::Hash>),
|
||||
primitives::Error,
|
||||
> {
|
||||
Self::generate_historical_batch_proof(leaf_indices, Self::mmr_leaves())
|
||||
Self::generate_historical_batch_proof(
|
||||
block_numbers,
|
||||
<frame_system::Pallet<T>>::block_number(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Generate a MMR proof for the given `leaf_indices` for the MMR of `leaves_count` size.
|
||||
/// Generate a MMR proof for the given `block_numbers` given the `best_known_block_number`.
|
||||
///
|
||||
/// Note this method can only be used from an off-chain context
|
||||
/// (Offchain Worker or Runtime API call), since it requires
|
||||
/// all the leaves to be present.
|
||||
/// It may return an error or panic if used incorrectly.
|
||||
pub fn generate_historical_batch_proof(
|
||||
leaf_indices: Vec<LeafIndex>,
|
||||
leaves_count: LeafIndex,
|
||||
block_numbers: Vec<T::BlockNumber>,
|
||||
best_known_block_number: T::BlockNumber,
|
||||
) -> Result<
|
||||
(Vec<LeafOf<T, I>>, primitives::BatchProof<<T as Config<I>>::Hash>),
|
||||
primitives::Error,
|
||||
> {
|
||||
if leaves_count > Self::mmr_leaves() {
|
||||
return Err(Error::InvalidLeavesCount)
|
||||
}
|
||||
let leaves_count =
|
||||
Self::block_num_to_leaf_index(best_known_block_number)?.saturating_add(1);
|
||||
|
||||
// we need to translate the block_numbers into leaf indices.
|
||||
let leaf_indices = block_numbers
|
||||
.iter()
|
||||
.map(|block_num| -> Result<LeafIndex, primitives::Error> {
|
||||
Self::block_num_to_leaf_index(*block_num)
|
||||
})
|
||||
.collect::<Result<Vec<LeafIndex>, _>>()?;
|
||||
|
||||
let mmr: ModuleMmr<mmr::storage::OffchainStorage, T, I> = mmr::Mmr::new(leaves_count);
|
||||
mmr.generate_batch_proof(leaf_indices)
|
||||
|
||||
@@ -235,22 +235,21 @@ fn should_generate_proofs_correctly() {
|
||||
// to retrieve full leaf data.
|
||||
register_offchain_ext(&mut ext);
|
||||
ext.execute_with(|| {
|
||||
// when generate proofs for all leaves
|
||||
let proofs = (0_u64..crate::NumberOfLeaves::<Test>::get())
|
||||
let best_block_number = frame_system::Pallet::<Test>::block_number();
|
||||
// when generate proofs for all leaves.
|
||||
let proofs = (1_u64..=best_block_number)
|
||||
.into_iter()
|
||||
.map(|leaf_index| {
|
||||
crate::Pallet::<Test>::generate_batch_proof(vec![leaf_index]).unwrap()
|
||||
})
|
||||
.map(|block_num| crate::Pallet::<Test>::generate_batch_proof(vec![block_num]).unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
// when generate historical proofs for all leaves
|
||||
let historical_proofs = (0_u64..crate::NumberOfLeaves::<Test>::get())
|
||||
let historical_proofs = (1_u64..best_block_number)
|
||||
.into_iter()
|
||||
.map(|leaf_index| {
|
||||
.map(|block_num| {
|
||||
let mut proofs = vec![];
|
||||
for leaves_count in leaf_index + 1..=num_blocks {
|
||||
for leaves_count in block_num..=num_blocks {
|
||||
proofs.push(
|
||||
crate::Pallet::<Test>::generate_historical_batch_proof(
|
||||
vec![leaf_index],
|
||||
vec![block_num],
|
||||
leaves_count,
|
||||
)
|
||||
.unwrap(),
|
||||
@@ -321,7 +320,7 @@ fn should_generate_proofs_correctly() {
|
||||
leaf_count: 3,
|
||||
items: vec![hex(
|
||||
"672c04a9cd05a644789d769daa552d35d8de7c33129f8a7cbf49e595234c4854"
|
||||
),],
|
||||
)],
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -352,6 +351,7 @@ fn should_generate_proofs_correctly() {
|
||||
assert_eq!(
|
||||
proofs[4],
|
||||
(
|
||||
// NOTE: the leaf index is equivalent to the block number(in this case 5) - 1
|
||||
vec![Compact::new(((4, H256::repeat_byte(5)).into(), LeafData::new(5).into(),))],
|
||||
BatchProof {
|
||||
leaf_indices: vec![4],
|
||||
@@ -393,7 +393,7 @@ fn should_generate_proofs_correctly() {
|
||||
}
|
||||
)
|
||||
);
|
||||
assert_eq!(historical_proofs[6][0], proofs[6]);
|
||||
assert_eq!(historical_proofs[5][1], proofs[5]);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -410,11 +410,12 @@ fn should_generate_batch_proof_correctly() {
|
||||
register_offchain_ext(&mut ext);
|
||||
ext.execute_with(|| {
|
||||
// when generate proofs for a batch of leaves
|
||||
let (.., proof) = crate::Pallet::<Test>::generate_batch_proof(vec![0, 4, 5]).unwrap();
|
||||
let (.., proof) = crate::Pallet::<Test>::generate_batch_proof(vec![1, 5, 6]).unwrap();
|
||||
// then
|
||||
assert_eq!(
|
||||
proof,
|
||||
BatchProof {
|
||||
// the leaf indices are equivalent to the above specified block numbers - 1.
|
||||
leaf_indices: vec![0, 4, 5],
|
||||
leaf_count: 7,
|
||||
items: vec![
|
||||
@@ -427,7 +428,7 @@ fn should_generate_batch_proof_correctly() {
|
||||
|
||||
// when generate historical proofs for a batch of leaves
|
||||
let (.., historical_proof) =
|
||||
crate::Pallet::<Test>::generate_historical_batch_proof(vec![0, 4, 5], 6).unwrap();
|
||||
crate::Pallet::<Test>::generate_historical_batch_proof(vec![1, 5, 6], 6).unwrap();
|
||||
// then
|
||||
assert_eq!(
|
||||
historical_proof,
|
||||
@@ -443,7 +444,7 @@ fn should_generate_batch_proof_correctly() {
|
||||
|
||||
// when generate historical proofs for a batch of leaves
|
||||
let (.., historical_proof) =
|
||||
crate::Pallet::<Test>::generate_historical_batch_proof(vec![0, 4, 5], 7).unwrap();
|
||||
crate::Pallet::<Test>::generate_historical_batch_proof(vec![1, 5, 6], 7).unwrap();
|
||||
// then
|
||||
assert_eq!(historical_proof, proof);
|
||||
});
|
||||
@@ -500,15 +501,15 @@ fn should_verify() {
|
||||
fn should_verify_batch_proofs() {
|
||||
fn generate_and_verify_batch_proof(
|
||||
ext: &mut sp_io::TestExternalities,
|
||||
leaf_indices: &Vec<u64>,
|
||||
block_numbers: &Vec<u64>,
|
||||
blocks_to_add: usize,
|
||||
) {
|
||||
let (leaves, proof) = ext.execute_with(|| {
|
||||
crate::Pallet::<Test>::generate_batch_proof(leaf_indices.to_vec()).unwrap()
|
||||
crate::Pallet::<Test>::generate_batch_proof(block_numbers.to_vec()).unwrap()
|
||||
});
|
||||
|
||||
let mmr_size = ext.execute_with(|| crate::Pallet::<Test>::mmr_leaves());
|
||||
let min_mmr_size = leaf_indices.iter().max().unwrap() + 1;
|
||||
let min_mmr_size = block_numbers.iter().max().unwrap() + 1;
|
||||
|
||||
// generate historical proofs for all possible mmr sizes,
|
||||
// lower bound being index of highest leaf to be proven
|
||||
@@ -516,7 +517,7 @@ fn should_verify_batch_proofs() {
|
||||
.map(|mmr_size| {
|
||||
ext.execute_with(|| {
|
||||
crate::Pallet::<Test>::generate_historical_batch_proof(
|
||||
leaf_indices.to_vec(),
|
||||
block_numbers.to_vec(),
|
||||
mmr_size,
|
||||
)
|
||||
.unwrap()
|
||||
@@ -546,39 +547,41 @@ fn should_verify_batch_proofs() {
|
||||
// to retrieve full leaf data when generating proofs
|
||||
register_offchain_ext(&mut ext);
|
||||
|
||||
// verify that up to n=10, valid proofs are generated for all possible leaf combinations
|
||||
for n in 0..10 {
|
||||
// verify that up to n=10, valid proofs are generated for all possible block number
|
||||
// combinations.
|
||||
for n in 1..=10 {
|
||||
ext.execute_with(|| new_block());
|
||||
ext.persist_offchain_overlay();
|
||||
|
||||
// generate powerset (skipping empty set) of all possible leaf combinations for mmr size n
|
||||
let leaves_set: Vec<Vec<u64>> = (0..=n).into_iter().powerset().skip(1).collect();
|
||||
// generate powerset (skipping empty set) of all possible block number combinations for mmr
|
||||
// size n.
|
||||
let blocks_set: Vec<Vec<u64>> = (1..=n).into_iter().powerset().skip(1).collect();
|
||||
|
||||
leaves_set.iter().for_each(|leaves_subset| {
|
||||
generate_and_verify_batch_proof(&mut ext, leaves_subset, 0);
|
||||
blocks_set.iter().for_each(|blocks_subset| {
|
||||
generate_and_verify_batch_proof(&mut ext, &blocks_subset, 0);
|
||||
ext.persist_offchain_overlay();
|
||||
});
|
||||
}
|
||||
|
||||
// verify that up to n=15, valid proofs are generated for all possible 2-leaf combinations
|
||||
for n in 10..15 {
|
||||
// (MMR Leafs)
|
||||
// verify that up to n=15, valid proofs are generated for all possible 2-block number
|
||||
// combinations.
|
||||
for n in 11..=15 {
|
||||
ext.execute_with(|| new_block());
|
||||
ext.persist_offchain_overlay();
|
||||
|
||||
// generate all possible 2-leaf combinations for mmr size n
|
||||
let leaves_set: Vec<Vec<u64>> = (0..=n).into_iter().combinations(2).collect();
|
||||
// generate all possible 2-block number combinations for mmr size n.
|
||||
let blocks_set: Vec<Vec<u64>> = (1..=n).into_iter().combinations(2).collect();
|
||||
|
||||
leaves_set.iter().for_each(|leaves_subset| {
|
||||
generate_and_verify_batch_proof(&mut ext, leaves_subset, 0);
|
||||
blocks_set.iter().for_each(|blocks_subset| {
|
||||
generate_and_verify_batch_proof(&mut ext, &blocks_subset, 0);
|
||||
ext.persist_offchain_overlay();
|
||||
});
|
||||
}
|
||||
|
||||
generate_and_verify_batch_proof(&mut ext, &vec![7, 11], 20);
|
||||
generate_and_verify_batch_proof(&mut ext, &vec![8, 12], 20);
|
||||
ext.execute_with(|| add_blocks(1000));
|
||||
ext.persist_offchain_overlay();
|
||||
generate_and_verify_batch_proof(&mut ext, &vec![7, 11, 100, 800], 100);
|
||||
generate_and_verify_batch_proof(&mut ext, &vec![8, 12, 100, 800], 100);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -650,11 +653,11 @@ fn should_verify_batch_proof_statelessly() {
|
||||
register_offchain_ext(&mut ext);
|
||||
let (leaves, proof) = ext.execute_with(|| {
|
||||
// when
|
||||
crate::Pallet::<Test>::generate_batch_proof(vec![0, 4, 5]).unwrap()
|
||||
crate::Pallet::<Test>::generate_batch_proof(vec![1, 4, 5]).unwrap()
|
||||
});
|
||||
let (historical_leaves, historical_proof) = ext.execute_with(|| {
|
||||
// when
|
||||
crate::Pallet::<Test>::generate_historical_batch_proof(vec![0, 4, 5], 6).unwrap()
|
||||
crate::Pallet::<Test>::generate_historical_batch_proof(vec![1, 4, 5], 6).unwrap()
|
||||
});
|
||||
|
||||
// Verify proof without relying on any on-chain data.
|
||||
@@ -920,7 +923,7 @@ fn should_verify_canonicalized() {
|
||||
|
||||
// Generate proofs for some blocks.
|
||||
let (leaves, proofs) =
|
||||
ext.execute_with(|| crate::Pallet::<Test>::generate_batch_proof(vec![0, 4, 5, 7]).unwrap());
|
||||
ext.execute_with(|| crate::Pallet::<Test>::generate_batch_proof(vec![1, 4, 5, 7]).unwrap());
|
||||
// Verify all previously generated proofs.
|
||||
ext.execute_with(|| {
|
||||
assert_eq!(crate::Pallet::<Test>::verify_leaves(leaves, proofs), Ok(()));
|
||||
@@ -953,19 +956,19 @@ fn does_not_panic_when_generating_historical_proofs() {
|
||||
// when leaf index is invalid
|
||||
assert_eq!(
|
||||
crate::Pallet::<Test>::generate_historical_batch_proof(vec![10], 7),
|
||||
Err(Error::LeafNotFound),
|
||||
Err(Error::BlockNumToLeafIndex),
|
||||
);
|
||||
|
||||
// when leaves count is invalid
|
||||
assert_eq!(
|
||||
crate::Pallet::<Test>::generate_historical_batch_proof(vec![3], 100),
|
||||
Err(Error::InvalidLeavesCount),
|
||||
Err(Error::BlockNumToLeafIndex),
|
||||
);
|
||||
|
||||
// when both leaf index and leaves count are invalid
|
||||
assert_eq!(
|
||||
crate::Pallet::<Test>::generate_historical_batch_proof(vec![10], 100),
|
||||
Err(Error::InvalidLeavesCount),
|
||||
Err(Error::BlockNumToLeafIndex),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user