mithril_aggregator/database/query/certificate/
insert_certificate.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use std::iter::repeat;

use sqlite::Value;

use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};

use crate::database::record::CertificateRecord;

/// Query to insert [CertificateRecord] in the sqlite database
pub struct InsertCertificateRecordQuery {
    condition: WhereCondition,
}

impl InsertCertificateRecordQuery {
    pub fn one(certificate_record: CertificateRecord) -> Self {
        Self::many(vec![certificate_record])
    }

    pub fn many(certificates_records: Vec<CertificateRecord>) -> Self {
        let columns = "(\
        certificate_id, \
        parent_certificate_id, \
        message, \
        signature, \
        aggregate_verification_key, \
        epoch, \
        network, \
        signed_entity_type_id, \
        signed_entity_beacon, \
        protocol_version, \
        protocol_parameters, \
        protocol_message, \
        signers, \
        initiated_at, \
        sealed_at)";
        let values_columns: Vec<&str> =
            repeat("(?*, ?*, ?*, ?*, ?*, ?*, ?*, ?*, ?*, ?*, ?*, ?*, ?*, ?*, ?*)")
                .take(certificates_records.len())
                .collect();

        let values: Vec<Value> = certificates_records
            .into_iter()
            .flat_map(|certificate_record| {
                vec![
                    Value::String(certificate_record.certificate_id),
                    match certificate_record.parent_certificate_id {
                        Some(parent_certificate_id) => Value::String(parent_certificate_id),
                        None => Value::Null,
                    },
                    Value::String(certificate_record.message),
                    Value::String(certificate_record.signature),
                    Value::String(certificate_record.aggregate_verification_key),
                    Value::Integer(certificate_record.epoch.try_into().unwrap()),
                    Value::String(certificate_record.network),
                    Value::Integer(certificate_record.signed_entity_type.index() as i64),
                    Value::String(
                        certificate_record
                            .signed_entity_type
                            .get_json_beacon()
                            .unwrap(),
                    ),
                    Value::String(certificate_record.protocol_version),
                    Value::String(
                        serde_json::to_string(&certificate_record.protocol_parameters).unwrap(),
                    ),
                    Value::String(
                        serde_json::to_string(&certificate_record.protocol_message).unwrap(),
                    ),
                    Value::String(serde_json::to_string(&certificate_record.signers).unwrap()),
                    Value::String(certificate_record.initiated_at.to_rfc3339()),
                    Value::String(certificate_record.sealed_at.to_rfc3339()),
                ]
            })
            .collect();

        let condition = WhereCondition::new(
            format!("{columns} values {}", values_columns.join(", ")).as_str(),
            values,
        );

        Self { condition }
    }
}

impl Query for InsertCertificateRecordQuery {
    type Entity = CertificateRecord;

    fn filters(&self) -> WhereCondition {
        self.condition.clone()
    }

    fn get_definition(&self, condition: &str) -> String {
        // it is important to alias the fields with the same name as the table
        // since the table cannot be aliased in a RETURNING statement in SQLite.
        let projection = Self::Entity::get_projection()
            .expand(SourceAlias::new(&[("{:certificate:}", "certificate")]));

        format!("insert into certificate {condition} returning {projection}")
    }
}

#[cfg(test)]
mod tests {
    use mithril_common::crypto_helper::tests_setup::setup_certificate_chain;
    use mithril_persistence::sqlite::ConnectionExtensions;

    use crate::database::test_helper::main_db_connection;

    use super::*;

    #[test]
    fn test_insert_certificate_record() {
        let (certificates, _) = setup_certificate_chain(5, 2);

        let connection = main_db_connection().unwrap();

        for certificate in certificates {
            let certificate_record: CertificateRecord = certificate.into();
            let certificate_record_saved = connection
                .fetch_first(InsertCertificateRecordQuery::one(
                    certificate_record.clone(),
                ))
                .unwrap();
            assert_eq!(Some(certificate_record), certificate_record_saved);
        }
    }

    #[test]
    fn test_insert_many_certificates_records() {
        let (certificates, _) = setup_certificate_chain(5, 2);
        let certificates_records: Vec<CertificateRecord> =
            certificates.into_iter().map(|cert| cert.into()).collect();

        let connection = main_db_connection().unwrap();

        let certificates_records_saved: Vec<CertificateRecord> = connection
            .fetch_collect(InsertCertificateRecordQuery::many(
                certificates_records.clone(),
            ))
            .expect("saving many records should not fail");

        assert_eq!(certificates_records, certificates_records_saved);
    }
}