spacemeshos / svm

SVM - Spacemesh Virtual Machine
https://spacemesh.io
MIT License
85 stars 14 forks source link

Extend the `Runtime` to create a new `Account` with its `Immutable Data` (for `Non-Self Spawn`) #477

Open YaronWittenstein opened 2 years ago

YaronWittenstein commented 2 years ago

Depends on: #470 #471

When we run a Spawn Transaction (the Non-Self Spawn if to be precise) we want to create the Account and assign it its immutable_data given in the transaction input.

For now, we'll set the TxKind to TxKind::Spawn since we don't support the Self-Spawn yet. For more info about TxKind see issue #470

impl Runtime {
  pub fn spawn(
        &mut self,
        envelope: &Envelope,
        message: &[u8],
        context: &Context,
    ) -> SpawnReceipt { 
  info!("Runtime `spawn`");

  let gas_left = GasTank::new(envelope.gas_limit());
  let spawn = SpawnAccount::decode_bytes(message).expect(ERR_VALIDATE_SPAWN);

  // If the `Principal` is a `Pending Account` (a.k.a a `Stub Account`)
  // then we say we're running a `Self-Spawn` transaction.
  let self_spawn = ...

  if self_spawn {
    todo!("Simple Coin Iteration #4")
  }
  else {
    // now the `self.create_account` expects an additional `immutable_data` param
    self.create_account(
            &target,
            template_addr.clone(),
            account.name().to_string(),
            0,
            0,
            &spawn.immutable_data;
        )
        .unwrap();

     self.call_ctor(&spawn, target, envelope, context, gas_left)
   }
 }

 pub fn create_account(
        &mut self,
        account_addr: &Address,
        template_addr: TemplateAddr,
        name: String,
        balance: u64,
        counter: u128,
        immutable_data: &[u8] 
    ) -> Result<()> {
   let mut account = AccountStorage::create(
            self.gs.clone(),
            account_addr,
            name,
            template_addr,
            balance,
            counter,
        )
        .unwrap();

    // We might want `AccountStorage::create` to receive `immutable_data`.
    // as a parameter, at at least as `Option<Vec<u8>>>` (when creating a `Pending Account`) 
    // However, the `Pending Account` is part of the `Self-Spawn` or `Simple Coin Iteration #4`
    account.set_immutable(immutable_data);

    Ok(())
  }
}