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
// 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/>.

//! Disk-backed, ref-counted `JournalDB` implementation.

use std::{
    collections::{BTreeMap, HashMap},
    io,
    sync::Arc,
};

use super::{traits::JournalDB, LATEST_ERA_KEY};
use bytes::Bytes;
use ethcore_db::{DBTransaction, DBValue, KeyValueDB};
use ethereum_types::H256;
use hash_db::HashDB;
use keccak_hasher::KeccakHasher;
use memory_db::MemoryDB;
use overlaydb::OverlayDB;
use parity_util_mem::{allocators::new_malloc_size_ops, MallocSizeOf};
use rlp::{decode, encode};
use util::{DatabaseKey, DatabaseValueRef, DatabaseValueView};
use DB_PREFIX_LEN;

/// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay
/// and latent-removal semantics.
///
/// Like `OverlayDB`, there is a memory overlay; `commit()` must be called in order to
/// write operations out to disk. Unlike `OverlayDB`, `remove()` operations do not take effect
/// immediately. Rather some age (based on a linear but arbitrary metric) must pass before
/// the removals actually take effect.
///
/// journal format:
/// ```text
/// [era, 0] => [ id, [insert_0, ...], [remove_0, ...] ]
/// [era, 1] => [ id, [insert_0, ...], [remove_0, ...] ]
/// [era, n] => [ ... ]
/// ```
///
/// when we make a new commit, we journal the inserts and removes.
/// for each `end_era` that we journaled that we are no passing by,
/// we remove all of its removes assuming it is canonical and all
/// of its inserts otherwise.
// TODO: store last_era, reclaim_period.
pub struct RefCountedDB {
    forward: OverlayDB,
    backing: Arc<dyn KeyValueDB>,
    latest_era: Option<u64>,
    inserts: Vec<H256>,
    removes: Vec<H256>,
    column: Option<u32>,
}

impl RefCountedDB {
    /// Create a new instance given a `backing` database.
    pub fn new(backing: Arc<dyn KeyValueDB>, column: Option<u32>) -> RefCountedDB {
        let latest_era = backing
            .get(column, &LATEST_ERA_KEY)
            .expect("Low-level database error.")
            .map(|v| decode::<u64>(&v).expect("decoding db value failed"));

        RefCountedDB {
            forward: OverlayDB::new(backing.clone(), column),
            backing,
            inserts: vec![],
            removes: vec![],
            latest_era,
            column,
        }
    }
}

impl HashDB<KeccakHasher, DBValue> for RefCountedDB {
    fn get(&self, key: &H256) -> Option<DBValue> {
        self.forward.get(key)
    }
    fn contains(&self, key: &H256) -> bool {
        self.forward.contains(key)
    }
    fn insert(&mut self, value: &[u8]) -> H256 {
        let r = self.forward.insert(value);
        self.inserts.push(r.clone());
        r
    }
    fn emplace(&mut self, key: H256, value: DBValue) {
        self.inserts.push(key.clone());
        self.forward.emplace(key, value);
    }
    fn remove(&mut self, key: &H256) {
        self.removes.push(key.clone());
    }
}

impl ::traits::KeyedHashDB for RefCountedDB {
    fn keys(&self) -> HashMap<H256, i32> {
        self.forward.keys()
    }
}

impl JournalDB for RefCountedDB {
    fn boxed_clone(&self) -> Box<dyn JournalDB> {
        Box::new(RefCountedDB {
            forward: self.forward.clone(),
            backing: self.backing.clone(),
            latest_era: self.latest_era,
            inserts: self.inserts.clone(),
            removes: self.removes.clone(),
            column: self.column.clone(),
        })
    }

    fn get_sizes(&self, sizes: &mut BTreeMap<String, usize>) {
        let mut ops = new_malloc_size_ops();
        sizes.insert(
            String::from("db_ref_counted_inserts"),
            self.inserts.size_of(&mut ops),
        );
        sizes.insert(
            String::from("db_ref_counted_removes"),
            self.removes.size_of(&mut ops),
        );
    }

