spacemeshos / svm

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

API change: substitute `env`, `context`, and `msg` for `tx` #484

Open YaronWittenstein opened 2 years ago

YaronWittenstein commented 2 years ago

Depends on #458

On issue #458, we've extended the svm-codec to be aware of the Envelope. We'll adapt both the Runtime and the C-API require a Transaction instead of Envelope and Message.

As an example, here we have the svm_deploy under the FFI API: https://github.com/spacemeshos/svm/blob/6edb73de199fafce0953f82af061ca1090ff911c/crates/runtime-ffi/src/api.rs#L262

#[must_use]
#[no_mangle]
pub unsafe extern "C" fn svm_deploy(
    runtime: *mut c_void,
    envelope: *const u8,
    message: *const u8,
    message_size: u32,
    context: *const u8,
) -> svm_result_t {
    svm_runtime_action(
        runtime,
        envelope,
        message,
        message_size,
        context,
        |r, e, m, c| Runtime::deploy(r, e, m, c),
        "svm_deploy",
    )
}

We'll modify the API to look as the following:

#[must_use]
#[no_mangle]
pub unsafe extern "C" fn svm_deploy(
    runtime: *mut c_void,
    tx: *const u8,
    tx_size: u32,
    context: *const u8,
) -> svm_result_t {
    svm_runtime_action(
        runtime,
        tx,
        tx_size,
        context,
        |r, t, c| Runtime::deploy(r, t, c),
        "svm_deploy",
    )
}

Consequently, we'll change the Runtime. In the Deploy context, we've this link: https://github.com/spacemeshos/svm/blob/6edb73de199fafce0953f82af061ca1090ff911c/crates/runtime/src/runtime/runtime.rs#L548

impl Runtime {
  pub fn deploy(
    &mut self,
    envelope: &Envelope,
    message: &[u8],
    context: &Context,
  ) -> DeployReceipt {
  // ...
}

it'll be modified to something similar to this:

impl Runtime {
  pub fn deploy(
    &mut self,
    raw_tx: &[u8],
    context: &Context,
  ) -> DeployReceipt {
    let tx = svm_codec::tx::decode_deploy(raw_tx);

    if tx.is_err() {
      let err = err::func_invalid_deploy(tx);
      Err(err)
    }

    let tx = tx.unwrap();
    let env = tx.envelope();
    let msg = tx.message();
  }
}

Now, we want to keep the raw_tx since we'll need it for further usage. In the Deploy transaction, it'll be used for deriving the Template Address.

We'll need the' raw_tx' for Spawn, Call, and Verify we'll need the raw_tx for executing code. (the raw transaction will be copied to the Wasm Instance Memory - see issue #462).

In similar fashion, we need to update the spawn, call and verify to require only the raw transaction. The svm_codec should expose:

Notes

For example, this code: https://github.com/spacemeshos/svm/blob/6edb73de199fafce0953f82af061ca1090ff911c/crates/runtime/src/runtime/runtime.rs#L521

It should be modified to validate the whole transaction instead of only the Message part of a Deploy Transaction.

pub fn validate_deploy(&self, tx: &[u8]) -> std::result::Result<(), ValidateError> {
  // ...
}
neysofu commented 2 years ago

Closely related to #493.