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
use crate::transaction_pool::transaction_pool_inner::TX_POOL_GET_STATE_TIMER;
use cfx_executor::state::State;
use cfx_statedb::Result as DbResult;
use cfx_types::{Address, AddressWithSpace, U256};
use metrics::MeterTimer;
use primitives::SponsorInfo;
use std::sync::Arc;

// TODO: perhaps rename to StateWrapper.
pub struct AccountCache {
    state: Arc<State>,
}

impl AccountCache {
    pub fn new(state: Arc<State>) -> Self { AccountCache { state } }

    pub fn get_nonce_and_balance(
        &self, address: &AddressWithSpace,
    ) -> DbResult<(U256, U256)> {
        let _timer = MeterTimer::time_func(TX_POOL_GET_STATE_TIMER.as_ref());
        Ok((self.state.nonce(address)?, self.state.balance(address)?))
    }

    pub fn get_nonce(&self, address: &AddressWithSpace) -> DbResult<U256> {
        self.state.nonce(address)
    }

    pub fn get_sponsor_info(
        &self, contract_address: &Address,
    ) -> DbResult<Option<SponsorInfo>> {
        self.state.sponsor_info(contract_address)
    }

    pub fn check_commission_privilege(
        &self, contract_address: &Address, user: &Address,
    ) -> DbResult<bool> {
        self.state.check_contract_whitelist(contract_address, user)
    }
}