penumbra_shielded_pool/
fmd.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use anyhow::{anyhow, Result};
use decaf377_fmd::Precision;
use penumbra_proto::{
    core::component::shielded_pool::v1::{self as pb},
    DomainType,
};
use serde::{Deserialize, Serialize};

pub mod state_key;

/// How long users have to switch to updated parameters.
pub const FMD_GRACE_PERIOD_BLOCKS_DEFAULT: u64 = 1 << 4;

pub fn should_update_fmd_params(fmd_grace_period_blocks: u64, height: u64) -> bool {
    height % fmd_grace_period_blocks == 0
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(try_from = "pb::FmdParameters", into = "pb::FmdParameters")]
pub struct Parameters {
    /// FMD Precision.
    pub precision: Precision,
    /// The block height at which these parameters became effective.
    pub as_of_block_height: u64,
}

impl DomainType for Parameters {
    type Proto = pb::FmdParameters;
}

impl TryFrom<pb::FmdParameters> for Parameters {
    type Error = anyhow::Error;

    fn try_from(msg: pb::FmdParameters) -> Result<Self> {
        Ok(Parameters {
            precision: msg.precision_bits.try_into()?,
            as_of_block_height: msg.as_of_block_height,
        })
    }
}

impl From<Parameters> for pb::FmdParameters {
    fn from(params: Parameters) -> Self {
        pb::FmdParameters {
            precision_bits: params.precision.bits() as u32,
            as_of_block_height: params.as_of_block_height,
        }
    }
}

impl Default for Parameters {
    fn default() -> Self {
        Self {
            precision: Precision::default(),
            as_of_block_height: 1,
        }
    }
}

/// A struct holding params for the sliding window algorithm
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SlidingWindow {
    window: u32,
    targeted_detections_per_window: u32,
}

impl SlidingWindow {
    pub fn updated_fmd_params(
        &self,
        old: &Parameters,
        state: MetaParametersAlgorithmState,
        height: u64,
        clue_count_delta: (u64, u64),
    ) -> (Parameters, MetaParametersAlgorithmState) {
        // An edge case, which should act as a constant.
        if self.window == 0 {
            return (
                old.clone(),
                MetaParametersAlgorithmState::SlidingWindow {
                    approximate_clue_count: 0,
                },
            );
        }

        let new_clues_in_period = clue_count_delta.1.saturating_sub(clue_count_delta.0);

        let projected_clue_count = u64::from(self.window) * new_clues_in_period;
        let old_approximate_clue_count = match state {
            MetaParametersAlgorithmState::SlidingWindow {
                approximate_clue_count,
            } => approximate_clue_count,
            _ => 0,
        };
        // ((w - 1) * old + new) / w, but using u64 for more precision, and saturating
        let approximate_clue_count: u32 = u32::try_from(
            (u64::from(old_approximate_clue_count)
                .saturating_mul((self.window - 1).into())
                .saturating_add(projected_clue_count))
                / u64::from(self.window),
        )
        .unwrap_or(u32::MAX);

        // 1 / this_number of transactions should be detected as false positives
        let inverse_detection_ratio = approximate_clue_count
            .checked_div(self.targeted_detections_per_window)
            .unwrap_or(0);
        // To receive the power of two *above* the targeted number of clues,
        // take the base 2 logarithm, round down, and use 1 for 0 clues
        let required_precision = if inverse_detection_ratio == 0 {
            Precision::new(1u8).expect("1 is a valid precision")
        } else {
            let lg_inverse_ratio = 63 - inverse_detection_ratio.leading_zeros();
            if lg_inverse_ratio > Precision::MAX.bits().into() {
                Precision::MAX
            } else {
                Precision::new(lg_inverse_ratio as u8)
                    .expect("unexpected precision overflow after check")
            }
        };
        (
            Parameters {
                precision: required_precision,
                as_of_block_height: height,
            },
            MetaParametersAlgorithmState::SlidingWindow {
                approximate_clue_count,
            },
        )
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MetaParametersAlgorithm {
    /// Use a fixed precision forever.
    Fixed(Precision),
    /// Use a sliding window
    SlidingWindow(SlidingWindow),
}

/// Meta parameters governing how FMD parameters change.
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(try_from = "pb::FmdMetaParameters", into = "pb::FmdMetaParameters")]
pub struct MetaParameters {
    pub fmd_grace_period_blocks: u64,
    pub algorithm: MetaParametersAlgorithm,
}

impl TryFrom<pb::FmdMetaParameters> for MetaParameters {
    type Error = anyhow::Error;

