scale-type-resolver integration (#1460)

* start migrating, broken

* first iteration of updating

* fmt and clippy

* add Composite<u32> decoding via scale value patch

* bump scale type gen versions

* fix decoding with new scale decode

* fix js-sys versions

* add nit comments
This commit is contained in:
Tadeo Hepperle
2024-03-13 14:48:11 +01:00
committed by GitHub
parent 952cde3f79
commit 8bdd276d2f
22 changed files with 256 additions and 239 deletions
Generated
+30 -19
View File
@@ -3471,38 +3471,38 @@ dependencies = [
[[package]] [[package]]
name = "scale-bits" name = "scale-bits"
version = "0.4.0" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "036575c29af9b6e4866ffb7fa055dbf623fe7a9cc159b33786de6013a6969d89" checksum = "662d10dcd57b1c2a3c41c9cf68f71fb09747ada1ea932ad961aca7e2ca28315f"
dependencies = [ dependencies = [
"parity-scale-codec", "parity-scale-codec",
"scale-info", "scale-info",
"scale-type-resolver",
"serde", "serde",
] ]
[[package]] [[package]]
name = "scale-decode" name = "scale-decode"
version = "0.10.0" version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7caaf753f8ed1ab4752c6afb20174f03598c664724e0e32628e161c21000ff76" checksum = "afc79ba56a1c742f5aeeed1f1801f3edf51f7e818f0a54582cac6f131364ea7b"
dependencies = [ dependencies = [
"derive_more", "derive_more",
"parity-scale-codec", "parity-scale-codec",
"primitive-types", "primitive-types",
"scale-bits", "scale-bits",
"scale-decode-derive", "scale-decode-derive",
"scale-info", "scale-type-resolver",
"smallvec", "smallvec",
] ]
[[package]] [[package]]
name = "scale-decode-derive" name = "scale-decode-derive"
version = "0.10.0" version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3475108a1b62c7efd1b5c65974f30109a598b2f45f23c9ae030acb9686966db" checksum = "5398fdb3c7bea3cb419bac4983aadacae93fe1a7b5f693f4ebd98c3821aad7a5"
dependencies = [ dependencies = [
"darling 0.14.4", "darling 0.14.4",
"proc-macro-crate 1.3.1",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 1.0.109", "syn 1.0.109",
@@ -3510,24 +3510,24 @@ dependencies = [
[[package]] [[package]]
name = "scale-encode" name = "scale-encode"
version = "0.5.0" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d70cb4b29360105483fac1ed567ff95d65224a14dd275b6303ed0a654c78de5" checksum = "628800925a33794fb5387781b883b5e14d130fece9af5a63613867b8de07c5c7"
dependencies = [ dependencies = [
"derive_more", "derive_more",
"parity-scale-codec", "parity-scale-codec",
"primitive-types", "primitive-types",
"scale-bits", "scale-bits",
"scale-encode-derive", "scale-encode-derive",
"scale-info", "scale-type-resolver",
"smallvec", "smallvec",
] ]
[[package]] [[package]]
name = "scale-encode-derive" name = "scale-encode-derive"
version = "0.5.0" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "995491f110efdc6bea96d6a746140e32bfceb4ea47510750a5467295a4707a25" checksum = "7a304e1af7cdfbe7a24e08b012721456cc8cecdedadc14b3d10513eada63233c"
dependencies = [ dependencies = [
"darling 0.14.4", "darling 0.14.4",
"proc-macro-crate 1.3.1", "proc-macro-crate 1.3.1",
@@ -3563,10 +3563,20 @@ dependencies = [
] ]
[[package]] [[package]]
name = "scale-typegen" name = "scale-type-resolver"
version = "0.1.1" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00860983481ac590ac87972062909bef0d6a658013b592ccc0f2feb272feab11" checksum = "10b800069bfd43374e0f96f653e0d46882a2cb16d6d961ac43bea80f26c76843"
dependencies = [
"scale-info",
"smallvec",
]
[[package]]
name = "scale-typegen"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d6108609f017741c78d35967c7afe4aeaa3999b848282581041428e10d23b63"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -3577,9 +3587,9 @@ dependencies = [
[[package]] [[package]]
name = "scale-typegen-description" name = "scale-typegen-description"
version = "0.1.0" version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae69c1dfd81e9859a5fb42c1b560369e6ed82d2c5b5cb4cac3bba1962a73f017" checksum = "479f0b8b0d75cce8d284ace5a9b7f5a12c523c94387c710835695e8b194a17bb"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"peekmore", "peekmore",
@@ -3595,9 +3605,9 @@ dependencies = [
[[package]] [[package]]
name = "scale-value" name = "scale-value"
version = "0.13.0" version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58223c7691bf0bd46b43c9aea6f0472d1067f378d574180232358d7c6e0a8089" checksum = "c07ccfee963104335c971aaf8b7b0e749be8569116322df23f1f75c4ca9e4a28"
dependencies = [ dependencies = [
"base58", "base58",
"blake2", "blake2",
@@ -3609,6 +3619,7 @@ dependencies = [
"scale-decode", "scale-decode",
"scale-encode", "scale-encode",
"scale-info", "scale-info",
"scale-type-resolver",
"serde", "serde",
"yap", "yap",
] ]
+14 -7
View File
@@ -18,7 +18,14 @@ members = [
# We exclude any crates that would depend on non mutually # We exclude any crates that would depend on non mutually
# exclusive feature flags and thus can't compile with the # exclusive feature flags and thus can't compile with the
# workspace: # workspace:
exclude = ["testing/no-std-tests", "testing/wasm-rpc-tests", "testing/wasm-lightclient-tests", "signer/wasm-tests", "examples/wasm-example", "examples/parachain-example"] exclude = [
"testing/no-std-tests",
"testing/wasm-rpc-tests",
"testing/wasm-lightclient-tests",
"signer/wasm-tests",
"examples/wasm-example",
"examples/parachain-example"
]
resolver = "2" resolver = "2"
[workspace.package] [workspace.package]
@@ -81,10 +88,10 @@ proc-macro2 = "1.0.78"
quote = "1.0.35" quote = "1.0.35"
regex = "1.10.3" regex = "1.10.3"
scale-info = { version = "2.10.0", default-features = false } scale-info = { version = "2.10.0", default-features = false }
scale-value = "0.13.0" scale-value = "0.14.1"
scale-bits = "0.4.0" scale-bits = "0.5.0"
scale-decode = "0.10.0" scale-decode = "0.11.1"
scale-encode = "0.5.0" scale-encode = "0.6.0"
serde = { version = "1.0.197" } serde = { version = "1.0.197" }
serde_json = { version = "1.0.114" } serde_json = { version = "1.0.114" }
syn = { version = "2.0.15", features = ["full", "extra-traits"] } syn = { version = "2.0.15", features = ["full", "extra-traits"] }
@@ -98,8 +105,8 @@ url = "2.5.0"
wabt = "0.10.0" wabt = "0.10.0"
wasm-bindgen-test = "0.3.24" wasm-bindgen-test = "0.3.24"
which = "5.0.0" which = "5.0.0"
scale-typegen-description = "0.1.0" scale-typegen-description = "0.2.0"
scale-typegen = "0.1.1" scale-typegen = "0.2.0"
strip-ansi-escapes = "0.2.0" strip-ansi-escapes = "0.2.0"
# Light client support: # Light client support:
@@ -64,8 +64,11 @@ pub fn explore_constants(
.highlight(); .highlight();
// value // value
let value = let value = scale_value::scale::decode_as_type(
scale_value::scale::decode_as_type(&mut constant.value(), constant.ty(), metadata.types())?; &mut constant.value(),
&constant.ty(),
metadata.types(),
)?;
let value = format_scale_value(&value).indent(4); let value = format_scale_value(&value).indent(4);
writedoc!( writedoc!(
+1 -1
View File
@@ -169,7 +169,7 @@ pub async fn explore_storage(
{value_str} {value_str}
"}?; "}?;
let key_bytes = value.encode_as_type(type_id, metadata.types())?; let key_bytes = value.encode_as_type(&type_id, metadata.types())?;
let bytes_composite = Value::from_bytes(key_bytes); let bytes_composite = Value::from_bytes(key_bytes);
vec![bytes_composite] vec![bytes_composite]
} }
+2 -3
View File
@@ -161,9 +161,8 @@ pub async fn run<'a>(
{value_str} {value_str}
"}?; "}?;
// encode, then decode. This ensures that the scale value is of the correct shape for the param: // encode, then decode. This ensures that the scale value is of the correct shape for the param:
let bytes = value.encode_as_type(ty.ty, metadata.types())?; let bytes = value.encode_as_type(&ty.ty, metadata.types())?;
let value = Value::decode_as_type(&mut &bytes[..], ty.ty, metadata.types())? let value = Value::decode_as_type(&mut &bytes[..], &ty.ty, metadata.types())?;
.map_context(|_| ());
Ok(value) Ok(value)
}) })
.collect::<color_eyre::Result<Vec<Value>>>()?; .collect::<color_eyre::Result<Vec<Value>>>()?;
+56 -45
View File
@@ -476,12 +476,12 @@ dependencies = [
[[package]] [[package]]
name = "darling" name = "darling"
version = "0.20.6" version = "0.20.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c376d08ea6aa96aafe61237c7200d1241cb177b7d3a542d791f2d118e9cbb955" checksum = "54e36fcd13ed84ffdfda6f5be89b31287cbb80c439841fe69e04841435464391"
dependencies = [ dependencies = [
"darling_core 0.20.6", "darling_core 0.20.8",
"darling_macro 0.20.6", "darling_macro 0.20.8",
] ]
[[package]] [[package]]
@@ -500,9 +500,9 @@ dependencies = [
[[package]] [[package]]
name = "darling_core" name = "darling_core"
version = "0.20.6" version = "0.20.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33043dcd19068b8192064c704b3f83eb464f91f1ff527b44a4e2b08d9cdb8855" checksum = "9c2cf1c23a687a1feeb728783b993c4e1ad83d99f351801977dd809b48d0a70f"
dependencies = [ dependencies = [
"fnv", "fnv",
"ident_case", "ident_case",
@@ -525,11 +525,11 @@ dependencies = [
[[package]] [[package]]
name = "darling_macro" name = "darling_macro"
version = "0.20.6" version = "0.20.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5a91391accf613803c2a9bf9abccdbaa07c54b4244a5b64883f9c3c137c86be" checksum = "a668eda54683121533a393014d8692171709ff57a7d61f187b6e782719f8933f"
dependencies = [ dependencies = [
"darling_core 0.20.6", "darling_core 0.20.8",
"quote", "quote",
"syn 2.0.48", "syn 2.0.48",
] ]
@@ -1329,9 +1329,9 @@ checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.68" version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "406cda4b368d531c842222cf9d2600a9a4acce8d29423695379c6868a143a9ee" checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
dependencies = [ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
@@ -1713,18 +1713,18 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]] [[package]]
name = "pin-project" name = "pin-project"
version = "1.1.4" version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0302c4a0442c456bd56f841aee5c3bfd17967563f6fadc9ceb9f9c23cf3807e0" checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3"
dependencies = [ dependencies = [
"pin-project-internal", "pin-project-internal",
] ]
[[package]] [[package]]
name = "pin-project-internal" name = "pin-project-internal"
version = "1.1.4" version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "266c042b60c9c76b8d53061e52b2e0d1116abc57cefc8c5cd671619a56ac3690" checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -2084,38 +2084,38 @@ checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
[[package]] [[package]]
name = "scale-bits" name = "scale-bits"
version = "0.4.0" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "036575c29af9b6e4866ffb7fa055dbf623fe7a9cc159b33786de6013a6969d89" checksum = "662d10dcd57b1c2a3c41c9cf68f71fb09747ada1ea932ad961aca7e2ca28315f"
dependencies = [ dependencies = [
"parity-scale-codec", "parity-scale-codec",
"scale-info", "scale-info",
"scale-type-resolver",
"serde", "serde",
] ]
[[package]] [[package]]
name = "scale-decode" name = "scale-decode"
version = "0.10.0" version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7caaf753f8ed1ab4752c6afb20174f03598c664724e0e32628e161c21000ff76" checksum = "afc79ba56a1c742f5aeeed1f1801f3edf51f7e818f0a54582cac6f131364ea7b"
dependencies = [ dependencies = [
"derive_more", "derive_more",
"parity-scale-codec", "parity-scale-codec",
"primitive-types", "primitive-types",
"scale-bits", "scale-bits",
"scale-decode-derive", "scale-decode-derive",
"scale-info", "scale-type-resolver",
"smallvec", "smallvec",
] ]
[[package]] [[package]]
name = "scale-decode-derive" name = "scale-decode-derive"
version = "0.10.0" version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3475108a1b62c7efd1b5c65974f30109a598b2f45f23c9ae030acb9686966db" checksum = "5398fdb3c7bea3cb419bac4983aadacae93fe1a7b5f693f4ebd98c3821aad7a5"
dependencies = [ dependencies = [
"darling 0.14.4", "darling 0.14.4",
"proc-macro-crate 1.3.1",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 1.0.109", "syn 1.0.109",
@@ -2123,24 +2123,24 @@ dependencies = [
[[package]] [[package]]
name = "scale-encode" name = "scale-encode"
version = "0.5.0" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d70cb4b29360105483fac1ed567ff95d65224a14dd275b6303ed0a654c78de5" checksum = "628800925a33794fb5387781b883b5e14d130fece9af5a63613867b8de07c5c7"
dependencies = [ dependencies = [
"derive_more", "derive_more",
"parity-scale-codec", "parity-scale-codec",
"primitive-types", "primitive-types",
"scale-bits", "scale-bits",
"scale-encode-derive", "scale-encode-derive",
"scale-info", "scale-type-resolver",
"smallvec", "smallvec",
] ]
[[package]] [[package]]
name = "scale-encode-derive" name = "scale-encode-derive"
version = "0.5.0" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "995491f110efdc6bea96d6a746140e32bfceb4ea47510750a5467295a4707a25" checksum = "7a304e1af7cdfbe7a24e08b012721456cc8cecdedadc14b3d10513eada63233c"
dependencies = [ dependencies = [
"darling 0.14.4", "darling 0.14.4",
"proc-macro-crate 1.3.1", "proc-macro-crate 1.3.1",
@@ -2176,10 +2176,20 @@ dependencies = [
] ]
[[package]] [[package]]
name = "scale-typegen" name = "scale-type-resolver"
version = "0.1.1" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00860983481ac590ac87972062909bef0d6a658013b592ccc0f2feb272feab11" checksum = "10b800069bfd43374e0f96f653e0d46882a2cb16d6d961ac43bea80f26c76843"
dependencies = [
"scale-info",
"smallvec",
]
[[package]]
name = "scale-typegen"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d6108609f017741c78d35967c7afe4aeaa3999b848282581041428e10d23b63"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -2190,9 +2200,9 @@ dependencies = [
[[package]] [[package]]
name = "scale-value" name = "scale-value"
version = "0.13.0" version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58223c7691bf0bd46b43c9aea6f0472d1067f378d574180232358d7c6e0a8089" checksum = "c07ccfee963104335c971aaf8b7b0e749be8569116322df23f1f75c4ca9e4a28"
dependencies = [ dependencies = [
"base58", "base58",
"blake2", "blake2",
@@ -2204,6 +2214,7 @@ dependencies = [
"scale-decode", "scale-decode",
"scale-encode", "scale-encode",
"scale-info", "scale-info",
"scale-type-resolver",
"serde", "serde",
"yap", "yap",
] ]
@@ -2659,7 +2670,7 @@ dependencies = [
name = "subxt-macro" name = "subxt-macro"
version = "0.34.0" version = "0.34.0"
dependencies = [ dependencies = [
"darling 0.20.6", "darling 0.20.8",
"parity-scale-codec", "parity-scale-codec",
"proc-macro-error", "proc-macro-error",
"quote", "quote",
@@ -3011,9 +3022,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.91" version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"wasm-bindgen-macro", "wasm-bindgen-macro",
@@ -3021,9 +3032,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-backend" name = "wasm-bindgen-backend"
version = "0.2.91" version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b" checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
dependencies = [ dependencies = [
"bumpalo", "bumpalo",
"log", "log",
@@ -3048,9 +3059,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro" name = "wasm-bindgen-macro"
version = "0.2.91" version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed" checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
dependencies = [ dependencies = [
"quote", "quote",
"wasm-bindgen-macro-support", "wasm-bindgen-macro-support",
@@ -3058,9 +3069,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro-support" name = "wasm-bindgen-macro-support"
version = "0.2.91" version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66" checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -3071,9 +3082,9 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-shared" name = "wasm-bindgen-shared"
version = "0.2.91" version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
[[package]] [[package]]
name = "wasm-example" name = "wasm-example"
@@ -3135,9 +3146,9 @@ dependencies = [
[[package]] [[package]]
name = "web-sys" name = "web-sys"
version = "0.3.67" version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58cd2333b6e0be7a39605f0e255892fd7418a682d8da8fe042fe25128794d2ed" checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef"
dependencies = [ dependencies = [
"js-sys", "js-sys",
"wasm-bindgen", "wasm-bindgen",
+2 -2
View File
@@ -9,10 +9,10 @@ edition = "2021"
futures = "0.3.28" futures = "0.3.28"
subxt = { path = "../../subxt", default-features = false, features = ["jsonrpsee", "web"], target_arch = "wasm32" } subxt = { path = "../../subxt", default-features = false, features = ["jsonrpsee", "web"], target_arch = "wasm32" }
yew = { version = "0.20.0", features = ["csr"] } yew = { version = "0.20.0", features = ["csr"] }
web-sys = "0.3.63" web-sys = "0.3.69"
hex = "0.4.3" hex = "0.4.3"
yew-router = "0.17.0" yew-router = "0.17.0"
js-sys = "0.3.63" js-sys = "0.3.69"
wasm-bindgen = "0.2.86" wasm-bindgen = "0.2.86"
wasm-bindgen-futures = "0.4.36" wasm-bindgen-futures = "0.4.36"
anyhow = "1.0.71" anyhow = "1.0.71"
+21 -21
View File
@@ -245,27 +245,27 @@ where
// Skip over the address, signature and extra fields. // Skip over the address, signature and extra fields.
scale_decode::visitor::decode_with_visitor( scale_decode::visitor::decode_with_visitor(
cursor, cursor,
ids.address, &ids.address,
metadata.types(), metadata.types(),
scale_decode::visitor::IgnoreVisitor, scale_decode::visitor::IgnoreVisitor::new(),
) )
.map_err(scale_decode::Error::from)?; .map_err(scale_decode::Error::from)?;
let address_end_idx = bytes.len() - cursor.len(); let address_end_idx = bytes.len() - cursor.len();
scale_decode::visitor::decode_with_visitor( scale_decode::visitor::decode_with_visitor(
cursor, cursor,
ids.signature, &ids.signature,
metadata.types(), metadata.types(),
scale_decode::visitor::IgnoreVisitor, scale_decode::visitor::IgnoreVisitor::new(),
) )
.map_err(scale_decode::Error::from)?; .map_err(scale_decode::Error::from)?;
let signature_end_idx = bytes.len() - cursor.len(); let signature_end_idx = bytes.len() - cursor.len();
scale_decode::visitor::decode_with_visitor( scale_decode::visitor::decode_with_visitor(
cursor, cursor,
ids.extra, &ids.extra,
metadata.types(), metadata.types(),
scale_decode::visitor::IgnoreVisitor, scale_decode::visitor::IgnoreVisitor::new(),
) )
.map_err(scale_decode::Error::from)?; .map_err(scale_decode::Error::from)?;
let extra_end_idx = bytes.len() - cursor.len(); let extra_end_idx = bytes.len() - cursor.len();
@@ -420,9 +420,7 @@ where
/// Decode and provide the extrinsic fields back in the form of a [`scale_value::Composite`] /// Decode and provide the extrinsic fields back in the form of a [`scale_value::Composite`]
/// type which represents the named or unnamed fields that were present in the extrinsic. /// type which represents the named or unnamed fields that were present in the extrinsic.
pub fn field_values( pub fn field_values(&self) -> Result<scale_value::Composite<u32>, Error> {
&self,
) -> Result<scale_value::Composite<scale_value::scale::TypeId>, Error> {
let bytes = &mut self.field_bytes(); let bytes = &mut self.field_bytes();
let extrinsic_metadata = self.extrinsic_metadata()?; let extrinsic_metadata = self.extrinsic_metadata()?;
@@ -430,12 +428,9 @@ where
.variant .variant
.fields .fields
.iter() .iter()
.map(|f| scale_decode::Field::new(f.ty.id, f.name.as_deref())); .map(|f| scale_decode::Field::new(&f.ty.id, f.name.as_deref()));
let decoded = <scale_value::Composite<scale_value::scale::TypeId>>::decode_as_fields( let decoded =
bytes, scale_value::scale::decode_as_fields(bytes, &mut fields, self.metadata.types())?;
&mut fields,
self.metadata.types(),
)?;
Ok(decoded) Ok(decoded)
} }
@@ -451,7 +446,7 @@ where
.variant .variant
.fields .fields
.iter() .iter()
.map(|f| scale_decode::Field::new(f.ty.id, f.name.as_deref())); .map(|f| scale_decode::Field::new(&f.ty.id, f.name.as_deref()));
let decoded = let decoded =
E::decode_as_fields(&mut self.field_bytes(), &mut fields, self.metadata.types())?; E::decode_as_fields(&mut self.field_bytes(), &mut fields, self.metadata.types())?;
Ok(Some(decoded)) Ok(Some(decoded))
@@ -466,7 +461,7 @@ where
pub fn as_root_extrinsic<E: DecodeAsType>(&self) -> Result<E, Error> { pub fn as_root_extrinsic<E: DecodeAsType>(&self) -> Result<E, Error> {
let decoded = E::decode_as_type( let decoded = E::decode_as_type(
&mut &self.call_bytes()[..], &mut &self.call_bytes()[..],
self.metadata.outer_enums().call_enum_ty(), &self.metadata.outer_enums().call_enum_ty(),
self.metadata.types(), self.metadata.types(),
)?; )?;
@@ -651,9 +646,9 @@ impl<'a, T: Config> ExtrinsicSignedExtensions<'a, T> {
let cursor = &mut &bytes[byte_start_idx..]; let cursor = &mut &bytes[byte_start_idx..];
if let Err(err) = scale_decode::visitor::decode_with_visitor( if let Err(err) = scale_decode::visitor::decode_with_visitor(
cursor, cursor,
ty_id, &ty_id,
metadata.types(), metadata.types(),
scale_decode::visitor::IgnoreVisitor, scale_decode::visitor::IgnoreVisitor::new(),
) )
.map_err(|e| Error::Decode(e.into())) .map_err(|e| Error::Decode(e.into()))
{ {
@@ -748,7 +743,12 @@ impl<'a, T: Config> ExtrinsicSignedExtension<'a, T> {
/// Signed Extension as a [`scale_value::Value`] /// Signed Extension as a [`scale_value::Value`]
pub fn value(&self) -> Result<DecodedValue, Error> { pub fn value(&self) -> Result<DecodedValue, Error> {
self.as_type() let value = scale_value::scale::decode_as_type(
&mut &self.bytes[..],
&self.ty_id,
self.metadata.types(),
)?;
Ok(value)
} }
/// Decodes the bytes of this Signed Extension into its associated `Decoded` type. /// Decodes the bytes of this Signed Extension into its associated `Decoded` type.
@@ -762,7 +762,7 @@ impl<'a, T: Config> ExtrinsicSignedExtension<'a, T> {
} }
fn as_type<E: DecodeAsType>(&self) -> Result<E, Error> { fn as_type<E: DecodeAsType>(&self) -> Result<E, Error> {
let value = E::decode_as_type(&mut &self.bytes[..], self.ty_id, self.metadata.types())?; let value = E::decode_as_type(&mut &self.bytes[..], &self.ty_id, self.metadata.types())?;
Ok(value) Ok(value)
} }
} }
+4 -4
View File
@@ -17,7 +17,7 @@ pub use scale_value::{At, Value};
/// regarding what type was used to decode each part of it. This implements /// regarding what type was used to decode each part of it. This implements
/// [`crate::metadata::DecodeWithMetadata`], and is used as a return type /// [`crate::metadata::DecodeWithMetadata`], and is used as a return type
/// for dynamic requests. /// for dynamic requests.
pub type DecodedValue = scale_value::Value<scale_value::scale::TypeId>; pub type DecodedValue = scale_value::Value<u32>;
// Submit dynamic transactions. // Submit dynamic transactions.
pub use crate::tx::dynamic as tx; pub use crate::tx::dynamic as tx;
@@ -68,9 +68,9 @@ impl DecodedValueThunk {
} }
/// Decode the SCALE encoded storage entry into a dynamic [`DecodedValue`] type. /// Decode the SCALE encoded storage entry into a dynamic [`DecodedValue`] type.
pub fn to_value(&self) -> Result<DecodedValue, Error> { pub fn to_value(&self) -> Result<DecodedValue, Error> {
let val = DecodedValue::decode_as_type( let val = scale_value::scale::decode_as_type(
&mut &*self.scale_bytes, &mut &*self.scale_bytes,
self.type_id, &self.type_id,
self.metadata.types(), self.metadata.types(),
)?; )?;
Ok(val) Ok(val)
@@ -79,7 +79,7 @@ impl DecodedValueThunk {
pub fn as_type<T: DecodeAsType>(&self) -> Result<T, scale_decode::Error> { pub fn as_type<T: DecodeAsType>(&self) -> Result<T, scale_decode::Error> {
T::decode_as_type( T::decode_as_type(
&mut &self.scale_bytes[..], &mut &self.scale_bytes[..],
self.type_id, &self.type_id,
self.metadata.types(), self.metadata.types(),
) )
} }
+14 -10
View File
@@ -7,8 +7,9 @@
use crate::metadata::{DecodeWithMetadata, Metadata}; use crate::metadata::{DecodeWithMetadata, Metadata};
use core::fmt::Debug; use core::fmt::Debug;
use scale_decode::{visitor::DecodeAsTypeResult, DecodeAsType}; use scale_decode::{visitor::DecodeAsTypeResult, DecodeAsType, TypeResolver};
use std::borrow::Cow;
use std::{borrow::Cow, marker::PhantomData};
use super::{Error, MetadataError}; use super::{Error, MetadataError};
@@ -209,7 +210,7 @@ impl ModuleError {
pub fn as_root_error<E: DecodeAsType>(&self) -> Result<E, Error> { pub fn as_root_error<E: DecodeAsType>(&self) -> Result<E, Error> {
let decoded = E::decode_as_type( let decoded = E::decode_as_type(
&mut &self.bytes[..], &mut &self.bytes[..],
self.metadata.outer_enums().error_enum_ty(), &self.metadata.outer_enums().error_enum_ty(),
self.metadata.types(), self.metadata.types(),
)?; )?;
@@ -262,24 +263,27 @@ impl DispatchError {
// a legacy format of 2 bytes, or a newer format of 5 bytes. So, just grab the bytes // a legacy format of 2 bytes, or a newer format of 5 bytes. So, just grab the bytes
// out when decoding to manually work with them. // out when decoding to manually work with them.
struct DecodedModuleErrorBytes(Vec<u8>); struct DecodedModuleErrorBytes(Vec<u8>);
struct DecodedModuleErrorBytesVisitor; struct DecodedModuleErrorBytesVisitor<R: TypeResolver>(PhantomData<R>);
impl scale_decode::Visitor for DecodedModuleErrorBytesVisitor { impl<R: TypeResolver> scale_decode::Visitor for DecodedModuleErrorBytesVisitor<R> {
type Error = scale_decode::Error; type Error = scale_decode::Error;
type Value<'scale, 'info> = DecodedModuleErrorBytes; type Value<'scale, 'info> = DecodedModuleErrorBytes;
type TypeResolver = R;
fn unchecked_decode_as_type<'scale, 'info>( fn unchecked_decode_as_type<'scale, 'info>(
self, self,
input: &mut &'scale [u8], input: &mut &'scale [u8],
_type_id: scale_decode::visitor::TypeId, _type_id: &R::TypeId,
_types: &'info scale_info::PortableRegistry, _types: &'info R,
) -> DecodeAsTypeResult<Self, Result<Self::Value<'scale, 'info>, Self::Error>> ) -> DecodeAsTypeResult<Self, Result<Self::Value<'scale, 'info>, Self::Error>>
{ {
DecodeAsTypeResult::Decoded(Ok(DecodedModuleErrorBytes(input.to_vec()))) DecodeAsTypeResult::Decoded(Ok(DecodedModuleErrorBytes(input.to_vec())))
} }
} }
impl scale_decode::IntoVisitor for DecodedModuleErrorBytes { impl scale_decode::IntoVisitor for DecodedModuleErrorBytes {
type Visitor = DecodedModuleErrorBytesVisitor; type AnyVisitor<R: TypeResolver> = DecodedModuleErrorBytesVisitor<R>;
fn into_visitor() -> Self::Visitor { fn into_visitor<R: TypeResolver>() -> DecodedModuleErrorBytesVisitor<R> {
DecodedModuleErrorBytesVisitor DecodedModuleErrorBytesVisitor(PhantomData)
} }
} }
+6
View File
@@ -99,6 +99,12 @@ impl From<std::convert::Infallible> for Error {
} }
} }
impl From<scale_decode::visitor::DecodeError> for Error {
fn from(value: scale_decode::visitor::DecodeError) -> Self {
Error::Decode(value.into())
}
}
impl Error { impl Error {
/// Checks whether the error was caused by a RPC re-connection. /// Checks whether the error was caused by a RPC re-connection.
pub fn is_disconnected_will_reconnect(&self) -> bool { pub fn is_disconnected_will_reconnect(&self) -> bool {
+8 -14
View File
@@ -228,9 +228,9 @@ impl<T: Config> EventDetails<T> {
// Skip over the bytes for this field: // Skip over the bytes for this field:
scale_decode::visitor::decode_with_visitor( scale_decode::visitor::decode_with_visitor(
input, input,
field_metadata.ty.id, &field_metadata.ty.id,
metadata.types(), metadata.types(),
scale_decode::visitor::IgnoreVisitor, scale_decode::visitor::IgnoreVisitor::new(),
) )
.map_err(scale_decode::Error::from)?; .map_err(scale_decode::Error::from)?;
} }
@@ -321,9 +321,7 @@ impl<T: Config> EventDetails<T> {
/// Decode and provide the event fields back in the form of a [`scale_value::Composite`] /// Decode and provide the event fields back in the form of a [`scale_value::Composite`]
/// type which represents the named or unnamed fields that were present in the event. /// type which represents the named or unnamed fields that were present in the event.
pub fn field_values( pub fn field_values(&self) -> Result<scale_value::Composite<u32>, Error> {
&self,
) -> Result<scale_value::Composite<scale_value::scale::TypeId>, Error> {
let bytes = &mut self.field_bytes(); let bytes = &mut self.field_bytes();
let event_metadata = self.event_metadata(); let event_metadata = self.event_metadata();
@@ -331,14 +329,10 @@ impl<T: Config> EventDetails<T> {
.variant .variant
.fields .fields
.iter() .iter()
.map(|f| scale_decode::Field::new(f.ty.id, f.name.as_deref())); .map(|f| scale_decode::Field::new(&f.ty.id, f.name.as_deref()));
use scale_decode::DecodeAsFields; let decoded =
let decoded = <scale_value::Composite<scale_value::scale::TypeId>>::decode_as_fields( scale_value::scale::decode_as_fields(bytes, &mut fields, self.metadata.types())?;
bytes,
&mut fields,
self.metadata.types(),
)?;
Ok(decoded) Ok(decoded)
} }
@@ -352,7 +346,7 @@ impl<T: Config> EventDetails<T> {
.variant .variant
.fields .fields
.iter() .iter()
.map(|f| scale_decode::Field::new(f.ty.id, f.name.as_deref())); .map(|f| scale_decode::Field::new(&f.ty.id, f.name.as_deref()));
let decoded = let decoded =
E::decode_as_fields(&mut self.field_bytes(), &mut fields, self.metadata.types())?; E::decode_as_fields(&mut self.field_bytes(), &mut fields, self.metadata.types())?;
Ok(Some(decoded)) Ok(Some(decoded))
@@ -369,7 +363,7 @@ impl<T: Config> EventDetails<T> {
let decoded = E::decode_as_type( let decoded = E::decode_as_type(
&mut &bytes[..], &mut &bytes[..],
self.metadata.outer_enums().event_enum_ty(), &self.metadata.outer_enums().event_enum_ty(),
self.metadata.types(), self.metadata.types(),
)?; )?;
+2 -2
View File
@@ -21,7 +21,7 @@ impl<T: scale_decode::DecodeAsType> DecodeWithMetadata for T {
type_id: u32, type_id: u32,
metadata: &Metadata, metadata: &Metadata,
) -> Result<T, Error> { ) -> Result<T, Error> {
let val = T::decode_as_type(bytes, type_id, metadata.types())?; let val = T::decode_as_type(bytes, &type_id, metadata.types())?;
Ok(val) Ok(val)
} }
} }
@@ -45,7 +45,7 @@ impl<T: scale_encode::EncodeAsType> EncodeWithMetadata for T {
metadata: &Metadata, metadata: &Metadata,
bytes: &mut Vec<u8>, bytes: &mut Vec<u8>,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.encode_as_type_to(type_id, metadata.types(), bytes)?; self.encode_as_type_to(&type_id, metadata.types(), bytes)?;
Ok(()) Ok(())
} }
} }
+1 -1
View File
@@ -103,7 +103,7 @@ impl<ArgsData: EncodeAsFields, ReturnTy: DecodeWithMetadata> RuntimeApiPayload
.ok_or_else(|| MetadataError::RuntimeMethodNotFound((*self.method_name).to_owned()))?; .ok_or_else(|| MetadataError::RuntimeMethodNotFound((*self.method_name).to_owned()))?;
let mut fields = api_method let mut fields = api_method
.inputs() .inputs()
.map(|input| scale_encode::Field::named(input.ty, &input.name)); .map(|input| scale_encode::Field::named(&input.ty, &input.name));
self.args_data self.args_data
.encode_as_fields_to(&mut fields, metadata.types(), out)?; .encode_as_fields_to(&mut fields, metadata.types(), out)?;
+12 -6
View File
@@ -2,7 +2,7 @@ use crate::{
error::{Error, MetadataError, StorageAddressError}, error::{Error, MetadataError, StorageAddressError},
utils::{Encoded, Static}, utils::{Encoded, Static},
}; };
use scale_decode::{visitor::IgnoreVisitor, DecodeAsType}; use scale_decode::visitor::IgnoreVisitor;
use scale_encode::EncodeAsType; use scale_encode::EncodeAsType;
use scale_info::{PortableRegistry, TypeDef}; use scale_info::{PortableRegistry, TypeDef};
use scale_value::Value; use scale_value::Value;
@@ -203,7 +203,7 @@ impl<K: ?Sized> StorageKey for StaticStorageKey<K> {
types: &PortableRegistry, types: &PortableRegistry,
) -> Result<(), Error> { ) -> Result<(), Error> {
let (hasher, ty_id) = hashers.next_or_err()?; let (hasher, ty_id) = hashers.next_or_err()?;
let encoded_value = self.bytes.encode_as_type(ty_id, types)?; let encoded_value = self.bytes.encode_as_type(&ty_id, types)?;
hash_bytes(&encoded_value, hasher, bytes); hash_bytes(&encoded_value, hasher, bytes);
Ok(()) Ok(())
} }
@@ -242,7 +242,7 @@ impl StorageKey for Vec<scale_value::Value> {
) -> Result<(), Error> { ) -> Result<(), Error> {
for value in self.iter() { for value in self.iter() {
let (hasher, ty_id) = hashers.next_or_err()?; let (hasher, ty_id) = hashers.next_or_err()?;
let encoded_value = value.encode_as_type(ty_id, types)?; let encoded_value = value.encode_as_type(&ty_id, types)?;
hash_bytes(&encoded_value, hasher, bytes); hash_bytes(&encoded_value, hasher, bytes);
} }
Ok(()) Ok(())
@@ -260,7 +260,8 @@ impl StorageKey for Vec<scale_value::Value> {
for (hasher, ty_id) in hashers.by_ref() { for (hasher, ty_id) in hashers.by_ref() {
match consume_hash_returning_key_bytes(bytes, hasher, ty_id, types)? { match consume_hash_returning_key_bytes(bytes, hasher, ty_id, types)? {
Some(value_bytes) => { Some(value_bytes) => {
let value = Value::decode_as_type(&mut &*value_bytes, ty_id, types)?; let value =
scale_value::scale::decode_as_type(&mut &*value_bytes, &ty_id, types)?;
result.push(value.remove_context()); result.push(value.remove_context());
} }
None => { None => {
@@ -296,8 +297,13 @@ fn consume_hash_returning_key_bytes<'a>(
// Now, find the bytes representing the key, consuming them. // Now, find the bytes representing the key, consuming them.
let before_key = *bytes; let before_key = *bytes;
if hasher.ends_with_key() { if hasher.ends_with_key() {
scale_decode::visitor::decode_with_visitor(bytes, ty_id, types, IgnoreVisitor) scale_decode::visitor::decode_with_visitor(
.map_err(|err| Error::Decode(err.into()))?; bytes,
&ty_id,
types,
IgnoreVisitor::<PortableRegistry>::new(),
)
.map_err(|err| Error::Decode(err.into()))?;
// Return the key bytes, having advanced the input cursor past them. // Return the key bytes, having advanced the input cursor past them.
let key_bytes = &before_key[..before_key.len() - bytes.len()]; let key_bytes = &before_key[..before_key.len() - bytes.len()];
+1 -1
View File
@@ -28,6 +28,6 @@ pub use self::{
PartialExtrinsic, SubmittableExtrinsic, TransactionInvalid, TransactionUnknown, TxClient, PartialExtrinsic, SubmittableExtrinsic, TransactionInvalid, TransactionUnknown, TxClient,
ValidationResult, ValidationResult,
}, },
tx_payload::{dynamic, BoxedPayload, DynamicPayload, Payload, TxPayload}, tx_payload::{dynamic, DynamicPayload, Payload, TxPayload},
tx_progress::{TxInBlock, TxProgress, TxStatus}, tx_progress::{TxInBlock, TxProgress, TxStatus},
}; };
+2 -19
View File
@@ -14,7 +14,7 @@ use codec::Encode;
use derivative::Derivative; use derivative::Derivative;
use scale_encode::EncodeAsFields; use scale_encode::EncodeAsFields;
use scale_value::{Composite, ValueDef, Variant}; use scale_value::{Composite, ValueDef, Variant};
use std::{borrow::Cow, sync::Arc}; use std::borrow::Cow;
/// This represents a transaction payload that can be submitted /// This represents a transaction payload that can be submitted
/// to a node. /// to a node.
@@ -65,10 +65,6 @@ pub struct Payload<CallData> {
validation_hash: Option<[u8; 32]>, validation_hash: Option<[u8; 32]>,
} }
/// A boxed transaction payload.
// Dev Note: Arc used to enable easy cloning (given that we can't have dyn Clone).
pub type BoxedPayload = Payload<Arc<dyn EncodeAsFields + Send + Sync + 'static>>;
/// The type of a payload typically used for dynamic transaction payloads. /// The type of a payload typically used for dynamic transaction payloads.
pub type DynamicPayload = Payload<Composite<()>>; pub type DynamicPayload = Payload<Composite<()>>;
@@ -104,19 +100,6 @@ impl<CallData> Payload<CallData> {
} }
} }
/// Box the payload.
pub fn boxed(self) -> BoxedPayload
where
CallData: EncodeAsFields + Send + Sync + 'static,
{
BoxedPayload {
pallet_name: self.pallet_name,
call_name: self.call_name,
call_data: Arc::new(self.call_data),
validation_hash: self.validation_hash,
}
}
/// Do not validate this call prior to submitting it. /// Do not validate this call prior to submitting it.
pub fn unvalidated(self) -> Self { pub fn unvalidated(self) -> Self {
Self { Self {
@@ -174,7 +157,7 @@ impl<CallData: EncodeAsFields> TxPayload for Payload<CallData> {
let mut fields = call let mut fields = call
.fields .fields
.iter() .iter()
.map(|f| scale_encode::Field::new(f.ty.id, f.name.as_deref())); .map(|f| scale_encode::Field::new(&f.ty.id, f.name.as_deref()));
self.call_data self.call_data
.encode_as_fields_to(&mut fields, metadata.types(), out)?; .encode_as_fields_to(&mut fields, metadata.types(), out)?;
+19 -20
View File
@@ -9,7 +9,8 @@ use scale_bits::{
scale::format::{Format, OrderFormat, StoreFormat}, scale::format::{Format, OrderFormat, StoreFormat},
Bits, Bits,
}; };
use scale_decode::IntoVisitor; use scale_decode::{IntoVisitor, TypeResolver};
use std::marker::PhantomData; use std::marker::PhantomData;
/// Associates `bitvec::store::BitStore` trait with corresponding, type-erased `scale_bits::StoreFormat` enum. /// Associates `bitvec::store::BitStore` trait with corresponding, type-erased `scale_bits::StoreFormat` enum.
@@ -144,45 +145,43 @@ impl<Store: BitStore, Order: BitOrder> codec::Encode for DecodedBits<Store, Orde
} }
#[doc(hidden)] #[doc(hidden)]
pub struct DecodedBitsVisitor<S, O>(std::marker::PhantomData<(S, O)>); pub struct DecodedBitsVisitor<S, O, R: TypeResolver>(std::marker::PhantomData<(S, O, R)>);
impl<Store, Order> scale_decode::Visitor for DecodedBitsVisitor<Store, Order> {
impl<Store, Order, R: TypeResolver> scale_decode::Visitor for DecodedBitsVisitor<Store, Order, R> {
type Value<'scale, 'info> = DecodedBits<Store, Order>; type Value<'scale, 'info> = DecodedBits<Store, Order>;
type Error = scale_decode::Error; type Error = scale_decode::Error;
type TypeResolver = R;
fn unchecked_decode_as_type<'scale, 'info>( fn unchecked_decode_as_type<'scale, 'info>(
self, self,
input: &mut &'scale [u8], input: &mut &'scale [u8],
type_id: scale_decode::visitor::TypeId, type_id: &R::TypeId,
types: &'info scale_info::PortableRegistry, types: &'info R,
) -> scale_decode::visitor::DecodeAsTypeResult< ) -> scale_decode::visitor::DecodeAsTypeResult<
Self, Self,
Result<Self::Value<'scale, 'info>, Self::Error>, Result<Self::Value<'scale, 'info>, Self::Error>,
> { > {
let res = scale_decode::visitor::decode_with_visitor( let res =
input, scale_decode::visitor::decode_with_visitor(input, type_id, types, Bits::into_visitor())
type_id.0, .map(|bits| DecodedBits {
types, bits,
Bits::into_visitor(), _marker: PhantomData,
) });
.map(|bits| DecodedBits {
bits,
_marker: PhantomData,
});
scale_decode::visitor::DecodeAsTypeResult::Decoded(res) scale_decode::visitor::DecodeAsTypeResult::Decoded(res)
} }
} }
impl<Store, Order> scale_decode::IntoVisitor for DecodedBits<Store, Order> { impl<Store, Order> scale_decode::IntoVisitor for DecodedBits<Store, Order> {
type Visitor = DecodedBitsVisitor<Store, Order>; type AnyVisitor<R: scale_decode::TypeResolver> = DecodedBitsVisitor<Store, Order, R>;
fn into_visitor() -> Self::Visitor { fn into_visitor<R: TypeResolver>() -> DecodedBitsVisitor<Store, Order, R> {
DecodedBitsVisitor(PhantomData) DecodedBitsVisitor(PhantomData)
} }
} }
impl<Store, Order> scale_encode::EncodeAsType for DecodedBits<Store, Order> { impl<Store, Order> scale_encode::EncodeAsType for DecodedBits<Store, Order> {
fn encode_as_type_to( fn encode_as_type_to<R: TypeResolver>(
&self, &self,
type_id: u32, type_id: &R::TypeId,
types: &scale_info::PortableRegistry, types: &R,
out: &mut Vec<u8>, out: &mut Vec<u8>,
) -> Result<(), scale_encode::Error> { ) -> Result<(), scale_encode::Error> {
self.bits.encode_as_type_to(type_id, types, out) self.bits.encode_as_type_to(type_id, types, out)
+11 -10
View File
@@ -3,7 +3,7 @@
// see LICENSE for license details. // see LICENSE for license details.
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use scale_decode::{visitor::DecodeAsTypeResult, IntoVisitor, Visitor}; use scale_decode::{visitor::DecodeAsTypeResult, IntoVisitor, TypeResolver, Visitor};
use scale_encode::EncodeAsType; use scale_encode::EncodeAsType;
/// If the type inside this implements [`Encode`], this will implement [`scale_encode::EncodeAsType`]. /// If the type inside this implements [`Encode`], this will implement [`scale_encode::EncodeAsType`].
@@ -18,10 +18,10 @@ use scale_encode::EncodeAsType;
pub struct Static<T>(pub T); pub struct Static<T>(pub T);
impl<T: Encode> EncodeAsType for Static<T> { impl<T: Encode> EncodeAsType for Static<T> {
fn encode_as_type_to( fn encode_as_type_to<R: TypeResolver>(
&self, &self,
_type_id: u32, _type_id: &R::TypeId,
_types: &scale_decode::PortableRegistry, _types: &R,
out: &mut Vec<u8>, out: &mut Vec<u8>,
) -> Result<(), scale_encode::Error> { ) -> Result<(), scale_encode::Error> {
self.0.encode_to(out); self.0.encode_to(out);
@@ -29,17 +29,18 @@ impl<T: Encode> EncodeAsType for Static<T> {
} }
} }
pub struct StaticDecodeAsTypeVisitor<T>(std::marker::PhantomData<T>); pub struct StaticDecodeAsTypeVisitor<T, R>(std::marker::PhantomData<(T, R)>);
impl<T: Decode> Visitor for StaticDecodeAsTypeVisitor<T> { impl<T: Decode, R: TypeResolver> Visitor for StaticDecodeAsTypeVisitor<T, R> {
type Value<'scale, 'info> = Static<T>; type Value<'scale, 'info> = Static<T>;
type Error = scale_decode::Error; type Error = scale_decode::Error;
type TypeResolver = R;
fn unchecked_decode_as_type<'scale, 'info>( fn unchecked_decode_as_type<'scale, 'info>(
self, self,
input: &mut &'scale [u8], input: &mut &'scale [u8],
_type_id: scale_decode::visitor::TypeId, _type_id: &R::TypeId,
_types: &'info scale_info::PortableRegistry, _types: &'info R,
) -> DecodeAsTypeResult<Self, Result<Self::Value<'scale, 'info>, Self::Error>> { ) -> DecodeAsTypeResult<Self, Result<Self::Value<'scale, 'info>, Self::Error>> {
use scale_decode::{visitor::DecodeError, Error}; use scale_decode::{visitor::DecodeError, Error};
let decoded = T::decode(input) let decoded = T::decode(input)
@@ -50,8 +51,8 @@ impl<T: Decode> Visitor for StaticDecodeAsTypeVisitor<T> {
} }
impl<T: Decode> IntoVisitor for Static<T> { impl<T: Decode> IntoVisitor for Static<T> {
type Visitor = StaticDecodeAsTypeVisitor<T>; type AnyVisitor<R: TypeResolver> = StaticDecodeAsTypeVisitor<T, R>;
fn into_visitor() -> Self::Visitor { fn into_visitor<R: TypeResolver>() -> StaticDecodeAsTypeVisitor<T, R> {
StaticDecodeAsTypeVisitor(std::marker::PhantomData) StaticDecodeAsTypeVisitor(std::marker::PhantomData)
} }
} }
+16 -13
View File
@@ -12,7 +12,7 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use scale_decode::{visitor::DecodeAsTypeResult, DecodeAsType, IntoVisitor, Visitor}; use scale_decode::{visitor::DecodeAsTypeResult, DecodeAsType, IntoVisitor, TypeResolver, Visitor};
use super::{Encoded, Static}; use super::{Encoded, Static};
@@ -52,10 +52,10 @@ impl<Address, Call, Signature, Extra> Decode
impl<Address, Call, Signature, Extra> scale_encode::EncodeAsType impl<Address, Call, Signature, Extra> scale_encode::EncodeAsType
for UncheckedExtrinsic<Address, Call, Signature, Extra> for UncheckedExtrinsic<Address, Call, Signature, Extra>
{ {
fn encode_as_type_to( fn encode_as_type_to<R: TypeResolver>(
&self, &self,
type_id: u32, type_id: &R::TypeId,
types: &scale_info::PortableRegistry, types: &R,
out: &mut Vec<u8>, out: &mut Vec<u8>,
) -> Result<(), scale_encode::Error> { ) -> Result<(), scale_encode::Error> {
self.0.encode_as_type_to(type_id, types, out) self.0.encode_as_type_to(type_id, types, out)
@@ -78,32 +78,35 @@ impl<Address, Call, Signature, Extra> From<UncheckedExtrinsic<Address, Call, Sig
} }
} }
pub struct UncheckedExtrinsicDecodeAsTypeVisitor<Address, Call, Signature, Extra>( pub struct UncheckedExtrinsicDecodeAsTypeVisitor<Address, Call, Signature, Extra, R: TypeResolver>(
PhantomData<(Address, Call, Signature, Extra)>, PhantomData<(Address, Call, Signature, Extra, R)>,
); );
impl<Address, Call, Signature, Extra> Visitor impl<Address, Call, Signature, Extra, R: TypeResolver> Visitor
for UncheckedExtrinsicDecodeAsTypeVisitor<Address, Call, Signature, Extra> for UncheckedExtrinsicDecodeAsTypeVisitor<Address, Call, Signature, Extra, R>
{ {
type Value<'scale, 'info> = UncheckedExtrinsic<Address, Call, Signature, Extra>; type Value<'scale, 'info> = UncheckedExtrinsic<Address, Call, Signature, Extra>;
type Error = scale_decode::Error; type Error = scale_decode::Error;
type TypeResolver = R;
fn unchecked_decode_as_type<'scale, 'info>( fn unchecked_decode_as_type<'scale, 'info>(
self, self,
input: &mut &'scale [u8], input: &mut &'scale [u8],
type_id: scale_decode::visitor::TypeId, type_id: &R::TypeId,
types: &'info scale_info::PortableRegistry, types: &'info R,
) -> DecodeAsTypeResult<Self, Result<Self::Value<'scale, 'info>, Self::Error>> { ) -> DecodeAsTypeResult<Self, Result<Self::Value<'scale, 'info>, Self::Error>> {
DecodeAsTypeResult::Decoded(Self::Value::decode_as_type(input, type_id.0, types)) DecodeAsTypeResult::Decoded(Self::Value::decode_as_type(input, type_id, types))
} }
} }
impl<Address, Call, Signature, Extra> IntoVisitor impl<Address, Call, Signature, Extra> IntoVisitor
for UncheckedExtrinsic<Address, Call, Signature, Extra> for UncheckedExtrinsic<Address, Call, Signature, Extra>
{ {
type Visitor = UncheckedExtrinsicDecodeAsTypeVisitor<Address, Call, Signature, Extra>; type AnyVisitor<R: TypeResolver> =
UncheckedExtrinsicDecodeAsTypeVisitor<Address, Call, Signature, Extra, R>;
fn into_visitor() -> Self::Visitor { fn into_visitor<R: TypeResolver>(
) -> UncheckedExtrinsicDecodeAsTypeVisitor<Address, Call, Signature, Extra, R> {
UncheckedExtrinsicDecodeAsTypeVisitor(PhantomData) UncheckedExtrinsicDecodeAsTypeVisitor(PhantomData)
} }
} }
+28 -38
View File
@@ -5,7 +5,7 @@
use super::PhantomDataSendSync; use super::PhantomDataSendSync;
use codec::{Compact, Decode, DecodeAll, Encode}; use codec::{Compact, Decode, DecodeAll, Encode};
use derivative::Derivative; use derivative::Derivative;
use scale_decode::{IntoVisitor, Visitor}; use scale_decode::{ext::scale_type_resolver::visitor, IntoVisitor, TypeResolver, Visitor};
use scale_encode::EncodeAsType; use scale_encode::EncodeAsType;
/// A wrapper for any type `T` which implement encode/decode in a way compatible with `Vec<u8>`. /// A wrapper for any type `T` which implement encode/decode in a way compatible with `Vec<u8>`.
@@ -74,57 +74,47 @@ impl<T> WrapperKeepOpaque<T> {
} }
impl<T> EncodeAsType for WrapperKeepOpaque<T> { impl<T> EncodeAsType for WrapperKeepOpaque<T> {
fn encode_as_type_to( fn encode_as_type_to<R: TypeResolver>(
&self, &self,
type_id: u32, type_id: &R::TypeId,
types: &scale_info::PortableRegistry, types: &R,
out: &mut Vec<u8>, out: &mut Vec<u8>,
) -> Result<(), scale_encode::Error> { ) -> Result<(), scale_encode::Error> {
use scale_encode::error::{Error, ErrorKind, Kind}; use scale_encode::error::{Error, ErrorKind, Kind};
let Some(ty) = types.resolve(type_id) else { let visitor = visitor::new(out, |_, _| {
return Err(Error::new(ErrorKind::TypeNotFound(type_id))); // Check that the target shape lines up: any other shape but the composite is wrong.
}; Err(Error::new(ErrorKind::WrongShape {
// Do a basic check that the target shape lines up.
let scale_info::TypeDef::Composite(_) = &ty.type_def else {
return Err(Error::new(ErrorKind::WrongShape {
actual: Kind::Struct, actual: Kind::Struct,
expected: type_id, expected_id: format!("{:?}", type_id),
})); }))
}; })
.visit_composite(|out, _fields| {
self.data.encode_to(out);
Ok(())
});
// Check that the name also lines up. types
if ty.path.ident().as_deref() != Some("WrapperKeepOpaque") { .resolve_type(type_id, visitor)
return Err(Error::new(ErrorKind::WrongShape { .map_err(|_| Error::new(ErrorKind::TypeNotFound(format!("{:?}", type_id))))?
actual: Kind::Struct,
expected: type_id,
}));
}
// Just blat the bytes out.
self.data.encode_to(out);
Ok(())
} }
} }
pub struct WrapperKeepOpaqueVisitor<T>(std::marker::PhantomData<T>); pub struct WrapperKeepOpaqueVisitor<T, R>(std::marker::PhantomData<(T, R)>);
impl<T> Visitor for WrapperKeepOpaqueVisitor<T> { impl<T, R: TypeResolver> Visitor for WrapperKeepOpaqueVisitor<T, R> {
type Value<'scale, 'info> = WrapperKeepOpaque<T>; type Value<'scale, 'info> = WrapperKeepOpaque<T>;
type Error = scale_decode::Error; type Error = scale_decode::Error;
type TypeResolver = R;
fn visit_composite<'scale, 'info>( fn visit_composite<'scale, 'info>(
self, self,
value: &mut scale_decode::visitor::types::Composite<'scale, 'info>, value: &mut scale_decode::visitor::types::Composite<'scale, 'info, R>,
_type_id: scale_decode::visitor::TypeId, _type_id: &R::TypeId,
) -> Result<Self::Value<'scale, 'info>, Self::Error> { ) -> Result<Self::Value<'scale, 'info>, Self::Error> {
use scale_decode::error::{Error, ErrorKind}; use scale_decode::error::{Error, ErrorKind};
if value.path().ident().as_deref() != Some("WrapperKeepOpaque") { // TODO: When `scale-type-resolver` [provides struct names](https://github.com/paritytech/scale-type-resolver/issues/4), check that this struct name is `WrapperKeepOpaque`
return Err(Error::custom_str(
"Type to decode is not 'WrapperTypeKeepOpaque'",
));
}
if value.remaining() != 2 { if value.remaining() != 2 {
return Err(Error::new(ErrorKind::WrongLength { return Err(Error::new(ErrorKind::WrongLength {
actual_len: value.remaining(), actual_len: value.remaining(),
@@ -151,8 +141,8 @@ impl<T> Visitor for WrapperKeepOpaqueVisitor<T> {
} }
impl<T> IntoVisitor for WrapperKeepOpaque<T> { impl<T> IntoVisitor for WrapperKeepOpaque<T> {
type Visitor = WrapperKeepOpaqueVisitor<T>; type AnyVisitor<R: TypeResolver> = WrapperKeepOpaqueVisitor<T, R>;
fn into_visitor() -> Self::Visitor { fn into_visitor<R: TypeResolver>() -> WrapperKeepOpaqueVisitor<T, R> {
WrapperKeepOpaqueVisitor(std::marker::PhantomData) WrapperKeepOpaqueVisitor(std::marker::PhantomData)
} }
} }
@@ -205,7 +195,7 @@ mod test {
let (type_id, types) = make_type::<T>(); let (type_id, types) = make_type::<T>();
let scale_codec_encoded = t.encode(); let scale_codec_encoded = t.encode();
let encode_as_type_encoded = t.encode_as_type(type_id, &types).unwrap(); let encode_as_type_encoded = t.encode_as_type(&type_id, &types).unwrap();
assert_eq!( assert_eq!(
scale_codec_encoded, encode_as_type_encoded, scale_codec_encoded, encode_as_type_encoded,
@@ -213,7 +203,7 @@ mod test {
); );
let decode_as_type_bytes = &mut &*scale_codec_encoded; let decode_as_type_bytes = &mut &*scale_codec_encoded;
let decoded_as_type = T::decode_as_type(decode_as_type_bytes, type_id, &types) let decoded_as_type = T::decode_as_type(decode_as_type_bytes, &type_id, &types)
.expect("decode-as-type decodes"); .expect("decode-as-type decodes");
let decode_scale_codec_bytes = &mut &*scale_codec_encoded; let decode_scale_codec_bytes = &mut &*scale_codec_encoded;