    fn is_empty(&self) -> bool {
        self.latest_era.is_none()
    }

    fn backing(&self) -> &Arc<dyn KeyValueDB> {
        &self.backing
    }

    fn latest_era(&self) -> Option<u64> {
        self.latest_era
    }

    fn journal_under(&mut self, batch: &mut DBTransaction, now: u64, id: &H256) -> io::Result<u32> {
        // record new commit's details.
        let mut db_key = DatabaseKey {
            era: now,
            index: 0usize,
        };
        let mut last;

        while self
            .backing
            .get(self.column, {
                last = encode(&db_key);
                &last
            })?
            .is_some()
        {
            db_key.index += 1;
        }

        {
            let value_ref = DatabaseValueRef {
                id,
                inserts: &self.inserts,
                deletes: &self.removes,
            };

            batch.put(self.column, &last, &encode(&value_ref));
        }

        let ops = self.inserts.len() + self.removes.len();

        trace!(target: "rcdb", "new journal for time #{}.{} => {}: inserts={:?}, removes={:?}", now, db_key.index, id, self.inserts, self.removes);

        self.inserts.clear();
        self.removes.clear();

        if self.latest_era.map_or(true, |e| now > e) {
            batch.put(self.column, &LATEST_ERA_KEY, &encode(&now));
            self.latest_era = Some(now);
        }

        Ok(ops as u32)
    }

    fn mark_canonical(
        &mut self,
        batch: &mut DBTransaction,
        end_era: u64,
        canon_id: &H256,
    ) -> io::Result<u32> {
        // apply old commits' details
        let mut db_key = DatabaseKey {
            era: end_era,
            index: 0usize,
        };
        let mut last;
        while let Some(rlp_data) = {
            self.backing.get(self.column, {
                last = encode(&db_key);
                &last
            })?
        } {
            let view = DatabaseValueView::from_rlp(&rlp_data);
            let our_id = view.id().expect("rlp read from db; qed");
            let to_remove = if canon_id == &our_id {
                view.deletes()
            } else {
                view.inserts()
            }
            .expect("rlp read from db; qed");
            trace!(target: "rcdb", "delete journal for time #{}.{}=>{}, (canon was {}): deleting {:?}", end_era, db_key.index, our_id, canon_id, to_remove);
            for i in &to_remove {
                self.forward.remove(i);
            }
            batch.delete(self.column, &last);
            db_key.index += 1;
        }

        let r = self.forward.commit_to_batch(batch)?;
        Ok(r)
    }

    fn inject(&mut self, batch: &mut DBTransaction) -> io::Result<u32> {
        self.inserts.clear();
        for remove in self.removes.drain(..) {
            self.forward.remove(&remove);
        }
        self.forward.commit_to_batch(batch)
    }

    fn consolidate(&mut self, mut with: MemoryDB<KeccakHasher, DBValue>) {
        for (key, (value, rc)) in with.drain() {
            for _ in 0..rc {
                self.emplace(key, value.clone());
            }

            for _ in rc..0 {
                self.remove(&key);
            }
        }
    }

