runtime API: Substitute UncheckedExtrinsic with custom encoding (#1076)

* codegen: Add uncheckedExtrinsic substitute

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Add uncheckedExtrinsic replacement

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Test uncheckedExtrinsic encoding

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Apply clippy

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Implement encode_to instead of encode for uncheckedExtrinsic

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Remove encode_as_fields from uncheckedExtrinsic

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* utils: Extend the UncheckedExtrinsic interface

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* utils: Use Static<Encoded> for uncheckedExtrinsic

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* utils: Remove extra impl on the uncheckedExtrinsic

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* utils: Add back the EncodeAsType

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* utils: Simplify the decode_as_type

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* utils: Use encode_as_type

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* utils: impl Decode for UncheckedExtrinsic

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update subxt/src/utils/unchecked_extrinsic.rs

Co-authored-by: James Wilson <james@jsdw.me>

* utils: Apply cargo fmt

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* utils: Check encoding / decoding of uncheckedExtrinsic

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* utils/tests: Use an already encoded tx bytes to start with

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: James Wilson <james@jsdw.me>
This commit is contained in:
Alexandru Vasile
2023-07-21 13:34:34 +03:00
committed by GitHub
parent c2875de172
commit fd8f60c8a9
4 changed files with 203 additions and 2 deletions
@@ -3,6 +3,7 @@
// see LICENSE for license details.
use crate::{node_runtime, test_context};
use codec::Encode;
use subxt::utils::AccountId32;
use subxt_signer::sr25519::dev;
@@ -47,3 +48,57 @@ async fn account_nonce() -> Result<(), subxt::Error> {
Ok(())
}
#[tokio::test]
async fn unchecked_extrinsic_encoding() -> Result<(), subxt::Error> {
let ctx = test_context().await;
let api = ctx.client();
let alice = dev::alice();
let bob = dev::bob();
let bob_address = bob.public_key().to_address();
// Construct a tx from Alice to Bob.
let tx = node_runtime::tx().balances().transfer(bob_address, 10_000);
let signed_extrinsic = api
.tx()
.create_signed(&tx, &alice, Default::default())
.await
.unwrap();
let tx_bytes = signed_extrinsic.into_encoded();
let len = tx_bytes.len() as u32;
// Manually encode the runtime API call arguments to make a raw call.
let mut encoded = tx_bytes.clone();
encoded.extend(len.encode());
let expected_result: node_runtime::runtime_types::pallet_transaction_payment::types::FeeDetails<
::core::primitive::u128,
> = api
.runtime_api()
.at_latest()
.await?
.call_raw(
"TransactionPaymentApi_query_fee_details",
Some(encoded.as_ref()),
)
.await?;
// Use the generated API to confirm the result with the raw call.
let runtime_api_call = node_runtime::apis()
.transaction_payment_api()
.query_fee_details(tx_bytes.into(), len);
let result = api
.runtime_api()
.at_latest()
.await?
.call(runtime_api_call)
.await?;
assert_eq!(expected_result, result);
Ok(())
}