use alloc::collections::BTreeSet;
use core::hash::Hash;
use scale_info::TypeInfo;
use sp_core::{Decode, Encode};
use trie_db::{RecordedForKey, TrieAccess, TrieRecorder};
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug, TypeInfo)]
pub enum Error {
UnusedNodes,
}
pub struct AccessedNodesTracker<H: Hash> {
proof_nodes_count: usize,
recorder: BTreeSet<H>,
}
impl<H: Hash> AccessedNodesTracker<H> {
pub fn new(proof_nodes_count: usize) -> Self {
Self { proof_nodes_count, recorder: BTreeSet::new() }
}
pub fn ensure_no_unused_nodes(self) -> Result<(), Error> {
if self.proof_nodes_count != self.recorder.len() {
return Err(Error::UnusedNodes)
}
Ok(())
}
}
impl<H: Hash + Ord> TrieRecorder<H> for AccessedNodesTracker<H> {
fn record(&mut self, access: TrieAccess<H>) {
match access {
TrieAccess::NodeOwned { hash, .. } |
TrieAccess::EncodedNode { hash, .. } |
TrieAccess::Value { hash, .. } => {
self.recorder.insert(hash);
},
_ => {},
}
}
fn trie_nodes_recorded_for_key(&self, _key: &[u8]) -> RecordedForKey {
RecordedForKey::None
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::{tests::create_storage_proof, StorageProof};
use hash_db::Hasher;
use trie_db::{Trie, TrieDBBuilder};
type Hash = <sp_core::Blake2Hasher as Hasher>::Out;
type Layout = crate::LayoutV1<sp_core::Blake2Hasher>;
const TEST_DATA: &[(&[u8], &[u8])] =
&[(b"key1", &[1; 64]), (b"key2", &[2; 64]), (b"key3", &[3; 64])];
#[test]
fn proof_with_unused_nodes_is_rejected() {
let (raw_proof, root) = create_storage_proof::<Layout>(TEST_DATA);
let proof = StorageProof::new(raw_proof.clone());
let proof_nodes_count = proof.len();
let mut accessed_nodes_tracker = AccessedNodesTracker::<Hash>::new(proof_nodes_count);
{
let db = proof.clone().into_memory_db();
let trie = TrieDBBuilder::<Layout>::new(&db, &root)
.with_recorder(&mut accessed_nodes_tracker)
.build();
trie.get(b"key1").unwrap().unwrap();
trie.get(b"key2").unwrap().unwrap();
trie.get(b"key3").unwrap().unwrap();
}
assert_eq!(accessed_nodes_tracker.ensure_no_unused_nodes(), Ok(()));
let mut accessed_nodes_tracker = AccessedNodesTracker::<Hash>::new(proof_nodes_count);
{
let db = proof.into_memory_db();
let trie = TrieDBBuilder::<Layout>::new(&db, &root)
.with_recorder(&mut accessed_nodes_tracker)
.build();
trie.get(b"key1").unwrap().unwrap();
trie.get(b"key2").unwrap().unwrap();
}
assert_eq!(accessed_nodes_tracker.ensure_no_unused_nodes(), Err(Error::UnusedNodes));
}
}