spacemeshos / svm

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

Extend the `svm-codec` Wasm API with the `Envelope` #459

Open YaronWittenstein opened 2 years ago

YaronWittenstein commented 2 years ago

Depends on #458

This issue will extend the Wasm API of the svm_codec.wasm Consequently, the svm-codec-npm will have to be extended as well

On the Simple Coin #Iteration 2, the svm_codec.wasm will be extended again to support the Signatures appended to the end of the Transaction.

@avive FYI

Extensions to be implemented:

  1. Encode Envelope
    #[no_mangle]
    pub unsafe extern "C" fn wasm_encode_envelope(ptr: *mut u8) -> *mut u8 {
    ...
    }

The input should be a JSON:

{
  version: 0,
  type: 'deploy', // or `spawn` / `call`
  principal: '...',
  amount: 0,
  nonce: 0,
  gasLimit: 0,
  gasFee: 0
}
  1. Decode Envelope
    #[no_mangle]
    pub unsafe extern "C" fn wasm_decode_envelope(ptr: *mut u8) -> *mut u8 {
    //
    }

Should return a JSON of the same schema as the input given to wasm_encode_envelope

  1. Encode Transaction
#[no_mangle]
pub unsafe extern "C" fn wasm_encode_tx(env: *mut u8, msg: *mut u8) -> *mut u8 {
    ...
}

@neysofu @avive, my recommendation is that the svm-codec-npm will introduce a method such as loadDeployTemplate that will ask for a file-path pointing to a binary Deploy Transaction.

Then, it will call wasm_alloc asking to allocate the Deploy Transaction size. Once the JS code holds the Wasm buffer, it will call again, this time to wasm_buffer_data and copy Deploy Transaction to the Wasm Instance Memory.

  1. Decode Transaction

This one is a bit more tricky. Probably better to split into a couple of methods:

First we'll have the wasm_extract_tx_envelope method:

#[no_mangle]
pub unsafe extern "C" fn wasm_extract_tx_envelope(ptr: *mut u8) -> *mut u8 {
    //
}

It will return a Wasm buffer pointing only to the Envelope part (in its binary form). Then a call to wasm_decode_envelope will return the Envelope in its JSON format.

Similary, we'll have the wasm_extract_tx_message method:

#[no_mangle]
pub unsafe extern "C" fn wasm_extract_tx_message(ptr: *mut u8) -> *mut u8 {
    //
}

It will return a Wasm buffer pointing only to the Message part (in its binary form). Then a call to wasm_decode_spawn or wasm_decode_call will return the decoded Message in a JSON format.

Notes