eigen_client_avsregistry/
reader.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
use crate::error::AvsRegistryError;
use alloy::primitives::{Address, Bytes, FixedBytes, B256, U256};
use alloy::providers::Provider;
use alloy::rpc::types::Filter;
use ark_ff::Zero;
use async_trait::async_trait;
use eigen_common::{get_provider, get_ws_provider, NEW_PUBKEY_REGISTRATION_EVENT};
use eigen_crypto_bls::{
    alloy_registry_g1_point_to_g1_affine, alloy_registry_g2_point_to_g2_affine, BlsG1Point,
    BlsG2Point,
};
use eigen_logging::logger::SharedLogger;
use eigen_types::operator::{
    bitmap_to_quorum_ids, bitmap_to_quorum_ids_from_u192, OperatorPubKeys,
};
use eigen_utils::{
    deploy::stakeregistry::StakeRegistry, middleware::blsapkregistry::BLSApkRegistry,
    middleware::operatorstateretriever::OperatorStateRetriever,
    middleware::registrycoordinator::RegistryCoordinator,
};

use num_bigint::BigInt;
use std::fmt::Debug;
use std::{collections::HashMap, str::FromStr};

/// Avs Registry chainreader
#[derive(Debug, Clone)]
pub struct AvsRegistryChainReader {
    logger: SharedLogger,
    bls_apk_registry_addr: Address,
    registry_coordinator_addr: Address,
    operator_state_retriever: Address,
    stake_registry_addr: Address,
    provider: String,
}

#[async_trait]
/// Common methods for AvsRegistryChainReader
pub trait AvsRegistryReader {
    /// Get operators stake in quorums at a particular block
    ///
    /// # Arguments
    ///
    /// * `block_number` - The block number.
    /// * `quorum_numbers` - The list of quorum numbers.
    ///
    /// # Returns
    ///
    /// A vector of operators, containing each operator's address, id and stake.
    async fn get_operators_stake_in_quorums_at_block(
        &self,
        block_number: u32,
        quorum_numbers: Bytes,
    ) -> Result<Vec<Vec<OperatorStateRetriever::Operator>>, AvsRegistryError>;

    /// Get signature indices
    ///
    /// # Arguments
    ///
    /// * `reference_block_number` - The block number.
    /// * `quorum_numbers` - The list of quorum numbers.
    /// * `non_signer_operator_ids` -  The list of non-signer operator ids.
    ///
    /// # Returns
    ///
    /// A struct containing the indices of the quorum members that signed,
    /// and the ones that didn't
    async fn get_check_signatures_indices(
        &self,
        reference_block_number: u32,
        quorum_numbers: Vec<u8>,
        non_signer_operator_ids: Vec<FixedBytes<32>>,
    ) -> Result<OperatorStateRetriever::CheckSignaturesIndices, AvsRegistryError>;

    /// Get operator from operator id
    ///
    /// # Arguments
    ///
    /// * `operator_id` - The operator id.
    ///
    /// # Returns
    ///
    /// The operator address.
    async fn get_operator_from_id(
        &self,
        operator_id: [u8; 32],
    ) -> Result<Address, AvsRegistryError>;
}

#[async_trait]
impl AvsRegistryReader for AvsRegistryChainReader {
    async fn get_operators_stake_in_quorums_at_block(
        &self,
        block_number: u32,
        quorum_numbers: Bytes,
    ) -> Result<Vec<Vec<OperatorStateRetriever::Operator>>, AvsRegistryError> {
        let provider = get_provider(&self.provider);

        let contract_operator_state_retriever =
            OperatorStateRetriever::new(self.operator_state_retriever, provider);
        let operator_state = contract_operator_state_retriever
            .getOperatorState_0(self.registry_coordinator_addr, quorum_numbers, block_number)
            .call()
            .await
            .map_err(|_| AvsRegistryError::GetOperatorState)?;

        let OperatorStateRetriever::getOperatorState_0Return { _0: quorum } = operator_state;
        Ok(quorum)
    }

    async fn get_check_signatures_indices(
        &self,
        reference_block_number: u32,
        quorum_numbers: Vec<u8>,
        non_signer_operator_ids: Vec<FixedBytes<32>>,
    ) -> Result<OperatorStateRetriever::CheckSignaturesIndices, AvsRegistryError> {
        let provider = get_provider(&self.provider);

        let contract_operator_state_retriever =
            OperatorStateRetriever::new(self.operator_state_retriever, provider);

        let check_signature_indices = contract_operator_state_retriever
            .getCheckSignaturesIndices(
                self.registry_coordinator_addr,
                reference_block_number,
                quorum_numbers.into(),
                non_signer_operator_ids,
            )
            .call()
            .await?;
        let OperatorStateRetriever::getCheckSignaturesIndicesReturn { _0: indices } =
            check_signature_indices;
        Ok(indices)
    }

