0xPolygonMiden / miden-base

Core components of the Polygon Miden rollup
MIT License
73 stars 45 forks source link

Implement `AccountComponent`s #941

Closed PhilippGackstatter closed 2 weeks ago

PhilippGackstatter commented 3 weeks ago

Description

This PR implements AccountComponents. An AccountComponent defines a Library of code and the initial value and types of the StorageSlots it accesses.

One or more components can be used to built AccountCode and AccountStorage.

Each component is independent of other components and can only access its own storage slots. Each component defines its own storage layout starting at index 0 up to the length of the storage slots vector.

Components define the AccountTypes they support, meaning whether the component can be used to instantiate an account of that type. For example, a component implementing a fungible faucet would only specify support for AccountType::FungibleFaucet. Using it to instantiate a regular account would fail. By default, the set of supported types is empty, so each component is forced to explicitly define what it supports.

The PR implements three pre-defined components:

These can be combined to build an account from components. Typical usage would look like this:

let (account_code, account_storage) = Account::initialize_from_components(
    AccountType::RegularAccountUpdatableCode,
    &[RpoFalcon512::new(pub_key).into(), BasicWallet.into()],
)?;

let account_seed = AccountId::get_account_seed(
    init_seed,
    account_type,
    account_storage_mode,
    account_code.commitment(),
    account_storage.commitment(),
)?;

let account = Account::new(account_seed, account_code, account_storage)?;

Other Noteworthy Changes

closes #935

PhilippGackstatter commented 3 weeks ago

@bobbinth I addressed all your comments now and am creating new issues for the leftover tasks.