brc20-prog 0.9.0

BRC20 programmable module - Smart contract execution engine compatible with BRC20 standard
Documentation
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use std::collections::{HashMap, HashSet};
use std::error::Error;
use std::hash::Hash;
use std::path::Path;

use rocksdb::{IteratorMode, Options, DB};

use crate::db::cached_database::BlockHistoryCache;
use crate::db::types::{Decode, Encode};

// Database to store data that is mapped to a block number with a history cache
//
// It uses a cache to store the data in memory and only writes to the database when commit is called
// It also supports reorg by reverting back the state to the latest valid block
//
// K: the type of the key
// V: the type of the value to store
// C: the type of the cache
// It should implement BlockHistoryCache<V> to store the history of the value
pub struct BlockCachedDatabase<K, V, C>
where
    K: Encode + Decode + Eq + Hash + Clone,
    V: Encode + Decode + Clone + Eq,
    C: BlockHistoryCache<V> + Encode + Decode + Clone,
{
    db: DB,
    cache_db: DB,
    cache: HashMap<K, C>,

    _phantom: std::marker::PhantomData<V>,
}

impl<K, V, C> BlockCachedDatabase<K, V, C>
where
    K: Encode + Decode + Eq + Hash + Clone,
    V: Encode + Decode + Eq + Clone,
    C: BlockHistoryCache<V> + Encode + Decode + Clone,
{
    /// Create a new BlockCachedDatabase
    ///
    /// It creates a new database if it does not exist
    ///
    /// path: &Path - the path to store the database
    /// name: &str - the name of the database
    ///
    /// Returns: BlockCachedDatabase<K, V, C> - the created BlockCachedDatabase
    pub fn new(path: &Path, name: &str) -> Result<Self, Box<dyn Error>> {
        let mut opts = Options::default();
        opts.create_if_missing(true);
        opts.set_max_open_files(256);
        let db = DB::open(&opts, &path.join(Path::new(name)))?;
        let cache_db = DB::open(&opts, &path.join(Path::new(&format!("{}_cache", name))))?;
        let cache = HashMap::new();
        Ok(Self {
            db,
            cache_db,
            cache,
            _phantom: std::marker::PhantomData,
        })
    }

    /// Get the value for a key
    ///
    /// It first checks the cache and then the database
    /// If the value is not found, it returns None
    /// If the value is found, it returns Some(value)
    ///
    /// key: &K - the key to get the value for
    /// Returns: Option<V> - the value for the key
    pub fn latest(&self, key: &K) -> Result<Option<V>, Box<dyn Error>> {
        if let Some(cache) = self.cache.get(key) {
            return Ok(cache.latest());
        }
        if let Some(value) = self.db.get(&key.encode_vec())? {
            let value = V::decode_vec(&value.to_vec())?;
            return Ok(Some(value));
        }
        return Ok(None);
    }

    /// Get the range of values between start_key and end_key
    ///
    /// It returns a list of key-value pairs between start_key and end_key
    ///
    /// This only works if keys can be compared in their encoded form
    ///
    /// start_key: &K - the start key
    /// end_key: &K - the end key, exclusive
    /// Returns: Vec<(K, V)> - the list of key-value pairs
    pub fn get_range(&self, start_key: &K, end_key: &K) -> Result<Vec<(K, V)>, Box<dyn Error>> {
        let mut kv_pairs = HashMap::new();
        let start_key_bytes = start_key.encode_vec();
        let end_key_bytes = end_key.encode_vec();

        for kv_pair in self.db.iterator(IteratorMode::From(
            &start_key_bytes,
            rocksdb::Direction::Forward,
        )) {
            let (key, value) = kv_pair?;
            if *key >= *end_key_bytes {
                break;
            }
            let key = K::decode_vec(&key.to_vec())?;
            let value = V::decode_vec(&value.to_vec())?;
            kv_pairs.insert(key, value);
        }

        for key in self.cache.keys() {
            let key_bytes = key.encode_vec();
            if *key_bytes < *start_key_bytes {
                continue;
            }
            if *key_bytes >= *end_key_bytes {
                break;
            }
            if let Some(cache) = self.cache.get(key) {
                if let Some(value) = cache.latest() {
                    kv_pairs.insert(key.clone(), value.clone());
                } else {
                    kv_pairs.remove(key);
                }
            }
        }

        Ok(kv_pairs.into_iter().collect())
    }

    /// Returns all keys and values in the database
    ///
    /// It returns a list of all key-value pairs in the database
    pub fn all(&self) -> Result<Vec<(K, V)>, Box<dyn Error>> {
        let mut kv_pairs: HashMap<K, V> = HashMap::new();

        for kv_pair in self.db.full_iterator(IteratorMode::Start) {
            let (key, value) = kv_pair?;
            let key = K::decode_vec(&key.to_vec())?;
            let value = V::decode_vec(&value.to_vec())?;
            if !kv_pairs.contains_key(&key) {
                kv_pairs.insert(key, value);
            }
        }

        for (key, cache) in &self.cache {
            if let Some(value) = cache.latest() {
                kv_pairs.insert(key.clone(), value.clone());
            } else {
                kv_pairs.remove(key);
            }
        }

        Ok(kv_pairs.into_iter().collect())
    }

    /// Set the value for a key
    ///
    /// It sets the value in the cache, it's not written to the database until commit is called
    ///
    /// block_number: U256 - the block number to set the value for
    /// key: K - the key to set the value for
    /// value: V - the value to set
    pub fn set(&mut self, block_number: u64, key: &K, value: V) -> Result<(), Box<dyn Error>> {
        let cache = self.retrieve_cache(&key)?;
        cache.set(block_number, value);
        Ok(())
    }

    /// Unset the value for a key
    ///
    /// It removes the value from the cache, it's not written to the database until commit is called
    /// block_number: U256 - the block number to unset the value for
    /// key: K - the key to unset the value for
    pub fn unset(&mut self, block_number: u64, key: &K) -> Result<(), Box<dyn Error>> {
        let cache = self.retrieve_cache(&key)?;
        cache.unset(block_number);
        Ok(())
    }

    /// Commit the cache to the database
    ///
    /// It writes all the values in the cache to the database
    /// It does not clear the cache
    ///
    /// block_number: U256 - the block number to commit at
    pub fn commit(&mut self, block_number: u64) -> Result<(), Box<dyn Error>> {
        for (key, cache) in self.cache.iter() {
            let key_bytes = key.encode_vec();
            let cache_bytes = cache.encode_vec();
            if cache.is_old(block_number) {
                self.cache_db.delete(&key_bytes)?;
            } else {
                self.cache_db.put(&key_bytes, &cache_bytes)?;
            }

            if let Some(value) = cache.latest() {
                self.db.put(&key_bytes, &value.encode_vec())?;
            } else {
                self.db.delete(&key_bytes)?;
            }
        }

        let keys_to_remove: Vec<K> = self
            .cache
            .iter()
            .filter(|(_, cache)| cache.is_old(block_number))
            .map(|(key, _)| key.clone())
            .collect();
        for key in keys_to_remove {
            self.cache.remove(&key);
        }

        self.clear_cache();
        Ok(())
    }

    /// Revert the state to the latest valid block
    ///
    /// It reverts the state of all the caches to the latest valid block
    /// It does not clear the cache
    ///
    /// latest_valid_block_number: U256 - the latest valid block number
    pub fn reorg(&mut self, latest_valid_block_number: u64) -> Result<(), Box<dyn Error>> {
        let mut keys = HashSet::new();
        {
            for kv_pair in self.cache_db.full_iterator(IteratorMode::Start) {
                keys.insert(K::decode_vec(&kv_pair?.0.to_vec())?);
            }
            for key in self.cache.keys() {
                keys.insert(key.clone());
            }
        }
        for key in keys {
            let cache = self.retrieve_cache(&key)?;
            cache.reorg(latest_valid_block_number);
        }
        self.commit(latest_valid_block_number)?;
        self.clear_cache();
        Ok(())
    }

    /// Clear the cache
    ///
    /// It clears the cache, make sure to call commit before clearing the cache to write the data to the database
    /// Otherwise the data will be lost
    pub fn clear_cache(&mut self) {
        self.cache.clear();
    }

    fn retrieve_cache(&mut self, key: &K) -> Result<&mut C, Box<dyn Error>> {
        if self.cache.contains_key(key) {
            // Do nothing, the cache is already in memory
        } else if let Some(cache_bytes) = self.cache_db.get(&key.encode_vec())? {
            let cache = C::decode_vec(&cache_bytes.to_vec())?;
            self.cache.insert(key.clone(), cache);
        } else {
            self.cache.insert(key.clone(), C::new(None));
        }
        Ok(self.cache.get_mut(key).ok_or("Cache not found")?)
    }
}