    async fn get_operator_from_id(
        &self,
        operator_id: [u8; 32],
    ) -> Result<Address, AvsRegistryError> {
        let provider = get_provider(&self.provider);

        let contract_registry_coordinator =
            RegistryCoordinator::new(self.registry_coordinator_addr, &provider);

        let operator_address_return = contract_registry_coordinator
            .getOperatorFromId(operator_id.into())
            .call()
            .await?;
        let RegistryCoordinator::getOperatorFromIdReturn {
            _0: operator_address,
        } = operator_address_return;

        Ok(operator_address)
    }
}

impl AvsRegistryChainReader {
    /// Create a new instance of the AvsRegistryChainReader
    ///
    /// # Arguments
    ///
    /// * `logger` - A reference to the logger.
    /// * `registry_coordinator_addr` - The address of the RegistryCoordinator contract.
    /// * `operator_state_retriever_addr` - The address of the OperatorStateRetriever contract.
    /// * `http_provider_url` - The http provider url.
    pub async fn new(
        logger: SharedLogger,
        registry_coordinator_addr: Address,
        operator_state_retriever_addr: Address,
        http_provider_url: String,
    ) -> Result<AvsRegistryChainReader, AvsRegistryError> {
        let provider = get_provider(&http_provider_url);

        let contract_registry_coordinator =
            RegistryCoordinator::new(registry_coordinator_addr, &provider);
        let bls_apk_registry_return = contract_registry_coordinator
            .blsApkRegistry()
            .call()
            .await
            .map_err(|_| AvsRegistryError::GetBlsApkRegistry)?;

        let RegistryCoordinator::blsApkRegistryReturn {
            _0: bls_apk_registry_addr,
        } = bls_apk_registry_return;

        let stake_registry_return = contract_registry_coordinator
            .stakeRegistry()
            .call()
            .await
            .map_err(|_| AvsRegistryError::GetStakeRegistry)?;

        let RegistryCoordinator::stakeRegistryReturn {
            _0: stake_registry_addr,
        } = stake_registry_return;

        Ok(AvsRegistryChainReader {
            logger,
            bls_apk_registry_addr,
            registry_coordinator_addr,
            operator_state_retriever: operator_state_retriever_addr,
            stake_registry_addr,
            provider: http_provider_url.clone(),
        })
    }

    /// Get quorum count
    ///
    /// # Returns
    ///
    /// The total quorum count read from the RegistryCoordinator.
    pub async fn get_quorum_count(&self) -> Result<u8, AvsRegistryError> {
        let provider = get_provider(&self.provider);

        let contract_registry_coordinator =
            RegistryCoordinator::new(self.registry_coordinator_addr, provider);

        let quorum_count = contract_registry_coordinator
            .quorumCount()
            .call()
            .await
            .map_err(|_| AvsRegistryError::GetQuorumCount)?;

        let RegistryCoordinator::quorumCountReturn { _0: quorum } = quorum_count;
        Ok(quorum)
    }

    /// Get operators stake in quorums at block operator id
    ///
    /// # Arguments
    ///
    /// * `block_number` - The block number.
    /// * `operator_id` - The operator id.
    ///
    /// # Returns
    ///
    /// - a bitmap of the quorums the operator was registered for at `block_number`.
    /// - for each of the quorums mentioned above, a vector of the operators registered for
    ///   that quorum at `block_number`, containing each operator's `operatorId` and `stake`.
    pub async fn get_operators_stake_in_quorums_at_block_operator_id(
        &self,
        block_number: u32,
        operator_id: B256,
    ) -> Result<(U256, Vec<Vec<OperatorStateRetriever::Operator>>), AvsRegistryError> {
        let provider = get_provider(&self.provider);

        let contract_operator_state_retriever =
            OperatorStateRetriever::new(self.operator_state_retriever, provider);

        let operator_state_with_registry_coordinator_and_operator_id =
            contract_operator_state_retriever
                .getOperatorState_1(self.registry_coordinator_addr, operator_id, block_number)
                .call()
                .await
                .map_err(|_| {
                    AvsRegistryError::GetOperatorStateWithRegistryCoordinatorAndOperatorId
                })?;

        let OperatorStateRetriever::getOperatorState_1Return {
            _0: stake,
            _1: operator_state,
        } = operator_state_with_registry_coordinator_and_operator_id;
        Ok((stake, operator_state))
    }

