spacemeshos / svm

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

Add to the `svm-codec` the `tx_offsets` API #457

Open YaronWittenstein opened 2 years ago

YaronWittenstein commented 2 years ago

Depends on: #458

This issue is preparation work for copying the Transaction binary data into the Wasm Instance's Memory.

We want the svm-codec crate to expose a new API called tx_offsets that will receive the binary Transaction and return a HashMap mapping between a Transaction Part to its offsets within the Transaction.

The mapped offsets will be Tuple of (usize, usize) when the first element points to the offset beginning the Transaction Part and the second element will be the byte-length of that part.

It's recommended to introduce a new type for representing a Transaction Type. Probably the best place would be to put it inside the svm-types crate.

#[derive(Debug, PartiaEq, Copy, Clone, Hash)]
enum TxPart {
  // The `Envelope` and `Message` (without the `SigData` part)
  Full,        

  // The transaction `Envelope`
  Envelope,

  // The `Message` part of the transaction (doesn't include the `SigData`)      
  Message,             

  // The Encoding version of the transaction (should be 0x0 for Genesis)
  Version,

  // Tx kind (Deploy/Spawn/Call)
  Kind,         

  // The input for `verify`
  VerifyData,   

  // The input for the function to execute
  FuncData,   

  // The `Function` to run (the String)
  // 
  // Deploy - this field is undefined
  // Spawn  - this field will be the `ctor` name
  // Call   - this field will be the `func` name  
  FuncName, 

  // The `Principal` Address
  Principal,

  // The `Target` Address (defined only for `Call` transactions)
  Target,

  // The `Template` Address (defined only for `Spawn` transactions)
  TemplateAddr,

  // The blob of Signatures-related data
  // The `SigData` is an optional field that is sits right 
  // after the `Message` part ends.
  SigData,     

  // Other transaction parts: (we can probably ignore them for now in the codec `tx_offsets` context)
  // GasLimit,
  // GasFee,
  // Nonce,

  // In case we'll have `authorize`
  // AuthorizeData
}

Usage API

let tx: &[u8] = ...;
let offsets: HashMap<TxPart, (usize, usize)> = svm_codec::tx_offsets(tx);

Notes

Note that part of the Transaction Parts is relevant to specific transaction types, such as Target (relevant only for Call transactions). Also, other parts might be optional (for example the SigData).