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
use ethereum_types::{Address, H256};
use std::{
    borrow::Borrow,
    collections::HashMap,
    hash::{Hash, Hasher},
};

use std::{cell::RefCell, rc::Rc};

// Implementation of a hasheable borrowed pair
trait KeyPair<A, B> {
    fn a(&self) -> &A;
    fn b(&self) -> &B;
}
impl<'a, A, B> Borrow<dyn KeyPair<A, B> + 'a> for (A, B)
where
    A: Eq + Hash + 'a,
    B: Eq + Hash + 'a,
{
    fn borrow(&self) -> &(dyn KeyPair<A, B> + 'a) {
        self
    }
}
impl<A: Hash, B: Hash> Hash for (dyn KeyPair<A, B> + '_) {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.a().hash(state);
        self.b().hash(state);
    }
}
impl<A: Eq, B: Eq> PartialEq for (dyn KeyPair<A, B> + '_) {
    fn eq(&self, other: &Self) -> bool {
        self.a() == other.a() && self.b() == other.b()
    }
}
impl<A: Eq, B: Eq> Eq for (dyn KeyPair<A, B> + '_) {}
impl<A, B> KeyPair<A, B> for (A, B) {
    fn a(&self) -> &A {
        &self.0
    }
    fn b(&self) -> &B {
        &self.1
    }
}
impl<A, B> KeyPair<A, B> for (&A, &B) {
    fn a(&self) -> &A {
        self.0
    }
    fn b(&self) -> &B {
        self.1
    }
}

#[derive(Debug)]
struct Journal {
    enabled: bool,
    last_id: usize,
    addresses: HashMap<Address, usize>,
    storage_keys: HashMap<(Address, H256), usize>,
}
#[derive(Debug)]
pub struct AccessList {
    id: usize,
    journal: Rc<RefCell<Journal>>,
}

impl Clone for AccessList {
    fn clone(&self) -> Self {
        let mut journal = self.journal.as_ref().borrow_mut();
        let id = journal.last_id + 1;
        journal.last_id = id;
        Self {
            id: id,
            journal: self.journal.clone(),
        }
    }
}

impl Default for AccessList {
    fn default() -> Self {
        AccessList::new(false)
    }
}

impl std::fmt::Display for AccessList {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let journal = self.journal.as_ref().borrow();
        for (addr, id) in journal.addresses.iter() {
            write!(f, "| ADDR {} -> {}\n", addr, id)?;
        }
        for ((addr, slot), id) in journal.storage_keys.iter() {
            write!(f, "| SLOT {}:{} -> {}\n", addr, slot, id)?;
        }
        Ok(())
    }
}

impl AccessList {
    /// Returns if the list is enabled
    pub fn new(enabled: bool) -> Self {
        let journal = Journal {
            enabled,
            last_id: 0,
            addresses: HashMap::new(),
            storage_keys: HashMap::new(),
        };
        Self {
            id: 0,
            journal: Rc::new(RefCell::new(journal)),
        }
    }

    /// Returns if the list is enabled
    pub fn is_enabled(&self) -> bool {
        let journal = self.journal.as_ref().borrow();
        journal.enabled
    }

    /// Enable the access list control
    pub fn enable(&mut self) {
        let mut journal = self.journal.as_ref().borrow_mut();
        journal.enabled = true;
    }

    /// Checks if contains an storage key
    pub fn contains_storage_key(&self, address: &Address, key: &H256) -> bool {
        let journal = self.journal.as_ref().borrow();
        if journal.enabled {
            journal
                .storage_keys
                .contains_key(&(address, key) as &dyn KeyPair<Address, H256>)
        } else {
            false
        }
    }

    /// Inserts a storage key
    pub fn insert_storage_key(&mut self, address: Address, key: H256) {
        let mut journal = self.journal.as_ref().borrow_mut();
        if journal.enabled
            && !journal
                .storage_keys
                .contains_key(&(address, key) as &dyn KeyPair<Address, H256>)
        {
            journal.storage_keys.insert((address, key), self.id);
        }
    }