    /// Get operators stake in quorums at current block
    ///
    /// # Arguments
    ///
    /// * `quorum_numbers` - The list of quorum numbers.
    ///
    /// # Returns
    ///
    /// For each quorum in `quorum_numbers`, a vector of the operators registered for
    /// that quorum at the current block, containing each operator's `operatorId` and `stake`.
    pub async fn get_operators_stake_in_quorums_at_current_block(
        &self,
        quorum_numbers: Bytes,
    ) -> Result<Vec<Vec<OperatorStateRetriever::Operator>>, AvsRegistryError> {
        let provider = get_provider(&self.provider);

        let current_block_number = provider
            .get_block_number()
            .await
            .map_err(|_| AvsRegistryError::GetBlockNumber)?;

        if current_block_number > u32::MAX.into() {
            return Err(AvsRegistryError::BlockNumberOverflow);
        }

        self.get_operators_stake_in_quorums_at_block(current_block_number as u32, quorum_numbers)
            .await
            .map_err(|_| AvsRegistryError::GetOperatorStakeInQuorumAtBlockNumber)
    }

    /// Get operators stake in quorums of operator at block
    ///
    /// # Arguments
    ///
    /// * `operator_id` - The operator id.
    /// * `block_number` - The block number.
    ///
    /// # Returns
    ///
    /// - a vector of the quorum numbers the operator was registered for at `block_number`.
    /// - for each of the quorums mentioned above, a vector of the operators registered for
    ///   that quorum at `block_number`, containing each operator's `operatorId` and `stake`.
    pub async fn get_operators_stake_in_quorums_of_operator_at_block(
        &self,
        operator_id: B256,
        block_number: u32,
    ) -> Result<(Vec<u8>, Vec<Vec<OperatorStateRetriever::Operator>>), AvsRegistryError> {
        let (quorum_bitmaps, operator_stakes) = self
            .get_operators_stake_in_quorums_at_block_operator_id(block_number, operator_id)
            .await
            .map_err(|_| AvsRegistryError::GetOperatorStakeInQuorumAtBlockOperatorId)?;

        let quorums = bitmap_to_quorum_ids(quorum_bitmaps);
        let s = (quorums, operator_stakes);
        Ok(s)
    }

    /// Get operators stake in quorums of operator at current block
    ///
    /// # Arguments
    ///
    /// * `operator_id` - The operator id.
    ///
    /// # Returns
    ///
    /// - a vector of the quorum numbers the operator was registered for at the current block.
    /// - for each of the quorums mentioned above, a vector of the operators registered for
    ///   that quorum at the current block, containing each operator's `operatorId` and `stake`.
    pub async fn get_operators_stake_in_quorums_of_operator_at_current_block(
        &self,
        operator_id: B256,
    ) -> Result<(Vec<u8>, Vec<Vec<OperatorStateRetriever::Operator>>), AvsRegistryError> {
        let provider = get_provider(&self.provider);

        let current_block_number = provider.get_block_number().await.map_err(|e| {
            AvsRegistryError::AlloyContractError(alloy::contract::Error::TransportError(e))
        })?;

        if current_block_number > u32::MAX.into() {
            return Err(AvsRegistryError::BlockNumberOverflow);
        }

        self.get_operators_stake_in_quorums_of_operator_at_block(
            operator_id,
            current_block_number as u32,
        )
        .await
    }

    /// Get operator's stake in quorums at current block
    ///
    /// # Arguments
    ///
    /// * `operator_id` - The operator id.
    ///
    /// # Returns
    ///
    /// A hashmap containing the quorum numbers that the operator is registered for,
    /// and the amount staked on each quorum.
    pub async fn get_operator_stake_in_quorums_of_operator_at_current_block(
        &self,
        operator_id: B256,
    ) -> Result<HashMap<u8, BigInt>, AvsRegistryError> {
        let provider = get_provider(&self.provider);

        let registry_coordinator =
            RegistryCoordinator::new(self.registry_coordinator_addr, &provider);

        let quorum_bitmap = registry_coordinator
            .getCurrentQuorumBitmap(operator_id)
            .call()
            .await?;

        let RegistryCoordinator::getCurrentQuorumBitmapReturn { _0: quo } = quorum_bitmap;

        let quorums = bitmap_to_quorum_ids_from_u192(quo);

        let mut quorum_stakes: HashMap<u8, BigInt> = HashMap::new();

        let stake_registry = StakeRegistry::new(self.stake_registry_addr, &provider);
        for quorum in quorums.iter() {
            let stakes_result = stake_registry
                .getCurrentStake(operator_id, *quorum)
                .call()
                .await?;

            let StakeRegistry::getCurrentStakeReturn { _0: c_stake } = stakes_result;
            quorum_stakes.insert(
                *quorum,
                BigInt::from_str(&U256::from(c_stake).to_string())
                    .map_err(|_| AvsRegistryError::ParseBigIntError)?,
            );
        }
        Ok(quorum_stakes)
    }

