Skip to content

Commit c721515

Browse files
authored
Merge pull request #1601 from input-output-hk/ensemble/1562/multiple_beacon_types
Handle multiple beacon types in Certificate
2 parents 46c7b79 + 92d877a commit c721515

File tree

81 files changed

+3336
-2485
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+3336
-2485
lines changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-aggregator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-aggregator"
3-
version = "0.4.50"
3+
version = "0.4.51"
44
description = "A Mithril Aggregator server"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-aggregator/src/artifact_builder/cardano_immutable_files_full.rs

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use crate::{
1313
use super::ArtifactBuilder;
1414
use mithril_common::{
1515
entities::{
16-
CardanoDbBeacon, Certificate, CompressionAlgorithm, ProtocolMessage,
17-
ProtocolMessagePartKey, Snapshot,
16+
CardanoDbBeacon, Certificate, CompressionAlgorithm, ProtocolMessagePartKey, Snapshot,
1817
},
1918
StdResult,
2019
};
@@ -55,16 +54,11 @@ impl CardanoImmutableFilesFullArtifactBuilder {
5554
async fn create_snapshot_archive(
5655
&self,
5756
beacon: &CardanoDbBeacon,
58-
protocol_message: &ProtocolMessage,
57+
snapshot_digest: &str,
5958
) -> StdResult<OngoingSnapshot> {
6059
debug!("CardanoImmutableFilesFullArtifactBuilder: create snapshot archive");
6160

6261
let snapshotter = self.snapshotter.clone();
63-
let snapshot_digest = protocol_message
64-
.get_message_part(&ProtocolMessagePartKey::SnapshotDigest)
65-
.ok_or_else(|| {
66-
CardanoImmutableFilesFullArtifactError::MissingProtocolMessage(beacon.clone())
67-
})?;
6862
let snapshot_name = format!(
6963
"{}-e{}-i{}.{}.{}",
7064
beacon.network,
@@ -107,23 +101,16 @@ impl CardanoImmutableFilesFullArtifactBuilder {
107101

108102
async fn create_snapshot(
109103
&self,
110-
certificate: &Certificate,
104+
beacon: CardanoDbBeacon,
111105
ongoing_snapshot: &OngoingSnapshot,
106+
snapshot_digest: String,
112107
remote_locations: Vec<String>,
113108
) -> StdResult<Snapshot> {
114109
debug!("CardanoImmutableFilesFullArtifactBuilder: create snapshot");
115-
let snapshot_digest = certificate
116-
.protocol_message
117-
.get_message_part(&ProtocolMessagePartKey::SnapshotDigest)
118-
.ok_or_else(|| {
119-
CardanoImmutableFilesFullArtifactError::MissingProtocolMessage(
120-
certificate.beacon.clone(),
121-
)
122-
})?
123-
.to_owned();
110+
124111
let snapshot = Snapshot::new(
125112
snapshot_digest,
126-
certificate.beacon.to_owned(),
113+
beacon,
127114
*ongoing_snapshot.get_file_size(),
128115
remote_locations,
129116
self.compression_algorithm,
@@ -141,8 +128,16 @@ impl ArtifactBuilder<CardanoDbBeacon, Snapshot> for CardanoImmutableFilesFullArt
141128
beacon: CardanoDbBeacon,
142129
certificate: &Certificate,
143130
) -> StdResult<Snapshot> {
131+
let snapshot_digest = certificate
132+
.protocol_message
133+
.get_message_part(&ProtocolMessagePartKey::SnapshotDigest)
134+
.ok_or_else(|| {
135+
CardanoImmutableFilesFullArtifactError::MissingProtocolMessage(beacon.clone())
136+
})?
137+
.to_owned();
138+
144139
let ongoing_snapshot = self
145-
.create_snapshot_archive(&beacon, &certificate.protocol_message)
140+
.create_snapshot_archive(&beacon, &snapshot_digest)
146141
.await
147142
.with_context(|| {
148143
"Cardano Immutable Files Full Artifact Builder can not create snapshot archive"
@@ -155,7 +150,7 @@ impl ArtifactBuilder<CardanoDbBeacon, Snapshot> for CardanoImmutableFilesFullArt
155150
})?;
156151

157152
let snapshot = self
158-
.create_snapshot(certificate, &ongoing_snapshot, locations)
153+
.create_snapshot(beacon, &ongoing_snapshot, snapshot_digest, locations)
159154
.await?;
160155

161156
Ok(snapshot)
@@ -194,7 +189,7 @@ mod tests {
194189
CompressionAlgorithm::Zstandard,
195190
);
196191
let artifact = cardano_immutable_files_full_artifact_builder
197-
.compute_artifact(beacon, &certificate)
192+
.compute_artifact(beacon.clone(), &certificate)
198193
.await
199194
.unwrap();
200195
let last_ongoing_snapshot = dumb_snapshotter
@@ -208,7 +203,7 @@ mod tests {
208203
.expect("A snapshot should have been 'uploaded'")];
209204
let artifact_expected = Snapshot::new(
210205
snapshot_digest.to_owned(),
211-
certificate.beacon.to_owned(),
206+
beacon,
212207
*last_ongoing_snapshot.get_file_size(),
213208
remote_locations,
214209
CompressionAlgorithm::Zstandard,
@@ -245,11 +240,7 @@ mod tests {
245240
#[tokio::test]
246241
async fn snapshot_archive_name_after_beacon_values() {
247242
let beacon = CardanoDbBeacon::new("network".to_string(), 20, 145);
248-
let mut message = ProtocolMessage::new();
249-
message.set_message_part(
250-
ProtocolMessagePartKey::SnapshotDigest,
251-
"test+digest".to_string(),
252-
);
243+
let digest = "test+digest";
253244

254245
let cardano_immutable_files_full_artifact_builder =
255246
CardanoImmutableFilesFullArtifactBuilder::new(
@@ -260,27 +251,21 @@ mod tests {
260251
);
261252

262253
let ongoing_snapshot = cardano_immutable_files_full_artifact_builder
263-
.create_snapshot_archive(&beacon, &message)
254+
.create_snapshot_archive(&beacon, digest)
264255
.await
265256
.expect("create_snapshot_archive should not fail");
266257

267258
assert_eq!(
268259
Path::new(&format!(
269-
"{}-e{}-i{}.{}.tar.gz",
270-
beacon.network, *beacon.epoch, beacon.immutable_file_number, "test+digest"
260+
"{}-e{}-i{}.{digest}.tar.gz",
261+
beacon.network, *beacon.epoch, beacon.immutable_file_number,
271262
)),
272263
ongoing_snapshot.get_file_path()
273264
);
274265
}
275266

276267
#[tokio::test]
277268
async fn snapshot_archive_name_after_compression_algorithm() {
278-
let mut message = ProtocolMessage::new();
279-
message.set_message_part(
280-
ProtocolMessagePartKey::SnapshotDigest,
281-
"test+digest".to_string(),
282-
);
283-
284269
let mut invalid_result: Vec<CompressionAlgorithm> = vec![];
285270

286271
for algorithm in CompressionAlgorithm::list() {
@@ -293,7 +278,7 @@ mod tests {
293278
);
294279

295280
let ongoing_snapshot = cardano_immutable_files_full_artifact_builder
296-
.create_snapshot_archive(&CardanoDbBeacon::default(), &message)
281+
.create_snapshot_archive(&CardanoDbBeacon::default(), "test+digest")
297282
.await
298283
.expect("create_snapshot_archive should not fail");
299284
let file_name = ongoing_snapshot

mithril-aggregator/src/artifact_builder/cardano_transactions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ mod tests {
7171
certificate
7272
};
7373

74+
let beacon = certificate.as_cardano_db_beacon();
7475
let cardano_transaction_artifact_builder = CardanoTransactionsArtifactBuilder::new();
7576
let artifact = cardano_transaction_artifact_builder
76-
.compute_artifact(certificate.beacon.clone(), &certificate)
77+
.compute_artifact(beacon.clone(), &certificate)
7778
.await
7879
.unwrap();
79-
let artifact_expected =
80-
CardanoTransactionsSnapshot::new("merkleroot".to_string(), certificate.beacon);
80+
let artifact_expected = CardanoTransactionsSnapshot::new("merkleroot".to_string(), beacon);
8181
assert_eq!(artifact_expected, artifact);
8282
}
8383

mithril-aggregator/src/configuration.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use std::path::PathBuf;
99
use std::str::FromStr;
1010

1111
use mithril_common::entities::{
12-
CardanoDbBeacon, CompressionAlgorithm, HexEncodedGenesisVerificationKey, ProtocolParameters,
13-
SignedEntityType, SignedEntityTypeDiscriminants,
12+
CompressionAlgorithm, HexEncodedGenesisVerificationKey, ProtocolParameters, SignedEntityType,
13+
SignedEntityTypeDiscriminants, TimePoint,
1414
};
1515
use mithril_common::{CardanoNetwork, StdResult};
1616

@@ -285,12 +285,14 @@ impl Configuration {
285285
/// The signed entity types are discarded if they are not declared in the [SignedEntityType] enum.
286286
pub fn list_allowed_signed_entity_types(
287287
&self,
288-
beacon: &CardanoDbBeacon,
288+
time_point: &TimePoint,
289289
) -> StdResult<Vec<SignedEntityType>> {
290290
let allowed_discriminants = self.list_allowed_signed_entity_types_discriminants()?;
291291
let signed_entity_types = allowed_discriminants
292292
.into_iter()
293-
.map(|discriminant| SignedEntityType::from_beacon(&discriminant, beacon))
293+
.map(|discriminant| {
294+
SignedEntityType::from_time_point(&discriminant, &self.network, time_point)
295+
})
294296
.collect();
295297

296298
Ok(signed_entity_types)
@@ -502,16 +504,20 @@ mod test {
502504

503505
#[test]
504506
fn test_list_allowed_signed_entity_types_without_specific_configuration() {
507+
let beacon = fake_data::beacon();
505508
let config = Configuration {
506509
signed_entity_types: None,
510+
network: beacon.network.clone(),
507511
..Configuration::new_sample()
508512
};
509-
let beacon = fake_data::beacon();
513+
let time_point = TimePoint::new(*beacon.epoch, beacon.immutable_file_number);
510514

511515
let discriminants = config
512516
.list_allowed_signed_entity_types_discriminants()
513517
.unwrap();
514-
let signed_entity_types = config.list_allowed_signed_entity_types(&beacon).unwrap();
518+
let signed_entity_types = config
519+
.list_allowed_signed_entity_types(&time_point)
520+
.unwrap();
515521

516522
assert_eq!(
517523
vec![
@@ -531,18 +537,22 @@ mod test {
531537

532538
#[test]
533539
fn test_list_allowed_signed_entity_types_with_specific_configuration() {
540+
let beacon = fake_data::beacon();
534541
let config = Configuration {
542+
network: beacon.network.clone(),
535543
signed_entity_types: Some(
536544
"MithrilStakeDistribution,Unknown, CardanoStakeDistribution".to_string(),
537545
),
538546
..Configuration::new_sample()
539547
};
540-
let beacon = fake_data::beacon();
548+
let time_point = TimePoint::new(*beacon.epoch, beacon.immutable_file_number);
541549

542550
let discriminants = config
543551
.list_allowed_signed_entity_types_discriminants()
544552
.unwrap();
545-
let signed_entity_types = config.list_allowed_signed_entity_types(&beacon).unwrap();
553+
let signed_entity_types = config
554+
.list_allowed_signed_entity_types(&time_point)
555+
.unwrap();
546556

547557
assert_eq!(
548558
vec![

mithril-aggregator/src/database/migration.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,5 +619,85 @@ insert into signed_entity_type (signed_entity_type_id, name)
619619
values (3, 'Cardano Transactions');
620620
"#,
621621
),
622+
// Migration 22
623+
// Certificate table:
624+
// * Remove beacon
625+
// * Add network, immutable file number, signed_entity_type_id, signed_entity_type columns
626+
SqlMigration::new(
627+
22,
628+
r#"
629+
-- disable foreign keys since we will delete tables linked using them
630+
pragma foreign_keys=false;
631+
632+
CREATE TABLE IF NOT EXISTS "new_certificate" (
633+
certificate_id text not null,
634+
parent_certificate_id text,
635+
message text not null,
636+
signature text not null,
637+
aggregate_verification_key text not null,
638+
epoch integer not null,
639+
network text not null,
640+
immutable_file_number integer not null,
641+
signed_entity_type_id integer not null,
642+
signed_entity_beacon json not null,
643+
protocol_version text not null,
644+
protocol_parameters json not null,
645+
protocol_message json not null,
646+
signers json not null,
647+
initiated_at text not null,
648+
sealed_at text not null,
649+
primary key (certificate_id),
650+
foreign key (parent_certificate_id) references certificate(certificate_id)
651+
foreign key (signed_entity_type_id) references signed_entity_type(signed_entity_type_id)
652+
);
653+
654+
insert into new_certificate
655+
( certificate_id, parent_certificate_id, message, signature, aggregate_verification_key,
656+
epoch,
657+
network,
658+
immutable_file_number,
659+
signed_entity_type_id,
660+
signed_entity_beacon,
661+
protocol_version, protocol_parameters, protocol_message,
662+
signers, initiated_at, sealed_at)
663+
select c.certificate_id, c.parent_certificate_id, c.message, c.signature, c.aggregate_verification_key,
664+
c.epoch,
665+
json_extract(c.beacon, '$.network'),
666+
json_extract(c.beacon, '$.immutable_file_number'),
667+
-- genesis certificate doesn't have a signed_entity, we can just use directly the MithrilStakeDistribution
668+
coalesce(s.signed_entity_type_id, 0),
669+
-- genesis certificate doesn't have a signed_entity, so we need to deduce it from the old certificate
670+
coalesce(s.beacon, c.epoch),
671+
c.protocol_version, c.protocol_parameters, c.protocol_message,
672+
c.signers, c.initiated_at, c.sealed_at
673+
from certificate c
674+
left join signed_entity s on s.certificate_id = c.certificate_id
675+
order by c.rowid asc;
676+
677+
678+
drop table certificate;
679+
alter table new_certificate rename to certificate;
680+
681+
CREATE INDEX epoch_index on certificate(epoch);
682+
683+
-- reenable foreign keys
684+
pragma foreign_key_check;
685+
pragma foreign_keys=true;
686+
"#,
687+
),
688+
// Migration 23
689+
// Alter `pending_certificate` table to use only an Epoch instead of a full beacon.
690+
SqlMigration::new(
691+
23,
692+
r#"
693+
create table if not exists pending_certificate (key_hash text primary key, key json not null, value json not null);
694+
update pending_certificate
695+
set value =
696+
json_remove(
697+
json_insert(value, '$.epoch', json_extract(value, '$.beacon.epoch')),
698+
'$.beacon'
699+
);
700+
"#,
701+
),
622702
]
623703
}

0 commit comments

Comments
 (0)