#[cfg(test)]
mod tests {
    use alloy::primitives::{Address, B256, U256};
    use revm_state::AccountInfo;
    use tempfile::TempDir;

    use super::*;
    use crate::db::cached_database::BlockHistoryCacheData;
    use crate::db::types::{AccountInfoED, AddressED};

    #[test]
    fn test_cache_only() {
        let path = TempDir::new().unwrap();
        let mut db = BlockCachedDatabase::<
            AddressED,
            AccountInfoED,
            BlockHistoryCacheData<AccountInfoED>,
        >::new(path.path(), "test_db")
        .unwrap();

        let address: Address = "0x1234567890123456789012345678901234567890"
            .parse()
            .unwrap();
        let account_info: AccountInfoED = AccountInfo {
            balance: U256::from(100),
            nonce: 1,
            code_hash: [1; 32].into(),
            code: None,
        }
        .into();
        let address_ed: AddressED = address.into();
        let _ = db.set(1, &address_ed.clone(), account_info.clone());

        let account_info = db.latest(&address_ed).unwrap().unwrap();
        assert_eq!(account_info.balance, 100u64.into());
        assert_eq!(account_info.nonce, 1u64.into());
        assert_eq!(account_info.code_hash, [1; 32].into());

        // verify the cache content
        let cache = db.cache.get(&address_ed).unwrap();
        assert_eq!(cache.latest().unwrap().balance, U256::from(100).into());
        assert_eq!(cache.latest().unwrap().nonce, 1u64.into());
        assert_eq!(cache.latest().unwrap().code_hash, [1; 32].into());
    }

