1
1
use {
2
2
self :: {
3
3
proof:: wormhole_merkle:: construct_update_data,
4
- storage:: StorageInstance ,
4
+ storage:: {
5
+ MessageStateFilter ,
6
+ StorageInstance ,
7
+ } ,
5
8
types:: {
6
9
AccumulatorMessages ,
7
10
MessageType ,
17
20
construct_message_states_proofs,
18
21
store_wormhole_merkle_verified_message,
19
22
} ,
23
+ storage:: AccumulatorState ,
20
24
types:: {
21
25
MessageState ,
22
26
ProofSet ,
41
45
collections:: HashSet ,
42
46
sync:: Arc ,
43
47
time:: {
44
- Duration ,
45
48
SystemTime ,
46
49
UNIX_EPOCH ,
47
50
} ,
@@ -62,28 +65,16 @@ pub mod storage;
62
65
pub mod types;
63
66
pub mod wormhole;
64
67
65
- #[ derive( Clone , PartialEq , Debug , Builder ) ]
66
- #[ builder( derive( Debug ) , pattern = "immutable" ) ]
67
- pub struct AccumulatorState {
68
- pub accumulator_messages : AccumulatorMessages ,
69
- pub wormhole_merkle_proof : ( WormholeMerkleRoot , Vec < u8 > ) ,
70
- }
71
-
72
68
pub struct Store {
73
- pub storage : StorageInstance ,
74
- pub pending_accumulations : Cache < Slot , AccumulatorStateBuilder > ,
75
- pub guardian_set : RwLock < Option < Vec < GuardianAddress > > > ,
76
- pub update_tx : Sender < ( ) > ,
69
+ pub storage : StorageInstance ,
70
+ pub guardian_set : RwLock < Option < Vec < GuardianAddress > > > ,
71
+ pub update_tx : Sender < ( ) > ,
77
72
}
78
73
79
74
impl Store {
80
- pub fn new_with_local_cache ( update_tx : Sender < ( ) > , max_size_per_key : usize ) -> Arc < Self > {
75
+ pub fn new_with_local_cache ( update_tx : Sender < ( ) > , cache_size : u64 ) -> Arc < Self > {
81
76
Arc :: new ( Self {
82
- storage : storage:: local_storage:: LocalStorage :: new_instance ( max_size_per_key) ,
83
- pending_accumulations : Cache :: builder ( )
84
- . max_capacity ( 10_000 )
85
- . time_to_live ( Duration :: from_secs ( 60 * 5 ) )
86
- . build ( ) , // FIXME: Make this configurable
77
+ storage : storage:: local_storage:: LocalStorage :: new_instance ( cache_size) ,
87
78
guardian_set : RwLock :: new ( None ) ,
88
79
update_tx,
89
80
} )
@@ -117,44 +108,47 @@ impl Store {
117
108
}
118
109
}
119
110
}
111
+
120
112
Update :: AccumulatorMessages ( accumulator_messages) => {
121
113
let slot = accumulator_messages. slot ;
122
-
123
114
log:: info!( "Storing accumulator messages for slot {:?}." , slot, ) ;
124
-
125
- let pending_acc = self
126
- . pending_accumulations
127
- . entry ( slot)
128
- . or_default ( )
129
- . await
130
- . into_value ( ) ;
131
- self . pending_accumulations
132
- . insert ( slot, pending_acc. accumulator_messages ( accumulator_messages) )
133
- . await ;
134
-
115
+ let mut accumulator_state = self
116
+ . storage
117
+ . fetch_accumulator_state ( slot)
118
+ . await ?
119
+ . unwrap_or ( AccumulatorState {
120
+ slot,
121
+ accumulator_messages : None ,
122
+ wormhole_merkle_proof : None ,
123
+ } ) ;
124
+ accumulator_state. accumulator_messages = Some ( accumulator_messages) ;
125
+ self . storage
126
+ . store_accumulator_state ( accumulator_state)
127
+ . await ?;
135
128
slot
136
129
}
137
130
} ;
138
131
139
- let pending_state = self . pending_accumulations . get ( & slot) ;
140
- let pending_state = match pending_state {
141
- Some ( pending_state) => pending_state,
142
- // Due to some race conditions this might happen when it's processed before
132
+ let state = match self . storage . fetch_accumulator_state ( slot) . await ? {
133
+ Some ( state) => state,
143
134
None => return Ok ( ( ) ) ,
144
135
} ;
145
136
146
- let state = match pending_state. build ( ) {
147
- Ok ( state) => state,
148
- Err ( _) => return Ok ( ( ) ) ,
149
- } ;
137
+ let ( accumulator_messages, wormhole_merkle_proof) =
138
+ match ( state. accumulator_messages , state. wormhole_merkle_proof ) {
139
+ ( Some ( accumulator_messages) , Some ( wormhole_merkle_proof) ) => {
140
+ ( accumulator_messages, wormhole_merkle_proof)
141
+ }
142
+ _ => return Ok ( ( ) ) ,
143
+ } ;
150
144
151
- let wormhole_merkle_message_states_proofs = construct_message_states_proofs ( state. clone ( ) ) ?;
145
+ let wormhole_merkle_message_states_proofs =
146
+ construct_message_states_proofs ( & accumulator_messages, & wormhole_merkle_proof) ?;
152
147
153
148
let current_time: UnixTimestamp =
154
149
SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) ?. as_secs ( ) as _ ;
155
150
156
- let message_states = state
157
- . accumulator_messages
151
+ let message_states = accumulator_messages
158
152
. messages
159
153
. iter ( )
160
154
. enumerate ( )
@@ -170,17 +164,15 @@ impl Store {
170
164
. ok_or ( anyhow ! ( "Missing proof for message" ) ) ?
171
165
. clone ( ) ,
172
166
} ,
173
- state . accumulator_messages . slot ,
167
+ accumulator_messages. slot ,
174
168
current_time,
175
169
) )
176
170
} )
177
171
. collect :: < Result < Vec < _ > > > ( ) ?;
178
172
179
173
log:: info!( "Message states len: {:?}" , message_states. len( ) ) ;
180
174
181
- self . storage . store_message_states ( message_states) ?;
182
-
183
- self . pending_accumulations . invalidate ( & slot) . await ;
175
+ self . storage . store_message_states ( message_states) . await ?;
184
176
185
177
self . update_tx . send ( ( ) ) . await ?;
186
178
@@ -191,16 +183,19 @@ impl Store {
191
183
self . guardian_set . write ( ) . await . replace ( guardian_set) ;
192
184
}
193
185
194
- pub fn get_price_feeds_with_update_data (
186
+ pub async fn get_price_feeds_with_update_data (
195
187
& self ,
196
188
price_ids : Vec < PriceIdentifier > ,
197
189
request_time : RequestTime ,
198
190
) -> Result < PriceFeedsWithUpdateData > {
199
- let messages = self . storage . retrieve_message_states (
200
- price_ids,
201
- request_time,
202
- Some ( & |message_type| * message_type == MessageType :: PriceFeedMessage ) ,
203
- ) ?;
191
+ let messages = self
192
+ . storage
193
+ . fetch_message_states (
194
+ price_ids,
195
+ request_time,
196
+ MessageStateFilter :: Only ( MessageType :: PriceFeedMessage ) ,
197
+ )
198
+ . await ?;
204
199
205
200
let price_feeds = messages
206
201
. iter ( )
@@ -226,7 +221,12 @@ impl Store {
226
221
} )
227
222
}
228
223
229
- pub fn get_price_feed_ids ( & self ) -> HashSet < PriceIdentifier > {
230
- self . storage . keys ( ) . iter ( ) . map ( |key| key. price_id ) . collect ( )
224
+ pub async fn get_price_feed_ids ( & self ) -> HashSet < PriceIdentifier > {
225
+ self . storage
226
+ . message_state_keys ( )
227
+ . await
228
+ . iter ( )
229
+ . map ( |key| key. price_id )
230
+ . collect ( )
231
231
}
232
232
}
0 commit comments