|
1 | 1 | use crate::aptos::primitive_types::AptosStorage;
|
2 | 2 | use aptos_api_types::{Address, HexEncodedBytes, MoveModule, MoveModuleBytecode, MoveResource};
|
| 3 | +use aptos_crypto::HashValue; |
| 4 | +use aptos_db::AptosDB; |
3 | 5 | use aptos_sdk::rest_client::Account;
|
4 |
| -use sov_modules_api::{StateMapAccessor, WorkingSet}; |
5 |
| -use sov_state::codec::BcsCodec; |
6 |
| -use std::convert::Infallible; |
7 |
| - |
8 |
| -use super::{AccountInfo, DbAccount}; |
| 6 | +use aptos_storage_interface::state_view::DbStateViewAtVersion; |
| 7 | +use aptos_storage_interface::DbReader; |
| 8 | +use aptos_types::state_store::errors::StateviewError; |
| 9 | +use aptos_types::state_store::state_key::StateKey; |
| 10 | +use aptos_types::state_store::state_storage_usage::StateStorageUsage; |
| 11 | +use aptos_types::state_store::{state_value::StateValue as AptosStateValue, TStateView}; |
| 12 | +use aptos_types::transaction::Version; |
| 13 | +use sov_modules_api::{StateMap, StateMapAccessor, StateValue, WorkingSet}; |
| 14 | +use std::sync::Arc; |
9 | 15 |
|
| 16 | +type Result<T, E = StateviewError> = std::result::Result<T, E>; |
10 | 17 | /// The Aptos Database structure for storing and working with accounts and their modules.
|
11 |
| -pub(crate) struct AptosDb<'a, S: sov_modules_api::Spec> { |
12 |
| - /// Accounts storage |
13 |
| - pub(crate) accounts: sov_modules_api::StateMap<Address, DbAccount, BcsCodec>, |
14 |
| - /// Modules storage |
15 |
| - pub(crate) modules: sov_modules_api::StateMap<Address, Vec<MoveModule>, BcsCodec>, |
16 |
| - /// Resources storage |
17 |
| - pub(crate) resources: sov_modules_api::StateMap<Address, Vec<MoveResource>, BcsCodec>, |
| 18 | +pub(crate) struct SovAptosDb<'a, S: sov_modules_api::Spec> { |
| 19 | + pub(crate) state_kv_db: StateMap<Version, AptosStateValue>, |
18 | 20 | /// Working set
|
19 | 21 | pub(crate) working_set: &'a mut WorkingSet<S>,
|
20 | 22 | }
|
21 | 23 |
|
22 |
| -impl<'a, S: sov_modules_api::Spec> AptosDb<'a, S> { |
23 |
| - pub(crate) fn new( |
24 |
| - accounts: sov_modules_api::StateMap<Address, DbAccount, BcsCodec>, |
25 |
| - modules: sov_modules_api::StateMap<Address, Vec<MoveModule>, BcsCodec>, |
26 |
| - resources: sov_modules_api::StateMap<Address, Vec<MoveResource>, BcsCodec>, |
27 |
| - working_set: &'a mut WorkingSet<S>, |
28 |
| - ) -> Self { |
29 |
| - Self { accounts, modules, resources, working_set } |
| 24 | +impl<'a, S: sov_modules_api::Spec> SovAptosDb<'a, S> { |
| 25 | + pub(crate) fn new(working_set: &'a mut WorkingSet<S>, db: StateValue<AptosDB>) -> Self { |
| 26 | + Self { working_set, db } |
30 | 27 | }
|
31 |
| -} |
32 | 28 |
|
33 |
| -impl<'a, S: sov_modules_api::Spec> AptosStorage for AptosDb<'a, S> { |
34 |
| - type Error = Infallible; |
| 29 | + /// Get state view at `Version`, this is analogous to `blockheight`. |
| 30 | + /// `Version` is a type alias for `u64` in the `aptos_types` module. |
| 31 | + /// Source code: https://github.com/0xmovses/aptos-core/blob/bd1644729bc2598d9769fbf556797d5a4f51bf35/types/src/transaction/mod.rs#L77 |
| 32 | + /// |
| 33 | + /// For reading state from the SovAptosDb. We purposefully do not implement the Aptos native `DbReader` trait |
| 34 | + /// because it is `Send` and `Sync`. The sov_modules_api::StateMap is not `Send` and `Sync` so instead we add |
| 35 | + /// this custom method. |
| 36 | + pub(crate) fn state_view_at_version( |
| 37 | + &self, |
| 38 | + version: Option<Version>, |
| 39 | + ) -> Result<DbStateView<'a, S>> { |
| 40 | + Ok(DbStateView { db: self.clone(), version, verify_against_state_root_hash: None }) |
| 41 | + } |
| 42 | +} |
35 | 43 |
|
36 |
| - fn account(&mut self, account: Account) -> Result<AccountInfo, Self::Error> { |
37 |
| - let db_account = self |
38 |
| - .accounts |
39 |
| - .get(&Address::from(account.authentication_key.account_address()), self.working_set) |
40 |
| - .expect("Account not found"); |
| 44 | +/// The DbStateView that is passed to the VM for transaction execution. |
| 45 | +/// We don't use the Aptos native `DbStateView` because its `db` field is `Arc<dyn DbReader>`, |
| 46 | +/// meaning it's a dynamically dispatched trait object, so unable to derive |
| 47 | +/// serialization/deserialization. Instead, in our custom type we use a concrete type `SovAptosDb`. |
| 48 | +pub struct DbStateView<'a, S> |
| 49 | +where |
| 50 | + S: sov_modules_api::Spec, |
| 51 | +{ |
| 52 | + db: &'a SovAptosDb<'a, S>, |
| 53 | + version: Option<Version>, |
| 54 | + verify_against_state_root_hash: Option<HashValue>, |
| 55 | +} |
41 | 56 |
|
42 |
| - Ok(db_account.info) |
43 |
| - } |
| 57 | +//`DbStateView` must implement `TStateView` trait for `AptosVM` to execute transactions. |
| 58 | +impl<'a, S> TStateView for DbStateView<'a, S> |
| 59 | +where |
| 60 | + S: sov_modules_api::Spec, |
| 61 | +{ |
| 62 | + type Key = StateKey; |
44 | 63 |
|
45 |
| - fn resources(&mut self, account: Account) -> Result<Vec<MoveResource>, Self::Error> { |
46 |
| - let resources = self |
47 |
| - .resources |
48 |
| - .get(&Address::from(account.authentication_key.account_address()), self.working_set) |
49 |
| - .expect("Modules not found"); |
50 |
| - Ok(resources) |
| 64 | + fn get_state_value(&self, state_key: &StateKey) -> Result<Option<AptosStateValue>> { |
| 65 | + self.get(state_key).map_err(Into::into) |
51 | 66 | }
|
52 | 67 |
|
53 |
| - fn modules(&mut self, account: Account) -> Result<Vec<MoveModule>, Self::Error> { |
54 |
| - let modules = self |
55 |
| - .modules |
56 |
| - .get(&Address::from(account.authentication_key.account_address()), self.working_set) |
57 |
| - .expect("Modules not found"); |
58 |
| - Ok(modules) |
| 68 | + fn get_usage(&self) -> Result<StateStorageUsage> { |
| 69 | + self.db.get_state_storage_usage(self.version).map_err(Into::into) |
59 | 70 | }
|
60 | 71 | }
|
0 commit comments