Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Extend the AccountStorage to support Immutable Storage #471

Open
YaronWittenstein opened this issue Dec 21, 2021 · 0 comments
Open

Extend the AccountStorage to support Immutable Storage #471

YaronWittenstein opened this issue Dec 21, 2021 · 0 comments
Assignees
Labels
AA Related to the Accounts Abstraction simple-coin-iteration-3 svm svm-core SVM core change

Comments

@YaronWittenstein
Copy link
Contributor

YaronWittenstein commented Dec 21, 2021

Depends on: #469 #470

As explained at #470, We'll implement the Self-Spawn feature only in the Simple Coin Integration #4.

The Account Immutable Data is given in the immutable_data field of the Spawn Transaction (see issue: #469).
The Immutable Data interpretation is specified in the Storage Section.
It's part of the Deploy Template Sections (the Data Section).

When we want to read an Account Immutable Storage, we need to ask the AccountStorage to do so for us.
This issue extends the AccuontStorage to be Immutable Storage Aware.

There is one exception calling verify of a Self-Spawn.
Right now, we panic. For more info, see the description of #470

fn read<T, F: () -> T>(env: &FuncEnv, section_idx: u32, f: F) -> T {
  assert!(env.read_allowed(section_idx));

  if env.within_self_spawn_verify() {
    todo!("Simple Coin Iteration #4")
  }
  else {
    f()
  }
}

Here is a draft of how the AccountStorage should be modified for supporting Immutable Storage.
Note that the storage host functions should stay intact. They continue to delegate read and write
operations that have been granted permissions back to the AccountStorage.

Also: this proposed implementation put the whole Immutable Data of an Account to sit under one key under the underlying key-value store. That will make creating news Accounts faster since we can take the immutable_data given in the Spawn Transaction and store it in one bulk under the AccountStorage (without caring about the Immutable Storage Layout).

#[derive(Debug, Clone)]
pub struct AccountStorage {
  /// The `Account`'s Immutable Storage Layout (Section #0).
  ///
  /// Important: we'll need interior-mutability here
  //// so might end up having `Arc<Mutex<FixedLayout>>`
  immutable_layout: FixedLayout,

  /// The Immutable Data of the `Account`
  /// This data should be loaded lazily and once
  ///
  /// Important: we'll need interior-mutability here
  //// so might end up having `Arc<Mutex<Option<Vec<u8>>>`
  immutable_data: Option<Vec<u8>>,

  /// The `Account`'s Mutable Storage Layout.
  /// (maybe we should rename to `mutable_layout`?
  layout: FixedLayout,
}

impl AccountStorage {
  pub fn get_var(&self, var_id: u32, section_idx: u32, mut var: &mut [u8]) -> StorageResult<()> {
    if section_idx == 0 {
      if immutable_data.is_none() {
        let (layout, data) = self.immutable_info();
        // store `self.immutable_layout` and `self.immutable_data`
        // (acting as a cache)
      }

      // TODO: extract `var_id` out of the `self.immutable_data`
      // using the layout under `self.immutable_layout`.
    }
    else {
      // The same code as today
      // ...
    }
  }

  pub fn set_var_bytes(&mut self, var_id: u32, section_idx: u32, mut new_value: &[u8]) -> StorageResult<()> {
    assert!(section_idx != 0);

     // The same code as today
     // ...
  } 

  pub set_immutable(&mut self, immutable_data: &[u8]) -> StorageResult<()> {
    // store immutable under one key (see `key_account_immutable`)
    let key = key_account_immutable(&self.address);
   
    // TODO:
    // store the `Immutable Storage` (deledate to the `self.gs`)
  }

  // Returns the `Immutable Section Layout` and its whole data as a single blob
  pub fn immutable_info(&self) -> StorageResult<(FixedLayout, Vec<u8>)> {
    let layout = self.immutable_layout()?;
    let data = self.immutable_data()?;

    Ok((layout, data))
  }

  pub fn immutable_layout(&self) -> StorageResult<FixedLayout> {
    let account_data = AccountData::read(&gs, address)?;
    
    // We should make it part of the `Data Section`
    // instead of relying on hardcoded `Section Index` being zero
    let immutable_section_idx = 0;
    
    let layout = template_layout(gs.clone(), &account_data.template_addr, immutable_section_idx)?;
    Ok(layout)
  }
  
  pub fn immutable_data(&self) -> StorageResult<FixedLayout> {
      let key = key_account_immutable(&self.address);

      // TODO: load the Blob associated with `key.`
  }
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
AA Related to the Accounts Abstraction simple-coin-iteration-3 svm svm-core SVM core change
Projects
None yet
Development

No branches or pull requests

2 participants