[][src]Struct exonum_merkledb::indexes::proof_map::MapProof

pub struct MapProof<K, V, KeyMode = Hashed> { /* fields omitted */ }

View of a ProofMapIndex, i.e., a subset of its elements coupled with a proof, which jointly allow restoring the object_hash() of the index. Apart from the existing elements, MapProof can assert absence of certain keys from the underlying index.

Workflow

You can create MapProofs with get_proof() and get_multiproof() methods of ProofMapIndex. Proofs can be verified on the server side with the help of check(). Prior to the check conversion, you may use *unchecked methods to obtain information about the proof.

let fork = { let db = TemporaryDB::new(); db.fork() };
let mut map = fork.get_proof_map("index");
let (h1, h2, h3) = (hash(&[1]), hash(&[2]), hash(&[3]));
map.put(&h1, 100u32);
map.put(&h2, 200u32);

// Get the proof from the index.
let proof = map.get_multiproof(vec![h1, h3]);

// Check the proof consistency.
let checked_proof = proof.check()?;
assert!(checked_proof.entries().eq(vec![(&h1, &100u32)]));
assert!(checked_proof.missing_keys().eq(vec![&h3]));
assert_eq!(checked_proof.index_hash(), map.object_hash());

// If the trusted list hash is known, there is a convenient method
// to combine integrity check and hash equality check.
let checked_proof = proof.check_against_hash(map.object_hash())?;

JSON serialization

MapProof is serialized to JSON as an object with 2 array fields:

  • proof is an array of { path: ProofPath, hash: Hash } objects.
  • entries is an array with 2 kinds of objects: { missing: K } for keys missing from the underlying index, and { key: K, value: V } for key-value pairs, existence of which is asserted by the proof.
let fork = { let db = TemporaryDB::new(); db.fork() };
let mut map = fork.get_proof_map("index");
let (h1, h2) = (HashTag::hash_leaf(&[1]), HashTag::hash_leaf(&[2]));
map.put(&h1, 100_u32);
map.put(&h2, 200_u32);

let proof = map.get_proof(h2);
assert_eq!(
    serde_json::to_value(&proof).unwrap(),
    json!({
        "proof": [{
            "path": Hashed::transform_key(&h1),
            "hash": HashTag::hash_leaf(&100_u32.to_bytes()),
        }],
        "entries": [{ "key": h2, "value": 200 }],
    })
);

Note on external implementations

External implementations (e.g., in light clients) must treat serialized MapProofs as untrusted inputs. Implementations may rely on the invariants provided by Exonum nodes (e.g., ordering of proof; see check()) only if these invariants are checked during proof verification.

Methods

impl<K, V, KeyMode> MapProof<K, V, KeyMode>[src]

pub fn proof_unchecked(&self) -> Vec<(ProofPath, Hash)>[src]

Provides access to the proof part of the view. Useful mainly for debug purposes.

pub fn missing_keys_unchecked(&self) -> impl Iterator<Item = &K>[src]

Retrieves references to keys that the proof shows as missing from the map. This method does not perform any integrity checks of the proof.

pub fn all_entries_unchecked(&self) -> impl Iterator<Item = (&K, Option<&V>)>[src]

Retrieves references to existing and non-existing entries in the proof.

Existing entries have Some value, non-existing have None. This method does not perform any integrity checks of the proof.

impl<K, V, KeyMode> MapProof<K, V, KeyMode> where
    V: BinaryValue,
    KeyMode: ToProofPath<K>, 
[src]

pub fn check(&self) -> Result<CheckedMapProof<K, V>, MapProofError>[src]

Checks this proof.

Errors

An error is returned if proof is malformed. The following checks are performed:

  • proof elements are ordered by increasing path field.
  • No path in proof is a prefix of another path in proof or a path inferred from an entry.
  • Paths in proof and ones computed from entries are all distinct.

Examples

let fork = { let db = TemporaryDB::new(); db.fork() };
let mut map = fork.get_proof_map("index");
let (h1, h2) = (hash(&[1]), hash(&[2]));
map.put(&h1, 100u32);
map.put(&h2, 200u32);

let proof = map.get_proof(h2);
let checked_proof = proof.check().unwrap();
assert_eq!(checked_proof.entries().collect::<Vec<_>>(), vec![(&h2, &200u32)]);
assert_eq!(checked_proof.index_hash(), map.object_hash());

pub fn check_against_hash(
    &self,
    expected_map_hash: Hash
) -> Result<CheckedMapProof<K, V>, ValidationError<MapProofError>>
[src]

Checks this proof against a trusted map hash. Fails if the proof is malformed or the hash does not match the one computed from the proof.

pub fn map_values<U, F>(self, map_fn: F) -> MapProof<K, U, KeyMode> where
    U: BinaryValue,
    F: FnMut(V) -> U, 
[src]

Maps values in this proof. Note that this transform may render the proof invalid.

Trait Implementations

impl<K: Clone, V: Clone, KeyMode: Clone> Clone for MapProof<K, V, KeyMode>[src]

impl<K: Debug, V: Debug, KeyMode: Debug> Debug for MapProof<K, V, KeyMode>[src]

impl<'de, K, V, KeyMode> Deserialize<'de> for MapProof<K, V, KeyMode> where
    K: Deserialize<'de>,
    V: Deserialize<'de>, 
[src]

impl<K: PartialEq, V: PartialEq, KeyMode: PartialEq> PartialEq<MapProof<K, V, KeyMode>> for MapProof<K, V, KeyMode>[src]

impl<K, V, S> ProtobufConvert for MapProof<K, V, S> where
    K: BinaryValue,
    V: BinaryValue
[src]

type ProtoStruct = MapProof

Type generated from the Protobuf definition.

impl<K, V, KeyMode> Serialize for MapProof<K, V, KeyMode> where
    K: Serialize,
    V: Serialize
[src]

impl<K, V, KeyMode> StructuralPartialEq for MapProof<K, V, KeyMode>[src]

Auto Trait Implementations

impl<K, V, KeyMode> RefUnwindSafe for MapProof<K, V, KeyMode> where
    K: RefUnwindSafe,
    KeyMode: RefUnwindSafe,
    V: RefUnwindSafe

impl<K, V, KeyMode> Send for MapProof<K, V, KeyMode> where
    K: Send,
    KeyMode: Send,
    V: Send

impl<K, V, KeyMode> Sync for MapProof<K, V, KeyMode> where
    K: Sync,
    KeyMode: Sync,
    V: Sync

impl<K, V, KeyMode> Unpin for MapProof<K, V, KeyMode> where
    K: Unpin,
    KeyMode: Unpin,
    V: Unpin

impl<K, V, KeyMode> UnwindSafe for MapProof<K, V, KeyMode> where
    K: UnwindSafe,
    KeyMode: UnwindSafe,
    V: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,