@@ -11,6 +11,10 @@ use mithril_common::{
11
11
HexEncodedAgregateVerificationKey , HexEncodedKey , ProtocolMessage , ProtocolParameters ,
12
12
ProtocolVersion , StakeDistributionParty ,
13
13
} ,
14
+ messages:: {
15
+ CertificateListItemMessage , CertificateListItemMessageMetadata , CertificateMessage ,
16
+ CertificateMetadataMessagePart ,
17
+ } ,
14
18
sqlite:: {
15
19
EntityCursor , HydrationError , Projection , Provider , SourceAlias , SqLiteEntity ,
16
20
WhereCondition ,
@@ -159,6 +163,57 @@ impl From<CertificateRecord> for Certificate {
159
163
}
160
164
}
161
165
166
+ impl From < CertificateRecord > for CertificateMessage {
167
+ fn from ( value : CertificateRecord ) -> Self {
168
+ let metadata = CertificateMetadataMessagePart {
169
+ protocol_version : value. protocol_version ,
170
+ protocol_parameters : value. protocol_parameters ,
171
+ initiated_at : value. initiated_at ,
172
+ sealed_at : value. sealed_at ,
173
+ signers : value. signers ,
174
+ } ;
175
+ let ( multi_signature, genesis_signature) = if value. parent_certificate_id . is_none ( ) {
176
+ ( String :: new ( ) , value. signature )
177
+ } else {
178
+ ( value. signature , String :: new ( ) )
179
+ } ;
180
+
181
+ CertificateMessage {
182
+ hash : value. certificate_id ,
183
+ previous_hash : value. parent_certificate_id . unwrap_or_default ( ) ,
184
+ beacon : value. beacon ,
185
+ metadata,
186
+ protocol_message : value. protocol_message ,
187
+ signed_message : value. message ,
188
+ aggregate_verification_key : value. aggregate_verification_key ,
189
+ multi_signature,
190
+ genesis_signature,
191
+ }
192
+ }
193
+ }
194
+
195
+ impl From < CertificateRecord > for CertificateListItemMessage {
196
+ fn from ( value : CertificateRecord ) -> Self {
197
+ let metadata = CertificateListItemMessageMetadata {
198
+ protocol_version : value. protocol_version ,
199
+ protocol_parameters : value. protocol_parameters ,
200
+ initiated_at : value. initiated_at ,
201
+ sealed_at : value. sealed_at ,
202
+ total_signers : value. signers . len ( ) ,
203
+ } ;
204
+
205
+ CertificateListItemMessage {
206
+ hash : value. certificate_id ,
207
+ previous_hash : value. parent_certificate_id . unwrap_or_default ( ) ,
208
+ beacon : value. beacon ,
209
+ metadata,
210
+ protocol_message : value. protocol_message ,
211
+ signed_message : value. message ,
212
+ aggregate_verification_key : value. aggregate_verification_key ,
213
+ }
214
+ }
215
+ }
216
+
162
217
impl SqLiteEntity for CertificateRecord {
163
218
fn hydrate ( row : sqlite:: Row ) -> Result < Self , HydrationError >
164
219
where
@@ -549,15 +604,21 @@ impl CertificateRepository {
549
604
}
550
605
551
606
/// Return the certificate corresponding to the given hash if any.
552
- pub async fn get_certificate ( & self , hash : & str ) -> StdResult < Option < Certificate > > {
607
+ pub async fn get_certificate < T > ( & self , hash : & str ) -> StdResult < Option < T > >
608
+ where
609
+ T : From < CertificateRecord > ,
610
+ {
553
611
let provider = CertificateRecordProvider :: new ( & self . connection ) ;
554
612
let mut cursor = provider. get_by_certificate_id ( hash) ?;
555
613
556
614
Ok ( cursor. next ( ) . map ( |v| v. into ( ) ) )
557
615
}
558
616
559
617
/// Return the latest certificates.
560
- pub async fn get_latest_certificates ( & self , last_n : usize ) -> StdResult < Vec < Certificate > > {
618
+ pub async fn get_latest_certificates < T > ( & self , last_n : usize ) -> StdResult < Vec < T > >
619
+ where
620
+ T : From < CertificateRecord > ,
621
+ {
561
622
let provider = CertificateRecordProvider :: new ( & self . connection ) ;
562
623
let cursor = provider. get_all ( ) ?;
563
624
@@ -567,10 +628,10 @@ impl CertificateRepository {
567
628
/// Return the first certificate signed per epoch as the reference
568
629
/// certificate for this Epoch. This will be the parent certificate for all
569
630
/// other certificates issued within this Epoch.
570
- pub async fn get_master_certificate_for_epoch (
571
- & self ,
572
- epoch : Epoch ,
573
- ) -> StdResult < Option < Certificate > > {
631
+ pub async fn get_master_certificate_for_epoch < T > ( & self , epoch : Epoch ) -> StdResult < Option < T > >
632
+ where
633
+ T : From < CertificateRecord > ,
634
+ {
574
635
let provider = MasterCertificateProvider :: new ( & self . connection ) ;
575
636
let mut cursor = provider. find ( provider. get_master_certificate_condition ( epoch) ) ?;
576
637
@@ -1037,12 +1098,15 @@ protocol_message, signers, initiated_at, sealed_at) values \
1037
1098
}
1038
1099
}
1039
1100
1040
- let repository = CertificateRepository :: new ( connection) ;
1041
- let certificate = repository. get_certificate ( "whatever" ) . await . unwrap ( ) ;
1101
+ let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
1102
+ let certificate = repository
1103
+ . get_certificate :: < Certificate > ( "whatever" )
1104
+ . await
1105
+ . unwrap ( ) ;
1042
1106
assert ! ( certificate. is_none( ) ) ;
1043
1107
1044
1108
let certificate = repository
1045
- . get_certificate ( & expected_hash)
1109
+ . get_certificate :: < Certificate > ( & expected_hash)
1046
1110
. await
1047
1111
. unwrap ( )
1048
1112
. expect ( "The certificate exist and should be returned." ) ;
@@ -1090,9 +1154,9 @@ protocol_message, signers, initiated_at, sealed_at) values \
1090
1154
let certificates = vec ! [ ] ;
1091
1155
insert_certificate_records ( connection. clone ( ) , certificates) . await ;
1092
1156
1093
- let repository = CertificateRepository :: new ( connection) ;
1157
+ let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
1094
1158
let certificate = repository
1095
- . get_master_certificate_for_epoch ( Epoch ( 1 ) )
1159
+ . get_master_certificate_for_epoch :: < Certificate > ( Epoch ( 1 ) )
1096
1160
. await
1097
1161
. unwrap ( ) ;
1098
1162
@@ -1107,9 +1171,9 @@ protocol_message, signers, initiated_at, sealed_at) values \
1107
1171
let expected_certificate: Certificate = certificate. clone ( ) . into ( ) ;
1108
1172
insert_certificate_records ( connection. clone ( ) , vec ! [ certificate] ) . await ;
1109
1173
1110
- let repository = CertificateRepository :: new ( connection) ;
1174
+ let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
1111
1175
let certificate = repository
1112
- . get_master_certificate_for_epoch ( Epoch ( 1 ) )
1176
+ . get_master_certificate_for_epoch :: < Certificate > ( Epoch ( 1 ) )
1113
1177
. await
1114
1178
. unwrap ( )
1115
1179
. expect ( "This should return a certificate." ) ;
@@ -1130,9 +1194,9 @@ protocol_message, signers, initiated_at, sealed_at) values \
1130
1194
let expected_certificate: Certificate = certificates. first ( ) . unwrap ( ) . clone ( ) . into ( ) ;
1131
1195
insert_certificate_records ( connection. clone ( ) , certificates) . await ;
1132
1196
1133
- let repository = CertificateRepository :: new ( connection) ;
1197
+ let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
1134
1198
let certificate = repository
1135
- . get_master_certificate_for_epoch ( Epoch ( 1 ) )
1199
+ . get_master_certificate_for_epoch :: < Certificate > ( Epoch ( 1 ) )
1136
1200
. await
1137
1201
. unwrap ( )
1138
1202
. expect ( "This should return a certificate." ) ;
@@ -1153,9 +1217,9 @@ protocol_message, signers, initiated_at, sealed_at) values \
1153
1217
let expected_certificate: Certificate = certificates. first ( ) . unwrap ( ) . clone ( ) . into ( ) ;
1154
1218
insert_certificate_records ( connection. clone ( ) , certificates) . await ;
1155
1219
1156
- let repository = CertificateRepository :: new ( connection) ;
1220
+ let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
1157
1221
let certificate = repository
1158
- . get_master_certificate_for_epoch ( Epoch ( 2 ) )
1222
+ . get_master_certificate_for_epoch :: < Certificate > ( Epoch ( 2 ) )
1159
1223
. await
1160
1224
. unwrap ( )
1161
1225
. expect ( "This should return a certificate." ) ;
@@ -1177,9 +1241,9 @@ protocol_message, signers, initiated_at, sealed_at) values \
1177
1241
let expected_certificate: Certificate = certificates. last ( ) . unwrap ( ) . clone ( ) . into ( ) ;
1178
1242
insert_certificate_records ( connection. clone ( ) , certificates) . await ;
1179
1243
1180
- let repository = CertificateRepository :: new ( connection) ;
1244
+ let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
1181
1245
let certificate = repository
1182
- . get_master_certificate_for_epoch ( Epoch ( 2 ) )
1246
+ . get_master_certificate_for_epoch :: < Certificate > ( Epoch ( 2 ) )
1183
1247
. await
1184
1248
. unwrap ( )
1185
1249
. expect ( "This should return a certificate." ) ;
@@ -1203,7 +1267,7 @@ protocol_message, signers, initiated_at, sealed_at) values \
1203
1267
let expected_certificate: Certificate = certificates. get ( 3 ) . unwrap ( ) . clone ( ) . into ( ) ;
1204
1268
insert_certificate_records ( connection. clone ( ) , certificates) . await ;
1205
1269
1206
- let repository = CertificateRepository :: new ( connection) ;
1270
+ let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
1207
1271
let certificate = repository
1208
1272
. get_master_certificate_for_epoch ( Epoch ( 2 ) )
1209
1273
. await
@@ -1224,9 +1288,9 @@ protocol_message, signers, initiated_at, sealed_at) values \
1224
1288
] ;
1225
1289
insert_certificate_records ( connection. clone ( ) , certificates) . await ;
1226
1290
1227
- let repository = CertificateRepository :: new ( connection) ;
1291
+ let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
1228
1292
let certificate = repository
1229
- . get_master_certificate_for_epoch ( Epoch ( 3 ) )
1293
+ . get_master_certificate_for_epoch :: < Certificate > ( Epoch ( 3 ) )
1230
1294
. await
1231
1295
. unwrap ( ) ;
1232
1296
@@ -1247,7 +1311,7 @@ protocol_message, signers, initiated_at, sealed_at) values \
1247
1311
let expected_certificate: Certificate = certificates. last ( ) . unwrap ( ) . clone ( ) . into ( ) ;
1248
1312
insert_certificate_records ( connection. clone ( ) , certificates) . await ;
1249
1313
1250
- let repository = CertificateRepository :: new ( connection) ;
1314
+ let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
1251
1315
let certificate = repository
1252
1316
. get_master_certificate_for_epoch ( Epoch ( 2 ) )
1253
1317
. await
@@ -1273,7 +1337,7 @@ protocol_message, signers, initiated_at, sealed_at) values \
1273
1337
let expected_certificate: Certificate = certificates. last ( ) . unwrap ( ) . clone ( ) . into ( ) ;
1274
1338
insert_certificate_records ( connection. clone ( ) , certificates) . await ;
1275
1339
1276
- let repository = CertificateRepository :: new ( connection) ;
1340
+ let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
1277
1341
let certificate = repository
1278
1342
. get_master_certificate_for_epoch ( Epoch ( 2 ) )
1279
1343
. await
@@ -1297,7 +1361,7 @@ protocol_message, signers, initiated_at, sealed_at) values \
1297
1361
let expected_certificate: Certificate = certificates. last ( ) . unwrap ( ) . clone ( ) . into ( ) ;
1298
1362
insert_certificate_records ( connection. clone ( ) , certificates) . await ;
1299
1363
1300
- let repository = CertificateRepository :: new ( connection) ;
1364
+ let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
1301
1365
let certificate = repository
1302
1366
. get_master_certificate_for_epoch ( Epoch ( 2 ) )
1303
1367
. await
@@ -1322,9 +1386,9 @@ protocol_message, signers, initiated_at, sealed_at) values \
1322
1386
}
1323
1387
}
1324
1388
1325
- let repository = CertificateRepository :: new ( connection) ;
1389
+ let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
1326
1390
let certificate = repository
1327
- . get_master_certificate_for_epoch ( * epoch)
1391
+ . get_master_certificate_for_epoch :: < Certificate > ( * epoch)
1328
1392
. await
1329
1393
. unwrap ( )
1330
1394
. expect ( "This should return a certificate." ) ;
@@ -1337,7 +1401,7 @@ protocol_message, signers, initiated_at, sealed_at) values \
1337
1401
let ( certificates, _) = setup_certificate_chain ( 5 , 3 ) ;
1338
1402
let mut deps = DependenciesBuilder :: new ( Configuration :: new_sample ( ) ) ;
1339
1403
let connection = deps. get_sqlite_connection ( ) . await . unwrap ( ) ;
1340
- let repository = CertificateRepository :: new ( connection) ;
1404
+ let repository: CertificateRepository = CertificateRepository :: new ( connection) ;
1341
1405
let certificate = repository
1342
1406
. create_certificate ( certificates[ 4 ] . clone ( ) )
1343
1407
. await
0 commit comments