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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.

// OpenEthereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// OpenEthereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with OpenEthereum.  If not, see <http://www.gnu.org/licenses/>.

//! Database utilities and definitions.

use kvdb::DBTransaction;
use kvdb_rocksdb::Database;
use parking_lot::RwLock;
use stats::{PrometheusMetrics, PrometheusRegistry};
use std::{collections::HashMap, hash::Hash, io::Read};

use rlp;

// database columns
/// Column for State
pub const COL_STATE: Option<u32> = Some(0);
/// Column for Block headers
pub const COL_HEADERS: Option<u32> = Some(1);
/// Column for Block bodies
pub const COL_BODIES: Option<u32> = Some(2);
/// Column for Extras
pub const COL_EXTRA: Option<u32> = Some(3);
/// Column for Traces
pub const COL_TRACE: Option<u32> = Some(4);
/// Column for the accounts existence bloom filter.
#[deprecated(since = "3.0.0", note = "Accounts bloom column is deprecated")]
pub const COL_ACCOUNT_BLOOM: Option<u32> = Some(5);
/// Column for general information from the local node which can persist.
pub const COL_NODE_INFO: Option<u32> = Some(6);
/// Number of columns in DB
pub const NUM_COLUMNS: Option<u32> = Some(7);

/// Modes for updating caches.
#[derive(Clone, Copy)]
pub enum CacheUpdatePolicy {
    /// Overwrite entries.
    Overwrite,
    /// Remove entries.
    Remove,
}

/// A cache for arbitrary key-value pairs.
pub trait Cache<K, V> {
    /// Insert an entry into the cache and get the old value.
    fn insert(&mut self, k: K, v: V) -> Option<V>;

    /// Remove an entry from the cache, getting the old value if it existed.
    fn remove(&mut self, k: &K) -> Option<V>;

    /// Query the cache for a key's associated value.
    fn get(&self, k: &K) -> Option<&V>;
}

impl<K, V> Cache<K, V> for HashMap<K, V>
where
    K: Hash + Eq,
{
    fn insert(&mut self, k: K, v: V) -> Option<V> {
        HashMap::insert(self, k, v)
    }

    fn remove(&mut self, k: &K) -> Option<V> {
        HashMap::remove(self, k)
    }

    fn get(&self, k: &K) -> Option<&V> {
        HashMap::get(self, k)
    }
}

/// Should be used to get database key associated with given value.
pub trait Key<T> {
    /// The db key associated with this value.
    type Target: AsRef<[u8]>;

    /// Returns db key.
    fn key(&self) -> Self::Target;
}

/// Should be used to write value into database.
pub trait Writable {
    /// Writes the value into the database.
    fn write<T, R>(&mut self, col: Option<u32>, key: &dyn Key<T, Target = R>, value: &T)
    where
        T: rlp::Encodable,
        R: AsRef<[u8]>;

    /// Deletes key from the databse.
    fn delete<T, R>(&mut self, col: Option<u32>, key: &dyn Key<T, Target = R>)
    where
        T: rlp::Encodable,
        R: AsRef<[u8]>;

    /// Writes the value into the database and updates the cache.
    fn write_with_cache<K, T, R>(
        &mut self,
        col: Option<u32>,
        cache: &mut dyn Cache<K, T>,
        key: K,
        value: T,
        policy: CacheUpdatePolicy,
    ) where
        K: Key<T, Target = R> + Hash + Eq,
        T: rlp::Encodable,
        R: AsRef<[u8]>,
    {
        self.write(col, &key, &value);
        match policy {
            CacheUpdatePolicy::Overwrite => {
                cache.insert(key, value);
            }
            CacheUpdatePolicy::Remove => {
                cache.remove(&key);
            }
        }
    }

    /// Writes the values into the database and updates the cache.
    fn extend_with_cache<K, T, R>(
        &mut self,
        col: Option<u32>,
        cache: &mut dyn Cache<K, T>,
        values: HashMap<K, T>,
        policy: CacheUpdatePolicy,
    ) where
        K: Key<T, Target = R> + Hash + Eq,
        T: rlp::Encodable,
        R: AsRef<[u8]>,
    {
        match policy {
            CacheUpdatePolicy::Overwrite => {
                for (key, value) in values {
                    self.write(col, &key, &value);
                    cache.insert(key, value);
                }
            }
            CacheUpdatePolicy::Remove => {
                for (key, value) in &values {
                    self.write(col, key, value);
                    cache.remove(key);
                }
            }
        }
    }

    /// Writes and removes the values into the database and updates the cache.
    fn extend_with_option_cache<K, T, R>(
        &mut self,
        col: Option<u32>,
        cache: &mut dyn Cache<K, Option<T>>,
        values: HashMap<K, Option<T>>,
        policy: CacheUpdatePolicy,
    ) where
        K: Key<T, Target = R> + Hash + Eq,
        T: rlp::Encodable,
        R: AsRef<[u8]>,
    {
        match policy {
            CacheUpdatePolicy::Overwrite => {
                for (key, value) in values {
                    match value {
                        Some(ref v) => self.write(col, &key, v),
                        None => self.delete(col, &key),
                    }
                    cache.insert(key, value);
                }
            }
            CacheUpdatePolicy::Remove => {
                for (key, value) in values {
                    match value {
                        Some(v) => self.write(col, &key, &v),
                        None => self.delete(col, &key),
                    }
                    cache.remove(&key);
                }
            }
        }
    }
}

