spacemeshos / svm

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

Implement host function: `svm_tx_sigdata_range` #441

Open YaronWittenstein opened 2 years ago

YaronWittenstein commented 2 years ago

Depends on: #457

So assuming the FuncEnv holds tx of type Option<(usize, size>) we now have access to the raw transaction.

Another good option would be to keep in addition the binary transaction itself. Let's call it tx_bytes. Given the tx_bytes we could ask the svm-codec to return for us the offset within the binary transaction where the sigdata starts - let's call it tx_sigdata_offset

The Memory Address where the sigdata starts is the first byte is the Memory Address where the transaction begins plus the tx_sigdata_offset.

Here is a pseudo-code in Rust how it should or work:

let offsets = svm_codec::tx_offsets(tx);
if offsets.contains(TxPart::SigData) == false {
  // The `sigdata` is optional, it transaction doesn't seem to have one
   panic!() 
}

// `tx_ptr` points to the start of the binary transaction in Memory
let tx_ptr = func_env.tx.as_ref().0;

// we got back the `sigdata` starting offset and the `sigdata` byte-length
// bace from the `svm-codec`
let (sigdata_start, sigdata_len) = tx_sigdata.unwrap();

// computing where `sigdata` starts in Memory
let sigdata_start_ptr = tx_ptr + sigdata_start;

// computing where `sigdata` ends in Memory
let sigdata_end_ptr = sigdata_start_ptr + sigdata_len - 1;

Given the computed sigdata_start_ptr and sigdata_end_ptr we can now memoize them for future usage. Say the FuncEnv will have tx_range: Option<(usize, size>) for holding this value.

In terms of Gas pricing, we can take that one-time computation into account and always price it. (what's the point of having a sigdata in the transaction and never using it? seems unlikely).