Skip to main content

sui_rpc_api/
client_protocol_version.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use sui_protocol_config::ProtocolVersion;
5use tonic::metadata::MetadataMap;
6
7/// Request header carrying the highest protocol version whose types the client can decode.
8pub const X_SUI_CLIENT_PROTOCOL_VERSION: &str = "x-sui-client-protocol-version";
9
10/// The highest protocol version the client that sent a request understands, if it said.
11///
12/// Self-reported, so it may be used to pick a response shape the client can decode but never for
13/// access control. `None` for clients that predate the header or send a malformed value.
14pub fn client_protocol_version(metadata: &MetadataMap) -> Option<ProtocolVersion> {
15    metadata
16        .get(X_SUI_CLIENT_PROTOCOL_VERSION)?
17        .to_str()
18        .ok()?
19        .parse()
20        .ok()
21        .map(ProtocolVersion::new)
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    fn parse(value: Option<&'static str>) -> Option<ProtocolVersion> {
29        let mut metadata = MetadataMap::new();
30        if let Some(value) = value {
31            metadata.insert(X_SUI_CLIENT_PROTOCOL_VERSION, value.parse().unwrap());
32        }
33        client_protocol_version(&metadata)
34    }
35
36    #[test]
37    fn parses_header() {
38        assert_eq!(parse(Some("138")), Some(ProtocolVersion::new(138)));
39        assert_eq!(parse(None), None);
40        assert_eq!(parse(Some("")), None);
41        assert_eq!(parse(Some("-1")), None);
42        assert_eq!(parse(Some("1.2")), None);
43        assert_eq!(parse(Some("18446744073709551616")), None);
44    }
45}