@@ -122,12 +122,12 @@ pub trait Cache {
122
122
async fn store_accumulator_messages (
123
123
& self ,
124
124
accumulator_messages : AccumulatorMessages ,
125
- ) -> Result < ( ) > ;
125
+ ) -> Result < bool > ;
126
126
async fn fetch_accumulator_messages ( & self , slot : Slot ) -> Result < Option < AccumulatorMessages > > ;
127
127
async fn store_wormhole_merkle_state (
128
128
& self ,
129
129
wormhole_merkle_state : WormholeMerkleState ,
130
- ) -> Result < ( ) > ;
130
+ ) -> Result < bool > ;
131
131
async fn fetch_wormhole_merkle_state ( & self , slot : Slot ) -> Result < Option < WormholeMerkleState > > ;
132
132
async fn message_state_keys ( & self ) -> Vec < MessageStateKey > ;
133
133
async fn fetch_message_states (
@@ -226,13 +226,22 @@ where
226
226
async fn store_accumulator_messages (
227
227
& self ,
228
228
accumulator_messages : AccumulatorMessages ,
229
- ) -> Result < ( ) > {
229
+ ) -> Result < bool > {
230
230
let mut cache = self . into ( ) . accumulator_messages_cache . write ( ) . await ;
231
- cache. insert ( accumulator_messages. slot , accumulator_messages) ;
231
+ let slot = accumulator_messages. slot ;
232
+
233
+ // Check if we already have messages for this slot while holding the lock
234
+ if cache. contains_key ( & slot) {
235
+ // Messages already exist, return false to indicate no insertion happened
236
+ return Ok ( false ) ;
237
+ }
238
+
239
+ // Messages don't exist, store them
240
+ cache. insert ( slot, accumulator_messages) ;
232
241
while cache. len ( ) > self . into ( ) . cache_size as usize {
233
242
cache. pop_first ( ) ;
234
243
}
235
- Ok ( ( ) )
244
+ Ok ( true )
236
245
}
237
246
238
247
async fn fetch_accumulator_messages ( & self , slot : Slot ) -> Result < Option < AccumulatorMessages > > {
@@ -243,13 +252,22 @@ where
243
252
async fn store_wormhole_merkle_state (
244
253
& self ,
245
254
wormhole_merkle_state : WormholeMerkleState ,
246
- ) -> Result < ( ) > {
255
+ ) -> Result < bool > {
247
256
let mut cache = self . into ( ) . wormhole_merkle_state_cache . write ( ) . await ;
248
- cache. insert ( wormhole_merkle_state. root . slot , wormhole_merkle_state) ;
257
+ let slot = wormhole_merkle_state. root . slot ;
258
+
259
+ // Check if we already have a state for this slot while holding the lock
260
+ if cache. contains_key ( & slot) {
261
+ // State already exists, return false to indicate no insertion happened
262
+ return Ok ( false ) ;
263
+ }
264
+
265
+ // State doesn't exist, store it
266
+ cache. insert ( slot, wormhole_merkle_state) ;
249
267
while cache. len ( ) > self . into ( ) . cache_size as usize {
250
268
cache. pop_first ( ) ;
251
269
}
252
- Ok ( ( ) )
270
+ Ok ( true )
253
271
}
254
272
255
273
async fn fetch_wormhole_merkle_state ( & self , slot : Slot ) -> Result < Option < WormholeMerkleState > > {
0 commit comments