    fn state(&self, id: &H256) -> Option<Bytes> {
        self.backing
            .get_by_prefix(self.column, &id[0..DB_PREFIX_LEN])
            .map(|b| b.into_vec())
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    use hash_db::HashDB;
    use keccak::keccak;
    use JournalDB;

    fn new_db() -> RefCountedDB {
        let backing = Arc::new(ethcore_db::InMemoryWithMetrics::create(0));
        RefCountedDB::new(backing, None)
    }

    #[test]
    fn long_history() {
        // history is 3
        let mut jdb = new_db();
        let h = jdb.insert(b"foo");
        jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
        assert!(jdb.contains(&h));
        jdb.remove(&h);
        jdb.commit_batch(1, &keccak(b"1"), None).unwrap();
        assert!(jdb.contains(&h));
        jdb.commit_batch(2, &keccak(b"2"), None).unwrap();
        assert!(jdb.contains(&h));
        jdb.commit_batch(3, &keccak(b"3"), Some((0, keccak(b"0"))))
            .unwrap();
        assert!(jdb.contains(&h));
        jdb.commit_batch(4, &keccak(b"4"), Some((1, keccak(b"1"))))
            .unwrap();
        assert!(!jdb.contains(&h));
    }

    #[test]
    fn latest_era_should_work() {
        // history is 3
        let mut jdb = new_db();
        assert_eq!(jdb.latest_era(), None);
        let h = jdb.insert(b"foo");
        jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
        assert_eq!(jdb.latest_era(), Some(0));
        jdb.remove(&h);
        jdb.commit_batch(1, &keccak(b"1"), None).unwrap();
        assert_eq!(jdb.latest_era(), Some(1));
        jdb.commit_batch(2, &keccak(b"2"), None).unwrap();
        assert_eq!(jdb.latest_era(), Some(2));
        jdb.commit_batch(3, &keccak(b"3"), Some((0, keccak(b"0"))))
            .unwrap();
        assert_eq!(jdb.latest_era(), Some(3));
        jdb.commit_batch(4, &keccak(b"4"), Some((1, keccak(b"1"))))
            .unwrap();
        assert_eq!(jdb.latest_era(), Some(4));
    }

    #[test]
    fn complex() {
        // history is 1
        let mut jdb = new_db();

        let foo = jdb.insert(b"foo");
        let bar = jdb.insert(b"bar");
        jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
        assert!(jdb.contains(&foo));
        assert!(jdb.contains(&bar));

        jdb.remove(&foo);
        jdb.remove(&bar);
        let baz = jdb.insert(b"baz");
        jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0"))))
            .unwrap();
        assert!(jdb.contains(&foo));
        assert!(jdb.contains(&bar));
        assert!(jdb.contains(&baz));

        let foo = jdb.insert(b"foo");
        jdb.remove(&baz);
        jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1"))))
            .unwrap();
        assert!(jdb.contains(&foo));
        assert!(!jdb.contains(&bar));
        assert!(jdb.contains(&baz));

        jdb.remove(&foo);
        jdb.commit_batch(3, &keccak(b"3"), Some((2, keccak(b"2"))))
            .unwrap();
        assert!(jdb.contains(&foo));
        assert!(!jdb.contains(&bar));
        assert!(!jdb.contains(&baz));

        jdb.commit_batch(4, &keccak(b"4"), Some((3, keccak(b"3"))))
            .unwrap();
        assert!(!jdb.contains(&foo));
        assert!(!jdb.contains(&bar));
        assert!(!jdb.contains(&baz));
    }

    #[test]
    fn fork() {
        // history is 1
        let mut jdb = new_db();

        let foo = jdb.insert(b"foo");
        let bar = jdb.insert(b"bar");
        jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
        assert!(jdb.contains(&foo));
        assert!(jdb.contains(&bar));

        jdb.remove(&foo);
        let baz = jdb.insert(b"baz");
        jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0"))))
            .unwrap();

        jdb.remove(&bar);
        jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0"))))
            .unwrap();

        assert!(jdb.contains(&foo));
        assert!(jdb.contains(&bar));
        assert!(jdb.contains(&baz));

        jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b"))))
            .unwrap();
        assert!(jdb.contains(&foo));
        assert!(!jdb.contains(&baz));
        assert!(!jdb.contains(&bar));
    }

    #[test]
    fn inject() {
        let mut jdb = new_db();
        let key = jdb.insert(b"dog");
        jdb.inject_batch().unwrap();

        assert_eq!(jdb.get(&key).unwrap(), DBValue::from_slice(b"dog"));
        jdb.remove(&key);
        jdb.inject_batch().unwrap();

        assert!(jdb.get(&key).is_none());
    }
}