as_signatures_map.rs

Overview

This file provides functionality to convert a collection of cryptographic attestations into a structured mapping of signatures, organized by their associated block and attestation target type. The primary purpose is to aggregate signer indices related to specific attestations, enabling efficient lookup and verification of which signers endorsed which blocks and attestation targets.

The core abstraction is the AsSignaturesMap trait, which defines a method to transform a set of envelopes (attestations with signatures) into a map keyed by (BlockIdentifier, AttestationTargetType) pairs, with corresponding sets of SignerIndex values representing the signers involved.


Traits and Implementations

Trait: AsSignaturesMap

pub trait AsSignaturesMap {
    fn as_signatures_map(
        &self,
    ) -> HashMap<(BlockIdentifier, AttestationTargetType), HashSet<SignerIndex>>;
}

Implementation for Vec<Envelope<GoshBLS, AttestationData>>

impl AsSignaturesMap for Vec<Envelope<GoshBLS, AttestationData>> {
    fn as_signatures_map(
        &self,
    ) -> HashMap<(BlockIdentifier, AttestationTargetType), HashSet<SignerIndex>> {
        let mut attestations_map =
            HashMap::<(BlockIdentifier, AttestationTargetType), HashSet<SignerIndex>>::new();
        for attestation in self.iter() {
            let attestation_target =
                (attestation.data().block_id().clone(), *attestation.data().target_type());
            let attestation_signers =
                HashSet::from_iter(attestation.clone_signature_occurrences().keys().cloned());
            attestations_map
                .entry(attestation_target)
                .and_modify(|e| e.extend(attestation_signers.clone().into_iter()))
                .or_insert(attestation_signers);
        }
        attestations_map
    }
}

Parameters

Return Value

Usage Example

let envelopes: Vec<Envelope<GoshBLS, AttestationData>> = get_attestations();
let signatures_map = envelopes.as_signatures_map();

for ((block_id, target_type), signers) in signatures_map.iter() {
    println!("Block: {:?}, Target: {:?}, Signers: {:?}", block_id, target_type, signers);
}

Implementation Details

This approach aggregates all signers who have signed attestations for the same block and target type, effectively merging signature sets where attestations overlap.


Important Types

These types are imported from other modules, and their detailed descriptions can be found under the topics related to Cryptographic Envelopes, Block Identification, and Attestation Data Structures.


Interaction with Other Parts of the System


Diagram: File Structure and Functional Flow

flowchart TD
A[Vec<Envelope<GoshBLS, AttestationData>>] -->|as_signatures_map()| B["HashMap<(BlockIdentifier, AttestationTargetType), HashSet<SignerIndex>>"]
subgraph AsSignaturesMap Trait
B
end
B --> C{Iterate over each Envelope}
C --> D["Extract attestation_target: (BlockIdentifier, AttestationTargetType)"]
C --> E[Extract attestation_signers: HashSet<SignerIndex>]
D & E --> F[Insert or update entry in HashMap]