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
use crate::Account;
use hashbrown::HashMap as Map;
use primitive_types::{H160, H256, U256};

use crate::AccountInfo;
use auto_impl::auto_impl;
use bytes::Bytes;

#[auto_impl(& mut, Box)]
pub trait Database {
    /// Get basic account information.
    fn basic(&mut self, address: H160) -> AccountInfo;
    /// Get account code by its hash
    fn code_by_hash(&mut self, code_hash: H256) -> Bytes;
    /// Get storage value of address at index.
    fn storage(&mut self, address: H160, index: U256) -> U256;

    // History related
    fn block_hash(&mut self, number: U256) -> H256;
}

#[auto_impl(& mut, Box)]
pub trait DatabaseCommit {
    fn commit(&mut self, changes: Map<H160, Account>);
}

#[auto_impl(&, Box)]
pub trait DatabaseRef {
    /// Whether account at address exists.
    //fn exists(&self, address: H160) -> Option<AccountInfo>;
    /// Get basic account information.
    fn basic(&self, address: H160) -> AccountInfo;
    /// Get account code by its hash
    fn code_by_hash(&self, code_hash: H256) -> Bytes;
    /// Get storage value of address at index.
    fn storage(&self, address: H160, index: U256) -> U256;

    // History related
    fn block_hash(&self, number: U256) -> H256;
}

pub struct RefDBWrapper<'a> {
    pub db: &'a dyn DatabaseRef,
}

impl<'a> RefDBWrapper<'a> {
    pub fn new(db: &'a dyn DatabaseRef) -> Self {
        Self { db }
    }
}

impl<'a> Database for RefDBWrapper<'a> {
    /// Whether account at address exists.
    // fn exists(&mut self, address: H160) -> Option<AccountInfo> {
    //     self.db.exists(address)
    // }
    /// Get basic account information.
    fn basic(&mut self, address: H160) -> AccountInfo {
        self.db.basic(address)
    }
    /// Get account code by its hash
    fn code_by_hash(&mut self, code_hash: H256) -> Bytes {
        self.db.code_by_hash(code_hash)
    }
    /// Get storage value of address at index.
    fn storage(&mut self, address: H160, index: U256) -> U256 {
        self.db.storage(address, index)
    }

    // History related
    fn block_hash(&mut self, number: U256) -> H256 {
        self.db.block_hash(number)
    }
}