accounts.rs

Overview

The accounts.rs file defines the AccountsRepository struct, which manages the persistent storage and lifecycle of account states in a filesystem-based repository. It supports loading, storing, and clearing old account states based on transaction identifiers and provides thread-safe tracking of deleted accounts. The repository organizes account data under a specified directory and uses a naming scheme involving transaction logical time (LT) and transaction hashes for versioning.

This file primarily interacts with:

AccountsRepository Struct

Fields

Field Name

Type

Description

data_dir

PathBuf

Base directory path where account data is stored, specifically under a subdirectory named "accounts".

unload_after

Option<u32>

Optional configuration controlling how long accounts can remain loaded before they should be unloaded.

store_after

u32

Configuration parameter defining thresholds related to storing accounts.

deleted_accounts

Arc<Mutex<HashMap<ThreadIdentifier, BTreeMap<u64, Vec<AccountAddress>>>>>

Thread-safe map tracking deleted accounts per thread, indexed by logical time (LT).

Methods

new

pub fn new(data_dir: PathBuf, unload_after: Option<u32>, store_after: u32) -> Self

Creates a new AccountsRepository instance.

let repo = AccountsRepository::new(PathBuf::from("/var/data"), Some(300), 100);

account_path

fn account_path(
    &self,
    account_id: &AccountAddress,
    last_trans_hash: &tvm_types::UInt256,
    last_trans_lt: u64,
) -> PathBuf

Constructs the filesystem path for storing or loading an account state based on its identifier and transaction metadata.

load_account

pub fn load_account(
    &self,
    account_id: &AccountAddress,
    last_trans_hash: &tvm_types::UInt256,
    last_trans_lt: u64,
) -> anyhow::Result<tvm_types::Cell>

Loads a serialized account state from the filesystem and deserializes it into a tvm_types::Cell.

let account_cell = repo.load_account(&account_id, &last_hash, last_lt)?;

store_account

pub fn store_account(
    &self,
    account_id: &AccountAddress,
    last_trans_hash: &tvm_types::UInt256,
    last_trans_lt: u64,
    account: tvm_types::Cell,
) -> anyhow::Result<()>

Serializes and stores the given account state to disk atomically using a temporary file.

repo.store_account(&account_id, &last_hash, last_lt, account_cell)?;

clear_old_accounts

pub fn clear_old_accounts(
    &self,
    thread_id: &ThreadIdentifier,
    relevant_state: &ShardAccounts,
    cut_lt: u64,
)

Removes old account state files and directories that are no longer relevant, based on logical time thresholds.

accounts_deleted

pub fn accounts_deleted(
    &self,
    thread_id: &ThreadIdentifier,
    accounts: Vec<AccountAddress>,
    lt: u64,
)

Records accounts that have been deleted for a specific thread and logical time.

Accessors

Implementation Details and Algorithms

Interaction with Other System Components

Diagram: AccountsRepository Structure and Workflow

classDiagram
class AccountsRepository {
-data_dir: PathBuf
-unload_after: Option<u32>
-store_after: u32
-deleted_accounts: Arc<Mutex<HashMap<ThreadIdentifier, BTreeMap<u64, Vec<AccountAddress>>>>>
+new()
+account_path()
+load_account()
+store_account()
+clear_old_accounts()
+accounts_deleted()
+get_unload_after()
+get_store_after()
}
AccountsRepository --> AccountAddress : uses
AccountsRepository --> tvm_types::Cell : stores/loads
AccountsRepository --> ShardAccounts : iterates accounts
AccountsRepository --> ThreadIdentifier : tracks thread IDs
AccountsRepository --> Mutex : synchronizes deleted_accounts
AccountsRepository --> PathBuf : manages file paths

This diagram highlights the main properties and public methods of AccountsRepository, as well as its relationships with key types used for account identification, storage, synchronization, and iteration.