    /// Checks if contains an address
    pub fn contains_address(&self, address: &Address) -> bool {
        let journal = self.journal.as_ref().borrow();
        if journal.enabled {
            journal.addresses.contains_key(&address)
        } else {
            false
        }
    }
    /// Inserts an address
    pub fn insert_address(&mut self, address: Address) {
        let mut journal = self.journal.as_ref().borrow_mut();
        if journal.enabled && !journal.addresses.contains_key(&address) {
            journal.addresses.insert(address, self.id);
        }
    }
    /// Removes all changes in journal
    pub fn rollback(&self) {
        let mut journal = self.journal.as_ref().borrow_mut();
        // `id < self.id` instead `id != self.if` is to take care about recursive calls
        journal.addresses.retain(|_, id| *id < self.id);
        journal.storage_keys.retain(|_, id| *id < self.id);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn default_accesslist_is_disabled() {
        let access_list = AccessList::default();
        assert_eq!(false, access_list.is_enabled());
    }

    #[test]
    fn default_disabled_accesslist_does_nothing() {
        let mut access_list = AccessList::default();
        access_list.insert_address(Address::from_low_u64_be(1));
        access_list.insert_storage_key(Address::from_low_u64_be(2), H256::from_low_u64_be(3));
        assert_eq!(
            false,
            access_list.contains_address(&Address::from_low_u64_be(1))
        );
        assert_eq!(
            false,
            access_list
                .contains_storage_key(&Address::from_low_u64_be(2), &H256::from_low_u64_be(3))
        );
    }

    #[test]
    fn default_enabled_accesslist_registers() {
        let mut access_list = AccessList::default();
        access_list.enable();
        assert_eq!(true, access_list.is_enabled());
        access_list.insert_address(Address::from_low_u64_be(1));
        access_list.insert_storage_key(Address::from_low_u64_be(2), H256::from_low_u64_be(3));
        assert_eq!(
            true,
            access_list.contains_address(&Address::from_low_u64_be(1))
        );
        assert_eq!(
            true,
            access_list
                .contains_storage_key(&Address::from_low_u64_be(2), &H256::from_low_u64_be(3))
        );
    }

    #[test]
    fn cloned_accesslist_registers_in_parent() {
        let mut access_list = AccessList::default();
        access_list.enable();
        assert_eq!(true, access_list.is_enabled());
        access_list.insert_address(Address::from_low_u64_be(1));
        access_list.insert_storage_key(Address::from_low_u64_be(2), H256::from_low_u64_be(3));

        let access_list_call = access_list.clone();
        assert_eq!(
            true,
            access_list_call.contains_address(&Address::from_low_u64_be(1))
        );
        assert_eq!(
            true,
            access_list_call
                .contains_storage_key(&Address::from_low_u64_be(2), &H256::from_low_u64_be(3))
        );
        access_list.insert_address(Address::from_low_u64_be(4));
        assert_eq!(
            true,
            access_list_call.contains_address(&Address::from_low_u64_be(4))
        );

        assert_eq!(
            true,
            access_list.contains_address(&Address::from_low_u64_be(4))
        );
    }
    #[test]
    fn cloned_accesslist_rollbacks_in_parent() {
        let mut access_list = AccessList::default();
        access_list.enable();
        assert_eq!(true, access_list.is_enabled());
        access_list.insert_address(Address::from_low_u64_be(1));
        access_list.insert_storage_key(Address::from_low_u64_be(2), H256::from_low_u64_be(3));

        let mut access_list_call = access_list.clone();
        access_list_call.insert_address(Address::from_low_u64_be(1));
        access_list_call.insert_storage_key(Address::from_low_u64_be(2), H256::from_low_u64_be(3));
        access_list_call.insert_address(Address::from_low_u64_be(4));

        let mut access_list_call_call = access_list.clone();
        access_list_call_call.insert_address(Address::from_low_u64_be(1));
        access_list_call_call
            .insert_storage_key(Address::from_low_u64_be(2), H256::from_low_u64_be(3));
        access_list_call_call.insert_address(Address::from_low_u64_be(5));
        access_list_call_call
            .insert_storage_key(Address::from_low_u64_be(6), H256::from_low_u64_be(7));

        access_list_call.rollback();

        assert_eq!(
            true,
            access_list.contains_address(&Address::from_low_u64_be(1))
        );
        assert_eq!(
            false,
            access_list.contains_address(&Address::from_low_u64_be(4))
        );
        assert_eq!(
            false,
            access_list.contains_address(&Address::from_low_u64_be(5))
        );
        assert_eq!(
            true,
            access_list
                .contains_storage_key(&Address::from_low_u64_be(2), &H256::from_low_u64_be(3))
        );
        assert_eq!(
            false,
            access_list
                .contains_storage_key(&Address::from_low_u64_be(6), &H256::from_low_u64_be(7))
        );
    }
}