eigen_services_blsaggregation/lib.rs
1//! BLS Agreggation Service crate.
2//! # BLS Aggregation Service
3//!
4//! ## Introduction
5//!
6//! The BLS Aggregation Service provides functionality to aggregate BLS signatures from multiple operators into a single aggregated signature. This is used by the Aggregator to verify the signatures of the operators and send the aggregated response once the quorum is reached or time expires. AVS developers can use it to define and manage tasks, set quorum requirements and integrate aggregated results into their smart contracts.
7//!
8//! ## Key Features
9//!
10//! - **BLS Signature Aggregation**: Combines multiple individual signatures into a single verifiable aggregated signature.
11//! - **Quorum Verification**: Ensures that the required participation threshold is reached for each quorum.
12//! - **Task Management**: Allows initializing tasks and processing signatures for those tasks.
13//! - **Configurable Time Window**: Allows defining waiting periods for signature collection.
14//!
15//! ## Main Components
16//!
17//! - [`TaskMetadata`]: Defines task parameters, including quorums and thresholds.
18//! - [`TaskSignature`]: Contains an individual BLS signature for a specific task.
19//! - [`ServiceHandle`]: Interface for sending tasks and signatures to the service.
20//! - [`AggregateReceiver`]: Channel for receiving aggregated responses.
21//! - [`BlsAggregatorService`]: The main service that manages tasks and aggregates signatures.
22//!
23//! ## Usage
24//!
25//! When you initialize the BLS Aggregation Service, it returns a tuple of [`ServiceHandle`] and [`AggregateReceiver`]. The `ServiceHandle` is used to interact with the service. The available messages to send to the service are:
26//!
27//! - [`initialize_task(metadata: TaskMetadata)`]: Initializes a new task. If you want to set a time to expiry for the task, you can use the [`with_window_duration()`] method in a builder pattern.
28//! - [`process_signature(task_signature: TaskSignature)`]: Processes a signature for a task
29//!
30//! The `AggregateReceiver` is used to receive aggregated responses from the service. To get the aggregated response, you can use the [`receive_aggregated_response()`] method.
31//!
32//! Once a task is initialized, the service will start processing the task in a loop in the background. The service will wait for the quorum to be reached or the time to expire. Once the quorum is reached or the time expires, the service will aggregate the signatures and send the aggregated response to the `AggregateReceiver`.
33//!
34//! ### Initialize the Service
35//!
36//! ```rust,no_run
37//! # use eigen_services_blsaggregation::bls_agg::{
38//! # AggregateReceiver, BlsAggregatorService, TaskMetadata, TaskSignature
39//! # };
40//! # use eigen_client_avsregistry::{
41//! # reader::AvsRegistryChainReader, writer::AvsRegistryChainWriter,
42//! # };
43//! # use eigen_services_avsregistry::chaincaller::AvsRegistryServiceChainCaller;
44//! # use eigen_services_avsregistry::AvsRegistryService;
45//! # use eigen_testing_utils::{
46//! # anvil_constants::{
47//! # get_operator_state_retriever_address, get_registry_coordinator_address,
48//! # },
49//! # };
50//! # use eigen_services_operatorsinfo::operatorsinfo_inmemory::OperatorInfoServiceInMemory;
51//! # #[tokio::main]
52//! # async fn main() {
53//! # let http_endpoint = "http://localhost:8545";
54//! # let ws_endpoint = "ws://localhost:8546";
55//! # let registry_coordinator_address =
56//! # get_registry_coordinator_address(http_endpoint.to_string()).await;
57//! # let operator_state_retriever_address =
58//! # get_operator_state_retriever_address(http_endpoint.to_string()).await;
59//! #
60//! # // Create avs clients to interact with contracts deployed on anvil
61//! # let avs_registry_reader = AvsRegistryChainReader::new(
62//! # registry_coordinator_address,
63//! # operator_state_retriever_address,
64//! # http_endpoint.to_string(),
65//! # )
66//! # .await
67//! # .unwrap();
68//! #
69//! # // Create operators info service
70//! # let operators_info = OperatorInfoServiceInMemory::new(
71//! # avs_registry_reader.clone(),
72//! # ws_endpoint.to_string(),
73//! # )
74//! # .await
75//! # .unwrap()
76//! # .0;
77//! #
78//! # // Create avs registry service chain caller
79//! # let avs_registry_service =
80//! # AvsRegistryServiceChainCaller::new(avs_registry_reader.clone(), operators_info);
81//! let (service_handle, mut aggregate_receiver) =
82//! BlsAggregatorService::new(avs_registry_service).start();
83//! # }
84//! ```
85//!
86//! ### Initialize a Task
87//!
88//! ```rust,no_run
89//! # use eigen_services_blsaggregation::bls_agg::{
90//! # AggregateReceiver, BlsAggregatorService, TaskMetadata, TaskSignature
91//! # };
92//! # use eigen_client_avsregistry::{
93//! # reader::AvsRegistryChainReader, writer::AvsRegistryChainWriter,
94//! # };
95//! # use eigen_services_avsregistry::chaincaller::AvsRegistryServiceChainCaller;
96//! # use eigen_services_avsregistry::AvsRegistryService;
97//! # use eigen_testing_utils::{
98//! # anvil_constants::{
99//! # get_operator_state_retriever_address, get_registry_coordinator_address,
100//! # },
101//! # };
102//! # use eigen_services_operatorsinfo::operatorsinfo_inmemory::OperatorInfoServiceInMemory;
103//! # use std::time::Duration;
104//! # use eigen_testing_utils::anvil_constants::{ANVIL_HTTP_URL, ANVIL_WS_URL};
105//! # #[tokio::main]
106//! # async fn main() {
107//! # let http_endpoint = ANVIL_HTTP_URL;
108//! # let ws_endpoint = ANVIL_WS_URL;
109//! # let registry_coordinator_address =
110//! # get_registry_coordinator_address(http_endpoint.to_string()).await;
111//! # let operator_state_retriever_address =
112//! # get_operator_state_retriever_address(http_endpoint.to_string()).await;
113//! #
114//! # // Create avs clients to interact with contracts deployed on anvil
115//! # let avs_registry_reader = AvsRegistryChainReader::new(
116//! # registry_coordinator_address,
117//! # operator_state_retriever_address,
118//! # http_endpoint.to_string(),
119//! # )
120//! # .await
121//! # .unwrap();
122//! #
123//! # // Create operators info service
124//! # let operators_info = OperatorInfoServiceInMemory::new(
125//! # avs_registry_reader.clone(),
126//! # ws_endpoint.to_string(),
127//! # )
128//! # .await
129//! # .unwrap()
130//! # .0;
131//! #
132//! # // Create avs registry service chain caller
133//! # let avs_registry_service =
134//! # AvsRegistryServiceChainCaller::new(avs_registry_reader.clone(), operators_info);
135//! # let (service_handle, mut aggregate_receiver) =
136//! # BlsAggregatorService::new(avs_registry_service).start();
137//! #
138//! # let task_index = 0;
139//! # let block_number = 1;
140//! # let quorum_numbers = vec![0];
141//! # let quorum_threshold_percentages = vec![100];
142//! # let time_to_expiry = Duration::from_secs(60);
143//! #
144//! let metadata = TaskMetadata::new(
145//! task_index,
146//! block_number,
147//! quorum_numbers,
148//! quorum_threshold_percentages,
149//! time_to_expiry,
150//! );
151//! service_handle.initialize_task(metadata).await.unwrap();
152//! # }
153//! ```
154//!
155//! ### Process a Signature
156//!
157//! ```rust,no_run
158//! # use eigen_services_blsaggregation::bls_agg::{
159//! # BlsAggregatorService, TaskSignature
160//! # };
161//! # use eigen_client_avsregistry::{
162//! # reader::AvsRegistryChainReader, writer::AvsRegistryChainWriter,
163//! # };
164//! # use eigen_services_avsregistry::chaincaller::AvsRegistryServiceChainCaller;
165//! # use eigen_services_avsregistry::AvsRegistryService;
166//! # use eigen_testing_utils::{
167//! # anvil_constants::{
168//! # get_operator_state_retriever_address, get_registry_coordinator_address,
169//! # },
170//! # };
171//! # use eigen_services_operatorsinfo::operatorsinfo_inmemory::OperatorInfoServiceInMemory;
172//! # use sha2::{Digest, Sha256};
173//! # use alloy::primitives::{B256, FixedBytes};
174//! # use eigen_crypto_bls::BlsKeyPair;
175//! # use eigen_testing_utils::anvil_constants::{ANVIL_HTTP_URL, ANVIL_WS_URL, OPERATOR_BLS_KEY};
176//! # #[tokio::main]
177//! # async fn main() {
178//! # let http_endpoint = ANVIL_HTTP_URL;
179//! # let ws_endpoint = ANVIL_WS_URL;
180//! # let registry_coordinator_address =
181//! # get_registry_coordinator_address(http_endpoint.to_string()).await;
182//! # let operator_state_retriever_address =
183//! # get_operator_state_retriever_address(http_endpoint.to_string()).await;
184//! #
185//! # // Create avs clients to interact with contracts deployed on anvil
186//! # let avs_registry_reader = AvsRegistryChainReader::new(
187//! # registry_coordinator_address,
188//! # operator_state_retriever_address,
189//! # http_endpoint.to_string(),
190//! # )
191//! # .await
192//! # .unwrap();
193//! #
194//! # // Create operators info service
195//! # let operators_info = OperatorInfoServiceInMemory::new(
196//! # avs_registry_reader.clone(),
197//! # ws_endpoint.to_string(),
198//! # )
199//! # .await
200//! # .unwrap()
201//! # .0;
202//! #
203//! # // Create avs registry service chain caller
204//! # let avs_registry_service =
205//! # AvsRegistryServiceChainCaller::new(avs_registry_reader.clone(), operators_info);
206//! # let (service_handle, mut aggregate_receiver) =
207//! # BlsAggregatorService::new(avs_registry_service).start();
208//! #
209//! # let task_index = 0;
210//! # let task_response: u64 = 123;
211//! # let mut hasher = Sha256::new();
212//! # hasher.update(task_response.to_be_bytes());
213//! # let task_response_digest = B256::from_slice(hasher.finalize().as_ref());
214//! # let bls_key_pair = BlsKeyPair::new(OPERATOR_BLS_KEY.to_string()).unwrap();
215//! # let bls_signature = bls_key_pair.sign_message(task_response_digest.as_ref());
216//! # let operator_id = FixedBytes::from_slice(&[1]);
217//! #
218//! let task_signature = TaskSignature::new(
219//! task_index,
220//! task_response_digest,
221//! bls_signature,
222//! operator_id,
223//! );
224//! service_handle.process_signature(task_signature).await.unwrap();
225//! # }
226//! ```
227//!
228//! ### Receive an Aggregated Response
229//!
230//! ```rust,no_run
231//! # use eigen_services_blsaggregation::bls_agg::BlsAggregatorService;
232//! # use eigen_client_avsregistry::{
233//! # reader::AvsRegistryChainReader, writer::AvsRegistryChainWriter,
234//! # };
235//! # use eigen_services_avsregistry::chaincaller::AvsRegistryServiceChainCaller;
236//! # use eigen_services_avsregistry::AvsRegistryService;
237//! # use eigen_testing_utils::{
238//! # anvil_constants::{
239//! # get_operator_state_retriever_address, get_registry_coordinator_address,
240//! # },
241//! # };
242//! # use eigen_services_operatorsinfo::operatorsinfo_inmemory::OperatorInfoServiceInMemory;
243//! # use eigen_testing_utils::anvil_constants::{ANVIL_HTTP_URL, ANVIL_WS_URL};
244//! # #[tokio::main]
245//! # async fn main() {
246//! # let http_endpoint = ANVIL_HTTP_URL;
247//! # let ws_endpoint = ANVIL_WS_URL;
248//! # let registry_coordinator_address =
249//! # get_registry_coordinator_address(http_endpoint.to_string()).await;
250//! # let operator_state_retriever_address =
251//! # get_operator_state_retriever_address(http_endpoint.to_string()).await;
252//! #
253//! # // Create avs clients to interact with contracts deployed on anvil
254//! # let avs_registry_reader = AvsRegistryChainReader::new(
255//! # registry_coordinator_address,
256//! # operator_state_retriever_address,
257//! # http_endpoint.to_string(),
258//! # )
259//! # .await
260//! # .unwrap();
261//! #
262//! # // Create operators info service
263//! # let operators_info = OperatorInfoServiceInMemory::new(
264//! # avs_registry_reader.clone(),
265//! # ws_endpoint.to_string(),
266//! # )
267//! # .await
268//! # .unwrap()
269//! # .0;
270//! #
271//! # // Create avs registry service chain caller
272//! # let avs_registry_service =
273//! # AvsRegistryServiceChainCaller::new(avs_registry_reader.clone(), operators_info);
274//! # let (service_handle, mut aggregate_receiver) =
275//! # BlsAggregatorService::new(avs_registry_service).start();
276//! #
277//! match aggregate_receiver.receive_aggregated_response().await {
278//! Ok(aggregated_response) => {
279//! // Handle the aggregated response
280//! }
281//! Err(e) => {
282//! // Handle the error
283//! }
284//! }
285//! # }
286//! ```
287//!
288//! ## Example Diagram
289//!
290//! The following diagram shows the sequence of events when a user creates the BLS Aggregator Service, starts the service, and then initializes a task. The service processes two signatures from the operators and then aggregates them into a single aggregated signature.
291//!
292//! <pre>
293#![cfg_attr(doc, doc = simple_mermaid::mermaid!("../diagram/sequence-bls.mmd"))]
294//! </pre>
295//!
296//! ## Testing
297//!
298//! To run the [integration tests](https://github.com/Layr-Labs/eigensdk-rs/blob/dev/crates/services/bls_aggregation/src/bls_agg_test.rs), you can use the following command:
299//!
300//! ```sh
301//! cargo test --package eigen-services-blsaggregation --lib -- bls_agg_test::integration_test --show-output
302//! ```
303//! [`new()`]: bls_agg::BlsAggregatorService::new()
304//! [`start()`]: bls_agg::BlsAggregatorService::start()
305//! [`initialize_task(metadata: TaskMetadata)`]: bls_agg::ServiceHandle::initialize_task()
306//! [`process_signature(task_signature: TaskSignature)`]: bls_agg::ServiceHandle::process_signature()
307//! [`receive_aggregated_response()`]: bls_agg::AggregateReceiver::receive_aggregated_response()
308//! [`with_window_duration()`]: bls_agg::TaskMetadata::with_window_duration()
309//! [`ServiceHandle`]: bls_agg::ServiceHandle
310//! [`AggregateReceiver`]: bls_agg::AggregateReceiver
311//! [`TaskMetadata`]: bls_agg::TaskMetadata
312//! [`TaskSignature`]: bls_agg::TaskSignature
313//! [`BlsAggregatorService`]: bls_agg::BlsAggregatorService
314#![doc(
315 html_logo_url = "https://github.com/Layr-Labs/eigensdk-rs/assets/91280922/bd13caec-3c00-4afc-839a-b83d2890beb5",
316 issue_tracker_base_url = "https://github.com/Layr-Labs/eigensdk-rs/issues/"
317)]
318
319pub mod bls_agg;
320mod bls_agg_test;
321pub mod bls_aggregation_service_error;
322pub mod bls_aggregation_service_response;