1
1
use mithril_common:: StdError ;
2
2
3
3
use mithril_common:: {
4
- entities:: { Beacon , Epoch , SignedEntityType } ,
4
+ entities:: { Epoch , SignedEntityType } ,
5
5
sqlite:: { HydrationError , Projection , SqLiteEntity , WhereCondition } ,
6
6
sqlite:: { Provider , SourceAlias } ,
7
7
} ;
@@ -30,10 +30,6 @@ pub struct OpenMessage {
30
30
/// Epoch
31
31
epoch : Epoch ,
32
32
33
- /// Beacon, this is the discriminant of this message type in the current
34
- /// Epoch
35
- beacon : Beacon ,
36
-
37
33
/// Type of message
38
34
signed_entity_type : SignedEntityType ,
39
35
@@ -60,21 +56,13 @@ impl SqLiteEntity for OpenMessage {
60
56
let epoch_val = u64:: try_from ( epoch_setting_id)
61
57
. map_err ( |e| panic ! ( "Integer field open_message.epoch_setting_id (value={epoch_setting_id}) is incompatible with u64 Epoch representation. Error = {e}" ) ) ?;
62
58
59
+ let beacon_str = row. get :: < String , _ > ( 2 ) ;
63
60
let signed_entity_type_id = usize:: try_from ( row. get :: < i64 , _ > ( 3 ) ) . map_err ( |e| {
64
61
panic ! (
65
62
"Integer field open_message.signed_entity_type_id cannot be turned into usize: {e}"
66
63
)
67
64
} ) ?;
68
- let signed_entity_type = SignedEntityType :: from_repr ( signed_entity_type_id)
69
- . ok_or_else ( || HydrationError :: InvalidData ( format ! (
70
- "Field open_message.signed_type_id can be either 0, 1 or 2, ({signed_entity_type_id} given)."
71
- ) ) ) ?;
72
- let beacon_str = row. get :: < String , _ > ( 2 ) ;
73
- let beacon: Beacon = serde_json:: from_str ( & beacon_str) . map_err ( |e| {
74
- HydrationError :: InvalidData ( format ! (
75
- "Invalid Beacon JSON in open_message.beacon: '{beacon_str}'. Error: {e}"
76
- ) )
77
- } ) ?;
65
+ let signed_entity_type = SignedEntityType :: hydrate ( signed_entity_type_id, & beacon_str) ?;
78
66
let datetime = & row. get :: < String , _ > ( 5 ) ;
79
67
let created_at =
80
68
NaiveDateTime :: parse_from_str ( datetime, "%Y-%m-%d %H:%M:%S" ) . map_err ( |e| {
@@ -86,7 +74,6 @@ impl SqLiteEntity for OpenMessage {
86
74
let open_message = Self {
87
75
open_message_id,
88
76
epoch : Epoch ( epoch_val) ,
89
- beacon,
90
77
signed_entity_type,
91
78
message,
92
79
created_at,
@@ -141,7 +128,7 @@ impl<'client> OpenMessageProvider<'client> {
141
128
) -> WhereCondition {
142
129
WhereCondition :: new (
143
130
"signed_entity_type_id = ?*" ,
144
- vec ! [ Value :: Integer ( * signed_entity_type as i64 ) ] ,
131
+ vec ! [ Value :: Integer ( signed_entity_type. index ( ) as i64 ) ] ,
145
132
)
146
133
}
147
134
@@ -181,16 +168,16 @@ impl<'client> InsertOpenMessageProvider<'client> {
181
168
fn get_insert_condition (
182
169
& self ,
183
170
epoch : Epoch ,
184
- beacon : & Beacon ,
185
171
signed_entity_type : & SignedEntityType ,
186
172
message : & str ,
187
173
) -> StdResult < WhereCondition > {
188
174
let expression = "(open_message_id, epoch_setting_id, beacon, signed_entity_type_id, message) values (?*, ?*, ?*, ?*, ?*)" ;
175
+ let beacon_str = signed_entity_type. get_json_beacon ( ) ?;
189
176
let parameters = vec ! [
190
177
Value :: String ( Uuid :: new_v4( ) . to_string( ) ) ,
191
178
Value :: Integer ( epoch. 0 as i64 ) ,
192
- Value :: String ( serde_json :: to_string ( beacon ) ? ) ,
193
- Value :: Integer ( * signed_entity_type as i64 ) ,
179
+ Value :: String ( beacon_str ) ,
180
+ Value :: Integer ( signed_entity_type. index ( ) as i64 ) ,
194
181
Value :: String ( message. to_string( ) ) ,
195
182
] ;
196
183
@@ -279,13 +266,12 @@ impl OpenMessageRepository {
279
266
pub async fn create_open_message (
280
267
& self ,
281
268
epoch : Epoch ,
282
- beacon : & Beacon ,
283
269
signed_entity_type : & SignedEntityType ,
284
270
message : & str ,
285
271
) -> StdResult < OpenMessage > {
286
272
let lock = self . connection . lock ( ) . await ;
287
273
let provider = InsertOpenMessageProvider :: new ( & lock) ;
288
- let filters = provider. get_insert_condition ( epoch, beacon , signed_entity_type, message) ?;
274
+ let filters = provider. get_insert_condition ( epoch, signed_entity_type, message) ?;
289
275
let mut cursor = provider. find ( filters) ?;
290
276
291
277
cursor
@@ -307,7 +293,7 @@ impl OpenMessageRepository {
307
293
308
294
#[ cfg( test) ]
309
295
mod tests {
310
- use mithril_common:: sqlite:: SourceAlias ;
296
+ use mithril_common:: { entities :: Beacon , sqlite:: SourceAlias } ;
311
297
312
298
use crate :: { dependency_injection:: DependenciesBuilder , Configuration } ;
313
299
@@ -338,8 +324,13 @@ mod tests {
338
324
fn provider_message_type_condition ( ) {
339
325
let connection = Connection :: open ( ":memory:" ) . unwrap ( ) ;
340
326
let provider = OpenMessageProvider :: new ( & connection) ;
327
+ let beacon = Beacon {
328
+ network : "whatever" . to_string ( ) ,
329
+ epoch : Epoch ( 4 ) ,
330
+ immutable_file_number : 400 ,
331
+ } ;
341
332
let ( expr, params) = provider
342
- . get_signed_entity_type_condition ( & SignedEntityType :: CardanoImmutableFilesFull )
333
+ . get_signed_entity_type_condition ( & SignedEntityType :: CardanoImmutableFilesFull ( beacon ) )
343
334
. expand ( ) ;
344
335
345
336
assert_eq ! ( "signed_entity_type_id = ?1" . to_string( ) , expr) ;
@@ -367,11 +358,11 @@ mod tests {
367
358
fn insert_provider_condition ( ) {
368
359
let connection = Connection :: open ( ":memory:" ) . unwrap ( ) ;
369
360
let provider = InsertOpenMessageProvider :: new ( & connection) ;
361
+ let epoch = Epoch ( 12 ) ;
370
362
let ( expr, params) = provider
371
363
. get_insert_condition (
372
- Epoch ( 12 ) ,
373
- & Beacon :: default ( ) ,
374
- & SignedEntityType :: CardanoStakeDistribution ,
364
+ epoch,
365
+ & SignedEntityType :: CardanoImmutableFilesFull ( Beacon :: default ( ) ) ,
375
366
"This is a message" ,
376
367
)
377
368
. unwrap ( )
@@ -383,7 +374,7 @@ mod tests {
383
374
Value :: String ( r#"{"network":"","epoch":0,"immutable_file_number":0}"# . to_string( ) ) ,
384
375
params[ 2 ]
385
376
) ;
386
- assert_eq ! ( Value :: Integer ( 1 ) , params[ 3 ] ) ;
377
+ assert_eq ! ( Value :: Integer ( 2 ) , params[ 3 ] ) ;
387
378
assert_eq ! ( Value :: String ( "This is a message" . to_string( ) ) , params[ 4 ] ) ;
388
379
}
389
380
@@ -413,11 +404,11 @@ mod tests {
413
404
async fn repository_create_open_message ( ) {
414
405
let connection = get_connection ( ) . await ;
415
406
let repository = OpenMessageRepository :: new ( connection. clone ( ) ) ;
407
+ let epoch = Epoch ( 1 ) ;
416
408
let open_message = repository
417
409
. create_open_message (
418
- Epoch ( 1 ) ,
419
- & Beacon :: default ( ) ,
420
- & SignedEntityType :: CardanoImmutableFilesFull ,
410
+ epoch,
411
+ & SignedEntityType :: CardanoImmutableFilesFull ( Beacon :: default ( ) ) ,
421
412
"this is a message" ,
422
413
)
423
414
. await
@@ -426,7 +417,7 @@ mod tests {
426
417
assert_eq ! ( Epoch ( 1 ) , open_message. epoch) ;
427
418
assert_eq ! ( "this is a message" . to_string( ) , open_message. message) ;
428
419
assert_eq ! (
429
- SignedEntityType :: CardanoImmutableFilesFull ,
420
+ SignedEntityType :: CardanoImmutableFilesFull ( Beacon :: default ( ) ) ,
430
421
open_message. signed_entity_type
431
422
) ;
432
423
@@ -459,17 +450,15 @@ mod tests {
459
450
let _ = repository
460
451
. create_open_message (
461
452
Epoch ( 1 ) ,
462
- & Beacon :: default ( ) ,
463
- & SignedEntityType :: CardanoImmutableFilesFull ,
453
+ & SignedEntityType :: CardanoImmutableFilesFull ( Beacon :: default ( ) ) ,
464
454
"this is a message" ,
465
455
)
466
456
. await
467
457
. unwrap ( ) ;
468
458
let _ = repository
469
459
. create_open_message (
470
460
Epoch ( 1 ) ,
471
- & Beacon :: default ( ) ,
472
- & SignedEntityType :: MithrilStakeDistribution ,
461
+ & SignedEntityType :: CardanoImmutableFilesFull ( Beacon :: default ( ) ) ,
473
462
"this is a stake distribution" ,
474
463
)
475
464
. await
0 commit comments