11// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22// SPDX-License-Identifier: Apache-2.0
33
4- use eventuals:: { Eventual , EventualExt } ;
54use std:: collections:: HashMap ;
65use std:: sync:: Arc ;
76use thegraph_core:: { Address , ChainId } ;
@@ -18,31 +17,19 @@ use crate::prelude::{Allocation, AttestationSigner};
1817
1918/// An always up-to-date list of attestation signers, one for each of the indexer's allocations.
2019pub async fn attestation_signers (
21- indexer_allocations : Eventual < HashMap < Address , Allocation > > ,
20+ mut indexer_allocations_rx : Receiver < HashMap < Address , Allocation > > ,
2221 indexer_mnemonic : String ,
2322 chain_id : ChainId ,
2423 mut dispute_manager_rx : Receiver < Option < Address > > ,
2524) -> Receiver < HashMap < Address , AttestationSigner > > {
2625 let attestation_signers_map: & ' static Mutex < HashMap < Address , AttestationSigner > > =
2726 Box :: leak ( Box :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ) ;
2827
29- // Actively listening to indexer_allocations to update allocations channel
30- // Temporary fix until the indexer_allocations is migrated to tokio watch
31- let ( allocations_tx, mut allocations_rx) =
32- watch:: channel ( indexer_allocations. value_immediate ( ) . unwrap_or_default ( ) ) ;
33- indexer_allocations
34- . pipe ( move |allocatons| {
35- allocations_tx
36- . send ( allocatons)
37- . expect ( "Failed to update allocations channel" ) ;
38- } )
39- . forever ( ) ;
40-
4128 let starter_signers_map = modify_sigers (
4229 Arc :: new ( indexer_mnemonic. clone ( ) ) ,
4330 chain_id,
4431 attestation_signers_map,
45- allocations_rx . clone ( ) ,
32+ indexer_allocations_rx . clone ( ) ,
4633 dispute_manager_rx. clone ( ) ,
4734 )
4835 . await ;
@@ -53,12 +40,12 @@ pub async fn attestation_signers(
5340 tokio:: spawn ( async move {
5441 loop {
5542 let updated_signers = select ! {
56- Ok ( ( ) ) = allocations_rx . changed( ) =>{
43+ Ok ( ( ) ) = indexer_allocations_rx . changed( ) =>{
5744 modify_sigers(
5845 Arc :: new( indexer_mnemonic. clone( ) ) ,
5946 chain_id,
6047 attestation_signers_map,
61- allocations_rx . clone( ) ,
48+ indexer_allocations_rx . clone( ) ,
6249 dispute_manager_rx. clone( ) ,
6350 ) . await
6451 } ,
@@ -67,7 +54,7 @@ pub async fn attestation_signers(
6754 Arc :: new( indexer_mnemonic. clone( ) ) ,
6855 chain_id,
6956 attestation_signers_map,
70- allocations_rx . clone( ) ,
57+ indexer_allocations_rx . clone( ) ,
7158 dispute_manager_rx. clone( )
7259 ) . await
7360 } ,
@@ -129,27 +116,27 @@ mod tests {
129116
130117 #[ tokio:: test]
131118 async fn test_attestation_signers_update_with_allocations ( ) {
132- let ( mut allocations_writer , allocations ) = Eventual :: < HashMap < Address , Allocation > > :: new ( ) ;
119+ let ( allocations_tx , allocations_rx ) = watch :: channel ( HashMap :: new ( ) ) ;
133120 let ( dispute_manager_tx, dispute_manager_rx) = watch:: channel ( None ) ;
134121 dispute_manager_tx
135122 . send ( Some ( * DISPUTE_MANAGER_ADDRESS ) )
136123 . unwrap ( ) ;
137124 let mut signers = attestation_signers (
138- allocations ,
125+ allocations_rx ,
139126 ( * INDEXER_OPERATOR_MNEMONIC ) . to_string ( ) ,
140127 1 ,
141128 dispute_manager_rx,
142129 )
143130 . await ;
144131
145132 // Test that an empty set of allocations leads to an empty set of signers
146- allocations_writer . write ( HashMap :: new ( ) ) ;
133+ allocations_tx . send ( HashMap :: new ( ) ) . unwrap ( ) ;
147134 signers. changed ( ) . await . unwrap ( ) ;
148135 let latest_signers = signers. borrow ( ) . clone ( ) ;
149136 assert_eq ! ( latest_signers, HashMap :: new( ) ) ;
150137
151138 // Test that writing our set of test allocations results in corresponding signers for all of them
152- allocations_writer . write ( ( * INDEXER_ALLOCATIONS ) . clone ( ) ) ;
139+ allocations_tx . send ( ( * INDEXER_ALLOCATIONS ) . clone ( ) ) . unwrap ( ) ;
153140 signers. changed ( ) . await . unwrap ( ) ;
154141 let latest_signers = signers. borrow ( ) . clone ( ) ;
155142 assert_eq ! ( latest_signers. len( ) , INDEXER_ALLOCATIONS . len( ) ) ;
0 commit comments