fedimint_client/
api_version_discovery.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use std::collections::BTreeMap;

use anyhow::format_err;
use fedimint_api_client::api::ApiVersionSet;
use fedimint_core::module::{
    ApiVersion, SupportedApiVersionsSummary, SupportedCoreApiVersions, SupportedModuleApiVersions,
};
use fedimint_core::PeerId;

fn discover_common_core_api_version(
    client_versions: &SupportedCoreApiVersions,
    peer_versions: &BTreeMap<PeerId, SupportedCoreApiVersions>,
) -> Option<ApiVersion> {
    let mut best_major = None;
    let mut best_major_peer_num = 0;

    // Find major api version with highest peer number supporting it
    for client_api_version in &client_versions.api {
        let peers_compatible_num = peer_versions
            .values()
            .filter_map(|supported_versions| {
                supported_versions
                    .get_minor_api_version(client_versions.core_consensus, client_api_version.major)
            })
            .filter(|peer_minor| client_api_version.minor <= *peer_minor)
            .count();

        if best_major_peer_num < peers_compatible_num {
            best_major = Some(client_api_version);
            best_major_peer_num = peers_compatible_num;
        }
    }

    // Adjust the minor version to the smallest supported by all matching peers
    best_major.map(
        |ApiVersion {
             major: best_major,
             minor: best_major_minor,
         }| ApiVersion {
            major: best_major,
            minor: peer_versions
                .values()
                .filter_map(|supported| {
                    supported.get_minor_api_version(client_versions.core_consensus, best_major)
                })
                .filter(|peer_minor| best_major_minor <= *peer_minor)
                .min()
                .expect("We must have at least one"),
        },
    )
}

#[test]
fn discover_common_core_api_version_sanity() {
    use fedimint_core::module::MultiApiVersion;

    let core_consensus = fedimint_core::module::CoreConsensusVersion::new(0, 0);
    let client_versions = SupportedCoreApiVersions {
        core_consensus,
        api: MultiApiVersion::try_from_iter([
            ApiVersion { major: 2, minor: 3 },
            ApiVersion { major: 3, minor: 1 },
        ])
        .unwrap(),
    };

    assert!(discover_common_core_api_version(&client_versions, &BTreeMap::from([])).is_none());
    assert_eq!(
        discover_common_core_api_version(
            &client_versions,
            &BTreeMap::from([(
                PeerId::from(0),
                SupportedCoreApiVersions {
                    core_consensus: fedimint_core::module::CoreConsensusVersion::new(0, 0),
                    api: MultiApiVersion::try_from_iter([ApiVersion { major: 2, minor: 3 }])
                        .unwrap(),
                }
            )])
        ),
        Some(ApiVersion { major: 2, minor: 3 })
    );
    assert_eq!(
        discover_common_core_api_version(
            &client_versions,
            &BTreeMap::from([(
                PeerId::from(0),
                SupportedCoreApiVersions {
                    core_consensus: fedimint_core::module::CoreConsensusVersion::new(0, 1), /* different minor consensus version, we don't care */
                    api: MultiApiVersion::try_from_iter([ApiVersion { major: 2, minor: 3 }])
                        .unwrap(),
                }
            )])
        ),
        Some(ApiVersion { major: 2, minor: 3 })
    );
    assert_eq!(
        discover_common_core_api_version(
            &client_versions,
            &BTreeMap::from([(
                PeerId::from(0),
                SupportedCoreApiVersions {
                    core_consensus: fedimint_core::module::CoreConsensusVersion::new(1, 0), /* wrong consensus version */
                    api: MultiApiVersion::try_from_iter([ApiVersion { major: 2, minor: 4 }])
                        .unwrap(),
                }
            )])
        ),
        None
    );
    assert_eq!(
        discover_common_core_api_version(
            &client_versions,
            &BTreeMap::from([
                (
                    PeerId::from(0),
                    SupportedCoreApiVersions {
                        core_consensus,
                        api: MultiApiVersion::try_from_iter([ApiVersion { major: 2, minor: 2 }])
                            .unwrap(),
                    }
                ),
                (
                    PeerId::from(1),
                    SupportedCoreApiVersions {
                        core_consensus,
                        api: MultiApiVersion::try_from_iter([ApiVersion { major: 2, minor: 1 }])
                            .unwrap(),
                    }
                ),
                (
                    PeerId::from(1),
                    SupportedCoreApiVersions {
                        core_consensus,
                        api: MultiApiVersion::try_from_iter([ApiVersion { major: 3, minor: 1 }])
                            .unwrap(),
                    }
                )
            ])
        ),
        Some(ApiVersion { major: 3, minor: 1 })
    );
    assert_eq!(
        discover_common_core_api_version(
            &client_versions,
            &BTreeMap::from([
                (
                    PeerId::from(0),
                    SupportedCoreApiVersions {
                        core_consensus,
                        api: MultiApiVersion::try_from_iter([ApiVersion { major: 2, minor: 4 }])
                            .unwrap(),
                    }
                ),
                (
                    PeerId::from(1),
                    SupportedCoreApiVersions {
                        core_consensus,
                        api: MultiApiVersion::try_from_iter([ApiVersion { major: 2, minor: 5 }])
                            .unwrap(),
                    }
                ),
            ])
        ),
        Some(ApiVersion { major: 2, minor: 4 })
    );
}