    /// Query registration detail
    ///
    /// # Arguments
    ///
    /// * `operator_address` - The operator address.
    ///
    /// # Returns
    ///
    /// A vector of booleans, where each boolean represents if the operator
    /// is registered for a quorum.
    ///
    /// # Errors
    ///
    /// Returns an error if the operator id cannot be fetched or if the quorum bitmap
    pub async fn query_registration_detail(
        &self,
        operator_address: Address,
    ) -> Result<[bool; 64], AvsRegistryError> {
        let operator_id = self.get_operator_id(operator_address).await?;

        let provider = get_provider(&self.provider);
        let registry_coordinator =
            RegistryCoordinator::new(self.registry_coordinator_addr, &provider);
        let quorum_bitmap = registry_coordinator
            .getCurrentQuorumBitmap(operator_id)
            .call()
            .await?;

        let inner_value = quorum_bitmap._0.into_limbs()[0];
        let mut quorums: [bool; 64] = [false; 64];
        for i in 0..64_u64 {
            if let Some(value) = quorums.get_mut(i as usize) {
                *value = inner_value & (1 << i) != 0;
            }
        }
        Ok(quorums)
    }

    /// Get operator id
    ///
    /// # Arguments
    ///
    /// * `operator_address` - The operator address.
    ///
    /// # Returns
    ///
    /// The operator id.
    pub async fn get_operator_id(
        &self,
        operator_address: Address,
    ) -> Result<FixedBytes<32>, AvsRegistryError> {
        let provider = get_provider(&self.provider);

        let contract_registry_coordinator =
            RegistryCoordinator::new(self.registry_coordinator_addr, provider);

        let operator_id_return = contract_registry_coordinator
            .getOperatorId(operator_address)
            .call()
            .await?;
        let RegistryCoordinator::getOperatorIdReturn { _0: operator_id } = operator_id_return;
        Ok(operator_id)
    }

    /// Check if operator is registered
    ///
    /// # Arguments
    ///
    /// * `operator_address` - The operator address.
    ///
    /// # Returns
    ///
    /// True if the operator is registered, false otherwise.
    pub async fn is_operator_registered(
        &self,
        operator_address: Address,
    ) -> Result<bool, AvsRegistryError> {
        let provider = get_provider(&self.provider);

        let contract_registry_coordinator =
            RegistryCoordinator::new(self.registry_coordinator_addr, provider);

        let operator_status_return = contract_registry_coordinator
            .getOperatorStatus(operator_address)
            .call()
            .await?;

        let RegistryCoordinator::getOperatorStatusReturn {
            _0: operator_status,
        } = operator_status_return;

        Ok(operator_status == 1)
    }