    #[test]
    fn test_database_commit() {
        let path = TempDir::new().unwrap();
        let mut db = BlockCachedDatabase::<
            AddressED,
            AccountInfoED,
            BlockHistoryCacheData<AccountInfoED>,
        >::new(path.path(), "test_db")
        .unwrap();

        let address: Address = "0x1234567890123456789012345678901234567890"
            .parse()
            .unwrap();
        let address_ed: AddressED = address.into();
        let account_info_ed: AccountInfoED = AccountInfo {
            balance: U256::from(100),
            nonce: 1,
            code_hash: [1; 32].into(),
            code: None,
        }
        .into();
        let _ = db.set(1, &address.into(), account_info_ed.clone());

        let account_info = db.latest(&address.into()).unwrap().unwrap();
        assert_eq!(account_info.balance, U256::from(100).into());
        assert_eq!(account_info.nonce, 1u64.into());
        assert_eq!(account_info.code_hash, B256::from([1; 32]).into());

        db.commit(1).unwrap();

        let real_db = db.db;

        let account_info = real_db.get(address_ed.encode_vec()).unwrap();

        let account_info = AccountInfoED::decode_vec(&account_info.unwrap().to_vec()).unwrap();
        assert_eq!(account_info.balance, U256::from(100).into());
        assert_eq!(account_info.nonce, 1u64.into());
        assert_eq!(account_info.code_hash, B256::from([1; 32]).into());

        let cache_db = db.cache_db;

        let cache = cache_db.get(address_ed.encode_vec()).unwrap();

        let cache =
            BlockHistoryCacheData::<AccountInfoED>::decode_vec(&cache.unwrap().to_vec()).unwrap();
        assert_eq!(cache.latest().unwrap().balance, U256::from(100).into());
        assert_eq!(cache.latest().unwrap().nonce, 1u64.into());
        assert_eq!(
            cache.latest().unwrap().code_hash,
            B256::from([1; 32]).into()
        );
    }

    #[test]
    fn test_database_reorg() {
        let path = TempDir::new().unwrap();
        let mut db = BlockCachedDatabase::<
            AddressED,
            AccountInfoED,
            BlockHistoryCacheData<AccountInfoED>,
        >::new(path.path(), "test_db")
        .unwrap();

        let address: Address = "0x1234567890123456789012345678901234567890"
            .parse()
            .unwrap();
        let account_info: AccountInfoED = AccountInfo {
            balance: U256::from(100),
            nonce: 1,
            code_hash: [1; 32].into(),
            code: None,
        }
        .into();
        let address_ed: AddressED = address.into();
        let _ = db.set(1, &address_ed.clone(), account_info.clone());

        let account_info = db.latest(&address_ed).unwrap().unwrap();
        assert_eq!(account_info.balance, U256::from(100).into());
        assert_eq!(account_info.nonce, 1u64.into());
        assert_eq!(account_info.code_hash, B256::from([1; 32]).into());

        db.commit(1).unwrap();
        db.reorg(0).unwrap();
        db.clear_cache();

        let account_info = db.latest(&address_ed);
        assert!(account_info.unwrap().is_none());
    }

    #[test]
    fn test_database_reorg_10_blocks() {
        let path = TempDir::new().unwrap();
        let mut db = BlockCachedDatabase::<
            AddressED,
            AccountInfoED,
            BlockHistoryCacheData<AccountInfoED>,
        >::new(path.path(), "test_db")
        .unwrap();

        let address: Address = "0x1234567890123456789012345678901234567890"
            .parse()
            .unwrap();
        let address_ed: AddressED = address.into();

        for i in 1..=10 {
            let _ = db.set(
                i,
                &address_ed.clone(),
                AccountInfo {
                    balance: U256::from(100 + i),
                    nonce: i + 1,
                    code_hash: [1; 32].into(),
                    code: None,
                }
                .into(),
            );
        }
        db.commit(10).unwrap();

        db.reorg(5).unwrap();

        db.commit(5).unwrap();

        let account_info = db.latest(&address_ed).unwrap().unwrap();
        assert_eq!(account_info.balance, U256::from(100 + 5).into());
        assert_eq!(account_info.nonce, 6u64.into());
        assert_eq!(account_info.code_hash, B256::from([1; 32]).into());
    }
}