/// Should be used to read values from database.
pub trait Readable {
    /// Returns value for given key.
    fn read<T, R>(&self, col: Option<u32>, key: &dyn Key<T, Target = R>) -> Option<T>
    where
        T: rlp::Decodable,
        R: AsRef<[u8]>;

    /// Returns value for given key either in cache or in database.
    fn read_with_cache<K, T, C>(&self, col: Option<u32>, cache: &RwLock<C>, key: &K) -> Option<T>
    where
        K: Key<T> + Eq + Hash + Clone,
        T: Clone + rlp::Decodable,
        C: Cache<K, T>,
    {
        {
            let read = cache.read();
            if let Some(v) = read.get(key) {
                return Some(v.clone());
            }
        }

        self.read(col, key).map(|value: T| {
            let mut write = cache.write();
            write.insert(key.clone(), value.clone());
            value
        })
    }

    /// Returns value for given key either in two-layered cache or in database.
    fn read_with_two_layer_cache<K, T, C>(
        &self,
        col: Option<u32>,
        l1_cache: &RwLock<C>,
        l2_cache: &RwLock<C>,
        key: &K,
    ) -> Option<T>
    where
        K: Key<T> + Eq + Hash + Clone,
        T: Clone + rlp::Decodable,
        C: Cache<K, T>,
    {
        {
            let read = l1_cache.read();
            if let Some(v) = read.get(key) {
                return Some(v.clone());
            }
        }

        self.read_with_cache(col, l2_cache, key)
    }

    /// Returns true if given value exists.
    fn exists<T, R>(&self, col: Option<u32>, key: &dyn Key<T, Target = R>) -> bool
    where
        R: AsRef<[u8]>;

    /// Returns true if given value exists either in cache or in database.
    fn exists_with_cache<K, T, R, C>(&self, col: Option<u32>, cache: &RwLock<C>, key: &K) -> bool
    where
        K: Eq + Hash + Key<T, Target = R>,
        R: AsRef<[u8]>,
        C: Cache<K, T>,
    {
        {
            let read = cache.read();
            if read.get(key).is_some() {
                return true;
            }
        }

        self.exists::<T, R>(col, key)
    }
}

impl Writable for DBTransaction {
    fn write<T, R>(&mut self, col: Option<u32>, key: &dyn Key<T, Target = R>, value: &T)
    where
        T: rlp::Encodable,
        R: AsRef<[u8]>,
    {
        self.put(col, key.key().as_ref(), &rlp::encode(value));
    }

    fn delete<T, R>(&mut self, col: Option<u32>, key: &dyn Key<T, Target = R>)
    where
        T: rlp::Encodable,
        R: AsRef<[u8]>,
    {
        self.delete(col, key.key().as_ref());
    }
}

impl<KVDB: kvdb::KeyValueDB + ?Sized> Readable for KVDB {
    fn read<T, R>(&self, col: Option<u32>, key: &dyn Key<T, Target = R>) -> Option<T>
    where
        T: rlp::Decodable,
        R: AsRef<[u8]>,
    {
        self.get(col, key.key().as_ref())
            .expect(&format!("db get failed, key: {:?}", key.key().as_ref()))
            .map(|v| rlp::decode(&v).expect("decode db value failed"))
    }

    fn exists<T, R>(&self, col: Option<u32>, key: &dyn Key<T, Target = R>) -> bool
    where
        R: AsRef<[u8]>,
    {
        let result = self.get(col, key.key().as_ref());

        match result {
            Ok(v) => v.is_some(),
            Err(err) => {
                panic!(
                    "db get failed, key: {:?}, err: {:?}",
                    key.key().as_ref(),
                    err
                );
            }
        }
    }
}

/// Database with enabled statistics
pub struct DatabaseWithMetrics {
    db: Database,
    reads: std::sync::atomic::AtomicI64,
    writes: std::sync::atomic::AtomicI64,
    bytes_read: std::sync::atomic::AtomicI64,
    bytes_written: std::sync::atomic::AtomicI64,
}

impl DatabaseWithMetrics {
    /// Create a new instance
    pub fn new(db: Database) -> Self {
        Self {
            db,
            reads: std::sync::atomic::AtomicI64::new(0),
            writes: std::sync::atomic::AtomicI64::new(0),
            bytes_read: std::sync::atomic::AtomicI64::new(0),
            bytes_written: std::sync::atomic::AtomicI64::new(0),
        }
    }
}

/// Ethcore definition of a KeyValueDB with embeeded metrics
pub trait KeyValueDB: kvdb::KeyValueDB + PrometheusMetrics {}