    /// Queries existing operators from for a particular block range.
    ///
    /// # Arguments
    ///
    /// * `start_block` - The block number to start querying from.
    /// * `stop_block` - The block number to stop querying at.
    /// * `ws_url` - The websocket url to use for querying.
    ///
    /// # Returns
    ///
    /// * (`Vec<Address>`, `Vec<OperatorPubKeys>`) - A vector of operator addresses and its
    ///   corresponding operator pub keys.
    pub async fn query_existing_registered_operator_pub_keys(
        &self,
        start_block: u64,
        mut stop_block: u64,
        ws_url: String,
    ) -> Result<(Vec<Address>, Vec<OperatorPubKeys>), AvsRegistryError> {
        let provider = get_ws_provider(&ws_url).await.map_err(|e| {
            AvsRegistryError::AlloyContractError(alloy::contract::Error::TransportError(e))
        })?;

        let query_block_range = 1024;
        let current_block_number = provider.get_block_number().await.map_err(|e| {
            AvsRegistryError::AlloyContractError(alloy::contract::Error::TransportError(e))
        })?;
        if stop_block.is_zero() {
            stop_block = current_block_number;
        }
        let mut i = start_block;
        let mut operator_addresses: Vec<Address> = vec![];
        let mut operator_pub_keys: Vec<OperatorPubKeys> = vec![];

        while i <= stop_block {
            let to_block = std::cmp::min(i + (query_block_range - 1), stop_block);
            let filter = Filter::new()
                .from_block(i)
                .to_block(to_block)
                .event(NEW_PUBKEY_REGISTRATION_EVENT)
                .address(self.bls_apk_registry_addr);

            let logs = provider.get_logs(&filter).await.map_err(|e| {
                AvsRegistryError::AlloyContractError(alloy::contract::Error::TransportError(e))
            })?;

            let len = logs.len();
            self.logger.debug(
                &format!("numTransactionLogs: {len}, fromBlock: {i}, toBlock: {to_block}",),
                "eigen-client-avsregistry.reader.query_existing_registered_operator_pub_keys",
            );

            for pub_key_reg in logs
                .iter()
                .map(|v| v.log_decode::<BLSApkRegistry::NewPubkeyRegistration>())
                .filter_map(Result::ok)
            {
                let data = pub_key_reg.data();
                let operator_addr = data.operator;
                operator_addresses.push(operator_addr);
                let g1_pub_key = data.pubkeyG1.clone();
                let g2_pub_key = data.pubkeyG2.clone();
                let operator_pub_key = OperatorPubKeys {
                    g1_pub_key: BlsG1Point::new(alloy_registry_g1_point_to_g1_affine(g1_pub_key)),
                    g2_pub_key: BlsG2Point::new(alloy_registry_g2_point_to_g2_affine(g2_pub_key)),
                };
                operator_pub_keys.push(operator_pub_key);
            }
            i += query_block_range;
        }

        Ok((operator_addresses, operator_pub_keys))
    }

