bk_set_watcher.rs

Overview

This file implements an asynchronous background task that continuously monitors and updates a set of "Bk" entities, represented by the ApiBkSet structure. The main functionality involves querying configured backend addresses (bk_addrs) for their current ApiBkSet data, selecting the most up-to-date set based on a sequence number (seq_no), and broadcasting changes in trusted public keys to other system components using Tokio watch channels.

The watcher ensures that the trusted Bk sets and their associated public keys remain synchronized with potentially multiple backend sources over time, updating only when newer data is available.


Detailed Explanation

Function: get_bk_set_url

fn get_bk_set_url(addr: SocketAddr) -> String

Async Function: run

pub async fn run(
    config_rx: tokio::sync::watch::Receiver<ProxyConfig>,
    bk_set_tx: tokio::sync::watch::Sender<HashSet<transport_layer::VerifyingKey>>,
    client: reqwest::Client,
    bk_set_watch_interval_sec: u64,
)

Function: get_winner_bk_set

fn get_winner_bk_set(current: Option<ApiBkSet>, candidate: ApiBkSet) -> Option<ApiBkSet>

Tests


Important Implementation Details and Algorithms


Interaction with Other System Components

For related concurrency patterns and asynchronous communication, see Tokio Asynchronous Rust and for cryptographic key handling, refer to Transport Layer Cryptography.


Data Structures


Mermaid Diagram: Flowchart of Main Functions and Workflow

flowchart TD
Start["Start run()"]
GetConfig["Read ProxyConfig (bk_addrs)"]
SpawnTasks["Spawn async tasks for each bk_addr"]
WaitResults["Await all bk_set responses"]
SelectWinner["Select ApiBkSet with max seq_no"]
CompareSeqNo["Compare winner.seq_no with current.seq_no"]
UpdateBkSet["Update internal ApiBkSet if newer"]
ExtractKeys["Extract owner pubkeys from current & future"]
CompareKeys["Compare new keys with previous keys"]
Broadcast["Broadcast new keys if changed"]
Sleep["Sleep for configured interval"]
LoopBack["Repeat loop"]
Start --> GetConfig --> SpawnTasks --> WaitResults --> SelectWinner
SelectWinner --> CompareSeqNo --> UpdateBkSet --> ExtractKeys --> CompareKeys
CompareKeys -->|Keys changed| Broadcast --> Sleep --> LoopBack
CompareKeys -->|No change| Sleep --> LoopBack

This flowchart illustrates the continuous loop inside the run function that updates the Bk set and trusted keys by querying backend servers, processing responses, and broadcasting changes to other system components.