service.rs

Overview

This file implements the AttestationTargetsService, a core service responsible for evaluating whether the required attestations from ancestor blocks are met for the next block in a blockchain thread. It mainly focuses on verifying the sufficiency of attestations against finalization checkpoints derived from ancestor blocks' states. This service interacts with the BlockStateRepository to retrieve state data and uses the concept of attestation target checkpoints to decide if the next block's ancestor attestations fulfill the protocol's criteria.


Structs and Enums

AttestationTargetsService

A service struct constructed with a BlockStateRepository instance used to access block states for evaluation.

AttestationsFailure

An enum listing possible failure reasons related to attestation processing.

Currently, this enum is defined but not extensively used in the implementation.


Functions and Methods

AttestationTargetsService::evaluate_if_next_block_ancestors_required_attestations_will_be_met

pub fn evaluate_if_next_block_ancestors_required_attestations_will_be_met(
    &self,
    thread_identifier: ThreadIdentifier,
    parent_block_identifier: BlockIdentifier,
    next_block_attestations: HashMap<(BlockIdentifier, AttestationTargetType), HashSet<SignerIndex>>,
) -> anyhow::Result<bool, UnfinalizedAncestorBlocksSelectError>

Purpose

Determines if the attestations provided for the next block meet the required attestation thresholds of ancestor blocks in the same thread, according to finalization checkpoints.

Parameters

Returns

Description

  1. Retrieves the parent block state from the repository.

  2. Extracts the parent block's thread identifier and ancestor blocks' finalization checkpoints.

  3. If the thread identifiers do not match, it assumes the attestations are sufficient and returns true.

  4. Otherwise, it creates mutable copies of the primary and fallback finalization checkpoints and applies the inherit_checkpoint transformation to each checkpoint.

  5. Constructs a new AncestorBlocksFinalizationCheckpointsConstructor using the inherited checkpoints.

  6. Calls .update(...) with the counts of attestations per (BlockIdentifier, AttestationTargetType).

  7. Checks the failed field in the update result; if empty, all required attestations are met.

  8. Returns the boolean result.

Usage Example

let service = AttestationTargetsService::builder()
    .block_state_repository(block_state_repo)
    .build();

let result = service.evaluate_if_next_block_ancestors_required_attestations_will_be_met(
    thread_id,
    parent_block_id,
    next_block_attestations_map,
);

match result {
    Ok(true) => println!("All ancestor attestations requirements met."),
    Ok(false) => println!("Some ancestor attestations requirements are missing."),
    Err(e) => eprintln!("Evaluation error: {:?}", e),
}

Implementation Details and Algorithms


Interaction with Other Components


Visual Diagram

classDiagram
class AttestationTargetsService {
-block_state_repository: BlockStateRepository
+evaluate_if_next_block_ancestors_required_attestations_will_be_met()
}
class BlockStateRepository
class AncestorBlocksFinalizationCheckpointsConstructor {
+builder()
+update()
}
class AncestorBlocksFinalizationCheckpoints {
+primary: HashMap
+fallback: HashMap
}
class AttestationTargetType
class UnfinalizedAncestorBlocksSelectError
AttestationTargetsService --> BlockStateRepository
AttestationTargetsService --> AncestorBlocksFinalizationCheckpointsConstructor
AncestorBlocksFinalizationCheckpointsConstructor --> AncestorBlocksFinalizationCheckpoints
AttestationTargetsService ..> AttestationTargetType
AttestationTargetsService ..> UnfinalizedAncestorBlocksSelectError

Test Coverage

The tests module includes two regression test cases that simulate real-world failure scenarios observed in load tests. These tests:

Tests utilize utilities such as tempfile for temporary repository paths and tracing_test to capture logs.


For more information on related data structures and error handling, see BlockStateRepository, AncestorBlocksFinalizationCheckpoints, and AttestationTargetType.