impl kvdb::KeyValueDB for DatabaseWithMetrics {
    fn get(&self, col: Option<u32>, key: &[u8]) -> std::io::Result<Option<kvdb::DBValue>> {
        let res = self.db.get(col, key);
        let count = res
            .as_ref()
            .map_or(0, |y| y.as_ref().map_or(0, |x| x.bytes().count()));

        self.reads
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        self.bytes_read
            .fetch_add(count as i64, std::sync::atomic::Ordering::Relaxed);

        res
    }
    fn get_by_prefix(&self, col: Option<u32>, prefix: &[u8]) -> Option<Box<[u8]>> {
        let res = self.db.get_by_prefix(col, prefix);
        let count = res.as_ref().map_or(0, |x| x.bytes().count());

        self.reads
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        self.bytes_read
            .fetch_add(count as i64, std::sync::atomic::Ordering::Relaxed);

        res
    }
    fn write_buffered(&self, transaction: DBTransaction) {
        let mut count = 0;
        for op in &transaction.ops {
            count += match op {
                kvdb::DBOp::Insert { value, .. } => value.bytes().count(),
                _ => 0,
            };
        }

        self.writes.fetch_add(
            transaction.ops.len() as i64,
            std::sync::atomic::Ordering::Relaxed,
        );
        self.bytes_written
            .fetch_add(count as i64, std::sync::atomic::Ordering::Relaxed);

        self.db.write_buffered(transaction)
    }
    fn write(&self, transaction: DBTransaction) -> std::io::Result<()> {
        let mut count = 0;
        for op in &transaction.ops {
            count += match op {
                kvdb::DBOp::Insert { value, .. } => value.bytes().count(),
                _ => 0,
            };
        }

        self.bytes_written
            .fetch_add(count as i64, std::sync::atomic::Ordering::Relaxed);
        self.writes.fetch_add(
            transaction.ops.len() as i64,
            std::sync::atomic::Ordering::Relaxed,
        );
        self.db.write(transaction)
    }
    fn flush(&self) -> std::io::Result<()> {
        self.db.flush()
    }

    fn iter<'a>(
        &'a self,
        col: Option<u32>,
    ) -> Box<(dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + 'a)> {
        kvdb::KeyValueDB::iter(&self.db, col)
    }

    fn iter_from_prefix<'a>(
        &'a self,
        col: Option<u32>,
        prefix: &'a [u8],
    ) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + 'a> {
        self.db.iter_from_prefix(col, prefix)
    }

    fn restore(&self, new_db: &str) -> std::io::Result<()> {
        self.db.restore(new_db)
    }
}

impl KeyValueDB for DatabaseWithMetrics {}

impl PrometheusMetrics for DatabaseWithMetrics {
    fn prometheus_metrics(&self, p: &mut PrometheusRegistry) {
        p.register_counter(
            "kvdb_reads",
            "db reads",
            self.reads.load(std::sync::atomic::Ordering::Relaxed) as i64,
        );
        p.register_counter(
            "kvdb_writes",
            "db writes",
            self.writes.load(std::sync::atomic::Ordering::Relaxed) as i64,
        );
        p.register_counter(
            "kvdb_bytes_read",
            "db bytes_reads",
            self.bytes_read.load(std::sync::atomic::Ordering::Relaxed) as i64,
        );
        p.register_counter(
            "kvdb_bytes_written",
            "db bytes_written",
            self.bytes_written
                .load(std::sync::atomic::Ordering::Relaxed) as i64,
        );
    }
}

/// InMemory with disabled statistics
pub struct InMemoryWithMetrics {
    db: kvdb_memorydb::InMemory,
}

impl kvdb::KeyValueDB for InMemoryWithMetrics {
    fn get(&self, col: Option<u32>, key: &[u8]) -> std::io::Result<Option<kvdb::DBValue>> {
        self.db.get(col, key)
    }
    fn get_by_prefix(&self, col: Option<u32>, prefix: &[u8]) -> Option<Box<[u8]>> {
        self.db.get_by_prefix(col, prefix)
    }
    fn write_buffered(&self, transaction: DBTransaction) {
        self.db.write_buffered(transaction)
    }
    fn write(&self, transaction: DBTransaction) -> std::io::Result<()> {
        self.db.write(transaction)
    }
    fn flush(&self) -> std::io::Result<()> {
        self.db.flush()
    }

    fn iter<'a>(
        &'a self,
        col: Option<u32>,
    ) -> Box<(dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + 'a)> {
        kvdb::KeyValueDB::iter(&self.db, col)
    }

    fn iter_from_prefix<'a>(
        &'a self,
        col: Option<u32>,
        prefix: &'a [u8],
    ) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)> + 'a> {
        self.db.iter_from_prefix(col, prefix)
    }

    fn restore(&self, new_db: &str) -> std::io::Result<()> {
        self.db.restore(new_db)
    }
}

impl PrometheusMetrics for InMemoryWithMetrics {
    fn prometheus_metrics(&self, _: &mut PrometheusRegistry) {}
}

impl KeyValueDB for InMemoryWithMetrics {}

impl InMemoryWithMetrics {
    /// Create new instance
    pub fn create(num_cols: u32) -> Self {
        Self {
            db: kvdb_memorydb::create(num_cols),
        }
    }
}