Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Improve "Runtime API" / "View Function" APIs: allow trying to decode results into multiple target types, like Storage APIs do.

Aperta
#2,199 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
44/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
rust

Direzione di ricerca

Inizia dagli entry point runtime_apis().call e call_raw, quindi confronta la gestione delle relative risposte con il comportamento di decodifica delle storage APIs. Definisci come una risposta possa essere decodificata in più tipi di destinazione mantenendo il percorso di decodifica predefinito del payload e considera l'ambito richiesto per View Functions e constants. Il lavoro è completo quando l'API proposta supporta i tentativi di decodifica sequenziali dell'esempio senza chiamate di rete ripetute.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

The Subxt storage APIs allow trying to decode the values we get back into either the default target type given by the Payload, or some custom types.

Particularly when dealing with historic types (eg Rest API), one often needs to support multiple different target types for a call. A motivating example is in this PR.

We have a couple of ways to do this but none are ideal:

// Option 1: call the Runtime API multiple times, trying different decode targets (slow because networking!):
let payload = subxt::runtime_apis::dynamic<Args, ResponseTypeV1>("Some", "runtime_api_call", args);
let response: GenericResponseType = match client_at_block.runtime_apis().call(payload).await {
    Ok(res) => response_v1_to_generic_response_type(res),
    Err(_e) => {
        let payload2 = subxt::runtime_apis::dynamic<Args, ResponseTypeV2>("Some", "runtime_api_call", args);
        match client_at_block.runtime_apis().call(payload2).await {
            Ok(res2) => response_v2_to_generic_response_type(res),
            Err(_e) => {
                let payload3 = subxt::runtime_apis::dynamic<Args, ResponseTypeV3>("Some", "runtime_api_call", args);
                client_at_block.runtime_apis().call(payload2).await.map(response_v3_to_generic_response_type)?
            }
        }
    }
}

// Option 2: Decode into inefficient scale_value::Value and manually inspect it (slow because Value bad for CPU/memory):
let payload = subxt::runtime_apis::dynamic<Args, scale_value::Value>("Some", "runtime_api_call", args);
let response_value = match client_at_block.runtime_apis().call(payload).await?;

let response = {
    if let Some(response) = from_v1_value_to_generic(&response_value) {
        response
    } else if let Some(response) = from_v2_value_to_generic(&response_value) {
        response
    } else if let Some(response) = from_v3_value_to_generic(&response_value) {
        response
    } else {
        return Err(SomeErrorThatResponseIsBad)
    }
};

// Option 3: make a raw call and manually decode things (verbose and error prone):
let call_args = (arg1, arg2, arg3).encode();

let response_bytes = client_at_block.runtime_apis().call_raw("Some_runtime_api_call", Some(&call_args)).await?;

let return_type_id = client_at_block
    .metadata()
    .runtime_api_trait_by_name("Some")?
    .method_by_name("runtime_api_call")?
    .output_ty();

let response = {
    if let Ok(res) = scale_decode::decode_as_type::<ResponseTypeV1>(&mut &*response_bytes), return_type_id, metadata.types()) {
        response_v1_to_generic_response_type(res)
    } else if let Ok(res) = scale_decode::decode_as_type::<ResponseTypeV2>(&mut &*response_bytes), return_type_id, metadata.types()) {
        response_v2_to_generic_response_type(res)
    } else if let Ok(res) = scale_decode::decode_as_type::<ResponseTypeV3>(&mut &*response_bytes), return_type_id, metadata.types()) {
        response_v3_to_generic_response_type(res)
    }
};

Let's instead make it possible to do something more like the storage APIs allow eg:

let payload = subxt::runtime_apis::dynamic<Args, ()>("Some", "runtime_api_call", args);
let response = client_at_block.runtime_apis().call(payload).await?;

let response = {
    if let Ok(res) = response.decode_as::<ResponseTypeV1>() {
        response_v1_to_generic_response_type(res)
    } else if let Ok(res) = response.decode_as::<ResponseTypeV2>() {
        response_v2_to_generic_response_type(res)
    } else if let Ok(res) = response.decode_as::<ResponseTypeV3>() {
        response_v3_to_generic_response_type(res)
    }
};

This avoids the unnecessary network calls, and makes it easy to try decoding into multiple statically defined types and transform these each into some general representation you'll return. Like storage, if a return type is defined in the payload then response.decode()? is all that you'd need to decode into said static type (which is the default path if you use Subxts generated types too).

It may be worth exposing similar APIs for View Functions and possibly constants too while we are at it.

Lingua principale
Rust
Stelle
490
Fork
292
Merge medio
8h 33m
PR unite (30g)
1

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di paritytech/subxt

Tutte le issue di paritytech/subxt

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.