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
//! Rate limiting data types

use holochain_serialized_bytes::prelude::*;

use crate::{Create, CreateLink, Delete, Entry, Update};

/// Input to the `weigh` callback. Includes an "unweighed" action, and Entry
/// if applicable.
#[derive(Clone, PartialEq, Serialize, Deserialize, SerializedBytes, Debug)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum WeighInput {
    /// A Link to be weighed
    Link(CreateLink<()>),
    /// A new Entry to be weighed
    Create(Create<()>, Entry),
    /// An updated Entry to be weighed
    Update(Update<()>, Entry),
    /// An Entry deletion to be weighed
    Delete(Delete<()>),
}

/// A bucket ID, for rate limiting
pub type RateBucketId = u8;

/// The weight of this action, for rate limiting
pub type RateUnits = u8;

/// The normalized total size of this action, for rate limiting
pub type RateBytes = u8;

/// The amount that a bucket is "filled"
pub type RateBucketCapacity = u32;

/// Combination of two rate limiting data types, for convenience
#[derive(
    Debug,
    Clone,
    serde::Serialize,
    serde::Deserialize,
    PartialEq,
    Eq,
    SerializedBytes,
    Hash,
    PartialOrd,
    Ord,
)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[allow(missing_docs)]
pub struct RateWeight {
    pub bucket_id: RateBucketId,
    pub units: RateUnits,
}

impl Default for RateWeight {
    fn default() -> Self {
        Self {
            bucket_id: 255,
            units: 0,
        }
    }
}

/// Combination of the three main rate limiting data types, for convenience
#[derive(
    Debug,
    Clone,
    serde::Serialize,
    serde::Deserialize,
    PartialEq,
    Eq,
    SerializedBytes,
    Hash,
    PartialOrd,
    Ord,
)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[allow(missing_docs)]
pub struct EntryRateWeight {
    pub bucket_id: RateBucketId,
    pub units: RateUnits,
    pub rate_bytes: RateBytes,
}

impl Default for EntryRateWeight {
    fn default() -> Self {
        Self {
            bucket_id: 255,
            units: 0,
            rate_bytes: 0,
        }
    }
}

impl From<EntryRateWeight> for RateWeight {
    fn from(w: EntryRateWeight) -> Self {
        Self {
            bucket_id: w.bucket_id,
            units: w.units,
        }
    }
}