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>>;
}
Purpose: Defines an interface for converting a collection of attestations into a map that associates each
(BlockIdentifier, AttestationTargetType)tuple with a set of signers who have attested to it.Returns: A
HashMapwhere:Key: A tuple
(BlockIdentifier, AttestationTargetType)uniquely identifying the attestation context.Value: A
HashSet<SignerIndex>containing the indices of signers who have signed for that attestation.
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
self: A reference to a vector ofEnvelope<GoshBLS, AttestationData>objects, where each envelope represents an attestation signed by one or more signers.
Return Value
Returns a
HashMapmapping(BlockIdentifier, AttestationTargetType)pairs to sets ofSignerIndexvalues representing the signers for each attestation target.
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
The method iterates over each
Envelopein the vector.For each envelope, it extracts:
The attestation target as a tuple
(BlockIdentifier, AttestationTargetType).The set of signer indices by cloning the keys of the signature occurrences map within the envelope.
It then inserts or updates the
attestations_mapby:Adding to the existing set of signers if the attestation target is already present.
Creating a new entry otherwise.
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
Envelope<GoshBLS, AttestationData>: Represents a signed attestation envelope containing attestation data and associated BLS signatures. TheEnvelopetype is generic over the signature scheme (GoshBLS) and the attestation data payload (AttestationData).BlockIdentifier: Uniquely identifies a block in the blockchain or ledger system.AttestationTargetType: Enumerates the types of attestation targets that can be signed.SignerIndex: Represents an index identifying a particular signer within the system.
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
Utilizes cryptographic primitives and envelope abstractions from the
blsmodule, specifically theEnvelopeandBLSSignedEnvelopetypes, which handle signature-related data and operations.References attestation-related associated types from the
node::associated_typesmodule, grounding the attestation data and target types in domain-specific definitions.The produced signatures map is a fundamental data structure for components that require efficient verification or querying of which signers have attested to specific blocks and targets, such as consensus algorithms or validation routines.
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]