    fn try_from(value: pb::FmdMetaParameters) -> Result<Self> {
        let fmd_grace_period_blocks = value.fmd_grace_period_blocks;
        let algorithm = match value
            .algorithm
            .ok_or(anyhow!("FmdMetaParameters missing algorithm"))?
        {
            pb::fmd_meta_parameters::Algorithm::FixedPrecisionBits(p) => {
                MetaParametersAlgorithm::Fixed(Precision::new(p as u8)?)
            }
            pb::fmd_meta_parameters::Algorithm::SlidingWindow(x) => {
                MetaParametersAlgorithm::SlidingWindow(SlidingWindow {
                    window: x.window_update_periods,
                    targeted_detections_per_window: x.targeted_detections_per_window,
                })
            }
        };
        Ok(MetaParameters {
            fmd_grace_period_blocks,
            algorithm,
        })
    }
}

impl From<MetaParameters> for pb::FmdMetaParameters {
    fn from(value: MetaParameters) -> Self {
        let algorithm = match value.algorithm {
            MetaParametersAlgorithm::Fixed(p) => {
                pb::fmd_meta_parameters::Algorithm::FixedPrecisionBits(p.bits().into())
            }
            MetaParametersAlgorithm::SlidingWindow(SlidingWindow {
                window,
                targeted_detections_per_window,
            }) => pb::fmd_meta_parameters::Algorithm::SlidingWindow(
                pb::fmd_meta_parameters::AlgorithmSlidingWindow {
                    window_update_periods: window,
                    targeted_detections_per_window,
                },
            ),
        };
        pb::FmdMetaParameters {
            fmd_grace_period_blocks: value.fmd_grace_period_blocks,
            algorithm: Some(algorithm),
        }
    }
}

impl DomainType for MetaParameters {
    type Proto = pb::FmdMetaParameters;
}

impl Default for MetaParameters {
    fn default() -> Self {
        Self {
            fmd_grace_period_blocks: FMD_GRACE_PERIOD_BLOCKS_DEFAULT,
            algorithm: MetaParametersAlgorithm::Fixed(Precision::default()),
        }
    }
}

/// If any, the current state for the algorithm we're using.
///
/// This allows algorithms to hold arbitrary state. The algorithms need to be able
/// to start from having no state and function appropriately, which allows for good
/// backwards-compatability.
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(
    try_from = "pb::FmdMetaParametersAlgorithmState",
    into = "pb::FmdMetaParametersAlgorithmState"
)]
pub enum MetaParametersAlgorithmState {
    /// A catch-all case to allow us to explicitly handle not having state
    Nothing,
    /// The state for the fixed algorithm
    Fixed,
    /// The state for the sliding window algorithm.
    SlidingWindow {
        /// The approximate number of clues in the previous window.
        approximate_clue_count: u32,
    },
}

impl TryFrom<pb::FmdMetaParametersAlgorithmState> for MetaParametersAlgorithmState {
    type Error = anyhow::Error;

    fn try_from(value: pb::FmdMetaParametersAlgorithmState) -> Result<Self> {
        Ok(match value.state {
            Some(pb::fmd_meta_parameters_algorithm_state::State::Fixed(_)) => Self::Fixed,
            Some(pb::fmd_meta_parameters_algorithm_state::State::SlidingWindow(x)) => {
                Self::SlidingWindow {
                    approximate_clue_count: x.approximate_clue_count,
                }
            }
            None => Self::Nothing,
        })
    }
}

impl From<MetaParametersAlgorithmState> for pb::FmdMetaParametersAlgorithmState {
    fn from(value: MetaParametersAlgorithmState) -> Self {
        let state = match value {
            MetaParametersAlgorithmState::Nothing => None,
            MetaParametersAlgorithmState::Fixed => {
                Some(pb::fmd_meta_parameters_algorithm_state::State::Fixed(
                    pb::fmd_meta_parameters_algorithm_state::FixedState {},
                ))
            }
            MetaParametersAlgorithmState::SlidingWindow {
                approximate_clue_count,
            } => Some(
                pb::fmd_meta_parameters_algorithm_state::State::SlidingWindow(
                    pb::fmd_meta_parameters_algorithm_state::SlidingWindowState {
                        approximate_clue_count,
                    },
                ),
            ),
        };
        pb::FmdMetaParametersAlgorithmState { state }
    }
}

impl DomainType for MetaParametersAlgorithmState {
    type Proto = pb::FmdMetaParametersAlgorithmState;
}

impl Default for MetaParametersAlgorithmState {
    fn default() -> Self {
        Self::Nothing
    }
}

impl MetaParameters {
    pub fn updated_fmd_params(
        &self,
        old: &Parameters,
        state: MetaParametersAlgorithmState,
        height: u64,
        clue_count_delta: (u64, u64),
    ) -> (Parameters, MetaParametersAlgorithmState) {
        if clue_count_delta.1 < clue_count_delta.0 {
            tracing::warn!(
                "decreasing clue count at height {}: {} then {}",
                height,
                clue_count_delta.0,
                clue_count_delta.1
            );
        }
        match self.algorithm {
            MetaParametersAlgorithm::Fixed(precision) => (
                Parameters {
                    precision,
                    as_of_block_height: height,
                },
                MetaParametersAlgorithmState::Fixed,
            ),
            MetaParametersAlgorithm::SlidingWindow(w) => {
                w.updated_fmd_params(old, state, height, clue_count_delta)
            }
        }
    }
}