fn discover_common_module_api_version(
    client_versions: &SupportedModuleApiVersions,
    peer_versions: &BTreeMap<PeerId, SupportedModuleApiVersions>,
) -> Option<ApiVersion> {
    let mut best_major = None;
    let mut best_major_peer_num = 0;

    // Find major api version with highest peer number supporting it
    for client_api_version in &client_versions.api {
        let peers_compatible_num = peer_versions
            .values()
            .filter_map(|supported_versions| {
                supported_versions.get_minor_api_version(
                    client_versions.core_consensus,
                    client_versions.module_consensus,
                    client_api_version.major,
                )
            })
            .filter(|peer_minor| client_api_version.minor <= *peer_minor)
            .count();

        if best_major_peer_num < peers_compatible_num {
            best_major = Some(client_api_version);
            best_major_peer_num = peers_compatible_num;
        }
    }

    // Adjust the minor version to the smallest supported by all matching peers
    best_major.map(
        |ApiVersion {
             major: best_major,
             minor: best_major_minor,
         }| ApiVersion {
            major: best_major,
            minor: peer_versions
                .values()
                .filter_map(|supported| {
                    supported.get_minor_api_version(
                        client_versions.core_consensus,
                        client_versions.module_consensus,
                        best_major,
                    )
                })
                .filter(|peer_minor| best_major_minor <= *peer_minor)
                .min()
                .expect("We must have at least one"),
        },
    )
}

pub fn discover_common_api_versions_set(
    client_versions: &SupportedApiVersionsSummary,
    peer_versions: &BTreeMap<PeerId, SupportedApiVersionsSummary>,
) -> anyhow::Result<ApiVersionSet> {
    Ok(ApiVersionSet {
        core: discover_common_core_api_version(
            &client_versions.core,
            &peer_versions
                .iter()
                .map(|(peer_id, peer_supported_api_versions)| {
                    (*peer_id, peer_supported_api_versions.core.clone())
                })
                .collect(),
        )
        .ok_or_else(|| format_err!("Could not find a common core API version"))?,
        modules: client_versions
            .modules
            .iter()
            .filter_map(
                |(module_instance_id, client_supported_module_api_versions)| {
                    let discover_common_module_api_version = discover_common_module_api_version(
                        client_supported_module_api_versions,
                        &peer_versions
                            .iter()
                            .filter_map(|(peer_id, peer_supported_api_versions_summary)| {
                                peer_supported_api_versions_summary
                                    .modules
                                    .get(module_instance_id)
                                    .map(|versions| (*peer_id, versions.clone()))
                            })
                            .collect(),
                    );
                    discover_common_module_api_version.map(|v| (*module_instance_id, v))
                },
            )
            .collect(),
    })
}