    /// TODO: Update bindings and then update this function
    /// Query existing operator sockets
    ///
    /// # Arguments
    ///
    /// * `start_block` - Start block number
    /// * `stop_block` - Stop block number. If zero is passed, then the current block number is fetched and used.
    ///
    /// # Returns
    ///
    /// * `HashMap<FixedBytes<32>, String>` - Operator Id to socket mapping containing all the operator
    ///   sockets registered in the given block range
    pub async fn query_existing_registered_operator_sockets(
        &self,
        start_block: u64,
        stop_block: u64,
    ) -> Result<HashMap<FixedBytes<32>, String>, AvsRegistryError> {
        let provider = get_provider(&self.provider);

        let mut operator_id_to_socket = HashMap::new();

        let query_block_range = 10000;

        let stop_block = if stop_block == 0 {
            provider.get_block_number().await.map_err(|e| {
                AvsRegistryError::AlloyContractError(alloy::contract::Error::TransportError(e))
            })?
        } else {
            stop_block
        };

        for from_block in (start_block..=stop_block).step_by(query_block_range as usize) {
            let to_block = (from_block + query_block_range - 1).min(stop_block);

            let filter = Filter::new()
                .from_block(from_block)
                .to_block(to_block)
                .event("OperatorSocketUpdate(bytes32,string)")
                .address(self.registry_coordinator_addr);

            let logs = provider.get_logs(&filter).await.map_err(|e| {
                AvsRegistryError::AlloyContractError(alloy::contract::Error::TransportError(e))
            })?;

            for v_log in logs.iter() {
                let socket_update_filter_option = v_log
                    .log_decode::<RegistryCoordinator::OperatorSocketUpdate>()
                    .ok();
                if let Some(socket_update_filter) = socket_update_filter_option {
                    let data = socket_update_filter.data();
                    let operator_id = data.operatorId;
                    let socket = &data.socket;
                    operator_id_to_socket.insert(operator_id, socket.clone());
                }
            }
            let len = logs.len();
            self.logger.debug(
                &format!("num_transaction_logs : {len} , from_block: {from_block} , to_block: {to_block}"),
                "eigen-client-avsregistry.reader.query_existing_registered_operator_sockets",
            );
        }
        Ok(operator_id_to_socket)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use eigen_logging::get_test_logger;
    use eigen_testing_utils::m2_holesky_constants::{
        HOLESKY_RPC_PROVIDER, OPERATOR_STATE_RETRIEVER, REGISTRY_COORDINATOR,
    };
    use hex::FromHex;
    use std::str::FromStr;

    async fn build_avs_registry_chain_reader() -> AvsRegistryChainReader {
        AvsRegistryChainReader::new(
            get_test_logger(),
            REGISTRY_COORDINATOR,
            OPERATOR_STATE_RETRIEVER,
            HOLESKY_RPC_PROVIDER.to_string(),
        )
        .await
        .unwrap()
    }

    #[tokio::test]
    async fn test_get_quorum_count() {
        let avs_reader = build_avs_registry_chain_reader().await;

        let _ = avs_reader.get_quorum_count().await.unwrap();
    }

    #[tokio::test]
    async fn test_get_operators_stake_in_quorums_at_block() {
        let avs_reader = build_avs_registry_chain_reader().await;

        let quorum_number = Bytes::from_hex("0x00").expect("bytes parse");
        let _ = avs_reader
            .get_operators_stake_in_quorums_at_block(1245063, quorum_number)
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_get_operators_stake_in_quorums_at_block_operator_id() {
        let avs_reader = build_avs_registry_chain_reader().await;

        let operator_id = U256::from_str(
            "35344093966194310405039483339636912150346494903629410125452342281826147822033",
        )
        .unwrap();

        let _ = avs_reader
            .get_operators_stake_in_quorums_at_block_operator_id(1245842, operator_id.into())
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_get_operators_stake_in_quorums_at_current_block() {
        let avs_reader = build_avs_registry_chain_reader().await;
        let quorum_number = Bytes::from_hex("0x00").expect("bytes parse");

        let _ = avs_reader
            .get_operators_stake_in_quorums_at_current_block(quorum_number)
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_get_operators_stake_in_quorums_of_operator_at_current_block() {
        let avs_reader = build_avs_registry_chain_reader().await;
        let operator_id = U256::from_str(
            "35344093966194310405039483339636912150346494903629410125452342281826147822033",
        )
        .unwrap();

        let (quorums, operators) = avs_reader
            .get_operators_stake_in_quorums_of_operator_at_current_block(operator_id.into())
            .await
            .unwrap();
        assert_eq!(quorums.len(), 0);
        assert_eq!(operators.len(), 0);
    }

    #[tokio::test]
    async fn test_get_operator_stake_in_quorums_of_operator_at_current_block() {
        let avs_reader = build_avs_registry_chain_reader().await;
        let operator_id = U256::from_str(
            "35344093966194310405039483339636912150346494903629410125452342281826147822033",
        )
        .unwrap();

        let stakes = avs_reader
            .get_operator_stake_in_quorums_of_operator_at_current_block(operator_id.into())
            .await
            .unwrap();
        assert_eq!(stakes.len(), 0);
    }

    #[tokio::test]
    async fn test_is_operator_registered() {
        let avs_reader = build_avs_registry_chain_reader().await;

        let is_registered = avs_reader
            .is_operator_registered(REGISTRY_COORDINATOR)
            .await
            .unwrap();
        assert!(!is_registered);
    }

    #[tokio::test]
    async fn test_get_operators_stake_in_quorums_of_operator_at_block() {
        let avs_reader = build_avs_registry_chain_reader().await;

        let operator_id = U256::from_str(
            "35344093966194310405039483339636912150346494903629410125452342281826147822033",
        )
        .unwrap();

        let _ = avs_reader
            .get_operators_stake_in_quorums_of_operator_at_block((operator_id).into(), 1246078)
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_query_existing_registered_operator_sockets() {
        let avs_reader = build_avs_registry_chain_reader().await;

        let _ = avs_reader
            .query_existing_registered_operator_sockets(0, 1000)
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_query_registration_detail() {
        let avs_reader = build_avs_registry_chain_reader().await;

        let operator_id = U256::from_str(
            "35344093966194310405039483339636912150346494903629410125452342281826147822033",
        )
        .unwrap();

        let operator_id_b256: B256 = operator_id.into();

        let operator_address = avs_reader
            .get_operator_from_id(operator_id_b256.into())
            .await
            .unwrap();

        let ret_query_registration_detail = avs_reader
            .query_registration_detail(operator_address)
            .await
            .unwrap();

        println!("{:?}", ret_query_registration_detail);

        // all the value are false
        for ret_value in ret_query_registration_detail.iter() {
            assert!(!ret_value);
        }

        // register an operator
        let is_registered = avs_reader
            .is_operator_registered(operator_address)
            .await
            .unwrap();
        assert!(!is_registered);
    }
}