diff --git a/Cargo.lock b/Cargo.lock index 71790af71610..f56868d72d24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -150,12 +150,93 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" +[[package]] +name = "alloy-primitives" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0628ec0ba5b98b3370bb6be17b12f23bfce8ee4ad83823325a20546d9b03b78" +dependencies = [ + "alloy-rlp", + "bytes", + "cfg-if", + "const-hex", + "derive_more", + "hex-literal", + "itoa", + "proptest", + "rand 0.8.5", + "ruint", + "serde", + "tiny-keccak", +] + +[[package]] +name = "alloy-rlp" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc0fac0fc16baf1f63f78b47c3d24718f3619b0714076f6a02957d808d52cbef" +dependencies = [ + "alloy-rlp-derive", + "arrayvec 0.7.4", + "bytes", + "smol_str", +] + +[[package]] +name = "alloy-rlp-derive" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0391754c09fab4eae3404d19d0d297aa1c670c1775ab51d8a5312afeca23157" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.41", +] + +[[package]] +name = "alloy-sol-macro" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a98ad1696a2e17f010ae8e43e9f2a1e930ed176a8e3ff77acfeff6dfb07b42c" +dependencies = [ + "const-hex", + "dunce", + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.41", + "syn-solidity", + "tiny-keccak", +] + +[[package]] +name = "alloy-sol-types" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98d7107bed88e8f09f0ddcc3335622d87bfb6821f3e0c7473329fb1cfad5e015" +dependencies = [ + "alloy-primitives", + "alloy-sol-macro", + "const-hex", + "serde", +] + [[package]] name = "always-assert" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4436e0292ab1bb631b42973c61205e704475fe8126af845c8d923c0996328127" +[[package]] +name = "amcl" +version = "0.3.0" +source = "git+https://github.com/snowfork/milagro_bls?rev=a6d66e4eb89015e352fb1c9f7b661ecdbb5b2176#a6d66e4eb89015e352fb1c9f7b661ecdbb5b2176" +dependencies = [ + "parity-scale-codec", + "scale-info", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -276,8 +357,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fb00293ba84f51ce3bd026bd0de55899c4e68f0a39a5728cebae3a73ffdc0a4f" dependencies = [ "ark-ec", - "ark-ff", - "ark-std", + "ark-ff 0.4.2", + "ark-std 0.4.0", ] [[package]] @@ -289,7 +370,7 @@ dependencies = [ "ark-bls12-377", "ark-ec", "ark-models-ext", - "ark-std", + "ark-std 0.4.0", ] [[package]] @@ -299,9 +380,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c775f0d12169cba7aae4caeb547bb6a50781c7449a8aa53793827c9ec4abf488" dependencies = [ "ark-ec", - "ark-ff", - "ark-serialize", - "ark-std", + "ark-ff 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", ] [[package]] @@ -312,10 +393,10 @@ checksum = "b1dc4b3d08f19e8ec06e949712f95b8361e43f1391d94f65e4234df03480631c" dependencies = [ "ark-bls12-381", "ark-ec", - "ark-ff", + "ark-ff 0.4.2", "ark-models-ext", - "ark-serialize", - "ark-std", + "ark-serialize 0.4.2", + "ark-std 0.4.0", ] [[package]] @@ -326,8 +407,8 @@ checksum = "2e0605daf0cc5aa2034b78d008aaf159f56901d92a52ee4f6ecdfdac4f426700" dependencies = [ "ark-bls12-377", "ark-ec", - "ark-ff", - "ark-std", + "ark-ff 0.4.2", + "ark-std 0.4.0", ] [[package]] @@ -338,9 +419,9 @@ checksum = "ccee5fba47266f460067588ee1bf070a9c760bf2050c1c509982c5719aadb4f2" dependencies = [ "ark-bw6-761", "ark-ec", - "ark-ff", + "ark-ff 0.4.2", "ark-models-ext", - "ark-std", + "ark-std 0.4.0", ] [[package]] @@ -349,10 +430,10 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "defd9a439d56ac24968cca0571f598a61bc8c55f71d50a89cda591cb750670ba" dependencies = [ - "ark-ff", + "ark-ff 0.4.2", "ark-poly", - "ark-serialize", - "ark-std", + "ark-serialize 0.4.2", + "ark-std 0.4.0", "derivative", "hashbrown 0.13.2", "itertools 0.10.5", @@ -369,8 +450,8 @@ checksum = "b10d901b9ac4b38f9c32beacedfadcdd64e46f8d7f8e88c1ae1060022cf6f6c6" dependencies = [ "ark-bls12-377", "ark-ec", - "ark-ff", - "ark-std", + "ark-ff 0.4.2", + "ark-std 0.4.0", ] [[package]] @@ -381,9 +462,9 @@ checksum = "524a4fb7540df2e1a8c2e67a83ba1d1e6c3947f4f9342cc2359fc2e789ad731d" dependencies = [ "ark-ec", "ark-ed-on-bls12-377", - "ark-ff", + "ark-ff 0.4.2", "ark-models-ext", - "ark-std", + "ark-std 0.4.0", ] [[package]] @@ -394,8 +475,8 @@ checksum = "f9cde0f2aa063a2a5c28d39b47761aa102bda7c13c84fc118a61b87c7b2f785c" dependencies = [ "ark-bls12-381", "ark-ec", - "ark-ff", - "ark-std", + "ark-ff 0.4.2", + "ark-std 0.4.0", ] [[package]] @@ -406,9 +487,27 @@ checksum = "d15185f1acb49a07ff8cbe5f11a1adc5a93b19e211e325d826ae98e98e124346" dependencies = [ "ark-ec", "ark-ed-on-bls12-381-bandersnatch", - "ark-ff", + "ark-ff 0.4.2", "ark-models-ext", - "ark-std", + "ark-std 0.4.0", +] + +[[package]] +name = "ark-ff" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b3235cc41ee7a12aaaf2c575a2ad7b46713a8a50bda2fc3b003a04845c05dd6" +dependencies = [ + "ark-ff-asm 0.3.0", + "ark-ff-macros 0.3.0", + "ark-serialize 0.3.0", + "ark-std 0.3.0", + "derivative", + "num-bigint", + "num-traits", + "paste", + "rustc_version 0.3.3", + "zeroize", ] [[package]] @@ -417,10 +516,10 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec847af850f44ad29048935519032c33da8aa03340876d351dfab5660d2966ba" dependencies = [ - "ark-ff-asm", - "ark-ff-macros", - "ark-serialize", - "ark-std", + "ark-ff-asm 0.4.2", + "ark-ff-macros 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", "derivative", "digest 0.10.7", "itertools 0.10.5", @@ -431,6 +530,16 @@ dependencies = [ "zeroize", ] +[[package]] +name = "ark-ff-asm" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db02d390bf6643fb404d3d22d31aee1c4bc4459600aef9113833d17e786c6e44" +dependencies = [ + "quote", + "syn 1.0.109", +] + [[package]] name = "ark-ff-asm" version = "0.4.2" @@ -441,6 +550,18 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "ark-ff-macros" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db2fd794a08ccb318058009eefdf15bcaaaaf6f8161eb3345f907222bac38b20" +dependencies = [ + "num-bigint", + "num-traits", + "quote", + "syn 1.0.109", +] + [[package]] name = "ark-ff-macros" version = "0.4.2" @@ -461,9 +582,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e9eab5d4b5ff2f228b763d38442adc9b084b0a465409b059fac5c2308835ec2" dependencies = [ "ark-ec", - "ark-ff", - "ark-serialize", - "ark-std", + "ark-ff 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", "derivative", ] @@ -473,9 +594,9 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d320bfc44ee185d899ccbadfa8bc31aab923ce1558716e1997a1e74057fe86bf" dependencies = [ - "ark-ff", - "ark-serialize", - "ark-std", + "ark-ff 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", "derivative", "hashbrown 0.13.2", ] @@ -487,9 +608,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51bd73bb6ddb72630987d37fa963e99196896c0d0ea81b7c894567e74a2f83af" dependencies = [ "ark-ec", - "ark-ff", - "ark-serialize", - "ark-std", + "ark-ff 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", "parity-scale-codec", "scale-info", ] @@ -501,9 +622,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f69c00b3b529be29528a6f2fd5fa7b1790f8bed81b9cdca17e326538545a179" dependencies = [ "ark-ec", - "ark-ff", - "ark-serialize", - "ark-std", + "ark-ff 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", "parity-scale-codec", "scale-info", ] @@ -514,15 +635,25 @@ version = "0.0.2" source = "git+https://github.com/w3f/ring-vrf?rev=e9782f9#e9782f938629c90f3adb3fff2358bc8d1386af3e" dependencies = [ "ark-ec", - "ark-ff", - "ark-serialize", - "ark-std", + "ark-ff 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", "ark-transcript", "digest 0.10.7", "getrandom_or_panic", "zeroize", ] +[[package]] +name = "ark-serialize" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6c2b318ee6e10f8c2853e73a83adc0ccb88995aa978d8a3408d492ab2ee671" +dependencies = [ + "ark-std 0.3.0", + "digest 0.9.0", +] + [[package]] name = "ark-serialize" version = "0.4.2" @@ -530,7 +661,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb7b85a02b83d2f22f89bd5cac66c9c89474240cb6207cb1efc16d098e822a5" dependencies = [ "ark-serialize-derive", - "ark-std", + "ark-std 0.4.0", "digest 0.10.7", "num-bigint", ] @@ -546,6 +677,16 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "ark-std" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df2c09229cbc5a028b1d70e00fdb2acee28b1055dfb5ca73eea49c5a25c4e7c" +dependencies = [ + "num-traits", + "rand 0.8.5", +] + [[package]] name = "ark-std" version = "0.4.0" @@ -562,9 +703,9 @@ name = "ark-transcript" version = "0.0.2" source = "git+https://github.com/w3f/ring-vrf?rev=e9782f9#e9782f938629c90f3adb3fff2358bc8d1386af3e" dependencies = [ - "ark-ff", - "ark-serialize", - "ark-std", + "ark-ff 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", "digest 0.10.7", "rand_core 0.6.4", "sha3", @@ -765,6 +906,8 @@ dependencies = [ "rococo-runtime-constants", "scale-info", "smallvec", + "snowbridge-rococo-common", + "snowbridge-router-primitives", "sp-api", "sp-block-builder", "sp-consensus-aura", @@ -1139,6 +1282,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "auto_impl" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fee3da8ef1276b0bee5dd1c7258010d8fffd31801447323115a25560e1327b89" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -1168,9 +1323,9 @@ dependencies = [ "ark-bls12-381", "ark-ec", "ark-ed-on-bls12-381-bandersnatch", - "ark-ff", - "ark-serialize", - "ark-std", + "ark-ff 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", "dleq_vrf", "fflonk", "merlin 3.0.0", @@ -1286,6 +1441,21 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "bit-set" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" + [[package]] name = "bitcoin_hashes" version = "0.11.0" @@ -1753,16 +1923,38 @@ dependencies = [ "sp-runtime", ] +[[package]] +name = "bridge-hub-common" +version = "0.1.0" +dependencies = [ + "cumulus-primitives-core", + "frame-support", + "pallet-message-queue", + "parity-scale-codec", + "scale-info", + "snowbridge-core", + "sp-core", + "sp-runtime", + "sp-std 8.0.0", + "staging-xcm", +] + [[package]] name = "bridge-hub-rococo-emulated-chain" version = "0.0.0" dependencies = [ + "bridge-hub-common", "bridge-hub-rococo-runtime", "cumulus-primitives-core", "emulated-integration-tests-common", "frame-support", "parachains-common", "serde_json", + "snowbridge-core", + "snowbridge-inbound-queue", + "snowbridge-outbound-queue", + "snowbridge-router-primitives", + "snowbridge-system", "sp-core", "sp-runtime", ] @@ -1771,6 +1963,7 @@ dependencies = [ name = "bridge-hub-rococo-integration-tests" version = "1.0.0" dependencies = [ + "asset-hub-rococo-runtime", "asset-test-utils", "bp-messages", "bridge-hub-rococo-runtime", @@ -1778,6 +1971,8 @@ dependencies = [ "cumulus-pallet-xcmp-queue", "emulated-integration-tests-common", "frame-support", + "hex", + "hex-literal", "pallet-assets", "pallet-balances", "pallet-bridge-messages", @@ -1785,7 +1980,17 @@ dependencies = [ "pallet-xcm", "parachains-common", "parity-scale-codec", + "penpal-runtime", + "rococo-system-emulated-network", "rococo-westend-system-emulated-network", + "scale-info", + "snowbridge-core", + "snowbridge-inbound-queue", + "snowbridge-outbound-queue", + "snowbridge-rococo-common", + "snowbridge-router-primitives", + "snowbridge-system", + "sp-core", "sp-runtime", "staging-xcm", "staging-xcm-executor", @@ -1809,6 +2014,7 @@ dependencies = [ "bp-rococo", "bp-runtime", "bp-westend", + "bridge-hub-common", "bridge-hub-test-utils", "bridge-runtime-common", "cumulus-pallet-aura-ext", @@ -1854,6 +2060,17 @@ dependencies = [ "scale-info", "serde", "smallvec", + "snowbridge-beacon-primitives", + "snowbridge-core", + "snowbridge-ethereum-beacon-client", + "snowbridge-inbound-queue", + "snowbridge-outbound-queue", + "snowbridge-outbound-queue-runtime-api", + "snowbridge-rococo-common", + "snowbridge-router-primitives", + "snowbridge-runtime-common", + "snowbridge-system", + "snowbridge-system-runtime-api", "sp-api", "sp-block-builder", "sp-consensus-aura", @@ -1927,6 +2144,7 @@ dependencies = [ name = "bridge-hub-westend-emulated-chain" version = "0.0.0" dependencies = [ + "bridge-hub-common", "bridge-hub-westend-runtime", "cumulus-primitives-core", "emulated-integration-tests-common", @@ -1977,6 +2195,7 @@ dependencies = [ "bp-rococo", "bp-runtime", "bp-westend", + "bridge-hub-common", "bridge-hub-test-utils", "bridge-runtime-common", "cumulus-pallet-aura-ext", @@ -2677,10 +2896,10 @@ version = "0.1.0" source = "git+https://github.com/w3f/ring-proof#b273d33f9981e2bb3375ab45faeb537f7ee35224" dependencies = [ "ark-ec", - "ark-ff", + "ark-ff 0.4.2", "ark-poly", - "ark-serialize", - "ark-std", + "ark-serialize 0.4.2", + "ark-std 0.4.0", "fflonk", "getrandom_or_panic", "merlin 3.0.0", @@ -2715,6 +2934,29 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + +[[package]] +name = "const-hex" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5104de16b218eddf8e34ffe2f86f74bfa4e61e95a1b89732fccf6325efd0557" +dependencies = [ + "cfg-if", + "cpufeatures", + "hex", + "proptest", + "serde", +] + [[package]] name = "const-oid" version = "0.9.5" @@ -4458,11 +4700,11 @@ version = "0.0.2" source = "git+https://github.com/w3f/ring-vrf?rev=e9782f9#e9782f938629c90f3adb3fff2358bc8d1386af3e" dependencies = [ "ark-ec", - "ark-ff", + "ark-ff 0.4.2", "ark-scale 0.0.12", "ark-secret-scalar", - "ark-serialize", - "ark-std", + "ark-serialize 0.4.2", + "ark-std 0.4.0", "ark-transcript", "arrayvec 0.7.4", "zeroize", @@ -4528,6 +4770,12 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcbb2bf8e87535c23f7a8a321e364ce21462d0ff10cb6407820e8e96dfff6653" +[[package]] +name = "dunce" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" + [[package]] name = "dyn-clonable" version = "0.9.0" @@ -4828,6 +5076,15 @@ dependencies = [ "libc", ] +[[package]] +name = "ethabi-decode" +version = "1.4.0" +source = "git+https://github.com/snowfork/ethabi-decode.git?branch=master#7d215837b626650bd9a076821e57ad488101301f" +dependencies = [ + "ethereum-types", + "tiny-keccak", +] + [[package]] name = "ethbloom" version = "0.13.0" @@ -4836,8 +5093,10 @@ checksum = "c22d4b5885b6aa2fe5e8b9329fb8d232bf739e434e6b87347c63bdd00c120f60" dependencies = [ "crunchy", "fixed-hash", + "impl-codec", "impl-rlp", "impl-serde", + "scale-info", "tiny-keccak", ] @@ -4849,9 +5108,11 @@ checksum = "02d215cbf040552efcbe99a38372fe80ab9d00268e20012b79fcd0f073edd8ee" dependencies = [ "ethbloom", "fixed-hash", + "impl-codec", "impl-rlp", "impl-serde", "primitive-types", + "scale-info", "uint", ] @@ -4932,6 +5193,17 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" +[[package]] +name = "fastrlp" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "139834ddba373bbdd213dffe02c8d110508dcf1726c2be27e8d1f7d7e1856418" +dependencies = [ + "arrayvec 0.7.4", + "auto_impl", + "bytes", +] + [[package]] name = "fatality" version = "0.0.6" @@ -4999,10 +5271,10 @@ version = "0.1.0" source = "git+https://github.com/w3f/fflonk#1e854f35e9a65d08b11a86291405cdc95baa0a35" dependencies = [ "ark-ec", - "ark-ff", + "ark-ff 0.4.2", "ark-poly", - "ark-serialize", - "ark-std", + "ark-serialize 0.4.2", + "ark-std 0.4.0", "merlin 3.0.0", ] @@ -7783,6 +8055,20 @@ dependencies = [ "thrift", ] +[[package]] +name = "milagro_bls" +version = "1.5.0" +source = "git+https://github.com/snowfork/milagro_bls?rev=a6d66e4eb89015e352fb1c9f7b661ecdbb5b2176#a6d66e4eb89015e352fb1c9f7b661ecdbb5b2176" +dependencies = [ + "amcl", + "hex", + "lazy_static", + "parity-scale-codec", + "rand 0.8.5", + "scale-info", + "zeroize", +] + [[package]] name = "mime" version = "0.3.17" @@ -11120,6 +11406,12 @@ dependencies = [ "substrate-wasm-builder", ] +[[package]] +name = "parity-bytes" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b56e3a2420138bdb970f84dfb9c774aea80fa0e7371549eedec0d80c209c67" + [[package]] name = "parity-db" version = "0.4.12" @@ -11327,6 +11619,7 @@ dependencies = [ name = "penpal-runtime" version = "0.9.27" dependencies = [ + "assets-common", "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", "cumulus-pallet-parachain-system", @@ -11364,6 +11657,7 @@ dependencies = [ "polkadot-runtime-common", "scale-info", "smallvec", + "snowbridge-rococo-common", "sp-api", "sp-block-builder", "sp-consensus-aura", @@ -12919,6 +13213,7 @@ dependencies = [ "pallet-authorship", "pallet-babe", "pallet-balances", + "pallet-broker", "pallet-election-provider-multi-phase", "pallet-fast-unstake", "pallet-identity", @@ -12989,6 +13284,7 @@ dependencies = [ "pallet-authorship", "pallet-babe", "pallet-balances", + "pallet-broker", "pallet-message-queue", "pallet-session", "pallet-staking", @@ -13009,6 +13305,7 @@ dependencies = [ "serde_json", "sp-api", "sp-application-crypto", + "sp-arithmetic", "sp-core", "sp-inherents", "sp-io", @@ -13819,6 +14116,26 @@ dependencies = [ "regex", ] +[[package]] +name = "proptest" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31b476131c3c86cb68032fdc5cb6d5a1045e3e42d96b69fa599fd77701e1f5bf" +dependencies = [ + "bit-set", + "bit-vec", + "bitflags 2.4.0", + "lazy_static", + "num-traits", + "rand 0.8.5", + "rand_chacha 0.3.1", + "rand_xorshift", + "regex-syntax 0.8.2", + "rusty-fork", + "tempfile", + "unarray", +] + [[package]] name = "prost" version = "0.11.9" @@ -14085,6 +14402,15 @@ dependencies = [ "rand_core 0.6.4", ] +[[package]] +name = "rand_xorshift" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f" +dependencies = [ + "rand_core 0.6.4", +] + [[package]] name = "rawpointer" version = "0.2.1" @@ -14338,10 +14664,10 @@ version = "0.1.0" source = "git+https://github.com/w3f/ring-proof#b273d33f9981e2bb3375ab45faeb537f7ee35224" dependencies = [ "ark-ec", - "ark-ff", + "ark-ff 0.4.2", "ark-poly", - "ark-serialize", - "ark-std", + "ark-serialize 0.4.2", + "ark-std 0.4.0", "blake2 0.10.6", "common", "fflonk", @@ -14607,6 +14933,7 @@ dependencies = [ "bridge-hub-rococo-emulated-chain", "bridge-hub-westend-emulated-chain", "emulated-integration-tests-common", + "penpal-emulated-chain", "rococo-emulated-chain", "westend-emulated-chain", ] @@ -14647,6 +14974,36 @@ dependencies = [ "winapi", ] +[[package]] +name = "ruint" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "608a5726529f2f0ef81b8fde9873c4bb829d6b5b5ca6be4d97345ddf0749c825" +dependencies = [ + "alloy-rlp", + "ark-ff 0.3.0", + "ark-ff 0.4.2", + "bytes", + "fastrlp", + "num-bigint", + "num-traits", + "parity-scale-codec", + "primitive-types", + "proptest", + "rand 0.8.5", + "rlp", + "ruint-macro", + "serde", + "valuable", + "zeroize", +] + +[[package]] +name = "ruint-macro" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e666a5496a0b2186dbcd0ff6106e29e093c15591bde62c20d3842007c6978a09" + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -14674,6 +15031,15 @@ dependencies = [ "semver 0.9.0", ] +[[package]] +name = "rustc_version" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0dfe2087c51c460008730de8b57e6a320782fbfb312e1f4d520e6c6fae155ee" +dependencies = [ + "semver 0.11.0", +] + [[package]] name = "rustc_version" version = "0.4.0" @@ -16435,8 +16801,14 @@ dependencies = [ ] [[package]] -name = "scopeguard" -version = "1.2.0" +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + +[[package]] +name = "scopeguard" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" @@ -16570,7 +16942,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a3186ec9e65071a2095434b1f5bb24838d4e8e130f584c790f6033c79943537" dependencies = [ - "semver-parser", + "semver-parser 0.7.0", ] [[package]] @@ -16579,7 +16951,16 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" dependencies = [ - "semver-parser", + "semver-parser 0.7.0", +] + +[[package]] +name = "semver" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" +dependencies = [ + "semver-parser 0.10.2", ] [[package]] @@ -16597,6 +16978,15 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +[[package]] +name = "semver-parser" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7" +dependencies = [ + "pest", +] + [[package]] name = "separator" version = "0.4.1" @@ -16612,6 +17002,15 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-big-array" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd31f59f6fe2b0c055371bb2f16d7f0aa7d8881676c04a55b1596d1a17cd10a4" +dependencies = [ + "serde", +] + [[package]] name = "serde_bytes" version = "0.11.12" @@ -16966,6 +17365,15 @@ dependencies = [ "futures-lite", ] +[[package]] +name = "smol_str" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74212e6bbe9a4352329b2f68ba3130c15a3f26fe88ff22dbdc6cdd58fa85e99c" +dependencies = [ + "serde", +] + [[package]] name = "smoldot" version = "0.11.0" @@ -17079,6 +17487,358 @@ dependencies = [ "subtle 2.4.1", ] +[[package]] +name = "snowbridge-beacon-primitives" +version = "0.0.1" +dependencies = [ + "byte-slice-cast", + "frame-support", + "frame-system", + "hex", + "hex-literal", + "milagro_bls", + "parity-scale-codec", + "rlp", + "scale-info", + "serde", + "snowbridge-ethereum", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std 8.0.0", + "ssz_rs", + "ssz_rs_derive", + "static_assertions", +] + +[[package]] +name = "snowbridge-core" +version = "0.1.1" +dependencies = [ + "ethabi-decode", + "frame-support", + "frame-system", + "hex", + "hex-literal", + "parity-scale-codec", + "polkadot-parachain-primitives", + "scale-info", + "serde", + "snowbridge-beacon-primitives", + "sp-arithmetic", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std 8.0.0", + "staging-xcm", + "staging-xcm-builder", +] + +[[package]] +name = "snowbridge-ethereum" +version = "0.1.0" +dependencies = [ + "ethabi-decode", + "ethbloom", + "ethereum-types", + "hex-literal", + "parity-bytes", + "parity-scale-codec", + "rand 0.8.5", + "rlp", + "rustc-hex", + "scale-info", + "serde", + "serde-big-array", + "serde_json", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std 8.0.0", + "wasm-bindgen-test", +] + +[[package]] +name = "snowbridge-ethereum-beacon-client" +version = "0.0.1" +dependencies = [ + "bp-runtime", + "byte-slice-cast", + "frame-benchmarking", + "frame-support", + "frame-system", + "hex-literal", + "log", + "pallet-timestamp", + "parity-scale-codec", + "rand 0.8.5", + "rlp", + "scale-info", + "serde", + "serde_json", + "snowbridge-beacon-primitives", + "snowbridge-core", + "snowbridge-ethereum", + "sp-core", + "sp-io", + "sp-keyring", + "sp-runtime", + "sp-std 8.0.0", + "ssz_rs", + "ssz_rs_derive", + "static_assertions", +] + +[[package]] +name = "snowbridge-inbound-queue" +version = "0.1.1" +dependencies = [ + "alloy-primitives", + "alloy-rlp", + "alloy-sol-types", + "frame-benchmarking", + "frame-support", + "frame-system", + "hex-literal", + "log", + "num-traits", + "pallet-balances", + "parity-scale-codec", + "scale-info", + "serde", + "snowbridge-beacon-primitives", + "snowbridge-core", + "snowbridge-ethereum", + "snowbridge-ethereum-beacon-client", + "snowbridge-router-primitives", + "sp-core", + "sp-io", + "sp-keyring", + "sp-runtime", + "sp-std 8.0.0", + "staging-xcm", + "staging-xcm-builder", +] + +[[package]] +name = "snowbridge-outbound-queue" +version = "0.1.1" +dependencies = [ + "bridge-hub-common", + "ethabi-decode", + "frame-benchmarking", + "frame-support", + "frame-system", + "hex-literal", + "pallet-message-queue", + "parity-scale-codec", + "scale-info", + "serde", + "snowbridge-core", + "snowbridge-outbound-queue-merkle-tree", + "sp-arithmetic", + "sp-core", + "sp-io", + "sp-keyring", + "sp-runtime", + "sp-std 8.0.0", + "staging-xcm", +] + +[[package]] +name = "snowbridge-outbound-queue-merkle-tree" +version = "0.1.1" +dependencies = [ + "array-bytes 4.2.0", + "env_logger 0.9.3", + "hex", + "hex-literal", + "parity-scale-codec", + "scale-info", + "sp-core", + "sp-runtime", +] + +[[package]] +name = "snowbridge-outbound-queue-runtime-api" +version = "0.1.0" +dependencies = [ + "frame-support", + "parity-scale-codec", + "snowbridge-core", + "snowbridge-outbound-queue-merkle-tree", + "sp-api", + "sp-core", + "sp-std 8.0.0", + "staging-xcm", +] + +[[package]] +name = "snowbridge-rococo-common" +version = "0.0.1" +dependencies = [ + "frame-support", + "log", + "staging-xcm", +] + +[[package]] +name = "snowbridge-router-primitives" +version = "0.1.1" +dependencies = [ + "ethabi-decode", + "frame-support", + "frame-system", + "hex-literal", + "log", + "parity-scale-codec", + "rustc-hex", + "scale-info", + "serde", + "snowbridge-core", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std 8.0.0", + "staging-xcm", + "staging-xcm-builder", + "staging-xcm-executor", +] + +[[package]] +name = "snowbridge-runtime-common" +version = "0.1.1" +dependencies = [ + "frame-support", + "frame-system", + "log", + "snowbridge-core", + "sp-arithmetic", + "staging-xcm", + "staging-xcm-builder", + "staging-xcm-executor", +] + +[[package]] +name = "snowbridge-runtime-tests" +version = "0.1.0" +dependencies = [ + "asset-hub-rococo-runtime", + "assets-common", + "bridge-hub-rococo-runtime", + "bridge-hub-test-utils", + "bridge-runtime-common", + "cumulus-pallet-aura-ext", + "cumulus-pallet-dmp-queue", + "cumulus-pallet-parachain-system", + "cumulus-pallet-session-benchmarking", + "cumulus-pallet-xcm", + "cumulus-pallet-xcmp-queue", + "cumulus-primitives-core", + "cumulus-primitives-utility", + "frame-benchmarking", + "frame-executive", + "frame-support", + "frame-system", + "frame-system-benchmarking", + "frame-system-rpc-runtime-api", + "frame-try-runtime", + "hex-literal", + "log", + "pallet-aura", + "pallet-authorship", + "pallet-balances", + "pallet-collator-selection", + "pallet-message-queue", + "pallet-multisig", + "pallet-session", + "pallet-timestamp", + "pallet-transaction-payment", + "pallet-transaction-payment-rpc-runtime-api", + "pallet-utility", + "pallet-xcm", + "pallet-xcm-benchmarks", + "parachains-common", + "parachains-runtimes-test-utils", + "parity-scale-codec", + "polkadot-core-primitives", + "polkadot-parachain-primitives", + "polkadot-runtime-common", + "rococo-runtime-constants", + "scale-info", + "serde", + "smallvec", + "snowbridge-beacon-primitives", + "snowbridge-core", + "snowbridge-ethereum-beacon-client", + "snowbridge-inbound-queue", + "snowbridge-outbound-queue", + "snowbridge-outbound-queue-runtime-api", + "snowbridge-router-primitives", + "snowbridge-system", + "snowbridge-system-runtime-api", + "sp-api", + "sp-block-builder", + "sp-consensus-aura", + "sp-core", + "sp-genesis-builder", + "sp-inherents", + "sp-io", + "sp-keyring", + "sp-offchain", + "sp-runtime", + "sp-session", + "sp-std 8.0.0", + "sp-storage 13.0.0", + "sp-transaction-pool", + "sp-version", + "staging-parachain-info", + "staging-xcm", + "staging-xcm-builder", + "staging-xcm-executor", + "static_assertions", +] + +[[package]] +name = "snowbridge-system" +version = "0.1.1" +dependencies = [ + "ethabi-decode", + "frame-benchmarking", + "frame-support", + "frame-system", + "hex", + "hex-literal", + "log", + "pallet-balances", + "pallet-message-queue", + "parity-scale-codec", + "polkadot-primitives", + "scale-info", + "snowbridge-core", + "snowbridge-outbound-queue", + "sp-core", + "sp-io", + "sp-keyring", + "sp-runtime", + "sp-std 8.0.0", + "staging-xcm", + "staging-xcm-builder", + "staging-xcm-executor", +] + +[[package]] +name = "snowbridge-system-runtime-api" +version = "0.1.0" +dependencies = [ + "parity-scale-codec", + "snowbridge-core", + "sp-api", + "sp-core", + "sp-std 8.0.0", + "staging-xcm", +] + [[package]] name = "socket2" version = "0.4.9" @@ -18198,6 +18958,29 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "ssz_rs" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "057291e5631f280978fa9c8009390663ca4613359fc1318e36a8c24c392f6d1f" +dependencies = [ + "bitvec", + "num-bigint", + "sha2 0.9.9", + "ssz_rs_derive", +] + +[[package]] +name = "ssz_rs_derive" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f07d54c4d01a1713eb363b55ba51595da15f6f1211435b71466460da022aa140" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -18927,6 +19710,18 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn-solidity" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b837ef12ab88835251726eb12237655e61ec8dc8a280085d1961cdc3dfd047" +dependencies = [ + "paste", + "proc-macro2", + "quote", + "syn 2.0.41", +] + [[package]] name = "synstructure" version = "0.12.6" @@ -19849,6 +20644,12 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "unarray" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" + [[package]] name = "unicode-bidi" version = "0.3.13" @@ -19904,9 +20705,9 @@ dependencies = [ [[package]] name = "unsafe-libyaml" -version = "0.2.9" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f28467d3e1d3c6586d8f25fa243f544f5800fec42d97032474e17222c2b75cfa" +checksum = "ab4c90930b95a82d00dc9e9ac071b4991924390d46cbd0dfe566148667605e4b" [[package]] name = "unsigned-varint" @@ -20024,8 +20825,8 @@ dependencies = [ "ark-bls12-377", "ark-bls12-381", "ark-ec", - "ark-ff", - "ark-serialize", + "ark-ff 0.4.2", + "ark-serialize 0.4.2", "ark-serialize-derive", "arrayref", "constcat", @@ -20153,6 +20954,30 @@ version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" +[[package]] +name = "wasm-bindgen-test" +version = "0.3.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e6e302a7ea94f83a6d09e78e7dc7d9ca7b186bc2829c24a22d0753efd680671" +dependencies = [ + "console_error_panic_hook", + "js-sys", + "scoped-tls", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-bindgen-test-macro", +] + +[[package]] +name = "wasm-bindgen-test-macro" +version = "0.3.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecb993dd8c836930ed130e020e77d9b2e65dd0fbab1b67c790b0f5d80b11a575" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "wasm-encoder" version = "0.31.1" diff --git a/Cargo.toml b/Cargo.toml index 18a7de0e2b41..55958b0d83ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,20 @@ members = [ "bridges/primitives/test-utils", "bridges/primitives/xcm-bridge-hub", "bridges/primitives/xcm-bridge-hub-router", + "bridges/snowbridge/parachain/pallets/ethereum-beacon-client", + "bridges/snowbridge/parachain/pallets/inbound-queue", + "bridges/snowbridge/parachain/pallets/outbound-queue", + "bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree", + "bridges/snowbridge/parachain/pallets/outbound-queue/runtime-api", + "bridges/snowbridge/parachain/pallets/system", + "bridges/snowbridge/parachain/pallets/system/runtime-api", + "bridges/snowbridge/parachain/primitives/beacon", + "bridges/snowbridge/parachain/primitives/core", + "bridges/snowbridge/parachain/primitives/ethereum", + "bridges/snowbridge/parachain/primitives/router", + "bridges/snowbridge/parachain/runtime/rococo-common", + "bridges/snowbridge/parachain/runtime/runtime-common", + "bridges/snowbridge/parachain/runtime/tests", "cumulus/client/cli", "cumulus/client/collator", "cumulus/client/consensus/aura", diff --git a/bridges/snowbridge/LICENSE b/bridges/snowbridge/LICENSE new file mode 100644 index 000000000000..261eeb9e9f8b --- /dev/null +++ b/bridges/snowbridge/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/bridges/snowbridge/README.md b/bridges/snowbridge/README.md new file mode 100644 index 000000000000..a38910da3164 --- /dev/null +++ b/bridges/snowbridge/README.md @@ -0,0 +1,127 @@ +# Snowbridge · +[![codecov](https://codecov.io/gh/Snowfork/snowbridge/branch/main/graph/badge.svg?token=9hvgSws4rN)] +(https://codecov.io/gh/Snowfork/snowbridge) +![GitHub](https://img.shields.io/github/license/Snowfork/snowbridge) + +Snowbridge is a trustless bridge between Polkadot and Ethereum. For documentation, visit https://docs.snowbridge.network. + +## Components + +### Parachain + +Polkadot parachain and our pallets. See [parachain/README.md](https://github.com/Snowfork/snowbridge/blob/main/parachain/README.md). + +### Contracts + +Ethereum contracts and unit tests. See [contracts/README.md](https://github.com/Snowfork/snowbridge/blob/main/contracts/README.md) + +### Relayer + +Off-chain relayer services for relaying messages between Polkadot and Ethereum. See +[relayer/README.md](https://github.com/Snowfork/snowbridge/blob/main/relayer/README.md) + +### Local Testnet + +Scripts to provision a local testnet, running the above services to bridge between local deployments of Polkadot and +Ethereum. See [web/packages/test/README.md](https://github.com/Snowfork/snowbridge/blob/main/web/packages/test/README.md). + +### Smoke Tests + +Integration tests for our local testnet. See [smoketest/README.md](https://github.com/Snowfork/snowbridge/blob/main/smoketest/README.md). + +## Development + +We use the Nix package manager to provide a reproducible and maintainable developer environment. + +After [installing nix](https://nixos.org/download.html) Nix, enable [flakes](https://nixos.wiki/wiki/Flakes): + +```sh +mkdir -p ~/.config/nix +echo 'experimental-features = nix-command flakes' >> ~/.config/nix/nix.conf +``` + +Then activate a developer shell in the root of our repo, where +[`flake.nix`](https://github.com/Snowfork/snowbridge/blob/main/flake.nix) is located: + +```sh +nix develop +``` + +Also make sure to run this initialization script once: +```sh +scripts/init.sh +``` + +### Support for code editors + +To ensure your code editor (such as VS Code) can execute tools in the nix shell, startup your editor within the +interactive shell. + +Example for VS Code: + +```sh +nix develop +code . +``` + +### Custom shells + +The developer shell is bash by default. To preserve your existing shell: + +```sh +nix develop --command $SHELL +``` + +### Automatic developer shells + +To automatically enter the developer shell whenever you open the project, install +[`direnv`](https://direnv.net/docs/installation.html) and use the template `.envrc`: + +```sh +cp .envrc.example .envrc +direnv allow +``` + +### Upgrading the Rust toolchain + +Sometimes we would like to upgrade rust toolchain. First update `parachain/rust-toolchain.toml` as required and then +update `flake.lock` running +```sh +nix flake lock --update-input rust-overlay +``` + +## Troubleshooting + +Check the contents of all `.envrc` files. + +Remove untracked files: +```sh +git clean -idx +``` + +Ensure that the current Rust toolchain is the one selected in `scripts/init.sh`. + +Ensure submodules are up-to-date: +```sh +git submodule update +``` + +Check untracked files & directories: +```sh +git clean -ndx | awk '{print $3}' +``` +After removing `node_modules` directories (eg. with `git clean above`), clear the pnpm cache: +```sh +pnpm store prune +``` + +Check Nix config in `~/.config/nix/nix.conf`. + +Run a pure developer shell (note that this removes access to your local tools): +```sh +nix develop -i --pure-eval +``` + +## Security + +The security policy and procedures can be found in SECURITY.md. diff --git a/bridges/snowbridge/parachain/LICENSE b/bridges/snowbridge/parachain/LICENSE new file mode 100644 index 000000000000..261eeb9e9f8b --- /dev/null +++ b/bridges/snowbridge/parachain/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/bridges/snowbridge/parachain/README.md b/bridges/snowbridge/parachain/README.md new file mode 100644 index 000000000000..ddcbedab0c63 --- /dev/null +++ b/bridges/snowbridge/parachain/README.md @@ -0,0 +1,155 @@ +# Parachain modules + +## Configuration + +Note: This section is not necessary for local development, as there are scripts to auto-configure the parachain in the +[test directory](https://github.com/Snowfork/snowbridge/blob/main/web/packages/test). + +For a fully operational chain, further configuration of the initial chain spec is required. The specific configuration will +depend heavily on your environment, so this guide will remain high-level. + +After completing a release build of the parachain, build an initial spec for the snowbase runtime: + +```bash +target/release/snowbridge build-spec --chain snowbase --disable-default-bootnode > spec.json +``` + +Now edit the spec and configure the following: +1. Recently finalized ethereum header and difficulty for the ethereum light client +2. Contract addresses for the Ether, Erc20, and Dot apps. +3. Authorized principal for the basic channel + +For an example configuration, consult the [setup script](https://github.com/Snowfork/snowbridge/blob/main/web/packages/test/scripts/start-services.sh) +for our local development stack. Specifically the `start_polkadot_launch` bash function. + +## Tests + +To run the parachain tests locally, use `cargo test --workspace`. For the full suite of tests, use +`cargo test --workspace --features runtime-benchmarks`. + +Optionally exclude the top-level and runtime crates: + +```bash +cargo test --workspace \ + --features runtime-benchmarks \ + --exclude snowbridge \ + --exclude snowbridge-runtime \ + --exclude snowblink-runtime \ + --exclude snowbase-runtime +``` + +### Updating test data for inbound channel unit tests + +To regenerate the test data, use a test with multiple `submit` calls in `ethereum/test/test_basic_outbound_channel.js`, eg. +"should increment nonces correctly". + +Add the following preamble: + +```javascript +const rlp = require("rlp"); +const contract = BasicOutboundChannel; +const signature = 'Message(address,address,uint64,uint64,bytes)'; +``` + +For each encoded log you want to create, find a transaction object `tx` returned from a `submit` call and run this: + +```javascript +const rawLog = tx.receipt.rawLogs[0]; +const encodedLog = rlp.encode([rawLog.address, rawLog.topics, rawLog.data]).toString("hex"); +console.log(`encodedLog: ${encodedLog}`); +const iface = new ethers.utils.Interface(contract.abi); +const decodedEventLog = iface.decodeEventLog( + signature, + rawLog.data, + rawLog.topics, +); +console.log(`decoded rawLog.data: ${JSON.stringify(decodedEventLog)}`); +``` + +Place the `encodedLog` string in the `message.data` field in the test data. Use the `decoded rawLog.data` field to +update the comments with the decoded log data. + +## Generating pallet weights from benchmarks + +Build the parachain with the runtime benchmark flags for the chosen runtime: + +```bash +runtime=snowbase +cargo build \ + --release \ + --no-default-features \ + --features "$runtime-native,rococo-native,runtime-benchmarks,$runtime-runtime-benchmarks" \ + --bin snowbridge +``` + +List available pallets and their benchmarks: + +```bash +./target/release/snowbridge benchmark pallet --chain $runtime --list +``` + +Run a benchmark for a pallet, generating weights: + +```bash +target/release/snowbridge benchmark pallet \ + --chain=$runtime \ + --execution=wasm \ + --wasm-execution=compiled \ + --pallet=basic_channel_inbound \ + --extra \ + --extrinsic=* \ + --repeat=20 \ + --steps=50 \ + --output=pallets/basic-channel/src/inbound/weights.rs \ + --template=templates/module-weight-template.hbs +``` + +## Generating beacon test fixtures and benchmarking data + +### Minimal Spec + +To generate `minimal` test data and benchmarking data, make sure to start the local E2E setup to spin up a local beacon +node instance to connect to: + +```bash +cd web/packages/test +./scripts/start-services.sh +``` + +Wait for output `Testnet has been initialized`. + +In a separate terminal, from the `snowbridge` directory, run: + +```bash +mage -d relayer build && relayer/build/snowbridge-relay generate-beacon-data --spec "minimal" && cd parachain && +cargo +nightly fmt -- --config-path rustfmt.toml && cd - +``` + +### Mainnet Spec + +We only use the mainnet spec for generating fixtures for pallet weight benchmarks. + +To generate the data we can connect to the Lodestar Goerli public node. The script already connects to the Lodestar node, +so no need to start up additional services. In the event of the Lodestar node not being available, you can start up your +own stack with these commands: + +```bash +cd web/packages/test +./scripts/start-goerli.sh +``` + +From the `snowbridge` directory, run: + +```bash +mage -d relayer build && relayer/build/snowbridge-relay generate-beacon-data --spec "mainnet" && cd parachain && +cargo +nightly fmt -- --config-path rustfmt.toml && cd - +``` + +### Benchmarking tests + +To run the benchmark tests + +```bash +cd parachain/pallets/ethereum-beacon-client +cargo test --release --features runtime-benchmarks +``` diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/Cargo.toml b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/Cargo.toml new file mode 100644 index 000000000000..5c4acda13d8d --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/Cargo.toml @@ -0,0 +1,95 @@ +[package] +name = "snowbridge-ethereum-beacon-client" +description = "Snowbridge Beacon Client Pallet" +version = "0.0.1" +edition = "2021" +authors = ["Snowfork "] +repository = "https://github.com/Snowfork/snowbridge" +license = "Apache-2.0" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +serde = { version = "1.0.188", optional = true } +serde_json = { version = "1.0.96", optional = true } +codec = { version = "3.6.1", package = "parity-scale-codec", default-features = false, features = ["derive"] } +scale-info = { version = "2.9.0", default-features = false, features = ["derive"] } +ssz_rs = { version = "0.9.0", default-features = false } +ssz_rs_derive = { version = "0.9.0", default-features = false } +byte-slice-cast = { version = "1.2.1", default-features = false } +rlp = { version = "0.5.2", default-features = false } +hex-literal = { version = "0.4.1", optional = true } +log = { version = "0.4.20", default-features = false } + +frame-benchmarking = { path = "../../../../../substrate/frame/benchmarking", default-features = false, optional = true } +frame-support = { path = "../../../../../substrate/frame/support", default-features = false } +frame-system = { path = "../../../../../substrate/frame/system", default-features = false } +sp-core = { path = "../../../../../substrate/primitives/core", default-features = false } +sp-std = { path = "../../../../../substrate/primitives/std", default-features = false } +sp-runtime = { path = "../../../../../substrate/primitives/runtime", default-features = false } +sp-io = { path = "../../../../../substrate/primitives/io", default-features = false, optional = true } + +snowbridge-core = { path = "../../primitives/core", default-features = false } +snowbridge-ethereum = { path = "../../primitives/ethereum", default-features = false } +primitives = { package = "snowbridge-beacon-primitives", path = "../../primitives/beacon", default-features = false } +static_assertions = { version = "1.1.0", default-features = false } +bp-runtime = { path = "../../../../../bridges/primitives/runtime", default-features = false } +pallet-timestamp = { path = "../../../../../substrate/frame/timestamp", default-features = false, optional = true } + +[dev-dependencies] +rand = "0.8.5" +sp-keyring = { path = "../../../../../substrate/primitives/keyring" } +serde_json = "1.0.96" +hex-literal = "0.4.1" +pallet-timestamp = { path = "../../../../../substrate/frame/timestamp" } +sp-io = { path = "../../../../../substrate/primitives/io" } +serde = "1.0.188" + +[features] +default = ["std"] +fuzzing = [ + "hex-literal", + "pallet-timestamp", + "serde", + "serde_json", + "sp-io", +] +std = [ + "bp-runtime/std", + "byte-slice-cast/std", + "codec/std", + "frame-support/std", + "frame-system/std", + "log/std", + "pallet-timestamp/std", + "primitives/std", + "rlp/std", + "scale-info/std", + "serde", + "snowbridge-core/std", + "snowbridge-ethereum/std", + "sp-core/std", + "sp-io/std", + "sp-runtime/std", + "sp-std/std", + "ssz_rs/std", + 'frame-benchmarking/std', +] +runtime-benchmarks = [ + "beacon-spec-mainnet", + "frame-benchmarking/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "hex-literal", + "pallet-timestamp?/runtime-benchmarks", + "snowbridge-core/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", +] +try-runtime = [ + "frame-support/try-runtime", + "frame-system/try-runtime", + "pallet-timestamp?/try-runtime", + "sp-runtime/try-runtime", +] +beacon-spec-mainnet = [] diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/benchmark.md b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/benchmark.md new file mode 100644 index 000000000000..de976e121496 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/benchmark.md @@ -0,0 +1,88 @@ +# Motivation +Demonstrate that +[FastAggregateVerify](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-bls-signature-04#section-3.3.4) is the most +expensive call in ethereum beacon light client, though in [#13031](https://github.com/paritytech/substrate/pull/13031) +Parity team has wrapped some low level host functions for `bls-12381` but adding a high level host function specific +for it is super helpful. + +# Benchmark +We add several benchmarks +[here](https://github.com/Snowfork/snowbridge/blob/8891ca3cdcf2e04d8118c206588c956541ae4710/parachain/pallets/ethereum-beacon-client/src/benchmarking/mod.rs#L98-L124) +as following to demonstrate +[bls_fast_aggregate_verify](https://github.com/Snowfork/snowbridge/blob/8891ca3cdcf2e04d8118c206588c956541ae4710/parachain/pallets/ethereum-beacon-client/src/lib.rs#L764) +is the main bottleneck. Test data +[here](https://github.com/Snowfork/snowbridge/blob/8891ca3cdcf2e04d8118c206588c956541ae4710/parachain/pallets/ethereum-beacon-client/src/benchmarking/data_mainnet.rs#L553-L1120) +is real from goerli network which contains 512 public keys from sync committee. + +## sync_committee_period_update +Base line benchmark for extrinsic [sync_committee_period_update](https://github.com/Snowfork/snowbridge/blob/8891ca3cdcf2e04d8118c206588c956541ae4710/parachain/pallets/ethereum-beacon-client/src/lib.rs#L233) + +## bls_fast_aggregate_verify +Subfunction of extrinsic `sync_committee_period_update` which does what +[FastAggregateVerify](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-bls-signature-04#section-3.3.4) requires. + +## bls_aggregate_pubkey +Subfunction of `bls_fast_aggregate_verify` which decompress and instantiate G1 pubkeys only. + +## bls_verify_message +Subfunction of `bls_fast_aggregate_verify` which verify the prepared signature only. + + +# Result + +## hardware spec +Run benchmark in a EC2 instance +``` +cargo run --release --bin polkadot-parachain --features runtime-benchmarks -- benchmark machine --base-path /mnt/scratch/benchmark + ++----------+----------------+-------------+-------------+-------------------+ +| Category | Function | Score | Minimum | Result | ++===========================================================================+ +| CPU | BLAKE2-256 | 1.08 GiBs | 1.00 GiBs | ✅ Pass (107.5 %) | +|----------+----------------+-------------+-------------+-------------------| +| CPU | SR25519-Verify | 568.87 KiBs | 666.00 KiBs | ❌ Fail ( 85.4 %) | +|----------+----------------+-------------+-------------+-------------------| +| Memory | Copy | 13.67 GiBs | 14.32 GiBs | ✅ Pass ( 95.4 %) | +|----------+----------------+-------------+-------------+-------------------| +| Disk | Seq Write | 334.35 MiBs | 450.00 MiBs | ❌ Fail ( 74.3 %) | +|----------+----------------+-------------+-------------+-------------------| +| Disk | Rnd Write | 143.59 MiBs | 200.00 MiBs | ❌ Fail ( 71.8 %) | ++----------+----------------+-------------+-------------+-------------------+ +``` + +## benchmark + +``` +cargo run --release --bin polkadot-parachain \ +--features runtime-benchmarks \ +-- \ +benchmark pallet \ +--base-path /mnt/scratch/benchmark \ +--chain=bridge-hub-rococo-dev \ +--pallet=snowbridge_ethereum_beacon_client \ +--extrinsic="*" \ +--execution=wasm --wasm-execution=compiled \ +--steps 50 --repeat 20 \ +--output ./parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_ethereum_beacon_client.rs +``` + +### [Weights](https://github.com/Snowfork/cumulus/blob/ron/benchmark-beacon-bridge/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_ethereum_beacon_client.rs) + +|extrinsic | minimum execution time benchmarked(us) | +| --------------------------------------- |----------------------------------------| +|sync_committee_period_update | 123_126 | +|bls_fast_aggregate_verify| 121_083 | +|bls_aggregate_pubkey | 90_306 | +|bls_verify_message | 28_000 | + +- [bls_fast_aggregate_verify](#bls_fast_aggregate_verify) consumes 98% execution time of [sync_committee_period_update](#sync_committee_period_update) + +- [bls_aggregate_pubkey](#bls_aggregate_pubkey) consumes 75% execution time of [bls_fast_aggregate_verify](#bls_fast_aggregate_verify) + +- [bls_verify_message](#bls_verify_message) consumes 23% execution time of [bls_fast_aggregate_verify](#bls_fast_aggregate_verify) + +# Conclusion + +A high level host function specific for +[bls_fast_aggregate_verify](https://github.com/Snowfork/snowbridge/blob/8891ca3cdcf2e04d8118c206588c956541ae4710/parachain/pallets/ethereum-beacon-client/src/lib.rs#L764) +is super helpful. diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/benchmarking/fixtures.rs b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/benchmarking/fixtures.rs new file mode 100644 index 000000000000..b50be81360a3 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/benchmarking/fixtures.rs @@ -0,0 +1,1215 @@ +// Generated, do not edit! +// See README.md for instructions to generate +use crate::{CheckpointUpdate, ExecutionHeaderUpdate, Update}; +use hex_literal::hex; +use primitives::{ + updates::AncestryProof, BeaconHeader, ExecutionPayloadHeader, NextSyncCommitteeUpdate, + SyncAggregate, SyncCommittee, +}; +use sp_core::U256; +use sp_std::{boxed::Box, vec}; + +pub fn make_checkpoint() -> Box { + Box::new(CheckpointUpdate { + header: BeaconHeader { + slot: 5809344, + proposer_index: 101696, + parent_root: hex!("ea7ce4ad810829cf37a2235b1126c82aecfc5955a1647ec83640cf3f7db91bd2").into(), + state_root: hex!("56f6363d3604e61a907c774edf0bddf6477a8d410f026414bc420f751de1f092").into(), + body_root: hex!("8c799aeef815cbc4499e0b46723623105afb177a5c522ecda3415ad9fb259e6c").into(), + }, + current_sync_committee: SyncCommittee { + pubkeys: [ + hex!("adf5a4907639db7bdcbecbc295b57d8950b0abe34ab17798686643427023c4f3983550d1496f81a27e52b070e4f4e6ee").into(), + hex!("91b036b30405531cacebf5d4f7e939b44438bb9942123ee55b44453e32febfbf2c846e0e4fb08190b01a000d072dcaa7").into(), + hex!("a86e70f00161ec6c4b780b4fc631c8dbae979f1e6c9ed037dd0745833ed6e3e18831478eb4753861f339293c0508f4d4").into(), + hex!("84196b1f39fba1fb7570074e7dd2768ed5c28db7f91a6374e413c8fc82f97738af771f90496526088bfa1ee2c01ee299").into(), + hex!("b9a230fc12d85281cbfcc7f5e6b13ab17b3dcbf0adf256d031c01acb30734d061683d40fd62175da73a621440bb04367").into(), + hex!("80b8be5a3d6f39aa7362c5feee9f89b75d1e5c2b485ea9a776c60fd60dba611e9bf5ca8b2528f42651b3dad212acfe77").into(), + hex!("ac8fcfc40028d04bdbea87b4b335781e35e10f881bfeb07b94c538eb37a43b18b1a04aad3dbe80bbff6e128017251f2e").into(), + hex!("b0b2136cc729b7de8868c02de6247ff2a68694296c78f088ce967219f08cfb7be9e1830e2630b10ca650c715d85d89f3").into(), + hex!("a70627c99777970eb9bf3268bac06bdc41c2ead41b1b76d30e7fd2aefe83319461d03aaf7ab93150343be9fbd2e48e7d").into(), + hex!("9599aae109d31ddc9028c428a148ea9ebffdb5ab6a684895dbd3772c1baf947c8a255e4c7ebaedae2a9e046219d80d76").into(), + hex!("8652e099adb88b2a25ab64fb01314e24cf26dbc4ae110d7fd73d74a0e0a4fea2ce2ce87cb1ddb7c0b9fb50cc6afa2153").into(), + hex!("83ba74c6e31073865eaed3c38a8e885ee715f03cfd6e36929655b6aa790d8f676c4ac4ae27f963e311d00923357eb087").into(), + hex!("86b05425d880027fbde9be3ea526283c5b958ccb31eff997d9d7b5e3b70e2d011ed95f891248082e870d55704a471deb").into(), + hex!("a4eeb958121bc5be5b1a68b73faa83b19272ef2f2cb627431be08e9844ef9d4548b4208670754d0954e5b012e3933859").into(), + hex!("87b0119e4c54aa2b4f8260f9ace7722788578bc6d822361d011e751caf13be5ca94b7d5842df5c16e52bfb4d658a405a").into(), + hex!("b696ec7f5dc82655cf027f5827fff3ce39195c1ee4ea9fba1808880cdea16d6086fb583edfbf66608e4f33211ddf9f27").into(), + hex!("a69bafcb3af59786acf009cc31e245009156a7e4fd2af98cdf2b7e63c39aff2baba08a338cdac93e94f6132b2cfe7a7c").into(), + hex!("8aa0aebb24b8c62168b255b6041474e8abf0d589a7467bc4712d910a9c2d470432d251e336a80dded27c0e9eea43aebd").into(), + hex!("a08d6976d3579411080957dfa2ca9487b1c4d8dbcdf640c1fea0a46f2c0228c2ddadf0553781c3e0dc5c7503b1bf29a0").into(), + hex!("a4abccfcfce6754e4d6c5c8bbad6668a55dc555ba99189936385e4dafa0435b323b813ce69f76e24299b5842c244139a").into(), + hex!("b89e4fd2dfb46c2af6df73f7185693ac535b67ab31f2805b1c24f400e0068cb32aff164b53668512f5895883189f7c02").into(), + hex!("b7c221a5884d10048bf9dd8611fb5231ce444fb756e59dd60d18a2332e889c014b27d739ed6012ff86056c04f36a87f1").into(), + hex!("a82875d66a4da52d6eac9dcd9ba9c728332253ad4b83acf1601a34efdd0c398eaad4acd1a948203d1bbd4a17d01a7b56").into(), + hex!("8c0a8ec162b3d48ee6a0f39620432cd67a1eb33a6d6f7bf1aada4f24c7498cf2e2b2476898f14c875f5efcf439aa0bce").into(), + hex!("a4b5d6451ae5baba3984bfbd5eef59543bf7c923662ea182cec6bb29aded9afd4c89e618ff756b5290506e0bf5a7e690").into(), + hex!("80664b43e3bd2f6e8eeb81a46cdca4f571499f3f0c77eb007ceb33c5b3dc18348a68cedc19d9967117f21a6c1ea29060").into(), + hex!("9174e46939c6915c757e793c9a02e46ed49d869216b720d0924b9697d94098182cad2cc6d4caf4cf140445377f1b4c80").into(), + hex!("b6e25cc134e089d306c648228d84aeee9516c8a276a2d37b54b458314b8f8980d49614550e821aac31000a5e7d518fca").into(), + hex!("ab1ab623a70f1e33cdbee356530a6f8fd9d00b2f9d8f94c0854816e5dd2bb8b258b5fb7a89ebc0725d8ebb74395847b6").into(), + hex!("8e75ea6f0678abfe1a7a9201c9fde992451327d61290fd803b3a1cdf2f7537fd7c23e0e06af5bb886a28928d0baba1c5").into(), + hex!("b41f4d4421de7b94eb9bc61602d08d77dc3f5f5d025e04f44c1426f14e8f707031b9e1d3a6e92c200f54166b2040f0a0").into(), + hex!("a8fa0c61435b851f9bda4da8dae0d544984ba5c0fef338eb6896b9c08306c3e2aec0a6c2028da319f210387320df4f73").into(), + hex!("a884af449df4cbd39e161de10f9d1f645eedfae0d259ddd84347733836fded047ca079916d365ef3d93fb354c8c795a6").into(), + hex!("936970bdeffc92f32915d141ccd8334df7966a3cafecb6d33fab9f477c389179f612cd6e368b615a98112d71756756aa").into(), + hex!("88dfe631c1e16ec3634e06a83a857ce9c909cb5b05d40490e6d02e553dd3bf213f0e178d31b4d913a4796b7226ab3ba2").into(), + hex!("a0c8357b4fb9c4431bc88e6336c2218590a7cb351ef4405a80aa6d352912f0b3110f3a09eef337bfe98da6b0841c6214").into(), + hex!("a7def54e08e2cea7def767d1108bc5c24d64e2dbdea9d07f0c8c63e60eec2db4e095b4a84bf6a4822103560b0497d1e7").into(), + hex!("80991c4f933985d9662d2e047187f244dcbb79606410aeb66ab250b2fdb9bd9daa392339e9b16d0d07648e847b02f942").into(), + hex!("b63fe559e2b4580238a0b0ce52ab8258838c2b64c1922c56b64cd65be2f88c140cb4e6ce96932e92f1ae06b20ee9e613").into(), + hex!("926ece3480c5c1f24f03d8289dcac7d3b202fcda277dd4453294dffc2953d91c842653a9e272b76fe2cebe1de3aba63d").into(), + hex!("930d29990821d26a748018bb6998488d5de811c8ed506c213c0ba346c8011e2d7c2235aa427a320129c1d016948eabf1").into(), + hex!("ae4e3fcd6c99a8f320dd4d160704db6baaae9aaa885f5d792212ea03cf06a459eb4a2730da9161ddd72a5e6544744e81").into(), + hex!("a01ae9d4f0008efdc5203abfdd0807ee7cc58c49cc5946d0b991ea2e069a224f0d99ea714b4d5cc464deb57372790660").into(), + hex!("ab17902ab255c575a133ed47fbafd62f898748ef6fc33455a2adcb2d4ea72c31255a9cd0c545d2ad8007a5a694f13371").into(), + hex!("86e2384e0469ab8ea9c5628d619d10336b3c0f334969dbc3335dfd72ed9899639fccfb5d1d9d6667a0dba20651584100").into(), + hex!("88ededd6ef3502be7c3c1b018cd814d4a8f98a7cf9521fe9cce6faaa6056315e22828397435b57d15e996fd15d7d700a").into(), + hex!("affa898a30a0dd2ccbe8d46c5a69dc1a8696b311094ac00e8ef6d398f6f132edb2d823004bc7895ac53bc09b6fb1059c").into(), + hex!("ac9f5bc3e2045a1eb356d88f2a62781fdb5775349c9bbdd9417b7bb7a9132a1cd42bf4986fcdc706eabbce45ab9f1cc9").into(), + hex!("986e3813ade7b9533ccfdcd76ea17490bc3dc62c8a596c8d07b246c28d7dea465c4dadda05111d2793d72d317c7c2833").into(), + hex!("ad4fbc51581cd520bcd0b88379ec0482c94b2ac344ce48a5facc1cf3e026edb088ce5cba5113f2c31f630a1fb37c5214").into(), + hex!("b499bfe19a22900c46bc0af1925887bfdd62455a3b85144e44b9a0a3756548c72b4f5c61f21c15f5e70b627d9f53a9c4").into(), + hex!("a5f40fcbe99b494c6acdd65ec3516d5a4a784ffc501e686764bf655c1f2f4bf784bac6b85e961d6a5ae513555a638323").into(), + hex!("a44c695e5490bca1cdc6b69fa80d6540239e42ae1765bff39285d1798696574c409e27c288b3b57196cf7c1753366969").into(), + hex!("a1ec5d9b8e55803477fbdc454d9c5cc605ead24147e5b04a03f80558a14f1e08bd6557f3fd11dab06a2816d32138cd43").into(), + hex!("98957c2e0a82210e2329b60dac05dd8b219ca00b18206227b725d011bb2f8b0dc1a79149a7d49a9a5505301b1b3847c5").into(), + hex!("ac8df8b1b596039c24c185108c89ef4da384e92c957c84bdefdab923e4be7c0e9480d837e1ff2e6269828b86b0b9f6c2").into(), + hex!("9358e3a48d4938b189059c3696169d31339851a1f4205dfe4de423ad4b96e7015c0dccb8bcebb64c1c357b65d5da6914").into(), + hex!("af1ab721df52d8b106449aaed05761b637c38f4c5513062afa55a09b94c8b088fe98cf430be4633b4f2f818da545b37e").into(), + hex!("8b8c063568a13f6522fc66285ab116a06d5e226a72f5a00fb321dd9b7d0bdb53a6b46ac17a1c9faf468c19b128c84488").into(), + hex!("b537f79bf008cd16711390c188c52d8dcf23cc41990c82005de2b0ff1fa09de85d3c34328ad9124e108ec252ec667f70").into(), + hex!("b9e8cf06d584197093d4b4b1e1f745786bb1f9772c0a771b321ae2dccac167ea79632138d60d8ed6dbbba2accd0a3c11").into(), + hex!("a674106b2c965c708b98a9f28a3f511f5dd8dfe7b2b9ca64a29152c7fd8de428316638d5091412e22e50406c372d9950").into(), + hex!("900a4953af5a28ca5b6f789a0dab9a03e3e5b9a0e866465d371b871fc59ecba31b524b0fc414eb7467d4384ca1e4ab5c").into(), + hex!("8b12a6114f8e0947995d72fee19e9204a4b552a85743d5320c1942bd2294d52a2d6d346f5e930ed788fc929069733297").into(), + hex!("a118f4b631a0bfc00df74a94930d69075b83be0bfec2affdf5e1ca4762d40592132108a7d2088891e7fe078c84ea9d9c").into(), + hex!("b074d18f84787d245018a2dabe2f0a51bf2f3786a802317122982a598f7f953339468e0392665c3f3a6b8fb17cd8f72c").into(), + hex!("8e6230b8186009b765ae6b176eb7dbcf503139472c0c2c5574e3d608a49932f0b852e744d2e215c58512ed2b4e8178da").into(), + hex!("98cd13f8400ecc9db458cb81840ed6467795f54f25680931881808909cc661230716fcf8b3fc2b18f4c12d30c32d9e20").into(), + hex!("b861f89275776bd640bc7d0d347df31e784292fa14ec468a6b0e8d64a9b077b6bdae1bdbc56385bf8f5676ba62002606").into(), + hex!("80ac9088ce82fd7a90c91b24e5e91636ed76844303a3b1105bc284b4e9a860acca3207d1804200a55eeb3ef45ba97558").into(), + hex!("940bc07a53373f075128b6c12f59120bc7368289d5a960c55d52abb68e8444f6f830b07850d31a9ed137f5357b38406c").into(), + hex!("b8c5919657811270976eb45b9f3f09be798fbcf6b34ce7443fb36207816b6a84d2b945a6e6522480c2feed12ca980df9").into(), + hex!("8294e6ce7d7d3b77bf48252eb97cca47a5b9dff1b8c97f8b7af8034b938864f38bad932bfe44a9bf4df81e394aee7ef2").into(), + hex!("8d9075a2c42c1cf51e082ab1a57a66670f0e81af2a651aacac5aaaf3876711aabbc0c96e44d883e6ad8a91a81be22940").into(), + hex!("882aea71e4512d5c41b5fd6509ede0fa35a49fff9648d277b531364613342711b2c7520a5f8d58ff31b7fc9407f968fc").into(), + hex!("8a34a824cbac06cd99fc68f36ae09c6adf2d472fcbd378cda61b838c29ca9750a7f7d63d9b61d1f30deda1ce048ffd42").into(), + hex!("a4f027b5466de4fd633cba98d2c8c78db10f91c8bee76c32ca5a80c5e87ffaf19df84e675f31bb6c14ee733ac3d4a33b").into(), + hex!("b990db2d743b389132d4d8281701b348bfc52f707e6553f309e4f59706871142e6b8e3d081057a5c43243911148318bc").into(), + hex!("a4500c88a58971460c33a404a65260a9e21c4a345601d1686ecc352ab088b2bd93d30930383901fd2d042dd037286761").into(), + hex!("a4de9078286cde237b4ff2da52a0fc1b8fe7b931ac35b3033dca0b87b5c86e58bec951a66a7308fc951ebb70fd0d0bfd").into(), + hex!("a9adb6dbff69b115be1cb37d5ee3a95d8c9f466f059128b6ab197d4119f6c0f87d1cf4d14d4beb2a43f7913700c1d909").into(), + hex!("a8a33f167e473eb2ace3bedc1cac2281bc9f522a0fdf6a9eb365859b9116067e07b7c380e8ea4dd33a4fbe23e2412be5").into(), + hex!("88c3785f853a192c50c38040ed52c413084dd069729cb14d806223f8a51aef40c21648d1e03bbbca031ce811e1b708ce").into(), + hex!("a7db446f88ef0d018675c0e7be0e9655d098529ce4b4c92ad809ed9f588c3f6c6dd98267ae0efb1659dc16a29e1feac0").into(), + hex!("916fb4b864ef46a2f8f570364cdf02c93d8432d8155e7ad1a15777d7f5d5fab94797257d0943aed5f16033528a4290b9").into(), + hex!("b9b7ae2ec02b0694d7828a69bbd1fd6e9070cc217319dc4e8fc854e48f5ce6e133fb0d5492e2f87e7cb8d3d557f17037").into(), + hex!("8cc36ade2b8039ffd41459272091d9cc4c46a49a187e18e9e3b283136831724313ee6eb5954c34acf9ecf7d79c30dcfe").into(), + hex!("b89a28db91eb06f191731a927445ca64cb685a206f4d77f335f510eab3a4973eb1d199525aa12df17f97cb4e079bc35a").into(), + hex!("87fb2881b92d5d9a2555080afe033512fd93306bd25f4a841d034c0fd9685ae09ecd29d27523e8f18664cdf2127ae6bc").into(), + hex!("aaf63cd83256de5b8558bdfd7f5fa44b3b3cd767983484ee482241286f82c6f51c1de99e2c03c8c99e6d4a27b7379cc1").into(), + hex!("89b52eb17e1c2868a3da6727e6f122046739c99cbb6aa7e30f65d7e649d09540b7ba77ea3efad77be597e48f7abe7643").into(), + hex!("88d097674b763a770872f17266f38200a4034347756c5ece6f11fb841e82447a75f6f3279ba8ab54e668b2072c6596ef").into(), + hex!("8de58933f07ac60ae919adb4be0becfb5e6f7330708a39cbc4c988c79033f0ffb365ae2a6308c234c218f0e45ec1ce6b").into(), + hex!("a4ec080db7716a0eae593b0ca91a16273abf534b8d153232e4b7d613c9fd21081bc8442d6e57a6e8e026c66c93bf0289").into(), + hex!("b680857b566466d1fb92d78a09b75d93753c275249dd05a17702e48560e4df5c7f9ca68370847250fd001433972c8b0e").into(), + hex!("84ec67623fc58128f4504b071b53e3c0fc60ed07318febc8d450035567691e2e58468b6799e5f3d93536100bb4b5f3f0").into(), + hex!("b22ce364b11e164d3df52050d59085c29c398556617277349637290b193727e3942064693ba1d7bda313905908a071d5").into(), + hex!("876198b5734f1ab5f7ec0231110e9cbe59d116ccc410678d5e0108fae42bab5dcc7cd15df200bc71f471ecbeb0e80d68").into(), + hex!("b081fc87056c34a8a78cc34f308502c5b6509adb0b344e83227b997aade90d6f0f1b8e5302601ca3217ecf9dcfa24ddb").into(), + hex!("8fe71e517db52831a0c4adb83fc6524817b9a358fc6bf58c03c91d4125482921936391e54c767ad2f13071e0e5ea266d").into(), + hex!("91212d83807ca11b030e5ab72ec85a8d13d468c714fc97c20b5d0569c382ee270d2ca486c88354994ee54f02dacbea50").into(), + hex!("b022bf24d2ea893125f3a89186f444aa5f301333f0667eb5862586a5eea8233c90202064eaf9363f7b367a6dda15d45b").into(), + hex!("b1f45fa596a32d3fe267c830eb042432eec6b79efcd5c84ea835d7108bd4290fb64749ebdf7e7e51e6138fedb3cd2eb8").into(), + hex!("8567d2366c584d975bea34d4f9e2ce62a62765125326c5e56b5cda08fd2f2e7f769db47eb514d2d9324b645879c82a66").into(), + hex!("b4816fa77c76e1f75e6fa903a4c0c031ac7a5f5ccb5f553b4eed83bb34067480804c0f6f308f8d0fd723dbe2198b0608").into(), + hex!("a9ceeea8878d799fbb6d52dc4112ae712429b9213f72b284698d68ea6827432f34e51daae1727b5402916a836567f611").into(), + hex!("984c8ca226df18ec574e0fabd6dc9ab3a2e1b319c4b6ab5b19872239833dcf447ee5d720305b2385d65facd297704809").into(), + hex!("b02fc16b3530532a2373776e2512421d6d2e42f3fd3c3e71393706d74ef9324571c8c1ba7b9caf65dedfff2bad946d71").into(), + hex!("849faa2060a75d08850b54e06376f252d9cb4add3e740225ee37d23e29f80cb9f98188a7eaf6a381af4b4bcc9874b792").into(), + hex!("b5331ab6646a8be3bc37c5fec56a597b914683402828dd4098b189f245c638e063c933a542c0122f063f98a9468687b7").into(), + hex!("868d8214f26e14e71ae0bd514275ae9442760af72269790228e733005293019984d3d463c2bb82936193dfae8c267fa3").into(), + hex!("a88c6c4af4b9d285997c9b5cca7e22ab9b8e5873ff36455bab6a6dd4518e966079524f8c35391811f16eca421f588694").into(), + hex!("a6f508524a938fb49a7da70251fc8300192882a3fe784f0bc51027ef2193d90c75d0d0720f2f5be634d81f38f3d85b8b").into(), + hex!("80a923acf2aa0349b4852f47edec37cd47bd74447f2f91c110ed092d015887a6625d5f1fd1f5d00c994edbff1435956c").into(), + hex!("84ed9f1ee0db4c9ae55492fd07fdd44851a6d0209da5b521435229b1b45d57c3b835af627122756ad0e63e915c15909c").into(), + hex!("b4c4c1e6fdc418bd29cbe8082fd774f678dc28a51896a51349c888ec98124b7a522baf70b820ae0729696624f3695af0").into(), + hex!("a8cdde029503aa3e23776a67952b2bc954fc0dc06f07a78bc94e6408edf381bdadc29efe4e5340a9b7efe99a3f3b3687").into(), + hex!("ab02a92f5ee21035e6e3e40a026d8d5680f98afdbf82dc037dfa30a87a1c101387a0085da8474989b24196ff494aa618").into(), + hex!("986c88a5d0bc6ff4127e34e0a5fefc1290936ec88d1765e776e199601f9660a8425c1fc6defff07fb81576461d0ed7bc").into(), + hex!("abe2259e880aa8b587ac9a31f794895bb18ff1bbed378a70fa09388e5a6a7343c072401c856bdd92fc2f60ca11056aea").into(), + hex!("8c04c98815c0c1f281c8783ff8098b0d806039a39fb2f4642a08f821c02506bc7c80ba7a1f4b225ecda9971ee3a22121").into(), + hex!("87b7bb0cce6244aa1a540941f0ff5ae4126fd4c62bd98d34993380a35e1c9a9f43b561506e5eafc1ddb8aef131403590").into(), + hex!("93cdf5f956e8b40ca0e31cf559937b997897c137a411930ca28075899abb6c08dd6aad5b2bfd5c09f07613a6854b3be5").into(), + hex!("83ebb284e03b4414694522caedabb391062ba9ec373e94427feff071644df91635ef498dcd9351ae259c5b15d6edbb38").into(), + hex!("80747223ba06d6465e26a354b79c42ec0624d33cdf015da7bcfb31f7009d92aaf6321f4a921f6b38e0a394a412edcefa").into(), + hex!("967b4b10a341abc5c01ffd413103c6468e3efa32c8ddc7e8622fd3ab2a765a420e0be3c81e1da05679becc5bd03e59f6").into(), + hex!("8a01b90d551f26c265b9987c67b641d15bd3b8e5af5c25472037645da05d9d54c0e59bab32578eeb3c1c5889f4fd9aa1").into(), + hex!("b6b38e40e3fbb31b257ecc18dcfff2fd850a41c2cfa5a642b0c383cc1a86b2b9adbcb22130665f544f1d9fdf87e92dd2").into(), + hex!("b490529e0da56e7e4d4cb2f79b704576c8b6569e9960dafef059dac0144b29ec337f4beb515465a57414d8965268a3dd").into(), + hex!("8a3b14616bb721543bfe007f1a042e76ef068a6e4f8964d68dbb7a733ea92dfe4e51f4008aabaa01a8a3566b00d083ae").into(), + hex!("8b82dbd0d0592d45d6309c795beb42274b74d844cfc393b34cb6992e3b25ea8f62f777124584eedf482949bb999ca5c3").into(), + hex!("918226266d7f02b2081edba64ca4b70339b7a63ef9194cd77a214620bc25618495cb4335c0c3621c75f821e685af3f1e").into(), + hex!("a61f56310689b9f383b45e8c8c647bd7150fc6dc3be96afb464b0c67c6f8c73ac9941bc8a5b0e2093255c204646c94af").into(), + hex!("abe204f55b8dd101bbd554561c1a7b50c01b31c967f6cea18dc898a10021eadca3c314f6b7afcc2251682717540d2100").into(), + hex!("a0cc3fbc4a05e3b5f93f4df85b92c1bed221f21700d8faaf84e99954cf6994d0052c8ba8ab894503b5515bdd1460ac5b").into(), + hex!("a5e5e175726b31163e13447e360f835ca64c3883901fb1fdc275b487106b39fdf43b83ee8f3985dc85157b94aaf8c389").into(), + hex!("b34106e71862b290f7bb47e5492417b07b541fdba23ed474f29d666cfb50bb5a3ea137ab717a41ff769af53ab385a3d6").into(), + hex!("aab726a0317c365aa15ef9527e5101b1a90cdd60888b733cdbd61dffac3374f995206abd154d099b2dfa03dbe666d503").into(), + hex!("b3fe9956454604b2aa1d51480ae96182ad1a8af64e80adbba1034619090c23d0d7ddb4163f400399d5946babada2f5a1").into(), + hex!("831a0d4008865576d9d0200dbe80eeafa7e6e6d442a46ebf949f39e32ad311995535a261795a7e27f2b23a6d506b7a33").into(), + hex!("8d7fe284c9ea1a2dce6ac70e3f225994f65ba9791520fecf5359b80f7c32c1e45e75b8b787ccf24b83c79301e046bb4d").into(), + hex!("b00df7ea640dcdaec66317924090f49380edc5c669ef1249ec8a24a3436b4bb41be0edd4cb0d04bc6ffd540ac8efad18").into(), + hex!("aa55d72470c024627edff24f9a19ae958d6b382bab6a24581183f762d736ac10f189ec3f34a7a41516f81696352f16c8").into(), + hex!("99e172cdb14a23161b5e8aa80121d98e69506ae0ab956912eb2ed959b73ee901852f263cb65798554ec0ee35089b4c03").into(), + hex!("84ce8ec6a7debf3cb2e53afff7d1f68bf75b7b209938192c7675286b17489d7996ecd9514c5233af0a17390b9982d805").into(), + hex!("943cddf3f5a6dc04f425aaca25be44438aabc7a661476761de596fcef5746f9d83c361da3fe1f21227ee5b9bf9a3ac76").into(), + hex!("aa81e66cb01e77c5db882792e9d896c83aa418b12c3b5201e1937ba5b74bf5fda974c82f6f40e3ca48bae72ed93437db").into(), + hex!("83bee12657fb462a5988ee26e2d0ef8b11e5fcec108724f6135e95913d7f4ec338031b697b24fd7a650cbeb088b26733").into(), + hex!("ac0070705c447e635b8df509de9ae03ea9b0314fc58b3befe14c316cb7b70ceaed081aad115a2126ecaef630c6bbab0f").into(), + hex!("a1c9c3b6f28c14ba91cf063153c50253d82440b81dd8ef938e181ef4116ede0b0ec62844e1d7e8b387668ddb8644852c").into(), + hex!("b41811ee8e385836fd709081a9a65a33ae1571bf943937615e97d49aeec3289624882915a5f5b6ee98601e752bac1212").into(), + hex!("a59cfa6d60f5c3b62197d3058a9b42c66bb841ee5d67ae34ad452dc70974de0a56a770c4ee5905c0d214c3d81a5269fd").into(), + hex!("ac47ff714d42056df3962cb4494019c977fb6200cdeabfa3ba85ec7d7d70c7d3ff4aa05e26aeae6ec6a3afa460244ea7").into(), + hex!("83c0ff348f1d018485c18417037016ec592c249830fa649b27754dfa70b94a549a42eada20ab1c4de2a5a513d742186c").into(), + hex!("ac5ddeddc94d18cbbad0e1889a2b64e91b3f927d3eb666ba018807f1e4e1451a43498ebacf12fd370b62c5386e36fedc").into(), + hex!("91606f0315fadbc42b1b27ff35b5601196a7a8beec3d5c76643e38ef28f0aef0aba9123bac7ceff0b297ca53727edbd5").into(), + hex!("af9cd077736f17c89ab4fd21fa2cee63b16f67277e9c5d54663f6d5a7abc3141f3045558899da70419b1e92ce88eba86").into(), + hex!("ab0757213aa3fcdc326925e0dadc2206f43c53f7abaf34a077f1cb29427261b4bd9981dbd1e33fedbd77fe00bbdaf8bd").into(), + hex!("8c97d256f9d4e0f309522f3899c5f74fd7e8c4dab6adf4886e7b058b323e294229fafce28871fd39e5c43f28c670b8a0").into(), + hex!("86eb85ad6fb7a3d5cd9aa5b22fd648fe9db688fe663c835abec75a6bfb67af0df0421d24203083aeb8ccda06dfb230c9").into(), + hex!("afa2dd3712eb94c9097135a69573c1f373ee0d7916f4ccec5a62445726aa4c1548bca45038e1a44ab7c8b7e3ea22dd6a").into(), + hex!("91e82407c442937af665ff8952d8b7ce3d68ebf807166aaf0fd710b76c65b39283e511b3314297ab0f2a9c8a2d76ffbe").into(), + hex!("a52cdd05f6e254c6da7a00c9210e33a49658f035b78bc7f15b527fce20c3893d3f7dc27a616eb3e107da060df251b082").into(), + hex!("81e6ddbaad6a18404832d2923697ea8df8dc3b39e53390269f197b976b5edbad074639e1e7bb25ae87b00681973fa021").into(), + hex!("a776979b38184661cc36ae9bf99b98cfb64babe37b16ed7e16e33e2187af71d9f62af4fab2bf0671baf3172727741d79").into(), + hex!("9395a004323f4ae604518224292a1fdb359fe9d4ec2a2262f13fb33d90a9dc50040d03fa6315a5ab2db043e7e16fb971").into(), + hex!("81bfad9e94c00fc810c4e63042fed6dc54c7c48637064376d5a4df8c8d6be3c2eed335640bf45bb8df99327a7e070d06").into(), + hex!("b6b38236ea973f91eff175206c4328cb97335bc8e498d9c9a2040468885f7d8464a8a1168929cdcc4c59513885e1589c").into(), + hex!("aa14a7baeb7b6d0048bdb8c772de512001174f764b37396c6481bff5aad30abd6143e4654a3d80406f8d08948ff8145f").into(), + hex!("a7297d6c09873e481c04f2e9e9a07567d78da504d2929c8b9d8ecae1c4d919611e061caa632776a8716b20e031cfd203").into(), + hex!("a0942935f58ef26a111d77b1c4598207eb6e3414c106b286f1c4dd344b3e70d3a46595ebb657e43f0e71dfff3b532382").into(), + hex!("95dca5de041e96f8c64f945817ab1ad62414b3002073b18331e288878c7a889774468d3c24f04e0714958ebd79ebe71c").into(), + hex!("b64f58f4eea309ea03c60f6ee66107fbe45c5ba81b8ea397a515435a179ee86bd098cb9acab4f374d29c8a388152fc6b").into(), + hex!("a70fe2ce7cfbdc22183a1a81c779c6071199768ad9b39ad0727ced4fcea5fc79e9833279ce93e1ef16cfc6dc0ef4f15a").into(), + hex!("8169d05ef0406b661022af53dde8ccd7315b3e35065c568673bfe5e59828480312a8ad418ad431beee12e7882d11142e").into(), + hex!("8148070a20eba3b61bd168e00ae8262d698263a8f22ee01ed6d46d154a08708a85533f54935bf92ee6ab0c04569eb3ac").into(), + hex!("b5e4f8b8a011c9cde6a9338d7c751ce2828fcf41b40e140ecf543150a5b4859f87836461d0ea2ed7cd0e6bfb8febdfc7").into(), + hex!("aab7b0022a1791339fbf567e771c43e9a2a46fcfed394b7216b556aacdebc259e5fc599eca66b12c23467b2443fa9c76").into(), + hex!("a843e5929fa14bbdb5f370d28547a7b585443f4d2fdf8e7237fcbb93a5220d62c8033665996f36288127a2bb4822f357").into(), + hex!("92a5abe1a8d508193c88827f93156e84199b14731a68b0b434663e5b9ce8e6e3005ccefb3ad8330c56fc0898eb9334d2").into(), + hex!("948c5a4bce25157f5f779fcdd89bfb4747a6178d464d15148742920aa2ab7fc63d6989b586152e1e79eced93f8686206").into(), + hex!("8702f3fccf470a294946970f8ecfed499f5ab3df799601f872d7be3d9227ff78a764550fd1a97ab25b7be96d366c82e5").into(), + hex!("85c5f99e913f1cb67a30807386b5292c841b51e959a13912fef2e0f4ba84ab3b1c483dd5fd33e80774de19695b622888").into(), + hex!("8a903e39b9b46dcaaca4fc968b298430b982ed3916f8ad533ceef5131dd507f1188fbe856c80bedc7bf34799743fa86c").into(), + hex!("a91ecd938c2a399b97576c43c5d1621fe748732090e360fa1e3ddd145438f9569d39a7be9d032b435a5d14ca4c905d15").into(), + hex!("aba9def4db5bbf2ba185c134f7734feeef976573e20d76aee476bfaa2af389ba5576a1476aae2d42d5470a46ca3f58ba").into(), + hex!("91d3529480d066817c0111bbd92714a40472ed6c877df358de98f0258f79fb8ccd54a4fa8bab3b9cc15bfabeb620c196").into(), + hex!("8cbce4e674d90185c47225c587dac654428427cef8a563cb89aee6fdc2ae6f12a8b11be46c779ed9afe38ea97d7d71ec").into(), + hex!("ac684cedfa58b2adbb6a13a94aa8398ef4a14970f5a43a344986cab68fff7fd48f7bcdc0506026eb0a2867efe86f283b").into(), + hex!("a21a2f8df2b811550d6e115c095d4d6781a84ba25b7f4017adb318776ba7452f48ed8b83a6a94aa68d83f2226a4c0549").into(), + hex!("a0d96e01d937144627c695aa4256f1d1a16c708894ce854f5ac656585e6852a43c39080909e7029b6611ef519d9983b2").into(), + hex!("a2f32ffa61e370d087058cd3ffc534da6a917f75ed5de568938885cf5220d474c930ba9bfdce91e031aab3b3167ad362").into(), + hex!("aa6a7c0162520c6706ab0f6188b718c1909a4aa12e71afc1c2d40e51fe44f667db0e7f1f0cdd81594447e267720f2dae").into(), + hex!("b4f16474ec3f37765e8750729f3245167b82472ec454329c9183a5d5ec939041d85b83523d11f2b895e2d15586f81422").into(), + hex!("917af7d2995b6466baaea2b3eaee5f76508d0c117d0452bca6a07ecb87c0cf595161abd5bd31a904e05684e55475a4ce").into(), + hex!("93dc25ff6a8ed93fa40d198a97955d40a5f29e50fc6fa6dbad34582478d3d1bbac0ec5789f11a92a738e533939c281ba").into(), + hex!("a41c824ff14ff5ee486a6130dc6cb01043e59f71e234390d464c95ac49ceda8b5400079ed4edecbb59be2083d8f06da7").into(), + hex!("850da042d678ac0aa31dfe0eca861ce17cc306188f260195fd10f940c67d42c9431cb68a64d27232e989c9c23a6e3d1a").into(), + hex!("a3e223f30d9782fa1fae634497c64fc58bc8289e48a67c8517621918e2b921cba1e90b2b01f838ea36071ed89bf64ed3").into(), + hex!("809001b01c33bf49a97ab6fbdf708fb224879c71679a2b335cfbb3cb4aba3201a32113de289878606c9feca057d9faea").into(), + hex!("a10ee1706f4c49a9cb2fee4ec6a0dbdc883fe40d4e1cd7a0388f49edf1f5f23a38e6075fa5fb54fd8e77ac3742266a6c").into(), + hex!("8c96e17529d7051f09f93faca150f5313e4d7f32235a4af6d12270780d6c14418749489a2674c728095a56585e0ed924").into(), + hex!("a9a7bf56eb25c9bda003a70128117adca9c33c6bb24bb4c381ac405e014d9c75aed0d704d801b9feacdf81c3b7f0040f").into(), + hex!("ad53d11d31bb9ea53bd23d673fb26211ee39ff4442e9efa1259bddae866e97bf07b0f9ca44e166b3b85d19b5865b1612").into(), + hex!("95e86d1427c8abd87e7f966c2ff9468d0bc3f76175bda677acea5113b5bd0d7631972c4172220e3a72e0dad1496bc14a").into(), + hex!("b8894228542dfbbc6eb65c9adf6549eb4ade838701356e7d672c095b1f997be5bbf3ff19474ee99e81320efeb04cd529").into(), + hex!("88bc36d6d90424e86374499e330ce5afeb63164fe81fcb4d56c5c997e07093a37df33437389879895c8b2cccac28ed0e").into(), + hex!("8ec1d43488aebb9544ae0a12ac7311bf873bee05caafca5176e26d681b881ef6b5e3ae5d9853b33577cc21d3acfa1e82").into(), + hex!("aec567db9a542eacf68cf4b7b9682ffe0b385dfd192296f8d8cd9e1db9d7da0b4a4a0d0ec2419825177413faad458cfa").into(), + hex!("ae242e9e2c7ff2c0898f92e7e9742ee5e19376ab97195c4eea0487490068199d0fd7ea08b832c43e208336b5c77d1947").into(), + hex!("93447c215479b68442636384d29fd5b4815cff904668e909c67fdcef1cf5d594219371f62edf189d6e54f04872705947").into(), + hex!("87f993a564e69e132c6cc4874fefd83bb5032b98bda5eaa8fe9e1713baaf08486aea21eac3231028715e846e33a3fa23").into(), + hex!("a9c4eade07d3d51bf733d8357005e08a5f86cb44c3dc6b66dccbdbb67cb5727bb456d6092418275f34b59063b3fd64eb").into(), + hex!("8939e3cc9c1dd203d8079aa4ac0d40d2e1b85bd876616bc6b589b0bd187701fcc36c52d79ec7f14b5e54fff459c99028").into(), + hex!("978122dfec6fabe4f737a3c9326f2f721cc212455001ca7e09b65b70ec1ada1ae26d451632f31f648cbc65a3337250c2").into(), + hex!("8d1eeed7fb1902deaf7d6dec7c86807c4aa8ea1d7130d6caf01d65e36f6a30e3442a97ad6918e67d2e17fff4ebe7a97c").into(), + hex!("a16203ba484b5b02a1b210d487a54c3da41b3815c307a30fdbcda0c3f5f2205e16bc7232e1b8d57d5f58718ed4941ec1").into(), + hex!("89b4e7bdd90323c53aa502a9839f57133ab0cbae1cb133fd0beb54f4d7785988eab89eb0bcdf61bf62a29b341befb883").into(), + hex!("b75550a71a4144a4f23ee27d86c10c44e8e57c118ff4f9a2685762a98a3770a6c2d1fd9229f9792dd4e784e8b2eb675c").into(), + hex!("9171ea1599ae47b04ffc307dfe5e49da0f48835cda926355606ddff47b18ce3c224828ddb942e63dd8153c273105125a").into(), + hex!("8e0e78d069f4d51b9b0c370100a9d10e395b8f88d009e33ed7fc4959bf140176cc316843c76d2a741a3471d56ced5db4").into(), + hex!("aad6b97330e76b22781e78ebb2fb2e92148d74546cddc7348e7a7f0563b986a7553907c8946258cc343a15a8918f7491").into(), + hex!("a9e8e436356d44c945d8248d249e20f5c50bd147de94418d4f04e1f67be2319e4d2a7291981378a1e457874dc91a9948").into(), + hex!("800b092bbe1f56e78d766c510dfe42f0d6670335f5931b3f821c77689fc11a502d7c82d2d887ab21caf312f8e5a037f7").into(), + hex!("aa8b05f90da0056b7659c26171df70c748d7a8fb52bfda42b7d129df386de331c1fef9d5ad1b19f0452cafbb813c3ec6").into(), + hex!("a6bb8153637b097a905342895ec1c927faa92ef8d59af86e43864ffeb6b8caa3f5b025079ccfa83214332aa4f6b71a9d").into(), + hex!("8244cab3e6f39492c8fda490a363dcbd8af265dd3a158c2af0e66182b48fc2b49f473b402bbf0951b42da5bb669504e8").into(), + hex!("8696508e20c144ef2cd954fe420c60f8432529b97a865a52de5292215c448984dd591170d88c286d7bf5a1cf8b94ae53").into(), + hex!("abb5b057c6b91d51df313b4690b15a218dfac6a30a05041c5cf451f515062eb02c54ee6cf6ff2df7640d15ddc7a95dc9").into(), + hex!("ada583911773366a4ca0b5d407520a590e4f3c6628c6d050f2641655d1654809b886807c1efeee9e9ca187b79b7676c8").into(), + hex!("9730edb86b7161715296bd5267bba55d3bd956dc9f4c640df92cc8aaeed8ab2dc1ff74988ec2122f6c3ea57b6e30dd91").into(), + hex!("8da42bb48c6f17c0e96b5edb71ed5e937c9aa65af142234e3ce61403df7f6ef05a4309e92469eaf68d83afc5bd800373").into(), + hex!("b3cb17866a99dfd048c4ad6024823841eb6602c7e4728340f1167b8af3c810926f0eb3e1a0f51ed6fba4a80743660db0").into(), + hex!("af121178dfa05ac08a2acd56f895f444e56968b703ec6b6cfae1e836d78afe6f51021d4a415aed89913df49bffe27ec6").into(), + hex!("958d5dad1aabe840881f29617dcd2f759f220974515507b0a63b3487b4cbfec69be7d22f4f7f45d693101177ab205303").into(), + hex!("ae6160e53c2c9ce5495bd0f0476703684d854048f2a8bdfeb6cd1e93fda36e44d879531f213feb1dce706d35f9fbb04d").into(), + hex!("8dca41a5808d3c75f41919cbe65a226355df9ebc7c1a2d2263d654bc66d1f5786ffba84a1670a7369258bc92d6bd68e6").into(), + hex!("81585a607df11d0a5dd778adfa1eca440a49e37b21677fe88709142243f5ffb2205e703366de53fbdbe3d7ded093e834").into(), + hex!("a80b1f358d284d3d8b18ef9f101d4f0d84c2ff99342e7150a55bb2f54ee231e333ffa930487a86e97f460696348e897b").into(), + hex!("a4a1f79af8ca4a5c5b44b05828449002c92c313c8bdc33465c099ce8f74c3d575ffcf0ac1ec5d29e80ff96b07f08636d").into(), + hex!("8797e455d44ad2721ce7de2fb8125af1bc4c0757d9c2fae25394e44b8952dc5fa597e0cf5d2b1c2ce996e380597a1db6").into(), + hex!("acd840fe9ed7f38ceb48d65c9f9f02fba4df0fd871efb58b35547c9b526e6e2416195d2c131a04408df7298db50a76aa").into(), + hex!("a9ae67c2d2bfe04d64bd4def66509c108f9ce85394da48d97407535c1aec05df39e3f7c66203f72bc65fa72c18bfa77d").into(), + hex!("abb785c66a7aa06200bec1960c572d61a9cf2c283404601259ba720a506b3831391e998346ee73392a3f7f12915b6f6b").into(), + hex!("8a453eb657c2a85bf93193d47ec26102c4d3adb666a7e1f05f1991782319bbfe104cea57ae1f4379cd115ff711be67fc").into(), + hex!("96c7151e34ed488a06946059722dd9d1b5a2ad2fec96b545ed662a3d0fc23bce6973d93dd2932128d95b0686f9208fc5").into(), + hex!("b5f39ed29d3f85e56e21ec2bce47b04ba16d72a9fad492815b485a93065f2a83dd46f92d74274f815c84792278a67cf0").into(), + hex!("ac9449a216a875c2f288e34443a94a521f8e9de28f70b729f393a483359ffd3ef8537b8a798b8c9a259ca390f9fa9751").into(), + hex!("b15584e841de0e25a301bc3378e89baee55989d9610c513b79748e7c51c484f7bb1d9adb33f3b63d52d36918514aed2a").into(), + hex!("b4dcbabb43cf694b024d36734baf824830304257d959f6300ce17f892a23000e036c4d3d59d7d1198bf4f6ad5ff07e57").into(), + hex!("b8a1f0a8ae246442517606f34ca4029deb727cab005c9952ee9858dd99497ba8a0e3311bd43aeee35275db74c7bbc52d").into(), + hex!("b1c443db1b5a00a87a399880ccbff4481f5742423c47d38b175527e84b32fd66110791c117fdb70782d75c476683f9fd").into(), + hex!("8e018c4b2b4cf1f1a417d00b13fc51ccebcd09a502bb14795b8274585d2e30d71c2c7a9b9f56a717f0676e685e65e907").into(), + hex!("871ea4444c7080995472fc8bc08f9091f9f706e9cfc49eeea5357867badd837649f059163835a7ad7263cf03fd13b198").into(), + hex!("88b67b5819119372e0fd7f97ba1eef877cc32d4be465001c35096adfa18e1811bec1620849a608de8420126fee9c37e5").into(), + hex!("9060dc7f55fdfc237799a2814a6bfe2d2f539ab76c38a9b1206890323bb4eb7b1ce011ea4fa552b412bbf6c67a95f025").into(), + hex!("8bd156a3a54bffe373fea65ecb2ffb12c96f04e07eee582200a0ade24d543bd6523ae5eb8a710c1de1912b2b4712fa0e").into(), + hex!("a8c3fb552f1a8c6cc2714b97d0cb8b2b6028bc3aa4571a7e3e33f46eb4c150771556c7884d575ce8fb7b62a5770ed2aa").into(), + hex!("a808f5a34beb7d62d23405a64d27ee5d7bf83cd880caf7bd4a615b84f22e1dbf11eab129d9cc9ad90d4e1dcd68613f0a").into(), + hex!("ae56febedf59fe99e79e87d7fe7aea5989493833a52f2e6012fd3400c69a6dde951fba50e0c280779d530d74452d63f3").into(), + hex!("8ed5f6de4a3ba85c6c857068bad6432e96c6054ea38ef07391b914c052c2262856d19403a590e8df63c6dec99da35b68").into(), + hex!("89c01fd1f37d826b9ef3b73e2b1aa5f4b4f86a263b2822cff0153fd2b945bbcf16eb3868ce66910073bf86b222becfc1").into(), + hex!("97b3ef6e0bfd3c399ec959d22d29fa9a79fe8746eec49e1675afbf7a955d02db2e89190ebf43118b65a7dc2db0c4d72d").into(), + hex!("a95700745f0ecbb1e794f4db9788af60df4772b5ffc8f5f693f213ce6230810df31716382dccd5a832ced7f34945d144").into(), + hex!("87d0a6a8cdc36fbd788bf744a443b632369fa0cd983d2b60e20856533ed6451d8476b9b3cff39ed0f75de94ad5c7aa48").into(), + hex!("a14c6f6463aadc8d1d2985b601bae8e74de54954ee7e3aa918837064a98efa5ab736628446582ccd13bd458ec2d50b1b").into(), + hex!("8b68ec274484c910f1a73a8cf8a8a149ff2942ac9de6e73641619fdd9e778e9c0fe6198745f049fa9fed9e56287da0b6").into(), + hex!("b77e981a03493852e7ebd6efecaa647c69ce6d46b2190bb2d08f0eede4addda776fda92e9d943ba57331bc985cc8e112").into(), + hex!("9870ea49ed03991dc1a4f47fc978618d549b4f0ddea01d91e7c409db775c91cb2a58c0c0c57eb73e7b6d3418f850b0e6").into(), + hex!("a518fea50400ae263ab9cea0180079d0d353bdb7cd440cb4d2156b9628e487b704630d931bcab742e0f3d7230821ae91").into(), + hex!("861ba1b761d0ce92972f28b7a65cbf6026bdf7427774fe78ff1f45c67f9083fe94fd2c42f47082b6fba722abb648c61c").into(), + hex!("83d257a40e8418407c80851e2f14d0bc47c3b9ce9e2de53b5c6cd99f31dd25dc200fa90c822060d47c4225d61560706e").into(), + hex!("881f7f674959e4176731ecaf6e2c9b490e70c07abceef15707dba8c9aa3cfa2293a96bc9d5455f769642c9717a4fe949").into(), + hex!("835116735f8e21064c497fb0dcfc929004ae5eea1f3e6863ad0b227c820d36255230090812da0800e03af9fde4354a13").into(), + hex!("98d9c12ede55af8067af5b62b89002c66e3b6556ee201ecbaf585fe5026f997fda75105068d62fe5d2403c6c64c314d8").into(), + hex!("ab359e8e0ef4cbaa9830e2aab892db7cd7ddaaea54cf455c2ca24f10ca337f989641ea33fdb1772ed90a988083405cb6").into(), + hex!("ae9ded9c9fc4e812dcab3d8a1c74ea264eab2df0715c7107ec1ec336c0bb5f3761ac9580ca18109278be5cff837f754e").into(), + hex!("b373013674404122f39dd6fc29abef1b2634e2bf650b42c15d5a2f7d762eed98166be26372e8dc6bddeaff84cc2aaf4b").into(), + hex!("b34adf4c3acadd7e11a9d61f7df20cb2520cdbf2d16c217f39e3afbdf2180abe59d37115910b77a504b81a6000b982c4").into(), + hex!("8161d446296d39d0c27a3db1bcbf0619e0c49739c655af49f49ea8403374afa4f98aaf530413848b7d4b53eabc16864a").into(), + hex!("8256cd8e3c9354ffbb59818f0b24db969a7765d64c2fcedf591ce65f619237d6eabd110293bff42b388c9965ff6d51a5").into(), + hex!("a1c562787d2ba1cb64dba278080fccb1c6538ccb00b94db34b62ed1cd863792f8acc4df78d181badc38dd9bda544e395").into(), + hex!("94ad66b4066f53ff299dc4bde2bdc23a891959903174e8ec08dd79f163c6f4661b3eb3458a786bd8f3fa153c806e793a").into(), + hex!("ad1a50bcfaa5641422c6f10d31316035eaf061ad1fc0a36c8835e078d3fa6efbe6dde4bdb28158d9b7aa74fd9241523d").into(), + hex!("a5952780f78fd6afd9c31226a23d307be72aefe0bd99c32a139c3909b1ff1769e2441dd2c03f33cf98df25c76178e492").into(), + hex!("954100f83b800dfb89721ac06728c3d5e8a8edb7e1b56513a63c2b49dd44b9930edd897fecd262984301cb6df23a338d").into(), + hex!("85de42b8de3cc88181a50e9aae696d92e66cabcc7b86425f846dfac138d26eda7cbc420cfd10f5d2681b63bcf411afcc").into(), + hex!("abeda3b142d621f94f829ed4174d042461e95d978be206fd31f8661263bb7a87c648aeff8bf640ec173a77ab0970a93c").into(), + hex!("af99434f06a13d9c5ee7195ce58beea07940949b686b4ce06727bbbdfa1621c608c891227e2f026bcb58c60a6e925533").into(), + hex!("96e97ce1ed97b8afbbf282bfbdbdb4f863a6931cc781e9d7938617310ded35dcf043c2320507e91e94f470f0cbb98621").into(), + hex!("8693fe1860981505e4540b79ee7a7ef33b26535cde6e9aa019bd1e0d26f359a2d26f0341b7c1634eff1f5859ed3a8625").into(), + hex!("a94e940bc2d8f826c23bcce8fc4e49e29c5f918180f566a67395d33cd573e6dfb149490de1ae75068feaedfe6cce0e40").into(), + hex!("9697d6be370d808a49563e062c2b3a0b347281e00839dc3dc0ed888c623f346a42094fbe2489d0487049f2fe47887cb5").into(), + hex!("b06d4cc8e83acc4121bd278784061f6fd391b3ac378fc6ef46ca2158207f5c0d33a51d3dcc4a499aa48e5b27539c4a16").into(), + hex!("972aa57628ce57381aef9710ebb7cb7a8db28b1b64d7db8be38936479f39772c60d766b0c5dcb79676e9330d0406761e").into(), + hex!("ad2164467404544aabb70605a56e9b0f7887491a1691302b2bedf271b50cb6f1bea1b9637214aa3624f5d8f854359607").into(), + hex!("a9da2595107e9db07c56a59d2af529b036c50033ff43c282e2da551bed8faa96eca744881e600b6406569675643046cf").into(), + hex!("8680660eb867978df2474b25e225fd7536b88e9e73f0188c0dbe835677de701fd402916d8e3d17fe652c7ac6d2fa0330").into(), + hex!("b721e239f50bbc7af5578b75c8befa439474bc4e6ea8d35d1006ed54c6d81c718fa675901df591a69b4cc30899974362").into(), + hex!("a8af30765f1b00ad51a32d856a2b2f97831843878a1668a43e66b65b8d0bd4a2e2826fef5ca5bf140050dd81eaa6174d").into(), + hex!("82baad156e89d7b3da9ded4516603ec9aec36e3a0a9bdd0ded604e4fcc0ba10179f9517fc8372d3743cce5e676c8cd17").into(), + hex!("b3392745016dfafca36a7af4be273c5fb4170b71938d6b93691d7a3bc8791bda537ef001d19ffcf7b393a89c898b8b14").into(), + hex!("92f83c901eccb742618313ee2f5ca571406bbfb1d077ebffb92d52ca962403d34a24f9d333c3a155bb9a72a0fc2eda34").into(), + hex!("9792598e2f303896010e35bf670dc2f3799cbe6f0c66379030b0ea01b44ebf24b9257841ab80d2d6a401fc56bb722e68").into(), + hex!("ad1a3c9c5d699ebe4f1b2727dc94b290c84f44c9ffb38d5498b14fcfc5914f4ef4d1d57853e036ca11e42396808556cb").into(), + hex!("90729c7ec4250613062a4ffbcba5829743ba7fd03a4e3407c2ae00b4513c21f3ebd68d10759ac4dfda5544e77b2ac306").into(), + hex!("840add692580be7aea866045826baa4a07804f8e3f56593a2af6fe317046d7d0fae181632f4009ae0d64dfacd4600c4a").into(), + hex!("b1be58941fe077aa8721b020f34d3ad94d1d5083244c276b7f3e6f4c918517f8c5c8d5c1376178bf27cb35ed76699e6f").into(), + hex!("97ba3e3be55d17113fb63abdc808d89fe205d75fc1ac808ebb78ea1b7570f7a014fae099cbc4b2a4b2ec884977405f80").into(), + hex!("a2c57bf9373db5382465ce924bf7dc4e62f406c187a39ab456a7387ed9231ce059d197f8701af9ce2d6cc772367ecfde").into(), + hex!("a62ba8b81b9f8d40fad7fa1d7e8e49ee547f170889b5d6a2be9e2ad2ab0b265b4197fbbfd3b17803a6e727d41cba83a1").into(), + hex!("a94559a51d438b194fea96975a4571a118105479fbb7a37abc7d676fc8b8d2ca30c66b25b7727dcd297384773ddca074").into(), + hex!("957f9be0f15d8eecf621eb0978267c3fc85607f31c501179d9c83864ed9a9e5d526aae278af3767632bf56a20eca62d1").into(), + hex!("b89928c19d1101486d4299c493db5bb72f56f8bc24b71bb54eedf84284452250427b179dddd7fcfc0f521fbba09d0c5e").into(), + hex!("97dda9cbf61015b296307e510295be258045a1de9de52117f0aa28de48e27ffd24ff711f9187936293babe89da226fa1").into(), + hex!("a66b4064c5b1ee95a35a93209c89206b352e0666abd1b5c95eed3c382210334fedad7d531b9939cdb2fc649c4369236c").into(), + hex!("89d85e413030dc45194b2676a1f2a76801920535ccd909277af1ac87fd9b0d16d94d8c62a421c9d95e7a053cc4c3e0bb").into(), + hex!("b8f0350e1ff988bf23846f2d64e40b35a370d1c5d1f9dca4021508205611f788fe5485e966ff2bb6fe8acc1540a2e751").into(), + hex!("aef9ed7229aaadb1ac4344e5b4d0eaf9b89b20d50d8ae8dae24294ec3c0f2f68dee3186dce35d46020d8d1b2626a29e3").into(), + hex!("b9362f34ead1b0cef1fa9b35b76b644a323ff71c48f375c27e22c6878cdf778b1a3125445cc8cffb6c8b9a3ed046c3d5").into(), + hex!("b69c578e2223f3727b7b5e99f3926eb7424869b356541feecda54a0882e4a009b182c358052d788c2a7e776768ce2b7f").into(), + hex!("a9ab536cca003598d88f76cd0d666b1738802c7452201f5d99ba4fd82b6d7da3c9ad8c4707445d6cb4b2c43de7ba06b8").into(), + hex!("a02648e465634db73fd49bbcdb23cb6feed9688eaf4c5678799734d49f2d4cec9cdddf598114896de961ebdc07436884").into(), + hex!("8253283df9690e171af958f2a3cc37e0b2cd67768f2362bd604ba5c5db8d3500c0d7d6cdee982165eaece63bd016f2c5").into(), + hex!("a06d8256d22ef3891a751716449f97d374e27bedbb9dbd0f1c9528307787e4d9ab9450002bead2922c31fd4ccab9abc9").into(), + hex!("a3bcf7d7c1f1c5a2bdde1e0f4cd3cc6dfa061156534aacd6318d8192c27218b6e34e734a118a282b0d5fdf639e704c21").into(), + hex!("b5577e876cb3de8196b485ae828362419c2f7f8ca9c8f38b1254059873fbe53b57110be543c864bbcf8b485f63925169").into(), + hex!("8410e1b1ab99cf1868fa4494dc75129f42a5e633448e64321cb379175cf6eb704ad6863e3a6475f9cd3cd3f1fcd4b49e").into(), + hex!("b6b21b346d709d4897da4c535556d599486878f5c574bca2823ff9d382fea2f45e8d03aa0fc5a5d623098f1b67c77a60").into(), + hex!("92951386b734171accd57b66afad7ddd0ffdebbd9da835e273b11f96639aefa6259a1387fe3947f9f7eacdcfcbb54b65").into(), + hex!("837cca28b3bb00034d619a3b667b06b66d7cd351ee2c161014b4c33692c705e0fca1352c1e5a7fe8ee00515f4b9c9658").into(), + hex!("95589319552ee815c1e1b053cc4452f9ba600142c37bd700feb3c27468c769e45e6307d7c0a3f366440cdb4d3b997d1c").into(), + hex!("87ed23d8d5fd6aa0922565367ab405e666996e7b918795da299c204cb6d1e51a9c6ff1760dc3fde555b0900e677a9b9a").into(), + hex!("88564f50eb215320ad93b979c617cafcda9122ea02f113029702df1f39116e00b35e0beb0c70bb7d7f2d9b4bf68a1419").into(), + hex!("adce6ad54e5d24a2da5a04751151034245184bd7e61998ddfed673fddbe4e3d069b580e833b3bd4509c30a2e6a81f528").into(), + hex!("80800ca4fa6d0f3ea555ac7d37e54e7776f640f76fe89cd7f172c74723cdb3324da01314c6f66c4fc404c393aa8c7841").into(), + hex!("a16f473cbc9070881b9dd63be9f99670ca571822a67520cba885b2636137731b440561f83e199713ecdd51d4dd542997").into(), + hex!("a08c0625cbaaaba847a63ab4a96206bbcc7bfeb659505d0b6b0e58d22f00aadec41f8cd62ceb116258b78063f26796ea").into(), + hex!("a358200c83ff15fe3fef7a1610bfacdf86d55452258e7f4701082f993c8bd5d234b4d86f96d444e96c01367503663886").into(), + hex!("97278525e5e590ab6cc4fa4a1bac4ed7164a65887b29e658bdb2146008b02907488565dcea09e761f6922118d9933ed3").into(), + hex!("b4e76e4cf2c711a7f3be5b404f076b9d2272e15ea7c61fa4b7a14c5608c352a92fe6674a25dc493a6bc9e864ff4b4a85").into(), + hex!("a4612d268cffd31d092892268e6b4f9f564f0036bfee4f200d270a61a8e5e239996d288d28d6f281d8446a88da56cb55").into(), + hex!("81a70c88fd7d99165b41ee12883ae529458758650fd13001c86f8fc6ce5f8f9b69ce3a133e310619692faa1580dd5d67").into(), + hex!("a87124cbaafc8ee5418639ff27795003a43ac18d07a60fb8a1c155c5fece0f8b25525166ba697e07a5f2d47af6cf0bec").into(), + hex!("90807f573a322aac9d1cd546100e49cb8c771f3d32c89da1890c1c819d90b1dc668c2374249a093c0863d5988c358d4d").into(), + hex!("90cb26b40d10f193da22975b0507f04de6cd9d002e33226852cb40d948d1814009a39332c75f1067e8192b0c9230ce63").into(), + hex!("aefbe25ad8bef226d434b970bda8a47bce8269ad69cf91e02e04208a74055ea79a3a7dbc988981b79bcb9298af467e62").into(), + hex!("b4932ab425f4abad270b32b6f66066cd01fd6f18cf8c84dde99d21c9c2676d58be4f9ea5ebbc454c9d9f921c0333cff5").into(), + hex!("ad19368c70cd241b1a90e5b46f34c44351d298e2fc9ee5906596b20f5aa9e592e878afe8924aea112be60c07648fef8c").into(), + hex!("8b2d2953b4603b73bfb1edda8313cf07f6d9d16b0272d90bc46816677b602b4a7e6fcb36c4f69335918f1ba4ec95dec3").into(), + hex!("ac64d362f730c790ee3f9311990d9ffa3b99d4952ea74f63d141360d2edb7fd0b70d94e5865504af3caa94b63e34ff4f").into(), + hex!("b71ad17d1cdf6be4b7f0bf7d3fca689d5a69a7426dbedfc137bb162142d26e079f49c99797316e2a577225d881e31a04").into(), + hex!("8dd692a01ecd819981ea31f39a5950bac2af0deafb35323358736bc59f8fc69f58c865ed9cfe239ee34d6f80bceb562b").into(), + hex!("b2ae1f2d871d48b6157aa9d74c24e3fa3f09d6c40f421de9c545de5ecdc44d44d6c4a7caff315694d8173974b92c6119").into(), + hex!("8c934a58d5d2c06221c10c14f08f17265e918c6f7f158f6989acca4b1bdf3db58952e5500f930af02ac3e6e44133669e").into(), + hex!("9466e7c328d12ad5439ac01c994825a94665091aa00e212a75c4ffd39f4473a62c160d63e568b534dd7d5577ad266544").into(), + hex!("8e35a84ca6f167c75f98728000b5df8d9c5611080d2dda8c49f8d4afd2196da349cda481fdad8a0e7dad1cebf4b82446").into(), + hex!("8ec436a9690744a1c6a31fa796bfc8f054ea5efb1c8d6a70e9094dbdc32bc199a7c1b29216d706616029525883a9e342").into(), + hex!("87cadc50459a643648f5995ff7ac751c24454040f788e218c4894854ac658fc64c2ea0a8cae4973056ea11f7bb0e5a26").into(), + hex!("aa5a2d8278dffefc43d598186f1119bf1a6d2343143f4874aee24d3869fd4b58401e8bb220f2a228416c89c1f5344af4").into(), + hex!("a36f48c232cba1daae013418421fac6278bca09ee3816eb46cc4059254f1e7672dcecd6eebc9bc0896e96cfa0d8b485b").into(), + hex!("937f8e28abdb3859575cff574517975b96ad41dddd4efb23af86429b01ecbfea553be6cce336d170116752118368e05b").into(), + hex!("aaeb4e5c7f67ee23b1976b2e86f2897ced033e79012532599d130cdbe29d8cd551d9451794741d2ede8564af9070f07c").into(), + hex!("884d9608b7556dbddd0ed13bbe04a5bd9f2bda0bd090d47550f7362bd769a3b3dfa890191c64e44378792d97ee4df5f3").into(), + hex!("a8276ce24aabe42cab32ba7d77c2ef2ca84b3a3e3d750f8d0385f9027f0e009365c78196638dadde186bf44b780fff53").into(), + hex!("9148c5c797b4a6438360072c463008df8b17978335f36d4972b4f826861e8a175265a9eb00c56f47d3892783dcbd080d").into(), + hex!("a7e5100f51c611f010b2601d340ad7aa65bab89c8aeecb181e76185f3a739892f8b172e5cd2c108d5aed8f5cc91cb6e7").into(), + hex!("87c46e67d6643c07e0e318dcb22032753c624e646e4871be4098005100b306f3cfb25b6d81c718e83b42b92841c577d3").into(), + hex!("8bf429c735133cb05edd9b8943b5d4040e83191553452e69b3a1fd06edf2a9eefe8e280cdda811795e1da33e41e58345").into(), + hex!("800ce4d6c257a365e5d8e1bcde67a38c04ff723e38b0926af8b9fc352545317beb40621497aaac8d0e5da291c0208630").into(), + hex!("a5c795afa0ce78cbc11de13c8f58d0bd9ca5b8665d5d8d28513a0d8666f9336b0dc3295557801ba253983fc99f45ce3c").into(), + hex!("99e3e4a8e16ed2ea44aae56b537ca9b159d57e987b0511b6d34767744b04700ff287d431dcc6c67d7cba5748b3580899").into(), + hex!("ac88e78183973e9730ff0c88dd62eb23e2794067ac2574a1c8deed85a4aa6229ee620668dd16084c8f168b2555f04cb9").into(), + hex!("a9c4fa68984527577c6da60dfff110163f35ab7393b852c053d73485155fa1536d9701f660a08b160bf49a647ce3cf92").into(), + hex!("902c3138ff660230158ff69c33911cbe958d29178a54cbd13480addd948b19b0b97f6b235df2beeadb7f7e1803b8cbad").into(), + hex!("a6169eabbed09e08a0d290f9075c62ca4b14e1f7adc53545abae49858b3d5d7fd6cf8d8ba2ee1bf13f36f79ee4092935").into(), + hex!("85a0d7387b1db5635f17899bbadddc0fe6b11976385e12ea4272a8f61d81004406604c8f04f10ce928fb6b547d3bc654").into(), + hex!("a39e5f2112944a7ee31fdccaca927e4f4bbefed1274a134e8a038307820c1d14a6260e25ca5a3af0589f8faa8f516c5c").into(), + hex!("b5c86d0482a417bc94b42f8478de06918b36c5f45d0695275da2a3e773088268cc127ac1380f912307b6455dd0b16d07").into(), + hex!("b8b5e32afc4cf0fb92251b422ef9b757130455150c49ce51f6d6d95d895dafa25649363660608bbe7507d787db9d643c").into(), + hex!("b11f3856f691d84d49fdbeb4f33c45c37dd401a2bb71bbf946f3ddc53a57ce5c4da583f76cf4467d43034a61e1dc88fe").into(), + hex!("a56d1de276b2d0a4482e17cd358455aa19a47bd25c7fc97af8457ebc37781358cbf8a7eec9427881550a8db1bbf51771").into(), + hex!("9508c85488f15d2772522dd1e991f41da1f3af7e3527e098d5408ea96f11f4150415ac68ba0a3031e95528664b261de3").into(), + hex!("a28e129c656bc44d0b6892103b7d7c0d15ef1f1ce583e90e2c644c3016ab348ae26a662652c953a3c447272e52b007a5").into(), + hex!("a68dcb67d0cb585cf22f753602c46c7ccffff246b106db9c56248ecc5e94a036009bde23864879af62a4ede81d040c56").into(), + hex!("b9725383c63b2a522f4d976ad6be14a35b9e80145e058baa622238500f1a2ffd6869cde87fdb984654b2e57615bde3ee").into(), + hex!("90248e4cf47ec8b00ee874ac98227759a3f7ce4819e44176dd9b1acaa6270d144d3d707a35e0cbdd7ae23b15537a20e2").into(), + hex!("81327abb95401fc2fe0b1c2d27d7d9972811a63e12be0173fe8311678ff1fb097d73fa32cb85f13935e1a4b7fc59113a").into(), + hex!("a31bd2dfc9ddfa9ac748968b532a41a26007b23cda258fdacb3b0abb751cd7ce2eccecab2d5e3781fedfe0d8da027481").into(), + hex!("809b28c11c1abbf53f2dc005d30403937d9826960b24f4de857a9470067add08d49345586d7e58bb1107d232b3b47bbf").into(), + hex!("b0597958b75e64fe5c6e56ef803284b3b7420fd537e5625c75af3aef814a87a5ff01951261c2c7b27e374466658711d8").into(), + hex!("ad8fdb91216db4fa779774324162fd5bd7ff9a999030c42d9f90248bc328221aae315ef8617c9ba623091adaa0556074").into(), + hex!("8668991c8bffca4cbfc06f3429961c595d85803a262105907361758d677920796be70d2cf820f0b1caaef708d924e676").into(), + hex!("ab22ff4c2ec9e683d2d1ecf57f7af9b3aab1cd289e22b1b66f37dc3779e83e35211f7d4919dc3ef0babf876d491e0bff").into(), + hex!("b8696c811af5d3360951d7bcc9ba4e82e17a125501e91ff74b915de28a8cc217c6fb05d90985fea7ee431e519e494d9e").into(), + hex!("845c0bc4769c428fb30e63c8e4631f22e69f934b0e6089431ddda2c232172a4980cdb6f650563992667a0790f1a3870d").into(), + hex!("b4113cfe79b6b198b517ab5d14900a7189ba78b7ef85d04551e18fe1ce6a69564377fb86a0e11627cb794d1f416fbeb1").into(), + hex!("aa6848837b26df24b04198b0b09b77d7b59d26dd3b20f7843352f2436c046e9975af2985e64fbd6267d897e728a9e721").into(), + hex!("861456839cb76a9490dbe055559e3dfe3bcdc41646aa656d8aaabeb4c0e39a1b370a4f0107b78f900614e3fa09b46bb5").into(), + hex!("ad6a8ca9a21b7874f8b115024a7f079f5a1dac3b165267b33a59d1c8de2065bce4552369c930e9a949d0b07110a71452").into(), + hex!("aa83179de1682892257c57774844b040299789430a262c8b44dcbd81f8062deaa731ff4bebab1a815d3148ec719f4cb0").into(), + hex!("895549691861582abd102fc19a4ed269b335010aeef45ae9ae6b0d9c6d26f26c31371086ddeda626e76f7f07dc622fc6").into(), + hex!("a94e590989e81d269b5246f22a9c97b604af58352c70100f8a20454fecf36d19e601dc1201342841ab231dcefc461f2a").into(), + hex!("82541b5b7a392456d1936373396012b086c370e6dde41f6d4409d35373d1586d3c7119f6f0d1e38bce9cae67c97fcdd8").into(), + hex!("8b89fecfe83adce613e75a52e785ffc90847c09ed779ebef4d29048bbac04b58e27311461c25d4e68cc0e6778228b037").into(), + hex!("b3eeb09bc9ace8a62b71747711bbfa308b746c86ca87297cf2a8e768765a86ed1add2e1acd2925cf05537dffd4dbec50").into(), + hex!("a5364dc8221a37d73250f8498d9b6e163babfcb01e1fdef8ae570538e128b562a0ed8b353235159c3781ada8506ada33").into(), + hex!("ad0d54f0f67d0231ca0ab54ae881386b055c169fd7415c12e511e5ddc5d4b1beba0e1a157211996c7ab61f6e94cacd64").into(), + hex!("b4b978f7f9b084c089923e73a593a24d5aa22600c879eb03645a19ffe8b36cb8ae040378a378ebcb4a5b73331a2064e5").into(), + hex!("b3d79038de0c62c667ab4213524679934b33de22111626c258bf8fd8e16134425549dc7e3dee15239c32bcf122f5bbcc").into(), + hex!("af7a243eb665d9b2c37033363b252c42bc2c202c266ee125b5676b6f9f94ee5d46d3e2ca217f107719051de511625dfd").into(), + hex!("9726a0d607674fbde3fa5c346806c4083e092921c303ec86bf5a16e4b760d031a24585ab407b9b1b0692d12276912961").into(), + hex!("85a8eb7ff82937e2ccddf2f049af9d871c653de4d71ab36b198bbc7bfef2e32eb3f22462dd01affbce13813332193262").into(), + hex!("a8889fa7291017a3363a5e14cee8cb24c273be4aeb74c4b0e1e375f70060dbc9ba296b291e154eaa56703b8e3f7e85a3").into(), + hex!("a789cbc52c5468bf404fc1b19651ef6a805d96ab8a8991407e149a68d10d1f67d1d4b380c08f8be1faea8d1b32bb8c1a").into(), + hex!("9535392a86b73ac66ff8ac1ee0549d266a9e25e1db542b077d136d26710282529f34c43dd94ee77aa97c647e0e05356a").into(), + hex!("a1ae9d5fd0ca16021e0a33fa116eb5b94991aae02efc6f116e073def47253fe2d1f2438275f09f204cec0610ad523ff3").into(), + hex!("b5c6c5b37943e99bed4e63c9215bea95fc365a576a9f8f0b9da8d5ceb5f9da881a273f5692b0913a3bb922772923c07e").into(), + hex!("b3b0144fb027e0c7f1a0c4c703cc5e1c09422be38de4e10010c28bfac358a6c834df6794e5007ecc4c8d866ffb9a8725").into(), + hex!("aaea409068fd2cb94a7c6fe031ed47c7c5b366a33905d12a107799aea57a052b9bfbb1c4f88b1b3775d5bef7d6204b73").into(), + hex!("a10cea5af8405d807b66ad492a1aab8618324ec3d9d01181ce29512e38f03bfaf556251dc490b3d1e80576bbecb27dbb").into(), + hex!("b700d4046b0be98b3cdfb8a2eeee52df68bcc1c5550d1c17205664b0d896028bddaf5dd38482645e76f643ad9d2ea9ae").into(), + hex!("ac9163d5f57a2d1def901e74c1b07f4d14b1e9a5c362d3b082c45389ae8add929a1dfb0baf14f64790f86cddbdcaa32d").into(), + hex!("8e3b79b19d49d77844e3401c01984af7211268bdf6609919c9867a83cbbea21870ff108143795a85fbdb2c15a2d127f1").into(), + hex!("b9db73eeeafbeee4edc52c1fcac7de6d8acd22a8d3d1c4cf760a81dfb6cc91ee454385044301fb2253588f23f3a24079").into(), + hex!("b268631698668059d8eb44d50d532c9d8c49953ea2029d6a2d0eaf69713afc85c42d989cdd3bc0a479ad77cbad24fc0a").into(), + hex!("95f74950a24d82ba9a9df5d839d17d7ff830a5dff38663630efad5abe9c58724802d5bef891ca2b3b81923b55a94c6f4").into(), + hex!("969297c612c35347019f5bc80d2887a3c95c8ffbb011f5cefac63a1f51e48dc84d961aed56afc353791589a45c871cfe").into(), + hex!("98743e9521e5fb6a643c086a00423fea51b8ad2e55bbeadf791ede16eae64f9fa45c41101c6cbe4a8e96c692fc57c030").into(), + hex!("9374001ac3a8673e337b078da1b72090bf2450a5f53f6a600f4cd43ea4b5fe86a73d14bd0103b110f23e417dcb4c2e47").into(), + hex!("a91bd42a87f28a6fed7fac68b5306e5382a93fde2bc9aa5c48b747ab774f9c557343cafb46dcd4e93df5aa95ed832410").into(), + hex!("8fcdec0a825737ee2c61401014287079e729a8b8e49337e99b34c25dd9da570a1fefc532a0cf6ce1bec80be2d9ff46e8").into(), + hex!("8197cd84016cb41e4287d29b3a0fe8d221868e5993aef8c15c1578e038f9c43e93bc26dfc67fbef919322178223d0b9e").into(), + hex!("a4607e2b6ff802a4e497c53b206972d35520c78f14f7d4d78514333e90b2a8852603bf223f1c9eee3793008f87cd8fc3").into(), + hex!("a72b6185e451b3be2c140fb3f48225927e7c052805682e3de9b2c826997419084bf1e2034aef0c5d364b0004b3b7807c").into(), + hex!("aa99a2cd46884d2ecae4257c1db8fe1ef6b0cc1a0c25dcefb53540ae91ea7bc8955b8acfc6d96ef47fc3a5733f2f28e0").into(), + hex!("878ea42dbda59fb6f839f0b65eec295f2d543541f4fd576a60d104b94b49b1a1ac6e9a15ed3274e6305de3f35ce1e3a1").into(), + hex!("ab2f77f6036200b4ffd3192b8d06dfdae4eaed4e1105b27e64ae2e120c909095e59f4dbbc44e818afdffc7f9ea1f42be").into(), + hex!("a37e3fd9b1337734cdaf34111762403db11b1aa0324937a17e053242a9099b3db0d396b485ca996f91117c64623915bd").into(), + hex!("97c65c28a6a81690d4d6ff17d5cd3be0e15ab9cafe66e6f7b8da66ead8beec561c3abbc79af52a8986d576363f14cd27").into(), + hex!("ae138b3020373ca238d5dd780862fa28a2c3e05903366cdc0fd7a142db3d18eda63b8d049abed37f1fcb25f6cdedbd67").into(), + hex!("a800bf90b4e7aa7b5b00fbe6b5f45067e0d7ef2b1ed9a626211e07b66b12ddaf90ed05d369f8da13ecfd8cb499f192a1").into(), + hex!("96565fc4ef721f754b6f53db97d32ef5e5c3cc0f55928eba3eb341f4962b815381178763ef05c21c1247124d592b4449").into(), + hex!("b4896f9e2f88c990fab764a4e006f3f39ba6bdd0e1c75fc8dab2973544e052ce63f8c61a6ab213c85f6799d988a6fd61").into(), + hex!("87aa22b60a13edcede78e629d54714436a8d7d1e6e232d4df6047213b4a91e61e5feb38216e0ebb209f1dd8d7e4f5f9d").into(), + hex!("ac4d5e8bb39a2564f3bff053e2058f261209cf14e65f7dc540070d40dad7ec4f5fa81efe6274aeab8691b85a774307c5").into(), + hex!("8b13673c306988222f09ad896a75a6232ef3bfd2f6c37c2d751668466d45511542fe982ca5720c6518891830674e2cfd").into(), + hex!("afeaafa07eaf14f248d2a34e4f86064c5bccd92d3a6c0ace1ae827dd59111e9b3cf2722a270234eb5aa633c12e140354").into(), + hex!("aa31119422a52a7a5ba90f4e0b5676434b4f05289ea3143e8a2162e32b1e19b582a586da796ad9876d0991274f3363a8").into(), + hex!("82c78ac9f2018540eea744c003a76cd7bc8984103e941c680a4a833a7c81defdd28165256890d534aaca2991dfd856b7").into(), + hex!("b4a99846af0ffe14d0f820a574d42571e0186cd078840ebdf0684c806c08eac1d6afb2f7f9f9dbfee19c2ea12af5307a").into(), + hex!("93cd10366714618a7e8d4edf3c93a9bd30a280f765cb93071a279eb5bc4fe8dcff8d91a1efe8fe697d1cb5e760a07fd0").into(), + hex!("b17dd2a8817471efd91b60ee31bb5f7c2848bb40251dafc0e2250cdeec3202cbfc7a8af6d7f5c3300a53a73bb4a11b54").into(), + hex!("b396d11ed53f287ecab591707ec5ecd0c5d34a67854783dbf263fe2614c707a2226231fd8dbd6bb1ca0760f06f2fe7d9").into(), + hex!("9982a0fbdde6ad91f35e64de34183e4e7f7df6cf422912f3dde0cd16394f0f172dc32c4f68f2a09647fed32894471fb6").into(), + hex!("ac5889086fbfd2f2570191b5d92659fd17283509477f442dae81a8491b8641a5f63e275659e6592ebd0e62a8c7a9bdb2").into(), + hex!("8fd6151866cc75461b69b4685ac4efb5a21c10d5b3291617bee4ff300d90fa2290319967faa2b7189c090d3b60994fbf").into(), + hex!("adb4459e4e6410606a74742d6e48f7b84f30ecc8e849b6af9b4b617236dedf1707ec388a97523f18ec7e047743fa7151").into(), + hex!("a9fd9329ea3b6fb77dc577e2c891eb66c61a575ab75a66dcd897f1127e8bd9ad8d3eb9059c7f6a8b08199913b83e5ba7").into(), + hex!("b94cfafbd3ef7673023ea37996084acb3109ffbccd210184aaf8ce8d29bf36390bdbbf2870b0970e66831d28e90b248b").into(), + hex!("922404dd76801113ba23df87ba689e5cb609c94930370576d0d16e99a489de0fb079fb273159b8b07fc58bfe4f787c70").into(), + hex!("8c6005451c02b18458c3f069a521aafb44fab40f4260a60da6b9bb5f920e91990a868aafa4b6b071a899d3bc51fac72e").into(), + hex!("b0cf68badbb39413649b3171281ebfbabbcda1123549a4c6c09dbd4dc0427b51d555056c79f38840c52cf920dfa2c8d5").into(), + hex!("a08a09c8dac1f7bbbff2b7ea96899b64fed53e971569171224e675399467daea6870e48fddcf47179d3c7eab4cfde3ad").into(), + hex!("881c89cfce577898ad367abc2cf5989c647bf8904be5e061e632256b3750b0aedaf4d30c17f994358aecd069b62cff09").into(), + hex!("81674253d6664d414f667b10f6e8edc32af0a67b2b99d3e4657991f8a8b1dbf260447871c1c680d92f99abdf3da2d035").into(), + hex!("92e34adb15141ea58b2b481f9dd69e2584f512531bab13779fe99e18d48b6ab039bb28d9f444d517298e11464ebde4da").into(), + hex!("81ee1554da84a0a487e52c57528b69cd79f1b6530418354095ab976207e368379ae2fe0a4a340d209f13ac9783cd6d5f").into(), + hex!("839c0316ca07242ab52b76f55049f2da3e83f021591b0bba295677d80d4b407f88b0d207f3ecbe7eb85f19eba5d152c2").into(), + hex!("88b0ad748a61db5eb96016d9bf16bb05ffc4cf5ef56569397e9395f454fc1f731b48dcd8b3368163c36a3fb41577286f").into(), + hex!("b94fac438903b1e6cd11135b8fd35b91b61a0354addf8ead5cae4fc853ebb5be68bfb9901d8b4d3bdf58224675b6d675").into(), + hex!("a015eb1a7e1b814625b13c1b1bca7f738e80be5972f3a3a27cd9b21b033f16e4b5934bab69e38a6edb8d03e84e8725ef").into(), + hex!("83ef4eb739353f7679b27d3679551b2a0eb1bd4d372def5c0e8e5922a9ce7138dae4b62d5c147e6439a551d3ebb1e1cd").into(), + hex!("95907a3b288f3d4199434590340293881b94322e82f7fe9c186da3fdad7881b9a92cdcc5fc29d4124b1d05886bc9ec2c").into(), + hex!("868078f74e35b72a894d72f93d45333e423b1aec6d7e3cd7550254ed6a156957d4c5919489a84986f6134be8334bdf4e").into(), + hex!("8f089ffa10d8ff27470b8f6fcff49a69bd06ef3f88faee54a6bc8ea0dca6bb799199bdcd9a9e7686c43302cbadb584a0").into(), + hex!("980f4614de867eed7571cb3100f9566542b90d2b4110806dbad64249944cf3e4e484d03543107fdff0e91634d5193530").into(), + hex!("abab8578ecc6096bf063da248b376bd9e76a8b9364000a98c85813ada835017ffe693f908aa789cf09ca3020f3bbb9b8").into(), + hex!("a2beaf5eb12232e44bd251aaef3e007989794ad1df7a5f41ce1e6d862ff0607db47cacd4b04d684dc22d3640f6b8aa16").into(), + hex!("8f57e3d8d68264b5a07c9fc2399db0dfdd079abac46e1023373c22377612d3b005053e0df490d2435110c9fc2791b8ec").into(), + hex!("af7d2c45de945cb09adeaa1898fe0b026b5a5c5de2ac21f3b2c298d82fe4ae253d859c47f71def164a77b542905bfc73").into(), + hex!("93b026c1b083d82b3bd52d0004985f374acc81f754f7dd4e563a9ca5b20780f7872925a03e48d2154c526723b6d3fa88").into(), + hex!("b8a11c00250f9e148086818aad4dda9d69480b378ef5ca9e2fe85dfbb709d2a919067d9abd2eafdb5202cd334081b5a2").into(), + hex!("a7026b1a57d9d64f2526bc42c771cacf43a718725c2d0dd889b1af12481e3112bb5357e6434b3e83754b067bd5d533f8").into(), + hex!("84771640879d9628fea0ae1106a45bb8a383ca0a9a110093355395968b4d5af9e4a34201024a187d3a31b78a839af6ea").into(), + hex!("b99e5eda7144057e439ae752c2d879345ebba19e83c35785743d6fc1646069b0258ebb1ef62fdd43493498ed08a8de15").into(), + hex!("b2a17e5253f695a61697469df96f3182a62faf0b5d10c150f833543e7b3f5979506a068ddaebf310fe86efaf3adf13e5").into(), + hex!("8ce51f0a8ffbcfdddead1a94f45a42ec4f9e8770c0ca33b58207b8db06f3004c05e88f5b31896e1b7d3c79ff571426b7").into(), + hex!("a775ae2c3c59968d0add4c446c5f20e92f92eebb704016ea45a47c5496945b7d0935b4d8ea315990e6f58ec33d00aa9c").into(), + hex!("93bac2d2038c87f111c8000e721e4637b04ff8c3b7b1e8a9f02cae40e465a1b2928aa226af73ed643a4c21f3fec436f2").into(), + hex!("ae050b3f4784f2c12c902fd1881f6bb940806c0a9d7cceeeafd194dcd49cfa48acb10a09c55ea47b508d5d16702800db").into(), + hex!("a8c6680208271473433781cafde59a4aca2496d58c85f48ebaf447a0c175e784c4a1351d94b5d1a64c78bcf8c84f8f2e").into(), + hex!("8f9f71c20844682e17bfc30d745ee2848bd8782b05572ea64f9b9bdb2c24b3d1953bae317d79e267982c42f3f3aa60f9").into(), + hex!("abc4ec57e1625bcf3c10754a07a2047d359e503b05e2524175b48d5844756ea21f626a65a08f4f7b32cfb0646cc68fc8").into(), + hex!("abaccbb39d4fc4c252997503604764731889f5bd66382c90477df2f2d413f86892f7efdbee0e3a908b3cd69321d3db78").into(), + hex!("906a87855cc2b3765774d3e8c7daa62630fa7ba761ee2a0bfc5594380dc1d9b62740d1190fb882ffaf17c0d869356261").into(), + hex!("afd0ed291ba237f8b626698e3c54b8a84341628a524b897e53aad5231aa01977990c55215a677e9adf4319a98e81bef2").into(), + hex!("89512dacd42d13f5618f1979d9afd93c06247de2e1e9ab6625b5bc9efe24439189a564ea220c4ce2795f059064f5f5d5").into(), + hex!("90238fe2ab6b623ecc990ef8d2849d26229db5f3c8587cf06501e459c7742d81653c7dee45da82f5946e041f129a8df3").into(), + hex!("a420b197f4863782fbd676bdf1808ff1c6f49a506d525952c575c10a32cf63afef22f977d4b7d6258334c498f36e7f95").into(), + hex!("b7d8fe926876d1d01529eb6e30a78db47b6693af3171b2014bd18180066609cca9036e35953ebad7355362fa671d903d").into(), + hex!("a4b7dc0ae7e7d8c8eb65081427f5d41a122bcad56456dbcf4a4e83e188af1e3d80dc7f450766b8e87ed8b28ca6c5c479").into(), + hex!("8d3e1060196b4e8e827052c2ed782aa554540f21690b67d39db46fcf088d10f67f2b6e0d98689e7497e8029bc8f355bc").into(), + hex!("861657c4e2a48891939b49706791a03f63d092643cb491adfefb3cfa7d86ecacccaf86e4a1382444e466a61e29a12bf4").into(), + hex!("92f1c5b122943341854ab7d5c4603e083019344e2252da5664c676ffdd564345d7e28e2a5692dcaadc4371a95ea2a142").into(), + hex!("86100ccc2ee10dedee28a3ffa0bbaccc1ecb19d36a99e6deef1f2373c1c1c3dcb8b8fbec5809304d883c5d60f617d875").into(), + hex!("97c06ca658fe3ec45a6573bde07b3a95d80cff213c9f4eb8933c8dbaf147262291a5ebd55b0e58a4686c4971cdc45671").into(), + hex!("b13e9055709868f723014736b1fde59c05899bbf2aa6d4591d724c35c15ebe6d215e37fcb1e7b8585abab0b2ba309c00").into(), + hex!("8ce0532b968d8d770eb611131978a253e7940ceb627b7364b3a4dd517f26c31bee51c134a5b1297362f6f9b6d714ef33").into(), + hex!("92edce89bea01fdb25da5d0f903de2fee626681cec3db418a9161286a2e95bbb90ef2ccd8dad434b51776f85d982700a").into(), + hex!("aefc7dea295547984ab42a64f2f59ba2ae8220712778a71530623351a44372ddd1018cd8d4951934ae0fa39653ad6aae").into(), + hex!("ab297f28266bd5ed104c1b55088f114592e80aa098a0865e5543e12c6392b0f94b5cd4e0b6f375d1a0d0809d80c1fca0").into(), + ], + aggregate_pubkey: hex!("81a5778df2e724c98b4ef79ff33b9c5fa3ea265de81d49de5c4ab3be2165d32fe15c59c982f758c3b9c522ca5e659fce").into(), + }, + current_sync_committee_branch: vec![ + hex!("1da42c54eb912009030c084b700d2e0031c0a0e5759b0cc593601b99764a725c").into(), + hex!("0c960bd59f4a61104153da676eb38ebae603e9cbb55b0f6677cc1df0d535d60e").into(), + hex!("1682c67e0936255e351f8be6ccbdf048db06a80749aa900bd4265af1c366bd52").into(), + hex!("d95bb6af7d6be07e5d7d27337ab9b54d5bf725ac37671b9483434d22d724bb92").into(), + hex!("3abb1af4e9c3acb052119a42c2d4222d99e8b5b958c520a03526a8177b921cf5").into(), + ], + validators_root: hex!("043db0d9a83813551ee2f33450d23797757d430911a9320530ad8a0eabc43efb").into(), + block_roots_root: hex!("ed6ca045637c1c7dd54fbef547b8b1aa3f5b9fa8f0bfa5df26142a0c4237e617").into(), + block_roots_branch: vec![ + hex!("620abd1a8757614facfd9d2fa43795281bccd4055bc9b12e5cb3742a16a9f9cb").into(), + hex!("14c793be544d5fe1993a1b25d39f4b69e832e914c3d470745276a25d982df4f5").into(), + hex!("46aea5f0a7d66cffbd55e676b915be97cbf3dc6281146cdf4952047214ff74bd").into(), + hex!("0ccefa47e43d03e26def9fa07bacd91a5a2a20c6c5dec2ea090f71f91ac99282").into(), + hex!("f03f3d7a52241ab959560beb9b748a8ab93e2b7221c8070561a12a5fba8d4434").into(), + ], + }) +} + +pub fn make_sync_committee_update() -> Box { + Box::new(Update { + attested_header: BeaconHeader { + slot: 5808573, + proposer_index: 430716, + parent_root: hex!("0be3932fbc9ebdf3220e2195d87653f283d9f999946e53f6a9f6172b6f532779").into(), + state_root: hex!("cdfacee5c92a351843fdc4591ccf16c4f040d0276add8421f08dbd5c71035a1f").into(), + body_root: hex!("3d248ca71ec98250b8dcdeab1207806406f1434c11874655af56925da6bd88da").into(), + }, + sync_aggregate: SyncAggregate{ + sync_committee_bits: hex!("ffabbff6fcdefbebaefffff9e37dfffebff57f7bffe3efbdfef1f7f987751dd176f3b3ff7bfa3fedff5fdf7f7afff7ff777bef5f9f7fe75f97fffe7dfdfffbdf"), + sync_committee_signature: hex!("b405701a0227b7c40805504a66069fb5ef99cdd84f1e295c9b4a4eccbe4d93718740efa9f8eca62f563dbc73021c00e914a69b00a9ebaa906e78f26c1cb8088af916096801c787f18f493b1479fd43f1f5b28d15af827a1e580713fa82bfa1d7").into(), + }, + signature_slot: 5808575, + next_sync_committee_update: Some(NextSyncCommitteeUpdate { + next_sync_committee: SyncCommittee { + pubkeys: [ + hex!("8e9fbd36b3cbaaefc176cf46336592e2b59a51e3035d095da9e1df9d2fb5aac5e47ad05d27784ca675442abb875a6559").into(), + hex!("b4c6164c5ea19f3da5a76a2435db598bb012ea34cc8fb6d749f1588463e5c39d29cb3d45ceae0543372246549b17deaa").into(), + hex!("a89c780da1a713e86b149d63312aa840e865dd926565f0ee9d9627d363eadadf5a4bd5f79d8039f2e2927ed7fa60209f").into(), + hex!("aeff2bd9faa0201abd7dd681ff97888c0ae71d84e71590f424facb2e37b5759f07d338dcbb695ad6ffd08d903c0f92ec").into(), + hex!("b89cfb61a59cdcb61e9f3ed76cb5cf13c907bdf6b2622e16d140743c5021d45cb6d91ee94331130b876efd984575948e").into(), + hex!("a2c889cc5195532bcb5c83d035cd6881b889ffb9d0536843d3fb6f7b1c093a927162add5ab6ca5f06e7c3ec4ca4522e5").into(), + hex!("b5966a6d047ef679a9613114149530facbfc7b4bee6ab23a60853f45de034435b624ad0126ac6c7d6a12b1be93177e0d").into(), + hex!("99249360fc064fc2778b37b107d834eecd5eae29e8f10a45d946f11fe358db065242482935224226e83f518fa6916962").into(), + hex!("8c3548aa879d974c5542e59ea43bb34db91f92c7d21eca5e3e4fb9d01364c21e8e2341eeaba1d22da67f1f455644afe6").into(), + hex!("828b95590a46cdc4756fc1a7b7d7c4031637494938521b74a3740a970ff532b88ffcb5333197088f6700925dcab5c42a").into(), + hex!("ae2e6ae80c16831c02170dd273ff6808e4379a8baf00e707d497eec6cb50b5a1f132eddf053f243765a54695ca35c443").into(), + hex!("a7bfac686f7b307d794fb1740a05cb1a6ef14b06150e64353a0b6544e7b0c5e3a7c8985d257c5bd74e411c0cc8424479").into(), + hex!("b3671a59e2d425ef0ed109932402ff7dfeec72cee39c1840cade48a13f3ff36bd0f9b3931d0651fddd214a2dcaf7bf89").into(), + hex!("b56c962ab20fae058c256e37ba4091d7a9e5d3c602e3eaa2d90df65fd5a11ab68f245a5a6e53262335c6dd4f3e0e51f6").into(), + hex!("a8be83de4b06ebd8c14bce332a1175a4c651fdddd4a58ec85bf4c68cbce83a50dfff8c26070d104556883af678693076").into(), + hex!("b4b33b7013c6af21797478b14b1dc81fb7c5661fa2471d8cb4eeaa62a62f795aa9be2cfb65ff6b957cb7f89487a587af").into(), + hex!("991a42351791da02bf6c1a9ca8248901657d6f2a95225e4827ac3171b5247cd31f9465c9ab1c2b78e268c82b61db1f36").into(), + hex!("88a38e70998cbed82ae7f9c192e06df8abdd35278efb25a1112246d46a3d3f0bddee41f5c492949f15e651ed7fdd6a15").into(), + hex!("87dcb7f13c6af7f7d102c643db0406ac7fb06fbe1fc647f436ea839e75561b27beabdd6133da332383bf22ed4f83fb9f").into(), + hex!("a441e2c51448b6b2ddb38dab213d9ae3d1fe70e91e1feb0f98590b5fb6f3c18ec0adccc221fc44ba027511c52e5fa626").into(), + hex!("ae5f4dc4266016943cbe1db6538619c430639a1179d246cb820adf8edcbe55e9f79471134d06365b0d459b280aa2282c").into(), + hex!("8165bbc59ef3b15f29379a7ef90d8b3610590c662207ea7c49267f36b5b62af3d48008d182ec3384ca7c1063bd25b284").into(), + hex!("8cdc4e6a238afc55406920620fa90f696403afc1797562b424c26e679096950e7f42b8d8327ab0d7573608056364fa4c").into(), + hex!("a1f4c958f7bd1182cd4ef88561eb534c9ea3563d149a276fc256645be0b2e86a3d642ac17261696ada39a04a866973fc").into(), + hex!("b19e5ca1ef1d4fbac5633cd29e9510116bafb3229749e0e4444caf9819fabf9c4c805b5966c02446c1eb0029b3c1293a").into(), + hex!("a4682af7e19328a145a1a5c43ad3e14648b90f664c6139eabe1a13da9b763ef23947dc3ea2054af7d0b7018f7498df51").into(), + hex!("999ae1a8f2e0cad6a0378e7e0a67c8a6ef4a824043b34e67074d05ceee93cb7b49d3c3acc961f1aab69b45f89d12180f").into(), + hex!("94a9f1686e91ec733799b569e3b0313db64f3a219b48482e2a56c21016e800d4373c2f8b876a923e0753a464e5fe4684").into(), + hex!("b4936942d807ad09cbaead9f56ca124617fd1fda2ff5cd94fffbbdf5ff2b295867acd1e41599928ae455d597ea45cfb0").into(), + hex!("a74166db86410c9722e657cdd0f4d1da86a4f83168e2bd9ac71850bbfd9471e1ed88a6476b75ae5ddb42afc62a9ac121").into(), + hex!("a97909c10241e046dc707ff9d822c385dd68be297d6b54c84fbdc18f5a1dbb3350e93496698d6304ad1d6bfd34b4a041").into(), + hex!("b6d9b775129b048a6c577656ac2de15135c2bf1a3c7c8140ce20a990274e42d7b602ebe932855c1d03373797ea0bec63").into(), + hex!("b59f975937cfc8eb510c1da0a7fff1960c46b9235550cd6decb514805439f08b8f18d88ae0373bbf50b028a08612d552").into(), + hex!("8af01facbaefb24cc4c11e13c64445600b1d716be66908964ef79e12c0eded04e1d23295444818f024e55df2aa911034").into(), + hex!("b79607bbe31f159b208a0d1b2f95cc5373631908292126b8b75fc44b22a8bcc9550de7b51ada33e5596d0f17d5f4e48a").into(), + hex!("a1a474a66940bbf6e601b6c6e63103de2d5eb76d7ad3d39dbd74149658a14e31143a9723327a73bf72eaa75dea42c3c8").into(), + hex!("ada18b62cf80098f36921cb0c2f85200fa362721c4673546f8554e2f5fc8639f2ffb2cac68e888af7ead8c660b0db13a").into(), + hex!("9198582e8aebc174dd168c6cf20836a21cbb6baeffacf9f933850d8e0fe0619ac1ebb99fa6fe902c75927531c108ee5f").into(), + hex!("b8a44b23d29cc5ae1f00d5384fd06f31b73ef1a7ffe334b59db668c924aef2cdf60c3070a44a12b52a14ba185198035f").into(), + hex!("a688064e0b3fb3baca87d711b29419a02c06e6a1dd764af31574dd84fe870c8ef614d4c2d42fc9508711dc05fe373776").into(), + hex!("b86c167a1c6738bfef1feb7eef8f553898f69a933876acf675596fc2e39f0a8c83ac37df69dffb669fcba4e3f1caab92").into(), + hex!("a32d52f3e9acea45dfe9ce6c577dea8200e68d6ca39eab5d6fd24c508d2028f533b8b04f1a4fca7965315ee5dc5e2809").into(), + hex!("8ec96bf235d5e9bf36382d79b4bd1be8a8e2b23a9f7f9e02ab6d708e96a1c12fa81eb236f02b0180a0cb9f3c1bc28cc4").into(), + hex!("a0261a76664fe2fcebe1501e18eac7bce32b947db7bccb7b746757ba51cabbc8bb385600a99b248887edeb84f82a6f49").into(), + hex!("a44313f945a1d462376e03fafe6d7a9659dd81046460f45ff8914732ed268b2430ff632aa0d368828c2076144bdc8595").into(), + hex!("a55fbee79559e1fa7b85718306185e3769a92052cceb600283d0236accc6ba2343799c1856609faeb7c685dd504384e5").into(), + hex!("8a3d4ea2eea81742fcbde7a1bea5ffda55c58b5e4618ace17773057932b7216b96ad4a117d9054de18f71b3345a0076d").into(), + hex!("aeaa0984232b1fd5607a1a67051d42df3ffe71363639e5130de243cea84c87554e6597f3f07952b7d40d19b6e18957ad").into(), + hex!("986c868f8f25db957f44a39bc209f1ff8e98e9bff52236b2473b8ba977d0b7e90d146ec86a518a581c5de796290d505d").into(), + hex!("b1cb0755c54e0619c8306636e926930605f15901c01e36822131dde1538b063d0dc485a97534ef1e12f2f0febd1092c5").into(), + hex!("93cf415d4d7ea309b85bfba7ebefc0d1ea91b8e93fa351262d9eb34b728c7a516ef0904cb2b3549db2b3b3f788b147af").into(), + hex!("97359ca81e9fa330f4c0a3b4de96ff45391c2f83247d1f73a6884bd123d34edc66a4d3f29718f5543350204488ee51f3").into(), + hex!("8348c9b229787630ce26a41e7c016adeef5dd3ec1f124081baf9db4ebc1a3f3a37b40d94183ea9eedf9a458a2e65fb41").into(), + hex!("a4c6b13c7dd27497917bc9a4c4a91b953b88c819e147087b125c93657ad082971152d384e8c512f48cfe07a69f54fd95").into(), + hex!("88008b395718646492ab944a9139b95251214c42e90720c703b19b99afc971824bb87c2a4d40202cfbb62bd2ee30c15c").into(), + hex!("946969ae721cdb08dab293a638387dca6045e230cd7c7b7237c75e123355db9b8e444089633d0977dbb6e42a729ab4ea").into(), + hex!("83435817ddeeb242c37d31877a55194f208f4cab406b10a4a0605a54a19745f3517a880dcab8a5a4422c0e19e2ea8a2f").into(), + hex!("94fd0a0f870a6ed2e6a4f53f5dff5b5adc1a6943203da6a34c73694702733c991e146f8c7108ff35d563fb67f55a106b").into(), + hex!("89bc609d5223c73afbca46a8c3cc271990a8bac5191f1ef6a2c88d7984adff00d67bcfcdb3958c259e17d5cba62beb28").into(), + hex!("b39077093900919b51e68f647d11e0f78359be69c405fde5735ce6839f739081437b899f33c6c9e6c86d4bcfed059186").into(), + hex!("a0a6f9f588e336c14b91a9c0c56085830611df85ce6e99d759c72a4dbae500b47dbe736287f6b2d65b448a2a0e6ca237").into(), + hex!("8222a17ad961ad325b819bd0625e079a471e597adb89f2170cb490c40f6b8b1a08b2e23a1abec02011452d589b183702").into(), + hex!("8b77ca7fa195450ab3399f88341a9d323e8b9b7b9b2ca30985d97ebb287e1f9b7e0279f22ab3a2dad682d7906f6c8d59").into(), + hex!("b6b8389382d3336bc5ffdc752bc699a6bb0057dda7879901c7633787a2b412fb7852fc896ce95cd09a9b98c76bef1b1b").into(), + hex!("970a20613047ad84b61ede90efc41a91ef7259a5fa79ba23964ea907fba1fd88d2710b69fe5bcb0d75ed9fb68d02e557").into(), + hex!("8843dcb71117b6044b1c7eccb5010d0a2f93775a98909bf23c1773ac9eb1c0f43aba26dad08ea7823da593c38a30598a").into(), + hex!("b5b147bf651ef9696ed3ebcfc3ce226b2748a4c4e7cbfbf12b3ff5f1f0b2ee1372477e1d7d8aef8d9ce3ec602a63d01d").into(), + hex!("95433edd328aa9223f521daf6d78ab272fb83150bac78ef6639cbc032de8834049d4992af0828946eca69f359987584d").into(), + hex!("b2f4d2154ac750245e966f62b92022a136ed0313964ddf534ff3e9b4456cf58bfe429ac83b718bb38db5a4fbafab23ab").into(), + hex!("b6b1f2a3a99496dab156c6159b8c98990501894b5b0cf200c792bc462263cb0aaac570f5a785aecf367a0531ac2a87e2").into(), + hex!("9985e3ae265653082f068b8ac4c09d10b2543f920a19911cddf18ac53a7f921da86f11836f51f2adfb26c7bf4ad51efe").into(), + hex!("90c31f4985c7481e5939766dd080f6ee01ac7a4fedb9954b9d1fa8fa1cb0e6e7185a1e31d8503542f1d409ed2f550e88").into(), + hex!("830736923cdcbf7de3ae650768482845ed9b45c4dc9928d66481c76ead9b27427a96989389c8583a153851ab957d67a9").into(), + hex!("82312631f5b301fd3ecd8b0a6e83b130b6e997a5a1e6255e883c590efa00b0ac3bee45c15308efe824aa665c8d7a365b").into(), + hex!("b8ed7b3c092f7bb05aca8cc4c2041161426908e8db349cdd2064e95044f9e7649cd569039e0ef0a94e006094113d0e22").into(), + hex!("b1d6e5344b74a67699cda807ad4883369a77d79335cb8eded6e0ad9b64c8661b7ddb47ce4308ff69f947fc173f496ac0").into(), + hex!("922f0a2f84e476e6fc00c196eb913ebfaa6b205fa8ce8c8453330a58956872eb2e4ec0087b398eb51819ea2d0aae6b21").into(), + hex!("8c729483a3d2ea34337d9c6260944da7e2ea8646de66d39617924681189c79672b0ccdb63525cb4635e3cce1d8f72f13").into(), + hex!("a00af936fe87caf17f5b365f59d019f8438a62b8f174510d863da59097986011a9e76319d4125ceb64f1d83defd822c4").into(), + hex!("96d33c3832bf0af5900a20c067bd45dbf3f0ecdc086eb065afea6c44f117eaf9ae8841848578d2915452e61bad014803").into(), + hex!("8a16c15a161a1e898bf06a23f62a9ec042c5b9e875cbd54d62e11be181647cf09e6a0bc65fd62017ae150525c16ee746").into(), + hex!("b4a0443c452085bb77b5790be42005178dec8f9085e2f1b963d55de6978cb608b7ebb42e4a84f24350c768c2e78d22c4").into(), + hex!("9168aa07b4e29c67723f4b87a025fbe6876f13c69505520b4dd6b387f16530a886bfae5e5304539564debabc059589fc").into(), + hex!("9504db9c5ece4ac0b703ccf751503665746bd580f11106df3c8a903ae7a5c9b0520dd16c89671967e2aa12775af4f67a").into(), + hex!("a776127d4e2e46c7dee8559ac56b266e7eaaa26eb8db0a7f4df0c66fa1564a349f414c9091d1e4c3e7ba96938916c769").into(), + hex!("a85785ed5832dfa8c9a5dd9d20523591f04536712c19a38c2c1496ce9c8787cf37964d739f83d9979db5574ed524d557").into(), + hex!("a4ba9a3312c2c253394891714719d2cb369eea993353b07f9a6efe3ecbd245f08d69f3e1302d6ee312e743c05ae85cf0").into(), + hex!("8329604134885c08173b14b7c68b74ceceb3694a0a3f7997f566ba94bb3fb2ad3f78ab3d02c496858bfc95655f072e7f").into(), + hex!("8e3f5485c98cc317653375ceb44636054a3202045bea6e9f6faac128e115de7a658a49a6432858db7b4b14fccbb93f7c").into(), + hex!("a706c82514d19152bc4097f8602f792a4917f5cb409c42dd42a5e4f2ebd1bec8318019934ed6d19cb43123293bf4ec98").into(), + hex!("ae14d5f32cb99bf3eb0d844157f12b836963be0d6f91b776f973a66701924c1ad9c3496540db4292580e6be871486486").into(), + hex!("a45ee325452d4bb2c60ef5be60b7d601158ca1cfeef0734727562b94ef8f72190005567e2007e8940f8cc538838f1147").into(), + hex!("b8f1fcdabf33ba011c86487a082b19fb146de932a469b19518cda2ac046c319059382cb8ab3715f8025573ab53c5cdd6").into(), + hex!("a613f3dd6c8361893be08f816c640cdea4d57d3207704774eeea8818edf102cba7ff7b06c4c5d0fcf0873b09f72d1ef4").into(), + hex!("aaeab877b1d16a4db6e47a8a864e073c4742e0a84e46ae8dea1a0eed0d2cc9f23adce9e0c8d88464ec0c059df99a9583").into(), + hex!("8fed26ca2cc519a44ae38398d856c3f75d1ea6cd02dd36dab004188f3ef2167cd67d279580f37176dd70c1a0ab08d72c").into(), + hex!("ae18905c02f96e110f40d3bd99ab26bf28e0af939c6945966fd5e3ff440e54bcee56d667a0a21d8326f88e5c22e42506").into(), + hex!("97d803614adb6571f4ea11833d0d9ca8221e7fc99a960c637d4990a72727ed2713da874bf156dbaa70bd4c2f668681fa").into(), + hex!("aa86c3bf79ebf46e1cee54f517b7bdced4c7a96d3ab27405e7d68dba92ee6fc7fc91a107f3cca85096f0d2581cb4039e").into(), + hex!("b75cdcde1702b5bd6be180dc8ea26e5534da77b1c7bf711c8447a565a63d073474f0270d78dcec78ecf5baaea1f75d1b").into(), + hex!("a36e810e50d283e8ef625cf684c1fd333a0373e5b0a9d81ba40cabb76299af93c536285c5d7239e86ec56905245ed2b8").into(), + hex!("a16b6b41e5c31901f3c0fc2a7dd8c084fb508947314c4bf4b6fb338d95ff2cf49fdc5de1d6b9acddb1b096b835df6ad0").into(), + hex!("8b580da99256b1d0d7a90dc46a98ce5132fb3928d416f2df5ed1769544692482ec8f2ad5f57871041d8c78d00c949a0f").into(), + hex!("945dc91cffe575f06b4b01fdcd580da57403469a21db6ffaa77ae06d31b8a2aa9957e26db1bf89554611f51f10c8f73e").into(), + hex!("8c55c4000195fd1155ea608f586a327cccd1221036ffd29eb9903f8f28009083203f18480b35cf82e0390a5ffef4bfb9").into(), + hex!("87cec982094c85f6c1e402b74b52f7c0495ab4a2d3f2309734aa0bd2bfdbc88b8bdd9556664015c7d9fe2f138dd7c807").into(), + hex!("8fc576e4f9057d82e2fd2270a787c596bce5fedbdb9f6d612c2caeb1a778450d8c1f6e86dd011a45f3fe7f201e520438").into(), + hex!("a43eb1acf0de695d478a661a71128ce9c58923e3adfb62728a2e9f185c9f46877db645398546a300b75f2c849f5ab14c").into(), + hex!("b57be020fd23d3fcd6057997099fdd648dae32cb750e8d058b62a5e902ee5ca27771d762020cba2985884ffcfded3500").into(), + hex!("ad3ad1089c8232280e9fa2f6c314ae57758cfbc3a0663ad9517e35b74b19e49345e03d1d33d0d7b69d736501ec5b3f4e").into(), + hex!("aed7faac2e65c10b52d7b3009eef010a624c7f57a5c76c55afd310345707bc8959ad619101b9c1ee4bde44152697c537").into(), + hex!("a6b177c7f945cde42c5389f7258689aefe1b6ee0b243f9901c6e60ef1bebbea9bc297689cda0c93aac9b28c7d70d0022").into(), + hex!("b079f925c29da333461adc949ff4daf19d0500f516d95e3a4c3dc2e2f5ce26ba0f08b2473c03b6974146b239532deade").into(), + hex!("8c573c73d603c8ed73ac3eedacd8ffef4c18425699e30d46be2dbbeb3590380b0fb713daf3ff3cf7544da502dcf35cfe").into(), + hex!("a39331c8acd40377f020611ac9f3a758832e0a644a5cca318c01e654696fc607e299b744c0cc2ecee2bca755c9aa3581").into(), + hex!("8e71e261664d5a6094ee912fa7e3e866ebb5c4c610062fb5fd733359d0e5a5d806a3370155ec3b04e83cf7a2d7c4a0d0").into(), + hex!("ab048af1200dfb67b4fb6bc8bbcd8344547e57942f7397c06988c9c42cce53784a0282fc13bc878635a3317b8f306a81").into(), + hex!("b32fc2da89d3a3541a61338e6b0c5a7f477a23bbb9a7c63b1087f36c49b6d9a42d4720708af496d82c56e1e6836f5cb4").into(), + hex!("97d0e8f961033e4aaf96a75f585d16eca691dd05f4a5477e8d3a0fd97d02555d67b29d314b5d150dc0de3b72810338fa").into(), + hex!("8223bb67c99eda58237a765c8fb426871a1a9e02e6e91d956b16e57b8dbc30c0edeb76abb30ecb2f4139a19922a4c62a").into(), + hex!("8d9ec5c0575500e433a4bc66d196d404b8619ea38b0dcaa036e1c1453eb23c6949509243531ce59318c22db6e33ee1ef").into(), + hex!("933ac0e3e6acc7a238fb5495835a591db77c39e27f4034dfaea20bce7c072ff6bb6f59a9823a07a76a431905afa2dbbc").into(), + hex!("99f3cab4e8005fdb6bb44900a4f166ef0c2c48dad85c0a127c4d854bca4ad32a2954a586734ee0e57f3317e5b81923cc").into(), + hex!("8f01dc4011ea394a9f7a73b7f246bb00472632fe715314525f1db2cc6158b22dad22d1371e7d0b2d5e72cc408f07cb25").into(), + hex!("96e702adba7420e819338f6f8740946289bca6f24a5f14a5bdc727d1cd66bb7d2a573cdec8ef1333ca39685c33f6e7b0").into(), + hex!("890ab24865a2652a8fb96ced381530192d072cad275c19539cd74e03c001321216a0999ba83c8f3a162bed003dcbaae1").into(), + hex!("96e2f1ed5f78c0b018cb388447bb85b33da331a5a306ce4e216d1070beb7c3900f979ef128e85180c56958c0d729ecdc").into(), + hex!("8ef642d5a1fa4b32fd69f7f57886d1d9447ddd9a8425a03f15633cd688e41054d5243ad6f352a5a3fea2be2f3cb7bede").into(), + hex!("84a3177be656623fe280f91e2acddba52c068cf8a37bd79b9b4186ef199bad65f52cf3e47b581f1964c9987f088fabd2").into(), + hex!("8b913725eb48feaaed46b2e3ddc0cc414aeb433dfa584155e2eaf29020f6f1fa0e801b85bee4bd28831b5cc66944f411").into(), + hex!("88d802c75d422a713c19a600cfc9cd843ca41e35722e21a0614c3195ea84752337ee30991d860fa75a57ce3f614e0a50").into(), + hex!("b43cf4b09d02b20073903bf152f569a43864095622a472656d8a96efebfe3a20dca86871268ffc528a194bd951662d71").into(), + hex!("ac1c65ef79ad0e56184bcdf0680dde5547bd01b95d7e9c3c71671c71683709cdf7fb988440c3bfcb847c26f198b94f81").into(), + hex!("abebe453b3f2430a9287d0d5fc043f7ee434b33feac6b7dab58d5deed7568e0730d59f94b1883e3d43f3c2934d3f40c8").into(), + hex!("84e0acddddd0c202eaabfca7cbf88774ae374e841899942a2353064f132c6205ee378277b2703744a8bea9bc16449537").into(), + hex!("a69865f8a3f66ff4e548ce29a212041bcacdc85410b8467f0515842062b3204fb1b7616e45fb5f46a5619808fb390dbb").into(), + hex!("a55b426b402e9b27fadf87b27cabe5375c6941b22597dea75586eb9dcb699d925db77250b1d755512aefbd4eab0a2e4d").into(), + hex!("8047da13f072c9e848d33a0f397ecf3e783e7dd507ded7a4de25327fe89c183c8dd1da3d419b48f53d93537bc2c1a8e3").into(), + hex!("94a9345e464b9b28798c608115438f1eaaa60a56abad028729dddea3c856f7f871031b4f100626f8bb7a06d88f7cc6c8").into(), + hex!("aac8cc93a4bf5b383080738021fc56cf732988622fe0d493540545b19a6a54cdfe9f8cd2d2dcbd572bdde0d1f8cbb101").into(), + hex!("80f24fae3c8d202e8072092342f8b046dc9edfa1234c86e9f06cdd7fd2a1dc0f81ad69886a8c219f53a94b9a75cf6b78").into(), + hex!("88ac9c1d5c036f14566203d8e18421cdd21b2305cbd20f9857e4edd09e002ba0bb5c89b039cba417b353c6f2f63c50de").into(), + hex!("a45c8ac231d0ddca06f1bf03eeed331e9b524ecafa74642e4c4591cead603d4228cbb0701af58770100964fc880ff85f").into(), + hex!("88767dcda5fc82e5ee515639992868790ad56d2a4fdf1bd1ba1c5be51b381c149fc9db23b93488b54adc89fd4c48dcf9").into(), + hex!("b23e34136c22ac73157c7c5cc8a9491b0b5bc968c95a9c104b402cad9de598e323ada4cc527555157cbecadf48faf87e").into(), + hex!("99910638bfe8b9974a1bb7efed279de750deb046bc21a9655da4ea81a1aa807f2b76aa2a64d773b1b23af283ba3878f0").into(), + hex!("a05869387ea3b4c8f7403d85ec788499a993482538e0e2078d016f00d67571d1342187ae088c788dea518bdf295da88d").into(), + hex!("a7dd1735f178d53908e29db85ba6166640da8c8bc6f717e0da9bf74c547bb98a512266cf737937201cbf6d14bd9420ca").into(), + hex!("87f5b096a1263b51df28417fb423604879b18c4d0a8a48630f70e0f95226bd51a252d8be362df801680344330857fb5c").into(), + hex!("b1a5a549e27b8256c388465be3017dd123a7d257fdb49b2bb409c6430b6056cb8125bf88b5f196bc9e02567a6728c7e8").into(), + hex!("b70862d190351d6bec9c618057e407b43864a0dcf860b31ab6617f75e1ea02de49ff338a45af53783cbf10400c878a32").into(), + hex!("b27a654ece8541b9bf9c6ae0047969ebb69c4687a43030b1c412991dfaf349e2d3caeb6b7ae3d72ff0e2d758a04510fc").into(), + hex!("81368aaa4489c992a6ef3b55df26ece993958df2e40f04a95ca514fda56c2fb98f11d61faedd31860b89e89eab965f0d").into(), + hex!("94e14e03de977732b7c7faa60ec8180e77233a43d513a37c443be4fa0bac64308d6a1929de075b5d51efaa9bbd6855f7").into(), + hex!("b2e26d7b979f93e8dd55eea5a0f4985bb254128963a939ca07fbc33bf83ad7796e9426660b2f35088d7aa5fa0cda2ec2").into(), + hex!("850aee846e93c5204c1906a2782da71c0ff9e2d1962a778dac77561846e6f9290ab10daf72f189df0a57c1548bd4e6cb").into(), + hex!("aabcd7f870c299cabe4dad1857b3b6cc3b9fde2b525e9d8ec0fc1f497cd199108971173e61cdd5937c45758cbf7b9403").into(), + hex!("ac069d7ff2633fc73bb0b7607d9c27305a4e15c189c8da396d6685798c12ef179bb44cffeeb7435667fb03a799eee5cc").into(), + hex!("99e3eb82b955b2411d1b81d946e5ef6b9c6957ae0e368f4a9c279a0541c3a46e289fbff526a1f9db4aa21b92d13bc9e9").into(), + hex!("b5bc3dd1e05a66a1d775ae0ad159df19c7188f2c73a8553525855ab34617c7f080e217732003e09b29a5b36b12ba564e").into(), + hex!("aa631a69aa4a9c14de2c49fde83453633d17bb258a2b7ada723bc8e71ef22c617ebdf8ba64c72675440b35d419d0f836").into(), + hex!("8f42bb48587cafbb3936adc495e82981d7fd81d8c0233a4e4d44f9df72f8439a9a0228d6cf9d156ea608caffab8d9eaa").into(), + hex!("94e079215b8d187d546f33d5384673215ee65c70d3bd0778f67c11665af5fb025b4302518a0db6266996c136ee90d4e8").into(), + hex!("b3dcb504b50dc58ee7f2e2f78ca884d5fc081b570d1177b884c92bd34272ececf2a9319cb1cfb9df011d4db3ad266e42").into(), + hex!("a5e5b55940e379e6c0fe7c6ac9ab86f3836f261942e3933087f1e1deecd280af9afd95d1bfb384976d5947d5069531e9").into(), + hex!("b1a36c3a0a79836817a2890ac53c6768ed3965bf5d1663e2df69b1bba60910e84dcd4f917991812b305367786edfa288").into(), + hex!("a8d07cbfbbf31d113b80d3a1f82ec7c29c4d78007efb66b5592255acebbd8e1b0c8b927a866c79211d5d4994648153ca").into(), + hex!("acad1228fd1ffbc118ada45a27f33ea02a09455d0c295510da693d741ca3b5725af41b99967ea6d429f604736a4fac81").into(), + hex!("b201ad414928e315aa00dc60b89c7a15464d5e97c30b551a462d02c35e327d2ef3244a98a402f9e055a2f9af6e970733").into(), + hex!("861689f35fb72780dc0be92c140dec07857290495baf3137bd2e83ace2f268f205ffc58edfb0e09f323ea5f14d0ce10d").into(), + hex!("89138730c80c30dea01abfebbed79bbe6016b4924193d9c2e8bfaeef30616bcd92f0eb24d5345bfb005bcfea989fd8d3").into(), + hex!("b78092afc3f16397d2eaeb5bdd7fc6c01ef516a71102124febc0cb443f4446c18037ae75c7c1d0c8177454b092922ace").into(), + hex!("a739cb664cdefe7a2f38333fff13bacaca129d718a043fae1a1b7c4251a77319b44589429dcb9ad113f24e11d3b75024").into(), + hex!("879996d4bed3d3235c0f73ac8f3f612eecb6aef6756896920e0229f5deb1d91feff95734e6b4143ba89badb5cc1f0cf2").into(), + hex!("a213d854a0496d74526b3c37a48d6f610452b44202424a419acf206df1cf76f7357ff5c0899e45adb565535bb09c29c1").into(), + hex!("8afa04d66a3e8a2759ff088395cd98597883b3ca6d8811703f5fc74b822ce4e56e1dddeea2c099fb3e0f6648f990f1e9").into(), + hex!("8a2e7ca192972af2b77660b07aa612811fff94c951532e3fb6829e8031355363f4aeed0f9e02b845f00cc9ad4b744c4c").into(), + hex!("8f0757fa7ab1eabf429802c3811caad65833e763029c3aaaa43ce921abeb277d7dfd06e0e58d36e494871ab9bb090668").into(), + hex!("91c0c0b0564fd95db51c73637fad622e6769206bfa03e41474a4e68369d10de7da5d1bd2b5d226f0564cc1ee8c3e9074").into(), + hex!("82b35466d835a6f13080628ce407cfe495cbeee26a5168de9e595a122ae3757f2eb0a64a71ff1ef6ef26c8cc97ec1f52").into(), + hex!("80d11c7a711fd2dbfedc76fe018fca09295d5a3146df92496ba01063e5e098198cd9c52d3802e6cd033f64b3c651b67e").into(), + hex!("8607de2cda6838c70f262dadb21409649900c27a5bf3505ce2166ef6f616f4b7119aa3e3f3c62c1a508662d7d68e8f0e").into(), + hex!("a1c14cbb653115b6225f53e3e6ef8e25f87cf47315b25dea5e658493121ca22733d3fd2781920dfa3a04271d58970749").into(), + hex!("aa876cfb3d572bc1f84a5579dfe8df82a9177b441492382e8ef6528e28e46ce59fce9a82d42c1c2000b28cff06596d18").into(), + hex!("8742ee128452ee98f21360f903c0a57e600d622d4ac793f32d6732f5fc315f757bac89b0f39a4ddcf4b8668cd02f3e78").into(), + hex!("a2e04418db55c0d9163a1bc242e6d43230a943ead121bf8a5f50c109e4ecf0fd99e5b126a4fe2ae9b0a248e613b54f7e").into(), + hex!("841b7c0ab57c2cfb1a180a9b0a2875a7675624f0e5c779f01f3f92a1ea547cd1164485f54bc433d71c7b054a6fdfff15").into(), + hex!("9855f3506dabfea5a133ad49557c3c9e1c7b6965215cd940bce4bfa90e98d9c62999feb29da0af8768b99f5f82c64489").into(), + hex!("a65939fb29f1d913e36be1f877c8b9a3549ec17313c4354b1834cf7ca9ae220af26a72cdfdbc59567bcd7e4152d90930").into(), + hex!("b64edc36e0bcd48cb350350fce955609ae51f5bd197cb7d42b04a2ec7f8dbf236b2a3b23a6e0778d57796433f0e6e9df").into(), + hex!("9379a72a722c1a5c8399acf72ebadb7ab1c5a2e18137cd3850b211dfef907850399b6151ae7bdb590a6eb04387ce0c31").into(), + hex!("b621023f0d3c731f49a48378c3709a0c051fa1e3f8788d27169a76dc35d46cd6095b32d7e91794c35f4af8d75f950411").into(), + hex!("a82421a53687a444a065ff1e11c439cb7342a3edf496f2ccc04f56fc6630bcd79ccce1437479a6e7d6dce918d3d45181").into(), + hex!("857fb59242e6687e940fc114df3c06af5a89d85c762140b1e4b0f8cbcae9d604f435377d7a2d153a65e0dec099e3e8f3").into(), + hex!("95e6a571cbd7c7a58c1c599cb4c837c9f31757a6ee4ed6740e9d55c350baa847ce6d081023b43b397a3798c6843baf13").into(), + hex!("adc1e1f8523fc6e3d683dc0ca15ebdbf471de635f25fa7be6fde9907bd3fad130baafd7d21b43fa04738d4c19448d788").into(), + hex!("85f8ff5b661f9e529421f7e5f831db1919ba3170a59673546db695c3af8a82cb1ca352e07e6c801ef9fe6f501d5896ad").into(), + hex!("937b374871e35266c2815e4d0ad72dc2b6c756e840ec36fdb90a71ecdd4afe13f6064ef36b9d1590a39a7be2156fd728").into(), + hex!("ad4aa9b451187905652222dedfe6135111ce4eeebcca74ecc74f3464a07831754eb0072abfb96adc23d0c5c33a1d9f16").into(), + hex!("815bc7c9c7c84396bd0de5c71b78f2be5fddbbca3f600f341a21533ae6dcfef8bb94f4340ec2a90f40ab091efe4cc6e7").into(), + hex!("90bef1fc273005610cd79161686b25d88ed2ff2abe18f16a4054fa05dbcbaa339825616c117f55ab26d12bdf2e414f70").into(), + hex!("b96e51c2d2bd0fd78c4d3b9873d217eb76642c329a9ef293010fcadb45ef2f9ee3a9c34b0365e344d33c464c08a0f51c").into(), + hex!("94c4048c3fe7dfe736458ee16566027290f93b1f052c3cfaf28f5c33c32af6b9cc960d86181d54361dcc10aca9f81a58").into(), + hex!("aeaad402961126722aa5033c6bc7735d4cddf35ededaa08073ab1a8412e5d1d06e95c58c7a95409edf1566ae904d795d").into(), + hex!("93d019b814d00d5eb6e7545e6480da089fa48ba34f0a961c704db12e34a144818c306cdc4f31320e542c75eb0f1ca96a").into(), + hex!("a3c21f7864512c38a58f02c0c83993ce6294329b074b801404e4f941d21e4e7e5eaeccd41da8ac423c967b7230b2a505").into(), + hex!("80c26a2aff26da9d8d739496b5a63da6d8d35544c71b7b05b41ce4cbda89e6d32e85fb1e38a215aa01dc64cb43e089e0").into(), + hex!("8c5e66d7668ab7e0de06ebab4a4ffd13f24e4458610e64a972a1e1f15356f6e745cf36b8cde658d03817f2749616fe84").into(), + hex!("a1260c9a6727d6d4a4c147e0c7ba91c5e2a47b5a08a07a3ba0eaf9b50360b6919495b4aca5f85e8fb2e4ef1c307286c2").into(), + hex!("9150fbf49242afc6ab7f865d4a92e7013eabab432b341710232e0fd971eb2b214a3da5b82617a8b7defacbe060538ea6").into(), + hex!("ab44ddafebcba5a0fc1002f3bedf595f3245ae07c9184d640154968e4993d85087efa8a173a670d07eba1c00d3ed1c5f").into(), + hex!("91580bd78343a09b62e31bdef63dfa9e0c874d7b39eb8a4300388ab053f262e118f790392f73d4fcf7714b521690d94a").into(), + hex!("ad4b8eb477ff8e573a911a1a4c1ba027088828cde7907e193ccc4b853aea74c66d19ea99c3779f6ce4d505ad83f2174e").into(), + hex!("a5791ba6dd8534607100f405b2f104c987336d5c47a544ed571d0babc6dd88a634296773faacfc3fdc13a5f7ab0f0cc6").into(), + hex!("ada0d1c948bd8f66442cb4b9cdc3a5368ab6c585cd8be766b468864a8fbd60535e454943731d4121ca134743d05221d8").into(), + hex!("a83556f376d8cc4a26b53223b11426da96bdad5351c2fd451ea053346b334eafef9773e3486928b9d407d3e13d5dcdd0").into(), + hex!("83be01b65302a31c5ba09c8329f683904943cc8017cc8975272d7d284b6a15a3313e27887e4de9110f64445581747ec9").into(), + hex!("8acd831b27588a99743c5d4f61e6f0610faa530d6259f187aeade29f1c9d5956f1c01a5faed8be8924fe1e8d9de03571").into(), + hex!("b17e88d1fc760f3d6633f5b48411b7237e276e2fca621d3db4619da62993bdeb3c12cfe7d130a92e4d3a14693d1b87eb").into(), + hex!("b769850bf9b77a1958d5eb932f99807cb695eedafd476d99131e3d7340cb845a33cb2cc7b640a0f3b14901b506802ff8").into(), + hex!("89e7eb7cc852326b6e18cf5c720c4e44c474d254de9e912d22510aa4cc1952b5e5c40b46a4907be375b89bd57d9f1152").into(), + hex!("af7ec9a4b836709701fb497f69dbcd0d94bf986fb6894f48c67014bc8b0ca947da71722d87de0371923f5bf2ec82ec64").into(), + hex!("8d3ff45719a7fc5254e91c710d956a16b8e8435fc4c8f68d1a47672335246bba9627d7058510d25417b7eed5ece5c110").into(), + hex!("a99a7b987f7050c230ab1adfa50a30b4f3782cd31467ff9c2a749182a1974a36a6a375ed5b0909d1e627b32ce0245ef5").into(), + hex!("a0bea35f339b54d82e345204fd4b75d41af3bd08d33b223211e496ef7fcdd8e327dc5a9ecb6fcb7de134b3eaa43d30f5").into(), + hex!("9419df6b2bd022fb6c79566f932c37828ca7a5a1a9efb64f470d7ec06e0d6b0b0147cba88814581a0773c80cf3d21033").into(), + hex!("948b0cd553cafa57de03279b83fa4f28cdbd0ec4e2219e25fad53c9d3d28c619ede568ad6e095a155d117caadfa87551").into(), + hex!("b21383b264f67c8c66011a79e20a4d739d1f8fc258562e6351eb1e1c5b83e42090f1525886ff4b65875868ae17a8faa1").into(), + hex!("aca93939c30eeac8fc83c82ff6ba3549ff38121115a60b7fc94b7d64e1f36f65e932bd8f3bbf2bcba986c9309861fcfa").into(), + hex!("a728507043b7e86c0bf19cbd81a45e1ac98d2edcea4c7faa3381df13f6352232b711b03829e3eddb7770213866dde7ff").into(), + hex!("ae0bafa42eece82975171e94b14e7063a09bbcd44cea6b7da4b820cd5d984be4c00a2e9e5137b0d34603e1ac914f889b").into(), + hex!("b63cf4b55c4a62c50c356cc2721ae5a89244ba9aef2c9f5c93762837fb14197479435f593947c8943ac77e6a2ade0208").into(), + hex!("acb5cdbe2cc7c44ad3980f9ba74b0a97f36add3fbb4e9b513c62157c14812aa73fac68be8c30170c39d1aee626f5a1b8").into(), + hex!("b14ddfe1c42321feb8ffd76ac041814f3d690fc16ff47b23ffcc247e8722d50ae001b6df4e1af6cd7c67c8799c8a1907").into(), + hex!("abe60d6024a9d6874df7e59b4bbd7e1e55da22adba1d16320fbfa2b68e8db995997ce6f81f8809e96c40f548ba005787").into(), + hex!("a23307e2ee6d96561452294a9265cf0eb1d6f86b30c7ec48066cbbe889eb7f0d64819225293496db709a1fd60dde7e5f").into(), + hex!("8bbd00e149a9fd5eaca24581821c3dca114e008c3e92a36db536944f6b5e5e983628f155c2319cba9a8a2a26d3885add").into(), + hex!("954fcbe0655b82bfc15679237d98c3759a49ffd0eb7f5da1827711814b92e0a4be2c0b7a96fc16ee3e31099c993ce6ef").into(), + hex!("a9bb0a14061ab4de136605e94899a41c3585ed190b2a97f529e911f02ff389652049616b408aaaea81d38f08a8f6c533").into(), + hex!("b58fa12cd0b69ac2e5c50b543bb15abcc3a0c96cc9cebfff34c4f7dc83bb5ece69d881348860385456eb6198ecc640ad").into(), + hex!("b73597c5ffd3a812e8a553bd3ad2216282fe7c1203120624b86cacb8a7421ea6807e29fac3383cfd61d632db8e3af5d8").into(), + hex!("ad153b873be0eaa71ad3b0191067874e085164f8428b89c7d2e01af0802169a9afa1775ca0f9491350db9e6c7c6581e9").into(), + hex!("8a7fcbfc564fd1af76df52ac5802a7342aff25d745307d2b9cf29c4470273686d9877b4588754af0e1bbbbe0310c3fdb").into(), + hex!("84553c8b77c7e5c81bfb1413cfcda7f8fd95c78c011c19189784be6a5e7352248b3b30cf5c80d9262de6c35ae6d4f1a5").into(), + hex!("8866da76cc8ca6522c3d41c950ea7fd67d448e1d567ecfd0cb916912d597b754807f7489f5e3bea7b4110cc8088ded24").into(), + hex!("84adaf9a79d5c3bc8dc7e669ccc5d4964254d0fc32bc535e54c5e4a4f45aa3c409c11eadd4fb21f4c831329087adef06").into(), + hex!("a77ce1ee4f8feed6ffa0c5cb8fb7fe0f95a03117e746b56b5e8178d27a1582804e84a86aac1cbab53aadffd9f84c0bd4").into(), + hex!("863321bf40995482cff032854ad5017bd885baaa6ec4ef47ab6bc713640b1e258eb40797ba049fe677937e3ff7a2ba2b").into(), + hex!("a46fb6fad471bb923cc38748253f887b53153ccad475240bf7244c1f9f568ade931b0522911348d64460021639bd831a").into(), + hex!("86604e383195be9c40ab728db426af87698d0e34157edaecc357544037d66d40e558cbfef7b005f8db3c9faf541f2c6d").into(), + hex!("abb7d323687c1d0ecfe89d411d9a81d05d009b84e652af437cee40e89bd2657641cbacf28120fe93deb0a1d3b410fbd4").into(), + hex!("a8d2488d99e04b79057739e6e0522b38a0f68a21bc190696c38d96f0e58a9395e3b9011e54d2cf7e8fd0b380e753f2ea").into(), + hex!("a36fd6a64f64f40ece0babcca8926dfc005cb1d90e4adcaa9c01ff3bff8d73cc0f95bdcb4f09d7e3b5d761ad5c3c065a").into(), + hex!("9699c0c5b1695416470c302f3097e93b94004f42369be26afdf04aad49bed67851d50f14c44efc0a90e311ecb27b3387").into(), + hex!("929e6ba1338579c1cbe76f1b075c0fd9725adfa97ab8b821aeee75133a874426414fbfb5cde7f7f8b74fbd8b27bbf7db").into(), + hex!("ae874a087a61c3ba4bdc2a582cdeace6d321f81683636440943dd860c783344d4133196197a108f6f473bb1e75c597ae").into(), + hex!("95bb2da076a9fc25e96affc7e4adf71496dd5802d7443cb5a77e3d52ef544aaf939c0884169df547000b3afc55cc208d").into(), + hex!("b0b63d1993f601c8aa96448183ff560f291903e649192e2e34e796bb66a31f9d0edd0f03ba4f1d299fcfb1e931abbf39").into(), + hex!("84cc92f8897d0bc0efee72d62ec3a8c07b7c72e00913860623982bba412307c2c42069ed90dd996bc56ffd0573b607f4").into(), + hex!("8ae9804b99addebafd3672785d4402a583c97821087589b7a129961b6131fb18c2fa60d606cef4f636b6cbe46b5d6415").into(), + hex!("8f4c85506e99d383b103217077c70571ad8b9046d039174df6d9f1902f8b85143754969bcec37519a1340c79046f1c32").into(), + hex!("81a5a0214a381d72657e1142a781fa8db0d849e1e012babe4c912a1edebc5dbfc265bc7fbfdf8b6ccbefb55eb0fcbf86").into(), + hex!("82c0e11c9016d95501a97e551b8b926fa317f02fb6764cdf0981796e6e23cbf13e48d46bacc875681900fe8c1741cd27").into(), + hex!("a00912f7bf9abb33f1624dbeb5a960ed32addb4d6bbf9770b6d82d514eabdc339f751e41c8f4461e560141b53f086f8c").into(), + hex!("8d905bcf245556e52587f92957459a41b9974b5d8b8d2baf2d8a98edcac2a77fc8b1eb70024e1e28d5c7a190d9f2a77c").into(), + hex!("b8e19d883289d97b0174cebb92d12a8c6ab16e4a8f0db9d7b67ccb9bdf97f070352e6b24c2decd89e7894099445d8b96").into(), + hex!("89cee93ceec6e742aff71ff60085f04e9550ef5568012b4ef0aceec9928c677f9711ea553499db812bf80eb5df021396").into(), + hex!("89d58564c0215295070329974e51e528ee4d9cb197b089755a86451098bc2f347be8be5b0cc06240315f75222ba2e9a7").into(), + hex!("a312cf33dd49ba6488ee13200193f06a5801407a15fc79956f977586a27a4b2d4cebf0da22f6c1100ce2a0d08730a383").into(), + hex!("acb541c487eb8fb4034ca6208f542c5adec863f1346dfd50fcc0ba1c6866e43f0071c8cbdd62ce6a2498e16e80855fff").into(), + hex!("98022eb774377f49b90f41439cc6703fa152d1d38c0e0c78eed49cbc54670369cb2b7acbbc37dac6617e57e527e41b83").into(), + hex!("8f00d44e73473d7b96a686d1d3b0848095d0514b70128c22ccc4141219dc3f5d2ddc3345cc506ce0e747ba358289bcc6").into(), + hex!("911e37896367a3e8603eaf995480bbb62229a3758a608c4822410e46de45a1048ee6f67c2039aba9fc95281ba5476623").into(), + hex!("80a349a605d2968fbe362e40672b33eface969c975ef75a8fe82ee7ace1d0b5034b7af8667650e813876a8a7484414c8").into(), + hex!("8b00779d873c6976d8b01afc94734fcf943c1819ab1c46e512e0c43469cd08b93158caea1cde84d13a48f27407048748").into(), + hex!("99030a66c4afac7e3753abb669cdf576cf96e21b1d698135148ba133e2d8fe97b4875d770e6246597461958224f653c7").into(), + hex!("8766f592d757c09f617090eb8f226016073a992990e16fd64a705c0c3104b202d36de18ccadcdb3dac5d68afc2495b4c").into(), + hex!("961cce69a7a39c20500c96332d2ce4cdeb3d844082edd527fd1694cf499b30bd33f06da66047d3849b49f4c2ccc8bcf3").into(), + hex!("8bd5dee639c3ef32712931295cc5bd0a8820192aafd35d1f1f9a24130bf208b7cc3f2ae99d6fce02dcac4c8225564d5f").into(), + hex!("a89ba310a62330e7396ae361da7a74a596e4a8be02496c8f4b3c860ac5c3cacdfcf4790d00b2ffa75fa900db2bdb15fe").into(), + hex!("89bebf6f59151404989f282a567c378f2f5a04d85225e23e22e0963da27673f3c7e8990dfb526a1133a988811cd03f45").into(), + hex!("81e9ec6ed189c12ca8d4fe32e21c60834d7938f739545c7dd76303ce347b69beba9eb14ab780c00cfe3804c5756417ba").into(), + hex!("8bbbd7b584948e34852a26d18b9dbb46f2974fb68bc8317ba5a168094a74bbe2304e1dc438777ccf92117831f7986c84").into(), + hex!("8ffbf94991bddefde2bf0ccb115b00d7b19a6a448f093816b9db0c65a43a27519a52ac7b88e6e37a7f33d384d42b05e7").into(), + hex!("b62267be83a54451ace1b8e2b54994990d2e1d619e040c2075cf1906c25089dcbc08ed8c2f2f8f62953b822f163324fd").into(), + hex!("af6d55c342e0e7f0bc3ec547ebb4688a884b59410aca90ad6d8730b4fd3543952fe2476e2db871618d12901fbbd2b91b").into(), + hex!("9027b69f6e5d550acb459f8b4b9e3f05cf291c104594cd244b224eeb8cac419800ebfd5255d87e0dde31bba662e20134").into(), + hex!("8bd160918bfd8049878826f443fa416fe32bd018262b1b4802e015ffb0049197c34d730c5ecba951a93986cca1e23825").into(), + hex!("b82f2bf2bea66697c4b5ed6d340ba74bbe0dce84b2d23904270f3500507318ccca0dbc967a69c4379bd12766708dcbd8").into(), + hex!("b4d00a38be54fe5ba5984af648c9092b133f21b22e56ea106442421c03c26d282b81d31ef8d22ebf92c0c26f86d27512").into(), + hex!("857d95d8aab25f91e7cbe0ea70a3159723566192c1d6dc0e68c2b19565a865a0daeceb4b1c733f75b0dc9cbfe246d870").into(), + hex!("b222a1f3f2d05912987902a861796f43cde8124f7dc398170beb76b6434e7095a8e2d5de54b2692490cb0c325cef8956").into(), + hex!("b016f69dc65c3e72e77d43334863db2c364f3697c552d4bc0730f45cc32fab5f60a5dc0f9f2f6df409fe3e2ba3f2f3a2").into(), + hex!("931b966d70e048570c463bfe7ce7decaea3ad80d0540ca079ddb10958398ea27df85de2fb2b7c238d0763d6293d34b4c").into(), + hex!("aaa3cacaf65a90a6a8e8fcbb98b673160e5f410b28b08a8733444cd71de9e807e00b146ae35fff05160a786eba6793c7").into(), + hex!("8effa24ae2c3cc12aef32e737faf7985c03e2ccd984cdf740f31aac7a93ac295be7fafa3a4d47812d9e0fa5bc2b3472c").into(), + hex!("ae8f1044330885e22c376ba50926ca10177799628b1c3f6b731113126ace5faa7756ca80fda0c535ddc77d051632266e").into(), + hex!("8a6613706dce5417736438d9bc779e29646a0b12fb1c5b5e118aeffbe72d37ce71ef78d3da4f2cc8c1f3bb47f8721cd8").into(), + hex!("8a435b4d265a01f7de575ee8893105de8be608a370c2f1870b7b097bf3635abbdfaf164ac1c704a6c9b31d7baf48028a").into(), + hex!("ab842c0851dc81e247a42806ff83a2e23b86147d884cfc828cbd1f3abc7fee929657bb49a2910975be746f97d0bb7c7b").into(), + hex!("81966af3ed4bba12f6895bcc1e2d4af8a0b313f45446f4f2e966494460f77015d2c9e65eaf396653f8f55e50413e7986").into(), + hex!("b61357419b1d65649e79ccef51d68d1e7c746d77c7f32692b6ff315d9dacefdee2a527816ac3118e5c80e00212725c87").into(), + hex!("b3a0c1e36006ab666e4d4e98c78df5630abcc76e86b3c13c342efbb64c2f669d12a98e797429871c12a7171f7a751422").into(), + hex!("93601527015bc30178505d37cea121f19145e366be178e42c9ea7380ae34053c45938a3b4d8ef852ff8701764bf74a52").into(), + hex!("998a446e7b4dbd7a7a2055f437859dd3ddb44c52d3dad9250f085d797c821ed91a17dd13d00f532c5f0f2321c5b3eb9e").into(), + hex!("af35c5f4bb11a87ee3f626007cacfee4ca892851459cb9cb2e127e92c9c274f9082c905165758976f9c7bbaeb984acf6").into(), + hex!("a0df73b065667fa0f6a4894aba39b3e4aac620fb1a8a3be96c94423231917c3a7d75f04383b40cd802909d9cf018b0d1").into(), + hex!("85c9c5a5302b706af5af436c07d1a1a952ee1cf4a0cccf002f514473fcf85d05bc4c23b5da2d6d0b5d0aa503f7e41a65").into(), + hex!("abd1ffa853de61d8b26e6eb6c7eba5636967c155233a6d73fdddd361379ec51a74c242715b6ad0033a6343157aee7ebb").into(), + hex!("ae45b130af61f3e76012da75b19d46a786e0c21ca7c6b5bde193b2203d6d8b7b8afad25a198e8c920c69954d3d6bdc14").into(), + hex!("93729120899ef573f6f276a1ba861a400a35efff7a2074400bb6b5df818e3fc1f353cd5a8e4ce122a2bbd8f5b30126dc").into(), + hex!("b34615b2cf8912c51c02264008e0cd78b79c87b87d56db810d899490bc438d446f734ca958c7c291aff68e3211ec8c5c").into(), + hex!("a1ca372d158fd7eee15091582c6c1b9ac9854959677ee25e5786a94cf8c1d15b64f0019aef20331d9675e1f1ce41fd6c").into(), + hex!("a4bd66cf90f38233b579b8698f5655f077bdb1d626e1d36ceebc67cf7ab8ee8e129cbdc307895bf0fb6e34a4aeeffc68").into(), + hex!("ae06f6db3a3ea3a21193f6c6231db42d18fa3aa06a8295741bab35589dcb1d51256838dca01356d580e8c423c45ddbe9").into(), + hex!("b70b5f0cf21cb98c70996a9eb6e4b3562732505299149bbebef821477ff406dba3979a2526b9969213b9ba75e35de3f8").into(), + hex!("95cf5980f21a58f4604ffb99c8651752f724faacaa216f8c7cbe400774deda53f26aaa15fc6415e936f52ce13cec6ecb").into(), + hex!("933c0e5bbb358f5f83cf9e60c67ea8fdcc0b7a203fe5b07131e3bd69295c507880a1e542ab2a6a8d182866f7c6b14a8e").into(), + hex!("a8baa60ce583afcf85e4756dbb0a4871b330ee70f7872c3e36ac4d43f2587fbbdfa2a13162ceb7efdf897bb96fd2d97f").into(), + hex!("a127b3828c422ff51a067d482fb67074d45fd0a86bea5066c7f6dfa83f4b82b4584646518e898ff725cf5de055c6b236").into(), + hex!("871ef5a7f50e5ae528eb16bc30ceb64b97d111896d34fb4a65c93c8d0498eb7032033eb663f7b169a8af4b96e7acbe21").into(), + hex!("80d0bb10037029f0d8f8c9b9b46f0d0ff32b2198af44a4f84c8c0ace60f2b39f8f8d284308769c6075e9425d6229905b").into(), + hex!("b882ecbb78c758c951fe53b434af25b594e602dd783787f09ed077b79f7dd7851fed769a1593f5a5b938ea2171987d3c").into(), + hex!("ad41cb47b16077f73cbbc157527a17c936efa78a59d1f36e3c0dad67cd19fdc60cb772556018029611aa46643084f024").into(), + hex!("82ad3ad7a706ec19b39b0c8cf75d061ea3a1966dab04643f5b9d711e6651b45f0cd22ce5048cb51e4e118b305bdf231b").into(), + hex!("8286a4970b8db361abd04e5d197849dd335b7074f9c3fb91dfd19b7f43d2a3ae9e114b0cb6342463986d32d262c34d79").into(), + hex!("a844f14ffc4c99989a6c666dcdcc135c2fda96914220b1c565215d5c2c3102f5413b7edf9b25882a02a19aa78c2bc545").into(), + hex!("8e758f3b03fab7f5d0993e78674efe3f9cc211e268c12d23911fc01ae7a4c8f879a393e3fcde0c05c10106a59b59ab72").into(), + hex!("a973caa021aca6f0470460b84df9324ed894a441435a53c4f0c48fed4359242ee71fb3a0e4cf438839ed838f37e5c02e").into(), + hex!("ae17c713f10747282798487f02d25d2d8e7459ed436d90a895617afb9299ee81994ed68ef87ecdc0660b7565c323f0e8").into(), + hex!("978e68aa5f44daf9cfb9220147ff509ffde89d121d08d982a0fabae9f07cb3145c2312ad200f2f0dc051820fc54d07c0").into(), + hex!("844e58a9e35ce1005fd5785f56fdc9b3f7e8e073f48fa40da19a5e9e84aca00b8743c5407920cb554e926873092015c8").into(), + hex!("b296da231b6ca9d5535432c81f7d0c20a71cbd32d357740d1543e1e3910ea5d32b005938f9273af96e401637180f4606").into(), + hex!("8aee25d881e7ba99fa6aa2fc65ebd44aa498d31ecffc595ac8cab010f6cfdaca308f56e616ae51d7e2c2e15864eda0bc").into(), + hex!("a04298e32052d7f91096285c73d67cfb3f3f5463abb3d7caf3108d8d77aedf9896c359986ae598bf9602dc90e6eb3178").into(), + hex!("8c336e463dd98ecccefc55fa366ac70a2fcaf60acba2f2171490642a6f616a1c6b72601bfc3533a5f49ba02dd1e39fa1").into(), + hex!("b00b383ef5d68f0939c0538c7564614401283c6923dab4db4c72a05a88e05bb576ac374eda61c024345227bf45161e05").into(), + hex!("9273a1a6c9cbbc8d5a46498b7658f6125e955cbf19f0461d1372ea9de200688e4f7376b23b132b41cfd672fb42ec48b1").into(), + hex!("a7f04c0377207a6bb5d96e2e6cb9f7696d5dba2acc3dcd6021ecdb3d121a558999b2b3d92497f72f28f39a551b2fbfcb").into(), + hex!("8d530cf98af85dbd0bb7b1f2fdc24d499b19a941ef431bc7f37ca9328a4f6ceb0660eb87edbb1a5d868d3141fe6c51f5").into(), + hex!("ae88acd7fddf35c72c3ad1c507f8dc185546d8acdd92d70f00991f50afd67809167ad3171c7e45976456fca033f0a95b").into(), + hex!("8df71ffbc265a4bf475ac7ebdd5eba137f4c3b585075ea8957757661b3f11e7a92888094e85c8863ed53d91df45e37a7").into(), + hex!("917737cb24287f30c899dd88853ee3b9be54ea707ef38f1545ef9e436865be1399fd2de4d2c04e3fa5b4a3205f4305ac").into(), + hex!("837e57594b1b71fd9f25f27967b721df500e3d7c72f22e90f4315e10fabbef027ec1db0dd9863b817071fe3c9413a5af").into(), + hex!("94b9c2155509b2189883d2237cd37c9ed19c3a22203e9e2b045184aed072e406e93eb7b5c3fbfb85eeca4c5e630e4ed7").into(), + hex!("b022c75923080450ffe5ecb8e01972785628ec2027b6bf3dc2b09c92ba8ba55222965767c21a240705ed5af6e9d92695").into(), + hex!("b993e15339e28a472b3c98bec723ddeb7728822571ef1fb1c2a1607a4023f37d663b615c7855426170d9ad8f6a971617").into(), + hex!("b34db4df6a97056021b088c53cfa7cedc8e585f907a67d1ee8412a50d84e5e3d347011fb99d5d71b111c88f5efd44610").into(), + hex!("b582bbdd7e0d2ccabe94e6d193d1b8dcad1932d1e96ae8e1a295cc05b381646f682f1f66cb90ddcfcd7735b335ea0242").into(), + hex!("b289c068f7c988a69173d347361047211c302abfafbac1d87259388b5197274f5ee90d56228093a42eec32039e490868").into(), + hex!("88e84114e8e536051ef5197ad181f96ce13fcd5627f18964bf4bb2f461c6638033ba363800326e494e43aeee94c62125").into(), + hex!("80d16c3e8717274533ff3b764984479b3bc709f11ef5129644dfcbb5d8bdedc7a8cb2e539a4524bb0fd4d977ffd25fb0").into(), + hex!("9405d059e30017152eca6d6d86366a7a5570501d78c3869d638a2b8a0bdc8c5bce9f0b46764e78680e2fd41697af9d52").into(), + hex!("ae23483a1d25f8b9a5adea9527560cce5552994b3964ffde2fdda0ce7b6156e1d76e698d7314b0820976776baee37b63").into(), + hex!("8d8395fa4ff7ad0ae3be3d3c446cab058890cf7a07d0a2825e22396cc938cf2d7a986745be5e3c1758c5ca0dc29c0ea3").into(), + hex!("8c5899cfb437a72d99085d8abf54eeb345d7da59ef93978b0cd9207853dc491451939f4b1a7bf317c87504ce949713b2").into(), + hex!("816ca0740bce43365bb20e41c3d0c88cad587e4c743b2c0cac9dc966aa8de220da347d65392a9b750a2001499027e3c5").into(), + hex!("b3d23c55cec1d18bedd276d1454f93ad28c72d921dd6600d8102710770f52b79ee8cf445f6781c2ca095c9a25d41489f").into(), + hex!("95d541d6196c1221dfa5ff213dc3e658649a3cd4afc8e738631fc7b6914bf0dca74f41cf382fab364dcb0d2d6ed489ae").into(), + hex!("a1ab2fd361b973027f6ee6a9f8f2c081cb5d7199d69e36c280c7f9c3e99b1cfc994ad7f68ac0cd78c61bb419251fa14e").into(), + hex!("ad36ebc0cf82369457b665dfb2f8444fd2add0b49658cef2c800ab0297bade2c0249bb124f3d321536933128c1149c92").into(), + hex!("8f917e000d7688a0f508777b8db7c0ace39677c4458d7e50d8c9dc59d32faba4abdedfce3af26cf3d04c020e526ab597").into(), + hex!("aa994238e432c51896efdfa240f75fe40f1b1a7b624ff0aba66a35f827b9bf1197de3cdc0bbd9d0147304061ced0325f").into(), + hex!("943da065ec673dd41351270747e40c1f5a8dbd7ba259c501349ed754ffb91c56747a78c392cb4b78a796d748044798d7").into(), + hex!("b0c45ed1457710daa48edf2e61ba59988a8257ddf902318c6bb00be7a4ad8235b46180f7353d9f0c0f747a4cf219d1fc").into(), + hex!("81b56da0943d2940fc8041a51c74dd03f6dcd8a705ef2ec3b685395e313224861f29de31205d45e944e437179f19398d").into(), + hex!("aadd5eb1be98f3fc7e93925e46062353576ef2ee81421fe3f6850701728b8f74637d66cbcd344364565b0893d8bdcc9f").into(), + hex!("a5dd3dc1e172c899f4ed17fbdb842bea7d7f3f0d6b284af9749e25acbfbb2f9c1afb1848490bb22da9ddfeef30232323").into(), + hex!("84ba149e940db1663271eb16e920442bfeb035fc601a7389f85c78ce7bf27b13b5c1a5625d8b45dbbf199caf2d753bf9").into(), + hex!("b5634eb31a68f45a2aa17e8eae7752d8a58673fcc9efad34118b0f3db7415edfdc27166e6d809406da0bd26a0ae1371c").into(), + hex!("a2ad4aa94a40f1c159c7588ebdd77b80edbcfd95e867ec6991f711d39b4dc911cfb6da6075db23bc090218976e9ffa38").into(), + hex!("8de6002f3e789b014254b32161b5595257eb01ace67a8cd9657235e2d04f7ffce6ae0d059488bd9dc070c32b5b7b3fb3").into(), + hex!("a5f659b41f35fe6a9f43d1f72c803da876ee4aa5b879fbe5d5e93be38dcd5f10716122d83afd003b79b8120d83358884").into(), + hex!("a64e6b71dc6ab9eeb17d50e1d2516c5ae63680b50a6077fd870780aebffca70a8b7f8627e23731b79b3866813a20af0d").into(), + hex!("b0003260e70f86eeba286d3d9f6d73bf15f084e7240d9aeee38a1173ea5c47fd9a9637384204001873732e6a407edadf").into(), + hex!("8d4df6703cc9f1d0760c67ae6b20928ffeb6b13d67bc406b8a534ecb07d6ef415a106ed992b50267677f7d114f9d69c3").into(), + hex!("8cb982f382ac327918387c16c73de1ec5ff979923f1110b9660836ddc2f5f742aaf970700f7b27fb2fdacb126d341353").into(), + hex!("b6988aa5e4043e278c01c83a9774175b1188bc2d78b96817a7fb406f86aa4395b7eb666267e819a5a0615803f840171c").into(), + hex!("b69c70007de643e3fbaf7b557bcaaacb67288ef6ff616c9c89dee0cedd33a76396a90cb44207225568870c7c5601438c").into(), + hex!("971102768c0dba73925cfff2053b1cd8fa88f5c21aeba2cf3a78ac853818bb4a85cff714134879e5c7d7c7994cff20a7").into(), + hex!("b00b8d49b1f1fc0e792e257b0c3c33ab554fe231aaa6366f5aac11ff35051ae23cd2a5c6b9eecb3ad00e840c78d6a587").into(), + hex!("a34bf3b6d9659f1a89e40e3b35afe741a670a3a9305278a52d479535f4b5973b23b10ccdfa194cb2937f150beca3535f").into(), + hex!("a84c4fd757d86613a4bfe72cc9d7864c2c236b9463e365441360686e19f7f772ebb6c07a24c680462ffb1f3939c870d9").into(), + hex!("b3c34a2470e32395bd9c8789905166ba77b7b7d27cb504b8647dfb4fa6d65884afb2f120d83afd876b4a00cb104347a5").into(), + hex!("b9d18f57a669686eb8ec08555972e54505b7d487dfea7105afc76333138d5f934b9b5f9a3a7896481782fad5328bdbda").into(), + hex!("a23f65babfef6a2442833441200291028ebf56031d7154a4d7d0fa18acd4b7bd78dc34b85924c3073eb5be7733ac10f5").into(), + hex!("80948f3e12ffccd88605cd4d67abb83014c35d8d1a3254f6c546aa7197f810cdd06eb49b99f424187df218c8e8d7254e").into(), + hex!("b24ac23a86e23a16f4f04ed683ebaa51ccc6d2d038674e36d00a14fb71d46c433373a8a0bb75a0afd3c2f9605dd4867f").into(), + hex!("b002aef96f1702f4e3c92b00aa2976b57b77ed97a4d64619c6db676b286c6e633eca63fb232cec0d533a803660c20147").into(), + hex!("9867492f550c1f12276a201717bb4c420bffe904d55008655f929368b664e0293c1dabe9b5a5a71ad884a12686c1d9de").into(), + hex!("96b702733a7ef38b23e45999a04bacebe414a01bb41792cd9aff566fc23610d02e069c85c6fd4173846a40657a958e78").into(), + hex!("8e588db21f84245d034d7427055996f109133b9b3aa095472b879bd180052657da20a48513c1f625c339be90a64878e8").into(), + hex!("b6821bb1a130460ed1936d34cc189980d8fae8c5debc5149d47e90aad69ed3b143a3a00de5959e21ada73a06b3e8c9d1").into(), + hex!("a751ae06c6d699b5593f5928910e1ef3634dedd460ccd3f23d74f5f61a3950392aa66149ae366c048c6c7ece968c2d9c").into(), + hex!("86835cbe686b81fe7e096af5ac8c24f7cfffe2ce2b993626a606e42f6356c0d7c3d4ea19d110aa1a7b7f7ec5e68808f1").into(), + hex!("93719cea4911ce7e436f7b3ea77b9cfb83a1db903cb38f1de3b04d0a69a0f06bbc4f9acd3b313d46113009a917dd5996").into(), + hex!("b0d240c09c137a742d77edf11a7257030ef8b1a785b810de104fb24b22535cf0d62bc54f544b027d532f16b43a2df7e9").into(), + hex!("9376f46ab931f5c58b1be49c529ace24fade089af1af43b339721321a273169c5bb668250b2c2b0aa16aa522e6675bdd").into(), + hex!("82636e68d0d59d20dafd7486176b62ca2d5dd0275c8fff552fbb974565ef2406ff56cff5c43a5b9e383e0b09737de446").into(), + hex!("8f300fd7f29640aeb759d09735f9ac36d1035248c35ff38a165d2d931058268a055971b4fd4dd9d467960180cd255dba").into(), + hex!("a7d5f4e2b01dccd9cd7cbf566b5ab604efdaf9b682bb4ecae1b7801c2f93425350620e91ec807b0a110971c316e68cc1").into(), + hex!("8952700221cb45e3ab933ec20978d9c9c7b873784299b86d0c2d9998bb6b1d1efc1ee3bcd00c5148b2fd9473838ce067").into(), + hex!("b787e77d194e4dfb89968f4e289e97195c2674adba0a3e7d5582cefacdedf93a0e5f27d9d144aab68aaba878fd640414").into(), + hex!("8466c67bed3fbc30e46639de92c422f06bc80df658340a47299ad7798cae412c976fa6ae6bc0a32ce655b93b08cbeaf5").into(), + hex!("ab5d78cf76e16fcfc0491886fc1c95a492ccc67fd31060eb183b89bf59ed6e2d349324f9734c504e74c360272a22f369").into(), + hex!("a4f9f8539f9f89dc5a2c8296af591277c2d308275234729dee23e35e3d541919d1aa9a260780899615e2a895ad8fe703").into(), + hex!("94ed2cf23c5d28497b506515597ab71120f4ca42f7ebc5a4e87b798353eb70590923eaaf163bc550bf312bd6bb2c0b05").into(), + hex!("a78f064aa69402af33f1c9c1bcf04634384ad8528aaf8d28ce1d1a04804bdf93a8b49baddd6870eedf642e566d091af7").into(), + hex!("86796909d2dd3010e8d46c3d99a3c0efcbd4e986e581ef5be4c7810ed8b92268bbdac1dbf1b24d6805df6642f53f0b58").into(), + hex!("a6b555b50ec3b60e9768f407b84c5fd8a055150c17f78f2d5a3b0daa13f2c692e5041184bfc8919280c230c57adc9ddd").into(), + hex!("b4f9b0a0242ded4dc0a4903f16d270f21f2e15668b3abd45f79e1b465bf50074d232f905c6de6f2727a0a9ef039f7681").into(), + hex!("930b7dd8666a35358c6a0a42c600dbe8ad5d9682dfc641474fbd8fba90ddaac7d3ed5f5395f297dd571051ac2d603333").into(), + hex!("abba3bdcee368688a8e53d56627b915148fcab59717174fe8136c85bc24e8d15046da09b31c0c7c9a5bac1016bbfafb5").into(), + hex!("ac199e71110e15ebfb3e58b8ac14f1de8ebd3e0894f273f84783ef3fa4fd16ce6bfa5d41421e884e258fe8e2dce68075").into(), + hex!("a53eaed94fe550c07375ff5ed9706d69563bdba724a4022cf7c639737c97683f036400dcb87268184a9877eb116bd479").into(), + hex!("86e8269c1b368228438de5aa4065ab9d2888de7599b00d5382ebba8fb0600cf357de27e20edaa50127e213c7f6be1f6f").into(), + hex!("8f8d9b9b03b178d370a9e918dd54264f83c4ed20824be79427cbc973a9acf3d74637e5c35080192bb67e64390299c19a").into(), + hex!("a230cbc17aa669c2a9b02e736d20519d93a6fa1b6ed452e065fa78a0fb4b3a0f55fcbc5e719db6417353ef0798f70b43").into(), + hex!("b4e28c30e57cd33fdeb257cee66389365f4a1b9847f94e8b0552c65adff5719e51d50ec8bba36a71ec24641ce4fc6338").into(), + hex!("91799f066e16f9a07a419d3e425c959052f8ad1aee0e2f613d3b023829fb1946c81b16e8b733ed03ca924d03481bafcb").into(), + hex!("8ad935420f026b166233ad387c58857eafdd2fcd4efe55ce1bce9ffd668b8997555927fcec88b04de795129a10263d1e").into(), + hex!("a098f20bf1ae2510f1955c586c7115a29c64bd22a086a2f2a7ff5e08349bf24086504a1e5e1fa82b3aa73a097bcd948f").into(), + hex!("909a3d2e7bb5538bd89c446aa53f1f05a1ec17c88d793a310866cdda6e5d836d53fbb80afbf8baa8aa49db3836c912e8").into(), + hex!("89e64879667f34f1127cfacbd9a3337fa28c0227bac5c5ac907d6ad9c3a853472f4f7cf093e3b735968229398bc7c94b").into(), + hex!("b6ea32c4d640f9b7de841575a00003c1b25d0a845eb54b065129c271790bf602cc39761316c4e9dfc1644ae3ff4b05e1").into(), + hex!("80ef23a1cf6f50f96d5b4645bc79ed4c958f1394da9e5cda0cdaca3815ac8435a8d3a690ed19665b7cd1bef8bf7b0366").into(), + hex!("8b7346d1f30de7e50e3d3b32b441ba5681335d25ae6025623e1469466addc5515415a29ecfed987e07bd6a85ec1eabbc").into(), + hex!("99277ae52f7f2f193549739a704aa2f756c2ddff68b9848040ccacc675fa12d62c9fc1318daecba514089537a4e7b83a").into(), + hex!("94d392e29a3a1b8ce63c52288fdcd3f95f80bdd2a600626e2ce3517162e69b9c0eb36bae6786701ba12e23a33b8f90b4").into(), + hex!("8f270be40047911a4cd5997668bd6d62c90780882451c544ea4bdebe061a9e61bafa1d8bb8af62e0ce4fd73611a7f34d").into(), + hex!("a841f7229fd0490a853523edbd12a5dc6772bd607afd0516c582a813a10fd74d19f480d02b3e7b7b6256896620905976").into(), + hex!("aaf65a2e4e6a3d903ccb51a063d64688590a3ae54c7df3c5e8820d88533a6f10b81b130ab476c9f16c660a81f66dd3bf").into(), + hex!("b3683ebb70696339844ccac03925ec85c8dc06959608527a1744c23a67a805e71b5a91610fc6fbe0f667d054b4087e37").into(), + hex!("b042a5614245184e5a4620cd3a67c51811fcc0a0dff63ac06dc8030f01d8343ff0bd39ab3cdc3c09e4f3c1503ed35e62").into(), + hex!("8a343ff96dfae2c2e62558db3cc424b3c055e2cf84b53f63bf49c95d98ad0b372517df7afdb189007b7db725a3fbd567").into(), + hex!("a3a81a2b37160a371ceee3d07194a70e58041a3e6f75f47c4d8e2c619cae3f44b2de5703e3b80fbae9778e284927170a").into(), + hex!("b9554f94b7c611c2b3f63df5ac0f920d1eaff7b424bc8e857b94c354aa1d3c02a4348510099750e0af3e16dcd0f9a245").into(), + hex!("8678aae32f5bfb9dc249a39eb5d0638bd45d48d2b5b7be32b5e1c2be7b8d29d7198a15e2c24fa7813f060309f2493843").into(), + hex!("85f020a228f8952986c979028dbf2c2e8e59191970ea5e4cff6e6bb46251138d9edd90a211ccd53fd395db8addfb6d71").into(), + hex!("8b0801e8ea30467063d68caa5d3315809b3f21c7429f256fc2a10f7e173f0e1d1bbcf59e025b9e44238a53ef1b8318d9").into(), + hex!("a4d3fbad305853d8057c09bb10fdea9234ff90d51ad0c21342248895d77ead5a8c104c554e6008677e396b55d4efdea5").into(), + hex!("a7f12f870c5a3e2332f10cc2db7dc26ce58a94ff60ebc28f3ae06e820e6514d80c5133563225011213e53f51f748413e").into(), + hex!("a94ed25fba32b4302fff40e2c55e06b6fa4b9820635beb0b43df61010ab5cdaf944199bc73c4827214ae1f57bd75e70a").into(), + hex!("8607e973bc67d217ea67104eb538fcaabdcefcaa7da981ae0322916f7a74229b47f18820458823e7ef160f69f5363dbe").into(), + hex!("a83206fcc995ca57583c5952bef2027503308472bd712c536050217a391bf0fa9617d956ff6c913c2566cbc515cb291a").into(), + hex!("b9110bd697c294a0905503490d17d5536ac1782514bcbdd6f67e8fbb75c0922b39cb7f742d28cbf5356e4f1885b060c2").into(), + hex!("96e1d2b723a458f6b8cd4a8b2a83f33fc7931901cac1fe2169ffbf7c1ac8b4d8547165af3dc61c2b37ab88d3e81f940b").into(), + hex!("92cddef13af28962b7e281d5c0552ee5135b7a401944c9ab31d617b072cf00365e24dd87f86f6618b202d51c25f63fd6").into(), + hex!("a751e27a646ac1f3c828cb88585aeae6899d0940252973329b0589b05714bdc1cd271bf745d482f670f4ccbcd9a60d98").into(), + hex!("861fc00a2edf468353c6012a89ab7cddeaf964ee387e5eac48037eddc536df3d097c69a689f09bdad189384719d50e0e").into(), + hex!("a7dda298ac153aaa6a59785ed7b6900362b1220588a29b44764cc45834859bba0f5b9f8f17bba97fb49fa2c7ed4eb65f").into(), + hex!("a6b679d47e1ea1469e5dc14e1eba97ba2a0f2cec0a9a0983ca086f917298c93af45de60765aa2cb3759ed62c9eb5e4dc").into(), + hex!("b95b1f4472f8f69ee010d36db46c268c59cbd13864c63ce9e6a4755ad00c2db04c951e312b88060e8411018cf655e76a").into(), + hex!("935883b9eca730ef868329a67fe99ed5363b0384e7e6f97147d4c80a76d9b2d8ae6783e80d103149a8a3fbfd51f9f6be").into(), + hex!("ace980d1e3c76dcf78bbb87f3ca9bd0bba7897fcf9e24b27e00fa22855b1b4ac224137361ef6817c94fcc81fc3d3a3de").into(), + hex!("a614b6f113d74d4dd6dea66125b11195212031fd7d3da825b24739e5107cae653fb89c34527b399c43340064a9744a6e").into(), + hex!("83c27783856f9af7491ec9fd34be730600afa59484cae9d3981685cadb869dbd05555a07b93db7d0f361c9ae8e0bfe73").into(), + hex!("850f1389e21bec1c8785d17316a09af9355d32b75d02d9ad72791cfc5a411595a367ba9eb641c5b7acd6be1ee21579ea").into(), + hex!("b810405c7415c49bce0f7893aecde90da33b71684668877c5d6cdbe82161f9cd7eaa2d68597140e39fff8b9cd67424e5").into(), + hex!("a5923930d34526d70e083f4633de4766f04df901ca3adba4a462d03423b609d9813b78a2fcaa2d770a5abc27c260c39e").into(), + hex!("a6ec439bff50a4bef8d0cd47c52e92cf00846ca3fe97cf88e5b6d7800ea22d0ebcac49d9ecd123d4c156642f8bb4389f").into(), + hex!("97b7d3fbd11886976291a24e9e7d6f4974345e06024121eaf57097057c103b6f548d1f523416cd6b465e8109aa0be911").into(), + hex!("b45f04e0d4c17df2815a6fea2b04fd7cc2cbb8e6789084e224db3c1d00db1c6f1d325a63df25ee4a9a992553dab420b2").into(), + hex!("a91dc563b48b4cc210119ff55bec2957e8be50aa25928147d0434a9ea4088e98ba1f17c2050e713d2891a3c741ea6c6c").into(), + hex!("a12256c39f3b17c0540d2d3442b732b2485ae9da240a1ef47782549dc7e84f7a9c9240ff59a73fad228a3c01fe953169").into(), + hex!("aebc98b50533d844fe3149735750c10eab861e765aa7820e3d54fb66089ce15409206bb58d3aae5ef22a29ac5207c702").into(), + hex!("b023d0e4fc2eba4c60cedae9d2ffa79cdcd5c79279fa41baa94f536c17d746a6aa76e8fe203fb1678da126c4343eed8b").into(), + hex!("a673262367bde5d1775961c7d9aa26d9859c59600536130f9adc8f99f81f0106d2eab2c5ac3912476affcab3821fecdd").into(), + hex!("8008298954370c0c3026fd71fb48fc619caa394c9ea17273284a903b07de1d385336fad69c8ab6fe6692774d5fabcfc1").into(), + hex!("a98a824454bc0fba41a6543ba11ec6879c979e97c87e3f7f3a228ba995e33bcae9378740f925800b81d3627f2af36c51").into(), + hex!("a29c667540db41eb7ce06d74ad91c7ddeb3e1f019a028b8ed4ee705d8d079d6f7d36f1b64fd4b9b807f0dc1ab3d65d2d").into(), + hex!("86c32be8a7155f26696c1e541096ab43b3836315490a4bdad867b973ebfa8ac414e4074819b9639348a048bd6fc4bdee").into(), + hex!("a0de748319bb0c53c03f172c9aebc4f7538dfce6ffdd362d24ffdd111448277a8a705832e94f65c61aa635a5f40b6f0b").into(), + hex!("970de7621ef90cb3921f747ce1cf9d389f322cebc286c339395714a6d40167fe26e04e1fc3116d3b7bea1c99bfedf0fb").into(), + hex!("841ae6b0bdd22718fd917ca4a871d39cf6811b9a460b99422fb324235b2c51b460e48159d7e1bc1778de3513b7ab1f29").into(), + hex!("86764a78587892407b311892c4c4e70890bd757d3f72a832257f411646e9298eb4f042ec1c929cc6ffcf539dda90fe7e").into(), + hex!("b33008c5025d35243e91b6749e7d7934bd8334e5f58e88db907715435a28f02d018b09127fb0302d1ee68d7f97391040").into(), + hex!("b5ea29ebc8107525894d9872ac88b4c9662fd18d87316565a8b013529b478f17f6c1c0ddf6482db6ede54d27c0e80782").into(), + hex!("9576def9e5dd8673d3dec2090536672d44368b78b2c2f70dd9e36a9d0e5889cd9e2a47b77ebda94388ac75679257b829").into(), + hex!("823dd44b4635bf095786e47f836c4af02d4dea848bc1cd823876266341d56496e09bec8612413c302fb34c3383a55e0e").into(), + hex!("b6a32b13f8b1339b0591b9db343696aab77a0c2ff181d2069ff39581da8904499d619c26e6e44209b7791c3aa2fb7aa2").into(), + hex!("b9b3262a97049e236e29afc4a7c6d2e2253f437e013b90bd0882d35d46d7a67e1344b3dc822fcae27717a9887d842a81").into(), + hex!("8a94e08cda133175049eb2cddae936b16c477db54a749b8d3378233034f56fdea99520755ec8eb355b738af61138d9a2").into(), + hex!("a55933f63c4cce4d99d8df4fe6dc9d9a3054379ed2560bb2a9147f9e456925ab29f7b1f14321ab2501d67dd759f5ef36").into(), + hex!("8b5a713fb50a2aa0671f1405876b0a829a5c1f7c9915906bcad26de7e2011f8eb2e2b7a1cbf94c19685edf8b364f242d").into(), + hex!("97cd2419d4aaabe457b1efa6d2e557a0c4d8725e57e33018626fb31395ed78555b79cf90da49975360a497049004b20c").into(), + hex!("90d640d0c949a73543b476e0b634d442c0bbe0f4d3f1f4f99d19fd8d37d3b99c4bb3c5c08ee77c24f0af6dc7c29ce658").into(), + hex!("8f65263c9ac0026d9a536360c8b01b40243cc27a7896fada367372ba82ffcf2c758bba9f92c96fdd66956b25d7162903").into(), + hex!("ac50c7c4b3201f2c98e812e83452ded2e2dfbeb4c1910b873978c4b5b443b34dde042b13cb4943759cefe1a546bd8197").into(), + hex!("a07a157ebb964282729ab09205f5a2df132b5a0103233dca67dd55215b84e66423c576c6e0b055bf85c6a27025fe1aba").into(), + hex!("b89462ff76b35af9a1f23bbeb7f3b6e2f72f4d96aaa30fcce820573654895c76d813f27dea9251c96ea9e3f1726da99e").into(), + hex!("a81866a83434f91d464a93a50c4906224f3108777f731a1d363d16f769795be8ffd23c25f0e051d8c43a55b665c15bda").into(), + hex!("a974e15a6315d56d612f5e8d70171b01baba976ae1602265cdb74e1f5cf3a48c94e8b90bf1d343f3f56f8f9ece604ecb").into(), + hex!("97ad199e4ba05c97c5aa90cc6e63ed86f78fbea35b9f56f5107df643efe40647d1ac7e50cf6e1de8b4ede8e2225e090c").into(), + hex!("a67c3822c0e5902355fe053c3c41765146018dd90de0df89c6ae230826056e91cad2a28f06859b8ea899486ddbdbe6fa").into(), + hex!("a5dd8329edde8fdb8f82fb71637c7a4d1e13c51ab2b24cc98b80952575e821fe31c3e7de5a566f8a98e0243cebc6d1d5").into(), + hex!("b7070e8349dbd4359414c0d909de10a472c4a7fc4827804b3e1980b1cd8f468d21d4c6163c234c05f05534e423f6199b").into(), + hex!("a1acc31ccbb89ba6ba5be3ffc26ef83a28c3b0f76be71f87ef618f9f8171c8afdbc5f05c521c67171ea9fde1bad7a9dc").into(), + hex!("a86d620245d119b34f55c6145a65937e5ed5281803675e120d8c2de05eabc08f85eb8d8c877aebf535663518c4fe2f88").into(), + hex!("885317798c5b49430d50d5c5eb527540b0b704794d88d510b2511e9fea2de299d8f2cf2ae2e96196a9c0925ab1f30da1").into(), + hex!("8a5c37ad34f1867fc9fdd3303ffb86fb0d98ce27fd6739078949c070b44081349368302d7c910fd3d886165b5fbd3304").into(), + hex!("a1935e801664a3f51c22e616d25fbecb3f3be76b936e4d2dc28a4bbe79d002ddf01b94cdf9e18d07f2e2c4bccdfd76a9").into(), + hex!("b3d13a611ada38fb6c802e6f7e09d03703e9e7bf82424ded2774d6493dc0c7d8965e7839ff4d3f9fe00a1e8fb20bf13c").into(), + hex!("84f6edb320cf92e147ba3ba8b2470c57dd107b80ea8083219d3c91b77fba7b20b6774ae7ace26924c02c9a2247842bcc").into(), + hex!("86546f16627c3b875790b604aeb19c925f6d32db27c2ebf405df0324095fab1f8b748fd94b6c2b5a2dfeaf748a565b3f").into(), + hex!("b95563cebc351d8237e5f9b8ab984976e84ebf7e16c62102629cbda06c86b013c6a973d007d72a870883408b8343fd1f").into(), + hex!("8d5eff4d3238446be0f805b8f387fd57f298dfcadba88a0d87234bbede46d0ef833beb0f2ede7a51fa84a808393757b4").into(), + hex!("862179dd50e7a0fa7906248c0f3671d8d3b25504e30276da74a36edfebc8c92a04abc9dc3cba8c1b34005beb06cceca3").into(), + hex!("b12bf5775e3f9ae29da6b127ffe2892d5dec12f9c1bc21426c225220cb0d5fca5c6376afb5bf5f112f79c6563694007d").into(), + hex!("957cd40aa0864b86bc64420a184988be4489c0f0a3363f39571e71a7443ac6815a1b8ce4862c736be8441108dd78101b").into(), + hex!("8f31cdb655b66e1f8ad877639f71524aa78c09acc24aa493bd6f6be383c295f51a6e70f2573081cf87cd41ef55f5428d").into(), + hex!("a8f304c1f2faa78683d409dbb0c11e26583fdfe845f2557e378c56acc9baaa88cce25442733edcdd0da70f3e5557e53b").into(), + ], + aggregate_pubkey: hex!("870e9dfe2c909b7116a9a4180da4fb6ac4865f9304adc4c36dde6f82338c43352b58dfb494e6095bfade1dbf86e7f939").into(), + }, + next_sync_committee_branch: vec![ + hex!("d138fae7ec85d4d5ebd8d7375b3f39f4bf0d05439e6920a44bcc977e62ee0dfa").into(), + hex!("a6baa91932e6f9d9fec678e9fd75a140c8e74bd87f11d37093839826b95ceeec").into(), + hex!("926c0348ccc4c44119ca84e50911ac22078ab704b0784ebc593155da5c5adb53").into(), + hex!("c4a04575645ebf0cf5b3317a092e595adf49dd93669424c2a5efef700ed082a1").into(), + hex!("81a062566009887529ffc6350f713cd2aa30460c13173fe9ffcdbde71fd69f8b").into(), + ], + }), + finalized_header: BeaconHeader{ + slot: 5808479, + proposer_index: 218610, + parent_root: hex!("ac0c3b35e7e21d11d0563f98fb16bbfb0460aef2ee5fe39ea209aed66694601e").into(), + state_root: hex!("c66e3a4f1718ce82f35c898e8df8080c540aca493a535a2f6170a13b550faef3").into(), + body_root: hex!("207806f82ac8c5bdb6793dc61f31ce91dd06a7fe3a143d29b6579975c64d1d9c").into(), + }, + finality_branch: vec![ + hex!("0bc5020000000000000000000000000000000000000000000000000000000000").into(), + hex!("8c04962a994aadff4d3042da73e167e666323757db5b0234a497c7ddba058ded").into(), + hex!("95901d6dae3edaab0f29f2c6155edbc4eb3980b6816339a464fb51b91fafdb7a").into(), + hex!("926c0348ccc4c44119ca84e50911ac22078ab704b0784ebc593155da5c5adb53").into(), + hex!("c4a04575645ebf0cf5b3317a092e595adf49dd93669424c2a5efef700ed082a1").into(), + hex!("81a062566009887529ffc6350f713cd2aa30460c13173fe9ffcdbde71fd69f8b").into(), + ], + block_roots_root: hex!("93a5736680a9dfe23df1f8a6098c0671c583dae469847e25da3532b3649ae11b").into(), + block_roots_branch: vec![ + hex!("31a647639bd26edd8e3976b4475933d18d7d238210881f57570b7b4030133da0").into(), + hex!("0a3392c5febec2f099f93c5465c68f4f1630927d0326ad84c8d0b318364dcd82").into(), + hex!("986071ec073d43597d67a6595f7f6fc807ef1042c6821fda41ff80aa2717536f").into(), + hex!("732f545955de627e65c46201f053569dceab609948147690136bc64e060f38b4").into(), + hex!("2e7c74db495877af1e95da27113e89757ea475e8d672d319e655810ec64d4ba2").into(), + ], + }) +} + +pub fn make_finalized_header_update() -> Box { + Box::new(Update { + attested_header: BeaconHeader { + slot: 5809441, + proposer_index: 169069, + parent_root: hex!("a4d2fbf3ee62f32589738f386559a1e2358f4f54aff5f7eaea61144d3d9c00d1").into(), + state_root: hex!("4aad4183bc21fc96c90f8e043049f8c1d5ed205c6880c89cd99f2e080ef85138").into(), + body_root: hex!("406c96c6adad01df901df3625cbd622f1d541249b05c768ccc4db5643d973141").into(), + }, + sync_aggregate: SyncAggregate{ + sync_committee_bits: hex!("7fabbff6fcdefbebaefffff9e37dfffebff57f7bf3e3efbcfef1f7f987551dd176f3b3ff7bfa3fedff5fdf7f7afff5ff777bef5f9f7fe65f97fffe7dfdfffbdd"), + sync_committee_signature: hex!("84dc756c452ec9a3ba01cc98d03cf5471b871e9f3f77ddfe72ddf6d5d318ec3de9e5c1508e47ed362300cd45a144655a076d50073c24a67591b0454d2a4632bc01e97eab80f937a8288131a31ab76f400ba9c26a19df176c7e67b724f70407c3").into(), + }, + signature_slot: 5809445, + next_sync_committee_update: None, + finalized_header: BeaconHeader { + slot: 5809375, + proposer_index: 170923, + parent_root: hex!("87fed31787712fa6e802b9f296c1eb0b0ac5bc77f6945d4478c4d25bd7160d1a").into(), + state_root: hex!("246ac89e1854bada03be1da64081954e008238de219088609ddf45efc8000346").into(), + body_root: hex!("ffe0fdbdc2bf57bebdd084fce3820a13801095d236a2fb8f3a64c9d7cf94f8b9").into(), + }, + finality_branch: vec![ + hex!("27c5020000000000000000000000000000000000000000000000000000000000").into(), + hex!("8c04962a994aadff4d3042da73e167e666323757db5b0234a497c7ddba058ded").into(), + hex!("95901d6dae3edaab0f29f2c6155edbc4eb3980b6816339a464fb51b91fafdb7a").into(), + hex!("34e68ed57efdf18c5d2f455e77fa8b2a5be95bb827bdf7f7f6648103688d84b7").into(), + hex!("fc1d45f882aa66020a92c55da663ab9758581a020eb7336173fe84ef861bbdf9").into(), + hex!("7d1745c42ec44d4b2493a55dafdb770f6d38eb4a7ad68ae0264949cb7432e4a7").into(), + ], + block_roots_root: hex!("9e5aeee5467301f3a44d1ab664cebd198519423e73e2118ad046d9bae217f497").into(), + block_roots_branch: vec![ + hex!("ef671e41918c36e23a3673407050b420366022886dcce1b707622de97a695121").into(), + hex!("707cb79caeaf310c10ce1c177312e48b2331164c8327d2635203148c4d974f09").into(), + hex!("1fd802c27384482fdaacfa7406072f6f96ff5428f003af748068d1965cc36981").into(), + hex!("8a31cc13bddabda4f79d948e5e3d70806f638b61d89c87b40aa7131af43c18a8").into(), + hex!("70eb43218a3a6f619f1d0dc7f173fc9c3323fa7e3824ae6cd79af2f7d19634ad").into(), + ] + }) +} + +pub fn make_execution_header_update() -> Box { + Box::new(ExecutionHeaderUpdate { + header: BeaconHeader { + slot: 5809374, + proposer_index: 130336, + parent_root: hex!("2bb54c61560a80d1cfb0528e8ea207dfb9d55ab49238523e21609a9ee3b8a9b5").into(), + state_root: hex!("0116ea76f5c36373dfc8e039811eba86c8e8e16cfe9f0614376559b6585741a7").into(), + body_root: hex!("9a10b47e30bc11fc2ee1e21943a8382d727444f646f09664192236458b555ffe").into(), + }, + ancestry_proof: Some(AncestryProof { + header_branch: vec![ + hex!("eca009f3262f75b055e6c919e2c0a2c017f017e581a825a2618a2a76926a264e").into(), + hex!("a647371a5590630186dd47b9b8571f27e39a77b4aac1f763fabefe104bf94985").into(), + hex!("9a414690540e4c87ff5171b619b3ab6ff1115c21f247196989f5a0a9085b59a1").into(), + hex!("1bf7ae16fcde0833c6e97a83b72aef31a0b5ca055b87f86602b9b4aa193f557c").into(), + hex!("588d993d05b59bf3352f0f5ebb4cd3ec97ca3e41800da675996741e8fca374c0").into(), + hex!("fdfc6280944bb0a18c9cd0afa9f4a255719a4650233f19de478399276f198c92").into(), + hex!("1d96235b47c604f029b9ab7eb913b13b3c0c2df7f79e3301341b1ec38ea44e4c").into(), + hex!("3ee3af17ce8f5a4946d30b6bce7d6e7580b3981cd2af92246401e2326224f6d1").into(), + hex!("b53b5450e070adf02f4bb9d7c65dd131d07ae2218340eec95ac8aa5e5cdd82aa").into(), + hex!("4d7c09715a1f25574afa1dc3dd7bb44e4c1a723b9360c893b8510f675f85227a").into(), + hex!("38c159fc38dedc1e4f399a3f773ab4376fc40b126634b40d172d5daa6602cf94").into(), + hex!("9faac6fa44ed19fcf530f77b7090dd50dd17aeedabe763931ab7567276025a75").into(), + hex!("c6549c1b0f0027ac373164437e7010b955fbae1a0e78485408ec33ca906beb2d").into(), + ], + finalized_block_root: hex!("f6e721e4e65d9565091a557705285ec6db0a3a3072317317719ec8ad563859a3").into(), + }), + execution_header: ExecutionPayloadHeader { + parent_hash: hex!("6d51d7c94763813ffefa234097a51c6fd7009424d2991695f7bd6203157c86f9").into(), + fee_recipient: hex!("000095e79eac4d76aab57cb2c1f091d553b36ca0").into(), + state_root: hex!("fe9f753520a7b5c0263bbf4fdba728f69e9cf861ce1883aa13de5da30ff75d74").into(), + receipts_root: hex!("cf6ab47d8fc336155b18abfa2d965aae57d9d35a2fcf5cfc992b8dcd136958cb").into(), + logs_bloom: hex!("8427414fce71480d7e70cdbac68dd6f77608c05cf349c34c87ad3256e8dde9e3f9c52131945876c03b6e83ea5970536428283a180eb40efcc5fd834ce424f0dbf622dbba6cfda7945cc1f93a1b6e7ae448c598b4f45f7cfa933fe9808d835cb86e8a38261a031448e262f8e4f2dc4c3254c460e5faae4b518438c1330012154a1ba33ab7d85c8acaa9c47dc582fd003a771c9b09aa16c34d4f0c01fbb3f8c0a28e11d2eafb4e73b75a18e182eac7c021706832a9a785836d31f651efacf88a329334e5b3def3bf1871573dc3553f415f298a9457f7837a31302937a4178be1339cdbb83af329ae7e88d8ab6cba62f018be139896ecbc7ac11ef24b0b4ae343e9").into(), + prev_randao: hex!("5a76eff974d26bf74dc3003fac473ab4abc541be26bd61f124a1818a70ea0b3e").into(), + block_number: 9143323, + gas_limit: 30000000, + gas_used: 28165724, + timestamp: 1686220488, + extra_data: hex!("").into(), + base_fee_per_gas: U256::from(2267_u64), + block_hash: hex!("e4a67cdb1512f29ad9b331e7a37cf8e376222eafa58e72cee7771ad582cc0610").into(), + transactions_root: hex!("bd7eaeb676c14c37bbf0b6f3db2ce021a04a41dbf002f6c7df3bb61639ac7287").into(), + withdrawals_root: hex!("8647d3ecaaf62e1d087c5ab54a23f1d64f477b7ddd16fff458847181d89fc432").into(), + }, + execution_branch: vec![ + hex!("795608ac1294bcc663127b8428513ba4a5ffe952ff72f8322dca23628f13d716").into(), + hex!("336488033fe5f3ef4ccc12af07b9370b92e553e35ecb4a337a1b1c0e4afe1e0e").into(), + hex!("db56114e00fdd4c1f85c892bf35ac9a89289aaecb1ebd0a96cde606a748b5d71").into(), + hex!("2a8f5c65655edeb2800f248f2e14044fc651061d0c00c8e8b627cb21ba421fb4").into(), + ], + }) +} diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/benchmarking/mod.rs b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/benchmarking/mod.rs new file mode 100644 index 000000000000..cba22fc86c99 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/benchmarking/mod.rs @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use super::*; + +mod fixtures; +mod util; + +use crate::Pallet as EthereumBeaconClient; +use frame_benchmarking::v2::*; +use frame_system::RawOrigin; + +use fixtures::{ + make_checkpoint, make_execution_header_update, make_finalized_header_update, + make_sync_committee_update, +}; + +use primitives::{ + fast_aggregate_verify, prepare_aggregate_pubkey, prepare_aggregate_signature, + verify_merkle_branch, +}; +use util::*; + +#[benchmarks] +mod benchmarks { + use super::*; + + #[benchmark] + fn force_checkpoint() -> Result<(), BenchmarkError> { + let checkpoint_update = make_checkpoint(); + let block_root: H256 = checkpoint_update.header.hash_tree_root().unwrap(); + + #[extrinsic_call] + _(RawOrigin::Root, Box::new(*checkpoint_update)); + + assert!(>::get() == block_root); + assert!(>::get(block_root).is_some()); + + Ok(()) + } + + #[benchmark] + fn submit() -> Result<(), BenchmarkError> { + let caller: T::AccountId = whitelisted_caller(); + let checkpoint_update = make_checkpoint(); + let finalized_header_update = make_finalized_header_update(); + let block_root: H256 = finalized_header_update.finalized_header.hash_tree_root().unwrap(); + EthereumBeaconClient::::process_checkpoint_update(&checkpoint_update)?; + + #[extrinsic_call] + submit(RawOrigin::Signed(caller.clone()), Box::new(*finalized_header_update)); + + assert!(>::get() == block_root); + assert!(>::get(block_root).is_some()); + + Ok(()) + } + + #[benchmark] + fn submit_with_sync_committee() -> Result<(), BenchmarkError> { + let caller: T::AccountId = whitelisted_caller(); + let checkpoint_update = make_checkpoint(); + let sync_committee_update = make_sync_committee_update(); + EthereumBeaconClient::::process_checkpoint_update(&checkpoint_update)?; + + #[extrinsic_call] + submit(RawOrigin::Signed(caller.clone()), Box::new(*sync_committee_update)); + + assert!(>::exists()); + + Ok(()) + } + + #[benchmark] + fn submit_execution_header() -> Result<(), BenchmarkError> { + let caller: T::AccountId = whitelisted_caller(); + let checkpoint_update = make_checkpoint(); + let finalized_header_update = make_finalized_header_update(); + let execution_header_update = make_execution_header_update(); + let execution_header_hash = execution_header_update.execution_header.block_hash; + EthereumBeaconClient::::process_checkpoint_update(&checkpoint_update)?; + EthereumBeaconClient::::process_update(&finalized_header_update)?; + + #[extrinsic_call] + _(RawOrigin::Signed(caller.clone()), Box::new(*execution_header_update)); + + assert!(>::contains_key(execution_header_hash)); + + Ok(()) + } + + #[benchmark(extra)] + fn bls_fast_aggregate_verify_pre_aggregated() -> Result<(), BenchmarkError> { + EthereumBeaconClient::::process_checkpoint_update(&make_checkpoint())?; + let update = make_sync_committee_update(); + let participant_pubkeys = participant_pubkeys::(&update)?; + let signing_root = signing_root::(&update)?; + let agg_sig = + prepare_aggregate_signature(&update.sync_aggregate.sync_committee_signature).unwrap(); + let agg_pub_key = prepare_aggregate_pubkey(&participant_pubkeys).unwrap(); + + #[block] + { + agg_sig.fast_aggregate_verify_pre_aggregated(signing_root.as_bytes(), &agg_pub_key); + } + + Ok(()) + } + + #[benchmark(extra)] + fn bls_fast_aggregate_verify() -> Result<(), BenchmarkError> { + EthereumBeaconClient::::process_checkpoint_update(&make_checkpoint())?; + let update = make_sync_committee_update(); + let current_sync_committee = >::get(); + let absent_pubkeys = absent_pubkeys::(&update)?; + let signing_root = signing_root::(&update)?; + + #[block] + { + fast_aggregate_verify( + ¤t_sync_committee.aggregate_pubkey, + &absent_pubkeys, + signing_root, + &update.sync_aggregate.sync_committee_signature, + ) + .unwrap(); + } + + Ok(()) + } + + #[benchmark(extra)] + fn verify_merkle_proof() -> Result<(), BenchmarkError> { + EthereumBeaconClient::::process_checkpoint_update(&make_checkpoint())?; + let update = make_sync_committee_update(); + let block_root: H256 = update.finalized_header.hash_tree_root().unwrap(); + + #[block] + { + verify_merkle_branch( + block_root, + &update.finality_branch, + config::FINALIZED_ROOT_SUBTREE_INDEX, + config::FINALIZED_ROOT_DEPTH, + update.attested_header.state_root, + ); + } + + Ok(()) + } + + impl_benchmark_test_suite!( + EthereumBeaconClient, + crate::mock::mainnet::new_tester(), + crate::mock::mainnet::Test + ); +} diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/benchmarking/util.rs b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/benchmarking/util.rs new file mode 100644 index 000000000000..7e5ded6e1f0d --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/benchmarking/util.rs @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use crate::{ + decompress_sync_committee_bits, Config, CurrentSyncCommittee, Pallet as EthereumBeaconClient, + Update, ValidatorsRoot, Vec, +}; +use primitives::PublicKeyPrepared; +use sp_core::H256; + +pub fn participant_pubkeys( + update: &Update, +) -> Result, &'static str> { + let sync_committee_bits = + decompress_sync_committee_bits(update.sync_aggregate.sync_committee_bits); + let current_sync_committee = >::get(); + let pubkeys = EthereumBeaconClient::::find_pubkeys( + &sync_committee_bits, + (*current_sync_committee.pubkeys).as_ref(), + true, + ); + Ok(pubkeys) +} + +pub fn absent_pubkeys(update: &Update) -> Result, &'static str> { + let sync_committee_bits = + decompress_sync_committee_bits(update.sync_aggregate.sync_committee_bits); + let current_sync_committee = >::get(); + let pubkeys = EthereumBeaconClient::::find_pubkeys( + &sync_committee_bits, + (*current_sync_committee.pubkeys).as_ref(), + false, + ); + Ok(pubkeys) +} + +pub fn signing_root(update: &Update) -> Result { + let validators_root = >::get(); + let signing_root = EthereumBeaconClient::::signing_root( + &update.attested_header, + validators_root, + update.signature_slot, + )?; + Ok(signing_root) +} diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/config/mainnet.rs b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/config/mainnet.rs new file mode 100644 index 000000000000..3d22ad82cec0 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/config/mainnet.rs @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +pub const SLOTS_PER_EPOCH: usize = 32; +pub const SECONDS_PER_SLOT: usize = 12; +pub const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: usize = 256; +pub const SYNC_COMMITTEE_SIZE: usize = 512; +pub const SYNC_COMMITTEE_BITS_SIZE: usize = SYNC_COMMITTEE_SIZE / 8; +pub const SLOTS_PER_HISTORICAL_ROOT: usize = 8192; +pub const IS_MINIMAL: bool = false; +pub const BLOCK_ROOT_AT_INDEX_DEPTH: usize = 13; diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/config/minimal.rs b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/config/minimal.rs new file mode 100644 index 000000000000..affa86db9761 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/config/minimal.rs @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +pub const SLOTS_PER_EPOCH: usize = 8; +pub const SECONDS_PER_SLOT: usize = 6; +pub const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: usize = 8; +pub const SYNC_COMMITTEE_SIZE: usize = 32; +pub const SYNC_COMMITTEE_BITS_SIZE: usize = SYNC_COMMITTEE_SIZE / 8; +pub const SLOTS_PER_HISTORICAL_ROOT: usize = 64; +pub const IS_MINIMAL: bool = true; +pub const BLOCK_ROOT_AT_INDEX_DEPTH: usize = 6; diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/config/mod.rs b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/config/mod.rs new file mode 100644 index 000000000000..6b959ebfec94 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/config/mod.rs @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use primitives::merkle_proof::{generalized_index_length, subtree_index}; +use static_assertions::const_assert; + +pub mod mainnet; +pub mod minimal; + +#[cfg(not(feature = "beacon-spec-mainnet"))] +pub use minimal::*; + +#[cfg(feature = "beacon-spec-mainnet")] +pub use mainnet::*; + +// Generalized Indices + +// get_generalized_index(BeaconState, 'block_roots') +pub const BLOCK_ROOTS_INDEX: usize = 37; +pub const BLOCK_ROOTS_SUBTREE_INDEX: usize = subtree_index(BLOCK_ROOTS_INDEX); +pub const BLOCK_ROOTS_DEPTH: usize = generalized_index_length(BLOCK_ROOTS_INDEX); + +// get_generalized_index(BeaconState, 'finalized_checkpoint', 'root') +pub const FINALIZED_ROOT_INDEX: usize = 105; +pub const FINALIZED_ROOT_SUBTREE_INDEX: usize = subtree_index(FINALIZED_ROOT_INDEX); +pub const FINALIZED_ROOT_DEPTH: usize = generalized_index_length(FINALIZED_ROOT_INDEX); + +// get_generalized_index(BeaconState, 'current_sync_committee') +pub const CURRENT_SYNC_COMMITTEE_INDEX: usize = 54; +pub const CURRENT_SYNC_COMMITTEE_SUBTREE_INDEX: usize = subtree_index(CURRENT_SYNC_COMMITTEE_INDEX); +pub const CURRENT_SYNC_COMMITTEE_DEPTH: usize = + generalized_index_length(CURRENT_SYNC_COMMITTEE_INDEX); + +// get_generalized_index(BeaconState, 'next_sync_committee') +pub const NEXT_SYNC_COMMITTEE_INDEX: usize = 55; +pub const NEXT_SYNC_COMMITTEE_SUBTREE_INDEX: usize = subtree_index(NEXT_SYNC_COMMITTEE_INDEX); +pub const NEXT_SYNC_COMMITTEE_DEPTH: usize = generalized_index_length(NEXT_SYNC_COMMITTEE_INDEX); + +// get_generalized_index(BeaconBlockBody, 'execution_payload') +pub const EXECUTION_HEADER_INDEX: usize = 25; +pub const EXECUTION_HEADER_SUBTREE_INDEX: usize = subtree_index(EXECUTION_HEADER_INDEX); +pub const EXECUTION_HEADER_DEPTH: usize = generalized_index_length(EXECUTION_HEADER_INDEX); + +pub const MAX_EXTRA_DATA_BYTES: usize = 32; +pub const MAX_LOGS_BLOOM_SIZE: usize = 256; +pub const MAX_FEE_RECIPIENT_SIZE: usize = 20; + +pub const MAX_BRANCH_PROOF_SIZE: usize = 20; + +/// DomainType('0x07000000') +/// +pub const DOMAIN_SYNC_COMMITTEE: [u8; 4] = [7, 0, 0, 0]; + +pub const PUBKEY_SIZE: usize = 48; +pub const SIGNATURE_SIZE: usize = 96; + +const_assert!(SYNC_COMMITTEE_BITS_SIZE == SYNC_COMMITTEE_SIZE / 8); diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/functions.rs b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/functions.rs new file mode 100644 index 000000000000..751e63c7f86a --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/functions.rs @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use crate::config::{ + EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SLOTS_PER_EPOCH, SYNC_COMMITTEE_BITS_SIZE, + SYNC_COMMITTEE_SIZE, +}; + +/// Decompress packed bitvector into byte vector according to SSZ deserialization rules. Each byte +/// in the decompressed vector is either 0 or 1. +pub fn decompress_sync_committee_bits( + input: [u8; SYNC_COMMITTEE_BITS_SIZE], +) -> [u8; SYNC_COMMITTEE_SIZE] { + primitives::decompress_sync_committee_bits::( + input, + ) +} + +/// Compute the sync committee period in which a slot is contained. +pub fn compute_period(slot: u64) -> u64 { + slot / SLOTS_PER_EPOCH as u64 / EPOCHS_PER_SYNC_COMMITTEE_PERIOD as u64 +} + +/// Compute epoch in which a slot is contained. +pub fn compute_epoch(slot: u64, slots_per_epoch: u64) -> u64 { + slot / slots_per_epoch +} + +/// Sums the bit vector of sync committee participation. +pub fn sync_committee_sum(sync_committee_bits: &[u8]) -> u32 { + sync_committee_bits.iter().fold(0, |acc: u32, x| acc + *x as u32) +} diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/impls.rs b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/impls.rs new file mode 100644 index 000000000000..7e72b12631cc --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/impls.rs @@ -0,0 +1,93 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use super::*; + +use snowbridge_core::inbound::{ + VerificationError::{self, *}, + *, +}; +use snowbridge_ethereum::Receipt; + +impl Verifier for Pallet { + /// Verify a message by verifying the existence of the corresponding + /// Ethereum log in a block. Returns the log if successful. The execution header containing + /// the log should be in the beacon client storage, meaning it has been verified and is an + /// ancestor of a finalized beacon block. + fn verify(event_log: &Log, proof: &Proof) -> Result<(), VerificationError> { + log::info!( + target: "ethereum-beacon-client", + "💫 Verifying message with block hash {}", + proof.block_hash, + ); + + let header = >::get(proof.block_hash).ok_or(HeaderNotFound)?; + + let receipt = match Self::verify_receipt_inclusion(header.receipts_root, proof) { + Ok(receipt) => receipt, + Err(err) => { + log::error!( + target: "ethereum-beacon-client", + "💫 Verification of receipt inclusion failed for block {}: {:?}", + proof.block_hash, + err + ); + return Err(err) + }, + }; + + log::trace!( + target: "ethereum-beacon-client", + "💫 Verified receipt inclusion for transaction at index {} in block {}", + proof.tx_index, proof.block_hash, + ); + + event_log.validate().map_err(|_| InvalidLog)?; + + // Convert snowbridge_core::inbound::Log to snowbridge_ethereum::Log. + let event_log = snowbridge_ethereum::Log { + address: event_log.address, + topics: event_log.topics.clone(), + data: event_log.data.clone(), + }; + + if !receipt.contains_log(&event_log) { + log::error!( + target: "ethereum-beacon-client", + "💫 Event log not found in receipt for transaction at index {} in block {}", + proof.tx_index, proof.block_hash, + ); + return Err(LogNotFound) + } + + log::info!( + target: "ethereum-beacon-client", + "💫 Receipt verification successful for {}", + proof.block_hash, + ); + + Ok(()) + } +} + +impl Pallet { + /// Verifies that the receipt encoded in `proof.data` is included in the block given by + /// `proof.block_hash`. + pub fn verify_receipt_inclusion( + receipts_root: H256, + proof: &Proof, + ) -> Result { + let result = verify_receipt_proof(receipts_root, &proof.data.1).ok_or(InvalidProof)?; + + match result { + Ok(receipt) => Ok(receipt), + Err(err) => { + log::trace!( + target: "ethereum-beacon-client", + "💫 Failed to decode transaction receipt: {}", + err + ); + Err(InvalidProof) + }, + } + } +} diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/lib.rs b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/lib.rs new file mode 100644 index 000000000000..fdda200251ac --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/lib.rs @@ -0,0 +1,841 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! Ethereum Beacon Client +//! +//! A light client that verifies consensus updates signed by the sync committee of the beacon chain. +//! +//! # Extrinsics +//! +//! ## Governance +//! +//! * [`Call::force_checkpoint`]: Set the initial trusted consensus checkpoint. +//! * [`Call::set_operating_mode`]: Set the operating mode of the pallet. Can be used to disable +//! processing of conensus updates. +//! +//! ## Consensus Updates +//! +//! * [`Call::submit`]: Submit a finalized beacon header with an optional sync committee update +//! * [`Call::submit_execution_header`]: Submit an execution header together with an ancestry proof +//! that can be verified against an already imported finalized beacon header. +#![cfg_attr(not(feature = "std"), no_std)] + +pub mod config; +pub mod functions; +pub mod impls; +pub mod types; +pub mod weights; + +#[cfg(any(test, feature = "fuzzing"))] +pub mod mock; + +#[cfg(all(test, not(feature = "beacon-spec-mainnet")))] +mod tests; + +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; + +use frame_support::{ + dispatch::DispatchResult, pallet_prelude::OptionQuery, traits::Get, transactional, +}; +use frame_system::ensure_signed; +use primitives::{ + fast_aggregate_verify, verify_merkle_branch, verify_receipt_proof, BeaconHeader, BlsError, + CompactBeaconState, CompactExecutionHeader, ExecutionHeaderState, ForkData, ForkVersion, + ForkVersions, PublicKeyPrepared, SigningData, +}; +use snowbridge_core::{BasicOperatingMode, RingBufferMap}; +use sp_core::H256; +use sp_std::prelude::*; +pub use weights::WeightInfo; + +use functions::{ + compute_epoch, compute_period, decompress_sync_committee_bits, sync_committee_sum, +}; +pub use types::ExecutionHeaderBuffer; +use types::{ + CheckpointUpdate, ExecutionHeaderUpdate, FinalizedBeaconStateBuffer, SyncCommitteePrepared, + Update, +}; + +pub use pallet::*; + +pub use config::SLOTS_PER_HISTORICAL_ROOT; + +pub const LOG_TARGET: &str = "ethereum-beacon-client"; + +#[frame_support::pallet] +pub mod pallet { + use super::*; + + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + + #[derive(scale_info::TypeInfo, codec::Encode, codec::Decode, codec::MaxEncodedLen)] + #[codec(mel_bound(T: Config))] + #[scale_info(skip_type_params(T))] + pub struct MaxFinalizedHeadersToKeep(PhantomData); + impl Get for MaxFinalizedHeadersToKeep { + fn get() -> u32 { + // Consider max latency allowed between LatestFinalizedState and LatestExecutionState is + // the total slots in one sync_committee_period so 1 should be fine we keep 2 periods + // here for redundancy. + const MAX_REDUNDANCY: u32 = 2; + config::EPOCHS_PER_SYNC_COMMITTEE_PERIOD as u32 * MAX_REDUNDANCY + } + } + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config { + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + #[pallet::constant] + type ForkVersions: Get; + /// Maximum number of execution headers to keep + #[pallet::constant] + type MaxExecutionHeadersToKeep: Get; + type WeightInfo: WeightInfo; + } + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + BeaconHeaderImported { + block_hash: H256, + slot: u64, + }, + ExecutionHeaderImported { + block_hash: H256, + block_number: u64, + }, + SyncCommitteeUpdated { + period: u64, + }, + /// Set OperatingMode + OperatingModeChanged { + mode: BasicOperatingMode, + }, + } + + #[pallet::error] + pub enum Error { + SkippedSyncCommitteePeriod, + /// Attested header is older than latest finalized header. + IrrelevantUpdate, + NotBootstrapped, + SyncCommitteeParticipantsNotSupermajority, + InvalidHeaderMerkleProof, + InvalidSyncCommitteeMerkleProof, + InvalidExecutionHeaderProof, + InvalidAncestryMerkleProof, + InvalidBlockRootsRootMerkleProof, + HeaderNotFinalized, + BlockBodyHashTreeRootFailed, + HeaderHashTreeRootFailed, + SyncCommitteeHashTreeRootFailed, + SigningRootHashTreeRootFailed, + ForkDataHashTreeRootFailed, + ExpectedFinalizedHeaderNotStored, + BLSPreparePublicKeysFailed, + BLSVerificationFailed(BlsError), + InvalidUpdateSlot, + /// The given update is not in the expected period, or the given next sync committee does + /// not match the next sync committee in storage. + InvalidSyncCommitteeUpdate, + ExecutionHeaderTooFarBehind, + ExecutionHeaderSkippedBlock, + Halted, + } + + /// Latest imported checkpoint root + #[pallet::storage] + #[pallet::getter(fn initial_checkpoint_root)] + pub(super) type InitialCheckpointRoot = StorageValue<_, H256, ValueQuery>; + + /// Latest imported finalized block root + #[pallet::storage] + #[pallet::getter(fn latest_finalized_block_root)] + pub(super) type LatestFinalizedBlockRoot = StorageValue<_, H256, ValueQuery>; + + /// Beacon state by finalized block root + #[pallet::storage] + #[pallet::getter(fn finalized_beacon_state)] + pub(super) type FinalizedBeaconState = + StorageMap<_, Identity, H256, CompactBeaconState, OptionQuery>; + + /// Finalized Headers: Current position in ring buffer + #[pallet::storage] + pub(crate) type FinalizedBeaconStateIndex = StorageValue<_, u32, ValueQuery>; + + /// Finalized Headers: Mapping of ring buffer index to a pruning candidate + #[pallet::storage] + pub(crate) type FinalizedBeaconStateMapping = + StorageMap<_, Identity, u32, H256, ValueQuery>; + + #[pallet::storage] + #[pallet::getter(fn validators_root)] + pub(super) type ValidatorsRoot = StorageValue<_, H256, ValueQuery>; + + /// Sync committee for current period + #[pallet::storage] + pub(super) type CurrentSyncCommittee = + StorageValue<_, SyncCommitteePrepared, ValueQuery>; + + /// Sync committee for next period + #[pallet::storage] + pub(super) type NextSyncCommittee = + StorageValue<_, SyncCommitteePrepared, ValueQuery>; + + /// Latest imported execution header + #[pallet::storage] + #[pallet::getter(fn latest_execution_state)] + pub(super) type LatestExecutionState = + StorageValue<_, ExecutionHeaderState, ValueQuery>; + + /// Execution Headers + #[pallet::storage] + pub type ExecutionHeaders = + StorageMap<_, Identity, H256, CompactExecutionHeader, OptionQuery>; + + /// Execution Headers: Current position in ring buffer + #[pallet::storage] + pub type ExecutionHeaderIndex = StorageValue<_, u32, ValueQuery>; + + /// Execution Headers: Mapping of ring buffer index to a pruning candidate + #[pallet::storage] + pub type ExecutionHeaderMapping = StorageMap<_, Identity, u32, H256, ValueQuery>; + + /// The current operating mode of the pallet. + #[pallet::storage] + #[pallet::getter(fn operating_mode)] + pub type OperatingMode = StorageValue<_, BasicOperatingMode, ValueQuery>; + + #[pallet::call] + impl Pallet { + #[pallet::call_index(0)] + #[pallet::weight(T::WeightInfo::force_checkpoint())] + #[transactional] + /// Used for pallet initialization and light client resetting. Needs to be called by + /// the root origin. + pub fn force_checkpoint( + origin: OriginFor, + update: Box, + ) -> DispatchResult { + ensure_root(origin)?; + Self::process_checkpoint_update(&update)?; + Ok(()) + } + + #[pallet::call_index(1)] + #[pallet::weight({ + match update.next_sync_committee_update { + None => T::WeightInfo::submit(), + Some(_) => T::WeightInfo::submit_with_sync_committee(), + } + })] + #[transactional] + /// Submits a new finalized beacon header update. The update may contain the next + /// sync committee. + pub fn submit(origin: OriginFor, update: Box) -> DispatchResult { + ensure_signed(origin)?; + ensure!(!Self::operating_mode().is_halted(), Error::::Halted); + Self::process_update(&update)?; + Ok(()) + } + + #[pallet::call_index(2)] + #[pallet::weight(T::WeightInfo::submit_execution_header())] + #[transactional] + /// Submits a new execution header update. The relevant related beacon header + /// is also included to prove the execution header, as well as ancestry proof data. + pub fn submit_execution_header( + origin: OriginFor, + update: Box, + ) -> DispatchResult { + ensure_signed(origin)?; + ensure!(!Self::operating_mode().is_halted(), Error::::Halted); + Self::process_execution_header_update(&update)?; + Ok(()) + } + + /// Halt or resume all pallet operations. May only be called by root. + #[pallet::call_index(3)] + #[pallet::weight((T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational))] + pub fn set_operating_mode( + origin: OriginFor, + mode: BasicOperatingMode, + ) -> DispatchResult { + ensure_root(origin)?; + OperatingMode::::set(mode); + Self::deposit_event(Event::OperatingModeChanged { mode }); + Ok(()) + } + } + + impl Pallet { + /// Forces a finalized beacon header checkpoint update. The current sync committee, + /// with a header attesting to the current sync committee, should be provided. + /// An `block_roots` proof should also be provided. This is used for ancestry proofs + /// for execution header updates. + pub(crate) fn process_checkpoint_update(update: &CheckpointUpdate) -> DispatchResult { + let sync_committee_root = update + .current_sync_committee + .hash_tree_root() + .map_err(|_| Error::::SyncCommitteeHashTreeRootFailed)?; + + // Verifies the sync committee in the Beacon state. + ensure!( + verify_merkle_branch( + sync_committee_root, + &update.current_sync_committee_branch, + config::CURRENT_SYNC_COMMITTEE_SUBTREE_INDEX, + config::CURRENT_SYNC_COMMITTEE_DEPTH, + update.header.state_root + ), + Error::::InvalidSyncCommitteeMerkleProof + ); + + let header_root: H256 = update + .header + .hash_tree_root() + .map_err(|_| Error::::HeaderHashTreeRootFailed)?; + + // This is used for ancestry proofs in ExecutionHeader updates. This verifies the + // BeaconState: the beacon state root is the tree root; the `block_roots` hash is the + // tree leaf. + ensure!( + verify_merkle_branch( + update.block_roots_root, + &update.block_roots_branch, + config::BLOCK_ROOTS_SUBTREE_INDEX, + config::BLOCK_ROOTS_DEPTH, + update.header.state_root + ), + Error::::InvalidBlockRootsRootMerkleProof + ); + + let sync_committee_prepared: SyncCommitteePrepared = (&update.current_sync_committee) + .try_into() + .map_err(|_| >::BLSPreparePublicKeysFailed)?; + >::set(sync_committee_prepared); + >::kill(); + InitialCheckpointRoot::::set(header_root); + >::kill(); + + Self::store_validators_root(update.validators_root); + Self::store_finalized_header(header_root, update.header, update.block_roots_root)?; + + Ok(()) + } + + pub(crate) fn process_update(update: &Update) -> DispatchResult { + Self::cross_check_execution_state()?; + Self::verify_update(update)?; + Self::apply_update(update)?; + Ok(()) + } + + /// Cross check to make sure that execution header import does not fall too far behind + /// finalised beacon header import. If that happens just return an error and pause + /// processing until execution header processing has caught up. + pub(crate) fn cross_check_execution_state() -> DispatchResult { + let latest_finalized_state = + FinalizedBeaconState::::get(LatestFinalizedBlockRoot::::get()) + .ok_or(Error::::NotBootstrapped)?; + let latest_execution_state = Self::latest_execution_state(); + // The execution header import should be at least within the slot range of a sync + // committee period. + let max_latency = config::EPOCHS_PER_SYNC_COMMITTEE_PERIOD * config::SLOTS_PER_EPOCH; + ensure!( + latest_execution_state.beacon_slot == 0 || + latest_finalized_state.slot < + latest_execution_state.beacon_slot + max_latency as u64, + Error::::ExecutionHeaderTooFarBehind + ); + Ok(()) + } + + /// References and strictly follows + /// Verifies that provided next sync committee is valid through a series of checks + /// (including checking that a sync committee period isn't skipped and that the header is + /// signed by the current sync committee. + fn verify_update(update: &Update) -> DispatchResult { + // Verify sync committee has sufficient participants. + let participation = + decompress_sync_committee_bits(update.sync_aggregate.sync_committee_bits); + Self::sync_committee_participation_is_supermajority(&participation)?; + + // Verify update does not skip a sync committee period. + ensure!( + update.signature_slot > update.attested_header.slot && + update.attested_header.slot >= update.finalized_header.slot, + Error::::InvalidUpdateSlot + ); + // Retrieve latest finalized state. + let latest_finalized_state = + FinalizedBeaconState::::get(LatestFinalizedBlockRoot::::get()) + .ok_or(Error::::NotBootstrapped)?; + let store_period = compute_period(latest_finalized_state.slot); + let signature_period = compute_period(update.signature_slot); + if >::exists() { + ensure!( + (store_period..=store_period + 1).contains(&signature_period), + Error::::SkippedSyncCommitteePeriod + ) + } else { + ensure!(signature_period == store_period, Error::::SkippedSyncCommitteePeriod) + } + + // Verify update is relevant. + let update_attested_period = compute_period(update.attested_header.slot); + let update_has_next_sync_committee = !>::exists() && + (update.next_sync_committee_update.is_some() && + update_attested_period == store_period); + ensure!( + update.attested_header.slot > latest_finalized_state.slot || + update_has_next_sync_committee, + Error::::IrrelevantUpdate + ); + + // Verify that the `finality_branch`, if present, confirms `finalized_header` to match + // the finalized checkpoint root saved in the state of `attested_header`. + let finalized_block_root: H256 = update + .finalized_header + .hash_tree_root() + .map_err(|_| Error::::HeaderHashTreeRootFailed)?; + ensure!( + verify_merkle_branch( + finalized_block_root, + &update.finality_branch, + config::FINALIZED_ROOT_SUBTREE_INDEX, + config::FINALIZED_ROOT_DEPTH, + update.attested_header.state_root + ), + Error::::InvalidHeaderMerkleProof + ); + + // Though following check does not belong to ALC spec we verify block_roots_root to + // match the finalized checkpoint root saved in the state of `finalized_header` so to + // cache it for later use in `verify_ancestry_proof`. + ensure!( + verify_merkle_branch( + update.block_roots_root, + &update.block_roots_branch, + config::BLOCK_ROOTS_SUBTREE_INDEX, + config::BLOCK_ROOTS_DEPTH, + update.finalized_header.state_root + ), + Error::::InvalidBlockRootsRootMerkleProof + ); + + // Verify that the `next_sync_committee`, if present, actually is the next sync + // committee saved in the state of the `attested_header`. + if let Some(next_sync_committee_update) = &update.next_sync_committee_update { + let sync_committee_root = next_sync_committee_update + .next_sync_committee + .hash_tree_root() + .map_err(|_| Error::::SyncCommitteeHashTreeRootFailed)?; + if update_attested_period == store_period && >::exists() { + let next_committee_root = >::get().root; + ensure!( + sync_committee_root == next_committee_root, + Error::::InvalidSyncCommitteeUpdate + ); + } + ensure!( + verify_merkle_branch( + sync_committee_root, + &next_sync_committee_update.next_sync_committee_branch, + config::NEXT_SYNC_COMMITTEE_SUBTREE_INDEX, + config::NEXT_SYNC_COMMITTEE_DEPTH, + update.attested_header.state_root + ), + Error::::InvalidSyncCommitteeMerkleProof + ); + } + + // Verify sync committee aggregate signature. + let sync_committee = if signature_period == store_period { + >::get() + } else { + >::get() + }; + let absent_pubkeys = + Self::find_pubkeys(&participation, (*sync_committee.pubkeys).as_ref(), false); + let signing_root = Self::signing_root( + &update.attested_header, + Self::validators_root(), + update.signature_slot, + )?; + // Improvement here per + // suggested start from the full set aggregate_pubkey then subtracting the absolute + // minority that did not participate. + fast_aggregate_verify( + &sync_committee.aggregate_pubkey, + &absent_pubkeys, + signing_root, + &update.sync_aggregate.sync_committee_signature, + ) + .map_err(|e| Error::::BLSVerificationFailed(e))?; + + Ok(()) + } + + /// Reference and strictly follows DispatchResult { + let latest_finalized_state = + FinalizedBeaconState::::get(LatestFinalizedBlockRoot::::get()) + .ok_or(Error::::NotBootstrapped)?; + if let Some(next_sync_committee_update) = &update.next_sync_committee_update { + let store_period = compute_period(latest_finalized_state.slot); + let update_finalized_period = compute_period(update.finalized_header.slot); + let sync_committee_prepared: SyncCommitteePrepared = (&next_sync_committee_update + .next_sync_committee) + .try_into() + .map_err(|_| >::BLSPreparePublicKeysFailed)?; + + if !>::exists() { + ensure!( + update_finalized_period == store_period, + >::InvalidSyncCommitteeUpdate + ); + >::set(sync_committee_prepared); + } else if update_finalized_period == store_period + 1 { + >::set(>::get()); + >::set(sync_committee_prepared); + } + log::info!( + target: LOG_TARGET, + "💫 SyncCommitteeUpdated at period {}.", + update_finalized_period + ); + Self::deposit_event(Event::SyncCommitteeUpdated { + period: update_finalized_period, + }); + }; + + if update.finalized_header.slot > latest_finalized_state.slot { + let finalized_block_root: H256 = update + .finalized_header + .hash_tree_root() + .map_err(|_| Error::::HeaderHashTreeRootFailed)?; + Self::store_finalized_header( + finalized_block_root, + update.finalized_header, + update.block_roots_root, + )?; + } + + Ok(()) + } + + /// Validates an execution header for import. The beacon header containing the execution + /// header is sent, plus the execution header, along with a proof that the execution header + /// is rooted in the beacon header body. + pub(crate) fn process_execution_header_update( + update: &ExecutionHeaderUpdate, + ) -> DispatchResult { + let latest_finalized_state = + FinalizedBeaconState::::get(LatestFinalizedBlockRoot::::get()) + .ok_or(Error::::NotBootstrapped)?; + // Checks that the header is an ancestor of a finalized header, using slot number. + ensure!( + update.header.slot <= latest_finalized_state.slot, + Error::::HeaderNotFinalized + ); + + // Checks that we don't skip execution headers, they need to be imported sequentially. + let latest_execution_state: ExecutionHeaderState = Self::latest_execution_state(); + ensure!( + latest_execution_state.block_number == 0 || + update.execution_header.block_number == + latest_execution_state.block_number + 1, + Error::::ExecutionHeaderSkippedBlock + ); + + // Gets the hash tree root of the execution header, in preparation for the execution + // header proof (used to check that the execution header is rooted in the beacon + // header body. + let execution_header_root: H256 = update + .execution_header + .hash_tree_root() + .map_err(|_| Error::::BlockBodyHashTreeRootFailed)?; + + ensure!( + verify_merkle_branch( + execution_header_root, + &update.execution_branch, + config::EXECUTION_HEADER_SUBTREE_INDEX, + config::EXECUTION_HEADER_DEPTH, + update.header.body_root + ), + Error::::InvalidExecutionHeaderProof + ); + + let block_root: H256 = update + .header + .hash_tree_root() + .map_err(|_| Error::::HeaderHashTreeRootFailed)?; + + match &update.ancestry_proof { + Some(proof) => { + Self::verify_ancestry_proof( + block_root, + update.header.slot, + &proof.header_branch, + proof.finalized_block_root, + )?; + }, + None => { + // If the ancestry proof is not provided, we expect this header to be a + // finalized header. We need to check that the header hash matches the finalized + // header root at the expected slot. + let state = >::get(block_root) + .ok_or(Error::::ExpectedFinalizedHeaderNotStored)?; + if update.header.slot != state.slot { + return Err(Error::::ExpectedFinalizedHeaderNotStored.into()) + } + }, + } + + Self::store_execution_header( + update.execution_header.block_hash, + update.execution_header.clone().into(), + update.header.slot, + block_root, + ); + + Ok(()) + } + + /// Verify that `block_root` is an ancestor of `finalized_block_root` Used to prove that + /// an execution header is an ancestor of a finalized header (i.e. the blocks are + /// on the same chain). + fn verify_ancestry_proof( + block_root: H256, + block_slot: u64, + block_root_proof: &[H256], + finalized_block_root: H256, + ) -> DispatchResult { + let state = >::get(finalized_block_root) + .ok_or(Error::::ExpectedFinalizedHeaderNotStored)?; + + ensure!(block_slot < state.slot, Error::::HeaderNotFinalized); + + let index_in_array = block_slot % (SLOTS_PER_HISTORICAL_ROOT as u64); + let leaf_index = (SLOTS_PER_HISTORICAL_ROOT as u64) + index_in_array; + + ensure!( + verify_merkle_branch( + block_root, + block_root_proof, + leaf_index as usize, + config::BLOCK_ROOT_AT_INDEX_DEPTH, + state.block_roots_root + ), + Error::::InvalidAncestryMerkleProof + ); + + Ok(()) + } + + /// Computes the signing root for a given beacon header and domain. The hash tree root + /// of the beacon header is computed, and then the combination of the beacon header hash + /// and the domain makes up the signing root. + pub(super) fn compute_signing_root( + beacon_header: &BeaconHeader, + domain: H256, + ) -> Result { + let beacon_header_root = beacon_header + .hash_tree_root() + .map_err(|_| Error::::HeaderHashTreeRootFailed)?; + + let hash_root = SigningData { object_root: beacon_header_root, domain } + .hash_tree_root() + .map_err(|_| Error::::SigningRootHashTreeRootFailed)?; + + Ok(hash_root) + } + + /// Stores a compacted (slot and block roots root (hash of the `block_roots` beacon state + /// field, used for ancestry proof)) beacon state in a ring buffer map, with the header root + /// as map key. + fn store_finalized_header( + header_root: H256, + header: BeaconHeader, + block_roots_root: H256, + ) -> DispatchResult { + let slot = header.slot; + + >::insert( + header_root, + CompactBeaconState { slot: header.slot, block_roots_root }, + ); + >::set(header_root); + + log::info!( + target: LOG_TARGET, + "💫 Updated latest finalized block root {} at slot {}.", + header_root, + slot + ); + + Self::deposit_event(Event::BeaconHeaderImported { block_hash: header_root, slot }); + + Ok(()) + } + + /// Stores the provided execution header in pallet storage. The header is stored + /// in a ring buffer map, with the block hash as map key. The last imported execution + /// header is also kept in storage, for the relayer to check import progress. + pub(crate) fn store_execution_header( + block_hash: H256, + header: CompactExecutionHeader, + beacon_slot: u64, + beacon_block_root: H256, + ) { + let block_number = header.block_number; + + >::insert(block_hash, header); + + log::trace!( + target: LOG_TARGET, + "💫 Updated latest execution block at {} to number {}.", + block_hash, + block_number + ); + + LatestExecutionState::::mutate(|s| { + s.beacon_block_root = beacon_block_root; + s.beacon_slot = beacon_slot; + s.block_hash = block_hash; + s.block_number = block_number; + }); + + Self::deposit_event(Event::ExecutionHeaderImported { block_hash, block_number }); + } + + /// Stores the validators root in storage. Validators root is the hash tree root of all the + /// validators at genesis and is used to used to identify the chain that we are on + /// (used in conjunction with the fork version). + /// + fn store_validators_root(validators_root: H256) { + >::set(validators_root); + } + + /// Returns the domain for the domain_type and fork_version. The domain is used to + /// distinguish between the different players in the chain (see DomainTypes + /// ) and to ensure we are + /// addressing the correct chain. + /// + pub(super) fn compute_domain( + domain_type: Vec, + fork_version: ForkVersion, + genesis_validators_root: H256, + ) -> Result { + let fork_data_root = + Self::compute_fork_data_root(fork_version, genesis_validators_root)?; + + let mut domain = [0u8; 32]; + domain[0..4].copy_from_slice(&(domain_type)); + domain[4..32].copy_from_slice(&(fork_data_root.0[..28])); + + Ok(domain.into()) + } + + /// Computes the fork data root. The fork data root is a merkleization of the current + /// fork version and the genesis validators root. + fn compute_fork_data_root( + current_version: ForkVersion, + genesis_validators_root: H256, + ) -> Result { + let hash_root = ForkData { + current_version, + genesis_validators_root: genesis_validators_root.into(), + } + .hash_tree_root() + .map_err(|_| Error::::ForkDataHashTreeRootFailed)?; + + Ok(hash_root) + } + + /// Checks that the sync committee bits (the votes of the sync committee members, + /// represented by bits 0 and 1) is more than a supermajority (2/3 of the votes are + /// positive). + pub(super) fn sync_committee_participation_is_supermajority( + sync_committee_bits: &[u8], + ) -> DispatchResult { + let sync_committee_sum = sync_committee_sum(sync_committee_bits); + ensure!( + ((sync_committee_sum * 3) as usize) >= sync_committee_bits.len() * 2, + Error::::SyncCommitteeParticipantsNotSupermajority + ); + + Ok(()) + } + + /// Returns the fork version based on the current epoch. The hard fork versions + /// are defined in pallet config. + pub(super) fn compute_fork_version(epoch: u64) -> ForkVersion { + Self::select_fork_version(&T::ForkVersions::get(), epoch) + } + + /// Returns the fork version based on the current epoch. + pub(super) fn select_fork_version(fork_versions: &ForkVersions, epoch: u64) -> ForkVersion { + if epoch >= fork_versions.capella.epoch { + return fork_versions.capella.version + } + if epoch >= fork_versions.bellatrix.epoch { + return fork_versions.bellatrix.version + } + if epoch >= fork_versions.altair.epoch { + return fork_versions.altair.version + } + + fork_versions.genesis.version + } + + /// Returns a vector of public keys that participated in the sync committee block signage. + /// Sync committee bits is an array of 0s and 1s, 0 meaning the corresponding sync committee + /// member did not participate in the vote, 1 meaning they participated. + /// This method can find the absent or participating members, based on the participant + /// parameter. participant = false will return absent participants, participant = true will + /// return participating members. + pub fn find_pubkeys( + sync_committee_bits: &[u8], + sync_committee_pubkeys: &[PublicKeyPrepared], + participant: bool, + ) -> Vec { + let mut pubkeys: Vec = Vec::new(); + for (bit, pubkey) in sync_committee_bits.iter().zip(sync_committee_pubkeys.iter()) { + if *bit == u8::from(participant) { + pubkeys.push(*pubkey); + } + } + pubkeys + } + + /// Calculates signing root for BeaconHeader. The signing root is used for the message + /// value in BLS signature verification. + pub fn signing_root( + header: &BeaconHeader, + validators_root: H256, + signature_slot: u64, + ) -> Result { + let fork_version = Self::compute_fork_version(compute_epoch( + signature_slot, + config::SLOTS_PER_EPOCH as u64, + )); + let domain_type = config::DOMAIN_SYNC_COMMITTEE.to_vec(); + // Domains are used for for seeds, for signatures, and for selecting aggregators. + let domain = Self::compute_domain(domain_type, fork_version, validators_root)?; + // Hash tree root of SigningData - object root + domain + let signing_root = Self::compute_signing_root(header, domain)?; + Ok(signing_root) + } + } +} diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/mock.rs b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/mock.rs new file mode 100644 index 000000000000..4d1d14a10158 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/mock.rs @@ -0,0 +1,275 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use crate as ethereum_beacon_client; +use frame_support::parameter_types; +use pallet_timestamp; +use primitives::{Fork, ForkVersions}; +use sp_core::H256; +use sp_runtime::traits::{BlakeTwo256, IdentityLookup}; + +#[cfg(not(feature = "beacon-spec-mainnet"))] +pub mod minimal { + use super::*; + + use crate::config; + use hex_literal::hex; + use primitives::CompactExecutionHeader; + use snowbridge_core::inbound::{Log, Proof}; + use sp_runtime::BuildStorage; + use std::{fs::File, path::PathBuf}; + + type Block = frame_system::mocking::MockBlock; + + frame_support::construct_runtime!( + pub enum Test { + System: frame_system::{Pallet, Call, Storage, Event}, + Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent}, + EthereumBeaconClient: ethereum_beacon_client::{Pallet, Call, Storage, Event}, + } + ); + + parameter_types! { + pub const BlockHashCount: u64 = 250; + pub const SS58Prefix: u8 = 42; + } + + impl frame_system::Config for Test { + type BaseCallFilter = frame_support::traits::Everything; + type OnSetCode = (); + type BlockWeights = (); + type BlockLength = (); + type DbWeight = (); + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type RuntimeTask = RuntimeTask; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = u64; + type Lookup = IdentityLookup; + type RuntimeEvent = RuntimeEvent; + type BlockHashCount = BlockHashCount; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = (); + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = SS58Prefix; + type MaxConsumers = frame_support::traits::ConstU32<16>; + type Nonce = u64; + type Block = Block; + } + + impl pallet_timestamp::Config for Test { + type Moment = u64; + type OnTimestampSet = (); + type MinimumPeriod = (); + type WeightInfo = (); + } + + parameter_types! { + pub const ExecutionHeadersPruneThreshold: u32 = 10; + pub const ChainForkVersions: ForkVersions = ForkVersions{ + genesis: Fork { + version: [0, 0, 0, 1], // 0x00000001 + epoch: 0, + }, + altair: Fork { + version: [1, 0, 0, 1], // 0x01000001 + epoch: 0, + }, + bellatrix: Fork { + version: [2, 0, 0, 1], // 0x02000001 + epoch: 0, + }, + capella: Fork { + version: [3, 0, 0, 1], // 0x03000001 + epoch: 0, + }, + }; + } + + impl ethereum_beacon_client::Config for Test { + type RuntimeEvent = RuntimeEvent; + type ForkVersions = ChainForkVersions; + type MaxExecutionHeadersToKeep = ExecutionHeadersPruneThreshold; + type WeightInfo = (); + } + + // Build genesis storage according to the mock runtime. + pub fn new_tester() -> sp_io::TestExternalities { + let t = frame_system::GenesisConfig::::default().build_storage().unwrap(); + let mut ext = sp_io::TestExternalities::new(t); + let _ = ext.execute_with(|| Timestamp::set(RuntimeOrigin::signed(1), 30_000)); + ext + } + + fn load_fixture(basename: &str) -> Result + where + T: for<'de> serde::Deserialize<'de>, + { + let filepath: PathBuf = + [env!("CARGO_MANIFEST_DIR"), "tests", "fixtures", basename].iter().collect(); + serde_json::from_reader(File::open(filepath).unwrap()) + } + + pub fn load_execution_header_update_fixture() -> primitives::ExecutionHeaderUpdate { + load_fixture("execution-header-update.minimal.json").unwrap() + } + + pub fn load_checkpoint_update_fixture( + ) -> primitives::CheckpointUpdate<{ config::SYNC_COMMITTEE_SIZE }> { + load_fixture("initial-checkpoint.minimal.json").unwrap() + } + + pub fn load_sync_committee_update_fixture( + ) -> primitives::Update<{ config::SYNC_COMMITTEE_SIZE }, { config::SYNC_COMMITTEE_BITS_SIZE }> { + load_fixture("sync-committee-update.minimal.json").unwrap() + } + + pub fn load_finalized_header_update_fixture( + ) -> primitives::Update<{ config::SYNC_COMMITTEE_SIZE }, { config::SYNC_COMMITTEE_BITS_SIZE }> { + load_fixture("finalized-header-update.minimal.json").unwrap() + } + + pub fn load_next_sync_committee_update_fixture( + ) -> primitives::Update<{ config::SYNC_COMMITTEE_SIZE }, { config::SYNC_COMMITTEE_BITS_SIZE }> { + load_fixture("next-sync-committee-update.minimal.json").unwrap() + } + + pub fn load_next_finalized_header_update_fixture( + ) -> primitives::Update<{ config::SYNC_COMMITTEE_SIZE }, { config::SYNC_COMMITTEE_BITS_SIZE }> { + load_fixture("next-finalized-header-update.minimal.json").unwrap() + } + + pub fn get_message_verification_payload() -> (Log, Proof) { + ( + Log { + address: hex!("ee9170abfbf9421ad6dd07f6bdec9d89f2b581e0").into(), + topics: vec![ + hex!("1b11dcf133cc240f682dab2d3a8e4cd35c5da8c9cf99adac4336f8512584c5ad").into(), + hex!("00000000000000000000000000000000000000000000000000000000000003e8").into(), + hex!("0000000000000000000000000000000000000000000000000000000000000001").into(), + ], + data: hex!("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004b000f000000000000000100d184c103f7acc340847eee82a0b909e3358bc28d440edffa1352b13227e8ee646f3ea37456dec701345772617070656420457468657210574554481235003511000000000000000000000000000000000000000000").into(), + }, + Proof { + block_hash: hex!("05aaa60b0f27cce9e71909508527264b77ee14da7b5bf915fcc4e32715333213").into(), + tx_index: 0, + data: (vec![ + hex!("cf0d1c1ba57d1e0edfb59786c7e30c2b7e12bd54612b00cd21c4eaeecedf44fb").to_vec(), + hex!("d21fc4f68ab05bc4dcb23c67008e92c4d466437cdd6ed7aad0c008944c185510").to_vec(), + hex!("b9890f91ca0d77aa2a4adfaf9b9e40c94cac9e638b6d9797923865872944b646").to_vec(), + ], vec![ + hex!("f90131a0b601337b3aa10a671caa724eba641e759399979856141d3aea6b6b4ac59b889ba00c7d5dd48be9060221a02fb8fa213860b4c50d47046c8fa65ffaba5737d569e0a094601b62a1086cd9c9cb71a7ebff9e718f3217fd6e837efe4246733c0a196f63a06a4b0dd0aefc37b3c77828c8f07d1b7a2455ceb5dbfd3c77d7d6aeeddc2f7e8ca0d6e8e23142cdd8ec219e1f5d8b56aa18e456702b195deeaa210327284d42ade4a08a313d4c87023005d1ab631bbfe3f5de1e405d0e66d0bef3e033f1e5711b5521a0bf09a5d9a48b10ade82b8d6a5362a15921c8b5228a3487479b467db97411d82fa0f95cccae2a7c572ef3c566503e30bac2b2feb2d2f26eebf6d870dcf7f8cf59cea0d21fc4f68ab05bc4dcb23c67008e92c4d466437cdd6ed7aad0c008944c1855108080808080808080").to_vec(), + hex!("f851a0b9890f91ca0d77aa2a4adfaf9b9e40c94cac9e638b6d9797923865872944b646a060a634b9280e3a23fb63375e7bbdd9ab07fd379ab6a67e2312bbc112195fa358808080808080808080808080808080").to_vec(), + hex!("f9030820b9030402f90300018301d6e2b9010000000000000800000000000020040008000000000000000000000000400000008000000000000000000000000000000000000000000000000000000000042010000000001000000000000000000000000000000000040000000000000000000000000000000000000000000000008000000000000000002000000000000000000000000200000000000000200000000000100000000040000001000200008000000000000200000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000f901f5f87a942ffa5ecdbe006d30397c7636d3e015eee251369ff842a0c965575a00553e094ca7c5d14f02e107c258dda06867cbf9e0e69f80e71bbcc1a000000000000000000000000000000000000000000000000000000000000003e8a000000000000000000000000000000000000000000000000000000000000003e8f9011c94ee9170abfbf9421ad6dd07f6bdec9d89f2b581e0f863a01b11dcf133cc240f682dab2d3a8e4cd35c5da8c9cf99adac4336f8512584c5ada000000000000000000000000000000000000000000000000000000000000003e8a00000000000000000000000000000000000000000000000000000000000000001b8a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004b000f000000000000000100d184c103f7acc340847eee82a0b909e3358bc28d440edffa1352b13227e8ee646f3ea37456dec701345772617070656420457468657210574554481235003511000000000000000000000000000000000000000000f858948cf6147918a5cbb672703f879f385036f8793a24e1a01449abf21e49fd025f33495e77f7b1461caefdd3d4bb646424a3f445c4576a5ba0000000000000000000000000440edffa1352b13227e8ee646f3ea37456dec701").to_vec(), + ]), + } + ) + } + + pub fn get_message_verification_header() -> CompactExecutionHeader { + CompactExecutionHeader { + parent_hash: hex!("04a7f6ab8282203562c62f38b0ab41d32aaebe2c7ea687702b463148a6429e04") + .into(), + block_number: 55, + state_root: hex!("894d968712976d613519f973a317cb0781c7b039c89f27ea2b7ca193f7befdb3") + .into(), + receipts_root: hex!("cf0d1c1ba57d1e0edfb59786c7e30c2b7e12bd54612b00cd21c4eaeecedf44fb") + .into(), + } + } +} + +#[cfg(feature = "beacon-spec-mainnet")] +pub mod mainnet { + use super::*; + + type Block = frame_system::mocking::MockBlock; + use sp_runtime::BuildStorage; + + frame_support::construct_runtime!( + pub enum Test { + System: frame_system::{Pallet, Call, Storage, Event}, + Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent}, + EthereumBeaconClient: ethereum_beacon_client::{Pallet, Call, Storage, Event}, + } + ); + + parameter_types! { + pub const BlockHashCount: u64 = 250; + pub const SS58Prefix: u8 = 42; + } + + impl frame_system::Config for Test { + type BaseCallFilter = frame_support::traits::Everything; + type OnSetCode = (); + type BlockWeights = (); + type BlockLength = (); + type DbWeight = (); + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type RuntimeTask = RuntimeTask; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = u64; + type Lookup = IdentityLookup; + type RuntimeEvent = RuntimeEvent; + type BlockHashCount = BlockHashCount; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = (); + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = SS58Prefix; + type MaxConsumers = frame_support::traits::ConstU32<16>; + type Nonce = u64; + type Block = Block; + } + + impl pallet_timestamp::Config for Test { + type Moment = u64; + type OnTimestampSet = (); + type MinimumPeriod = (); + type WeightInfo = (); + } + + parameter_types! { + pub const ChainForkVersions: ForkVersions = ForkVersions{ + genesis: Fork { + version: [0, 0, 16, 32], // 0x00001020 + epoch: 0, + }, + altair: Fork { + version: [1, 0, 16, 32], // 0x01001020 + epoch: 36660, + }, + bellatrix: Fork { + version: [2, 0, 16, 32], // 0x02001020 + epoch: 112260, + }, + capella: Fork { + version: [3, 0, 16, 32], // 0x03001020 + epoch: 162304, + }, + }; + pub const ExecutionHeadersPruneThreshold: u32 = 10; + } + + impl ethereum_beacon_client::Config for Test { + type RuntimeEvent = RuntimeEvent; + type ForkVersions = ChainForkVersions; + type MaxExecutionHeadersToKeep = ExecutionHeadersPruneThreshold; + type WeightInfo = (); + } + + // Build genesis storage according to the mock runtime. + pub fn new_tester() -> sp_io::TestExternalities { + let t = frame_system::GenesisConfig::::default().build_storage().unwrap(); + let mut ext = sp_io::TestExternalities::new(t); + let _ = ext.execute_with(|| Timestamp::set(RuntimeOrigin::signed(1), 30_000)); + ext + } +} diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/tests.rs b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/tests.rs new file mode 100644 index 000000000000..92a93720ae93 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/tests.rs @@ -0,0 +1,1032 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use crate::{ + config::{EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SLOTS_PER_EPOCH}, + functions::compute_period, + mock::minimal::*, + pallet::ExecutionHeaders, + sync_committee_sum, verify_merkle_branch, BeaconHeader, CompactBeaconState, Error, + ExecutionHeaderBuffer, FinalizedBeaconState, LatestExecutionState, LatestFinalizedBlockRoot, + NextSyncCommittee, SyncCommitteePrepared, +}; + +use frame_support::{assert_err, assert_noop, assert_ok}; +use hex_literal::hex; +use primitives::{ + CompactExecutionHeader, ExecutionHeaderState, Fork, ForkVersions, NextSyncCommitteeUpdate, +}; +use rand::{thread_rng, Rng}; +use snowbridge_core::{ + inbound::{VerificationError, Verifier}, + RingBufferMap, +}; +use sp_core::H256; +use sp_runtime::DispatchError; + +/// Arbitrary hash used for tests and invalid hashes. +const TEST_HASH: [u8; 32] = + hex!["5f6f02af29218292d21a69b64a794a7c0873b3e0f54611972863706e8cbdf371"]; + +/* UNIT TESTS */ + +#[test] +pub fn sum_sync_committee_participation() { + new_tester().execute_with(|| { + assert_eq!(sync_committee_sum(&[0, 1, 0, 1, 1, 0, 1, 0, 1]), 5); + }); +} + +#[test] +pub fn compute_domain() { + new_tester().execute_with(|| { + let domain = EthereumBeaconClient::compute_domain( + hex!("07000000").into(), + hex!("00000001"), + hex!("5dec7ae03261fde20d5b024dfabce8bac3276c9a4908e23d50ba8c9b50b0adff").into(), + ); + + assert_ok!(&domain); + assert_eq!( + domain.unwrap(), + hex!("0700000046324489ceb6ada6d118eacdbe94f49b1fcb49d5481a685979670c7c").into() + ); + }); +} + +#[test] +pub fn compute_signing_root_bls() { + new_tester().execute_with(|| { + let signing_root = EthereumBeaconClient::compute_signing_root( + &BeaconHeader { + slot: 3529537, + proposer_index: 192549, + parent_root: hex!( + "1f8dc05ea427f78e84e2e2666e13c3befb7106fd1d40ef8a3f67cf615f3f2a4c" + ) + .into(), + state_root: hex!( + "0dfb492a83da711996d2d76b64604f9bca9dc08b6c13cf63b3be91742afe724b" + ) + .into(), + body_root: hex!("66fba38f7c8c2526f7ddfe09c1a54dd12ff93bdd4d0df6a0950e88e802228bfa") + .into(), + }, + hex!("07000000afcaaba0efab1ca832a15152469bb09bb84641c405171dfa2d3fb45f").into(), + ); + + assert_ok!(&signing_root); + assert_eq!( + signing_root.unwrap(), + hex!("3ff6e9807da70b2f65cdd58ea1b25ed441a1d589025d2c4091182026d7af08fb").into() + ); + }); +} + +#[test] +pub fn compute_signing_root() { + new_tester().execute_with(|| { + let signing_root = EthereumBeaconClient::compute_signing_root( + &BeaconHeader { + slot: 222472, + proposer_index: 10726, + parent_root: hex!( + "5d481a9721f0ecce9610eab51d400d223683d599b7fcebca7e4c4d10cdef6ebb" + ) + .into(), + state_root: hex!( + "14eb4575895f996a84528b789ff2e4d5148242e2983f03068353b2c37015507a" + ) + .into(), + body_root: hex!("7bb669c75b12e0781d6fa85d7fc2f32d64eafba89f39678815b084c156e46cac") + .into(), + }, + hex!("07000000e7acb21061790987fa1c1e745cccfb358370b33e8af2b2c18938e6c2").into(), + ); + + assert_ok!(&signing_root); + assert_eq!( + signing_root.unwrap(), + hex!("da12b6a6d3516bc891e8a49f82fc1925cec40b9327e06457f695035303f55cd8").into() + ); + }); +} + +#[test] +pub fn compute_domain_bls() { + new_tester().execute_with(|| { + let domain = EthereumBeaconClient::compute_domain( + hex!("07000000").into(), + hex!("01000000"), + hex!("4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95").into(), + ); + + assert_ok!(&domain); + assert_eq!( + domain.unwrap(), + hex!("07000000afcaaba0efab1ca832a15152469bb09bb84641c405171dfa2d3fb45f").into() + ); + }); +} + +#[test] +pub fn verify_merkle_branch_for_finalized_root() { + new_tester().execute_with(|| { + assert!(verify_merkle_branch( + hex!("0000000000000000000000000000000000000000000000000000000000000000").into(), + &[ + hex!("0000000000000000000000000000000000000000000000000000000000000000").into(), + hex!("5f6f02af29218292d21a69b64a794a7c0873b3e0f54611972863706e8cbdf371").into(), + hex!("e7125ff9ab5a840c44bedb4731f440a405b44e15f2d1a89e27341b432fabe13d").into(), + hex!("002c1fe5bc0bd62db6f299a582f2a80a6d5748ccc82e7ed843eaf0ae0739f74a").into(), + hex!("d2dc4ba9fd4edff6716984136831e70a6b2e74fca27b8097a820cbbaa5a6e3c3").into(), + hex!("91f77a19d8afa4a08e81164bb2e570ecd10477b3b65c305566a6d2be88510584").into(), + ], + crate::config::FINALIZED_ROOT_INDEX, + crate::config::FINALIZED_ROOT_DEPTH, + hex!("e46559327592741956f6beaa0f52e49625eb85dce037a0bd2eff333c743b287f").into() + )); + }); +} + +#[test] +pub fn verify_merkle_branch_fails_if_depth_and_branch_dont_match() { + new_tester().execute_with(|| { + assert!(!verify_merkle_branch( + hex!("0000000000000000000000000000000000000000000000000000000000000000").into(), + &[ + hex!("0000000000000000000000000000000000000000000000000000000000000000").into(), + hex!("5f6f02af29218292d21a69b64a794a7c0873b3e0f54611972863706e8cbdf371").into(), + hex!("e7125ff9ab5a840c44bedb4731f440a405b44e15f2d1a89e27341b432fabe13d").into(), + ], + crate::config::FINALIZED_ROOT_INDEX, + crate::config::FINALIZED_ROOT_DEPTH, + hex!("e46559327592741956f6beaa0f52e49625eb85dce037a0bd2eff333c743b287f").into() + )); + }); +} + +#[test] +pub fn sync_committee_participation_is_supermajority() { + let bits = + hex!("bffffffff7f1ffdfcfeffeffbfdffffbfffffdffffefefffdffff7f7ffff77fffdf7bff77ffdf7fffafffffff77fefffeff7effffffff5f7fedfffdfb6ddff7b" + ); + let participation = primitives::decompress_sync_committee_bits::<512, 64>(bits); + assert_ok!(EthereumBeaconClient::sync_committee_participation_is_supermajority(&participation)); +} + +#[test] +pub fn sync_committee_participation_is_supermajority_errors_when_not_supermajority() { + new_tester().execute_with(|| { + let participation: [u8; 512] = [ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, + 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, + 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + ]; + + assert_err!( + EthereumBeaconClient::sync_committee_participation_is_supermajority(&participation), + Error::::SyncCommitteeParticipantsNotSupermajority + ); + }); +} + +#[test] +pub fn execution_header_pruning() { + new_tester().execute_with(|| { + let execution_header_prune_threshold = ExecutionHeadersPruneThreshold::get(); + let to_be_deleted = execution_header_prune_threshold / 2; + + let mut stored_hashes = vec![]; + + for i in 0..execution_header_prune_threshold { + let mut hash = H256::default(); + thread_rng().try_fill(&mut hash.0[..]).unwrap(); + EthereumBeaconClient::store_execution_header( + hash, + CompactExecutionHeader::default(), + i as u64, + hash, + ); + stored_hashes.push(hash); + } + + // We should have stored everything until now + assert_eq!({ ExecutionHeaders::::iter().count() }, stored_hashes.len()); + + // Let's push extra entries so that some of the previous entries are deleted. + for i in 0..to_be_deleted { + let mut hash = H256::default(); + thread_rng().try_fill(&mut hash.0[..]).unwrap(); + EthereumBeaconClient::store_execution_header( + hash, + CompactExecutionHeader::default(), + (i + execution_header_prune_threshold) as u64, + hash, + ); + + stored_hashes.push(hash); + } + + // We should have only stored upto `execution_header_prune_threshold` + assert_eq!( + ExecutionHeaders::::iter().count() as u32, + execution_header_prune_threshold + ); + + // First `to_be_deleted` items must be deleted + for i in 0..to_be_deleted { + assert!(!ExecutionHeaders::::contains_key(stored_hashes[i as usize])); + } + + // Other entries should be part of data + for i in to_be_deleted..(to_be_deleted + execution_header_prune_threshold) { + assert!(ExecutionHeaders::::contains_key(stored_hashes[i as usize])); + } + }); +} + +#[test] +fn compute_fork_version() { + let mock_fork_versions = ForkVersions { + genesis: Fork { version: [0, 0, 0, 0], epoch: 0 }, + altair: Fork { version: [0, 0, 0, 1], epoch: 10 }, + bellatrix: Fork { version: [0, 0, 0, 2], epoch: 20 }, + capella: Fork { version: [0, 0, 0, 3], epoch: 30 }, + }; + new_tester().execute_with(|| { + assert_eq!(EthereumBeaconClient::select_fork_version(&mock_fork_versions, 0), [0, 0, 0, 0]); + assert_eq!(EthereumBeaconClient::select_fork_version(&mock_fork_versions, 1), [0, 0, 0, 0]); + assert_eq!( + EthereumBeaconClient::select_fork_version(&mock_fork_versions, 10), + [0, 0, 0, 1] + ); + assert_eq!( + EthereumBeaconClient::select_fork_version(&mock_fork_versions, 21), + [0, 0, 0, 2] + ); + assert_eq!( + EthereumBeaconClient::select_fork_version(&mock_fork_versions, 20), + [0, 0, 0, 2] + ); + assert_eq!( + EthereumBeaconClient::select_fork_version(&mock_fork_versions, 32), + [0, 0, 0, 3] + ); + }); +} + +#[test] +fn find_absent_keys() { + let participation: [u8; 32] = [ + 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, + ]; + let update = load_sync_committee_update_fixture(); + let sync_committee_prepared: SyncCommitteePrepared = + (&update.next_sync_committee_update.unwrap().next_sync_committee) + .try_into() + .unwrap(); + + new_tester().execute_with(|| { + let pubkeys = EthereumBeaconClient::find_pubkeys( + &participation, + (*sync_committee_prepared.pubkeys).as_ref(), + false, + ); + assert_eq!(pubkeys.len(), 2); + assert_eq!(pubkeys[0], sync_committee_prepared.pubkeys[0]); + assert_eq!(pubkeys[1], sync_committee_prepared.pubkeys[7]); + }); +} + +#[test] +fn find_present_keys() { + let participation: [u8; 32] = [ + 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, + 1, 0, + ]; + let update = load_sync_committee_update_fixture(); + let sync_committee_prepared: SyncCommitteePrepared = + (&update.next_sync_committee_update.unwrap().next_sync_committee) + .try_into() + .unwrap(); + + new_tester().execute_with(|| { + let pubkeys = EthereumBeaconClient::find_pubkeys( + &participation, + (*sync_committee_prepared.pubkeys).as_ref(), + true, + ); + assert_eq!(pubkeys.len(), 4); + assert_eq!(pubkeys[0], sync_committee_prepared.pubkeys[1]); + assert_eq!(pubkeys[1], sync_committee_prepared.pubkeys[8]); + assert_eq!(pubkeys[2], sync_committee_prepared.pubkeys[26]); + assert_eq!(pubkeys[3], sync_committee_prepared.pubkeys[30]); + }); +} + +#[test] +fn cross_check_execution_state() { + new_tester().execute_with(|| { + let header_root: H256 = TEST_HASH.into(); + >::insert( + header_root, + CompactBeaconState { + // set slot to period 5 + slot: ((EPOCHS_PER_SYNC_COMMITTEE_PERIOD * SLOTS_PER_EPOCH) * 5) as u64, + block_roots_root: Default::default(), + }, + ); + LatestFinalizedBlockRoot::::set(header_root); + >::set(ExecutionHeaderState { + beacon_block_root: Default::default(), + // set slot to period 2 + beacon_slot: ((EPOCHS_PER_SYNC_COMMITTEE_PERIOD * SLOTS_PER_EPOCH) * 2) as u64, + block_hash: Default::default(), + block_number: 0, + }); + + assert_err!( + EthereumBeaconClient::cross_check_execution_state(), + Error::::ExecutionHeaderTooFarBehind + ); + }); +} + +/* SYNC PROCESS TESTS */ + +#[test] +fn process_initial_checkpoint() { + let checkpoint = load_checkpoint_update_fixture(); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::force_checkpoint( + RuntimeOrigin::root(), + Box::new(checkpoint.clone()) + )); + let block_root: H256 = checkpoint.header.hash_tree_root().unwrap(); + assert!(>::contains_key(block_root)); + }); +} + +#[test] +fn process_initial_checkpoint_with_invalid_sync_committee_proof() { + let mut checkpoint = load_checkpoint_update_fixture(); + checkpoint.current_sync_committee_branch[0] = TEST_HASH.into(); + + new_tester().execute_with(|| { + assert_err!( + EthereumBeaconClient::force_checkpoint(RuntimeOrigin::root(), Box::new(checkpoint)), + Error::::InvalidSyncCommitteeMerkleProof + ); + }); +} + +#[test] +fn process_initial_checkpoint_with_invalid_blocks_root_proof() { + let mut checkpoint = load_checkpoint_update_fixture(); + checkpoint.block_roots_branch[0] = TEST_HASH.into(); + + new_tester().execute_with(|| { + assert_err!( + EthereumBeaconClient::force_checkpoint(RuntimeOrigin::root(), Box::new(checkpoint)), + Error::::InvalidBlockRootsRootMerkleProof + ); + }); +} + +#[test] +fn submit_update_in_current_period() { + let checkpoint = load_checkpoint_update_fixture(); + let update = load_finalized_header_update_fixture(); + let initial_period = compute_period(checkpoint.header.slot); + let update_period = compute_period(update.finalized_header.slot); + assert_eq!(initial_period, update_period); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(update.clone()) + )); + let block_root: H256 = update.finalized_header.hash_tree_root().unwrap(); + assert!(>::contains_key(block_root)); + }); +} + +#[test] +fn submit_update_with_sync_committee_in_current_period() { + let checkpoint = load_checkpoint_update_fixture(); + let update = load_sync_committee_update_fixture(); + let init_period = compute_period(checkpoint.header.slot); + let update_period = compute_period(update.finalized_header.slot); + assert_eq!(init_period, update_period); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert!(!>::exists()); + assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(update))); + assert!(>::exists()); + }); +} + +#[test] +fn submit_update_in_next_period() { + let checkpoint = load_checkpoint_update_fixture(); + let sync_committee_update = load_sync_committee_update_fixture(); + let update = load_next_finalized_header_update_fixture(); + let sync_committee_period = compute_period(sync_committee_update.finalized_header.slot); + let next_sync_committee_period = compute_period(update.finalized_header.slot); + assert_eq!(sync_committee_period + 1, next_sync_committee_period); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(sync_committee_update.clone()) + )); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(update.clone()) + )); + let block_root: H256 = update.finalized_header.clone().hash_tree_root().unwrap(); + assert!(>::contains_key(block_root)); + }); +} + +#[test] +fn submit_update_with_invalid_header_proof() { + let checkpoint = load_checkpoint_update_fixture(); + let mut update = load_sync_committee_update_fixture(); + let init_period = compute_period(checkpoint.header.slot); + let update_period = compute_period(update.finalized_header.slot); + assert_eq!(init_period, update_period); + update.finality_branch[0] = TEST_HASH.into(); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert!(!>::exists()); + assert_err!( + EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(update)), + Error::::InvalidHeaderMerkleProof + ); + }); +} + +#[test] +fn submit_update_with_invalid_block_roots_proof() { + let checkpoint = load_checkpoint_update_fixture(); + let mut update = load_sync_committee_update_fixture(); + let init_period = compute_period(checkpoint.header.slot); + let update_period = compute_period(update.finalized_header.slot); + assert_eq!(init_period, update_period); + update.block_roots_branch[0] = TEST_HASH.into(); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert!(!>::exists()); + assert_err!( + EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(update)), + Error::::InvalidBlockRootsRootMerkleProof + ); + }); +} + +#[test] +fn submit_update_with_invalid_next_sync_committee_proof() { + let checkpoint = load_checkpoint_update_fixture(); + let mut update = load_sync_committee_update_fixture(); + let init_period = compute_period(checkpoint.header.slot); + let update_period = compute_period(update.finalized_header.slot); + assert_eq!(init_period, update_period); + if let Some(ref mut next_sync_committee_update) = update.next_sync_committee_update { + next_sync_committee_update.next_sync_committee_branch[0] = TEST_HASH.into(); + } + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert!(!>::exists()); + assert_err!( + EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(update)), + Error::::InvalidSyncCommitteeMerkleProof + ); + }); +} + +#[test] +fn submit_update_with_skipped_period() { + let checkpoint = load_checkpoint_update_fixture(); + let sync_committee_update = load_sync_committee_update_fixture(); + let mut update = load_next_finalized_header_update_fixture(); + update.signature_slot += (EPOCHS_PER_SYNC_COMMITTEE_PERIOD * SLOTS_PER_EPOCH) as u64; + update.attested_header.slot = update.signature_slot - 1; + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(sync_committee_update.clone()) + )); + assert_err!( + EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(update)), + Error::::SkippedSyncCommitteePeriod + ); + }); +} + +#[test] +fn submit_update_with_sync_committee_in_next_period() { + let checkpoint = load_checkpoint_update_fixture(); + let update = load_sync_committee_update_fixture(); + let next_update = load_next_sync_committee_update_fixture(); + let update_period = compute_period(update.finalized_header.slot); + let next_update_period = compute_period(next_update.finalized_header.slot); + assert_eq!(update_period + 1, next_update_period); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert!(!>::exists()); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(update.clone()) + )); + assert!(>::exists()); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(next_update.clone()) + )); + let last_finalized_state = + FinalizedBeaconState::::get(LatestFinalizedBlockRoot::::get()).unwrap(); + let last_synced_period = compute_period(last_finalized_state.slot); + assert_eq!(last_synced_period, next_update_period); + }); +} + +#[test] +fn submit_update_with_sync_committee_invalid_signature_slot() { + let checkpoint = load_checkpoint_update_fixture(); + let mut update = load_sync_committee_update_fixture(); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + + // makes a invalid update with signature_slot should be more than attested_slot + update.signature_slot = update.attested_header.slot; + + assert_err!( + EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(update)), + Error::::InvalidUpdateSlot + ); + }); +} + +#[test] +fn submit_update_with_skipped_sync_committee_period() { + let checkpoint = load_checkpoint_update_fixture(); + let finalized_update = load_next_finalized_header_update_fixture(); + let checkpoint_period = compute_period(checkpoint.header.slot); + let next_sync_committee_period = compute_period(finalized_update.finalized_header.slot); + assert_eq!(checkpoint_period + 1, next_sync_committee_period); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert_err!( + EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(finalized_update)), + Error::::SkippedSyncCommitteePeriod + ); + }); +} + +#[test] +fn submit_update_execution_headers_too_far_behind() { + let checkpoint = load_checkpoint_update_fixture(); + let finalized_header_update = load_finalized_header_update_fixture(); + let execution_header_update = load_execution_header_update_fixture(); + let next_update = load_next_sync_committee_update_fixture(); + + new_tester().execute_with(|| { + let far_ahead_finalized_header_slot = finalized_header_update.finalized_header.slot + + (EPOCHS_PER_SYNC_COMMITTEE_PERIOD * SLOTS_PER_EPOCH * 2) as u64; + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(finalized_header_update) + )); + assert_ok!(EthereumBeaconClient::submit_execution_header( + RuntimeOrigin::signed(1), + Box::new(execution_header_update) + )); + + let header_root: H256 = TEST_HASH.into(); + >::insert( + header_root, + CompactBeaconState { + slot: far_ahead_finalized_header_slot, + block_roots_root: Default::default(), + }, + ); + LatestFinalizedBlockRoot::::set(header_root); + + assert_err!( + EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(next_update)), + Error::::ExecutionHeaderTooFarBehind + ); + }); +} + +#[test] +fn submit_irrelevant_update() { + let checkpoint = load_checkpoint_update_fixture(); + let mut update = load_next_finalized_header_update_fixture(); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + + // makes an invalid update where the attested_header slot value should be greater than the + // checkpoint slot value + update.finalized_header.slot = checkpoint.header.slot; + update.attested_header.slot = checkpoint.header.slot; + update.signature_slot = checkpoint.header.slot + 1; + + assert_err!( + EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(update)), + Error::::IrrelevantUpdate + ); + }); +} + +#[test] +fn submit_update_with_missing_bootstrap() { + let update = load_next_finalized_header_update_fixture(); + + new_tester().execute_with(|| { + assert_err!( + EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(update)), + Error::::NotBootstrapped + ); + }); +} + +#[test] +fn submit_update_with_invalid_sync_committee_update() { + let checkpoint = load_checkpoint_update_fixture(); + let update = load_sync_committee_update_fixture(); + let mut next_update = load_next_sync_committee_update_fixture(); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + + assert_ok!(EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(update))); + + // makes update with invalid next_sync_committee + >::mutate(>::get(), |x| { + let prev = x.unwrap(); + *x = Some(CompactBeaconState { slot: next_update.attested_header.slot, ..prev }); + }); + next_update.attested_header.slot += 1; + next_update.signature_slot = next_update.attested_header.slot + 1; + let next_sync_committee = NextSyncCommitteeUpdate::default(); + next_update.next_sync_committee_update = Some(next_sync_committee); + + assert_err!( + EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(next_update)), + Error::::InvalidSyncCommitteeUpdate + ); + }); +} + +#[test] +fn submit_execution_header_update() { + let checkpoint = load_checkpoint_update_fixture(); + let finalized_header_update = load_finalized_header_update_fixture(); + let execution_header_update = load_execution_header_update_fixture(); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(finalized_header_update) + )); + assert_ok!(EthereumBeaconClient::submit_execution_header( + RuntimeOrigin::signed(1), + Box::new(execution_header_update.clone()) + )); + assert!(>::contains_key( + execution_header_update.execution_header.block_hash + )); + }); +} + +#[test] +fn submit_execution_header_update_invalid_ancestry_proof() { + let checkpoint = load_checkpoint_update_fixture(); + let finalized_header_update = load_finalized_header_update_fixture(); + let mut execution_header_update = load_execution_header_update_fixture(); + if let Some(ref mut ancestry_proof) = execution_header_update.ancestry_proof { + ancestry_proof.header_branch[0] = TEST_HASH.into() + } + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(finalized_header_update) + )); + assert_err!( + EthereumBeaconClient::submit_execution_header( + RuntimeOrigin::signed(1), + Box::new(execution_header_update) + ), + Error::::InvalidAncestryMerkleProof + ); + }); +} + +#[test] +fn submit_execution_header_update_invalid_execution_header_proof() { + let checkpoint = load_checkpoint_update_fixture(); + let finalized_header_update = load_finalized_header_update_fixture(); + let mut execution_header_update = load_execution_header_update_fixture(); + execution_header_update.execution_branch[0] = TEST_HASH.into(); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(finalized_header_update) + )); + assert_err!( + EthereumBeaconClient::submit_execution_header( + RuntimeOrigin::signed(1), + Box::new(execution_header_update) + ), + Error::::InvalidExecutionHeaderProof + ); + }); +} + +#[test] +fn submit_execution_header_update_that_skips_block() { + let checkpoint = load_checkpoint_update_fixture(); + let finalized_header_update = load_finalized_header_update_fixture(); + let execution_header_update = load_execution_header_update_fixture(); + let mut skipped_block_execution_header_update = load_execution_header_update_fixture(); + skipped_block_execution_header_update.execution_header.block_number = + execution_header_update.execution_header.block_number + 2; + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(finalized_header_update) + )); + assert_ok!(EthereumBeaconClient::submit_execution_header( + RuntimeOrigin::signed(1), + Box::new(execution_header_update.clone()) + )); + assert!(>::contains_key( + execution_header_update.execution_header.block_hash + )); + assert_err!( + EthereumBeaconClient::submit_execution_header( + RuntimeOrigin::signed(1), + Box::new(skipped_block_execution_header_update) + ), + Error::::ExecutionHeaderSkippedBlock + ); + }); +} + +#[test] +fn submit_execution_header_update_that_is_also_finalized_header_which_is_not_stored() { + let checkpoint = load_checkpoint_update_fixture(); + let finalized_header_update = load_finalized_header_update_fixture(); + let mut execution_header_update = load_execution_header_update_fixture(); + execution_header_update.ancestry_proof = None; + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(finalized_header_update) + )); + assert_err!( + EthereumBeaconClient::submit_execution_header( + RuntimeOrigin::signed(1), + Box::new(execution_header_update) + ), + Error::::ExpectedFinalizedHeaderNotStored + ); + }); +} + +#[test] +fn submit_execution_header_update_that_is_also_finalized_header_which_is_stored_but_slots_dont_match( +) { + let checkpoint = load_checkpoint_update_fixture(); + let finalized_header_update = load_finalized_header_update_fixture(); + let mut execution_header_update = load_execution_header_update_fixture(); + execution_header_update.ancestry_proof = None; + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(finalized_header_update) + )); + + let block_root: H256 = execution_header_update.header.hash_tree_root().unwrap(); + + >::insert( + block_root, + CompactBeaconState { + slot: execution_header_update.header.slot + 1, + block_roots_root: Default::default(), + }, + ); + LatestFinalizedBlockRoot::::set(block_root); + + assert_err!( + EthereumBeaconClient::submit_execution_header( + RuntimeOrigin::signed(1), + Box::new(execution_header_update) + ), + Error::::ExpectedFinalizedHeaderNotStored + ); + }); +} + +#[test] +fn submit_execution_header_not_finalized() { + let checkpoint = load_checkpoint_update_fixture(); + let finalized_header_update = load_finalized_header_update_fixture(); + let update = load_execution_header_update_fixture(); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + assert_ok!(EthereumBeaconClient::submit( + RuntimeOrigin::signed(1), + Box::new(finalized_header_update) + )); + + >::mutate(>::get(), |x| { + let prev = x.unwrap(); + *x = Some(CompactBeaconState { slot: update.header.slot - 1, ..prev }); + }); + + assert_err!( + EthereumBeaconClient::submit_execution_header( + RuntimeOrigin::signed(1), + Box::new(update) + ), + Error::::HeaderNotFinalized + ); + }); +} + +/* IMPLS */ + +#[test] +fn verify_message() { + let header = get_message_verification_header(); + let (event_log, proof) = get_message_verification_payload(); + let block_hash = proof.block_hash; + + new_tester().execute_with(|| { + >::insert(block_hash, header); + assert_ok!(EthereumBeaconClient::verify(&event_log, &proof)); + }); +} + +#[test] +fn verify_message_missing_header() { + let (event_log, proof) = get_message_verification_payload(); + + new_tester().execute_with(|| { + assert_err!( + EthereumBeaconClient::verify(&event_log, &proof), + VerificationError::HeaderNotFound + ); + }); +} + +#[test] +fn verify_message_invalid_proof() { + let header = get_message_verification_header(); + let (event_log, mut proof) = get_message_verification_payload(); + proof.data.1[0] = TEST_HASH.into(); + let block_hash = proof.block_hash; + + new_tester().execute_with(|| { + >::insert(block_hash, header); + assert_err!( + EthereumBeaconClient::verify(&event_log, &proof), + VerificationError::InvalidProof + ); + }); +} + +#[test] +fn verify_message_invalid_receipts_root() { + let mut header = get_message_verification_header(); + let (event_log, proof) = get_message_verification_payload(); + let block_hash = proof.block_hash; + header.receipts_root = TEST_HASH.into(); + + new_tester().execute_with(|| { + >::insert(block_hash, header); + assert_err!( + EthereumBeaconClient::verify(&event_log, &proof), + VerificationError::InvalidProof + ); + }); +} + +#[test] +fn verify_message_invalid_log() { + let header = get_message_verification_header(); + let (mut event_log, proof) = get_message_verification_payload(); + let block_hash = proof.block_hash; + event_log.topics = vec![H256::zero(); 10]; + + new_tester().execute_with(|| { + >::insert(block_hash, header); + assert_err!( + EthereumBeaconClient::verify(&event_log, &proof), + VerificationError::InvalidLog + ); + }); +} + +#[test] +fn verify_message_receipt_does_not_contain_log() { + let header = get_message_verification_header(); + let (mut event_log, proof) = get_message_verification_payload(); + let block_hash = proof.block_hash; + event_log.data = hex!("f9013c94ee9170abfbf9421ad6dd07f6bdec9d89f2b581e0f863a01b11dcf133cc240f682dab2d3a8e4cd35c5da8c9cf99adac4336f8512584c5ada000000000000000000000000000000000000000000000000000000000000003e8a00000000000000000000000000000000000000000000000000000000000000002b8c000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000068000f000000000000000101d184c103f7acc340847eee82a0b909e3358bc28d440edffa1352b13227e8ee646f3ea37456dec70100000101001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c0000e8890423c78a0000000000000000000000000000000000000000000000000000000000000000").to_vec(); + + new_tester().execute_with(|| { + >::insert(block_hash, header); + assert_err!( + EthereumBeaconClient::verify(&event_log, &proof), + VerificationError::LogNotFound + ); + }); +} + +#[test] +fn set_operating_mode() { + let checkpoint = load_checkpoint_update_fixture(); + let update = load_finalized_header_update_fixture(); + let execution_header_update = load_execution_header_update_fixture(); + + new_tester().execute_with(|| { + assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); + + assert_ok!(EthereumBeaconClient::set_operating_mode( + RuntimeOrigin::root(), + snowbridge_core::BasicOperatingMode::Halted + )); + + assert_noop!( + EthereumBeaconClient::submit(RuntimeOrigin::signed(1), Box::new(update)), + Error::::Halted + ); + + assert_noop!( + EthereumBeaconClient::submit_execution_header( + RuntimeOrigin::signed(1), + Box::new(execution_header_update) + ), + Error::::Halted + ); + }); +} + +#[test] +fn set_operating_mode_root_only() { + new_tester().execute_with(|| { + assert_noop!( + EthereumBeaconClient::set_operating_mode( + RuntimeOrigin::signed(1), + snowbridge_core::BasicOperatingMode::Halted + ), + DispatchError::BadOrigin + ); + }); +} diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/types.rs b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/types.rs new file mode 100644 index 000000000000..5dcefea9f80f --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/types.rs @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +pub use crate::config::{ + SLOTS_PER_HISTORICAL_ROOT, SYNC_COMMITTEE_BITS_SIZE as SC_BITS_SIZE, + SYNC_COMMITTEE_SIZE as SC_SIZE, +}; +use frame_support::storage::types::OptionQuery; +use snowbridge_core::RingBufferMapImpl; + +// Specialize types based on configured sync committee size +pub type SyncCommittee = primitives::SyncCommittee; +pub type SyncCommitteePrepared = primitives::SyncCommitteePrepared; +pub type SyncAggregate = primitives::SyncAggregate; +pub type CheckpointUpdate = primitives::CheckpointUpdate; +pub type Update = primitives::Update; +pub type NextSyncCommitteeUpdate = primitives::NextSyncCommitteeUpdate; + +pub use primitives::ExecutionHeaderUpdate; + +/// ExecutionHeader ring buffer implementation +pub type ExecutionHeaderBuffer = RingBufferMapImpl< + u32, + ::MaxExecutionHeadersToKeep, + crate::ExecutionHeaderIndex, + crate::ExecutionHeaderMapping, + crate::ExecutionHeaders, + OptionQuery, +>; + +/// FinalizedState ring buffer implementation +pub(crate) type FinalizedBeaconStateBuffer = RingBufferMapImpl< + u32, + crate::MaxFinalizedHeadersToKeep, + crate::FinalizedBeaconStateIndex, + crate::FinalizedBeaconStateMapping, + crate::FinalizedBeaconState, + OptionQuery, +>; diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/weights.rs b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/weights.rs new file mode 100644 index 000000000000..69d3e809986b --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/src/weights.rs @@ -0,0 +1,68 @@ +//! Autogenerated weights for ethereum_beacon_client +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2022-09-27, STEPS: `10`, REPEAT: 10, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("/tmp/snowbridge/spec.json"), DB CACHE: 1024 + +// Executed Command: +// ./target/release/snowbridge +// benchmark +// pallet +// --chain +// /tmp/snowbridge/spec.json +// --execution=wasm +// --pallet +// ethereum_beacon_client +// --extrinsic +// * +// --steps +// 10 +// --repeat +// 10 +// --output +// pallets/ethereum-beacon-client/src/weights.rs +// --template +// templates/module-weight-template.hbs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use sp_std::marker::PhantomData; + +/// Weight functions needed for ethereum_beacon_client. +pub trait WeightInfo { + fn force_checkpoint() -> Weight; + fn submit() -> Weight; + fn submit_with_sync_committee() -> Weight; + fn submit_execution_header() -> Weight; +} + +// For backwards compatibility and tests +impl WeightInfo for () { + fn force_checkpoint() -> Weight { + Weight::from_parts(97_263_571_000_u64, 0) + .saturating_add(Weight::from_parts(0, 3501)) + .saturating_add(RocksDbWeight::get().reads(2)) + .saturating_add(RocksDbWeight::get().writes(9)) + } + fn submit() -> Weight { + Weight::from_parts(26_051_019_000_u64, 0) + .saturating_add(Weight::from_parts(0, 93857)) + .saturating_add(RocksDbWeight::get().reads(8)) + .saturating_add(RocksDbWeight::get().writes(4)) + } + fn submit_with_sync_committee() -> Weight { + Weight::from_parts(122_461_312_000_u64, 0) + .saturating_add(Weight::from_parts(0, 93857)) + .saturating_add(RocksDbWeight::get().reads(6)) + .saturating_add(RocksDbWeight::get().writes(1)) + } + fn submit_execution_header() -> Weight { + Weight::from_parts(113_158_000_u64, 0) + .saturating_add(Weight::from_parts(0, 3537)) + .saturating_add(RocksDbWeight::get().reads(5)) + .saturating_add(RocksDbWeight::get().writes(4)) + } +} diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/execution-header-update.minimal.json b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/execution-header-update.minimal.json new file mode 100644 index 000000000000..3e17c14f4adb --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/execution-header-update.minimal.json @@ -0,0 +1,43 @@ +{ + "header": { + "slot": 3622, + "proposer_index": 7, + "parent_root": "0x254c9215f6cce83e21b9776afb482181639602d3cb58cf99452a6a4a4f603930", + "state_root": "0xea98df6d30817d63f3e54ea118e2b1ba8675753c72dec1661c503d4eb43f9bdd", + "body_root": "0x765a0616a31d38e0ca2d10f6e8b234dd3d07e16aa929bcbc4de775c93f1972fd" + }, + "ancestry_proof": { + "header_branch": [ + "0x7690506882ac8c5f01d00f3ade06439259a3a0261ef5d61ec44920678b4104e6", + "0xf01aa0fdd7c9ef7b1affb7854fe8cbcc5c70643ee5b83e032faa702a0675a8cb", + "0x273a7b300b75ffa2c765af50680aa836299264f2107f38010278822313181801", + "0x30fe73a3bae6a31af32656ab759a4b67d27a213e01012b96cc4fedd0f2e77c75", + "0x7246cb3a35f13a1f0bbf907887985bb5382c45f2aa1699dbca48a0a82d5330af", + "0x5e7270e88a22dd4a905b2e76da2c8c358baeddd34de6c64a71bb1c80070ab717" + ], + "finalized_block_root": "0xa6fdc5df11c1759d11c9f0353a666715e5677e9ffd7d414e44cff0970553f1c9" + }, + "execution_header": { + "parent_hash": "0x6c9657f1267ad6040ea017ff6d02b55c4ba25cb092b8326d321dd98d01d1ee64", + "fee_recipient": "0x0000000000000000000000000000000000000000", + "state_root": "0x01f975f7cdff9b0a8844304aa59062fe18af0fef4636539312dfe20d238600ba", + "receipts_root": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "logs_bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "prev_randao": "0xcdfcab74bc26b3f4311afdc72d2d21d33a4b045187a01fa208a9d687a6d1d25c", + "block_number": 3622, + "gas_limit": 30000000, + "gas_used": 0, + "timestamp": 1685722543, + "extra_data": "0xd983010b02846765746888676f312e31392e358664617277696e", + "base_fee_per_gas": 7, + "block_hash": "0x38c80e0e26cb80730df627d32f50266bd0fe32fb12b7606300ad81aa2b4033db", + "transactions_root": "0x7ffe241ea60187fdb0187bfa22de35d1f9bed7ab061d9401fd47e34a54fbede1", + "withdrawals_root": "0x28ba1834a3a7b657460ce79fa3a1d909ab8828fd557659d4d0554a9bdbc0ec30" + }, + "execution_branch": [ + "0x005b8d55b34b4323bfd4773c28b09eb53bc87959e65411ccd23728c7e42d5ff2", + "0x336488033fe5f3ef4ccc12af07b9370b92e553e35ecb4a337a1b1c0e4afe1e0e", + "0xdb56114e00fdd4c1f85c892bf35ac9a89289aaecb1ebd0a96cde606a748b5d71", + "0x7061330dada1ba1c602ba98f647a441885460ed0db00483fea1282385dfab84b" + ] +} \ No newline at end of file diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/finalized-header-update.minimal.json b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/finalized-header-update.minimal.json new file mode 100644 index 000000000000..c6473529b10c --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/finalized-header-update.minimal.json @@ -0,0 +1,38 @@ +{ + "attested_header": { + "slot": 3640, + "proposer_index": 5, + "parent_root": "0xf062fcec9c3379a08e6add37a834b1e39af395fc343973e44957ecebbf2ecddd", + "state_root": "0xb1581cb62fe376e305e02f26463153f5dfb804d8df97ef40fc315c1bc30731ba", + "body_root": "0x98461abcc6d130b7bcb9430292c8a269ea9f01082685347e2968d892f716067c" + }, + "sync_aggregate": { + "sync_committee_bits": "0xffffffff", + "sync_committee_signature": "0x925c6e4b67890a7e28a7ca19853f88247e92014b9d233ac9058efd4f3827f0055db308debe17596e635b93727b5a851e1366ca801f30b03fdec722f45011504702a27646488b5ab5e3428fe7b4d4a50132f374612f66e45d68db27c568f96f08" + }, + "signature_slot": 3641, + "next_sync_committee_update": null, + "finalized_header": { + "slot": 3624, + "proposer_index": 7, + "parent_root": "0x7690506882ac8c5f01d00f3ade06439259a3a0261ef5d61ec44920678b4104e6", + "state_root": "0x3726ebb8d9973977a71a8389caf5fc5830eeb8cd4fdfbbc7b0c4e6ca3e6a4090", + "body_root": "0x0f9a3f0fa5a4ffaf7c10504c86f23e7d554366ffd069fe958a160b253c3fd409" + }, + "finality_branch": [ + "0xc501000000000000000000000000000000000000000000000000000000000000", + "0x10c726fac935bf9657cc7476d3cfa7bedec5983dcfb59e8a7df6d0a619e108d7", + "0x83c3d5360d254f4a44be712c1f433e88e810b6d1e0e789e90bada9e36126b857", + "0x97245fa01a89a6d7b4542cd731fef699f58b2bbaabdd6f641334c9e9eeae3a20", + "0xc3d19c773f66ab94bc2106d5e75a3205398dd6e94b6f8a5716f347741eb9fc5a", + "0x9e5040e56d765c1add56779a716be7497be27cba37f866cd8d34418d55e48715" + ], + "block_roots_root": "0x29a54625749fa25f9e36df14a3baa335c58246bba2f8c7eb8b1ec2e4908e2fd0", + "block_roots_branch": [ + "0x53616f9298818a8423c98adc47c92aaf82f0c5c911dc4ee5f88ba6d3022341c1", + "0x5d2f1c4bce6f63f26cbe3fbf480281c04a6b14bea74350a88ee945354ecbd79d", + "0x8333eefc7eaa4d10091e2014b3aae2bf6bd2d10c22c67100e189f8ab6caab261", + "0x3edfa69130bc193dec47c27a5903f03d5262b75899b69c0e95ac1816a664a3e7", + "0x5e046000f85aede8d4c28140b27778488d4ad21b1e16e345055d07ee53f2711b" + ] +} \ No newline at end of file diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/initial-checkpoint.minimal.json b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/initial-checkpoint.minimal.json new file mode 100644 index 000000000000..a7e48f459019 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/initial-checkpoint.minimal.json @@ -0,0 +1,62 @@ +{ + "header": { + "slot": 3616, + "proposer_index": 7, + "parent_root": "0x6c5e8c7b32b7bfbb250fa8fd7bc348d7325fb2bfc869e4c506af6802fcad87f4", + "state_root": "0x3e467e3429a1ae36572fe3fe1c953381242e950254cf97c7527a8cea8aa6c9de", + "body_root": "0x7da749680d2b0b4f779047fcfe7d0c13d247f6d23478817fe9c6fbe07993adb2" + }, + "current_sync_committee": { + "pubkeys": [ + "0xab0bdda0f85f842f431beaccf1250bf1fd7ba51b4100fd64364b6401fda85bb0069b3e715b58819684e7fc0b10a72a34", + "0x81283b7a20e1ca460ebd9bbd77005d557370cabb1f9a44f530c4c4c66230f675f8df8b4c2818851aa7d77a80ca5a4a5e", + "0x9977f1c8b731a8d5558146bfb86caea26434f3c5878b589bf280a42c9159e700e9df0e4086296c20b011d2e78c27d373", + "0xa3a32b0f8b4ddb83f1a0a853d81dd725dfe577d4f4c3db8ece52ce2b026eca84815c1a7e8e92a4de3d755733bf7e4a9b", + "0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", + "0xa8d4c7c27795a725961317ef5953a7032ed6d83739db8b0e8a72353d1b8b4439427f7efa2c89caa03cc9f28f8cbab8ac", + "0x88c141df77cd9d8d7a71a75c826c41a9c9f03c6ee1b180f3e7852f6a280099ded351b58d66e653af8e42816a4d8f532e", + "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c", + "0xab0bdda0f85f842f431beaccf1250bf1fd7ba51b4100fd64364b6401fda85bb0069b3e715b58819684e7fc0b10a72a34", + "0x81283b7a20e1ca460ebd9bbd77005d557370cabb1f9a44f530c4c4c66230f675f8df8b4c2818851aa7d77a80ca5a4a5e", + "0x9977f1c8b731a8d5558146bfb86caea26434f3c5878b589bf280a42c9159e700e9df0e4086296c20b011d2e78c27d373", + "0xa3a32b0f8b4ddb83f1a0a853d81dd725dfe577d4f4c3db8ece52ce2b026eca84815c1a7e8e92a4de3d755733bf7e4a9b", + "0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", + "0xa8d4c7c27795a725961317ef5953a7032ed6d83739db8b0e8a72353d1b8b4439427f7efa2c89caa03cc9f28f8cbab8ac", + "0x88c141df77cd9d8d7a71a75c826c41a9c9f03c6ee1b180f3e7852f6a280099ded351b58d66e653af8e42816a4d8f532e", + "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c", + "0xab0bdda0f85f842f431beaccf1250bf1fd7ba51b4100fd64364b6401fda85bb0069b3e715b58819684e7fc0b10a72a34", + "0x81283b7a20e1ca460ebd9bbd77005d557370cabb1f9a44f530c4c4c66230f675f8df8b4c2818851aa7d77a80ca5a4a5e", + "0x9977f1c8b731a8d5558146bfb86caea26434f3c5878b589bf280a42c9159e700e9df0e4086296c20b011d2e78c27d373", + "0xa3a32b0f8b4ddb83f1a0a853d81dd725dfe577d4f4c3db8ece52ce2b026eca84815c1a7e8e92a4de3d755733bf7e4a9b", + "0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", + "0xa8d4c7c27795a725961317ef5953a7032ed6d83739db8b0e8a72353d1b8b4439427f7efa2c89caa03cc9f28f8cbab8ac", + "0x88c141df77cd9d8d7a71a75c826c41a9c9f03c6ee1b180f3e7852f6a280099ded351b58d66e653af8e42816a4d8f532e", + "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c", + "0xab0bdda0f85f842f431beaccf1250bf1fd7ba51b4100fd64364b6401fda85bb0069b3e715b58819684e7fc0b10a72a34", + "0x81283b7a20e1ca460ebd9bbd77005d557370cabb1f9a44f530c4c4c66230f675f8df8b4c2818851aa7d77a80ca5a4a5e", + "0x9977f1c8b731a8d5558146bfb86caea26434f3c5878b589bf280a42c9159e700e9df0e4086296c20b011d2e78c27d373", + "0xa3a32b0f8b4ddb83f1a0a853d81dd725dfe577d4f4c3db8ece52ce2b026eca84815c1a7e8e92a4de3d755733bf7e4a9b", + "0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", + "0xa8d4c7c27795a725961317ef5953a7032ed6d83739db8b0e8a72353d1b8b4439427f7efa2c89caa03cc9f28f8cbab8ac", + "0x88c141df77cd9d8d7a71a75c826c41a9c9f03c6ee1b180f3e7852f6a280099ded351b58d66e653af8e42816a4d8f532e", + "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c" + ], + "aggregate_pubkey": "0x8fe11476a05750c52618deb79918e2e674f56dfbf12dbce55ae4386d108e8a1e83c6326f5957e2ef19137582ce270dc6" + }, + "current_sync_committee_branch": [ + "0x46af3f54acbea439b63aa5bb699c8f25ff584b23912366788f7c8e95011ce324", + "0x41dcb71ec3b3940399118d28e09fdc58a8e33b818b8c5cbb933c59929504ca08", + "0xfa53febb29348e3493a50c0e7c6d35796bf69c54dfc6f42f7600612789d0ed6d", + "0x5e7ea1693066b604fc60d4657b43e7a4aafd3f4f54d9a740d2abe765e92d8385", + "0x16c9bca64a82e80c23817bfec345d088e0adc3865e392965c1244f97979f816a" + ], + "validators_root": "0x270d43e74ce340de4bca2b1936beca0f4f5408d9e78aec4850920baf659d5b69", + "block_roots_root": "0x00f6bbdeac1e1a922a9bf0e78720c0bffe558d8195e8ede8cb72bbd295f242f2", + "block_roots_branch": [ + "0x7a61086fb9e53ab4dd87243d6288c51793696168a73773277630da5b20bf6091", + "0x60733905cdc5dd65d05161bb3138eecc47d6d6057ab36b0d36cf5a3200484143", + "0x86d7de634ae45de5b3cbbc562dd976de7d06a3d96f83147413536e6b108c7a39", + "0x0ada571c9e0da6fce8dd13e6d9ce173768521ac32e0af456634556176789fa6e", + "0x2341538fd0aafbc1ff0f513545e5dcd4b8905dc9e00d6173480c18a4e8086ebc" + ] +} \ No newline at end of file diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/next-finalized-header-update.minimal.json b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/next-finalized-header-update.minimal.json new file mode 100755 index 000000000000..8f1ddc827c1f --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/next-finalized-header-update.minimal.json @@ -0,0 +1,38 @@ +{ + "attested_header": { + "slot": 3696, + "proposer_index": 1, + "parent_root": "0x04a63c5dfb726c31a32a72c1c426ff89e21363223d7096486b629f1d58abe5d8", + "state_root": "0xbe20e69420cbf9400224ec5edeb0843776a2ccf945e9a3ba9311ae812cad1e30", + "body_root": "0x1d2acd1748f1c58096d1edc8badd3a1d7e1dc3c33bcb9229e4c03f3a84efeadb" + }, + "sync_aggregate": { + "sync_committee_bits": "0xffffffff", + "sync_committee_signature": "0xafa79bc0f3c731ab1eb6aeafc582a7dd1c100ea471df3af6ff485b58661b3ef8077264dea0b60df9aec2d3ca8ddab6770fc9d061462e5a6dc718146085425f863d00921c42413805cb5b4c5175f36f2087cfed740bb7d57e8d5b48352643cd5b" + }, + "signature_slot": 3697, + "next_sync_committee_update": null, + "finalized_header": { + "slot": 3680, + "proposer_index": 7, + "parent_root": "0x4d8f4fc47ad3eb045bd20cae13af6df02f96a3f8d7c8a285190ba10cfe2b84cf", + "state_root": "0xd498766d77277fe16a6a4609ab3ac3a6e9887d162d8dfffdfc9cc4ae833e4127", + "body_root": "0x9ba73bc9a4907cac0b887550e2b01a63dcc70473753ffcc243d33394cc64b4c0" + }, + "finality_branch": [ + "0xcc01000000000000000000000000000000000000000000000000000000000000", + "0x10c726fac935bf9657cc7476d3cfa7bedec5983dcfb59e8a7df6d0a619e108d7", + "0x142061c4bc3673bf774cb8c7b6085057bd0ca85672b43afa2d9581b0b6a44e54", + "0x48b8cd8ca9d9563e30c1cca2a854cd7f75eb4cb013d10809b3138a72d94ea0c5", + "0x9b39523d05013ac7cbb9f43e5d6f9dc033b12aa1d6d6edd994ddc4f5efe7be9d", + "0x066c9aa26107bc8cb28bc73e518da6cc865ec1d67516b6ca24663b6b7ae3cb21" + ], + "block_roots_root": "0xb15aa2483811d8c5616cb93710f4fcb809d97443caac9de163f943a30f385db6", + "block_roots_branch": [ + "0xf7a43ad317417daa4c2a1e93c54895895a824ef1e43320eb44eab16673da5a61", + "0xe4b8d640660f765c2ef4dc886025dc8e54c6e70b66192582f42837ed5e9d8d41", + "0x841f113dc81e76419b6cdec8b0cf2fc20f9381492ed3c79e9b49179b4d3eacbc", + "0xeb5fdc4d8b5282b653ecbc9caa93bcfe482f6d6a32cbb0d9eb011bef947579bb", + "0x1f328cc5640efb191ae6aa86223b1aa9d083b26ac3e1fa3c071327bb09dc5727" + ] +} \ No newline at end of file diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/next-sync-committee-update.minimal.json b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/next-sync-committee-update.minimal.json new file mode 100755 index 000000000000..8f1c8b9ce21c --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/next-sync-committee-update.minimal.json @@ -0,0 +1,83 @@ +{ + "attested_header": { + "slot": 3664, + "proposer_index": 4, + "parent_root": "0x15ac23a0c16bfa81e8595621118040c3e6cbddd4b09bae6fb39ba5fefd0258e8", + "state_root": "0x6fb81aa3827e7d580bb05b4df2686c9a49508bde2f8342fd75be609a23dd8362", + "body_root": "0x9906a1ae8065d268f8acb7f1b3119408d2f7f8e6e0764370c16ea3d15134981f" + }, + "sync_aggregate": { + "sync_committee_bits": "0xffffffff", + "sync_committee_signature": "0xa9b5584ec9290a4ac6c5616639d031f9ab1064d63b4889f1da52f6f4d66b645fca48bbe2fe8484adb0c05c647edd694d0340cf684b8ccf8e34c6d8cf447cfcfdcb856f5abdcfd85ada5a4a04d4c8f6f40c6e99308893c3941485a436d6c8e5f7" + }, + "signature_slot": 3665, + "next_sync_committee_update": { + "next_sync_committee": { + "pubkeys": [ + "0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", + "0x81283b7a20e1ca460ebd9bbd77005d557370cabb1f9a44f530c4c4c66230f675f8df8b4c2818851aa7d77a80ca5a4a5e", + "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c", + "0xa3a32b0f8b4ddb83f1a0a853d81dd725dfe577d4f4c3db8ece52ce2b026eca84815c1a7e8e92a4de3d755733bf7e4a9b", + "0xa8d4c7c27795a725961317ef5953a7032ed6d83739db8b0e8a72353d1b8b4439427f7efa2c89caa03cc9f28f8cbab8ac", + "0x88c141df77cd9d8d7a71a75c826c41a9c9f03c6ee1b180f3e7852f6a280099ded351b58d66e653af8e42816a4d8f532e", + "0xab0bdda0f85f842f431beaccf1250bf1fd7ba51b4100fd64364b6401fda85bb0069b3e715b58819684e7fc0b10a72a34", + "0x9977f1c8b731a8d5558146bfb86caea26434f3c5878b589bf280a42c9159e700e9df0e4086296c20b011d2e78c27d373", + "0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", + "0x81283b7a20e1ca460ebd9bbd77005d557370cabb1f9a44f530c4c4c66230f675f8df8b4c2818851aa7d77a80ca5a4a5e", + "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c", + "0xa3a32b0f8b4ddb83f1a0a853d81dd725dfe577d4f4c3db8ece52ce2b026eca84815c1a7e8e92a4de3d755733bf7e4a9b", + "0xa8d4c7c27795a725961317ef5953a7032ed6d83739db8b0e8a72353d1b8b4439427f7efa2c89caa03cc9f28f8cbab8ac", + "0x88c141df77cd9d8d7a71a75c826c41a9c9f03c6ee1b180f3e7852f6a280099ded351b58d66e653af8e42816a4d8f532e", + "0xab0bdda0f85f842f431beaccf1250bf1fd7ba51b4100fd64364b6401fda85bb0069b3e715b58819684e7fc0b10a72a34", + "0x9977f1c8b731a8d5558146bfb86caea26434f3c5878b589bf280a42c9159e700e9df0e4086296c20b011d2e78c27d373", + "0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", + "0x81283b7a20e1ca460ebd9bbd77005d557370cabb1f9a44f530c4c4c66230f675f8df8b4c2818851aa7d77a80ca5a4a5e", + "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c", + "0xa3a32b0f8b4ddb83f1a0a853d81dd725dfe577d4f4c3db8ece52ce2b026eca84815c1a7e8e92a4de3d755733bf7e4a9b", + "0xa8d4c7c27795a725961317ef5953a7032ed6d83739db8b0e8a72353d1b8b4439427f7efa2c89caa03cc9f28f8cbab8ac", + "0x88c141df77cd9d8d7a71a75c826c41a9c9f03c6ee1b180f3e7852f6a280099ded351b58d66e653af8e42816a4d8f532e", + "0xab0bdda0f85f842f431beaccf1250bf1fd7ba51b4100fd64364b6401fda85bb0069b3e715b58819684e7fc0b10a72a34", + "0x9977f1c8b731a8d5558146bfb86caea26434f3c5878b589bf280a42c9159e700e9df0e4086296c20b011d2e78c27d373", + "0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", + "0x81283b7a20e1ca460ebd9bbd77005d557370cabb1f9a44f530c4c4c66230f675f8df8b4c2818851aa7d77a80ca5a4a5e", + "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c", + "0xa3a32b0f8b4ddb83f1a0a853d81dd725dfe577d4f4c3db8ece52ce2b026eca84815c1a7e8e92a4de3d755733bf7e4a9b", + "0xa8d4c7c27795a725961317ef5953a7032ed6d83739db8b0e8a72353d1b8b4439427f7efa2c89caa03cc9f28f8cbab8ac", + "0x88c141df77cd9d8d7a71a75c826c41a9c9f03c6ee1b180f3e7852f6a280099ded351b58d66e653af8e42816a4d8f532e", + "0xab0bdda0f85f842f431beaccf1250bf1fd7ba51b4100fd64364b6401fda85bb0069b3e715b58819684e7fc0b10a72a34", + "0x9977f1c8b731a8d5558146bfb86caea26434f3c5878b589bf280a42c9159e700e9df0e4086296c20b011d2e78c27d373" + ], + "aggregate_pubkey": "0x8fe11476a05750c52618deb79918e2e674f56dfbf12dbce55ae4386d108e8a1e83c6326f5957e2ef19137582ce270dc6" + }, + "next_sync_committee_branch": [ + "0x46af3f54acbea439b63aa5bb699c8f25ff584b23912366788f7c8e95011ce324", + "0x5b118fe110ee4a1b0cf9823bc189fb38eb55a7b49adbdafcf466ec7cd4b7fd68", + "0xc2f12fb91a61abedb47f62a98258960edca21f31494cdf59b47a1c721e3e98f8", + "0x16fdfd5e6b591b3140a76efa4593a9c4d105b9e5c62d6f44edbd24790657be50", + "0xc8175ab66690cc94c0a24452754addd62a06948de5db9814e813437a130de452" + ] + }, + "finalized_header": { + "slot": 3648, + "proposer_index": 1, + "parent_root": "0x991ee98a70e8f90bdd61d0f5554e53d37473e75e16af171f6d88f27d20223dae", + "state_root": "0x59b04d660ac772005a13a7dc1d5f99bb0d0292f3c422f04f7365198d70dd30de", + "body_root": "0x5151f035e146258e7327ad9cf1df13f8ddec7a7842c19993cf739358717b5565" + }, + "finality_branch": [ + "0xc801000000000000000000000000000000000000000000000000000000000000", + "0x10c726fac935bf9657cc7476d3cfa7bedec5983dcfb59e8a7df6d0a619e108d7", + "0x142061c4bc3673bf774cb8c7b6085057bd0ca85672b43afa2d9581b0b6a44e54", + "0xc2f12fb91a61abedb47f62a98258960edca21f31494cdf59b47a1c721e3e98f8", + "0x16fdfd5e6b591b3140a76efa4593a9c4d105b9e5c62d6f44edbd24790657be50", + "0xc8175ab66690cc94c0a24452754addd62a06948de5db9814e813437a130de452" + ], + "block_roots_root": "0xe6e2adaaad45363d7112945ef670e21c66bcb3276dc450962ade1e8950230380", + "block_roots_branch": [ + "0x386ede102258966d4c23031c5a02de2af8180d475c4c1716b07fb5b9f142a817", + "0x35e6c89bc38d993a1957f8a9fb1fbeab7420688091ba2cd7ee7b19b7e187f7d6", + "0x99249309825cafef7e694c09c4fdf95eb4b1e8743d3b23f6959d9980ad2d69b0", + "0x5e028d1d905db6430f0ce4aafbc78f442047ec3a132b4e69557fdf804a4cfbf3", + "0xd34afeab37851937920243683a1c926c41c626aacb145718fce755782d4996dd" + ] +} \ No newline at end of file diff --git a/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/sync-committee-update.minimal.json b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/sync-committee-update.minimal.json new file mode 100644 index 000000000000..a962a0c87c4c --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/ethereum-beacon-client/tests/fixtures/sync-committee-update.minimal.json @@ -0,0 +1,83 @@ +{ + "attested_header": { + "slot": 3600, + "proposer_index": 7, + "parent_root": "0xdf60c2d58beccd89678b9267c689e9ba1cf1d58ce5114ad5c16e8341459cfd75", + "state_root": "0x023f14c7a38ef4d6ec19b522edfb427c6b70c6ffbd8610ca802dd1491c92c852", + "body_root": "0x0f78a1c45e42711efc5fb7b7f6238be1bee9273f7c44ff6892d815858bb77e25" + }, + "sync_aggregate": { + "sync_committee_bits": "0xffffffff", + "sync_committee_signature": "0xa4dd8f0991de88ca6f81476f72f48cdb67b9414ad7bf6bba37f627c5ec84dd2c2ebc12cddd5d2e7c927276cee2d3d144158b4c067db3e9911fe52fe1875b14c93f90e4eb57bf5e8f0e6e6effe22f9ba076f30207e0ec683354961ae8e9779556" + }, + "signature_slot": 3601, + "next_sync_committee_update": { + "next_sync_committee": { + "pubkeys": [ + "0xab0bdda0f85f842f431beaccf1250bf1fd7ba51b4100fd64364b6401fda85bb0069b3e715b58819684e7fc0b10a72a34", + "0xa8d4c7c27795a725961317ef5953a7032ed6d83739db8b0e8a72353d1b8b4439427f7efa2c89caa03cc9f28f8cbab8ac", + "0xa3a32b0f8b4ddb83f1a0a853d81dd725dfe577d4f4c3db8ece52ce2b026eca84815c1a7e8e92a4de3d755733bf7e4a9b", + "0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", + "0x88c141df77cd9d8d7a71a75c826c41a9c9f03c6ee1b180f3e7852f6a280099ded351b58d66e653af8e42816a4d8f532e", + "0x81283b7a20e1ca460ebd9bbd77005d557370cabb1f9a44f530c4c4c66230f675f8df8b4c2818851aa7d77a80ca5a4a5e", + "0x9977f1c8b731a8d5558146bfb86caea26434f3c5878b589bf280a42c9159e700e9df0e4086296c20b011d2e78c27d373", + "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c", + "0xab0bdda0f85f842f431beaccf1250bf1fd7ba51b4100fd64364b6401fda85bb0069b3e715b58819684e7fc0b10a72a34", + "0xa8d4c7c27795a725961317ef5953a7032ed6d83739db8b0e8a72353d1b8b4439427f7efa2c89caa03cc9f28f8cbab8ac", + "0xa3a32b0f8b4ddb83f1a0a853d81dd725dfe577d4f4c3db8ece52ce2b026eca84815c1a7e8e92a4de3d755733bf7e4a9b", + "0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", + "0x88c141df77cd9d8d7a71a75c826c41a9c9f03c6ee1b180f3e7852f6a280099ded351b58d66e653af8e42816a4d8f532e", + "0x81283b7a20e1ca460ebd9bbd77005d557370cabb1f9a44f530c4c4c66230f675f8df8b4c2818851aa7d77a80ca5a4a5e", + "0x9977f1c8b731a8d5558146bfb86caea26434f3c5878b589bf280a42c9159e700e9df0e4086296c20b011d2e78c27d373", + "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c", + "0xab0bdda0f85f842f431beaccf1250bf1fd7ba51b4100fd64364b6401fda85bb0069b3e715b58819684e7fc0b10a72a34", + "0xa8d4c7c27795a725961317ef5953a7032ed6d83739db8b0e8a72353d1b8b4439427f7efa2c89caa03cc9f28f8cbab8ac", + "0xa3a32b0f8b4ddb83f1a0a853d81dd725dfe577d4f4c3db8ece52ce2b026eca84815c1a7e8e92a4de3d755733bf7e4a9b", + "0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", + "0x88c141df77cd9d8d7a71a75c826c41a9c9f03c6ee1b180f3e7852f6a280099ded351b58d66e653af8e42816a4d8f532e", + "0x81283b7a20e1ca460ebd9bbd77005d557370cabb1f9a44f530c4c4c66230f675f8df8b4c2818851aa7d77a80ca5a4a5e", + "0x9977f1c8b731a8d5558146bfb86caea26434f3c5878b589bf280a42c9159e700e9df0e4086296c20b011d2e78c27d373", + "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c", + "0xab0bdda0f85f842f431beaccf1250bf1fd7ba51b4100fd64364b6401fda85bb0069b3e715b58819684e7fc0b10a72a34", + "0xa8d4c7c27795a725961317ef5953a7032ed6d83739db8b0e8a72353d1b8b4439427f7efa2c89caa03cc9f28f8cbab8ac", + "0xa3a32b0f8b4ddb83f1a0a853d81dd725dfe577d4f4c3db8ece52ce2b026eca84815c1a7e8e92a4de3d755733bf7e4a9b", + "0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", + "0x88c141df77cd9d8d7a71a75c826c41a9c9f03c6ee1b180f3e7852f6a280099ded351b58d66e653af8e42816a4d8f532e", + "0x81283b7a20e1ca460ebd9bbd77005d557370cabb1f9a44f530c4c4c66230f675f8df8b4c2818851aa7d77a80ca5a4a5e", + "0x9977f1c8b731a8d5558146bfb86caea26434f3c5878b589bf280a42c9159e700e9df0e4086296c20b011d2e78c27d373", + "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c" + ], + "aggregate_pubkey": "0x8fe11476a05750c52618deb79918e2e674f56dfbf12dbce55ae4386d108e8a1e83c6326f5957e2ef19137582ce270dc6" + }, + "next_sync_committee_branch": [ + "0x1446606d0129c324a4ea374bd29a625175e0659512cd8650097e0a9c38ce6379", + "0xd92466c7e9a53b7b55f4fdb151746a3058931d7559b7e84e7b15384ddc903ca0", + "0x9fd10c3f68b75cfd3ebd2af0d4e2cbbfbe120e0b5423dde89ff0f743c7a4f937", + "0x1ed6aac0ab29a883de2bb2e3579ad4d6807ddcf3db8afcaf0ae25a076ac9a5f4", + "0xf17a840df410a15f0e4e48abf521c29ad0d296d3fb4e8b847ea37f2cc8236f1f" + ] + }, + "finalized_header": { + "slot": 3584, + "proposer_index": 1, + "parent_root": "0x91c285af2ec25d485310391afe667108b787ec570cdbb0e3fd87b1e0e2c47bd7", + "state_root": "0xccc4baf90024e035f1252520d2f2ef1e50f840ff0ecc8e6e365721e083871a32", + "body_root": "0x91df5e0077434aad609aaa7e030005cee77cca83868ffc2724e5befe9a3f6a02" + }, + "finality_branch": [ + "0xc001000000000000000000000000000000000000000000000000000000000000", + "0x10c726fac935bf9657cc7476d3cfa7bedec5983dcfb59e8a7df6d0a619e108d7", + "0x83c3d5360d254f4a44be712c1f433e88e810b6d1e0e789e90bada9e36126b857", + "0x9fd10c3f68b75cfd3ebd2af0d4e2cbbfbe120e0b5423dde89ff0f743c7a4f937", + "0x1ed6aac0ab29a883de2bb2e3579ad4d6807ddcf3db8afcaf0ae25a076ac9a5f4", + "0xf17a840df410a15f0e4e48abf521c29ad0d296d3fb4e8b847ea37f2cc8236f1f" + ], + "block_roots_root": "0x9eab8a05c396a29c32f4f8ac9654fc0fb7cd97ec659236392ede48951a794505", + "block_roots_branch": [ + "0x5c175efdbafacdfdab21c93a318b0e8e2291a5a86c40b1fc564f91ad33c106d4", + "0x5c1e0b76176ab033858b2835f90d5e25d708b563f77efd7d9938f0faa1c20878", + "0x7aea32464adee801e2a05c3af227f24231d3c088e3b7265a5fada9ac850549fe", + "0x9d9fca29e23c5d4ae433adf17e7fd9a0e4d1b09b68f5c45e7ca1b13ebe4a9e98", + "0x6b35238f188021c859d6b317457ebb6fe4cf362cab35c988010cb1343eabbfc5" + ] +} \ No newline at end of file diff --git a/bridges/snowbridge/parachain/pallets/inbound-queue/Cargo.toml b/bridges/snowbridge/parachain/pallets/inbound-queue/Cargo.toml new file mode 100644 index 000000000000..f9e4d20be0fd --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/inbound-queue/Cargo.toml @@ -0,0 +1,93 @@ +[package] +name = "snowbridge-inbound-queue" +description = "Snowbridge Inbound Queue" +version = "0.1.1" +edition = "2021" +authors = ["Snowfork "] +repository = "https://github.com/Snowfork/snowbridge" +license = "Apache-2.0" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +serde = { version = "1.0.188", optional = true } +codec = { version = "3.6.1", package = "parity-scale-codec", default-features = false, features = ["derive"] } +scale-info = { version = "2.9.0", default-features = false, features = ["derive"] } +hex-literal = { version = "0.4.1", optional = true } +log = { version = "0.4.20", default-features = false } +alloy-primitives = { version = "0.4.2", default-features = false, features = ["rlp"] } +alloy-sol-types = { version = "0.4.2", default-features = false } +alloy-rlp = { version = "0.3.3", default-features = false, features = ["derive"] } +num-traits = { version = "0.2.16", default-features = false } + +frame-benchmarking = { path = "../../../../../substrate/frame/benchmarking", default-features = false, optional = true } +frame-support = { path = "../../../../../substrate/frame/support", default-features = false } +frame-system = { path = "../../../../../substrate/frame/system", default-features = false } +pallet-balances = { path = "../../../../../substrate/frame/balances", default-features = false } +sp-core = { path = "../../../../../substrate/primitives/core", default-features = false } +sp-std = { path = "../../../../../substrate/primitives/std", default-features = false } +sp-io = { path = "../../../../../substrate/primitives/io", default-features = false } +sp-runtime = { path = "../../../../../substrate/primitives/runtime", default-features = false } + +xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false } +xcm-builder = { package = "staging-xcm-builder", path = "../../../../../polkadot/xcm/xcm-builder", default-features = false } + +snowbridge-core = { path = "../../primitives/core", default-features = false } +snowbridge-ethereum = { path = "../../primitives/ethereum", default-features = false } +snowbridge-router-primitives = { path = "../../primitives/router", default-features = false } +snowbridge-beacon-primitives = { path = "../../primitives/beacon", default-features = false, optional = true } + +[dev-dependencies] +frame-benchmarking = { path = "../../../../../substrate/frame/benchmarking" } +sp-keyring = { path = "../../../../../substrate/primitives/keyring" } +snowbridge-beacon-primitives = { path = "../../primitives/beacon" } +snowbridge-ethereum-beacon-client = { path = "../../pallets/ethereum-beacon-client" } +hex-literal = { version = "0.4.1" } + +[features] +default = ["std"] +std = [ + "alloy-primitives/std", + "alloy-rlp/std", + "alloy-sol-types/std", + "codec/std", + "frame-benchmarking/std", + "frame-support/std", + "frame-system/std", + "log/std", + "num-traits/std", + "pallet-balances/std", + "scale-info/std", + "serde", + "snowbridge-core/std", + "snowbridge-ethereum/std", + "snowbridge-router-primitives/std", + "sp-core/std", + "sp-io/std", + "sp-runtime/std", + "sp-std/std", + "xcm-builder/std", + "xcm/std", +] +runtime-benchmarks = [ + "frame-benchmarking", + "frame-benchmarking/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "hex-literal", + "pallet-balances/runtime-benchmarks", + "snowbridge-beacon-primitives", + "snowbridge-core/runtime-benchmarks", + "snowbridge-ethereum-beacon-client/runtime-benchmarks", + "snowbridge-router-primitives/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", + "xcm-builder/runtime-benchmarks", +] +try-runtime = [ + "frame-support/try-runtime", + "frame-system/try-runtime", + "pallet-balances/try-runtime", + "snowbridge-ethereum-beacon-client/try-runtime", + "sp-runtime/try-runtime", +] diff --git a/bridges/snowbridge/parachain/pallets/inbound-queue/src/benchmarking/fixtures.rs b/bridges/snowbridge/parachain/pallets/inbound-queue/src/benchmarking/fixtures.rs new file mode 100644 index 000000000000..4f2382d072ab --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/inbound-queue/src/benchmarking/fixtures.rs @@ -0,0 +1,40 @@ +use hex_literal::hex; +use snowbridge_beacon_primitives::CompactExecutionHeader; +use snowbridge_core::inbound::{Log, Message, Proof}; +use sp_std::vec; + +pub struct InboundQueueTest { + pub execution_header: CompactExecutionHeader, + pub message: Message, +} + +pub fn make_create_message() -> InboundQueueTest { + InboundQueueTest{ + execution_header: CompactExecutionHeader{ + parent_hash: hex!("b5608f0af7c3b6fe5c593772fc25436b8d6549eb236adb0855c6ad33e0004e04").into(), + block_number: 115, + state_root: hex!("47ed174789836c622499d9659a4ac32c3b91a7b15642d39b0a11b82ff23995c1").into(), + receipts_root: hex!("42c08b5303fcdf9e49c833fe5f1182cdbc8206bf8aec581125fc34aba11e1f1a").into(), + }, + message: Message { + event_log: Log { + address: hex!("eda338e4dc46038493b885327842fd3e301cab39").into(), + topics: vec![ + hex!("7153f9357c8ea496bba60bf82e67143e27b64462b49041f8e689e1b05728f84f").into(), + hex!("c173fac324158e77fb5840738a1a541f633cbec8884c6a601c567d2b376a0539").into(), + hex!("5f7060e971b0dc81e63f0aa41831091847d97c1a4693ac450cc128c7214e65e0").into(), + ], + data: hex!("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002e00a736aa00000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7d00e40b54020000000000000000000000000000000000000000000000000000000000").into(), + }, + proof: Proof { + block_hash: hex!("add15f439c8a57fe375d0a679870b1359921d70cb0e3e44f0dd3e272849f4097").into(), + tx_index: 0, + data: (vec![ + hex!("42c08b5303fcdf9e49c833fe5f1182cdbc8206bf8aec581125fc34aba11e1f1a").to_vec(), + ], vec![ + hex!("f9028e822080b9028802f90284018301ed20b9010000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000080000000000000000000000000000004000000000080000000000000000000000000000000000010100000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000040004000000000000002000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000200000000000010f90179f85894eda338e4dc46038493b885327842fd3e301cab39e1a0f78bb28d4b1d7da699e5c0bc2be29c2b04b5aab6aacf6298fe5304f9db9c6d7ea000000000000000000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7df9011c94eda338e4dc46038493b885327842fd3e301cab39f863a07153f9357c8ea496bba60bf82e67143e27b64462b49041f8e689e1b05728f84fa0c173fac324158e77fb5840738a1a541f633cbec8884c6a601c567d2b376a0539a05f7060e971b0dc81e63f0aa41831091847d97c1a4693ac450cc128c7214e65e0b8a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002e00a736aa00000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7d00e40b54020000000000000000000000000000000000000000000000000000000000").to_vec(), + ]), + }, + }, + } +} diff --git a/bridges/snowbridge/parachain/pallets/inbound-queue/src/benchmarking/mod.rs b/bridges/snowbridge/parachain/pallets/inbound-queue/src/benchmarking/mod.rs new file mode 100644 index 000000000000..c10de9dff2ff --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/inbound-queue/src/benchmarking/mod.rs @@ -0,0 +1,55 @@ +mod fixtures; + +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use super::*; + +use crate::Pallet as InboundQueue; +use frame_benchmarking::v2::*; +use frame_support::assert_ok; +use frame_system::RawOrigin; + +#[benchmarks] +mod benchmarks { + use super::*; + use crate::benchmarking::fixtures::make_create_message; + + #[benchmark] + fn submit() -> Result<(), BenchmarkError> { + let caller: T::AccountId = whitelisted_caller(); + + let create_message = make_create_message(); + + T::Helper::initialize_storage( + create_message.message.proof.block_hash, + create_message.execution_header, + ); + + let sovereign_account = sibling_sovereign_account::(1000u32.into()); + + let minimum_balance = T::Token::minimum_balance(); + + // So that the receiving account exists + assert_ok!(T::Token::mint_into(&caller, minimum_balance)); + // Fund the sovereign account (parachain sovereign account) so it can transfer a reward + // fee to the caller account + assert_ok!(T::Token::mint_into( + &sovereign_account, + 3_000_000_000_000u128 + .try_into() + .unwrap_or_else(|_| panic!("unable to cast sovereign account balance")), + )); + + #[block] + { + assert_ok!(InboundQueue::::submit( + RawOrigin::Signed(caller.clone()).into(), + create_message.message, + )); + } + + Ok(()) + } + + impl_benchmark_test_suite!(InboundQueue, crate::mock::new_tester(), crate::mock::Test); +} diff --git a/bridges/snowbridge/parachain/pallets/inbound-queue/src/envelope.rs b/bridges/snowbridge/parachain/pallets/inbound-queue/src/envelope.rs new file mode 100644 index 000000000000..826d535c2cb9 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/inbound-queue/src/envelope.rs @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use snowbridge_core::{inbound::Log, ChannelId}; + +use sp_core::{RuntimeDebug, H160, H256}; +use sp_std::{convert::TryFrom, prelude::*}; + +use alloy_primitives::B256; +use alloy_sol_types::{sol, SolEvent}; + +sol! { + event OutboundMessageAccepted(bytes32 indexed channel_id, uint64 nonce, bytes32 indexed message_id, bytes payload); +} + +/// An inbound message that has had its outer envelope decoded. +#[derive(Clone, RuntimeDebug)] +pub struct Envelope { + /// The address of the outbound queue on Ethereum that emitted this message as an event log + pub gateway: H160, + /// The message Channel + pub channel_id: ChannelId, + /// A nonce for enforcing replay protection and ordering. + pub nonce: u64, + /// An id for tracing the message on its route (has no role in bridge consensus) + pub message_id: H256, + /// The inner payload generated from the source application. + pub payload: Vec, +} + +#[derive(Copy, Clone, RuntimeDebug)] +pub struct EnvelopeDecodeError; + +impl TryFrom<&Log> for Envelope { + type Error = EnvelopeDecodeError; + + fn try_from(log: &Log) -> Result { + let topics: Vec = log.topics.iter().map(|x| B256::from_slice(x.as_ref())).collect(); + + let event = OutboundMessageAccepted::decode_log(topics, &log.data, true) + .map_err(|_| EnvelopeDecodeError)?; + + Ok(Self { + gateway: log.address, + channel_id: ChannelId::from(event.channel_id.as_ref()), + nonce: event.nonce, + message_id: H256::from(event.message_id.as_ref()), + payload: event.payload, + }) + } +} diff --git a/bridges/snowbridge/parachain/pallets/inbound-queue/src/lib.rs b/bridges/snowbridge/parachain/pallets/inbound-queue/src/lib.rs new file mode 100644 index 000000000000..834e805fbef5 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/inbound-queue/src/lib.rs @@ -0,0 +1,342 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! Inbound Queue +//! +//! # Overview +//! +//! Receives messages emitted by the Gateway contract on Ethereum, whereupon they are verified, +//! translated to XCM, and finally sent to their final destination parachain. +//! +//! The message relayers are rewarded using native currency from the sovereign account of the +//! destination parachain. +//! +//! # Extrinsics +//! +//! ## Governance +//! +//! * [`Call::set_operating_mode`]: Set the operating mode of the pallet. Can be used to disable +//! processing of inbound messages. +//! +//! ## Message Submission +//! +//! * [`Call::submit`]: Submit a message for verification and dispatch the final destination +//! parachain. +#![cfg_attr(not(feature = "std"), no_std)] + +mod envelope; + +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; + +#[cfg(feature = "runtime-benchmarks")] +use snowbridge_beacon_primitives::CompactExecutionHeader; + +pub mod weights; + +#[cfg(test)] +mod mock; + +#[cfg(test)] +mod test; + +use codec::{Decode, DecodeAll, Encode}; +use envelope::Envelope; +use frame_support::{ + traits::{ + fungible::{Inspect, Mutate}, + tokens::{Fortitude, Precision, Preservation}, + }, + weights::WeightToFee, + PalletError, +}; +use frame_system::ensure_signed; +use scale_info::TypeInfo; +use sp_core::{H160, H256}; +use sp_std::{convert::TryFrom, vec}; +use xcm::prelude::{ + send_xcm, Instruction::SetTopic, Junction::*, Junctions::*, MultiLocation, + SendError as XcmpSendError, SendXcm, Xcm, XcmHash, +}; + +use snowbridge_core::{ + inbound::{Message, VerificationError, Verifier}, + sibling_sovereign_account, BasicOperatingMode, Channel, ChannelId, ParaId, StaticLookup, +}; +use snowbridge_router_primitives::{ + inbound, + inbound::{ConvertMessage, ConvertMessageError}, +}; +use sp_runtime::traits::Saturating; + +pub use weights::WeightInfo; + +type BalanceOf = + <::Token as Inspect<::AccountId>>::Balance; + +pub use pallet::*; + +pub const LOG_TARGET: &str = "snowbridge-inbound-queue"; + +#[frame_support::pallet] +pub mod pallet { + use super::*; + + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use snowbridge_core::PricingParameters; + + #[pallet::pallet] + pub struct Pallet(_); + + #[cfg(feature = "runtime-benchmarks")] + pub trait BenchmarkHelper { + fn initialize_storage(block_hash: H256, header: CompactExecutionHeader); + } + + #[pallet::config] + pub trait Config: frame_system::Config { + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + + /// The verifier for inbound messages from Ethereum + type Verifier: Verifier; + + /// Message relayers are rewarded with this asset + type Token: Mutate + Inspect; + + /// XCM message sender + type XcmSender: SendXcm; + + // Address of the Gateway contract + #[pallet::constant] + type GatewayAddress: Get; + + /// Convert inbound message to XCM + type MessageConverter: ConvertMessage< + AccountId = Self::AccountId, + Balance = BalanceOf, + >; + + /// Lookup a channel descriptor + type ChannelLookup: StaticLookup; + + /// Lookup pricing parameters + type PricingParameters: Get>>; + + type WeightInfo: WeightInfo; + + #[cfg(feature = "runtime-benchmarks")] + type Helper: BenchmarkHelper; + + /// Convert a weight value into deductible balance type. + type WeightToFee: WeightToFee>; + + /// Convert a length value into deductible balance type + type LengthToFee: WeightToFee>; + + /// The upper limit here only used to estimate delivery cost + type MaxMessageSize: Get; + } + + #[pallet::hooks] + impl Hooks> for Pallet {} + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + /// A message was received from Ethereum + MessageReceived { + /// The message channel + channel_id: ChannelId, + /// The message nonce + nonce: u64, + /// ID of the XCM message which was forwarded to the final destination parachain + message_id: [u8; 32], + }, + /// Set OperatingMode + OperatingModeChanged { mode: BasicOperatingMode }, + } + + #[pallet::error] + pub enum Error { + /// Message came from an invalid outbound channel on the Ethereum side. + InvalidGateway, + /// Message has an invalid envelope. + InvalidEnvelope, + /// Message has an unexpected nonce. + InvalidNonce, + /// Message has an invalid payload. + InvalidPayload, + /// Message channel is invalid + InvalidChannel, + /// The max nonce for the type has been reached + MaxNonceReached, + /// Cannot convert location + InvalidAccountConversion, + /// Pallet is halted + Halted, + /// Message verification error, + Verification(VerificationError), + /// XCMP send failure + Send(SendError), + /// Message conversion error + ConvertMessage(ConvertMessageError), + } + + #[derive(Clone, Encode, Decode, Eq, PartialEq, Debug, TypeInfo, PalletError)] + pub enum SendError { + NotApplicable, + NotRoutable, + Transport, + DestinationUnsupported, + ExceedsMaxMessageSize, + MissingArgument, + Fees, + } + + impl From for Error { + fn from(e: XcmpSendError) -> Self { + match e { + XcmpSendError::NotApplicable => Error::::Send(SendError::NotApplicable), + XcmpSendError::Unroutable => Error::::Send(SendError::NotRoutable), + XcmpSendError::Transport(_) => Error::::Send(SendError::Transport), + XcmpSendError::DestinationUnsupported => + Error::::Send(SendError::DestinationUnsupported), + XcmpSendError::ExceedsMaxMessageSize => + Error::::Send(SendError::ExceedsMaxMessageSize), + XcmpSendError::MissingArgument => Error::::Send(SendError::MissingArgument), + XcmpSendError::Fees => Error::::Send(SendError::Fees), + } + } + } + + /// The current nonce for each channel + #[pallet::storage] + pub type Nonce = StorageMap<_, Twox64Concat, ChannelId, u64, ValueQuery>; + + /// The current operating mode of the pallet. + #[pallet::storage] + #[pallet::getter(fn operating_mode)] + pub type OperatingMode = StorageValue<_, BasicOperatingMode, ValueQuery>; + + #[pallet::call] + impl Pallet { + /// Submit an inbound message originating from the Gateway contract on Ethereum + #[pallet::call_index(0)] + #[pallet::weight(T::WeightInfo::submit())] + pub fn submit(origin: OriginFor, message: Message) -> DispatchResult { + let who = ensure_signed(origin)?; + ensure!(!Self::operating_mode().is_halted(), Error::::Halted); + + // submit message to verifier for verification + T::Verifier::verify(&message.event_log, &message.proof) + .map_err(|e| Error::::Verification(e))?; + + // Decode event log into an Envelope + let envelope = + Envelope::try_from(&message.event_log).map_err(|_| Error::::InvalidEnvelope)?; + + // Verify that the message was submitted from the known Gateway contract + ensure!(T::GatewayAddress::get() == envelope.gateway, Error::::InvalidGateway); + + // Retrieve the registered channel for this message + let channel = + T::ChannelLookup::lookup(envelope.channel_id).ok_or(Error::::InvalidChannel)?; + + // Verify message nonce + >::try_mutate(envelope.channel_id, |nonce| -> DispatchResult { + if *nonce == u64::MAX { + return Err(Error::::MaxNonceReached.into()) + } + if envelope.nonce != nonce.saturating_add(1) { + Err(Error::::InvalidNonce.into()) + } else { + *nonce = nonce.saturating_add(1); + Ok(()) + } + })?; + + // Reward relayer from the sovereign account of the destination parachain + // Expected to fail if sovereign account has no funds + let sovereign_account = sibling_sovereign_account::(channel.para_id); + let delivery_cost = Self::calculate_delivery_cost(message.encode().len() as u32); + T::Token::transfer(&sovereign_account, &who, delivery_cost, Preservation::Preserve)?; + + // Decode message into XCM + let (xcm, fee) = + match inbound::VersionedMessage::decode_all(&mut envelope.payload.as_ref()) { + Ok(message) => Self::do_convert(envelope.message_id, message)?, + Err(_) => return Err(Error::::InvalidPayload.into()), + }; + + // We embed fees for xcm execution inside the xcm program using teleports + // so we must burn the amount of the fee embedded into the XCM script. + T::Token::burn_from(&sovereign_account, fee, Precision::Exact, Fortitude::Polite)?; + + log::info!( + target: LOG_TARGET, + "💫 xcm {:?} sent with fee {:?}", + xcm, + fee + ); + + // Attempt to send XCM to a dest parachain + let message_id = Self::send_xcm(xcm, channel.para_id)?; + + Self::deposit_event(Event::MessageReceived { + channel_id: envelope.channel_id, + nonce: envelope.nonce, + message_id, + }); + + Ok(()) + } + + /// Halt or resume all pallet operations. May only be called by root. + #[pallet::call_index(1)] + #[pallet::weight((T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational))] + pub fn set_operating_mode( + origin: OriginFor, + mode: BasicOperatingMode, + ) -> DispatchResult { + ensure_root(origin)?; + OperatingMode::::set(mode); + Self::deposit_event(Event::OperatingModeChanged { mode }); + Ok(()) + } + } + + impl Pallet { + pub fn do_convert( + message_id: H256, + message: inbound::VersionedMessage, + ) -> Result<(Xcm<()>, BalanceOf), Error> { + let (mut xcm, fee) = + T::MessageConverter::convert(message).map_err(|e| Error::::ConvertMessage(e))?; + // Append the message id as an XCM topic + xcm.inner_mut().extend(vec![SetTopic(message_id.into())]); + Ok((xcm, fee)) + } + + pub fn send_xcm(xcm: Xcm<()>, dest: ParaId) -> Result> { + let dest = MultiLocation { parents: 1, interior: X1(Parachain(dest.into())) }; + let (xcm_hash, _) = send_xcm::(dest, xcm).map_err(Error::::from)?; + Ok(xcm_hash) + } + + pub fn calculate_delivery_cost(length: u32) -> BalanceOf { + let weight_fee = T::WeightToFee::weight_to_fee(&T::WeightInfo::submit()); + let len_fee = T::LengthToFee::weight_to_fee(&Weight::from_parts(length as u64, 0)); + weight_fee + .saturating_add(len_fee) + .saturating_add(T::PricingParameters::get().rewards.local) + } + } + + /// API for accessing the delivery cost of a message + impl Get> for Pallet { + fn get() -> BalanceOf { + // Cost here based on MaxMessagePayloadSize(the worst case) + Self::calculate_delivery_cost(T::MaxMessageSize::get()) + } + } +} diff --git a/bridges/snowbridge/parachain/pallets/inbound-queue/src/mock.rs b/bridges/snowbridge/parachain/pallets/inbound-queue/src/mock.rs new file mode 100644 index 000000000000..6b79a55e3c93 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/inbound-queue/src/mock.rs @@ -0,0 +1,311 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use super::*; + +use frame_support::{ + parameter_types, + traits::{ConstU128, ConstU32, Everything}, + weights::IdentityFee, +}; +use hex_literal::hex; +use snowbridge_beacon_primitives::{Fork, ForkVersions}; +use snowbridge_core::{ + gwei, + inbound::{Log, Proof, VerificationError}, + meth, Channel, ChannelId, PricingParameters, Rewards, StaticLookup, +}; +use snowbridge_router_primitives::inbound::MessageToXcm; +use sp_core::{H160, H256}; +use sp_runtime::{ + traits::{BlakeTwo256, IdentifyAccount, IdentityLookup, Verify}, + BuildStorage, FixedU128, MultiSignature, +}; +use sp_std::convert::From; +use xcm::v3::{prelude::*, MultiAssets, SendXcm}; + +use crate::{self as inbound_queue}; + +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test + { + System: frame_system::{Pallet, Call, Storage, Event}, + Balances: pallet_balances::{Pallet, Call, Storage, Config, Event}, + EthereumBeaconClient: snowbridge_ethereum_beacon_client::{Pallet, Call, Storage, Event}, + InboundQueue: inbound_queue::{Pallet, Call, Storage, Event}, + } +); + +pub type Signature = MultiSignature; +pub type AccountId = <::Signer as IdentifyAccount>::AccountId; + +parameter_types! { + pub const BlockHashCount: u64 = 250; +} + +type Balance = u128; + +impl frame_system::Config for Test { + type BaseCallFilter = Everything; + type BlockWeights = (); + type BlockLength = (); + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type RuntimeTask = RuntimeTask; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = AccountId; + type Lookup = IdentityLookup; + type RuntimeEvent = RuntimeEvent; + type BlockHashCount = BlockHashCount; + type DbWeight = (); + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = pallet_balances::AccountData; + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = (); + type OnSetCode = (); + type MaxConsumers = frame_support::traits::ConstU32<16>; + type Nonce = u64; + type Block = Block; +} + +impl pallet_balances::Config for Test { + type MaxLocks = (); + type MaxReserves = (); + type ReserveIdentifier = [u8; 8]; + type Balance = Balance; + type RuntimeEvent = RuntimeEvent; + type DustRemoval = (); + type ExistentialDeposit = ConstU128<1>; + type AccountStore = System; + type WeightInfo = (); + type FreezeIdentifier = (); + type MaxFreezes = (); + type RuntimeHoldReason = (); + type RuntimeFreezeReason = (); + type MaxHolds = (); +} + +parameter_types! { + pub const ExecutionHeadersPruneThreshold: u32 = 10; + pub const ChainForkVersions: ForkVersions = ForkVersions{ + genesis: Fork { + version: [0, 0, 0, 1], // 0x00000001 + epoch: 0, + }, + altair: Fork { + version: [1, 0, 0, 1], // 0x01000001 + epoch: 0, + }, + bellatrix: Fork { + version: [2, 0, 0, 1], // 0x02000001 + epoch: 0, + }, + capella: Fork { + version: [3, 0, 0, 1], // 0x03000001 + epoch: 0, + }, + }; +} + +impl snowbridge_ethereum_beacon_client::Config for Test { + type RuntimeEvent = RuntimeEvent; + type ForkVersions = ChainForkVersions; + type MaxExecutionHeadersToKeep = ExecutionHeadersPruneThreshold; + type WeightInfo = (); +} + +// Mock verifier +pub struct MockVerifier; + +impl Verifier for MockVerifier { + fn verify(_: &Log, _: &Proof) -> Result<(), VerificationError> { + Ok(()) + } +} + +const GATEWAY_ADDRESS: [u8; 20] = hex!["eda338e4dc46038493b885327842fd3e301cab39"]; + +parameter_types! { + pub const EthereumNetwork: xcm::v3::NetworkId = xcm::v3::NetworkId::Ethereum { chain_id: 11155111 }; + pub const GatewayAddress: H160 = H160(GATEWAY_ADDRESS); + pub const CreateAssetCall: [u8;2] = [53, 0]; + pub const CreateAssetExecutionFee: u128 = 2_000_000_000; + pub const CreateAssetDeposit: u128 = 100_000_000_000; + pub const SendTokenExecutionFee: u128 = 1_000_000_000; + pub const InitialFund: u128 = 1_000_000_000_000; + pub const InboundQueuePalletInstance: u8 = 80; +} + +#[cfg(feature = "runtime-benchmarks")] +impl BenchmarkHelper for Test { + // not implemented since the MockVerifier is used for tests + fn initialize_storage(_: H256, _: CompactExecutionHeader) {} +} + +// Mock XCM sender that always succeeds +pub struct MockXcmSender; + +impl SendXcm for MockXcmSender { + type Ticket = Xcm<()>; + + fn validate( + dest: &mut Option, + xcm: &mut Option>, + ) -> SendResult { + match dest { + Some(MultiLocation { interior, .. }) => { + if let X1(Parachain(1001)) = interior { + return Err(XcmpSendError::NotApplicable) + } + Ok((xcm.clone().unwrap(), MultiAssets::default())) + }, + _ => Ok((xcm.clone().unwrap(), MultiAssets::default())), + } + } + + fn deliver(xcm: Self::Ticket) -> core::result::Result { + let hash = xcm.using_encoded(sp_io::hashing::blake2_256); + Ok(hash) + } +} + +parameter_types! { + pub const OwnParaId: ParaId = ParaId::new(1013); + pub Parameters: PricingParameters = PricingParameters { + exchange_rate: FixedU128::from_rational(1, 400), + fee_per_gas: gwei(20), + rewards: Rewards { local: DOT, remote: meth(1) } + }; +} + +pub const DOT: u128 = 10_000_000_000; + +pub struct MockChannelLookup; +impl StaticLookup for MockChannelLookup { + type Source = ChannelId; + type Target = Channel; + + fn lookup(channel_id: Self::Source) -> Option { + if channel_id != + hex!("c173fac324158e77fb5840738a1a541f633cbec8884c6a601c567d2b376a0539").into() + { + return None + } + Some(Channel { agent_id: H256::zero(), para_id: ASSET_HUB_PARAID.into() }) + } +} + +impl inbound_queue::Config for Test { + type RuntimeEvent = RuntimeEvent; + type Verifier = MockVerifier; + type Token = Balances; + type XcmSender = MockXcmSender; + type WeightInfo = (); + type GatewayAddress = GatewayAddress; + type MessageConverter = MessageToXcm< + CreateAssetCall, + CreateAssetDeposit, + InboundQueuePalletInstance, + AccountId, + Balance, + >; + type PricingParameters = Parameters; + type ChannelLookup = MockChannelLookup; + #[cfg(feature = "runtime-benchmarks")] + type Helper = Test; + type WeightToFee = IdentityFee; + type LengthToFee = IdentityFee; + type MaxMessageSize = ConstU32<1024>; +} + +pub fn last_events(n: usize) -> Vec { + frame_system::Pallet::::events() + .into_iter() + .rev() + .take(n) + .rev() + .map(|e| e.event) + .collect() +} + +pub fn expect_events(e: Vec) { + assert_eq!(last_events(e.len()), e); +} + +pub fn setup() { + System::set_block_number(1); + Balances::mint_into( + &sibling_sovereign_account::(ASSET_HUB_PARAID.into()), + InitialFund::get(), + ) + .unwrap(); + Balances::mint_into( + &sibling_sovereign_account::(TEMPLATE_PARAID.into()), + InitialFund::get(), + ) + .unwrap(); +} + +pub fn new_tester() -> sp_io::TestExternalities { + let storage = frame_system::GenesisConfig::::default().build_storage().unwrap(); + let mut ext: sp_io::TestExternalities = storage.into(); + ext.execute_with(setup); + ext +} + +// Generated from smoketests: +// cd smoketests +// ./make-bindings +// cargo test --test register_token -- --nocapture +pub fn mock_event_log() -> Log { + Log { + // gateway address + address: hex!("eda338e4dc46038493b885327842fd3e301cab39").into(), + topics: vec![ + hex!("7153f9357c8ea496bba60bf82e67143e27b64462b49041f8e689e1b05728f84f").into(), + // channel id + hex!("c173fac324158e77fb5840738a1a541f633cbec8884c6a601c567d2b376a0539").into(), + // message id + hex!("5f7060e971b0dc81e63f0aa41831091847d97c1a4693ac450cc128c7214e65e0").into(), + ], + // Nonce + Payload + data: hex!("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002e000f000000000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7d00e40b54020000000000000000000000000000000000000000000000000000000000").into(), + } +} + +pub fn mock_event_log_invalid_channel() -> Log { + Log { + address: hex!("eda338e4dc46038493b885327842fd3e301cab39").into(), + topics: vec![ + hex!("7153f9357c8ea496bba60bf82e67143e27b64462b49041f8e689e1b05728f84f").into(), + // invalid channel id + hex!("0000000000000000000000000000000000000000000000000000000000000000").into(), + hex!("5f7060e971b0dc81e63f0aa41831091847d97c1a4693ac450cc128c7214e65e0").into(), + ], + data: hex!("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001e000f000000000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000").into(), + } +} + +pub fn mock_event_log_invalid_gateway() -> Log { + Log { + // gateway address + address: H160::zero(), + topics: vec![ + hex!("7153f9357c8ea496bba60bf82e67143e27b64462b49041f8e689e1b05728f84f").into(), + // channel id + hex!("c173fac324158e77fb5840738a1a541f633cbec8884c6a601c567d2b376a0539").into(), + // message id + hex!("5f7060e971b0dc81e63f0aa41831091847d97c1a4693ac450cc128c7214e65e0").into(), + ], + // Nonce + Payload + data: hex!("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001e000f000000000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000").into(), + } +} + +pub const ASSET_HUB_PARAID: u32 = 1000u32; +pub const TEMPLATE_PARAID: u32 = 1001u32; diff --git a/bridges/snowbridge/parachain/pallets/inbound-queue/src/test.rs b/bridges/snowbridge/parachain/pallets/inbound-queue/src/test.rs new file mode 100644 index 000000000000..6dc3ac453745 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/inbound-queue/src/test.rs @@ -0,0 +1,211 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use super::*; + +use frame_support::{assert_noop, assert_ok}; +use hex_literal::hex; +use snowbridge_core::{inbound::Proof, ChannelId}; +use sp_keyring::AccountKeyring as Keyring; +use sp_runtime::{DispatchError, TokenError}; +use sp_std::convert::From; + +use crate::{Error, Event as InboundQueueEvent}; + +use crate::mock::*; + +#[test] +fn test_submit_happy_path() { + new_tester().execute_with(|| { + let relayer: AccountId = Keyring::Bob.into(); + let channel_sovereign = sibling_sovereign_account::(ASSET_HUB_PARAID.into()); + + let origin = RuntimeOrigin::signed(relayer.clone()); + + // Submit message + let message = Message { + event_log: mock_event_log(), + proof: Proof { + block_hash: Default::default(), + tx_index: Default::default(), + data: Default::default(), + }, + }; + + let initial_fund = InitialFund::get(); + assert_eq!(Balances::balance(&relayer), 0); + assert_eq!(Balances::balance(&channel_sovereign), initial_fund); + + assert_ok!(InboundQueue::submit(origin.clone(), message.clone())); + expect_events(vec![InboundQueueEvent::MessageReceived { + channel_id: hex!("c173fac324158e77fb5840738a1a541f633cbec8884c6a601c567d2b376a0539") + .into(), + nonce: 1, + message_id: [ + 27, 217, 88, 127, 46, 143, 199, 70, 236, 66, 212, 244, 85, 221, 153, 104, 175, 37, + 224, 20, 140, 95, 140, 7, 27, 74, 182, 199, 77, 12, 194, 236, + ], + } + .into()]); + + let delivery_cost = InboundQueue::calculate_delivery_cost(message.encode().len() as u32); + assert!( + Parameters::get().rewards.local < delivery_cost, + "delivery cost exceeds pure reward" + ); + + assert_eq!(Balances::balance(&relayer), delivery_cost, "relayer was rewarded"); + assert!( + Balances::balance(&channel_sovereign) <= initial_fund - delivery_cost, + "sovereign account paid reward" + ); + }); +} + +#[test] +fn test_submit_xcm_invalid_channel() { + new_tester().execute_with(|| { + let relayer: AccountId = Keyring::Bob.into(); + let origin = RuntimeOrigin::signed(relayer); + + // Deposit funds into sovereign account of parachain 1001 + let sovereign_account = sibling_sovereign_account::(TEMPLATE_PARAID.into()); + println!("account: {}", sovereign_account); + let _ = Balances::mint_into(&sovereign_account, 10000); + + // Submit message + let message = Message { + event_log: mock_event_log_invalid_channel(), + proof: Proof { + block_hash: Default::default(), + tx_index: Default::default(), + data: Default::default(), + }, + }; + assert_noop!( + InboundQueue::submit(origin.clone(), message.clone()), + Error::::InvalidChannel, + ); + }); +} + +#[test] +fn test_submit_with_invalid_gateway() { + new_tester().execute_with(|| { + let relayer: AccountId = Keyring::Bob.into(); + let origin = RuntimeOrigin::signed(relayer); + + // Deposit funds into sovereign account of Asset Hub (Statemint) + let sovereign_account = sibling_sovereign_account::(ASSET_HUB_PARAID.into()); + let _ = Balances::mint_into(&sovereign_account, 10000); + + // Submit message + let message = Message { + event_log: mock_event_log_invalid_gateway(), + proof: Proof { + block_hash: Default::default(), + tx_index: Default::default(), + data: Default::default(), + }, + }; + assert_noop!( + InboundQueue::submit(origin.clone(), message.clone()), + Error::::InvalidGateway + ); + }); +} + +#[test] +fn test_submit_with_invalid_nonce() { + new_tester().execute_with(|| { + let relayer: AccountId = Keyring::Bob.into(); + let origin = RuntimeOrigin::signed(relayer); + + // Deposit funds into sovereign account of Asset Hub (Statemint) + let sovereign_account = sibling_sovereign_account::(ASSET_HUB_PARAID.into()); + let _ = Balances::mint_into(&sovereign_account, 10000); + + // Submit message + let message = Message { + event_log: mock_event_log(), + proof: Proof { + block_hash: Default::default(), + tx_index: Default::default(), + data: Default::default(), + }, + }; + assert_ok!(InboundQueue::submit(origin.clone(), message.clone())); + + let nonce: u64 = >::get(ChannelId::from(hex!( + "c173fac324158e77fb5840738a1a541f633cbec8884c6a601c567d2b376a0539" + ))); + assert_eq!(nonce, 1); + + // Submit the same again + assert_noop!( + InboundQueue::submit(origin.clone(), message.clone()), + Error::::InvalidNonce + ); + }); +} + +#[test] +fn test_submit_no_funds_to_reward_relayers() { + new_tester().execute_with(|| { + let relayer: AccountId = Keyring::Bob.into(); + let origin = RuntimeOrigin::signed(relayer); + + // Reset balance of sovereign_account to zero so to trigger the FundsUnavailable error + let sovereign_account = sibling_sovereign_account::(ASSET_HUB_PARAID.into()); + Balances::set_balance(&sovereign_account, 0); + + // Submit message + let message = Message { + event_log: mock_event_log(), + proof: Proof { + block_hash: Default::default(), + tx_index: Default::default(), + data: Default::default(), + }, + }; + assert_noop!( + InboundQueue::submit(origin.clone(), message.clone()), + TokenError::FundsUnavailable + ); + }); +} + +#[test] +fn test_set_operating_mode() { + new_tester().execute_with(|| { + let relayer: AccountId = Keyring::Bob.into(); + let origin = RuntimeOrigin::signed(relayer); + let message = Message { + event_log: mock_event_log(), + proof: Proof { + block_hash: Default::default(), + tx_index: Default::default(), + data: Default::default(), + }, + }; + + assert_ok!(InboundQueue::set_operating_mode( + RuntimeOrigin::root(), + snowbridge_core::BasicOperatingMode::Halted + )); + + assert_noop!(InboundQueue::submit(origin, message), Error::::Halted); + }); +} + +#[test] +fn test_set_operating_mode_root_only() { + new_tester().execute_with(|| { + assert_noop!( + InboundQueue::set_operating_mode( + RuntimeOrigin::signed(Keyring::Bob.into()), + snowbridge_core::BasicOperatingMode::Halted + ), + DispatchError::BadOrigin + ); + }); +} diff --git a/bridges/snowbridge/parachain/pallets/inbound-queue/src/weights.rs b/bridges/snowbridge/parachain/pallets/inbound-queue/src/weights.rs new file mode 100644 index 000000000000..c2c665f40d9e --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/inbound-queue/src/weights.rs @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! Autogenerated weights for `snowbridge_inbound_queue` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-07-14, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `macbook pro 14 m2`, CPU: `m2-arm64` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("bridge-hub-rococo-dev"), DB CACHE: 1024 + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use sp_std::marker::PhantomData; + +/// Weight functions needed for ethereum_beacon_client. +pub trait WeightInfo { + fn submit() -> Weight; +} + +// For backwards compatibility and tests +impl WeightInfo for () { + fn submit() -> Weight { + Weight::from_parts(70_000_000, 0) + .saturating_add(Weight::from_parts(0, 3601)) + .saturating_add(RocksDbWeight::get().reads(2)) + .saturating_add(RocksDbWeight::get().writes(2)) + } +} diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/Cargo.toml b/bridges/snowbridge/parachain/pallets/outbound-queue/Cargo.toml new file mode 100644 index 000000000000..66dd1d838e7d --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/Cargo.toml @@ -0,0 +1,78 @@ +[package] +name = "snowbridge-outbound-queue" +description = "Snowbridge Outbound Queue" +version = "0.1.1" +edition = "2021" +authors = ["Snowfork "] +repository = "https://github.com/Snowfork/snowbridge" +license = "Apache-2.0" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +serde = { version = "1.0.188", features = ["alloc", "derive"], default-features = false } +codec = { version = "3.6.1", package = "parity-scale-codec", default-features = false, features = ["derive"] } +scale-info = { version = "2.9.0", default-features = false, features = ["derive"] } +hex-literal = { version = "0.4.1", optional = true } + +frame-benchmarking = { path = "../../../../../substrate/frame/benchmarking", default-features = false, optional = true } +frame-support = { path = "../../../../../substrate/frame/support", default-features = false } +frame-system = { path = "../../../../../substrate/frame/system", default-features = false } +sp-core = { path = "../../../../../substrate/primitives/core", default-features = false } +sp-std = { path = "../../../../../substrate/primitives/std", default-features = false } +sp-runtime = { path = "../../../../../substrate/primitives/runtime", default-features = false } +sp-io = { path = "../../../../../substrate/primitives/io", default-features = false } +sp-arithmetic = { path = "../../../../../substrate/primitives/arithmetic", default-features = false } + +bridge-hub-common = { path = "../../../../../cumulus/parachains/runtimes/bridge-hubs/common", default-features = false } + +snowbridge-core = { path = "../../primitives/core", features = ["serde"], default-features = false } +snowbridge-outbound-queue-merkle-tree = { path = "merkle-tree", default-features = false } +ethabi = { git = "https://github.com/snowfork/ethabi-decode.git", package = "ethabi-decode", branch = "master", default-features = false } + +xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false } + +[dev-dependencies] +pallet-message-queue = { path = "../../../../../substrate/frame/message-queue", default-features = false } +sp-keyring = { path = "../../../../../substrate/primitives/keyring" } +hex-literal = { version = "0.4.1" } + +[features] +default = ["std"] +std = [ + "bridge-hub-common/std", + "codec/std", + "ethabi/std", + "frame-benchmarking/std", + "frame-support/std", + "frame-system/std", + "pallet-message-queue/std", + "scale-info/std", + "serde/std", + "snowbridge-core/std", + "snowbridge-outbound-queue-merkle-tree/std", + "sp-arithmetic/std", + "sp-core/std", + "sp-io/std", + "sp-runtime/std", + "sp-std/std", + "xcm/std", +] +runtime-benchmarks = [ + "bridge-hub-common/runtime-benchmarks", + "frame-benchmarking", + "frame-benchmarking/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "hex-literal", + "pallet-message-queue/runtime-benchmarks", + "snowbridge-core/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", +] +try-runtime = [ + "frame-support/try-runtime", + "frame-system/try-runtime", + "pallet-message-queue/try-runtime", + "sp-runtime/try-runtime", +] diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/Cargo.toml b/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/Cargo.toml new file mode 100644 index 000000000000..a3432163622d --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "snowbridge-outbound-queue-merkle-tree" +description = "Snowbridge Outbound Queue Merkle Tree" +version = "0.1.1" +edition = "2021" +authors = ["Snowfork "] +repository = "https://github.com/Snowfork/snowbridge" +license = "Apache-2.0" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +codec = { version = "3.1.5", package = "parity-scale-codec", default-features = false, features = ["derive"] } +scale-info = { version = "2.7.0", default-features = false, features = ["derive"] } + +sp-core = { path = "../../../../../../substrate/primitives/core", default-features = false } +sp-runtime = { path = "../../../../../../substrate/primitives/runtime", default-features = false } + +[dev-dependencies] +hex-literal = { version = "0.4.1" } +env_logger = "0.9" +hex = "0.4" +array-bytes = "4.1" + +[features] +default = ["std"] +std = [ + "codec/std", + "scale-info/std", + "sp-core/std", + "sp-runtime/std", +] diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/src/lib.rs b/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/src/lib.rs new file mode 100644 index 000000000000..d03eb578ef4d --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/merkle-tree/src/lib.rs @@ -0,0 +1,464 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +// SPDX-FileCopyrightText: 2021-2022 Parity Technologies (UK) Ltd. +#![cfg_attr(not(feature = "std"), no_std)] +#![warn(missing_docs)] + +//! This crate implements a simple binary Merkle Tree utilities required for inter-op with Ethereum +//! bridge & Solidity contract. +//! +//! The implementation is optimised for usage within Substrate Runtime and supports no-std +//! compilation targets. +//! +//! Merkle Tree is constructed from arbitrary-length leaves, that are initially hashed using the +//! same `\[`Hasher`\]` as the inner nodes. +//! Inner nodes are created by concatenating child hashes and hashing again. The implementation +//! does not perform any sorting of the input data (leaves) nor when inner nodes are created. +//! +//! If the number of leaves is not even, last leaf (hash of) is promoted to the upper layer. + +#[cfg(not(feature = "std"))] +extern crate alloc; +#[cfg(not(feature = "std"))] +use alloc::vec; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + +use codec::{Decode, Encode}; +use scale_info::TypeInfo; +use sp_core::{RuntimeDebug, H256}; +use sp_runtime::traits::Hash; + +/// Construct a root hash of a Binary Merkle Tree created from given leaves. +/// +/// See crate-level docs for details about Merkle Tree construction. +/// +/// In case an empty list of leaves is passed the function returns a 0-filled hash. +pub fn merkle_root(leaves: I) -> H256 +where + H: Hash, + I: Iterator, +{ + merkelize::(leaves, &mut ()) +} + +fn merkelize(leaves: I, visitor: &mut V) -> H256 +where + H: Hash, + V: Visitor, + I: Iterator, +{ + let upper = Vec::with_capacity(leaves.size_hint().0); + let mut next = match merkelize_row::(leaves, upper, visitor) { + Ok(root) => return root, + Err(next) if next.is_empty() => return H256::default(), + Err(next) => next, + }; + + let mut upper = Vec::with_capacity((next.len() + 1) / 2); + loop { + visitor.move_up(); + + match merkelize_row::(next.drain(..), upper, visitor) { + Ok(root) => return root, + Err(t) => { + // swap collections to avoid allocations + upper = next; + next = t; + }, + }; + } +} + +/// A generated merkle proof. +/// +/// The structure contains all necessary data to later on verify the proof and the leaf itself. +#[derive(Encode, Decode, RuntimeDebug, PartialEq, Eq, TypeInfo)] +pub struct MerkleProof { + /// Root hash of generated merkle tree. + pub root: H256, + /// Proof items (does not contain the leaf hash, nor the root obviously). + /// + /// This vec contains all inner node hashes necessary to reconstruct the root hash given the + /// leaf hash. + pub proof: Vec, + /// Number of leaves in the original tree. + /// + /// This is needed to detect a case where we have an odd number of leaves that "get promoted" + /// to upper layers. + pub number_of_leaves: u64, + /// Index of the leaf the proof is for (0-based). + pub leaf_index: u64, + /// Leaf content (hashed). + pub leaf: H256, +} + +/// A trait of object inspecting merkle root creation. +/// +/// It can be passed to [`merkelize_row`] or [`merkelize`] functions and will be notified +/// about tree traversal. +trait Visitor { + /// We are moving one level up in the tree. + fn move_up(&mut self); + + /// We are creating an inner node from given `left` and `right` nodes. + /// + /// Note that in case of last odd node in the row `right` might be empty. + /// The method will also visit the `root` hash (level 0). + /// + /// The `index` is an index of `left` item. + fn visit(&mut self, index: u64, left: &Option, right: &Option); +} + +/// No-op implementation of the visitor. +impl Visitor for () { + fn move_up(&mut self) {} + fn visit(&mut self, _index: u64, _left: &Option, _right: &Option) {} +} + +/// Construct a Merkle Proof for leaves given by indices. +/// +/// The function constructs a (partial) Merkle Tree first and stores all elements required +/// to prove the requested item (leaf) given the root hash. +/// +/// Both the Proof and the Root Hash are returned. +/// +/// # Panic +/// +/// The function will panic if given `leaf_index` is greater than the number of leaves. +pub fn merkle_proof(leaves: I, leaf_index: u64) -> MerkleProof +where + H: Hash, + I: Iterator, +{ + let mut leaf = None; + let mut hashes = vec![]; + let mut number_of_leaves = 0; + for (idx, l) in (0u64..).zip(leaves) { + // count the leaves + number_of_leaves = idx + 1; + hashes.push(l); + // find the leaf for the proof + if idx == leaf_index { + leaf = Some(l); + } + } + + /// The struct collects a proof for single leaf. + struct ProofCollection { + proof: Vec, + position: u64, + } + + impl ProofCollection { + fn new(position: u64) -> Self { + ProofCollection { proof: Default::default(), position } + } + } + + impl Visitor for ProofCollection { + fn move_up(&mut self) { + self.position /= 2; + } + + fn visit(&mut self, index: u64, left: &Option, right: &Option) { + // we are at left branch - right goes to the proof. + if self.position == index { + if let Some(right) = right { + self.proof.push(*right); + } + } + // we are at right branch - left goes to the proof. + if self.position == index + 1 { + if let Some(left) = left { + self.proof.push(*left); + } + } + } + } + + let mut collect_proof = ProofCollection::new(leaf_index); + + let root = merkelize::(hashes.into_iter(), &mut collect_proof); + let leaf = leaf.expect("Requested `leaf_index` is greater than number of leaves."); + + #[cfg(feature = "debug")] + log::debug!( + "[merkle_proof] Proof: {:?}", + collect_proof.proof.iter().map(hex::encode).collect::>() + ); + + MerkleProof { root, proof: collect_proof.proof, number_of_leaves, leaf_index, leaf } +} + +/// Leaf node for proof verification. +/// +/// Can be either a value that needs to be hashed first, +/// or the hash itself. +#[derive(Debug, PartialEq, Eq)] +pub enum Leaf<'a> { + /// Leaf content. + Value(&'a [u8]), + /// Hash of the leaf content. + Hash(H256), +} + +impl<'a, T: AsRef<[u8]>> From<&'a T> for Leaf<'a> { + fn from(v: &'a T) -> Self { + Leaf::Value(v.as_ref()) + } +} + +impl<'a> From for Leaf<'a> { + fn from(v: H256) -> Self { + Leaf::Hash(v) + } +} + +/// Verify Merkle Proof correctness versus given root hash. +/// +/// The proof is NOT expected to contain leaf hash as the first +/// element, but only all adjacent nodes required to eventually by process of +/// concatenating and hashing end up with given root hash. +/// +/// The proof must not contain the root hash. +pub fn verify_proof<'a, H, P, L>( + root: &'a H256, + proof: P, + number_of_leaves: u64, + leaf_index: u64, + leaf: L, +) -> bool +where + H: Hash, + P: IntoIterator, + L: Into>, +{ + if leaf_index >= number_of_leaves { + return false + } + + let leaf_hash = match leaf.into() { + Leaf::Value(content) => ::hash(content), + Leaf::Hash(hash) => hash, + }; + + let hash_len = ::LENGTH; + let mut combined = [0_u8; 64]; + let computed = proof.into_iter().fold(leaf_hash, |a, b| { + if a < b { + combined[..hash_len].copy_from_slice(a.as_ref()); + combined[hash_len..].copy_from_slice(b.as_ref()); + } else { + combined[..hash_len].copy_from_slice(b.as_ref()); + combined[hash_len..].copy_from_slice(a.as_ref()); + } + ::hash(&combined) + }); + + root == &computed +} + +/// Processes a single row (layer) of a tree by taking pairs of elements, +/// concatenating them, hashing and placing into resulting vector. +/// +/// In case only one element is provided it is returned via `Ok` result, in any other case (also an +/// empty iterator) an `Err` with the inner nodes of upper layer is returned. +fn merkelize_row( + mut iter: I, + mut next: Vec, + visitor: &mut V, +) -> Result> +where + H: Hash, + V: Visitor, + I: Iterator, +{ + #[cfg(feature = "debug")] + log::debug!("[merkelize_row]"); + next.clear(); + + let hash_len = ::LENGTH; + let mut index = 0; + let mut combined = vec![0_u8; hash_len * 2]; + loop { + let a = iter.next(); + let b = iter.next(); + visitor.visit(index, &a, &b); + + #[cfg(feature = "debug")] + log::debug!(" {:?}\n {:?}", a.as_ref().map(hex::encode), b.as_ref().map(hex::encode)); + + index += 2; + match (a, b) { + (Some(a), Some(b)) => { + if a < b { + combined[..hash_len].copy_from_slice(a.as_ref()); + combined[hash_len..].copy_from_slice(b.as_ref()); + } else { + combined[..hash_len].copy_from_slice(b.as_ref()); + combined[hash_len..].copy_from_slice(a.as_ref()); + } + + next.push(::hash(&combined)); + }, + // Odd number of items. Promote the item to the upper layer. + (Some(a), None) if !next.is_empty() => { + next.push(a); + }, + // Last item = root. + (Some(a), None) => return Ok(a), + // Finish up, no more items. + _ => { + #[cfg(feature = "debug")] + log::debug!( + "[merkelize_row] Next: {:?}", + next.iter().map(hex::encode).collect::>() + ); + return Err(next) + }, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use hex_literal::hex; + use sp_core::keccak_256; + use sp_runtime::traits::Keccak256; + + fn make_leaves(count: u64) -> Vec { + (0..count).map(|i| keccak_256(&i.to_le_bytes()).into()).collect() + } + + #[test] + fn should_generate_empty_root() { + // given + let _ = env_logger::try_init(); + let data = vec![]; + + // when + let out = merkle_root::(data.into_iter()); + + // then + assert_eq!( + hex::encode(out), + "0000000000000000000000000000000000000000000000000000000000000000" + ); + } + + #[test] + fn should_generate_single_root() { + // given + let _ = env_logger::try_init(); + let data = make_leaves(1); + + // when + let out = merkle_root::(data.into_iter()); + + // then + assert_eq!( + hex::encode(out), + "011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce" + ); + } + + #[test] + fn should_generate_root_pow_2() { + // given + let _ = env_logger::try_init(); + let data = make_leaves(2); + + // when + let out = merkle_root::(data.into_iter()); + + // then + assert_eq!( + hex::encode(out), + "e497bd1c13b13a60af56fa0d2703517c232fde213ad20d2c3dd60735c6604512" + ); + } + + #[test] + fn should_generate_root_complex() { + let _ = env_logger::try_init(); + let test = |root, data: Vec| { + assert_eq!( + array_bytes::bytes2hex("", merkle_root::(data.into_iter()).as_ref()), + root + ); + }; + + test("816cc37bd8d39f7b0851838ebc875faf2afe58a03e95aca3b1333b3693f39dd3", make_leaves(3)); + + test("7501ea976cb92f305cca65ab11254589ea28bb8b59d3161506350adaa237d22f", make_leaves(4)); + + test("d26ba4eb398747bdd39255b1fadb99b803ce39696021b3b0bff7301ac146ee4e", make_leaves(10)); + } + + #[test] + #[ignore] + fn should_generate_and_verify_proof() { + // given + let _ = env_logger::try_init(); + let data: Vec = make_leaves(3); + + // when + let proof0 = merkle_proof::(data.clone().into_iter(), 0); + assert!(verify_proof::( + &proof0.root, + proof0.proof.clone(), + data.len() as u64, + proof0.leaf_index, + &data[0], + )); + + let proof1 = merkle_proof::(data.clone().into_iter(), 1); + assert!(verify_proof::( + &proof1.root, + proof1.proof, + data.len() as u64, + proof1.leaf_index, + &proof1.leaf, + )); + + let proof2 = merkle_proof::(data.clone().into_iter(), 2); + assert!(verify_proof::( + &proof2.root, + proof2.proof, + data.len() as u64, + proof2.leaf_index, + &proof2.leaf + )); + + // then + assert_eq!(hex::encode(proof0.root), hex::encode(proof1.root)); + assert_eq!(hex::encode(proof2.root), hex::encode(proof1.root)); + + assert!(!verify_proof::( + &H256::from_slice(&hex!( + "fb3b3be94be9e983ba5e094c9c51a7d96a4fa2e5d8e891df00ca89ba05bb1239" + )), + proof0.proof, + data.len() as u64, + proof0.leaf_index, + &proof0.leaf + )); + + assert!(!verify_proof::( + &proof0.root, + vec![], + data.len() as u64, + proof0.leaf_index, + &proof0.leaf + )); + } + + #[test] + #[should_panic] + fn should_panic_on_invalid_leaf_index() { + let _ = env_logger::try_init(); + merkle_proof::(make_leaves(1).into_iter(), 5); + } +} diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/runtime-api/Cargo.toml b/bridges/snowbridge/parachain/pallets/outbound-queue/runtime-api/Cargo.toml new file mode 100644 index 000000000000..c92e725c60d5 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/runtime-api/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "snowbridge-outbound-queue-runtime-api" +description = "Snowbridge Outbound Queue Runtime API" +version = "0.1.0" +edition = "2021" +authors = ["Snowfork "] +repository = "https://github.com/Snowfork/snowbridge" +license = "Apache-2.0" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +codec = { version = "3.1.5", package = "parity-scale-codec", features = ["derive"], default-features = false } +sp-core = { path = "../../../../../../substrate/primitives/core", default-features = false } +sp-std = { path = "../../../../../../substrate/primitives/std", default-features = false } +sp-api = { path = "../../../../../../substrate/primitives/api", default-features = false } +frame-support = { path = "../../../../../../substrate/frame/support", default-features = false } +xcm = { package = "staging-xcm", path = "../../../../../../polkadot/xcm", default-features = false } +snowbridge-outbound-queue-merkle-tree = { path = "../merkle-tree", default-features = false } +snowbridge-core = { path = "../../../primitives/core", default-features = false } + +[features] +default = ["std"] +std = [ + "codec/std", + "frame-support/std", + "snowbridge-core/std", + "snowbridge-outbound-queue-merkle-tree/std", + "sp-api/std", + "sp-core/std", + "sp-std/std", + "xcm/std", +] diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/runtime-api/src/lib.rs b/bridges/snowbridge/parachain/pallets/outbound-queue/runtime-api/src/lib.rs new file mode 100644 index 000000000000..51f46a7b49c8 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/runtime-api/src/lib.rs @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +#![cfg_attr(not(feature = "std"), no_std)] + +use frame_support::traits::tokens::Balance as BalanceT; +use snowbridge_core::outbound::Message; +use snowbridge_outbound_queue_merkle_tree::MerkleProof; + +sp_api::decl_runtime_apis! { + pub trait OutboundQueueApi where Balance: BalanceT + { + /// Generate a merkle proof for a committed message identified by `leaf_index`. + /// The merkle root is stored in the block header as a + /// `\[`sp_runtime::generic::DigestItem::Other`\]` + fn prove_message(leaf_index: u64) -> Option; + + /// Calculate the delivery fee for `message` + fn calculate_fee(message: Message) -> Option; + } +} diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/src/api.rs b/bridges/snowbridge/parachain/pallets/outbound-queue/src/api.rs new file mode 100644 index 000000000000..44d63f1e2d23 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/src/api.rs @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! Helpers for implementing runtime api + +use crate::{Config, MessageLeaves}; +use frame_support::storage::StorageStreamIter; +use snowbridge_core::outbound::{Message, SendMessage}; +use snowbridge_outbound_queue_merkle_tree::{merkle_proof, MerkleProof}; + +pub fn prove_message(leaf_index: u64) -> Option +where + T: Config, +{ + if !MessageLeaves::::exists() { + return None + } + let proof = + merkle_proof::<::Hashing, _>(MessageLeaves::::stream_iter(), leaf_index); + Some(proof) +} + +pub fn calculate_fee(message: Message) -> Option +where + T: Config, +{ + match crate::Pallet::::validate(&message) { + Ok((_, fees)) => Some(fees.total()), + _ => None, + } +} diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/src/benchmarking.rs b/bridges/snowbridge/parachain/pallets/outbound-queue/src/benchmarking.rs new file mode 100644 index 000000000000..ee5754e86962 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/src/benchmarking.rs @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use super::*; + +use bridge_hub_common::AggregateMessageOrigin; +use codec::Encode; +use frame_benchmarking::v2::*; +use snowbridge_core::{ + outbound::{Command, Initializer}, + ChannelId, +}; +use sp_core::{H160, H256}; + +#[allow(unused_imports)] +use crate::Pallet as OutboundQueue; + +#[benchmarks( + where + ::MaxMessagePayloadSize: Get, +)] +mod benchmarks { + use super::*; + + /// Benchmark for processing a message. + #[benchmark] + fn do_process_message() -> Result<(), BenchmarkError> { + let enqueued_message = QueuedMessage { + id: H256::zero(), + channel_id: ChannelId::from([1; 32]), + command: Command::Upgrade { + impl_address: H160::zero(), + impl_code_hash: H256::zero(), + initializer: Some(Initializer { + params: [7u8; 256].into_iter().collect(), + maximum_required_gas: 200_000, + }), + }, + }; + let origin = AggregateMessageOrigin::Snowbridge([1; 32].into()); + let encoded_enqueued_message = enqueued_message.encode(); + + #[block] + { + let _ = OutboundQueue::::do_process_message(origin, &encoded_enqueued_message); + } + + assert_eq!(MessageLeaves::::decode_len().unwrap(), 1); + + Ok(()) + } + + /// Benchmark for producing final messages commitment + #[benchmark] + fn commit() -> Result<(), BenchmarkError> { + // Assume worst case, where `MaxMessagesPerBlock` messages need to be committed. + for i in 0..T::MaxMessagesPerBlock::get() { + let leaf_data: [u8; 1] = [i as u8]; + let leaf = ::Hashing::hash(&leaf_data); + MessageLeaves::::append(leaf); + } + + #[block] + { + OutboundQueue::::commit(); + } + + Ok(()) + } + + /// Benchmark for producing commitment for a single message + #[benchmark] + fn commit_single() -> Result<(), BenchmarkError> { + let leaf = ::Hashing::hash(&[100; 1]); + MessageLeaves::::append(leaf); + + #[block] + { + OutboundQueue::::commit(); + } + + Ok(()) + } + + impl_benchmark_test_suite!(OutboundQueue, crate::mock::new_tester(), crate::mock::Test,); +} diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/src/lib.rs b/bridges/snowbridge/parachain/pallets/outbound-queue/src/lib.rs new file mode 100644 index 000000000000..201e524fb912 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/src/lib.rs @@ -0,0 +1,413 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! Pallet for committing outbound messages for delivery to Ethereum +//! +//! # Overview +//! +//! Messages come either from sibling parachains via XCM, or BridgeHub itself +//! via the `snowbridge-system` pallet: +//! +//! 1. `snowbridge_router_primitives::outbound::EthereumBlobExporter::deliver` +//! 2. `snowbridge_system::Pallet::send` +//! +//! The message submission pipeline works like this: +//! 1. The message is first validated via the implementation for +//! [`snowbridge_core::outbound::SendMessage::validate`] +//! 2. The message is then enqueued for later processing via the implementation for +//! [`snowbridge_core::outbound::SendMessage::deliver`] +//! 3. The underlying message queue is implemented by [`Config::MessageQueue`] +//! 4. The message queue delivers messages back to this pallet via the implementation for +//! [`frame_support::traits::ProcessMessage::process_message`] +//! 5. The message is processed in `Pallet::do_process_message`: a. Assigned a nonce b. ABI-encoded, +//! hashed, and stored in the `MessageLeaves` vector +//! 6. At the end of the block, a merkle root is constructed from all the leaves in `MessageLeaves`. +//! 7. This merkle root is inserted into the parachain header as a digest item +//! 8. Offchain relayers are able to relay the message to Ethereum after: a. Generating a merkle +//! proof for the committed message using the `prove_message` runtime API b. Reading the actual +//! message content from the `Messages` vector in storage +//! +//! On the Ethereum side, the message root is ultimately the thing being +//! verified by the Polkadot light client. +//! +//! # Message Priorities +//! +//! The processing of governance commands can never be halted. This effectively +//! allows us to pause processing of normal user messages while still allowing +//! governance commands to be sent to Ethereum. +//! +//! # Fees +//! +//! An upfront fee must be paid for delivering a message. This fee covers several +//! components: +//! 1. The weight of processing the message locally +//! 2. The gas refund paid out to relayers for message submission +//! 3. An additional reward paid out to relayers for message submission +//! +//! Messages are weighed to determine the maximum amount of gas they could +//! consume on Ethereum. Using this upper bound, a final fee can be calculated. +//! +//! The fee calculation also requires the following parameters: +//! * ETH/DOT exchange rate +//! * Ether fee per unit of gas +//! +//! By design, it is expected that governance should manually update these +//! parameters every few weeks using the `set_pricing_parameters` extrinsic in the +//! system pallet. +//! +//! ## Fee Computation Function +//! +//! ```text +//! LocalFee(Message) = WeightToFee(ProcessMessageWeight(Message)) +//! RemoteFee(Message) = MaxGasRequired(Message) * FeePerGas + Reward +//! Fee(Message) = LocalFee(Message) + (RemoteFee(Message) / Ratio("ETH/DOT")) +//! ``` +//! +//! By design, the computed fee is always going to conservative, to cover worst-case +//! costs of dispatch on Ethereum. In future iterations of the design, we will optimize +//! this, or provide a mechanism to asynchronously refund a portion of collected fees. +//! +//! # Extrinsics +//! +//! * [`Call::set_operating_mode`]: Set the operating mode +//! +//! # Runtime API +//! +//! * `prove_message`: Generate a merkle proof for a committed message +//! * `calculate_fee`: Calculate the delivery fee for a message +#![cfg_attr(not(feature = "std"), no_std)] +pub mod api; +pub mod process_message_impl; +pub mod send_message_impl; +pub mod types; +pub mod weights; + +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; + +#[cfg(test)] +mod mock; + +#[cfg(test)] +mod test; + +use bridge_hub_common::{AggregateMessageOrigin, CustomDigestItem}; +use codec::Decode; +use frame_support::{ + storage::StorageStreamIter, + traits::{tokens::Balance, Contains, Defensive, EnqueueMessage, Get, ProcessMessageError}, + weights::{Weight, WeightToFee}, +}; +use snowbridge_core::{ + outbound::{Fee, GasMeter, QueuedMessage, VersionedQueuedMessage, ETHER_DECIMALS}, + BasicOperatingMode, ChannelId, +}; +use snowbridge_outbound_queue_merkle_tree::merkle_root; +pub use snowbridge_outbound_queue_merkle_tree::MerkleProof; +use sp_core::{H256, U256}; +use sp_runtime::{ + traits::{CheckedDiv, Hash}, + DigestItem, +}; +use sp_std::prelude::*; +pub use types::{CommittedMessage, FeeConfigRecord, ProcessMessageOriginOf}; +pub use weights::WeightInfo; + +pub use pallet::*; + +#[frame_support::pallet] +pub mod pallet { + use super::*; + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use snowbridge_core::PricingParameters; + use sp_arithmetic::FixedU128; + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config { + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + + type Hashing: Hash; + + type MessageQueue: EnqueueMessage; + + /// Measures the maximum gas used to execute a command on Ethereum + type GasMeter: GasMeter; + + type Balance: Balance + From; + + /// Number of decimal places in native currency + #[pallet::constant] + type Decimals: Get; + + /// Max bytes in a message payload + #[pallet::constant] + type MaxMessagePayloadSize: Get; + + /// Max number of messages processed per block + #[pallet::constant] + type MaxMessagesPerBlock: Get; + + /// Check whether a channel exists + type Channels: Contains; + + type PricingParameters: Get>; + + /// Convert a weight value into a deductible fee based. + type WeightToFee: WeightToFee; + + /// Weight information for extrinsics in this pallet + type WeightInfo: WeightInfo; + } + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + /// Message has been queued and will be processed in the future + MessageQueued { + /// ID of the message. Usually the XCM message hash or a SetTopic. + id: H256, + }, + /// Message will be committed at the end of current block. From now on, to track the + /// progress the message, use the `nonce` of `id`. + MessageAccepted { + /// ID of the message + id: H256, + /// The nonce assigned to this message + nonce: u64, + }, + /// Some messages have been committed + MessagesCommitted { + /// Merkle root of the committed messages + root: H256, + /// number of committed messages + count: u64, + }, + /// Set OperatingMode + OperatingModeChanged { + mode: BasicOperatingMode, + }, + FeeConfigChanged { + fee_config: FeeConfigRecord, + }, + } + + #[pallet::error] + pub enum Error { + /// The message is too large + MessageTooLarge, + /// The pallet is halted + Halted, + // Invalid fee config + InvalidFeeConfig, + /// Invalid Channel + InvalidChannel, + } + + /// Messages to be committed in the current block. This storage value is killed in + /// `on_initialize`, so should never go into block PoV. + /// + /// Is never read in the runtime, only by offchain message relayers. + /// + /// Inspired by the `frame_system::Pallet::Events` storage value + #[pallet::storage] + #[pallet::unbounded] + pub(super) type Messages = StorageValue<_, Vec, ValueQuery>; + + /// Hashes of the ABI-encoded messages in the [`Messages`] storage value. Used to generate a + /// merkle root during `on_finalize`. This storage value is killed in + /// `on_initialize`, so should never go into block PoV. + #[pallet::storage] + #[pallet::unbounded] + #[pallet::getter(fn message_leaves)] + pub(super) type MessageLeaves = StorageValue<_, Vec, ValueQuery>; + + /// The current nonce for each message origin + #[pallet::storage] + pub type Nonce = StorageMap<_, Twox64Concat, ChannelId, u64, ValueQuery>; + + /// The current operating mode of the pallet. + #[pallet::storage] + #[pallet::getter(fn operating_mode)] + pub type OperatingMode = StorageValue<_, BasicOperatingMode, ValueQuery>; + + #[pallet::hooks] + impl Hooks> for Pallet + where + T::AccountId: AsRef<[u8]>, + { + fn on_initialize(_: BlockNumberFor) -> Weight { + // Remove storage from previous block + Messages::::kill(); + MessageLeaves::::kill(); + // Reserve some weight for the `on_finalize` handler + T::WeightInfo::commit() + } + + fn on_finalize(_: BlockNumberFor) { + Self::commit(); + } + + fn integrity_test() { + let decimals = T::Decimals::get(); + assert!(decimals == 10 || decimals == 12, "Decimals should be 10 or 12"); + } + } + + #[pallet::call] + impl Pallet { + /// Halt or resume all pallet operations. May only be called by root. + #[pallet::call_index(0)] + #[pallet::weight((T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational))] + pub fn set_operating_mode( + origin: OriginFor, + mode: BasicOperatingMode, + ) -> DispatchResult { + ensure_root(origin)?; + OperatingMode::::put(mode); + Self::deposit_event(Event::OperatingModeChanged { mode }); + Ok(()) + } + } + + impl Pallet { + /// Generate a messages commitment and insert it into the header digest + pub(crate) fn commit() { + let count = MessageLeaves::::decode_len().unwrap_or_default() as u64; + if count == 0 { + return + } + + // Create merkle root of messages + let root = merkle_root::<::Hashing, _>(MessageLeaves::::stream_iter()); + + let digest_item: DigestItem = CustomDigestItem::Snowbridge(root).into(); + + // Insert merkle root into the header digest + >::deposit_log(digest_item); + + Self::deposit_event(Event::MessagesCommitted { root, count }); + } + + /// Process a message delivered by the MessageQueue pallet + pub(crate) fn do_process_message( + _: ProcessMessageOriginOf, + mut message: &[u8], + ) -> Result { + use ProcessMessageError::*; + + // Yield if the maximum number of messages has been processed this block. + // This ensures that the weight of `on_finalize` has a known maximum bound. + ensure!( + MessageLeaves::::decode_len().unwrap_or(0) < + T::MaxMessagesPerBlock::get() as usize, + Yield + ); + + // Decode bytes into versioned message + let versioned_queued_message: VersionedQueuedMessage = + VersionedQueuedMessage::decode(&mut message).map_err(|_| Corrupt)?; + + // Convert versioned message into latest supported message version + let queued_message: QueuedMessage = + versioned_queued_message.try_into().map_err(|_| Unsupported)?; + + // Obtain next nonce + let nonce = >::try_mutate( + queued_message.channel_id, + |nonce| -> Result { + *nonce = nonce.checked_add(1).ok_or(Unsupported)?; + Ok(*nonce) + }, + )?; + + let pricing_params = T::PricingParameters::get(); + let command = queued_message.command.index(); + let params = queued_message.command.abi_encode(); + let max_dispatch_gas = + T::GasMeter::maximum_dispatch_gas_used_at_most(&queued_message.command); + let reward = pricing_params.rewards.remote; + + // Construct the final committed message + let message = CommittedMessage { + channel_id: queued_message.channel_id, + nonce, + command, + params, + max_dispatch_gas, + max_fee_per_gas: pricing_params + .fee_per_gas + .try_into() + .defensive_unwrap_or(u128::MAX), + reward: reward.try_into().defensive_unwrap_or(u128::MAX), + id: queued_message.id, + }; + + // ABI-encode and hash the prepared message + let message_abi_encoded = ethabi::encode(&[message.clone().into()]); + let message_abi_encoded_hash = ::Hashing::hash(&message_abi_encoded); + + Messages::::append(Box::new(message)); + MessageLeaves::::append(message_abi_encoded_hash); + + Self::deposit_event(Event::MessageAccepted { id: queued_message.id, nonce }); + + Ok(true) + } + + /// Calculate total fee in native currency to cover all costs of delivering a message to the + /// remote destination. See module-level documentation for more details. + pub(crate) fn calculate_fee( + gas_used_at_most: u64, + params: PricingParameters, + ) -> Fee { + // Remote fee in ether + let fee = Self::calculate_remote_fee( + gas_used_at_most, + params.fee_per_gas, + params.rewards.remote, + ); + + // downcast to u128 + let fee: u128 = fee.try_into().defensive_unwrap_or(u128::MAX); + + // convert to local currency + let fee = FixedU128::from_inner(fee) + .checked_div(¶ms.exchange_rate) + .expect("exchange rate is not zero; qed") + .into_inner(); + + // adjust fixed point to match local currency + let fee = Self::convert_from_ether_decimals(fee); + + Fee::from((Self::calculate_local_fee(), fee)) + } + + /// Calculate fee in remote currency for dispatching a message on Ethereum + pub(crate) fn calculate_remote_fee( + gas_used_at_most: u64, + fee_per_gas: U256, + reward: U256, + ) -> U256 { + fee_per_gas.saturating_mul(gas_used_at_most.into()).saturating_add(reward) + } + + /// The local component of the message processing fees in native currency + pub(crate) fn calculate_local_fee() -> T::Balance { + T::WeightToFee::weight_to_fee( + &T::WeightInfo::do_process_message().saturating_add(T::WeightInfo::commit_single()), + ) + } + + // 1 DOT has 10 digits of precision + // 1 KSM has 12 digits of precision + // 1 ETH has 18 digits of precision + pub(crate) fn convert_from_ether_decimals(value: u128) -> T::Balance { + let decimals = ETHER_DECIMALS.saturating_sub(T::Decimals::get()) as u32; + let denom = 10u128.saturating_pow(decimals); + value.checked_div(denom).expect("divisor is non-zero; qed").into() + } + } +} diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/src/mock.rs b/bridges/snowbridge/parachain/pallets/outbound-queue/src/mock.rs new file mode 100644 index 000000000000..dd8fee4e2ed0 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/src/mock.rs @@ -0,0 +1,189 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use super::*; + +use frame_support::{ + parameter_types, + traits::{Everything, Hooks}, + weights::IdentityFee, +}; + +use snowbridge_core::{ + gwei, meth, + outbound::*, + pricing::{PricingParameters, Rewards}, + ParaId, PRIMARY_GOVERNANCE_CHANNEL, +}; +use sp_core::{ConstU32, ConstU8, H160, H256}; +use sp_runtime::{ + traits::{BlakeTwo256, IdentityLookup, Keccak256}, + AccountId32, BuildStorage, FixedU128, +}; +use sp_std::marker::PhantomData; + +type Block = frame_system::mocking::MockBlock; +type AccountId = AccountId32; + +frame_support::construct_runtime!( + pub enum Test + { + System: frame_system::{Pallet, Call, Storage, Event}, + MessageQueue: pallet_message_queue::{Pallet, Call, Storage, Event}, + OutboundQueue: crate::{Pallet, Storage, Event}, + } +); + +parameter_types! { + pub const BlockHashCount: u64 = 250; +} + +impl frame_system::Config for Test { + type BaseCallFilter = Everything; + type BlockWeights = (); + type BlockLength = (); + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type RuntimeTask = RuntimeTask; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = AccountId; + type Lookup = IdentityLookup; + type RuntimeEvent = RuntimeEvent; + type BlockHashCount = BlockHashCount; + type DbWeight = (); + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = (); + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = (); + type OnSetCode = (); + type MaxConsumers = frame_support::traits::ConstU32<16>; + type Nonce = u64; + type Block = Block; +} + +parameter_types! { + pub const HeapSize: u32 = 32 * 1024; + pub const MaxStale: u32 = 32; + pub static ServiceWeight: Option = Some(Weight::from_parts(100, 100)); +} + +impl pallet_message_queue::Config for Test { + type RuntimeEvent = RuntimeEvent; + type WeightInfo = (); + type MessageProcessor = OutboundQueue; + type Size = u32; + type QueueChangeHandler = (); + type HeapSize = HeapSize; + type MaxStale = MaxStale; + type ServiceWeight = ServiceWeight; + type QueuePausedQuery = (); +} + +parameter_types! { + pub const OwnParaId: ParaId = ParaId::new(1013); + pub Parameters: PricingParameters = PricingParameters { + exchange_rate: FixedU128::from_rational(1, 400), + fee_per_gas: gwei(20), + rewards: Rewards { local: DOT, remote: meth(1) } + }; +} + +pub const DOT: u128 = 10_000_000_000; + +impl crate::Config for Test { + type RuntimeEvent = RuntimeEvent; + type Hashing = Keccak256; + type MessageQueue = MessageQueue; + type Decimals = ConstU8<12>; + type MaxMessagePayloadSize = ConstU32<1024>; + type MaxMessagesPerBlock = ConstU32<20>; + type GasMeter = ConstantGasMeter; + type Balance = u128; + type PricingParameters = Parameters; + type Channels = Everything; + type WeightToFee = IdentityFee; + type WeightInfo = (); +} + +fn setup() { + System::set_block_number(1); +} + +pub fn new_tester() -> sp_io::TestExternalities { + let storage = frame_system::GenesisConfig::::default().build_storage().unwrap(); + let mut ext: sp_io::TestExternalities = storage.into(); + ext.execute_with(setup); + ext +} + +pub fn run_to_end_of_next_block() { + // finish current block + MessageQueue::on_finalize(System::block_number()); + OutboundQueue::on_finalize(System::block_number()); + System::on_finalize(System::block_number()); + // start next block + System::set_block_number(System::block_number() + 1); + System::on_initialize(System::block_number()); + OutboundQueue::on_initialize(System::block_number()); + MessageQueue::on_initialize(System::block_number()); + // finish next block + MessageQueue::on_finalize(System::block_number()); + OutboundQueue::on_finalize(System::block_number()); + System::on_finalize(System::block_number()); +} + +pub fn mock_governance_message() -> Message +where + T: Config, +{ + let _marker = PhantomData::; // for clippy + + Message { + id: None, + channel_id: PRIMARY_GOVERNANCE_CHANNEL, + command: Command::Upgrade { + impl_address: H160::zero(), + impl_code_hash: H256::zero(), + initializer: None, + }, + } +} + +// Message should fail validation as it is too large +pub fn mock_invalid_governance_message() -> Message +where + T: Config, +{ + let _marker = PhantomData::; // for clippy + + Message { + id: None, + channel_id: PRIMARY_GOVERNANCE_CHANNEL, + command: Command::Upgrade { + impl_address: H160::zero(), + impl_code_hash: H256::zero(), + initializer: Some(Initializer { + params: (0..1000).map(|_| 1u8).collect::>(), + maximum_required_gas: 0, + }), + }, + } +} + +pub fn mock_message(sibling_para_id: u32) -> Message { + Message { + id: None, + channel_id: ParaId::from(sibling_para_id).into(), + command: Command::AgentExecute { + agent_id: Default::default(), + command: AgentExecuteCommand::TransferToken { + token: Default::default(), + recipient: Default::default(), + amount: 0, + }, + }, + } +} diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/src/process_message_impl.rs b/bridges/snowbridge/parachain/pallets/outbound-queue/src/process_message_impl.rs new file mode 100644 index 000000000000..575ed9e0e7c2 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/src/process_message_impl.rs @@ -0,0 +1,23 @@ +//! Implementation for [`frame_support::traits::ProcessMessage`] +use super::*; +use crate::weights::WeightInfo; +use frame_support::{ + traits::{ProcessMessage, ProcessMessageError}, + weights::WeightMeter, +}; + +impl ProcessMessage for Pallet { + type Origin = AggregateMessageOrigin; + fn process_message( + message: &[u8], + origin: Self::Origin, + meter: &mut WeightMeter, + _: &mut [u8; 32], + ) -> Result { + let weight = T::WeightInfo::do_process_message(); + if meter.try_consume(weight).is_err() { + return Err(ProcessMessageError::Overweight(weight)) + } + Self::do_process_message(origin, message) + } +} diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/src/send_message_impl.rs b/bridges/snowbridge/parachain/pallets/outbound-queue/src/send_message_impl.rs new file mode 100644 index 000000000000..a84e2c520e59 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/src/send_message_impl.rs @@ -0,0 +1,98 @@ +//! Implementation for [`snowbridge_core::outbound::SendMessage`] +use super::*; +use bridge_hub_common::AggregateMessageOrigin; +use codec::Encode; +use frame_support::{ + ensure, + traits::{EnqueueMessage, Get}, + CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound, +}; +use frame_system::unique; +use snowbridge_core::{ + outbound::{ + Fee, Message, QueuedMessage, SendError, SendMessage, SendMessageFeeProvider, + VersionedQueuedMessage, + }, + ChannelId, PRIMARY_GOVERNANCE_CHANNEL, +}; +use sp_core::H256; +use sp_runtime::BoundedVec; + +/// The maximal length of an enqueued message, as determined by the MessageQueue pallet +pub type MaxEnqueuedMessageSizeOf = + <::MessageQueue as EnqueueMessage>::MaxMessageLen; + +#[derive(Encode, Decode, CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound)] +pub struct Ticket +where + T: Config, +{ + pub message_id: H256, + pub channel_id: ChannelId, + pub message: BoundedVec>, +} + +impl SendMessage for Pallet +where + T: Config, +{ + type Ticket = Ticket; + + fn validate( + message: &Message, + ) -> Result<(Self::Ticket, Fee<::Balance>), SendError> { + // The inner payload should not be too large + let payload = message.command.abi_encode(); + ensure!( + payload.len() < T::MaxMessagePayloadSize::get() as usize, + SendError::MessageTooLarge + ); + + // Ensure there is a registered channel we can transmit this message on + ensure!(T::Channels::contains(&message.channel_id), SendError::InvalidChannel); + + // Generate a unique message id unless one is provided + let message_id: H256 = message + .id + .unwrap_or_else(|| unique((message.channel_id, &message.command)).into()); + + let gas_used_at_most = T::GasMeter::maximum_gas_used_at_most(&message.command); + let fee = Self::calculate_fee(gas_used_at_most, T::PricingParameters::get()); + + let queued_message: VersionedQueuedMessage = QueuedMessage { + id: message_id, + channel_id: message.channel_id, + command: message.command.clone(), + } + .into(); + // The whole message should not be too large + let encoded = queued_message.encode().try_into().map_err(|_| SendError::MessageTooLarge)?; + + let ticket = Ticket { message_id, channel_id: message.channel_id, message: encoded }; + + Ok((ticket, fee)) + } + + fn deliver(ticket: Self::Ticket) -> Result { + let origin = AggregateMessageOrigin::Snowbridge(ticket.channel_id); + + if ticket.channel_id != PRIMARY_GOVERNANCE_CHANNEL { + ensure!(!Self::operating_mode().is_halted(), SendError::Halted); + } + + let message = ticket.message.as_bounded_slice(); + + T::MessageQueue::enqueue_message(message, origin); + Self::deposit_event(Event::MessageQueued { id: ticket.message_id }); + Ok(ticket.message_id) + } +} + +impl SendMessageFeeProvider for Pallet { + type Balance = T::Balance; + + /// The local component of the message processing fees in native currency + fn local_fee() -> Self::Balance { + Self::calculate_local_fee() + } +} diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/src/test.rs b/bridges/snowbridge/parachain/pallets/outbound-queue/src/test.rs new file mode 100644 index 000000000000..0028d75e7b79 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/src/test.rs @@ -0,0 +1,268 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use crate::{mock::*, *}; + +use frame_support::{ + assert_err, assert_noop, assert_ok, + traits::{Hooks, ProcessMessage, ProcessMessageError}, + weights::WeightMeter, +}; + +use codec::Encode; +use snowbridge_core::{ + outbound::{Command, SendError, SendMessage}, + ParaId, +}; +use sp_arithmetic::FixedU128; +use sp_core::H256; +use sp_runtime::FixedPointNumber; + +#[test] +fn submit_messages_and_commit() { + new_tester().execute_with(|| { + for para_id in 1000..1004 { + let message = mock_message(para_id); + let (ticket, _) = OutboundQueue::validate(&message).unwrap(); + assert_ok!(OutboundQueue::deliver(ticket)); + } + + ServiceWeight::set(Some(Weight::MAX)); + run_to_end_of_next_block(); + + for para_id in 1000..1004 { + let origin: ParaId = (para_id as u32).into(); + let channel_id: ChannelId = origin.into(); + assert_eq!(Nonce::::get(channel_id), 1); + } + + let digest = System::digest(); + let digest_items = digest.logs(); + assert!(digest_items.len() == 1 && digest_items[0].as_other().is_some()); + assert_eq!(Messages::::decode_len(), Some(4)); + }); +} + +#[test] +fn submit_message_fail_too_large() { + new_tester().execute_with(|| { + let message = mock_invalid_governance_message::(); + assert_err!(OutboundQueue::validate(&message), SendError::MessageTooLarge); + }); +} + +#[test] +fn convert_from_ether_decimals() { + assert_eq!( + OutboundQueue::convert_from_ether_decimals(1_000_000_000_000_000_000), + 1_000_000_000_000 + ); +} + +#[test] +fn commit_exits_early_if_no_processed_messages() { + new_tester().execute_with(|| { + // on_finalize should do nothing, nor should it panic + OutboundQueue::on_finalize(System::block_number()); + + let digest = System::digest(); + let digest_items = digest.logs(); + assert_eq!(digest_items.len(), 0); + }); +} + +#[test] +fn process_message_yields_on_max_messages_per_block() { + new_tester().execute_with(|| { + for _ in 0..::MaxMessagesPerBlock::get() { + MessageLeaves::::append(H256::zero()) + } + + let channel_id: ChannelId = ParaId::from(1000).into(); + let origin = AggregateMessageOrigin::Snowbridge(channel_id); + let message = QueuedMessage { + id: Default::default(), + channel_id, + command: Command::Upgrade { + impl_address: Default::default(), + impl_code_hash: Default::default(), + initializer: None, + }, + } + .encode(); + + let mut meter = WeightMeter::new(); + + assert_noop!( + OutboundQueue::process_message(message.as_slice(), origin, &mut meter, &mut [0u8; 32]), + ProcessMessageError::Yield + ); + }) +} + +#[test] +fn process_message_fails_on_max_nonce_reached() { + new_tester().execute_with(|| { + let sibling_id = 1000; + let channel_id: ChannelId = ParaId::from(sibling_id).into(); + let origin = AggregateMessageOrigin::Snowbridge(channel_id); + let message: QueuedMessage = QueuedMessage { + id: H256::zero(), + channel_id, + command: mock_message(sibling_id).command, + }; + let versioned_queued_message: VersionedQueuedMessage = message.try_into().unwrap(); + let encoded = versioned_queued_message.encode(); + let mut meter = WeightMeter::with_limit(Weight::MAX); + + Nonce::::set(channel_id, u64::MAX); + + assert_noop!( + OutboundQueue::process_message(encoded.as_slice(), origin, &mut meter, &mut [0u8; 32]), + ProcessMessageError::Unsupported + ); + }) +} + +#[test] +fn process_message_fails_on_overweight_message() { + new_tester().execute_with(|| { + let sibling_id = 1000; + let channel_id: ChannelId = ParaId::from(sibling_id).into(); + let origin = AggregateMessageOrigin::Snowbridge(channel_id); + let message: QueuedMessage = QueuedMessage { + id: H256::zero(), + channel_id, + command: mock_message(sibling_id).command, + }; + let versioned_queued_message: VersionedQueuedMessage = message.try_into().unwrap(); + let encoded = versioned_queued_message.encode(); + let mut meter = WeightMeter::with_limit(Weight::from_parts(1, 1)); + assert_noop!( + OutboundQueue::process_message(encoded.as_slice(), origin, &mut meter, &mut [0u8; 32]), + ProcessMessageError::Overweight(::WeightInfo::do_process_message()) + ); + }) +} + +// Governance messages should be able to bypass a halted operating mode +// Other message sends should fail when halted +#[test] +fn submit_upgrade_message_success_when_queue_halted() { + new_tester().execute_with(|| { + // halt the outbound queue + OutboundQueue::set_operating_mode(RuntimeOrigin::root(), BasicOperatingMode::Halted) + .unwrap(); + + // submit a high priority message from bridge_hub should success + let message = mock_governance_message::(); + let (ticket, _) = OutboundQueue::validate(&message).unwrap(); + assert_ok!(OutboundQueue::deliver(ticket)); + + // submit a low priority message from asset_hub will fail as pallet is halted + let message = mock_message(1000); + let (ticket, _) = OutboundQueue::validate(&message).unwrap(); + assert_noop!(OutboundQueue::deliver(ticket), SendError::Halted); + }); +} + +#[test] +fn governance_message_does_not_get_the_chance_to_processed_in_same_block_when_congest_of_low_priority_sibling_messages( +) { + use snowbridge_core::PRIMARY_GOVERNANCE_CHANNEL; + use AggregateMessageOrigin::*; + + let sibling_id: u32 = 1000; + let sibling_channel_id: ChannelId = ParaId::from(sibling_id).into(); + + new_tester().execute_with(|| { + // submit a lot of low priority messages from asset_hub which will need multiple blocks to + // execute(20 messages for each block so 40 required at least 2 blocks) + let max_messages = 40; + for _ in 0..max_messages { + // submit low priority message + let message = mock_message(sibling_id); + let (ticket, _) = OutboundQueue::validate(&message).unwrap(); + OutboundQueue::deliver(ticket).unwrap(); + } + + let footprint = MessageQueue::footprint(Snowbridge(sibling_channel_id)); + assert_eq!(footprint.storage.count, (max_messages) as u64); + + let message = mock_governance_message::(); + let (ticket, _) = OutboundQueue::validate(&message).unwrap(); + OutboundQueue::deliver(ticket).unwrap(); + + // move to next block + ServiceWeight::set(Some(Weight::MAX)); + run_to_end_of_next_block(); + + // first process 20 messages from sibling channel + let footprint = MessageQueue::footprint(Snowbridge(sibling_channel_id)); + assert_eq!(footprint.storage.count, 40 - 20); + + // and governance message does not have the chance to execute in same block + let footprint = MessageQueue::footprint(Snowbridge(PRIMARY_GOVERNANCE_CHANNEL)); + assert_eq!(footprint.storage.count, 1); + + // move to next block + ServiceWeight::set(Some(Weight::MAX)); + run_to_end_of_next_block(); + + // now governance message get executed in this block + let footprint = MessageQueue::footprint(Snowbridge(PRIMARY_GOVERNANCE_CHANNEL)); + assert_eq!(footprint.storage.count, 0); + + // and this time process 19 messages from sibling channel so we have 1 message left + let footprint = MessageQueue::footprint(Snowbridge(sibling_channel_id)); + assert_eq!(footprint.storage.count, 1); + + // move to the next block, the last 1 message from sibling channel get executed + ServiceWeight::set(Some(Weight::MAX)); + run_to_end_of_next_block(); + let footprint = MessageQueue::footprint(Snowbridge(sibling_channel_id)); + assert_eq!(footprint.storage.count, 0); + }); +} + +#[test] +fn convert_local_currency() { + new_tester().execute_with(|| { + let fee: u128 = 1_000_000; + let fee1 = FixedU128::from_inner(fee).into_inner(); + let fee2 = FixedU128::from(fee) + .into_inner() + .checked_div(FixedU128::accuracy()) + .expect("accuracy is not zero; qed"); + assert_eq!(fee, fee1); + assert_eq!(fee, fee2); + }); +} + +#[test] +fn encode_digest_item_with_correct_index() { + new_tester().execute_with(|| { + let digest_item: DigestItem = CustomDigestItem::Snowbridge(H256::default()).into(); + let enum_prefix = match digest_item { + DigestItem::Other(data) => data[0], + _ => u8::MAX, + }; + assert_eq!(enum_prefix, 0); + }); +} + +#[test] +fn encode_digest_item() { + new_tester().execute_with(|| { + let digest_item: DigestItem = CustomDigestItem::Snowbridge([5u8; 32].into()).into(); + let digest_item_raw = digest_item.encode(); + assert_eq!(digest_item_raw[0], 0); // DigestItem::Other + assert_eq!(digest_item_raw[2], 0); // CustomDigestItem::Snowbridge + assert_eq!( + digest_item_raw, + [ + 0, 132, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5 + ] + ); + }); +} diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/src/types.rs b/bridges/snowbridge/parachain/pallets/outbound-queue/src/types.rs new file mode 100644 index 000000000000..07803ed9b738 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/src/types.rs @@ -0,0 +1,99 @@ +use codec::{Decode, Encode, MaxEncodedLen}; +use ethabi::Token; +use frame_support::traits::ProcessMessage; +use scale_info::TypeInfo; +use serde::{Deserialize, Serialize}; +use sp_arithmetic::FixedU128; +use sp_core::H256; +use sp_runtime::{traits::Zero, RuntimeDebug}; +use sp_std::prelude::*; + +use super::Pallet; + +use snowbridge_core::ChannelId; +pub use snowbridge_outbound_queue_merkle_tree::MerkleProof; + +pub type ProcessMessageOriginOf = as ProcessMessage>::Origin; + +pub const LOG_TARGET: &str = "snowbridge-outbound-queue"; + +/// Message which has been assigned a nonce and will be committed at the end of a block +#[derive(Encode, Decode, Clone, PartialEq, RuntimeDebug, TypeInfo)] +pub struct CommittedMessage { + /// Message channel + pub channel_id: ChannelId, + /// Unique nonce to prevent replaying messages + #[codec(compact)] + pub nonce: u64, + /// Command to execute in the Gateway contract + pub command: u8, + /// Params for the command + pub params: Vec, + /// Maximum gas allowed for message dispatch + #[codec(compact)] + pub max_dispatch_gas: u64, + /// Maximum fee per gas + #[codec(compact)] + pub max_fee_per_gas: u128, + /// Reward in ether for delivering this message, in addition to the gas refund + #[codec(compact)] + pub reward: u128, + /// Message ID (Used for tracing messages across route, has no role in consensus) + pub id: H256, +} + +/// Convert message into an ABI-encoded form for delivery to the InboundQueue contract on Ethereum +impl From for Token { + fn from(x: CommittedMessage) -> Token { + Token::Tuple(vec![ + Token::FixedBytes(Vec::from(x.channel_id.as_ref())), + Token::Uint(x.nonce.into()), + Token::Uint(x.command.into()), + Token::Bytes(x.params.to_vec()), + Token::Uint(x.max_dispatch_gas.into()), + Token::Uint(x.max_fee_per_gas.into()), + Token::Uint(x.reward.into()), + Token::FixedBytes(Vec::from(x.id.as_ref())), + ]) + } +} + +/// Configuration for fee calculations +#[derive( + Encode, + Decode, + Copy, + Clone, + PartialEq, + RuntimeDebug, + MaxEncodedLen, + TypeInfo, + Serialize, + Deserialize, +)] +pub struct FeeConfigRecord { + /// ETH/DOT exchange rate + pub exchange_rate: FixedU128, + /// Ether fee per unit of gas + pub fee_per_gas: u128, + /// Ether reward for delivering message + pub reward: u128, +} + +#[derive(RuntimeDebug)] +pub struct InvalidFeeConfig; + +impl FeeConfigRecord { + pub fn validate(&self) -> Result<(), InvalidFeeConfig> { + if self.exchange_rate == FixedU128::zero() { + return Err(InvalidFeeConfig) + } + if self.fee_per_gas == 0 { + return Err(InvalidFeeConfig) + } + if self.reward == 0 { + return Err(InvalidFeeConfig) + } + Ok(()) + } +} diff --git a/bridges/snowbridge/parachain/pallets/outbound-queue/src/weights.rs b/bridges/snowbridge/parachain/pallets/outbound-queue/src/weights.rs new file mode 100644 index 000000000000..e4b6f8439b0f --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/outbound-queue/src/weights.rs @@ -0,0 +1,81 @@ + +//! Autogenerated weights for `snowbridge_outbound_queue` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-10-19, STEPS: `2`, REPEAT: `1`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `192.168.1.7`, CPU: `` +//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("bridge-hub-rococo-dev")`, DB CACHE: `1024` + +// Executed Command: +// target/release/polkadot-parachain +// benchmark +// pallet +// --chain=bridge-hub-rococo-dev +// --pallet=snowbridge_outbound_queue +// --extrinsic=* +// --execution=wasm +// --wasm-execution=compiled +// --template +// ../parachain/templates/module-weight-template.hbs +// --output +// ../parachain/pallets/outbound-queue/src/weights.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(missing_docs)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use core::marker::PhantomData; + +/// Weight functions needed for `snowbridge_outbound_queue`. +pub trait WeightInfo { + fn do_process_message() -> Weight; + fn commit() -> Weight; + fn commit_single() -> Weight; +} + +// For backwards compatibility and tests. +impl WeightInfo for () { + /// Storage: EthereumOutboundQueue MessageLeaves (r:1 w:1) + /// Proof Skipped: EthereumOutboundQueue MessageLeaves (max_values: Some(1), max_size: None, mode: Measured) + /// Storage: EthereumOutboundQueue PendingHighPriorityMessageCount (r:1 w:1) + /// Proof: EthereumOutboundQueue PendingHighPriorityMessageCount (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue Nonce (r:1 w:1) + /// Proof: EthereumOutboundQueue Nonce (max_values: None, max_size: Some(20), added: 2495, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue Messages (r:1 w:1) + /// Proof Skipped: EthereumOutboundQueue Messages (max_values: Some(1), max_size: None, mode: Measured) + fn do_process_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `42` + // Estimated: `3485` + // Minimum execution time: 39_000_000 picoseconds. + Weight::from_parts(39_000_000, 3485) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().writes(4_u64)) + } + /// Storage: EthereumOutboundQueue MessageLeaves (r:1 w:0) + /// Proof Skipped: EthereumOutboundQueue MessageLeaves (max_values: Some(1), max_size: None, mode: Measured) + /// Storage: System Digest (r:1 w:1) + /// Proof Skipped: System Digest (max_values: Some(1), max_size: None, mode: Measured) + fn commit() -> Weight { + // Proof Size summary in bytes: + // Measured: `1094` + // Estimated: `2579` + // Minimum execution time: 28_000_000 picoseconds. + Weight::from_parts(28_000_000, 2579) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + + fn commit_single() -> Weight { + // Proof Size summary in bytes: + // Measured: `1094` + // Estimated: `2579` + // Minimum execution time: 9_000_000 picoseconds. + Weight::from_parts(9_000_000, 1586) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } +} diff --git a/bridges/snowbridge/parachain/pallets/system/Cargo.toml b/bridges/snowbridge/parachain/pallets/system/Cargo.toml new file mode 100644 index 000000000000..4356bf572205 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/system/Cargo.toml @@ -0,0 +1,83 @@ +[package] +name = "snowbridge-system" +description = "Snowbridge System" +version = "0.1.1" +authors = ["Snowfork "] +edition = "2021" +repository = "https://github.com/Snowfork/snowbridge" +license = "Apache-2.0" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = [ + "derive", +] } +scale-info = { version = "2.9.0", default-features = false, features = ["derive"] } +frame-benchmarking = { path = "../../../../../substrate/frame/benchmarking", default-features = false, optional = true } +frame-support = { path = "../../../../../substrate/frame/support", default-features = false } +frame-system = { path = "../../../../../substrate/frame/system", default-features = false } +log = { version = "0.4.20", default-features = false } + +sp-core = { path = "../../../../../substrate/primitives/core", default-features = false } +sp-std = { path = "../../../../../substrate/primitives/std", default-features = false } +sp-io = { path = "../../../../../substrate/primitives/io", default-features = false } +sp-runtime = { path = "../../../../../substrate/primitives/runtime", default-features = false } + +xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false } +xcm-builder = { package = "staging-xcm-builder", path = "../../../../../polkadot/xcm/xcm-builder", default-features = false } +xcm-executor = { package = "staging-xcm-executor", path = "../../../../../polkadot/xcm/xcm-executor", default-features = false } + +ethabi = { git = "https://github.com/Snowfork/ethabi-decode.git", package = "ethabi-decode", branch = "master", default-features = false } +snowbridge-core = { path = "../../primitives/core", default-features = false } + +[dev-dependencies] +hex = "0.4.1" +hex-literal = { version = "0.4.1" } +pallet-balances = { path = "../../../../../substrate/frame/balances" } +sp-keyring = { path = "../../../../../substrate/primitives/keyring" } +polkadot-primitives = { path = "../../../../../polkadot/primitives" } +pallet-message-queue = { path = "../../../../../substrate/frame/message-queue" } +snowbridge-outbound-queue = { path = "../outbound-queue" } + +[features] +default = ["std"] +std = [ + "codec/std", + "ethabi/std", + "frame-benchmarking?/std", + "frame-support/std", + "frame-system/std", + "log/std", + "scale-info/std", + "snowbridge-core/std", + "sp-core/std", + "sp-io/std", + "sp-runtime/std", + "sp-std/std", + "xcm-builder/std", + "xcm-executor/std", + "xcm/std", +] +runtime-benchmarks = [ + "frame-benchmarking/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "pallet-balances/runtime-benchmarks", + "pallet-message-queue/runtime-benchmarks", + "polkadot-primitives/runtime-benchmarks", + "snowbridge-core/runtime-benchmarks", + "snowbridge-outbound-queue/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", + "xcm-builder/runtime-benchmarks", + "xcm-executor/runtime-benchmarks", +] +try-runtime = [ + "frame-support/try-runtime", + "frame-system/try-runtime", + "pallet-balances/try-runtime", + "pallet-message-queue/try-runtime", + "snowbridge-outbound-queue/try-runtime", + "sp-runtime/try-runtime", +] diff --git a/bridges/snowbridge/parachain/pallets/system/README.md b/bridges/snowbridge/parachain/pallets/system/README.md new file mode 100644 index 000000000000..9e4dc55267d6 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/system/README.md @@ -0,0 +1 @@ +License: MIT-0 diff --git a/bridges/snowbridge/parachain/pallets/system/runtime-api/Cargo.toml b/bridges/snowbridge/parachain/pallets/system/runtime-api/Cargo.toml new file mode 100644 index 000000000000..97d0735bf63d --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/system/runtime-api/Cargo.toml @@ -0,0 +1,32 @@ +[package] +name = "snowbridge-system-runtime-api" +description = "Snowbridge System Runtime API" +version = "0.1.0" +edition = "2021" +authors = ["Snowfork "] +repository = "https://github.com/Snowfork/snowbridge" +license = "Apache-2.0" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = [ + "derive", +] } +sp-core = { path = "../../../../../../substrate/primitives/core", default-features = false } +sp-std = { path = "../../../../../../substrate/primitives/std", default-features = false } +sp-api = { path = "../../../../../../substrate/primitives/api", default-features = false } +xcm = { package = "staging-xcm", path = "../../../../../../polkadot/xcm", default-features = false } +snowbridge-core = { path = "../../../primitives/core", default-features = false } + +[features] +default = ["std"] +std = [ + "codec/std", + "snowbridge-core/std", + "sp-api/std", + "sp-core/std", + "sp-std/std", + "xcm/std", +] diff --git a/bridges/snowbridge/parachain/pallets/system/runtime-api/src/lib.rs b/bridges/snowbridge/parachain/pallets/system/runtime-api/src/lib.rs new file mode 100644 index 000000000000..d99b456c8488 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/system/runtime-api/src/lib.rs @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +#![cfg_attr(not(feature = "std"), no_std)] + +use snowbridge_core::AgentId; +use xcm::VersionedMultiLocation; + +sp_api::decl_runtime_apis! { + pub trait ControlApi + { + fn agent_id(location: VersionedMultiLocation) -> Option; + } +} diff --git a/bridges/snowbridge/parachain/pallets/system/src/api.rs b/bridges/snowbridge/parachain/pallets/system/src/api.rs new file mode 100644 index 000000000000..245e6eea1c14 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/system/src/api.rs @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! Helpers for implementing runtime api + +use snowbridge_core::AgentId; +use xcm::{prelude::*, VersionedMultiLocation}; + +use crate::{agent_id_of, Config}; + +pub fn agent_id(location: VersionedMultiLocation) -> Option +where + Runtime: Config, +{ + let location: MultiLocation = location.try_into().ok()?; + agent_id_of::(&location).ok() +} diff --git a/bridges/snowbridge/parachain/pallets/system/src/benchmarking.rs b/bridges/snowbridge/parachain/pallets/system/src/benchmarking.rs new file mode 100644 index 000000000000..8d26408b38e5 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/system/src/benchmarking.rs @@ -0,0 +1,167 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! Benchmarking setup for pallet-template +use super::*; + +#[allow(unused)] +use crate::Pallet as SnowbridgeControl; +use frame_benchmarking::v2::*; +use frame_system::RawOrigin; +use snowbridge_core::{eth, outbound::OperatingMode}; +use sp_runtime::SaturatedConversion; +use xcm::prelude::*; + +#[allow(clippy::result_large_err)] +fn fund_sovereign_account(para_id: ParaId) -> Result<(), BenchmarkError> { + let amount: BalanceOf = (10_000_000_000_000_u64).saturated_into::().saturated_into(); + let sovereign_account = sibling_sovereign_account::(para_id); + T::Token::mint_into(&sovereign_account, amount)?; + Ok(()) +} + +#[benchmarks] +mod benchmarks { + use super::*; + + #[benchmark] + fn upgrade() -> Result<(), BenchmarkError> { + let impl_address = H160::repeat_byte(1); + let impl_code_hash = H256::repeat_byte(1); + + // Assume 256 bytes passed to initializer + let params: Vec = (0..256).map(|_| 1u8).collect(); + + #[extrinsic_call] + _( + RawOrigin::Root, + impl_address, + impl_code_hash, + Some(Initializer { params, maximum_required_gas: 100000 }), + ); + + Ok(()) + } + + #[benchmark] + fn set_operating_mode() -> Result<(), BenchmarkError> { + #[extrinsic_call] + _(RawOrigin::Root, OperatingMode::RejectingOutboundMessages); + + Ok(()) + } + + #[benchmark] + fn set_pricing_parameters() -> Result<(), BenchmarkError> { + let params = T::DefaultPricingParameters::get(); + + #[extrinsic_call] + _(RawOrigin::Root, params); + + Ok(()) + } + + #[benchmark] + fn create_agent() -> Result<(), BenchmarkError> { + let origin_para_id = 2000; + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(origin_para_id)) }; + let origin = T::Helper::make_xcm_origin(origin_location); + fund_sovereign_account::(origin_para_id.into())?; + + #[extrinsic_call] + _(origin as T::RuntimeOrigin); + + Ok(()) + } + + #[benchmark] + fn create_channel() -> Result<(), BenchmarkError> { + let origin_para_id = 2000; + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(origin_para_id)) }; + let origin = T::Helper::make_xcm_origin(origin_location); + fund_sovereign_account::(origin_para_id.into())?; + + SnowbridgeControl::::create_agent(origin.clone())?; + + #[extrinsic_call] + _(origin as T::RuntimeOrigin, OperatingMode::Normal); + + Ok(()) + } + + #[benchmark] + fn update_channel() -> Result<(), BenchmarkError> { + let origin_para_id = 2000; + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(origin_para_id)) }; + let origin = T::Helper::make_xcm_origin(origin_location); + fund_sovereign_account::(origin_para_id.into())?; + SnowbridgeControl::::create_agent(origin.clone())?; + SnowbridgeControl::::create_channel(origin.clone(), OperatingMode::Normal)?; + + #[extrinsic_call] + _(origin as T::RuntimeOrigin, OperatingMode::RejectingOutboundMessages); + + Ok(()) + } + + #[benchmark] + fn force_update_channel() -> Result<(), BenchmarkError> { + let origin_para_id = 2000; + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(origin_para_id)) }; + let origin = T::Helper::make_xcm_origin(origin_location); + let channel_id: ChannelId = ParaId::from(origin_para_id).into(); + + fund_sovereign_account::(origin_para_id.into())?; + SnowbridgeControl::::create_agent(origin.clone())?; + SnowbridgeControl::::create_channel(origin.clone(), OperatingMode::Normal)?; + + #[extrinsic_call] + _(RawOrigin::Root, channel_id, OperatingMode::RejectingOutboundMessages); + + Ok(()) + } + + #[benchmark] + fn transfer_native_from_agent() -> Result<(), BenchmarkError> { + let origin_para_id = 2000; + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(origin_para_id)) }; + let origin = T::Helper::make_xcm_origin(origin_location); + fund_sovereign_account::(origin_para_id.into())?; + SnowbridgeControl::::create_agent(origin.clone())?; + SnowbridgeControl::::create_channel(origin.clone(), OperatingMode::Normal)?; + + #[extrinsic_call] + _(origin as T::RuntimeOrigin, H160::default(), 1); + + Ok(()) + } + + #[benchmark] + fn force_transfer_native_from_agent() -> Result<(), BenchmarkError> { + let origin_para_id = 2000; + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(origin_para_id)) }; + let origin = T::Helper::make_xcm_origin(origin_location); + fund_sovereign_account::(origin_para_id.into())?; + SnowbridgeControl::::create_agent(origin.clone())?; + + let versioned_location: VersionedMultiLocation = origin_location.into(); + + #[extrinsic_call] + _(RawOrigin::Root, Box::new(versioned_location), H160::default(), 1); + + Ok(()) + } + + #[benchmark] + fn set_token_transfer_fees() -> Result<(), BenchmarkError> { + #[extrinsic_call] + _(RawOrigin::Root, 1, 1, eth(1)); + + Ok(()) + } + + impl_benchmark_test_suite!( + SnowbridgeControl, + crate::mock::new_test_ext(true), + crate::mock::Test + ); +} diff --git a/bridges/snowbridge/parachain/pallets/system/src/lib.rs b/bridges/snowbridge/parachain/pallets/system/src/lib.rs new file mode 100644 index 000000000000..0042093ee662 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/system/src/lib.rs @@ -0,0 +1,681 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! Governance API for controlling the Ethereum side of the bridge +//! +//! # Extrinsics +//! +//! ## Agents +//! +//! Agents are smart contracts on Ethereum that act as proxies for consensus systems on Polkadot +//! networks. +//! +//! * [`Call::create_agent`]: Create agent for a sibling parachain +//! * [`Call::transfer_native_from_agent`]: Withdraw ether from an agent +//! +//! The `create_agent` extrinsic should be called via an XCM `Transact` instruction from the sibling +//! parachain. +//! +//! ## Channels +//! +//! Each sibling parachain has its own dedicated messaging channel for sending and receiving +//! messages. As a prerequisite to creating a channel, the sibling should have already created +//! an agent using the `create_agent` extrinsic. +//! +//! * [`Call::create_channel`]: Create channel for a sibling +//! * [`Call::update_channel`]: Update a channel for a sibling +//! +//! ## Governance +//! +//! Only Polkadot governance itself can call these extrinsics. Delivery fees are waived. +//! +//! * [`Call::upgrade`]`: Upgrade the gateway contract +//! * [`Call::set_operating_mode`]: Update the operating mode of the gateway contract +//! * [`Call::force_update_channel`]: Allow root to update a channel for a sibling +//! * [`Call::force_transfer_native_from_agent`]: Allow root to withdraw ether from an agent +//! +//! Typically, Polkadot governance will use the `force_transfer_native_from_agent` and +//! `force_update_channel` and extrinsics to manage agents and channels for system parachains. +#![cfg_attr(not(feature = "std"), no_std)] + +pub use pallet::*; + +#[cfg(test)] +mod mock; + +#[cfg(test)] +mod tests; + +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; +pub mod migration; + +pub mod api; +pub mod weights; +pub use weights::*; + +use frame_support::{ + pallet_prelude::*, + traits::{ + fungible::{Inspect, Mutate}, + tokens::Preservation, + Contains, EnsureOrigin, + }, +}; +use frame_system::pallet_prelude::*; +use snowbridge_core::{ + meth, + outbound::{Command, Initializer, Message, OperatingMode, SendError, SendMessage}, + sibling_sovereign_account, AgentId, Channel, ChannelId, ParaId, + PricingParameters as PricingParametersRecord, PRIMARY_GOVERNANCE_CHANNEL, + SECONDARY_GOVERNANCE_CHANNEL, +}; +use sp_core::{RuntimeDebug, H160, H256}; +use sp_io::hashing::blake2_256; +use sp_runtime::{traits::BadOrigin, DispatchError, SaturatedConversion}; +use sp_std::prelude::*; +use xcm::prelude::*; +use xcm_executor::traits::ConvertLocation; + +#[cfg(feature = "runtime-benchmarks")] +use frame_support::traits::OriginTrait; + +pub use pallet::*; + +pub type BalanceOf = + <::Token as Inspect<::AccountId>>::Balance; +pub type AccountIdOf = ::AccountId; +pub type PricingParametersOf = PricingParametersRecord>; + +/// Ensure origin location is a sibling +fn ensure_sibling(location: &MultiLocation) -> Result<(ParaId, H256), DispatchError> +where + T: Config, +{ + match location { + MultiLocation { parents: 1, interior: X1(Parachain(para_id)) } => { + let agent_id = agent_id_of::(location)?; + Ok(((*para_id).into(), agent_id)) + }, + _ => Err(BadOrigin.into()), + } +} + +/// Hash the location to produce an agent id +fn agent_id_of(location: &MultiLocation) -> Result { + T::AgentIdOf::convert_location(location).ok_or(Error::::LocationConversionFailed.into()) +} + +#[cfg(feature = "runtime-benchmarks")] +pub trait BenchmarkHelper +where + O: OriginTrait, +{ + fn make_xcm_origin(location: MultiLocation) -> O; +} + +/// Whether a fee should be withdrawn to an account for sending an outbound message +#[derive(Clone, PartialEq, RuntimeDebug)] +pub enum PaysFee +where + T: Config, +{ + /// Fully charge includes (local + remote fee) + Yes(AccountIdOf), + /// Partially charge includes local fee only + Partial(AccountIdOf), + /// No charge + No, +} + +#[frame_support::pallet] +pub mod pallet { + use snowbridge_core::StaticLookup; + use sp_core::U256; + + use super::*; + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config { + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + + /// Send messages to Ethereum + type OutboundQueue: SendMessage>; + + /// Origin check for XCM locations that can create agents + type SiblingOrigin: EnsureOrigin; + + /// Converts MultiLocation to AgentId + type AgentIdOf: ConvertLocation; + + /// Token reserved for control operations + type Token: Mutate; + + /// TreasuryAccount to collect fees + #[pallet::constant] + type TreasuryAccount: Get; + + /// Number of decimal places of local currency + type DefaultPricingParameters: Get>; + + /// Cost of delivering a message from Ethereum + type InboundDeliveryCost: Get>; + + type WeightInfo: WeightInfo; + + #[cfg(feature = "runtime-benchmarks")] + type Helper: BenchmarkHelper; + } + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + /// An Upgrade message was sent to the Gateway + Upgrade { + impl_address: H160, + impl_code_hash: H256, + initializer_params_hash: Option, + }, + /// An CreateAgent message was sent to the Gateway + CreateAgent { + location: Box, + agent_id: AgentId, + }, + /// An CreateChannel message was sent to the Gateway + CreateChannel { + channel_id: ChannelId, + agent_id: AgentId, + }, + /// An UpdateChannel message was sent to the Gateway + UpdateChannel { + channel_id: ChannelId, + mode: OperatingMode, + }, + /// An SetOperatingMode message was sent to the Gateway + SetOperatingMode { + mode: OperatingMode, + }, + /// An TransferNativeFromAgent message was sent to the Gateway + TransferNativeFromAgent { + agent_id: AgentId, + recipient: H160, + amount: u128, + }, + /// A SetTokenTransferFees message was sent to the Gateway + SetTokenTransferFees { + create_asset_xcm: u128, + transfer_asset_xcm: u128, + register_token: U256, + }, + PricingParametersChanged { + params: PricingParametersOf, + }, + } + + #[pallet::error] + pub enum Error { + LocationConversionFailed, + AgentAlreadyCreated, + NoAgent, + ChannelAlreadyCreated, + NoChannel, + UnsupportedLocationVersion, + InvalidLocation, + Send(SendError), + InvalidTokenTransferFees, + InvalidPricingParameters, + } + + /// The set of registered agents + #[pallet::storage] + #[pallet::getter(fn agents)] + pub type Agents = StorageMap<_, Twox64Concat, AgentId, (), OptionQuery>; + + /// The set of registered channels + #[pallet::storage] + #[pallet::getter(fn channels)] + pub type Channels = StorageMap<_, Twox64Concat, ChannelId, Channel, OptionQuery>; + + #[pallet::storage] + #[pallet::getter(fn parameters)] + pub type PricingParameters = + StorageValue<_, PricingParametersOf, ValueQuery, T::DefaultPricingParameters>; + + #[pallet::genesis_config] + #[derive(frame_support::DefaultNoBound)] + pub struct GenesisConfig { + // Own parachain id + pub para_id: ParaId, + // AssetHub's parachain id + pub asset_hub_para_id: ParaId, + #[serde(skip)] + pub _config: PhantomData, + } + + #[pallet::genesis_build] + impl BuildGenesisConfig for GenesisConfig { + fn build(&self) { + Pallet::::initialize(self.para_id, self.asset_hub_para_id).expect("infallible; qed"); + } + } + + #[pallet::call] + impl Pallet { + /// Sends command to the Gateway contract to upgrade itself with a new implementation + /// contract + /// + /// Fee required: No + /// + /// - `origin`: Must be `Root`. + /// - `impl_address`: The address of the implementation contract. + /// - `impl_code_hash`: The codehash of the implementation contract. + /// - `initializer`: Optionally call an initializer on the implementation contract. + #[pallet::call_index(0)] + #[pallet::weight((T::WeightInfo::upgrade(), DispatchClass::Operational))] + pub fn upgrade( + origin: OriginFor, + impl_address: H160, + impl_code_hash: H256, + initializer: Option, + ) -> DispatchResult { + ensure_root(origin)?; + + let initializer_params_hash: Option = + initializer.as_ref().map(|i| H256::from(blake2_256(i.params.as_ref()))); + let command = Command::Upgrade { impl_address, impl_code_hash, initializer }; + Self::send(PRIMARY_GOVERNANCE_CHANNEL, command, PaysFee::::No)?; + + Self::deposit_event(Event::::Upgrade { + impl_address, + impl_code_hash, + initializer_params_hash, + }); + Ok(()) + } + + /// Sends a message to the Gateway contract to change its operating mode + /// + /// Fee required: No + /// + /// - `origin`: Must be `MultiLocation` + #[pallet::call_index(1)] + #[pallet::weight((T::WeightInfo::set_operating_mode(), DispatchClass::Operational))] + pub fn set_operating_mode(origin: OriginFor, mode: OperatingMode) -> DispatchResult { + ensure_root(origin)?; + + let command = Command::SetOperatingMode { mode }; + Self::send(PRIMARY_GOVERNANCE_CHANNEL, command, PaysFee::::No)?; + + Self::deposit_event(Event::::SetOperatingMode { mode }); + Ok(()) + } + + /// Set pricing parameters on both sides of the bridge + /// + /// Fee required: No + /// + /// - `origin`: Must be root + #[pallet::call_index(2)] + #[pallet::weight((T::WeightInfo::set_pricing_parameters(), DispatchClass::Operational))] + pub fn set_pricing_parameters( + origin: OriginFor, + params: PricingParametersOf, + ) -> DispatchResult { + ensure_root(origin)?; + params.validate().map_err(|_| Error::::InvalidPricingParameters)?; + PricingParameters::::put(params.clone()); + + let command = Command::SetPricingParameters { + exchange_rate: params.exchange_rate.into(), + delivery_cost: T::InboundDeliveryCost::get().saturated_into::(), + }; + Self::send(PRIMARY_GOVERNANCE_CHANNEL, command, PaysFee::::No)?; + + Self::deposit_event(Event::PricingParametersChanged { params }); + Ok(()) + } + + /// Sends a command to the Gateway contract to instantiate a new agent contract representing + /// `origin`. + /// + /// Fee required: Yes + /// + /// - `origin`: Must be `MultiLocation` of a sibling parachain + #[pallet::call_index(3)] + #[pallet::weight(T::WeightInfo::create_agent())] + pub fn create_agent(origin: OriginFor) -> DispatchResult { + let origin_location: MultiLocation = T::SiblingOrigin::ensure_origin(origin)?; + + // Ensure that origin location is some consensus system on a sibling parachain + let (para_id, agent_id) = ensure_sibling::(&origin_location)?; + + // Record the agent id or fail if it has already been created + ensure!(!Agents::::contains_key(agent_id), Error::::AgentAlreadyCreated); + Agents::::insert(agent_id, ()); + + let command = Command::CreateAgent { agent_id }; + let pays_fee = PaysFee::::Yes(sibling_sovereign_account::(para_id)); + Self::send(SECONDARY_GOVERNANCE_CHANNEL, command, pays_fee)?; + + Self::deposit_event(Event::::CreateAgent { + location: Box::new(origin_location), + agent_id, + }); + Ok(()) + } + + /// Sends a message to the Gateway contract to create a new channel representing `origin` + /// + /// Fee required: Yes + /// + /// This extrinsic is permissionless, so a fee is charged to prevent spamming and pay + /// for execution costs on the remote side. + /// + /// The message is sent over the bridge on BridgeHub's own channel to the Gateway. + /// + /// - `origin`: Must be `MultiLocation` + /// - `mode`: Initial operating mode of the channel + #[pallet::call_index(4)] + #[pallet::weight(T::WeightInfo::create_channel())] + pub fn create_channel(origin: OriginFor, mode: OperatingMode) -> DispatchResult { + let origin_location: MultiLocation = T::SiblingOrigin::ensure_origin(origin)?; + + // Ensure that origin location is a sibling parachain + let (para_id, agent_id) = ensure_sibling::(&origin_location)?; + + let channel_id: ChannelId = para_id.into(); + + ensure!(Agents::::contains_key(agent_id), Error::::NoAgent); + ensure!(!Channels::::contains_key(channel_id), Error::::ChannelAlreadyCreated); + + let channel = Channel { agent_id, para_id }; + Channels::::insert(channel_id, channel); + + let command = Command::CreateChannel { channel_id, agent_id, mode }; + let pays_fee = PaysFee::::Yes(sibling_sovereign_account::(para_id)); + Self::send(SECONDARY_GOVERNANCE_CHANNEL, command, pays_fee)?; + + Self::deposit_event(Event::::CreateChannel { channel_id, agent_id }); + Ok(()) + } + + /// Sends a message to the Gateway contract to update a channel configuration + /// + /// The origin must already have a channel initialized, as this message is sent over it. + /// + /// A partial fee will be charged for local processing only. + /// + /// - `origin`: Must be `MultiLocation` + /// - `mode`: Initial operating mode of the channel + #[pallet::call_index(5)] + #[pallet::weight(T::WeightInfo::update_channel())] + pub fn update_channel(origin: OriginFor, mode: OperatingMode) -> DispatchResult { + let origin_location: MultiLocation = T::SiblingOrigin::ensure_origin(origin)?; + + // Ensure that origin location is a sibling parachain + let (para_id, _) = ensure_sibling::(&origin_location)?; + + let channel_id: ChannelId = para_id.into(); + + ensure!(Channels::::contains_key(channel_id), Error::::NoChannel); + + let command = Command::UpdateChannel { channel_id, mode }; + let pays_fee = PaysFee::::Partial(sibling_sovereign_account::(para_id)); + + // Parachains send the update message on their own channel + Self::send(channel_id, command, pays_fee)?; + + Self::deposit_event(Event::::UpdateChannel { channel_id, mode }); + Ok(()) + } + + /// Sends a message to the Gateway contract to update an arbitrary channel + /// + /// Fee required: No + /// + /// - `origin`: Must be root + /// - `channel_id`: ID of channel + /// - `mode`: Initial operating mode of the channel + /// - `outbound_fee`: Fee charged to users for sending outbound messages to Polkadot + #[pallet::call_index(6)] + #[pallet::weight(T::WeightInfo::force_update_channel())] + pub fn force_update_channel( + origin: OriginFor, + channel_id: ChannelId, + mode: OperatingMode, + ) -> DispatchResult { + ensure_root(origin)?; + + ensure!(Channels::::contains_key(channel_id), Error::::NoChannel); + + let command = Command::UpdateChannel { channel_id, mode }; + Self::send(PRIMARY_GOVERNANCE_CHANNEL, command, PaysFee::::No)?; + + Self::deposit_event(Event::::UpdateChannel { channel_id, mode }); + Ok(()) + } + + /// Sends a message to the Gateway contract to transfer ether from an agent to `recipient`. + /// + /// A partial fee will be charged for local processing only. + /// + /// - `origin`: Must be `MultiLocation` + #[pallet::call_index(7)] + #[pallet::weight(T::WeightInfo::transfer_native_from_agent())] + pub fn transfer_native_from_agent( + origin: OriginFor, + recipient: H160, + amount: u128, + ) -> DispatchResult { + let origin_location: MultiLocation = T::SiblingOrigin::ensure_origin(origin)?; + + // Ensure that origin location is some consensus system on a sibling parachain + let (para_id, agent_id) = ensure_sibling::(&origin_location)?; + + // Since the origin is also the owner of the channel, they only need to pay + // the local processing fee. + let pays_fee = PaysFee::::Partial(sibling_sovereign_account::(para_id)); + + Self::do_transfer_native_from_agent( + agent_id, + para_id.into(), + recipient, + amount, + pays_fee, + ) + } + + /// Sends a message to the Gateway contract to transfer ether from an agent to `recipient`. + /// + /// Privileged. Can only be called by root. + /// + /// Fee required: No + /// + /// - `origin`: Must be root + /// - `location`: Location used to resolve the agent + /// - `recipient`: Recipient of funds + /// - `amount`: Amount to transfer + #[pallet::call_index(8)] + #[pallet::weight(T::WeightInfo::force_transfer_native_from_agent())] + pub fn force_transfer_native_from_agent( + origin: OriginFor, + location: Box, + recipient: H160, + amount: u128, + ) -> DispatchResult { + ensure_root(origin)?; + + // Ensure that location is some consensus system on a sibling parachain + let location: MultiLocation = + (*location).try_into().map_err(|_| Error::::UnsupportedLocationVersion)?; + let (_, agent_id) = + ensure_sibling::(&location).map_err(|_| Error::::InvalidLocation)?; + + let pays_fee = PaysFee::::No; + + Self::do_transfer_native_from_agent( + agent_id, + PRIMARY_GOVERNANCE_CHANNEL, + recipient, + amount, + pays_fee, + ) + } + + /// Sends a message to the Gateway contract to update fee related parameters for + /// token transfers. + /// + /// Privileged. Can only be called by root. + /// + /// Fee required: No + /// + /// - `origin`: Must be root + /// - `create_asset_xcm`: The XCM execution cost for creating a new asset class on AssetHub, + /// in DOT + /// - `transfer_asset_xcm`: The XCM execution cost for performing a reserve transfer on + /// AssetHub, in DOT + /// - `register_token`: The Ether fee for registering a new token, to discourage spamming + #[pallet::call_index(9)] + #[pallet::weight((T::WeightInfo::set_token_transfer_fees(), DispatchClass::Operational))] + pub fn set_token_transfer_fees( + origin: OriginFor, + create_asset_xcm: u128, + transfer_asset_xcm: u128, + register_token: U256, + ) -> DispatchResult { + ensure_root(origin)?; + + // Basic validation of new costs. Particularly for token registration, we want to ensure + // its relatively expensive to discourage spamming. Like at least 100 USD. + ensure!( + create_asset_xcm > 0 && transfer_asset_xcm > 0 && register_token > meth(100), + Error::::InvalidTokenTransferFees + ); + + let command = Command::SetTokenTransferFees { + create_asset_xcm, + transfer_asset_xcm, + register_token, + }; + Self::send(PRIMARY_GOVERNANCE_CHANNEL, command, PaysFee::::No)?; + + Self::deposit_event(Event::::SetTokenTransferFees { + create_asset_xcm, + transfer_asset_xcm, + register_token, + }); + Ok(()) + } + } + + impl Pallet { + /// Send `command` to the Gateway on the Channel identified by `channel_id` + fn send(channel_id: ChannelId, command: Command, pays_fee: PaysFee) -> DispatchResult { + let message = Message { id: None, channel_id, command }; + let (ticket, fee) = + T::OutboundQueue::validate(&message).map_err(|err| Error::::Send(err))?; + + let payment = match pays_fee { + PaysFee::Yes(account) => Some((account, fee.total())), + PaysFee::Partial(account) => Some((account, fee.local)), + PaysFee::No => None, + }; + + if let Some((payer, fee)) = payment { + T::Token::transfer( + &payer, + &T::TreasuryAccount::get(), + fee, + Preservation::Preserve, + )?; + } + + T::OutboundQueue::deliver(ticket).map_err(|err| Error::::Send(err))?; + Ok(()) + } + + /// Issue a `Command::TransferNativeFromAgent` command. The command will be sent on the + /// channel `channel_id` + pub fn do_transfer_native_from_agent( + agent_id: H256, + channel_id: ChannelId, + recipient: H160, + amount: u128, + pays_fee: PaysFee, + ) -> DispatchResult { + ensure!(Agents::::contains_key(agent_id), Error::::NoAgent); + + let command = Command::TransferNativeFromAgent { agent_id, recipient, amount }; + Self::send(channel_id, command, pays_fee)?; + + Self::deposit_event(Event::::TransferNativeFromAgent { + agent_id, + recipient, + amount, + }); + Ok(()) + } + + /// Initializes agents and channels. + pub fn initialize(para_id: ParaId, asset_hub_para_id: ParaId) -> Result<(), DispatchError> { + // Asset Hub + let asset_hub_location: MultiLocation = + ParentThen(X1(Parachain(asset_hub_para_id.into()))).into(); + let asset_hub_agent_id = agent_id_of::(&asset_hub_location)?; + let asset_hub_channel_id: ChannelId = asset_hub_para_id.into(); + Agents::::insert(asset_hub_agent_id, ()); + Channels::::insert( + asset_hub_channel_id, + Channel { agent_id: asset_hub_agent_id, para_id: asset_hub_para_id }, + ); + + // Governance channels + let bridge_hub_agent_id = agent_id_of::(&MultiLocation::here())?; + // Agent for BridgeHub + Agents::::insert(bridge_hub_agent_id, ()); + + // Primary governance channel + Channels::::insert( + PRIMARY_GOVERNANCE_CHANNEL, + Channel { agent_id: bridge_hub_agent_id, para_id }, + ); + + // Secondary governance channel + Channels::::insert( + SECONDARY_GOVERNANCE_CHANNEL, + Channel { agent_id: bridge_hub_agent_id, para_id }, + ); + + Ok(()) + } + + /// Checks if the pallet has been initialized. + pub(crate) fn is_initialized() -> bool { + let primary_exists = Channels::::contains_key(PRIMARY_GOVERNANCE_CHANNEL); + let secondary_exists = Channels::::contains_key(SECONDARY_GOVERNANCE_CHANNEL); + primary_exists && secondary_exists + } + } + + impl StaticLookup for Pallet { + type Source = ChannelId; + type Target = Channel; + fn lookup(channel_id: Self::Source) -> Option { + Channels::::get(channel_id) + } + } + + impl Contains for Pallet { + fn contains(channel_id: &ChannelId) -> bool { + Channels::::get(channel_id).is_some() + } + } + + impl Get> for Pallet { + fn get() -> PricingParametersOf { + PricingParameters::::get() + } + } +} diff --git a/bridges/snowbridge/parachain/pallets/system/src/migration.rs b/bridges/snowbridge/parachain/pallets/system/src/migration.rs new file mode 100644 index 000000000000..ee94fc091bd1 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/system/src/migration.rs @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! Governance API for controlling the Ethereum side of the bridge +use super::*; +use frame_support::traits::OnRuntimeUpgrade; +use log; + +#[cfg(feature = "try-runtime")] +use sp_runtime::TryRuntimeError; + +pub mod v0 { + use frame_support::{pallet_prelude::*, weights::Weight}; + + use super::*; + + const LOG_TARGET: &str = "ethereum_system::migration"; + + pub struct InitializeOnUpgrade( + sp_std::marker::PhantomData<(T, BridgeHubParaId, AssetHubParaId)>, + ); + impl OnRuntimeUpgrade + for InitializeOnUpgrade + where + T: Config, + BridgeHubParaId: Get, + AssetHubParaId: Get, + { + fn on_runtime_upgrade() -> Weight { + if !Pallet::::is_initialized() { + Pallet::::initialize( + BridgeHubParaId::get().into(), + AssetHubParaId::get().into(), + ) + .expect("infallible; qed"); + log::info!( + target: LOG_TARGET, + "Ethereum system initialized." + ); + T::DbWeight::get().reads_writes(2, 5) + } else { + log::info!( + target: LOG_TARGET, + "Ethereum system already initialized. Skipping." + ); + T::DbWeight::get().reads(2) + } + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, TryRuntimeError> { + if !Pallet::::is_initialized() { + log::info!( + target: LOG_TARGET, + "Agents and channels not initialized. Initialization will run." + ); + } else { + log::info!( + target: LOG_TARGET, + "Agents and channels are initialized. Initialization will not run." + ); + } + Ok(vec![]) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_: Vec) -> Result<(), TryRuntimeError> { + frame_support::ensure!( + Pallet::::is_initialized(), + "Agents and channels were not initialized." + ); + Ok(()) + } + } +} diff --git a/bridges/snowbridge/parachain/pallets/system/src/mock.rs b/bridges/snowbridge/parachain/pallets/system/src/mock.rs new file mode 100644 index 000000000000..7a4f61189305 --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/system/src/mock.rs @@ -0,0 +1,270 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use crate as snowbridge_system; +use frame_support::{ + parameter_types, + traits::{tokens::fungible::Mutate, ConstU128, ConstU16, ConstU64, ConstU8}, + weights::IdentityFee, + PalletId, +}; +use sp_core::H256; +use xcm_executor::traits::ConvertLocation; + +use snowbridge_core::{ + gwei, meth, outbound::ConstantGasMeter, sibling_sovereign_account, AgentId, AllowSiblingsOnly, + ParaId, PricingParameters, Rewards, +}; +use sp_runtime::{ + traits::{AccountIdConversion, BlakeTwo256, IdentityLookup, Keccak256}, + AccountId32, BuildStorage, FixedU128, +}; +use xcm::prelude::*; + +#[cfg(feature = "runtime-benchmarks")] +use crate::BenchmarkHelper; + +type Block = frame_system::mocking::MockBlock; +type Balance = u128; + +pub type AccountId = AccountId32; + +// A stripped-down version of pallet-xcm that only inserts an XCM origin into the runtime +#[allow(dead_code)] +#[frame_support::pallet] +mod pallet_xcm_origin { + use frame_support::{ + pallet_prelude::*, + traits::{Contains, OriginTrait}, + }; + use xcm::latest::prelude::*; + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config { + type RuntimeOrigin: From + From<::RuntimeOrigin>; + } + + // Insert this custom Origin into the aggregate RuntimeOrigin + #[pallet::origin] + #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)] + pub struct Origin(pub MultiLocation); + + impl From for Origin { + fn from(location: MultiLocation) -> Origin { + Origin(location) + } + } + + /// `EnsureOrigin` implementation succeeding with a `MultiLocation` value to recognize and + /// filter the contained location + pub struct EnsureXcm(PhantomData); + impl, F: Contains> EnsureOrigin for EnsureXcm + where + O::PalletsOrigin: From + TryInto, + { + type Success = MultiLocation; + + fn try_origin(outer: O) -> Result { + outer.try_with_caller(|caller| { + caller.try_into().and_then(|o| match o { + Origin(location) if F::contains(&location) => Ok(location), + o => Err(o.into()), + }) + }) + } + + #[cfg(feature = "runtime-benchmarks")] + fn try_successful_origin() -> Result { + Ok(O::from(Origin(MultiLocation { parents: 1, interior: X1(Parachain(2000)) }))) + } + } +} + +// Configure a mock runtime to test the pallet. +frame_support::construct_runtime!( + pub enum Test + { + System: frame_system, + Balances: pallet_balances::{Pallet, Call, Storage, Config, Event}, + XcmOrigin: pallet_xcm_origin::{Pallet, Origin}, + OutboundQueue: snowbridge_outbound_queue::{Pallet, Call, Storage, Event}, + EthereumSystem: snowbridge_system, + MessageQueue: pallet_message_queue::{Pallet, Call, Storage, Event} + } +); + +impl frame_system::Config for Test { + type BaseCallFilter = frame_support::traits::Everything; + type BlockWeights = (); + type BlockLength = (); + type DbWeight = (); + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type RuntimeTask = RuntimeTask; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = AccountId; + type Lookup = IdentityLookup; + type RuntimeEvent = RuntimeEvent; + type BlockHashCount = ConstU64<250>; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = pallet_balances::AccountData; + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = ConstU16<42>; + type OnSetCode = (); + type MaxConsumers = frame_support::traits::ConstU32<16>; + type Nonce = u64; + type Block = Block; +} + +impl pallet_balances::Config for Test { + type MaxLocks = (); + type MaxReserves = (); + type ReserveIdentifier = [u8; 8]; + type Balance = Balance; + type RuntimeEvent = RuntimeEvent; + type DustRemoval = (); + type ExistentialDeposit = ConstU128<1>; + type AccountStore = System; + type WeightInfo = (); + type FreezeIdentifier = (); + type MaxFreezes = (); + type RuntimeHoldReason = (); + type RuntimeFreezeReason = (); + type MaxHolds = (); +} + +impl pallet_xcm_origin::Config for Test { + type RuntimeOrigin = RuntimeOrigin; +} + +parameter_types! { + pub const HeapSize: u32 = 32 * 1024; + pub const MaxStale: u32 = 32; + pub static ServiceWeight: Option = Some(Weight::from_parts(100, 100)); +} + +impl pallet_message_queue::Config for Test { + type RuntimeEvent = RuntimeEvent; + type WeightInfo = (); + type MessageProcessor = OutboundQueue; + type Size = u32; + type QueueChangeHandler = (); + type HeapSize = HeapSize; + type MaxStale = MaxStale; + type ServiceWeight = ServiceWeight; + type QueuePausedQuery = (); +} + +parameter_types! { + pub const MaxMessagePayloadSize: u32 = 1024; + pub const MaxMessagesPerBlock: u32 = 20; + pub const OwnParaId: ParaId = ParaId::new(1013); +} + +impl snowbridge_outbound_queue::Config for Test { + type RuntimeEvent = RuntimeEvent; + type Hashing = Keccak256; + type MessageQueue = MessageQueue; + type Decimals = ConstU8<10>; + type MaxMessagePayloadSize = MaxMessagePayloadSize; + type MaxMessagesPerBlock = MaxMessagesPerBlock; + type GasMeter = ConstantGasMeter; + type Balance = u128; + type PricingParameters = EthereumSystem; + type Channels = EthereumSystem; + type WeightToFee = IdentityFee; + type WeightInfo = (); +} + +parameter_types! { + pub const SS58Prefix: u8 = 42; + pub const AnyNetwork: Option = None; + pub const RelayNetwork: Option = Some(NetworkId::Kusama); + pub const RelayLocation: MultiLocation = MultiLocation::parent(); + pub UniversalLocation: InteriorMultiLocation = + X2(GlobalConsensus(RelayNetwork::get().unwrap()), Parachain(1013)); +} + +pub const DOT: u128 = 10_000_000_000; + +parameter_types! { + pub TreasuryAccount: AccountId = PalletId(*b"py/trsry").into_account_truncating(); + pub Fee: u64 = 1000; + pub const RococoNetwork: NetworkId = NetworkId::Rococo; + pub const InitialFunding: u128 = 1_000_000_000_000; + pub AssetHubParaId: ParaId = ParaId::new(1000); + pub TestParaId: u32 = 2000; + pub Parameters: PricingParameters = PricingParameters { + exchange_rate: FixedU128::from_rational(1, 400), + fee_per_gas: gwei(20), + rewards: Rewards { local: DOT, remote: meth(1) } + }; + pub const InboundDeliveryCost: u128 = 1_000_000_000; + +} + +#[cfg(feature = "runtime-benchmarks")] +impl BenchmarkHelper for () { + fn make_xcm_origin(location: MultiLocation) -> RuntimeOrigin { + RuntimeOrigin::from(pallet_xcm_origin::Origin(location)) + } +} + +impl crate::Config for Test { + type RuntimeEvent = RuntimeEvent; + type OutboundQueue = OutboundQueue; + type SiblingOrigin = pallet_xcm_origin::EnsureXcm; + type AgentIdOf = snowbridge_core::AgentIdOf; + type TreasuryAccount = TreasuryAccount; + type Token = Balances; + type DefaultPricingParameters = Parameters; + type WeightInfo = (); + type InboundDeliveryCost = InboundDeliveryCost; + #[cfg(feature = "runtime-benchmarks")] + type Helper = (); +} + +// Build genesis storage according to the mock runtime. +pub fn new_test_ext(genesis_build: bool) -> sp_io::TestExternalities { + let mut storage = frame_system::GenesisConfig::::default().build_storage().unwrap(); + + if genesis_build { + crate::GenesisConfig:: { + para_id: OwnParaId::get(), + asset_hub_para_id: AssetHubParaId::get(), + _config: Default::default(), + } + .assimilate_storage(&mut storage) + .unwrap(); + } + + let mut ext: sp_io::TestExternalities = storage.into(); + let initial_amount = InitialFunding::get(); + let test_para_id = TestParaId::get(); + let sovereign_account = sibling_sovereign_account::(test_para_id.into()); + let treasury_account = TreasuryAccount::get(); + ext.execute_with(|| { + System::set_block_number(1); + Balances::mint_into(&AccountId32::from([0; 32]), initial_amount).unwrap(); + Balances::mint_into(&sovereign_account, initial_amount).unwrap(); + Balances::mint_into(&treasury_account, initial_amount).unwrap(); + }); + ext +} + +// Test helpers + +pub fn make_xcm_origin(location: MultiLocation) -> RuntimeOrigin { + pallet_xcm_origin::Origin(location).into() +} + +pub fn make_agent_id(location: MultiLocation) -> AgentId { + ::AgentIdOf::convert_location(&location) + .expect("convert location") +} diff --git a/bridges/snowbridge/parachain/pallets/system/src/tests.rs b/bridges/snowbridge/parachain/pallets/system/src/tests.rs new file mode 100644 index 000000000000..e07481c1e33e --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/system/src/tests.rs @@ -0,0 +1,664 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use crate::{mock::*, *}; +use frame_support::{assert_noop, assert_ok}; +use hex_literal::hex; +use snowbridge_core::{eth, sibling_sovereign_account_raw}; +use sp_core::H256; +use sp_runtime::{AccountId32, DispatchError::BadOrigin, TokenError}; + +#[test] +fn create_agent() { + new_test_ext(true).execute_with(|| { + let origin_para_id = 2000; + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(origin_para_id)) }; + let agent_id = make_agent_id(origin_location); + let sovereign_account = sibling_sovereign_account::(origin_para_id.into()); + + // fund sovereign account of origin + let _ = Balances::mint_into(&sovereign_account, 10000); + + assert!(!Agents::::contains_key(agent_id)); + + let origin = make_xcm_origin(origin_location); + assert_ok!(EthereumSystem::create_agent(origin)); + + assert!(Agents::::contains_key(agent_id)); + }); +} + +#[test] +fn test_agent_for_here() { + new_test_ext(true).execute_with(|| { + let origin_location = MultiLocation::here(); + let agent_id = make_agent_id(origin_location); + assert_eq!( + agent_id, + hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into(), + ) + }); +} + +#[test] +fn create_agent_fails_on_funds_unavailable() { + new_test_ext(true).execute_with(|| { + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(2000)) }; + let origin = make_xcm_origin(origin_location); + // Reset balance of sovereign_account to zero so to trigger the FundsUnavailable error + let sovereign_account = sibling_sovereign_account::(2000.into()); + Balances::set_balance(&sovereign_account, 0); + assert_noop!(EthereumSystem::create_agent(origin), TokenError::FundsUnavailable); + }); +} + +#[test] +fn create_agent_bad_origin() { + new_test_ext(true).execute_with(|| { + // relay chain location not allowed + assert_noop!( + EthereumSystem::create_agent(make_xcm_origin(MultiLocation { + parents: 1, + interior: Here, + })), + BadOrigin, + ); + + // local account location not allowed + assert_noop!( + EthereumSystem::create_agent(make_xcm_origin(MultiLocation { + parents: 0, + interior: X1(Junction::AccountId32 { network: None, id: [67u8; 32] }), + })), + BadOrigin, + ); + + // Signed origin not allowed + assert_noop!( + EthereumSystem::create_agent(RuntimeOrigin::signed([14; 32].into())), + BadOrigin + ); + + // None origin not allowed + assert_noop!(EthereumSystem::create_agent(RuntimeOrigin::none()), BadOrigin); + }); +} + +#[test] +fn upgrade_as_root() { + new_test_ext(true).execute_with(|| { + let origin = RuntimeOrigin::root(); + let address: H160 = Default::default(); + let code_hash: H256 = Default::default(); + + assert_ok!(EthereumSystem::upgrade(origin, address, code_hash, None)); + + System::assert_last_event(RuntimeEvent::EthereumSystem(crate::Event::Upgrade { + impl_address: address, + impl_code_hash: code_hash, + initializer_params_hash: None, + })); + }); +} + +#[test] +fn upgrade_as_signed_fails() { + new_test_ext(true).execute_with(|| { + let origin = RuntimeOrigin::signed(AccountId32::new([0; 32])); + let address: H160 = Default::default(); + let code_hash: H256 = Default::default(); + + assert_noop!(EthereumSystem::upgrade(origin, address, code_hash, None), BadOrigin); + }); +} + +#[test] +fn upgrade_with_params() { + new_test_ext(true).execute_with(|| { + let origin = RuntimeOrigin::root(); + let address: H160 = Default::default(); + let code_hash: H256 = Default::default(); + let initializer: Option = + Some(Initializer { params: [0; 256].into(), maximum_required_gas: 10000 }); + assert_ok!(EthereumSystem::upgrade(origin, address, code_hash, initializer)); + }); +} + +#[test] +fn set_operating_mode() { + new_test_ext(true).execute_with(|| { + let origin = RuntimeOrigin::root(); + let mode = OperatingMode::RejectingOutboundMessages; + + assert_ok!(EthereumSystem::set_operating_mode(origin, mode)); + + System::assert_last_event(RuntimeEvent::EthereumSystem(crate::Event::SetOperatingMode { + mode, + })); + }); +} + +#[test] +fn set_operating_mode_as_signed_fails() { + new_test_ext(true).execute_with(|| { + let origin = RuntimeOrigin::signed([14; 32].into()); + let mode = OperatingMode::RejectingOutboundMessages; + + assert_noop!(EthereumSystem::set_operating_mode(origin, mode), BadOrigin); + }); +} + +#[test] +fn set_pricing_parameters() { + new_test_ext(true).execute_with(|| { + let origin = RuntimeOrigin::root(); + let mut params = Parameters::get(); + params.rewards.local = 7; + + assert_ok!(EthereumSystem::set_pricing_parameters(origin, params)); + + assert_eq!(PricingParameters::::get().rewards.local, 7); + }); +} + +#[test] +fn set_pricing_parameters_as_signed_fails() { + new_test_ext(true).execute_with(|| { + let origin = RuntimeOrigin::signed([14; 32].into()); + let params = Parameters::get(); + + assert_noop!(EthereumSystem::set_pricing_parameters(origin, params), BadOrigin); + }); +} + +#[test] +fn set_pricing_parameters_invalid() { + new_test_ext(true).execute_with(|| { + let origin = RuntimeOrigin::root(); + let mut params = Parameters::get(); + params.rewards.local = 0; + + assert_noop!( + EthereumSystem::set_pricing_parameters(origin.clone(), params), + Error::::InvalidPricingParameters + ); + + let mut params = Parameters::get(); + params.exchange_rate = 0u128.into(); + assert_noop!( + EthereumSystem::set_pricing_parameters(origin.clone(), params), + Error::::InvalidPricingParameters + ); + params = Parameters::get(); + params.fee_per_gas = sp_core::U256::zero(); + assert_noop!( + EthereumSystem::set_pricing_parameters(origin.clone(), params), + Error::::InvalidPricingParameters + ); + params = Parameters::get(); + params.rewards.local = 0; + assert_noop!( + EthereumSystem::set_pricing_parameters(origin.clone(), params), + Error::::InvalidPricingParameters + ); + params = Parameters::get(); + params.rewards.remote = sp_core::U256::zero(); + assert_noop!( + EthereumSystem::set_pricing_parameters(origin, params), + Error::::InvalidPricingParameters + ); + }); +} + +#[test] +fn set_token_transfer_fees() { + new_test_ext(true).execute_with(|| { + let origin = RuntimeOrigin::root(); + + assert_ok!(EthereumSystem::set_token_transfer_fees(origin, 1, 1, eth(1))); + }); +} + +#[test] +fn set_token_transfer_fees_root_only() { + new_test_ext(true).execute_with(|| { + let origin = RuntimeOrigin::signed([14; 32].into()); + + assert_noop!(EthereumSystem::set_token_transfer_fees(origin, 1, 1, 1.into()), BadOrigin); + }); +} + +#[test] +fn set_token_transfer_fees_invalid() { + new_test_ext(true).execute_with(|| { + let origin = RuntimeOrigin::root(); + + assert_noop!( + EthereumSystem::set_token_transfer_fees(origin, 0, 0, 0.into()), + Error::::InvalidTokenTransferFees + ); + }); +} + +#[test] +fn create_channel() { + new_test_ext(true).execute_with(|| { + let origin_para_id = 2000; + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(origin_para_id)) }; + let sovereign_account = sibling_sovereign_account::(origin_para_id.into()); + let origin = make_xcm_origin(origin_location); + + // fund sovereign account of origin + let _ = Balances::mint_into(&sovereign_account, 10000); + + assert_ok!(EthereumSystem::create_agent(origin.clone())); + assert_ok!(EthereumSystem::create_channel(origin, OperatingMode::Normal)); + }); +} + +#[test] +fn create_channel_fail_already_exists() { + new_test_ext(true).execute_with(|| { + let origin_para_id = 2000; + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(origin_para_id)) }; + let sovereign_account = sibling_sovereign_account::(origin_para_id.into()); + let origin = make_xcm_origin(origin_location); + + // fund sovereign account of origin + let _ = Balances::mint_into(&sovereign_account, 10000); + + assert_ok!(EthereumSystem::create_agent(origin.clone())); + assert_ok!(EthereumSystem::create_channel(origin.clone(), OperatingMode::Normal)); + + assert_noop!( + EthereumSystem::create_channel(origin, OperatingMode::Normal), + Error::::ChannelAlreadyCreated + ); + }); +} + +#[test] +fn create_channel_bad_origin() { + new_test_ext(true).execute_with(|| { + // relay chain location not allowed + assert_noop!( + EthereumSystem::create_channel( + make_xcm_origin(MultiLocation { parents: 1, interior: Here }), + OperatingMode::Normal, + ), + BadOrigin, + ); + + // child of sibling location not allowed + assert_noop!( + EthereumSystem::create_channel( + make_xcm_origin(MultiLocation { + parents: 1, + interior: X2( + Parachain(2000), + Junction::AccountId32 { network: None, id: [67u8; 32] } + ), + }), + OperatingMode::Normal, + ), + BadOrigin, + ); + + // local account location not allowed + assert_noop!( + EthereumSystem::create_channel( + make_xcm_origin(MultiLocation { + parents: 0, + interior: X1(Junction::AccountId32 { network: None, id: [67u8; 32] }), + }), + OperatingMode::Normal, + ), + BadOrigin, + ); + + // Signed origin not allowed + assert_noop!( + EthereumSystem::create_channel( + RuntimeOrigin::signed([14; 32].into()), + OperatingMode::Normal, + ), + BadOrigin + ); + + // None origin not allowed + assert_noop!(EthereumSystem::create_agent(RuntimeOrigin::none()), BadOrigin); + }); +} + +#[test] +fn update_channel() { + new_test_ext(true).execute_with(|| { + let origin_para_id = 2000; + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(origin_para_id)) }; + let sovereign_account = sibling_sovereign_account::(origin_para_id.into()); + let origin = make_xcm_origin(origin_location); + + // First create the channel + let _ = Balances::mint_into(&sovereign_account, 10000); + assert_ok!(EthereumSystem::create_agent(origin.clone())); + assert_ok!(EthereumSystem::create_channel(origin.clone(), OperatingMode::Normal)); + + // Now try to update it + assert_ok!(EthereumSystem::update_channel(origin, OperatingMode::Normal)); + + System::assert_last_event(RuntimeEvent::EthereumSystem(crate::Event::UpdateChannel { + channel_id: ParaId::from(2000).into(), + mode: OperatingMode::Normal, + })); + }); +} + +#[test] +fn update_channel_bad_origin() { + new_test_ext(true).execute_with(|| { + let mode = OperatingMode::Normal; + + // relay chain location not allowed + assert_noop!( + EthereumSystem::update_channel( + make_xcm_origin(MultiLocation { parents: 1, interior: Here }), + mode, + ), + BadOrigin, + ); + + // child of sibling location not allowed + assert_noop!( + EthereumSystem::update_channel( + make_xcm_origin(MultiLocation { + parents: 1, + interior: X2( + Parachain(2000), + Junction::AccountId32 { network: None, id: [67u8; 32] } + ), + }), + mode, + ), + BadOrigin, + ); + + // local account location not allowed + assert_noop!( + EthereumSystem::update_channel( + make_xcm_origin(MultiLocation { + parents: 0, + interior: X1(Junction::AccountId32 { network: None, id: [67u8; 32] }), + }), + mode, + ), + BadOrigin, + ); + + // Signed origin not allowed + assert_noop!( + EthereumSystem::update_channel(RuntimeOrigin::signed([14; 32].into()), mode), + BadOrigin + ); + + // None origin not allowed + assert_noop!(EthereumSystem::update_channel(RuntimeOrigin::none(), mode), BadOrigin); + }); +} + +#[test] +fn update_channel_fails_not_exist() { + new_test_ext(true).execute_with(|| { + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(2000)) }; + let origin = make_xcm_origin(origin_location); + + // Now try to update it + assert_noop!( + EthereumSystem::update_channel(origin, OperatingMode::Normal), + Error::::NoChannel + ); + }); +} + +#[test] +fn force_update_channel() { + new_test_ext(true).execute_with(|| { + let origin_para_id = 2000; + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(origin_para_id)) }; + let sovereign_account = sibling_sovereign_account::(origin_para_id.into()); + let origin = make_xcm_origin(origin_location); + + let channel_id: ChannelId = ParaId::from(origin_para_id).into(); + + // First create the channel + let _ = Balances::mint_into(&sovereign_account, 10000); + assert_ok!(EthereumSystem::create_agent(origin.clone())); + assert_ok!(EthereumSystem::create_channel(origin.clone(), OperatingMode::Normal)); + + // Now try to force update it + let force_origin = RuntimeOrigin::root(); + assert_ok!(EthereumSystem::force_update_channel( + force_origin, + channel_id, + OperatingMode::Normal, + )); + + System::assert_last_event(RuntimeEvent::EthereumSystem(crate::Event::UpdateChannel { + channel_id: ParaId::from(2000).into(), + mode: OperatingMode::Normal, + })); + }); +} + +#[test] +fn force_update_channel_bad_origin() { + new_test_ext(true).execute_with(|| { + let mode = OperatingMode::Normal; + + // signed origin not allowed + assert_noop!( + EthereumSystem::force_update_channel( + RuntimeOrigin::signed([14; 32].into()), + ParaId::from(1000).into(), + mode, + ), + BadOrigin, + ); + }); +} + +#[test] +fn transfer_native_from_agent() { + new_test_ext(true).execute_with(|| { + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(2000)) }; + let origin = make_xcm_origin(origin_location); + let recipient: H160 = [27u8; 20].into(); + let amount = 103435; + + // First create the agent and channel + assert_ok!(EthereumSystem::create_agent(origin.clone())); + assert_ok!(EthereumSystem::create_channel(origin, OperatingMode::Normal)); + + let origin = make_xcm_origin(origin_location); + assert_ok!(EthereumSystem::transfer_native_from_agent(origin, recipient, amount),); + + System::assert_last_event(RuntimeEvent::EthereumSystem( + crate::Event::TransferNativeFromAgent { + agent_id: make_agent_id(origin_location), + recipient, + amount, + }, + )); + }); +} + +#[test] +fn force_transfer_native_from_agent() { + new_test_ext(true).execute_with(|| { + let origin = RuntimeOrigin::root(); + let location = MultiLocation { parents: 1, interior: X1(Parachain(2000)) }; + let versioned_location: Box = Box::new(location.into()); + let recipient: H160 = [27u8; 20].into(); + let amount = 103435; + + // First create the agent + Agents::::insert(make_agent_id(location), ()); + + assert_ok!(EthereumSystem::force_transfer_native_from_agent( + origin, + versioned_location, + recipient, + amount + ),); + + System::assert_last_event(RuntimeEvent::EthereumSystem( + crate::Event::TransferNativeFromAgent { + agent_id: make_agent_id(location), + recipient, + amount, + }, + )); + }); +} + +#[test] +fn force_transfer_native_from_agent_bad_origin() { + new_test_ext(true).execute_with(|| { + let recipient: H160 = [27u8; 20].into(); + let amount = 103435; + + // signed origin not allowed + assert_noop!( + EthereumSystem::force_transfer_native_from_agent( + RuntimeOrigin::signed([14; 32].into()), + Box::new( + MultiLocation { + parents: 1, + interior: X2( + Parachain(2000), + Junction::AccountId32 { network: None, id: [67u8; 32] } + ), + } + .into() + ), + recipient, + amount, + ), + BadOrigin, + ); + }); +} + +// NOTE: The following tests are not actually tests and are more about obtaining location +// conversions for devops purposes. They need to be removed here and incorporated into a command +// line utility. + +#[ignore] +#[test] +fn check_sibling_sovereign_account() { + new_test_ext(true).execute_with(|| { + let para_id = 1001; + let sovereign_account = sibling_sovereign_account::(para_id.into()); + let sovereign_account_raw = sibling_sovereign_account_raw(para_id.into()); + println!( + "Sovereign account for parachain {}: {:#?}", + para_id, + hex::encode(sovereign_account.clone()) + ); + assert_eq!(sovereign_account, sovereign_account_raw.into()); + }); +} + +#[test] +fn charge_fee_for_create_agent() { + new_test_ext(true).execute_with(|| { + let para_id: u32 = TestParaId::get(); + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(para_id)) }; + let origin = make_xcm_origin(origin_location); + let sovereign_account = sibling_sovereign_account::(para_id.into()); + let (_, agent_id) = ensure_sibling::(&origin_location).unwrap(); + + let initial_sovereign_balance = Balances::balance(&sovereign_account); + assert_ok!(EthereumSystem::create_agent(origin.clone())); + let fee_charged = initial_sovereign_balance - Balances::balance(&sovereign_account); + + assert_ok!(EthereumSystem::create_channel(origin, OperatingMode::Normal)); + + // assert sovereign_balance decreased by (fee.base_fee + fee.delivery_fee) + let message = Message { + id: None, + channel_id: ParaId::from(para_id).into(), + command: Command::CreateAgent { agent_id }, + }; + let (_, fee) = OutboundQueue::validate(&message).unwrap(); + assert_eq!(fee.local + fee.remote, fee_charged); + + // and treasury_balance increased + let treasury_balance = Balances::balance(&TreasuryAccount::get()); + assert!(treasury_balance > InitialFunding::get()); + + let final_sovereign_balance = Balances::balance(&sovereign_account); + // (sovereign_balance + treasury_balance) keeps the same + assert_eq!(final_sovereign_balance + treasury_balance, { InitialFunding::get() * 2 }); + }); +} + +#[test] +fn charge_fee_for_transfer_native_from_agent() { + new_test_ext(true).execute_with(|| { + let para_id: u32 = TestParaId::get(); + let origin_location = MultiLocation { parents: 1, interior: X1(Parachain(para_id)) }; + let recipient: H160 = [27u8; 20].into(); + let amount = 103435; + let origin = make_xcm_origin(origin_location); + let (_, agent_id) = ensure_sibling::(&origin_location).unwrap(); + + let sovereign_account = sibling_sovereign_account::(para_id.into()); + + // create_agent & create_channel first + assert_ok!(EthereumSystem::create_agent(origin.clone())); + assert_ok!(EthereumSystem::create_channel(origin.clone(), OperatingMode::Normal)); + + // assert sovereign_balance decreased by only the base_fee + let sovereign_balance_before = Balances::balance(&sovereign_account); + assert_ok!(EthereumSystem::transfer_native_from_agent(origin.clone(), recipient, amount)); + let message = Message { + id: None, + channel_id: ParaId::from(para_id).into(), + command: Command::TransferNativeFromAgent { agent_id, recipient, amount }, + }; + let (_, fee) = OutboundQueue::validate(&message).unwrap(); + let sovereign_balance_after = Balances::balance(&sovereign_account); + assert_eq!(sovereign_balance_after + fee.local, sovereign_balance_before); + }); +} + +#[test] +fn charge_fee_for_upgrade() { + new_test_ext(true).execute_with(|| { + let para_id: u32 = TestParaId::get(); + let origin = RuntimeOrigin::root(); + let address: H160 = Default::default(); + let code_hash: H256 = Default::default(); + let initializer: Option = + Some(Initializer { params: [0; 256].into(), maximum_required_gas: 10000 }); + assert_ok!(EthereumSystem::upgrade(origin, address, code_hash, initializer.clone())); + + // assert sovereign_balance does not change as we do not charge for sudo operations + let sovereign_account = sibling_sovereign_account::(para_id.into()); + let sovereign_balance = Balances::balance(&sovereign_account); + assert_eq!(sovereign_balance, InitialFunding::get()); + }); +} + +#[test] +fn genesis_build_initializes_correctly() { + new_test_ext(true).execute_with(|| { + assert!(EthereumSystem::is_initialized(), "Ethereum uninitialized."); + }); +} + +#[test] +fn no_genesis_build_is_uninitialized() { + new_test_ext(false).execute_with(|| { + assert!(!EthereumSystem::is_initialized(), "Ethereum initialized."); + }); +} diff --git a/bridges/snowbridge/parachain/pallets/system/src/weights.rs b/bridges/snowbridge/parachain/pallets/system/src/weights.rs new file mode 100644 index 000000000000..6e532a0d8a8c --- /dev/null +++ b/bridges/snowbridge/parachain/pallets/system/src/weights.rs @@ -0,0 +1,249 @@ + +//! Autogenerated weights for `snowbridge_system` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-10-09, STEPS: `2`, REPEAT: `1`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `crake.local`, CPU: `` +//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("bridge-hub-rococo-dev")`, DB CACHE: `1024` + +// Executed Command: +// target/release/polkadot-parachain +// benchmark +// pallet +// --chain +// bridge-hub-rococo-dev +// --pallet=snowbridge_system +// --extrinsic=* +// --execution=wasm +// --wasm-execution=compiled +// --template +// ../parachain/templates/module-weight-template.hbs +// --output +// ../parachain/pallets/control/src/weights.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(missing_docs)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use core::marker::PhantomData; + +/// Weight functions needed for `snowbridge_system`. +pub trait WeightInfo { + fn upgrade() -> Weight; + fn create_agent() -> Weight; + fn create_channel() -> Weight; + fn update_channel() -> Weight; + fn force_update_channel() -> Weight; + fn set_operating_mode() -> Weight; + fn transfer_native_from_agent() -> Weight; + fn force_transfer_native_from_agent() -> Weight; + fn set_token_transfer_fees() -> Weight; + fn set_pricing_parameters() -> Weight; +} + +// For backwards compatibility and tests. +impl WeightInfo for () { + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:1 w:1) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:1) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn upgrade() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3517` + // Minimum execution time: 44_000_000 picoseconds. + Weight::from_parts(44_000_000, 3517) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } + /// Storage: EthereumSystem Agents (r:1 w:1) + /// Proof: EthereumSystem Agents (max_values: None, max_size: Some(40), added: 2515, mode: MaxEncodedLen) + /// Storage: System Account (r:2 w:2) + /// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen) + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:1 w:1) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:1) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn create_agent() -> Weight { + // Proof Size summary in bytes: + // Measured: `187` + // Estimated: `6196` + // Minimum execution time: 85_000_000 picoseconds. + Weight::from_parts(85_000_000, 6196) + .saturating_add(RocksDbWeight::get().reads(7_u64)) + .saturating_add(RocksDbWeight::get().writes(6_u64)) + } + /// Storage: System Account (r:2 w:2) + /// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen) + /// Storage: EthereumSystem Agents (r:1 w:0) + /// Proof: EthereumSystem Agents (max_values: None, max_size: Some(40), added: 2515, mode: MaxEncodedLen) + /// Storage: EthereumSystem Channels (r:1 w:1) + /// Proof: EthereumSystem Channels (max_values: None, max_size: Some(12), added: 2487, mode: MaxEncodedLen) + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:1 w:1) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:1 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn create_channel() -> Weight { + // Proof Size summary in bytes: + // Measured: `602` + // Estimated: `69050` + // Minimum execution time: 83_000_000 picoseconds. + Weight::from_parts(83_000_000, 69050) + .saturating_add(RocksDbWeight::get().reads(8_u64)) + .saturating_add(RocksDbWeight::get().writes(5_u64)) + } + /// Storage: EthereumSystem Channels (r:1 w:0) + /// Proof: EthereumSystem Channels (max_values: None, max_size: Some(12), added: 2487, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:2 w:2) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:0) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn update_channel() -> Weight { + // Proof Size summary in bytes: + // Measured: `256` + // Estimated: `6044` + // Minimum execution time: 40_000_000 picoseconds. + Weight::from_parts(40_000_000, 6044) + .saturating_add(RocksDbWeight::get().reads(5_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } + /// Storage: EthereumSystem Channels (r:1 w:0) + /// Proof: EthereumSystem Channels (max_values: None, max_size: Some(12), added: 2487, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:2 w:2) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:0) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn force_update_channel() -> Weight { + // Proof Size summary in bytes: + // Measured: `256` + // Estimated: `6044` + // Minimum execution time: 41_000_000 picoseconds. + Weight::from_parts(41_000_000, 6044) + .saturating_add(RocksDbWeight::get().reads(5_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:1 w:1) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:1) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn set_operating_mode() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3517` + // Minimum execution time: 31_000_000 picoseconds. + Weight::from_parts(31_000_000, 3517) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } + /// Storage: EthereumSystem Agents (r:1 w:0) + /// Proof: EthereumSystem Agents (max_values: None, max_size: Some(40), added: 2515, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:2 w:2) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:0) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn transfer_native_from_agent() -> Weight { + // Proof Size summary in bytes: + // Measured: `252` + // Estimated: `6044` + // Minimum execution time: 45_000_000 picoseconds. + Weight::from_parts(45_000_000, 6044) + .saturating_add(RocksDbWeight::get().reads(5_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } + /// Storage: EthereumSystem Agents (r:1 w:0) + /// Proof: EthereumSystem Agents (max_values: None, max_size: Some(40), added: 2515, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:2 w:2) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:0) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn force_transfer_native_from_agent() -> Weight { + // Proof Size summary in bytes: + // Measured: `252` + // Estimated: `6044` + // Minimum execution time: 42_000_000 picoseconds. + Weight::from_parts(42_000_000, 6044) + .saturating_add(RocksDbWeight::get().reads(5_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } + + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:1 w:1) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:1) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn set_token_transfer_fees() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3517` + // Minimum execution time: 31_000_000 picoseconds. + Weight::from_parts(42_000_000, 3517) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } + + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:1 w:1) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:1) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn set_pricing_parameters() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3517` + // Minimum execution time: 31_000_000 picoseconds. + Weight::from_parts(42_000_000, 3517) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } +} diff --git a/bridges/snowbridge/parachain/primitives/beacon/Cargo.toml b/bridges/snowbridge/parachain/primitives/beacon/Cargo.toml new file mode 100644 index 000000000000..e81e0208ba14 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/beacon/Cargo.toml @@ -0,0 +1,52 @@ +[package] +name = "snowbridge-beacon-primitives" +description = "Snowbridge Beacon Primitives" +version = "0.0.1" +authors = ["Snowfork "] +edition = "2021" +license = "Apache-2.0" + +[dependencies] +serde = { version = "1.0.188", optional = true, features = ["derive"] } +hex = { version = "0.4", default-features = false } +codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false } +scale-info = { version = "2.9.0", default-features = false, features = ["derive"] } +rlp = { version = "0.5", default-features = false } + +frame-support = { path = "../../../../../substrate/frame/support", default-features = false } +frame-system = { path = "../../../../../substrate/frame/system", default-features = false } +sp-runtime = { path = "../../../../../substrate/primitives/runtime", default-features = false } +sp-core = { path = "../../../../../substrate/primitives/core", default-features = false } +sp-std = { path = "../../../../../substrate/primitives/std", default-features = false } +sp-io = { path = "../../../../../substrate/primitives/io", default-features = false } + +ssz_rs = { version = "0.9.0", default-features = false } +ssz_rs_derive = { version = "0.9.0", default-features = false } +byte-slice-cast = { version = "1.2.1", default-features = false } + +snowbridge-ethereum = { path = "../../primitives/ethereum", default-features = false } +static_assertions = { version = "1.1.0" } +milagro_bls = { git = "https://github.com/snowfork/milagro_bls", default-features = false, rev = "a6d66e4eb89015e352fb1c9f7b661ecdbb5b2176" } + +[dev-dependencies] +hex-literal = { version = "0.4.1" } + +[features] +default = ["std"] +std = [ + "byte-slice-cast/std", + "codec/std", + "frame-support/std", + "frame-system/std", + "hex/std", + "milagro_bls/std", + "rlp/std", + "scale-info/std", + "serde", + "snowbridge-ethereum/std", + "sp-core/std", + "sp-io/std", + "sp-runtime/std", + "sp-std/std", + "ssz_rs/std", +] diff --git a/bridges/snowbridge/parachain/primitives/beacon/src/bits.rs b/bridges/snowbridge/parachain/primitives/beacon/src/bits.rs new file mode 100644 index 000000000000..72b7135ee293 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/beacon/src/bits.rs @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use sp_std::{convert::TryInto, prelude::*}; +use ssz_rs::{Bitvector, Deserialize}; + +pub fn decompress_sync_committee_bits< + const SYNC_COMMITTEE_SIZE: usize, + const SYNC_COMMITTEE_BITS_SIZE: usize, +>( + input: [u8; SYNC_COMMITTEE_BITS_SIZE], +) -> [u8; SYNC_COMMITTEE_SIZE] { + Bitvector::<{ SYNC_COMMITTEE_SIZE }>::deserialize(&input) + .expect("checked statically; qed") + .iter() + .map(|bit| u8::from(bit == true)) + .collect::>() + .try_into() + .expect("checked statically; qed") +} diff --git a/bridges/snowbridge/parachain/primitives/beacon/src/bls.rs b/bridges/snowbridge/parachain/primitives/beacon/src/bls.rs new file mode 100644 index 000000000000..589b72e67348 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/beacon/src/bls.rs @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use crate::{PublicKey, Signature}; +use codec::{Decode, Encode}; +use frame_support::{ensure, PalletError}; +pub use milagro_bls::{ + AggregatePublicKey, AggregateSignature, PublicKey as PublicKeyPrepared, + Signature as SignaturePrepared, +}; +use scale_info::TypeInfo; +use sp_core::H256; +use sp_runtime::RuntimeDebug; +use sp_std::prelude::*; + +#[derive(Copy, Clone, Encode, Decode, Eq, PartialEq, TypeInfo, RuntimeDebug, PalletError)] +pub enum BlsError { + InvalidSignature, + InvalidPublicKey, + InvalidAggregatePublicKeys, + SignatureVerificationFailed, +} + +/// fast_aggregate_verify optimized with aggregate key subtracting absent ones. +pub fn fast_aggregate_verify( + aggregate_pubkey: &PublicKeyPrepared, + absent_pubkeys: &Vec, + message: H256, + signature: &Signature, +) -> Result<(), BlsError> { + let agg_sig = prepare_aggregate_signature(signature)?; + let agg_key = prepare_aggregate_pubkey_from_absent(aggregate_pubkey, absent_pubkeys)?; + fast_aggregate_verify_pre_aggregated(agg_sig, agg_key, message) +} + +/// Decompress one public key into a point in G1. +pub fn prepare_milagro_pubkey(pubkey: &PublicKey) -> Result { + PublicKeyPrepared::from_bytes_unchecked(&pubkey.0).map_err(|_| BlsError::InvalidPublicKey) +} + +/// Prepare for G1 public keys. +pub fn prepare_g1_pubkeys(pubkeys: &[PublicKey]) -> Result, BlsError> { + pubkeys + .iter() + // Deserialize one public key from compressed bytes + .map(prepare_milagro_pubkey) + .collect::, BlsError>>() +} + +/// Prepare for G1 AggregatePublicKey. +pub fn prepare_aggregate_pubkey( + pubkeys: &[PublicKeyPrepared], +) -> Result { + AggregatePublicKey::into_aggregate(pubkeys).map_err(|_| BlsError::InvalidPublicKey) +} + +/// Prepare for G1 AggregatePublicKey. +pub fn prepare_aggregate_pubkey_from_absent( + aggregate_key: &PublicKeyPrepared, + absent_pubkeys: &Vec, +) -> Result { + let mut aggregate_pubkey = AggregatePublicKey::from_public_key(aggregate_key); + if !absent_pubkeys.is_empty() { + let absent_aggregate_key = prepare_aggregate_pubkey(absent_pubkeys)?; + aggregate_pubkey.point.sub(&absent_aggregate_key.point); + } + Ok(AggregatePublicKey { point: aggregate_pubkey.point }) +} + +/// Prepare for G2 AggregateSignature, normally more expensive than G1 operation. +pub fn prepare_aggregate_signature(signature: &Signature) -> Result { + Ok(AggregateSignature::from_signature( + &SignaturePrepared::from_bytes(&signature.0).map_err(|_| BlsError::InvalidSignature)?, + )) +} + +/// fast_aggregate_verify_pre_aggregated which is the most expensive call in beacon light client. +pub fn fast_aggregate_verify_pre_aggregated( + agg_sig: AggregateSignature, + aggregate_key: AggregatePublicKey, + message: H256, +) -> Result<(), BlsError> { + ensure!( + agg_sig.fast_aggregate_verify_pre_aggregated(&message[..], &aggregate_key), + BlsError::SignatureVerificationFailed + ); + Ok(()) +} diff --git a/bridges/snowbridge/parachain/primitives/beacon/src/config.rs b/bridges/snowbridge/parachain/primitives/beacon/src/config.rs new file mode 100644 index 000000000000..aa5fda706f99 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/beacon/src/config.rs @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +pub const MAX_PROOF_SIZE: u32 = 20; + +pub const FEE_RECIPIENT_SIZE: usize = 20; +pub const EXTRA_DATA_SIZE: usize = 32; +pub const LOGS_BLOOM_SIZE: usize = 256; + +pub const PUBKEY_SIZE: usize = 48; +pub const SIGNATURE_SIZE: usize = 96; diff --git a/bridges/snowbridge/parachain/primitives/beacon/src/lib.rs b/bridges/snowbridge/parachain/primitives/beacon/src/lib.rs new file mode 100644 index 000000000000..3527e1ff0d19 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/beacon/src/lib.rs @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +#![cfg_attr(not(feature = "std"), no_std)] + +pub mod bits; +pub mod bls; +pub mod config; +pub mod merkle_proof; +pub mod receipt; +pub mod ssz; +pub mod types; +pub mod updates; + +#[cfg(feature = "std")] +mod serde_utils; + +pub use types::{ + BeaconHeader, CompactBeaconState, CompactExecutionHeader, ExecutionHeaderState, + ExecutionPayloadHeader, FinalizedHeaderState, Fork, ForkData, ForkVersion, ForkVersions, Mode, + PublicKey, Signature, SigningData, SyncAggregate, SyncCommittee, SyncCommitteePrepared, +}; +pub use updates::{CheckpointUpdate, ExecutionHeaderUpdate, NextSyncCommitteeUpdate, Update}; + +pub use bits::decompress_sync_committee_bits; +pub use bls::{ + fast_aggregate_verify, prepare_aggregate_pubkey, prepare_aggregate_pubkey_from_absent, + prepare_aggregate_signature, prepare_g1_pubkeys, AggregatePublicKey, AggregateSignature, + BlsError, PublicKeyPrepared, SignaturePrepared, +}; +pub use merkle_proof::verify_merkle_branch; +pub use receipt::verify_receipt_proof; diff --git a/bridges/snowbridge/parachain/primitives/beacon/src/merkle_proof.rs b/bridges/snowbridge/parachain/primitives/beacon/src/merkle_proof.rs new file mode 100644 index 000000000000..a6ee6e9452c3 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/beacon/src/merkle_proof.rs @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use sp_core::H256; +use sp_io::hashing::sha2_256; + +/// Specified by +/// with improvements from +pub fn verify_merkle_branch( + leaf: H256, + branch: &[H256], + index: usize, + depth: usize, + root: H256, +) -> bool { + // verify the proof length + if branch.len() != depth { + return false + } + // verify the computed merkle root + root == compute_merkle_root(leaf, branch, index) +} + +fn compute_merkle_root(leaf: H256, proof: &[H256], index: usize) -> H256 { + let mut value: [u8; 32] = leaf.into(); + for (i, node) in proof.iter().enumerate() { + let mut data = [0u8; 64]; + if generalized_index_bit(index, i) { + // right node + data[0..32].copy_from_slice(node.as_bytes()); + data[32..64].copy_from_slice(&value); + value = sha2_256(&data); + } else { + // left node + data[0..32].copy_from_slice(&value); + data[32..64].copy_from_slice(node.as_bytes()); + value = sha2_256(&data); + } + } + value.into() +} + +/// Spec: +fn generalized_index_bit(index: usize, position: usize) -> bool { + index & (1 << position) > 0 +} + +/// Spec: +pub const fn subtree_index(generalized_index: usize) -> usize { + generalized_index % (1 << generalized_index_length(generalized_index)) +} + +/// Spec: +pub const fn generalized_index_length(generalized_index: usize) -> usize { + match generalized_index.checked_ilog2() { + Some(v) => v as usize, + None => panic!("checked statically; qed"), + } +} diff --git a/bridges/snowbridge/parachain/primitives/beacon/src/receipt.rs b/bridges/snowbridge/parachain/primitives/beacon/src/receipt.rs new file mode 100644 index 000000000000..0588f3f73f71 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/beacon/src/receipt.rs @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use sp_core::H256; +use sp_io::hashing::keccak_256; +use sp_std::prelude::*; + +use snowbridge_ethereum::{mpt, Receipt}; + +pub fn verify_receipt_proof( + receipts_root: H256, + proof: &[Vec], +) -> Option> { + match apply_merkle_proof(proof) { + Some((root, data)) if root == receipts_root => Some(rlp::decode(&data)), + Some((_, _)) => None, + None => None, + } +} + +fn apply_merkle_proof(proof: &[Vec]) -> Option<(H256, Vec)> { + let mut iter = proof.iter().rev(); + let first_bytes = match iter.next() { + Some(b) => b, + None => return None, + }; + let item_to_prove: mpt::ShortNode = rlp::decode(first_bytes).ok()?; + + let final_hash: Option<[u8; 32]> = iter.try_fold(keccak_256(first_bytes), |acc, x| { + let node: Box = x.as_slice().try_into().ok()?; + if (*node).contains_hash(acc.into()) { + return Some(keccak_256(x)) + } + None + }); + + final_hash.map(|hash| (hash.into(), item_to_prove.value)) +} + +#[cfg(test)] +mod tests { + use super::*; + use hex_literal::hex; + + #[test] + fn test_verify_receipt_proof() { + let root: H256 = + hex!("fd5e397a84884641f53c496804f24b5276cbb8c5c9cfc2342246be8e3ce5ad02").into(); + + // Valid proof + let proof_receipt5 = vec!( + hex!("f90131a0b5ba404eb5a6a88e56579f4d37ef9813b5ad7f86f0823ff3b407ac5a6bb465eca0398ead2655e78e03c127ce22c5830e90f18b1601ec055f938336c084feb915a9a026d322c26e46c50942c1aabde50e36df5cde572aed650ce73ea3182c6e90a02ca00600a356135f4db1db0d9842264cdff2652676f881669e91e316c0b6dd783011a0837f1deb4075336da320388c1edfffc56c448a43f4a5ba031300d32a7b509fc5a01c3ac82fd65b4aba7f9afaf604d9c82ec7e2deb573a091ae235751bc5c0c288da05d454159d9071b0f68b6e0503d290f23ac7602c1db0c569dee4605d8f5298f09a00bbed10350ec954448df795f6fd46e3faefc800ede061b3840eedc6e2b07a74da0acb02d26a3650f2064c14a435fdf1f668d8655daf455ebdf671713a7c089b3898080808080808080").to_vec(), + hex!("f901f180a00046a08d4f0bdbdc6b31903086ce323182bce6725e7d9415f7ff91ee8f4820bda0e7cd26ad5f3d2771e4b5ab788e268a14a10209f94ee918eb6c829d21d3d11c1da00d4a56d9e9a6751874fd86c7e3cb1c6ad5a848da62751325f478978a00ea966ea064b81920c8f04a8a1e21f53a8280e739fbb7b00b2ab92493ca3f610b70e8ac85a0b1040ed4c55a73178b76abb16f946ce5bebd6b93ab873c83327df54047d12c27a0de6485e9ac58dc6e2b04b4bb38f562684f0b1a2ee586cc11079e7d9a9dc40b32a0d394f4d3532c3124a65fa36e69147e04fd20453a72ee9c50660f17e13ce9df48a066501003fc3e3478efd2803cd0eded6bbe9243ca01ba754d6327071ddbcbc649a0b2684e518f325fee39fc8ea81b68f3f5c785be00d087f3bed8857ae2ee8da26ea071060a5c52042e8d7ce21092f8ecf06053beb9a0b773a6f91a30c4220aa276b2a0fc22436632574ccf6043d0986dede27ea94c9ca9a3bb5ec03ce776a4ddef24a9a05a8a1d6698c4e7d8cc3a2506cb9b12ea9a079c9c7099bc919dc804033cc556e4a0170c468b0716fd36d161f0bf05875f15756a2976de92f9efe7716320509d79c9a0182f909a90cab169f3efb62387f9cccdd61440acc4deec42f68a4f7ca58075c7a055cf0e9202ac75689b76318f1171f3a44465eddc06aae0713bfb6b34fdd27b7980").to_vec(), + hex!("f904de20b904daf904d701830652f0b9010004200000000000000000000080020000000000010000000000010000000000000000000000000000000000000000000002000000080000000000000000200000000000000000000000000008000000220000000000400010000000000000000000000000000000000000000000000000000000000000040000000010000100000000000800000000004000000000000000000000000000080000004000000000020000000000020000000000000000000000000000000000000000000004000000000002000000000100000000000000000000000000001000000002000020000010200000000000010000000000000000000000000000000000000010000000f903ccf89b9421130f34829b4c343142047a28ce96ec07814b15f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa00000000000000000000000007d843005c7433c16b27ff939cb37471541561ebda0000000000000000000000000e9c1281aae66801fa35ec404d5f2aea393ff6988a000000000000000000000000000000000000000000000000000000005d09b7380f89b9421130f34829b4c343142047a28ce96ec07814b15f863a08c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925a00000000000000000000000007d843005c7433c16b27ff939cb37471541561ebda00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da0ffffffffffffffffffffffffffffffffffffffffffffffffffffffcc840c6920f89b94c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000e9c1281aae66801fa35ec404d5f2aea393ff6988a00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da000000000000000000000000000000000000000000000000003e973b5a5d1078ef87994e9c1281aae66801fa35ec404d5f2aea393ff6988e1a01c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1b840000000000000000000000000000000000000000000000000000001f1420ad1d40000000000000000000000000000000000000000000000014ad400879d159a38f8fc94e9c1281aae66801fa35ec404d5f2aea393ff6988f863a0d78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822a00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488db88000000000000000000000000000000000000000000000000000000005d415f3320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e973b5a5d1078ef87a94c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2f842a07fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65a00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da000000000000000000000000000000000000000000000000003e973b5a5d1078e").to_vec(), + ); + assert!(verify_receipt_proof(root, &proof_receipt5).is_some()); + + // Various invalid proofs + let proof_empty: Vec> = vec![]; + let proof_missing_full_node = vec![proof_receipt5[0].clone(), proof_receipt5[2].clone()]; + let proof_missing_short_node1 = vec![proof_receipt5[0].clone(), proof_receipt5[1].clone()]; + let proof_missing_short_node2 = vec![proof_receipt5[0].clone()]; + let proof_invalid_encoding = vec![proof_receipt5[2][2..].to_vec()]; + let proof_no_full_node = vec![proof_receipt5[2].clone(), proof_receipt5[2].clone()]; + assert!(verify_receipt_proof(root, &proof_empty).is_none()); + assert!(verify_receipt_proof(root, &proof_missing_full_node).is_none()); + + assert_eq!( + verify_receipt_proof(root, &proof_missing_short_node1), + Some(Err(rlp::DecoderError::Custom("Unsupported receipt type"))) + ); + + assert_eq!( + verify_receipt_proof(root, &proof_missing_short_node2), + Some(Err(rlp::DecoderError::Custom("Unsupported receipt type"))) + ); + + assert!(verify_receipt_proof(root, &proof_invalid_encoding).is_none()); + assert!(verify_receipt_proof(root, &proof_no_full_node).is_none()); + } + + #[test] + fn test_verify_receipt_proof_with_intermediate_short_node() { + let root: H256 = + hex!("d128e3a57142d2bf15bc0cbcac7ad54f40750d571b5c3097e425882c10c9ba66").into(); + + let proof_receipt263 = vec![ + hex!("f90131a00d3cb8d3f57ac1c0e12918a2ebe0cafed8c273577b9dd73e7ed1079b403ef494a0678b9835b834f8a287c0dd33a8fca9146e456ca688555ed4ec1361a2180b778da0fe42da181a46677a043b3d9d4b8bb05a6a17b7b5c010c17e7c1d31cfb7c4f911a0c89f0e2c53241cdb578e1f2b4caf6ba36e00500bdc57fecd66b84a6a58394c19a086c3c1fae5a0575940b5d38e111c469d07883106c26856f3ef608469a2081f13a06c5992ff00aab6226a70a032fd2f571ba22f797321f45e2daa73020d638d21b0a050861e9503ef68728f6c90a44f7fe1bceb2a9bdab6957bbe7136166bd849561ea006aa6eaca8a07e57176e9aa41e6a09edfb7678d1a112404e0ec779d7e567e82ea0bb0b430d303ba21b0af11c487b8a218bd75db54c98940b3f11bad8ff47cad3ef8080808080808080").to_vec(), + hex!("f871a0246de222036ee6a03329b0105da0a6b3f916fc95a9ed5a403a581a0c4d74242ca0ac108a49a88b57a05ac34a108b39f1e45f6f167f2b9fbc8d52fb58e2e5a6af1ea0fcfe07ac2ccd3c28b6eab68d1bce112f6f6dbd9023e4ec3c05b96615aa803d798080808080808080808080808080").to_vec(), + hex!("e4820001a04fff54398cad4d05ea6abfd8b0f3b4fe14c04d7ff5f5211c5b927d9cf72ac1d8").to_vec(), + hex!("f851a096d010643ca2d47412ca66898286b5f2412963b9ec051b33e570d575914c9c5ca028cd24c652989542fe89479ec6388eac4592432242af5ba97563b3ac7c71c019808080808080808080808080808080").to_vec(), + hex!("f90211a0bb35a84c5b1dcb78ec9d32614912c696e62df77bebf9ab326ee55b5d3acdde46a01084b30dac8df0accfcd0fd6330b7f6fc72a4651246d0694be9162151686a620a03eed50afdce7909d784c6157c445a444c806b5f23d31f3b63786f600c84a95b2a0af5232f1df6c6d41879804d081abe867002abe26ba3e5f8e0254a83a54769831a0607915fb13dd5da594256389a45007a67a7f7a86e95d38d8462792b6c98a722ea00e1260fda1730f2738c650ce2bfba83857bc10f8fb119ebc4fb39acba24e6fbaa0d11de17e417327457812675ca3b84ae8e1b64827abfe01420953697c8313d5b1a05fcaf2f7a88f76336a0c32ffc78acb87ae2005454bd25d658035331be3173b46a03f94f4952ab9e650f83cfd0e7f367b1bcc493aacf39a06f16c4a2e1b5605da48a0bdb4ec79785ca8ae22d60f1bbd42d707b4d7ec4aff231a3ebab755e315b35053a043a67c3f2bcef37c8f47a673adcb7061007a553696d1092408601c11b2e6846aa0c519d5af48cae87c7f4538845417c9735813bee892a6fe2dda79f5c414e8576aa0f7058256e09589501d7c231d739e61c84a850e139690989d24fda6058b432e98a081a52faab520978cb19ce14400dba0cd5bcdc4e5a3c0740678aa8f97ee0e5c56a0bcecc61cadeae52518e3b68a48af4b11603dfd9d99d99d7985efa6d2de44f904a02cba4accfc6f39bc5adb6d4440eb6358b4a5103ef93298e4e694f1f940f8b48280").to_vec(), + hex!("f901ae20b901aaf901a70183bb444eb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000001000000000000000000000000000100000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000010000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000080000000000000000000000000000000000000000000000002000000000000000000081000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000f89df89b94dac17f958d2ee523a2206206994597c13d831ec7f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa00000000000000000000000002e514404ff6823f1b46a8318a709251db414e5e1a000000000000000000000000055021c55847c00d764357a352e5803237d328954a0000000000000000000000000000000000000000000000000000000000201c370").to_vec(), + ]; + assert!(verify_receipt_proof(root, &proof_receipt263).is_some()); + } +} diff --git a/bridges/snowbridge/parachain/primitives/beacon/src/serde_utils.rs b/bridges/snowbridge/parachain/primitives/beacon/src/serde_utils.rs new file mode 100644 index 000000000000..07f5cbe724ed --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/beacon/src/serde_utils.rs @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use sp_core::U256; + +use core::fmt::Formatter; +use serde::{Deserialize, Deserializer}; + +// helper to deserialize arbitrary arrays like [T; N] +pub mod arrays { + use std::{convert::TryInto, marker::PhantomData}; + + use serde::{ + de::{SeqAccess, Visitor}, + ser::SerializeTuple, + Deserialize, Deserializer, Serialize, Serializer, + }; + + pub fn serialize( + data: &[T; N], + ser: S, + ) -> Result { + let mut s = ser.serialize_tuple(N)?; + for item in data { + s.serialize_element(item)?; + } + s.end() + } + + struct ArrayVisitor(PhantomData); + + impl<'de, T, const N: usize> Visitor<'de> for ArrayVisitor + where + T: Deserialize<'de>, + { + type Value = [T; N]; + + fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + formatter.write_str(&format!("an array of length {}", N)) + } + + #[inline] + fn visit_seq(self, mut seq: A) -> Result + where + A: SeqAccess<'de>, + { + // can be optimized using MaybeUninit + let mut data = Vec::with_capacity(N); + for _ in 0..N { + match (seq.next_element())? { + Some(val) => data.push(val), + None => return Err(serde::de::Error::invalid_length(N, &self)), + } + } + match data.try_into() { + Ok(arr) => Ok(arr), + Err(_) => unreachable!(), + } + } + } + + pub fn deserialize<'de, D, T, const N: usize>(deserializer: D) -> Result<[T; N], D::Error> + where + D: Deserializer<'de>, + T: Deserialize<'de>, + { + deserializer.deserialize_tuple(N, ArrayVisitor::(PhantomData)) + } +} + +pub(crate) fn from_hex_to_bytes<'de, D>(deserializer: D) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + let s = String::deserialize(deserializer)?; + + let str_without_0x = match s.strip_prefix("0x") { + Some(val) => val, + None => &s, + }; + + let hex_bytes = match hex::decode(str_without_0x) { + Ok(bytes) => bytes, + Err(e) => return Err(serde::de::Error::custom(e.to_string())), + }; + + Ok(hex_bytes) +} + +pub(crate) fn from_int_to_u256<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + let number = u128::deserialize(deserializer)?; + + Ok(U256::from(number)) +} + +pub struct HexVisitor(); + +impl<'de, const LENGTH: usize> serde::de::Visitor<'de> for HexVisitor { + type Value = [u8; LENGTH]; + + fn expecting(&self, formatter: &mut Formatter) -> sp_std::fmt::Result { + formatter.write_str("a hex string with an '0x' prefix") + } + + fn visit_str(self, v: &str) -> Result + where + E: serde::de::Error, + { + let stripped = match v.strip_prefix("0x") { + Some(stripped) => stripped, + None => v, + }; + + let decoded = match hex::decode(stripped) { + Ok(decoded) => decoded, + Err(e) => return Err(serde::de::Error::custom(e.to_string())), + }; + if decoded.len() != LENGTH { + return Err(serde::de::Error::custom("publickey expected to be 48 characters")) + } + + let data: Self::Value = decoded + .try_into() + .map_err(|_e| serde::de::Error::custom("hex data has unexpected length"))?; + + Ok(data) + } +} diff --git a/bridges/snowbridge/parachain/primitives/beacon/src/ssz.rs b/bridges/snowbridge/parachain/primitives/beacon/src/ssz.rs new file mode 100644 index 000000000000..4f8b19ca8892 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/beacon/src/ssz.rs @@ -0,0 +1,194 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use crate::{ + config::{EXTRA_DATA_SIZE, FEE_RECIPIENT_SIZE, LOGS_BLOOM_SIZE, PUBKEY_SIZE, SIGNATURE_SIZE}, + types::{ + BeaconHeader, ExecutionPayloadHeader, ForkData, SigningData, SyncAggregate, SyncCommittee, + }, +}; +use byte_slice_cast::AsByteSlice; +use sp_core::H256; +use sp_std::{vec, vec::Vec}; +use ssz_rs::{ + prelude::{List, Vector}, + Bitvector, Deserialize, DeserializeError, SimpleSerialize, SimpleSerializeError, Sized, U256, +}; +use ssz_rs_derive::SimpleSerialize as SimpleSerializeDerive; + +#[derive(Default, SimpleSerializeDerive, Clone, Debug)] +pub struct SSZBeaconBlockHeader { + pub slot: u64, + pub proposer_index: u64, + pub parent_root: [u8; 32], + pub state_root: [u8; 32], + pub body_root: [u8; 32], +} + +impl From for SSZBeaconBlockHeader { + fn from(beacon_header: BeaconHeader) -> Self { + SSZBeaconBlockHeader { + slot: beacon_header.slot, + proposer_index: beacon_header.proposer_index, + parent_root: beacon_header.parent_root.to_fixed_bytes(), + state_root: beacon_header.state_root.to_fixed_bytes(), + body_root: beacon_header.body_root.to_fixed_bytes(), + } + } +} + +#[derive(Default, SimpleSerializeDerive, Clone)] +pub struct SSZSyncCommittee { + pub pubkeys: Vector, COMMITTEE_SIZE>, + pub aggregate_pubkey: Vector, +} + +impl From> + for SSZSyncCommittee +{ + fn from(sync_committee: SyncCommittee) -> Self { + let mut pubkeys_vec = Vec::new(); + + for pubkey in sync_committee.pubkeys.iter() { + // The only thing that can go wrong in the conversion from vec to Vector (ssz type) is + // that the Vector size is 0, or that the given data to create the Vector from does not + // match the expected size N. Because these sizes are statically checked (i.e. + // PublicKey's size is 48, and const PUBKEY_SIZE is 48, it is impossible for "try_from" + // to return an error condition. + let conv_pubkey = Vector::::try_from(pubkey.0.to_vec()) + .expect("checked statically; qed"); + + pubkeys_vec.push(conv_pubkey); + } + + let pubkeys = Vector::, { COMMITTEE_SIZE }>::try_from(pubkeys_vec) + .expect("checked statically; qed"); + + let aggregate_pubkey = + Vector::::try_from(sync_committee.aggregate_pubkey.0.to_vec()) + .expect("checked statically; qed"); + + SSZSyncCommittee { pubkeys, aggregate_pubkey } + } +} + +#[derive(Default, Debug, SimpleSerializeDerive, Clone)] +pub struct SSZSyncAggregate { + pub sync_committee_bits: Bitvector, + pub sync_committee_signature: Vector, +} + +impl + From> for SSZSyncAggregate +{ + fn from(sync_aggregate: SyncAggregate) -> Self { + SSZSyncAggregate { + sync_committee_bits: Bitvector::::deserialize( + &sync_aggregate.sync_committee_bits, + ) + .expect("checked statically; qed"), + sync_committee_signature: Vector::::try_from( + sync_aggregate.sync_committee_signature.0.to_vec(), + ) + .expect("checked statically; qed"), + } + } +} + +#[derive(Default, SimpleSerializeDerive, Clone)] +pub struct SSZForkData { + pub current_version: [u8; 4], + pub genesis_validators_root: [u8; 32], +} + +impl From for SSZForkData { + fn from(fork_data: ForkData) -> Self { + SSZForkData { + current_version: fork_data.current_version, + genesis_validators_root: fork_data.genesis_validators_root, + } + } +} + +#[derive(Default, SimpleSerializeDerive, Clone)] +pub struct SSZSigningData { + pub object_root: [u8; 32], + pub domain: [u8; 32], +} + +impl From for SSZSigningData { + fn from(signing_data: SigningData) -> Self { + SSZSigningData { + object_root: signing_data.object_root.into(), + domain: signing_data.domain.into(), + } + } +} + +#[derive(Default, SimpleSerializeDerive, Clone, Debug)] +pub struct SSZExecutionPayloadHeader { + pub parent_hash: [u8; 32], + pub fee_recipient: Vector, + pub state_root: [u8; 32], + pub receipts_root: [u8; 32], + pub logs_bloom: Vector, + pub prev_randao: [u8; 32], + pub block_number: u64, + pub gas_limit: u64, + pub gas_used: u64, + pub timestamp: u64, + pub extra_data: List, + pub base_fee_per_gas: U256, + pub block_hash: [u8; 32], + pub transactions_root: [u8; 32], + pub withdrawals_root: [u8; 32], +} + +impl TryFrom for SSZExecutionPayloadHeader { + type Error = SimpleSerializeError; + + fn try_from(payload: ExecutionPayloadHeader) -> Result { + Ok(SSZExecutionPayloadHeader { + parent_hash: payload.parent_hash.to_fixed_bytes(), + fee_recipient: Vector::::try_from( + payload.fee_recipient.to_fixed_bytes().to_vec(), + ) + .expect("checked statically; qed"), + state_root: payload.state_root.to_fixed_bytes(), + receipts_root: payload.receipts_root.to_fixed_bytes(), + // Logs bloom bytes size is not constrained, so here we do need to check the try_from + // error + logs_bloom: Vector::::try_from(payload.logs_bloom) + .map_err(|(_, err)| err)?, + prev_randao: payload.prev_randao.to_fixed_bytes(), + block_number: payload.block_number, + gas_limit: payload.gas_limit, + gas_used: payload.gas_used, + timestamp: payload.timestamp, + // Extra data bytes size is not constrained, so here we do need to check the try_from + // error + extra_data: List::::try_from(payload.extra_data) + .map_err(|(_, err)| err)?, + base_fee_per_gas: U256::from_bytes_le( + payload + .base_fee_per_gas + .as_byte_slice() + .try_into() + .expect("checked in prep; qed"), + ), + block_hash: payload.block_hash.to_fixed_bytes(), + transactions_root: payload.transactions_root.to_fixed_bytes(), + withdrawals_root: payload.withdrawals_root.to_fixed_bytes(), + }) + } +} + +pub fn hash_tree_root(mut object: T) -> Result { + match object.hash_tree_root() { + Ok(node) => { + let fixed_bytes: [u8; 32] = + node.as_ref().try_into().expect("Node is a newtype over [u8; 32]; qed"); + Ok(fixed_bytes.into()) + }, + Err(err) => Err(err.into()), + } +} diff --git a/bridges/snowbridge/parachain/primitives/beacon/src/types.rs b/bridges/snowbridge/parachain/primitives/beacon/src/types.rs new file mode 100644 index 000000000000..f893551d9d17 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/beacon/src/types.rs @@ -0,0 +1,512 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use codec::{Decode, Encode, MaxEncodedLen}; +use frame_support::{CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound}; +use scale_info::TypeInfo; +use sp_core::{H160, H256, U256}; +use sp_runtime::RuntimeDebug; +use sp_std::{boxed::Box, prelude::*}; + +use crate::config::{PUBKEY_SIZE, SIGNATURE_SIZE}; + +#[cfg(feature = "std")] +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +#[cfg(feature = "std")] +use crate::serde_utils::HexVisitor; + +use crate::ssz::{ + hash_tree_root, SSZBeaconBlockHeader, SSZExecutionPayloadHeader, SSZForkData, SSZSigningData, + SSZSyncAggregate, SSZSyncCommittee, +}; +use ssz_rs::SimpleSerializeError; + +pub use crate::bits::decompress_sync_committee_bits; + +use crate::bls::{prepare_g1_pubkeys, prepare_milagro_pubkey, BlsError}; +use milagro_bls::PublicKey as PublicKeyPrepared; + +pub type ValidatorIndex = u64; +pub type ForkVersion = [u8; 4]; + +#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] +pub struct ForkVersions { + pub genesis: Fork, + pub altair: Fork, + pub bellatrix: Fork, + pub capella: Fork, +} + +#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] +pub struct Fork { + pub version: [u8; 4], + pub epoch: u64, +} + +#[derive(Copy, Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] +pub struct PublicKey(pub [u8; PUBKEY_SIZE]); + +impl Default for PublicKey { + fn default() -> Self { + PublicKey([0u8; PUBKEY_SIZE]) + } +} + +impl From<[u8; PUBKEY_SIZE]> for PublicKey { + fn from(v: [u8; PUBKEY_SIZE]) -> Self { + Self(v) + } +} + +impl MaxEncodedLen for PublicKey { + fn max_encoded_len() -> usize { + PUBKEY_SIZE + } +} + +#[cfg(feature = "std")] +impl<'de> Deserialize<'de> for PublicKey { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + deserializer.deserialize_str(HexVisitor::()).map(|v| v.into()) + } +} + +#[cfg(feature = "std")] +impl Serialize for PublicKey { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_bytes(&self.0) + } +} + +#[derive(Copy, Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] +pub struct Signature(pub [u8; SIGNATURE_SIZE]); + +impl Default for Signature { + fn default() -> Self { + Signature([0u8; SIGNATURE_SIZE]) + } +} + +impl From<[u8; SIGNATURE_SIZE]> for Signature { + fn from(v: [u8; SIGNATURE_SIZE]) -> Self { + Self(v) + } +} + +#[cfg(feature = "std")] +impl<'de> Deserialize<'de> for Signature { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + deserializer.deserialize_str(HexVisitor::()).map(|v| v.into()) + } +} + +#[derive(Copy, Clone, Default, Encode, Decode, TypeInfo, MaxEncodedLen)] +pub struct ExecutionHeaderState { + pub beacon_block_root: H256, + pub beacon_slot: u64, + pub block_hash: H256, + pub block_number: u64, +} + +#[derive(Copy, Clone, Default, Encode, Decode, TypeInfo, MaxEncodedLen)] +pub struct FinalizedHeaderState { + pub beacon_block_root: H256, + pub beacon_slot: u64, +} + +#[derive(Clone, Default, Encode, Decode, PartialEq, RuntimeDebug)] +pub struct ForkData { + // 1 or 0 bit, indicates whether a sync committee participated in a vote + pub current_version: [u8; 4], + pub genesis_validators_root: [u8; 32], +} + +impl ForkData { + pub fn hash_tree_root(&self) -> Result { + hash_tree_root::(self.clone().into()) + } +} + +#[derive(Clone, Default, Encode, Decode, PartialEq, RuntimeDebug)] +pub struct SigningData { + pub object_root: H256, + pub domain: H256, +} + +impl SigningData { + pub fn hash_tree_root(&self) -> Result { + hash_tree_root::(self.clone().into()) + } +} + +/// Sync committee as it is stored in the runtime storage. +#[derive( + Encode, Decode, PartialEqNoBound, CloneNoBound, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen, +)] +#[cfg_attr( + feature = "std", + derive(Serialize, Deserialize), + serde(deny_unknown_fields, bound(serialize = ""), bound(deserialize = "")) +)] +#[codec(mel_bound())] +pub struct SyncCommittee { + #[cfg_attr(feature = "std", serde(with = "crate::serde_utils::arrays"))] + pub pubkeys: [PublicKey; COMMITTEE_SIZE], + pub aggregate_pubkey: PublicKey, +} + +impl Default for SyncCommittee { + fn default() -> Self { + SyncCommittee { + pubkeys: [Default::default(); COMMITTEE_SIZE], + aggregate_pubkey: Default::default(), + } + } +} + +impl SyncCommittee { + pub fn hash_tree_root(&self) -> Result { + hash_tree_root::>(self.clone().into()) + } +} + +/// Prepared G1 public key of sync committee as it is stored in the runtime storage. +#[derive(Clone, PartialEq, Eq, Encode, Decode, TypeInfo, MaxEncodedLen)] +pub struct SyncCommitteePrepared { + pub root: H256, + pub pubkeys: Box<[PublicKeyPrepared; COMMITTEE_SIZE]>, + pub aggregate_pubkey: PublicKeyPrepared, +} + +impl Default for SyncCommitteePrepared { + fn default() -> Self { + SyncCommitteePrepared { + root: H256::default(), + pubkeys: Box::new([PublicKeyPrepared::default(); COMMITTEE_SIZE]), + aggregate_pubkey: PublicKeyPrepared::default(), + } + } +} + +impl TryFrom<&SyncCommittee> + for SyncCommitteePrepared +{ + type Error = BlsError; + + fn try_from(sync_committee: &SyncCommittee) -> Result { + let g1_pubkeys = prepare_g1_pubkeys(&sync_committee.pubkeys)?; + let sync_committee_root = sync_committee.hash_tree_root().expect("checked statically; qed"); + + Ok(SyncCommitteePrepared:: { + pubkeys: g1_pubkeys.try_into().expect("checked statically; qed"), + aggregate_pubkey: prepare_milagro_pubkey(&sync_committee.aggregate_pubkey)?, + root: sync_committee_root, + }) + } +} + +/// Beacon block header as it is stored in the runtime storage. The block root is the +/// Merkleization of a BeaconHeader. +#[derive( + Copy, Clone, Default, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen, +)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct BeaconHeader { + // The slot for which this block is created. Must be greater than the slot of the block defined + // by parent root. + pub slot: u64, + // The index of the validator that proposed the block. + pub proposer_index: ValidatorIndex, + // The block root of the parent block, forming a block chain. + pub parent_root: H256, + // The hash root of the post state of running the state transition through this block. + pub state_root: H256, + // The hash root of the beacon block body + pub body_root: H256, +} + +impl BeaconHeader { + pub fn hash_tree_root(&self) -> Result { + hash_tree_root::((*self).into()) + } +} + +#[derive(Encode, Decode, CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo)] +#[cfg_attr( + feature = "std", + derive(Deserialize), + serde( + try_from = "IntermediateSyncAggregate", + deny_unknown_fields, + bound(serialize = ""), + bound(deserialize = "") + ) +)] +#[codec(mel_bound())] +pub struct SyncAggregate { + pub sync_committee_bits: [u8; COMMITTEE_BITS_SIZE], + pub sync_committee_signature: Signature, +} + +impl Default + for SyncAggregate +{ + fn default() -> Self { + SyncAggregate { + sync_committee_bits: [0; COMMITTEE_BITS_SIZE], + sync_committee_signature: Default::default(), + } + } +} + +impl + SyncAggregate +{ + pub fn hash_tree_root(&self) -> Result { + hash_tree_root::>(self.clone().into()) + } +} + +/// Serde deserialization helper for SyncAggregate +#[cfg(feature = "std")] +#[derive(Deserialize)] +struct IntermediateSyncAggregate { + #[cfg_attr(feature = "std", serde(deserialize_with = "crate::serde_utils::from_hex_to_bytes"))] + pub sync_committee_bits: Vec, + pub sync_committee_signature: Signature, +} + +#[cfg(feature = "std")] +impl + TryFrom for SyncAggregate +{ + type Error = String; + + fn try_from(other: IntermediateSyncAggregate) -> Result { + Ok(Self { + sync_committee_bits: other + .sync_committee_bits + .try_into() + .map_err(|_| "unexpected length".to_owned())?, + sync_committee_signature: other.sync_committee_signature, + }) + } +} + +/// ExecutionPayloadHeader +/// +#[derive( + Default, Encode, Decode, CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo, +)] +#[cfg_attr( + feature = "std", + derive(Deserialize), + serde(deny_unknown_fields, bound(serialize = ""), bound(deserialize = "")) +)] +#[codec(mel_bound())] +pub struct ExecutionPayloadHeader { + pub parent_hash: H256, + pub fee_recipient: H160, + pub state_root: H256, + pub receipts_root: H256, + #[cfg_attr(feature = "std", serde(deserialize_with = "crate::serde_utils::from_hex_to_bytes"))] + pub logs_bloom: Vec, + pub prev_randao: H256, + pub block_number: u64, + pub gas_limit: u64, + pub gas_used: u64, + pub timestamp: u64, + #[cfg_attr(feature = "std", serde(deserialize_with = "crate::serde_utils::from_hex_to_bytes"))] + pub extra_data: Vec, + #[cfg_attr(feature = "std", serde(deserialize_with = "crate::serde_utils::from_int_to_u256"))] + pub base_fee_per_gas: U256, + pub block_hash: H256, + pub transactions_root: H256, + pub withdrawals_root: H256, +} + +impl ExecutionPayloadHeader { + pub fn hash_tree_root(&self) -> Result { + hash_tree_root::(self.clone().try_into()?) + } +} + +#[derive( + Default, + Encode, + Decode, + CloneNoBound, + PartialEqNoBound, + RuntimeDebugNoBound, + TypeInfo, + MaxEncodedLen, +)] +pub struct CompactExecutionHeader { + pub parent_hash: H256, + #[codec(compact)] + pub block_number: u64, + pub state_root: H256, + pub receipts_root: H256, +} + +impl From for CompactExecutionHeader { + fn from(execution_payload: ExecutionPayloadHeader) -> Self { + Self { + parent_hash: execution_payload.parent_hash, + block_number: execution_payload.block_number, + state_root: execution_payload.state_root, + receipts_root: execution_payload.receipts_root, + } + } +} + +#[derive( + Default, + Encode, + Decode, + Copy, + Clone, + PartialEqNoBound, + RuntimeDebugNoBound, + TypeInfo, + MaxEncodedLen, +)] +pub struct CompactBeaconState { + #[codec(compact)] + pub slot: u64, + pub block_roots_root: H256, +} + +#[cfg(test)] +mod tests { + use super::*; + use hex_literal::hex; + + #[test] + pub fn test_hash_beacon_header1() { + let hash_root = BeaconHeader { + slot: 3, + proposer_index: 2, + parent_root: hex!("796ea53efb534eab7777809cc5ee2d84e7f25024b9d0c4d7e5bcaab657e4bdbd") + .into(), + state_root: hex!("ba3ff080912be5c9c158b2e962c1b39a91bc0615762ba6fa2ecacafa94e9ae0a") + .into(), + body_root: hex!("a18d7fcefbb74a177c959160e0ee89c23546482154e6831237710414465dcae5") + .into(), + } + .hash_tree_root(); + + assert!(hash_root.is_ok()); + assert_eq!( + hash_root.unwrap(), + hex!("7d42595818709e805dd2fa710a2d2c1f62576ef1ab7273941ac9130fb94b91f7").into() + ); + } + + #[test] + pub fn test_hash_beacon_header2() { + let hash_root = BeaconHeader { + slot: 3476424, + proposer_index: 314905, + parent_root: hex!("c069d7b49cffd2b815b0fb8007eb9ca91202ea548df6f3db60000f29b2489f28") + .into(), + state_root: hex!("444d293e4533501ee508ad608783a7d677c3c566f001313e8a02ce08adf590a3") + .into(), + body_root: hex!("6508a0241047f21ba88f05d05b15534156ab6a6f8e029a9a5423da429834e04a") + .into(), + } + .hash_tree_root(); + + assert!(hash_root.is_ok()); + assert_eq!( + hash_root.unwrap(), + hex!("0aa41166ff01e58e111ac8c42309a738ab453cf8d7285ed8477b1c484acb123e").into() + ); + } + + #[test] + pub fn test_hash_fork_data() { + let hash_root = ForkData { + current_version: hex!("83f38a34"), + genesis_validators_root: hex!( + "22370bbbb358800f5711a10ea9845284272d8493bed0348cab87b8ab1e127930" + ), + } + .hash_tree_root(); + + assert!(hash_root.is_ok()); + assert_eq!( + hash_root.unwrap(), + hex!("57c12c4246bc7152b174b51920506bf943eff9c7ffa50b9533708e9cc1f680fc").into() + ); + } + + #[test] + pub fn test_hash_signing_data() { + let hash_root = SigningData { + object_root: hex!("63654cbe64fc07853f1198c165dd3d49c54fc53bc417989bbcc66da15f850c54") + .into(), + domain: hex!("037da907d1c3a03c0091b2254e1480d9b1783476e228ab29adaaa8f133e08f7a").into(), + } + .hash_tree_root(); + + assert!(hash_root.is_ok()); + assert_eq!( + hash_root.unwrap(), + hex!("b9eb2caf2d691b183c2d57f322afe505c078cd08101324f61c3641714789a54e").into() + ); + } + + #[test] + pub fn test_hash_sync_aggregate() { + let hash_root = SyncAggregate::<512, 64>{ + sync_committee_bits: hex!("cefffffefffffff767fffbedffffeffffeeffdffffdebffffff7f7dbdf7fffdffffbffcfffdff79dfffbbfefff2ffffff7ddeff7ffffc98ff7fbfffffffffff7"), + sync_committee_signature: hex!("8af1a8577bba419fe054ee49b16ed28e081dda6d3ba41651634685e890992a0b675e20f8d9f2ec137fe9eb50e838aa6117f9f5410e2e1024c4b4f0e098e55144843ce90b7acde52fe7b94f2a1037342c951dc59f501c92acf7ed944cb6d2b5f7").into(), + }.hash_tree_root(); + + assert!(hash_root.is_ok()); + assert_eq!( + hash_root.unwrap(), + hex!("e6dcad4f60ce9ff8a587b110facbaf94721f06cd810b6d8bf6cffa641272808d").into() + ); + } + + #[test] + pub fn test_hash_execution_payload() { + let hash_root = + ExecutionPayloadHeader{ + parent_hash: hex!("eadee5ab098dde64e9fd02ae5858064bad67064070679625b09f8d82dec183f7").into(), + fee_recipient: hex!("f97e180c050e5ab072211ad2c213eb5aee4df134").into(), + state_root: hex!("564fa064c2a324c2b5978d7fdfc5d4224d4f421a45388af1ed405a399c845dff").into(), + receipts_root: hex!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").into(), + logs_bloom: hex!("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").to_vec(), + prev_randao: hex!("6bf538bdfbdf1c96ff528726a40658a91d0bda0f1351448c4c4f3604db2a0ccf").into(), + block_number: 477434, + gas_limit: 8154925, + gas_used: 0, + timestamp: 1652816940, + extra_data: vec![], + base_fee_per_gas: U256::from(7_i16), + block_hash: hex!("cd8df91b4503adb8f2f1c7a4f60e07a1f1a2cbdfa2a95bceba581f3ff65c1968").into(), + transactions_root: hex!("7ffe241ea60187fdb0187bfa22de35d1f9bed7ab061d9401fd47e34a54fbede1").into(), + withdrawals_root: hex!("28ba1834a3a7b657460ce79fa3a1d909ab8828fd557659d4d0554a9bdbc0ec30").into(), + }.hash_tree_root(); + assert!(hash_root.is_ok()); + } +} + +/// Operating modes for beacon client +#[derive(Encode, Decode, Copy, Clone, PartialEq, RuntimeDebug, TypeInfo)] +pub enum Mode { + Active, + Blocked, +} diff --git a/bridges/snowbridge/parachain/primitives/beacon/src/updates.rs b/bridges/snowbridge/parachain/primitives/beacon/src/updates.rs new file mode 100644 index 000000000000..9a78b4f1e2d3 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/beacon/src/updates.rs @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use codec::{Decode, Encode}; +use frame_support::{CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound}; +use scale_info::TypeInfo; +use sp_core::H256; +use sp_std::prelude::*; + +use crate::types::{BeaconHeader, ExecutionPayloadHeader, SyncAggregate, SyncCommittee}; + +#[derive(Encode, Decode, CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo)] +#[cfg_attr( + feature = "std", + derive(serde::Serialize, serde::Deserialize), + serde(deny_unknown_fields, bound(serialize = ""), bound(deserialize = "")) +)] +pub struct CheckpointUpdate { + pub header: BeaconHeader, + pub current_sync_committee: SyncCommittee, + pub current_sync_committee_branch: Vec, + pub validators_root: H256, + pub block_roots_root: H256, + pub block_roots_branch: Vec, +} + +impl Default for CheckpointUpdate { + fn default() -> Self { + CheckpointUpdate { + header: Default::default(), + current_sync_committee: Default::default(), + current_sync_committee_branch: Default::default(), + validators_root: Default::default(), + block_roots_root: Default::default(), + block_roots_branch: Default::default(), + } + } +} + +#[derive( + Default, Encode, Decode, CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo, +)] +#[cfg_attr( + feature = "std", + derive(serde::Deserialize), + serde(deny_unknown_fields, bound(serialize = ""), bound(deserialize = "")) +)] +pub struct Update { + /// A recent header attesting to the finalized header, using its `state_root`. + pub attested_header: BeaconHeader, + /// The signing data that the sync committee produced for this attested header, including + /// who participated in the vote and the resulting signature. + pub sync_aggregate: SyncAggregate, + /// The slot at which the sync aggregate can be found, typically attested_header.slot + 1, if + /// the next slot block was not missed. + pub signature_slot: u64, + /// The next sync committee for the next sync committee period, if present. + pub next_sync_committee_update: Option>, + /// The latest finalized header. + pub finalized_header: BeaconHeader, + /// The merkle proof testifying to the finalized header, using the `attested_header.state_root` + /// as tree root. + pub finality_branch: Vec, + /// The finalized_header's `block_roots` root in the beacon state, used for ancestry proofs. + pub block_roots_root: H256, + /// The merkle path to prove the `block_roots_root` value. + pub block_roots_branch: Vec, +} + +#[derive( + Default, Encode, Decode, CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo, +)] +#[cfg_attr( + feature = "std", + derive(serde::Deserialize), + serde(deny_unknown_fields, bound(serialize = ""), bound(deserialize = "")) +)] +pub struct NextSyncCommitteeUpdate { + pub next_sync_committee: SyncCommittee, + pub next_sync_committee_branch: Vec, +} + +#[derive(Encode, Decode, CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo)] +#[cfg_attr( + feature = "std", + derive(serde::Deserialize), + serde(deny_unknown_fields, bound(serialize = ""), bound(deserialize = "")) +)] +pub struct ExecutionHeaderUpdate { + /// Header for the beacon block containing the execution payload + pub header: BeaconHeader, + /// Proof that `header` is an ancestor of a finalized header + pub ancestry_proof: Option, + /// Execution header to be imported + pub execution_header: ExecutionPayloadHeader, + /// Merkle proof that execution payload is contained within `header` + pub execution_branch: Vec, +} + +#[derive(Encode, Decode, CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo)] +#[cfg_attr( + feature = "std", + derive(serde::Deserialize), + serde(deny_unknown_fields, bound(serialize = ""), bound(deserialize = "")) +)] +pub struct AncestryProof { + /// Merkle proof that `header` is an ancestor of `finalized_header` + pub header_branch: Vec, + /// Root of a finalized block that has already been imported into the light client + pub finalized_block_root: H256, +} diff --git a/bridges/snowbridge/parachain/primitives/core/Cargo.toml b/bridges/snowbridge/parachain/primitives/core/Cargo.toml new file mode 100644 index 000000000000..262fc60b0cba --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/core/Cargo.toml @@ -0,0 +1,60 @@ +[package] +name = "snowbridge-core" +description = "Snowbridge Core" +version = "0.1.1" +authors = ["Snowfork "] +edition = "2021" +license = "Apache-2.0" + +[dependencies] +serde = { version = "1.0.188", optional = true, features = ["alloc", "derive"], default-features = false } +codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false } +scale-info = { version = "2.9.0", default-features = false, features = ["derive"] } +hex-literal = { version = "0.4.1" } + +polkadot-parachain-primitives = { path = "../../../../../polkadot/parachain", default-features = false } +xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false } +xcm-builder = { package = "staging-xcm-builder", path = "../../../../../polkadot/xcm/xcm-builder", default-features = false } + +frame-support = { path = "../../../../../substrate/frame/support", default-features = false } +frame-system = { path = "../../../../../substrate/frame/system", default-features = false } +sp-runtime = { path = "../../../../../substrate/primitives/runtime", default-features = false } +sp-std = { path = "../../../../../substrate/primitives/std", default-features = false } +sp-io = { path = "../../../../../substrate/primitives/io", default-features = false } +sp-core = { path = "../../../../../substrate/primitives/core", default-features = false } +sp-arithmetic = { path = "../../../../../substrate/primitives/arithmetic", default-features = false } + +snowbridge-beacon-primitives = { path = "../../primitives/beacon", default-features = false } + +ethabi = { git = "https://github.com/Snowfork/ethabi-decode.git", package = "ethabi-decode", branch = "master", default-features = false } + +[dev-dependencies] +hex = { version = "0.4.3" } + +[features] +default = ["std"] +std = [ + "codec/std", + "ethabi/std", + "frame-support/std", + "frame-system/std", + "polkadot-parachain-primitives/std", + "scale-info/std", + "serde/std", + "snowbridge-beacon-primitives/std", + "sp-arithmetic/std", + "sp-core/std", + "sp-io/std", + "sp-runtime/std", + "sp-std/std", + "xcm-builder/std", + "xcm/std", +] +serde = ["dep:serde", "scale-info/serde"] +runtime-benchmarks = [ + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "polkadot-parachain-primitives/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", + "xcm-builder/runtime-benchmarks", +] diff --git a/bridges/snowbridge/parachain/primitives/core/src/inbound.rs b/bridges/snowbridge/parachain/primitives/core/src/inbound.rs new file mode 100644 index 000000000000..4b04470ad026 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/core/src/inbound.rs @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! Types for representing inbound messages + +use codec::{Decode, Encode}; +use frame_support::PalletError; +use scale_info::TypeInfo; +use sp_core::{H160, H256}; +use sp_runtime::RuntimeDebug; +use sp_std::vec::Vec; + +/// A trait for verifying inbound messages from Ethereum. +pub trait Verifier { + fn verify(event: &Log, proof: &Proof) -> Result<(), VerificationError>; +} + +#[derive(Clone, Encode, Decode, RuntimeDebug, PalletError, TypeInfo)] +#[cfg_attr(feature = "std", derive(PartialEq))] +pub enum VerificationError { + /// Execution header is missing + HeaderNotFound, + /// Event log was not found in the verified transaction receipt + LogNotFound, + /// Event log has an invalid format + InvalidLog, + /// Unable to verify the transaction receipt with the provided proof + InvalidProof, +} + +pub type MessageNonce = u64; + +/// A bridge message from the Gateway contract on Ethereum +#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] +pub struct Message { + /// Event log emitted by Gateway contract + pub event_log: Log, + /// Inclusion proof for a transaction receipt containing the event log + pub proof: Proof, +} + +const MAX_TOPICS: usize = 4; + +#[derive(Clone, RuntimeDebug)] +pub enum LogValidationError { + TooManyTopics, +} + +/// Event log +#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] +pub struct Log { + pub address: H160, + pub topics: Vec, + pub data: Vec, +} + +impl Log { + pub fn validate(&self) -> Result<(), LogValidationError> { + if self.topics.len() > MAX_TOPICS { + return Err(LogValidationError::TooManyTopics) + } + Ok(()) + } +} + +/// Inclusion proof for a transaction receipt +#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] +pub struct Proof { + // The block hash of the block in which the receipt was included. + pub block_hash: H256, + // The index of the transaction (and receipt) within the block. + pub tx_index: u32, + // Proof keys and values (receipts tree) + pub data: (Vec>, Vec>), +} diff --git a/bridges/snowbridge/parachain/primitives/core/src/lib.rs b/bridges/snowbridge/parachain/primitives/core/src/lib.rs new file mode 100644 index 000000000000..ecbc3bb365fc --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/core/src/lib.rs @@ -0,0 +1,174 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! # Core +//! +//! Common traits and types +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(test)] +mod tests; + +pub mod inbound; +pub mod operating_mode; +pub mod outbound; +pub mod pricing; +pub mod ringbuffer; + +pub use polkadot_parachain_primitives::primitives::{ + Id as ParaId, IsSystem, Sibling as SiblingParaId, +}; +pub use ringbuffer::{RingBufferMap, RingBufferMapImpl}; +pub use sp_core::U256; + +use codec::{Decode, Encode, MaxEncodedLen}; +use frame_support::traits::Contains; +use hex_literal::hex; +use scale_info::TypeInfo; +use sp_core::H256; +use sp_io::hashing::keccak_256; +use sp_runtime::{traits::AccountIdConversion, RuntimeDebug}; +use sp_std::prelude::*; +use xcm::prelude::{ + Junction::Parachain, + Junctions::{Here, X1}, + MultiLocation, +}; +use xcm_builder::{DescribeAllTerminal, DescribeFamily, DescribeLocation, HashedDescription}; + +/// The ID of an agent contract +pub type AgentId = H256; +pub use operating_mode::BasicOperatingMode; + +pub use pricing::{PricingParameters, Rewards}; + +pub fn sibling_sovereign_account(para_id: ParaId) -> T::AccountId +where + T: frame_system::Config, +{ + SiblingParaId::from(para_id).into_account_truncating() +} + +pub fn sibling_sovereign_account_raw(para_id: ParaId) -> [u8; 32] { + SiblingParaId::from(para_id).into_account_truncating() +} + +pub struct AllowSiblingsOnly; +impl Contains for AllowSiblingsOnly { + fn contains(location: &MultiLocation) -> bool { + matches!(location, MultiLocation { parents: 1, interior: X1(Parachain(_)) }) + } +} + +pub fn gwei(x: u128) -> U256 { + U256::from(1_000_000_000u128).saturating_mul(x.into()) +} + +pub fn meth(x: u128) -> U256 { + U256::from(1_000_000_000_000_000u128).saturating_mul(x.into()) +} + +pub fn eth(x: u128) -> U256 { + U256::from(1_000_000_000_000_000_000u128).saturating_mul(x.into()) +} + +pub const ROC: u128 = 1_000_000_000_000; + +/// Identifier for a message channel +#[derive( + Clone, Copy, Encode, Decode, PartialEq, Eq, Default, RuntimeDebug, MaxEncodedLen, TypeInfo, +)] +pub struct ChannelId([u8; 32]); + +/// Deterministically derive a ChannelId for a sibling parachain +/// Generator: keccak256("para" + big_endian_bytes(para_id)) +/// +/// The equivalent generator on the Solidity side is in +/// contracts/src/Types.sol:into(). +fn derive_channel_id_for_sibling(para_id: ParaId) -> ChannelId { + let para_id: u32 = para_id.into(); + let para_id_bytes: [u8; 4] = para_id.to_be_bytes(); + let prefix: [u8; 4] = *b"para"; + let preimage: Vec = prefix.into_iter().chain(para_id_bytes).collect(); + keccak_256(&preimage).into() +} + +impl ChannelId { + pub const fn new(id: [u8; 32]) -> Self { + ChannelId(id) + } +} + +impl From for ChannelId { + fn from(value: ParaId) -> Self { + derive_channel_id_for_sibling(value) + } +} + +impl From<[u8; 32]> for ChannelId { + fn from(value: [u8; 32]) -> Self { + ChannelId(value) + } +} + +impl From for [u8; 32] { + fn from(value: ChannelId) -> Self { + value.0 + } +} + +impl<'a> From<&'a [u8; 32]> for ChannelId { + fn from(value: &'a [u8; 32]) -> Self { + ChannelId(*value) + } +} + +impl From for ChannelId { + fn from(value: H256) -> Self { + ChannelId(value.into()) + } +} + +impl AsRef<[u8]> for ChannelId { + fn as_ref(&self) -> &[u8] { + &self.0 + } +} + +#[derive(Clone, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo)] +pub struct Channel { + /// ID of the agent contract deployed on Ethereum + pub agent_id: AgentId, + /// ID of the parachain who will receive or send messages using this channel + pub para_id: ParaId, +} + +pub trait StaticLookup { + /// Type to lookup from. + type Source; + /// Type to lookup into. + type Target; + /// Attempt a lookup. + fn lookup(s: Self::Source) -> Option; +} + +/// Channel for high-priority governance commands +pub const PRIMARY_GOVERNANCE_CHANNEL: ChannelId = + ChannelId::new(hex!("0000000000000000000000000000000000000000000000000000000000000001")); + +/// Channel for lower-priority governance commands +pub const SECONDARY_GOVERNANCE_CHANNEL: ChannelId = + ChannelId::new(hex!("0000000000000000000000000000000000000000000000000000000000000002")); + +pub struct DescribeHere; +impl DescribeLocation for DescribeHere { + fn describe_location(l: &MultiLocation) -> Option> { + match (l.parents, l.interior) { + (0, Here) => Some(Vec::::new().encode()), + _ => None, + } + } +} + +/// Creates an AgentId from a MultiLocation. An AgentId is a unique mapping to a Agent contract on +/// Ethereum which acts as the sovereign account for the MultiLocation. +pub type AgentIdOf = HashedDescription)>; diff --git a/bridges/snowbridge/parachain/primitives/core/src/operating_mode.rs b/bridges/snowbridge/parachain/primitives/core/src/operating_mode.rs new file mode 100644 index 000000000000..9894e587ef5e --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/core/src/operating_mode.rs @@ -0,0 +1,25 @@ +use codec::{Decode, Encode, MaxEncodedLen}; +use scale_info::TypeInfo; +use sp_runtime::RuntimeDebug; + +/// Basic operating modes for a bridges module (Normal/Halted). +#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub enum BasicOperatingMode { + /// Normal mode, when all operations are allowed. + Normal, + /// The pallet is halted. All non-governance operations are disabled. + Halted, +} + +impl Default for BasicOperatingMode { + fn default() -> Self { + Self::Normal + } +} + +impl BasicOperatingMode { + pub fn is_halted(&self) -> bool { + *self == BasicOperatingMode::Halted + } +} diff --git a/bridges/snowbridge/parachain/primitives/core/src/outbound.rs b/bridges/snowbridge/parachain/primitives/core/src/outbound.rs new file mode 100644 index 000000000000..bce123878d3a --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/core/src/outbound.rs @@ -0,0 +1,413 @@ +use codec::{Decode, Encode}; +use frame_support::PalletError; +use scale_info::TypeInfo; +use sp_arithmetic::traits::{BaseArithmetic, Unsigned}; +use sp_core::{RuntimeDebug, H256}; +pub use v1::{AgentExecuteCommand, Command, Initializer, Message, OperatingMode, QueuedMessage}; + +/// Enqueued outbound messages need to be versioned to prevent data corruption +/// or loss after forkless runtime upgrades +#[derive(Encode, Decode, TypeInfo, Clone, RuntimeDebug)] +#[cfg_attr(feature = "std", derive(PartialEq))] +pub enum VersionedQueuedMessage { + V1(QueuedMessage), +} + +impl TryFrom for QueuedMessage { + type Error = (); + fn try_from(x: VersionedQueuedMessage) -> Result { + use VersionedQueuedMessage::*; + match x { + V1(x) => Ok(x), + } + } +} + +impl> From for VersionedQueuedMessage { + fn from(x: T) -> Self { + VersionedQueuedMessage::V1(x.into()) + } +} + +mod v1 { + use crate::{pricing::UD60x18, ChannelId}; + use codec::{Decode, Encode}; + use ethabi::Token; + use scale_info::TypeInfo; + use sp_core::{RuntimeDebug, H160, H256, U256}; + use sp_std::{borrow::ToOwned, vec, vec::Vec}; + + /// A message which can be accepted by implementations of `/[`SendMessage`\]` + #[derive(Encode, Decode, TypeInfo, Clone, RuntimeDebug)] + #[cfg_attr(feature = "std", derive(PartialEq))] + pub struct Message { + /// ID for this message. One will be automatically generated if not provided. + /// + /// When this message is created from an XCM message, the ID should be extracted + /// from the `SetTopic` instruction. + /// + /// The ID plays no role in bridge consensus, and is purely meant for message tracing. + pub id: Option, + /// The message channel ID + pub channel_id: ChannelId, + /// The stable ID for a receiving gateway contract + pub command: Command, + } + + /// The operating mode of Channels and Gateway contract on Ethereum. + #[derive(Copy, Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo)] + pub enum OperatingMode { + /// Normal operations. Allow sending and receiving messages. + Normal, + /// Reject outbound messages. This allows receiving governance messages but does now allow + /// enqueuing of new messages from the Ethereum side. This can be used to close off an + /// deprecated channel or pause the bridge for upgrade operations. + RejectingOutboundMessages, + } + + /// A command which is executable by the Gateway contract on Ethereum + #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] + #[cfg_attr(feature = "std", derive(PartialEq))] + pub enum Command { + /// Execute a sub-command within an agent for a consensus system in Polkadot + AgentExecute { + /// The ID of the agent + agent_id: H256, + /// The sub-command to be executed + command: AgentExecuteCommand, + }, + /// Upgrade the Gateway contract + Upgrade { + /// Address of the new implementation contract + impl_address: H160, + /// Codehash of the implementation contract + impl_code_hash: H256, + /// Optionally invoke an initializer in the implementation contract + initializer: Option, + }, + /// Create an agent representing a consensus system on Polkadot + CreateAgent { + /// The ID of the agent, derived from the `MultiLocation` of the consensus system on + /// Polkadot + agent_id: H256, + }, + /// Create bidirectional messaging channel to a parachain + CreateChannel { + /// The ID of the channel + channel_id: ChannelId, + /// The agent ID of the parachain + agent_id: H256, + /// Initial operating mode + mode: OperatingMode, + }, + /// Update the configuration of a channel + UpdateChannel { + /// The ID of the channel + channel_id: ChannelId, + /// The new operating mode + mode: OperatingMode, + }, + /// Set the global operating mode of the Gateway contract + SetOperatingMode { + /// The new operating mode + mode: OperatingMode, + }, + /// Transfer ether from an agent contract to a recipient account + TransferNativeFromAgent { + /// The agent ID + agent_id: H256, + /// The recipient of the ether + recipient: H160, + /// The amount to transfer + amount: u128, + }, + /// Set token fees of the Gateway contract + SetTokenTransferFees { + /// The fee(DOT) for the cost of creating asset on AssetHub + create_asset_xcm: u128, + /// The fee(DOT) for the cost of sending asset on AssetHub + transfer_asset_xcm: u128, + /// The fee(Ether) for register token to discourage spamming + register_token: U256, + }, + /// Set pricing parameters + SetPricingParameters { + // ETH/DOT exchange rate + exchange_rate: UD60x18, + // Cost of delivering a message from Ethereum to BridgeHub, in ROC/KSM/DOT + delivery_cost: u128, + }, + } + + impl Command { + /// Compute the enum variant index + pub fn index(&self) -> u8 { + match self { + Command::AgentExecute { .. } => 0, + Command::Upgrade { .. } => 1, + Command::CreateAgent { .. } => 2, + Command::CreateChannel { .. } => 3, + Command::UpdateChannel { .. } => 4, + Command::SetOperatingMode { .. } => 5, + Command::TransferNativeFromAgent { .. } => 6, + Command::SetTokenTransferFees { .. } => 7, + Command::SetPricingParameters { .. } => 8, + } + } + + /// ABI-encode the Command. + pub fn abi_encode(&self) -> Vec { + match self { + Command::AgentExecute { agent_id, command } => + ethabi::encode(&[Token::Tuple(vec![ + Token::FixedBytes(agent_id.as_bytes().to_owned()), + Token::Bytes(command.abi_encode()), + ])]), + Command::Upgrade { impl_address, impl_code_hash, initializer, .. } => + ethabi::encode(&[Token::Tuple(vec![ + Token::Address(*impl_address), + Token::FixedBytes(impl_code_hash.as_bytes().to_owned()), + initializer + .clone() + .map_or(Token::Bytes(vec![]), |i| Token::Bytes(i.params)), + ])]), + Command::CreateAgent { agent_id } => + ethabi::encode(&[Token::Tuple(vec![Token::FixedBytes( + agent_id.as_bytes().to_owned(), + )])]), + Command::CreateChannel { channel_id, agent_id, mode } => + ethabi::encode(&[Token::Tuple(vec![ + Token::FixedBytes(channel_id.as_ref().to_owned()), + Token::FixedBytes(agent_id.as_bytes().to_owned()), + Token::Uint(U256::from((*mode) as u64)), + ])]), + Command::UpdateChannel { channel_id, mode } => + ethabi::encode(&[Token::Tuple(vec![ + Token::FixedBytes(channel_id.as_ref().to_owned()), + Token::Uint(U256::from((*mode) as u64)), + ])]), + Command::SetOperatingMode { mode } => + ethabi::encode(&[Token::Tuple(vec![Token::Uint(U256::from((*mode) as u64))])]), + Command::TransferNativeFromAgent { agent_id, recipient, amount } => + ethabi::encode(&[Token::Tuple(vec![ + Token::FixedBytes(agent_id.as_bytes().to_owned()), + Token::Address(*recipient), + Token::Uint(U256::from(*amount)), + ])]), + Command::SetTokenTransferFees { + create_asset_xcm, + transfer_asset_xcm, + register_token, + } => ethabi::encode(&[Token::Tuple(vec![ + Token::Uint(U256::from(*create_asset_xcm)), + Token::Uint(U256::from(*transfer_asset_xcm)), + Token::Uint(*register_token), + ])]), + Command::SetPricingParameters { exchange_rate, delivery_cost } => + ethabi::encode(&[Token::Tuple(vec![ + Token::Uint(exchange_rate.clone().into_inner()), + Token::Uint(U256::from(*delivery_cost)), + ])]), + } + } + } + + /// Representation of a call to the initializer of an implementation contract. + /// The initializer has the following ABI signature: `initialize(bytes)`. + #[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] + pub struct Initializer { + /// ABI-encoded params of type `bytes` to pass to the initializer + pub params: Vec, + /// The initializer is allowed to consume this much gas at most. + pub maximum_required_gas: u64, + } + + /// A Sub-command executable within an agent + #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] + #[cfg_attr(feature = "std", derive(PartialEq))] + pub enum AgentExecuteCommand { + /// Transfer ERC20 tokens + TransferToken { + /// Address of the ERC20 token + token: H160, + /// The recipient of the tokens + recipient: H160, + /// The amount of tokens to transfer + amount: u128, + }, + } + + impl AgentExecuteCommand { + fn index(&self) -> u8 { + match self { + AgentExecuteCommand::TransferToken { .. } => 0, + } + } + + /// ABI-encode the sub-command + pub fn abi_encode(&self) -> Vec { + match self { + AgentExecuteCommand::TransferToken { token, recipient, amount } => + ethabi::encode(&[ + Token::Uint(self.index().into()), + Token::Bytes(ethabi::encode(&[ + Token::Address(*token), + Token::Address(*recipient), + Token::Uint(U256::from(*amount)), + ])), + ]), + } + } + } + + /// Message which is awaiting processing in the MessageQueue pallet + #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] + #[cfg_attr(feature = "std", derive(PartialEq))] + pub struct QueuedMessage { + /// Message ID + pub id: H256, + /// Channel ID + pub channel_id: ChannelId, + /// Command to execute in the Gateway contract + pub command: Command, + } +} + +#[cfg_attr(feature = "std", derive(PartialEq, Debug))] +/// Fee for delivering message +pub struct Fee +where + Balance: BaseArithmetic + Unsigned + Copy, +{ + /// Fee to cover cost of processing the message locally + pub local: Balance, + /// Fee to cover cost processing the message remotely + pub remote: Balance, +} + +impl Fee +where + Balance: BaseArithmetic + Unsigned + Copy, +{ + pub fn total(&self) -> Balance { + self.local.saturating_add(self.remote) + } +} + +impl From<(Balance, Balance)> for Fee +where + Balance: BaseArithmetic + Unsigned + Copy, +{ + fn from((local, remote): (Balance, Balance)) -> Self { + Self { local, remote } + } +} + +/// A trait for sending messages to Ethereum +pub trait SendMessage: SendMessageFeeProvider { + type Ticket: Clone + Encode + Decode; + + /// Validate an outbound message and return a tuple: + /// 1. Ticket for submitting the message + /// 2. Delivery fee + fn validate( + message: &Message, + ) -> Result<(Self::Ticket, Fee<::Balance>), SendError>; + + /// Submit the message ticket for eventual delivery to Ethereum + fn deliver(ticket: Self::Ticket) -> Result; +} + +pub trait Ticket: Encode + Decode + Clone { + fn message_id(&self) -> H256; +} + +/// A trait for getting the local costs associated with sending a message. +pub trait SendMessageFeeProvider { + type Balance: BaseArithmetic + Unsigned + Copy; + + /// The local component of the message processing fees in native currency + fn local_fee() -> Self::Balance; +} + +/// Reasons why sending to Ethereum could not be initiated +#[derive(Copy, Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, PalletError, TypeInfo)] +pub enum SendError { + /// Message is too large to be safely executed on Ethereum + MessageTooLarge, + /// The bridge has been halted for maintenance + Halted, + /// Invalid Channel + InvalidChannel, +} + +pub trait GasMeter { + /// All the gas used for submitting a message to Ethereum, minus the cost of dispatching + /// the command within the message + const MAXIMUM_BASE_GAS: u64; + + fn maximum_gas_used_at_most(command: &Command) -> u64 { + Self::MAXIMUM_BASE_GAS + Self::maximum_dispatch_gas_used_at_most(command) + } + + /// Measures the maximum amount of gas a command payload will require to dispatch, AFTER + /// validation & verification. + fn maximum_dispatch_gas_used_at_most(command: &Command) -> u64; +} + +/// A meter that assigns a constant amount of gas for the execution of a command +/// +/// The gas figures are extracted from this report: +/// > forge test --match-path test/Gateway.t.sol --gas-report +/// +/// A healthy buffer is added on top of these figures to account for: +/// * The EIP-150 63/64 rule +/// * Future EVM upgrades that may increase gas cost +pub struct ConstantGasMeter; + +impl GasMeter for ConstantGasMeter { + // The base transaction cost, which includes: + // 21_000 transaction cost, roughly worst case 64_000 for calldata, and 100_000 + // for message verification + const MAXIMUM_BASE_GAS: u64 = 185_000; + + fn maximum_dispatch_gas_used_at_most(command: &Command) -> u64 { + match command { + Command::CreateAgent { .. } => 275_000, + Command::CreateChannel { .. } => 100_000, + Command::UpdateChannel { .. } => 50_000, + Command::TransferNativeFromAgent { .. } => 60_000, + Command::SetOperatingMode { .. } => 40_000, + Command::AgentExecute { command, .. } => match command { + // Execute IERC20.transferFrom + // + // Worst-case assumptions are important: + // * No gas refund for clearing storage slot of source account in ERC20 contract + // * Assume dest account in ERC20 contract does not yet have a storage slot + // * ERC20.transferFrom possibly does other business logic besides updating balances + AgentExecuteCommand::TransferToken { .. } => 100_000, + }, + Command::Upgrade { initializer, .. } => { + let initializer_max_gas = match *initializer { + Some(Initializer { maximum_required_gas, .. }) => maximum_required_gas, + None => 0, + }; + // total maximum gas must also include the gas used for updating the proxy before + // the the initializer is called. + 50_000 + initializer_max_gas + }, + Command::SetTokenTransferFees { .. } => 60_000, + Command::SetPricingParameters { .. } => 60_000, + } + } +} + +impl GasMeter for () { + const MAXIMUM_BASE_GAS: u64 = 1; + + fn maximum_dispatch_gas_used_at_most(_: &Command) -> u64 { + 1 + } +} + +pub const ETHER_DECIMALS: u8 = 18; diff --git a/bridges/snowbridge/parachain/primitives/core/src/pricing.rs b/bridges/snowbridge/parachain/primitives/core/src/pricing.rs new file mode 100644 index 000000000000..33aeda6d15c4 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/core/src/pricing.rs @@ -0,0 +1,67 @@ +use codec::{Decode, Encode, MaxEncodedLen}; +use scale_info::TypeInfo; +use sp_arithmetic::traits::{BaseArithmetic, Unsigned, Zero}; +use sp_core::U256; +use sp_runtime::{FixedU128, RuntimeDebug}; +use sp_std::prelude::*; + +#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, MaxEncodedLen, TypeInfo)] +pub struct PricingParameters { + /// ETH/DOT exchange rate + pub exchange_rate: FixedU128, + /// Relayer rewards + pub rewards: Rewards, + /// Ether (wei) fee per gas unit + pub fee_per_gas: U256, +} + +#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, MaxEncodedLen, TypeInfo)] +pub struct Rewards { + /// Local reward in DOT + pub local: Balance, + /// Remote reward in ETH (wei) + pub remote: U256, +} + +#[derive(RuntimeDebug)] +pub struct InvalidPricingParameters; + +impl PricingParameters +where + Balance: BaseArithmetic + Unsigned + Copy, +{ + pub fn validate(&self) -> Result<(), InvalidPricingParameters> { + if self.exchange_rate == FixedU128::zero() { + return Err(InvalidPricingParameters) + } + if self.fee_per_gas == U256::zero() { + return Err(InvalidPricingParameters) + } + if self.rewards.local.is_zero() { + return Err(InvalidPricingParameters) + } + if self.rewards.remote.is_zero() { + return Err(InvalidPricingParameters) + } + Ok(()) + } +} + +/// Holder for fixed point number implemented in +#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)] +#[cfg_attr(feature = "std", derive(PartialEq))] +pub struct UD60x18(U256); + +impl From for UD60x18 { + fn from(value: FixedU128) -> Self { + // Both FixedU128 and UD60x18 have 18 decimal places + let inner: u128 = value.into_inner(); + UD60x18(inner.into()) + } +} + +impl UD60x18 { + pub fn into_inner(self) -> U256 { + self.0 + } +} diff --git a/bridges/snowbridge/parachain/primitives/core/src/ringbuffer.rs b/bridges/snowbridge/parachain/primitives/core/src/ringbuffer.rs new file mode 100644 index 000000000000..dcee20359a78 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/core/src/ringbuffer.rs @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use codec::FullCodec; +use core::{cmp::Ord, marker::PhantomData, ops::Add}; +use frame_support::storage::{types::QueryKindTrait, StorageMap, StorageValue}; +use sp_core::{Get, GetDefault}; +use sp_runtime::traits::{One, Zero}; + +/// Trait object presenting the ringbuffer interface. +pub trait RingBufferMap +where + Key: FullCodec, + Value: FullCodec, + QueryKind: QueryKindTrait, +{ + /// Insert a map entry. + fn insert(k: Key, v: Value); + + /// Check if map contains a key + fn contains_key(k: Key) -> bool; + + /// Get the value of the key + fn get(k: Key) -> QueryKind::Query; +} + +pub struct RingBufferMapImpl( + PhantomData<(Index, B, CurrentIndex, Intermediate, M, QueryKind)>, +); + +/// Ringbuffer implementation based on `RingBufferTransient` +impl + RingBufferMap + for RingBufferMapImpl +where + Key: FullCodec + Clone, + Value: FullCodec, + Index: Ord + One + Zero + Add + Copy + FullCodec + Eq, + B: Get, + CurrentIndex: StorageValue, + Intermediate: StorageMap, + M: StorageMap, + QueryKind: QueryKindTrait, +{ + /// Insert a map entry. + fn insert(k: Key, v: Value) { + let bound = B::get(); + let mut current_index = CurrentIndex::get(); + + // Adding one here as bound denotes number of items but our index starts with zero. + if (current_index + Index::one()) >= bound { + current_index = Index::zero(); + } else { + current_index = current_index + Index::one(); + } + + // Deleting earlier entry if it exists + if Intermediate::contains_key(current_index) { + let older_key = Intermediate::get(current_index); + M::remove(older_key); + } + + Intermediate::insert(current_index, k.clone()); + CurrentIndex::set(current_index); + M::insert(k, v); + } + + /// Check if map contains a key + fn contains_key(k: Key) -> bool { + M::contains_key(k) + } + + /// Get the value associated with key + fn get(k: Key) -> M::Query { + M::get(k) + } +} diff --git a/bridges/snowbridge/parachain/primitives/core/src/tests.rs b/bridges/snowbridge/parachain/primitives/core/src/tests.rs new file mode 100644 index 000000000000..725fff1a9c94 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/core/src/tests.rs @@ -0,0 +1,13 @@ +use crate::{ChannelId, ParaId}; +use hex_literal::hex; + +const EXPECT_CHANNEL_ID: [u8; 32] = + hex!("c173fac324158e77fb5840738a1a541f633cbec8884c6a601c567d2b376a0539"); + +// The Solidity equivalent code is tested in Gateway.t.sol:testDeriveChannelID +#[test] +fn generate_channel_id() { + let para_id: ParaId = 1000.into(); + let channel_id: ChannelId = para_id.into(); + assert_eq!(channel_id, EXPECT_CHANNEL_ID.into()); +} diff --git a/bridges/snowbridge/parachain/primitives/core/tests/fixtures/packet.scale b/bridges/snowbridge/parachain/primitives/core/tests/fixtures/packet.scale new file mode 100644 index 000000000000..d5f6696ea69f Binary files /dev/null and b/bridges/snowbridge/parachain/primitives/core/tests/fixtures/packet.scale differ diff --git a/bridges/snowbridge/parachain/primitives/core/tests/mod.rs b/bridges/snowbridge/parachain/primitives/core/tests/mod.rs new file mode 100644 index 000000000000..2da5d2df182e --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/core/tests/mod.rs @@ -0,0 +1,14 @@ +#[cfg(test)] +mod tests { + use frame_support::traits::Contains; + use snowbridge_core::AllowSiblingsOnly; + use xcm::prelude::{Junction::Parachain, Junctions::X1, MultiLocation}; + + #[test] + fn allow_siblings_predicate_only_allows_siblings() { + let sibling = MultiLocation::new(1, X1(Parachain(1000))); + let child = MultiLocation::new(0, X1(Parachain(1000))); + assert!(AllowSiblingsOnly::contains(&sibling), "Sibling returns true."); + assert!(!AllowSiblingsOnly::contains(&child), "Child returns false."); + } +} diff --git a/bridges/snowbridge/parachain/primitives/ethereum/.cargo/config.toml b/bridges/snowbridge/parachain/primitives/ethereum/.cargo/config.toml new file mode 100644 index 000000000000..4ec2f3b86203 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/ethereum/.cargo/config.toml @@ -0,0 +1,2 @@ +[target.wasm32-unknown-unknown] +runner = 'wasm-bindgen-test-runner' diff --git a/bridges/snowbridge/parachain/primitives/ethereum/Cargo.toml b/bridges/snowbridge/parachain/primitives/ethereum/Cargo.toml new file mode 100644 index 000000000000..1eff2632b099 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/ethereum/Cargo.toml @@ -0,0 +1,51 @@ +[package] +name = "snowbridge-ethereum" +description = "Snowbridge Ethereum" +version = "0.1.0" +authors = ["Snowfork "] +edition = "2021" +license = "Apache-2.0" + +[dependencies] +serde = { version = "1.0.188", optional = true, features = ["derive"] } +serde-big-array = { version = "0.3.2", optional = true, features = ["const-generics"] } +codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive"] } +scale-info = { version = "2.9.0", default-features = false, features = ["derive"] } +ethbloom = { version = "0.13.0", default-features = false } +ethereum-types = { version = "0.14.1", default-features = false, features = ["codec", "rlp", "serialize"] } +hex = { package = "rustc-hex", version = "2.1.0", default-features = false } +hex-literal = { version = "0.4.1", default-features = false } +parity-bytes = { version = "0.1.2", default-features = false } +rlp = { version = "0.5.2", default-features = false } + +sp-io = { path = "../../../../../substrate/primitives/io", default-features = false } +sp-std = { path = "../../../../../substrate/primitives/std", default-features = false } +sp-core = { path = "../../../../../substrate/primitives/core", default-features = false } +sp-runtime = { path = "../../../../../substrate/primitives/runtime", default-features = false } + +ethabi = { git = "https://github.com/snowfork/ethabi-decode.git", package = "ethabi-decode", branch = "master", default-features = false } + +[dev-dependencies] +wasm-bindgen-test = "0.3.19" +rand = "0.8.5" +serde_json = "1.0.96" + +[features] +default = ["std"] +expensive_tests = [] +std = [ + "codec/std", + "ethabi/std", + "ethbloom/std", + "ethereum-types/std", + "hex/std", + "parity-bytes/std", + "rlp/std", + "scale-info/std", + "serde", + "serde-big-array", + "sp-core/std", + "sp-io/std", + "sp-runtime/std", + "sp-std/std", +] diff --git a/bridges/snowbridge/parachain/primitives/ethereum/src/header.rs b/bridges/snowbridge/parachain/primitives/ethereum/src/header.rs new file mode 100644 index 000000000000..f0b51f8c79de --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/ethereum/src/header.rs @@ -0,0 +1,414 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use codec::{Decode, Encode}; +use ethbloom::Bloom as EthBloom; +use hex_literal::hex; +use parity_bytes::Bytes; +use rlp::RlpStream; +use scale_info::TypeInfo; +use sp_io::hashing::keccak_256; +use sp_runtime::RuntimeDebug; +use sp_std::{convert::TryInto, prelude::*}; + +#[cfg(feature = "std")] +use serde::{Deserialize, Serialize}; + +#[cfg(feature = "std")] +use serde_big_array::BigArray; + +use ethereum_types::{Address, H256, H64, U256}; + +use crate::{mpt, receipt}; + +/// Complete block header id. +#[derive(Clone, Copy, Default, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] +pub struct HeaderId { + /// Header number. + pub number: u64, + /// Header hash. + pub hash: H256, +} + +const EMPTY_OMMERS_HASH: [u8; 32] = + hex!("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"); + +/// An Ethereum block header. +#[derive(Clone, Default, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct Header { + /// Parent block hash. + pub parent_hash: H256, + /// Block timestamp. + pub timestamp: u64, + /// Block number. + pub number: u64, + /// Block author. + pub author: Address, + + /// Transactions root. + pub transactions_root: H256, + /// Block ommers hash. + pub ommers_hash: H256, + /// Block extra data. + pub extra_data: Bytes, + + /// State root. + pub state_root: H256, + /// Block receipts root. + pub receipts_root: H256, + /// Block bloom. + pub logs_bloom: Bloom, + /// Gas used for contracts execution. + pub gas_used: U256, + /// Block gas limit. + pub gas_limit: U256, + + /// Block difficulty. + pub difficulty: U256, + /// Vector of post-RLP-encoded fields. + pub seal: Vec, + + // Base fee per gas (EIP-1559), only in headers from the London hardfork onwards. + pub base_fee: Option, +} + +impl Header { + /// Compute hash of this header (keccak of the RLP with seal). + pub fn compute_hash(&self) -> H256 { + keccak_256(&self.rlp(true)).into() + } + + /// Compute hash of the truncated header i.e. excluding seal. + pub fn compute_partial_hash(&self) -> H256 { + keccak_256(&self.rlp(false)).into() + } + + pub fn check_receipt_proof( + &self, + proof: &[Vec], + ) -> Option> { + match self.apply_merkle_proof(proof) { + Some((root, data)) if root == self.receipts_root => Some(rlp::decode(&data)), + Some((_, _)) => None, + None => None, + } + } + + pub fn apply_merkle_proof(&self, proof: &[Vec]) -> Option<(H256, Vec)> { + let mut iter = proof.iter().rev(); + let first_bytes = match iter.next() { + Some(b) => b, + None => return None, + }; + let item_to_prove: mpt::ShortNode = rlp::decode(first_bytes).ok()?; + + let final_hash: Option<[u8; 32]> = iter.try_fold(keccak_256(first_bytes), |acc, x| { + let node: Box = x.as_slice().try_into().ok()?; + if (*node).contains_hash(acc.into()) { + return Some(keccak_256(x)) + } + None + }); + + final_hash.map(|hash| (hash.into(), item_to_prove.value)) + } + + pub fn mix_hash(&self) -> Option { + let bytes: Bytes = self.decoded_seal_field(0, 32)?; + let size = bytes.len(); + let mut mix_hash = [0u8; 32]; + for i in 0..size { + mix_hash[31 - i] = bytes[size - 1 - i]; + } + Some(mix_hash.into()) + } + + pub fn nonce(&self) -> Option { + let bytes: Bytes = self.decoded_seal_field(1, 8)?; + let size = bytes.len(); + let mut nonce = [0u8; 8]; + for i in 0..size { + nonce[7 - i] = bytes[size - 1 - i]; + } + Some(nonce.into()) + } + + pub fn has_ommers(&self) -> bool { + self.ommers_hash != EMPTY_OMMERS_HASH.into() + } + + fn decoded_seal_field(&self, index: usize, max_len: usize) -> Option { + let bytes: Bytes = rlp::decode(self.seal.get(index)?).ok()?; + if bytes.len() > max_len { + return None + } + Some(bytes) + } + + /// Returns header RLP with or without seals. + /// For EIP-1559 baseFee addition refer to: + /// + fn rlp(&self, with_seal: bool) -> Bytes { + let mut s = RlpStream::new(); + + let stream_length_without_seal = if self.base_fee.is_some() { 14 } else { 13 }; + + if with_seal { + s.begin_list(stream_length_without_seal + self.seal.len()); + } else { + s.begin_list(stream_length_without_seal); + } + + s.append(&self.parent_hash); + s.append(&self.ommers_hash); + s.append(&self.author); + s.append(&self.state_root); + s.append(&self.transactions_root); + s.append(&self.receipts_root); + s.append(&EthBloom::from(self.logs_bloom.0)); + s.append(&self.difficulty); + s.append(&self.number); + s.append(&self.gas_limit); + s.append(&self.gas_used); + s.append(&self.timestamp); + s.append(&self.extra_data); + + if with_seal { + for b in &self.seal { + s.append_raw(b, 1); + } + } + + if let Some(base_fee) = self.base_fee { + s.append(&base_fee); + } + + s.out().to_vec() + } +} + +/// Logs bloom. +#[derive(Clone, Debug, Encode, Decode, TypeInfo)] +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +pub struct Bloom(#[cfg_attr(feature = "std", serde(with = "BigArray"))] [u8; 256]); + +impl<'a> From<&'a [u8; 256]> for Bloom { + fn from(buffer: &'a [u8; 256]) -> Bloom { + Bloom(*buffer) + } +} + +impl PartialEq for Bloom { + fn eq(&self, other: &Bloom) -> bool { + self.0.iter().zip(other.0.iter()).all(|(l, r)| l == r) + } +} + +impl Default for Bloom { + fn default() -> Self { + Bloom([0; 256]) + } +} + +impl rlp::Decodable for Bloom { + fn decode(rlp: &rlp::Rlp) -> Result { + let v: Vec = rlp.as_val()?; + match v.len() { + 256 => { + let mut bytes = [0u8; 256]; + bytes.copy_from_slice(&v); + Ok(Self(bytes)) + }, + _ => Err(rlp::DecoderError::Custom("Expected 256 bytes")), + } + } +} + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn bloom_decode_rlp() { + let raw_bloom = hex!( + " + b901000420000000000000000000008002000000000001000000000001000000000000000000 + 0000000000000000000000000002000000080000000000000000200000000000000000000000 + 0000080000002200000000004000100000000000000000000000000000000000000000000000 + 0000000000000004000000001000010000000000080000000000400000000000000000000000 + 0000080000004000000000020000000000020000000000000000000000000000000000000000 + 0000040000000000020000000001000000000000000000000000000010000000020000200000 + 10200000000000010000000000000000000000000000000000000010000000 + " + ); + let expected_bytes = &raw_bloom[3..]; + let bloom: Bloom = rlp::decode(&raw_bloom).unwrap(); + assert_eq!(bloom.0, expected_bytes); + } + + #[test] + fn header_compute_hash_poa() { + // PoA header + let header = Header { + parent_hash: Default::default(), + timestamp: 0, + number: 0, + author: Default::default(), + transactions_root: hex!( + "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" + ) + .into(), + ommers_hash: hex!("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347") + .into(), + extra_data: vec![], + state_root: hex!("eccf6b74c2bcbe115c71116a23fe963c54406010c244d9650526028ad3e32cce") + .into(), + receipts_root: hex!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") + .into(), + logs_bloom: Default::default(), + gas_used: Default::default(), + gas_limit: 0x222222.into(), + difficulty: 0x20000.into(), + seal: vec![vec![0x80], { + let mut vec = vec![0xb8, 0x41]; + vec.resize(67, 0); + vec + }], + base_fee: None, + }; + assert_eq!( + header.compute_hash().as_bytes(), + hex!("9ff57c7fa155853586382022f0982b71c51fa313a0942f8c456300896643e890"), + ); + } + + #[test] + fn header_compute_hash_pow() { + // + let nonce = hex!("6935bbe7b63c4f8e").to_vec(); + let mix_hash = + hex!("be3adfb0087be62b28b716e2cdf3c79329df5caa04c9eee035d35b5d52102815").to_vec(); + let header = Header { + parent_hash: hex!("bede0bddd6f32c895fc505ffe0c39d9bde58e9a5272f31a3dee448b796edcbe3") + .into(), + timestamp: 1603160977, + number: 11090290, + author: hex!("ea674fdde714fd979de3edf0f56aa9716b898ec8").into(), + transactions_root: hex!( + "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" + ) + .into(), + ommers_hash: hex!("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347") + .into(), + extra_data: hex!("65746865726d696e652d61736961312d33").to_vec(), + state_root: hex!("7dcb8aca872b712bad81df34a89d4efedc293566ffc3eeeb5cbcafcc703e42c9") + .into(), + receipts_root: hex!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") + .into(), + logs_bloom: Default::default(), + gas_used: 0.into(), + gas_limit: 0xbe8c19.into(), + difficulty: 0xbc140caa61087i64.into(), + seal: vec![rlp::encode(&mix_hash).to_vec(), rlp::encode(&nonce).to_vec()], + base_fee: None, + }; + assert_eq!( + header.compute_hash().as_bytes(), + hex!("0f9bdc91c2e0140acb873330742bda8c8181fa3add91fe7ae046251679cedef7"), + ); + } + + #[test] + fn header_pow_seal_fields_extracted_correctly() { + let nonce: H64 = hex!("6935bbe7b63c4f8e").into(); + let mix_hash: H256 = + hex!("be3adfb0087be62b28b716e2cdf3c79329df5caa04c9eee035d35b5d52102815").into(); + let header = Header { + seal: vec![ + rlp::encode(&mix_hash.0.to_vec()).to_vec(), + rlp::encode(&nonce.0.to_vec()).to_vec(), + ], + ..Default::default() + }; + + assert_eq!(header.nonce().unwrap(), nonce); + assert_eq!(header.mix_hash().unwrap(), mix_hash); + } + + #[test] + fn header_pow_seal_fields_return_none_for_invalid_values() { + let nonce = hex!("696935bbe7b63c4f8e").to_vec(); + let mix_hash = + hex!("bebe3adfb0087be62b28b716e2cdf3c79329df5caa04c9eee035d35b5d52102815").to_vec(); + let mut header = Header { + seal: vec![rlp::encode(&mix_hash).to_vec(), rlp::encode(&nonce).to_vec()], + ..Default::default() + }; + assert_eq!(header.nonce(), None); + assert_eq!(header.mix_hash(), None); + + header.seal = Vec::new(); + assert_eq!(header.nonce(), None); + assert_eq!(header.mix_hash(), None); + } + + #[test] + fn header_check_receipt_proof() { + let header = Header { + receipts_root: hex!("fd5e397a84884641f53c496804f24b5276cbb8c5c9cfc2342246be8e3ce5ad02") + .into(), + ..Default::default() + }; + + // Valid proof + let proof_receipt5 = vec!( + hex!("f90131a0b5ba404eb5a6a88e56579f4d37ef9813b5ad7f86f0823ff3b407ac5a6bb465eca0398ead2655e78e03c127ce22c5830e90f18b1601ec055f938336c084feb915a9a026d322c26e46c50942c1aabde50e36df5cde572aed650ce73ea3182c6e90a02ca00600a356135f4db1db0d9842264cdff2652676f881669e91e316c0b6dd783011a0837f1deb4075336da320388c1edfffc56c448a43f4a5ba031300d32a7b509fc5a01c3ac82fd65b4aba7f9afaf604d9c82ec7e2deb573a091ae235751bc5c0c288da05d454159d9071b0f68b6e0503d290f23ac7602c1db0c569dee4605d8f5298f09a00bbed10350ec954448df795f6fd46e3faefc800ede061b3840eedc6e2b07a74da0acb02d26a3650f2064c14a435fdf1f668d8655daf455ebdf671713a7c089b3898080808080808080").to_vec(), + hex!("f901f180a00046a08d4f0bdbdc6b31903086ce323182bce6725e7d9415f7ff91ee8f4820bda0e7cd26ad5f3d2771e4b5ab788e268a14a10209f94ee918eb6c829d21d3d11c1da00d4a56d9e9a6751874fd86c7e3cb1c6ad5a848da62751325f478978a00ea966ea064b81920c8f04a8a1e21f53a8280e739fbb7b00b2ab92493ca3f610b70e8ac85a0b1040ed4c55a73178b76abb16f946ce5bebd6b93ab873c83327df54047d12c27a0de6485e9ac58dc6e2b04b4bb38f562684f0b1a2ee586cc11079e7d9a9dc40b32a0d394f4d3532c3124a65fa36e69147e04fd20453a72ee9c50660f17e13ce9df48a066501003fc3e3478efd2803cd0eded6bbe9243ca01ba754d6327071ddbcbc649a0b2684e518f325fee39fc8ea81b68f3f5c785be00d087f3bed8857ae2ee8da26ea071060a5c52042e8d7ce21092f8ecf06053beb9a0b773a6f91a30c4220aa276b2a0fc22436632574ccf6043d0986dede27ea94c9ca9a3bb5ec03ce776a4ddef24a9a05a8a1d6698c4e7d8cc3a2506cb9b12ea9a079c9c7099bc919dc804033cc556e4a0170c468b0716fd36d161f0bf05875f15756a2976de92f9efe7716320509d79c9a0182f909a90cab169f3efb62387f9cccdd61440acc4deec42f68a4f7ca58075c7a055cf0e9202ac75689b76318f1171f3a44465eddc06aae0713bfb6b34fdd27b7980").to_vec(), + hex!("f904de20b904daf904d701830652f0b9010004200000000000000000000080020000000000010000000000010000000000000000000000000000000000000000000002000000080000000000000000200000000000000000000000000008000000220000000000400010000000000000000000000000000000000000000000000000000000000000040000000010000100000000000800000000004000000000000000000000000000080000004000000000020000000000020000000000000000000000000000000000000000000004000000000002000000000100000000000000000000000000001000000002000020000010200000000000010000000000000000000000000000000000000010000000f903ccf89b9421130f34829b4c343142047a28ce96ec07814b15f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa00000000000000000000000007d843005c7433c16b27ff939cb37471541561ebda0000000000000000000000000e9c1281aae66801fa35ec404d5f2aea393ff6988a000000000000000000000000000000000000000000000000000000005d09b7380f89b9421130f34829b4c343142047a28ce96ec07814b15f863a08c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925a00000000000000000000000007d843005c7433c16b27ff939cb37471541561ebda00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da0ffffffffffffffffffffffffffffffffffffffffffffffffffffffcc840c6920f89b94c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000e9c1281aae66801fa35ec404d5f2aea393ff6988a00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da000000000000000000000000000000000000000000000000003e973b5a5d1078ef87994e9c1281aae66801fa35ec404d5f2aea393ff6988e1a01c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1b840000000000000000000000000000000000000000000000000000001f1420ad1d40000000000000000000000000000000000000000000000014ad400879d159a38f8fc94e9c1281aae66801fa35ec404d5f2aea393ff6988f863a0d78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822a00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488db88000000000000000000000000000000000000000000000000000000005d415f3320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e973b5a5d1078ef87a94c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2f842a07fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65a00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da000000000000000000000000000000000000000000000000003e973b5a5d1078e").to_vec(), + ); + assert!(header.check_receipt_proof(&proof_receipt5).is_some()); + + // Various invalid proofs + let proof_empty: Vec> = vec![]; + let proof_missing_full_node = vec![proof_receipt5[0].clone(), proof_receipt5[2].clone()]; + let proof_missing_short_node1 = vec![proof_receipt5[0].clone(), proof_receipt5[1].clone()]; + let proof_missing_short_node2 = vec![proof_receipt5[0].clone()]; + let proof_invalid_encoding = vec![proof_receipt5[2][2..].to_vec()]; + let proof_no_full_node = vec![proof_receipt5[2].clone(), proof_receipt5[2].clone()]; + assert!(header.check_receipt_proof(&proof_empty).is_none()); + assert!(header.check_receipt_proof(&proof_missing_full_node).is_none()); + + assert_eq!( + header.check_receipt_proof(&proof_missing_short_node1), + Some(Err(rlp::DecoderError::Custom("Unsupported receipt type"))) + ); + + assert_eq!( + header.check_receipt_proof(&proof_missing_short_node2), + Some(Err(rlp::DecoderError::Custom("Unsupported receipt type"))) + ); + + assert!(header.check_receipt_proof(&proof_invalid_encoding).is_none()); + assert!(header.check_receipt_proof(&proof_no_full_node).is_none()); + } + + #[test] + fn header_check_receipt_proof_with_intermediate_short_node() { + let header = Header { + receipts_root: hex!("d128e3a57142d2bf15bc0cbcac7ad54f40750d571b5c3097e425882c10c9ba66") + .into(), + ..Default::default() + }; + + let proof_receipt263 = vec![ + hex!("f90131a00d3cb8d3f57ac1c0e12918a2ebe0cafed8c273577b9dd73e7ed1079b403ef494a0678b9835b834f8a287c0dd33a8fca9146e456ca688555ed4ec1361a2180b778da0fe42da181a46677a043b3d9d4b8bb05a6a17b7b5c010c17e7c1d31cfb7c4f911a0c89f0e2c53241cdb578e1f2b4caf6ba36e00500bdc57fecd66b84a6a58394c19a086c3c1fae5a0575940b5d38e111c469d07883106c26856f3ef608469a2081f13a06c5992ff00aab6226a70a032fd2f571ba22f797321f45e2daa73020d638d21b0a050861e9503ef68728f6c90a44f7fe1bceb2a9bdab6957bbe7136166bd849561ea006aa6eaca8a07e57176e9aa41e6a09edfb7678d1a112404e0ec779d7e567e82ea0bb0b430d303ba21b0af11c487b8a218bd75db54c98940b3f11bad8ff47cad3ef8080808080808080").to_vec(), + hex!("f871a0246de222036ee6a03329b0105da0a6b3f916fc95a9ed5a403a581a0c4d74242ca0ac108a49a88b57a05ac34a108b39f1e45f6f167f2b9fbc8d52fb58e2e5a6af1ea0fcfe07ac2ccd3c28b6eab68d1bce112f6f6dbd9023e4ec3c05b96615aa803d798080808080808080808080808080").to_vec(), + hex!("e4820001a04fff54398cad4d05ea6abfd8b0f3b4fe14c04d7ff5f5211c5b927d9cf72ac1d8").to_vec(), + hex!("f851a096d010643ca2d47412ca66898286b5f2412963b9ec051b33e570d575914c9c5ca028cd24c652989542fe89479ec6388eac4592432242af5ba97563b3ac7c71c019808080808080808080808080808080").to_vec(), + hex!("f90211a0bb35a84c5b1dcb78ec9d32614912c696e62df77bebf9ab326ee55b5d3acdde46a01084b30dac8df0accfcd0fd6330b7f6fc72a4651246d0694be9162151686a620a03eed50afdce7909d784c6157c445a444c806b5f23d31f3b63786f600c84a95b2a0af5232f1df6c6d41879804d081abe867002abe26ba3e5f8e0254a83a54769831a0607915fb13dd5da594256389a45007a67a7f7a86e95d38d8462792b6c98a722ea00e1260fda1730f2738c650ce2bfba83857bc10f8fb119ebc4fb39acba24e6fbaa0d11de17e417327457812675ca3b84ae8e1b64827abfe01420953697c8313d5b1a05fcaf2f7a88f76336a0c32ffc78acb87ae2005454bd25d658035331be3173b46a03f94f4952ab9e650f83cfd0e7f367b1bcc493aacf39a06f16c4a2e1b5605da48a0bdb4ec79785ca8ae22d60f1bbd42d707b4d7ec4aff231a3ebab755e315b35053a043a67c3f2bcef37c8f47a673adcb7061007a553696d1092408601c11b2e6846aa0c519d5af48cae87c7f4538845417c9735813bee892a6fe2dda79f5c414e8576aa0f7058256e09589501d7c231d739e61c84a850e139690989d24fda6058b432e98a081a52faab520978cb19ce14400dba0cd5bcdc4e5a3c0740678aa8f97ee0e5c56a0bcecc61cadeae52518e3b68a48af4b11603dfd9d99d99d7985efa6d2de44f904a02cba4accfc6f39bc5adb6d4440eb6358b4a5103ef93298e4e694f1f940f8b48280").to_vec(), + hex!("f901ae20b901aaf901a70183bb444eb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000001000000000000000000000000000100000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000010000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000080000000000000000000000000000000000000000000000002000000000000000000081000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000f89df89b94dac17f958d2ee523a2206206994597c13d831ec7f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa00000000000000000000000002e514404ff6823f1b46a8318a709251db414e5e1a000000000000000000000000055021c55847c00d764357a352e5803237d328954a0000000000000000000000000000000000000000000000000000000000201c370").to_vec(), + ]; + assert!(header.check_receipt_proof(&proof_receipt263).is_some()); + } +} diff --git a/bridges/snowbridge/parachain/primitives/ethereum/src/lib.rs b/bridges/snowbridge/parachain/primitives/ethereum/src/lib.rs new file mode 100644 index 000000000000..1a10ea9abb77 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/ethereum/src/lib.rs @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +#![cfg_attr(not(feature = "std"), no_std)] + +pub mod header; +pub mod log; +pub mod mpt; +pub mod receipt; + +pub use ethereum_types::{Address, H160, H256, H64, U256}; + +pub use header::{Bloom, Header, HeaderId}; +pub use log::Log; +pub use receipt::Receipt; + +#[derive(Debug)] +pub enum DecodeError { + // Unexpected RLP data + InvalidRLP(rlp::DecoderError), + // Data does not match expected ABI + InvalidABI(ethabi::Error), + // Invalid message payload + InvalidPayload, +} + +impl From for DecodeError { + fn from(err: rlp::DecoderError) -> Self { + DecodeError::InvalidRLP(err) + } +} + +impl From for DecodeError { + fn from(err: ethabi::Error) -> Self { + DecodeError::InvalidABI(err) + } +} diff --git a/bridges/snowbridge/parachain/primitives/ethereum/src/log.rs b/bridges/snowbridge/parachain/primitives/ethereum/src/log.rs new file mode 100644 index 000000000000..7b8e35bb1133 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/ethereum/src/log.rs @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use codec::{Decode, Encode}; +use ethereum_types::{H160, H256}; +use sp_std::prelude::*; + +#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq)] +pub struct Log { + pub address: H160, + pub topics: Vec, + pub data: Vec, +} + +impl rlp::Decodable for Log { + /// We need to implement rlp::Decodable manually as the derive macro RlpDecodable + /// didn't seem to generate the correct code for parsing our logs. + fn decode(rlp: &rlp::Rlp) -> Result { + let mut iter = rlp.iter(); + + let address: H160 = match iter.next() { + Some(data) => data.as_val()?, + None => return Err(rlp::DecoderError::Custom("Expected log address")), + }; + + let topics: Vec = match iter.next() { + Some(data) => data.as_list()?, + None => return Err(rlp::DecoderError::Custom("Expected log topics")), + }; + + let data: Vec = match iter.next() { + Some(data) => data.data()?.to_vec(), + None => return Err(rlp::DecoderError::Custom("Expected log data")), + }; + + Ok(Self { address, topics, data }) + } +} + +#[cfg(test)] +mod tests { + + use super::Log; + use hex_literal::hex; + + const RAW_LOG: [u8; 605] = hex!( + " + f9025a941cfd66659d44cfe2e627c5742ba7477a3284cffae1a0266413be5700ce8dd5ac6b9a7dfb + abe99b3e45cae9a68ac2757858710b401a38b9022000000000000000000000000000000000000000 + 00000000000000000000000060000000000000000000000000000000000000000000000000000000 + 00000000c00000000000000000000000000000000000000000000000000000000000000100000000 + 00000000000000000000000000000000000000000000000000000000283163466436363635394434 + 34636665324536323763353734324261373437376133323834634666410000000000000000000000 + 00000000000000000000000000000000000000000000000000000000000000000000000000000000 + 000000000773656e6445544800000000000000000000000000000000000000000000000000000000 + 00000000000000000000000000000000000000000000000000000001000000000000000000000000 + 00cffeaaf7681c89285d65cfbe808b80e50269657300000000000000000000000000000000000000 + 000000000000000000000000a0000000000000000000000000000000000000000000000000000000 + 0000000000000000000000000000000000000000000000000000000000000000000000000a000000 + 00000000000000000000000000000000000000000000000000000000020000000000000000000000 + 00000000000000000000000000000000000000002f3146524d4d3850456957585961783772705336 + 5834585a5831614141785357783143724b5479725659685632346667000000000000000000000000 + 0000000000 + " + ); + + #[test] + fn decode_log() { + let log: Log = rlp::decode(&RAW_LOG).unwrap(); + assert_eq!(log.address.as_bytes(), hex!["1cfd66659d44cfe2e627c5742ba7477a3284cffa"]); + assert_eq!( + log.topics[0].as_bytes(), + hex!["266413be5700ce8dd5ac6b9a7dfbabe99b3e45cae9a68ac2757858710b401a38"] + ); + } +} diff --git a/bridges/snowbridge/parachain/primitives/ethereum/src/mpt.rs b/bridges/snowbridge/parachain/primitives/ethereum/src/mpt.rs new file mode 100644 index 000000000000..9a2dae486dcc --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/ethereum/src/mpt.rs @@ -0,0 +1,142 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! Helper types to work with Ethereum's Merkle Patricia Trie nodes + +use ethereum_types::H256; +use sp_std::{convert::TryFrom, prelude::*}; + +pub trait Node { + fn contains_hash(&self, hash: H256) -> bool; +} + +impl TryFrom<&[u8]> for Box { + type Error = rlp::DecoderError; + + fn try_from(bytes: &[u8]) -> Result, Self::Error> { + let rlp = rlp::Rlp::new(bytes); + match rlp.item_count()? { + 2 => { + let node: ShortNode = rlp.as_val()?; + Ok(Box::new(node)) + }, + 17 => { + let node: FullNode = rlp.as_val()?; + Ok(Box::new(node)) + }, + _ => Err(rlp::DecoderError::Custom("Invalid number of list elements")), + } + } +} + +/// Intermediate trie node with children (refers to node with same name in Geth). +/// This struct only handles the proof representation, i.e. a child is either empty +/// or a 32-byte hash of its subtree. +pub struct FullNode { + pub children: Vec>, +} + +impl rlp::Decodable for FullNode { + fn decode(rlp: &rlp::Rlp) -> Result { + let children: Vec> = rlp + .iter() + .map(|item| { + let v: Vec = item.as_val()?; + match v.len() { + 0 => Ok(None), + 32 => { + let mut bytes = [0u8; 32]; + bytes.copy_from_slice(&v); + Ok(Some(bytes.into())) + }, + _ => Err(rlp::DecoderError::Custom("Expected 32-byte hash or empty child")), + } + }) + .collect::>()?; + + Ok(Self { children }) + } +} + +impl Node for FullNode { + fn contains_hash(&self, hash: H256) -> bool { + self.children.iter().any(|h| Some(hash) == *h) + } +} + +/// Trie node where `value` is either the RLP-encoded item we're +/// proving or an intermediate hash (refers to node with same name in Geth) +/// Proof verification should return `value`. `key` is an implementation +/// detail of the trie. +pub struct ShortNode { + pub key: Vec, + pub value: Vec, +} + +impl rlp::Decodable for ShortNode { + fn decode(rlp: &rlp::Rlp) -> Result { + let mut iter = rlp.iter(); + + let key: Vec = match iter.next() { + Some(data) => data.as_val()?, + None => return Err(rlp::DecoderError::Custom("Expected key bytes")), + }; + + let value: Vec = match iter.next() { + Some(data) => data.as_val()?, + None => return Err(rlp::DecoderError::Custom("Expected value bytes")), + }; + + Ok(Self { key, value }) + } +} + +impl Node for ShortNode { + fn contains_hash(&self, hash: H256) -> bool { + self.value == hash.0 + } +} + +#[cfg(test)] +mod tests { + + use super::*; + use hex_literal::hex; + + const RAW_PROOF: [&[u8]; 3] = [ + &hex!("f90131a0b5ba404eb5a6a88e56579f4d37ef9813b5ad7f86f0823ff3b407ac5a6bb465eca0398ead2655e78e03c127ce22c5830e90f18b1601ec055f938336c084feb915a9a026d322c26e46c50942c1aabde50e36df5cde572aed650ce73ea3182c6e90a02ca00600a356135f4db1db0d9842264cdff2652676f881669e91e316c0b6dd783011a0837f1deb4075336da320388c1edfffc56c448a43f4a5ba031300d32a7b509fc5a01c3ac82fd65b4aba7f9afaf604d9c82ec7e2deb573a091ae235751bc5c0c288da05d454159d9071b0f68b6e0503d290f23ac7602c1db0c569dee4605d8f5298f09a00bbed10350ec954448df795f6fd46e3faefc800ede061b3840eedc6e2b07a74da0acb02d26a3650f2064c14a435fdf1f668d8655daf455ebdf671713a7c089b3898080808080808080"), + &hex!("f901f180a00046a08d4f0bdbdc6b31903086ce323182bce6725e7d9415f7ff91ee8f4820bda0e7cd26ad5f3d2771e4b5ab788e268a14a10209f94ee918eb6c829d21d3d11c1da00d4a56d9e9a6751874fd86c7e3cb1c6ad5a848da62751325f478978a00ea966ea064b81920c8f04a8a1e21f53a8280e739fbb7b00b2ab92493ca3f610b70e8ac85a0b1040ed4c55a73178b76abb16f946ce5bebd6b93ab873c83327df54047d12c27a0de6485e9ac58dc6e2b04b4bb38f562684f0b1a2ee586cc11079e7d9a9dc40b32a0d394f4d3532c3124a65fa36e69147e04fd20453a72ee9c50660f17e13ce9df48a066501003fc3e3478efd2803cd0eded6bbe9243ca01ba754d6327071ddbcbc649a0b2684e518f325fee39fc8ea81b68f3f5c785be00d087f3bed8857ae2ee8da26ea071060a5c52042e8d7ce21092f8ecf06053beb9a0b773a6f91a30c4220aa276b2a0fc22436632574ccf6043d0986dede27ea94c9ca9a3bb5ec03ce776a4ddef24a9a05a8a1d6698c4e7d8cc3a2506cb9b12ea9a079c9c7099bc919dc804033cc556e4a0170c468b0716fd36d161f0bf05875f15756a2976de92f9efe7716320509d79c9a0182f909a90cab169f3efb62387f9cccdd61440acc4deec42f68a4f7ca58075c7a055cf0e9202ac75689b76318f1171f3a44465eddc06aae0713bfb6b34fdd27b7980"), + &hex!("f904de20b904daf904d701830652f0b9010004200000000000000000000080020000000000010000000000010000000000000000000000000000000000000000000002000000080000000000000000200000000000000000000000000008000000220000000000400010000000000000000000000000000000000000000000000000000000000000040000000010000100000000000800000000004000000000000000000000000000080000004000000000020000000000020000000000000000000000000000000000000000000004000000000002000000000100000000000000000000000000001000000002000020000010200000000000010000000000000000000000000000000000000010000000f903ccf89b9421130f34829b4c343142047a28ce96ec07814b15f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa00000000000000000000000007d843005c7433c16b27ff939cb37471541561ebda0000000000000000000000000e9c1281aae66801fa35ec404d5f2aea393ff6988a000000000000000000000000000000000000000000000000000000005d09b7380f89b9421130f34829b4c343142047a28ce96ec07814b15f863a08c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925a00000000000000000000000007d843005c7433c16b27ff939cb37471541561ebda00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da0ffffffffffffffffffffffffffffffffffffffffffffffffffffffcc840c6920f89b94c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000e9c1281aae66801fa35ec404d5f2aea393ff6988a00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da000000000000000000000000000000000000000000000000003e973b5a5d1078ef87994e9c1281aae66801fa35ec404d5f2aea393ff6988e1a01c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1b840000000000000000000000000000000000000000000000000000001f1420ad1d40000000000000000000000000000000000000000000000014ad400879d159a38f8fc94e9c1281aae66801fa35ec404d5f2aea393ff6988f863a0d78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822a00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488db88000000000000000000000000000000000000000000000000000000005d415f3320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e973b5a5d1078ef87a94c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2f842a07fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65a00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da000000000000000000000000000000000000000000000000003e973b5a5d1078e"), + ]; + + #[test] + fn decode_full_node() { + let node1: FullNode = rlp::decode(RAW_PROOF[0]).unwrap(); + let node2: FullNode = rlp::decode(RAW_PROOF[1]).unwrap(); + assert_eq!(node1.children.len(), 17); + assert_eq!(node2.children.len(), 17); + assert_eq!(node1.children.iter().filter(|c| c.is_none()).count(), 8); + assert_eq!(node2.children.iter().filter(|c| c.is_none()).count(), 2); + + let result: Result = rlp::decode(RAW_PROOF[2]); + assert!(result.is_err()); + } + + #[test] + fn decode_short_node() { + // key + item value + let node: ShortNode = rlp::decode(RAW_PROOF[2]).unwrap(); + assert_eq!(node.key, vec![32]); + assert!(!node.value.is_empty()); + + // key + item hash + let node: ShortNode = rlp::decode(&hex!( + "e4820001a04fff54398cad4d05ea6abfd8b0f3b4fe14c04d7ff5f5211c5b927d9cf72ac1d8" + )) + .unwrap(); + assert_eq!(node.key, vec![0, 1]); + assert_eq!( + node.value, + hex!("4fff54398cad4d05ea6abfd8b0f3b4fe14c04d7ff5f5211c5b927d9cf72ac1d8").to_vec() + ); + } +} diff --git a/bridges/snowbridge/parachain/primitives/ethereum/src/receipt.rs b/bridges/snowbridge/parachain/primitives/ethereum/src/receipt.rs new file mode 100644 index 000000000000..665a93dbb1e2 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/ethereum/src/receipt.rs @@ -0,0 +1,139 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +use crate::{Bloom, Log}; +use codec::{Decode, Encode}; +use sp_runtime::RuntimeDebug; +use sp_std::prelude::*; + +#[derive(Clone, Default, Encode, Decode, PartialEq, RuntimeDebug)] +pub struct Receipt { + pub post_state_or_status: Vec, + pub cumulative_gas_used: u64, + pub bloom: Bloom, + pub logs: Vec, +} + +impl Receipt { + pub fn contains_log(&self, log: &Log) -> bool { + self.logs.iter().any(|l| l == log) + } + + fn decode_list(rlp: &rlp::Rlp) -> Result { + let mut iter = rlp.iter(); + + let post_state_or_status: Vec = match iter.next() { + Some(data) => data.as_val()?, + None => return Err(rlp::DecoderError::Custom("Expected receipt post state or status")), + }; + + let cumulative_gas_used: u64 = match iter.next() { + Some(data) => data.as_val()?, + None => return Err(rlp::DecoderError::Custom("Expected receipt cumulative gas used")), + }; + + let bloom: Bloom = match iter.next() { + Some(data) => data.as_val()?, + None => return Err(rlp::DecoderError::Custom("Expected receipt bloom")), + }; + + let logs: Vec = match iter.next() { + Some(data) => data.as_list()?, + None => return Err(rlp::DecoderError::Custom("Expected receipt logs")), + }; + + Ok(Self { post_state_or_status, cumulative_gas_used, bloom, logs }) + } +} + +impl rlp::Decodable for Receipt { + fn decode(rlp: &rlp::Rlp) -> Result { + if rlp.is_data() { + // Typed receipt + let data = rlp.as_raw(); + match data[0] { + // 1 = EIP-2930, 2 = EIP-1559 + 1 | 2 => { + let receipt_rlp = &rlp::Rlp::new(&data[1..]); + if !receipt_rlp.is_list() { + return Err(rlp::DecoderError::RlpExpectedToBeList) + } + Self::decode_list(&rlp::Rlp::new(&data[1..])) + }, + _ => Err(rlp::DecoderError::Custom("Unsupported receipt type")), + } + } else if rlp.is_list() { + // Legacy receipt + Self::decode_list(rlp) + } else { + Err(rlp::DecoderError::RlpExpectedToBeList) + } + } +} + +#[cfg(test)] +mod tests { + + use super::Receipt; + use hex_literal::hex; + + const RAW_RECEIPT: [u8; 1242] = hex!( + " + f904d701830652f0b901000420000000000000000000008002000000000001000000000001000000 + 00000000000000000000000000000000000000020000000800000000000000002000000000000000 + 00000000000008000000220000000000400010000000000000000000000000000000000000000000 + 00000000000000000004000000001000010000000000080000000000400000000000000000000000 + 00000800000040000000000200000000000200000000000000000000000000000000000000000000 + 04000000000002000000000100000000000000000000000000001000000002000020000010200000 + 000000010000000000000000000000000000000000000010000000f903ccf89b9421130f34829b4c + 343142047a28ce96ec07814b15f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a116 + 28f55a4df523b3efa00000000000000000000000007d843005c7433c16b27ff939cb37471541561e + bda0000000000000000000000000e9c1281aae66801fa35ec404d5f2aea393ff6988a00000000000 + 0000000000000000000000000000000000000000000005d09b7380f89b9421130f34829b4c343142 + 047a28ce96ec07814b15f863a08c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200a + c8c7c3b925a00000000000000000000000007d843005c7433c16b27ff939cb37471541561ebda000 + 00000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da0ffffffffffffffff + ffffffffffffffffffffffffffffffffffffffcc840c6920f89b94c02aaa39b223fe8d0a0e5c4f27 + ead9083c756cc2f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523 + b3efa0000000000000000000000000e9c1281aae66801fa35ec404d5f2aea393ff6988a000000000 + 00000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da00000000000000000000000 + 0000000000000000000000000003e973b5a5d1078ef87994e9c1281aae66801fa35ec404d5f2aea3 + 93ff6988e1a01c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1b840 + 000000000000000000000000000000000000000000000000000001f1420ad1d40000000000000000 + 000000000000000000000000000000014ad400879d159a38f8fc94e9c1281aae66801fa35ec404d5 + f2aea393ff6988f863a0d78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159 + d822a00000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488da000000000 + 00000000000000007a250d5630b4cf539739df2c5dacb4c659f2488db88000000000000000000000 + 000000000000000000000000000000000005d415f332000000000000000000000000000000000000 + 00000000000000000000000000000000000000000000000000000000000000000000000000000000 + 00000000000000000000000000000000000000000000000000000000000003e973b5a5d1078ef87a + 94c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2f842a07fcf532c15f0a6db0bd6d0e038bea71d + 30d808c7d98cb3bf7268a95bf5081b65a00000000000000000000000007a250d5630b4cf539739df + 2c5dacb4c659f2488da000000000000000000000000000000000000000000000000003e973b5a5d1 + 078e + " + ); + + #[test] + fn decode_legacy_receipt() { + let receipt: Receipt = rlp::decode(&RAW_RECEIPT).unwrap(); + assert_eq!(receipt.post_state_or_status, vec!(1)); + assert_eq!(receipt.cumulative_gas_used, 414448); + assert_eq!( + receipt.bloom, + (&hex!( + " + 042000000000000000000000800200000000000100000000000100000000000000000000 + 000000000000000000000000020000000800000000000000002000000000000000000000 + 000000080000002200000000004000100000000000000000000000000000000000000000 + 000000000000000000000400000000100001000000000008000000000040000000000000 + 000000000000000800000040000000000200000000000200000000000000000000000000 + 000000000000000000040000000000020000000001000000000000000000000000000010 + 000000020000200000102000000000000100000000000000000000000000000000000000 + 10000000 + " + )) + .into(), + ); + assert_eq!(receipt.logs.len(), 6); + } +} diff --git a/bridges/snowbridge/parachain/primitives/router/Cargo.toml b/bridges/snowbridge/parachain/primitives/router/Cargo.toml new file mode 100644 index 000000000000..7badfebb6068 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/router/Cargo.toml @@ -0,0 +1,61 @@ +[package] +name = "snowbridge-router-primitives" +description = "Snowbridge Router Primitives" +version = "0.1.1" +authors = ["Snowfork "] +edition = "2021" +license = "Apache-2.0" + +[dependencies] +serde = { version = "1.0.188", optional = true, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false } +scale-info = { version = "2.9.0", default-features = false, features = ["derive"] } +log = { version = "0.4.20", default-features = false } + +frame-support = { path = "../../../../../substrate/frame/support", default-features = false } +frame-system = { path = "../../../../../substrate/frame/system", default-features = false } +sp-core = { path = "../../../../../substrate/primitives/core", default-features = false } +sp-io = { path = "../../../../../substrate/primitives/io", default-features = false } +sp-runtime = { path = "../../../../../substrate/primitives/runtime", default-features = false } +sp-std = { path = "../../../../../substrate/primitives/std", default-features = false } + +xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false } +xcm-builder = { package = "staging-xcm-builder", path = "../../../../../polkadot/xcm/xcm-builder", default-features = false } +xcm-executor = { package = "staging-xcm-executor", path = "../../../../../polkadot/xcm/xcm-executor", default-features = false } + +snowbridge-core = { path = "../../primitives/core", default-features = false } + +ethabi = { git = "https://github.com/Snowfork/ethabi-decode.git", package = "ethabi-decode", branch = "master", default-features = false } + +hex-literal = { version = "0.4.1" } + +[dev-dependencies] +hex = { package = "rustc-hex", version = "2.1.0" } + +[features] +default = ["std"] +std = [ + "codec/std", + "ethabi/std", + "frame-support/std", + "frame-system/std", + "log/std", + "scale-info/std", + "serde", + "snowbridge-core/std", + "sp-core/std", + "sp-io/std", + "sp-runtime/std", + "sp-std/std", + "xcm-builder/std", + "xcm-executor/std", + "xcm/std", +] +runtime-benchmarks = [ + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "snowbridge-core/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", + "xcm-builder/runtime-benchmarks", + "xcm-executor/runtime-benchmarks", +] diff --git a/bridges/snowbridge/parachain/primitives/router/src/inbound/mod.rs b/bridges/snowbridge/parachain/primitives/router/src/inbound/mod.rs new file mode 100644 index 000000000000..a07e0eae5d73 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/router/src/inbound/mod.rs @@ -0,0 +1,320 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! Converts messages from Ethereum to XCM messages + +#[cfg(test)] +mod tests; + +use codec::{Decode, Encode}; +use core::marker::PhantomData; +use frame_support::{traits::tokens::Balance as BalanceT, weights::Weight, PalletError}; +use scale_info::TypeInfo; +use sp_core::{Get, RuntimeDebug, H160}; +use sp_io::hashing::blake2_256; +use sp_runtime::MultiAddress; +use sp_std::prelude::*; +use xcm::prelude::{Junction::AccountKey20, *}; +use xcm_executor::traits::ConvertLocation; + +const MINIMUM_DEPOSIT: u128 = 1; + +/// Messages from Ethereum are versioned. This is because in future, +/// we may want to evolve the protocol so that the ethereum side sends XCM messages directly. +/// Instead having BridgeHub transcode the messages into XCM. +#[derive(Clone, Encode, Decode, RuntimeDebug)] +pub enum VersionedMessage { + V1(MessageV1), +} + +/// For V1, the ethereum side sends messages which are transcoded into XCM. These messages are +/// self-contained, in that they can be transcoded using only information in the message. +#[derive(Clone, Encode, Decode, RuntimeDebug)] +pub struct MessageV1 { + /// EIP-155 chain id of the origin Ethereum network + pub chain_id: u64, + /// The command originating from the Gateway contract + pub command: Command, +} + +#[derive(Clone, Encode, Decode, RuntimeDebug)] +pub enum Command { + /// Register a wrapped token on the AssetHub `ForeignAssets` pallet + RegisterToken { + /// The address of the ERC20 token to be bridged over to AssetHub + token: H160, + /// XCM execution fee on AssetHub + fee: u128, + }, + /// Send a token to AssetHub or another parachain + SendToken { + /// The address of the ERC20 token to be bridged over to AssetHub + token: H160, + /// The destination for the transfer + destination: Destination, + /// Amount to transfer + amount: u128, + /// XCM execution fee on AssetHub + fee: u128, + }, +} + +/// Destination for bridged tokens +#[derive(Clone, Encode, Decode, RuntimeDebug)] +pub enum Destination { + /// The funds will be deposited into account `id` on AssetHub + AccountId32 { id: [u8; 32] }, + /// The funds will deposited into the sovereign account of destination parachain `para_id` on + /// AssetHub, Account `id` on the destination parachain will receive the funds via a + /// reserve-backed transfer. See + ForeignAccountId32 { + para_id: u32, + id: [u8; 32], + /// XCM execution fee on final destination + fee: u128, + }, + /// The funds will deposited into the sovereign account of destination parachain `para_id` on + /// AssetHub, Account `id` on the destination parachain will receive the funds via a + /// reserve-backed transfer. See + ForeignAccountId20 { + para_id: u32, + id: [u8; 20], + /// XCM execution fee on final destination + fee: u128, + }, +} + +pub struct MessageToXcm< + CreateAssetCall, + CreateAssetDeposit, + InboundQueuePalletInstance, + AccountId, + Balance, +> where + CreateAssetCall: Get, + CreateAssetDeposit: Get, + Balance: BalanceT, +{ + _phantom: PhantomData<( + CreateAssetCall, + CreateAssetDeposit, + InboundQueuePalletInstance, + AccountId, + Balance, + )>, +} + +/// Reason why a message conversion failed. +#[derive(Copy, Clone, TypeInfo, PalletError, Encode, Decode, RuntimeDebug)] +pub enum ConvertMessageError { + /// The message version is not supported for conversion. + UnsupportedVersion, +} + +/// convert the inbound message to xcm which will be forwarded to the destination chain +pub trait ConvertMessage { + type Balance: BalanceT + From; + type AccountId; + /// Converts a versioned message into an XCM message and an optional topicID + fn convert(message: VersionedMessage) -> Result<(Xcm<()>, Self::Balance), ConvertMessageError>; +} + +pub type CallIndex = [u8; 2]; + +impl + ConvertMessage + for MessageToXcm< + CreateAssetCall, + CreateAssetDeposit, + InboundQueuePalletInstance, + AccountId, + Balance, + > where + CreateAssetCall: Get, + CreateAssetDeposit: Get, + InboundQueuePalletInstance: Get, + Balance: BalanceT + From, + AccountId: Into<[u8; 32]>, +{ + type Balance = Balance; + type AccountId = AccountId; + + fn convert(message: VersionedMessage) -> Result<(Xcm<()>, Self::Balance), ConvertMessageError> { + use Command::*; + use VersionedMessage::*; + match message { + V1(MessageV1 { chain_id, command: RegisterToken { token, fee } }) => + Ok(Self::convert_register_token(chain_id, token, fee)), + V1(MessageV1 { chain_id, command: SendToken { token, destination, amount, fee } }) => + Ok(Self::convert_send_token(chain_id, token, destination, amount, fee)), + } + } +} + +impl + MessageToXcm +where + CreateAssetCall: Get, + CreateAssetDeposit: Get, + InboundQueuePalletInstance: Get, + Balance: BalanceT + From, + AccountId: Into<[u8; 32]>, +{ + fn convert_register_token(chain_id: u64, token: H160, fee: u128) -> (Xcm<()>, Balance) { + let network = Ethereum { chain_id }; + let xcm_fee: MultiAsset = (MultiLocation::parent(), fee).into(); + let deposit: MultiAsset = (MultiLocation::parent(), CreateAssetDeposit::get()).into(); + + let total_amount = fee + CreateAssetDeposit::get(); + let total: MultiAsset = (MultiLocation::parent(), total_amount).into(); + + let bridge_location: MultiLocation = (Parent, Parent, GlobalConsensus(network)).into(); + + let owner = GlobalConsensusEthereumConvertsFor::<[u8; 32]>::from_chain_id(&chain_id); + let asset_id = Self::convert_token_address(network, token); + let create_call_index: [u8; 2] = CreateAssetCall::get(); + let inbound_queue_pallet_index = InboundQueuePalletInstance::get(); + + let xcm: Xcm<()> = vec![ + // Teleport required fees. + ReceiveTeleportedAsset(total.into()), + // Pay for execution. + BuyExecution { fees: xcm_fee, weight_limit: Unlimited }, + // Fund the snowbridge sovereign with the required deposit for creation. + DepositAsset { assets: Definite(deposit.into()), beneficiary: bridge_location }, + // Only our inbound-queue pallet is allowed to invoke `UniversalOrigin` + DescendOrigin(X1(PalletInstance(inbound_queue_pallet_index))), + // Change origin to the bridge. + UniversalOrigin(GlobalConsensus(network)), + // Call create_asset on foreign assets pallet. + Transact { + origin_kind: OriginKind::Xcm, + require_weight_at_most: Weight::from_parts(400_000_000, 8_000), + call: ( + create_call_index, + asset_id, + MultiAddress::<[u8; 32], ()>::Id(owner), + MINIMUM_DEPOSIT, + ) + .encode() + .into(), + }, + RefundSurplus, + // Clear the origin so that remaining assets in holding + // are claimable by the physical origin (BridgeHub) + ClearOrigin, + ] + .into(); + + (xcm, total_amount.into()) + } + + fn convert_send_token( + chain_id: u64, + token: H160, + destination: Destination, + amount: u128, + asset_hub_fee: u128, + ) -> (Xcm<()>, Balance) { + let network = Ethereum { chain_id }; + let asset_hub_fee_asset: MultiAsset = (MultiLocation::parent(), asset_hub_fee).into(); + let asset: MultiAsset = (Self::convert_token_address(network, token), amount).into(); + + let (dest_para_id, beneficiary, dest_para_fee) = match destination { + // Final destination is a 32-byte account on AssetHub + Destination::AccountId32 { id } => ( + None, + MultiLocation { parents: 0, interior: X1(AccountId32 { network: None, id }) }, + 0, + ), + // Final destination is a 32-byte account on a sibling of AssetHub + Destination::ForeignAccountId32 { para_id, id, fee } => ( + Some(para_id), + MultiLocation { parents: 0, interior: X1(AccountId32 { network: None, id }) }, + // Total fee needs to cover execution on AssetHub and Sibling + fee, + ), + // Final destination is a 20-byte account on a sibling of AssetHub + Destination::ForeignAccountId20 { para_id, id, fee } => ( + Some(para_id), + MultiLocation { parents: 0, interior: X1(AccountKey20 { network: None, key: id }) }, + // Total fee needs to cover execution on AssetHub and Sibling + fee, + ), + }; + + let total_fees = asset_hub_fee.saturating_add(dest_para_fee); + let total_fee_asset: MultiAsset = (MultiLocation::parent(), total_fees).into(); + let inbound_queue_pallet_index = InboundQueuePalletInstance::get(); + + let mut instructions = vec![ + ReceiveTeleportedAsset(total_fee_asset.into()), + BuyExecution { fees: asset_hub_fee_asset, weight_limit: Unlimited }, + DescendOrigin(X1(PalletInstance(inbound_queue_pallet_index))), + UniversalOrigin(GlobalConsensus(network)), + ReserveAssetDeposited(asset.clone().into()), + ClearOrigin, + ]; + + match dest_para_id { + Some(dest_para_id) => { + let dest_para_fee_asset: MultiAsset = + (MultiLocation::parent(), dest_para_fee).into(); + + instructions.extend(vec![ + // Perform a deposit reserve to send to destination chain. + DepositReserveAsset { + assets: Definite(vec![dest_para_fee_asset.clone(), asset.clone()].into()), + dest: MultiLocation { parents: 1, interior: X1(Parachain(dest_para_id)) }, + xcm: vec![ + // Buy execution on target. + BuyExecution { fees: dest_para_fee_asset, weight_limit: Unlimited }, + // Deposit asset to beneficiary. + DepositAsset { assets: Definite(asset.into()), beneficiary }, + ] + .into(), + }, + ]); + }, + None => { + instructions.extend(vec![ + // Deposit asset to beneficiary. + DepositAsset { assets: Definite(asset.into()), beneficiary }, + ]); + }, + } + + (instructions.into(), total_fees.into()) + } + + // Convert ERC20 token address to a Multilocation that can be understood by Assets Hub. + fn convert_token_address(network: NetworkId, token: H160) -> MultiLocation { + MultiLocation { + parents: 2, + interior: X2( + GlobalConsensus(network), + AccountKey20 { network: None, key: token.into() }, + ), + } + } +} + +pub struct GlobalConsensusEthereumConvertsFor(PhantomData); +impl ConvertLocation for GlobalConsensusEthereumConvertsFor +where + AccountId: From<[u8; 32]> + Clone, +{ + fn convert_location(location: &MultiLocation) -> Option { + if let MultiLocation { interior: X1(GlobalConsensus(Ethereum { chain_id })), .. } = location + { + Some(Self::from_chain_id(chain_id).into()) + } else { + None + } + } +} + +impl GlobalConsensusEthereumConvertsFor { + pub fn from_chain_id(chain_id: &u64) -> [u8; 32] { + (b"ethereum-chain", chain_id).using_encoded(blake2_256) + } +} diff --git a/bridges/snowbridge/parachain/primitives/router/src/inbound/tests.rs b/bridges/snowbridge/parachain/primitives/router/src/inbound/tests.rs new file mode 100644 index 000000000000..8c96c13cf223 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/router/src/inbound/tests.rs @@ -0,0 +1,41 @@ +use super::GlobalConsensusEthereumConvertsFor; +use crate::inbound::CallIndex; +use frame_support::parameter_types; +use hex_literal::hex; +use xcm::v3::prelude::*; +use xcm_executor::traits::ConvertLocation; + +const NETWORK: NetworkId = Ethereum { chain_id: 11155111 }; + +parameter_types! { + pub EthereumNetwork: NetworkId = NETWORK; + + pub const CreateAssetCall: CallIndex = [1, 1]; + pub const CreateAssetExecutionFee: u128 = 123; + pub const CreateAssetDeposit: u128 = 891; + pub const SendTokenExecutionFee: u128 = 592; +} + +#[test] +fn test_contract_location_with_network_converts_successfully() { + let expected_account: [u8; 32] = + hex!("ce796ae65569a670d0c1cc1ac12515a3ce21b5fbf729d63d7b289baad070139d"); + let contract_location = MultiLocation { parents: 2, interior: X1(GlobalConsensus(NETWORK)) }; + + let account = + GlobalConsensusEthereumConvertsFor::<[u8; 32]>::convert_location(&contract_location) + .unwrap(); + + assert_eq!(account, expected_account); +} + +#[test] +fn test_contract_location_with_incorrect_location_fails_convert() { + let contract_location = + MultiLocation { parents: 2, interior: X2(GlobalConsensus(Polkadot), Parachain(1000)) }; + + assert_eq!( + GlobalConsensusEthereumConvertsFor::<[u8; 32]>::convert_location(&contract_location), + None, + ); +} diff --git a/bridges/snowbridge/parachain/primitives/router/src/lib.rs b/bridges/snowbridge/parachain/primitives/router/src/lib.rs new file mode 100644 index 000000000000..d9031c69b22b --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/router/src/lib.rs @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +#![cfg_attr(not(feature = "std"), no_std)] + +pub mod inbound; +pub mod outbound; diff --git a/bridges/snowbridge/parachain/primitives/router/src/outbound/mod.rs b/bridges/snowbridge/parachain/primitives/router/src/outbound/mod.rs new file mode 100644 index 000000000000..c7f2f440834c --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/router/src/outbound/mod.rs @@ -0,0 +1,282 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! Converts XCM messages into simpler commands that can be processed by the Gateway contract + +#[cfg(test)] +mod tests; + +use core::slice::Iter; + +use codec::{Decode, Encode}; + +use frame_support::{ensure, traits::Get}; +use snowbridge_core::{ + outbound::{AgentExecuteCommand, Command, Message, SendMessage}, + ChannelId, ParaId, +}; +use sp_core::{H160, H256}; +use sp_std::{iter::Peekable, marker::PhantomData, prelude::*}; +use xcm::v3::prelude::*; +use xcm_executor::traits::{ConvertLocation, ExportXcm}; + +pub struct EthereumBlobExporter< + UniversalLocation, + EthereumNetwork, + OutboundQueue, + AgentHashedDescription, +>(PhantomData<(UniversalLocation, EthereumNetwork, OutboundQueue, AgentHashedDescription)>); + +impl ExportXcm + for EthereumBlobExporter +where + UniversalLocation: Get, + EthereumNetwork: Get, + OutboundQueue: SendMessage, + AgentHashedDescription: ConvertLocation, +{ + type Ticket = (Vec, XcmHash); + + fn validate( + network: NetworkId, + _channel: u32, + universal_source: &mut Option, + destination: &mut Option, + message: &mut Option>, + ) -> SendResult { + let expected_network = EthereumNetwork::get(); + let universal_location = UniversalLocation::get(); + + if network != expected_network { + log::trace!(target: "xcm::ethereum_blob_exporter", "skipped due to unmatched bridge network {network:?}."); + return Err(SendError::NotApplicable) + } + + let dest = destination.take().ok_or(SendError::MissingArgument)?; + if dest != Here { + log::trace!(target: "xcm::ethereum_blob_exporter", "skipped due to unmatched remote destination {dest:?}."); + return Err(SendError::NotApplicable) + } + + let (local_net, local_sub) = universal_source + .take() + .ok_or_else(|| { + log::error!(target: "xcm::ethereum_blob_exporter", "universal source not provided."); + SendError::MissingArgument + })? + .split_global() + .map_err(|()| { + log::error!(target: "xcm::ethereum_blob_exporter", "could not get global consensus from universal source '{universal_source:?}'."); + SendError::Unroutable + })?; + + if Ok(local_net) != universal_location.global_consensus() { + log::trace!(target: "xcm::ethereum_blob_exporter", "skipped due to unmatched relay network {local_net:?}."); + return Err(SendError::NotApplicable) + } + + let para_id = match local_sub { + X1(Parachain(para_id)) => para_id, + _ => { + log::error!(target: "xcm::ethereum_blob_exporter", "could not get parachain id from universal source '{local_sub:?}'."); + return Err(SendError::MissingArgument) + }, + }; + + let message = message.take().ok_or_else(|| { + log::error!(target: "xcm::ethereum_blob_exporter", "xcm message not provided."); + SendError::MissingArgument + })?; + + let mut converter = XcmConverter::new(&message, &expected_network); + let (agent_execute_command, message_id) = converter.convert().map_err(|err|{ + log::error!(target: "xcm::ethereum_blob_exporter", "unroutable due to pattern matching error '{err:?}'."); + SendError::Unroutable + })?; + + let source_location: MultiLocation = MultiLocation { parents: 1, interior: local_sub }; + let agent_id = match AgentHashedDescription::convert_location(&source_location) { + Some(id) => id, + None => { + log::error!(target: "xcm::ethereum_blob_exporter", "unroutable due to not being able to create agent id. '{source_location:?}'"); + return Err(SendError::Unroutable) + }, + }; + + let channel_id: ChannelId = ParaId::from(para_id).into(); + + let outbound_message = Message { + id: Some(message_id.into()), + channel_id, + command: Command::AgentExecute { agent_id, command: agent_execute_command }, + }; + + // validate the message + let (ticket, fee) = OutboundQueue::validate(&outbound_message).map_err(|err| { + log::error!(target: "xcm::ethereum_blob_exporter", "OutboundQueue validation of message failed. {err:?}"); + SendError::Unroutable + })?; + + // convert fee to MultiAsset + let fee = MultiAsset::from((MultiLocation::parent(), fee.total())).into(); + + Ok(((ticket.encode(), message_id), fee)) + } + + fn deliver(blob: (Vec, XcmHash)) -> Result { + let ticket: OutboundQueue::Ticket = OutboundQueue::Ticket::decode(&mut blob.0.as_ref()) + .map_err(|_| { + log::trace!(target: "xcm::ethereum_blob_exporter", "undeliverable due to decoding error"); + SendError::NotApplicable + })?; + + let message_id = OutboundQueue::deliver(ticket).map_err(|_| { + log::error!(target: "xcm::ethereum_blob_exporter", "OutboundQueue submit of message failed"); + SendError::Transport("other transport error") + })?; + + log::info!(target: "xcm::ethereum_blob_exporter", "message delivered {message_id:#?}."); + Ok(message_id.into()) + } +} + +/// Errors that can be thrown to the pattern matching step. +#[derive(PartialEq, Debug)] +enum XcmConverterError { + UnexpectedEndOfXcm, + EndOfXcmMessageExpected, + WithdrawAssetExpected, + DepositAssetExpected, + NoReserveAssets, + FilterDoesNotConsumeAllAssets, + TooManyAssets, + ZeroAssetTransfer, + BeneficiaryResolutionFailed, + AssetResolutionFailed, + InvalidFeeAsset, + SetTopicExpected, +} + +macro_rules! match_expression { + ($expression:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?, $value:expr $(,)?) => { + match $expression { + $( $pattern )|+ $( if $guard )? => Some($value), + _ => None, + } + }; +} + +struct XcmConverter<'a, Call> { + iter: Peekable>>, + ethereum_network: &'a NetworkId, +} +impl<'a, Call> XcmConverter<'a, Call> { + fn new(message: &'a Xcm, ethereum_network: &'a NetworkId) -> Self { + Self { iter: message.inner().iter().peekable(), ethereum_network } + } + + fn convert(&mut self) -> Result<(AgentExecuteCommand, [u8; 32]), XcmConverterError> { + // Get withdraw/deposit and make native tokens create message. + let result = self.native_tokens_unlock_message()?; + + // All xcm instructions must be consumed before exit. + if self.next().is_ok() { + return Err(XcmConverterError::EndOfXcmMessageExpected) + } + + Ok(result) + } + + fn native_tokens_unlock_message( + &mut self, + ) -> Result<(AgentExecuteCommand, [u8; 32]), XcmConverterError> { + use XcmConverterError::*; + + // Get the reserve assets from WithdrawAsset. + let reserve_assets = + match_expression!(self.next()?, WithdrawAsset(reserve_assets), reserve_assets) + .ok_or(WithdrawAssetExpected)?; + + // Check if clear origin exists and skip over it. + if match_expression!(self.peek(), Ok(ClearOrigin), ()).is_some() { + let _ = self.next(); + } + + // Get the fee asset item from BuyExecution or continue parsing. + let fee_asset = match_expression!(self.peek(), Ok(BuyExecution { fees, .. }), fees); + if fee_asset.is_some() { + let _ = self.next(); + } + + let (deposit_assets, beneficiary) = match_expression!( + self.next()?, + DepositAsset { assets, beneficiary }, + (assets, beneficiary) + ) + .ok_or(DepositAssetExpected)?; + + // assert that the beneficiary is AccountKey20. + let recipient = match_expression!( + beneficiary, + MultiLocation { parents: 0, interior: X1(AccountKey20 { network, key }) } + if self.network_matches(network), + H160(*key) + ) + .ok_or(BeneficiaryResolutionFailed)?; + + // Make sure there are reserved assets. + if reserve_assets.len() == 0 { + return Err(NoReserveAssets) + } + + // Check the the deposit asset filter matches what was reserved. + if reserve_assets.inner().iter().any(|asset| !deposit_assets.matches(asset)) { + return Err(FilterDoesNotConsumeAllAssets) + } + + // We only support a single asset at a time. + ensure!(reserve_assets.len() == 1, TooManyAssets); + let reserve_asset = reserve_assets.get(0).ok_or(AssetResolutionFailed)?; + + // If there was a fee specified verify it. + if let Some(fee_asset) = fee_asset { + // The fee asset must be the same as the reserve asset. + if fee_asset.id != reserve_asset.id || fee_asset.fun > reserve_asset.fun { + return Err(InvalidFeeAsset) + } + } + + let (token, amount) = match_expression!( + reserve_asset, + MultiAsset { + id: Concrete(MultiLocation { parents: 0, interior: X1(AccountKey20 { network , key })}), + fun: Fungible(amount) + } if self.network_matches(network), + (H160(*key), *amount) + ) + .ok_or(AssetResolutionFailed)?; + + // transfer amount must be greater than 0. + ensure!(amount > 0, ZeroAssetTransfer); + + // Check if there is a SetTopic and skip over it if found. + let topic_id = match_expression!(self.next()?, SetTopic(id), id).ok_or(SetTopicExpected)?; + + Ok((AgentExecuteCommand::TransferToken { token, recipient, amount }, *topic_id)) + } + + fn next(&mut self) -> Result<&'a Instruction, XcmConverterError> { + self.iter.next().ok_or(XcmConverterError::UnexpectedEndOfXcm) + } + + fn peek(&mut self) -> Result<&&'a Instruction, XcmConverterError> { + self.iter.peek().ok_or(XcmConverterError::UnexpectedEndOfXcm) + } + + fn network_matches(&self, network: &Option) -> bool { + if let Some(network) = network { + network == self.ethereum_network + } else { + true + } + } +} diff --git a/bridges/snowbridge/parachain/primitives/router/src/outbound/tests.rs b/bridges/snowbridge/parachain/primitives/router/src/outbound/tests.rs new file mode 100644 index 000000000000..153d934c3909 --- /dev/null +++ b/bridges/snowbridge/parachain/primitives/router/src/outbound/tests.rs @@ -0,0 +1,1063 @@ +use frame_support::parameter_types; +use hex_literal::hex; +use snowbridge_core::{ + outbound::{Fee, SendError, SendMessageFeeProvider}, + AgentIdOf, +}; +use xcm::v3::prelude::SendError as XcmSendError; + +use super::*; + +parameter_types! { + const MaxMessageSize: u32 = u32::MAX; + const RelayNetwork: NetworkId = Polkadot; + const UniversalLocation: InteriorMultiLocation = X2(GlobalConsensus(RelayNetwork::get()), Parachain(1013)); + const BridgedNetwork: NetworkId = Ethereum{ chain_id: 1 }; + const NonBridgedNetwork: NetworkId = Ethereum{ chain_id: 2 }; +} + +struct MockOkOutboundQueue; +impl SendMessage for MockOkOutboundQueue { + type Ticket = (); + + fn validate(_: &Message) -> Result<(Self::Ticket, Fee), SendError> { + Ok(((), Fee { local: 1, remote: 1 })) + } + + fn deliver(_: Self::Ticket) -> Result { + Ok(H256::zero()) + } +} + +impl SendMessageFeeProvider for MockOkOutboundQueue { + type Balance = u128; + + fn local_fee() -> Self::Balance { + 1 + } +} +struct MockErrOutboundQueue; +impl SendMessage for MockErrOutboundQueue { + type Ticket = (); + + fn validate(_: &Message) -> Result<(Self::Ticket, Fee), SendError> { + Err(SendError::MessageTooLarge) + } + + fn deliver(_: Self::Ticket) -> Result { + Err(SendError::MessageTooLarge) + } +} + +impl SendMessageFeeProvider for MockErrOutboundQueue { + type Balance = u128; + + fn local_fee() -> Self::Balance { + 1 + } +} + +#[test] +fn exporter_validate_with_unknown_network_yields_not_applicable() { + let network = Ethereum { chain_id: 1337 }; + let channel: u32 = 0; + let mut universal_source: Option = None; + let mut destination: Option = None; + let mut message: Option> = None; + + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockOkOutboundQueue, + AgentIdOf, + >::validate( + network, channel, &mut universal_source, &mut destination, &mut message + ); + assert_eq!(result, Err(XcmSendError::NotApplicable)); +} + +#[test] +fn exporter_validate_with_invalid_destination_yields_missing_argument() { + let network = BridgedNetwork::get(); + let channel: u32 = 0; + let mut universal_source: Option = None; + let mut destination: Option = None; + let mut message: Option> = None; + + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockOkOutboundQueue, + AgentIdOf, + >::validate( + network, channel, &mut universal_source, &mut destination, &mut message + ); + assert_eq!(result, Err(XcmSendError::MissingArgument)); +} + +#[test] +fn exporter_validate_with_x8_destination_yields_not_applicable() { + let network = BridgedNetwork::get(); + let channel: u32 = 0; + let mut universal_source: Option = None; + let mut destination: Option = Some(X8( + OnlyChild, OnlyChild, OnlyChild, OnlyChild, OnlyChild, OnlyChild, OnlyChild, OnlyChild, + )); + let mut message: Option> = None; + + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockOkOutboundQueue, + AgentIdOf, + >::validate( + network, channel, &mut universal_source, &mut destination, &mut message + ); + assert_eq!(result, Err(XcmSendError::NotApplicable)); +} + +#[test] +fn exporter_validate_without_universal_source_yields_missing_argument() { + let network = BridgedNetwork::get(); + let channel: u32 = 0; + let mut universal_source: Option = None; + let mut destination: Option = Here.into(); + let mut message: Option> = None; + + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockOkOutboundQueue, + AgentIdOf, + >::validate( + network, channel, &mut universal_source, &mut destination, &mut message + ); + assert_eq!(result, Err(XcmSendError::MissingArgument)); +} + +#[test] +fn exporter_validate_without_global_universal_location_yields_unroutable() { + let network = BridgedNetwork::get(); + let channel: u32 = 0; + let mut universal_source: Option = Here.into(); + let mut destination: Option = Here.into(); + let mut message: Option> = None; + + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockOkOutboundQueue, + AgentIdOf, + >::validate( + network, channel, &mut universal_source, &mut destination, &mut message + ); + assert_eq!(result, Err(XcmSendError::Unroutable)); +} + +#[test] +fn exporter_validate_without_global_bridge_location_yields_not_applicable() { + let network = NonBridgedNetwork::get(); + let channel: u32 = 0; + let mut universal_source: Option = Here.into(); + let mut destination: Option = Here.into(); + let mut message: Option> = None; + + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockOkOutboundQueue, + AgentIdOf, + >::validate( + network, channel, &mut universal_source, &mut destination, &mut message + ); + assert_eq!(result, Err(XcmSendError::NotApplicable)); +} + +#[test] +fn exporter_validate_with_remote_universal_source_yields_not_applicable() { + let network = BridgedNetwork::get(); + let channel: u32 = 0; + let mut universal_source: Option = + Some(X2(GlobalConsensus(Kusama), Parachain(1000))); + let mut destination: Option = Here.into(); + let mut message: Option> = None; + + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockOkOutboundQueue, + AgentIdOf, + >::validate( + network, channel, &mut universal_source, &mut destination, &mut message + ); + assert_eq!(result, Err(XcmSendError::NotApplicable)); +} + +#[test] +fn exporter_validate_without_para_id_in_source_yields_missing_argument() { + let network = BridgedNetwork::get(); + let channel: u32 = 0; + let mut universal_source: Option = Some(X1(GlobalConsensus(Polkadot))); + let mut destination: Option = Here.into(); + let mut message: Option> = None; + + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockOkOutboundQueue, + AgentIdOf, + >::validate( + network, channel, &mut universal_source, &mut destination, &mut message + ); + assert_eq!(result, Err(XcmSendError::MissingArgument)); +} + +#[test] +fn exporter_validate_complex_para_id_in_source_yields_missing_argument() { + let network = BridgedNetwork::get(); + let channel: u32 = 0; + let mut universal_source: Option = + Some(X3(GlobalConsensus(Polkadot), Parachain(1000), PalletInstance(12))); + let mut destination: Option = Here.into(); + let mut message: Option> = None; + + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockOkOutboundQueue, + AgentIdOf, + >::validate( + network, channel, &mut universal_source, &mut destination, &mut message + ); + assert_eq!(result, Err(XcmSendError::MissingArgument)); +} + +#[test] +fn exporter_validate_without_xcm_message_yields_missing_argument() { + let network = BridgedNetwork::get(); + let channel: u32 = 0; + let mut universal_source: Option = + Some(X2(GlobalConsensus(Polkadot), Parachain(1000))); + let mut destination: Option = Here.into(); + let mut message: Option> = None; + + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockOkOutboundQueue, + AgentIdOf, + >::validate( + network, channel, &mut universal_source, &mut destination, &mut message + ); + assert_eq!(result, Err(XcmSendError::MissingArgument)); +} + +#[test] +fn exporter_validate_with_max_target_fee_yields_unroutable() { + let network = BridgedNetwork::get(); + let mut destination: Option = Here.into(); + + let mut universal_source: Option = + Some(X2(GlobalConsensus(Polkadot), Parachain(1000))); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let channel: u32 = 0; + let fee = MultiAsset { id: Concrete(Here.into()), fun: Fungible(1000) }; + let fees: MultiAssets = vec![fee.clone()].into(); + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }] + .into(); + let filter: MultiAssetFilter = assets.clone().into(); + + let mut message: Option> = Some( + vec![ + WithdrawAsset(fees), + BuyExecution { fees: fee, weight_limit: Unlimited }, + WithdrawAsset(assets), + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: Some(network), key: beneficiary_address }) + .into(), + }, + SetTopic([0; 32]), + ] + .into(), + ); + + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockOkOutboundQueue, + AgentIdOf, + >::validate( + network, channel, &mut universal_source, &mut destination, &mut message + ); + + assert_eq!(result, Err(XcmSendError::Unroutable)); +} + +#[test] +fn exporter_validate_with_unparsable_xcm_yields_unroutable() { + let network = BridgedNetwork::get(); + let mut destination: Option = Here.into(); + + let mut universal_source: Option = + Some(X2(GlobalConsensus(Polkadot), Parachain(1000))); + + let channel: u32 = 0; + let fee = MultiAsset { id: Concrete(Here.into()), fun: Fungible(1000) }; + let fees: MultiAssets = vec![fee.clone()].into(); + + let mut message: Option> = + Some(vec![WithdrawAsset(fees), BuyExecution { fees: fee, weight_limit: Unlimited }].into()); + + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockOkOutboundQueue, + AgentIdOf, + >::validate( + network, channel, &mut universal_source, &mut destination, &mut message + ); + + assert_eq!(result, Err(XcmSendError::Unroutable)); +} + +#[test] +fn exporter_validate_xcm_success_case_1() { + let network = BridgedNetwork::get(); + let mut destination: Option = Here.into(); + + let mut universal_source: Option = + Some(X2(GlobalConsensus(Polkadot), Parachain(1000))); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let channel: u32 = 0; + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }] + .into(); + let fee = assets.clone().get(0).unwrap().clone(); + let filter: MultiAssetFilter = assets.clone().into(); + + let mut message: Option> = Some( + vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: fee, weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(), + ); + + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockOkOutboundQueue, + AgentIdOf, + >::validate( + network, channel, &mut universal_source, &mut destination, &mut message + ); + + assert!(result.is_ok()); +} + +#[test] +fn exporter_deliver_with_submit_failure_yields_unroutable() { + let result = EthereumBlobExporter::< + UniversalLocation, + BridgedNetwork, + MockErrOutboundQueue, + AgentIdOf, + >::deliver((hex!("deadbeef").to_vec(), XcmHash::default())); + assert_eq!(result, Err(XcmSendError::Transport("other transport error"))) +} + +#[test] +fn xcm_converter_convert_success() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }] + .into(); + let filter: MultiAssetFilter = assets.clone().into(); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + let expected_payload = AgentExecuteCommand::TransferToken { + token: token_address.into(), + recipient: beneficiary_address.into(), + amount: 1000, + }; + let result = converter.convert(); + assert_eq!(result, Ok((expected_payload, [0; 32]))); +} + +#[test] +fn xcm_converter_convert_without_buy_execution_yields_success() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }] + .into(); + let filter: MultiAssetFilter = assets.clone().into(); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + let expected_payload = AgentExecuteCommand::TransferToken { + token: token_address.into(), + recipient: beneficiary_address.into(), + amount: 1000, + }; + let result = converter.convert(); + assert_eq!(result, Ok((expected_payload, [0; 32]))); +} + +#[test] +fn xcm_converter_convert_with_wildcard_all_asset_filter_succeeds() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }] + .into(); + let filter: MultiAssetFilter = Wild(All); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + let expected_payload = AgentExecuteCommand::TransferToken { + token: token_address.into(), + recipient: beneficiary_address.into(), + amount: 1000, + }; + let result = converter.convert(); + assert_eq!(result, Ok((expected_payload, [0; 32]))); +} + +#[test] +fn xcm_converter_convert_with_fees_less_than_reserve_yields_success() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let asset_location = X1(AccountKey20 { network: None, key: token_address }).into(); + let fee_asset = MultiAsset { id: Concrete(asset_location), fun: Fungible(500) }; + + let assets: MultiAssets = + vec![MultiAsset { id: Concrete(asset_location), fun: Fungible(1000) }].into(); + + let filter: MultiAssetFilter = assets.clone().into(); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: fee_asset, weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + let expected_payload = AgentExecuteCommand::TransferToken { + token: token_address.into(), + recipient: beneficiary_address.into(), + amount: 1000, + }; + let result = converter.convert(); + assert_eq!(result, Ok((expected_payload, [0; 32]))); +} + +#[test] +fn xcm_converter_convert_without_set_topic_yields_set_topic_expected() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }] + .into(); + let filter: MultiAssetFilter = assets.clone().into(); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + ClearTopic, + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::SetTopicExpected)); +} + +#[test] +fn xcm_converter_convert_with_partial_message_yields_unexpected_end_of_xcm() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }] + .into(); + let message: Xcm<()> = vec![WithdrawAsset(assets)].into(); + + let mut converter = XcmConverter::new(&message, &network); + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::UnexpectedEndOfXcm)); +} + +#[test] +fn xcm_converter_with_different_fee_asset_fails() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let asset_location = X1(AccountKey20 { network: None, key: token_address }).into(); + let fee_asset = MultiAsset { + id: Concrete(MultiLocation { parents: 0, interior: Here }), + fun: Fungible(1000), + }; + + let assets: MultiAssets = + vec![MultiAsset { id: Concrete(asset_location), fun: Fungible(1000) }].into(); + + let filter: MultiAssetFilter = assets.clone().into(); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: fee_asset, weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::InvalidFeeAsset)); +} + +#[test] +fn xcm_converter_with_fees_greater_than_reserve_fails() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let asset_location = X1(AccountKey20 { network: None, key: token_address }).into(); + let fee_asset = MultiAsset { id: Concrete(asset_location), fun: Fungible(1001) }; + + let assets: MultiAssets = + vec![MultiAsset { id: Concrete(asset_location), fun: Fungible(1000) }].into(); + + let filter: MultiAssetFilter = assets.clone().into(); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: fee_asset, weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::InvalidFeeAsset)); +} + +#[test] +fn xcm_converter_convert_with_empty_xcm_yields_unexpected_end_of_xcm() { + let network = BridgedNetwork::get(); + + let message: Xcm<()> = vec![].into(); + + let mut converter = XcmConverter::new(&message, &network); + + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::UnexpectedEndOfXcm)); +} + +#[test] +fn xcm_converter_convert_with_extra_instructions_yields_end_of_xcm_message_expected() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }] + .into(); + let filter: MultiAssetFilter = assets.clone().into(); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ClearError, + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::EndOfXcmMessageExpected)); +} + +#[test] +fn xcm_converter_convert_without_withdraw_asset_yields_withdraw_expected() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }] + .into(); + let filter: MultiAssetFilter = assets.clone().into(); + + let message: Xcm<()> = vec![ + ClearOrigin, + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::WithdrawAssetExpected)); +} + +#[test] +fn xcm_converter_convert_without_withdraw_asset_yields_deposit_expected() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }] + .into(); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::DepositAssetExpected)); +} + +#[test] +fn xcm_converter_convert_without_assets_yields_no_reserve_assets() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![].into(); + let filter: MultiAssetFilter = assets.clone().into(); + + let fee = MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }; + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: fee, weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::NoReserveAssets)); +} + +#[test] +fn xcm_converter_convert_with_two_assets_yields_too_many_assets() { + let network = BridgedNetwork::get(); + + let token_address_1: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let token_address_2: [u8; 20] = hex!("1100000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![ + MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address_1 }).into()), + fun: Fungible(1000), + }, + MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address_2 }).into()), + fun: Fungible(500), + }, + ] + .into(); + let filter: MultiAssetFilter = assets.clone().into(); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::TooManyAssets)); +} + +#[test] +fn xcm_converter_convert_without_consuming_filter_yields_filter_does_not_consume_all_assets() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }] + .into(); + let filter: MultiAssetFilter = Wild(WildMultiAsset::AllCounted(0)); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::FilterDoesNotConsumeAllAssets)); +} + +#[test] +fn xcm_converter_convert_with_zero_amount_asset_yields_zero_asset_transfer() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(0), + }] + .into(); + let filter: MultiAssetFilter = Wild(WildMultiAsset::AllCounted(1)); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::ZeroAssetTransfer)); +} + +#[test] +fn xcm_converter_convert_non_ethereum_asset_yields_asset_resolution_failed() { + let network = BridgedNetwork::get(); + + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X3(GlobalConsensus(Polkadot), Parachain(1000), GeneralIndex(0)).into()), + fun: Fungible(1000), + }] + .into(); + let filter: MultiAssetFilter = Wild(WildMultiAsset::AllCounted(1)); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::AssetResolutionFailed)); +} + +#[test] +fn xcm_converter_convert_non_ethereum_chain_asset_yields_asset_resolution_failed() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete( + X1(AccountKey20 { network: Some(Ethereum { chain_id: 2 }), key: token_address }).into(), + ), + fun: Fungible(1000), + }] + .into(); + let filter: MultiAssetFilter = Wild(WildMultiAsset::AllCounted(1)); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::AssetResolutionFailed)); +} + +#[test] +fn xcm_converter_convert_non_ethereum_chain_yields_asset_resolution_failed() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete( + X1(AccountKey20 { network: Some(NonBridgedNetwork::get()), key: token_address }).into(), + ), + fun: Fungible(1000), + }] + .into(); + let filter: MultiAssetFilter = Wild(WildMultiAsset::AllCounted(1)); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { network: None, key: beneficiary_address }).into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::AssetResolutionFailed)); +} + +#[test] +fn xcm_converter_convert_with_non_ethereum_beneficiary_yields_beneficiary_resolution_failed() { + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + + let beneficiary_address: [u8; 32] = + hex!("2000000000000000000000000000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }] + .into(); + let filter: MultiAssetFilter = Wild(WildMultiAsset::AllCounted(1)); + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X3( + GlobalConsensus(Polkadot), + Parachain(1000), + AccountId32 { network: Some(Polkadot), id: beneficiary_address }, + ) + .into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::BeneficiaryResolutionFailed)); +} + +#[test] +fn xcm_converter_convert_with_non_ethereum_chain_beneficiary_yields_beneficiary_resolution_failed() +{ + let network = BridgedNetwork::get(); + + let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000"); + let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000"); + + let assets: MultiAssets = vec![MultiAsset { + id: Concrete(X1(AccountKey20 { network: None, key: token_address }).into()), + fun: Fungible(1000), + }] + .into(); + let filter: MultiAssetFilter = Wild(WildMultiAsset::AllCounted(1)); + + let message: Xcm<()> = vec![ + WithdrawAsset(assets.clone()), + ClearOrigin, + BuyExecution { fees: assets.get(0).unwrap().clone(), weight_limit: Unlimited }, + DepositAsset { + assets: filter, + beneficiary: X1(AccountKey20 { + network: Some(Ethereum { chain_id: 2 }), + key: beneficiary_address, + }) + .into(), + }, + SetTopic([0; 32]), + ] + .into(); + let mut converter = XcmConverter::new(&message, &network); + + let result = converter.convert(); + assert_eq!(result.err(), Some(XcmConverterError::BeneficiaryResolutionFailed)); +} + +#[test] +fn test_describe_asset_hub() { + let legacy_location: MultiLocation = + MultiLocation { parents: 0, interior: X1(Parachain(1000)) }; + let legacy_agent_id = AgentIdOf::convert_location(&legacy_location).unwrap(); + assert_eq!( + legacy_agent_id, + hex!("72456f48efed08af20e5b317abf8648ac66e86bb90a411d9b0b713f7364b75b4").into() + ); + let location: MultiLocation = MultiLocation { parents: 1, interior: X1(Parachain(1000)) }; + let agent_id = AgentIdOf::convert_location(&location).unwrap(); + assert_eq!( + agent_id, + hex!("81c5ab2571199e3188135178f3c2c8e2d268be1313d029b30f534fa579b69b79").into() + ) +} + +#[test] +fn test_describe_here() { + let location: MultiLocation = MultiLocation { parents: 0, interior: Here }; + let agent_id = AgentIdOf::convert_location(&location).unwrap(); + assert_eq!( + agent_id, + hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into() + ) +} diff --git a/bridges/snowbridge/parachain/runtime/rococo-common/Cargo.toml b/bridges/snowbridge/parachain/runtime/rococo-common/Cargo.toml new file mode 100644 index 000000000000..656ed6de26e8 --- /dev/null +++ b/bridges/snowbridge/parachain/runtime/rococo-common/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "snowbridge-rococo-common" +description = "Snowbridge Rococo Common" +version = "0.0.1" +authors = ["Snowfork "] +edition = "2021" +license = "Apache-2.0" + +[dependencies] +log = { version = "0.4.20", default-features = false } + +frame-support = { path = "../../../../../substrate/frame/support", default-features = false } +xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false } + +[dev-dependencies] + +[features] +default = ["std"] +std = [ + "frame-support/std", + "log/std", + "xcm/std", +] +runtime-benchmarks = [ + "frame-support/runtime-benchmarks", +] diff --git a/bridges/snowbridge/parachain/runtime/rococo-common/src/lib.rs b/bridges/snowbridge/parachain/runtime/rococo-common/src/lib.rs new file mode 100644 index 000000000000..97f0332fe66b --- /dev/null +++ b/bridges/snowbridge/parachain/runtime/rococo-common/src/lib.rs @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! # Rococo Common +//! +//! Config used for the Rococo asset hub and bridge hub runtimes. +#![cfg_attr(not(feature = "std"), no_std)] + +use frame_support::parameter_types; +use xcm::opaque::lts::NetworkId; + +pub const INBOUND_QUEUE_MESSAGES_PALLET_INDEX: u8 = 80; + +parameter_types! { + // Network and location for the Ethereum chain. + pub EthereumNetwork: NetworkId = NetworkId::Ethereum { chain_id: 11155111 }; +} diff --git a/bridges/snowbridge/parachain/runtime/runtime-common/Cargo.toml b/bridges/snowbridge/parachain/runtime/runtime-common/Cargo.toml new file mode 100644 index 000000000000..b835152cac0d --- /dev/null +++ b/bridges/snowbridge/parachain/runtime/runtime-common/Cargo.toml @@ -0,0 +1,41 @@ +[package] +name = "snowbridge-runtime-common" +description = "Snowbridge Runtime Common" +version = "0.1.1" +authors = ["Snowfork "] +edition = "2021" +license = "Apache-2.0" + +[dependencies] +log = { version = "0.4.20", default-features = false } + +frame-support = { path = "../../../../../substrate/frame/support", default-features = false } +frame-system = { path = "../../../../../substrate/frame/system", default-features = false } +sp-arithmetic = { path = "../../../../../substrate/primitives/arithmetic", default-features = false } +xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false } +xcm-builder = { package = "staging-xcm-builder", path = "../../../../../polkadot/xcm/xcm-builder", default-features = false } +xcm-executor = { package = "staging-xcm-executor", path = "../../../../../polkadot/xcm/xcm-executor", default-features = false } + +snowbridge-core = { path = "../../primitives/core", default-features = false } + +[dev-dependencies] + +[features] +default = ["std"] +std = [ + "frame-support/std", + "frame-system/std", + "log/std", + "snowbridge-core/std", + "sp-arithmetic/std", + "xcm-builder/std", + "xcm-executor/std", + "xcm/std", +] +runtime-benchmarks = [ + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "snowbridge-core/runtime-benchmarks", + "xcm-builder/runtime-benchmarks", + "xcm-executor/runtime-benchmarks", +] diff --git a/bridges/snowbridge/parachain/runtime/runtime-common/src/lib.rs b/bridges/snowbridge/parachain/runtime/runtime-common/src/lib.rs new file mode 100644 index 000000000000..b7f54d262bbb --- /dev/null +++ b/bridges/snowbridge/parachain/runtime/runtime-common/src/lib.rs @@ -0,0 +1,129 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork +//! # Runtime Common +//! +//! Common traits and types shared by runtimes. +#![cfg_attr(not(feature = "std"), no_std)] + +use core::marker::PhantomData; +use frame_support::traits::Get; +use snowbridge_core::{outbound::SendMessageFeeProvider, sibling_sovereign_account_raw}; +use sp_arithmetic::traits::{BaseArithmetic, Unsigned}; +use xcm::prelude::*; +use xcm_builder::{deposit_or_burn_fee, HandleFee}; +use xcm_executor::traits::{FeeReason, TransactAsset}; + +/// A `HandleFee` implementation that takes fees from `ExportMessage` XCM instructions +/// to Snowbridge and splits off the remote fee and deposits it to the origin +/// parachain sovereign account. The local fee is then returned back to be handled by +/// the next fee handler in the chain. Most likely the treasury account. +pub struct XcmExportFeeToSibling< + Balance, + AccountId, + FeeAssetLocation, + EthereumNetwork, + AssetTransactor, + FeeProvider, +>( + PhantomData<( + Balance, + AccountId, + FeeAssetLocation, + EthereumNetwork, + AssetTransactor, + FeeProvider, + )>, +); + +impl HandleFee + for XcmExportFeeToSibling< + Balance, + AccountId, + FeeAssetLocation, + EthereumNetwork, + AssetTransactor, + FeeProvider, + > where + Balance: BaseArithmetic + Unsigned + Copy + From + Into, + AccountId: Clone + Into<[u8; 32]> + From<[u8; 32]>, + FeeAssetLocation: Get, + EthereumNetwork: Get, + AssetTransactor: TransactAsset, + FeeProvider: SendMessageFeeProvider, +{ + fn handle_fee( + fees: MultiAssets, + context: Option<&XcmContext>, + reason: FeeReason, + ) -> MultiAssets { + let token_location = FeeAssetLocation::get(); + + // Check the reason to see if this export is for snowbridge. + if !matches!( + reason, + FeeReason::Export { network: bridged_network, destination } + if bridged_network == EthereumNetwork::get() && destination == Here + ) { + return fees + } + + // Get the parachain sovereign from the `context`. + let para_sovereign = if let Some(XcmContext { + origin: Some(MultiLocation { parents: 1, interior }), + .. + }) = context + { + if let Some(Parachain(sibling_para_id)) = interior.first() { + let account: AccountId = + sibling_sovereign_account_raw((*sibling_para_id).into()).into(); + account + } else { + return fees + } + } else { + return fees + }; + + // Get the total fee offered by export message. + let maybe_total_supplied_fee: Option<(usize, Balance)> = fees + .inner() + .iter() + .enumerate() + .filter_map(|(index, asset)| { + if let MultiAsset { id: Concrete(location), fun: Fungible(amount) } = asset { + if *location == token_location { + return Some((index, (*amount).into())) + } + } + None + }) + .next(); + + if let Some((fee_index, total_fee)) = maybe_total_supplied_fee { + let remote_fee = total_fee.saturating_sub(FeeProvider::local_fee()); + if remote_fee > (0u128).into() { + // Refund remote component of fee to physical origin + deposit_or_burn_fee::( + MultiAsset { id: Concrete(token_location), fun: Fungible(remote_fee.into()) } + .into(), + context, + para_sovereign, + ); + // Return remaining fee to the next fee handler in the chain. + let mut modified_fees = fees.inner().clone(); + modified_fees.remove(fee_index); + modified_fees.push(MultiAsset { + id: Concrete(token_location), + fun: Fungible((total_fee - remote_fee).into()), + }); + return modified_fees.into() + } + } + + log::info!( + target: "xcm::fees", + "XcmExportFeeToSibling skipped: {fees:?}, context: {context:?}, reason: {reason:?}", + ); + fees + } +} diff --git a/bridges/snowbridge/parachain/runtime/tests/Cargo.toml b/bridges/snowbridge/parachain/runtime/tests/Cargo.toml new file mode 100644 index 000000000000..da1fe878d935 --- /dev/null +++ b/bridges/snowbridge/parachain/runtime/tests/Cargo.toml @@ -0,0 +1,244 @@ +[package] +name = "snowbridge-runtime-tests" +description = "Snowbridge Runtime Tests" +version = "0.1.0" +authors = ["Snowfork "] +edition = "2021" +license = "Apache-2.0" + +[dependencies] +codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } +hex-literal = { version = "0.4.1" } +log = { version = "0.4.20", default-features = false } +scale-info = { version = "2.10.0", default-features = false, features = ["derive"] } +serde = { version = "1.0.188", optional = true, features = ["derive"] } +smallvec = "1.11.0" + +# Substrate +frame-benchmarking = { path = "../../../../../substrate/frame/benchmarking", default-features = false, optional = true } +frame-executive = { path = "../../../../../substrate/frame/executive", default-features = false } +frame-support = { path = "../../../../../substrate/frame/support", default-features = false } +frame-system = { path = "../../../../../substrate/frame/system", default-features = false } +frame-system-benchmarking = { path = "../../../../../substrate/frame/system/benchmarking", default-features = false, optional = true } +frame-system-rpc-runtime-api = { path = "../../../../../substrate/frame/system/rpc/runtime-api", default-features = false } +frame-try-runtime = { path = "../../../../../substrate/frame/try-runtime", default-features = false, optional = true } +pallet-aura = { path = "../../../../../substrate/frame/aura", default-features = false } +pallet-authorship = { path = "../../../../../substrate/frame/authorship", default-features = false } +pallet-balances = { path = "../../../../../substrate/frame/balances", default-features = false } +pallet-session = { path = "../../../../../substrate/frame/session", default-features = false } +pallet-multisig = { path = "../../../../../substrate/frame/multisig", default-features = false } +pallet-message-queue = { path = "../../../../../substrate/frame/message-queue", default-features = false } +pallet-timestamp = { path = "../../../../../substrate/frame/timestamp", default-features = false } +pallet-transaction-payment = { path = "../../../../../substrate/frame/transaction-payment", default-features = false } +pallet-transaction-payment-rpc-runtime-api = { path = "../../../../../substrate/frame/transaction-payment/rpc/runtime-api", default-features = false } +pallet-utility = { path = "../../../../../substrate/frame/utility", default-features = false } +sp-api = { path = "../../../../../substrate/primitives/api", default-features = false } +sp-block-builder = { path = "../../../../../substrate/primitives/block-builder", default-features = false } +sp-consensus-aura = { path = "../../../../../substrate/primitives/consensus/aura", default-features = false } +sp-core = { path = "../../../../../substrate/primitives/core", default-features = false } +sp-genesis-builder = { path = "../../../../../substrate/primitives/genesis-builder", default-features = false } +sp-inherents = { path = "../../../../../substrate/primitives/inherents", default-features = false } +sp-io = { path = "../../../../../substrate/primitives/io", default-features = false } +sp-offchain = { path = "../../../../../substrate/primitives/offchain", default-features = false } +sp-runtime = { path = "../../../../../substrate/primitives/runtime", default-features = false } +sp-session = { path = "../../../../../substrate/primitives/session", default-features = false } +sp-std = { path = "../../../../../substrate/primitives/std", default-features = false } +sp-storage = { path = "../../../../../substrate/primitives/storage", default-features = false } +sp-transaction-pool = { path = "../../../../../substrate/primitives/transaction-pool", default-features = false } +sp-version = { path = "../../../../../substrate/primitives/version", default-features = false } + +# Polkadot +rococo-runtime-constants = { path = "../../../../../polkadot/runtime/rococo/constants", default-features = false } +pallet-xcm = { path = "../../../../../polkadot/xcm/pallet-xcm", default-features = false } +pallet-xcm-benchmarks = { path = "../../../../../polkadot/xcm/pallet-xcm-benchmarks", default-features = false, optional = true } +polkadot-core-primitives = { path = "../../../../../polkadot/core-primitives", default-features = false } +polkadot-parachain-primitives = { path = "../../../../../polkadot/parachain", default-features = false } +polkadot-runtime-common = { path = "../../../../../polkadot/runtime/common", default-features = false } +xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false } +xcm-builder = { package = "staging-xcm-builder", path = "../../../../../polkadot/xcm/xcm-builder", default-features = false } +xcm-executor = { package = "staging-xcm-executor", path = "../../../../../polkadot/xcm/xcm-executor", default-features = false } + +# Cumulus +cumulus-pallet-aura-ext = { path = "../../../../../cumulus/pallets/aura-ext", default-features = false } +cumulus-pallet-dmp-queue = { path = "../../../../../cumulus/pallets/dmp-queue", default-features = false } +cumulus-pallet-parachain-system = { path = "../../../../../cumulus/pallets/parachain-system", default-features = false, features = ["parameterized-consensus-hook"] } +cumulus-pallet-session-benchmarking = { path = "../../../../../cumulus/pallets/session-benchmarking", default-features = false } +cumulus-pallet-xcm = { path = "../../../../../cumulus/pallets/xcm", default-features = false } +cumulus-pallet-xcmp-queue = { path = "../../../../../cumulus/pallets/xcmp-queue", default-features = false, features = ["bridging"] } +cumulus-primitives-core = { path = "../../../../../cumulus/primitives/core", default-features = false } +cumulus-primitives-utility = { path = "../../../../../cumulus/primitives/utility", default-features = false } +pallet-collator-selection = { path = "../../../../../cumulus/pallets/collator-selection", default-features = false } +parachain-info = { package = "staging-parachain-info", path = "../../../../../cumulus/parachains/pallets/parachain-info", default-features = false } +parachains-common = { path = "../../../../../cumulus/parachains/common", default-features = false } +parachains-runtimes-test-utils = { path = "../../../../../cumulus/parachains/runtimes/test-utils", default-features = false } +bridge-hub-rococo-runtime = { path = "../../../../../cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo", default-features = false } +asset-hub-rococo-runtime = { path = "../../../../../cumulus/parachains/runtimes/assets/asset-hub-rococo", default-features = false } +assets-common = { path = "../../../../../cumulus/parachains/runtimes/assets/common", default-features = false } + +# Ethereum Bridge (Snowbridge) +snowbridge-core = { path = "../../primitives/core", default-features = false } +snowbridge-beacon-primitives = { path = "../../primitives/beacon", default-features = false } +snowbridge-router-primitives = { path = "../../primitives/router", default-features = false } +snowbridge-ethereum-beacon-client = { path = "../../pallets/ethereum-beacon-client", default-features = false } +snowbridge-inbound-queue = { path = "../../pallets/inbound-queue", default-features = false } +snowbridge-outbound-queue = { path = "../../pallets/outbound-queue", default-features = false } +snowbridge-outbound-queue-runtime-api = { path = "../../pallets/outbound-queue/runtime-api", default-features = false } +snowbridge-system = { path = "../../pallets/system", default-features = false } +snowbridge-system-runtime-api = { path = "../../pallets/system/runtime-api", default-features = false } + +[dev-dependencies] +static_assertions = "1.1" +bridge-hub-test-utils = { path = "../../../../../cumulus/parachains/runtimes/bridge-hubs/test-utils" } +bridge-runtime-common = { path = "../../../../../bridges/bin/runtime-common", features = ["integrity-test"] } +sp-keyring = { path = "../../../../../substrate/primitives/keyring" } + +[features] +default = ["std"] +std = [ + "asset-hub-rococo-runtime/std", + "assets-common/std", + "bridge-hub-rococo-runtime/std", + "codec/std", + "cumulus-pallet-aura-ext/std", + "cumulus-pallet-dmp-queue/std", + "cumulus-pallet-parachain-system/std", + "cumulus-pallet-session-benchmarking/std", + "cumulus-pallet-xcm/std", + "cumulus-pallet-xcmp-queue/std", + "cumulus-primitives-core/std", + "cumulus-primitives-utility/std", + "frame-benchmarking/std", + "frame-executive/std", + "frame-support/std", + "frame-system-benchmarking?/std", + "frame-system-rpc-runtime-api/std", + "frame-system/std", + "frame-try-runtime?/std", + "log/std", + "pallet-aura/std", + "pallet-authorship/std", + "pallet-balances/std", + "pallet-collator-selection/std", + "pallet-message-queue/std", + "pallet-multisig/std", + "pallet-session/std", + "pallet-timestamp/std", + "pallet-transaction-payment-rpc-runtime-api/std", + "pallet-transaction-payment/std", + "pallet-utility/std", + "pallet-xcm-benchmarks?/std", + "pallet-xcm/std", + "parachain-info/std", + "parachains-common/std", + "parachains-runtimes-test-utils/std", + "polkadot-core-primitives/std", + "polkadot-parachain-primitives/std", + "polkadot-runtime-common/std", + "rococo-runtime-constants/std", + "scale-info/std", + "serde", + "snowbridge-beacon-primitives/std", + "snowbridge-core/std", + "snowbridge-ethereum-beacon-client/std", + "snowbridge-inbound-queue/std", + "snowbridge-outbound-queue-runtime-api/std", + "snowbridge-outbound-queue/std", + "snowbridge-router-primitives/std", + "snowbridge-system-runtime-api/std", + "snowbridge-system/std", + "sp-api/std", + "sp-block-builder/std", + "sp-consensus-aura/std", + "sp-core/std", + "sp-genesis-builder/std", + "sp-inherents/std", + "sp-io/std", + "sp-offchain/std", + "sp-runtime/std", + "sp-session/std", + "sp-std/std", + "sp-storage/std", + "sp-transaction-pool/std", + "sp-version/std", + "xcm-builder/std", + "xcm-executor/std", + "xcm/std", +] + +runtime-benchmarks = [ + "asset-hub-rococo-runtime/runtime-benchmarks", + "assets-common/runtime-benchmarks", + "bridge-hub-rococo-runtime/runtime-benchmarks", + "bridge-runtime-common/runtime-benchmarks", + "cumulus-pallet-dmp-queue/runtime-benchmarks", + "cumulus-pallet-parachain-system/runtime-benchmarks", + "cumulus-pallet-session-benchmarking/runtime-benchmarks", + "cumulus-pallet-xcmp-queue/runtime-benchmarks", + "cumulus-primitives-core/runtime-benchmarks", + "cumulus-primitives-utility/runtime-benchmarks", + "frame-benchmarking/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system-benchmarking/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "pallet-balances/runtime-benchmarks", + "pallet-collator-selection/runtime-benchmarks", + "pallet-message-queue/runtime-benchmarks", + "pallet-multisig/runtime-benchmarks", + "pallet-timestamp/runtime-benchmarks", + "pallet-utility/runtime-benchmarks", + "pallet-xcm-benchmarks/runtime-benchmarks", + "pallet-xcm/runtime-benchmarks", + "parachains-common/runtime-benchmarks", + "polkadot-parachain-primitives/runtime-benchmarks", + "polkadot-runtime-common/runtime-benchmarks", + "snowbridge-core/runtime-benchmarks", + "snowbridge-ethereum-beacon-client/runtime-benchmarks", + "snowbridge-inbound-queue/runtime-benchmarks", + "snowbridge-outbound-queue/runtime-benchmarks", + "snowbridge-router-primitives/runtime-benchmarks", + "snowbridge-system/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", + "xcm-builder/runtime-benchmarks", + "xcm-executor/runtime-benchmarks", +] + +try-runtime = [ + "asset-hub-rococo-runtime/try-runtime", + "bridge-hub-rococo-runtime/try-runtime", + "cumulus-pallet-aura-ext/try-runtime", + "cumulus-pallet-dmp-queue/try-runtime", + "cumulus-pallet-parachain-system/try-runtime", + "cumulus-pallet-xcm/try-runtime", + "cumulus-pallet-xcmp-queue/try-runtime", + "frame-executive/try-runtime", + "frame-support/try-runtime", + "frame-system/try-runtime", + "frame-try-runtime/try-runtime", + "pallet-aura/try-runtime", + "pallet-authorship/try-runtime", + "pallet-balances/try-runtime", + "pallet-collator-selection/try-runtime", + "pallet-message-queue/try-runtime", + "pallet-multisig/try-runtime", + "pallet-session/try-runtime", + "pallet-timestamp/try-runtime", + "pallet-transaction-payment/try-runtime", + "pallet-utility/try-runtime", + "pallet-xcm/try-runtime", + "parachain-info/try-runtime", + "polkadot-runtime-common/try-runtime", + "snowbridge-ethereum-beacon-client/try-runtime", + "snowbridge-inbound-queue/try-runtime", + "snowbridge-outbound-queue/try-runtime", + "snowbridge-system/try-runtime", + "sp-runtime/try-runtime", +] +beacon-spec-mainnet = [ + "snowbridge-ethereum-beacon-client/beacon-spec-mainnet", +] +experimental = ["pallet-aura/experimental"] + +# A feature that should be enabled when the runtime should be built for on-chain +# deployment. This will disable stuff that shouldn't be part of the on-chain wasm +# to make it smaller like logging for example. +on-chain-release-build = ["sp-api/disable-logging"] diff --git a/bridges/snowbridge/parachain/runtime/tests/src/lib.rs b/bridges/snowbridge/parachain/runtime/tests/src/lib.rs new file mode 100644 index 000000000000..9a5d12e28926 --- /dev/null +++ b/bridges/snowbridge/parachain/runtime/tests/src/lib.rs @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork + +#![cfg(test)] + +mod test_cases; + +use asset_hub_rococo_runtime::xcm_config::bridging::to_ethereum::DefaultBridgeHubEthereumBaseFee; +use bridge_hub_rococo_runtime::{ + xcm_config::XcmConfig, MessageQueueServiceWeight, Runtime, RuntimeEvent, SessionKeys, +}; +use codec::Decode; +use cumulus_primitives_core::XcmError::{FailedToTransactAsset, NotHoldingFees}; +use parachains_common::{AccountId, AuraId}; +use snowbridge_ethereum_beacon_client::WeightInfo; +use sp_core::H160; +use sp_keyring::AccountKeyring::Alice; + +pub fn collator_session_keys() -> bridge_hub_test_utils::CollatorSessionKeys { + bridge_hub_test_utils::CollatorSessionKeys::new( + AccountId::from(Alice), + AccountId::from(Alice), + SessionKeys { aura: AuraId::from(Alice.public()) }, + ) +} + +#[test] +pub fn transfer_token_to_ethereum_works() { + test_cases::send_transfer_token_message_success::( + collator_session_keys(), + 1013, + 1000, + H160::random(), + H160::random(), + DefaultBridgeHubEthereumBaseFee::get(), + Box::new(|runtime_event_encoded: Vec| { + match RuntimeEvent::decode(&mut &runtime_event_encoded[..]) { + Ok(RuntimeEvent::EthereumOutboundQueue(event)) => Some(event), + _ => None, + } + }), + ) +} + +#[test] +pub fn unpaid_transfer_token_to_ethereum_fails_with_barrier() { + test_cases::send_unpaid_transfer_token_message::( + collator_session_keys(), + 1013, + 1000, + H160::random(), + H160::random(), + ) +} + +#[test] +pub fn transfer_token_to_ethereum_fee_not_enough() { + test_cases::send_transfer_token_message_failure::( + collator_session_keys(), + 1013, + 1000, + DefaultBridgeHubEthereumBaseFee::get() + 1_000_000_000, + H160::random(), + H160::random(), + // fee not enough + 1_000_000_000, + NotHoldingFees, + ) +} + +#[test] +pub fn transfer_token_to_ethereum_insufficient_fund() { + test_cases::send_transfer_token_message_failure::( + collator_session_keys(), + 1013, + 1000, + 1_000_000_000, + H160::random(), + H160::random(), + DefaultBridgeHubEthereumBaseFee::get(), + FailedToTransactAsset("InsufficientBalance"), + ) +} + +#[test] +fn max_message_queue_service_weight_is_more_than_beacon_extrinsic_weights() { + let max_message_queue_weight = MessageQueueServiceWeight::get(); + let force_checkpoint = + ::WeightInfo::force_checkpoint(); + let submit_checkpoint = + ::WeightInfo::submit(); + max_message_queue_weight.all_gt(force_checkpoint); + max_message_queue_weight.all_gt(submit_checkpoint); +} diff --git a/bridges/snowbridge/parachain/runtime/tests/src/test_cases.rs b/bridges/snowbridge/parachain/runtime/tests/src/test_cases.rs new file mode 100644 index 000000000000..19e45f7a15a7 --- /dev/null +++ b/bridges/snowbridge/parachain/runtime/tests/src/test_cases.rs @@ -0,0 +1,293 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023 Snowfork + +//! Module contains predefined test-case scenarios for `Runtime` with bridging capabilities. + +use asset_hub_rococo_runtime::xcm_config::bridging::to_ethereum::DefaultBridgeHubEthereumBaseFee; +use bridge_hub_rococo_runtime::EthereumSystem; +use codec::Encode; +use frame_support::{assert_err, assert_ok, traits::fungible::Mutate}; +use parachains_runtimes_test_utils::{ + AccountIdOf, BalanceOf, CollatorSessionKeys, ExtBuilder, ValidatorIdOf, XcmReceivedFrom, +}; +use sp_core::H160; +use sp_runtime::SaturatedConversion; +use xcm::latest::prelude::*; +use xcm_executor::XcmExecutor; +// Re-export test_case from `parachains-runtimes-test-utils` +pub use parachains_runtimes_test_utils::test_cases::change_storage_constant_by_governance_works; +use xcm::v3::Error::{self, Barrier}; + +type RuntimeHelper = + parachains_runtimes_test_utils::RuntimeHelper; + +pub fn initial_fund(assethub_parachain_id: u32, initial_amount: u128) +where + Runtime: frame_system::Config + pallet_balances::Config, +{ + // fund asset hub sovereign account enough so it can pay fees + let asset_hub_sovereign_account = + snowbridge_core::sibling_sovereign_account::(assethub_parachain_id.into()); + >::mint_into( + &asset_hub_sovereign_account, + initial_amount.saturated_into::>(), + ) + .unwrap(); +} + +pub fn send_transfer_token_message( + assethub_parachain_id: u32, + weth_contract_address: H160, + destination_address: H160, + fee_amount: u128, +) -> Outcome +where + Runtime: frame_system::Config + + pallet_balances::Config + + pallet_session::Config + + pallet_xcm::Config + + parachain_info::Config + + pallet_collator_selection::Config + + cumulus_pallet_parachain_system::Config + + snowbridge_outbound_queue::Config, + XcmConfig: xcm_executor::Config, +{ + let assethub_parachain_location = MultiLocation::new(1, Parachain(assethub_parachain_id)); + let asset = MultiAsset { + id: Concrete(MultiLocation { + parents: 0, + interior: X1(AccountKey20 { network: None, key: weth_contract_address.into() }), + }), + fun: Fungible(1000000000), + }; + let assets = vec![asset.clone()]; + + let inner_xcm = Xcm(vec![ + WithdrawAsset(MultiAssets::from(assets.clone())), + ClearOrigin, + BuyExecution { fees: asset, weight_limit: Unlimited }, + DepositAsset { + assets: Wild(All), + beneficiary: MultiLocation { + parents: 0, + interior: X1(AccountKey20 { network: None, key: destination_address.into() }), + }, + }, + SetTopic([0; 32]), + ]); + + let fee = MultiAsset { + id: Concrete(MultiLocation { parents: 1, interior: Here }), + fun: Fungible(fee_amount), + }; + + // prepare transfer token message + let xcm = Xcm(vec![ + WithdrawAsset(MultiAssets::from(vec![fee.clone()])), + BuyExecution { fees: fee, weight_limit: Unlimited }, + ExportMessage { + network: Ethereum { chain_id: 11155111 }, + destination: Here, + xcm: inner_xcm, + }, + ]); + + // execute XCM + let hash = xcm.using_encoded(sp_io::hashing::blake2_256); + XcmExecutor::::execute_xcm( + assethub_parachain_location, + xcm, + hash, + RuntimeHelper::::xcm_max_weight(XcmReceivedFrom::Sibling), + ) +} + +pub fn send_transfer_token_message_success( + collator_session_key: CollatorSessionKeys, + runtime_para_id: u32, + assethub_parachain_id: u32, + weth_contract_address: H160, + destination_address: H160, + fee_amount: u128, + snowbridge_outbound_queue: Box< + dyn Fn(Vec) -> Option>, + >, +) where + Runtime: frame_system::Config + + pallet_balances::Config + + pallet_session::Config + + pallet_xcm::Config + + parachain_info::Config + + pallet_collator_selection::Config + + cumulus_pallet_parachain_system::Config + + snowbridge_outbound_queue::Config + + snowbridge_system::Config, + XcmConfig: xcm_executor::Config, + ValidatorIdOf: From>, +{ + ExtBuilder::::default() + .with_collators(collator_session_key.collators()) + .with_session_keys(collator_session_key.session_keys()) + .with_para_id(runtime_para_id.into()) + .with_tracing() + .build() + .execute_with(|| { + EthereumSystem::initialize(runtime_para_id.into(), assethub_parachain_id.into()) + .unwrap(); + + // fund asset hub sovereign account enough so it can pay fees + initial_fund::( + assethub_parachain_id, + DefaultBridgeHubEthereumBaseFee::get() + 1_000_000_000, + ); + + let outcome = send_transfer_token_message::( + assethub_parachain_id, + weth_contract_address, + destination_address, + fee_amount, + ); + + assert_ok!(outcome.ensure_complete()); + + // check events + let mut events = >::events() + .into_iter() + .filter_map(|e| snowbridge_outbound_queue(e.event.encode())); + assert!( + events.any(|e| matches!(e, snowbridge_outbound_queue::Event::MessageQueued { .. })) + ); + }); +} + +pub fn send_unpaid_transfer_token_message( + collator_session_key: CollatorSessionKeys, + runtime_para_id: u32, + assethub_parachain_id: u32, + weth_contract_address: H160, + destination_contract: H160, +) where + Runtime: frame_system::Config + + pallet_balances::Config + + pallet_session::Config + + pallet_xcm::Config + + parachain_info::Config + + pallet_collator_selection::Config + + cumulus_pallet_parachain_system::Config + + snowbridge_outbound_queue::Config, + XcmConfig: xcm_executor::Config, + ValidatorIdOf: From>, +{ + let assethub_parachain_location = MultiLocation::new(1, Parachain(assethub_parachain_id)); + + ExtBuilder::::default() + .with_collators(collator_session_key.collators()) + .with_session_keys(collator_session_key.session_keys()) + .with_para_id(runtime_para_id.into()) + .with_tracing() + .build() + .execute_with(|| { + let asset_hub_sovereign_account = + snowbridge_core::sibling_sovereign_account::(assethub_parachain_id.into()); + + >::mint_into( + &asset_hub_sovereign_account, + 4000000000u32.into(), + ) + .unwrap(); + + let asset = MultiAsset { + id: Concrete(MultiLocation { + parents: 0, + interior: X1(AccountKey20 { network: None, key: weth_contract_address.into() }), + }), + fun: Fungible(1000000000), + }; + let assets = vec![asset.clone()]; + + let inner_xcm = Xcm(vec![ + WithdrawAsset(MultiAssets::from(assets.clone())), + ClearOrigin, + BuyExecution { fees: asset, weight_limit: Unlimited }, + DepositAsset { + assets: Wild(AllCounted(1)), + beneficiary: MultiLocation { + parents: 0, + interior: X1(AccountKey20 { + network: None, + key: destination_contract.into(), + }), + }, + }, + SetTopic([0; 32]), + ]); + + // prepare transfer token message + let xcm = Xcm(vec![ + UnpaidExecution { weight_limit: Unlimited, check_origin: None }, + ExportMessage { + network: Ethereum { chain_id: 11155111 }, + destination: Here, + xcm: inner_xcm, + }, + ]); + + // execute XCM + let hash = xcm.using_encoded(sp_io::hashing::blake2_256); + let outcome = XcmExecutor::::execute_xcm( + assethub_parachain_location, + xcm, + hash, + RuntimeHelper::::xcm_max_weight(XcmReceivedFrom::Sibling), + ); + // check error is barrier + assert_err!(outcome.ensure_complete(), Barrier); + }); +} + +#[allow(clippy::too_many_arguments)] +pub fn send_transfer_token_message_failure( + collator_session_key: CollatorSessionKeys, + runtime_para_id: u32, + assethub_parachain_id: u32, + initial_amount: u128, + weth_contract_address: H160, + destination_address: H160, + fee_amount: u128, + expected_error: Error, +) where + Runtime: frame_system::Config + + pallet_balances::Config + + pallet_session::Config + + pallet_xcm::Config + + parachain_info::Config + + pallet_collator_selection::Config + + cumulus_pallet_parachain_system::Config + + snowbridge_outbound_queue::Config + + snowbridge_system::Config, + XcmConfig: xcm_executor::Config, + ValidatorIdOf: From>, +{ + ExtBuilder::::default() + .with_collators(collator_session_key.collators()) + .with_session_keys(collator_session_key.session_keys()) + .with_para_id(runtime_para_id.into()) + .with_tracing() + .build() + .execute_with(|| { + EthereumSystem::initialize(runtime_para_id.into(), assethub_parachain_id.into()) + .unwrap(); + + // fund asset hub sovereign account enough so it can pay fees + initial_fund::(assethub_parachain_id, initial_amount); + + let outcome = send_transfer_token_message::( + assethub_parachain_id, + weth_contract_address, + destination_address, + fee_amount, + ); + // check err is NotHoldingFees + assert_err!(outcome.ensure_complete(), expected_error); + }); +} diff --git a/bridges/snowbridge/parachain/scripts/benchmark.sh b/bridges/snowbridge/parachain/scripts/benchmark.sh new file mode 100755 index 000000000000..c47649b2eebe --- /dev/null +++ b/bridges/snowbridge/parachain/scripts/benchmark.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +# Example command for updating pallet benchmarking +pushd ../cumulus +cargo run --release --bin polkadot-parachain \ +--features runtime-benchmarks \ +-- \ +benchmark pallet \ +--chain=bridge-hub-rococo-dev \ +--pallet=snowbridge_ethereum_beacon_client \ +--extrinsic="*" \ +--execution=wasm --wasm-execution=compiled \ +--steps 50 --repeat 20 \ +--output ./parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_ethereum_beacon_client.rs +popd diff --git a/bridges/snowbridge/parachain/scripts/hexliteral.sh b/bridges/snowbridge/parachain/scripts/hexliteral.sh new file mode 100755 index 000000000000..e34a2b9b5151 --- /dev/null +++ b/bridges/snowbridge/parachain/scripts/hexliteral.sh @@ -0,0 +1,5 @@ +#!/bin/bash +# Creates a string constant from STDIN +echo "const DATA: &'static str = concat!(" +cat - | fold | sed 's/^.*/\t"&",/' +echo ");" \ No newline at end of file diff --git a/bridges/snowbridge/parachain/scripts/init.sh b/bridges/snowbridge/parachain/scripts/init.sh new file mode 100755 index 000000000000..1405a41ef333 --- /dev/null +++ b/bridges/snowbridge/parachain/scripts/init.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +set -e + +echo "*** Initializing WASM build environment" + +if [ -z $CI_PROJECT_NAME ] ; then + rustup update nightly + rustup update stable +fi + +rustup target add wasm32-unknown-unknown --toolchain nightly diff --git a/bridges/snowbridge/parachain/scripts/make-build-config.sh b/bridges/snowbridge/parachain/scripts/make-build-config.sh new file mode 100755 index 000000000000..a1b116a5dd0c --- /dev/null +++ b/bridges/snowbridge/parachain/scripts/make-build-config.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cd ../ethereum + +truffle exec scripts/dumpParachainConfig.js | sed '/^Using/d;/^$/d' diff --git a/bridges/snowbridge/parachain/scripts/verify-pallets-build.sh b/bridges/snowbridge/parachain/scripts/verify-pallets-build.sh new file mode 100755 index 000000000000..f060cf958b75 --- /dev/null +++ b/bridges/snowbridge/parachain/scripts/verify-pallets-build.sh @@ -0,0 +1,113 @@ +#!/bin/bash + +# A script to remove everything from snowbridge repository/subtree, except: +# +# - parachain +# - readme +# - license + +set -eu + +# show CLI help +function show_help() { + set +x + echo " " + echo Error: $1 + echo "Usage:" + echo " ./scripts/verify-pallets-build.sh Exit with code 0 if pallets code is well decoupled from the other code in the repo" + echo "Options:" + echo " --no-revert Leaves only runtime code on exit" + echo " --ignore-git-state Ignores git actual state" + exit 1 +} + +# parse CLI args +NO_REVERT= +IGNORE_GIT_STATE= +for i in "$@" +do + case $i in + --no-revert) + NO_REVERT=true + shift + ;; + --ignore-git-state) + IGNORE_GIT_STATE=true + shift + ;; + *) + show_help "Unknown option: $i" + ;; + esac +done + +# the script is able to work only on clean git copy, unless we want to ignore this check +[[ ! -z "${IGNORE_GIT_STATE}" ]] || [[ -z "$(git status --porcelain)" ]] || { echo >&2 "The git copy must be clean"; exit 1; } + +# let's avoid any restrictions on where this script can be called for - snowbridge repo may be +# plugged into any other repo folder. So the script (and other stuff that needs to be removed) +# may be located either in call dir, or one of it subdirs. +SNOWBRIDGE_FOLDER="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )/../.." + +# remove everything we think is not required for our needs +rm -rf $SNOWBRIDGE_FOLDER/.cargo +rm -rf $SNOWBRIDGE_FOLDER/.github +rm -rf $SNOWBRIDGE_FOLDER/contracts +rm -rf $SNOWBRIDGE_FOLDER/codecov.yml +rm -rf $SNOWBRIDGE_FOLDER/docs +rm -rf $SNOWBRIDGE_FOLDER/hooks +rm -rf $SNOWBRIDGE_FOLDER/relayer +rm -rf $SNOWBRIDGE_FOLDER/smoketest +rm -rf $SNOWBRIDGE_FOLDER/web +rm -rf $SNOWBRIDGE_FOLDER/.envrc-example +rm -rf $SNOWBRIDGE_FOLDER/.gitbook.yaml +rm -rf $SNOWBRIDGE_FOLDER/.gitignore +rm -rf $SNOWBRIDGE_FOLDER/.gitmodules +rm -rf $SNOWBRIDGE_FOLDER/_typos.toml +rm -rf $SNOWBRIDGE_FOLDER/_codecov.yml +rm -rf $SNOWBRIDGE_FOLDER/flake.lock +rm -rf $SNOWBRIDGE_FOLDER/flake.nix +rm -rf $SNOWBRIDGE_FOLDER/go.work +rm -rf $SNOWBRIDGE_FOLDER/go.work.sum +rm -rf $SNOWBRIDGE_FOLDER/polkadot-sdk +rm -rf $SNOWBRIDGE_FOLDER/rust-toolchain.toml +rm -rf $SNOWBRIDGE_FOLDER/parachain/rustfmt.toml +rm -rf $SNOWBRIDGE_FOLDER/parachain/.gitignore +rm -rf $SNOWBRIDGE_FOLDER/parachain/templates +rm -rf $SNOWBRIDGE_FOLDER/parachain/.config +rm -rf $SNOWBRIDGE_FOLDER/parachain/pallets/ethereum-beacon-client/fuzz + +cd bridges/snowbridge/parachain + +# fix polkadot-sdk paths in Cargo.toml files +find "." -name 'Cargo.toml' | while read -r file; do + replace=$(printf '../../' ) + if [[ "$(uname)" = "Darwin" ]] || [[ "$(uname)" = *BSD ]]; then + sed -i '' "s|polkadot-sdk/|$replace|g" "$file" + else + sed -i "s|polkadot-sdk/|$replace|g" "$file" + fi +done + +# let's test if everything we need compiles +cargo check -p snowbridge-ethereum-beacon-client +cargo check -p snowbridge-ethereum-beacon-client --features runtime-benchmarks +cargo check -p snowbridge-ethereum-beacon-client --features try-runtime +cargo check -p snowbridge-inbound-queue +cargo check -p snowbridge-inbound-queue --features runtime-benchmarks +cargo check -p snowbridge-inbound-queue --features try-runtime +cargo check -p snowbridge-outbound-queue +cargo check -p snowbridge-outbound-queue --features runtime-benchmarks +cargo check -p snowbridge-outbound-queue --features try-runtime +cargo check -p snowbridge-system +cargo check -p snowbridge-system --features runtime-benchmarks +cargo check -p snowbridge-system --features try-runtime + +cd - + +# we're removing lock file after all checks are done. Otherwise we may use different +# Substrate/Polkadot/Cumulus commits and our checks will fail +rm -f $SNOWBRIDGE_FOLDER/parachain/Cargo.toml +rm -f $SNOWBRIDGE_FOLDER/parachain/Cargo.lock + +echo "OK" diff --git a/cumulus/parachains/integration-tests/emulated/chains/parachains/assets/asset-hub-rococo/src/lib.rs b/cumulus/parachains/integration-tests/emulated/chains/parachains/assets/asset-hub-rococo/src/lib.rs index 05454a2e5736..00f412564205 100644 --- a/cumulus/parachains/integration-tests/emulated/chains/parachains/assets/asset-hub-rococo/src/lib.rs +++ b/cumulus/parachains/integration-tests/emulated/chains/parachains/assets/asset-hub-rococo/src/lib.rs @@ -38,6 +38,7 @@ decl_test_parachains! { XcmpMessageHandler: asset_hub_rococo_runtime::XcmpQueue, LocationToAccountId: asset_hub_rococo_runtime::xcm_config::LocationToAccountId, ParachainInfo: asset_hub_rococo_runtime::ParachainInfo, + MessageOrigin: cumulus_primitives_core::AggregateMessageOrigin, }, pallets = { PolkadotXcm: asset_hub_rococo_runtime::PolkadotXcm, diff --git a/cumulus/parachains/integration-tests/emulated/chains/parachains/assets/asset-hub-westend/src/lib.rs b/cumulus/parachains/integration-tests/emulated/chains/parachains/assets/asset-hub-westend/src/lib.rs index 56382fad5641..25d7c1079b4d 100644 --- a/cumulus/parachains/integration-tests/emulated/chains/parachains/assets/asset-hub-westend/src/lib.rs +++ b/cumulus/parachains/integration-tests/emulated/chains/parachains/assets/asset-hub-westend/src/lib.rs @@ -38,6 +38,7 @@ decl_test_parachains! { XcmpMessageHandler: asset_hub_westend_runtime::XcmpQueue, LocationToAccountId: asset_hub_westend_runtime::xcm_config::LocationToAccountId, ParachainInfo: asset_hub_westend_runtime::ParachainInfo, + MessageOrigin: cumulus_primitives_core::AggregateMessageOrigin, }, pallets = { PolkadotXcm: asset_hub_westend_runtime::PolkadotXcm, diff --git a/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-rococo/Cargo.toml b/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-rococo/Cargo.toml index 8a56bb7b27f4..d0c498b54b4e 100644 --- a/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-rococo/Cargo.toml +++ b/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-rococo/Cargo.toml @@ -25,3 +25,11 @@ parachains-common = { path = "../../../../../../../parachains/common" } cumulus-primitives-core = { path = "../../../../../../../primitives/core", default-features = false } emulated-integration-tests-common = { path = "../../../../common", default-features = false } bridge-hub-rococo-runtime = { path = "../../../../../../runtimes/bridge-hubs/bridge-hub-rococo" } +bridge-hub-common = { path = "../../../../../../runtimes/bridge-hubs/common", default-features = false } + +# Snowbridge +snowbridge-core = { path = "../../../../../../../../bridges/snowbridge/parachain/primitives/core", default-features = false } +snowbridge-router-primitives = { path = "../../../../../../../../bridges/snowbridge/parachain/primitives/router", default-features = false } +snowbridge-system = { path = "../../../../../../../../bridges/snowbridge/parachain/pallets/system", default-features = false } +snowbridge-inbound-queue = { path = "../../../../../../../../bridges/snowbridge/parachain/pallets/inbound-queue", default-features = false } +snowbridge-outbound-queue = { path = "../../../../../../../../bridges/snowbridge/parachain/pallets/outbound-queue", default-features = false } diff --git a/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-rococo/src/genesis.rs b/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-rococo/src/genesis.rs index fa9a287adf88..3dd0cb10ab69 100644 --- a/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-rococo/src/genesis.rs +++ b/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-rococo/src/genesis.rs @@ -22,6 +22,7 @@ use emulated_integration_tests_common::{ }; use parachains_common::Balance; +pub const ASSETHUB_PARA_ID: u32 = 1000; pub const PARA_ID: u32 = 1013; pub const ED: Balance = parachains_common::rococo::currency::EXISTENTIAL_DEPOSIT; @@ -64,6 +65,11 @@ pub fn genesis() -> Storage { owner: Some(get_account_id_from_seed::(accounts::BOB)), ..Default::default() }, + ethereum_system: bridge_hub_rococo_runtime::EthereumSystemConfig { + para_id: PARA_ID.into(), + asset_hub_para_id: ASSETHUB_PARA_ID.into(), + ..Default::default() + }, ..Default::default() }; diff --git a/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-rococo/src/lib.rs b/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-rococo/src/lib.rs index 8162823dfceb..8c18d112bc12 100644 --- a/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-rococo/src/lib.rs +++ b/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-rococo/src/lib.rs @@ -36,10 +36,14 @@ decl_test_parachains! { XcmpMessageHandler: bridge_hub_rococo_runtime::XcmpQueue, LocationToAccountId: bridge_hub_rococo_runtime::xcm_config::LocationToAccountId, ParachainInfo: bridge_hub_rococo_runtime::ParachainInfo, + MessageOrigin: bridge_hub_common::AggregateMessageOrigin, }, pallets = { PolkadotXcm: bridge_hub_rococo_runtime::PolkadotXcm, Balances: bridge_hub_rococo_runtime::Balances, + EthereumSystem: bridge_hub_rococo_runtime::EthereumSystem, + EthereumInboundQueue: bridge_hub_rococo_runtime::EthereumInboundQueue, + EthereumOutboundQueue: bridge_hub_rococo_runtime::EthereumOutboundQueue, } }, } diff --git a/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-westend/Cargo.toml b/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-westend/Cargo.toml index a2268f3b17a6..3d5a7e1071d5 100644 --- a/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-westend/Cargo.toml +++ b/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-westend/Cargo.toml @@ -25,3 +25,4 @@ parachains-common = { path = "../../../../../../../parachains/common" } cumulus-primitives-core = { path = "../../../../../../../primitives/core", default-features = false } emulated-integration-tests-common = { path = "../../../../common", default-features = false } bridge-hub-westend-runtime = { path = "../../../../../../runtimes/bridge-hubs/bridge-hub-westend" } +bridge-hub-common = { path = "../../../../../../runtimes/bridge-hubs/common", default-features = false } diff --git a/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-westend/src/lib.rs b/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-westend/src/lib.rs index c996b8045e7e..b0dddc9dbf9a 100644 --- a/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-westend/src/lib.rs +++ b/cumulus/parachains/integration-tests/emulated/chains/parachains/bridges/bridge-hub-westend/src/lib.rs @@ -36,6 +36,7 @@ decl_test_parachains! { XcmpMessageHandler: bridge_hub_westend_runtime::XcmpQueue, LocationToAccountId: bridge_hub_westend_runtime::xcm_config::LocationToAccountId, ParachainInfo: bridge_hub_westend_runtime::ParachainInfo, + MessageOrigin: bridge_hub_common::AggregateMessageOrigin, }, pallets = { PolkadotXcm: bridge_hub_westend_runtime::PolkadotXcm, diff --git a/cumulus/parachains/integration-tests/emulated/chains/parachains/collectives/collectives-westend/src/lib.rs b/cumulus/parachains/integration-tests/emulated/chains/parachains/collectives/collectives-westend/src/lib.rs index 5d553b6f1034..a32e865dd9ce 100644 --- a/cumulus/parachains/integration-tests/emulated/chains/parachains/collectives/collectives-westend/src/lib.rs +++ b/cumulus/parachains/integration-tests/emulated/chains/parachains/collectives/collectives-westend/src/lib.rs @@ -36,6 +36,7 @@ decl_test_parachains! { XcmpMessageHandler: collectives_westend_runtime::XcmpQueue, LocationToAccountId: collectives_westend_runtime::xcm_config::LocationToAccountId, ParachainInfo: collectives_westend_runtime::ParachainInfo, + MessageOrigin: cumulus_primitives_core::AggregateMessageOrigin, }, pallets = { PolkadotXcm: collectives_westend_runtime::PolkadotXcm, diff --git a/cumulus/parachains/integration-tests/emulated/chains/parachains/testing/penpal/src/lib.rs b/cumulus/parachains/integration-tests/emulated/chains/parachains/testing/penpal/src/lib.rs index 62bafb5cb30f..244a846bbc2f 100644 --- a/cumulus/parachains/integration-tests/emulated/chains/parachains/testing/penpal/src/lib.rs +++ b/cumulus/parachains/integration-tests/emulated/chains/parachains/testing/penpal/src/lib.rs @@ -40,10 +40,12 @@ decl_test_parachains! { XcmpMessageHandler: penpal_runtime::XcmpQueue, LocationToAccountId: penpal_runtime::xcm_config::LocationToAccountId, ParachainInfo: penpal_runtime::ParachainInfo, + MessageOrigin: cumulus_primitives_core::AggregateMessageOrigin, }, pallets = { PolkadotXcm: penpal_runtime::PolkadotXcm, Assets: penpal_runtime::Assets, + ForeignAssets: penpal_runtime::ForeignAssets, Balances: penpal_runtime::Balances, } }, @@ -57,10 +59,12 @@ decl_test_parachains! { XcmpMessageHandler: penpal_runtime::XcmpQueue, LocationToAccountId: penpal_runtime::xcm_config::LocationToAccountId, ParachainInfo: penpal_runtime::ParachainInfo, + MessageOrigin: cumulus_primitives_core::AggregateMessageOrigin, }, pallets = { PolkadotXcm: penpal_runtime::PolkadotXcm, Assets: penpal_runtime::Assets, + ForeignAssets: penpal_runtime::ForeignAssets, Balances: penpal_runtime::Balances, } }, diff --git a/cumulus/parachains/integration-tests/emulated/networks/rococo-westend-system/Cargo.toml b/cumulus/parachains/integration-tests/emulated/networks/rococo-westend-system/Cargo.toml index 2a538b8e28c5..744cbe4f8c1e 100644 --- a/cumulus/parachains/integration-tests/emulated/networks/rococo-westend-system/Cargo.toml +++ b/cumulus/parachains/integration-tests/emulated/networks/rococo-westend-system/Cargo.toml @@ -19,3 +19,4 @@ asset-hub-rococo-emulated-chain = { path = "../../chains/parachains/assets/asset asset-hub-westend-emulated-chain = { path = "../../chains/parachains/assets/asset-hub-westend" } bridge-hub-rococo-emulated-chain = { path = "../../chains/parachains/bridges/bridge-hub-rococo" } bridge-hub-westend-emulated-chain = { path = "../../chains/parachains/bridges/bridge-hub-westend" } +penpal-emulated-chain = { path = "../../chains/parachains/testing/penpal" } diff --git a/cumulus/parachains/integration-tests/emulated/networks/rococo-westend-system/src/lib.rs b/cumulus/parachains/integration-tests/emulated/networks/rococo-westend-system/src/lib.rs index b03ff692b952..ee8b038a364d 100644 --- a/cumulus/parachains/integration-tests/emulated/networks/rococo-westend-system/src/lib.rs +++ b/cumulus/parachains/integration-tests/emulated/networks/rococo-westend-system/src/lib.rs @@ -17,6 +17,7 @@ pub use asset_hub_rococo_emulated_chain; pub use asset_hub_westend_emulated_chain; pub use bridge_hub_rococo_emulated_chain; pub use bridge_hub_westend_emulated_chain; +pub use penpal_emulated_chain; pub use rococo_emulated_chain; pub use westend_emulated_chain; @@ -24,6 +25,7 @@ use asset_hub_rococo_emulated_chain::AssetHubRococo; use asset_hub_westend_emulated_chain::AssetHubWestend; use bridge_hub_rococo_emulated_chain::BridgeHubRococo; use bridge_hub_westend_emulated_chain::BridgeHubWestend; +use penpal_emulated_chain::PenpalA; use rococo_emulated_chain::Rococo; use westend_emulated_chain::Westend; @@ -43,6 +45,7 @@ decl_test_networks! { parachains = vec![ AssetHubRococo, BridgeHubRococo, + PenpalA, ], bridge = RococoWestendMockBridge @@ -92,5 +95,6 @@ decl_test_sender_receiver_accounts_parameter_types! { BridgeHubRococoPara { sender: ALICE, receiver: BOB }, WestendRelay { sender: ALICE, receiver: BOB }, AssetHubWestendPara { sender: ALICE, receiver: BOB }, - BridgeHubWestendPara { sender: ALICE, receiver: BOB } + BridgeHubWestendPara { sender: ALICE, receiver: BOB }, + PenpalAPara { sender: ALICE, receiver: BOB } } diff --git a/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/Cargo.toml b/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/Cargo.toml index ce6b8c24a44a..e75187bea95e 100644 --- a/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/Cargo.toml +++ b/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/Cargo.toml @@ -12,8 +12,12 @@ workspace = true [dependencies] codec = { package = "parity-scale-codec", version = "3.4.0", default-features = false } +scale-info = { version = "2.10.0", default-features = false, features = ["derive"] } +hex = "0.4.3" +hex-literal = "0.4.1" # Substrate +sp-core = { path = "../../../../../../../substrate/primitives/core", default-features = false } frame-support = { path = "../../../../../../../substrate/frame/support", default-features = false } pallet-assets = { path = "../../../../../../../substrate/frame/assets", default-features = false } pallet-balances = { path = "../../../../../../../substrate/frame/balances", default-features = false } @@ -37,3 +41,14 @@ cumulus-pallet-dmp-queue = { path = "../../../../../../pallets/dmp-queue", defau bridge-hub-rococo-runtime = { path = "../../../../../../parachains/runtimes/bridge-hubs/bridge-hub-rococo", default-features = false } emulated-integration-tests-common = { path = "../../../common", default-features = false } rococo-westend-system-emulated-network = { path = "../../../networks/rococo-westend-system" } +penpal-runtime = { path = "../../../../../runtimes/testing/penpal", default-features = false } +rococo-system-emulated-network = { path = "../../../networks/rococo-system" } +asset-hub-rococo-runtime = { path = "../../../../../runtimes/assets/asset-hub-rococo", default-features = false } + +# Snowbridge +snowbridge-core = { path = "../../../../../../../bridges/snowbridge/parachain/primitives/core", default-features = false } +snowbridge-router-primitives = { path = "../../../../../../../bridges/snowbridge/parachain/primitives/router", default-features = false } +snowbridge-system = { path = "../../../../../../../bridges/snowbridge/parachain/pallets/system", default-features = false } +snowbridge-inbound-queue = { path = "../../../../../../../bridges/snowbridge/parachain/pallets/inbound-queue", default-features = false } +snowbridge-outbound-queue = { path = "../../../../../../../bridges/snowbridge/parachain/pallets/outbound-queue", default-features = false } +snowbridge-rococo-common = { path = "../../../../../../../bridges/snowbridge/parachain/runtime/rococo-common", default-features = false } diff --git a/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/lib.rs b/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/lib.rs index 4ae2c6cc9025..5127bd759dc6 100644 --- a/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/lib.rs +++ b/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/lib.rs @@ -43,6 +43,11 @@ pub use emulated_integration_tests_common::{ PROOF_SIZE_THRESHOLD, REF_TIME_THRESHOLD, XCM_V3, }; pub use parachains_common::{AccountId, Balance}; +pub use rococo_system_emulated_network::{ + penpal_emulated_chain::PenpalAParaPallet as PenpalAPallet, + BridgeHubRococoParaReceiver as BridgeHubRococoReceiver, PenpalAPara as PenpalA, + PenpalAParaReceiver as PenpalAReceiver, PenpalAParaSender as PenpalASender, +}; pub use rococo_westend_system_emulated_network::{ asset_hub_rococo_emulated_chain::{ genesis::ED as ASSET_HUB_ROCOCO_ED, AssetHubRococoParaPallet as AssetHubRococoPallet, @@ -53,12 +58,13 @@ pub use rococo_westend_system_emulated_network::{ bridge_hub_rococo_emulated_chain::{ genesis::ED as BRIDGE_HUB_ROCOCO_ED, BridgeHubRococoParaPallet as BridgeHubRococoPallet, }, - rococo_emulated_chain::RococoRelayPallet as RococoPallet, + rococo_emulated_chain::{genesis::ED as ROCOCO_ED, RococoRelayPallet as RococoPallet}, AssetHubRococoPara as AssetHubRococo, AssetHubRococoParaReceiver as AssetHubRococoReceiver, AssetHubRococoParaSender as AssetHubRococoSender, AssetHubWestendPara as AssetHubWestend, AssetHubWestendParaReceiver as AssetHubWestendReceiver, BridgeHubRococoPara as BridgeHubRococo, BridgeHubRococoParaSender as BridgeHubRococoSender, BridgeHubWestendPara as BridgeHubWestend, - RococoRelay as Rococo, + RococoRelay as Rococo, RococoRelayReceiver as RococoReceiver, + RococoRelaySender as RococoSender, }; pub const ASSET_ID: u32 = 1; diff --git a/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/tests/mod.rs b/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/tests/mod.rs index d102dd2e5d69..e71a022af4cf 100644 --- a/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/tests/mod.rs +++ b/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/tests/mod.rs @@ -17,6 +17,7 @@ use crate::*; mod asset_transfers; mod send_xcm; +mod snowbridge; mod teleport; pub(crate) fn asset_hub_westend_location() -> MultiLocation { diff --git a/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/tests/snowbridge.rs b/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/tests/snowbridge.rs new file mode 100644 index 000000000000..e62a73caff58 --- /dev/null +++ b/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/tests/snowbridge.rs @@ -0,0 +1,505 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +use crate::*; +use codec::{Decode, Encode}; +use emulated_integration_tests_common::xcm_emulator::ConvertLocation; +use frame_support::pallet_prelude::TypeInfo; +use hex_literal::hex; +use snowbridge_core::outbound::OperatingMode; +use snowbridge_rococo_common::EthereumNetwork; +use snowbridge_router_primitives::inbound::{ + Command, Destination, GlobalConsensusEthereumConvertsFor, MessageV1, VersionedMessage, +}; +use snowbridge_system; +use sp_core::H256; + +const INITIAL_FUND: u128 = 5_000_000_000 * ROCOCO_ED; +const CHAIN_ID: u64 = 11155111; +const TREASURY_ACCOUNT: [u8; 32] = + hex!("6d6f646c70792f74727372790000000000000000000000000000000000000000"); +const WETH: [u8; 20] = hex!("87d1f7fdfEe7f651FaBc8bFCB6E086C278b77A7d"); +const ETHEREUM_DESTINATION_ADDRESS: [u8; 20] = hex!("44a57ee2f2FCcb85FDa2B0B18EBD0D8D2333700e"); +const XCM_FEE: u128 = 4_000_000_000; + +#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] +pub enum ControlCall { + #[codec(index = 3)] + CreateAgent, + #[codec(index = 4)] + CreateChannel { mode: OperatingMode }, +} + +#[allow(clippy::large_enum_variant)] +#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] +pub enum SnowbridgeControl { + #[codec(index = 83)] + Control(ControlCall), +} + +#[test] +fn create_agent() { + let origin_para: u32 = 1001; + + BridgeHubRococo::fund_para_sovereign(origin_para.into(), INITIAL_FUND); + + let sudo_origin = ::RuntimeOrigin::root(); + let destination = Rococo::child_location_of(BridgeHubRococo::para_id()).into(); + + let create_agent_call = SnowbridgeControl::Control(ControlCall::CreateAgent {}); + + let remote_xcm = VersionedXcm::from(Xcm(vec![ + UnpaidExecution { weight_limit: Unlimited, check_origin: None }, + DescendOrigin(X1(Parachain(origin_para))), + Transact { + require_weight_at_most: 3000000000.into(), + origin_kind: OriginKind::Xcm, + call: create_agent_call.encode().into(), + }, + ])); + + //Rococo Global Consensus + // Send XCM message from Relay Chain to Bridge Hub source Parachain + Rococo::execute_with(|| { + assert_ok!(::XcmPallet::send( + sudo_origin, + bx!(destination), + bx!(remote_xcm), + )); + + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + Rococo, + vec![ + RuntimeEvent::XcmPallet(pallet_xcm::Event::Sent { .. }) => {}, + ] + ); + }); + + BridgeHubRococo::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + BridgeHubRococo, + vec![ + RuntimeEvent::EthereumSystem(snowbridge_system::Event::CreateAgent { + .. + }) => {}, + ] + ); + }); +} + +#[test] +fn create_channel() { + let origin_para: u32 = 1001; + + BridgeHubRococo::fund_para_sovereign(origin_para.into(), INITIAL_FUND); + + let sudo_origin = ::RuntimeOrigin::root(); + let destination: VersionedMultiLocation = + Rococo::child_location_of(BridgeHubRococo::para_id()).into(); + + let create_agent_call = SnowbridgeControl::Control(ControlCall::CreateAgent {}); + + let create_agent_xcm = VersionedXcm::from(Xcm(vec![ + UnpaidExecution { weight_limit: Unlimited, check_origin: None }, + DescendOrigin(X1(Parachain(origin_para))), + Transact { + require_weight_at_most: 3000000000.into(), + origin_kind: OriginKind::Xcm, + call: create_agent_call.encode().into(), + }, + ])); + + let create_channel_call = + SnowbridgeControl::Control(ControlCall::CreateChannel { mode: OperatingMode::Normal }); + + let create_channel_xcm = VersionedXcm::from(Xcm(vec![ + UnpaidExecution { weight_limit: Unlimited, check_origin: None }, + DescendOrigin(X1(Parachain(origin_para))), + Transact { + require_weight_at_most: 3000000000.into(), + origin_kind: OriginKind::Xcm, + call: create_channel_call.encode().into(), + }, + ])); + + //Rococo Global Consensus + // Send XCM message from Relay Chain to Bridge Hub source Parachain + Rococo::execute_with(|| { + assert_ok!(::XcmPallet::send( + sudo_origin.clone(), + bx!(destination.clone()), + bx!(create_agent_xcm), + )); + + assert_ok!(::XcmPallet::send( + sudo_origin, + bx!(destination), + bx!(create_channel_xcm), + )); + + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + Rococo, + vec![ + RuntimeEvent::XcmPallet(pallet_xcm::Event::Sent { .. }) => {}, + ] + ); + }); + + BridgeHubRococo::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + BridgeHubRococo, + vec![ + RuntimeEvent::EthereumSystem(snowbridge_system::Event::CreateChannel { + .. + }) => {}, + ] + ); + }); +} + +#[test] +fn register_weth_token_from_ethereum_to_asset_hub() { + BridgeHubRococo::fund_para_sovereign(AssetHubRococo::para_id().into(), INITIAL_FUND); + + let message_id_: H256 = [1; 32].into(); + + BridgeHubRococo::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + type EthereumInboundQueue = + ::EthereumInboundQueue; + let message = VersionedMessage::V1(MessageV1 { + chain_id: CHAIN_ID, + command: Command::RegisterToken { token: WETH.into(), fee: XCM_FEE }, + }); + let (xcm, _) = EthereumInboundQueue::do_convert(message_id_, message).unwrap(); + let _ = EthereumInboundQueue::send_xcm(xcm, AssetHubRococo::para_id().into()).unwrap(); + + assert_expected_events!( + BridgeHubRococo, + vec![ + RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {}, + ] + ); + }); + + AssetHubRococo::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + AssetHubRococo, + vec![ + RuntimeEvent::ForeignAssets(pallet_assets::Event::Created { .. }) => {}, + ] + ); + }); +} + +#[test] +fn send_token_from_ethereum_to_penpal() { + let asset_hub_sovereign = BridgeHubRococo::sovereign_account_id_of(MultiLocation { + parents: 1, + interior: X1(Parachain(AssetHubRococo::para_id().into())), + }); + BridgeHubRococo::fund_accounts(vec![(asset_hub_sovereign.clone(), INITIAL_FUND)]); + + PenpalA::fund_accounts(vec![ + (PenpalAReceiver::get(), INITIAL_FUND), + (PenpalASender::get(), INITIAL_FUND), + ]); + + let weth_asset_location: MultiLocation = + (Parent, Parent, EthereumNetwork::get(), AccountKey20 { network: None, key: WETH }).into(); + let weth_asset_id = weth_asset_location.into(); + + let origin_location = (Parent, Parent, EthereumNetwork::get()).into(); + + // Fund ethereum sovereign in asset hub + let ethereum_sovereign: AccountId = + GlobalConsensusEthereumConvertsFor::::convert_location(&origin_location) + .unwrap(); + AssetHubRococo::fund_accounts(vec![(ethereum_sovereign.clone(), INITIAL_FUND)]); + + // Create asset on assethub. + AssetHubRococo::execute_with(|| { + assert_ok!(::ForeignAssets::create( + pallet_xcm::Origin::Xcm(origin_location).into(), + weth_asset_id, + asset_hub_sovereign.clone().into(), + 1000, + )); + + assert!(::ForeignAssets::asset_exists( + weth_asset_id + )); + }); + + // Create asset on penpal. + PenpalA::execute_with(|| { + assert_ok!(::ForeignAssets::create( + ::RuntimeOrigin::signed(PenpalASender::get()), + weth_asset_id, + asset_hub_sovereign.into(), + 1000, + )); + + assert!(::ForeignAssets::asset_exists(weth_asset_id)); + }); + + let message_id_: H256 = [1; 32].into(); + + BridgeHubRococo::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + type EthereumInboundQueue = + ::EthereumInboundQueue; + let message = VersionedMessage::V1(MessageV1 { + chain_id: CHAIN_ID, + command: Command::SendToken { + token: WETH.into(), + destination: Destination::ForeignAccountId32 { + para_id: 2000, + id: PenpalAReceiver::get().into(), + fee: XCM_FEE, + }, + amount: 1_000_000_000, + fee: XCM_FEE, + }, + }); + let (xcm, _) = EthereumInboundQueue::do_convert(message_id_, message).unwrap(); + let _ = EthereumInboundQueue::send_xcm(xcm, AssetHubRococo::para_id().into()).unwrap(); + + assert_expected_events!( + BridgeHubRococo, + vec![ + RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {}, + ] + ); + }); + + AssetHubRococo::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + AssetHubRococo, + vec![ + RuntimeEvent::ForeignAssets(pallet_assets::Event::Issued { .. }) => {}, + RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {}, + ] + ); + }); + + PenpalA::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + PenpalA, + vec![ + RuntimeEvent::ForeignAssets(pallet_assets::Event::Issued { .. }) => {}, + ] + ); + }); +} + +#[test] +fn send_token_from_ethereum_to_asset_hub() { + BridgeHubRococo::fund_para_sovereign(AssetHubRococo::para_id().into(), INITIAL_FUND); + + // Fund ethereum sovereign in asset hub + AssetHubRococo::fund_accounts(vec![(AssetHubRococoReceiver::get(), INITIAL_FUND)]); + + let message_id_: H256 = [1; 32].into(); + + BridgeHubRococo::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + type EthereumInboundQueue = + ::EthereumInboundQueue; + let message = VersionedMessage::V1(MessageV1 { + chain_id: CHAIN_ID, + command: Command::RegisterToken { token: WETH.into(), fee: XCM_FEE }, + }); + let (xcm, _) = EthereumInboundQueue::do_convert(message_id_, message).unwrap(); + let _ = EthereumInboundQueue::send_xcm(xcm, AssetHubRococo::para_id().into()).unwrap(); + let message = VersionedMessage::V1(MessageV1 { + chain_id: CHAIN_ID, + command: Command::SendToken { + token: WETH.into(), + destination: Destination::AccountId32 { id: AssetHubRococoReceiver::get().into() }, + amount: 1_000_000_000, + fee: XCM_FEE, + }, + }); + let (xcm, _) = EthereumInboundQueue::do_convert(message_id_, message).unwrap(); + let _ = EthereumInboundQueue::send_xcm(xcm, AssetHubRococo::para_id().into()).unwrap(); + + assert_expected_events!( + BridgeHubRococo, + vec![ + RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {}, + ] + ); + }); + + AssetHubRococo::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + AssetHubRococo, + vec![ + RuntimeEvent::ForeignAssets(pallet_assets::Event::Issued { .. }) => {}, + ] + ); + }); +} + +#[test] +fn send_weth_asset_from_asset_hub_to_ethereum() { + use asset_hub_rococo_runtime::xcm_config::bridging::to_ethereum::DefaultBridgeHubEthereumBaseFee; + let assethub_sovereign = BridgeHubRococo::sovereign_account_id_of(MultiLocation { + parents: 1, + interior: X1(Parachain(AssetHubRococo::para_id().into())), + }); + + AssetHubRococo::force_default_xcm_version(Some(XCM_VERSION)); + BridgeHubRococo::force_default_xcm_version(Some(XCM_VERSION)); + AssetHubRococo::force_xcm_version( + MultiLocation { + parents: 2, + interior: X1(GlobalConsensus(Ethereum { chain_id: CHAIN_ID })), + }, + XCM_VERSION, + ); + + BridgeHubRococo::fund_accounts(vec![(assethub_sovereign.clone(), INITIAL_FUND)]); + AssetHubRococo::fund_accounts(vec![(AssetHubRococoReceiver::get(), INITIAL_FUND)]); + + const WETH_AMOUNT: u128 = 1_000_000_000; + let message_id_: H256 = [1; 32].into(); + + BridgeHubRococo::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + type EthereumInboundQueue = + ::EthereumInboundQueue; + + let message = VersionedMessage::V1(MessageV1 { + chain_id: CHAIN_ID, + command: Command::RegisterToken { token: WETH.into(), fee: XCM_FEE }, + }); + let (xcm, _) = EthereumInboundQueue::do_convert(message_id_, message).unwrap(); + let _ = EthereumInboundQueue::send_xcm(xcm, AssetHubRococo::para_id().into()).unwrap(); + let message = VersionedMessage::V1(MessageV1 { + chain_id: CHAIN_ID, + command: Command::SendToken { + token: WETH.into(), + destination: Destination::AccountId32 { id: AssetHubRococoReceiver::get().into() }, + amount: WETH_AMOUNT, + fee: XCM_FEE, + }, + }); + let (xcm, _) = EthereumInboundQueue::do_convert(message_id_, message).unwrap(); + let _ = EthereumInboundQueue::send_xcm(xcm, AssetHubRococo::para_id().into()).unwrap(); + + assert_expected_events!( + BridgeHubRococo, + vec![ + RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {}, + ] + ); + }); + + AssetHubRococo::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + type RuntimeOrigin = ::RuntimeOrigin; + + assert_expected_events!( + AssetHubRococo, + vec![ + RuntimeEvent::ForeignAssets(pallet_assets::Event::Issued { .. }) => {}, + ] + ); + let assets = vec![MultiAsset { + id: Concrete(MultiLocation { + parents: 2, + interior: X2( + GlobalConsensus(Ethereum { chain_id: CHAIN_ID }), + AccountKey20 { network: None, key: WETH }, + ), + }), + fun: Fungible(WETH_AMOUNT), + }]; + let multi_assets = VersionedMultiAssets::V3(MultiAssets::from(assets)); + + let destination = VersionedMultiLocation::V3(MultiLocation { + parents: 2, + interior: X1(GlobalConsensus(Ethereum { chain_id: CHAIN_ID })), + }); + + let beneficiary = VersionedMultiLocation::V3(MultiLocation { + parents: 0, + interior: X1(AccountKey20 { network: None, key: ETHEREUM_DESTINATION_ADDRESS.into() }), + }); + + let free_balance_before = ::Balances::free_balance( + AssetHubRococoReceiver::get(), + ); + ::PolkadotXcm::reserve_transfer_assets( + RuntimeOrigin::signed(AssetHubRococoReceiver::get()), + Box::new(destination), + Box::new(beneficiary), + Box::new(multi_assets), + 0, + ) + .unwrap(); + let free_balance_after = ::Balances::free_balance( + AssetHubRococoReceiver::get(), + ); + // assert at least DefaultBridgeHubEthereumBaseFee charged from the sender + let free_balance_diff = free_balance_before - free_balance_after; + assert!(free_balance_diff > DefaultBridgeHubEthereumBaseFee::get()); + }); + + BridgeHubRococo::execute_with(|| { + type RuntimeEvent = ::RuntimeEvent; + + assert_expected_events!( + BridgeHubRococo, + vec![ + RuntimeEvent::EthereumOutboundQueue(snowbridge_outbound_queue::Event::MessageQueued {..}) => {}, + ] + ); + let events = BridgeHubRococo::events(); + assert!( + events.iter().any(|event| matches!( + event, + RuntimeEvent::Balances(pallet_balances::Event::Deposit{ who, amount }) + if *who == TREASURY_ACCOUNT.into() && *amount == 16903333 + )), + "Snowbridge sovereign takes local fee." + ); + assert!( + events.iter().any(|event| matches!( + event, + RuntimeEvent::Balances(pallet_balances::Event::Deposit{ who, amount }) + if *who == assethub_sovereign && *amount == 2680000000000, + )), + "AssetHub sovereign takes remote fee." + ); + }); +} diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/Cargo.toml b/cumulus/parachains/runtimes/assets/asset-hub-rococo/Cargo.toml index 47af627d6b20..43579cfe5bb9 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/Cargo.toml +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/Cargo.toml @@ -90,6 +90,8 @@ bp-asset-hub-rococo = { path = "../../../../../bridges/primitives/chain-asset-hu bp-asset-hub-westend = { path = "../../../../../bridges/primitives/chain-asset-hub-westend", default-features = false } bp-bridge-hub-rococo = { path = "../../../../../bridges/primitives/chain-bridge-hub-rococo", default-features = false } bp-bridge-hub-westend = { path = "../../../../../bridges/primitives/chain-bridge-hub-westend", default-features = false } +snowbridge-router-primitives = { path = "../../../../../bridges/snowbridge/parachain/primitives/router", default-features = false } +snowbridge-rococo-common = { path = "../../../../../bridges/snowbridge/parachain/runtime/rococo-common", default-features = false } [dev-dependencies] asset-test-utils = { path = "../test-utils" } @@ -137,6 +139,8 @@ runtime-benchmarks = [ "parachains-common/runtime-benchmarks", "polkadot-parachain-primitives/runtime-benchmarks", "polkadot-runtime-common/runtime-benchmarks", + "snowbridge-rococo-common/runtime-benchmarks", + "snowbridge-router-primitives/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "xcm-executor/runtime-benchmarks", @@ -227,6 +231,8 @@ std = [ "primitive-types/std", "rococo-runtime-constants/std", "scale-info/std", + "snowbridge-rococo-common/std", + "snowbridge-router-primitives/std", "sp-api/std", "sp-block-builder/std", "sp-consensus-aura/std", diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs index ca5d5be241ba..61939a2c80a7 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs @@ -30,11 +30,12 @@ pub mod xcm_config; use assets_common::{ foreign_creators::ForeignCreators, local_and_foreign_assets::{LocalFromLeft, TargetFromLeft}, - matching::FromSiblingParachain, + matching::{FromNetwork, FromSiblingParachain}, AssetIdForTrustBackedAssetsConvert, MultiLocationForAssetId, }; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; use cumulus_primitives_core::AggregateMessageOrigin; +use snowbridge_rococo_common::EthereumNetwork; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ @@ -110,7 +111,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("statemine"), impl_name: create_runtime_str!("statemine"), authoring_version: 1, - spec_version: 1_005_000, + spec_version: 1_005_001, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 14, @@ -379,7 +380,10 @@ impl pallet_assets::Config for Runtime { type AssetIdParameter = MultiLocationForAssetId; type Currency = Balances; type CreateOrigin = ForeignCreators< - (FromSiblingParachain>,), + ( + FromSiblingParachain>, + FromNetwork, + ), ForeignCreatorsSovereignAccountOf, AccountId, >; @@ -913,7 +917,6 @@ construct_runtime!( // Bridge utilities. ToWestendXcmRouter: pallet_xcm_bridge_hub_router::::{Pallet, Storage, Call} = 45, - // The main stage. Assets: pallet_assets::::{Pallet, Call, Storage, Event} = 50, Uniques: pallet_uniques::{Pallet, Call, Storage, Event} = 51, diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/xcm_config.rs index e94a14b304b8..2826c18f3f7c 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/xcm_config.rs @@ -21,7 +21,7 @@ use super::{ }; use assets_common::{ local_and_foreign_assets::MatchesLocalAndForeignAssetsMultiLocation, - matching::{FromSiblingParachain, IsForeignConcreteAsset}, + matching::{FromNetwork, FromSiblingParachain, IsForeignConcreteAsset}, }; use frame_support::{ match_types, parameter_types, @@ -39,6 +39,8 @@ use parachains_common::{ }; use polkadot_parachain_primitives::primitives::Sibling; use polkadot_runtime_common::xcm_sender::ExponentialPrice; +use snowbridge_rococo_common::EthereumNetwork; +use snowbridge_router_primitives::inbound::GlobalConsensusEthereumConvertsFor; use sp_runtime::traits::{AccountIdConversion, ConvertInto}; use xcm::latest::prelude::*; #[allow(deprecated)] @@ -50,9 +52,10 @@ use xcm_builder::{ GlobalConsensusParachainConvertsFor, HashedDescription, IsConcrete, LocalMint, NetworkExportTableItem, NoChecking, ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative, - SignedToAccountId32, SovereignSignedViaLocation, StartsWith, StartsWithExplicitGlobalConsensus, - TakeWeightCredit, TrailingSetTopicAsId, UsingComponents, WeightInfoBounds, WithComputedOrigin, - WithUniqueTopic, XcmFeeManagerFromComponents, XcmFeeToAccount, + SignedToAccountId32, SovereignPaidRemoteExporter, SovereignSignedViaLocation, StartsWith, + StartsWithExplicitGlobalConsensus, TakeWeightCredit, TrailingSetTopicAsId, UsingComponents, + WeightInfoBounds, WithComputedOrigin, WithUniqueTopic, XcmFeeManagerFromComponents, + XcmFeeToAccount, }; use xcm_executor::{traits::WithOriginFilter, XcmExecutor}; @@ -90,6 +93,9 @@ pub type LocationToAccountId = ( // Different global consensus parachain sovereign account. // (Used for over-bridge transfers and reserve processing) GlobalConsensusParachainConvertsFor, + // Ethereum contract sovereign account. + // (Used to get convert ethereum contract locations to sovereign account) + GlobalConsensusEthereumConvertsFor, ); /// Means for transacting the native currency on this chain. @@ -259,10 +265,11 @@ impl Contains for SafeCallFilter { // Allow to change dedicated storage items (called by governance-like) match call { RuntimeCall::System(frame_system::Call::set_storage { items }) - if items.iter().all(|(k, _)| k.eq(&bridging::XcmBridgeHubRouterByteFee::key())) || - items - .iter() - .all(|(k, _)| k.eq(&bridging::XcmBridgeHubRouterBaseFee::key())) => + if items.iter().all(|(k, _)| { + k.eq(&bridging::XcmBridgeHubRouterByteFee::key()) | + k.eq(&bridging::XcmBridgeHubRouterBaseFee::key()) | + k.eq(&bridging::to_ethereum::BridgeHubEthereumBaseFee::key()) + }) => return true, _ => (), }; @@ -276,19 +283,14 @@ impl Contains for SafeCallFilter { frame_system::Call::set_heap_pages { .. } | frame_system::Call::set_code { .. } | frame_system::Call::set_code_without_checks { .. } | + frame_system::Call::authorize_upgrade { .. } | + frame_system::Call::authorize_upgrade_without_checks { .. } | frame_system::Call::kill_prefix { .. }, ) | RuntimeCall::ParachainSystem(..) | RuntimeCall::Timestamp(..) | RuntimeCall::Balances(..) | - RuntimeCall::CollatorSelection( - pallet_collator_selection::Call::set_desired_candidates { .. } | - pallet_collator_selection::Call::set_candidacy_bond { .. } | - pallet_collator_selection::Call::register_as_candidate { .. } | - pallet_collator_selection::Call::leave_intent { .. } | - pallet_collator_selection::Call::set_invulnerables { .. } | - pallet_collator_selection::Call::add_invulnerable { .. } | - pallet_collator_selection::Call::remove_invulnerable { .. }, - ) | RuntimeCall::Session(pallet_session::Call::purge_keys { .. }) | + RuntimeCall::CollatorSelection(..) | + RuntimeCall::Session(pallet_session::Call::purge_keys { .. }) | RuntimeCall::XcmpQueue(..) | RuntimeCall::MessageQueue(..) | RuntimeCall::Assets( @@ -534,7 +536,10 @@ impl xcm_executor::Config for XcmConfig { // as reserve locations (we trust the Bridge Hub to relay the message that a reserve is being // held). Asset Hub may _act_ as a reserve location for ROC and assets created // under `pallet-assets`. Users must use teleport where allowed (e.g. ROC with the Relay Chain). - type IsReserve = (bridging::to_westend::IsTrustedBridgedReserveLocationForConcreteAsset,); + type IsReserve = ( + bridging::to_westend::IsTrustedBridgedReserveLocationForConcreteAsset, + bridging::to_ethereum::IsTrustedBridgedReserveLocationForForeignAsset, + ); type IsTeleporter = TrustedTeleporters; type UniversalLocation = UniversalLocation; type Barrier = Barrier; @@ -585,7 +590,8 @@ impl xcm_executor::Config for XcmConfig { XcmFeeToAccount, >; type MessageExporter = (); - type UniversalAliases = (bridging::to_westend::UniversalAliases,); + type UniversalAliases = + (bridging::to_westend::UniversalAliases, bridging::to_ethereum::UniversalAliases); type CallDispatcher = WithOriginFilter; type SafeCallFilter = SafeCallFilter; type Aliasers = Nothing; @@ -613,6 +619,9 @@ pub type XcmRouter = WithUniqueTopic<( // Router which wraps and sends xcm to BridgeHub to be delivered to the Westend // GlobalConsensus ToWestendXcmRouter, + // Router which wraps and sends xcm to BridgeHub to be delivered to the Ethereum + // GlobalConsensus + SovereignPaidRemoteExporter, )>; impl pallet_xcm::Config for Runtime { @@ -658,6 +667,7 @@ pub type ForeignCreatorsSovereignAccountOf = ( SiblingParachainConvertsVia, AccountId32Aliases, ParentIsPreset, + GlobalConsensusEthereumConvertsFor, ); /// Simple conversion of `u32` into an `AssetId` for use in benchmarking. @@ -706,10 +716,17 @@ pub mod bridging { sp_std::vec::Vec::new().into_iter() .chain(to_westend::BridgeTable::get()) .collect(); + + pub EthereumBridgeTable: sp_std::vec::Vec = + sp_std::vec::Vec::new().into_iter() + .chain(to_ethereum::BridgeTable::get()) + .collect(); } pub type NetworkExportTable = xcm_builder::NetworkExportTable; + pub type EthereumNetworkExportTable = xcm_builder::NetworkExportTable; + pub mod to_westend { use super::*; @@ -786,6 +803,56 @@ pub mod bridging { } } + pub mod to_ethereum { + use super::*; + + parameter_types! { + /// User fee for ERC20 token transfer back to Ethereum. + /// (initially was calculated by test `OutboundQueue::calculate_fees` - ETH/ROC 1/400 and fee_per_gas 20 GWEI = 2200698000000 + *25%) + /// Needs to be more than fee calculated from DefaultFeeConfig FeeConfigRecord in snowbridge:parachain/pallets/outbound-queue/src/lib.rs + /// Polkadot uses 10 decimals, Kusama and Rococo 12 decimals. + pub const DefaultBridgeHubEthereumBaseFee: Balance = 2_750_872_500_000; + pub storage BridgeHubEthereumBaseFee: Balance = DefaultBridgeHubEthereumBaseFee::get(); + pub SiblingBridgeHubWithEthereumInboundQueueInstance: MultiLocation = MultiLocation::new( + 1, + X2( + Parachain(SiblingBridgeHubParaId::get()), + PalletInstance(snowbridge_rococo_common::INBOUND_QUEUE_MESSAGES_PALLET_INDEX) + ) + ); + + /// Set up exporters configuration. + /// `Option` represents static "base fee" which is used for total delivery fee calculation. + pub BridgeTable: sp_std::vec::Vec = sp_std::vec![ + NetworkExportTableItem::new( + EthereumNetwork::get(), + Some(sp_std::vec![Junctions::Here]), + SiblingBridgeHub::get(), + Some(( + XcmBridgeHubRouterFeeAssetId::get(), + BridgeHubEthereumBaseFee::get(), + ).into()) + ), + ]; + + /// Universal aliases + pub UniversalAliases: BTreeSet<(MultiLocation, Junction)> = BTreeSet::from_iter( + sp_std::vec![ + (SiblingBridgeHubWithEthereumInboundQueueInstance::get(), GlobalConsensus(EthereumNetwork::get())), + ] + ); + } + + pub type IsTrustedBridgedReserveLocationForForeignAsset = + matching::IsForeignConcreteAsset>; + + impl Contains<(MultiLocation, Junction)> for UniversalAliases { + fn contains(alias: &(MultiLocation, Junction)) -> bool { + UniversalAliases::get().contains(alias) + } + } + } + /// Benchmarks helper for bridging configuration. #[cfg(feature = "runtime-benchmarks")] pub struct BridgingBenchmarksHelper; diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/tests.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/tests.rs index 030a3723319b..42c91cc8ea69 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/tests.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/tests/tests.rs @@ -865,3 +865,55 @@ fn change_xcm_bridge_hub_router_byte_fee_by_governance_works() { }, ) } + +#[test] +fn change_xcm_bridge_hub_router_base_fee_by_governance_works() { + asset_test_utils::test_cases::change_storage_constant_by_governance_works::< + Runtime, + bridging::XcmBridgeHubRouterBaseFee, + Balance, + >( + collator_session_keys(), + 1000, + Box::new(|call| RuntimeCall::System(call).encode()), + || { + ( + bridging::XcmBridgeHubRouterBaseFee::key().to_vec(), + bridging::XcmBridgeHubRouterBaseFee::get(), + ) + }, + |old_value| { + if let Some(new_value) = old_value.checked_add(1) { + new_value + } else { + old_value.checked_sub(1).unwrap() + } + }, + ) +} + +#[test] +fn change_xcm_bridge_hub_ethereum_base_fee_by_governance_works() { + asset_test_utils::test_cases::change_storage_constant_by_governance_works::< + Runtime, + bridging::to_ethereum::BridgeHubEthereumBaseFee, + Balance, + >( + collator_session_keys(), + 1000, + Box::new(|call| RuntimeCall::System(call).encode()), + || { + ( + bridging::to_ethereum::BridgeHubEthereumBaseFee::key().to_vec(), + bridging::to_ethereum::BridgeHubEthereumBaseFee::get(), + ) + }, + |old_value| { + if let Some(new_value) = old_value.checked_add(1) { + new_value + } else { + old_value.checked_sub(1).unwrap() + } + }, + ) +} diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs index b257f263d48e..cb2fedeb146f 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs @@ -289,19 +289,14 @@ impl Contains for SafeCallFilter { frame_system::Call::set_heap_pages { .. } | frame_system::Call::set_code { .. } | frame_system::Call::set_code_without_checks { .. } | + frame_system::Call::authorize_upgrade { .. } | + frame_system::Call::authorize_upgrade_without_checks { .. } | frame_system::Call::kill_prefix { .. }, ) | RuntimeCall::ParachainSystem(..) | RuntimeCall::Timestamp(..) | RuntimeCall::Balances(..) | - RuntimeCall::CollatorSelection( - pallet_collator_selection::Call::set_desired_candidates { .. } | - pallet_collator_selection::Call::set_candidacy_bond { .. } | - pallet_collator_selection::Call::register_as_candidate { .. } | - pallet_collator_selection::Call::leave_intent { .. } | - pallet_collator_selection::Call::set_invulnerables { .. } | - pallet_collator_selection::Call::add_invulnerable { .. } | - pallet_collator_selection::Call::remove_invulnerable { .. }, - ) | RuntimeCall::Session(pallet_session::Call::purge_keys { .. }) | + RuntimeCall::CollatorSelection(..) | + RuntimeCall::Session(pallet_session::Call::purge_keys { .. }) | RuntimeCall::XcmpQueue(..) | RuntimeCall::MessageQueue(..) | RuntimeCall::Assets( diff --git a/cumulus/parachains/runtimes/assets/common/src/matching.rs b/cumulus/parachains/runtimes/assets/common/src/matching.rs index 914972812521..d6ecc3ec99f0 100644 --- a/cumulus/parachains/runtimes/assets/common/src/matching.rs +++ b/cumulus/parachains/runtimes/assets/common/src/matching.rs @@ -58,6 +58,37 @@ impl> ContainsPair } } +/// Checks if `a` is from the expected global consensus network. Checks that `MultiLocation-a` +/// starts with `MultiLocation-b`, and that network is a foreign consensus system. +pub struct FromNetwork( + sp_std::marker::PhantomData<(UniversalLocation, ExpectedNetworkId)>, +); +impl, ExpectedNetworkId: Get> + ContainsPair for FromNetwork +{ + fn contains(&a: &MultiLocation, b: &MultiLocation) -> bool { + // `a` needs to be from `b` at least + if !a.starts_with(b) { + return false + } + + let universal_source = UniversalLocation::get(); + + // ensure that `a`` is remote and from the expected network + match ensure_is_remote(universal_source, a) { + Ok((network_id, _)) => network_id == ExpectedNetworkId::get(), + Err(e) => { + log::trace!( + target: "xcm::contains", + "FromNetwork origin: {:?} is not remote to the universal_source: {:?} {:?}", + a, universal_source, e + ); + false + }, + } + } +} + /// Adapter verifies if it is allowed to receive `MultiAsset` from `MultiLocation`. /// /// Note: `MultiLocation` has to be from a different global consensus. @@ -95,3 +126,92 @@ impl< Reserves::contains(asset, origin) } } + +#[cfg(test)] +mod tests { + use super::*; + use frame_support::parameter_types; + + parameter_types! { + pub UniversalLocation: InteriorMultiLocation = X2(GlobalConsensus(Rococo), Parachain(1000)); + pub ExpectedNetworkId: NetworkId = Wococo; + } + + #[test] + fn from_network_contains_works() { + // asset and origin from foreign consensus works + let asset: MultiLocation = ( + Parent, + Parent, + GlobalConsensus(Wococo), + Parachain(1000), + PalletInstance(1), + GeneralIndex(1), + ) + .into(); + let origin: MultiLocation = + (Parent, Parent, GlobalConsensus(Wococo), Parachain(1000)).into(); + assert!(FromNetwork::::contains(&asset, &origin)); + + // asset and origin from local consensus fails + let asset: MultiLocation = ( + Parent, + Parent, + GlobalConsensus(Rococo), + Parachain(1000), + PalletInstance(1), + GeneralIndex(1), + ) + .into(); + let origin: MultiLocation = + (Parent, Parent, GlobalConsensus(Rococo), Parachain(1000)).into(); + assert!(!FromNetwork::::contains(&asset, &origin)); + + // asset and origin from here fails + let asset: MultiLocation = (PalletInstance(1), GeneralIndex(1)).into(); + let origin: MultiLocation = Here.into(); + assert!(!FromNetwork::::contains(&asset, &origin)); + + // asset from different consensus fails + let asset: MultiLocation = ( + Parent, + Parent, + GlobalConsensus(Polkadot), + Parachain(1000), + PalletInstance(1), + GeneralIndex(1), + ) + .into(); + let origin: MultiLocation = + (Parent, Parent, GlobalConsensus(Wococo), Parachain(1000)).into(); + assert!(!FromNetwork::::contains(&asset, &origin)); + + // origin from different consensus fails + let asset: MultiLocation = ( + Parent, + Parent, + GlobalConsensus(Wococo), + Parachain(1000), + PalletInstance(1), + GeneralIndex(1), + ) + .into(); + let origin: MultiLocation = + (Parent, Parent, GlobalConsensus(Polkadot), Parachain(1000)).into(); + assert!(!FromNetwork::::contains(&asset, &origin)); + + // asset and origin from unexpected consensus fails + let asset: MultiLocation = ( + Parent, + Parent, + GlobalConsensus(Polkadot), + Parachain(1000), + PalletInstance(1), + GeneralIndex(1), + ) + .into(); + let origin: MultiLocation = + (Parent, Parent, GlobalConsensus(Polkadot), Parachain(1000)).into(); + assert!(!FromNetwork::::contains(&asset, &origin)); + } +} diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml index f8b279e3e3d3..d362c5f12a60 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml @@ -106,6 +106,21 @@ pallet-bridge-relayers = { path = "../../../../../bridges/modules/relayers", def pallet-xcm-bridge-hub = { path = "../../../../../bridges/modules/xcm-bridge-hub", default-features = false } bridge-runtime-common = { path = "../../../../../bridges/bin/runtime-common", default-features = false } +# Ethereum Bridge (Snowbridge) +snowbridge-beacon-primitives = { path = "../../../../../bridges/snowbridge/parachain/primitives/beacon", default-features = false } +snowbridge-system = { path = "../../../../../bridges/snowbridge/parachain/pallets/system", default-features = false } +snowbridge-system-runtime-api = { path = "../../../../../bridges/snowbridge/parachain/pallets/system/runtime-api", default-features = false } +snowbridge-core = { path = "../../../../../bridges/snowbridge/parachain/primitives/core", default-features = false } +snowbridge-ethereum-beacon-client = { path = "../../../../../bridges/snowbridge/parachain/pallets/ethereum-beacon-client", default-features = false } +snowbridge-inbound-queue = { path = "../../../../../bridges/snowbridge/parachain/pallets/inbound-queue", default-features = false } +snowbridge-outbound-queue = { path = "../../../../../bridges/snowbridge/parachain/pallets/outbound-queue", default-features = false } +snowbridge-outbound-queue-runtime-api = { path = "../../../../../bridges/snowbridge/parachain/pallets/outbound-queue/runtime-api", default-features = false } +snowbridge-router-primitives = { path = "../../../../../bridges/snowbridge/parachain/primitives/router", default-features = false } +snowbridge-runtime-common = { path = "../../../../../bridges/snowbridge/parachain/runtime/runtime-common", default-features = false } +snowbridge-rococo-common = { path = "../../../../../bridges/snowbridge/parachain/runtime/rococo-common", default-features = false } + +bridge-hub-common = { path = "../common", default-features = false } + [dev-dependencies] static_assertions = "1.1" bridge-hub-test-utils = { path = "../test-utils" } @@ -115,7 +130,7 @@ bridge-runtime-common = { path = "../../../../../bridges/bin/runtime-common", fe sp-keyring = { path = "../../../../../substrate/primitives/keyring" } [features] -default = ["std"] +default = ["beacon-spec-mainnet", "std"] std = [ "bp-asset-hub-rococo/std", "bp-asset-hub-westend/std", @@ -131,6 +146,7 @@ std = [ "bp-rococo/std", "bp-runtime/std", "bp-westend/std", + "bridge-hub-common/std", "bridge-runtime-common/std", "codec/std", "cumulus-pallet-aura-ext/std", @@ -174,6 +190,17 @@ std = [ "rococo-runtime-constants/std", "scale-info/std", "serde", + "snowbridge-beacon-primitives/std", + "snowbridge-core/std", + "snowbridge-ethereum-beacon-client/std", + "snowbridge-inbound-queue/std", + "snowbridge-outbound-queue-runtime-api/std", + "snowbridge-outbound-queue/std", + "snowbridge-rococo-common/std", + "snowbridge-router-primitives/std", + "snowbridge-runtime-common/std", + "snowbridge-system-runtime-api/std", + "snowbridge-system/std", "sp-api/std", "sp-block-builder/std", "sp-consensus-aura/std", @@ -189,12 +216,15 @@ std = [ "sp-transaction-pool/std", "sp-version/std", "substrate-wasm-builder", + "substrate-wasm-builder", "xcm-builder/std", "xcm-executor/std", "xcm/std", ] runtime-benchmarks = [ + "beacon-spec-mainnet", + "bridge-hub-common/runtime-benchmarks", "bridge-runtime-common/runtime-benchmarks", "cumulus-pallet-parachain-system/runtime-benchmarks", "cumulus-pallet-session-benchmarking/runtime-benchmarks", @@ -221,6 +251,14 @@ runtime-benchmarks = [ "parachains-common/runtime-benchmarks", "polkadot-parachain-primitives/runtime-benchmarks", "polkadot-runtime-common/runtime-benchmarks", + "snowbridge-core/runtime-benchmarks", + "snowbridge-ethereum-beacon-client/runtime-benchmarks", + "snowbridge-inbound-queue/runtime-benchmarks", + "snowbridge-outbound-queue/runtime-benchmarks", + "snowbridge-rococo-common/runtime-benchmarks", + "snowbridge-router-primitives/runtime-benchmarks", + "snowbridge-runtime-common/runtime-benchmarks", + "snowbridge-system/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "xcm-executor/runtime-benchmarks", @@ -253,10 +291,17 @@ try-runtime = [ "pallet-xcm/try-runtime", "parachain-info/try-runtime", "polkadot-runtime-common/try-runtime", + "snowbridge-ethereum-beacon-client/try-runtime", + "snowbridge-inbound-queue/try-runtime", + "snowbridge-outbound-queue/try-runtime", + "snowbridge-system/try-runtime", "sp-runtime/try-runtime", ] experimental = ["pallet-aura/experimental"] +beacon-spec-mainnet = [ + "snowbridge-ethereum-beacon-client/beacon-spec-mainnet", +] # A feature that should be enabled when the runtime should be built for on-chain # deployment. This will disable stuff that shouldn't be part of the on-chain wasm diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_to_ethereum_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_to_ethereum_config.rs new file mode 100644 index 000000000000..b0526148fa3a --- /dev/null +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_to_ethereum_config.rs @@ -0,0 +1,30 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Cumulus. + +// Cumulus is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Cumulus is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Cumulus. If not, see . + +use crate::{ + xcm_config::{AgentIdOf, UniversalLocation}, + Runtime, +}; +use snowbridge_rococo_common::EthereumNetwork; +use snowbridge_router_primitives::outbound::EthereumBlobExporter; + +/// Exports message to the Ethereum Gateway contract. +pub type SnowbridgeExporter = EthereumBlobExporter< + UniversalLocation, + EthereumNetwork, + snowbridge_outbound_queue::Pallet, + AgentIdOf, +>; diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs index 75af5a8ef7b1..b21cde248e11 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs @@ -30,18 +30,24 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); pub mod bridge_common_config; pub mod bridge_to_bulletin_config; +pub mod bridge_to_ethereum_config; pub mod bridge_to_westend_config; mod weights; pub mod xcm_config; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; +use snowbridge_beacon_primitives::{Fork, ForkVersions}; +use snowbridge_core::{ + gwei, meth, outbound::Message, AgentId, AllowSiblingsOnly, PricingParameters, Rewards, +}; +use snowbridge_router_primitives::inbound::MessageToXcm; use sp_api::impl_runtime_apis; -use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; +use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, - traits::Block as BlockT, + traits::{Block as BlockT, Keccak256}, transaction_validity::{TransactionSource, TransactionValidity}, - ApplyExtrinsicResult, + ApplyExtrinsicResult, FixedU128, }; use sp_std::prelude::*; @@ -49,7 +55,7 @@ use sp_std::prelude::*; use sp_version::NativeVersion; use sp_version::RuntimeVersion; -use cumulus_primitives_core::{AggregateMessageOrigin, ParaId}; +use cumulus_primitives_core::ParaId; use frame_support::{ construct_runtime, derive_impl, dispatch::DispatchClass, @@ -63,17 +69,25 @@ use frame_system::{ limits::{BlockLength, BlockWeights}, EnsureRoot, }; -use parachains_common::message_queue::{NarrowOriginToSibling, ParaIdToSibling}; -pub use sp_consensus_aura::sr25519::AuthorityId as AuraId; -pub use sp_runtime::{MultiAddress, Perbill, Permill}; -use xcm_config::{XcmOriginToTransactDispatchOrigin, XcmRouter}; use bp_runtime::HeaderId; +#[cfg(not(feature = "runtime-benchmarks"))] +use bridge_hub_common::BridgeHubMessageRouter; +use bridge_hub_common::{ + message_queue::{NarrowOriginToSibling, ParaIdToSibling}, + AggregateMessageOrigin, +}; +use pallet_xcm::EnsureXcm; +pub use sp_consensus_aura::sr25519::AuthorityId as AuraId; +pub use sp_runtime::{MultiAddress, Perbill, Permill}; +use xcm::VersionedMultiLocation; +use xcm_config::{TreasuryAccount, XcmOriginToTransactDispatchOrigin, XcmRouter}; #[cfg(any(feature = "std", test))] pub use sp_runtime::BuildStorage; use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate}; +use rococo_runtime_constants::system_parachain::{ASSET_HUB_ID, BRIDGE_HUB_ID}; use xcm::latest::prelude::*; use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}; @@ -85,6 +99,19 @@ use parachains_common::{ HOURS, MAXIMUM_BLOCK_WEIGHT, NORMAL_DISPATCH_RATIO, SLOT_DURATION, }; +#[cfg(feature = "runtime-benchmarks")] +use crate::xcm_config::benchmark_helpers::DoNothingRouter; +#[cfg(feature = "runtime-benchmarks")] +use snowbridge_beacon_primitives::CompactExecutionHeader; +#[cfg(feature = "runtime-benchmarks")] +use snowbridge_core::RingBufferMap; +#[cfg(feature = "runtime-benchmarks")] +pub use snowbridge_ethereum_beacon_client::ExecutionHeaderBuffer; +#[cfg(feature = "runtime-benchmarks")] +use snowbridge_inbound_queue::BenchmarkHelper; +#[cfg(feature = "runtime-benchmarks")] +use sp_core::H256; + /// The address format for describing accounts. pub type Address = MultiAddress; @@ -124,6 +151,12 @@ pub type Migrations = ( pallet_multisig::migrations::v1::MigrateToV1, InitStorageVersions, cumulus_pallet_xcmp_queue::migration::v4::MigrationToV4, + // unreleased + snowbridge_system::migration::v0::InitializeOnUpgrade< + Runtime, + ConstU32, + ConstU32, + >, ); /// Migration to initialize storage versions for pallets added after genesis. @@ -176,7 +209,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("bridge-hub-rococo"), impl_name: create_runtime_str!("bridge-hub-rococo"), authoring_version: 1, - spec_version: 1_005_000, + spec_version: 1_005_001, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 4, @@ -325,21 +358,27 @@ impl cumulus_pallet_parachain_system::Config for Runtime { impl parachain_info::Config for Runtime {} parameter_types! { - pub MessageQueueServiceWeight: Weight = Perbill::from_percent(35) * RuntimeBlockWeights::get().max_block; + /// Amount of weight that can be spent per block to service messages. This was increased + /// from 35% to 60% of the max block weight to accommodate the Ethereum beacon light client + /// extrinsics. The force_checkpoint and submit extrinsics (for submit, optionally) includes + /// the sync committee's pubkeys (512 x 48 bytes) + pub MessageQueueServiceWeight: Weight = Perbill::from_percent(60) * RuntimeBlockWeights::get().max_block; } impl pallet_message_queue::Config for Runtime { type RuntimeEvent = RuntimeEvent; type WeightInfo = weights::pallet_message_queue::WeightInfo; #[cfg(feature = "runtime-benchmarks")] - type MessageProcessor = pallet_message_queue::mock_helpers::NoopMessageProcessor< - cumulus_primitives_core::AggregateMessageOrigin, - >; + type MessageProcessor = + pallet_message_queue::mock_helpers::NoopMessageProcessor; #[cfg(not(feature = "runtime-benchmarks"))] - type MessageProcessor = xcm_builder::ProcessXcmMessage< - AggregateMessageOrigin, - xcm_executor::XcmExecutor, - RuntimeCall, + type MessageProcessor = BridgeHubMessageRouter< + xcm_builder::ProcessXcmMessage< + AggregateMessageOrigin, + xcm_executor::XcmExecutor, + RuntimeCall, + >, + EthereumOutboundQueue, >; type Size = u32; // The XCMP queue pallet is only ever able to handle the `Sibling(ParaId)` origin: @@ -456,6 +495,151 @@ impl pallet_utility::Config for Runtime { type WeightInfo = weights::pallet_utility::WeightInfo; } +// Ethereum Bridge + +#[cfg(not(feature = "runtime-benchmarks"))] +parameter_types! { + pub storage EthereumGatewayAddress: H160 = H160::zero(); +} + +#[cfg(feature = "runtime-benchmarks")] +parameter_types! { + pub storage EthereumGatewayAddress: H160 = H160(hex_literal::hex!("EDa338E4dC46038493b885327842fD3E301CaB39")); +} + +parameter_types! { + pub const CreateAssetCall: [u8;2] = [53, 0]; + pub const CreateAssetDeposit: u128 = (UNITS / 10) + EXISTENTIAL_DEPOSIT; + pub const InboundQueuePalletInstance: u8 = snowbridge_rococo_common::INBOUND_QUEUE_MESSAGES_PALLET_INDEX; + pub Parameters: PricingParameters = PricingParameters { + exchange_rate: FixedU128::from_rational(1, 400), + fee_per_gas: gwei(20), + rewards: Rewards { local: 1 * UNITS, remote: meth(1) } + }; +} + +#[cfg(feature = "runtime-benchmarks")] +impl BenchmarkHelper for Runtime { + fn initialize_storage(block_hash: H256, header: CompactExecutionHeader) { + >::insert(block_hash, header); + } +} + +impl snowbridge_inbound_queue::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type Verifier = snowbridge_ethereum_beacon_client::Pallet; + type Token = Balances; + #[cfg(not(feature = "runtime-benchmarks"))] + type XcmSender = XcmRouter; + #[cfg(feature = "runtime-benchmarks")] + type XcmSender = DoNothingRouter; + type ChannelLookup = EthereumSystem; + type GatewayAddress = EthereumGatewayAddress; + #[cfg(feature = "runtime-benchmarks")] + type Helper = Runtime; + type MessageConverter = MessageToXcm< + CreateAssetCall, + CreateAssetDeposit, + InboundQueuePalletInstance, + AccountId, + Balance, + >; + type WeightToFee = WeightToFee; + type LengthToFee = ConstantMultiplier; + type MaxMessageSize = ConstU32<2048>; + type WeightInfo = weights::snowbridge_inbound_queue::WeightInfo; + type PricingParameters = EthereumSystem; +} + +impl snowbridge_outbound_queue::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type Hashing = Keccak256; + type MessageQueue = MessageQueue; + type Decimals = ConstU8<12>; + type MaxMessagePayloadSize = ConstU32<2048>; + type MaxMessagesPerBlock = ConstU32<32>; + type GasMeter = snowbridge_core::outbound::ConstantGasMeter; + type Balance = Balance; + type WeightToFee = WeightToFee; + type WeightInfo = weights::snowbridge_outbound_queue::WeightInfo; + type PricingParameters = EthereumSystem; + type Channels = EthereumSystem; +} + +#[cfg(not(feature = "beacon-spec-mainnet"))] +parameter_types! { + pub const ChainForkVersions: ForkVersions = ForkVersions { + genesis: Fork { + version: [0, 0, 0, 1], // 0x00000001 + epoch: 0, + }, + altair: Fork { + version: [1, 0, 0, 1], // 0x01000001 + epoch: 0, + }, + bellatrix: Fork { + version: [2, 0, 0, 1], // 0x02000001 + epoch: 0, + }, + capella: Fork { + version: [3, 0, 0, 1], // 0x03000001 + epoch: 0, + }, + }; + pub const MaxExecutionHeadersToKeep:u32 = 1000; +} + +#[cfg(feature = "beacon-spec-mainnet")] +parameter_types! { + pub const ChainForkVersions: ForkVersions = ForkVersions { + genesis: Fork { + version: [0, 0, 16, 32], // 0x00001020 + epoch: 0, + }, + altair: Fork { + version: [1, 0, 16, 32], // 0x01001020 + epoch: 36660, + }, + bellatrix: Fork { + version: [2, 0, 16, 32], // 0x02001020 + epoch: 112260, + }, + capella: Fork { + version: [3, 0, 16, 32], // 0x03001020 + epoch: 162304, + }, + }; + pub const MaxExecutionHeadersToKeep:u32 = 8192 * 2; +} + +impl snowbridge_ethereum_beacon_client::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type ForkVersions = ChainForkVersions; + type MaxExecutionHeadersToKeep = MaxExecutionHeadersToKeep; + type WeightInfo = weights::snowbridge_ethereum_beacon_client::WeightInfo; +} + +#[cfg(feature = "runtime-benchmarks")] +impl snowbridge_system::BenchmarkHelper for () { + fn make_xcm_origin(location: xcm::latest::MultiLocation) -> RuntimeOrigin { + RuntimeOrigin::from(pallet_xcm::Origin::Xcm(location)) + } +} + +impl snowbridge_system::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type OutboundQueue = EthereumOutboundQueue; + type SiblingOrigin = EnsureXcm; + type AgentIdOf = xcm_config::AgentIdOf; + type TreasuryAccount = TreasuryAccount; + type Token = Balances; + type WeightInfo = weights::snowbridge_system::WeightInfo; + #[cfg(feature = "runtime-benchmarks")] + type Helper = (); + type DefaultPricingParameters = Parameters; + type InboundDeliveryCost = EthereumInboundQueue; +} + // Create the runtime by composing the FRAME pallets that were previously configured. construct_runtime!( pub enum Runtime @@ -515,6 +699,11 @@ construct_runtime!( // With-Rococo Bulletin bridge hub pallet. XcmOverPolkadotBulletin: pallet_xcm_bridge_hub::::{Pallet} = 62, + EthereumInboundQueue: snowbridge_inbound_queue::{Pallet, Call, Storage, Event} = 80, + EthereumOutboundQueue: snowbridge_outbound_queue::{Pallet, Call, Storage, Event} = 81, + EthereumBeaconClient: snowbridge_ethereum_beacon_client::{Pallet, Call, Storage, Event} = 82, + EthereumSystem: snowbridge_system::{Pallet, Call, Storage, Config, Event} = 83, + // Message Queue. Importantly, is registered last so that messages are processed after // the `on_initialize` hooks of bridging pallets. MessageQueue: pallet_message_queue::{Pallet, Call, Storage, Event} = 250, @@ -564,6 +753,11 @@ mod benches { [pallet_bridge_messages, RococoToWestend] [pallet_bridge_messages, RococoToRococoBulletin] [pallet_bridge_relayers, BridgeRelayersBench::] + // Ethereum Bridge + [snowbridge_inbound_queue, EthereumInboundQueue] + [snowbridge_outbound_queue, EthereumOutboundQueue] + [snowbridge_system, EthereumSystem] + [snowbridge_ethereum_beacon_client, EthereumBeaconClient] ); } @@ -792,6 +986,22 @@ impl_runtime_apis! { } } + impl snowbridge_outbound_queue_runtime_api::OutboundQueueApi for Runtime { + fn prove_message(leaf_index: u64) -> Option { + snowbridge_outbound_queue::api::prove_message::(leaf_index) + } + + fn calculate_fee(message: Message) -> Option { + snowbridge_outbound_queue::api::calculate_fee::(message) + } + } + + impl snowbridge_system_runtime_api::ControlApi for Runtime { + fn agent_id(location: VersionedMultiLocation) -> Option { + snowbridge_system::api::agent_id::(location) + } + } + #[cfg(feature = "try-runtime")] impl frame_try_runtime::TryRuntime for Runtime { fn on_runtime_upgrade(checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) { diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/mod.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/mod.rs index 41e7ac541631..b134bb41ed13 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/mod.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/mod.rs @@ -40,6 +40,10 @@ pub mod pallet_utility; pub mod pallet_xcm; pub mod paritydb_weights; pub mod rocksdb_weights; +pub mod snowbridge_ethereum_beacon_client; +pub mod snowbridge_inbound_queue; +pub mod snowbridge_outbound_queue; +pub mod snowbridge_system; pub mod xcm; pub use block_weights::constants::BlockExecutionWeight; diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_ethereum_beacon_client.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_ethereum_beacon_client.rs new file mode 100644 index 000000000000..cd960597b441 --- /dev/null +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_ethereum_beacon_client.rs @@ -0,0 +1,151 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Autogenerated weights for `snowbridge_ethereum_beacon_client` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-06-08, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `ip-172-31-8-124`, CPU: `Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("bridge-hub-rococo-dev"), DB CACHE: 1024 + +// Executed Command: +// target/release/polkadot-parachain +// benchmark +// pallet +// --base-path +// /mnt/scratch/benchmark +// --chain=bridge-hub-rococo-dev +// --pallet=snowbridge_ethereum_beacon_client +// --extrinsic=* +// --execution=wasm +// --wasm-execution=compiled +// --steps +// 50 +// --repeat +// 20 +// --output +// ./parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_ethereum_beacon_client.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(missing_docs)] + +use frame_support::{traits::Get, weights::Weight}; +use core::marker::PhantomData; + +/// Weight functions for `snowbridge_ethereum_beacon_client`. +pub struct WeightInfo(PhantomData); +impl snowbridge_ethereum_beacon_client::WeightInfo for WeightInfo { + /// Storage: EthereumBeaconClient FinalizedBeaconStateIndex (r:1 w:1) + /// Proof: EthereumBeaconClient FinalizedBeaconStateIndex (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient FinalizedBeaconStateMapping (r:1 w:1) + /// Proof: EthereumBeaconClient FinalizedBeaconStateMapping (max_values: None, max_size: Some(36), added: 2511, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient NextSyncCommittee (r:0 w:1) + /// Proof: EthereumBeaconClient NextSyncCommittee (max_values: Some(1), max_size: Some(92372), added: 92867, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient InitialCheckpointRoot (r:0 w:1) + /// Proof: EthereumBeaconClient InitialCheckpointRoot (max_values: Some(1), max_size: Some(32), added: 527, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient ValidatorsRoot (r:0 w:1) + /// Proof: EthereumBeaconClient ValidatorsRoot (max_values: Some(1), max_size: Some(32), added: 527, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient LatestFinalizedBlockRoot (r:0 w:1) + /// Proof: EthereumBeaconClient LatestFinalizedBlockRoot (max_values: Some(1), max_size: Some(32), added: 527, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient CurrentSyncCommittee (r:0 w:1) + /// Proof: EthereumBeaconClient CurrentSyncCommittee (max_values: Some(1), max_size: Some(92372), added: 92867, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient LatestExecutionState (r:0 w:1) + /// Proof: EthereumBeaconClient LatestExecutionState (max_values: Some(1), max_size: Some(80), added: 575, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient FinalizedBeaconState (r:0 w:1) + /// Proof: EthereumBeaconClient FinalizedBeaconState (max_values: None, max_size: Some(72), added: 2547, mode: MaxEncodedLen) + fn force_checkpoint() -> Weight { + // Proof Size summary in bytes: + // Measured: `42` + // Estimated: `3501` + // Minimum execution time: 97_185_781_000 picoseconds. + Weight::from_parts(97_263_571_000, 0) + .saturating_add(Weight::from_parts(0, 3501)) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().writes(9)) + } + /// Storage: EthereumBeaconClient LatestFinalizedBlockRoot (r:1 w:1) + /// Proof: EthereumBeaconClient LatestFinalizedBlockRoot (max_values: Some(1), max_size: Some(32), added: 527, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient FinalizedBeaconState (r:1 w:1) + /// Proof: EthereumBeaconClient FinalizedBeaconState (max_values: None, max_size: Some(72), added: 2547, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient LatestExecutionState (r:1 w:0) + /// Proof: EthereumBeaconClient LatestExecutionState (max_values: Some(1), max_size: Some(80), added: 575, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient NextSyncCommittee (r:1 w:0) + /// Proof: EthereumBeaconClient NextSyncCommittee (max_values: Some(1), max_size: Some(92372), added: 92867, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient CurrentSyncCommittee (r:1 w:0) + /// Proof: EthereumBeaconClient CurrentSyncCommittee (max_values: Some(1), max_size: Some(92372), added: 92867, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient ValidatorsRoot (r:1 w:0) + /// Proof: EthereumBeaconClient ValidatorsRoot (max_values: Some(1), max_size: Some(32), added: 527, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient FinalizedBeaconStateIndex (r:1 w:1) + /// Proof: EthereumBeaconClient FinalizedBeaconStateIndex (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient FinalizedBeaconStateMapping (r:1 w:1) + /// Proof: EthereumBeaconClient FinalizedBeaconStateMapping (max_values: None, max_size: Some(36), added: 2511, mode: MaxEncodedLen) + fn submit() -> Weight { + // Proof Size summary in bytes: + // Measured: `92753` + // Estimated: `93857` + // Minimum execution time: 25_999_968_000 picoseconds. + Weight::from_parts(26_051_019_000, 0) + .saturating_add(Weight::from_parts(0, 93857)) + .saturating_add(T::DbWeight::get().reads(8)) + .saturating_add(T::DbWeight::get().writes(4)) + } + /// Storage: EthereumBeaconClient LatestFinalizedBlockRoot (r:1 w:0) + /// Proof: EthereumBeaconClient LatestFinalizedBlockRoot (max_values: Some(1), max_size: Some(32), added: 527, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient FinalizedBeaconState (r:1 w:0) + /// Proof: EthereumBeaconClient FinalizedBeaconState (max_values: None, max_size: Some(72), added: 2547, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient LatestExecutionState (r:1 w:0) + /// Proof: EthereumBeaconClient LatestExecutionState (max_values: Some(1), max_size: Some(80), added: 575, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient NextSyncCommittee (r:1 w:1) + /// Proof: EthereumBeaconClient NextSyncCommittee (max_values: Some(1), max_size: Some(92372), added: 92867, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient CurrentSyncCommittee (r:1 w:0) + /// Proof: EthereumBeaconClient CurrentSyncCommittee (max_values: Some(1), max_size: Some(92372), added: 92867, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient ValidatorsRoot (r:1 w:0) + /// Proof: EthereumBeaconClient ValidatorsRoot (max_values: Some(1), max_size: Some(32), added: 527, mode: MaxEncodedLen) + fn submit_with_sync_committee() -> Weight { + // Proof Size summary in bytes: + // Measured: `92717` + // Estimated: `93857` + // Minimum execution time: 122_354_917_000 picoseconds. + Weight::from_parts(122_461_312_000, 0) + .saturating_add(Weight::from_parts(0, 93857)) + .saturating_add(T::DbWeight::get().reads(6)) + .saturating_add(T::DbWeight::get().writes(1)) + } + /// Storage: EthereumBeaconClient LatestFinalizedBlockRoot (r:1 w:0) + /// Proof: EthereumBeaconClient LatestFinalizedBlockRoot (max_values: Some(1), max_size: Some(32), added: 527, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient FinalizedBeaconState (r:1 w:0) + /// Proof: EthereumBeaconClient FinalizedBeaconState (max_values: None, max_size: Some(72), added: 2547, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient LatestExecutionState (r:1 w:1) + /// Proof: EthereumBeaconClient LatestExecutionState (max_values: Some(1), max_size: Some(80), added: 575, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient ExecutionHeaderIndex (r:1 w:1) + /// Proof: EthereumBeaconClient ExecutionHeaderIndex (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient ExecutionHeaderMapping (r:1 w:1) + /// Proof: EthereumBeaconClient ExecutionHeaderMapping (max_values: None, max_size: Some(36), added: 2511, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient ExecutionHeaders (r:0 w:1) + /// Proof: EthereumBeaconClient ExecutionHeaders (max_values: None, max_size: Some(136), added: 2611, mode: MaxEncodedLen) + fn submit_execution_header() -> Weight { + // Proof Size summary in bytes: + // Measured: `386` + // Estimated: `3537` + // Minimum execution time: 108_761_000 picoseconds. + Weight::from_parts(113_158_000, 0) + .saturating_add(Weight::from_parts(0, 3537)) + .saturating_add(T::DbWeight::get().reads(5)) + .saturating_add(T::DbWeight::get().writes(4)) + } +} diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_inbound_queue.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_inbound_queue.rs new file mode 100644 index 000000000000..f734227a4111 --- /dev/null +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_inbound_queue.rs @@ -0,0 +1,69 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Autogenerated weights for `snowbridge_inbound_queue` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-09-06, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `macbook pro 14 m2`, CPU: `m2-arm64` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("bridge-hub-rococo-dev"), DB CACHE: 1024 + +// Executed Command: +// target/release/polkadot-parachain +// benchmark +// pallet +// --chain=bridge-hub-rococo-dev +// --pallet=snowbridge_inbound_queue +// --extrinsic=* +// --execution=wasm +// --wasm-execution=compiled +// --steps +// 50 +// --repeat +// 20 +// --output +// ./parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_inbound_queue.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(missing_docs)] + +use frame_support::{traits::Get, weights::Weight}; +use core::marker::PhantomData; + +/// Weight functions for `snowbridge_inbound_queue`. +pub struct WeightInfo(PhantomData); +impl snowbridge_inbound_queue::WeightInfo for WeightInfo { + /// Storage: EthereumInboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumInboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: EthereumBeaconClient ExecutionHeaders (r:1 w:0) + /// Proof: EthereumBeaconClient ExecutionHeaders (max_values: None, max_size: Some(136), added: 2611, mode: MaxEncodedLen) + /// Storage: EthereumInboundQueue Nonce (r:1 w:1) + /// Proof: EthereumInboundQueue Nonce (max_values: None, max_size: Some(20), added: 2495, mode: MaxEncodedLen) + /// Storage: System Account (r:1 w:1) + /// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen) + fn submit() -> Weight { + // Proof Size summary in bytes: + // Measured: `457` + // Estimated: `3601` + // Minimum execution time: 69_000_000 picoseconds. + Weight::from_parts(70_000_000, 0) + .saturating_add(Weight::from_parts(0, 3601)) + .saturating_add(T::DbWeight::get().reads(4)) + .saturating_add(T::DbWeight::get().writes(2)) + } +} diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_outbound_queue.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_outbound_queue.rs new file mode 100644 index 000000000000..6cffbc5344a6 --- /dev/null +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_outbound_queue.rs @@ -0,0 +1,87 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Autogenerated weights for `snowbridge_outbound_queue` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-10-20, STEPS: `2`, REPEAT: `1`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `192.168.1.13`, CPU: `` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("bridge-hub-rococo-dev"), DB CACHE: 1024 + +// Executed Command: +// ../target/release/polkadot-parachain +// benchmark +// pallet +// --chain=bridge-hub-rococo-dev +// --pallet=snowbridge_outbound_queue +// --extrinsic=* +// --execution=wasm +// --wasm-execution=compiled +// --output +// ../parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_outbound_queue.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(missing_docs)] + +use frame_support::{traits::Get, weights::Weight}; +use core::marker::PhantomData; + +/// Weight functions for `snowbridge_outbound_queue`. +pub struct WeightInfo(PhantomData); +impl snowbridge_outbound_queue::WeightInfo for WeightInfo { + /// Storage: EthereumOutboundQueue MessageLeaves (r:1 w:1) + /// Proof Skipped: EthereumOutboundQueue MessageLeaves (max_values: Some(1), max_size: None, mode: Measured) + /// Storage: EthereumOutboundQueue PendingHighPriorityMessageCount (r:1 w:1) + /// Proof: EthereumOutboundQueue PendingHighPriorityMessageCount (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue Nonce (r:1 w:1) + /// Proof: EthereumOutboundQueue Nonce (max_values: None, max_size: Some(20), added: 2495, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue Messages (r:1 w:1) + /// Proof Skipped: EthereumOutboundQueue Messages (max_values: Some(1), max_size: None, mode: Measured) + fn do_process_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `42` + // Estimated: `3485` + // Minimum execution time: 39_000_000 picoseconds. + Weight::from_parts(39_000_000, 3485) + .saturating_add(T::DbWeight::get().reads(4_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) + } + /// Storage: EthereumOutboundQueue MessageLeaves (r:1 w:0) + /// Proof Skipped: EthereumOutboundQueue MessageLeaves (max_values: Some(1), max_size: None, mode: Measured) + /// Storage: System Digest (r:1 w:1) + /// Proof Skipped: System Digest (max_values: Some(1), max_size: None, mode: Measured) + fn commit() -> Weight { + // Proof Size summary in bytes: + // Measured: `1094` + // Estimated: `2579` + // Minimum execution time: 28_000_000 picoseconds. + Weight::from_parts(28_000_000, 2579) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + + fn commit_single() -> Weight { + // Proof Size summary in bytes: + // Measured: `1094` + // Estimated: `2579` + // Minimum execution time: 9_000_000 picoseconds. + Weight::from_parts(9_000_000, 1586) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } +} diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_system.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_system.rs new file mode 100644 index 000000000000..88c6c669c880 --- /dev/null +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_system.rs @@ -0,0 +1,256 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Autogenerated weights for `snowbridge_system` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-10-09, STEPS: `2`, REPEAT: `1`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `crake.local`, CPU: `` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("bridge-hub-rococo-dev"), DB CACHE: 1024 + +// Executed Command: +// target/release/polkadot-parachain +// benchmark +// pallet +// --chain +// bridge-hub-rococo-dev +// --pallet=snowbridge_system +// --extrinsic=* +// --execution=wasm +// --wasm-execution=compiled +// --output +// parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/snowbridge_system.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(missing_docs)] + +use frame_support::{traits::Get, weights::Weight}; +use core::marker::PhantomData; + +/// Weight functions for `snowbridge_system`. +pub struct WeightInfo(PhantomData); +impl snowbridge_system::WeightInfo for WeightInfo { + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:1 w:1) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:1) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn upgrade() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3517` + // Minimum execution time: 47_000_000 picoseconds. + Weight::from_parts(47_000_000, 0) + .saturating_add(Weight::from_parts(0, 3517)) + .saturating_add(T::DbWeight::get().reads(4)) + .saturating_add(T::DbWeight::get().writes(3)) + } + /// Storage: EthereumSystem Agents (r:1 w:1) + /// Proof: EthereumSystem Agents (max_values: None, max_size: Some(40), added: 2515, mode: MaxEncodedLen) + /// Storage: System Account (r:2 w:2) + /// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen) + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:1 w:1) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:1) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn create_agent() -> Weight { + // Proof Size summary in bytes: + // Measured: `187` + // Estimated: `6196` + // Minimum execution time: 87_000_000 picoseconds. + Weight::from_parts(87_000_000, 0) + .saturating_add(Weight::from_parts(0, 6196)) + .saturating_add(T::DbWeight::get().reads(7)) + .saturating_add(T::DbWeight::get().writes(6)) + } + /// Storage: System Account (r:2 w:2) + /// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen) + /// Storage: EthereumSystem Agents (r:1 w:0) + /// Proof: EthereumSystem Agents (max_values: None, max_size: Some(40), added: 2515, mode: MaxEncodedLen) + /// Storage: EthereumSystem Channels (r:1 w:1) + /// Proof: EthereumSystem Channels (max_values: None, max_size: Some(12), added: 2487, mode: MaxEncodedLen) + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:1 w:1) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:1 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn create_channel() -> Weight { + // Proof Size summary in bytes: + // Measured: `602` + // Estimated: `69050` + // Minimum execution time: 84_000_000 picoseconds. + Weight::from_parts(84_000_000, 0) + .saturating_add(Weight::from_parts(0, 69050)) + .saturating_add(T::DbWeight::get().reads(8)) + .saturating_add(T::DbWeight::get().writes(5)) + } + /// Storage: EthereumSystem Channels (r:1 w:0) + /// Proof: EthereumSystem Channels (max_values: None, max_size: Some(12), added: 2487, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:2 w:2) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:0) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn update_channel() -> Weight { + // Proof Size summary in bytes: + // Measured: `256` + // Estimated: `6044` + // Minimum execution time: 41_000_000 picoseconds. + Weight::from_parts(41_000_000, 0) + .saturating_add(Weight::from_parts(0, 6044)) + .saturating_add(T::DbWeight::get().reads(5)) + .saturating_add(T::DbWeight::get().writes(3)) + } + /// Storage: EthereumSystem Channels (r:1 w:0) + /// Proof: EthereumSystem Channels (max_values: None, max_size: Some(12), added: 2487, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:2 w:2) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:0) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn force_update_channel() -> Weight { + // Proof Size summary in bytes: + // Measured: `256` + // Estimated: `6044` + // Minimum execution time: 41_000_000 picoseconds. + Weight::from_parts(41_000_000, 0) + .saturating_add(Weight::from_parts(0, 6044)) + .saturating_add(T::DbWeight::get().reads(5)) + .saturating_add(T::DbWeight::get().writes(3)) + } + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:1 w:1) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:1) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn set_operating_mode() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3517` + // Minimum execution time: 30_000_000 picoseconds. + Weight::from_parts(30_000_000, 0) + .saturating_add(Weight::from_parts(0, 3517)) + .saturating_add(T::DbWeight::get().reads(4)) + .saturating_add(T::DbWeight::get().writes(3)) + } + /// Storage: EthereumSystem Agents (r:1 w:0) + /// Proof: EthereumSystem Agents (max_values: None, max_size: Some(40), added: 2515, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:2 w:2) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:0) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn transfer_native_from_agent() -> Weight { + // Proof Size summary in bytes: + // Measured: `252` + // Estimated: `6044` + // Minimum execution time: 43_000_000 picoseconds. + Weight::from_parts(43_000_000, 0) + .saturating_add(Weight::from_parts(0, 6044)) + .saturating_add(T::DbWeight::get().reads(5)) + .saturating_add(T::DbWeight::get().writes(3)) + } + /// Storage: EthereumSystem Agents (r:1 w:0) + /// Proof: EthereumSystem Agents (max_values: None, max_size: Some(40), added: 2515, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:2 w:2) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:0) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn force_transfer_native_from_agent() -> Weight { + // Proof Size summary in bytes: + // Measured: `252` + // Estimated: `6044` + // Minimum execution time: 42_000_000 picoseconds. + Weight::from_parts(42_000_000, 0) + .saturating_add(Weight::from_parts(0, 6044)) + .saturating_add(T::DbWeight::get().reads(5)) + .saturating_add(T::DbWeight::get().writes(3)) + } + + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:1 w:1) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:1) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn set_token_transfer_fees() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3517` + // Minimum execution time: 31_000_000 picoseconds. + Weight::from_parts(42_000_000, 3517) + .saturating_add(T::DbWeight::get().reads(4_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + } + + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: EthereumOutboundQueue PalletOperatingMode (r:1 w:0) + /// Proof: EthereumOutboundQueue PalletOperatingMode (max_values: Some(1), max_size: Some(1), added: 496, mode: MaxEncodedLen) + /// Storage: MessageQueue BookStateFor (r:1 w:1) + /// Proof: MessageQueue BookStateFor (max_values: None, max_size: Some(52), added: 2527, mode: MaxEncodedLen) + /// Storage: MessageQueue ServiceHead (r:1 w:1) + /// Proof: MessageQueue ServiceHead (max_values: Some(1), max_size: Some(5), added: 500, mode: MaxEncodedLen) + /// Storage: MessageQueue Pages (r:0 w:1) + /// Proof: MessageQueue Pages (max_values: None, max_size: Some(65585), added: 68060, mode: MaxEncodedLen) + fn set_pricing_parameters() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3517` + // Minimum execution time: 31_000_000 picoseconds. + Weight::from_parts(42_000_000, 3517) + .saturating_add(T::DbWeight::get().reads(4_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + } +} diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs index f89e7b39db85..ac5c4afd52d8 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs @@ -26,6 +26,7 @@ use crate::{ }, bridge_to_bulletin_config::WithRococoBulletinMessagesInstance, bridge_to_westend_config::WithBridgeHubWestendMessagesInstance, + EthereumGatewayAddress, }; use bp_messages::LaneId; use bp_relayers::{PayRewardFromAccount, RewardsAccountOwner, RewardsAccountParams}; @@ -46,24 +47,26 @@ use parachains_common::{ }; use polkadot_parachain_primitives::primitives::Sibling; use polkadot_runtime_common::xcm_sender::ExponentialPrice; -use sp_core::Get; +use snowbridge_core::DescribeHere; +use snowbridge_rococo_common::EthereumNetwork; +use snowbridge_runtime_common::XcmExportFeeToSibling; +use sp_core::{Get, H256}; use sp_runtime::traits::AccountIdConversion; use sp_std::marker::PhantomData; use xcm::latest::prelude::*; #[allow(deprecated)] -use xcm_builder::CurrencyAdapter; use xcm_builder::{ deposit_or_burn_fee, AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, - DenyReserveTransferToRelayChain, DenyThenTry, EnsureXcmOrigin, HandleFee, IsConcrete, - ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, - SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, - SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId, UsingComponents, - WeightInfoBounds, WithComputedOrigin, WithUniqueTopic, XcmFeeManagerFromComponents, + CurrencyAdapter, DenyReserveTransferToRelayChain, DenyThenTry, DescribeAllTerminal, + DescribeFamily, EnsureXcmOrigin, HandleFee, HashedDescription, IsConcrete, ParentAsSuperuser, + ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, + SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, + TrailingSetTopicAsId, UsingComponents, WeightInfoBounds, WithComputedOrigin, WithUniqueTopic, XcmFeeToAccount, }; use xcm_executor::{ - traits::{FeeReason, TransactAsset, WithOriginFilter}, + traits::{FeeManager, FeeReason, FeeReason::Export, TransactAsset, WithOriginFilter}, XcmExecutor, }; @@ -160,7 +163,8 @@ impl Contains for SafeCallFilter { RuntimeCall::System(frame_system::Call::set_storage { items }) if items.iter().all(|(k, _)| { k.eq(&DeliveryRewardInBalance::key()) | - k.eq(&RequiredStakeForStakeAndSlash::key()) + k.eq(&RequiredStakeForStakeAndSlash::key()) | + k.eq(&EthereumGatewayAddress::key()) }) => return true, _ => (), @@ -175,19 +179,14 @@ impl Contains for SafeCallFilter { frame_system::Call::set_heap_pages { .. } | frame_system::Call::set_code { .. } | frame_system::Call::set_code_without_checks { .. } | + frame_system::Call::authorize_upgrade { .. } | + frame_system::Call::authorize_upgrade_without_checks { .. } | frame_system::Call::kill_prefix { .. }, ) | RuntimeCall::ParachainSystem(..) | RuntimeCall::Timestamp(..) | RuntimeCall::Balances(..) | - RuntimeCall::CollatorSelection( - pallet_collator_selection::Call::set_desired_candidates { .. } | - pallet_collator_selection::Call::set_candidacy_bond { .. } | - pallet_collator_selection::Call::register_as_candidate { .. } | - pallet_collator_selection::Call::leave_intent { .. } | - pallet_collator_selection::Call::set_invulnerables { .. } | - pallet_collator_selection::Call::add_invulnerable { .. } | - pallet_collator_selection::Call::remove_invulnerable { .. }, - ) | RuntimeCall::Session(pallet_session::Call::purge_keys { .. }) | + RuntimeCall::CollatorSelection(..) | + RuntimeCall::Session(pallet_session::Call::purge_keys { .. }) | RuntimeCall::XcmpQueue(..) | RuntimeCall::MessageQueue(..) | RuntimeCall::BridgeWestendGrandpa(pallet_bridge_grandpa::Call::< @@ -217,7 +216,15 @@ impl Contains for SafeCallFilter { RuntimeCall::BridgePolkadotBulletinMessages(pallet_bridge_messages::Call::< Runtime, WithRococoBulletinMessagesInstance, - >::set_operating_mode { .. }) + >::set_operating_mode { .. }) | + RuntimeCall::EthereumBeaconClient( + snowbridge_ethereum_beacon_client::Call::force_checkpoint { .. } | + snowbridge_ethereum_beacon_client::Call::set_operating_mode { .. }, + ) | RuntimeCall::EthereumInboundQueue( + snowbridge_inbound_queue::Call::set_operating_mode { .. }, + ) | RuntimeCall::EthereumOutboundQueue( + snowbridge_outbound_queue::Call::set_operating_mode { .. }, + ) | RuntimeCall::EthereumSystem(..) ) } } @@ -291,7 +298,7 @@ impl xcm_executor::Config for XcmConfig { type SubscriptionService = PolkadotXcm; type PalletInstancesInfo = AllPalletsWithSystem; type MaxAssetsIntoHolding = MaxAssetsIntoHolding; - type FeeManager = XcmFeeManagerFromComponents< + type FeeManager = XcmFeeManagerFromComponentsBridgeHub< WaivedLocations, ( XcmExportFeeToRelayerRewardAccounts< @@ -301,12 +308,21 @@ impl xcm_executor::Config for XcmConfig { crate::bridge_to_westend_config::BridgeHubWestendChainId, crate::bridge_to_westend_config::AssetHubRococoToAssetHubWestendMessagesLane, >, + XcmExportFeeToSibling< + bp_rococo::Balance, + AccountId, + TokenLocation, + EthereumNetwork, + Self::AssetTransactor, + crate::EthereumOutboundQueue, + >, XcmFeeToAccount, ), >; type MessageExporter = ( crate::bridge_to_westend_config::ToBridgeHubWestendHaulBlobExporter, crate::bridge_to_bulletin_config::ToRococoBulletinHaulBlobExporter, + crate::bridge_to_ethereum_config::SnowbridgeExporter, ); type UniversalAliases = Nothing; type CallDispatcher = WithOriginFilter; @@ -367,6 +383,10 @@ impl cumulus_pallet_xcm::Config for Runtime { type XcmExecutor = XcmExecutor; } +/// Creates an AgentId from a MultiLocation. An AgentId is a unique mapping to a Agent contract on +/// Ethereum which acts as the sovereign account for the MultiLocation. +pub type AgentIdOf = HashedDescription)>; + /// A `HandleFee` implementation that simply deposits the fees for `ExportMessage` XCM instructions /// into the accounts that are used for paying the relayer rewards. /// Burns the fees in case of a failure. @@ -459,3 +479,41 @@ impl< fee } } + +pub struct XcmFeeManagerFromComponentsBridgeHub( + PhantomData<(WaivedLocations, HandleFee)>, +); +impl, FeeHandler: HandleFee> FeeManager + for XcmFeeManagerFromComponentsBridgeHub +{ + fn is_waived(origin: Option<&MultiLocation>, fee_reason: FeeReason) -> bool { + let Some(loc) = origin else { return false }; + if let Export { network, destination: Here } = fee_reason { + return !(network == EthereumNetwork::get()) + } + WaivedLocations::contains(loc) + } + + fn handle_fee(fee: MultiAssets, context: Option<&XcmContext>, reason: FeeReason) { + FeeHandler::handle_fee(fee, context, reason); + } +} + +#[cfg(feature = "runtime-benchmarks")] +pub mod benchmark_helpers { + use crate::{MultiAssets, MultiLocation, SendError, SendResult, SendXcm, Xcm, XcmHash}; + + pub struct DoNothingRouter; + impl SendXcm for DoNothingRouter { + type Ticket = (); + fn validate( + _dest: &mut Option, + _msg: &mut Option>, + ) -> SendResult<()> { + Ok(((), MultiAssets::new())) + } + fn deliver(_: ()) -> Result { + Ok([0; 32]) + } + } +} diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/tests/tests.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/tests/tests.rs index 390ac449f8cc..0fba28c47b43 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/tests/tests.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/tests/tests.rs @@ -20,13 +20,14 @@ use bp_polkadot_core::Signature; use bridge_hub_rococo_runtime::{ bridge_common_config, bridge_to_bulletin_config, bridge_to_westend_config, xcm_config::{RelayNetwork, TokenLocation, XcmConfig}, - AllPalletsWithoutSystem, BridgeRejectObsoleteHeadersAndMessages, Executive, ExistentialDeposit, - ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, SessionKeys, - SignedExtra, TransactionPayment, UncheckedExtrinsic, + AllPalletsWithoutSystem, BridgeRejectObsoleteHeadersAndMessages, EthereumGatewayAddress, + Executive, ExistentialDeposit, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, + RuntimeEvent, RuntimeOrigin, SessionKeys, SignedExtra, TransactionPayment, UncheckedExtrinsic, }; use codec::{Decode, Encode}; use frame_support::{dispatch::GetDispatchInfo, parameter_types, traits::ConstU8}; use parachains_common::{rococo::fee::WeightToFee, AccountId, AuraId, Balance}; +use sp_core::H160; use sp_keyring::AccountKeyring::Alice; use sp_runtime::{ generic::{Era, SignedPayload}, @@ -137,6 +138,7 @@ mod bridge_hub_westend_tests { use bridge_common_config::{ BridgeGrandpaWestendInstance, BridgeParachainWestendInstance, DeliveryRewardInBalance, }; + use bridge_hub_test_utils::test_cases::from_parachain; use bridge_to_westend_config::{ BridgeHubWestendChainId, BridgeHubWestendLocation, WestendGlobalConsensusNetwork, WithBridgeHubWestendMessageBridge, WithBridgeHubWestendMessagesInstance, @@ -146,6 +148,16 @@ mod bridge_hub_westend_tests { // Para id of sibling chain used in tests. pub const SIBLING_PARACHAIN_ID: u32 = 1000; + // Runtime from tests PoV + type RuntimeTestsAdapter = from_parachain::WithRemoteParachainHelperAdapter< + Runtime, + AllPalletsWithoutSystem, + BridgeGrandpaWestendInstance, + BridgeParachainWestendInstance, + WithBridgeHubWestendMessagesInstance, + WithBridgeHubWestendMessageBridge, + >; + #[test] fn initialize_bridge_by_governance_works() { // for RococoBulletin finality @@ -182,6 +194,21 @@ mod bridge_hub_westend_tests { >(collator_session_keys(), bp_bridge_hub_rococo::BRIDGE_HUB_ROCOCO_PARACHAIN_ID) } + #[test] + fn change_ethereum_gateway_by_governance_works() { + bridge_hub_test_utils::test_cases::change_storage_constant_by_governance_works::< + Runtime, + EthereumGatewayAddress, + H160, + >( + collator_session_keys(), + bp_bridge_hub_rococo::BRIDGE_HUB_ROCOCO_PARACHAIN_ID, + Box::new(|call| RuntimeCall::System(call).encode()), + || (EthereumGatewayAddress::key().to_vec(), EthereumGatewayAddress::get()), + |_| [1; 20].into(), + ) + } + #[test] fn change_delivery_reward_by_governance_works() { bridge_hub_test_utils::test_cases::change_storage_constant_by_governance_works::< @@ -259,15 +286,7 @@ mod bridge_hub_westend_tests { #[test] fn relayed_incoming_message_works() { // from Westend - bridge_hub_test_utils::test_cases::from_parachain::relayed_incoming_message_works::< - Runtime, - AllPalletsWithoutSystem, - ParachainSystem, - BridgeGrandpaWestendInstance, - BridgeParachainWestendInstance, - WithBridgeHubWestendMessagesInstance, - WithBridgeHubWestendMessageBridge, - >( + from_parachain::relayed_incoming_message_works::( collator_session_keys(), bp_bridge_hub_rococo::BRIDGE_HUB_ROCOCO_PARACHAIN_ID, bp_bridge_hub_westend::BRIDGE_HUB_WESTEND_PARACHAIN_ID, @@ -283,16 +302,7 @@ mod bridge_hub_westend_tests { #[test] pub fn complex_relay_extrinsic_works() { // for Westend - bridge_hub_test_utils::test_cases::from_parachain::complex_relay_extrinsic_works::< - Runtime, - AllPalletsWithoutSystem, - XcmConfig, - ParachainSystem, - BridgeGrandpaWestendInstance, - BridgeParachainWestendInstance, - WithBridgeHubWestendMessagesInstance, - WithBridgeHubWestendMessageBridge, - >( + from_parachain::complex_relay_extrinsic_works::( collator_session_keys(), bp_bridge_hub_rococo::BRIDGE_HUB_ROCOCO_PARACHAIN_ID, bp_bridge_hub_westend::BRIDGE_HUB_WESTEND_PARACHAIN_ID, @@ -325,16 +335,9 @@ mod bridge_hub_westend_tests { #[test] pub fn can_calculate_fee_for_complex_message_delivery_transaction() { - let estimated = bridge_hub_test_utils::test_cases::from_parachain::can_calculate_fee_for_complex_message_delivery_transaction::< - Runtime, - BridgeGrandpaWestendInstance, - BridgeParachainWestendInstance, - WithBridgeHubWestendMessagesInstance, - WithBridgeHubWestendMessageBridge, - >( - collator_session_keys(), - construct_and_estimate_extrinsic_fee - ); + let estimated = from_parachain::can_calculate_fee_for_complex_message_delivery_transaction::< + RuntimeTestsAdapter, + >(collator_session_keys(), construct_and_estimate_extrinsic_fee); // check if estimated value is sane let max_expected = bp_bridge_hub_rococo::BridgeHubRococoBaseDeliveryFeeInRocs::get(); @@ -348,16 +351,10 @@ mod bridge_hub_westend_tests { #[test] pub fn can_calculate_fee_for_complex_message_confirmation_transaction() { - let estimated = bridge_hub_test_utils::test_cases::from_parachain::can_calculate_fee_for_complex_message_confirmation_transaction::< - Runtime, - BridgeGrandpaWestendInstance, - BridgeParachainWestendInstance, - WithBridgeHubWestendMessagesInstance, - WithBridgeHubWestendMessageBridge, - >( - collator_session_keys(), - construct_and_estimate_extrinsic_fee - ); + let estimated = + from_parachain::can_calculate_fee_for_complex_message_confirmation_transaction::< + RuntimeTestsAdapter, + >(collator_session_keys(), construct_and_estimate_extrinsic_fee); // check if estimated value is sane let max_expected = bp_bridge_hub_rococo::BridgeHubRococoBaseConfirmationFeeInRocs::get(); diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/Cargo.toml b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/Cargo.toml index 07c1a3282857..94e29fb90ac3 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/Cargo.toml +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/Cargo.toml @@ -95,6 +95,7 @@ pallet-bridge-parachains = { path = "../../../../../bridges/modules/parachains", pallet-bridge-relayers = { path = "../../../../../bridges/modules/relayers", default-features = false } pallet-xcm-bridge-hub = { path = "../../../../../bridges/modules/xcm-bridge-hub", default-features = false } bridge-runtime-common = { path = "../../../../../bridges/bin/runtime-common", default-features = false } +bridge-hub-common = { path = "../../bridge-hubs/common", default-features = false } [dev-dependencies] static_assertions = "1.1" @@ -117,6 +118,7 @@ std = [ "bp-rococo/std", "bp-runtime/std", "bp-westend/std", + "bridge-hub-common/std", "bridge-runtime-common/std", "codec/std", "cumulus-pallet-aura-ext/std", @@ -181,6 +183,7 @@ std = [ ] runtime-benchmarks = [ + "bridge-hub-common/runtime-benchmarks", "bridge-runtime-common/runtime-benchmarks", "cumulus-pallet-parachain-system/runtime-benchmarks", "cumulus-pallet-session-benchmarking/runtime-benchmarks", diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs index a052cd929b73..717cde6280db 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs @@ -33,8 +33,7 @@ mod weights; pub mod xcm_config; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; -use cumulus_primitives_core::{AggregateMessageOrigin, ParaId}; -use parachains_common::message_queue::{NarrowOriginToSibling, ParaIdToSibling}; +use cumulus_primitives_core::ParaId; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ @@ -49,6 +48,10 @@ use sp_std::prelude::*; use sp_version::NativeVersion; use sp_version::RuntimeVersion; +use bridge_hub_common::{ + message_queue::{NarrowOriginToSibling, ParaIdToSibling}, + AggregateMessageOrigin, +}; use frame_support::{ construct_runtime, derive_impl, dispatch::DispatchClass, @@ -328,9 +331,8 @@ impl pallet_message_queue::Config for Runtime { type RuntimeEvent = RuntimeEvent; type WeightInfo = weights::pallet_message_queue::WeightInfo; #[cfg(feature = "runtime-benchmarks")] - type MessageProcessor = pallet_message_queue::mock_helpers::NoopMessageProcessor< - cumulus_primitives_core::AggregateMessageOrigin, - >; + type MessageProcessor = + pallet_message_queue::mock_helpers::NoopMessageProcessor; #[cfg(not(feature = "runtime-benchmarks"))] type MessageProcessor = xcm_builder::ProcessXcmMessage< AggregateMessageOrigin, diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/xcm_config.rs index d112328723d8..397019190f3f 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/xcm_config.rs @@ -158,19 +158,14 @@ impl Contains for SafeCallFilter { frame_system::Call::set_heap_pages { .. } | frame_system::Call::set_code { .. } | frame_system::Call::set_code_without_checks { .. } | + frame_system::Call::authorize_upgrade { .. } | + frame_system::Call::authorize_upgrade_without_checks { .. } | frame_system::Call::kill_prefix { .. }, ) | RuntimeCall::ParachainSystem(..) | RuntimeCall::Timestamp(..) | RuntimeCall::Balances(..) | - RuntimeCall::CollatorSelection( - pallet_collator_selection::Call::set_desired_candidates { .. } | - pallet_collator_selection::Call::set_candidacy_bond { .. } | - pallet_collator_selection::Call::register_as_candidate { .. } | - pallet_collator_selection::Call::leave_intent { .. } | - pallet_collator_selection::Call::set_invulnerables { .. } | - pallet_collator_selection::Call::add_invulnerable { .. } | - pallet_collator_selection::Call::remove_invulnerable { .. }, - ) | RuntimeCall::Session(pallet_session::Call::purge_keys { .. }) | + RuntimeCall::CollatorSelection(..) | + RuntimeCall::Session(pallet_session::Call::purge_keys { .. }) | RuntimeCall::XcmpQueue(..) | RuntimeCall::MessageQueue(..) | RuntimeCall::BridgeRococoGrandpa(pallet_bridge_grandpa::Call::< diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/tests/tests.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/tests/tests.rs index 9a7f13f14c31..0e58b7b408eb 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/tests/tests.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/tests/tests.rs @@ -18,6 +18,7 @@ use bp_polkadot_core::Signature; use bridge_common_config::{DeliveryRewardInBalance, RequiredStakeForStakeAndSlash}; +use bridge_hub_test_utils::test_cases::from_parachain; use bridge_hub_westend_runtime::{ bridge_common_config, bridge_to_rococo_config, xcm_config::{RelayNetwork, WestendLocation, XcmConfig}, @@ -43,6 +44,16 @@ use xcm::latest::prelude::*; // Para id of sibling chain used in tests. pub const SIBLING_PARACHAIN_ID: u32 = 1000; +// Runtime from tests PoV +type RuntimeTestsAdapter = from_parachain::WithRemoteParachainHelperAdapter< + Runtime, + AllPalletsWithoutSystem, + BridgeGrandpaRococoInstance, + BridgeParachainRococoInstance, + WithBridgeHubRococoMessagesInstance, + WithBridgeHubRococoMessageBridge, +>; + parameter_types! { pub CheckingAccount: AccountId = PolkadotXcm::check_account(); } @@ -239,15 +250,7 @@ fn message_dispatch_routing_works() { #[test] fn relayed_incoming_message_works() { - bridge_hub_test_utils::test_cases::from_parachain::relayed_incoming_message_works::< - Runtime, - AllPalletsWithoutSystem, - ParachainSystem, - BridgeGrandpaRococoInstance, - BridgeParachainRococoInstance, - WithBridgeHubRococoMessagesInstance, - WithBridgeHubRococoMessageBridge, - >( + from_parachain::relayed_incoming_message_works::( collator_session_keys(), bp_bridge_hub_westend::BRIDGE_HUB_WESTEND_PARACHAIN_ID, bp_bridge_hub_rococo::BRIDGE_HUB_ROCOCO_PARACHAIN_ID, @@ -262,16 +265,7 @@ fn relayed_incoming_message_works() { #[test] pub fn complex_relay_extrinsic_works() { - bridge_hub_test_utils::test_cases::from_parachain::complex_relay_extrinsic_works::< - Runtime, - AllPalletsWithoutSystem, - XcmConfig, - ParachainSystem, - BridgeGrandpaRococoInstance, - BridgeParachainRococoInstance, - WithBridgeHubRococoMessagesInstance, - WithBridgeHubRococoMessageBridge, - >( + from_parachain::complex_relay_extrinsic_works::( collator_session_keys(), bp_bridge_hub_westend::BRIDGE_HUB_WESTEND_PARACHAIN_ID, bp_bridge_hub_rococo::BRIDGE_HUB_ROCOCO_PARACHAIN_ID, @@ -304,16 +298,9 @@ pub fn can_calculate_weight_for_paid_export_message_with_reserve_transfer() { #[test] pub fn can_calculate_fee_for_complex_message_delivery_transaction() { - let estimated = bridge_hub_test_utils::test_cases::from_parachain::can_calculate_fee_for_complex_message_delivery_transaction::< - Runtime, - BridgeGrandpaRococoInstance, - BridgeParachainRococoInstance, - WithBridgeHubRococoMessagesInstance, - WithBridgeHubRococoMessageBridge, - >( - collator_session_keys(), - construct_and_estimate_extrinsic_fee - ); + let estimated = from_parachain::can_calculate_fee_for_complex_message_delivery_transaction::< + RuntimeTestsAdapter, + >(collator_session_keys(), construct_and_estimate_extrinsic_fee); // check if estimated value is sane let max_expected = bp_bridge_hub_westend::BridgeHubWestendBaseDeliveryFeeInWnds::get(); @@ -327,16 +314,9 @@ pub fn can_calculate_fee_for_complex_message_delivery_transaction() { #[test] pub fn can_calculate_fee_for_complex_message_confirmation_transaction() { - let estimated = bridge_hub_test_utils::test_cases::from_parachain::can_calculate_fee_for_complex_message_confirmation_transaction::< - Runtime, - BridgeGrandpaRococoInstance, - BridgeParachainRococoInstance, - WithBridgeHubRococoMessagesInstance, - WithBridgeHubRococoMessageBridge, - >( - collator_session_keys(), - construct_and_estimate_extrinsic_fee - ); + let estimated = from_parachain::can_calculate_fee_for_complex_message_confirmation_transaction::< + RuntimeTestsAdapter, + >(collator_session_keys(), construct_and_estimate_extrinsic_fee); // check if estimated value is sane let max_expected = bp_bridge_hub_westend::BridgeHubWestendBaseConfirmationFeeInWnds::get(); diff --git a/cumulus/parachains/runtimes/bridge-hubs/common/Cargo.toml b/cumulus/parachains/runtimes/bridge-hubs/common/Cargo.toml new file mode 100644 index 000000000000..0d75bb2213f8 --- /dev/null +++ b/cumulus/parachains/runtimes/bridge-hubs/common/Cargo.toml @@ -0,0 +1,42 @@ +[package] +name = "bridge-hub-common" +version = "0.1.0" +authors.workspace = true +edition.workspace = true +description = "Bridge hub common utilities" +license = "Apache-2.0" + +[dependencies] +codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } +scale-info = { version = "2.10.0", default-features = false, features = ["derive"] } +frame-support = { path = "../../../../../substrate/frame/support", default-features = false } +sp-std = { path = "../../../../../substrate/primitives/std", default-features = false } +sp-core = { path = "../../../../../substrate/primitives/core", default-features = false } +sp-runtime = { path = "../../../../../substrate/primitives/runtime", default-features = false } +cumulus-primitives-core = { path = "../../../../primitives/core", default-features = false } +xcm = { package = "staging-xcm", path = "../../../../../polkadot/xcm", default-features = false } +pallet-message-queue = { path = "../../../../../substrate/frame/message-queue", default-features = false } +snowbridge-core = { path = "../../../../../bridges/snowbridge/parachain/primitives/core", default-features = false } + +[features] +default = ["std"] +std = [ + "codec/std", + "cumulus-primitives-core/std", + "frame-support/std", + "pallet-message-queue/std", + "scale-info/std", + "snowbridge-core/std", + "sp-core/std", + "sp-runtime/std", + "sp-std/std", + "xcm/std", +] + +runtime-benchmarks = [ + "cumulus-primitives-core/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "pallet-message-queue/runtime-benchmarks", + "snowbridge-core/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", +] diff --git a/cumulus/parachains/runtimes/bridge-hubs/common/src/digest_item.rs b/cumulus/parachains/runtimes/bridge-hubs/common/src/digest_item.rs new file mode 100644 index 000000000000..bdfcaedbe82d --- /dev/null +++ b/cumulus/parachains/runtimes/bridge-hubs/common/src/digest_item.rs @@ -0,0 +1,34 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//! Custom digest items + +use codec::{Decode, Encode}; +use sp_core::{RuntimeDebug, H256}; +use sp_runtime::generic::DigestItem; + +/// Custom header digest items, inserted as DigestItem::Other +#[derive(Encode, Decode, Copy, Clone, Eq, PartialEq, RuntimeDebug)] +pub enum CustomDigestItem { + #[codec(index = 0)] + /// Merkle root of outbound Snowbridge messages. + Snowbridge(H256), +} + +/// Convert custom application digest item into a concrete digest item +impl From for DigestItem { + fn from(val: CustomDigestItem) -> Self { + DigestItem::Other(val.encode()) + } +} diff --git a/cumulus/parachains/runtimes/bridge-hubs/common/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/common/src/lib.rs new file mode 100644 index 000000000000..aac6eb036526 --- /dev/null +++ b/cumulus/parachains/runtimes/bridge-hubs/common/src/lib.rs @@ -0,0 +1,21 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#![cfg_attr(not(feature = "std"), no_std)] + +pub mod digest_item; +pub mod message_queue; + +pub use digest_item::CustomDigestItem; +pub use message_queue::{AggregateMessageOrigin, BridgeHubMessageRouter}; diff --git a/cumulus/parachains/runtimes/bridge-hubs/common/src/message_queue.rs b/cumulus/parachains/runtimes/bridge-hubs/common/src/message_queue.rs new file mode 100644 index 000000000000..651537ff8b71 --- /dev/null +++ b/cumulus/parachains/runtimes/bridge-hubs/common/src/message_queue.rs @@ -0,0 +1,146 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//! Runtime configuration for MessageQueue pallet +use codec::{Decode, Encode, MaxEncodedLen}; +use cumulus_primitives_core::{AggregateMessageOrigin as CumulusAggregateMessageOrigin, ParaId}; +use frame_support::{ + traits::{ProcessMessage, ProcessMessageError, QueueFootprint, QueuePausedQuery}, + weights::WeightMeter, +}; +use pallet_message_queue::OnQueueChanged; +use scale_info::TypeInfo; +use snowbridge_core::ChannelId; +use sp_std::{marker::PhantomData, prelude::*}; +use xcm::v3::{Junction, MultiLocation}; + +/// The aggregate origin of an inbound message. +/// This is specialized for BridgeHub, as the snowbridge-outbound-queue pallet is also using +/// the shared MessageQueue pallet. +#[derive(Encode, Decode, Copy, MaxEncodedLen, Clone, Eq, PartialEq, TypeInfo, Debug)] +pub enum AggregateMessageOrigin { + /// The message came from the para-chain itself. + Here, + /// The message came from the relay-chain. + /// + /// This is used by the DMP queue. + Parent, + /// The message came from a sibling para-chain. + /// + /// This is used by the HRMP queue. + Sibling(ParaId), + /// The message came from a snowbridge channel. + /// + /// This is used by Snowbridge inbound queue. + Snowbridge(ChannelId), +} + +impl From for MultiLocation { + fn from(origin: AggregateMessageOrigin) -> Self { + use AggregateMessageOrigin::*; + match origin { + Here => MultiLocation::here(), + Parent => MultiLocation::parent(), + Sibling(id) => MultiLocation::new(1, Junction::Parachain(id.into())), + // NOTE: We don't need this conversion for Snowbridge. However we have to + // implement it anyway as xcm_builder::ProcessXcmMessage requires it. + Snowbridge(_) => MultiLocation::default(), + } + } +} + +impl From for AggregateMessageOrigin { + fn from(origin: CumulusAggregateMessageOrigin) -> Self { + match origin { + CumulusAggregateMessageOrigin::Here => Self::Here, + CumulusAggregateMessageOrigin::Parent => Self::Parent, + CumulusAggregateMessageOrigin::Sibling(id) => Self::Sibling(id), + } + } +} + +#[cfg(feature = "runtime-benchmarks")] +impl From for AggregateMessageOrigin { + fn from(x: u32) -> Self { + match x { + 0 => Self::Here, + 1 => Self::Parent, + p => Self::Sibling(ParaId::from(p)), + } + } +} + +/// Routes messages to either the XCMP or Snowbridge processor. +pub struct BridgeHubMessageRouter( + PhantomData<(XcmpProcessor, SnowbridgeProcessor)>, +) +where + XcmpProcessor: ProcessMessage, + SnowbridgeProcessor: ProcessMessage; + +impl ProcessMessage + for BridgeHubMessageRouter +where + XcmpProcessor: ProcessMessage, + SnowbridgeProcessor: ProcessMessage, +{ + type Origin = AggregateMessageOrigin; + + fn process_message( + message: &[u8], + origin: Self::Origin, + meter: &mut WeightMeter, + id: &mut [u8; 32], + ) -> Result { + use AggregateMessageOrigin::*; + match origin { + Here | Parent | Sibling(_) => + XcmpProcessor::process_message(message, origin, meter, id), + Snowbridge(_) => SnowbridgeProcessor::process_message(message, origin, meter, id), + } + } +} + +/// Narrow the scope of the `Inner` query from `AggregateMessageOrigin` to `ParaId`. +/// +/// All non-`Sibling` variants will be ignored. +pub struct NarrowOriginToSibling(PhantomData); +impl> QueuePausedQuery + for NarrowOriginToSibling +{ + fn is_paused(origin: &AggregateMessageOrigin) -> bool { + match origin { + AggregateMessageOrigin::Sibling(id) => Inner::is_paused(id), + _ => false, + } + } +} + +impl> OnQueueChanged + for NarrowOriginToSibling +{ + fn on_queue_changed(origin: AggregateMessageOrigin, fp: QueueFootprint) { + if let AggregateMessageOrigin::Sibling(id) = origin { + Inner::on_queue_changed(id, fp) + } + } +} + +/// Convert a sibling `ParaId` to an `AggregateMessageOrigin`. +pub struct ParaIdToSibling; +impl sp_runtime::traits::Convert for ParaIdToSibling { + fn convert(para_id: ParaId) -> AggregateMessageOrigin { + AggregateMessageOrigin::Sibling(para_id) + } +} diff --git a/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/from_grandpa_chain.rs b/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/from_grandpa_chain.rs index e6407e60989e..e0e75f093cfc 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/from_grandpa_chain.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/from_grandpa_chain.rs @@ -18,7 +18,7 @@ //! with remote GRANDPA chain. use crate::{ - test_cases::{helpers, run_test}, + test_cases::{bridges_prelude::*, helpers, run_test}, test_data, }; @@ -38,8 +38,6 @@ use bridge_runtime_common::{ }; use frame_support::traits::{Get, OnFinalize, OnInitialize}; use frame_system::pallet_prelude::BlockNumberFor; -use pallet_bridge_grandpa::{Call as BridgeGrandpaCall, Config as BridgeGrandpaConfig}; -use pallet_bridge_messages::{Call as BridgeMessagesCall, Config as BridgeMessagesConfig}; use parachains_runtimes_test_utils::{ AccountIdOf, BasicParachainRuntime, CollatorSessionKeys, RuntimeCallOf, }; @@ -49,9 +47,9 @@ use xcm::latest::prelude::*; /// Helper trait to test bridges with remote GRANDPA chain. /// -/// This is only used to decrease amount of lines, dedicated to bounds +/// This is only used to decrease amount of lines, dedicated to bounds. pub trait WithRemoteGrandpaChainHelper { - /// This chaiin runtime. + /// This chain runtime. type Runtime: BasicParachainRuntime + cumulus_pallet_xcmp_queue::Config + BridgeGrandpaConfig< @@ -74,7 +72,7 @@ pub trait WithRemoteGrandpaChainHelper { type MB: MessageBridge; } -/// Adapter struct that implements `WithRemoteGrandpaChainHelper` +/// Adapter struct that implements [`WithRemoteGrandpaChainHelper`]. pub struct WithRemoteGrandpaChainHelperAdapter( sp_std::marker::PhantomData<(Runtime, AllPalletsWithoutSystem, GPI, MPI, MB)>, ); diff --git a/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/from_parachain.rs b/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/from_parachain.rs index dff71d23df87..91bebb36b187 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/from_parachain.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/from_parachain.rs @@ -18,47 +18,100 @@ //! with remote parachain. use crate::{ - test_cases::{helpers, run_test}, + test_cases::{bridges_prelude::*, helpers, run_test}, test_data, }; +use bp_header_chain::ChainWithGrandpa; use bp_messages::{ source_chain::TargetHeaderChain, target_chain::SourceHeaderChain, LaneId, UnrewardedRelayersState, }; use bp_polkadot_core::parachains::ParaHash; use bp_relayers::{RewardsAccountOwner, RewardsAccountParams}; -use bp_runtime::{Parachain, UnderlyingChainOf}; +use bp_runtime::{HashOf, Parachain, UnderlyingChainOf}; use bridge_runtime_common::{ messages::{ source::FromBridgedChainMessagesDeliveryProof, target::FromBridgedChainMessagesProof, - BridgedChain as MessageBridgedChain, MessageBridge, + BridgedChain as MessageBridgedChain, MessageBridge, ThisChain as MessageThisChain, }, messages_xcm_extension::XcmAsPlainPayload, }; -use frame_support::traits::{Get, OnFinalize, OnInitialize, OriginTrait}; +use frame_support::traits::{Get, OnFinalize, OnInitialize}; use frame_system::pallet_prelude::BlockNumberFor; -use pallet_bridge_parachains::{RelayBlockHash, RelayBlockNumber}; use parachains_runtimes_test_utils::{ - AccountIdOf, BasicParachainRuntime, CollatorSessionKeys, ValidatorIdOf, + AccountIdOf, BasicParachainRuntime, CollatorSessionKeys, RuntimeCallOf, }; use sp_keyring::AccountKeyring::*; use sp_runtime::{traits::Header as HeaderT, AccountId32}; use xcm::latest::prelude::*; +/// Helper trait to test bridges with remote parachain. +/// +/// This is only used to decrease amount of lines, dedicated to bounds. +pub trait WithRemoteParachainHelper { + /// This chain runtime. + type Runtime: BasicParachainRuntime + + cumulus_pallet_xcmp_queue::Config + + BridgeGrandpaConfig + + BridgeParachainsConfig + + BridgeMessagesConfig< + Self::MPI, + InboundPayload = XcmAsPlainPayload, + InboundRelayer = bp_runtime::AccountIdOf>, + OutboundPayload = XcmAsPlainPayload, + > + pallet_bridge_relayers::Config; + /// All pallets of this chain, excluding system pallet. + type AllPalletsWithoutSystem: OnInitialize> + + OnFinalize>; + /// Instance of the `pallet-bridge-grandpa`, used to bridge with remote relay chain. + type GPI: 'static; + /// Instance of the `pallet-bridge-parachains`, used to bridge with remote parachain. + type PPI: 'static; + /// Instance of the `pallet-bridge-messages`, used to bridge with remote parachain. + type MPI: 'static; + /// Messages bridge definition. + type MB: MessageBridge; +} + +/// Adapter struct that implements `WithRemoteParachainHelper`. +pub struct WithRemoteParachainHelperAdapter( + sp_std::marker::PhantomData<(Runtime, AllPalletsWithoutSystem, GPI, PPI, MPI, MB)>, +); + +impl WithRemoteParachainHelper + for WithRemoteParachainHelperAdapter +where + Runtime: BasicParachainRuntime + + cumulus_pallet_xcmp_queue::Config + + BridgeGrandpaConfig + + BridgeParachainsConfig + + BridgeMessagesConfig< + MPI, + InboundPayload = XcmAsPlainPayload, + InboundRelayer = bp_runtime::AccountIdOf>, + OutboundPayload = XcmAsPlainPayload, + > + pallet_bridge_relayers::Config, + AllPalletsWithoutSystem: + OnInitialize> + OnFinalize>, + GPI: 'static, + PPI: 'static, + MPI: 'static, + MB: MessageBridge, +{ + type Runtime = Runtime; + type AllPalletsWithoutSystem = AllPalletsWithoutSystem; + type GPI = GPI; + type PPI = PPI; + type MPI = MPI; + type MB = MB; +} + /// Test-case makes sure that Runtime can dispatch XCM messages submitted by relayer, /// with proofs (finality, para heads, message) independently submitted. /// Also verifies relayer transaction signed extensions work as intended. -pub fn relayed_incoming_message_works< - Runtime, - AllPalletsWithoutSystem, - HrmpChannelOpener, - GPI, - PPI, - MPI, - MB, ->( - collator_session_key: CollatorSessionKeys, +pub fn relayed_incoming_message_works( + collator_session_key: CollatorSessionKeys, runtime_para_id: u32, bridged_para_id: u32, bridged_chain_id: bp_runtime::ChainId, @@ -68,46 +121,30 @@ pub fn relayed_incoming_message_works< prepare_configuration: impl Fn(), construct_and_apply_extrinsic: fn( sp_keyring::AccountKeyring, - ::RuntimeCall, + ::RuntimeCall, ) -> sp_runtime::DispatchOutcome, ) where - Runtime: BasicParachainRuntime - + cumulus_pallet_xcmp_queue::Config - + cumulus_pallet_parachain_system::Config - + pallet_bridge_grandpa::Config - + pallet_bridge_parachains::Config - + pallet_bridge_messages::Config - + pallet_bridge_relayers::Config, - AllPalletsWithoutSystem: - OnInitialize> + OnFinalize>, - GPI: 'static, - PPI: 'static, - MPI: 'static, - MB: MessageBridge, - ::BridgedChain: Send + Sync + 'static, - ::ThisChain: Send + Sync + 'static, - UnderlyingChainOf>: bp_runtime::Chain + Parachain, - HrmpChannelOpener: frame_support::inherent::ProvideInherent< - Call = cumulus_pallet_parachain_system::Call, - >, - ValidatorIdOf: From>, - >::SourceHeaderChain: - SourceHeaderChain>, - >::BridgedChain: - bp_runtime::Chain, - ParaHash: From< - <>::BridgedChain as bp_runtime::Chain>::Hash, - >, - ::AccountId: - Into<<::RuntimeOrigin as OriginTrait>::AccountId>, - ::AccountId: From, - AccountIdOf: From, - >::InboundRelayer: From, - ::RuntimeCall: From> - + From> - + From>, + RuntimeHelper: WithRemoteParachainHelper, + AccountIdOf: From, + RuntimeCallOf: From> + + From> + + From>, + UnderlyingChainOf>: + bp_runtime::Chain + Parachain, + >::BridgedChain: + bp_runtime::Chain + ChainWithGrandpa, + >::SourceHeaderChain: + SourceHeaderChain< + MessagesProof = FromBridgedChainMessagesProof< + HashOf>, + >, + >, { - helpers::relayed_incoming_message_works::( + helpers::relayed_incoming_message_works::< + RuntimeHelper::Runtime, + RuntimeHelper::AllPalletsWithoutSystem, + RuntimeHelper::MPI, + >( collator_session_key, runtime_para_id, sibling_parachain_id, @@ -124,8 +161,8 @@ pub fn relayed_incoming_message_works< prepare_configuration(); // start with bridged relay chain block#0 - helpers::initialize_bridge_grandpa_pallet::( - test_data::initialization_data::(0), + helpers::initialize_bridge_grandpa_pallet::( + test_data::initialization_data::(0), ); // generate bridged relay chain finality, parachain heads and message proofs, @@ -138,8 +175,8 @@ pub fn relayed_incoming_message_works< para_heads_proof, message_proof, ) = test_data::from_parachain::make_complex_relayer_delivery_proofs::< - >::BridgedChain, - MB, + >::BridgedChain, + RuntimeHelper::MB, (), >( lane_id, @@ -156,30 +193,38 @@ pub fn relayed_incoming_message_works< let relay_chain_header_number = *relay_chain_header.number(); vec![ ( - pallet_bridge_grandpa::Call::::submit_finality_proof { + BridgeGrandpaCall::::submit_finality_proof { finality_target: Box::new(relay_chain_header), justification: grandpa_justification, }.into(), - helpers::VerifySubmitGrandpaFinalityProofOutcome::::expect_best_header_hash(relay_chain_header_hash), + helpers::VerifySubmitGrandpaFinalityProofOutcome::::expect_best_header_hash( + relay_chain_header_hash, + ), ), ( - pallet_bridge_parachains::Call::::submit_parachain_heads { + BridgeParachainsCall::::submit_parachain_heads { at_relay_block: (relay_chain_header_number, relay_chain_header_hash), parachains: parachain_heads, parachain_heads_proof: para_heads_proof, }.into(), - helpers::VerifySubmitParachainHeaderProofOutcome::::expect_best_header_hash(bridged_para_id, parachain_head_hash), + helpers::VerifySubmitParachainHeaderProofOutcome::::expect_best_header_hash( + bridged_para_id, + parachain_head_hash, + ), ), ( - pallet_bridge_messages::Call::::receive_messages_proof { + BridgeMessagesCall::::receive_messages_proof { relayer_id_at_bridged_chain, proof: message_proof, messages_count: 1, dispatch_weight: Weight::from_parts(1000000000, 0), }.into(), Box::new(( - helpers::VerifySubmitMessagesProofOutcome::::expect_last_delivered_nonce(lane_id, 1), - helpers::VerifyRelayerRewarded::::expect_relayer_reward( + helpers::VerifySubmitMessagesProofOutcome::::expect_last_delivered_nonce( + lane_id, + 1, + ), + helpers::VerifyRelayerRewarded::::expect_relayer_reward( relayer_id_at_this_chain, RewardsAccountParams::new( lane_id, @@ -197,17 +242,8 @@ pub fn relayed_incoming_message_works< /// Test-case makes sure that Runtime can dispatch XCM messages submitted by relayer, /// with proofs (finality, para heads, message) batched together in signed extrinsic. /// Also verifies relayer transaction signed extensions work as intended. -pub fn complex_relay_extrinsic_works< - Runtime, - AllPalletsWithoutSystem, - XcmConfig, - HrmpChannelOpener, - GPI, - PPI, - MPI, - MB, ->( - collator_session_key: CollatorSessionKeys, +pub fn complex_relay_extrinsic_works( + collator_session_key: CollatorSessionKeys, runtime_para_id: u32, bridged_para_id: u32, sibling_parachain_id: u32, @@ -217,48 +253,33 @@ pub fn complex_relay_extrinsic_works< prepare_configuration: impl Fn(), construct_and_apply_extrinsic: fn( sp_keyring::AccountKeyring, - ::RuntimeCall, + ::RuntimeCall, ) -> sp_runtime::DispatchOutcome, ) where - Runtime: BasicParachainRuntime - + cumulus_pallet_xcmp_queue::Config - + cumulus_pallet_parachain_system::Config - + pallet_bridge_grandpa::Config - + pallet_bridge_parachains::Config - + pallet_bridge_messages::Config - + pallet_bridge_relayers::Config - + pallet_utility::Config, - AllPalletsWithoutSystem: - OnInitialize> + OnFinalize>, - GPI: 'static, - PPI: 'static, - MPI: 'static, - MB: MessageBridge, - ::BridgedChain: Send + Sync + 'static, - ::ThisChain: Send + Sync + 'static, - UnderlyingChainOf>: bp_runtime::Chain + Parachain, - HrmpChannelOpener: frame_support::inherent::ProvideInherent< - Call = cumulus_pallet_parachain_system::Call, - >, - ValidatorIdOf: From>, - >::SourceHeaderChain: - SourceHeaderChain>, - >::BridgedChain: - bp_runtime::Chain, - ParaHash: From< - <>::BridgedChain as bp_runtime::Chain>::Hash, - >, - ::AccountId: - Into<<::RuntimeOrigin as OriginTrait>::AccountId>, - ::AccountId: From, - AccountIdOf: From, - >::InboundRelayer: From, - ::RuntimeCall: From> - + From> - + From>, - ::RuntimeCall: From>, + RuntimeHelper: WithRemoteParachainHelper, + RuntimeHelper::Runtime: + pallet_utility::Config>, + AccountIdOf: From, + RuntimeCallOf: From> + + From> + + From> + + From>, + UnderlyingChainOf>: + bp_runtime::Chain + Parachain, + >::BridgedChain: + bp_runtime::Chain + ChainWithGrandpa, + >::SourceHeaderChain: + SourceHeaderChain< + MessagesProof = FromBridgedChainMessagesProof< + HashOf>, + >, + >, { - helpers::relayed_incoming_message_works::( + helpers::relayed_incoming_message_works::< + RuntimeHelper::Runtime, + RuntimeHelper::AllPalletsWithoutSystem, + RuntimeHelper::MPI, + >( collator_session_key, runtime_para_id, sibling_parachain_id, @@ -275,8 +296,8 @@ pub fn complex_relay_extrinsic_works< prepare_configuration(); // start with bridged relay chain block#0 - helpers::initialize_bridge_grandpa_pallet::( - test_data::initialization_data::(0), + helpers::initialize_bridge_grandpa_pallet::( + test_data::initialization_data::(0), ); // generate bridged relay chain finality, parachain heads and message proofs, @@ -289,8 +310,8 @@ pub fn complex_relay_extrinsic_works< para_heads_proof, message_proof, ) = test_data::from_parachain::make_complex_relayer_delivery_proofs::< - >::BridgedChain, - MB, + >::BridgedChain, + RuntimeHelper::MB, (), >( lane_id, @@ -306,30 +327,40 @@ pub fn complex_relay_extrinsic_works< let relay_chain_header_hash = relay_chain_header.hash(); let relay_chain_header_number = *relay_chain_header.number(); vec![( - pallet_utility::Call::::batch_all { + pallet_utility::Call::::batch_all { calls: vec![ - pallet_bridge_grandpa::Call::::submit_finality_proof { + BridgeGrandpaCall::::submit_finality_proof { finality_target: Box::new(relay_chain_header), justification: grandpa_justification, }.into(), - pallet_bridge_parachains::Call::::submit_parachain_heads { + BridgeParachainsCall::::submit_parachain_heads { at_relay_block: (relay_chain_header_number, relay_chain_header_hash), parachains: parachain_heads, parachain_heads_proof: para_heads_proof, }.into(), - pallet_bridge_messages::Call::::receive_messages_proof { + BridgeMessagesCall::::receive_messages_proof { relayer_id_at_bridged_chain, proof: message_proof, messages_count: 1, dispatch_weight: Weight::from_parts(1000000000, 0), }.into(), ], - }.into(), + } + .into(), Box::new(( - helpers::VerifySubmitGrandpaFinalityProofOutcome::::expect_best_header_hash(relay_chain_header_hash), - helpers::VerifySubmitParachainHeaderProofOutcome::::expect_best_header_hash(bridged_para_id, parachain_head_hash), - helpers::VerifySubmitMessagesProofOutcome::::expect_last_delivered_nonce(lane_id, 1), - helpers::VerifyRelayerRewarded::::expect_relayer_reward( + helpers::VerifySubmitGrandpaFinalityProofOutcome::< + RuntimeHelper::Runtime, + RuntimeHelper::GPI, + >::expect_best_header_hash(relay_chain_header_hash), + helpers::VerifySubmitParachainHeaderProofOutcome::< + RuntimeHelper::Runtime, + RuntimeHelper::PPI, + >::expect_best_header_hash(bridged_para_id, parachain_head_hash), + helpers::VerifySubmitMessagesProofOutcome::< + RuntimeHelper::Runtime, + RuntimeHelper::MPI, + >::expect_last_delivered_nonce(lane_id, 1), + helpers::VerifyRelayerRewarded::::expect_relayer_reward( relayer_id_at_this_chain, RewardsAccountParams::new( lane_id, @@ -345,45 +376,29 @@ pub fn complex_relay_extrinsic_works< /// Estimates transaction fee for default message delivery transaction (batched with required /// proofs) from bridged parachain. -pub fn can_calculate_fee_for_complex_message_delivery_transaction( - collator_session_key: CollatorSessionKeys, - compute_extrinsic_fee: fn(pallet_utility::Call::) -> u128, +pub fn can_calculate_fee_for_complex_message_delivery_transaction( + collator_session_key: CollatorSessionKeys, + compute_extrinsic_fee: fn(pallet_utility::Call) -> u128, ) -> u128 where - Runtime: frame_system::Config - + pallet_balances::Config - + pallet_session::Config - + pallet_xcm::Config - + parachain_info::Config - + pallet_collator_selection::Config - + cumulus_pallet_parachain_system::Config - + pallet_bridge_grandpa::Config - + pallet_bridge_parachains::Config - + pallet_bridge_messages::Config - + pallet_utility::Config, - GPI: 'static, - PPI: 'static, - MPI: 'static, - MB: MessageBridge, - ::BridgedChain: Send + Sync + 'static, - ::ThisChain: Send + Sync + 'static, - UnderlyingChainOf>: bp_runtime::Chain + Parachain, - ValidatorIdOf: From>, - <>::SourceHeaderChain as SourceHeaderChain>::MessagesProof: - From>, - >::BridgedChain: bp_runtime::Chain, - ParaHash: From<<>::BridgedChain as bp_runtime::Chain>::Hash>, - ::AccountId: - Into<<::RuntimeOrigin as OriginTrait>::AccountId>, - ::AccountId: From, - AccountIdOf: From, - >::InboundRelayer: From, - ::RuntimeCall: - From> - + From> - + From>, + RuntimeHelper: WithRemoteParachainHelper, + RuntimeHelper::Runtime: + pallet_utility::Config>, + RuntimeCallOf: From> + + From> + + From>, + UnderlyingChainOf>: + bp_runtime::Chain + Parachain, + >::BridgedChain: + bp_runtime::Chain + ChainWithGrandpa, + >::SourceHeaderChain: + SourceHeaderChain< + MessagesProof = FromBridgedChainMessagesProof< + HashOf>, + >, + >, { - run_test::(collator_session_key, 1000, vec![], || { + run_test::(collator_session_key, 1000, vec![], || { // generate bridged relay chain finality, parachain heads and message proofs, // to be submitted by relayer to this chain. // @@ -398,8 +413,8 @@ where para_heads_proof, message_proof, ) = test_data::from_parachain::make_complex_relayer_delivery_proofs::< - >::BridgedChain, - MB, + >::BridgedChain, + RuntimeHelper::MB, (), >( LaneId::default(), @@ -414,17 +429,18 @@ where // generate batch call that provides finality for bridged relay and parachains + message // proof let batch = test_data::from_parachain::make_complex_relayer_delivery_batch::< - Runtime, - GPI, - PPI, - MPI, + RuntimeHelper::Runtime, + RuntimeHelper::GPI, + RuntimeHelper::PPI, + RuntimeHelper::MPI, + _, >( relay_chain_header, grandpa_justification, parachain_heads, para_heads_proof, message_proof, - Dave.public().into(), + helpers::relayer_id_at_bridged_chain::(), ); let estimated_fee = compute_extrinsic_fee(batch); @@ -432,7 +448,7 @@ where target: "bridges::estimate", "Estimate fee: {:?} for single message delivery for runtime: {:?}", estimated_fee, - Runtime::Version::get(), + ::Version::get(), ); estimated_fee @@ -441,50 +457,34 @@ where /// Estimates transaction fee for default message confirmation transaction (batched with required /// proofs) from bridged parachain. -pub fn can_calculate_fee_for_complex_message_confirmation_transaction( - collator_session_key: CollatorSessionKeys, - compute_extrinsic_fee: fn(pallet_utility::Call::) -> u128, +pub fn can_calculate_fee_for_complex_message_confirmation_transaction( + collator_session_key: CollatorSessionKeys, + compute_extrinsic_fee: fn(pallet_utility::Call) -> u128, ) -> u128 where - Runtime: frame_system::Config - + pallet_balances::Config - + pallet_session::Config - + pallet_xcm::Config - + parachain_info::Config - + pallet_collator_selection::Config - + cumulus_pallet_parachain_system::Config - + pallet_bridge_grandpa::Config - + pallet_bridge_parachains::Config - + pallet_bridge_messages::Config - + pallet_utility::Config, - GPI: 'static, - PPI: 'static, - MPI: 'static, - MB: MessageBridge, - ::BridgedChain: Send + Sync + 'static, - ::ThisChain: Send + Sync + 'static, - <::ThisChain as bp_runtime::Chain>::AccountId: From, - UnderlyingChainOf>: bp_runtime::Chain + Parachain, - ValidatorIdOf: From>, - <>::SourceHeaderChain as SourceHeaderChain>::MessagesProof: - From>, - >::BridgedChain: bp_runtime::Chain, - ParaHash: From<<>::BridgedChain as bp_runtime::Chain>::Hash>, - ::AccountId: - Into<<::RuntimeOrigin as OriginTrait>::AccountId>, - ::AccountId: From, - AccountIdOf: From, - >::InboundRelayer: From, - <>::TargetHeaderChain as TargetHeaderChain< + RuntimeHelper: WithRemoteParachainHelper, + AccountIdOf: From, + RuntimeHelper::Runtime: + pallet_utility::Config>, + MessageThisChain: + bp_runtime::Chain>, + RuntimeCallOf: From> + + From> + + From>, + UnderlyingChainOf>: + bp_runtime::Chain + Parachain, + >::BridgedChain: + bp_runtime::Chain + ChainWithGrandpa, + >::TargetHeaderChain: + TargetHeaderChain< XcmAsPlainPayload, - Runtime::AccountId, - >>::MessagesDeliveryProof: From>, - ::RuntimeCall: - From> - + From> - + From>, + AccountIdOf, + MessagesDeliveryProof = FromBridgedChainMessagesDeliveryProof< + HashOf>>, + >, + >, { - run_test::(collator_session_key, 1000, vec![], || { + run_test::(collator_session_key, 1000, vec![], || { // generate bridged relay chain finality, parachain heads and message proofs, // to be submitted by relayer to this chain. let unrewarded_relayers = UnrewardedRelayersState { @@ -500,18 +500,25 @@ where para_heads_proof, message_delivery_proof, ) = test_data::from_parachain::make_complex_relayer_confirmation_proofs::< - >::BridgedChain, - MB, + >::BridgedChain, + RuntimeHelper::MB, (), - >(LaneId::default(), 1, 5, 1_000, Alice.public().into(), unrewarded_relayers.clone()); + >( + LaneId::default(), + 1, + 5, + 1_000, + AccountId32::from(Alice.public()).into(), + unrewarded_relayers.clone(), + ); // generate batch call that provides finality for bridged relay and parachains + message // proof let batch = test_data::from_parachain::make_complex_relayer_confirmation_batch::< - Runtime, - GPI, - PPI, - MPI, + RuntimeHelper::Runtime, + RuntimeHelper::GPI, + RuntimeHelper::PPI, + RuntimeHelper::MPI, >( relay_chain_header, grandpa_justification, @@ -526,7 +533,7 @@ where target: "bridges::estimate", "Estimate fee: {:?} for single message confirmation for runtime: {:?}", estimated_fee, - Runtime::Version::get(), + ::Version::get(), ); estimated_fee diff --git a/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/helpers.rs b/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/helpers.rs index 4f824129bf71..69aa61db3cc6 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/helpers.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/helpers.rs @@ -16,7 +16,7 @@ //! Module contains tests code, that is shared by all types of bridges -use crate::test_cases::{run_test, RuntimeHelper}; +use crate::test_cases::{bridges_prelude::*, run_test, RuntimeHelper}; use asset_test_utils::BasicParachainRuntime; use bp_messages::{LaneId, MessageNonce}; @@ -30,13 +30,16 @@ use frame_support::{ use frame_system::pallet_prelude::BlockNumberFor; use pallet_bridge_grandpa::{BridgedBlockHash, BridgedHeader}; use parachains_common::AccountId; -use parachains_runtimes_test_utils::{mock_open_hrmp_channel, AccountIdOf, CollatorSessionKeys}; +use parachains_runtimes_test_utils::{ + mock_open_hrmp_channel, AccountIdOf, CollatorSessionKeys, RuntimeCallOf, +}; use sp_core::Get; use sp_keyring::AccountKeyring::*; use sp_runtime::{traits::TrailingZeroInput, AccountId32}; use sp_std::marker::PhantomData; use xcm::latest::prelude::*; +/// Verify that the transaction has succeeded. #[impl_trait_for_tuples::impl_for_tuples(30)] pub trait VerifyTransactionOutcome { fn verify_outcome(&self); @@ -51,7 +54,7 @@ impl VerifyTransactionOutcome for Box { /// Checks that the best finalized header hash in the bridge GRANDPA pallet equals to given one. pub struct VerifySubmitGrandpaFinalityProofOutcome where - Runtime: pallet_bridge_grandpa::Config, + Runtime: BridgeGrandpaConfig, GPI: 'static, { expected_best_hash: BridgedBlockHash, @@ -59,7 +62,7 @@ where impl VerifySubmitGrandpaFinalityProofOutcome where - Runtime: pallet_bridge_grandpa::Config, + Runtime: BridgeGrandpaConfig, GPI: 'static, { /// Expect given header hash to be the best after transaction. @@ -73,7 +76,7 @@ where impl VerifyTransactionOutcome for VerifySubmitGrandpaFinalityProofOutcome where - Runtime: pallet_bridge_grandpa::Config, + Runtime: BridgeGrandpaConfig, GPI: 'static, { fn verify_outcome(&self) { @@ -96,7 +99,7 @@ pub struct VerifySubmitParachainHeaderProofOutcome { impl VerifySubmitParachainHeaderProofOutcome where - Runtime: pallet_bridge_parachains::Config, + Runtime: BridgeParachainsConfig, PPI: 'static, { /// Expect given header hash to be the best after transaction. @@ -111,7 +114,7 @@ where impl VerifyTransactionOutcome for VerifySubmitParachainHeaderProofOutcome where - Runtime: pallet_bridge_parachains::Config, + Runtime: BridgeParachainsConfig, PPI: 'static, { fn verify_outcome(&self) { @@ -132,7 +135,7 @@ pub struct VerifySubmitMessagesProofOutcome { impl VerifySubmitMessagesProofOutcome where - Runtime: pallet_bridge_messages::Config, + Runtime: BridgeMessagesConfig, MPI: 'static, { /// Expect given delivered nonce to be the latest after transaction. @@ -146,7 +149,7 @@ where impl VerifyTransactionOutcome for VerifySubmitMessagesProofOutcome where - Runtime: pallet_bridge_messages::Config, + Runtime: BridgeMessagesConfig, MPI: 'static, { fn verify_outcome(&self) { @@ -194,7 +197,7 @@ where pub(crate) fn initialize_bridge_grandpa_pallet( init_data: bp_header_chain::InitializationData>, ) where - Runtime: pallet_bridge_grandpa::Config, + Runtime: BridgeGrandpaConfig, { pallet_bridge_grandpa::Pallet::::initialize( RuntimeHelper::::root_origin(), @@ -205,7 +208,7 @@ pub(crate) fn initialize_bridge_grandpa_pallet( /// Runtime calls and their verifiers. pub type CallsAndVerifiers = - Vec<(::RuntimeCall, Box)>; + Vec<(RuntimeCallOf, Box)>; /// Returns relayer id at the bridged chain. pub fn relayer_id_at_bridged_chain, MPI>( @@ -222,7 +225,7 @@ pub fn relayed_incoming_message_works( local_relay_chain_id: NetworkId, construct_and_apply_extrinsic: fn( sp_keyring::AccountKeyring, - ::RuntimeCall, + RuntimeCallOf, ) -> sp_runtime::DispatchOutcome, prepare_message_proof_import: impl FnOnce( Runtime::AccountId, @@ -232,9 +235,7 @@ pub fn relayed_incoming_message_works( Xcm<()>, ) -> CallsAndVerifiers, ) where - Runtime: BasicParachainRuntime - + cumulus_pallet_xcmp_queue::Config - + pallet_bridge_messages::Config, + Runtime: BasicParachainRuntime + cumulus_pallet_xcmp_queue::Config + BridgeMessagesConfig, AllPalletsWithoutSystem: OnInitialize> + OnFinalize>, MPI: 'static, @@ -251,7 +252,12 @@ pub fn relayed_incoming_message_works( runtime_para_id, vec![( relayer_id_on_target.clone().into(), - Runtime::ExistentialDeposit::get() * 100000u32.into(), + // this value should be enough to cover all transaction costs, but computing the actual + // value here is tricky - there are several transaction payment pallets and we don't + // want to introduce additional bounds and traits here just for that, so let's just + // select some presumably large value + sp_std::cmp::max::(Runtime::ExistentialDeposit::get(), 1u32.into()) * + 100_000_000u32.into(), )], || { let mut alice = [0u8; 32]; @@ -327,7 +333,7 @@ fn execute_and_verify_calls( submitter: sp_keyring::AccountKeyring, construct_and_apply_extrinsic: fn( sp_keyring::AccountKeyring, - ::RuntimeCall, + RuntimeCallOf, ) -> sp_runtime::DispatchOutcome, calls_and_verifiers: CallsAndVerifiers, ) { diff --git a/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/mod.rs b/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/mod.rs index 11210841bd39..64ec87265992 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/mod.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases/mod.rs @@ -24,7 +24,7 @@ pub mod from_parachain; pub(crate) mod helpers; -use crate::test_data; +use crate::{test_cases::bridges_prelude::*, test_data}; use asset_test_utils::BasicParachainRuntime; use bp_messages::{ @@ -38,12 +38,13 @@ use bridge_runtime_common::messages_xcm_extension::{ use codec::Encode; use frame_support::{ assert_ok, + dispatch::GetDispatchInfo, traits::{Get, OnFinalize, OnInitialize, OriginTrait}, }; use frame_system::pallet_prelude::BlockNumberFor; use parachains_common::AccountId; use parachains_runtimes_test_utils::{ - mock_open_hrmp_channel, AccountIdOf, BalanceOf, CollatorSessionKeys, ExtBuilder, ValidatorIdOf, + mock_open_hrmp_channel, AccountIdOf, BalanceOf, CollatorSessionKeys, ExtBuilder, RuntimeCallOf, XcmReceivedFrom, }; use sp_runtime::{traits::Zero, AccountId32}; @@ -54,6 +55,16 @@ use xcm_executor::{ XcmExecutor, }; +/// Common bridges exports. +pub(crate) mod bridges_prelude { + pub use pallet_bridge_grandpa::{Call as BridgeGrandpaCall, Config as BridgeGrandpaConfig}; + pub use pallet_bridge_messages::{Call as BridgeMessagesCall, Config as BridgeMessagesConfig}; + pub use pallet_bridge_parachains::{ + Call as BridgeParachainsCall, Config as BridgeParachainsConfig, RelayBlockHash, + RelayBlockNumber, + }; +} + // Re-export test_case from assets pub use asset_test_utils::include_teleports_for_native_asset_works; @@ -89,11 +100,10 @@ pub fn initialize_bridge_by_governance_works( collator_session_key: CollatorSessionKeys, runtime_para_id: u32, ) where - Runtime: BasicParachainRuntime + pallet_bridge_grandpa::Config, + Runtime: BasicParachainRuntime + BridgeGrandpaConfig, GrandpaPalletInstance: 'static, - ValidatorIdOf: From>, - ::RuntimeCall: - From>, + RuntimeCallOf: + GetDispatchInfo + From>, { run_test::(collator_session_key, runtime_para_id, vec![], || { // check mode before @@ -102,24 +112,18 @@ pub fn initialize_bridge_by_governance_works( Err(()) ); - // encode `initialize` call - let initialize_call = - ::RuntimeCall::from(pallet_bridge_grandpa::Call::< - Runtime, - GrandpaPalletInstance, - >::initialize { - init_data: test_data::initialization_data::(12345), - }) - .encode(); - - // overestimate - check weight for `pallet_bridge_grandpa::Pallet::initialize()` call - let require_weight_at_most = - ::DbWeight::get().reads_writes(7, 7); + // prepare the `initialize` call + let initialize_call = RuntimeCallOf::::from(BridgeGrandpaCall::< + Runtime, + GrandpaPalletInstance, + >::initialize { + init_data: test_data::initialization_data::(12345), + }); // execute XCM with Transacts to `initialize bridge` as governance does assert_ok!(RuntimeHelper::::execute_as_governance( - initialize_call, - require_weight_at_most + initialize_call.encode(), + initialize_call.get_dispatch_info().weight, ) .ensure_complete()); @@ -137,11 +141,10 @@ pub fn change_bridge_grandpa_pallet_mode_by_governance_works, runtime_para_id: u32, ) where - Runtime: BasicParachainRuntime + pallet_bridge_grandpa::Config, + Runtime: BasicParachainRuntime + BridgeGrandpaConfig, GrandpaPalletInstance: 'static, - ValidatorIdOf: From>, - ::RuntimeCall: - From>, + RuntimeCallOf: + GetDispatchInfo + From>, { run_test::(collator_session_key, runtime_para_id, vec![], || { let dispatch_set_operating_mode_call = |old_mode, new_mode| { @@ -151,23 +154,17 @@ pub fn change_bridge_grandpa_pallet_mode_by_governance_works::DbWeight::get().reads_writes(7, 7); - - // encode `set_operating_mode` call + // prepare the `set_operating_mode` call let set_operating_mode_call = ::RuntimeCall::from( pallet_bridge_grandpa::Call::::set_operating_mode { operating_mode: new_mode, }, - ) - .encode(); + ); // execute XCM with Transacts to `initialize bridge` as governance does assert_ok!(RuntimeHelper::::execute_as_governance( - set_operating_mode_call, - require_weight_at_most + set_operating_mode_call.encode(), + set_operating_mode_call.get_dispatch_info().weight, ) .ensure_complete()); @@ -195,11 +192,10 @@ pub fn change_bridge_parachains_pallet_mode_by_governance_works, runtime_para_id: u32, ) where - Runtime: BasicParachainRuntime + pallet_bridge_parachains::Config, + Runtime: BasicParachainRuntime + BridgeParachainsConfig, ParachainsPalletInstance: 'static, - ValidatorIdOf: From>, - ::RuntimeCall: - From>, + RuntimeCallOf: + GetDispatchInfo + From>, { run_test::(collator_session_key, runtime_para_id, vec![], || { let dispatch_set_operating_mode_call = |old_mode, new_mode| { @@ -209,23 +205,19 @@ pub fn change_bridge_parachains_pallet_mode_by_governance_works::DbWeight::get().reads_writes(7, 7); - - // encode `set_operating_mode` call - let set_operating_mode_call = ::RuntimeCall::from(pallet_bridge_parachains::Call::< - Runtime, - ParachainsPalletInstance, - >::set_operating_mode { - operating_mode: new_mode, - }).encode(); + // prepare the `set_operating_mode` call + let set_operating_mode_call = + RuntimeCallOf::::from(pallet_bridge_parachains::Call::< + Runtime, + ParachainsPalletInstance, + >::set_operating_mode { + operating_mode: new_mode, + }); // execute XCM with Transacts to `initialize bridge` as governance does assert_ok!(RuntimeHelper::::execute_as_governance( - set_operating_mode_call, - require_weight_at_most + set_operating_mode_call.encode(), + set_operating_mode_call.get_dispatch_info().weight, ) .ensure_complete()); @@ -253,11 +245,10 @@ pub fn change_bridge_messages_pallet_mode_by_governance_works, runtime_para_id: u32, ) where - Runtime: BasicParachainRuntime + pallet_bridge_messages::Config, + Runtime: BasicParachainRuntime + BridgeMessagesConfig, MessagesPalletInstance: 'static, - ValidatorIdOf: From>, - ::RuntimeCall: - From>, + RuntimeCallOf: + GetDispatchInfo + From>, { run_test::(collator_session_key, runtime_para_id, vec![], || { let dispatch_set_operating_mode_call = |old_mode, new_mode| { @@ -268,23 +259,18 @@ pub fn change_bridge_messages_pallet_mode_by_governance_works::DbWeight::get().reads_writes(7, 7); - // encode `set_operating_mode` call - let set_operating_mode_call = ::RuntimeCall::from(pallet_bridge_messages::Call::< + let set_operating_mode_call = RuntimeCallOf::::from(BridgeMessagesCall::< Runtime, MessagesPalletInstance, >::set_operating_mode { operating_mode: new_mode, - }).encode(); + }); // execute XCM with Transacts to `initialize bridge` as governance does assert_ok!(RuntimeHelper::::execute_as_governance( - set_operating_mode_call, - require_weight_at_most + set_operating_mode_call.encode(), + set_operating_mode_call.get_dispatch_info().weight, ) .ensure_complete()); @@ -337,10 +323,9 @@ pub fn handle_export_message_from_system_parachain_to_outbound_queue_works< maybe_paid_export_message: Option, prepare_configuration: impl Fn(), ) where - Runtime: BasicParachainRuntime + pallet_bridge_messages::Config, + Runtime: BasicParachainRuntime + BridgeMessagesConfig, XcmConfig: xcm_executor::Config, MessagesPalletInstance: 'static, - ValidatorIdOf: From>, { assert_ne!(runtime_para_id, sibling_parachain_id); let sibling_parachain_location = MultiLocation::new(1, Parachain(sibling_parachain_id)); @@ -446,15 +431,13 @@ pub fn message_dispatch_routing_works< ) where Runtime: BasicParachainRuntime + cumulus_pallet_xcmp_queue::Config - + pallet_bridge_messages::Config, + + BridgeMessagesConfig, AllPalletsWithoutSystem: OnInitialize> + OnFinalize>, - ::AccountId: - Into<<::RuntimeOrigin as OriginTrait>::AccountId>, + AccountIdOf: From + + Into<<::RuntimeOrigin as OriginTrait>::AccountId>, XcmConfig: xcm_executor::Config, MessagesPalletInstance: 'static, - ValidatorIdOf: From>, - ::AccountId: From, HrmpChannelOpener: frame_support::inherent::ProvideInherent< Call = cumulus_pallet_parachain_system::Call, >, @@ -488,9 +471,10 @@ pub fn message_dispatch_routing_works< NetworkWithParentCount, AlwaysLatest, >((RuntimeNetwork::get(), Here)); - let result = <>::MessageDispatch>::dispatch( - test_data::dispatch_message(expected_lane_id, 1, bridging_message) - ); + let result = + <>::MessageDispatch>::dispatch( + test_data::dispatch_message(expected_lane_id, 1, bridging_message), + ); assert_eq!( format!("{:?}", result.dispatch_level_result), format!("{:?}", XcmBlobMessageDispatchResult::Dispatched) @@ -515,11 +499,11 @@ pub fn message_dispatch_routing_works< // 2.1. WITHOUT opened hrmp channel -> RoutingError let result = - <>::MessageDispatch>::dispatch( + <>::MessageDispatch>::dispatch( DispatchMessage { key: MessageKey { lane_id: expected_lane_id, nonce: 1 }, data: DispatchMessageData { payload: Ok(bridging_message.clone()) }, - } + }, ); assert_eq!( format!("{:?}", result.dispatch_level_result), @@ -545,12 +529,13 @@ pub fn message_dispatch_routing_works< included_head, &alice, ); - let result = <>::MessageDispatch>::dispatch( - DispatchMessage { - key: MessageKey { lane_id: expected_lane_id, nonce: 1 }, - data: DispatchMessageData { payload: Ok(bridging_message) }, - } - ); + let result = + <>::MessageDispatch>::dispatch( + DispatchMessage { + key: MessageKey { lane_id: expected_lane_id, nonce: 1 }, + data: DispatchMessageData { payload: Ok(bridging_message) }, + }, + ); assert_eq!( format!("{:?}", result.dispatch_level_result), format!("{:?}", XcmBlobMessageDispatchResult::Dispatched) diff --git a/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_data/from_parachain.rs b/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_data/from_parachain.rs index 7ac10aa9e730..932ba2312399 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_data/from_parachain.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_data/from_parachain.rs @@ -22,12 +22,14 @@ use bp_messages::{ source_chain::TargetHeaderChain, target_chain::SourceHeaderChain, LaneId, UnrewardedRelayersState, Weight, }; -use bp_runtime::{BlockNumberOf, HeaderOf, Parachain, StorageProofSize, UnderlyingChainOf}; +use bp_runtime::{ + AccountIdOf, BlockNumberOf, HeaderOf, Parachain, StorageProofSize, UnderlyingChainOf, +}; use bp_test_utils::prepare_parachain_heads_proof; use bridge_runtime_common::{ messages::{ source::FromBridgedChainMessagesDeliveryProof, target::FromBridgedChainMessagesProof, - BridgedChain as MessageBridgedChain, MessageBridge, + BridgedChain as MessageBridgedChain, MessageBridge, ThisChain as MessageThisChain, }, messages_generation::{ encode_all_messages, encode_lane_data, prepare_message_delivery_storage_proof, @@ -38,7 +40,7 @@ use bridge_runtime_common::{ use codec::Encode; use pallet_bridge_grandpa::BridgedHeader; use pallet_bridge_parachains::{RelayBlockHash, RelayBlockNumber}; -use sp_runtime::{traits::Header as HeaderT, AccountId32}; +use sp_runtime::traits::Header as HeaderT; use xcm::latest::prelude::*; use bp_header_chain::{justification::GrandpaJustification, ChainWithGrandpa}; @@ -47,17 +49,21 @@ use bp_polkadot_core::parachains::{ParaHash, ParaHead, ParaHeadsProof, ParaId}; use sp_runtime::SaturatedConversion; /// Prepare a batch call with relay finality proof, parachain head proof and message proof. -pub fn make_complex_relayer_delivery_batch( +pub fn make_complex_relayer_delivery_batch( relay_chain_header: BridgedHeader, grandpa_justification: GrandpaJustification>, parachain_heads: Vec<(ParaId, ParaHash)>, para_heads_proof: ParaHeadsProof, message_proof: FromBridgedChainMessagesProof, - relayer_id_at_bridged_chain: AccountId32, + relayer_id_at_bridged_chain: InboundRelayer, ) -> pallet_utility::Call where Runtime:pallet_bridge_grandpa::Config + pallet_bridge_parachains::Config - + pallet_bridge_messages::Config + + pallet_bridge_messages::Config< + MPI, + InboundPayload = XcmAsPlainPayload, + InboundRelayer = InboundRelayer, + > + pallet_utility::Config, GPI: 'static, PPI: 'static, @@ -66,7 +72,6 @@ pub fn make_complex_relayer_delivery_batch( <>::BridgedChain as bp_runtime::Chain>::Hash: From, <>::SourceHeaderChain as SourceHeaderChain>::MessagesProof: From>, - >::InboundRelayer: From, ::RuntimeCall: From> + From> @@ -117,10 +122,11 @@ where MPI: 'static, >::BridgedChain: bp_runtime::Chain + ChainWithGrandpa, - <>::TargetHeaderChain as TargetHeaderChain< + >::TargetHeaderChain: TargetHeaderChain< XcmAsPlainPayload, Runtime::AccountId, - >>::MessagesDeliveryProof: From>, + MessagesDeliveryProof = FromBridgedChainMessagesDeliveryProof, + >, ::RuntimeCall: From> + From> + From>, @@ -141,7 +147,7 @@ where }; let submit_message_delivery_proof = pallet_bridge_messages::Call::::receive_messages_delivery_proof { - proof: message_delivery_proof.into(), + proof: message_delivery_proof, relayers_state, }; pallet_utility::Call::::batch_all { @@ -174,8 +180,6 @@ where BridgedRelayChain: bp_runtime::Chain + ChainWithGrandpa, MB: MessageBridge, - ::BridgedChain: Send + Sync + 'static, - ::ThisChain: Send + Sync + 'static, UnderlyingChainOf>: bp_runtime::Chain + Parachain, { let message_payload = prepare_inbound_xcm(xcm_message, message_destination); @@ -223,7 +227,7 @@ pub fn make_complex_relayer_confirmation_proofs>, relayers_state: UnrewardedRelayersState, ) -> ( HeaderOf, @@ -237,9 +241,6 @@ where BridgedRelayChain: bp_runtime::Chain + ChainWithGrandpa, MB: MessageBridge, - ::BridgedChain: Send + Sync + 'static, - ::ThisChain: Send + Sync + 'static, - <::ThisChain as bp_runtime::Chain>::AccountId: From, UnderlyingChainOf>: bp_runtime::Chain + Parachain, { // prepare para storage proof containing message delivery proof diff --git a/cumulus/parachains/runtimes/collectives/collectives-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/collectives/collectives-westend/src/xcm_config.rs index 1fde722e42e3..2e64127d6a1d 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-westend/src/xcm_config.rs @@ -166,19 +166,14 @@ impl Contains for SafeCallFilter { frame_system::Call::set_heap_pages { .. } | frame_system::Call::set_code { .. } | frame_system::Call::set_code_without_checks { .. } | + frame_system::Call::authorize_upgrade { .. } | + frame_system::Call::authorize_upgrade_without_checks { .. } | frame_system::Call::kill_prefix { .. }, ) | RuntimeCall::ParachainSystem(..) | RuntimeCall::Timestamp(..) | RuntimeCall::Balances(..) | - RuntimeCall::CollatorSelection( - pallet_collator_selection::Call::set_desired_candidates { .. } | - pallet_collator_selection::Call::set_candidacy_bond { .. } | - pallet_collator_selection::Call::register_as_candidate { .. } | - pallet_collator_selection::Call::leave_intent { .. } | - pallet_collator_selection::Call::set_invulnerables { .. } | - pallet_collator_selection::Call::add_invulnerable { .. } | - pallet_collator_selection::Call::remove_invulnerable { .. }, - ) | RuntimeCall::Session(pallet_session::Call::purge_keys { .. }) | + RuntimeCall::CollatorSelection(..) | + RuntimeCall::Session(pallet_session::Call::purge_keys { .. }) | RuntimeCall::PolkadotXcm( pallet_xcm::Call::force_xcm_version { .. } | pallet_xcm::Call::force_default_xcm_version { .. } diff --git a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/coretime.rs b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/coretime.rs index 3cdcff33cfbd..a85d67f7b4cb 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/coretime.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/coretime.rs @@ -199,23 +199,12 @@ impl CoretimeInterface for CoretimeAllocator { } } - fn check_notify_core_count() -> Option { - let count = CoreCount::get(); - CoreCount::set(&None); - count - } - fn check_notify_revenue_info() -> Option<(RCBlockNumberOf, Self::Balance)> { let revenue = CoretimeRevenue::get(); CoretimeRevenue::set(&None); revenue } - #[cfg(feature = "runtime-benchmarks")] - fn ensure_notify_core_count(count: u16) { - CoreCount::set(&Some(count)); - } - #[cfg(feature = "runtime-benchmarks")] fn ensure_notify_revenue_info(when: RCBlockNumberOf, revenue: Self::Balance) { CoretimeRevenue::set(&Some((when, revenue))); diff --git a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs index ef6eb02b4e2a..2e7889ca0123 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs @@ -121,7 +121,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("coretime-rococo"), impl_name: create_runtime_str!("coretime-rococo"), authoring_version: 1, - spec_version: 1_005_000, + spec_version: 1_005_002, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 0, diff --git a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/weights/pallet_broker.rs b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/weights/pallet_broker.rs index c33cc422d11b..3cc51a247f7b 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/weights/pallet_broker.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/weights/pallet_broker.rs @@ -463,6 +463,9 @@ impl pallet_broker::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().reads(5)) .saturating_add(T::DbWeight::get().writes(2)) } + fn notify_core_count() -> Weight { + T::DbWeight::get().reads_writes(1, 1) + } /// Storage: `Broker::Status` (r:1 w:1) /// Proof: `Broker::Status` (`max_values`: Some(1), `max_size`: Some(18), added: 513, mode: `MaxEncodedLen`) /// Storage: `Broker::Configuration` (r:1 w:0) diff --git a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/xcm_config.rs b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/xcm_config.rs index 15ee924ddb59..00bbe5b5037f 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/xcm_config.rs @@ -28,7 +28,7 @@ use pallet_xcm::XcmPassthrough; use parachains_common::{ impls::ToStakingPot, xcm_config::{ - AllSiblingSystemParachains, ConcreteNativeAssetFrom, ParentRelayOrSiblingParachains, + AllSiblingSystemParachains, ConcreteAssetFromSystem, ParentRelayOrSiblingParachains, RelayOrOtherSystemParachains, }, TREASURY_PALLET_ID, @@ -140,15 +140,22 @@ impl Contains for SafeCallFilter { matches!( call, - RuntimeCall::PolkadotXcm(pallet_xcm::Call::force_xcm_version { .. }) | - RuntimeCall::System( - frame_system::Call::set_heap_pages { .. } | + RuntimeCall::PolkadotXcm( + pallet_xcm::Call::force_xcm_version { .. } | + pallet_xcm::Call::force_default_xcm_version { .. } + ) | RuntimeCall::System( + frame_system::Call::set_heap_pages { .. } | frame_system::Call::set_code { .. } | frame_system::Call::set_code_without_checks { .. } | - frame_system::Call::kill_prefix { .. }, - ) | RuntimeCall::ParachainSystem(..) | + frame_system::Call::authorize_upgrade { .. } | + frame_system::Call::authorize_upgrade_without_checks { .. } | + frame_system::Call::kill_prefix { .. } | + // Should not be in Polkadot/Kusama. Here in order to speed up testing. + frame_system::Call::set_storage { .. }, + ) | RuntimeCall::ParachainSystem(..) | RuntimeCall::Timestamp(..) | RuntimeCall::Balances(..) | + RuntimeCall::Sudo(..) | RuntimeCall::CollatorSelection(..) | RuntimeCall::Session(pallet_session::Call::purge_keys { .. }) | RuntimeCall::XcmpQueue(..) | @@ -187,8 +194,7 @@ parameter_types! { pub RelayTreasuryLocation: MultiLocation = (Parent, PalletInstance(rococo_runtime_constants::TREASURY_PALLET_ID)).into(); } -/// Locations that will not be charged fees in the executor, -/// either execution or delivery. +/// Locations that will not be charged fees in the executor, neither for execution nor delivery. /// We only waive fees for system functions, which these locations represent. pub type WaivedLocations = ( RelayOrOtherSystemParachains, @@ -205,7 +211,7 @@ impl xcm_executor::Config for XcmConfig { // where allowed (e.g. with the Relay Chain). type IsReserve = (); /// Only allow teleportation of ROC. - type IsTeleporter = ConcreteNativeAssetFrom; + type IsTeleporter = ConcreteAssetFromSystem; type UniversalLocation = UniversalLocation; type Barrier = Barrier; type Weigher = WeightInfoBounds< diff --git a/cumulus/parachains/runtimes/coretime/coretime-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/coretime/coretime-westend/src/xcm_config.rs index 9de9da4b2d1f..59d76d10d902 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-westend/src/xcm_config.rs @@ -28,7 +28,7 @@ use pallet_xcm::XcmPassthrough; use parachains_common::{ impls::ToStakingPot, xcm_config::{ - AllSiblingSystemParachains, ConcreteNativeAssetFrom, ParentRelayOrSiblingParachains, + AllSiblingSystemParachains, ConcreteAssetFromSystem, ParentRelayOrSiblingParachains, RelayOrOtherSystemParachains, }, TREASURY_PALLET_ID, @@ -143,16 +143,21 @@ impl Contains for SafeCallFilter { matches!( call, - RuntimeCall::PolkadotXcm(pallet_xcm::Call::force_xcm_version { .. }) | - RuntimeCall::System( - frame_system::Call::set_heap_pages { .. } | - frame_system::Call::set_code { .. } | - frame_system::Call::set_code_without_checks { .. } | - frame_system::Call::kill_prefix { .. }, - ) | RuntimeCall::ParachainSystem(..) | + RuntimeCall::PolkadotXcm( + pallet_xcm::Call::force_xcm_version { .. } | + pallet_xcm::Call::force_default_xcm_version { .. } + ) | RuntimeCall::System( + frame_system::Call::set_heap_pages { .. } | + frame_system::Call::set_code { .. } | + frame_system::Call::set_code_without_checks { .. } | + frame_system::Call::authorize_upgrade { .. } | + frame_system::Call::authorize_upgrade_without_checks { .. } | + frame_system::Call::kill_prefix { .. }, + ) | RuntimeCall::ParachainSystem(..) | RuntimeCall::Timestamp(..) | RuntimeCall::Balances(..) | RuntimeCall::CollatorSelection(..) | + RuntimeCall::Sudo(..) | RuntimeCall::Session(pallet_session::Call::purge_keys { .. }) | RuntimeCall::XcmpQueue(..) ) @@ -190,8 +195,7 @@ parameter_types! { pub RelayTreasuryLocation: MultiLocation = (Parent, PalletInstance(westend_runtime_constants::TREASURY_PALLET_ID)).into(); } -/// Locations that will not be charged fees in the executor, -/// either execution or delivery. +/// Locations that will not be charged fees in the executor, neither for execution nor delivery. /// We only waive fees for system functions, which these locations represent. pub type WaivedLocations = ( RelayOrOtherSystemParachains, @@ -208,7 +212,7 @@ impl xcm_executor::Config for XcmConfig { // where allowed (e.g. with the Relay Chain). type IsReserve = (); /// Only allow teleportation of WND. - type IsTeleporter = ConcreteNativeAssetFrom; + type IsTeleporter = ConcreteAssetFromSystem; type UniversalLocation = UniversalLocation; type Barrier = Barrier; type Weigher = WeightInfoBounds< diff --git a/cumulus/parachains/runtimes/testing/penpal/Cargo.toml b/cumulus/parachains/runtimes/testing/penpal/Cargo.toml index 4e2d145feb45..a21023a93313 100644 --- a/cumulus/parachains/runtimes/testing/penpal/Cargo.toml +++ b/cumulus/parachains/runtimes/testing/penpal/Cargo.toml @@ -78,10 +78,13 @@ cumulus-primitives-utility = { path = "../../../../primitives/utility", default- pallet-collator-selection = { path = "../../../../pallets/collator-selection", default-features = false } parachain-info = { package = "staging-parachain-info", path = "../../../pallets/parachain-info", default-features = false } parachains-common = { path = "../../../common", default-features = false } +assets-common = { path = "../../assets/common", default-features = false } +snowbridge-rococo-common = { path = "../../../../../bridges/snowbridge/parachain/runtime/rococo-common", default-features = false } [features] default = ["std"] std = [ + "assets-common/std", "codec/std", "cumulus-pallet-aura-ext/std", "cumulus-pallet-dmp-queue/std", @@ -118,6 +121,7 @@ std = [ "polkadot-primitives/std", "polkadot-runtime-common/std", "scale-info/std", + "snowbridge-rococo-common/std", "sp-api/std", "sp-block-builder/std", "sp-consensus-aura/std", @@ -138,6 +142,7 @@ std = [ ] runtime-benchmarks = [ + "assets-common/runtime-benchmarks", "cumulus-pallet-dmp-queue/runtime-benchmarks", "cumulus-pallet-parachain-system/runtime-benchmarks", "cumulus-pallet-session-benchmarking/runtime-benchmarks", @@ -161,6 +166,7 @@ runtime-benchmarks = [ "polkadot-parachain-primitives/runtime-benchmarks", "polkadot-primitives/runtime-benchmarks", "polkadot-runtime-common/runtime-benchmarks", + "snowbridge-rococo-common/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "xcm-executor/runtime-benchmarks", diff --git a/cumulus/parachains/runtimes/testing/penpal/src/lib.rs b/cumulus/parachains/runtimes/testing/penpal/src/lib.rs index 9387c454715f..541bcd05644f 100644 --- a/cumulus/parachains/runtimes/testing/penpal/src/lib.rs +++ b/cumulus/parachains/runtimes/testing/penpal/src/lib.rs @@ -32,6 +32,7 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); mod weights; pub mod xcm_config; +use assets_common::MultiLocationForAssetId; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; use cumulus_primitives_core::{AggregateMessageOrigin, ParaId}; use frame_support::{ @@ -458,6 +459,41 @@ impl pallet_assets::Config for Runtime { type BenchmarkHelper = (); } +parameter_types! { + // we just reuse the same deposits + pub const ForeignAssetsAssetDeposit: Balance = AssetDeposit::get(); + pub const ForeignAssetsAssetAccountDeposit: Balance = AssetAccountDeposit::get(); + pub const ForeignAssetsApprovalDeposit: Balance = ApprovalDeposit::get(); + pub const ForeignAssetsAssetsStringLimit: u32 = AssetsStringLimit::get(); + pub const ForeignAssetsMetadataDepositBase: Balance = MetadataDepositBase::get(); + pub const ForeignAssetsMetadataDepositPerByte: Balance = MetadataDepositPerByte::get(); +} + +/// Another pallet assets instance to store foreign assets from bridgehub. +pub type ForeignAssetsInstance = pallet_assets::Instance2; +impl pallet_assets::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type Balance = Balance; + type AssetId = MultiLocationForAssetId; + type AssetIdParameter = MultiLocationForAssetId; + type Currency = Balances; + type CreateOrigin = AsEnsureOriginWithArg>; + type ForceOrigin = EnsureRoot; + type AssetDeposit = ForeignAssetsAssetDeposit; + type MetadataDepositBase = ForeignAssetsMetadataDepositBase; + type MetadataDepositPerByte = ForeignAssetsMetadataDepositPerByte; + type ApprovalDeposit = ForeignAssetsApprovalDeposit; + type StringLimit = ForeignAssetsAssetsStringLimit; + type Freezer = (); + type Extra = (); + type WeightInfo = pallet_assets::weights::SubstrateWeight; + type CallbackHandle = (); + type AssetAccountDeposit = ForeignAssetsAssetAccountDeposit; + type RemoveItemsLimit = frame_support::traits::ConstU32<1000>; + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkHelper = xcm_config::XcmBenchmarkHelper; +} + parameter_types! { pub const ReservedXcmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_div(4); pub const ReservedDmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_div(4); @@ -626,6 +662,7 @@ construct_runtime!( // The main stage. Assets: pallet_assets::::{Pallet, Call, Storage, Event} = 50, + ForeignAssets: pallet_assets::::{Pallet, Call, Storage, Event} = 51, Sudo: pallet_sudo::{Pallet, Call, Storage, Event, Config} = 255, } diff --git a/cumulus/parachains/runtimes/testing/penpal/src/xcm_config.rs b/cumulus/parachains/runtimes/testing/penpal/src/xcm_config.rs index e4f0afc854b9..ed405aeddb38 100644 --- a/cumulus/parachains/runtimes/testing/penpal/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/testing/penpal/src/xcm_config.rs @@ -24,8 +24,8 @@ //! soon. use super::{ AccountId, AllPalletsWithSystem, AssetId as AssetIdPalletAssets, Assets, Balance, Balances, - ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, - WeightToFee, XcmpQueue, + ForeignAssets, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, + RuntimeOrigin, WeightToFee, XcmpQueue, }; use core::marker::PhantomData; use frame_support::{ @@ -42,18 +42,19 @@ use pallet_assets::Instance1; use pallet_xcm::XcmPassthrough; use polkadot_parachain_primitives::primitives::Sibling; use polkadot_runtime_common::impls::ToAuthor; +use snowbridge_rococo_common::EthereumNetwork; use sp_runtime::traits::Zero; use xcm::latest::prelude::*; #[allow(deprecated)] -use xcm_builder::CurrencyAdapter; use xcm_builder::{ AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, AsPrefixedGeneralIndex, - ConvertedConcreteId, DenyReserveTransferToRelayChain, DenyThenTry, EnsureXcmOrigin, - FixedWeightBounds, FungiblesAdapter, IsConcrete, LocalMint, NativeAsset, ParentAsSuperuser, - ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, - SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, - TrailingSetTopicAsId, UsingComponents, WithComputedOrigin, WithUniqueTopic, + ConvertedConcreteId, CurrencyAdapter, DenyReserveTransferToRelayChain, DenyThenTry, + EnsureXcmOrigin, FixedWeightBounds, FungiblesAdapter, IsConcrete, LocalMint, NativeAsset, + NoChecking, ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, + SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, + SovereignSignedViaLocation, StartsWith, TakeWeightCredit, TrailingSetTopicAsId, + UsingComponents, WithComputedOrigin, WithUniqueTopic, }; use xcm_executor::{traits::JustTry, XcmExecutor}; @@ -125,8 +126,28 @@ pub type FungiblesTransactor = FungiblesAdapter< CheckingAccount, >; +/// `AssetId/Balance` converter for `TrustBackedAssets` +pub type ForeignAssetsConvertedConcreteId = + assets_common::ForeignAssetsConvertedConcreteId, Balance>; + +/// Means for transacting foreign assets from different global consensus. +pub type ForeignFungiblesTransactor = FungiblesAdapter< + // Use this fungibles implementation: + ForeignAssets, + // Use this currency when it is a fungible asset matching the given location or name: + ForeignAssetsConvertedConcreteId, + // Convert an XCM MultiLocation into a local account id: + LocationToAccountId, + // Our chain's account ID type (we can't get away without mentioning it explicitly): + AccountId, + // We dont need to check teleports here. + NoChecking, + // The account to use for tracking teleports. + CheckingAccount, +>; + /// Means for transacting assets on this chain. -pub type AssetTransactors = (CurrencyTransactor, FungiblesTransactor); +pub type AssetTransactors = (CurrencyTransactor, ForeignFungiblesTransactor, FungiblesTransactor); /// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance, /// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can @@ -202,16 +223,22 @@ pub type Barrier = TrailingSetTopicAsId< pub type AccountIdOf = ::AccountId; /// Asset filter that allows all assets from a certain location matching asset id. -pub struct AssetsFrom(PhantomData); -impl> ContainsPair for AssetsFrom { +pub struct AssetPrefixFrom(PhantomData<(Prefix, Origin)>); +impl ContainsPair for AssetPrefixFrom +where + Prefix: Get, + Origin: Get, +{ fn contains(asset: &MultiAsset, origin: &MultiLocation) -> bool { - let loc = T::get(); + let loc = Origin::get(); &loc == origin && matches!(asset, MultiAsset { id: AssetId::Concrete(asset_loc), fun: Fungible(_a) } - if asset_loc.starts_with(&loc)) + if asset_loc.starts_with(&Prefix::get())) } } +type AssetsFrom = AssetPrefixFrom; + /// Asset filter that allows native/relay asset if coming from a certain location. pub struct NativeAssetFrom(PhantomData); impl> ContainsPair for NativeAssetFrom { @@ -267,6 +294,7 @@ parameter_types! { 0, X2(PalletInstance(50), GeneralIndex(TELEPORTABLE_ASSET_ID.into())) ); + pub EthereumLocation: MultiLocation = MultiLocation::new(2, X1(GlobalConsensus(EthereumNetwork::get()))); } /// Accepts asset with ID `AssetLocation` and is coming from `Origin` chain. @@ -280,8 +308,12 @@ impl, Origin: Get> } } -pub type Reserves = - (NativeAsset, AssetsFrom, NativeAssetFrom); +pub type Reserves = ( + NativeAsset, + AssetsFrom, + NativeAssetFrom, + AssetPrefixFrom, +); pub type TrustedTeleporters = (AssetFromChain,); @@ -362,3 +394,12 @@ impl cumulus_pallet_xcm::Config for Runtime { type RuntimeEvent = RuntimeEvent; type XcmExecutor = XcmExecutor; } + +/// Simple conversion of `u32` into an `AssetId` for use in benchmarking. +pub struct XcmBenchmarkHelper; +#[cfg(feature = "runtime-benchmarks")] +impl pallet_assets::BenchmarkHelper for XcmBenchmarkHelper { + fn create_asset_id_parameter(id: u32) -> MultiLocation { + MultiLocation { parents: 1, interior: X1(Parachain(id)) } + } +} diff --git a/cumulus/polkadot-parachain/src/chain_spec/bridge_hubs.rs b/cumulus/polkadot-parachain/src/chain_spec/bridge_hubs.rs index 8dab692c1cd0..1f43edf2243c 100644 --- a/cumulus/polkadot-parachain/src/chain_spec/bridge_hubs.rs +++ b/cumulus/polkadot-parachain/src/chain_spec/bridge_hubs.rs @@ -231,6 +231,10 @@ pub mod rococo { "bridgeWestendMessages": { "owner": bridges_pallet_owner.clone(), }, + "ethereumSystem": { + "paraId": id, + "assetHubParaId": 1000 + } }) } } diff --git a/cumulus/polkadot-parachain/src/command.rs b/cumulus/polkadot-parachain/src/command.rs index c799e05fb29e..516bdb768524 100644 --- a/cumulus/polkadot-parachain/src/command.rs +++ b/cumulus/polkadot-parachain/src/command.rs @@ -273,6 +273,7 @@ fn load_spec(id: &str) -> std::result::Result, String> { /// (H/T to Phala for the idea) /// E.g. "penpal-kusama-2004" yields ("penpal-kusama", Some(2004)) fn extract_parachain_id(id: &str) -> (&str, &str, Option) { + const ROCOCO_TEST_PARA_PREFIX: &str = "penpal-rococo-"; const KUSAMA_TEST_PARA_PREFIX: &str = "penpal-kusama-"; const POLKADOT_TEST_PARA_PREFIX: &str = "penpal-polkadot-"; @@ -284,7 +285,10 @@ fn extract_parachain_id(id: &str) -> (&str, &str, Option) { const GLUTTON_WESTEND_PARA_LOCAL_PREFIX: &str = "glutton-westend-local-"; const GLUTTON_WESTEND_PARA_GENESIS_PREFIX: &str = "glutton-westend-genesis-"; - let (norm_id, orig_id, para) = if let Some(suffix) = id.strip_prefix(KUSAMA_TEST_PARA_PREFIX) { + let (norm_id, orig_id, para) = if let Some(suffix) = id.strip_prefix(ROCOCO_TEST_PARA_PREFIX) { + let para_id: u32 = suffix.parse().expect("Invalid parachain-id suffix"); + (&id[..ROCOCO_TEST_PARA_PREFIX.len() - 1], id, Some(para_id)) + } else if let Some(suffix) = id.strip_prefix(KUSAMA_TEST_PARA_PREFIX) { let para_id: u32 = suffix.parse().expect("Invalid parachain-id suffix"); (&id[..KUSAMA_TEST_PARA_PREFIX.len() - 1], id, Some(para_id)) } else if let Some(suffix) = id.strip_prefix(POLKADOT_TEST_PARA_PREFIX) { diff --git a/cumulus/xcm/xcm-emulator/src/lib.rs b/cumulus/xcm/xcm-emulator/src/lib.rs index 579949407d41..c9e4de21d43c 100644 --- a/cumulus/xcm/xcm-emulator/src/lib.rs +++ b/cumulus/xcm/xcm-emulator/src/lib.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Polkadot. If not, see . -pub use codec::{Decode, Encode, EncodeLike}; +pub use codec::{Decode, Encode, EncodeLike, MaxEncodedLen}; pub use lazy_static::lazy_static; pub use log; pub use paste; @@ -249,7 +249,7 @@ pub trait Parachain: Chain { type LocationToAccountId: ConvertLocation>; type ParachainInfo: Get; type ParachainSystem; - type MessageProcessor: ProcessMessage + ServiceQueues; + type MessageProcessor: ProcessMessage + ServiceQueues; fn init(); @@ -580,7 +580,7 @@ macro_rules! decl_test_parachains { XcmpMessageHandler: $xcmp_message_handler:path, LocationToAccountId: $location_to_account:path, ParachainInfo: $parachain_info:path, - // MessageProcessor: $message_processor:path, + MessageOrigin: $message_origin:path, }, pallets = { $($pallet_name:ident: $pallet_path:path,)* @@ -619,7 +619,7 @@ macro_rules! decl_test_parachains { type LocationToAccountId = $location_to_account; type ParachainSystem = $crate::ParachainSystemPallet<::Runtime>; type ParachainInfo = $parachain_info; - type MessageProcessor = $crate::DefaultParaMessageProcessor<$name>; + type MessageProcessor = $crate::DefaultParaMessageProcessor<$name, $message_origin>; // We run an empty block during initialisation to open HRMP channels // and have them ready for the next block @@ -1011,7 +1011,7 @@ macro_rules! decl_test_networks { <$parachain>::ext_wrapper(|| { let _ = <$parachain as Parachain>::MessageProcessor::process_message( &msg[..], - $crate::CumulusAggregateMessageOrigin::Parent, + $crate::CumulusAggregateMessageOrigin::Parent.into(), &mut weight_meter, &mut msg.using_encoded($crate::blake2_256), ); @@ -1317,17 +1317,23 @@ macro_rules! decl_test_sender_receiver_accounts_parameter_types { }; } -pub struct DefaultParaMessageProcessor(PhantomData); +pub struct DefaultParaMessageProcessor(PhantomData<(T, M)>); // Process HRMP messages from sibling paraids -impl ProcessMessage for DefaultParaMessageProcessor +impl ProcessMessage for DefaultParaMessageProcessor where + M: codec::FullCodec + + MaxEncodedLen + + Clone + + Eq + + PartialEq + + frame_support::pallet_prelude::TypeInfo + + Debug, T: Parachain, T::Runtime: MessageQueueConfig, - <::MessageProcessor as ProcessMessage>::Origin: - PartialEq, - MessageQueuePallet: EnqueueMessage + ServiceQueues, + <::MessageProcessor as ProcessMessage>::Origin: PartialEq, + MessageQueuePallet: EnqueueMessage + ServiceQueues, { - type Origin = CumulusAggregateMessageOrigin; + type Origin = M; fn process_message( msg: &[u8], @@ -1344,13 +1350,13 @@ where Ok(true) } } -impl ServiceQueues for DefaultParaMessageProcessor +impl ServiceQueues for DefaultParaMessageProcessor where + M: MaxEncodedLen, T: Parachain, T::Runtime: MessageQueueConfig, - <::MessageProcessor as ProcessMessage>::Origin: - PartialEq, - MessageQueuePallet: EnqueueMessage + ServiceQueues, + <::MessageProcessor as ProcessMessage>::Origin: PartialEq, + MessageQueuePallet: EnqueueMessage + ServiceQueues, { type OverweightMessageAddress = (); diff --git a/polkadot/node/core/prospective-parachains/src/tests.rs b/polkadot/node/core/prospective-parachains/src/tests.rs index 51a5ef622c04..7e369245c0e1 100644 --- a/polkadot/node/core/prospective-parachains/src/tests.rs +++ b/polkadot/node/core/prospective-parachains/src/tests.rs @@ -101,9 +101,8 @@ fn test_harness>( let mut view = View::new(); let subsystem = async move { - match run_iteration(&mut context, &mut view, &Metrics(None)).await { - Ok(()) => {}, - Err(e) => panic!("{:?}", e), + if let Err(e) = run_iteration(&mut context, &mut view, &Metrics(None)).await { + panic!("{:?}", e); } view diff --git a/polkadot/roadmap/implementers-guide/src/runtime/scheduler.md b/polkadot/roadmap/implementers-guide/src/runtime/scheduler.md index 26058c446cb9..32a7fe652dbc 100644 --- a/polkadot/roadmap/implementers-guide/src/runtime/scheduler.md +++ b/polkadot/roadmap/implementers-guide/src/runtime/scheduler.md @@ -182,6 +182,7 @@ struct CoreAssignment { core: CoreIndex, para_id: ParaId, kind: AssignmentKind, + group_idx: GroupIndex, } // reasons a core might be freed. enum FreedReason { diff --git a/polkadot/runtime/common/Cargo.toml b/polkadot/runtime/common/Cargo.toml index 1af29be0ae8f..c841c0847c0f 100644 --- a/polkadot/runtime/common/Cargo.toml +++ b/polkadot/runtime/common/Cargo.toml @@ -32,6 +32,7 @@ sp-npos-elections = { path = "../../../substrate/primitives/npos-elections", def pallet-authorship = { path = "../../../substrate/frame/authorship", default-features = false } pallet-balances = { path = "../../../substrate/frame/balances", default-features = false } +pallet-broker = { path = "../../../substrate/frame/broker", default-features = false } pallet-fast-unstake = { path = "../../../substrate/frame/fast-unstake", default-features = false } pallet-identity = { path = "../../../substrate/frame/identity", default-features = false } pallet-session = { path = "../../../substrate/frame/session", default-features = false } @@ -87,6 +88,7 @@ std = [ "pallet-asset-rate?/std", "pallet-authorship/std", "pallet-balances/std", + "pallet-broker/std", "pallet-election-provider-multi-phase/std", "pallet-fast-unstake/std", "pallet-identity/std", @@ -127,6 +129,7 @@ runtime-benchmarks = [ "pallet-asset-rate/runtime-benchmarks", "pallet-babe/runtime-benchmarks", "pallet-balances/runtime-benchmarks", + "pallet-broker/runtime-benchmarks", "pallet-election-provider-multi-phase/runtime-benchmarks", "pallet-fast-unstake/runtime-benchmarks", "pallet-identity/runtime-benchmarks", @@ -151,6 +154,7 @@ try-runtime = [ "pallet-authorship/try-runtime", "pallet-babe?/try-runtime", "pallet-balances/try-runtime", + "pallet-broker/try-runtime", "pallet-election-provider-multi-phase/try-runtime", "pallet-fast-unstake/try-runtime", "pallet-identity/try-runtime", diff --git a/polkadot/runtime/common/src/assigned_slots/mod.rs b/polkadot/runtime/common/src/assigned_slots/mod.rs index f5e3aaef6324..cb56cb8a118c 100644 --- a/polkadot/runtime/common/src/assigned_slots/mod.rs +++ b/polkadot/runtime/common/src/assigned_slots/mod.rs @@ -743,6 +743,7 @@ mod tests { type QueueFootprinter = (); type NextSessionRotation = crate::mock::TestNextSessionRotation; type OnNewHead = (); + type AssignCoretime = (); } impl parachains_shared::Config for Test {} diff --git a/polkadot/runtime/common/src/integration_tests.rs b/polkadot/runtime/common/src/integration_tests.rs index b0d277a702d6..4870432d22f9 100644 --- a/polkadot/runtime/common/src/integration_tests.rs +++ b/polkadot/runtime/common/src/integration_tests.rs @@ -212,6 +212,7 @@ impl paras::Config for Test { type QueueFootprinter = (); type NextSessionRotation = crate::mock::TestNextSessionRotation; type OnNewHead = (); + type AssignCoretime = (); } parameter_types! { diff --git a/polkadot/runtime/common/src/paras_registrar/mod.rs b/polkadot/runtime/common/src/paras_registrar/mod.rs index 12376ae6f1ff..9719f02677dc 100644 --- a/polkadot/runtime/common/src/paras_registrar/mod.rs +++ b/polkadot/runtime/common/src/paras_registrar/mod.rs @@ -814,6 +814,7 @@ mod tests { type QueueFootprinter = (); type NextSessionRotation = crate::mock::TestNextSessionRotation; type OnNewHead = (); + type AssignCoretime = (); } impl configuration::Config for Test { diff --git a/polkadot/runtime/common/src/paras_sudo_wrapper.rs b/polkadot/runtime/common/src/paras_sudo_wrapper.rs index 0fc2644b2a0b..4735c1763291 100644 --- a/polkadot/runtime/common/src/paras_sudo_wrapper.rs +++ b/polkadot/runtime/common/src/paras_sudo_wrapper.rs @@ -23,7 +23,7 @@ use parity_scale_codec::Encode; use primitives::Id as ParaId; use runtime_parachains::{ configuration, dmp, hrmp, - paras::{self, ParaGenesisArgs}, + paras::{self, AssignCoretime, ParaGenesisArgs}, ParaLifecycle, }; use sp_std::boxed::Box; @@ -58,6 +58,8 @@ pub mod pallet { CannotUpgrade, /// Cannot downgrade lease holding parachain to on-demand. CannotDowngrade, + /// There are more cores than supported by the runtime. + TooManyCores, } #[pallet::hooks] @@ -66,6 +68,10 @@ pub mod pallet { #[pallet::call] impl Pallet { /// Schedule a para to be initialized at the start of the next session. + /// + /// This should only be used for TESTING and not on PRODUCTION chains. It automatically + /// assigns Coretime to the chain and increases the number of cores. Thus, there is no + /// running coretime chain required. #[pallet::call_index(0)] #[pallet::weight((1_000, DispatchClass::Operational))] pub fn sudo_schedule_para_initialize( @@ -76,6 +82,9 @@ pub mod pallet { ensure_root(origin)?; runtime_parachains::schedule_para_initialize::(id, genesis) .map_err(|_| Error::::ParaAlreadyExists)?; + + T::AssignCoretime::assign_coretime(id)?; + Ok(()) } diff --git a/polkadot/runtime/common/src/slots/mod.rs b/polkadot/runtime/common/src/slots/mod.rs index c3aaf8b51b81..6a8cddd8d914 100644 --- a/polkadot/runtime/common/src/slots/mod.rs +++ b/polkadot/runtime/common/src/slots/mod.rs @@ -326,6 +326,18 @@ impl Pallet { tracker.into_iter().collect() } + + /// Current lease index and how many blocks we are already in. + pub fn lease_period_index_plus_progress( + b: BlockNumberFor, + ) -> Option<(>>::LeasePeriod, BlockNumberFor)> { + // Note that blocks before `LeaseOffset` do not count as any lease period. + let offset_block_now = b.checked_sub(&T::LeaseOffset::get())?; + let lease_period = offset_block_now / T::LeasePeriod::get(); + let in_lease = offset_block_now % T::LeasePeriod::get(); + + Some((lease_period, in_lease)) + } } impl crate::traits::OnSwap for Pallet { @@ -449,12 +461,8 @@ impl Leaser> for Pallet { } fn lease_period_index(b: BlockNumberFor) -> Option<(Self::LeasePeriod, bool)> { - // Note that blocks before `LeaseOffset` do not count as any lease period. - let offset_block_now = b.checked_sub(&T::LeaseOffset::get())?; - let lease_period = offset_block_now / T::LeasePeriod::get(); - let first_block = (offset_block_now % T::LeasePeriod::get()).is_zero(); - - Some((lease_period, first_block)) + Self::lease_period_index_plus_progress(b) + .map(|(period, progress)| (period, progress.is_zero())) } fn already_leased( diff --git a/polkadot/runtime/parachains/Cargo.toml b/polkadot/runtime/parachains/Cargo.toml index 0bcf5c04c344..1f381400cf53 100644 --- a/polkadot/runtime/parachains/Cargo.toml +++ b/polkadot/runtime/parachains/Cargo.toml @@ -31,11 +31,13 @@ sp-core = { path = "../../../substrate/primitives/core", default-features = fals sp-keystore = { path = "../../../substrate/primitives/keystore", optional = true } sp-application-crypto = { path = "../../../substrate/primitives/application-crypto", default-features = false, optional = true } sp-tracing = { path = "../../../substrate/primitives/tracing", default-features = false, optional = true } +sp-arithmetic = { path = "../../../substrate/primitives/arithmetic", default-features = false } pallet-authority-discovery = { path = "../../../substrate/frame/authority-discovery", default-features = false } pallet-authorship = { path = "../../../substrate/frame/authorship", default-features = false } pallet-balances = { path = "../../../substrate/frame/balances", default-features = false } pallet-babe = { path = "../../../substrate/frame/babe", default-features = false } +pallet-broker = { path = "../../../substrate/frame/broker", default-features = false } pallet-message-queue = { path = "../../../substrate/frame/message-queue", default-features = false } pallet-session = { path = "../../../substrate/frame/session", default-features = false } pallet-staking = { path = "../../../substrate/frame/staking", default-features = false } @@ -82,6 +84,7 @@ std = [ "pallet-authorship/std", "pallet-babe/std", "pallet-balances/std", + "pallet-broker/std", "pallet-message-queue/std", "pallet-session/std", "pallet-staking/std", @@ -99,6 +102,7 @@ std = [ "serde/std", "sp-api/std", "sp-application-crypto?/std", + "sp-arithmetic/std", "sp-core/std", "sp-io/std", "sp-keystore", @@ -115,6 +119,7 @@ runtime-benchmarks = [ "frame-system/runtime-benchmarks", "pallet-babe/runtime-benchmarks", "pallet-balances/runtime-benchmarks", + "pallet-broker/runtime-benchmarks", "pallet-message-queue/runtime-benchmarks", "pallet-staking/runtime-benchmarks", "pallet-timestamp/runtime-benchmarks", @@ -135,6 +140,7 @@ try-runtime = [ "pallet-authorship/try-runtime", "pallet-babe/try-runtime", "pallet-balances/try-runtime", + "pallet-broker/try-runtime", "pallet-message-queue/try-runtime", "pallet-session/try-runtime", "pallet-staking/try-runtime", diff --git a/polkadot/runtime/parachains/src/assigner.rs b/polkadot/runtime/parachains/src/assigner.rs deleted file mode 100644 index 9e408df61dc1..000000000000 --- a/polkadot/runtime/parachains/src/assigner.rs +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright (C) Parity Technologies (UK) Ltd. -// This file is part of Polkadot. - -// Polkadot is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Polkadot is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Polkadot. If not, see . - -//! The Polkadot multiplexing assignment provider. -//! Provides blockspace assignments for both bulk and on demand parachains. -use frame_system::pallet_prelude::BlockNumberFor; -use primitives::{CoreIndex, Id as ParaId}; - -use crate::{ - configuration, paras, - scheduler::common::{Assignment, AssignmentProvider, AssignmentProviderConfig}, -}; - -pub use pallet::*; - -#[frame_support::pallet] -pub mod pallet { - use super::*; - - #[pallet::pallet] - #[pallet::without_storage_info] - pub struct Pallet(_); - - #[pallet::config] - pub trait Config: frame_system::Config + configuration::Config + paras::Config { - type ParachainsAssignmentProvider: AssignmentProvider>; - type OnDemandAssignmentProvider: AssignmentProvider>; - } -} - -// Aliases to make the impl more readable. -type ParachainAssigner = ::ParachainsAssignmentProvider; -type OnDemandAssigner = ::OnDemandAssignmentProvider; - -impl Pallet { - // Helper fn for the AssignmentProvider implementation. - // Assumes that the first allocation of cores is to bulk parachains. - // This function will return false if there are no cores assigned to the bulk parachain - // assigner. - fn is_bulk_core(core_idx: &CoreIndex) -> bool { - let parachain_cores = - as AssignmentProvider>>::session_core_count(); - - core_idx.0 < parachain_cores - } -} - -impl AssignmentProvider> for Pallet { - fn session_core_count() -> u32 { - let parachain_cores = - as AssignmentProvider>>::session_core_count(); - let on_demand_cores = - as AssignmentProvider>>::session_core_count(); - - parachain_cores.saturating_add(on_demand_cores) - } - - /// Pops an `Assignment` from a specified `CoreIndex` - fn pop_assignment_for_core( - core_idx: CoreIndex, - concluded_para: Option, - ) -> Option { - if Pallet::::is_bulk_core(&core_idx) { - as AssignmentProvider>>::pop_assignment_for_core( - core_idx, - concluded_para, - ) - } else { - as AssignmentProvider>>::pop_assignment_for_core( - core_idx, - concluded_para, - ) - } - } - - fn push_assignment_for_core(core_idx: CoreIndex, assignment: Assignment) { - if Pallet::::is_bulk_core(&core_idx) { - as AssignmentProvider>>::push_assignment_for_core( - core_idx, assignment, - ) - } else { - as AssignmentProvider>>::push_assignment_for_core( - core_idx, assignment, - ) - } - } - - fn get_provider_config(core_idx: CoreIndex) -> AssignmentProviderConfig> { - if Pallet::::is_bulk_core(&core_idx) { - as AssignmentProvider>>::get_provider_config( - core_idx, - ) - } else { - as AssignmentProvider>>::get_provider_config( - core_idx, - ) - } - } -} diff --git a/polkadot/runtime/parachains/src/assigner_coretime/mock_helpers.rs b/polkadot/runtime/parachains/src/assigner_coretime/mock_helpers.rs new file mode 100644 index 000000000000..71c3f1fa39f7 --- /dev/null +++ b/polkadot/runtime/parachains/src/assigner_coretime/mock_helpers.rs @@ -0,0 +1,87 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Helper functions for tests, also used in runtime-benchmarks. + +#![cfg(test)] + +use super::*; + +use crate::{ + mock::MockGenesisConfig, + paras::{ParaGenesisArgs, ParaKind}, +}; +use sp_runtime::Perbill; + +use primitives::{Balance, HeadData, ValidationCode}; + +fn default_genesis_config() -> MockGenesisConfig { + MockGenesisConfig { + configuration: crate::configuration::GenesisConfig { + config: crate::configuration::HostConfiguration { ..Default::default() }, + }, + ..Default::default() + } +} + +#[derive(Debug)] +pub struct GenesisConfigBuilder { + pub on_demand_cores: u32, + pub on_demand_base_fee: Balance, + pub on_demand_fee_variability: Perbill, + pub on_demand_max_queue_size: u32, + pub on_demand_target_queue_utilization: Perbill, + pub onboarded_on_demand_chains: Vec, +} + +impl Default for GenesisConfigBuilder { + fn default() -> Self { + Self { + on_demand_cores: 10, + on_demand_base_fee: 10_000, + on_demand_fee_variability: Perbill::from_percent(1), + on_demand_max_queue_size: 100, + on_demand_target_queue_utilization: Perbill::from_percent(25), + onboarded_on_demand_chains: vec![], + } + } +} + +impl GenesisConfigBuilder { + pub(super) fn build(self) -> MockGenesisConfig { + let mut genesis = default_genesis_config(); + let config = &mut genesis.configuration.config; + config.coretime_cores = self.on_demand_cores; + config.on_demand_base_fee = self.on_demand_base_fee; + config.on_demand_fee_variability = self.on_demand_fee_variability; + config.on_demand_queue_max_size = self.on_demand_max_queue_size; + config.on_demand_target_queue_utilization = self.on_demand_target_queue_utilization; + + let paras = &mut genesis.paras.paras; + for para_id in self.onboarded_on_demand_chains { + paras.push(( + para_id, + ParaGenesisArgs { + genesis_head: HeadData::from(vec![0u8]), + validation_code: ValidationCode::from(vec![0u8]), + para_kind: ParaKind::Parathread, + }, + )) + } + + genesis + } +} diff --git a/polkadot/runtime/parachains/src/assigner_coretime/mod.rs b/polkadot/runtime/parachains/src/assigner_coretime/mod.rs new file mode 100644 index 000000000000..9da81dc816ca --- /dev/null +++ b/polkadot/runtime/parachains/src/assigner_coretime/mod.rs @@ -0,0 +1,496 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! The parachain coretime assignment module. +//! +//! Handles scheduling of assignments coming from the coretime/broker chain. For on-demand +//! assignments it relies on the separate on-demand assignment provider, where it forwards requests +//! to. +//! +//! `CoreDescriptor` contains pointers to the begin and the end of a list of schedules, together +//! with the currently active assignments. + +mod mock_helpers; +#[cfg(test)] +mod tests; + +use crate::{ + assigner_on_demand, configuration, + paras::AssignCoretime, + scheduler::common::{Assignment, AssignmentProvider, AssignmentProviderConfig}, + ParaId, +}; + +use frame_support::{defensive, pallet_prelude::*}; +use frame_system::pallet_prelude::*; +use pallet_broker::CoreAssignment; +use primitives::CoreIndex; +use sp_runtime::traits::{One, Saturating}; + +use sp_std::prelude::*; + +pub use pallet::*; + +/// Fraction expressed as a nominator with an assumed denominator of 57,600. +#[derive(RuntimeDebug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, TypeInfo)] +pub struct PartsOf57600(u16); + +impl PartsOf57600 { + pub const ZERO: Self = Self(0); + pub const FULL: Self = Self(57600); + + pub fn new_saturating(v: u16) -> Self { + Self::ZERO.saturating_add(Self(v)) + } + + pub fn is_full(&self) -> bool { + *self == Self::FULL + } + + pub fn saturating_add(self, rhs: Self) -> Self { + let inner = self.0.saturating_add(rhs.0); + if inner > 57600 { + Self(57600) + } else { + Self(inner) + } + } + + pub fn saturating_sub(self, rhs: Self) -> Self { + Self(self.0.saturating_sub(rhs.0)) + } + + pub fn checked_add(self, rhs: Self) -> Option { + let inner = self.0.saturating_add(rhs.0); + if inner > 57600 { + None + } else { + Some(Self(inner)) + } + } +} + +/// Assignments as they are scheduled by block number +/// +/// for a particular core. +#[derive(Encode, Decode, TypeInfo)] +#[cfg_attr(test, derive(PartialEq, RuntimeDebug))] +struct Schedule { + // Original assignments + assignments: Vec<(CoreAssignment, PartsOf57600)>, + /// When do our assignments become invalid, if at all? + /// + /// If this is `Some`, then this `CoreState` will be dropped at that block number. If this is + /// `None`, then we will keep serving our core assignments in a circle until a new set of + /// assignments is scheduled. + end_hint: Option, + + /// The next queued schedule for this core. + /// + /// Schedules are forming a queue. + next_schedule: Option, +} + +/// Descriptor for a core. +/// +/// Contains pointers to first and last schedule into `CoreSchedules` for that core and keeps track +/// of the currently active work as well. +#[derive(Encode, Decode, TypeInfo, Default)] +#[cfg_attr(test, derive(PartialEq, RuntimeDebug, Clone))] +struct CoreDescriptor { + /// Meta data about the queued schedules for this core. + queue: Option>, + /// Currently performed work. + current_work: Option>, +} + +/// Pointers into `CoreSchedules` for a particular core. +/// +/// Schedules in `CoreSchedules` form a queue. `Schedule::next_schedule` always pointing to the next +/// item. +#[derive(Encode, Decode, TypeInfo, Copy, Clone)] +#[cfg_attr(test, derive(PartialEq, RuntimeDebug))] +struct QueueDescriptor { + /// First scheduled item, that is not yet active. + first: N, + /// Last scheduled item. + last: N, +} + +#[derive(Encode, Decode, TypeInfo)] +#[cfg_attr(test, derive(PartialEq, RuntimeDebug, Clone))] +struct WorkState { + /// Assignments with current state. + /// + /// Assignments and book keeping on how much has been served already. We keep track of serviced + /// assignments in order to adhere to the specified ratios. + assignments: Vec<(CoreAssignment, AssignmentState)>, + /// When do our assignments become invalid if at all? + /// + /// If this is `Some`, then this `CoreState` will be dropped at that block number. If this is + /// `None`, then we will keep serving our core assignments in a circle until a new set of + /// assignments is scheduled. + end_hint: Option, + /// Position in the assignments we are currently in. + /// + /// Aka which core assignment will be popped next on + /// `AssignmentProvider::pop_assignment_for_core`. + pos: u16, + /// Step width + /// + /// How much we subtract from `AssignmentState::remaining` for a core served. + step: PartsOf57600, +} + +#[derive(Encode, Decode, TypeInfo)] +#[cfg_attr(test, derive(PartialEq, RuntimeDebug, Clone, Copy))] +struct AssignmentState { + /// Ratio of the core this assignment has. + /// + /// As initially received via `assign_core`. + ratio: PartsOf57600, + /// How many parts are remaining in this round? + /// + /// At the end of each round (in preparation for the next), ratio will be added to remaining. + /// Then every time we get scheduled we subtract a core worth of points. Once we reach 0 or a + /// number lower than what a core is worth (`CoreState::step` size), we move on to the next + /// item in the `Vec`. + /// + /// The first round starts with remaining = ratio. + remaining: PartsOf57600, +} + +impl From> for WorkState { + fn from(schedule: Schedule) -> Self { + let Schedule { assignments, end_hint, next_schedule: _ } = schedule; + let step = + if let Some(min_step_assignment) = assignments.iter().min_by(|a, b| a.1.cmp(&b.1)) { + min_step_assignment.1 + } else { + // Assignments empty, should not exist. In any case step size does not matter here: + log::debug!("assignments of a `Schedule` should never be empty."); + PartsOf57600(1) + }; + let assignments = assignments + .into_iter() + .map(|(a, ratio)| (a, AssignmentState { ratio, remaining: ratio })) + .collect(); + + Self { assignments, end_hint, pos: 0, step } + } +} + +#[frame_support::pallet] +pub mod pallet { + use super::*; + + #[pallet::pallet] + #[pallet::without_storage_info] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: + frame_system::Config + configuration::Config + assigner_on_demand::Config + { + } + + /// Scheduled assignment sets. + /// + /// Assignments as of the given block number. They will go into state once the block number is + /// reached (and replace whatever was in there before). + #[pallet::storage] + pub(super) type CoreSchedules = StorageMap< + _, + Twox256, + (BlockNumberFor, CoreIndex), + Schedule>, + OptionQuery, + >; + + /// Assignments which are currently active. + /// + /// They will be picked from `PendingAssignments` once we reach the scheduled block number in + /// `PendingAssignments`. + #[pallet::storage] + pub(super) type CoreDescriptors = StorageMap< + _, + Twox256, + CoreIndex, + CoreDescriptor>, + ValueQuery, + GetDefault, + >; + + #[pallet::hooks] + impl Hooks> for Pallet {} + + #[pallet::error] + pub enum Error { + AssignmentsEmpty, + /// Assignments together exceeded 57600. + OverScheduled, + /// Assignments together less than 57600 + UnderScheduled, + /// assign_core is only allowed to append new assignments at the end of already existing + /// ones. + DisallowedInsert, + /// Tried to insert a schedule for the same core and block number as an existing schedule + DuplicateInsert, + /// Tried to add an unsorted set of assignments + AssignmentsNotSorted, + } +} + +impl AssignmentProvider> for Pallet { + fn pop_assignment_for_core(core_idx: CoreIndex) -> Option { + let now = >::block_number(); + + CoreDescriptors::::mutate(core_idx, |core_state| { + Self::ensure_workload(now, core_idx, core_state); + + let work_state = core_state.current_work.as_mut()?; + + // Wrap around: + work_state.pos = work_state.pos % work_state.assignments.len() as u16; + let (a_type, a_state) = &mut work_state + .assignments + .get_mut(work_state.pos as usize) + .expect("We limited pos to the size of the vec one line above. qed"); + + // advance for next pop: + a_state.remaining = a_state.remaining.saturating_sub(work_state.step); + if a_state.remaining < work_state.step { + // Assignment exhausted, need to move to the next and credit remaining for + // next round. + work_state.pos += 1; + // Reset to ratio + still remaining "credits": + a_state.remaining = a_state.remaining.saturating_add(a_state.ratio); + } + + match a_type { + CoreAssignment::Idle => None, + CoreAssignment::Pool => + assigner_on_demand::Pallet::::pop_assignment_for_core(core_idx), + CoreAssignment::Task(para_id) => Some(Assignment::Bulk((*para_id).into())), + } + }) + } + + fn report_processed(assignment: Assignment) { + match assignment { + Assignment::Pool { para_id, core_index } => + assigner_on_demand::Pallet::::report_processed(para_id, core_index), + Assignment::Bulk(_) => {}, + } + } + + /// Push an assignment back to the front of the queue. + /// + /// The assignment has not been processed yet. Typically used on session boundaries. + /// Parameters: + /// - `assignment`: The on demand assignment. + fn push_back_assignment(assignment: Assignment) { + match assignment { + Assignment::Pool { para_id, core_index } => + assigner_on_demand::Pallet::::push_back_assignment(para_id, core_index), + Assignment::Bulk(_) => { + // Session changes are rough. We just drop assignments that did not make it on a + // session boundary. This seems sensible as bulk is region based. Meaning, even if + // we made the effort catching up on those dropped assignments, this would very + // likely lead to other assignments not getting served at the "end" (when our + // assignment set gets replaced). + }, + } + } + + fn get_provider_config(_core_idx: CoreIndex) -> AssignmentProviderConfig> { + let config = >::config(); + AssignmentProviderConfig { + max_availability_timeouts: config.on_demand_retries, + ttl: config.on_demand_ttl, + } + } + + #[cfg(any(feature = "runtime-benchmarks", test))] + fn get_mock_assignment(_: CoreIndex, para_id: primitives::Id) -> Assignment { + // Given that we are not tracking anything in `Bulk` assignments, it is safe to always + // return a bulk assignment. + Assignment::Bulk(para_id) + } + + fn session_core_count() -> u32 { + let config = >::config(); + config.coretime_cores + } +} + +impl Pallet { + /// Ensure given workload for core is up to date. + fn ensure_workload( + now: BlockNumberFor, + core_idx: CoreIndex, + descriptor: &mut CoreDescriptor>, + ) { + // Workload expired? + if descriptor + .current_work + .as_ref() + .and_then(|w| w.end_hint) + .map_or(false, |e| e <= now) + { + descriptor.current_work = None; + } + + let Some(queue) = descriptor.queue else { + // No queue. + return + }; + + let mut next_scheduled = queue.first; + + if next_scheduled > now { + // Not yet ready. + return + } + + // Update is needed: + let update = loop { + let Some(update) = CoreSchedules::::take((next_scheduled, core_idx)) else { + break None + }; + // Still good? + if update.end_hint.map_or(true, |e| e > now) { + break Some(update) + } + // Move on if possible: + if let Some(n) = update.next_schedule { + next_scheduled = n; + } else { + break None + } + }; + + let new_first = update.as_ref().and_then(|u| u.next_schedule); + descriptor.current_work = update.map(Into::into); + + descriptor.queue = new_first.map(|new_first| { + QueueDescriptor { + first: new_first, + // `last` stays unaffected, if not empty: + last: queue.last, + } + }); + } + + /// Append another assignment for a core. + /// + /// Important only appending is allowed. Meaning, all already existing assignments must have a + /// begin smaller than the one passed here. This restriction exists, because it makes the + /// insertion O(1) and the author could not think of a reason, why this restriction should be + /// causing any problems. Inserting arbitrarily causes a `DispatchError::DisallowedInsert` + /// error. This restriction could easily be lifted if need be and in fact an implementation is + /// available + /// [here](https://github.com/paritytech/polkadot-sdk/pull/1694/commits/c0c23b01fd2830910cde92c11960dad12cdff398#diff-0c85a46e448de79a5452395829986ee8747e17a857c27ab624304987d2dde8baR386). + /// The problem is that insertion complexity then depends on the size of the existing queue, + /// which makes determining weights hard and could lead to issues like overweight blocks (at + /// least in theory). + pub fn assign_core( + core_idx: CoreIndex, + begin: BlockNumberFor, + assignments: Vec<(CoreAssignment, PartsOf57600)>, + end_hint: Option>, + ) -> Result<(), DispatchError> { + // There should be at least one assignment. + ensure!(!assignments.is_empty(), Error::::AssignmentsEmpty); + + // Checking for sort and unique manually, since we don't have access to iterator tools. + // This way of checking uniqueness only works since we also check sortedness. + assignments.iter().map(|x| &x.0).try_fold(None, |prev, cur| { + if prev.map_or(false, |p| p >= cur) { + Err(Error::::AssignmentsNotSorted) + } else { + Ok(Some(cur)) + } + })?; + + // Check that the total parts between all assignments are equal to 57600 + let parts_sum = assignments + .iter() + .map(|assignment| assignment.1) + .try_fold(PartsOf57600::ZERO, |sum, parts| { + sum.checked_add(parts).ok_or(Error::::OverScheduled) + })?; + ensure!(parts_sum.is_full(), Error::::UnderScheduled); + + CoreDescriptors::::mutate(core_idx, |core_descriptor| { + let new_queue = match core_descriptor.queue { + Some(queue) => { + ensure!(begin > queue.last, Error::::DisallowedInsert); + + CoreSchedules::::try_mutate((queue.last, core_idx), |schedule| { + if let Some(schedule) = schedule.as_mut() { + debug_assert!(schedule.next_schedule.is_none(), "queue.end was supposed to be the end, so the next item must be `None`!"); + schedule.next_schedule = Some(begin); + } else { + defensive!("Queue end entry does not exist?"); + } + CoreSchedules::::try_mutate((begin, core_idx), |schedule| { + // It should already be impossible to overwrite an existing schedule due + // to strictly increasing block number. But we check here for safety and + // in case the design changes. + ensure!(schedule.is_none(), Error::::DuplicateInsert); + *schedule = + Some(Schedule { assignments, end_hint, next_schedule: None }); + Ok::<(), DispatchError>(()) + })?; + Ok::<(), DispatchError>(()) + })?; + + QueueDescriptor { first: queue.first, last: begin } + }, + None => { + // Queue empty, just insert: + CoreSchedules::::insert( + (begin, core_idx), + Schedule { assignments, end_hint, next_schedule: None }, + ); + QueueDescriptor { first: begin, last: begin } + }, + }; + core_descriptor.queue = Some(new_queue); + Ok(()) + }) + } +} + +impl AssignCoretime for Pallet { + fn assign_coretime(id: ParaId) -> DispatchResult { + let current_block = frame_system::Pallet::::block_number(); + + // Add a new core and assign the para to it. + let mut config = >::config(); + let core = config.coretime_cores; + config.coretime_cores.saturating_inc(); + + // `assign_coretime` is only called at genesis or by root, so setting the active + // config here is fine. + configuration::Pallet::::force_set_active_config(config); + + let begin = current_block + One::one(); + let assignment = vec![(pallet_broker::CoreAssignment::Task(id.into()), PartsOf57600::FULL)]; + Pallet::::assign_core(CoreIndex(core), begin, assignment, None) + } +} diff --git a/polkadot/runtime/parachains/src/assigner_coretime/tests.rs b/polkadot/runtime/parachains/src/assigner_coretime/tests.rs new file mode 100644 index 000000000000..998e39670f97 --- /dev/null +++ b/polkadot/runtime/parachains/src/assigner_coretime/tests.rs @@ -0,0 +1,817 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +use super::*; + +use crate::{ + assigner_coretime::{mock_helpers::GenesisConfigBuilder, pallet::Error, Schedule}, + initializer::SessionChangeNotification, + mock::{ + new_test_ext, Balances, CoretimeAssigner, OnDemandAssigner, Paras, ParasShared, + RuntimeOrigin, Scheduler, System, Test, + }, + paras::{ParaGenesisArgs, ParaKind}, + scheduler::common::Assignment, +}; +use frame_support::{assert_noop, assert_ok, pallet_prelude::*, traits::Currency}; +use pallet_broker::TaskId; +use primitives::{BlockNumber, Id as ParaId, SessionIndex, ValidationCode}; +use sp_std::collections::btree_map::BTreeMap; + +fn schedule_blank_para(id: ParaId, parakind: ParaKind) { + let validation_code: ValidationCode = vec![1, 2, 3].into(); + assert_ok!(Paras::schedule_para_initialize( + id, + ParaGenesisArgs { + genesis_head: Vec::new().into(), + validation_code: validation_code.clone(), + para_kind: parakind, + } + )); + + assert_ok!(Paras::add_trusted_validation_code(RuntimeOrigin::root(), validation_code)); +} + +fn run_to_block( + to: BlockNumber, + new_session: impl Fn(BlockNumber) -> Option>, +) { + while System::block_number() < to { + let b = System::block_number(); + + Scheduler::initializer_finalize(); + Paras::initializer_finalize(b); + + if let Some(notification) = new_session(b + 1) { + let mut notification_with_session_index = notification; + // We will make every session change trigger an action queue. Normally this may require + // 2 or more session changes. + if notification_with_session_index.session_index == SessionIndex::default() { + notification_with_session_index.session_index = ParasShared::scheduled_session(); + } + Paras::initializer_on_new_session(¬ification_with_session_index); + Scheduler::initializer_on_new_session(¬ification_with_session_index); + } + + System::on_finalize(b); + + System::on_initialize(b + 1); + System::set_block_number(b + 1); + + Paras::initializer_initialize(b + 1); + Scheduler::initializer_initialize(b + 1); + + // In the real runtime this is expected to be called by the `InclusionInherent` pallet. + Scheduler::free_cores_and_fill_claimqueue(BTreeMap::new(), b + 1); + } +} + +fn default_test_assignments() -> Vec<(CoreAssignment, PartsOf57600)> { + vec![(CoreAssignment::Idle, PartsOf57600::FULL)] +} + +fn default_test_schedule() -> Schedule> { + Schedule { assignments: default_test_assignments(), end_hint: None, next_schedule: None } +} + +#[test] +// Should create new QueueDescriptor and add new schedule to CoreSchedules +fn assign_core_works_with_no_prior_schedule() { + let core_idx = CoreIndex(0); + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + run_to_block(1, |n| if n == 1 { Some(Default::default()) } else { None }); + + // Call assign_core + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + default_test_assignments(), + None, + )); + + // Check CoreSchedules + assert_eq!( + CoreSchedules::::get((BlockNumberFor::::from(11u32), core_idx)), + Some(default_test_schedule()) + ); + + // Check QueueDescriptor + assert_eq!( + CoreDescriptors::::get(core_idx) + .queue + .as_ref() + .and_then(|q| Some(q.first)), + Some(BlockNumberFor::::from(11u32)) + ); + assert_eq!( + CoreDescriptors::::get(core_idx).queue.as_ref().and_then(|q| Some(q.last)), + Some(BlockNumberFor::::from(11u32)) + ); + }); +} + +#[test] +fn end_hint_is_properly_honored() { + let core_idx = CoreIndex(0); + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + run_to_block(1, |n| if n == 1 { Some(Default::default()) } else { None }); + + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + vec![(CoreAssignment::Task(1), PartsOf57600::FULL)], + Some(15u32), + )); + + assert!( + CoretimeAssigner::pop_assignment_for_core(core_idx).is_none(), + "No assignment yet in effect" + ); + + run_to_block(11, |_| None); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(1.into())), + "Assignment should now be present" + ); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(1.into())), + "Nothing changed, assignment should still be present" + ); + + run_to_block(15, |_| None); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + None, + "Assignment should now be gone" + ); + + // Insert assignment that is already dead: + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + vec![(CoreAssignment::Task(1), PartsOf57600::FULL)], + Some(15u32), + )); + + // Core should still be empty: + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + None, + "Assignment should now be gone" + ); + }); +} + +#[test] +// Should update last in QueueDescriptor and add new schedule to CoreSchedules +fn assign_core_works_with_prior_schedule() { + let core_idx = CoreIndex(0); + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + run_to_block(1, |n| if n == 1 { Some(Default::default()) } else { None }); + let default_with_next_schedule = + Schedule { next_schedule: Some(15u32), ..default_test_schedule() }; + + // Call assign_core twice + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + default_test_assignments(), + None, + )); + + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(15u32), + default_test_assignments(), + None, + )); + + // Check CoreSchedules for two entries + assert_eq!( + CoreSchedules::::get((BlockNumberFor::::from(11u32), core_idx)), + Some(default_with_next_schedule) + ); + assert_eq!( + CoreSchedules::::get((BlockNumberFor::::from(15u32), core_idx)), + Some(default_test_schedule()) + ); + + // Check QueueDescriptor + assert_eq!( + CoreDescriptors::::get(core_idx) + .queue + .as_ref() + .and_then(|q| Some(q.first)), + Some(BlockNumberFor::::from(11u32)) + ); + assert_eq!( + CoreDescriptors::::get(core_idx).queue.as_ref().and_then(|q| Some(q.last)), + Some(BlockNumberFor::::from(15u32)) + ); + }); +} + +#[test] +// Invariants: We assume that CoreSchedules is append only and consumed. In other words new +// schedules inserted for a core must have a higher block number than all of the already existing +// schedules. +fn assign_core_enforces_higher_block_number() { + let core_idx = CoreIndex(0); + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + run_to_block(1, |n| if n == 1 { Some(Default::default()) } else { None }); + + // Call assign core twice to establish some schedules + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(12u32), + default_test_assignments(), + None, + )); + + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(15u32), + default_test_assignments(), + None, + )); + + // Call assign core with block number before QueueDescriptor first, expecting an error + assert_noop!( + CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + default_test_assignments(), + None, + ), + Error::::DisallowedInsert + ); + + // Call assign core with block number between already scheduled assignments, expecting an + // error + assert_noop!( + CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(13u32), + default_test_assignments(), + None, + ), + Error::::DisallowedInsert + ); + }); +} + +#[test] +fn assign_core_enforces_well_formed_schedule() { + let para_id = ParaId::from(1u32); + let core_idx = CoreIndex(0); + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + run_to_block(1, |n| if n == 1 { Some(Default::default()) } else { None }); + + let empty_assignments: Vec<(CoreAssignment, PartsOf57600)> = vec![]; + let overscheduled = vec![ + (CoreAssignment::Pool, PartsOf57600::FULL), + (CoreAssignment::Task(para_id.into()), PartsOf57600::FULL), + ]; + let underscheduled = vec![(CoreAssignment::Pool, PartsOf57600(30000))]; + let not_unique = vec![ + (CoreAssignment::Pool, PartsOf57600::FULL / 2), + (CoreAssignment::Pool, PartsOf57600::FULL / 2), + ]; + let not_sorted = vec![ + (CoreAssignment::Task(para_id.into()), PartsOf57600(19200)), + (CoreAssignment::Pool, PartsOf57600(19200)), + (CoreAssignment::Idle, PartsOf57600(19200)), + ]; + + // Attempting assign_core with malformed assignments such that all error cases + // are tested + assert_noop!( + CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + empty_assignments, + None, + ), + Error::::AssignmentsEmpty + ); + assert_noop!( + CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + overscheduled, + None, + ), + Error::::OverScheduled + ); + assert_noop!( + CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + underscheduled, + None, + ), + Error::::UnderScheduled + ); + assert_noop!( + CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + not_unique, + None, + ), + Error::::AssignmentsNotSorted + ); + assert_noop!( + CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + not_sorted, + None, + ), + Error::::AssignmentsNotSorted + ); + }); +} + +#[test] +fn next_schedule_always_points_to_next_work_plan_item() { + let core_idx = CoreIndex(0); + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + run_to_block(1, |n| if n == 1 { Some(Default::default()) } else { None }); + let start_1 = 15u32; + let start_2 = 20u32; + let start_3 = 25u32; + let start_4 = 30u32; + let start_5 = 35u32; + + let expected_schedule_3 = + Schedule { next_schedule: Some(start_4), ..default_test_schedule() }; + let expected_schedule_4 = + Schedule { next_schedule: Some(start_5), ..default_test_schedule() }; + let expected_schedule_5 = default_test_schedule(); + + // Call assign_core for each of five schedules + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(start_1), + default_test_assignments(), + None, + )); + + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(start_2), + default_test_assignments(), + None, + )); + + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(start_3), + default_test_assignments(), + None, + )); + + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(start_4), + default_test_assignments(), + None, + )); + + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(start_5), + default_test_assignments(), + None, + )); + + // Rotate through the first two schedules + run_to_block(start_1, |n| if n == start_1 { Some(Default::default()) } else { None }); + CoretimeAssigner::pop_assignment_for_core(core_idx); + run_to_block(start_2, |n| if n == start_2 { Some(Default::default()) } else { None }); + CoretimeAssigner::pop_assignment_for_core(core_idx); + + // Use saved starting block numbers to check that schedules chain + // together correctly + assert_eq!( + CoreSchedules::::get((BlockNumberFor::::from(start_3), core_idx)), + Some(expected_schedule_3) + ); + assert_eq!( + CoreSchedules::::get((BlockNumberFor::::from(start_4), core_idx)), + Some(expected_schedule_4) + ); + assert_eq!( + CoreSchedules::::get((BlockNumberFor::::from(start_5), core_idx)), + Some(expected_schedule_5) + ); + + // Check QueueDescriptor + assert_eq!( + CoreDescriptors::::get(core_idx) + .queue + .as_ref() + .and_then(|q| Some(q.first)), + Some(start_3) + ); + assert_eq!( + CoreDescriptors::::get(core_idx).queue.as_ref().and_then(|q| Some(q.last)), + Some(start_5) + ); + }); +} + +#[test] +fn ensure_workload_works() { + let core_idx = CoreIndex(0); + let test_assignment_state = + AssignmentState { ratio: PartsOf57600::FULL, remaining: PartsOf57600::FULL }; + + let empty_descriptor: CoreDescriptor> = + CoreDescriptor { queue: None, current_work: None }; + let assignments_queued_descriptor = CoreDescriptor { + queue: Some(QueueDescriptor { + first: BlockNumberFor::::from(11u32), + last: BlockNumberFor::::from(11u32), + }), + current_work: None, + }; + let assignments_active_descriptor = CoreDescriptor { + queue: None, + current_work: Some(WorkState { + assignments: vec![(CoreAssignment::Pool, test_assignment_state)], + end_hint: Some(BlockNumberFor::::from(15u32)), + pos: 0, + step: PartsOf57600::FULL, + }), + }; + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + let mut core_descriptor: CoreDescriptor> = empty_descriptor.clone(); + run_to_block(1, |n| if n == 1 { Some(Default::default()) } else { None }); + + // Case 1: No new schedule in CoreSchedules for core + CoretimeAssigner::ensure_workload(10u32, core_idx, &mut core_descriptor); + assert_eq!(core_descriptor, empty_descriptor); + + // Case 2: New schedule exists in CoreSchedules for core, but new + // schedule start is not yet reached. + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + vec![(CoreAssignment::Pool, PartsOf57600::FULL)], + Some(BlockNumberFor::::from(15u32)), + )); + + // Propagate changes from storage to Core_Descriptor handle. Normally + // pop_assignment_for_core would handle this. + core_descriptor = CoreDescriptors::::get(core_idx); + + CoretimeAssigner::ensure_workload(10u32, core_idx, &mut core_descriptor); + assert_eq!(core_descriptor, assignments_queued_descriptor); + + // Case 3: Next schedule exists in CoreSchedules for core. Next starting + // block has been reached. Swaps new WorkState into CoreDescriptors from + // CoreSchedules. + CoretimeAssigner::ensure_workload(11u32, core_idx, &mut core_descriptor); + assert_eq!(core_descriptor, assignments_active_descriptor); + + // Case 4: end_hint reached but new schedule start not yet reached. WorkState in + // CoreDescriptor is cleared + CoretimeAssigner::ensure_workload(15u32, core_idx, &mut core_descriptor); + assert_eq!(core_descriptor, empty_descriptor); + }); +} + +#[test] +fn pop_assignment_for_core_works() { + let para_id = ParaId::from(1); + let core_idx = CoreIndex(0); + let alice = 1u64; + let amt = 10_000_000u128; + + let assignments_pool = vec![(CoreAssignment::Pool, PartsOf57600::FULL)]; + let assignments_task = vec![(CoreAssignment::Task(para_id.into()), PartsOf57600::FULL)]; + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + // Initialize the parathread, wait for it to be ready, then add an + // on demand order to later pop with our Coretime assigner. + schedule_blank_para(para_id, ParaKind::Parathread); + Balances::make_free_balance_be(&alice, amt); + run_to_block(1, |n| if n == 1 { Some(Default::default()) } else { None }); + assert_ok!(OnDemandAssigner::place_order_allow_death( + RuntimeOrigin::signed(alice), + amt, + para_id + )); + + // Case 1: Assignment idle + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + default_test_assignments(), // Default is Idle + None, + )); + + run_to_block(11, |n| if n == 11 { Some(Default::default()) } else { None }); + + assert_eq!(CoretimeAssigner::pop_assignment_for_core(core_idx), None); + + // Case 2: Assignment pool + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(21u32), + assignments_pool, + None, + )); + + run_to_block(21, |n| if n == 21 { Some(Default::default()) } else { None }); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Pool { para_id, core_index: 0.into() }) + ); + + // Case 3: Assignment task + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(31u32), + assignments_task, + None, + )); + + run_to_block(31, |n| if n == 31 { Some(Default::default()) } else { None }); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(para_id)) + ); + }); +} + +#[test] +fn assignment_proportions_in_core_state_work() { + let core_idx = CoreIndex(0); + let task_1 = TaskId::from(1u32); + let task_2 = TaskId::from(2u32); + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + run_to_block(1, |n| if n == 1 { Some(Default::default()) } else { None }); + + // Task 1 gets 2/3 core usage, while task 2 gets 1/3 + let test_assignments = vec![ + (CoreAssignment::Task(task_1), PartsOf57600::FULL / 3 * 2), + (CoreAssignment::Task(task_2), PartsOf57600::FULL / 3), + ]; + + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + test_assignments, + None, + )); + + run_to_block(11, |n| if n == 11 { Some(Default::default()) } else { None }); + + // Case 1: Current assignment remaining >= step after pop + { + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_1.into())) + ); + + assert_eq!( + CoreDescriptors::::get(core_idx) + .current_work + .as_ref() + .and_then(|w| Some(w.pos)), + Some(0u16) + ); + // Consumed step should be 1/3 of core parts, leaving 1/3 remaining + assert_eq!( + CoreDescriptors::::get(core_idx) + .current_work + .as_ref() + .and_then(|w| Some(w.assignments[0].1.remaining)), + Some(PartsOf57600::FULL / 3) + ); + } + + // Case 2: Current assignment remaning < step after pop + { + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_1.into())) + ); + // Pos should have incremented, as assignment had remaining < step + assert_eq!( + CoreDescriptors::::get(core_idx) + .current_work + .as_ref() + .and_then(|w| Some(w.pos)), + Some(1u16) + ); + // Remaining should have started at 1/3 of core work parts. We then subtract + // step (1/3) and add back ratio (2/3), leaving us with 2/3 of core work parts. + assert_eq!( + CoreDescriptors::::get(core_idx) + .current_work + .as_ref() + .and_then(|w| Some(w.assignments[0].1.remaining)), + Some(PartsOf57600::FULL / 3 * 2) + ); + } + + // Final check, task 2's turn to be served + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_2.into())) + ); + }); +} + +#[test] +fn equal_assignments_served_equally() { + let core_idx = CoreIndex(0); + let task_1 = TaskId::from(1u32); + let task_2 = TaskId::from(2u32); + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + run_to_block(1, |n| if n == 1 { Some(Default::default()) } else { None }); + + // Tasks 1 and 2 get equal work parts + let test_assignments = vec![ + (CoreAssignment::Task(task_1), PartsOf57600::FULL / 2), + (CoreAssignment::Task(task_2), PartsOf57600::FULL / 2), + ]; + + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + test_assignments, + None, + )); + + run_to_block(11, |n| if n == 11 { Some(Default::default()) } else { None }); + + // Test that popped assignments alternate between tasks 1 and 2 + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_1.into())) + ); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_2.into())) + ); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_1.into())) + ); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_2.into())) + ); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_1.into())) + ); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_2.into())) + ); + }); +} + +#[test] +// Checks that core is shared fairly, even in case of `ratio` not being +// divisible by `step` (over multiple rounds). +fn assignment_proportions_indivisible_by_step_work() { + let core_idx = CoreIndex(0); + let task_1 = TaskId::from(1u32); + let ratio_1 = PartsOf57600::FULL / 5 * 3; + let ratio_2 = PartsOf57600::FULL / 5 * 2; + let task_2 = TaskId::from(2u32); + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + run_to_block(1, |n| if n == 1 { Some(Default::default()) } else { None }); + + // Task 1 gets 3/5 core usage, while task 2 gets 2/5. That way + // step is set to 2/5 and task 1 is indivisible by step. + let test_assignments = + vec![(CoreAssignment::Task(task_1), ratio_1), (CoreAssignment::Task(task_2), ratio_2)]; + + assert_ok!(CoretimeAssigner::assign_core( + core_idx, + BlockNumberFor::::from(11u32), + test_assignments, + None, + )); + + run_to_block(11, |n| if n == 11 { Some(Default::default()) } else { None }); + + // Pop 5 assignments. Should Result in the the following work ordering: + // 1, 2, 1, 1, 2. The remaining parts for each assignment should be same + // at the end as in the beginning. + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_1.into())) + ); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_2.into())) + ); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_1.into())) + ); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_1.into())) + ); + + assert_eq!( + CoretimeAssigner::pop_assignment_for_core(core_idx), + Some(Assignment::Bulk(task_2.into())) + ); + + // Remaining should equal ratio for both assignments. + assert_eq!( + CoreDescriptors::::get(core_idx) + .current_work + .as_ref() + .and_then(|w| Some(w.assignments[0].1.remaining)), + Some(ratio_1) + ); + assert_eq!( + CoreDescriptors::::get(core_idx) + .current_work + .as_ref() + .and_then(|w| Some(w.assignments[1].1.remaining)), + Some(ratio_2) + ); + }); +} + +#[cfg(test)] +impl std::ops::Div for PartsOf57600 { + type Output = Self; + + fn div(self, rhs: u16) -> Self::Output { + if rhs == 0 { + panic!("Cannot divide by zero!"); + } + + Self(self.0 / rhs) + } +} + +#[cfg(test)] +impl std::ops::Mul for PartsOf57600 { + type Output = Self; + + fn mul(self, rhs: u16) -> Self { + Self(self.0 * rhs) + } +} + +#[test] +fn parts_of_57600_ops() { + assert!(PartsOf57600::new_saturating(57601).is_full()); + assert!(PartsOf57600::FULL.saturating_add(PartsOf57600(1)).is_full()); + assert_eq!(PartsOf57600::ZERO.saturating_sub(PartsOf57600(1)), PartsOf57600::ZERO); + assert_eq!(PartsOf57600::FULL.checked_add(PartsOf57600(0)), Some(PartsOf57600::FULL)); + assert_eq!(PartsOf57600::FULL.checked_add(PartsOf57600(1)), None); +} diff --git a/polkadot/runtime/parachains/src/assigner_on_demand/benchmarking.rs b/polkadot/runtime/parachains/src/assigner_on_demand/benchmarking.rs index 42ca94d5185f..5a6060cd2b4e 100644 --- a/polkadot/runtime/parachains/src/assigner_on_demand/benchmarking.rs +++ b/polkadot/runtime/parachains/src/assigner_on_demand/benchmarking.rs @@ -43,7 +43,7 @@ where { ParasShared::::set_session_index(SESSION_INDEX); let mut config = HostConfiguration::default(); - config.on_demand_cores = 1; + config.coretime_cores = 1; ConfigurationPallet::::force_set_active_config(config); let mut parachains = ParachainsCache::new(); ParasPallet::::initialize_para_now( @@ -70,11 +70,10 @@ mod benchmarks { let para_id = ParaId::from(111u32); init_parathread::(para_id); T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); - let assignment = Assignment::new(para_id); + let order = EnqueuedOrder::new(para_id); for _ in 0..s { - Pallet::::add_on_demand_assignment(assignment.clone(), QueuePushDirection::Back) - .unwrap(); + Pallet::::add_on_demand_order(order.clone(), QueuePushDirection::Back).unwrap(); } #[extrinsic_call] @@ -88,11 +87,10 @@ mod benchmarks { let para_id = ParaId::from(111u32); init_parathread::(para_id); T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); - let assignment = Assignment::new(para_id); + let order = EnqueuedOrder::new(para_id); for _ in 0..s { - Pallet::::add_on_demand_assignment(assignment.clone(), QueuePushDirection::Back) - .unwrap(); + Pallet::::add_on_demand_order(order.clone(), QueuePushDirection::Back).unwrap(); } #[extrinsic_call] diff --git a/polkadot/runtime/parachains/src/assigner_on_demand/mock_helpers.rs b/polkadot/runtime/parachains/src/assigner_on_demand/mock_helpers.rs index acfb24cbf194..de30330ac84e 100644 --- a/polkadot/runtime/parachains/src/assigner_on_demand/mock_helpers.rs +++ b/polkadot/runtime/parachains/src/assigner_on_demand/mock_helpers.rs @@ -27,7 +27,7 @@ use crate::{ use primitives::{Balance, HeadData, ValidationCode}; -pub fn default_genesis_config() -> MockGenesisConfig { +fn default_genesis_config() -> MockGenesisConfig { MockGenesisConfig { configuration: crate::configuration::GenesisConfig { config: crate::configuration::HostConfiguration { ..Default::default() }, @@ -63,7 +63,7 @@ impl GenesisConfigBuilder { pub(super) fn build(self) -> MockGenesisConfig { let mut genesis = default_genesis_config(); let config = &mut genesis.configuration.config; - config.on_demand_cores = self.on_demand_cores; + config.coretime_cores = self.on_demand_cores; config.on_demand_base_fee = self.on_demand_base_fee; config.on_demand_fee_variability = self.on_demand_fee_variability; config.on_demand_queue_max_size = self.on_demand_max_queue_size; diff --git a/polkadot/runtime/parachains/src/assigner_on_demand/mod.rs b/polkadot/runtime/parachains/src/assigner_on_demand/mod.rs index f4214027a6bd..1b746e88694c 100644 --- a/polkadot/runtime/parachains/src/assigner_on_demand/mod.rs +++ b/polkadot/runtime/parachains/src/assigner_on_demand/mod.rs @@ -32,10 +32,7 @@ mod mock_helpers; #[cfg(test)] mod tests; -use crate::{ - configuration, paras, - scheduler::common::{Assignment, AssignmentProvider, AssignmentProviderConfig}, -}; +use crate::{configuration, paras, scheduler::common::Assignment}; use frame_support::{ pallet_prelude::*, @@ -79,7 +76,7 @@ impl WeightInfo for TestWeightInfo { /// Keeps track of how many assignments a scheduler currently has at a specific `CoreIndex` for a /// specific `ParaId`. #[derive(Encode, Decode, Default, Clone, Copy, TypeInfo)] -#[cfg_attr(test, derive(PartialEq, Debug))] +#[cfg_attr(test, derive(PartialEq, RuntimeDebug))] pub struct CoreAffinityCount { core_idx: CoreIndex, count: u32, @@ -107,6 +104,18 @@ pub enum SpotTrafficCalculationErr { Division, } +/// Internal representation of an order after it has been enqueued already. +#[derive(Encode, Decode, TypeInfo, Debug, PartialEq, Clone)] +pub(super) struct EnqueuedOrder { + pub para_id: ParaId, +} + +impl EnqueuedOrder { + pub fn new(para_id: ParaId) -> Self { + Self { para_id } + } +} + #[frame_support::pallet] pub mod pallet { @@ -140,7 +149,7 @@ pub mod pallet { /// Creates an empty on demand queue if one isn't present in storage already. #[pallet::type_value] - pub fn OnDemandQueueOnEmpty() -> VecDeque { + pub(super) fn OnDemandQueueOnEmpty() -> VecDeque { VecDeque::new() } @@ -153,8 +162,8 @@ pub mod pallet { /// The order storage entry. Uses a VecDeque to be able to push to the front of the /// queue from the scheduler on session boundaries. #[pallet::storage] - pub type OnDemandQueue = - StorageValue<_, VecDeque, ValueQuery, OnDemandQueueOnEmpty>; + pub(super) type OnDemandQueue = + StorageValue<_, VecDeque, ValueQuery, OnDemandQueueOnEmpty>; /// Maps a `ParaId` to `CoreIndex` and keeps track of how many assignments the scheduler has in /// it's lookahead. Keeping track of this affinity prevents parallel execution of the same @@ -182,9 +191,6 @@ pub mod pallet { /// The current spot price is higher than the max amount specified in the `place_order` /// call, making it invalid. SpotPriceHigherThanMaxAmount, - /// There are no on demand cores available. `place_order` will not add anything to the - /// queue. - NoOnDemandCores, } #[pallet::hooks] @@ -248,7 +254,6 @@ pub mod pallet { /// - `InvalidParaId` /// - `QueueFull` /// - `SpotPriceHigherThanMaxAmount` - /// - `NoOnDemandCores` /// /// Events: /// - `SpotOrderPlaced` @@ -276,7 +281,6 @@ pub mod pallet { /// - `InvalidParaId` /// - `QueueFull` /// - `SpotPriceHigherThanMaxAmount` - /// - `NoOnDemandCores` /// /// Events: /// - `SpotOrderPlaced` @@ -311,7 +315,6 @@ where /// - `InvalidParaId` /// - `QueueFull` /// - `SpotPriceHigherThanMaxAmount` - /// - `NoOnDemandCores` /// /// Events: /// - `SpotOrderPlaced` @@ -323,9 +326,6 @@ where ) -> DispatchResult { let config = >::config(); - // Are there any schedulable cores in this session - ensure!(config.on_demand_cores > 0, Error::::NoOnDemandCores); - // Traffic always falls back to 1.0 let traffic = SpotTraffic::::get(); @@ -344,17 +344,15 @@ where existence_requirement, )?; - let assignment = Assignment::new(para_id); + let order = EnqueuedOrder::new(para_id); - let res = Pallet::::add_on_demand_assignment(assignment, QueuePushDirection::Back); + let res = Pallet::::add_on_demand_order(order, QueuePushDirection::Back); - match res { - Ok(_) => { - Pallet::::deposit_event(Event::::OnDemandOrderPlaced { para_id, spot_price }); - return Ok(()) - }, - Err(err) => return Err(err), + if res.is_ok() { + Pallet::::deposit_event(Event::::OnDemandOrderPlaced { para_id, spot_price }); } + + res } /// The spot price multiplier. This is based on the transaction fee calculations defined in: @@ -428,10 +426,10 @@ where } } - /// Adds an assignment to the on demand queue. + /// Adds an order to the on demand queue. /// /// Paramenters: - /// - `assignment`: The on demand assignment to add to the queue. + /// - `order`: The `EnqueuedOrder` to add to the queue. /// - `location`: Whether to push this entry to the back or the front of the queue. Pushing an /// entry to the front of the queue is only used when the scheduler wants to push back an /// entry it has already popped. @@ -441,12 +439,12 @@ where /// Errors: /// - `InvalidParaId` /// - `QueueFull` - pub fn add_on_demand_assignment( - assignment: Assignment, + fn add_on_demand_order( + order: EnqueuedOrder, location: QueuePushDirection, ) -> Result<(), DispatchError> { // Only parathreads are valid paraids for on the go parachains. - ensure!(>::is_parathread(assignment.para_id), Error::::InvalidParaId); + ensure!(>::is_parathread(order.para_id), Error::::InvalidParaId); let config = >::config(); @@ -454,8 +452,8 @@ where // Abort transaction if queue is too large ensure!(Self::queue_size() < config.on_demand_queue_max_size, Error::::QueueFull); match location { - QueuePushDirection::Back => queue.push_back(assignment), - QueuePushDirection::Front => queue.push_front(assignment), + QueuePushDirection::Back => queue.push_back(order), + QueuePushDirection::Front => queue.push_front(order), }; Ok(()) }) @@ -480,7 +478,8 @@ where } /// Getter for the order queue. - pub fn get_queue() -> VecDeque { + #[cfg(test)] + fn get_queue() -> VecDeque { OnDemandQueue::::get() } @@ -528,12 +527,7 @@ where } } -impl AssignmentProvider> for Pallet { - fn session_core_count() -> u32 { - let config = >::config(); - config.on_demand_cores - } - +impl Pallet { /// Take the next queued entry that is available for a given core index. /// Invalidates and removes orders with a `para_id` that is not `ParaLifecycle::Parathread` /// but only in [0..P] range slice of the order queue, where P is the element that is @@ -541,20 +535,8 @@ impl AssignmentProvider> for Pallet { /// /// Parameters: /// - `core_idx`: The core index - /// - `previous_paraid`: Which paraid was previously processed on the requested core. Is None if - /// nothing was processed on the core. - fn pop_assignment_for_core( - core_idx: CoreIndex, - previous_para: Option, - ) -> Option { - // Only decrease the affinity of the previous para if it exists. - // A nonexistant `ParaId` indicates that the scheduler has not processed any - // `ParaId` this session. - if let Some(previous_para_id) = previous_para { - Pallet::::decrease_affinity(previous_para_id, core_idx) - } - - let mut queue: VecDeque = OnDemandQueue::::get(); + pub fn pop_assignment_for_core(core_idx: CoreIndex) -> Option { + let mut queue: VecDeque = OnDemandQueue::::get(); let mut invalidated_para_id_indexes: Vec = vec![]; @@ -591,28 +573,28 @@ impl AssignmentProvider> for Pallet { // Write changes to storage. OnDemandQueue::::set(queue); - popped + popped.map(|p| Assignment::Pool { para_id: p.para_id, core_index: core_idx }) } - /// Push an assignment back to the queue. - /// Typically used on session boundaries. + /// Report that the `para_id` & `core_index` combination was processed. + pub fn report_processed(para_id: ParaId, core_index: CoreIndex) { + Pallet::::decrease_affinity(para_id, core_index) + } + + /// Push an assignment back to the front of the queue. + /// + /// The assignment has not been processed yet. Typically used on session boundaries. /// Parameters: - /// - `core_idx`: The core index /// - `assignment`: The on demand assignment. - fn push_assignment_for_core(core_idx: CoreIndex, assignment: Assignment) { - Pallet::::decrease_affinity(assignment.para_id, core_idx); + pub fn push_back_assignment(para_id: ParaId, core_index: CoreIndex) { + Pallet::::decrease_affinity(para_id, core_index); // Skip the queue on push backs from scheduler - match Pallet::::add_on_demand_assignment(assignment, QueuePushDirection::Front) { + match Pallet::::add_on_demand_order( + EnqueuedOrder::new(para_id), + QueuePushDirection::Front, + ) { Ok(_) => {}, Err(_) => {}, } } - - fn get_provider_config(_core_idx: CoreIndex) -> AssignmentProviderConfig> { - let config = >::config(); - AssignmentProviderConfig { - max_availability_timeouts: config.on_demand_retries, - ttl: config.on_demand_ttl, - } - } } diff --git a/polkadot/runtime/parachains/src/assigner_on_demand/tests.rs b/polkadot/runtime/parachains/src/assigner_on_demand/tests.rs index d07964b69165..8404700780c8 100644 --- a/polkadot/runtime/parachains/src/assigner_on_demand/tests.rs +++ b/polkadot/runtime/parachains/src/assigner_on_demand/tests.rs @@ -24,7 +24,6 @@ use crate::{ System, Test, }, paras::{ParaGenesisArgs, ParaKind}, - scheduler::common::Assignment, }; use frame_support::{assert_noop, assert_ok, error::BadOrigin}; use pallet_balances::Error as BalancesError; @@ -75,7 +74,7 @@ fn run_to_block( Scheduler::initializer_initialize(b + 1); // In the real runtime this is expected to be called by the `InclusionInherent` pallet. - Scheduler::update_claimqueue(BTreeMap::new(), b + 1); + Scheduler::free_cores_and_fill_claimqueue(BTreeMap::new(), b + 1); } } @@ -280,9 +279,9 @@ fn place_order_keep_alive_keeps_alive() { } #[test] -fn add_on_demand_assignment_works() { +fn add_on_demand_order_works() { let para_a = ParaId::from(111); - let assignment = Assignment::new(para_a); + let order = EnqueuedOrder::new(para_a); let mut genesis = GenesisConfigBuilder::default(); genesis.on_demand_max_queue_size = 1; @@ -292,10 +291,7 @@ fn add_on_demand_assignment_works() { // `para_a` is not onboarded as a parathread yet. assert_noop!( - OnDemandAssigner::add_on_demand_assignment( - assignment.clone(), - QueuePushDirection::Back - ), + OnDemandAssigner::add_on_demand_order(order.clone(), QueuePushDirection::Back), Error::::InvalidParaId ); @@ -304,14 +300,11 @@ fn add_on_demand_assignment_works() { assert!(Paras::is_parathread(para_a)); // `para_a` is now onboarded as a valid parathread. - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment.clone(), - QueuePushDirection::Back - )); + assert_ok!(OnDemandAssigner::add_on_demand_order(order.clone(), QueuePushDirection::Back)); // Max queue size is 1, queue should be full. assert_noop!( - OnDemandAssigner::add_on_demand_assignment(assignment, QueuePushDirection::Back), + OnDemandAssigner::add_on_demand_order(order, QueuePushDirection::Back), Error::::QueueFull ); }); @@ -330,29 +323,131 @@ fn spotqueue_push_directions() { run_to_block(11, |n| if n == 11 { Some(Default::default()) } else { None }); - let assignment_a = Assignment { para_id: para_a }; - let assignment_b = Assignment { para_id: para_b }; - let assignment_c = Assignment { para_id: para_c }; + let order_a = EnqueuedOrder::new(para_a); + let order_b = EnqueuedOrder::new(para_b); + let order_c = EnqueuedOrder::new(para_c); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_a.clone(), + assert_ok!(OnDemandAssigner::add_on_demand_order( + order_a.clone(), QueuePushDirection::Front )); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_b.clone(), + assert_ok!(OnDemandAssigner::add_on_demand_order( + order_b.clone(), QueuePushDirection::Front )); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_c.clone(), + assert_ok!(OnDemandAssigner::add_on_demand_order( + order_c.clone(), QueuePushDirection::Back )); assert_eq!(OnDemandAssigner::queue_size(), 3); + assert_eq!(OnDemandAssigner::get_queue(), VecDeque::from(vec![order_b, order_a, order_c])) + }); +} + +#[test] +fn pop_assignment_for_core_works() { + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + let para_a = ParaId::from(111); + let para_b = ParaId::from(110); + schedule_blank_para(para_a, ParaKind::Parathread); + schedule_blank_para(para_b, ParaKind::Parathread); + + run_to_block(11, |n| if n == 11 { Some(Default::default()) } else { None }); + + let order_a = EnqueuedOrder::new(para_a); + let order_b = EnqueuedOrder::new(para_b); + let assignment_a = Assignment::Pool { para_id: para_a, core_index: CoreIndex(0) }; + let assignment_b = Assignment::Pool { para_id: para_b, core_index: CoreIndex(1) }; + + // Pop should return none with empty queue + assert_eq!(OnDemandAssigner::pop_assignment_for_core(CoreIndex(0)), None); + + // Add enough assignments to the order queue. + for _ in 0..2 { + OnDemandAssigner::add_on_demand_order(order_a.clone(), QueuePushDirection::Back) + .expect("Invalid paraid or queue full"); + + OnDemandAssigner::add_on_demand_order(order_b.clone(), QueuePushDirection::Back) + .expect("Invalid paraid or queue full"); + } + + // Queue should contain orders a, b, a, b + { + let queue: Vec = OnDemandQueue::::get().into_iter().collect(); + assert_eq!( + queue, + vec![order_a.clone(), order_b.clone(), order_a.clone(), order_b.clone()] + ); + } + + // Popped assignments should be for the correct paras and cores assert_eq!( - OnDemandAssigner::get_queue(), - VecDeque::from(vec![assignment_b, assignment_a, assignment_c]) - ) + OnDemandAssigner::pop_assignment_for_core(CoreIndex(0)), + Some(assignment_a.clone()) + ); + assert_eq!( + OnDemandAssigner::pop_assignment_for_core(CoreIndex(1)), + Some(assignment_b.clone()) + ); + assert_eq!( + OnDemandAssigner::pop_assignment_for_core(CoreIndex(0)), + Some(assignment_a.clone()) + ); + + // Queue should contain one left over order + { + let queue: Vec = OnDemandQueue::::get().into_iter().collect(); + assert_eq!(queue, vec![order_b.clone(),]); + } + }); +} + +#[test] +fn push_back_assignment_works() { + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + let para_a = ParaId::from(111); + let para_b = ParaId::from(110); + schedule_blank_para(para_a, ParaKind::Parathread); + schedule_blank_para(para_b, ParaKind::Parathread); + + run_to_block(11, |n| if n == 11 { Some(Default::default()) } else { None }); + + let order_a = EnqueuedOrder::new(para_a); + let order_b = EnqueuedOrder::new(para_b); + + // Add enough assignments to the order queue. + OnDemandAssigner::add_on_demand_order(order_a.clone(), QueuePushDirection::Back) + .expect("Invalid paraid or queue full"); + + OnDemandAssigner::add_on_demand_order(order_b.clone(), QueuePushDirection::Back) + .expect("Invalid paraid or queue full"); + + // Pop order a + OnDemandAssigner::pop_assignment_for_core(CoreIndex(0)); + + // Para a should have affinity for core 0 + assert_eq!(OnDemandAssigner::get_affinity_map(para_a).unwrap().count, 1); + assert_eq!(OnDemandAssigner::get_affinity_map(para_a).unwrap().core_idx, CoreIndex(0)); + + // Queue should still contain order b + { + let queue: Vec = OnDemandQueue::::get().into_iter().collect(); + assert_eq!(queue, vec![order_b.clone()]); + } + + // Push back order a + OnDemandAssigner::push_back_assignment(para_a, CoreIndex(0)); + + // Para a should have no affinity + assert_eq!(OnDemandAssigner::get_affinity_map(para_a).is_none(), true); + + // Queue should contain orders a, b. A in front of b. + { + let queue: Vec = OnDemandQueue::::get().into_iter().collect(); + assert_eq!(queue, vec![order_a.clone(), order_b.clone()]); + } }); } @@ -360,39 +455,38 @@ fn spotqueue_push_directions() { fn affinity_changes_work() { new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { let para_a = ParaId::from(111); + let core_index = CoreIndex(0); schedule_blank_para(para_a, ParaKind::Parathread); + let order_a = EnqueuedOrder::new(para_a); run_to_block(11, |n| if n == 11 { Some(Default::default()) } else { None }); - let assignment_a = Assignment { para_id: para_a }; // There should be no affinity before starting. assert!(OnDemandAssigner::get_affinity_map(para_a).is_none()); // Add enough assignments to the order queue. for _ in 0..10 { - OnDemandAssigner::add_on_demand_assignment( - assignment_a.clone(), - QueuePushDirection::Front, - ) - .expect("Invalid paraid or queue full"); + OnDemandAssigner::add_on_demand_order(order_a.clone(), QueuePushDirection::Front) + .expect("Invalid paraid or queue full"); } // There should be no affinity before the scheduler pops. assert!(OnDemandAssigner::get_affinity_map(para_a).is_none()); - OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), None); + OnDemandAssigner::pop_assignment_for_core(core_index); // Affinity count is 1 after popping. assert_eq!(OnDemandAssigner::get_affinity_map(para_a).unwrap().count, 1); - OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), Some(para_a)); + OnDemandAssigner::report_processed(para_a, 0.into()); + OnDemandAssigner::pop_assignment_for_core(core_index); // Affinity count is 1 after popping with a previous para. assert_eq!(OnDemandAssigner::get_affinity_map(para_a).unwrap().count, 1); assert_eq!(OnDemandAssigner::queue_size(), 8); for _ in 0..3 { - OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), None); + OnDemandAssigner::pop_assignment_for_core(core_index); } // Affinity count is 4 after popping 3 times without a previous para. @@ -400,7 +494,8 @@ fn affinity_changes_work() { assert_eq!(OnDemandAssigner::queue_size(), 5); for _ in 0..5 { - OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), Some(para_a)); + OnDemandAssigner::report_processed(para_a, 0.into()); + OnDemandAssigner::pop_assignment_for_core(core_index); } // Affinity count should still be 4 but queue should be empty. @@ -409,12 +504,14 @@ fn affinity_changes_work() { // Pop 4 times and get to exactly 0 (None) affinity. for _ in 0..4 { - OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), Some(para_a)); + OnDemandAssigner::report_processed(para_a, 0.into()); + OnDemandAssigner::pop_assignment_for_core(core_index); } assert!(OnDemandAssigner::get_affinity_map(para_a).is_none()); // Decreasing affinity beyond 0 should still be None. - OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), Some(para_a)); + OnDemandAssigner::report_processed(para_a, 0.into()); + OnDemandAssigner::pop_assignment_for_core(core_index); assert!(OnDemandAssigner::get_affinity_map(para_a).is_none()); }); } @@ -430,28 +527,28 @@ fn affinity_prohibits_parallel_scheduling() { run_to_block(11, |n| if n == 11 { Some(Default::default()) } else { None }); - let assignment_a = Assignment { para_id: para_a }; - let assignment_b = Assignment { para_id: para_b }; + let order_a = EnqueuedOrder::new(para_a); + let order_b = EnqueuedOrder::new(para_b); // There should be no affinity before starting. assert!(OnDemandAssigner::get_affinity_map(para_a).is_none()); assert!(OnDemandAssigner::get_affinity_map(para_b).is_none()); // Add 2 assignments for para_a for every para_b. - OnDemandAssigner::add_on_demand_assignment(assignment_a.clone(), QueuePushDirection::Back) + OnDemandAssigner::add_on_demand_order(order_a.clone(), QueuePushDirection::Back) .expect("Invalid paraid or queue full"); - OnDemandAssigner::add_on_demand_assignment(assignment_a.clone(), QueuePushDirection::Back) + OnDemandAssigner::add_on_demand_order(order_a.clone(), QueuePushDirection::Back) .expect("Invalid paraid or queue full"); - OnDemandAssigner::add_on_demand_assignment(assignment_b.clone(), QueuePushDirection::Back) + OnDemandAssigner::add_on_demand_order(order_b.clone(), QueuePushDirection::Back) .expect("Invalid paraid or queue full"); assert_eq!(OnDemandAssigner::queue_size(), 3); // Approximate having 1 core. for _ in 0..3 { - OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), None); + OnDemandAssigner::pop_assignment_for_core(CoreIndex(0)); } // Affinity on one core is meaningless. @@ -463,24 +560,25 @@ fn affinity_prohibits_parallel_scheduling() { ); // Clear affinity - OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), Some(para_a)); - OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), Some(para_a)); - OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), Some(para_b)); + OnDemandAssigner::report_processed(para_a, 0.into()); + OnDemandAssigner::report_processed(para_a, 0.into()); + OnDemandAssigner::report_processed(para_b, 0.into()); // Add 2 assignments for para_a for every para_b. - OnDemandAssigner::add_on_demand_assignment(assignment_a.clone(), QueuePushDirection::Back) + OnDemandAssigner::add_on_demand_order(order_a.clone(), QueuePushDirection::Back) .expect("Invalid paraid or queue full"); - OnDemandAssigner::add_on_demand_assignment(assignment_a.clone(), QueuePushDirection::Back) + OnDemandAssigner::add_on_demand_order(order_a.clone(), QueuePushDirection::Back) .expect("Invalid paraid or queue full"); - OnDemandAssigner::add_on_demand_assignment(assignment_b.clone(), QueuePushDirection::Back) + OnDemandAssigner::add_on_demand_order(order_b.clone(), QueuePushDirection::Back) .expect("Invalid paraid or queue full"); - // Approximate having 2 cores. + // Approximate having 3 cores. CoreIndex 2 should be unable to obtain an assignment for _ in 0..3 { - OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), None); - OnDemandAssigner::pop_assignment_for_core(CoreIndex(1), None); + OnDemandAssigner::pop_assignment_for_core(CoreIndex(0)); + OnDemandAssigner::pop_assignment_for_core(CoreIndex(1)); + assert_eq!(None, OnDemandAssigner::pop_assignment_for_core(CoreIndex(2))); } // Affinity should be the same as before, but on different cores. @@ -488,38 +586,23 @@ fn affinity_prohibits_parallel_scheduling() { assert_eq!(OnDemandAssigner::get_affinity_map(para_b).unwrap().count, 1); assert_eq!(OnDemandAssigner::get_affinity_map(para_a).unwrap().core_idx, CoreIndex(0)); assert_eq!(OnDemandAssigner::get_affinity_map(para_b).unwrap().core_idx, CoreIndex(1)); - }); -} - -#[test] -fn cannot_place_order_when_no_on_demand_cores() { - let mut genesis = GenesisConfigBuilder::default(); - genesis.on_demand_cores = 0; - let para_id = ParaId::from(10); - let alice = 1u64; - let amt = 10_000_000u128; - - new_test_ext(genesis.build()).execute_with(|| { - schedule_blank_para(para_id, ParaKind::Parathread); - Balances::make_free_balance_be(&alice, amt); - assert!(!Paras::is_parathread(para_id)); - - run_to_block(10, |n| if n == 10 { Some(Default::default()) } else { None }); - - assert!(Paras::is_parathread(para_id)); + // Clear affinity + OnDemandAssigner::report_processed(para_a, 0.into()); + OnDemandAssigner::report_processed(para_a, 0.into()); + OnDemandAssigner::report_processed(para_b, 1.into()); - assert_noop!( - OnDemandAssigner::place_order_allow_death(RuntimeOrigin::signed(alice), amt, para_id), - Error::::NoOnDemandCores - ); + // There should be no affinity after clearing. + assert!(OnDemandAssigner::get_affinity_map(para_a).is_none()); + assert!(OnDemandAssigner::get_affinity_map(para_b).is_none()); }); } #[test] fn on_demand_orders_cannot_be_popped_if_lifecycle_changes() { let para_id = ParaId::from(10); - let assignment = Assignment { para_id }; + let core_index = CoreIndex(0); + let order = EnqueuedOrder::new(para_id); new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { // Register the para_id as a parathread @@ -530,17 +613,14 @@ fn on_demand_orders_cannot_be_popped_if_lifecycle_changes() { assert!(Paras::is_parathread(para_id)); // Add two assignments for a para_id with a valid lifecycle. - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment.clone(), - QueuePushDirection::Back - )); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment.clone(), - QueuePushDirection::Back - )); + assert_ok!(OnDemandAssigner::add_on_demand_order(order.clone(), QueuePushDirection::Back)); + assert_ok!(OnDemandAssigner::add_on_demand_order(order.clone(), QueuePushDirection::Back)); // First pop is fine - assert!(OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), None) == Some(assignment)); + assert!( + OnDemandAssigner::pop_assignment_for_core(core_index) == + Some(Assignment::Pool { para_id, core_index }) + ); // Deregister para assert_ok!(Paras::schedule_para_cleanup(para_id)); @@ -551,6 +631,7 @@ fn on_demand_orders_cannot_be_popped_if_lifecycle_changes() { assert!(!Paras::is_parathread(para_id)); // Second pop should be None. - assert!(OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), Some(para_id)) == None); + OnDemandAssigner::report_processed(para_id, core_index); + assert_eq!(OnDemandAssigner::pop_assignment_for_core(core_index), None); }); } diff --git a/polkadot/runtime/parachains/src/assigner_parachains.rs b/polkadot/runtime/parachains/src/assigner_parachains.rs index 866e8290052a..34b5d3c1ec51 100644 --- a/polkadot/runtime/parachains/src/assigner_parachains.rs +++ b/polkadot/runtime/parachains/src/assigner_parachains.rs @@ -17,13 +17,20 @@ //! The bulk (parachain slot auction) blockspace assignment provider. //! This provider is tightly coupled with the configuration and paras modules. +#[cfg(test)] +mod mock_helpers; +#[cfg(test)] +mod tests; + +use frame_system::pallet_prelude::BlockNumberFor; +use primitives::CoreIndex; + use crate::{ configuration, paras, scheduler::common::{Assignment, AssignmentProvider, AssignmentProviderConfig}, }; -use frame_system::pallet_prelude::BlockNumberFor; + pub use pallet::*; -use primitives::{CoreIndex, Id as ParaId}; #[frame_support::pallet] pub mod pallet { @@ -38,23 +45,18 @@ pub mod pallet { } impl AssignmentProvider> for Pallet { - fn session_core_count() -> u32 { - paras::Parachains::::decode_len().unwrap_or(0) as u32 - } - - fn pop_assignment_for_core( - core_idx: CoreIndex, - _concluded_para: Option, - ) -> Option { + fn pop_assignment_for_core(core_idx: CoreIndex) -> Option { >::parachains() .get(core_idx.0 as usize) .copied() - .map(|para_id| Assignment::new(para_id)) + .map(Assignment::Bulk) } + fn report_processed(_: Assignment) {} + /// Bulk assignment has no need to push the assignment back on a session change, /// this is a no-op in the case of a bulk assignment slot. - fn push_assignment_for_core(_: CoreIndex, _: Assignment) {} + fn push_back_assignment(_: Assignment) {} fn get_provider_config(_core_idx: CoreIndex) -> AssignmentProviderConfig> { AssignmentProviderConfig { @@ -65,4 +67,13 @@ impl AssignmentProvider> for Pallet { ttl: 10u32.into(), } } + + #[cfg(any(feature = "runtime-benchmarks", test))] + fn get_mock_assignment(_: CoreIndex, para_id: primitives::Id) -> Assignment { + Assignment::Bulk(para_id) + } + + fn session_core_count() -> u32 { + paras::Parachains::::decode_len().unwrap_or(0) as u32 + } } diff --git a/polkadot/runtime/parachains/src/assigner_parachains/mock_helpers.rs b/polkadot/runtime/parachains/src/assigner_parachains/mock_helpers.rs new file mode 100644 index 000000000000..e6e9fb074aa9 --- /dev/null +++ b/polkadot/runtime/parachains/src/assigner_parachains/mock_helpers.rs @@ -0,0 +1,83 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Helper functions for tests + +use crate::{ + mock::MockGenesisConfig, + paras::{ParaGenesisArgs, ParaKind}, +}; + +use primitives::{Balance, HeadData, ValidationCode}; +use sp_runtime::Perbill; + +fn default_genesis_config() -> MockGenesisConfig { + MockGenesisConfig { + configuration: crate::configuration::GenesisConfig { + config: crate::configuration::HostConfiguration { ..Default::default() }, + }, + ..Default::default() + } +} + +#[derive(Debug)] +pub struct GenesisConfigBuilder { + pub on_demand_cores: u32, + pub on_demand_base_fee: Balance, + pub on_demand_fee_variability: Perbill, + pub on_demand_max_queue_size: u32, + pub on_demand_target_queue_utilization: Perbill, + pub onboarded_on_demand_chains: Vec, +} + +impl Default for GenesisConfigBuilder { + fn default() -> Self { + Self { + on_demand_cores: 10, + on_demand_base_fee: 10_000, + on_demand_fee_variability: Perbill::from_percent(1), + on_demand_max_queue_size: 100, + on_demand_target_queue_utilization: Perbill::from_percent(25), + onboarded_on_demand_chains: vec![], + } + } +} + +impl GenesisConfigBuilder { + pub(super) fn build(self) -> MockGenesisConfig { + let mut genesis = default_genesis_config(); + let config = &mut genesis.configuration.config; + config.coretime_cores = self.on_demand_cores; + config.on_demand_base_fee = self.on_demand_base_fee; + config.on_demand_fee_variability = self.on_demand_fee_variability; + config.on_demand_queue_max_size = self.on_demand_max_queue_size; + config.on_demand_target_queue_utilization = self.on_demand_target_queue_utilization; + + let paras = &mut genesis.paras.paras; + for para_id in self.onboarded_on_demand_chains { + paras.push(( + para_id, + ParaGenesisArgs { + genesis_head: HeadData::from(vec![0u8]), + validation_code: ValidationCode::from(vec![0u8]), + para_kind: ParaKind::Parathread, + }, + )) + } + + genesis + } +} diff --git a/polkadot/runtime/parachains/src/assigner_parachains/tests.rs b/polkadot/runtime/parachains/src/assigner_parachains/tests.rs new file mode 100644 index 000000000000..a110686aaeb0 --- /dev/null +++ b/polkadot/runtime/parachains/src/assigner_parachains/tests.rs @@ -0,0 +1,112 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +use super::*; +use crate::{ + assigner_parachains::mock_helpers::GenesisConfigBuilder, + initializer::SessionChangeNotification, + mock::{ + new_test_ext, ParachainsAssigner, Paras, ParasShared, RuntimeOrigin, Scheduler, System, + }, + paras::{ParaGenesisArgs, ParaKind}, +}; +use frame_support::{assert_ok, pallet_prelude::*}; +use primitives::{BlockNumber, Id as ParaId, SessionIndex, ValidationCode}; +use sp_std::collections::btree_map::BTreeMap; + +fn schedule_blank_para(id: ParaId, parakind: ParaKind) { + let validation_code: ValidationCode = vec![1, 2, 3].into(); + assert_ok!(Paras::schedule_para_initialize( + id, + ParaGenesisArgs { + genesis_head: Vec::new().into(), + validation_code: validation_code.clone(), + para_kind: parakind, + } + )); + + assert_ok!(Paras::add_trusted_validation_code(RuntimeOrigin::root(), validation_code)); +} + +fn run_to_block( + to: BlockNumber, + new_session: impl Fn(BlockNumber) -> Option>, +) { + while System::block_number() < to { + let b = System::block_number(); + + Scheduler::initializer_finalize(); + Paras::initializer_finalize(b); + + if let Some(notification) = new_session(b + 1) { + let mut notification_with_session_index = notification; + // We will make every session change trigger an action queue. Normally this may require + // 2 or more session changes. + if notification_with_session_index.session_index == SessionIndex::default() { + notification_with_session_index.session_index = ParasShared::scheduled_session(); + } + Paras::initializer_on_new_session(¬ification_with_session_index); + Scheduler::initializer_on_new_session(¬ification_with_session_index); + } + + System::on_finalize(b); + + System::on_initialize(b + 1); + System::set_block_number(b + 1); + + Paras::initializer_initialize(b + 1); + Scheduler::initializer_initialize(b + 1); + + // In the real runtime this is expected to be called by the `InclusionInherent` pallet. + Scheduler::free_cores_and_fill_claimqueue(BTreeMap::new(), b + 1); + } +} + +// This and the scheduler test schedule_schedules_including_just_freed together +// ensure that next_up_on_available and next_up_on_time_out will always be +// filled with scheduler claims for lease holding parachains. (Removes the need +// for two other scheduler tests) +#[test] +fn parachains_assigner_pop_assignment_is_always_some() { + let core_index = CoreIndex(0); + let para_id = ParaId::from(10); + let expected_assignment = Assignment::Bulk(para_id); + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + // Register the para_id as a lease holding parachain + schedule_blank_para(para_id, ParaKind::Parachain); + + assert!(!Paras::is_parachain(para_id)); + run_to_block(10, |n| if n == 10 { Some(Default::default()) } else { None }); + assert!(Paras::is_parachain(para_id)); + + for _ in 0..20 { + assert!( + ParachainsAssigner::pop_assignment_for_core(core_index) == + Some(expected_assignment.clone()) + ); + } + + run_to_block(20, |n| if n == 20 { Some(Default::default()) } else { None }); + + for _ in 0..20 { + assert!( + ParachainsAssigner::pop_assignment_for_core(core_index) == + Some(expected_assignment.clone()) + ); + } + }); +} diff --git a/polkadot/runtime/parachains/src/builder.rs b/polkadot/runtime/parachains/src/builder.rs index 23916bbdc8ad..016b3fca589a 100644 --- a/polkadot/runtime/parachains/src/builder.rs +++ b/polkadot/runtime/parachains/src/builder.rs @@ -20,7 +20,7 @@ use crate::{ paras_inherent, scheduler::{ self, - common::{Assignment, AssignmentProviderConfig}, + common::{AssignmentProvider, AssignmentProviderConfig}, CoreOccupied, ParasEntry, }, session_info, shared, @@ -96,6 +96,8 @@ pub(crate) struct BenchBuilder { /// Make every candidate include a code upgrade by setting this to `Some` where the interior /// value is the byte length of the new code. code_upgrade: Option, + /// Specifies whether the claimqueue should be filled. + fill_claimqueue: bool, _phantom: sp_std::marker::PhantomData, } @@ -122,6 +124,7 @@ impl BenchBuilder { dispute_sessions: Default::default(), backed_and_concluding_cores: Default::default(), code_upgrade: None, + fill_claimqueue: true, _phantom: sp_std::marker::PhantomData::, } } @@ -225,6 +228,13 @@ impl BenchBuilder { self.max_validators() / self.max_validators_per_core() } + /// Set whether the claim queue should be filled. + #[cfg(not(feature = "runtime-benchmarks"))] + pub(crate) fn set_fill_claimqueue(mut self, f: bool) -> Self { + self.fill_claimqueue = f; + self + } + /// Get the minimum number of validity votes in order for a backed candidate to be included. #[cfg(feature = "runtime-benchmarks")] pub(crate) fn fallback_min_validity_votes() -> u32 { @@ -643,7 +653,7 @@ impl BenchBuilder { }) .collect(); - DisputeStatementSet { candidate_hash: candidate_hash, session, statements } + DisputeStatementSet { candidate_hash, session, statements } }) .collect() } @@ -663,14 +673,18 @@ impl BenchBuilder { inclusion::PendingAvailability::::remove_all(None); // We don't allow a core to have both disputes and be marked fully available at this block. - let cores = self.max_cores(); + let max_cores = self.max_cores(); let used_cores = (self.dispute_sessions.len() + self.backed_and_concluding_cores.len()) as u32; - assert!(used_cores <= cores); + assert!(used_cores <= max_cores); + let fill_claimqueue = self.fill_claimqueue; // NOTE: there is an n+2 session delay for these actions to take effect. // We are currently in Session 0, so these changes will take effect in Session 2. Self::setup_para_ids(used_cores); + configuration::ActiveConfig::::mutate(|c| { + c.coretime_cores = used_cores; + }); let validator_ids = Self::generate_validator_pairs(self.max_validators()); let target_session = SessionIndex::from(self.target_session); @@ -702,13 +716,33 @@ impl BenchBuilder { .map(|i| { let AssignmentProviderConfig { ttl, .. } = scheduler::Pallet::::assignment_provider_config(CoreIndex(i)); - CoreOccupied::Paras(ParasEntry::new( - Assignment::new(ParaId::from(i as u32)), - now + ttl, - )) + // Load an assignment into provider so that one is present to pop + let assignment = ::AssignmentProvider::get_mock_assignment( + CoreIndex(i), + ParaId::from(i), + ); + CoreOccupied::Paras(ParasEntry::new(assignment, now + ttl)) }) .collect(); scheduler::AvailabilityCores::::set(cores); + if fill_claimqueue { + // Add items to claim queue as well: + let cores = (0..used_cores) + .into_iter() + .map(|i| { + let AssignmentProviderConfig { ttl, .. } = + scheduler::Pallet::::assignment_provider_config(CoreIndex(i)); + // Load an assignment into provider so that one is present to pop + let assignment = + ::AssignmentProvider::get_mock_assignment( + CoreIndex(i), + ParaId::from(i), + ); + (CoreIndex(i), [ParasEntry::new(assignment, now + ttl)].into()) + }) + .collect(); + scheduler::ClaimQueue::::set(cores); + } Bench:: { data: ParachainsInherentData { diff --git a/polkadot/runtime/parachains/src/configuration.rs b/polkadot/runtime/parachains/src/configuration.rs index 272c227dfefe..4619313590eb 100644 --- a/polkadot/runtime/parachains/src/configuration.rs +++ b/polkadot/runtime/parachains/src/configuration.rs @@ -172,8 +172,8 @@ pub struct HostConfiguration { /// How long to keep code on-chain, in blocks. This should be sufficiently long that disputes /// have concluded. pub code_retention_period: BlockNumber, - /// The amount of execution cores to dedicate to on demand execution. - pub on_demand_cores: u32, + /// How many cores are managed by the coretime chain. + pub coretime_cores: u32, /// The number of retries that a on demand author has to submit their block. pub on_demand_retries: u32, /// The maximum queue size of the pay as you go module. @@ -284,7 +284,7 @@ impl> Default for HostConfiguration, new: u32) -> DispatchResult { + pub fn set_coretime_cores(origin: OriginFor, new: u32) -> DispatchResult { ensure_root(origin)?; - Self::schedule_config_update(|config| { - config.on_demand_cores = new; - }) + Self::set_coretime_cores_unchecked(new) } /// Set the number of retries for a particular on demand. @@ -1245,6 +1246,17 @@ pub mod pallet { } } + impl Pallet { + /// Set coretime cores. + /// + /// To be used if authorization is checked otherwise. + pub fn set_coretime_cores_unchecked(new: u32) -> DispatchResult { + Self::schedule_config_update(|config| { + config.coretime_cores = new; + }) + } + } + #[pallet::hooks] impl Hooks> for Pallet { fn integrity_test() { diff --git a/polkadot/runtime/parachains/src/configuration/migration/v11.rs b/polkadot/runtime/parachains/src/configuration/migration/v11.rs index b7dec7070f93..f4db9196b1a0 100644 --- a/polkadot/runtime/parachains/src/configuration/migration/v11.rs +++ b/polkadot/runtime/parachains/src/configuration/migration/v11.rs @@ -126,7 +126,7 @@ hrmp_max_parachain_inbound_channels : pre.hrmp_max_parachain_inbound_channe hrmp_max_parachain_outbound_channels : pre.hrmp_max_parachain_outbound_channels, hrmp_channel_max_message_size : pre.hrmp_channel_max_message_size, code_retention_period : pre.code_retention_period, -on_demand_cores : pre.on_demand_cores, +coretime_cores : pre.on_demand_cores, on_demand_retries : pre.on_demand_retries, group_rotation_frequency : pre.group_rotation_frequency, paras_availability_period : pre.paras_availability_period, @@ -222,7 +222,7 @@ mod tests { assert_eq!(v11.n_delay_tranches, 25); assert_eq!(v11.minimum_validation_upgrade_delay, 5); assert_eq!(v11.group_rotation_frequency, 20); - assert_eq!(v11.on_demand_cores, 0); + assert_eq!(v11.coretime_cores, 0); assert_eq!(v11.on_demand_base_fee, 10_000_000); assert_eq!(v11.minimum_backing_votes, LEGACY_MIN_BACKING_VOTES); assert_eq!(v11.approval_voting_params.max_approval_coalesce_count, 1); @@ -286,7 +286,7 @@ mod tests { assert_eq!(v10.hrmp_max_parachain_inbound_channels , v11.hrmp_max_parachain_inbound_channels); assert_eq!(v10.hrmp_channel_max_message_size , v11.hrmp_channel_max_message_size); assert_eq!(v10.code_retention_period , v11.code_retention_period); - assert_eq!(v10.on_demand_cores , v11.on_demand_cores); + assert_eq!(v10.coretime_cores , v11.coretime_cores); assert_eq!(v10.on_demand_retries , v11.on_demand_retries); assert_eq!(v10.group_rotation_frequency , v11.group_rotation_frequency); assert_eq!(v10.paras_availability_period , v11.paras_availability_period); diff --git a/polkadot/runtime/parachains/src/configuration/tests.rs b/polkadot/runtime/parachains/src/configuration/tests.rs index d88572d3b558..c915eb12a0ca 100644 --- a/polkadot/runtime/parachains/src/configuration/tests.rs +++ b/polkadot/runtime/parachains/src/configuration/tests.rs @@ -283,7 +283,7 @@ fn setting_pending_config_members() { max_code_size: 100_000, max_pov_size: 1024, max_head_data_size: 1_000, - on_demand_cores: 2, + coretime_cores: 2, on_demand_retries: 5, group_rotation_frequency: 20, paras_availability_period: 10, @@ -342,7 +342,7 @@ fn setting_pending_config_members() { Configuration::set_max_pov_size(RuntimeOrigin::root(), new_config.max_pov_size).unwrap(); Configuration::set_max_head_data_size(RuntimeOrigin::root(), new_config.max_head_data_size) .unwrap(); - Configuration::set_on_demand_cores(RuntimeOrigin::root(), new_config.on_demand_cores) + Configuration::set_coretime_cores(RuntimeOrigin::root(), new_config.coretime_cores) .unwrap(); Configuration::set_on_demand_retries(RuntimeOrigin::root(), new_config.on_demand_retries) .unwrap(); diff --git a/polkadot/runtime/parachains/src/coretime/benchmarking.rs b/polkadot/runtime/parachains/src/coretime/benchmarking.rs new file mode 100644 index 000000000000..d1ac71f580ee --- /dev/null +++ b/polkadot/runtime/parachains/src/coretime/benchmarking.rs @@ -0,0 +1,73 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! On demand assigner pallet benchmarking. + +#![cfg(feature = "runtime-benchmarks")] + +use super::*; +use frame_benchmarking::v2::*; +use frame_support::traits::OriginTrait; +use pallet_broker::CoreIndex as BrokerCoreIndex; + +#[benchmarks] +mod benchmarks { + use super::*; + use assigner_coretime::PartsOf57600; + + #[benchmark] + fn request_core_count() { + // Setup + let root_origin = ::RuntimeOrigin::root(); + + #[extrinsic_call] + _( + root_origin as ::RuntimeOrigin, + // random core count + 100, + ) + } + + #[benchmark] + fn assign_core(s: Linear<1, 100>) { + // Setup + let root_origin = ::RuntimeOrigin::root(); + + // Use parameterized assignment count + let mut assignments: Vec<(CoreAssignment, PartsOf57600)> = vec![0u16; s as usize - 1] + .into_iter() + .enumerate() + .map(|(index, parts)| { + (CoreAssignment::Task(index as u32), PartsOf57600::new_saturating(parts)) + }) + .collect(); + // Parts must add up to exactly 57600. Here we add all the parts in one assignment, as + // it won't effect the weight and splitting up the parts into even groupings may not + // work for every value `s`. + assignments.push((CoreAssignment::Task(s as u32), PartsOf57600::FULL)); + + let core_index: BrokerCoreIndex = 0; + + #[extrinsic_call] + _( + root_origin as ::RuntimeOrigin, + core_index, + BlockNumberFor::::from(5u32), + assignments, + Some(BlockNumberFor::::from(20u32)), + ) + } +} diff --git a/polkadot/runtime/parachains/src/coretime/migration.rs b/polkadot/runtime/parachains/src/coretime/migration.rs new file mode 100644 index 000000000000..64c10f731988 --- /dev/null +++ b/polkadot/runtime/parachains/src/coretime/migration.rs @@ -0,0 +1,285 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Migrations for the Coretime pallet. + +pub use v_coretime::{GetLegacyLease, MigrateToCoretime}; + +mod v_coretime { + #[cfg(feature = "try-runtime")] + use crate::scheduler::common::AssignmentProvider; + use crate::{ + assigner_coretime, configuration, + coretime::{mk_coretime_call, Config, PartsOf57600, WeightInfo}, + paras, + }; + #[cfg(feature = "try-runtime")] + use frame_support::ensure; + use frame_support::{ + traits::{OnRuntimeUpgrade, PalletInfoAccess, StorageVersion}, + weights::Weight, + }; + use frame_system::pallet_prelude::BlockNumberFor; + use pallet_broker::{CoreAssignment, CoreMask, ScheduleItem}; + #[cfg(feature = "try-runtime")] + use parity_scale_codec::Decode; + #[cfg(feature = "try-runtime")] + use parity_scale_codec::Encode; + use polkadot_parachain_primitives::primitives::IsSystem; + use primitives::{CoreIndex, Id as ParaId}; + use sp_arithmetic::traits::SaturatedConversion; + use sp_core::Get; + use sp_runtime::BoundedVec; + #[cfg(feature = "try-runtime")] + use sp_std::vec::Vec; + use sp_std::{iter, prelude::*, result}; + use xcm::v3::{ + send_xcm, Instruction, Junction, Junctions, MultiLocation, SendError, WeightLimit, Xcm, + }; + + /// Return information about a legacy lease of a parachain. + pub trait GetLegacyLease { + /// If parachain is a lease holding parachain, return the block at which the lease expires. + fn get_parachain_lease_in_blocks(para: ParaId) -> Option; + } + + /// Migrate a chain to use coretime. + /// + /// This assumes that the `Coretime` and the `AssignerCoretime` pallets are added at the same + /// time to a runtime. + pub struct MigrateToCoretime( + sp_std::marker::PhantomData<(T, SendXcm, LegacyLease)>, + ); + + impl>> + MigrateToCoretime + { + fn already_migrated() -> bool { + // We are using the assigner coretime because the coretime pallet doesn't has any + // storage data. But both pallets are introduced at the same time, so this is fine. + let name_hash = assigner_coretime::Pallet::::name_hash(); + let mut next_key = name_hash.to_vec(); + let storage_version_key = StorageVersion::storage_key::>(); + + loop { + match sp_io::storage::next_key(&next_key) { + // StorageVersion is initialized before, so we need to ingore it. + Some(key) if &key == &storage_version_key => { + next_key = key; + }, + // If there is any other key with the prefix of the pallet, + // we already have executed the migration. + Some(key) if key.starts_with(&name_hash) => { + log::info!("`MigrateToCoretime` already executed!"); + return true + }, + // Any other key/no key means that we did not yet have migrated. + None | Some(_) => return false, + } + } + } + } + + impl< + T: Config + crate::dmp::Config, + SendXcm: xcm::v3::SendXcm, + LegacyLease: GetLegacyLease>, + > OnRuntimeUpgrade for MigrateToCoretime + { + fn on_runtime_upgrade() -> Weight { + if Self::already_migrated() { + return Weight::zero() + } + + log::info!("Migrating existing parachains to coretime."); + migrate_to_coretime::() + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + if Self::already_migrated() { + return Ok(Vec::new()) + } + + let legacy_paras = paras::Parachains::::get(); + let config = >::config(); + let total_core_count = config.coretime_cores + legacy_paras.len() as u32; + + let dmp_queue_size = + crate::dmp::Pallet::::dmq_contents(T::BrokerId::get().into()).len() as u32; + + let total_core_count = total_core_count as u32; + + Ok((total_core_count, dmp_queue_size).encode()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(state: Vec) -> Result<(), sp_runtime::DispatchError> { + if state.is_empty() { + return Ok(()) + } + + log::trace!("Running post_upgrade()"); + + let (prev_core_count, prev_dmp_queue_size) = + <(u32, u32)>::decode(&mut &state[..]).unwrap(); + + let dmp_queue_size = + crate::dmp::Pallet::::dmq_contents(T::BrokerId::get().into()).len() as u32; + let new_core_count = assigner_coretime::Pallet::::session_core_count(); + ensure!(new_core_count == prev_core_count, "Total number of cores need to not change."); + ensure!( + dmp_queue_size == prev_dmp_queue_size + 1, + "There should have been enqueued one DMP message." + ); + + Ok(()) + } + } + + // Migrate to Coretime. + // + // NOTE: Also migrates coretime_cores config value in configuration::ActiveConfig. + fn migrate_to_coretime< + T: Config, + SendXcm: xcm::v3::SendXcm, + LegacyLease: GetLegacyLease>, + >() -> Weight { + let legacy_paras = paras::Pallet::::parachains(); + let legacy_count = legacy_paras.len() as u32; + let now = >::block_number(); + for (core, para_id) in legacy_paras.into_iter().enumerate() { + let r = assigner_coretime::Pallet::::assign_core( + CoreIndex(core as u32), + now, + vec![(CoreAssignment::Task(para_id.into()), PartsOf57600::FULL)], + None, + ); + if let Err(err) = r { + log::error!( + "Creating assignment for existing para failed: {:?}, error: {:?}", + para_id, + err + ); + } + } + + let config = >::config(); + // coretime_cores was on_demand_cores until now: + for on_demand in 0..config.coretime_cores { + let core = CoreIndex(legacy_count.saturating_add(on_demand as _)); + let r = assigner_coretime::Pallet::::assign_core( + core, + now, + vec![(CoreAssignment::Pool, PartsOf57600::FULL)], + None, + ); + if let Err(err) = r { + log::error!("Creating assignment for existing on-demand core, failed: {:?}", err); + } + } + let total_cores = config.coretime_cores + legacy_count; + configuration::ActiveConfig::::mutate(|c| { + c.coretime_cores = total_cores; + }); + + if let Err(err) = migrate_send_assignments_to_coretime_chain::() { + log::error!("Sending legacy chain data to coretime chain failed: {:?}", err); + } + + let single_weight = ::WeightInfo::assign_core(1); + single_weight + .saturating_mul(u64::from(legacy_count.saturating_add(config.coretime_cores))) + // Second read from sending assignments to the coretime chain. + .saturating_add(T::DbWeight::get().reads_writes(2, 1)) + } + + fn migrate_send_assignments_to_coretime_chain< + T: Config, + SendXcm: xcm::v3::SendXcm, + LegacyLease: GetLegacyLease>, + >() -> result::Result<(), SendError> { + let legacy_paras = paras::Pallet::::parachains(); + let legacy_paras_count = legacy_paras.len(); + let (system_chains, lease_holding): (Vec<_>, Vec<_>) = + legacy_paras.into_iter().partition(IsSystem::is_system); + + let reservations = system_chains.into_iter().map(|p| { + let schedule = BoundedVec::truncate_from(vec![ScheduleItem { + mask: CoreMask::complete(), + assignment: CoreAssignment::Task(p.into()), + }]); + mk_coretime_call(crate::coretime::CoretimeCalls::Reserve(schedule)) + }); + + let leases = lease_holding.into_iter().filter_map(|p| { + log::trace!(target: "coretime-migration", "Preparing sending of lease holding para {:?}", p); + let Some(valid_until) = LegacyLease::get_parachain_lease_in_blocks(p) else { + log::error!("Lease holding chain with no lease information?!"); + return None + }; + let valid_until: u32 = match valid_until.try_into() { + Ok(val) => val, + Err(_) => { + log::error!("Converting block number to u32 failed!"); + return None + }, + }; + // We assume the coretime chain set this parameter to the recommened value in RFC-1: + const TIME_SLICE_PERIOD: u32 = 80; + let round_up = if valid_until % TIME_SLICE_PERIOD > 0 { 1 } else { 0 }; + let time_slice = valid_until / TIME_SLICE_PERIOD + TIME_SLICE_PERIOD * round_up; + log::trace!(target: "coretime-migration", "Sending of lease holding para {:?}, valid_until: {:?}, time_slice: {:?}", p, valid_until, time_slice); + Some(mk_coretime_call(crate::coretime::CoretimeCalls::SetLease(p.into(), time_slice))) + }); + + let core_count: u16 = configuration::Pallet::::config().coretime_cores.saturated_into(); + let set_core_count = iter::once(mk_coretime_call( + crate::coretime::CoretimeCalls::NotifyCoreCount(core_count), + )); + + let pool = (legacy_paras_count..core_count.into()).map(|_| { + let schedule = BoundedVec::truncate_from(vec![ScheduleItem { + mask: CoreMask::complete(), + assignment: CoreAssignment::Pool, + }]); + // Reserved cores will come before lease cores, so cores will change their assignments + // when coretime chain sends us their assign_core calls -> Good test. + mk_coretime_call(crate::coretime::CoretimeCalls::Reserve(schedule)) + }); + + let message_content = iter::once(Instruction::UnpaidExecution { + weight_limit: WeightLimit::Unlimited, + check_origin: None, + }) + .chain(reservations) + .chain(pool) + .chain(leases) + .chain(set_core_count) + .collect(); + + let message = Xcm(message_content); + + send_xcm::( + MultiLocation { + parents: 0, + interior: Junctions::X1(Junction::Parachain(T::BrokerId::get())), + }, + message, + )?; + Ok(()) + } +} diff --git a/polkadot/runtime/parachains/src/coretime/mod.rs b/polkadot/runtime/parachains/src/coretime/mod.rs new file mode 100644 index 000000000000..d5b044c0631e --- /dev/null +++ b/polkadot/runtime/parachains/src/coretime/mod.rs @@ -0,0 +1,251 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Extrinsics implementing the relay chain side of the Coretime interface. +//! +//! + +use sp_std::{prelude::*, result}; + +use frame_support::{pallet_prelude::*, traits::Currency}; +use frame_system::pallet_prelude::*; +pub use pallet::*; +use pallet_broker::{CoreAssignment, CoreIndex as BrokerCoreIndex}; +use primitives::{CoreIndex, Id as ParaId}; +use sp_arithmetic::traits::SaturatedConversion; +use xcm::v3::{ + send_xcm, Instruction, Junction, Junctions, MultiLocation, OriginKind, SendXcm, Xcm, +}; + +use crate::{ + assigner_coretime::{self, PartsOf57600}, + initializer::{OnNewSession, SessionChangeNotification}, + origin::{ensure_parachain, Origin}, +}; + +mod benchmarking; +pub mod migration; + +pub trait WeightInfo { + fn request_core_count() -> Weight; + //fn request_revenue_info_at() -> Weight; + //fn credit_account() -> Weight; + fn assign_core(s: u32) -> Weight; +} + +/// A weight info that is only suitable for testing. +pub struct TestWeightInfo; + +impl WeightInfo for TestWeightInfo { + fn request_core_count() -> Weight { + Weight::MAX + } + // TODO: Add real benchmarking functionality for each of these to + // benchmarking.rs, then uncomment here and in trait definition. + /*fn request_revenue_info_at() -> Weight { + Weight::MAX + } + fn credit_account() -> Weight { + Weight::MAX + }*/ + fn assign_core(_s: u32) -> Weight { + Weight::MAX + } +} + +/// Broker pallet index on the coretime chain. Used to +/// +/// construct remote calls. The codec index must correspond to the index of `Broker` in the +/// `construct_runtime` of the coretime chain. +#[derive(Encode, Decode)] +enum BrokerRuntimePallets { + #[codec(index = 50)] + Broker(CoretimeCalls), +} + +/// Call encoding for the calls needed from the Broker pallet. +#[derive(Encode, Decode)] +enum CoretimeCalls { + #[codec(index = 1)] + Reserve(pallet_broker::Schedule), + #[codec(index = 3)] + SetLease(pallet_broker::TaskId, pallet_broker::Timeslice), + #[codec(index = 19)] + NotifyCoreCount(u16), +} + +#[frame_support::pallet] +pub mod pallet { + use crate::configuration; + + use super::*; + + #[pallet::pallet] + #[pallet::without_storage_info] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config + assigner_coretime::Config { + type RuntimeOrigin: From<::RuntimeOrigin> + + Into::RuntimeOrigin>>; + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + /// The runtime's definition of a Currency. + type Currency: Currency; + /// The ParaId of the broker system parachain. + #[pallet::constant] + type BrokerId: Get; + /// Something that provides the weight of this pallet. + type WeightInfo: WeightInfo; + type SendXcm: SendXcm; + } + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + /// The broker chain has asked for revenue information for a specific block. + RevenueInfoRequested { when: BlockNumberFor }, + /// A core has received a new assignment from the broker chain. + CoreAssigned { core: CoreIndex }, + } + + #[pallet::error] + pub enum Error { + /// The paraid making the call is not the coretime brokerage system parachain. + NotBroker, + } + + #[pallet::hooks] + impl Hooks> for Pallet {} + + #[pallet::call] + impl Pallet { + #[pallet::weight(::WeightInfo::request_core_count())] + #[pallet::call_index(1)] + pub fn request_core_count(origin: OriginFor, count: u16) -> DispatchResult { + // Ignore requests not coming from the broker parachain or root. + Self::ensure_root_or_para(origin, ::BrokerId::get().into())?; + + configuration::Pallet::::set_coretime_cores_unchecked(u32::from(count)) + } + + //// TODO Impl me! + ////#[pallet::weight(::WeightInfo::request_revenue_info_at())] + //#[pallet::call_index(2)] + //pub fn request_revenue_info_at( + // origin: OriginFor, + // _when: BlockNumberFor, + //) -> DispatchResult { + // // Ignore requests not coming from the broker parachain or root. + // Self::ensure_root_or_para(origin, ::BrokerId::get().into())?; + // Ok(()) + //} + + //// TODO Impl me! + ////#[pallet::weight(::WeightInfo::credit_account())] + //#[pallet::call_index(3)] + //pub fn credit_account( + // origin: OriginFor, + // _who: T::AccountId, + // _amount: BalanceOf, + //) -> DispatchResult { + // // Ignore requests not coming from the broker parachain or root. + // Self::ensure_root_or_para(origin, ::BrokerId::get().into())?; + // Ok(()) + //} + + /// Receive instructions from the `ExternalBrokerOrigin`, detailing how a specific core is + /// to be used. + /// + /// Parameters: + /// -`origin`: The `ExternalBrokerOrigin`, assumed to be the Broker system parachain. + /// -`core`: The core that should be scheduled. + /// -`begin`: The starting blockheight of the instruction. + /// -`assignment`: How the blockspace should be utilised. + /// -`end_hint`: An optional hint as to when this particular set of instructions will end. + // The broker pallet's `CoreIndex` definition is `u16` but on the relay chain it's `struct + // CoreIndex(u32)` + #[pallet::call_index(4)] + #[pallet::weight(::WeightInfo::assign_core(assignment.len() as u32))] + pub fn assign_core( + origin: OriginFor, + core: BrokerCoreIndex, + begin: BlockNumberFor, + assignment: Vec<(CoreAssignment, PartsOf57600)>, + end_hint: Option>, + ) -> DispatchResult { + // Ignore requests not coming from the broker parachain or root. + Self::ensure_root_or_para(origin, T::BrokerId::get().into())?; + + let core = u32::from(core).into(); + + >::assign_core(core, begin, assignment, end_hint)?; + Self::deposit_event(Event::::CoreAssigned { core }); + Ok(()) + } + } +} + +impl Pallet { + /// Ensure the origin is one of Root or the `para` itself. + fn ensure_root_or_para( + origin: ::RuntimeOrigin, + id: ParaId, + ) -> DispatchResult { + if let Ok(caller_id) = ensure_parachain(::RuntimeOrigin::from(origin.clone())) + { + // Check if matching para id... + ensure!(caller_id == id, Error::::NotBroker); + } else { + // Check if root... + ensure_root(origin.clone())?; + } + Ok(()) + } + + pub fn initializer_on_new_session(notification: &SessionChangeNotification>) { + let old_core_count = notification.prev_config.coretime_cores; + let new_core_count = notification.new_config.coretime_cores; + if new_core_count != old_core_count { + let core_count: u16 = new_core_count.saturated_into(); + let message = Xcm(vec![mk_coretime_call( + crate::coretime::CoretimeCalls::NotifyCoreCount(core_count), + )]); + if let Err(err) = send_xcm::( + MultiLocation { + parents: 0, + interior: Junctions::X1(Junction::Parachain(T::BrokerId::get())), + }, + message, + ) { + log::error!("Sending `NotifyCoreCount` to coretime chain failed: {:?}", err); + } + } + } +} + +impl OnNewSession> for Pallet { + fn on_new_session(notification: &SessionChangeNotification>) { + Self::initializer_on_new_session(notification); + } +} + +fn mk_coretime_call(call: crate::coretime::CoretimeCalls) -> Instruction<()> { + Instruction::Transact { + origin_kind: OriginKind::Native, + require_weight_at_most: Weight::from_parts(1000000000, 200000), + call: BrokerRuntimePallets::Broker(call).encode().into(), + } +} diff --git a/polkadot/runtime/parachains/src/inclusion/tests.rs b/polkadot/runtime/parachains/src/inclusion/tests.rs index 6bb731671f6f..232e65d78ed2 100644 --- a/polkadot/runtime/parachains/src/inclusion/tests.rs +++ b/polkadot/runtime/parachains/src/inclusion/tests.rs @@ -47,7 +47,7 @@ use test_helpers::{dummy_collator, dummy_collator_signature, dummy_validation_co fn default_config() -> HostConfiguration { let mut config = HostConfiguration::default(); - config.on_demand_cores = 1; + config.coretime_cores = 1; config.max_code_size = 0b100000; config.max_head_data_size = 0b100000; config.group_rotation_frequency = u32::MAX; @@ -218,7 +218,7 @@ pub(crate) fn run_to_block( } pub(crate) fn expected_bits() -> usize { - Paras::parachains().len() + Configuration::config().on_demand_cores as usize + Paras::parachains().len() + Configuration::config().coretime_cores as usize } fn default_bitfield() -> AvailabilityBitfield { diff --git a/polkadot/runtime/parachains/src/initializer.rs b/polkadot/runtime/parachains/src/initializer.rs index b4f8721be518..3c8ab7c4726f 100644 --- a/polkadot/runtime/parachains/src/initializer.rs +++ b/polkadot/runtime/parachains/src/initializer.rs @@ -60,6 +60,16 @@ pub struct SessionChangeNotification { pub session_index: SessionIndex, } +/// Inform something about a new session. +pub trait OnNewSession { + /// A new session was started. + fn on_new_session(notification: &SessionChangeNotification); +} + +impl OnNewSession for () { + fn on_new_session(_: &SessionChangeNotification) {} +} + /// Number of validators (not only parachain) in a session. pub type ValidatorSetCount = u32; @@ -120,6 +130,10 @@ pub mod pallet { type Randomness: Randomness>; /// An origin which is allowed to force updates to parachains. type ForceOrigin: EnsureOrigin<::RuntimeOrigin>; + /// Temporary hack to call `Coretime::on_new_session` on chains that support `Coretime` or + /// to disable it on the ones that don't support it. Can be removed and replaced by a simple + /// bound to `coretime::Config` once all chains support it. + type CoretimeOnNewSession: OnNewSession>; /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; } @@ -271,6 +285,7 @@ impl Pallet { T::SlashingHandler::initializer_on_new_session(session_index); dmp::Pallet::::initializer_on_new_session(¬ification, &outgoing_paras); hrmp::Pallet::::initializer_on_new_session(¬ification, &outgoing_paras); + T::CoretimeOnNewSession::on_new_session(¬ification); } /// Should be called when a new session occurs. Buffers the session notification to be applied diff --git a/polkadot/runtime/parachains/src/lib.rs b/polkadot/runtime/parachains/src/lib.rs index 2509edbee3cb..b0dc27b72863 100644 --- a/polkadot/runtime/parachains/src/lib.rs +++ b/polkadot/runtime/parachains/src/lib.rs @@ -23,10 +23,11 @@ #![cfg_attr(feature = "runtime-benchmarks", recursion_limit = "256")] #![cfg_attr(not(feature = "std"), no_std)] -pub mod assigner; +pub mod assigner_coretime; pub mod assigner_on_demand; pub mod assigner_parachains; pub mod configuration; +pub mod coretime; pub mod disputes; pub mod dmp; pub mod hrmp; diff --git a/polkadot/runtime/parachains/src/mock.rs b/polkadot/runtime/parachains/src/mock.rs index 222942922f91..fbaab1d24aaf 100644 --- a/polkadot/runtime/parachains/src/mock.rs +++ b/polkadot/runtime/parachains/src/mock.rs @@ -17,12 +17,17 @@ //! Mocks for all the traits. use crate::{ - assigner, assigner_on_demand, assigner_parachains, configuration, disputes, dmp, hrmp, + assigner_coretime, assigner_on_demand, assigner_parachains, configuration, coretime, disputes, + dmp, hrmp, inclusion::{self, AggregateMessageOrigin, UmpQueueId}, initializer, origin, paras, paras::ParaKind, - paras_inherent, scheduler, session_info, shared, ParaId, + paras_inherent, scheduler, + scheduler::common::{AssignmentProvider, AssignmentProviderConfig}, + session_info, shared, ParaId, }; +use frame_support::pallet_prelude::*; +use primitives::CoreIndex; use frame_support::{ assert_ok, derive_impl, parameter_types, @@ -45,7 +50,9 @@ use sp_runtime::{ transaction_validity::TransactionPriority, BuildStorage, FixedU128, Perbill, Permill, }; +use sp_std::collections::vec_deque::VecDeque; use std::{cell::RefCell, collections::HashMap}; +use xcm::v3::{MultiAssets, MultiLocation, SendError, SendResult, SendXcm, Xcm, XcmHash}; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlockU32; @@ -62,9 +69,11 @@ frame_support::construct_runtime!( ParaInclusion: inclusion, ParaInherent: paras_inherent, Scheduler: scheduler, - Assigner: assigner, - OnDemandAssigner: assigner_on_demand, + MockAssigner: mock_assigner, ParachainsAssigner: assigner_parachains, + OnDemandAssigner: assigner_on_demand, + CoretimeAssigner: assigner_coretime, + Coretime: coretime, Initializer: initializer, Dmp: dmp, Hrmp: hrmp, @@ -178,6 +187,7 @@ impl crate::initializer::Config for Test { type Randomness = TestRandomness; type ForceOrigin = frame_system::EnsureRoot; type WeightInfo = (); + type CoretimeOnNewSession = Coretime; } impl crate::configuration::Config for Test { @@ -217,6 +227,7 @@ impl crate::paras::Config for Test { type QueueFootprinter = ParaInclusion; type NextSessionRotation = TestNextSessionRotation; type OnNewHead = (); + type AssignCoretime = (); } impl crate::dmp::Config for Test {} @@ -288,7 +299,7 @@ impl crate::disputes::SlashingHandler for Test { } impl crate::scheduler::Config for Test { - type AssignmentProvider = Assigner; + type AssignmentProvider = MockAssigner; } pub struct TestMessageQueueWeight; @@ -342,17 +353,12 @@ impl pallet_message_queue::Config for Test { type ServiceWeight = MessageQueueServiceWeight; } -impl assigner::Config for Test { - type ParachainsAssignmentProvider = ParachainsAssigner; - type OnDemandAssignmentProvider = OnDemandAssigner; -} - -impl assigner_parachains::Config for Test {} - parameter_types! { pub const OnDemandTrafficDefaultValue: FixedU128 = FixedU128::from_u32(1); } +impl assigner_parachains::Config for Test {} + impl assigner_on_demand::Config for Test { type RuntimeEvent = RuntimeEvent; type Currency = Balances; @@ -360,6 +366,37 @@ impl assigner_on_demand::Config for Test { type WeightInfo = crate::assigner_on_demand::TestWeightInfo; } +impl assigner_coretime::Config for Test {} + +parameter_types! { + pub const BrokerId: u32 = 10u32; +} + +impl coretime::Config for Test { + type RuntimeOrigin = RuntimeOrigin; + type RuntimeEvent = RuntimeEvent; + type Currency = pallet_balances::Pallet; + type BrokerId = BrokerId; + type WeightInfo = crate::coretime::TestWeightInfo; + type SendXcm = DummyXcmSender; +} + +pub struct DummyXcmSender; +impl SendXcm for DummyXcmSender { + type Ticket = (); + fn validate( + _: &mut Option, + _: &mut Option>, + ) -> SendResult { + Ok(((), MultiAssets::new())) + } + + /// Actually carry out the delivery operation for a previously validated message sending. + fn deliver(_ticket: Self::Ticket) -> Result { + Ok([0u8; 32]) + } +} + impl crate::inclusion::Config for Test { type WeightInfo = (); type RuntimeEvent = RuntimeEvent; @@ -390,6 +427,104 @@ impl ValidatorSetWithIdentification for MockValidatorSet { type IdentificationOf = FoolIdentificationOf; } +/// A mock assigner which acts as the scheduler's `AssignmentProvider` for tests. The mock +/// assigner provides bare minimum functionality to test scheduler internals. Since they +/// have no direct effect on scheduler state, AssignmentProvider functions such as +/// `push_back_assignment` can be left empty. +pub mod mock_assigner { + use crate::scheduler::common::Assignment; + + use super::*; + pub use pallet::*; + + #[frame_support::pallet] + pub mod pallet { + use super::*; + + #[pallet::pallet] + #[pallet::without_storage_info] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config + configuration::Config + paras::Config {} + + #[pallet::storage] + pub(super) type MockAssignmentQueue = + StorageValue<_, VecDeque, ValueQuery>; + + #[pallet::storage] + pub(super) type MockProviderConfig = + StorageValue<_, AssignmentProviderConfig, OptionQuery>; + + #[pallet::storage] + pub(super) type MockCoreCount = StorageValue<_, u32, OptionQuery>; + } + + impl Pallet { + /// Adds a claim to the `MockAssignmentQueue` this claim can later be popped by the + /// scheduler when filling the claim queue for tests. + pub fn add_test_assignment(assignment: Assignment) { + MockAssignmentQueue::::mutate(|queue| queue.push_back(assignment)); + } + + // This configuration needs to be customized to service `get_provider_config` in + // scheduler tests. + pub fn set_assignment_provider_config(config: AssignmentProviderConfig) { + MockProviderConfig::::set(Some(config)); + } + + // Allows for customized core count in scheduler tests, rather than a core count + // derived from on-demand config + parachain count. + pub fn set_core_count(count: u32) { + MockCoreCount::::set(Some(count)); + } + } + + impl AssignmentProvider for Pallet { + // With regards to popping_assignments, the scheduler just needs to be tested under + // the following two conditions: + // 1. An assignment is provided + // 2. No assignment is provided + // A simple assignment queue populated to fit each test fulfills these needs. + fn pop_assignment_for_core(_core_idx: CoreIndex) -> Option { + let mut queue: VecDeque = MockAssignmentQueue::::get(); + let front = queue.pop_front(); + // Write changes to storage. + MockAssignmentQueue::::set(queue); + front + } + + // We don't care about core affinity in the test assigner + fn report_processed(_assignment: Assignment) {} + + // The results of this are tested in assigner_on_demand tests. No need to represent it + // in the mock assigner. + fn push_back_assignment(_assignment: Assignment) {} + + // Gets the provider config we set earlier using `set_assignment_provider_config`, falling + // back to the on demand parachain configuration if none was set. + fn get_provider_config(_core_idx: CoreIndex) -> AssignmentProviderConfig { + match MockProviderConfig::::get() { + Some(config) => config, + None => AssignmentProviderConfig { + max_availability_timeouts: 1, + ttl: BlockNumber::from(5u32), + }, + } + } + #[cfg(any(feature = "runtime-benchmarks", test))] + fn get_mock_assignment(_: CoreIndex, para_id: ParaId) -> Assignment { + Assignment::Bulk(para_id) + } + + fn session_core_count() -> u32 { + MockCoreCount::::get().unwrap_or(5) + } + } +} + +impl mock_assigner::pallet::Config for Test {} + pub struct FoolIdentificationOf; impl sp_runtime::traits::Convert> for FoolIdentificationOf { fn convert(_: AccountId) -> Option<()> { diff --git a/polkadot/runtime/parachains/src/paras/mod.rs b/polkadot/runtime/parachains/src/paras/mod.rs index ef9dfedd7350..e97df8e4a2b3 100644 --- a/polkadot/runtime/parachains/src/paras/mod.rs +++ b/polkadot/runtime/parachains/src/paras/mod.rs @@ -506,6 +506,21 @@ impl OnNewHead for Tuple { } } +/// Assign coretime to some parachain. +/// +/// This assigns coretime to a parachain without using the coretime chain. Thus, this should only be +/// used for testing purposes. +pub trait AssignCoretime { + /// ONLY USE FOR TESTING OR GENESIS. + fn assign_coretime(id: ParaId) -> DispatchResult; +} + +impl AssignCoretime for () { + fn assign_coretime(_: ParaId) -> DispatchResult { + Ok(()) + } +} + pub trait WeightInfo { fn force_set_current_code(c: u32) -> Weight; fn force_set_current_head(s: u32) -> Weight; @@ -605,6 +620,13 @@ pub mod pallet { /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; + + /// Runtime hook for assigning coretime for a given parachain. + /// + /// This is only used at genesis or by root. + /// + /// TODO: Remove once coretime is the standard accross all chains. + type AssignCoretime: AssignCoretime; } #[pallet::event] @@ -838,6 +860,8 @@ pub mod pallet { panic!("empty validation code is not allowed in genesis"); } Pallet::::initialize_para_now(&mut parachains, *id, genesis_args); + T::AssignCoretime::assign_coretime(*id) + .expect("Assigning coretime works at genesis; qed"); } // parachains are flushed on drop } diff --git a/polkadot/runtime/parachains/src/paras_inherent/benchmarking.rs b/polkadot/runtime/parachains/src/paras_inherent/benchmarking.rs index 4509c5c3cc72..0f6b23ae1b39 100644 --- a/polkadot/runtime/parachains/src/paras_inherent/benchmarking.rs +++ b/polkadot/runtime/parachains/src/paras_inherent/benchmarking.rs @@ -18,7 +18,9 @@ use super::*; use crate::{inclusion, ParaId}; use frame_benchmarking::{benchmarks, impl_benchmark_test_suite}; use frame_system::RawOrigin; -use sp_std::collections::btree_map::BTreeMap; +use sp_std::{cmp::min, collections::btree_map::BTreeMap}; + +use primitives::v6::GroupIndex; use crate::builder::BenchBuilder; @@ -116,7 +118,9 @@ benchmarks! { // There is 1 backed, assert_eq!(benchmark.backed_candidates.len(), 1); // with `v` validity votes. - assert_eq!(benchmark.backed_candidates.get(0).unwrap().validity_votes.len(), v as usize); + // let votes = v as usize; + let votes = min(scheduler::Pallet::::group_validators(GroupIndex::from(0)).unwrap().len(), v as usize); + assert_eq!(benchmark.backed_candidates.get(0).unwrap().validity_votes.len(), votes); benchmark.bitfields.clear(); benchmark.disputes.clear(); @@ -138,7 +142,7 @@ benchmarks! { let descriptor = backing_validators.0.descriptor(); assert_eq!(ParaId::from(para_id), descriptor.para_id); assert_eq!(header.hash(), descriptor.relay_parent); - assert_eq!(backing_validators.1.len(), v as usize); + assert_eq!(backing_validators.1.len(), votes); } assert_eq!( @@ -167,11 +171,14 @@ benchmarks! { let mut benchmark = scenario.data.clone(); + // let votes = BenchBuilder::::fallback_min_validity_votes() as usize; + let votes = min(scheduler::Pallet::::group_validators(GroupIndex::from(0)).unwrap().len(), BenchBuilder::::fallback_min_validity_votes() as usize); + // There is 1 backed assert_eq!(benchmark.backed_candidates.len(), 1); assert_eq!( - benchmark.backed_candidates.get(0).unwrap().validity_votes.len() as u32, - BenchBuilder::::fallback_min_validity_votes() + benchmark.backed_candidates.get(0).unwrap().validity_votes.len(), + votes, ); benchmark.bitfields.clear(); @@ -197,8 +204,8 @@ benchmarks! { assert_eq!(ParaId::from(para_id), descriptor.para_id); assert_eq!(header.hash(), descriptor.relay_parent); assert_eq!( - backing_validators.1.len() as u32, - BenchBuilder::::fallback_min_validity_votes() + backing_validators.1.len(), + votes, ); } diff --git a/polkadot/runtime/parachains/src/paras_inherent/mod.rs b/polkadot/runtime/parachains/src/paras_inherent/mod.rs index 8e918d35d5ff..8c33199c0923 100644 --- a/polkadot/runtime/parachains/src/paras_inherent/mod.rs +++ b/polkadot/runtime/parachains/src/paras_inherent/mod.rs @@ -548,7 +548,7 @@ impl Pallet { let disputed_bitfield = create_disputed_bitfield(expected_bits, freed_disputed.keys()); if !freed_disputed.is_empty() { - >::update_claimqueue(freed_disputed.clone(), now); + >::free_cores_and_fill_claimqueue(freed_disputed.clone(), now); } let bitfields = sanitize_bitfields::( @@ -580,7 +580,7 @@ impl Pallet { let freed = collect_all_freed_cores::(freed_concluded.iter().cloned()); - >::update_claimqueue(freed, now); + >::free_cores_and_fill_claimqueue(freed, now); let scheduled = >::scheduled_paras() .map(|(core_idx, para_id)| (para_id, core_idx)) .collect(); diff --git a/polkadot/runtime/parachains/src/paras_inherent/tests.rs b/polkadot/runtime/parachains/src/paras_inherent/tests.rs index 4fc60792e346..e62d1cb68ffe 100644 --- a/polkadot/runtime/parachains/src/paras_inherent/tests.rs +++ b/polkadot/runtime/parachains/src/paras_inherent/tests.rs @@ -25,7 +25,8 @@ mod enter { use super::*; use crate::{ builder::{Bench, BenchBuilder}, - mock::{new_test_ext, BlockLength, BlockWeights, MockGenesisConfig, Test}, + mock::{mock_assigner, new_test_ext, BlockLength, BlockWeights, MockGenesisConfig, Test}, + scheduler::common::Assignment, }; use assert_matches::assert_matches; use frame_support::assert_ok; @@ -39,6 +40,7 @@ mod enter { backed_and_concluding: BTreeMap, num_validators_per_core: u32, code_upgrade: Option, + fill_claimqueue: bool, } fn make_inherent_data( @@ -48,6 +50,7 @@ mod enter { backed_and_concluding, num_validators_per_core, code_upgrade, + fill_claimqueue, }: TestConfig, ) -> Bench { let builder = BenchBuilder::::new() @@ -58,7 +61,15 @@ mod enter { .set_max_validators_per_core(num_validators_per_core) .set_dispute_statements(dispute_statements) .set_backed_and_concluding_cores(backed_and_concluding) - .set_dispute_sessions(&dispute_sessions[..]); + .set_dispute_sessions(&dispute_sessions[..]) + .set_fill_claimqueue(fill_claimqueue); + + // Setup some assignments as needed: + mock_assigner::Pallet::::set_core_count(builder.max_cores()); + for core_index in 0..builder.max_cores() { + // Core index == para_id in this case + mock_assigner::Pallet::::add_test_assignment(Assignment::Bulk(core_index.into())); + } if let Some(code_size) = code_upgrade { builder.set_code_upgrade(code_size).build() @@ -88,6 +99,7 @@ mod enter { backed_and_concluding, num_validators_per_core: 1, code_upgrade: None, + fill_claimqueue: false, }); // We expect the scenario to have cores 0 & 1 with pending availability. The backed @@ -238,6 +250,7 @@ mod enter { backed_and_concluding, num_validators_per_core: 5, code_upgrade: None, + fill_claimqueue: false, }); let expected_para_inherent_data = scenario.data.clone(); @@ -308,6 +321,7 @@ mod enter { backed_and_concluding, num_validators_per_core: 6, code_upgrade: None, + fill_claimqueue: false, }); let expected_para_inherent_data = scenario.data.clone(); @@ -376,6 +390,7 @@ mod enter { backed_and_concluding, num_validators_per_core: 4, code_upgrade: None, + fill_claimqueue: false, }); let expected_para_inherent_data = scenario.data.clone(); @@ -460,6 +475,7 @@ mod enter { backed_and_concluding, num_validators_per_core: 5, code_upgrade: None, + fill_claimqueue: false, }); let expected_para_inherent_data = scenario.data.clone(); @@ -544,6 +560,7 @@ mod enter { backed_and_concluding, num_validators_per_core: 5, code_upgrade: None, + fill_claimqueue: false, }); let expected_para_inherent_data = scenario.data.clone(); @@ -627,6 +644,7 @@ mod enter { backed_and_concluding, num_validators_per_core: 5, code_upgrade: None, + fill_claimqueue: false, }); let expected_para_inherent_data = scenario.data.clone(); @@ -666,15 +684,9 @@ mod enter { // * 3 disputes. assert_eq!(limit_inherent_data.disputes.len(), 2); - assert_ok!(Pallet::::enter( - frame_system::RawOrigin::None.into(), - limit_inherent_data, - )); - - // TODO [now]: this assertion fails with async backing runtime. assert_eq!( - // The length of this vec is equal to the number of candidates, so we know our 2 - // backed candidates did not get filtered out + // The length of this vec is equal to the number of candidates, so we know 1 + // candidate got filtered out Pallet::::on_chain_votes().unwrap().backing_validators_per_candidate.len(), 1 ); @@ -684,6 +696,11 @@ mod enter { Pallet::::on_chain_votes().unwrap().session, 2 ); + + assert_ok!(Pallet::::enter( + frame_system::RawOrigin::None.into(), + limit_inherent_data, + )); }); } @@ -713,6 +730,7 @@ mod enter { backed_and_concluding, num_validators_per_core: 5, code_upgrade: None, + fill_claimqueue: false, }); let expected_para_inherent_data = scenario.data.clone(); @@ -778,6 +796,7 @@ mod enter { backed_and_concluding, num_validators_per_core: 5, code_upgrade: None, + fill_claimqueue: false, }); let expected_para_inherent_data = scenario.data.clone(); @@ -841,6 +860,7 @@ mod enter { backed_and_concluding, num_validators_per_core: 5, code_upgrade: None, + fill_claimqueue: false, }); let expected_para_inherent_data = scenario.data.clone(); @@ -905,6 +925,7 @@ mod enter { backed_and_concluding, num_validators_per_core: 5, code_upgrade: None, + fill_claimqueue: false, }); let expected_para_inherent_data = scenario.data.clone(); diff --git a/polkadot/runtime/parachains/src/runtime_api_impl/v7.rs b/polkadot/runtime/parachains/src/runtime_api_impl/v7.rs index 4d0bbc6a8960..b3a060e1cb8a 100644 --- a/polkadot/runtime/parachains/src/runtime_api_impl/v7.rs +++ b/polkadot/runtime/parachains/src/runtime_api_impl/v7.rs @@ -62,7 +62,7 @@ pub fn availability_cores() -> Vec>::update_claimqueue(Vec::new(), now); + >::free_cores_and_fill_claimqueue(Vec::new(), now); let time_out_for = >::availability_timeout_predicate(); diff --git a/polkadot/runtime/parachains/src/scheduler.rs b/polkadot/runtime/parachains/src/scheduler.rs index dea84c69f52d..08ce656b2b28 100644 --- a/polkadot/runtime/parachains/src/scheduler.rs +++ b/polkadot/runtime/parachains/src/scheduler.rs @@ -65,7 +65,7 @@ pub mod migration; pub mod pallet { use super::*; - const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + const STORAGE_VERSION: StorageVersion = StorageVersion::new(2); #[pallet::pallet] #[pallet::without_storage_info] @@ -99,15 +99,14 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn availability_cores)] pub(crate) type AvailabilityCores = - StorageValue<_, Vec>>, ValueQuery>; + StorageValue<_, Vec>, ValueQuery>; /// Representation of a core in `AvailabilityCores`. /// /// This is not to be confused with `CoreState` which is an enriched variant of this and exposed /// to the node side. It also provides information about scheduled/upcoming assignments for /// example and is computed on the fly in the `availability_cores` runtime call. - #[derive(Clone, Encode, Decode, TypeInfo, RuntimeDebug)] - #[cfg_attr(feature = "std", derive(PartialEq))] + #[derive(Encode, Decode, TypeInfo, RuntimeDebug, PartialEq)] pub enum CoreOccupied { /// No candidate is waiting availability on this core right now (the core is not occupied). Free, @@ -115,6 +114,9 @@ pub mod pallet { Paras(ParasEntry), } + /// Conveninece type alias for `CoreOccupied`. + pub type CoreOccupiedType = CoreOccupied>; + impl CoreOccupied { /// Is core free? pub fn is_free(&self) -> bool { @@ -149,16 +151,13 @@ pub mod pallet { /// a block. Runtime APIs should be used to determine scheduled cores/ for the upcoming block. #[pallet::storage] #[pallet::getter(fn claimqueue)] - pub(crate) type ClaimQueue = StorageValue< - _, - BTreeMap>>>>, - ValueQuery, - >; + pub(crate) type ClaimQueue = + StorageValue<_, BTreeMap>>, ValueQuery>; /// Assignments as tracked in the claim queue. - #[derive(Clone, Encode, Decode, TypeInfo, PartialEq, RuntimeDebug)] - pub struct ParasEntry { - /// The underlying `Assignment` + #[derive(Encode, Decode, TypeInfo, RuntimeDebug, PartialEq, Clone)] + pub struct ParasEntry { + /// The underlying [`Assignment`]. pub assignment: Assignment, /// The number of times the entry has timed out in availability already. pub availability_timeouts: u32, @@ -169,37 +168,18 @@ pub mod pallet { pub ttl: N, } - impl ParasEntry { - /// Return `Id` from the underlying `Assignment`. - pub fn para_id(&self) -> ParaId { - self.assignment.para_id - } + /// Convenience type declaration for `ParasEntry`. + pub type ParasEntryType = ParasEntry>; + impl ParasEntry { /// Create a new `ParasEntry`. pub fn new(assignment: Assignment, now: N) -> Self { ParasEntry { assignment, availability_timeouts: 0, ttl: now } } - } - /// How a core is mapped to a backing group and a `ParaId` - #[derive(Clone, Encode, Decode, PartialEq, TypeInfo)] - #[cfg_attr(feature = "std", derive(Debug))] - pub struct CoreAssignment { - /// The core that is assigned. - pub core: CoreIndex, - /// The para id and accompanying information needed to collate and back a parablock. - pub paras_entry: ParasEntry, - } - - impl CoreAssignment { - /// Returns the [`ParaId`] of the assignment. + /// Return `Id` from the underlying `Assignment`. pub fn para_id(&self) -> ParaId { - self.paras_entry.para_id() - } - - /// Returns the inner [`ParasEntry`] of the assignment. - pub fn to_paras_entry(self) -> ParasEntry { - self.paras_entry + self.assignment.para_id() } } @@ -219,8 +199,6 @@ pub mod pallet { } type PositionInClaimqueue = u32; -type TimedoutParas = BTreeMap>>; -type ConcludedParas = BTreeMap; impl Pallet { /// Called by the initializer to initialize the scheduler pallet. @@ -253,7 +231,7 @@ impl Pallet { ); AvailabilityCores::::mutate(|cores| { - cores.resize(n_cores as _, CoreOccupied::Free); + cores.resize_with(n_cores as _, || CoreOccupied::Free); }); // shuffle validators into groups. @@ -298,9 +276,8 @@ impl Pallet { /// with the reason for them being freed. Returns a tuple of concluded and timedout paras. fn free_cores( just_freed_cores: impl IntoIterator, - ) -> (ConcludedParas, TimedoutParas) { - let mut timedout_paras: BTreeMap>> = - BTreeMap::new(); + ) -> (BTreeMap, BTreeMap>) { + let mut timedout_paras: BTreeMap> = BTreeMap::new(); let mut concluded_paras = BTreeMap::new(); AvailabilityCores::::mutate(|cores| { @@ -310,21 +287,22 @@ impl Pallet { .into_iter() .filter(|(freed_index, _)| (freed_index.0 as usize) < c_len) .for_each(|(freed_index, freed_reason)| { - match &cores[freed_index.0 as usize] { + match sp_std::mem::replace( + &mut cores[freed_index.0 as usize], + CoreOccupied::Free, + ) { CoreOccupied::Free => {}, CoreOccupied::Paras(entry) => { match freed_reason { FreedReason::Concluded => { - concluded_paras.insert(freed_index, entry.para_id()); + concluded_paras.insert(freed_index, entry.assignment); }, FreedReason::TimedOut => { - timedout_paras.insert(freed_index, entry.clone()); + timedout_paras.insert(freed_index, entry); }, }; }, }; - - cores[freed_index.0 as usize] = CoreOccupied::Free; }) }); @@ -379,30 +357,36 @@ impl Pallet { for (idx, _) in (0u32..).zip(availability_cores) { let core_idx = CoreIndex(idx); if let Some(core_claimqueue) = cq.get_mut(&core_idx) { - let mut dropped_claims: Vec> = vec![]; - core_claimqueue.retain(|maybe_entry| { - if let Some(entry) = maybe_entry { + let mut i = 0; + let mut num_dropped = 0; + while i < core_claimqueue.len() { + let maybe_dropped = if let Some(entry) = core_claimqueue.get(i) { if entry.ttl < now { - dropped_claims.push(Some(entry.para_id())); - return false + core_claimqueue.remove(i) + } else { + None } + } else { + None + }; + + if let Some(dropped) = maybe_dropped { + num_dropped += 1; + T::AssignmentProvider::report_processed(dropped.assignment); + } else { + i += 1; } - true - }); - - // For all claims dropped due to TTL, attempt to pop a new entry to - // the back of the claimqueue. - for drop in dropped_claims { - match T::AssignmentProvider::pop_assignment_for_core(core_idx, drop) { - Some(assignment) => { - let AssignmentProviderConfig { ttl, .. } = - T::AssignmentProvider::get_provider_config(core_idx); - core_claimqueue.push_back(Some(ParasEntry::new( - assignment.clone(), - now + ttl, - ))); - }, - None => (), + } + + for _ in 0..num_dropped { + // For all claims dropped due to TTL, attempt to pop a new entry to + // the back of the claimqueue. + if let Some(assignment) = + T::AssignmentProvider::pop_assignment_for_core(core_idx) + { + let AssignmentProviderConfig { ttl, .. } = + T::AssignmentProvider::get_provider_config(core_idx); + core_claimqueue.push_back(ParasEntry::new(assignment, now + ttl)); } } } @@ -514,14 +498,12 @@ impl Pallet { /// Return the next thing that will be scheduled on this core assuming it is currently /// occupied and the candidate occupying it became available. pub(crate) fn next_up_on_available(core: CoreIndex) -> Option { - ClaimQueue::::get().get(&core).and_then(|a| { - a.iter() - .find_map(|e| e.as_ref()) - .map(|pe| Self::paras_entry_to_scheduled_core(pe)) - }) + ClaimQueue::::get() + .get(&core) + .and_then(|a| a.front().map(|pe| Self::paras_entry_to_scheduled_core(pe))) } - fn paras_entry_to_scheduled_core(pe: &ParasEntry>) -> ScheduledCore { + fn paras_entry_to_scheduled_core(pe: &ParasEntryType) -> ScheduledCore { ScheduledCore { para_id: pe.para_id(), collator: None } } @@ -552,35 +534,33 @@ impl Pallet { /// Pushes occupied cores to the assignment provider. fn push_occupied_cores_to_assignment_provider() { AvailabilityCores::::mutate(|cores| { - for (core_idx, core) in cores.iter_mut().enumerate() { - match core { + for core in cores.iter_mut() { + match sp_std::mem::replace(core, CoreOccupied::Free) { CoreOccupied::Free => continue, CoreOccupied::Paras(entry) => { - let core_idx = CoreIndex::from(core_idx as u32); - Self::maybe_push_assignment(core_idx, entry.clone()); + Self::maybe_push_assignment(entry); }, } - *core = CoreOccupied::Free; } }); } // on new session fn push_claimqueue_items_to_assignment_provider() { - for (core_idx, core_claimqueue) in ClaimQueue::::take() { + for (_, claim_queue) in ClaimQueue::::take() { // Push back in reverse order so that when we pop from the provider again, // the entries in the claimqueue are in the same order as they are right now. - for para_entry in core_claimqueue.into_iter().flatten().rev() { - Self::maybe_push_assignment(core_idx, para_entry); + for para_entry in claim_queue.into_iter().rev() { + Self::maybe_push_assignment(para_entry); } } } /// Push assignments back to the provider on session change unless the paras /// timed out on availability before. - fn maybe_push_assignment(core_idx: CoreIndex, pe: ParasEntry>) { + fn maybe_push_assignment(pe: ParasEntryType) { if pe.availability_timeouts == 0 { - T::AssignmentProvider::push_assignment_for_core(core_idx, pe.assignment); + T::AssignmentProvider::push_back_assignment(pe.assignment); } } @@ -591,31 +571,8 @@ impl Pallet { >::config().scheduling_lookahead } - /// Updates the claimqueue by moving it to the next paras and filling empty spots with new - /// paras. - pub(crate) fn update_claimqueue( - just_freed_cores: impl IntoIterator, - now: BlockNumberFor, - ) { - Self::move_claimqueue_forward(); - Self::free_cores_and_fill_claimqueue(just_freed_cores, now) - } - - /// Moves all elements in the claimqueue forward. - fn move_claimqueue_forward() { - let mut cq = ClaimQueue::::get(); - for core_queue in cq.values_mut() { - // First pop the finished claims from the front. - if let Some(None) = core_queue.front() { - core_queue.pop_front(); - } - } - - ClaimQueue::::set(cq); - } - /// Frees cores and fills the free claimqueue spots by popping from the `AssignmentProvider`. - fn free_cores_and_fill_claimqueue( + pub fn free_cores_and_fill_claimqueue( just_freed_cores: impl IntoIterator, now: BlockNumberFor, ) { @@ -651,19 +608,19 @@ impl Pallet { } else { // Consider timed out assignments for on demand parachains as concluded for // the assignment provider - let ret = concluded_paras.insert(core_idx, entry.para_id()); + let ret = concluded_paras.insert(core_idx, entry.assignment); debug_assert!(ret.is_none()); } } - // We consider occupied cores to be part of the claimqueue + if let Some(concluded_para) = concluded_paras.remove(&core_idx) { + T::AssignmentProvider::report_processed(concluded_para); + } + // We consider occupied cores to be part of the claimqueue let n_lookahead_used = cq.get(&core_idx).map_or(0, |v| v.len() as u32) + if Self::is_core_occupied(core_idx) { 1 } else { 0 }; for _ in n_lookahead_used..n_lookahead { - let concluded_para = concluded_paras.remove(&core_idx); - if let Some(assignment) = - T::AssignmentProvider::pop_assignment_for_core(core_idx, concluded_para) - { + if let Some(assignment) = T::AssignmentProvider::pop_assignment_for_core(core_idx) { Self::add_to_claimqueue(core_idx, ParasEntry::new(assignment, now + ttl)); } } @@ -680,9 +637,9 @@ impl Pallet { } } - fn add_to_claimqueue(core_idx: CoreIndex, pe: ParasEntry>) { + fn add_to_claimqueue(core_idx: CoreIndex, pe: ParasEntryType) { ClaimQueue::::mutate(|la| { - la.entry(core_idx).or_default().push_back(Some(pe)); + la.entry(core_idx).or_default().push_back(pe); }); } @@ -690,19 +647,16 @@ impl Pallet { fn remove_from_claimqueue( core_idx: CoreIndex, para_id: ParaId, - ) -> Result<(PositionInClaimqueue, ParasEntry>), &'static str> { + ) -> Result<(PositionInClaimqueue, ParasEntryType), &'static str> { ClaimQueue::::mutate(|cq| { let core_claims = cq.get_mut(&core_idx).ok_or("core_idx not found in lookahead")?; let pos = core_claims .iter() - .position(|a| a.as_ref().map_or(false, |pe| pe.para_id() == para_id)) + .position(|pe| pe.para_id() == para_id) .ok_or("para id not found at core_idx lookahead")?; - let pe = core_claims - .remove(pos) - .ok_or("remove returned None")? - .ok_or("Element in Claimqueue was None.")?; + let pe = core_claims.remove(pos).ok_or("remove returned None")?; Ok((pos as u32, pe)) }) @@ -710,16 +664,10 @@ impl Pallet { /// Paras scheduled next in the claim queue. pub(crate) fn scheduled_paras() -> impl Iterator { - Self::scheduled_entries().map(|(core_idx, e)| (core_idx, e.assignment.para_id)) - } - - /// Internal access to entries at the top of the claim queue. - fn scheduled_entries() -> impl Iterator>)> { let claimqueue = ClaimQueue::::get(); - claimqueue .into_iter() - .filter_map(|(core_idx, v)| v.front().cloned().flatten().map(|e| (core_idx, e))) + .filter_map(|(core_idx, v)| v.front().map(|e| (core_idx, e.assignment.para_id()))) } #[cfg(any(feature = "runtime-benchmarks", test))] diff --git a/polkadot/runtime/parachains/src/scheduler/common.rs b/polkadot/runtime/parachains/src/scheduler/common.rs index 316e8e3b760c..2eb73385803c 100644 --- a/polkadot/runtime/parachains/src/scheduler/common.rs +++ b/polkadot/runtime/parachains/src/scheduler/common.rs @@ -16,29 +16,39 @@ //! Common traits and types used by the scheduler and assignment providers. -use frame_support::pallet_prelude::*; -use primitives::{CoreIndex, Id as ParaId}; use scale_info::TypeInfo; -use sp_std::prelude::*; +use sp_runtime::{ + codec::{Decode, Encode}, + RuntimeDebug, +}; -// Only used to link to configuration documentation. -#[allow(unused)] -use crate::configuration::HostConfiguration; +use primitives::{CoreIndex, Id as ParaId}; -/// An assignment for a parachain scheduled to be backed and included in a relay chain block. -#[derive(Clone, Encode, Decode, PartialEq, TypeInfo, RuntimeDebug)] -pub struct Assignment { - /// Assignment's ParaId - pub para_id: ParaId, +/// Assignment (ParaId -> CoreIndex). +#[derive(Encode, Decode, TypeInfo, RuntimeDebug, Clone, PartialEq)] +pub enum Assignment { + /// A pool assignment. + Pool { + /// The assigned para id. + para_id: ParaId, + /// The core index the para got assigned to. + core_index: CoreIndex, + }, + /// A bulk assignment. + Bulk(ParaId), } impl Assignment { - /// Create a new `Assignment`. - pub fn new(para_id: ParaId) -> Self { - Self { para_id } + /// Returns the [`ParaId`] this assignment is associated to. + pub fn para_id(&self) -> ParaId { + match self { + Self::Pool { para_id, .. } => *para_id, + Self::Bulk(para_id) => *para_id, + } } } +#[derive(Encode, Decode, TypeInfo)] /// A set of variables required by the scheduler in order to operate. pub struct AssignmentProviderConfig { /// How many times a collation can time out on availability. @@ -51,22 +61,42 @@ pub struct AssignmentProviderConfig { } pub trait AssignmentProvider { - /// How many cores are allocated to this provider. - fn session_core_count() -> u32; - /// Pops an [`Assignment`] from the provider for a specified [`CoreIndex`]. - /// The `concluded_para` field makes the caller report back to the provider - /// which [`ParaId`] it processed last on the supplied [`CoreIndex`]. - fn pop_assignment_for_core( - core_idx: CoreIndex, - concluded_para: Option, - ) -> Option; - - /// Push back an already popped assignment. Intended for provider implementations - /// that need to be able to keep track of assignments over session boundaries, - /// such as the on demand assignment provider. - fn push_assignment_for_core(core_idx: CoreIndex, assignment: Assignment); + /// + /// This is where assignments come into existance. + fn pop_assignment_for_core(core_idx: CoreIndex) -> Option; + + /// A previously popped `Assignment` has been fully processed. + /// + /// Report back to the assignment provider that an assignment is done and no longer present in + /// the scheduler. + /// + /// This is one way of the life of an assignment coming to an end. + fn report_processed(assignment: Assignment); + + /// Push back a previously popped assignment. + /// + /// If the assignment could not be processed within the current session, it can be pushed back + /// to the assignment provider in order to be poppped again later. + /// + /// This is the second way the life of an assignment can come to an end. + fn push_back_assignment(assignment: Assignment); /// Returns a set of variables needed by the scheduler fn get_provider_config(core_idx: CoreIndex) -> AssignmentProviderConfig; + + /// Push some assignment for mocking/benchmarks purposes. + /// + /// Useful for benchmarks and testing. The returned assignment is "valid" and can if need be + /// passed into `report_processed` for example. + #[cfg(any(feature = "runtime-benchmarks", test))] + fn get_mock_assignment(core_idx: CoreIndex, para_id: ParaId) -> Assignment; + + /// How many cores are allocated to this provider. + /// + /// As the name suggests the core count has to be session buffered: + /// + /// - Core count has to be predetermined for the next session in the current session. + /// - Core count must not change during a session. + fn session_core_count() -> u32; } diff --git a/polkadot/runtime/parachains/src/scheduler/migration.rs b/polkadot/runtime/parachains/src/scheduler/migration.rs index bb9a647e955c..4c0a07d73674 100644 --- a/polkadot/runtime/parachains/src/scheduler/migration.rs +++ b/polkadot/runtime/parachains/src/scheduler/migration.rs @@ -22,9 +22,18 @@ use frame_support::{ traits::OnRuntimeUpgrade, weights::Weight, }; +/// Old/legacy assignment representation (v0). +/// +/// `Assignment` used to be a concrete type with the same layout V0Assignment, idential on all +/// assignment providers. This can be removed once storage has been migrated. +#[derive(Encode, Decode, RuntimeDebug, TypeInfo, PartialEq, Clone)] +struct V0Assignment { + pub para_id: ParaId, +} + +/// Old scheduler with explicit parathreads and `Scheduled` storage instead of `ClaimQueue`. mod v0 { use super::*; - use primitives::{CollatorId, Id}; #[storage_alias] @@ -90,29 +99,123 @@ mod v0 { } } -pub mod v1 { +// `ClaimQueue` got introduced. +// +// - Items are `Option` for some weird reason. +// - Assignments only consist of `ParaId`, `Assignment` is a concrete type (Same as V0Assignment). +mod v1 { + use frame_support::{ + pallet_prelude::ValueQuery, storage_alias, traits::OnRuntimeUpgrade, weights::Weight, + }; + use frame_system::pallet_prelude::BlockNumberFor; + use super::*; use crate::scheduler; - #[allow(deprecated)] - pub type MigrateToV1 = VersionedMigration< - 0, - 1, - UncheckedMigrateToV1, + #[storage_alias] + pub(super) type ClaimQueue = StorageValue< Pallet, - ::DbWeight, + BTreeMap>>>>, + ValueQuery, >; - #[deprecated(note = "Use MigrateToV1 instead")] + #[storage_alias] + pub(super) type AvailabilityCores = + StorageValue, Vec>>, ValueQuery>; + + #[derive(Encode, Decode, TypeInfo, RuntimeDebug, PartialEq)] + pub(super) enum CoreOccupied { + /// No candidate is waiting availability on this core right now (the core is not occupied). + Free, + /// A para is currently waiting for availability/inclusion on this core. + Paras(ParasEntry), + } + + #[derive(Encode, Decode, TypeInfo, RuntimeDebug, PartialEq)] + pub(super) struct ParasEntry { + /// The underlying `Assignment` + pub(super) assignment: V0Assignment, + /// The number of times the entry has timed out in availability already. + pub(super) availability_timeouts: u32, + /// The block height until this entry needs to be backed. + /// + /// If missed the entry will be removed from the claim queue without ever having occupied + /// the core. + pub(super) ttl: N, + } + + impl ParasEntry { + /// Create a new `ParasEntry`. + pub(super) fn new(assignment: V0Assignment, now: N) -> Self { + ParasEntry { assignment, availability_timeouts: 0, ttl: now } + } + + /// Return `Id` from the underlying `Assignment`. + pub(super) fn para_id(&self) -> ParaId { + self.assignment.para_id + } + } + + fn add_to_claimqueue(core_idx: CoreIndex, pe: ParasEntry>) { + ClaimQueue::::mutate(|la| { + la.entry(core_idx).or_default().push_back(Some(pe)); + }); + } + + /// Migration to V1 pub struct UncheckedMigrateToV1(sp_std::marker::PhantomData); - #[allow(deprecated)] impl OnRuntimeUpgrade for UncheckedMigrateToV1 { fn on_runtime_upgrade() -> Weight { - let weight_consumed = migrate_to_v1::(); + let mut weight: Weight = Weight::zero(); + + v0::ParathreadQueue::::kill(); + v0::ParathreadClaimIndex::::kill(); + + let now = >::block_number(); + let scheduled = v0::Scheduled::::take(); + let sched_len = scheduled.len() as u64; + for core_assignment in scheduled { + let core_idx = core_assignment.core; + let assignment = V0Assignment { para_id: core_assignment.para_id }; + let pe = v1::ParasEntry::new(assignment, now); + v1::add_to_claimqueue::(core_idx, pe); + } + + let parachains = paras::Pallet::::parachains(); + let availability_cores = v0::AvailabilityCores::::take(); + let mut new_availability_cores = Vec::new(); + + for (core_index, core) in availability_cores.into_iter().enumerate() { + let new_core = if let Some(core) = core { + match core { + v0::CoreOccupied::Parachain => + v1::CoreOccupied::Paras(v1::ParasEntry::new( + V0Assignment { para_id: parachains[core_index] }, + now, + )), + v0::CoreOccupied::Parathread(entry) => v1::CoreOccupied::Paras( + v1::ParasEntry::new(V0Assignment { para_id: entry.claim.0 }, now), + ), + } + } else { + v1::CoreOccupied::Free + }; + + new_availability_cores.push(new_core); + } + + v1::AvailabilityCores::::set(new_availability_cores); - log::info!(target: scheduler::LOG_TARGET, "Migrating para scheduler storage to v1"); + // 2x as once for Scheduled and once for Claimqueue + weight.saturating_accrue(T::DbWeight::get().reads_writes(2 * sched_len, 2 * sched_len)); + // reading parachains + availability_cores, writing AvailabilityCores + weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 1)); + // 2x kill + weight.saturating_accrue(T::DbWeight::get().writes(2)); - weight_consumed + log::info!(target: scheduler::LOG_TARGET, "Migrated para scheduler storage to v1"); + + weight } #[cfg(feature = "try-runtime")] @@ -138,9 +241,9 @@ pub mod v1 { ); let expected_len = u32::decode(&mut &state[..]).unwrap(); - let availability_cores_waiting = super::AvailabilityCores::::get() - .iter() - .filter(|c| !matches!(c, CoreOccupied::Free)) + let availability_cores_waiting = v1::AvailabilityCores::::get() + .into_iter() + .filter(|c| !matches!(c, v1::CoreOccupied::Free)) .count(); ensure!( @@ -154,51 +257,150 @@ pub mod v1 { } } -pub fn migrate_to_v1() -> Weight { - let mut weight: Weight = Weight::zero(); +/// Migrate `V0` to `V1` of the storage format. +pub type MigrateV0ToV1 = VersionedMigration< + 0, + 1, + v1::UncheckedMigrateToV1, + Pallet, + ::DbWeight, +>; - v0::ParathreadQueue::::kill(); - v0::ParathreadClaimIndex::::kill(); +mod v2 { + use super::*; + use crate::scheduler; - let now = >::block_number(); - let scheduled = v0::Scheduled::::take(); - let sched_len = scheduled.len() as u64; - for core_assignment in scheduled { - let core_idx = core_assignment.core; - let assignment = Assignment::new(core_assignment.para_id); - let pe = ParasEntry::new(assignment, now); - Pallet::::add_to_claimqueue(core_idx, pe); + #[derive(Encode, Decode, TypeInfo, RuntimeDebug, PartialEq)] + pub(crate) enum CoreOccupied { + Free, + Paras(ParasEntry), } - let parachains = paras::Pallet::::parachains(); - let availability_cores = v0::AvailabilityCores::::take(); - let mut new_availability_cores = Vec::new(); - - for (core_index, core) in availability_cores.into_iter().enumerate() { - let new_core = if let Some(core) = core { - match core { - v0::CoreOccupied::Parachain => CoreOccupied::Paras(ParasEntry::new( - Assignment::new(parachains[core_index]), - now, - )), - v0::CoreOccupied::Parathread(entry) => - CoreOccupied::Paras(ParasEntry::new(Assignment::new(entry.claim.0), now)), - } - } else { - CoreOccupied::Free - }; + #[derive(Encode, Decode, TypeInfo, RuntimeDebug, PartialEq)] + pub(crate) struct ParasEntry { + pub assignment: Assignment, + pub availability_timeouts: u32, + pub ttl: N, + } - new_availability_cores.push(new_core); + // V2 (no Option wrapper) and new [`Assignment`]. + #[storage_alias] + pub(crate) type ClaimQueue = StorageValue< + Pallet, + BTreeMap>>>, + ValueQuery, + >; + + #[storage_alias] + pub(crate) type AvailabilityCores = + StorageValue, Vec>>, ValueQuery>; + + fn is_bulk(core_index: CoreIndex) -> bool { + core_index.0 < paras::Parachains::::decode_len().unwrap_or(0) as u32 } - super::AvailabilityCores::::set(new_availability_cores); + /// Migration to V2 + pub struct UncheckedMigrateToV2(sp_std::marker::PhantomData); + + impl OnRuntimeUpgrade for UncheckedMigrateToV2 { + fn on_runtime_upgrade() -> Weight { + let mut weight: Weight = Weight::zero(); + + let old = v1::ClaimQueue::::take(); + let new = old + .into_iter() + .map(|(k, v)| { + ( + k, + v.into_iter() + .flatten() + .map(|p| { + let assignment = if is_bulk::(k) { + Assignment::Bulk(p.para_id()) + } else { + Assignment::Pool { para_id: p.para_id(), core_index: k } + }; + + ParasEntry { + assignment, + availability_timeouts: p.availability_timeouts, + ttl: p.ttl, + } + }) + .collect::>(), + ) + }) + .collect::>>>>(); + + ClaimQueue::::put(new); + + let old = v1::AvailabilityCores::::get(); + + let new = old + .into_iter() + .enumerate() + .map(|(k, a)| match a { + v1::CoreOccupied::Free => CoreOccupied::Free, + v1::CoreOccupied::Paras(paras) => { + let assignment = if is_bulk::((k as u32).into()) { + Assignment::Bulk(paras.para_id()) + } else { + Assignment::Pool { + para_id: paras.para_id(), + core_index: (k as u32).into(), + } + }; + + CoreOccupied::Paras(ParasEntry { + assignment, + availability_timeouts: paras.availability_timeouts, + ttl: paras.ttl, + }) + }, + }) + .collect::>(); + AvailabilityCores::::put(new); + + weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); + + log::info!(target: scheduler::LOG_TARGET, "Migrating para scheduler storage to v2"); + + weight + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + log::trace!( + target: crate::scheduler::LOG_TARGET, + "ClaimQueue before migration: {}", + v1::ClaimQueue::::get().len() + ); + + let bytes = u32::to_be_bytes(v1::ClaimQueue::::get().len() as u32); - // 2x as once for Scheduled and once for Claimqueue - weight = weight.saturating_add(T::DbWeight::get().reads_writes(2 * sched_len, 2 * sched_len)); - // reading parachains + availability_cores, writing AvailabilityCores - weight = weight.saturating_add(T::DbWeight::get().reads_writes(2, 1)); - // 2x kill - weight = weight.saturating_add(T::DbWeight::get().writes(2)); + Ok(bytes.to_vec()) + } - weight + #[cfg(feature = "try-runtime")] + fn post_upgrade(state: Vec) -> Result<(), sp_runtime::DispatchError> { + log::trace!(target: crate::scheduler::LOG_TARGET, "Running post_upgrade()"); + + let old_len = u32::from_be_bytes(state.try_into().unwrap()); + ensure!( + v2::ClaimQueue::::get().len() as u32 == old_len, + "Old ClaimQueue completely moved to new ClaimQueue after migration" + ); + + Ok(()) + } + } } + +/// Migrate `V1` to `V2` of the storage format. +pub type MigrateV1ToV2 = VersionedMigration< + 1, + 2, + v2::UncheckedMigrateToV2, + Pallet, + ::DbWeight, +>; diff --git a/polkadot/runtime/parachains/src/scheduler/tests.rs b/polkadot/runtime/parachains/src/scheduler/tests.rs index 108f365d6b5c..9af23ce64bd6 100644 --- a/polkadot/runtime/parachains/src/scheduler/tests.rs +++ b/polkadot/runtime/parachains/src/scheduler/tests.rs @@ -22,24 +22,24 @@ use primitives::{BlockNumber, SessionIndex, ValidationCode, ValidatorId}; use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet}; use crate::{ - assigner_on_demand::QueuePushDirection, configuration::HostConfiguration, initializer::SessionChangeNotification, mock::{ - new_test_ext, MockGenesisConfig, OnDemandAssigner, Paras, ParasShared, RuntimeOrigin, + new_test_ext, MockAssigner, MockGenesisConfig, Paras, ParasShared, RuntimeOrigin, Scheduler, System, Test, }, paras::{ParaGenesisArgs, ParaKind}, + scheduler::{common::Assignment, ClaimQueue}, }; -fn schedule_blank_para(id: ParaId, parakind: ParaKind) { +fn schedule_blank_para(id: ParaId) { let validation_code: ValidationCode = vec![1, 2, 3].into(); assert_ok!(Paras::schedule_para_initialize( id, ParaGenesisArgs { genesis_head: Vec::new().into(), validation_code: validation_code.clone(), - para_kind: parakind, + para_kind: ParaKind::Parathread, // This most closely mimics our test assigner } )); @@ -78,7 +78,7 @@ fn run_to_block( Scheduler::initializer_initialize(b + 1); // In the real runtime this is expected to be called by the `InclusionInherent` pallet. - Scheduler::update_claimqueue(BTreeMap::new(), b + 1); + Scheduler::free_cores_and_fill_claimqueue(BTreeMap::new(), b + 1); } } @@ -103,11 +103,10 @@ fn run_to_end_of_block( fn default_config() -> HostConfiguration { HostConfiguration { - on_demand_cores: 3, + coretime_cores: 3, group_rotation_frequency: 10, paras_availability_period: 3, scheduling_lookahead: 2, - on_demand_retries: 1, // This field does not affect anything that scheduler does. However, `HostConfiguration` // is still a subject to consistency test. It requires that // `minimum_validation_upgrade_delay` is greater than `chain_availability_period` and @@ -124,29 +123,16 @@ fn genesis_config(config: &HostConfiguration) -> MockGenesisConfig } } -pub(crate) fn claimqueue_contains_only_none() -> bool { - let mut cq = Scheduler::claimqueue(); - for (_, v) in cq.iter_mut() { - v.retain(|e| e.is_some()); - } - - cq.values().map(|v| v.len()).sum::() == 0 -} - -pub(crate) fn claimqueue_contains_para_ids(pids: Vec) -> bool { +fn claimqueue_contains_para_ids(pids: Vec) -> bool { let set: BTreeSet = ClaimQueue::::get() .into_iter() - .flat_map(|(_, assignments)| { - assignments - .into_iter() - .filter_map(|assignment| assignment.and_then(|pe| Some(pe.para_id()))) - }) + .flat_map(|(_, paras_entries)| paras_entries.into_iter().map(|pe| pe.assignment.para_id())) .collect(); pids.into_iter().all(|pid| set.contains(&pid)) } -pub(crate) fn availability_cores_contains_para_ids(pids: Vec) -> bool { +fn availability_cores_contains_para_ids(pids: Vec) -> bool { let set: BTreeSet = AvailabilityCores::::get() .into_iter() .filter_map(|core| match core { @@ -158,6 +144,14 @@ pub(crate) fn availability_cores_contains_para_ids(pids: Vec) pids.into_iter().all(|pid| set.contains(&pid)) } +/// Internal access to entries at the top of the claim queue. +fn scheduled_entries() -> impl Iterator>)> { + let claimqueue = ClaimQueue::::get(); + claimqueue + .into_iter() + .filter_map(|(core_idx, v)| v.front().map(|e| (core_idx, e.clone()))) +} + #[test] fn claimqueue_ttl_drop_fn_works() { let mut config = default_config(); @@ -169,13 +163,14 @@ fn claimqueue_ttl_drop_fn_works() { let mut now = 10; new_test_ext(genesis_config).execute_with(|| { - assert!(default_config().on_demand_ttl == 5); + let assignment_provider_ttl = MockAssigner::get_provider_config(CoreIndex::from(0)).ttl; + assert!(assignment_provider_ttl == 5); // Register and run to a blockheight where the para is in a valid state. - schedule_blank_para(para_id, ParaKind::Parathread); - run_to_block(10, |n| if n == 10 { Some(Default::default()) } else { None }); + schedule_blank_para(para_id); + run_to_block(now, |n| if n == now { Some(Default::default()) } else { None }); // Add a claim on core 0 with a ttl in the past. - let paras_entry = ParasEntry::new(Assignment::new(para_id), now - 5); + let paras_entry = ParasEntry::new(Assignment::Bulk(para_id), now - 5 as u32); Scheduler::add_to_claimqueue(core_idx, paras_entry.clone()); // Claim is in queue prior to call. @@ -186,7 +181,7 @@ fn claimqueue_ttl_drop_fn_works() { assert!(!claimqueue_contains_para_ids::(vec![para_id])); // Add a claim on core 0 with a ttl in the future (15). - let paras_entry = ParasEntry::new(Assignment::new(para_id), now + 5); + let paras_entry = ParasEntry::new(Assignment::Bulk(para_id), now + 5); Scheduler::add_to_claimqueue(core_idx, paras_entry.clone()); // Claim is in queue post call. @@ -201,7 +196,7 @@ fn claimqueue_ttl_drop_fn_works() { assert!(!claimqueue_contains_para_ids::(vec![para_id])); // Add a claim on core 0 with a ttl == now (16) - let paras_entry = ParasEntry::new(Assignment::new(para_id), now); + let paras_entry = ParasEntry::new(Assignment::Bulk(para_id), now); Scheduler::add_to_claimqueue(core_idx, paras_entry.clone()); // Claim is in queue post call. @@ -215,8 +210,8 @@ fn claimqueue_ttl_drop_fn_works() { Scheduler::drop_expired_claims_from_claimqueue(); // Add a claim on core 0 with a ttl == now (17) - let paras_entry_non_expired = ParasEntry::new(Assignment::new(para_id), now); - let paras_entry_expired = ParasEntry::new(Assignment::new(para_id), now - 2); + let paras_entry_non_expired = ParasEntry::new(Assignment::Bulk(para_id), now); + let paras_entry_expired = ParasEntry::new(Assignment::Bulk(para_id), now - 2); // ttls = [17, 15, 17] Scheduler::add_to_claimqueue(core_idx, paras_entry_non_expired.clone()); Scheduler::add_to_claimqueue(core_idx, paras_entry_expired.clone()); @@ -224,18 +219,10 @@ fn claimqueue_ttl_drop_fn_works() { let cq = Scheduler::claimqueue(); assert!(cq.get(&core_idx).unwrap().len() == 3); - // Add claims to on demand assignment provider. - let assignment = Assignment::new(para_id); + // Add a claim to the test assignment provider. + let assignment = Assignment::Bulk(para_id); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment.clone(), - QueuePushDirection::Back - )); - - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment, - QueuePushDirection::Back - )); + MockAssigner::add_test_assignment(assignment.clone()); // Drop expired claim. Scheduler::drop_expired_claims_from_claimqueue(); @@ -248,58 +235,25 @@ fn claimqueue_ttl_drop_fn_works() { // The first 2 claims in the queue should have a ttl of 17, // being the ones set up prior in this test as claims 1 and 3. // The third claim is popped from the assignment provider and - // has a new ttl set by the scheduler of now + config.on_demand_ttl. - // ttls = [17, 17, 22] + // has a new ttl set by the scheduler of now + + // assignment_provider_ttl. ttls = [17, 17, 22] assert!(cqc.iter().enumerate().all(|(index, entry)| { match index { - 0 | 1 => return entry.clone().unwrap().ttl == 17, - 2 => return entry.clone().unwrap().ttl == 22, - _ => return false, + 0 | 1 => entry.clone().ttl == 17, + 2 => entry.clone().ttl == 22, + _ => false, } })) }); } -// Pretty useless here. Should be on parathread assigner... if at all -#[test] -fn add_parathread_claim_works() { - let genesis_config = genesis_config(&default_config()); - - let thread_id = ParaId::from(10); - let core_index = CoreIndex::from(0); - let entry_ttl = 10_000; - - new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(thread_id, ParaKind::Parathread); - - assert!(!Paras::is_parathread(thread_id)); - - run_to_block(10, |n| if n == 10 { Some(Default::default()) } else { None }); - - assert!(Paras::is_parathread(thread_id)); - - let pe = ParasEntry::new(Assignment::new(thread_id), entry_ttl); - Scheduler::add_to_claimqueue(core_index, pe.clone()); - - let cq = Scheduler::claimqueue(); - assert_eq!(Scheduler::claimqueue_len(), 1); - assert_eq!(*(cq.get(&core_index).unwrap().front().unwrap()), Some(pe)); - }) -} - #[test] fn session_change_shuffles_validators() { let genesis_config = genesis_config(&default_config()); - assert_eq!(default_config().on_demand_cores, 3); new_test_ext(genesis_config).execute_with(|| { - let chain_a = ParaId::from(1_u32); - let chain_b = ParaId::from(2_u32); - - // ensure that we have 5 groups by registering 2 parachains. - schedule_blank_para(chain_a, ParaKind::Parachain); - schedule_blank_para(chain_b, ParaKind::Parachain); - + // Need five cores for this test + MockAssigner::set_core_count(5); run_to_block(1, |number| match number { 1 => Some(SessionChangeNotification { new_config: default_config(), @@ -336,7 +290,6 @@ fn session_change_shuffles_validators() { fn session_change_takes_only_max_per_core() { let config = { let mut config = default_config(); - config.on_demand_cores = 0; config.max_validators_per_core = Some(1); config }; @@ -344,14 +297,8 @@ fn session_change_takes_only_max_per_core() { let genesis_config = genesis_config(&config); new_test_ext(genesis_config).execute_with(|| { - let chain_a = ParaId::from(1_u32); - let chain_b = ParaId::from(2_u32); - let chain_c = ParaId::from(3_u32); - - // ensure that we have 5 groups by registering 2 parachains. - schedule_blank_para(chain_a, ParaKind::Parachain); - schedule_blank_para(chain_b, ParaKind::Parachain); - schedule_blank_para(chain_c, ParaKind::Parathread); + // Simulate 2 cores between all usage types + MockAssigner::set_core_count(2); run_to_block(1, |number| match number { 1 => Some(SessionChangeNotification { @@ -374,7 +321,7 @@ fn session_change_takes_only_max_per_core() { let groups = ValidatorGroups::::get(); assert_eq!(groups.len(), 7); - // Every validator gets its own group, even though there are 2 paras. + // Every validator gets its own group, even though there are 2 cores. for i in 0..7 { assert_eq!(groups[i].len(), 1); } @@ -385,31 +332,25 @@ fn session_change_takes_only_max_per_core() { fn fill_claimqueue_fills() { let genesis_config = genesis_config(&default_config()); - let lookahead = genesis_config.configuration.config.scheduling_lookahead as usize; - let chain_a = ParaId::from(1_u32); - let chain_b = ParaId::from(2_u32); - - let thread_a = ParaId::from(3_u32); - let thread_b = ParaId::from(4_u32); - let thread_c = ParaId::from(5_u32); + let para_a = ParaId::from(3_u32); + let para_b = ParaId::from(4_u32); + let para_c = ParaId::from(5_u32); - let assignment_a = Assignment { para_id: thread_a }; - let assignment_b = Assignment { para_id: thread_b }; - let assignment_c = Assignment { para_id: thread_c }; + let assignment_a = Assignment::Bulk(para_a); + let assignment_b = Assignment::Bulk(para_b); + let assignment_c = Assignment::Bulk(para_c); new_test_ext(genesis_config).execute_with(|| { - assert_eq!(default_config().on_demand_cores, 3); + MockAssigner::set_core_count(2); + let AssignmentProviderConfig { ttl: config_ttl, .. } = + MockAssigner::get_provider_config(CoreIndex(0)); - // register 2 lease holding parachains - schedule_blank_para(chain_a, ParaKind::Parachain); - schedule_blank_para(chain_b, ParaKind::Parachain); + // Add 3 paras + schedule_blank_para(para_a); + schedule_blank_para(para_b); + schedule_blank_para(para_c); - // and 3 parathreads (on-demand parachains) - schedule_blank_para(thread_a, ParaKind::Parathread); - schedule_blank_para(thread_b, ParaKind::Parathread); - schedule_blank_para(thread_c, ParaKind::Parathread); - - // start a new session to activate, 5 validators for 5 cores. + // start a new session to activate, 3 validators for 3 cores. run_to_block(1, |number| match number { 1 => Some(SessionChangeNotification { new_config: default_config(), @@ -417,107 +358,47 @@ fn fill_claimqueue_fills() { ValidatorId::from(Sr25519Keyring::Alice.public()), ValidatorId::from(Sr25519Keyring::Bob.public()), ValidatorId::from(Sr25519Keyring::Charlie.public()), - ValidatorId::from(Sr25519Keyring::Dave.public()), - ValidatorId::from(Sr25519Keyring::Eve.public()), ], ..Default::default() }), _ => None, }); - { - assert_eq!(Scheduler::claimqueue_len(), 2 * lookahead); - let scheduled: BTreeMap<_, _> = Scheduler::scheduled_entries().collect(); - - // Cannot assert on indices anymore as they depend on the assignment providers - assert!(claimqueue_contains_para_ids::(vec![chain_a, chain_b])); - - assert_eq!( - scheduled.get(&CoreIndex(0)).unwrap(), - &ParasEntry { - assignment: Assignment { para_id: chain_a }, - availability_timeouts: 0, - ttl: 6 - }, - ); - - assert_eq!( - scheduled.get(&CoreIndex(1)).unwrap(), - &ParasEntry { - assignment: Assignment { para_id: chain_b }, - availability_timeouts: 0, - ttl: 6 - }, - ); - } - - // add a couple of parathread assignments. - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_a, - QueuePushDirection::Back - )); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_b, - QueuePushDirection::Back - )); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_c, - QueuePushDirection::Back - )); + // add some para assignments. + MockAssigner::add_test_assignment(assignment_a.clone()); + MockAssigner::add_test_assignment(assignment_b.clone()); + MockAssigner::add_test_assignment(assignment_c.clone()); run_to_block(2, |_| None); - // cores 0 and 1 should be occupied. mark them as such. - Scheduler::occupied( - vec![(CoreIndex(0), chain_a), (CoreIndex(1), chain_b)].into_iter().collect(), - ); - - run_to_block(3, |_| None); { - assert_eq!(Scheduler::claimqueue_len(), 5); - let scheduled: BTreeMap<_, _> = Scheduler::scheduled_entries().collect(); - - assert_eq!( - scheduled.get(&CoreIndex(0)).unwrap(), - &ParasEntry { - assignment: Assignment { para_id: chain_a }, - availability_timeouts: 0, - ttl: 6 - }, - ); - assert_eq!( - scheduled.get(&CoreIndex(1)).unwrap(), - &ParasEntry { - assignment: Assignment { para_id: chain_b }, - availability_timeouts: 0, - ttl: 6 - }, - ); + assert_eq!(Scheduler::claimqueue_len(), 3); + let scheduled: BTreeMap<_, _> = scheduled_entries().collect(); // Was added a block later, note the TTL. assert_eq!( - scheduled.get(&CoreIndex(2)).unwrap(), + scheduled.get(&CoreIndex(0)).unwrap(), &ParasEntry { - assignment: Assignment { para_id: thread_a }, + assignment: assignment_a.clone(), availability_timeouts: 0, - ttl: 7 + ttl: 2 + config_ttl }, ); - // Sits on the same core as `thread_a` + // Sits on the same core as `para_a` assert_eq!( - Scheduler::claimqueue().get(&CoreIndex(2)).unwrap()[1], - Some(ParasEntry { - assignment: Assignment { para_id: thread_b }, + Scheduler::claimqueue().get(&CoreIndex(0)).unwrap()[1], + ParasEntry { + assignment: assignment_b.clone(), availability_timeouts: 0, - ttl: 7 - }) + ttl: 2 + config_ttl + } ); assert_eq!( - scheduled.get(&CoreIndex(3)).unwrap(), + scheduled.get(&CoreIndex(1)).unwrap(), &ParasEntry { - assignment: Assignment { para_id: thread_c }, + assignment: assignment_c.clone(), availability_timeouts: 0, - ttl: 7 + ttl: 2 + config_ttl }, ); } @@ -532,36 +413,29 @@ fn schedule_schedules_including_just_freed() { config.scheduling_lookahead = 1; let genesis_config = genesis_config(&config); - let chain_a = ParaId::from(1_u32); - let chain_b = ParaId::from(2_u32); - - let thread_a = ParaId::from(3_u32); - let thread_b = ParaId::from(4_u32); - let thread_c = ParaId::from(5_u32); - let thread_d = ParaId::from(6_u32); - let thread_e = ParaId::from(7_u32); + let para_a = ParaId::from(3_u32); + let para_b = ParaId::from(4_u32); + let para_c = ParaId::from(5_u32); + let para_d = ParaId::from(6_u32); + let para_e = ParaId::from(7_u32); - let assignment_a = Assignment { para_id: thread_a }; - let assignment_b = Assignment { para_id: thread_b }; - let assignment_c = Assignment { para_id: thread_c }; - let assignment_d = Assignment { para_id: thread_d }; - let assignment_e = Assignment { para_id: thread_e }; + let assignment_a = Assignment::Bulk(para_a); + let assignment_b = Assignment::Bulk(para_b); + let assignment_c = Assignment::Bulk(para_c); + let assignment_d = Assignment::Bulk(para_d); + let assignment_e = Assignment::Bulk(para_e); new_test_ext(genesis_config).execute_with(|| { - assert_eq!(default_config().on_demand_cores, 3); + MockAssigner::set_core_count(3); - // register 2 lease holding parachains - schedule_blank_para(chain_a, ParaKind::Parachain); - schedule_blank_para(chain_b, ParaKind::Parachain); + // add 5 paras + schedule_blank_para(para_a); + schedule_blank_para(para_b); + schedule_blank_para(para_c); + schedule_blank_para(para_d); + schedule_blank_para(para_e); - // and 5 parathreads (on-demand parachains) - schedule_blank_para(thread_a, ParaKind::Parathread); - schedule_blank_para(thread_b, ParaKind::Parathread); - schedule_blank_para(thread_c, ParaKind::Parathread); - schedule_blank_para(thread_d, ParaKind::Parathread); - schedule_blank_para(thread_e, ParaKind::Parathread); - - // start a new session to activate, 5 validators for 5 cores. + // start a new session to activate, 3 validators for 3 cores. run_to_block(1, |number| match number { 1 => Some(SessionChangeNotification { new_config: default_config(), @@ -569,153 +443,113 @@ fn schedule_schedules_including_just_freed() { ValidatorId::from(Sr25519Keyring::Alice.public()), ValidatorId::from(Sr25519Keyring::Bob.public()), ValidatorId::from(Sr25519Keyring::Charlie.public()), - ValidatorId::from(Sr25519Keyring::Dave.public()), - ValidatorId::from(Sr25519Keyring::Eve.public()), ], ..Default::default() }), _ => None, }); - // add a couple of parathread claims now that the parathreads are live. - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_a, - QueuePushDirection::Back - )); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_c, - QueuePushDirection::Back - )); + // add a couple of para claims now that paras are live + MockAssigner::add_test_assignment(assignment_a.clone()); + MockAssigner::add_test_assignment(assignment_c.clone()); let mut now = 2; run_to_block(now, |_| None); - assert_eq!(Scheduler::scheduled_paras().collect::>().len(), 4); + assert_eq!(Scheduler::scheduled_paras().collect::>().len(), 2); - // cores 0, 1, 2, and 3 should be occupied. mark them as such. + // cores 0, 1 should be occupied. mark them as such. let mut occupied_map: BTreeMap = BTreeMap::new(); - occupied_map.insert(CoreIndex(0), chain_a); - occupied_map.insert(CoreIndex(1), chain_b); - occupied_map.insert(CoreIndex(2), thread_a); - occupied_map.insert(CoreIndex(3), thread_c); + occupied_map.insert(CoreIndex(0), para_a); + occupied_map.insert(CoreIndex(1), para_c); Scheduler::occupied(occupied_map); { let cores = AvailabilityCores::::get(); - // cores 0, 1, 2, and 3 are all `CoreOccupied::Paras(ParasEntry...)` + // cores 0, 1 are `CoreOccupied::Paras(ParasEntry...)` assert!(cores[0] != CoreOccupied::Free); assert!(cores[1] != CoreOccupied::Free); - assert!(cores[2] != CoreOccupied::Free); - assert!(cores[3] != CoreOccupied::Free); - // core 4 is free - assert!(cores[4] == CoreOccupied::Free); + // core 2 is free + assert!(cores[2] == CoreOccupied::Free); assert!(Scheduler::scheduled_paras().collect::>().is_empty()); - // All core index entries in the claimqueue should have `None` in them. - Scheduler::claimqueue().iter().for_each(|(_core_idx, core_queue)| { - assert!(core_queue.iter().all(|claim| claim.is_none())) - }) + // All `core_queue`s should be empty + Scheduler::claimqueue() + .iter() + .for_each(|(_core_idx, core_queue)| assert!(core_queue.len() == 0)) } - // add a couple more parathread claims - the claim on `b` will go to the 3rd parathread core - // (4) and the claim on `d` will go back to the 1st parathread core (2). The claim on `e` - // then will go for core `3`. - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_b, - QueuePushDirection::Back - )); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_d, - QueuePushDirection::Back - )); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_e.clone(), - QueuePushDirection::Back - )); + // add a couple more para claims - the claim on `b` will go to the 3rd core + // (2) and the claim on `d` will go back to the 1st para core (0). The claim on `e` + // then will go for core `1`. + MockAssigner::add_test_assignment(assignment_b.clone()); + MockAssigner::add_test_assignment(assignment_d.clone()); + MockAssigner::add_test_assignment(assignment_e.clone()); now = 3; run_to_block(now, |_| None); { - let scheduled: BTreeMap<_, _> = Scheduler::scheduled_entries().collect(); + let scheduled: BTreeMap<_, _> = scheduled_entries().collect(); - // cores 0 and 1 are occupied by lease holding parachains. cores 2 and 3 are occupied by - // on-demand parachain claims. core 4 was free. + // cores 0 and 1 are occupied by claims. core 2 was free. assert_eq!(scheduled.len(), 1); assert_eq!( - scheduled.get(&CoreIndex(4)).unwrap(), + scheduled.get(&CoreIndex(2)).unwrap(), &ParasEntry { - assignment: Assignment { para_id: thread_b }, + assignment: Assignment::Bulk(para_b), availability_timeouts: 0, ttl: 8 }, ); } - // now note that cores 0, 2, and 3 were freed. + // now note that cores 0 and 1 were freed. let just_updated: BTreeMap = vec![ (CoreIndex(0), FreedReason::Concluded), - (CoreIndex(2), FreedReason::Concluded), - (CoreIndex(3), FreedReason::TimedOut), // should go back on queue. + (CoreIndex(1), FreedReason::TimedOut), // should go back on queue. ] .into_iter() .collect(); - Scheduler::update_claimqueue(just_updated, now); + Scheduler::free_cores_and_fill_claimqueue(just_updated, now); { - let scheduled: BTreeMap<_, _> = Scheduler::scheduled_entries().collect(); + let scheduled: BTreeMap<_, _> = scheduled_entries().collect(); - // 1 thing scheduled before, + 3 cores freed. - assert_eq!(scheduled.len(), 4); + // 1 thing scheduled before, + 2 cores freed. + assert_eq!(scheduled.len(), 3); assert_eq!( scheduled.get(&CoreIndex(0)).unwrap(), &ParasEntry { - assignment: Assignment { para_id: chain_a }, - availability_timeouts: 0, - ttl: 8 - }, - ); - assert_eq!( - scheduled.get(&CoreIndex(2)).unwrap(), - &ParasEntry { - assignment: Assignment { para_id: thread_d }, + assignment: Assignment::Bulk(para_d), availability_timeouts: 0, ttl: 8 }, ); - // Although C was descheduled, the core `4` was occupied so C goes back to the queue. + // Although C was descheduled, the core `2` was occupied so C goes back to the queue. assert_eq!( - scheduled.get(&CoreIndex(3)).unwrap(), + scheduled.get(&CoreIndex(1)).unwrap(), &ParasEntry { - assignment: Assignment { para_id: thread_c }, + assignment: Assignment::Bulk(para_c), availability_timeouts: 1, ttl: 8 }, ); assert_eq!( - scheduled.get(&CoreIndex(4)).unwrap(), + scheduled.get(&CoreIndex(2)).unwrap(), &ParasEntry { - assignment: Assignment { para_id: thread_b }, + assignment: Assignment::Bulk(para_b), availability_timeouts: 0, ttl: 8 }, ); - // The only assignment yet to be popped on to the claim queue is `thread_e`. - // This is due to `thread_c` timing out. - let order_queue = OnDemandAssigner::get_queue(); - assert!(order_queue.len() == 1); - assert!(order_queue[0] == assignment_e); - - // Chain B's core was not marked concluded or timed out, it should be on an - // availability core - assert!(availability_cores_contains_para_ids::(vec![chain_b])); - // Thread A claim should have been wiped, but thread C claim should remain. - assert!(!claimqueue_contains_para_ids::(vec![thread_a])); - assert!(claimqueue_contains_para_ids::(vec![thread_c])); - assert!(!availability_cores_contains_para_ids::(vec![thread_a, thread_c])); + // Para A claim should have been wiped, but para C claim should remain. + assert!(!claimqueue_contains_para_ids::(vec![para_a])); + assert!(claimqueue_contains_para_ids::(vec![para_c])); + assert!(!availability_cores_contains_para_ids::(vec![para_a, para_c])); } }); } @@ -726,28 +560,35 @@ fn schedule_clears_availability_cores() { config.scheduling_lookahead = 1; let genesis_config = genesis_config(&config); - let chain_a = ParaId::from(1_u32); - let chain_b = ParaId::from(2_u32); - let chain_c = ParaId::from(3_u32); + let para_a = ParaId::from(1_u32); + let para_b = ParaId::from(2_u32); + let para_c = ParaId::from(3_u32); + + let assignment_a = Assignment::Bulk(para_a); + let assignment_b = Assignment::Bulk(para_b); + let assignment_c = Assignment::Bulk(para_c); new_test_ext(genesis_config).execute_with(|| { - assert_eq!(default_config().on_demand_cores, 3); + MockAssigner::set_core_count(3); + + // register 3 paras + schedule_blank_para(para_a); + schedule_blank_para(para_b); + schedule_blank_para(para_c); - // register 3 parachains - schedule_blank_para(chain_a, ParaKind::Parachain); - schedule_blank_para(chain_b, ParaKind::Parachain); - schedule_blank_para(chain_c, ParaKind::Parachain); + // Adding assignments then running block to populate claim queue + MockAssigner::add_test_assignment(assignment_a.clone()); + MockAssigner::add_test_assignment(assignment_b.clone()); + MockAssigner::add_test_assignment(assignment_c.clone()); - // start a new session to activate, 5 validators for 5 cores. + // start a new session to activate, 3 validators for 3 cores. run_to_block(1, |number| match number { 1 => Some(SessionChangeNotification { - new_config: default_config(), + new_config: config.clone(), validators: vec![ ValidatorId::from(Sr25519Keyring::Alice.public()), ValidatorId::from(Sr25519Keyring::Bob.public()), ValidatorId::from(Sr25519Keyring::Charlie.public()), - ValidatorId::from(Sr25519Keyring::Dave.public()), - ValidatorId::from(Sr25519Keyring::Eve.public()), ], ..Default::default() }), @@ -760,7 +601,7 @@ fn schedule_clears_availability_cores() { // cores 0, 1, and 2 should be occupied. mark them as such. Scheduler::occupied( - vec![(CoreIndex(0), chain_a), (CoreIndex(1), chain_b), (CoreIndex(2), chain_c)] + vec![(CoreIndex(0), para_a), (CoreIndex(1), para_b), (CoreIndex(2), para_c)] .into_iter() .collect(), ); @@ -772,9 +613,16 @@ fn schedule_clears_availability_cores() { assert_eq!(cores[1].is_free(), false); assert_eq!(cores[2].is_free(), false); - assert!(claimqueue_contains_only_none()); + // All `core_queue`s should be empty + Scheduler::claimqueue() + .iter() + .for_each(|(_core_idx, core_queue)| assert!(core_queue.len() == 0)) } + // Add more assignments + MockAssigner::add_test_assignment(assignment_a.clone()); + MockAssigner::add_test_assignment(assignment_c.clone()); + run_to_block(3, |_| None); // now note that cores 0 and 2 were freed. @@ -786,20 +634,18 @@ fn schedule_clears_availability_cores() { ); { - let claimqueue = Scheduler::claimqueue(); + let claimqueue = ClaimQueue::::get(); let claimqueue_0 = claimqueue.get(&CoreIndex(0)).unwrap().clone(); let claimqueue_2 = claimqueue.get(&CoreIndex(2)).unwrap().clone(); let entry_ttl = 8; assert_eq!(claimqueue_0.len(), 1); assert_eq!(claimqueue_2.len(), 1); - assert_eq!( - claimqueue_0, - vec![Some(ParasEntry::new(Assignment::new(chain_a), entry_ttl))], - ); - assert_eq!( - claimqueue_2, - vec![Some(ParasEntry::new(Assignment::new(chain_c), entry_ttl))], - ); + let queue_0_expectation: VecDeque> = + vec![ParasEntry::new(assignment_a, entry_ttl as u32)].into_iter().collect(); + let queue_2_expectation: VecDeque> = + vec![ParasEntry::new(assignment_c, entry_ttl as u32)].into_iter().collect(); + assert_eq!(claimqueue_0, queue_0_expectation); + assert_eq!(claimqueue_2, queue_2_expectation); // The freed cores should be `Free` in `AvailabilityCores`. let cores = AvailabilityCores::::get(); @@ -813,32 +659,28 @@ fn schedule_clears_availability_cores() { fn schedule_rotates_groups() { let config = { let mut config = default_config(); - - // make sure on demand requests don't retry-out - config.on_demand_retries = config.group_rotation_frequency * 3; - config.on_demand_cores = 2; config.scheduling_lookahead = 1; config }; let rotation_frequency = config.group_rotation_frequency; - let on_demand_cores = config.on_demand_cores; + let on_demand_cores = 2; let genesis_config = genesis_config(&config); - let thread_a = ParaId::from(1_u32); - let thread_b = ParaId::from(2_u32); + let para_a = ParaId::from(1_u32); + let para_b = ParaId::from(2_u32); - let assignment_a = Assignment { para_id: thread_a }; - let assignment_b = Assignment { para_id: thread_b }; + let assignment_a = Assignment::Bulk(para_a); + let assignment_b = Assignment::Bulk(para_b); new_test_ext(genesis_config).execute_with(|| { - assert_eq!(default_config().on_demand_cores, 3); + MockAssigner::set_core_count(on_demand_cores); - schedule_blank_para(thread_a, ParaKind::Parathread); - schedule_blank_para(thread_b, ParaKind::Parathread); + schedule_blank_para(para_a); + schedule_blank_para(para_b); - // start a new session to activate, 5 validators for 5 cores. + // start a new session to activate, 2 validators for 2 cores. run_to_block(1, |number| match number { 1 => Some(SessionChangeNotification { new_config: config.clone(), @@ -854,14 +696,8 @@ fn schedule_rotates_groups() { let session_start_block = Scheduler::session_start_block(); assert_eq!(session_start_block, 1); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_a, - QueuePushDirection::Back - )); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_b, - QueuePushDirection::Back - )); + MockAssigner::add_test_assignment(assignment_a.clone()); + MockAssigner::add_test_assignment(assignment_b.clone()); let mut now = 2; run_to_block(now, |_| None); @@ -909,16 +745,20 @@ fn on_demand_claims_are_pruned_after_timing_out() { let max_retries = 20; let mut config = default_config(); config.scheduling_lookahead = 1; - config.on_demand_cores = 2; - config.on_demand_retries = max_retries; let genesis_config = genesis_config(&config); - let thread_a = ParaId::from(1_u32); + let para_a = ParaId::from(1_u32); - let assignment_a = Assignment { para_id: thread_a }; + let assignment_a = Assignment::Bulk(para_a); new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(thread_a, ParaKind::Parathread); + MockAssigner::set_core_count(2); + // Need more timeouts for this test + MockAssigner::set_assignment_provider_config(AssignmentProviderConfig { + max_availability_timeouts: max_retries, + ttl: BlockNumber::from(5u32), + }); + schedule_blank_para(para_a); // #1 let mut now = 1; @@ -934,23 +774,20 @@ fn on_demand_claims_are_pruned_after_timing_out() { _ => None, }); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_a.clone(), - QueuePushDirection::Back - )); + MockAssigner::add_test_assignment(assignment_a.clone()); // #2 now += 1; run_to_block(now, |_| None); assert_eq!(Scheduler::claimqueue().len(), 1); // ParaId a is in the claimqueue. - assert!(claimqueue_contains_para_ids::(vec![thread_a])); + assert!(claimqueue_contains_para_ids::(vec![para_a])); - Scheduler::occupied(vec![(CoreIndex(0), thread_a)].into_iter().collect()); + Scheduler::occupied(vec![(CoreIndex(0), para_a)].into_iter().collect()); // ParaId a is no longer in the claimqueue. - assert!(!claimqueue_contains_para_ids::(vec![thread_a])); + assert!(!claimqueue_contains_para_ids::(vec![para_a])); // It is in availability cores. - assert!(availability_cores_contains_para_ids::(vec![thread_a])); + assert!(availability_cores_contains_para_ids::(vec![para_a])); // #3 now += 1; @@ -966,36 +803,32 @@ fn on_demand_claims_are_pruned_after_timing_out() { ] .into_iter() .collect(); - Scheduler::update_claimqueue(just_updated, now); + Scheduler::free_cores_and_fill_claimqueue(just_updated, now); // ParaId a exists in the claim queue until max_retries is reached. if n < max_retries + now { - assert!(claimqueue_contains_para_ids::(vec![thread_a])); + assert!(claimqueue_contains_para_ids::(vec![para_a])); } else { - assert!(!claimqueue_contains_para_ids::(vec![thread_a])); + assert!(!claimqueue_contains_para_ids::(vec![para_a])); } let core_assignments = Scheduler::scheduled_paras().collect(); - // Occupy the cores based on the result of update_claimqueue. Scheduler::occupied(core_assignments); } // ParaId a does not exist in the claimqueue/availability_cores after // threshold has been reached. - assert!(!claimqueue_contains_para_ids::(vec![thread_a])); - assert!(!availability_cores_contains_para_ids::(vec![thread_a])); + assert!(!claimqueue_contains_para_ids::(vec![para_a])); + assert!(!availability_cores_contains_para_ids::(vec![para_a])); // #25 now += max_retries + 2; // Add assignment back to the mix. - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_a.clone(), - QueuePushDirection::Back - )); + MockAssigner::add_test_assignment(assignment_a.clone()); run_to_block(now, |_| None); - assert!(claimqueue_contains_para_ids::(vec![thread_a])); + assert!(claimqueue_contains_para_ids::(vec![para_a])); // #26 now += 1; @@ -1017,24 +850,23 @@ fn on_demand_claims_are_pruned_after_timing_out() { } } - Scheduler::update_claimqueue(just_updated, now); + Scheduler::free_cores_and_fill_claimqueue(just_updated, now); // ParaId a exists in the claim queue until groups are rotated. if n < 31 { - assert!(claimqueue_contains_para_ids::(vec![thread_a])); + assert!(claimqueue_contains_para_ids::(vec![para_a])); } else { - assert!(!claimqueue_contains_para_ids::(vec![thread_a])); + assert!(!claimqueue_contains_para_ids::(vec![para_a])); } let core_assignments = Scheduler::scheduled_paras().collect(); - // Occupy the cores based on the result of update_claimqueue. Scheduler::occupied(core_assignments); } // ParaId a does not exist in the claimqueue/availability_cores after // being concluded - assert!(!claimqueue_contains_para_ids::(vec![thread_a])); - assert!(!availability_cores_contains_para_ids::(vec![thread_a])); + assert!(!claimqueue_contains_para_ids::(vec![para_a])); + assert!(!availability_cores_contains_para_ids::(vec![para_a])); }); } @@ -1047,40 +879,7 @@ fn availability_predicate_works() { assert!(paras_availability_period < group_rotation_frequency); - let chain_a = ParaId::from(1_u32); - let thread_a = ParaId::from(2_u32); - new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(chain_a, ParaKind::Parachain); - schedule_blank_para(thread_a, ParaKind::Parathread); - - // start a new session with our chain registered. - run_to_block(1, |number| match number { - 1 => Some(SessionChangeNotification { - new_config: default_config(), - validators: vec![ - ValidatorId::from(Sr25519Keyring::Alice.public()), - ValidatorId::from(Sr25519Keyring::Bob.public()), - ValidatorId::from(Sr25519Keyring::Charlie.public()), - ValidatorId::from(Sr25519Keyring::Dave.public()), - ValidatorId::from(Sr25519Keyring::Eve.public()), - ], - ..Default::default() - }), - _ => None, - }); - - // assign some availability cores. - { - let entry_ttl = 10_000; - AvailabilityCores::::mutate(|cores| { - cores[0] = - CoreOccupied::Paras(ParasEntry::new(Assignment::new(chain_a), entry_ttl)); - cores[1] = - CoreOccupied::Paras(ParasEntry::new(Assignment::new(thread_a), entry_ttl)); - }); - } - run_to_block(1 + paras_availability_period, |_| None); assert!(!Scheduler::availability_timeout_check_required()); @@ -1103,29 +902,25 @@ fn availability_predicate_works() { // check the threshold is exact. assert!(!pred(would_be_timed_out + 1).timed_out); } - - run_to_block(1 + group_rotation_frequency + paras_availability_period, |_| None); }); } #[test] -fn next_up_on_available_uses_next_scheduled_or_none_for_thread() { - let mut config = default_config(); - config.on_demand_cores = 1; - - let genesis_config = genesis_config(&config); +fn next_up_on_available_uses_next_scheduled_or_none() { + let genesis_config = genesis_config(&default_config()); - let thread_a = ParaId::from(1_u32); - let thread_b = ParaId::from(2_u32); + let para_a = ParaId::from(1_u32); + let para_b = ParaId::from(2_u32); new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(thread_a, ParaKind::Parathread); - schedule_blank_para(thread_b, ParaKind::Parathread); + MockAssigner::set_core_count(1); + schedule_blank_para(para_a); + schedule_blank_para(para_b); - // start a new session to activate, 5 validators for 5 cores. + // start a new session to activate, 2 validators for 2 cores. run_to_block(1, |number| match number { 1 => Some(SessionChangeNotification { - new_config: config.clone(), + new_config: default_config(), validators: vec![ ValidatorId::from(Sr25519Keyring::Alice.public()), ValidatorId::from(Sr25519Keyring::Eve.public()), @@ -1135,18 +930,18 @@ fn next_up_on_available_uses_next_scheduled_or_none_for_thread() { _ => None, }); - let thread_entry_a = ParasEntry { - assignment: Assignment { para_id: thread_a }, - availability_timeouts: 0, - ttl: 5, + let entry_a = ParasEntry { + assignment: Assignment::Bulk(para_a), + availability_timeouts: 0 as u32, + ttl: 5 as u32, }; - let thread_entry_b = ParasEntry { - assignment: Assignment { para_id: thread_b }, - availability_timeouts: 0, - ttl: 5, + let entry_b = ParasEntry { + assignment: Assignment::Bulk(para_b), + availability_timeouts: 0 as u32, + ttl: 5 as u32, }; - Scheduler::add_to_claimqueue(CoreIndex(0), thread_entry_a.clone()); + Scheduler::add_to_claimqueue(CoreIndex(0), entry_a.clone()); run_to_block(2, |_| None); @@ -1155,22 +950,22 @@ fn next_up_on_available_uses_next_scheduled_or_none_for_thread() { assert_eq!(Scheduler::availability_cores().len(), 1); let mut map = BTreeMap::new(); - map.insert(CoreIndex(0), thread_a); + map.insert(CoreIndex(0), para_a); Scheduler::occupied(map); let cores = Scheduler::availability_cores(); match &cores[0] { - CoreOccupied::Paras(entry) => assert_eq!(entry, &thread_entry_a), - _ => panic!("with no chains, only core should be a thread core"), + CoreOccupied::Paras(entry) => assert_eq!(entry, &entry_a), + _ => panic!("There should only be one test assigner core"), } assert!(Scheduler::next_up_on_available(CoreIndex(0)).is_none()); - Scheduler::add_to_claimqueue(CoreIndex(0), thread_entry_b); + Scheduler::add_to_claimqueue(CoreIndex(0), entry_b); assert_eq!( Scheduler::next_up_on_available(CoreIndex(0)).unwrap(), - ScheduledCore { para_id: thread_b, collator: None } + ScheduledCore { para_id: para_b, collator: None } ); } }); @@ -1178,25 +973,23 @@ fn next_up_on_available_uses_next_scheduled_or_none_for_thread() { #[test] fn next_up_on_time_out_reuses_claim_if_nothing_queued() { - let mut config = default_config(); - config.on_demand_cores = 1; - - let genesis_config = genesis_config(&config); + let genesis_config = genesis_config(&default_config()); - let thread_a = ParaId::from(1_u32); - let thread_b = ParaId::from(2_u32); + let para_a = ParaId::from(1_u32); + let para_b = ParaId::from(2_u32); - let assignment_a = Assignment { para_id: thread_a }; - let assignment_b = Assignment { para_id: thread_b }; + let assignment_a = Assignment::Bulk(para_a); + let assignment_b = Assignment::Bulk(para_b); new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(thread_a, ParaKind::Parathread); - schedule_blank_para(thread_b, ParaKind::Parathread); + MockAssigner::set_core_count(1); + schedule_blank_para(para_a); + schedule_blank_para(para_b); - // start a new session to activate, 5 validators for 5 cores. + // start a new session to activate, 2 validators for 2 cores. run_to_block(1, |number| match number { 1 => Some(SessionChangeNotification { - new_config: config.clone(), + new_config: default_config(), validators: vec![ ValidatorId::from(Sr25519Keyring::Alice.public()), ValidatorId::from(Sr25519Keyring::Eve.public()), @@ -1206,10 +999,7 @@ fn next_up_on_time_out_reuses_claim_if_nothing_queued() { _ => None, }); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_a.clone(), - QueuePushDirection::Back - )); + MockAssigner::add_test_assignment(assignment_a.clone()); run_to_block(2, |_| None); @@ -1218,150 +1008,62 @@ fn next_up_on_time_out_reuses_claim_if_nothing_queued() { assert_eq!(Scheduler::availability_cores().len(), 1); let mut map = BTreeMap::new(); - map.insert(CoreIndex(0), thread_a); + map.insert(CoreIndex(0), para_a); Scheduler::occupied(map); let cores = Scheduler::availability_cores(); match cores.get(0).unwrap() { - CoreOccupied::Paras(entry) => assert_eq!(entry.assignment, assignment_a.clone()), - _ => panic!("with no chains, only core should be a thread core"), + CoreOccupied::Paras(entry) => { + assert_eq!(entry.assignment, assignment_a.clone()); + }, + _ => panic!("There should only be a single test assigner core"), } // There's nothing more to pop for core 0 from the assignment provider. - assert!( - OnDemandAssigner::pop_assignment_for_core(CoreIndex(0), Some(thread_a)).is_none() - ); + assert!(MockAssigner::pop_assignment_for_core(CoreIndex(0)).is_none()); assert_eq!( Scheduler::next_up_on_time_out(CoreIndex(0)).unwrap(), - ScheduledCore { para_id: thread_a, collator: None } + ScheduledCore { para_id: para_a, collator: None } ); - assert_ok!(OnDemandAssigner::add_on_demand_assignment( - assignment_b.clone(), - QueuePushDirection::Back - )); + MockAssigner::add_test_assignment(assignment_b.clone()); // Pop assignment_b into the claimqueue - Scheduler::update_claimqueue(BTreeMap::new(), 2); + Scheduler::free_cores_and_fill_claimqueue(BTreeMap::new(), 2); //// Now that there is an earlier next-up, we use that. assert_eq!( Scheduler::next_up_on_available(CoreIndex(0)).unwrap(), - ScheduledCore { para_id: thread_b, collator: None } + ScheduledCore { para_id: para_b, collator: None } ); } }); } #[test] -fn next_up_on_available_is_parachain_always() { +fn session_change_requires_reschedule_dropping_removed_paras() { let mut config = default_config(); - config.on_demand_cores = 0; + config.scheduling_lookahead = 1; let genesis_config = genesis_config(&config); - let chain_a = ParaId::from(1_u32); - - new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(chain_a, ParaKind::Parachain); - - // start a new session to activate, 5 validators for 5 cores. - run_to_block(1, |number| match number { - 1 => Some(SessionChangeNotification { - new_config: config.clone(), - validators: vec![ - ValidatorId::from(Sr25519Keyring::Alice.public()), - ValidatorId::from(Sr25519Keyring::Eve.public()), - ], - ..Default::default() - }), - _ => None, - }); - - run_to_block(2, |_| None); - - { - assert_eq!(Scheduler::claimqueue().len(), 1); - assert_eq!(Scheduler::availability_cores().len(), 1); - - Scheduler::occupied(vec![(CoreIndex(0), chain_a)].into_iter().collect()); - - let cores = Scheduler::availability_cores(); - match &cores[0] { - CoreOccupied::Paras(pe) if pe.para_id() == chain_a => {}, - _ => panic!("with no threads, only core should be a chain core"), - } - // Now that there is an earlier next-up, we use that. - assert_eq!( - Scheduler::next_up_on_available(CoreIndex(0)).unwrap(), - ScheduledCore { para_id: chain_a, collator: None } - ); - } - }); -} + let para_a = ParaId::from(1_u32); + let para_b = ParaId::from(2_u32); -#[test] -fn next_up_on_time_out_is_parachain_always() { - let mut config = default_config(); - config.on_demand_cores = 0; - - let genesis_config = genesis_config(&config); - - let chain_a = ParaId::from(1_u32); + let assignment_a = Assignment::Bulk(para_a); + let assignment_b = Assignment::Bulk(para_b); new_test_ext(genesis_config).execute_with(|| { - schedule_blank_para(chain_a, ParaKind::Parachain); - - // start a new session to activate, 5 validators for 5 cores. - run_to_block(1, |number| match number { - 1 => Some(SessionChangeNotification { - new_config: config.clone(), - validators: vec![ - ValidatorId::from(Sr25519Keyring::Alice.public()), - ValidatorId::from(Sr25519Keyring::Eve.public()), - ], - ..Default::default() - }), - _ => None, - }); - - run_to_block(2, |_| None); + // Setting explicit core count + MockAssigner::set_core_count(5); + let assignment_provider_ttl = MockAssigner::get_provider_config(CoreIndex::from(0)).ttl; - { - assert_eq!(Scheduler::claimqueue().len(), 1); - assert_eq!(Scheduler::availability_cores().len(), 1); - - Scheduler::occupied(vec![(CoreIndex(0), chain_a)].into_iter().collect()); - - let cores = Scheduler::availability_cores(); - match &cores[0] { - CoreOccupied::Paras(pe) if pe.para_id() == chain_a => {}, - _ => panic!("Core should be occupied by chain_a ParaId"), - } - - // Now that there is an earlier next-up, we use that. - assert_eq!( - Scheduler::next_up_on_available(CoreIndex(0)).unwrap(), - ScheduledCore { para_id: chain_a, collator: None } - ); - } - }); -} + schedule_blank_para(para_a); + schedule_blank_para(para_b); -#[test] -fn session_change_requires_reschedule_dropping_removed_paras() { - let mut config = default_config(); - config.scheduling_lookahead = 1; - let genesis_config = genesis_config(&config); - - assert_eq!(default_config().on_demand_cores, 3); - new_test_ext(genesis_config).execute_with(|| { - let chain_a = ParaId::from(1_u32); - let chain_b = ParaId::from(2_u32); - - // ensure that we have 5 groups by registering 2 parachains. - schedule_blank_para(chain_a, ParaKind::Parachain); - schedule_blank_para(chain_b, ParaKind::Parachain); + // Add assignments + MockAssigner::add_test_assignment(assignment_a.clone()); + MockAssigner::add_test_assignment(assignment_b.clone()); run_to_block(1, |number| match number { 1 => Some(SessionChangeNotification { @@ -1386,7 +1088,11 @@ fn session_change_requires_reschedule_dropping_removed_paras() { let groups = ValidatorGroups::::get(); assert_eq!(groups.len(), 5); - assert_ok!(Paras::schedule_para_cleanup(chain_b)); + assert_ok!(Paras::schedule_para_cleanup(para_b)); + + // Add assignment + MockAssigner::add_test_assignment(assignment_a.clone()); + run_to_end_of_block(2, |number| match number { 2 => Some(SessionChangeNotification { new_config: default_config(), @@ -1405,17 +1111,17 @@ fn session_change_requires_reschedule_dropping_removed_paras() { _ => None, }); - Scheduler::update_claimqueue(BTreeMap::new(), 3); + Scheduler::free_cores_and_fill_claimqueue(BTreeMap::new(), 3); assert_eq!( Scheduler::claimqueue(), vec![( CoreIndex(0), - vec![Some(ParasEntry::new( - Assignment::new(chain_a), + vec![ParasEntry::new( + Assignment::Bulk(para_a), // At end of block 2 - config.on_demand_ttl + 2 - ))] + assignment_provider_ttl + 2 + )] .into_iter() .collect() )] @@ -1423,8 +1129,12 @@ fn session_change_requires_reschedule_dropping_removed_paras() { .collect() ); - // Add parachain back - schedule_blank_para(chain_b, ParaKind::Parachain); + // Add para back + schedule_blank_para(para_b); + + // Add assignments + MockAssigner::add_test_assignment(assignment_a.clone()); + MockAssigner::add_test_assignment(assignment_b.clone()); run_to_block(3, |number| match number { 3 => Some(SessionChangeNotification { @@ -1449,28 +1159,28 @@ fn session_change_requires_reschedule_dropping_removed_paras() { let groups = ValidatorGroups::::get(); assert_eq!(groups.len(), 5); - Scheduler::update_claimqueue(BTreeMap::new(), 4); + Scheduler::free_cores_and_fill_claimqueue(BTreeMap::new(), 4); assert_eq!( Scheduler::claimqueue(), vec![ ( CoreIndex(0), - vec![Some(ParasEntry::new( - Assignment::new(chain_a), + vec![ParasEntry::new( + Assignment::Bulk(para_a), // At block 3 - config.on_demand_ttl + 3 - ))] + assignment_provider_ttl + 3 + )] .into_iter() .collect() ), ( CoreIndex(1), - vec![Some(ParasEntry::new( - Assignment::new(chain_b), + vec![ParasEntry::new( + Assignment::Bulk(para_b), // At block 3 - config.on_demand_ttl + 3 - ))] + assignment_provider_ttl + 3 + )] .into_iter() .collect() ), diff --git a/polkadot/runtime/parachains/src/session_info/tests.rs b/polkadot/runtime/parachains/src/session_info/tests.rs index 727b7c79fbae..92a50575deda 100644 --- a/polkadot/runtime/parachains/src/session_info/tests.rs +++ b/polkadot/runtime/parachains/src/session_info/tests.rs @@ -62,7 +62,7 @@ fn run_to_block( fn default_config() -> HostConfiguration { HostConfiguration { - on_demand_cores: 1, + coretime_cores: 1, dispute_period: 2, needed_approvals: 3, ..Default::default() diff --git a/polkadot/runtime/rococo/constants/src/lib.rs b/polkadot/runtime/rococo/constants/src/lib.rs index 4e728421b67b..9209045364c2 100644 --- a/polkadot/runtime/rococo/constants/src/lib.rs +++ b/polkadot/runtime/rococo/constants/src/lib.rs @@ -116,6 +116,8 @@ pub mod system_parachain { pub const PEOPLE_ID: u32 = 1004; /// BridgeHub parachain ID. pub const BRIDGE_HUB_ID: u32 = 1013; + /// Brokerage parachain ID. + pub const BROKER_ID: u32 = 1005; /// All system parachains of Rococo. pub type SystemParachains = IsChildSystemParachain; diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index acc3f723cddf..a15911c21f65 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -31,6 +31,7 @@ use primitives::{ OccupiedCoreAssumption, PersistedValidationData, ScrapedOnChainVotes, SessionInfo, Signature, ValidationCode, ValidationCodeHash, ValidatorId, ValidatorIndex, PARACHAIN_KEY_TYPE_ID, }; +use rococo_runtime_constants::system_parachain::BROKER_ID; use runtime_common::{ assigned_slots, auctions, claims, crowdloan, identity_migrator, impl_runtime_weights, impls::{ @@ -43,9 +44,10 @@ use scale_info::TypeInfo; use sp_std::{cmp::Ordering, collections::btree_map::BTreeMap, prelude::*}; use runtime_parachains::{ - assigner as parachains_assigner, assigner_on_demand as parachains_assigner_on_demand, + assigner_coretime as parachains_assigner_coretime, + assigner_on_demand as parachains_assigner_on_demand, assigner_parachains as parachains_assigner_parachains, - configuration as parachains_configuration, disputes as parachains_disputes, + configuration as parachains_configuration, coretime, disputes as parachains_disputes, disputes::slashing as parachains_slashing, dmp as parachains_dmp, hrmp as parachains_hrmp, inclusion as parachains_inclusion, inclusion::{AggregateMessageOrigin, UmpQueueId}, @@ -150,7 +152,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("rococo"), impl_name: create_runtime_str!("parity-rococo-v2.0"), authoring_version: 0, - spec_version: 1_005_000, + spec_version: 1_005_001, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 24, @@ -935,6 +937,7 @@ impl parachains_paras::Config for Runtime { type QueueFootprinter = ParaInclusion; type NextSessionRotation = Babe; type OnNewHead = Registrar; + type AssignCoretime = CoretimeAssignmentProvider; } parameter_types! { @@ -1001,7 +1004,22 @@ impl parachains_paras_inherent::Config for Runtime { } impl parachains_scheduler::Config for Runtime { - type AssignmentProvider = ParaAssignmentProvider; + // If you change this, make sure the `Assignment` type of the new provider is binary compatible, + // otherwise provide a migration. + type AssignmentProvider = CoretimeAssignmentProvider; +} + +parameter_types! { + pub const BrokerId: u32 = BROKER_ID; +} + +impl coretime::Config for Runtime { + type RuntimeOrigin = RuntimeOrigin; + type RuntimeEvent = RuntimeEvent; + type Currency = Balances; + type BrokerId = BrokerId; + type WeightInfo = weights::runtime_parachains_coretime::WeightInfo; + type SendXcm = crate::xcm_config::XcmRouter; } parameter_types! { @@ -1017,15 +1035,13 @@ impl parachains_assigner_on_demand::Config for Runtime { impl parachains_assigner_parachains::Config for Runtime {} -impl parachains_assigner::Config for Runtime { - type OnDemandAssignmentProvider = OnDemandAssignmentProvider; - type ParachainsAssignmentProvider = ParachainsAssignmentProvider; -} +impl parachains_assigner_coretime::Config for Runtime {} impl parachains_initializer::Config for Runtime { type Randomness = pallet_babe::RandomnessFromOneEpochAgo; type ForceOrigin = EnsureRoot; type WeightInfo = weights::runtime_parachains_initializer::WeightInfo; + type CoretimeOnNewSession = Coretime; } impl parachains_disputes::Config for Runtime { @@ -1405,15 +1421,16 @@ construct_runtime! { ParasDisputes: parachains_disputes::{Pallet, Call, Storage, Event} = 62, ParasSlashing: parachains_slashing::{Pallet, Call, Storage, ValidateUnsigned} = 63, MessageQueue: pallet_message_queue::{Pallet, Call, Storage, Event} = 64, - ParaAssignmentProvider: parachains_assigner::{Pallet, Storage} = 65, OnDemandAssignmentProvider: parachains_assigner_on_demand::{Pallet, Call, Storage, Event} = 66, ParachainsAssignmentProvider: parachains_assigner_parachains::{Pallet} = 67, + CoretimeAssignmentProvider: parachains_assigner_coretime::{Pallet, Storage} = 68, // Parachain Onboarding Pallets. Start indices at 70 to leave room. Registrar: paras_registrar::{Pallet, Call, Storage, Event, Config} = 70, Slots: slots::{Pallet, Call, Storage, Event} = 71, Auctions: auctions::{Pallet, Call, Storage, Event} = 72, Crowdloan: crowdloan::{Pallet, Call, Storage, Event} = 73, + Coretime: coretime::{Pallet, Call, Event} = 74, // Pallet for sending XCM. XcmPallet: pallet_xcm::{Pallet, Call, Storage, Event, Origin, Config} = 99, @@ -1477,9 +1494,39 @@ pub mod migrations { use frame_support::traits::LockIdentifier; use frame_system::pallet_prelude::BlockNumberFor; + use sp_arithmetic::traits::Zero; #[cfg(feature = "try-runtime")] use sp_core::crypto::ByteArray; + pub struct GetLegacyLeaseImpl; + impl coretime::migration::GetLegacyLease for GetLegacyLeaseImpl { + fn get_parachain_lease_in_blocks(para: ParaId) -> Option { + let now = frame_system::Pallet::::block_number(); + let mut leases = slots::Pallet::::lease(para).into_iter(); + let initial_sum = if let Some(Some(_)) = leases.next() { + let (_, progress) = + slots::Pallet::::lease_period_index_plus_progress(now)?; + LeasePeriod::get().saturating_sub(progress) + } else { + // The parachain lease did not yet start + Zero::zero() + }; + log::trace!( + target: "coretime-migration", + "Getting lease info for para {:?}:\n LEASE_PERIOD: {:?}, initial_sum: {:?}, number of leases: {:?}", + para, + LeasePeriod::get(), + initial_sum, + slots::Pallet::::lease(para).len(), + ); + + Some(leases.into_iter().fold(initial_sum, |sum, lease| { + // If the parachain lease did not yet start, we ignore them by multiplying by `0`. + sum + LeasePeriod::get() * lease.map_or(0, |_| 1) + })) + } + } + parameter_types! { pub const DemocracyPalletName: &'static str = "Democracy"; pub const CouncilPalletName: &'static str = "Council"; @@ -1602,7 +1649,7 @@ pub mod migrations { pallet_society::migrations::MigrateToV2, parachains_configuration::migration::v7::MigrateToV7, assigned_slots::migration::v1::MigrateToV1, - parachains_scheduler::migration::v1::MigrateToV1, + parachains_scheduler::migration::MigrateV1ToV2, parachains_configuration::migration::v8::MigrateToV8, parachains_configuration::migration::v9::MigrateToV9, paras_registrar::migration::MigrateToV1, @@ -1633,6 +1680,8 @@ pub mod migrations { // Remove `im-online` pallet on-chain storage frame_support::migrations::RemovePallet::DbWeight>, parachains_configuration::migration::v11::MigrateToV11, + // This needs to come after the `parachains_configuration` above as we are reading the configuration. + coretime::migration::MigrateToCoretime, ); } @@ -1681,6 +1730,7 @@ mod benches { // the that path resolves correctly in the generated file. [runtime_common::assigned_slots, AssignedSlots] [runtime_common::auctions, Auctions] + [runtime_common::coretime, Coretime] [runtime_common::crowdloan, Crowdloan] [runtime_common::claims, Claims] [runtime_common::identity_migrator, IdentityMigrator] diff --git a/polkadot/runtime/rococo/src/weights/mod.rs b/polkadot/runtime/rococo/src/weights/mod.rs index bd2079ce8277..3613fb4305ba 100644 --- a/polkadot/runtime/rococo/src/weights/mod.rs +++ b/polkadot/runtime/rococo/src/weights/mod.rs @@ -51,6 +51,7 @@ pub mod runtime_common_paras_registrar; pub mod runtime_common_slots; pub mod runtime_parachains_assigner_on_demand; pub mod runtime_parachains_configuration; +pub mod runtime_parachains_coretime; pub mod runtime_parachains_disputes; pub mod runtime_parachains_hrmp; pub mod runtime_parachains_inclusion; diff --git a/polkadot/runtime/rococo/src/weights/runtime_parachains_coretime.rs b/polkadot/runtime/rococo/src/weights/runtime_parachains_coretime.rs new file mode 100644 index 000000000000..d9f2d45207b9 --- /dev/null +++ b/polkadot/runtime/rococo/src/weights/runtime_parachains_coretime.rs @@ -0,0 +1,73 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Autogenerated weights for `runtime_parachains::coretime` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-12-01, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `runner-r43aesjn-project-674-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz` +//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("rococo-dev")`, DB CACHE: 1024 + +// Executed Command: +// target/production/polkadot +// benchmark +// pallet +// --steps=50 +// --repeat=20 +// --extrinsic=* +// --wasm-execution=compiled +// --heap-pages=4096 +// --json-file=/builds/parity/mirrors/polkadot-sdk/.git/.artifacts/bench.json +// --pallet=runtime_common::coretime +// --chain=rococo-dev +// --header=./polkadot/file_header.txt +// --output=./polkadot/runtime/rococo/src/weights/ + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(missing_docs)] + +use frame_support::{traits::Get, weights::Weight}; +use core::marker::PhantomData; + +use runtime_parachains::configuration::{self, WeightInfo as ConfigWeightInfo}; + +/// Weight functions for `runtime_common::coretime`. +pub struct WeightInfo(PhantomData); +impl runtime_parachains::coretime::WeightInfo for WeightInfo { + fn request_core_count() -> Weight { + ::WeightInfo::set_config_with_u32() + } + /// Storage: `CoreTimeAssignmentProvider::CoreDescriptors` (r:1 w:1) + /// Proof: `CoreTimeAssignmentProvider::CoreDescriptors` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `CoreTimeAssignmentProvider::CoreSchedules` (r:0 w:1) + /// Proof: `CoreTimeAssignmentProvider::CoreSchedules` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `s` is `[1, 100]`. + fn assign_core(s: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `76` + // Estimated: `3541` + // Minimum execution time: 6_275_000 picoseconds. + Weight::from_parts(6_883_543, 0) + .saturating_add(Weight::from_parts(0, 3541)) + // Standard Error: 202 + .saturating_add(Weight::from_parts(15_028, 0).saturating_mul(s.into())) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(2)) + } +} diff --git a/polkadot/runtime/rococo/src/xcm_config.rs b/polkadot/runtime/rococo/src/xcm_config.rs index 05b32c325fd5..f3a2ca6f3a6c 100644 --- a/polkadot/runtime/rococo/src/xcm_config.rs +++ b/polkadot/runtime/rococo/src/xcm_config.rs @@ -122,6 +122,7 @@ parameter_types! { pub const Encointer: MultiLocation = Parachain(ENCOINTER_ID).into_location(); pub const BridgeHub: MultiLocation = Parachain(BRIDGE_HUB_ID).into_location(); pub const People: MultiLocation = Parachain(PEOPLE_ID).into_location(); + pub const Broker: MultiLocation = Parachain(BROKER_ID).into_location(); pub const Tick: MultiLocation = Parachain(100).into_location(); pub const Trick: MultiLocation = Parachain(110).into_location(); pub const Track: MultiLocation = Parachain(120).into_location(); @@ -133,6 +134,7 @@ parameter_types! { pub const RocForEncointer: (MultiAssetFilter, MultiLocation) = (Roc::get(), Encointer::get()); pub const RocForBridgeHub: (MultiAssetFilter, MultiLocation) = (Roc::get(), BridgeHub::get()); pub const RocForPeople: (MultiAssetFilter, MultiLocation) = (Roc::get(), People::get()); + pub const RocForBroker: (MultiAssetFilter, MultiLocation) = (Roc::get(), Broker::get()); pub const MaxInstructions: u32 = 100; pub const MaxAssetsIntoHolding: u32 = 64; } @@ -145,6 +147,7 @@ pub type TrustedTeleporters = ( xcm_builder::Case, xcm_builder::Case, xcm_builder::Case, + xcm_builder::Case, ); match_types! { diff --git a/polkadot/runtime/test-runtime/src/lib.rs b/polkadot/runtime/test-runtime/src/lib.rs index cd9590796ae0..f472b619ba75 100644 --- a/polkadot/runtime/test-runtime/src/lib.rs +++ b/polkadot/runtime/test-runtime/src/lib.rs @@ -74,7 +74,7 @@ use sp_runtime::{ SaturatedConversion, StaticLookup, Verify, }, transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity}, - ApplyExtrinsicResult, KeyTypeId, Perbill, + ApplyExtrinsicResult, FixedU128, KeyTypeId, Perbill, }; use sp_staking::SessionIndex; #[cfg(any(feature = "std", test))] @@ -520,6 +520,7 @@ impl parachains_initializer::Config for Runtime { type Randomness = pallet_babe::RandomnessFromOneEpochAgo; type ForceOrigin = frame_system::EnsureRoot; type WeightInfo = (); + type CoretimeOnNewSession = (); } impl parachains_session_info::Config for Runtime { @@ -537,6 +538,15 @@ impl parachains_paras::Config for Runtime { type QueueFootprinter = ParaInclusion; type NextSessionRotation = Babe; type OnNewHead = (); + type AssignCoretime = (); +} + +parameter_types! { + pub const BrokerId: u32 = 10u32; +} + +parameter_types! { + pub const OnDemandTrafficDefaultValue: FixedU128 = FixedU128::from_u32(1); } impl parachains_dmp::Config for Runtime {} diff --git a/polkadot/runtime/westend/constants/src/lib.rs b/polkadot/runtime/westend/constants/src/lib.rs index adcbfd775637..848cccd559dc 100644 --- a/polkadot/runtime/westend/constants/src/lib.rs +++ b/polkadot/runtime/westend/constants/src/lib.rs @@ -109,6 +109,8 @@ pub mod system_parachain { pub const BRIDGE_HUB_ID: u32 = 1002; /// People Chain parachain ID. pub const PEOPLE_ID: u32 = 1004; + /// Brokerage parachain ID. + pub const BROKER_ID: u32 = 1005; /// All system parachains of Westend. pub type SystemParachains = IsChildSystemParachain; diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index e958a660e6ec..fb54bec509b3 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -115,7 +115,7 @@ use sp_runtime::traits::Get; pub use sp_runtime::BuildStorage; /// Constant values used within the runtime. -use westend_runtime_constants::{currency::*, fee::*, time::*}; +use westend_runtime_constants::{currency::*, fee::*, system_parachain::BROKER_ID, time::*}; mod bag_thresholds; mod weights; @@ -1149,6 +1149,7 @@ impl parachains_paras::Config for Runtime { type QueueFootprinter = ParaInclusion; type NextSessionRotation = Babe; type OnNewHead = (); + type AssignCoretime = (); } parameter_types! { @@ -1215,7 +1216,13 @@ impl parachains_paras_inherent::Config for Runtime { } impl parachains_scheduler::Config for Runtime { - type AssignmentProvider = ParaAssignmentProvider; + // If you change this, make sure the `Assignment` type of the new provider is binary compatible, + // otherwise provide a migration. + type AssignmentProvider = ParachainsAssignmentProvider; +} + +parameter_types! { + pub const BrokerId: u32 = BROKER_ID; } impl parachains_assigner_parachains::Config for Runtime {} @@ -1224,6 +1231,7 @@ impl parachains_initializer::Config for Runtime { type Randomness = pallet_babe::RandomnessFromOneEpochAgo; type ForceOrigin = EnsureRoot; type WeightInfo = weights::runtime_parachains_initializer::WeightInfo; + type CoretimeOnNewSession = (); } impl paras_sudo_wrapper::Config for Runtime {} @@ -1485,7 +1493,7 @@ construct_runtime! { ParaSessionInfo: parachains_session_info::{Pallet, Storage} = 52, ParasDisputes: parachains_disputes::{Pallet, Call, Storage, Event} = 53, ParasSlashing: parachains_slashing::{Pallet, Call, Storage, ValidateUnsigned} = 54, - ParaAssignmentProvider: parachains_assigner_parachains::{Pallet, Storage} = 55, + ParachainsAssignmentProvider: parachains_assigner_parachains::{Pallet} = 55, // Parachain Onboarding Pallets. Start indices at 60 to leave room. Registrar: paras_registrar::{Pallet, Call, Storage, Event, Config} = 60, @@ -1636,7 +1644,7 @@ pub mod migrations { parachains_configuration::migration::v7::MigrateToV7, pallet_staking::migrations::v14::MigrateToV14, assigned_slots::migration::v1::MigrateToV1, - parachains_scheduler::migration::v1::MigrateToV1, + parachains_scheduler::migration::MigrateV1ToV2, parachains_configuration::migration::v8::MigrateToV8, parachains_configuration::migration::v9::MigrateToV9, paras_registrar::migration::MigrateToV1, diff --git a/polkadot/runtime/westend/src/weights/mod.rs b/polkadot/runtime/westend/src/weights/mod.rs index 3841579088a9..d8a2ae5d2da6 100644 --- a/polkadot/runtime/westend/src/weights/mod.rs +++ b/polkadot/runtime/westend/src/weights/mod.rs @@ -49,7 +49,9 @@ pub mod runtime_common_crowdloan; pub mod runtime_common_identity_migrator; pub mod runtime_common_paras_registrar; pub mod runtime_common_slots; +pub mod runtime_parachains_assigner_on_demand; pub mod runtime_parachains_configuration; +pub mod runtime_parachains_coretime; pub mod runtime_parachains_disputes; pub mod runtime_parachains_disputes_slashing; pub mod runtime_parachains_hrmp; diff --git a/polkadot/runtime/westend/src/weights/runtime_parachains_assigner_on_demand.rs b/polkadot/runtime/westend/src/weights/runtime_parachains_assigner_on_demand.rs new file mode 100644 index 000000000000..ac0f05301b48 --- /dev/null +++ b/polkadot/runtime/westend/src/weights/runtime_parachains_assigner_on_demand.rs @@ -0,0 +1,91 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Autogenerated weights for `runtime_parachains::assigner_on_demand` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-08-11, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `runner-fljshgub-project-163-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz` +//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("rococo-dev")`, DB CACHE: 1024 + +// Executed Command: +// target/production/polkadot +// benchmark +// pallet +// --steps=50 +// --repeat=20 +// --extrinsic=* +// --wasm-execution=compiled +// --heap-pages=4096 +// --json-file=/builds/parity/mirrors/polkadot/.git/.artifacts/bench.json +// --pallet=runtime_parachains::assigner_on_demand +// --chain=rococo-dev +// --header=./file_header.txt +// --output=./runtime/rococo/src/weights/ + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(missing_docs)] + +use frame_support::{traits::Get, weights::Weight}; +use core::marker::PhantomData; + +/// Weight functions for `runtime_parachains::assigner_on_demand`. +pub struct WeightInfo(PhantomData); +impl runtime_parachains::assigner_on_demand::WeightInfo for WeightInfo { + /// Storage: `OnDemandAssignmentProvider::SpotTraffic` (r:1 w:0) + /// Proof: `OnDemandAssignmentProvider::SpotTraffic` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `Paras::ParaLifecycles` (r:1 w:0) + /// Proof: `Paras::ParaLifecycles` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `OnDemandAssignmentProvider::OnDemandQueue` (r:1 w:1) + /// Proof: `OnDemandAssignmentProvider::OnDemandQueue` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// The range of component `s` is `[1, 9999]`. + fn place_order_keep_alive(s: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `297 + s * (4 ±0)` + // Estimated: `3762 + s * (4 ±0)` + // Minimum execution time: 33_522_000 picoseconds. + Weight::from_parts(35_436_835, 0) + .saturating_add(Weight::from_parts(0, 3762)) + // Standard Error: 129 + .saturating_add(Weight::from_parts(14_041, 0).saturating_mul(s.into())) + .saturating_add(T::DbWeight::get().reads(3)) + .saturating_add(T::DbWeight::get().writes(1)) + .saturating_add(Weight::from_parts(0, 4).saturating_mul(s.into())) + } + /// Storage: `OnDemandAssignmentProvider::SpotTraffic` (r:1 w:0) + /// Proof: `OnDemandAssignmentProvider::SpotTraffic` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `Paras::ParaLifecycles` (r:1 w:0) + /// Proof: `Paras::ParaLifecycles` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `OnDemandAssignmentProvider::OnDemandQueue` (r:1 w:1) + /// Proof: `OnDemandAssignmentProvider::OnDemandQueue` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// The range of component `s` is `[1, 9999]`. + fn place_order_allow_death(s: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `297 + s * (4 ±0)` + // Estimated: `3762 + s * (4 ±0)` + // Minimum execution time: 33_488_000 picoseconds. + Weight::from_parts(34_848_934, 0) + .saturating_add(Weight::from_parts(0, 3762)) + // Standard Error: 143 + .saturating_add(Weight::from_parts(14_215, 0).saturating_mul(s.into())) + .saturating_add(T::DbWeight::get().reads(3)) + .saturating_add(T::DbWeight::get().writes(1)) + .saturating_add(Weight::from_parts(0, 4).saturating_mul(s.into())) + } +} diff --git a/polkadot/runtime/westend/src/weights/runtime_parachains_coretime.rs b/polkadot/runtime/westend/src/weights/runtime_parachains_coretime.rs new file mode 100644 index 000000000000..d9f2d45207b9 --- /dev/null +++ b/polkadot/runtime/westend/src/weights/runtime_parachains_coretime.rs @@ -0,0 +1,73 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Autogenerated weights for `runtime_parachains::coretime` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-12-01, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `runner-r43aesjn-project-674-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz` +//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("rococo-dev")`, DB CACHE: 1024 + +// Executed Command: +// target/production/polkadot +// benchmark +// pallet +// --steps=50 +// --repeat=20 +// --extrinsic=* +// --wasm-execution=compiled +// --heap-pages=4096 +// --json-file=/builds/parity/mirrors/polkadot-sdk/.git/.artifacts/bench.json +// --pallet=runtime_common::coretime +// --chain=rococo-dev +// --header=./polkadot/file_header.txt +// --output=./polkadot/runtime/rococo/src/weights/ + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] +#![allow(missing_docs)] + +use frame_support::{traits::Get, weights::Weight}; +use core::marker::PhantomData; + +use runtime_parachains::configuration::{self, WeightInfo as ConfigWeightInfo}; + +/// Weight functions for `runtime_common::coretime`. +pub struct WeightInfo(PhantomData); +impl runtime_parachains::coretime::WeightInfo for WeightInfo { + fn request_core_count() -> Weight { + ::WeightInfo::set_config_with_u32() + } + /// Storage: `CoreTimeAssignmentProvider::CoreDescriptors` (r:1 w:1) + /// Proof: `CoreTimeAssignmentProvider::CoreDescriptors` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `CoreTimeAssignmentProvider::CoreSchedules` (r:0 w:1) + /// Proof: `CoreTimeAssignmentProvider::CoreSchedules` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `s` is `[1, 100]`. + fn assign_core(s: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `76` + // Estimated: `3541` + // Minimum execution time: 6_275_000 picoseconds. + Weight::from_parts(6_883_543, 0) + .saturating_add(Weight::from_parts(0, 3541)) + // Standard Error: 202 + .saturating_add(Weight::from_parts(15_028, 0).saturating_mul(s.into())) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(2)) + } +} diff --git a/polkadot/zombienet_tests/functional/0005-parachains-disputes-past-session.zndsl b/polkadot/zombienet_tests/functional/0005-parachains-disputes-past-session.zndsl index a3f1f0669ac9..d92820391d53 100644 --- a/polkadot/zombienet_tests/functional/0005-parachains-disputes-past-session.zndsl +++ b/polkadot/zombienet_tests/functional/0005-parachains-disputes-past-session.zndsl @@ -32,6 +32,10 @@ honest-flaky-validator-1: reports parachain_candidate_disputes_total is at least honest-flaky-validator-1: pause # Wait for 1 full session to pass after the last unconcluded dispute. +# +# TODO: replace with assertion for "New session detected" in logs. I think that +# would match on previous log lines, so we may need to programmatically wait for +# a specific session, requiring zombienet v2. sleep 110 seconds # Now resume flaky validators diff --git a/polkadot/zombienet_tests/smoke/0004-configure-broker.js b/polkadot/zombienet_tests/smoke/0004-configure-broker.js new file mode 100644 index 000000000000..a4939ffe1cb8 --- /dev/null +++ b/polkadot/zombienet_tests/smoke/0004-configure-broker.js @@ -0,0 +1,66 @@ +const assert = require("assert"); + +async function run(nodeName, networkInfo, _jsArgs) { + const { wsUri, userDefinedTypes } = networkInfo.nodesByName[nodeName]; + const api = await zombie.connect(wsUri, userDefinedTypes); + + await zombie.util.cryptoWaitReady(); + + // account to submit tx + const keyring = new zombie.Keyring({ type: "sr25519" }); + const alice = keyring.addFromUri("//Alice"); + + const calls = [ + // Default broker configuration + api.tx.broker.configure({ + advanceNotice: 2, + interludeLength: 1, + leadinLength: 1, + regionLength: 3, + idealBulkProportion: 100, + limitCoresOffered: null, + renewalBump: 10, + contributionTimeout: 5, + }), + // Make reservation for ParaId 100 (adder-a) every other block + // and ParaId 101 (adder-b) every other block. + api.tx.broker.reserve([ + { + mask: [255, 0, 255, 0, 255, 0, 255, 0, 255, 0], + assignment: { Task: 100 }, + }, + { + mask: [0, 255, 0, 255, 0, 255, 0, 255, 0, 255], + assignment: { Task: 101 }, + }, + ]), + // Start sale with 1 core starting at 1 planck + api.tx.broker.startSales(1, 1), + ]; + const sudo_batch = api.tx.sudo.sudo(api.tx.utility.batch(calls)); + + await new Promise(async (resolve, reject) => { + const unsub = await sudo_batch.signAndSend(alice, (result) => { + console.log(`Current status is ${result.status}`); + if (result.status.isInBlock) { + console.log( + `Transaction included at blockHash ${result.status.asInBlock}` + ); + } else if (result.status.isFinalized) { + console.log( + `Transaction finalized at blockHash ${result.status.asFinalized}` + ); + unsub(); + return resolve(); + } else if (result.isError) { + console.log(`Transaction Error`); + unsub(); + return reject(); + } + }); + }); + + return 0; +} + +module.exports = { run }; diff --git a/polkadot/zombienet_tests/smoke/0004-configure-relay.js b/polkadot/zombienet_tests/smoke/0004-configure-relay.js new file mode 100644 index 000000000000..9ca23d86a561 --- /dev/null +++ b/polkadot/zombienet_tests/smoke/0004-configure-relay.js @@ -0,0 +1,43 @@ +const assert = require("assert"); + +async function run(nodeName, networkInfo, _jsArgs) { + const { wsUri, userDefinedTypes } = networkInfo.nodesByName[nodeName]; + const api = await zombie.connect(wsUri, userDefinedTypes); + + await zombie.util.cryptoWaitReady(); + + // account to submit tx + const keyring = new zombie.Keyring({ type: "sr25519" }); + const alice = keyring.addFromUri("//Alice"); + + const calls = [ + api.tx.configuration.setCoretimeCores({ new: 1 }), + api.tx.coretime.assignCore(0, 20,[[ { task: 1005 }, 57600 ]], null) + ]; + const sudo_batch = api.tx.sudo.sudo(api.tx.utility.batch(calls)); + + await new Promise(async (resolve, reject) => { + const unsub = await sudo_batch.signAndSend(alice, (result) => { + console.log(`Current status is ${result.status}`); + if (result.status.isInBlock) { + console.log( + `Transaction included at blockHash ${result.status.asInBlock}` + ); + } else if (result.status.isFinalized) { + console.log( + `Transaction finalized at blockHash ${result.status.asFinalized}` + ); + unsub(); + return resolve(); + } else if (result.isError) { + console.log(`Transaction Error`); + unsub(); + return reject(); + } + }); + }); + + return 0; +} + +module.exports = { run }; diff --git a/polkadot/zombienet_tests/smoke/0004-coretime-smoke-test.toml b/polkadot/zombienet_tests/smoke/0004-coretime-smoke-test.toml new file mode 100644 index 000000000000..3bcd0bee3c71 --- /dev/null +++ b/polkadot/zombienet_tests/smoke/0004-coretime-smoke-test.toml @@ -0,0 +1,58 @@ +[settings] +timeout = 1000 + +[relaychain] +default_image = "{{ZOMBIENET_INTEGRATION_TEST_IMAGE}}" +chain = "rococo-local" +command = "polkadot" + + [[relaychain.nodes]] + name = "alice" + args = ["-lruntime=debug,parachain=trace" ] + + [[relaychain.nodes]] + name = "bob" + args = ["-lruntime=debug,parachain=trace" ] + + [[relaychain.nodes]] + name = "charlie" + args = ["-lruntime=debug,parachain=trace" ] + +[[parachains]] +id = 1005 +chain = "coretime-rococo-local" + + [parachains.collator] + name = "coretime-collator" + image = "{{COL_IMAGE}}" + command = "polkadot-parachain" + args = [ "-lruntime=debug,parachain=trace" ] + +[[parachains]] +id = 100 +add_to_genesis = false +register_para = true +onboard_as_parachain = false + + [parachains.collator] + name = "adder-a" + image = "{{COL_IMAGE}}" + command = "adder-collator" + args = [ "-lruntime=debug,parachain=trace" ] + +[[parachains]] +id = 101 +add_to_genesis = false +register_para = true +onboard_as_parachain = false + + [parachains.collator] + name = "adder-b" + image = "{{COL_IMAGE}}" + command = "adder-collator" + args = [ "-lruntime=debug,parachain=trace" ] + +[types.Header] +number = "u64" +parent_hash = "Hash" +post_state = "Hash" diff --git a/polkadot/zombienet_tests/smoke/0004-coretime-smoke-test.zndsl b/polkadot/zombienet_tests/smoke/0004-coretime-smoke-test.zndsl new file mode 100644 index 000000000000..45e000e0bf85 --- /dev/null +++ b/polkadot/zombienet_tests/smoke/0004-coretime-smoke-test.zndsl @@ -0,0 +1,19 @@ +Description: Bulk core assignment Smoke +Network: ./0004-coretime-smoke-test.toml +Creds: config + +alice: is up +coretime-collator: is up + +alice: reports block height is at least 3 within 30 seconds +# configure relay chain +alice: js-script ./0004-configure-relay.js with "" return is 0 within 600 secs + +# Wait 2 sessions. The parachain doesn't start block production immediately. +alice: log line contains "New session detected session_index=2" within 600 seconds + +# configure broker chain +coretime-collator: js-script ./0004-configure-broker.js with "" return is 0 within 600 secs + +# TODO: Fix this +# alice: parachain 100 block height is at least 10 within 600 seconds diff --git a/prdoc/pr_1694.prdoc b/prdoc/pr_1694.prdoc new file mode 100644 index 000000000000..24797630efc9 --- /dev/null +++ b/prdoc/pr_1694.prdoc @@ -0,0 +1,24 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: Agile Coretime Base Relaychain Functionality + +doc: + - audience: Runtime User + description: | + The relay chain is now capable of receiving assignments from the coretime + chain and will schedule parachains and on-demand orders accordingly. + Existing leases and system chains are preserved. They get a reserved + coretime core via a migration. +migrations: + db: [] + runtime: + - reference: polkadot-runtime-parachains + description: | + Claim queue in scheduler now no longer contains Option values and + assignments now contain information necessary to accomodate for coretime + features. Also all existing parachains are converted to coretime + assignments. + +crates: + - name: polkadot-runtime-parachains diff --git a/prdoc/pr_2522.prdoc b/prdoc/pr_2522.prdoc new file mode 100644 index 000000000000..9a98f984bacb --- /dev/null +++ b/prdoc/pr_2522.prdoc @@ -0,0 +1,12 @@ +title: "Adds Snowbridge to Rococo runtime" + +doc: + - audience: Runtime Dev + description: | + Adds the snowbridge pallets as a git subtree under the bridges directory. Adds Snowbridge + to the Rococo Asset Hub and Bridge Hub runtimes. + + +crates: + - name: asset-hub-rococo-runtime + - name: bridge-hub-rococo-runtime diff --git a/prdoc/pr_2783.prdoc b/prdoc/pr_2783.prdoc new file mode 100644 index 000000000000..0e4c99065414 --- /dev/null +++ b/prdoc/pr_2783.prdoc @@ -0,0 +1,12 @@ +title: "Accept Root origin as valid sudo" + +doc: + - audience: Runtime User + description: | + Dispatchables of `pallet-sudo` will now also accept the `Root` origin + as valid `sudo` origin. This enhancement is useful for parachains that + allow the relay chain as a superuser. It enables the relay chain to send + an XCM message to initialize the sudo key. + +crates: + - name: "pallet-sudo" diff --git a/scripts/snowbridge_update_subtree.sh b/scripts/snowbridge_update_subtree.sh new file mode 100755 index 000000000000..2276bb35469f --- /dev/null +++ b/scripts/snowbridge_update_subtree.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +# A script to udpate bridges repo as subtree to Cumulus +# Usage: +# ./scripts/update_subtree_snowbridge.sh fetch +# ./scripts/update_subtree_snowbridge.sh patch + +set -e + +SNOWBRIDGE_BRANCH="${SNOWBRIDGE_BRANCH:-main}" +POLKADOT_SDK_BRANCH="${POLKADOT_SDK_BRANCH:-master}" +SNOWBRIDGE_TARGET_DIR="${TARGET_DIR:-bridges/snowbridge}" + +function fetch() { + # the script is able to work only on clean git copy + [[ -z "$(git status --porcelain)" ]] || { + echo >&2 "The git copy must be clean (stash all your changes):"; + git status --porcelain + exit 1; + } + + local snowbridge_remote=$(git remote -v | grep "snowbridge.git (fetch)" | head -n1 | awk '{print $1;}') + if [ -z "$snowbridge_remote" ]; then + echo "Adding new remote: 'snowbridge' repo..." + git remote add -f snowbridge https://github.com/Snowfork/snowbridge.git + snowbridge_remote="snowbridge" + else + echo "Fetching remote: '${snowbridge_remote}' repo..." + git fetch https://github.com/Snowfork/snowbridge.git --prune + fi + + echo "Syncing/updating subtree with remote branch '${snowbridge_remote}/$SNOWBRIDGE_BRANCH' to target directory: '$SNOWBRIDGE_TARGET_DIR'" + git subtree pull --prefix=$SNOWBRIDGE_TARGET_DIR ${snowbridge_remote} $SNOWBRIDGE_BRANCH --squash +} + +function clean() { + echo "Patching/removing unneeded stuff from subtree in target directory: '$SNOWBRIDGE_TARGET_DIR'" + chmod +x $SNOWBRIDGE_TARGET_DIR/parachain/scripts/verify-pallets-build.sh + $SNOWBRIDGE_TARGET_DIR/parachain/scripts/verify-pallets-build.sh --ignore-git-state --no-revert +} + +function create_patch() { + [[ -z "$(git status --porcelain)" ]] || { + echo >&2 "The git copy must be clean (stash all your changes):"; + git status --porcelain + exit 1; + } + echo "Creating diff patch file to apply to snowbridge. No Cargo.toml files will be included in the patch." + git diff snowbridge/$SNOWBRIDGE_BRANCH $POLKADOT_SDK_BRANCH:bridges/snowbridge --diff-filter=ACM -- . ':(exclude)*/Cargo.toml' > snowbridge.patch +} + +case "$1" in + fetch) + fetch + ;; + clean) + clean + ;; + create_patch) + create_patch + ;; + update) + fetch + clean + ;; +esac diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 849bc7ca6fb9..4d409a791ba2 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -1993,7 +1993,6 @@ impl OnUnbalanced> for IntoAuthor { } parameter_types! { - pub storage CoreCount: Option = None; pub storage CoretimeRevenue: Option<(BlockNumber, Balance)> = None; } @@ -2012,21 +2011,12 @@ impl CoretimeInterface for CoretimeProvider { _end_hint: Option, ) { } - fn check_notify_core_count() -> Option { - let count = CoreCount::get(); - CoreCount::set(&None); - count - } fn check_notify_revenue_info() -> Option<(u32, Self::Balance)> { let revenue = CoretimeRevenue::get(); CoretimeRevenue::set(&None); revenue } #[cfg(feature = "runtime-benchmarks")] - fn ensure_notify_core_count(count: u16) { - CoreCount::set(&Some(count)); - } - #[cfg(feature = "runtime-benchmarks")] fn ensure_notify_revenue_info(when: u32, revenue: Self::Balance) { CoretimeRevenue::set(&Some((when, revenue))); } diff --git a/substrate/client/chain-spec/src/chain_spec.rs b/substrate/client/chain-spec/src/chain_spec.rs index 8d97d9410229..fe8fcfda216e 100644 --- a/substrate/client/chain-spec/src/chain_spec.rs +++ b/substrate/client/chain-spec/src/chain_spec.rs @@ -784,9 +784,7 @@ fn json_eval_value_at_key( path: &mut VecDeque<&str>, fun: &dyn Fn(&json::Value) -> bool, ) -> bool { - let Some(key) = path.pop_front() else { - return false; - }; + let Some(key) = path.pop_front() else { return false }; if path.is_empty() { doc.as_object().map_or(false, |o| o.get(key).map_or(false, |v| fun(v))) diff --git a/substrate/frame/assets/src/functions.rs b/substrate/frame/assets/src/functions.rs index c2c1b6839060..8791aaa736b3 100644 --- a/substrate/frame/assets/src/functions.rs +++ b/substrate/frame/assets/src/functions.rs @@ -77,7 +77,7 @@ impl, I: 'static> Pallet { } } else if d.is_sufficient { frame_system::Pallet::::inc_sufficients(who); - d.sufficients += 1; + d.sufficients.saturating_inc(); ExistenceReason::Sufficient } else { frame_system::Pallet::::inc_consumers(who) diff --git a/substrate/frame/broker/src/benchmarking.rs b/substrate/frame/broker/src/benchmarking.rs index 2cb4826f4acc..c57c4ccb8ce4 100644 --- a/substrate/frame/broker/src/benchmarking.rs +++ b/substrate/frame/broker/src/benchmarking.rs @@ -705,7 +705,7 @@ mod benches { let core_count = n.try_into().unwrap(); - ::ensure_notify_core_count(core_count); + CoreCountInbox::::put(core_count); let mut status = Status::::get().ok_or(BenchmarkError::Weightless)?; diff --git a/substrate/frame/broker/src/coretime_interface.rs b/substrate/frame/broker/src/coretime_interface.rs index 9f23561ce945..9e853e8f3fe0 100644 --- a/substrate/frame/broker/src/coretime_interface.rs +++ b/substrate/frame/broker/src/coretime_interface.rs @@ -107,11 +107,6 @@ pub trait CoretimeInterface { end_hint: Option>, ); - /// Indicate that from this block onwards, the range of acceptable values of the `core` - /// parameter of `assign_core` message is `[0, count)`. `assign_core` will be a no-op if - /// provided with a value for `core` outside of this range. - fn check_notify_core_count() -> Option; - /// Provide the amount of revenue accumulated from Instantaneous Coretime Sales from Relay-chain /// block number `last_until` to `until`, not including `until` itself. `last_until` is defined /// as being the `until` argument of the last `notify_revenue` message sent, or zero for the @@ -123,12 +118,6 @@ pub trait CoretimeInterface { /// single revenue information destination exists. fn check_notify_revenue_info() -> Option<(RCBlockNumberOf, Self::Balance)>; - /// Ensure that core count is updated to the provided value. - /// - /// This is only used for benchmarking. - #[cfg(feature = "runtime-benchmarks")] - fn ensure_notify_core_count(count: u16); - /// Ensure that revenue information is updated to the provided value. /// /// This is only used for benchmarking. @@ -151,14 +140,9 @@ impl CoretimeInterface for () { _end_hint: Option>, ) { } - fn check_notify_core_count() -> Option { - None - } fn check_notify_revenue_info() -> Option<(RCBlockNumberOf, Self::Balance)> { None } #[cfg(feature = "runtime-benchmarks")] - fn ensure_notify_core_count(_count: u16) {} - #[cfg(feature = "runtime-benchmarks")] fn ensure_notify_revenue_info(_when: RCBlockNumberOf, _revenue: Self::Balance) {} } diff --git a/substrate/frame/broker/src/dispatchable_impls.rs b/substrate/frame/broker/src/dispatchable_impls.rs index 0b08a7b665b7..b04e15b169bc 100644 --- a/substrate/frame/broker/src/dispatchable_impls.rs +++ b/substrate/frame/broker/src/dispatchable_impls.rs @@ -37,6 +37,11 @@ impl Pallet { Ok(()) } + pub(crate) fn do_notify_core_count(core_count: CoreIndex) -> DispatchResult { + CoreCountInbox::::put(core_count); + Ok(()) + } + pub(crate) fn do_reserve(workload: Schedule) -> DispatchResult { let mut r = Reservations::::get(); let index = r.len() as u32; diff --git a/substrate/frame/broker/src/lib.rs b/substrate/frame/broker/src/lib.rs index ee3501d56071..38a504900546 100644 --- a/substrate/frame/broker/src/lib.rs +++ b/substrate/frame/broker/src/lib.rs @@ -159,6 +159,10 @@ pub mod pallet { pub type InstaPoolHistory = StorageMap<_, Blake2_128Concat, Timeslice, InstaPoolHistoryRecordOf>; + /// Received core count change from the relay chain. + #[pallet::storage] + pub type CoreCountInbox = StorageValue<_, CoreIndex, OptionQuery>; + #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { @@ -774,5 +778,13 @@ pub mod pallet { Self::do_request_core_count(core_count)?; Ok(()) } + + #[pallet::call_index(19)] + #[pallet::weight(T::WeightInfo::notify_core_count())] + pub fn notify_core_count(origin: OriginFor, core_count: CoreIndex) -> DispatchResult { + T::AdminOrigin::ensure_origin_or_root(origin)?; + Self::do_notify_core_count(core_count)?; + Ok(()) + } } } diff --git a/substrate/frame/broker/src/mock.rs b/substrate/frame/broker/src/mock.rs index 7444040ec9b7..19c72340353c 100644 --- a/substrate/frame/broker/src/mock.rs +++ b/substrate/frame/broker/src/mock.rs @@ -70,7 +70,6 @@ parameter_types! { pub static CoretimeWorkplan: BTreeMap<(u32, CoreIndex), Vec<(CoreAssignment, PartsOf57600)>> = Default::default(); pub static CoretimeUsage: BTreeMap> = Default::default(); pub static CoretimeInPool: CoreMaskBitCount = 0; - pub static NotifyCoreCount: Vec = Default::default(); pub static NotifyRevenueInfo: Vec<(u32, u64)> = Default::default(); } @@ -80,7 +79,7 @@ impl CoretimeInterface for TestCoretimeProvider { type Balance = u64; type RealyChainBlockNumberProvider = System; fn request_core_count(count: CoreIndex) { - NotifyCoreCount::mutate(|s| s.insert(0, count)); + CoreCountInbox::::put(count); } fn request_revenue_info_at(when: RCBlockNumberOf) { if when > RCBlockNumberProviderOf::::current_block_number() { @@ -126,17 +125,10 @@ impl CoretimeInterface for TestCoretimeProvider { ); CoretimeTrace::mutate(|v| v.push(item)); } - fn check_notify_core_count() -> Option { - NotifyCoreCount::mutate(|s| s.pop()) - } fn check_notify_revenue_info() -> Option<(RCBlockNumberOf, Self::Balance)> { NotifyRevenueInfo::mutate(|s| s.pop()).map(|v| (v.0 as _, v.1)) } #[cfg(feature = "runtime-benchmarks")] - fn ensure_notify_core_count(count: u16) { - NotifyCoreCount::mutate(|s| s.insert(0, count)); - } - #[cfg(feature = "runtime-benchmarks")] fn ensure_notify_revenue_info(when: RCBlockNumberOf, revenue: Self::Balance) { NotifyRevenueInfo::mutate(|s| s.push((when as u32, revenue))); } diff --git a/substrate/frame/broker/src/tick_impls.rs b/substrate/frame/broker/src/tick_impls.rs index 5f2be268b228..8b7860c8e3af 100644 --- a/substrate/frame/broker/src/tick_impls.rs +++ b/substrate/frame/broker/src/tick_impls.rs @@ -87,7 +87,7 @@ impl Pallet { } pub(crate) fn process_core_count(status: &mut StatusRecord) -> bool { - if let Some(core_count) = T::Coretime::check_notify_core_count() { + if let Some(core_count) = CoreCountInbox::::take() { status.core_count = core_count; Self::deposit_event(Event::::CoreCountChanged { core_count }); return true diff --git a/substrate/frame/broker/src/weights.rs b/substrate/frame/broker/src/weights.rs index b3a151c6062c..a8f50eeee6e6 100644 --- a/substrate/frame/broker/src/weights.rs +++ b/substrate/frame/broker/src/weights.rs @@ -74,6 +74,7 @@ pub trait WeightInfo { fn process_pool() -> Weight; fn process_core_schedule() -> Weight; fn request_revenue_info_at() -> Weight; + fn notify_core_count() -> Weight; fn do_tick_base() -> Weight; } @@ -447,6 +448,9 @@ impl WeightInfo for SubstrateWeight { // Minimum execution time: 147_000 picoseconds. Weight::from_parts(184_000, 0) } + fn notify_core_count() -> Weight { + T::DbWeight::get().reads_writes(1, 1) + } /// Storage: `Broker::Status` (r:1 w:1) /// Proof: `Broker::Status` (`max_values`: Some(1), `max_size`: Some(18), added: 513, mode: `MaxEncodedLen`) /// Storage: `Broker::Configuration` (r:1 w:0) @@ -835,6 +839,10 @@ impl WeightInfo for () { // Minimum execution time: 147_000 picoseconds. Weight::from_parts(184_000, 0) } + fn notify_core_count() -> Weight { + RocksDbWeight::get().reads(1) + .saturating_add(RocksDbWeight::get().writes(1)) + } /// Storage: `Broker::Status` (r:1 w:1) /// Proof: `Broker::Status` (`max_values`: Some(1), `max_size`: Some(18), added: 513, mode: `MaxEncodedLen`) /// Storage: `Broker::Configuration` (r:1 w:0) diff --git a/substrate/frame/nomination-pools/src/lib.rs b/substrate/frame/nomination-pools/src/lib.rs index 3a23b894ec80..c538f12e20bf 100644 --- a/substrate/frame/nomination-pools/src/lib.rs +++ b/substrate/frame/nomination-pools/src/lib.rs @@ -2955,9 +2955,12 @@ impl Pallet { }, (false, false) => { // Equivalent to (current_points / current_balance) * new_funds - balance(u256(current_points).saturating_mul(u256(new_funds))) - // We check for zero above - .div(current_balance) + balance( + u256(current_points) + .saturating_mul(u256(new_funds)) + // We check for zero above + .div(u256(current_balance)), + ) }, } } diff --git a/substrate/frame/nomination-pools/src/tests.rs b/substrate/frame/nomination-pools/src/tests.rs index 7fe1e704bb13..657b50e8594a 100644 --- a/substrate/frame/nomination-pools/src/tests.rs +++ b/substrate/frame/nomination-pools/src/tests.rs @@ -292,6 +292,28 @@ mod bonded_pool { assert_ok!(pool.ok_to_join()); }); } + + #[test] + fn points_and_balance_conversions_are_safe() { + ExtBuilder::default().build_and_execute(|| { + let bonded_pool = BondedPool:: { + id: 123123, + inner: BondedPoolInner { + commission: Commission::default(), + member_counter: 1, + points: u128::MAX, + roles: DEFAULT_ROLES, + state: PoolState::Open, + }, + }; + StakingMock::set_bonded_balance(bonded_pool.bonded_account(), u128::MAX); + + // Max out the points and balance of the pool and make sure the conversion works as + // expected and does not overflow. + assert_eq!(bonded_pool.balance_to_point(u128::MAX), u128::MAX); + assert_eq!(bonded_pool.points_to_balance(u128::MAX), u128::MAX); + }) + } } mod reward_pool { diff --git a/substrate/frame/sudo/src/lib.rs b/substrate/frame/sudo/src/lib.rs index d556c5eb6ae6..4f14c32ff76b 100644 --- a/substrate/frame/sudo/src/lib.rs +++ b/substrate/frame/sudo/src/lib.rs @@ -349,12 +349,16 @@ pub mod pallet { impl Pallet { /// Ensure that the caller is the sudo key. pub(crate) fn ensure_sudo(origin: OriginFor) -> DispatchResult { - let sender = ensure_signed(origin)?; - - if Self::key().map_or(false, |k| k == sender) { - Ok(()) + let sender = ensure_signed_or_root(origin)?; + + if let Some(sender) = sender { + if Self::key().map_or(false, |k| k == sender) { + Ok(()) + } else { + Err(Error::::RequireSudo.into()) + } } else { - Err(Error::::RequireSudo.into()) + Ok(()) } } } diff --git a/substrate/frame/sudo/src/tests.rs b/substrate/frame/sudo/src/tests.rs index 13dc069ddef1..73689415a737 100644 --- a/substrate/frame/sudo/src/tests.rs +++ b/substrate/frame/sudo/src/tests.rs @@ -169,6 +169,18 @@ fn remove_key_works() { }); } +#[test] +fn using_root_origin_works() { + new_test_ext(1).execute_with(|| { + assert_ok!(Sudo::remove_key(RuntimeOrigin::root())); + assert!(Sudo::key().is_none()); + System::assert_has_event(TestEvent::Sudo(Event::KeyRemoved {})); + + assert_ok!(Sudo::set_key(RuntimeOrigin::root(), 1)); + assert_eq!(Some(1), Sudo::key()); + }); +} + #[test] fn sudo_as_basics() { new_test_ext(1).execute_with(|| { diff --git a/substrate/frame/support/src/storage/unhashed.rs b/substrate/frame/support/src/storage/unhashed.rs index aae83034ab71..776c7d0f3c3a 100644 --- a/substrate/frame/support/src/storage/unhashed.rs +++ b/substrate/frame/support/src/storage/unhashed.rs @@ -27,8 +27,8 @@ pub fn get(key: &[u8]) -> Option { // TODO #3700: error should be handleable. log::error!( target: "runtime::storage", - "Corrupted state at `{:?}: {:?}`", - key, + "Corrupted state at `{}`: {:?}", + array_bytes::bytes2hex("0x", key), e, ); None