Improve "Runtime API" / "View Function" APIs: allow trying to decode results into multiple target types, like Storage APIs do.
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
- Ambito
- api, backend-api-design
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di paritytech/subxt
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
paritytech/subxt#2238 ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 55/100
paritytech/subxt#2297 ·
-
Support transaction extensions that authorize over the inherited implicationForse già presa @TarikGul l’ha presa 2 giorni fa. Aperta
paritytech/subxt#2295 · 1 assegnatario ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 70/100
paritytech/subxt#2291 ·
-
View functions don't work as expectedForse già presa @RaphaelFakhri l’ha presa 4 giorni fa. Aperta
Difficoltà 4/5 1-2 giorni Idoneità per principianti 35/100
paritytech/subxt#2290 ·
Tutte le issue di paritytech/subxt
Issue simili
-
area: library data-loss type: bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
alplabai/signex#1740 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
test(fslc): the unreadable mutate catalog test pins the Unix I/O error text and fails on WindowsForse già presa @rizumita l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 69/100
I maintainer di solito rispondono entro 1 giorno
-
ServerProperties defaults white-list to true, so a fresh --dedicated server silently locks itselfAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
horizon-rust/horizon#3888 ·
I maintainer di solito rispondono entro 1 giorno
-
refactor
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
I maintainer di solito rispondono entro 1 giorno