streamingfast / substreams-sink-kv

Substreams KV sink
Apache License 2.0
2 stars 5 forks source link

Add Rust Macro to better handle object packing and unpacking and error handling in WASM Query #20

Closed jubeless closed 4 months ago

jubeless commented 1 year ago

Build a macro to simplify the Rust code, handling:

unpack proto and pack proto. let req = GetMonthRequest::decode(&v[..]).expect("Failed to decode");

error handling

The current function signatures are eth_service_v1_blockmeta_getmonth(v: Vec<u8>) -> Result<Vec<u8>, String>

We should return Result<MyCustomObject, substreams_sink_kv::error:Error>

Where the substreams_sink_kv::error:Error object should contain a message and potentially a GRPC Status code that we can return to the user

Ideally this

#[wasmedge_bindgen]
pub fn eth_service_v1_blockmeta_getmonth(v: Vec<u8>) -> Result<Vec<u8>, String> {
    let req = GetMonthRequest::decode(&v[..]).expect("Failed to decode");
    .....
    let out = MonthResponse{}
   Ok(out.encode_to_vec())
}

becomes

#[wasmedge_bindgen]
#[my_macro]
pub fn eth_service_v1_blockmeta_getmonth(req: GetMonthRequest) -> Result<MonthResponse, substreams_sink_kv::error:Error> {
    let req = GetMonthRequest::decode(&v[..]).expect("Failed to decode");
    .....
    let out = MonthResponse{}
   Ok(out)
}

knowing that Result<Vec<u8>, String> needs to be the final output we would wrap the user's custom proto object and user define error into a intermidate proto object to handle these cases in the go handler