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
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

mod storage_interface;

pub use crate::storage_interface::DBDebuggerInterface;

use anyhow::{anyhow, Result};
use aptos_state_view::StateView;
use aptos_types::{
    account_address::AccountAddress,
    account_config,
    account_state::AccountState,
    account_view::AccountView,
    contract_event::EventWithProof,
    event::EventKey,
    on_chain_config::ValidatorSet,
    state_store::{state_key::StateKey, state_value::StateValue},
    transaction::{Transaction, Version},
};
use move_deps::move_binary_format::file_format::CompiledModule;

// TODO(skedia) Clean up this interfact to remove account specific logic and move to state store
// key-value interface with fine grained storage project
pub trait AptosValidatorInterface: Sync {
    fn get_account_state_by_version(
        &self,
        account: AccountAddress,
        version: Version,
    ) -> Result<Option<AccountState>>;

    fn get_state_value_by_version(
        &self,
        state_key: &StateKey,
        version: Version,
    ) -> Result<Option<StateValue>>;

    fn get_events(&self, key: &EventKey, start_seq: u64, limit: u64)
        -> Result<Vec<EventWithProof>>;

    fn get_committed_transactions(&self, start: Version, limit: u64) -> Result<Vec<Transaction>>;

    fn get_latest_version(&self) -> Result<Version>;

    fn get_version_by_account_sequence(
        &self,
        account: AccountAddress,
        seq: u64,
    ) -> Result<Option<Version>>;

    fn get_framework_modules_by_version(&self, version: Version) -> Result<Vec<CompiledModule>> {
        let mut acc = vec![];
        for module_bytes in self
            .get_account_state_by_version(account_config::CORE_CODE_ADDRESS, version)?
            .ok_or_else(|| anyhow!("Failure reading aptos root address state"))?
            .get_modules()
        {
            acc.push(
                CompiledModule::deserialize(module_bytes)
                    .map_err(|e| anyhow!("Failure deserializing module: {:?}", e))?,
            )
        }
        Ok(acc)
    }

    /// Get the account states of the most critical accounts, including:
    /// 1. Aptos Framework code address
    /// 2. Aptos Root address
    /// 3. All validator addresses
    fn get_admin_accounts(&self, version: Version) -> Result<Vec<(AccountAddress, AccountState)>> {
        let mut result = vec![];
        let aptos_root = self
            .get_account_state_by_version(account_config::aptos_root_address(), version)?
            .ok_or_else(|| anyhow!("aptos_root_address doesn't exist"))?;

        // Get all validator accounts
        let validators = aptos_root
            .get_config::<ValidatorSet>()?
            .ok_or_else(|| anyhow!("validator_config doesn't exist"))?;

        // Get code account, aptos_root
        result.push((
            account_config::CORE_CODE_ADDRESS,
            self.get_account_state_by_version(account_config::CORE_CODE_ADDRESS, version)?
                .ok_or_else(|| anyhow!("core_code_address doesn't exist"))?,
        ));
        result.push((account_config::aptos_root_address(), aptos_root));

        // Get all validator accounts
        for validator_info in validators.payload() {
            let addr = *validator_info.account_address();
            result.push((
                addr,
                self.get_account_state_by_version(addr, version)?
                    .ok_or_else(|| anyhow!("validator {:?} doesn't exist", addr))?,
            ));
        }
        Ok(result)
    }
}

pub struct DebuggerStateView<'a> {
    db: &'a dyn AptosValidatorInterface,
    version: Option<Version>,
}

impl<'a> DebuggerStateView<'a> {
    pub fn new(db: &'a dyn AptosValidatorInterface, version: Option<Version>) -> Self {
        Self { db, version }
    }

    fn get_state_value_internal(
        &self,
        state_key: &StateKey,
        version: Version,
    ) -> Result<Option<Vec<u8>>> {
        match self.db.get_state_value_by_version(state_key, version)? {
            None => Ok(None),
            Some(state_value) => Ok(state_value.maybe_bytes),
        }
    }
}

impl<'a> StateView for DebuggerStateView<'a> {
    fn get_state_value(&self, state_key: &StateKey) -> Result<Option<Vec<u8>>> {
        match self.version {
            None => Ok(None),
            Some(version) => self.get_state_value_internal(state_key, version),
        }
    }

    fn is_genesis(&self) -> bool {
        false
    }
}