@@ -3,10 +3,11 @@ package pkg
33import (
44 "context"
55 "encoding/hex"
6- gethtypes "github.com/ethereum/go-ethereum/core/types"
76 "sync"
87 "time"
98
9+ gethtypes "github.com/ethereum/go-ethereum/core/types"
10+
1011 "github.com/prometheus/client_golang/prometheus"
1112 "github.com/yetanotherco/aligned_layer/metrics"
1213
@@ -56,6 +57,12 @@ type Aggregator struct {
5657 // Stores the taskCreatedBlock for each batch bt batch index
5758 batchCreatedBlockByIdx map [uint32 ]uint64
5859
60+ // Stores if an operator already submitted a response for a batch
61+ // This is to avoid double submissions
62+ // struct{} is used as a placeholder because it is the smallest type
63+ // go does not have a set type
64+ operatorRespondedBatch map [uint32 ]map [eigentypes.Bytes32 ]struct {}
65+
5966 // This task index is to communicate with the local BLS
6067 // Service.
6168 // Note: In case of a reboot it can start from 0 again
@@ -133,6 +140,7 @@ func NewAggregator(aggregatorConfig config.AggregatorConfig) (*Aggregator, error
133140 batchesRootByIdx : batchesRootByIdx ,
134141 batchesIdxByRoot : batchesIdxByRoot ,
135142 batchCreatedBlockByIdx : batchCreatedBlockByIdx ,
143+ operatorRespondedBatch : make (map [uint32 ]map [eigentypes.Bytes32 ]struct {}),
136144 nextBatchIndex : nextBatchIndex ,
137145 taskMutex : & sync.Mutex {},
138146 walletMutex : & sync.Mutex {},
@@ -183,6 +191,14 @@ const MaxSentTxRetries = 5
183191func (agg * Aggregator ) handleBlsAggServiceResponse (blsAggServiceResp blsagg.BlsAggregationServiceResponse ) {
184192 if blsAggServiceResp .Err != nil {
185193 agg .logger .Warn ("BlsAggregationServiceResponse contains an error" , "err" , blsAggServiceResp .Err )
194+ agg .logger .Info ("- Locking task mutex: Delete task from operator map" , "taskIndex" , blsAggServiceResp .TaskIndex )
195+ agg .taskMutex .Lock ()
196+
197+ // Remove task from the list of tasks
198+ delete (agg .operatorRespondedBatch , blsAggServiceResp .TaskIndex )
199+
200+ agg .logger .Info ("- Unlocking task mutex: Delete task from operator map" , "taskIndex" , blsAggServiceResp .TaskIndex )
201+ agg .taskMutex .Unlock ()
186202 return
187203 }
188204 nonSignerPubkeys := []servicemanager.BN254G1Point {}
@@ -209,13 +225,16 @@ func (agg *Aggregator) handleBlsAggServiceResponse(blsAggServiceResp blsagg.BlsA
209225 agg .AggregatorConfig .BaseConfig .Logger .Info ("- Locked Resources: Fetching merkle root" )
210226 batchMerkleRoot := agg .batchesRootByIdx [blsAggServiceResp .TaskIndex ]
211227 taskCreatedBlock := agg .batchCreatedBlockByIdx [blsAggServiceResp .TaskIndex ]
228+
229+ // Delete the task from the map
230+ delete (agg .operatorRespondedBatch , blsAggServiceResp .TaskIndex )
231+
212232 agg .AggregatorConfig .BaseConfig .Logger .Info ("- Unlocked Resources: Fetching merkle root" )
213233 agg .taskMutex .Unlock ()
214234
215235 agg .logger .Info ("Threshold reached" , "taskIndex" , blsAggServiceResp .TaskIndex ,
216236 "merkleRoot" , hex .EncodeToString (batchMerkleRoot [:]))
217237
218-
219238 currentBlock , err := agg .AggregatorConfig .BaseConfig .EthRpcClient .BlockNumber (context .Background ())
220239 if err != nil {
221240 agg .logger .Error ("Error getting current block number" , "err" , err )
@@ -270,10 +289,8 @@ func (agg *Aggregator) handleBlsAggServiceResponse(blsAggServiceResp blsagg.BlsA
270289 "merkleRoot" , hex .EncodeToString (batchMerkleRoot [:]))
271290}
272291
273-
274-
275- /// Sends response to contract and waits for transaction receipt
276- /// Returns error if it fails to send tx or receipt is not found
292+ // / Sends response to contract and waits for transaction receipt
293+ // / Returns error if it fails to send tx or receipt is not found
277294func (agg * Aggregator ) sendAggregatedResponse (batchMerkleRoot [32 ]byte , nonSignerStakesAndSignature servicemanager.IBLSSignatureCheckerNonSignerStakesAndSignature ) (* gethtypes.Receipt , error ) {
278295 agg .walletMutex .Lock ()
279296 agg .logger .Infof ("- Locked Wallet Resources: Sending aggregated response for batch %s" , hex .EncodeToString (batchMerkleRoot [:]))
@@ -299,7 +316,6 @@ func (agg *Aggregator) sendAggregatedResponse(batchMerkleRoot [32]byte, nonSigne
299316 return receipt , nil
300317}
301318
302-
303319func (agg * Aggregator ) AddNewTask (batchMerkleRoot [32 ]byte , taskCreatedBlock uint32 ) {
304320 agg .AggregatorConfig .BaseConfig .Logger .Info ("Adding new task" ,
305321 "Batch merkle root" , hex .EncodeToString (batchMerkleRoot [:]))
0 commit comments