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
use casper_execution_engine::shared::{additive_map::AdditiveMap, transform::Transform};
use casper_types::Key;

/// Represents the difference between two `AdditiveMap`s.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct AdditiveMapDiff {
    left: AdditiveMap<Key, Transform>,
    both: AdditiveMap<Key, Transform>,
    right: AdditiveMap<Key, Transform>,
}

impl AdditiveMapDiff {
    /// Creates a diff from two `AdditiveMap`s.
    pub fn new(
        mut left: AdditiveMap<Key, Transform>,
        mut right: AdditiveMap<Key, Transform>,
    ) -> Self {
        let mut both = AdditiveMap::new();
        for key in left.keys().copied().collect::<Vec<_>>() {
            // Safe to unwrap here since we're iterating `left` keys, so `left.remove` must succeed.
            let left_value = left.remove(&key).unwrap();
            if let Some(right_value) = right.remove(&key) {
                if left_value == right_value {
                    both.insert(key, left_value);
                } else {
                    left.insert(key, left_value);
                    right.insert(key, right_value);
                }
            } else {
                left.insert(key, left_value);
            }
        }

        AdditiveMapDiff { left, both, right }
    }

    /// Returns the entries that are unique to the `left` input.
    pub fn left(&self) -> &AdditiveMap<Key, Transform> {
        &self.left
    }

    /// Returns the entries that are unique to the `right` input.
    pub fn right(&self) -> &AdditiveMap<Key, Transform> {
        &self.right
    }

    /// Returns the entries shared by both inputs.
    pub fn both(&self) -> &AdditiveMap<Key, Transform> {
        &self.both
    }
}

#[cfg(test)]
mod tests {
    use once_cell::sync::Lazy;
    use rand::{self, Rng};

    use casper_types::{AccessRights, Key, URef, BLAKE2B_DIGEST_LENGTH};

    use super::*;

    const MIN_ELEMENTS: u8 = 1;
    const MAX_ELEMENTS: u8 = 10;

    static LEFT_ONLY: Lazy<AdditiveMap<Key, Transform>> = Lazy::new(|| {
        let mut map = AdditiveMap::new();
        for i in 0..random_element_count() {
            map.insert(
                Key::URef(URef::new(
                    [i; BLAKE2B_DIGEST_LENGTH],
                    AccessRights::READ_ADD_WRITE,
                )),
                Transform::AddInt32(i.into()),
            );
        }
        map
    });
    static BOTH: Lazy<AdditiveMap<Key, Transform>> = Lazy::new(|| {
        let mut map = AdditiveMap::new();
        for i in 0..random_element_count() {
            map.insert(
                Key::URef(URef::new(
                    [i + MAX_ELEMENTS; BLAKE2B_DIGEST_LENGTH],
                    AccessRights::READ_ADD_WRITE,
                )),
                Transform::Identity,
            );
        }
        map
    });
    static RIGHT_ONLY: Lazy<AdditiveMap<Key, Transform>> = Lazy::new(|| {
        let mut map = AdditiveMap::new();
        for i in 0..random_element_count() {
            map.insert(
                Key::URef(URef::new(
                    [i; BLAKE2B_DIGEST_LENGTH],
                    AccessRights::READ_ADD_WRITE,
                )),
                Transform::AddUInt512(i.into()),
            );
        }
        map
    });

    fn random_element_count() -> u8 {
        rand::thread_rng().gen_range(MIN_ELEMENTS..=MAX_ELEMENTS)
    }

    struct TestFixture {
        expected: AdditiveMapDiff,
    }

    impl TestFixture {
        fn new(
            left_only: AdditiveMap<Key, Transform>,
            both: AdditiveMap<Key, Transform>,
            right_only: AdditiveMap<Key, Transform>,
        ) -> Self {
            TestFixture {
                expected: AdditiveMapDiff {
                    left: left_only,
                    both,
                    right: right_only,
                },
            }
        }

        fn left(&self) -> AdditiveMap<Key, Transform> {
            self.expected
                .left
                .iter()
                .chain(self.expected.both.iter())
                .map(|(key, transform)| (*key, transform.clone()))
                .collect()
        }

        fn right(&self) -> AdditiveMap<Key, Transform> {
            self.expected
                .right
                .iter()
                .chain(self.expected.both.iter())
                .map(|(key, transform)| (*key, transform.clone()))
                .collect()
        }

        fn run(&self) {
            let diff = AdditiveMapDiff::new(self.left(), self.right());
            assert_eq!(self.expected, diff);
        }
    }

    #[test]
    fn should_create_diff_where_left_is_subset_of_right() {
        let fixture = TestFixture::new(AdditiveMap::new(), BOTH.clone(), RIGHT_ONLY.clone());
        fixture.run();
    }

    #[test]
    fn should_create_diff_where_right_is_subset_of_left() {
        let fixture = TestFixture::new(LEFT_ONLY.clone(), BOTH.clone(), AdditiveMap::new());
        fixture.run();
    }

    #[test]
    fn should_create_diff_where_no_intersection() {
        let fixture = TestFixture::new(LEFT_ONLY.clone(), AdditiveMap::new(), RIGHT_ONLY.clone());
        fixture.run();
    }

    #[test]
    fn should_create_diff_where_both_equal() {
        let fixture = TestFixture::new(AdditiveMap::new(), BOTH.clone(), AdditiveMap::new());
        fixture.run();
    }

    #[test]
    fn should_create_diff_where_left_is_empty() {
        let fixture = TestFixture::new(AdditiveMap::new(), AdditiveMap::new(), RIGHT_ONLY.clone());
        fixture.run();
    }

    #[test]
    fn should_create_diff_where_right_is_empty() {
        let fixture = TestFixture::new(LEFT_ONLY.clone(), AdditiveMap::new(), AdditiveMap::new());
        fixture.run();
    }

    #[test]
    fn should_create_diff_where_both_are_empty() {
        let fixture = TestFixture::new(AdditiveMap::new(), AdditiveMap::new(), AdditiveMap::new());
        fixture.run();
    }
}