From 7c2eac2b9dd24c2a50eba53cdd6670bba2f51f8c Mon Sep 17 00:00:00 2001 From: R0GUE Date: Fri, 5 Apr 2024 17:58:06 +0200 Subject: [PATCH 01/76] feat: pallet assets pop api integration --- Cargo.lock | 312 +++++------ .../examples/trust_backed_assets/.gitignore | 9 + .../examples/trust_backed_assets/Cargo.toml | 25 + pop-api/examples/trust_backed_assets/lib.rs | 69 +++ pop-api/src/lib.rs | 6 +- pop-api/src/v0/assets/mod.rs | 1 + pop-api/src/v0/assets/trust_backed.rs | 519 ++++++++++++++++++ pop-api/src/v0/mod.rs | 3 + pop-api/src/v0/nfts.rs | 2 +- primitives/src/lib.rs | 6 +- primitives/src/storage_keys.rs | 7 + runtime/devnet/src/config/assets.rs | 16 +- runtime/devnet/src/extensions.rs | 139 ++++- runtime/devnet/src/lib.rs | 103 ++-- runtime/testnet/src/config/assets.rs | 16 +- runtime/testnet/src/extensions.rs | 150 ++++- runtime/testnet/src/lib.rs | 103 ++-- 17 files changed, 1221 insertions(+), 265 deletions(-) create mode 100755 pop-api/examples/trust_backed_assets/.gitignore create mode 100755 pop-api/examples/trust_backed_assets/Cargo.toml create mode 100755 pop-api/examples/trust_backed_assets/lib.rs create mode 100644 pop-api/src/v0/assets/mod.rs create mode 100644 pop-api/src/v0/assets/trust_backed.rs diff --git a/Cargo.lock b/Cargo.lock index 4dc89da6..e43d2b18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -214,7 +214,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -578,16 +578,16 @@ checksum = "f28243a43d821d11341ab73c80bed182dc015c514b951616cf79bd4af39af0c3" dependencies = [ "concurrent-queue", "event-listener 5.2.0", - "event-listener-strategy 0.5.0", + "event-listener-strategy 0.5.1", "futures-core", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] name = "async-executor" -version = "1.8.0" +version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17ae5ebefcc48e7452b4987947920dac9450be1110cadf34d1b8c116bdbaf97c" +checksum = "10b3e585719c2358d2660232671ca8ca4ddb4be4ce8a1842d6c2dc8685303316" dependencies = [ "async-lock 3.3.0", "async-task", @@ -676,7 +676,7 @@ checksum = "d034b430882f8381900d3fe6f0aaa3ad94f2cb4ac519b429692a1bc2dda4ae7b" dependencies = [ "event-listener 4.0.3", "event-listener-strategy 0.4.0", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] @@ -768,7 +768,7 @@ checksum = "a507401cad91ec6a857ed5513a2073c82a9b9048762b885bb98655b306964681" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -781,7 +781,7 @@ dependencies = [ "futures-sink", "futures-util", "memchr", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] @@ -899,7 +899,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -1526,9 +1526,9 @@ dependencies = [ [[package]] name = "chrono" -version = "0.4.35" +version = "0.4.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8eaf5903dcbc0a39312feb77df2ff4c76387d591b9fc7b04a238dcf8bb62639a" +checksum = "8a0d04d43504c61aa6c7531f1871dd0d418d91130162063b789da00fd7057a5e" dependencies = [ "android-tzdata", "iana-time-zone", @@ -1610,7 +1610,7 @@ dependencies = [ "anstream", "anstyle", "clap_lex", - "strsim 0.11.0", + "strsim 0.11.1", "terminal_size", ] @@ -1623,7 +1623,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -2337,7 +2337,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -2658,7 +2658,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -2698,7 +2698,7 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -2715,7 +2715,7 @@ checksum = "b404f596046b0bb2d903a9c786b875a126261b52b7c3a64bbb66382c41c771df" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -2763,7 +2763,7 @@ dependencies = [ "proc-macro2", "quote", "strsim 0.10.0", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -2785,7 +2785,7 @@ checksum = "a668eda54683121533a393014d8692171709ff57a7d61f187b6e782719f8933f" dependencies = [ "darling_core 0.20.8", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -2816,9 +2816,9 @@ dependencies = [ [[package]] name = "der" -version = "0.7.8" +version = "0.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fffa369a668c8af7dbf8b5e56c9f744fbd399949ed171606040001947de40b1c" +checksum = "f55bf8e7b65898637379c1b74eb1551107c8294ed26d855ceb9fd1a09cfc9bc0" dependencies = [ "const-oid", "zeroize", @@ -2869,6 +2869,17 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "derive-syn-parse" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d65d7ce8132b7c0e54497a4d9a55a1c2a0912a0d786cf894472ba818fba45762" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.58", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -2968,31 +2979,31 @@ checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] name = "docify" -version = "0.2.7" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cc4fd38aaa9fb98ac70794c82a00360d1e165a87fbf96a8a91f9dfc602aaee2" +checksum = "43a2f138ad521dc4a2ced1a4576148a6a610b4c5923933b062a263130a6802ce" dependencies = [ "docify_macros", ] [[package]] name = "docify_macros" -version = "0.2.7" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63fa215f3a0d40fb2a221b3aa90d8e1fbb8379785a990cb60d62ac71ebdc6460" +checksum = "1a081e51fb188742f5a7a1164ad752121abcb22874b21e2c3b0dd040c515fdad" dependencies = [ "common-path", - "derive-syn-parse", + "derive-syn-parse 0.2.0", "once_cell", "proc-macro2", "quote", "regex", - "syn 2.0.55", + "syn 2.0.58", "termcolor", "toml 0.8.12", "walkdir", @@ -3204,7 +3215,7 @@ checksum = "5c785274071b1b420972453b306eeca06acf4633829db4223b58a2a8c5953bc4" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -3215,7 +3226,7 @@ checksum = "6fd000fd6988e73bbe993ea3db9b1aa64906ab88766d654973924340c8cddb42" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -3331,7 +3342,7 @@ checksum = "d93877bcde0eb80ca09131a08d23f0a5c18a620b01db137dba666d18cd9b30c2" dependencies = [ "concurrent-queue", "parking", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] @@ -3342,7 +3353,7 @@ checksum = "67b215c49b2b248c855fb73579eb1f4f26c38ffdc12973e20e07b91d78d5646e" dependencies = [ "concurrent-queue", "parking", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] @@ -3353,7 +3364,7 @@ checksum = "2b5fb89194fa3cad959b833185b3063ba881dbfc7030680b314250779fb4cc91" dependencies = [ "concurrent-queue", "parking", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] @@ -3363,17 +3374,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "958e4d70b6d5e81971bebec42271ec641e7ff4e170a6fa605f2b8a8b65cb97d3" dependencies = [ "event-listener 4.0.3", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] name = "event-listener-strategy" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "feedafcaa9b749175d5ac357452a9d41ea2911da598fde46ce1fe02c37751291" +checksum = "332f51cb23d20b0de8458b86580878211da09bcd4503cb579c225b3d124cabb3" dependencies = [ "event-listener 5.2.0", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] @@ -3408,7 +3419,7 @@ dependencies = [ "prettier-please", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -3673,7 +3684,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -3761,9 +3772,9 @@ dependencies = [ [[package]] name = "frame-support" -version = "29.0.0" +version = "29.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3b24824d29c43d0af94be3356bbf30338ededed649f6841d315a9ae067ce872" +checksum = "b8e52c84b611d2049d9253f83a62ab0f093e4be5c42a7ef42ea5bb16d6611e32" dependencies = [ "aquamarine", "array-bytes 6.2.2", @@ -3809,7 +3820,7 @@ checksum = "3bf1d648c4007d421b9677b3c893256913498fff159dc2d85022cdd9cc432f3c" dependencies = [ "Inflector", "cfg-expr", - "derive-syn-parse", + "derive-syn-parse 0.1.5", "expander 2.1.0", "frame-support-procedural-tools", "itertools 0.10.5", @@ -3818,7 +3829,7 @@ dependencies = [ "proc-macro2", "quote", "sp-crypto-hashing", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -3831,7 +3842,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -3842,7 +3853,7 @@ checksum = "68672b9ec6fe72d259d3879dc212c5e42e977588cdac830c76f54d9f492aeb58" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -4010,7 +4021,7 @@ dependencies = [ "futures-io", "memchr", "parking", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "waker-fn", ] @@ -4024,7 +4035,7 @@ dependencies = [ "futures-core", "futures-io", "parking", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] @@ -4035,7 +4046,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -4080,7 +4091,7 @@ dependencies = [ "futures-sink", "futures-task", "memchr", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "pin-utils", "slab", ] @@ -4404,7 +4415,7 @@ checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" dependencies = [ "bytes", "http", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] @@ -4447,8 +4458,8 @@ dependencies = [ "httparse", "httpdate", "itoa", - "pin-project-lite 0.2.13", - "socket2 0.4.10", + "pin-project-lite 0.2.14", + "socket2 0.5.6", "tokio", "tower-service", "tracing", @@ -4806,9 +4817,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.10" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "jobserver" @@ -5125,9 +5136,9 @@ dependencies = [ [[package]] name = "landlock" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1530c5b973eeed4ac216af7e24baf5737645a6272e361f1fb95710678b67d9cc" +checksum = "9baa9eeb6e315942429397e617a190f4fdc696ef1ee0342939d641029cbb4ea7" dependencies = [ "enumflags2", "libc", @@ -5563,13 +5574,12 @@ dependencies = [ [[package]] name = "libredox" -version = "0.0.1" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ "bitflags 2.5.0", "libc", - "redox_syscall 0.4.1", ] [[package]] @@ -5805,7 +5815,7 @@ dependencies = [ "macro_magic_core", "macro_magic_macros", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -5815,11 +5825,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "468155613a44cfd825f1fb0ffa532b018253920d404e6fca1e8d43155198a46d" dependencies = [ "const-random", - "derive-syn-parse", + "derive-syn-parse 0.1.5", "macro_magic_core_macros", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -5830,7 +5840,7 @@ checksum = "9ea73aa640dc01d62a590d48c0c3521ed739d53b27f919b25c3551e233481654" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -5841,7 +5851,7 @@ checksum = "ef9d79ae96aaba821963320eb2b6e34d17df1e5a83d8a1985c29cc5be59577b3" dependencies = [ "macro_magic_core", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -5892,9 +5902,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.7.1" +version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" [[package]] name = "memfd" @@ -6229,9 +6239,9 @@ dependencies = [ [[package]] name = "nalgebra" -version = "0.32.4" +version = "0.32.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4541eb06dce09c0241ebbaab7102f0a01a0c8994afed2e5d0d66775016e25ac2" +checksum = "3ea4908d4f23254adda3daa60ffef0f1ac7b8c3e9a864cf3cc154b251908a2ef" dependencies = [ "approx", "matrixmultiply", @@ -6324,9 +6334,9 @@ dependencies = [ [[package]] name = "netlink-sys" -version = "0.8.5" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6471bf08e7ac0135876a9581bf3217ef0333c191c128d34878079f42ee150411" +checksum = "416060d346fbaf1f23f9512963e3e878f1a78e707cb699ba9215761754244307" dependencies = [ "bytes", "futures", @@ -6763,9 +6773,9 @@ dependencies = [ [[package]] name = "pallet-balances" -version = "29.0.0" +version = "29.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "942007f4f7aace74b77009db1675e7ca98683a42dde5e2d85bba2a9f404d2e5a" +checksum = "e27946a57494d7c6231ae8909275bbd3cb5460ee3d27b7a5774a8b8e64d3ab92" dependencies = [ "docify", "frame-benchmarking", @@ -7045,7 +7055,7 @@ checksum = "3163c6bc21b55a0ccb74c546ba784d9c9e69beb9240c059d28a3052f4cbce509" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -7682,9 +7692,9 @@ dependencies = [ [[package]] name = "pallet-staking" -version = "29.0.0" +version = "29.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a94127295cf027a26e933ea6788b0824e9fedd90740013673e2d36b5d707efe" +checksum = "668b7d28c499f0d9f295fad26cf6c342472e21842e3b13bcaaac8536358b2d6c" dependencies = [ "frame-benchmarking", "frame-election-provider-support", @@ -7713,7 +7723,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -8274,9 +8284,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pest" -version = "2.7.8" +version = "2.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56f8023d0fb78c8e03784ea1c7f3fa36e68a723138990b8d5a47d916b651e7a8" +checksum = "311fb059dee1a7b802f036316d790138c613a4e8b180c822e3925a662e9f0c95" dependencies = [ "memchr", "thiserror", @@ -8285,9 +8295,9 @@ dependencies = [ [[package]] name = "pest_derive" -version = "2.7.8" +version = "2.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0d24f72393fd16ab6ac5738bc33cdb6a9aa73f8b902e8fe29cf4e67d7dd1026" +checksum = "f73541b156d32197eecda1a4014d7f868fd2bcb3c550d5386087cfba442bf69c" dependencies = [ "pest", "pest_generator", @@ -8295,22 +8305,22 @@ dependencies = [ [[package]] name = "pest_generator" -version = "2.7.8" +version = "2.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdc17e2a6c7d0a492f0158d7a4bd66cc17280308bbaff78d5bef566dca35ab80" +checksum = "c35eeed0a3fab112f75165fdc026b3913f4183133f19b49be773ac9ea966e8bd" dependencies = [ "pest", "pest_meta", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] name = "pest_meta" -version = "2.7.8" +version = "2.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "934cd7631c050f4674352a6e835d5f6711ffbfb9345c2fc0107155ac495ae293" +checksum = "2adbf29bb9776f28caece835398781ab24435585fe0d4dc1374a61db5accedca" dependencies = [ "once_cell", "pest", @@ -8344,7 +8354,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -8355,9 +8365,9 @@ checksum = "257b64915a082f7811703966789728173279bdebb956b143dbcd23f6f970a777" [[package]] name = "pin-project-lite" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" [[package]] name = "pin-utils" @@ -8394,9 +8404,9 @@ checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" [[package]] name = "platforms" -version = "3.3.0" +version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "626dec3cac7cc0e1577a2ec3fc496277ec2baa084bebad95bb6fdbfae235f84c" +checksum = "db23d408679286588f4d4644f965003d056e3dd5abcaaa938116871d7ce2fee7" [[package]] name = "polkadot-approval-distribution" @@ -9524,7 +9534,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6380dbe1fb03ecc74ad55d841cfc75480222d153ba69ddcb00977866cbdabdb8" dependencies = [ "polkavm-derive-impl", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -9536,7 +9546,7 @@ dependencies = [ "polkavm-common", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -9551,7 +9561,7 @@ dependencies = [ "concurrent-queue", "libc", "log", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "windows-sys 0.48.0", ] @@ -9564,7 +9574,7 @@ dependencies = [ "cfg-if", "concurrent-queue", "hermit-abi", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "rustix 0.38.32", "tracing", "windows-sys 0.52.0", @@ -9710,19 +9720,16 @@ dependencies = [ "pallet-collator-selection", "pallet-contracts", "pallet-message-queue", - "pallet-multisig", "pallet-nft-fractionalization", "pallet-nfts", "pallet-nfts-runtime-api", "pallet-preimage", - "pallet-proxy", "pallet-scheduler", "pallet-session", "pallet-sudo", "pallet-timestamp", "pallet-transaction-payment", "pallet-transaction-payment-rpc-runtime-api", - "pallet-utility", "pallet-xcm", "parachains-common", "parity-scale-codec", @@ -9783,19 +9790,16 @@ dependencies = [ "pallet-collator-selection", "pallet-contracts", "pallet-message-queue", - "pallet-multisig", "pallet-nft-fractionalization", "pallet-nfts", "pallet-nfts-runtime-api", "pallet-preimage", - "pallet-proxy", "pallet-scheduler", "pallet-session", "pallet-sudo", "pallet-timestamp", "pallet-transaction-payment", "pallet-transaction-payment-rpc-runtime-api", - "pallet-utility", "pallet-xcm", "parachains-common", "parity-scale-codec", @@ -9880,7 +9884,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22020dfcf177fcc7bf5deaf7440af371400c67c0de14c399938d8ed4fb4645d3" dependencies = [ "proc-macro2", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -9900,7 +9904,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d3928fb5db768cb86f891ff014f0144589297e3c6a1aba6ed7cecfdace270c7" dependencies = [ "proc-macro2", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -9994,7 +9998,7 @@ checksum = "834da187cfe638ae8abb0203f0b33e5ccdb02a28e7199f2f47b3e2754f50edca" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -10040,7 +10044,7 @@ checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -10108,7 +10112,7 @@ dependencies = [ "itertools 0.11.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -10327,9 +10331,9 @@ dependencies = [ [[package]] name = "redox_users" -version = "0.4.4" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4" +checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" dependencies = [ "getrandom 0.2.12", "libredox", @@ -10365,7 +10369,7 @@ checksum = "5fddb4f8d99b0a2ebafc65a87a69a7b9875e4b1ae1f00db265d300ef7f28bccc" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -10389,7 +10393,7 @@ dependencies = [ "aho-corasick", "memchr", "regex-automata 0.4.6", - "regex-syntax 0.8.2", + "regex-syntax 0.8.3", ] [[package]] @@ -10409,7 +10413,7 @@ checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" dependencies = [ "aho-corasick", "memchr", - "regex-syntax 0.8.2", + "regex-syntax 0.8.3", ] [[package]] @@ -10420,9 +10424,9 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] name = "regex-syntax" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" [[package]] name = "resolv-conf" @@ -11025,7 +11029,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -12039,7 +12043,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -12205,7 +12209,7 @@ dependencies = [ "proc-macro2", "quote", "scale-info", - "syn 2.0.55", + "syn 2.0.58", "thiserror", ] @@ -12358,9 +12362,9 @@ dependencies = [ [[package]] name = "security-framework" -version = "2.9.2" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de" +checksum = "770452e37cad93e0a50d5abc3990d2bc351c36d0328f86cefec2f2fb206eaef6" dependencies = [ "bitflags 1.3.2", "core-foundation", @@ -12371,9 +12375,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.9.1" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e932934257d3b408ed8f30db49d85ea163bfe74961f017f405b025af298f0c7a" +checksum = "41f3cc463c0ef97e11c3461a9d3787412d30e8e7eb907c79180c4a57bf7c04ef" dependencies = [ "core-foundation-sys", "libc", @@ -12438,14 +12442,14 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] name = "serde_json" -version = "1.0.114" +version = "1.0.115" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" +checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd" dependencies = [ "itoa", "ryu", @@ -13057,7 +13061,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -13323,7 +13327,7 @@ checksum = "b85d0f1f1e44bd8617eb2a48203ee854981229e3e79e6f468c7175d5fd37489b" dependencies = [ "quote", "sp-crypto-hashing", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -13344,7 +13348,7 @@ checksum = "48d09fa0a5f7299fb81ee25ae3853d26200f7a348148aed6de76be905c007dbe" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -13593,7 +13597,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -13800,7 +13804,7 @@ dependencies = [ "parity-scale-codec", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -14044,9 +14048,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "strsim" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "strum" @@ -14086,7 +14090,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -14262,7 +14266,7 @@ dependencies = [ "scale-info", "scale-typegen", "subxt-metadata", - "syn 2.0.55", + "syn 2.0.58", "thiserror", "tokio", ] @@ -14296,7 +14300,7 @@ dependencies = [ "quote", "scale-typegen", "subxt-codegen", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -14347,9 +14351,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.55" +version = "2.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "002a1b3dbf967edfafc32655d0f377ab0bb7b994aa1d32c8cc7e9b8bf3ebb8f0" +checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" dependencies = [ "proc-macro2", "quote", @@ -14479,7 +14483,7 @@ checksum = "e4c60d69f36615a077cc7663b9cb8e42275722d23e58a7fa3d2c7f2915d09d04" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -14490,7 +14494,7 @@ checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -14619,7 +14623,7 @@ dependencies = [ "mio", "num_cpus", "parking_lot 0.12.1", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "signal-hook-registry", "socket2 0.5.6", "tokio-macros", @@ -14634,7 +14638,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -14676,7 +14680,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "267ac89e0bec6e691e5813911606935d77c476ff49024f98abcea3e7b15e37af" dependencies = [ "futures-core", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "tokio", "tokio-util", ] @@ -14691,7 +14695,7 @@ dependencies = [ "futures-core", "futures-io", "futures-sink", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "tokio", "tracing", ] @@ -14781,7 +14785,7 @@ dependencies = [ "futures-core", "futures-util", "pin-project", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "tower-layer", "tower-service", "tracing", @@ -14800,7 +14804,7 @@ dependencies = [ "http", "http-body", "http-range-header", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "tower-layer", "tower-service", ] @@ -14824,7 +14828,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ "log", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "tracing-attributes", "tracing-core", ] @@ -14837,7 +14841,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -14882,7 +14886,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -15308,7 +15312,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", "wasm-bindgen-shared", ] @@ -15342,7 +15346,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -15364,9 +15368,9 @@ dependencies = [ [[package]] name = "wasm-opt" -version = "0.116.0" +version = "0.116.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc942673e7684671f0c5708fc18993569d184265fd5223bb51fc8e5b9b6cfd52" +checksum = "2fd87a4c135535ffed86123b6fb0f0a5a0bc89e50416c942c5f0662c645f679c" dependencies = [ "anyhow", "libc", @@ -16222,7 +16226,7 @@ dependencies = [ "Inflector", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -16271,7 +16275,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -16291,7 +16295,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.58", ] [[package]] @@ -16334,9 +16338,9 @@ dependencies = [ [[package]] name = "zstd-sys" -version = "2.0.9+zstd.1.5.5" +version = "2.0.10+zstd.1.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e16efa8a874a0481a574084d34cc26fdb3b99627480f785888deb6386506656" +checksum = "c253a4914af5bafc8fa8c86ee400827e83cf6ec01195ec1f1ed8441bf00d65aa" dependencies = [ "cc", "pkg-config", diff --git a/pop-api/examples/trust_backed_assets/.gitignore b/pop-api/examples/trust_backed_assets/.gitignore new file mode 100755 index 00000000..8de8f877 --- /dev/null +++ b/pop-api/examples/trust_backed_assets/.gitignore @@ -0,0 +1,9 @@ +# Ignore build artifacts from the local tests sub-crate. +/target/ + +# Ignore backup files creates by cargo fmt. +**/*.rs.bk + +# Remove Cargo.lock when creating an executable, leave it for libraries +# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock +Cargo.lock diff --git a/pop-api/examples/trust_backed_assets/Cargo.toml b/pop-api/examples/trust_backed_assets/Cargo.toml new file mode 100755 index 00000000..3c3716fc --- /dev/null +++ b/pop-api/examples/trust_backed_assets/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "pop_api_trust_backed_assets_example" +version = "0.1.0" +authors = ["[your_name] <[your_email]>"] +edition = "2021" + +[dependencies] +ink = { version = "5.0.0", default-features = false } +pop-api = { path = "../../../pop-api", default-features = false } +scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } +scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true } + +[lib] +path = "lib.rs" + +[features] +default = ["std"] +std = [ + "ink/std", + "pop-api/std", + "scale/std", + "scale-info/std", +] +ink-as-dependency = [] +e2e-tests = [] diff --git a/pop-api/examples/trust_backed_assets/lib.rs b/pop-api/examples/trust_backed_assets/lib.rs new file mode 100755 index 00000000..8b2fbda6 --- /dev/null +++ b/pop-api/examples/trust_backed_assets/lib.rs @@ -0,0 +1,69 @@ +#![cfg_attr(not(feature = "std"), no_std, no_main)] + +use pop_api::assets::trust_backed as trust_backed_assets; + +#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum ContractError { + TrustBackedAssetsError(trust_backed_assets::Error), + UnknownAsset, +} + +impl From for ContractError { + fn from(value: trust_backed_assets::Error) -> Self { + ContractError::TrustBackedAssetsError(value) + } +} + +#[ink::contract(env = pop_api::Environment)] +mod pop_api_extension_demo { + use super::*; + + #[ink(storage)] + #[derive(Default)] + pub struct PopApiExtensionDemo; + + impl PopApiExtensionDemo { + #[ink(constructor, payable)] + pub fn new() -> Self { + ink::env::debug_println!("Contract::new"); + Default::default() + } + + #[ink(message)] + pub fn mint_asset_through_runtime( + &mut self, + id: u32, + beneficiary: AccountId, + amount: Balance, + ) -> Result<(), ContractError> { + ink::env::debug_println!( + "Contract::mint_asset_through_runtime: id: {:?} beneficiary: {:?} amount: {:?}", + id, + beneficiary, + amount + ); + + // Check if asset doesn't exist. + if !trust_backed_assets::asset_exists(id)? { + return Err(ContractError::UnknownAsset); + } + + // Mint asset via pop api. + trust_backed_assets::mint(id, beneficiary, amount)?; + ink::env::debug_println!("Contract::mint_asset_through_runtime: asset(s) minted successfully"); + Ok(()) + } + } + + #[cfg(test)] + mod tests { + use super::*; + + #[ink::test] + fn default_works() { + PopApiExtensionDemo::new(); + } + } +} + diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index e4590c89..68ce6906 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -3,12 +3,12 @@ pub mod primitives; pub mod v0; -use crate::PopApiError::{Balances, Nfts, UnknownStatusCode}; +use crate::PopApiError::{Balances, Nfts, TrustBackedAssets, UnknownStatusCode}; use ink::{prelude::vec::Vec, ChainExtensionInstance}; use primitives::{cross_chain::*, storage_keys::*}; pub use sp_runtime::{BoundedVec, MultiAddress, MultiSignature}; use v0::RuntimeCall; -pub use v0::{balances, cross_chain, nfts, relay_chain_block_number, state}; +pub use v0::{balances, cross_chain, nfts, relay_chain_block_number, state, assets}; type AccountId = ::AccountId; type Balance = ::Balance; @@ -26,6 +26,7 @@ pub enum PopApiError { SystemCallFiltered, Balances(balances::Error), Nfts(nfts::Error), + TrustBackedAssets(assets::trust_backed::Error), Xcm(cross_chain::Error), } @@ -37,6 +38,7 @@ impl ink::env::chain_extension::FromStatusCode for PopApiError { 5 => Err(PopApiError::SystemCallFiltered), 10_000..=10_999 => Err(Balances((status_code - 10_000).try_into()?)), 50_000..=50_999 => Err(Nfts((status_code - 50_000).try_into()?)), + 52_000..=52_999 => Err(TrustBackedAssets((status_code - 52_000).try_into()?)), _ => Err(UnknownStatusCode(status_code)), } } diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs new file mode 100644 index 00000000..7ad40f15 --- /dev/null +++ b/pop-api/src/v0/assets/mod.rs @@ -0,0 +1 @@ +pub mod trust_backed; \ No newline at end of file diff --git a/pop-api/src/v0/assets/trust_backed.rs b/pop-api/src/v0/assets/trust_backed.rs new file mode 100644 index 00000000..fb413413 --- /dev/null +++ b/pop-api/src/v0/assets/trust_backed.rs @@ -0,0 +1,519 @@ +use crate::{Balance, PopApiError::UnknownStatusCode, RuntimeCall, *}; +use ink::prelude::vec::Vec; +use primitives::{AssetId, MultiAddress}; +use scale::{Compact, Encode}; + +type Result = core::result::Result; + +/// https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/assets/src/lib.rs +/// +/// Extrinsics within pallet assets (TrustBackedAssets Instance) that can be used via the pop api on Pop Network: +/// 1. create +/// 2. start_destroy +/// 3. destroy_accounts +/// 4. destroy_approvals +/// 5. finish_destroy +/// 6. mint +/// 7. burn +/// 8. transfer +/// 9. transfer_keep_alive +/// 10. force_transfer +/// 11. freeze +/// 12. thaw +/// 13. freeze_asset +/// 14. thaw_asset +/// 15. transfer_ownership +/// 16. set_team +/// 17. set_metadata +/// 18. clear_metadata +/// 19. approve_transfer +/// 20. cancel_approval +/// 21. force_cancel_approval +/// 22. transfer_approved +/// 23. touch +/// 24. refund +/// 25. set_min_balance +/// 26. touch_other +/// 27. refund_other +/// 28. block + + +/// Issue a new class of fungible assets from a public origin. +pub fn create( + id: AssetId, + admin: impl Into>, + min_balance: Balance, +) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Create { + id: id.into(), + admin: admin.into(), + min_balance: Compact(min_balance), + }))?) +} + +/// Start the process of destroying a fungible asset class. +pub fn start_destroy(id: AssetId) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::StartDestroy { + id: id.into(), + }))?) +} + +/// Destroy all accounts associated with a given asset. +pub fn destroy_accounts(id: AssetId) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::DestroyAccounts { + id: id.into(), + }))?) +} + +/// Destroy all approvals associated with a given asset up to the max (see runtime configuration TrustBackedAssets `RemoveItemsLimit`). +pub fn destroy_approvals(id: AssetId) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::DestroyApprovals { + id: id.into(), + }))?) +} + +/// Complete destroying asset and unreserve currency. +pub fn finish_destroy(id: AssetId) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::FinishDestroy { + id: id.into(), + }))?) +} + +/// Mint assets of a particular class. +pub fn mint( + id: AssetId, + beneficiary: impl Into>, + amount: Balance, +) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Mint { + id: id.into(), + beneficiary: beneficiary.into(), + amount: Compact(amount), + }))?) +} + +/// Reduce the balance of `who` by as much as possible up to `amount` assets of `id`. +pub fn burn( + id: AssetId, + who: impl Into>, + amount: Balance, +) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Burn { + id: id.into(), + who: who.into(), + amount: Compact(amount), + }))?) +} + +/// Move some assets from the sender account to another. +pub fn transfer( + id: AssetId, + target: impl Into>, + amount: Balance, +) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Transfer { + id: id.into(), + target: target.into(), + amount: Compact(amount), + }))?) +} + +/// Move some assets from the sender account to another, keeping the sender account alive. +pub fn transfer_keep_alive( + id: AssetId, + target: impl Into>, + amount: Balance, +) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::TransferKeepAlive { + id: id.into(), + target: target.into(), + amount: Compact(amount), + }))?) +} + +/// Move some assets from one account to another. Sender should be the Admin of the asset `id`. +pub fn force_transfer( + id: AssetId, + source: impl Into>, + dest: impl Into>, + amount: Balance, +) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::ForceTransfer { + id: id.into(), + source: source.into(), + dest: dest.into(), + amount: Compact(amount), + }))?) +} + +/// Disallow further unprivileged transfers of an asset `id` from an account `who`. `who` +/// must already exist as an entry in `Account`s of the asset. If you want to freeze an +/// account that does not have an entry, use `touch_other` first. +pub fn freeze(id: AssetId, who: impl Into>) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Freeze { + id: id.into(), + who: who.into(), + }))?) +} + +/// Allow unprivileged transfers to and from an account again. +pub fn thaw(id: AssetId, who: impl Into>) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Thaw { + id: id.into(), + who: who.into(), + }))?) +} + +/// Disallow further unprivileged transfers for the asset class. +pub fn freeze_asset(id: AssetId) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::FreezeAsset { + id: id.into(), + }))?) +} + +/// Allow unprivileged transfers for the asset again. +pub fn thaw_asset(id: AssetId) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::ThawAsset { + id: id.into(), + }))?) +} + +/// Change the Owner of an asset. +pub fn transfer_ownership( + id: AssetId, + owner: impl Into>, +) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::TransferOwnership { + id: id.into(), + owner: owner.into(), + }))?) +} + +/// Change the Issuer, Admin and Freezer of an asset. +pub fn set_team( + id: AssetId, + issuer: impl Into>, + admin: impl Into>, + freezer: impl Into>, +) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::SetTeam { + id: id.into(), + issuer: issuer.into(), + admin: admin.into(), + freezer: freezer.into(), + }))?) +} + +/// Set the metadata for an asset. +pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::SetMetadata { + id: id.into(), + name, + symbol, + decimals, + }))?) +} + +/// Clear the metadata for an asset. +pub fn clear_metadata(id: AssetId) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::ClearMetadata { + id: id.into(), + }))?) +} + +/// Approve an amount of asset for transfer by a delegated third-party account. +pub fn approve_transfer( + id: AssetId, + delegate: impl Into>, + amount: Balance, +) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::ApproveTransfer { + id: id.into(), + delegate: delegate.into(), + amount: Compact(amount), + }))?) +} + +/// Cancel all of some asset approved for delegated transfer by a third-party account. +pub fn cancel_approval( + id: AssetId, + delegate: impl Into>, +) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::CancelApproval { + id: id.into(), + delegate: delegate.into(), + }))?) +} + +/// Cancel all of some asset approved for delegated transfer by a third-party account. +pub fn force_cancel_approval( + id: AssetId, + owner: impl Into>, + delegate: impl Into>, +) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::ForceCancelApproval { + id: id.into(), + owner: owner.into(), + delegate: delegate.into(), + }))?) +} + +/// Transfer some asset balance from a previously delegated account to some third-party +/// account. +pub fn transfer_approved( + id: AssetId, + owner: impl Into>, + destination: impl Into>, + amount: Balance, +) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::TransferApproved { + id: id.into(), + owner: owner.into(), + destination: destination.into(), + amount: Compact(amount), + }))?) +} + +/// Create an asset account for non-provider assets. +pub fn touch(id: AssetId) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Touch { + id: id.into(), + }))?) +} + +/// Return the deposit (if any) of an asset account or a consumer reference (if any) of an +/// account. +pub fn refund(id: AssetId, allow_burn: bool) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Refund { + id: id.into(), + allow_burn, + }))?) +} + +/// Sets the minimum balance of an asset. +pub fn set_min_balance(id: AssetId, min_balance: Balance) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::SetMinBalance { + id: id.into(), + min_balance: Compact(min_balance), + }))?) +} + +/// Create an asset account for `who`. +pub fn touch_other(id: AssetId, who: impl Into>) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::TouchOther { + id: id.into(), + who: who.into(), + }))?) +} + +/// Return the deposit (if any) of a target asset account. Useful if you are the depositor. +pub fn refund_other(id: AssetId, who: impl Into>) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::RefundOther { + id: id.into(), + who: who.into(), + }))?) +} + +/// Disallow further unprivileged transfers of an asset `id` to and from an account `who`. +pub fn block(id: AssetId, who: impl Into>) -> Result<()> { + Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Block { + id: id.into(), + who: who.into(), + }))?) +} + +pub fn asset_exists(id: AssetId) -> Result { + Ok(state::read(RuntimeStateKeys::TrustBackedAssets(TrustBackedAssetsKeys::AssetExists(id)))?) +} + +// Parameters to extrinsics representing an asset id (`AssetIdParameter`) and a balance amount (`Balance`) are expected +// to be compact encoded. The pop api handles that for the developer. +// +// reference: https://substrate.stackexchange.com/questions/1873/what-is-the-meaning-of-palletcompact-in-pallet-development +// +// Asset id that is compact encoded. +type AssetIdParameter = Compact; +// Balance amount that is compact encoded. +type BalanceParameter = Compact; + +#[derive(Encode)] +pub(crate) enum TrustBackedAssetsCalls { + #[codec(index = 0)] + Create { + id: AssetIdParameter, + admin: MultiAddress, + min_balance: BalanceParameter, + }, + #[codec(index = 2)] + StartDestroy { id: AssetIdParameter }, + #[codec(index = 3)] + DestroyAccounts { id: AssetIdParameter }, + #[codec(index = 4)] + DestroyApprovals { id: AssetIdParameter }, + #[codec(index = 5)] + FinishDestroy { id: AssetIdParameter }, + #[codec(index = 6)] + Mint { + id: AssetIdParameter, + beneficiary: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 7)] + Burn { id: AssetIdParameter, who: MultiAddress, amount: BalanceParameter }, + #[codec(index = 8)] + Transfer { id: AssetIdParameter, target: MultiAddress, amount: BalanceParameter }, + #[codec(index = 9)] + TransferKeepAlive { + id: AssetIdParameter, + target: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 10)] + ForceTransfer { + id: AssetIdParameter, + source: MultiAddress, + dest: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 11)] + Freeze { id: AssetIdParameter, who: MultiAddress }, + #[codec(index = 12)] + Thaw { id: AssetIdParameter, who: MultiAddress }, + #[codec(index = 13)] + FreezeAsset { id: AssetIdParameter }, + #[codec(index = 14)] + ThawAsset { id: AssetIdParameter }, + #[codec(index = 15)] + TransferOwnership { id: AssetIdParameter, owner: MultiAddress }, + #[codec(index = 16)] + SetTeam { + id: AssetIdParameter, + issuer: MultiAddress, + admin: MultiAddress, + freezer: MultiAddress, + }, + #[codec(index = 17)] + SetMetadata { id: AssetIdParameter, name: Vec, symbol: Vec, decimals: u8 }, + #[codec(index = 18)] + ClearMetadata { id: AssetIdParameter }, + #[codec(index = 22)] + ApproveTransfer { + id: AssetIdParameter, + delegate: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 23)] + CancelApproval { id: AssetIdParameter, delegate: MultiAddress }, + #[codec(index = 24)] + ForceCancelApproval { + id: AssetIdParameter, + owner: MultiAddress, + delegate: MultiAddress, + }, + #[codec(index = 25)] + TransferApproved { + id: AssetIdParameter, + owner: MultiAddress, + destination: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 26)] + Touch { id: AssetIdParameter }, + #[codec(index = 27)] + Refund { id: AssetIdParameter, allow_burn: bool }, + #[codec(index = 28)] + SetMinBalance { id: AssetIdParameter, min_balance: BalanceParameter }, + #[codec(index = 29)] + TouchOther { id: AssetIdParameter, who: MultiAddress }, + #[codec(index = 30)] + RefundOther { id: AssetIdParameter, who: MultiAddress }, + #[codec(index = 31)] + Block { id: AssetIdParameter, who: MultiAddress }, +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum Error { + /// Account balance must be greater than or equal to the transfer amount. + BalanceLow, + /// The account to alter does not exist. + NoAccount, + /// The signing account has no permission to do the operation. + NoPermission, + /// The given asset ID is unknown. + Unknown, + /// The origin account is frozen. + Frozen, + /// The asset ID is already taken. + InUse, + /// Invalid witness data given. + BadWitness, + /// Minimum balance should be non-zero. + MinBalanceZero, + /// Unable to increment the consumer reference counters on the account. Either no provider + /// reference exists to allow a non-zero balance of a non-self-sufficient asset, or one + /// fewer then the maximum number of consumers has been reached. + UnavailableConsumer, + /// Invalid metadata given. + BadMetadata, + /// No approval exists that would allow the transfer. + Unapproved, + /// The source account would not survive the transfer and it needs to stay alive. + WouldDie, + /// The asset-account already exists. + AlreadyExists, + /// The asset-account doesn't have an associated deposit. + NoDeposit, + /// The operation would result in funds being burned. + WouldBurn, + /// The asset is a live asset and is actively being used. Usually emit for operations such + /// as `start_destroy` which require the asset to be in a destroying state. + LiveAsset, + /// The asset is not live, and likely being destroyed. + AssetNotLive, + /// The asset status is not the expected status. + IncorrectStatus, + /// The asset should be frozen before the given operation. + NotFrozen, + /// Callback action resulted in error + CallbackFailed, +} + +impl TryFrom for Error { + type Error = PopApiError; + + fn try_from(status_code: u32) -> core::result::Result { + use Error::*; + match status_code { + 0 => Ok(BalanceLow), + 1 => Ok(NoAccount), + 2 => Ok(NoPermission), + 3 => Ok(Unknown), + 4 => Ok(Frozen), + 5 => Ok(InUse), + 6 => Ok(BadWitness), + 7 => Ok(MinBalanceZero), + 8 => Ok(UnavailableConsumer), + 9 => Ok(BadMetadata), + 10 => Ok(Unapproved), + 11 => Ok(WouldDie), + 12 => Ok(AlreadyExists), + 13 => Ok(NoDeposit), + 14 => Ok(WouldBurn), + 15 => Ok(LiveAsset), + 16 => Ok(AssetNotLive), + 17 => Ok(IncorrectStatus), + 18 => Ok(NotFrozen), + _ => Err(UnknownStatusCode(status_code)), + } + } +} + +impl From for Error { + fn from(error: PopApiError) -> Self { + match error { + PopApiError::TrustBackedAssets(e) => e, + _ => panic!("Unexpected pallet assets error. This error is unknown to pallet assets"), + } + } +} diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index 2ae0b821..d914db24 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -7,6 +7,7 @@ pub mod balances; pub mod cross_chain; pub mod nfts; pub mod state; +pub mod assets; pub fn relay_chain_block_number() -> Result { state::read(RuntimeStateKeys::ParachainSystem(ParachainSystemKeys::LastRelayChainBlockNumber)) @@ -18,4 +19,6 @@ pub(crate) enum RuntimeCall { Balances(balances::BalancesCall), #[codec(index = 50)] Nfts(nfts::NftCalls), + #[codec(index = 52)] + TrustBackedAssets(assets::trust_backed::TrustBackedAssetsCalls), } diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index 3db08cd1..5a55154f 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -669,7 +669,7 @@ impl From for Error { fn from(error: PopApiError) -> Self { match error { PopApiError::Nfts(e) => e, - _ => panic!("expected nfts error"), + _ => panic!("unexpected pallet nfts error. This error is unknown to pallet nfts"), } } } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 8ad74ade..ebad36d3 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -1,14 +1,12 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] pub use bounded_collections::{BoundedBTreeMap, BoundedBTreeSet, BoundedVec, ConstU32}; -//use scale::{Decode, Encode, MaxEncodedLen}; pub mod cross_chain; pub mod storage_keys; -// /// Some way of identifying an account on the chain. -// #[derive(Encode, Decode, Debug, MaxEncodedLen)] -// pub struct AccountId([u8; 32]); +// Identifier for the class of asset. +pub type AssetId = u32; // Id used for identifying non-fungible collections. pub type CollectionId = u32; // Id used for identifying non-fungible items. diff --git a/primitives/src/storage_keys.rs b/primitives/src/storage_keys.rs index 2bcb41ec..a03b3a09 100644 --- a/primitives/src/storage_keys.rs +++ b/primitives/src/storage_keys.rs @@ -5,6 +5,7 @@ use scale::{Decode, Encode, MaxEncodedLen}; pub enum RuntimeStateKeys { Nfts(NftsKeys), ParachainSystem(ParachainSystemKeys), + TrustBackedAssets(TrustBackedAssetsKeys), } #[derive(Encode, Decode, Debug, MaxEncodedLen)] @@ -33,3 +34,9 @@ pub enum NftsKeys { /// Get the attribute value of `item` of `collection` corresponding to `key`. CollectionAttribute(CollectionId, BoundedVec), } + +#[derive(Encode, Decode, Debug, MaxEncodedLen)] +pub enum TrustBackedAssetsKeys { + /// Check if the asset exists. + AssetExists(AssetId), +} diff --git a/runtime/devnet/src/config/assets.rs b/runtime/devnet/src/config/assets.rs index 34035c1f..f51f8875 100644 --- a/runtime/devnet/src/config/assets.rs +++ b/runtime/devnet/src/config/assets.rs @@ -1,6 +1,6 @@ use crate::{ - deposit, AccountId, Assets, Balance, Balances, BlockNumber, Nfts, Runtime, RuntimeEvent, - RuntimeHoldReason, DAYS, EXISTENTIAL_DEPOSIT, UNIT, + deposit, AccountId, Balance, Balances, BlockNumber, Nfts, Runtime, RuntimeEvent, + RuntimeHoldReason, TrustBackedAssets, DAYS, EXISTENTIAL_DEPOSIT, UNIT, }; use frame_support::{ parameter_types, @@ -84,9 +84,9 @@ impl pallet_nft_fractionalization::Config for Runtime { type StringLimit = AssetsStringLimit; type NftCollectionId = ::CollectionId; type NftId = ::ItemId; - type AssetBalance = >::Balance; - type AssetId = >::AssetId; - type Assets = Assets; + type AssetBalance = >::Balance; + type AssetId = >::AssetId; + type Assets = TrustBackedAssets; type Nfts = Nfts; type PalletId = NftFractionalizationPalletId; type WeightInfo = pallet_nft_fractionalization::weights::SubstrateWeight; @@ -95,9 +95,9 @@ impl pallet_nft_fractionalization::Config for Runtime { type BenchmarkHelper = (); } -pub type TrustBackedAssets = pallet_assets::Instance1; -pub type TrustBackedAssetsCall = pallet_assets::Call; -impl pallet_assets::Config for Runtime { +pub type TrustBackedAssetsInstance = pallet_assets::Instance1; +pub(crate) type TrustBackedAssetsCall = pallet_assets::Call; +impl pallet_assets::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Balance = Balance; type AssetId = AssetIdForTrustBackedAssets; diff --git a/runtime/devnet/src/extensions.rs b/runtime/devnet/src/extensions.rs index 7e2a53c0..c8e88eb2 100644 --- a/runtime/devnet/src/extensions.rs +++ b/runtime/devnet/src/extensions.rs @@ -3,15 +3,15 @@ use frame_support::traits::{Contains, OriginTrait}; use frame_support::{ dispatch::{GetDispatchInfo, RawOrigin}, pallet_prelude::*, - traits::nonfungibles_v2::Inspect, + traits::{fungibles::Inspect, nonfungibles_v2::Inspect as NonFungiblesInspect}, }; use pallet_contracts::chain_extension::{ BufInBufOutState, ChainExtension, ChargedAmount, Environment, Ext, InitState, RetVal, }; use pop_primitives::{ cross_chain::CrossChainMessage, - storage_keys::{NftsKeys, ParachainSystemKeys, RuntimeStateKeys}, - CollectionId, ItemId, + storage_keys::{NftsKeys, ParachainSystemKeys, RuntimeStateKeys, TrustBackedAssetsKeys}, + AssetId, CollectionId, ItemId, }; use sp_core::crypto::UncheckedFrom; use sp_runtime::{ @@ -24,7 +24,10 @@ use xcm::{ VersionedXcm, }; -use crate::{AccountId, AllowedApiCalls, RuntimeCall, RuntimeOrigin, UNIT}; +use crate::{ + assets_config::TrustBackedAssetsInstance, AccountId, AllowedPopApiCalls, RuntimeCall, + RuntimeOrigin, UNIT, +}; const LOG_TARGET: &str = "pop-api::extension"; @@ -37,6 +40,7 @@ impl ChainExtension for PopApiExtension where T: pallet_contracts::Config + pallet_xcm::Config + + pallet_assets::Config + pallet_nfts::Config + cumulus_pallet_parachain_system::Config + frame_system::Config< @@ -120,7 +124,7 @@ where log::debug!(target:LOG_TARGET, "{} inputted RuntimeCall: {:?}", log_prefix, call); - origin.add_filter(AllowedApiCalls::contains); + origin.add_filter(AllowedPopApiCalls::contains); match call.dispatch(origin) { Ok(info) => { @@ -192,6 +196,7 @@ where fn read_state(env: Environment) -> Result<(), DispatchError> where T: pallet_contracts::Config + + pallet_assets::Config + pallet_nfts::Config + cumulus_pallet_parachain_system::Config + frame_system::Config, @@ -217,6 +222,9 @@ where RuntimeStateKeys::ParachainSystem(key) => { read_parachain_system_state::(key, &mut env) }, + RuntimeStateKeys::TrustBackedAssets(key) => { + read_trust_backed_assets_state::(key, &mut env) + }, }? .encode(); @@ -292,6 +300,23 @@ where } } +fn read_trust_backed_assets_state( + key: TrustBackedAssetsKeys, + env: &mut Environment, +) -> Result, DispatchError> +where + T: pallet_contracts::Config + + pallet_assets::Config, + E: Ext, +{ + match key { + TrustBackedAssetsKeys::AssetExists(id) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_assets::Pallet::::asset_exists(id).encode()) + }, + } +} + fn send_xcm(env: Environment) -> Result<(), DispatchError> where T: pallet_contracts::Config @@ -828,6 +853,110 @@ mod tests { }); } + #[test] + #[ignore] + fn dispatch_trust_backed_assets_mint_from_contract_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + + let (wasm_binary, _) = load_wasm_module::( + "../../pop-api/examples/trust_backed_assets/target/ink/pop_api_trust_backed_assets_example.wasm", + ) + .unwrap(); + + let init_value = 100; + + let result = Contracts::bare_instantiate( + ALICE, + init_value, + GAS_LIMIT, + None, + Code::Upload(wasm_binary), + function_selector("new"), + vec![], + DEBUG_OUTPUT, + pallet_contracts::CollectEvents::Skip, + ) + .result + .unwrap(); + + assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); + let addr = result.account_id; + + let asset_id: u32 = 1; + let min_balance = 1; + let amount: u128 = 100 * UNIT; + let function = function_selector("mint_asset_through_runtime"); + let params = [function, asset_id.encode(), BOB.encode(), amount.encode()].concat(); + + // Mint asset which does not exist. + let result = Contracts::bare_call( + ALICE, + addr.clone(), + 0, + GAS_LIMIT, + None, + params.clone(), + DEBUG_OUTPUT, + pallet_contracts::CollectEvents::Skip, + pallet_contracts::Determinism::Enforced, + ); + + if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { + log::debug!( + "Contract debug buffer - {:?}", + String::from_utf8(result.debug_message.clone()) + ); + log::debug!("result: {:?}", result); + } + + // Check for revert. + assert!(result.result.unwrap().did_revert(), "Contract should have been reverted!"); + + // Create asset with contract as owner. + assert_eq!( + TrustBackedAssets::force_create( + RuntimeOrigin::root(), + asset_id.into(), + addr.clone().into(), + true, + min_balance, + ), + Ok(()) + ); + + // Check Bob's asset balance before minting through contract. + let bob_balance_before = TrustBackedAssets::balance(asset_id, &BOB); + assert_eq!(bob_balance_before, 0); + + let result = Contracts::bare_call( + ALICE, + addr.clone(), + 0, + GAS_LIMIT, + None, + params, + DEBUG_OUTPUT, + pallet_contracts::CollectEvents::Skip, + pallet_contracts::Determinism::Enforced, + ); + + if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { + log::debug!( + "Contract debug buffer - {:?}", + String::from_utf8(result.debug_message.clone()) + ); + log::debug!("result: {:?}", result); + } + + // Check for revert + assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); + + let bob_balance_after = TrustBackedAssets::balance(asset_id, &BOB); + assert_eq!(bob_balance_after, bob_balance_before + amount); + }); + } + #[test] #[ignore] fn allow_call_filter_blocks_call() { diff --git a/runtime/devnet/src/lib.rs b/runtime/devnet/src/lib.rs index ffca8a12..52a0938f 100644 --- a/runtime/devnet/src/lib.rs +++ b/runtime/devnet/src/lib.rs @@ -251,46 +251,77 @@ impl Contains for FilteredCalls { } /// A type to identify allowed calls to the Runtime from contracts. Used by Pop API -pub struct AllowedApiCalls; -impl Contains for crate::AllowedApiCalls { +pub struct AllowedPopApiCalls; +impl Contains for crate::AllowedPopApiCalls { fn contains(c: &RuntimeCall) -> bool { + use assets_config::TrustBackedAssetsCall; use pallet_nfts::Call as NftsCall; matches!( c, RuntimeCall::Balances(BalancesCall::transfer_keep_alive { .. }) - | RuntimeCall::Nfts( - NftsCall::create { .. } - | NftsCall::destroy { .. } - | NftsCall::mint { .. } | NftsCall::burn { .. } - | NftsCall::transfer { .. } - | NftsCall::redeposit { .. } - | NftsCall::lock_item_transfer { .. } - | NftsCall::unlock_item_transfer { .. } - | NftsCall::lock_collection { .. } - | NftsCall::transfer_ownership { .. } - | NftsCall::set_team { .. } - | NftsCall::approve_transfer { .. } - | NftsCall::cancel_approval { .. } - | NftsCall::clear_all_transfer_approvals { .. } - | NftsCall::lock_item_properties { .. } - | NftsCall::set_attribute { .. } - | NftsCall::clear_attribute { .. } - | NftsCall::approve_item_attributes { .. } - | NftsCall::cancel_item_attributes_approval { .. } - | NftsCall::set_metadata { .. } - | NftsCall::clear_metadata { .. } - | NftsCall::set_collection_metadata { .. } - | NftsCall::clear_collection_metadata { .. } - | NftsCall::set_accept_ownership { .. } - | NftsCall::set_collection_max_supply { .. } - | NftsCall::update_mint_settings { .. } - | NftsCall::set_price { .. } - | NftsCall::buy_item { .. } - | NftsCall::pay_tips { .. } - | NftsCall::create_swap { .. } - | NftsCall::cancel_swap { .. } - | NftsCall::claim_swap { .. } - ) + | RuntimeCall::TrustBackedAssets( + TrustBackedAssetsCall::create { .. } + | TrustBackedAssetsCall::start_destroy { .. } + | TrustBackedAssetsCall::destroy_accounts { .. } + | TrustBackedAssetsCall::destroy_approvals { .. } + | TrustBackedAssetsCall::finish_destroy { .. } + | TrustBackedAssetsCall::mint { .. } + | TrustBackedAssetsCall::burn { .. } + | TrustBackedAssetsCall::transfer { .. } + | TrustBackedAssetsCall::transfer_keep_alive { .. } + | TrustBackedAssetsCall::force_transfer { .. } + | TrustBackedAssetsCall::freeze { .. } + | TrustBackedAssetsCall::thaw { .. } + | TrustBackedAssetsCall::freeze_asset { .. } + | TrustBackedAssetsCall::thaw_asset { .. } + | TrustBackedAssetsCall::transfer_ownership { .. } + | TrustBackedAssetsCall::set_team { .. } + | TrustBackedAssetsCall::set_metadata { .. } + | TrustBackedAssetsCall::clear_metadata { .. } + | TrustBackedAssetsCall::approve_transfer { .. } + | TrustBackedAssetsCall::cancel_approval { .. } + | TrustBackedAssetsCall::force_cancel_approval { .. } + | TrustBackedAssetsCall::transfer_approved { .. } + | TrustBackedAssetsCall::touch { .. } + | TrustBackedAssetsCall::refund { .. } + | TrustBackedAssetsCall::set_min_balance { .. } + | TrustBackedAssetsCall::touch_other { .. } + | TrustBackedAssetsCall::refund_other { .. } + | TrustBackedAssetsCall::block { .. } + ) | RuntimeCall::Nfts( + NftsCall::create { .. } + | NftsCall::destroy { .. } + | NftsCall::mint { .. } + | NftsCall::burn { .. } + | NftsCall::transfer { .. } + | NftsCall::redeposit { .. } + | NftsCall::lock_item_transfer { .. } + | NftsCall::unlock_item_transfer { .. } + | NftsCall::lock_collection { .. } + | NftsCall::transfer_ownership { .. } + | NftsCall::set_team { .. } + | NftsCall::approve_transfer { .. } + | NftsCall::cancel_approval { .. } + | NftsCall::clear_all_transfer_approvals { .. } + | NftsCall::lock_item_properties { .. } + | NftsCall::set_attribute { .. } + | NftsCall::clear_attribute { .. } + | NftsCall::approve_item_attributes { .. } + | NftsCall::cancel_item_attributes_approval { .. } + | NftsCall::set_metadata { .. } + | NftsCall::clear_metadata { .. } + | NftsCall::set_collection_metadata { .. } + | NftsCall::clear_collection_metadata { .. } + | NftsCall::set_accept_ownership { .. } + | NftsCall::set_collection_max_supply { .. } + | NftsCall::update_mint_settings { .. } + | NftsCall::set_price { .. } + | NftsCall::buy_item { .. } + | NftsCall::pay_tips { .. } + | NftsCall::create_swap { .. } + | NftsCall::cancel_swap { .. } + | NftsCall::claim_swap { .. } + ) ) } } @@ -631,7 +662,7 @@ construct_runtime!( // Assets Nfts: pallet_nfts = 50, NftFractionalization: pallet_nft_fractionalization = 51, - Assets: pallet_assets:: = 52, + TrustBackedAssets: pallet_assets:: = 52, } ); diff --git a/runtime/testnet/src/config/assets.rs b/runtime/testnet/src/config/assets.rs index 34035c1f..f51f8875 100644 --- a/runtime/testnet/src/config/assets.rs +++ b/runtime/testnet/src/config/assets.rs @@ -1,6 +1,6 @@ use crate::{ - deposit, AccountId, Assets, Balance, Balances, BlockNumber, Nfts, Runtime, RuntimeEvent, - RuntimeHoldReason, DAYS, EXISTENTIAL_DEPOSIT, UNIT, + deposit, AccountId, Balance, Balances, BlockNumber, Nfts, Runtime, RuntimeEvent, + RuntimeHoldReason, TrustBackedAssets, DAYS, EXISTENTIAL_DEPOSIT, UNIT, }; use frame_support::{ parameter_types, @@ -84,9 +84,9 @@ impl pallet_nft_fractionalization::Config for Runtime { type StringLimit = AssetsStringLimit; type NftCollectionId = ::CollectionId; type NftId = ::ItemId; - type AssetBalance = >::Balance; - type AssetId = >::AssetId; - type Assets = Assets; + type AssetBalance = >::Balance; + type AssetId = >::AssetId; + type Assets = TrustBackedAssets; type Nfts = Nfts; type PalletId = NftFractionalizationPalletId; type WeightInfo = pallet_nft_fractionalization::weights::SubstrateWeight; @@ -95,9 +95,9 @@ impl pallet_nft_fractionalization::Config for Runtime { type BenchmarkHelper = (); } -pub type TrustBackedAssets = pallet_assets::Instance1; -pub type TrustBackedAssetsCall = pallet_assets::Call; -impl pallet_assets::Config for Runtime { +pub type TrustBackedAssetsInstance = pallet_assets::Instance1; +pub(crate) type TrustBackedAssetsCall = pallet_assets::Call; +impl pallet_assets::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Balance = Balance; type AssetId = AssetIdForTrustBackedAssets; diff --git a/runtime/testnet/src/extensions.rs b/runtime/testnet/src/extensions.rs index d6f2d656..0d552090 100644 --- a/runtime/testnet/src/extensions.rs +++ b/runtime/testnet/src/extensions.rs @@ -3,23 +3,25 @@ use frame_support::traits::{Contains, OriginTrait}; use frame_support::{ dispatch::{GetDispatchInfo, RawOrigin}, pallet_prelude::*, - traits::nonfungibles_v2::Inspect, + traits::{fungibles::Inspect, nonfungibles_v2::Inspect as NonFungiblesInspect}, }; use pallet_contracts::chain_extension::{ BufInBufOutState, ChainExtension, ChargedAmount, Environment, Ext, InitState, RetVal, }; use pop_primitives::{ - storage_keys::{NftsKeys, ParachainSystemKeys, RuntimeStateKeys}, - CollectionId, ItemId, + storage_keys::{NftsKeys, ParachainSystemKeys, RuntimeStateKeys, TrustBackedAssetsKeys}, + AssetId, CollectionId, ItemId, }; use sp_core::crypto::UncheckedFrom; use sp_runtime::{ traits::{BlockNumberProvider, Dispatchable}, DispatchError, }; -use sp_std::vec::Vec; -use crate::{AccountId, AllowedApiCalls, RuntimeCall, RuntimeOrigin}; +use crate::{ + assets_config::TrustBackedAssetsInstance, AccountId, AllowedPopApiCalls, RuntimeCall, + RuntimeOrigin, +}; const LOG_TARGET: &str = "pop-api::extension"; @@ -32,6 +34,7 @@ impl ChainExtension for PopApiExtension where T: pallet_contracts::Config + pallet_xcm::Config + + pallet_assets::Config + pallet_nfts::Config + cumulus_pallet_parachain_system::Config + frame_system::Config< @@ -109,7 +112,7 @@ where log::debug!(target:LOG_TARGET, "{} inputted RuntimeCall: {:?}", log_prefix, call); - origin.add_filter(AllowedApiCalls::contains); + origin.add_filter(AllowedPopApiCalls::contains); match call.dispatch(origin) { Ok(info) => { @@ -181,6 +184,7 @@ where fn read_state(env: Environment) -> Result<(), DispatchError> where T: pallet_contracts::Config + + pallet_assets::Config + pallet_nfts::Config + cumulus_pallet_parachain_system::Config + frame_system::Config, @@ -206,6 +210,9 @@ where RuntimeStateKeys::ParachainSystem(key) => { read_parachain_system_state::(key, &mut env) }, + RuntimeStateKeys::TrustBackedAssets(key) => { + read_trust_backed_assets_state::(key, &mut env) + }, }? .encode(); @@ -281,6 +288,23 @@ where } } +fn read_trust_backed_assets_state( + key: TrustBackedAssetsKeys, + env: &mut Environment, +) -> Result, DispatchError> +where + T: pallet_contracts::Config + + pallet_assets::Config, + E: Ext, +{ + match key { + TrustBackedAssetsKeys::AssetExists(id) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_assets::Pallet::::asset_exists(id).encode()) + }, + } +} + #[cfg(test)] mod tests { pub use super::*; @@ -481,7 +505,7 @@ mod tests { log::debug!("result: {:?}", result); } - // check for revert + // Check for revert. assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); assert_eq!(Nfts::owner(collection_id, item_id), Some(BOB.into())); @@ -546,7 +570,7 @@ mod tests { log::debug!("result: {:?}", result); } - // check for revert with expected error + // Check for revert with expected error. let result = result.result.unwrap(); assert!(result.did_revert()); }); @@ -606,7 +630,7 @@ mod tests { log::debug!("result: {:?}", result); } - // check for revert + // Check for revert. assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); }); } @@ -669,7 +693,7 @@ mod tests { log::debug!("result: {:?}", result); } - // check for revert + // Check for revert. assert!( result.result.is_err(), "Contract execution should have failed - unimplemented runtime call!" @@ -677,6 +701,110 @@ mod tests { }); } + #[test] + #[ignore] + fn dispatch_trust_backed_assets_mint_from_contract_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + + let (wasm_binary, _) = load_wasm_module::( + "../../pop-api/examples/trust_backed_assets/target/ink/pop_api_trust_backed_assets_example.wasm", + ) + .unwrap(); + + let init_value = 100; + + let result = Contracts::bare_instantiate( + ALICE, + init_value, + GAS_LIMIT, + None, + Code::Upload(wasm_binary), + function_selector("new"), + vec![], + DEBUG_OUTPUT, + pallet_contracts::CollectEvents::Skip, + ) + .result + .unwrap(); + + assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); + let addr = result.account_id; + + let asset_id: u32 = 1; + let min_balance = 1; + let amount: u128 = 100 * UNIT; + let function = function_selector("mint_asset_through_runtime"); + let params = [function, asset_id.encode(), BOB.encode(), amount.encode()].concat(); + + // Mint asset which does not exist. + let result = Contracts::bare_call( + ALICE, + addr.clone(), + 0, + GAS_LIMIT, + None, + params.clone(), + DEBUG_OUTPUT, + pallet_contracts::CollectEvents::Skip, + pallet_contracts::Determinism::Enforced, + ); + + if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { + log::debug!( + "Contract debug buffer - {:?}", + String::from_utf8(result.debug_message.clone()) + ); + log::debug!("result: {:?}", result); + } + + // Check for revert. + assert!(result.result.unwrap().did_revert(), "Contract should have been reverted!"); + + // Create asset with contract as owner. + assert_eq!( + TrustBackedAssets::force_create( + RuntimeOrigin::root(), + asset_id.into(), + addr.clone().into(), + true, + min_balance, + ), + Ok(()) + ); + + // Check Bob's asset balance before minting through contract. + let bob_balance_before = TrustBackedAssets::balance(asset_id, &BOB); + assert_eq!(bob_balance_before, 0); + + let result = Contracts::bare_call( + ALICE, + addr.clone(), + 0, + GAS_LIMIT, + None, + params, + DEBUG_OUTPUT, + pallet_contracts::CollectEvents::Skip, + pallet_contracts::Determinism::Enforced, + ); + + if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { + log::debug!( + "Contract debug buffer - {:?}", + String::from_utf8(result.debug_message.clone()) + ); + log::debug!("result: {:?}", result); + } + + // Check for revert + assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); + + let bob_balance_after = TrustBackedAssets::balance(asset_id, &BOB); + assert_eq!(bob_balance_after, bob_balance_before + amount); + }); + } + #[test] #[ignore] fn allow_call_filter_blocks_call() { @@ -731,7 +859,7 @@ mod tests { log::debug!("filtered result: {:?}", result); } - // check for revert + // Check for revert. assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); }); } diff --git a/runtime/testnet/src/lib.rs b/runtime/testnet/src/lib.rs index 37f378f7..312d9b50 100644 --- a/runtime/testnet/src/lib.rs +++ b/runtime/testnet/src/lib.rs @@ -250,46 +250,77 @@ impl Contains for FilteredCalls { } /// A type to identify allowed calls to the Runtime from contracts. Used by Pop API -pub struct AllowedApiCalls; -impl Contains for crate::AllowedApiCalls { +pub struct AllowedPopApiCalls; +impl Contains for crate::AllowedPopApiCalls { fn contains(c: &RuntimeCall) -> bool { + use assets_config::TrustBackedAssetsCall; use pallet_nfts::Call as NftsCall; matches!( c, RuntimeCall::Balances(BalancesCall::transfer_keep_alive { .. }) - | RuntimeCall::Nfts( - NftsCall::create { .. } - | NftsCall::destroy { .. } - | NftsCall::mint { .. } | NftsCall::burn { .. } - | NftsCall::transfer { .. } - | NftsCall::redeposit { .. } - | NftsCall::lock_item_transfer { .. } - | NftsCall::unlock_item_transfer { .. } - | NftsCall::lock_collection { .. } - | NftsCall::transfer_ownership { .. } - | NftsCall::set_team { .. } - | NftsCall::approve_transfer { .. } - | NftsCall::cancel_approval { .. } - | NftsCall::clear_all_transfer_approvals { .. } - | NftsCall::lock_item_properties { .. } - | NftsCall::set_attribute { .. } - | NftsCall::clear_attribute { .. } - | NftsCall::approve_item_attributes { .. } - | NftsCall::cancel_item_attributes_approval { .. } - | NftsCall::set_metadata { .. } - | NftsCall::clear_metadata { .. } - | NftsCall::set_collection_metadata { .. } - | NftsCall::clear_collection_metadata { .. } - | NftsCall::set_accept_ownership { .. } - | NftsCall::set_collection_max_supply { .. } - | NftsCall::update_mint_settings { .. } - | NftsCall::set_price { .. } - | NftsCall::buy_item { .. } - | NftsCall::pay_tips { .. } - | NftsCall::create_swap { .. } - | NftsCall::cancel_swap { .. } - | NftsCall::claim_swap { .. } - ) + | RuntimeCall::TrustBackedAssets( + TrustBackedAssetsCall::create { .. } + | TrustBackedAssetsCall::start_destroy { .. } + | TrustBackedAssetsCall::destroy_accounts { .. } + | TrustBackedAssetsCall::destroy_approvals { .. } + | TrustBackedAssetsCall::finish_destroy { .. } + | TrustBackedAssetsCall::mint { .. } + | TrustBackedAssetsCall::burn { .. } + | TrustBackedAssetsCall::transfer { .. } + | TrustBackedAssetsCall::transfer_keep_alive { .. } + | TrustBackedAssetsCall::force_transfer { .. } + | TrustBackedAssetsCall::freeze { .. } + | TrustBackedAssetsCall::thaw { .. } + | TrustBackedAssetsCall::freeze_asset { .. } + | TrustBackedAssetsCall::thaw_asset { .. } + | TrustBackedAssetsCall::transfer_ownership { .. } + | TrustBackedAssetsCall::set_team { .. } + | TrustBackedAssetsCall::set_metadata { .. } + | TrustBackedAssetsCall::clear_metadata { .. } + | TrustBackedAssetsCall::approve_transfer { .. } + | TrustBackedAssetsCall::cancel_approval { .. } + | TrustBackedAssetsCall::force_cancel_approval { .. } + | TrustBackedAssetsCall::transfer_approved { .. } + | TrustBackedAssetsCall::touch { .. } + | TrustBackedAssetsCall::refund { .. } + | TrustBackedAssetsCall::set_min_balance { .. } + | TrustBackedAssetsCall::touch_other { .. } + | TrustBackedAssetsCall::refund_other { .. } + | TrustBackedAssetsCall::block { .. } + ) | RuntimeCall::Nfts( + NftsCall::create { .. } + | NftsCall::destroy { .. } + | NftsCall::mint { .. } + | NftsCall::burn { .. } + | NftsCall::transfer { .. } + | NftsCall::redeposit { .. } + | NftsCall::lock_item_transfer { .. } + | NftsCall::unlock_item_transfer { .. } + | NftsCall::lock_collection { .. } + | NftsCall::transfer_ownership { .. } + | NftsCall::set_team { .. } + | NftsCall::approve_transfer { .. } + | NftsCall::cancel_approval { .. } + | NftsCall::clear_all_transfer_approvals { .. } + | NftsCall::lock_item_properties { .. } + | NftsCall::set_attribute { .. } + | NftsCall::clear_attribute { .. } + | NftsCall::approve_item_attributes { .. } + | NftsCall::cancel_item_attributes_approval { .. } + | NftsCall::set_metadata { .. } + | NftsCall::clear_metadata { .. } + | NftsCall::set_collection_metadata { .. } + | NftsCall::clear_collection_metadata { .. } + | NftsCall::set_accept_ownership { .. } + | NftsCall::set_collection_max_supply { .. } + | NftsCall::update_mint_settings { .. } + | NftsCall::set_price { .. } + | NftsCall::buy_item { .. } + | NftsCall::pay_tips { .. } + | NftsCall::create_swap { .. } + | NftsCall::cancel_swap { .. } + | NftsCall::claim_swap { .. } + ) ) } } @@ -630,7 +661,7 @@ construct_runtime!( // Assets Nfts: pallet_nfts = 50, NftFractionalization: pallet_nft_fractionalization = 51, - Assets: pallet_assets:: = 52, + TrustBackedAssets: pallet_assets:: = 52, } ); From 418aa6311498112ec43a23ea72962a43464826b3 Mon Sep 17 00:00:00 2001 From: R0GUE Date: Fri, 12 Apr 2024 15:33:32 +0200 Subject: [PATCH 02/76] style: renaming assets example --- pop-api/examples/trust_backed_assets/lib.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/pop-api/examples/trust_backed_assets/lib.rs b/pop-api/examples/trust_backed_assets/lib.rs index 8b2fbda6..3606f852 100755 --- a/pop-api/examples/trust_backed_assets/lib.rs +++ b/pop-api/examples/trust_backed_assets/lib.rs @@ -1,5 +1,9 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] +// Utilizing Trust Backed Assets with the Pop API. +// +// This example demonstrates interaction with trust backed assets via the assets pallet. Trust backed assets are originated +// and managed within Pop Network, harnessing the platform's inherent trust, security, and governance models. use pop_api::assets::trust_backed as trust_backed_assets; #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] @@ -16,14 +20,14 @@ impl From for ContractError { } #[ink::contract(env = pop_api::Environment)] -mod pop_api_extension_demo { +mod pop_api_tb_assets_example { use super::*; #[ink(storage)] #[derive(Default)] - pub struct PopApiExtensionDemo; + pub struct PopApiTBAssetsExample; - impl PopApiExtensionDemo { + impl PopApiTBAssetsExample { #[ink(constructor, payable)] pub fn new() -> Self { ink::env::debug_println!("Contract::new"); @@ -45,13 +49,15 @@ mod pop_api_extension_demo { ); // Check if asset doesn't exist. - if !trust_backed_assets::asset_exists(id)? { + if !trust_backed_assets::asset_exists(id)? { return Err(ContractError::UnknownAsset); } // Mint asset via pop api. trust_backed_assets::mint(id, beneficiary, amount)?; - ink::env::debug_println!("Contract::mint_asset_through_runtime: asset(s) minted successfully"); + ink::env::debug_println!( + "Contract::mint_asset_through_runtime: asset(s) minted successfully" + ); Ok(()) } } @@ -62,8 +68,7 @@ mod pop_api_extension_demo { #[ink::test] fn default_works() { - PopApiExtensionDemo::new(); + PopApiTBAssetsExample::new(); } } } - From 3d385df789e84a678badd94a70a6833d61329437 Mon Sep 17 00:00:00 2001 From: R0GUE Date: Mon, 22 Apr 2024 15:05:31 +0200 Subject: [PATCH 03/76] refactor: chain extension tests --- Cargo.lock | 8 + pop-api/Cargo.toml | 2 +- .../.gitignore | 0 .../Cargo.toml | 2 +- pop-api/examples/fungibles/expanded.rs | 2766 +++++++++++++++++ pop-api/examples/fungibles/lib.rs | 176 ++ pop-api/examples/trust_backed_assets/lib.rs | 74 - pop-api/src/lib.rs | 15 +- pop-api/src/v0/assets/fungibles.rs | 554 ++++ pop-api/src/v0/assets/mod.rs | 2 +- pop-api/src/v0/assets/trust_backed.rs | 519 ---- pop-api/src/v0/balances.rs | 2 +- pop-api/src/v0/contracts.rs | 156 + pop-api/src/v0/mod.rs | 3 +- primitives/Cargo.toml | 14 +- primitives/src/lib.rs | 12 + primitives/src/storage_keys.rs | 14 +- runtime/devnet/Cargo.toml | 2 +- runtime/devnet/src/config/assets.rs | 8 +- runtime/devnet/src/config/mod.rs | 2 +- runtime/devnet/src/config/proxy.rs | 40 +- runtime/devnet/src/extensions.rs | 1018 ------ runtime/devnet/src/extensions/mod.rs | 896 ++++++ .../src/extensions/tests/local_fungibles.rs | 302 ++ runtime/devnet/src/extensions/tests/mod.rs | 86 + runtime/devnet/src/lib.rs | 62 +- runtime/testnet/Cargo.toml | 2 +- runtime/testnet/src/config/assets.rs | 8 +- runtime/testnet/src/config/proxy.rs | 40 +- runtime/testnet/src/extensions.rs | 138 +- runtime/testnet/src/lib.rs | 99 +- 31 files changed, 5116 insertions(+), 1906 deletions(-) rename pop-api/examples/{trust_backed_assets => fungibles}/.gitignore (100%) rename pop-api/examples/{trust_backed_assets => fungibles}/Cargo.toml (93%) create mode 100644 pop-api/examples/fungibles/expanded.rs create mode 100755 pop-api/examples/fungibles/lib.rs delete mode 100755 pop-api/examples/trust_backed_assets/lib.rs create mode 100644 pop-api/src/v0/assets/fungibles.rs delete mode 100644 pop-api/src/v0/assets/trust_backed.rs create mode 100644 pop-api/src/v0/contracts.rs delete mode 100644 runtime/devnet/src/extensions.rs create mode 100644 runtime/devnet/src/extensions/mod.rs create mode 100644 runtime/devnet/src/extensions/tests/local_fungibles.rs create mode 100644 runtime/devnet/src/extensions/tests/mod.rs diff --git a/Cargo.lock b/Cargo.lock index e43d2b18..17cc70e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9673,6 +9673,8 @@ version = "0.0.0" dependencies = [ "bounded-collections 0.1.9", "parity-scale-codec", + "scale-decode", + "scale-encode", "scale-info", ] @@ -9720,16 +9722,19 @@ dependencies = [ "pallet-collator-selection", "pallet-contracts", "pallet-message-queue", + "pallet-multisig", "pallet-nft-fractionalization", "pallet-nfts", "pallet-nfts-runtime-api", "pallet-preimage", + "pallet-proxy", "pallet-scheduler", "pallet-session", "pallet-sudo", "pallet-timestamp", "pallet-transaction-payment", "pallet-transaction-payment-rpc-runtime-api", + "pallet-utility", "pallet-xcm", "parachains-common", "parity-scale-codec", @@ -9790,16 +9795,19 @@ dependencies = [ "pallet-collator-selection", "pallet-contracts", "pallet-message-queue", + "pallet-multisig", "pallet-nft-fractionalization", "pallet-nfts", "pallet-nfts-runtime-api", "pallet-preimage", + "pallet-proxy", "pallet-scheduler", "pallet-session", "pallet-sudo", "pallet-timestamp", "pallet-transaction-payment", "pallet-transaction-payment-rpc-runtime-api", + "pallet-utility", "pallet-xcm", "parachains-common", "parity-scale-codec", diff --git a/pop-api/Cargo.toml b/pop-api/Cargo.toml index 0fa1119c..80818235 100644 --- a/pop-api/Cargo.toml +++ b/pop-api/Cargo.toml @@ -13,7 +13,7 @@ scale-info = { version = "2.6", default-features = false, features = ["derive"] sp-io = { version = "23.0.0", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] } sp-runtime = { version = "24.0", default-features = false } -pop-primitives = { path = "../primitives", default-features = false } +pop-primitives = { path = "../primitives", features = ["devnet"], default-features = false } [lib] name = "pop_api" diff --git a/pop-api/examples/trust_backed_assets/.gitignore b/pop-api/examples/fungibles/.gitignore similarity index 100% rename from pop-api/examples/trust_backed_assets/.gitignore rename to pop-api/examples/fungibles/.gitignore diff --git a/pop-api/examples/trust_backed_assets/Cargo.toml b/pop-api/examples/fungibles/Cargo.toml similarity index 93% rename from pop-api/examples/trust_backed_assets/Cargo.toml rename to pop-api/examples/fungibles/Cargo.toml index 3c3716fc..1fe32d6d 100755 --- a/pop-api/examples/trust_backed_assets/Cargo.toml +++ b/pop-api/examples/fungibles/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "pop_api_trust_backed_assets_example" +name = "fungibles" version = "0.1.0" authors = ["[your_name] <[your_email]>"] edition = "2021" diff --git a/pop-api/examples/fungibles/expanded.rs b/pop-api/examples/fungibles/expanded.rs new file mode 100644 index 00000000..c73cdeb7 --- /dev/null +++ b/pop-api/examples/fungibles/expanded.rs @@ -0,0 +1,2766 @@ +#![feature(prelude_import)] +#[prelude_import] +use std::prelude::rust_2021::*; +#[macro_use] +extern crate std; +use pop_api::{ + primitives::{AccountId as AccountId32, AssetId}, + assets::fungibles::*, +}; +pub enum FungiblesError { + /// Not enough balance to fulfill a request is available. + InsufficientBalance, + /// Not enough allowance to fulfill a request is available. + InsufficientAllowance, + /// The asset status is not the expected status. + IncorrectStatus, + /// The asset ID is already taken. + InUse, + /// Minimum balance should be non-zero. + MinBalanceZero, + /// The signing account has no permission to do the operation. + NoPermission, + /// The given asset ID is unknown. + Unknown, + /// Recipient's address is zero. + ZeroRecipientAddress, + /// Sender's address is zero. + ZeroSenderAddress, +} +#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] +const _: () = { + impl ::scale_info::TypeInfo for FungiblesError { + type Identity = Self; + fn type_info() -> ::scale_info::Type { + ::scale_info::Type::builder() + .path( + ::scale_info::Path::new_with_replace( + "FungiblesError", + "fungibles", + &[], + ), + ) + .type_params(::alloc::vec::Vec::new()) + .variant( + ::scale_info::build::Variants::new() + .variant( + "InsufficientBalance", + |v| { + v + .index(0usize as ::core::primitive::u8) + .docs( + &["Not enough balance to fulfill a request is available."], + ) + }, + ) + .variant( + "InsufficientAllowance", + |v| { + v + .index(1usize as ::core::primitive::u8) + .docs( + &["Not enough allowance to fulfill a request is available."], + ) + }, + ) + .variant( + "IncorrectStatus", + |v| { + v + .index(2usize as ::core::primitive::u8) + .docs(&["The asset status is not the expected status."]) + }, + ) + .variant( + "InUse", + |v| { + v + .index(3usize as ::core::primitive::u8) + .docs(&["The asset ID is already taken."]) + }, + ) + .variant( + "MinBalanceZero", + |v| { + v + .index(4usize as ::core::primitive::u8) + .docs(&["Minimum balance should be non-zero."]) + }, + ) + .variant( + "NoPermission", + |v| { + v + .index(5usize as ::core::primitive::u8) + .docs( + &[ + "The signing account has no permission to do the operation.", + ], + ) + }, + ) + .variant( + "Unknown", + |v| { + v + .index(6usize as ::core::primitive::u8) + .docs(&["The given asset ID is unknown."]) + }, + ) + .variant( + "ZeroRecipientAddress", + |v| { + v + .index(7usize as ::core::primitive::u8) + .docs(&["Recipient's address is zero."]) + }, + ) + .variant( + "ZeroSenderAddress", + |v| { + v + .index(8usize as ::core::primitive::u8) + .docs(&["Sender's address is zero."]) + }, + ), + ) + } + } +}; +#[automatically_derived] +impl ::core::fmt::Debug for FungiblesError { + #[inline] + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + ::core::fmt::Formatter::write_str( + f, + match self { + FungiblesError::InsufficientBalance => "InsufficientBalance", + FungiblesError::InsufficientAllowance => "InsufficientAllowance", + FungiblesError::IncorrectStatus => "IncorrectStatus", + FungiblesError::InUse => "InUse", + FungiblesError::MinBalanceZero => "MinBalanceZero", + FungiblesError::NoPermission => "NoPermission", + FungiblesError::Unknown => "Unknown", + FungiblesError::ZeroRecipientAddress => "ZeroRecipientAddress", + FungiblesError::ZeroSenderAddress => "ZeroSenderAddress", + }, + ) + } +} +#[automatically_derived] +impl ::core::marker::Copy for FungiblesError {} +#[automatically_derived] +impl ::core::clone::Clone for FungiblesError { + #[inline] + fn clone(&self) -> FungiblesError { + *self + } +} +#[automatically_derived] +impl ::core::marker::StructuralPartialEq for FungiblesError {} +#[automatically_derived] +impl ::core::cmp::PartialEq for FungiblesError { + #[inline] + fn eq(&self, other: &FungiblesError) -> bool { + let __self_tag = ::core::intrinsics::discriminant_value(self); + let __arg1_tag = ::core::intrinsics::discriminant_value(other); + __self_tag == __arg1_tag + } +} +#[automatically_derived] +impl ::core::cmp::Eq for FungiblesError { + #[inline] + #[doc(hidden)] + #[coverage(off)] + fn assert_receiver_is_total_eq(&self) -> () {} +} +#[allow(deprecated)] +const _: () = { + #[automatically_derived] + impl ::scale::Encode for FungiblesError { + fn size_hint(&self) -> usize { + 1_usize + + match *self { + FungiblesError::InsufficientBalance => 0_usize, + FungiblesError::InsufficientAllowance => 0_usize, + FungiblesError::IncorrectStatus => 0_usize, + FungiblesError::InUse => 0_usize, + FungiblesError::MinBalanceZero => 0_usize, + FungiblesError::NoPermission => 0_usize, + FungiblesError::Unknown => 0_usize, + FungiblesError::ZeroRecipientAddress => 0_usize, + FungiblesError::ZeroSenderAddress => 0_usize, + _ => 0_usize, + } + } + fn encode_to<__CodecOutputEdqy: ::scale::Output + ?::core::marker::Sized>( + &self, + __codec_dest_edqy: &mut __CodecOutputEdqy, + ) { + match *self { + FungiblesError::InsufficientBalance => { + #[allow(clippy::unnecessary_cast)] + __codec_dest_edqy.push_byte(0usize as ::core::primitive::u8); + } + FungiblesError::InsufficientAllowance => { + #[allow(clippy::unnecessary_cast)] + __codec_dest_edqy.push_byte(1usize as ::core::primitive::u8); + } + FungiblesError::IncorrectStatus => { + #[allow(clippy::unnecessary_cast)] + __codec_dest_edqy.push_byte(2usize as ::core::primitive::u8); + } + FungiblesError::InUse => { + #[allow(clippy::unnecessary_cast)] + __codec_dest_edqy.push_byte(3usize as ::core::primitive::u8); + } + FungiblesError::MinBalanceZero => { + #[allow(clippy::unnecessary_cast)] + __codec_dest_edqy.push_byte(4usize as ::core::primitive::u8); + } + FungiblesError::NoPermission => { + #[allow(clippy::unnecessary_cast)] + __codec_dest_edqy.push_byte(5usize as ::core::primitive::u8); + } + FungiblesError::Unknown => { + #[allow(clippy::unnecessary_cast)] + __codec_dest_edqy.push_byte(6usize as ::core::primitive::u8); + } + FungiblesError::ZeroRecipientAddress => { + #[allow(clippy::unnecessary_cast)] + __codec_dest_edqy.push_byte(7usize as ::core::primitive::u8); + } + FungiblesError::ZeroSenderAddress => { + #[allow(clippy::unnecessary_cast)] + __codec_dest_edqy.push_byte(8usize as ::core::primitive::u8); + } + _ => {} + } + } + } + #[automatically_derived] + impl ::scale::EncodeLike for FungiblesError {} +}; +#[allow(deprecated)] +const _: () = { + #[automatically_derived] + impl ::scale::Decode for FungiblesError { + fn decode<__CodecInputEdqy: ::scale::Input>( + __codec_input_edqy: &mut __CodecInputEdqy, + ) -> ::core::result::Result { + match __codec_input_edqy + .read_byte() + .map_err(|e| { + e + .chain( + "Could not decode `FungiblesError`, failed to read variant byte", + ) + })? + { + #[allow(clippy::unnecessary_cast)] + __codec_x_edqy if __codec_x_edqy == 0usize as ::core::primitive::u8 => { + #[allow(clippy::redundant_closure_call)] + return (move || { + ::core::result::Result::Ok(FungiblesError::InsufficientBalance) + })(); + } + #[allow(clippy::unnecessary_cast)] + __codec_x_edqy if __codec_x_edqy == 1usize as ::core::primitive::u8 => { + #[allow(clippy::redundant_closure_call)] + return (move || { + ::core::result::Result::Ok(FungiblesError::InsufficientAllowance) + })(); + } + #[allow(clippy::unnecessary_cast)] + __codec_x_edqy if __codec_x_edqy == 2usize as ::core::primitive::u8 => { + #[allow(clippy::redundant_closure_call)] + return (move || { + ::core::result::Result::Ok(FungiblesError::IncorrectStatus) + })(); + } + #[allow(clippy::unnecessary_cast)] + __codec_x_edqy if __codec_x_edqy == 3usize as ::core::primitive::u8 => { + #[allow(clippy::redundant_closure_call)] + return (move || { + ::core::result::Result::Ok(FungiblesError::InUse) + })(); + } + #[allow(clippy::unnecessary_cast)] + __codec_x_edqy if __codec_x_edqy == 4usize as ::core::primitive::u8 => { + #[allow(clippy::redundant_closure_call)] + return (move || { + ::core::result::Result::Ok(FungiblesError::MinBalanceZero) + })(); + } + #[allow(clippy::unnecessary_cast)] + __codec_x_edqy if __codec_x_edqy == 5usize as ::core::primitive::u8 => { + #[allow(clippy::redundant_closure_call)] + return (move || { + ::core::result::Result::Ok(FungiblesError::NoPermission) + })(); + } + #[allow(clippy::unnecessary_cast)] + __codec_x_edqy if __codec_x_edqy == 6usize as ::core::primitive::u8 => { + #[allow(clippy::redundant_closure_call)] + return (move || { + ::core::result::Result::Ok(FungiblesError::Unknown) + })(); + } + #[allow(clippy::unnecessary_cast)] + __codec_x_edqy if __codec_x_edqy == 7usize as ::core::primitive::u8 => { + #[allow(clippy::redundant_closure_call)] + return (move || { + ::core::result::Result::Ok(FungiblesError::ZeroRecipientAddress) + })(); + } + #[allow(clippy::unnecessary_cast)] + __codec_x_edqy if __codec_x_edqy == 8usize as ::core::primitive::u8 => { + #[allow(clippy::redundant_closure_call)] + return (move || { + ::core::result::Result::Ok(FungiblesError::ZeroSenderAddress) + })(); + } + _ => { + #[allow(clippy::redundant_closure_call)] + return (move || { + ::core::result::Result::Err( + <_ as ::core::convert::Into< + _, + >>::into( + "Could not decode `FungiblesError`, variant doesn't exist", + ), + ) + })(); + } + } + } + } +}; +impl From for FungiblesError { + fn from(error: Error) -> Self { + match error { + Error::InUse => FungiblesError::InUse, + Error::MinBalanceZero => FungiblesError::MinBalanceZero, + Error::Unknown => FungiblesError::Unknown, + _ => ::core::panicking::panic("not yet implemented"), + } + } +} +/// The fungibles result type. +pub type Result = core::result::Result; +mod fungibles { + impl ::ink::env::ContractEnv for Fungibles { + type Env = pop_api::Environment; + } + type Environment = ::Env; + type AccountId = <::Env as ::ink::env::Environment>::AccountId; + type Balance = <::Env as ::ink::env::Environment>::Balance; + type Hash = <::Env as ::ink::env::Environment>::Hash; + type Timestamp = <::Env as ::ink::env::Environment>::Timestamp; + type BlockNumber = <::Env as ::ink::env::Environment>::BlockNumber; + type ChainExtension = <::Env as ::ink::env::Environment>::ChainExtension; + const MAX_EVENT_TOPICS: usize = <::Env as ::ink::env::Environment>::MAX_EVENT_TOPICS; + const _: () = { + struct Check { + salt: (), + } + }; + #[scale_info(crate = ::ink::scale_info)] + #[cfg(not(feature = "__ink_dylint_Storage"))] + pub struct Fungibles {} + const _: () = { + impl< + __ink_generic_salt: ::ink::storage::traits::StorageKey, + > ::ink::storage::traits::StorableHint<__ink_generic_salt> for Fungibles { + type Type = Fungibles; + type PreferredKey = ::ink::storage::traits::AutoKey; + } + }; + const _: () = { + impl ::ink::storage::traits::StorageKey for Fungibles { + const KEY: ::ink::primitives::Key = <() as ::ink::storage::traits::StorageKey>::KEY; + } + }; + const _: () = { + impl ::ink::storage::traits::Storable for Fungibles { + #[inline(always)] + #[allow(non_camel_case_types)] + fn decode<__ink_I: ::ink::scale::Input>( + __input: &mut __ink_I, + ) -> ::core::result::Result { + ::core::result::Result::Ok(Fungibles {}) + } + #[inline(always)] + #[allow(non_camel_case_types)] + fn encode<__ink_O: ::ink::scale::Output + ?::core::marker::Sized>( + &self, + __dest: &mut __ink_O, + ) { + match self { + Fungibles {} => {} + } + } + #[inline(always)] + #[allow(non_camel_case_types)] + fn encoded_size(&self) -> ::core::primitive::usize { + match self { + Fungibles {} => ::core::primitive::usize::MIN, + } + } + } + }; + #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] + const _: () = { + impl ::ink::scale_info::TypeInfo for Fungibles { + type Identity = Self; + fn type_info() -> ::ink::scale_info::Type { + ::ink::scale_info::Type::builder() + .path( + ::ink::scale_info::Path::new_with_replace( + "Fungibles", + "fungibles::fungibles", + &[], + ), + ) + .type_params(::alloc::vec::Vec::new()) + .composite(::ink::scale_info::build::Fields::named()) + } + } + }; + const _: () = { + impl ::ink::storage::traits::StorageLayout for Fungibles { + fn layout( + __key: &::ink::primitives::Key, + ) -> ::ink::metadata::layout::Layout { + ::ink::metadata::layout::Layout::Struct( + ::ink::metadata::layout::StructLayout::new("Fungibles", []), + ) + } + } + }; + #[automatically_derived] + impl ::core::default::Default for Fungibles { + #[inline] + fn default() -> Fungibles { + Fungibles {} + } + } + const _: () = { + impl ::ink::reflect::ContractName for Fungibles { + const NAME: &'static str = "Fungibles"; + } + }; + const _: () = { + impl<'a> ::ink::codegen::Env for &'a Fungibles { + type EnvAccess = ::ink::EnvAccess< + 'a, + ::Env, + >; + fn env(self) -> Self::EnvAccess { + <::EnvAccess as ::core::default::Default>::default() + } + } + impl<'a> ::ink::codegen::StaticEnv for Fungibles { + type EnvAccess = ::ink::EnvAccess< + 'static, + ::Env, + >; + fn env() -> Self::EnvAccess { + <::EnvAccess as ::core::default::Default>::default() + } + } + }; + const _: () = { + #[allow(unused_imports)] + use ::ink::codegen::{Env as _, StaticEnv as _}; + }; + impl ::ink::reflect::DispatchableConstructorInfo<0x9BAE9D5E_u32> for Fungibles { + type Input = (); + type Output = Self; + type Storage = Fungibles; + type Error = <::ink::reflect::ConstructorOutputValue< + Self, + > as ::ink::reflect::ConstructorOutput>::Error; + const IS_RESULT: ::core::primitive::bool = <::ink::reflect::ConstructorOutputValue< + Self, + > as ::ink::reflect::ConstructorOutput>::IS_RESULT; + const CALLABLE: fn(Self::Input) -> Self::Output = |_| { Fungibles::new() }; + const PAYABLE: ::core::primitive::bool = true; + const SELECTOR: [::core::primitive::u8; 4usize] = [ + 0x9B_u8, + 0xAE_u8, + 0x9D_u8, + 0x5E_u8, + ]; + const LABEL: &'static ::core::primitive::str = "new"; + } + impl ::ink::reflect::DispatchableMessageInfo<0xDB6375A8_u32> for Fungibles { + type Input = AssetId; + type Output = Result; + type Storage = Fungibles; + const CALLABLE: fn(&mut Self::Storage, Self::Input) -> Self::Output = | + storage, + __ink_binding_0| + { Fungibles::total_supply(storage, __ink_binding_0) }; + const SELECTOR: [::core::primitive::u8; 4usize] = [ + 0xDB_u8, + 0x63_u8, + 0x75_u8, + 0xA8_u8, + ]; + const PAYABLE: ::core::primitive::bool = false; + const MUTATES: ::core::primitive::bool = false; + const LABEL: &'static ::core::primitive::str = "total_supply"; + } + impl ::ink::reflect::DispatchableMessageInfo<0x0F755A56_u32> for Fungibles { + type Input = (AssetId, AccountId32); + type Output = Result; + type Storage = Fungibles; + const CALLABLE: fn(&mut Self::Storage, Self::Input) -> Self::Output = | + storage, + (__ink_binding_0, __ink_binding_1)| + { Fungibles::balance_of(storage, __ink_binding_0, __ink_binding_1) }; + const SELECTOR: [::core::primitive::u8; 4usize] = [ + 0x0F_u8, + 0x75_u8, + 0x5A_u8, + 0x56_u8, + ]; + const PAYABLE: ::core::primitive::bool = false; + const MUTATES: ::core::primitive::bool = false; + const LABEL: &'static ::core::primitive::str = "balance_of"; + } + impl ::ink::reflect::DispatchableMessageInfo<0x6A00165E_u32> for Fungibles { + type Input = (AssetId, AccountId32, AccountId32); + type Output = Result; + type Storage = Fungibles; + const CALLABLE: fn(&mut Self::Storage, Self::Input) -> Self::Output = | + storage, + (__ink_binding_0, __ink_binding_1, __ink_binding_2)| + { + Fungibles::allowance( + storage, + __ink_binding_0, + __ink_binding_1, + __ink_binding_2, + ) + }; + const SELECTOR: [::core::primitive::u8; 4usize] = [ + 0x6A_u8, + 0x00_u8, + 0x16_u8, + 0x5E_u8, + ]; + const PAYABLE: ::core::primitive::bool = false; + const MUTATES: ::core::primitive::bool = false; + const LABEL: &'static ::core::primitive::str = "allowance"; + } + impl ::ink::reflect::DispatchableMessageInfo<0xAA6B65DB_u32> for Fungibles { + type Input = AssetId; + type Output = Result; + type Storage = Fungibles; + const CALLABLE: fn(&mut Self::Storage, Self::Input) -> Self::Output = | + storage, + __ink_binding_0| + { Fungibles::asset_exists(storage, __ink_binding_0) }; + const SELECTOR: [::core::primitive::u8; 4usize] = [ + 0xAA_u8, + 0x6B_u8, + 0x65_u8, + 0xDB_u8, + ]; + const PAYABLE: ::core::primitive::bool = false; + const MUTATES: ::core::primitive::bool = false; + const LABEL: &'static ::core::primitive::str = "asset_exists"; + } + impl ::ink::reflect::DispatchableMessageInfo<0x1F8E8E22_u32> for Fungibles { + type Input = (u32, AccountId32, Balance); + type Output = Result<()>; + type Storage = Fungibles; + const CALLABLE: fn(&mut Self::Storage, Self::Input) -> Self::Output = | + storage, + (__ink_binding_0, __ink_binding_1, __ink_binding_2)| + { + Fungibles::mint_asset( + storage, + __ink_binding_0, + __ink_binding_1, + __ink_binding_2, + ) + }; + const SELECTOR: [::core::primitive::u8; 4usize] = [ + 0x1F_u8, + 0x8E_u8, + 0x8E_u8, + 0x22_u8, + ]; + const PAYABLE: ::core::primitive::bool = false; + const MUTATES: ::core::primitive::bool = false; + const LABEL: &'static ::core::primitive::str = "mint_asset"; + } + const _: () = { + #[allow(non_camel_case_types)] + pub enum __ink_ConstructorDecoder { + Constructor0( + >::Input, + ), + } + impl ::ink::reflect::DecodeDispatch for __ink_ConstructorDecoder { + fn decode_dispatch( + input: &mut I, + ) -> ::core::result::Result + where + I: ::ink::scale::Input, + { + const CONSTRUCTOR_0: [::core::primitive::u8; 4usize] = >::SELECTOR; + match <[::core::primitive::u8; 4usize] as ::ink::scale::Decode>::decode( + input, + ) + .map_err(|_| ::ink::reflect::DispatchError::InvalidSelector)? + { + CONSTRUCTOR_0 => { + ::core::result::Result::Ok( + Self::Constructor0( + <>::Input as ::ink::scale::Decode>::decode(input) + .map_err(|_| { + ::ink::reflect::DispatchError::InvalidParameters + })?, + ), + ) + } + _invalid => { + ::core::result::Result::Err( + ::ink::reflect::DispatchError::UnknownSelector, + ) + } + } + } + } + impl ::ink::scale::Decode for __ink_ConstructorDecoder { + fn decode( + input: &mut I, + ) -> ::core::result::Result + where + I: ::ink::scale::Input, + { + ::decode_dispatch(input) + .map_err(::core::convert::Into::into) + } + } + impl ::ink::reflect::ExecuteDispatchable for __ink_ConstructorDecoder { + #[allow(clippy::nonminimal_bool)] + fn execute_dispatchable( + self, + ) -> ::core::result::Result<(), ::ink::reflect::DispatchError> { + match self { + Self::Constructor0(input) => { + if { + false + || { + let constructor_0 = false; + let constructor_0 = >::PAYABLE; + constructor_0 + } + } + && !>::PAYABLE + { + ::ink::codegen::deny_payment::< + ::Env, + >()?; + } + let result: >::Output = >::CALLABLE(input); + let output_value = ::ink::reflect::ConstructorOutputValue::new( + result, + ); + let output_result = <::ink::reflect::ConstructorOutputValue< + >::Output, + > as ::ink::reflect::ConstructorOutput< + Fungibles, + >>::as_result(&output_value); + if let ::core::result::Result::Ok(contract) = output_result + .as_ref() + { + ::ink::env::set_contract_storage::< + ::ink::primitives::Key, + Fungibles, + >( + &::KEY, + contract, + ); + } + let mut flag = ::ink::env::ReturnFlags::empty(); + if output_result.is_err() { + flag = ::ink::env::ReturnFlags::REVERT; + } + ::ink::env::return_value::< + ::ink::ConstructorResult< + ::core::result::Result< + (), + &<::ink::reflect::ConstructorOutputValue< + >::Output, + > as ::ink::reflect::ConstructorOutput>::Error, + >, + >, + >( + flag, + &::ink::ConstructorResult::Ok(output_result.map(|_| ())), + ); + } + } + } + } + impl ::ink::reflect::ContractConstructorDecoder for Fungibles { + type Type = __ink_ConstructorDecoder; + } + }; + const _: () = { + #[allow(non_camel_case_types)] + pub enum __ink_MessageDecoder { + Message0( + >::Input, + ), + Message1( + >::Input, + ), + Message2( + >::Input, + ), + Message3( + >::Input, + ), + Message4( + >::Input, + ), + } + impl ::ink::reflect::DecodeDispatch for __ink_MessageDecoder { + fn decode_dispatch( + input: &mut I, + ) -> ::core::result::Result + where + I: ::ink::scale::Input, + { + const MESSAGE_0: [::core::primitive::u8; 4usize] = >::SELECTOR; + const MESSAGE_1: [::core::primitive::u8; 4usize] = >::SELECTOR; + const MESSAGE_2: [::core::primitive::u8; 4usize] = >::SELECTOR; + const MESSAGE_3: [::core::primitive::u8; 4usize] = >::SELECTOR; + const MESSAGE_4: [::core::primitive::u8; 4usize] = >::SELECTOR; + match <[::core::primitive::u8; 4usize] as ::ink::scale::Decode>::decode( + input, + ) + .map_err(|_| ::ink::reflect::DispatchError::InvalidSelector)? + { + MESSAGE_0 => { + ::core::result::Result::Ok( + Self::Message0( + <>::Input as ::ink::scale::Decode>::decode(input) + .map_err(|_| { + ::ink::reflect::DispatchError::InvalidParameters + })?, + ), + ) + } + MESSAGE_1 => { + ::core::result::Result::Ok( + Self::Message1( + <>::Input as ::ink::scale::Decode>::decode(input) + .map_err(|_| { + ::ink::reflect::DispatchError::InvalidParameters + })?, + ), + ) + } + MESSAGE_2 => { + ::core::result::Result::Ok( + Self::Message2( + <>::Input as ::ink::scale::Decode>::decode(input) + .map_err(|_| { + ::ink::reflect::DispatchError::InvalidParameters + })?, + ), + ) + } + MESSAGE_3 => { + ::core::result::Result::Ok( + Self::Message3( + <>::Input as ::ink::scale::Decode>::decode(input) + .map_err(|_| { + ::ink::reflect::DispatchError::InvalidParameters + })?, + ), + ) + } + MESSAGE_4 => { + ::core::result::Result::Ok( + Self::Message4( + <>::Input as ::ink::scale::Decode>::decode(input) + .map_err(|_| { + ::ink::reflect::DispatchError::InvalidParameters + })?, + ), + ) + } + _invalid => { + ::core::result::Result::Err( + ::ink::reflect::DispatchError::UnknownSelector, + ) + } + } + } + } + impl ::ink::scale::Decode for __ink_MessageDecoder { + fn decode( + input: &mut I, + ) -> ::core::result::Result + where + I: ::ink::scale::Input, + { + ::decode_dispatch(input) + .map_err(::core::convert::Into::into) + } + } + fn push_contract(contract: ::core::mem::ManuallyDrop, mutates: bool) { + if mutates { + ::ink::env::set_contract_storage::< + ::ink::primitives::Key, + Fungibles, + >(&::KEY, &contract); + } + } + impl ::ink::reflect::ExecuteDispatchable for __ink_MessageDecoder { + #[allow(clippy::nonminimal_bool, clippy::let_unit_value)] + fn execute_dispatchable( + self, + ) -> ::core::result::Result<(), ::ink::reflect::DispatchError> { + let key = ::KEY; + let mut contract: ::core::mem::ManuallyDrop = ::core::mem::ManuallyDrop::new( + match ::ink::env::get_contract_storage(&key) { + ::core::result::Result::Ok( + ::core::option::Option::Some(value), + ) => value, + ::core::result::Result::Ok(::core::option::Option::None) => { + ::core::panicking::panic_fmt( + format_args!("storage entry was empty"), + ); + } + ::core::result::Result::Err(_) => { + ::core::panicking::panic_fmt( + format_args!("could not properly decode storage entry"), + ); + } + }, + ); + match self { + Self::Message0(input) => { + if { + false + || { + let message_0 = false; + let message_0 = >::PAYABLE; + message_0 + } + || { + let message_1 = false; + let message_1 = >::PAYABLE; + message_1 + } + || { + let message_2 = false; + let message_2 = >::PAYABLE; + message_2 + } + || { + let message_3 = false; + let message_3 = >::PAYABLE; + message_3 + } + || { + let message_4 = false; + let message_4 = >::PAYABLE; + message_4 + } + } + && !>::PAYABLE + { + ::ink::codegen::deny_payment::< + ::Env, + >()?; + } + let result: >::Output = >::CALLABLE(&mut contract, input); + let is_reverted = { + #[allow(unused_imports)] + use ::ink::result_info::IsResultTypeFallback as _; + ::ink::result_info::IsResultType::< + >::Output, + >::VALUE + } + && { + #[allow(unused_imports)] + use ::ink::result_info::IsResultErrFallback as _; + ::ink::result_info::IsResultErr(&result).value() + }; + let mut flag = ::ink::env::ReturnFlags::REVERT; + if !is_reverted { + flag = ::ink::env::ReturnFlags::empty(); + push_contract( + contract, + >::MUTATES, + ); + } + ::ink::env::return_value::< + ::ink::MessageResult< + >::Output, + >, + >(flag, &::ink::MessageResult::Ok(result)) + } + Self::Message1(input) => { + if { + false + || { + let message_0 = false; + let message_0 = >::PAYABLE; + message_0 + } + || { + let message_1 = false; + let message_1 = >::PAYABLE; + message_1 + } + || { + let message_2 = false; + let message_2 = >::PAYABLE; + message_2 + } + || { + let message_3 = false; + let message_3 = >::PAYABLE; + message_3 + } + || { + let message_4 = false; + let message_4 = >::PAYABLE; + message_4 + } + } + && !>::PAYABLE + { + ::ink::codegen::deny_payment::< + ::Env, + >()?; + } + let result: >::Output = >::CALLABLE(&mut contract, input); + let is_reverted = { + #[allow(unused_imports)] + use ::ink::result_info::IsResultTypeFallback as _; + ::ink::result_info::IsResultType::< + >::Output, + >::VALUE + } + && { + #[allow(unused_imports)] + use ::ink::result_info::IsResultErrFallback as _; + ::ink::result_info::IsResultErr(&result).value() + }; + let mut flag = ::ink::env::ReturnFlags::REVERT; + if !is_reverted { + flag = ::ink::env::ReturnFlags::empty(); + push_contract( + contract, + >::MUTATES, + ); + } + ::ink::env::return_value::< + ::ink::MessageResult< + >::Output, + >, + >(flag, &::ink::MessageResult::Ok(result)) + } + Self::Message2(input) => { + if { + false + || { + let message_0 = false; + let message_0 = >::PAYABLE; + message_0 + } + || { + let message_1 = false; + let message_1 = >::PAYABLE; + message_1 + } + || { + let message_2 = false; + let message_2 = >::PAYABLE; + message_2 + } + || { + let message_3 = false; + let message_3 = >::PAYABLE; + message_3 + } + || { + let message_4 = false; + let message_4 = >::PAYABLE; + message_4 + } + } + && !>::PAYABLE + { + ::ink::codegen::deny_payment::< + ::Env, + >()?; + } + let result: >::Output = >::CALLABLE(&mut contract, input); + let is_reverted = { + #[allow(unused_imports)] + use ::ink::result_info::IsResultTypeFallback as _; + ::ink::result_info::IsResultType::< + >::Output, + >::VALUE + } + && { + #[allow(unused_imports)] + use ::ink::result_info::IsResultErrFallback as _; + ::ink::result_info::IsResultErr(&result).value() + }; + let mut flag = ::ink::env::ReturnFlags::REVERT; + if !is_reverted { + flag = ::ink::env::ReturnFlags::empty(); + push_contract( + contract, + >::MUTATES, + ); + } + ::ink::env::return_value::< + ::ink::MessageResult< + >::Output, + >, + >(flag, &::ink::MessageResult::Ok(result)) + } + Self::Message3(input) => { + if { + false + || { + let message_0 = false; + let message_0 = >::PAYABLE; + message_0 + } + || { + let message_1 = false; + let message_1 = >::PAYABLE; + message_1 + } + || { + let message_2 = false; + let message_2 = >::PAYABLE; + message_2 + } + || { + let message_3 = false; + let message_3 = >::PAYABLE; + message_3 + } + || { + let message_4 = false; + let message_4 = >::PAYABLE; + message_4 + } + } + && !>::PAYABLE + { + ::ink::codegen::deny_payment::< + ::Env, + >()?; + } + let result: >::Output = >::CALLABLE(&mut contract, input); + let is_reverted = { + #[allow(unused_imports)] + use ::ink::result_info::IsResultTypeFallback as _; + ::ink::result_info::IsResultType::< + >::Output, + >::VALUE + } + && { + #[allow(unused_imports)] + use ::ink::result_info::IsResultErrFallback as _; + ::ink::result_info::IsResultErr(&result).value() + }; + let mut flag = ::ink::env::ReturnFlags::REVERT; + if !is_reverted { + flag = ::ink::env::ReturnFlags::empty(); + push_contract( + contract, + >::MUTATES, + ); + } + ::ink::env::return_value::< + ::ink::MessageResult< + >::Output, + >, + >(flag, &::ink::MessageResult::Ok(result)) + } + Self::Message4(input) => { + if { + false + || { + let message_0 = false; + let message_0 = >::PAYABLE; + message_0 + } + || { + let message_1 = false; + let message_1 = >::PAYABLE; + message_1 + } + || { + let message_2 = false; + let message_2 = >::PAYABLE; + message_2 + } + || { + let message_3 = false; + let message_3 = >::PAYABLE; + message_3 + } + || { + let message_4 = false; + let message_4 = >::PAYABLE; + message_4 + } + } + && !>::PAYABLE + { + ::ink::codegen::deny_payment::< + ::Env, + >()?; + } + let result: >::Output = >::CALLABLE(&mut contract, input); + let is_reverted = { + #[allow(unused_imports)] + use ::ink::result_info::IsResultTypeFallback as _; + ::ink::result_info::IsResultType::< + >::Output, + >::VALUE + } + && { + #[allow(unused_imports)] + use ::ink::result_info::IsResultErrFallback as _; + ::ink::result_info::IsResultErr(&result).value() + }; + let mut flag = ::ink::env::ReturnFlags::REVERT; + if !is_reverted { + flag = ::ink::env::ReturnFlags::empty(); + push_contract( + contract, + >::MUTATES, + ); + } + ::ink::env::return_value::< + ::ink::MessageResult< + >::Output, + >, + >(flag, &::ink::MessageResult::Ok(result)) + } + }; + } + } + impl ::ink::reflect::ContractMessageDecoder for Fungibles { + type Type = __ink_MessageDecoder; + } + }; + const _: () = { + use ::ink::codegen::{Env as _, StaticEnv as _}; + const _: ::ink::codegen::utils::IsSameType = ::ink::codegen::utils::IsSameType::< + Fungibles, + >::new(); + impl Fungibles { + #[cfg(not(feature = "__ink_dylint_Constructor"))] + pub fn new() -> Self { + ::ink_env::debug_message( + &{ + let res = ::alloc::fmt::format( + format_args!( + "{0}\n", + { + let res = ::alloc::fmt::format( + format_args!("PopApiAssetsExample::new"), + ); + res + }, + ), + ); + res + }, + ); + Default::default() + } + pub fn total_supply(&self, id: AssetId) -> Result { + total_supply(id).map_err(From::from) + } + pub fn balance_of( + &self, + id: AssetId, + owner: AccountId32, + ) -> Result { + balance_of(id, owner).map_err(From::from) + } + pub fn allowance( + &self, + id: AssetId, + owner: AccountId32, + spender: AccountId32, + ) -> Result { + allowance(id, owner, spender).map_err(From::from) + } + pub fn asset_exists(&self, id: AssetId) -> Result { + asset_exists(id).map_err(From::from) + } + pub fn mint_asset( + &self, + id: u32, + beneficiary: AccountId32, + amount: Balance, + ) -> Result<()> { + ::ink_env::debug_message( + &{ + let res = ::alloc::fmt::format( + format_args!( + "{0}\n", + { + let res = ::alloc::fmt::format( + format_args!( + "PopApiAssetsExample::mint_asset_through_runtime: id: {0:?} beneficiary: {1:?} amount: {2:?}", + id, + beneficiary, + amount, + ), + ); + res + }, + ), + ); + res + }, + ); + let result = mint(id, beneficiary, amount)?; + ::ink_env::debug_message( + &{ + let res = ::alloc::fmt::format( + format_args!( + "{0}\n", + { + let res = ::alloc::fmt::format( + format_args!("Result: {0:?}", result), + ); + res + }, + ), + ); + res + }, + ); + Ok(()) + } + } + const _: () = { + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchInput, + >(); + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchOutput>, + >(); + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchInput, + >(); + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchInput, + >(); + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchOutput>, + >(); + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchInput, + >(); + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchInput, + >(); + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchInput, + >(); + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchOutput>, + >(); + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchInput, + >(); + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchOutput>, + >(); + ::ink::codegen::utils::consume_type::<::ink::codegen::DispatchInput>(); + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchInput, + >(); + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchInput, + >(); + ::ink::codegen::utils::consume_type::< + ::ink::codegen::DispatchOutput>, + >(); + }; + }; + const _: () = { + #[codec(crate = ::ink::scale)] + #[scale_info(crate = ::ink::scale_info)] + /// The ink! smart contract's call builder. + /// + /// Implements the underlying on-chain calling of the ink! smart contract + /// messages and trait implementations in a type safe way. + #[repr(transparent)] + pub struct CallBuilder { + account_id: AccountId, + } + #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] + const _: () = { + impl ::ink::scale_info::TypeInfo for CallBuilder { + type Identity = Self; + fn type_info() -> ::ink::scale_info::Type { + ::ink::scale_info::Type::builder() + .path( + ::ink::scale_info::Path::new_with_replace( + "CallBuilder", + "fungibles::fungibles", + &[], + ), + ) + .type_params(::alloc::vec::Vec::new()) + .docs( + &[ + "The ink! smart contract's call builder.", + "", + "Implements the underlying on-chain calling of the ink! smart contract", + "messages and trait implementations in a type safe way.", + ], + ) + .composite( + ::ink::scale_info::build::Fields::named() + .field(|f| { + f + .ty::() + .name("account_id") + .type_name("AccountId") + }), + ) + } + } + }; + #[allow(deprecated)] + const _: () = { + #[automatically_derived] + impl ::ink::scale::Decode for CallBuilder { + fn decode<__CodecInputEdqy: ::ink::scale::Input>( + __codec_input_edqy: &mut __CodecInputEdqy, + ) -> ::core::result::Result { + ::core::result::Result::Ok(CallBuilder { + account_id: { + let __codec_res_edqy = ::decode( + __codec_input_edqy, + ); + match __codec_res_edqy { + ::core::result::Result::Err(e) => { + return ::core::result::Result::Err( + e.chain("Could not decode `CallBuilder::account_id`"), + ); + } + ::core::result::Result::Ok(__codec_res_edqy) => { + __codec_res_edqy + } + } + }, + }) + } + fn decode_into<__CodecInputEdqy: ::ink::scale::Input>( + __codec_input_edqy: &mut __CodecInputEdqy, + dst_: &mut ::core::mem::MaybeUninit, + ) -> ::core::result::Result< + ::ink::scale::DecodeFinished, + ::ink::scale::Error, + > { + match ( + &::core::mem::size_of::(), + &::core::mem::size_of::(), + ) { + (left_val, right_val) => { + if !(*left_val == *right_val) { + let kind = ::core::panicking::AssertKind::Eq; + ::core::panicking::assert_failed( + kind, + &*left_val, + &*right_val, + ::core::option::Option::None, + ); + } + } + }; + if !(if ::core::mem::size_of::() > 0 { 1 } else { 0 } + <= 1) + { + ::core::panicking::panic( + "assertion failed: if ::core::mem::size_of::() > 0 { 1 } else { 0 } <= 1", + ) + } + { + let dst_: &mut ::core::mem::MaybeUninit = dst_; + let dst_: &mut ::core::mem::MaybeUninit = unsafe { + &mut *dst_ + .as_mut_ptr() + .cast::<::core::mem::MaybeUninit>() + }; + ::decode_into( + __codec_input_edqy, + dst_, + )?; + } + unsafe { + ::core::result::Result::Ok( + ::ink::scale::DecodeFinished::assert_decoding_finished(), + ) + } + } + } + }; + #[allow(deprecated)] + const _: () = { + #[automatically_derived] + impl ::ink::scale::Encode for CallBuilder { + fn size_hint(&self) -> usize { + ::ink::scale::Encode::size_hint(&&self.account_id) + } + fn encode_to< + __CodecOutputEdqy: ::ink::scale::Output + ?::core::marker::Sized, + >(&self, __codec_dest_edqy: &mut __CodecOutputEdqy) { + ::ink::scale::Encode::encode_to(&&self.account_id, __codec_dest_edqy) + } + fn encode( + &self, + ) -> ::ink::scale::alloc::vec::Vec<::core::primitive::u8> { + ::ink::scale::Encode::encode(&&self.account_id) + } + fn using_encoded< + __CodecOutputReturn, + __CodecUsingEncodedCallback: ::core::ops::FnOnce( + &[::core::primitive::u8], + ) -> __CodecOutputReturn, + >(&self, f: __CodecUsingEncodedCallback) -> __CodecOutputReturn { + ::ink::scale::Encode::using_encoded(&&self.account_id, f) + } + } + #[automatically_derived] + impl ::ink::scale::EncodeLike for CallBuilder {} + }; + #[automatically_derived] + impl ::core::fmt::Debug for CallBuilder { + #[inline] + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + ::core::fmt::Formatter::debug_struct_field1_finish( + f, + "CallBuilder", + "account_id", + &&self.account_id, + ) + } + } + #[automatically_derived] + impl ::core::hash::Hash for CallBuilder { + #[inline] + fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { + ::core::hash::Hash::hash(&self.account_id, state) + } + } + #[automatically_derived] + impl ::core::marker::StructuralPartialEq for CallBuilder {} + #[automatically_derived] + impl ::core::cmp::PartialEq for CallBuilder { + #[inline] + fn eq(&self, other: &CallBuilder) -> bool { + self.account_id == other.account_id + } + } + #[automatically_derived] + impl ::core::cmp::Eq for CallBuilder { + #[inline] + #[doc(hidden)] + #[coverage(off)] + fn assert_receiver_is_total_eq(&self) -> () { + let _: ::core::cmp::AssertParamIsEq; + } + } + #[automatically_derived] + impl ::core::clone::Clone for CallBuilder { + #[inline] + fn clone(&self) -> CallBuilder { + CallBuilder { + account_id: ::core::clone::Clone::clone(&self.account_id), + } + } + } + const _: () = { + impl ::ink::storage::traits::StorageLayout for CallBuilder { + fn layout( + __key: &::ink::primitives::Key, + ) -> ::ink::metadata::layout::Layout { + ::ink::metadata::layout::Layout::Struct( + ::ink::metadata::layout::StructLayout::new( + "CallBuilder", + [ + ::ink::metadata::layout::FieldLayout::new( + "account_id", + ::layout( + __key, + ), + ), + ], + ), + ) + } + } + }; + const _: () = { + impl ::ink::codegen::ContractCallBuilder for Fungibles { + type Type = CallBuilder; + } + impl ::ink::env::ContractEnv for CallBuilder { + type Env = ::Env; + } + }; + impl ::ink::env::call::FromAccountId for CallBuilder { + #[inline] + fn from_account_id(account_id: AccountId) -> Self { + Self { account_id } + } + } + impl ::ink::ToAccountId for CallBuilder { + #[inline] + fn to_account_id(&self) -> AccountId { + ::clone(&self.account_id) + } + } + impl ::core::convert::AsRef for CallBuilder { + fn as_ref(&self) -> &AccountId { + &self.account_id + } + } + impl ::core::convert::AsMut for CallBuilder { + fn as_mut(&mut self) -> &mut AccountId { + &mut self.account_id + } + } + impl CallBuilder { + #[allow(clippy::type_complexity)] + #[inline] + pub fn total_supply( + &self, + __ink_binding_0: AssetId, + ) -> ::ink::env::call::CallBuilder< + Environment, + ::ink::env::call::utils::Set<::ink::env::call::Call>, + ::ink::env::call::utils::Set< + ::ink::env::call::ExecutionInput< + ::ink::env::call::utils::ArgumentList< + ::ink::env::call::utils::Argument, + ::ink::env::call::utils::EmptyArgumentList, + >, + >, + >, + ::ink::env::call::utils::Set< + ::ink::env::call::utils::ReturnType>, + >, + > { + ::ink::env::call::build_call::() + .call(::ink::ToAccountId::to_account_id(self)) + .exec_input( + ::ink::env::call::ExecutionInput::new( + ::ink::env::call::Selector::new([ + 0xDB_u8, + 0x63_u8, + 0x75_u8, + 0xA8_u8, + ]), + ) + .push_arg(__ink_binding_0), + ) + .returns::>() + } + #[allow(clippy::type_complexity)] + #[inline] + pub fn balance_of( + &self, + __ink_binding_0: AssetId, + __ink_binding_1: AccountId32, + ) -> ::ink::env::call::CallBuilder< + Environment, + ::ink::env::call::utils::Set<::ink::env::call::Call>, + ::ink::env::call::utils::Set< + ::ink::env::call::ExecutionInput< + ::ink::env::call::utils::ArgumentList< + ::ink::env::call::utils::Argument, + ::ink::env::call::utils::ArgumentList< + ::ink::env::call::utils::Argument, + ::ink::env::call::utils::EmptyArgumentList, + >, + >, + >, + >, + ::ink::env::call::utils::Set< + ::ink::env::call::utils::ReturnType>, + >, + > { + ::ink::env::call::build_call::() + .call(::ink::ToAccountId::to_account_id(self)) + .exec_input( + ::ink::env::call::ExecutionInput::new( + ::ink::env::call::Selector::new([ + 0x0F_u8, + 0x75_u8, + 0x5A_u8, + 0x56_u8, + ]), + ) + .push_arg(__ink_binding_0) + .push_arg(__ink_binding_1), + ) + .returns::>() + } + #[allow(clippy::type_complexity)] + #[inline] + pub fn allowance( + &self, + __ink_binding_0: AssetId, + __ink_binding_1: AccountId32, + __ink_binding_2: AccountId32, + ) -> ::ink::env::call::CallBuilder< + Environment, + ::ink::env::call::utils::Set<::ink::env::call::Call>, + ::ink::env::call::utils::Set< + ::ink::env::call::ExecutionInput< + ::ink::env::call::utils::ArgumentList< + ::ink::env::call::utils::Argument, + ::ink::env::call::utils::ArgumentList< + ::ink::env::call::utils::Argument, + ::ink::env::call::utils::ArgumentList< + ::ink::env::call::utils::Argument, + ::ink::env::call::utils::EmptyArgumentList, + >, + >, + >, + >, + >, + ::ink::env::call::utils::Set< + ::ink::env::call::utils::ReturnType>, + >, + > { + ::ink::env::call::build_call::() + .call(::ink::ToAccountId::to_account_id(self)) + .exec_input( + ::ink::env::call::ExecutionInput::new( + ::ink::env::call::Selector::new([ + 0x6A_u8, + 0x00_u8, + 0x16_u8, + 0x5E_u8, + ]), + ) + .push_arg(__ink_binding_0) + .push_arg(__ink_binding_1) + .push_arg(__ink_binding_2), + ) + .returns::>() + } + #[allow(clippy::type_complexity)] + #[inline] + pub fn asset_exists( + &self, + __ink_binding_0: AssetId, + ) -> ::ink::env::call::CallBuilder< + Environment, + ::ink::env::call::utils::Set<::ink::env::call::Call>, + ::ink::env::call::utils::Set< + ::ink::env::call::ExecutionInput< + ::ink::env::call::utils::ArgumentList< + ::ink::env::call::utils::Argument, + ::ink::env::call::utils::EmptyArgumentList, + >, + >, + >, + ::ink::env::call::utils::Set< + ::ink::env::call::utils::ReturnType>, + >, + > { + ::ink::env::call::build_call::() + .call(::ink::ToAccountId::to_account_id(self)) + .exec_input( + ::ink::env::call::ExecutionInput::new( + ::ink::env::call::Selector::new([ + 0xAA_u8, + 0x6B_u8, + 0x65_u8, + 0xDB_u8, + ]), + ) + .push_arg(__ink_binding_0), + ) + .returns::>() + } + #[allow(clippy::type_complexity)] + #[inline] + pub fn mint_asset( + &self, + __ink_binding_0: u32, + __ink_binding_1: AccountId32, + __ink_binding_2: Balance, + ) -> ::ink::env::call::CallBuilder< + Environment, + ::ink::env::call::utils::Set<::ink::env::call::Call>, + ::ink::env::call::utils::Set< + ::ink::env::call::ExecutionInput< + ::ink::env::call::utils::ArgumentList< + ::ink::env::call::utils::Argument, + ::ink::env::call::utils::ArgumentList< + ::ink::env::call::utils::Argument, + ::ink::env::call::utils::ArgumentList< + ::ink::env::call::utils::Argument, + ::ink::env::call::utils::EmptyArgumentList, + >, + >, + >, + >, + >, + ::ink::env::call::utils::Set< + ::ink::env::call::utils::ReturnType>, + >, + > { + ::ink::env::call::build_call::() + .call(::ink::ToAccountId::to_account_id(self)) + .exec_input( + ::ink::env::call::ExecutionInput::new( + ::ink::env::call::Selector::new([ + 0x1F_u8, + 0x8E_u8, + 0x8E_u8, + 0x22_u8, + ]), + ) + .push_arg(__ink_binding_0) + .push_arg(__ink_binding_1) + .push_arg(__ink_binding_2), + ) + .returns::>() + } + } + }; + #[codec(crate = ::ink::scale)] + #[scale_info(crate = ::ink::scale_info)] + pub struct FungiblesRef { + inner: ::Type, + } + #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] + const _: () = { + impl ::ink::scale_info::TypeInfo for FungiblesRef { + type Identity = Self; + fn type_info() -> ::ink::scale_info::Type { + ::ink::scale_info::Type::builder() + .path( + ::ink::scale_info::Path::new_with_replace( + "FungiblesRef", + "fungibles::fungibles", + &[], + ), + ) + .type_params(::alloc::vec::Vec::new()) + .composite( + ::ink::scale_info::build::Fields::named() + .field(|f| { + f + .ty::< + ::Type, + >() + .name("inner") + .type_name( + "::Type", + ) + }), + ) + } + } + }; + #[allow(deprecated)] + const _: () = { + #[automatically_derived] + impl ::ink::scale::Decode for FungiblesRef { + fn decode<__CodecInputEdqy: ::ink::scale::Input>( + __codec_input_edqy: &mut __CodecInputEdqy, + ) -> ::core::result::Result { + ::core::result::Result::Ok(FungiblesRef { + inner: { + let __codec_res_edqy = <::Type as ::ink::scale::Decode>::decode( + __codec_input_edqy, + ); + match __codec_res_edqy { + ::core::result::Result::Err(e) => { + return ::core::result::Result::Err( + e.chain("Could not decode `FungiblesRef::inner`"), + ); + } + ::core::result::Result::Ok(__codec_res_edqy) => { + __codec_res_edqy + } + } + }, + }) + } + } + }; + #[allow(deprecated)] + const _: () = { + #[automatically_derived] + impl ::ink::scale::Encode for FungiblesRef { + fn size_hint(&self) -> usize { + ::ink::scale::Encode::size_hint(&&self.inner) + } + fn encode_to< + __CodecOutputEdqy: ::ink::scale::Output + ?::core::marker::Sized, + >(&self, __codec_dest_edqy: &mut __CodecOutputEdqy) { + ::ink::scale::Encode::encode_to(&&self.inner, __codec_dest_edqy) + } + fn encode(&self) -> ::ink::scale::alloc::vec::Vec<::core::primitive::u8> { + ::ink::scale::Encode::encode(&&self.inner) + } + fn using_encoded< + __CodecOutputReturn, + __CodecUsingEncodedCallback: ::core::ops::FnOnce( + &[::core::primitive::u8], + ) -> __CodecOutputReturn, + >(&self, f: __CodecUsingEncodedCallback) -> __CodecOutputReturn { + ::ink::scale::Encode::using_encoded(&&self.inner, f) + } + } + #[automatically_derived] + impl ::ink::scale::EncodeLike for FungiblesRef {} + }; + #[automatically_derived] + impl ::core::fmt::Debug for FungiblesRef { + #[inline] + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + ::core::fmt::Formatter::debug_struct_field1_finish( + f, + "FungiblesRef", + "inner", + &&self.inner, + ) + } + } + #[automatically_derived] + impl ::core::hash::Hash for FungiblesRef { + #[inline] + fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { + ::core::hash::Hash::hash(&self.inner, state) + } + } + #[automatically_derived] + impl ::core::marker::StructuralPartialEq for FungiblesRef {} + #[automatically_derived] + impl ::core::cmp::PartialEq for FungiblesRef { + #[inline] + fn eq(&self, other: &FungiblesRef) -> bool { + self.inner == other.inner + } + } + #[automatically_derived] + impl ::core::cmp::Eq for FungiblesRef { + #[inline] + #[doc(hidden)] + #[coverage(off)] + fn assert_receiver_is_total_eq(&self) -> () { + let _: ::core::cmp::AssertParamIsEq< + ::Type, + >; + } + } + #[automatically_derived] + impl ::core::clone::Clone for FungiblesRef { + #[inline] + fn clone(&self) -> FungiblesRef { + FungiblesRef { + inner: ::core::clone::Clone::clone(&self.inner), + } + } + } + const _: () = { + impl ::ink::storage::traits::StorageLayout for FungiblesRef { + fn layout( + __key: &::ink::primitives::Key, + ) -> ::ink::metadata::layout::Layout { + ::ink::metadata::layout::Layout::Struct( + ::ink::metadata::layout::StructLayout::new( + "FungiblesRef", + [ + ::ink::metadata::layout::FieldLayout::new( + "inner", + <::Type as ::ink::storage::traits::StorageLayout>::layout( + __key, + ), + ), + ], + ), + ) + } + } + }; + const _: () = { + impl ::ink::env::ContractReference for Fungibles { + type Type = FungiblesRef; + } + impl ::ink::env::call::ConstructorReturnType for Fungibles { + type Output = FungiblesRef; + type Error = (); + fn ok(value: FungiblesRef) -> Self::Output { + value + } + } + impl ::ink::env::call::ConstructorReturnType + for ::core::result::Result + where + E: ::ink::scale::Decode, + { + const IS_RESULT: bool = true; + type Output = ::core::result::Result; + type Error = E; + fn ok(value: FungiblesRef) -> Self::Output { + ::core::result::Result::Ok(value) + } + fn err(err: Self::Error) -> ::core::option::Option { + ::core::option::Option::Some(::core::result::Result::Err(err)) + } + } + impl ::ink::env::ContractEnv for FungiblesRef { + type Env = ::Env; + } + }; + impl FungiblesRef { + #[inline] + #[allow(clippy::type_complexity)] + pub fn new() -> ::ink::env::call::CreateBuilder< + Environment, + Self, + ::ink::env::call::utils::Unset, + ::ink::env::call::utils::Set< + ::ink::env::call::LimitParamsV2< + ::Env, + >, + >, + ::ink::env::call::utils::Unset, + ::ink::env::call::utils::Set< + ::ink::env::call::ExecutionInput< + ::ink::env::call::utils::EmptyArgumentList, + >, + >, + ::ink::env::call::utils::Unset<::ink::env::call::state::Salt>, + ::ink::env::call::utils::Set<::ink::env::call::utils::ReturnType>, + > { + ::ink::env::call::build_create::() + .exec_input( + ::ink::env::call::ExecutionInput::new( + ::ink::env::call::Selector::new([ + 0x9B_u8, + 0xAE_u8, + 0x9D_u8, + 0x5E_u8, + ]), + ), + ) + .returns::() + } + #[inline] + pub fn total_supply(&self, id: AssetId) -> Result { + self.try_total_supply(id) + .unwrap_or_else(|error| { + ::core::panicking::panic_fmt( + format_args!( + "encountered error while calling {0}::{1}: {2:?}", + "Fungibles", + "total_supply", + error, + ), + ); + }) + } + #[inline] + pub fn try_total_supply( + &self, + id: AssetId, + ) -> ::ink::MessageResult> { + ::call(self) + .total_supply(id) + .try_invoke() + .unwrap_or_else(|error| { + ::core::panicking::panic_fmt( + format_args!( + "encountered error while calling {0}::{1}: {2:?}", + "Fungibles", + "total_supply", + error, + ), + ); + }) + } + #[inline] + pub fn balance_of(&self, id: AssetId, owner: AccountId32) -> Result { + self.try_balance_of(id, owner) + .unwrap_or_else(|error| { + ::core::panicking::panic_fmt( + format_args!( + "encountered error while calling {0}::{1}: {2:?}", + "Fungibles", + "balance_of", + error, + ), + ); + }) + } + #[inline] + pub fn try_balance_of( + &self, + id: AssetId, + owner: AccountId32, + ) -> ::ink::MessageResult> { + ::call(self) + .balance_of(id, owner) + .try_invoke() + .unwrap_or_else(|error| { + ::core::panicking::panic_fmt( + format_args!( + "encountered error while calling {0}::{1}: {2:?}", + "Fungibles", + "balance_of", + error, + ), + ); + }) + } + #[inline] + pub fn allowance( + &self, + id: AssetId, + owner: AccountId32, + spender: AccountId32, + ) -> Result { + self.try_allowance(id, owner, spender) + .unwrap_or_else(|error| { + ::core::panicking::panic_fmt( + format_args!( + "encountered error while calling {0}::{1}: {2:?}", + "Fungibles", + "allowance", + error, + ), + ); + }) + } + #[inline] + pub fn try_allowance( + &self, + id: AssetId, + owner: AccountId32, + spender: AccountId32, + ) -> ::ink::MessageResult> { + ::call(self) + .allowance(id, owner, spender) + .try_invoke() + .unwrap_or_else(|error| { + ::core::panicking::panic_fmt( + format_args!( + "encountered error while calling {0}::{1}: {2:?}", + "Fungibles", + "allowance", + error, + ), + ); + }) + } + #[inline] + pub fn asset_exists(&self, id: AssetId) -> Result { + self.try_asset_exists(id) + .unwrap_or_else(|error| { + ::core::panicking::panic_fmt( + format_args!( + "encountered error while calling {0}::{1}: {2:?}", + "Fungibles", + "asset_exists", + error, + ), + ); + }) + } + #[inline] + pub fn try_asset_exists( + &self, + id: AssetId, + ) -> ::ink::MessageResult> { + ::call(self) + .asset_exists(id) + .try_invoke() + .unwrap_or_else(|error| { + ::core::panicking::panic_fmt( + format_args!( + "encountered error while calling {0}::{1}: {2:?}", + "Fungibles", + "asset_exists", + error, + ), + ); + }) + } + #[inline] + pub fn mint_asset( + &self, + id: u32, + beneficiary: AccountId32, + amount: Balance, + ) -> Result<()> { + self.try_mint_asset(id, beneficiary, amount) + .unwrap_or_else(|error| { + ::core::panicking::panic_fmt( + format_args!( + "encountered error while calling {0}::{1}: {2:?}", + "Fungibles", + "mint_asset", + error, + ), + ); + }) + } + #[inline] + pub fn try_mint_asset( + &self, + id: u32, + beneficiary: AccountId32, + amount: Balance, + ) -> ::ink::MessageResult> { + ::call(self) + .mint_asset(id, beneficiary, amount) + .try_invoke() + .unwrap_or_else(|error| { + ::core::panicking::panic_fmt( + format_args!( + "encountered error while calling {0}::{1}: {2:?}", + "Fungibles", + "mint_asset", + error, + ), + ); + }) + } + } + const _: () = { + impl ::ink::codegen::TraitCallBuilder for FungiblesRef { + type Builder = ::Type; + #[inline] + fn call(&self) -> &Self::Builder { + &self.inner + } + #[inline] + fn call_mut(&mut self) -> &mut Self::Builder { + &mut self.inner + } + } + }; + impl ::ink::env::call::FromAccountId for FungiblesRef { + #[inline] + fn from_account_id(account_id: AccountId) -> Self { + Self { + inner: <::Type as ::ink::env::call::FromAccountId< + Environment, + >>::from_account_id(account_id), + } + } + } + impl ::ink::ToAccountId for FungiblesRef { + #[inline] + fn to_account_id(&self) -> AccountId { + <::Type as ::ink::ToAccountId< + Environment, + >>::to_account_id(&self.inner) + } + } + impl ::core::convert::AsRef for FungiblesRef { + fn as_ref(&self) -> &AccountId { + <_ as ::core::convert::AsRef>::as_ref(&self.inner) + } + } + impl ::core::convert::AsMut for FungiblesRef { + fn as_mut(&mut self) -> &mut AccountId { + <_ as ::core::convert::AsMut>::as_mut(&mut self.inner) + } + } + #[cfg(feature = "std")] + #[cfg(not(feature = "ink-as-dependency"))] + const _: () = { + #[no_mangle] + pub fn __ink_generate_metadata() -> ::ink::metadata::InkProject { + let layout = ::ink::metadata::layout::Layout::Root( + ::ink::metadata::layout::RootLayout::new( + <::ink::metadata::layout::LayoutKey as ::core::convert::From< + ::ink::primitives::Key, + >>::from(::KEY), + ::layout( + &::KEY, + ), + ::ink::scale_info::meta_type::(), + ), + ); + ::ink::metadata::layout::ValidateLayout::validate(&layout) + .unwrap_or_else(|error| { + { + ::core::panicking::panic_fmt( + format_args!("metadata ink! generation failed: {0}", error), + ); + } + }); + ::ink::metadata::InkProject::new( + layout, + ::ink::metadata::ContractSpec::new() + .constructors([ + ::ink::metadata::ConstructorSpec::from_label("new") + .selector([0x9B_u8, 0xAE_u8, 0x9D_u8, 0x5E_u8]) + .args([]) + .payable(true) + .default(false) + .returns( + ::ink::metadata::ReturnTypeSpec::new( + if >::IS_RESULT { + ::ink::metadata::TypeSpec::with_name_str::< + ::ink::ConstructorResult< + ::core::result::Result< + (), + >::Error, + >, + >, + >("ink_primitives::ConstructorResult") + } else { + ::ink::metadata::TypeSpec::with_name_str::< + ::ink::ConstructorResult<()>, + >("ink_primitives::ConstructorResult") + }, + ), + ) + .docs([]) + .done(), + ]) + .messages([ + ::ink::metadata::MessageSpec::from_label("total_supply") + .selector([0xDB_u8, 0x63_u8, 0x75_u8, 0xA8_u8]) + .args([ + ::ink::metadata::MessageParamSpec::new("id") + .of_type( + ::ink::metadata::TypeSpec::with_name_segs::< + AssetId, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["AssetId"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .done(), + ]) + .returns( + ::ink::metadata::ReturnTypeSpec::new( + ::ink::metadata::TypeSpec::with_name_segs::< + ::ink::MessageResult>, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter([ + "ink", + "MessageResult", + ]), + ::core::convert::AsRef::as_ref, + ), + ), + ), + ) + .mutates(false) + .payable(false) + .default(false) + .docs([]) + .done(), + ::ink::metadata::MessageSpec::from_label("balance_of") + .selector([0x0F_u8, 0x75_u8, 0x5A_u8, 0x56_u8]) + .args([ + ::ink::metadata::MessageParamSpec::new("id") + .of_type( + ::ink::metadata::TypeSpec::with_name_segs::< + AssetId, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["AssetId"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .done(), + ::ink::metadata::MessageParamSpec::new("owner") + .of_type( + ::ink::metadata::TypeSpec::with_name_segs::< + AccountId32, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["AccountId32"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .done(), + ]) + .returns( + ::ink::metadata::ReturnTypeSpec::new( + ::ink::metadata::TypeSpec::with_name_segs::< + ::ink::MessageResult>, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter([ + "ink", + "MessageResult", + ]), + ::core::convert::AsRef::as_ref, + ), + ), + ), + ) + .mutates(false) + .payable(false) + .default(false) + .docs([]) + .done(), + ::ink::metadata::MessageSpec::from_label("allowance") + .selector([0x6A_u8, 0x00_u8, 0x16_u8, 0x5E_u8]) + .args([ + ::ink::metadata::MessageParamSpec::new("id") + .of_type( + ::ink::metadata::TypeSpec::with_name_segs::< + AssetId, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["AssetId"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .done(), + ::ink::metadata::MessageParamSpec::new("owner") + .of_type( + ::ink::metadata::TypeSpec::with_name_segs::< + AccountId32, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["AccountId32"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .done(), + ::ink::metadata::MessageParamSpec::new("spender") + .of_type( + ::ink::metadata::TypeSpec::with_name_segs::< + AccountId32, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["AccountId32"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .done(), + ]) + .returns( + ::ink::metadata::ReturnTypeSpec::new( + ::ink::metadata::TypeSpec::with_name_segs::< + ::ink::MessageResult>, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter([ + "ink", + "MessageResult", + ]), + ::core::convert::AsRef::as_ref, + ), + ), + ), + ) + .mutates(false) + .payable(false) + .default(false) + .docs([]) + .done(), + ::ink::metadata::MessageSpec::from_label("asset_exists") + .selector([0xAA_u8, 0x6B_u8, 0x65_u8, 0xDB_u8]) + .args([ + ::ink::metadata::MessageParamSpec::new("id") + .of_type( + ::ink::metadata::TypeSpec::with_name_segs::< + AssetId, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["AssetId"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .done(), + ]) + .returns( + ::ink::metadata::ReturnTypeSpec::new( + ::ink::metadata::TypeSpec::with_name_segs::< + ::ink::MessageResult>, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter([ + "ink", + "MessageResult", + ]), + ::core::convert::AsRef::as_ref, + ), + ), + ), + ) + .mutates(false) + .payable(false) + .default(false) + .docs([]) + .done(), + ::ink::metadata::MessageSpec::from_label("mint_asset") + .selector([0x1F_u8, 0x8E_u8, 0x8E_u8, 0x22_u8]) + .args([ + ::ink::metadata::MessageParamSpec::new("id") + .of_type( + ::ink::metadata::TypeSpec::with_name_segs::< + u32, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["u32"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .done(), + ::ink::metadata::MessageParamSpec::new("beneficiary") + .of_type( + ::ink::metadata::TypeSpec::with_name_segs::< + AccountId32, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["AccountId32"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .done(), + ::ink::metadata::MessageParamSpec::new("amount") + .of_type( + ::ink::metadata::TypeSpec::with_name_segs::< + Balance, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["Balance"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .done(), + ]) + .returns( + ::ink::metadata::ReturnTypeSpec::new( + ::ink::metadata::TypeSpec::with_name_segs::< + ::ink::MessageResult>, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter([ + "ink", + "MessageResult", + ]), + ::core::convert::AsRef::as_ref, + ), + ), + ), + ) + .mutates(false) + .payable(false) + .default(false) + .docs([]) + .done(), + ]) + .collect_events() + .docs([]) + .lang_error( + ::ink::metadata::TypeSpec::with_name_segs::< + ::ink::LangError, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["ink", "LangError"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .environment( + ::ink::metadata::EnvironmentSpec::new() + .account_id( + ::ink::metadata::TypeSpec::with_name_segs::< + AccountId, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["AccountId"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .balance( + ::ink::metadata::TypeSpec::with_name_segs::< + Balance, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["Balance"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .hash( + ::ink::metadata::TypeSpec::with_name_segs::< + Hash, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["Hash"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .timestamp( + ::ink::metadata::TypeSpec::with_name_segs::< + Timestamp, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["Timestamp"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .block_number( + ::ink::metadata::TypeSpec::with_name_segs::< + BlockNumber, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["BlockNumber"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .chain_extension( + ::ink::metadata::TypeSpec::with_name_segs::< + ChainExtension, + _, + >( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["ChainExtension"]), + ::core::convert::AsRef::as_ref, + ), + ), + ) + .max_event_topics(MAX_EVENT_TOPICS) + .static_buffer_size(::ink::env::BUFFER_SIZE) + .done(), + ) + .done(), + ) + } + }; + use super::*; +} diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs new file mode 100755 index 00000000..9c0ae754 --- /dev/null +++ b/pop-api/examples/fungibles/lib.rs @@ -0,0 +1,176 @@ +#![cfg_attr(not(feature = "std"), no_std, no_main)] + +// Fungibles wrapper contract to allow contracts to interact with local fungibles without the pop api. +use ink::prelude::vec::Vec; +use pop_api::{ + assets::fungibles::*, + primitives::{AccountId as AccountId32, AssetId}, +}; + +#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum ContractError { + // AssetsError(Error), + // /// The origin of the call doesn't have the right permission. + // BadOrigin, + // /// Custom error type for cases in which an implementation adds its own restrictions. + // Custom(String), + /// Not enough balance to fulfill a request is available. + InsufficientBalance, + /// Not enough allowance to fulfill a request is available. + InsufficientAllowance, + /// The asset status is not the expected status. + IncorrectStatus, + /// The asset ID is already taken. + InUse, + /// Minimum balance should be non-zero. + MinBalanceZero, + /// The signing account has no permission to do the operation. + NoPermission, + // /// Safe transfer check fails (e.g. if the receiving contract does not accept tokens). + // SafeTransferCheckFailed(String), + /// The given asset ID is unknown. + Unknown, + /// Recipient's address is zero. + ZeroRecipientAddress, + /// Sender's address is zero. + ZeroSenderAddress, + UndefinedError, +} + +impl From for ContractError { + fn from(error: FungiblesError) -> Self { + match error { + // Error::BalanceLow => Err(InsufficientBalance), + FungiblesError::InUse => ContractError::InUse, + FungiblesError::MinBalanceZero => ContractError::MinBalanceZero, + FungiblesError::Unknown => ContractError::Unknown, + _ => ContractError::UndefinedError, + } + } +} + +/// The fungibles result type. +pub type Result = core::result::Result; + +#[ink::contract(env = pop_api::Environment)] +mod fungibles { + use super::*; + + #[ink(storage)] + #[derive(Default)] + pub struct Fungibles; + + impl Fungibles { + #[ink(constructor, payable)] + pub fn new() -> Self { + ink::env::debug_println!("PopApiAssetsExample::new"); + Default::default() + } + + #[ink(message)] + pub fn total_supply(&self, id: AssetId) -> Result { + total_supply(id).map_err(From::from) + } + + #[ink(message)] + pub fn balance_of(&self, id: AssetId, owner: AccountId32) -> Result { + balance_of(id, owner).map_err(From::from) + } + + #[ink(message)] + pub fn allowance( + &self, + id: AssetId, + owner: AccountId32, + spender: AccountId32, + ) -> Result { + allowance(id, owner, spender).map_err(From::from) + } + + #[ink(message)] + pub fn asset_exists(&self, id: AssetId) -> Result { + asset_exists(id).map_err(From::from) + } + + #[ink(message)] + pub fn create(&self, id: AssetId, admin: AccountId32, min_balance: Balance) -> Result<()> { + // create(id, admin, min_balance).map_err(From::from) + ink::env::debug_println!( + "PopApiAssetsExample::create: id: {:?} admin: {:?} min_balance: {:?}", + id, + admin, + min_balance, + ); + let result = create(id, admin, min_balance); + ink::env::debug_println!("Result: {:?}", result); + result.map_err(From::from) + } + + #[ink(message)] + pub fn set_metadata( + &self, + id: AssetId, + name: Vec, + symbol: Vec, + decimals: u8, + ) -> Result<()> { + // set_metadata(id, name, symbol, decimals).map_err(From::from) + ink::env::debug_println!( + "PopApiAssetsExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}", + id, + name, + symbol, + decimals, + ); + let result = set_metadata(id, name, symbol, decimals); + ink::env::debug_println!("Result: {:?}", result); + result.map_err(From::from) + } + + #[ink(message)] + pub fn mint(&self, id: AssetId, beneficiary: AccountId32, amount: Balance) -> Result<()> { + ink::env::debug_println!( + "PopApiAssetsExample::mint: id: {:?}, beneficiary: {:?} amount: {:?}", + id, + beneficiary, + amount, + ); + + let result = mint(id, beneficiary, amount); + ink::env::debug_println!("Result: {:?}", result); + result.map_err(From::from) + } + + // #[ink(message)] + // pub fn transfer_from( + // id: AssetId, + // from: Option, + // to: Option, + // value: Balance, + // data: [u8], + // ) -> Result<()> { + // ink::env::debug_println!( + // "PopApiAssetsExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}", + // id, + // from, + // to, + // value, + // ); + // + // let result = transfer_from(id, from, to, value)?; + // ink::env::debug_println!("Result: {:?}", result); + // result + // } + } + + #[cfg(test)] + mod tests { + use super::*; + + #[ink::test] + fn default_works() { + PopApiAssetsExample::new(); + } + } +} diff --git a/pop-api/examples/trust_backed_assets/lib.rs b/pop-api/examples/trust_backed_assets/lib.rs deleted file mode 100755 index 3606f852..00000000 --- a/pop-api/examples/trust_backed_assets/lib.rs +++ /dev/null @@ -1,74 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std, no_main)] - -// Utilizing Trust Backed Assets with the Pop API. -// -// This example demonstrates interaction with trust backed assets via the assets pallet. Trust backed assets are originated -// and managed within Pop Network, harnessing the platform's inherent trust, security, and governance models. -use pop_api::assets::trust_backed as trust_backed_assets; - -#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum ContractError { - TrustBackedAssetsError(trust_backed_assets::Error), - UnknownAsset, -} - -impl From for ContractError { - fn from(value: trust_backed_assets::Error) -> Self { - ContractError::TrustBackedAssetsError(value) - } -} - -#[ink::contract(env = pop_api::Environment)] -mod pop_api_tb_assets_example { - use super::*; - - #[ink(storage)] - #[derive(Default)] - pub struct PopApiTBAssetsExample; - - impl PopApiTBAssetsExample { - #[ink(constructor, payable)] - pub fn new() -> Self { - ink::env::debug_println!("Contract::new"); - Default::default() - } - - #[ink(message)] - pub fn mint_asset_through_runtime( - &mut self, - id: u32, - beneficiary: AccountId, - amount: Balance, - ) -> Result<(), ContractError> { - ink::env::debug_println!( - "Contract::mint_asset_through_runtime: id: {:?} beneficiary: {:?} amount: {:?}", - id, - beneficiary, - amount - ); - - // Check if asset doesn't exist. - if !trust_backed_assets::asset_exists(id)? { - return Err(ContractError::UnknownAsset); - } - - // Mint asset via pop api. - trust_backed_assets::mint(id, beneficiary, amount)?; - ink::env::debug_println!( - "Contract::mint_asset_through_runtime: asset(s) minted successfully" - ); - Ok(()) - } - } - - #[cfg(test)] - mod tests { - use super::*; - - #[ink::test] - fn default_works() { - PopApiTBAssetsExample::new(); - } - } -} diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 68ce6906..0ca4a817 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -3,14 +3,15 @@ pub mod primitives; pub mod v0; -use crate::PopApiError::{Balances, Nfts, TrustBackedAssets, UnknownStatusCode}; +use crate::PopApiError::{Assets, Balances, Contracts, Nfts, UnknownStatusCode}; use ink::{prelude::vec::Vec, ChainExtensionInstance}; -use primitives::{cross_chain::*, storage_keys::*}; +use primitives::{cross_chain::*, storage_keys::*, AccountId as AccountId32}; pub use sp_runtime::{BoundedVec, MultiAddress, MultiSignature}; use v0::RuntimeCall; -pub use v0::{balances, cross_chain, nfts, relay_chain_block_number, state, assets}; +pub use v0::{assets, balances, contracts, cross_chain, nfts, relay_chain_block_number, state}; -type AccountId = ::AccountId; +// type AccountId = ::AccountId; +type AccountId = AccountId32; type Balance = ::Balance; type BlockNumber = ::BlockNumber; type StringLimit = u32; @@ -25,8 +26,9 @@ pub enum PopApiError { DecodingFailed, SystemCallFiltered, Balances(balances::Error), + Contracts(contracts::Error), Nfts(nfts::Error), - TrustBackedAssets(assets::trust_backed::Error), + Assets(assets::fungibles::AssetsError), Xcm(cross_chain::Error), } @@ -37,8 +39,9 @@ impl ink::env::chain_extension::FromStatusCode for PopApiError { // CallFiltered originates from `frame_system` with pallet-index 0. The CallFiltered error is at index 5 5 => Err(PopApiError::SystemCallFiltered), 10_000..=10_999 => Err(Balances((status_code - 10_000).try_into()?)), + 40_000..=40_999 => Err(Contracts((status_code - 40_000).try_into()?)), 50_000..=50_999 => Err(Nfts((status_code - 50_000).try_into()?)), - 52_000..=52_999 => Err(TrustBackedAssets((status_code - 52_000).try_into()?)), + 52_000..=52_999 => Err(Assets((status_code - 52_000).try_into()?)), _ => Err(UnknownStatusCode(status_code)), } } diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs new file mode 100644 index 00000000..f2d87a0e --- /dev/null +++ b/pop-api/src/v0/assets/fungibles.rs @@ -0,0 +1,554 @@ +use crate::{AccountId, Balance, PopApiError::UnknownStatusCode, RuntimeCall, *}; +use ink::prelude::vec::Vec; +use primitives::AssetId; +use scale::{Compact, Encode}; + +type Result = core::result::Result; + +/// Local Fungibles: +/// 1. PSP-22 Interface +/// 2. PSP-22 Metadata Interface +/// 3. Asset Management + +/// 1. PSP-22 Interface: +/// - total_supply +/// - balance_of +/// - allowance +/// - transfer +/// - transfer_from +/// - approve +/// - increase_allowance +/// - decrease_allowance + +/// Returns the total token supply for a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// The total supply of the token, or an error if the operation fails. +pub fn total_supply(id: AssetId) -> Result { + Ok(state::read(RuntimeStateKeys::Assets(AssetsKeys::TotalSupply(id)))?) +} + +/// Returns the account balance for the specified `owner` for a given asset ID. Returns `0` if +/// the account is non-existent. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `owner` - The account whose balance is being queried. +/// +/// # Returns +/// The balance of the specified account, or an error if the operation fails. +pub fn balance_of(id: AssetId, owner: AccountId) -> Result { + Ok(state::read(RuntimeStateKeys::Assets(AssetsKeys::BalanceOf(id, owner)))?) +} + +/// Returns the amount which `spender` is still allowed to withdraw from `owner` for a given +/// asset ID. Returns `0` if no allowance has been set. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `owner` - The account that owns the tokens. +/// * `spender` - The account that is allowed to spend the tokens. +/// +/// # Returns +/// The remaining allowance, or an error if the operation fails. +pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { + Ok(state::read(RuntimeStateKeys::Assets(AssetsKeys::Allowance(id, owner, spender)))?) +} + +/// Create a new token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `admin` - The account that will administer the asset. +/// * `min_balance` - The minimum balance required for accounts holding this asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the creation fails. +// pub fn create(id: AssetId, admin: impl Into>, min_balance: Balance) -> Result<()> { +pub fn create( + id: AssetId, + admin: impl Into>, + min_balance: Balance, +) -> Result<()> { + Ok(dispatch(RuntimeCall::Assets(AssetsCall::Create { + id: id.into(), + admin: admin.into(), + min_balance, + }))?) +} + +/// Transfers `value` amount of tokens from the caller's account to account `to`, with additional +/// `data` in unspecified format. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `to` - The recipient account. +/// * `value` - The number of tokens to transfer. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the transfer fails. +// #[allow(unused_variables)] +// pub fn transfer( +// id: AssetId, +// to: impl Into>, +// value: Balance, +// ) -> Result<()> { +// todo!() +// // TODO: transfer or transfer_keep_alive +// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::Transfer { +// // id: id.into(), +// // target: target.into(), +// // amount: Compact(amount), +// // }))?) +// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { +// // id: id.into(), +// // target: target.into(), +// // amount: Compact(amount), +// // }))?) +// } + +/// Transfers `value` tokens on the behalf of `from` to the account `to` with additional `data` +/// in unspecified format. This can be used to allow a contract to transfer tokens on ones behalf +/// and/or to charge fees in sub-currencies, for example. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `from` - The account from which the tokens are transferred. +/// * `to` - The recipient account. +/// * `value` - The number of tokens to transfer. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the transfer fails. +// pub fn transfer_from( +// id: AssetId, +// from: impl Into>, +// to: impl Into>, +// value: Balance, +// ) -> Result<()> { +//todo!() +// TODO: depending on `from` and `to`, decide whether to mint, burn or transfer_approved. +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::Mint { +// id: id.into(), +// beneficiary: beneficiary.into(), +// amount: Compact(amount), +// }))?) +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::Burn { +// id: id.into(), +// who: who.into(), +// amount: Compact(amount), +// }))?) +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::TransferApproved { +// id: id.into(), +// owner: from.into(), +// destination: to.into(), +// amount: Compact(value), +// }))?) +// } + +/// Mint assets of a particular class. +pub fn mint( + id: AssetId, + beneficiary: impl Into>, + amount: Balance, +) -> Result<()> { + Ok(dispatch(RuntimeCall::Assets(AssetsCall::Mint { + id: id.into(), + beneficiary: beneficiary.into(), + amount: Compact(amount), + }))?) +} + +/// Approves an account to spend a specified number of tokens on behalf of the caller. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `spender` - The account that is allowed to spend the tokens. +/// * `value` - The number of tokens to approve. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the approval fails. +// #[allow(unused_variables)] +// fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { +// todo!() +// // TODO: read allowance and increase or decrease. +// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { +// // id: id.into(), +// // delegate: spender.into(), +// // amount: Compact(value), +// // }))?) +// } + +/// Increases the allowance of a spender. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `spender` - The account that is allowed to spend the tokens. +/// * `value` - The number of tokens to increase the allowance by. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { +// id: id.into(), +// delegate: spender.into(), +// amount: Compact(value), +// }))?) +// } + +/// Decreases the allowance of a spender. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `spender` - The account that is allowed to spend the tokens. +/// * `value` - The number of tokens to decrease the allowance by. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// #[allow(unused_variables)] +// fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { +// todo!() +// // TODO: cancel_approval + approve_transfer +// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::CancelApproval { +// // id: id.into(), +// // delegate: delegate.into(), +// // }))?) +// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { +// // id: id.into(), +// // delegate: spender.into(), +// // amount: Compact(value), +// // }))?) +// } + +/// 2. PSP-22 Metadata Interface: +/// - token_name +/// - token_symbol +/// - token_decimals + +/// Returns the token name for a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// The name of the token as a byte vector, or an error if the operation fails. +// #[allow(unused_variables)] +// pub fn token_name(id: AssetId) -> Result>> { +// todo!() +// // Ok(state::read(RuntimeStateKeys::Assets(AssetsKeys::TokenName(id)))?) +// } + +/// Returns the token symbol for a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// The symbol of the token as a byte vector, or an error if the operation fails. +// #[allow(unused_variables)] +// fn token_symbol(id: AssetId) -> Result>> { +// todo!() +// } + +/// Returns the token decimals for a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// The number of decimals of the token as a byte vector, or an error if the operation fails. +// #[allow(unused_variables)] +// fn token_decimals(id: AssetId) -> Result>> { +// todo!() +// } + +/// 3. Asset Management: +/// - create +/// - start_destroy +/// - destroy_accounts +/// - destroy_approvals +/// - finish_destroy +/// - set_metadata +/// - clear_metadata + +/// Start the process of destroying a token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// fn start_destroy(id: AssetId) -> Result<()> { +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::StartDestroy { +// id: id.into(), +// }))?) +// } + +/// Destroy all accounts associated with a token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// fn destroy_accounts(id: AssetId) -> Result<()> { +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::DestroyAccounts { +// id: id.into(), +// }))?) +// } + +/// Destroy all approvals associated with a token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// fn destroy_approvals(id: AssetId) -> Result<()> { +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::DestroyApprovals { +// id: id.into(), +// }))?) +// } + +/// Complete the process of destroying a token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// fn finish_destroy(id: AssetId) -> Result<()> { +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::FinishDestroy { +// id: id.into(), +// }))?) +// } + +/// Set the metadata for a token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) -> Result<()> { + Ok(dispatch(RuntimeCall::Assets(AssetsCall::SetMetadata { + id: id.into(), + name, + symbol, + decimals, + }))?) +} + +/// Clear the metadata for a token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// fn clear_metadata(id: AssetId) -> Result<()> { +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::ClearMetadata { +// id: id.into(), +// }))?) +// } + +pub fn asset_exists(id: AssetId) -> Result { + Ok(state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id)))?) +} + +// Parameters to extrinsics representing an asset id (`AssetIdParameter`) and a balance amount (`Balance`) are expected +// to be compact encoded. The pop api handles that for the developer. +// +// reference: https://substrate.stackexchange.com/questions/1873/what-is-the-meaning-of-palletcompact-in-pallet-development +// +// Asset id that is compact encoded. +type AssetIdParameter = Compact; +// Balance amount that is compact encoded. +type BalanceParameter = Compact; + +#[allow(warnings, unused)] +#[derive(Encode)] +pub(crate) enum AssetsCall { + #[codec(index = 0)] + Create { id: AssetIdParameter, admin: MultiAddress, min_balance: Balance }, + #[codec(index = 2)] + StartDestroy { id: AssetIdParameter }, + #[codec(index = 3)] + DestroyAccounts { id: AssetIdParameter }, + #[codec(index = 4)] + DestroyApprovals { id: AssetIdParameter }, + #[codec(index = 5)] + FinishDestroy { id: AssetIdParameter }, + #[codec(index = 6)] + Mint { + id: AssetIdParameter, + beneficiary: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 7)] + Burn { id: AssetIdParameter, who: MultiAddress, amount: BalanceParameter }, + // TODO: ED or not + // #[codec(index = 8)] + // Transfer { id: AssetIdParameter, target: MultiAddress, amount: BalanceParameter }, + #[codec(index = 9)] + TransferKeepAlive { + id: AssetIdParameter, + target: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 17)] + SetMetadata { id: AssetIdParameter, name: Vec, symbol: Vec, decimals: u8 }, + #[codec(index = 18)] + ClearMetadata { id: AssetIdParameter }, + #[codec(index = 22)] + ApproveTransfer { + id: AssetIdParameter, + delegate: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 23)] + CancelApproval { id: AssetIdParameter, delegate: MultiAddress }, + #[codec(index = 25)] + TransferApproved { + id: AssetIdParameter, + owner: MultiAddress, + destination: MultiAddress, + amount: BalanceParameter, + }, +} + +// TODO: remove unnecessary errors +#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub(crate) enum AssetsError { + /// Account balance must be greater than or equal to the transfer amount. + BalanceLow, + /// The account to alter does not exist. + NoAccount, + /// The signing account has no permission to do the operation. + NoPermission, + /// The given asset ID is unknown. + Unknown, + /// The origin account is frozen. + Frozen, + /// The asset ID is already taken. + InUse, + /// Invalid witness data given. + BadWitness, + /// Minimum balance should be non-zero. + MinBalanceZero, + /// Unable to increment the consumer reference counters on the account. Either no provider + /// reference exists to allow a non-zero balance of a non-self-sufficient asset, or one + /// fewer then the maximum number of consumers has been reached. + UnavailableConsumer, + /// Invalid metadata given. + BadMetadata, + /// No approval exists that would allow the transfer. + Unapproved, + /// The source account would not survive the transfer and it needs to stay alive. + WouldDie, + /// The asset-account already exists. + AlreadyExists, + /// The asset-account doesn't have an associated deposit. + NoDeposit, + /// The operation would result in funds being burned. + WouldBurn, + /// The asset is a live asset and is actively being used. Usually emit for operations such + /// as `start_destroy` which require the asset to be in a destroying state. + LiveAsset, + /// The asset is not live, and likely being destroyed. + AssetNotLive, + /// The asset status is not the expected status. + IncorrectStatus, + /// The asset should be frozen before the given operation. + NotFrozen, + /// Callback action resulted in error + CallbackFailed, +} + +impl From for AssetsError { + fn from(error: PopApiError) -> Self { + match error { + PopApiError::Assets(e) => e, + _ => panic!("Expected AssetsError"), + } + } +} + +impl TryFrom for AssetsError { + type Error = PopApiError; + + fn try_from(status_code: u32) -> core::result::Result { + use AssetsError::*; + match status_code { + 0 => Ok(BalanceLow), + 1 => Ok(NoAccount), + 2 => Ok(NoPermission), + 3 => Ok(Unknown), + 4 => Ok(Frozen), + 5 => Ok(InUse), + 6 => Ok(BadWitness), + 7 => Ok(MinBalanceZero), + 8 => Ok(UnavailableConsumer), + 9 => Ok(BadMetadata), + 10 => Ok(Unapproved), + 11 => Ok(WouldDie), + 12 => Ok(AlreadyExists), + 13 => Ok(NoDeposit), + 14 => Ok(WouldBurn), + 15 => Ok(LiveAsset), + 16 => Ok(AssetNotLive), + 17 => Ok(IncorrectStatus), + 18 => Ok(NotFrozen), + _ => Err(UnknownStatusCode(status_code)), + } + } +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum FungiblesError { + /// The signing account has no permission to do the operation. + NoPermission, + /// The given asset ID is unknown. + Unknown, + InsufficientBalance, + /// The asset ID is already taken. + InUse, + /// Minimum balance should be non-zero. + MinBalanceZero, +} + +impl From for FungiblesError { + fn from(error: balances::Error) -> Self { + match error { + balances::Error::InsufficientBalance => FungiblesError::InsufficientBalance, + _ => panic!("Unexpected pallet assets error. This error is unknown to pallet assets"), + } + } +} + +impl From for FungiblesError { + fn from(error: AssetsError) -> Self { + match error { + AssetsError::InUse => FungiblesError::InUse, + _ => panic!("Unexpected pallet assets error. This error is unknown to pallet assets"), + } + } +} + +impl From for FungiblesError { + fn from(error: PopApiError) -> Self { + match error { + PopApiError::Assets(e) => e.into(), + // PopApiError::Balances(e) => todo!("balances: {:?}", e), + PopApiError::Balances(e) => e.into(), + // PopApiError::Contracts(_e) => todo!("contracts"), + // PopApiError::SystemCallFiltered => 100, + // PopApiError::UnknownStatusCode(u) => u, + _ => panic!("Unexpected pallet assets error. This error is unknown to pallet assets"), + } + } +} diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index 7ad40f15..d6b0261c 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1 +1 @@ -pub mod trust_backed; \ No newline at end of file +pub mod fungibles; \ No newline at end of file diff --git a/pop-api/src/v0/assets/trust_backed.rs b/pop-api/src/v0/assets/trust_backed.rs deleted file mode 100644 index fb413413..00000000 --- a/pop-api/src/v0/assets/trust_backed.rs +++ /dev/null @@ -1,519 +0,0 @@ -use crate::{Balance, PopApiError::UnknownStatusCode, RuntimeCall, *}; -use ink::prelude::vec::Vec; -use primitives::{AssetId, MultiAddress}; -use scale::{Compact, Encode}; - -type Result = core::result::Result; - -/// https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/assets/src/lib.rs -/// -/// Extrinsics within pallet assets (TrustBackedAssets Instance) that can be used via the pop api on Pop Network: -/// 1. create -/// 2. start_destroy -/// 3. destroy_accounts -/// 4. destroy_approvals -/// 5. finish_destroy -/// 6. mint -/// 7. burn -/// 8. transfer -/// 9. transfer_keep_alive -/// 10. force_transfer -/// 11. freeze -/// 12. thaw -/// 13. freeze_asset -/// 14. thaw_asset -/// 15. transfer_ownership -/// 16. set_team -/// 17. set_metadata -/// 18. clear_metadata -/// 19. approve_transfer -/// 20. cancel_approval -/// 21. force_cancel_approval -/// 22. transfer_approved -/// 23. touch -/// 24. refund -/// 25. set_min_balance -/// 26. touch_other -/// 27. refund_other -/// 28. block - - -/// Issue a new class of fungible assets from a public origin. -pub fn create( - id: AssetId, - admin: impl Into>, - min_balance: Balance, -) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Create { - id: id.into(), - admin: admin.into(), - min_balance: Compact(min_balance), - }))?) -} - -/// Start the process of destroying a fungible asset class. -pub fn start_destroy(id: AssetId) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::StartDestroy { - id: id.into(), - }))?) -} - -/// Destroy all accounts associated with a given asset. -pub fn destroy_accounts(id: AssetId) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::DestroyAccounts { - id: id.into(), - }))?) -} - -/// Destroy all approvals associated with a given asset up to the max (see runtime configuration TrustBackedAssets `RemoveItemsLimit`). -pub fn destroy_approvals(id: AssetId) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::DestroyApprovals { - id: id.into(), - }))?) -} - -/// Complete destroying asset and unreserve currency. -pub fn finish_destroy(id: AssetId) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::FinishDestroy { - id: id.into(), - }))?) -} - -/// Mint assets of a particular class. -pub fn mint( - id: AssetId, - beneficiary: impl Into>, - amount: Balance, -) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Mint { - id: id.into(), - beneficiary: beneficiary.into(), - amount: Compact(amount), - }))?) -} - -/// Reduce the balance of `who` by as much as possible up to `amount` assets of `id`. -pub fn burn( - id: AssetId, - who: impl Into>, - amount: Balance, -) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Burn { - id: id.into(), - who: who.into(), - amount: Compact(amount), - }))?) -} - -/// Move some assets from the sender account to another. -pub fn transfer( - id: AssetId, - target: impl Into>, - amount: Balance, -) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Transfer { - id: id.into(), - target: target.into(), - amount: Compact(amount), - }))?) -} - -/// Move some assets from the sender account to another, keeping the sender account alive. -pub fn transfer_keep_alive( - id: AssetId, - target: impl Into>, - amount: Balance, -) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::TransferKeepAlive { - id: id.into(), - target: target.into(), - amount: Compact(amount), - }))?) -} - -/// Move some assets from one account to another. Sender should be the Admin of the asset `id`. -pub fn force_transfer( - id: AssetId, - source: impl Into>, - dest: impl Into>, - amount: Balance, -) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::ForceTransfer { - id: id.into(), - source: source.into(), - dest: dest.into(), - amount: Compact(amount), - }))?) -} - -/// Disallow further unprivileged transfers of an asset `id` from an account `who`. `who` -/// must already exist as an entry in `Account`s of the asset. If you want to freeze an -/// account that does not have an entry, use `touch_other` first. -pub fn freeze(id: AssetId, who: impl Into>) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Freeze { - id: id.into(), - who: who.into(), - }))?) -} - -/// Allow unprivileged transfers to and from an account again. -pub fn thaw(id: AssetId, who: impl Into>) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Thaw { - id: id.into(), - who: who.into(), - }))?) -} - -/// Disallow further unprivileged transfers for the asset class. -pub fn freeze_asset(id: AssetId) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::FreezeAsset { - id: id.into(), - }))?) -} - -/// Allow unprivileged transfers for the asset again. -pub fn thaw_asset(id: AssetId) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::ThawAsset { - id: id.into(), - }))?) -} - -/// Change the Owner of an asset. -pub fn transfer_ownership( - id: AssetId, - owner: impl Into>, -) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::TransferOwnership { - id: id.into(), - owner: owner.into(), - }))?) -} - -/// Change the Issuer, Admin and Freezer of an asset. -pub fn set_team( - id: AssetId, - issuer: impl Into>, - admin: impl Into>, - freezer: impl Into>, -) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::SetTeam { - id: id.into(), - issuer: issuer.into(), - admin: admin.into(), - freezer: freezer.into(), - }))?) -} - -/// Set the metadata for an asset. -pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::SetMetadata { - id: id.into(), - name, - symbol, - decimals, - }))?) -} - -/// Clear the metadata for an asset. -pub fn clear_metadata(id: AssetId) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::ClearMetadata { - id: id.into(), - }))?) -} - -/// Approve an amount of asset for transfer by a delegated third-party account. -pub fn approve_transfer( - id: AssetId, - delegate: impl Into>, - amount: Balance, -) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::ApproveTransfer { - id: id.into(), - delegate: delegate.into(), - amount: Compact(amount), - }))?) -} - -/// Cancel all of some asset approved for delegated transfer by a third-party account. -pub fn cancel_approval( - id: AssetId, - delegate: impl Into>, -) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::CancelApproval { - id: id.into(), - delegate: delegate.into(), - }))?) -} - -/// Cancel all of some asset approved for delegated transfer by a third-party account. -pub fn force_cancel_approval( - id: AssetId, - owner: impl Into>, - delegate: impl Into>, -) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::ForceCancelApproval { - id: id.into(), - owner: owner.into(), - delegate: delegate.into(), - }))?) -} - -/// Transfer some asset balance from a previously delegated account to some third-party -/// account. -pub fn transfer_approved( - id: AssetId, - owner: impl Into>, - destination: impl Into>, - amount: Balance, -) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::TransferApproved { - id: id.into(), - owner: owner.into(), - destination: destination.into(), - amount: Compact(amount), - }))?) -} - -/// Create an asset account for non-provider assets. -pub fn touch(id: AssetId) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Touch { - id: id.into(), - }))?) -} - -/// Return the deposit (if any) of an asset account or a consumer reference (if any) of an -/// account. -pub fn refund(id: AssetId, allow_burn: bool) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Refund { - id: id.into(), - allow_burn, - }))?) -} - -/// Sets the minimum balance of an asset. -pub fn set_min_balance(id: AssetId, min_balance: Balance) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::SetMinBalance { - id: id.into(), - min_balance: Compact(min_balance), - }))?) -} - -/// Create an asset account for `who`. -pub fn touch_other(id: AssetId, who: impl Into>) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::TouchOther { - id: id.into(), - who: who.into(), - }))?) -} - -/// Return the deposit (if any) of a target asset account. Useful if you are the depositor. -pub fn refund_other(id: AssetId, who: impl Into>) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::RefundOther { - id: id.into(), - who: who.into(), - }))?) -} - -/// Disallow further unprivileged transfers of an asset `id` to and from an account `who`. -pub fn block(id: AssetId, who: impl Into>) -> Result<()> { - Ok(dispatch(RuntimeCall::TrustBackedAssets(TrustBackedAssetsCalls::Block { - id: id.into(), - who: who.into(), - }))?) -} - -pub fn asset_exists(id: AssetId) -> Result { - Ok(state::read(RuntimeStateKeys::TrustBackedAssets(TrustBackedAssetsKeys::AssetExists(id)))?) -} - -// Parameters to extrinsics representing an asset id (`AssetIdParameter`) and a balance amount (`Balance`) are expected -// to be compact encoded. The pop api handles that for the developer. -// -// reference: https://substrate.stackexchange.com/questions/1873/what-is-the-meaning-of-palletcompact-in-pallet-development -// -// Asset id that is compact encoded. -type AssetIdParameter = Compact; -// Balance amount that is compact encoded. -type BalanceParameter = Compact; - -#[derive(Encode)] -pub(crate) enum TrustBackedAssetsCalls { - #[codec(index = 0)] - Create { - id: AssetIdParameter, - admin: MultiAddress, - min_balance: BalanceParameter, - }, - #[codec(index = 2)] - StartDestroy { id: AssetIdParameter }, - #[codec(index = 3)] - DestroyAccounts { id: AssetIdParameter }, - #[codec(index = 4)] - DestroyApprovals { id: AssetIdParameter }, - #[codec(index = 5)] - FinishDestroy { id: AssetIdParameter }, - #[codec(index = 6)] - Mint { - id: AssetIdParameter, - beneficiary: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 7)] - Burn { id: AssetIdParameter, who: MultiAddress, amount: BalanceParameter }, - #[codec(index = 8)] - Transfer { id: AssetIdParameter, target: MultiAddress, amount: BalanceParameter }, - #[codec(index = 9)] - TransferKeepAlive { - id: AssetIdParameter, - target: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 10)] - ForceTransfer { - id: AssetIdParameter, - source: MultiAddress, - dest: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 11)] - Freeze { id: AssetIdParameter, who: MultiAddress }, - #[codec(index = 12)] - Thaw { id: AssetIdParameter, who: MultiAddress }, - #[codec(index = 13)] - FreezeAsset { id: AssetIdParameter }, - #[codec(index = 14)] - ThawAsset { id: AssetIdParameter }, - #[codec(index = 15)] - TransferOwnership { id: AssetIdParameter, owner: MultiAddress }, - #[codec(index = 16)] - SetTeam { - id: AssetIdParameter, - issuer: MultiAddress, - admin: MultiAddress, - freezer: MultiAddress, - }, - #[codec(index = 17)] - SetMetadata { id: AssetIdParameter, name: Vec, symbol: Vec, decimals: u8 }, - #[codec(index = 18)] - ClearMetadata { id: AssetIdParameter }, - #[codec(index = 22)] - ApproveTransfer { - id: AssetIdParameter, - delegate: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 23)] - CancelApproval { id: AssetIdParameter, delegate: MultiAddress }, - #[codec(index = 24)] - ForceCancelApproval { - id: AssetIdParameter, - owner: MultiAddress, - delegate: MultiAddress, - }, - #[codec(index = 25)] - TransferApproved { - id: AssetIdParameter, - owner: MultiAddress, - destination: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 26)] - Touch { id: AssetIdParameter }, - #[codec(index = 27)] - Refund { id: AssetIdParameter, allow_burn: bool }, - #[codec(index = 28)] - SetMinBalance { id: AssetIdParameter, min_balance: BalanceParameter }, - #[codec(index = 29)] - TouchOther { id: AssetIdParameter, who: MultiAddress }, - #[codec(index = 30)] - RefundOther { id: AssetIdParameter, who: MultiAddress }, - #[codec(index = 31)] - Block { id: AssetIdParameter, who: MultiAddress }, -} - -#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum Error { - /// Account balance must be greater than or equal to the transfer amount. - BalanceLow, - /// The account to alter does not exist. - NoAccount, - /// The signing account has no permission to do the operation. - NoPermission, - /// The given asset ID is unknown. - Unknown, - /// The origin account is frozen. - Frozen, - /// The asset ID is already taken. - InUse, - /// Invalid witness data given. - BadWitness, - /// Minimum balance should be non-zero. - MinBalanceZero, - /// Unable to increment the consumer reference counters on the account. Either no provider - /// reference exists to allow a non-zero balance of a non-self-sufficient asset, or one - /// fewer then the maximum number of consumers has been reached. - UnavailableConsumer, - /// Invalid metadata given. - BadMetadata, - /// No approval exists that would allow the transfer. - Unapproved, - /// The source account would not survive the transfer and it needs to stay alive. - WouldDie, - /// The asset-account already exists. - AlreadyExists, - /// The asset-account doesn't have an associated deposit. - NoDeposit, - /// The operation would result in funds being burned. - WouldBurn, - /// The asset is a live asset and is actively being used. Usually emit for operations such - /// as `start_destroy` which require the asset to be in a destroying state. - LiveAsset, - /// The asset is not live, and likely being destroyed. - AssetNotLive, - /// The asset status is not the expected status. - IncorrectStatus, - /// The asset should be frozen before the given operation. - NotFrozen, - /// Callback action resulted in error - CallbackFailed, -} - -impl TryFrom for Error { - type Error = PopApiError; - - fn try_from(status_code: u32) -> core::result::Result { - use Error::*; - match status_code { - 0 => Ok(BalanceLow), - 1 => Ok(NoAccount), - 2 => Ok(NoPermission), - 3 => Ok(Unknown), - 4 => Ok(Frozen), - 5 => Ok(InUse), - 6 => Ok(BadWitness), - 7 => Ok(MinBalanceZero), - 8 => Ok(UnavailableConsumer), - 9 => Ok(BadMetadata), - 10 => Ok(Unapproved), - 11 => Ok(WouldDie), - 12 => Ok(AlreadyExists), - 13 => Ok(NoDeposit), - 14 => Ok(WouldBurn), - 15 => Ok(LiveAsset), - 16 => Ok(AssetNotLive), - 17 => Ok(IncorrectStatus), - 18 => Ok(NotFrozen), - _ => Err(UnknownStatusCode(status_code)), - } - } -} - -impl From for Error { - fn from(error: PopApiError) -> Self { - match error { - PopApiError::TrustBackedAssets(e) => e, - _ => panic!("Unexpected pallet assets error. This error is unknown to pallet assets"), - } - } -} diff --git a/pop-api/src/v0/balances.rs b/pop-api/src/v0/balances.rs index bf029178..bc48711e 100644 --- a/pop-api/src/v0/balances.rs +++ b/pop-api/src/v0/balances.rs @@ -28,7 +28,7 @@ pub(crate) enum BalancesCall { #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum Error { +pub(crate) enum Error { /// Vesting balance too high to send value. VestingBalance, /// Account liquidity restrictions prevent withdrawal. diff --git a/pop-api/src/v0/contracts.rs b/pop-api/src/v0/contracts.rs new file mode 100644 index 00000000..d7a1a5dd --- /dev/null +++ b/pop-api/src/v0/contracts.rs @@ -0,0 +1,156 @@ +use crate::{ + PopApiError, + PopApiError::UnknownStatusCode, +}; + +#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum Error { + /// Invalid schedule supplied, e.g. with zero weight of a basic operation. + InvalidSchedule, + /// Invalid combination of flags supplied to `seal_call` or `seal_delegate_call`. + InvalidCallFlags, + /// The executed contract exhausted its gas limit. + OutOfGas, + /// The output buffer supplied to a contract API call was too small. + OutputBufferTooSmall, + /// Performing the requested transfer failed. Probably because there isn't enough + /// free balance in the sender's account. + TransferFailed, + /// Performing a call was denied because the calling depth reached the limit + /// of what is specified in the schedule. + MaxCallDepthReached, + /// No contract was found at the specified address. + ContractNotFound, + /// The code supplied to `instantiate_with_code` exceeds the limit specified in the + /// current schedule. + CodeTooLarge, + /// No code could be found at the supplied code hash. + CodeNotFound, + /// No code info could be found at the supplied code hash. + CodeInfoNotFound, + /// A buffer outside of sandbox memory was passed to a contract API function. + OutOfBounds, + /// Input passed to a contract API function failed to decode as expected type. + DecodingFailed, + /// Contract trapped during execution. + ContractTrapped, + /// The size defined in `T::MaxValueSize` was exceeded. + ValueTooLarge, + /// Termination of a contract is not allowed while the contract is already + /// on the call stack. Can be triggered by `seal_terminate`. + TerminatedWhileReentrant, + /// `seal_call` forwarded this contracts input. It therefore is no longer available. + InputForwarded, + /// The subject passed to `seal_random` exceeds the limit. + RandomSubjectTooLong, + /// The amount of topics passed to `seal_deposit_events` exceeds the limit. + TooManyTopics, + /// The chain does not provide a chain extension. Calling the chain extension results + /// in this error. Note that this usually shouldn't happen as deploying such contracts + /// is rejected. + NoChainExtension, + /// Failed to decode the XCM program. + XCMDecodeFailed, + /// A contract with the same AccountId already exists. + DuplicateContract, + /// A contract self destructed in its constructor. + /// + /// This can be triggered by a call to `seal_terminate`. + TerminatedInConstructor, + /// A call tried to invoke a contract that is flagged as non-reentrant. + /// The only other cause is that a call from a contract into the runtime tried to call back + /// into `pallet-contracts`. This would make the whole pallet reentrant with regard to + /// contract code execution which is not supported. + ReentranceDenied, + /// Origin doesn't have enough balance to pay the required storage deposits. + StorageDepositNotEnoughFunds, + /// More storage was created than allowed by the storage deposit limit. + StorageDepositLimitExhausted, + /// Code removal was denied because the code is still in use by at least one contract. + CodeInUse, + /// The contract ran to completion but decided to revert its storage changes. + /// Please note that this error is only returned from extrinsics. When called directly + /// or via RPC an `Ok` will be returned. In this case the caller needs to inspect the flags + /// to determine whether a reversion has taken place. + ContractReverted, + /// The contract's code was found to be invalid during validation. + /// + /// The most likely cause of this is that an API was used which is not supported by the + /// node. This happens if an older node is used with a new version of ink!. Try updating + /// your node to the newest available version. + /// + /// A more detailed error can be found on the node console if debug messages are enabled + /// by supplying `-lruntime::contracts=debug`. + CodeRejected, + /// An indeterministic code was used in a context where this is not permitted. + Indeterministic, + /// A pending migration needs to complete before the extrinsic can be called. + MigrationInProgress, + /// Migrate dispatch call was attempted but no migration was performed. + NoMigrationPerformed, + /// The contract has reached its maximum number of delegate dependencies. + MaxDelegateDependenciesReached, + /// The dependency was not found in the contract's delegate dependencies. + DelegateDependencyNotFound, + /// The contract already depends on the given delegate dependency. + DelegateDependencyAlreadyExists, + /// Can not add a delegate dependency to the code hash of the contract itself. + CannotAddSelfAsDelegateDependency, +} + + +impl TryFrom for Error { + type Error = PopApiError; + + fn try_from(status_code: u32) -> core::result::Result { + use Error::*; + match status_code { + 0 => Ok(InvalidSchedule), + 1 => Ok(InvalidCallFlags), + 2 => Ok(OutOfGas), + 3 => Ok(OutputBufferTooSmall), + 4 => Ok(TransferFailed), + 5 => Ok(MaxCallDepthReached), + 6 => Ok(ContractNotFound), + 7 => Ok(CodeTooLarge), + 8 => Ok(CodeNotFound), + 9 => Ok(CodeInfoNotFound), + 10 => Ok(OutOfBounds), + 11 => Ok(DecodingFailed), + 12 => Ok(ContractTrapped), + 13 => Ok(ValueTooLarge), + 14 => Ok(TerminatedWhileReentrant), + 15 => Ok(InputForwarded), + 16 => Ok(RandomSubjectTooLong), + 17 => Ok(TooManyTopics), + 18 => Ok(NoChainExtension), + 19 => Ok(XCMDecodeFailed), + 20 => Ok(DuplicateContract), + 21 => Ok(TerminatedInConstructor), + 22 => Ok(ReentranceDenied), + 23 => Ok(StorageDepositNotEnoughFunds), + 24 => Ok(StorageDepositLimitExhausted), + 25 => Ok(CodeInUse), + 26 => Ok(ContractReverted), + 27 => Ok(CodeRejected), + 28 => Ok(Indeterministic), + 29 => Ok(MigrationInProgress), + 30 => Ok(NoMigrationPerformed), + 31 => Ok(MaxDelegateDependenciesReached), + 32 => Ok(DelegateDependencyNotFound), + 33 => Ok(DelegateDependencyAlreadyExists), + 34 => Ok(CannotAddSelfAsDelegateDependency), + _ => Err(UnknownStatusCode(status_code)), + } + } +} + +impl From for Error { + fn from(error: PopApiError) -> Self { + match error { + PopApiError::Contracts(e) => e, + _ => panic!("expected balances error"), + } + } +} \ No newline at end of file diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index d914db24..def37e55 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -4,6 +4,7 @@ use crate::{ }; pub mod balances; +pub mod contracts; pub mod cross_chain; pub mod nfts; pub mod state; @@ -20,5 +21,5 @@ pub(crate) enum RuntimeCall { #[codec(index = 50)] Nfts(nfts::NftCalls), #[codec(index = 52)] - TrustBackedAssets(assets::trust_backed::TrustBackedAssetsCalls), + Assets(assets::fungibles::AssetsCall), } diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml index b6f9adaa..1098b557 100644 --- a/primitives/Cargo.toml +++ b/primitives/Cargo.toml @@ -6,13 +6,25 @@ edition = "2021" [dependencies] bounded-collections = { version = "0.1", default-features = false } -scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } + +scale = { package = "parity-scale-codec", version = "3.6.9", default-features = false, features = ["derive"] } +scale-decode = { version = "0.10.0", default-features = false, features = ["derive"], optional = true } +scale-encode = { version = "0.5.0", default-features = false, features = ["derive"], optional = true } scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true } +#scale = { workspace = true, features = ["max-encoded-len"] } +#scale-decode = { workspace = true, features = ["derive"], optional = true } +#scale-encode = { workspace = true, features = ["derive"], optional = true } +#scale-info = { workspace = true, features = ["derive"], optional = true } + [features] default = ["std"] std = [ "bounded-collections/std", "scale/std", + "scale-decode/std", + "scale-encode/std", "scale-info/std", ] +devnet = [] +testnet = [] \ No newline at end of file diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index ebad36d3..7c1672b8 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -1,10 +1,22 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] pub use bounded_collections::{BoundedBTreeMap, BoundedBTreeSet, BoundedVec, ConstU32}; +use scale::{Decode, Encode, MaxEncodedLen}; +#[cfg(feature = "std")] +use { + scale_decode::DecodeAsType, + scale_encode::EncodeAsType, + scale_info::TypeInfo, +}; + pub mod cross_chain; pub mod storage_keys; +#[derive(Encode, Decode, Debug, MaxEncodedLen, Eq, PartialEq, Ord, PartialOrd)] +#[cfg_attr(feature = "std", derive(TypeInfo, DecodeAsType, EncodeAsType))] +pub struct AccountId(pub [u8; 32]); + // Identifier for the class of asset. pub type AssetId = u32; // Id used for identifying non-fungible collections. diff --git a/primitives/src/storage_keys.rs b/primitives/src/storage_keys.rs index a03b3a09..67448842 100644 --- a/primitives/src/storage_keys.rs +++ b/primitives/src/storage_keys.rs @@ -1,11 +1,12 @@ use super::*; -use scale::{Decode, Encode, MaxEncodedLen}; +// use scale::{Decode, Encode, MaxEncodedLen}; #[derive(Encode, Decode, Debug, MaxEncodedLen)] pub enum RuntimeStateKeys { Nfts(NftsKeys), ParachainSystem(ParachainSystemKeys), - TrustBackedAssets(TrustBackedAssetsKeys), + #[cfg(feature = "devnet")] + Assets(AssetsKeys), } #[derive(Encode, Decode, Debug, MaxEncodedLen)] @@ -35,8 +36,15 @@ pub enum NftsKeys { CollectionAttribute(CollectionId, BoundedVec), } +/// The required input for state queries in pallet assets. +#[cfg(feature = "devnet")] #[derive(Encode, Decode, Debug, MaxEncodedLen)] -pub enum TrustBackedAssetsKeys { +pub enum AssetsKeys { + Allowance(AssetId, AccountId, AccountId), /// Check if the asset exists. AssetExists(AssetId), + /// Check balance. + BalanceOf(AssetId, AccountId), + /// Returns the total token supply for a given asset ID. + TotalSupply(AssetId), } diff --git a/runtime/devnet/Cargo.toml b/runtime/devnet/Cargo.toml index 55d9942d..d4fe2923 100644 --- a/runtime/devnet/Cargo.toml +++ b/runtime/devnet/Cargo.toml @@ -22,7 +22,7 @@ scale-info.workspace = true smallvec.workspace = true # Local -pop-primitives.workspace = true +pop-primitives = { workspace = true, default-features = false, features = ["devnet"] } pop-runtime-common = { workspace = true, default-features = false } # Substrate diff --git a/runtime/devnet/src/config/assets.rs b/runtime/devnet/src/config/assets.rs index f51f8875..2c8ea952 100644 --- a/runtime/devnet/src/config/assets.rs +++ b/runtime/devnet/src/config/assets.rs @@ -1,6 +1,6 @@ use crate::{ - deposit, AccountId, Balance, Balances, BlockNumber, Nfts, Runtime, RuntimeEvent, - RuntimeHoldReason, TrustBackedAssets, DAYS, EXISTENTIAL_DEPOSIT, UNIT, + deposit, AccountId, Assets, Balance, Balances, BlockNumber, Nfts, Runtime, RuntimeEvent, + RuntimeHoldReason, DAYS, EXISTENTIAL_DEPOSIT, UNIT, }; use frame_support::{ parameter_types, @@ -86,7 +86,7 @@ impl pallet_nft_fractionalization::Config for Runtime { type NftId = ::ItemId; type AssetBalance = >::Balance; type AssetId = >::AssetId; - type Assets = TrustBackedAssets; + type Assets = Assets; type Nfts = Nfts; type PalletId = NftFractionalizationPalletId; type WeightInfo = pallet_nft_fractionalization::weights::SubstrateWeight; @@ -96,7 +96,7 @@ impl pallet_nft_fractionalization::Config for Runtime { } pub type TrustBackedAssetsInstance = pallet_assets::Instance1; -pub(crate) type TrustBackedAssetsCall = pallet_assets::Call; +pub(crate) type AssetsCall = pallet_assets::Call; impl pallet_assets::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Balance = Balance; diff --git a/runtime/devnet/src/config/mod.rs b/runtime/devnet/src/config/mod.rs index a3a64c92..c370bb1d 100644 --- a/runtime/devnet/src/config/mod.rs +++ b/runtime/devnet/src/config/mod.rs @@ -1,4 +1,4 @@ -mod assets; +pub(crate) mod assets; mod contracts; mod proxy; // Public due to integration tests crate. diff --git a/runtime/devnet/src/config/proxy.rs b/runtime/devnet/src/config/proxy.rs index a4fd479a..07d5f0f8 100644 --- a/runtime/devnet/src/config/proxy.rs +++ b/runtime/devnet/src/config/proxy.rs @@ -1,4 +1,4 @@ -use super::assets::TrustBackedAssetsCall; +use super::assets::AssetsCall; use crate::{Balances, Runtime, RuntimeCall, RuntimeEvent}; use frame_support::traits::InstanceFilter; use pop_runtime_common::proxy::{ @@ -34,16 +34,16 @@ impl InstanceFilter for ProxyType { }, ProxyType::AssetOwner => matches!( c, - RuntimeCall::Assets(TrustBackedAssetsCall::create { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::start_destroy { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::destroy_accounts { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::destroy_approvals { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::finish_destroy { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::transfer_ownership { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::set_team { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::set_metadata { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::clear_metadata { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::set_min_balance { .. }) + RuntimeCall::Assets(AssetsCall::create { .. }) + | RuntimeCall::Assets(AssetsCall::start_destroy { .. }) + | RuntimeCall::Assets(AssetsCall::destroy_accounts { .. }) + | RuntimeCall::Assets(AssetsCall::destroy_approvals { .. }) + | RuntimeCall::Assets(AssetsCall::finish_destroy { .. }) + | RuntimeCall::Assets(AssetsCall::transfer_ownership { .. }) + | RuntimeCall::Assets(AssetsCall::set_team { .. }) + | RuntimeCall::Assets(AssetsCall::set_metadata { .. }) + | RuntimeCall::Assets(AssetsCall::clear_metadata { .. }) + | RuntimeCall::Assets(AssetsCall::set_min_balance { .. }) | RuntimeCall::Nfts(pallet_nfts::Call::create { .. }) | RuntimeCall::Nfts(pallet_nfts::Call::destroy { .. }) | RuntimeCall::Nfts(pallet_nfts::Call::redeposit { .. }) @@ -56,15 +56,15 @@ impl InstanceFilter for ProxyType { ), ProxyType::AssetManager => matches!( c, - RuntimeCall::Assets(TrustBackedAssetsCall::mint { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::burn { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::freeze { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::block { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::thaw { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::freeze_asset { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::thaw_asset { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::touch_other { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::refund_other { .. }) + RuntimeCall::Assets(AssetsCall::mint { .. }) + | RuntimeCall::Assets(AssetsCall::burn { .. }) + | RuntimeCall::Assets(AssetsCall::freeze { .. }) + | RuntimeCall::Assets(AssetsCall::block { .. }) + | RuntimeCall::Assets(AssetsCall::thaw { .. }) + | RuntimeCall::Assets(AssetsCall::freeze_asset { .. }) + | RuntimeCall::Assets(AssetsCall::thaw_asset { .. }) + | RuntimeCall::Assets(AssetsCall::touch_other { .. }) + | RuntimeCall::Assets(AssetsCall::refund_other { .. }) | RuntimeCall::Nfts(pallet_nfts::Call::force_mint { .. }) | RuntimeCall::Nfts(pallet_nfts::Call::update_mint_settings { .. }) | RuntimeCall::Nfts(pallet_nfts::Call::mint_pre_signed { .. }) diff --git a/runtime/devnet/src/extensions.rs b/runtime/devnet/src/extensions.rs deleted file mode 100644 index c8e88eb2..00000000 --- a/runtime/devnet/src/extensions.rs +++ /dev/null @@ -1,1018 +0,0 @@ -use cumulus_pallet_parachain_system::RelaychainDataProvider; -use frame_support::traits::{Contains, OriginTrait}; -use frame_support::{ - dispatch::{GetDispatchInfo, RawOrigin}, - pallet_prelude::*, - traits::{fungibles::Inspect, nonfungibles_v2::Inspect as NonFungiblesInspect}, -}; -use pallet_contracts::chain_extension::{ - BufInBufOutState, ChainExtension, ChargedAmount, Environment, Ext, InitState, RetVal, -}; -use pop_primitives::{ - cross_chain::CrossChainMessage, - storage_keys::{NftsKeys, ParachainSystemKeys, RuntimeStateKeys, TrustBackedAssetsKeys}, - AssetId, CollectionId, ItemId, -}; -use sp_core::crypto::UncheckedFrom; -use sp_runtime::{ - traits::{BlockNumberProvider, Dispatchable}, - DispatchError, -}; -use sp_std::{boxed::Box, vec::Vec}; -use xcm::{ - latest::{prelude::*, OriginKind::SovereignAccount}, - VersionedXcm, -}; - -use crate::{ - assets_config::TrustBackedAssetsInstance, AccountId, AllowedPopApiCalls, RuntimeCall, - RuntimeOrigin, UNIT, -}; - -const LOG_TARGET: &str = "pop-api::extension"; - -type ContractSchedule = ::Schedule; - -#[derive(Default)] -pub struct PopApiExtension; - -impl ChainExtension for PopApiExtension -where - T: pallet_contracts::Config - + pallet_xcm::Config - + pallet_assets::Config - + pallet_nfts::Config - + cumulus_pallet_parachain_system::Config - + frame_system::Config< - RuntimeOrigin = RuntimeOrigin, - AccountId = AccountId, - RuntimeCall = RuntimeCall, - >, - T::AccountId: UncheckedFrom + AsRef<[u8]>, -{ - fn call(&mut self, env: Environment) -> Result - where - E: Ext, - T::AccountId: UncheckedFrom + AsRef<[u8]>, - { - log::debug!(target:LOG_TARGET, " extension called "); - match v0::FuncId::try_from(env.func_id())? { - v0::FuncId::Dispatch => { - match dispatch::(env) { - Ok(()) => Ok(RetVal::Converging(0)), - Err(DispatchError::Module(error)) => { - // encode status code = pallet index in runtime + error index, allowing for - // 999 errors - Ok(RetVal::Converging( - (error.index as u32 * 1_000) + u32::from_le_bytes(error.error), - )) - }, - Err(e) => Err(e), - } - }, - v0::FuncId::ReadState => { - read_state::(env)?; - Ok(RetVal::Converging(0)) - }, - v0::FuncId::SendXcm => { - send_xcm::(env)?; - Ok(RetVal::Converging(0)) - }, - } - } -} - -pub mod v0 { - #[derive(Debug)] - pub enum FuncId { - Dispatch, - ReadState, - SendXcm, - } -} - -impl TryFrom for v0::FuncId { - type Error = DispatchError; - - fn try_from(func_id: u16) -> Result { - let id = match func_id { - 0x0 => Self::Dispatch, - 0x1 => Self::ReadState, - 0x2 => Self::SendXcm, - _ => { - log::error!("called an unregistered `func_id`: {:}", func_id); - return Err(DispatchError::Other("unimplemented func_id")); - }, - }; - - Ok(id) - } -} - -fn dispatch_call( - env: &mut Environment, - call: RuntimeCall, - mut origin: RuntimeOrigin, - log_prefix: &str, -) -> Result<(), DispatchError> -where - T: frame_system::Config, - RuntimeOrigin: From>, - E: Ext, -{ - let charged_dispatch_weight = env.charge_weight(call.get_dispatch_info().weight)?; - - log::debug!(target:LOG_TARGET, "{} inputted RuntimeCall: {:?}", log_prefix, call); - - origin.add_filter(AllowedPopApiCalls::contains); - - match call.dispatch(origin) { - Ok(info) => { - log::debug!(target:LOG_TARGET, "{} success, actual weight: {:?}", log_prefix, info.actual_weight); - - // refund weight if the actual weight is less than the charged weight - if let Some(actual_weight) = info.actual_weight { - env.adjust_weight(charged_dispatch_weight, actual_weight); - } - - Ok(()) - }, - Err(err) => { - log::debug!(target:LOG_TARGET, "{} failed: error: {:?}", log_prefix, err.error); - Err(err.error) - }, - } -} - -fn charge_overhead_weight( - env: &mut Environment, - len: u32, - log_prefix: &str, -) -> Result -where - T: pallet_contracts::Config, - E: Ext, -{ - let contract_host_weight = ContractSchedule::::get().host_fn_weights; - - // calculate weight for reading bytes of `len` - // reference: https://github.com/paritytech/polkadot-sdk/blob/117a9433dac88d5ac00c058c9b39c511d47749d2/substrate/frame/contracts/src/wasm/runtime.rs#L267 - let base_weight: Weight = contract_host_weight.return_per_byte.saturating_mul(len.into()); - - // debug_message weight is a good approximation of the additional overhead of going - // from contract layer to substrate layer. - // reference: https://github.com/paritytech/ink-examples/blob/b8d2caa52cf4691e0ddd7c919e4462311deb5ad0/psp22-extension/runtime/psp22-extension-example.rs#L236 - let overhead = contract_host_weight.debug_message; - - let charged_weight = env.charge_weight(base_weight.saturating_add(overhead))?; - log::debug!(target: LOG_TARGET, "{} charged weight: {:?}", log_prefix, charged_weight); - - Ok(charged_weight) -} - -fn dispatch(env: Environment) -> Result<(), DispatchError> -where - T: pallet_contracts::Config - + frame_system::Config, - RuntimeOrigin: From>, - E: Ext, -{ - const LOG_PREFIX: &str = " dispatch |"; - - let mut env = env.buf_in_buf_out(); - let len = env.in_len(); - - charge_overhead_weight::(&mut env, len, LOG_PREFIX)?; - - // read the input as RuntimeCall - let call: RuntimeCall = env.read_as_unbounded(len)?; - - // contract is the origin by default - let origin: RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); - - dispatch_call::(&mut env, call, origin, LOG_PREFIX) -} - -fn read_state(env: Environment) -> Result<(), DispatchError> -where - T: pallet_contracts::Config - + pallet_assets::Config - + pallet_nfts::Config - + cumulus_pallet_parachain_system::Config - + frame_system::Config, - E: Ext, -{ - const LOG_PREFIX: &str = " read_state |"; - - let mut env = env.buf_in_buf_out(); - - // To be conservative, we charge the weight for reading the input bytes of a fixed-size type. - let base_weight: Weight = ContractSchedule::::get() - .host_fn_weights - .return_per_byte - .saturating_mul(env.in_len().into()); - let charged_weight = env.charge_weight(base_weight)?; - - log::debug!(target:LOG_TARGET, "{} charged weight: {:?}", LOG_PREFIX, charged_weight); - - let key: RuntimeStateKeys = env.read_as()?; - - let result = match key { - RuntimeStateKeys::Nfts(key) => read_nfts_state::(key, &mut env), - RuntimeStateKeys::ParachainSystem(key) => { - read_parachain_system_state::(key, &mut env) - }, - RuntimeStateKeys::TrustBackedAssets(key) => { - read_trust_backed_assets_state::(key, &mut env) - }, - }? - .encode(); - - log::trace!( - target:LOG_TARGET, - "{} result: {:?}.", LOG_PREFIX, result - ); - env.write(&result, false, None).map_err(|e| { - log::trace!(target: LOG_TARGET, "{:?}", e); - DispatchError::Other("unable to write results to contract memory") - }) -} - -fn read_parachain_system_state( - key: ParachainSystemKeys, - env: &mut Environment, -) -> Result, DispatchError> -where - T: pallet_contracts::Config + cumulus_pallet_parachain_system::Config, - E: Ext, -{ - match key { - ParachainSystemKeys::LastRelayChainBlockNumber => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(RelaychainDataProvider::::current_block_number().encode()) - }, - } -} - -fn read_nfts_state( - key: NftsKeys, - env: &mut Environment, -) -> Result, DispatchError> -where - T: pallet_contracts::Config + pallet_nfts::Config, - E: Ext, -{ - match key { - NftsKeys::Collection(collection) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Collection::::get(collection).encode()) - }, - NftsKeys::CollectionOwner(collection) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::collection_owner(collection).encode()) - }, - NftsKeys::Item(collection, item) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Item::::get(collection, item).encode()) - }, - NftsKeys::Owner(collection, item) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::owner(collection, item).encode()) - }, - NftsKeys::Attribute(collection, item, key) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::attribute(&collection, &item, &key).encode()) - }, - // NftsKeys::CustomAttribute(account, collection, item, key) => { - // env.charge_weight(T::DbWeight::get().reads(1_u64))?; - // Ok(pallet_nfts::Pallet::::custom_attribute(&account, &collection, &item, &key) - // .encode()) - // }, - NftsKeys::SystemAttribute(collection, item, key) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::system_attribute(&collection, item.as_ref(), &key) - .encode()) - }, - NftsKeys::CollectionAttribute(collection, key) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::collection_attribute(&collection, &key).encode()) - }, - } -} - -fn read_trust_backed_assets_state( - key: TrustBackedAssetsKeys, - env: &mut Environment, -) -> Result, DispatchError> -where - T: pallet_contracts::Config - + pallet_assets::Config, - E: Ext, -{ - match key { - TrustBackedAssetsKeys::AssetExists(id) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_assets::Pallet::::asset_exists(id).encode()) - }, - } -} - -fn send_xcm(env: Environment) -> Result<(), DispatchError> -where - T: pallet_contracts::Config - + frame_system::Config< - RuntimeOrigin = RuntimeOrigin, - AccountId = AccountId, - RuntimeCall = RuntimeCall, - >, - E: Ext, -{ - const LOG_PREFIX: &str = " send_xcm |"; - - let mut env = env.buf_in_buf_out(); - let len = env.in_len(); - - let _ = charge_overhead_weight::(&mut env, len, LOG_PREFIX)?; - - // read the input as CrossChainMessage - let xc_call: CrossChainMessage = env.read_as::()?; - - // Determine the call to dispatch - let (dest, message) = match xc_call { - CrossChainMessage::Relay(message) => { - let dest = Location::parent().into_versioned(); - let assets: Asset = (Here, 10 * UNIT).into(); - let beneficiary: Location = - AccountId32 { id: (env.ext().address().clone()).into(), network: None }.into(); - let message = Xcm::builder() - .withdraw_asset(assets.clone().into()) - .buy_execution(assets.clone(), Unlimited) - .transact( - SovereignAccount, - Weight::from_parts(250_000_000, 10_000), - message.encode().into(), - ) - .refund_surplus() - .deposit_asset(assets.into(), beneficiary) - .build(); - (dest, message) - }, - }; - - // TODO: revisit to replace with signed contract origin - let origin: RuntimeOrigin = RawOrigin::Root.into(); - - // Generate runtime call to dispatch - let call = RuntimeCall::PolkadotXcm(pallet_xcm::Call::send { - dest: Box::new(dest), - message: Box::new(VersionedXcm::V4(message)), - }); - - dispatch_call::(&mut env, call, origin, LOG_PREFIX) -} - -#[cfg(test)] -mod tests { - pub use super::*; - pub use crate::*; - use enumflags2::BitFlags; - pub use pallet_contracts::Code; - use pallet_nfts::{CollectionConfig, CollectionSetting, CollectionSettings, MintSettings}; - use parachains_common::CollectionId; - pub use sp_runtime::{traits::Hash, AccountId32}; - - const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug; - - const ALICE: AccountId32 = AccountId32::new([1_u8; 32]); - const BOB: AccountId32 = AccountId32::new([2_u8; 32]); - const INITIAL_AMOUNT: u128 = 100_000 * UNIT; - const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024); - - fn new_test_ext() -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::::default() - .build_storage() - .expect("Frame system builds valid default genesis config"); - - pallet_balances::GenesisConfig:: { - balances: vec![(ALICE, INITIAL_AMOUNT), (BOB, INITIAL_AMOUNT)], - } - .assimilate_storage(&mut t) - .expect("Pallet balances storage can be assimilated"); - - let mut ext = sp_io::TestExternalities::new(t); - ext.execute_with(|| System::set_block_number(1)); - ext - } - - fn load_wasm_module(path: &str) -> std::io::Result<(Vec, ::Output)> - where - T: frame_system::Config, - { - let wasm_binary = std::fs::read(path)?; - let code_hash = T::Hashing::hash(&wasm_binary); - Ok((wasm_binary, code_hash)) - } - - fn function_selector(name: &str) -> Vec { - let hash = sp_io::hashing::blake2_256(name.as_bytes()); - [hash[0..4].to_vec()].concat() - } - - // NFT helper functions - fn collection_config_from_disabled_settings( - settings: BitFlags, - ) -> CollectionConfig { - CollectionConfig { - settings: CollectionSettings::from_disabled(settings), - max_supply: None, - mint_settings: MintSettings::default(), - } - } - - fn default_collection_config() -> CollectionConfig { - collection_config_from_disabled_settings(CollectionSetting::DepositRequired.into()) - } - - #[test] - #[ignore] - fn dispatch_balance_transfer_from_contract_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = load_wasm_module::( - "../../pop-api/examples/balance-transfer/target/ink/balance_transfer.wasm", - ) - .unwrap(); - - let init_value = 100 * UNIT; - - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - - let addr = result.account_id; - - let function = function_selector("transfer_through_runtime"); - let value_to_send: u128 = 10 * UNIT; - let params = [function, BOB.encode(), value_to_send.encode()].concat(); - - let bob_balance_before = Balances::free_balance(&BOB); - assert_eq!(bob_balance_before, INITIAL_AMOUNT); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // check for revert - assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); - - let bob_balance_after = Balances::free_balance(&BOB); - assert_eq!(bob_balance_before + value_to_send, bob_balance_after); - }); - } - - // Create a test for tesing create_nft_collection - #[test] - #[ignore] - fn dispatch_nfts_create_nft_collection() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = load_wasm_module::( - "../../pop-api/examples/nfts/target/ink/pop_api_nft_example.wasm", - ) - .unwrap(); - - let init_value = 100 * UNIT; - - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - - let addr = result.account_id; - - let function = function_selector("create_nft_collection"); - - let params = [function].concat(); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // check that the nft collection was created - assert_eq!(Nfts::collection_owner(0), Some(addr.clone().into())); - - // test reading the collection - let function = function_selector("read_collection"); - - let params = [function, 0.encode()].concat(); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // assert that the collection was read successfully - assert_eq!(result.result.clone().unwrap().data, vec![1, 1]); - }); - } - - #[test] - #[ignore] - fn dispatch_nfts_mint_from_contract_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = - load_wasm_module::("../../pop-api/examples/nfts/target/ink/nfts.wasm") - .unwrap(); - - let init_value = 100; - - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - - let addr = result.account_id; - - let collection_id: u32 = 0; - let item_id: u32 = 1; - - // create nft collection with contract as owner - assert_eq!( - Nfts::force_create( - RuntimeOrigin::root(), - addr.clone().into(), - default_collection_config() - ), - Ok(()) - ); - - assert_eq!(Nfts::collection_owner(collection_id), Some(addr.clone().into())); - // assert that the item does not exist yet - assert_eq!(Nfts::owner(collection_id, item_id), None); - - let function = function_selector("mint_through_runtime"); - - let params = - [function, collection_id.encode(), item_id.encode(), BOB.encode()].concat(); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // check for revert - assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); - - assert_eq!(Nfts::owner(collection_id, item_id), Some(BOB.into())); - }); - } - - #[test] - #[ignore] - fn nfts_mint_surfaces_error() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = - load_wasm_module::("../../pop-api/examples/nfts/target/ink/nfts.wasm") - .unwrap(); - - let init_value = 100; - - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - - let addr = result.account_id; - - let collection_id: u32 = 0; - let item_id: u32 = 1; - - let function = function_selector("mint_through_runtime"); - - let params = - [function, collection_id.encode(), item_id.encode(), BOB.encode()].concat(); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // check for revert with expected error - let result = result.result.unwrap(); - assert!(result.did_revert()); - }); - } - - #[test] - #[ignore] - fn reading_last_relay_chain_block_number_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = load_wasm_module::( - "../../pop-api/examples/read-runtime-state/target/ink/read_relay_blocknumber.wasm", - ) - .unwrap(); - - let init_value = 100; - - let contract = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!contract.result.did_revert(), "deploying contract reverted {:?}", contract); - - let addr = contract.account_id; - - let function = function_selector("read_relay_block_number"); - let params = [function].concat(); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::UnsafeCollect, - pallet_contracts::Determinism::Relaxed, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // check for revert - assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); - }); - } - - #[test] - #[ignore] - fn place_spot_order_from_contract_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = load_wasm_module::( - "../../pop-api/examples/place-spot-order/target/ink/spot_order.wasm", - ) - .unwrap(); - - let init_value = 100 * UNIT; - - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - - let addr = result.account_id; - - let function = function_selector("place_spot_order"); - - let max_amount = 1 * UNIT; - let para_id = 2000; - - let params = [function, max_amount.encode(), para_id.encode()].concat(); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // check for revert - assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); - }); - } - - #[test] - #[ignore] - fn dispatch_trust_backed_assets_mint_from_contract_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = load_wasm_module::( - "../../pop-api/examples/trust_backed_assets/target/ink/pop_api_trust_backed_assets_example.wasm", - ) - .unwrap(); - - let init_value = 100; - - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - let addr = result.account_id; - - let asset_id: u32 = 1; - let min_balance = 1; - let amount: u128 = 100 * UNIT; - let function = function_selector("mint_asset_through_runtime"); - let params = [function, asset_id.encode(), BOB.encode(), amount.encode()].concat(); - - // Mint asset which does not exist. - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - GAS_LIMIT, - None, - params.clone(), - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // Check for revert. - assert!(result.result.unwrap().did_revert(), "Contract should have been reverted!"); - - // Create asset with contract as owner. - assert_eq!( - TrustBackedAssets::force_create( - RuntimeOrigin::root(), - asset_id.into(), - addr.clone().into(), - true, - min_balance, - ), - Ok(()) - ); - - // Check Bob's asset balance before minting through contract. - let bob_balance_before = TrustBackedAssets::balance(asset_id, &BOB); - assert_eq!(bob_balance_before, 0); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - GAS_LIMIT, - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // Check for revert - assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); - - let bob_balance_after = TrustBackedAssets::balance(asset_id, &BOB); - assert_eq!(bob_balance_after, bob_balance_before + amount); - }); - } - - #[test] - #[ignore] - fn allow_call_filter_blocks_call() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = load_wasm_module::( - "../../tests/contracts/filtered-call/target/ink/pop_api_filtered_call.wasm", - ) - .unwrap(); - - let init_value = 100 * UNIT; - - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - - let addr = result.account_id; - - let function = function_selector("get_filtered"); - let params = [function].concat(); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("filtered result: {:?}", result); - } - - // check for revert - assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); - }); - } -} diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs new file mode 100644 index 00000000..096f21c9 --- /dev/null +++ b/runtime/devnet/src/extensions/mod.rs @@ -0,0 +1,896 @@ +use cumulus_pallet_parachain_system::RelaychainDataProvider; +use frame_support::traits::{Contains, OriginTrait}; +use frame_support::{ + dispatch::{GetDispatchInfo, RawOrigin}, + pallet_prelude::*, + traits::{ + fungibles::{approvals::Inspect as ApprovalInspect, Inspect}, + nonfungibles_v2::Inspect as NonFungiblesInspect, + }, +}; +use pallet_contracts::chain_extension::{ + BufInBufOutState, ChainExtension, ChargedAmount, Environment, Ext, InitState, RetVal, +}; +use pop_primitives::{ + cross_chain::CrossChainMessage, + storage_keys::{AssetsKeys, NftsKeys, ParachainSystemKeys, RuntimeStateKeys}, + AssetId, CollectionId, ItemId, +}; +use sp_core::crypto::UncheckedFrom; +use sp_runtime::{ + traits::{BlockNumberProvider, Dispatchable}, + DispatchError, +}; +use sp_std::{boxed::Box, vec::Vec}; +use xcm::{ + latest::{prelude::*, OriginKind::SovereignAccount}, + VersionedXcm, +}; + +use crate::{ + config::assets::TrustBackedAssetsInstance, AccountId, AllowedPopApiCalls, RuntimeCall, + RuntimeOrigin, UNIT, +}; + +#[cfg(test)] +mod tests; + +const LOG_TARGET: &str = "pop-api::extension"; + +type ContractSchedule = ::Schedule; + +#[derive(Default)] +pub struct PopApiExtension; + +impl ChainExtension for PopApiExtension +where + T: pallet_contracts::Config + + pallet_xcm::Config + + pallet_assets::Config + + pallet_nfts::Config + + cumulus_pallet_parachain_system::Config + + frame_system::Config< + RuntimeOrigin = RuntimeOrigin, + AccountId = AccountId, + RuntimeCall = RuntimeCall, + >, + T::AccountId: UncheckedFrom + AsRef<[u8]>, +{ + fn call(&mut self, env: Environment) -> Result + where + E: Ext, + // T::AccountId: UncheckedFrom + AsRef<[u8]>, + { + log::debug!(target:LOG_TARGET, " extension called "); + match v0::FuncId::try_from(env.func_id())? { + v0::FuncId::Dispatch => { + match dispatch::(env) { + Ok(()) => Ok(RetVal::Converging(0)), + Err(DispatchError::Module(error)) => { + // encode status code = pallet index in runtime + error index, allowing for + // 999 errors + Ok(RetVal::Converging( + (error.index as u32 * 1_000) + u32::from_le_bytes(error.error), + )) + }, + Err(e) => Err(e), + } + }, + v0::FuncId::ReadState => { + read_state::(env)?; + Ok(RetVal::Converging(0)) + }, + v0::FuncId::SendXcm => { + send_xcm::(env)?; + Ok(RetVal::Converging(0)) + }, + } + } +} + +pub mod v0 { + #[derive(Debug)] + pub enum FuncId { + Dispatch, + ReadState, + SendXcm, + } +} + +impl TryFrom for v0::FuncId { + type Error = DispatchError; + + fn try_from(func_id: u16) -> Result { + let id = match func_id { + 0x0 => Self::Dispatch, + 0x1 => Self::ReadState, + 0x2 => Self::SendXcm, + _ => { + log::error!("called an unregistered `func_id`: {:}", func_id); + return Err(DispatchError::Other("unimplemented func_id")); + }, + }; + + Ok(id) + } +} + +fn dispatch_call( + env: &mut Environment, + call: RuntimeCall, + mut origin: RuntimeOrigin, + log_prefix: &str, +) -> Result<(), DispatchError> +where + T: frame_system::Config, + RuntimeOrigin: From>, + E: Ext, +{ + let charged_dispatch_weight = env.charge_weight(call.get_dispatch_info().weight)?; + + log::debug!(target:LOG_TARGET, "{} Inputted RuntimeCall: {:?}", log_prefix, call); + + origin.add_filter(AllowedPopApiCalls::contains); + + match call.dispatch(origin) { + Ok(info) => { + log::debug!(target:LOG_TARGET, "{} success, actual weight: {:?}", log_prefix, info.actual_weight); + + // refund weight if the actual weight is less than the charged weight + if let Some(actual_weight) = info.actual_weight { + env.adjust_weight(charged_dispatch_weight, actual_weight); + } + + Ok(()) + }, + Err(err) => { + log::debug!(target:LOG_TARGET, "{} failed: error: {:?}", log_prefix, err.error); + Err(err.error) + }, + } +} + +fn charge_overhead_weight( + env: &mut Environment, + len: u32, + log_prefix: &str, +) -> Result +where + T: pallet_contracts::Config, + E: Ext, +{ + let contract_host_weight = ContractSchedule::::get().host_fn_weights; + + // calculate weight for reading bytes of `len` + // reference: https://github.com/paritytech/polkadot-sdk/blob/117a9433dac88d5ac00c058c9b39c511d47749d2/substrate/frame/contracts/src/wasm/runtime.rs#L267 + let base_weight: Weight = contract_host_weight.return_per_byte.saturating_mul(len.into()); + + // debug_message weight is a good approximation of the additional overhead of going + // from contract layer to substrate layer. + // reference: https://github.com/paritytech/ink-examples/blob/b8d2caa52cf4691e0ddd7c919e4462311deb5ad0/psp22-extension/runtime/psp22-extension-example.rs#L236 + let overhead = contract_host_weight.debug_message; + + let charged_weight = env.charge_weight(base_weight.saturating_add(overhead))?; + log::debug!(target: LOG_TARGET, "{} charged weight: {:?}", log_prefix, charged_weight); + + Ok(charged_weight) +} + +fn dispatch(env: Environment) -> Result<(), DispatchError> +where + T: pallet_contracts::Config + + frame_system::Config, + RuntimeOrigin: From>, + E: Ext, +{ + const LOG_PREFIX: &str = " dispatch |"; + + let mut env = env.buf_in_buf_out(); + let len = env.in_len(); + + charge_overhead_weight::(&mut env, len, LOG_PREFIX)?; + + // read the input as RuntimeCall + let call: RuntimeCall = env.read_as_unbounded(len)?; + + log::debug!(target: LOG_TARGET, "Read input as call successfully"); + + // contract is the origin by default + let origin: RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); + + dispatch_call::(&mut env, call, origin, LOG_PREFIX) +} + +fn read_state(env: Environment) -> Result<(), DispatchError> +where + T: pallet_contracts::Config + + pallet_assets::Config + + pallet_nfts::Config + + cumulus_pallet_parachain_system::Config + + frame_system::Config, + E: Ext, +{ + const LOG_PREFIX: &str = " read_state |"; + + let mut env = env.buf_in_buf_out(); + + // To be conservative, we charge the weight for reading the input bytes of a fixed-size type. + let base_weight: Weight = ContractSchedule::::get() + .host_fn_weights + .return_per_byte + .saturating_mul(env.in_len().into()); + let charged_weight = env.charge_weight(base_weight)?; + + log::debug!(target:LOG_TARGET, "{} charged weight: {:?}", LOG_PREFIX, charged_weight); + + let key: RuntimeStateKeys = env.read_as()?; + + let result = match key { + RuntimeStateKeys::Nfts(key) => read_nfts_state::(key, &mut env), + RuntimeStateKeys::ParachainSystem(key) => { + read_parachain_system_state::(key, &mut env) + }, + RuntimeStateKeys::Assets(key) => read_trust_backed_assets_state::(key, &mut env), + }? + .encode(); + + log::trace!( + target:LOG_TARGET, + "{} result: {:?}.", LOG_PREFIX, result + ); + env.write(&result, false, None).map_err(|e| { + log::trace!(target: LOG_TARGET, "{:?}", e); + DispatchError::Other("unable to write results to contract memory") + }) +} + +fn read_parachain_system_state( + key: ParachainSystemKeys, + env: &mut Environment, +) -> Result, DispatchError> +where + T: pallet_contracts::Config + cumulus_pallet_parachain_system::Config, + E: Ext, +{ + match key { + ParachainSystemKeys::LastRelayChainBlockNumber => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(RelaychainDataProvider::::current_block_number().encode()) + }, + } +} + +fn read_nfts_state( + key: NftsKeys, + env: &mut Environment, +) -> Result, DispatchError> +where + T: pallet_contracts::Config + pallet_nfts::Config, + E: Ext, +{ + match key { + NftsKeys::Collection(collection) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_nfts::Collection::::get(collection).encode()) + }, + NftsKeys::CollectionOwner(collection) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_nfts::Pallet::::collection_owner(collection).encode()) + }, + NftsKeys::Item(collection, item) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_nfts::Item::::get(collection, item).encode()) + }, + NftsKeys::Owner(collection, item) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_nfts::Pallet::::owner(collection, item).encode()) + }, + NftsKeys::Attribute(collection, item, key) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_nfts::Pallet::::attribute(&collection, &item, &key).encode()) + }, + // NftsKeys::CustomAttribute(account, collection, item, key) => { + // env.charge_weight(T::DbWeight::get().reads(1_u64))?; + // Ok(pallet_nfts::Pallet::::custom_attribute(&account, &collection, &item, &key) + // .encode()) + // }, + NftsKeys::SystemAttribute(collection, item, key) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_nfts::Pallet::::system_attribute(&collection, item.as_ref(), &key) + .encode()) + }, + NftsKeys::CollectionAttribute(collection, key) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_nfts::Pallet::::collection_attribute(&collection, &key).encode()) + }, + } +} + +fn read_trust_backed_assets_state( + key: AssetsKeys, + env: &mut Environment, +) -> Result, DispatchError> +where + T: pallet_contracts::Config + + pallet_assets::Config, + E: Ext, + T: frame_system::Config, +{ + match key { + AssetsKeys::Allowance(id, owner, spender) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_assets::Pallet::::allowance( + id, + &owner.0.into(), + &spender.0.into(), + ) + .encode()) + }, + AssetsKeys::AssetExists(id) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_assets::Pallet::::asset_exists(id).encode()) + }, + AssetsKeys::BalanceOf(id, owner) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_assets::Pallet::::balance(id, &owner.0.into()) + .encode()) + }, + AssetsKeys::TotalSupply(id) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_assets::Pallet::::total_supply(id).encode()) + }, + } +} + +fn send_xcm(env: Environment) -> Result<(), DispatchError> +where + T: pallet_contracts::Config + + frame_system::Config< + RuntimeOrigin = RuntimeOrigin, + AccountId = AccountId, + RuntimeCall = RuntimeCall, + >, + E: Ext, +{ + const LOG_PREFIX: &str = " send_xcm |"; + + let mut env = env.buf_in_buf_out(); + let len = env.in_len(); + + let _ = charge_overhead_weight::(&mut env, len, LOG_PREFIX)?; + + // read the input as CrossChainMessage + let xc_call: CrossChainMessage = env.read_as::()?; + + // Determine the call to dispatch + let (dest, message) = match xc_call { + CrossChainMessage::Relay(message) => { + let dest = Location::parent().into_versioned(); + let assets: Asset = (Here, 10 * UNIT).into(); + let beneficiary: Location = + AccountId32 { id: (env.ext().address().clone()).into(), network: None }.into(); + let message = Xcm::builder() + .withdraw_asset(assets.clone().into()) + .buy_execution(assets.clone(), Unlimited) + .transact( + SovereignAccount, + Weight::from_parts(250_000_000, 10_000), + message.encode().into(), + ) + .refund_surplus() + .deposit_asset(assets.into(), beneficiary) + .build(); + (dest, message) + }, + }; + + // TODO: revisit to replace with signed contract origin + let origin: RuntimeOrigin = RawOrigin::Root.into(); + + // Generate runtime call to dispatch + let call = RuntimeCall::PolkadotXcm(pallet_xcm::Call::send { + dest: Box::new(dest), + message: Box::new(VersionedXcm::V4(message)), + }); + + dispatch_call::(&mut env, call, origin, LOG_PREFIX) +} + +// use enumflags2::BitFlags; +// use pallet_nfts::{CollectionConfig, CollectionSetting, CollectionSettings, MintSettings}; +// use parachains_common::CollectionId; +// { +// // NFT helper functions +// fn collection_config_from_disabled_settings( +// settings: BitFlags, +// ) -> CollectionConfig { +// CollectionConfig { +// settings: CollectionSettings::from_disabled(settings), +// max_supply: None, +// mint_settings: MintSettings::default(), +// } +// } +// +// fn default_collection_config() -> CollectionConfig { +// collection_config_from_disabled_settings(CollectionSetting::DepositRequired.into()) +// } +// +// #[test] +// #[ignore] +// fn dispatch_balance_transfer_from_contract_works() { +// new_test_ext().execute_with(|| { +// let _ = env_logger::try_init(); +// +// let (wasm_binary, _) = load_wasm_module::( +// "../../pop-api/examples/balance-transfer/target/ink/balance_transfer.wasm", +// ) +// .unwrap(); +// +// let init_value = 100 * UNIT; +// +// let result = Contracts::bare_instantiate( +// ALICE, +// init_value, +// GAS_LIMIT, +// None, +// Code::Upload(wasm_binary), +// function_selector("new"), +// vec![], +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// ) +// .result +// .unwrap(); +// +// assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); +// +// let addr = result.account_id; +// +// let function = function_selector("transfer_through_runtime"); +// let value_to_send: u128 = 10 * UNIT; +// let params = [function, BOB.encode(), value_to_send.encode()].concat(); +// +// let bob_balance_before = Balances::free_balance(&BOB); +// assert_eq!(bob_balance_before, INITIAL_AMOUNT); +// +// let result = Contracts::bare_call( +// ALICE, +// addr.clone(), +// 0, +// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), +// None, +// params, +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// pallet_contracts::Determinism::Enforced, +// ); +// +// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { +// log::debug!( +// "Contract debug buffer - {:?}", +// String::from_utf8(result.debug_message.clone()) +// ); +// log::debug!("result: {:?}", result); +// } +// +// // check for revert +// assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); +// +// let bob_balance_after = Balances::free_balance(&BOB); +// assert_eq!(bob_balance_before + value_to_send, bob_balance_after); +// }); +// } +// +// // Create a test for tesing create_nft_collection +// #[test] +// #[ignore] +// fn dispatch_nfts_create_nft_collection() { +// new_test_ext().execute_with(|| { +// let _ = env_logger::try_init(); +// +// let (wasm_binary, _) = load_wasm_module::( +// "../../pop-api/examples/nfts/target/ink/pop_api_nft_example.wasm", +// ) +// .unwrap(); +// +// let init_value = 100 * UNIT; +// +// let result = Contracts::bare_instantiate( +// ALICE, +// init_value, +// GAS_LIMIT, +// None, +// Code::Upload(wasm_binary), +// function_selector("new"), +// vec![], +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// ) +// .result +// .unwrap(); +// +// assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); +// +// let addr = result.account_id; +// +// let function = function_selector("create_nft_collection"); +// +// let params = [function].concat(); +// +// let result = Contracts::bare_call( +// ALICE, +// addr.clone(), +// 0, +// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), +// None, +// params, +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// pallet_contracts::Determinism::Enforced, +// ); +// +// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { +// log::debug!( +// "Contract debug buffer - {:?}", +// String::from_utf8(result.debug_message.clone()) +// ); +// log::debug!("result: {:?}", result); +// } +// +// // check that the nft collection was created +// assert_eq!(Nfts::collection_owner(0), Some(addr.clone().into())); +// +// // test reading the collection +// let function = function_selector("read_collection"); +// +// let params = [function, 0.encode()].concat(); +// +// let result = Contracts::bare_call( +// ALICE, +// addr.clone(), +// 0, +// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), +// None, +// params, +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// pallet_contracts::Determinism::Enforced, +// ); +// +// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { +// log::debug!( +// "Contract debug buffer - {:?}", +// String::from_utf8(result.debug_message.clone()) +// ); +// log::debug!("result: {:?}", result); +// } +// +// // assert that the collection was read successfully +// assert_eq!(result.result.clone().unwrap().data, vec![1, 1]); +// }); +// } +// +// #[test] +// #[ignore] +// fn dispatch_nfts_mint_from_contract_works() { +// new_test_ext().execute_with(|| { +// let _ = env_logger::try_init(); +// +// let (wasm_binary, _) = +// load_wasm_module::("../../pop-api/examples/nfts/target/ink/nfts.wasm") +// .unwrap(); +// +// let init_value = 100; +// +// let result = Contracts::bare_instantiate( +// ALICE, +// init_value, +// GAS_LIMIT, +// None, +// Code::Upload(wasm_binary), +// function_selector("new"), +// vec![], +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// ) +// .result +// .unwrap(); +// +// assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); +// +// let addr = result.account_id; +// +// let collection_id: u32 = 0; +// let item_id: u32 = 1; +// +// // create nft collection with contract as owner +// assert_eq!( +// Nfts::force_create( +// RuntimeOrigin::root(), +// addr.clone().into(), +// default_collection_config() +// ), +// Ok(()) +// ); +// +// assert_eq!(Nfts::collection_owner(collection_id), Some(addr.clone().into())); +// // assert that the item does not exist yet +// assert_eq!(Nfts::owner(collection_id, item_id), None); +// +// let function = function_selector("mint_through_runtime"); +// +// let params = +// [function, collection_id.encode(), item_id.encode(), BOB.encode()].concat(); +// +// let result = Contracts::bare_call( +// ALICE, +// addr.clone(), +// 0, +// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), +// None, +// params, +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// pallet_contracts::Determinism::Enforced, +// ); +// +// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { +// log::debug!( +// "Contract debug buffer - {:?}", +// String::from_utf8(result.debug_message.clone()) +// ); +// log::debug!("result: {:?}", result); +// } +// +// // check for revert +// assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); +// +// assert_eq!(Nfts::owner(collection_id, item_id), Some(BOB.into())); +// }); +// } +// +// #[test] +// #[ignore] +// fn nfts_mint_surfaces_error() { +// new_test_ext().execute_with(|| { +// let _ = env_logger::try_init(); +// +// let (wasm_binary, _) = +// load_wasm_module::("../../pop-api/examples/nfts/target/ink/nfts.wasm") +// .unwrap(); +// +// let init_value = 100; +// +// let result = Contracts::bare_instantiate( +// ALICE, +// init_value, +// GAS_LIMIT, +// None, +// Code::Upload(wasm_binary), +// function_selector("new"), +// vec![], +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// ) +// .result +// .unwrap(); +// +// assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); +// +// let addr = result.account_id; +// +// let collection_id: u32 = 0; +// let item_id: u32 = 1; +// +// let function = function_selector("mint_through_runtime"); +// +// let params = +// [function, collection_id.encode(), item_id.encode(), BOB.encode()].concat(); +// +// let result = Contracts::bare_call( +// ALICE, +// addr.clone(), +// 0, +// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), +// None, +// params, +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// pallet_contracts::Determinism::Enforced, +// ); +// +// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { +// log::debug!( +// "Contract debug buffer - {:?}", +// String::from_utf8(result.debug_message.clone()) +// ); +// log::debug!("result: {:?}", result); +// } +// +// // check for revert with expected error +// let result = result.result.unwrap(); +// assert!(result.did_revert()); +// }); +// } +// +// #[test] +// #[ignore] +// fn reading_last_relay_chain_block_number_works() { +// new_test_ext().execute_with(|| { +// let _ = env_logger::try_init(); +// +// let (wasm_binary, _) = load_wasm_module::( +// "../../pop-api/examples/read-runtime-state/target/ink/read_relay_blocknumber.wasm", +// ) +// .unwrap(); +// +// let init_value = 100; +// +// let contract = Contracts::bare_instantiate( +// ALICE, +// init_value, +// GAS_LIMIT, +// None, +// Code::Upload(wasm_binary), +// function_selector("new"), +// vec![], +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// ) +// .result +// .unwrap(); +// +// assert!(!contract.result.did_revert(), "deploying contract reverted {:?}", contract); +// +// let addr = contract.account_id; +// +// let function = function_selector("read_relay_block_number"); +// let params = [function].concat(); +// +// let result = Contracts::bare_call( +// ALICE, +// addr.clone(), +// 0, +// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), +// None, +// params, +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::UnsafeCollect, +// pallet_contracts::Determinism::Relaxed, +// ); +// +// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { +// log::debug!( +// "Contract debug buffer - {:?}", +// String::from_utf8(result.debug_message.clone()) +// ); +// log::debug!("result: {:?}", result); +// } +// +// // check for revert +// assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); +// }); +// } +// +// #[test] +// #[ignore] +// fn place_spot_order_from_contract_works() { +// new_test_ext().execute_with(|| { +// let _ = env_logger::try_init(); +// +// let (wasm_binary, _) = load_wasm_module::( +// "../../pop-api/examples/place-spot-order/target/ink/spot_order.wasm", +// ) +// .unwrap(); +// +// let init_value = 100 * UNIT; +// +// let result = Contracts::bare_instantiate( +// ALICE, +// init_value, +// GAS_LIMIT, +// None, +// Code::Upload(wasm_binary), +// function_selector("new"), +// vec![], +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// ) +// .result +// .unwrap(); +// +// assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); +// +// let addr = result.account_id; +// +// let function = function_selector("place_spot_order"); +// +// let max_amount = 1 * UNIT; +// let para_id = 2000; +// +// let params = [function, max_amount.encode(), para_id.encode()].concat(); +// +// let result = Contracts::bare_call( +// ALICE, +// addr.clone(), +// 0, +// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), +// None, +// params, +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// pallet_contracts::Determinism::Enforced, +// ); +// +// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { +// log::debug!( +// "Contract debug buffer - {:?}", +// String::from_utf8(result.debug_message.clone()) +// ); +// log::debug!("result: {:?}", result); +// } +// +// // check for revert +// assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); +// }); +// } +// +// #[test] +// #[ignore] +// fn allow_call_filter_blocks_call() { +// new_test_ext().execute_with(|| { +// let _ = env_logger::try_init(); +// +// let (wasm_binary, _) = load_wasm_module::( +// "../../tests/contracts/filtered-call/target/ink/pop_api_filtered_call.wasm", +// ) +// .unwrap(); +// +// let init_value = 100 * UNIT; +// +// let result = Contracts::bare_instantiate( +// ALICE, +// init_value, +// GAS_LIMIT, +// None, +// Code::Upload(wasm_binary), +// function_selector("new"), +// vec![], +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// ) +// .result +// .unwrap(); +// +// assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); +// +// let addr = result.account_id; +// +// let function = function_selector("get_filtered"); +// let params = [function].concat(); +// +// let result = Contracts::bare_call( +// ALICE, +// addr.clone(), +// 0, +// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), +// None, +// params, +// DEBUG_OUTPUT, +// pallet_contracts::CollectEvents::Skip, +// pallet_contracts::Determinism::Enforced, +// ); +// +// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { +// log::debug!( +// "Contract debug buffer - {:?}", +// String::from_utf8(result.debug_message.clone()) +// ); +// log::debug!("filtered result: {:?}", result); +// } +// +// // check for revert +// assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); +// }); +// } +// } diff --git a/runtime/devnet/src/extensions/tests/local_fungibles.rs b/runtime/devnet/src/extensions/tests/local_fungibles.rs new file mode 100644 index 00000000..2d752361 --- /dev/null +++ b/runtime/devnet/src/extensions/tests/local_fungibles.rs @@ -0,0 +1,302 @@ +#![cfg(test)] + +use super::*; +use pallet_contracts::debug::ExecResult; + +#[derive(Decode, Encode, Debug, Eq, PartialEq)] +enum FungiblesError { + // AssetsError(Error), + // /// The origin of the call doesn't have the right permission. + // BadOrigin, + // /// Custom error type for cases in which an implementation adds its own restrictions. + // Custom(String), + /// Not enough balance to fulfill a request is available. + InsufficientBalance, + /// Not enough allowance to fulfill a request is available. + InsufficientAllowance, + /// The asset status is not the expected status. + IncorrectStatus, + /// The asset ID is already taken. + InUse, + /// Minimum balance should be non-zero. + MinBalanceZero, + /// The signing account has no permission to do the operation. + NoPermission, + // /// Safe transfer check fails (e.g. if the receiving contract does not accept tokens). + // SafeTransferCheckFailed(String), + /// The given asset ID is unknown. + Unknown, + /// Recipient's address is zero. + ZeroRecipientAddress, + /// Sender's address is zero. + ZeroSenderAddress, + UndefinedError, +} + +const ASSET_ID: u32 = 1; + +fn allowance( + addr: AccountId32, + asset_id: u32, + owner: AccountId32, + spender: AccountId32, +) -> ExecReturnValue { + let function = function_selector("allowance"); + let params = [function, asset_id.encode(), owner.encode(), spender.encode()].concat(); + do_bare_call(addr, params, 0).expect("should work") +} + +// Call balance_of contract message. +fn balance_of(addr: AccountId32, asset_id: u32, owner: AccountId32) -> ExecReturnValue { + let function = function_selector("balance_of"); + let params = [function, asset_id.encode(), owner.encode()].concat(); + do_bare_call(addr, params, 0).expect("should work") +} + +// Call total_supply contract message. +fn total_supply(addr: AccountId32, asset_id: u32) -> ExecReturnValue { + let function = function_selector("total_supply"); + let params = [function, asset_id.encode()].concat(); + do_bare_call(addr, params, 0).expect("should work") +} + +fn asset_exists(addr: AccountId32, asset_id: u32) -> ExecReturnValue { + let function = function_selector("asset_exists"); + let params = [function, asset_id.encode()].concat(); + do_bare_call(addr, params, 0).expect("should work") +} + +fn create( + addr: AccountId32, + asset_id: u32, + admin: AccountId32, + min_balance: u128, +) -> ExecReturnValue { + let function = function_selector("create"); + let params = [function, asset_id.encode(), admin.encode(), min_balance.encode()].concat(); + do_bare_call(addr, params, 0).expect("should work") +} + +fn set_metadata( + addr: AccountId32, + asset_id: u32, + name: Vec, + symbol: Vec, + decimals: u8, +) -> ExecReturnValue { + let function = function_selector("set_metadata"); + let params = + [function, asset_id.encode(), name.encode(), symbol.encode(), decimals.encode()].concat(); + do_bare_call(addr, params, 0).expect("should work") +} + +fn transfer_from( + addr: AccountId32, + asset_id: u32, + _from: Option, + to: Option, + value: u128, + _data: &[u8], +) -> ExecReturnValue { + // let function = function_selector("transfer_from"); + // let params = + // [function, asset_id.encode(), from.encode(), to.encode(), value.encode(), data.encode()] + // .concat(); + // do_bare_call(addr, params, 0) + let function = function_selector("mint"); + let params = [function, asset_id.encode(), to.unwrap().encode(), value.encode()].concat(); + do_bare_call(addr, params, 0).expect("should work") +} + +// Create an asset and mint to owner. +fn create_asset(asset_id: u32, owner: AccountId32) { + assert_eq!( + Assets::create(RuntimeOrigin::signed(owner.clone()), asset_id.into(), owner.into(), 1), + Ok(()) + ); +} + +// Create an asset and mint to owner. +fn create_asset_and_mint_to(asset_id: u32, owner: AccountId32, to: AccountId32, value: u128) { + create_asset(asset_id, owner.clone()); + assert_eq!( + Assets::mint(RuntimeOrigin::signed(owner.into()), asset_id.into(), to.into(), value,), + Ok(()) + ); +} + +// Create an asset, mints to, and approves spender. +fn create_asset_mint_and_approve( + asset_id: u32, + owner: AccountId32, + to: AccountId32, + mint: u128, + spender: AccountId32, + approve: u128, +) { + create_asset_and_mint_to(asset_id, owner.clone(), to.clone(), mint); + assert_eq!( + Assets::approve_transfer( + RuntimeOrigin::signed(to.into()), + asset_id.into(), + spender.into(), + approve, + ), + Ok(()) + ); +} + +#[test] +#[ignore] +fn total_supply_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = + instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE); + + // No tokens in circulation. + assert_eq!( + Assets::total_supply(ASSET_ID).encode(), + total_supply(addr.clone(), ASSET_ID).data[2..] + ); + + // Tokens in circulation. + create_asset_and_mint_to(ASSET_ID, addr.clone(), BOB, 100); + assert_eq!(Assets::total_supply(ASSET_ID).encode(), total_supply(addr, ASSET_ID).data[2..]); + }); +} + +#[test] +#[ignore] +fn balance_of_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = + instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE); + + // No tokens in circulation. + assert_eq!( + Assets::balance(ASSET_ID, BOB).encode(), + balance_of(addr.clone(), ASSET_ID, BOB).data[2..] + ); + + // Tokens in circulation. + create_asset_and_mint_to(ASSET_ID, addr.clone(), BOB, 100); + assert_eq!( + Assets::balance(ASSET_ID, BOB).encode(), + balance_of(addr, ASSET_ID, BOB).data[2..] + ); + }); +} + +#[test] +#[ignore] +fn allowance_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = + instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE); + + // No tokens in circulation. + assert_eq!( + Assets::allowance(ASSET_ID, &BOB, &ALICE).encode(), + allowance(addr.clone(), ASSET_ID, BOB, ALICE).data[2..] + ); + + // Tokens in circulation. + create_asset_mint_and_approve(ASSET_ID, addr.clone(), BOB, 100, ALICE, 50); + assert_eq!( + Assets::allowance(ASSET_ID, &BOB, &ALICE).encode(), + allowance(addr, ASSET_ID, BOB, ALICE).data[2..] + ); + }); +} + +#[test] +#[ignore] +fn asset_exists_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = + instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE); + + // No tokens in circulation. + assert_eq!( + Assets::asset_exists(ASSET_ID).encode(), + asset_exists(addr.clone(), ASSET_ID).data[2..] + ); + + // Tokens in circulation. + create_asset(ASSET_ID, addr.clone()); + assert_eq!(Assets::asset_exists(ASSET_ID).encode(), asset_exists(addr, ASSET_ID).data[2..]); + }); +} + +fn decode_error(result: ExecReturnValue) -> FungiblesError { + FungiblesError::decode(&mut &result.data[2..]).unwrap() +} + +#[test] +#[ignore] +fn create_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", 0); + let new_asset = 2; + + assert_eq!( + decode_error(create(addr.clone(), new_asset, BOB, 1)), + FungiblesError::UndefinedError + ); + // Todo: errors Badorigin, Lookup, reserve(), Callback + // create_asset(ASSET_ID, ALICE); + // // Error `InUse`. + // assert_eq!(decode_error(create(addr.clone(), ASSET_ID, BOB, 1)), FungiblesError::InUse); + // // Error `MinBalanceZero`. + // assert_eq!( + // decode_error(create(addr.clone(), new_asset, BOB, 0)), + // FungiblesError::MinBalanceZero + // ); + // assert!( + // !create(addr.clone(), new_asset, BOB, 1).did_revert(), + // "Contract should have been reverted!" + // ); + }); +} + +#[test] +#[ignore] +fn set_metadata_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = + instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE); + + create_asset(ASSET_ID, addr.clone()); + + let result = set_metadata(addr.clone(), ASSET_ID, vec![12], vec![12], 12); + assert!(!result.did_revert(), "Contract should have been reverted!"); + }); +} + +#[test] +#[ignore] +fn transfer_from_aka_mint_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = + instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE); + + let amount: u128 = 100 * UNIT; + // Create asset with contract as owner. + create_asset(ASSET_ID, addr.clone()); + // Check Bob's asset balance before minting through contract. + let bob_balance_before = Assets::balance(ASSET_ID, &BOB); + + let result = transfer_from(addr.clone(), ASSET_ID, None, Some(BOB), 100 * UNIT, &[0u8]); + assert!(!result.did_revert(), "Contract reverted!"); + + let bob_balance_after = Assets::balance(ASSET_ID, &BOB); + assert_eq!(bob_balance_after, bob_balance_before + amount); + }); +} diff --git a/runtime/devnet/src/extensions/tests/mod.rs b/runtime/devnet/src/extensions/tests/mod.rs new file mode 100644 index 00000000..30d1a924 --- /dev/null +++ b/runtime/devnet/src/extensions/tests/mod.rs @@ -0,0 +1,86 @@ +#![cfg(test)] +use super::*; +use crate::{Assets, Balances, Contracts, Runtime, System}; +use pallet_contracts::{Code, CollectEvents, Determinism, ExecReturnValue}; +use sp_runtime::{traits::Hash, AccountId32, BuildStorage}; + +mod local_fungibles; + +const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug; + +const ALICE: AccountId32 = AccountId32::new([1_u8; 32]); +const BOB: AccountId32 = AccountId32::new([2_u8; 32]); +const INIT_VALUE: u128 = 100_000_000 * UNIT; +const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024); + +fn new_test_ext() -> sp_io::TestExternalities { + let mut t = frame_system::GenesisConfig::::default() + .build_storage() + .expect("Frame system builds valid default genesis config"); + + pallet_balances::GenesisConfig:: { + balances: vec![(ALICE, INIT_VALUE), (BOB, INIT_VALUE)], + } + .assimilate_storage(&mut t) + .expect("Pallet balances storage can be assimilated"); + + let mut ext = sp_io::TestExternalities::new(t); + ext.execute_with(|| System::set_block_number(1)); + ext +} + +fn load_wasm_module(path: &str) -> std::io::Result<(Vec, ::Output)> +where + T: frame_system::Config, +{ + let wasm_binary = std::fs::read(path)?; + let code_hash = T::Hashing::hash(&wasm_binary); + Ok((wasm_binary, code_hash)) +} + +fn function_selector(name: &str) -> Vec { + let hash = sp_io::hashing::blake2_256(name.as_bytes()); + [hash[0..4].to_vec()].concat() +} + +fn do_bare_call( + addr: AccountId32, + input: Vec, + value: u128, +) -> Result { + let result = Contracts::bare_call( + ALICE, + addr.into(), + value.into(), + GAS_LIMIT, + None, + input, + DEBUG_OUTPUT, + CollectEvents::Skip, + Determinism::Enforced, + ); + log::debug!("Contract debug buffer - {:?}", String::from_utf8(result.debug_message.clone())); + log::debug!("result: {:?}", result); + result.result +} + +// Deploy, instantiate and return contract address. +fn instantiate(contract: &str, init_value: u128) -> AccountId32 { + let (wasm_binary, _) = + load_wasm_module::(contract).expect("could not read .wasm file"); + let result = Contracts::bare_instantiate( + ALICE, + init_value, + GAS_LIMIT, + None, + Code::Upload(wasm_binary), + function_selector("new"), + vec![], + DEBUG_OUTPUT, + CollectEvents::Skip, + ) + .result + .unwrap(); + assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); + result.account_id +} diff --git a/runtime/devnet/src/lib.rs b/runtime/devnet/src/lib.rs index 52a0938f..a82be804 100644 --- a/runtime/devnet/src/lib.rs +++ b/runtime/devnet/src/lib.rs @@ -254,40 +254,40 @@ impl Contains for FilteredCalls { pub struct AllowedPopApiCalls; impl Contains for crate::AllowedPopApiCalls { fn contains(c: &RuntimeCall) -> bool { - use assets_config::TrustBackedAssetsCall; + use config::assets::AssetsCall; use pallet_nfts::Call as NftsCall; matches!( c, RuntimeCall::Balances(BalancesCall::transfer_keep_alive { .. }) - | RuntimeCall::TrustBackedAssets( - TrustBackedAssetsCall::create { .. } - | TrustBackedAssetsCall::start_destroy { .. } - | TrustBackedAssetsCall::destroy_accounts { .. } - | TrustBackedAssetsCall::destroy_approvals { .. } - | TrustBackedAssetsCall::finish_destroy { .. } - | TrustBackedAssetsCall::mint { .. } - | TrustBackedAssetsCall::burn { .. } - | TrustBackedAssetsCall::transfer { .. } - | TrustBackedAssetsCall::transfer_keep_alive { .. } - | TrustBackedAssetsCall::force_transfer { .. } - | TrustBackedAssetsCall::freeze { .. } - | TrustBackedAssetsCall::thaw { .. } - | TrustBackedAssetsCall::freeze_asset { .. } - | TrustBackedAssetsCall::thaw_asset { .. } - | TrustBackedAssetsCall::transfer_ownership { .. } - | TrustBackedAssetsCall::set_team { .. } - | TrustBackedAssetsCall::set_metadata { .. } - | TrustBackedAssetsCall::clear_metadata { .. } - | TrustBackedAssetsCall::approve_transfer { .. } - | TrustBackedAssetsCall::cancel_approval { .. } - | TrustBackedAssetsCall::force_cancel_approval { .. } - | TrustBackedAssetsCall::transfer_approved { .. } - | TrustBackedAssetsCall::touch { .. } - | TrustBackedAssetsCall::refund { .. } - | TrustBackedAssetsCall::set_min_balance { .. } - | TrustBackedAssetsCall::touch_other { .. } - | TrustBackedAssetsCall::refund_other { .. } - | TrustBackedAssetsCall::block { .. } + | RuntimeCall::Assets( + AssetsCall::create { .. } + | AssetsCall::start_destroy { .. } + | AssetsCall::destroy_accounts { .. } + | AssetsCall::destroy_approvals { .. } + | AssetsCall::finish_destroy { .. } + | AssetsCall::mint { .. } + | AssetsCall::burn { .. } + | AssetsCall::transfer { .. } + | AssetsCall::transfer_keep_alive { .. } + | AssetsCall::force_transfer { .. } + | AssetsCall::freeze { .. } + | AssetsCall::thaw { .. } + | AssetsCall::freeze_asset { .. } + | AssetsCall::thaw_asset { .. } + | AssetsCall::transfer_ownership { .. } + | AssetsCall::set_team { .. } + | AssetsCall::set_metadata { .. } + | AssetsCall::clear_metadata { .. } + | AssetsCall::approve_transfer { .. } + | AssetsCall::cancel_approval { .. } + | AssetsCall::force_cancel_approval { .. } + | AssetsCall::transfer_approved { .. } + | AssetsCall::touch { .. } + | AssetsCall::refund { .. } + | AssetsCall::set_min_balance { .. } + | AssetsCall::touch_other { .. } + | AssetsCall::refund_other { .. } + | AssetsCall::block { .. } ) | RuntimeCall::Nfts( NftsCall::create { .. } | NftsCall::destroy { .. } @@ -662,7 +662,7 @@ construct_runtime!( // Assets Nfts: pallet_nfts = 50, NftFractionalization: pallet_nft_fractionalization = 51, - TrustBackedAssets: pallet_assets:: = 52, + Assets: pallet_assets:: = 52, } ); diff --git a/runtime/testnet/Cargo.toml b/runtime/testnet/Cargo.toml index d68bfd15..43b1e310 100644 --- a/runtime/testnet/Cargo.toml +++ b/runtime/testnet/Cargo.toml @@ -22,7 +22,7 @@ scale-info.workspace = true smallvec.workspace = true # Local -pop-primitives.workspace = true +pop-primitives = { workspace = true, features = ["testnet"] } pop-runtime-common = { workspace = true, default-features = false } # Substrate diff --git a/runtime/testnet/src/config/assets.rs b/runtime/testnet/src/config/assets.rs index f51f8875..2c8ea952 100644 --- a/runtime/testnet/src/config/assets.rs +++ b/runtime/testnet/src/config/assets.rs @@ -1,6 +1,6 @@ use crate::{ - deposit, AccountId, Balance, Balances, BlockNumber, Nfts, Runtime, RuntimeEvent, - RuntimeHoldReason, TrustBackedAssets, DAYS, EXISTENTIAL_DEPOSIT, UNIT, + deposit, AccountId, Assets, Balance, Balances, BlockNumber, Nfts, Runtime, RuntimeEvent, + RuntimeHoldReason, DAYS, EXISTENTIAL_DEPOSIT, UNIT, }; use frame_support::{ parameter_types, @@ -86,7 +86,7 @@ impl pallet_nft_fractionalization::Config for Runtime { type NftId = ::ItemId; type AssetBalance = >::Balance; type AssetId = >::AssetId; - type Assets = TrustBackedAssets; + type Assets = Assets; type Nfts = Nfts; type PalletId = NftFractionalizationPalletId; type WeightInfo = pallet_nft_fractionalization::weights::SubstrateWeight; @@ -96,7 +96,7 @@ impl pallet_nft_fractionalization::Config for Runtime { } pub type TrustBackedAssetsInstance = pallet_assets::Instance1; -pub(crate) type TrustBackedAssetsCall = pallet_assets::Call; +pub(crate) type AssetsCall = pallet_assets::Call; impl pallet_assets::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Balance = Balance; diff --git a/runtime/testnet/src/config/proxy.rs b/runtime/testnet/src/config/proxy.rs index a4fd479a..07d5f0f8 100644 --- a/runtime/testnet/src/config/proxy.rs +++ b/runtime/testnet/src/config/proxy.rs @@ -1,4 +1,4 @@ -use super::assets::TrustBackedAssetsCall; +use super::assets::AssetsCall; use crate::{Balances, Runtime, RuntimeCall, RuntimeEvent}; use frame_support::traits::InstanceFilter; use pop_runtime_common::proxy::{ @@ -34,16 +34,16 @@ impl InstanceFilter for ProxyType { }, ProxyType::AssetOwner => matches!( c, - RuntimeCall::Assets(TrustBackedAssetsCall::create { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::start_destroy { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::destroy_accounts { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::destroy_approvals { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::finish_destroy { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::transfer_ownership { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::set_team { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::set_metadata { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::clear_metadata { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::set_min_balance { .. }) + RuntimeCall::Assets(AssetsCall::create { .. }) + | RuntimeCall::Assets(AssetsCall::start_destroy { .. }) + | RuntimeCall::Assets(AssetsCall::destroy_accounts { .. }) + | RuntimeCall::Assets(AssetsCall::destroy_approvals { .. }) + | RuntimeCall::Assets(AssetsCall::finish_destroy { .. }) + | RuntimeCall::Assets(AssetsCall::transfer_ownership { .. }) + | RuntimeCall::Assets(AssetsCall::set_team { .. }) + | RuntimeCall::Assets(AssetsCall::set_metadata { .. }) + | RuntimeCall::Assets(AssetsCall::clear_metadata { .. }) + | RuntimeCall::Assets(AssetsCall::set_min_balance { .. }) | RuntimeCall::Nfts(pallet_nfts::Call::create { .. }) | RuntimeCall::Nfts(pallet_nfts::Call::destroy { .. }) | RuntimeCall::Nfts(pallet_nfts::Call::redeposit { .. }) @@ -56,15 +56,15 @@ impl InstanceFilter for ProxyType { ), ProxyType::AssetManager => matches!( c, - RuntimeCall::Assets(TrustBackedAssetsCall::mint { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::burn { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::freeze { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::block { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::thaw { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::freeze_asset { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::thaw_asset { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::touch_other { .. }) - | RuntimeCall::Assets(TrustBackedAssetsCall::refund_other { .. }) + RuntimeCall::Assets(AssetsCall::mint { .. }) + | RuntimeCall::Assets(AssetsCall::burn { .. }) + | RuntimeCall::Assets(AssetsCall::freeze { .. }) + | RuntimeCall::Assets(AssetsCall::block { .. }) + | RuntimeCall::Assets(AssetsCall::thaw { .. }) + | RuntimeCall::Assets(AssetsCall::freeze_asset { .. }) + | RuntimeCall::Assets(AssetsCall::thaw_asset { .. }) + | RuntimeCall::Assets(AssetsCall::touch_other { .. }) + | RuntimeCall::Assets(AssetsCall::refund_other { .. }) | RuntimeCall::Nfts(pallet_nfts::Call::force_mint { .. }) | RuntimeCall::Nfts(pallet_nfts::Call::update_mint_settings { .. }) | RuntimeCall::Nfts(pallet_nfts::Call::mint_pre_signed { .. }) diff --git a/runtime/testnet/src/extensions.rs b/runtime/testnet/src/extensions.rs index 0d552090..6bbfaa36 100644 --- a/runtime/testnet/src/extensions.rs +++ b/runtime/testnet/src/extensions.rs @@ -3,25 +3,23 @@ use frame_support::traits::{Contains, OriginTrait}; use frame_support::{ dispatch::{GetDispatchInfo, RawOrigin}, pallet_prelude::*, - traits::{fungibles::Inspect, nonfungibles_v2::Inspect as NonFungiblesInspect}, + traits::nonfungibles_v2::Inspect as NonFungiblesInspect, }; use pallet_contracts::chain_extension::{ BufInBufOutState, ChainExtension, ChargedAmount, Environment, Ext, InitState, RetVal, }; use pop_primitives::{ - storage_keys::{NftsKeys, ParachainSystemKeys, RuntimeStateKeys, TrustBackedAssetsKeys}, - AssetId, CollectionId, ItemId, + storage_keys::{NftsKeys, ParachainSystemKeys, RuntimeStateKeys}, + CollectionId, ItemId, }; use sp_core::crypto::UncheckedFrom; use sp_runtime::{ traits::{BlockNumberProvider, Dispatchable}, DispatchError, }; +use sp_std::vec::Vec; -use crate::{ - assets_config::TrustBackedAssetsInstance, AccountId, AllowedPopApiCalls, RuntimeCall, - RuntimeOrigin, -}; +use crate::{AccountId, AllowedPopApiCalls, RuntimeCall, RuntimeOrigin}; const LOG_TARGET: &str = "pop-api::extension"; @@ -34,7 +32,6 @@ impl ChainExtension for PopApiExtension where T: pallet_contracts::Config + pallet_xcm::Config - + pallet_assets::Config + pallet_nfts::Config + cumulus_pallet_parachain_system::Config + frame_system::Config< @@ -184,7 +181,6 @@ where fn read_state(env: Environment) -> Result<(), DispatchError> where T: pallet_contracts::Config - + pallet_assets::Config + pallet_nfts::Config + cumulus_pallet_parachain_system::Config + frame_system::Config, @@ -210,9 +206,6 @@ where RuntimeStateKeys::ParachainSystem(key) => { read_parachain_system_state::(key, &mut env) }, - RuntimeStateKeys::TrustBackedAssets(key) => { - read_trust_backed_assets_state::(key, &mut env) - }, }? .encode(); @@ -288,23 +281,6 @@ where } } -fn read_trust_backed_assets_state( - key: TrustBackedAssetsKeys, - env: &mut Environment, -) -> Result, DispatchError> -where - T: pallet_contracts::Config - + pallet_assets::Config, - E: Ext, -{ - match key { - TrustBackedAssetsKeys::AssetExists(id) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_assets::Pallet::::asset_exists(id).encode()) - }, - } -} - #[cfg(test)] mod tests { pub use super::*; @@ -701,110 +677,6 @@ mod tests { }); } - #[test] - #[ignore] - fn dispatch_trust_backed_assets_mint_from_contract_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = load_wasm_module::( - "../../pop-api/examples/trust_backed_assets/target/ink/pop_api_trust_backed_assets_example.wasm", - ) - .unwrap(); - - let init_value = 100; - - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - let addr = result.account_id; - - let asset_id: u32 = 1; - let min_balance = 1; - let amount: u128 = 100 * UNIT; - let function = function_selector("mint_asset_through_runtime"); - let params = [function, asset_id.encode(), BOB.encode(), amount.encode()].concat(); - - // Mint asset which does not exist. - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - GAS_LIMIT, - None, - params.clone(), - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // Check for revert. - assert!(result.result.unwrap().did_revert(), "Contract should have been reverted!"); - - // Create asset with contract as owner. - assert_eq!( - TrustBackedAssets::force_create( - RuntimeOrigin::root(), - asset_id.into(), - addr.clone().into(), - true, - min_balance, - ), - Ok(()) - ); - - // Check Bob's asset balance before minting through contract. - let bob_balance_before = TrustBackedAssets::balance(asset_id, &BOB); - assert_eq!(bob_balance_before, 0); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - GAS_LIMIT, - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // Check for revert - assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); - - let bob_balance_after = TrustBackedAssets::balance(asset_id, &BOB); - assert_eq!(bob_balance_after, bob_balance_before + amount); - }); - } - #[test] #[ignore] fn allow_call_filter_blocks_call() { diff --git a/runtime/testnet/src/lib.rs b/runtime/testnet/src/lib.rs index 312d9b50..66a5092c 100644 --- a/runtime/testnet/src/lib.rs +++ b/runtime/testnet/src/lib.rs @@ -253,74 +253,43 @@ impl Contains for FilteredCalls { pub struct AllowedPopApiCalls; impl Contains for crate::AllowedPopApiCalls { fn contains(c: &RuntimeCall) -> bool { - use assets_config::TrustBackedAssetsCall; use pallet_nfts::Call as NftsCall; matches!( c, RuntimeCall::Balances(BalancesCall::transfer_keep_alive { .. }) - | RuntimeCall::TrustBackedAssets( - TrustBackedAssetsCall::create { .. } - | TrustBackedAssetsCall::start_destroy { .. } - | TrustBackedAssetsCall::destroy_accounts { .. } - | TrustBackedAssetsCall::destroy_approvals { .. } - | TrustBackedAssetsCall::finish_destroy { .. } - | TrustBackedAssetsCall::mint { .. } - | TrustBackedAssetsCall::burn { .. } - | TrustBackedAssetsCall::transfer { .. } - | TrustBackedAssetsCall::transfer_keep_alive { .. } - | TrustBackedAssetsCall::force_transfer { .. } - | TrustBackedAssetsCall::freeze { .. } - | TrustBackedAssetsCall::thaw { .. } - | TrustBackedAssetsCall::freeze_asset { .. } - | TrustBackedAssetsCall::thaw_asset { .. } - | TrustBackedAssetsCall::transfer_ownership { .. } - | TrustBackedAssetsCall::set_team { .. } - | TrustBackedAssetsCall::set_metadata { .. } - | TrustBackedAssetsCall::clear_metadata { .. } - | TrustBackedAssetsCall::approve_transfer { .. } - | TrustBackedAssetsCall::cancel_approval { .. } - | TrustBackedAssetsCall::force_cancel_approval { .. } - | TrustBackedAssetsCall::transfer_approved { .. } - | TrustBackedAssetsCall::touch { .. } - | TrustBackedAssetsCall::refund { .. } - | TrustBackedAssetsCall::set_min_balance { .. } - | TrustBackedAssetsCall::touch_other { .. } - | TrustBackedAssetsCall::refund_other { .. } - | TrustBackedAssetsCall::block { .. } - ) | RuntimeCall::Nfts( - NftsCall::create { .. } - | NftsCall::destroy { .. } - | NftsCall::mint { .. } - | NftsCall::burn { .. } - | NftsCall::transfer { .. } - | NftsCall::redeposit { .. } - | NftsCall::lock_item_transfer { .. } - | NftsCall::unlock_item_transfer { .. } - | NftsCall::lock_collection { .. } - | NftsCall::transfer_ownership { .. } - | NftsCall::set_team { .. } - | NftsCall::approve_transfer { .. } - | NftsCall::cancel_approval { .. } - | NftsCall::clear_all_transfer_approvals { .. } - | NftsCall::lock_item_properties { .. } - | NftsCall::set_attribute { .. } - | NftsCall::clear_attribute { .. } - | NftsCall::approve_item_attributes { .. } - | NftsCall::cancel_item_attributes_approval { .. } - | NftsCall::set_metadata { .. } - | NftsCall::clear_metadata { .. } - | NftsCall::set_collection_metadata { .. } - | NftsCall::clear_collection_metadata { .. } - | NftsCall::set_accept_ownership { .. } - | NftsCall::set_collection_max_supply { .. } - | NftsCall::update_mint_settings { .. } - | NftsCall::set_price { .. } - | NftsCall::buy_item { .. } - | NftsCall::pay_tips { .. } - | NftsCall::create_swap { .. } - | NftsCall::cancel_swap { .. } - | NftsCall::claim_swap { .. } - ) + | RuntimeCall::Nfts( + NftsCall::create { .. } + | NftsCall::destroy { .. } + | NftsCall::mint { .. } | NftsCall::burn { .. } + | NftsCall::transfer { .. } + | NftsCall::redeposit { .. } + | NftsCall::lock_item_transfer { .. } + | NftsCall::unlock_item_transfer { .. } + | NftsCall::lock_collection { .. } + | NftsCall::transfer_ownership { .. } + | NftsCall::set_team { .. } + | NftsCall::approve_transfer { .. } + | NftsCall::cancel_approval { .. } + | NftsCall::clear_all_transfer_approvals { .. } + | NftsCall::lock_item_properties { .. } + | NftsCall::set_attribute { .. } + | NftsCall::clear_attribute { .. } + | NftsCall::approve_item_attributes { .. } + | NftsCall::cancel_item_attributes_approval { .. } + | NftsCall::set_metadata { .. } + | NftsCall::clear_metadata { .. } + | NftsCall::set_collection_metadata { .. } + | NftsCall::clear_collection_metadata { .. } + | NftsCall::set_accept_ownership { .. } + | NftsCall::set_collection_max_supply { .. } + | NftsCall::update_mint_settings { .. } + | NftsCall::set_price { .. } + | NftsCall::buy_item { .. } + | NftsCall::pay_tips { .. } + | NftsCall::create_swap { .. } + | NftsCall::cancel_swap { .. } + | NftsCall::claim_swap { .. } + ) ) } } @@ -661,7 +630,7 @@ construct_runtime!( // Assets Nfts: pallet_nfts = 50, NftFractionalization: pallet_nft_fractionalization = 51, - TrustBackedAssets: pallet_assets:: = 52, + Assets: pallet_assets:: = 52, } ); From fd46a279073abd855c327df6de32b2e155a74840 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Tue, 4 Jun 2024 15:49:03 +0200 Subject: [PATCH 04/76] fix: add error handling other than ModuleError --- pop-api/examples/fungibles/lib.rs | 36 +-- pop-api/src/lib.rs | 53 +++- pop-api/src/v0/assets/fungibles.rs | 60 +++- pop-api/src/v0/balances.rs | 6 +- pop-api/src/v0/contracts.rs | 286 +++++++++--------- pop-api/src/v0/cross_chain/mod.rs | 4 +- pop-api/src/v0/dispatch_error.rs | 57 ++++ pop-api/src/v0/mod.rs | 3 +- pop-api/src/v0/nfts.rs | 4 +- runtime/devnet/src/extensions/mod.rs | 11 +- .../src/extensions/tests/local_fungibles.rs | 214 ++++++++----- runtime/devnet/src/extensions/tests/mod.rs | 10 +- 12 files changed, 462 insertions(+), 282 deletions(-) create mode 100644 pop-api/src/v0/dispatch_error.rs diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs index 9c0ae754..e44c4347 100755 --- a/pop-api/examples/fungibles/lib.rs +++ b/pop-api/examples/fungibles/lib.rs @@ -10,42 +10,42 @@ use pop_api::{ #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum ContractError { - // AssetsError(Error), - // /// The origin of the call doesn't have the right permission. - // BadOrigin, - // /// Custom error type for cases in which an implementation adds its own restrictions. - // Custom(String), - /// Not enough balance to fulfill a request is available. - InsufficientBalance, + /// The amount to mint is less than the existential deposit. + BelowMinimum, + /// Unspecified dispatch error, providing the index and its error index (if none `0`). + DispatchError { index: u8, error: u8 }, /// Not enough allowance to fulfill a request is available. InsufficientAllowance, - /// The asset status is not the expected status. - IncorrectStatus, + /// Not enough balance to fulfill a request is available. + InsufficientBalance, /// The asset ID is already taken. InUse, /// Minimum balance should be non-zero. MinBalanceZero, + /// Unspecified pallet error, providing pallet index and error index. + ModuleError { pallet: u8, error: u16 }, /// The signing account has no permission to do the operation. NoPermission, - // /// Safe transfer check fails (e.g. if the receiving contract does not accept tokens). - // SafeTransferCheckFailed(String), /// The given asset ID is unknown. Unknown, - /// Recipient's address is zero. - ZeroRecipientAddress, - /// Sender's address is zero. - ZeroSenderAddress, - UndefinedError, } impl From for ContractError { fn from(error: FungiblesError) -> Self { match error { - // Error::BalanceLow => Err(InsufficientBalance), + FungiblesError::BelowMinimum => ContractError::BelowMinimum, + FungiblesError::DispatchError { index, error } => { + ContractError::DispatchError { index, error } + }, + FungiblesError::InsufficientAllowance => ContractError::InsufficientAllowance, + FungiblesError::InsufficientBalance => ContractError::InsufficientBalance, FungiblesError::InUse => ContractError::InUse, FungiblesError::MinBalanceZero => ContractError::MinBalanceZero, + FungiblesError::ModuleError { pallet, error } => { + ContractError::ModuleError { pallet, error } + }, + FungiblesError::NoPermission => ContractError::NoPermission, FungiblesError::Unknown => ContractError::Unknown, - _ => ContractError::UndefinedError, } } } diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 0ca4a817..014b5a29 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -1,14 +1,16 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub mod primitives; -pub mod v0; - -use crate::PopApiError::{Assets, Balances, Contracts, Nfts, UnknownStatusCode}; +use core::convert::TryInto; use ink::{prelude::vec::Vec, ChainExtensionInstance}; use primitives::{cross_chain::*, storage_keys::*, AccountId as AccountId32}; pub use sp_runtime::{BoundedVec, MultiAddress, MultiSignature}; use v0::RuntimeCall; -pub use v0::{assets, balances, contracts, cross_chain, nfts, relay_chain_block_number, state}; +pub use v0::{ + assets, balances, contracts, cross_chain, dispatch_error, nfts, relay_chain_block_number, state, +}; + +pub mod primitives; +pub mod v0; // type AccountId = ::AccountId; type AccountId = AccountId32; @@ -19,30 +21,49 @@ type MaxTips = u32; pub type Result = core::result::Result; +struct ModuleError { + pallet: u8, + error: u16, +} + #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum PopApiError { - UnknownStatusCode(u32), - DecodingFailed, - SystemCallFiltered, + Assets(assets::fungibles::AssetsError), Balances(balances::Error), Contracts(contracts::Error), + DecodingFailed, Nfts(nfts::Error), - Assets(assets::fungibles::AssetsError), + SystemCallFiltered, + TokenError(dispatch_error::TokenError), + UnknownModuleStatusCode(u32), + UnknownDispatchStatusCode(u32), Xcm(cross_chain::Error), } impl ink::env::chain_extension::FromStatusCode for PopApiError { fn from_status_code(status_code: u32) -> core::result::Result<(), Self> { + use crate::PopApiError::{ + Assets, Balances, Contracts, Nfts, TokenError, UnknownDispatchStatusCode, + UnknownModuleStatusCode, + }; + match status_code { 0 => Ok(()), - // CallFiltered originates from `frame_system` with pallet-index 0. The CallFiltered error is at index 5 - 5 => Err(PopApiError::SystemCallFiltered), - 10_000..=10_999 => Err(Balances((status_code - 10_000).try_into()?)), - 40_000..=40_999 => Err(Contracts((status_code - 40_000).try_into()?)), - 50_000..=50_999 => Err(Nfts((status_code - 50_000).try_into()?)), - 52_000..=52_999 => Err(Assets((status_code - 52_000).try_into()?)), - _ => Err(UnknownStatusCode(status_code)), + 3_000_000..=3_999_999 => { + let status_code = status_code - 3_000_000; + match status_code { + // CallFiltered originates from `frame_system` with pallet-index 0. The CallFiltered error is at index 5 + 5 => Err(PopApiError::SystemCallFiltered), + 10_000..=10_999 => Err(Balances((status_code - 10_000).try_into()?)), + 40_000..=40_999 => Err(Contracts((status_code - 40_000).try_into()?)), + 50_000..=50_999 => Err(Nfts((status_code - 50_000).try_into()?)), + 52_000..=52_999 => Err(Assets((status_code - 52_000).try_into()?)), + _ => Err(UnknownModuleStatusCode(status_code)), + } + }, + 7_000_000..=7_999_999 => Err(TokenError((status_code - 7_000_000).try_into()?)), + _ => Err(UnknownDispatchStatusCode(status_code)), } } } diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index f2d87a0e..de624938 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -1,4 +1,8 @@ -use crate::{AccountId, Balance, PopApiError::UnknownStatusCode, RuntimeCall, *}; +use crate::{ + AccountId, Balance, + PopApiError::{UnknownDispatchStatusCode, UnknownModuleStatusCode}, + RuntimeCall, *, +}; use ink::prelude::vec::Vec; use primitives::AssetId; use scale::{Compact, Encode}; @@ -502,7 +506,7 @@ impl TryFrom for AssetsError { 16 => Ok(AssetNotLive), 17 => Ok(IncorrectStatus), 18 => Ok(NotFrozen), - _ => Err(UnknownStatusCode(status_code)), + _ => Err(UnknownModuleStatusCode(status_code)), } } } @@ -510,22 +514,41 @@ impl TryFrom for AssetsError { #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum FungiblesError { - /// The signing account has no permission to do the operation. - NoPermission, - /// The given asset ID is unknown. - Unknown, + /// The amount to mint is less than the existential deposit. + BelowMinimum, + /// Unspecified dispatch error, providing the index and its error index (if none `0`). + DispatchError { index: u8, error: u8 }, + /// Not enough allowance to fulfill a request is available. + InsufficientAllowance, + /// Not enough balance to fulfill a request is available. InsufficientBalance, /// The asset ID is already taken. InUse, /// Minimum balance should be non-zero. MinBalanceZero, + /// Unspecified pallet error, providing pallet index and error index. + ModuleError { pallet: u8, error: u16 }, + /// The signing account has no permission to do the operation. + NoPermission, + /// The given asset ID is unknown. + Unknown, } impl From for FungiblesError { fn from(error: balances::Error) -> Self { match error { balances::Error::InsufficientBalance => FungiblesError::InsufficientBalance, - _ => panic!("Unexpected pallet assets error. This error is unknown to pallet assets"), + _ => FungiblesError::ModuleError { pallet: 40, error: error as u16 }, + } + } +} + +impl From for FungiblesError { + fn from(error: dispatch_error::TokenError) -> Self { + match error { + dispatch_error::TokenError::UnknownAsset => FungiblesError::Unknown, + dispatch_error::TokenError::BelowMinimum => FungiblesError::BelowMinimum, + _ => FungiblesError::DispatchError { index: 7, error: error as u8 }, } } } @@ -533,8 +556,12 @@ impl From for FungiblesError { impl From for FungiblesError { fn from(error: AssetsError) -> Self { match error { + AssetsError::Unapproved => FungiblesError::InsufficientAllowance, AssetsError::InUse => FungiblesError::InUse, - _ => panic!("Unexpected pallet assets error. This error is unknown to pallet assets"), + AssetsError::MinBalanceZero => FungiblesError::MinBalanceZero, + AssetsError::NoPermission => FungiblesError::NoPermission, + AssetsError::Unknown => FungiblesError::Unknown, + _ => FungiblesError::ModuleError { pallet: 40, error: error as u16 }, } } } @@ -543,12 +570,19 @@ impl From for FungiblesError { fn from(error: PopApiError) -> Self { match error { PopApiError::Assets(e) => e.into(), - // PopApiError::Balances(e) => todo!("balances: {:?}", e), PopApiError::Balances(e) => e.into(), - // PopApiError::Contracts(_e) => todo!("contracts"), - // PopApiError::SystemCallFiltered => 100, - // PopApiError::UnknownStatusCode(u) => u, - _ => panic!("Unexpected pallet assets error. This error is unknown to pallet assets"), + PopApiError::TokenError(e) => e.into(), + PopApiError::UnknownModuleStatusCode(e) => { + let pallet = (e / 1_000) as u8; + let error = (e % 1_000) as u16; + FungiblesError::ModuleError { pallet, error } + }, + PopApiError::UnknownDispatchStatusCode(e) => { + let index = (e / 1_000_000) as u8; + let error = (3 % 1_000_000) as u8; + FungiblesError::DispatchError { index, error } + }, + _ => todo!(), } } } diff --git a/pop-api/src/v0/balances.rs b/pop-api/src/v0/balances.rs index bc48711e..bc422ef4 100644 --- a/pop-api/src/v0/balances.rs +++ b/pop-api/src/v0/balances.rs @@ -1,6 +1,6 @@ use crate::{ dispatch, primitives::MultiAddress, v0::RuntimeCall, AccountId, PopApiError, - PopApiError::UnknownStatusCode, + PopApiError::UnknownModuleStatusCode, }; type Result = core::result::Result; @@ -73,7 +73,7 @@ impl TryFrom for Error { 9 => Ok(TooManyFreezes), 10 => Ok(IssuanceDeactivated), 11 => Ok(DeltaZero), - _ => Err(UnknownStatusCode(status_code)), + _ => Err(UnknownModuleStatusCode(status_code)), } } } @@ -82,7 +82,7 @@ impl From for Error { fn from(error: PopApiError) -> Self { match error { PopApiError::Balances(e) => e, - _ => panic!("expected balances error"), + _ => todo!(), } } } diff --git a/pop-api/src/v0/contracts.rs b/pop-api/src/v0/contracts.rs index d7a1a5dd..5d4a3692 100644 --- a/pop-api/src/v0/contracts.rs +++ b/pop-api/src/v0/contracts.rs @@ -1,156 +1,152 @@ -use crate::{ - PopApiError, - PopApiError::UnknownStatusCode, -}; +use crate::{PopApiError, PopApiError::UnknownModuleStatusCode}; #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum Error { - /// Invalid schedule supplied, e.g. with zero weight of a basic operation. - InvalidSchedule, - /// Invalid combination of flags supplied to `seal_call` or `seal_delegate_call`. - InvalidCallFlags, - /// The executed contract exhausted its gas limit. - OutOfGas, - /// The output buffer supplied to a contract API call was too small. - OutputBufferTooSmall, - /// Performing the requested transfer failed. Probably because there isn't enough - /// free balance in the sender's account. - TransferFailed, - /// Performing a call was denied because the calling depth reached the limit - /// of what is specified in the schedule. - MaxCallDepthReached, - /// No contract was found at the specified address. - ContractNotFound, - /// The code supplied to `instantiate_with_code` exceeds the limit specified in the - /// current schedule. - CodeTooLarge, - /// No code could be found at the supplied code hash. - CodeNotFound, - /// No code info could be found at the supplied code hash. - CodeInfoNotFound, - /// A buffer outside of sandbox memory was passed to a contract API function. - OutOfBounds, - /// Input passed to a contract API function failed to decode as expected type. - DecodingFailed, - /// Contract trapped during execution. - ContractTrapped, - /// The size defined in `T::MaxValueSize` was exceeded. - ValueTooLarge, - /// Termination of a contract is not allowed while the contract is already - /// on the call stack. Can be triggered by `seal_terminate`. - TerminatedWhileReentrant, - /// `seal_call` forwarded this contracts input. It therefore is no longer available. - InputForwarded, - /// The subject passed to `seal_random` exceeds the limit. - RandomSubjectTooLong, - /// The amount of topics passed to `seal_deposit_events` exceeds the limit. - TooManyTopics, - /// The chain does not provide a chain extension. Calling the chain extension results - /// in this error. Note that this usually shouldn't happen as deploying such contracts - /// is rejected. - NoChainExtension, - /// Failed to decode the XCM program. - XCMDecodeFailed, - /// A contract with the same AccountId already exists. - DuplicateContract, - /// A contract self destructed in its constructor. - /// - /// This can be triggered by a call to `seal_terminate`. - TerminatedInConstructor, - /// A call tried to invoke a contract that is flagged as non-reentrant. - /// The only other cause is that a call from a contract into the runtime tried to call back - /// into `pallet-contracts`. This would make the whole pallet reentrant with regard to - /// contract code execution which is not supported. - ReentranceDenied, - /// Origin doesn't have enough balance to pay the required storage deposits. - StorageDepositNotEnoughFunds, - /// More storage was created than allowed by the storage deposit limit. - StorageDepositLimitExhausted, - /// Code removal was denied because the code is still in use by at least one contract. - CodeInUse, - /// The contract ran to completion but decided to revert its storage changes. - /// Please note that this error is only returned from extrinsics. When called directly - /// or via RPC an `Ok` will be returned. In this case the caller needs to inspect the flags - /// to determine whether a reversion has taken place. - ContractReverted, - /// The contract's code was found to be invalid during validation. - /// - /// The most likely cause of this is that an API was used which is not supported by the - /// node. This happens if an older node is used with a new version of ink!. Try updating - /// your node to the newest available version. - /// - /// A more detailed error can be found on the node console if debug messages are enabled - /// by supplying `-lruntime::contracts=debug`. - CodeRejected, - /// An indeterministic code was used in a context where this is not permitted. - Indeterministic, - /// A pending migration needs to complete before the extrinsic can be called. - MigrationInProgress, - /// Migrate dispatch call was attempted but no migration was performed. - NoMigrationPerformed, - /// The contract has reached its maximum number of delegate dependencies. - MaxDelegateDependenciesReached, - /// The dependency was not found in the contract's delegate dependencies. - DelegateDependencyNotFound, - /// The contract already depends on the given delegate dependency. - DelegateDependencyAlreadyExists, - /// Can not add a delegate dependency to the code hash of the contract itself. - CannotAddSelfAsDelegateDependency, + /// Invalid schedule supplied, e.g. with zero weight of a basic operation. + InvalidSchedule, + /// Invalid combination of flags supplied to `seal_call` or `seal_delegate_call`. + InvalidCallFlags, + /// The executed contract exhausted its gas limit. + OutOfGas, + /// The output buffer supplied to a contract API call was too small. + OutputBufferTooSmall, + /// Performing the requested transfer failed. Probably because there isn't enough + /// free balance in the sender's account. + TransferFailed, + /// Performing a call was denied because the calling depth reached the limit + /// of what is specified in the schedule. + MaxCallDepthReached, + /// No contract was found at the specified address. + ContractNotFound, + /// The code supplied to `instantiate_with_code` exceeds the limit specified in the + /// current schedule. + CodeTooLarge, + /// No code could be found at the supplied code hash. + CodeNotFound, + /// No code info could be found at the supplied code hash. + CodeInfoNotFound, + /// A buffer outside of sandbox memory was passed to a contract API function. + OutOfBounds, + /// Input passed to a contract API function failed to decode as expected type. + DecodingFailed, + /// Contract trapped during execution. + ContractTrapped, + /// The size defined in `T::MaxValueSize` was exceeded. + ValueTooLarge, + /// Termination of a contract is not allowed while the contract is already + /// on the call stack. Can be triggered by `seal_terminate`. + TerminatedWhileReentrant, + /// `seal_call` forwarded this contracts input. It therefore is no longer available. + InputForwarded, + /// The subject passed to `seal_random` exceeds the limit. + RandomSubjectTooLong, + /// The amount of topics passed to `seal_deposit_events` exceeds the limit. + TooManyTopics, + /// The chain does not provide a chain extension. Calling the chain extension results + /// in this error. Note that this usually shouldn't happen as deploying such contracts + /// is rejected. + NoChainExtension, + /// Failed to decode the XCM program. + XCMDecodeFailed, + /// A contract with the same AccountId already exists. + DuplicateContract, + /// A contract self destructed in its constructor. + /// + /// This can be triggered by a call to `seal_terminate`. + TerminatedInConstructor, + /// A call tried to invoke a contract that is flagged as non-reentrant. + /// The only other cause is that a call from a contract into the runtime tried to call back + /// into `pallet-contracts`. This would make the whole pallet reentrant with regard to + /// contract code execution which is not supported. + ReentranceDenied, + /// Origin doesn't have enough balance to pay the required storage deposits. + StorageDepositNotEnoughFunds, + /// More storage was created than allowed by the storage deposit limit. + StorageDepositLimitExhausted, + /// Code removal was denied because the code is still in use by at least one contract. + CodeInUse, + /// The contract ran to completion but decided to revert its storage changes. + /// Please note that this error is only returned from extrinsics. When called directly + /// or via RPC an `Ok` will be returned. In this case the caller needs to inspect the flags + /// to determine whether a reversion has taken place. + ContractReverted, + /// The contract's code was found to be invalid during validation. + /// + /// The most likely cause of this is that an API was used which is not supported by the + /// node. This happens if an older node is used with a new version of ink!. Try updating + /// your node to the newest available version. + /// + /// A more detailed error can be found on the node console if debug messages are enabled + /// by supplying `-lruntime::contracts=debug`. + CodeRejected, + /// An indeterministic code was used in a context where this is not permitted. + Indeterministic, + /// A pending migration needs to complete before the extrinsic can be called. + MigrationInProgress, + /// Migrate dispatch call was attempted but no migration was performed. + NoMigrationPerformed, + /// The contract has reached its maximum number of delegate dependencies. + MaxDelegateDependenciesReached, + /// The dependency was not found in the contract's delegate dependencies. + DelegateDependencyNotFound, + /// The contract already depends on the given delegate dependency. + DelegateDependencyAlreadyExists, + /// Can not add a delegate dependency to the code hash of the contract itself. + CannotAddSelfAsDelegateDependency, } - impl TryFrom for Error { - type Error = PopApiError; + type Error = PopApiError; - fn try_from(status_code: u32) -> core::result::Result { - use Error::*; - match status_code { - 0 => Ok(InvalidSchedule), - 1 => Ok(InvalidCallFlags), - 2 => Ok(OutOfGas), - 3 => Ok(OutputBufferTooSmall), - 4 => Ok(TransferFailed), - 5 => Ok(MaxCallDepthReached), - 6 => Ok(ContractNotFound), - 7 => Ok(CodeTooLarge), - 8 => Ok(CodeNotFound), - 9 => Ok(CodeInfoNotFound), - 10 => Ok(OutOfBounds), - 11 => Ok(DecodingFailed), - 12 => Ok(ContractTrapped), - 13 => Ok(ValueTooLarge), - 14 => Ok(TerminatedWhileReentrant), - 15 => Ok(InputForwarded), - 16 => Ok(RandomSubjectTooLong), - 17 => Ok(TooManyTopics), - 18 => Ok(NoChainExtension), - 19 => Ok(XCMDecodeFailed), - 20 => Ok(DuplicateContract), - 21 => Ok(TerminatedInConstructor), - 22 => Ok(ReentranceDenied), - 23 => Ok(StorageDepositNotEnoughFunds), - 24 => Ok(StorageDepositLimitExhausted), - 25 => Ok(CodeInUse), - 26 => Ok(ContractReverted), - 27 => Ok(CodeRejected), - 28 => Ok(Indeterministic), - 29 => Ok(MigrationInProgress), - 30 => Ok(NoMigrationPerformed), - 31 => Ok(MaxDelegateDependenciesReached), - 32 => Ok(DelegateDependencyNotFound), - 33 => Ok(DelegateDependencyAlreadyExists), - 34 => Ok(CannotAddSelfAsDelegateDependency), - _ => Err(UnknownStatusCode(status_code)), - } - } + fn try_from(status_code: u32) -> core::result::Result { + use Error::*; + match status_code { + 0 => Ok(InvalidSchedule), + 1 => Ok(InvalidCallFlags), + 2 => Ok(OutOfGas), + 3 => Ok(OutputBufferTooSmall), + 4 => Ok(TransferFailed), + 5 => Ok(MaxCallDepthReached), + 6 => Ok(ContractNotFound), + 7 => Ok(CodeTooLarge), + 8 => Ok(CodeNotFound), + 9 => Ok(CodeInfoNotFound), + 10 => Ok(OutOfBounds), + 11 => Ok(DecodingFailed), + 12 => Ok(ContractTrapped), + 13 => Ok(ValueTooLarge), + 14 => Ok(TerminatedWhileReentrant), + 15 => Ok(InputForwarded), + 16 => Ok(RandomSubjectTooLong), + 17 => Ok(TooManyTopics), + 18 => Ok(NoChainExtension), + 19 => Ok(XCMDecodeFailed), + 20 => Ok(DuplicateContract), + 21 => Ok(TerminatedInConstructor), + 22 => Ok(ReentranceDenied), + 23 => Ok(StorageDepositNotEnoughFunds), + 24 => Ok(StorageDepositLimitExhausted), + 25 => Ok(CodeInUse), + 26 => Ok(ContractReverted), + 27 => Ok(CodeRejected), + 28 => Ok(Indeterministic), + 29 => Ok(MigrationInProgress), + 30 => Ok(NoMigrationPerformed), + 31 => Ok(MaxDelegateDependenciesReached), + 32 => Ok(DelegateDependencyNotFound), + 33 => Ok(DelegateDependencyAlreadyExists), + 34 => Ok(CannotAddSelfAsDelegateDependency), + _ => Err(UnknownModuleStatusCode(status_code)), + } + } } impl From for Error { - fn from(error: PopApiError) -> Self { - match error { - PopApiError::Contracts(e) => e, - _ => panic!("expected balances error"), - } - } -} \ No newline at end of file + fn from(error: PopApiError) -> Self { + match error { + PopApiError::Contracts(e) => e, + _ => panic!("expected balances error"), + } + } +} diff --git a/pop-api/src/v0/cross_chain/mod.rs b/pop-api/src/v0/cross_chain/mod.rs index 6732c119..1e5afbf5 100644 --- a/pop-api/src/v0/cross_chain/mod.rs +++ b/pop-api/src/v0/cross_chain/mod.rs @@ -1,6 +1,6 @@ pub mod coretime; -use crate::{PopApiError::UnknownStatusCode, *}; +use crate::{PopApiError::UnknownModuleStatusCode, *}; type Result = core::result::Result; @@ -92,7 +92,7 @@ impl TryFrom for Error { 21 => Ok(InvalidAssetUnknownReserve), 22 => Ok(InvalidAssetUnsupportedReserve), 23 => Ok(TooManyReserves), - _ => Err(UnknownStatusCode(status_code)), + _ => Err(UnknownModuleStatusCode(status_code)), } } } diff --git a/pop-api/src/v0/dispatch_error.rs b/pop-api/src/v0/dispatch_error.rs new file mode 100644 index 00000000..6ed40ce5 --- /dev/null +++ b/pop-api/src/v0/dispatch_error.rs @@ -0,0 +1,57 @@ +use super::*; + +#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub(crate) enum TokenError { + /// Funds are unavailable. + FundsUnavailable, + /// Some part of the balance gives the only provider reference to the account and thus cannot + /// be (re)moved. + OnlyProvider, + /// Account cannot exist with the funds that would be given. + BelowMinimum, + /// Account cannot be created. + CannotCreate, + /// The asset in question is unknown. + UnknownAsset, + /// Funds exist but are frozen. + Frozen, + /// Operation is not supported by the asset. + Unsupported, + /// Account cannot be created for a held balance. + CannotCreateHold, + /// Withdrawal would cause unwanted loss of account. + NotExpendable, + /// Account cannot receive the assets. + Blocked, +} + +impl TryFrom for TokenError { + type Error = crate::PopApiError; + + fn try_from(status_code: u32) -> core::result::Result { + use TokenError::*; + match status_code { + 0 => Ok(FundsUnavailable), + 1 => Ok(OnlyProvider), + 2 => Ok(BelowMinimum), + 3 => Ok(CannotCreate), + 4 => Ok(UnknownAsset), + 5 => Ok(Frozen), + 6 => Ok(Unsupported), + 7 => Ok(CannotCreateHold), + 8 => Ok(NotExpendable), + 9 => Ok(Blocked), + _ => todo!(), + } + } +} + +impl From for TokenError { + fn from(error: PopApiError) -> Self { + match error { + PopApiError::TokenError(e) => e, + _ => todo!(), + } + } +} diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index def37e55..02169c22 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -3,12 +3,13 @@ use crate::{ BlockNumber, PopApiError, }; +pub mod assets; pub mod balances; pub mod contracts; pub mod cross_chain; +pub mod dispatch_error; pub mod nfts; pub mod state; -pub mod assets; pub fn relay_chain_block_number() -> Result { state::read(RuntimeStateKeys::ParachainSystem(ParachainSystemKeys::LastRelayChainBlockNumber)) diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index 5a55154f..946a3eca 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -1,5 +1,5 @@ use super::RuntimeCall; -use crate::{PopApiError::UnknownStatusCode, *}; +use crate::{PopApiError::UnknownModuleStatusCode, *}; use ink::prelude::vec::Vec; use primitives::{ApprovalsLimit, BoundedBTreeMap, KeyLimit, MultiAddress}; pub use primitives::{CollectionId, ItemId}; @@ -660,7 +660,7 @@ impl TryFrom for Error { 42 => Ok(WrongNamespace), 43 => Ok(CollectionNotEmpty), 44 => Ok(WitnessRequired), - _ => Err(UnknownStatusCode(status_code)), + _ => Err(UnknownModuleStatusCode(status_code)), } } } diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs index 096f21c9..d2129c4f 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions/mod.rs @@ -69,10 +69,19 @@ where Err(DispatchError::Module(error)) => { // encode status code = pallet index in runtime + error index, allowing for // 999 errors + let first = (3u32 * 1_000_000u32) + + (error.index as u32 * 1_000u32) + + u32::from_le_bytes(error.error); Ok(RetVal::Converging( - (error.index as u32 * 1_000) + u32::from_le_bytes(error.error), + // (3u32 * 1_000_000u32) + // + (error.index as u32 * 1_000u32) + // + u32::from_le_bytes(error.error), + first, )) }, + Err(DispatchError::Token(error)) => { + Ok(RetVal::Converging((7u32 * 1_000_000u32) + error as u32)) + }, Err(e) => Err(e), } }, diff --git a/runtime/devnet/src/extensions/tests/local_fungibles.rs b/runtime/devnet/src/extensions/tests/local_fungibles.rs index 2d752361..5bd7dac0 100644 --- a/runtime/devnet/src/extensions/tests/local_fungibles.rs +++ b/runtime/devnet/src/extensions/tests/local_fungibles.rs @@ -5,36 +5,32 @@ use pallet_contracts::debug::ExecResult; #[derive(Decode, Encode, Debug, Eq, PartialEq)] enum FungiblesError { - // AssetsError(Error), - // /// The origin of the call doesn't have the right permission. - // BadOrigin, - // /// Custom error type for cases in which an implementation adds its own restrictions. - // Custom(String), - /// Not enough balance to fulfill a request is available. - InsufficientBalance, + /// The amount to mint is less than the existential deposit. + BelowMinimum, + /// Unspecified dispatch error, providing the index and optionally its error index. + DispatchError { index: u8, error: Option }, /// Not enough allowance to fulfill a request is available. InsufficientAllowance, - /// The asset status is not the expected status. - IncorrectStatus, + /// Not enough balance to fulfill a request is available. + InsufficientBalance, /// The asset ID is already taken. InUse, /// Minimum balance should be non-zero. MinBalanceZero, + /// Unspecified pallet error, providing pallet index and error index. + ModuleError { pallet: u8, error: u16 }, /// The signing account has no permission to do the operation. NoPermission, - // /// Safe transfer check fails (e.g. if the receiving contract does not accept tokens). - // SafeTransferCheckFailed(String), /// The given asset ID is unknown. Unknown, - /// Recipient's address is zero. - ZeroRecipientAddress, - /// Sender's address is zero. - ZeroSenderAddress, - UndefinedError, } const ASSET_ID: u32 = 1; +fn decoded(result: ExecReturnValue) -> T { + ::decode(&mut &result.data[2..]).unwrap() +} + fn allowance( addr: AccountId32, asset_id: u32, @@ -54,10 +50,11 @@ fn balance_of(addr: AccountId32, asset_id: u32, owner: AccountId32) -> ExecRetur } // Call total_supply contract message. -fn total_supply(addr: AccountId32, asset_id: u32) -> ExecReturnValue { +fn total_supply(addr: AccountId32, asset_id: u32) -> Balance { let function = function_selector("total_supply"); let params = [function, asset_id.encode()].concat(); - do_bare_call(addr, params, 0).expect("should work") + let result = do_bare_call(addr, params, 0).expect("should work"); + decoded::(result) } fn asset_exists(addr: AccountId32, asset_id: u32) -> ExecReturnValue { @@ -70,7 +67,7 @@ fn create( addr: AccountId32, asset_id: u32, admin: AccountId32, - min_balance: u128, + min_balance: Balance, ) -> ExecReturnValue { let function = function_selector("create"); let params = [function, asset_id.encode(), admin.encode(), min_balance.encode()].concat(); @@ -95,7 +92,7 @@ fn transfer_from( asset_id: u32, _from: Option, to: Option, - value: u128, + value: Balance, _data: &[u8], ) -> ExecReturnValue { // let function = function_selector("transfer_from"); @@ -109,16 +106,21 @@ fn transfer_from( } // Create an asset and mint to owner. -fn create_asset(asset_id: u32, owner: AccountId32) { +fn create_asset(asset_id: u32, owner: AccountId32, min_balance: Balance) { assert_eq!( - Assets::create(RuntimeOrigin::signed(owner.clone()), asset_id.into(), owner.into(), 1), + Assets::create( + RuntimeOrigin::signed(owner.clone()), + asset_id.into(), + owner.into(), + min_balance + ), Ok(()) ); } // Create an asset and mint to owner. -fn create_asset_and_mint_to(asset_id: u32, owner: AccountId32, to: AccountId32, value: u128) { - create_asset(asset_id, owner.clone()); +fn create_asset_and_mint_to(asset_id: u32, owner: AccountId32, to: AccountId32, value: Balance) { + create_asset(asset_id, owner.clone(), 1); assert_eq!( Assets::mint(RuntimeOrigin::signed(owner.into()), asset_id.into(), to.into(), value,), Ok(()) @@ -130,9 +132,9 @@ fn create_asset_mint_and_approve( asset_id: u32, owner: AccountId32, to: AccountId32, - mint: u128, + mint: Balance, spender: AccountId32, - approve: u128, + approve: Balance, ) { create_asset_and_mint_to(asset_id, owner.clone(), to.clone(), mint); assert_eq!( @@ -151,18 +153,18 @@ fn create_asset_mint_and_approve( fn total_supply_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = - instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE); + let addr = instantiate( + "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", + INIT_VALUE, + vec![], + ); // No tokens in circulation. - assert_eq!( - Assets::total_supply(ASSET_ID).encode(), - total_supply(addr.clone(), ASSET_ID).data[2..] - ); + assert_eq!(Assets::total_supply(ASSET_ID), total_supply(addr.clone(), ASSET_ID)); // Tokens in circulation. create_asset_and_mint_to(ASSET_ID, addr.clone(), BOB, 100); - assert_eq!(Assets::total_supply(ASSET_ID).encode(), total_supply(addr, ASSET_ID).data[2..]); + assert_eq!(Assets::total_supply(ASSET_ID), total_supply(addr, ASSET_ID)); }); } @@ -171,8 +173,11 @@ fn total_supply_works() { fn balance_of_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = - instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE); + let addr = instantiate( + "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", + INIT_VALUE, + vec![], + ); // No tokens in circulation. assert_eq!( @@ -194,8 +199,11 @@ fn balance_of_works() { fn allowance_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = - instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE); + let addr = instantiate( + "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", + INIT_VALUE, + vec![], + ); // No tokens in circulation. assert_eq!( @@ -217,8 +225,11 @@ fn allowance_works() { fn asset_exists_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = - instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE); + let addr = instantiate( + "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", + INIT_VALUE, + vec![], + ); // No tokens in circulation. assert_eq!( @@ -227,40 +238,45 @@ fn asset_exists_works() { ); // Tokens in circulation. - create_asset(ASSET_ID, addr.clone()); + create_asset(ASSET_ID, addr.clone(), 1); assert_eq!(Assets::asset_exists(ASSET_ID).encode(), asset_exists(addr, ASSET_ID).data[2..]); }); } -fn decode_error(result: ExecReturnValue) -> FungiblesError { - FungiblesError::decode(&mut &result.data[2..]).unwrap() -} - +// Todo - errors: +// - Badorigin: contract is always signed +// - Lookup: is a valid AccountId due to the contract +// - reserve(): Overflow, LiquidityRestrictions; frozen +// - Callback +// - StorageDepositLimitExhausted #[test] #[ignore] fn create_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", 0); let new_asset = 2; + let addr = + instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", 0, vec![0]); assert_eq!( - decode_error(create(addr.clone(), new_asset, BOB, 1)), - FungiblesError::UndefinedError + decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), + FungiblesError::InsufficientBalance ); - // Todo: errors Badorigin, Lookup, reserve(), Callback - // create_asset(ASSET_ID, ALICE); - // // Error `InUse`. - // assert_eq!(decode_error(create(addr.clone(), ASSET_ID, BOB, 1)), FungiblesError::InUse); - // // Error `MinBalanceZero`. - // assert_eq!( - // decode_error(create(addr.clone(), new_asset, BOB, 0)), - // FungiblesError::MinBalanceZero - // ); - // assert!( - // !create(addr.clone(), new_asset, BOB, 1).did_revert(), - // "Contract should have been reverted!" - // ); + let addr = instantiate( + "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", + INIT_VALUE, + vec![1], + ); + create_asset(ASSET_ID, ALICE, 1); + assert_eq!( + decoded::(create(addr.clone(), ASSET_ID, BOB, 1)), + FungiblesError::InUse + ); + assert_eq!( + decoded::(create(addr.clone(), new_asset, BOB, 0)), + FungiblesError::MinBalanceZero + ); + assert!(!create(addr.clone(), new_asset, BOB, 1).did_revert(), "Contract reverted!"); }); } @@ -269,34 +285,78 @@ fn create_works() { fn set_metadata_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = - instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE); + let addr = instantiate( + "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", + INIT_VALUE, + vec![], + ); - create_asset(ASSET_ID, addr.clone()); + create_asset(ASSET_ID, addr.clone(), 1); let result = set_metadata(addr.clone(), ASSET_ID, vec![12], vec![12], 12); - assert!(!result.did_revert(), "Contract should have been reverted!"); + assert!(!result.did_revert(), "Contract reverted!"); }); } +// todo: errors: +// - AssetNotLive: when frozen or being destroyed +// - TokenErrors: https://github.com/paritytech/polkadot-sdk/blob/3977f389cce4a00fd7100f95262e0563622b9aa4/substrate/frame/assets/src/functions.rs#L125 #[test] #[ignore] -fn transfer_from_aka_mint_works() { +fn mint_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = - instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE); - - let amount: u128 = 100 * UNIT; - // Create asset with contract as owner. - create_asset(ASSET_ID, addr.clone()); - // Check Bob's asset balance before minting through contract. - let bob_balance_before = Assets::balance(ASSET_ID, &BOB); + let addr = instantiate( + "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", + INIT_VALUE, + vec![], + ); + let amount: Balance = 100 * UNIT; - let result = transfer_from(addr.clone(), ASSET_ID, None, Some(BOB), 100 * UNIT, &[0u8]); + assert_eq!( + decoded::(transfer_from( + addr.clone(), + ASSET_ID, + None, + Some(BOB), + amount, + &[0u8] + )), + FungiblesError::Unknown + ); + create_asset(ASSET_ID, ALICE, 2); + assert_eq!( + decoded::(transfer_from( + addr.clone(), + ASSET_ID, + None, + Some(BOB), + amount, + &[0u8] + )), + FungiblesError::NoPermission + ); + assert_eq!( + decoded::(transfer_from( + addr.clone(), + ASSET_ID, + None, + Some(BOB), + 1, + &[0u8] + )), + FungiblesError::BelowMinimum + ); + let asset = 2; + create_asset(asset, addr.clone(), 2); + let bob_balance_before_mint = Assets::balance(asset, &BOB); + let result = transfer_from(addr.clone(), asset, None, Some(BOB), 100 * UNIT, &[0u8]); assert!(!result.did_revert(), "Contract reverted!"); - - let bob_balance_after = Assets::balance(ASSET_ID, &BOB); - assert_eq!(bob_balance_after, bob_balance_before + amount); + let bob_balance_after_mint = Assets::balance(asset, &BOB); + assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount); }); } + +#[test] +#[ignore] +fn transfer_works() {} diff --git a/runtime/devnet/src/extensions/tests/mod.rs b/runtime/devnet/src/extensions/tests/mod.rs index 30d1a924..03cda2cd 100644 --- a/runtime/devnet/src/extensions/tests/mod.rs +++ b/runtime/devnet/src/extensions/tests/mod.rs @@ -6,11 +6,13 @@ use sp_runtime::{traits::Hash, AccountId32, BuildStorage}; mod local_fungibles; +type Balance = u128; const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug; const ALICE: AccountId32 = AccountId32::new([1_u8; 32]); const BOB: AccountId32 = AccountId32::new([2_u8; 32]); -const INIT_VALUE: u128 = 100_000_000 * UNIT; +const INIT_AMOUNT: Balance = 100_000_000 * UNIT; +const INIT_VALUE: Balance = 100 * UNIT; const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024); fn new_test_ext() -> sp_io::TestExternalities { @@ -19,7 +21,7 @@ fn new_test_ext() -> sp_io::TestExternalities { .expect("Frame system builds valid default genesis config"); pallet_balances::GenesisConfig:: { - balances: vec![(ALICE, INIT_VALUE), (BOB, INIT_VALUE)], + balances: vec![(ALICE, INIT_AMOUNT), (BOB, INIT_AMOUNT)], } .assimilate_storage(&mut t) .expect("Pallet balances storage can be assimilated"); @@ -65,7 +67,7 @@ fn do_bare_call( } // Deploy, instantiate and return contract address. -fn instantiate(contract: &str, init_value: u128) -> AccountId32 { +fn instantiate(contract: &str, init_value: u128, salt: Vec) -> AccountId32 { let (wasm_binary, _) = load_wasm_module::(contract).expect("could not read .wasm file"); let result = Contracts::bare_instantiate( @@ -75,7 +77,7 @@ fn instantiate(contract: &str, init_value: u128) -> AccountId32 { None, Code::Upload(wasm_binary), function_selector("new"), - vec![], + salt, DEBUG_OUTPUT, CollectEvents::Skip, ) From 1022926e0d35ee56930a0757655df3c7f12b6ec4 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Thu, 6 Jun 2024 11:48:55 +0200 Subject: [PATCH 05/76] test: add transfer --- pop-api/examples/fungibles/lib.rs | 108 +++++---- pop-api/src/lib.rs | 7 +- pop-api/src/v0/assets/fungibles.rs | 206 ++++++++++-------- pop-api/src/v0/balances.rs | 10 +- .../src/extensions/tests/local_fungibles.rs | 205 ++++++++++++----- runtime/devnet/src/extensions/tests/mod.rs | 1 + 6 files changed, 344 insertions(+), 193 deletions(-) diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs index e44c4347..68e43865 100755 --- a/pop-api/examples/fungibles/lib.rs +++ b/pop-api/examples/fungibles/lib.rs @@ -1,6 +1,10 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -// Fungibles wrapper contract to allow contracts to interact with local fungibles without the pop api. +/// Local Fungibles: +/// 1. PSP-22 Interface +/// 2. PSP-22 Metadata Interface +/// 3. Asset Management +/// use ink::prelude::vec::Vec; use pop_api::{ assets::fungibles::*, @@ -10,6 +14,8 @@ use pop_api::{ #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum ContractError { + /// The asset is not live; either frozen or being destroyed. + AssetNotLive, /// The amount to mint is less than the existential deposit. BelowMinimum, /// Unspecified dispatch error, providing the index and its error index (if none `0`). @@ -24,6 +30,8 @@ pub enum ContractError { MinBalanceZero, /// Unspecified pallet error, providing pallet index and error index. ModuleError { pallet: u8, error: u16 }, + /// The account to alter does not exist. + NoAccount, /// The signing account has no permission to do the operation. NoPermission, /// The given asset ID is unknown. @@ -33,6 +41,7 @@ pub enum ContractError { impl From for ContractError { fn from(error: FungiblesError) -> Self { match error { + FungiblesError::AssetNotLive => ContractError::AssetNotLive, FungiblesError::BelowMinimum => ContractError::BelowMinimum, FungiblesError::DispatchError { index, error } => { ContractError::DispatchError { index, error } @@ -44,6 +53,7 @@ impl From for ContractError { FungiblesError::ModuleError { pallet, error } => { ContractError::ModuleError { pallet, error } }, + FungiblesError::NoAccount => ContractError::NoAccount, FungiblesError::NoPermission => ContractError::NoPermission, FungiblesError::Unknown => ContractError::Unknown, } @@ -68,6 +78,16 @@ mod fungibles { Default::default() } + /// 1. PSP-22 Interface: + /// - total_supply + /// - balance_of + /// - allowance + /// - transfer + /// - transfer_from + /// - approve + /// - increase_allowance + /// - decrease_allowance + #[ink(message)] pub fn total_supply(&self, id: AssetId) -> Result { total_supply(id).map_err(From::from) @@ -89,13 +109,58 @@ mod fungibles { } #[ink(message)] - pub fn asset_exists(&self, id: AssetId) -> Result { - asset_exists(id).map_err(From::from) + pub fn transfer(&self, id: AssetId, to: AccountId32, value: Balance) -> Result<()> { + ink::env::debug_println!( + "PopApiAssetsExample::transfer: id: {:?}, to: {:?} value: {:?}", + id, + to, + value, + ); + + let result = transfer(id, to, value); + ink::env::debug_println!("Result: {:?}", result); + result.map_err(From::from) + } + + #[ink(message)] + pub fn transfer_from( + &self, + id: AssetId, + from: Option, + to: Option, + value: Balance, + // Size needs to be known at compile time or ink's `Vec` + data: Vec, + ) -> Result<()> { + ink::env::debug_println!( + "PopApiAssetsExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}", + id, + from, + to, + value, + ); + + let result = transfer_from(id, from, to, value, &data); + ink::env::debug_println!("Result: {:?}", result); + result.map_err(From::from) } + /// 2. PSP-22 Metadata Interface: + /// - token_name + /// - token_symbol + /// - token_decimals + + /// 3. Asset Management: + /// - create + /// - start_destroy + /// - destroy_accounts + /// - destroy_approvals + /// - finish_destroy + /// - set_metadata + /// - clear_metadata + #[ink(message)] pub fn create(&self, id: AssetId, admin: AccountId32, min_balance: Balance) -> Result<()> { - // create(id, admin, min_balance).map_err(From::from) ink::env::debug_println!( "PopApiAssetsExample::create: id: {:?} admin: {:?} min_balance: {:?}", id, @@ -115,7 +180,6 @@ mod fungibles { symbol: Vec, decimals: u8, ) -> Result<()> { - // set_metadata(id, name, symbol, decimals).map_err(From::from) ink::env::debug_println!( "PopApiAssetsExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}", id, @@ -129,39 +193,9 @@ mod fungibles { } #[ink(message)] - pub fn mint(&self, id: AssetId, beneficiary: AccountId32, amount: Balance) -> Result<()> { - ink::env::debug_println!( - "PopApiAssetsExample::mint: id: {:?}, beneficiary: {:?} amount: {:?}", - id, - beneficiary, - amount, - ); - - let result = mint(id, beneficiary, amount); - ink::env::debug_println!("Result: {:?}", result); - result.map_err(From::from) + pub fn asset_exists(&self, id: AssetId) -> Result { + asset_exists(id).map_err(From::from) } - - // #[ink(message)] - // pub fn transfer_from( - // id: AssetId, - // from: Option, - // to: Option, - // value: Balance, - // data: [u8], - // ) -> Result<()> { - // ink::env::debug_println!( - // "PopApiAssetsExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}", - // id, - // from, - // to, - // value, - // ); - // - // let result = transfer_from(id, from, to, value)?; - // ink::env::debug_println!("Result: {:?}", result); - // result - // } } #[cfg(test)] diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 014b5a29..be448890 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -21,16 +21,11 @@ type MaxTips = u32; pub type Result = core::result::Result; -struct ModuleError { - pallet: u8, - error: u16, -} - #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum PopApiError { Assets(assets::fungibles::AssetsError), - Balances(balances::Error), + Balances(balances::BalancesError), Contracts(contracts::Error), DecodingFailed, Nfts(nfts::Error), diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index de624938..df9033c7 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -1,7 +1,6 @@ use crate::{ - AccountId, Balance, - PopApiError::{UnknownDispatchStatusCode, UnknownModuleStatusCode}, - RuntimeCall, *, + balances::BalancesError, AccountId, Balance, PopApiError::UnknownModuleStatusCode, RuntimeCall, + *, }; use ink::prelude::vec::Vec; use primitives::AssetId; @@ -62,28 +61,6 @@ pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result>, min_balance: Balance) -> Result<()> { -pub fn create( - id: AssetId, - admin: impl Into>, - min_balance: Balance, -) -> Result<()> { - Ok(dispatch(RuntimeCall::Assets(AssetsCall::Create { - id: id.into(), - admin: admin.into(), - min_balance, - }))?) -} - /// Transfers `value` amount of tokens from the caller's account to account `to`, with additional /// `data` in unspecified format. /// @@ -94,29 +71,27 @@ pub fn create( /// /// # Returns /// Returns `Ok(())` if successful, or an error if the transfer fails. -// #[allow(unused_variables)] -// pub fn transfer( -// id: AssetId, -// to: impl Into>, -// value: Balance, -// ) -> Result<()> { -// todo!() -// // TODO: transfer or transfer_keep_alive -// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::Transfer { -// // id: id.into(), -// // target: target.into(), -// // amount: Compact(amount), -// // }))?) -// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { -// // id: id.into(), -// // target: target.into(), -// // amount: Compact(amount), -// // }))?) -// } +pub fn transfer( + id: AssetId, + to: impl Into>, + value: Balance, +) -> Result<()> { + // TODO: transfer or transfer_keep_alive + // Ok(dispatch(RuntimeCall::Assets(AssetsCall::Transfer { + // id: id.into(), + // target: target.into(), + // amount: Compact(amount), + // }))?) + Ok(dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { + id: id.into(), + target: to.into(), + amount: Compact(value), + }))?) +} -/// Transfers `value` tokens on the behalf of `from` to the account `to` with additional `data` -/// in unspecified format. This can be used to allow a contract to transfer tokens on ones behalf -/// and/or to charge fees in sub-currencies, for example. +/// Transfers `value` tokens on behalf of `from` to account `to` with additional `data` +/// in unspecified format. If `from` is equal to `None`, tokens will be minted to account `to`. If +/// `to` is equal to `None`, tokens will be burned from account `from`. /// /// # Arguments /// * `id` - The ID of the asset. @@ -126,43 +101,26 @@ pub fn create( /// /// # Returns /// Returns `Ok(())` if successful, or an error if the transfer fails. -// pub fn transfer_from( -// id: AssetId, -// from: impl Into>, -// to: impl Into>, -// value: Balance, -// ) -> Result<()> { -//todo!() -// TODO: depending on `from` and `to`, decide whether to mint, burn or transfer_approved. -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::Mint { -// id: id.into(), -// beneficiary: beneficiary.into(), -// amount: Compact(amount), -// }))?) -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::Burn { -// id: id.into(), -// who: who.into(), -// amount: Compact(amount), -// }))?) -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::TransferApproved { -// id: id.into(), -// owner: from.into(), -// destination: to.into(), -// amount: Compact(value), -// }))?) -// } - -/// Mint assets of a particular class. -pub fn mint( +pub fn transfer_from( id: AssetId, - beneficiary: impl Into>, - amount: Balance, + from: Option>>, + to: Option>>, + value: Balance, + _data: &[u8], ) -> Result<()> { - Ok(dispatch(RuntimeCall::Assets(AssetsCall::Mint { - id: id.into(), - beneficiary: beneficiary.into(), - amount: Compact(amount), - }))?) + match (from, to) { + (None, Some(to)) => mint(id, to, value), + // (Some(from), None) => burn(id, from, value), + (Some(from), Some(to)) => { + Ok(dispatch(RuntimeCall::Assets(AssetsCall::TransferApproved { + id: id.into(), + owner: from.into(), + destination: to.into(), + amount: Compact(value), + }))?) + }, + _ => Ok(()), + } } /// Approves an account to spend a specified number of tokens on behalf of the caller. @@ -277,6 +235,27 @@ pub fn mint( /// - set_metadata /// - clear_metadata +/// Create a new token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `admin` - The account that will administer the asset. +/// * `min_balance` - The minimum balance required for accounts holding this asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the creation fails. +pub fn create( + id: AssetId, + admin: impl Into>, + min_balance: Balance, +) -> Result<()> { + Ok(dispatch(RuntimeCall::Assets(AssetsCall::Create { + id: id.into(), + admin: admin.into(), + min_balance, + }))?) +} + /// Start the process of destroying a token with a given asset ID. /// /// # Arguments @@ -362,10 +341,22 @@ pub fn asset_exists(id: AssetId) -> Result { Ok(state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id)))?) } -// Parameters to extrinsics representing an asset id (`AssetIdParameter`) and a balance amount (`Balance`) are expected -// to be compact encoded. The pop api handles that for the developer. -// -// reference: https://substrate.stackexchange.com/questions/1873/what-is-the-meaning-of-palletcompact-in-pallet-development +/// Mint assets of a particular class. +fn mint( + id: AssetId, + beneficiary: impl Into>, + amount: Balance, +) -> Result<()> { + Ok(dispatch(RuntimeCall::Assets(AssetsCall::Mint { + id: id.into(), + beneficiary: beneficiary.into(), + amount: Compact(amount), + }))?) +} + +// Parameters to extrinsics representing an asset id (`AssetIdParameter`) and a balance amount +// (`Balance`) are expected to be compact encoded. The pop api handles that for the developer. +// https://substrate.stackexchange.com/questions/1873/what-is-the-meaning-of-palletcompact-in-pallet-development // // Asset id that is compact encoded. type AssetIdParameter = Compact; @@ -423,7 +414,6 @@ pub(crate) enum AssetsCall { }, } -// TODO: remove unnecessary errors #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub(crate) enum AssetsError { @@ -514,6 +504,8 @@ impl TryFrom for AssetsError { #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum FungiblesError { + /// The asset is not live; either frozen or being destroyed. + AssetNotLive, /// The amount to mint is less than the existential deposit. BelowMinimum, /// Unspecified dispatch error, providing the index and its error index (if none `0`). @@ -528,16 +520,20 @@ pub enum FungiblesError { MinBalanceZero, /// Unspecified pallet error, providing pallet index and error index. ModuleError { pallet: u8, error: u16 }, + /// The account to alter does not exist. + NoAccount, /// The signing account has no permission to do the operation. NoPermission, /// The given asset ID is unknown. Unknown, } -impl From for FungiblesError { - fn from(error: balances::Error) -> Self { +impl From for FungiblesError { + fn from(error: BalancesError) -> Self { match error { - balances::Error::InsufficientBalance => FungiblesError::InsufficientBalance, + // TODO: this insufficient balance is different than the assets variant. This one is + // for a deposit of creating an asset, the latter is for transfer tokens. + BalancesError::InsufficientBalance => FungiblesError::InsufficientBalance, _ => FungiblesError::ModuleError { pallet: 40, error: error as u16 }, } } @@ -546,8 +542,10 @@ impl From for FungiblesError { impl From for FungiblesError { fn from(error: dispatch_error::TokenError) -> Self { match error { - dispatch_error::TokenError::UnknownAsset => FungiblesError::Unknown, dispatch_error::TokenError::BelowMinimum => FungiblesError::BelowMinimum, + // ED is not respected. + dispatch_error::TokenError::OnlyProvider => FungiblesError::InsufficientBalance, + dispatch_error::TokenError::UnknownAsset => FungiblesError::Unknown, _ => FungiblesError::DispatchError { index: 7, error: error as u8 }, } } @@ -556,12 +554,15 @@ impl From for FungiblesError { impl From for FungiblesError { fn from(error: AssetsError) -> Self { match error { + AssetsError::AssetNotLive => FungiblesError::AssetNotLive, + AssetsError::BalanceLow => FungiblesError::InsufficientBalance, AssetsError::Unapproved => FungiblesError::InsufficientAllowance, AssetsError::InUse => FungiblesError::InUse, AssetsError::MinBalanceZero => FungiblesError::MinBalanceZero, AssetsError::NoPermission => FungiblesError::NoPermission, + AssetsError::NoAccount => FungiblesError::NoAccount, AssetsError::Unknown => FungiblesError::Unknown, - _ => FungiblesError::ModuleError { pallet: 40, error: error as u16 }, + _ => FungiblesError::ModuleError { pallet: 52, error: error as u16 }, } } } @@ -586,3 +587,24 @@ impl From for FungiblesError { } } } + +// macro_rules! impl_error_conversion { +// ($pallet_index:, $pallet_error:ty, $interface_error:ty, $($variant:ident),*) => { +// impl From<$pallet_error> for $interface_error { +// fn from(error: $pallet_error) -> Self { +// match error { +// $( +// <$pallet_error>::$variant => <$interface_error>::$variant, +// )* +// _ => <$interface_error>::ModuleError { pallet: 0, error: [255, 0, 0, 0] }, // Default case +// } +// } +// } +// +// impl FromPalletError<$pallet_error> for $interface_error { +// fn from_pallet_error(error: $pallet_error) -> Self { +// Self::from(error) +// } +// } +// }; +// } diff --git a/pop-api/src/v0/balances.rs b/pop-api/src/v0/balances.rs index bc422ef4..08db0a75 100644 --- a/pop-api/src/v0/balances.rs +++ b/pop-api/src/v0/balances.rs @@ -3,7 +3,7 @@ use crate::{ PopApiError::UnknownModuleStatusCode, }; -type Result = core::result::Result; +type Result = core::result::Result; pub fn transfer_keep_alive( dest: impl Into>, @@ -28,7 +28,7 @@ pub(crate) enum BalancesCall { #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub(crate) enum Error { +pub(crate) enum BalancesError { /// Vesting balance too high to send value. VestingBalance, /// Account liquidity restrictions prevent withdrawal. @@ -55,11 +55,11 @@ pub(crate) enum Error { DeltaZero, } -impl TryFrom for Error { +impl TryFrom for BalancesError { type Error = PopApiError; fn try_from(status_code: u32) -> core::result::Result { - use Error::*; + use BalancesError::*; match status_code { 0 => Ok(VestingBalance), 1 => Ok(LiquidityRestrictions), @@ -78,7 +78,7 @@ impl TryFrom for Error { } } -impl From for Error { +impl From for BalancesError { fn from(error: PopApiError) -> Self { match error { PopApiError::Balances(e) => e, diff --git a/runtime/devnet/src/extensions/tests/local_fungibles.rs b/runtime/devnet/src/extensions/tests/local_fungibles.rs index 5bd7dac0..5de162c9 100644 --- a/runtime/devnet/src/extensions/tests/local_fungibles.rs +++ b/runtime/devnet/src/extensions/tests/local_fungibles.rs @@ -1,10 +1,11 @@ #![cfg(test)] use super::*; -use pallet_contracts::debug::ExecResult; #[derive(Decode, Encode, Debug, Eq, PartialEq)] enum FungiblesError { + /// The asset is not live; either frozen or being destroyed. + AssetNotLive, /// The amount to mint is less than the existential deposit. BelowMinimum, /// Unspecified dispatch error, providing the index and optionally its error index. @@ -19,13 +20,15 @@ enum FungiblesError { MinBalanceZero, /// Unspecified pallet error, providing pallet index and error index. ModuleError { pallet: u8, error: u16 }, + /// The account to alter does not exist. + NoAccount, /// The signing account has no permission to do the operation. NoPermission, /// The given asset ID is unknown. Unknown, } -const ASSET_ID: u32 = 1; +const ASSET_ID: AssetId = 1; fn decoded(result: ExecReturnValue) -> T { ::decode(&mut &result.data[2..]).unwrap() @@ -33,39 +36,42 @@ fn decoded(result: ExecReturnValue) -> T { fn allowance( addr: AccountId32, - asset_id: u32, + asset_id: AssetId, owner: AccountId32, spender: AccountId32, -) -> ExecReturnValue { +) -> Balance { let function = function_selector("allowance"); let params = [function, asset_id.encode(), owner.encode(), spender.encode()].concat(); - do_bare_call(addr, params, 0).expect("should work") + let result = do_bare_call(addr, params, 0).expect("should work"); + decoded::(result) } // Call balance_of contract message. -fn balance_of(addr: AccountId32, asset_id: u32, owner: AccountId32) -> ExecReturnValue { +fn balance_of(addr: AccountId32, asset_id: AssetId, owner: AccountId32) -> Balance { let function = function_selector("balance_of"); let params = [function, asset_id.encode(), owner.encode()].concat(); - do_bare_call(addr, params, 0).expect("should work") + let result = do_bare_call(addr, params, 0).expect("should work"); + decoded::(result) } // Call total_supply contract message. -fn total_supply(addr: AccountId32, asset_id: u32) -> Balance { +fn total_supply(addr: AccountId32, asset_id: AssetId) -> Balance { let function = function_selector("total_supply"); let params = [function, asset_id.encode()].concat(); let result = do_bare_call(addr, params, 0).expect("should work"); decoded::(result) } -fn asset_exists(addr: AccountId32, asset_id: u32) -> ExecReturnValue { +fn asset_exists(addr: AccountId32, asset_id: AssetId) -> bool { let function = function_selector("asset_exists"); let params = [function, asset_id.encode()].concat(); - do_bare_call(addr, params, 0).expect("should work") + let result = do_bare_call(addr, params, 0).expect("should work"); + decoded::(result) } fn create( addr: AccountId32, - asset_id: u32, + asset_id: AssetId, admin: AccountId32, min_balance: Balance, ) -> ExecReturnValue { @@ -76,7 +82,7 @@ fn create( fn set_metadata( addr: AccountId32, - asset_id: u32, + asset_id: AssetId, name: Vec, symbol: Vec, decimals: u8, @@ -87,26 +93,34 @@ fn set_metadata( do_bare_call(addr, params, 0).expect("should work") } +fn transfer( + addr: AccountId32, + asset_id: AssetId, + to: AccountId32, + value: Balance, +) -> ExecReturnValue { + let function = function_selector("transfer"); + let params = [function, asset_id.encode(), to.encode(), value.encode()].concat(); + do_bare_call(addr, params, 0).expect("should work") +} + fn transfer_from( addr: AccountId32, - asset_id: u32, - _from: Option, + asset_id: AssetId, + from: Option, to: Option, value: Balance, - _data: &[u8], + data: &[u8], ) -> ExecReturnValue { - // let function = function_selector("transfer_from"); - // let params = - // [function, asset_id.encode(), from.encode(), to.encode(), value.encode(), data.encode()] - // .concat(); - // do_bare_call(addr, params, 0) - let function = function_selector("mint"); - let params = [function, asset_id.encode(), to.unwrap().encode(), value.encode()].concat(); + let function = function_selector("transfer_from"); + let params = + [function, asset_id.encode(), from.encode(), to.encode(), value.encode(), data.encode()] + .concat(); do_bare_call(addr, params, 0).expect("should work") } // Create an asset and mint to owner. -fn create_asset(asset_id: u32, owner: AccountId32, min_balance: Balance) { +fn create_asset(asset_id: AssetId, owner: AccountId32, min_balance: Balance) -> AssetId { assert_eq!( Assets::create( RuntimeOrigin::signed(owner.clone()), @@ -116,20 +130,27 @@ fn create_asset(asset_id: u32, owner: AccountId32, min_balance: Balance) { ), Ok(()) ); + asset_id } // Create an asset and mint to owner. -fn create_asset_and_mint_to(asset_id: u32, owner: AccountId32, to: AccountId32, value: Balance) { +fn create_asset_and_mint_to( + asset_id: AssetId, + owner: AccountId32, + to: AccountId32, + value: Balance, +) -> AssetId { create_asset(asset_id, owner.clone(), 1); assert_eq!( Assets::mint(RuntimeOrigin::signed(owner.into()), asset_id.into(), to.into(), value,), Ok(()) ); + asset_id } // Create an asset, mints to, and approves spender. fn create_asset_mint_and_approve( - asset_id: u32, + asset_id: AssetId, owner: AccountId32, to: AccountId32, mint: Balance, @@ -148,6 +169,21 @@ fn create_asset_mint_and_approve( ); } +// Freeze an asset. +fn freeze_asset(asset_id: AssetId, owner: AccountId32) { + assert_eq!(Assets::freeze_asset(RuntimeOrigin::signed(owner.into()), asset_id.into()), Ok(())); +} + +// Thaw an asset. +fn thaw_asset(asset_id: AssetId, owner: AccountId32) { + assert_eq!(Assets::thaw_asset(RuntimeOrigin::signed(owner.into()), asset_id.into()), Ok(())); +} + +// Start destroying an asset. +fn start_destroy_asset(asset_id: AssetId, owner: AccountId32) { + assert_eq!(Assets::start_destroy(RuntimeOrigin::signed(owner.into()), asset_id.into()), Ok(())); +} + #[test] #[ignore] fn total_supply_works() { @@ -180,17 +216,11 @@ fn balance_of_works() { ); // No tokens in circulation. - assert_eq!( - Assets::balance(ASSET_ID, BOB).encode(), - balance_of(addr.clone(), ASSET_ID, BOB).data[2..] - ); + assert_eq!(Assets::balance(ASSET_ID, BOB), balance_of(addr.clone(), ASSET_ID, BOB)); // Tokens in circulation. create_asset_and_mint_to(ASSET_ID, addr.clone(), BOB, 100); - assert_eq!( - Assets::balance(ASSET_ID, BOB).encode(), - balance_of(addr, ASSET_ID, BOB).data[2..] - ); + assert_eq!(Assets::balance(ASSET_ID, BOB), balance_of(addr, ASSET_ID, BOB)); }); } @@ -207,15 +237,15 @@ fn allowance_works() { // No tokens in circulation. assert_eq!( - Assets::allowance(ASSET_ID, &BOB, &ALICE).encode(), - allowance(addr.clone(), ASSET_ID, BOB, ALICE).data[2..] + Assets::allowance(ASSET_ID, &BOB, &ALICE), + allowance(addr.clone(), ASSET_ID, BOB, ALICE) ); // Tokens in circulation. create_asset_mint_and_approve(ASSET_ID, addr.clone(), BOB, 100, ALICE, 50); assert_eq!( - Assets::allowance(ASSET_ID, &BOB, &ALICE).encode(), - allowance(addr, ASSET_ID, BOB, ALICE).data[2..] + Assets::allowance(ASSET_ID, &BOB, &ALICE), + allowance(addr, ASSET_ID, BOB, ALICE) ); }); } @@ -232,14 +262,11 @@ fn asset_exists_works() { ); // No tokens in circulation. - assert_eq!( - Assets::asset_exists(ASSET_ID).encode(), - asset_exists(addr.clone(), ASSET_ID).data[2..] - ); + assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr.clone(), ASSET_ID)); // Tokens in circulation. create_asset(ASSET_ID, addr.clone(), 1); - assert_eq!(Assets::asset_exists(ASSET_ID).encode(), asset_exists(addr, ASSET_ID).data[2..]); + assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr, ASSET_ID)); }); } @@ -299,11 +326,12 @@ fn set_metadata_works() { } // todo: errors: -// - AssetNotLive: when frozen or being destroyed -// - TokenErrors: https://github.com/paritytech/polkadot-sdk/blob/3977f389cce4a00fd7100f95262e0563622b9aa4/substrate/frame/assets/src/functions.rs#L125 +// - TokenErrors +// - Arithmetic +// - https://github.com/paritytech/polkadot-sdk/blob/3977f389cce4a00fd7100f95262e0563622b9aa4/substrate/frame/assets/src/functions.rs#L125 #[test] #[ignore] -fn mint_works() { +fn transfer_from_mint_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); let addr = instantiate( @@ -316,7 +344,7 @@ fn mint_works() { assert_eq!( decoded::(transfer_from( addr.clone(), - ASSET_ID, + 1, None, Some(BOB), amount, @@ -324,11 +352,11 @@ fn mint_works() { )), FungiblesError::Unknown ); - create_asset(ASSET_ID, ALICE, 2); + let asset = create_asset(1, ALICE, 2); assert_eq!( decoded::(transfer_from( addr.clone(), - ASSET_ID, + asset, None, Some(BOB), amount, @@ -339,7 +367,7 @@ fn mint_works() { assert_eq!( decoded::(transfer_from( addr.clone(), - ASSET_ID, + asset, None, Some(BOB), 1, @@ -347,16 +375,87 @@ fn mint_works() { )), FungiblesError::BelowMinimum ); - let asset = 2; - create_asset(asset, addr.clone(), 2); + let asset = create_asset(2, addr.clone(), 2); + freeze_asset(asset, addr.clone()); + assert_eq!( + decoded::(transfer_from( + addr.clone(), + asset, + None, + Some(BOB), + amount, + &[0u8] + )), + FungiblesError::AssetNotLive + ); + thaw_asset(asset, addr.clone()); let bob_balance_before_mint = Assets::balance(asset, &BOB); - let result = transfer_from(addr.clone(), asset, None, Some(BOB), 100 * UNIT, &[0u8]); + let result = transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8]); assert!(!result.did_revert(), "Contract reverted!"); let bob_balance_after_mint = Assets::balance(asset, &BOB); assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount); + start_destroy_asset(asset, addr.clone()); + assert_eq!( + decoded::(transfer_from( + addr.clone(), + asset, + None, + Some(BOB), + amount, + &[0u8] + )), + FungiblesError::AssetNotLive + ); }); } +// Todo: error: +// - Frozen: account is frozen, who do you freeze an account? +// - https://github.com/paritytech/polkadot-sdk/blob/2460cddf57660a88844d201f769eb17a7accce5a/substrate/frame/assets/src/functions.rs#L161 +// - ArithmeticError: Underflow, Overflow +// - https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/assets/src/functions.rs#L125 +// - #[test] #[ignore] -fn transfer_works() {} +fn transfer_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate( + "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", + INIT_VALUE, + vec![], + ); + let amount: Balance = 100 * UNIT; + + assert_eq!( + decoded::(transfer(addr.clone(), 1, BOB, amount,)), + FungiblesError::Unknown + ); + let asset = create_asset_and_mint_to(1, ALICE, addr.clone(), amount); + freeze_asset(asset, ALICE); + assert_eq!( + decoded::(transfer(addr.clone(), asset, BOB, amount,)), + FungiblesError::AssetNotLive + ); + thaw_asset(asset, ALICE); + assert_eq!( + decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), + FungiblesError::InsufficientBalance + ); + // Errors due to ED. Could be Belowminimum + assert_eq!( + decoded::(transfer(addr.clone(), asset, BOB, amount)), + FungiblesError::InsufficientBalance + ); + let bob_balance_before_mint = Assets::balance(asset, &BOB); + let result = transfer(addr.clone(), asset, BOB, amount / 2); + assert!(!result.did_revert(), "Contract reverted!"); + let bob_balance_after_mint = Assets::balance(asset, &BOB); + assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount / 2); + start_destroy_asset(asset, ALICE); + assert_eq!( + decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), + FungiblesError::AssetNotLive + ); + }); +} diff --git a/runtime/devnet/src/extensions/tests/mod.rs b/runtime/devnet/src/extensions/tests/mod.rs index 03cda2cd..cbd13d62 100644 --- a/runtime/devnet/src/extensions/tests/mod.rs +++ b/runtime/devnet/src/extensions/tests/mod.rs @@ -7,6 +7,7 @@ use sp_runtime::{traits::Hash, AccountId32, BuildStorage}; mod local_fungibles; type Balance = u128; +type AssetId = u32; const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug; const ALICE: AccountId32 = AccountId32::new([1_u8; 32]); From ecc82e266d772bbe6d57b3feb9049e3228152f8a Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Thu, 6 Jun 2024 11:51:10 +0200 Subject: [PATCH 06/76] chore: remove polkadot launch --- polkadot-launch/config.json | 39 ------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 polkadot-launch/config.json diff --git a/polkadot-launch/config.json b/polkadot-launch/config.json deleted file mode 100644 index f03f983a..00000000 --- a/polkadot-launch/config.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "relaychain": { - "bin": "../../polkadot/target/release/polkadot", - "chain": "rococo-local", - "nodes": [ - { - "name": "alice", - "wsPort": 9944, - "port": 30444 - }, - { - "name": "bob", - "wsPort": 9955, - "port": 30555 - } - ] - }, - "parachains": [ - { - "bin": "../target/release/polkadot-parachain", - "id": "200", - "balance": "1000000000000000000000", - "nodes": [ - { - "wsPort": 9988, - "name": "alice", - "port": 31200, - "flags": [ - "--force-authoring", - "--", - "--execution=wasm" - ] - } - ] - } - ], - "types": { - } -} From e81f78a957ff3773bc45e93a61f8bf3c17538bc0 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Thu, 6 Jun 2024 14:04:31 +0200 Subject: [PATCH 07/76] # This is a combination of 8 commits. # This is the 1st commit message: refactor: general # This is the commit message #2: init # This is the commit message #3: begin refactor # This is the commit message #4: refactor: error handling # This is the commit message #5: tests: add error handling tests # This is the commit message #6: WIP # This is the commit message #7: finalise error handling # This is the commit message #8: refactor: easier review --- Cargo.lock | 2723 ++++++++++------ pop-api/examples/fungibles/expanded.rs | 2766 ----------------- pop-api/examples/fungibles/lib.rs | 94 +- pop-api/src/error.rs | 255 ++ pop-api/src/lib.rs | 58 +- pop-api/src/v0/assets/fungibles.rs | 610 ---- pop-api/src/v0/assets/mod.rs | 3 +- pop-api/src/v0/assets/pallets/assets.rs | 491 +++ pop-api/src/v0/assets/pallets/mod.rs | 1 + pop-api/src/v0/assets/use_cases/fungibles.rs | 372 +++ pop-api/src/v0/assets/use_cases/mod.rs | 1 + pop-api/src/v0/balances.rs | 21 +- pop-api/src/v0/contracts.rs | 152 - pop-api/src/v0/cross_chain/mod.rs | 15 +- pop-api/src/v0/dispatch_error.rs | 57 - pop-api/src/v0/mod.rs | 4 +- pop-api/src/v0/nfts.rs | 15 +- primitives/src/storage_keys.rs | 3 - runtime/devnet/Cargo.toml | 2 + .../src/{extensions/mod.rs => extensions.rs} | 45 +- runtime/devnet/src/extensions/tests/mod.rs | 89 - runtime/devnet/src/lib.rs | 2 + .../{extensions => }/tests/local_fungibles.rs | 199 +- runtime/devnet/src/tests/mod.rs | 262 ++ 24 files changed, 3335 insertions(+), 4905 deletions(-) delete mode 100644 pop-api/examples/fungibles/expanded.rs create mode 100644 pop-api/src/error.rs delete mode 100644 pop-api/src/v0/assets/fungibles.rs create mode 100644 pop-api/src/v0/assets/pallets/assets.rs create mode 100644 pop-api/src/v0/assets/pallets/mod.rs create mode 100644 pop-api/src/v0/assets/use_cases/fungibles.rs create mode 100644 pop-api/src/v0/assets/use_cases/mod.rs delete mode 100644 pop-api/src/v0/contracts.rs delete mode 100644 pop-api/src/v0/dispatch_error.rs rename runtime/devnet/src/{extensions/mod.rs => extensions.rs} (97%) delete mode 100644 runtime/devnet/src/extensions/tests/mod.rs rename runtime/devnet/src/{extensions => }/tests/local_fungibles.rs (70%) create mode 100644 runtime/devnet/src/tests/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 17cc70e5..77c563fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -343,7 +343,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94893f1e0c6eeab764ade8dc4c0db24caf4fe7cbbaafc0eba0a9030f447b5185" dependencies = [ "num-traits", - "rand", + "rand 0.8.5", ] [[package]] @@ -358,6 +358,12 @@ version = "6.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f840fb7195bcfc5e17ea40c26e5ce6d5b9ce5d584466e17703209657e459ae0" +[[package]] +name = "array-init" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d62b7694a562cdf5a74227903507c56ab2cc8bdd1f781ed5cb4cf9c9f810bfc" + [[package]] name = "arrayref" version = "0.3.7" @@ -373,6 +379,12 @@ dependencies = [ "nodrop", ] +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + [[package]] name = "arrayvec" version = "0.7.4" @@ -404,7 +416,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.109", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -486,17 +498,17 @@ dependencies = [ "sp-api", "sp-block-builder", "sp-consensus-aura", - "sp-core", + "sp-core 29.0.0", "sp-genesis-builder", "sp-inherents", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", - "sp-std", - "sp-storage", + "sp-std 14.0.0", + "sp-storage 20.0.0", "sp-transaction-pool", "sp-version", - "sp-weights", + "sp-weights 28.0.0", "staging-parachain-info", "staging-xcm", "staging-xcm-builder", @@ -525,9 +537,9 @@ dependencies = [ "parachains-common", "parachains-runtimes-test-utils", "parity-scale-codec", - "sp-io", - "sp-runtime", - "sp-std", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "staging-parachain-info", "staging-xcm", "staging-xcm-builder", @@ -551,8 +563,8 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -909,7 +921,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93f2635620bf0b9d4576eb7bb9a38a55df78bd1205d26fa994b25911a69f212f" dependencies = [ "bitcoin_hashes", - "rand", + "rand 0.8.5", "rand_core 0.6.4", "serde", "unicode-normalization", @@ -1012,6 +1024,18 @@ dependencies = [ "constant_time_eq 0.3.0", ] +[[package]] +name = "block-buffer" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" +dependencies = [ + "block-padding", + "byte-tools", + "byteorder", + "generic-array 0.12.4", +] + [[package]] name = "block-buffer" version = "0.9.0" @@ -1030,6 +1054,15 @@ dependencies = [ "generic-array 0.14.7", ] +[[package]] +name = "block-padding" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" +dependencies = [ + "byte-tools", +] + [[package]] name = "blocking" version = "1.5.1" @@ -1116,7 +1149,7 @@ dependencies = [ "frame-system", "polkadot-primitives", "sp-api", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -1130,8 +1163,8 @@ dependencies = [ "bp-runtime", "frame-support", "sp-api", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -1145,8 +1178,8 @@ dependencies = [ "bp-runtime", "frame-support", "sp-api", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -1162,9 +1195,9 @@ dependencies = [ "scale-info", "serde", "sp-consensus-grandpa", - "sp-core", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -1179,8 +1212,8 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-std", + "sp-core 29.0.0", + "sp-std 14.0.0", ] [[package]] @@ -1196,9 +1229,9 @@ dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -1215,9 +1248,9 @@ dependencies = [ "parity-util-mem", "scale-info", "serde", - "sp-core", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -1231,8 +1264,8 @@ dependencies = [ "frame-support", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -1250,13 +1283,13 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", - "sp-state-machine", - "sp-std", - "sp-trie", - "trie-db", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-std 14.0.0", + "sp-trie 30.0.0", + "trie-db 0.28.0", ] [[package]] @@ -1269,15 +1302,15 @@ dependencies = [ "bp-parachains", "bp-polkadot-core", "bp-runtime", - "ed25519-dalek", + "ed25519-dalek 2.1.1", "finality-grandpa", "parity-scale-codec", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-grandpa", - "sp-core", - "sp-runtime", - "sp-std", - "sp-trie", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-trie 30.0.0", ] [[package]] @@ -1286,7 +1319,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6663e0179d475e30cfcf28cf597cdc8f4bb1c2c39a557b4cbe0057db0657fb67" dependencies = [ - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -1297,8 +1330,8 @@ checksum = "86ff4abe93be7bc1663adc41817b1aa3476fbec953ce361537419924310d5dd4" dependencies = [ "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", ] [[package]] @@ -1328,11 +1361,11 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", - "sp-trie", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-trie 30.0.0", "staging-xcm", "staging-xcm-builder", ] @@ -1745,6 +1778,26 @@ dependencies = [ "tiny-keccak", ] +[[package]] +name = "const_env" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e9e4f72c6e3398ca6da372abd9affd8f89781fe728869bbf986206e9af9627e" +dependencies = [ + "const_env_impl", +] + +[[package]] +name = "const_env_impl" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a4f51209740b5e1589e702b3044cdd4562cef41b6da404904192ffffb852d62" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "constant_time_eq" version = "0.1.5" @@ -2044,8 +2097,8 @@ dependencies = [ "sc-client-api", "sc-service", "sp-blockchain", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "url", ] @@ -2068,8 +2121,8 @@ dependencies = [ "sc-client-api", "sp-api", "sp-consensus", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "tracing", ] @@ -2101,16 +2154,16 @@ dependencies = [ "sc-telemetry", "schnellru", "sp-api", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-block-builder", "sp-blockchain", "sp-consensus", "sp-consensus-aura", - "sp-core", + "sp-core 29.0.0", "sp-inherents", - "sp-keystore", - "sp-runtime", - "sp-state-machine", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "sp-timestamp", "substrate-prometheus-endpoint", "tracing", @@ -2138,10 +2191,10 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-consensus-slots", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-timestamp", - "sp-trie", + "sp-trie 30.0.0", "substrate-prometheus-endpoint", "tracing", ] @@ -2157,8 +2210,8 @@ dependencies = [ "cumulus-primitives-parachain-inherent", "sp-consensus", "sp-inherents", - "sp-runtime", - "sp-state-machine", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "thiserror", ] @@ -2180,9 +2233,9 @@ dependencies = [ "sc-client-api", "sp-blockchain", "sp-consensus", - "sp-core", - "sp-runtime", - "sp-state-machine", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "tracing", ] @@ -2203,11 +2256,11 @@ dependencies = [ "sp-api", "sp-crypto-hashing", "sp-inherents", - "sp-runtime", - "sp-state-machine", - "sp-std", - "sp-storage", - "sp-trie", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-std 14.0.0", + "sp-storage 20.0.0", + "sp-trie 30.0.0", "tracing", ] @@ -2227,12 +2280,12 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-overseer", "polkadot-primitives", - "rand", + "rand 0.8.5", "sc-client-api", "sc-consensus", "sp-consensus", "sp-maybe-compressed-blob", - "sp-runtime", + "sp-runtime 32.0.0", "tracing", ] @@ -2268,8 +2321,8 @@ dependencies = [ "sp-api", "sp-blockchain", "sp-consensus", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-transaction-pool", ] @@ -2286,10 +2339,10 @@ dependencies = [ "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-aura", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -2315,17 +2368,17 @@ dependencies = [ "polkadot-runtime-common", "polkadot-runtime-parachains", "scale-info", - "sp-core", - "sp-externalities", + "sp-core 29.0.0", + "sp-externalities 0.26.0", "sp-inherents", - "sp-io", - "sp-runtime", - "sp-state-machine", - "sp-std", - "sp-trie", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-std 14.0.0", + "sp-trie 30.0.0", "sp-version", "staging-xcm", - "trie-db", + "trie-db 0.28.0", ] [[package]] @@ -2351,8 +2404,8 @@ dependencies = [ "frame-system", "pallet-session", "parity-scale-codec", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -2366,9 +2419,9 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", - "sp-std", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "staging-xcm", ] @@ -2390,10 +2443,10 @@ dependencies = [ "polkadot-runtime-common", "polkadot-runtime-parachains", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "staging-xcm", "staging-xcm-executor", ] @@ -2409,8 +2462,8 @@ dependencies = [ "polkadot-primitives", "sp-api", "sp-consensus-aura", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -2425,9 +2478,9 @@ dependencies = [ "polkadot-primitives", "scale-info", "sp-api", - "sp-runtime", - "sp-std", - "sp-trie", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-trie 30.0.0", "staging-xcm", ] @@ -2441,10 +2494,10 @@ dependencies = [ "cumulus-primitives-core", "parity-scale-codec", "scale-info", - "sp-core", + "sp-core 29.0.0", "sp-inherents", - "sp-std", - "sp-trie", + "sp-std 14.0.0", + "sp-trie 30.0.0", ] [[package]] @@ -2453,9 +2506,9 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1b74f9141190b9f4bf96a947ade46da64097b77f1ebfa8d611c81724250e119" dependencies = [ - "sp-externalities", - "sp-runtime-interface", - "sp-trie", + "sp-externalities 0.26.0", + "sp-runtime-interface 25.0.0", + "sp-trie 30.0.0", ] [[package]] @@ -2471,9 +2524,9 @@ dependencies = [ "parity-scale-codec", "polkadot-runtime-common", "polkadot-runtime-parachains", - "sp-io", - "sp-runtime", - "sp-std", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -2499,9 +2552,9 @@ dependencies = [ "sc-tracing", "sp-api", "sp-consensus", - "sp-core", - "sp-runtime", - "sp-state-machine", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", ] [[package]] @@ -2519,7 +2572,7 @@ dependencies = [ "sc-client-api", "sp-api", "sp-blockchain", - "sp-state-machine", + "sp-state-machine 0.36.0", "thiserror", ] @@ -2559,7 +2612,7 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-consensus-babe", - "sp-runtime", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "tokio", "tracing", @@ -2581,7 +2634,7 @@ dependencies = [ "parity-scale-codec", "pin-project", "polkadot-overseer", - "rand", + "rand 0.8.5", "sc-client-api", "sc-rpc-api", "sc-service", @@ -2593,10 +2646,10 @@ dependencies = [ "sp-api", "sp-authority-discovery", "sp-consensus-babe", - "sp-core", - "sp-runtime", - "sp-state-machine", - "sp-storage", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-storage 20.0.0", "sp-version", "thiserror", "tokio", @@ -2614,10 +2667,23 @@ dependencies = [ "cumulus-primitives-core", "parity-scale-codec", "polkadot-primitives", - "sp-runtime", - "sp-state-machine", - "sp-std", - "sp-trie", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-std 14.0.0", + "sp-trie 30.0.0", +] + +[[package]] +name = "curve25519-dalek" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a9b85542f99a2dfa2a1b8e192662741c9859a846b296bef1c92ef9b58b5a216" +dependencies = [ + "byteorder", + "digest 0.8.1", + "rand_core 0.5.1", + "subtle 2.5.0", + "zeroize", ] [[package]] @@ -3064,10 +3130,19 @@ dependencies = [ "digest 0.10.7", "elliptic-curve", "rfc6979", - "signature", + "signature 2.2.0", "spki", ] +[[package]] +name = "ed25519" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91cff35c70bba8a626e3185d8cd48cc11b5437e1a5bcd15b9b5fa3c64b6dfee7" +dependencies = [ + "signature 1.6.4", +] + [[package]] name = "ed25519" version = "2.2.3" @@ -3075,7 +3150,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" dependencies = [ "pkcs8", - "signature", + "signature 2.2.0", +] + +[[package]] +name = "ed25519-dalek" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d" +dependencies = [ + "curve25519-dalek 3.2.0", + "ed25519 1.5.3", + "sha2 0.9.9", + "zeroize", ] [[package]] @@ -3085,7 +3172,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871" dependencies = [ "curve25519-dalek 4.1.2", - "ed25519", + "ed25519 2.2.3", "rand_core 0.6.4", "serde", "sha2 0.10.8", @@ -3114,7 +3201,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d9ce6874da5d4415896cd45ffbc4d1cfc0c4f9c079427bd870742c30f2f65a9" dependencies = [ "curve25519-dalek 4.1.2", - "ed25519", + "ed25519 2.2.3", "hashbrown 0.14.3", "hex", "rand_core 0.6.4", @@ -3174,8 +3261,8 @@ dependencies = [ "sc-consensus-grandpa", "sp-authority-discovery", "sp-consensus-babe", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "staging-xcm", "xcm-emulator", ] @@ -3422,6 +3509,12 @@ dependencies = [ "syn 2.0.58", ] +[[package]] +name = "fake-simd" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" + [[package]] name = "fallible-iterator" version = "0.2.0" @@ -3539,7 +3632,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534" dependencies = [ "byteorder", - "rand", + "rand 0.8.5", "rustc-hex", "static_assertions", ] @@ -3616,13 +3709,13 @@ dependencies = [ "scale-info", "serde", "sp-api", - "sp-application-crypto", - "sp-core", - "sp-io", - "sp-runtime", - "sp-runtime-interface", - "sp-std", - "sp-storage", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-runtime-interface 25.0.0", + "sp-std 14.0.0", + "sp-storage 20.0.0", "static_assertions", ] @@ -3647,7 +3740,7 @@ dependencies = [ "linked-hash-map", "log", "parity-scale-codec", - "rand", + "rand 0.8.5", "rand_pcg", "sc-block-builder", "sc-cli", @@ -3660,17 +3753,17 @@ dependencies = [ "serde_json", "sp-api", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-database", - "sp-externalities", + "sp-externalities 0.26.0", "sp-inherents", - "sp-io", - "sp-keystore", - "sp-runtime", - "sp-state-machine", - "sp-storage", - "sp-trie", - "sp-wasm-interface", + "sp-io 31.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-storage 20.0.0", + "sp-trie 30.0.0", + "sp-wasm-interface 20.0.0", "thiserror", "thousands", ] @@ -3698,11 +3791,11 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-core", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", "sp-npos-elections", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -3717,11 +3810,11 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", - "sp-tracing", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-tracing 16.0.0", ] [[package]] @@ -3759,11 +3852,11 @@ dependencies = [ "log", "parity-scale-codec", "serde", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-io", - "sp-runtime", - "sp-state-machine", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "spinners", "substrate-rpc-client", "tokio", @@ -3794,20 +3887,20 @@ dependencies = [ "serde_json", "smallvec", "sp-api", - "sp-arithmetic", - "sp-core", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", "sp-crypto-hashing-proc-macro", - "sp-debug-derive", + "sp-debug-derive 14.0.0", "sp-genesis-builder", "sp-inherents", - "sp-io", + "sp-io 31.0.0", "sp-metadata-ir", - "sp-runtime", + "sp-runtime 32.0.0", "sp-staking", - "sp-state-machine", - "sp-std", - "sp-tracing", - "sp-weights", + "sp-state-machine 0.36.0", + "sp-std 14.0.0", + "sp-tracing 16.0.0", + "sp-weights 28.0.0", "static_assertions", "tt-call", ] @@ -3869,12 +3962,12 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "sp-version", - "sp-weights", + "sp-weights 28.0.0", ] [[package]] @@ -3888,9 +3981,9 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -3912,8 +4005,8 @@ dependencies = [ "frame-support", "parity-scale-codec", "sp-api", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -4163,7 +4256,7 @@ version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ea1015b5a70616b688dc230cfe50c8af89d972cb132d5a622814d29773b10b9" dependencies = [ - "rand", + "rand 0.8.5", "rand_core 0.6.4", ] @@ -4669,6 +4762,206 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "ink" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d4a862aedbfda93175ddf75c9aaa2ae4c4b39ee5cee06c16d50bccce05bf5c7" +dependencies = [ + "derive_more", + "ink_env", + "ink_macro", + "ink_metadata", + "ink_prelude", + "ink_primitives", + "ink_storage", + "pallet-contracts-uapi-next", + "parity-scale-codec", + "scale-info", +] + +[[package]] +name = "ink_allocator" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cee56055bac6d928d425e944c5f3b69baa33c9635822fd1c00cd4afc70fde3e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "ink_codegen" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70a1f8473fa09e0f9b6f3cb3f8d18c07c14ebf9ea1f7cdfee270f009d45ee8e9" +dependencies = [ + "blake2 0.10.6", + "derive_more", + "either", + "heck 0.4.1", + "impl-serde", + "ink_ir", + "ink_primitives", + "itertools 0.12.1", + "parity-scale-codec", + "proc-macro2", + "quote", + "serde", + "serde_json", + "syn 2.0.58", +] + +[[package]] +name = "ink_engine" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f357e2e867f4e222ffc4015a6e61d1073548de89f70a4e36a8b0385562777fa" +dependencies = [ + "blake2 0.10.6", + "derive_more", + "ink_primitives", + "pallet-contracts-uapi-next", + "parity-scale-codec", + "secp256k1 0.28.2", + "sha2 0.10.8", + "sha3", +] + +[[package]] +name = "ink_env" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42cec50b7e4f8406aab25801b015d3802a52d76cfbe48ce11cfb4200fa88e296" +dependencies = [ + "blake2 0.10.6", + "cfg-if", + "const_env", + "derive_more", + "ink_allocator", + "ink_engine", + "ink_prelude", + "ink_primitives", + "ink_storage_traits", + "num-traits", + "pallet-contracts-uapi-next", + "parity-scale-codec", + "paste", + "rlibc", + "scale-decode", + "scale-encode", + "scale-info", + "schnorrkel 0.11.4", + "secp256k1 0.28.2", + "sha2 0.10.8", + "sha3", + "static_assertions", +] + +[[package]] +name = "ink_ir" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b1ad2975551c4ed800af971289ed6d2c68ac41ffc03a42010b3e01d7360dfb2" +dependencies = [ + "blake2 0.10.6", + "either", + "impl-serde", + "ink_prelude", + "itertools 0.12.1", + "proc-macro2", + "quote", + "syn 2.0.58", +] + +[[package]] +name = "ink_macro" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aee1a546f37eae3b3cd223832d31702033c5369dcfa3405899587c110a7908d3" +dependencies = [ + "ink_codegen", + "ink_ir", + "ink_primitives", + "parity-scale-codec", + "proc-macro2", + "quote", + "syn 2.0.58", + "synstructure 0.13.1", +] + +[[package]] +name = "ink_metadata" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a98fcc0ff9292ff68c7ee7b84c93533c9ff13859ec3b148faa822e2da9954fe6" +dependencies = [ + "derive_more", + "impl-serde", + "ink_prelude", + "ink_primitives", + "linkme", + "parity-scale-codec", + "scale-info", + "schemars", + "serde", +] + +[[package]] +name = "ink_prelude" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea1734d058c80aa72e59c8ae75624fd8a51791efba21469f273156c0f4cad5c9" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "ink_primitives" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11ec35ef7f45e67a53b6142d7e7f18e6d9292d76c3a2a1da14cf8423e481813d" +dependencies = [ + "derive_more", + "ink_prelude", + "parity-scale-codec", + "scale-decode", + "scale-encode", + "scale-info", + "xxhash-rust", +] + +[[package]] +name = "ink_storage" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbdb04cad74df858c05bc9cb6f30bbf12da33c3e2cb7ca211749c001fa761aa9" +dependencies = [ + "array-init", + "cfg-if", + "derive_more", + "ink_env", + "ink_metadata", + "ink_prelude", + "ink_primitives", + "ink_storage_traits", + "pallet-contracts-uapi-next", + "parity-scale-codec", + "scale-info", +] + +[[package]] +name = "ink_storage_traits" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83ce49e3d2935fc1ec3e73117119712b187d3123339f6a31624e92f75fa2293d" +dependencies = [ + "ink_metadata", + "ink_prelude", + "ink_primitives", + "parity-scale-codec", + "scale-info", +] + [[package]] name = "inout" version = "0.1.3" @@ -4726,8 +5019,8 @@ dependencies = [ "sp-consensus-babe", "sp-consensus-beefy", "sp-consensus-grandpa", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "staging-xcm", "staging-xcm-executor", "tracing-subscriber 0.3.18", @@ -4923,7 +5216,7 @@ dependencies = [ "hyper", "jsonrpsee-types 0.20.3", "parking_lot 0.12.1", - "rand", + "rand 0.8.5", "rustc-hash", "serde", "serde_json", @@ -5256,7 +5549,7 @@ dependencies = [ "parking_lot 0.12.1", "pin-project", "quick-protobuf", - "rand", + "rand 0.8.5", "rw-stream-sink", "smallvec", "thiserror", @@ -5307,12 +5600,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "276bb57e7af15d8f100d3c11cbdd32c6752b7eef4ba7a18ecf464972c07abcce" dependencies = [ "bs58 0.4.0", - "ed25519-dalek", + "ed25519-dalek 2.1.1", "log", "multiaddr", "multihash 0.17.0", "quick-protobuf", - "rand", + "rand 0.8.5", "sha2 0.10.8", "thiserror", "zeroize", @@ -5337,7 +5630,7 @@ dependencies = [ "libp2p-swarm", "log", "quick-protobuf", - "rand", + "rand 0.8.5", "sha2 0.10.8", "smallvec", "thiserror", @@ -5359,7 +5652,7 @@ dependencies = [ "libp2p-identity", "libp2p-swarm", "log", - "rand", + "rand 0.8.5", "smallvec", "socket2 0.4.10", "tokio", @@ -5395,7 +5688,7 @@ dependencies = [ "log", "once_cell", "quick-protobuf", - "rand", + "rand 0.8.5", "sha2 0.10.8", "snow", "static_assertions", @@ -5417,7 +5710,7 @@ dependencies = [ "libp2p-core", "libp2p-swarm", "log", - "rand", + "rand 0.8.5", "void", ] @@ -5437,7 +5730,7 @@ dependencies = [ "log", "parking_lot 0.12.1", "quinn-proto", - "rand", + "rand 0.8.5", "rustls 0.20.9", "thiserror", "tokio", @@ -5455,7 +5748,7 @@ dependencies = [ "libp2p-core", "libp2p-identity", "libp2p-swarm", - "rand", + "rand 0.8.5", "smallvec", ] @@ -5474,7 +5767,7 @@ dependencies = [ "libp2p-identity", "libp2p-swarm-derive", "log", - "rand", + "rand 0.8.5", "smallvec", "tokio", "void", @@ -5610,7 +5903,7 @@ dependencies = [ "libsecp256k1-core", "libsecp256k1-gen-ecmult", "libsecp256k1-gen-genmult", - "rand", + "rand 0.8.5", "serde", "sha2 0.9.9", "typenum", @@ -5680,6 +5973,26 @@ dependencies = [ "linked-hash-map", ] +[[package]] +name = "linkme" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccb76662d78edc9f9bf56360d6919bdacc8b7761227727e5082f128eeb90bbf5" +dependencies = [ + "linkme-impl", +] + +[[package]] +name = "linkme-impl" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dccda732e04fa3baf2e17cf835bfe2601c7c2edafd64417c627dabae3a8cda" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.58", +] + [[package]] name = "linregress" version = "0.5.3" @@ -5951,6 +6264,18 @@ dependencies = [ "hash-db", ] +[[package]] +name = "merlin" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e261cf0f8b3c42ded9f7d2bb59dea03aa52bc8a1cbc7482f9fc3fd1229d3b42" +dependencies = [ + "byteorder", + "keccak", + "rand_core 0.5.1", + "zeroize", +] + [[package]] name = "merlin" version = "3.0.0" @@ -5970,7 +6295,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69672161530e8aeca1d1400fbf3f1a1747ff60ea604265a4e906c2442df20532" dependencies = [ "futures", - "rand", + "rand 0.8.5", "thrift", ] @@ -6017,7 +6342,7 @@ dependencies = [ "lioness", "log", "parking_lot 0.12.1", - "rand", + "rand 0.8.5", "rand_chacha 0.3.1", "rand_distr", "subtle 2.5.0", @@ -6040,9 +6365,9 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-consensus-beefy", - "sp-core", + "sp-core 29.0.0", "sp-mmr-primitives", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -6056,9 +6381,9 @@ dependencies = [ "serde", "sp-api", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-mmr-primitives", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -6189,7 +6514,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.109", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -6214,7 +6539,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.109", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -6270,7 +6595,7 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7bddcd3bf5144b6392de80e04c347cd7fab2508f6df16a85fc496ecd5cec39bc" dependencies = [ - "rand", + "rand 0.8.5", ] [[package]] @@ -6598,11 +6923,11 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-arithmetic", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6617,8 +6942,8 @@ dependencies = [ "pallet-transaction-payment", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6632,9 +6957,9 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6650,10 +6975,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6668,9 +6993,9 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6685,10 +7010,10 @@ dependencies = [ "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-aura", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6702,10 +7027,10 @@ dependencies = [ "pallet-session", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-authority-discovery", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6719,8 +7044,8 @@ dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6738,14 +7063,14 @@ dependencies = [ "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-babe", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-session", "sp-staking", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -6764,11 +7089,11 @@ dependencies = [ "pallet-balances", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", - "sp-tracing", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-tracing 16.0.0", ] [[package]] @@ -6784,8 +7109,8 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6803,10 +7128,10 @@ dependencies = [ "scale-info", "serde", "sp-consensus-beefy", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", "sp-staking", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -6828,11 +7153,11 @@ dependencies = [ "serde", "sp-api", "sp-consensus-beefy", - "sp-core", - "sp-io", - "sp-runtime", - "sp-state-machine", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-std 14.0.0", ] [[package]] @@ -6848,10 +7173,10 @@ dependencies = [ "pallet-treasury", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6871,9 +7196,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-consensus-grandpa", - "sp-runtime", - "sp-std", - "sp-trie", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-trie 30.0.0", ] [[package]] @@ -6891,8 +7216,8 @@ dependencies = [ "num-traits", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6912,9 +7237,9 @@ dependencies = [ "pallet-bridge-grandpa", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-std", - "sp-trie", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-trie 30.0.0", ] [[package]] @@ -6933,9 +7258,9 @@ dependencies = [ "pallet-bridge-messages", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-runtime", - "sp-std", + "sp-arithmetic 24.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6950,10 +7275,10 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-core", - "sp-runtime", - "sp-std", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6970,10 +7295,10 @@ dependencies = [ "pallet-treasury", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -6989,11 +7314,11 @@ dependencies = [ "pallet-authorship", "pallet-session", "parity-scale-codec", - "rand", + "rand 0.8.5", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-staking", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -7008,10 +7333,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7031,16 +7356,16 @@ dependencies = [ "pallet-contracts-proc-macro", "pallet-contracts-uapi", "parity-scale-codec", - "rand", + "rand 0.8.5", "rand_pcg", "scale-info", "serde", "smallvec", "sp-api", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "staging-xcm", "staging-xcm-builder", "wasm-instrument", @@ -7071,6 +7396,17 @@ dependencies = [ "scale-info", ] +[[package]] +name = "pallet-contracts-uapi-next" +version = "6.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd549c16296ea5b2eb7c65c56aba548b286c1be4d7675b424ff6ccb8319c97a9" +dependencies = [ + "bitflags 1.3.2", + "paste", + "polkavm-derive", +] + [[package]] name = "pallet-conviction-voting" version = "29.0.0" @@ -7084,9 +7420,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-io", - "sp-runtime", - "sp-std", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7102,10 +7438,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7121,14 +7457,14 @@ dependencies = [ "log", "pallet-election-provider-support-benchmarking", "parity-scale-codec", - "rand", + "rand 0.8.5", "scale-info", - "sp-arithmetic", - "sp-core", - "sp-io", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", "sp-npos-elections", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "strum 0.24.1", ] @@ -7143,8 +7479,8 @@ dependencies = [ "frame-system", "parity-scale-codec", "sp-npos-elections", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7159,12 +7495,12 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", + "sp-core 29.0.0", + "sp-io 31.0.0", "sp-npos-elections", - "sp-runtime", + "sp-runtime 32.0.0", "sp-staking", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -7181,10 +7517,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-staking", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -7201,14 +7537,14 @@ dependencies = [ "pallet-session", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-grandpa", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-session", "sp-staking", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -7224,9 +7560,9 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", - "sp-std", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7242,12 +7578,12 @@ dependencies = [ "pallet-authorship", "parity-scale-codec", "scale-info", - "sp-application-crypto", - "sp-core", - "sp-io", - "sp-runtime", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-staking", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -7261,11 +7597,11 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", + "sp-core 29.0.0", + "sp-io 31.0.0", "sp-keyring", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7280,10 +7616,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7299,12 +7635,12 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", - "sp-weights", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-weights 28.0.0", ] [[package]] @@ -7319,11 +7655,11 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", + "sp-core 29.0.0", + "sp-io 31.0.0", "sp-mmr-primitives", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7338,9 +7674,9 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", - "sp-std", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7357,8 +7693,8 @@ dependencies = [ "pallet-nfts", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7374,10 +7710,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7389,7 +7725,7 @@ dependencies = [ "pallet-nfts", "parity-scale-codec", "sp-api", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -7403,10 +7739,10 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-core", - "sp-runtime", - "sp-std", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7421,12 +7757,12 @@ dependencies = [ "pallet-balances", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-staking", - "sp-std", - "sp-tracing", + "sp-std 14.0.0", + "sp-tracing 16.0.0", ] [[package]] @@ -7444,10 +7780,10 @@ dependencies = [ "pallet-staking", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-runtime-interface", + "sp-runtime 32.0.0", + "sp-runtime-interface 25.0.0", "sp-staking", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -7459,7 +7795,7 @@ dependencies = [ "pallet-nomination-pools", "parity-scale-codec", "sp-api", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -7475,9 +7811,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-runtime", + "sp-runtime 32.0.0", "sp-staking", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -7500,9 +7836,9 @@ dependencies = [ "pallet-staking", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-staking", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -7517,10 +7853,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7534,9 +7870,9 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", - "sp-std", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7552,11 +7888,11 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7570,9 +7906,9 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", - "sp-std", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7589,10 +7925,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-arithmetic", - "sp-io", - "sp-runtime", - "sp-std", + "sp-arithmetic 24.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7605,10 +7941,10 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7624,10 +7960,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", - "sp-std", - "sp-weights", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-weights 28.0.0", ] [[package]] @@ -7643,14 +7979,14 @@ dependencies = [ "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-session", "sp-staking", - "sp-state-machine", - "sp-std", - "sp-trie", + "sp-state-machine 0.36.0", + "sp-std 14.0.0", + "sp-trie 30.0.0", ] [[package]] @@ -7665,10 +8001,10 @@ dependencies = [ "pallet-session", "pallet-staking", "parity-scale-codec", - "rand", - "sp-runtime", + "rand 0.8.5", + "sp-runtime 32.0.0", "sp-session", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -7684,10 +8020,10 @@ dependencies = [ "parity-scale-codec", "rand_chacha 0.2.2", "scale-info", - "sp-arithmetic", - "sp-io", - "sp-runtime", - "sp-std", + "sp-arithmetic 24.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7707,11 +8043,11 @@ dependencies = [ "rand_chacha 0.2.2", "scale-info", "serde", - "sp-application-crypto", - "sp-io", - "sp-runtime", + "sp-application-crypto 31.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-staking", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -7733,7 +8069,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "505d45e08bad052f55fb51f00a6b6244d23ee46ffdc8091f6cddf4e3a880319d" dependencies = [ "log", - "sp-arithmetic", + "sp-arithmetic 24.0.0", ] [[package]] @@ -7759,10 +8095,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7777,9 +8113,9 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", - "sp-std", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7796,10 +8132,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-inherents", - "sp-io", - "sp-runtime", - "sp-std", - "sp-storage", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-storage 20.0.0", "sp-timestamp", ] @@ -7817,10 +8153,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7834,10 +8170,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7851,10 +8187,10 @@ dependencies = [ "parity-scale-codec", "sp-api", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-rpc", - "sp-runtime", - "sp-weights", + "sp-runtime 32.0.0", + "sp-weights 28.0.0", ] [[package]] @@ -7866,8 +8202,8 @@ dependencies = [ "pallet-transaction-payment", "parity-scale-codec", "sp-api", - "sp-runtime", - "sp-weights", + "sp-runtime 32.0.0", + "sp-weights 28.0.0", ] [[package]] @@ -7885,9 +8221,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7902,8 +8238,8 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7917,10 +8253,10 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7935,8 +8271,8 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7951,8 +8287,8 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -7970,10 +8306,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -7991,9 +8327,9 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", - "sp-std", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -8012,9 +8348,9 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "staging-xcm", "staging-xcm-builder", ] @@ -8041,10 +8377,10 @@ dependencies = [ "polkadot-primitives", "scale-info", "sp-consensus-aura", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "staging-parachain-info", "staging-xcm", "staging-xcm-executor", @@ -8071,11 +8407,11 @@ dependencies = [ "parity-scale-codec", "polkadot-parachain-primitives", "sp-consensus-aura", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", - "sp-tracing", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-tracing 16.0.0", "staging-parachain-info", "staging-xcm", "staging-xcm-executor", @@ -8103,7 +8439,7 @@ dependencies = [ "lz4", "memmap2 0.5.10", "parking_lot 0.12.1", - "rand", + "rand 0.8.5", "siphasher 0.3.11", "snap", "winapi", @@ -8168,7 +8504,7 @@ checksum = "f557c32c6d268a07c921471619c0295f5efad3a0e76d4f97a05c091a51d110b2" dependencies = [ "proc-macro2", "syn 1.0.109", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -8252,6 +8588,15 @@ dependencies = [ "crypto-mac 0.11.0", ] +[[package]] +name = "pbkdf2" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83a0692ec44e4cf1ef28ca317f14f8f07da2d95ec3fa01f86e4467b725e60917" +dependencies = [ + "digest 0.10.7", +] + [[package]] name = "pbkdf2" version = "0.12.2" @@ -8425,7 +8770,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "rand", + "rand 0.8.5", "tracing-gum", ] @@ -8442,7 +8787,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "rand", + "rand 0.8.5", "tracing-gum", ] @@ -8462,10 +8807,10 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "rand", + "rand 0.8.5", "schnellru", - "sp-core", - "sp-keystore", + "sp-core 29.0.0", + "sp-keystore 0.35.0", "thiserror", "tracing-gum", ] @@ -8486,7 +8831,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "rand", + "rand 0.8.5", "sc-network", "schnellru", "thiserror", @@ -8514,8 +8859,8 @@ dependencies = [ "sc-storage-monitor", "sc-sysinfo", "sc-tracing", - "sp-core", - "sp-io", + "sp-core 29.0.0", + "sp-io 31.0.0", "sp-keyring", "sp-maybe-compressed-blob", "substrate-build-script-utils", @@ -8538,9 +8883,9 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "sp-core", - "sp-keystore", - "sp-runtime", + "sp-core 29.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "thiserror", "tokio-util", "tracing-gum", @@ -8554,9 +8899,9 @@ checksum = "b6a08e4e014c853b252ecbbe3ccd67b2d33d78e46988d309b8cccf4ac06e25ef" dependencies = [ "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -8579,8 +8924,8 @@ dependencies = [ "polkadot-primitives", "sc-network", "schnellru", - "sp-application-crypto", - "sp-keystore", + "sp-application-crypto 31.0.0", + "sp-keystore 0.35.0", "thiserror", "tracing-gum", ] @@ -8595,8 +8940,8 @@ dependencies = [ "polkadot-node-primitives", "polkadot-primitives", "reed-solomon-novelpoly", - "sp-core", - "sp-trie", + "sp-core 29.0.0", + "sp-trie 30.0.0", "thiserror", ] @@ -8612,14 +8957,14 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "rand", + "rand 0.8.5", "rand_chacha 0.3.1", "sc-network", "sc-network-common", - "sp-application-crypto", - "sp-core", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-keystore", + "sp-keystore 0.35.0", "tracing-gum", ] @@ -8660,7 +9005,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "sp-core", + "sp-core 29.0.0", "sp-maybe-compressed-blob", "thiserror", "tracing-gum", @@ -8678,7 +9023,7 @@ dependencies = [ "futures-timer", "itertools 0.10.5", "kvdb", - "merlin", + "merlin 3.0.0", "parity-scale-codec", "polkadot-node-jaeger", "polkadot-node-primitives", @@ -8686,16 +9031,16 @@ dependencies = [ "polkadot-node-subsystem-util", "polkadot-overseer", "polkadot-primitives", - "rand", + "rand 0.8.5", "rand_chacha 0.3.1", "rand_core 0.6.4", "sc-keystore", "schnellru", "schnorrkel 0.11.4", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus", "sp-consensus-slots", - "sp-runtime", + "sp-runtime 32.0.0", "thiserror", "tracing-gum", ] @@ -8739,7 +9084,7 @@ dependencies = [ "polkadot-primitives", "polkadot-statement-table", "schnellru", - "sp-keystore", + "sp-keystore 0.35.0", "thiserror", "tracing-gum", ] @@ -8754,7 +9099,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "sp-keystore", + "sp-keystore 0.35.0", "thiserror", "tracing-gum", "wasm-timer", @@ -8912,11 +9257,11 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-parachain-primitives", "polkadot-primitives", - "rand", + "rand 0.8.5", "slotmap", - "sp-core", + "sp-core 29.0.0", "sp-maybe-compressed-blob", - "sp-wasm-interface", + "sp-wasm-interface 20.0.0", "tempfile", "thiserror", "tokio", @@ -8935,7 +9280,7 @@ dependencies = [ "polkadot-node-subsystem-util", "polkadot-overseer", "polkadot-primitives", - "sp-keystore", + "sp-keystore 0.35.0", "thiserror", "tracing-gum", ] @@ -8959,11 +9304,11 @@ dependencies = [ "sc-executor-common", "sc-executor-wasmtime", "seccompiler", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-externalities", - "sp-io", - "sp-tracing", + "sp-externalities 0.26.0", + "sp-io 31.0.0", + "sp-tracing 16.0.0", "thiserror", "tracing-gum", ] @@ -8998,7 +9343,7 @@ dependencies = [ "polkadot-node-primitives", "polkadot-primitives", "sc-network", - "sp-core", + "sp-core 29.0.0", "thiserror", "tokio", ] @@ -9040,7 +9385,7 @@ dependencies = [ "polkadot-node-jaeger", "polkadot-node-primitives", "polkadot-primitives", - "rand", + "rand 0.8.5", "sc-authority-discovery", "sc-network", "strum 0.24.1", @@ -9062,12 +9407,12 @@ dependencies = [ "polkadot-primitives", "schnorrkel 0.11.4", "serde", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-babe", - "sp-core", - "sp-keystore", + "sp-core 29.0.0", + "sp-keystore 0.35.0", "sp-maybe-compressed-blob", - "sp-runtime", + "sp-runtime 32.0.0", "thiserror", "zstd 0.12.4", ] @@ -9107,7 +9452,7 @@ dependencies = [ "sp-authority-discovery", "sp-blockchain", "sp-consensus-babe", - "sp-runtime", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -9138,12 +9483,12 @@ dependencies = [ "polkadot-overseer", "polkadot-primitives", "prioritized-metered-channel", - "rand", + "rand 0.8.5", "sc-client-api", "schnellru", - "sp-application-crypto", - "sp-core", - "sp-keystore", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", + "sp-keystore 0.35.0", "thiserror", "tracing-gum", ] @@ -9166,7 +9511,7 @@ dependencies = [ "polkadot-primitives", "sc-client-api", "sp-api", - "sp-core", + "sp-core 29.0.0", "tikv-jemalloc-ctl", "tracing-gum", ] @@ -9183,10 +9528,10 @@ dependencies = [ "polkadot-core-primitives", "scale-info", "serde", - "sp-core", - "sp-runtime", - "sp-std", - "sp-weights", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-weights 28.0.0", ] [[package]] @@ -9204,17 +9549,17 @@ dependencies = [ "scale-info", "serde", "sp-api", - "sp-application-crypto", - "sp-arithmetic", + "sp-application-crypto 31.0.0", + "sp-arithmetic 24.0.0", "sp-authority-discovery", "sp-consensus-slots", - "sp-core", + "sp-core 29.0.0", "sp-inherents", - "sp-io", - "sp-keystore", - "sp-runtime", + "sp-io 31.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "sp-staking", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -9245,8 +9590,8 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-consensus-babe", - "sp-keystore", - "sp-runtime", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "substrate-frame-rpc-system", "substrate-state-trie-migration-rpc", ] @@ -9289,14 +9634,14 @@ dependencies = [ "serde_derive", "slot-range-helper", "sp-api", - "sp-core", + "sp-core 29.0.0", "sp-inherents", - "sp-io", + "sp-io 31.0.0", "sp-npos-elections", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", "sp-staking", - "sp-std", + "sp-std 14.0.0", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -9313,8 +9658,8 @@ dependencies = [ "frame-benchmarking", "parity-scale-codec", "polkadot-primitives", - "sp-std", - "sp-tracing", + "sp-std 14.0.0", + "sp-tracing 16.0.0", ] [[package]] @@ -9346,22 +9691,22 @@ dependencies = [ "polkadot-parachain-primitives", "polkadot-primitives", "polkadot-runtime-metrics", - "rand", + "rand 0.8.5", "rand_chacha 0.3.1", "rustc-hex", "scale-info", "serde", "sp-api", - "sp-application-crypto", - "sp-arithmetic", - "sp-core", + "sp-application-crypto 31.0.0", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", "sp-inherents", - "sp-io", - "sp-keystore", - "sp-runtime", + "sp-io 31.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "sp-session", "sp-staking", - "sp-std", + "sp-std 14.0.0", "staging-xcm", "staging-xcm-executor", "static_assertions", @@ -9464,21 +9809,21 @@ dependencies = [ "sp-consensus-babe", "sp-consensus-beefy", "sp-consensus-grandpa", - "sp-core", + "sp-core 29.0.0", "sp-inherents", - "sp-io", + "sp-io 31.0.0", "sp-keyring", - "sp-keystore", + "sp-keystore 0.35.0", "sp-mmr-primitives", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", - "sp-state-machine", - "sp-storage", + "sp-state-machine 0.36.0", + "sp-storage 20.0.0", "sp-timestamp", "sp-transaction-pool", "sp-version", - "sp-weights", + "sp-weights 28.0.0", "substrate-prometheus-endpoint", "thiserror", "tracing-gum", @@ -9503,7 +9848,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "sp-keystore", + "sp-keystore 0.35.0", "sp-staking", "thiserror", "tracing-gum", @@ -9517,7 +9862,7 @@ checksum = "de5e010da3c6a65d8f263d0f825a04d995ffc8a37f886f674fcbbc73bf158d01" dependencies = [ "parity-scale-codec", "polkadot-primitives", - "sp-core", + "sp-core 29.0.0", "tracing-gum", ] @@ -9603,6 +9948,19 @@ dependencies = [ "universal-hash", ] +[[package]] +name = "pop-api" +version = "0.0.0" +dependencies = [ + "enumflags2", + "ink", + "parity-scale-codec", + "pop-primitives", + "scale-info", + "sp-io 23.0.0", + "sp-runtime 24.0.0", +] + [[package]] name = "pop-node" version = "0.1.0-alpha" @@ -9653,11 +10011,11 @@ dependencies = [ "sp-block-builder", "sp-blockchain", "sp-consensus-aura", - "sp-core", - "sp-io", - "sp-keystore", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-keystore 0.35.0", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", "sp-timestamp", "sp-transaction-pool", @@ -9687,8 +10045,8 @@ dependencies = [ "parity-scale-codec", "polkadot-primitives", "scale-info", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -9740,6 +10098,7 @@ dependencies = [ "parity-scale-codec", "polkadot-parachain-primitives", "polkadot-runtime-common", + "pop-api", "pop-primitives", "pop-runtime-common", "scale-info", @@ -9747,14 +10106,14 @@ dependencies = [ "sp-api", "sp-block-builder", "sp-consensus-aura", - "sp-core", + "sp-core 29.0.0", "sp-genesis-builder", "sp-inherents", - "sp-io", + "sp-io 31.0.0", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", - "sp-std", + "sp-std 14.0.0", "sp-transaction-pool", "sp-version", "staging-parachain-info", @@ -9820,14 +10179,14 @@ dependencies = [ "sp-api", "sp-block-builder", "sp-consensus-aura", - "sp-core", + "sp-core 29.0.0", "sp-genesis-builder", "sp-inherents", - "sp-io", + "sp-io 31.0.0", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", - "sp-std", + "sp-std 14.0.0", "sp-transaction-pool", "sp-version", "staging-parachain-info", @@ -10187,7 +10546,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94b0b33c13a79f669c85defaf4c275dc86a0c0372807d0ca3d78e0bb87274863" dependencies = [ "bytes", - "rand", + "rand 0.8.5", "ring 0.16.20", "rustc-hash", "rustls 0.20.9", @@ -10215,7 +10574,20 @@ checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" [[package]] name = "rand" -version = "0.8.5" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom 0.1.16", + "libc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc", +] + +[[package]] +name = "rand" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ @@ -10269,7 +10641,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32cb0b9bc82b0a0876c2dd994a7e7a2683d3e7390ca40e6886785ef0c7e3ee31" dependencies = [ "num-traits", - "rand", + "rand 0.8.5", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core 0.5.1", ] [[package]] @@ -10495,6 +10876,12 @@ dependencies = [ "digest 0.10.7", ] +[[package]] +name = "rlibc" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc874b127765f014d792f16763a81245ab80500e2ad921ed4ee9e82481ee08fe" + [[package]] name = "rlp" version = "0.5.2" @@ -10587,22 +10974,22 @@ dependencies = [ "serde_derive", "smallvec", "sp-api", - "sp-arithmetic", + "sp-arithmetic 24.0.0", "sp-authority-discovery", "sp-block-builder", "sp-consensus-babe", "sp-consensus-beefy", - "sp-core", + "sp-core 29.0.0", "sp-genesis-builder", "sp-inherents", - "sp-io", + "sp-io 31.0.0", "sp-mmr-primitives", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", "sp-staking", - "sp-std", - "sp-storage", + "sp-std 14.0.0", + "sp-storage 20.0.0", "sp-transaction-pool", "sp-version", "staging-xcm", @@ -10622,9 +11009,9 @@ dependencies = [ "polkadot-primitives", "polkadot-runtime-common", "smallvec", - "sp-core", - "sp-runtime", - "sp-weights", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-weights 28.0.0", "staging-xcm", "staging-xcm-builder", ] @@ -10927,8 +11314,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "357127c91373ed6d1ae582f6e3300ab5b13bcde43bbf270a891f44194ef48b70" dependencies = [ "log", - "sp-core", - "sp-wasm-interface", + "sp-core 29.0.0", + "sp-wasm-interface 20.0.0", "thiserror", ] @@ -10949,15 +11336,15 @@ dependencies = [ "parity-scale-codec", "prost 0.12.3", "prost-build", - "rand", + "rand 0.8.5", "sc-client-api", "sc-network", "sp-api", "sp-authority-discovery", "sp-blockchain", - "sp-core", - "sp-keystore", - "sp-runtime", + "sp-core 29.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -10979,9 +11366,9 @@ dependencies = [ "sp-api", "sp-blockchain", "sp-consensus", - "sp-core", + "sp-core 29.0.0", "sp-inherents", - "sp-runtime", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", ] @@ -10995,10 +11382,10 @@ dependencies = [ "sp-api", "sp-block-builder", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-inherents", - "sp-runtime", - "sp-trie", + "sp-runtime 32.0.0", + "sp-trie 30.0.0", ] [[package]] @@ -11020,12 +11407,12 @@ dependencies = [ "serde", "serde_json", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", "sp-genesis-builder", - "sp-io", - "sp-runtime", - "sp-state-machine", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", ] [[package]] @@ -11057,7 +11444,7 @@ dependencies = [ "log", "names", "parity-scale-codec", - "rand", + "rand 0.8.5", "regex", "rpassword", "sc-client-api", @@ -11072,11 +11459,11 @@ dependencies = [ "serde", "serde_json", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-keyring", - "sp-keystore", - "sp-panic-handler", - "sp-runtime", + "sp-keystore 0.35.0", + "sp-panic-handler 13.0.0", + "sp-runtime 32.0.0", "sp-version", "thiserror", "tokio", @@ -11099,14 +11486,14 @@ dependencies = [ "sp-api", "sp-blockchain", "sp-consensus", - "sp-core", + "sp-core 29.0.0", "sp-database", - "sp-externalities", - "sp-runtime", - "sp-state-machine", + "sp-externalities 0.26.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "sp-statement-store", - "sp-storage", - "sp-trie", + "sp-storage 20.0.0", + "sp-trie 30.0.0", "substrate-prometheus-endpoint", ] @@ -11128,13 +11515,13 @@ dependencies = [ "sc-client-api", "sc-state-db", "schnellru", - "sp-arithmetic", + "sp-arithmetic 24.0.0", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-database", - "sp-runtime", - "sp-state-machine", - "sp-trie", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-trie 30.0.0", ] [[package]] @@ -11156,9 +11543,9 @@ dependencies = [ "sp-api", "sp-blockchain", "sp-consensus", - "sp-core", - "sp-runtime", - "sp-state-machine", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -11179,16 +11566,16 @@ dependencies = [ "sc-consensus-slots", "sc-telemetry", "sp-api", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-block-builder", "sp-blockchain", "sp-consensus", "sp-consensus-aura", "sp-consensus-slots", - "sp-core", + "sp-core 29.0.0", "sp-inherents", - "sp-keystore", - "sp-runtime", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -11215,17 +11602,17 @@ dependencies = [ "sc-telemetry", "sc-transaction-pool-api", "sp-api", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-block-builder", "sp-blockchain", "sp-consensus", "sp-consensus-babe", "sp-consensus-slots", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", "sp-inherents", - "sp-keystore", - "sp-runtime", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -11243,13 +11630,13 @@ dependencies = [ "sc-rpc-api", "serde", "sp-api", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-blockchain", "sp-consensus", "sp-consensus-babe", - "sp-core", - "sp-keystore", - "sp-runtime", + "sp-core 29.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "thiserror", ] @@ -11274,16 +11661,16 @@ dependencies = [ "sc-network-sync", "sc-utils", "sp-api", - "sp-application-crypto", - "sp-arithmetic", + "sp-application-crypto 31.0.0", + "sp-arithmetic 24.0.0", "sp-blockchain", "sp-consensus", "sp-consensus-beefy", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-keystore", + "sp-keystore 0.35.0", "sp-mmr-primitives", - "sp-runtime", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", "tokio", @@ -11305,8 +11692,8 @@ dependencies = [ "sc-rpc", "serde", "sp-consensus-beefy", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "thiserror", ] @@ -11321,7 +11708,7 @@ dependencies = [ "sc-client-api", "sc-consensus", "sp-blockchain", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -11341,7 +11728,7 @@ dependencies = [ "log", "parity-scale-codec", "parking_lot 0.12.1", - "rand", + "rand 0.8.5", "sc-block-builder", "sc-chain-spec", "sc-client-api", @@ -11355,15 +11742,15 @@ dependencies = [ "sc-utils", "serde_json", "sp-api", - "sp-application-crypto", - "sp-arithmetic", + "sp-application-crypto 31.0.0", + "sp-arithmetic 24.0.0", "sp-blockchain", "sp-consensus", "sp-consensus-grandpa", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-keystore", - "sp-runtime", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -11384,8 +11771,8 @@ dependencies = [ "sc-rpc", "serde", "sp-blockchain", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "thiserror", ] @@ -11403,14 +11790,14 @@ dependencies = [ "sc-client-api", "sc-consensus", "sc-telemetry", - "sp-arithmetic", + "sp-arithmetic 24.0.0", "sp-blockchain", "sp-consensus", "sp-consensus-slots", - "sp-core", + "sp-core 29.0.0", "sp-inherents", - "sp-runtime", - "sp-state-machine", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", ] [[package]] @@ -11425,14 +11812,14 @@ dependencies = [ "sc-executor-wasmtime", "schnellru", "sp-api", - "sp-core", - "sp-externalities", - "sp-io", - "sp-panic-handler", - "sp-runtime-interface", - "sp-trie", + "sp-core 29.0.0", + "sp-externalities 0.26.0", + "sp-io 31.0.0", + "sp-panic-handler 13.0.0", + "sp-runtime-interface 25.0.0", + "sp-trie 30.0.0", "sp-version", - "sp-wasm-interface", + "sp-wasm-interface 20.0.0", "tracing", ] @@ -11444,7 +11831,7 @@ checksum = "07498138dee3ddf2c71299ca372d8449880bb3a8a8a299a483094e9c26b0823e" dependencies = [ "sc-allocator", "sp-maybe-compressed-blob", - "sp-wasm-interface", + "sp-wasm-interface 20.0.0", "thiserror", "wasm-instrument", ] @@ -11463,8 +11850,8 @@ dependencies = [ "rustix 0.36.17", "sc-allocator", "sc-executor-common", - "sp-runtime-interface", - "sp-wasm-interface", + "sp-runtime-interface 25.0.0", + "sp-wasm-interface 20.0.0", "wasmtime", ] @@ -11483,7 +11870,7 @@ dependencies = [ "sc-network-common", "sc-network-sync", "sp-blockchain", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -11495,9 +11882,9 @@ dependencies = [ "array-bytes 6.2.2", "parking_lot 0.12.1", "serde_json", - "sp-application-crypto", - "sp-core", - "sp-keystore", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", + "sp-keystore 0.35.0", "thiserror", ] @@ -11524,10 +11911,10 @@ dependencies = [ "sc-transaction-pool-api", "sp-api", "sp-consensus", - "sp-core", - "sp-keystore", + "sp-core 29.0.0", + "sp-keystore 0.35.0", "sp-mixnet", - "sp-runtime", + "sp-runtime 32.0.0", "thiserror", ] @@ -11555,17 +11942,17 @@ dependencies = [ "parking_lot 0.12.1", "partial_sort", "pin-project", - "rand", + "rand 0.8.5", "sc-client-api", "sc-network-common", "sc-utils", "serde", "serde_json", "smallvec", - "sp-arithmetic", + "sp-arithmetic 24.0.0", "sp-blockchain", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", "tokio", @@ -11591,7 +11978,7 @@ dependencies = [ "sc-client-api", "sc-network", "sp-blockchain", - "sp-runtime", + "sp-runtime 32.0.0", "thiserror", "unsigned-varint", ] @@ -11611,7 +11998,7 @@ dependencies = [ "sc-consensus", "sp-consensus", "sp-consensus-grandpa", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -11629,7 +12016,7 @@ dependencies = [ "sc-network-common", "sc-network-sync", "schnellru", - "sp-runtime", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "tracing", ] @@ -11651,8 +12038,8 @@ dependencies = [ "sc-client-api", "sc-network", "sp-blockchain", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "thiserror", ] @@ -11681,12 +12068,12 @@ dependencies = [ "sc-utils", "schnellru", "smallvec", - "sp-arithmetic", + "sp-arithmetic 24.0.0", "sp-blockchain", "sp-consensus", "sp-consensus-grandpa", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", "tokio", @@ -11709,7 +12096,7 @@ dependencies = [ "sc-network-sync", "sc-utils", "sp-consensus", - "sp-runtime", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", ] @@ -11732,18 +12119,18 @@ dependencies = [ "once_cell", "parity-scale-codec", "parking_lot 0.12.1", - "rand", + "rand 0.8.5", "sc-client-api", "sc-network", "sc-network-common", "sc-transaction-pool-api", "sc-utils", "sp-api", - "sp-core", - "sp-externalities", - "sp-keystore", + "sp-core 29.0.0", + "sp-externalities 0.26.0", + "sp-keystore 0.35.0", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "threadpool", "tracing", ] @@ -11780,11 +12167,11 @@ dependencies = [ "serde_json", "sp-api", "sp-blockchain", - "sp-core", - "sp-keystore", + "sp-core 29.0.0", + "sp-keystore 0.35.0", "sp-offchain", "sp-rpc", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", "sp-statement-store", "sp-version", @@ -11805,9 +12192,9 @@ dependencies = [ "scale-info", "serde", "serde_json", - "sp-core", + "sp-core 29.0.0", "sp-rpc", - "sp-runtime", + "sp-runtime 32.0.0", "sp-version", "thiserror", ] @@ -11850,9 +12237,9 @@ dependencies = [ "serde", "sp-api", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-rpc", - "sp-runtime", + "sp-runtime 32.0.0", "sp-version", "thiserror", "tokio", @@ -11875,7 +12262,7 @@ dependencies = [ "parity-scale-codec", "parking_lot 0.12.1", "pin-project", - "rand", + "rand 0.8.5", "sc-chain-spec", "sc-client-api", "sc-client-db", @@ -11903,16 +12290,16 @@ dependencies = [ "sp-api", "sp-blockchain", "sp-consensus", - "sp-core", - "sp-externalities", - "sp-keystore", - "sp-runtime", + "sp-core 29.0.0", + "sp-externalities 0.26.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "sp-session", - "sp-state-machine", - "sp-storage", + "sp-state-machine 0.36.0", + "sp-storage 20.0.0", "sp-transaction-pool", "sp-transaction-storage-proof", - "sp-trie", + "sp-trie 30.0.0", "sp-version", "static_init", "substrate-prometheus-endpoint", @@ -11932,7 +12319,7 @@ dependencies = [ "log", "parity-scale-codec", "parking_lot 0.12.1", - "sp-core", + "sp-core 29.0.0", ] [[package]] @@ -11944,7 +12331,7 @@ dependencies = [ "clap", "fs4", "log", - "sp-core", + "sp-core 29.0.0", "thiserror", "tokio", ] @@ -11965,7 +12352,7 @@ dependencies = [ "serde", "serde_json", "sp-blockchain", - "sp-runtime", + "sp-runtime 32.0.0", "thiserror", ] @@ -11979,16 +12366,16 @@ dependencies = [ "futures", "libc", "log", - "rand", + "rand 0.8.5", "rand_pcg", "regex", "sc-telemetry", "serde", "serde_json", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-io", - "sp-std", + "sp-io 31.0.0", + "sp-std 14.0.0", ] [[package]] @@ -12003,7 +12390,7 @@ dependencies = [ "log", "parking_lot 0.12.1", "pin-project", - "rand", + "rand 0.8.5", "sc-utils", "serde", "serde_json", @@ -12032,10 +12419,10 @@ dependencies = [ "serde", "sp-api", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-rpc", - "sp-runtime", - "sp-tracing", + "sp-runtime 32.0.0", + "sp-tracing 16.0.0", "thiserror", "tracing", "tracing-log 0.1.4", @@ -12073,10 +12460,10 @@ dependencies = [ "serde", "sp-api", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-runtime", - "sp-tracing", + "sp-runtime 32.0.0", + "sp-tracing 16.0.0", "sp-transaction-pool", "substrate-prometheus-endpoint", "thiserror", @@ -12094,8 +12481,8 @@ dependencies = [ "parity-scale-codec", "serde", "sp-blockchain", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "thiserror", ] @@ -12112,7 +12499,7 @@ dependencies = [ "log", "parking_lot 0.12.1", "prometheus", - "sp-arithmetic", + "sp-arithmetic 24.0.0", ] [[package]] @@ -12193,6 +12580,7 @@ dependencies = [ "derive_more", "parity-scale-codec", "scale-info-derive", + "schemars", "serde", ] @@ -12250,6 +12638,30 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "schemars" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09c024468a378b7e36765cd36702b7a90cc3cba11654f6685c8f233408e89e92" +dependencies = [ + "dyn-clone", + "schemars_derive", + "serde", + "serde_json", +] + +[[package]] +name = "schemars_derive" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1eee588578aff73f856ab961cd2f79e36bc45d7ded33a7562adba4667aecc0e" +dependencies = [ + "proc-macro2", + "quote", + "serde_derive_internals", + "syn 2.0.58", +] + [[package]] name = "schnellru" version = "0.2.1" @@ -12261,6 +12673,24 @@ dependencies = [ "hashbrown 0.13.2", ] +[[package]] +name = "schnorrkel" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "021b403afe70d81eea68f6ea12f6b3c9588e5d536a94c3bf80f15e7faa267862" +dependencies = [ + "arrayref", + "arrayvec 0.5.2", + "curve25519-dalek 2.1.3", + "getrandom 0.1.16", + "merlin 2.0.1", + "rand 0.7.3", + "rand_core 0.5.1", + "sha2 0.8.2", + "subtle 2.5.0", + "zeroize", +] + [[package]] name = "schnorrkel" version = "0.10.2" @@ -12270,7 +12700,7 @@ dependencies = [ "arrayref", "arrayvec 0.7.4", "curve25519-dalek-ng", - "merlin", + "merlin 3.0.0", "rand_core 0.6.4", "sha2 0.9.9", "subtle-ng", @@ -12288,7 +12718,7 @@ dependencies = [ "arrayvec 0.7.4", "curve25519-dalek 4.1.2", "getrandom_or_panic", - "merlin", + "merlin 3.0.0", "rand_core 0.6.4", "serde_bytes", "sha2 0.10.8", @@ -12341,13 +12771,31 @@ dependencies = [ "libc", ] +[[package]] +name = "secp256k1" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1629c9c557ef9b293568b338dddfc8208c98a18c59d722a9d53f859d9c9b62" +dependencies = [ + "secp256k1-sys 0.6.1", +] + [[package]] name = "secp256k1" version = "0.28.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d24b59d129cdadea20aea4fb2352fa053712e5d713eee47d700cd4b2bc002f10" dependencies = [ - "secp256k1-sys", + "secp256k1-sys 0.9.2", +] + +[[package]] +name = "secp256k1-sys" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83080e2c2fc1006e625be82e5d1eb6a43b7fd9578b617fcc55814daf286bba4b" +dependencies = [ + "cc", ] [[package]] @@ -12453,6 +12901,17 @@ dependencies = [ "syn 2.0.58", ] +[[package]] +name = "serde_derive_internals" +version = "0.29.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.58", +] + [[package]] name = "serde_json" version = "1.0.115" @@ -12497,6 +12956,18 @@ dependencies = [ "digest 0.10.7", ] +[[package]] +name = "sha2" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69" +dependencies = [ + "block-buffer 0.7.3", + "digest 0.8.1", + "fake-simd", + "opaque-debug 0.2.3", +] + [[package]] name = "sha2" version = "0.9.9" @@ -12555,6 +13026,12 @@ dependencies = [ "libc", ] +[[package]] +name = "signature" +version = "1.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c" + [[package]] name = "signature" version = "2.2.0" @@ -12620,8 +13097,8 @@ dependencies = [ "enumn", "parity-scale-codec", "paste", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -12700,7 +13177,7 @@ dependencies = [ "hmac 0.12.1", "itertools 0.11.0", "libsecp256k1", - "merlin", + "merlin 3.0.0", "no-std-net", "nom", "num-bigint", @@ -12709,7 +13186,7 @@ dependencies = [ "pbkdf2 0.12.2", "pin-project", "poly1305", - "rand", + "rand 0.8.5", "rand_chacha 0.3.1", "ruzstd 0.4.0", "schnorrkel 0.10.2", @@ -12755,7 +13232,7 @@ dependencies = [ "itertools 0.12.1", "libm", "libsecp256k1", - "merlin", + "merlin 3.0.0", "no-std-net", "nom", "num-bigint", @@ -12764,7 +13241,7 @@ dependencies = [ "pbkdf2 0.12.2", "pin-project", "poly1305", - "rand", + "rand 0.8.5", "rand_chacha 0.3.1", "ruzstd 0.5.0", "schnorrkel 0.11.4", @@ -12807,7 +13284,7 @@ dependencies = [ "no-std-net", "parking_lot 0.12.1", "pin-project", - "rand", + "rand 0.8.5", "rand_chacha 0.3.1", "serde", "serde_json", @@ -12843,7 +13320,7 @@ dependencies = [ "no-std-net", "parking_lot 0.12.1", "pin-project", - "rand", + "rand 0.8.5", "rand_chacha 0.3.1", "serde", "serde_json", @@ -12903,10 +13380,10 @@ dependencies = [ "serde", "snowbridge-ethereum", "snowbridge-milagro-bls", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "ssz_rs", "ssz_rs_derive", "static_assertions", @@ -12927,11 +13404,11 @@ dependencies = [ "scale-info", "serde", "snowbridge-beacon-primitives", - "sp-arithmetic", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "staging-xcm", "staging-xcm-builder", ] @@ -12953,10 +13430,10 @@ dependencies = [ "scale-info", "serde", "serde-big-array", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -12968,7 +13445,7 @@ dependencies = [ "hex", "lazy_static", "parity-scale-codec", - "rand", + "rand 0.8.5", "scale-info", "snowbridge-amcl", "zeroize", @@ -12989,10 +13466,10 @@ dependencies = [ "scale-info", "serde", "snowbridge-core", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -13031,7 +13508,7 @@ dependencies = [ "http", "httparse", "log", - "rand", + "rand 0.8.5", "sha-1", ] @@ -13046,13 +13523,13 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api-proc-macro", - "sp-core", - "sp-externalities", + "sp-core 29.0.0", + "sp-externalities 0.26.0", "sp-metadata-ir", - "sp-runtime", - "sp-state-machine", - "sp-std", - "sp-trie", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-std 14.0.0", + "sp-trie 30.0.0", "sp-version", "thiserror", ] @@ -13072,6 +13549,20 @@ dependencies = [ "syn 2.0.58", ] +[[package]] +name = "sp-application-crypto" +version = "23.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899492ea547816d5dfe9a5a2ecc32f65a7110805af6da3380aa4902371b31dc2" +dependencies = [ + "parity-scale-codec", + "scale-info", + "serde", + "sp-core 21.0.0", + "sp-io 23.0.0", + "sp-std 8.0.0", +] + [[package]] name = "sp-application-crypto" version = "31.0.0" @@ -13081,9 +13572,24 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-std", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-std 14.0.0", +] + +[[package]] +name = "sp-arithmetic" +version = "16.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb6020576e544c6824a51d651bc8df8e6ab67cd59f1c9ac09868bb81a5199ded" +dependencies = [ + "integer-sqrt", + "num-traits", + "parity-scale-codec", + "scale-info", + "serde", + "sp-std 8.0.0", + "static_assertions", ] [[package]] @@ -13097,7 +13603,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-std", + "sp-std 14.0.0", "static_assertions", ] @@ -13110,9 +13616,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-application-crypto", - "sp-runtime", - "sp-std", + "sp-application-crypto 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -13123,8 +13629,8 @@ checksum = "1b36ce171caa7eb2bbe682c089f755fdefa71d3702e4fb1ba30d10146aef99d5" dependencies = [ "sp-api", "sp-inherents", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -13141,8 +13647,8 @@ dependencies = [ "sp-api", "sp-consensus", "sp-database", - "sp-runtime", - "sp-state-machine", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "thiserror", ] @@ -13155,10 +13661,10 @@ dependencies = [ "async-trait", "futures", "log", - "sp-core", + "sp-core 29.0.0", "sp-inherents", - "sp-runtime", - "sp-state-machine", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "thiserror", ] @@ -13172,11 +13678,11 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-slots", "sp-inherents", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "sp-timestamp", ] @@ -13191,12 +13697,12 @@ dependencies = [ "scale-info", "serde", "sp-api", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-slots", - "sp-core", + "sp-core 29.0.0", "sp-inherents", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "sp-timestamp", ] @@ -13211,13 +13717,13 @@ dependencies = [ "scale-info", "serde", "sp-api", - "sp-application-crypto", - "sp-core", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-io", + "sp-io 31.0.0", "sp-mmr-primitives", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "strum 0.24.1", ] @@ -13233,11 +13739,11 @@ dependencies = [ "scale-info", "serde", "sp-api", - "sp-application-crypto", - "sp-core", - "sp-keystore", - "sp-runtime", - "sp-std", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -13249,10 +13755,55 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-std", + "sp-std 14.0.0", "sp-timestamp", ] +[[package]] +name = "sp-core" +version = "21.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f18d9e2f67d8661f9729f35347069ac29d92758b59135176799db966947a7336" +dependencies = [ + "array-bytes 4.2.0", + "bitflags 1.3.2", + "blake2 0.10.6", + "bounded-collections 0.1.9", + "bs58 0.4.0", + "dyn-clonable", + "ed25519-zebra 3.1.0", + "futures", + "hash-db", + "hash256-std-hasher", + "impl-serde", + "lazy_static", + "libsecp256k1", + "log", + "merlin 2.0.1", + "parity-scale-codec", + "parking_lot 0.12.1", + "paste", + "primitive-types", + "rand 0.8.5", + "regex", + "scale-info", + "schnorrkel 0.9.1", + "secp256k1 0.24.3", + "secrecy", + "serde", + "sp-core-hashing 9.0.0", + "sp-debug-derive 8.0.0", + "sp-externalities 0.19.0", + "sp-runtime-interface 17.0.0", + "sp-std 8.0.0", + "sp-storage 13.0.0", + "ss58-registry", + "substrate-bip39", + "thiserror", + "tiny-bip39", + "zeroize", +] + [[package]] name = "sp-core" version = "29.0.0" @@ -13274,23 +13825,23 @@ dependencies = [ "itertools 0.10.5", "libsecp256k1", "log", - "merlin", + "merlin 3.0.0", "parity-scale-codec", "parking_lot 0.12.1", "paste", "primitive-types", - "rand", + "rand 0.8.5", "scale-info", "schnorrkel 0.11.4", - "secp256k1", + "secp256k1 0.28.2", "secrecy", "serde", "sp-crypto-hashing", - "sp-debug-derive", - "sp-externalities", - "sp-runtime-interface", - "sp-std", - "sp-storage", + "sp-debug-derive 14.0.0", + "sp-externalities 0.26.0", + "sp-runtime-interface 25.0.0", + "sp-std 14.0.0", + "sp-storage 20.0.0", "ss58-registry", "substrate-bip39", "thiserror", @@ -13299,6 +13850,21 @@ dependencies = [ "zeroize", ] +[[package]] +name = "sp-core-hashing" +version = "9.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ee599a8399448e65197f9a6cee338ad192e9023e35e31f22382964c3c174c68" +dependencies = [ + "blake2b_simd", + "byteorder", + "digest 0.10.7", + "sha2 0.10.8", + "sha3", + "sp-std 8.0.0", + "twox-hash", +] + [[package]] name = "sp-core-hashing" version = "15.0.0" @@ -13348,6 +13914,17 @@ dependencies = [ "parking_lot 0.12.1", ] +[[package]] +name = "sp-debug-derive" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f531814d2f16995144c74428830ccf7d94ff4a7749632b83ad8199b181140c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.58", +] + [[package]] name = "sp-debug-derive" version = "14.0.0" @@ -13359,6 +13936,18 @@ dependencies = [ "syn 2.0.58", ] +[[package]] +name = "sp-externalities" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0f71c671e01a8ca60da925d43a1b351b69626e268b8837f8371e320cf1dd100" +dependencies = [ + "environmental", + "parity-scale-codec", + "sp-std 8.0.0", + "sp-storage 13.0.0", +] + [[package]] name = "sp-externalities" version = "0.26.0" @@ -13367,8 +13956,8 @@ checksum = "e7096ed024cec397804864898b093b51e14c7299f1d00c67dd5800330e02bb82" dependencies = [ "environmental", "parity-scale-codec", - "sp-std", - "sp-storage", + "sp-std 14.0.0", + "sp-storage 20.0.0", ] [[package]] @@ -13379,8 +13968,8 @@ checksum = "fd865540ec19479c7349b584ccd78cc34c3f3a628a2a69dbb6365ceec36295ee" dependencies = [ "serde_json", "sp-api", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -13393,11 +13982,38 @@ dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "thiserror", ] +[[package]] +name = "sp-io" +version = "23.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d597e35a9628fe7454b08965b2442e3ec0f264b0a90d41328e87422cec02e99" +dependencies = [ + "bytes", + "ed25519 1.5.3", + "ed25519-dalek 1.0.1", + "futures", + "libsecp256k1", + "log", + "parity-scale-codec", + "rustversion", + "secp256k1 0.24.3", + "sp-core 21.0.0", + "sp-externalities 0.19.0", + "sp-keystore 0.27.0", + "sp-runtime-interface 17.0.0", + "sp-state-machine 0.28.0", + "sp-std 8.0.0", + "sp-tracing 10.0.0", + "sp-trie 22.0.0", + "tracing", + "tracing-core", +] + [[package]] name = "sp-io" version = "31.0.0" @@ -13405,21 +14021,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec43aa073eab35fcb920d7592474d5427ea3be2bf938706a3ad955d7ba54fd8d" dependencies = [ "bytes", - "ed25519-dalek", + "ed25519-dalek 2.1.1", "libsecp256k1", "log", "parity-scale-codec", "rustversion", - "secp256k1", - "sp-core", + "secp256k1 0.28.2", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-externalities", - "sp-keystore", - "sp-runtime-interface", - "sp-state-machine", - "sp-std", - "sp-tracing", - "sp-trie", + "sp-externalities 0.26.0", + "sp-keystore 0.35.0", + "sp-runtime-interface 25.0.0", + "sp-state-machine 0.36.0", + "sp-std 14.0.0", + "sp-tracing 16.0.0", + "sp-trie 30.0.0", "tracing", "tracing-core", ] @@ -13430,11 +14046,25 @@ version = "32.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cf0a2f881958466fc92bc9b39bbc2c0d815ded4a21f8f953372b0ac2e11b02" dependencies = [ - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "strum 0.24.1", ] +[[package]] +name = "sp-keystore" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9be3cdd67cc1d9c1db17c5cbc4ec4924054a8437009d167f21f6590797e4aa45" +dependencies = [ + "futures", + "parity-scale-codec", + "parking_lot 0.12.1", + "sp-core 21.0.0", + "sp-externalities 0.19.0", + "thiserror", +] + [[package]] name = "sp-keystore" version = "0.35.0" @@ -13443,8 +14073,8 @@ checksum = "444f2d53968b1ce5e908882710ff1f3873fcf3e95f59d57432daf685bbacb959" dependencies = [ "parity-scale-codec", "parking_lot 0.12.1", - "sp-core", - "sp-externalities", + "sp-core 29.0.0", + "sp-externalities 0.26.0", "thiserror", ] @@ -13467,7 +14097,7 @@ dependencies = [ "frame-metadata 16.0.0", "parity-scale-codec", "scale-info", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -13479,8 +14109,8 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-application-crypto", - "sp-std", + "sp-application-crypto 31.0.0", + "sp-std 14.0.0", ] [[package]] @@ -13495,10 +14125,10 @@ dependencies = [ "scale-info", "serde", "sp-api", - "sp-core", - "sp-debug-derive", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-debug-derive 14.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "thiserror", ] @@ -13511,10 +14141,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-arithmetic", - "sp-core", - "sp-runtime", - "sp-std", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -13524,8 +14154,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d83b955dce0b6d143bec3f60571311168f362b1c16cf044da7037a407b66c19" dependencies = [ "sp-api", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", +] + +[[package]] +name = "sp-panic-handler" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebd2de46003fa8212426838ca71cd42ee36a26480ba9ffea983506ce03131033" +dependencies = [ + "backtrace", + "lazy_static", + "regex", ] [[package]] @@ -13547,7 +14188,30 @@ checksum = "9af4b73fe7ddd88b1641cca90048c4e525e721763199e6fd29c4f590884f4d16" dependencies = [ "rustc-hash", "serde", - "sp-core", + "sp-core 29.0.0", +] + +[[package]] +name = "sp-runtime" +version = "24.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21c5bfc764a1a8259d7e8f7cfd22c84006275a512c958d3ff966c92151e134d5" +dependencies = [ + "either", + "hash256-std-hasher", + "impl-trait-for-tuples", + "log", + "parity-scale-codec", + "paste", + "rand 0.8.5", + "scale-info", + "serde", + "sp-application-crypto 23.0.0", + "sp-arithmetic 16.0.0", + "sp-core 21.0.0", + "sp-io 23.0.0", + "sp-std 8.0.0", + "sp-weights 20.0.0", ] [[package]] @@ -13563,16 +14227,35 @@ dependencies = [ "log", "parity-scale-codec", "paste", - "rand", + "rand 0.8.5", "scale-info", "serde", "simple-mermaid", - "sp-application-crypto", - "sp-arithmetic", - "sp-core", - "sp-io", - "sp-std", - "sp-weights", + "sp-application-crypto 31.0.0", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-std 14.0.0", + "sp-weights 28.0.0", +] + +[[package]] +name = "sp-runtime-interface" +version = "17.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e676128182f90015e916f806cba635c8141e341e7abbc45d25525472e1bbce8" +dependencies = [ + "bytes", + "impl-trait-for-tuples", + "parity-scale-codec", + "primitive-types", + "sp-externalities 0.19.0", + "sp-runtime-interface-proc-macro 11.0.0", + "sp-std 8.0.0", + "sp-storage 13.0.0", + "sp-tracing 10.0.0", + "sp-wasm-interface 14.0.0", + "static_assertions", ] [[package]] @@ -13585,15 +14268,28 @@ dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", "primitive-types", - "sp-externalities", - "sp-runtime-interface-proc-macro", - "sp-std", - "sp-storage", - "sp-tracing", - "sp-wasm-interface", + "sp-externalities 0.26.0", + "sp-runtime-interface-proc-macro 17.0.0", + "sp-std 14.0.0", + "sp-storage 20.0.0", + "sp-tracing 16.0.0", + "sp-wasm-interface 20.0.0", "static_assertions", ] +[[package]] +name = "sp-runtime-interface-proc-macro" +version = "11.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5d5bd5566fe5633ec48dfa35ab152fd29f8a577c21971e1c6db9f28afb9bbb9" +dependencies = [ + "Inflector", + "proc-macro-crate 1.3.1", + "proc-macro2", + "quote", + "syn 2.0.58", +] + [[package]] name = "sp-runtime-interface-proc-macro" version = "17.0.0" @@ -13617,11 +14313,11 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-core", - "sp-keystore", - "sp-runtime", + "sp-core 29.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "sp-staking", - "sp-std", + "sp-std 14.0.0", ] [[package]] @@ -13634,9 +14330,30 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-runtime", - "sp-std", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", +] + +[[package]] +name = "sp-state-machine" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ef45d31f9e7ac648f8899a0cd038a3608f8499028bff55b6c799702592325b6" +dependencies = [ + "hash-db", + "log", + "parity-scale-codec", + "parking_lot 0.12.1", + "rand 0.8.5", + "smallvec", + "sp-core 21.0.0", + "sp-externalities 0.19.0", + "sp-panic-handler 8.0.0", + "sp-std 8.0.0", + "sp-trie 22.0.0", + "thiserror", + "tracing", ] [[package]] @@ -13649,16 +14366,16 @@ dependencies = [ "log", "parity-scale-codec", "parking_lot 0.12.1", - "rand", + "rand 0.8.5", "smallvec", - "sp-core", - "sp-externalities", - "sp-panic-handler", - "sp-std", - "sp-trie", + "sp-core 29.0.0", + "sp-externalities 0.26.0", + "sp-panic-handler 13.0.0", + "sp-std 14.0.0", + "sp-trie 30.0.0", "thiserror", "tracing", - "trie-db", + "trie-db 0.28.0", ] [[package]] @@ -13669,30 +14386,50 @@ checksum = "309a9ae4e8134bbed8ffc510cf4d461a4a651f9250b556de782cedd876abe1ff" dependencies = [ "aes-gcm", "curve25519-dalek 4.1.2", - "ed25519-dalek", + "ed25519-dalek 2.1.1", "hkdf", "parity-scale-codec", - "rand", + "rand 0.8.5", "scale-info", "sha2 0.10.8", "sp-api", - "sp-application-crypto", - "sp-core", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-externalities", - "sp-runtime", - "sp-runtime-interface", - "sp-std", + "sp-externalities 0.26.0", + "sp-runtime 32.0.0", + "sp-runtime-interface 25.0.0", + "sp-std 14.0.0", "thiserror", "x25519-dalek 2.0.1", ] +[[package]] +name = "sp-std" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53458e3c57df53698b3401ec0934bea8e8cfce034816873c0b0abbd83d7bac0d" + [[package]] name = "sp-std" version = "14.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "12f8ee986414b0a9ad741776762f4083cd3a5128449b982a3919c4df36874834" +[[package]] +name = "sp-storage" +version = "13.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94294be83f11d4958cfea89ed5798f0b6605f5defc3a996948848458abbcc18e" +dependencies = [ + "impl-serde", + "parity-scale-codec", + "ref-cast", + "serde", + "sp-debug-derive 8.0.0", + "sp-std 8.0.0", +] + [[package]] name = "sp-storage" version = "20.0.0" @@ -13703,8 +14440,8 @@ dependencies = [ "parity-scale-codec", "ref-cast", "serde", - "sp-debug-derive", - "sp-std", + "sp-debug-derive 14.0.0", + "sp-std 14.0.0", ] [[package]] @@ -13716,11 +14453,24 @@ dependencies = [ "async-trait", "parity-scale-codec", "sp-inherents", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "thiserror", ] +[[package]] +name = "sp-tracing" +version = "10.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357f7591980dd58305956d32f8f6646d0a8ea9ea0e7e868e46f53b68ddf00cec" +dependencies = [ + "parity-scale-codec", + "sp-std 8.0.0", + "tracing", + "tracing-core", + "tracing-subscriber 0.2.25", +] + [[package]] name = "sp-tracing" version = "16.0.0" @@ -13728,7 +14478,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0351810b9d074df71c4514c5228ed05c250607cba131c1c9d1526760ab69c05c" dependencies = [ "parity-scale-codec", - "sp-std", + "sp-std 14.0.0", "tracing", "tracing-core", "tracing-subscriber 0.2.25", @@ -13741,7 +14491,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9742861c5330bdcb42856a6eed3d3745b58ee1c92ca4c9260032ff4e6c387165" dependencies = [ "sp-api", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -13753,11 +14503,35 @@ dependencies = [ "async-trait", "parity-scale-codec", "scale-info", - "sp-core", + "sp-core 29.0.0", "sp-inherents", - "sp-runtime", - "sp-std", - "sp-trie", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-trie 30.0.0", +] + +[[package]] +name = "sp-trie" +version = "22.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e4eeb7ef23f79eba8609db79ef9cef242f994f1f87a3c0387b4b5f177fda74" +dependencies = [ + "ahash 0.8.11", + "hash-db", + "hashbrown 0.13.2", + "lazy_static", + "memory-db", + "nohash-hasher", + "parity-scale-codec", + "parking_lot 0.12.1", + "scale-info", + "schnellru", + "sp-core 21.0.0", + "sp-std 8.0.0", + "thiserror", + "tracing", + "trie-db 0.27.1", + "trie-root", ] [[package]] @@ -13773,15 +14547,15 @@ dependencies = [ "nohash-hasher", "parity-scale-codec", "parking_lot 0.12.1", - "rand", + "rand 0.8.5", "scale-info", "schnellru", - "sp-core", - "sp-externalities", - "sp-std", + "sp-core 29.0.0", + "sp-externalities 0.26.0", + "sp-std 14.0.0", "thiserror", "tracing", - "trie-db", + "trie-db 0.28.0", "trie-root", ] @@ -13797,8 +14571,8 @@ dependencies = [ "scale-info", "serde", "sp-crypto-hashing-proc-macro", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", "sp-version-proc-macro", "thiserror", ] @@ -13815,6 +14589,20 @@ dependencies = [ "syn 2.0.58", ] +[[package]] +name = "sp-wasm-interface" +version = "14.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19c122609ca5d8246be6386888596320d03c7bc880959eaa2c36bcd5acd6846" +dependencies = [ + "anyhow", + "impl-trait-for-tuples", + "log", + "parity-scale-codec", + "sp-std 8.0.0", + "wasmtime", +] + [[package]] name = "sp-wasm-interface" version = "20.0.0" @@ -13825,10 +14613,26 @@ dependencies = [ "impl-trait-for-tuples", "log", "parity-scale-codec", - "sp-std", + "sp-std 14.0.0", "wasmtime", ] +[[package]] +name = "sp-weights" +version = "20.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45d084c735544f70625b821c3acdbc7a2fc1893ca98b85f1942631284692c75b" +dependencies = [ + "parity-scale-codec", + "scale-info", + "serde", + "smallvec", + "sp-arithmetic 16.0.0", + "sp-core 21.0.0", + "sp-debug-derive 8.0.0", + "sp-std 8.0.0", +] + [[package]] name = "sp-weights" version = "28.0.0" @@ -13840,9 +14644,9 @@ dependencies = [ "scale-info", "serde", "smallvec", - "sp-arithmetic", - "sp-debug-derive", - "sp-std", + "sp-arithmetic 24.0.0", + "sp-debug-derive 14.0.0", + "sp-std 14.0.0", ] [[package]] @@ -13933,8 +14737,8 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-std", + "sp-runtime 32.0.0", + "sp-std 14.0.0", ] [[package]] @@ -13952,7 +14756,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-weights", + "sp-weights 28.0.0", "xcm-procedural", ] @@ -13970,11 +14774,11 @@ dependencies = [ "parity-scale-codec", "polkadot-parachain-primitives", "scale-info", - "sp-arithmetic", - "sp-io", - "sp-runtime", - "sp-std", - "sp-weights", + "sp-arithmetic 24.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-weights 28.0.0", "staging-xcm", "staging-xcm-executor", ] @@ -13992,12 +14796,12 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", - "sp-weights", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-weights 28.0.0", "staging-xcm", ] @@ -14136,8 +14940,8 @@ dependencies = [ "sp-api", "sp-block-builder", "sp-blockchain", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", ] [[package]] @@ -14164,7 +14968,7 @@ dependencies = [ "log", "sc-rpc-api", "serde", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -14178,11 +14982,11 @@ dependencies = [ "sc-client-api", "sc-rpc-api", "serde", - "sp-core", - "sp-runtime", - "sp-state-machine", - "sp-trie", - "trie-db", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-trie 30.0.0", + "trie-db 0.28.0", ] [[package]] @@ -14248,7 +15052,7 @@ dependencies = [ "scale-value", "serde", "serde_json", - "sp-core-hashing", + "sp-core-hashing 15.0.0", "subxt-lightclient", "subxt-macro", "subxt-metadata", @@ -14320,7 +15124,7 @@ dependencies = [ "frame-metadata 16.0.0", "parity-scale-codec", "scale-info", - "sp-core-hashing", + "sp-core-hashing 15.0.0", "thiserror", ] @@ -14337,10 +15141,10 @@ dependencies = [ "pbkdf2 0.12.2", "regex", "schnorrkel 0.11.4", - "secp256k1", + "secp256k1 0.28.2", "secrecy", "sha2 0.10.8", - "sp-core-hashing", + "sp-core-hashing 15.0.0", "subxt", "thiserror", "zeroize", @@ -14380,6 +15184,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "synstructure" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.58", +] + [[package]] name = "system-configuration" version = "0.5.1" @@ -14460,7 +15275,7 @@ dependencies = [ "polkadot-core-primitives", "rococo-runtime-constants", "smallvec", - "sp-runtime", + "sp-runtime 32.0.0", "staging-xcm", "westend-runtime-constants", ] @@ -14595,6 +15410,25 @@ dependencies = [ "time-core", ] +[[package]] +name = "tiny-bip39" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62cc94d358b5a1e84a5cb9109f559aa3c4d634d2b1b4de3d0fa4adc7c78e2861" +dependencies = [ + "anyhow", + "hmac 0.12.1", + "once_cell", + "pbkdf2 0.11.0", + "rand 0.8.5", + "rustc-hash", + "sha2 0.10.8", + "thiserror", + "unicode-normalization", + "wasm-bindgen", + "zeroize", +] + [[package]] name = "tiny-keccak" version = "2.0.2" @@ -14656,7 +15490,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f57eb36ecbe0fc510036adff84824dd3c24bb781e21bfa67b69d556aa85214f" dependencies = [ "pin-project", - "rand", + "rand 0.8.5", "tokio", ] @@ -14968,6 +15802,19 @@ dependencies = [ "tracing-log 0.2.0", ] +[[package]] +name = "trie-db" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "767abe6ffed88a1889671a102c2861ae742726f52e0a5a425b92c9fbfa7e9c85" +dependencies = [ + "hash-db", + "hashbrown 0.13.2", + "log", + "rustc-hex", + "smallvec", +] + [[package]] name = "trie-db" version = "0.28.0" @@ -15006,7 +15853,7 @@ dependencies = [ "idna 0.2.3", "ipnet", "lazy_static", - "rand", + "rand 0.8.5", "smallvec", "socket2 0.4.10", "thiserror", @@ -15062,19 +15909,19 @@ dependencies = [ "sp-api", "sp-consensus-aura", "sp-consensus-babe", - "sp-core", - "sp-debug-derive", - "sp-externalities", + "sp-core 29.0.0", + "sp-debug-derive 14.0.0", + "sp-externalities 0.26.0", "sp-inherents", - "sp-io", - "sp-keystore", + "sp-io 31.0.0", + "sp-keystore 0.35.0", "sp-rpc", - "sp-runtime", - "sp-state-machine", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "sp-timestamp", "sp-transaction-storage-proof", "sp-version", - "sp-weights", + "sp-weights 28.0.0", "substrate-rpc-client", "zstd 0.12.4", ] @@ -15093,7 +15940,7 @@ checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ "cfg-if", "digest 0.10.7", - "rand", + "rand 0.8.5", "static_assertions", ] @@ -15244,7 +16091,7 @@ dependencies = [ "arrayref", "constcat", "digest 0.10.7", - "rand", + "rand 0.8.5", "rand_chacha 0.3.1", "rand_core 0.6.4", "sha2 0.10.8", @@ -15654,7 +16501,7 @@ dependencies = [ "memfd", "memoffset", "paste", - "rand", + "rand 0.8.5", "rustix 0.36.17", "wasmtime-asm-macros", "wasmtime-environ", @@ -15783,24 +16630,24 @@ dependencies = [ "serde_derive", "smallvec", "sp-api", - "sp-application-crypto", - "sp-arithmetic", + "sp-application-crypto 31.0.0", + "sp-arithmetic 24.0.0", "sp-authority-discovery", "sp-block-builder", "sp-consensus-babe", "sp-consensus-beefy", - "sp-core", + "sp-core 29.0.0", "sp-genesis-builder", "sp-inherents", - "sp-io", + "sp-io 31.0.0", "sp-mmr-primitives", "sp-npos-elections", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", "sp-staking", - "sp-std", - "sp-storage", + "sp-std 14.0.0", + "sp-storage 20.0.0", "sp-transaction-pool", "sp-version", "staging-xcm", @@ -15820,9 +16667,9 @@ dependencies = [ "polkadot-primitives", "polkadot-runtime-common", "smallvec", - "sp-core", - "sp-runtime", - "sp-weights", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-weights 28.0.0", "staging-xcm", "staging-xcm-builder", ] @@ -16214,13 +17061,13 @@ dependencies = [ "polkadot-parachain-primitives", "polkadot-primitives", "polkadot-runtime-parachains", - "sp-arithmetic", - "sp-core", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-io", - "sp-runtime", - "sp-std", - "sp-tracing", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std 14.0.0", + "sp-tracing 16.0.0", "staging-xcm", "staging-xcm-executor", ] @@ -16237,6 +17084,12 @@ dependencies = [ "syn 2.0.58", ] +[[package]] +name = "xxhash-rust" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "927da81e25be1e1a2901d59b81b37dd2efd1fc9c9345a55007f09bf5a2d3ee03" + [[package]] name = "yamux" version = "0.10.2" @@ -16247,7 +17100,7 @@ dependencies = [ "log", "nohash-hasher", "parking_lot 0.12.1", - "rand", + "rand 0.8.5", "static_assertions", ] diff --git a/pop-api/examples/fungibles/expanded.rs b/pop-api/examples/fungibles/expanded.rs deleted file mode 100644 index c73cdeb7..00000000 --- a/pop-api/examples/fungibles/expanded.rs +++ /dev/null @@ -1,2766 +0,0 @@ -#![feature(prelude_import)] -#[prelude_import] -use std::prelude::rust_2021::*; -#[macro_use] -extern crate std; -use pop_api::{ - primitives::{AccountId as AccountId32, AssetId}, - assets::fungibles::*, -}; -pub enum FungiblesError { - /// Not enough balance to fulfill a request is available. - InsufficientBalance, - /// Not enough allowance to fulfill a request is available. - InsufficientAllowance, - /// The asset status is not the expected status. - IncorrectStatus, - /// The asset ID is already taken. - InUse, - /// Minimum balance should be non-zero. - MinBalanceZero, - /// The signing account has no permission to do the operation. - NoPermission, - /// The given asset ID is unknown. - Unknown, - /// Recipient's address is zero. - ZeroRecipientAddress, - /// Sender's address is zero. - ZeroSenderAddress, -} -#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _: () = { - impl ::scale_info::TypeInfo for FungiblesError { - type Identity = Self; - fn type_info() -> ::scale_info::Type { - ::scale_info::Type::builder() - .path( - ::scale_info::Path::new_with_replace( - "FungiblesError", - "fungibles", - &[], - ), - ) - .type_params(::alloc::vec::Vec::new()) - .variant( - ::scale_info::build::Variants::new() - .variant( - "InsufficientBalance", - |v| { - v - .index(0usize as ::core::primitive::u8) - .docs( - &["Not enough balance to fulfill a request is available."], - ) - }, - ) - .variant( - "InsufficientAllowance", - |v| { - v - .index(1usize as ::core::primitive::u8) - .docs( - &["Not enough allowance to fulfill a request is available."], - ) - }, - ) - .variant( - "IncorrectStatus", - |v| { - v - .index(2usize as ::core::primitive::u8) - .docs(&["The asset status is not the expected status."]) - }, - ) - .variant( - "InUse", - |v| { - v - .index(3usize as ::core::primitive::u8) - .docs(&["The asset ID is already taken."]) - }, - ) - .variant( - "MinBalanceZero", - |v| { - v - .index(4usize as ::core::primitive::u8) - .docs(&["Minimum balance should be non-zero."]) - }, - ) - .variant( - "NoPermission", - |v| { - v - .index(5usize as ::core::primitive::u8) - .docs( - &[ - "The signing account has no permission to do the operation.", - ], - ) - }, - ) - .variant( - "Unknown", - |v| { - v - .index(6usize as ::core::primitive::u8) - .docs(&["The given asset ID is unknown."]) - }, - ) - .variant( - "ZeroRecipientAddress", - |v| { - v - .index(7usize as ::core::primitive::u8) - .docs(&["Recipient's address is zero."]) - }, - ) - .variant( - "ZeroSenderAddress", - |v| { - v - .index(8usize as ::core::primitive::u8) - .docs(&["Sender's address is zero."]) - }, - ), - ) - } - } -}; -#[automatically_derived] -impl ::core::fmt::Debug for FungiblesError { - #[inline] - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - ::core::fmt::Formatter::write_str( - f, - match self { - FungiblesError::InsufficientBalance => "InsufficientBalance", - FungiblesError::InsufficientAllowance => "InsufficientAllowance", - FungiblesError::IncorrectStatus => "IncorrectStatus", - FungiblesError::InUse => "InUse", - FungiblesError::MinBalanceZero => "MinBalanceZero", - FungiblesError::NoPermission => "NoPermission", - FungiblesError::Unknown => "Unknown", - FungiblesError::ZeroRecipientAddress => "ZeroRecipientAddress", - FungiblesError::ZeroSenderAddress => "ZeroSenderAddress", - }, - ) - } -} -#[automatically_derived] -impl ::core::marker::Copy for FungiblesError {} -#[automatically_derived] -impl ::core::clone::Clone for FungiblesError { - #[inline] - fn clone(&self) -> FungiblesError { - *self - } -} -#[automatically_derived] -impl ::core::marker::StructuralPartialEq for FungiblesError {} -#[automatically_derived] -impl ::core::cmp::PartialEq for FungiblesError { - #[inline] - fn eq(&self, other: &FungiblesError) -> bool { - let __self_tag = ::core::intrinsics::discriminant_value(self); - let __arg1_tag = ::core::intrinsics::discriminant_value(other); - __self_tag == __arg1_tag - } -} -#[automatically_derived] -impl ::core::cmp::Eq for FungiblesError { - #[inline] - #[doc(hidden)] - #[coverage(off)] - fn assert_receiver_is_total_eq(&self) -> () {} -} -#[allow(deprecated)] -const _: () = { - #[automatically_derived] - impl ::scale::Encode for FungiblesError { - fn size_hint(&self) -> usize { - 1_usize - + match *self { - FungiblesError::InsufficientBalance => 0_usize, - FungiblesError::InsufficientAllowance => 0_usize, - FungiblesError::IncorrectStatus => 0_usize, - FungiblesError::InUse => 0_usize, - FungiblesError::MinBalanceZero => 0_usize, - FungiblesError::NoPermission => 0_usize, - FungiblesError::Unknown => 0_usize, - FungiblesError::ZeroRecipientAddress => 0_usize, - FungiblesError::ZeroSenderAddress => 0_usize, - _ => 0_usize, - } - } - fn encode_to<__CodecOutputEdqy: ::scale::Output + ?::core::marker::Sized>( - &self, - __codec_dest_edqy: &mut __CodecOutputEdqy, - ) { - match *self { - FungiblesError::InsufficientBalance => { - #[allow(clippy::unnecessary_cast)] - __codec_dest_edqy.push_byte(0usize as ::core::primitive::u8); - } - FungiblesError::InsufficientAllowance => { - #[allow(clippy::unnecessary_cast)] - __codec_dest_edqy.push_byte(1usize as ::core::primitive::u8); - } - FungiblesError::IncorrectStatus => { - #[allow(clippy::unnecessary_cast)] - __codec_dest_edqy.push_byte(2usize as ::core::primitive::u8); - } - FungiblesError::InUse => { - #[allow(clippy::unnecessary_cast)] - __codec_dest_edqy.push_byte(3usize as ::core::primitive::u8); - } - FungiblesError::MinBalanceZero => { - #[allow(clippy::unnecessary_cast)] - __codec_dest_edqy.push_byte(4usize as ::core::primitive::u8); - } - FungiblesError::NoPermission => { - #[allow(clippy::unnecessary_cast)] - __codec_dest_edqy.push_byte(5usize as ::core::primitive::u8); - } - FungiblesError::Unknown => { - #[allow(clippy::unnecessary_cast)] - __codec_dest_edqy.push_byte(6usize as ::core::primitive::u8); - } - FungiblesError::ZeroRecipientAddress => { - #[allow(clippy::unnecessary_cast)] - __codec_dest_edqy.push_byte(7usize as ::core::primitive::u8); - } - FungiblesError::ZeroSenderAddress => { - #[allow(clippy::unnecessary_cast)] - __codec_dest_edqy.push_byte(8usize as ::core::primitive::u8); - } - _ => {} - } - } - } - #[automatically_derived] - impl ::scale::EncodeLike for FungiblesError {} -}; -#[allow(deprecated)] -const _: () = { - #[automatically_derived] - impl ::scale::Decode for FungiblesError { - fn decode<__CodecInputEdqy: ::scale::Input>( - __codec_input_edqy: &mut __CodecInputEdqy, - ) -> ::core::result::Result { - match __codec_input_edqy - .read_byte() - .map_err(|e| { - e - .chain( - "Could not decode `FungiblesError`, failed to read variant byte", - ) - })? - { - #[allow(clippy::unnecessary_cast)] - __codec_x_edqy if __codec_x_edqy == 0usize as ::core::primitive::u8 => { - #[allow(clippy::redundant_closure_call)] - return (move || { - ::core::result::Result::Ok(FungiblesError::InsufficientBalance) - })(); - } - #[allow(clippy::unnecessary_cast)] - __codec_x_edqy if __codec_x_edqy == 1usize as ::core::primitive::u8 => { - #[allow(clippy::redundant_closure_call)] - return (move || { - ::core::result::Result::Ok(FungiblesError::InsufficientAllowance) - })(); - } - #[allow(clippy::unnecessary_cast)] - __codec_x_edqy if __codec_x_edqy == 2usize as ::core::primitive::u8 => { - #[allow(clippy::redundant_closure_call)] - return (move || { - ::core::result::Result::Ok(FungiblesError::IncorrectStatus) - })(); - } - #[allow(clippy::unnecessary_cast)] - __codec_x_edqy if __codec_x_edqy == 3usize as ::core::primitive::u8 => { - #[allow(clippy::redundant_closure_call)] - return (move || { - ::core::result::Result::Ok(FungiblesError::InUse) - })(); - } - #[allow(clippy::unnecessary_cast)] - __codec_x_edqy if __codec_x_edqy == 4usize as ::core::primitive::u8 => { - #[allow(clippy::redundant_closure_call)] - return (move || { - ::core::result::Result::Ok(FungiblesError::MinBalanceZero) - })(); - } - #[allow(clippy::unnecessary_cast)] - __codec_x_edqy if __codec_x_edqy == 5usize as ::core::primitive::u8 => { - #[allow(clippy::redundant_closure_call)] - return (move || { - ::core::result::Result::Ok(FungiblesError::NoPermission) - })(); - } - #[allow(clippy::unnecessary_cast)] - __codec_x_edqy if __codec_x_edqy == 6usize as ::core::primitive::u8 => { - #[allow(clippy::redundant_closure_call)] - return (move || { - ::core::result::Result::Ok(FungiblesError::Unknown) - })(); - } - #[allow(clippy::unnecessary_cast)] - __codec_x_edqy if __codec_x_edqy == 7usize as ::core::primitive::u8 => { - #[allow(clippy::redundant_closure_call)] - return (move || { - ::core::result::Result::Ok(FungiblesError::ZeroRecipientAddress) - })(); - } - #[allow(clippy::unnecessary_cast)] - __codec_x_edqy if __codec_x_edqy == 8usize as ::core::primitive::u8 => { - #[allow(clippy::redundant_closure_call)] - return (move || { - ::core::result::Result::Ok(FungiblesError::ZeroSenderAddress) - })(); - } - _ => { - #[allow(clippy::redundant_closure_call)] - return (move || { - ::core::result::Result::Err( - <_ as ::core::convert::Into< - _, - >>::into( - "Could not decode `FungiblesError`, variant doesn't exist", - ), - ) - })(); - } - } - } - } -}; -impl From for FungiblesError { - fn from(error: Error) -> Self { - match error { - Error::InUse => FungiblesError::InUse, - Error::MinBalanceZero => FungiblesError::MinBalanceZero, - Error::Unknown => FungiblesError::Unknown, - _ => ::core::panicking::panic("not yet implemented"), - } - } -} -/// The fungibles result type. -pub type Result = core::result::Result; -mod fungibles { - impl ::ink::env::ContractEnv for Fungibles { - type Env = pop_api::Environment; - } - type Environment = ::Env; - type AccountId = <::Env as ::ink::env::Environment>::AccountId; - type Balance = <::Env as ::ink::env::Environment>::Balance; - type Hash = <::Env as ::ink::env::Environment>::Hash; - type Timestamp = <::Env as ::ink::env::Environment>::Timestamp; - type BlockNumber = <::Env as ::ink::env::Environment>::BlockNumber; - type ChainExtension = <::Env as ::ink::env::Environment>::ChainExtension; - const MAX_EVENT_TOPICS: usize = <::Env as ::ink::env::Environment>::MAX_EVENT_TOPICS; - const _: () = { - struct Check { - salt: (), - } - }; - #[scale_info(crate = ::ink::scale_info)] - #[cfg(not(feature = "__ink_dylint_Storage"))] - pub struct Fungibles {} - const _: () = { - impl< - __ink_generic_salt: ::ink::storage::traits::StorageKey, - > ::ink::storage::traits::StorableHint<__ink_generic_salt> for Fungibles { - type Type = Fungibles; - type PreferredKey = ::ink::storage::traits::AutoKey; - } - }; - const _: () = { - impl ::ink::storage::traits::StorageKey for Fungibles { - const KEY: ::ink::primitives::Key = <() as ::ink::storage::traits::StorageKey>::KEY; - } - }; - const _: () = { - impl ::ink::storage::traits::Storable for Fungibles { - #[inline(always)] - #[allow(non_camel_case_types)] - fn decode<__ink_I: ::ink::scale::Input>( - __input: &mut __ink_I, - ) -> ::core::result::Result { - ::core::result::Result::Ok(Fungibles {}) - } - #[inline(always)] - #[allow(non_camel_case_types)] - fn encode<__ink_O: ::ink::scale::Output + ?::core::marker::Sized>( - &self, - __dest: &mut __ink_O, - ) { - match self { - Fungibles {} => {} - } - } - #[inline(always)] - #[allow(non_camel_case_types)] - fn encoded_size(&self) -> ::core::primitive::usize { - match self { - Fungibles {} => ::core::primitive::usize::MIN, - } - } - } - }; - #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] - const _: () = { - impl ::ink::scale_info::TypeInfo for Fungibles { - type Identity = Self; - fn type_info() -> ::ink::scale_info::Type { - ::ink::scale_info::Type::builder() - .path( - ::ink::scale_info::Path::new_with_replace( - "Fungibles", - "fungibles::fungibles", - &[], - ), - ) - .type_params(::alloc::vec::Vec::new()) - .composite(::ink::scale_info::build::Fields::named()) - } - } - }; - const _: () = { - impl ::ink::storage::traits::StorageLayout for Fungibles { - fn layout( - __key: &::ink::primitives::Key, - ) -> ::ink::metadata::layout::Layout { - ::ink::metadata::layout::Layout::Struct( - ::ink::metadata::layout::StructLayout::new("Fungibles", []), - ) - } - } - }; - #[automatically_derived] - impl ::core::default::Default for Fungibles { - #[inline] - fn default() -> Fungibles { - Fungibles {} - } - } - const _: () = { - impl ::ink::reflect::ContractName for Fungibles { - const NAME: &'static str = "Fungibles"; - } - }; - const _: () = { - impl<'a> ::ink::codegen::Env for &'a Fungibles { - type EnvAccess = ::ink::EnvAccess< - 'a, - ::Env, - >; - fn env(self) -> Self::EnvAccess { - <::EnvAccess as ::core::default::Default>::default() - } - } - impl<'a> ::ink::codegen::StaticEnv for Fungibles { - type EnvAccess = ::ink::EnvAccess< - 'static, - ::Env, - >; - fn env() -> Self::EnvAccess { - <::EnvAccess as ::core::default::Default>::default() - } - } - }; - const _: () = { - #[allow(unused_imports)] - use ::ink::codegen::{Env as _, StaticEnv as _}; - }; - impl ::ink::reflect::DispatchableConstructorInfo<0x9BAE9D5E_u32> for Fungibles { - type Input = (); - type Output = Self; - type Storage = Fungibles; - type Error = <::ink::reflect::ConstructorOutputValue< - Self, - > as ::ink::reflect::ConstructorOutput>::Error; - const IS_RESULT: ::core::primitive::bool = <::ink::reflect::ConstructorOutputValue< - Self, - > as ::ink::reflect::ConstructorOutput>::IS_RESULT; - const CALLABLE: fn(Self::Input) -> Self::Output = |_| { Fungibles::new() }; - const PAYABLE: ::core::primitive::bool = true; - const SELECTOR: [::core::primitive::u8; 4usize] = [ - 0x9B_u8, - 0xAE_u8, - 0x9D_u8, - 0x5E_u8, - ]; - const LABEL: &'static ::core::primitive::str = "new"; - } - impl ::ink::reflect::DispatchableMessageInfo<0xDB6375A8_u32> for Fungibles { - type Input = AssetId; - type Output = Result; - type Storage = Fungibles; - const CALLABLE: fn(&mut Self::Storage, Self::Input) -> Self::Output = | - storage, - __ink_binding_0| - { Fungibles::total_supply(storage, __ink_binding_0) }; - const SELECTOR: [::core::primitive::u8; 4usize] = [ - 0xDB_u8, - 0x63_u8, - 0x75_u8, - 0xA8_u8, - ]; - const PAYABLE: ::core::primitive::bool = false; - const MUTATES: ::core::primitive::bool = false; - const LABEL: &'static ::core::primitive::str = "total_supply"; - } - impl ::ink::reflect::DispatchableMessageInfo<0x0F755A56_u32> for Fungibles { - type Input = (AssetId, AccountId32); - type Output = Result; - type Storage = Fungibles; - const CALLABLE: fn(&mut Self::Storage, Self::Input) -> Self::Output = | - storage, - (__ink_binding_0, __ink_binding_1)| - { Fungibles::balance_of(storage, __ink_binding_0, __ink_binding_1) }; - const SELECTOR: [::core::primitive::u8; 4usize] = [ - 0x0F_u8, - 0x75_u8, - 0x5A_u8, - 0x56_u8, - ]; - const PAYABLE: ::core::primitive::bool = false; - const MUTATES: ::core::primitive::bool = false; - const LABEL: &'static ::core::primitive::str = "balance_of"; - } - impl ::ink::reflect::DispatchableMessageInfo<0x6A00165E_u32> for Fungibles { - type Input = (AssetId, AccountId32, AccountId32); - type Output = Result; - type Storage = Fungibles; - const CALLABLE: fn(&mut Self::Storage, Self::Input) -> Self::Output = | - storage, - (__ink_binding_0, __ink_binding_1, __ink_binding_2)| - { - Fungibles::allowance( - storage, - __ink_binding_0, - __ink_binding_1, - __ink_binding_2, - ) - }; - const SELECTOR: [::core::primitive::u8; 4usize] = [ - 0x6A_u8, - 0x00_u8, - 0x16_u8, - 0x5E_u8, - ]; - const PAYABLE: ::core::primitive::bool = false; - const MUTATES: ::core::primitive::bool = false; - const LABEL: &'static ::core::primitive::str = "allowance"; - } - impl ::ink::reflect::DispatchableMessageInfo<0xAA6B65DB_u32> for Fungibles { - type Input = AssetId; - type Output = Result; - type Storage = Fungibles; - const CALLABLE: fn(&mut Self::Storage, Self::Input) -> Self::Output = | - storage, - __ink_binding_0| - { Fungibles::asset_exists(storage, __ink_binding_0) }; - const SELECTOR: [::core::primitive::u8; 4usize] = [ - 0xAA_u8, - 0x6B_u8, - 0x65_u8, - 0xDB_u8, - ]; - const PAYABLE: ::core::primitive::bool = false; - const MUTATES: ::core::primitive::bool = false; - const LABEL: &'static ::core::primitive::str = "asset_exists"; - } - impl ::ink::reflect::DispatchableMessageInfo<0x1F8E8E22_u32> for Fungibles { - type Input = (u32, AccountId32, Balance); - type Output = Result<()>; - type Storage = Fungibles; - const CALLABLE: fn(&mut Self::Storage, Self::Input) -> Self::Output = | - storage, - (__ink_binding_0, __ink_binding_1, __ink_binding_2)| - { - Fungibles::mint_asset( - storage, - __ink_binding_0, - __ink_binding_1, - __ink_binding_2, - ) - }; - const SELECTOR: [::core::primitive::u8; 4usize] = [ - 0x1F_u8, - 0x8E_u8, - 0x8E_u8, - 0x22_u8, - ]; - const PAYABLE: ::core::primitive::bool = false; - const MUTATES: ::core::primitive::bool = false; - const LABEL: &'static ::core::primitive::str = "mint_asset"; - } - const _: () = { - #[allow(non_camel_case_types)] - pub enum __ink_ConstructorDecoder { - Constructor0( - >::Input, - ), - } - impl ::ink::reflect::DecodeDispatch for __ink_ConstructorDecoder { - fn decode_dispatch( - input: &mut I, - ) -> ::core::result::Result - where - I: ::ink::scale::Input, - { - const CONSTRUCTOR_0: [::core::primitive::u8; 4usize] = >::SELECTOR; - match <[::core::primitive::u8; 4usize] as ::ink::scale::Decode>::decode( - input, - ) - .map_err(|_| ::ink::reflect::DispatchError::InvalidSelector)? - { - CONSTRUCTOR_0 => { - ::core::result::Result::Ok( - Self::Constructor0( - <>::Input as ::ink::scale::Decode>::decode(input) - .map_err(|_| { - ::ink::reflect::DispatchError::InvalidParameters - })?, - ), - ) - } - _invalid => { - ::core::result::Result::Err( - ::ink::reflect::DispatchError::UnknownSelector, - ) - } - } - } - } - impl ::ink::scale::Decode for __ink_ConstructorDecoder { - fn decode( - input: &mut I, - ) -> ::core::result::Result - where - I: ::ink::scale::Input, - { - ::decode_dispatch(input) - .map_err(::core::convert::Into::into) - } - } - impl ::ink::reflect::ExecuteDispatchable for __ink_ConstructorDecoder { - #[allow(clippy::nonminimal_bool)] - fn execute_dispatchable( - self, - ) -> ::core::result::Result<(), ::ink::reflect::DispatchError> { - match self { - Self::Constructor0(input) => { - if { - false - || { - let constructor_0 = false; - let constructor_0 = >::PAYABLE; - constructor_0 - } - } - && !>::PAYABLE - { - ::ink::codegen::deny_payment::< - ::Env, - >()?; - } - let result: >::Output = >::CALLABLE(input); - let output_value = ::ink::reflect::ConstructorOutputValue::new( - result, - ); - let output_result = <::ink::reflect::ConstructorOutputValue< - >::Output, - > as ::ink::reflect::ConstructorOutput< - Fungibles, - >>::as_result(&output_value); - if let ::core::result::Result::Ok(contract) = output_result - .as_ref() - { - ::ink::env::set_contract_storage::< - ::ink::primitives::Key, - Fungibles, - >( - &::KEY, - contract, - ); - } - let mut flag = ::ink::env::ReturnFlags::empty(); - if output_result.is_err() { - flag = ::ink::env::ReturnFlags::REVERT; - } - ::ink::env::return_value::< - ::ink::ConstructorResult< - ::core::result::Result< - (), - &<::ink::reflect::ConstructorOutputValue< - >::Output, - > as ::ink::reflect::ConstructorOutput>::Error, - >, - >, - >( - flag, - &::ink::ConstructorResult::Ok(output_result.map(|_| ())), - ); - } - } - } - } - impl ::ink::reflect::ContractConstructorDecoder for Fungibles { - type Type = __ink_ConstructorDecoder; - } - }; - const _: () = { - #[allow(non_camel_case_types)] - pub enum __ink_MessageDecoder { - Message0( - >::Input, - ), - Message1( - >::Input, - ), - Message2( - >::Input, - ), - Message3( - >::Input, - ), - Message4( - >::Input, - ), - } - impl ::ink::reflect::DecodeDispatch for __ink_MessageDecoder { - fn decode_dispatch( - input: &mut I, - ) -> ::core::result::Result - where - I: ::ink::scale::Input, - { - const MESSAGE_0: [::core::primitive::u8; 4usize] = >::SELECTOR; - const MESSAGE_1: [::core::primitive::u8; 4usize] = >::SELECTOR; - const MESSAGE_2: [::core::primitive::u8; 4usize] = >::SELECTOR; - const MESSAGE_3: [::core::primitive::u8; 4usize] = >::SELECTOR; - const MESSAGE_4: [::core::primitive::u8; 4usize] = >::SELECTOR; - match <[::core::primitive::u8; 4usize] as ::ink::scale::Decode>::decode( - input, - ) - .map_err(|_| ::ink::reflect::DispatchError::InvalidSelector)? - { - MESSAGE_0 => { - ::core::result::Result::Ok( - Self::Message0( - <>::Input as ::ink::scale::Decode>::decode(input) - .map_err(|_| { - ::ink::reflect::DispatchError::InvalidParameters - })?, - ), - ) - } - MESSAGE_1 => { - ::core::result::Result::Ok( - Self::Message1( - <>::Input as ::ink::scale::Decode>::decode(input) - .map_err(|_| { - ::ink::reflect::DispatchError::InvalidParameters - })?, - ), - ) - } - MESSAGE_2 => { - ::core::result::Result::Ok( - Self::Message2( - <>::Input as ::ink::scale::Decode>::decode(input) - .map_err(|_| { - ::ink::reflect::DispatchError::InvalidParameters - })?, - ), - ) - } - MESSAGE_3 => { - ::core::result::Result::Ok( - Self::Message3( - <>::Input as ::ink::scale::Decode>::decode(input) - .map_err(|_| { - ::ink::reflect::DispatchError::InvalidParameters - })?, - ), - ) - } - MESSAGE_4 => { - ::core::result::Result::Ok( - Self::Message4( - <>::Input as ::ink::scale::Decode>::decode(input) - .map_err(|_| { - ::ink::reflect::DispatchError::InvalidParameters - })?, - ), - ) - } - _invalid => { - ::core::result::Result::Err( - ::ink::reflect::DispatchError::UnknownSelector, - ) - } - } - } - } - impl ::ink::scale::Decode for __ink_MessageDecoder { - fn decode( - input: &mut I, - ) -> ::core::result::Result - where - I: ::ink::scale::Input, - { - ::decode_dispatch(input) - .map_err(::core::convert::Into::into) - } - } - fn push_contract(contract: ::core::mem::ManuallyDrop, mutates: bool) { - if mutates { - ::ink::env::set_contract_storage::< - ::ink::primitives::Key, - Fungibles, - >(&::KEY, &contract); - } - } - impl ::ink::reflect::ExecuteDispatchable for __ink_MessageDecoder { - #[allow(clippy::nonminimal_bool, clippy::let_unit_value)] - fn execute_dispatchable( - self, - ) -> ::core::result::Result<(), ::ink::reflect::DispatchError> { - let key = ::KEY; - let mut contract: ::core::mem::ManuallyDrop = ::core::mem::ManuallyDrop::new( - match ::ink::env::get_contract_storage(&key) { - ::core::result::Result::Ok( - ::core::option::Option::Some(value), - ) => value, - ::core::result::Result::Ok(::core::option::Option::None) => { - ::core::panicking::panic_fmt( - format_args!("storage entry was empty"), - ); - } - ::core::result::Result::Err(_) => { - ::core::panicking::panic_fmt( - format_args!("could not properly decode storage entry"), - ); - } - }, - ); - match self { - Self::Message0(input) => { - if { - false - || { - let message_0 = false; - let message_0 = >::PAYABLE; - message_0 - } - || { - let message_1 = false; - let message_1 = >::PAYABLE; - message_1 - } - || { - let message_2 = false; - let message_2 = >::PAYABLE; - message_2 - } - || { - let message_3 = false; - let message_3 = >::PAYABLE; - message_3 - } - || { - let message_4 = false; - let message_4 = >::PAYABLE; - message_4 - } - } - && !>::PAYABLE - { - ::ink::codegen::deny_payment::< - ::Env, - >()?; - } - let result: >::Output = >::CALLABLE(&mut contract, input); - let is_reverted = { - #[allow(unused_imports)] - use ::ink::result_info::IsResultTypeFallback as _; - ::ink::result_info::IsResultType::< - >::Output, - >::VALUE - } - && { - #[allow(unused_imports)] - use ::ink::result_info::IsResultErrFallback as _; - ::ink::result_info::IsResultErr(&result).value() - }; - let mut flag = ::ink::env::ReturnFlags::REVERT; - if !is_reverted { - flag = ::ink::env::ReturnFlags::empty(); - push_contract( - contract, - >::MUTATES, - ); - } - ::ink::env::return_value::< - ::ink::MessageResult< - >::Output, - >, - >(flag, &::ink::MessageResult::Ok(result)) - } - Self::Message1(input) => { - if { - false - || { - let message_0 = false; - let message_0 = >::PAYABLE; - message_0 - } - || { - let message_1 = false; - let message_1 = >::PAYABLE; - message_1 - } - || { - let message_2 = false; - let message_2 = >::PAYABLE; - message_2 - } - || { - let message_3 = false; - let message_3 = >::PAYABLE; - message_3 - } - || { - let message_4 = false; - let message_4 = >::PAYABLE; - message_4 - } - } - && !>::PAYABLE - { - ::ink::codegen::deny_payment::< - ::Env, - >()?; - } - let result: >::Output = >::CALLABLE(&mut contract, input); - let is_reverted = { - #[allow(unused_imports)] - use ::ink::result_info::IsResultTypeFallback as _; - ::ink::result_info::IsResultType::< - >::Output, - >::VALUE - } - && { - #[allow(unused_imports)] - use ::ink::result_info::IsResultErrFallback as _; - ::ink::result_info::IsResultErr(&result).value() - }; - let mut flag = ::ink::env::ReturnFlags::REVERT; - if !is_reverted { - flag = ::ink::env::ReturnFlags::empty(); - push_contract( - contract, - >::MUTATES, - ); - } - ::ink::env::return_value::< - ::ink::MessageResult< - >::Output, - >, - >(flag, &::ink::MessageResult::Ok(result)) - } - Self::Message2(input) => { - if { - false - || { - let message_0 = false; - let message_0 = >::PAYABLE; - message_0 - } - || { - let message_1 = false; - let message_1 = >::PAYABLE; - message_1 - } - || { - let message_2 = false; - let message_2 = >::PAYABLE; - message_2 - } - || { - let message_3 = false; - let message_3 = >::PAYABLE; - message_3 - } - || { - let message_4 = false; - let message_4 = >::PAYABLE; - message_4 - } - } - && !>::PAYABLE - { - ::ink::codegen::deny_payment::< - ::Env, - >()?; - } - let result: >::Output = >::CALLABLE(&mut contract, input); - let is_reverted = { - #[allow(unused_imports)] - use ::ink::result_info::IsResultTypeFallback as _; - ::ink::result_info::IsResultType::< - >::Output, - >::VALUE - } - && { - #[allow(unused_imports)] - use ::ink::result_info::IsResultErrFallback as _; - ::ink::result_info::IsResultErr(&result).value() - }; - let mut flag = ::ink::env::ReturnFlags::REVERT; - if !is_reverted { - flag = ::ink::env::ReturnFlags::empty(); - push_contract( - contract, - >::MUTATES, - ); - } - ::ink::env::return_value::< - ::ink::MessageResult< - >::Output, - >, - >(flag, &::ink::MessageResult::Ok(result)) - } - Self::Message3(input) => { - if { - false - || { - let message_0 = false; - let message_0 = >::PAYABLE; - message_0 - } - || { - let message_1 = false; - let message_1 = >::PAYABLE; - message_1 - } - || { - let message_2 = false; - let message_2 = >::PAYABLE; - message_2 - } - || { - let message_3 = false; - let message_3 = >::PAYABLE; - message_3 - } - || { - let message_4 = false; - let message_4 = >::PAYABLE; - message_4 - } - } - && !>::PAYABLE - { - ::ink::codegen::deny_payment::< - ::Env, - >()?; - } - let result: >::Output = >::CALLABLE(&mut contract, input); - let is_reverted = { - #[allow(unused_imports)] - use ::ink::result_info::IsResultTypeFallback as _; - ::ink::result_info::IsResultType::< - >::Output, - >::VALUE - } - && { - #[allow(unused_imports)] - use ::ink::result_info::IsResultErrFallback as _; - ::ink::result_info::IsResultErr(&result).value() - }; - let mut flag = ::ink::env::ReturnFlags::REVERT; - if !is_reverted { - flag = ::ink::env::ReturnFlags::empty(); - push_contract( - contract, - >::MUTATES, - ); - } - ::ink::env::return_value::< - ::ink::MessageResult< - >::Output, - >, - >(flag, &::ink::MessageResult::Ok(result)) - } - Self::Message4(input) => { - if { - false - || { - let message_0 = false; - let message_0 = >::PAYABLE; - message_0 - } - || { - let message_1 = false; - let message_1 = >::PAYABLE; - message_1 - } - || { - let message_2 = false; - let message_2 = >::PAYABLE; - message_2 - } - || { - let message_3 = false; - let message_3 = >::PAYABLE; - message_3 - } - || { - let message_4 = false; - let message_4 = >::PAYABLE; - message_4 - } - } - && !>::PAYABLE - { - ::ink::codegen::deny_payment::< - ::Env, - >()?; - } - let result: >::Output = >::CALLABLE(&mut contract, input); - let is_reverted = { - #[allow(unused_imports)] - use ::ink::result_info::IsResultTypeFallback as _; - ::ink::result_info::IsResultType::< - >::Output, - >::VALUE - } - && { - #[allow(unused_imports)] - use ::ink::result_info::IsResultErrFallback as _; - ::ink::result_info::IsResultErr(&result).value() - }; - let mut flag = ::ink::env::ReturnFlags::REVERT; - if !is_reverted { - flag = ::ink::env::ReturnFlags::empty(); - push_contract( - contract, - >::MUTATES, - ); - } - ::ink::env::return_value::< - ::ink::MessageResult< - >::Output, - >, - >(flag, &::ink::MessageResult::Ok(result)) - } - }; - } - } - impl ::ink::reflect::ContractMessageDecoder for Fungibles { - type Type = __ink_MessageDecoder; - } - }; - const _: () = { - use ::ink::codegen::{Env as _, StaticEnv as _}; - const _: ::ink::codegen::utils::IsSameType = ::ink::codegen::utils::IsSameType::< - Fungibles, - >::new(); - impl Fungibles { - #[cfg(not(feature = "__ink_dylint_Constructor"))] - pub fn new() -> Self { - ::ink_env::debug_message( - &{ - let res = ::alloc::fmt::format( - format_args!( - "{0}\n", - { - let res = ::alloc::fmt::format( - format_args!("PopApiAssetsExample::new"), - ); - res - }, - ), - ); - res - }, - ); - Default::default() - } - pub fn total_supply(&self, id: AssetId) -> Result { - total_supply(id).map_err(From::from) - } - pub fn balance_of( - &self, - id: AssetId, - owner: AccountId32, - ) -> Result { - balance_of(id, owner).map_err(From::from) - } - pub fn allowance( - &self, - id: AssetId, - owner: AccountId32, - spender: AccountId32, - ) -> Result { - allowance(id, owner, spender).map_err(From::from) - } - pub fn asset_exists(&self, id: AssetId) -> Result { - asset_exists(id).map_err(From::from) - } - pub fn mint_asset( - &self, - id: u32, - beneficiary: AccountId32, - amount: Balance, - ) -> Result<()> { - ::ink_env::debug_message( - &{ - let res = ::alloc::fmt::format( - format_args!( - "{0}\n", - { - let res = ::alloc::fmt::format( - format_args!( - "PopApiAssetsExample::mint_asset_through_runtime: id: {0:?} beneficiary: {1:?} amount: {2:?}", - id, - beneficiary, - amount, - ), - ); - res - }, - ), - ); - res - }, - ); - let result = mint(id, beneficiary, amount)?; - ::ink_env::debug_message( - &{ - let res = ::alloc::fmt::format( - format_args!( - "{0}\n", - { - let res = ::alloc::fmt::format( - format_args!("Result: {0:?}", result), - ); - res - }, - ), - ); - res - }, - ); - Ok(()) - } - } - const _: () = { - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchInput, - >(); - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchOutput>, - >(); - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchInput, - >(); - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchInput, - >(); - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchOutput>, - >(); - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchInput, - >(); - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchInput, - >(); - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchInput, - >(); - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchOutput>, - >(); - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchInput, - >(); - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchOutput>, - >(); - ::ink::codegen::utils::consume_type::<::ink::codegen::DispatchInput>(); - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchInput, - >(); - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchInput, - >(); - ::ink::codegen::utils::consume_type::< - ::ink::codegen::DispatchOutput>, - >(); - }; - }; - const _: () = { - #[codec(crate = ::ink::scale)] - #[scale_info(crate = ::ink::scale_info)] - /// The ink! smart contract's call builder. - /// - /// Implements the underlying on-chain calling of the ink! smart contract - /// messages and trait implementations in a type safe way. - #[repr(transparent)] - pub struct CallBuilder { - account_id: AccountId, - } - #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] - const _: () = { - impl ::ink::scale_info::TypeInfo for CallBuilder { - type Identity = Self; - fn type_info() -> ::ink::scale_info::Type { - ::ink::scale_info::Type::builder() - .path( - ::ink::scale_info::Path::new_with_replace( - "CallBuilder", - "fungibles::fungibles", - &[], - ), - ) - .type_params(::alloc::vec::Vec::new()) - .docs( - &[ - "The ink! smart contract's call builder.", - "", - "Implements the underlying on-chain calling of the ink! smart contract", - "messages and trait implementations in a type safe way.", - ], - ) - .composite( - ::ink::scale_info::build::Fields::named() - .field(|f| { - f - .ty::() - .name("account_id") - .type_name("AccountId") - }), - ) - } - } - }; - #[allow(deprecated)] - const _: () = { - #[automatically_derived] - impl ::ink::scale::Decode for CallBuilder { - fn decode<__CodecInputEdqy: ::ink::scale::Input>( - __codec_input_edqy: &mut __CodecInputEdqy, - ) -> ::core::result::Result { - ::core::result::Result::Ok(CallBuilder { - account_id: { - let __codec_res_edqy = ::decode( - __codec_input_edqy, - ); - match __codec_res_edqy { - ::core::result::Result::Err(e) => { - return ::core::result::Result::Err( - e.chain("Could not decode `CallBuilder::account_id`"), - ); - } - ::core::result::Result::Ok(__codec_res_edqy) => { - __codec_res_edqy - } - } - }, - }) - } - fn decode_into<__CodecInputEdqy: ::ink::scale::Input>( - __codec_input_edqy: &mut __CodecInputEdqy, - dst_: &mut ::core::mem::MaybeUninit, - ) -> ::core::result::Result< - ::ink::scale::DecodeFinished, - ::ink::scale::Error, - > { - match ( - &::core::mem::size_of::(), - &::core::mem::size_of::(), - ) { - (left_val, right_val) => { - if !(*left_val == *right_val) { - let kind = ::core::panicking::AssertKind::Eq; - ::core::panicking::assert_failed( - kind, - &*left_val, - &*right_val, - ::core::option::Option::None, - ); - } - } - }; - if !(if ::core::mem::size_of::() > 0 { 1 } else { 0 } - <= 1) - { - ::core::panicking::panic( - "assertion failed: if ::core::mem::size_of::() > 0 { 1 } else { 0 } <= 1", - ) - } - { - let dst_: &mut ::core::mem::MaybeUninit = dst_; - let dst_: &mut ::core::mem::MaybeUninit = unsafe { - &mut *dst_ - .as_mut_ptr() - .cast::<::core::mem::MaybeUninit>() - }; - ::decode_into( - __codec_input_edqy, - dst_, - )?; - } - unsafe { - ::core::result::Result::Ok( - ::ink::scale::DecodeFinished::assert_decoding_finished(), - ) - } - } - } - }; - #[allow(deprecated)] - const _: () = { - #[automatically_derived] - impl ::ink::scale::Encode for CallBuilder { - fn size_hint(&self) -> usize { - ::ink::scale::Encode::size_hint(&&self.account_id) - } - fn encode_to< - __CodecOutputEdqy: ::ink::scale::Output + ?::core::marker::Sized, - >(&self, __codec_dest_edqy: &mut __CodecOutputEdqy) { - ::ink::scale::Encode::encode_to(&&self.account_id, __codec_dest_edqy) - } - fn encode( - &self, - ) -> ::ink::scale::alloc::vec::Vec<::core::primitive::u8> { - ::ink::scale::Encode::encode(&&self.account_id) - } - fn using_encoded< - __CodecOutputReturn, - __CodecUsingEncodedCallback: ::core::ops::FnOnce( - &[::core::primitive::u8], - ) -> __CodecOutputReturn, - >(&self, f: __CodecUsingEncodedCallback) -> __CodecOutputReturn { - ::ink::scale::Encode::using_encoded(&&self.account_id, f) - } - } - #[automatically_derived] - impl ::ink::scale::EncodeLike for CallBuilder {} - }; - #[automatically_derived] - impl ::core::fmt::Debug for CallBuilder { - #[inline] - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - ::core::fmt::Formatter::debug_struct_field1_finish( - f, - "CallBuilder", - "account_id", - &&self.account_id, - ) - } - } - #[automatically_derived] - impl ::core::hash::Hash for CallBuilder { - #[inline] - fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { - ::core::hash::Hash::hash(&self.account_id, state) - } - } - #[automatically_derived] - impl ::core::marker::StructuralPartialEq for CallBuilder {} - #[automatically_derived] - impl ::core::cmp::PartialEq for CallBuilder { - #[inline] - fn eq(&self, other: &CallBuilder) -> bool { - self.account_id == other.account_id - } - } - #[automatically_derived] - impl ::core::cmp::Eq for CallBuilder { - #[inline] - #[doc(hidden)] - #[coverage(off)] - fn assert_receiver_is_total_eq(&self) -> () { - let _: ::core::cmp::AssertParamIsEq; - } - } - #[automatically_derived] - impl ::core::clone::Clone for CallBuilder { - #[inline] - fn clone(&self) -> CallBuilder { - CallBuilder { - account_id: ::core::clone::Clone::clone(&self.account_id), - } - } - } - const _: () = { - impl ::ink::storage::traits::StorageLayout for CallBuilder { - fn layout( - __key: &::ink::primitives::Key, - ) -> ::ink::metadata::layout::Layout { - ::ink::metadata::layout::Layout::Struct( - ::ink::metadata::layout::StructLayout::new( - "CallBuilder", - [ - ::ink::metadata::layout::FieldLayout::new( - "account_id", - ::layout( - __key, - ), - ), - ], - ), - ) - } - } - }; - const _: () = { - impl ::ink::codegen::ContractCallBuilder for Fungibles { - type Type = CallBuilder; - } - impl ::ink::env::ContractEnv for CallBuilder { - type Env = ::Env; - } - }; - impl ::ink::env::call::FromAccountId for CallBuilder { - #[inline] - fn from_account_id(account_id: AccountId) -> Self { - Self { account_id } - } - } - impl ::ink::ToAccountId for CallBuilder { - #[inline] - fn to_account_id(&self) -> AccountId { - ::clone(&self.account_id) - } - } - impl ::core::convert::AsRef for CallBuilder { - fn as_ref(&self) -> &AccountId { - &self.account_id - } - } - impl ::core::convert::AsMut for CallBuilder { - fn as_mut(&mut self) -> &mut AccountId { - &mut self.account_id - } - } - impl CallBuilder { - #[allow(clippy::type_complexity)] - #[inline] - pub fn total_supply( - &self, - __ink_binding_0: AssetId, - ) -> ::ink::env::call::CallBuilder< - Environment, - ::ink::env::call::utils::Set<::ink::env::call::Call>, - ::ink::env::call::utils::Set< - ::ink::env::call::ExecutionInput< - ::ink::env::call::utils::ArgumentList< - ::ink::env::call::utils::Argument, - ::ink::env::call::utils::EmptyArgumentList, - >, - >, - >, - ::ink::env::call::utils::Set< - ::ink::env::call::utils::ReturnType>, - >, - > { - ::ink::env::call::build_call::() - .call(::ink::ToAccountId::to_account_id(self)) - .exec_input( - ::ink::env::call::ExecutionInput::new( - ::ink::env::call::Selector::new([ - 0xDB_u8, - 0x63_u8, - 0x75_u8, - 0xA8_u8, - ]), - ) - .push_arg(__ink_binding_0), - ) - .returns::>() - } - #[allow(clippy::type_complexity)] - #[inline] - pub fn balance_of( - &self, - __ink_binding_0: AssetId, - __ink_binding_1: AccountId32, - ) -> ::ink::env::call::CallBuilder< - Environment, - ::ink::env::call::utils::Set<::ink::env::call::Call>, - ::ink::env::call::utils::Set< - ::ink::env::call::ExecutionInput< - ::ink::env::call::utils::ArgumentList< - ::ink::env::call::utils::Argument, - ::ink::env::call::utils::ArgumentList< - ::ink::env::call::utils::Argument, - ::ink::env::call::utils::EmptyArgumentList, - >, - >, - >, - >, - ::ink::env::call::utils::Set< - ::ink::env::call::utils::ReturnType>, - >, - > { - ::ink::env::call::build_call::() - .call(::ink::ToAccountId::to_account_id(self)) - .exec_input( - ::ink::env::call::ExecutionInput::new( - ::ink::env::call::Selector::new([ - 0x0F_u8, - 0x75_u8, - 0x5A_u8, - 0x56_u8, - ]), - ) - .push_arg(__ink_binding_0) - .push_arg(__ink_binding_1), - ) - .returns::>() - } - #[allow(clippy::type_complexity)] - #[inline] - pub fn allowance( - &self, - __ink_binding_0: AssetId, - __ink_binding_1: AccountId32, - __ink_binding_2: AccountId32, - ) -> ::ink::env::call::CallBuilder< - Environment, - ::ink::env::call::utils::Set<::ink::env::call::Call>, - ::ink::env::call::utils::Set< - ::ink::env::call::ExecutionInput< - ::ink::env::call::utils::ArgumentList< - ::ink::env::call::utils::Argument, - ::ink::env::call::utils::ArgumentList< - ::ink::env::call::utils::Argument, - ::ink::env::call::utils::ArgumentList< - ::ink::env::call::utils::Argument, - ::ink::env::call::utils::EmptyArgumentList, - >, - >, - >, - >, - >, - ::ink::env::call::utils::Set< - ::ink::env::call::utils::ReturnType>, - >, - > { - ::ink::env::call::build_call::() - .call(::ink::ToAccountId::to_account_id(self)) - .exec_input( - ::ink::env::call::ExecutionInput::new( - ::ink::env::call::Selector::new([ - 0x6A_u8, - 0x00_u8, - 0x16_u8, - 0x5E_u8, - ]), - ) - .push_arg(__ink_binding_0) - .push_arg(__ink_binding_1) - .push_arg(__ink_binding_2), - ) - .returns::>() - } - #[allow(clippy::type_complexity)] - #[inline] - pub fn asset_exists( - &self, - __ink_binding_0: AssetId, - ) -> ::ink::env::call::CallBuilder< - Environment, - ::ink::env::call::utils::Set<::ink::env::call::Call>, - ::ink::env::call::utils::Set< - ::ink::env::call::ExecutionInput< - ::ink::env::call::utils::ArgumentList< - ::ink::env::call::utils::Argument, - ::ink::env::call::utils::EmptyArgumentList, - >, - >, - >, - ::ink::env::call::utils::Set< - ::ink::env::call::utils::ReturnType>, - >, - > { - ::ink::env::call::build_call::() - .call(::ink::ToAccountId::to_account_id(self)) - .exec_input( - ::ink::env::call::ExecutionInput::new( - ::ink::env::call::Selector::new([ - 0xAA_u8, - 0x6B_u8, - 0x65_u8, - 0xDB_u8, - ]), - ) - .push_arg(__ink_binding_0), - ) - .returns::>() - } - #[allow(clippy::type_complexity)] - #[inline] - pub fn mint_asset( - &self, - __ink_binding_0: u32, - __ink_binding_1: AccountId32, - __ink_binding_2: Balance, - ) -> ::ink::env::call::CallBuilder< - Environment, - ::ink::env::call::utils::Set<::ink::env::call::Call>, - ::ink::env::call::utils::Set< - ::ink::env::call::ExecutionInput< - ::ink::env::call::utils::ArgumentList< - ::ink::env::call::utils::Argument, - ::ink::env::call::utils::ArgumentList< - ::ink::env::call::utils::Argument, - ::ink::env::call::utils::ArgumentList< - ::ink::env::call::utils::Argument, - ::ink::env::call::utils::EmptyArgumentList, - >, - >, - >, - >, - >, - ::ink::env::call::utils::Set< - ::ink::env::call::utils::ReturnType>, - >, - > { - ::ink::env::call::build_call::() - .call(::ink::ToAccountId::to_account_id(self)) - .exec_input( - ::ink::env::call::ExecutionInput::new( - ::ink::env::call::Selector::new([ - 0x1F_u8, - 0x8E_u8, - 0x8E_u8, - 0x22_u8, - ]), - ) - .push_arg(__ink_binding_0) - .push_arg(__ink_binding_1) - .push_arg(__ink_binding_2), - ) - .returns::>() - } - } - }; - #[codec(crate = ::ink::scale)] - #[scale_info(crate = ::ink::scale_info)] - pub struct FungiblesRef { - inner: ::Type, - } - #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] - const _: () = { - impl ::ink::scale_info::TypeInfo for FungiblesRef { - type Identity = Self; - fn type_info() -> ::ink::scale_info::Type { - ::ink::scale_info::Type::builder() - .path( - ::ink::scale_info::Path::new_with_replace( - "FungiblesRef", - "fungibles::fungibles", - &[], - ), - ) - .type_params(::alloc::vec::Vec::new()) - .composite( - ::ink::scale_info::build::Fields::named() - .field(|f| { - f - .ty::< - ::Type, - >() - .name("inner") - .type_name( - "::Type", - ) - }), - ) - } - } - }; - #[allow(deprecated)] - const _: () = { - #[automatically_derived] - impl ::ink::scale::Decode for FungiblesRef { - fn decode<__CodecInputEdqy: ::ink::scale::Input>( - __codec_input_edqy: &mut __CodecInputEdqy, - ) -> ::core::result::Result { - ::core::result::Result::Ok(FungiblesRef { - inner: { - let __codec_res_edqy = <::Type as ::ink::scale::Decode>::decode( - __codec_input_edqy, - ); - match __codec_res_edqy { - ::core::result::Result::Err(e) => { - return ::core::result::Result::Err( - e.chain("Could not decode `FungiblesRef::inner`"), - ); - } - ::core::result::Result::Ok(__codec_res_edqy) => { - __codec_res_edqy - } - } - }, - }) - } - } - }; - #[allow(deprecated)] - const _: () = { - #[automatically_derived] - impl ::ink::scale::Encode for FungiblesRef { - fn size_hint(&self) -> usize { - ::ink::scale::Encode::size_hint(&&self.inner) - } - fn encode_to< - __CodecOutputEdqy: ::ink::scale::Output + ?::core::marker::Sized, - >(&self, __codec_dest_edqy: &mut __CodecOutputEdqy) { - ::ink::scale::Encode::encode_to(&&self.inner, __codec_dest_edqy) - } - fn encode(&self) -> ::ink::scale::alloc::vec::Vec<::core::primitive::u8> { - ::ink::scale::Encode::encode(&&self.inner) - } - fn using_encoded< - __CodecOutputReturn, - __CodecUsingEncodedCallback: ::core::ops::FnOnce( - &[::core::primitive::u8], - ) -> __CodecOutputReturn, - >(&self, f: __CodecUsingEncodedCallback) -> __CodecOutputReturn { - ::ink::scale::Encode::using_encoded(&&self.inner, f) - } - } - #[automatically_derived] - impl ::ink::scale::EncodeLike for FungiblesRef {} - }; - #[automatically_derived] - impl ::core::fmt::Debug for FungiblesRef { - #[inline] - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - ::core::fmt::Formatter::debug_struct_field1_finish( - f, - "FungiblesRef", - "inner", - &&self.inner, - ) - } - } - #[automatically_derived] - impl ::core::hash::Hash for FungiblesRef { - #[inline] - fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { - ::core::hash::Hash::hash(&self.inner, state) - } - } - #[automatically_derived] - impl ::core::marker::StructuralPartialEq for FungiblesRef {} - #[automatically_derived] - impl ::core::cmp::PartialEq for FungiblesRef { - #[inline] - fn eq(&self, other: &FungiblesRef) -> bool { - self.inner == other.inner - } - } - #[automatically_derived] - impl ::core::cmp::Eq for FungiblesRef { - #[inline] - #[doc(hidden)] - #[coverage(off)] - fn assert_receiver_is_total_eq(&self) -> () { - let _: ::core::cmp::AssertParamIsEq< - ::Type, - >; - } - } - #[automatically_derived] - impl ::core::clone::Clone for FungiblesRef { - #[inline] - fn clone(&self) -> FungiblesRef { - FungiblesRef { - inner: ::core::clone::Clone::clone(&self.inner), - } - } - } - const _: () = { - impl ::ink::storage::traits::StorageLayout for FungiblesRef { - fn layout( - __key: &::ink::primitives::Key, - ) -> ::ink::metadata::layout::Layout { - ::ink::metadata::layout::Layout::Struct( - ::ink::metadata::layout::StructLayout::new( - "FungiblesRef", - [ - ::ink::metadata::layout::FieldLayout::new( - "inner", - <::Type as ::ink::storage::traits::StorageLayout>::layout( - __key, - ), - ), - ], - ), - ) - } - } - }; - const _: () = { - impl ::ink::env::ContractReference for Fungibles { - type Type = FungiblesRef; - } - impl ::ink::env::call::ConstructorReturnType for Fungibles { - type Output = FungiblesRef; - type Error = (); - fn ok(value: FungiblesRef) -> Self::Output { - value - } - } - impl ::ink::env::call::ConstructorReturnType - for ::core::result::Result - where - E: ::ink::scale::Decode, - { - const IS_RESULT: bool = true; - type Output = ::core::result::Result; - type Error = E; - fn ok(value: FungiblesRef) -> Self::Output { - ::core::result::Result::Ok(value) - } - fn err(err: Self::Error) -> ::core::option::Option { - ::core::option::Option::Some(::core::result::Result::Err(err)) - } - } - impl ::ink::env::ContractEnv for FungiblesRef { - type Env = ::Env; - } - }; - impl FungiblesRef { - #[inline] - #[allow(clippy::type_complexity)] - pub fn new() -> ::ink::env::call::CreateBuilder< - Environment, - Self, - ::ink::env::call::utils::Unset, - ::ink::env::call::utils::Set< - ::ink::env::call::LimitParamsV2< - ::Env, - >, - >, - ::ink::env::call::utils::Unset, - ::ink::env::call::utils::Set< - ::ink::env::call::ExecutionInput< - ::ink::env::call::utils::EmptyArgumentList, - >, - >, - ::ink::env::call::utils::Unset<::ink::env::call::state::Salt>, - ::ink::env::call::utils::Set<::ink::env::call::utils::ReturnType>, - > { - ::ink::env::call::build_create::() - .exec_input( - ::ink::env::call::ExecutionInput::new( - ::ink::env::call::Selector::new([ - 0x9B_u8, - 0xAE_u8, - 0x9D_u8, - 0x5E_u8, - ]), - ), - ) - .returns::() - } - #[inline] - pub fn total_supply(&self, id: AssetId) -> Result { - self.try_total_supply(id) - .unwrap_or_else(|error| { - ::core::panicking::panic_fmt( - format_args!( - "encountered error while calling {0}::{1}: {2:?}", - "Fungibles", - "total_supply", - error, - ), - ); - }) - } - #[inline] - pub fn try_total_supply( - &self, - id: AssetId, - ) -> ::ink::MessageResult> { - ::call(self) - .total_supply(id) - .try_invoke() - .unwrap_or_else(|error| { - ::core::panicking::panic_fmt( - format_args!( - "encountered error while calling {0}::{1}: {2:?}", - "Fungibles", - "total_supply", - error, - ), - ); - }) - } - #[inline] - pub fn balance_of(&self, id: AssetId, owner: AccountId32) -> Result { - self.try_balance_of(id, owner) - .unwrap_or_else(|error| { - ::core::panicking::panic_fmt( - format_args!( - "encountered error while calling {0}::{1}: {2:?}", - "Fungibles", - "balance_of", - error, - ), - ); - }) - } - #[inline] - pub fn try_balance_of( - &self, - id: AssetId, - owner: AccountId32, - ) -> ::ink::MessageResult> { - ::call(self) - .balance_of(id, owner) - .try_invoke() - .unwrap_or_else(|error| { - ::core::panicking::panic_fmt( - format_args!( - "encountered error while calling {0}::{1}: {2:?}", - "Fungibles", - "balance_of", - error, - ), - ); - }) - } - #[inline] - pub fn allowance( - &self, - id: AssetId, - owner: AccountId32, - spender: AccountId32, - ) -> Result { - self.try_allowance(id, owner, spender) - .unwrap_or_else(|error| { - ::core::panicking::panic_fmt( - format_args!( - "encountered error while calling {0}::{1}: {2:?}", - "Fungibles", - "allowance", - error, - ), - ); - }) - } - #[inline] - pub fn try_allowance( - &self, - id: AssetId, - owner: AccountId32, - spender: AccountId32, - ) -> ::ink::MessageResult> { - ::call(self) - .allowance(id, owner, spender) - .try_invoke() - .unwrap_or_else(|error| { - ::core::panicking::panic_fmt( - format_args!( - "encountered error while calling {0}::{1}: {2:?}", - "Fungibles", - "allowance", - error, - ), - ); - }) - } - #[inline] - pub fn asset_exists(&self, id: AssetId) -> Result { - self.try_asset_exists(id) - .unwrap_or_else(|error| { - ::core::panicking::panic_fmt( - format_args!( - "encountered error while calling {0}::{1}: {2:?}", - "Fungibles", - "asset_exists", - error, - ), - ); - }) - } - #[inline] - pub fn try_asset_exists( - &self, - id: AssetId, - ) -> ::ink::MessageResult> { - ::call(self) - .asset_exists(id) - .try_invoke() - .unwrap_or_else(|error| { - ::core::panicking::panic_fmt( - format_args!( - "encountered error while calling {0}::{1}: {2:?}", - "Fungibles", - "asset_exists", - error, - ), - ); - }) - } - #[inline] - pub fn mint_asset( - &self, - id: u32, - beneficiary: AccountId32, - amount: Balance, - ) -> Result<()> { - self.try_mint_asset(id, beneficiary, amount) - .unwrap_or_else(|error| { - ::core::panicking::panic_fmt( - format_args!( - "encountered error while calling {0}::{1}: {2:?}", - "Fungibles", - "mint_asset", - error, - ), - ); - }) - } - #[inline] - pub fn try_mint_asset( - &self, - id: u32, - beneficiary: AccountId32, - amount: Balance, - ) -> ::ink::MessageResult> { - ::call(self) - .mint_asset(id, beneficiary, amount) - .try_invoke() - .unwrap_or_else(|error| { - ::core::panicking::panic_fmt( - format_args!( - "encountered error while calling {0}::{1}: {2:?}", - "Fungibles", - "mint_asset", - error, - ), - ); - }) - } - } - const _: () = { - impl ::ink::codegen::TraitCallBuilder for FungiblesRef { - type Builder = ::Type; - #[inline] - fn call(&self) -> &Self::Builder { - &self.inner - } - #[inline] - fn call_mut(&mut self) -> &mut Self::Builder { - &mut self.inner - } - } - }; - impl ::ink::env::call::FromAccountId for FungiblesRef { - #[inline] - fn from_account_id(account_id: AccountId) -> Self { - Self { - inner: <::Type as ::ink::env::call::FromAccountId< - Environment, - >>::from_account_id(account_id), - } - } - } - impl ::ink::ToAccountId for FungiblesRef { - #[inline] - fn to_account_id(&self) -> AccountId { - <::Type as ::ink::ToAccountId< - Environment, - >>::to_account_id(&self.inner) - } - } - impl ::core::convert::AsRef for FungiblesRef { - fn as_ref(&self) -> &AccountId { - <_ as ::core::convert::AsRef>::as_ref(&self.inner) - } - } - impl ::core::convert::AsMut for FungiblesRef { - fn as_mut(&mut self) -> &mut AccountId { - <_ as ::core::convert::AsMut>::as_mut(&mut self.inner) - } - } - #[cfg(feature = "std")] - #[cfg(not(feature = "ink-as-dependency"))] - const _: () = { - #[no_mangle] - pub fn __ink_generate_metadata() -> ::ink::metadata::InkProject { - let layout = ::ink::metadata::layout::Layout::Root( - ::ink::metadata::layout::RootLayout::new( - <::ink::metadata::layout::LayoutKey as ::core::convert::From< - ::ink::primitives::Key, - >>::from(::KEY), - ::layout( - &::KEY, - ), - ::ink::scale_info::meta_type::(), - ), - ); - ::ink::metadata::layout::ValidateLayout::validate(&layout) - .unwrap_or_else(|error| { - { - ::core::panicking::panic_fmt( - format_args!("metadata ink! generation failed: {0}", error), - ); - } - }); - ::ink::metadata::InkProject::new( - layout, - ::ink::metadata::ContractSpec::new() - .constructors([ - ::ink::metadata::ConstructorSpec::from_label("new") - .selector([0x9B_u8, 0xAE_u8, 0x9D_u8, 0x5E_u8]) - .args([]) - .payable(true) - .default(false) - .returns( - ::ink::metadata::ReturnTypeSpec::new( - if >::IS_RESULT { - ::ink::metadata::TypeSpec::with_name_str::< - ::ink::ConstructorResult< - ::core::result::Result< - (), - >::Error, - >, - >, - >("ink_primitives::ConstructorResult") - } else { - ::ink::metadata::TypeSpec::with_name_str::< - ::ink::ConstructorResult<()>, - >("ink_primitives::ConstructorResult") - }, - ), - ) - .docs([]) - .done(), - ]) - .messages([ - ::ink::metadata::MessageSpec::from_label("total_supply") - .selector([0xDB_u8, 0x63_u8, 0x75_u8, 0xA8_u8]) - .args([ - ::ink::metadata::MessageParamSpec::new("id") - .of_type( - ::ink::metadata::TypeSpec::with_name_segs::< - AssetId, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["AssetId"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .done(), - ]) - .returns( - ::ink::metadata::ReturnTypeSpec::new( - ::ink::metadata::TypeSpec::with_name_segs::< - ::ink::MessageResult>, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter([ - "ink", - "MessageResult", - ]), - ::core::convert::AsRef::as_ref, - ), - ), - ), - ) - .mutates(false) - .payable(false) - .default(false) - .docs([]) - .done(), - ::ink::metadata::MessageSpec::from_label("balance_of") - .selector([0x0F_u8, 0x75_u8, 0x5A_u8, 0x56_u8]) - .args([ - ::ink::metadata::MessageParamSpec::new("id") - .of_type( - ::ink::metadata::TypeSpec::with_name_segs::< - AssetId, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["AssetId"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .done(), - ::ink::metadata::MessageParamSpec::new("owner") - .of_type( - ::ink::metadata::TypeSpec::with_name_segs::< - AccountId32, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["AccountId32"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .done(), - ]) - .returns( - ::ink::metadata::ReturnTypeSpec::new( - ::ink::metadata::TypeSpec::with_name_segs::< - ::ink::MessageResult>, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter([ - "ink", - "MessageResult", - ]), - ::core::convert::AsRef::as_ref, - ), - ), - ), - ) - .mutates(false) - .payable(false) - .default(false) - .docs([]) - .done(), - ::ink::metadata::MessageSpec::from_label("allowance") - .selector([0x6A_u8, 0x00_u8, 0x16_u8, 0x5E_u8]) - .args([ - ::ink::metadata::MessageParamSpec::new("id") - .of_type( - ::ink::metadata::TypeSpec::with_name_segs::< - AssetId, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["AssetId"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .done(), - ::ink::metadata::MessageParamSpec::new("owner") - .of_type( - ::ink::metadata::TypeSpec::with_name_segs::< - AccountId32, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["AccountId32"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .done(), - ::ink::metadata::MessageParamSpec::new("spender") - .of_type( - ::ink::metadata::TypeSpec::with_name_segs::< - AccountId32, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["AccountId32"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .done(), - ]) - .returns( - ::ink::metadata::ReturnTypeSpec::new( - ::ink::metadata::TypeSpec::with_name_segs::< - ::ink::MessageResult>, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter([ - "ink", - "MessageResult", - ]), - ::core::convert::AsRef::as_ref, - ), - ), - ), - ) - .mutates(false) - .payable(false) - .default(false) - .docs([]) - .done(), - ::ink::metadata::MessageSpec::from_label("asset_exists") - .selector([0xAA_u8, 0x6B_u8, 0x65_u8, 0xDB_u8]) - .args([ - ::ink::metadata::MessageParamSpec::new("id") - .of_type( - ::ink::metadata::TypeSpec::with_name_segs::< - AssetId, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["AssetId"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .done(), - ]) - .returns( - ::ink::metadata::ReturnTypeSpec::new( - ::ink::metadata::TypeSpec::with_name_segs::< - ::ink::MessageResult>, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter([ - "ink", - "MessageResult", - ]), - ::core::convert::AsRef::as_ref, - ), - ), - ), - ) - .mutates(false) - .payable(false) - .default(false) - .docs([]) - .done(), - ::ink::metadata::MessageSpec::from_label("mint_asset") - .selector([0x1F_u8, 0x8E_u8, 0x8E_u8, 0x22_u8]) - .args([ - ::ink::metadata::MessageParamSpec::new("id") - .of_type( - ::ink::metadata::TypeSpec::with_name_segs::< - u32, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["u32"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .done(), - ::ink::metadata::MessageParamSpec::new("beneficiary") - .of_type( - ::ink::metadata::TypeSpec::with_name_segs::< - AccountId32, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["AccountId32"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .done(), - ::ink::metadata::MessageParamSpec::new("amount") - .of_type( - ::ink::metadata::TypeSpec::with_name_segs::< - Balance, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["Balance"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .done(), - ]) - .returns( - ::ink::metadata::ReturnTypeSpec::new( - ::ink::metadata::TypeSpec::with_name_segs::< - ::ink::MessageResult>, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter([ - "ink", - "MessageResult", - ]), - ::core::convert::AsRef::as_ref, - ), - ), - ), - ) - .mutates(false) - .payable(false) - .default(false) - .docs([]) - .done(), - ]) - .collect_events() - .docs([]) - .lang_error( - ::ink::metadata::TypeSpec::with_name_segs::< - ::ink::LangError, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["ink", "LangError"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .environment( - ::ink::metadata::EnvironmentSpec::new() - .account_id( - ::ink::metadata::TypeSpec::with_name_segs::< - AccountId, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["AccountId"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .balance( - ::ink::metadata::TypeSpec::with_name_segs::< - Balance, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["Balance"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .hash( - ::ink::metadata::TypeSpec::with_name_segs::< - Hash, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["Hash"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .timestamp( - ::ink::metadata::TypeSpec::with_name_segs::< - Timestamp, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["Timestamp"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .block_number( - ::ink::metadata::TypeSpec::with_name_segs::< - BlockNumber, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["BlockNumber"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .chain_extension( - ::ink::metadata::TypeSpec::with_name_segs::< - ChainExtension, - _, - >( - ::core::iter::Iterator::map( - ::core::iter::IntoIterator::into_iter(["ChainExtension"]), - ::core::convert::AsRef::as_ref, - ), - ), - ) - .max_event_topics(MAX_EVENT_TOPICS) - .static_buffer_size(::ink::env::BUFFER_SIZE) - .done(), - ) - .done(), - ) - } - }; - use super::*; -} diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs index 68e43865..59040590 100755 --- a/pop-api/examples/fungibles/lib.rs +++ b/pop-api/examples/fungibles/lib.rs @@ -7,61 +7,12 @@ /// use ink::prelude::vec::Vec; use pop_api::{ - assets::fungibles::*, + assets::use_cases::fungibles as api, + error::PopApiError, primitives::{AccountId as AccountId32, AssetId}, }; -#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum ContractError { - /// The asset is not live; either frozen or being destroyed. - AssetNotLive, - /// The amount to mint is less than the existential deposit. - BelowMinimum, - /// Unspecified dispatch error, providing the index and its error index (if none `0`). - DispatchError { index: u8, error: u8 }, - /// Not enough allowance to fulfill a request is available. - InsufficientAllowance, - /// Not enough balance to fulfill a request is available. - InsufficientBalance, - /// The asset ID is already taken. - InUse, - /// Minimum balance should be non-zero. - MinBalanceZero, - /// Unspecified pallet error, providing pallet index and error index. - ModuleError { pallet: u8, error: u16 }, - /// The account to alter does not exist. - NoAccount, - /// The signing account has no permission to do the operation. - NoPermission, - /// The given asset ID is unknown. - Unknown, -} - -impl From for ContractError { - fn from(error: FungiblesError) -> Self { - match error { - FungiblesError::AssetNotLive => ContractError::AssetNotLive, - FungiblesError::BelowMinimum => ContractError::BelowMinimum, - FungiblesError::DispatchError { index, error } => { - ContractError::DispatchError { index, error } - }, - FungiblesError::InsufficientAllowance => ContractError::InsufficientAllowance, - FungiblesError::InsufficientBalance => ContractError::InsufficientBalance, - FungiblesError::InUse => ContractError::InUse, - FungiblesError::MinBalanceZero => ContractError::MinBalanceZero, - FungiblesError::ModuleError { pallet, error } => { - ContractError::ModuleError { pallet, error } - }, - FungiblesError::NoAccount => ContractError::NoAccount, - FungiblesError::NoPermission => ContractError::NoPermission, - FungiblesError::Unknown => ContractError::Unknown, - } - } -} - -/// The fungibles result type. -pub type Result = core::result::Result; +pub type Result = core::result::Result; #[ink::contract(env = pop_api::Environment)] mod fungibles { @@ -74,7 +25,7 @@ mod fungibles { impl Fungibles { #[ink(constructor, payable)] pub fn new() -> Self { - ink::env::debug_println!("PopApiAssetsExample::new"); + ink::env::debug_println!("PopApiFungiblesExample::new"); Default::default() } @@ -90,12 +41,12 @@ mod fungibles { #[ink(message)] pub fn total_supply(&self, id: AssetId) -> Result { - total_supply(id).map_err(From::from) + api::total_supply(id) } #[ink(message)] pub fn balance_of(&self, id: AssetId, owner: AccountId32) -> Result { - balance_of(id, owner).map_err(From::from) + api::balance_of(id, owner) } #[ink(message)] @@ -105,21 +56,21 @@ mod fungibles { owner: AccountId32, spender: AccountId32, ) -> Result { - allowance(id, owner, spender).map_err(From::from) + api::allowance(id, owner, spender) } #[ink(message)] pub fn transfer(&self, id: AssetId, to: AccountId32, value: Balance) -> Result<()> { ink::env::debug_println!( - "PopApiAssetsExample::transfer: id: {:?}, to: {:?} value: {:?}", + "PopApiFungiblesExample::transfer: id: {:?}, to: {:?} value: {:?}", id, to, value, ); - let result = transfer(id, to, value); + let result = api::transfer(id, to, value); ink::env::debug_println!("Result: {:?}", result); - result.map_err(From::from) + result } #[ink(message)] @@ -129,20 +80,21 @@ mod fungibles { from: Option, to: Option, value: Balance, - // Size needs to be known at compile time or ink's `Vec` + // In the standard a `[u8]`, but the size needs to be known at compile time ink's `Vec` + // has to be used. data: Vec, ) -> Result<()> { ink::env::debug_println!( - "PopApiAssetsExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}", + "PopApiFungiblesExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}", id, from, to, value, ); - let result = transfer_from(id, from, to, value, &data); + let result = api::transfer_from(id, from, to, value, &data); ink::env::debug_println!("Result: {:?}", result); - result.map_err(From::from) + result } /// 2. PSP-22 Metadata Interface: @@ -162,14 +114,14 @@ mod fungibles { #[ink(message)] pub fn create(&self, id: AssetId, admin: AccountId32, min_balance: Balance) -> Result<()> { ink::env::debug_println!( - "PopApiAssetsExample::create: id: {:?} admin: {:?} min_balance: {:?}", + "PopApiFungiblesExample::create: id: {:?} admin: {:?} min_balance: {:?}", id, admin, min_balance, ); - let result = create(id, admin, min_balance); + let result = api::create(id, admin, min_balance); ink::env::debug_println!("Result: {:?}", result); - result.map_err(From::from) + result } #[ink(message)] @@ -181,20 +133,20 @@ mod fungibles { decimals: u8, ) -> Result<()> { ink::env::debug_println!( - "PopApiAssetsExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}", + "PopApiFungiblesExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}", id, name, symbol, decimals, ); - let result = set_metadata(id, name, symbol, decimals); + let result = api::set_metadata(id, name, symbol, decimals); ink::env::debug_println!("Result: {:?}", result); - result.map_err(From::from) + result } #[ink(message)] pub fn asset_exists(&self, id: AssetId) -> Result { - asset_exists(id).map_err(From::from) + api::asset_exists(id) } } @@ -204,7 +156,7 @@ mod fungibles { #[ink::test] fn default_works() { - PopApiAssetsExample::new(); + PopApiFungiblesExample::new(); } } } diff --git a/pop-api/src/error.rs b/pop-api/src/error.rs new file mode 100644 index 00000000..92da4045 --- /dev/null +++ b/pop-api/src/error.rs @@ -0,0 +1,255 @@ +use crate::assets::use_cases::fungibles::{convert_to_fungibles_error, FungiblesError}; +use ink::env::chain_extension::FromStatusCode; +use scale::{Decode, Encode}; +use PopApiError::*; + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +#[repr(u8)] +pub enum PopApiError { + /// Some unknown error occurred. Go to the Pop API docs section `Pop API error`. + Other { + // Index within the `DispatchError` + dispatch_error_index: u8, + // Index within the `DispatchError` variant. + error_index: u8, + // Index for further nesting, e.g. pallet error. + error: u8, + } = 0, + /// Failed to lookup some data. + CannotLookup = 1, + /// A bad origin. + BadOrigin = 2, + /// A custom error in a module. + Module { + index: u8, + error: u8, + } = 3, + /// At least one consumer is remaining so the account cannot be destroyed. + ConsumerRemaining = 4, + /// There are no providers so the account cannot be created. + NoProviders = 5, + /// There are too many consumers so the account cannot be created. + TooManyConsumers = 6, + /// An error to do with tokens. + Token(TokenError) = 7, + /// An arithmetic error. + Arithmetic(ArithmeticError) = 8, + /// The number of transactional layers has been reached, or we are not in a transactional + /// layer. + Transactional(TransactionalError) = 9, + /// Resources exhausted, e.g. attempt to read/write data which is too large to manipulate. + Exhausted = 10, + /// The state is corrupt; this is generally not going to fix itself. + Corruption = 11, + /// Some resource (e.g. a preimage) is unavailable right now. This might fix itself later. + Unavailable = 12, + /// Root origin is not allowed. + RootNotAllowed = 13, + // TODO: make generic and add docs. + UseCaseError(FungiblesError) = 254, + DecodingFailed = 255, +} + +impl FromStatusCode for PopApiError { + fn from_status_code(status_code: u32) -> core::result::Result<(), Self> { + match status_code { + 0 => Ok(()), + _ => Err(convert_to_pop_api_error(status_code)), + } + } +} + +// `pub` because it is used in `runtime/devnet/src/extensions/tests/mod.rs`'s test: +// `dispatch_error_to_status_code_to_pop_api_error_works` +// +// This function converts a given `status_code` (u32) into a `PopApiError`. First it encodes the +// status code into a 4-byte array and checks for unknown nested errors. If decoding into +// `PopApiError` fails (e.g. a breaking change in the `DispatchError`), it handles the error by +// converting it to the `Other` variant by shifting each byte one position forward (the last byte is +// not used for anything)and setting the first byte to 0. If decoding succeeds, it checks if the +// error is of the `Module` variant and performs any necessary conversion based on the use case. +pub fn convert_to_pop_api_error(status_code: u32) -> PopApiError { + let mut encoded: [u8; 4] = + status_code.encode().try_into().expect("qid u32 always encodes to 4 bytes"); + encoded = check_for_unknown_nesting(encoded); + let error = match PopApiError::decode(&mut &encoded[..]) { + Err(_) => { + encoded[3] = encoded[2]; + encoded[2] = encoded[1]; + encoded[1] = encoded[0]; + encoded[0] = 0; + PopApiError::decode(&mut &encoded[..]).unwrap().into() + }, + Ok(error) => { + if let crate::PopApiError::Module { index, error } = error { + // TODO: make generic. + convert_to_fungibles_error(index, error) + } else { + error + } + }, + }; + ink::env::debug_println!("PopApiError: {:?}", error); + error +} + +// If a unknown nested variant of the `DispatchError` is detected meaning any of the subsequent +// bytes are non-zero (e.g. breaking change in the DispatchError), the error needs to be converted +// into `PopApiError::Other`'s encoded value. This conversion is done by shifting the bytes one +// position forward (the last byte is discarded as it is not being used) and replacing the first +// byte with the `Other` encoded value (0u8). This ensures that the error is correctly categorized +// as an `Other` variant. +fn check_for_unknown_nesting(encoded_error: [u8; 4]) -> [u8; 4] { + if non_nested_pop_api_errors().contains(&encoded_error[0]) + && encoded_error[1..].iter().any(|x| *x != 0u8) + { + [0u8, encoded_error[0], encoded_error[1], encoded_error[2]] + } else if singular_nested_pop_api_errors().contains(&encoded_error[0]) + && encoded_error[2..].iter().any(|x| *x != 0u8) + { + [0u8, encoded_error[0], encoded_error[1], encoded_error[2]] + } else { + encoded_error + } +} + +impl From for PopApiError { + fn from(_: scale::Error) -> Self { + DecodingFailed + } +} +#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum TokenError { + /// Funds are unavailable. + FundsUnavailable, + /// Some part of the balance gives the only provider reference to the account and thus cannot + /// be (re)moved. + OnlyProvider, + /// Account cannot exist with the funds that would be given. + BelowMinimum, + /// Account cannot be created. + CannotCreate, + /// The asset in question is unknown. + UnknownAsset, + /// Funds exist but are frozen. + Frozen, + /// Operation is not supported by the asset. + Unsupported, + /// Account cannot be created for a held balance. + CannotCreateHold, + /// Withdrawal would cause unwanted loss of account. + NotExpendable, + /// Account cannot receive the assets. + Blocked, +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum ArithmeticError { + /// Underflow. + Underflow, + /// Overflow. + Overflow, + /// Division by zero. + DivisionByZero, +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum TransactionalError { + /// Too many transactional layers have been spawned. + LimitReached, + /// A transactional layer was expected, but does not exist. + NoLayer, +} + +fn singular_nested_pop_api_errors() -> [u8; 3] { + const TOKEN_ERROR: u8 = 7; + const ARITHMETIC_ERROR: u8 = 8; + const TRANSACTION_ERROR: u8 = 9; + [TOKEN_ERROR, ARITHMETIC_ERROR, TRANSACTION_ERROR] +} + +fn non_nested_pop_api_errors() -> [u8; 9] { + const CANNOT_LOOKUP: u8 = 1; + const BAD_ORIGIN: u8 = 2; + const CONSUMER_REMAINING: u8 = 4; + const NO_PROVIDERS: u8 = 5; + const TOO_MANY_CONSUMERS: u8 = 6; + const EXHAUSTED: u8 = 10; + const CORRUPTION: u8 = 11; + const UNAVAILABLE: u8 = 12; + const ROOT_NOT_ALLOWED: u8 = 13; + [ + CANNOT_LOOKUP, + BAD_ORIGIN, + CONSUMER_REMAINING, + NO_PROVIDERS, + TOO_MANY_CONSUMERS, + EXHAUSTED, + CORRUPTION, + UNAVAILABLE, + ROOT_NOT_ALLOWED, + ] +} + +#[test] +fn u32_always_encodes_to_4_bytes() { + assert_eq!(0u32.encode().len(), 4); + assert_eq!(u32::MAX.encode().len(), 4); +} + +// If decoding failed the encoded value is converted to the `PopApiError::Other`. This handles +// unknown errors coming from the runtime. This could happen if a contract is not upgraded to the +// latest Pop API version. +#[test] +fn test_non_existing_pop_api_errors() { + let encoded_error = [7u8, 100u8, 0u8, 0u8]; + let status_code = u32::decode(&mut &encoded_error[..]).unwrap(); + let pop_api_error = ::from_status_code(status_code); + assert_eq!(Err(Other { dispatch_error_index: 7, error_index: 100, error: 0 }), pop_api_error); +} + +// If the encoded value indicates a nested PopApiError which is not handled by the Pop API version, +// the encoded value is converted into `PopApiError::Other`. +#[test] +fn check_for_unknown_nested_pop_api_errors_works() { + for &error_code in &non_nested_pop_api_errors() { + let encoded_error = [error_code, 1, 2, 3]; + let result = check_for_unknown_nesting(encoded_error); + let decoded = PopApiError::decode(&mut &result[..]).unwrap(); + + assert_eq!( + decoded, + Other { dispatch_error_index: error_code, error_index: 1, error: 2 }, + "Failed for error code: {}", + error_code + ); + } + for &error_code in &singular_nested_pop_api_errors() { + let encoded_error = [error_code, 1, 2, 3]; + let result = check_for_unknown_nesting(encoded_error); + let decoded = PopApiError::decode(&mut &result[..]).unwrap(); + + assert_eq!( + decoded, + Other { dispatch_error_index: error_code, error_index: 1, error: 2 }, + "Failed for error code: {}", + error_code + ); + } +} + +// This test ensures that a non-zero value for unused bytes does not interfere with the correct +// decoding of the error. It verifies that even with an additional byte, the errors are correctly +// decoded and represented in its correct variant. +#[test] +fn extra_byte_does_not_mess_up_decoding() { + // Module error + let encoded_error = [3u8, 4u8, 5u8, 6u8]; + let status_code = u32::decode(&mut &encoded_error[..]).unwrap(); + let pop_api_error = ::from_status_code(status_code); + assert_eq!(Err(Module { index: 4, error: 5 }), pop_api_error); +} diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index be448890..a785aebe 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -1,19 +1,19 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -use core::convert::TryInto; +use crate::error::PopApiError; use ink::{prelude::vec::Vec, ChainExtensionInstance}; use primitives::{cross_chain::*, storage_keys::*, AccountId as AccountId32}; +use scale::Encode; pub use sp_runtime::{BoundedVec, MultiAddress, MultiSignature}; use v0::RuntimeCall; -pub use v0::{ - assets, balances, contracts, cross_chain, dispatch_error, nfts, relay_chain_block_number, state, -}; +pub use v0::{assets, balances, cross_chain, nfts, relay_chain_block_number, state}; +pub mod error; pub mod primitives; pub mod v0; -// type AccountId = ::AccountId; type AccountId = AccountId32; +// TODO: do the same as above and check expanded code. type Balance = ::Balance; type BlockNumber = ::BlockNumber; type StringLimit = u32; @@ -21,54 +21,6 @@ type MaxTips = u32; pub type Result = core::result::Result; -#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum PopApiError { - Assets(assets::fungibles::AssetsError), - Balances(balances::BalancesError), - Contracts(contracts::Error), - DecodingFailed, - Nfts(nfts::Error), - SystemCallFiltered, - TokenError(dispatch_error::TokenError), - UnknownModuleStatusCode(u32), - UnknownDispatchStatusCode(u32), - Xcm(cross_chain::Error), -} - -impl ink::env::chain_extension::FromStatusCode for PopApiError { - fn from_status_code(status_code: u32) -> core::result::Result<(), Self> { - use crate::PopApiError::{ - Assets, Balances, Contracts, Nfts, TokenError, UnknownDispatchStatusCode, - UnknownModuleStatusCode, - }; - - match status_code { - 0 => Ok(()), - 3_000_000..=3_999_999 => { - let status_code = status_code - 3_000_000; - match status_code { - // CallFiltered originates from `frame_system` with pallet-index 0. The CallFiltered error is at index 5 - 5 => Err(PopApiError::SystemCallFiltered), - 10_000..=10_999 => Err(Balances((status_code - 10_000).try_into()?)), - 40_000..=40_999 => Err(Contracts((status_code - 40_000).try_into()?)), - 50_000..=50_999 => Err(Nfts((status_code - 50_000).try_into()?)), - 52_000..=52_999 => Err(Assets((status_code - 52_000).try_into()?)), - _ => Err(UnknownModuleStatusCode(status_code)), - } - }, - 7_000_000..=7_999_999 => Err(TokenError((status_code - 7_000_000).try_into()?)), - _ => Err(UnknownDispatchStatusCode(status_code)), - } - } -} - -impl From for PopApiError { - fn from(_: scale::Error) -> Self { - panic!("encountered unexpected invalid SCALE encoding") - } -} - #[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum Environment {} diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs deleted file mode 100644 index df9033c7..00000000 --- a/pop-api/src/v0/assets/fungibles.rs +++ /dev/null @@ -1,610 +0,0 @@ -use crate::{ - balances::BalancesError, AccountId, Balance, PopApiError::UnknownModuleStatusCode, RuntimeCall, - *, -}; -use ink::prelude::vec::Vec; -use primitives::AssetId; -use scale::{Compact, Encode}; - -type Result = core::result::Result; - -/// Local Fungibles: -/// 1. PSP-22 Interface -/// 2. PSP-22 Metadata Interface -/// 3. Asset Management - -/// 1. PSP-22 Interface: -/// - total_supply -/// - balance_of -/// - allowance -/// - transfer -/// - transfer_from -/// - approve -/// - increase_allowance -/// - decrease_allowance - -/// Returns the total token supply for a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// The total supply of the token, or an error if the operation fails. -pub fn total_supply(id: AssetId) -> Result { - Ok(state::read(RuntimeStateKeys::Assets(AssetsKeys::TotalSupply(id)))?) -} - -/// Returns the account balance for the specified `owner` for a given asset ID. Returns `0` if -/// the account is non-existent. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `owner` - The account whose balance is being queried. -/// -/// # Returns -/// The balance of the specified account, or an error if the operation fails. -pub fn balance_of(id: AssetId, owner: AccountId) -> Result { - Ok(state::read(RuntimeStateKeys::Assets(AssetsKeys::BalanceOf(id, owner)))?) -} - -/// Returns the amount which `spender` is still allowed to withdraw from `owner` for a given -/// asset ID. Returns `0` if no allowance has been set. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `owner` - The account that owns the tokens. -/// * `spender` - The account that is allowed to spend the tokens. -/// -/// # Returns -/// The remaining allowance, or an error if the operation fails. -pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { - Ok(state::read(RuntimeStateKeys::Assets(AssetsKeys::Allowance(id, owner, spender)))?) -} - -/// Transfers `value` amount of tokens from the caller's account to account `to`, with additional -/// `data` in unspecified format. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `to` - The recipient account. -/// * `value` - The number of tokens to transfer. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the transfer fails. -pub fn transfer( - id: AssetId, - to: impl Into>, - value: Balance, -) -> Result<()> { - // TODO: transfer or transfer_keep_alive - // Ok(dispatch(RuntimeCall::Assets(AssetsCall::Transfer { - // id: id.into(), - // target: target.into(), - // amount: Compact(amount), - // }))?) - Ok(dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { - id: id.into(), - target: to.into(), - amount: Compact(value), - }))?) -} - -/// Transfers `value` tokens on behalf of `from` to account `to` with additional `data` -/// in unspecified format. If `from` is equal to `None`, tokens will be minted to account `to`. If -/// `to` is equal to `None`, tokens will be burned from account `from`. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `from` - The account from which the tokens are transferred. -/// * `to` - The recipient account. -/// * `value` - The number of tokens to transfer. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the transfer fails. -pub fn transfer_from( - id: AssetId, - from: Option>>, - to: Option>>, - value: Balance, - _data: &[u8], -) -> Result<()> { - match (from, to) { - (None, Some(to)) => mint(id, to, value), - // (Some(from), None) => burn(id, from, value), - (Some(from), Some(to)) => { - Ok(dispatch(RuntimeCall::Assets(AssetsCall::TransferApproved { - id: id.into(), - owner: from.into(), - destination: to.into(), - amount: Compact(value), - }))?) - }, - _ => Ok(()), - } -} - -/// Approves an account to spend a specified number of tokens on behalf of the caller. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `spender` - The account that is allowed to spend the tokens. -/// * `value` - The number of tokens to approve. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the approval fails. -// #[allow(unused_variables)] -// fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { -// todo!() -// // TODO: read allowance and increase or decrease. -// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { -// // id: id.into(), -// // delegate: spender.into(), -// // amount: Compact(value), -// // }))?) -// } - -/// Increases the allowance of a spender. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `spender` - The account that is allowed to spend the tokens. -/// * `value` - The number of tokens to increase the allowance by. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -// fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { -// id: id.into(), -// delegate: spender.into(), -// amount: Compact(value), -// }))?) -// } - -/// Decreases the allowance of a spender. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `spender` - The account that is allowed to spend the tokens. -/// * `value` - The number of tokens to decrease the allowance by. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -// #[allow(unused_variables)] -// fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { -// todo!() -// // TODO: cancel_approval + approve_transfer -// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::CancelApproval { -// // id: id.into(), -// // delegate: delegate.into(), -// // }))?) -// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { -// // id: id.into(), -// // delegate: spender.into(), -// // amount: Compact(value), -// // }))?) -// } - -/// 2. PSP-22 Metadata Interface: -/// - token_name -/// - token_symbol -/// - token_decimals - -/// Returns the token name for a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// The name of the token as a byte vector, or an error if the operation fails. -// #[allow(unused_variables)] -// pub fn token_name(id: AssetId) -> Result>> { -// todo!() -// // Ok(state::read(RuntimeStateKeys::Assets(AssetsKeys::TokenName(id)))?) -// } - -/// Returns the token symbol for a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// The symbol of the token as a byte vector, or an error if the operation fails. -// #[allow(unused_variables)] -// fn token_symbol(id: AssetId) -> Result>> { -// todo!() -// } - -/// Returns the token decimals for a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// The number of decimals of the token as a byte vector, or an error if the operation fails. -// #[allow(unused_variables)] -// fn token_decimals(id: AssetId) -> Result>> { -// todo!() -// } - -/// 3. Asset Management: -/// - create -/// - start_destroy -/// - destroy_accounts -/// - destroy_approvals -/// - finish_destroy -/// - set_metadata -/// - clear_metadata - -/// Create a new token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `admin` - The account that will administer the asset. -/// * `min_balance` - The minimum balance required for accounts holding this asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the creation fails. -pub fn create( - id: AssetId, - admin: impl Into>, - min_balance: Balance, -) -> Result<()> { - Ok(dispatch(RuntimeCall::Assets(AssetsCall::Create { - id: id.into(), - admin: admin.into(), - min_balance, - }))?) -} - -/// Start the process of destroying a token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -// fn start_destroy(id: AssetId) -> Result<()> { -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::StartDestroy { -// id: id.into(), -// }))?) -// } - -/// Destroy all accounts associated with a token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -// fn destroy_accounts(id: AssetId) -> Result<()> { -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::DestroyAccounts { -// id: id.into(), -// }))?) -// } - -/// Destroy all approvals associated with a token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -// fn destroy_approvals(id: AssetId) -> Result<()> { -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::DestroyApprovals { -// id: id.into(), -// }))?) -// } - -/// Complete the process of destroying a token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -// fn finish_destroy(id: AssetId) -> Result<()> { -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::FinishDestroy { -// id: id.into(), -// }))?) -// } - -/// Set the metadata for a token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) -> Result<()> { - Ok(dispatch(RuntimeCall::Assets(AssetsCall::SetMetadata { - id: id.into(), - name, - symbol, - decimals, - }))?) -} - -/// Clear the metadata for a token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -// fn clear_metadata(id: AssetId) -> Result<()> { -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::ClearMetadata { -// id: id.into(), -// }))?) -// } - -pub fn asset_exists(id: AssetId) -> Result { - Ok(state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id)))?) -} - -/// Mint assets of a particular class. -fn mint( - id: AssetId, - beneficiary: impl Into>, - amount: Balance, -) -> Result<()> { - Ok(dispatch(RuntimeCall::Assets(AssetsCall::Mint { - id: id.into(), - beneficiary: beneficiary.into(), - amount: Compact(amount), - }))?) -} - -// Parameters to extrinsics representing an asset id (`AssetIdParameter`) and a balance amount -// (`Balance`) are expected to be compact encoded. The pop api handles that for the developer. -// https://substrate.stackexchange.com/questions/1873/what-is-the-meaning-of-palletcompact-in-pallet-development -// -// Asset id that is compact encoded. -type AssetIdParameter = Compact; -// Balance amount that is compact encoded. -type BalanceParameter = Compact; - -#[allow(warnings, unused)] -#[derive(Encode)] -pub(crate) enum AssetsCall { - #[codec(index = 0)] - Create { id: AssetIdParameter, admin: MultiAddress, min_balance: Balance }, - #[codec(index = 2)] - StartDestroy { id: AssetIdParameter }, - #[codec(index = 3)] - DestroyAccounts { id: AssetIdParameter }, - #[codec(index = 4)] - DestroyApprovals { id: AssetIdParameter }, - #[codec(index = 5)] - FinishDestroy { id: AssetIdParameter }, - #[codec(index = 6)] - Mint { - id: AssetIdParameter, - beneficiary: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 7)] - Burn { id: AssetIdParameter, who: MultiAddress, amount: BalanceParameter }, - // TODO: ED or not - // #[codec(index = 8)] - // Transfer { id: AssetIdParameter, target: MultiAddress, amount: BalanceParameter }, - #[codec(index = 9)] - TransferKeepAlive { - id: AssetIdParameter, - target: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 17)] - SetMetadata { id: AssetIdParameter, name: Vec, symbol: Vec, decimals: u8 }, - #[codec(index = 18)] - ClearMetadata { id: AssetIdParameter }, - #[codec(index = 22)] - ApproveTransfer { - id: AssetIdParameter, - delegate: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 23)] - CancelApproval { id: AssetIdParameter, delegate: MultiAddress }, - #[codec(index = 25)] - TransferApproved { - id: AssetIdParameter, - owner: MultiAddress, - destination: MultiAddress, - amount: BalanceParameter, - }, -} - -#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub(crate) enum AssetsError { - /// Account balance must be greater than or equal to the transfer amount. - BalanceLow, - /// The account to alter does not exist. - NoAccount, - /// The signing account has no permission to do the operation. - NoPermission, - /// The given asset ID is unknown. - Unknown, - /// The origin account is frozen. - Frozen, - /// The asset ID is already taken. - InUse, - /// Invalid witness data given. - BadWitness, - /// Minimum balance should be non-zero. - MinBalanceZero, - /// Unable to increment the consumer reference counters on the account. Either no provider - /// reference exists to allow a non-zero balance of a non-self-sufficient asset, or one - /// fewer then the maximum number of consumers has been reached. - UnavailableConsumer, - /// Invalid metadata given. - BadMetadata, - /// No approval exists that would allow the transfer. - Unapproved, - /// The source account would not survive the transfer and it needs to stay alive. - WouldDie, - /// The asset-account already exists. - AlreadyExists, - /// The asset-account doesn't have an associated deposit. - NoDeposit, - /// The operation would result in funds being burned. - WouldBurn, - /// The asset is a live asset and is actively being used. Usually emit for operations such - /// as `start_destroy` which require the asset to be in a destroying state. - LiveAsset, - /// The asset is not live, and likely being destroyed. - AssetNotLive, - /// The asset status is not the expected status. - IncorrectStatus, - /// The asset should be frozen before the given operation. - NotFrozen, - /// Callback action resulted in error - CallbackFailed, -} - -impl From for AssetsError { - fn from(error: PopApiError) -> Self { - match error { - PopApiError::Assets(e) => e, - _ => panic!("Expected AssetsError"), - } - } -} - -impl TryFrom for AssetsError { - type Error = PopApiError; - - fn try_from(status_code: u32) -> core::result::Result { - use AssetsError::*; - match status_code { - 0 => Ok(BalanceLow), - 1 => Ok(NoAccount), - 2 => Ok(NoPermission), - 3 => Ok(Unknown), - 4 => Ok(Frozen), - 5 => Ok(InUse), - 6 => Ok(BadWitness), - 7 => Ok(MinBalanceZero), - 8 => Ok(UnavailableConsumer), - 9 => Ok(BadMetadata), - 10 => Ok(Unapproved), - 11 => Ok(WouldDie), - 12 => Ok(AlreadyExists), - 13 => Ok(NoDeposit), - 14 => Ok(WouldBurn), - 15 => Ok(LiveAsset), - 16 => Ok(AssetNotLive), - 17 => Ok(IncorrectStatus), - 18 => Ok(NotFrozen), - _ => Err(UnknownModuleStatusCode(status_code)), - } - } -} - -#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum FungiblesError { - /// The asset is not live; either frozen or being destroyed. - AssetNotLive, - /// The amount to mint is less than the existential deposit. - BelowMinimum, - /// Unspecified dispatch error, providing the index and its error index (if none `0`). - DispatchError { index: u8, error: u8 }, - /// Not enough allowance to fulfill a request is available. - InsufficientAllowance, - /// Not enough balance to fulfill a request is available. - InsufficientBalance, - /// The asset ID is already taken. - InUse, - /// Minimum balance should be non-zero. - MinBalanceZero, - /// Unspecified pallet error, providing pallet index and error index. - ModuleError { pallet: u8, error: u16 }, - /// The account to alter does not exist. - NoAccount, - /// The signing account has no permission to do the operation. - NoPermission, - /// The given asset ID is unknown. - Unknown, -} - -impl From for FungiblesError { - fn from(error: BalancesError) -> Self { - match error { - // TODO: this insufficient balance is different than the assets variant. This one is - // for a deposit of creating an asset, the latter is for transfer tokens. - BalancesError::InsufficientBalance => FungiblesError::InsufficientBalance, - _ => FungiblesError::ModuleError { pallet: 40, error: error as u16 }, - } - } -} - -impl From for FungiblesError { - fn from(error: dispatch_error::TokenError) -> Self { - match error { - dispatch_error::TokenError::BelowMinimum => FungiblesError::BelowMinimum, - // ED is not respected. - dispatch_error::TokenError::OnlyProvider => FungiblesError::InsufficientBalance, - dispatch_error::TokenError::UnknownAsset => FungiblesError::Unknown, - _ => FungiblesError::DispatchError { index: 7, error: error as u8 }, - } - } -} - -impl From for FungiblesError { - fn from(error: AssetsError) -> Self { - match error { - AssetsError::AssetNotLive => FungiblesError::AssetNotLive, - AssetsError::BalanceLow => FungiblesError::InsufficientBalance, - AssetsError::Unapproved => FungiblesError::InsufficientAllowance, - AssetsError::InUse => FungiblesError::InUse, - AssetsError::MinBalanceZero => FungiblesError::MinBalanceZero, - AssetsError::NoPermission => FungiblesError::NoPermission, - AssetsError::NoAccount => FungiblesError::NoAccount, - AssetsError::Unknown => FungiblesError::Unknown, - _ => FungiblesError::ModuleError { pallet: 52, error: error as u16 }, - } - } -} - -impl From for FungiblesError { - fn from(error: PopApiError) -> Self { - match error { - PopApiError::Assets(e) => e.into(), - PopApiError::Balances(e) => e.into(), - PopApiError::TokenError(e) => e.into(), - PopApiError::UnknownModuleStatusCode(e) => { - let pallet = (e / 1_000) as u8; - let error = (e % 1_000) as u16; - FungiblesError::ModuleError { pallet, error } - }, - PopApiError::UnknownDispatchStatusCode(e) => { - let index = (e / 1_000_000) as u8; - let error = (3 % 1_000_000) as u8; - FungiblesError::DispatchError { index, error } - }, - _ => todo!(), - } - } -} - -// macro_rules! impl_error_conversion { -// ($pallet_index:, $pallet_error:ty, $interface_error:ty, $($variant:ident),*) => { -// impl From<$pallet_error> for $interface_error { -// fn from(error: $pallet_error) -> Self { -// match error { -// $( -// <$pallet_error>::$variant => <$interface_error>::$variant, -// )* -// _ => <$interface_error>::ModuleError { pallet: 0, error: [255, 0, 0, 0] }, // Default case -// } -// } -// } -// -// impl FromPalletError<$pallet_error> for $interface_error { -// fn from_pallet_error(error: $pallet_error) -> Self { -// Self::from(error) -// } -// } -// }; -// } diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index d6b0261c..736ccc0e 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1 +1,2 @@ -pub mod fungibles; \ No newline at end of file +pub(crate) mod pallets; +pub mod use_cases; diff --git a/pop-api/src/v0/assets/pallets/assets.rs b/pop-api/src/v0/assets/pallets/assets.rs new file mode 100644 index 00000000..7a575e08 --- /dev/null +++ b/pop-api/src/v0/assets/pallets/assets.rs @@ -0,0 +1,491 @@ +// TODO: what to put in this file? +#![allow(dead_code)] + +use crate::{Balance, RuntimeCall, *}; +use ink::prelude::vec::Vec; +use primitives::{AssetId, MultiAddress}; +use scale::{Compact, Encode}; + +type Result = core::result::Result; + +/// [Pallet Assets](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/assets/src/lib.rs): +/// 1. Dispatchables +/// 2. Read state functions +/// +/// 1. Dispatchables within pallet assets (TrustBackedAssets instance) that can be used via the pop api on Pop Network: +/// - create +/// - start_destroy +/// - destroy_accounts +/// - destroy_approvals +/// - finish_destroy +/// - mint +/// - burn +/// - transfer +/// - transfer_keep_alive +/// - force_transfer +/// - freeze +/// - thaw +/// - freeze_asset +/// - thaw_asset +/// - transfer_ownership +/// - set_team +/// - set_metadata +/// - clear_metadata +/// - approve_transfer +/// - cancel_approval +/// - force_cancel_approval +/// - transfer_approved +/// - touch +/// - refund +/// - set_min_balance +/// - touch_other +/// - refund_other +/// - block + +/// Issue a new class of fungible assets from a public origin. +pub(crate) fn create( + id: AssetId, + admin: impl Into>, + min_balance: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Create { + id: id.into(), + admin: admin.into(), + min_balance, + })) +} + +/// Start the process of destroying a fungible asset class. +pub(crate) fn start_destroy(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::StartDestroy { id: id.into() })) +} + +/// Destroy all accounts associated with a given asset. +pub(crate) fn destroy_accounts(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::DestroyAccounts { id: id.into() })) +} + +/// Destroy all approvals associated with a given asset up to the max (see runtime configuration Assets `RemoveItemsLimit`). +pub(crate) fn destroy_approvals(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::DestroyApprovals { id: id.into() })) +} + +/// Complete destroying asset and unreserve currency. +pub(crate) fn finish_destroy(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::FinishDestroy { id: id.into() })) +} + +/// Mint assets of a particular class. +pub(crate) fn mint( + id: AssetId, + beneficiary: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Mint { + id: id.into(), + beneficiary: beneficiary.into(), + amount: Compact(amount), + })) +} + +/// Reduce the balance of `who` by as much as possible up to `amount` assets of `id`. +pub(crate) fn burn( + id: AssetId, + who: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Burn { + id: id.into(), + who: who.into(), + amount: Compact(amount), + })) +} + +/// Move some assets from the sender account to another. +pub(crate) fn transfer( + id: AssetId, + target: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { + id: id.into(), + target: target.into(), + amount: Compact(amount), + })) +} + +/// Move some assets from the sender account to another, keeping the sender account alive. +pub(crate) fn transfer_keep_alive( + id: AssetId, + target: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { + id: id.into(), + target: target.into(), + amount: Compact(amount), + })) +} + +/// Move some assets from one account to another. Sender should be the Admin of the asset `id`. +pub(crate) fn force_transfer( + id: AssetId, + source: impl Into>, + dest: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::ForceTransfer { + id: id.into(), + source: source.into(), + dest: dest.into(), + amount: Compact(amount), + })) +} + +/// Disallow further unprivileged transfers of an asset `id` from an account `who`. `who` +/// must already exist as an entry in `Account`s of the asset. If you want to freeze an +/// account that does not have an entry, use `touch_other` first. +pub(crate) fn freeze(id: AssetId, who: impl Into>) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Freeze { id: id.into(), who: who.into() })) +} + +/// Allow unprivileged transfers to and from an account again. +pub(crate) fn thaw(id: AssetId, who: impl Into>) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Thaw { id: id.into(), who: who.into() })) +} + +/// Disallow further unprivileged transfers for the asset class. +pub(crate) fn freeze_asset(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::FreezeAsset { id: id.into() })) +} + +/// Allow unprivileged transfers for the asset again. +pub(crate) fn thaw_asset(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::ThawAsset { id: id.into() })) +} + +/// Change the Owner of an asset. +pub(crate) fn transfer_ownership( + id: AssetId, + owner: impl Into>, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::TransferOwnership { + id: id.into(), + owner: owner.into(), + })) +} + +/// Change the Issuer, Admin and Freezer of an asset. +pub(crate) fn set_team( + id: AssetId, + issuer: impl Into>, + admin: impl Into>, + freezer: impl Into>, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::SetTeam { + id: id.into(), + issuer: issuer.into(), + admin: admin.into(), + freezer: freezer.into(), + })) +} + +/// Set the metadata for an asset. +pub(crate) fn set_metadata( + id: AssetId, + name: Vec, + symbol: Vec, + decimals: u8, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::SetMetadata { id: id.into(), name, symbol, decimals })) +} + +/// Clear the metadata for an asset. +pub(crate) fn clear_metadata(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::ClearMetadata { id: id.into() })) +} + +/// Approve an amount of asset for transfer by a delegated third-party account. +pub(crate) fn approve_transfer( + id: AssetId, + delegate: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { + id: id.into(), + delegate: delegate.into(), + amount: Compact(amount), + })) +} + +/// Cancel all of some asset approved for delegated transfer by a third-party account. +pub(crate) fn cancel_approval( + id: AssetId, + delegate: impl Into>, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::CancelApproval { + id: id.into(), + delegate: delegate.into(), + })) +} + +/// Cancel all of some asset approved for delegated transfer by a third-party account. +pub(crate) fn force_cancel_approval( + id: AssetId, + owner: impl Into>, + delegate: impl Into>, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::ForceCancelApproval { + id: id.into(), + owner: owner.into(), + delegate: delegate.into(), + })) +} + +/// Transfer some asset balance from a previously delegated account to some third-party +/// account. +pub(crate) fn transfer_approved( + id: AssetId, + owner: impl Into>, + destination: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::TransferApproved { + id: id.into(), + owner: owner.into(), + destination: destination.into(), + amount: Compact(amount), + })) +} + +/// Create an asset account for non-provider assets. +pub(crate) fn touch(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Touch { id: id.into() })) +} + +/// Return the deposit (if any) of an asset account or a consumer reference (if any) of an +/// account. +pub(crate) fn refund(id: AssetId, allow_burn: bool) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Refund { id: id.into(), allow_burn })) +} + +/// Sets the minimum balance of an asset. +pub(crate) fn set_min_balance(id: AssetId, min_balance: Balance) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::SetMinBalance { + id: id.into(), + min_balance: Compact(min_balance), + })) +} + +/// Create an asset account for `who`. +pub(crate) fn touch_other(id: AssetId, who: impl Into>) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::TouchOther { id: id.into(), who: who.into() })) +} + +/// Return the deposit (if any) of a target asset account. Useful if you are the depositor. +pub(crate) fn refund_other(id: AssetId, who: impl Into>) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::RefundOther { id: id.into(), who: who.into() })) +} + +/// Disallow further unprivileged transfers of an asset `id` to and from an account `who`. +pub(crate) fn block(id: AssetId, who: impl Into>) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Block { id: id.into(), who: who.into() })) +} + +/// 2. Read state functions +/// - total_supply +/// - + +pub(crate) fn total_supply(id: AssetId) -> Result { + state::read(RuntimeStateKeys::Assets(AssetsKeys::TotalSupply(id))).into() +} + +pub(crate) fn balance_of(id: AssetId, owner: AccountId) -> Result { + state::read(RuntimeStateKeys::Assets(AssetsKeys::BalanceOf(id, owner))).into() +} + +pub(crate) fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { + state::read(RuntimeStateKeys::Assets(AssetsKeys::Allowance(id, owner, spender))).into() +} +pub(crate) fn asset_exists(id: AssetId) -> Result { + state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id))).into() +} + +// Parameters to extrinsics representing an asset id (`AssetIdParameter`) and a balance amount (`Balance`) are expected +// to be compact encoded. The pop api handles that for the developer. +// +// reference: https://substrate.stackexchange.com/questions/1873/what-is-the-meaning-of-palletcompact-in-pallet-development +// +// Asset id that is compact encoded. +type AssetIdParameter = Compact; +// Balance amount that is compact encoded. +type BalanceParameter = Compact; + +#[derive(Encode)] +pub(crate) enum AssetsCall { + #[codec(index = 0)] + Create { id: AssetIdParameter, admin: MultiAddress, min_balance: Balance }, + #[codec(index = 2)] + StartDestroy { id: AssetIdParameter }, + #[codec(index = 3)] + DestroyAccounts { id: AssetIdParameter }, + #[codec(index = 4)] + DestroyApprovals { id: AssetIdParameter }, + #[codec(index = 5)] + FinishDestroy { id: AssetIdParameter }, + #[codec(index = 6)] + Mint { + id: AssetIdParameter, + beneficiary: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 7)] + Burn { id: AssetIdParameter, who: MultiAddress, amount: BalanceParameter }, + #[codec(index = 8)] + Transfer { id: AssetIdParameter, target: MultiAddress, amount: BalanceParameter }, + #[codec(index = 9)] + TransferKeepAlive { + id: AssetIdParameter, + target: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 10)] + ForceTransfer { + id: AssetIdParameter, + source: MultiAddress, + dest: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 11)] + Freeze { id: AssetIdParameter, who: MultiAddress }, + #[codec(index = 12)] + Thaw { id: AssetIdParameter, who: MultiAddress }, + #[codec(index = 13)] + FreezeAsset { id: AssetIdParameter }, + #[codec(index = 14)] + ThawAsset { id: AssetIdParameter }, + #[codec(index = 15)] + TransferOwnership { id: AssetIdParameter, owner: MultiAddress }, + #[codec(index = 16)] + SetTeam { + id: AssetIdParameter, + issuer: MultiAddress, + admin: MultiAddress, + freezer: MultiAddress, + }, + #[codec(index = 17)] + SetMetadata { id: AssetIdParameter, name: Vec, symbol: Vec, decimals: u8 }, + #[codec(index = 18)] + ClearMetadata { id: AssetIdParameter }, + #[codec(index = 22)] + ApproveTransfer { + id: AssetIdParameter, + delegate: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 23)] + CancelApproval { id: AssetIdParameter, delegate: MultiAddress }, + #[codec(index = 24)] + ForceCancelApproval { + id: AssetIdParameter, + owner: MultiAddress, + delegate: MultiAddress, + }, + #[codec(index = 25)] + TransferApproved { + id: AssetIdParameter, + owner: MultiAddress, + destination: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 26)] + Touch { id: AssetIdParameter }, + #[codec(index = 27)] + Refund { id: AssetIdParameter, allow_burn: bool }, + #[codec(index = 28)] + SetMinBalance { id: AssetIdParameter, min_balance: BalanceParameter }, + #[codec(index = 29)] + TouchOther { id: AssetIdParameter, who: MultiAddress }, + #[codec(index = 30)] + RefundOther { id: AssetIdParameter, who: MultiAddress }, + #[codec(index = 31)] + Block { id: AssetIdParameter, who: MultiAddress }, +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum AssetsError { + /// Account balance must be greater than or equal to the transfer amount. + BalanceLow, + /// The account to alter does not exist. + NoAccount, + /// The signing account has no permission to do the operation. + NoPermission, + /// The given asset ID is unknown. + Unknown, + /// The origin account is frozen. + Frozen, + /// The asset ID is already taken. + InUse, + /// Invalid witness data given. + BadWitness, + /// Minimum balance should be non-zero. + MinBalanceZero, + /// Unable to increment the consumer reference counters on the account. Either no provider + /// reference exists to allow a non-zero balance of a non-self-sufficient asset, or one + /// fewer then the maximum number of consumers has been reached. + UnavailableConsumer, + /// Invalid metadata given. + BadMetadata, + /// No approval exists that would allow the transfer. + Unapproved, + /// The source account would not survive the transfer and it needs to stay alive. + WouldDie, + /// The asset-account already exists. + AlreadyExists, + /// The asset-account doesn't have an associated deposit. + NoDeposit, + /// The operation would result in funds being burned. + WouldBurn, + /// The asset is a live asset and is actively being used. Usually emit for operations such + /// as `start_destroy` which require the asset to be in a destroying state. + LiveAsset, + /// The asset is not live, and likely being destroyed. + AssetNotLive, + /// The asset status is not the expected status. + IncorrectStatus, + /// The asset should be frozen before the given operation. + NotFrozen, + /// Callback action resulted in error. + CallbackFailed, +} + +impl TryFrom for AssetsError { + type Error = PopApiError; + + fn try_from(status_code: u32) -> core::result::Result { + use AssetsError::*; + match status_code { + 0 => Ok(BalanceLow), + 1 => Ok(NoAccount), + 2 => Ok(NoPermission), + 3 => Ok(Unknown), + 4 => Ok(Frozen), + 5 => Ok(InUse), + 6 => Ok(BadWitness), + 7 => Ok(MinBalanceZero), + 8 => Ok(UnavailableConsumer), + 9 => Ok(BadMetadata), + 10 => Ok(Unapproved), + 11 => Ok(WouldDie), + 12 => Ok(AlreadyExists), + 13 => Ok(NoDeposit), + 14 => Ok(WouldBurn), + 15 => Ok(LiveAsset), + 16 => Ok(AssetNotLive), + 17 => Ok(IncorrectStatus), + 18 => Ok(NotFrozen), + _ => todo!(), + } + } +} diff --git a/pop-api/src/v0/assets/pallets/mod.rs b/pop-api/src/v0/assets/pallets/mod.rs new file mode 100644 index 00000000..0b8a53a0 --- /dev/null +++ b/pop-api/src/v0/assets/pallets/mod.rs @@ -0,0 +1 @@ +pub(crate) mod assets; diff --git a/pop-api/src/v0/assets/use_cases/fungibles.rs b/pop-api/src/v0/assets/use_cases/fungibles.rs new file mode 100644 index 00000000..4af5d97e --- /dev/null +++ b/pop-api/src/v0/assets/use_cases/fungibles.rs @@ -0,0 +1,372 @@ +use crate::{ + assets::pallets, + error::PopApiError::{self, *}, + AccountId, Balance, *, +}; +use ink::prelude::vec::Vec; +use primitives::AssetId; + +type Result = core::result::Result; + +/// Local Fungibles: +/// 1. PSP-22 Interface +/// 2. PSP-22 Metadata Interface +/// 3. Asset Management + +/// 1. PSP-22 Interface: +/// - total_supply +/// - balance_of +/// - allowance +/// - transfer +/// - transfer_from +/// - approve +/// - increase_allowance +/// - decrease_allowance + +/// Returns the total token supply for a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// The total supply of the token, or an error if the operation fails. +pub fn total_supply(id: AssetId) -> Result { + pallets::assets::total_supply(id) +} + +/// Returns the account balance for the specified `owner` for a given asset ID. Returns `0` if +/// the account is non-existent. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `owner` - The account whose balance is being queried. +/// +/// # Returns +/// The balance of the specified account, or an error if the operation fails. +pub fn balance_of(id: AssetId, owner: AccountId) -> Result { + pallets::assets::balance_of(id, owner) +} + +/// Returns the amount which `spender` is still allowed to withdraw from `owner` for a given +/// asset ID. Returns `0` if no allowance has been set. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `owner` - The account that owns the tokens. +/// * `spender` - The account that is allowed to spend the tokens. +/// +/// # Returns +/// The remaining allowance, or an error if the operation fails. +pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { + pallets::assets::allowance(id, owner, spender) +} + +/// Transfers `value` amount of tokens from the caller's account to account `to`, with additional +/// `data` in unspecified format. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `to` - The recipient account. +/// * `value` - The number of tokens to transfer. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the transfer fails. +pub fn transfer( + id: AssetId, + to: impl Into>, + value: Balance, +) -> Result<()> { + pallets::assets::transfer(id, to, value) +} + +/// Transfers `value` tokens on behalf of `from` to account `to` with additional `data` +/// in unspecified format. If `from` is equal to `None`, tokens will be minted to account `to`. If +/// `to` is equal to `None`, tokens will be burned from account `from`. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `from` - The account from which the tokens are transferred. +/// * `to` - The recipient account. +/// * `value` - The number of tokens to transfer. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the transfer fails. +pub fn transfer_from( + id: AssetId, + from: Option>>, + to: Option>>, + value: Balance, + _data: &[u8], +) -> Result<()> { + match (from, to) { + (None, Some(to)) => pallets::assets::mint(id, to, value), + (Some(from), None) => pallets::assets::burn(id, from, value), + (Some(from), Some(to)) => pallets::assets::transfer_approved(id, from, to, value), + _ => Ok(()), + } +} + +/// Approves an account to spend a specified number of tokens on behalf of the caller. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `spender` - The account that is allowed to spend the tokens. +/// * `value` - The number of tokens to approve. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the approval fails. +// #[allow(unused_variables)] +// fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { +// todo!() +// // TODO: read allowance and increase or decrease. +// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { +// // id: id.into(), +// // delegate: spender.into(), +// // amount: Compact(value), +// // }))?) +// } + +/// Increases the allowance of a spender. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `spender` - The account that is allowed to spend the tokens. +/// * `value` - The number of tokens to increase the allowance by. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { +// id: id.into(), +// delegate: spender.into(), +// amount: Compact(value), +// }))?) +// } + +/// Decreases the allowance of a spender. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `spender` - The account that is allowed to spend the tokens. +/// * `value` - The number of tokens to decrease the allowance by. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// #[allow(unused_variables)] +// fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { +// todo!() +// // TODO: cancel_approval + approve_transfer +// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::CancelApproval { +// // id: id.into(), +// // delegate: delegate.into(), +// // }))?) +// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { +// // id: id.into(), +// // delegate: spender.into(), +// // amount: Compact(value), +// // }))?) +// } + +/// 2. PSP-22 Metadata Interface: +/// - token_name +/// - token_symbol +/// - token_decimals + +/// Returns the token name for a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// The name of the token as a byte vector, or an error if the operation fails. +// #[allow(unused_variables)] +// pub fn token_name(id: AssetId) -> Result>> { +// todo!() +// // Ok(state::read(RuntimeStateKeys::Assets(AssetsKeys::TokenName(id)))?) +// } + +/// Returns the token symbol for a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// The symbol of the token as a byte vector, or an error if the operation fails. +// #[allow(unused_variables)] +// fn token_symbol(id: AssetId) -> Result>> { +// todo!() +// } + +/// Returns the token decimals for a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// The number of decimals of the token as a byte vector, or an error if the operation fails. +// #[allow(unused_variables)] +// fn token_decimals(id: AssetId) -> Result>> { +// todo!() +// } + +/// 3. Asset Management: +/// - create +/// - start_destroy +/// - destroy_accounts +/// - destroy_approvals +/// - finish_destroy +/// - set_metadata +/// - clear_metadata + +/// Create a new token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// * `admin` - The account that will administer the asset. +/// * `min_balance` - The minimum balance required for accounts holding this asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the creation fails. +pub fn create( + id: AssetId, + admin: impl Into>, + min_balance: Balance, +) -> Result<()> { + pallets::assets::create(id, admin, min_balance) +} + +/// Start the process of destroying a token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// fn start_destroy(id: AssetId) -> Result<()> { +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::StartDestroy { +// id: id.into(), +// }))?) +// } + +/// Destroy all accounts associated with a token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// fn destroy_accounts(id: AssetId) -> Result<()> { +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::DestroyAccounts { +// id: id.into(), +// }))?) +// } + +/// Destroy all approvals associated with a token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// fn destroy_approvals(id: AssetId) -> Result<()> { +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::DestroyApprovals { +// id: id.into(), +// }))?) +// } + +/// Complete the process of destroying a token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// fn finish_destroy(id: AssetId) -> Result<()> { +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::FinishDestroy { +// id: id.into(), +// }))?) +// } + +/// Set the metadata for a token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) -> Result<()> { + pallets::assets::set_metadata(id, name, symbol, decimals) +} + +/// Clear the metadata for a token with a given asset ID. +/// +/// # Arguments +/// * `id` - The ID of the asset. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +// fn clear_metadata(id: AssetId) -> Result<()> { +// Ok(dispatch(RuntimeCall::Assets(AssetsCall::ClearMetadata { +// id: id.into(), +// }))?) +// } + +pub fn asset_exists(id: AssetId) -> Result { + pallets::assets::asset_exists(id) +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum FungiblesError { + /// The asset is not live; either frozen or being destroyed. + AssetNotLive, + /// Not enough allowance to fulfill a request is available. + InsufficientAllowance, + /// Not enough balance to fulfill a request is available. + InsufficientBalance, + /// The asset ID is already taken. + InUse, + /// Minimum balance should be non-zero. + MinBalanceZero, + /// The account to alter does not exist. + NoAccount, + /// The signing account has no permission to do the operation. + NoPermission, + /// The given asset ID is unknown. + Unknown, + // // TODO: + // // - Originally `InsufficientBalance` for the deposit but this would result in the same error + // // as the error when there is insufficient balance for transferring an asset. + /// No balance for creation of assets or fees. + NoBalance, +} + +// TODO: make generic. +pub(crate) fn convert_to_fungibles_error(index: u8, error: u8) -> PopApiError { + match index { + 10 => balance_into(error), + 52 => assets_into(error), + _ => Module { index, error }, + } +} + +fn balance_into(error: u8) -> PopApiError { + match error { + 2 => UseCaseError(FungiblesError::NoBalance), + _ => Module { index: 10, error }, + } +} + +fn assets_into(error: u8) -> PopApiError { + match error { + 0 => UseCaseError(FungiblesError::InsufficientBalance), + 1 => UseCaseError(FungiblesError::NoAccount), + 2 => UseCaseError(FungiblesError::NoPermission), + 3 => UseCaseError(FungiblesError::Unknown), + 5 => UseCaseError(FungiblesError::InUse), + 7 => UseCaseError(FungiblesError::MinBalanceZero), + 10 => UseCaseError(FungiblesError::InsufficientAllowance), + 16 => UseCaseError(FungiblesError::AssetNotLive), + _ => Module { index: 52, error }, + } +} diff --git a/pop-api/src/v0/assets/use_cases/mod.rs b/pop-api/src/v0/assets/use_cases/mod.rs new file mode 100644 index 00000000..182590df --- /dev/null +++ b/pop-api/src/v0/assets/use_cases/mod.rs @@ -0,0 +1 @@ +pub mod fungibles; diff --git a/pop-api/src/v0/balances.rs b/pop-api/src/v0/balances.rs index 08db0a75..5d38d851 100644 --- a/pop-api/src/v0/balances.rs +++ b/pop-api/src/v0/balances.rs @@ -1,9 +1,7 @@ -use crate::{ - dispatch, primitives::MultiAddress, v0::RuntimeCall, AccountId, PopApiError, - PopApiError::UnknownModuleStatusCode, -}; +// TODO: what to put in this file? +use crate::{dispatch, error::PopApiError, primitives::MultiAddress, v0::RuntimeCall, AccountId}; -type Result = core::result::Result; +type Result = core::result::Result; pub fn transfer_keep_alive( dest: impl Into>, @@ -17,7 +15,7 @@ pub fn transfer_keep_alive( #[derive(scale::Encode)] #[allow(dead_code)] -pub(crate) enum BalancesCall { +pub enum BalancesCall { #[codec(index = 3)] TransferKeepAlive { dest: MultiAddress, @@ -28,7 +26,7 @@ pub(crate) enum BalancesCall { #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub(crate) enum BalancesError { +pub enum BalancesError { /// Vesting balance too high to send value. VestingBalance, /// Account liquidity restrictions prevent withdrawal. @@ -73,15 +71,6 @@ impl TryFrom for BalancesError { 9 => Ok(TooManyFreezes), 10 => Ok(IssuanceDeactivated), 11 => Ok(DeltaZero), - _ => Err(UnknownModuleStatusCode(status_code)), - } - } -} - -impl From for BalancesError { - fn from(error: PopApiError) -> Self { - match error { - PopApiError::Balances(e) => e, _ => todo!(), } } diff --git a/pop-api/src/v0/contracts.rs b/pop-api/src/v0/contracts.rs deleted file mode 100644 index 5d4a3692..00000000 --- a/pop-api/src/v0/contracts.rs +++ /dev/null @@ -1,152 +0,0 @@ -use crate::{PopApiError, PopApiError::UnknownModuleStatusCode}; - -#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum Error { - /// Invalid schedule supplied, e.g. with zero weight of a basic operation. - InvalidSchedule, - /// Invalid combination of flags supplied to `seal_call` or `seal_delegate_call`. - InvalidCallFlags, - /// The executed contract exhausted its gas limit. - OutOfGas, - /// The output buffer supplied to a contract API call was too small. - OutputBufferTooSmall, - /// Performing the requested transfer failed. Probably because there isn't enough - /// free balance in the sender's account. - TransferFailed, - /// Performing a call was denied because the calling depth reached the limit - /// of what is specified in the schedule. - MaxCallDepthReached, - /// No contract was found at the specified address. - ContractNotFound, - /// The code supplied to `instantiate_with_code` exceeds the limit specified in the - /// current schedule. - CodeTooLarge, - /// No code could be found at the supplied code hash. - CodeNotFound, - /// No code info could be found at the supplied code hash. - CodeInfoNotFound, - /// A buffer outside of sandbox memory was passed to a contract API function. - OutOfBounds, - /// Input passed to a contract API function failed to decode as expected type. - DecodingFailed, - /// Contract trapped during execution. - ContractTrapped, - /// The size defined in `T::MaxValueSize` was exceeded. - ValueTooLarge, - /// Termination of a contract is not allowed while the contract is already - /// on the call stack. Can be triggered by `seal_terminate`. - TerminatedWhileReentrant, - /// `seal_call` forwarded this contracts input. It therefore is no longer available. - InputForwarded, - /// The subject passed to `seal_random` exceeds the limit. - RandomSubjectTooLong, - /// The amount of topics passed to `seal_deposit_events` exceeds the limit. - TooManyTopics, - /// The chain does not provide a chain extension. Calling the chain extension results - /// in this error. Note that this usually shouldn't happen as deploying such contracts - /// is rejected. - NoChainExtension, - /// Failed to decode the XCM program. - XCMDecodeFailed, - /// A contract with the same AccountId already exists. - DuplicateContract, - /// A contract self destructed in its constructor. - /// - /// This can be triggered by a call to `seal_terminate`. - TerminatedInConstructor, - /// A call tried to invoke a contract that is flagged as non-reentrant. - /// The only other cause is that a call from a contract into the runtime tried to call back - /// into `pallet-contracts`. This would make the whole pallet reentrant with regard to - /// contract code execution which is not supported. - ReentranceDenied, - /// Origin doesn't have enough balance to pay the required storage deposits. - StorageDepositNotEnoughFunds, - /// More storage was created than allowed by the storage deposit limit. - StorageDepositLimitExhausted, - /// Code removal was denied because the code is still in use by at least one contract. - CodeInUse, - /// The contract ran to completion but decided to revert its storage changes. - /// Please note that this error is only returned from extrinsics. When called directly - /// or via RPC an `Ok` will be returned. In this case the caller needs to inspect the flags - /// to determine whether a reversion has taken place. - ContractReverted, - /// The contract's code was found to be invalid during validation. - /// - /// The most likely cause of this is that an API was used which is not supported by the - /// node. This happens if an older node is used with a new version of ink!. Try updating - /// your node to the newest available version. - /// - /// A more detailed error can be found on the node console if debug messages are enabled - /// by supplying `-lruntime::contracts=debug`. - CodeRejected, - /// An indeterministic code was used in a context where this is not permitted. - Indeterministic, - /// A pending migration needs to complete before the extrinsic can be called. - MigrationInProgress, - /// Migrate dispatch call was attempted but no migration was performed. - NoMigrationPerformed, - /// The contract has reached its maximum number of delegate dependencies. - MaxDelegateDependenciesReached, - /// The dependency was not found in the contract's delegate dependencies. - DelegateDependencyNotFound, - /// The contract already depends on the given delegate dependency. - DelegateDependencyAlreadyExists, - /// Can not add a delegate dependency to the code hash of the contract itself. - CannotAddSelfAsDelegateDependency, -} - -impl TryFrom for Error { - type Error = PopApiError; - - fn try_from(status_code: u32) -> core::result::Result { - use Error::*; - match status_code { - 0 => Ok(InvalidSchedule), - 1 => Ok(InvalidCallFlags), - 2 => Ok(OutOfGas), - 3 => Ok(OutputBufferTooSmall), - 4 => Ok(TransferFailed), - 5 => Ok(MaxCallDepthReached), - 6 => Ok(ContractNotFound), - 7 => Ok(CodeTooLarge), - 8 => Ok(CodeNotFound), - 9 => Ok(CodeInfoNotFound), - 10 => Ok(OutOfBounds), - 11 => Ok(DecodingFailed), - 12 => Ok(ContractTrapped), - 13 => Ok(ValueTooLarge), - 14 => Ok(TerminatedWhileReentrant), - 15 => Ok(InputForwarded), - 16 => Ok(RandomSubjectTooLong), - 17 => Ok(TooManyTopics), - 18 => Ok(NoChainExtension), - 19 => Ok(XCMDecodeFailed), - 20 => Ok(DuplicateContract), - 21 => Ok(TerminatedInConstructor), - 22 => Ok(ReentranceDenied), - 23 => Ok(StorageDepositNotEnoughFunds), - 24 => Ok(StorageDepositLimitExhausted), - 25 => Ok(CodeInUse), - 26 => Ok(ContractReverted), - 27 => Ok(CodeRejected), - 28 => Ok(Indeterministic), - 29 => Ok(MigrationInProgress), - 30 => Ok(NoMigrationPerformed), - 31 => Ok(MaxDelegateDependenciesReached), - 32 => Ok(DelegateDependencyNotFound), - 33 => Ok(DelegateDependencyAlreadyExists), - 34 => Ok(CannotAddSelfAsDelegateDependency), - _ => Err(UnknownModuleStatusCode(status_code)), - } - } -} - -impl From for Error { - fn from(error: PopApiError) -> Self { - match error { - PopApiError::Contracts(e) => e, - _ => panic!("expected balances error"), - } - } -} diff --git a/pop-api/src/v0/cross_chain/mod.rs b/pop-api/src/v0/cross_chain/mod.rs index 1e5afbf5..1d3c5b7d 100644 --- a/pop-api/src/v0/cross_chain/mod.rs +++ b/pop-api/src/v0/cross_chain/mod.rs @@ -1,8 +1,8 @@ pub mod coretime; -use crate::{PopApiError::UnknownModuleStatusCode, *}; +use crate::error::PopApiError; -type Result = core::result::Result; +type Result = core::result::Result; #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] @@ -92,16 +92,7 @@ impl TryFrom for Error { 21 => Ok(InvalidAssetUnknownReserve), 22 => Ok(InvalidAssetUnsupportedReserve), 23 => Ok(TooManyReserves), - _ => Err(UnknownModuleStatusCode(status_code)), - } - } -} - -impl From for Error { - fn from(error: PopApiError) -> Self { - match error { - PopApiError::Xcm(e) => e, - _ => panic!("expected xcm error"), + _ => todo!(), } } } diff --git a/pop-api/src/v0/dispatch_error.rs b/pop-api/src/v0/dispatch_error.rs deleted file mode 100644 index 6ed40ce5..00000000 --- a/pop-api/src/v0/dispatch_error.rs +++ /dev/null @@ -1,57 +0,0 @@ -use super::*; - -#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub(crate) enum TokenError { - /// Funds are unavailable. - FundsUnavailable, - /// Some part of the balance gives the only provider reference to the account and thus cannot - /// be (re)moved. - OnlyProvider, - /// Account cannot exist with the funds that would be given. - BelowMinimum, - /// Account cannot be created. - CannotCreate, - /// The asset in question is unknown. - UnknownAsset, - /// Funds exist but are frozen. - Frozen, - /// Operation is not supported by the asset. - Unsupported, - /// Account cannot be created for a held balance. - CannotCreateHold, - /// Withdrawal would cause unwanted loss of account. - NotExpendable, - /// Account cannot receive the assets. - Blocked, -} - -impl TryFrom for TokenError { - type Error = crate::PopApiError; - - fn try_from(status_code: u32) -> core::result::Result { - use TokenError::*; - match status_code { - 0 => Ok(FundsUnavailable), - 1 => Ok(OnlyProvider), - 2 => Ok(BelowMinimum), - 3 => Ok(CannotCreate), - 4 => Ok(UnknownAsset), - 5 => Ok(Frozen), - 6 => Ok(Unsupported), - 7 => Ok(CannotCreateHold), - 8 => Ok(NotExpendable), - 9 => Ok(Blocked), - _ => todo!(), - } - } -} - -impl From for TokenError { - fn from(error: PopApiError) -> Self { - match error { - PopApiError::TokenError(e) => e, - _ => todo!(), - } - } -} diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index 02169c22..75bcb878 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -5,9 +5,7 @@ use crate::{ pub mod assets; pub mod balances; -pub mod contracts; pub mod cross_chain; -pub mod dispatch_error; pub mod nfts; pub mod state; @@ -22,5 +20,5 @@ pub(crate) enum RuntimeCall { #[codec(index = 50)] Nfts(nfts::NftCalls), #[codec(index = 52)] - Assets(assets::fungibles::AssetsCall), + Assets(assets::pallets::assets::AssetsCall), } diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index 946a3eca..2de306a9 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -1,12 +1,12 @@ use super::RuntimeCall; -use crate::{PopApiError::UnknownModuleStatusCode, *}; +use crate::{PopApiError, *}; use ink::prelude::vec::Vec; use primitives::{ApprovalsLimit, BoundedBTreeMap, KeyLimit, MultiAddress}; pub use primitives::{CollectionId, ItemId}; use scale::Encode; pub use types::*; -type Result = core::result::Result; +type Result = core::result::Result; /// Issue a new collection of non-fungible items pub fn create( @@ -660,16 +660,7 @@ impl TryFrom for Error { 42 => Ok(WrongNamespace), 43 => Ok(CollectionNotEmpty), 44 => Ok(WitnessRequired), - _ => Err(UnknownModuleStatusCode(status_code)), - } - } -} - -impl From for Error { - fn from(error: PopApiError) -> Self { - match error { - PopApiError::Nfts(e) => e, - _ => panic!("unexpected pallet nfts error. This error is unknown to pallet nfts"), + _ => todo!(), } } } diff --git a/primitives/src/storage_keys.rs b/primitives/src/storage_keys.rs index 67448842..fc7234aa 100644 --- a/primitives/src/storage_keys.rs +++ b/primitives/src/storage_keys.rs @@ -1,5 +1,4 @@ use super::*; -// use scale::{Decode, Encode, MaxEncodedLen}; #[derive(Encode, Decode, Debug, MaxEncodedLen)] pub enum RuntimeStateKeys { @@ -28,8 +27,6 @@ pub enum NftsKeys { Owner(CollectionId, ItemId), /// Get the attribute value of `item` of `collection` corresponding to `key`. Attribute(CollectionId, ItemId, BoundedVec), - // /// Get the custom attribute value of `item` of `collection` corresponding to `key`. - // CustomAttribute(AccountId, CollectionId, ItemId, BoundedVec), /// Get the system attribute value of `item` of `collection` corresponding to `key` SystemAttribute(CollectionId, Option, BoundedVec), /// Get the attribute value of `item` of `collection` corresponding to `key`. diff --git a/runtime/devnet/Cargo.toml b/runtime/devnet/Cargo.toml index d4fe2923..46208d8f 100644 --- a/runtime/devnet/Cargo.toml +++ b/runtime/devnet/Cargo.toml @@ -91,6 +91,7 @@ parachain-info.workspace = true env_logger = "0.11.2" hex = "0.4.3" enumflags2 = "0.7.9" +pop-api = { path = "../../pop-api", default-features = false } [features] default = ["std"] @@ -138,6 +139,7 @@ std = [ "polkadot-parachain-primitives/std", "polkadot-runtime-common/std", "pop-primitives/std", + "pop-api/std", "scale-info/std", "sp-api/std", "sp-io/std", diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions.rs similarity index 97% rename from runtime/devnet/src/extensions/mod.rs rename to runtime/devnet/src/extensions.rs index d2129c4f..014893f9 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions.rs @@ -17,10 +17,7 @@ use pop_primitives::{ AssetId, CollectionId, ItemId, }; use sp_core::crypto::UncheckedFrom; -use sp_runtime::{ - traits::{BlockNumberProvider, Dispatchable}, - DispatchError, -}; +use sp_runtime::traits::{BlockNumberProvider, Dispatchable}; use sp_std::{boxed::Box, vec::Vec}; use xcm::{ latest::{prelude::*, OriginKind::SovereignAccount}, @@ -32,9 +29,6 @@ use crate::{ RuntimeOrigin, UNIT, }; -#[cfg(test)] -mod tests; - const LOG_TARGET: &str = "pop-api::extension"; type ContractSchedule = ::Schedule; @@ -42,6 +36,7 @@ type ContractSchedule = ::Schedule; #[derive(Default)] pub struct PopApiExtension; +// TODO: check removal or simplification of trait bounds. impl ChainExtension for PopApiExtension where T: pallet_contracts::Config @@ -59,31 +54,12 @@ where fn call(&mut self, env: Environment) -> Result where E: Ext, - // T::AccountId: UncheckedFrom + AsRef<[u8]>, { log::debug!(target:LOG_TARGET, " extension called "); match v0::FuncId::try_from(env.func_id())? { - v0::FuncId::Dispatch => { - match dispatch::(env) { - Ok(()) => Ok(RetVal::Converging(0)), - Err(DispatchError::Module(error)) => { - // encode status code = pallet index in runtime + error index, allowing for - // 999 errors - let first = (3u32 * 1_000_000u32) - + (error.index as u32 * 1_000u32) - + u32::from_le_bytes(error.error); - Ok(RetVal::Converging( - // (3u32 * 1_000_000u32) - // + (error.index as u32 * 1_000u32) - // + u32::from_le_bytes(error.error), - first, - )) - }, - Err(DispatchError::Token(error)) => { - Ok(RetVal::Converging((7u32 * 1_000_000u32) + error as u32)) - }, - Err(e) => Err(e), - } + v0::FuncId::Dispatch => match dispatch::(env) { + Ok(()) => Ok(RetVal::Converging(0)), + Err(e) => Ok(RetVal::Converging(convert_to_status_code(e))), }, v0::FuncId::ReadState => { read_state::(env)?; @@ -97,6 +73,17 @@ where } } +pub(crate) fn convert_to_status_code(error: DispatchError) -> u32 { + match error { + _ => { + let mut encoded_error = error.encode(); + // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). + encoded_error.resize(4, 0); + u32::decode(&mut &encoded_error[..]).unwrap() + }, + } +} + pub mod v0 { #[derive(Debug)] pub enum FuncId { diff --git a/runtime/devnet/src/extensions/tests/mod.rs b/runtime/devnet/src/extensions/tests/mod.rs deleted file mode 100644 index cbd13d62..00000000 --- a/runtime/devnet/src/extensions/tests/mod.rs +++ /dev/null @@ -1,89 +0,0 @@ -#![cfg(test)] -use super::*; -use crate::{Assets, Balances, Contracts, Runtime, System}; -use pallet_contracts::{Code, CollectEvents, Determinism, ExecReturnValue}; -use sp_runtime::{traits::Hash, AccountId32, BuildStorage}; - -mod local_fungibles; - -type Balance = u128; -type AssetId = u32; -const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug; - -const ALICE: AccountId32 = AccountId32::new([1_u8; 32]); -const BOB: AccountId32 = AccountId32::new([2_u8; 32]); -const INIT_AMOUNT: Balance = 100_000_000 * UNIT; -const INIT_VALUE: Balance = 100 * UNIT; -const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024); - -fn new_test_ext() -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::::default() - .build_storage() - .expect("Frame system builds valid default genesis config"); - - pallet_balances::GenesisConfig:: { - balances: vec![(ALICE, INIT_AMOUNT), (BOB, INIT_AMOUNT)], - } - .assimilate_storage(&mut t) - .expect("Pallet balances storage can be assimilated"); - - let mut ext = sp_io::TestExternalities::new(t); - ext.execute_with(|| System::set_block_number(1)); - ext -} - -fn load_wasm_module(path: &str) -> std::io::Result<(Vec, ::Output)> -where - T: frame_system::Config, -{ - let wasm_binary = std::fs::read(path)?; - let code_hash = T::Hashing::hash(&wasm_binary); - Ok((wasm_binary, code_hash)) -} - -fn function_selector(name: &str) -> Vec { - let hash = sp_io::hashing::blake2_256(name.as_bytes()); - [hash[0..4].to_vec()].concat() -} - -fn do_bare_call( - addr: AccountId32, - input: Vec, - value: u128, -) -> Result { - let result = Contracts::bare_call( - ALICE, - addr.into(), - value.into(), - GAS_LIMIT, - None, - input, - DEBUG_OUTPUT, - CollectEvents::Skip, - Determinism::Enforced, - ); - log::debug!("Contract debug buffer - {:?}", String::from_utf8(result.debug_message.clone())); - log::debug!("result: {:?}", result); - result.result -} - -// Deploy, instantiate and return contract address. -fn instantiate(contract: &str, init_value: u128, salt: Vec) -> AccountId32 { - let (wasm_binary, _) = - load_wasm_module::(contract).expect("could not read .wasm file"); - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - salt, - DEBUG_OUTPUT, - CollectEvents::Skip, - ) - .result - .unwrap(); - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - result.account_id -} diff --git a/runtime/devnet/src/lib.rs b/runtime/devnet/src/lib.rs index a82be804..416a3298 100644 --- a/runtime/devnet/src/lib.rs +++ b/runtime/devnet/src/lib.rs @@ -10,6 +10,8 @@ mod extensions; mod weights; // Public due to integration tests crate. pub mod config; +#[cfg(test)] +mod tests; use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases; use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; diff --git a/runtime/devnet/src/extensions/tests/local_fungibles.rs b/runtime/devnet/src/tests/local_fungibles.rs similarity index 70% rename from runtime/devnet/src/extensions/tests/local_fungibles.rs rename to runtime/devnet/src/tests/local_fungibles.rs index 5de162c9..bc13fbde 100644 --- a/runtime/devnet/src/extensions/tests/local_fungibles.rs +++ b/runtime/devnet/src/tests/local_fungibles.rs @@ -2,31 +2,10 @@ use super::*; -#[derive(Decode, Encode, Debug, Eq, PartialEq)] -enum FungiblesError { - /// The asset is not live; either frozen or being destroyed. - AssetNotLive, - /// The amount to mint is less than the existential deposit. - BelowMinimum, - /// Unspecified dispatch error, providing the index and optionally its error index. - DispatchError { index: u8, error: Option }, - /// Not enough allowance to fulfill a request is available. - InsufficientAllowance, - /// Not enough balance to fulfill a request is available. - InsufficientBalance, - /// The asset ID is already taken. - InUse, - /// Minimum balance should be non-zero. - MinBalanceZero, - /// Unspecified pallet error, providing pallet index and error index. - ModuleError { pallet: u8, error: u16 }, - /// The account to alter does not exist. - NoAccount, - /// The signing account has no permission to do the operation. - NoPermission, - /// The given asset ID is unknown. - Unknown, -} +use pop_api::{ + error::{ArithmeticError::*, PopApiError::*, TokenError::*}, + v0::assets::use_cases::fungibles::FungiblesError::*, +}; const ASSET_ID: AssetId = 1; @@ -119,8 +98,7 @@ fn transfer_from( do_bare_call(addr, params, 0).expect("should work") } -// Create an asset and mint to owner. -fn create_asset(asset_id: AssetId, owner: AccountId32, min_balance: Balance) -> AssetId { +fn create_asset(owner: AccountId32, asset_id: AssetId, min_balance: Balance) -> AssetId { assert_eq!( Assets::create( RuntimeOrigin::signed(owner.clone()), @@ -133,31 +111,34 @@ fn create_asset(asset_id: AssetId, owner: AccountId32, min_balance: Balance) -> asset_id } -// Create an asset and mint to owner. +fn mint_asset(owner: AccountId32, asset_id: AssetId, to: AccountId32, value: Balance) -> AssetId { + assert_eq!( + Assets::mint(RuntimeOrigin::signed(owner.clone()), asset_id.into(), to.into(), value), + Ok(()) + ); + asset_id +} + fn create_asset_and_mint_to( - asset_id: AssetId, owner: AccountId32, + asset_id: AssetId, to: AccountId32, value: Balance, ) -> AssetId { - create_asset(asset_id, owner.clone(), 1); - assert_eq!( - Assets::mint(RuntimeOrigin::signed(owner.into()), asset_id.into(), to.into(), value,), - Ok(()) - ); - asset_id + create_asset(owner.clone(), asset_id, 1); + mint_asset(owner, asset_id, to, value) } // Create an asset, mints to, and approves spender. fn create_asset_mint_and_approve( - asset_id: AssetId, owner: AccountId32, + asset_id: AssetId, to: AccountId32, mint: Balance, spender: AccountId32, approve: Balance, ) { - create_asset_and_mint_to(asset_id, owner.clone(), to.clone(), mint); + create_asset_and_mint_to(owner.clone(), asset_id, to.clone(), mint); assert_eq!( Assets::approve_transfer( RuntimeOrigin::signed(to.into()), @@ -199,7 +180,7 @@ fn total_supply_works() { assert_eq!(Assets::total_supply(ASSET_ID), total_supply(addr.clone(), ASSET_ID)); // Tokens in circulation. - create_asset_and_mint_to(ASSET_ID, addr.clone(), BOB, 100); + create_asset_and_mint_to(addr.clone(), ASSET_ID, BOB, 100); assert_eq!(Assets::total_supply(ASSET_ID), total_supply(addr, ASSET_ID)); }); } @@ -219,7 +200,7 @@ fn balance_of_works() { assert_eq!(Assets::balance(ASSET_ID, BOB), balance_of(addr.clone(), ASSET_ID, BOB)); // Tokens in circulation. - create_asset_and_mint_to(ASSET_ID, addr.clone(), BOB, 100); + create_asset_and_mint_to(addr.clone(), ASSET_ID, BOB, 100); assert_eq!(Assets::balance(ASSET_ID, BOB), balance_of(addr, ASSET_ID, BOB)); }); } @@ -242,7 +223,7 @@ fn allowance_works() { ); // Tokens in circulation. - create_asset_mint_and_approve(ASSET_ID, addr.clone(), BOB, 100, ALICE, 50); + create_asset_mint_and_approve(addr.clone(), ASSET_ID, BOB, 100, ALICE, 50); assert_eq!( Assets::allowance(ASSET_ID, &BOB, &ALICE), allowance(addr, ASSET_ID, BOB, ALICE) @@ -265,7 +246,7 @@ fn asset_exists_works() { assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr.clone(), ASSET_ID)); // Tokens in circulation. - create_asset(ASSET_ID, addr.clone(), 1); + create_asset(addr.clone(), ASSET_ID, 1); assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr, ASSET_ID)); }); } @@ -276,34 +257,52 @@ fn asset_exists_works() { // - reserve(): Overflow, LiquidityRestrictions; frozen // - Callback // - StorageDepositLimitExhausted +// todo: errors: +// - TokenErrors +// - Arithmetic +// - https://github.com/paritytech/polkadot-sdk/blob/3977f389cce4a00fd7100f95262e0563622b9aa4/substrate/frame/assets/src/functions.rs#L125 #[test] #[ignore] fn create_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); let new_asset = 2; + // Instantiate a contract without balance (relay token). let addr = instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", 0, vec![0]); - + // No balance to pay for fees. assert_eq!( - decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), - FungiblesError::InsufficientBalance + decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), + UseCaseError(NoBalance) ); + // Instantiate a contract without balance (relay token). + let addr = + instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", 100, vec![2]); + // TODO: make sure it has enough for the fees but not for the deposit. + // No balance to pay fe deposit. + assert_eq!( + decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), + UseCaseError(NoBalance) + ); + // Instantiate a contract with balance. let addr = instantiate( "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![1], ); - create_asset(ASSET_ID, ALICE, 1); + create_asset(ALICE, ASSET_ID, 1); + // Asset ID is already taken. assert_eq!( - decoded::(create(addr.clone(), ASSET_ID, BOB, 1)), - FungiblesError::InUse + decoded::(create(addr.clone(), ASSET_ID, BOB, 1)), + UseCaseError(InUse) ); + // The minimal balance for an asset must be non zero. assert_eq!( - decoded::(create(addr.clone(), new_asset, BOB, 0)), - FungiblesError::MinBalanceZero + decoded::(create(addr.clone(), new_asset, BOB, 0)), + UseCaseError(MinBalanceZero) ); - assert!(!create(addr.clone(), new_asset, BOB, 1).did_revert(), "Contract reverted!"); + let result = create(addr.clone(), new_asset, BOB, 1); + assert!(!result.did_revert(), "Contract reverted!"); }); } @@ -318,17 +317,12 @@ fn set_metadata_works() { vec![], ); - create_asset(ASSET_ID, addr.clone(), 1); - + create_asset(addr.clone(), ASSET_ID, 1); let result = set_metadata(addr.clone(), ASSET_ID, vec![12], vec![12], 12); assert!(!result.did_revert(), "Contract reverted!"); }); } -// todo: errors: -// - TokenErrors -// - Arithmetic -// - https://github.com/paritytech/polkadot-sdk/blob/3977f389cce4a00fd7100f95262e0563622b9aa4/substrate/frame/assets/src/functions.rs#L125 #[test] #[ignore] fn transfer_from_mint_works() { @@ -341,20 +335,15 @@ fn transfer_from_mint_works() { ); let amount: Balance = 100 * UNIT; + // Asset does not exist. assert_eq!( - decoded::(transfer_from( - addr.clone(), - 1, - None, - Some(BOB), - amount, - &[0u8] - )), - FungiblesError::Unknown + decoded::(transfer_from(addr.clone(), 1, None, Some(BOB), amount, &[0u8])), + Token(UnknownAsset) ); - let asset = create_asset(1, ALICE, 2); + let asset = create_asset(ALICE, 1, 2); + // Minting can only be done by the owner. assert_eq!( - decoded::(transfer_from( + decoded::(transfer_from( addr.clone(), asset, None, @@ -362,23 +351,18 @@ fn transfer_from_mint_works() { amount, &[0u8] )), - FungiblesError::NoPermission + UseCaseError(NoPermission) ); + // Minimum balance of an asset can not be zero. assert_eq!( - decoded::(transfer_from( - addr.clone(), - asset, - None, - Some(BOB), - 1, - &[0u8] - )), - FungiblesError::BelowMinimum + decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), 1, &[0u8])), + Token(BelowMinimum) ); - let asset = create_asset(2, addr.clone(), 2); + let asset = create_asset(addr.clone(), 2, 2); + // Asset is not live, i.e. frozen or being destroyed. freeze_asset(asset, addr.clone()); assert_eq!( - decoded::(transfer_from( + decoded::(transfer_from( addr.clone(), asset, None, @@ -386,17 +370,31 @@ fn transfer_from_mint_works() { amount, &[0u8] )), - FungiblesError::AssetNotLive + UseCaseError(AssetNotLive) ); thaw_asset(asset, addr.clone()); + // Successful mint. let bob_balance_before_mint = Assets::balance(asset, &BOB); let result = transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8]); assert!(!result.did_revert(), "Contract reverted!"); let bob_balance_after_mint = Assets::balance(asset, &BOB); assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount); + // Can not mint more tokens than Balance::MAX. + assert_eq!( + decoded::(transfer_from( + addr.clone(), + asset, + None, + Some(BOB), + Balance::MAX, + &[0u8] + )), + Arithmetic(Overflow) + ); + // Asset is not live, i.e. frozen or being destroyed. start_destroy_asset(asset, addr.clone()); assert_eq!( - decoded::(transfer_from( + decoded::(transfer_from( addr.clone(), asset, None, @@ -404,17 +402,15 @@ fn transfer_from_mint_works() { amount, &[0u8] )), - FungiblesError::AssetNotLive + UseCaseError(AssetNotLive) ); }); } // Todo: error: -// - Frozen: account is frozen, who do you freeze an account? // - https://github.com/paritytech/polkadot-sdk/blob/2460cddf57660a88844d201f769eb17a7accce5a/substrate/frame/assets/src/functions.rs#L161 // - ArithmeticError: Underflow, Overflow // - https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/assets/src/functions.rs#L125 -// - #[test] #[ignore] fn transfer_works() { @@ -427,35 +423,46 @@ fn transfer_works() { ); let amount: Balance = 100 * UNIT; + // Asset does not exist. assert_eq!( - decoded::(transfer(addr.clone(), 1, BOB, amount,)), - FungiblesError::Unknown + decoded::(transfer(addr.clone(), 1, BOB, amount,)), + UseCaseError(Unknown) ); - let asset = create_asset_and_mint_to(1, ALICE, addr.clone(), amount); + // Create asset with Alice as owner and mint `amount` to contract address. + let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); + // Asset is not live, i.e. frozen or being destroyed. freeze_asset(asset, ALICE); assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount,)), - FungiblesError::AssetNotLive + decoded::(transfer(addr.clone(), asset, BOB, amount,)), + UseCaseError(AssetNotLive) ); thaw_asset(asset, ALICE); + // Not enough balance. assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), - FungiblesError::InsufficientBalance + decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), + UseCaseError(InsufficientBalance) ); - // Errors due to ED. Could be Belowminimum + // Not enough balance due to ED. assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount)), - FungiblesError::InsufficientBalance + decoded::(transfer(addr.clone(), asset, BOB, amount)), + UseCaseError(InsufficientBalance) ); + // Successful transfer. let bob_balance_before_mint = Assets::balance(asset, &BOB); let result = transfer(addr.clone(), asset, BOB, amount / 2); assert!(!result.did_revert(), "Contract reverted!"); let bob_balance_after_mint = Assets::balance(asset, &BOB); assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount / 2); + // Transfer asset to account that does not exist. + assert_eq!( + decoded::(transfer(addr.clone(), asset, FERDIE, amount / 4)), + Token(CannotCreate) + ); + // Asset is not live, i.e. frozen or being destroyed. start_destroy_asset(asset, ALICE); assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), - FungiblesError::AssetNotLive + decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), + UseCaseError(AssetNotLive) ); }); } diff --git a/runtime/devnet/src/tests/mod.rs b/runtime/devnet/src/tests/mod.rs new file mode 100644 index 00000000..c13030a3 --- /dev/null +++ b/runtime/devnet/src/tests/mod.rs @@ -0,0 +1,262 @@ +#![cfg(test)] + +use crate::{ + config::assets::TrustBackedAssetsInstance, Assets, Contracts, Runtime, RuntimeOrigin, System, + Weight, UNIT, +}; +use codec::{Decode, Encode}; +use frame_support::traits::fungibles::{approvals::Inspect as ApprovalInspect, Inspect}; +use frame_system::Config; +use pallet_contracts::{Code, CollectEvents, Determinism, ExecReturnValue}; +use pop_api::error::{ArithmeticError, PopApiError, TokenError, TransactionalError}; +use sp_runtime::{traits::Hash, AccountId32, BuildStorage, DispatchError}; + +mod local_fungibles; + +type Balance = u128; +type AssetId = u32; +const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug; + +const ALICE: AccountId32 = AccountId32::new([1_u8; 32]); +const BOB: AccountId32 = AccountId32::new([2_u8; 32]); +// FERDIE has no initial balance. +const FERDIE: AccountId32 = AccountId32::new([3_u8; 32]); +const INIT_AMOUNT: Balance = 100_000_000 * UNIT; +const INIT_VALUE: Balance = 100 * UNIT; +const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024); + +fn new_test_ext() -> sp_io::TestExternalities { + let mut t = frame_system::GenesisConfig::::default() + .build_storage() + .expect("Frame system builds valid default genesis config"); + + pallet_balances::GenesisConfig:: { + balances: vec![(ALICE, INIT_AMOUNT), (BOB, INIT_AMOUNT)], + } + .assimilate_storage(&mut t) + .expect("Pallet balances storage can be assimilated"); + + let mut ext = sp_io::TestExternalities::new(t); + ext.execute_with(|| System::set_block_number(1)); + ext +} + +fn load_wasm_module(path: &str) -> std::io::Result<(Vec, ::Output)> +where + T: frame_system::Config, +{ + let wasm_binary = std::fs::read(path)?; + let code_hash = T::Hashing::hash(&wasm_binary); + Ok((wasm_binary, code_hash)) +} + +fn function_selector(name: &str) -> Vec { + let hash = sp_io::hashing::blake2_256(name.as_bytes()); + [hash[0..4].to_vec()].concat() +} + +fn do_bare_call( + addr: AccountId32, + input: Vec, + value: u128, +) -> Result { + let result = Contracts::bare_call( + ALICE, + addr.into(), + value.into(), + GAS_LIMIT, + None, + input, + DEBUG_OUTPUT, + CollectEvents::Skip, + Determinism::Enforced, + ); + log::debug!("Contract debug buffer - {:?}", String::from_utf8(result.debug_message.clone())); + log::debug!("result: {:?}", result); + result.result +} + +// Deploy, instantiate and return contract address. +fn instantiate(contract: &str, init_value: u128, salt: Vec) -> AccountId32 { + let (wasm_binary, _) = + load_wasm_module::(contract).expect("could not read .wasm file"); + let result = Contracts::bare_instantiate( + ALICE, + init_value, + GAS_LIMIT, + None, + Code::Upload(wasm_binary), + function_selector("new"), + salt, + DEBUG_OUTPUT, + CollectEvents::Skip, + ) + .result + .unwrap(); + assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); + result.account_id +} + +pub fn get_pallet_index() -> u8 { + // Get the index of the pallet (module) + <::PalletInfo as frame_support::traits::PalletInfo>::index::() + .expect("Every active module has an index in the runtime; qed") as u8 +} + +#[test] +fn encoding_decoding_dispatch_error() { + use codec::{Decode, Encode}; + use sp_runtime::{ArithmeticError, DispatchError, ModuleError, TokenError}; + + new_test_ext().execute_with(|| { + let error = DispatchError::Module(ModuleError { + index: 255, + error: [2, 0, 0, 0], + message: Some("error message"), + }); + let encoded = error.encode(); + let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); + assert_eq!(encoded, vec![3, 255, 2, 0, 0, 0]); + assert_eq!( + decoded, + // `message` is skipped for encoding. + DispatchError::Module(ModuleError { index: 255, error: [2, 0, 0, 0], message: None }) + ); + println!("Encoded Module Error: {:?}", encoded); + + // Example pallet assets Error into ModuleError + let index = get_pallet_index::(); + let mut error = + pallet_assets::Error::NotFrozen::.encode(); + error.resize(sp_runtime::MAX_MODULE_ERROR_ENCODED_SIZE, 0); + let message = None; + let error = DispatchError::Module(ModuleError { + index, + error: TryInto::try_into(error).expect("should work"), + message, + }); + let encoded = error.encode(); + let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); + assert_eq!(encoded, vec![3, 52, 18, 0, 0, 0]); + assert_eq!( + decoded, + DispatchError::Module(ModuleError { index: 52, error: [18, 0, 0, 0], message: None }) + ); + println!("Encoded Module Error: {:?}", encoded); + + // Example DispatchError::Token + let error = DispatchError::Token(TokenError::UnknownAsset); + let encoded = error.encode(); + assert_eq!(encoded, vec![7, 4]); + println!("Encoded Token Error: {:?}", encoded); + + // Example DispatchError::Arithmetic + let error = DispatchError::Arithmetic(ArithmeticError::Overflow); + let encoded = error.encode(); + assert_eq!(encoded, vec![8, 1]); + println!("Encoded Arithmetic Error: {:?}", encoded); + }); +} + +#[test] +fn encoding_of_enum() { + use codec::{Decode, Encode}; + + // Comprehensive enum with all different type of variants. + #[derive(Debug, PartialEq, Encode, Decode)] + enum ComprehensiveEnum { + SimpleVariant, + DataVariant(u8), + NamedFields { w: u8 }, + NestedEnum(InnerEnum), + OptionVariant(Option), + VecVariant(Vec), + TupleVariant(u8, u8), + NestedStructVariant(NestedStruct), + NestedEnumStructVariant(NestedEnumStruct), + } + + #[derive(Debug, PartialEq, Encode, Decode)] + enum InnerEnum { + A, + B { inner_data: u8 }, + C(u8), + } + + #[derive(Debug, PartialEq, Encode, Decode)] + struct NestedStruct { + x: u8, + y: u8, + } + + #[derive(Debug, PartialEq, Encode, Decode)] + struct NestedEnumStruct { + inner_enum: InnerEnum, + } + + // Creating each possible variant for an enum. + let enum_simple = ComprehensiveEnum::SimpleVariant; + let enum_data = ComprehensiveEnum::DataVariant(42); + let enum_named = ComprehensiveEnum::NamedFields { w: 42 }; + let enum_nested = ComprehensiveEnum::NestedEnum(InnerEnum::B { inner_data: 42 }); + let enum_option = ComprehensiveEnum::OptionVariant(Some(42)); + let enum_vec = ComprehensiveEnum::VecVariant(vec![1, 2, 3, 4, 5]); + let enum_tuple = ComprehensiveEnum::TupleVariant(42, 42); + let enum_nested_struct = ComprehensiveEnum::NestedStructVariant(NestedStruct { x: 42, y: 42 }); + let enum_nested_enum_struct = ComprehensiveEnum::NestedEnumStructVariant(NestedEnumStruct { + inner_enum: InnerEnum::C(42), + }); + + // Encode and print each variant individually to see their encoded values. + println!("{:?} -> {:?}", enum_simple, enum_simple.encode()); + println!("{:?} -> {:?}", enum_data, enum_data.encode()); + println!("{:?} -> {:?}", enum_named, enum_named.encode()); + println!("{:?} -> {:?}", enum_nested, enum_nested.encode()); + println!("{:?} -> {:?}", enum_option, enum_option.encode()); + println!("{:?} -> {:?}", enum_vec, enum_vec.encode()); + println!("{:?} -> {:?}", enum_tuple, enum_tuple.encode()); + println!("{:?} -> {:?}", enum_nested_struct, enum_nested_struct.encode()); + println!("{:?} -> {:?}", enum_nested_enum_struct, enum_nested_enum_struct.encode()); +} + +#[test] +fn dispatch_error_to_status_code_to_pop_api_error_works() { + // Create all the different `DispatchError` variants with its respective `PopApiError`. + let test_cases = vec![ + (DispatchError::CannotLookup, PopApiError::CannotLookup), + (DispatchError::BadOrigin, PopApiError::BadOrigin), + ( + DispatchError::Module(sp_runtime::ModuleError { + index: 1, + error: [2, 0, 0, 0], + message: Some("hallo"), + }), + PopApiError::Module { index: 1, error: 2 }, + ), + (DispatchError::ConsumerRemaining, PopApiError::ConsumerRemaining), + (DispatchError::NoProviders, PopApiError::NoProviders), + (DispatchError::TooManyConsumers, PopApiError::TooManyConsumers), + ( + DispatchError::Token(sp_runtime::TokenError::FundsUnavailable), + PopApiError::Token(TokenError::FundsUnavailable), + ), + ( + DispatchError::Arithmetic(sp_runtime::ArithmeticError::Overflow), + PopApiError::Arithmetic(ArithmeticError::Overflow), + ), + ( + DispatchError::Transactional(sp_runtime::TransactionalError::LimitReached), + PopApiError::Transactional(TransactionalError::LimitReached), + ), + (DispatchError::Exhausted, PopApiError::Exhausted), + (DispatchError::Corruption, PopApiError::Corruption), + (DispatchError::Unavailable, PopApiError::Unavailable), + (DispatchError::RootNotAllowed, PopApiError::RootNotAllowed), + ]; + for (error, pop_api_error) in test_cases { + // Show that the encoding and decoding of the PopApiError <> u32 (status code) works. + let status_code = crate::extensions::convert_to_status_code(error); + let error = pop_api::error::convert_to_pop_api_error(status_code); + assert_eq!(pop_api_error, error,); + } +} From f824801e092ac5a6224779bbf06f93ec3b04402b Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Thu, 20 Jun 2024 15:50:00 +0200 Subject: [PATCH 08/76] refactor: error handling --- pop-api/src/lib.rs | 2 +- pop-api/src/v0/assets/pallets/assets.rs | 3 ++- pop-api/src/v0/balances.rs | 3 ++- primitives/Cargo.toml | 5 ----- primitives/src/lib.rs | 7 +------ runtime/devnet/src/tests/local_fungibles.rs | 22 ++++++++------------- runtime/testnet/src/extensions.rs | 5 ++++- 7 files changed, 18 insertions(+), 29 deletions(-) diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index a785aebe..e78aff9e 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -13,7 +13,7 @@ pub mod primitives; pub mod v0; type AccountId = AccountId32; -// TODO: do the same as above and check expanded code. +// TODO: do the same as above and check expanded macro code. type Balance = ::Balance; type BlockNumber = ::BlockNumber; type StringLimit = u32; diff --git a/pop-api/src/v0/assets/pallets/assets.rs b/pop-api/src/v0/assets/pallets/assets.rs index 7a575e08..b28d3086 100644 --- a/pop-api/src/v0/assets/pallets/assets.rs +++ b/pop-api/src/v0/assets/pallets/assets.rs @@ -1,4 +1,3 @@ -// TODO: what to put in this file? #![allow(dead_code)] use crate::{Balance, RuntimeCall, *}; @@ -412,6 +411,8 @@ pub(crate) enum AssetsCall { Block { id: AssetIdParameter, who: MultiAddress }, } +// TODO: do we want add this. Not being used atm but necessary if we want to provide access to the +// rest of the pallet. #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum AssetsError { diff --git a/pop-api/src/v0/balances.rs b/pop-api/src/v0/balances.rs index 5d38d851..c9759a45 100644 --- a/pop-api/src/v0/balances.rs +++ b/pop-api/src/v0/balances.rs @@ -1,4 +1,3 @@ -// TODO: what to put in this file? use crate::{dispatch, error::PopApiError, primitives::MultiAddress, v0::RuntimeCall, AccountId}; type Result = core::result::Result; @@ -24,6 +23,8 @@ pub enum BalancesCall { }, } +// TODO: do we want add this. Not being used atm but necessary if we want to provide access to the +// rest of the pallet. #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum BalancesError { diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml index 1098b557..e7237b51 100644 --- a/primitives/Cargo.toml +++ b/primitives/Cargo.toml @@ -12,11 +12,6 @@ scale-decode = { version = "0.10.0", default-features = false, features = ["deri scale-encode = { version = "0.5.0", default-features = false, features = ["derive"], optional = true } scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true } -#scale = { workspace = true, features = ["max-encoded-len"] } -#scale-decode = { workspace = true, features = ["derive"], optional = true } -#scale-encode = { workspace = true, features = ["derive"], optional = true } -#scale-info = { workspace = true, features = ["derive"], optional = true } - [features] default = ["std"] std = [ diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 7c1672b8..1e008c31 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -3,12 +3,7 @@ pub use bounded_collections::{BoundedBTreeMap, BoundedBTreeSet, BoundedVec, ConstU32}; use scale::{Decode, Encode, MaxEncodedLen}; #[cfg(feature = "std")] -use { - scale_decode::DecodeAsType, - scale_encode::EncodeAsType, - scale_info::TypeInfo, -}; - +use {scale_decode::DecodeAsType, scale_encode::EncodeAsType, scale_info::TypeInfo}; pub mod cross_chain; pub mod storage_keys; diff --git a/runtime/devnet/src/tests/local_fungibles.rs b/runtime/devnet/src/tests/local_fungibles.rs index bc13fbde..b1ddda2e 100644 --- a/runtime/devnet/src/tests/local_fungibles.rs +++ b/runtime/devnet/src/tests/local_fungibles.rs @@ -1,3 +1,11 @@ +// Todo - errors: +// - Badorigin: contract is always signed +// - Lookup: is a valid AccountId due to the contract +// - Many errors can occur from calling a dispatchable. As of now, most of the DispatchErrors are +// handled by the pop api but not all of them are tested. How should I approach this? I.e.: +// - Arithmetic errors +// - Token errors +// - others (besides Module errors) that I might haven't found yet. #![cfg(test)] use super::*; @@ -251,16 +259,6 @@ fn asset_exists_works() { }); } -// Todo - errors: -// - Badorigin: contract is always signed -// - Lookup: is a valid AccountId due to the contract -// - reserve(): Overflow, LiquidityRestrictions; frozen -// - Callback -// - StorageDepositLimitExhausted -// todo: errors: -// - TokenErrors -// - Arithmetic -// - https://github.com/paritytech/polkadot-sdk/blob/3977f389cce4a00fd7100f95262e0563622b9aa4/substrate/frame/assets/src/functions.rs#L125 #[test] #[ignore] fn create_works() { @@ -407,10 +405,6 @@ fn transfer_from_mint_works() { }); } -// Todo: error: -// - https://github.com/paritytech/polkadot-sdk/blob/2460cddf57660a88844d201f769eb17a7accce5a/substrate/frame/assets/src/functions.rs#L161 -// - ArithmeticError: Underflow, Overflow -// - https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/assets/src/functions.rs#L125 #[test] #[ignore] fn transfer_works() { diff --git a/runtime/testnet/src/extensions.rs b/runtime/testnet/src/extensions.rs index 6bbfaa36..4e7402a7 100644 --- a/runtime/testnet/src/extensions.rs +++ b/runtime/testnet/src/extensions.rs @@ -86,6 +86,7 @@ impl TryFrom for v0::FuncId { 0x1 => Self::ReadState, _ => { log::error!("called an unregistered `func_id`: {:}", func_id); + // TODO: Other error. return Err(DispatchError::Other("unimplemented func_id")); }, }; @@ -206,6 +207,7 @@ where RuntimeStateKeys::ParachainSystem(key) => { read_parachain_system_state::(key, &mut env) }, + _ => Err(DispatchError::Other("Unknown state keys")), }? .encode(); @@ -215,7 +217,8 @@ where ); env.write(&result, false, None).map_err(|e| { log::trace!(target: LOG_TARGET, "{:?}", e); - DispatchError::Other("unable to write results to contract memory") + // TODO: Other error. + DispatchError::Other("Unable to write results to contract memory") }) } From 467050307884c6692bcbbbfa6b704f0c42f4ab16 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Thu, 20 Jun 2024 18:49:18 +0200 Subject: [PATCH 09/76] refactor: clarify todos --- pop-api/src/lib.rs | 2 +- pop-api/src/v0/assets/pallets/assets.rs | 4 ++-- pop-api/src/v0/balances.rs | 4 ++-- runtime/devnet/src/extensions.rs | 2 ++ runtime/devnet/src/tests/local_fungibles.rs | 8 +++----- runtime/testnet/src/extensions.rs | 1 + 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index e78aff9e..d00f1e9a 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -13,7 +13,7 @@ pub mod primitives; pub mod v0; type AccountId = AccountId32; -// TODO: do the same as above and check expanded macro code. +// TODO: do the same as the AccountId above and check expanded macro code. type Balance = ::Balance; type BlockNumber = ::BlockNumber; type StringLimit = u32; diff --git a/pop-api/src/v0/assets/pallets/assets.rs b/pop-api/src/v0/assets/pallets/assets.rs index b28d3086..87695b81 100644 --- a/pop-api/src/v0/assets/pallets/assets.rs +++ b/pop-api/src/v0/assets/pallets/assets.rs @@ -411,8 +411,8 @@ pub(crate) enum AssetsCall { Block { id: AssetIdParameter, who: MultiAddress }, } -// TODO: do we want add this. Not being used atm but necessary if we want to provide access to the -// rest of the pallet. +// TODO: Not being used atm but necessary if we want to provide access to the +// rest of the pallet, outside of the use cases. #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum AssetsError { diff --git a/pop-api/src/v0/balances.rs b/pop-api/src/v0/balances.rs index c9759a45..56c3ca72 100644 --- a/pop-api/src/v0/balances.rs +++ b/pop-api/src/v0/balances.rs @@ -23,8 +23,8 @@ pub enum BalancesCall { }, } -// TODO: do we want add this. Not being used atm but necessary if we want to provide access to the -// rest of the pallet. +// TODO: Not being used atm but necessary if we want to provide access to the +// rest of the pallet, outside of the use cases. #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum BalancesError { diff --git a/runtime/devnet/src/extensions.rs b/runtime/devnet/src/extensions.rs index 014893f9..32667d16 100644 --- a/runtime/devnet/src/extensions.rs +++ b/runtime/devnet/src/extensions.rs @@ -103,6 +103,7 @@ impl TryFrom for v0::FuncId { 0x2 => Self::SendXcm, _ => { log::error!("called an unregistered `func_id`: {:}", func_id); + // TODO: Other error. return Err(DispatchError::Other("unimplemented func_id")); }, }; @@ -236,6 +237,7 @@ where ); env.write(&result, false, None).map_err(|e| { log::trace!(target: LOG_TARGET, "{:?}", e); + // TODO: Other error. DispatchError::Other("unable to write results to contract memory") }) } diff --git a/runtime/devnet/src/tests/local_fungibles.rs b/runtime/devnet/src/tests/local_fungibles.rs index b1ddda2e..9f208b15 100644 --- a/runtime/devnet/src/tests/local_fungibles.rs +++ b/runtime/devnet/src/tests/local_fungibles.rs @@ -1,11 +1,9 @@ // Todo - errors: // - Badorigin: contract is always signed // - Lookup: is a valid AccountId due to the contract -// - Many errors can occur from calling a dispatchable. As of now, most of the DispatchErrors are -// handled by the pop api but not all of them are tested. How should I approach this? I.e.: -// - Arithmetic errors -// - Token errors -// - others (besides Module errors) that I might haven't found yet. +// - Many errors can occur from calling a dispatchable. All the DispatchErrors are handled by the +// pop api but not all the possible errors for each dipatchable are tested. How should I approach +// this? #![cfg(test)] use super::*; diff --git a/runtime/testnet/src/extensions.rs b/runtime/testnet/src/extensions.rs index 4e7402a7..a3284ad4 100644 --- a/runtime/testnet/src/extensions.rs +++ b/runtime/testnet/src/extensions.rs @@ -207,6 +207,7 @@ where RuntimeStateKeys::ParachainSystem(key) => { read_parachain_system_state::(key, &mut env) }, + // TODO: devnet / testnet feature. _ => Err(DispatchError::Other("Unknown state keys")), }? .encode(); From 2283161fdf9334697cbc84b9437767b189b9ce06 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Mon, 24 Jun 2024 17:38:28 +0200 Subject: [PATCH 10/76] refactor: error handling comments part 1 --- pop-api/Cargo.toml | 4 + pop-api/examples/fungibles/Cargo.toml | 2 +- pop-api/examples/fungibles/lib.rs | 25 +- pop-api/src/error.rs | 341 +++++++----- pop-api/src/lib.rs | 27 +- .../v0/assets/{use_cases => }/fungibles.rs | 74 ++- pop-api/src/v0/assets/mod.rs | 496 +++++++++++++++++- pop-api/src/v0/assets/pallets/assets.rs | 492 ----------------- pop-api/src/v0/assets/pallets/mod.rs | 1 - pop-api/src/v0/assets/use_cases/mod.rs | 1 - pop-api/src/v0/balances.rs | 6 +- pop-api/src/v0/cross_chain/mod.rs | 13 +- pop-api/src/v0/mod.rs | 26 +- pop-api/src/v0/nfts.rs | 8 +- pop-api/src/v0/state.rs | 3 +- runtime/devnet/Cargo.toml | 2 +- runtime/devnet/src/extensions.rs | 13 +- runtime/devnet/src/tests/local_fungibles.rs | 24 +- runtime/devnet/src/tests/mod.rs | 4 +- 19 files changed, 816 insertions(+), 746 deletions(-) rename pop-api/src/v0/assets/{use_cases => }/fungibles.rs (86%) delete mode 100644 pop-api/src/v0/assets/pallets/assets.rs delete mode 100644 pop-api/src/v0/assets/pallets/mod.rs delete mode 100644 pop-api/src/v0/assets/use_cases/mod.rs diff --git a/pop-api/Cargo.toml b/pop-api/Cargo.toml index 80818235..b80cb3b0 100644 --- a/pop-api/Cargo.toml +++ b/pop-api/Cargo.toml @@ -31,3 +31,7 @@ std = [ "sp-io/std", "sp-runtime/std", ] +assets = [] +balances = [] +nfts = [] +cross-chain = [] diff --git a/pop-api/examples/fungibles/Cargo.toml b/pop-api/examples/fungibles/Cargo.toml index 1fe32d6d..514f000f 100755 --- a/pop-api/examples/fungibles/Cargo.toml +++ b/pop-api/examples/fungibles/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [dependencies] ink = { version = "5.0.0", default-features = false } -pop-api = { path = "../../../pop-api", default-features = false } +pop-api = { path = "../../../pop-api", default-features = false, features = ["assets"] } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true } diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs index 59040590..161c7fdd 100755 --- a/pop-api/examples/fungibles/lib.rs +++ b/pop-api/examples/fungibles/lib.rs @@ -7,12 +7,12 @@ /// use ink::prelude::vec::Vec; use pop_api::{ - assets::use_cases::fungibles as api, - error::PopApiError, + assets::fungibles::{self as api, FungiblesError}, + error::{PopApiError, StatusCode}, primitives::{AccountId as AccountId32, AssetId}, }; -pub type Result = core::result::Result; +pub type Result = core::result::Result; #[ink::contract(env = pop_api::Environment)] mod fungibles { @@ -41,12 +41,12 @@ mod fungibles { #[ink(message)] pub fn total_supply(&self, id: AssetId) -> Result { - api::total_supply(id) + api::total_supply(id).map_err(|e| e.into()) } #[ink(message)] pub fn balance_of(&self, id: AssetId, owner: AccountId32) -> Result { - api::balance_of(id, owner) + api::balance_of(id, owner).map_err(|e| e.into()) } #[ink(message)] @@ -56,7 +56,7 @@ mod fungibles { owner: AccountId32, spender: AccountId32, ) -> Result { - api::allowance(id, owner, spender) + api::allowance(id, owner, spender).map_err(|e| e.into()) } #[ink(message)] @@ -68,7 +68,7 @@ mod fungibles { value, ); - let result = api::transfer(id, to, value); + let result = api::transfer(id, to, value).map_err(|e| e.into()); ink::env::debug_println!("Result: {:?}", result); result } @@ -80,8 +80,7 @@ mod fungibles { from: Option, to: Option, value: Balance, - // In the standard a `[u8]`, but the size needs to be known at compile time ink's `Vec` - // has to be used. + // In the standard a `[u8]`, but the size needs to be known at compile time. data: Vec, ) -> Result<()> { ink::env::debug_println!( @@ -92,7 +91,7 @@ mod fungibles { value, ); - let result = api::transfer_from(id, from, to, value, &data); + let result = api::transfer_from(id, from, to, value, &data).map_err(|e| e.into()); ink::env::debug_println!("Result: {:?}", result); result } @@ -119,7 +118,7 @@ mod fungibles { admin, min_balance, ); - let result = api::create(id, admin, min_balance); + let result = api::create(id, admin, min_balance).map_err(|e| e.into()); ink::env::debug_println!("Result: {:?}", result); result } @@ -139,14 +138,14 @@ mod fungibles { symbol, decimals, ); - let result = api::set_metadata(id, name, symbol, decimals); + let result = api::set_metadata(id, name, symbol, decimals).map_err(|e| e.into()); ink::env::debug_println!("Result: {:?}", result); result } #[ink(message)] pub fn asset_exists(&self, id: AssetId) -> Result { - api::asset_exists(id) + api::asset_exists(id).map_err(|e| e.into()) } } diff --git a/pop-api/src/error.rs b/pop-api/src/error.rs index 92da4045..4d5a8fb9 100644 --- a/pop-api/src/error.rs +++ b/pop-api/src/error.rs @@ -1,8 +1,35 @@ -use crate::assets::use_cases::fungibles::{convert_to_fungibles_error, FungiblesError}; use ink::env::chain_extension::FromStatusCode; use scale::{Decode, Encode}; use PopApiError::*; +#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub struct StatusCode(pub u32); + +impl From for StatusCode { + fn from(value: u32) -> Self { + StatusCode(value) + } +} +impl FromStatusCode for StatusCode { + fn from_status_code(status_code: u32) -> Result<(), Self> { + match status_code { + 0 => Ok(()), + _ => { + let mut encoded = status_code.to_le_bytes(); + convert_unknown_errors(&mut encoded); + Err(StatusCode::from(u32::from_le_bytes(encoded))) + }, + } + } +} + +impl From for StatusCode { + fn from(_: scale::Error) -> Self { + DecodingFailed.into() + } +} + #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] #[repr(u8)] @@ -46,79 +73,87 @@ pub enum PopApiError { Unavailable = 12, /// Root origin is not allowed. RootNotAllowed = 13, - // TODO: make generic and add docs. - UseCaseError(FungiblesError) = 254, DecodingFailed = 255, } -impl FromStatusCode for PopApiError { - fn from_status_code(status_code: u32) -> core::result::Result<(), Self> { - match status_code { - 0 => Ok(()), - _ => Err(convert_to_pop_api_error(status_code)), - } +impl From for StatusCode { + fn from(value: PopApiError) -> Self { + let mut encoded_error = value.encode(); + // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). + encoded_error.resize(4, 0); + StatusCode::from( + u32::decode(&mut &encoded_error[..]).expect("qid, resized to 4 bytes line above"), + ) } } -// `pub` because it is used in `runtime/devnet/src/extensions/tests/mod.rs`'s test: -// `dispatch_error_to_status_code_to_pop_api_error_works` -// -// This function converts a given `status_code` (u32) into a `PopApiError`. First it encodes the -// status code into a 4-byte array and checks for unknown nested errors. If decoding into -// `PopApiError` fails (e.g. a breaking change in the `DispatchError`), it handles the error by -// converting it to the `Other` variant by shifting each byte one position forward (the last byte is -// not used for anything)and setting the first byte to 0. If decoding succeeds, it checks if the -// error is of the `Module` variant and performs any necessary conversion based on the use case. -pub fn convert_to_pop_api_error(status_code: u32) -> PopApiError { - let mut encoded: [u8; 4] = - status_code.encode().try_into().expect("qid u32 always encodes to 4 bytes"); - encoded = check_for_unknown_nesting(encoded); - let error = match PopApiError::decode(&mut &encoded[..]) { - Err(_) => { - encoded[3] = encoded[2]; - encoded[2] = encoded[1]; - encoded[1] = encoded[0]; - encoded[0] = 0; - PopApiError::decode(&mut &encoded[..]).unwrap().into() - }, - Ok(error) => { - if let crate::PopApiError::Module { index, error } = error { - // TODO: make generic. - convert_to_fungibles_error(index, error) - } else { - error - } - }, - }; - ink::env::debug_println!("PopApiError: {:?}", error); - error +impl From for PopApiError { + // `pub` because it is used in `runtime/devnet/src/extensions/tests/mod.rs`'s test: + // `dispatch_error_to_status_code_to_pop_api_error_works` + // + // This function converts a given `status_code` (u32) into a `PopApiError`. + fn from(value: StatusCode) -> Self { + let encoded: [u8; 4] = value.0.to_le_bytes(); + PopApiError::decode(&mut &encoded[..]).unwrap_or(DecodingFailed) + } } -// If a unknown nested variant of the `DispatchError` is detected meaning any of the subsequent -// bytes are non-zero (e.g. breaking change in the DispatchError), the error needs to be converted -// into `PopApiError::Other`'s encoded value. This conversion is done by shifting the bytes one -// position forward (the last byte is discarded as it is not being used) and replacing the first -// byte with the `Other` encoded value (0u8). This ensures that the error is correctly categorized -// as an `Other` variant. -fn check_for_unknown_nesting(encoded_error: [u8; 4]) -> [u8; 4] { - if non_nested_pop_api_errors().contains(&encoded_error[0]) - && encoded_error[1..].iter().any(|x| *x != 0u8) - { - [0u8, encoded_error[0], encoded_error[1], encoded_error[2]] - } else if singular_nested_pop_api_errors().contains(&encoded_error[0]) +// If an unknown nested variant of the `DispatchError` is detected (i.e., any of the subsequent +// bytes are non-zero, indicating a breaking change in the `DispatchError`), the error needs to be +// converted into the encoded value of `PopApiError::Other`. This conversion is performed by +// shifting the bytes one position forward (discarding the last byte as it is not used) and setting +// the first byte to the encoded value of `Other` (0u8). This ensures the error is correctly +// categorized as an `Other` variant. +// +// Byte layout explanation: +// - Byte 0: PopApiError +// - Byte 1: +// - Must be zero for `UNIT_ERRORS`. +// - Represents the nested error in `SINGLE_NESTED_ERRORS`. +// - Represents the first level of nesting in `DOUBLE_NESTED_ERRORS`. +// - Byte 2: +// - Represents the second level of nesting in `DOUBLE_NESTED_ERRORS`. +// - Byte 3: +// - Unused or represents further nested information. +// +// This mechanism ensures backward compatibility by correctly categorizing any unknown nested errors +// into the `Other` variant, thus preventing issues caused by new or unexpected error formats. +pub(crate) fn convert_unknown_nested_errors(encoded_error: &mut [u8; 4]) { + // Converts single nested errors that are known to the Pop API as unit errors into `Other`. + if UNIT_ERRORS.contains(&encoded_error[0]) && encoded_error[1..].iter().any(|x| *x != 0u8) { + encoded_error[..].rotate_right(1); + encoded_error[0] = 0u8; + // Converts double nested errors that are known to the Pop API as single nested errors into + // `Other`. + } else if SINGLE_NESTED_ERRORS.contains(&encoded_error[0]) && encoded_error[2..].iter().any(|x| *x != 0u8) { - [0u8, encoded_error[0], encoded_error[1], encoded_error[2]] - } else { - encoded_error + encoded_error[..].rotate_right(1); + encoded_error[0] = 0u8; + } else if DOUBLE_NESTED_ERRORS.contains(&encoded_error[0]) + && encoded_error[3..].iter().any(|x| *x != 0u8) + { + encoded_error[..].rotate_right(1); + encoded_error[0] = 0u8; } } -impl From for PopApiError { - fn from(_: scale::Error) -> Self { - DecodingFailed +pub(crate) fn convert_unknown_errors(encoded_error: &mut [u8; 4]) { + let all_errors = [ + UNIT_ERRORS.as_slice(), + SINGLE_NESTED_ERRORS.as_slice(), + DOUBLE_NESTED_ERRORS.as_slice(), + // `DecodingFailed`. + &[255u8], + ] + .concat(); + if !all_errors.contains(&encoded_error[0]) { + encoded_error[..].rotate_right(1); + encoded_error[0] = 0u8; } + convert_unknown_nested_errors(encoded_error); } + #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum TokenError { @@ -165,91 +200,129 @@ pub enum TransactionalError { NoLayer, } -fn singular_nested_pop_api_errors() -> [u8; 3] { - const TOKEN_ERROR: u8 = 7; - const ARITHMETIC_ERROR: u8 = 8; - const TRANSACTION_ERROR: u8 = 9; - [TOKEN_ERROR, ARITHMETIC_ERROR, TRANSACTION_ERROR] -} +// Unit `DispatchError` variants (variant: index): +// - CannotLookup: 1, +// - BadOrigin: 2, +// - ConsumerRemaining: 4, +// - NoProviders: 5, +// - TooManyConsumers: 6, +// - Exhausted: 10, +// - Corruption: 11, +// - Unavailable: 12, +// - RootNotAllowed: 13, +const UNIT_ERRORS: [u8; 9] = [1, 2, 4, 5, 6, 10, 11, 12, 13]; -fn non_nested_pop_api_errors() -> [u8; 9] { - const CANNOT_LOOKUP: u8 = 1; - const BAD_ORIGIN: u8 = 2; - const CONSUMER_REMAINING: u8 = 4; - const NO_PROVIDERS: u8 = 5; - const TOO_MANY_CONSUMERS: u8 = 6; - const EXHAUSTED: u8 = 10; - const CORRUPTION: u8 = 11; - const UNAVAILABLE: u8 = 12; - const ROOT_NOT_ALLOWED: u8 = 13; - [ - CANNOT_LOOKUP, - BAD_ORIGIN, - CONSUMER_REMAINING, - NO_PROVIDERS, - TOO_MANY_CONSUMERS, - EXHAUSTED, - CORRUPTION, - UNAVAILABLE, - ROOT_NOT_ALLOWED, - ] -} +// Single nested `DispatchError` variants (variant: index): +// - Token: 3, +// - Arithmetic: 8, +// - Transaction: 9, +const SINGLE_NESTED_ERRORS: [u8; 3] = [7, 8, 9]; -#[test] -fn u32_always_encodes_to_4_bytes() { - assert_eq!(0u32.encode().len(), 4); - assert_eq!(u32::MAX.encode().len(), 4); -} +const DOUBLE_NESTED_ERRORS: [u8; 1] = [3]; -// If decoding failed the encoded value is converted to the `PopApiError::Other`. This handles -// unknown errors coming from the runtime. This could happen if a contract is not upgraded to the -// latest Pop API version. -#[test] -fn test_non_existing_pop_api_errors() { - let encoded_error = [7u8, 100u8, 0u8, 0u8]; - let status_code = u32::decode(&mut &encoded_error[..]).unwrap(); - let pop_api_error = ::from_status_code(status_code); - assert_eq!(Err(Other { dispatch_error_index: 7, error_index: 100, error: 0 }), pop_api_error); -} +#[cfg(test)] +mod tests { + use super::*; + use crate::error::{ArithmeticError::*, TokenError::*, TransactionalError::*}; + + #[test] + fn u32_always_encodes_to_4_bytes() { + assert_eq!(0u32.encode().len(), 4); + assert_eq!(u32::MAX.encode().len(), 4); + } -// If the encoded value indicates a nested PopApiError which is not handled by the Pop API version, -// the encoded value is converted into `PopApiError::Other`. -#[test] -fn check_for_unknown_nested_pop_api_errors_works() { - for &error_code in &non_nested_pop_api_errors() { - let encoded_error = [error_code, 1, 2, 3]; - let result = check_for_unknown_nesting(encoded_error); - let decoded = PopApiError::decode(&mut &result[..]).unwrap(); + // Decodes into `StatusCode(u32)` and converts it into the `PopApiError`. + fn into_pop_api_error(encoded_error: [u8; 4]) -> PopApiError { + let status_code = + StatusCode::from_status_code(u32::decode(&mut &encoded_error[..]).unwrap()) + .unwrap_err(); + status_code.into() + } + // Tests for the `From` implementation for `PopApiError`. + // + // If the encoded value indicates a nested `PopApiError` which is not handled by the Pop API + // version, the encoded value is converted into `PopApiError::Other`. + #[test] + fn test_unit_pop_api_error_variants() { + let errors = vec![ + CannotLookup, + BadOrigin, + ConsumerRemaining, + NoProviders, + TooManyConsumers, + Exhausted, + Corruption, + Unavailable, + RootNotAllowed, + ]; + for (i, &error_code) in UNIT_ERRORS.iter().enumerate() { + assert_eq!(into_pop_api_error([error_code, 0, 0, 0]), errors[i]); + assert_eq!( + into_pop_api_error([error_code, 1, 0, 0]), + Other { dispatch_error_index: error_code, error_index: 1, error: 0 }, + ); + assert_eq!( + into_pop_api_error([error_code, 1, 1, 0]), + Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, + ); + assert_eq!( + into_pop_api_error([error_code, 1, 1, 1]), + Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, + ); + } + } + + #[test] + fn test_single_nested_pop_api_error_variants() { + let errors = vec![ + [Token(FundsUnavailable), Token(OnlyProvider)], + [Arithmetic(Underflow), Arithmetic(Overflow)], + [Transactional(LimitReached), Transactional(NoLayer)], + ]; + for (i, &error_code) in SINGLE_NESTED_ERRORS.iter().enumerate() { + assert_eq!(into_pop_api_error([error_code, 0, 0, 0]), errors[i][0]); + assert_eq!(into_pop_api_error([error_code, 1, 0, 0]), errors[i][1]); + assert_eq!( + into_pop_api_error([error_code, 1, 1, 0]), + Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, + ); + assert_eq!( + into_pop_api_error([error_code, 1, 1, 1]), + Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, + ); + } + } + + #[test] + fn test_double_nested_pop_api_error_variants() { + assert_eq!(into_pop_api_error([3, 0, 0, 0]), Module { index: 0, error: 0 }); + assert_eq!(into_pop_api_error([3, 1, 0, 0]), Module { index: 1, error: 0 }); + assert_eq!(into_pop_api_error([3, 1, 1, 0]), Module { index: 1, error: 1 }); + // TODO: doesn't make sense. assert_eq!( - decoded, - Other { dispatch_error_index: error_code, error_index: 1, error: 2 }, - "Failed for error code: {}", - error_code + into_pop_api_error([3, 1, 1, 1]), + Other { dispatch_error_index: 3, error_index: 1, error: 1 }, ); } - for &error_code in &singular_nested_pop_api_errors() { - let encoded_error = [error_code, 1, 2, 3]; - let result = check_for_unknown_nesting(encoded_error); - let decoded = PopApiError::decode(&mut &result[..]).unwrap(); + #[test] + fn test_decoding_failed() { + assert_eq!(into_pop_api_error([255, 0, 0, 0]), DecodingFailed); + assert_eq!(into_pop_api_error([255, 255, 0, 0]), DecodingFailed); + assert_eq!(into_pop_api_error([255, 255, 255, 0]), DecodingFailed); + assert_eq!(into_pop_api_error([255, 255, 255, 255]), DecodingFailed); + } + + #[test] + fn test_random_encoded_values() { + assert_eq!( + into_pop_api_error([100, 100, 100, 100]), + Other { dispatch_error_index: 100, error_index: 100, error: 100 } + ); assert_eq!( - decoded, - Other { dispatch_error_index: error_code, error_index: 1, error: 2 }, - "Failed for error code: {}", - error_code + into_pop_api_error([200, 200, 200, 200]), + Other { dispatch_error_index: 200, error_index: 200, error: 200 } ); } } - -// This test ensures that a non-zero value for unused bytes does not interfere with the correct -// decoding of the error. It verifies that even with an additional byte, the errors are correctly -// decoded and represented in its correct variant. -#[test] -fn extra_byte_does_not_mess_up_decoding() { - // Module error - let encoded_error = [3u8, 4u8, 5u8, 6u8]; - let status_code = u32::decode(&mut &encoded_error[..]).unwrap(); - let pop_api_error = ::from_status_code(status_code); - assert_eq!(Err(Module { index: 4, error: 5 }), pop_api_error); -} diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index d00f1e9a..90c81998 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -1,12 +1,19 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -use crate::error::PopApiError; use ink::{prelude::vec::Vec, ChainExtensionInstance}; -use primitives::{cross_chain::*, storage_keys::*, AccountId as AccountId32}; -use scale::Encode; pub use sp_runtime::{BoundedVec, MultiAddress, MultiSignature}; -use v0::RuntimeCall; -pub use v0::{assets, balances, cross_chain, nfts, relay_chain_block_number, state}; + +use crate::error::{PopApiError, StatusCode}; +use primitives::{storage_keys::*, AccountId as AccountId32}; +#[cfg(feature = "assets")] +pub use v0::assets; +#[cfg(feature = "balances")] +pub use v0::balances; +#[cfg(feature = "cross-chain")] +pub use v0::cross_chain; +#[cfg(feature = "nfts")] +pub use v0::nfts; +use v0::{state, RuntimeCall}; pub mod error; pub mod primitives; @@ -15,11 +22,7 @@ pub mod v0; type AccountId = AccountId32; // TODO: do the same as the AccountId above and check expanded macro code. type Balance = ::Balance; -type BlockNumber = ::BlockNumber; -type StringLimit = u32; -type MaxTips = u32; - -pub type Result = core::result::Result; +pub type Result = core::result::Result; #[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] @@ -40,7 +43,7 @@ impl ink::env::Environment for Environment { #[ink::chain_extension(extension = 909)] pub trait PopApi { - type ErrorCode = PopApiError; + type ErrorCode = StatusCode; #[ink(function = 0)] #[allow(private_interfaces)] @@ -50,6 +53,7 @@ pub trait PopApi { #[allow(private_interfaces)] fn read_state(key: RuntimeStateKeys) -> Result>; + #[cfg(feature = "cross-chain")] #[ink(function = 2)] #[allow(private_interfaces)] fn send_xcm(xcm: CrossChainMessage) -> Result<()>; @@ -67,6 +71,7 @@ fn read_state(key: RuntimeStateKeys) -> Result> { .read_state(key) } +#[cfg(feature = "cross-chain")] fn send_xcm(xcm: CrossChainMessage) -> Result<()> { <::ChainExtension as ChainExtensionInstance>::instantiate( ) diff --git a/pop-api/src/v0/assets/use_cases/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs similarity index 86% rename from pop-api/src/v0/assets/use_cases/fungibles.rs rename to pop-api/src/v0/assets/fungibles.rs index 4af5d97e..b1bb86a7 100644 --- a/pop-api/src/v0/assets/use_cases/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -1,12 +1,8 @@ -use crate::{ - assets::pallets, - error::PopApiError::{self, *}, - AccountId, Balance, *, -}; +use crate::{assets, primitives::AssetId, AccountId, Balance, MultiAddress, StatusCode}; use ink::prelude::vec::Vec; -use primitives::AssetId; +use scale::Encode; -type Result = core::result::Result; +type Result = core::result::Result; /// Local Fungibles: /// 1. PSP-22 Interface @@ -31,7 +27,7 @@ type Result = core::result::Result; /// # Returns /// The total supply of the token, or an error if the operation fails. pub fn total_supply(id: AssetId) -> Result { - pallets::assets::total_supply(id) + assets::total_supply(id) } /// Returns the account balance for the specified `owner` for a given asset ID. Returns `0` if @@ -44,7 +40,7 @@ pub fn total_supply(id: AssetId) -> Result { /// # Returns /// The balance of the specified account, or an error if the operation fails. pub fn balance_of(id: AssetId, owner: AccountId) -> Result { - pallets::assets::balance_of(id, owner) + assets::balance_of(id, owner) } /// Returns the amount which `spender` is still allowed to withdraw from `owner` for a given @@ -58,7 +54,7 @@ pub fn balance_of(id: AssetId, owner: AccountId) -> Result { /// # Returns /// The remaining allowance, or an error if the operation fails. pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { - pallets::assets::allowance(id, owner, spender) + assets::allowance(id, owner, spender) } /// Transfers `value` amount of tokens from the caller's account to account `to`, with additional @@ -76,7 +72,7 @@ pub fn transfer( to: impl Into>, value: Balance, ) -> Result<()> { - pallets::assets::transfer(id, to, value) + assets::transfer(id, to, value) } /// Transfers `value` tokens on behalf of `from` to account `to` with additional `data` @@ -99,9 +95,9 @@ pub fn transfer_from( _data: &[u8], ) -> Result<()> { match (from, to) { - (None, Some(to)) => pallets::assets::mint(id, to, value), - (Some(from), None) => pallets::assets::burn(id, from, value), - (Some(from), Some(to)) => pallets::assets::transfer_approved(id, from, to, value), + (None, Some(to)) => assets::mint(id, to, value), + (Some(from), None) => assets::burn(id, from, value), + (Some(from), Some(to)) => assets::transfer_approved(id, from, to, value), _ => Ok(()), } } @@ -232,7 +228,7 @@ pub fn create( admin: impl Into>, min_balance: Balance, ) -> Result<()> { - pallets::assets::create(id, admin, min_balance) + assets::create(id, admin, min_balance) } /// Start the process of destroying a token with a given asset ID. @@ -295,7 +291,7 @@ pub fn create( /// # Returns /// Returns `Ok(())` if successful, or an error if the operation fails. pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) -> Result<()> { - pallets::assets::set_metadata(id, name, symbol, decimals) + assets::set_metadata(id, name, symbol, decimals) } /// Clear the metadata for a token with a given asset ID. @@ -312,12 +308,13 @@ pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) - // } pub fn asset_exists(id: AssetId) -> Result { - pallets::assets::asset_exists(id) + assets::asset_exists(id) } #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum FungiblesError { + Other(StatusCode), /// The asset is not live; either frozen or being destroyed. AssetNotLive, /// Not enough allowance to fulfill a request is available. @@ -341,32 +338,21 @@ pub enum FungiblesError { NoBalance, } -// TODO: make generic. -pub(crate) fn convert_to_fungibles_error(index: u8, error: u8) -> PopApiError { - match index { - 10 => balance_into(error), - 52 => assets_into(error), - _ => Module { index, error }, - } -} - -fn balance_into(error: u8) -> PopApiError { - match error { - 2 => UseCaseError(FungiblesError::NoBalance), - _ => Module { index: 10, error }, - } -} - -fn assets_into(error: u8) -> PopApiError { - match error { - 0 => UseCaseError(FungiblesError::InsufficientBalance), - 1 => UseCaseError(FungiblesError::NoAccount), - 2 => UseCaseError(FungiblesError::NoPermission), - 3 => UseCaseError(FungiblesError::Unknown), - 5 => UseCaseError(FungiblesError::InUse), - 7 => UseCaseError(FungiblesError::MinBalanceZero), - 10 => UseCaseError(FungiblesError::InsufficientAllowance), - 16 => UseCaseError(FungiblesError::AssetNotLive), - _ => Module { index: 52, error }, +impl From for FungiblesError { + fn from(value: StatusCode) -> Self { + let encoded = value.0.to_le_bytes(); + match encoded { + // Balances. + [3, 10, 2, _] => FungiblesError::NoBalance, + // Assets. + [3, 52, 0, _] => FungiblesError::NoAccount, + [3, 52, 1, _] => FungiblesError::NoPermission, + [3, 52, 2, _] => FungiblesError::Unknown, + [3, 52, 3, _] => FungiblesError::InUse, + [3, 52, 5, _] => FungiblesError::MinBalanceZero, + [3, 52, 7, _] => FungiblesError::InsufficientAllowance, + [3, 52, 10, _] => FungiblesError::AssetNotLive, + _ => FungiblesError::Other(value), + } } } diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index 736ccc0e..4ce68219 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1,2 +1,494 @@ -pub(crate) mod pallets; -pub mod use_cases; +#![allow(dead_code)] + +use crate::{Balance, RuntimeCall, *}; +use ink::prelude::vec::Vec; +use primitives::{AssetId, MultiAddress}; +use scale::{Compact, Encode}; + +pub mod fungibles; + +type Result = core::result::Result; + +/// [Pallet Assets](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/assets/src/lib.rs): +/// 1. Dispatchables +/// 2. Read state functions +/// +/// 1. Dispatchables within pallet assets (TrustBackedAssets instance) that can be used via the pop api on Pop Network: +/// - create +/// - start_destroy +/// - destroy_accounts +/// - destroy_approvals +/// - finish_destroy +/// - mint +/// - burn +/// - transfer +/// - transfer_keep_alive +/// - force_transfer +/// - freeze +/// - thaw +/// - freeze_asset +/// - thaw_asset +/// - transfer_ownership +/// - set_team +/// - set_metadata +/// - clear_metadata +/// - approve_transfer +/// - cancel_approval +/// - force_cancel_approval +/// - transfer_approved +/// - touch +/// - refund +/// - set_min_balance +/// - touch_other +/// - refund_other +/// - block + +/// Issue a new class of fungible assets from a public origin. +pub(crate) fn create( + id: AssetId, + admin: impl Into>, + min_balance: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Create { + id: id.into(), + admin: admin.into(), + min_balance, + })) +} + +/// Start the process of destroying a fungible asset class. +pub(crate) fn start_destroy(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::StartDestroy { id: id.into() })) +} + +/// Destroy all accounts associated with a given asset. +pub(crate) fn destroy_accounts(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::DestroyAccounts { id: id.into() })) +} + +/// Destroy all approvals associated with a given asset up to the max (see runtime configuration Assets `RemoveItemsLimit`). +pub(crate) fn destroy_approvals(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::DestroyApprovals { id: id.into() })) +} + +/// Complete destroying asset and unreserve currency. +pub(crate) fn finish_destroy(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::FinishDestroy { id: id.into() })) +} + +/// Mint assets of a particular class. +pub(crate) fn mint( + id: AssetId, + beneficiary: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Mint { + id: id.into(), + beneficiary: beneficiary.into(), + amount: Compact(amount), + })) +} + +/// Reduce the balance of `who` by as much as possible up to `amount` assets of `id`. +pub(crate) fn burn( + id: AssetId, + who: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Burn { + id: id.into(), + who: who.into(), + amount: Compact(amount), + })) +} + +/// Move some assets from the sender account to another. +pub(crate) fn transfer( + id: AssetId, + target: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { + id: id.into(), + target: target.into(), + amount: Compact(amount), + })) +} + +/// Move some assets from the sender account to another, keeping the sender account alive. +pub(crate) fn transfer_keep_alive( + id: AssetId, + target: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { + id: id.into(), + target: target.into(), + amount: Compact(amount), + })) +} + +/// Move some assets from one account to another. Sender should be the Admin of the asset `id`. +pub(crate) fn force_transfer( + id: AssetId, + source: impl Into>, + dest: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::ForceTransfer { + id: id.into(), + source: source.into(), + dest: dest.into(), + amount: Compact(amount), + })) +} + +/// Disallow further unprivileged transfers of an asset `id` from an account `who`. `who` +/// must already exist as an entry in `Account`s of the asset. If you want to freeze an +/// account that does not have an entry, use `touch_other` first. +pub(crate) fn freeze(id: AssetId, who: impl Into>) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Freeze { id: id.into(), who: who.into() })) +} + +/// Allow unprivileged transfers to and from an account again. +pub(crate) fn thaw(id: AssetId, who: impl Into>) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Thaw { id: id.into(), who: who.into() })) +} + +/// Disallow further unprivileged transfers for the asset class. +pub(crate) fn freeze_asset(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::FreezeAsset { id: id.into() })) +} + +/// Allow unprivileged transfers for the asset again. +pub(crate) fn thaw_asset(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::ThawAsset { id: id.into() })) +} + +/// Change the Owner of an asset. +pub(crate) fn transfer_ownership( + id: AssetId, + owner: impl Into>, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::TransferOwnership { + id: id.into(), + owner: owner.into(), + })) +} + +/// Change the Issuer, Admin and Freezer of an asset. +pub(crate) fn set_team( + id: AssetId, + issuer: impl Into>, + admin: impl Into>, + freezer: impl Into>, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::SetTeam { + id: id.into(), + issuer: issuer.into(), + admin: admin.into(), + freezer: freezer.into(), + })) +} + +/// Set the metadata for an asset. +pub(crate) fn set_metadata( + id: AssetId, + name: Vec, + symbol: Vec, + decimals: u8, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::SetMetadata { id: id.into(), name, symbol, decimals })) +} + +/// Clear the metadata for an asset. +pub(crate) fn clear_metadata(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::ClearMetadata { id: id.into() })) +} + +/// Approve an amount of asset for transfer by a delegated third-party account. +pub(crate) fn approve_transfer( + id: AssetId, + delegate: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { + id: id.into(), + delegate: delegate.into(), + amount: Compact(amount), + })) +} + +/// Cancel all of some asset approved for delegated transfer by a third-party account. +pub(crate) fn cancel_approval( + id: AssetId, + delegate: impl Into>, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::CancelApproval { + id: id.into(), + delegate: delegate.into(), + })) +} + +/// Cancel all of some asset approved for delegated transfer by a third-party account. +pub(crate) fn force_cancel_approval( + id: AssetId, + owner: impl Into>, + delegate: impl Into>, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::ForceCancelApproval { + id: id.into(), + owner: owner.into(), + delegate: delegate.into(), + })) +} + +/// Transfer some asset balance from a previously delegated account to some third-party +/// account. +pub(crate) fn transfer_approved( + id: AssetId, + owner: impl Into>, + destination: impl Into>, + amount: Balance, +) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::TransferApproved { + id: id.into(), + owner: owner.into(), + destination: destination.into(), + amount: Compact(amount), + })) +} + +/// Create an asset account for non-provider assets. +pub(crate) fn touch(id: AssetId) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Touch { id: id.into() })) +} + +/// Return the deposit (if any) of an asset account or a consumer reference (if any) of an +/// account. +pub(crate) fn refund(id: AssetId, allow_burn: bool) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Refund { id: id.into(), allow_burn })) +} + +/// Sets the minimum balance of an asset. +pub(crate) fn set_min_balance(id: AssetId, min_balance: Balance) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::SetMinBalance { + id: id.into(), + min_balance: Compact(min_balance), + })) +} + +/// Create an asset account for `who`. +pub(crate) fn touch_other(id: AssetId, who: impl Into>) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::TouchOther { id: id.into(), who: who.into() })) +} + +/// Return the deposit (if any) of a target asset account. Useful if you are the depositor. +pub(crate) fn refund_other(id: AssetId, who: impl Into>) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::RefundOther { id: id.into(), who: who.into() })) +} + +/// Disallow further unprivileged transfers of an asset `id` to and from an account `who`. +pub(crate) fn block(id: AssetId, who: impl Into>) -> Result<()> { + dispatch(RuntimeCall::Assets(AssetsCall::Block { id: id.into(), who: who.into() })) +} + +/// 2. Read state functions +/// - total_supply +/// - + +pub(crate) fn total_supply(id: AssetId) -> Result { + state::read(RuntimeStateKeys::Assets(AssetsKeys::TotalSupply(id))).into() +} + +pub(crate) fn balance_of(id: AssetId, owner: AccountId) -> Result { + state::read(RuntimeStateKeys::Assets(AssetsKeys::BalanceOf(id, owner))).into() +} + +pub(crate) fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { + state::read(RuntimeStateKeys::Assets(AssetsKeys::Allowance(id, owner, spender))).into() +} +pub(crate) fn asset_exists(id: AssetId) -> Result { + state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id))).into() +} + +// Parameters to extrinsics representing an asset id (`AssetIdParameter`) and a balance amount (`Balance`) are expected +// to be compact encoded. The pop api handles that for the developer. +// +// reference: https://substrate.stackexchange.com/questions/1873/what-is-the-meaning-of-palletcompact-in-pallet-development +// +// Asset id that is compact encoded. +type AssetIdParameter = Compact; +// Balance amount that is compact encoded. +type BalanceParameter = Compact; + +#[derive(Encode)] +pub(crate) enum AssetsCall { + #[codec(index = 0)] + Create { id: AssetIdParameter, admin: MultiAddress, min_balance: Balance }, + #[codec(index = 2)] + StartDestroy { id: AssetIdParameter }, + #[codec(index = 3)] + DestroyAccounts { id: AssetIdParameter }, + #[codec(index = 4)] + DestroyApprovals { id: AssetIdParameter }, + #[codec(index = 5)] + FinishDestroy { id: AssetIdParameter }, + #[codec(index = 6)] + Mint { + id: AssetIdParameter, + beneficiary: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 7)] + Burn { id: AssetIdParameter, who: MultiAddress, amount: BalanceParameter }, + #[codec(index = 8)] + Transfer { id: AssetIdParameter, target: MultiAddress, amount: BalanceParameter }, + #[codec(index = 9)] + TransferKeepAlive { + id: AssetIdParameter, + target: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 10)] + ForceTransfer { + id: AssetIdParameter, + source: MultiAddress, + dest: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 11)] + Freeze { id: AssetIdParameter, who: MultiAddress }, + #[codec(index = 12)] + Thaw { id: AssetIdParameter, who: MultiAddress }, + #[codec(index = 13)] + FreezeAsset { id: AssetIdParameter }, + #[codec(index = 14)] + ThawAsset { id: AssetIdParameter }, + #[codec(index = 15)] + TransferOwnership { id: AssetIdParameter, owner: MultiAddress }, + #[codec(index = 16)] + SetTeam { + id: AssetIdParameter, + issuer: MultiAddress, + admin: MultiAddress, + freezer: MultiAddress, + }, + #[codec(index = 17)] + SetMetadata { id: AssetIdParameter, name: Vec, symbol: Vec, decimals: u8 }, + #[codec(index = 18)] + ClearMetadata { id: AssetIdParameter }, + #[codec(index = 22)] + ApproveTransfer { + id: AssetIdParameter, + delegate: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 23)] + CancelApproval { id: AssetIdParameter, delegate: MultiAddress }, + #[codec(index = 24)] + ForceCancelApproval { + id: AssetIdParameter, + owner: MultiAddress, + delegate: MultiAddress, + }, + #[codec(index = 25)] + TransferApproved { + id: AssetIdParameter, + owner: MultiAddress, + destination: MultiAddress, + amount: BalanceParameter, + }, + #[codec(index = 26)] + Touch { id: AssetIdParameter }, + #[codec(index = 27)] + Refund { id: AssetIdParameter, allow_burn: bool }, + #[codec(index = 28)] + SetMinBalance { id: AssetIdParameter, min_balance: BalanceParameter }, + #[codec(index = 29)] + TouchOther { id: AssetIdParameter, who: MultiAddress }, + #[codec(index = 30)] + RefundOther { id: AssetIdParameter, who: MultiAddress }, + #[codec(index = 31)] + Block { id: AssetIdParameter, who: MultiAddress }, +} + +// TODO: Not being used atm but necessary if we want to provide access to the +// rest of the pallet, outside of the use cases. +#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum AssetsError { + /// Account balance must be greater than or equal to the transfer amount. + BalanceLow, + /// The account to alter does not exist. + NoAccount, + /// The signing account has no permission to do the operation. + NoPermission, + /// The given asset ID is unknown. + Unknown, + /// The origin account is frozen. + Frozen, + /// The asset ID is already taken. + InUse, + /// Invalid witness data given. + BadWitness, + /// Minimum balance should be non-zero. + MinBalanceZero, + /// Unable to increment the consumer reference counters on the account. Either no provider + /// reference exists to allow a non-zero balance of a non-self-sufficient asset, or one + /// fewer then the maximum number of consumers has been reached. + UnavailableConsumer, + /// Invalid metadata given. + BadMetadata, + /// No approval exists that would allow the transfer. + Unapproved, + /// The source account would not survive the transfer and it needs to stay alive. + WouldDie, + /// The asset-account already exists. + AlreadyExists, + /// The asset-account doesn't have an associated deposit. + NoDeposit, + /// The operation would result in funds being burned. + WouldBurn, + /// The asset is a live asset and is actively being used. Usually emit for operations such + /// as `start_destroy` which require the asset to be in a destroying state. + LiveAsset, + /// The asset is not live, and likely being destroyed. + AssetNotLive, + /// The asset status is not the expected status. + IncorrectStatus, + /// The asset should be frozen before the given operation. + NotFrozen, + /// Callback action resulted in error. + CallbackFailed, +} + +impl TryFrom for AssetsError { + type Error = PopApiError; + + fn try_from(status_code: u32) -> core::result::Result { + use AssetsError::*; + match status_code { + 0 => Ok(BalanceLow), + 1 => Ok(NoAccount), + 2 => Ok(NoPermission), + 3 => Ok(Unknown), + 4 => Ok(Frozen), + 5 => Ok(InUse), + 6 => Ok(BadWitness), + 7 => Ok(MinBalanceZero), + 8 => Ok(UnavailableConsumer), + 9 => Ok(BadMetadata), + 10 => Ok(Unapproved), + 11 => Ok(WouldDie), + 12 => Ok(AlreadyExists), + 13 => Ok(NoDeposit), + 14 => Ok(WouldBurn), + 15 => Ok(LiveAsset), + 16 => Ok(AssetNotLive), + 17 => Ok(IncorrectStatus), + 18 => Ok(NotFrozen), + _ => todo!(), + } + } +} diff --git a/pop-api/src/v0/assets/pallets/assets.rs b/pop-api/src/v0/assets/pallets/assets.rs deleted file mode 100644 index 87695b81..00000000 --- a/pop-api/src/v0/assets/pallets/assets.rs +++ /dev/null @@ -1,492 +0,0 @@ -#![allow(dead_code)] - -use crate::{Balance, RuntimeCall, *}; -use ink::prelude::vec::Vec; -use primitives::{AssetId, MultiAddress}; -use scale::{Compact, Encode}; - -type Result = core::result::Result; - -/// [Pallet Assets](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/assets/src/lib.rs): -/// 1. Dispatchables -/// 2. Read state functions -/// -/// 1. Dispatchables within pallet assets (TrustBackedAssets instance) that can be used via the pop api on Pop Network: -/// - create -/// - start_destroy -/// - destroy_accounts -/// - destroy_approvals -/// - finish_destroy -/// - mint -/// - burn -/// - transfer -/// - transfer_keep_alive -/// - force_transfer -/// - freeze -/// - thaw -/// - freeze_asset -/// - thaw_asset -/// - transfer_ownership -/// - set_team -/// - set_metadata -/// - clear_metadata -/// - approve_transfer -/// - cancel_approval -/// - force_cancel_approval -/// - transfer_approved -/// - touch -/// - refund -/// - set_min_balance -/// - touch_other -/// - refund_other -/// - block - -/// Issue a new class of fungible assets from a public origin. -pub(crate) fn create( - id: AssetId, - admin: impl Into>, - min_balance: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Create { - id: id.into(), - admin: admin.into(), - min_balance, - })) -} - -/// Start the process of destroying a fungible asset class. -pub(crate) fn start_destroy(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::StartDestroy { id: id.into() })) -} - -/// Destroy all accounts associated with a given asset. -pub(crate) fn destroy_accounts(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::DestroyAccounts { id: id.into() })) -} - -/// Destroy all approvals associated with a given asset up to the max (see runtime configuration Assets `RemoveItemsLimit`). -pub(crate) fn destroy_approvals(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::DestroyApprovals { id: id.into() })) -} - -/// Complete destroying asset and unreserve currency. -pub(crate) fn finish_destroy(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::FinishDestroy { id: id.into() })) -} - -/// Mint assets of a particular class. -pub(crate) fn mint( - id: AssetId, - beneficiary: impl Into>, - amount: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Mint { - id: id.into(), - beneficiary: beneficiary.into(), - amount: Compact(amount), - })) -} - -/// Reduce the balance of `who` by as much as possible up to `amount` assets of `id`. -pub(crate) fn burn( - id: AssetId, - who: impl Into>, - amount: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Burn { - id: id.into(), - who: who.into(), - amount: Compact(amount), - })) -} - -/// Move some assets from the sender account to another. -pub(crate) fn transfer( - id: AssetId, - target: impl Into>, - amount: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { - id: id.into(), - target: target.into(), - amount: Compact(amount), - })) -} - -/// Move some assets from the sender account to another, keeping the sender account alive. -pub(crate) fn transfer_keep_alive( - id: AssetId, - target: impl Into>, - amount: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { - id: id.into(), - target: target.into(), - amount: Compact(amount), - })) -} - -/// Move some assets from one account to another. Sender should be the Admin of the asset `id`. -pub(crate) fn force_transfer( - id: AssetId, - source: impl Into>, - dest: impl Into>, - amount: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::ForceTransfer { - id: id.into(), - source: source.into(), - dest: dest.into(), - amount: Compact(amount), - })) -} - -/// Disallow further unprivileged transfers of an asset `id` from an account `who`. `who` -/// must already exist as an entry in `Account`s of the asset. If you want to freeze an -/// account that does not have an entry, use `touch_other` first. -pub(crate) fn freeze(id: AssetId, who: impl Into>) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Freeze { id: id.into(), who: who.into() })) -} - -/// Allow unprivileged transfers to and from an account again. -pub(crate) fn thaw(id: AssetId, who: impl Into>) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Thaw { id: id.into(), who: who.into() })) -} - -/// Disallow further unprivileged transfers for the asset class. -pub(crate) fn freeze_asset(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::FreezeAsset { id: id.into() })) -} - -/// Allow unprivileged transfers for the asset again. -pub(crate) fn thaw_asset(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::ThawAsset { id: id.into() })) -} - -/// Change the Owner of an asset. -pub(crate) fn transfer_ownership( - id: AssetId, - owner: impl Into>, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::TransferOwnership { - id: id.into(), - owner: owner.into(), - })) -} - -/// Change the Issuer, Admin and Freezer of an asset. -pub(crate) fn set_team( - id: AssetId, - issuer: impl Into>, - admin: impl Into>, - freezer: impl Into>, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::SetTeam { - id: id.into(), - issuer: issuer.into(), - admin: admin.into(), - freezer: freezer.into(), - })) -} - -/// Set the metadata for an asset. -pub(crate) fn set_metadata( - id: AssetId, - name: Vec, - symbol: Vec, - decimals: u8, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::SetMetadata { id: id.into(), name, symbol, decimals })) -} - -/// Clear the metadata for an asset. -pub(crate) fn clear_metadata(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::ClearMetadata { id: id.into() })) -} - -/// Approve an amount of asset for transfer by a delegated third-party account. -pub(crate) fn approve_transfer( - id: AssetId, - delegate: impl Into>, - amount: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { - id: id.into(), - delegate: delegate.into(), - amount: Compact(amount), - })) -} - -/// Cancel all of some asset approved for delegated transfer by a third-party account. -pub(crate) fn cancel_approval( - id: AssetId, - delegate: impl Into>, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::CancelApproval { - id: id.into(), - delegate: delegate.into(), - })) -} - -/// Cancel all of some asset approved for delegated transfer by a third-party account. -pub(crate) fn force_cancel_approval( - id: AssetId, - owner: impl Into>, - delegate: impl Into>, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::ForceCancelApproval { - id: id.into(), - owner: owner.into(), - delegate: delegate.into(), - })) -} - -/// Transfer some asset balance from a previously delegated account to some third-party -/// account. -pub(crate) fn transfer_approved( - id: AssetId, - owner: impl Into>, - destination: impl Into>, - amount: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::TransferApproved { - id: id.into(), - owner: owner.into(), - destination: destination.into(), - amount: Compact(amount), - })) -} - -/// Create an asset account for non-provider assets. -pub(crate) fn touch(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Touch { id: id.into() })) -} - -/// Return the deposit (if any) of an asset account or a consumer reference (if any) of an -/// account. -pub(crate) fn refund(id: AssetId, allow_burn: bool) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Refund { id: id.into(), allow_burn })) -} - -/// Sets the minimum balance of an asset. -pub(crate) fn set_min_balance(id: AssetId, min_balance: Balance) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::SetMinBalance { - id: id.into(), - min_balance: Compact(min_balance), - })) -} - -/// Create an asset account for `who`. -pub(crate) fn touch_other(id: AssetId, who: impl Into>) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::TouchOther { id: id.into(), who: who.into() })) -} - -/// Return the deposit (if any) of a target asset account. Useful if you are the depositor. -pub(crate) fn refund_other(id: AssetId, who: impl Into>) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::RefundOther { id: id.into(), who: who.into() })) -} - -/// Disallow further unprivileged transfers of an asset `id` to and from an account `who`. -pub(crate) fn block(id: AssetId, who: impl Into>) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Block { id: id.into(), who: who.into() })) -} - -/// 2. Read state functions -/// - total_supply -/// - - -pub(crate) fn total_supply(id: AssetId) -> Result { - state::read(RuntimeStateKeys::Assets(AssetsKeys::TotalSupply(id))).into() -} - -pub(crate) fn balance_of(id: AssetId, owner: AccountId) -> Result { - state::read(RuntimeStateKeys::Assets(AssetsKeys::BalanceOf(id, owner))).into() -} - -pub(crate) fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { - state::read(RuntimeStateKeys::Assets(AssetsKeys::Allowance(id, owner, spender))).into() -} -pub(crate) fn asset_exists(id: AssetId) -> Result { - state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id))).into() -} - -// Parameters to extrinsics representing an asset id (`AssetIdParameter`) and a balance amount (`Balance`) are expected -// to be compact encoded. The pop api handles that for the developer. -// -// reference: https://substrate.stackexchange.com/questions/1873/what-is-the-meaning-of-palletcompact-in-pallet-development -// -// Asset id that is compact encoded. -type AssetIdParameter = Compact; -// Balance amount that is compact encoded. -type BalanceParameter = Compact; - -#[derive(Encode)] -pub(crate) enum AssetsCall { - #[codec(index = 0)] - Create { id: AssetIdParameter, admin: MultiAddress, min_balance: Balance }, - #[codec(index = 2)] - StartDestroy { id: AssetIdParameter }, - #[codec(index = 3)] - DestroyAccounts { id: AssetIdParameter }, - #[codec(index = 4)] - DestroyApprovals { id: AssetIdParameter }, - #[codec(index = 5)] - FinishDestroy { id: AssetIdParameter }, - #[codec(index = 6)] - Mint { - id: AssetIdParameter, - beneficiary: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 7)] - Burn { id: AssetIdParameter, who: MultiAddress, amount: BalanceParameter }, - #[codec(index = 8)] - Transfer { id: AssetIdParameter, target: MultiAddress, amount: BalanceParameter }, - #[codec(index = 9)] - TransferKeepAlive { - id: AssetIdParameter, - target: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 10)] - ForceTransfer { - id: AssetIdParameter, - source: MultiAddress, - dest: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 11)] - Freeze { id: AssetIdParameter, who: MultiAddress }, - #[codec(index = 12)] - Thaw { id: AssetIdParameter, who: MultiAddress }, - #[codec(index = 13)] - FreezeAsset { id: AssetIdParameter }, - #[codec(index = 14)] - ThawAsset { id: AssetIdParameter }, - #[codec(index = 15)] - TransferOwnership { id: AssetIdParameter, owner: MultiAddress }, - #[codec(index = 16)] - SetTeam { - id: AssetIdParameter, - issuer: MultiAddress, - admin: MultiAddress, - freezer: MultiAddress, - }, - #[codec(index = 17)] - SetMetadata { id: AssetIdParameter, name: Vec, symbol: Vec, decimals: u8 }, - #[codec(index = 18)] - ClearMetadata { id: AssetIdParameter }, - #[codec(index = 22)] - ApproveTransfer { - id: AssetIdParameter, - delegate: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 23)] - CancelApproval { id: AssetIdParameter, delegate: MultiAddress }, - #[codec(index = 24)] - ForceCancelApproval { - id: AssetIdParameter, - owner: MultiAddress, - delegate: MultiAddress, - }, - #[codec(index = 25)] - TransferApproved { - id: AssetIdParameter, - owner: MultiAddress, - destination: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 26)] - Touch { id: AssetIdParameter }, - #[codec(index = 27)] - Refund { id: AssetIdParameter, allow_burn: bool }, - #[codec(index = 28)] - SetMinBalance { id: AssetIdParameter, min_balance: BalanceParameter }, - #[codec(index = 29)] - TouchOther { id: AssetIdParameter, who: MultiAddress }, - #[codec(index = 30)] - RefundOther { id: AssetIdParameter, who: MultiAddress }, - #[codec(index = 31)] - Block { id: AssetIdParameter, who: MultiAddress }, -} - -// TODO: Not being used atm but necessary if we want to provide access to the -// rest of the pallet, outside of the use cases. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum AssetsError { - /// Account balance must be greater than or equal to the transfer amount. - BalanceLow, - /// The account to alter does not exist. - NoAccount, - /// The signing account has no permission to do the operation. - NoPermission, - /// The given asset ID is unknown. - Unknown, - /// The origin account is frozen. - Frozen, - /// The asset ID is already taken. - InUse, - /// Invalid witness data given. - BadWitness, - /// Minimum balance should be non-zero. - MinBalanceZero, - /// Unable to increment the consumer reference counters on the account. Either no provider - /// reference exists to allow a non-zero balance of a non-self-sufficient asset, or one - /// fewer then the maximum number of consumers has been reached. - UnavailableConsumer, - /// Invalid metadata given. - BadMetadata, - /// No approval exists that would allow the transfer. - Unapproved, - /// The source account would not survive the transfer and it needs to stay alive. - WouldDie, - /// The asset-account already exists. - AlreadyExists, - /// The asset-account doesn't have an associated deposit. - NoDeposit, - /// The operation would result in funds being burned. - WouldBurn, - /// The asset is a live asset and is actively being used. Usually emit for operations such - /// as `start_destroy` which require the asset to be in a destroying state. - LiveAsset, - /// The asset is not live, and likely being destroyed. - AssetNotLive, - /// The asset status is not the expected status. - IncorrectStatus, - /// The asset should be frozen before the given operation. - NotFrozen, - /// Callback action resulted in error. - CallbackFailed, -} - -impl TryFrom for AssetsError { - type Error = PopApiError; - - fn try_from(status_code: u32) -> core::result::Result { - use AssetsError::*; - match status_code { - 0 => Ok(BalanceLow), - 1 => Ok(NoAccount), - 2 => Ok(NoPermission), - 3 => Ok(Unknown), - 4 => Ok(Frozen), - 5 => Ok(InUse), - 6 => Ok(BadWitness), - 7 => Ok(MinBalanceZero), - 8 => Ok(UnavailableConsumer), - 9 => Ok(BadMetadata), - 10 => Ok(Unapproved), - 11 => Ok(WouldDie), - 12 => Ok(AlreadyExists), - 13 => Ok(NoDeposit), - 14 => Ok(WouldBurn), - 15 => Ok(LiveAsset), - 16 => Ok(AssetNotLive), - 17 => Ok(IncorrectStatus), - 18 => Ok(NotFrozen), - _ => todo!(), - } - } -} diff --git a/pop-api/src/v0/assets/pallets/mod.rs b/pop-api/src/v0/assets/pallets/mod.rs deleted file mode 100644 index 0b8a53a0..00000000 --- a/pop-api/src/v0/assets/pallets/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub(crate) mod assets; diff --git a/pop-api/src/v0/assets/use_cases/mod.rs b/pop-api/src/v0/assets/use_cases/mod.rs deleted file mode 100644 index 182590df..00000000 --- a/pop-api/src/v0/assets/use_cases/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod fungibles; diff --git a/pop-api/src/v0/balances.rs b/pop-api/src/v0/balances.rs index 56c3ca72..ae2709e7 100644 --- a/pop-api/src/v0/balances.rs +++ b/pop-api/src/v0/balances.rs @@ -1,6 +1,8 @@ -use crate::{dispatch, error::PopApiError, primitives::MultiAddress, v0::RuntimeCall, AccountId}; +use crate::{ + dispatch, primitives::MultiAddress, v0::RuntimeCall, AccountId, PopApiError, StatusCode, +}; -type Result = core::result::Result; +type Result = core::result::Result; pub fn transfer_keep_alive( dest: impl Into>, diff --git a/pop-api/src/v0/cross_chain/mod.rs b/pop-api/src/v0/cross_chain/mod.rs index 1d3c5b7d..583447b1 100644 --- a/pop-api/src/v0/cross_chain/mod.rs +++ b/pop-api/src/v0/cross_chain/mod.rs @@ -1,8 +1,15 @@ pub mod coretime; -use crate::error::PopApiError; +use crate::StatusCode; -type Result = core::result::Result; +type Result = core::result::Result; +type BlockNumber = ::BlockNumber; + +pub fn relay_chain_block_number() -> std::result::Result { + crate::v0::state::read(RuntimeStateKeys::ParachainSystem( + ParachainSystemKeys::LastRelayChainBlockNumber, + )) +} #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] @@ -63,7 +70,7 @@ pub enum Error { } impl TryFrom for Error { - type Error = PopApiError; + type Error = Error; fn try_from(status_code: u32) -> core::result::Result { use Error::*; diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index 75bcb878..41574cfb 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -1,24 +1,22 @@ -use crate::{ - primitives::storage_keys::{ParachainSystemKeys, RuntimeStateKeys}, - BlockNumber, PopApiError, -}; - +#[cfg(feature = "assets")] pub mod assets; +#[cfg(feature = "balances")] pub mod balances; +#[cfg(feature = "cross-chain")] pub mod cross_chain; +#[cfg(feature = "nfts")] pub mod nfts; pub mod state; -pub fn relay_chain_block_number() -> Result { - state::read(RuntimeStateKeys::ParachainSystem(ParachainSystemKeys::LastRelayChainBlockNumber)) -} - #[derive(scale::Encode)] pub(crate) enum RuntimeCall { - #[codec(index = 10)] - Balances(balances::BalancesCall), - #[codec(index = 50)] - Nfts(nfts::NftCalls), + // #[codec(index = 10)] + // #[cfg(feature = "balances")] + // Balances(balances::BalancesCall), + // #[codec(index = 50)] + // #[cfg(feature = "nfts")] + // Nfts(nfts::NftCalls), #[codec(index = 52)] - Assets(assets::pallets::assets::AssetsCall), + #[cfg(feature = "assets")] + Assets(assets::AssetsCall), } diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index 2de306a9..1e4406e5 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -1,12 +1,14 @@ use super::RuntimeCall; -use crate::{PopApiError, *}; +use crate::*; use ink::prelude::vec::Vec; use primitives::{ApprovalsLimit, BoundedBTreeMap, KeyLimit, MultiAddress}; pub use primitives::{CollectionId, ItemId}; use scale::Encode; pub use types::*; -type Result = core::result::Result; +type Result = core::result::Result; +type StringLimit = u32; +type MaxTips = u32; /// Issue a new collection of non-fungible items pub fn create( @@ -610,7 +612,7 @@ pub enum Error { } impl TryFrom for Error { - type Error = PopApiError; + type Error = Error; fn try_from(status_code: u32) -> core::result::Result { use Error::*; diff --git a/pop-api/src/v0/state.rs b/pop-api/src/v0/state.rs index 9f5e4c0c..1aca01cf 100644 --- a/pop-api/src/v0/state.rs +++ b/pop-api/src/v0/state.rs @@ -2,5 +2,6 @@ use crate::{primitives::storage_keys::RuntimeStateKeys, read_state, PopApiError} use scale::Decode; pub fn read(key: RuntimeStateKeys) -> crate::Result { - read_state(key).and_then(|v| T::decode(&mut &v[..]).map_err(|_e| PopApiError::DecodingFailed)) + read_state(key) + .and_then(|v| T::decode(&mut &v[..]).map_err(|_e| PopApiError::DecodingFailed.into())) } diff --git a/runtime/devnet/Cargo.toml b/runtime/devnet/Cargo.toml index 46208d8f..daa5457e 100644 --- a/runtime/devnet/Cargo.toml +++ b/runtime/devnet/Cargo.toml @@ -91,7 +91,7 @@ parachain-info.workspace = true env_logger = "0.11.2" hex = "0.4.3" enumflags2 = "0.7.9" -pop-api = { path = "../../pop-api", default-features = false } +pop-api = { path = "../../pop-api", defeult-features = false, features = ["assets"] } [features] default = ["std"] diff --git a/runtime/devnet/src/extensions.rs b/runtime/devnet/src/extensions.rs index 32667d16..b4c6ba84 100644 --- a/runtime/devnet/src/extensions.rs +++ b/runtime/devnet/src/extensions.rs @@ -72,16 +72,11 @@ where } } } - pub(crate) fn convert_to_status_code(error: DispatchError) -> u32 { - match error { - _ => { - let mut encoded_error = error.encode(); - // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). - encoded_error.resize(4, 0); - u32::decode(&mut &encoded_error[..]).unwrap() - }, - } + let mut encoded_error = error.encode(); + // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). + encoded_error.resize(4, 0); + u32::decode(&mut &encoded_error[..]).expect("qid, resized to 4 bytes line above") } pub mod v0 { diff --git a/runtime/devnet/src/tests/local_fungibles.rs b/runtime/devnet/src/tests/local_fungibles.rs index 9f208b15..154e9897 100644 --- a/runtime/devnet/src/tests/local_fungibles.rs +++ b/runtime/devnet/src/tests/local_fungibles.rs @@ -269,7 +269,7 @@ fn create_works() { // No balance to pay for fees. assert_eq!( decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), - UseCaseError(NoBalance) + Module { index: 10, error: 2 }, ); // Instantiate a contract without balance (relay token). let addr = @@ -278,7 +278,7 @@ fn create_works() { // No balance to pay fe deposit. assert_eq!( decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), - UseCaseError(NoBalance) + Module { index: 10, error: 2 }, ); // Instantiate a contract with balance. let addr = instantiate( @@ -290,12 +290,12 @@ fn create_works() { // Asset ID is already taken. assert_eq!( decoded::(create(addr.clone(), ASSET_ID, BOB, 1)), - UseCaseError(InUse) + Module { index: 52, error: 5 }, ); // The minimal balance for an asset must be non zero. assert_eq!( decoded::(create(addr.clone(), new_asset, BOB, 0)), - UseCaseError(MinBalanceZero) + Module { index: 52, error: 7 }, ); let result = create(addr.clone(), new_asset, BOB, 1); assert!(!result.did_revert(), "Contract reverted!"); @@ -347,7 +347,7 @@ fn transfer_from_mint_works() { amount, &[0u8] )), - UseCaseError(NoPermission) + Module { index: 52, error: 2 }, ); // Minimum balance of an asset can not be zero. assert_eq!( @@ -366,7 +366,7 @@ fn transfer_from_mint_works() { amount, &[0u8] )), - UseCaseError(AssetNotLive) + Module { index: 52, error: 16 }, ); thaw_asset(asset, addr.clone()); // Successful mint. @@ -398,7 +398,7 @@ fn transfer_from_mint_works() { amount, &[0u8] )), - UseCaseError(AssetNotLive) + Module { index: 52, error: 16 }, ); }); } @@ -418,7 +418,7 @@ fn transfer_works() { // Asset does not exist. assert_eq!( decoded::(transfer(addr.clone(), 1, BOB, amount,)), - UseCaseError(Unknown) + Module { index: 52, error: 3 }, ); // Create asset with Alice as owner and mint `amount` to contract address. let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); @@ -426,18 +426,18 @@ fn transfer_works() { freeze_asset(asset, ALICE); assert_eq!( decoded::(transfer(addr.clone(), asset, BOB, amount,)), - UseCaseError(AssetNotLive) + Module { index: 52, error: 16 }, ); thaw_asset(asset, ALICE); // Not enough balance. assert_eq!( decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), - UseCaseError(InsufficientBalance) + Module { index: 52, error: 0 }, ); // Not enough balance due to ED. assert_eq!( decoded::(transfer(addr.clone(), asset, BOB, amount)), - UseCaseError(InsufficientBalance) + Module { index: 52, error: 0 }, ); // Successful transfer. let bob_balance_before_mint = Assets::balance(asset, &BOB); @@ -454,7 +454,7 @@ fn transfer_works() { start_destroy_asset(asset, ALICE); assert_eq!( decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), - UseCaseError(AssetNotLive) + Module { index: 52, error: 16 }, ); }); } diff --git a/runtime/devnet/src/tests/mod.rs b/runtime/devnet/src/tests/mod.rs index c13030a3..dcb97ae1 100644 --- a/runtime/devnet/src/tests/mod.rs +++ b/runtime/devnet/src/tests/mod.rs @@ -256,7 +256,7 @@ fn dispatch_error_to_status_code_to_pop_api_error_works() { for (error, pop_api_error) in test_cases { // Show that the encoding and decoding of the PopApiError <> u32 (status code) works. let status_code = crate::extensions::convert_to_status_code(error); - let error = pop_api::error::convert_to_pop_api_error(status_code); - assert_eq!(pop_api_error, error,); + // let error = pop_api::error::convert_to_pop_api_error(status_code); + // assert_eq!(pop_api_error, error,); } } From f88dd89b8fbbfebc28126f0262fc8b5397b903d6 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Tue, 25 Jun 2024 16:58:18 +0200 Subject: [PATCH 11/76] refactor: apply comments part 2 --- pop-api/Cargo.toml | 6 +- .../{balance-transfer => }/.gitignore | 2 +- pop-api/examples/fungibles/.gitignore | 9 - pop-api/examples/fungibles/lib.rs | 2 +- pop-api/examples/nfts/.gitignore | 9 - pop-api/examples/place-spot-order/.gitignore | 9 - .../examples/read-runtime-state/.gitignore | 9 - pop-api/src/error.rs | 317 ++++++++++------- pop-api/src/lib.rs | 12 +- pop-api/src/primitives.rs | 1 - pop-api/src/v0/assets/fungibles.rs | 64 ++++ pop-api/src/v0/assets/mod.rs | 8 +- pop-api/src/v0/balances.rs | 6 +- pop-api/src/v0/cross_chain/mod.rs | 9 +- pop-api/src/v0/mod.rs | 12 +- pop-api/src/v0/nfts.rs | 318 +++++++++--------- pop-api/src/v0/state.rs | 5 +- primitives/Cargo.toml | 5 +- primitives/src/lib.rs | 25 +- primitives/src/storage_keys.rs | 10 +- runtime/devnet/Cargo.toml | 4 +- runtime/devnet/src/extensions.rs | 19 +- runtime/devnet/src/lib.rs | 4 +- runtime/devnet/src/tests/local_fungibles.rs | 57 +--- runtime/devnet/src/tests/mod.rs | 311 ++++++++--------- runtime/testnet/Cargo.toml | 2 +- runtime/testnet/src/extensions.rs | 6 +- runtime/testnet/src/lib.rs | 4 +- 28 files changed, 677 insertions(+), 568 deletions(-) rename pop-api/examples/{balance-transfer => }/.gitignore (93%) delete mode 100755 pop-api/examples/fungibles/.gitignore delete mode 100755 pop-api/examples/nfts/.gitignore delete mode 100755 pop-api/examples/place-spot-order/.gitignore delete mode 100755 pop-api/examples/read-runtime-state/.gitignore diff --git a/pop-api/Cargo.toml b/pop-api/Cargo.toml index b80cb3b0..9a868cfa 100644 --- a/pop-api/Cargo.toml +++ b/pop-api/Cargo.toml @@ -31,7 +31,7 @@ std = [ "sp-io/std", "sp-runtime/std", ] -assets = [] +assets = ["pop-primitives/assets"] balances = [] -nfts = [] -cross-chain = [] +nfts = ["pop-primitives/nfts"] +cross-chain = ["pop-primitives/cross-chain"] diff --git a/pop-api/examples/balance-transfer/.gitignore b/pop-api/examples/.gitignore similarity index 93% rename from pop-api/examples/balance-transfer/.gitignore rename to pop-api/examples/.gitignore index 8de8f877..e0caa493 100755 --- a/pop-api/examples/balance-transfer/.gitignore +++ b/pop-api/examples/.gitignore @@ -1,5 +1,5 @@ # Ignore build artifacts from the local tests sub-crate. -/target/ +/fungibles/target/ # Ignore backup files creates by cargo fmt. **/*.rs.bk diff --git a/pop-api/examples/fungibles/.gitignore b/pop-api/examples/fungibles/.gitignore deleted file mode 100755 index 8de8f877..00000000 --- a/pop-api/examples/fungibles/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Ignore build artifacts from the local tests sub-crate. -/target/ - -# Ignore backup files creates by cargo fmt. -**/*.rs.bk - -# Remove Cargo.lock when creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs index 161c7fdd..41ea1b14 100755 --- a/pop-api/examples/fungibles/lib.rs +++ b/pop-api/examples/fungibles/lib.rs @@ -8,7 +8,7 @@ use ink::prelude::vec::Vec; use pop_api::{ assets::fungibles::{self as api, FungiblesError}, - error::{PopApiError, StatusCode}, + error::{Error, StatusCode}, primitives::{AccountId as AccountId32, AssetId}, }; diff --git a/pop-api/examples/nfts/.gitignore b/pop-api/examples/nfts/.gitignore deleted file mode 100755 index 8de8f877..00000000 --- a/pop-api/examples/nfts/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Ignore build artifacts from the local tests sub-crate. -/target/ - -# Ignore backup files creates by cargo fmt. -**/*.rs.bk - -# Remove Cargo.lock when creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock diff --git a/pop-api/examples/place-spot-order/.gitignore b/pop-api/examples/place-spot-order/.gitignore deleted file mode 100755 index 8de8f877..00000000 --- a/pop-api/examples/place-spot-order/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Ignore build artifacts from the local tests sub-crate. -/target/ - -# Ignore backup files creates by cargo fmt. -**/*.rs.bk - -# Remove Cargo.lock when creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock diff --git a/pop-api/examples/read-runtime-state/.gitignore b/pop-api/examples/read-runtime-state/.gitignore deleted file mode 100755 index 8de8f877..00000000 --- a/pop-api/examples/read-runtime-state/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Ignore build artifacts from the local tests sub-crate. -/target/ - -# Ignore backup files creates by cargo fmt. -**/*.rs.bk - -# Remove Cargo.lock when creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock diff --git a/pop-api/src/error.rs b/pop-api/src/error.rs index 4d5a8fb9..b8a56f55 100644 --- a/pop-api/src/error.rs +++ b/pop-api/src/error.rs @@ -1,6 +1,7 @@ use ink::env::chain_extension::FromStatusCode; use scale::{Decode, Encode}; -use PopApiError::*; + +use Error::*; #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] @@ -30,10 +31,69 @@ impl From for StatusCode { } } +// If an unknown variant of the `DispatchError` is detected the error needs to be converted +// into the encoded value of `Error::Other`. This conversion is performed by shifting the bytes one +// position forward (discarding the last byte as it is not used) and setting the first byte to the +// encoded value of `Other` (0u8). This ensures the error is correctly categorized as an `Other` +// variant which provides all the necessary information to debug which error occurred in the runtime. +// +// Byte layout explanation: +// - Byte 0: index of the variant within `Error` +// - Byte 1: +// - Must be zero for `UNIT_ERRORS`. +// - Represents the nested error in `SINGLE_NESTED_ERRORS`. +// - Represents the first level of nesting in `DOUBLE_NESTED_ERRORS`. +// - Byte 2: +// - Represents the second level of nesting in `DOUBLE_NESTED_ERRORS`. +// - Byte 3: +// - Unused or represents further nested information. +// +// This mechanism ensures backward compatibility by correctly categorizing any unknown errors +// into the `Other` variant, thus preventing issues caused by breaking changes. +fn convert_unknown_errors(encoded_error: &mut [u8; 4]) { + let all_errors = [ + UNIT_ERRORS.as_slice(), + SINGLE_NESTED_ERRORS.as_slice(), + DOUBLE_NESTED_ERRORS.as_slice(), + // `DecodingFailed`. + &[255u8], + ] + .concat(); + // Unknown errors, i.e. an encoded value where the first byte is non-zero (indicating a variant + // in `Error`) but unknown. + if !all_errors.contains(&encoded_error[0]) { + encoded_error[..].rotate_right(1); + encoded_error[0] = 0u8; + } + convert_unknown_nested_errors(encoded_error); +} + +// If an unknown nested variant of the `DispatchError` is detected (i.e. when any of the subsequent +// bytes are non-zero). +fn convert_unknown_nested_errors(encoded_error: &mut [u8; 4]) { + // Converts single nested errors that are known to the Pop API as unit errors into `Other`. + if UNIT_ERRORS.contains(&encoded_error[0]) && encoded_error[1..].iter().any(|x| *x != 0u8) { + encoded_error[..].rotate_right(1); + encoded_error[0] = 0u8; + // Converts double nested errors that are known to the Pop API as single nested errors into + // `Other`. + } else if SINGLE_NESTED_ERRORS.contains(&encoded_error[0]) + && encoded_error[2..].iter().any(|x| *x != 0u8) + { + encoded_error[..].rotate_right(1); + encoded_error[0] = 0u8; + } else if DOUBLE_NESTED_ERRORS.contains(&encoded_error[0]) + && encoded_error[3..].iter().any(|x| *x != 0u8) + { + encoded_error[..].rotate_right(1); + encoded_error[0] = 0u8; + } +} + #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] #[repr(u8)] -pub enum PopApiError { +pub enum Error { /// Some unknown error occurred. Go to the Pop API docs section `Pop API error`. Other { // Index within the `DispatchError` @@ -76,8 +136,34 @@ pub enum PopApiError { DecodingFailed = 255, } -impl From for StatusCode { - fn from(value: PopApiError) -> Self { +// Unit `Error` variants. +// (variant: index): +// - CannotLookup: 1, +// - BadOrigin: 2, +// - ConsumerRemaining: 4, +// - NoProviders: 5, +// - TooManyConsumers: 6, +// - Exhausted: 10, +// - Corruption: 11, +// - Unavailable: 12, +// - RootNotAllowed: 13, +// - DecodingFailed: 255, +const UNIT_ERRORS: [u8; 10] = [1, 2, 4, 5, 6, 10, 11, 12, 13, 255]; + +// Single nested `Error` variants. +// (variant: index): +// - Token: 7, +// - Arithmetic: 8, +// - Transaction: 9, +const SINGLE_NESTED_ERRORS: [u8; 3] = [7, 8, 9]; + +// Double nested `Error` variants +// (variant: index): +// - Module: 3, +const DOUBLE_NESTED_ERRORS: [u8; 1] = [3]; + +impl From for StatusCode { + fn from(value: Error) -> Self { let mut encoded_error = value.encode(); // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). encoded_error.resize(4, 0); @@ -87,71 +173,11 @@ impl From for StatusCode { } } -impl From for PopApiError { - // `pub` because it is used in `runtime/devnet/src/extensions/tests/mod.rs`'s test: - // `dispatch_error_to_status_code_to_pop_api_error_works` - // - // This function converts a given `status_code` (u32) into a `PopApiError`. +impl From for Error { fn from(value: StatusCode) -> Self { let encoded: [u8; 4] = value.0.to_le_bytes(); - PopApiError::decode(&mut &encoded[..]).unwrap_or(DecodingFailed) - } -} - -// If an unknown nested variant of the `DispatchError` is detected (i.e., any of the subsequent -// bytes are non-zero, indicating a breaking change in the `DispatchError`), the error needs to be -// converted into the encoded value of `PopApiError::Other`. This conversion is performed by -// shifting the bytes one position forward (discarding the last byte as it is not used) and setting -// the first byte to the encoded value of `Other` (0u8). This ensures the error is correctly -// categorized as an `Other` variant. -// -// Byte layout explanation: -// - Byte 0: PopApiError -// - Byte 1: -// - Must be zero for `UNIT_ERRORS`. -// - Represents the nested error in `SINGLE_NESTED_ERRORS`. -// - Represents the first level of nesting in `DOUBLE_NESTED_ERRORS`. -// - Byte 2: -// - Represents the second level of nesting in `DOUBLE_NESTED_ERRORS`. -// - Byte 3: -// - Unused or represents further nested information. -// -// This mechanism ensures backward compatibility by correctly categorizing any unknown nested errors -// into the `Other` variant, thus preventing issues caused by new or unexpected error formats. -pub(crate) fn convert_unknown_nested_errors(encoded_error: &mut [u8; 4]) { - // Converts single nested errors that are known to the Pop API as unit errors into `Other`. - if UNIT_ERRORS.contains(&encoded_error[0]) && encoded_error[1..].iter().any(|x| *x != 0u8) { - encoded_error[..].rotate_right(1); - encoded_error[0] = 0u8; - // Converts double nested errors that are known to the Pop API as single nested errors into - // `Other`. - } else if SINGLE_NESTED_ERRORS.contains(&encoded_error[0]) - && encoded_error[2..].iter().any(|x| *x != 0u8) - { - encoded_error[..].rotate_right(1); - encoded_error[0] = 0u8; - } else if DOUBLE_NESTED_ERRORS.contains(&encoded_error[0]) - && encoded_error[3..].iter().any(|x| *x != 0u8) - { - encoded_error[..].rotate_right(1); - encoded_error[0] = 0u8; - } -} - -pub(crate) fn convert_unknown_errors(encoded_error: &mut [u8; 4]) { - let all_errors = [ - UNIT_ERRORS.as_slice(), - SINGLE_NESTED_ERRORS.as_slice(), - DOUBLE_NESTED_ERRORS.as_slice(), - // `DecodingFailed`. - &[255u8], - ] - .concat(); - if !all_errors.contains(&encoded_error[0]) { - encoded_error[..].rotate_right(1); - encoded_error[0] = 0u8; + Error::decode(&mut &encoded[..]).unwrap_or(DecodingFailed) } - convert_unknown_nested_errors(encoded_error); } #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] @@ -200,26 +226,6 @@ pub enum TransactionalError { NoLayer, } -// Unit `DispatchError` variants (variant: index): -// - CannotLookup: 1, -// - BadOrigin: 2, -// - ConsumerRemaining: 4, -// - NoProviders: 5, -// - TooManyConsumers: 6, -// - Exhausted: 10, -// - Corruption: 11, -// - Unavailable: 12, -// - RootNotAllowed: 13, -const UNIT_ERRORS: [u8; 9] = [1, 2, 4, 5, 6, 10, 11, 12, 13]; - -// Single nested `DispatchError` variants (variant: index): -// - Token: 3, -// - Arithmetic: 8, -// - Transaction: 9, -const SINGLE_NESTED_ERRORS: [u8; 3] = [7, 8, 9]; - -const DOUBLE_NESTED_ERRORS: [u8; 1] = [3]; - #[cfg(test)] mod tests { use super::*; @@ -231,20 +237,30 @@ mod tests { assert_eq!(u32::MAX.encode().len(), 4); } - // Decodes into `StatusCode(u32)` and converts it into the `PopApiError`. - fn into_pop_api_error(encoded_error: [u8; 4]) -> PopApiError { - let status_code = - StatusCode::from_status_code(u32::decode(&mut &encoded_error[..]).unwrap()) - .unwrap_err(); + // Decodes 4 bytes into a `u32` and converts it into `StatusCode`. + fn into_status_code(encoded_error: [u8; 4]) -> StatusCode { + let decoded_u32 = u32::decode(&mut &encoded_error[..]).unwrap(); + StatusCode::from_status_code(decoded_u32).unwrap_err() + } + + // Decodes 4 bytes into a `u32` and converts it into `Error`. + fn into_error(encoded_error: [u8; 4]) -> Error { + let decoded_u32 = u32::decode(&mut &encoded_error[..]).unwrap(); + let status_code = StatusCode::from_status_code(decoded_u32).unwrap_err(); status_code.into() } - // Tests for the `From` implementation for `PopApiError`. + // Tests the `From` implementation for `Error`. // - // If the encoded value indicates a nested `PopApiError` which is not handled by the Pop API - // version, the encoded value is converted into `PopApiError::Other`. + // Unit variants: + // If the encoded value indicates a nested `Error` which is known by the Pop API version as a + // unit variant, the encoded value is converted into `Error::Other`. + // + // Example: the error `BadOrigin` (encoded: `[2, 0, 0, 0]`) with a non-zero value for one + // of the bytes [1..4]: `[2, 0, 1, 0]` is converted into `[0, 2, 0, 1]`. This is decoded to + // `Error::Other { dispatch_error: 2, index: 0, error: 1 }`. #[test] - fn test_unit_pop_api_error_variants() { + fn unit_error_variants() { let errors = vec![ CannotLookup, BadOrigin, @@ -255,73 +271,134 @@ mod tests { Corruption, Unavailable, RootNotAllowed, + DecodingFailed, ]; + // Four scenarios, 2 tests each: + // 1. Compare a `StatusCode`, which is converted from an encoded value, with a `StatusCode` + // converted from an `Error`. + // 2. Compare an `Error, which is converted from an encoded value, with the expected `Error`. for (i, &error_code) in UNIT_ERRORS.iter().enumerate() { - assert_eq!(into_pop_api_error([error_code, 0, 0, 0]), errors[i]); + // No nesting and unit variant correctly returned. + assert_eq!(into_status_code([error_code, 0, 0, 0]), errors[i].into()); + assert_eq!(into_error([error_code, 0, 0, 0]), errors[i]); + // Unexpected second byte nested. + assert_eq!( + into_status_code([error_code, 1, 0, 0]), + (Other { dispatch_error_index: error_code, error_index: 1, error: 0 }).into(), + ); assert_eq!( - into_pop_api_error([error_code, 1, 0, 0]), + into_error([error_code, 1, 0, 0]), Other { dispatch_error_index: error_code, error_index: 1, error: 0 }, ); + // Unexpected third byte nested. + assert_eq!( + into_status_code([error_code, 1, 1, 0]), + (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }).into(), + ); assert_eq!( - into_pop_api_error([error_code, 1, 1, 0]), + into_error([error_code, 1, 1, 0]), Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, ); + // Unexpected fourth byte nested. assert_eq!( - into_pop_api_error([error_code, 1, 1, 1]), + into_status_code([error_code, 1, 1, 1]), + (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }).into(), + ); + assert_eq!( + into_error([error_code, 1, 1, 1]), Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, ); } } + // Single nested variants: + // If the encoded value indicates a double nested `Error` which is known by the Pop API version + // as a single nested variant, the encoded value is converted into `Error::Other`. + // + // Example: the error `Arithmetic(Overflow)` (encoded: `[8, 1, 0, 0]`) with a non-zero + // value for one of the bytes [2..4]: `[8, 1, 1, 0]` is converted into `[0, 8, 1, 1]`. This is + // decoded to `Error::Other { dispatch_error: 8, index: 1, error: 1 }`. #[test] - fn test_single_nested_pop_api_error_variants() { + fn single_nested_error_variants() { let errors = vec![ [Token(FundsUnavailable), Token(OnlyProvider)], [Arithmetic(Underflow), Arithmetic(Overflow)], [Transactional(LimitReached), Transactional(NoLayer)], ]; + // Four scenarios, 2 tests each: + // 1. Compare a `StatusCode`, which is converted from an encoded value, with a `StatusCode` + // converted from an `Error`. + // 2. Compare an `Error, which is converted from an encoded value, with the expected `Error`. for (i, &error_code) in SINGLE_NESTED_ERRORS.iter().enumerate() { - assert_eq!(into_pop_api_error([error_code, 0, 0, 0]), errors[i][0]); - assert_eq!(into_pop_api_error([error_code, 1, 0, 0]), errors[i][1]); + // No nesting and unit variant correctly returned. + assert_eq!(into_status_code([error_code, 0, 0, 0]), errors[i][0].into()); + assert_eq!(into_error([error_code, 0, 0, 0]), errors[i][0]); + // Allowed single nesting variant correctly returned. + assert_eq!(into_status_code([error_code, 1, 0, 0]), errors[i][1].into()); + assert_eq!(into_error([error_code, 1, 0, 0]), errors[i][1]); + // Unexpected third byte nested. + assert_eq!( + into_status_code([error_code, 1, 1, 0]), + (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }).into(), + ); assert_eq!( - into_pop_api_error([error_code, 1, 1, 0]), + into_error([error_code, 1, 1, 0]), Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, ); + // Unexpected fourth byte nested. + assert_eq!( + into_status_code([error_code, 1, 1, 1]), + (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }).into(), + ); assert_eq!( - into_pop_api_error([error_code, 1, 1, 1]), + into_error([error_code, 1, 1, 1]), Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, ); } } + // Double nested variants: + // If the encoded value indicates a triple nested `Error` which is known by the Pop API version + // as a double nested variant, the encoded value is converted into `Error::Other`. + // + // Example: the error `Module { index: 10, error 5 }` (encoded: `[3, 10, 5, 0]`) with a non-zero + // value for the last byte: `[3, 10, 5, 3]` is converted into `[0, 3, 10, 5]`. This is + // decoded to `Error::Other { dispatch_error: 3, index: 10, error: 5 }`. #[test] - fn test_double_nested_pop_api_error_variants() { - assert_eq!(into_pop_api_error([3, 0, 0, 0]), Module { index: 0, error: 0 }); - assert_eq!(into_pop_api_error([3, 1, 0, 0]), Module { index: 1, error: 0 }); - assert_eq!(into_pop_api_error([3, 1, 1, 0]), Module { index: 1, error: 1 }); - // TODO: doesn't make sense. + fn double_nested_error_variants() { + // Four scenarios, 2 tests each: + // 1. Compare a `StatusCode`, which is converted from an encoded value, with a `StatusCode` + // converted from an `Error`. + // 2. Compare an `Error, which is converted from an encoded value, with the expected `Error`. + // + // No nesting and unit variant correctly returned. + assert_eq!(into_status_code([3, 0, 0, 0]), (Module { index: 0, error: 0 }).into()); + assert_eq!(into_error([3, 0, 0, 0]), Module { index: 0, error: 0 }); + // Allowed single nesting and variant correctly returned. + assert_eq!(into_status_code([3, 1, 0, 0]), (Module { index: 1, error: 0 }).into()); + assert_eq!(into_error([3, 1, 0, 0]), Module { index: 1, error: 0 }); + // Allowed double nesting and variant correctly returned. + assert_eq!(into_status_code([3, 1, 1, 0]), (Module { index: 1, error: 1 }).into()); + assert_eq!(into_error([3, 1, 1, 0]), Module { index: 1, error: 1 }); + // Unexpected fourth byte nested. assert_eq!( - into_pop_api_error([3, 1, 1, 1]), + into_status_code([3, 1, 1, 1]), + (Other { dispatch_error_index: 3, error_index: 1, error: 1 }).into(), + ); + assert_eq!( + into_error([3, 1, 1, 1]), Other { dispatch_error_index: 3, error_index: 1, error: 1 }, ); } - #[test] - fn test_decoding_failed() { - assert_eq!(into_pop_api_error([255, 0, 0, 0]), DecodingFailed); - assert_eq!(into_pop_api_error([255, 255, 0, 0]), DecodingFailed); - assert_eq!(into_pop_api_error([255, 255, 255, 0]), DecodingFailed); - assert_eq!(into_pop_api_error([255, 255, 255, 255]), DecodingFailed); - } - #[test] fn test_random_encoded_values() { assert_eq!( - into_pop_api_error([100, 100, 100, 100]), + into_error([100, 100, 100, 100]), Other { dispatch_error_index: 100, error_index: 100, error: 100 } ); assert_eq!( - into_pop_api_error([200, 200, 200, 200]), + into_error([200, 200, 200, 200]), Other { dispatch_error_index: 200, error_index: 200, error: 200 } ); } diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 90c81998..3e9ead7f 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -3,7 +3,7 @@ use ink::{prelude::vec::Vec, ChainExtensionInstance}; pub use sp_runtime::{BoundedVec, MultiAddress, MultiSignature}; -use crate::error::{PopApiError, StatusCode}; +use crate::error::{Error, StatusCode}; use primitives::{storage_keys::*, AccountId as AccountId32}; #[cfg(feature = "assets")] pub use v0::assets; @@ -21,7 +21,11 @@ pub mod v0; type AccountId = AccountId32; // TODO: do the same as the AccountId above and check expanded macro code. -type Balance = ::Balance; +// type Balance = ::Balance; +type Balance = u128; +#[cfg(any(feature = "nfts", feature = "cross-chain"))] +type BlockNumber = ::BlockNumber; + pub type Result = core::result::Result; #[derive(Debug, Clone, PartialEq, Eq)] @@ -56,7 +60,7 @@ pub trait PopApi { #[cfg(feature = "cross-chain")] #[ink(function = 2)] #[allow(private_interfaces)] - fn send_xcm(xcm: CrossChainMessage) -> Result<()>; + fn send_xcm(xcm: primitives::cross_chain::CrossChainMessage) -> Result<()>; } fn dispatch(call: RuntimeCall) -> Result<()> { @@ -72,7 +76,7 @@ fn read_state(key: RuntimeStateKeys) -> Result> { } #[cfg(feature = "cross-chain")] -fn send_xcm(xcm: CrossChainMessage) -> Result<()> { +fn send_xcm(xcm: primitives::cross_chain::CrossChainMessage) -> Result<()> { <::ChainExtension as ChainExtensionInstance>::instantiate( ) .send_xcm(xcm) diff --git a/pop-api/src/primitives.rs b/pop-api/src/primitives.rs index e8098f69..e174a111 100644 --- a/pop-api/src/primitives.rs +++ b/pop-api/src/primitives.rs @@ -1,2 +1 @@ pub use pop_primitives::*; -pub use sp_runtime::{BoundedVec, MultiAddress}; diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index b1bb86a7..eaf1c6f5 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -356,3 +356,67 @@ impl From for FungiblesError { } } } + +#[cfg(test)] +mod tests { + use super::FungiblesError; + use crate::error::{ + ArithmeticError::*, + Error::{self, *}, + StatusCode, + TokenError::*, + TransactionalError::*, + }; + + fn into_fungibles_error(error: Error) -> FungiblesError { + let status_code: StatusCode = error.into(); + status_code.into() + } + + #[test] + fn conversion_status_code_into_fungibles_error_works() { + let errors = vec![ + Other { dispatch_error_index: 5, error_index: 5, error: 1 }, + CannotLookup, + BadOrigin, + Module { index: 2, error: 5 }, + ConsumerRemaining, + NoProviders, + TooManyConsumers, + Token(OnlyProvider), + Arithmetic(Overflow), + Transactional(NoLayer), + Exhausted, + Corruption, + Unavailable, + RootNotAllowed, + DecodingFailed, + ]; + for error in errors { + let status_code: StatusCode = error.into(); + let fungibles_error: FungiblesError = status_code.into(); + assert_eq!(fungibles_error, FungiblesError::Other(status_code)) + } + + assert_eq!(into_fungibles_error(Module { index: 10, error: 2 }), FungiblesError::NoBalance); + assert_eq!(into_fungibles_error(Module { index: 52, error: 0 }), FungiblesError::NoAccount); + assert_eq!( + into_fungibles_error(Module { index: 52, error: 1 }), + FungiblesError::NoPermission + ); + assert_eq!(into_fungibles_error(Module { index: 52, error: 2 }), FungiblesError::Unknown); + assert_eq!(into_fungibles_error(Module { index: 52, error: 3 }), FungiblesError::InUse); + assert_eq!( + into_fungibles_error(Module { index: 52, error: 5 }), + FungiblesError::MinBalanceZero + ); + assert_eq!( + into_fungibles_error(Module { index: 52, error: 7 }), + FungiblesError::InsufficientAllowance + ); + assert_eq!( + into_fungibles_error(Module { index: 52, error: 10 }), + FungiblesError::AssetNotLive + ); + } +} diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index 4ce68219..d67d30bd 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1,8 +1,8 @@ #![allow(dead_code)] -use crate::{Balance, RuntimeCall, *}; +use crate::{Balance, MultiAddress, RuntimeCall, *}; use ink::prelude::vec::Vec; -use primitives::{AssetId, MultiAddress}; +use primitives::AssetId; use scale::{Compact, Encode}; pub mod fungibles; @@ -13,7 +13,7 @@ type Result = core::result::Result; /// 1. Dispatchables /// 2. Read state functions /// -/// 1. Dispatchables within pallet assets (TrustBackedAssets instance) that can be used via the pop api on Pop Network: +/// 1. Dispatchables within pallet assets (TrustBackedAssets instance): /// - create /// - start_destroy /// - destroy_accounts @@ -464,7 +464,7 @@ pub enum AssetsError { } impl TryFrom for AssetsError { - type Error = PopApiError; + type Error = Error; fn try_from(status_code: u32) -> core::result::Result { use AssetsError::*; diff --git a/pop-api/src/v0/balances.rs b/pop-api/src/v0/balances.rs index ae2709e7..e14e6e32 100644 --- a/pop-api/src/v0/balances.rs +++ b/pop-api/src/v0/balances.rs @@ -1,6 +1,4 @@ -use crate::{ - dispatch, primitives::MultiAddress, v0::RuntimeCall, AccountId, PopApiError, StatusCode, -}; +use crate::{dispatch, primitives::MultiAddress, v0::RuntimeCall, AccountId, Error, StatusCode}; type Result = core::result::Result; @@ -57,7 +55,7 @@ pub enum BalancesError { } impl TryFrom for BalancesError { - type Error = PopApiError; + type Error = Error; fn try_from(status_code: u32) -> core::result::Result { use BalancesError::*; diff --git a/pop-api/src/v0/cross_chain/mod.rs b/pop-api/src/v0/cross_chain/mod.rs index 583447b1..5a0dda6c 100644 --- a/pop-api/src/v0/cross_chain/mod.rs +++ b/pop-api/src/v0/cross_chain/mod.rs @@ -1,11 +1,8 @@ -pub mod coretime; - -use crate::StatusCode; +use crate::{BlockNumber, ParachainSystemKeys, Result, RuntimeStateKeys}; -type Result = core::result::Result; -type BlockNumber = ::BlockNumber; +pub mod coretime; -pub fn relay_chain_block_number() -> std::result::Result { +pub fn relay_chain_block_number() -> Result { crate::v0::state::read(RuntimeStateKeys::ParachainSystem( ParachainSystemKeys::LastRelayChainBlockNumber, )) diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index 41574cfb..20dc6476 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -10,12 +10,12 @@ pub mod state; #[derive(scale::Encode)] pub(crate) enum RuntimeCall { - // #[codec(index = 10)] - // #[cfg(feature = "balances")] - // Balances(balances::BalancesCall), - // #[codec(index = 50)] - // #[cfg(feature = "nfts")] - // Nfts(nfts::NftCalls), + #[codec(index = 10)] + #[cfg(feature = "balances")] + Balances(balances::BalancesCall), + #[codec(index = 50)] + #[cfg(feature = "nfts")] + Nfts(nfts::NftCalls), #[codec(index = 52)] #[cfg(feature = "assets")] Assets(assets::AssetsCall), diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index 1e4406e5..29219c66 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -1,9 +1,15 @@ -use super::RuntimeCall; -use crate::*; use ink::prelude::vec::Vec; -use primitives::{ApprovalsLimit, BoundedBTreeMap, KeyLimit, MultiAddress}; -pub use primitives::{CollectionId, ItemId}; use scale::Encode; + +use crate::{ + dispatch, + primitives::{ + nfts::{ApprovalsLimit, CollectionId, ItemId, KeyLimit}, + BoundedBTreeMap, + }, + state, AccountId, Balance, BlockNumber, BoundedVec, MultiAddress, NftsKeys, RuntimeCall, + RuntimeStateKeys, StatusCode, +}; pub use types::*; type Result = core::result::Result; @@ -515,163 +521,163 @@ pub(crate) enum NftCalls { receive_item: ItemId, }, } - -#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum Error { - /// The signing account has no permission to do the operation. - NoPermission, - /// The given item ID is unknown. - UnknownCollection, - /// The item ID has already been used for an item. - AlreadyExists, - /// The approval had a deadline that expired, so the approval isn't valid anymore. - ApprovalExpired, - /// The owner turned out to be different to what was expected. - WrongOwner, - /// The witness data given does not match the current state of the chain. - BadWitness, - /// Collection ID is already taken. - CollectionIdInUse, - /// Items within that collection are non-transferable. - ItemsNonTransferable, - /// The provided account is not a delegate. - NotDelegate, - /// The delegate turned out to be different to what was expected. - WrongDelegate, - /// No approval exists that would allow the transfer. - Unapproved, - /// The named owner has not signed ownership acceptance of the collection. - Unaccepted, - /// The item is locked (non-transferable). - ItemLocked, - /// Item's attributes are locked. - LockedItemAttributes, - /// Collection's attributes are locked. - LockedCollectionAttributes, - /// Item's metadata is locked. - LockedItemMetadata, - /// Collection's metadata is locked. - LockedCollectionMetadata, - /// All items have been minted. - MaxSupplyReached, - /// The max supply is locked and can't be changed. - MaxSupplyLocked, - /// The provided max supply is less than the number of items a collection already has. - MaxSupplyTooSmall, - /// The given item ID is unknown. - UnknownItem, - /// Swap doesn't exist. - UnknownSwap, - /// The given item has no metadata set. - MetadataNotFound, - /// The provided attribute can't be found. - AttributeNotFound, - /// Item is not for sale. - NotForSale, - /// The provided bid is too low. - BidTooLow, - /// The item has reached its approval limit. - ReachedApprovalLimit, - /// The deadline has already expired. - DeadlineExpired, - /// The duration provided should be less than or equal to `MaxDeadlineDuration`. - WrongDuration, - /// The method is disabled by system settings. - MethodDisabled, - /// The provided setting can't be set. - WrongSetting, - /// Item's config already exists and should be equal to the provided one. - InconsistentItemConfig, - /// Config for a collection or an item can't be found. - NoConfig, - /// Some roles were not cleared. - RolesNotCleared, - /// Mint has not started yet. - MintNotStarted, - /// Mint has already ended. - MintEnded, - /// The provided Item was already used for claiming. - AlreadyClaimed, - /// The provided data is incorrect. - IncorrectData, - /// The extrinsic was sent by the wrong origin. - WrongOrigin, - /// The provided signature is incorrect. - WrongSignature, - /// The provided metadata might be too long. - IncorrectMetadata, - /// Can't set more attributes per one call. - MaxAttributesLimitReached, - /// The provided namespace isn't supported in this call. - WrongNamespace, - /// Can't delete non-empty collections. - CollectionNotEmpty, - /// The witness data should be provided. - WitnessRequired, -} - -impl TryFrom for Error { - type Error = Error; - - fn try_from(status_code: u32) -> core::result::Result { - use Error::*; - match status_code { - 0 => Ok(NoPermission), - 1 => Ok(UnknownCollection), - 2 => Ok(AlreadyExists), - 3 => Ok(ApprovalExpired), - 4 => Ok(WrongOwner), - 5 => Ok(BadWitness), - 6 => Ok(CollectionIdInUse), - 7 => Ok(ItemsNonTransferable), - 8 => Ok(NotDelegate), - 9 => Ok(WrongDelegate), - 10 => Ok(Unapproved), - 11 => Ok(Unaccepted), - 12 => Ok(ItemLocked), - 13 => Ok(LockedItemAttributes), - 14 => Ok(LockedCollectionAttributes), - 15 => Ok(LockedItemMetadata), - 16 => Ok(LockedCollectionMetadata), - 17 => Ok(MaxSupplyReached), - 18 => Ok(MaxSupplyLocked), - 19 => Ok(MaxSupplyTooSmall), - 20 => Ok(UnknownItem), - 21 => Ok(UnknownSwap), - 22 => Ok(MetadataNotFound), - 23 => Ok(AttributeNotFound), - 24 => Ok(NotForSale), - 25 => Ok(BidTooLow), - 26 => Ok(ReachedApprovalLimit), - 27 => Ok(DeadlineExpired), - 28 => Ok(WrongDuration), - 29 => Ok(MethodDisabled), - 30 => Ok(WrongSetting), - 31 => Ok(InconsistentItemConfig), - 32 => Ok(NoConfig), - 33 => Ok(RolesNotCleared), - 34 => Ok(MintNotStarted), - 35 => Ok(MintEnded), - 36 => Ok(AlreadyClaimed), - 37 => Ok(IncorrectData), - 38 => Ok(WrongOrigin), - 39 => Ok(WrongSignature), - 40 => Ok(IncorrectMetadata), - 41 => Ok(MaxAttributesLimitReached), - 42 => Ok(WrongNamespace), - 43 => Ok(CollectionNotEmpty), - 44 => Ok(WitnessRequired), - _ => todo!(), - } - } -} +// +// #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] +// #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +// pub enum Error { +// /// The signing account has no permission to do the operation. +// NoPermission, +// /// The given item ID is unknown. +// UnknownCollection, +// /// The item ID has already been used for an item. +// AlreadyExists, +// /// The approval had a deadline that expired, so the approval isn't valid anymore. +// ApprovalExpired, +// /// The owner turned out to be different to what was expected. +// WrongOwner, +// /// The witness data given does not match the current state of the chain. +// BadWitness, +// /// Collection ID is already taken. +// CollectionIdInUse, +// /// Items within that collection are non-transferable. +// ItemsNonTransferable, +// /// The provided account is not a delegate. +// NotDelegate, +// /// The delegate turned out to be different to what was expected. +// WrongDelegate, +// /// No approval exists that would allow the transfer. +// Unapproved, +// /// The named owner has not signed ownership acceptance of the collection. +// Unaccepted, +// /// The item is locked (non-transferable). +// ItemLocked, +// /// Item's attributes are locked. +// LockedItemAttributes, +// /// Collection's attributes are locked. +// LockedCollectionAttributes, +// /// Item's metadata is locked. +// LockedItemMetadata, +// /// Collection's metadata is locked. +// LockedCollectionMetadata, +// /// All items have been minted. +// MaxSupplyReached, +// /// The max supply is locked and can't be changed. +// MaxSupplyLocked, +// /// The provided max supply is less than the number of items a collection already has. +// MaxSupplyTooSmall, +// /// The given item ID is unknown. +// UnknownItem, +// /// Swap doesn't exist. +// UnknownSwap, +// /// The given item has no metadata set. +// MetadataNotFound, +// /// The provided attribute can't be found. +// AttributeNotFound, +// /// Item is not for sale. +// NotForSale, +// /// The provided bid is too low. +// BidTooLow, +// /// The item has reached its approval limit. +// ReachedApprovalLimit, +// /// The deadline has already expired. +// DeadlineExpired, +// /// The duration provided should be less than or equal to `MaxDeadlineDuration`. +// WrongDuration, +// /// The method is disabled by system settings. +// MethodDisabled, +// /// The provided setting can't be set. +// WrongSetting, +// /// Item's config already exists and should be equal to the provided one. +// InconsistentItemConfig, +// /// Config for a collection or an item can't be found. +// NoConfig, +// /// Some roles were not cleared. +// RolesNotCleared, +// /// Mint has not started yet. +// MintNotStarted, +// /// Mint has already ended. +// MintEnded, +// /// The provided Item was already used for claiming. +// AlreadyClaimed, +// /// The provided data is incorrect. +// IncorrectData, +// /// The extrinsic was sent by the wrong origin. +// WrongOrigin, +// /// The provided signature is incorrect. +// WrongSignature, +// /// The provided metadata might be too long. +// IncorrectMetadata, +// /// Can't set more attributes per one call. +// MaxAttributesLimitReached, +// /// The provided namespace isn't supported in this call. +// WrongNamespace, +// /// Can't delete non-empty collections. +// CollectionNotEmpty, +// /// The witness data should be provided. +// WitnessRequired, +// } +// +// impl TryFrom for Error { +// type Error = Error; +// +// fn try_from(status_code: u32) -> core::result::Result { +// use Error::*; +// match status_code { +// 0 => Ok(NoPermission), +// 1 => Ok(UnknownCollection), +// 2 => Ok(AlreadyExists), +// 3 => Ok(ApprovalExpired), +// 4 => Ok(WrongOwner), +// 5 => Ok(BadWitness), +// 6 => Ok(CollectionIdInUse), +// 7 => Ok(ItemsNonTransferable), +// 8 => Ok(NotDelegate), +// 9 => Ok(WrongDelegate), +// 10 => Ok(Unapproved), +// 11 => Ok(Unaccepted), +// 12 => Ok(ItemLocked), +// 13 => Ok(LockedItemAttributes), +// 14 => Ok(LockedCollectionAttributes), +// 15 => Ok(LockedItemMetadata), +// 16 => Ok(LockedCollectionMetadata), +// 17 => Ok(MaxSupplyReached), +// 18 => Ok(MaxSupplyLocked), +// 19 => Ok(MaxSupplyTooSmall), +// 20 => Ok(UnknownItem), +// 21 => Ok(UnknownSwap), +// 22 => Ok(MetadataNotFound), +// 23 => Ok(AttributeNotFound), +// 24 => Ok(NotForSale), +// 25 => Ok(BidTooLow), +// 26 => Ok(ReachedApprovalLimit), +// 27 => Ok(DeadlineExpired), +// 28 => Ok(WrongDuration), +// 29 => Ok(MethodDisabled), +// 30 => Ok(WrongSetting), +// 31 => Ok(InconsistentItemConfig), +// 32 => Ok(NoConfig), +// 33 => Ok(RolesNotCleared), +// 34 => Ok(MintNotStarted), +// 35 => Ok(MintEnded), +// 36 => Ok(AlreadyClaimed), +// 37 => Ok(IncorrectData), +// 38 => Ok(WrongOrigin), +// 39 => Ok(WrongSignature), +// 40 => Ok(IncorrectMetadata), +// 41 => Ok(MaxAttributesLimitReached), +// 42 => Ok(WrongNamespace), +// 43 => Ok(CollectionNotEmpty), +// 44 => Ok(WitnessRequired), +// _ => todo!(), +// } +// } +// } // Local implementations of pallet-nfts types mod types { use super::*; use crate::{ - primitives::{CollectionId, ItemId}, + primitives::nfts::{CollectionId, ItemId}, Balance, BlockNumber, }; pub use enumflags2::{bitflags, BitFlags}; diff --git a/pop-api/src/v0/state.rs b/pop-api/src/v0/state.rs index 1aca01cf..e3d38129 100644 --- a/pop-api/src/v0/state.rs +++ b/pop-api/src/v0/state.rs @@ -1,7 +1,6 @@ -use crate::{primitives::storage_keys::RuntimeStateKeys, read_state, PopApiError}; +use crate::{primitives::storage_keys::RuntimeStateKeys, read_state, Error}; use scale::Decode; pub fn read(key: RuntimeStateKeys) -> crate::Result { - read_state(key) - .and_then(|v| T::decode(&mut &v[..]).map_err(|_e| PopApiError::DecodingFailed.into())) + read_state(key).and_then(|v| T::decode(&mut &v[..]).map_err(|_e| Error::DecodingFailed.into())) } diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml index e7237b51..6ba3fea7 100644 --- a/primitives/Cargo.toml +++ b/primitives/Cargo.toml @@ -22,4 +22,7 @@ std = [ "scale-info/std", ] devnet = [] -testnet = [] \ No newline at end of file +testnet = [] +assets = [] +cross-chain = [] +nfts = [] diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 1e008c31..3b91f713 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -1,10 +1,11 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use bounded_collections::{BoundedBTreeMap, BoundedBTreeSet, BoundedVec, ConstU32}; +pub use bounded_collections::{BoundedBTreeMap, BoundedBTreeSet, BoundedVec}; use scale::{Decode, Encode, MaxEncodedLen}; #[cfg(feature = "std")] use {scale_decode::DecodeAsType, scale_encode::EncodeAsType, scale_info::TypeInfo}; +#[cfg(feature = "cross-chain")] pub mod cross_chain; pub mod storage_keys; @@ -14,11 +15,17 @@ pub struct AccountId(pub [u8; 32]); // Identifier for the class of asset. pub type AssetId = u32; -// Id used for identifying non-fungible collections. -pub type CollectionId = u32; -// Id used for identifying non-fungible items. -pub type ItemId = u32; -/// The maximum length of an attribute key. -pub type KeyLimit = ConstU32<64>; -/// The maximum approvals an item could have. -pub type ApprovalsLimit = ConstU32<20>; + +#[cfg(feature = "nfts")] +pub mod nfts { + use bounded_collections::ConstU32; + + // Id used for identifying non-fungible collections. + pub type CollectionId = u32; + // Id used for identifying non-fungible items. + pub type ItemId = u32; + /// The maximum length of an attribute key. + pub type KeyLimit = ConstU32<64>; + /// The maximum approvals an item could have. + pub type ApprovalsLimit = ConstU32<20>; +} diff --git a/primitives/src/storage_keys.rs b/primitives/src/storage_keys.rs index fc7234aa..4be95986 100644 --- a/primitives/src/storage_keys.rs +++ b/primitives/src/storage_keys.rs @@ -1,13 +1,18 @@ +#[cfg(feature = "nfts")] +use super::nfts::*; use super::*; #[derive(Encode, Decode, Debug, MaxEncodedLen)] pub enum RuntimeStateKeys { + #[cfg(feature = "nfts")] Nfts(NftsKeys), + #[cfg(feature = "cross-chain")] ParachainSystem(ParachainSystemKeys), - #[cfg(feature = "devnet")] + #[cfg(feature = "assets")] Assets(AssetsKeys), } +#[cfg(feature = "cross-chain")] #[derive(Encode, Decode, Debug, MaxEncodedLen)] pub enum ParachainSystemKeys { /// Get the last relay chain block number seen by the parachain. @@ -15,6 +20,7 @@ pub enum ParachainSystemKeys { } // https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/nfts/src/impl_nonfungibles.rs +#[cfg(feature = "nfts")] #[derive(Encode, Decode, Debug, MaxEncodedLen)] pub enum NftsKeys { // Get the details of a collection. @@ -34,7 +40,7 @@ pub enum NftsKeys { } /// The required input for state queries in pallet assets. -#[cfg(feature = "devnet")] +#[cfg(feature = "assets")] #[derive(Encode, Decode, Debug, MaxEncodedLen)] pub enum AssetsKeys { Allowance(AssetId, AccountId, AccountId), diff --git a/runtime/devnet/Cargo.toml b/runtime/devnet/Cargo.toml index daa5457e..b72bf4f7 100644 --- a/runtime/devnet/Cargo.toml +++ b/runtime/devnet/Cargo.toml @@ -22,7 +22,7 @@ scale-info.workspace = true smallvec.workspace = true # Local -pop-primitives = { workspace = true, default-features = false, features = ["devnet"] } +pop-primitives = { workspace = true, default-features = false, features = ["assets", "cross-chain", "nfts"] } pop-runtime-common = { workspace = true, default-features = false } # Substrate @@ -91,7 +91,7 @@ parachain-info.workspace = true env_logger = "0.11.2" hex = "0.4.3" enumflags2 = "0.7.9" -pop-api = { path = "../../pop-api", defeult-features = false, features = ["assets"] } +pop-api = { path = "../../pop-api", features = ["assets", "cross-chain", "nfts"] } [features] default = ["std"] diff --git a/runtime/devnet/src/extensions.rs b/runtime/devnet/src/extensions.rs index b4c6ba84..9db31e83 100644 --- a/runtime/devnet/src/extensions.rs +++ b/runtime/devnet/src/extensions.rs @@ -11,11 +11,6 @@ use frame_support::{ use pallet_contracts::chain_extension::{ BufInBufOutState, ChainExtension, ChargedAmount, Environment, Ext, InitState, RetVal, }; -use pop_primitives::{ - cross_chain::CrossChainMessage, - storage_keys::{AssetsKeys, NftsKeys, ParachainSystemKeys, RuntimeStateKeys}, - AssetId, CollectionId, ItemId, -}; use sp_core::crypto::UncheckedFrom; use sp_runtime::traits::{BlockNumberProvider, Dispatchable}; use sp_std::{boxed::Box, vec::Vec}; @@ -25,9 +20,15 @@ use xcm::{ }; use crate::{ - config::assets::TrustBackedAssetsInstance, AccountId, AllowedPopApiCalls, RuntimeCall, + config::assets::TrustBackedAssetsInstance, AccountId, AllowedApiCalls, RuntimeCall, RuntimeOrigin, UNIT, }; +use pop_primitives::{ + cross_chain::CrossChainMessage, + nfts::{CollectionId, ItemId}, + storage_keys::{AssetsKeys, NftsKeys, ParachainSystemKeys, RuntimeStateKeys}, + AssetId, +}; const LOG_TARGET: &str = "pop-api::extension"; @@ -122,7 +123,7 @@ where log::debug!(target:LOG_TARGET, "{} Inputted RuntimeCall: {:?}", log_prefix, call); - origin.add_filter(AllowedPopApiCalls::contains); + origin.add_filter(AllowedApiCalls::contains); match call.dispatch(origin) { Ok(info) => { @@ -222,7 +223,7 @@ where RuntimeStateKeys::ParachainSystem(key) => { read_parachain_system_state::(key, &mut env) }, - RuntimeStateKeys::Assets(key) => read_trust_backed_assets_state::(key, &mut env), + RuntimeStateKeys::Assets(key) => read_assets_state::(key, &mut env), }? .encode(); @@ -299,7 +300,7 @@ where } } -fn read_trust_backed_assets_state( +fn read_assets_state( key: AssetsKeys, env: &mut Environment, ) -> Result, DispatchError> diff --git a/runtime/devnet/src/lib.rs b/runtime/devnet/src/lib.rs index 416a3298..b03bdac6 100644 --- a/runtime/devnet/src/lib.rs +++ b/runtime/devnet/src/lib.rs @@ -253,8 +253,8 @@ impl Contains for FilteredCalls { } /// A type to identify allowed calls to the Runtime from contracts. Used by Pop API -pub struct AllowedPopApiCalls; -impl Contains for crate::AllowedPopApiCalls { +pub struct AllowedApiCalls; +impl Contains for crate::AllowedApiCalls { fn contains(c: &RuntimeCall) -> bool { use config::assets::AssetsCall; use pallet_nfts::Call as NftsCall; diff --git a/runtime/devnet/src/tests/local_fungibles.rs b/runtime/devnet/src/tests/local_fungibles.rs index 154e9897..ac2b8ca0 100644 --- a/runtime/devnet/src/tests/local_fungibles.rs +++ b/runtime/devnet/src/tests/local_fungibles.rs @@ -9,8 +9,8 @@ use super::*; use pop_api::{ - error::{ArithmeticError::*, PopApiError::*, TokenError::*}, - v0::assets::use_cases::fungibles::FungiblesError::*, + error::{ArithmeticError::*, Error::*, TokenError::*}, + v0::assets::fungibles::FungiblesError::*, }; const ASSET_ID: AssetId = 1; @@ -268,7 +268,7 @@ fn create_works() { instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", 0, vec![0]); // No balance to pay for fees. assert_eq!( - decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), + decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), Module { index: 10, error: 2 }, ); // Instantiate a contract without balance (relay token). @@ -277,7 +277,7 @@ fn create_works() { // TODO: make sure it has enough for the fees but not for the deposit. // No balance to pay fe deposit. assert_eq!( - decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), + decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), Module { index: 10, error: 2 }, ); // Instantiate a contract with balance. @@ -289,12 +289,12 @@ fn create_works() { create_asset(ALICE, ASSET_ID, 1); // Asset ID is already taken. assert_eq!( - decoded::(create(addr.clone(), ASSET_ID, BOB, 1)), + decoded::(create(addr.clone(), ASSET_ID, BOB, 1)), Module { index: 52, error: 5 }, ); // The minimal balance for an asset must be non zero. assert_eq!( - decoded::(create(addr.clone(), new_asset, BOB, 0)), + decoded::(create(addr.clone(), new_asset, BOB, 0)), Module { index: 52, error: 7 }, ); let result = create(addr.clone(), new_asset, BOB, 1); @@ -333,39 +333,25 @@ fn transfer_from_mint_works() { // Asset does not exist. assert_eq!( - decoded::(transfer_from(addr.clone(), 1, None, Some(BOB), amount, &[0u8])), + decoded::(transfer_from(addr.clone(), 1, None, Some(BOB), amount, &[0u8])), Token(UnknownAsset) ); let asset = create_asset(ALICE, 1, 2); // Minting can only be done by the owner. assert_eq!( - decoded::(transfer_from( - addr.clone(), - asset, - None, - Some(BOB), - amount, - &[0u8] - )), + decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), Module { index: 52, error: 2 }, ); // Minimum balance of an asset can not be zero. assert_eq!( - decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), 1, &[0u8])), + decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), 1, &[0u8])), Token(BelowMinimum) ); let asset = create_asset(addr.clone(), 2, 2); // Asset is not live, i.e. frozen or being destroyed. freeze_asset(asset, addr.clone()); assert_eq!( - decoded::(transfer_from( - addr.clone(), - asset, - None, - Some(BOB), - amount, - &[0u8] - )), + decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), Module { index: 52, error: 16 }, ); thaw_asset(asset, addr.clone()); @@ -377,7 +363,7 @@ fn transfer_from_mint_works() { assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount); // Can not mint more tokens than Balance::MAX. assert_eq!( - decoded::(transfer_from( + decoded::(transfer_from( addr.clone(), asset, None, @@ -390,14 +376,7 @@ fn transfer_from_mint_works() { // Asset is not live, i.e. frozen or being destroyed. start_destroy_asset(asset, addr.clone()); assert_eq!( - decoded::(transfer_from( - addr.clone(), - asset, - None, - Some(BOB), - amount, - &[0u8] - )), + decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), Module { index: 52, error: 16 }, ); }); @@ -417,7 +396,7 @@ fn transfer_works() { // Asset does not exist. assert_eq!( - decoded::(transfer(addr.clone(), 1, BOB, amount,)), + decoded::(transfer(addr.clone(), 1, BOB, amount,)), Module { index: 52, error: 3 }, ); // Create asset with Alice as owner and mint `amount` to contract address. @@ -425,18 +404,18 @@ fn transfer_works() { // Asset is not live, i.e. frozen or being destroyed. freeze_asset(asset, ALICE); assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount,)), + decoded::(transfer(addr.clone(), asset, BOB, amount,)), Module { index: 52, error: 16 }, ); thaw_asset(asset, ALICE); // Not enough balance. assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), + decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), Module { index: 52, error: 0 }, ); // Not enough balance due to ED. assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount)), + decoded::(transfer(addr.clone(), asset, BOB, amount)), Module { index: 52, error: 0 }, ); // Successful transfer. @@ -447,13 +426,13 @@ fn transfer_works() { assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount / 2); // Transfer asset to account that does not exist. assert_eq!( - decoded::(transfer(addr.clone(), asset, FERDIE, amount / 4)), + decoded::(transfer(addr.clone(), asset, FERDIE, amount / 4)), Token(CannotCreate) ); // Asset is not live, i.e. frozen or being destroyed. start_destroy_asset(asset, ALICE); assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), + decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), Module { index: 52, error: 16 }, ); }); diff --git a/runtime/devnet/src/tests/mod.rs b/runtime/devnet/src/tests/mod.rs index dcb97ae1..828aa06b 100644 --- a/runtime/devnet/src/tests/mod.rs +++ b/runtime/devnet/src/tests/mod.rs @@ -1,16 +1,13 @@ #![cfg(test)] -use crate::{ - config::assets::TrustBackedAssetsInstance, Assets, Contracts, Runtime, RuntimeOrigin, System, - Weight, UNIT, -}; use codec::{Decode, Encode}; use frame_support::traits::fungibles::{approvals::Inspect as ApprovalInspect, Inspect}; use frame_system::Config; use pallet_contracts::{Code, CollectEvents, Determinism, ExecReturnValue}; -use pop_api::error::{ArithmeticError, PopApiError, TokenError, TransactionalError}; use sp_runtime::{traits::Hash, AccountId32, BuildStorage, DispatchError}; +use crate::{Assets, Contracts, Runtime, RuntimeOrigin, System, Weight, UNIT}; + mod local_fungibles; type Balance = u128; @@ -97,166 +94,174 @@ fn instantiate(contract: &str, init_value: u128, salt: Vec) -> AccountId32 { result.account_id } -pub fn get_pallet_index() -> u8 { - // Get the index of the pallet (module) - <::PalletInfo as frame_support::traits::PalletInfo>::index::() - .expect("Every active module has an index in the runtime; qed") as u8 -} - -#[test] -fn encoding_decoding_dispatch_error() { - use codec::{Decode, Encode}; - use sp_runtime::{ArithmeticError, DispatchError, ModuleError, TokenError}; +mod encoding { + use super::*; + use crate::config::assets::TrustBackedAssetsInstance; + use crate::Runtime; + use sp_runtime::DispatchError; - new_test_ext().execute_with(|| { - let error = DispatchError::Module(ModuleError { - index: 255, - error: [2, 0, 0, 0], - message: Some("error message"), - }); - let encoded = error.encode(); - let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); - assert_eq!(encoded, vec![3, 255, 2, 0, 0, 0]); - assert_eq!( - decoded, - // `message` is skipped for encoding. - DispatchError::Module(ModuleError { index: 255, error: [2, 0, 0, 0], message: None }) - ); - println!("Encoded Module Error: {:?}", encoded); + #[test] + fn encoding_decoding_dispatch_error() { + use codec::{Decode, Encode}; + use sp_runtime::{ArithmeticError, DispatchError, ModuleError, TokenError}; - // Example pallet assets Error into ModuleError - let index = get_pallet_index::(); - let mut error = - pallet_assets::Error::NotFrozen::.encode(); - error.resize(sp_runtime::MAX_MODULE_ERROR_ENCODED_SIZE, 0); - let message = None; - let error = DispatchError::Module(ModuleError { - index, - error: TryInto::try_into(error).expect("should work"), - message, - }); - let encoded = error.encode(); - let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); - assert_eq!(encoded, vec![3, 52, 18, 0, 0, 0]); - assert_eq!( - decoded, - DispatchError::Module(ModuleError { index: 52, error: [18, 0, 0, 0], message: None }) - ); - println!("Encoded Module Error: {:?}", encoded); + new_test_ext().execute_with(|| { + let error = DispatchError::Module(ModuleError { + index: 255, + error: [2, 0, 0, 0], + message: Some("error message"), + }); + let encoded = error.encode(); + let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); + assert_eq!(encoded, vec![3, 255, 2, 0, 0, 0]); + assert_eq!( + decoded, + // `message` is skipped for encoding. + DispatchError::Module(ModuleError { + index: 255, + error: [2, 0, 0, 0], + message: None + }) + ); + println!("Encoded Module Error: {:?}", encoded); - // Example DispatchError::Token - let error = DispatchError::Token(TokenError::UnknownAsset); - let encoded = error.encode(); - assert_eq!(encoded, vec![7, 4]); - println!("Encoded Token Error: {:?}", encoded); + // Example pallet assets Error into ModuleError. + let index = + <::PalletInfo as frame_support::traits::PalletInfo>::index::< + Assets, + >() + .expect("Every active module has an index in the runtime; qed") as u8; - // Example DispatchError::Arithmetic - let error = DispatchError::Arithmetic(ArithmeticError::Overflow); - let encoded = error.encode(); - assert_eq!(encoded, vec![8, 1]); - println!("Encoded Arithmetic Error: {:?}", encoded); - }); -} + let mut error = + pallet_assets::Error::NotFrozen::.encode(); + error.resize(sp_runtime::MAX_MODULE_ERROR_ENCODED_SIZE, 0); + let error = DispatchError::Module(ModuleError { + index, + error: TryInto::try_into(error).expect("should work"), + message: None, + }); + let encoded = error.encode(); + let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); + assert_eq!(encoded, vec![3, 52, 18, 0, 0, 0]); + assert_eq!( + decoded, + DispatchError::Module(ModuleError { + index: 52, + error: [18, 0, 0, 0], + message: None + }) + ); + println!("Encoded Module Error: {:?}", encoded); -#[test] -fn encoding_of_enum() { - use codec::{Decode, Encode}; + // Example DispatchError::Token + let error = DispatchError::Token(TokenError::UnknownAsset); + let encoded = error.encode(); + assert_eq!(encoded, vec![7, 4]); + println!("Encoded Token Error: {:?}", encoded); - // Comprehensive enum with all different type of variants. - #[derive(Debug, PartialEq, Encode, Decode)] - enum ComprehensiveEnum { - SimpleVariant, - DataVariant(u8), - NamedFields { w: u8 }, - NestedEnum(InnerEnum), - OptionVariant(Option), - VecVariant(Vec), - TupleVariant(u8, u8), - NestedStructVariant(NestedStruct), - NestedEnumStructVariant(NestedEnumStruct), + // Example DispatchError::Arithmetic + let error = DispatchError::Arithmetic(ArithmeticError::Overflow); + let encoded = error.encode(); + assert_eq!(encoded, vec![8, 1]); + println!("Encoded Arithmetic Error: {:?}", encoded); + }); } - #[derive(Debug, PartialEq, Encode, Decode)] - enum InnerEnum { - A, - B { inner_data: u8 }, - C(u8), - } + #[test] + fn encoding_of_enum() { + use codec::{Decode, Encode}; - #[derive(Debug, PartialEq, Encode, Decode)] - struct NestedStruct { - x: u8, - y: u8, - } + // Comprehensive enum with all different type of variants. + #[derive(Debug, PartialEq, Encode, Decode)] + enum ComprehensiveEnum { + SimpleVariant, + DataVariant(u8), + NamedFields { w: u8 }, + NestedEnum(InnerEnum), + OptionVariant(Option), + VecVariant(Vec), + TupleVariant(u8, u8), + NestedStructVariant(NestedStruct), + NestedEnumStructVariant(NestedEnumStruct), + } - #[derive(Debug, PartialEq, Encode, Decode)] - struct NestedEnumStruct { - inner_enum: InnerEnum, - } + #[derive(Debug, PartialEq, Encode, Decode)] + enum InnerEnum { + A, + B { inner_data: u8 }, + C(u8), + } - // Creating each possible variant for an enum. - let enum_simple = ComprehensiveEnum::SimpleVariant; - let enum_data = ComprehensiveEnum::DataVariant(42); - let enum_named = ComprehensiveEnum::NamedFields { w: 42 }; - let enum_nested = ComprehensiveEnum::NestedEnum(InnerEnum::B { inner_data: 42 }); - let enum_option = ComprehensiveEnum::OptionVariant(Some(42)); - let enum_vec = ComprehensiveEnum::VecVariant(vec![1, 2, 3, 4, 5]); - let enum_tuple = ComprehensiveEnum::TupleVariant(42, 42); - let enum_nested_struct = ComprehensiveEnum::NestedStructVariant(NestedStruct { x: 42, y: 42 }); - let enum_nested_enum_struct = ComprehensiveEnum::NestedEnumStructVariant(NestedEnumStruct { - inner_enum: InnerEnum::C(42), - }); + #[derive(Debug, PartialEq, Encode, Decode)] + struct NestedStruct { + x: u8, + y: u8, + } - // Encode and print each variant individually to see their encoded values. - println!("{:?} -> {:?}", enum_simple, enum_simple.encode()); - println!("{:?} -> {:?}", enum_data, enum_data.encode()); - println!("{:?} -> {:?}", enum_named, enum_named.encode()); - println!("{:?} -> {:?}", enum_nested, enum_nested.encode()); - println!("{:?} -> {:?}", enum_option, enum_option.encode()); - println!("{:?} -> {:?}", enum_vec, enum_vec.encode()); - println!("{:?} -> {:?}", enum_tuple, enum_tuple.encode()); - println!("{:?} -> {:?}", enum_nested_struct, enum_nested_struct.encode()); - println!("{:?} -> {:?}", enum_nested_enum_struct, enum_nested_enum_struct.encode()); -} + #[derive(Debug, PartialEq, Encode, Decode)] + struct NestedEnumStruct { + inner_enum: InnerEnum, + } -#[test] -fn dispatch_error_to_status_code_to_pop_api_error_works() { - // Create all the different `DispatchError` variants with its respective `PopApiError`. - let test_cases = vec![ - (DispatchError::CannotLookup, PopApiError::CannotLookup), - (DispatchError::BadOrigin, PopApiError::BadOrigin), - ( - DispatchError::Module(sp_runtime::ModuleError { - index: 1, - error: [2, 0, 0, 0], - message: Some("hallo"), - }), - PopApiError::Module { index: 1, error: 2 }, - ), - (DispatchError::ConsumerRemaining, PopApiError::ConsumerRemaining), - (DispatchError::NoProviders, PopApiError::NoProviders), - (DispatchError::TooManyConsumers, PopApiError::TooManyConsumers), - ( - DispatchError::Token(sp_runtime::TokenError::FundsUnavailable), - PopApiError::Token(TokenError::FundsUnavailable), - ), - ( - DispatchError::Arithmetic(sp_runtime::ArithmeticError::Overflow), - PopApiError::Arithmetic(ArithmeticError::Overflow), - ), - ( - DispatchError::Transactional(sp_runtime::TransactionalError::LimitReached), - PopApiError::Transactional(TransactionalError::LimitReached), - ), - (DispatchError::Exhausted, PopApiError::Exhausted), - (DispatchError::Corruption, PopApiError::Corruption), - (DispatchError::Unavailable, PopApiError::Unavailable), - (DispatchError::RootNotAllowed, PopApiError::RootNotAllowed), - ]; - for (error, pop_api_error) in test_cases { - // Show that the encoding and decoding of the PopApiError <> u32 (status code) works. - let status_code = crate::extensions::convert_to_status_code(error); - // let error = pop_api::error::convert_to_pop_api_error(status_code); - // assert_eq!(pop_api_error, error,); + // Creating each possible variant for an enum. + let enum_simple = ComprehensiveEnum::SimpleVariant; + let enum_data = ComprehensiveEnum::DataVariant(42); + let enum_named = ComprehensiveEnum::NamedFields { w: 42 }; + let enum_nested = ComprehensiveEnum::NestedEnum(InnerEnum::B { inner_data: 42 }); + let enum_option = ComprehensiveEnum::OptionVariant(Some(42)); + let enum_vec = ComprehensiveEnum::VecVariant(vec![1, 2, 3, 4, 5]); + let enum_tuple = ComprehensiveEnum::TupleVariant(42, 42); + let enum_nested_struct = + ComprehensiveEnum::NestedStructVariant(NestedStruct { x: 42, y: 42 }); + let enum_nested_enum_struct = + ComprehensiveEnum::NestedEnumStructVariant(NestedEnumStruct { + inner_enum: InnerEnum::C(42), + }); + + // Encode and print each variant individually to see their encoded values. + println!("{:?} -> {:?}", enum_simple, enum_simple.encode()); + println!("{:?} -> {:?}", enum_data, enum_data.encode()); + println!("{:?} -> {:?}", enum_named, enum_named.encode()); + println!("{:?} -> {:?}", enum_nested, enum_nested.encode()); + println!("{:?} -> {:?}", enum_option, enum_option.encode()); + println!("{:?} -> {:?}", enum_vec, enum_vec.encode()); + println!("{:?} -> {:?}", enum_tuple, enum_tuple.encode()); + println!("{:?} -> {:?}", enum_nested_struct, enum_nested_struct.encode()); + println!("{:?} -> {:?}", enum_nested_enum_struct, enum_nested_enum_struct.encode()); + } + + #[test] + fn dispatch_error_to_status_code() { + // Create all the different `DispatchError` variants with its respective `PopApiError`. + let test_cases = vec![ + (DispatchError::Other("hallo"), [0, 0, 0, 0]), + (DispatchError::CannotLookup, [1, 0, 0, 0]), + (DispatchError::BadOrigin, [2, 0, 0, 0]), + ( + DispatchError::Module(sp_runtime::ModuleError { + index: 1, + error: [2, 0, 0, 0], + message: Some("hallo"), + }), + [3, 1, 2, 0], + ), + (DispatchError::ConsumerRemaining, [4, 0, 0, 0]), + (DispatchError::NoProviders, [5, 0, 0, 0]), + (DispatchError::TooManyConsumers, [6, 0, 0, 0]), + (DispatchError::Token(sp_runtime::TokenError::BelowMinimum), [7, 2, 0, 0]), + (DispatchError::Arithmetic(sp_runtime::ArithmeticError::Overflow), [8, 1, 0, 0]), + ( + DispatchError::Transactional(sp_runtime::TransactionalError::LimitReached), + [9, 0, 0, 0], + ), + (DispatchError::Exhausted, [10, 0, 0, 0]), + (DispatchError::Corruption, [11, 0, 0, 0]), + (DispatchError::Unavailable, [12, 0, 0, 0]), + (DispatchError::RootNotAllowed, [13, 0, 0, 0]), + ]; + for (error, encoded_error) in test_cases { + let status_code = crate::extensions::convert_to_status_code(error); + assert_eq!(status_code, u32::decode(&mut &encoded_error[..]).unwrap()); + } } } diff --git a/runtime/testnet/Cargo.toml b/runtime/testnet/Cargo.toml index 43b1e310..b7aa7b76 100644 --- a/runtime/testnet/Cargo.toml +++ b/runtime/testnet/Cargo.toml @@ -22,7 +22,7 @@ scale-info.workspace = true smallvec.workspace = true # Local -pop-primitives = { workspace = true, features = ["testnet"] } +pop-primitives = { workspace = true, features = ["assets", "nfts", "cross-chain"] } pop-runtime-common = { workspace = true, default-features = false } # Substrate diff --git a/runtime/testnet/src/extensions.rs b/runtime/testnet/src/extensions.rs index a3284ad4..dd2c52bb 100644 --- a/runtime/testnet/src/extensions.rs +++ b/runtime/testnet/src/extensions.rs @@ -9,8 +9,8 @@ use pallet_contracts::chain_extension::{ BufInBufOutState, ChainExtension, ChargedAmount, Environment, Ext, InitState, RetVal, }; use pop_primitives::{ + nfts::{CollectionId, ItemId}, storage_keys::{NftsKeys, ParachainSystemKeys, RuntimeStateKeys}, - CollectionId, ItemId, }; use sp_core::crypto::UncheckedFrom; use sp_runtime::{ @@ -19,7 +19,7 @@ use sp_runtime::{ }; use sp_std::vec::Vec; -use crate::{AccountId, AllowedPopApiCalls, RuntimeCall, RuntimeOrigin}; +use crate::{AccountId, AllowedApiCalls, RuntimeCall, RuntimeOrigin}; const LOG_TARGET: &str = "pop-api::extension"; @@ -110,7 +110,7 @@ where log::debug!(target:LOG_TARGET, "{} inputted RuntimeCall: {:?}", log_prefix, call); - origin.add_filter(AllowedPopApiCalls::contains); + origin.add_filter(AllowedApiCalls::contains); match call.dispatch(origin) { Ok(info) => { diff --git a/runtime/testnet/src/lib.rs b/runtime/testnet/src/lib.rs index 66a5092c..c4178011 100644 --- a/runtime/testnet/src/lib.rs +++ b/runtime/testnet/src/lib.rs @@ -250,8 +250,8 @@ impl Contains for FilteredCalls { } /// A type to identify allowed calls to the Runtime from contracts. Used by Pop API -pub struct AllowedPopApiCalls; -impl Contains for crate::AllowedPopApiCalls { +pub struct AllowedApiCalls; +impl Contains for AllowedApiCalls { fn contains(c: &RuntimeCall) -> bool { use pallet_nfts::Call as NftsCall; matches!( From 61b3a17d39b9bbbc76f87665a9546da9e0b58f89 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Wed, 26 Jun 2024 10:48:23 +0200 Subject: [PATCH 12/76] refactor: tests local fungibles --- Cargo.lock | 2787 ++++++----------- Cargo.toml | 28 +- pop-api/Cargo.toml | 6 +- pop-api/examples/.gitignore | 4 +- pop-api/examples/fungibles/lib.rs | 18 +- pop-api/integration-tests/Cargo.toml | 31 + pop-api/integration-tests/src/lib.rs | 95 + .../integration-tests/src}/local_fungibles.rs | 71 +- pop-api/src/lib.rs | 3 +- pop-api/src/v0/assets/mod.rs | 8 +- primitives/Cargo.toml | 4 +- primitives/src/storage_keys.rs | 4 +- runtime/devnet/Cargo.toml | 2 - runtime/devnet/src/extensions.rs | 180 ++ runtime/devnet/src/lib.rs | 2 - runtime/devnet/src/tests/mod.rs | 267 -- 16 files changed, 1331 insertions(+), 2179 deletions(-) create mode 100644 pop-api/integration-tests/Cargo.toml create mode 100644 pop-api/integration-tests/src/lib.rs rename {runtime/devnet/src/tests => pop-api/integration-tests/src}/local_fungibles.rs (88%) delete mode 100644 runtime/devnet/src/tests/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 77c563fb..d798562b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -343,7 +343,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94893f1e0c6eeab764ade8dc4c0db24caf4fe7cbbaafc0eba0a9030f447b5185" dependencies = [ "num-traits", - "rand 0.8.5", + "rand", ] [[package]] @@ -358,12 +358,6 @@ version = "6.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f840fb7195bcfc5e17ea40c26e5ce6d5b9ce5d584466e17703209657e459ae0" -[[package]] -name = "array-init" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d62b7694a562cdf5a74227903507c56ab2cc8bdd1f781ed5cb4cf9c9f810bfc" - [[package]] name = "arrayref" version = "0.3.7" @@ -379,12 +373,6 @@ dependencies = [ "nodrop", ] -[[package]] -name = "arrayvec" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" - [[package]] name = "arrayvec" version = "0.7.4" @@ -416,7 +404,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.109", - "synstructure 0.12.6", + "synstructure", ] [[package]] @@ -498,17 +486,17 @@ dependencies = [ "sp-api", "sp-block-builder", "sp-consensus-aura", - "sp-core 29.0.0", + "sp-core", "sp-genesis-builder", "sp-inherents", "sp-offchain", - "sp-runtime 32.0.0", + "sp-runtime", "sp-session", - "sp-std 14.0.0", - "sp-storage 20.0.0", + "sp-std", + "sp-storage", "sp-transaction-pool", "sp-version", - "sp-weights 28.0.0", + "sp-weights", "staging-parachain-info", "staging-xcm", "staging-xcm-builder", @@ -537,9 +525,9 @@ dependencies = [ "parachains-common", "parachains-runtimes-test-utils", "parity-scale-codec", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-io", + "sp-runtime", + "sp-std", "staging-parachain-info", "staging-xcm", "staging-xcm-builder", @@ -563,8 +551,8 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -921,7 +909,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93f2635620bf0b9d4576eb7bb9a38a55df78bd1205d26fa994b25911a69f212f" dependencies = [ "bitcoin_hashes", - "rand 0.8.5", + "rand", "rand_core 0.6.4", "serde", "unicode-normalization", @@ -1024,18 +1012,6 @@ dependencies = [ "constant_time_eq 0.3.0", ] -[[package]] -name = "block-buffer" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" -dependencies = [ - "block-padding", - "byte-tools", - "byteorder", - "generic-array 0.12.4", -] - [[package]] name = "block-buffer" version = "0.9.0" @@ -1054,15 +1030,6 @@ dependencies = [ "generic-array 0.14.7", ] -[[package]] -name = "block-padding" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" -dependencies = [ - "byte-tools", -] - [[package]] name = "blocking" version = "1.5.1" @@ -1149,7 +1116,7 @@ dependencies = [ "frame-system", "polkadot-primitives", "sp-api", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -1163,8 +1130,8 @@ dependencies = [ "bp-runtime", "frame-support", "sp-api", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -1178,8 +1145,8 @@ dependencies = [ "bp-runtime", "frame-support", "sp-api", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -1195,9 +1162,9 @@ dependencies = [ "scale-info", "serde", "sp-consensus-grandpa", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-runtime", + "sp-std", ] [[package]] @@ -1212,8 +1179,8 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core 29.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-std", ] [[package]] @@ -1229,9 +1196,9 @@ dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-runtime", + "sp-std", ] [[package]] @@ -1248,9 +1215,9 @@ dependencies = [ "parity-util-mem", "scale-info", "serde", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-runtime", + "sp-std", ] [[package]] @@ -1264,8 +1231,8 @@ dependencies = [ "frame-support", "parity-scale-codec", "scale-info", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -1283,13 +1250,13 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", - "sp-std 14.0.0", - "sp-trie 30.0.0", - "trie-db 0.28.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-state-machine", + "sp-std", + "sp-trie", + "trie-db", ] [[package]] @@ -1302,15 +1269,15 @@ dependencies = [ "bp-parachains", "bp-polkadot-core", "bp-runtime", - "ed25519-dalek 2.1.1", + "ed25519-dalek", "finality-grandpa", "parity-scale-codec", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-consensus-grandpa", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-trie 30.0.0", + "sp-core", + "sp-runtime", + "sp-std", + "sp-trie", ] [[package]] @@ -1319,7 +1286,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6663e0179d475e30cfcf28cf597cdc8f4bb1c2c39a557b4cbe0057db0657fb67" dependencies = [ - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -1330,8 +1297,8 @@ checksum = "86ff4abe93be7bc1663adc41817b1aa3476fbec953ce361537419924310d5dd4" dependencies = [ "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", ] [[package]] @@ -1361,11 +1328,11 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-trie 30.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", + "sp-trie", "staging-xcm", "staging-xcm-builder", ] @@ -1778,26 +1745,6 @@ dependencies = [ "tiny-keccak", ] -[[package]] -name = "const_env" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e9e4f72c6e3398ca6da372abd9affd8f89781fe728869bbf986206e9af9627e" -dependencies = [ - "const_env_impl", -] - -[[package]] -name = "const_env_impl" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a4f51209740b5e1589e702b3044cdd4562cef41b6da404904192ffffb852d62" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "constant_time_eq" version = "0.1.5" @@ -2097,8 +2044,8 @@ dependencies = [ "sc-client-api", "sc-service", "sp-blockchain", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", "url", ] @@ -2121,8 +2068,8 @@ dependencies = [ "sc-client-api", "sp-api", "sp-consensus", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", "tracing", ] @@ -2154,16 +2101,16 @@ dependencies = [ "sc-telemetry", "schnellru", "sp-api", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-block-builder", "sp-blockchain", "sp-consensus", "sp-consensus-aura", - "sp-core 29.0.0", + "sp-core", "sp-inherents", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", + "sp-keystore", + "sp-runtime", + "sp-state-machine", "sp-timestamp", "substrate-prometheus-endpoint", "tracing", @@ -2191,10 +2138,10 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-consensus-slots", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", "sp-timestamp", - "sp-trie 30.0.0", + "sp-trie", "substrate-prometheus-endpoint", "tracing", ] @@ -2210,8 +2157,8 @@ dependencies = [ "cumulus-primitives-parachain-inherent", "sp-consensus", "sp-inherents", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", + "sp-runtime", + "sp-state-machine", "thiserror", ] @@ -2233,9 +2180,9 @@ dependencies = [ "sc-client-api", "sp-blockchain", "sp-consensus", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", + "sp-core", + "sp-runtime", + "sp-state-machine", "tracing", ] @@ -2256,11 +2203,11 @@ dependencies = [ "sp-api", "sp-crypto-hashing", "sp-inherents", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", - "sp-std 14.0.0", - "sp-storage 20.0.0", - "sp-trie 30.0.0", + "sp-runtime", + "sp-state-machine", + "sp-std", + "sp-storage", + "sp-trie", "tracing", ] @@ -2280,12 +2227,12 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-overseer", "polkadot-primitives", - "rand 0.8.5", + "rand", "sc-client-api", "sc-consensus", "sp-consensus", "sp-maybe-compressed-blob", - "sp-runtime 32.0.0", + "sp-runtime", "tracing", ] @@ -2321,8 +2268,8 @@ dependencies = [ "sp-api", "sp-blockchain", "sp-consensus", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", "sp-transaction-pool", ] @@ -2339,10 +2286,10 @@ dependencies = [ "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-consensus-aura", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -2368,17 +2315,17 @@ dependencies = [ "polkadot-runtime-common", "polkadot-runtime-parachains", "scale-info", - "sp-core 29.0.0", - "sp-externalities 0.26.0", + "sp-core", + "sp-externalities", "sp-inherents", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", - "sp-std 14.0.0", - "sp-trie 30.0.0", + "sp-io", + "sp-runtime", + "sp-state-machine", + "sp-std", + "sp-trie", "sp-version", "staging-xcm", - "trie-db 0.28.0", + "trie-db", ] [[package]] @@ -2404,8 +2351,8 @@ dependencies = [ "frame-system", "pallet-session", "parity-scale-codec", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -2419,9 +2366,9 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-io", + "sp-runtime", + "sp-std", "staging-xcm", ] @@ -2443,10 +2390,10 @@ dependencies = [ "polkadot-runtime-common", "polkadot-runtime-parachains", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", "staging-xcm", "staging-xcm-executor", ] @@ -2462,8 +2409,8 @@ dependencies = [ "polkadot-primitives", "sp-api", "sp-consensus-aura", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -2478,9 +2425,9 @@ dependencies = [ "polkadot-primitives", "scale-info", "sp-api", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-trie 30.0.0", + "sp-runtime", + "sp-std", + "sp-trie", "staging-xcm", ] @@ -2494,10 +2441,10 @@ dependencies = [ "cumulus-primitives-core", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", + "sp-core", "sp-inherents", - "sp-std 14.0.0", - "sp-trie 30.0.0", + "sp-std", + "sp-trie", ] [[package]] @@ -2506,9 +2453,9 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1b74f9141190b9f4bf96a947ade46da64097b77f1ebfa8d611c81724250e119" dependencies = [ - "sp-externalities 0.26.0", - "sp-runtime-interface 25.0.0", - "sp-trie 30.0.0", + "sp-externalities", + "sp-runtime-interface", + "sp-trie", ] [[package]] @@ -2524,9 +2471,9 @@ dependencies = [ "parity-scale-codec", "polkadot-runtime-common", "polkadot-runtime-parachains", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-io", + "sp-runtime", + "sp-std", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -2552,9 +2499,9 @@ dependencies = [ "sc-tracing", "sp-api", "sp-consensus", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", + "sp-core", + "sp-runtime", + "sp-state-machine", ] [[package]] @@ -2572,7 +2519,7 @@ dependencies = [ "sc-client-api", "sp-api", "sp-blockchain", - "sp-state-machine 0.36.0", + "sp-state-machine", "thiserror", ] @@ -2612,7 +2559,7 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-consensus-babe", - "sp-runtime 32.0.0", + "sp-runtime", "substrate-prometheus-endpoint", "tokio", "tracing", @@ -2634,7 +2581,7 @@ dependencies = [ "parity-scale-codec", "pin-project", "polkadot-overseer", - "rand 0.8.5", + "rand", "sc-client-api", "sc-rpc-api", "sc-service", @@ -2646,10 +2593,10 @@ dependencies = [ "sp-api", "sp-authority-discovery", "sp-consensus-babe", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", - "sp-storage 20.0.0", + "sp-core", + "sp-runtime", + "sp-state-machine", + "sp-storage", "sp-version", "thiserror", "tokio", @@ -2667,23 +2614,10 @@ dependencies = [ "cumulus-primitives-core", "parity-scale-codec", "polkadot-primitives", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", - "sp-std 14.0.0", - "sp-trie 30.0.0", -] - -[[package]] -name = "curve25519-dalek" -version = "2.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a9b85542f99a2dfa2a1b8e192662741c9859a846b296bef1c92ef9b58b5a216" -dependencies = [ - "byteorder", - "digest 0.8.1", - "rand_core 0.5.1", - "subtle 2.5.0", - "zeroize", + "sp-runtime", + "sp-state-machine", + "sp-std", + "sp-trie", ] [[package]] @@ -3130,19 +3064,10 @@ dependencies = [ "digest 0.10.7", "elliptic-curve", "rfc6979", - "signature 2.2.0", + "signature", "spki", ] -[[package]] -name = "ed25519" -version = "1.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91cff35c70bba8a626e3185d8cd48cc11b5437e1a5bcd15b9b5fa3c64b6dfee7" -dependencies = [ - "signature 1.6.4", -] - [[package]] name = "ed25519" version = "2.2.3" @@ -3150,19 +3075,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" dependencies = [ "pkcs8", - "signature 2.2.0", -] - -[[package]] -name = "ed25519-dalek" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d" -dependencies = [ - "curve25519-dalek 3.2.0", - "ed25519 1.5.3", - "sha2 0.9.9", - "zeroize", + "signature", ] [[package]] @@ -3172,7 +3085,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871" dependencies = [ "curve25519-dalek 4.1.2", - "ed25519 2.2.3", + "ed25519", "rand_core 0.6.4", "serde", "sha2 0.10.8", @@ -3201,7 +3114,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d9ce6874da5d4415896cd45ffbc4d1cfc0c4f9c079427bd870742c30f2f65a9" dependencies = [ "curve25519-dalek 4.1.2", - "ed25519 2.2.3", + "ed25519", "hashbrown 0.14.3", "hex", "rand_core 0.6.4", @@ -3261,8 +3174,8 @@ dependencies = [ "sc-consensus-grandpa", "sp-authority-discovery", "sp-consensus-babe", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", "staging-xcm", "xcm-emulator", ] @@ -3509,12 +3422,6 @@ dependencies = [ "syn 2.0.58", ] -[[package]] -name = "fake-simd" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" - [[package]] name = "fallible-iterator" version = "0.2.0" @@ -3632,7 +3539,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534" dependencies = [ "byteorder", - "rand 0.8.5", + "rand", "rustc-hex", "static_assertions", ] @@ -3709,13 +3616,13 @@ dependencies = [ "scale-info", "serde", "sp-api", - "sp-application-crypto 31.0.0", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-runtime-interface 25.0.0", - "sp-std 14.0.0", - "sp-storage 20.0.0", + "sp-application-crypto", + "sp-core", + "sp-io", + "sp-runtime", + "sp-runtime-interface", + "sp-std", + "sp-storage", "static_assertions", ] @@ -3740,7 +3647,7 @@ dependencies = [ "linked-hash-map", "log", "parity-scale-codec", - "rand 0.8.5", + "rand", "rand_pcg", "sc-block-builder", "sc-cli", @@ -3753,17 +3660,17 @@ dependencies = [ "serde_json", "sp-api", "sp-blockchain", - "sp-core 29.0.0", + "sp-core", "sp-database", - "sp-externalities 0.26.0", + "sp-externalities", "sp-inherents", - "sp-io 31.0.0", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", - "sp-storage 20.0.0", - "sp-trie 30.0.0", - "sp-wasm-interface 20.0.0", + "sp-io", + "sp-keystore", + "sp-runtime", + "sp-state-machine", + "sp-storage", + "sp-trie", + "sp-wasm-interface", "thiserror", "thousands", ] @@ -3791,11 +3698,11 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", + "sp-arithmetic", + "sp-core", "sp-npos-elections", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -3810,11 +3717,11 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-tracing 16.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", + "sp-tracing", ] [[package]] @@ -3852,11 +3759,11 @@ dependencies = [ "log", "parity-scale-codec", "serde", - "sp-core 29.0.0", + "sp-core", "sp-crypto-hashing", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", + "sp-io", + "sp-runtime", + "sp-state-machine", "spinners", "substrate-rpc-client", "tokio", @@ -3887,20 +3794,20 @@ dependencies = [ "serde_json", "smallvec", "sp-api", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", + "sp-arithmetic", + "sp-core", "sp-crypto-hashing-proc-macro", - "sp-debug-derive 14.0.0", + "sp-debug-derive", "sp-genesis-builder", "sp-inherents", - "sp-io 31.0.0", + "sp-io", "sp-metadata-ir", - "sp-runtime 32.0.0", + "sp-runtime", "sp-staking", - "sp-state-machine 0.36.0", - "sp-std 14.0.0", - "sp-tracing 16.0.0", - "sp-weights 28.0.0", + "sp-state-machine", + "sp-std", + "sp-tracing", + "sp-weights", "static_assertions", "tt-call", ] @@ -3962,12 +3869,12 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", "sp-version", - "sp-weights 28.0.0", + "sp-weights", ] [[package]] @@ -3981,9 +3888,9 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-runtime", + "sp-std", ] [[package]] @@ -4005,8 +3912,8 @@ dependencies = [ "frame-support", "parity-scale-codec", "sp-api", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -4256,7 +4163,7 @@ version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ea1015b5a70616b688dc230cfe50c8af89d972cb132d5a622814d29773b10b9" dependencies = [ - "rand 0.8.5", + "rand", "rand_core 0.6.4", ] @@ -4762,206 +4669,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "ink" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d4a862aedbfda93175ddf75c9aaa2ae4c4b39ee5cee06c16d50bccce05bf5c7" -dependencies = [ - "derive_more", - "ink_env", - "ink_macro", - "ink_metadata", - "ink_prelude", - "ink_primitives", - "ink_storage", - "pallet-contracts-uapi-next", - "parity-scale-codec", - "scale-info", -] - -[[package]] -name = "ink_allocator" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cee56055bac6d928d425e944c5f3b69baa33c9635822fd1c00cd4afc70fde3e" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "ink_codegen" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70a1f8473fa09e0f9b6f3cb3f8d18c07c14ebf9ea1f7cdfee270f009d45ee8e9" -dependencies = [ - "blake2 0.10.6", - "derive_more", - "either", - "heck 0.4.1", - "impl-serde", - "ink_ir", - "ink_primitives", - "itertools 0.12.1", - "parity-scale-codec", - "proc-macro2", - "quote", - "serde", - "serde_json", - "syn 2.0.58", -] - -[[package]] -name = "ink_engine" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f357e2e867f4e222ffc4015a6e61d1073548de89f70a4e36a8b0385562777fa" -dependencies = [ - "blake2 0.10.6", - "derive_more", - "ink_primitives", - "pallet-contracts-uapi-next", - "parity-scale-codec", - "secp256k1 0.28.2", - "sha2 0.10.8", - "sha3", -] - -[[package]] -name = "ink_env" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42cec50b7e4f8406aab25801b015d3802a52d76cfbe48ce11cfb4200fa88e296" -dependencies = [ - "blake2 0.10.6", - "cfg-if", - "const_env", - "derive_more", - "ink_allocator", - "ink_engine", - "ink_prelude", - "ink_primitives", - "ink_storage_traits", - "num-traits", - "pallet-contracts-uapi-next", - "parity-scale-codec", - "paste", - "rlibc", - "scale-decode", - "scale-encode", - "scale-info", - "schnorrkel 0.11.4", - "secp256k1 0.28.2", - "sha2 0.10.8", - "sha3", - "static_assertions", -] - -[[package]] -name = "ink_ir" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b1ad2975551c4ed800af971289ed6d2c68ac41ffc03a42010b3e01d7360dfb2" -dependencies = [ - "blake2 0.10.6", - "either", - "impl-serde", - "ink_prelude", - "itertools 0.12.1", - "proc-macro2", - "quote", - "syn 2.0.58", -] - -[[package]] -name = "ink_macro" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aee1a546f37eae3b3cd223832d31702033c5369dcfa3405899587c110a7908d3" -dependencies = [ - "ink_codegen", - "ink_ir", - "ink_primitives", - "parity-scale-codec", - "proc-macro2", - "quote", - "syn 2.0.58", - "synstructure 0.13.1", -] - -[[package]] -name = "ink_metadata" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a98fcc0ff9292ff68c7ee7b84c93533c9ff13859ec3b148faa822e2da9954fe6" -dependencies = [ - "derive_more", - "impl-serde", - "ink_prelude", - "ink_primitives", - "linkme", - "parity-scale-codec", - "scale-info", - "schemars", - "serde", -] - -[[package]] -name = "ink_prelude" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea1734d058c80aa72e59c8ae75624fd8a51791efba21469f273156c0f4cad5c9" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "ink_primitives" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11ec35ef7f45e67a53b6142d7e7f18e6d9292d76c3a2a1da14cf8423e481813d" -dependencies = [ - "derive_more", - "ink_prelude", - "parity-scale-codec", - "scale-decode", - "scale-encode", - "scale-info", - "xxhash-rust", -] - -[[package]] -name = "ink_storage" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbdb04cad74df858c05bc9cb6f30bbf12da33c3e2cb7ca211749c001fa761aa9" -dependencies = [ - "array-init", - "cfg-if", - "derive_more", - "ink_env", - "ink_metadata", - "ink_prelude", - "ink_primitives", - "ink_storage_traits", - "pallet-contracts-uapi-next", - "parity-scale-codec", - "scale-info", -] - -[[package]] -name = "ink_storage_traits" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83ce49e3d2935fc1ec3e73117119712b187d3123339f6a31624e92f75fa2293d" -dependencies = [ - "ink_metadata", - "ink_prelude", - "ink_primitives", - "parity-scale-codec", - "scale-info", -] - [[package]] name = "inout" version = "0.1.3" @@ -5019,8 +4726,8 @@ dependencies = [ "sp-consensus-babe", "sp-consensus-beefy", "sp-consensus-grandpa", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", "staging-xcm", "staging-xcm-executor", "tracing-subscriber 0.3.18", @@ -5216,7 +4923,7 @@ dependencies = [ "hyper", "jsonrpsee-types 0.20.3", "parking_lot 0.12.1", - "rand 0.8.5", + "rand", "rustc-hash", "serde", "serde_json", @@ -5549,7 +5256,7 @@ dependencies = [ "parking_lot 0.12.1", "pin-project", "quick-protobuf", - "rand 0.8.5", + "rand", "rw-stream-sink", "smallvec", "thiserror", @@ -5600,12 +5307,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "276bb57e7af15d8f100d3c11cbdd32c6752b7eef4ba7a18ecf464972c07abcce" dependencies = [ "bs58 0.4.0", - "ed25519-dalek 2.1.1", + "ed25519-dalek", "log", "multiaddr", "multihash 0.17.0", "quick-protobuf", - "rand 0.8.5", + "rand", "sha2 0.10.8", "thiserror", "zeroize", @@ -5630,7 +5337,7 @@ dependencies = [ "libp2p-swarm", "log", "quick-protobuf", - "rand 0.8.5", + "rand", "sha2 0.10.8", "smallvec", "thiserror", @@ -5652,7 +5359,7 @@ dependencies = [ "libp2p-identity", "libp2p-swarm", "log", - "rand 0.8.5", + "rand", "smallvec", "socket2 0.4.10", "tokio", @@ -5688,7 +5395,7 @@ dependencies = [ "log", "once_cell", "quick-protobuf", - "rand 0.8.5", + "rand", "sha2 0.10.8", "snow", "static_assertions", @@ -5710,7 +5417,7 @@ dependencies = [ "libp2p-core", "libp2p-swarm", "log", - "rand 0.8.5", + "rand", "void", ] @@ -5730,7 +5437,7 @@ dependencies = [ "log", "parking_lot 0.12.1", "quinn-proto", - "rand 0.8.5", + "rand", "rustls 0.20.9", "thiserror", "tokio", @@ -5748,7 +5455,7 @@ dependencies = [ "libp2p-core", "libp2p-identity", "libp2p-swarm", - "rand 0.8.5", + "rand", "smallvec", ] @@ -5767,7 +5474,7 @@ dependencies = [ "libp2p-identity", "libp2p-swarm-derive", "log", - "rand 0.8.5", + "rand", "smallvec", "tokio", "void", @@ -5903,7 +5610,7 @@ dependencies = [ "libsecp256k1-core", "libsecp256k1-gen-ecmult", "libsecp256k1-gen-genmult", - "rand 0.8.5", + "rand", "serde", "sha2 0.9.9", "typenum", @@ -5973,26 +5680,6 @@ dependencies = [ "linked-hash-map", ] -[[package]] -name = "linkme" -version = "0.3.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccb76662d78edc9f9bf56360d6919bdacc8b7761227727e5082f128eeb90bbf5" -dependencies = [ - "linkme-impl", -] - -[[package]] -name = "linkme-impl" -version = "0.3.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8dccda732e04fa3baf2e17cf835bfe2601c7c2edafd64417c627dabae3a8cda" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.58", -] - [[package]] name = "linregress" version = "0.5.3" @@ -6264,18 +5951,6 @@ dependencies = [ "hash-db", ] -[[package]] -name = "merlin" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e261cf0f8b3c42ded9f7d2bb59dea03aa52bc8a1cbc7482f9fc3fd1229d3b42" -dependencies = [ - "byteorder", - "keccak", - "rand_core 0.5.1", - "zeroize", -] - [[package]] name = "merlin" version = "3.0.0" @@ -6295,7 +5970,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69672161530e8aeca1d1400fbf3f1a1747ff60ea604265a4e906c2442df20532" dependencies = [ "futures", - "rand 0.8.5", + "rand", "thrift", ] @@ -6342,7 +6017,7 @@ dependencies = [ "lioness", "log", "parking_lot 0.12.1", - "rand 0.8.5", + "rand", "rand_chacha 0.3.1", "rand_distr", "subtle 2.5.0", @@ -6365,9 +6040,9 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-consensus-beefy", - "sp-core 29.0.0", + "sp-core", "sp-mmr-primitives", - "sp-runtime 32.0.0", + "sp-runtime", ] [[package]] @@ -6381,9 +6056,9 @@ dependencies = [ "serde", "sp-api", "sp-blockchain", - "sp-core 29.0.0", + "sp-core", "sp-mmr-primitives", - "sp-runtime 32.0.0", + "sp-runtime", ] [[package]] @@ -6514,7 +6189,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.109", - "synstructure 0.12.6", + "synstructure", ] [[package]] @@ -6539,7 +6214,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.109", - "synstructure 0.12.6", + "synstructure", ] [[package]] @@ -6595,7 +6270,7 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7bddcd3bf5144b6392de80e04c347cd7fab2508f6df16a85fc496ecd5cec39bc" dependencies = [ - "rand 0.8.5", + "rand", ] [[package]] @@ -6923,11 +6598,11 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-arithmetic", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -6942,8 +6617,8 @@ dependencies = [ "pallet-transaction-payment", "parity-scale-codec", "scale-info", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -6957,9 +6632,9 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-runtime", + "sp-std", ] [[package]] @@ -6975,10 +6650,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -6993,9 +6668,9 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-runtime", + "sp-std", ] [[package]] @@ -7010,10 +6685,10 @@ dependencies = [ "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-consensus-aura", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -7027,10 +6702,10 @@ dependencies = [ "pallet-session", "parity-scale-codec", "scale-info", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-authority-discovery", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -7044,8 +6719,8 @@ dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", "scale-info", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -7063,14 +6738,14 @@ dependencies = [ "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-consensus-babe", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-io", + "sp-runtime", "sp-session", "sp-staking", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -7089,18 +6764,18 @@ dependencies = [ "pallet-balances", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-tracing 16.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", + "sp-tracing", ] [[package]] name = "pallet-balances" -version = "29.0.1" +version = "29.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e27946a57494d7c6231ae8909275bbd3cb5460ee3d27b7a5774a8b8e64d3ab92" +checksum = "a9a54b5d0c7c4c3731883d6b1ac18aff44db20c3d0a3470c8861001a17afdc85" dependencies = [ "docify", "frame-benchmarking", @@ -7109,8 +6784,8 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -7128,10 +6803,10 @@ dependencies = [ "scale-info", "serde", "sp-consensus-beefy", - "sp-runtime 32.0.0", + "sp-runtime", "sp-session", "sp-staking", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -7153,11 +6828,11 @@ dependencies = [ "serde", "sp-api", "sp-consensus-beefy", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-state-machine", + "sp-std", ] [[package]] @@ -7173,10 +6848,10 @@ dependencies = [ "pallet-treasury", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7196,9 +6871,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-consensus-grandpa", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-trie 30.0.0", + "sp-runtime", + "sp-std", + "sp-trie", ] [[package]] @@ -7216,8 +6891,8 @@ dependencies = [ "num-traits", "parity-scale-codec", "scale-info", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -7237,9 +6912,9 @@ dependencies = [ "pallet-bridge-grandpa", "parity-scale-codec", "scale-info", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-trie 30.0.0", + "sp-runtime", + "sp-std", + "sp-trie", ] [[package]] @@ -7258,27 +6933,28 @@ dependencies = [ "pallet-bridge-messages", "parity-scale-codec", "scale-info", - "sp-arithmetic 24.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-arithmetic", + "sp-runtime", + "sp-std", ] [[package]] name = "pallet-broker" -version = "0.7.0" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd8cfe04e8c3f9ca8342ac785f2b1aee6140e1809546fc6f3a99fad20a8dfbf9" +checksum = "574c52fd629191c374c24a18036acac008ea92142309e5dd05e7f03149a667c3" dependencies = [ "bitvec", "frame-benchmarking", "frame-support", "frame-system", + "log", "parity-scale-codec", "scale-info", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-arithmetic", + "sp-core", + "sp-runtime", + "sp-std", ] [[package]] @@ -7295,30 +6971,31 @@ dependencies = [ "pallet-treasury", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] name = "pallet-collator-selection" -version = "10.0.0" +version = "10.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59b5ad46601c613396e92292a24c5b5d76e904c456ece9deb10913f6ea2e2999" +checksum = "49d1157d9a4b7966040158a7b4f1fb29f0cefa8deb6eb9b3452df7ce4161a31c" dependencies = [ "frame-benchmarking", "frame-support", "frame-system", "log", "pallet-authorship", + "pallet-balances", "pallet-session", "parity-scale-codec", - "rand 0.8.5", + "rand", "scale-info", - "sp-runtime 32.0.0", + "sp-runtime", "sp-staking", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -7333,10 +7010,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7356,16 +7033,16 @@ dependencies = [ "pallet-contracts-proc-macro", "pallet-contracts-uapi", "parity-scale-codec", - "rand 0.8.5", + "rand", "rand_pcg", "scale-info", "serde", "smallvec", "sp-api", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", "staging-xcm", "staging-xcm-builder", "wasm-instrument", @@ -7396,17 +7073,6 @@ dependencies = [ "scale-info", ] -[[package]] -name = "pallet-contracts-uapi-next" -version = "6.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd549c16296ea5b2eb7c65c56aba548b286c1be4d7675b424ff6ccb8319c97a9" -dependencies = [ - "bitflags 1.3.2", - "paste", - "polkavm-derive", -] - [[package]] name = "pallet-conviction-voting" version = "29.0.0" @@ -7420,9 +7086,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7438,10 +7104,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7457,14 +7123,14 @@ dependencies = [ "log", "pallet-election-provider-support-benchmarking", "parity-scale-codec", - "rand 0.8.5", + "rand", "scale-info", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", - "sp-io 31.0.0", + "sp-arithmetic", + "sp-core", + "sp-io", "sp-npos-elections", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", "strum 0.24.1", ] @@ -7479,8 +7145,8 @@ dependencies = [ "frame-system", "parity-scale-codec", "sp-npos-elections", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -7495,12 +7161,12 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", + "sp-core", + "sp-io", "sp-npos-elections", - "sp-runtime 32.0.0", + "sp-runtime", "sp-staking", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -7517,10 +7183,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-io 31.0.0", - "sp-runtime 32.0.0", + "sp-io", + "sp-runtime", "sp-staking", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -7537,21 +7203,21 @@ dependencies = [ "pallet-session", "parity-scale-codec", "scale-info", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-consensus-grandpa", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-io", + "sp-runtime", "sp-session", "sp-staking", - "sp-std 14.0.0", + "sp-std", ] [[package]] name = "pallet-identity" -version = "29.0.0" +version = "29.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9e1cae19e30e7dc822c419988b30bb1318d79a8d5da92733822d0e84fe760ca" +checksum = "452bba25325b7f0148eeecbde13e7c26dfb677ad46b3f160b359d7643b44c94b" dependencies = [ "enumflags2", "frame-benchmarking", @@ -7560,9 +7226,9 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7578,12 +7244,12 @@ dependencies = [ "pallet-authorship", "parity-scale-codec", "scale-info", - "sp-application-crypto 31.0.0", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", + "sp-application-crypto", + "sp-core", + "sp-io", + "sp-runtime", "sp-staking", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -7597,11 +7263,11 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", + "sp-core", + "sp-io", "sp-keyring", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -7616,10 +7282,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7635,12 +7301,12 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-weights 28.0.0", + "sp-arithmetic", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", + "sp-weights", ] [[package]] @@ -7655,11 +7321,11 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", + "sp-core", + "sp-io", "sp-mmr-primitives", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -7674,9 +7340,9 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7693,8 +7359,8 @@ dependencies = [ "pallet-nfts", "parity-scale-codec", "scale-info", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -7710,10 +7376,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7725,7 +7391,7 @@ dependencies = [ "pallet-nfts", "parity-scale-codec", "sp-api", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -7739,10 +7405,10 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-arithmetic", + "sp-core", + "sp-runtime", + "sp-std", ] [[package]] @@ -7757,12 +7423,12 @@ dependencies = [ "pallet-balances", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-io", + "sp-runtime", "sp-staking", - "sp-std 14.0.0", - "sp-tracing 16.0.0", + "sp-std", + "sp-tracing", ] [[package]] @@ -7780,10 +7446,10 @@ dependencies = [ "pallet-staking", "parity-scale-codec", "scale-info", - "sp-runtime 32.0.0", - "sp-runtime-interface 25.0.0", + "sp-runtime", + "sp-runtime-interface", "sp-staking", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -7795,7 +7461,7 @@ dependencies = [ "pallet-nomination-pools", "parity-scale-codec", "sp-api", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -7811,9 +7477,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-runtime 32.0.0", + "sp-runtime", "sp-staking", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -7836,9 +7502,9 @@ dependencies = [ "pallet-staking", "parity-scale-codec", "scale-info", - "sp-runtime 32.0.0", + "sp-runtime", "sp-staking", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -7853,10 +7519,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7870,9 +7536,9 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7888,11 +7554,11 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-arithmetic", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7906,9 +7572,9 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7925,10 +7591,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-arithmetic 24.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-arithmetic", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7941,10 +7607,10 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -7960,10 +7626,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-weights 28.0.0", + "sp-io", + "sp-runtime", + "sp-std", + "sp-weights", ] [[package]] @@ -7979,14 +7645,14 @@ dependencies = [ "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-io", + "sp-runtime", "sp-session", "sp-staking", - "sp-state-machine 0.36.0", - "sp-std 14.0.0", - "sp-trie 30.0.0", + "sp-state-machine", + "sp-std", + "sp-trie", ] [[package]] @@ -8001,10 +7667,10 @@ dependencies = [ "pallet-session", "pallet-staking", "parity-scale-codec", - "rand 0.8.5", - "sp-runtime 32.0.0", + "rand", + "sp-runtime", "sp-session", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -8020,17 +7686,17 @@ dependencies = [ "parity-scale-codec", "rand_chacha 0.2.2", "scale-info", - "sp-arithmetic 24.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-arithmetic", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] name = "pallet-staking" -version = "29.0.2" +version = "29.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "668b7d28c499f0d9f295fad26cf6c342472e21842e3b13bcaaac8536358b2d6c" +checksum = "061b00814eb794a40df4eca7972a7c67b26473cd85cc7c54f5816ae49ad6e11b" dependencies = [ "frame-benchmarking", "frame-election-provider-support", @@ -8043,11 +7709,11 @@ dependencies = [ "rand_chacha 0.2.2", "scale-info", "serde", - "sp-application-crypto 31.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", + "sp-application-crypto", + "sp-io", + "sp-runtime", "sp-staking", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -8069,7 +7735,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "505d45e08bad052f55fb51f00a6b6244d23ee46ffdc8091f6cddf4e3a880319d" dependencies = [ "log", - "sp-arithmetic 24.0.0", + "sp-arithmetic", ] [[package]] @@ -8095,10 +7761,10 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -8113,9 +7779,9 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -8132,10 +7798,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-inherents", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-storage 20.0.0", + "sp-io", + "sp-runtime", + "sp-std", + "sp-storage", "sp-timestamp", ] @@ -8153,27 +7819,27 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] name = "pallet-transaction-payment" -version = "29.0.0" +version = "29.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39f690f5c287ad34b28ca951ef7fae80b08cc9218d970723b7a70e4d29396872" +checksum = "0b0c408252aefe10cff96af1e54f06f45cb0dd184b4e450e9a2ecf837dfe506e" dependencies = [ "frame-support", "frame-system", "parity-scale-codec", "scale-info", "serde", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -8187,10 +7853,10 @@ dependencies = [ "parity-scale-codec", "sp-api", "sp-blockchain", - "sp-core 29.0.0", + "sp-core", "sp-rpc", - "sp-runtime 32.0.0", - "sp-weights 28.0.0", + "sp-runtime", + "sp-weights", ] [[package]] @@ -8202,15 +7868,15 @@ dependencies = [ "pallet-transaction-payment", "parity-scale-codec", "sp-api", - "sp-runtime 32.0.0", - "sp-weights 28.0.0", + "sp-runtime", + "sp-weights", ] [[package]] name = "pallet-treasury" -version = "28.0.0" +version = "28.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1605eb5083a2cd172544f33c6e59eca2e23ac49f02f13d1562b1b8a409df9c60" +checksum = "3eca44990d0d759213744f2d1f6fe1fadec1079a3e4e4da40556d6b4e42abbcd" dependencies = [ "docify", "frame-benchmarking", @@ -8221,9 +7887,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-runtime", + "sp-std", ] [[package]] @@ -8238,8 +7904,8 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -8253,10 +7919,10 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -8271,8 +7937,8 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -8287,15 +7953,15 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] name = "pallet-xcm" -version = "8.0.1" +version = "8.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b0bade2eb6ce40af35a5af150692b4e150638f7f68c15735ab9cdf79650d68e" +checksum = "ba9138b04168b07b1aff4a2079f5514753c31dddba40e5fb471b9cda7da27ad6" dependencies = [ "bounded-collections 0.2.0", "frame-benchmarking", @@ -8306,10 +7972,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -8327,9 +7993,9 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-io", + "sp-runtime", + "sp-std", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -8348,9 +8014,9 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-runtime", + "sp-std", "staging-xcm", "staging-xcm-builder", ] @@ -8377,10 +8043,10 @@ dependencies = [ "polkadot-primitives", "scale-info", "sp-consensus-aura", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", "staging-parachain-info", "staging-xcm", "staging-xcm-executor", @@ -8407,11 +8073,11 @@ dependencies = [ "parity-scale-codec", "polkadot-parachain-primitives", "sp-consensus-aura", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-tracing 16.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", + "sp-tracing", "staging-parachain-info", "staging-xcm", "staging-xcm-executor", @@ -8439,7 +8105,7 @@ dependencies = [ "lz4", "memmap2 0.5.10", "parking_lot 0.12.1", - "rand 0.8.5", + "rand", "siphasher 0.3.11", "snap", "winapi", @@ -8504,7 +8170,7 @@ checksum = "f557c32c6d268a07c921471619c0295f5efad3a0e76d4f97a05c091a51d110b2" dependencies = [ "proc-macro2", "syn 1.0.109", - "synstructure 0.12.6", + "synstructure", ] [[package]] @@ -8590,18 +8256,9 @@ dependencies = [ [[package]] name = "pbkdf2" -version = "0.11.0" +version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83a0692ec44e4cf1ef28ca317f14f8f07da2d95ec3fa01f86e4467b725e60917" -dependencies = [ - "digest 0.10.7", -] - -[[package]] -name = "pbkdf2" -version = "0.12.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2" +checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2" dependencies = [ "digest 0.10.7", ] @@ -8770,7 +8427,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "rand 0.8.5", + "rand", "tracing-gum", ] @@ -8787,7 +8444,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "rand 0.8.5", + "rand", "tracing-gum", ] @@ -8807,10 +8464,10 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "rand 0.8.5", + "rand", "schnellru", - "sp-core 29.0.0", - "sp-keystore 0.35.0", + "sp-core", + "sp-keystore", "thiserror", "tracing-gum", ] @@ -8831,7 +8488,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "rand 0.8.5", + "rand", "sc-network", "schnellru", "thiserror", @@ -8859,8 +8516,8 @@ dependencies = [ "sc-storage-monitor", "sc-sysinfo", "sc-tracing", - "sp-core 29.0.0", - "sp-io 31.0.0", + "sp-core", + "sp-io", "sp-keyring", "sp-maybe-compressed-blob", "substrate-build-script-utils", @@ -8883,9 +8540,9 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "sp-core 29.0.0", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-keystore", + "sp-runtime", "thiserror", "tokio-util", "tracing-gum", @@ -8899,9 +8556,9 @@ checksum = "b6a08e4e014c853b252ecbbe3ccd67b2d33d78e46988d309b8cccf4ac06e25ef" dependencies = [ "parity-scale-codec", "scale-info", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-runtime", + "sp-std", ] [[package]] @@ -8924,8 +8581,8 @@ dependencies = [ "polkadot-primitives", "sc-network", "schnellru", - "sp-application-crypto 31.0.0", - "sp-keystore 0.35.0", + "sp-application-crypto", + "sp-keystore", "thiserror", "tracing-gum", ] @@ -8940,8 +8597,8 @@ dependencies = [ "polkadot-node-primitives", "polkadot-primitives", "reed-solomon-novelpoly", - "sp-core 29.0.0", - "sp-trie 30.0.0", + "sp-core", + "sp-trie", "thiserror", ] @@ -8957,14 +8614,14 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "rand 0.8.5", + "rand", "rand_chacha 0.3.1", "sc-network", "sc-network-common", - "sp-application-crypto 31.0.0", - "sp-core 29.0.0", + "sp-application-crypto", + "sp-core", "sp-crypto-hashing", - "sp-keystore 0.35.0", + "sp-keystore", "tracing-gum", ] @@ -9005,7 +8662,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "sp-core 29.0.0", + "sp-core", "sp-maybe-compressed-blob", "thiserror", "tracing-gum", @@ -9023,7 +8680,7 @@ dependencies = [ "futures-timer", "itertools 0.10.5", "kvdb", - "merlin 3.0.0", + "merlin", "parity-scale-codec", "polkadot-node-jaeger", "polkadot-node-primitives", @@ -9031,16 +8688,16 @@ dependencies = [ "polkadot-node-subsystem-util", "polkadot-overseer", "polkadot-primitives", - "rand 0.8.5", + "rand", "rand_chacha 0.3.1", "rand_core 0.6.4", "sc-keystore", "schnellru", "schnorrkel 0.11.4", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-consensus", "sp-consensus-slots", - "sp-runtime 32.0.0", + "sp-runtime", "thiserror", "tracing-gum", ] @@ -9084,7 +8741,7 @@ dependencies = [ "polkadot-primitives", "polkadot-statement-table", "schnellru", - "sp-keystore 0.35.0", + "sp-keystore", "thiserror", "tracing-gum", ] @@ -9099,7 +8756,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "sp-keystore 0.35.0", + "sp-keystore", "thiserror", "tracing-gum", "wasm-timer", @@ -9257,11 +8914,11 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-parachain-primitives", "polkadot-primitives", - "rand 0.8.5", + "rand", "slotmap", - "sp-core 29.0.0", + "sp-core", "sp-maybe-compressed-blob", - "sp-wasm-interface 20.0.0", + "sp-wasm-interface", "tempfile", "thiserror", "tokio", @@ -9280,7 +8937,7 @@ dependencies = [ "polkadot-node-subsystem-util", "polkadot-overseer", "polkadot-primitives", - "sp-keystore 0.35.0", + "sp-keystore", "thiserror", "tracing-gum", ] @@ -9304,11 +8961,11 @@ dependencies = [ "sc-executor-common", "sc-executor-wasmtime", "seccompiler", - "sp-core 29.0.0", + "sp-core", "sp-crypto-hashing", - "sp-externalities 0.26.0", - "sp-io 31.0.0", - "sp-tracing 16.0.0", + "sp-externalities", + "sp-io", + "sp-tracing", "thiserror", "tracing-gum", ] @@ -9343,7 +9000,7 @@ dependencies = [ "polkadot-node-primitives", "polkadot-primitives", "sc-network", - "sp-core 29.0.0", + "sp-core", "thiserror", "tokio", ] @@ -9385,7 +9042,7 @@ dependencies = [ "polkadot-node-jaeger", "polkadot-node-primitives", "polkadot-primitives", - "rand 0.8.5", + "rand", "sc-authority-discovery", "sc-network", "strum 0.24.1", @@ -9407,12 +9064,12 @@ dependencies = [ "polkadot-primitives", "schnorrkel 0.11.4", "serde", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-consensus-babe", - "sp-core 29.0.0", - "sp-keystore 0.35.0", + "sp-core", + "sp-keystore", "sp-maybe-compressed-blob", - "sp-runtime 32.0.0", + "sp-runtime", "thiserror", "zstd 0.12.4", ] @@ -9452,7 +9109,7 @@ dependencies = [ "sp-authority-discovery", "sp-blockchain", "sp-consensus-babe", - "sp-runtime 32.0.0", + "sp-runtime", "substrate-prometheus-endpoint", "thiserror", ] @@ -9483,12 +9140,12 @@ dependencies = [ "polkadot-overseer", "polkadot-primitives", "prioritized-metered-channel", - "rand 0.8.5", + "rand", "sc-client-api", "schnellru", - "sp-application-crypto 31.0.0", - "sp-core 29.0.0", - "sp-keystore 0.35.0", + "sp-application-crypto", + "sp-core", + "sp-keystore", "thiserror", "tracing-gum", ] @@ -9511,7 +9168,7 @@ dependencies = [ "polkadot-primitives", "sc-client-api", "sp-api", - "sp-core 29.0.0", + "sp-core", "tikv-jemalloc-ctl", "tracing-gum", ] @@ -9528,10 +9185,10 @@ dependencies = [ "polkadot-core-primitives", "scale-info", "serde", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-weights 28.0.0", + "sp-core", + "sp-runtime", + "sp-std", + "sp-weights", ] [[package]] @@ -9549,17 +9206,17 @@ dependencies = [ "scale-info", "serde", "sp-api", - "sp-application-crypto 31.0.0", - "sp-arithmetic 24.0.0", + "sp-application-crypto", + "sp-arithmetic", "sp-authority-discovery", "sp-consensus-slots", - "sp-core 29.0.0", + "sp-core", "sp-inherents", - "sp-io 31.0.0", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", + "sp-io", + "sp-keystore", + "sp-runtime", "sp-staking", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -9590,17 +9247,17 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-consensus-babe", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", + "sp-keystore", + "sp-runtime", "substrate-frame-rpc-system", "substrate-state-trie-migration-rpc", ] [[package]] name = "polkadot-runtime-common" -version = "8.0.1" +version = "8.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06afbb3bd10245ad1907242a98ddffc3c0c1e209738b8382bc5bcfc1f28c0429" +checksum = "d815f0ff0a69dce7235d42c6e7d5e2b8b7429cba1252b4802ddc7879e2e74d4a" dependencies = [ "bitvec", "frame-benchmarking", @@ -9634,14 +9291,14 @@ dependencies = [ "serde_derive", "slot-range-helper", "sp-api", - "sp-core 29.0.0", + "sp-core", "sp-inherents", - "sp-io 31.0.0", + "sp-io", "sp-npos-elections", - "sp-runtime 32.0.0", + "sp-runtime", "sp-session", "sp-staking", - "sp-std 14.0.0", + "sp-std", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -9658,15 +9315,15 @@ dependencies = [ "frame-benchmarking", "parity-scale-codec", "polkadot-primitives", - "sp-std 14.0.0", - "sp-tracing 16.0.0", + "sp-std", + "sp-tracing", ] [[package]] name = "polkadot-runtime-parachains" -version = "8.0.1" +version = "8.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bcfd672be236fd1c38c702e7e99fe3f3e54df0ddb8127e542423221d1f50669" +checksum = "b8d37cd3e014b06daf396d1483b5327782a0ebadc816423419665166b75b3e3e" dependencies = [ "bitflags 1.3.2", "bitvec", @@ -9691,22 +9348,22 @@ dependencies = [ "polkadot-parachain-primitives", "polkadot-primitives", "polkadot-runtime-metrics", - "rand 0.8.5", + "rand", "rand_chacha 0.3.1", "rustc-hex", "scale-info", "serde", "sp-api", - "sp-application-crypto 31.0.0", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", + "sp-application-crypto", + "sp-arithmetic", + "sp-core", "sp-inherents", - "sp-io 31.0.0", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", + "sp-io", + "sp-keystore", + "sp-runtime", "sp-session", "sp-staking", - "sp-std 14.0.0", + "sp-std", "staging-xcm", "staging-xcm-executor", "static_assertions", @@ -9809,21 +9466,21 @@ dependencies = [ "sp-consensus-babe", "sp-consensus-beefy", "sp-consensus-grandpa", - "sp-core 29.0.0", + "sp-core", "sp-inherents", - "sp-io 31.0.0", + "sp-io", "sp-keyring", - "sp-keystore 0.35.0", + "sp-keystore", "sp-mmr-primitives", "sp-offchain", - "sp-runtime 32.0.0", + "sp-runtime", "sp-session", - "sp-state-machine 0.36.0", - "sp-storage 20.0.0", + "sp-state-machine", + "sp-storage", "sp-timestamp", "sp-transaction-pool", "sp-version", - "sp-weights 28.0.0", + "sp-weights", "substrate-prometheus-endpoint", "thiserror", "tracing-gum", @@ -9848,7 +9505,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "sp-keystore 0.35.0", + "sp-keystore", "sp-staking", "thiserror", "tracing-gum", @@ -9862,7 +9519,7 @@ checksum = "de5e010da3c6a65d8f263d0f825a04d995ffc8a37f886f674fcbbc73bf158d01" dependencies = [ "parity-scale-codec", "polkadot-primitives", - "sp-core 29.0.0", + "sp-core", "tracing-gum", ] @@ -9948,19 +9605,6 @@ dependencies = [ "universal-hash", ] -[[package]] -name = "pop-api" -version = "0.0.0" -dependencies = [ - "enumflags2", - "ink", - "parity-scale-codec", - "pop-primitives", - "scale-info", - "sp-io 23.0.0", - "sp-runtime 24.0.0", -] - [[package]] name = "pop-node" version = "0.1.0-alpha" @@ -10011,11 +9655,11 @@ dependencies = [ "sp-block-builder", "sp-blockchain", "sp-consensus-aura", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-keystore 0.35.0", + "sp-core", + "sp-io", + "sp-keystore", "sp-offchain", - "sp-runtime 32.0.0", + "sp-runtime", "sp-session", "sp-timestamp", "sp-transaction-pool", @@ -10045,8 +9689,8 @@ dependencies = [ "parity-scale-codec", "polkadot-primitives", "scale-info", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -10098,7 +9742,6 @@ dependencies = [ "parity-scale-codec", "polkadot-parachain-primitives", "polkadot-runtime-common", - "pop-api", "pop-primitives", "pop-runtime-common", "scale-info", @@ -10106,14 +9749,14 @@ dependencies = [ "sp-api", "sp-block-builder", "sp-consensus-aura", - "sp-core 29.0.0", + "sp-core", "sp-genesis-builder", "sp-inherents", - "sp-io 31.0.0", + "sp-io", "sp-offchain", - "sp-runtime 32.0.0", + "sp-runtime", "sp-session", - "sp-std 14.0.0", + "sp-std", "sp-transaction-pool", "sp-version", "staging-parachain-info", @@ -10179,14 +9822,14 @@ dependencies = [ "sp-api", "sp-block-builder", "sp-consensus-aura", - "sp-core 29.0.0", + "sp-core", "sp-genesis-builder", "sp-inherents", - "sp-io 31.0.0", + "sp-io", "sp-offchain", - "sp-runtime 32.0.0", + "sp-runtime", "sp-session", - "sp-std 14.0.0", + "sp-std", "sp-transaction-pool", "sp-version", "staging-parachain-info", @@ -10546,7 +10189,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94b0b33c13a79f669c85defaf4c275dc86a0c0372807d0ca3d78e0bb87274863" dependencies = [ "bytes", - "rand 0.8.5", + "rand", "ring 0.16.20", "rustc-hash", "rustls 0.20.9", @@ -10572,19 +10215,6 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" -[[package]] -name = "rand" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" -dependencies = [ - "getrandom 0.1.16", - "libc", - "rand_chacha 0.2.2", - "rand_core 0.5.1", - "rand_hc", -] - [[package]] name = "rand" version = "0.8.5" @@ -10641,16 +10271,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32cb0b9bc82b0a0876c2dd994a7e7a2683d3e7390ca40e6886785ef0c7e3ee31" dependencies = [ "num-traits", - "rand 0.8.5", -] - -[[package]] -name = "rand_hc" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" -dependencies = [ - "rand_core 0.5.1", + "rand", ] [[package]] @@ -10876,12 +10497,6 @@ dependencies = [ "digest 0.10.7", ] -[[package]] -name = "rlibc" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc874b127765f014d792f16763a81245ab80500e2ad921ed4ee9e82481ee08fe" - [[package]] name = "rlp" version = "0.5.2" @@ -10974,22 +10589,22 @@ dependencies = [ "serde_derive", "smallvec", "sp-api", - "sp-arithmetic 24.0.0", + "sp-arithmetic", "sp-authority-discovery", "sp-block-builder", "sp-consensus-babe", "sp-consensus-beefy", - "sp-core 29.0.0", + "sp-core", "sp-genesis-builder", "sp-inherents", - "sp-io 31.0.0", + "sp-io", "sp-mmr-primitives", "sp-offchain", - "sp-runtime 32.0.0", + "sp-runtime", "sp-session", "sp-staking", - "sp-std 14.0.0", - "sp-storage 20.0.0", + "sp-std", + "sp-storage", "sp-transaction-pool", "sp-version", "staging-xcm", @@ -11009,9 +10624,9 @@ dependencies = [ "polkadot-primitives", "polkadot-runtime-common", "smallvec", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-weights 28.0.0", + "sp-core", + "sp-runtime", + "sp-weights", "staging-xcm", "staging-xcm-builder", ] @@ -11314,8 +10929,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "357127c91373ed6d1ae582f6e3300ab5b13bcde43bbf270a891f44194ef48b70" dependencies = [ "log", - "sp-core 29.0.0", - "sp-wasm-interface 20.0.0", + "sp-core", + "sp-wasm-interface", "thiserror", ] @@ -11336,15 +10951,15 @@ dependencies = [ "parity-scale-codec", "prost 0.12.3", "prost-build", - "rand 0.8.5", + "rand", "sc-client-api", "sc-network", "sp-api", "sp-authority-discovery", "sp-blockchain", - "sp-core 29.0.0", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-keystore", + "sp-runtime", "substrate-prometheus-endpoint", "thiserror", ] @@ -11366,9 +10981,9 @@ dependencies = [ "sp-api", "sp-blockchain", "sp-consensus", - "sp-core 29.0.0", + "sp-core", "sp-inherents", - "sp-runtime 32.0.0", + "sp-runtime", "substrate-prometheus-endpoint", ] @@ -11382,10 +10997,10 @@ dependencies = [ "sp-api", "sp-block-builder", "sp-blockchain", - "sp-core 29.0.0", + "sp-core", "sp-inherents", - "sp-runtime 32.0.0", - "sp-trie 30.0.0", + "sp-runtime", + "sp-trie", ] [[package]] @@ -11407,12 +11022,12 @@ dependencies = [ "serde", "serde_json", "sp-blockchain", - "sp-core 29.0.0", + "sp-core", "sp-crypto-hashing", "sp-genesis-builder", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", + "sp-io", + "sp-runtime", + "sp-state-machine", ] [[package]] @@ -11444,7 +11059,7 @@ dependencies = [ "log", "names", "parity-scale-codec", - "rand 0.8.5", + "rand", "regex", "rpassword", "sc-client-api", @@ -11459,11 +11074,11 @@ dependencies = [ "serde", "serde_json", "sp-blockchain", - "sp-core 29.0.0", + "sp-core", "sp-keyring", - "sp-keystore 0.35.0", - "sp-panic-handler 13.0.0", - "sp-runtime 32.0.0", + "sp-keystore", + "sp-panic-handler", + "sp-runtime", "sp-version", "thiserror", "tokio", @@ -11486,14 +11101,14 @@ dependencies = [ "sp-api", "sp-blockchain", "sp-consensus", - "sp-core 29.0.0", + "sp-core", "sp-database", - "sp-externalities 0.26.0", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", + "sp-externalities", + "sp-runtime", + "sp-state-machine", "sp-statement-store", - "sp-storage 20.0.0", - "sp-trie 30.0.0", + "sp-storage", + "sp-trie", "substrate-prometheus-endpoint", ] @@ -11515,13 +11130,13 @@ dependencies = [ "sc-client-api", "sc-state-db", "schnellru", - "sp-arithmetic 24.0.0", + "sp-arithmetic", "sp-blockchain", - "sp-core 29.0.0", + "sp-core", "sp-database", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", - "sp-trie 30.0.0", + "sp-runtime", + "sp-state-machine", + "sp-trie", ] [[package]] @@ -11543,9 +11158,9 @@ dependencies = [ "sp-api", "sp-blockchain", "sp-consensus", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", + "sp-core", + "sp-runtime", + "sp-state-machine", "substrate-prometheus-endpoint", "thiserror", ] @@ -11566,16 +11181,16 @@ dependencies = [ "sc-consensus-slots", "sc-telemetry", "sp-api", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-block-builder", "sp-blockchain", "sp-consensus", "sp-consensus-aura", "sp-consensus-slots", - "sp-core 29.0.0", + "sp-core", "sp-inherents", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", + "sp-keystore", + "sp-runtime", "substrate-prometheus-endpoint", "thiserror", ] @@ -11602,17 +11217,17 @@ dependencies = [ "sc-telemetry", "sc-transaction-pool-api", "sp-api", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-block-builder", "sp-blockchain", "sp-consensus", "sp-consensus-babe", "sp-consensus-slots", - "sp-core 29.0.0", + "sp-core", "sp-crypto-hashing", "sp-inherents", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", + "sp-keystore", + "sp-runtime", "substrate-prometheus-endpoint", "thiserror", ] @@ -11630,13 +11245,13 @@ dependencies = [ "sc-rpc-api", "serde", "sp-api", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-blockchain", "sp-consensus", "sp-consensus-babe", - "sp-core 29.0.0", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-keystore", + "sp-runtime", "thiserror", ] @@ -11661,16 +11276,16 @@ dependencies = [ "sc-network-sync", "sc-utils", "sp-api", - "sp-application-crypto 31.0.0", - "sp-arithmetic 24.0.0", + "sp-application-crypto", + "sp-arithmetic", "sp-blockchain", "sp-consensus", "sp-consensus-beefy", - "sp-core 29.0.0", + "sp-core", "sp-crypto-hashing", - "sp-keystore 0.35.0", + "sp-keystore", "sp-mmr-primitives", - "sp-runtime 32.0.0", + "sp-runtime", "substrate-prometheus-endpoint", "thiserror", "tokio", @@ -11692,8 +11307,8 @@ dependencies = [ "sc-rpc", "serde", "sp-consensus-beefy", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", "thiserror", ] @@ -11708,7 +11323,7 @@ dependencies = [ "sc-client-api", "sc-consensus", "sp-blockchain", - "sp-runtime 32.0.0", + "sp-runtime", ] [[package]] @@ -11728,7 +11343,7 @@ dependencies = [ "log", "parity-scale-codec", "parking_lot 0.12.1", - "rand 0.8.5", + "rand", "sc-block-builder", "sc-chain-spec", "sc-client-api", @@ -11742,15 +11357,15 @@ dependencies = [ "sc-utils", "serde_json", "sp-api", - "sp-application-crypto 31.0.0", - "sp-arithmetic 24.0.0", + "sp-application-crypto", + "sp-arithmetic", "sp-blockchain", "sp-consensus", "sp-consensus-grandpa", - "sp-core 29.0.0", + "sp-core", "sp-crypto-hashing", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", + "sp-keystore", + "sp-runtime", "substrate-prometheus-endpoint", "thiserror", ] @@ -11771,8 +11386,8 @@ dependencies = [ "sc-rpc", "serde", "sp-blockchain", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", "thiserror", ] @@ -11790,14 +11405,14 @@ dependencies = [ "sc-client-api", "sc-consensus", "sc-telemetry", - "sp-arithmetic 24.0.0", + "sp-arithmetic", "sp-blockchain", "sp-consensus", "sp-consensus-slots", - "sp-core 29.0.0", + "sp-core", "sp-inherents", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", + "sp-runtime", + "sp-state-machine", ] [[package]] @@ -11812,14 +11427,14 @@ dependencies = [ "sc-executor-wasmtime", "schnellru", "sp-api", - "sp-core 29.0.0", - "sp-externalities 0.26.0", - "sp-io 31.0.0", - "sp-panic-handler 13.0.0", - "sp-runtime-interface 25.0.0", - "sp-trie 30.0.0", + "sp-core", + "sp-externalities", + "sp-io", + "sp-panic-handler", + "sp-runtime-interface", + "sp-trie", "sp-version", - "sp-wasm-interface 20.0.0", + "sp-wasm-interface", "tracing", ] @@ -11831,7 +11446,7 @@ checksum = "07498138dee3ddf2c71299ca372d8449880bb3a8a8a299a483094e9c26b0823e" dependencies = [ "sc-allocator", "sp-maybe-compressed-blob", - "sp-wasm-interface 20.0.0", + "sp-wasm-interface", "thiserror", "wasm-instrument", ] @@ -11850,8 +11465,8 @@ dependencies = [ "rustix 0.36.17", "sc-allocator", "sc-executor-common", - "sp-runtime-interface 25.0.0", - "sp-wasm-interface 20.0.0", + "sp-runtime-interface", + "sp-wasm-interface", "wasmtime", ] @@ -11870,7 +11485,7 @@ dependencies = [ "sc-network-common", "sc-network-sync", "sp-blockchain", - "sp-runtime 32.0.0", + "sp-runtime", ] [[package]] @@ -11882,9 +11497,9 @@ dependencies = [ "array-bytes 6.2.2", "parking_lot 0.12.1", "serde_json", - "sp-application-crypto 31.0.0", - "sp-core 29.0.0", - "sp-keystore 0.35.0", + "sp-application-crypto", + "sp-core", + "sp-keystore", "thiserror", ] @@ -11911,10 +11526,10 @@ dependencies = [ "sc-transaction-pool-api", "sp-api", "sp-consensus", - "sp-core 29.0.0", - "sp-keystore 0.35.0", + "sp-core", + "sp-keystore", "sp-mixnet", - "sp-runtime 32.0.0", + "sp-runtime", "thiserror", ] @@ -11942,17 +11557,17 @@ dependencies = [ "parking_lot 0.12.1", "partial_sort", "pin-project", - "rand 0.8.5", + "rand", "sc-client-api", "sc-network-common", "sc-utils", "serde", "serde_json", "smallvec", - "sp-arithmetic 24.0.0", + "sp-arithmetic", "sp-blockchain", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", "substrate-prometheus-endpoint", "thiserror", "tokio", @@ -11978,7 +11593,7 @@ dependencies = [ "sc-client-api", "sc-network", "sp-blockchain", - "sp-runtime 32.0.0", + "sp-runtime", "thiserror", "unsigned-varint", ] @@ -11998,7 +11613,7 @@ dependencies = [ "sc-consensus", "sp-consensus", "sp-consensus-grandpa", - "sp-runtime 32.0.0", + "sp-runtime", ] [[package]] @@ -12016,7 +11631,7 @@ dependencies = [ "sc-network-common", "sc-network-sync", "schnellru", - "sp-runtime 32.0.0", + "sp-runtime", "substrate-prometheus-endpoint", "tracing", ] @@ -12038,8 +11653,8 @@ dependencies = [ "sc-client-api", "sc-network", "sp-blockchain", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", "thiserror", ] @@ -12068,12 +11683,12 @@ dependencies = [ "sc-utils", "schnellru", "smallvec", - "sp-arithmetic 24.0.0", + "sp-arithmetic", "sp-blockchain", "sp-consensus", "sp-consensus-grandpa", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", "substrate-prometheus-endpoint", "thiserror", "tokio", @@ -12096,7 +11711,7 @@ dependencies = [ "sc-network-sync", "sc-utils", "sp-consensus", - "sp-runtime 32.0.0", + "sp-runtime", "substrate-prometheus-endpoint", ] @@ -12119,18 +11734,18 @@ dependencies = [ "once_cell", "parity-scale-codec", "parking_lot 0.12.1", - "rand 0.8.5", + "rand", "sc-client-api", "sc-network", "sc-network-common", "sc-transaction-pool-api", "sc-utils", "sp-api", - "sp-core 29.0.0", - "sp-externalities 0.26.0", - "sp-keystore 0.35.0", + "sp-core", + "sp-externalities", + "sp-keystore", "sp-offchain", - "sp-runtime 32.0.0", + "sp-runtime", "threadpool", "tracing", ] @@ -12167,11 +11782,11 @@ dependencies = [ "serde_json", "sp-api", "sp-blockchain", - "sp-core 29.0.0", - "sp-keystore 0.35.0", + "sp-core", + "sp-keystore", "sp-offchain", "sp-rpc", - "sp-runtime 32.0.0", + "sp-runtime", "sp-session", "sp-statement-store", "sp-version", @@ -12192,9 +11807,9 @@ dependencies = [ "scale-info", "serde", "serde_json", - "sp-core 29.0.0", + "sp-core", "sp-rpc", - "sp-runtime 32.0.0", + "sp-runtime", "sp-version", "thiserror", ] @@ -12237,9 +11852,9 @@ dependencies = [ "serde", "sp-api", "sp-blockchain", - "sp-core 29.0.0", + "sp-core", "sp-rpc", - "sp-runtime 32.0.0", + "sp-runtime", "sp-version", "thiserror", "tokio", @@ -12262,7 +11877,7 @@ dependencies = [ "parity-scale-codec", "parking_lot 0.12.1", "pin-project", - "rand 0.8.5", + "rand", "sc-chain-spec", "sc-client-api", "sc-client-db", @@ -12290,16 +11905,16 @@ dependencies = [ "sp-api", "sp-blockchain", "sp-consensus", - "sp-core 29.0.0", - "sp-externalities 0.26.0", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-externalities", + "sp-keystore", + "sp-runtime", "sp-session", - "sp-state-machine 0.36.0", - "sp-storage 20.0.0", + "sp-state-machine", + "sp-storage", "sp-transaction-pool", "sp-transaction-storage-proof", - "sp-trie 30.0.0", + "sp-trie", "sp-version", "static_init", "substrate-prometheus-endpoint", @@ -12319,7 +11934,7 @@ dependencies = [ "log", "parity-scale-codec", "parking_lot 0.12.1", - "sp-core 29.0.0", + "sp-core", ] [[package]] @@ -12331,7 +11946,7 @@ dependencies = [ "clap", "fs4", "log", - "sp-core 29.0.0", + "sp-core", "thiserror", "tokio", ] @@ -12352,7 +11967,7 @@ dependencies = [ "serde", "serde_json", "sp-blockchain", - "sp-runtime 32.0.0", + "sp-runtime", "thiserror", ] @@ -12366,16 +11981,16 @@ dependencies = [ "futures", "libc", "log", - "rand 0.8.5", + "rand", "rand_pcg", "regex", "sc-telemetry", "serde", "serde_json", - "sp-core 29.0.0", + "sp-core", "sp-crypto-hashing", - "sp-io 31.0.0", - "sp-std 14.0.0", + "sp-io", + "sp-std", ] [[package]] @@ -12390,7 +12005,7 @@ dependencies = [ "log", "parking_lot 0.12.1", "pin-project", - "rand 0.8.5", + "rand", "sc-utils", "serde", "serde_json", @@ -12419,10 +12034,10 @@ dependencies = [ "serde", "sp-api", "sp-blockchain", - "sp-core 29.0.0", + "sp-core", "sp-rpc", - "sp-runtime 32.0.0", - "sp-tracing 16.0.0", + "sp-runtime", + "sp-tracing", "thiserror", "tracing", "tracing-log 0.1.4", @@ -12460,10 +12075,10 @@ dependencies = [ "serde", "sp-api", "sp-blockchain", - "sp-core 29.0.0", + "sp-core", "sp-crypto-hashing", - "sp-runtime 32.0.0", - "sp-tracing 16.0.0", + "sp-runtime", + "sp-tracing", "sp-transaction-pool", "substrate-prometheus-endpoint", "thiserror", @@ -12481,8 +12096,8 @@ dependencies = [ "parity-scale-codec", "serde", "sp-blockchain", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", "thiserror", ] @@ -12499,7 +12114,7 @@ dependencies = [ "log", "parking_lot 0.12.1", "prometheus", - "sp-arithmetic 24.0.0", + "sp-arithmetic", ] [[package]] @@ -12580,7 +12195,6 @@ dependencies = [ "derive_more", "parity-scale-codec", "scale-info-derive", - "schemars", "serde", ] @@ -12638,30 +12252,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "schemars" -version = "0.8.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09c024468a378b7e36765cd36702b7a90cc3cba11654f6685c8f233408e89e92" -dependencies = [ - "dyn-clone", - "schemars_derive", - "serde", - "serde_json", -] - -[[package]] -name = "schemars_derive" -version = "0.8.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1eee588578aff73f856ab961cd2f79e36bc45d7ded33a7562adba4667aecc0e" -dependencies = [ - "proc-macro2", - "quote", - "serde_derive_internals", - "syn 2.0.58", -] - [[package]] name = "schnellru" version = "0.2.1" @@ -12673,24 +12263,6 @@ dependencies = [ "hashbrown 0.13.2", ] -[[package]] -name = "schnorrkel" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "021b403afe70d81eea68f6ea12f6b3c9588e5d536a94c3bf80f15e7faa267862" -dependencies = [ - "arrayref", - "arrayvec 0.5.2", - "curve25519-dalek 2.1.3", - "getrandom 0.1.16", - "merlin 2.0.1", - "rand 0.7.3", - "rand_core 0.5.1", - "sha2 0.8.2", - "subtle 2.5.0", - "zeroize", -] - [[package]] name = "schnorrkel" version = "0.10.2" @@ -12700,7 +12272,7 @@ dependencies = [ "arrayref", "arrayvec 0.7.4", "curve25519-dalek-ng", - "merlin 3.0.0", + "merlin", "rand_core 0.6.4", "sha2 0.9.9", "subtle-ng", @@ -12718,7 +12290,7 @@ dependencies = [ "arrayvec 0.7.4", "curve25519-dalek 4.1.2", "getrandom_or_panic", - "merlin 3.0.0", + "merlin", "rand_core 0.6.4", "serde_bytes", "sha2 0.10.8", @@ -12771,31 +12343,13 @@ dependencies = [ "libc", ] -[[package]] -name = "secp256k1" -version = "0.24.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b1629c9c557ef9b293568b338dddfc8208c98a18c59d722a9d53f859d9c9b62" -dependencies = [ - "secp256k1-sys 0.6.1", -] - [[package]] name = "secp256k1" version = "0.28.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d24b59d129cdadea20aea4fb2352fa053712e5d713eee47d700cd4b2bc002f10" dependencies = [ - "secp256k1-sys 0.9.2", -] - -[[package]] -name = "secp256k1-sys" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83080e2c2fc1006e625be82e5d1eb6a43b7fd9578b617fcc55814daf286bba4b" -dependencies = [ - "cc", + "secp256k1-sys", ] [[package]] @@ -12901,17 +12455,6 @@ dependencies = [ "syn 2.0.58", ] -[[package]] -name = "serde_derive_internals" -version = "0.29.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.58", -] - [[package]] name = "serde_json" version = "1.0.115" @@ -12956,18 +12499,6 @@ dependencies = [ "digest 0.10.7", ] -[[package]] -name = "sha2" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69" -dependencies = [ - "block-buffer 0.7.3", - "digest 0.8.1", - "fake-simd", - "opaque-debug 0.2.3", -] - [[package]] name = "sha2" version = "0.9.9" @@ -13026,12 +12557,6 @@ dependencies = [ "libc", ] -[[package]] -name = "signature" -version = "1.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c" - [[package]] name = "signature" version = "2.2.0" @@ -13097,8 +12622,8 @@ dependencies = [ "enumn", "parity-scale-codec", "paste", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -13177,7 +12702,7 @@ dependencies = [ "hmac 0.12.1", "itertools 0.11.0", "libsecp256k1", - "merlin 3.0.0", + "merlin", "no-std-net", "nom", "num-bigint", @@ -13186,7 +12711,7 @@ dependencies = [ "pbkdf2 0.12.2", "pin-project", "poly1305", - "rand 0.8.5", + "rand", "rand_chacha 0.3.1", "ruzstd 0.4.0", "schnorrkel 0.10.2", @@ -13232,7 +12757,7 @@ dependencies = [ "itertools 0.12.1", "libm", "libsecp256k1", - "merlin 3.0.0", + "merlin", "no-std-net", "nom", "num-bigint", @@ -13241,7 +12766,7 @@ dependencies = [ "pbkdf2 0.12.2", "pin-project", "poly1305", - "rand 0.8.5", + "rand", "rand_chacha 0.3.1", "ruzstd 0.5.0", "schnorrkel 0.11.4", @@ -13284,7 +12809,7 @@ dependencies = [ "no-std-net", "parking_lot 0.12.1", "pin-project", - "rand 0.8.5", + "rand", "rand_chacha 0.3.1", "serde", "serde_json", @@ -13320,7 +12845,7 @@ dependencies = [ "no-std-net", "parking_lot 0.12.1", "pin-project", - "rand 0.8.5", + "rand", "rand_chacha 0.3.1", "serde", "serde_json", @@ -13380,10 +12905,10 @@ dependencies = [ "serde", "snowbridge-ethereum", "snowbridge-milagro-bls", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", "ssz_rs", "ssz_rs_derive", "static_assertions", @@ -13404,11 +12929,11 @@ dependencies = [ "scale-info", "serde", "snowbridge-beacon-primitives", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-arithmetic", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", "staging-xcm", "staging-xcm-builder", ] @@ -13430,10 +12955,10 @@ dependencies = [ "scale-info", "serde", "serde-big-array", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", ] [[package]] @@ -13445,7 +12970,7 @@ dependencies = [ "hex", "lazy_static", "parity-scale-codec", - "rand 0.8.5", + "rand", "scale-info", "snowbridge-amcl", "zeroize", @@ -13466,10 +12991,10 @@ dependencies = [ "scale-info", "serde", "snowbridge-core", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -13508,37 +13033,37 @@ dependencies = [ "http", "httparse", "log", - "rand 0.8.5", + "rand", "sha-1", ] [[package]] name = "sp-api" -version = "27.0.0" +version = "27.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ef42aa652381ade883c14ffbbb5c0fec36d382d2217b5bace01b8a0e8634778" +checksum = "2e4f8702afd77f14a32733e2b589c02694bf79d0b3a641963c508016208724d0" dependencies = [ "hash-db", "log", "parity-scale-codec", "scale-info", "sp-api-proc-macro", - "sp-core 29.0.0", - "sp-externalities 0.26.0", + "sp-core", + "sp-externalities", "sp-metadata-ir", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", - "sp-std 14.0.0", - "sp-trie 30.0.0", + "sp-runtime", + "sp-state-machine", + "sp-std", + "sp-trie", "sp-version", "thiserror", ] [[package]] name = "sp-api-proc-macro" -version = "15.0.0" +version = "15.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0694be2891593450916d6b53a274d234bccbc86bcbada36ba23fc356989070c7" +checksum = "0301e2f77afb450fbf2b093f8b324c7ad88cc82e5e69bd5dc8658a1f068b2a96" dependencies = [ "Inflector", "blake2 0.10.6", @@ -13549,20 +13074,6 @@ dependencies = [ "syn 2.0.58", ] -[[package]] -name = "sp-application-crypto" -version = "23.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "899492ea547816d5dfe9a5a2ecc32f65a7110805af6da3380aa4902371b31dc2" -dependencies = [ - "parity-scale-codec", - "scale-info", - "serde", - "sp-core 21.0.0", - "sp-io 23.0.0", - "sp-std 8.0.0", -] - [[package]] name = "sp-application-crypto" version = "31.0.0" @@ -13572,24 +13083,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-std 14.0.0", -] - -[[package]] -name = "sp-arithmetic" -version = "16.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb6020576e544c6824a51d651bc8df8e6ab67cd59f1c9ac09868bb81a5199ded" -dependencies = [ - "integer-sqrt", - "num-traits", - "parity-scale-codec", - "scale-info", - "serde", - "sp-std 8.0.0", - "static_assertions", + "sp-core", + "sp-io", + "sp-std", ] [[package]] @@ -13603,7 +13099,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-std 14.0.0", + "sp-std", "static_assertions", ] @@ -13616,9 +13112,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-application-crypto 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-application-crypto", + "sp-runtime", + "sp-std", ] [[package]] @@ -13629,8 +13125,8 @@ checksum = "1b36ce171caa7eb2bbe682c089f755fdefa71d3702e4fb1ba30d10146aef99d5" dependencies = [ "sp-api", "sp-inherents", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -13647,8 +13143,8 @@ dependencies = [ "sp-api", "sp-consensus", "sp-database", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", + "sp-runtime", + "sp-state-machine", "thiserror", ] @@ -13661,10 +13157,10 @@ dependencies = [ "async-trait", "futures", "log", - "sp-core 29.0.0", + "sp-core", "sp-inherents", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", + "sp-runtime", + "sp-state-machine", "thiserror", ] @@ -13678,11 +13174,11 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-consensus-slots", "sp-inherents", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", "sp-timestamp", ] @@ -13697,12 +13193,12 @@ dependencies = [ "scale-info", "serde", "sp-api", - "sp-application-crypto 31.0.0", + "sp-application-crypto", "sp-consensus-slots", - "sp-core 29.0.0", + "sp-core", "sp-inherents", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", "sp-timestamp", ] @@ -13717,13 +13213,13 @@ dependencies = [ "scale-info", "serde", "sp-api", - "sp-application-crypto 31.0.0", - "sp-core 29.0.0", + "sp-application-crypto", + "sp-core", "sp-crypto-hashing", - "sp-io 31.0.0", + "sp-io", "sp-mmr-primitives", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", "strum 0.24.1", ] @@ -13739,11 +13235,11 @@ dependencies = [ "scale-info", "serde", "sp-api", - "sp-application-crypto 31.0.0", - "sp-core 29.0.0", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-application-crypto", + "sp-core", + "sp-keystore", + "sp-runtime", + "sp-std", ] [[package]] @@ -13755,55 +13251,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-std 14.0.0", + "sp-std", "sp-timestamp", ] -[[package]] -name = "sp-core" -version = "21.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f18d9e2f67d8661f9729f35347069ac29d92758b59135176799db966947a7336" -dependencies = [ - "array-bytes 4.2.0", - "bitflags 1.3.2", - "blake2 0.10.6", - "bounded-collections 0.1.9", - "bs58 0.4.0", - "dyn-clonable", - "ed25519-zebra 3.1.0", - "futures", - "hash-db", - "hash256-std-hasher", - "impl-serde", - "lazy_static", - "libsecp256k1", - "log", - "merlin 2.0.1", - "parity-scale-codec", - "parking_lot 0.12.1", - "paste", - "primitive-types", - "rand 0.8.5", - "regex", - "scale-info", - "schnorrkel 0.9.1", - "secp256k1 0.24.3", - "secrecy", - "serde", - "sp-core-hashing 9.0.0", - "sp-debug-derive 8.0.0", - "sp-externalities 0.19.0", - "sp-runtime-interface 17.0.0", - "sp-std 8.0.0", - "sp-storage 13.0.0", - "ss58-registry", - "substrate-bip39", - "thiserror", - "tiny-bip39", - "zeroize", -] - [[package]] name = "sp-core" version = "29.0.0" @@ -13825,23 +13276,23 @@ dependencies = [ "itertools 0.10.5", "libsecp256k1", "log", - "merlin 3.0.0", + "merlin", "parity-scale-codec", "parking_lot 0.12.1", "paste", "primitive-types", - "rand 0.8.5", + "rand", "scale-info", "schnorrkel 0.11.4", - "secp256k1 0.28.2", + "secp256k1", "secrecy", "serde", "sp-crypto-hashing", - "sp-debug-derive 14.0.0", - "sp-externalities 0.26.0", - "sp-runtime-interface 25.0.0", - "sp-std 14.0.0", - "sp-storage 20.0.0", + "sp-debug-derive", + "sp-externalities", + "sp-runtime-interface", + "sp-std", + "sp-storage", "ss58-registry", "substrate-bip39", "thiserror", @@ -13850,21 +13301,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "sp-core-hashing" -version = "9.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ee599a8399448e65197f9a6cee338ad192e9023e35e31f22382964c3c174c68" -dependencies = [ - "blake2b_simd", - "byteorder", - "digest 0.10.7", - "sha2 0.10.8", - "sha3", - "sp-std 8.0.0", - "twox-hash", -] - [[package]] name = "sp-core-hashing" version = "15.0.0" @@ -13914,17 +13350,6 @@ dependencies = [ "parking_lot 0.12.1", ] -[[package]] -name = "sp-debug-derive" -version = "8.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7f531814d2f16995144c74428830ccf7d94ff4a7749632b83ad8199b181140c" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.58", -] - [[package]] name = "sp-debug-derive" version = "14.0.0" @@ -13936,18 +13361,6 @@ dependencies = [ "syn 2.0.58", ] -[[package]] -name = "sp-externalities" -version = "0.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0f71c671e01a8ca60da925d43a1b351b69626e268b8837f8371e320cf1dd100" -dependencies = [ - "environmental", - "parity-scale-codec", - "sp-std 8.0.0", - "sp-storage 13.0.0", -] - [[package]] name = "sp-externalities" version = "0.26.0" @@ -13956,8 +13369,8 @@ checksum = "e7096ed024cec397804864898b093b51e14c7299f1d00c67dd5800330e02bb82" dependencies = [ "environmental", "parity-scale-codec", - "sp-std 14.0.0", - "sp-storage 20.0.0", + "sp-std", + "sp-storage", ] [[package]] @@ -13968,8 +13381,8 @@ checksum = "fd865540ec19479c7349b584ccd78cc34c3f3a628a2a69dbb6365ceec36295ee" dependencies = [ "serde_json", "sp-api", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -13982,38 +13395,11 @@ dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", "scale-info", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", "thiserror", ] -[[package]] -name = "sp-io" -version = "23.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d597e35a9628fe7454b08965b2442e3ec0f264b0a90d41328e87422cec02e99" -dependencies = [ - "bytes", - "ed25519 1.5.3", - "ed25519-dalek 1.0.1", - "futures", - "libsecp256k1", - "log", - "parity-scale-codec", - "rustversion", - "secp256k1 0.24.3", - "sp-core 21.0.0", - "sp-externalities 0.19.0", - "sp-keystore 0.27.0", - "sp-runtime-interface 17.0.0", - "sp-state-machine 0.28.0", - "sp-std 8.0.0", - "sp-tracing 10.0.0", - "sp-trie 22.0.0", - "tracing", - "tracing-core", -] - [[package]] name = "sp-io" version = "31.0.0" @@ -14021,21 +13407,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec43aa073eab35fcb920d7592474d5427ea3be2bf938706a3ad955d7ba54fd8d" dependencies = [ "bytes", - "ed25519-dalek 2.1.1", + "ed25519-dalek", "libsecp256k1", "log", "parity-scale-codec", "rustversion", - "secp256k1 0.28.2", - "sp-core 29.0.0", + "secp256k1", + "sp-core", "sp-crypto-hashing", - "sp-externalities 0.26.0", - "sp-keystore 0.35.0", - "sp-runtime-interface 25.0.0", - "sp-state-machine 0.36.0", - "sp-std 14.0.0", - "sp-tracing 16.0.0", - "sp-trie 30.0.0", + "sp-externalities", + "sp-keystore", + "sp-runtime-interface", + "sp-state-machine", + "sp-std", + "sp-tracing", + "sp-trie", "tracing", "tracing-core", ] @@ -14046,25 +13432,11 @@ version = "32.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cf0a2f881958466fc92bc9b39bbc2c0d815ded4a21f8f953372b0ac2e11b02" dependencies = [ - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", "strum 0.24.1", ] -[[package]] -name = "sp-keystore" -version = "0.27.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9be3cdd67cc1d9c1db17c5cbc4ec4924054a8437009d167f21f6590797e4aa45" -dependencies = [ - "futures", - "parity-scale-codec", - "parking_lot 0.12.1", - "sp-core 21.0.0", - "sp-externalities 0.19.0", - "thiserror", -] - [[package]] name = "sp-keystore" version = "0.35.0" @@ -14073,8 +13445,8 @@ checksum = "444f2d53968b1ce5e908882710ff1f3873fcf3e95f59d57432daf685bbacb959" dependencies = [ "parity-scale-codec", "parking_lot 0.12.1", - "sp-core 29.0.0", - "sp-externalities 0.26.0", + "sp-core", + "sp-externalities", "thiserror", ] @@ -14097,7 +13469,7 @@ dependencies = [ "frame-metadata 16.0.0", "parity-scale-codec", "scale-info", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -14109,8 +13481,8 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-application-crypto 31.0.0", - "sp-std 14.0.0", + "sp-application-crypto", + "sp-std", ] [[package]] @@ -14125,10 +13497,10 @@ dependencies = [ "scale-info", "serde", "sp-api", - "sp-core 29.0.0", - "sp-debug-derive 14.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-core", + "sp-debug-derive", + "sp-runtime", + "sp-std", "thiserror", ] @@ -14141,10 +13513,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-arithmetic", + "sp-core", + "sp-runtime", + "sp-std", ] [[package]] @@ -14154,19 +13526,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d83b955dce0b6d143bec3f60571311168f362b1c16cf044da7037a407b66c19" dependencies = [ "sp-api", - "sp-core 29.0.0", - "sp-runtime 32.0.0", -] - -[[package]] -name = "sp-panic-handler" -version = "8.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebd2de46003fa8212426838ca71cd42ee36a26480ba9ffea983506ce03131033" -dependencies = [ - "backtrace", - "lazy_static", - "regex", + "sp-core", + "sp-runtime", ] [[package]] @@ -14188,30 +13549,7 @@ checksum = "9af4b73fe7ddd88b1641cca90048c4e525e721763199e6fd29c4f590884f4d16" dependencies = [ "rustc-hash", "serde", - "sp-core 29.0.0", -] - -[[package]] -name = "sp-runtime" -version = "24.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21c5bfc764a1a8259d7e8f7cfd22c84006275a512c958d3ff966c92151e134d5" -dependencies = [ - "either", - "hash256-std-hasher", - "impl-trait-for-tuples", - "log", - "parity-scale-codec", - "paste", - "rand 0.8.5", - "scale-info", - "serde", - "sp-application-crypto 23.0.0", - "sp-arithmetic 16.0.0", - "sp-core 21.0.0", - "sp-io 23.0.0", - "sp-std 8.0.0", - "sp-weights 20.0.0", + "sp-core", ] [[package]] @@ -14227,35 +13565,16 @@ dependencies = [ "log", "parity-scale-codec", "paste", - "rand 0.8.5", + "rand", "scale-info", "serde", "simple-mermaid", - "sp-application-crypto 31.0.0", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-std 14.0.0", - "sp-weights 28.0.0", -] - -[[package]] -name = "sp-runtime-interface" -version = "17.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e676128182f90015e916f806cba635c8141e341e7abbc45d25525472e1bbce8" -dependencies = [ - "bytes", - "impl-trait-for-tuples", - "parity-scale-codec", - "primitive-types", - "sp-externalities 0.19.0", - "sp-runtime-interface-proc-macro 11.0.0", - "sp-std 8.0.0", - "sp-storage 13.0.0", - "sp-tracing 10.0.0", - "sp-wasm-interface 14.0.0", - "static_assertions", + "sp-application-crypto", + "sp-arithmetic", + "sp-core", + "sp-io", + "sp-std", + "sp-weights", ] [[package]] @@ -14268,28 +13587,15 @@ dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", "primitive-types", - "sp-externalities 0.26.0", - "sp-runtime-interface-proc-macro 17.0.0", - "sp-std 14.0.0", - "sp-storage 20.0.0", - "sp-tracing 16.0.0", - "sp-wasm-interface 20.0.0", + "sp-externalities", + "sp-runtime-interface-proc-macro", + "sp-std", + "sp-storage", + "sp-tracing", + "sp-wasm-interface", "static_assertions", ] -[[package]] -name = "sp-runtime-interface-proc-macro" -version = "11.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5d5bd5566fe5633ec48dfa35ab152fd29f8a577c21971e1c6db9f28afb9bbb9" -dependencies = [ - "Inflector", - "proc-macro-crate 1.3.1", - "proc-macro2", - "quote", - "syn 2.0.58", -] - [[package]] name = "sp-runtime-interface-proc-macro" version = "17.0.0" @@ -14313,11 +13619,11 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-api", - "sp-core 29.0.0", - "sp-keystore 0.35.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-keystore", + "sp-runtime", "sp-staking", - "sp-std 14.0.0", + "sp-std", ] [[package]] @@ -14330,30 +13636,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", -] - -[[package]] -name = "sp-state-machine" -version = "0.28.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ef45d31f9e7ac648f8899a0cd038a3608f8499028bff55b6c799702592325b6" -dependencies = [ - "hash-db", - "log", - "parity-scale-codec", - "parking_lot 0.12.1", - "rand 0.8.5", - "smallvec", - "sp-core 21.0.0", - "sp-externalities 0.19.0", - "sp-panic-handler 8.0.0", - "sp-std 8.0.0", - "sp-trie 22.0.0", - "thiserror", - "tracing", + "sp-core", + "sp-runtime", + "sp-std", ] [[package]] @@ -14366,16 +13651,16 @@ dependencies = [ "log", "parity-scale-codec", "parking_lot 0.12.1", - "rand 0.8.5", + "rand", "smallvec", - "sp-core 29.0.0", - "sp-externalities 0.26.0", - "sp-panic-handler 13.0.0", - "sp-std 14.0.0", - "sp-trie 30.0.0", + "sp-core", + "sp-externalities", + "sp-panic-handler", + "sp-std", + "sp-trie", "thiserror", "tracing", - "trie-db 0.28.0", + "trie-db", ] [[package]] @@ -14386,50 +13671,30 @@ checksum = "309a9ae4e8134bbed8ffc510cf4d461a4a651f9250b556de782cedd876abe1ff" dependencies = [ "aes-gcm", "curve25519-dalek 4.1.2", - "ed25519-dalek 2.1.1", + "ed25519-dalek", "hkdf", "parity-scale-codec", - "rand 0.8.5", + "rand", "scale-info", "sha2 0.10.8", "sp-api", - "sp-application-crypto 31.0.0", - "sp-core 29.0.0", + "sp-application-crypto", + "sp-core", "sp-crypto-hashing", - "sp-externalities 0.26.0", - "sp-runtime 32.0.0", - "sp-runtime-interface 25.0.0", - "sp-std 14.0.0", + "sp-externalities", + "sp-runtime", + "sp-runtime-interface", + "sp-std", "thiserror", "x25519-dalek 2.0.1", ] -[[package]] -name = "sp-std" -version = "8.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53458e3c57df53698b3401ec0934bea8e8cfce034816873c0b0abbd83d7bac0d" - [[package]] name = "sp-std" version = "14.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "12f8ee986414b0a9ad741776762f4083cd3a5128449b982a3919c4df36874834" -[[package]] -name = "sp-storage" -version = "13.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94294be83f11d4958cfea89ed5798f0b6605f5defc3a996948848458abbcc18e" -dependencies = [ - "impl-serde", - "parity-scale-codec", - "ref-cast", - "serde", - "sp-debug-derive 8.0.0", - "sp-std 8.0.0", -] - [[package]] name = "sp-storage" version = "20.0.0" @@ -14440,8 +13705,8 @@ dependencies = [ "parity-scale-codec", "ref-cast", "serde", - "sp-debug-derive 14.0.0", - "sp-std 14.0.0", + "sp-debug-derive", + "sp-std", ] [[package]] @@ -14453,24 +13718,11 @@ dependencies = [ "async-trait", "parity-scale-codec", "sp-inherents", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", "thiserror", ] -[[package]] -name = "sp-tracing" -version = "10.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "357f7591980dd58305956d32f8f6646d0a8ea9ea0e7e868e46f53b68ddf00cec" -dependencies = [ - "parity-scale-codec", - "sp-std 8.0.0", - "tracing", - "tracing-core", - "tracing-subscriber 0.2.25", -] - [[package]] name = "sp-tracing" version = "16.0.0" @@ -14478,7 +13730,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0351810b9d074df71c4514c5228ed05c250607cba131c1c9d1526760ab69c05c" dependencies = [ "parity-scale-codec", - "sp-std 14.0.0", + "sp-std", "tracing", "tracing-core", "tracing-subscriber 0.2.25", @@ -14491,7 +13743,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9742861c5330bdcb42856a6eed3d3745b58ee1c92ca4c9260032ff4e6c387165" dependencies = [ "sp-api", - "sp-runtime 32.0.0", + "sp-runtime", ] [[package]] @@ -14503,35 +13755,11 @@ dependencies = [ "async-trait", "parity-scale-codec", "scale-info", - "sp-core 29.0.0", + "sp-core", "sp-inherents", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-trie 30.0.0", -] - -[[package]] -name = "sp-trie" -version = "22.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48e4eeb7ef23f79eba8609db79ef9cef242f994f1f87a3c0387b4b5f177fda74" -dependencies = [ - "ahash 0.8.11", - "hash-db", - "hashbrown 0.13.2", - "lazy_static", - "memory-db", - "nohash-hasher", - "parity-scale-codec", - "parking_lot 0.12.1", - "scale-info", - "schnellru", - "sp-core 21.0.0", - "sp-std 8.0.0", - "thiserror", - "tracing", - "trie-db 0.27.1", - "trie-root", + "sp-runtime", + "sp-std", + "sp-trie", ] [[package]] @@ -14547,15 +13775,15 @@ dependencies = [ "nohash-hasher", "parity-scale-codec", "parking_lot 0.12.1", - "rand 0.8.5", + "rand", "scale-info", "schnellru", - "sp-core 29.0.0", - "sp-externalities 0.26.0", - "sp-std 14.0.0", + "sp-core", + "sp-externalities", + "sp-std", "thiserror", "tracing", - "trie-db 0.28.0", + "trie-db", "trie-root", ] @@ -14571,8 +13799,8 @@ dependencies = [ "scale-info", "serde", "sp-crypto-hashing-proc-macro", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", "sp-version-proc-macro", "thiserror", ] @@ -14589,20 +13817,6 @@ dependencies = [ "syn 2.0.58", ] -[[package]] -name = "sp-wasm-interface" -version = "14.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a19c122609ca5d8246be6386888596320d03c7bc880959eaa2c36bcd5acd6846" -dependencies = [ - "anyhow", - "impl-trait-for-tuples", - "log", - "parity-scale-codec", - "sp-std 8.0.0", - "wasmtime", -] - [[package]] name = "sp-wasm-interface" version = "20.0.0" @@ -14613,26 +13827,10 @@ dependencies = [ "impl-trait-for-tuples", "log", "parity-scale-codec", - "sp-std 14.0.0", + "sp-std", "wasmtime", ] -[[package]] -name = "sp-weights" -version = "20.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45d084c735544f70625b821c3acdbc7a2fc1893ca98b85f1942631284692c75b" -dependencies = [ - "parity-scale-codec", - "scale-info", - "serde", - "smallvec", - "sp-arithmetic 16.0.0", - "sp-core 21.0.0", - "sp-debug-derive 8.0.0", - "sp-std 8.0.0", -] - [[package]] name = "sp-weights" version = "28.0.0" @@ -14644,9 +13842,9 @@ dependencies = [ "scale-info", "serde", "smallvec", - "sp-arithmetic 24.0.0", - "sp-debug-derive 14.0.0", - "sp-std 14.0.0", + "sp-arithmetic", + "sp-debug-derive", + "sp-std", ] [[package]] @@ -14737,8 +13935,8 @@ dependencies = [ "frame-system", "parity-scale-codec", "scale-info", - "sp-runtime 32.0.0", - "sp-std 14.0.0", + "sp-runtime", + "sp-std", ] [[package]] @@ -14756,15 +13954,15 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-weights 28.0.0", + "sp-weights", "xcm-procedural", ] [[package]] name = "staging-xcm-builder" -version = "8.0.1" +version = "8.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8f6cfc27c1d45f9a67e20ed3f7e60296299688825350291606add10bf3bbff2" +checksum = "988d765ad5ab3b5cc90bb1dd143153ebdbe2b7600e10d5ef3a7f3e8df1bdac5d" dependencies = [ "frame-support", "frame-system", @@ -14774,20 +13972,20 @@ dependencies = [ "parity-scale-codec", "polkadot-parachain-primitives", "scale-info", - "sp-arithmetic 24.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-weights 28.0.0", + "sp-arithmetic", + "sp-io", + "sp-runtime", + "sp-std", + "sp-weights", "staging-xcm", "staging-xcm-executor", ] [[package]] name = "staging-xcm-executor" -version = "8.0.1" +version = "8.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a638f4c8735cc04b5c93920a1f59e679f48b131315a07d146798e0decebf7720" +checksum = "74b5c5f2a1d610c5e20e5fae2680c9a28380f305afafeed62f341bfbce57b79a" dependencies = [ "environmental", "frame-benchmarking", @@ -14796,12 +13994,12 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-weights 28.0.0", + "sp-arithmetic", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", + "sp-weights", "staging-xcm", ] @@ -14940,8 +14138,8 @@ dependencies = [ "sp-api", "sp-block-builder", "sp-blockchain", - "sp-core 29.0.0", - "sp-runtime 32.0.0", + "sp-core", + "sp-runtime", ] [[package]] @@ -14968,7 +14166,7 @@ dependencies = [ "log", "sc-rpc-api", "serde", - "sp-runtime 32.0.0", + "sp-runtime", ] [[package]] @@ -14982,18 +14180,18 @@ dependencies = [ "sc-client-api", "sc-rpc-api", "serde", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", - "sp-trie 30.0.0", - "trie-db 0.28.0", + "sp-core", + "sp-runtime", + "sp-state-machine", + "sp-trie", + "trie-db", ] [[package]] name = "substrate-wasm-builder" -version = "18.0.0" +version = "18.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "511bbc2df035f5fe2556d855369a1bbb45df620360391a1f6e3fa1a1d64af79a" +checksum = "4a39a20e17c24ede36b5bd5e7543a4cef8d8a0daf6e1a046dc31832b837a54a0" dependencies = [ "build-helper", "cargo_metadata", @@ -15052,7 +14250,7 @@ dependencies = [ "scale-value", "serde", "serde_json", - "sp-core-hashing 15.0.0", + "sp-core-hashing", "subxt-lightclient", "subxt-macro", "subxt-metadata", @@ -15124,7 +14322,7 @@ dependencies = [ "frame-metadata 16.0.0", "parity-scale-codec", "scale-info", - "sp-core-hashing 15.0.0", + "sp-core-hashing", "thiserror", ] @@ -15141,10 +14339,10 @@ dependencies = [ "pbkdf2 0.12.2", "regex", "schnorrkel 0.11.4", - "secp256k1 0.28.2", + "secp256k1", "secrecy", "sha2 0.10.8", - "sp-core-hashing 15.0.0", + "sp-core-hashing", "subxt", "thiserror", "zeroize", @@ -15184,17 +14382,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "synstructure" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.58", -] - [[package]] name = "system-configuration" version = "0.5.1" @@ -15275,7 +14462,7 @@ dependencies = [ "polkadot-core-primitives", "rococo-runtime-constants", "smallvec", - "sp-runtime 32.0.0", + "sp-runtime", "staging-xcm", "westend-runtime-constants", ] @@ -15410,25 +14597,6 @@ dependencies = [ "time-core", ] -[[package]] -name = "tiny-bip39" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62cc94d358b5a1e84a5cb9109f559aa3c4d634d2b1b4de3d0fa4adc7c78e2861" -dependencies = [ - "anyhow", - "hmac 0.12.1", - "once_cell", - "pbkdf2 0.11.0", - "rand 0.8.5", - "rustc-hash", - "sha2 0.10.8", - "thiserror", - "unicode-normalization", - "wasm-bindgen", - "zeroize", -] - [[package]] name = "tiny-keccak" version = "2.0.2" @@ -15490,7 +14658,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f57eb36ecbe0fc510036adff84824dd3c24bb781e21bfa67b69d556aa85214f" dependencies = [ "pin-project", - "rand 0.8.5", + "rand", "tokio", ] @@ -15802,19 +14970,6 @@ dependencies = [ "tracing-log 0.2.0", ] -[[package]] -name = "trie-db" -version = "0.27.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "767abe6ffed88a1889671a102c2861ae742726f52e0a5a425b92c9fbfa7e9c85" -dependencies = [ - "hash-db", - "hashbrown 0.13.2", - "log", - "rustc-hex", - "smallvec", -] - [[package]] name = "trie-db" version = "0.28.0" @@ -15853,7 +15008,7 @@ dependencies = [ "idna 0.2.3", "ipnet", "lazy_static", - "rand 0.8.5", + "rand", "smallvec", "socket2 0.4.10", "thiserror", @@ -15909,19 +15064,19 @@ dependencies = [ "sp-api", "sp-consensus-aura", "sp-consensus-babe", - "sp-core 29.0.0", - "sp-debug-derive 14.0.0", - "sp-externalities 0.26.0", + "sp-core", + "sp-debug-derive", + "sp-externalities", "sp-inherents", - "sp-io 31.0.0", - "sp-keystore 0.35.0", + "sp-io", + "sp-keystore", "sp-rpc", - "sp-runtime 32.0.0", - "sp-state-machine 0.36.0", + "sp-runtime", + "sp-state-machine", "sp-timestamp", "sp-transaction-storage-proof", "sp-version", - "sp-weights 28.0.0", + "sp-weights", "substrate-rpc-client", "zstd 0.12.4", ] @@ -15940,7 +15095,7 @@ checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ "cfg-if", "digest 0.10.7", - "rand 0.8.5", + "rand", "static_assertions", ] @@ -16091,7 +15246,7 @@ dependencies = [ "arrayref", "constcat", "digest 0.10.7", - "rand 0.8.5", + "rand", "rand_chacha 0.3.1", "rand_core 0.6.4", "sha2 0.10.8", @@ -16501,7 +15656,7 @@ dependencies = [ "memfd", "memoffset", "paste", - "rand 0.8.5", + "rand", "rustix 0.36.17", "wasmtime-asm-macros", "wasmtime-environ", @@ -16630,24 +15785,24 @@ dependencies = [ "serde_derive", "smallvec", "sp-api", - "sp-application-crypto 31.0.0", - "sp-arithmetic 24.0.0", + "sp-application-crypto", + "sp-arithmetic", "sp-authority-discovery", "sp-block-builder", "sp-consensus-babe", "sp-consensus-beefy", - "sp-core 29.0.0", + "sp-core", "sp-genesis-builder", "sp-inherents", - "sp-io 31.0.0", + "sp-io", "sp-mmr-primitives", "sp-npos-elections", "sp-offchain", - "sp-runtime 32.0.0", + "sp-runtime", "sp-session", "sp-staking", - "sp-std 14.0.0", - "sp-storage 20.0.0", + "sp-std", + "sp-storage", "sp-transaction-pool", "sp-version", "staging-xcm", @@ -16667,9 +15822,9 @@ dependencies = [ "polkadot-primitives", "polkadot-runtime-common", "smallvec", - "sp-core 29.0.0", - "sp-runtime 32.0.0", - "sp-weights 28.0.0", + "sp-core", + "sp-runtime", + "sp-weights", "staging-xcm", "staging-xcm-builder", ] @@ -17061,13 +16216,13 @@ dependencies = [ "polkadot-parachain-primitives", "polkadot-primitives", "polkadot-runtime-parachains", - "sp-arithmetic 24.0.0", - "sp-core 29.0.0", + "sp-arithmetic", + "sp-core", "sp-crypto-hashing", - "sp-io 31.0.0", - "sp-runtime 32.0.0", - "sp-std 14.0.0", - "sp-tracing 16.0.0", + "sp-io", + "sp-runtime", + "sp-std", + "sp-tracing", "staging-xcm", "staging-xcm-executor", ] @@ -17084,12 +16239,6 @@ dependencies = [ "syn 2.0.58", ] -[[package]] -name = "xxhash-rust" -version = "0.8.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "927da81e25be1e1a2901d59b81b37dd2efd1fc9c9345a55007f09bf5a2d3ee03" - [[package]] name = "yamux" version = "0.10.2" @@ -17100,7 +16249,7 @@ dependencies = [ "log", "nohash-hasher", "parking_lot 0.12.1", - "rand 0.8.5", + "rand", "static_assertions", ] diff --git a/Cargo.toml b/Cargo.toml index 3f1d6e1c..3719e83d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,7 +47,7 @@ subxt-signer = "0.34.0" tokio = { version = "1.36", features = ["macros", "time", "rt-multi-thread"] } # Build -substrate-wasm-builder = "18.0.0" +substrate-wasm-builder = "18.0.1" substrate-build-script-utils = "11.0.0" # Local @@ -76,7 +76,7 @@ sc-transaction-pool-api = "29.0.0" frame-benchmarking = { version = "29.0.0", default-features = false } frame-benchmarking-cli = "33.0.0" frame-executive = { version = "29.0.0", default-features = false } -frame-support = { version = "29.0.0", default-features = false } +frame-support = { version = "29.0.2", default-features = false } frame-system = { version = "29.0.0", default-features = false } frame-system-benchmarking = { version = "29.0.0", default-features = false } frame-system-rpc-runtime-api = { version = "27.0.0", default-features = false } @@ -84,7 +84,7 @@ frame-try-runtime = { version = "0.35.0", default-features = false } pallet-aura = { version = "28.0.0", default-features = false } pallet-authorship = { version = "29.0.0", default-features = false } pallet-assets = { version = "30.0.0", default-features = false } -pallet-balances = { version = "29.0.0", default-features = false } +pallet-balances = { version = "29.0.2", default-features = false } pallet-contracts = { version = "28.0.0", default-features = false } pallet-message-queue = { version = "32.0.0", default-features = false } pallet-multisig = { version = "29.0.0", default-features = false } @@ -97,11 +97,11 @@ pallet-scheduler = { version = "30.0.0", default-features = false } pallet-session = { version = "29.0.0", default-features = false } pallet-sudo = { version = "29.0.0", default-features = false } pallet-timestamp = { version = "28.0.0", default-features = false } -pallet-transaction-payment = { version = "29.0.0", default-features = false } +pallet-transaction-payment = { version = "29.0.1", default-features = false } pallet-transaction-payment-rpc = "31.0.0" pallet-transaction-payment-rpc-runtime-api = { version = "29.0.0", default-features = false } pallet-utility = { version = "29.0.0", default-features = false } -sp-api = { version = "27.0.0", default-features = false } +sp-api = { version = "27.0.1", default-features = false } sp-authority-discovery = { version = "27.0.0", default-features = false } sp-block-builder = { version = "27.0.0", default-features = false } sp-blockchain = "29.0.0" @@ -125,17 +125,17 @@ sp-transaction-pool = { version = "27.0.0", default-features = false } sp-version = { version = "30.0.0", default-features = false } # Polkadot -pallet-xcm = { version = "=8.0.1", default-features = false } +pallet-xcm = { version = "8.0.5", default-features = false } polkadot-cli = "8.0.0" polkadot-parachain-primitives = { version = "7.0.0", default-features = false } -polkadot-runtime-parachains = { version = "8.0.1", default-features = false } +polkadot-runtime-parachains = { version = "8.0.3", default-features = false } polkadot-primitives = { version = "8.0.1", default-features = false } -polkadot-runtime-common = { version = "8.0.1", default-features = false } +polkadot-runtime-common = { version = "8.0.2", default-features = false } rococo-runtime-constants = { version = "8.0.0", default-features = false } rococo-runtime = { version = "8.0.0", default-features = false } -xcm = { package = "staging-xcm", version = "8.0.1", default-features = false } -xcm-builder = { package = "staging-xcm-builder", version = "8.0.1", default-features = false } -xcm-executor = { package = "staging-xcm-executor", version = "8.0.1", default-features = false } +xcm = { version = "8.0.1", package = "staging-xcm", default-features = false } +xcm-builder = { version = "8.0.2", package = "staging-xcm-builder", default-features = false } +xcm-executor = { version = "8.0.2", package = "staging-xcm-executor", default-features = false } # Cumulus asset-hub-rococo-runtime = { version = "0.12.0", default-features = false } @@ -149,9 +149,9 @@ cumulus-primitives-aura = { version = "0.8.0", default-features = false } cumulus-primitives-core = { version = "0.8.0", default-features = false } cumulus-primitives-utility = { version = "0.8.1", default-features = false } emulated-integration-tests-common = { version = "4.0.0", default-features = false } -pallet-collator-selection = { version = "10.0.0", default-features = false } +pallet-collator-selection = { version = "10.0.2", default-features = false } parachains-common = { version = "8.0.0", default-features = false } -parachain-info = { package = "staging-parachain-info", version = "0.8.0", default-features = false } +parachain-info = { version = "0.8.0", package = "staging-parachain-info", default-features = false } cumulus-primitives-parachain-inherent = "0.8.0" cumulus-relay-chain-interface = "0.8.0" color-print = "0.3.4" @@ -160,4 +160,4 @@ cumulus-client-collator = "0.8.0" cumulus-client-consensus-aura = "0.8.0" cumulus-client-consensus-common = "0.8.0" cumulus-client-consensus-proposer = "0.8.0" -cumulus-client-service = "0.8.0" \ No newline at end of file +cumulus-client-service = "0.8.0" diff --git a/pop-api/Cargo.toml b/pop-api/Cargo.toml index 9a868cfa..2dd4519c 100644 --- a/pop-api/Cargo.toml +++ b/pop-api/Cargo.toml @@ -9,9 +9,9 @@ edition = "2021" enumflags2 = { version = "0.7.7" } ink = { version = "5.0.0", default-features = false } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } -scale-info = { version = "2.6", default-features = false, features = ["derive"] } -sp-io = { version = "23.0.0", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] } -sp-runtime = { version = "24.0", default-features = false } +scale-info = { version = "2.10.0", default-features = false, features = ["derive"] } +sp-io = { version = "31.0.0", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] } +sp-runtime = { version = "32.0.0", default-features = false } pop-primitives = { path = "../primitives", features = ["devnet"], default-features = false } diff --git a/pop-api/examples/.gitignore b/pop-api/examples/.gitignore index e0caa493..d60800c8 100755 --- a/pop-api/examples/.gitignore +++ b/pop-api/examples/.gitignore @@ -1,9 +1,9 @@ # Ignore build artifacts from the local tests sub-crate. -/fungibles/target/ +**/target/ # Ignore backup files creates by cargo fmt. **/*.rs.bk # Remove Cargo.lock when creating an executable, leave it for libraries # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock +**/Cargo.lock diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs index 41ea1b14..35719ac6 100755 --- a/pop-api/examples/fungibles/lib.rs +++ b/pop-api/examples/fungibles/lib.rs @@ -12,7 +12,7 @@ use pop_api::{ primitives::{AccountId as AccountId32, AssetId}, }; -pub type Result = core::result::Result; +pub type Result = core::result::Result; #[ink::contract(env = pop_api::Environment)] mod fungibles { @@ -68,9 +68,9 @@ mod fungibles { value, ); - let result = api::transfer(id, to, value).map_err(|e| e.into()); + let result = api::transfer(id, to, value); ink::env::debug_println!("Result: {:?}", result); - result + result.map_err(|e| e.into()) } #[ink(message)] @@ -91,9 +91,9 @@ mod fungibles { value, ); - let result = api::transfer_from(id, from, to, value, &data).map_err(|e| e.into()); + let result = api::transfer_from(id, from, to, value, &data); ink::env::debug_println!("Result: {:?}", result); - result + result.map_err(|e| e.into()) } /// 2. PSP-22 Metadata Interface: @@ -118,9 +118,9 @@ mod fungibles { admin, min_balance, ); - let result = api::create(id, admin, min_balance).map_err(|e| e.into()); + let result = api::create(id, admin, min_balance); ink::env::debug_println!("Result: {:?}", result); - result + result.map_err(|e| e.into()) } #[ink(message)] @@ -138,9 +138,9 @@ mod fungibles { symbol, decimals, ); - let result = api::set_metadata(id, name, symbol, decimals).map_err(|e| e.into()); + let result = api::set_metadata(id, name, symbol, decimals); ink::env::debug_println!("Result: {:?}", result); - result + result.map_err(|e| e.into()) } #[ink(message)] diff --git a/pop-api/integration-tests/Cargo.toml b/pop-api/integration-tests/Cargo.toml new file mode 100644 index 00000000..84769433 --- /dev/null +++ b/pop-api/integration-tests/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "integration-tests" +version = "0.1.0" +edition = "2021" + +[dev-dependencies] +env_logger = "0.11.2" +scale = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } +frame-support = { version = "29.0.0", default-features = false } +frame-system = { version = "29.0.0", default-features = false } +pallet-balances = { version = "29.0.2", default-features = false } +pallet-contracts = { version = "28.0.0", default-features = false } +pop-api = { path = "../.", default-features = false, features = ["assets"] } +pop-runtime-devnet = { path = "../../runtime/devnet", default-features = false } +sp-io = { version = "31.0.0", default-features = false } +sp-runtime = { version = "32.0.0", default-features = false } + + +[features] +default = ["std"] +std = [ + "frame-support/std", + "frame-system/std", + "pallet-balances/std", + "pallet-contracts/std", + "pop-api/std", + "pop-runtime-devnet/std", + "scale/std", + "sp-io/std", + "sp-runtime/std", +] \ No newline at end of file diff --git a/pop-api/integration-tests/src/lib.rs b/pop-api/integration-tests/src/lib.rs new file mode 100644 index 00000000..624c3000 --- /dev/null +++ b/pop-api/integration-tests/src/lib.rs @@ -0,0 +1,95 @@ +#![cfg(test)] + +use frame_support::{ + traits::fungibles::{approvals::Inspect as ApprovalInspect, Inspect}, + weights::Weight, +}; +use pallet_contracts::{Code, CollectEvents, Determinism, ExecReturnValue}; +use scale::{Decode, Encode}; +use sp_runtime::{traits::Hash, AccountId32, BuildStorage, DispatchError}; + +use pop_runtime_devnet::{Assets, Contracts, Runtime, RuntimeOrigin, System, UNIT}; + +mod local_fungibles; + +type Balance = u128; +type AssetId = u32; +const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug; + +const ALICE: AccountId32 = AccountId32::new([1_u8; 32]); +const BOB: AccountId32 = AccountId32::new([2_u8; 32]); +// FERDIE has no initial balance. +const FERDIE: AccountId32 = AccountId32::new([3_u8; 32]); +const INIT_AMOUNT: Balance = 100_000_000 * UNIT; +const INIT_VALUE: Balance = 100 * UNIT; +const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024); + +fn new_test_ext() -> sp_io::TestExternalities { + let mut t = frame_system::GenesisConfig::::default() + .build_storage() + .expect("Frame system builds valid default genesis config"); + + pallet_balances::GenesisConfig:: { + balances: vec![(ALICE, INIT_AMOUNT), (BOB, INIT_AMOUNT)], + } + .assimilate_storage(&mut t) + .expect("Pallet balances storage can be assimilated"); + + let mut ext = sp_io::TestExternalities::new(t); + ext.execute_with(|| System::set_block_number(1)); + ext +} + +fn load_wasm_module(path: &str) -> std::io::Result<(Vec, ::Output)> +where + T: frame_system::Config, +{ + let wasm_binary = std::fs::read(path)?; + let code_hash = T::Hashing::hash(&wasm_binary); + Ok((wasm_binary, code_hash)) +} + +fn function_selector(name: &str) -> Vec { + let hash = sp_io::hashing::blake2_256(name.as_bytes()); + [hash[0..4].to_vec()].concat() +} + +fn do_bare_call( + addr: AccountId32, + input: Vec, + value: u128, +) -> Result { + let result = Contracts::bare_call( + ALICE, + addr.into(), + value.into(), + GAS_LIMIT, + None, + input, + DEBUG_OUTPUT, + CollectEvents::Skip, + Determinism::Enforced, + ); + result.result +} + +// Deploy, instantiate and return contract address. +fn instantiate(contract: &str, init_value: u128, salt: Vec) -> AccountId32 { + let (wasm_binary, _) = + load_wasm_module::(contract).expect("could not read .wasm file"); + let result = Contracts::bare_instantiate( + ALICE, + init_value, + GAS_LIMIT, + None, + Code::Upload(wasm_binary), + function_selector("new"), + salt, + DEBUG_OUTPUT, + CollectEvents::Skip, + ) + .result + .unwrap(); + assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); + result.account_id +} diff --git a/runtime/devnet/src/tests/local_fungibles.rs b/pop-api/integration-tests/src/local_fungibles.rs similarity index 88% rename from runtime/devnet/src/tests/local_fungibles.rs rename to pop-api/integration-tests/src/local_fungibles.rs index ac2b8ca0..1b3c6633 100644 --- a/runtime/devnet/src/tests/local_fungibles.rs +++ b/pop-api/integration-tests/src/local_fungibles.rs @@ -1,16 +1,8 @@ -// Todo - errors: -// - Badorigin: contract is always signed -// - Lookup: is a valid AccountId due to the contract -// - Many errors can occur from calling a dispatchable. All the DispatchErrors are handled by the -// pop api but not all the possible errors for each dipatchable are tested. How should I approach -// this? -#![cfg(test)] - use super::*; - -use pop_api::{ - error::{ArithmeticError::*, Error::*, TokenError::*}, - v0::assets::fungibles::FungiblesError::*, +use pop_api::error::{ + ArithmeticError::*, + Error::{self, *}, + TokenError::*, }; const ASSET_ID: AssetId = 1; @@ -196,11 +188,8 @@ fn total_supply_works() { fn balance_of_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = instantiate( - "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", - INIT_VALUE, - vec![], - ); + let addr = + instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); // No tokens in circulation. assert_eq!(Assets::balance(ASSET_ID, BOB), balance_of(addr.clone(), ASSET_ID, BOB)); @@ -216,11 +205,8 @@ fn balance_of_works() { fn allowance_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = instantiate( - "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", - INIT_VALUE, - vec![], - ); + let addr = + instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); // No tokens in circulation. assert_eq!( @@ -242,11 +228,8 @@ fn allowance_works() { fn asset_exists_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = instantiate( - "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", - INIT_VALUE, - vec![], - ); + let addr = + instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); // No tokens in circulation. assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr.clone(), ASSET_ID)); @@ -264,16 +247,14 @@ fn create_works() { let _ = env_logger::try_init(); let new_asset = 2; // Instantiate a contract without balance (relay token). - let addr = - instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", 0, vec![0]); + let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 0, vec![0]); // No balance to pay for fees. assert_eq!( decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), Module { index: 10, error: 2 }, ); // Instantiate a contract without balance (relay token). - let addr = - instantiate("../../pop-api/examples/fungibles/target/ink/fungibles.wasm", 100, vec![2]); + let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 100, vec![2]); // TODO: make sure it has enough for the fees but not for the deposit. // No balance to pay fe deposit. assert_eq!( @@ -281,11 +262,8 @@ fn create_works() { Module { index: 10, error: 2 }, ); // Instantiate a contract with balance. - let addr = instantiate( - "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", - INIT_VALUE, - vec![1], - ); + let addr = + instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![1]); create_asset(ALICE, ASSET_ID, 1); // Asset ID is already taken. assert_eq!( @@ -307,11 +285,8 @@ fn create_works() { fn set_metadata_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = instantiate( - "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", - INIT_VALUE, - vec![], - ); + let addr = + instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); create_asset(addr.clone(), ASSET_ID, 1); let result = set_metadata(addr.clone(), ASSET_ID, vec![12], vec![12], 12); @@ -324,11 +299,8 @@ fn set_metadata_works() { fn transfer_from_mint_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = instantiate( - "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", - INIT_VALUE, - vec![], - ); + let addr = + instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); let amount: Balance = 100 * UNIT; // Asset does not exist. @@ -387,11 +359,8 @@ fn transfer_from_mint_works() { fn transfer_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = instantiate( - "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", - INIT_VALUE, - vec![], - ); + let addr = + instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); let amount: Balance = 100 * UNIT; // Asset does not exist. diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 3e9ead7f..c516adff 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -21,8 +21,7 @@ pub mod v0; type AccountId = AccountId32; // TODO: do the same as the AccountId above and check expanded macro code. -// type Balance = ::Balance; -type Balance = u128; +type Balance = ::Balance; #[cfg(any(feature = "nfts", feature = "cross-chain"))] type BlockNumber = ::BlockNumber; diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index d67d30bd..778387cb 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -298,18 +298,18 @@ pub(crate) fn block(id: AssetId, who: impl Into>) -> /// - pub(crate) fn total_supply(id: AssetId) -> Result { - state::read(RuntimeStateKeys::Assets(AssetsKeys::TotalSupply(id))).into() + state::read(RuntimeStateKeys::Assets(AssetsKeys::TotalSupply(id))) } pub(crate) fn balance_of(id: AssetId, owner: AccountId) -> Result { - state::read(RuntimeStateKeys::Assets(AssetsKeys::BalanceOf(id, owner))).into() + state::read(RuntimeStateKeys::Assets(AssetsKeys::BalanceOf(id, owner))) } pub(crate) fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { - state::read(RuntimeStateKeys::Assets(AssetsKeys::Allowance(id, owner, spender))).into() + state::read(RuntimeStateKeys::Assets(AssetsKeys::Allowance(id, owner, spender))) } pub(crate) fn asset_exists(id: AssetId) -> Result { - state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id))).into() + state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id))) } // Parameters to extrinsics representing an asset id (`AssetIdParameter`) and a balance amount (`Balance`) are expected diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml index 6ba3fea7..cb19b0ab 100644 --- a/primitives/Cargo.toml +++ b/primitives/Cargo.toml @@ -7,10 +7,10 @@ edition = "2021" [dependencies] bounded-collections = { version = "0.1", default-features = false } -scale = { package = "parity-scale-codec", version = "3.6.9", default-features = false, features = ["derive"] } +scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-decode = { version = "0.10.0", default-features = false, features = ["derive"], optional = true } scale-encode = { version = "0.5.0", default-features = false, features = ["derive"], optional = true } -scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true } +scale-info = { version = "2.10", default-features = false, features = ["derive"], optional = true } [features] default = ["std"] diff --git a/primitives/src/storage_keys.rs b/primitives/src/storage_keys.rs index 4be95986..5fc7cdb3 100644 --- a/primitives/src/storage_keys.rs +++ b/primitives/src/storage_keys.rs @@ -4,12 +4,12 @@ use super::*; #[derive(Encode, Decode, Debug, MaxEncodedLen)] pub enum RuntimeStateKeys { + #[cfg(feature = "assets")] + Assets(AssetsKeys), #[cfg(feature = "nfts")] Nfts(NftsKeys), #[cfg(feature = "cross-chain")] ParachainSystem(ParachainSystemKeys), - #[cfg(feature = "assets")] - Assets(AssetsKeys), } #[cfg(feature = "cross-chain")] diff --git a/runtime/devnet/Cargo.toml b/runtime/devnet/Cargo.toml index b72bf4f7..5f52b855 100644 --- a/runtime/devnet/Cargo.toml +++ b/runtime/devnet/Cargo.toml @@ -91,7 +91,6 @@ parachain-info.workspace = true env_logger = "0.11.2" hex = "0.4.3" enumflags2 = "0.7.9" -pop-api = { path = "../../pop-api", features = ["assets", "cross-chain", "nfts"] } [features] default = ["std"] @@ -139,7 +138,6 @@ std = [ "polkadot-parachain-primitives/std", "polkadot-runtime-common/std", "pop-primitives/std", - "pop-api/std", "scale-info/std", "sp-api/std", "sp-io/std", diff --git a/runtime/devnet/src/extensions.rs b/runtime/devnet/src/extensions.rs index 9db31e83..904d18ce 100644 --- a/runtime/devnet/src/extensions.rs +++ b/runtime/devnet/src/extensions.rs @@ -390,6 +390,186 @@ where dispatch_call::(&mut env, call, origin, LOG_PREFIX) } +#[cfg(test)] +mod tests { + use super::*; + use crate::{Assets, Runtime, System}; + use sp_runtime::BuildStorage; + + fn new_test_ext() -> sp_io::TestExternalities { + let t = frame_system::GenesisConfig::::default() + .build_storage() + .expect("Frame system builds valid default genesis config"); + let mut ext = sp_io::TestExternalities::new(t); + ext.execute_with(|| System::set_block_number(1)); + ext + } + + #[test] + fn encoding_decoding_dispatch_error() { + use codec::{Decode, Encode}; + use sp_runtime::{ArithmeticError, DispatchError, ModuleError, TokenError}; + + new_test_ext().execute_with(|| { + let error = DispatchError::Module(ModuleError { + index: 255, + error: [2, 0, 0, 0], + message: Some("error message"), + }); + let encoded = error.encode(); + let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); + assert_eq!(encoded, vec![3, 255, 2, 0, 0, 0]); + assert_eq!( + decoded, + // `message` is skipped for encoding. + DispatchError::Module(ModuleError { + index: 255, + error: [2, 0, 0, 0], + message: None + }) + ); + println!("Encoded Module Error: {:?}", encoded); + + // Example pallet assets Error into ModuleError. + let index = + <::PalletInfo as frame_support::traits::PalletInfo>::index::< + Assets, + >() + .expect("Every active module has an index in the runtime; qed") as u8; + + let mut error = + pallet_assets::Error::NotFrozen::.encode(); + error.resize(MAX_MODULE_ERROR_ENCODED_SIZE, 0); + let error = DispatchError::Module(ModuleError { + index, + error: TryInto::try_into(error).expect("should work"), + message: None, + }); + let encoded = error.encode(); + let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); + assert_eq!(encoded, vec![3, 52, 18, 0, 0, 0]); + assert_eq!( + decoded, + DispatchError::Module(ModuleError { + index: 52, + error: [18, 0, 0, 0], + message: None + }) + ); + println!("Encoded Module Error: {:?}", encoded); + + // Example DispatchError::Token + let error = DispatchError::Token(TokenError::UnknownAsset); + let encoded = error.encode(); + assert_eq!(encoded, vec![7, 4]); + println!("Encoded Token Error: {:?}", encoded); + + // Example DispatchError::Arithmetic + let error = DispatchError::Arithmetic(ArithmeticError::Overflow); + let encoded = error.encode(); + assert_eq!(encoded, vec![8, 1]); + println!("Encoded Arithmetic Error: {:?}", encoded); + }); + } + + #[test] + fn encoding_of_enum() { + use codec::{Decode, Encode}; + + // Comprehensive enum with all different type of variants. + #[derive(Debug, PartialEq, Encode, Decode)] + enum ComprehensiveEnum { + SimpleVariant, + DataVariant(u8), + NamedFields { w: u8 }, + NestedEnum(InnerEnum), + OptionVariant(Option), + VecVariant(Vec), + TupleVariant(u8, u8), + NestedStructVariant(NestedStruct), + NestedEnumStructVariant(NestedEnumStruct), + } + + #[derive(Debug, PartialEq, Encode, Decode)] + enum InnerEnum { + A, + B { inner_data: u8 }, + C(u8), + } + + #[derive(Debug, PartialEq, Encode, Decode)] + struct NestedStruct { + x: u8, + y: u8, + } + + #[derive(Debug, PartialEq, Encode, Decode)] + struct NestedEnumStruct { + inner_enum: InnerEnum, + } + + // Creating each possible variant for an enum. + let enum_simple = ComprehensiveEnum::SimpleVariant; + let enum_data = ComprehensiveEnum::DataVariant(42); + let enum_named = ComprehensiveEnum::NamedFields { w: 42 }; + let enum_nested = ComprehensiveEnum::NestedEnum(InnerEnum::B { inner_data: 42 }); + let enum_option = ComprehensiveEnum::OptionVariant(Some(42)); + let enum_vec = ComprehensiveEnum::VecVariant(vec![1, 2, 3, 4, 5]); + let enum_tuple = ComprehensiveEnum::TupleVariant(42, 42); + let enum_nested_struct = + ComprehensiveEnum::NestedStructVariant(NestedStruct { x: 42, y: 42 }); + let enum_nested_enum_struct = + ComprehensiveEnum::NestedEnumStructVariant(NestedEnumStruct { + inner_enum: InnerEnum::C(42), + }); + + // Encode and print each variant individually to see their encoded values. + println!("{:?} -> {:?}", enum_simple, enum_simple.encode()); + println!("{:?} -> {:?}", enum_data, enum_data.encode()); + println!("{:?} -> {:?}", enum_named, enum_named.encode()); + println!("{:?} -> {:?}", enum_nested, enum_nested.encode()); + println!("{:?} -> {:?}", enum_option, enum_option.encode()); + println!("{:?} -> {:?}", enum_vec, enum_vec.encode()); + println!("{:?} -> {:?}", enum_tuple, enum_tuple.encode()); + println!("{:?} -> {:?}", enum_nested_struct, enum_nested_struct.encode()); + println!("{:?} -> {:?}", enum_nested_enum_struct, enum_nested_enum_struct.encode()); + } + + #[test] + fn dispatch_error_to_status_code() { + // Create all the different `DispatchError` variants with its respective `PopApiError`. + let test_cases = vec![ + (DispatchError::Other("hallo"), [0, 0, 0, 0]), + (DispatchError::CannotLookup, [1, 0, 0, 0]), + (DispatchError::BadOrigin, [2, 0, 0, 0]), + ( + DispatchError::Module(sp_runtime::ModuleError { + index: 1, + error: [2, 0, 0, 0], + message: Some("hallo"), + }), + [3, 1, 2, 0], + ), + (DispatchError::ConsumerRemaining, [4, 0, 0, 0]), + (DispatchError::NoProviders, [5, 0, 0, 0]), + (DispatchError::TooManyConsumers, [6, 0, 0, 0]), + (DispatchError::Token(sp_runtime::TokenError::BelowMinimum), [7, 2, 0, 0]), + (DispatchError::Arithmetic(sp_runtime::ArithmeticError::Overflow), [8, 1, 0, 0]), + ( + DispatchError::Transactional(sp_runtime::TransactionalError::LimitReached), + [9, 0, 0, 0], + ), + (DispatchError::Exhausted, [10, 0, 0, 0]), + (DispatchError::Corruption, [11, 0, 0, 0]), + (DispatchError::Unavailable, [12, 0, 0, 0]), + (DispatchError::RootNotAllowed, [13, 0, 0, 0]), + ]; + for (error, encoded_error) in test_cases { + let status_code = crate::extensions::convert_to_status_code(error); + assert_eq!(status_code, u32::decode(&mut &encoded_error[..]).unwrap()); + } + } +} // use enumflags2::BitFlags; // use pallet_nfts::{CollectionConfig, CollectionSetting, CollectionSettings, MintSettings}; // use parachains_common::CollectionId; diff --git a/runtime/devnet/src/lib.rs b/runtime/devnet/src/lib.rs index b03bdac6..9ab64043 100644 --- a/runtime/devnet/src/lib.rs +++ b/runtime/devnet/src/lib.rs @@ -10,8 +10,6 @@ mod extensions; mod weights; // Public due to integration tests crate. pub mod config; -#[cfg(test)] -mod tests; use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases; use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; diff --git a/runtime/devnet/src/tests/mod.rs b/runtime/devnet/src/tests/mod.rs deleted file mode 100644 index 828aa06b..00000000 --- a/runtime/devnet/src/tests/mod.rs +++ /dev/null @@ -1,267 +0,0 @@ -#![cfg(test)] - -use codec::{Decode, Encode}; -use frame_support::traits::fungibles::{approvals::Inspect as ApprovalInspect, Inspect}; -use frame_system::Config; -use pallet_contracts::{Code, CollectEvents, Determinism, ExecReturnValue}; -use sp_runtime::{traits::Hash, AccountId32, BuildStorage, DispatchError}; - -use crate::{Assets, Contracts, Runtime, RuntimeOrigin, System, Weight, UNIT}; - -mod local_fungibles; - -type Balance = u128; -type AssetId = u32; -const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug; - -const ALICE: AccountId32 = AccountId32::new([1_u8; 32]); -const BOB: AccountId32 = AccountId32::new([2_u8; 32]); -// FERDIE has no initial balance. -const FERDIE: AccountId32 = AccountId32::new([3_u8; 32]); -const INIT_AMOUNT: Balance = 100_000_000 * UNIT; -const INIT_VALUE: Balance = 100 * UNIT; -const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024); - -fn new_test_ext() -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::::default() - .build_storage() - .expect("Frame system builds valid default genesis config"); - - pallet_balances::GenesisConfig:: { - balances: vec![(ALICE, INIT_AMOUNT), (BOB, INIT_AMOUNT)], - } - .assimilate_storage(&mut t) - .expect("Pallet balances storage can be assimilated"); - - let mut ext = sp_io::TestExternalities::new(t); - ext.execute_with(|| System::set_block_number(1)); - ext -} - -fn load_wasm_module(path: &str) -> std::io::Result<(Vec, ::Output)> -where - T: frame_system::Config, -{ - let wasm_binary = std::fs::read(path)?; - let code_hash = T::Hashing::hash(&wasm_binary); - Ok((wasm_binary, code_hash)) -} - -fn function_selector(name: &str) -> Vec { - let hash = sp_io::hashing::blake2_256(name.as_bytes()); - [hash[0..4].to_vec()].concat() -} - -fn do_bare_call( - addr: AccountId32, - input: Vec, - value: u128, -) -> Result { - let result = Contracts::bare_call( - ALICE, - addr.into(), - value.into(), - GAS_LIMIT, - None, - input, - DEBUG_OUTPUT, - CollectEvents::Skip, - Determinism::Enforced, - ); - log::debug!("Contract debug buffer - {:?}", String::from_utf8(result.debug_message.clone())); - log::debug!("result: {:?}", result); - result.result -} - -// Deploy, instantiate and return contract address. -fn instantiate(contract: &str, init_value: u128, salt: Vec) -> AccountId32 { - let (wasm_binary, _) = - load_wasm_module::(contract).expect("could not read .wasm file"); - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - salt, - DEBUG_OUTPUT, - CollectEvents::Skip, - ) - .result - .unwrap(); - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - result.account_id -} - -mod encoding { - use super::*; - use crate::config::assets::TrustBackedAssetsInstance; - use crate::Runtime; - use sp_runtime::DispatchError; - - #[test] - fn encoding_decoding_dispatch_error() { - use codec::{Decode, Encode}; - use sp_runtime::{ArithmeticError, DispatchError, ModuleError, TokenError}; - - new_test_ext().execute_with(|| { - let error = DispatchError::Module(ModuleError { - index: 255, - error: [2, 0, 0, 0], - message: Some("error message"), - }); - let encoded = error.encode(); - let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); - assert_eq!(encoded, vec![3, 255, 2, 0, 0, 0]); - assert_eq!( - decoded, - // `message` is skipped for encoding. - DispatchError::Module(ModuleError { - index: 255, - error: [2, 0, 0, 0], - message: None - }) - ); - println!("Encoded Module Error: {:?}", encoded); - - // Example pallet assets Error into ModuleError. - let index = - <::PalletInfo as frame_support::traits::PalletInfo>::index::< - Assets, - >() - .expect("Every active module has an index in the runtime; qed") as u8; - - let mut error = - pallet_assets::Error::NotFrozen::.encode(); - error.resize(sp_runtime::MAX_MODULE_ERROR_ENCODED_SIZE, 0); - let error = DispatchError::Module(ModuleError { - index, - error: TryInto::try_into(error).expect("should work"), - message: None, - }); - let encoded = error.encode(); - let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); - assert_eq!(encoded, vec![3, 52, 18, 0, 0, 0]); - assert_eq!( - decoded, - DispatchError::Module(ModuleError { - index: 52, - error: [18, 0, 0, 0], - message: None - }) - ); - println!("Encoded Module Error: {:?}", encoded); - - // Example DispatchError::Token - let error = DispatchError::Token(TokenError::UnknownAsset); - let encoded = error.encode(); - assert_eq!(encoded, vec![7, 4]); - println!("Encoded Token Error: {:?}", encoded); - - // Example DispatchError::Arithmetic - let error = DispatchError::Arithmetic(ArithmeticError::Overflow); - let encoded = error.encode(); - assert_eq!(encoded, vec![8, 1]); - println!("Encoded Arithmetic Error: {:?}", encoded); - }); - } - - #[test] - fn encoding_of_enum() { - use codec::{Decode, Encode}; - - // Comprehensive enum with all different type of variants. - #[derive(Debug, PartialEq, Encode, Decode)] - enum ComprehensiveEnum { - SimpleVariant, - DataVariant(u8), - NamedFields { w: u8 }, - NestedEnum(InnerEnum), - OptionVariant(Option), - VecVariant(Vec), - TupleVariant(u8, u8), - NestedStructVariant(NestedStruct), - NestedEnumStructVariant(NestedEnumStruct), - } - - #[derive(Debug, PartialEq, Encode, Decode)] - enum InnerEnum { - A, - B { inner_data: u8 }, - C(u8), - } - - #[derive(Debug, PartialEq, Encode, Decode)] - struct NestedStruct { - x: u8, - y: u8, - } - - #[derive(Debug, PartialEq, Encode, Decode)] - struct NestedEnumStruct { - inner_enum: InnerEnum, - } - - // Creating each possible variant for an enum. - let enum_simple = ComprehensiveEnum::SimpleVariant; - let enum_data = ComprehensiveEnum::DataVariant(42); - let enum_named = ComprehensiveEnum::NamedFields { w: 42 }; - let enum_nested = ComprehensiveEnum::NestedEnum(InnerEnum::B { inner_data: 42 }); - let enum_option = ComprehensiveEnum::OptionVariant(Some(42)); - let enum_vec = ComprehensiveEnum::VecVariant(vec![1, 2, 3, 4, 5]); - let enum_tuple = ComprehensiveEnum::TupleVariant(42, 42); - let enum_nested_struct = - ComprehensiveEnum::NestedStructVariant(NestedStruct { x: 42, y: 42 }); - let enum_nested_enum_struct = - ComprehensiveEnum::NestedEnumStructVariant(NestedEnumStruct { - inner_enum: InnerEnum::C(42), - }); - - // Encode and print each variant individually to see their encoded values. - println!("{:?} -> {:?}", enum_simple, enum_simple.encode()); - println!("{:?} -> {:?}", enum_data, enum_data.encode()); - println!("{:?} -> {:?}", enum_named, enum_named.encode()); - println!("{:?} -> {:?}", enum_nested, enum_nested.encode()); - println!("{:?} -> {:?}", enum_option, enum_option.encode()); - println!("{:?} -> {:?}", enum_vec, enum_vec.encode()); - println!("{:?} -> {:?}", enum_tuple, enum_tuple.encode()); - println!("{:?} -> {:?}", enum_nested_struct, enum_nested_struct.encode()); - println!("{:?} -> {:?}", enum_nested_enum_struct, enum_nested_enum_struct.encode()); - } - - #[test] - fn dispatch_error_to_status_code() { - // Create all the different `DispatchError` variants with its respective `PopApiError`. - let test_cases = vec![ - (DispatchError::Other("hallo"), [0, 0, 0, 0]), - (DispatchError::CannotLookup, [1, 0, 0, 0]), - (DispatchError::BadOrigin, [2, 0, 0, 0]), - ( - DispatchError::Module(sp_runtime::ModuleError { - index: 1, - error: [2, 0, 0, 0], - message: Some("hallo"), - }), - [3, 1, 2, 0], - ), - (DispatchError::ConsumerRemaining, [4, 0, 0, 0]), - (DispatchError::NoProviders, [5, 0, 0, 0]), - (DispatchError::TooManyConsumers, [6, 0, 0, 0]), - (DispatchError::Token(sp_runtime::TokenError::BelowMinimum), [7, 2, 0, 0]), - (DispatchError::Arithmetic(sp_runtime::ArithmeticError::Overflow), [8, 1, 0, 0]), - ( - DispatchError::Transactional(sp_runtime::TransactionalError::LimitReached), - [9, 0, 0, 0], - ), - (DispatchError::Exhausted, [10, 0, 0, 0]), - (DispatchError::Corruption, [11, 0, 0, 0]), - (DispatchError::Unavailable, [12, 0, 0, 0]), - (DispatchError::RootNotAllowed, [13, 0, 0, 0]), - ]; - for (error, encoded_error) in test_cases { - let status_code = crate::extensions::convert_to_status_code(error); - assert_eq!(status_code, u32::decode(&mut &encoded_error[..]).unwrap()); - } - } -} From c1616a3611f73f0185f822339c452549c63b2926 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Wed, 26 Jun 2024 15:41:18 +0200 Subject: [PATCH 13/76] refactor: naming pallet error --- pop-api/Cargo.toml | 6 +- pop-api/src/lib.rs | 2 +- pop-api/src/v0/cross_chain/mod.rs | 8 +- pop-api/src/v0/nfts.rs | 308 +++++++++++++++--------------- primitives/src/storage_keys.rs | 11 +- 5 files changed, 170 insertions(+), 165 deletions(-) diff --git a/pop-api/Cargo.toml b/pop-api/Cargo.toml index 2dd4519c..8454c1fa 100644 --- a/pop-api/Cargo.toml +++ b/pop-api/Cargo.toml @@ -5,12 +5,15 @@ license = "GPL-3.0-only" version = "0.0.0" edition = "2021" +[workspace] +exclude = [ + "integration-tests", +] [dependencies] enumflags2 = { version = "0.7.7" } ink = { version = "5.0.0", default-features = false } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-info = { version = "2.10.0", default-features = false, features = ["derive"] } -sp-io = { version = "31.0.0", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] } sp-runtime = { version = "32.0.0", default-features = false } pop-primitives = { path = "../primitives", features = ["devnet"], default-features = false } @@ -28,7 +31,6 @@ std = [ "pop-primitives/std", "scale/std", "scale-info/std", - "sp-io/std", "sp-runtime/std", ] assets = ["pop-primitives/assets"] diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index c516adff..04d08bce 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -1,7 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] use ink::{prelude::vec::Vec, ChainExtensionInstance}; -pub use sp_runtime::{BoundedVec, MultiAddress, MultiSignature}; +pub use sp_runtime::{MultiAddress, MultiSignature}; use crate::error::{Error, StatusCode}; use primitives::{storage_keys::*, AccountId as AccountId32}; diff --git a/pop-api/src/v0/cross_chain/mod.rs b/pop-api/src/v0/cross_chain/mod.rs index 5a0dda6c..ad58e0e8 100644 --- a/pop-api/src/v0/cross_chain/mod.rs +++ b/pop-api/src/v0/cross_chain/mod.rs @@ -10,7 +10,7 @@ pub fn relay_chain_block_number() -> Result { #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum Error { +pub enum CrossChainError { /// The desired destination was unreachable, generally because there is a no way of routing /// to it. Unreachable, @@ -66,11 +66,11 @@ pub enum Error { LocalExecutionIncomplete, } -impl TryFrom for Error { - type Error = Error; +impl TryFrom for CrossChainError { + type Error = crate::error::Error; fn try_from(status_code: u32) -> core::result::Result { - use Error::*; + use CrossChainError::*; match status_code { 0 => Ok(Unreachable), 1 => Ok(SendFailure), diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index 29219c66..32539576 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -5,10 +5,10 @@ use crate::{ dispatch, primitives::{ nfts::{ApprovalsLimit, CollectionId, ItemId, KeyLimit}, - BoundedBTreeMap, + BoundedBTreeMap, BoundedVec, }, - state, AccountId, Balance, BlockNumber, BoundedVec, MultiAddress, NftsKeys, RuntimeCall, - RuntimeStateKeys, StatusCode, + state, AccountId, Balance, BlockNumber, MultiAddress, NftsKeys, RuntimeCall, RuntimeStateKeys, + StatusCode, }; pub use types::*; @@ -521,157 +521,157 @@ pub(crate) enum NftCalls { receive_item: ItemId, }, } -// -// #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] -// #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -// pub enum Error { -// /// The signing account has no permission to do the operation. -// NoPermission, -// /// The given item ID is unknown. -// UnknownCollection, -// /// The item ID has already been used for an item. -// AlreadyExists, -// /// The approval had a deadline that expired, so the approval isn't valid anymore. -// ApprovalExpired, -// /// The owner turned out to be different to what was expected. -// WrongOwner, -// /// The witness data given does not match the current state of the chain. -// BadWitness, -// /// Collection ID is already taken. -// CollectionIdInUse, -// /// Items within that collection are non-transferable. -// ItemsNonTransferable, -// /// The provided account is not a delegate. -// NotDelegate, -// /// The delegate turned out to be different to what was expected. -// WrongDelegate, -// /// No approval exists that would allow the transfer. -// Unapproved, -// /// The named owner has not signed ownership acceptance of the collection. -// Unaccepted, -// /// The item is locked (non-transferable). -// ItemLocked, -// /// Item's attributes are locked. -// LockedItemAttributes, -// /// Collection's attributes are locked. -// LockedCollectionAttributes, -// /// Item's metadata is locked. -// LockedItemMetadata, -// /// Collection's metadata is locked. -// LockedCollectionMetadata, -// /// All items have been minted. -// MaxSupplyReached, -// /// The max supply is locked and can't be changed. -// MaxSupplyLocked, -// /// The provided max supply is less than the number of items a collection already has. -// MaxSupplyTooSmall, -// /// The given item ID is unknown. -// UnknownItem, -// /// Swap doesn't exist. -// UnknownSwap, -// /// The given item has no metadata set. -// MetadataNotFound, -// /// The provided attribute can't be found. -// AttributeNotFound, -// /// Item is not for sale. -// NotForSale, -// /// The provided bid is too low. -// BidTooLow, -// /// The item has reached its approval limit. -// ReachedApprovalLimit, -// /// The deadline has already expired. -// DeadlineExpired, -// /// The duration provided should be less than or equal to `MaxDeadlineDuration`. -// WrongDuration, -// /// The method is disabled by system settings. -// MethodDisabled, -// /// The provided setting can't be set. -// WrongSetting, -// /// Item's config already exists and should be equal to the provided one. -// InconsistentItemConfig, -// /// Config for a collection or an item can't be found. -// NoConfig, -// /// Some roles were not cleared. -// RolesNotCleared, -// /// Mint has not started yet. -// MintNotStarted, -// /// Mint has already ended. -// MintEnded, -// /// The provided Item was already used for claiming. -// AlreadyClaimed, -// /// The provided data is incorrect. -// IncorrectData, -// /// The extrinsic was sent by the wrong origin. -// WrongOrigin, -// /// The provided signature is incorrect. -// WrongSignature, -// /// The provided metadata might be too long. -// IncorrectMetadata, -// /// Can't set more attributes per one call. -// MaxAttributesLimitReached, -// /// The provided namespace isn't supported in this call. -// WrongNamespace, -// /// Can't delete non-empty collections. -// CollectionNotEmpty, -// /// The witness data should be provided. -// WitnessRequired, -// } -// -// impl TryFrom for Error { -// type Error = Error; -// -// fn try_from(status_code: u32) -> core::result::Result { -// use Error::*; -// match status_code { -// 0 => Ok(NoPermission), -// 1 => Ok(UnknownCollection), -// 2 => Ok(AlreadyExists), -// 3 => Ok(ApprovalExpired), -// 4 => Ok(WrongOwner), -// 5 => Ok(BadWitness), -// 6 => Ok(CollectionIdInUse), -// 7 => Ok(ItemsNonTransferable), -// 8 => Ok(NotDelegate), -// 9 => Ok(WrongDelegate), -// 10 => Ok(Unapproved), -// 11 => Ok(Unaccepted), -// 12 => Ok(ItemLocked), -// 13 => Ok(LockedItemAttributes), -// 14 => Ok(LockedCollectionAttributes), -// 15 => Ok(LockedItemMetadata), -// 16 => Ok(LockedCollectionMetadata), -// 17 => Ok(MaxSupplyReached), -// 18 => Ok(MaxSupplyLocked), -// 19 => Ok(MaxSupplyTooSmall), -// 20 => Ok(UnknownItem), -// 21 => Ok(UnknownSwap), -// 22 => Ok(MetadataNotFound), -// 23 => Ok(AttributeNotFound), -// 24 => Ok(NotForSale), -// 25 => Ok(BidTooLow), -// 26 => Ok(ReachedApprovalLimit), -// 27 => Ok(DeadlineExpired), -// 28 => Ok(WrongDuration), -// 29 => Ok(MethodDisabled), -// 30 => Ok(WrongSetting), -// 31 => Ok(InconsistentItemConfig), -// 32 => Ok(NoConfig), -// 33 => Ok(RolesNotCleared), -// 34 => Ok(MintNotStarted), -// 35 => Ok(MintEnded), -// 36 => Ok(AlreadyClaimed), -// 37 => Ok(IncorrectData), -// 38 => Ok(WrongOrigin), -// 39 => Ok(WrongSignature), -// 40 => Ok(IncorrectMetadata), -// 41 => Ok(MaxAttributesLimitReached), -// 42 => Ok(WrongNamespace), -// 43 => Ok(CollectionNotEmpty), -// 44 => Ok(WitnessRequired), -// _ => todo!(), -// } -// } -// } + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum NftsError { + /// The signing account has no permission to do the operation. + NoPermission, + /// The given item ID is unknown. + UnknownCollection, + /// The item ID has already been used for an item. + AlreadyExists, + /// The approval had a deadline that expired, so the approval isn't valid anymore. + ApprovalExpired, + /// The owner turned out to be different to what was expected. + WrongOwner, + /// The witness data given does not match the current state of the chain. + BadWitness, + /// Collection ID is already taken. + CollectionIdInUse, + /// Items within that collection are non-transferable. + ItemsNonTransferable, + /// The provided account is not a delegate. + NotDelegate, + /// The delegate turned out to be different to what was expected. + WrongDelegate, + /// No approval exists that would allow the transfer. + Unapproved, + /// The named owner has not signed ownership acceptance of the collection. + Unaccepted, + /// The item is locked (non-transferable). + ItemLocked, + /// Item's attributes are locked. + LockedItemAttributes, + /// Collection's attributes are locked. + LockedCollectionAttributes, + /// Item's metadata is locked. + LockedItemMetadata, + /// Collection's metadata is locked. + LockedCollectionMetadata, + /// All items have been minted. + MaxSupplyReached, + /// The max supply is locked and can't be changed. + MaxSupplyLocked, + /// The provided max supply is less than the number of items a collection already has. + MaxSupplyTooSmall, + /// The given item ID is unknown. + UnknownItem, + /// Swap doesn't exist. + UnknownSwap, + /// The given item has no metadata set. + MetadataNotFound, + /// The provided attribute can't be found. + AttributeNotFound, + /// Item is not for sale. + NotForSale, + /// The provided bid is too low. + BidTooLow, + /// The item has reached its approval limit. + ReachedApprovalLimit, + /// The deadline has already expired. + DeadlineExpired, + /// The duration provided should be less than or equal to `MaxDeadlineDuration`. + WrongDuration, + /// The method is disabled by system settings. + MethodDisabled, + /// The provided setting can't be set. + WrongSetting, + /// Item's config already exists and should be equal to the provided one. + InconsistentItemConfig, + /// Config for a collection or an item can't be found. + NoConfig, + /// Some roles were not cleared. + RolesNotCleared, + /// Mint has not started yet. + MintNotStarted, + /// Mint has already ended. + MintEnded, + /// The provided Item was already used for claiming. + AlreadyClaimed, + /// The provided data is incorrect. + IncorrectData, + /// The extrinsic was sent by the wrong origin. + WrongOrigin, + /// The provided signature is incorrect. + WrongSignature, + /// The provided metadata might be too long. + IncorrectMetadata, + /// Can't set more attributes per one call. + MaxAttributesLimitReached, + /// The provided namespace isn't supported in this call. + WrongNamespace, + /// Can't delete non-empty collections. + CollectionNotEmpty, + /// The witness data should be provided. + WitnessRequired, +} + +impl TryFrom for NftsError { + type Error = crate::error::Error; + + fn try_from(status_code: u32) -> core::result::Result { + use NftsError::*; + match status_code { + 0 => Ok(NoPermission), + 1 => Ok(UnknownCollection), + 2 => Ok(AlreadyExists), + 3 => Ok(ApprovalExpired), + 4 => Ok(WrongOwner), + 5 => Ok(BadWitness), + 6 => Ok(CollectionIdInUse), + 7 => Ok(ItemsNonTransferable), + 8 => Ok(NotDelegate), + 9 => Ok(WrongDelegate), + 10 => Ok(Unapproved), + 11 => Ok(Unaccepted), + 12 => Ok(ItemLocked), + 13 => Ok(LockedItemAttributes), + 14 => Ok(LockedCollectionAttributes), + 15 => Ok(LockedItemMetadata), + 16 => Ok(LockedCollectionMetadata), + 17 => Ok(MaxSupplyReached), + 18 => Ok(MaxSupplyLocked), + 19 => Ok(MaxSupplyTooSmall), + 20 => Ok(UnknownItem), + 21 => Ok(UnknownSwap), + 22 => Ok(MetadataNotFound), + 23 => Ok(AttributeNotFound), + 24 => Ok(NotForSale), + 25 => Ok(BidTooLow), + 26 => Ok(ReachedApprovalLimit), + 27 => Ok(DeadlineExpired), + 28 => Ok(WrongDuration), + 29 => Ok(MethodDisabled), + 30 => Ok(WrongSetting), + 31 => Ok(InconsistentItemConfig), + 32 => Ok(NoConfig), + 33 => Ok(RolesNotCleared), + 34 => Ok(MintNotStarted), + 35 => Ok(MintEnded), + 36 => Ok(AlreadyClaimed), + 37 => Ok(IncorrectData), + 38 => Ok(WrongOrigin), + 39 => Ok(WrongSignature), + 40 => Ok(IncorrectMetadata), + 41 => Ok(MaxAttributesLimitReached), + 42 => Ok(WrongNamespace), + 43 => Ok(CollectionNotEmpty), + 44 => Ok(WitnessRequired), + _ => todo!(), + } + } +} // Local implementations of pallet-nfts types mod types { diff --git a/primitives/src/storage_keys.rs b/primitives/src/storage_keys.rs index 5fc7cdb3..748d0662 100644 --- a/primitives/src/storage_keys.rs +++ b/primitives/src/storage_keys.rs @@ -4,12 +4,15 @@ use super::*; #[derive(Encode, Decode, Debug, MaxEncodedLen)] pub enum RuntimeStateKeys { - #[cfg(feature = "assets")] - Assets(AssetsKeys), - #[cfg(feature = "nfts")] - Nfts(NftsKeys), #[cfg(feature = "cross-chain")] + #[codec(index = 1)] ParachainSystem(ParachainSystemKeys), + #[cfg(feature = "nfts")] + #[codec(index = 50)] + Nfts(NftsKeys), + #[cfg(feature = "assets")] + #[codec(index = 52)] + Assets(AssetsKeys), } #[cfg(feature = "cross-chain")] From 13e947e74f82430bc50865ca6a44ec234620728e Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Wed, 26 Jun 2024 17:10:42 +0200 Subject: [PATCH 14/76] refactor: optimising pop api and additional logic + test --- pop-api/Cargo.toml | 6 +- pop-api/examples/fungibles/lib.rs | 161 +++++-- pop-api/src/error.rs | 166 ++----- pop-api/src/lib.rs | 6 +- pop-api/src/primitives.rs | 1 + pop-api/src/v0/assets/fungibles.rs | 314 +++++++------ pop-api/src/v0/assets/mod.rs | 718 +++++++++++++++-------------- pop-api/src/v0/state.rs | 5 +- primitives/src/lib.rs | 2 +- primitives/src/storage_keys.rs | 7 +- 10 files changed, 710 insertions(+), 676 deletions(-) diff --git a/pop-api/Cargo.toml b/pop-api/Cargo.toml index 8454c1fa..a88d8892 100644 --- a/pop-api/Cargo.toml +++ b/pop-api/Cargo.toml @@ -5,15 +5,12 @@ license = "GPL-3.0-only" version = "0.0.0" edition = "2021" -[workspace] -exclude = [ - "integration-tests", -] [dependencies] enumflags2 = { version = "0.7.7" } ink = { version = "5.0.0", default-features = false } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-info = { version = "2.10.0", default-features = false, features = ["derive"] } +sp-io = { version = "31.0.0", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] } sp-runtime = { version = "32.0.0", default-features = false } pop-primitives = { path = "../primitives", features = ["devnet"], default-features = false } @@ -31,6 +28,7 @@ std = [ "pop-primitives/std", "scale/std", "scale-info/std", +"sp-io/std", "sp-runtime/std", ] assets = ["pop-primitives/assets"] diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs index 35719ac6..bd51d760 100755 --- a/pop-api/examples/fungibles/lib.rs +++ b/pop-api/examples/fungibles/lib.rs @@ -7,12 +7,12 @@ /// use ink::prelude::vec::Vec; use pop_api::{ - assets::fungibles::{self as api, FungiblesError}, - error::{Error, StatusCode}, + assets::fungibles::{self as api}, + error::StatusCode, primitives::{AccountId as AccountId32, AssetId}, }; -pub type Result = core::result::Result; +pub type Result = core::result::Result; #[ink::contract(env = pop_api::Environment)] mod fungibles { @@ -41,12 +41,14 @@ mod fungibles { #[ink(message)] pub fn total_supply(&self, id: AssetId) -> Result { - api::total_supply(id).map_err(|e| e.into()) + // api::total_supply(id).map_err(|e| e.into()) + api::total_supply(id) } #[ink(message)] pub fn balance_of(&self, id: AssetId, owner: AccountId32) -> Result { - api::balance_of(id, owner).map_err(|e| e.into()) + // api::balance_of(id, owner).map_err(|e| e.into()) + api::balance_of(id, owner) } #[ink(message)] @@ -56,7 +58,8 @@ mod fungibles { owner: AccountId32, spender: AccountId32, ) -> Result { - api::allowance(id, owner, spender).map_err(|e| e.into()) + // api::allowance(id, owner, spender).map_err(|e| e.into()) + api::allowance(id, owner, spender) } #[ink(message)] @@ -70,15 +73,16 @@ mod fungibles { let result = api::transfer(id, to, value); ink::env::debug_println!("Result: {:?}", result); - result.map_err(|e| e.into()) + // result.map_err(|e| e.into()) + result } #[ink(message)] pub fn transfer_from( &self, id: AssetId, - from: Option, - to: Option, + from: AccountId32, + to: AccountId32, value: Balance, // In the standard a `[u8]`, but the size needs to be known at compile time. data: Vec, @@ -93,60 +97,137 @@ mod fungibles { let result = api::transfer_from(id, from, to, value, &data); ink::env::debug_println!("Result: {:?}", result); - result.map_err(|e| e.into()) + // result.map_err(|e| e.into()) + result } - /// 2. PSP-22 Metadata Interface: - /// - token_name - /// - token_symbol - /// - token_decimals + #[ink(message)] + pub fn approve(&self, id: AssetId, spender: AccountId32, value: Balance) -> Result<()> { + ink::env::debug_println!( + "PopApiFungiblesExample::approve: id: {:?}, spender {:?}, value: {:?}", + id, + spender, + value, + ); - /// 3. Asset Management: - /// - create - /// - start_destroy - /// - destroy_accounts - /// - destroy_approvals - /// - finish_destroy - /// - set_metadata - /// - clear_metadata + let result = api::approve(id, spender, value); + ink::env::debug_println!("Result: {:?}", result); + // result.map_err(|e| e.into()) + result + } #[ink(message)] - pub fn create(&self, id: AssetId, admin: AccountId32, min_balance: Balance) -> Result<()> { + pub fn increase_allowance( + &self, + id: AssetId, + spender: AccountId32, + value: Balance, + ) -> Result<()> { ink::env::debug_println!( - "PopApiFungiblesExample::create: id: {:?} admin: {:?} min_balance: {:?}", + "PopApiFungiblesExample::increase_allowance: id: {:?}, spender {:?}, value: {:?}", id, - admin, - min_balance, + spender, + value, ); - let result = api::create(id, admin, min_balance); + + let result = api::increase_allowance(id, spender, value); ink::env::debug_println!("Result: {:?}", result); - result.map_err(|e| e.into()) + // result.map_err(|e| e.into()) + result } #[ink(message)] - pub fn set_metadata( + pub fn decrease_allowance( &self, id: AssetId, - name: Vec, - symbol: Vec, - decimals: u8, + spender: AccountId32, + value: Balance, ) -> Result<()> { ink::env::debug_println!( - "PopApiFungiblesExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}", + "PopApiFungiblesExample::decrease_allowance: id: {:?}, spender {:?}, value: {:?}", id, - name, - symbol, - decimals, + spender, + value, ); - let result = api::set_metadata(id, name, symbol, decimals); + + let result = api::decrease_allowance(id, spender, value); ink::env::debug_println!("Result: {:?}", result); - result.map_err(|e| e.into()) + // result.map_err(|e| e.into()) + result + } + + /// 2. PSP-22 Metadata Interface: + /// - token_name + /// - token_symbol + /// - token_decimals + + #[ink(message)] + pub fn token_name(&self, id: AssetId) -> Result> { + // api::token_name(id).map_err(|e| e.into()) + api::token_name(id) } #[ink(message)] - pub fn asset_exists(&self, id: AssetId) -> Result { - api::asset_exists(id).map_err(|e| e.into()) + pub fn token_symbol(&self, id: AssetId) -> Result> { + // api::token_symbol(id).map_err(|e| e.into()) + api::token_symbol(id) } + + #[ink(message)] + pub fn token_decimals(&self, id: AssetId) -> Result { + // api::token_decimals(id).map_err(|e| e.into()) + api::token_decimals(id) + } + + // 3. Asset Management: + // - create + // - start_destroy + // - destroy_accounts + // - destroy_approvals + // - finish_destroy + // - set_metadata + // - clear_metadata + + // #[ink(message)] + // pub fn create(&self, id: AssetId, admin: AccountId32, min_balance: Balance) -> Result<()> { + // ink::env::debug_println!( + // "PopApiFungiblesExample::create: id: {:?} admin: {:?} min_balance: {:?}", + // id, + // admin, + // min_balance, + // ); + // let result = api::create(id, admin, min_balance); + // ink::env::debug_println!("Result: {:?}", result); + // // result.map_err(|e| e.into()) + // result + // } + + // #[ink(message)] + // pub fn set_metadata( + // &self, + // id: AssetId, + // name: Vec, + // symbol: Vec, + // decimals: u8, + // ) -> Result<()> { + // ink::env::debug_println!( + // "PopApiFungiblesExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}", + // id, + // name, + // symbol, + // decimals, + // ); + // let result = api::set_metadata(id, name, symbol, decimals); + // ink::env::debug_println!("Result: {:?}", result); + // // result.map_err(|e| e.into()) + // result + // } + // + // #[ink(message)] + // pub fn asset_exists(&self, id: AssetId) -> Result { + // // api::asset_exists(id).map_err(|e| e.into()) + // api::asset_exists(id) + // } } #[cfg(test)] diff --git a/pop-api/src/error.rs b/pop-api/src/error.rs index b8a56f55..54681e6d 100644 --- a/pop-api/src/error.rs +++ b/pop-api/src/error.rs @@ -14,79 +14,16 @@ impl From for StatusCode { } impl FromStatusCode for StatusCode { fn from_status_code(status_code: u32) -> Result<(), Self> { - match status_code { - 0 => Ok(()), - _ => { - let mut encoded = status_code.to_le_bytes(); - convert_unknown_errors(&mut encoded); - Err(StatusCode::from(u32::from_le_bytes(encoded))) - }, + if status_code == 0 { + return Ok(()); } + Err(StatusCode(status_code)) } } impl From for StatusCode { fn from(_: scale::Error) -> Self { - DecodingFailed.into() - } -} - -// If an unknown variant of the `DispatchError` is detected the error needs to be converted -// into the encoded value of `Error::Other`. This conversion is performed by shifting the bytes one -// position forward (discarding the last byte as it is not used) and setting the first byte to the -// encoded value of `Other` (0u8). This ensures the error is correctly categorized as an `Other` -// variant which provides all the necessary information to debug which error occurred in the runtime. -// -// Byte layout explanation: -// - Byte 0: index of the variant within `Error` -// - Byte 1: -// - Must be zero for `UNIT_ERRORS`. -// - Represents the nested error in `SINGLE_NESTED_ERRORS`. -// - Represents the first level of nesting in `DOUBLE_NESTED_ERRORS`. -// - Byte 2: -// - Represents the second level of nesting in `DOUBLE_NESTED_ERRORS`. -// - Byte 3: -// - Unused or represents further nested information. -// -// This mechanism ensures backward compatibility by correctly categorizing any unknown errors -// into the `Other` variant, thus preventing issues caused by breaking changes. -fn convert_unknown_errors(encoded_error: &mut [u8; 4]) { - let all_errors = [ - UNIT_ERRORS.as_slice(), - SINGLE_NESTED_ERRORS.as_slice(), - DOUBLE_NESTED_ERRORS.as_slice(), - // `DecodingFailed`. - &[255u8], - ] - .concat(); - // Unknown errors, i.e. an encoded value where the first byte is non-zero (indicating a variant - // in `Error`) but unknown. - if !all_errors.contains(&encoded_error[0]) { - encoded_error[..].rotate_right(1); - encoded_error[0] = 0u8; - } - convert_unknown_nested_errors(encoded_error); -} - -// If an unknown nested variant of the `DispatchError` is detected (i.e. when any of the subsequent -// bytes are non-zero). -fn convert_unknown_nested_errors(encoded_error: &mut [u8; 4]) { - // Converts single nested errors that are known to the Pop API as unit errors into `Other`. - if UNIT_ERRORS.contains(&encoded_error[0]) && encoded_error[1..].iter().any(|x| *x != 0u8) { - encoded_error[..].rotate_right(1); - encoded_error[0] = 0u8; - // Converts double nested errors that are known to the Pop API as single nested errors into - // `Other`. - } else if SINGLE_NESTED_ERRORS.contains(&encoded_error[0]) - && encoded_error[2..].iter().any(|x| *x != 0u8) - { - encoded_error[..].rotate_right(1); - encoded_error[0] = 0u8; - } else if DOUBLE_NESTED_ERRORS.contains(&encoded_error[0]) - && encoded_error[3..].iter().any(|x| *x != 0u8) - { - encoded_error[..].rotate_right(1); - encoded_error[0] = 0u8; + StatusCode(255u32) } } @@ -136,32 +73,7 @@ pub enum Error { DecodingFailed = 255, } -// Unit `Error` variants. -// (variant: index): -// - CannotLookup: 1, -// - BadOrigin: 2, -// - ConsumerRemaining: 4, -// - NoProviders: 5, -// - TooManyConsumers: 6, -// - Exhausted: 10, -// - Corruption: 11, -// - Unavailable: 12, -// - RootNotAllowed: 13, -// - DecodingFailed: 255, -const UNIT_ERRORS: [u8; 10] = [1, 2, 4, 5, 6, 10, 11, 12, 13, 255]; - -// Single nested `Error` variants. -// (variant: index): -// - Token: 7, -// - Arithmetic: 8, -// - Transaction: 9, -const SINGLE_NESTED_ERRORS: [u8; 3] = [7, 8, 9]; - -// Double nested `Error` variants -// (variant: index): -// - Module: 3, -const DOUBLE_NESTED_ERRORS: [u8; 1] = [3]; - +#[cfg(test)] impl From for StatusCode { fn from(value: Error) -> Self { let mut encoded_error = value.encode(); @@ -175,8 +87,15 @@ impl From for StatusCode { impl From for Error { fn from(value: StatusCode) -> Self { - let encoded: [u8; 4] = value.0.to_le_bytes(); - Error::decode(&mut &encoded[..]).unwrap_or(DecodingFailed) + let mut encoded: [u8; 4] = value.0.to_le_bytes(); + match Error::decode(&mut &encoded[..]) { + Err(_) => { + encoded[..].rotate_right(1); + encoded[0] = 0u8; + Error::decode(&mut &encoded[..]).unwrap_or(DecodingFailed) + }, + Ok(error) => error, + } } } @@ -286,28 +205,19 @@ mod tests { into_status_code([error_code, 1, 0, 0]), (Other { dispatch_error_index: error_code, error_index: 1, error: 0 }).into(), ); - assert_eq!( - into_error([error_code, 1, 0, 0]), - Other { dispatch_error_index: error_code, error_index: 1, error: 0 }, - ); + assert_eq!(into_error([error_code, 1, 0, 0]), errors[i]); // Unexpected third byte nested. assert_eq!( into_status_code([error_code, 1, 1, 0]), (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }).into(), ); - assert_eq!( - into_error([error_code, 1, 1, 0]), - Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, - ); + assert_eq!(into_error([error_code, 1, 1, 0]), errors[i]); // Unexpected fourth byte nested. assert_eq!( into_status_code([error_code, 1, 1, 1]), (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }).into(), ); - assert_eq!( - into_error([error_code, 1, 1, 1]), - Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, - ); + assert_eq!(into_error([error_code, 1, 1, 1]), errors[i]); } } @@ -341,19 +251,13 @@ mod tests { into_status_code([error_code, 1, 1, 0]), (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }).into(), ); - assert_eq!( - into_error([error_code, 1, 1, 0]), - Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, - ); + assert_eq!(into_error([error_code, 1, 1, 0]), errors[i][1]); // Unexpected fourth byte nested. assert_eq!( into_status_code([error_code, 1, 1, 1]), (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }).into(), ); - assert_eq!( - into_error([error_code, 1, 1, 1]), - Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, - ); + assert_eq!(into_error([error_code, 1, 1, 1]), errors[i][1]); } } @@ -385,9 +289,37 @@ mod tests { into_status_code([3, 1, 1, 1]), (Other { dispatch_error_index: 3, error_index: 1, error: 1 }).into(), ); + assert_eq!(into_error([3, 1, 1, 1]), Module { index: 1, error: 1 }); + } + + #[test] + fn single_nested_unknown_variants() { + // Unknown `TokenError` variant. + assert_eq!( + into_error([7, 10, 0, 0]), + Other { dispatch_error_index: 7, error_index: 10, error: 0 } + ); + assert_eq!( + into_status_code([7, 10, 0, 0]), + Other { dispatch_error_index: 7, error_index: 10, error: 0 }.into() + ); + // Unknown `Arithmetic` variant. + assert_eq!( + into_error([8, 3, 0, 0]), + Other { dispatch_error_index: 8, error_index: 3, error: 0 } + ); + assert_eq!( + into_status_code([8, 3, 0, 0]), + Other { dispatch_error_index: 8, error_index: 3, error: 0 }.into() + ); + // Unknown `Transactional` variant. + assert_eq!( + into_error([9, 2, 0, 0]), + Other { dispatch_error_index: 9, error_index: 2, error: 0 } + ); assert_eq!( - into_error([3, 1, 1, 1]), - Other { dispatch_error_index: 3, error_index: 1, error: 1 }, + into_status_code([9, 2, 0, 0]), + Other { dispatch_error_index: 9, error_index: 2, error: 0 }.into() ); } diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 04d08bce..53bbadc6 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -1,9 +1,9 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] use ink::{prelude::vec::Vec, ChainExtensionInstance}; -pub use sp_runtime::{MultiAddress, MultiSignature}; +pub use sp_runtime::MultiAddress; -use crate::error::{Error, StatusCode}; +use crate::error::StatusCode; use primitives::{storage_keys::*, AccountId as AccountId32}; #[cfg(feature = "assets")] pub use v0::assets; @@ -62,12 +62,14 @@ pub trait PopApi { fn send_xcm(xcm: primitives::cross_chain::CrossChainMessage) -> Result<()>; } +#[inline] fn dispatch(call: RuntimeCall) -> Result<()> { <::ChainExtension as ChainExtensionInstance>::instantiate( ) .dispatch(call) } +#[inline] fn read_state(key: RuntimeStateKeys) -> Result> { <::ChainExtension as ChainExtensionInstance>::instantiate( ) diff --git a/pop-api/src/primitives.rs b/pop-api/src/primitives.rs index e174a111..17419d5b 100644 --- a/pop-api/src/primitives.rs +++ b/pop-api/src/primitives.rs @@ -1 +1,2 @@ pub use pop_primitives::*; +pub use sp_runtime::MultiAddress; diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index eaf1c6f5..e7c3f41c 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -1,7 +1,12 @@ -use crate::{assets, primitives::AssetId, AccountId, Balance, MultiAddress, StatusCode}; use ink::prelude::vec::Vec; use scale::Encode; +use crate::{ + assets, + primitives::{AssetId, MultiAddress}, + AccountId, Balance, StatusCode, +}; + type Result = core::result::Result; /// Local Fungibles: @@ -26,6 +31,7 @@ type Result = core::result::Result; /// /// # Returns /// The total supply of the token, or an error if the operation fails. +#[inline] pub fn total_supply(id: AssetId) -> Result { assets::total_supply(id) } @@ -39,6 +45,7 @@ pub fn total_supply(id: AssetId) -> Result { /// /// # Returns /// The balance of the specified account, or an error if the operation fails. +#[inline] pub fn balance_of(id: AssetId, owner: AccountId) -> Result { assets::balance_of(id, owner) } @@ -53,6 +60,7 @@ pub fn balance_of(id: AssetId, owner: AccountId) -> Result { /// /// # Returns /// The remaining allowance, or an error if the operation fails. +#[inline] pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { assets::allowance(id, owner, spender) } @@ -67,6 +75,7 @@ pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result>, @@ -87,19 +96,15 @@ pub fn transfer( /// /// # Returns /// Returns `Ok(())` if successful, or an error if the transfer fails. +#[inline] pub fn transfer_from( id: AssetId, - from: Option>>, - to: Option>>, + from: impl Into>, + to: impl Into>, value: Balance, _data: &[u8], ) -> Result<()> { - match (from, to) { - (None, Some(to)) => assets::mint(id, to, value), - (Some(from), None) => assets::burn(id, from, value), - (Some(from), Some(to)) => assets::transfer_approved(id, from, to, value), - _ => Ok(()), - } + assets::transfer_approved(id, from, to, value) } /// Approves an account to spend a specified number of tokens on behalf of the caller. @@ -111,16 +116,10 @@ pub fn transfer_from( /// /// # Returns /// Returns `Ok(())` if successful, or an error if the approval fails. -// #[allow(unused_variables)] -// fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { -// todo!() -// // TODO: read allowance and increase or decrease. -// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { -// // id: id.into(), -// // delegate: spender.into(), -// // amount: Compact(value), -// // }))?) -// } +#[inline] +pub fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { + assets::approve_transfer(id, spender, value) +} /// Increases the allowance of a spender. /// @@ -131,13 +130,10 @@ pub fn transfer_from( /// /// # Returns /// Returns `Ok(())` if successful, or an error if the operation fails. -// fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { -// id: id.into(), -// delegate: spender.into(), -// amount: Compact(value), -// }))?) -// } +#[inline] +pub fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { + assets::approve_transfer(id, spender, value) +} /// Decreases the allowance of a spender. /// @@ -148,20 +144,11 @@ pub fn transfer_from( /// /// # Returns /// Returns `Ok(())` if successful, or an error if the operation fails. -// #[allow(unused_variables)] -// fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { -// todo!() -// // TODO: cancel_approval + approve_transfer -// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::CancelApproval { -// // id: id.into(), -// // delegate: delegate.into(), -// // }))?) -// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { -// // id: id.into(), -// // delegate: spender.into(), -// // amount: Compact(value), -// // }))?) -// } +#[inline] +pub fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { + assets::cancel_approval(id, spender.clone())?; + assets::approve_transfer(id, spender, value) +} /// 2. PSP-22 Metadata Interface: /// - token_name @@ -175,11 +162,10 @@ pub fn transfer_from( /// /// # Returns /// The name of the token as a byte vector, or an error if the operation fails. -// #[allow(unused_variables)] -// pub fn token_name(id: AssetId) -> Result>> { -// todo!() -// // Ok(state::read(RuntimeStateKeys::Assets(AssetsKeys::TokenName(id)))?) -// } +#[inline] +pub fn token_name(id: AssetId) -> Result> { + assets::token_name(id) +} /// Returns the token symbol for a given asset ID. /// @@ -188,10 +174,10 @@ pub fn transfer_from( /// /// # Returns /// The symbol of the token as a byte vector, or an error if the operation fails. -// #[allow(unused_variables)] -// fn token_symbol(id: AssetId) -> Result>> { -// todo!() -// } +#[inline] +pub fn token_symbol(id: AssetId) -> Result> { + assets::token_symbol(id) +} /// Returns the token decimals for a given asset ID. /// @@ -200,118 +186,114 @@ pub fn transfer_from( /// /// # Returns /// The number of decimals of the token as a byte vector, or an error if the operation fails. -// #[allow(unused_variables)] -// fn token_decimals(id: AssetId) -> Result>> { -// todo!() -// } - -/// 3. Asset Management: -/// - create -/// - start_destroy -/// - destroy_accounts -/// - destroy_approvals -/// - finish_destroy -/// - set_metadata -/// - clear_metadata - -/// Create a new token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `admin` - The account that will administer the asset. -/// * `min_balance` - The minimum balance required for accounts holding this asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the creation fails. -pub fn create( - id: AssetId, - admin: impl Into>, - min_balance: Balance, -) -> Result<()> { - assets::create(id, admin, min_balance) +#[inline] +pub fn token_decimals(id: AssetId) -> Result { + assets::token_decimals(id) } -/// Start the process of destroying a token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -// fn start_destroy(id: AssetId) -> Result<()> { -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::StartDestroy { -// id: id.into(), -// }))?) +// /// 3. Asset Management: +// /// - create +// /// - start_destroy +// /// - destroy_accounts +// /// - destroy_approvals +// /// - finish_destroy +// /// - set_metadata +// /// - clear_metadata +// +// /// Create a new token with a given asset ID. +// /// +// /// # Arguments +// /// * `id` - The ID of the asset. +// /// * `admin` - The account that will administer the asset. +// /// * `min_balance` - The minimum balance required for accounts holding this asset. +// /// +// /// # Returns +// /// Returns `Ok(())` if successful, or an error if the creation fails. +// // pub fn create(id: AssetId, admin: impl Into>, min_balance: Balance) -> Result<()> { +// // assets::create(id, admin, min_balance) +// // } +// +// /// Start the process of destroying a token with a given asset ID. +// /// +// /// # Arguments +// /// * `id` - The ID of the asset. +// /// +// /// # Returns +// /// Returns `Ok(())` if successful, or an error if the operation fails. +// // fn start_destroy(id: AssetId) -> Result<()> { +// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::StartDestroy { +// // id: id.into(), +// // }))?) +// // } +// +// /// Destroy all accounts associated with a token with a given asset ID. +// /// +// /// # Arguments +// /// * `id` - The ID of the asset. +// /// +// /// # Returns +// /// Returns `Ok(())` if successful, or an error if the operation fails. +// // fn destroy_accounts(id: AssetId) -> Result<()> { +// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::DestroyAccounts { +// // id: id.into(), +// // }))?) +// // } +// +// /// Destroy all approvals associated with a token with a given asset ID. +// /// +// /// # Arguments +// /// * `id` - The ID of the asset. +// /// +// /// # Returns +// /// Returns `Ok(())` if successful, or an error if the operation fails. +// // fn destroy_approvals(id: AssetId) -> Result<()> { +// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::DestroyApprovals { +// // id: id.into(), +// // }))?) +// // } +// +// /// Complete the process of destroying a token with a given asset ID. +// /// +// /// # Arguments +// /// * `id` - The ID of the asset. +// /// +// /// # Returns +// /// Returns `Ok(())` if successful, or an error if the operation fails. +// // fn finish_destroy(id: AssetId) -> Result<()> { +// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::FinishDestroy { +// // id: id.into(), +// // }))?) +// // } +// +// /// Set the metadata for a token with a given asset ID. +// /// +// /// # Arguments +// /// * `id` - The ID of the asset. +// /// +// /// # Returns +// /// Returns `Ok(())` if successful, or an error if the operation fails. +// // pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) -> Result<()> { +// // assets::set_metadata(id, name, symbol, decimals) +// // } +// +// /// Clear the metadata for a token with a given asset ID. +// /// +// /// # Arguments +// /// * `id` - The ID of the asset. +// /// +// /// # Returns +// /// Returns `Ok(())` if successful, or an error if the operation fails. +// // fn clear_metadata(id: AssetId) -> Result<()> { +// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::ClearMetadata { +// // id: id.into(), +// // }))?) +// // } +// +// pub fn asset_exists(id: AssetId) -> Result { +// assets::asset_exists(id) // } -/// Destroy all accounts associated with a token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -// fn destroy_accounts(id: AssetId) -> Result<()> { -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::DestroyAccounts { -// id: id.into(), -// }))?) -// } - -/// Destroy all approvals associated with a token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -// fn destroy_approvals(id: AssetId) -> Result<()> { -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::DestroyApprovals { -// id: id.into(), -// }))?) -// } - -/// Complete the process of destroying a token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -// fn finish_destroy(id: AssetId) -> Result<()> { -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::FinishDestroy { -// id: id.into(), -// }))?) -// } - -/// Set the metadata for a token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) -> Result<()> { - assets::set_metadata(id, name, symbol, decimals) -} - -/// Clear the metadata for a token with a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. -// fn clear_metadata(id: AssetId) -> Result<()> { -// Ok(dispatch(RuntimeCall::Assets(AssetsCall::ClearMetadata { -// id: id.into(), -// }))?) -// } - -pub fn asset_exists(id: AssetId) -> Result { - assets::asset_exists(id) -} - -#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] +#[derive(Encode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum FungiblesError { Other(StatusCode), @@ -359,11 +341,13 @@ impl From for FungiblesError { #[cfg(test)] mod tests { + use scale::Decode; + use super::FungiblesError; - use crate::error::{ + use crate::StatusCode; + use pop_primitives::{ ArithmeticError::*, Error::{self, *}, - StatusCode, TokenError::*, TransactionalError::*, }; @@ -373,6 +357,18 @@ mod tests { status_code.into() } + #[test] + fn status_code_vs_encoded() { + assert_eq!(u32::decode(&mut &[3u8, 10, 2, 0][..]).unwrap(), 133635u32); + assert_eq!(u32::decode(&mut &[3u8, 52, 0, 0][..]).unwrap(), 13315u32); + assert_eq!(u32::decode(&mut &[3u8, 52, 1, 0][..]).unwrap(), 78851u32); + assert_eq!(u32::decode(&mut &[3u8, 52, 2, 0][..]).unwrap(), 144387u32); + assert_eq!(u32::decode(&mut &[3u8, 52, 3, 0][..]).unwrap(), 209923u32); + assert_eq!(u32::decode(&mut &[3u8, 52, 5, 0][..]).unwrap(), 340995u32); + assert_eq!(u32::decode(&mut &[3u8, 52, 7, 0][..]).unwrap(), 472067u32); + assert_eq!(u32::decode(&mut &[3u8, 52, 10, 0][..]).unwrap(), 668675u32); + } + #[test] fn conversion_status_code_into_fungibles_error_works() { let errors = vec![ diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index 778387cb..22e32d47 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1,10 +1,9 @@ -#![allow(dead_code)] - -use crate::{Balance, MultiAddress, RuntimeCall, *}; use ink::prelude::vec::Vec; -use primitives::AssetId; use scale::{Compact, Encode}; +use crate::{state::read, Balance, RuntimeCall, *}; +use primitives::{AssetId, MultiAddress}; + pub mod fungibles; type Result = core::result::Result; @@ -44,65 +43,62 @@ type Result = core::result::Result; /// - block /// Issue a new class of fungible assets from a public origin. -pub(crate) fn create( - id: AssetId, - admin: impl Into>, - min_balance: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Create { - id: id.into(), - admin: admin.into(), - min_balance, - })) -} - -/// Start the process of destroying a fungible asset class. -pub(crate) fn start_destroy(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::StartDestroy { id: id.into() })) -} - -/// Destroy all accounts associated with a given asset. -pub(crate) fn destroy_accounts(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::DestroyAccounts { id: id.into() })) -} - -/// Destroy all approvals associated with a given asset up to the max (see runtime configuration Assets `RemoveItemsLimit`). -pub(crate) fn destroy_approvals(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::DestroyApprovals { id: id.into() })) -} - -/// Complete destroying asset and unreserve currency. -pub(crate) fn finish_destroy(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::FinishDestroy { id: id.into() })) -} - -/// Mint assets of a particular class. -pub(crate) fn mint( - id: AssetId, - beneficiary: impl Into>, - amount: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Mint { - id: id.into(), - beneficiary: beneficiary.into(), - amount: Compact(amount), - })) -} - -/// Reduce the balance of `who` by as much as possible up to `amount` assets of `id`. -pub(crate) fn burn( - id: AssetId, - who: impl Into>, - amount: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Burn { - id: id.into(), - who: who.into(), - amount: Compact(amount), - })) -} - -/// Move some assets from the sender account to another. +// pub(crate) fn create( +// id: AssetId, +// admin: impl Into>, +// min_balance: Balance, +// ) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::Create { +// id: id.into(), +// admin: admin.into(), +// min_balance, +// })) +// } +// +// /// Start the process of destroying a fungible asset class. +// pub(crate) fn start_destroy(id: AssetId) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::StartDestroy { id: id.into() })) +// } +// +// /// Destroy all accounts associated with a given asset. +// pub(crate) fn destroy_accounts(id: AssetId) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::DestroyAccounts { id: id.into() })) +// } +// +// /// Destroy all approvals associated with a given asset up to the max (see runtime configuration Assets `RemoveItemsLimit`). +// pub(crate) fn destroy_approvals(id: AssetId) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::DestroyApprovals { id: id.into() })) +// } +// +// /// Complete destroying asset and unreserve currency. +// pub(crate) fn finish_destroy(id: AssetId) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::FinishDestroy { id: id.into() })) +// } + +// /// Mint assets of a particular class. +// pub(crate) fn mint( +// id: AssetId, +// beneficiary: impl Into>, +// amount: Balance, +// ) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::Mint { +// id: id.into(), +// beneficiary: beneficiary.into(), +// amount: Compact(amount), +// })) +// } +// +// /// Reduce the balance of `who` by as much as possible up to `amount` assets of `id`. +// pub(crate) fn burn(id: AssetId, who: impl Into>, amount: Balance) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::Burn { +// id: id.into(), +// who: who.into(), +// amount: Compact(amount), +// })) +// } + +// /// Move some assets from the sender account to another. +#[inline] pub(crate) fn transfer( id: AssetId, target: impl Into>, @@ -115,98 +111,96 @@ pub(crate) fn transfer( })) } -/// Move some assets from the sender account to another, keeping the sender account alive. -pub(crate) fn transfer_keep_alive( - id: AssetId, - target: impl Into>, - amount: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { - id: id.into(), - target: target.into(), - amount: Compact(amount), - })) -} - -/// Move some assets from one account to another. Sender should be the Admin of the asset `id`. -pub(crate) fn force_transfer( - id: AssetId, - source: impl Into>, - dest: impl Into>, - amount: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::ForceTransfer { - id: id.into(), - source: source.into(), - dest: dest.into(), - amount: Compact(amount), - })) -} - -/// Disallow further unprivileged transfers of an asset `id` from an account `who`. `who` -/// must already exist as an entry in `Account`s of the asset. If you want to freeze an -/// account that does not have an entry, use `touch_other` first. -pub(crate) fn freeze(id: AssetId, who: impl Into>) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Freeze { id: id.into(), who: who.into() })) -} - -/// Allow unprivileged transfers to and from an account again. -pub(crate) fn thaw(id: AssetId, who: impl Into>) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Thaw { id: id.into(), who: who.into() })) -} - -/// Disallow further unprivileged transfers for the asset class. -pub(crate) fn freeze_asset(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::FreezeAsset { id: id.into() })) -} - -/// Allow unprivileged transfers for the asset again. -pub(crate) fn thaw_asset(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::ThawAsset { id: id.into() })) -} - -/// Change the Owner of an asset. -pub(crate) fn transfer_ownership( - id: AssetId, - owner: impl Into>, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::TransferOwnership { - id: id.into(), - owner: owner.into(), - })) -} - -/// Change the Issuer, Admin and Freezer of an asset. -pub(crate) fn set_team( - id: AssetId, - issuer: impl Into>, - admin: impl Into>, - freezer: impl Into>, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::SetTeam { - id: id.into(), - issuer: issuer.into(), - admin: admin.into(), - freezer: freezer.into(), - })) -} +// /// Move some assets from the sender account to another, keeping the sender account alive. +// pub(crate) fn transfer_keep_alive( +// id: AssetId, +// target: impl Into>, +// amount: Balance, +// ) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { +// id: id.into(), +// target: target.into(), +// amount: Compact(amount), +// })) +// } +// +// /// Move some assets from one account to another. Sender should be the Admin of the asset `id`. +// pub(crate) fn force_transfer( +// id: AssetId, +// source: impl Into>, +// dest: impl Into>, +// amount: Balance, +// ) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::ForceTransfer { +// id: id.into(), +// source: source.into(), +// dest: dest.into(), +// amount: Compact(amount), +// })) +// } +// +// /// Disallow further unprivileged transfers of an asset `id` from an account `who`. `who` +// /// must already exist as an entry in `Account`s of the asset. If you want to freeze an +// /// account that does not have an entry, use `touch_other` first. +// pub(crate) fn freeze(id: AssetId, who: impl Into>) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::Freeze { id: id.into(), who: who.into() })) +// } +// +// /// Allow unprivileged transfers to and from an account again. +// pub(crate) fn thaw(id: AssetId, who: impl Into>) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::Thaw { id: id.into(), who: who.into() })) +// } +// +// /// Disallow further unprivileged transfers for the asset class. +// pub(crate) fn freeze_asset(id: AssetId) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::FreezeAsset { id: id.into() })) +// } +// +// /// Allow unprivileged transfers for the asset again. +// pub(crate) fn thaw_asset(id: AssetId) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::ThawAsset { id: id.into() })) +// } +// +// /// Change the Owner of an asset. +// pub(crate) fn transfer_ownership(id: AssetId, owner: impl Into>) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::TransferOwnership { +// id: id.into(), +// owner: owner.into(), +// })) +// } +// +// /// Change the Issuer, Admin and Freezer of an asset. +// pub(crate) fn set_team( +// id: AssetId, +// issuer: impl Into>, +// admin: impl Into>, +// freezer: impl Into>, +// ) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::SetTeam { +// id: id.into(), +// issuer: issuer.into(), +// admin: admin.into(), +// freezer: freezer.into(), +// })) +// } /// Set the metadata for an asset. -pub(crate) fn set_metadata( - id: AssetId, - name: Vec, - symbol: Vec, - decimals: u8, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::SetMetadata { id: id.into(), name, symbol, decimals })) -} - -/// Clear the metadata for an asset. -pub(crate) fn clear_metadata(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::ClearMetadata { id: id.into() })) -} - -/// Approve an amount of asset for transfer by a delegated third-party account. +// pub(crate) fn set_metadata( +// id: AssetId, +// name: Vec, +// symbol: Vec, +// decimals: u8, +// ) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::SetMetadata { id: id.into(), name, symbol, decimals })) +// } + +// /// Clear the metadata for an asset. +// pub(crate) fn clear_metadata(id: AssetId) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::ClearMetadata { id: id.into() })) +// } + +// /// Approve an amount of asset for transfer by a delegated third-party account. +#[inline] pub(crate) fn approve_transfer( id: AssetId, delegate: impl Into>, @@ -220,6 +214,7 @@ pub(crate) fn approve_transfer( } /// Cancel all of some asset approved for delegated transfer by a third-party account. +#[inline] pub(crate) fn cancel_approval( id: AssetId, delegate: impl Into>, @@ -230,21 +225,22 @@ pub(crate) fn cancel_approval( })) } -/// Cancel all of some asset approved for delegated transfer by a third-party account. -pub(crate) fn force_cancel_approval( - id: AssetId, - owner: impl Into>, - delegate: impl Into>, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::ForceCancelApproval { - id: id.into(), - owner: owner.into(), - delegate: delegate.into(), - })) -} +// /// Cancel all of some asset approved for delegated transfer by a third-party account. +// pub(crate) fn force_cancel_approval( +// id: AssetId, +// owner: impl Into>, +// delegate: impl Into>, +// ) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::ForceCancelApproval { +// id: id.into(), +// owner: owner.into(), +// delegate: delegate.into(), +// })) +// } /// Transfer some asset balance from a previously delegated account to some third-party /// account. +#[inline] pub(crate) fn transfer_approved( id: AssetId, owner: impl Into>, @@ -258,58 +254,81 @@ pub(crate) fn transfer_approved( amount: Compact(amount), })) } - -/// Create an asset account for non-provider assets. -pub(crate) fn touch(id: AssetId) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Touch { id: id.into() })) -} - -/// Return the deposit (if any) of an asset account or a consumer reference (if any) of an -/// account. -pub(crate) fn refund(id: AssetId, allow_burn: bool) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Refund { id: id.into(), allow_burn })) -} - -/// Sets the minimum balance of an asset. -pub(crate) fn set_min_balance(id: AssetId, min_balance: Balance) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::SetMinBalance { - id: id.into(), - min_balance: Compact(min_balance), - })) -} - -/// Create an asset account for `who`. -pub(crate) fn touch_other(id: AssetId, who: impl Into>) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::TouchOther { id: id.into(), who: who.into() })) -} - -/// Return the deposit (if any) of a target asset account. Useful if you are the depositor. -pub(crate) fn refund_other(id: AssetId, who: impl Into>) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::RefundOther { id: id.into(), who: who.into() })) -} - -/// Disallow further unprivileged transfers of an asset `id` to and from an account `who`. -pub(crate) fn block(id: AssetId, who: impl Into>) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::Block { id: id.into(), who: who.into() })) -} +// +// /// Create an asset account for non-provider assets. +// pub(crate) fn touch(id: AssetId) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::Touch { id: id.into() })) +// } +// +// /// Return the deposit (if any) of an asset account or a consumer reference (if any) of an +// /// account. +// pub(crate) fn refund(id: AssetId, allow_burn: bool) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::Refund { id: id.into(), allow_burn })) +// } +// +// /// Sets the minimum balance of an asset. +// pub(crate) fn set_min_balance(id: AssetId, min_balance: Balance) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::SetMinBalance { +// id: id.into(), +// min_balance: Compact(min_balance), +// })) +// } +// +// /// Create an asset account for `who`. +// pub(crate) fn touch_other(id: AssetId, who: impl Into>) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::TouchOther { id: id.into(), who: who.into() })) +// } +// +// /// Return the deposit (if any) of a target asset account. Useful if you are the depositor. +// pub(crate) fn refund_other(id: AssetId, who: impl Into>) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::RefundOther { id: id.into(), who: who.into() })) +// } +// +// /// Disallow further unprivileged transfers of an asset `id` to and from an account `who`. +// pub(crate) fn block(id: AssetId, who: impl Into>) -> Result<()> { +// dispatch(RuntimeCall::Assets(AssetsCall::Block { id: id.into(), who: who.into() })) +// } /// 2. Read state functions /// - total_supply -/// - - +/// - balance_of +/// - allowance +/// - asset_exists +/// - token_name +/// - token_symbol +/// - token_decimals +// +#[inline] pub(crate) fn total_supply(id: AssetId) -> Result { state::read(RuntimeStateKeys::Assets(AssetsKeys::TotalSupply(id))) } +#[inline] pub(crate) fn balance_of(id: AssetId, owner: AccountId) -> Result { state::read(RuntimeStateKeys::Assets(AssetsKeys::BalanceOf(id, owner))) } +#[inline] pub(crate) fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { state::read(RuntimeStateKeys::Assets(AssetsKeys::Allowance(id, owner, spender))) } -pub(crate) fn asset_exists(id: AssetId) -> Result { - state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id))) +// pub(crate) fn asset_exists(id: AssetId) -> Result { +// state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id))) +// } + +#[inline] +pub(crate) fn token_name(id: AssetId) -> Result> { + state::read(RuntimeStateKeys::Assets(AssetsKeys::TokenName(id))) +} +// +#[inline] +pub(crate) fn token_symbol(id: AssetId) -> Result> { + state::read(RuntimeStateKeys::Assets(AssetsKeys::TokenSymbol(id))) +} + +#[inline] +pub(crate) fn token_decimals(id: AssetId) -> Result { + state::read(RuntimeStateKeys::Assets(AssetsKeys::TokenDecimals(id))) } // Parameters to extrinsics representing an asset id (`AssetIdParameter`) and a balance amount (`Balance`) are expected @@ -321,63 +340,63 @@ pub(crate) fn asset_exists(id: AssetId) -> Result { type AssetIdParameter = Compact; // Balance amount that is compact encoded. type BalanceParameter = Compact; - +// #[derive(Encode)] pub(crate) enum AssetsCall { - #[codec(index = 0)] - Create { id: AssetIdParameter, admin: MultiAddress, min_balance: Balance }, - #[codec(index = 2)] - StartDestroy { id: AssetIdParameter }, - #[codec(index = 3)] - DestroyAccounts { id: AssetIdParameter }, - #[codec(index = 4)] - DestroyApprovals { id: AssetIdParameter }, - #[codec(index = 5)] - FinishDestroy { id: AssetIdParameter }, - #[codec(index = 6)] - Mint { - id: AssetIdParameter, - beneficiary: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 7)] - Burn { id: AssetIdParameter, who: MultiAddress, amount: BalanceParameter }, - #[codec(index = 8)] - Transfer { id: AssetIdParameter, target: MultiAddress, amount: BalanceParameter }, + // #[codec(index = 0)] + // Create { id: AssetIdParameter, admin: MultiAddress, min_balance: Balance }, + // #[codec(index = 2)] + // StartDestroy { id: AssetIdParameter }, + // #[codec(index = 3)] + // DestroyAccounts { id: AssetIdParameter }, + // #[codec(index = 4)] + // DestroyApprovals { id: AssetIdParameter }, + // #[codec(index = 5)] + // FinishDestroy { id: AssetIdParameter }, + // #[codec(index = 6)] + // Mint { + // id: AssetIdParameter, + // beneficiary: MultiAddress, + // amount: BalanceParameter, + // }, + // #[codec(index = 7)] + // Burn { id: AssetIdParameter, who: MultiAddress, amount: BalanceParameter }, + // #[codec(index = 8)] + // Transfer { id: AssetIdParameter, target: MultiAddress, amount: BalanceParameter }, #[codec(index = 9)] TransferKeepAlive { id: AssetIdParameter, target: MultiAddress, amount: BalanceParameter, }, - #[codec(index = 10)] - ForceTransfer { - id: AssetIdParameter, - source: MultiAddress, - dest: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 11)] - Freeze { id: AssetIdParameter, who: MultiAddress }, - #[codec(index = 12)] - Thaw { id: AssetIdParameter, who: MultiAddress }, - #[codec(index = 13)] - FreezeAsset { id: AssetIdParameter }, - #[codec(index = 14)] - ThawAsset { id: AssetIdParameter }, - #[codec(index = 15)] - TransferOwnership { id: AssetIdParameter, owner: MultiAddress }, - #[codec(index = 16)] - SetTeam { - id: AssetIdParameter, - issuer: MultiAddress, - admin: MultiAddress, - freezer: MultiAddress, - }, - #[codec(index = 17)] - SetMetadata { id: AssetIdParameter, name: Vec, symbol: Vec, decimals: u8 }, - #[codec(index = 18)] - ClearMetadata { id: AssetIdParameter }, + // #[codec(index = 10)] + // ForceTransfer { + // id: AssetIdParameter, + // source: MultiAddress, + // dest: MultiAddress, + // amount: BalanceParameter, + // }, + // #[codec(index = 11)] + // Freeze { id: AssetIdParameter, who: MultiAddress }, + // #[codec(index = 12)] + // Thaw { id: AssetIdParameter, who: MultiAddress }, + // #[codec(index = 13)] + // FreezeAsset { id: AssetIdParameter }, + // #[codec(index = 14)] + // ThawAsset { id: AssetIdParameter }, + // #[codec(index = 15)] + // TransferOwnership { id: AssetIdParameter, owner: MultiAddress }, + // #[codec(index = 16)] + // SetTeam { + // id: AssetIdParameter, + // issuer: MultiAddress, + // admin: MultiAddress, + // freezer: MultiAddress, + // }, + // #[codec(index = 17)] + // SetMetadata { id: AssetIdParameter, name: Vec, symbol: Vec, decimals: u8 }, + // #[codec(index = 18)] + // ClearMetadata { id: AssetIdParameter }, #[codec(index = 22)] ApproveTransfer { id: AssetIdParameter, @@ -386,12 +405,12 @@ pub(crate) enum AssetsCall { }, #[codec(index = 23)] CancelApproval { id: AssetIdParameter, delegate: MultiAddress }, - #[codec(index = 24)] - ForceCancelApproval { - id: AssetIdParameter, - owner: MultiAddress, - delegate: MultiAddress, - }, + // #[codec(index = 24)] + // ForceCancelApproval { + // id: AssetIdParameter, + // owner: MultiAddress, + // delegate: MultiAddress, + // }, #[codec(index = 25)] TransferApproved { id: AssetIdParameter, @@ -399,96 +418,97 @@ pub(crate) enum AssetsCall { destination: MultiAddress, amount: BalanceParameter, }, - #[codec(index = 26)] - Touch { id: AssetIdParameter }, - #[codec(index = 27)] - Refund { id: AssetIdParameter, allow_burn: bool }, - #[codec(index = 28)] - SetMinBalance { id: AssetIdParameter, min_balance: BalanceParameter }, - #[codec(index = 29)] - TouchOther { id: AssetIdParameter, who: MultiAddress }, - #[codec(index = 30)] - RefundOther { id: AssetIdParameter, who: MultiAddress }, - #[codec(index = 31)] - Block { id: AssetIdParameter, who: MultiAddress }, -} - -// TODO: Not being used atm but necessary if we want to provide access to the -// rest of the pallet, outside of the use cases. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum AssetsError { - /// Account balance must be greater than or equal to the transfer amount. - BalanceLow, - /// The account to alter does not exist. - NoAccount, - /// The signing account has no permission to do the operation. - NoPermission, - /// The given asset ID is unknown. - Unknown, - /// The origin account is frozen. - Frozen, - /// The asset ID is already taken. - InUse, - /// Invalid witness data given. - BadWitness, - /// Minimum balance should be non-zero. - MinBalanceZero, - /// Unable to increment the consumer reference counters on the account. Either no provider - /// reference exists to allow a non-zero balance of a non-self-sufficient asset, or one - /// fewer then the maximum number of consumers has been reached. - UnavailableConsumer, - /// Invalid metadata given. - BadMetadata, - /// No approval exists that would allow the transfer. - Unapproved, - /// The source account would not survive the transfer and it needs to stay alive. - WouldDie, - /// The asset-account already exists. - AlreadyExists, - /// The asset-account doesn't have an associated deposit. - NoDeposit, - /// The operation would result in funds being burned. - WouldBurn, - /// The asset is a live asset and is actively being used. Usually emit for operations such - /// as `start_destroy` which require the asset to be in a destroying state. - LiveAsset, - /// The asset is not live, and likely being destroyed. - AssetNotLive, - /// The asset status is not the expected status. - IncorrectStatus, - /// The asset should be frozen before the given operation. - NotFrozen, - /// Callback action resulted in error. - CallbackFailed, + // // #[codec(index = 26)] + // // Touch { id: AssetIdParameter }, + // // #[codec(index = 27)] + // // Refund { id: AssetIdParameter, allow_burn: bool }, + // // #[codec(index = 28)] + // // SetMinBalance { id: AssetIdParameter, min_balance: BalanceParameter }, + // // #[codec(index = 29)] + // // TouchOther { id: AssetIdParameter, who: MultiAddress }, + // // #[codec(index = 30)] + // // RefundOther { id: AssetIdParameter, who: MultiAddress }, + // // #[codec(index = 31)] + // // Block { id: AssetIdParameter, who: MultiAddress }, + // } + + // // TODO: Not being used atm but necessary if we want to provide access to the + // // rest of the pallet, outside of the use cases. + // #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] + // #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] + // pub enum AssetsError { + // /// Account balance must be greater than or equal to the transfer amount. + // BalanceLow, + // /// The account to alter does not exist. + // NoAccount, + // /// The signing account has no permission to do the operation. + // NoPermission, + // /// The given asset ID is unknown. + // Unknown, + // /// The origin account is frozen. + // Frozen, + // /// The asset ID is already taken. + // InUse, + // /// Invalid witness data given. + // BadWitness, + // /// Minimum balance should be non-zero. + // MinBalanceZero, + // /// Unable to increment the consumer reference counters on the account. Either no provider + // /// reference exists to allow a non-zero balance of a non-self-sufficient asset, or one + // /// fewer then the maximum number of consumers has been reached. + // UnavailableConsumer, + // /// Invalid metadata given. + // BadMetadata, + // /// No approval exists that would allow the transfer. + // Unapproved, + // /// The source account would not survive the transfer and it needs to stay alive. + // WouldDie, + // /// The asset-account already exists. + // AlreadyExists, + // /// The asset-account doesn't have an associated deposit. + // NoDeposit, + // /// The operation would result in funds being burned. + // WouldBurn, + // /// The asset is a live asset and is actively being used. Usually emit for operations such + // /// as `start_destroy` which require the asset to be in a destroying state. + // LiveAsset, + // /// The asset is not live, and likely being destroyed. + // AssetNotLive, + // /// The asset status is not the expected status. + // IncorrectStatus, + // /// The asset should be frozen before the given operation. + // NotFrozen, + // /// Callback action resulted in error. + // CallbackFailed, } -impl TryFrom for AssetsError { - type Error = Error; - - fn try_from(status_code: u32) -> core::result::Result { - use AssetsError::*; - match status_code { - 0 => Ok(BalanceLow), - 1 => Ok(NoAccount), - 2 => Ok(NoPermission), - 3 => Ok(Unknown), - 4 => Ok(Frozen), - 5 => Ok(InUse), - 6 => Ok(BadWitness), - 7 => Ok(MinBalanceZero), - 8 => Ok(UnavailableConsumer), - 9 => Ok(BadMetadata), - 10 => Ok(Unapproved), - 11 => Ok(WouldDie), - 12 => Ok(AlreadyExists), - 13 => Ok(NoDeposit), - 14 => Ok(WouldBurn), - 15 => Ok(LiveAsset), - 16 => Ok(AssetNotLive), - 17 => Ok(IncorrectStatus), - 18 => Ok(NotFrozen), - _ => todo!(), - } - } -} +// +// impl TryFrom for AssetsError { +// type Error = Error; +// +// fn try_from(status_code: u32) -> core::result::Result { +// use AssetsError::*; +// match status_code { +// 0 => Ok(BalanceLow), +// 1 => Ok(NoAccount), +// 2 => Ok(NoPermission), +// 3 => Ok(Unknown), +// 4 => Ok(Frozen), +// 5 => Ok(InUse), +// 6 => Ok(BadWitness), +// 7 => Ok(MinBalanceZero), +// 8 => Ok(UnavailableConsumer), +// 9 => Ok(BadMetadata), +// 10 => Ok(Unapproved), +// 11 => Ok(WouldDie), +// 12 => Ok(AlreadyExists), +// 13 => Ok(NoDeposit), +// 14 => Ok(WouldBurn), +// 15 => Ok(LiveAsset), +// 16 => Ok(AssetNotLive), +// 17 => Ok(IncorrectStatus), +// 18 => Ok(NotFrozen), +// _ => todo!(), +// } +// } +// } diff --git a/pop-api/src/v0/state.rs b/pop-api/src/v0/state.rs index e3d38129..914a9603 100644 --- a/pop-api/src/v0/state.rs +++ b/pop-api/src/v0/state.rs @@ -1,6 +1,7 @@ -use crate::{primitives::storage_keys::RuntimeStateKeys, read_state, Error}; +use crate::{error::StatusCode, primitives::storage_keys::RuntimeStateKeys, read_state}; use scale::Decode; +#[inline] pub fn read(key: RuntimeStateKeys) -> crate::Result { - read_state(key).and_then(|v| T::decode(&mut &v[..]).map_err(|_e| Error::DecodingFailed.into())) + read_state(key).and_then(|v| T::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 3b91f713..dc0a5245 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -9,7 +9,7 @@ use {scale_decode::DecodeAsType, scale_encode::EncodeAsType, scale_info::TypeInf pub mod cross_chain; pub mod storage_keys; -#[derive(Encode, Decode, Debug, MaxEncodedLen, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Encode, Decode, Clone, Debug, MaxEncodedLen, Eq, PartialEq, Ord, PartialOrd)] #[cfg_attr(feature = "std", derive(TypeInfo, DecodeAsType, EncodeAsType))] pub struct AccountId(pub [u8; 32]); diff --git a/primitives/src/storage_keys.rs b/primitives/src/storage_keys.rs index 748d0662..7992089b 100644 --- a/primitives/src/storage_keys.rs +++ b/primitives/src/storage_keys.rs @@ -47,10 +47,13 @@ pub enum NftsKeys { #[derive(Encode, Decode, Debug, MaxEncodedLen)] pub enum AssetsKeys { Allowance(AssetId, AccountId, AccountId), - /// Check if the asset exists. - AssetExists(AssetId), + // /// Check if the asset exists. + // // AssetExists(AssetId), /// Check balance. BalanceOf(AssetId, AccountId), /// Returns the total token supply for a given asset ID. TotalSupply(AssetId), + TokenDecimals(AssetId), + TokenSymbol(AssetId), + TokenName(AssetId), } From 7757319213cab9de78b0a3f26eda15cf513af454 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Wed, 3 Jul 2024 08:18:27 +0200 Subject: [PATCH 15/76] refactor: runtime --- pop-api/Cargo.toml | 6 +- pop-api/examples/fungibles/Cargo.toml | 4 - pop-api/examples/fungibles/lib.rs | 4 +- pop-api/src/error.rs | 38 ++-- pop-api/src/lib.rs | 4 +- pop-api/src/v0/assets/fungibles.rs | 10 +- pop-api/src/v0/assets/mod.rs | 22 +- pop-api/src/v0/mod.rs | 3 +- pop-api/src/v0/state.rs | 2 +- runtime/devnet/src/extensions.rs | 295 +++++++++++--------------- 10 files changed, 168 insertions(+), 220 deletions(-) diff --git a/pop-api/Cargo.toml b/pop-api/Cargo.toml index a88d8892..82ee7613 100644 --- a/pop-api/Cargo.toml +++ b/pop-api/Cargo.toml @@ -8,8 +8,6 @@ edition = "2021" [dependencies] enumflags2 = { version = "0.7.7" } ink = { version = "5.0.0", default-features = false } -scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } -scale-info = { version = "2.10.0", default-features = false, features = ["derive"] } sp-io = { version = "31.0.0", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] } sp-runtime = { version = "32.0.0", default-features = false } @@ -26,9 +24,7 @@ std = [ "enumflags2/std", "ink/std", "pop-primitives/std", - "scale/std", - "scale-info/std", -"sp-io/std", + "sp-io/std", "sp-runtime/std", ] assets = ["pop-primitives/assets"] diff --git a/pop-api/examples/fungibles/Cargo.toml b/pop-api/examples/fungibles/Cargo.toml index 514f000f..565b0554 100755 --- a/pop-api/examples/fungibles/Cargo.toml +++ b/pop-api/examples/fungibles/Cargo.toml @@ -7,8 +7,6 @@ edition = "2021" [dependencies] ink = { version = "5.0.0", default-features = false } pop-api = { path = "../../../pop-api", default-features = false, features = ["assets"] } -scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } -scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true } [lib] path = "lib.rs" @@ -18,8 +16,6 @@ default = ["std"] std = [ "ink/std", "pop-api/std", - "scale/std", - "scale-info/std", ] ink-as-dependency = [] e2e-tests = [] diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs index bd51d760..1794fbe5 100755 --- a/pop-api/examples/fungibles/lib.rs +++ b/pop-api/examples/fungibles/lib.rs @@ -85,7 +85,7 @@ mod fungibles { to: AccountId32, value: Balance, // In the standard a `[u8]`, but the size needs to be known at compile time. - data: Vec, + _data: Vec, ) -> Result<()> { ink::env::debug_println!( "PopApiFungiblesExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}", @@ -95,7 +95,7 @@ mod fungibles { value, ); - let result = api::transfer_from(id, from, to, value, &data); + let result = api::transfer_from(id, from, to, value); ink::env::debug_println!("Result: {:?}", result); // result.map_err(|e| e.into()) result diff --git a/pop-api/src/error.rs b/pop-api/src/error.rs index 54681e6d..8989464f 100644 --- a/pop-api/src/error.rs +++ b/pop-api/src/error.rs @@ -1,10 +1,7 @@ -use ink::env::chain_extension::FromStatusCode; -use scale::{Decode, Encode}; +use ink::{env::chain_extension::FromStatusCode, scale::Decode}; -use Error::*; - -#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +#[derive(Debug, PartialEq, Eq)] +#[ink::scale_derive(Encode, Decode, TypeInfo)] pub struct StatusCode(pub u32); impl From for StatusCode { @@ -14,21 +11,21 @@ impl From for StatusCode { } impl FromStatusCode for StatusCode { fn from_status_code(status_code: u32) -> Result<(), Self> { - if status_code == 0 { - return Ok(()); + match status_code { + 0 => Ok(()), + _ => Err(StatusCode(status_code)), } - Err(StatusCode(status_code)) } } -impl From for StatusCode { - fn from(_: scale::Error) -> Self { +impl From for StatusCode { + fn from(_: ink::scale::Error) -> Self { StatusCode(255u32) } } -#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +#[derive(Debug, PartialEq, Eq)] +#[ink::scale_derive(Encode, Decode, TypeInfo)] #[repr(u8)] pub enum Error { /// Some unknown error occurred. Go to the Pop API docs section `Pop API error`. @@ -70,6 +67,7 @@ pub enum Error { Unavailable = 12, /// Root origin is not allowed. RootNotAllowed = 13, + UnknownFunctionId = 254, DecodingFailed = 255, } @@ -92,15 +90,15 @@ impl From for Error { Err(_) => { encoded[..].rotate_right(1); encoded[0] = 0u8; - Error::decode(&mut &encoded[..]).unwrap_or(DecodingFailed) + Error::decode(&mut &encoded[..]).unwrap_or(Error::DecodingFailed) }, Ok(error) => error, } } } -#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +#[derive(Debug, PartialEq, Eq)] +#[ink::scale_derive(Encode, Decode, TypeInfo)] pub enum TokenError { /// Funds are unavailable. FundsUnavailable, @@ -125,8 +123,8 @@ pub enum TokenError { Blocked, } -#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +#[derive(Debug, PartialEq, Eq)] +#[ink::scale_derive(Encode, Decode, TypeInfo)] pub enum ArithmeticError { /// Underflow. Underflow, @@ -136,8 +134,8 @@ pub enum ArithmeticError { DivisionByZero, } -#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +#[derive(Debug, PartialEq, Eq)] +#[ink::scale_derive(Encode, Decode, TypeInfo)] pub enum TransactionalError { /// Too many transactional layers have been spawned. LimitReached, diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 53bbadc6..55307f3e 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -27,8 +27,8 @@ type BlockNumber = ::BlockNumber; pub type Result = core::result::Result; -#[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +#[derive(Debug, PartialEq, Eq, Clone)] +#[ink::scale_derive(Encode, Decode, TypeInfo)] pub enum Environment {} impl ink::env::Environment for Environment { diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index e7c3f41c..376ca898 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -1,5 +1,4 @@ use ink::prelude::vec::Vec; -use scale::Encode; use crate::{ assets, @@ -102,7 +101,6 @@ pub fn transfer_from( from: impl Into>, to: impl Into>, value: Balance, - _data: &[u8], ) -> Result<()> { assets::transfer_approved(id, from, to, value) } @@ -293,8 +291,8 @@ pub fn token_decimals(id: AssetId) -> Result { // assets::asset_exists(id) // } -#[derive(Encode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +#[derive(Debug, PartialEq, Eq)] +#[ink::scale_derive(Encode, Decode, TypeInfo)] pub enum FungiblesError { Other(StatusCode), /// The asset is not live; either frozen or being destroyed. @@ -344,13 +342,13 @@ mod tests { use scale::Decode; use super::FungiblesError; - use crate::StatusCode; - use pop_primitives::{ + use crate::error::{ ArithmeticError::*, Error::{self, *}, TokenError::*, TransactionalError::*, }; + use crate::StatusCode; fn into_fungibles_error(error: Error) -> FungiblesError { let status_code: StatusCode = error.into(); diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index 22e32d47..241c5020 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1,5 +1,4 @@ -use ink::prelude::vec::Vec; -use scale::{Compact, Encode}; +use ink::{prelude::vec::Vec, scale::Compact}; use crate::{state::read, Balance, RuntimeCall, *}; use primitives::{AssetId, MultiAddress}; @@ -97,7 +96,7 @@ type Result = core::result::Result; // })) // } -// /// Move some assets from the sender account to another. +/// Move some assets from the sender account to another. #[inline] pub(crate) fn transfer( id: AssetId, @@ -199,7 +198,7 @@ pub(crate) fn transfer( // dispatch(RuntimeCall::Assets(AssetsCall::ClearMetadata { id: id.into() })) // } -// /// Approve an amount of asset for transfer by a delegated third-party account. +/// Approve an amount of asset for transfer by a delegated third-party account. #[inline] pub(crate) fn approve_transfer( id: AssetId, @@ -300,17 +299,17 @@ pub(crate) fn transfer_approved( // #[inline] pub(crate) fn total_supply(id: AssetId) -> Result { - state::read(RuntimeStateKeys::Assets(AssetsKeys::TotalSupply(id))) + read(RuntimeStateKeys::Assets(AssetsKeys::TotalSupply(id))) } #[inline] pub(crate) fn balance_of(id: AssetId, owner: AccountId) -> Result { - state::read(RuntimeStateKeys::Assets(AssetsKeys::BalanceOf(id, owner))) + read(RuntimeStateKeys::Assets(AssetsKeys::BalanceOf(id, owner))) } #[inline] pub(crate) fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { - state::read(RuntimeStateKeys::Assets(AssetsKeys::Allowance(id, owner, spender))) + read(RuntimeStateKeys::Assets(AssetsKeys::Allowance(id, owner, spender))) } // pub(crate) fn asset_exists(id: AssetId) -> Result { // state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id))) @@ -318,17 +317,17 @@ pub(crate) fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Re #[inline] pub(crate) fn token_name(id: AssetId) -> Result> { - state::read(RuntimeStateKeys::Assets(AssetsKeys::TokenName(id))) + read(RuntimeStateKeys::Assets(AssetsKeys::TokenName(id))) } // #[inline] pub(crate) fn token_symbol(id: AssetId) -> Result> { - state::read(RuntimeStateKeys::Assets(AssetsKeys::TokenSymbol(id))) + read(RuntimeStateKeys::Assets(AssetsKeys::TokenSymbol(id))) } #[inline] pub(crate) fn token_decimals(id: AssetId) -> Result { - state::read(RuntimeStateKeys::Assets(AssetsKeys::TokenDecimals(id))) + read(RuntimeStateKeys::Assets(AssetsKeys::TokenDecimals(id))) } // Parameters to extrinsics representing an asset id (`AssetIdParameter`) and a balance amount (`Balance`) are expected @@ -341,7 +340,8 @@ type AssetIdParameter = Compact; // Balance amount that is compact encoded. type BalanceParameter = Compact; // -#[derive(Encode)] +#[derive(Debug, PartialEq, Eq)] +#[ink::scale_derive(Encode, Decode, TypeInfo)] pub(crate) enum AssetsCall { // #[codec(index = 0)] // Create { id: AssetIdParameter, admin: MultiAddress, min_balance: Balance }, diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index 20dc6476..f91c4271 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -8,7 +8,8 @@ pub mod cross_chain; pub mod nfts; pub mod state; -#[derive(scale::Encode)] +#[derive(Debug, PartialEq, Eq)] +#[ink::scale_derive(Encode, Decode, TypeInfo)] pub(crate) enum RuntimeCall { #[codec(index = 10)] #[cfg(feature = "balances")] diff --git a/pop-api/src/v0/state.rs b/pop-api/src/v0/state.rs index 914a9603..afe48ba8 100644 --- a/pop-api/src/v0/state.rs +++ b/pop-api/src/v0/state.rs @@ -1,5 +1,5 @@ use crate::{error::StatusCode, primitives::storage_keys::RuntimeStateKeys, read_state}; -use scale::Decode; +use ink::scale::Decode; #[inline] pub fn read(key: RuntimeStateKeys) -> crate::Result { diff --git a/runtime/devnet/src/extensions.rs b/runtime/devnet/src/extensions.rs index 904d18ce..6f98bf96 100644 --- a/runtime/devnet/src/extensions.rs +++ b/runtime/devnet/src/extensions.rs @@ -57,24 +57,132 @@ where E: Ext, { log::debug!(target:LOG_TARGET, " extension called "); - match v0::FuncId::try_from(env.func_id())? { - v0::FuncId::Dispatch => match dispatch::(env) { - Ok(()) => Ok(RetVal::Converging(0)), - Err(e) => Ok(RetVal::Converging(convert_to_status_code(e))), - }, - v0::FuncId::ReadState => { - read_state::(env)?; - Ok(RetVal::Converging(0)) - }, - v0::FuncId::SendXcm => { - send_xcm::(env)?; - Ok(RetVal::Converging(0)) + let mut env = env.buf_in_buf_out(); + let contract_host_weight = ContractSchedule::::get().host_fn_weights; + // debug_message weight is a good approximation of the additional overhead of going + // from contract layer to substrate layer. + // reference: https://github.com/paritytech/ink-examples/blob/b8d2caa52cf4691e0ddd7c919e4462311deb5ad0/psp22-extension/runtime/psp22-extension-example.rs#L236 + env.charge_weight(contract_host_weight.debug_message)?; + + let result = match v0::FuncId::try_from(env.func_id()) { + Ok(function) => { + // calculate weight for reading bytes of `len` + // reference: https://github.com/paritytech/polkadot-sdk/blob/117a9433dac88d5ac00c058c9b39c511d47749d2/substrate/frame/contracts/src/wasm/runtime.rs#L267 + let len = env.in_len(); + env.charge_weight(contract_host_weight.return_per_byte.saturating_mul(len.into()))?; + match function { + v0::FuncId::Dispatch => dispatch::(&mut env, len), + v0::FuncId::ReadState => read_state::(&mut env), + v0::FuncId::SendXcm => send_xcm::(&mut env), + } }, + Err(e) => Err(e), + }; + + // Convert any error to a status code and return Ok with RetVal::Converging + match result { + Ok(_) => Ok(RetVal::Converging(0)), + Err(e) => Ok(RetVal::Converging(convert_to_status_code(e))), } } } + +fn dispatch(env: &mut Environment, len: u32) -> Result<(), DispatchError> +where + T: pallet_contracts::Config + + frame_system::Config, + RuntimeOrigin: From>, + E: Ext, +{ + const LOG_PREFIX: &str = " dispatch |"; + + // read the input as RuntimeCall + let call: RuntimeCall = env.read_as_unbounded(len)?; + log::debug!(target: LOG_TARGET, "Read input as call successfully"); + // contract is the origin by default + let origin: RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); + dispatch_call::(env, call, origin, LOG_PREFIX) +} + +fn read_state(env: &mut Environment) -> Result<(), DispatchError> +where + T: pallet_contracts::Config + + pallet_assets::Config + + pallet_nfts::Config + + cumulus_pallet_parachain_system::Config + + frame_system::Config, + E: Ext, +{ + const LOG_PREFIX: &str = " read_state |"; + + let key: RuntimeStateKeys = env.read_as()?; + let result = match key { + RuntimeStateKeys::Nfts(key) => read_nfts_state::(key, env), + RuntimeStateKeys::ParachainSystem(key) => read_parachain_system_state::(key, env), + RuntimeStateKeys::Assets(key) => read_assets_state::(key, env), + }? + .encode(); + + log::trace!( + target:LOG_TARGET, + "{} result: {:?}.", LOG_PREFIX, result + ); + env.write(&result, false, None) +} + +fn send_xcm(env: &mut Environment) -> Result<(), DispatchError> +where + T: pallet_contracts::Config + + frame_system::Config< + RuntimeOrigin = RuntimeOrigin, + AccountId = AccountId, + RuntimeCall = RuntimeCall, + >, + E: Ext, +{ + const LOG_PREFIX: &str = " send_xcm |"; + + // read the input as CrossChainMessage + let xc_call: CrossChainMessage = env.read_as::()?; + // Determine the call to dispatch + let (dest, message) = match xc_call { + CrossChainMessage::Relay(message) => { + let dest = Location::parent().into_versioned(); + let assets: Asset = (Here, 10 * UNIT).into(); + let beneficiary: Location = + AccountId32 { id: (env.ext().address().clone()).into(), network: None }.into(); + let message = Xcm::builder() + .withdraw_asset(assets.clone().into()) + .buy_execution(assets.clone(), Unlimited) + .transact( + SovereignAccount, + Weight::from_parts(250_000_000, 10_000), + message.encode().into(), + ) + .refund_surplus() + .deposit_asset(assets.into(), beneficiary) + .build(); + (dest, message) + }, + }; + + // TODO: revisit to replace with signed contract origin + let origin: RuntimeOrigin = RawOrigin::Root.into(); + + // Generate runtime call to dispatch + let call = RuntimeCall::PolkadotXcm(pallet_xcm::Call::send { + dest: Box::new(dest), + message: Box::new(VersionedXcm::V4(message)), + }); + + dispatch_call::(env, call, origin, LOG_PREFIX) +} + pub(crate) fn convert_to_status_code(error: DispatchError) -> u32 { - let mut encoded_error = error.encode(); + let mut encoded_error = match error { + DispatchError::Other("UnknownFunctionId") => vec![254, 0, 0, 0], + _ => error.encode(), + }; // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). encoded_error.resize(4, 0); u32::decode(&mut &encoded_error[..]).expect("qid, resized to 4 bytes line above") @@ -99,8 +207,7 @@ impl TryFrom for v0::FuncId { 0x2 => Self::SendXcm, _ => { log::error!("called an unregistered `func_id`: {:}", func_id); - // TODO: Other error. - return Err(DispatchError::Other("unimplemented func_id")); + return Err(DispatchError::Other("UnknownFuncId")); }, }; @@ -143,101 +250,6 @@ where } } -fn charge_overhead_weight( - env: &mut Environment, - len: u32, - log_prefix: &str, -) -> Result -where - T: pallet_contracts::Config, - E: Ext, -{ - let contract_host_weight = ContractSchedule::::get().host_fn_weights; - - // calculate weight for reading bytes of `len` - // reference: https://github.com/paritytech/polkadot-sdk/blob/117a9433dac88d5ac00c058c9b39c511d47749d2/substrate/frame/contracts/src/wasm/runtime.rs#L267 - let base_weight: Weight = contract_host_weight.return_per_byte.saturating_mul(len.into()); - - // debug_message weight is a good approximation of the additional overhead of going - // from contract layer to substrate layer. - // reference: https://github.com/paritytech/ink-examples/blob/b8d2caa52cf4691e0ddd7c919e4462311deb5ad0/psp22-extension/runtime/psp22-extension-example.rs#L236 - let overhead = contract_host_weight.debug_message; - - let charged_weight = env.charge_weight(base_weight.saturating_add(overhead))?; - log::debug!(target: LOG_TARGET, "{} charged weight: {:?}", log_prefix, charged_weight); - - Ok(charged_weight) -} - -fn dispatch(env: Environment) -> Result<(), DispatchError> -where - T: pallet_contracts::Config - + frame_system::Config, - RuntimeOrigin: From>, - E: Ext, -{ - const LOG_PREFIX: &str = " dispatch |"; - - let mut env = env.buf_in_buf_out(); - let len = env.in_len(); - - charge_overhead_weight::(&mut env, len, LOG_PREFIX)?; - - // read the input as RuntimeCall - let call: RuntimeCall = env.read_as_unbounded(len)?; - - log::debug!(target: LOG_TARGET, "Read input as call successfully"); - - // contract is the origin by default - let origin: RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); - - dispatch_call::(&mut env, call, origin, LOG_PREFIX) -} - -fn read_state(env: Environment) -> Result<(), DispatchError> -where - T: pallet_contracts::Config - + pallet_assets::Config - + pallet_nfts::Config - + cumulus_pallet_parachain_system::Config - + frame_system::Config, - E: Ext, -{ - const LOG_PREFIX: &str = " read_state |"; - - let mut env = env.buf_in_buf_out(); - - // To be conservative, we charge the weight for reading the input bytes of a fixed-size type. - let base_weight: Weight = ContractSchedule::::get() - .host_fn_weights - .return_per_byte - .saturating_mul(env.in_len().into()); - let charged_weight = env.charge_weight(base_weight)?; - - log::debug!(target:LOG_TARGET, "{} charged weight: {:?}", LOG_PREFIX, charged_weight); - - let key: RuntimeStateKeys = env.read_as()?; - - let result = match key { - RuntimeStateKeys::Nfts(key) => read_nfts_state::(key, &mut env), - RuntimeStateKeys::ParachainSystem(key) => { - read_parachain_system_state::(key, &mut env) - }, - RuntimeStateKeys::Assets(key) => read_assets_state::(key, &mut env), - }? - .encode(); - - log::trace!( - target:LOG_TARGET, - "{} result: {:?}.", LOG_PREFIX, result - ); - env.write(&result, false, None).map_err(|e| { - log::trace!(target: LOG_TARGET, "{:?}", e); - // TODO: Other error. - DispatchError::Other("unable to write results to contract memory") - }) -} - fn read_parachain_system_state( key: ParachainSystemKeys, env: &mut Environment, @@ -320,10 +332,10 @@ where ) .encode()) }, - AssetsKeys::AssetExists(id) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_assets::Pallet::::asset_exists(id).encode()) - }, + // AssetsKeys::AssetExists(id) => { + // env.charge_weight(T::DbWeight::get().reads(1_u64))?; + // Ok(pallet_assets::Pallet::::asset_exists(id).encode()) + // }, AssetsKeys::BalanceOf(id, owner) => { env.charge_weight(T::DbWeight::get().reads(1_u64))?; Ok(pallet_assets::Pallet::::balance(id, &owner.0.into()) @@ -333,63 +345,10 @@ where env.charge_weight(T::DbWeight::get().reads(1_u64))?; Ok(pallet_assets::Pallet::::total_supply(id).encode()) }, + _ => todo!(), } } -fn send_xcm(env: Environment) -> Result<(), DispatchError> -where - T: pallet_contracts::Config - + frame_system::Config< - RuntimeOrigin = RuntimeOrigin, - AccountId = AccountId, - RuntimeCall = RuntimeCall, - >, - E: Ext, -{ - const LOG_PREFIX: &str = " send_xcm |"; - - let mut env = env.buf_in_buf_out(); - let len = env.in_len(); - - let _ = charge_overhead_weight::(&mut env, len, LOG_PREFIX)?; - - // read the input as CrossChainMessage - let xc_call: CrossChainMessage = env.read_as::()?; - - // Determine the call to dispatch - let (dest, message) = match xc_call { - CrossChainMessage::Relay(message) => { - let dest = Location::parent().into_versioned(); - let assets: Asset = (Here, 10 * UNIT).into(); - let beneficiary: Location = - AccountId32 { id: (env.ext().address().clone()).into(), network: None }.into(); - let message = Xcm::builder() - .withdraw_asset(assets.clone().into()) - .buy_execution(assets.clone(), Unlimited) - .transact( - SovereignAccount, - Weight::from_parts(250_000_000, 10_000), - message.encode().into(), - ) - .refund_surplus() - .deposit_asset(assets.into(), beneficiary) - .build(); - (dest, message) - }, - }; - - // TODO: revisit to replace with signed contract origin - let origin: RuntimeOrigin = RawOrigin::Root.into(); - - // Generate runtime call to dispatch - let call = RuntimeCall::PolkadotXcm(pallet_xcm::Call::send { - dest: Box::new(dest), - message: Box::new(VersionedXcm::V4(message)), - }); - - dispatch_call::(&mut env, call, origin, LOG_PREFIX) -} - #[cfg(test)] mod tests { use super::*; From 924df85ebd353cd56ff816f722fc8cdb4a6c5956 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Tue, 2 Jul 2024 15:51:16 +0200 Subject: [PATCH 16/76] refactor: draft final design --- Cargo.lock | 3 +- pop-api/Cargo.toml | 4 +- pop-api/examples/fungibles/lib.rs | 119 ++--- pop-api/integration-tests/Cargo.toml | 4 +- .../integration-tests/src/local_fungibles.rs | 11 +- pop-api/src/error.rs | 335 ------------ pop-api/src/lib.rs | 85 ++- pop-api/src/primitives.rs | 1 - pop-api/src/v0/assets/fungibles.rs | 45 +- pop-api/src/v0/assets/mod.rs | 446 +++------------- pop-api/src/v0/balances.rs | 30 +- pop-api/src/v0/cross_chain/mod.rs | 29 +- pop-api/src/v0/mod.rs | 15 - pop-api/src/v0/nfts.rs | 37 +- pop-api/src/v0/state.rs | 7 - primitives/Cargo.toml | 7 - primitives/src/lib.rs | 124 ++++- primitives/src/storage_keys.rs | 13 +- runtime/devnet/Cargo.toml | 3 +- .../src/{extensions.rs => extensions/mod.rs} | 496 +++++++++++------- runtime/devnet/src/extensions/v0/assets.rs | 39 ++ runtime/devnet/src/extensions/v0/error.rs | 298 +++++++++++ runtime/devnet/src/extensions/v0/mod.rs | 2 + runtime/testnet/Cargo.toml | 2 +- runtime/testnet/src/extensions.rs | 18 +- 25 files changed, 1013 insertions(+), 1160 deletions(-) delete mode 100644 pop-api/src/error.rs delete mode 100644 pop-api/src/v0/state.rs rename runtime/devnet/src/{extensions.rs => extensions/mod.rs} (79%) create mode 100644 runtime/devnet/src/extensions/v0/assets.rs create mode 100644 runtime/devnet/src/extensions/v0/error.rs create mode 100644 runtime/devnet/src/extensions/v0/mod.rs diff --git a/Cargo.lock b/Cargo.lock index d798562b..cf7abf73 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9675,8 +9675,6 @@ version = "0.0.0" dependencies = [ "bounded-collections 0.1.9", "parity-scale-codec", - "scale-decode", - "scale-encode", "scale-info", ] @@ -9744,6 +9742,7 @@ dependencies = [ "polkadot-runtime-common", "pop-primitives", "pop-runtime-common", + "rand", "scale-info", "smallvec", "sp-api", diff --git a/pop-api/Cargo.toml b/pop-api/Cargo.toml index 82ee7613..dc48ea8a 100644 --- a/pop-api/Cargo.toml +++ b/pop-api/Cargo.toml @@ -9,9 +9,8 @@ edition = "2021" enumflags2 = { version = "0.7.7" } ink = { version = "5.0.0", default-features = false } sp-io = { version = "31.0.0", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] } -sp-runtime = { version = "32.0.0", default-features = false } -pop-primitives = { path = "../primitives", features = ["devnet"], default-features = false } +pop-primitives = { path = "../primitives", default-features = false } [lib] name = "pop_api" @@ -25,7 +24,6 @@ std = [ "ink/std", "pop-primitives/std", "sp-io/std", - "sp-runtime/std", ] assets = ["pop-primitives/assets"] balances = [] diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs index 1794fbe5..03841b74 100755 --- a/pop-api/examples/fungibles/lib.rs +++ b/pop-api/examples/fungibles/lib.rs @@ -8,13 +8,13 @@ use ink::prelude::vec::Vec; use pop_api::{ assets::fungibles::{self as api}, - error::StatusCode, - primitives::{AccountId as AccountId32, AssetId}, + primitives::AssetId, + StatusCode, }; pub type Result = core::result::Result; -#[ink::contract(env = pop_api::Environment)] +#[ink::contract] mod fungibles { use super::*; @@ -41,141 +41,84 @@ mod fungibles { #[ink(message)] pub fn total_supply(&self, id: AssetId) -> Result { - // api::total_supply(id).map_err(|e| e.into()) api::total_supply(id) } #[ink(message)] - pub fn balance_of(&self, id: AssetId, owner: AccountId32) -> Result { - // api::balance_of(id, owner).map_err(|e| e.into()) + pub fn balance_of(&self, id: AssetId, owner: AccountId) -> Balance { api::balance_of(id, owner) } #[ink(message)] - pub fn allowance( - &self, - id: AssetId, - owner: AccountId32, - spender: AccountId32, - ) -> Result { - // api::allowance(id, owner, spender).map_err(|e| e.into()) + pub fn allowance(&self, id: AssetId, owner: AccountId, spender: AccountId) -> Balance { api::allowance(id, owner, spender) } #[ink(message)] - pub fn transfer(&self, id: AssetId, to: AccountId32, value: Balance) -> Result<()> { - ink::env::debug_println!( - "PopApiFungiblesExample::transfer: id: {:?}, to: {:?} value: {:?}", - id, - to, - value, - ); - - let result = api::transfer(id, to, value); - ink::env::debug_println!("Result: {:?}", result); - // result.map_err(|e| e.into()) - result + pub fn transfer(&self, id: AssetId, to: AccountId, value: Balance) -> Result<()> { + api::transfer(id, to, value)?; + Ok(()) } #[ink(message)] pub fn transfer_from( &self, id: AssetId, - from: AccountId32, - to: AccountId32, + from: AccountId, + to: AccountId, value: Balance, // In the standard a `[u8]`, but the size needs to be known at compile time. _data: Vec, ) -> Result<()> { - ink::env::debug_println!( - "PopApiFungiblesExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}", - id, - from, - to, - value, - ); - - let result = api::transfer_from(id, from, to, value); - ink::env::debug_println!("Result: {:?}", result); - // result.map_err(|e| e.into()) - result + api::transfer_from(id, from, to, value)?; + Ok(()) } #[ink(message)] - pub fn approve(&self, id: AssetId, spender: AccountId32, value: Balance) -> Result<()> { - ink::env::debug_println!( - "PopApiFungiblesExample::approve: id: {:?}, spender {:?}, value: {:?}", - id, - spender, - value, - ); - - let result = api::approve(id, spender, value); - ink::env::debug_println!("Result: {:?}", result); - // result.map_err(|e| e.into()) - result + pub fn approve(&self, id: AssetId, spender: AccountId, value: Balance) -> Result<()> { + api::approve(id, spender, value)?; + Ok(()) } #[ink(message)] pub fn increase_allowance( &self, id: AssetId, - spender: AccountId32, + spender: AccountId, value: Balance, ) -> Result<()> { - ink::env::debug_println!( - "PopApiFungiblesExample::increase_allowance: id: {:?}, spender {:?}, value: {:?}", - id, - spender, - value, - ); - - let result = api::increase_allowance(id, spender, value); - ink::env::debug_println!("Result: {:?}", result); - // result.map_err(|e| e.into()) - result + api::increase_allowance(id, spender, value)?; + Ok(()) } #[ink(message)] pub fn decrease_allowance( &self, id: AssetId, - spender: AccountId32, + spender: AccountId, value: Balance, ) -> Result<()> { - ink::env::debug_println!( - "PopApiFungiblesExample::decrease_allowance: id: {:?}, spender {:?}, value: {:?}", - id, - spender, - value, - ); - - let result = api::decrease_allowance(id, spender, value); - ink::env::debug_println!("Result: {:?}", result); - // result.map_err(|e| e.into()) - result + api::decrease_allowance(id, spender, value)?; + Ok(()) } - /// 2. PSP-22 Metadata Interface: - /// - token_name - /// - token_symbol - /// - token_decimals + // 2. PSP-22 Metadata Interface: + // - token_name + // - token_symbol + // - token_decimals #[ink(message)] - pub fn token_name(&self, id: AssetId) -> Result> { - // api::token_name(id).map_err(|e| e.into()) + pub fn token_name(&self, id: AssetId) -> Vec { api::token_name(id) } #[ink(message)] - pub fn token_symbol(&self, id: AssetId) -> Result> { - // api::token_symbol(id).map_err(|e| e.into()) + pub fn token_symbol(&self, id: AssetId) -> Vec { api::token_symbol(id) } #[ink(message)] - pub fn token_decimals(&self, id: AssetId) -> Result { - // api::token_decimals(id).map_err(|e| e.into()) + pub fn token_decimals(&self, id: AssetId) -> u8 { api::token_decimals(id) } @@ -189,7 +132,7 @@ mod fungibles { // - clear_metadata // #[ink(message)] - // pub fn create(&self, id: AssetId, admin: AccountId32, min_balance: Balance) -> Result<()> { + // pub fn create(&self, id: AssetId, admin: AccountId, min_balance: Balance) -> Result<()> { // ink::env::debug_println!( // "PopApiFungiblesExample::create: id: {:?} admin: {:?} min_balance: {:?}", // id, @@ -198,8 +141,8 @@ mod fungibles { // ); // let result = api::create(id, admin, min_balance); // ink::env::debug_println!("Result: {:?}", result); - // // result.map_err(|e| e.into()) - // result + // result.map_err(|e| e.into()) + // result // } // #[ink(message)] diff --git a/pop-api/integration-tests/Cargo.toml b/pop-api/integration-tests/Cargo.toml index 84769433..c855a884 100644 --- a/pop-api/integration-tests/Cargo.toml +++ b/pop-api/integration-tests/Cargo.toml @@ -10,7 +10,7 @@ frame-support = { version = "29.0.0", default-features = false } frame-system = { version = "29.0.0", default-features = false } pallet-balances = { version = "29.0.2", default-features = false } pallet-contracts = { version = "28.0.0", default-features = false } -pop-api = { path = "../.", default-features = false, features = ["assets"] } +pop-primitives = { path = "../../primitives", default-features = false, features = ["assets"] } pop-runtime-devnet = { path = "../../runtime/devnet", default-features = false } sp-io = { version = "31.0.0", default-features = false } sp-runtime = { version = "32.0.0", default-features = false } @@ -23,7 +23,7 @@ std = [ "frame-system/std", "pallet-balances/std", "pallet-contracts/std", - "pop-api/std", + "pop-primitives/std", "pop-runtime-devnet/std", "scale/std", "sp-io/std", diff --git a/pop-api/integration-tests/src/local_fungibles.rs b/pop-api/integration-tests/src/local_fungibles.rs index 1b3c6633..968c43eb 100644 --- a/pop-api/integration-tests/src/local_fungibles.rs +++ b/pop-api/integration-tests/src/local_fungibles.rs @@ -1,5 +1,5 @@ use super::*; -use pop_api::error::{ +use pop_primitives::error::{ ArithmeticError::*, Error::{self, *}, TokenError::*, @@ -8,7 +8,10 @@ use pop_api::error::{ const ASSET_ID: AssetId = 1; fn decoded(result: ExecReturnValue) -> T { - ::decode(&mut &result.data[2..]).unwrap() + match ::decode(&mut &result.data[2..]) { + Ok(value) => value, + Err(_) => panic!("\nTest failed by trying to decode result: {:?} into `T`\n", result), + } } fn allowance( @@ -78,7 +81,9 @@ fn transfer( ) -> ExecReturnValue { let function = function_selector("transfer"); let params = [function, asset_id.encode(), to.encode(), value.encode()].concat(); - do_bare_call(addr, params, 0).expect("should work") + let result = do_bare_call(addr, params, 0).expect("should work"); + println!("Transfer result: {:?}", result); + result } fn transfer_from( diff --git a/pop-api/src/error.rs b/pop-api/src/error.rs deleted file mode 100644 index 8989464f..00000000 --- a/pop-api/src/error.rs +++ /dev/null @@ -1,335 +0,0 @@ -use ink::{env::chain_extension::FromStatusCode, scale::Decode}; - -#[derive(Debug, PartialEq, Eq)] -#[ink::scale_derive(Encode, Decode, TypeInfo)] -pub struct StatusCode(pub u32); - -impl From for StatusCode { - fn from(value: u32) -> Self { - StatusCode(value) - } -} -impl FromStatusCode for StatusCode { - fn from_status_code(status_code: u32) -> Result<(), Self> { - match status_code { - 0 => Ok(()), - _ => Err(StatusCode(status_code)), - } - } -} - -impl From for StatusCode { - fn from(_: ink::scale::Error) -> Self { - StatusCode(255u32) - } -} - -#[derive(Debug, PartialEq, Eq)] -#[ink::scale_derive(Encode, Decode, TypeInfo)] -#[repr(u8)] -pub enum Error { - /// Some unknown error occurred. Go to the Pop API docs section `Pop API error`. - Other { - // Index within the `DispatchError` - dispatch_error_index: u8, - // Index within the `DispatchError` variant. - error_index: u8, - // Index for further nesting, e.g. pallet error. - error: u8, - } = 0, - /// Failed to lookup some data. - CannotLookup = 1, - /// A bad origin. - BadOrigin = 2, - /// A custom error in a module. - Module { - index: u8, - error: u8, - } = 3, - /// At least one consumer is remaining so the account cannot be destroyed. - ConsumerRemaining = 4, - /// There are no providers so the account cannot be created. - NoProviders = 5, - /// There are too many consumers so the account cannot be created. - TooManyConsumers = 6, - /// An error to do with tokens. - Token(TokenError) = 7, - /// An arithmetic error. - Arithmetic(ArithmeticError) = 8, - /// The number of transactional layers has been reached, or we are not in a transactional - /// layer. - Transactional(TransactionalError) = 9, - /// Resources exhausted, e.g. attempt to read/write data which is too large to manipulate. - Exhausted = 10, - /// The state is corrupt; this is generally not going to fix itself. - Corruption = 11, - /// Some resource (e.g. a preimage) is unavailable right now. This might fix itself later. - Unavailable = 12, - /// Root origin is not allowed. - RootNotAllowed = 13, - UnknownFunctionId = 254, - DecodingFailed = 255, -} - -#[cfg(test)] -impl From for StatusCode { - fn from(value: Error) -> Self { - let mut encoded_error = value.encode(); - // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). - encoded_error.resize(4, 0); - StatusCode::from( - u32::decode(&mut &encoded_error[..]).expect("qid, resized to 4 bytes line above"), - ) - } -} - -impl From for Error { - fn from(value: StatusCode) -> Self { - let mut encoded: [u8; 4] = value.0.to_le_bytes(); - match Error::decode(&mut &encoded[..]) { - Err(_) => { - encoded[..].rotate_right(1); - encoded[0] = 0u8; - Error::decode(&mut &encoded[..]).unwrap_or(Error::DecodingFailed) - }, - Ok(error) => error, - } - } -} - -#[derive(Debug, PartialEq, Eq)] -#[ink::scale_derive(Encode, Decode, TypeInfo)] -pub enum TokenError { - /// Funds are unavailable. - FundsUnavailable, - /// Some part of the balance gives the only provider reference to the account and thus cannot - /// be (re)moved. - OnlyProvider, - /// Account cannot exist with the funds that would be given. - BelowMinimum, - /// Account cannot be created. - CannotCreate, - /// The asset in question is unknown. - UnknownAsset, - /// Funds exist but are frozen. - Frozen, - /// Operation is not supported by the asset. - Unsupported, - /// Account cannot be created for a held balance. - CannotCreateHold, - /// Withdrawal would cause unwanted loss of account. - NotExpendable, - /// Account cannot receive the assets. - Blocked, -} - -#[derive(Debug, PartialEq, Eq)] -#[ink::scale_derive(Encode, Decode, TypeInfo)] -pub enum ArithmeticError { - /// Underflow. - Underflow, - /// Overflow. - Overflow, - /// Division by zero. - DivisionByZero, -} - -#[derive(Debug, PartialEq, Eq)] -#[ink::scale_derive(Encode, Decode, TypeInfo)] -pub enum TransactionalError { - /// Too many transactional layers have been spawned. - LimitReached, - /// A transactional layer was expected, but does not exist. - NoLayer, -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::error::{ArithmeticError::*, TokenError::*, TransactionalError::*}; - - #[test] - fn u32_always_encodes_to_4_bytes() { - assert_eq!(0u32.encode().len(), 4); - assert_eq!(u32::MAX.encode().len(), 4); - } - - // Decodes 4 bytes into a `u32` and converts it into `StatusCode`. - fn into_status_code(encoded_error: [u8; 4]) -> StatusCode { - let decoded_u32 = u32::decode(&mut &encoded_error[..]).unwrap(); - StatusCode::from_status_code(decoded_u32).unwrap_err() - } - - // Decodes 4 bytes into a `u32` and converts it into `Error`. - fn into_error(encoded_error: [u8; 4]) -> Error { - let decoded_u32 = u32::decode(&mut &encoded_error[..]).unwrap(); - let status_code = StatusCode::from_status_code(decoded_u32).unwrap_err(); - status_code.into() - } - - // Tests the `From` implementation for `Error`. - // - // Unit variants: - // If the encoded value indicates a nested `Error` which is known by the Pop API version as a - // unit variant, the encoded value is converted into `Error::Other`. - // - // Example: the error `BadOrigin` (encoded: `[2, 0, 0, 0]`) with a non-zero value for one - // of the bytes [1..4]: `[2, 0, 1, 0]` is converted into `[0, 2, 0, 1]`. This is decoded to - // `Error::Other { dispatch_error: 2, index: 0, error: 1 }`. - #[test] - fn unit_error_variants() { - let errors = vec![ - CannotLookup, - BadOrigin, - ConsumerRemaining, - NoProviders, - TooManyConsumers, - Exhausted, - Corruption, - Unavailable, - RootNotAllowed, - DecodingFailed, - ]; - // Four scenarios, 2 tests each: - // 1. Compare a `StatusCode`, which is converted from an encoded value, with a `StatusCode` - // converted from an `Error`. - // 2. Compare an `Error, which is converted from an encoded value, with the expected `Error`. - for (i, &error_code) in UNIT_ERRORS.iter().enumerate() { - // No nesting and unit variant correctly returned. - assert_eq!(into_status_code([error_code, 0, 0, 0]), errors[i].into()); - assert_eq!(into_error([error_code, 0, 0, 0]), errors[i]); - // Unexpected second byte nested. - assert_eq!( - into_status_code([error_code, 1, 0, 0]), - (Other { dispatch_error_index: error_code, error_index: 1, error: 0 }).into(), - ); - assert_eq!(into_error([error_code, 1, 0, 0]), errors[i]); - // Unexpected third byte nested. - assert_eq!( - into_status_code([error_code, 1, 1, 0]), - (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }).into(), - ); - assert_eq!(into_error([error_code, 1, 1, 0]), errors[i]); - // Unexpected fourth byte nested. - assert_eq!( - into_status_code([error_code, 1, 1, 1]), - (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }).into(), - ); - assert_eq!(into_error([error_code, 1, 1, 1]), errors[i]); - } - } - - // Single nested variants: - // If the encoded value indicates a double nested `Error` which is known by the Pop API version - // as a single nested variant, the encoded value is converted into `Error::Other`. - // - // Example: the error `Arithmetic(Overflow)` (encoded: `[8, 1, 0, 0]`) with a non-zero - // value for one of the bytes [2..4]: `[8, 1, 1, 0]` is converted into `[0, 8, 1, 1]`. This is - // decoded to `Error::Other { dispatch_error: 8, index: 1, error: 1 }`. - #[test] - fn single_nested_error_variants() { - let errors = vec![ - [Token(FundsUnavailable), Token(OnlyProvider)], - [Arithmetic(Underflow), Arithmetic(Overflow)], - [Transactional(LimitReached), Transactional(NoLayer)], - ]; - // Four scenarios, 2 tests each: - // 1. Compare a `StatusCode`, which is converted from an encoded value, with a `StatusCode` - // converted from an `Error`. - // 2. Compare an `Error, which is converted from an encoded value, with the expected `Error`. - for (i, &error_code) in SINGLE_NESTED_ERRORS.iter().enumerate() { - // No nesting and unit variant correctly returned. - assert_eq!(into_status_code([error_code, 0, 0, 0]), errors[i][0].into()); - assert_eq!(into_error([error_code, 0, 0, 0]), errors[i][0]); - // Allowed single nesting variant correctly returned. - assert_eq!(into_status_code([error_code, 1, 0, 0]), errors[i][1].into()); - assert_eq!(into_error([error_code, 1, 0, 0]), errors[i][1]); - // Unexpected third byte nested. - assert_eq!( - into_status_code([error_code, 1, 1, 0]), - (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }).into(), - ); - assert_eq!(into_error([error_code, 1, 1, 0]), errors[i][1]); - // Unexpected fourth byte nested. - assert_eq!( - into_status_code([error_code, 1, 1, 1]), - (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }).into(), - ); - assert_eq!(into_error([error_code, 1, 1, 1]), errors[i][1]); - } - } - - // Double nested variants: - // If the encoded value indicates a triple nested `Error` which is known by the Pop API version - // as a double nested variant, the encoded value is converted into `Error::Other`. - // - // Example: the error `Module { index: 10, error 5 }` (encoded: `[3, 10, 5, 0]`) with a non-zero - // value for the last byte: `[3, 10, 5, 3]` is converted into `[0, 3, 10, 5]`. This is - // decoded to `Error::Other { dispatch_error: 3, index: 10, error: 5 }`. - #[test] - fn double_nested_error_variants() { - // Four scenarios, 2 tests each: - // 1. Compare a `StatusCode`, which is converted from an encoded value, with a `StatusCode` - // converted from an `Error`. - // 2. Compare an `Error, which is converted from an encoded value, with the expected `Error`. - // - // No nesting and unit variant correctly returned. - assert_eq!(into_status_code([3, 0, 0, 0]), (Module { index: 0, error: 0 }).into()); - assert_eq!(into_error([3, 0, 0, 0]), Module { index: 0, error: 0 }); - // Allowed single nesting and variant correctly returned. - assert_eq!(into_status_code([3, 1, 0, 0]), (Module { index: 1, error: 0 }).into()); - assert_eq!(into_error([3, 1, 0, 0]), Module { index: 1, error: 0 }); - // Allowed double nesting and variant correctly returned. - assert_eq!(into_status_code([3, 1, 1, 0]), (Module { index: 1, error: 1 }).into()); - assert_eq!(into_error([3, 1, 1, 0]), Module { index: 1, error: 1 }); - // Unexpected fourth byte nested. - assert_eq!( - into_status_code([3, 1, 1, 1]), - (Other { dispatch_error_index: 3, error_index: 1, error: 1 }).into(), - ); - assert_eq!(into_error([3, 1, 1, 1]), Module { index: 1, error: 1 }); - } - - #[test] - fn single_nested_unknown_variants() { - // Unknown `TokenError` variant. - assert_eq!( - into_error([7, 10, 0, 0]), - Other { dispatch_error_index: 7, error_index: 10, error: 0 } - ); - assert_eq!( - into_status_code([7, 10, 0, 0]), - Other { dispatch_error_index: 7, error_index: 10, error: 0 }.into() - ); - // Unknown `Arithmetic` variant. - assert_eq!( - into_error([8, 3, 0, 0]), - Other { dispatch_error_index: 8, error_index: 3, error: 0 } - ); - assert_eq!( - into_status_code([8, 3, 0, 0]), - Other { dispatch_error_index: 8, error_index: 3, error: 0 }.into() - ); - // Unknown `Transactional` variant. - assert_eq!( - into_error([9, 2, 0, 0]), - Other { dispatch_error_index: 9, error_index: 2, error: 0 } - ); - assert_eq!( - into_status_code([9, 2, 0, 0]), - Other { dispatch_error_index: 9, error_index: 2, error: 0 }.into() - ); - } - - #[test] - fn test_random_encoded_values() { - assert_eq!( - into_error([100, 100, 100, 100]), - Other { dispatch_error_index: 100, error_index: 100, error: 100 } - ); - assert_eq!( - into_error([200, 200, 200, 200]), - Other { dispatch_error_index: 200, error_index: 200, error: 200 } - ); - } -} diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 55307f3e..cb27b17f 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -1,10 +1,8 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -use ink::{prelude::vec::Vec, ChainExtensionInstance}; -pub use sp_runtime::MultiAddress; +use ink::env::{chain_extension::FromStatusCode, DefaultEnvironment, Environment}; +use primitives::error::Error; -use crate::error::StatusCode; -use primitives::{storage_keys::*, AccountId as AccountId32}; #[cfg(feature = "assets")] pub use v0::assets; #[cfg(feature = "balances")] @@ -13,72 +11,49 @@ pub use v0::balances; pub use v0::cross_chain; #[cfg(feature = "nfts")] pub use v0::nfts; -use v0::{state, RuntimeCall}; -pub mod error; pub mod primitives; pub mod v0; -type AccountId = AccountId32; -// TODO: do the same as the AccountId above and check expanded macro code. -type Balance = ::Balance; +type AccountId = ::AccountId; +type Balance = ::Balance; #[cfg(any(feature = "nfts", feature = "cross-chain"))] -type BlockNumber = ::BlockNumber; +type BlockNumber = ::BlockNumber; pub type Result = core::result::Result; -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[ink::scale_derive(Encode, Decode, TypeInfo)] -pub enum Environment {} +pub struct StatusCode(pub u32); -impl ink::env::Environment for Environment { - const MAX_EVENT_TOPICS: usize = - ::MAX_EVENT_TOPICS; - - type AccountId = ::AccountId; - type Balance = ::Balance; - type Hash = ::Hash; - type BlockNumber = ::BlockNumber; - type Timestamp = ::Timestamp; - - type ChainExtension = PopApi; +impl From for StatusCode { + fn from(value: u32) -> Self { + StatusCode(value) + } } - -#[ink::chain_extension(extension = 909)] -pub trait PopApi { - type ErrorCode = StatusCode; - - #[ink(function = 0)] - #[allow(private_interfaces)] - fn dispatch(call: RuntimeCall) -> Result<()>; - - #[ink(function = 1)] - #[allow(private_interfaces)] - fn read_state(key: RuntimeStateKeys) -> Result>; - - #[cfg(feature = "cross-chain")] - #[ink(function = 2)] - #[allow(private_interfaces)] - fn send_xcm(xcm: primitives::cross_chain::CrossChainMessage) -> Result<()>; +impl FromStatusCode for StatusCode { + fn from_status_code(status_code: u32) -> Result<()> { + match status_code { + 0 => Ok(()), + _ => Err(StatusCode(status_code)), + } + } } -#[inline] -fn dispatch(call: RuntimeCall) -> Result<()> { - <::ChainExtension as ChainExtensionInstance>::instantiate( - ) - .dispatch(call) +impl From for StatusCode { + fn from(_: ink::scale::Error) -> Self { + StatusCode(255u32) + } } -#[inline] -fn read_state(key: RuntimeStateKeys) -> Result> { - <::ChainExtension as ChainExtensionInstance>::instantiate( - ) - .read_state(key) +impl From for Error { + fn from(value: StatusCode) -> Self { + value.0.into() + } } -#[cfg(feature = "cross-chain")] -fn send_xcm(xcm: primitives::cross_chain::CrossChainMessage) -> Result<()> { - <::ChainExtension as ChainExtensionInstance>::instantiate( - ) - .send_xcm(xcm) +impl From for StatusCode { + fn from(value: Error) -> Self { + StatusCode::from(u32::from(value)) + } } diff --git a/pop-api/src/primitives.rs b/pop-api/src/primitives.rs index 17419d5b..e174a111 100644 --- a/pop-api/src/primitives.rs +++ b/pop-api/src/primitives.rs @@ -1,2 +1 @@ pub use pop_primitives::*; -pub use sp_runtime::MultiAddress; diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index 376ca898..8d0de768 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -1,12 +1,6 @@ use ink::prelude::vec::Vec; -use crate::{ - assets, - primitives::{AssetId, MultiAddress}, - AccountId, Balance, StatusCode, -}; - -type Result = core::result::Result; +use crate::{assets, primitives::AssetId, AccountId, Balance, Result, StatusCode}; /// Local Fungibles: /// 1. PSP-22 Interface @@ -45,7 +39,7 @@ pub fn total_supply(id: AssetId) -> Result { /// # Returns /// The balance of the specified account, or an error if the operation fails. #[inline] -pub fn balance_of(id: AssetId, owner: AccountId) -> Result { +pub fn balance_of(id: AssetId, owner: AccountId) -> Balance { assets::balance_of(id, owner) } @@ -60,7 +54,7 @@ pub fn balance_of(id: AssetId, owner: AccountId) -> Result { /// # Returns /// The remaining allowance, or an error if the operation fails. #[inline] -pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { +pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Balance { assets::allowance(id, owner, spender) } @@ -75,11 +69,7 @@ pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result>, - value: Balance, -) -> Result<()> { +pub fn transfer(id: AssetId, to: AccountId, value: Balance) -> Result<()> { assets::transfer(id, to, value) } @@ -96,12 +86,7 @@ pub fn transfer( /// # Returns /// Returns `Ok(())` if successful, or an error if the transfer fails. #[inline] -pub fn transfer_from( - id: AssetId, - from: impl Into>, - to: impl Into>, - value: Balance, -) -> Result<()> { +pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, value: Balance) -> Result<()> { assets::transfer_approved(id, from, to, value) } @@ -161,7 +146,7 @@ pub fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Re /// # Returns /// The name of the token as a byte vector, or an error if the operation fails. #[inline] -pub fn token_name(id: AssetId) -> Result> { +pub fn token_name(id: AssetId) -> Vec { assets::token_name(id) } @@ -173,7 +158,7 @@ pub fn token_name(id: AssetId) -> Result> { /// # Returns /// The symbol of the token as a byte vector, or an error if the operation fails. #[inline] -pub fn token_symbol(id: AssetId) -> Result> { +pub fn token_symbol(id: AssetId) -> Vec { assets::token_symbol(id) } @@ -185,7 +170,7 @@ pub fn token_symbol(id: AssetId) -> Result> { /// # Returns /// The number of decimals of the token as a byte vector, or an error if the operation fails. #[inline] -pub fn token_decimals(id: AssetId) -> Result { +pub fn token_decimals(id: AssetId) -> u8 { assets::token_decimals(id) } @@ -291,6 +276,7 @@ pub fn token_decimals(id: AssetId) -> Result { // assets::asset_exists(id) // } +// TODO: further implement the rest of the interfaces and conclude on the FungiblesError. #[derive(Debug, PartialEq, Eq)] #[ink::scale_derive(Encode, Decode, TypeInfo)] pub enum FungiblesError { @@ -311,13 +297,13 @@ pub enum FungiblesError { NoPermission, /// The given asset ID is unknown. Unknown, - // // TODO: - // // - Originally `InsufficientBalance` for the deposit but this would result in the same error - // // as the error when there is insufficient balance for transferring an asset. + // - Originally `InsufficientBalance` for the deposit but this would result in the same error + // as the error when there is insufficient balance for transferring an asset. /// No balance for creation of assets or fees. NoBalance, } +// TODO: include conversions from TokenError and add conversions based on added interfaces. impl From for FungiblesError { fn from(value: StatusCode) -> Self { let encoded = value.0.to_le_bytes(); @@ -339,10 +325,10 @@ impl From for FungiblesError { #[cfg(test)] mod tests { - use scale::Decode; + use ink::scale::Decode; use super::FungiblesError; - use crate::error::{ + use crate::primitives::error::{ ArithmeticError::*, Error::{self, *}, TokenError::*, @@ -355,6 +341,7 @@ mod tests { status_code.into() } + // If we ever want to change the conversion from bytes to `u32`. #[test] fn status_code_vs_encoded() { assert_eq!(u32::decode(&mut &[3u8, 10, 2, 0][..]).unwrap(), 133635u32); @@ -373,6 +360,7 @@ mod tests { Other { dispatch_error_index: 5, error_index: 5, error: 1 }, CannotLookup, BadOrigin, + // `ModuleError` other than assets module. Module { index: 2, error: 5 }, ConsumerRemaining, NoProviders, @@ -384,6 +372,7 @@ mod tests { Corruption, Unavailable, RootNotAllowed, + UnknownFunctionId, DecodingFailed, ]; for error in errors { diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index 241c5020..22657323 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1,17 +1,18 @@ -use ink::{prelude::vec::Vec, scale::Compact}; +use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec, scale::Decode}; -use crate::{state::read, Balance, RuntimeCall, *}; -use primitives::{AssetId, MultiAddress}; +use crate::{primitives::AssetId, AccountId, Balance, Result, StatusCode}; pub mod fungibles; -type Result = core::result::Result; +const ASSETS_MODULE: u8 = 52; +const VERSION: u8 = 0; /// [Pallet Assets](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/assets/src/lib.rs): /// 1. Dispatchables /// 2. Read state functions /// /// 1. Dispatchables within pallet assets (TrustBackedAssets instance): +const DISPATCH: u8 = 0; /// - create /// - start_destroy /// - destroy_accounts @@ -21,25 +22,12 @@ type Result = core::result::Result; /// - burn /// - transfer /// - transfer_keep_alive -/// - force_transfer -/// - freeze -/// - thaw -/// - freeze_asset -/// - thaw_asset -/// - transfer_ownership -/// - set_team +const TRANSFER_KEEP_ALIVE: u8 = 9; /// - set_metadata /// - clear_metadata /// - approve_transfer /// - cancel_approval -/// - force_cancel_approval /// - transfer_approved -/// - touch -/// - refund -/// - set_min_balance -/// - touch_other -/// - refund_other -/// - block /// Issue a new class of fungible assets from a public origin. // pub(crate) fn create( @@ -98,16 +86,18 @@ type Result = core::result::Result; /// Move some assets from the sender account to another. #[inline] -pub(crate) fn transfer( - id: AssetId, - target: impl Into>, - amount: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { - id: id.into(), - target: target.into(), - amount: Compact(amount), - })) +pub fn transfer(id: AssetId, target: AccountId, amount: Balance) -> Result<()> { + ChainExtensionMethod::build(u32::from_le_bytes([ + VERSION, + DISPATCH, + ASSETS_MODULE, + // TODO: E.D. is always respected with transferring tokens via the API. + TRANSFER_KEEP_ALIVE, + ])) + .input::<(AssetId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, target, amount)) } // /// Move some assets from the sender account to another, keeping the sender account alive. @@ -122,68 +112,8 @@ pub(crate) fn transfer( // amount: Compact(amount), // })) // } -// -// /// Move some assets from one account to another. Sender should be the Admin of the asset `id`. -// pub(crate) fn force_transfer( -// id: AssetId, -// source: impl Into>, -// dest: impl Into>, -// amount: Balance, -// ) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::ForceTransfer { -// id: id.into(), -// source: source.into(), -// dest: dest.into(), -// amount: Compact(amount), -// })) -// } -// -// /// Disallow further unprivileged transfers of an asset `id` from an account `who`. `who` -// /// must already exist as an entry in `Account`s of the asset. If you want to freeze an -// /// account that does not have an entry, use `touch_other` first. -// pub(crate) fn freeze(id: AssetId, who: impl Into>) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::Freeze { id: id.into(), who: who.into() })) -// } -// -// /// Allow unprivileged transfers to and from an account again. -// pub(crate) fn thaw(id: AssetId, who: impl Into>) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::Thaw { id: id.into(), who: who.into() })) -// } -// -// /// Disallow further unprivileged transfers for the asset class. -// pub(crate) fn freeze_asset(id: AssetId) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::FreezeAsset { id: id.into() })) -// } -// -// /// Allow unprivileged transfers for the asset again. -// pub(crate) fn thaw_asset(id: AssetId) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::ThawAsset { id: id.into() })) -// } -// -// /// Change the Owner of an asset. -// pub(crate) fn transfer_ownership(id: AssetId, owner: impl Into>) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::TransferOwnership { -// id: id.into(), -// owner: owner.into(), -// })) -// } -// -// /// Change the Issuer, Admin and Freezer of an asset. -// pub(crate) fn set_team( -// id: AssetId, -// issuer: impl Into>, -// admin: impl Into>, -// freezer: impl Into>, -// ) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::SetTeam { -// id: id.into(), -// issuer: issuer.into(), -// admin: admin.into(), -// freezer: freezer.into(), -// })) -// } -/// Set the metadata for an asset. +// /// Set the metadata for an asset. // pub(crate) fn set_metadata( // id: AssetId, // name: Vec, @@ -200,315 +130,111 @@ pub(crate) fn transfer( /// Approve an amount of asset for transfer by a delegated third-party account. #[inline] -pub(crate) fn approve_transfer( - id: AssetId, - delegate: impl Into>, - amount: Balance, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::ApproveTransfer { - id: id.into(), - delegate: delegate.into(), - amount: Compact(amount), - })) +pub fn approve_transfer(id: AssetId, delegate: AccountId, amount: Balance) -> Result<()> { + ChainExtensionMethod::build(u32::from_le_bytes([0u8, 0, 52, 69])) + .input::<(AssetId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, delegate, amount)) } /// Cancel all of some asset approved for delegated transfer by a third-party account. #[inline] -pub(crate) fn cancel_approval( - id: AssetId, - delegate: impl Into>, -) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::CancelApproval { - id: id.into(), - delegate: delegate.into(), - })) +pub fn cancel_approval(id: AssetId, delegate: AccountId) -> Result<()> { + ChainExtensionMethod::build(0) + .input::<(AssetId, AccountId)>() + .output::, true>() + .handle_error_code::() + .call(&(id, delegate)) } -// /// Cancel all of some asset approved for delegated transfer by a third-party account. -// pub(crate) fn force_cancel_approval( -// id: AssetId, -// owner: impl Into>, -// delegate: impl Into>, -// ) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::ForceCancelApproval { -// id: id.into(), -// owner: owner.into(), -// delegate: delegate.into(), -// })) -// } - /// Transfer some asset balance from a previously delegated account to some third-party /// account. #[inline] -pub(crate) fn transfer_approved( +pub fn transfer_approved( id: AssetId, - owner: impl Into>, - destination: impl Into>, + from: AccountId, + to: AccountId, amount: Balance, ) -> Result<()> { - dispatch(RuntimeCall::Assets(AssetsCall::TransferApproved { - id: id.into(), - owner: owner.into(), - destination: destination.into(), - amount: Compact(amount), - })) + ChainExtensionMethod::build(0) + .input::<(AssetId, AccountId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, from, to, amount)) } -// -// /// Create an asset account for non-provider assets. -// pub(crate) fn touch(id: AssetId) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::Touch { id: id.into() })) -// } -// -// /// Return the deposit (if any) of an asset account or a consumer reference (if any) of an -// /// account. -// pub(crate) fn refund(id: AssetId, allow_burn: bool) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::Refund { id: id.into(), allow_burn })) -// } -// -// /// Sets the minimum balance of an asset. -// pub(crate) fn set_min_balance(id: AssetId, min_balance: Balance) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::SetMinBalance { -// id: id.into(), -// min_balance: Compact(min_balance), -// })) -// } -// -// /// Create an asset account for `who`. -// pub(crate) fn touch_other(id: AssetId, who: impl Into>) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::TouchOther { id: id.into(), who: who.into() })) -// } -// -// /// Return the deposit (if any) of a target asset account. Useful if you are the depositor. -// pub(crate) fn refund_other(id: AssetId, who: impl Into>) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::RefundOther { id: id.into(), who: who.into() })) -// } -// -// /// Disallow further unprivileged transfers of an asset `id` to and from an account `who`. -// pub(crate) fn block(id: AssetId, who: impl Into>) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::Block { id: id.into(), who: who.into() })) -// } /// 2. Read state functions +const READ_STATE: u8 = 1; /// - total_supply +const TOTAL_SUPPLY: u8 = 0; /// - balance_of /// - allowance /// - asset_exists /// - token_name /// - token_symbol /// - token_decimals -// + #[inline] -pub(crate) fn total_supply(id: AssetId) -> Result { - read(RuntimeStateKeys::Assets(AssetsKeys::TotalSupply(id))) +pub fn total_supply(id: AssetId) -> Result { + ChainExtensionMethod::build(u32::from_le_bytes([ + VERSION, + READ_STATE, + ASSETS_MODULE, + TOTAL_SUPPLY, + ])) + .input::() + .output::>, true>() + .handle_error_code::() + .call(&(id)) + .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) } #[inline] -pub(crate) fn balance_of(id: AssetId, owner: AccountId) -> Result { - read(RuntimeStateKeys::Assets(AssetsKeys::BalanceOf(id, owner))) +pub fn balance_of(id: AssetId, owner: AccountId) -> Balance { + ChainExtensionMethod::build(1) + .input::<(AssetId, AccountId)>() + .output::() + .ignore_error_code() + .call(&(id, owner)) } #[inline] -pub(crate) fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { - read(RuntimeStateKeys::Assets(AssetsKeys::Allowance(id, owner, spender))) +pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Balance { + ChainExtensionMethod::build(1) + .input::<(AssetId, AccountId, AccountId)>() + .output::() + .ignore_error_code() + .call(&(id, owner, spender)) } -// pub(crate) fn asset_exists(id: AssetId) -> Result { -// state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id))) -// } #[inline] -pub(crate) fn token_name(id: AssetId) -> Result> { - read(RuntimeStateKeys::Assets(AssetsKeys::TokenName(id))) +pub fn token_name(id: AssetId) -> Vec { + ChainExtensionMethod::build(1) + .input::() + .output::, false>() + .ignore_error_code() + .call(&(id)) } // #[inline] -pub(crate) fn token_symbol(id: AssetId) -> Result> { - read(RuntimeStateKeys::Assets(AssetsKeys::TokenSymbol(id))) +pub fn token_symbol(id: AssetId) -> Vec { + ChainExtensionMethod::build(1) + .input::() + .output::, false>() + .ignore_error_code() + .call(&(id)) } #[inline] -pub(crate) fn token_decimals(id: AssetId) -> Result { - read(RuntimeStateKeys::Assets(AssetsKeys::TokenDecimals(id))) +pub fn token_decimals(id: AssetId) -> u8 { + ChainExtensionMethod::build(1) + .input::() + .output::() + .ignore_error_code() + .call(&(id)) } -// Parameters to extrinsics representing an asset id (`AssetIdParameter`) and a balance amount (`Balance`) are expected -// to be compact encoded. The pop api handles that for the developer. -// -// reference: https://substrate.stackexchange.com/questions/1873/what-is-the-meaning-of-palletcompact-in-pallet-development -// -// Asset id that is compact encoded. -type AssetIdParameter = Compact; -// Balance amount that is compact encoded. -type BalanceParameter = Compact; -// -#[derive(Debug, PartialEq, Eq)] -#[ink::scale_derive(Encode, Decode, TypeInfo)] -pub(crate) enum AssetsCall { - // #[codec(index = 0)] - // Create { id: AssetIdParameter, admin: MultiAddress, min_balance: Balance }, - // #[codec(index = 2)] - // StartDestroy { id: AssetIdParameter }, - // #[codec(index = 3)] - // DestroyAccounts { id: AssetIdParameter }, - // #[codec(index = 4)] - // DestroyApprovals { id: AssetIdParameter }, - // #[codec(index = 5)] - // FinishDestroy { id: AssetIdParameter }, - // #[codec(index = 6)] - // Mint { - // id: AssetIdParameter, - // beneficiary: MultiAddress, - // amount: BalanceParameter, - // }, - // #[codec(index = 7)] - // Burn { id: AssetIdParameter, who: MultiAddress, amount: BalanceParameter }, - // #[codec(index = 8)] - // Transfer { id: AssetIdParameter, target: MultiAddress, amount: BalanceParameter }, - #[codec(index = 9)] - TransferKeepAlive { - id: AssetIdParameter, - target: MultiAddress, - amount: BalanceParameter, - }, - // #[codec(index = 10)] - // ForceTransfer { - // id: AssetIdParameter, - // source: MultiAddress, - // dest: MultiAddress, - // amount: BalanceParameter, - // }, - // #[codec(index = 11)] - // Freeze { id: AssetIdParameter, who: MultiAddress }, - // #[codec(index = 12)] - // Thaw { id: AssetIdParameter, who: MultiAddress }, - // #[codec(index = 13)] - // FreezeAsset { id: AssetIdParameter }, - // #[codec(index = 14)] - // ThawAsset { id: AssetIdParameter }, - // #[codec(index = 15)] - // TransferOwnership { id: AssetIdParameter, owner: MultiAddress }, - // #[codec(index = 16)] - // SetTeam { - // id: AssetIdParameter, - // issuer: MultiAddress, - // admin: MultiAddress, - // freezer: MultiAddress, - // }, - // #[codec(index = 17)] - // SetMetadata { id: AssetIdParameter, name: Vec, symbol: Vec, decimals: u8 }, - // #[codec(index = 18)] - // ClearMetadata { id: AssetIdParameter }, - #[codec(index = 22)] - ApproveTransfer { - id: AssetIdParameter, - delegate: MultiAddress, - amount: BalanceParameter, - }, - #[codec(index = 23)] - CancelApproval { id: AssetIdParameter, delegate: MultiAddress }, - // #[codec(index = 24)] - // ForceCancelApproval { - // id: AssetIdParameter, - // owner: MultiAddress, - // delegate: MultiAddress, - // }, - #[codec(index = 25)] - TransferApproved { - id: AssetIdParameter, - owner: MultiAddress, - destination: MultiAddress, - amount: BalanceParameter, - }, - // // #[codec(index = 26)] - // // Touch { id: AssetIdParameter }, - // // #[codec(index = 27)] - // // Refund { id: AssetIdParameter, allow_burn: bool }, - // // #[codec(index = 28)] - // // SetMinBalance { id: AssetIdParameter, min_balance: BalanceParameter }, - // // #[codec(index = 29)] - // // TouchOther { id: AssetIdParameter, who: MultiAddress }, - // // #[codec(index = 30)] - // // RefundOther { id: AssetIdParameter, who: MultiAddress }, - // // #[codec(index = 31)] - // // Block { id: AssetIdParameter, who: MultiAddress }, - // } - - // // TODO: Not being used atm but necessary if we want to provide access to the - // // rest of the pallet, outside of the use cases. - // #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] - // #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] - // pub enum AssetsError { - // /// Account balance must be greater than or equal to the transfer amount. - // BalanceLow, - // /// The account to alter does not exist. - // NoAccount, - // /// The signing account has no permission to do the operation. - // NoPermission, - // /// The given asset ID is unknown. - // Unknown, - // /// The origin account is frozen. - // Frozen, - // /// The asset ID is already taken. - // InUse, - // /// Invalid witness data given. - // BadWitness, - // /// Minimum balance should be non-zero. - // MinBalanceZero, - // /// Unable to increment the consumer reference counters on the account. Either no provider - // /// reference exists to allow a non-zero balance of a non-self-sufficient asset, or one - // /// fewer then the maximum number of consumers has been reached. - // UnavailableConsumer, - // /// Invalid metadata given. - // BadMetadata, - // /// No approval exists that would allow the transfer. - // Unapproved, - // /// The source account would not survive the transfer and it needs to stay alive. - // WouldDie, - // /// The asset-account already exists. - // AlreadyExists, - // /// The asset-account doesn't have an associated deposit. - // NoDeposit, - // /// The operation would result in funds being burned. - // WouldBurn, - // /// The asset is a live asset and is actively being used. Usually emit for operations such - // /// as `start_destroy` which require the asset to be in a destroying state. - // LiveAsset, - // /// The asset is not live, and likely being destroyed. - // AssetNotLive, - // /// The asset status is not the expected status. - // IncorrectStatus, - // /// The asset should be frozen before the given operation. - // NotFrozen, - // /// Callback action resulted in error. - // CallbackFailed, -} - -// -// impl TryFrom for AssetsError { -// type Error = Error; -// -// fn try_from(status_code: u32) -> core::result::Result { -// use AssetsError::*; -// match status_code { -// 0 => Ok(BalanceLow), -// 1 => Ok(NoAccount), -// 2 => Ok(NoPermission), -// 3 => Ok(Unknown), -// 4 => Ok(Frozen), -// 5 => Ok(InUse), -// 6 => Ok(BadWitness), -// 7 => Ok(MinBalanceZero), -// 8 => Ok(UnavailableConsumer), -// 9 => Ok(BadMetadata), -// 10 => Ok(Unapproved), -// 11 => Ok(WouldDie), -// 12 => Ok(AlreadyExists), -// 13 => Ok(NoDeposit), -// 14 => Ok(WouldBurn), -// 15 => Ok(LiveAsset), -// 16 => Ok(AssetNotLive), -// 17 => Ok(IncorrectStatus), -// 18 => Ok(NotFrozen), -// _ => todo!(), -// } -// } +// pub(crate) fn asset_exists(id: AssetId) -> Result { +// state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id))) // } diff --git a/pop-api/src/v0/balances.rs b/pop-api/src/v0/balances.rs index e14e6e32..bf029178 100644 --- a/pop-api/src/v0/balances.rs +++ b/pop-api/src/v0/balances.rs @@ -1,6 +1,9 @@ -use crate::{dispatch, primitives::MultiAddress, v0::RuntimeCall, AccountId, Error, StatusCode}; +use crate::{ + dispatch, primitives::MultiAddress, v0::RuntimeCall, AccountId, PopApiError, + PopApiError::UnknownStatusCode, +}; -type Result = core::result::Result; +type Result = core::result::Result; pub fn transfer_keep_alive( dest: impl Into>, @@ -14,7 +17,7 @@ pub fn transfer_keep_alive( #[derive(scale::Encode)] #[allow(dead_code)] -pub enum BalancesCall { +pub(crate) enum BalancesCall { #[codec(index = 3)] TransferKeepAlive { dest: MultiAddress, @@ -23,11 +26,9 @@ pub enum BalancesCall { }, } -// TODO: Not being used atm but necessary if we want to provide access to the -// rest of the pallet, outside of the use cases. #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum BalancesError { +pub enum Error { /// Vesting balance too high to send value. VestingBalance, /// Account liquidity restrictions prevent withdrawal. @@ -54,11 +55,11 @@ pub enum BalancesError { DeltaZero, } -impl TryFrom for BalancesError { - type Error = Error; +impl TryFrom for Error { + type Error = PopApiError; fn try_from(status_code: u32) -> core::result::Result { - use BalancesError::*; + use Error::*; match status_code { 0 => Ok(VestingBalance), 1 => Ok(LiquidityRestrictions), @@ -72,7 +73,16 @@ impl TryFrom for BalancesError { 9 => Ok(TooManyFreezes), 10 => Ok(IssuanceDeactivated), 11 => Ok(DeltaZero), - _ => todo!(), + _ => Err(UnknownStatusCode(status_code)), + } + } +} + +impl From for Error { + fn from(error: PopApiError) -> Self { + match error { + PopApiError::Balances(e) => e, + _ => panic!("expected balances error"), } } } diff --git a/pop-api/src/v0/cross_chain/mod.rs b/pop-api/src/v0/cross_chain/mod.rs index ad58e0e8..6732c119 100644 --- a/pop-api/src/v0/cross_chain/mod.rs +++ b/pop-api/src/v0/cross_chain/mod.rs @@ -1,16 +1,12 @@ -use crate::{BlockNumber, ParachainSystemKeys, Result, RuntimeStateKeys}; - pub mod coretime; -pub fn relay_chain_block_number() -> Result { - crate::v0::state::read(RuntimeStateKeys::ParachainSystem( - ParachainSystemKeys::LastRelayChainBlockNumber, - )) -} +use crate::{PopApiError::UnknownStatusCode, *}; + +type Result = core::result::Result; #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum CrossChainError { +pub enum Error { /// The desired destination was unreachable, generally because there is a no way of routing /// to it. Unreachable, @@ -66,11 +62,11 @@ pub enum CrossChainError { LocalExecutionIncomplete, } -impl TryFrom for CrossChainError { - type Error = crate::error::Error; +impl TryFrom for Error { + type Error = PopApiError; fn try_from(status_code: u32) -> core::result::Result { - use CrossChainError::*; + use Error::*; match status_code { 0 => Ok(Unreachable), 1 => Ok(SendFailure), @@ -96,7 +92,16 @@ impl TryFrom for CrossChainError { 21 => Ok(InvalidAssetUnknownReserve), 22 => Ok(InvalidAssetUnsupportedReserve), 23 => Ok(TooManyReserves), - _ => todo!(), + _ => Err(UnknownStatusCode(status_code)), + } + } +} + +impl From for Error { + fn from(error: PopApiError) -> Self { + match error { + PopApiError::Xcm(e) => e, + _ => panic!("expected xcm error"), } } } diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index f91c4271..310b360c 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -6,18 +6,3 @@ pub mod balances; pub mod cross_chain; #[cfg(feature = "nfts")] pub mod nfts; -pub mod state; - -#[derive(Debug, PartialEq, Eq)] -#[ink::scale_derive(Encode, Decode, TypeInfo)] -pub(crate) enum RuntimeCall { - #[codec(index = 10)] - #[cfg(feature = "balances")] - Balances(balances::BalancesCall), - #[codec(index = 50)] - #[cfg(feature = "nfts")] - Nfts(nfts::NftCalls), - #[codec(index = 52)] - #[cfg(feature = "assets")] - Assets(assets::AssetsCall), -} diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index 32539576..e111c8dc 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -1,21 +1,17 @@ +use super::RuntimeCall; +use crate::{PopApiError::UnknownStatusCode, *}; use ink::prelude::vec::Vec; +use primitives::{ApprovalsLimit, BoundedBTreeMap, KeyLimit, MultiAddress}; +pub use primitives::{CollectionId, ItemId}; use scale::Encode; - -use crate::{ - dispatch, - primitives::{ - nfts::{ApprovalsLimit, CollectionId, ItemId, KeyLimit}, - BoundedBTreeMap, BoundedVec, - }, - state, AccountId, Balance, BlockNumber, MultiAddress, NftsKeys, RuntimeCall, RuntimeStateKeys, - StatusCode, -}; pub use types::*; type Result = core::result::Result; type StringLimit = u32; type MaxTips = u32; +type Result = core::result::Result; + /// Issue a new collection of non-fungible items pub fn create( admin: impl Into>, @@ -524,7 +520,7 @@ pub(crate) enum NftCalls { #[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum NftsError { +pub enum Error { /// The signing account has no permission to do the operation. NoPermission, /// The given item ID is unknown. @@ -617,11 +613,11 @@ pub enum NftsError { WitnessRequired, } -impl TryFrom for NftsError { - type Error = crate::error::Error; +impl TryFrom for Error { + type Error = PopApiError; fn try_from(status_code: u32) -> core::result::Result { - use NftsError::*; + use Error::*; match status_code { 0 => Ok(NoPermission), 1 => Ok(UnknownCollection), @@ -668,7 +664,16 @@ impl TryFrom for NftsError { 42 => Ok(WrongNamespace), 43 => Ok(CollectionNotEmpty), 44 => Ok(WitnessRequired), - _ => todo!(), + _ => Err(UnknownStatusCode(status_code)), + } + } +} + +impl From for Error { + fn from(error: PopApiError) -> Self { + match error { + PopApiError::Nfts(e) => e, + _ => panic!("expected nfts error"), } } } @@ -677,7 +682,7 @@ impl TryFrom for NftsError { mod types { use super::*; use crate::{ - primitives::nfts::{CollectionId, ItemId}, + primitives::{CollectionId, ItemId}, Balance, BlockNumber, }; pub use enumflags2::{bitflags, BitFlags}; diff --git a/pop-api/src/v0/state.rs b/pop-api/src/v0/state.rs deleted file mode 100644 index afe48ba8..00000000 --- a/pop-api/src/v0/state.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::{error::StatusCode, primitives::storage_keys::RuntimeStateKeys, read_state}; -use ink::scale::Decode; - -#[inline] -pub fn read(key: RuntimeStateKeys) -> crate::Result { - read_state(key).and_then(|v| T::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) -} diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml index cb19b0ab..5cbd6d6c 100644 --- a/primitives/Cargo.toml +++ b/primitives/Cargo.toml @@ -6,10 +6,7 @@ edition = "2021" [dependencies] bounded-collections = { version = "0.1", default-features = false } - scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } -scale-decode = { version = "0.10.0", default-features = false, features = ["derive"], optional = true } -scale-encode = { version = "0.5.0", default-features = false, features = ["derive"], optional = true } scale-info = { version = "2.10", default-features = false, features = ["derive"], optional = true } [features] @@ -17,12 +14,8 @@ default = ["std"] std = [ "bounded-collections/std", "scale/std", - "scale-decode/std", - "scale-encode/std", "scale-info/std", ] -devnet = [] -testnet = [] assets = [] cross-chain = [] nfts = [] diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index dc0a5245..9d31653a 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -3,14 +3,15 @@ pub use bounded_collections::{BoundedBTreeMap, BoundedBTreeSet, BoundedVec}; use scale::{Decode, Encode, MaxEncodedLen}; #[cfg(feature = "std")] -use {scale_decode::DecodeAsType, scale_encode::EncodeAsType, scale_info::TypeInfo}; +use scale_info::TypeInfo; +pub use v0::error; #[cfg(feature = "cross-chain")] pub mod cross_chain; pub mod storage_keys; -#[derive(Encode, Decode, Clone, Debug, MaxEncodedLen, Eq, PartialEq, Ord, PartialOrd)] -#[cfg_attr(feature = "std", derive(TypeInfo, DecodeAsType, EncodeAsType))] +#[derive(Encode, Decode, Debug, MaxEncodedLen, Eq, PartialEq)] +#[cfg_attr(feature = "std", derive(TypeInfo))] pub struct AccountId(pub [u8; 32]); // Identifier for the class of asset. @@ -29,3 +30,120 @@ pub mod nfts { /// The maximum approvals an item could have. pub type ApprovalsLimit = ConstU32<20>; } + +pub mod v0 { + use super::*; + pub mod error { + use super::*; + + #[derive(Encode, Decode, Debug, Eq, PartialEq)] + #[cfg_attr(feature = "std", derive(TypeInfo))] + #[repr(u8)] + pub enum Error { + /// Some unknown error occurred. Go to the Pop API docs section `Pop API error`. + Other { + // Index within the `DispatchError` + dispatch_error_index: u8, + // Index within the `DispatchError` variant. + error_index: u8, + // Index for further nesting, e.g. pallet error. + error: u8, + } = 0, + /// Failed to lookup some data. + CannotLookup = 1, + /// A bad origin. + BadOrigin = 2, + /// A custom error in a module. + Module { index: u8, error: u8 } = 3, + /// At least one consumer is remaining so the account cannot be destroyed. + ConsumerRemaining = 4, + /// There are no providers so the account cannot be created. + NoProviders = 5, + /// There are too many consumers so the account cannot be created. + TooManyConsumers = 6, + /// An error to do with tokens. + Token(TokenError) = 7, + /// An arithmetic error. + Arithmetic(ArithmeticError) = 8, + /// The number of transactional layers has been reached, or we are not in a transactional + /// layer. + Transactional(TransactionalError) = 9, + /// Resources exhausted, e.g. attempt to read/write data which is too large to manipulate. + Exhausted = 10, + /// The state is corrupt; this is generally not going to fix itself. + Corruption = 11, + /// Some resource (e.g. a preimage) is unavailable right now. This might fix itself later. + Unavailable = 12, + /// Root origin is not allowed. + RootNotAllowed = 13, + /// Unknown function id. + UnknownFunctionId = 254, + /// Decoding failed on the runtime. + DecodingFailed = 255, + } + + impl From for Error { + fn from(value: u32) -> Self { + let encoded = value.to_le_bytes(); + Error::decode(&mut &encoded[..]).unwrap_or(Error::DecodingFailed) + } + } + + impl From for u32 { + fn from(value: Error) -> Self { + let mut encoded_error = value.encode(); + // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). + encoded_error.resize(4, 0); + u32::from_le_bytes( + encoded_error.try_into().expect("qid, resized to 4 bytes line above"), + ) + } + } + + #[derive(Encode, Decode, Clone, Debug, MaxEncodedLen, Eq, PartialEq, Ord, PartialOrd)] + #[cfg_attr(feature = "std", derive(TypeInfo))] + pub enum TokenError { + /// Funds are unavailable. + FundsUnavailable, + /// Some part of the balance gives the only provider reference to the account and thus cannot + /// be (re)moved. + OnlyProvider, + /// Account cannot exist with the funds that would be given. + BelowMinimum, + /// Account cannot be created. + CannotCreate, + /// The asset in question is unknown. + UnknownAsset, + /// Funds exist but are frozen. + Frozen, + /// Operation is not supported by the asset. + Unsupported, + /// Account cannot be created for a held balance. + CannotCreateHold, + /// Withdrawal would cause unwanted loss of account. + NotExpendable, + /// Account cannot receive the assets. + Blocked, + } + + #[derive(Encode, Decode, Debug, Eq, PartialEq)] + #[cfg_attr(feature = "std", derive(TypeInfo))] + pub enum ArithmeticError { + /// Underflow. + Underflow, + /// Overflow. + Overflow, + /// Division by zero. + DivisionByZero, + } + + #[derive(Encode, Decode, Debug, Eq, PartialEq)] + #[cfg_attr(feature = "std", derive(TypeInfo))] + pub enum TransactionalError { + /// Too many transactional layers have been spawned. + LimitReached, + /// A transactional layer was expected, but does not exist. + NoLayer, + } + } +} diff --git a/primitives/src/storage_keys.rs b/primitives/src/storage_keys.rs index 7992089b..e42dbca0 100644 --- a/primitives/src/storage_keys.rs +++ b/primitives/src/storage_keys.rs @@ -46,14 +46,11 @@ pub enum NftsKeys { #[cfg(feature = "assets")] #[derive(Encode, Decode, Debug, MaxEncodedLen)] pub enum AssetsKeys { - Allowance(AssetId, AccountId, AccountId), - // /// Check if the asset exists. - // // AssetExists(AssetId), - /// Check balance. - BalanceOf(AssetId, AccountId), - /// Returns the total token supply for a given asset ID. TotalSupply(AssetId), - TokenDecimals(AssetId), - TokenSymbol(AssetId), + BalanceOf(AssetId, AccountId), + Allowance(AssetId, AccountId, AccountId), TokenName(AssetId), + TokenSymbol(AssetId), + TokenDecimals(AssetId), + // AssetExists(AssetId), } diff --git a/runtime/devnet/Cargo.toml b/runtime/devnet/Cargo.toml index 5f52b855..7db68576 100644 --- a/runtime/devnet/Cargo.toml +++ b/runtime/devnet/Cargo.toml @@ -89,8 +89,9 @@ parachain-info.workspace = true [dev-dependencies] env_logger = "0.11.2" -hex = "0.4.3" enumflags2 = "0.7.9" +hex = "0.4.3" +rand = "0.8.5" [features] default = ["std"] diff --git a/runtime/devnet/src/extensions.rs b/runtime/devnet/src/extensions/mod.rs similarity index 79% rename from runtime/devnet/src/extensions.rs rename to runtime/devnet/src/extensions/mod.rs index 6f98bf96..ac52d5bd 100644 --- a/runtime/devnet/src/extensions.rs +++ b/runtime/devnet/src/extensions/mod.rs @@ -1,18 +1,22 @@ +use codec::{Compact, Decode, Encode}; use cumulus_pallet_parachain_system::RelaychainDataProvider; use frame_support::traits::{Contains, OriginTrait}; use frame_support::{ dispatch::{GetDispatchInfo, RawOrigin}, pallet_prelude::*, traits::{ - fungibles::{approvals::Inspect as ApprovalInspect, Inspect}, + fungibles::approvals::Inspect as ApprovalInspect, nonfungibles_v2::Inspect as NonFungiblesInspect, }, }; use pallet_contracts::chain_extension::{ - BufInBufOutState, ChainExtension, ChargedAmount, Environment, Ext, InitState, RetVal, + BufInBufOutState, ChainExtension, Environment, Ext, InitState, RetVal, }; use sp_core::crypto::UncheckedFrom; -use sp_runtime::traits::{BlockNumberProvider, Dispatchable}; +use sp_runtime::{ + traits::{BlockNumberProvider, Dispatchable}, + DispatchError, MultiAddress, +}; use sp_std::{boxed::Box, vec::Vec}; use xcm::{ latest::{prelude::*, OriginKind::SovereignAccount}, @@ -20,17 +24,24 @@ use xcm::{ }; use crate::{ - config::assets::TrustBackedAssetsInstance, AccountId, AllowedApiCalls, RuntimeCall, - RuntimeOrigin, UNIT, + config::assets::TrustBackedAssetsInstance, AccountId, AllowedApiCalls, Balance, Runtime, + RuntimeCall, RuntimeOrigin, UNIT, }; use pop_primitives::{ cross_chain::CrossChainMessage, nfts::{CollectionId, ItemId}, - storage_keys::{AssetsKeys, NftsKeys, ParachainSystemKeys, RuntimeStateKeys}, + storage_keys::{ + AssetsKeys::{self, *}, + NftsKeys, ParachainSystemKeys, RuntimeStateKeys, + }, AssetId, }; +mod v0; + const LOG_TARGET: &str = "pop-api::extension"; +// Versions: +const V0: u8 = 0; type ContractSchedule = ::Schedule; @@ -58,53 +69,162 @@ where { log::debug!(target:LOG_TARGET, " extension called "); let mut env = env.buf_in_buf_out(); - let contract_host_weight = ContractSchedule::::get().host_fn_weights; - // debug_message weight is a good approximation of the additional overhead of going + // Charge weight for making a call from a contract to the runtime. + // `debug_message` weight is a good approximation of the additional overhead of going // from contract layer to substrate layer. // reference: https://github.com/paritytech/ink-examples/blob/b8d2caa52cf4691e0ddd7c919e4462311deb5ad0/psp22-extension/runtime/psp22-extension-example.rs#L236 + let contract_host_weight = ContractSchedule::::get().host_fn_weights; env.charge_weight(contract_host_weight.debug_message)?; - let result = match v0::FuncId::try_from(env.func_id()) { - Ok(function) => { - // calculate weight for reading bytes of `len` + // Extract version and function_id from first two bytes. + let (version, function_id) = { + let bytes = env.func_id().to_le_bytes(); + (bytes[0], bytes[1]) + }; + // Extract pallet index and call / key index from last two bytes. + let (pallet_index, call_index) = { + let bytes = env.ext_id().to_le_bytes(); + (bytes[0], bytes[1]) + }; + + let result = match FuncId::try_from(function_id) { + Ok(function_id) => { + // Read encoded parameters from buffer and calculate weight for reading `len` bytes`. // reference: https://github.com/paritytech/polkadot-sdk/blob/117a9433dac88d5ac00c058c9b39c511d47749d2/substrate/frame/contracts/src/wasm/runtime.rs#L267 let len = env.in_len(); env.charge_weight(contract_host_weight.return_per_byte.saturating_mul(len.into()))?; - match function { - v0::FuncId::Dispatch => dispatch::(&mut env, len), - v0::FuncId::ReadState => read_state::(&mut env), - v0::FuncId::SendXcm => send_xcm::(&mut env), + let params = env.read(len)?; + log::debug!(target: LOG_TARGET, "Read input successfully"); + match function_id { + FuncId::Dispatch => { + dispatch::(&mut env, version, pallet_index, call_index, params) + }, + FuncId::ReadState => { + read_state::(&mut env, version, pallet_index, call_index, params) + }, + // TODO + FuncId::SendXcm => send_xcm::(&mut env), } }, Err(e) => Err(e), }; - // Convert any error to a status code and return Ok with RetVal::Converging match result { Ok(_) => Ok(RetVal::Converging(0)), - Err(e) => Ok(RetVal::Converging(convert_to_status_code(e))), + Err(e) => Ok(RetVal::Converging(convert_to_status_code(e, version))), } } } -fn dispatch(env: &mut Environment, len: u32) -> Result<(), DispatchError> +fn dispatch( + env: &mut Environment, + version: u8, + pallet_index: u8, + call_index: u8, + params: Vec, +) -> Result<(), DispatchError> where - T: pallet_contracts::Config - + frame_system::Config, + T: frame_system::Config, RuntimeOrigin: From>, E: Ext, { const LOG_PREFIX: &str = " dispatch |"; - - // read the input as RuntimeCall - let call: RuntimeCall = env.read_as_unbounded(len)?; - log::debug!(target: LOG_TARGET, "Read input as call successfully"); - // contract is the origin by default + let call = construct_call(version, pallet_index, call_index, params) + .map_err(|_| DispatchError::Other("DecodingFailed"))?; + // Contract is the origin by default. let origin: RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); dispatch_call::(env, call, origin, LOG_PREFIX) } -fn read_state(env: &mut Environment) -> Result<(), DispatchError> +fn dispatch_call( + env: &mut Environment, + call: RuntimeCall, + mut origin: RuntimeOrigin, + log_prefix: &str, +) -> Result<(), DispatchError> +where + T: frame_system::Config, + RuntimeOrigin: From>, + E: Ext, +{ + let charged_dispatch_weight = env.charge_weight(call.get_dispatch_info().weight)?; + log::debug!(target:LOG_TARGET, "{} Inputted RuntimeCall: {:?}", log_prefix, call); + origin.add_filter(AllowedApiCalls::contains); + match call.dispatch(origin) { + Ok(info) => { + log::debug!(target:LOG_TARGET, "{} success, actual weight: {:?}", log_prefix, info.actual_weight); + // Refund weight if the actual weight is less than the charged weight. + if let Some(actual_weight) = info.actual_weight { + env.adjust_weight(charged_dispatch_weight, actual_weight); + } + Ok(()) + }, + Err(err) => { + log::debug!(target:LOG_TARGET, "{} failed: error: {:?}", log_prefix, err.error); + Err(err.error) + }, + } +} + +fn construct_call( + version: u8, + pallet_index: u8, + call_index: u8, + params: Vec, +) -> Result { + match pallet_index { + 52 => { + let call = versioned_construct_assets_call(version, call_index, params)?; + Ok(RuntimeCall::Assets(call)) + }, + _ => Err(DispatchError::Other("UnknownFunctionId")), + } +} + +fn construct_key( + version: u8, + pallet_index: u8, + call_index: u8, + params: Vec, +) -> Result { + match pallet_index { + 52 => { + let key = versioned_construct_assets_key(version, call_index, params)?; + Ok(RuntimeStateKeys::Assets(key)) + }, + _ => Err(DispatchError::Other("UnknownFunctionId")), + } +} + +fn versioned_construct_assets_call( + version: u8, + call_index: u8, + params: Vec, +) -> Result, DispatchError> { + match version { + V0 => v0::assets::construct_assets_call(call_index, params), + _ => Err(DispatchError::Other("UnknownFunctionId")), + } +} + +fn versioned_construct_assets_key( + version: u8, + call_index: u8, + params: Vec, +) -> Result { + match version { + V0 => v0::assets::construct_assets_key(call_index, params), + _ => Err(DispatchError::Other("UnknownFunctionId")), + } +} + +fn read_state( + env: &mut Environment, + version: u8, + pallet_index: u8, + call_index: u8, + params: Vec, +) -> Result<(), DispatchError> where T: pallet_contracts::Config + pallet_assets::Config @@ -114,15 +234,13 @@ where E: Ext, { const LOG_PREFIX: &str = " read_state |"; - - let key: RuntimeStateKeys = env.read_as()?; + let key = construct_key(version, pallet_index, call_index, params)?; let result = match key { RuntimeStateKeys::Nfts(key) => read_nfts_state::(key, env), RuntimeStateKeys::ParachainSystem(key) => read_parachain_system_state::(key, env), RuntimeStateKeys::Assets(key) => read_assets_state::(key, env), }? .encode(); - log::trace!( target:LOG_TARGET, "{} result: {:?}.", LOG_PREFIX, result @@ -141,10 +259,9 @@ where E: Ext, { const LOG_PREFIX: &str = " send_xcm |"; - - // read the input as CrossChainMessage + // Read the input as CrossChainMessage. let xc_call: CrossChainMessage = env.read_as::()?; - // Determine the call to dispatch + // Determine the call to dispatch. let (dest, message) = match xc_call { CrossChainMessage::Relay(message) => { let dest = Location::parent().into_versioned(); @@ -165,91 +282,85 @@ where (dest, message) }, }; - // TODO: revisit to replace with signed contract origin let origin: RuntimeOrigin = RawOrigin::Root.into(); - - // Generate runtime call to dispatch + // Generate runtime call to dispatch. let call = RuntimeCall::PolkadotXcm(pallet_xcm::Call::send { dest: Box::new(dest), message: Box::new(VersionedXcm::V4(message)), }); - dispatch_call::(env, call, origin, LOG_PREFIX) } -pub(crate) fn convert_to_status_code(error: DispatchError) -> u32 { +// Converts a `DispatchError` to a `u32` status code based on the version of the API the contract uses. +// The contract calling the chain extension can convert the status code to the descriptive `Error`. +// +// For `Error` see `pop_primitives::::error::Error`. +// +// The error encoding can vary per version, allowing for flexible and backward-compatible error handling. +// As a result, contracts maintain compatibility across different versions of the runtime. +// +// # Parameters +// +// - `error`: The `DispatchError` encountered during contract execution. +// - `version`: The version of the chain extension, used to determine the known errors. +pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { + // "UnknownFunctionId" and "DecodingFailed" are mapped to specific errors in the API and will + // never change. let mut encoded_error = match error { DispatchError::Other("UnknownFunctionId") => vec![254, 0, 0, 0], + DispatchError::Other("DecodingFailed") => vec![255, 0, 0, 0], _ => error.encode(), }; // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). encoded_error.resize(4, 0); - u32::decode(&mut &encoded_error[..]).expect("qid, resized to 4 bytes line above") + let mut encoded_error = encoded_error.try_into().expect("qid, resized to 4 bytes line above"); + match version { + // If an unknown variant of the `DispatchError` is detected the error needs to be converted + // into the encoded value of `Error::Other`. This conversion is performed by shifting the bytes one + // position forward (discarding the last byte as it is not used) and setting the first byte to the + // encoded value of `Other` (0u8). This ensures the error is correctly categorized as an `Other` + // variant which provides all the necessary information to debug which error occurred in the runtime. + // + // Byte layout explanation: + // - Byte 0: index of the variant within `Error` + // - Byte 1: + // - Must be zero for `UNIT_ERRORS`. + // - Represents the nested error in `SINGLE_NESTED_ERRORS`. + // - Represents the first level of nesting in `DOUBLE_NESTED_ERRORS`. + // - Byte 2: + // - Represents the second level of nesting in `DOUBLE_NESTED_ERRORS`. + // - Byte 3: + // - Unused or represents further nested information. + 0 => v0::error::handle_unknown_error(&mut encoded_error), + _ => encoded_error = [254, 0, 0, 0], + } + u32::from_le_bytes(encoded_error) } -pub mod v0 { - #[derive(Debug)] - pub enum FuncId { - Dispatch, - ReadState, - SendXcm, - } +#[derive(Debug)] +pub enum FuncId { + Dispatch, + ReadState, + SendXcm, } -impl TryFrom for v0::FuncId { +impl TryFrom for FuncId { type Error = DispatchError; - fn try_from(func_id: u16) -> Result { + fn try_from(func_id: u8) -> Result { let id = match func_id { - 0x0 => Self::Dispatch, - 0x1 => Self::ReadState, - 0x2 => Self::SendXcm, + 0 => Self::Dispatch, + 1 => Self::ReadState, + 2 => Self::SendXcm, _ => { - log::error!("called an unregistered `func_id`: {:}", func_id); return Err(DispatchError::Other("UnknownFuncId")); }, }; - Ok(id) } } -fn dispatch_call( - env: &mut Environment, - call: RuntimeCall, - mut origin: RuntimeOrigin, - log_prefix: &str, -) -> Result<(), DispatchError> -where - T: frame_system::Config, - RuntimeOrigin: From>, - E: Ext, -{ - let charged_dispatch_weight = env.charge_weight(call.get_dispatch_info().weight)?; - - log::debug!(target:LOG_TARGET, "{} Inputted RuntimeCall: {:?}", log_prefix, call); - - origin.add_filter(AllowedApiCalls::contains); - - match call.dispatch(origin) { - Ok(info) => { - log::debug!(target:LOG_TARGET, "{} success, actual weight: {:?}", log_prefix, info.actual_weight); - - // refund weight if the actual weight is less than the charged weight - if let Some(actual_weight) = info.actual_weight { - env.adjust_weight(charged_dispatch_weight, actual_weight); - } - - Ok(()) - }, - Err(err) => { - log::debug!(target:LOG_TARGET, "{} failed: error: {:?}", log_prefix, err.error); - Err(err.error) - }, - } -} - fn read_parachain_system_state( key: ParachainSystemKeys, env: &mut Environment, @@ -323,7 +434,7 @@ where T: frame_system::Config, { match key { - AssetsKeys::Allowance(id, owner, spender) => { + Allowance(id, owner, spender) => { env.charge_weight(T::DbWeight::get().reads(1_u64))?; Ok(pallet_assets::Pallet::::allowance( id, @@ -336,12 +447,12 @@ where // env.charge_weight(T::DbWeight::get().reads(1_u64))?; // Ok(pallet_assets::Pallet::::asset_exists(id).encode()) // }, - AssetsKeys::BalanceOf(id, owner) => { + BalanceOf(id, owner) => { env.charge_weight(T::DbWeight::get().reads(1_u64))?; Ok(pallet_assets::Pallet::::balance(id, &owner.0.into()) .encode()) }, - AssetsKeys::TotalSupply(id) => { + TotalSupply(id) => { env.charge_weight(T::DbWeight::get().reads(1_u64))?; Ok(pallet_assets::Pallet::::total_supply(id).encode()) }, @@ -354,88 +465,51 @@ mod tests { use super::*; use crate::{Assets, Runtime, System}; use sp_runtime::BuildStorage; + // Test ensuring `func_id()` and `ext_id()` work as expected, i.e. extracting the first two + // bytes and the last two bytes, respectively, from a 4 byte array. + #[test] + fn test_byte_extraction() { + use rand::Rng; - fn new_test_ext() -> sp_io::TestExternalities { - let t = frame_system::GenesisConfig::::default() - .build_storage() - .expect("Frame system builds valid default genesis config"); - let mut ext = sp_io::TestExternalities::new(t); - ext.execute_with(|| System::set_block_number(1)); - ext - } + // Helper functions + fn func_id(id: u32) -> u16 { + (id & 0x0000FFFF) as u16 + } + fn ext_id(id: u32) -> u16 { + (id >> 16) as u16 + } - #[test] - fn encoding_decoding_dispatch_error() { - use codec::{Decode, Encode}; - use sp_runtime::{ArithmeticError, DispatchError, ModuleError, TokenError}; + // Number of test iterations + let test_iterations = 1_000_000; - new_test_ext().execute_with(|| { - let error = DispatchError::Module(ModuleError { - index: 255, - error: [2, 0, 0, 0], - message: Some("error message"), - }); - let encoded = error.encode(); - let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); - assert_eq!(encoded, vec![3, 255, 2, 0, 0, 0]); - assert_eq!( - decoded, - // `message` is skipped for encoding. - DispatchError::Module(ModuleError { - index: 255, - error: [2, 0, 0, 0], - message: None - }) - ); - println!("Encoded Module Error: {:?}", encoded); + // Create a random number generator + let mut rng = rand::thread_rng(); - // Example pallet assets Error into ModuleError. - let index = - <::PalletInfo as frame_support::traits::PalletInfo>::index::< - Assets, - >() - .expect("Every active module has an index in the runtime; qed") as u8; + // Run the test for a large number of random 4-byte arrays + for _ in 0..test_iterations { + // Generate a random 4-byte array + let bytes: [u8; 4] = rng.gen(); - let mut error = - pallet_assets::Error::NotFrozen::.encode(); - error.resize(MAX_MODULE_ERROR_ENCODED_SIZE, 0); - let error = DispatchError::Module(ModuleError { - index, - error: TryInto::try_into(error).expect("should work"), - message: None, - }); - let encoded = error.encode(); - let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); - assert_eq!(encoded, vec![3, 52, 18, 0, 0, 0]); - assert_eq!( - decoded, - DispatchError::Module(ModuleError { - index: 52, - error: [18, 0, 0, 0], - message: None - }) - ); - println!("Encoded Module Error: {:?}", encoded); + // Convert the 4-byte array to a u32 value + let value = u32::from_le_bytes(bytes); - // Example DispatchError::Token - let error = DispatchError::Token(TokenError::UnknownAsset); - let encoded = error.encode(); - assert_eq!(encoded, vec![7, 4]); - println!("Encoded Token Error: {:?}", encoded); + // Extract the first two bytes (least significant 2 bytes) + let first_two_bytes = func_id(value); - // Example DispatchError::Arithmetic - let error = DispatchError::Arithmetic(ArithmeticError::Overflow); - let encoded = error.encode(); - assert_eq!(encoded, vec![8, 1]); - println!("Encoded Arithmetic Error: {:?}", encoded); - }); + // Extract the last two bytes (most significant 2 bytes) + let last_two_bytes = ext_id(value); + + // Check if the first two bytes match the expected value + assert_eq!([bytes[0], bytes[1]], first_two_bytes.to_le_bytes()); + + // Check if the last two bytes match the expected value + assert_eq!([bytes[2], bytes[3]], last_two_bytes.to_le_bytes()); + } } + // Test showing all the different type of variants and its encoding. #[test] fn encoding_of_enum() { - use codec::{Decode, Encode}; - - // Comprehensive enum with all different type of variants. #[derive(Debug, PartialEq, Encode, Decode)] enum ComprehensiveEnum { SimpleVariant, @@ -494,39 +568,77 @@ mod tests { println!("{:?} -> {:?}", enum_nested_enum_struct, enum_nested_enum_struct.encode()); } + fn new_test_ext() -> sp_io::TestExternalities { + let t = frame_system::GenesisConfig::::default() + .build_storage() + .expect("Frame system builds valid default genesis config"); + let mut ext = sp_io::TestExternalities::new(t); + ext.execute_with(|| System::set_block_number(1)); + ext + } + #[test] - fn dispatch_error_to_status_code() { - // Create all the different `DispatchError` variants with its respective `PopApiError`. - let test_cases = vec![ - (DispatchError::Other("hallo"), [0, 0, 0, 0]), - (DispatchError::CannotLookup, [1, 0, 0, 0]), - (DispatchError::BadOrigin, [2, 0, 0, 0]), - ( - DispatchError::Module(sp_runtime::ModuleError { - index: 1, + fn encoding_decoding_dispatch_error() { + use sp_runtime::{ArithmeticError, DispatchError, ModuleError, TokenError}; + + new_test_ext().execute_with(|| { + let error = DispatchError::Module(ModuleError { + index: 255, + error: [2, 0, 0, 0], + message: Some("error message"), + }); + let encoded = error.encode(); + let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); + assert_eq!(encoded, vec![3, 255, 2, 0, 0, 0]); + assert_eq!( + decoded, + // `message` is skipped for encoding. + DispatchError::Module(ModuleError { + index: 255, error: [2, 0, 0, 0], - message: Some("hallo"), - }), - [3, 1, 2, 0], - ), - (DispatchError::ConsumerRemaining, [4, 0, 0, 0]), - (DispatchError::NoProviders, [5, 0, 0, 0]), - (DispatchError::TooManyConsumers, [6, 0, 0, 0]), - (DispatchError::Token(sp_runtime::TokenError::BelowMinimum), [7, 2, 0, 0]), - (DispatchError::Arithmetic(sp_runtime::ArithmeticError::Overflow), [8, 1, 0, 0]), - ( - DispatchError::Transactional(sp_runtime::TransactionalError::LimitReached), - [9, 0, 0, 0], - ), - (DispatchError::Exhausted, [10, 0, 0, 0]), - (DispatchError::Corruption, [11, 0, 0, 0]), - (DispatchError::Unavailable, [12, 0, 0, 0]), - (DispatchError::RootNotAllowed, [13, 0, 0, 0]), - ]; - for (error, encoded_error) in test_cases { - let status_code = crate::extensions::convert_to_status_code(error); - assert_eq!(status_code, u32::decode(&mut &encoded_error[..]).unwrap()); - } + message: None + }) + ); + + // Example pallet assets Error into ModuleError. + let index = <::PalletInfo as frame_support::traits::PalletInfo>::index::< + Assets, + >() + .expect("Every active module has an index in the runtime; qed") as u8; + let mut error = + pallet_assets::Error::NotFrozen::.encode(); + error.resize(MAX_MODULE_ERROR_ENCODED_SIZE, 0); + let error = DispatchError::Module(ModuleError { + index, + error: TryInto::try_into(error).expect("should work"), + message: None, + }); + let encoded = error.encode(); + let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); + assert_eq!(encoded, vec![3, 52, 18, 0, 0, 0]); + assert_eq!( + decoded, + DispatchError::Module(ModuleError { + index: 52, + error: [18, 0, 0, 0], + message: None + }) + ); + + // Example DispatchError::Token + let error = DispatchError::Token(TokenError::UnknownAsset); + let encoded = error.encode(); + let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); + assert_eq!(encoded, vec![7, 4]); + assert_eq!(decoded, error); + + // Example DispatchError::Arithmetic + let error = DispatchError::Arithmetic(ArithmeticError::Overflow); + let encoded = error.encode(); + let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); + assert_eq!(encoded, vec![8, 1]); + assert_eq!(decoded, error); + }); } } // use enumflags2::BitFlags; diff --git a/runtime/devnet/src/extensions/v0/assets.rs b/runtime/devnet/src/extensions/v0/assets.rs new file mode 100644 index 00000000..912b116f --- /dev/null +++ b/runtime/devnet/src/extensions/v0/assets.rs @@ -0,0 +1,39 @@ +use crate::extensions::{ + AccountId, AssetId, + AssetsKeys::{self, TotalSupply}, + Balance, Compact, Decode, DispatchError, MultiAddress, Runtime, TrustBackedAssetsInstance, +}; + +pub(crate) fn construct_assets_key( + call_index: u8, + params: Vec, +) -> Result { + match call_index { + 0 => { + let id = ::decode(&mut ¶ms[..]) + .map_err(|_| DispatchError::Other("DecodingFailed"))?; + Ok(TotalSupply(id)) + }, + // other calls + _ => Err(DispatchError::Other("UnknownFunctionId")), + } +} + +pub(crate) fn construct_assets_call( + call_index: u8, + params: Vec, +) -> Result, DispatchError> { + match call_index { + 9 => { + let (id, target, amount) = <(AssetId, AccountId, Balance)>::decode(&mut ¶ms[..]) + .map_err(|_| DispatchError::Other("DecodingFailed"))?; + Ok(pallet_assets::Call::::transfer_keep_alive { + id: Compact(id), + target: MultiAddress::Id(target), + amount, + }) + }, + // other calls + _ => Err(DispatchError::Other("UnknownFunctionId")), + } +} diff --git a/runtime/devnet/src/extensions/v0/error.rs b/runtime/devnet/src/extensions/v0/error.rs new file mode 100644 index 00000000..b8af2250 --- /dev/null +++ b/runtime/devnet/src/extensions/v0/error.rs @@ -0,0 +1,298 @@ +#[cfg(test)] +use crate::extensions::convert_to_status_code; + +pub(crate) fn handle_unknown_error(encoded_error: &mut [u8; 4]) { + let unknown = match encoded_error[0] { + code if UNIT_ERRORS.contains(&code) => nested_errors(&encoded_error[1..], None), + // Single nested errors with a limit in their nesting. + // + // `TokenError`: has ten variants - translated to a limit of nine. + 7 => nested_errors(&encoded_error[1..], Some(9)), + // `ArithmeticError`: has 3 variants - translated to a limit of two. + 8 => nested_errors(&encoded_error[1..], Some(2)), + // `TransactionalError`: has 2 variants - translated to a limit of one. + 9 => nested_errors(&encoded_error[1..], Some(1)), + code if DOUBLE_NESTED_ERRORS.contains(&code) => nested_errors(&encoded_error[3..], None), + _ => true, + }; + if unknown { + encoded_error[..].rotate_right(1); + encoded_error[0] = 0u8; + } +} + +// Unit `Error` variants. +// (variant: index): +// - CannotLookup: 1, +// - BadOrigin: 2, +// - ConsumerRemaining: 4, +// - NoProviders: 5, +// - TooManyConsumers: 6, +// - Exhausted: 10, +// - Corruption: 11, +// - Unavailable: 12, +// - RootNotAllowed: 13, +// - UnknownFunctionId: 254, +// - DecodingFailed: 255, +const UNIT_ERRORS: [u8; 11] = [1, 2, 4, 5, 6, 10, 11, 12, 13, 254, 255]; + +#[cfg(test)] +const SINGLE_NESTED_ERRORS: [u8; 3] = [7, 8, 9]; + +// Double nested `Error` variants +// (variant: index): +// - Module: 3, +const DOUBLE_NESTED_ERRORS: [u8; 1] = [3]; + +// Checks for unknown nested errors within the `DispatchError`. +// - For single nested errors with a limit, it verifies if the nested value exceeds the limit. +// - For other nested errors, it checks if any subsequent bytes are non-zero. +// +// `nested_error` - The slice of bytes representing the nested error. +// `limit` - An optional limit for single nested errors. +fn nested_errors(nested_error: &[u8], limit: Option) -> bool { + match limit { + Some(l) => nested_error[0] > l || nested_error[1..].iter().any(|&x| x != 0u8), + None => nested_error.iter().any(|&x| x != 0u8), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use pop_primitives::error::{ + ArithmeticError::*, + Error::{self, *}, + TokenError::*, + TransactionalError::*, + }; + use sp_runtime::DispatchError; + + // Compare all the different `DispatchError` variants with the expected `Error`. + #[test] + fn dispatch_error_to_error() { + let test_cases = vec![ + ( + DispatchError::Other(""), + (Other { dispatch_error_index: 0, error_index: 0, error: 0 }), + ), + (DispatchError::Other("UnknownFunctionId"), UnknownFunctionId), + (DispatchError::Other("DecodingFailed"), DecodingFailed), + (DispatchError::CannotLookup, CannotLookup), + (DispatchError::BadOrigin, BadOrigin), + ( + DispatchError::Module(sp_runtime::ModuleError { + index: 1, + error: [2, 0, 0, 0], + message: Some("hallo"), + }), + Module { index: 1, error: 2 }, + ), + (DispatchError::ConsumerRemaining, ConsumerRemaining), + (DispatchError::NoProviders, NoProviders), + (DispatchError::TooManyConsumers, TooManyConsumers), + (DispatchError::Token(sp_runtime::TokenError::BelowMinimum), Token(BelowMinimum)), + ( + DispatchError::Arithmetic(sp_runtime::ArithmeticError::Overflow), + Arithmetic(Overflow), + ), + ( + DispatchError::Transactional(sp_runtime::TransactionalError::LimitReached), + Transactional(LimitReached), + ), + (DispatchError::Exhausted, Exhausted), + (DispatchError::Corruption, Corruption), + (DispatchError::Unavailable, Unavailable), + (DispatchError::RootNotAllowed, RootNotAllowed), + ]; + for (dispatch_error, expected) in test_cases { + let status_code = crate::extensions::convert_to_status_code(dispatch_error, 0); + let error: Error = status_code.into(); + assert_eq!(error, expected); + } + } + + // Compare all the different `DispatchError::Other` possibilities with the expected `Error`. + #[test] + fn other_error() { + let test_cases = vec![ + ( + DispatchError::Other("Random"), + (Other { dispatch_error_index: 0, error_index: 0, error: 0 }), + ), + (DispatchError::Other("UnknownFunctionId"), UnknownFunctionId), + (DispatchError::Other("DecodingFailed"), DecodingFailed), + ]; + for (dispatch_error, expected) in test_cases { + let status_code = convert_to_status_code(dispatch_error, 0); + let error: Error = status_code.into(); + assert_eq!(error, expected); + } + } + + // Compare all the different `DispatchError::Module` nesting possibilities, which can not be + // handled, with the expected `Error`. See `double_nested_error_variants` fourth byte nesting. + #[test] + fn test_module_error() { + let test_cases = vec![ + DispatchError::Module(sp_runtime::ModuleError { + index: 1, + error: [2, 2, 0, 0], + message: Some("Random"), + }), + DispatchError::Module(sp_runtime::ModuleError { + index: 1, + error: [2, 2, 2, 0], + message: Some("Random"), + }), + DispatchError::Module(sp_runtime::ModuleError { + index: 1, + error: [2, 2, 2, 2], + message: Some("Random"), + }), + ]; + for dispatch_error in test_cases { + let status_code = convert_to_status_code(dispatch_error, 0); + let error: Error = status_code.into(); + assert_eq!(error, Other { dispatch_error_index: 3, error_index: 1, error: 2 }); + } + } + + // Converts 4 bytes into `Error` and handles unknown errors (used in `convert_to_status_code`). + fn into_error(mut error_bytes: [u8; 4]) -> Error { + handle_unknown_error(&mut error_bytes); + u32::from_le_bytes(error_bytes).into() + } + + // Tests the `handle_unknown_error` for `Error`, version 0. + // + // Unit variants: + // If the encoded value indicates a nested `Error` which is known by V0 as a + // unit variant, the encoded value is converted into `Error::Other`. + // + // Example: the error `BadOrigin` (encoded: `[2, 0, 0, 0]`) with a non-zero value for one + // of the bytes [1..4]: `[2, 0, 1, 0]` is converted into `[0, 2, 0, 1]` (shifting the bits + // one forward). This is decoded to `Error::Other { dispatch_error: 2, index: 0, error: 1 }`. + #[test] + fn unit_error_variants() { + let errors = vec![ + CannotLookup, + BadOrigin, + ConsumerRemaining, + NoProviders, + TooManyConsumers, + Exhausted, + Corruption, + Unavailable, + RootNotAllowed, + UnknownFunctionId, + DecodingFailed, + ]; + // Compare an `Error`, which is converted from an encoded value, with the expected `Error`. + for (i, &error_code) in UNIT_ERRORS.iter().enumerate() { + // No nesting and unit variant correctly returned. + assert_eq!(into_error([error_code, 0, 0, 0]), errors[i]); + // Unexpected second byte nested. + assert_eq!( + into_error([error_code, 1, 0, 0]), + (Other { dispatch_error_index: error_code, error_index: 1, error: 0 }), + ); + // Unexpected third byte nested. + assert_eq!( + into_error([error_code, 1, 1, 0]), + (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }), + ); + // Unexpected fourth byte nested. + assert_eq!( + into_error([error_code, 1, 1, 1]), + (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }), + ); + } + } + + // Single nested variants: + // If the encoded value indicates a double nested `Error` which is known by V0 + // as a single nested variant, the encoded value is converted into `Error::Other`. + // + // Example: the error `Arithmetic(Overflow)` (encoded: `[8, 1, 0, 0]`) with a non-zero + // value for one of the bytes [2..4]: `[8, 1, 1, 0]` is converted into `[0, 8, 1, 1]`. This is + // decoded to `Error::Other { dispatch_error: 8, index: 1, error: 1 }`. + #[test] + fn single_nested_error_variants() { + let errors = vec![ + [Token(FundsUnavailable), Token(OnlyProvider)], + [Arithmetic(Underflow), Arithmetic(Overflow)], + [Transactional(LimitReached), Transactional(NoLayer)], + ]; + // Compare an `Error`, which is converted from an encoded value, with the expected `Error`. + for (i, &error_code) in SINGLE_NESTED_ERRORS.iter().enumerate() { + // No nested and single nested variant correctly returned. + assert_eq!(into_error([error_code, 0, 0, 0]), errors[i][0]); + assert_eq!(into_error([error_code, 1, 0, 0]), errors[i][1]); + // Unexpected third byte nested. + assert_eq!( + into_error([error_code, 1, 1, 0]), + (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }), + ); + // Unexpected fourth byte nested. + assert_eq!( + into_error([error_code, 1, 1, 1]), + Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, + ); + } + } + + #[test] + fn single_nested_unknown_variants() { + // Unknown `TokenError` variant. + assert_eq!( + into_error([7, 10, 0, 0]), + Other { dispatch_error_index: 7, error_index: 10, error: 0 } + ); + // Unknown `Arithmetic` variant. + assert_eq!( + into_error([8, 3, 0, 0]), + Other { dispatch_error_index: 8, error_index: 3, error: 0 } + ); + // Unknown `Transactional` variant. + assert_eq!( + into_error([9, 2, 0, 0]), + Other { dispatch_error_index: 9, error_index: 2, error: 0 } + ); + } + + // Double nested variants: + // If the encoded value indicates a triple nested `Error` which is known by V0 + // as a double nested variant, the encoded value is converted into `Error::Other`. + // + // Example: the error `Module { index: 10, error 5 }` (encoded: `[3, 10, 5, 0]`) with a non-zero + // value for the last byte: `[3, 10, 5, 3]` is converted into `[0, 3, 10, 5]`. This is + // decoded to `Error::Other { dispatch_error: 3, index: 10, error: 5 }`. + #[test] + fn double_nested_error_variants() { + // Compare an `Error`, which is converted from an encoded value, with the expected `Error`. + // No nesting and unit variant correctly returned. + assert_eq!(into_error([3, 0, 0, 0]), Module { index: 0, error: 0 }); + // Allowed single nesting and variant correctly returned. + assert_eq!(into_error([3, 1, 0, 0]), Module { index: 1, error: 0 }); + // Allowed double nesting and variant correctly returned. + assert_eq!(into_error([3, 1, 1, 0]), Module { index: 1, error: 1 }); + // Unexpected fourth byte nested. + assert_eq!( + into_error([3, 1, 1, 1]), + Other { dispatch_error_index: 3, error_index: 1, error: 1 }, + ); + } + + #[test] + fn test_random_encoded_values() { + assert_eq!( + into_error([100, 100, 100, 100]), + Other { dispatch_error_index: 100, error_index: 100, error: 100 } + ); + assert_eq!( + into_error([200, 200, 200, 200]), + Other { dispatch_error_index: 200, error_index: 200, error: 200 } + ); + } +} diff --git a/runtime/devnet/src/extensions/v0/mod.rs b/runtime/devnet/src/extensions/v0/mod.rs new file mode 100644 index 00000000..6406e08f --- /dev/null +++ b/runtime/devnet/src/extensions/v0/mod.rs @@ -0,0 +1,2 @@ +pub(crate) mod assets; +pub(crate) mod error; diff --git a/runtime/testnet/Cargo.toml b/runtime/testnet/Cargo.toml index b7aa7b76..b04c3102 100644 --- a/runtime/testnet/Cargo.toml +++ b/runtime/testnet/Cargo.toml @@ -22,7 +22,7 @@ scale-info.workspace = true smallvec.workspace = true # Local -pop-primitives = { workspace = true, features = ["assets", "nfts", "cross-chain"] } +pop-primitives = { workspace = true, features = ["nfts", "cross-chain"] } pop-runtime-common = { workspace = true, default-features = false } # Substrate diff --git a/runtime/testnet/src/extensions.rs b/runtime/testnet/src/extensions.rs index dd2c52bb..4c224544 100644 --- a/runtime/testnet/src/extensions.rs +++ b/runtime/testnet/src/extensions.rs @@ -3,7 +3,7 @@ use frame_support::traits::{Contains, OriginTrait}; use frame_support::{ dispatch::{GetDispatchInfo, RawOrigin}, pallet_prelude::*, - traits::nonfungibles_v2::Inspect as NonFungiblesInspect, + traits::nonfungibles_v2::Inspect, }; use pallet_contracts::chain_extension::{ BufInBufOutState, ChainExtension, ChargedAmount, Environment, Ext, InitState, RetVal, @@ -86,7 +86,6 @@ impl TryFrom for v0::FuncId { 0x1 => Self::ReadState, _ => { log::error!("called an unregistered `func_id`: {:}", func_id); - // TODO: Other error. return Err(DispatchError::Other("unimplemented func_id")); }, }; @@ -207,8 +206,6 @@ where RuntimeStateKeys::ParachainSystem(key) => { read_parachain_system_state::(key, &mut env) }, - // TODO: devnet / testnet feature. - _ => Err(DispatchError::Other("Unknown state keys")), }? .encode(); @@ -218,8 +215,7 @@ where ); env.write(&result, false, None).map_err(|e| { log::trace!(target: LOG_TARGET, "{:?}", e); - // TODO: Other error. - DispatchError::Other("Unable to write results to contract memory") + DispatchError::Other("unable to write results to contract memory") }) } @@ -485,7 +481,7 @@ mod tests { log::debug!("result: {:?}", result); } - // Check for revert. + // check for revert assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); assert_eq!(Nfts::owner(collection_id, item_id), Some(BOB.into())); @@ -550,7 +546,7 @@ mod tests { log::debug!("result: {:?}", result); } - // Check for revert with expected error. + // check for revert with expected error let result = result.result.unwrap(); assert!(result.did_revert()); }); @@ -610,7 +606,7 @@ mod tests { log::debug!("result: {:?}", result); } - // Check for revert. + // check for revert assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); }); } @@ -673,7 +669,7 @@ mod tests { log::debug!("result: {:?}", result); } - // Check for revert. + // check for revert assert!( result.result.is_err(), "Contract execution should have failed - unimplemented runtime call!" @@ -735,7 +731,7 @@ mod tests { log::debug!("filtered result: {:?}", result); } - // Check for revert. + // check for revert assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); }); } From 972e4c9e1519279ad351d4d900c65e3a375a6d29 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Fri, 5 Jul 2024 19:13:57 +0200 Subject: [PATCH 17/76] feat: add more interfaces --- pop-api/examples/fungibles/lib.rs | 38 +- pop-api/integration-tests/Cargo.toml | 2 + pop-api/integration-tests/src/lib.rs | 9 +- .../integration-tests/src/local_fungibles.rs | 440 ++++++++++++++---- pop-api/src/v0/assets/fungibles.rs | 19 +- pop-api/src/v0/assets/mod.rs | 140 ++++-- runtime/devnet/src/config/mod.rs | 2 +- runtime/devnet/src/extensions/mod.rs | 45 +- runtime/devnet/src/extensions/v0/assets.rs | 42 +- 9 files changed, 554 insertions(+), 183 deletions(-) diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs index 03841b74..2abc422c 100755 --- a/pop-api/examples/fungibles/lib.rs +++ b/pop-api/examples/fungibles/lib.rs @@ -45,19 +45,23 @@ mod fungibles { } #[ink(message)] - pub fn balance_of(&self, id: AssetId, owner: AccountId) -> Balance { + pub fn balance_of(&self, id: AssetId, owner: AccountId) -> Result { api::balance_of(id, owner) } #[ink(message)] - pub fn allowance(&self, id: AssetId, owner: AccountId, spender: AccountId) -> Balance { + pub fn allowance( + &self, + id: AssetId, + owner: AccountId, + spender: AccountId, + ) -> Result { api::allowance(id, owner, spender) } #[ink(message)] pub fn transfer(&self, id: AssetId, to: AccountId, value: Balance) -> Result<()> { - api::transfer(id, to, value)?; - Ok(()) + api::transfer(id, to, value) } #[ink(message)] @@ -70,14 +74,12 @@ mod fungibles { // In the standard a `[u8]`, but the size needs to be known at compile time. _data: Vec, ) -> Result<()> { - api::transfer_from(id, from, to, value)?; - Ok(()) + api::transfer_from(id, from, to, value) } #[ink(message)] pub fn approve(&self, id: AssetId, spender: AccountId, value: Balance) -> Result<()> { - api::approve(id, spender, value)?; - Ok(()) + api::approve(id, spender, value) } #[ink(message)] @@ -87,8 +89,7 @@ mod fungibles { spender: AccountId, value: Balance, ) -> Result<()> { - api::increase_allowance(id, spender, value)?; - Ok(()) + api::increase_allowance(id, spender, value) } #[ink(message)] @@ -98,27 +99,26 @@ mod fungibles { spender: AccountId, value: Balance, ) -> Result<()> { - api::decrease_allowance(id, spender, value)?; - Ok(()) + api::decrease_allowance(id, spender, value) } - // 2. PSP-22 Metadata Interface: - // - token_name - // - token_symbol - // - token_decimals + /// 2. PSP-22 Metadata Interface: + /// - token_name + /// - token_symbol + /// - token_decimals #[ink(message)] - pub fn token_name(&self, id: AssetId) -> Vec { + pub fn token_name(&self, id: AssetId) -> Result> { api::token_name(id) } #[ink(message)] - pub fn token_symbol(&self, id: AssetId) -> Vec { + pub fn token_symbol(&self, id: AssetId) -> Result> { api::token_symbol(id) } #[ink(message)] - pub fn token_decimals(&self, id: AssetId) -> u8 { + pub fn token_decimals(&self, id: AssetId) -> Result { api::token_decimals(id) } diff --git a/pop-api/integration-tests/Cargo.toml b/pop-api/integration-tests/Cargo.toml index c855a884..94c0ba83 100644 --- a/pop-api/integration-tests/Cargo.toml +++ b/pop-api/integration-tests/Cargo.toml @@ -9,6 +9,7 @@ scale = { package = "parity-scale-codec", version = "3.0.0", default-features = frame-support = { version = "29.0.0", default-features = false } frame-system = { version = "29.0.0", default-features = false } pallet-balances = { version = "29.0.2", default-features = false } +pallet-assets = { version = "30.0.0", default-features = false } pallet-contracts = { version = "28.0.0", default-features = false } pop-primitives = { path = "../../primitives", default-features = false, features = ["assets"] } pop-runtime-devnet = { path = "../../runtime/devnet", default-features = false } @@ -22,6 +23,7 @@ std = [ "frame-support/std", "frame-system/std", "pallet-balances/std", + "pallet-assets/std", "pallet-contracts/std", "pop-primitives/std", "pop-runtime-devnet/std", diff --git a/pop-api/integration-tests/src/lib.rs b/pop-api/integration-tests/src/lib.rs index 624c3000..c6732d91 100644 --- a/pop-api/integration-tests/src/lib.rs +++ b/pop-api/integration-tests/src/lib.rs @@ -1,14 +1,19 @@ #![cfg(test)] use frame_support::{ - traits::fungibles::{approvals::Inspect as ApprovalInspect, Inspect}, + traits::fungibles::{ + approvals::Inspect as ApprovalInspect, metadata::Inspect as MetadataInspect, Inspect, + }, weights::Weight, }; use pallet_contracts::{Code, CollectEvents, Determinism, ExecReturnValue}; use scale::{Decode, Encode}; use sp_runtime::{traits::Hash, AccountId32, BuildStorage, DispatchError}; -use pop_runtime_devnet::{Assets, Contracts, Runtime, RuntimeOrigin, System, UNIT}; +use pop_runtime_devnet::{ + config::assets::TrustBackedAssetsInstance, Assets, Contracts, Runtime, RuntimeOrigin, System, + UNIT, +}; mod local_fungibles; diff --git a/pop-api/integration-tests/src/local_fungibles.rs b/pop-api/integration-tests/src/local_fungibles.rs index 968c43eb..d0229c84 100644 --- a/pop-api/integration-tests/src/local_fungibles.rs +++ b/pop-api/integration-tests/src/local_fungibles.rs @@ -10,10 +10,27 @@ const ASSET_ID: AssetId = 1; fn decoded(result: ExecReturnValue) -> T { match ::decode(&mut &result.data[2..]) { Ok(value) => value, - Err(_) => panic!("\nTest failed by trying to decode result: {:?} into `T`\n", result), + Err(_) => panic!("\nTest failed by trying to decode `{:?}` into `T`\n", result), } } +// Call total_supply contract message. +fn total_supply(addr: AccountId32, asset_id: AssetId) -> Balance { + let function = function_selector("total_supply"); + let params = [function, asset_id.encode()].concat(); + let result = do_bare_call(addr, params, 0).expect("should work"); + decoded::(result) +} + +// Call balance_of contract message. +fn balance_of(addr: AccountId32, asset_id: AssetId, owner: AccountId32) -> Balance { + let function = function_selector("balance_of"); + let params = [function, asset_id.encode(), owner.encode()].concat(); + let result = do_bare_call(addr, params, 0).expect("should work"); + decoded::(result) +} + +// Call allowance contract message. fn allowance( addr: AccountId32, asset_id: AssetId, @@ -26,20 +43,28 @@ fn allowance( decoded::(result) } -// Call balance_of contract message. -fn balance_of(addr: AccountId32, asset_id: AssetId, owner: AccountId32) -> Balance { - let function = function_selector("balance_of"); - let params = [function, asset_id.encode(), owner.encode()].concat(); +// Call token_name contract message. +fn token_name(addr: AccountId32, asset_id: AssetId) -> Vec { + let function = function_selector("token_name"); + let params = [function, asset_id.encode()].concat(); let result = do_bare_call(addr, params, 0).expect("should work"); - decoded::(result) + decoded::>(result) } -// Call total_supply contract message. -fn total_supply(addr: AccountId32, asset_id: AssetId) -> Balance { - let function = function_selector("total_supply"); +// Call token_symbol contract message. +fn token_symbol(addr: AccountId32, asset_id: AssetId) -> Vec { + let function = function_selector("token_symbol"); let params = [function, asset_id.encode()].concat(); let result = do_bare_call(addr, params, 0).expect("should work"); - decoded::(result) + decoded::>(result) +} + +// Call token_decimals contract message. +fn token_decimals(addr: AccountId32, asset_id: AssetId) -> u8 { + let function = function_selector("token_decimals"); + let params = [function, asset_id.encode()].concat(); + let result = do_bare_call(addr, params, 0).expect("should work"); + decoded::(result) } fn asset_exists(addr: AccountId32, asset_id: AssetId) -> bool { @@ -82,7 +107,6 @@ fn transfer( let function = function_selector("transfer"); let params = [function, asset_id.encode(), to.encode(), value.encode()].concat(); let result = do_bare_call(addr, params, 0).expect("should work"); - println!("Transfer result: {:?}", result); result } @@ -101,6 +125,18 @@ fn transfer_from( do_bare_call(addr, params, 0).expect("should work") } +fn increase_allowance( + addr: AccountId32, + asset_id: AssetId, + spender: AccountId32, + value: Balance, +) -> ExecReturnValue { + let function = function_selector("increase_allowance"); + let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat(); + let result = do_bare_call(addr, params, 0).expect("should work"); + result +} + fn create_asset(owner: AccountId32, asset_id: AssetId, min_balance: Balance) -> AssetId { assert_eq!( Assets::create( @@ -154,20 +190,88 @@ fn create_asset_mint_and_approve( } // Freeze an asset. -fn freeze_asset(asset_id: AssetId, owner: AccountId32) { +fn freeze_asset(owner: AccountId32, asset_id: AssetId) { assert_eq!(Assets::freeze_asset(RuntimeOrigin::signed(owner.into()), asset_id.into()), Ok(())); } // Thaw an asset. -fn thaw_asset(asset_id: AssetId, owner: AccountId32) { +fn thaw_asset(owner: AccountId32, asset_id: AssetId) { assert_eq!(Assets::thaw_asset(RuntimeOrigin::signed(owner.into()), asset_id.into()), Ok(())); } // Start destroying an asset. -fn start_destroy_asset(asset_id: AssetId, owner: AccountId32) { +fn start_destroy_asset(owner: AccountId32, asset_id: AssetId) { assert_eq!(Assets::start_destroy(RuntimeOrigin::signed(owner.into()), asset_id.into()), Ok(())); } +// Create an asset and set metadata. +fn create_asset_and_set_metadata( + owner: AccountId32, + asset_id: AssetId, + name: Vec, + symbol: Vec, + decimals: u8, +) { + assert_eq!( + Assets::create( + RuntimeOrigin::signed(owner.clone()), + asset_id.into(), + owner.clone().into(), + 100 + ), + Ok(()) + ); + set_metadata_asset(owner, asset_id, name, symbol, decimals); +} + +// Set metadata of an asset. +fn set_metadata_asset( + owner: AccountId32, + asset_id: AssetId, + name: Vec, + symbol: Vec, + decimals: u8, +) { + assert_eq!( + Assets::set_metadata( + RuntimeOrigin::signed(owner.into()), + asset_id.into(), + name, + symbol, + decimals + ), + Ok(()) + ); +} + +fn token_name_asset(asset_id: AssetId) -> Vec { + as MetadataInspect>::name( + asset_id, + ) +} + +fn token_symbol_asset(asset_id: AssetId) -> Vec { + as MetadataInspect>::symbol( + asset_id, + ) +} + +fn token_decimals_asset(asset_id: AssetId) -> u8 { + as MetadataInspect>::decimals( + asset_id, + ) +} + +/// 1. PSP-22 Interface: +/// - total_supply +/// - balance_of +/// - allowance +/// - transfer +/// - transfer_from +/// - approve +/// - increase_allowance +/// - decrease_allowance + #[test] #[ignore] fn total_supply_works() { @@ -181,10 +285,12 @@ fn total_supply_works() { // No tokens in circulation. assert_eq!(Assets::total_supply(ASSET_ID), total_supply(addr.clone(), ASSET_ID)); + assert_eq!(0, total_supply(addr.clone(), ASSET_ID)); // Tokens in circulation. create_asset_and_mint_to(addr.clone(), ASSET_ID, BOB, 100); - assert_eq!(Assets::total_supply(ASSET_ID), total_supply(addr, ASSET_ID)); + assert_eq!(Assets::total_supply(ASSET_ID), total_supply(addr.clone(), ASSET_ID)); + assert_eq!(100, total_supply(addr, ASSET_ID)); }); } @@ -198,10 +304,12 @@ fn balance_of_works() { // No tokens in circulation. assert_eq!(Assets::balance(ASSET_ID, BOB), balance_of(addr.clone(), ASSET_ID, BOB)); + assert_eq!(0, balance_of(addr.clone(), ASSET_ID, BOB)); // Tokens in circulation. create_asset_and_mint_to(addr.clone(), ASSET_ID, BOB, 100); - assert_eq!(Assets::balance(ASSET_ID, BOB), balance_of(addr, ASSET_ID, BOB)); + assert_eq!(Assets::balance(ASSET_ID, BOB), balance_of(addr.clone(), ASSET_ID, BOB)); + assert_eq!(100, balance_of(addr, ASSET_ID, BOB)); }); } @@ -218,90 +326,240 @@ fn allowance_works() { Assets::allowance(ASSET_ID, &BOB, &ALICE), allowance(addr.clone(), ASSET_ID, BOB, ALICE) ); + assert_eq!(0, allowance(addr.clone(), ASSET_ID, BOB, ALICE)); // Tokens in circulation. create_asset_mint_and_approve(addr.clone(), ASSET_ID, BOB, 100, ALICE, 50); assert_eq!( Assets::allowance(ASSET_ID, &BOB, &ALICE), - allowance(addr, ASSET_ID, BOB, ALICE) + allowance(addr.clone(), ASSET_ID, BOB, ALICE) ); + assert_eq!(50, allowance(addr, ASSET_ID, BOB, ALICE)); }); } #[test] #[ignore] -fn asset_exists_works() { +fn transfer_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); + let amount: Balance = 100 * UNIT; - // No tokens in circulation. - assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr.clone(), ASSET_ID)); - - // Tokens in circulation. - create_asset(addr.clone(), ASSET_ID, 1); - assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr, ASSET_ID)); + // Asset does not exist. + assert_eq!( + decoded::(transfer(addr.clone(), 1, BOB, amount,)), + Module { index: 52, error: 3 }, + ); + // Create asset with Alice as owner and mint `amount` to contract address. + let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(ALICE, asset); + assert_eq!( + decoded::(transfer(addr.clone(), asset, BOB, amount,)), + Module { index: 52, error: 16 }, + ); + thaw_asset(ALICE, asset); + // Not enough balance. + assert_eq!( + decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), + Module { index: 52, error: 0 }, + ); + // Not enough balance due to ED. + assert_eq!( + decoded::(transfer(addr.clone(), asset, BOB, amount)), + Module { index: 52, error: 0 }, + ); + // Successful transfer. + let bob_balance_before_mint = Assets::balance(asset, &BOB); + let result = transfer(addr.clone(), asset, BOB, amount / 2); + assert!(!result.did_revert(), "Contract reverted!"); + let bob_balance_after_mint = Assets::balance(asset, &BOB); + assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount / 2); + // Transfer asset to account that does not exist. + assert_eq!( + decoded::(transfer(addr.clone(), asset, FERDIE, amount / 4)), + Token(CannotCreate) + ); + // Asset is not live, i.e. frozen or being destroyed. + start_destroy_asset(ALICE, asset); + assert_eq!( + decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), + Module { index: 52, error: 16 }, + ); }); } #[test] #[ignore] -fn create_works() { +fn increase_allowance_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let new_asset = 2; - // Instantiate a contract without balance (relay token). - let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 0, vec![0]); - // No balance to pay for fees. + let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 0, vec![]); + let amount: Balance = 100 * UNIT; + let asset = 0; + create_asset_and_mint_to(ALICE, asset, addr.clone(), amount); assert_eq!( - decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), - Module { index: 10, error: 2 }, + decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), + ConsumerRemaining ); - // Instantiate a contract without balance (relay token). - let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 100, vec![2]); - // TODO: make sure it has enough for the fees but not for the deposit. - // No balance to pay fe deposit. + + let addr = + instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![1]); + // Asset does not exist. + let asset = 1; assert_eq!( - decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), - Module { index: 10, error: 2 }, + decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), + Module { index: 52, error: 3 }, ); - // Instantiate a contract with balance. + // Create asset with Alice as owner and mint `amount` to contract address. + create_asset_and_mint_to(ALICE, asset, addr.clone(), amount); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(ALICE, asset); + assert_eq!( + decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), + Module { index: 52, error: 16 }, + ); + thaw_asset(ALICE, asset); + // Successful approval. + assert_eq!(0, Assets::allowance(asset, &addr, &BOB)); + assert!( + !increase_allowance(addr.clone(), asset, BOB, amount).did_revert(), + "Contract reverted!" + ); + assert_eq!(Assets::allowance(asset, &addr, &BOB), amount); + // Additive. + assert!( + !increase_allowance(addr.clone(), asset, BOB, amount).did_revert(), + "Contract reverted!" + ); + assert_eq!(Assets::allowance(asset, &addr, &BOB), amount * 2); + // Asset is not live, i.e. frozen or being destroyed. + start_destroy_asset(ALICE, asset); + assert_eq!( + decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), + Module { index: 52, error: 16 }, + ); + }); +} + +#[test] +#[ignore] +fn transfer_from_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); let addr = - instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![1]); - create_asset(ALICE, ASSET_ID, 1); - // Asset ID is already taken. + instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); + let amount: Balance = 100 * UNIT; + + // Asset does not exist. assert_eq!( - decoded::(create(addr.clone(), ASSET_ID, BOB, 1)), - Module { index: 52, error: 5 }, + decoded::(transfer(addr.clone(), 1, BOB, amount,)), + Module { index: 52, error: 3 }, ); - // The minimal balance for an asset must be non zero. + // Create asset with Alice as owner and mint `amount` to contract address. + let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(ALICE, asset); assert_eq!( - decoded::(create(addr.clone(), new_asset, BOB, 0)), - Module { index: 52, error: 7 }, + decoded::(transfer(addr.clone(), asset, BOB, amount,)), + Module { index: 52, error: 16 }, ); - let result = create(addr.clone(), new_asset, BOB, 1); + thaw_asset(ALICE, asset); + // Not enough balance. + assert_eq!( + decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), + Module { index: 52, error: 0 }, + ); + // Not enough balance due to ED. + assert_eq!( + decoded::(transfer(addr.clone(), asset, BOB, amount)), + Module { index: 52, error: 0 }, + ); + // Successful transfer. + let bob_balance_before_mint = Assets::balance(asset, &BOB); + let result = transfer(addr.clone(), asset, BOB, amount / 2); assert!(!result.did_revert(), "Contract reverted!"); + let bob_balance_after_mint = Assets::balance(asset, &BOB); + assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount / 2); + // Transfer asset to account that does not exist. + assert_eq!( + decoded::(transfer(addr.clone(), asset, FERDIE, amount / 4)), + Token(CannotCreate) + ); + // Asset is not live, i.e. frozen or being destroyed. + start_destroy_asset(ALICE, asset); + assert_eq!( + decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), + Module { index: 52, error: 16 }, + ); }); } +/// 2. PSP-22 Metadata Interface: +/// - token_name +/// - token_symbol +/// - token_decimals + #[test] #[ignore] -fn set_metadata_works() { +fn token_metadata_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate( + "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", + INIT_VALUE, + vec![], + ); + + let name: Vec = vec![11, 12, 13]; + let symbol: Vec = vec![21, 22, 23]; + let decimals: u8 = 69; + // Token does not exist. + assert_eq!(token_name_asset(ASSET_ID), token_name(addr.clone(), ASSET_ID)); + assert_eq!(Vec::::new(), token_name(addr.clone(), ASSET_ID)); + assert_eq!(token_symbol_asset(ASSET_ID), token_symbol(addr.clone(), ASSET_ID)); + assert_eq!(Vec::::new(), token_symbol(addr.clone(), ASSET_ID)); + assert_eq!(token_decimals_asset(ASSET_ID), token_decimals(addr.clone(), ASSET_ID)); + assert_eq!(0, token_decimals(addr.clone(), ASSET_ID)); + + create_asset_and_set_metadata( + addr.clone(), + ASSET_ID, + name.clone(), + symbol.clone(), + decimals, + ); + assert_eq!(token_name_asset(ASSET_ID), token_name(addr.clone(), ASSET_ID)); + assert_eq!(name, token_name(addr.clone(), ASSET_ID)); + assert_eq!(token_symbol_asset(ASSET_ID), token_symbol(addr.clone(), ASSET_ID)); + assert_eq!(symbol, token_symbol(addr.clone(), ASSET_ID)); + assert_eq!(token_decimals_asset(ASSET_ID), token_decimals(addr.clone(), ASSET_ID)); + assert_eq!(decimals, token_decimals(addr.clone(), ASSET_ID)); + }); +} + +#[test] +#[ignore] +fn asset_exists_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); + // No tokens in circulation. + assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr.clone(), ASSET_ID)); + + // Tokens in circulation. create_asset(addr.clone(), ASSET_ID, 1); - let result = set_metadata(addr.clone(), ASSET_ID, vec![12], vec![12], 12); - assert!(!result.did_revert(), "Contract reverted!"); + assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr, ASSET_ID)); }); } #[test] #[ignore] -fn transfer_from_mint_works() { +fn mint_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); let addr = @@ -326,12 +584,12 @@ fn transfer_from_mint_works() { ); let asset = create_asset(addr.clone(), 2, 2); // Asset is not live, i.e. frozen or being destroyed. - freeze_asset(asset, addr.clone()); + freeze_asset(addr.clone(), asset); assert_eq!( decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), Module { index: 52, error: 16 }, ); - thaw_asset(asset, addr.clone()); + thaw_asset(addr.clone(), asset); // Successful mint. let bob_balance_before_mint = Assets::balance(asset, &BOB); let result = transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8]); @@ -351,7 +609,7 @@ fn transfer_from_mint_works() { Arithmetic(Overflow) ); // Asset is not live, i.e. frozen or being destroyed. - start_destroy_asset(asset, addr.clone()); + start_destroy_asset(addr.clone(), asset); assert_eq!( decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), Module { index: 52, error: 16 }, @@ -361,53 +619,53 @@ fn transfer_from_mint_works() { #[test] #[ignore] -fn transfer_works() { +fn create_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = - instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); - let amount: Balance = 100 * UNIT; - - // Asset does not exist. + // Instantiate a contract without balance (relay token). + let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 0, vec![0]); + // No balance to pay for fees. assert_eq!( - decoded::(transfer(addr.clone(), 1, BOB, amount,)), - Module { index: 52, error: 3 }, + decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), + Module { index: 10, error: 2 }, ); - // Create asset with Alice as owner and mint `amount` to contract address. - let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); - // Asset is not live, i.e. frozen or being destroyed. - freeze_asset(asset, ALICE); + // Instantiate a contract without balance (relay token). + let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 100, vec![2]); + // No balance to pay the deposit. assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount,)), - Module { index: 52, error: 16 }, + decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), + Module { index: 10, error: 2 }, ); - thaw_asset(asset, ALICE); - // Not enough balance. + // Instantiate a contract with balance. + let addr = + instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![1]); assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), - Module { index: 52, error: 0 }, + decoded::(create(addr.clone(), ASSET_ID, BOB, 0)), + Module { index: 52, error: 7 }, ); - // Not enough balance due to ED. + create_asset(ALICE, ASSET_ID, 1); + // Asset ID is already taken. assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount)), - Module { index: 52, error: 0 }, + decoded::(create(addr.clone(), ASSET_ID, BOB, 1)), + Module { index: 52, error: 5 }, ); - // Successful transfer. - let bob_balance_before_mint = Assets::balance(asset, &BOB); - let result = transfer(addr.clone(), asset, BOB, amount / 2); + // The minimal balance for an asset must be non zero. + let new_asset = 2; + let result = create(addr.clone(), new_asset, BOB, 1); + assert!(!result.did_revert(), "Contract reverted!"); + }); +} + +#[test] +#[ignore] +fn set_metadata_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = + instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); + + create_asset(addr.clone(), ASSET_ID, 1); + let result = set_metadata(addr.clone(), ASSET_ID, vec![12], vec![12], 12); assert!(!result.did_revert(), "Contract reverted!"); - let bob_balance_after_mint = Assets::balance(asset, &BOB); - assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount / 2); - // Transfer asset to account that does not exist. - assert_eq!( - decoded::(transfer(addr.clone(), asset, FERDIE, amount / 4)), - Token(CannotCreate) - ); - // Asset is not live, i.e. frozen or being destroyed. - start_destroy_asset(asset, ALICE); - assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), - Module { index: 52, error: 16 }, - ); }); } diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index 8d0de768..5325cad3 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -39,7 +39,7 @@ pub fn total_supply(id: AssetId) -> Result { /// # Returns /// The balance of the specified account, or an error if the operation fails. #[inline] -pub fn balance_of(id: AssetId, owner: AccountId) -> Balance { +pub fn balance_of(id: AssetId, owner: AccountId) -> Result { assets::balance_of(id, owner) } @@ -54,7 +54,7 @@ pub fn balance_of(id: AssetId, owner: AccountId) -> Balance { /// # Returns /// The remaining allowance, or an error if the operation fails. #[inline] -pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Balance { +pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { assets::allowance(id, owner, spender) } @@ -101,6 +101,7 @@ pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, value: Balance /// Returns `Ok(())` if successful, or an error if the approval fails. #[inline] pub fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { + assets::cancel_approval(id, spender)?; assets::approve_transfer(id, spender, value) } @@ -128,9 +129,11 @@ pub fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Re /// # Returns /// Returns `Ok(())` if successful, or an error if the operation fails. #[inline] -pub fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { - assets::cancel_approval(id, spender.clone())?; - assets::approve_transfer(id, spender, value) +pub fn decrease_allowance(_id: AssetId, _spender: AccountId, _value: Balance) -> Result<()> { + // let allowance = assets::allowance(id, owner, spender)?; + // assets::cancel_approval(id, spender.clone())?; + // assets::approve_transfer(id, spender, value) + Ok(()) } /// 2. PSP-22 Metadata Interface: @@ -146,7 +149,7 @@ pub fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Re /// # Returns /// The name of the token as a byte vector, or an error if the operation fails. #[inline] -pub fn token_name(id: AssetId) -> Vec { +pub fn token_name(id: AssetId) -> Result> { assets::token_name(id) } @@ -158,7 +161,7 @@ pub fn token_name(id: AssetId) -> Vec { /// # Returns /// The symbol of the token as a byte vector, or an error if the operation fails. #[inline] -pub fn token_symbol(id: AssetId) -> Vec { +pub fn token_symbol(id: AssetId) -> Result> { assets::token_symbol(id) } @@ -170,7 +173,7 @@ pub fn token_symbol(id: AssetId) -> Vec { /// # Returns /// The number of decimals of the token as a byte vector, or an error if the operation fails. #[inline] -pub fn token_decimals(id: AssetId) -> u8 { +pub fn token_decimals(id: AssetId) -> Result { assets::token_decimals(id) } diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index 22657323..d14c3322 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -26,8 +26,11 @@ const TRANSFER_KEEP_ALIVE: u8 = 9; /// - set_metadata /// - clear_metadata /// - approve_transfer +const APPROVE_TRANSFER: u8 = 22; /// - cancel_approval +const CANCEL_APPROVAL: u8 = 23; /// - transfer_approved +const TRANSFER_APPROVED: u8 = 25; /// Issue a new class of fungible assets from a public origin. // pub(crate) fn create( @@ -91,7 +94,7 @@ pub fn transfer(id: AssetId, target: AccountId, amount: Balance) -> Result<()> { VERSION, DISPATCH, ASSETS_MODULE, - // TODO: E.D. is always respected with transferring tokens via the API. + // E.D. is always respected with transferring tokens via the API. TRANSFER_KEEP_ALIVE, ])) .input::<(AssetId, AccountId, Balance)>() @@ -131,21 +134,31 @@ pub fn transfer(id: AssetId, target: AccountId, amount: Balance) -> Result<()> { /// Approve an amount of asset for transfer by a delegated third-party account. #[inline] pub fn approve_transfer(id: AssetId, delegate: AccountId, amount: Balance) -> Result<()> { - ChainExtensionMethod::build(u32::from_le_bytes([0u8, 0, 52, 69])) - .input::<(AssetId, AccountId, Balance)>() - .output::, true>() - .handle_error_code::() - .call(&(id, delegate, amount)) + ChainExtensionMethod::build(u32::from_le_bytes([ + VERSION, + DISPATCH, + ASSETS_MODULE, + APPROVE_TRANSFER, + ])) + .input::<(AssetId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, delegate, amount)) } /// Cancel all of some asset approved for delegated transfer by a third-party account. #[inline] pub fn cancel_approval(id: AssetId, delegate: AccountId) -> Result<()> { - ChainExtensionMethod::build(0) - .input::<(AssetId, AccountId)>() - .output::, true>() - .handle_error_code::() - .call(&(id, delegate)) + ChainExtensionMethod::build(u32::from_le_bytes([ + VERSION, + DISPATCH, + ASSETS_MODULE, + CANCEL_APPROVAL, + ])) + .input::<(AssetId, AccountId)>() + .output::, true>() + .handle_error_code::() + .call(&(id, delegate)) } /// Transfer some asset balance from a previously delegated account to some third-party @@ -157,23 +170,33 @@ pub fn transfer_approved( to: AccountId, amount: Balance, ) -> Result<()> { - ChainExtensionMethod::build(0) - .input::<(AssetId, AccountId, AccountId, Balance)>() - .output::, true>() - .handle_error_code::() - .call(&(id, from, to, amount)) + ChainExtensionMethod::build(u32::from_le_bytes([ + VERSION, + DISPATCH, + ASSETS_MODULE, + TRANSFER_APPROVED, + ])) + .input::<(AssetId, AccountId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, from, to, amount)) } -/// 2. Read state functions +/// 2. Read state functions: const READ_STATE: u8 = 1; /// - total_supply const TOTAL_SUPPLY: u8 = 0; /// - balance_of +const BALANCE_OF: u8 = 1; /// - allowance -/// - asset_exists +const ALLOWANCE: u8 = 2; /// - token_name +const TOKEN_NAME: u8 = 3; /// - token_symbol +const TOKEN_SYMBOL: u8 = 4; /// - token_decimals +const TOKEN_DECIMALS: u8 = 5; +/// - asset_exists #[inline] pub fn total_supply(id: AssetId) -> Result { @@ -191,48 +214,73 @@ pub fn total_supply(id: AssetId) -> Result { } #[inline] -pub fn balance_of(id: AssetId, owner: AccountId) -> Balance { - ChainExtensionMethod::build(1) - .input::<(AssetId, AccountId)>() - .output::() - .ignore_error_code() - .call(&(id, owner)) +pub fn balance_of(id: AssetId, owner: AccountId) -> Result { + ChainExtensionMethod::build(u32::from_le_bytes([ + VERSION, + READ_STATE, + ASSETS_MODULE, + BALANCE_OF, + ])) + .input::<(AssetId, AccountId)>() + .output::>, true>() + .handle_error_code::() + .call(&(id, owner)) + .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) } #[inline] -pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Balance { - ChainExtensionMethod::build(1) +pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { + ChainExtensionMethod::build(u32::from_le_bytes([VERSION, READ_STATE, ASSETS_MODULE, ALLOWANCE])) .input::<(AssetId, AccountId, AccountId)>() - .output::() - .ignore_error_code() + .output::>, true>() + .handle_error_code::() .call(&(id, owner, spender)) + .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) } #[inline] -pub fn token_name(id: AssetId) -> Vec { - ChainExtensionMethod::build(1) - .input::() - .output::, false>() - .ignore_error_code() - .call(&(id)) +pub fn token_name(id: AssetId) -> Result> { + ChainExtensionMethod::build(u32::from_le_bytes([ + VERSION, + READ_STATE, + ASSETS_MODULE, + TOKEN_NAME, + ])) + .input::() + .output::>, true>() + .handle_error_code::() + .call(&(id)) + .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) } // #[inline] -pub fn token_symbol(id: AssetId) -> Vec { - ChainExtensionMethod::build(1) - .input::() - .output::, false>() - .ignore_error_code() - .call(&(id)) +pub fn token_symbol(id: AssetId) -> Result> { + ChainExtensionMethod::build(u32::from_le_bytes([ + VERSION, + READ_STATE, + ASSETS_MODULE, + TOKEN_SYMBOL, + ])) + .input::() + .output::>, true>() + .handle_error_code::() + .call(&(id)) + .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) } #[inline] -pub fn token_decimals(id: AssetId) -> u8 { - ChainExtensionMethod::build(1) - .input::() - .output::() - .ignore_error_code() - .call(&(id)) +pub fn token_decimals(id: AssetId) -> Result { + ChainExtensionMethod::build(u32::from_le_bytes([ + VERSION, + READ_STATE, + ASSETS_MODULE, + TOKEN_DECIMALS, + ])) + .input::() + .output::>, true>() + .handle_error_code::() + .call(&(id)) + .and_then(|v| ::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) } // pub(crate) fn asset_exists(id: AssetId) -> Result { diff --git a/runtime/devnet/src/config/mod.rs b/runtime/devnet/src/config/mod.rs index c370bb1d..e0aaa3a1 100644 --- a/runtime/devnet/src/config/mod.rs +++ b/runtime/devnet/src/config/mod.rs @@ -1,4 +1,4 @@ -pub(crate) mod assets; +pub mod assets; mod contracts; mod proxy; // Public due to integration tests crate. diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs index ac52d5bd..8f0a6eda 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions/mod.rs @@ -5,7 +5,7 @@ use frame_support::{ dispatch::{GetDispatchInfo, RawOrigin}, pallet_prelude::*, traits::{ - fungibles::approvals::Inspect as ApprovalInspect, + fungibles::{approvals::Inspect as ApprovalInspect, metadata::Inspect as MetadataInspect}, nonfungibles_v2::Inspect as NonFungiblesInspect, }, }; @@ -129,8 +129,7 @@ where E: Ext, { const LOG_PREFIX: &str = " dispatch |"; - let call = construct_call(version, pallet_index, call_index, params) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; + let call = construct_call(version, pallet_index, call_index, params)?; // Contract is the origin by default. let origin: RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); dispatch_call::(env, call, origin, LOG_PREFIX) @@ -434,6 +433,15 @@ where T: frame_system::Config, { match key { + TotalSupply(id) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_assets::Pallet::::total_supply(id).encode()) + }, + BalanceOf(id, owner) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok(pallet_assets::Pallet::::balance(id, &owner.0.into()) + .encode()) + }, Allowance(id, owner, spender) => { env.charge_weight(T::DbWeight::get().reads(1_u64))?; Ok(pallet_assets::Pallet::::allowance( @@ -443,20 +451,31 @@ where ) .encode()) }, - // AssetsKeys::AssetExists(id) => { - // env.charge_weight(T::DbWeight::get().reads(1_u64))?; - // Ok(pallet_assets::Pallet::::asset_exists(id).encode()) - // }, - BalanceOf(id, owner) => { + TokenName(id) => { env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_assets::Pallet::::balance(id, &owner.0.into()) - .encode()) + Ok( as MetadataInspect< + AccountId, + >>::name(id) + .encode()) }, - TotalSupply(id) => { + TokenSymbol(id) => { env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_assets::Pallet::::total_supply(id).encode()) + Ok( as MetadataInspect< + AccountId, + >>::symbol(id) + .encode()) }, - _ => todo!(), + TokenDecimals(id) => { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + Ok( as MetadataInspect< + AccountId, + >>::decimals(id) + .encode()) + }, + // AssetsKeys::AssetExists(id) => { + // env.charge_weight(T::DbWeight::get().reads(1_u64))?; + // Ok(pallet_assets::Pallet::::asset_exists(id).encode()) + // }, } } diff --git a/runtime/devnet/src/extensions/v0/assets.rs b/runtime/devnet/src/extensions/v0/assets.rs index 912b116f..407b0d55 100644 --- a/runtime/devnet/src/extensions/v0/assets.rs +++ b/runtime/devnet/src/extensions/v0/assets.rs @@ -1,8 +1,9 @@ use crate::extensions::{ - AccountId, AssetId, - AssetsKeys::{self, TotalSupply}, + AccountId as AccountId32, AssetId, + AssetsKeys::{self, *}, Balance, Compact, Decode, DispatchError, MultiAddress, Runtime, TrustBackedAssetsInstance, }; +use pop_primitives::AccountId; pub(crate) fn construct_assets_key( call_index: u8, @@ -14,6 +15,31 @@ pub(crate) fn construct_assets_key( .map_err(|_| DispatchError::Other("DecodingFailed"))?; Ok(TotalSupply(id)) }, + 1 => { + let (id, owner) = <(AssetId, AccountId)>::decode(&mut ¶ms[..]) + .map_err(|_| DispatchError::Other("DecodingFailed"))?; + Ok(BalanceOf(id, owner)) + }, + 2 => { + let (id, owner, spender) = <(AssetId, AccountId, AccountId)>::decode(&mut ¶ms[..]) + .map_err(|_| DispatchError::Other("DecodingFailed"))?; + Ok(Allowance(id, owner, spender)) + }, + 3 => { + let id = ::decode(&mut ¶ms[..]) + .map_err(|_| DispatchError::Other("DecodingFailed"))?; + Ok(TokenName(id)) + }, + 4 => { + let id = ::decode(&mut ¶ms[..]) + .map_err(|_| DispatchError::Other("DecodingFailed"))?; + Ok(TokenSymbol(id)) + }, + 5 => { + let id = ::decode(&mut ¶ms[..]) + .map_err(|_| DispatchError::Other("DecodingFailed"))?; + Ok(TokenDecimals(id)) + }, // other calls _ => Err(DispatchError::Other("UnknownFunctionId")), } @@ -25,7 +51,7 @@ pub(crate) fn construct_assets_call( ) -> Result, DispatchError> { match call_index { 9 => { - let (id, target, amount) = <(AssetId, AccountId, Balance)>::decode(&mut ¶ms[..]) + let (id, target, amount) = <(AssetId, AccountId32, Balance)>::decode(&mut ¶ms[..]) .map_err(|_| DispatchError::Other("DecodingFailed"))?; Ok(pallet_assets::Call::::transfer_keep_alive { id: Compact(id), @@ -33,6 +59,16 @@ pub(crate) fn construct_assets_call( amount, }) }, + 22 => { + let (id, delegate, amount) = + <(AssetId, AccountId32, Balance)>::decode(&mut ¶ms[..]) + .map_err(|_| DispatchError::Other("DecodingFailed"))?; + Ok(pallet_assets::Call::::approve_transfer { + id: Compact(id), + delegate: MultiAddress::Id(delegate), + amount, + }) + }, // other calls _ => Err(DispatchError::Other("UnknownFunctionId")), } From 47e2ea6770d0b6fe8fe5b611570cdfa772ed4c04 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Fri, 5 Jul 2024 23:49:03 +0200 Subject: [PATCH 18/76] refactor: final bits --- pop-api/src/v0/assets/fungibles.rs | 10 +++++----- pop-api/src/v0/assets/mod.rs | 17 ++--------------- runtime/devnet/src/extensions/mod.rs | 1 + runtime/devnet/src/extensions/v0/assets.rs | 1 + runtime/testnet/src/extensions.rs | 1 + 5 files changed, 10 insertions(+), 20 deletions(-) diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index 5325cad3..4abd46a6 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -70,7 +70,7 @@ pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result Result<()> { - assets::transfer(id, to, value) + assets::transfer_keep_alive(id, to, value) } /// Transfers `value` tokens on behalf of `from` to account `to` with additional `data` @@ -279,7 +279,6 @@ pub fn token_decimals(id: AssetId) -> Result { // assets::asset_exists(id) // } -// TODO: further implement the rest of the interfaces and conclude on the FungiblesError. #[derive(Debug, PartialEq, Eq)] #[ink::scale_derive(Encode, Decode, TypeInfo)] pub enum FungiblesError { @@ -300,13 +299,14 @@ pub enum FungiblesError { NoPermission, /// The given asset ID is unknown. Unknown, - // - Originally `InsufficientBalance` for the deposit but this would result in the same error - // as the error when there is insufficient balance for transferring an asset. /// No balance for creation of assets or fees. + // + // Originally `pallet_balances::Error::InsufficientBalance` but collides with the + // `InsufficientBalance` error that is used for `pallet_assets::Error::BalanceLow` to adhere to + // standard. NoBalance, } -// TODO: include conversions from TokenError and add conversions based on added interfaces. impl From for FungiblesError { fn from(value: StatusCode) -> Self { let encoded = value.0.to_le_bytes(); diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index d14c3322..ea8991a7 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -87,9 +87,9 @@ const TRANSFER_APPROVED: u8 = 25; // })) // } -/// Move some assets from the sender account to another. +/// Move some assets from the sender account to another, keeping the sender account alive. #[inline] -pub fn transfer(id: AssetId, target: AccountId, amount: Balance) -> Result<()> { +pub fn transfer_keep_alive(id: AssetId, target: AccountId, amount: Balance) -> Result<()> { ChainExtensionMethod::build(u32::from_le_bytes([ VERSION, DISPATCH, @@ -103,19 +103,6 @@ pub fn transfer(id: AssetId, target: AccountId, amount: Balance) -> Result<()> { .call(&(id, target, amount)) } -// /// Move some assets from the sender account to another, keeping the sender account alive. -// pub(crate) fn transfer_keep_alive( -// id: AssetId, -// target: impl Into>, -// amount: Balance, -// ) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::TransferKeepAlive { -// id: id.into(), -// target: target.into(), -// amount: Compact(amount), -// })) -// } - // /// Set the metadata for an asset. // pub(crate) fn set_metadata( // id: AssetId, diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs index 8f0a6eda..aa58bcea 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions/mod.rs @@ -304,6 +304,7 @@ where // - `error`: The `DispatchError` encountered during contract execution. // - `version`: The version of the chain extension, used to determine the known errors. pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { + use sp_std::vec; // "UnknownFunctionId" and "DecodingFailed" are mapped to specific errors in the API and will // never change. let mut encoded_error = match error { diff --git a/runtime/devnet/src/extensions/v0/assets.rs b/runtime/devnet/src/extensions/v0/assets.rs index 407b0d55..c6b15b4e 100644 --- a/runtime/devnet/src/extensions/v0/assets.rs +++ b/runtime/devnet/src/extensions/v0/assets.rs @@ -4,6 +4,7 @@ use crate::extensions::{ Balance, Compact, Decode, DispatchError, MultiAddress, Runtime, TrustBackedAssetsInstance, }; use pop_primitives::AccountId; +use sp_std::vec::Vec; pub(crate) fn construct_assets_key( call_index: u8, diff --git a/runtime/testnet/src/extensions.rs b/runtime/testnet/src/extensions.rs index 4c224544..a5eeb3c3 100644 --- a/runtime/testnet/src/extensions.rs +++ b/runtime/testnet/src/extensions.rs @@ -206,6 +206,7 @@ where RuntimeStateKeys::ParachainSystem(key) => { read_parachain_system_state::(key, &mut env) }, + _ => Ok(vec![0]), }? .encode(); From 9dcfccbe414ef4de57d12e59db6841fcbed9eeee Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Sat, 6 Jul 2024 10:05:17 +0200 Subject: [PATCH 19/76] fix: compiling --- Cargo.lock | 46 +++++++++++++++++++--------- Cargo.toml | 14 ++++----- runtime/devnet/src/extensions/mod.rs | 1 - runtime/testnet/src/extensions.rs | 2 +- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf7abf73..e0c87955 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -426,9 +426,9 @@ checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9" [[package]] name = "asset-hub-rococo-runtime" -version = "0.12.0" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e80dcb69497d50ceef11a046720e1c5a0feeb5575239c5ccb4b0be14a49bcfa" +checksum = "af52c4d3011936f615f3469b3c20f8b13b861f4c06d097bc1922200ce44781d9" dependencies = [ "assets-common", "bp-asset-hub-rococo", @@ -445,6 +445,7 @@ dependencies = [ "cumulus-primitives-utility", "frame-benchmarking", "frame-executive", + "frame-metadata-hash-extension", "frame-support", "frame-system", "frame-system-benchmarking", @@ -3747,6 +3748,22 @@ dependencies = [ "serde", ] +[[package]] +name = "frame-metadata-hash-extension" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bb1eec9eb46d3e016c95b2fa875118c04609f2150013c56a894cae00581e265" +dependencies = [ + "array-bytes 6.2.2", + "docify", + "frame-support", + "frame-system", + "log", + "parity-scale-codec", + "scale-info", + "sp-runtime", +] + [[package]] name = "frame-remote-externalities" version = "0.36.0" @@ -6979,9 +6996,9 @@ dependencies = [ [[package]] name = "pallet-collator-selection" -version = "10.0.2" +version = "10.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49d1157d9a4b7966040158a7b4f1fb29f0cefa8deb6eb9b3452df7ce4161a31c" +checksum = "a36858c4275b7d19671b321e95f545e07c9643f97dffed1b333774cb391a4456" dependencies = [ "frame-benchmarking", "frame-support", @@ -7827,9 +7844,9 @@ dependencies = [ [[package]] name = "pallet-transaction-payment" -version = "29.0.1" +version = "29.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b0c408252aefe10cff96af1e54f06f45cb0dd184b4e450e9a2ecf837dfe506e" +checksum = "5a5ba71f06f09e955b80dc313c333be3f8d9e8505b051558e0b7af4806b13310" dependencies = [ "frame-support", "frame-system", @@ -8023,9 +8040,9 @@ dependencies = [ [[package]] name = "parachains-common" -version = "8.0.0" +version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d34aa00981a24a2b772afaa49e258f9bcd6bb372db060a05614becc1c74d4456" +checksum = "711a4c073e7c83aac7e414ba16c7c641d6d9e22e6d32f9775ff35b2464ffd7ff" dependencies = [ "cumulus-primitives-core", "cumulus-primitives-utility", @@ -9255,9 +9272,9 @@ dependencies = [ [[package]] name = "polkadot-runtime-common" -version = "8.0.2" +version = "8.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d815f0ff0a69dce7235d42c6e7d5e2b8b7429cba1252b4802ddc7879e2e74d4a" +checksum = "12a70422ca43d30457e2d9502a5e4af35e20fa2ff3f7cd46e0d2997c784f2665" dependencies = [ "bitvec", "frame-benchmarking", @@ -10518,13 +10535,14 @@ dependencies = [ [[package]] name = "rococo-runtime" -version = "8.0.0" +version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d089e93be2b8b76dd0d4b794a6a995ca3a1d6cb0ea3dd1cd42462f048bcfc926" +checksum = "cfa4cc054efdd3bfbec965da01b1ae16475031308c109c173347717091f6e3a5" dependencies = [ "binary-merkle-tree", "frame-benchmarking", "frame-executive", + "frame-metadata-hash-extension", "frame-support", "frame-system", "frame-system-benchmarking", @@ -13959,9 +13977,9 @@ dependencies = [ [[package]] name = "staging-xcm-builder" -version = "8.0.2" +version = "8.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "988d765ad5ab3b5cc90bb1dd143153ebdbe2b7600e10d5ef3a7f3e8df1bdac5d" +checksum = "78b7447c38be3ca9fb21c7434de2243aa6ac74acde8944cda7bb6e2a4f765801" dependencies = [ "frame-support", "frame-system", diff --git a/Cargo.toml b/Cargo.toml index 3719e83d..6b3cde02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,7 +97,7 @@ pallet-scheduler = { version = "30.0.0", default-features = false } pallet-session = { version = "29.0.0", default-features = false } pallet-sudo = { version = "29.0.0", default-features = false } pallet-timestamp = { version = "28.0.0", default-features = false } -pallet-transaction-payment = { version = "29.0.1", default-features = false } +pallet-transaction-payment = { version = "29.0.2", default-features = false } pallet-transaction-payment-rpc = "31.0.0" pallet-transaction-payment-rpc-runtime-api = { version = "29.0.0", default-features = false } pallet-utility = { version = "29.0.0", default-features = false } @@ -130,15 +130,15 @@ polkadot-cli = "8.0.0" polkadot-parachain-primitives = { version = "7.0.0", default-features = false } polkadot-runtime-parachains = { version = "8.0.3", default-features = false } polkadot-primitives = { version = "8.0.1", default-features = false } -polkadot-runtime-common = { version = "8.0.2", default-features = false } +polkadot-runtime-common = { version = "8.0.3", default-features = false } rococo-runtime-constants = { version = "8.0.0", default-features = false } -rococo-runtime = { version = "8.0.0", default-features = false } +rococo-runtime = { version = "8.0.1", default-features = false } xcm = { version = "8.0.1", package = "staging-xcm", default-features = false } -xcm-builder = { version = "8.0.2", package = "staging-xcm-builder", default-features = false } +xcm-builder = { version = "8.0.3", package = "staging-xcm-builder", default-features = false } xcm-executor = { version = "8.0.2", package = "staging-xcm-executor", default-features = false } # Cumulus -asset-hub-rococo-runtime = { version = "0.12.0", default-features = false } +asset-hub-rococo-runtime = { version = "0.12.3", default-features = false } asset-test-utils = { version = "8.0.1", default-features = false } cumulus-pallet-aura-ext = { version = "0.8.0", default-features = false } cumulus-pallet-parachain-system = { version = "0.8.1", default-features = false, features = ["parameterized-consensus-hook"] } @@ -149,8 +149,8 @@ cumulus-primitives-aura = { version = "0.8.0", default-features = false } cumulus-primitives-core = { version = "0.8.0", default-features = false } cumulus-primitives-utility = { version = "0.8.1", default-features = false } emulated-integration-tests-common = { version = "4.0.0", default-features = false } -pallet-collator-selection = { version = "10.0.2", default-features = false } -parachains-common = { version = "8.0.0", default-features = false } +pallet-collator-selection = { version = "10.0.3", default-features = false } +parachains-common = { version = "8.0.1", default-features = false } parachain-info = { version = "0.8.0", package = "staging-parachain-info", default-features = false } cumulus-primitives-parachain-inherent = "0.8.0" cumulus-relay-chain-interface = "0.8.0" diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs index aa58bcea..8f0a6eda 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions/mod.rs @@ -304,7 +304,6 @@ where // - `error`: The `DispatchError` encountered during contract execution. // - `version`: The version of the chain extension, used to determine the known errors. pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { - use sp_std::vec; // "UnknownFunctionId" and "DecodingFailed" are mapped to specific errors in the API and will // never change. let mut encoded_error = match error { diff --git a/runtime/testnet/src/extensions.rs b/runtime/testnet/src/extensions.rs index a5eeb3c3..61d3cb43 100644 --- a/runtime/testnet/src/extensions.rs +++ b/runtime/testnet/src/extensions.rs @@ -206,7 +206,7 @@ where RuntimeStateKeys::ParachainSystem(key) => { read_parachain_system_state::(key, &mut env) }, - _ => Ok(vec![0]), + _ => Ok(vec![0u8]), }? .encode(); From b067edac54c1cb375dfa72472c03fab75aaa99d2 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Tue, 16 Jul 2024 18:27:27 +0200 Subject: [PATCH 20/76] fix: cannot find macro vec error --- Cargo.lock | 1489 +++++++++++++------------- runtime/devnet/Cargo.toml | 2 +- runtime/devnet/src/extensions/mod.rs | 4 +- runtime/testnet/src/extensions.rs | 2 +- 4 files changed, 755 insertions(+), 742 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e0c87955..1983b157 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,11 +23,11 @@ dependencies = [ [[package]] name = "addr2line" -version = "0.21.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" dependencies = [ - "gimli 0.28.1", + "gimli 0.29.0", ] [[package]] @@ -68,7 +68,7 @@ dependencies = [ "cipher 0.4.4", "ctr", "ghash", - "subtle 2.5.0", + "subtle 2.6.1", ] [[package]] @@ -77,7 +77,7 @@ version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" dependencies = [ - "getrandom 0.2.12", + "getrandom 0.2.15", "once_cell", "version_check", ] @@ -89,7 +89,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ "cfg-if", - "getrandom 0.2.12", + "getrandom 0.2.15", "once_cell", "version_check", "zerocopy", @@ -106,9 +106,9 @@ dependencies = [ [[package]] name = "allocator-api2" -version = "0.2.16" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" +checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" [[package]] name = "always-assert" @@ -142,47 +142,48 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.13" +version = "0.6.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", + "is_terminal_polyfill", "utf8parse", ] [[package]] name = "anstyle" -version = "1.0.6" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" [[package]] name = "anstyle-parse" -version = "0.2.3" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" -version = "1.0.2" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" dependencies = [ "windows-sys 0.52.0", ] [[package]] name = "anstyle-wincon" -version = "3.0.2" +version = "3.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" dependencies = [ "anstyle", "windows-sys 0.52.0", @@ -190,9 +191,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.81" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" [[package]] name = "approx" @@ -214,7 +215,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -354,9 +355,9 @@ checksum = "f52f63c5c1316a16a4b35eaac8b76a98248961a533f061684cb2a7cb0eafb6c6" [[package]] name = "array-bytes" -version = "6.2.2" +version = "6.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f840fb7195bcfc5e17ea40c26e5ce6d5b9ce5d584466e17703209657e459ae0" +checksum = "5d5dde061bd34119e902bbb2d9b90c5692635cf59fb91d582c2b68043f1b8293" [[package]] name = "arrayref" @@ -404,7 +405,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.109", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -573,27 +574,25 @@ dependencies = [ [[package]] name = "async-channel" -version = "2.2.0" +version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f28243a43d821d11341ab73c80bed182dc015c514b951616cf79bd4af39af0c3" +checksum = "89b47800b0be77592da0afd425cc03468052844aff33b84e33cc696f64e77b6a" dependencies = [ "concurrent-queue", - "event-listener 5.2.0", - "event-listener-strategy 0.5.1", + "event-listener-strategy", "futures-core", "pin-project-lite 0.2.14", ] [[package]] name = "async-executor" -version = "1.9.1" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10b3e585719c2358d2660232671ca8ca4ddb4be4ce8a1842d6c2dc8685303316" +checksum = "d7ebdfa2ebdab6b1760375fa7d6f382b9f486eac35fc994625a00e89280bdbb7" dependencies = [ - "async-lock 3.3.0", "async-task", "concurrent-queue", - "fastrand 2.0.2", + "fastrand 2.1.0", "futures-lite 2.3.0", "slab", ] @@ -612,11 +611,11 @@ dependencies = [ [[package]] name = "async-fs" -version = "2.1.1" +version = "2.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc19683171f287921f2405677dd2ed2549c3b3bda697a563ebc3a121ace2aba1" +checksum = "ebcd09b382f40fcd159c2d695175b2ae620ffa5f3bd6f664131efff4e8b9e04a" dependencies = [ - "async-lock 3.3.0", + "async-lock 3.4.0", "blocking", "futures-lite 2.3.0", ] @@ -643,18 +642,18 @@ dependencies = [ [[package]] name = "async-io" -version = "2.3.2" +version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcccb0f599cfa2f8ace422d3555572f47424da5648a4382a9dd0310ff8210884" +checksum = "0d6baa8f0178795da0e71bc42c9e5d13261aac7ee549853162e66a241ba17964" dependencies = [ - "async-lock 3.3.0", + "async-lock 3.4.0", "cfg-if", "concurrent-queue", "futures-io", "futures-lite 2.3.0", "parking", - "polling 3.6.0", - "rustix 0.38.32", + "polling 3.7.2", + "rustix 0.38.34", "slab", "tracing", "windows-sys 0.52.0", @@ -671,12 +670,12 @@ dependencies = [ [[package]] name = "async-lock" -version = "3.3.0" +version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d034b430882f8381900d3fe6f0aaa3ad94f2cb4ac519b429692a1bc2dda4ae7b" +checksum = "ff6e472cdea888a4bd64f342f09b3f50e1886d32afe8df3d663c01140b811b18" dependencies = [ - "event-listener 4.0.3", - "event-listener-strategy 0.4.0", + "event-listener 5.3.1", + "event-listener-strategy", "pin-project-lite 0.2.14", ] @@ -697,7 +696,7 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b948000fad4873c1c9339d60f2623323a0cfd3816e5181033c6a5cb68b2accf7" dependencies = [ - "async-io 2.3.2", + "async-io 2.3.3", "blocking", "futures-lite 2.3.0", ] @@ -715,61 +714,63 @@ dependencies = [ "cfg-if", "event-listener 3.1.0", "futures-lite 1.13.0", - "rustix 0.38.32", + "rustix 0.38.34", "windows-sys 0.48.0", ] [[package]] name = "async-process" -version = "2.1.0" +version = "2.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "451e3cf68011bd56771c79db04a9e333095ab6349f7e47592b788e9b98720cc8" +checksum = "f7eda79bbd84e29c2b308d1dc099d7de8dcc7035e48f4bf5dc4a531a44ff5e2a" dependencies = [ - "async-channel 2.2.0", - "async-io 2.3.2", - "async-lock 3.3.0", + "async-channel 2.3.1", + "async-io 2.3.3", + "async-lock 3.4.0", "async-signal", + "async-task", "blocking", "cfg-if", - "event-listener 5.2.0", + "event-listener 5.3.1", "futures-lite 2.3.0", - "rustix 0.38.32", + "rustix 0.38.34", + "tracing", "windows-sys 0.52.0", ] [[package]] name = "async-signal" -version = "0.2.5" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e47d90f65a225c4527103a8d747001fc56e375203592b25ad103e1ca13124c5" +checksum = "794f185324c2f00e771cd9f1ae8b5ac68be2ca7abb129a87afd6e86d228bc54d" dependencies = [ - "async-io 2.3.2", - "async-lock 2.8.0", + "async-io 2.3.3", + "async-lock 3.4.0", "atomic-waker", "cfg-if", "futures-core", "futures-io", - "rustix 0.38.32", + "rustix 0.38.34", "signal-hook-registry", "slab", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] name = "async-task" -version = "4.7.0" +version = "4.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbb36e985947064623dbd357f727af08ffd077f93d696782f3c56365fa2e2799" +checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" [[package]] name = "async-trait" -version = "0.1.79" +version = "0.1.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507401cad91ec6a857ed5513a2073c82a9b9048762b885bb98655b306964681" +checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -799,22 +800,22 @@ checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" [[package]] name = "autocfg" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "backtrace" -version = "0.3.71" +version = "0.3.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" +checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" dependencies = [ - "addr2line 0.21.0", + "addr2line 0.22.0", "cc", "cfg-if", "libc", "miniz_oxide", - "object 0.32.2", + "object 0.36.1", "rustc-demangle", ] @@ -848,6 +849,12 @@ version = "0.21.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + [[package]] name = "base64ct" version = "1.6.0" @@ -894,13 +901,13 @@ dependencies = [ "lazy_static", "lazycell", "peeking_take_while", - "prettyplease 0.2.17", + "prettyplease 0.2.20", "proc-macro2", "quote", "regex", "rustc-hash", "shlex", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -930,9 +937,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] name = "bitvec" @@ -1002,9 +1009,9 @@ dependencies = [ [[package]] name = "blake3" -version = "1.5.1" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30cca6d3674597c30ddf2c587bf8d9d65c9a84d2326d941cc79c9842dfe0ef52" +checksum = "e9ec96fe9a81b5e365f9db71fe00edc4fe4ca2cc7dcb7861f0603012a7caa210" dependencies = [ "arrayref", "arrayvec 0.7.4", @@ -1033,18 +1040,15 @@ dependencies = [ [[package]] name = "blocking" -version = "1.5.1" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a37913e8dc4ddcc604f0c6d3bf2887c995153af3611de9e23c352b44c1b9118" +checksum = "703f41c54fc768e63e091340b424302bb1c29ef4aa0c7f10fe849dfb114d29ea" dependencies = [ - "async-channel 2.2.0", - "async-lock 3.3.0", + "async-channel 2.3.1", "async-task", - "fastrand 2.0.2", "futures-io", "futures-lite 2.3.0", "piper", - "tracing", ] [[package]] @@ -1152,9 +1156,9 @@ dependencies = [ [[package]] name = "bp-header-chain" -version = "0.8.0" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96157f586811969b3911d26cc79e02b28cfbecf859d96d7c12b6af10b9ea9350" +checksum = "1c4d2c457d5e18a5dbfe47a2ecd01f95036930a4a7ac0f3e47c2843bb067331b" dependencies = [ "bp-runtime", "finality-grandpa", @@ -1364,9 +1368,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.15.4" +version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" [[package]] name = "byte-slice-cast" @@ -1382,9 +1386,9 @@ checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" [[package]] name = "bytemuck" -version = "1.15.0" +version = "1.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d6d68c57235a3a081186990eca2867354726650f42f7516ca50c28d6281fd15" +checksum = "b236fc92302c97ed75b38da1f4917b5cdda4984745740f153a5d3059e48d725e" [[package]] name = "byteorder" @@ -1394,9 +1398,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.6.0" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" +checksum = "a12916984aab3fa6e39d655a33e09c0071eb36d6ab3aea5c2d78551f1df6d952" [[package]] name = "bzip2-sys" @@ -1421,9 +1425,9 @@ dependencies = [ [[package]] name = "camino" -version = "1.1.6" +version = "1.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c" +checksum = "e0ec6b951b160caa93cc0c7b209e5a3bff7aae9062213451ac99493cd844c239" dependencies = [ "serde", ] @@ -1445,7 +1449,7 @@ checksum = "eee4243f1f26fc7a42710e7439c149e2b10b05472f88090acce52632f231a73a" dependencies = [ "camino", "cargo-platform", - "semver 1.0.22", + "semver 1.0.23", "serde", "serde_json", "thiserror", @@ -1453,9 +1457,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.90" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" +checksum = "324c74f2155653c90b04f25b2a47a8a631360cb908f92a772695f430c7e31052" dependencies = [ "jobserver", "libc", @@ -1472,9 +1476,9 @@ dependencies = [ [[package]] name = "cfg-expr" -version = "0.15.7" +version = "0.15.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa50868b64a9a6fda9d593ce778849ea8715cd2a3d2cc17ffdb4a2f2f2f1961d" +checksum = "d067ad48b8650848b989a59a86c6c36a995d02d2bf778d45c3c5d57bc2718f02" dependencies = [ "smallvec", ] @@ -1527,16 +1531,16 @@ dependencies = [ [[package]] name = "chrono" -version = "0.4.37" +version = "0.4.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a0d04d43504c61aa6c7531f1871dd0d418d91130162063b789da00fd7057a5e" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" dependencies = [ "android-tzdata", "iana-time-zone", "js-sys", "num-traits", "wasm-bindgen", - "windows-targets 0.52.4", + "windows-targets 0.52.6", ] [[package]] @@ -1583,9 +1587,9 @@ dependencies = [ [[package]] name = "clang-sys" -version = "1.7.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67523a3b4be3ce1989d607a828d036249522dd9c1c8de7f4dd2dae43a37369d1" +checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" dependencies = [ "glob", "libc", @@ -1594,9 +1598,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.4" +version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +checksum = "64acc1846d54c1fe936a78dc189c34e28d3f5afc348403f28ecf53660b9b8462" dependencies = [ "clap_builder", "clap_derive", @@ -1604,9 +1608,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.2" +version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +checksum = "6fb8393d67ba2e7bfaf28a23458e4e2b543cc73a99595511eb207fdb8aede942" dependencies = [ "anstream", "anstyle", @@ -1617,21 +1621,21 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.4" +version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +checksum = "2bac35c6dafb060fd4d275d9a4ffae97917c13a6327903a8be2153cd964f7085" dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] name = "clap_lex" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" +checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" [[package]] name = "coarsetime" @@ -1656,39 +1660,39 @@ dependencies = [ [[package]] name = "color-print" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a858372ff14bab9b1b30ea504f2a4bc534582aee3e42ba2d41d2a7baba63d5d" +checksum = "1ee543c60ff3888934877a5671f45494dd27ed4ba25c6670b9a7576b7ed7a8c0" dependencies = [ "color-print-proc-macro", ] [[package]] name = "color-print-proc-macro" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57e37866456a721d0a404439a1adae37a31be4e0055590d053dfe6981e05003f" +checksum = "77ff1a80c5f3cb1ca7c06ffdd71b6a6dd6d8f896c42141fbd43f50ed28dcdb93" dependencies = [ "nom", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.71", ] [[package]] name = "colorchoice" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" [[package]] name = "comfy-table" -version = "7.1.0" +version = "7.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c64043d6c7b7a4c58e39e7efccfdea7b93d885a795d0c054a69dbbf4dd52686" +checksum = "b34115915337defe99b2aff5c2ce6771e5fbc4079f4b506301f5cf394c8452f7" dependencies = [ - "strum 0.25.0", - "strum_macros 0.25.3", + "strum 0.26.3", + "strum_macros 0.26.4", "unicode-width", ] @@ -1700,9 +1704,9 @@ checksum = "2382f75942f4b3be3690fe4f86365e9c853c1587d6ee58212cebf6e2a9ccd101" [[package]] name = "concurrent-queue" -version = "2.4.0" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d16048cd947b08fa32c24458a22f5dc5e835264f689f4f5653210c69fd107363" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" dependencies = [ "crossbeam-utils", ] @@ -1741,7 +1745,7 @@ version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e" dependencies = [ - "getrandom 0.2.12", + "getrandom 0.2.15", "once_cell", "tiny-keccak", ] @@ -1923,9 +1927,9 @@ dependencies = [ [[package]] name = "crc32fast" -version = "1.4.0" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3855a8a784b474f333699ef2bbca9db2c4a1f6d9088a90a2d25b1eb53111eaa" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" dependencies = [ "cfg-if", ] @@ -1960,9 +1964,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.19" +version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" [[package]] name = "crunchy" @@ -1978,7 +1982,7 @@ checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ "generic-array 0.14.7", "rand_core 0.6.4", - "subtle 2.5.0", + "subtle 2.6.1", "zeroize", ] @@ -2010,7 +2014,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab" dependencies = [ "generic-array 0.14.7", - "subtle 2.5.0", + "subtle 2.6.1", ] [[package]] @@ -2020,7 +2024,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25fab6889090c8133f3deb8f73ba3c65a7f456f66436fc012a1b1e272b1e103e" dependencies = [ "generic-array 0.14.7", - "subtle 2.5.0", + "subtle 2.6.1", ] [[package]] @@ -2061,7 +2065,7 @@ dependencies = [ "cumulus-primitives-core", "futures", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-node-primitives", "polkadot-node-subsystem", "polkadot-overseer", @@ -2174,7 +2178,7 @@ dependencies = [ "futures", "futures-timer", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-node-primitives", "polkadot-parachain-primitives", "polkadot-primitives", @@ -2338,7 +2342,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -2530,13 +2534,13 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c7718fe298d567adc44fae3dd7024418d6eff08264041e4b0544d1892861cd6" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "async-trait", "cumulus-primitives-core", "cumulus-relay-chain-interface", "cumulus-relay-chain-rpc-interface", "futures", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-availability-recovery", "polkadot-collator-protocol", "polkadot-core-primitives", @@ -2630,24 +2634,23 @@ dependencies = [ "byteorder", "digest 0.9.0", "rand_core 0.5.1", - "subtle 2.5.0", + "subtle 2.6.1", "zeroize", ] [[package]] name = "curve25519-dalek" -version = "4.1.2" +version = "4.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a677b8922c94e01bdbb12126b0bc852f00447528dee1782229af9c720c3f348" +checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" dependencies = [ "cfg-if", "cpufeatures", "curve25519-dalek-derive", "digest 0.10.7", "fiat-crypto", - "platforms", "rustc_version", - "subtle 2.5.0", + "subtle 2.6.1", "zeroize", ] @@ -2659,7 +2662,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -2677,9 +2680,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.120" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff4dc7287237dd438b926a81a1a5605dad33d286870e5eee2db17bf2bcd9e92a" +checksum = "273dcfd3acd4e1e276af13ed2a43eea7001318823e7a726a6b3ed39b4acc0b82" dependencies = [ "cc", "cxxbridge-flags", @@ -2689,9 +2692,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.120" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f47c6c8ad7c1a10d3ef0fe3ff6733f4db0d78f08ef0b13121543163ef327058b" +checksum = "d8b2766fbd92be34e9ed143898fce6c572dc009de39506ed6903e5a05b68914e" dependencies = [ "cc", "codespan-reporting", @@ -2699,24 +2702,24 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] name = "cxxbridge-flags" -version = "1.0.120" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "701a1ac7a697e249cdd8dc026d7a7dafbfd0dbcd8bd24ec55889f2bc13dd6287" +checksum = "839fcd5e43464614ffaa989eaf1c139ef1f0c51672a1ed08023307fa1b909ccd" [[package]] name = "cxxbridge-macro" -version = "1.0.120" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b404f596046b0bb2d903a9c786b875a126261b52b7c3a64bbb66382c41c771df" +checksum = "4b2c1c1776b986979be68bb2285da855f8d8a35851a769fca8740df7c3d07877" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -2731,12 +2734,12 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.8" +version = "0.20.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54e36fcd13ed84ffdfda6f5be89b31287cbb80c439841fe69e04841435464391" +checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" dependencies = [ - "darling_core 0.20.8", - "darling_macro 0.20.8", + "darling_core 0.20.10", + "darling_macro 0.20.10", ] [[package]] @@ -2755,16 +2758,16 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.8" +version = "0.20.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c2cf1c23a687a1feeb728783b993c4e1ad83d99f351801977dd809b48d0a70f" +checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" dependencies = [ "fnv", "ident_case", "proc-macro2", "quote", - "strsim 0.10.0", - "syn 2.0.58", + "strsim 0.11.1", + "syn 2.0.71", ] [[package]] @@ -2780,26 +2783,26 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.8" +version = "0.20.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a668eda54683121533a393014d8692171709ff57a7d61f187b6e782719f8933f" +checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ - "darling_core 0.20.8", + "darling_core 0.20.10", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] name = "data-encoding" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5" +checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" [[package]] name = "data-encoding-macro" -version = "0.1.14" +version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20c01c06f5f429efdf2bae21eb67c28b3df3cf85b7dd2d8ef09c0838dac5d33e" +checksum = "f1559b6cba622276d6d63706db152618eeb15b89b3e4041446b05876e352e639" dependencies = [ "data-encoding", "data-encoding-macro-internal", @@ -2807,9 +2810,9 @@ dependencies = [ [[package]] name = "data-encoding-macro-internal" -version = "0.1.12" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0047d07f2c89b17dd631c80450d69841a6b5d7fb17278cbc43d7e4cfcf2576f3" +checksum = "332d754c0af53bc87c108fed664d121ecf59207ec4196041f04d6ab9002ad33f" dependencies = [ "data-encoding", "syn 1.0.109", @@ -2878,20 +2881,20 @@ checksum = "d65d7ce8132b7c0e54497a4d9a55a1c2a0912a0d786cf894472ba818fba45762" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] name = "derive_more" -version = "0.99.17" +version = "0.99.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +checksum = "5f33878137e4dafd7fa914ad4e259e18a4e8e532b9617a2d0150262bf53abfce" dependencies = [ "convert_case", "proc-macro2", "quote", "rustc_version", - "syn 1.0.109", + "syn 2.0.71", ] [[package]] @@ -2927,7 +2930,7 @@ dependencies = [ "block-buffer 0.10.4", "const-oid", "crypto-common", - "subtle 2.5.0", + "subtle 2.6.1", ] [[package]] @@ -2974,13 +2977,13 @@ dependencies = [ [[package]] name = "displaydoc" -version = "0.2.4" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -3004,9 +3007,9 @@ dependencies = [ "proc-macro2", "quote", "regex", - "syn 2.0.58", + "syn 2.0.71", "termcolor", - "toml 0.8.12", + "toml 0.8.14", "walkdir", ] @@ -3018,9 +3021,9 @@ checksum = "1435fa1053d8b2fbbe9be7e97eca7f33d37b28409959813daefc1446a14247f1" [[package]] name = "downcast-rs" -version = "1.2.0" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" [[package]] name = "dtoa" @@ -3085,12 +3088,12 @@ version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871" dependencies = [ - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "ed25519", "rand_core 0.6.4", "serde", "sha2 0.10.8", - "subtle 2.5.0", + "subtle 2.6.1", "zeroize", ] @@ -3114,9 +3117,9 @@ version = "4.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d9ce6874da5d4415896cd45ffbc4d1cfc0c4f9c079427bd870742c30f2f65a9" dependencies = [ - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "ed25519", - "hashbrown 0.14.3", + "hashbrown 0.14.5", "hex", "rand_core 0.6.4", "sha2 0.10.8", @@ -3125,9 +3128,9 @@ dependencies = [ [[package]] name = "either" -version = "1.10.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "elliptic-curve" @@ -3144,7 +3147,7 @@ dependencies = [ "pkcs8", "rand_core 0.6.4", "sec1", - "subtle 2.5.0", + "subtle 2.6.1", "zeroize", ] @@ -3201,22 +3204,22 @@ dependencies = [ [[package]] name = "enumflags2" -version = "0.7.9" +version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3278c9d5fb675e0a51dabcf4c0d355f692b064171535ba72361be1528a9d8e8d" +checksum = "d232db7f5956f3f14313dc2f87985c58bd2c695ce124c8cdd984e08e15ac133d" dependencies = [ "enumflags2_derive", ] [[package]] name = "enumflags2_derive" -version = "0.7.9" +version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c785274071b1b420972453b306eeca06acf4633829db4223b58a2a8c5953bc4" +checksum = "de0d48a183585823424a4ce1aa132d174a6a81bd540895822eb4c8373a8e49e8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -3227,7 +3230,7 @@ checksum = "6fd000fd6988e73bbe993ea3db9b1aa64906ab88766d654973924340c8cddb42" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -3280,9 +3283,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" dependencies = [ "libc", "windows-sys 0.52.0", @@ -3353,15 +3356,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67b215c49b2b248c855fb73579eb1f4f26c38ffdc12973e20e07b91d78d5646e" dependencies = [ "concurrent-queue", - "parking", "pin-project-lite 0.2.14", ] [[package]] name = "event-listener" -version = "5.2.0" +version = "5.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b5fb89194fa3cad959b833185b3063ba881dbfc7030680b314250779fb4cc91" +checksum = "6032be9bd27023a771701cc49f9f053c751055f71efb2e0ae5c15809093675ba" dependencies = [ "concurrent-queue", "parking", @@ -3370,21 +3372,11 @@ dependencies = [ [[package]] name = "event-listener-strategy" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "958e4d70b6d5e81971bebec42271ec641e7ff4e170a6fa605f2b8a8b65cb97d3" -dependencies = [ - "event-listener 4.0.3", - "pin-project-lite 0.2.14", -] - -[[package]] -name = "event-listener-strategy" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "332f51cb23d20b0de8458b86580878211da09bcd4503cb579c225b3d124cabb3" +checksum = "0f214dc438f977e6d4e3500aaa277f5ad94ca83fbbd9b1a15713ce2344ccc5a1" dependencies = [ - "event-listener 5.2.0", + "event-listener 5.3.1", "pin-project-lite 0.2.14", ] @@ -3411,16 +3403,17 @@ dependencies = [ [[package]] name = "expander" -version = "2.1.0" +version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00e83c02035136f1592a47964ea60c05a50e4ed8b5892cfac197063850898d4d" +checksum = "e2c470c71d91ecbd179935b24170459e926382eaaa86b590b78814e180d8a8e2" dependencies = [ "blake2 0.10.6", + "file-guard", "fs-err", - "prettier-please", + "prettyplease 0.2.20", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -3440,9 +3433,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.0.2" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "658bd65b1cf4c852a3cc96f18a8ce7b5640f6b703f905c7d74532294c2a63984" +checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" [[package]] name = "fatality" @@ -3462,7 +3455,7 @@ checksum = "f5aa1e3ae159e592ad222dc90c5acbad632b527779ba88486abe92782ab268bd" dependencies = [ "expander 0.0.4", "indexmap 1.9.3", - "proc-macro-crate 1.3.1", + "proc-macro-crate 1.1.3", "proc-macro2", "quote", "syn 1.0.109", @@ -3486,14 +3479,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ded41244b729663b1e574f1b4fb731469f69f79c17667b5d776b16cda0479449" dependencies = [ "rand_core 0.6.4", - "subtle 2.5.0", + "subtle 2.6.1", ] [[package]] name = "fiat-crypto" -version = "0.2.7" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c007b1ae3abe1cb6f85a16305acd418b7ca6343b953633fee2b76d8f108b830f" +checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" + +[[package]] +name = "file-guard" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21ef72acf95ec3d7dbf61275be556299490a245f017cf084bd23b4f68cf9407c" +dependencies = [ + "libc", + "winapi", +] [[package]] name = "file-per-thread-logger" @@ -3529,7 +3532,7 @@ dependencies = [ "log", "num-traits", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "scale-info", ] @@ -3553,9 +3556,9 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" [[package]] name = "flate2" -version = "1.0.28" +version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46303f565772937ffe1d394a4fac6f411c6013172fadde9dcdb1e147a086940e" +checksum = "5f54427cfd1c7829e2a139fcefea601bf088ebca651d2bf53ebc600eac295dae" dependencies = [ "crc32fast", "libz-sys", @@ -3634,7 +3637,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "efe02c96362e3c7308cdea7545859f767194a1f3f00928f0e1357f4b8a0b3b2c" dependencies = [ "Inflector", - "array-bytes 6.2.2", + "array-bytes 6.2.3", "chrono", "clap", "comfy-table", @@ -3685,7 +3688,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -3754,7 +3757,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5bb1eec9eb46d3e016c95b2fa875118c04609f2150013c56a894cae00581e265" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "docify", "frame-support", "frame-system", @@ -3794,7 +3797,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8e52c84b611d2049d9253f83a62ab0f093e4be5c42a7ef42ea5bb16d6611e32" dependencies = [ "aquamarine", - "array-bytes 6.2.2", + "array-bytes 6.2.3", "bitflags 1.3.2", "docify", "environmental", @@ -3838,7 +3841,7 @@ dependencies = [ "Inflector", "cfg-expr", "derive-syn-parse 0.1.5", - "expander 2.1.0", + "expander 2.2.1", "frame-support-procedural-tools", "itertools 0.10.5", "macro_magic", @@ -3846,7 +3849,7 @@ dependencies = [ "proc-macro2", "quote", "sp-crypto-hashing", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -3859,7 +3862,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -3870,7 +3873,7 @@ checksum = "68672b9ec6fe72d259d3879dc212c5e42e977588cdac830c76f54d9f492aeb58" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -3958,7 +3961,7 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29f9df8a11882c4e3335eb2d18a0137c505d9ca927470b0cac9c6f0ae07d28f7" dependencies = [ - "rustix 0.38.32", + "rustix 0.38.34", "windows-sys 0.48.0", ] @@ -4048,7 +4051,7 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52527eb5074e35e9339c6b4e8d12600c7128b68fb25dcb9fa9dec18f7c25f3a5" dependencies = [ - "fastrand 2.0.2", + "fastrand 2.1.0", "futures-core", "futures-io", "parking", @@ -4063,7 +4066,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -4165,9 +4168,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.12" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ "cfg-if", "libc", @@ -4207,9 +4210,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.28.1" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" [[package]] name = "glob" @@ -4225,7 +4228,7 @@ checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" dependencies = [ "ff", "rand_core 0.6.4", - "subtle 2.5.0", + "subtle 2.6.1", ] [[package]] @@ -4296,9 +4299,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.14.3" +version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" dependencies = [ "ahash 0.8.11", "allocator-api2", @@ -4311,7 +4314,7 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8094feaf31ff591f651a2664fb9cfd92bba7a60ce3197265e9482ebe753c8f7" dependencies = [ - "hashbrown 0.14.3", + "hashbrown 0.14.5", ] [[package]] @@ -4332,6 +4335,12 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "hermit-abi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" + [[package]] name = "hex" version = "0.4.3" @@ -4443,9 +4452,9 @@ checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f" [[package]] name = "httparse" -version = "1.8.0" +version = "1.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" +checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" [[package]] name = "httpdate" @@ -4461,9 +4470,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "hyper" -version = "0.14.28" +version = "0.14.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" +checksum = "a152ddd61dfaec7273fe8419ab357f33aee0d914c5f4efbf0d96fa749eea5ec9" dependencies = [ "bytes", "futures-channel", @@ -4476,7 +4485,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite 0.2.14", - "socket2 0.5.6", + "socket2 0.5.7", "tokio", "tower-service", "tracing", @@ -4493,7 +4502,7 @@ dependencies = [ "http", "hyper", "log", - "rustls 0.21.10", + "rustls 0.21.12", "rustls-native-certs 0.6.3", "tokio", "tokio-rustls 0.24.1", @@ -4565,7 +4574,7 @@ version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6b0422c86d7ce0e97169cc42e04ae643caf278874a7a3c87b8150a220dc7e1e" dependencies = [ - "async-io 2.3.2", + "async-io 2.3.3", "core-foundation", "fnv", "futures", @@ -4629,18 +4638,18 @@ dependencies = [ [[package]] name = "include_dir" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18762faeff7122e89e0857b02f7ce6fcc0d101d5e9ad2ad7846cc01d61b7f19e" +checksum = "923d117408f1e49d914f1a379a309cffe4f18c05cf4e3d12e613a15fc81bd0dd" dependencies = [ "include_dir_macros", ] [[package]] name = "include_dir_macros" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b139284b5cf57ecfa712bcc66950bb635b31aff41c188e8a4cfc758eca374a3f" +checksum = "7cab85a7ed0bd5f0e76d93846e0147172bed2e2d3f859bcc33a8d9699cad1a75" dependencies = [ "proc-macro2", "quote", @@ -4664,7 +4673,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" dependencies = [ "equivalent", - "hashbrown 0.14.3", + "hashbrown 0.14.5", ] [[package]] @@ -4697,9 +4706,9 @@ dependencies = [ [[package]] name = "instant" -version = "0.1.12" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" dependencies = [ "cfg-if", ] @@ -4756,7 +4765,7 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.9", "libc", "windows-sys 0.48.0", ] @@ -4773,7 +4782,7 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b58db92f96b720de98181bbbe63c831e87005ab460c1bf306eb2622b4707997f" dependencies = [ - "socket2 0.5.6", + "socket2 0.5.7", "widestring", "windows-sys 0.48.0", "winreg", @@ -4791,7 +4800,7 @@ version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.9", "libc", "windows-sys 0.52.0", ] @@ -4805,6 +4814,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + [[package]] name = "itertools" version = "0.10.5" @@ -4840,9 +4855,9 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "jobserver" -version = "0.1.28" +version = "0.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab46a6e9526ddef3ae7f787c06f0f2600639ba80ea3eade3d8e670a2230f51d6" +checksum = "d2b099aaa34a9751c5bf0878add70444e1ed2dd73f347be99003d4577277de6e" dependencies = [ "libc", ] @@ -4914,7 +4929,7 @@ dependencies = [ "http", "jsonrpsee-core 0.21.0", "pin-project", - "rustls-native-certs 0.7.0", + "rustls-native-certs 0.7.1", "rustls-pki-types", "soketto", "thiserror", @@ -4939,7 +4954,7 @@ dependencies = [ "futures-util", "hyper", "jsonrpsee-types 0.20.3", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "rustc-hash", "serde", @@ -4957,7 +4972,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "776d009e2f591b78c038e0d053a796f94575d66ca4e77dd84bfc5e81419e436c" dependencies = [ "anyhow", - "async-lock 3.3.0", + "async-lock 3.4.0", "async-trait", "beef", "futures-timer", @@ -5021,7 +5036,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29110019693a4fa2dbda04876499d098fa16d70eba06b1e6e2b3f1b251419515" dependencies = [ "heck 0.4.1", - "proc-macro-crate 1.3.1", + "proc-macro-crate 1.1.3", "proc-macro2", "quote", "syn 1.0.109", @@ -5134,7 +5149,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf7a85fe66f9ff9cd74e169fdd2c94c6e1e74c412c99a73b4df3200b5d3760b2" dependencies = [ "kvdb", - "parking_lot 0.12.1", + "parking_lot 0.12.3", ] [[package]] @@ -5145,7 +5160,7 @@ checksum = "b644c70b92285f66bfc2032922a79000ea30af7bc2ab31902992a5dcb9b434f6" dependencies = [ "kvdb", "num_cpus", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "regex", "rocksdb", "smallvec", @@ -5164,9 +5179,9 @@ dependencies = [ [[package]] name = "lazy_static" -version = "1.4.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "lazycell" @@ -5176,18 +5191,18 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.153" +version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "libloading" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" +checksum = "e310b3a6b5907f99202fcdb4960ff45b93735d7c7d96b760fcff8db2dc0e103d" dependencies = [ "cfg-if", - "windows-targets 0.52.4", + "windows-targets 0.52.6", ] [[package]] @@ -5205,7 +5220,7 @@ dependencies = [ "bytes", "futures", "futures-timer", - "getrandom 0.2.12", + "getrandom 0.2.15", "instant", "libp2p-allow-block-list", "libp2p-connection-limits", @@ -5270,7 +5285,7 @@ dependencies = [ "multihash 0.17.0", "multistream-select", "once_cell", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "pin-project", "quick-protobuf", "rand", @@ -5290,7 +5305,7 @@ dependencies = [ "futures", "libp2p-core", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "smallvec", "trust-dns-resolver", ] @@ -5452,7 +5467,7 @@ dependencies = [ "libp2p-identity", "libp2p-tls", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "quinn-proto", "rand", "rustls 0.20.9", @@ -5568,7 +5583,7 @@ dependencies = [ "futures-rustls", "libp2p-core", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "quicksink", "rw-stream-sink", "soketto", @@ -5595,7 +5610,7 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "libc", ] @@ -5641,7 +5656,7 @@ checksum = "5be9b9bb642d8522a44d533eab56c16c738301965504753b03ad1de3425d5451" dependencies = [ "crunchy", "digest 0.9.0", - "subtle 2.5.0", + "subtle 2.6.1", ] [[package]] @@ -5664,9 +5679,9 @@ dependencies = [ [[package]] name = "libz-sys" -version = "1.1.16" +version = "1.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e143b5e666b2695d28f6bca6497720813f699c9602dd7f5cac91008b8ada7f9" +checksum = "c15da26e5af7e25c90b37a2d75cdbf940cf4a55316de9d84c679c9b8bfabf82e" dependencies = [ "cc", "pkg-config", @@ -5720,9 +5735,9 @@ checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" [[package]] name = "linux-raw-sys" -version = "0.4.13" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] name = "lioness" @@ -5738,9 +5753,9 @@ dependencies = [ [[package]] name = "lock_api" -version = "0.4.11" +version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" dependencies = [ "autocfg", "scopeguard", @@ -5748,9 +5763,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.21" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "lru" @@ -5782,7 +5797,7 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3262e75e648fce39813cb56ac41f3c3e3f65217ebf3844d818d1f9398cfb0dc" dependencies = [ - "hashbrown 0.14.3", + "hashbrown 0.14.5", ] [[package]] @@ -5796,9 +5811,9 @@ dependencies = [ [[package]] name = "lz4" -version = "1.24.0" +version = "1.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e9e2dd86df36ce760a60f6ff6ad526f7ba1f14ba0356f8254fb6905e6494df1" +checksum = "d6eab492fe7f8651add23237ea56dbf11b3c4ff762ab83d40a47f11433421f91" dependencies = [ "libc", "lz4-sys", @@ -5806,9 +5821,9 @@ dependencies = [ [[package]] name = "lz4-sys" -version = "1.9.4" +version = "1.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57d27b317e207b10f69f5e75494119e391a96f48861ae870d1da6edac98ca900" +checksum = "e9764018d143cc854c9f17f0b907de70f14393b1f502da6375dce70f00514eb3" dependencies = [ "cc", "libc", @@ -5825,50 +5840,50 @@ dependencies = [ [[package]] name = "macro_magic" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e03844fc635e92f3a0067e25fa4bf3e3dbf3f2927bf3aa01bb7bc8f1c428949d" +checksum = "cc33f9f0351468d26fbc53d9ce00a096c8522ecb42f19b50f34f2c422f76d21d" dependencies = [ "macro_magic_core", "macro_magic_macros", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] name = "macro_magic_core" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "468155613a44cfd825f1fb0ffa532b018253920d404e6fca1e8d43155198a46d" +checksum = "1687dc887e42f352865a393acae7cf79d98fab6351cde1f58e9e057da89bf150" dependencies = [ "const-random", - "derive-syn-parse 0.1.5", + "derive-syn-parse 0.2.0", "macro_magic_core_macros", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] name = "macro_magic_core_macros" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ea73aa640dc01d62a590d48c0c3521ed739d53b27f919b25c3551e233481654" +checksum = "b02abfe41815b5bd98dbd4260173db2c116dda171dc0fe7838cb206333b83308" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] name = "macro_magic_macros" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef9d79ae96aaba821963320eb2b6e34d17df1e5a83d8a1985c29cc5be59577b3" +checksum = "73ea28ee64b88876bf45277ed9a5817c1817df061a74f2b988971a12570e5869" dependencies = [ "macro_magic_core", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -5919,9 +5934,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.7.2" +version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "memfd" @@ -5929,7 +5944,7 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2cffa4ad52c6f791f4f8b15f0c05f9824b2ced1160e88cc393d64fff9a8ac64" dependencies = [ - "rustix 0.38.32", + "rustix 0.38.34", ] [[package]] @@ -5999,9 +6014,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "miniz_oxide" -version = "0.7.2" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" dependencies = [ "adler", ] @@ -6028,16 +6043,16 @@ dependencies = [ "bitflags 1.3.2", "blake2 0.10.6", "c2-chacha", - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "either", "hashlink", "lioness", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "rand_chacha 0.3.1", "rand_distr", - "subtle 2.5.0", + "subtle 2.6.1", "thiserror", "zeroize", ] @@ -6146,7 +6161,7 @@ dependencies = [ "blake3", "core2", "digest 0.10.7", - "multihash-derive 0.8.0", + "multihash-derive 0.8.1", "sha2 0.10.8", "sha3", "unsigned-varint", @@ -6160,7 +6175,7 @@ checksum = "cfd8a792c1694c6da4f68db0a9d707c72bd260994da179e6030a5dcee00bb815" dependencies = [ "core2", "digest 0.10.7", - "multihash-derive 0.8.0", + "multihash-derive 0.8.1", "sha2 0.10.8", "unsigned-varint", ] @@ -6197,16 +6212,16 @@ dependencies = [ [[package]] name = "multihash-derive" -version = "0.8.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc076939022111618a5026d3be019fd8b366e76314538ff9a1b59ffbcbf98bcd" +checksum = "1d6d4752e6230d8ef7adf7bd5d8c4b1f6561c1014c5ba9a37445ccefe18aa1db" dependencies = [ - "proc-macro-crate 1.3.1", + "proc-macro-crate 1.1.3", "proc-macro-error", "proc-macro2", "quote", "syn 1.0.109", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -6222,16 +6237,16 @@ dependencies = [ [[package]] name = "multihash-derive-impl" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d38685e08adb338659871ecfc6ee47ba9b22dcc8abcf6975d379cc49145c3040" +checksum = "3958713ce794e12f7c6326fac9aa274c68d74c4881dd37b3e2662b8a2046bb19" dependencies = [ - "proc-macro-crate 1.3.1", + "proc-macro-crate 2.0.0", "proc-macro-error", "proc-macro2", "quote", - "syn 1.0.109", - "synstructure", + "syn 2.0.71", + "synstructure 0.13.1", ] [[package]] @@ -6256,9 +6271,9 @@ dependencies = [ [[package]] name = "nalgebra" -version = "0.32.5" +version = "0.32.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ea4908d4f23254adda3daa60ffef0f1ac7b8c3e9a864cf3cc154b251908a2ef" +checksum = "7b5c17de023a86f59ed79891b2e5d5a94c705dbe904a5b5c9c952ea6221b03e4" dependencies = [ "approx", "matrixmultiply", @@ -6272,13 +6287,13 @@ dependencies = [ [[package]] name = "nalgebra-macros" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91761aed67d03ad966ef783ae962ef9bbaca728d2dd7ceb7939ec110fffad998" +checksum = "254a5372af8fc138e36684761d3c0cdb758a4410e938babcff1c860ce14ddbfc" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.71", ] [[package]] @@ -6379,7 +6394,7 @@ version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "cfg-if", "libc", ] @@ -6420,20 +6435,19 @@ checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" [[package]] name = "num-bigint" -version = "0.4.4" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" dependencies = [ - "autocfg", "num-integer", "num-traits", ] [[package]] name = "num-complex" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" dependencies = [ "num-traits", ] @@ -6465,11 +6479,10 @@ dependencies = [ [[package]] name = "num-rational" -version = "0.4.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" dependencies = [ - "autocfg", "num-bigint", "num-integer", "num-traits", @@ -6477,9 +6490,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", "libm", @@ -6491,7 +6504,7 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.9", "libc", ] @@ -6515,9 +6528,9 @@ dependencies = [ [[package]] name = "object" -version = "0.32.2" +version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" +checksum = "081b846d1d56ddfc18fdf1a922e4f6e07a11768ea1b92dec44e42b72712ccfce" dependencies = [ "memchr", ] @@ -6563,9 +6576,9 @@ checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" [[package]] name = "orchestra" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2356622ffdfe72362a45a1e5e87bb113b8327e596e39b91f11f0ef4395c8da79" +checksum = "92829eef0328a3d1cd22a02c0e51deb92a5362df3e7d21a4e9bdc38934694e66" dependencies = [ "async-trait", "dyn-clonable", @@ -6580,15 +6593,15 @@ dependencies = [ [[package]] name = "orchestra-proc-macro" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eedb646674596266dc9bb2b5c7eea7c36b32ecc7777eba0d510196972d72c4fd" +checksum = "1344346d5af32c95bbddea91b18a88cc83eac394192d20ef2fc4c40a74332355" dependencies = [ - "expander 2.1.0", + "expander 2.2.1", "indexmap 2.2.6", "itertools 0.11.0", "petgraph", - "proc-macro-crate 1.3.1", + "proc-macro-crate 3.1.0", "proc-macro2", "quote", "syn 1.0.109", @@ -6832,7 +6845,7 @@ version = "29.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d334f24d3c0c016d16aa87d069485847d622e8ebebace18ec5cf56609ca3a67" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "binary-merkle-tree", "frame-support", "frame-system", @@ -7074,7 +7087,7 @@ checksum = "3163c6bc21b55a0ccb74c546ba784d9c9e69beb9240c059d28a3052f4cbce509" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -7430,9 +7443,9 @@ dependencies = [ [[package]] name = "pallet-nomination-pools" -version = "26.0.0" +version = "26.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f14519c1c613d2f8c95c27015864c11a37969a23deeba9f6dbaff4276e1b81c" +checksum = "b62091305ec7426e71c3da2b0944c2df5a804109ee4d2e8f4fe34865e049f8ac" dependencies = [ "frame-support", "frame-system", @@ -7742,7 +7755,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -7757,9 +7770,9 @@ dependencies = [ [[package]] name = "pallet-staking-runtime-api" -version = "15.0.0" +version = "15.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "237d7b5a10cb6cba727c3e957fb241776302aa3cce589e6759ba53f50129c1a5" +checksum = "e47c73850103db30b61ef170107afe1ef0dab6905c495bd6dfb57b3c1dd81bc7" dependencies = [ "parity-scale-codec", "sp-api", @@ -8121,7 +8134,7 @@ dependencies = [ "log", "lz4", "memmap2 0.5.10", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "siphasher 0.3.11", "snap", @@ -8130,9 +8143,9 @@ dependencies = [ [[package]] name = "parity-scale-codec" -version = "3.6.9" +version = "3.6.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "881331e34fa842a2fb61cc2db9643a8fedc615e47cfcc52597d1af0db9a7e8fe" +checksum = "306800abfa29c7f16596b5970a588435e3d5b3149683d00c12b699cc19f895ee" dependencies = [ "arrayvec 0.7.4", "bitvec", @@ -8145,11 +8158,11 @@ dependencies = [ [[package]] name = "parity-scale-codec-derive" -version = "3.6.9" +version = "3.6.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be30eaf4b0a9fba5336683b38de57bb86d179a35862ba6bfcf57625d006bde5b" +checksum = "d830939c76d294956402033aee57a6da7b438f2294eb94864c37b0569053a42c" dependencies = [ - "proc-macro-crate 2.0.0", + "proc-macro-crate 3.1.0", "proc-macro2", "quote", "syn 1.0.109", @@ -8173,7 +8186,7 @@ dependencies = [ "impl-trait-for-tuples", "lru 0.8.1", "parity-util-mem-derive", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "primitive-types", "smallvec", "winapi", @@ -8187,7 +8200,7 @@ checksum = "f557c32c6d268a07c921471619c0295f5efad3a0e76d4f97a05c091a51d110b2" dependencies = [ "proc-macro2", "syn 1.0.109", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -8215,12 +8228,12 @@ dependencies = [ [[package]] name = "parking_lot" -version = "0.12.1" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" dependencies = [ "lock_api", - "parking_lot_core 0.9.9", + "parking_lot_core 0.9.10", ] [[package]] @@ -8239,15 +8252,15 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.9.9" +version = "0.9.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ "cfg-if", "libc", - "redox_syscall 0.4.1", + "redox_syscall 0.5.3", "smallvec", - "windows-targets 0.48.5", + "windows-targets 0.52.6", ] [[package]] @@ -8258,9 +8271,9 @@ checksum = "7924d1d0ad836f665c9065e26d016c673ece3993f30d340068b16f282afc1156" [[package]] name = "paste" -version = "1.0.14" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "pbkdf2" @@ -8303,9 +8316,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pest" -version = "2.7.9" +version = "2.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "311fb059dee1a7b802f036316d790138c613a4e8b180c822e3925a662e9f0c95" +checksum = "cd53dff83f26735fdc1ca837098ccf133605d794cdae66acfc2bfac3ec809d95" dependencies = [ "memchr", "thiserror", @@ -8314,9 +8327,9 @@ dependencies = [ [[package]] name = "pest_derive" -version = "2.7.9" +version = "2.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f73541b156d32197eecda1a4014d7f868fd2bcb3c550d5386087cfba442bf69c" +checksum = "2a548d2beca6773b1c244554d36fcf8548a8a58e74156968211567250e48e49a" dependencies = [ "pest", "pest_generator", @@ -8324,22 +8337,22 @@ dependencies = [ [[package]] name = "pest_generator" -version = "2.7.9" +version = "2.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c35eeed0a3fab112f75165fdc026b3913f4183133f19b49be773ac9ea966e8bd" +checksum = "3c93a82e8d145725dcbaf44e5ea887c8a869efdcc28706df2d08c69e17077183" dependencies = [ "pest", "pest_meta", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] name = "pest_meta" -version = "2.7.9" +version = "2.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2adbf29bb9776f28caece835398781ab24435585fe0d4dc1374a61db5accedca" +checksum = "a941429fea7e08bedec25e4f6785b6ffaacc6b755da98df5ef3e7dcf4a124c4f" dependencies = [ "once_cell", "pest", @@ -8348,9 +8361,9 @@ dependencies = [ [[package]] name = "petgraph" -version = "0.6.4" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" +checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" dependencies = [ "fixedbitset", "indexmap 2.2.6", @@ -8373,7 +8386,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -8396,12 +8409,12 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "piper" -version = "0.2.1" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "668d31b1c4eba19242f2088b2bf3316b82ca31082a8335764db4e083db7485d4" +checksum = "ae1d5c74c9876f070d3e8fd503d748c7d974c3e48da8f41350fa5222ef9b4391" dependencies = [ "atomic-waker", - "fastrand 2.0.2", + "fastrand 2.1.0", "futures-io", ] @@ -8421,12 +8434,6 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" -[[package]] -name = "platforms" -version = "3.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db23d408679286588f4d4644f965003d056e3dd5abcaaa938116871d7ce2fee7" - [[package]] name = "polkadot-approval-distribution" version = "8.0.0" @@ -8654,7 +8661,7 @@ dependencies = [ "fatality", "futures", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-node-metrics", "polkadot-node-network-protocol", "polkadot-node-subsystem", @@ -8915,7 +8922,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3bbb1b5f4b966f21a0336e94c0a0222958d2f3cba451da1157af271d07f9748" dependencies = [ "always-assert", - "array-bytes 6.2.2", + "array-bytes 6.2.3", "blake3", "cfg-if", "futures", @@ -9013,7 +9020,7 @@ dependencies = [ "log", "mick-jaeger", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-node-primitives", "polkadot-primitives", "sc-network", @@ -9146,7 +9153,7 @@ dependencies = [ "kvdb", "parity-db", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "pin-project", "polkadot-node-jaeger", "polkadot-node-metrics", @@ -9177,7 +9184,7 @@ dependencies = [ "futures", "futures-timer", "orchestra", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-node-metrics", "polkadot-node-network-protocol", "polkadot-node-primitives", @@ -9412,7 +9419,7 @@ dependencies = [ "pallet-transaction-payment-rpc-runtime-api", "parity-db", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-approval-distribution", "polkadot-availability-bitfield-distribution", "polkadot-availability-distribution", @@ -9553,7 +9560,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6380dbe1fb03ecc74ad55d841cfc75480222d153ba69ddcb00977866cbdabdb8" dependencies = [ "polkavm-derive-impl", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -9565,7 +9572,7 @@ dependencies = [ "polkavm-common", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -9586,15 +9593,15 @@ dependencies = [ [[package]] name = "polling" -version = "3.6.0" +version = "3.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0c976a60b2d7e99d6f229e414670a9b85d13ac305cc6d1e9c134de58c5aaaf6" +checksum = "a3ed00ed3fbf728b5816498ecd316d1716eecaced9c0c8d2c5a6740ca214985b" dependencies = [ "cfg-if", "concurrent-queue", - "hermit-abi", + "hermit-abi 0.4.0", "pin-project-lite 0.2.14", - "rustix 0.38.32", + "rustix 0.38.34", "tracing", "windows-sys 0.52.0", ] @@ -9903,21 +9910,11 @@ dependencies = [ "termtree", ] -[[package]] -name = "prettier-please" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22020dfcf177fcc7bf5deaf7440af371400c67c0de14c399938d8ed4fb4645d3" -dependencies = [ - "proc-macro2", - "syn 2.0.58", -] - [[package]] name = "prettyplease" -version = "0.1.11" +version = "0.1.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f28f53e8b192565862cf99343194579a022eb9c7dd3a8d03134734803c7b3125" +checksum = "6c8646e95016a7a6c4adea95bafa8a16baab64b583356217f2c85db4a39d9a86" dependencies = [ "proc-macro2", "syn 1.0.109", @@ -9925,12 +9922,12 @@ dependencies = [ [[package]] name = "prettyplease" -version = "0.2.17" +version = "0.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d3928fb5db768cb86f891ff014f0144589297e3c6a1aba6ed7cecfdace270c7" +checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e" dependencies = [ "proc-macro2", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -9966,12 +9963,12 @@ dependencies = [ [[package]] name = "proc-macro-crate" -version = "1.3.1" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" +checksum = "e17d47ce914bf4de440332250b0edd23ce48c005f59fab39d3335866b114f11a" dependencies = [ - "once_cell", - "toml_edit 0.19.15", + "thiserror", + "toml 0.5.11", ] [[package]] @@ -10024,29 +10021,29 @@ checksum = "834da187cfe638ae8abb0203f0b33e5ccdb02a28e7199f2f47b3e2754f50edca" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] name = "proc-macro2" -version = "1.0.79" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] [[package]] name = "prometheus" -version = "0.13.3" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "449811d15fbdf5ceb5c1144416066429cf82316e2ec8ce0c1f6f8a02e7bbcf8c" +checksum = "3d33c28a30771f7f96db69893f78b857f7450d7e0237e9c8fc6427a81bae7ed1" dependencies = [ "cfg-if", "fnv", "lazy_static", "memchr", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "thiserror", ] @@ -10058,7 +10055,7 @@ checksum = "5d6fa99d535dd930d1249e6c79cb3c2915f9172a540fe2b02a4c8f9ca954721e" dependencies = [ "dtoa", "itoa", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "prometheus-client-derive-encode", ] @@ -10070,7 +10067,7 @@ checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -10085,12 +10082,12 @@ dependencies = [ [[package]] name = "prost" -version = "0.12.3" +version = "0.12.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "146c289cda302b98a28d40c8b3b90498d6e526dd24ac2ecea73e4e491685b94a" +checksum = "deb1435c188b76130da55f17a466d252ff7b1418b2ad3e037d127b94e3411f29" dependencies = [ "bytes", - "prost-derive 0.12.3", + "prost-derive 0.12.6", ] [[package]] @@ -10106,7 +10103,7 @@ dependencies = [ "log", "multimap", "petgraph", - "prettyplease 0.1.11", + "prettyplease 0.1.25", "prost 0.11.9", "prost-types", "regex", @@ -10130,15 +10127,15 @@ dependencies = [ [[package]] name = "prost-derive" -version = "0.12.3" +version = "0.12.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efb6c9a1dd1def8e2124d17e83a20af56f1570d6c2d2bd9e266ccb768df3840e" +checksum = "81bddcdb20abf9501610992b6759a4c888aef7d1a7247ef75e2404275ac24af1" dependencies = [ "anyhow", - "itertools 0.11.0", + "itertools 0.12.1", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -10218,9 +10215,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.35" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -10277,7 +10274,7 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ - "getrandom 0.2.12", + "getrandom 0.2.15", ] [[package]] @@ -10355,13 +10352,22 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "redox_syscall" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" +dependencies = [ + "bitflags 2.6.0", +] + [[package]] name = "redox_users" version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" dependencies = [ - "getrandom 0.2.12", + "getrandom 0.2.15", "libredox", "thiserror", ] @@ -10380,22 +10386,22 @@ dependencies = [ [[package]] name = "ref-cast" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4846d4c50d1721b1a3bef8af76924eef20d5e723647333798c1b519b3a9473f" +checksum = "ccf0a6f84d5f1d581da8b41b47ec8600871962f2a528115b542b362d4b744931" dependencies = [ "ref-cast-impl", ] [[package]] name = "ref-cast-impl" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fddb4f8d99b0a2ebafc65a87a69a7b9875e4b1ae1f00db265d300ef7f28bccc" +checksum = "bcc303e793d3734489387d205e9b186fac9c6cfacedd98cbb2e8a5943595f3e6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -10412,14 +10418,14 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.4" +version = "1.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.4.6", - "regex-syntax 0.8.3", + "regex-automata 0.4.7", + "regex-syntax 0.8.4", ] [[package]] @@ -10433,13 +10439,13 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" dependencies = [ "aho-corasick", "memchr", - "regex-syntax 0.8.3", + "regex-syntax 0.8.4", ] [[package]] @@ -10450,9 +10456,9 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] name = "regex-syntax" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" [[package]] name = "resolv-conf" @@ -10471,7 +10477,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2" dependencies = [ "hmac 0.12.1", - "subtle 2.5.0", + "subtle 2.6.1", ] [[package]] @@ -10497,7 +10503,7 @@ checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" dependencies = [ "cc", "cfg-if", - "getrandom 0.2.12", + "getrandom 0.2.15", "libc", "spin 0.9.8", "untrusted 0.9.0", @@ -10692,9 +10698,9 @@ dependencies = [ [[package]] name = "rustc-demangle" -version = "0.1.23" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "rustc-hash" @@ -10714,7 +10720,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" dependencies = [ - "semver 1.0.22", + "semver 1.0.23", ] [[package]] @@ -10756,14 +10762,14 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.32" +version = "0.38.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89" +checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "errno", "libc", - "linux-raw-sys 0.4.13", + "linux-raw-sys 0.4.14", "windows-sys 0.52.0", ] @@ -10781,9 +10787,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.21.10" +version = "0.21.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9d5a6813c0759e4609cd494e8e725babae6a2ca7b62a5536a13daaec6fcb7ba" +checksum = "3f56a14d1f48b391359b22f731fd4bd7e43c97f3c50eee276f3aa09c94784d3e" dependencies = [ "log", "ring 0.17.8", @@ -10793,15 +10799,15 @@ dependencies = [ [[package]] name = "rustls" -version = "0.22.3" +version = "0.22.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99008d7ad0bbbea527ec27bddbc0e432c5b87d8175178cee68d2eec9c4a1813c" +checksum = "bf4ef73721ac7bcd79b2b315da7779d8fc09718c6b3d2d1b2d94850eb8c18432" dependencies = [ "log", "ring 0.17.8", "rustls-pki-types", - "rustls-webpki 0.102.2", - "subtle 2.5.0", + "rustls-webpki 0.102.5", + "subtle 2.6.1", "zeroize", ] @@ -10819,12 +10825,12 @@ dependencies = [ [[package]] name = "rustls-native-certs" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f1fb85efa936c42c6d5fc28d2629bb51e4b2f4b8a5211e297d599cc5a093792" +checksum = "a88d6d420651b496bdd98684116959239430022a115c1240e6c3993be0b15fba" dependencies = [ "openssl-probe", - "rustls-pemfile 2.1.1", + "rustls-pemfile 2.1.2", "rustls-pki-types", "schannel", "security-framework", @@ -10841,19 +10847,19 @@ dependencies = [ [[package]] name = "rustls-pemfile" -version = "2.1.1" +version = "2.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f48172685e6ff52a556baa527774f61fcaa884f59daf3375c62a3f1cd2549dab" +checksum = "29993a25686778eb88d4189742cd713c9bce943bc54251a33509dc63cbacf73d" dependencies = [ - "base64 0.21.7", + "base64 0.22.1", "rustls-pki-types", ] [[package]] name = "rustls-pki-types" -version = "1.4.1" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ecd36cc4259e3e4514335c4a138c6b43171a8d61d8f5c9348f9fc7529416f247" +checksum = "976295e77ce332211c0d24d92c0e83e50f5c5f046d11082cea19f3df13a3562d" [[package]] name = "rustls-webpki" @@ -10867,9 +10873,9 @@ dependencies = [ [[package]] name = "rustls-webpki" -version = "0.102.2" +version = "0.102.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "faaa0a62740bedb9b2ef5afa303da42764c012f743917351dc9a237ea1663610" +checksum = "f9a6fccd794a42c2c105b513a2f62bc3fd8f3ba57a4593677ceb0bd035164d78" dependencies = [ "ring 0.17.8", "rustls-pki-types", @@ -10878,9 +10884,9 @@ dependencies = [ [[package]] name = "rustversion" -version = "1.0.14" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" [[package]] name = "ruzstd" @@ -10917,15 +10923,15 @@ dependencies = [ [[package]] name = "ryu" -version = "1.0.17" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "safe_arch" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f398075ce1e6a179b46f51bd88d0598b92b00d3551f1a2d4ac49e771b56ac354" +checksum = "c3460605018fdc9612bce72735cba0d27efbcd9904780d44c7e3a9948f96148a" dependencies = [ "bytemuck", ] @@ -10966,7 +10972,7 @@ dependencies = [ "multihash 0.18.1", "multihash-codetable", "parity-scale-codec", - "prost 0.12.3", + "prost 0.12.6", "prost-build", "rand", "sc-client-api", @@ -11026,7 +11032,7 @@ version = "28.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f73880050f8b04fed7f6301279ef3899df13a3891bd06156d56f9a1c50fefba" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "docify", "log", "memmap2 0.9.4", @@ -11056,7 +11062,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -11065,7 +11071,7 @@ version = "0.37.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8a284c10ea92b1fe789b9f0e5815d393f3a1e3bf6a4adaa884f24e36143b83b" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "bip39", "chrono", "clap", @@ -11111,7 +11117,7 @@ dependencies = [ "futures", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-executor", "sc-transaction-pool-api", "sc-utils", @@ -11143,7 +11149,7 @@ dependencies = [ "log", "parity-db", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-client-api", "sc-state-db", "schnellru", @@ -11168,7 +11174,7 @@ dependencies = [ "libp2p-identity", "log", "mockall", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-client-api", "sc-utils", "serde", @@ -11226,7 +11232,7 @@ dependencies = [ "num-rational", "num-traits", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-client-api", "sc-consensus", "sc-consensus-epochs", @@ -11278,14 +11284,14 @@ version = "14.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e9ce3ee15eff7fa642791966d427f185184df3c7f4e58893705f3e7781da8ef5" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "async-channel 1.9.0", "async-trait", "fnv", "futures", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-client-api", "sc-consensus", "sc-network", @@ -11319,7 +11325,7 @@ dependencies = [ "jsonrpsee 0.20.3", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-consensus-beefy", "sc-rpc", "serde", @@ -11350,7 +11356,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ae91e5b5a120be4d13a59eaf94fd85d7c7af528482b8e21d861fa1167df3083" dependencies = [ "ahash 0.8.11", - "array-bytes 6.2.2", + "array-bytes 6.2.3", "async-trait", "dyn-clone", "finality-grandpa", @@ -11359,7 +11365,7 @@ dependencies = [ "futures-timer", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "sc-block-builder", "sc-chain-spec", @@ -11439,7 +11445,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa2ac6c356538d67987bbb867e11a12a84ba87250c70fd50005b6d74f570a4f7" dependencies = [ "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-executor-common", "sc-executor-wasmtime", "schnellru", @@ -11478,7 +11484,7 @@ dependencies = [ "cfg-if", "libc", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rustix 0.36.17", "sc-allocator", "sc-executor-common", @@ -11511,8 +11517,8 @@ version = "26.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cc4f6a558dd23e3bae2e9f195da822465258b9aaf211c34360d7f6efb944e54" dependencies = [ - "array-bytes 6.2.2", - "parking_lot 0.12.1", + "array-bytes 6.2.3", + "parking_lot 0.12.3", "serde_json", "sp-application-crypto", "sp-core", @@ -11537,7 +11543,7 @@ dependencies = [ "mixnet", "multiaddr", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-client-api", "sc-network", "sc-transaction-pool-api", @@ -11556,7 +11562,7 @@ version = "0.35.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f231c7d5e749ec428b4cfa669d759ae76cd3da4f50d7352a2d711acdc7532891" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "async-channel 1.9.0", "async-trait", "asynchronous-codec", @@ -11571,7 +11577,7 @@ dependencies = [ "log", "mockall", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "partial_sort", "pin-project", "rand", @@ -11605,7 +11611,7 @@ dependencies = [ "futures", "libp2p-identity", "log", - "prost 0.12.3", + "prost 0.12.6", "prost-build", "sc-client-api", "sc-network", @@ -11659,13 +11665,13 @@ version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38d84ef0b212c775f58e0304ec09166089f6b09afddf559b7c2b5702933b3be4" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "async-channel 1.9.0", "futures", "libp2p-identity", "log", "parity-scale-codec", - "prost 0.12.3", + "prost 0.12.6", "prost-build", "sc-client-api", "sc-network", @@ -11681,7 +11687,7 @@ version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9aa9377059deece4e7d419d9ec456f657268c0c603e1cf98df4a920f6da83461" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "async-channel 1.9.0", "async-trait", "fork-tree", @@ -11691,7 +11697,7 @@ dependencies = [ "log", "mockall", "parity-scale-codec", - "prost 0.12.3", + "prost 0.12.6", "prost-build", "sc-client-api", "sc-consensus", @@ -11718,7 +11724,7 @@ version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "16c9cad4baf348725bd82eadcd1747fc112ec49c76b863755ce79c588fa73fe4" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "futures", "libp2p", "log", @@ -11738,7 +11744,7 @@ version = "30.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aee89f2abd406356bfd688bd7a51155dc963259e4b752bb85d1f8a061a194fd" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "bytes", "fnv", "futures", @@ -11750,7 +11756,7 @@ dependencies = [ "num_cpus", "once_cell", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "sc-client-api", "sc-network", @@ -11787,7 +11793,7 @@ dependencies = [ "jsonrpsee 0.20.3", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-block-builder", "sc-chain-spec", "sc-client-api", @@ -11853,14 +11859,14 @@ version = "0.35.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7f10275c62296a785f6e2ac716521e3b6e0fae470416fdf86491cbbfcc2e23d" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "futures", "futures-util", "hex", "jsonrpsee 0.20.3", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-chain-spec", "sc-client-api", "sc-rpc", @@ -11892,7 +11898,7 @@ dependencies = [ "jsonrpsee 0.20.3", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "pin-project", "rand", "sc-chain-spec", @@ -11950,7 +11956,7 @@ checksum = "aa842052c41ad379eaecdfddc0d5c953d57e311ae688233f68f461b91d38da0a" dependencies = [ "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sp-core", ] @@ -12020,7 +12026,7 @@ dependencies = [ "futures", "libp2p", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "pin-project", "rand", "sc-utils", @@ -12043,7 +12049,7 @@ dependencies = [ "libc", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "regex", "rustc-hash", "sc-client-api", @@ -12070,7 +12076,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -12085,7 +12091,7 @@ dependencies = [ "linked-hash-map", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-client-api", "sc-transaction-pool-api", "sc-utils", @@ -12129,7 +12135,7 @@ dependencies = [ "futures-timer", "lazy_static", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "prometheus", "sp-arithmetic", ] @@ -12167,7 +12173,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3475108a1b62c7efd1b5c65974f30109a598b2f45f23c9ae030acb9686966db" dependencies = [ "darling 0.14.4", - "proc-macro-crate 1.3.1", + "proc-macro-crate 1.1.3", "proc-macro2", "quote", "syn 1.0.109", @@ -12195,7 +12201,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "995491f110efdc6bea96d6a746140e32bfceb4ea47510750a5467295a4707a25" dependencies = [ "darling 0.14.4", - "proc-macro-crate 1.3.1", + "proc-macro-crate 1.1.3", "proc-macro2", "quote", "syn 1.0.109", @@ -12203,9 +12209,9 @@ dependencies = [ [[package]] name = "scale-info" -version = "2.11.1" +version = "2.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "788745a868b0e751750388f4e6546eb921ef714a4317fa6954f7cde114eb2eb7" +checksum = "eca070c12893629e2cc820a9761bedf6ce1dcddc9852984d1dc734b8bd9bd024" dependencies = [ "bitvec", "cfg-if", @@ -12217,11 +12223,11 @@ dependencies = [ [[package]] name = "scale-info-derive" -version = "2.11.1" +version = "2.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dc2f4e8bc344b9fc3d5f74f72c2e55bfc38d28dc2ebc69c194a3df424e4d9ac" +checksum = "2d35494501194174bda522a32605929eefc9ecf7e0a326c26db1fdd85881eb62" dependencies = [ - "proc-macro-crate 1.3.1", + "proc-macro-crate 3.1.0", "proc-macro2", "quote", "syn 1.0.109", @@ -12236,7 +12242,7 @@ dependencies = [ "proc-macro2", "quote", "scale-info", - "syn 2.0.58", + "syn 2.0.71", "thiserror", ] @@ -12271,9 +12277,9 @@ dependencies = [ [[package]] name = "schnellru" -version = "0.2.1" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "772575a524feeb803e5b0fcbc6dd9f367e579488197c94c6e4023aad2305774d" +checksum = "c9a8ef13a93c54d20580de1e5c413e624e53121d42fc7e2c11d10ef7f8b02367" dependencies = [ "ahash 0.8.11", "cfg-if", @@ -12305,13 +12311,13 @@ dependencies = [ "aead", "arrayref", "arrayvec 0.7.4", - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "getrandom_or_panic", "merlin", "rand_core 0.6.4", "serde_bytes", "sha2 0.10.8", - "subtle 2.5.0", + "subtle 2.6.1", "zeroize", ] @@ -12347,7 +12353,7 @@ dependencies = [ "der", "generic-array 0.14.7", "pkcs8", - "subtle 2.5.0", + "subtle 2.6.1", "zeroize", ] @@ -12389,11 +12395,11 @@ dependencies = [ [[package]] name = "security-framework" -version = "2.10.0" +version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "770452e37cad93e0a50d5abc3990d2bc351c36d0328f86cefec2f2fb206eaef6" +checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" dependencies = [ - "bitflags 1.3.2", + "bitflags 2.6.0", "core-foundation", "core-foundation-sys", "libc", @@ -12402,9 +12408,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.10.0" +version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41f3cc463c0ef97e11c3461a9d3787412d30e8e7eb907c79180c4a57bf7c04ef" +checksum = "75da29fe9b9b08fe9d6b22b5b4bcbc75d8db3aa31e639aa56bb62e9d46bfceaf" dependencies = [ "core-foundation-sys", "libc", @@ -12421,9 +12427,9 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" dependencies = [ "serde", ] @@ -12436,9 +12442,9 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.197" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] @@ -12454,29 +12460,29 @@ dependencies = [ [[package]] name = "serde_bytes" -version = "0.11.14" +version = "0.11.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b8497c313fd43ab992087548117643f6fcd935cbf36f176ffda0aacf9591734" +checksum = "387cc504cb06bb40a96c8e04e951fe01854cf6bc921053c954e4a606d9675c6a" dependencies = [ "serde", ] [[package]] name = "serde_derive" -version = "1.0.197" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] name = "serde_json" -version = "1.0.115" +version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" dependencies = [ "itoa", "ryu", @@ -12485,9 +12491,9 @@ dependencies = [ [[package]] name = "serde_spanned" -version = "0.6.5" +version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +checksum = "79e674e01f999af37c49f70a6ede167a8a60b2503e56c5599532a65baa5969a0" dependencies = [ "serde", ] @@ -12567,9 +12573,9 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "signal-hook-registry" -version = "1.4.1" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" dependencies = [ "libc", ] @@ -12681,13 +12687,13 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e635339259e51ef85ac7aa29a1cd991b957047507288697a690e80ab97d07cad" dependencies = [ - "async-channel 2.2.0", + "async-channel 2.3.1", "async-executor", - "async-fs 2.1.1", - "async-io 2.3.2", - "async-lock 3.3.0", + "async-fs 2.1.2", + "async-io 2.3.3", + "async-lock 3.4.0", "async-net 2.0.0", - "async-process 2.1.0", + "async-process 2.2.3", "blocking", "futures-lite 2.3.0", ] @@ -12714,7 +12720,7 @@ dependencies = [ "fnv", "futures-lite 1.13.0", "futures-util", - "hashbrown 0.14.3", + "hashbrown 0.14.5", "hex", "hmac 0.12.1", "itertools 0.11.0", @@ -12753,7 +12759,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6d1eaa97d77be4d026a1e7ffad1bb3b78448763b357ea6f8188d3e6f736a9b9" dependencies = [ "arrayvec 0.7.4", - "async-lock 3.3.0", + "async-lock 3.4.0", "atomic-take", "base64 0.21.7", "bip39", @@ -12768,7 +12774,7 @@ dependencies = [ "fnv", "futures-lite 2.3.0", "futures-util", - "hashbrown 0.14.3", + "hashbrown 0.14.5", "hex", "hmac 0.12.1", "itertools 0.12.1", @@ -12818,13 +12824,13 @@ dependencies = [ "futures-channel", "futures-lite 1.13.0", "futures-util", - "hashbrown 0.14.3", + "hashbrown 0.14.5", "hex", "itertools 0.11.0", "log", "lru 0.11.1", "no-std-net", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "pin-project", "rand", "rand_chacha 0.3.1", @@ -12843,8 +12849,8 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5496f2d116b7019a526b1039ec2247dd172b8670633b1a64a614c9ea12c9d8c7" dependencies = [ - "async-channel 2.2.0", - "async-lock 3.3.0", + "async-channel 2.3.1", + "async-lock 3.4.0", "base64 0.21.7", "blake2-rfc", "derive_more", @@ -12854,13 +12860,13 @@ dependencies = [ "futures-channel", "futures-lite 2.3.0", "futures-util", - "hashbrown 0.14.3", + "hashbrown 0.14.5", "hex", "itertools 0.12.1", "log", "lru 0.12.3", "no-std-net", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "pin-project", "rand", "rand_chacha 0.3.1", @@ -12888,12 +12894,12 @@ dependencies = [ "aes-gcm", "blake2 0.10.6", "chacha20poly1305", - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "rand_core 0.6.4", "ring 0.17.8", "rustc_version", "sha2 0.10.8", - "subtle 2.5.0", + "subtle 2.6.1", ] [[package]] @@ -12908,9 +12914,9 @@ dependencies = [ [[package]] name = "snowbridge-beacon-primitives" -version = "0.1.0" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60a73ef707257064bc4ecce8323cdb7c30e8ecd1ce74aa89a6e82e81fa8b9970" +checksum = "5404af73550b39022e08e5500b30fba627e109a56407b7e80b08da2305b11bfe" dependencies = [ "byte-slice-cast", "frame-support", @@ -12933,9 +12939,9 @@ dependencies = [ [[package]] name = "snowbridge-core" -version = "0.1.1" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a3e2e3b94bfcfc8f363e21a6c5a1d3c67eb4592ada672c868a3236ad1dd563b" +checksum = "aed4ebefed4c40b9c00e9adf5f02ab2760a7a2dad8bf05110c0013a7a59f4097" dependencies = [ "ethabi-decode", "frame-support", @@ -13029,9 +13035,9 @@ dependencies = [ [[package]] name = "socket2" -version = "0.5.6" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05ffd9c0a93b7543e062e759284fcf5f5e3b098501104bfbdde4d404db792871" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" dependencies = [ "libc", "windows-sys 0.52.0", @@ -13084,11 +13090,11 @@ checksum = "0301e2f77afb450fbf2b093f8b324c7ad88cc82e5e69bd5dc8658a1f068b2a96" dependencies = [ "Inflector", "blake2 0.10.6", - "expander 2.1.0", + "expander 2.2.1", "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -13155,7 +13161,7 @@ dependencies = [ "futures", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "schnellru", "sp-api", "sp-consensus", @@ -13278,7 +13284,7 @@ version = "29.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c33c7a1568175250628567d50c4e1c54a6ac5bc1190413b9be29a9e810cbe73" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "bip39", "bitflags 1.3.2", "blake2 0.10.6", @@ -13295,7 +13301,7 @@ dependencies = [ "log", "merlin", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "paste", "primitive-types", "rand", @@ -13354,7 +13360,7 @@ checksum = "b85d0f1f1e44bd8617eb2a48203ee854981229e3e79e6f468c7175d5fd37489b" dependencies = [ "quote", "sp-crypto-hashing", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -13364,7 +13370,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "722cbecdbf5b94578137dbd07feb51e95f7de221be0c1ff4dcfe0bb4cd986929" dependencies = [ "kvdb", - "parking_lot 0.12.1", + "parking_lot 0.12.3", ] [[package]] @@ -13375,7 +13381,7 @@ checksum = "48d09fa0a5f7299fb81ee25ae3853d26200f7a348148aed6de76be905c007dbe" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -13461,7 +13467,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "444f2d53968b1ce5e908882710ff1f3873fcf3e95f59d57432daf685bbacb959" dependencies = [ "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sp-core", "sp-externalities", "thiserror", @@ -13620,11 +13626,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfaf6e85b2ec12a4b99cd6d8d57d083e30c94b7f1b0d8f93547121495aae6f0c" dependencies = [ "Inflector", - "expander 2.1.0", + "expander 2.2.1", "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -13667,7 +13673,7 @@ dependencies = [ "hash-db", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "smallvec", "sp-core", @@ -13687,7 +13693,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "309a9ae4e8134bbed8ffc510cf4d461a4a651f9250b556de782cedd876abe1ff" dependencies = [ "aes-gcm", - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "ed25519-dalek", "hkdf", "parity-scale-codec", @@ -13791,7 +13797,7 @@ dependencies = [ "memory-db", "nohash-hasher", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "scale-info", "schnellru", @@ -13831,7 +13837,7 @@ dependencies = [ "parity-scale-codec", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -13962,7 +13968,7 @@ version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48fa328b87de3466bc38cc9a07244c42c647b7755b81115e1dfeb47cc13fc6e6" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "bounded-collections 0.2.0", "derivative", "environmental", @@ -14063,7 +14069,7 @@ dependencies = [ "bitflags 1.3.2", "byteorder", "keccak", - "subtle 2.5.0", + "subtle 2.6.1", "zeroize", ] @@ -14090,9 +14096,9 @@ dependencies = [ [[package]] name = "strum" -version = "0.25.0" +version = "0.26.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" +checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" [[package]] name = "strum_macros" @@ -14109,15 +14115,15 @@ dependencies = [ [[package]] name = "strum_macros" -version = "0.25.3" +version = "0.26.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23dc1fa9ac9c169a78ba62f0b841814b7abae11bdd047b9c58f893439e309ea0" +checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" dependencies = [ - "heck 0.4.1", + "heck 0.5.0", "proc-macro2", "quote", "rustversion", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -14218,7 +14224,7 @@ dependencies = [ "sp-maybe-compressed-blob", "strum 0.24.1", "tempfile", - "toml 0.8.12", + "toml 0.8.14", "walkdir", "wasm-opt", ] @@ -14231,9 +14237,9 @@ checksum = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" [[package]] name = "subtle" -version = "2.5.0" +version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "subtle-ng" @@ -14293,7 +14299,7 @@ dependencies = [ "scale-info", "scale-typegen", "subxt-metadata", - "syn 2.0.58", + "syn 2.0.71", "thiserror", "tokio", ] @@ -14321,13 +14327,13 @@ version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "365251668613323064803427af8c7c7bc366cd8b28e33639640757669dafebd5" dependencies = [ - "darling 0.20.8", + "darling 0.20.10", "parity-scale-codec", "proc-macro-error", "quote", "scale-typegen", "subxt-codegen", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -14378,9 +14384,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.58" +version = "2.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" +checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462" dependencies = [ "proc-macro2", "quote", @@ -14399,6 +14405,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "synstructure" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.71", +] + [[package]] name = "system-configuration" version = "0.5.1" @@ -14428,9 +14445,9 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] name = "target-lexicon" -version = "0.12.14" +version = "0.12.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1fc403891a21bcfb7c37834ba66a547a8f402146eba7265b5a6d88059c9ff2f" +checksum = "4873307b7c257eddcb50c9bedf158eb669578359fb28428bef438fec8e6ba7c2" [[package]] name = "tempfile" @@ -14439,8 +14456,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" dependencies = [ "cfg-if", - "fastrand 2.0.2", - "rustix 0.38.32", + "fastrand 2.1.0", + "rustix 0.38.34", "windows-sys 0.52.0", ] @@ -14459,7 +14476,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" dependencies = [ - "rustix 0.38.32", + "rustix 0.38.34", "windows-sys 0.48.0", ] @@ -14486,9 +14503,9 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.58" +version = "1.0.62" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297" +checksum = "f2675633b1499176c2dff06b0856a27976a8f9d436737b4cf4f312d4d91d8bbb" dependencies = [ "thiserror-impl", ] @@ -14510,18 +14527,18 @@ checksum = "e4c60d69f36615a077cc7663b9cb8e42275722d23e58a7fa3d2c7f2915d09d04" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] name = "thiserror-impl" -version = "1.0.58" +version = "1.0.62" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" +checksum = "d20468752b09f49e909e55a5d338caa8bedf615594e9d80bc4c565d30faf798c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -14585,9 +14602,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.34" +version = "0.3.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" dependencies = [ "deranged", "itoa", @@ -14606,9 +14623,9 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" dependencies = [ "num-conv", "time-core", @@ -14625,9 +14642,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.6.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" dependencies = [ "tinyvec_macros", ] @@ -14640,32 +14657,32 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.37.0" +version = "1.38.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787" +checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" dependencies = [ "backtrace", "bytes", "libc", "mio", "num_cpus", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "pin-project-lite 0.2.14", "signal-hook-registry", - "socket2 0.5.6", + "socket2 0.5.7", "tokio-macros", "windows-sys 0.48.0", ] [[package]] name = "tokio-macros" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -14685,7 +14702,7 @@ version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" dependencies = [ - "rustls 0.21.10", + "rustls 0.21.12", "tokio", ] @@ -14695,7 +14712,7 @@ version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "775e0c0f0adb3a2f22a00c4745d728b479985fc15ee7ca6a2608388c5569860f" dependencies = [ - "rustls 0.22.3", + "rustls 0.22.4", "rustls-pki-types", "tokio", ] @@ -14714,9 +14731,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.10" +version = "0.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15" +checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" dependencies = [ "bytes", "futures-core", @@ -14724,7 +14741,6 @@ dependencies = [ "futures-sink", "pin-project-lite 0.2.14", "tokio", - "tracing", ] [[package]] @@ -14738,36 +14754,25 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.12" +version = "0.8.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3" +checksum = "6f49eb2ab21d2f26bd6db7bf383edc527a7ebaee412d17af4d40fdccd442f335" dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.9", + "toml_edit 0.22.15", ] [[package]] name = "toml_datetime" -version = "0.6.5" +version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf" dependencies = [ "serde", ] -[[package]] -name = "toml_edit" -version = "0.19.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" -dependencies = [ - "indexmap 2.2.6", - "toml_datetime", - "winnow 0.5.40", -] - [[package]] name = "toml_edit" version = "0.20.7" @@ -14792,15 +14797,15 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.9" +version = "0.22.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e40bb779c5187258fd7aad0eb68cb8706a0a81fa712fbea808ab43c4b8374c4" +checksum = "d59a3a72298453f564e2b111fa896f8d07fabb36f51f06d7e875fc5e0b5a3ef1" dependencies = [ "indexmap 2.2.6", "serde", "serde_spanned", "toml_datetime", - "winnow 0.6.5", + "winnow 0.6.13", ] [[package]] @@ -14824,7 +14829,7 @@ version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "bytes", "futures-core", "futures-util", @@ -14868,7 +14873,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -14909,11 +14914,11 @@ version = "5.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f074568687ffdfd0adb6005aa8d1d96840197f2c159f80471285f08694cf0ce" dependencies = [ - "expander 2.1.0", + "expander 2.2.1", "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -15046,7 +15051,7 @@ dependencies = [ "ipconfig", "lazy_static", "lru-cache", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "resolv-conf", "smallvec", "thiserror", @@ -15163,9 +15168,9 @@ dependencies = [ [[package]] name = "unicode-width" -version = "0.1.11" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" +checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" [[package]] name = "unicode-xid" @@ -15180,7 +15185,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" dependencies = [ "crypto-common", - "subtle 2.5.0", + "subtle 2.6.1", ] [[package]] @@ -15209,9 +15214,9 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "url" -version = "2.5.0" +version = "2.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" dependencies = [ "form_urlencoded", "idna 0.5.0", @@ -15220,9 +15225,9 @@ dependencies = [ [[package]] name = "utf8parse" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "valuable" @@ -15250,9 +15255,9 @@ checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" [[package]] name = "w3f-bls" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7335e4c132c28cc43caef6adb339789e599e39adbe78da0c4d547fad48cbc331" +checksum = "9c5da5fa2c6afa2c9158eaa7cd9aee249765eb32b5fb0c63ad8b9e79336a47ec" dependencies = [ "ark-bls12-377", "ark-bls12-381", @@ -15274,9 +15279,9 @@ dependencies = [ [[package]] name = "waker-fn" -version = "1.1.1" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3c4517f54858c779bbcbf228f4fca63d121bf85fbecb2dc578cdf4a39395690" +checksum = "317211a0dc0ceedd78fb2ca9a44aed3d7b9b26f81870d485c07122b4350673b7" [[package]] name = "walkdir" @@ -15339,7 +15344,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", "wasm-bindgen-shared", ] @@ -15373,7 +15378,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -15491,9 +15496,9 @@ dependencies = [ [[package]] name = "wasmparser-nostd" -version = "0.100.1" +version = "0.100.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9157cab83003221bfd385833ab587a039f5d6fa7304854042ba358a3b09e0724" +checksum = "d5a015fe95f3504a94bb1462c717aae75253e39b9dd6c3fb1062c934535c64aa" dependencies = [ "indexmap-nostd", ] @@ -15724,15 +15729,16 @@ dependencies = [ [[package]] name = "westend-runtime" -version = "8.0.0" +version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db2a5cebb4c678a0d1291bb21f9d44ddebceae044b0fb5200fa3bed108a31595" +checksum = "b4aa5580861b05668a6af845aa271c4f699a2fc26646d524e5b0d9375fb0647e" dependencies = [ "binary-merkle-tree", "bitvec", "frame-benchmarking", "frame-election-provider-support", "frame-executive", + "frame-metadata-hash-extension", "frame-support", "frame-system", "frame-system-benchmarking", @@ -15855,14 +15861,14 @@ dependencies = [ "either", "home", "once_cell", - "rustix 0.38.32", + "rustix 0.38.34", ] [[package]] name = "wide" -version = "0.7.15" +version = "0.7.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89beec544f246e679fc25490e3f8e08003bc4bf612068f325120dad4cea02c1c" +checksum = "2caba658a80831539b30698ae9862a72db6697dfdd7151e46920f5f2755c3ce2" dependencies = [ "bytemuck", "safe_arch", @@ -15870,9 +15876,9 @@ dependencies = [ [[package]] name = "widestring" -version = "1.0.2" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "653f141f39ec16bba3c5abe400a0c60da7468261cc2cbf36805022876bc721a8" +checksum = "7219d36b6eac893fa81e84ebe06485e7dcbb616177469b142df14f1f4deb1311" [[package]] name = "winapi" @@ -15892,11 +15898,11 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.6" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" dependencies = [ - "winapi", + "windows-sys 0.52.0", ] [[package]] @@ -15930,7 +15936,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.52.4", + "windows-targets 0.52.6", ] [[package]] @@ -15957,7 +15963,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.4", + "windows-targets 0.52.6", ] [[package]] @@ -15992,17 +15998,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.4", - "windows_aarch64_msvc 0.52.4", - "windows_i686_gnu 0.52.4", - "windows_i686_msvc 0.52.4", - "windows_x86_64_gnu 0.52.4", - "windows_x86_64_gnullvm 0.52.4", - "windows_x86_64_msvc 0.52.4", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -16019,9 +16026,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" @@ -16037,9 +16044,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" @@ -16055,9 +16062,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.4" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" @@ -16073,9 +16086,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" @@ -16091,9 +16104,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" @@ -16109,9 +16122,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" @@ -16127,9 +16140,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" @@ -16142,9 +16155,9 @@ dependencies = [ [[package]] name = "winnow" -version = "0.6.5" +version = "0.6.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8" +checksum = "59b5e5f6c299a3c7890b876a2a587f3115162487e704907d9b6cd29473052ba1" dependencies = [ "memchr", ] @@ -16185,7 +16198,7 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7e468321c81fb07fa7f4c636c3972b9100f0346e5b6a9f2bd0603a52f7ed277" dependencies = [ - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "rand_core 0.6.4", "serde", "zeroize", @@ -16253,7 +16266,7 @@ dependencies = [ "Inflector", "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -16265,7 +16278,7 @@ dependencies = [ "futures", "log", "nohash-hasher", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "static_assertions", ] @@ -16287,29 +16300,29 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.7.32" +version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.7.32" +version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] name = "zeroize" -version = "1.7.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" dependencies = [ "zeroize_derive", ] @@ -16322,7 +16335,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.58", + "syn 2.0.71", ] [[package]] @@ -16365,9 +16378,9 @@ dependencies = [ [[package]] name = "zstd-sys" -version = "2.0.10+zstd.1.5.6" +version = "2.0.12+zstd.1.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c253a4914af5bafc8fa8c86ee400827e83cf6ec01195ec1f1ed8441bf00d65aa" +checksum = "0a4e40c320c3cb459d9a9ff6de98cff88f4751ee9275d140e2be94a2b74e4c13" dependencies = [ "cc", "pkg-config", diff --git a/runtime/devnet/Cargo.toml b/runtime/devnet/Cargo.toml index 7db68576..70e6e9c9 100644 --- a/runtime/devnet/Cargo.toml +++ b/runtime/devnet/Cargo.toml @@ -22,7 +22,7 @@ scale-info.workspace = true smallvec.workspace = true # Local -pop-primitives = { workspace = true, default-features = false, features = ["assets", "cross-chain", "nfts"] } +pop-primitives = { workspace = true, features = ["assets", "cross-chain", "nfts"] } pop-runtime-common = { workspace = true, default-features = false } # Substrate diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs index 8f0a6eda..c6bab850 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions/mod.rs @@ -307,8 +307,8 @@ pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { // "UnknownFunctionId" and "DecodingFailed" are mapped to specific errors in the API and will // never change. let mut encoded_error = match error { - DispatchError::Other("UnknownFunctionId") => vec![254, 0, 0, 0], - DispatchError::Other("DecodingFailed") => vec![255, 0, 0, 0], + DispatchError::Other("UnknownFunctionId") => Vec::from([254u8, 0, 0, 0]), + DispatchError::Other("DecodingFailed") => Vec::from([255u8, 0, 0, 0]), _ => error.encode(), }; // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). diff --git a/runtime/testnet/src/extensions.rs b/runtime/testnet/src/extensions.rs index 61d3cb43..866cb12c 100644 --- a/runtime/testnet/src/extensions.rs +++ b/runtime/testnet/src/extensions.rs @@ -206,7 +206,7 @@ where RuntimeStateKeys::ParachainSystem(key) => { read_parachain_system_state::(key, &mut env) }, - _ => Ok(vec![0u8]), + _ => Ok(Vec::from([0u8])), }? .encode(); From 8730f90d07dbd1ce564a14a19bb1288a70ddaa4b Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Wed, 17 Jul 2024 17:47:38 +0200 Subject: [PATCH 21/76] refactor: decoding failed and commenting out unimplemented tests --- .../integration-tests/src/local_fungibles.rs | 448 +++++++++--------- pop-api/src/lib.rs | 4 +- pop-api/src/v0/assets/mod.rs | 14 +- primitives/src/lib.rs | 2 +- runtime/devnet/src/extensions/mod.rs | 2 +- 5 files changed, 236 insertions(+), 234 deletions(-) diff --git a/pop-api/integration-tests/src/local_fungibles.rs b/pop-api/integration-tests/src/local_fungibles.rs index d0229c84..bef3aa6a 100644 --- a/pop-api/integration-tests/src/local_fungibles.rs +++ b/pop-api/integration-tests/src/local_fungibles.rs @@ -67,37 +67,6 @@ fn token_decimals(addr: AccountId32, asset_id: AssetId) -> u8 { decoded::(result) } -fn asset_exists(addr: AccountId32, asset_id: AssetId) -> bool { - let function = function_selector("asset_exists"); - let params = [function, asset_id.encode()].concat(); - let result = do_bare_call(addr, params, 0).expect("should work"); - decoded::(result) -} - -fn create( - addr: AccountId32, - asset_id: AssetId, - admin: AccountId32, - min_balance: Balance, -) -> ExecReturnValue { - let function = function_selector("create"); - let params = [function, asset_id.encode(), admin.encode(), min_balance.encode()].concat(); - do_bare_call(addr, params, 0).expect("should work") -} - -fn set_metadata( - addr: AccountId32, - asset_id: AssetId, - name: Vec, - symbol: Vec, - decimals: u8, -) -> ExecReturnValue { - let function = function_selector("set_metadata"); - let params = - [function, asset_id.encode(), name.encode(), symbol.encode(), decimals.encode()].concat(); - do_bare_call(addr, params, 0).expect("should work") -} - fn transfer( addr: AccountId32, asset_id: AssetId, @@ -137,18 +106,49 @@ fn increase_allowance( result } -fn create_asset(owner: AccountId32, asset_id: AssetId, min_balance: Balance) -> AssetId { - assert_eq!( - Assets::create( - RuntimeOrigin::signed(owner.clone()), - asset_id.into(), - owner.into(), - min_balance - ), - Ok(()) - ); - asset_id -} +// fn asset_exists(addr: AccountId32, asset_id: AssetId) -> bool { +// let function = function_selector("asset_exists"); +// let params = [function, asset_id.encode()].concat(); +// let result = do_bare_call(addr, params, 0).expect("should work"); +// decoded::(result) +// } +// +// fn create( +// addr: AccountId32, +// asset_id: AssetId, +// admin: AccountId32, +// min_balance: Balance, +// ) -> ExecReturnValue { +// let function = function_selector("create"); +// let params = [function, asset_id.encode(), admin.encode(), min_balance.encode()].concat(); +// do_bare_call(addr, params, 0).expect("should work") +// } +// +// fn set_metadata( +// addr: AccountId32, +// asset_id: AssetId, +// name: Vec, +// symbol: Vec, +// decimals: u8, +// ) -> ExecReturnValue { +// let function = function_selector("set_metadata"); +// let params = +// [function, asset_id.encode(), name.encode(), symbol.encode(), decimals.encode()].concat(); +// do_bare_call(addr, params, 0).expect("should work") +// } +// +// fn create_asset(owner: AccountId32, asset_id: AssetId, min_balance: Balance) -> AssetId { +// assert_eq!( +// Assets::create( +// RuntimeOrigin::signed(owner.clone()), +// asset_id.into(), +// owner.into(), +// min_balance +// ), +// Ok(()) +// ); +// asset_id +// } fn mint_asset(owner: AccountId32, asset_id: AssetId, to: AccountId32, value: Balance) -> AssetId { assert_eq!( @@ -393,52 +393,52 @@ fn transfer_works() { #[test] #[ignore] -fn increase_allowance_works() { +fn transfer_from_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 0, vec![]); + let addr = + instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); let amount: Balance = 100 * UNIT; - let asset = 0; - create_asset_and_mint_to(ALICE, asset, addr.clone(), amount); - assert_eq!( - decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), - ConsumerRemaining - ); - let addr = - instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![1]); // Asset does not exist. - let asset = 1; assert_eq!( - decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), + decoded::(transfer(addr.clone(), 1, BOB, amount,)), Module { index: 52, error: 3 }, ); // Create asset with Alice as owner and mint `amount` to contract address. - create_asset_and_mint_to(ALICE, asset, addr.clone(), amount); + let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); // Asset is not live, i.e. frozen or being destroyed. freeze_asset(ALICE, asset); assert_eq!( - decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), + decoded::(transfer(addr.clone(), asset, BOB, amount,)), Module { index: 52, error: 16 }, ); thaw_asset(ALICE, asset); - // Successful approval. - assert_eq!(0, Assets::allowance(asset, &addr, &BOB)); - assert!( - !increase_allowance(addr.clone(), asset, BOB, amount).did_revert(), - "Contract reverted!" + // Not enough balance. + assert_eq!( + decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), + Module { index: 52, error: 0 }, ); - assert_eq!(Assets::allowance(asset, &addr, &BOB), amount); - // Additive. - assert!( - !increase_allowance(addr.clone(), asset, BOB, amount).did_revert(), - "Contract reverted!" + // Not enough balance due to ED. + assert_eq!( + decoded::(transfer(addr.clone(), asset, BOB, amount)), + Module { index: 52, error: 0 }, + ); + // Successful transfer. + let bob_balance_before_mint = Assets::balance(asset, &BOB); + let result = transfer(addr.clone(), asset, BOB, amount / 2); + assert!(!result.did_revert(), "Contract reverted!"); + let bob_balance_after_mint = Assets::balance(asset, &BOB); + assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount / 2); + // Transfer asset to account that does not exist. + assert_eq!( + decoded::(transfer(addr.clone(), asset, FERDIE, amount / 4)), + Token(CannotCreate) ); - assert_eq!(Assets::allowance(asset, &addr, &BOB), amount * 2); // Asset is not live, i.e. frozen or being destroyed. start_destroy_asset(ALICE, asset); assert_eq!( - decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), + decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), Module { index: 52, error: 16 }, ); }); @@ -446,52 +446,52 @@ fn increase_allowance_works() { #[test] #[ignore] -fn transfer_from_works() { +fn increase_allowance_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = - instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); + let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 0, vec![]); let amount: Balance = 100 * UNIT; + let asset = 0; + create_asset_and_mint_to(ALICE, asset, addr.clone(), amount); + assert_eq!( + decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), + ConsumerRemaining + ); + let addr = + instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![1]); // Asset does not exist. + let asset = 1; assert_eq!( - decoded::(transfer(addr.clone(), 1, BOB, amount,)), + decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), Module { index: 52, error: 3 }, ); // Create asset with Alice as owner and mint `amount` to contract address. - let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); + create_asset_and_mint_to(ALICE, asset, addr.clone(), amount); // Asset is not live, i.e. frozen or being destroyed. freeze_asset(ALICE, asset); assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount,)), + decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), Module { index: 52, error: 16 }, ); thaw_asset(ALICE, asset); - // Not enough balance. - assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), - Module { index: 52, error: 0 }, - ); - // Not enough balance due to ED. - assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount)), - Module { index: 52, error: 0 }, + // Successful approval. + assert_eq!(0, Assets::allowance(asset, &addr, &BOB)); + assert!( + !increase_allowance(addr.clone(), asset, BOB, amount).did_revert(), + "Contract reverted!" ); - // Successful transfer. - let bob_balance_before_mint = Assets::balance(asset, &BOB); - let result = transfer(addr.clone(), asset, BOB, amount / 2); - assert!(!result.did_revert(), "Contract reverted!"); - let bob_balance_after_mint = Assets::balance(asset, &BOB); - assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount / 2); - // Transfer asset to account that does not exist. - assert_eq!( - decoded::(transfer(addr.clone(), asset, FERDIE, amount / 4)), - Token(CannotCreate) + assert_eq!(Assets::allowance(asset, &addr, &BOB), amount); + // Additive. + assert!( + !increase_allowance(addr.clone(), asset, BOB, amount).did_revert(), + "Contract reverted!" ); + assert_eq!(Assets::allowance(asset, &addr, &BOB), amount * 2); // Asset is not live, i.e. frozen or being destroyed. start_destroy_asset(ALICE, asset); assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), + decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), Module { index: 52, error: 16 }, ); }); @@ -540,132 +540,132 @@ fn token_metadata_works() { }); } -#[test] -#[ignore] -fn asset_exists_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - let addr = - instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); - - // No tokens in circulation. - assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr.clone(), ASSET_ID)); - - // Tokens in circulation. - create_asset(addr.clone(), ASSET_ID, 1); - assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr, ASSET_ID)); - }); -} - -#[test] -#[ignore] -fn mint_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - let addr = - instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); - let amount: Balance = 100 * UNIT; - - // Asset does not exist. - assert_eq!( - decoded::(transfer_from(addr.clone(), 1, None, Some(BOB), amount, &[0u8])), - Token(UnknownAsset) - ); - let asset = create_asset(ALICE, 1, 2); - // Minting can only be done by the owner. - assert_eq!( - decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), - Module { index: 52, error: 2 }, - ); - // Minimum balance of an asset can not be zero. - assert_eq!( - decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), 1, &[0u8])), - Token(BelowMinimum) - ); - let asset = create_asset(addr.clone(), 2, 2); - // Asset is not live, i.e. frozen or being destroyed. - freeze_asset(addr.clone(), asset); - assert_eq!( - decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), - Module { index: 52, error: 16 }, - ); - thaw_asset(addr.clone(), asset); - // Successful mint. - let bob_balance_before_mint = Assets::balance(asset, &BOB); - let result = transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8]); - assert!(!result.did_revert(), "Contract reverted!"); - let bob_balance_after_mint = Assets::balance(asset, &BOB); - assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount); - // Can not mint more tokens than Balance::MAX. - assert_eq!( - decoded::(transfer_from( - addr.clone(), - asset, - None, - Some(BOB), - Balance::MAX, - &[0u8] - )), - Arithmetic(Overflow) - ); - // Asset is not live, i.e. frozen or being destroyed. - start_destroy_asset(addr.clone(), asset); - assert_eq!( - decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), - Module { index: 52, error: 16 }, - ); - }); -} - -#[test] -#[ignore] -fn create_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - // Instantiate a contract without balance (relay token). - let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 0, vec![0]); - // No balance to pay for fees. - assert_eq!( - decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), - Module { index: 10, error: 2 }, - ); - // Instantiate a contract without balance (relay token). - let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 100, vec![2]); - // No balance to pay the deposit. - assert_eq!( - decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), - Module { index: 10, error: 2 }, - ); - // Instantiate a contract with balance. - let addr = - instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![1]); - assert_eq!( - decoded::(create(addr.clone(), ASSET_ID, BOB, 0)), - Module { index: 52, error: 7 }, - ); - create_asset(ALICE, ASSET_ID, 1); - // Asset ID is already taken. - assert_eq!( - decoded::(create(addr.clone(), ASSET_ID, BOB, 1)), - Module { index: 52, error: 5 }, - ); - // The minimal balance for an asset must be non zero. - let new_asset = 2; - let result = create(addr.clone(), new_asset, BOB, 1); - assert!(!result.did_revert(), "Contract reverted!"); - }); -} - -#[test] -#[ignore] -fn set_metadata_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - let addr = - instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); - - create_asset(addr.clone(), ASSET_ID, 1); - let result = set_metadata(addr.clone(), ASSET_ID, vec![12], vec![12], 12); - assert!(!result.did_revert(), "Contract reverted!"); - }); -} +// #[test] +// #[ignore] +// fn asset_exists_works() { +// new_test_ext().execute_with(|| { +// let _ = env_logger::try_init(); +// let addr = +// instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); +// +// // No tokens in circulation. +// assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr.clone(), ASSET_ID)); +// +// // Tokens in circulation. +// create_asset(addr.clone(), ASSET_ID, 1); +// assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr, ASSET_ID)); +// }); +// } + +// #[test] +// #[ignore] +// fn mint_works() { +// new_test_ext().execute_with(|| { +// let _ = env_logger::try_init(); +// let addr = +// instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); +// let amount: Balance = 100 * UNIT; +// +// // Asset does not exist. +// assert_eq!( +// decoded::(transfer_from(addr.clone(), 1, None, Some(BOB), amount, &[0u8])), +// Token(UnknownAsset) +// ); +// let asset = create_asset(ALICE, 1, 2); +// // Minting can only be done by the owner. +// assert_eq!( +// decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), +// Module { index: 52, error: 2 }, +// ); +// // Minimum balance of an asset can not be zero. +// assert_eq!( +// decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), 1, &[0u8])), +// Token(BelowMinimum) +// ); +// let asset = create_asset(addr.clone(), 2, 2); +// // Asset is not live, i.e. frozen or being destroyed. +// freeze_asset(addr.clone(), asset); +// assert_eq!( +// decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), +// Module { index: 52, error: 16 }, +// ); +// thaw_asset(addr.clone(), asset); +// // Successful mint. +// let bob_balance_before_mint = Assets::balance(asset, &BOB); +// let result = transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8]); +// assert!(!result.did_revert(), "Contract reverted!"); +// let bob_balance_after_mint = Assets::balance(asset, &BOB); +// assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount); +// // Can not mint more tokens than Balance::MAX. +// assert_eq!( +// decoded::(transfer_from( +// addr.clone(), +// asset, +// None, +// Some(BOB), +// Balance::MAX, +// &[0u8] +// )), +// Arithmetic(Overflow) +// ); +// // Asset is not live, i.e. frozen or being destroyed. +// start_destroy_asset(addr.clone(), asset); +// assert_eq!( +// decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), +// Module { index: 52, error: 16 }, +// ); +// }); +// } + +// #[test] +// #[ignore] +// fn create_works() { +// new_test_ext().execute_with(|| { +// let _ = env_logger::try_init(); +// // Instantiate a contract without balance (relay token). +// let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 0, vec![0]); +// // No balance to pay for fees. +// assert_eq!( +// decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), +// Module { index: 10, error: 2 }, +// ); +// // Instantiate a contract without balance (relay token). +// let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 100, vec![2]); +// // No balance to pay the deposit. +// assert_eq!( +// decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), +// Module { index: 10, error: 2 }, +// ); +// // Instantiate a contract with balance. +// let addr = +// instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![1]); +// assert_eq!( +// decoded::(create(addr.clone(), ASSET_ID, BOB, 0)), +// Module { index: 52, error: 7 }, +// ); +// create_asset(ALICE, ASSET_ID, 1); +// // Asset ID is already taken. +// assert_eq!( +// decoded::(create(addr.clone(), ASSET_ID, BOB, 1)), +// Module { index: 52, error: 5 }, +// ); +// // The minimal balance for an asset must be non zero. +// let new_asset = 2; +// let result = create(addr.clone(), new_asset, BOB, 1); +// assert!(!result.did_revert(), "Contract reverted!"); +// }); +// } + +// #[test] +// #[ignore] +// fn set_metadata_works() { +// new_test_ext().execute_with(|| { +// let _ = env_logger::try_init(); +// let addr = +// instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); +// +// create_asset(addr.clone(), ASSET_ID, 1); +// let result = set_metadata(addr.clone(), ASSET_ID, vec![12], vec![12], 12); +// assert!(!result.did_revert(), "Contract reverted!"); +// }); +// } diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index cb27b17f..9e1deefe 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -26,6 +26,8 @@ pub type Result = core::result::Result; #[ink::scale_derive(Encode, Decode, TypeInfo)] pub struct StatusCode(pub u32); +pub(crate) const DECODING_FAILED: u32 = 255; + impl From for StatusCode { fn from(value: u32) -> Self { StatusCode(value) @@ -42,7 +44,7 @@ impl FromStatusCode for StatusCode { impl From for StatusCode { fn from(_: ink::scale::Error) -> Self { - StatusCode(255u32) + StatusCode(DECODING_FAILED) } } diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index ea8991a7..4d942dc6 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1,6 +1,6 @@ use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec, scale::Decode}; -use crate::{primitives::AssetId, AccountId, Balance, Result, StatusCode}; +use crate::{primitives::AssetId, AccountId, Balance, Result, StatusCode, DECODING_FAILED}; pub mod fungibles; @@ -197,7 +197,7 @@ pub fn total_supply(id: AssetId) -> Result { .output::>, true>() .handle_error_code::() .call(&(id)) - .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) + .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } #[inline] @@ -212,7 +212,7 @@ pub fn balance_of(id: AssetId, owner: AccountId) -> Result { .output::>, true>() .handle_error_code::() .call(&(id, owner)) - .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) + .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } #[inline] @@ -222,7 +222,7 @@ pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result>, true>() .handle_error_code::() .call(&(id, owner, spender)) - .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) + .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } #[inline] @@ -237,7 +237,7 @@ pub fn token_name(id: AssetId) -> Result> { .output::>, true>() .handle_error_code::() .call(&(id)) - .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) + .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } // #[inline] @@ -252,7 +252,7 @@ pub fn token_symbol(id: AssetId) -> Result> { .output::>, true>() .handle_error_code::() .call(&(id)) - .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) + .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } #[inline] @@ -267,7 +267,7 @@ pub fn token_decimals(id: AssetId) -> Result { .output::>, true>() .handle_error_code::() .call(&(id)) - .and_then(|v| ::decode(&mut &v[..]).map_err(|_e| StatusCode(255u32))) + .and_then(|v| ::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } // pub(crate) fn asset_exists(id: AssetId) -> Result { diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 9d31653a..c0d9f0d3 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -95,7 +95,7 @@ pub mod v0 { // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). encoded_error.resize(4, 0); u32::from_le_bytes( - encoded_error.try_into().expect("qid, resized to 4 bytes line above"), + encoded_error.try_into().expect("qed, resized to 4 bytes line above"), ) } } diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs index c6bab850..f126031b 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions/mod.rs @@ -313,7 +313,7 @@ pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { }; // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). encoded_error.resize(4, 0); - let mut encoded_error = encoded_error.try_into().expect("qid, resized to 4 bytes line above"); + let mut encoded_error = encoded_error.try_into().expect("qed, resized to 4 bytes line above"); match version { // If an unknown variant of the `DispatchError` is detected the error needs to be converted // into the encoded value of `Error::Other`. This conversion is performed by shifting the bytes one From b591222168e40099c3d2b759da9765f45ee0a488 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Wed, 17 Jul 2024 18:00:11 +0200 Subject: [PATCH 22/76] style: renaming --- pop-api/examples/fungibles/lib.rs | 2 +- pop-api/integration-tests/src/lib.rs | 2 +- .../integration-tests/src/local_fungibles.rs | 50 +++++++++---------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs index 2abc422c..1b42fec4 100755 --- a/pop-api/examples/fungibles/lib.rs +++ b/pop-api/examples/fungibles/lib.rs @@ -71,7 +71,7 @@ mod fungibles { from: AccountId, to: AccountId, value: Balance, - // In the standard a `[u8]`, but the size needs to be known at compile time. + // In the PSP-22 standard a `[u8]`, but the size needs to be known at compile time. _data: Vec, ) -> Result<()> { api::transfer_from(id, from, to, value) diff --git a/pop-api/integration-tests/src/lib.rs b/pop-api/integration-tests/src/lib.rs index c6732d91..8c967855 100644 --- a/pop-api/integration-tests/src/lib.rs +++ b/pop-api/integration-tests/src/lib.rs @@ -59,7 +59,7 @@ fn function_selector(name: &str) -> Vec { [hash[0..4].to_vec()].concat() } -fn do_bare_call( +fn bare_call( addr: AccountId32, input: Vec, value: u128, diff --git a/pop-api/integration-tests/src/local_fungibles.rs b/pop-api/integration-tests/src/local_fungibles.rs index bef3aa6a..f39c2327 100644 --- a/pop-api/integration-tests/src/local_fungibles.rs +++ b/pop-api/integration-tests/src/local_fungibles.rs @@ -18,7 +18,7 @@ fn decoded(result: ExecReturnValue) -> T { fn total_supply(addr: AccountId32, asset_id: AssetId) -> Balance { let function = function_selector("total_supply"); let params = [function, asset_id.encode()].concat(); - let result = do_bare_call(addr, params, 0).expect("should work"); + let result = bare_call(addr, params, 0).expect("should work"); decoded::(result) } @@ -26,7 +26,7 @@ fn total_supply(addr: AccountId32, asset_id: AssetId) -> Balance { fn balance_of(addr: AccountId32, asset_id: AssetId, owner: AccountId32) -> Balance { let function = function_selector("balance_of"); let params = [function, asset_id.encode(), owner.encode()].concat(); - let result = do_bare_call(addr, params, 0).expect("should work"); + let result = bare_call(addr, params, 0).expect("should work"); decoded::(result) } @@ -39,7 +39,7 @@ fn allowance( ) -> Balance { let function = function_selector("allowance"); let params = [function, asset_id.encode(), owner.encode(), spender.encode()].concat(); - let result = do_bare_call(addr, params, 0).expect("should work"); + let result = bare_call(addr, params, 0).expect("should work"); decoded::(result) } @@ -47,7 +47,7 @@ fn allowance( fn token_name(addr: AccountId32, asset_id: AssetId) -> Vec { let function = function_selector("token_name"); let params = [function, asset_id.encode()].concat(); - let result = do_bare_call(addr, params, 0).expect("should work"); + let result = bare_call(addr, params, 0).expect("should work"); decoded::>(result) } @@ -55,7 +55,7 @@ fn token_name(addr: AccountId32, asset_id: AssetId) -> Vec { fn token_symbol(addr: AccountId32, asset_id: AssetId) -> Vec { let function = function_selector("token_symbol"); let params = [function, asset_id.encode()].concat(); - let result = do_bare_call(addr, params, 0).expect("should work"); + let result = bare_call(addr, params, 0).expect("should work"); decoded::>(result) } @@ -63,7 +63,7 @@ fn token_symbol(addr: AccountId32, asset_id: AssetId) -> Vec { fn token_decimals(addr: AccountId32, asset_id: AssetId) -> u8 { let function = function_selector("token_decimals"); let params = [function, asset_id.encode()].concat(); - let result = do_bare_call(addr, params, 0).expect("should work"); + let result = bare_call(addr, params, 0).expect("should work"); decoded::(result) } @@ -75,7 +75,7 @@ fn transfer( ) -> ExecReturnValue { let function = function_selector("transfer"); let params = [function, asset_id.encode(), to.encode(), value.encode()].concat(); - let result = do_bare_call(addr, params, 0).expect("should work"); + let result = bare_call(addr, params, 0).expect("should work"); result } @@ -91,7 +91,7 @@ fn transfer_from( let params = [function, asset_id.encode(), from.encode(), to.encode(), value.encode(), data.encode()] .concat(); - do_bare_call(addr, params, 0).expect("should work") + bare_call(addr, params, 0).expect("should work") } fn increase_allowance( @@ -102,14 +102,14 @@ fn increase_allowance( ) -> ExecReturnValue { let function = function_selector("increase_allowance"); let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat(); - let result = do_bare_call(addr, params, 0).expect("should work"); + let result = bare_call(addr, params, 0).expect("should work"); result } // fn asset_exists(addr: AccountId32, asset_id: AssetId) -> bool { // let function = function_selector("asset_exists"); // let params = [function, asset_id.encode()].concat(); -// let result = do_bare_call(addr, params, 0).expect("should work"); +// let result = bare_call(addr, params, 0).expect("should work"); // decoded::(result) // } // @@ -121,7 +121,7 @@ fn increase_allowance( // ) -> ExecReturnValue { // let function = function_selector("create"); // let params = [function, asset_id.encode(), admin.encode(), min_balance.encode()].concat(); -// do_bare_call(addr, params, 0).expect("should work") +// bare_call(addr, params, 0).expect("should work") // } // // fn set_metadata( @@ -134,22 +134,22 @@ fn increase_allowance( // let function = function_selector("set_metadata"); // let params = // [function, asset_id.encode(), name.encode(), symbol.encode(), decimals.encode()].concat(); -// do_bare_call(addr, params, 0).expect("should work") -// } -// -// fn create_asset(owner: AccountId32, asset_id: AssetId, min_balance: Balance) -> AssetId { -// assert_eq!( -// Assets::create( -// RuntimeOrigin::signed(owner.clone()), -// asset_id.into(), -// owner.into(), -// min_balance -// ), -// Ok(()) -// ); -// asset_id +// bare_call(addr, params, 0).expect("should work") // } +fn create_asset(owner: AccountId32, asset_id: AssetId, min_balance: Balance) -> AssetId { + assert_eq!( + Assets::create( + RuntimeOrigin::signed(owner.clone()), + asset_id.into(), + owner.into(), + min_balance + ), + Ok(()) + ); + asset_id +} + fn mint_asset(owner: AccountId32, asset_id: AssetId, to: AccountId32, value: Balance) -> AssetId { assert_eq!( Assets::mint(RuntimeOrigin::signed(owner.clone()), asset_id.into(), to.into(), value), From c2acbb57ae9be348a9b4b0c2d7e81330b3a8ef47 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Thu, 18 Jul 2024 11:43:37 +0200 Subject: [PATCH 23/76] refactor: bits and pieces --- pop-api/integration-tests/src/lib.rs | 7 +- .../integration-tests/src/local_fungibles.rs | 78 ++++++++----------- pop-api/src/lib.rs | 36 ++++----- pop-api/src/primitives.rs | 6 ++ pop-api/src/v0/assets/fungibles.rs | 58 +++++++++----- pop-api/src/v0/assets/mod.rs | 12 +-- pop-api/src/v0/mod.rs | 17 ++++ pop-api/src/v0/nfts.rs | 3 - primitives/src/lib.rs | 28 +++---- runtime/devnet/src/extensions/mod.rs | 2 +- runtime/testnet/src/extensions.rs | 4 +- 11 files changed, 138 insertions(+), 113 deletions(-) diff --git a/pop-api/integration-tests/src/lib.rs b/pop-api/integration-tests/src/lib.rs index 8c967855..ea5ccb05 100644 --- a/pop-api/integration-tests/src/lib.rs +++ b/pop-api/integration-tests/src/lib.rs @@ -1,6 +1,7 @@ #![cfg(test)] use frame_support::{ + assert_ok, traits::fungibles::{ approvals::Inspect as ApprovalInspect, metadata::Inspect as MetadataInspect, Inspect, }, @@ -17,17 +18,17 @@ use pop_runtime_devnet::{ mod local_fungibles; -type Balance = u128; type AssetId = u32; -const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug; +type Balance = u128; const ALICE: AccountId32 = AccountId32::new([1_u8; 32]); const BOB: AccountId32 = AccountId32::new([2_u8; 32]); +const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug; // FERDIE has no initial balance. const FERDIE: AccountId32 = AccountId32::new([3_u8; 32]); +const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024); const INIT_AMOUNT: Balance = 100_000_000 * UNIT; const INIT_VALUE: Balance = 100 * UNIT; -const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024); fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::::default() diff --git a/pop-api/integration-tests/src/local_fungibles.rs b/pop-api/integration-tests/src/local_fungibles.rs index f39c2327..8f0384c0 100644 --- a/pop-api/integration-tests/src/local_fungibles.rs +++ b/pop-api/integration-tests/src/local_fungibles.rs @@ -138,23 +138,22 @@ fn increase_allowance( // } fn create_asset(owner: AccountId32, asset_id: AssetId, min_balance: Balance) -> AssetId { - assert_eq!( - Assets::create( - RuntimeOrigin::signed(owner.clone()), - asset_id.into(), - owner.into(), - min_balance - ), - Ok(()) - ); + assert_ok!(Assets::create( + RuntimeOrigin::signed(owner.clone()), + asset_id.into(), + owner.into(), + min_balance + )); asset_id } fn mint_asset(owner: AccountId32, asset_id: AssetId, to: AccountId32, value: Balance) -> AssetId { - assert_eq!( - Assets::mint(RuntimeOrigin::signed(owner.clone()), asset_id.into(), to.into(), value), - Ok(()) - ); + assert_ok!(Assets::mint( + RuntimeOrigin::signed(owner.clone()), + asset_id.into(), + to.into(), + value + )); asset_id } @@ -178,30 +177,27 @@ fn create_asset_mint_and_approve( approve: Balance, ) { create_asset_and_mint_to(owner.clone(), asset_id, to.clone(), mint); - assert_eq!( - Assets::approve_transfer( - RuntimeOrigin::signed(to.into()), - asset_id.into(), - spender.into(), - approve, - ), - Ok(()) - ); + assert_ok!(Assets::approve_transfer( + RuntimeOrigin::signed(to.into()), + asset_id.into(), + spender.into(), + approve, + )); } // Freeze an asset. fn freeze_asset(owner: AccountId32, asset_id: AssetId) { - assert_eq!(Assets::freeze_asset(RuntimeOrigin::signed(owner.into()), asset_id.into()), Ok(())); + assert_ok!(Assets::freeze_asset(RuntimeOrigin::signed(owner.into()), asset_id.into())); } // Thaw an asset. fn thaw_asset(owner: AccountId32, asset_id: AssetId) { - assert_eq!(Assets::thaw_asset(RuntimeOrigin::signed(owner.into()), asset_id.into()), Ok(())); + assert_ok!(Assets::thaw_asset(RuntimeOrigin::signed(owner.into()), asset_id.into())); } // Start destroying an asset. fn start_destroy_asset(owner: AccountId32, asset_id: AssetId) { - assert_eq!(Assets::start_destroy(RuntimeOrigin::signed(owner.into()), asset_id.into()), Ok(())); + assert_ok!(Assets::start_destroy(RuntimeOrigin::signed(owner.into()), asset_id.into())); } // Create an asset and set metadata. @@ -212,15 +208,12 @@ fn create_asset_and_set_metadata( symbol: Vec, decimals: u8, ) { - assert_eq!( - Assets::create( - RuntimeOrigin::signed(owner.clone()), - asset_id.into(), - owner.clone().into(), - 100 - ), - Ok(()) - ); + assert_ok!(Assets::create( + RuntimeOrigin::signed(owner.clone()), + asset_id.into(), + owner.clone().into(), + 100 + )); set_metadata_asset(owner, asset_id, name, symbol, decimals); } @@ -232,16 +225,13 @@ fn set_metadata_asset( symbol: Vec, decimals: u8, ) { - assert_eq!( - Assets::set_metadata( - RuntimeOrigin::signed(owner.into()), - asset_id.into(), - name, - symbol, - decimals - ), - Ok(()) - ); + assert_ok!(Assets::set_metadata( + RuntimeOrigin::signed(owner.into()), + asset_id.into(), + name, + symbol, + decimals + )); } fn token_name_asset(asset_id: AssetId) -> Vec { diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 9e1deefe..8d865f0b 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -1,7 +1,8 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -use ink::env::{chain_extension::FromStatusCode, DefaultEnvironment, Environment}; -use primitives::error::Error; +use ink::env::chain_extension::FromStatusCode; + +use constants::DECODING_FAILED; #[cfg(feature = "assets")] pub use v0::assets; @@ -15,18 +16,25 @@ pub use v0::nfts; pub mod primitives; pub mod v0; -type AccountId = ::AccountId; -type Balance = ::Balance; -#[cfg(any(feature = "nfts", feature = "cross-chain"))] -type BlockNumber = ::BlockNumber; - pub type Result = core::result::Result; #[derive(Debug, Copy, Clone, PartialEq, Eq)] #[ink::scale_derive(Encode, Decode, TypeInfo)] pub struct StatusCode(pub u32); -pub(crate) const DECODING_FAILED: u32 = 255; +mod constants { + // Errors: + pub(crate) const DECODING_FAILED: u32 = 255; + pub(crate) const MODULE_ERROR: u8 = 3; + + // Function IDs: + pub(crate) const DISPATCH: u8 = 0; + pub(crate) const READ_STATE: u8 = 1; + + // Modules: + pub(crate) const ASSETS_MODULE: u8 = 52; + pub(crate) const BALANCES_MODULE: u8 = 10; +} impl From for StatusCode { fn from(value: u32) -> Self { @@ -47,15 +55,3 @@ impl From for StatusCode { StatusCode(DECODING_FAILED) } } - -impl From for Error { - fn from(value: StatusCode) -> Self { - value.0.into() - } -} - -impl From for StatusCode { - fn from(value: Error) -> Self { - StatusCode::from(u32::from(value)) - } -} diff --git a/pop-api/src/primitives.rs b/pop-api/src/primitives.rs index e174a111..b451ce79 100644 --- a/pop-api/src/primitives.rs +++ b/pop-api/src/primitives.rs @@ -1 +1,7 @@ +use ink::env::{DefaultEnvironment, Environment}; pub use pop_primitives::*; + +pub(crate) type AccountId = ::AccountId; +pub(crate) type Balance = ::Balance; +#[cfg(any(feature = "nfts", feature = "cross-chain"))] +type BlockNumber = ::BlockNumber; diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index 4abd46a6..8bc7d807 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -1,6 +1,11 @@ use ink::prelude::vec::Vec; -use crate::{assets, primitives::AssetId, AccountId, Balance, Result, StatusCode}; +use crate::{ + assets, + constants::{ASSETS_MODULE, BALANCES_MODULE, MODULE_ERROR}, + primitives::{AccountId, AssetId, Balance}, + Result, StatusCode, +}; /// Local Fungibles: /// 1. PSP-22 Interface @@ -101,7 +106,7 @@ pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, value: Balance /// Returns `Ok(())` if successful, or an error if the approval fails. #[inline] pub fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { - assets::cancel_approval(id, spender)?; + assets::cancel_approval(id, spender.clone())?; assets::approve_transfer(id, spender, value) } @@ -312,15 +317,15 @@ impl From for FungiblesError { let encoded = value.0.to_le_bytes(); match encoded { // Balances. - [3, 10, 2, _] => FungiblesError::NoBalance, + [MODULE_ERROR, BALANCES_MODULE, 2, _] => FungiblesError::NoBalance, // Assets. - [3, 52, 0, _] => FungiblesError::NoAccount, - [3, 52, 1, _] => FungiblesError::NoPermission, - [3, 52, 2, _] => FungiblesError::Unknown, - [3, 52, 3, _] => FungiblesError::InUse, - [3, 52, 5, _] => FungiblesError::MinBalanceZero, - [3, 52, 7, _] => FungiblesError::InsufficientAllowance, - [3, 52, 10, _] => FungiblesError::AssetNotLive, + [MODULE_ERROR, ASSETS_MODULE, 0, _] => FungiblesError::NoAccount, + [MODULE_ERROR, ASSETS_MODULE, 1, _] => FungiblesError::NoPermission, + [MODULE_ERROR, ASSETS_MODULE, 2, _] => FungiblesError::Unknown, + [MODULE_ERROR, ASSETS_MODULE, 3, _] => FungiblesError::InUse, + [MODULE_ERROR, ASSETS_MODULE, 5, _] => FungiblesError::MinBalanceZero, + [MODULE_ERROR, ASSETS_MODULE, 7, _] => FungiblesError::InsufficientAllowance, + [MODULE_ERROR, ASSETS_MODULE, 10, _] => FungiblesError::AssetNotLive, _ => FungiblesError::Other(value), } } @@ -328,6 +333,7 @@ impl From for FungiblesError { #[cfg(test)] mod tests { + use crate::constants::{ASSETS_MODULE, BALANCES_MODULE}; use ink::scale::Decode; use super::FungiblesError; @@ -359,7 +365,7 @@ mod tests { #[test] fn conversion_status_code_into_fungibles_error_works() { - let errors = vec![ + let other_errors = vec![ Other { dispatch_error_index: 5, error_index: 5, error: 1 }, CannotLookup, BadOrigin, @@ -378,30 +384,42 @@ mod tests { UnknownFunctionId, DecodingFailed, ]; - for error in errors { + for error in other_errors { let status_code: StatusCode = error.into(); let fungibles_error: FungiblesError = status_code.into(); assert_eq!(fungibles_error, FungiblesError::Other(status_code)) } - assert_eq!(into_fungibles_error(Module { index: 10, error: 2 }), FungiblesError::NoBalance); - assert_eq!(into_fungibles_error(Module { index: 52, error: 0 }), FungiblesError::NoAccount); assert_eq!( - into_fungibles_error(Module { index: 52, error: 1 }), + into_fungibles_error(Module { index: BALANCES_MODULE, error: 2 }), + FungiblesError::NoBalance + ); + assert_eq!( + into_fungibles_error(Module { index: ASSETS_MODULE, error: 0 }), + FungiblesError::NoAccount + ); + assert_eq!( + into_fungibles_error(Module { index: ASSETS_MODULE, error: 1 }), FungiblesError::NoPermission ); - assert_eq!(into_fungibles_error(Module { index: 52, error: 2 }), FungiblesError::Unknown); - assert_eq!(into_fungibles_error(Module { index: 52, error: 3 }), FungiblesError::InUse); assert_eq!( - into_fungibles_error(Module { index: 52, error: 5 }), + into_fungibles_error(Module { index: ASSETS_MODULE, error: 2 }), + FungiblesError::Unknown + ); + assert_eq!( + into_fungibles_error(Module { index: ASSETS_MODULE, error: 3 }), + FungiblesError::InUse + ); + assert_eq!( + into_fungibles_error(Module { index: ASSETS_MODULE, error: 5 }), FungiblesError::MinBalanceZero ); assert_eq!( - into_fungibles_error(Module { index: 52, error: 7 }), + into_fungibles_error(Module { index: ASSETS_MODULE, error: 7 }), FungiblesError::InsufficientAllowance ); assert_eq!( - into_fungibles_error(Module { index: 52, error: 10 }), + into_fungibles_error(Module { index: ASSETS_MODULE, error: 10 }), FungiblesError::AssetNotLive ); } diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index 4d942dc6..fa7d523b 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1,18 +1,19 @@ use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec, scale::Decode}; -use crate::{primitives::AssetId, AccountId, Balance, Result, StatusCode, DECODING_FAILED}; +use crate::{ + constants::{ASSETS_MODULE, DECODING_FAILED, DISPATCH, READ_STATE}, + primitives::{AccountId, AssetId, Balance}, + v0::VERSION, + Result, StatusCode, +}; pub mod fungibles; -const ASSETS_MODULE: u8 = 52; -const VERSION: u8 = 0; - /// [Pallet Assets](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/assets/src/lib.rs): /// 1. Dispatchables /// 2. Read state functions /// /// 1. Dispatchables within pallet assets (TrustBackedAssets instance): -const DISPATCH: u8 = 0; /// - create /// - start_destroy /// - destroy_accounts @@ -170,7 +171,6 @@ pub fn transfer_approved( } /// 2. Read state functions: -const READ_STATE: u8 = 1; /// - total_supply const TOTAL_SUPPLY: u8 = 0; /// - balance_of diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index 310b360c..56bccfc1 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -1,3 +1,6 @@ +use crate::StatusCode; +use pop_primitives::error::Error; + #[cfg(feature = "assets")] pub mod assets; #[cfg(feature = "balances")] @@ -6,3 +9,17 @@ pub mod balances; pub mod cross_chain; #[cfg(feature = "nfts")] pub mod nfts; + +pub(crate) const VERSION: u8 = 0; + +impl From for Error { + fn from(value: StatusCode) -> Self { + value.0.into() + } +} + +impl From for StatusCode { + fn from(value: Error) -> Self { + StatusCode::from(u32::from(value)) + } +} diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index e111c8dc..63b90a1f 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -6,12 +6,9 @@ pub use primitives::{CollectionId, ItemId}; use scale::Encode; pub use types::*; -type Result = core::result::Result; type StringLimit = u32; type MaxTips = u32; -type Result = core::result::Result; - /// Issue a new collection of non-fungible items pub fn create( admin: impl Into>, diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index c0d9f0d3..3b3ff16d 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -48,34 +48,34 @@ pub mod v0 { error_index: u8, // Index for further nesting, e.g. pallet error. error: u8, - } = 0, + }, /// Failed to lookup some data. - CannotLookup = 1, + CannotLookup, /// A bad origin. - BadOrigin = 2, + BadOrigin, /// A custom error in a module. - Module { index: u8, error: u8 } = 3, + Module { index: u8, error: u8 }, /// At least one consumer is remaining so the account cannot be destroyed. - ConsumerRemaining = 4, + ConsumerRemaining, /// There are no providers so the account cannot be created. - NoProviders = 5, + NoProviders, /// There are too many consumers so the account cannot be created. - TooManyConsumers = 6, + TooManyConsumers, /// An error to do with tokens. - Token(TokenError) = 7, + Token(TokenError), /// An arithmetic error. - Arithmetic(ArithmeticError) = 8, + Arithmetic(ArithmeticError), /// The number of transactional layers has been reached, or we are not in a transactional /// layer. - Transactional(TransactionalError) = 9, + Transactional(TransactionalError), /// Resources exhausted, e.g. attempt to read/write data which is too large to manipulate. - Exhausted = 10, + Exhausted, /// The state is corrupt; this is generally not going to fix itself. - Corruption = 11, + Corruption, /// Some resource (e.g. a preimage) is unavailable right now. This might fix itself later. - Unavailable = 12, + Unavailable, /// Root origin is not allowed. - RootNotAllowed = 13, + RootNotAllowed, /// Unknown function id. UnknownFunctionId = 254, /// Decoding failed on the runtime. diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs index f126031b..720458c8 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions/mod.rs @@ -172,7 +172,7 @@ fn construct_call( params: Vec, ) -> Result { match pallet_index { - 52 => { + index if index == super::Assets::index() as u8 => { let call = versioned_construct_assets_call(version, call_index, params)?; Ok(RuntimeCall::Assets(call)) }, diff --git a/runtime/testnet/src/extensions.rs b/runtime/testnet/src/extensions.rs index 866cb12c..bcb0f835 100644 --- a/runtime/testnet/src/extensions.rs +++ b/runtime/testnet/src/extensions.rs @@ -1,9 +1,9 @@ use cumulus_pallet_parachain_system::RelaychainDataProvider; -use frame_support::traits::{Contains, OriginTrait}; use frame_support::{ dispatch::{GetDispatchInfo, RawOrigin}, pallet_prelude::*, traits::nonfungibles_v2::Inspect, + traits::{Contains, OriginTrait}, }; use pallet_contracts::chain_extension::{ BufInBufOutState, ChainExtension, ChargedAmount, Environment, Ext, InitState, RetVal, @@ -206,7 +206,7 @@ where RuntimeStateKeys::ParachainSystem(key) => { read_parachain_system_state::(key, &mut env) }, - _ => Ok(Vec::from([0u8])), + _ => Ok(Vec::default()), }? .encode(); From 363932022c62e88e28b1360ff3e8c2a6a1aa6ffb Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Thu, 18 Jul 2024 14:50:52 +0200 Subject: [PATCH 24/76] docs: add docs to code --- pop-api/src/lib.rs | 25 +++++++++++++--- pop-api/src/v0/assets/fungibles.rs | 25 ++++++++++++---- primitives/src/lib.rs | 45 ++++++++++++++++++---------- runtime/devnet/src/extensions/mod.rs | 13 ++++++++ 4 files changed, 84 insertions(+), 24 deletions(-) diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 8d865f0b..7a0fe75b 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -16,12 +16,9 @@ pub use v0::nfts; pub mod primitives; pub mod v0; +/// A result type used by the API, with the `StatusCode` as the error type. pub type Result = core::result::Result; -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -#[ink::scale_derive(Encode, Decode, TypeInfo)] -pub struct StatusCode(pub u32); - mod constants { // Errors: pub(crate) const DECODING_FAILED: u32 = 255; @@ -36,12 +33,31 @@ mod constants { pub(crate) const BALANCES_MODULE: u8 = 10; } +/// Represents a status code returned by the runtime. +/// +/// `StatusCode` encapsulates a `u32` value that indicates the status of an operation performed +/// by the runtime. It helps to communicate the success or failure of a chain extension method +/// call to the contract, providing a standardized way to handle errors. +/// +/// This status code can be used to determine if an operation succeeded or if it encountered +/// an error. A `StatusCode` of `0` typically indicates success, while any other value represents +/// an error. +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[ink::scale_derive(Encode, Decode, TypeInfo)] +pub struct StatusCode(pub u32); + impl From for StatusCode { + /// Converts a `u32` into a `StatusCode`. fn from(value: u32) -> Self { StatusCode(value) } } + impl FromStatusCode for StatusCode { + /// Converts a `u32` status code to a `Result`. + /// + /// `Ok(())` if the status code is `0` and `Err(StatusCode(status_code))` for any other status + /// code. fn from_status_code(status_code: u32) -> Result<()> { match status_code { 0 => Ok(()), @@ -51,6 +67,7 @@ impl FromStatusCode for StatusCode { } impl From for StatusCode { + /// Converts a scale decoding error into a `StatusCode` indicating a decoding failure. fn from(_: ink::scale::Error) -> Self { StatusCode(DECODING_FAILED) } diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index 8bc7d807..e3c9b6be 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -284,9 +284,20 @@ pub fn token_decimals(id: AssetId) -> Result { // assets::asset_exists(id) // } +/// Represents various errors related to local fungible assets in the API. +/// +/// The `FungiblesError` provides a detailed and specific set of error types that can occur when +/// interacting with fungible assets through the Pop API. Each variant signifies a particular error +/// condition, facilitating precise error handling and debugging. +/// +/// It is designed to be lightweight, including only the essential errors relevant to fungible asset +/// operations. The `Other` variant serves as a catch-all for any unexpected errors. For more +/// detailed debugging, the `Other` variant can be converted into the richer `Error` type defined in +/// the primitives crate. #[derive(Debug, PartialEq, Eq)] #[ink::scale_derive(Encode, Decode, TypeInfo)] pub enum FungiblesError { + /// An unspecified or unknown error occurred. Other(StatusCode), /// The asset is not live; either frozen or being destroyed. AssetNotLive, @@ -305,14 +316,18 @@ pub enum FungiblesError { /// The given asset ID is unknown. Unknown, /// No balance for creation of assets or fees. - // - // Originally `pallet_balances::Error::InsufficientBalance` but collides with the - // `InsufficientBalance` error that is used for `pallet_assets::Error::BalanceLow` to adhere to - // standard. + // TODO: Originally `pallet_balances::Error::InsufficientBalance` but collides with the + // `InsufficientBalance` error that is used for `pallet_assets::Error::BalanceLow` to adhere to + // standard. This deserves a second look. NoBalance, } impl From for FungiblesError { + /// Converts a `StatusCode` to a `FungiblesError`. + /// + /// This conversion maps a `StatusCode`, returned by the runtime, to a more descriptive + /// `FungiblesError`. This provides better context and understanding of the error, allowing + /// developers to handle the most important errors effectively. fn from(value: StatusCode) -> Self { let encoded = value.0.to_le_bytes(); match encoded { @@ -381,7 +396,7 @@ mod tests { Corruption, Unavailable, RootNotAllowed, - UnknownFunctionId, + UnknownFunctionCall, DecodingFailed, ]; for error in other_errors { diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 3b3ff16d..d177b60f 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -10,20 +10,21 @@ pub use v0::error; pub mod cross_chain; pub mod storage_keys; +/// An opaque 32-byte cryptographic identifier. #[derive(Encode, Decode, Debug, MaxEncodedLen, Eq, PartialEq)] #[cfg_attr(feature = "std", derive(TypeInfo))] pub struct AccountId(pub [u8; 32]); -// Identifier for the class of asset. +/// Identifier for the class of asset. pub type AssetId = u32; #[cfg(feature = "nfts")] pub mod nfts { use bounded_collections::ConstU32; - // Id used for identifying non-fungible collections. + /// Id used for identifying non-fungible collections. pub type CollectionId = u32; - // Id used for identifying non-fungible items. + /// Id used for identifying non-fungible items. pub type ItemId = u32; /// The maximum length of an attribute key. pub type KeyLimit = ConstU32<64>; @@ -36,24 +37,29 @@ pub mod v0 { pub mod error { use super::*; + /// Reason why a Pop API function call failed. #[derive(Encode, Decode, Debug, Eq, PartialEq)] #[cfg_attr(feature = "std", derive(TypeInfo))] #[repr(u8)] pub enum Error { - /// Some unknown error occurred. Go to the Pop API docs section `Pop API error`. - Other { - // Index within the `DispatchError` - dispatch_error_index: u8, - // Index within the `DispatchError` variant. - error_index: u8, - // Index for further nesting, e.g. pallet error. - error: u8, - }, + /// An unknown error occurred. This variant captures any unexpected errors that the + /// contract cannot specifically handle. It is useful for cases where there are breaking + /// changes in the runtime or when an error falls outside the predefined categories. The + /// variant includes: + /// + /// - `dispatch_error_index`: The index within the `DispatchError`. + /// - `error_index`: The index within the `DispatchError` variant (e.g. a `TokenError`). + /// - `error`: The specific error code or sub-index, providing additional context (e.g. + /// `error` in `ModuleError`). + Other { dispatch_error_index: u8, error_index: u8, error: u8 }, /// Failed to lookup some data. CannotLookup, /// A bad origin. BadOrigin, /// A custom error in a module. + /// + /// - `index`: The pallet index. + /// - `error`: The error within the pallet. Module { index: u8, error: u8 }, /// At least one consumer is remaining so the account cannot be destroyed. ConsumerRemaining, @@ -76,13 +82,18 @@ pub mod v0 { Unavailable, /// Root origin is not allowed. RootNotAllowed, - /// Unknown function id. - UnknownFunctionId = 254, - /// Decoding failed on the runtime. + /// Unknown function called. + UnknownFunctionCall = 254, + /// Decoding failed. DecodingFailed = 255, } impl From for Error { + /// Converts a `u32` status code into an `Error`. + /// + /// This conversion maps a raw status code returned by the runtime into the more + /// descriptive `Error` enum variant, providing better context and understanding of the + /// error. fn from(value: u32) -> Self { let encoded = value.to_le_bytes(); Error::decode(&mut &encoded[..]).unwrap_or(Error::DecodingFailed) @@ -90,6 +101,7 @@ pub mod v0 { } impl From for u32 { + /// Converts an `Error` to a `u32` status code. fn from(value: Error) -> Self { let mut encoded_error = value.encode(); // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). @@ -100,6 +112,7 @@ pub mod v0 { } } + /// Description of what went wrong when trying to complete an operation on a token. #[derive(Encode, Decode, Clone, Debug, MaxEncodedLen, Eq, PartialEq, Ord, PartialOrd)] #[cfg_attr(feature = "std", derive(TypeInfo))] pub enum TokenError { @@ -126,6 +139,7 @@ pub mod v0 { Blocked, } + /// Arithmetic errors. #[derive(Encode, Decode, Debug, Eq, PartialEq)] #[cfg_attr(feature = "std", derive(TypeInfo))] pub enum ArithmeticError { @@ -137,6 +151,7 @@ pub mod v0 { DivisionByZero, } + /// Errors related to transactional storage layers. #[derive(Encode, Decode, Debug, Eq, PartialEq)] #[cfg_attr(feature = "std", derive(TypeInfo))] pub enum TransactionalError { diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs index 720458c8..ec2d0bc2 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions/mod.rs @@ -337,6 +337,15 @@ pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { u32::from_le_bytes(encoded_error) } +/// Enum representing the different function identifiers used in the Pop API chain extension. +/// +/// The `FuncId` enum specifies the available functions that can be called through the Pop API chain +/// extension. Each variant corresponds to a specific functionality provided by the chain extension, +/// facilitating the interaction between smart contracts and the runtime. +/// +/// - `Dispatch`: Represents a function call to dispatch a runtime call. +/// - `ReadState`: Represents a function call to read the state from the runtime. +/// - `SendXcm`: Represents a function call to send an XCM message. #[derive(Debug)] pub enum FuncId { Dispatch, @@ -347,6 +356,10 @@ pub enum FuncId { impl TryFrom for FuncId { type Error = DispatchError; + /// Attempts to convert a `u8` value to its corresponding `FuncId` variant. + /// + /// If the `u8` value does not match any known function identifier, it returns a + /// `DispatchError::Other` indicating an unknown function ID. fn try_from(func_id: u8) -> Result { let id = match func_id { 0 => Self::Dispatch, From 81e9844f056123abe88985e64463a753d50f004a Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Thu, 18 Jul 2024 15:05:42 +0200 Subject: [PATCH 25/76] fix: replace test code to api fungibles --- pop-api/src/v0/assets/fungibles.rs | 15 ++++++++++++--- pop-api/src/v0/mod.rs | 6 ------ primitives/src/lib.rs | 12 ------------ 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index e3c9b6be..9084708d 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -349,7 +349,7 @@ impl From for FungiblesError { #[cfg(test)] mod tests { use crate::constants::{ASSETS_MODULE, BALANCES_MODULE}; - use ink::scale::Decode; + use ink::scale::{Decode, Encode}; use super::FungiblesError; use crate::primitives::error::{ @@ -360,8 +360,17 @@ mod tests { }; use crate::StatusCode; + fn error_into_status_code(error: Error) -> StatusCode { + let mut encoded_error = error.encode(); + encoded_error.resize(4, 0); + let value = u32::from_le_bytes( + encoded_error.try_into().expect("qed, resized to 4 bytes line above"), + ); + value.into() + } + fn into_fungibles_error(error: Error) -> FungiblesError { - let status_code: StatusCode = error.into(); + let status_code: StatusCode = error_into_status_code(error); status_code.into() } @@ -400,7 +409,7 @@ mod tests { DecodingFailed, ]; for error in other_errors { - let status_code: StatusCode = error.into(); + let status_code: StatusCode = error_into_status_code(error); let fungibles_error: FungiblesError = status_code.into(); assert_eq!(fungibles_error, FungiblesError::Other(status_code)) } diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index 56bccfc1..8b7a8469 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -17,9 +17,3 @@ impl From for Error { value.0.into() } } - -impl From for StatusCode { - fn from(value: Error) -> Self { - StatusCode::from(u32::from(value)) - } -} diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index d177b60f..b9caf111 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -100,18 +100,6 @@ pub mod v0 { } } - impl From for u32 { - /// Converts an `Error` to a `u32` status code. - fn from(value: Error) -> Self { - let mut encoded_error = value.encode(); - // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). - encoded_error.resize(4, 0); - u32::from_le_bytes( - encoded_error.try_into().expect("qed, resized to 4 bytes line above"), - ) - } - } - /// Description of what went wrong when trying to complete an operation on a token. #[derive(Encode, Decode, Clone, Debug, MaxEncodedLen, Eq, PartialEq, Ord, PartialOrd)] #[cfg_attr(feature = "std", derive(TypeInfo))] From 023d3dcffd96e28fd05dcefe84f088b6b64a4f6a Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Thu, 18 Jul 2024 15:50:44 +0200 Subject: [PATCH 26/76] fix: own review + fixing CI fail --- pop-api/src/lib.rs | 8 +- pop-api/src/primitives.rs | 1 + pop-api/src/v0/assets/fungibles.rs | 57 +++++---- pop-api/src/v0/assets/mod.rs | 140 ++++++++-------------- pop-api/src/v0/mod.rs | 5 +- primitives/src/lib.rs | 6 +- runtime/devnet/src/extensions/mod.rs | 8 +- runtime/devnet/src/extensions/v0/error.rs | 6 +- 8 files changed, 95 insertions(+), 136 deletions(-) diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 7a0fe75b..86621c51 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -29,15 +29,15 @@ mod constants { pub(crate) const READ_STATE: u8 = 1; // Modules: - pub(crate) const ASSETS_MODULE: u8 = 52; - pub(crate) const BALANCES_MODULE: u8 = 10; + pub(crate) const ASSETS: u8 = 52; + pub(crate) const BALANCES: u8 = 10; } /// Represents a status code returned by the runtime. /// /// `StatusCode` encapsulates a `u32` value that indicates the status of an operation performed -/// by the runtime. It helps to communicate the success or failure of a chain extension method -/// call to the contract, providing a standardized way to handle errors. +/// by the runtime. It helps to communicate the success or failure of a Pop API call to the contract, +/// providing a standardized way to handle errors. /// /// This status code can be used to determine if an operation succeeded or if it encountered /// an error. A `StatusCode` of `0` typically indicates success, while any other value represents diff --git a/pop-api/src/primitives.rs b/pop-api/src/primitives.rs index b451ce79..33285044 100644 --- a/pop-api/src/primitives.rs +++ b/pop-api/src/primitives.rs @@ -1,4 +1,5 @@ use ink::env::{DefaultEnvironment, Environment}; + pub use pop_primitives::*; pub(crate) type AccountId = ::AccountId; diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index 9084708d..255e8502 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -2,7 +2,7 @@ use ink::prelude::vec::Vec; use crate::{ assets, - constants::{ASSETS_MODULE, BALANCES_MODULE, MODULE_ERROR}, + constants::{ASSETS, BALANCES, MODULE_ERROR}, primitives::{AccountId, AssetId, Balance}, Result, StatusCode, }; @@ -106,7 +106,7 @@ pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, value: Balance /// Returns `Ok(())` if successful, or an error if the approval fails. #[inline] pub fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { - assets::cancel_approval(id, spender.clone())?; + assets::cancel_approval(id, spender)?; assets::approve_transfer(id, spender, value) } @@ -284,7 +284,7 @@ pub fn token_decimals(id: AssetId) -> Result { // assets::asset_exists(id) // } -/// Represents various errors related to local fungible assets in the API. +/// Represents various errors related to local fungible assets in the Pop API. /// /// The `FungiblesError` provides a detailed and specific set of error types that can occur when /// interacting with fungible assets through the Pop API. Each variant signifies a particular error @@ -332,15 +332,15 @@ impl From for FungiblesError { let encoded = value.0.to_le_bytes(); match encoded { // Balances. - [MODULE_ERROR, BALANCES_MODULE, 2, _] => FungiblesError::NoBalance, + [MODULE_ERROR, BALANCES, 2, _] => FungiblesError::NoBalance, // Assets. - [MODULE_ERROR, ASSETS_MODULE, 0, _] => FungiblesError::NoAccount, - [MODULE_ERROR, ASSETS_MODULE, 1, _] => FungiblesError::NoPermission, - [MODULE_ERROR, ASSETS_MODULE, 2, _] => FungiblesError::Unknown, - [MODULE_ERROR, ASSETS_MODULE, 3, _] => FungiblesError::InUse, - [MODULE_ERROR, ASSETS_MODULE, 5, _] => FungiblesError::MinBalanceZero, - [MODULE_ERROR, ASSETS_MODULE, 7, _] => FungiblesError::InsufficientAllowance, - [MODULE_ERROR, ASSETS_MODULE, 10, _] => FungiblesError::AssetNotLive, + [MODULE_ERROR, ASSETS, 0, _] => FungiblesError::NoAccount, + [MODULE_ERROR, ASSETS, 1, _] => FungiblesError::NoPermission, + [MODULE_ERROR, ASSETS, 2, _] => FungiblesError::Unknown, + [MODULE_ERROR, ASSETS, 3, _] => FungiblesError::InUse, + [MODULE_ERROR, ASSETS, 5, _] => FungiblesError::MinBalanceZero, + [MODULE_ERROR, ASSETS, 7, _] => FungiblesError::InsufficientAllowance, + [MODULE_ERROR, ASSETS, 10, _] => FungiblesError::AssetNotLive, _ => FungiblesError::Other(value), } } @@ -348,17 +348,19 @@ impl From for FungiblesError { #[cfg(test)] mod tests { - use crate::constants::{ASSETS_MODULE, BALANCES_MODULE}; use ink::scale::{Decode, Encode}; use super::FungiblesError; - use crate::primitives::error::{ - ArithmeticError::*, - Error::{self, *}, - TokenError::*, - TransactionalError::*, + use crate::{ + constants::{ASSETS, BALANCES}, + primitives::error::{ + ArithmeticError::*, + Error::{self, *}, + TokenError::*, + TransactionalError::*, + }, + StatusCode, }; - use crate::StatusCode; fn error_into_status_code(error: Error) -> StatusCode { let mut encoded_error = error.encode(); @@ -415,35 +417,32 @@ mod tests { } assert_eq!( - into_fungibles_error(Module { index: BALANCES_MODULE, error: 2 }), + into_fungibles_error(Module { index: BALANCES, error: 2 }), FungiblesError::NoBalance ); assert_eq!( - into_fungibles_error(Module { index: ASSETS_MODULE, error: 0 }), + into_fungibles_error(Module { index: ASSETS, error: 0 }), FungiblesError::NoAccount ); assert_eq!( - into_fungibles_error(Module { index: ASSETS_MODULE, error: 1 }), + into_fungibles_error(Module { index: ASSETS, error: 1 }), FungiblesError::NoPermission ); assert_eq!( - into_fungibles_error(Module { index: ASSETS_MODULE, error: 2 }), + into_fungibles_error(Module { index: ASSETS, error: 2 }), FungiblesError::Unknown ); + assert_eq!(into_fungibles_error(Module { index: ASSETS, error: 3 }), FungiblesError::InUse); assert_eq!( - into_fungibles_error(Module { index: ASSETS_MODULE, error: 3 }), - FungiblesError::InUse - ); - assert_eq!( - into_fungibles_error(Module { index: ASSETS_MODULE, error: 5 }), + into_fungibles_error(Module { index: ASSETS, error: 5 }), FungiblesError::MinBalanceZero ); assert_eq!( - into_fungibles_error(Module { index: ASSETS_MODULE, error: 7 }), + into_fungibles_error(Module { index: ASSETS, error: 7 }), FungiblesError::InsufficientAllowance ); assert_eq!( - into_fungibles_error(Module { index: ASSETS_MODULE, error: 10 }), + into_fungibles_error(Module { index: ASSETS, error: 10 }), FungiblesError::AssetNotLive ); } diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index fa7d523b..00021fc0 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1,9 +1,9 @@ use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec, scale::Decode}; use crate::{ - constants::{ASSETS_MODULE, DECODING_FAILED, DISPATCH, READ_STATE}, + constants::{ASSETS, DECODING_FAILED, DISPATCH, READ_STATE}, primitives::{AccountId, AssetId, Balance}, - v0::VERSION, + v0::V0, Result, StatusCode, }; @@ -92,9 +92,9 @@ const TRANSFER_APPROVED: u8 = 25; #[inline] pub fn transfer_keep_alive(id: AssetId, target: AccountId, amount: Balance) -> Result<()> { ChainExtensionMethod::build(u32::from_le_bytes([ - VERSION, + V0, DISPATCH, - ASSETS_MODULE, + ASSETS, // E.D. is always respected with transferring tokens via the API. TRANSFER_KEEP_ALIVE, ])) @@ -122,31 +122,21 @@ pub fn transfer_keep_alive(id: AssetId, target: AccountId, amount: Balance) -> R /// Approve an amount of asset for transfer by a delegated third-party account. #[inline] pub fn approve_transfer(id: AssetId, delegate: AccountId, amount: Balance) -> Result<()> { - ChainExtensionMethod::build(u32::from_le_bytes([ - VERSION, - DISPATCH, - ASSETS_MODULE, - APPROVE_TRANSFER, - ])) - .input::<(AssetId, AccountId, Balance)>() - .output::, true>() - .handle_error_code::() - .call(&(id, delegate, amount)) + ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, ASSETS, APPROVE_TRANSFER])) + .input::<(AssetId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, delegate, amount)) } /// Cancel all of some asset approved for delegated transfer by a third-party account. #[inline] pub fn cancel_approval(id: AssetId, delegate: AccountId) -> Result<()> { - ChainExtensionMethod::build(u32::from_le_bytes([ - VERSION, - DISPATCH, - ASSETS_MODULE, - CANCEL_APPROVAL, - ])) - .input::<(AssetId, AccountId)>() - .output::, true>() - .handle_error_code::() - .call(&(id, delegate)) + ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, ASSETS, CANCEL_APPROVAL])) + .input::<(AssetId, AccountId)>() + .output::, true>() + .handle_error_code::() + .call(&(id, delegate)) } /// Transfer some asset balance from a previously delegated account to some third-party @@ -158,16 +148,11 @@ pub fn transfer_approved( to: AccountId, amount: Balance, ) -> Result<()> { - ChainExtensionMethod::build(u32::from_le_bytes([ - VERSION, - DISPATCH, - ASSETS_MODULE, - TRANSFER_APPROVED, - ])) - .input::<(AssetId, AccountId, AccountId, Balance)>() - .output::, true>() - .handle_error_code::() - .call(&(id, from, to, amount)) + ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, ASSETS, TRANSFER_APPROVED])) + .input::<(AssetId, AccountId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, from, to, amount)) } /// 2. Read state functions: @@ -187,37 +172,27 @@ const TOKEN_DECIMALS: u8 = 5; #[inline] pub fn total_supply(id: AssetId) -> Result { - ChainExtensionMethod::build(u32::from_le_bytes([ - VERSION, - READ_STATE, - ASSETS_MODULE, - TOTAL_SUPPLY, - ])) - .input::() - .output::>, true>() - .handle_error_code::() - .call(&(id)) - .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) + ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, ASSETS, TOTAL_SUPPLY])) + .input::() + .output::>, true>() + .handle_error_code::() + .call(&(id)) + .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } #[inline] pub fn balance_of(id: AssetId, owner: AccountId) -> Result { - ChainExtensionMethod::build(u32::from_le_bytes([ - VERSION, - READ_STATE, - ASSETS_MODULE, - BALANCE_OF, - ])) - .input::<(AssetId, AccountId)>() - .output::>, true>() - .handle_error_code::() - .call(&(id, owner)) - .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) + ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, ASSETS, BALANCE_OF])) + .input::<(AssetId, AccountId)>() + .output::>, true>() + .handle_error_code::() + .call(&(id, owner)) + .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } #[inline] pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { - ChainExtensionMethod::build(u32::from_le_bytes([VERSION, READ_STATE, ASSETS_MODULE, ALLOWANCE])) + ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, ASSETS, ALLOWANCE])) .input::<(AssetId, AccountId, AccountId)>() .output::>, true>() .handle_error_code::() @@ -227,47 +202,32 @@ pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result Result> { - ChainExtensionMethod::build(u32::from_le_bytes([ - VERSION, - READ_STATE, - ASSETS_MODULE, - TOKEN_NAME, - ])) - .input::() - .output::>, true>() - .handle_error_code::() - .call(&(id)) - .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) + ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, ASSETS, TOKEN_NAME])) + .input::() + .output::>, true>() + .handle_error_code::() + .call(&(id)) + .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } // #[inline] pub fn token_symbol(id: AssetId) -> Result> { - ChainExtensionMethod::build(u32::from_le_bytes([ - VERSION, - READ_STATE, - ASSETS_MODULE, - TOKEN_SYMBOL, - ])) - .input::() - .output::>, true>() - .handle_error_code::() - .call(&(id)) - .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) + ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, ASSETS, TOKEN_SYMBOL])) + .input::() + .output::>, true>() + .handle_error_code::() + .call(&(id)) + .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } #[inline] pub fn token_decimals(id: AssetId) -> Result { - ChainExtensionMethod::build(u32::from_le_bytes([ - VERSION, - READ_STATE, - ASSETS_MODULE, - TOKEN_DECIMALS, - ])) - .input::() - .output::>, true>() - .handle_error_code::() - .call(&(id)) - .and_then(|v| ::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) + ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, ASSETS, TOKEN_DECIMALS])) + .input::() + .output::>, true>() + .handle_error_code::() + .call(&(id)) + .and_then(|v| ::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } // pub(crate) fn asset_exists(id: AssetId) -> Result { diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index 8b7a8469..f7dab6b4 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -1,5 +1,4 @@ -use crate::StatusCode; -use pop_primitives::error::Error; +use crate::{primitives::error::Error, StatusCode}; #[cfg(feature = "assets")] pub mod assets; @@ -10,7 +9,7 @@ pub mod cross_chain; #[cfg(feature = "nfts")] pub mod nfts; -pub(crate) const VERSION: u8 = 0; +pub(crate) const V0: u8 = 0; impl From for Error { fn from(value: StatusCode) -> Self { diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index b9caf111..ec6670ba 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -37,7 +37,7 @@ pub mod v0 { pub mod error { use super::*; - /// Reason why a Pop API function call failed. + /// Reason why a Pop API call failed. #[derive(Encode, Decode, Debug, Eq, PartialEq)] #[cfg_attr(feature = "std", derive(TypeInfo))] #[repr(u8)] @@ -82,8 +82,8 @@ pub mod v0 { Unavailable, /// Root origin is not allowed. RootNotAllowed, - /// Unknown function called. - UnknownFunctionCall = 254, + /// Unknown call. + UnknownCall = 254, /// Decoding failed. DecodingFailed = 255, } diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs index ec2d0bc2..d2bd63e7 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions/mod.rs @@ -337,11 +337,11 @@ pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { u32::from_le_bytes(encoded_error) } -/// Enum representing the different function identifiers used in the Pop API chain extension. +/// Function identifiers used in the Pop API. /// -/// The `FuncId` enum specifies the available functions that can be called through the Pop API chain -/// extension. Each variant corresponds to a specific functionality provided by the chain extension, -/// facilitating the interaction between smart contracts and the runtime. +/// The `FuncId` specifies the available functions that can be called through the Pop API. Each +/// variant corresponds to a specific functionality provided by the API, facilitating the +/// interaction between smart contracts and the runtime. /// /// - `Dispatch`: Represents a function call to dispatch a runtime call. /// - `ReadState`: Represents a function call to read the state from the runtime. diff --git a/runtime/devnet/src/extensions/v0/error.rs b/runtime/devnet/src/extensions/v0/error.rs index b8af2250..b26668f7 100644 --- a/runtime/devnet/src/extensions/v0/error.rs +++ b/runtime/devnet/src/extensions/v0/error.rs @@ -76,7 +76,7 @@ mod tests { DispatchError::Other(""), (Other { dispatch_error_index: 0, error_index: 0, error: 0 }), ), - (DispatchError::Other("UnknownFunctionId"), UnknownFunctionId), + (DispatchError::Other("UnknownFunctionId"), UnknownCall), (DispatchError::Other("DecodingFailed"), DecodingFailed), (DispatchError::CannotLookup, CannotLookup), (DispatchError::BadOrigin, BadOrigin), @@ -120,7 +120,7 @@ mod tests { DispatchError::Other("Random"), (Other { dispatch_error_index: 0, error_index: 0, error: 0 }), ), - (DispatchError::Other("UnknownFunctionId"), UnknownFunctionId), + (DispatchError::Other("UnknownFunctionId"), UnknownCall), (DispatchError::Other("DecodingFailed"), DecodingFailed), ]; for (dispatch_error, expected) in test_cases { @@ -185,7 +185,7 @@ mod tests { Corruption, Unavailable, RootNotAllowed, - UnknownFunctionId, + UnknownCall, DecodingFailed, ]; // Compare an `Error`, which is converted from an encoded value, with the expected `Error`. From e683e160f4a753ecfcd596c823947ab02917c797 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Fri, 19 Jul 2024 10:24:21 +0200 Subject: [PATCH 27/76] refactor: add Error variants index + clippy allowance --- primitives/src/lib.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index ec6670ba..376b440d 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -41,6 +41,7 @@ pub mod v0 { #[derive(Encode, Decode, Debug, Eq, PartialEq)] #[cfg_attr(feature = "std", derive(TypeInfo))] #[repr(u8)] + #[allow(clippy::unnecessary_cast)] pub enum Error { /// An unknown error occurred. This variant captures any unexpected errors that the /// contract cannot specifically handle. It is useful for cases where there are breaking @@ -51,37 +52,37 @@ pub mod v0 { /// - `error_index`: The index within the `DispatchError` variant (e.g. a `TokenError`). /// - `error`: The specific error code or sub-index, providing additional context (e.g. /// `error` in `ModuleError`). - Other { dispatch_error_index: u8, error_index: u8, error: u8 }, + Other { dispatch_error_index: u8, error_index: u8, error: u8 } = 0, /// Failed to lookup some data. - CannotLookup, + CannotLookup = 1, /// A bad origin. - BadOrigin, + BadOrigin = 2, /// A custom error in a module. /// /// - `index`: The pallet index. /// - `error`: The error within the pallet. - Module { index: u8, error: u8 }, + Module { index: u8, error: u8 } = 3, /// At least one consumer is remaining so the account cannot be destroyed. - ConsumerRemaining, + ConsumerRemaining = 4, /// There are no providers so the account cannot be created. - NoProviders, + NoProviders = 5, /// There are too many consumers so the account cannot be created. - TooManyConsumers, + TooManyConsumers = 6, /// An error to do with tokens. - Token(TokenError), + Token(TokenError) = 7, /// An arithmetic error. - Arithmetic(ArithmeticError), + Arithmetic(ArithmeticError) = 8, /// The number of transactional layers has been reached, or we are not in a transactional /// layer. - Transactional(TransactionalError), + Transactional(TransactionalError) = 9, /// Resources exhausted, e.g. attempt to read/write data which is too large to manipulate. - Exhausted, + Exhausted = 10, /// The state is corrupt; this is generally not going to fix itself. - Corruption, + Corruption = 11, /// Some resource (e.g. a preimage) is unavailable right now. This might fix itself later. - Unavailable, + Unavailable = 12, /// Root origin is not allowed. - RootNotAllowed, + RootNotAllowed = 13, /// Unknown call. UnknownCall = 254, /// Decoding failed. From 28d971b11666aa16f3d4fd040f85473ce0d6a401 Mon Sep 17 00:00:00 2001 From: Daan van der Plas <93204684+Daanvdplas@users.noreply.github.com> Date: Fri, 26 Jul 2024 11:00:18 +0200 Subject: [PATCH 28/76] feat: api fungibles pallet (#113) Co-authored-by: Frank Bell --- Cargo.lock | 231 ++--- Cargo.toml | 2 + node/src/command.rs | 13 +- pallets/api/Cargo.toml | 55 ++ pallets/api/src/fungibles/benchmarking.rs | 106 +++ pallets/api/src/fungibles/mod.rs | 240 +++++ pallets/api/src/fungibles/tests.rs | 147 +++ pallets/api/src/fungibles/weights.rs | 94 ++ pallets/api/src/lib.rs | 5 + pallets/api/src/mock.rs | 123 +++ pop-api/Cargo.toml | 10 +- pop-api/examples/fungibles/Cargo.toml | 2 +- pop-api/integration-tests/Cargo.toml | 2 +- .../contracts/fungibles/Cargo.toml | 21 + .../contracts/fungibles/lib.rs | 185 ++++ .../integration-tests/src/local_fungibles.rs | 135 +-- pop-api/src/lib.rs | 14 +- pop-api/src/primitives.rs | 3 - pop-api/src/v0/assets/fungibles.rs | 284 +++--- pop-api/src/v0/assets/mod.rs | 235 +---- pop-api/src/v0/balances.rs | 88 -- pop-api/src/v0/cross_chain/coretime.rs | 11 - pop-api/src/v0/cross_chain/mod.rs | 107 --- pop-api/src/v0/mod.rs | 6 - pop-api/src/v0/nfts.rs | 883 ------------------ primitives/Cargo.toml | 14 +- primitives/README.md | 1 + primitives/src/cross_chain.rs | 19 - primitives/src/lib.rs | 28 +- primitives/src/storage_keys.rs | 56 -- runtime/devnet/Cargo.toml | 8 +- runtime/devnet/src/config/api.rs | 34 + runtime/devnet/src/config/assets.rs | 9 +- runtime/devnet/src/config/mod.rs | 1 + runtime/devnet/src/extensions/mod.rs | 824 ++-------------- .../src/extensions/{v0/error.rs => v0.rs} | 0 runtime/devnet/src/extensions/v0/assets.rs | 76 -- runtime/devnet/src/extensions/v0/mod.rs | 2 - runtime/devnet/src/lib.rs | 101 +- runtime/testnet/Cargo.toml | 5 +- runtime/testnet/src/extensions.rs | 558 +---------- runtime/testnet/src/lib.rs | 41 +- scripts/pallet-weights-template.hbs | 122 +++ 43 files changed, 1610 insertions(+), 3291 deletions(-) create mode 100644 pallets/api/Cargo.toml create mode 100644 pallets/api/src/fungibles/benchmarking.rs create mode 100644 pallets/api/src/fungibles/mod.rs create mode 100644 pallets/api/src/fungibles/tests.rs create mode 100644 pallets/api/src/fungibles/weights.rs create mode 100644 pallets/api/src/lib.rs create mode 100644 pallets/api/src/mock.rs create mode 100755 pop-api/integration-tests/contracts/fungibles/Cargo.toml create mode 100755 pop-api/integration-tests/contracts/fungibles/lib.rs delete mode 100644 pop-api/src/v0/balances.rs delete mode 100644 pop-api/src/v0/cross_chain/coretime.rs delete mode 100644 pop-api/src/v0/cross_chain/mod.rs delete mode 100644 pop-api/src/v0/nfts.rs create mode 100644 primitives/README.md delete mode 100644 primitives/src/cross_chain.rs delete mode 100644 primitives/src/storage_keys.rs create mode 100644 runtime/devnet/src/config/api.rs rename runtime/devnet/src/extensions/{v0/error.rs => v0.rs} (100%) delete mode 100644 runtime/devnet/src/extensions/v0/assets.rs delete mode 100644 runtime/devnet/src/extensions/v0/mod.rs create mode 100644 scripts/pallet-weights-template.hbs diff --git a/Cargo.lock b/Cargo.lock index 1983b157..d3ca53e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -215,7 +215,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -361,9 +361,9 @@ checksum = "5d5dde061bd34119e902bbb2d9b90c5692635cf59fb91d582c2b68043f1b8293" [[package]] name = "arrayref" -version = "0.3.7" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545" +checksum = "9d151e35f61089500b617991b791fc8bfd237ae50cd5950803758a179b41e67a" [[package]] name = "arrayvec" @@ -740,9 +740,9 @@ dependencies = [ [[package]] name = "async-signal" -version = "0.2.8" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "794f185324c2f00e771cd9f1ae8b5ac68be2ca7abb129a87afd6e86d228bc54d" +checksum = "dfb3634b73397aa844481f814fad23bbf07fdb0eabec10f2eb95e58944b1ec32" dependencies = [ "async-io 2.3.3", "async-lock 3.4.0", @@ -770,7 +770,7 @@ checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -907,7 +907,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -1051,18 +1051,6 @@ dependencies = [ "piper", ] -[[package]] -name = "bounded-collections" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca548b6163b872067dc5eb82fd130c56881435e30367d2073594a3d9744120dd" -dependencies = [ - "log", - "parity-scale-codec", - "scale-info", - "serde", -] - [[package]] name = "bounded-collections" version = "0.2.0" @@ -1457,9 +1445,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "324c74f2155653c90b04f25b2a47a8a631360cb908f92a772695f430c7e31052" +checksum = "2aba8f4e9906c7ce3c73463f62a7f0c65183ada1a2d47e397cc8810827f9694f" dependencies = [ "jobserver", "libc", @@ -1628,7 +1616,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -1676,7 +1664,7 @@ dependencies = [ "nom", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -2342,7 +2330,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -2383,7 +2371,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fa22d6e479a4d3a2790bab291269ba0917a1ac384255a54a2ebc3f7c37e505e" dependencies = [ - "bounded-collections 0.2.0", + "bounded-collections", "bp-xcm-bridge-hub-router", "cumulus-primitives-core", "frame-benchmarking", @@ -2662,7 +2650,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -2702,7 +2690,7 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -2719,7 +2707,7 @@ checksum = "4b2c1c1776b986979be68bb2285da855f8d8a35851a769fca8740df7c3d07877" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -2767,7 +2755,7 @@ dependencies = [ "proc-macro2", "quote", "strsim 0.11.1", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -2789,7 +2777,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ "darling_core 0.20.10", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -2881,7 +2869,7 @@ checksum = "d65d7ce8132b7c0e54497a4d9a55a1c2a0912a0d786cf894472ba818fba45762" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -2894,7 +2882,7 @@ dependencies = [ "proc-macro2", "quote", "rustc_version", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -2983,7 +2971,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -3007,9 +2995,9 @@ dependencies = [ "proc-macro2", "quote", "regex", - "syn 2.0.71", + "syn 2.0.72", "termcolor", - "toml 0.8.14", + "toml 0.8.15", "walkdir", ] @@ -3219,7 +3207,7 @@ checksum = "de0d48a183585823424a4ce1aa132d174a6a81bd540895822eb4c8373a8e49e8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -3230,7 +3218,7 @@ checksum = "6fd000fd6988e73bbe993ea3db9b1aa64906ab88766d654973924340c8cddb42" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -3413,7 +3401,7 @@ dependencies = [ "prettyplease 0.2.20", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -3688,7 +3676,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -3849,7 +3837,7 @@ dependencies = [ "proc-macro2", "quote", "sp-crypto-hashing", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -3862,7 +3850,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -3873,7 +3861,7 @@ checksum = "68672b9ec6fe72d259d3879dc212c5e42e977588cdac830c76f54d9f492aeb58" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -4066,7 +4054,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -5197,9 +5185,9 @@ checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "libloading" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e310b3a6b5907f99202fcdb4960ff45b93735d7c7d96b760fcff8db2dc0e103d" +checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" dependencies = [ "cfg-if", "windows-targets 0.52.6", @@ -5847,7 +5835,7 @@ dependencies = [ "macro_magic_core", "macro_magic_macros", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -5861,7 +5849,7 @@ dependencies = [ "macro_magic_core_macros", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -5872,7 +5860,7 @@ checksum = "b02abfe41815b5bd98dbd4260173db2c116dda171dc0fe7838cb206333b83308" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -5883,7 +5871,7 @@ checksum = "73ea28ee64b88876bf45277ed9a5817c1817df061a74f2b988971a12570e5869" dependencies = [ "macro_magic_core", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -6245,7 +6233,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", "synstructure 0.13.1", ] @@ -6293,7 +6281,7 @@ checksum = "254a5372af8fc138e36684761d3c0cdb758a4410e938babcff1c860ce14ddbfc" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -6616,6 +6604,23 @@ dependencies = [ "num-traits", ] +[[package]] +name = "pallet-api" +version = "0.1.0" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "pallet-assets", + "pallet-balances", + "parity-scale-codec", + "scale-info", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", +] + [[package]] name = "pallet-asset-conversion" version = "11.0.0" @@ -7087,7 +7092,7 @@ checksum = "3163c6bc21b55a0ccb74c546ba784d9c9e69beb9240c059d28a3052f4cbce509" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -7755,7 +7760,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -7993,7 +7998,7 @@ version = "8.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba9138b04168b07b1aff4a2079f5514753c31dddba40e5fb471b9cda7da27ad6" dependencies = [ - "bounded-collections 0.2.0", + "bounded-collections", "frame-benchmarking", "frame-support", "frame-system", @@ -8345,7 +8350,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -8386,7 +8391,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -9203,7 +9208,7 @@ version = "7.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "248ab090959a92e61493277e33b7e85104280a4beb4cb0815137d3c8c50a07f4" dependencies = [ - "bounded-collections 0.2.0", + "bounded-collections", "derive_more", "parity-scale-codec", "polkadot-core-primitives", @@ -9560,7 +9565,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6380dbe1fb03ecc74ad55d841cfc75480222d153ba69ddcb00977866cbdabdb8" dependencies = [ "polkavm-derive-impl", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -9572,7 +9577,7 @@ dependencies = [ "polkavm-common", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -9697,7 +9702,6 @@ dependencies = [ name = "pop-primitives" version = "0.0.0" dependencies = [ - "bounded-collections 0.1.9", "parity-scale-codec", "scale-info", ] @@ -9727,7 +9731,6 @@ dependencies = [ "cumulus-primitives-aura", "cumulus-primitives-core", "cumulus-primitives-utility", - "enumflags2", "env_logger 0.11.3", "frame-benchmarking", "frame-executive", @@ -9739,6 +9742,7 @@ dependencies = [ "hex", "hex-literal", "log", + "pallet-api", "pallet-assets", "pallet-aura", "pallet-authorship", @@ -9801,7 +9805,6 @@ dependencies = [ "cumulus-primitives-aura", "cumulus-primitives-core", "cumulus-primitives-utility", - "enumflags2", "env_logger 0.11.3", "frame-benchmarking", "frame-executive", @@ -9864,9 +9867,9 @@ dependencies = [ [[package]] name = "portable-atomic" -version = "1.6.0" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" +checksum = "da544ee218f0d287a911e9c99a39a8c9bc8fcad3cb8db5959940044ecfc67265" [[package]] name = "powerfmt" @@ -9927,7 +9930,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e" dependencies = [ "proc-macro2", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -10021,7 +10024,7 @@ checksum = "834da187cfe638ae8abb0203f0b33e5ccdb02a28e7199f2f47b3e2754f50edca" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -10067,7 +10070,7 @@ checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -10135,7 +10138,7 @@ dependencies = [ "itertools 0.12.1", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -10401,7 +10404,7 @@ checksum = "bcc303e793d3734489387d205e9b186fac9c6cfacedd98cbb2e8a5943595f3e6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -11062,7 +11065,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -12076,7 +12079,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -12242,7 +12245,7 @@ dependencies = [ "proc-macro2", "quote", "scale-info", - "syn 2.0.71", + "syn 2.0.72", "thiserror", ] @@ -12475,7 +12478,7 @@ checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -13094,7 +13097,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -13288,7 +13291,7 @@ dependencies = [ "bip39", "bitflags 1.3.2", "blake2 0.10.6", - "bounded-collections 0.2.0", + "bounded-collections", "bs58 0.5.1", "dyn-clonable", "ed25519-zebra 3.1.0", @@ -13360,7 +13363,7 @@ checksum = "b85d0f1f1e44bd8617eb2a48203ee854981229e3e79e6f468c7175d5fd37489b" dependencies = [ "quote", "sp-crypto-hashing", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -13381,7 +13384,7 @@ checksum = "48d09fa0a5f7299fb81ee25ae3853d26200f7a348148aed6de76be905c007dbe" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -13630,7 +13633,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -13837,7 +13840,7 @@ dependencies = [ "parity-scale-codec", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -13860,7 +13863,7 @@ version = "28.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3be30aec904994451dcacf841a9168cfbbaf817de6b24b6a1c1418cbf1af2fe" dependencies = [ - "bounded-collections 0.2.0", + "bounded-collections", "parity-scale-codec", "scale-info", "serde", @@ -13969,7 +13972,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48fa328b87de3466bc38cc9a07244c42c647b7755b81115e1dfeb47cc13fc6e6" dependencies = [ "array-bytes 6.2.3", - "bounded-collections 0.2.0", + "bounded-collections", "derivative", "environmental", "impl-trait-for-tuples", @@ -14123,7 +14126,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -14224,7 +14227,7 @@ dependencies = [ "sp-maybe-compressed-blob", "strum 0.24.1", "tempfile", - "toml 0.8.14", + "toml 0.8.15", "walkdir", "wasm-opt", ] @@ -14299,7 +14302,7 @@ dependencies = [ "scale-info", "scale-typegen", "subxt-metadata", - "syn 2.0.71", + "syn 2.0.72", "thiserror", "tokio", ] @@ -14333,7 +14336,7 @@ dependencies = [ "quote", "scale-typegen", "subxt-codegen", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -14384,9 +14387,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.71" +version = "2.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" dependencies = [ "proc-macro2", "quote", @@ -14413,7 +14416,7 @@ checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -14503,9 +14506,9 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.62" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2675633b1499176c2dff06b0856a27976a8f9d436737b4cf4f312d4d91d8bbb" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" dependencies = [ "thiserror-impl", ] @@ -14527,18 +14530,18 @@ checksum = "e4c60d69f36615a077cc7663b9cb8e42275722d23e58a7fa3d2c7f2915d09d04" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] name = "thiserror-impl" -version = "1.0.62" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d20468752b09f49e909e55a5d338caa8bedf615594e9d80bc4c565d30faf798c" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -14657,9 +14660,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.38.0" +version = "1.38.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" +checksum = "eb2caba9f80616f438e09748d5acda951967e1ea58508ef53d9c6402485a46df" dependencies = [ "backtrace", "bytes", @@ -14682,7 +14685,7 @@ checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -14754,14 +14757,14 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.14" +version = "0.8.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f49eb2ab21d2f26bd6db7bf383edc527a7ebaee412d17af4d40fdccd442f335" +checksum = "ac2caab0bf757388c6c0ae23b3293fdb463fee59434529014f85e3263b995c28" dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.15", + "toml_edit 0.22.16", ] [[package]] @@ -14797,15 +14800,15 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.15" +version = "0.22.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d59a3a72298453f564e2b111fa896f8d07fabb36f51f06d7e875fc5e0b5a3ef1" +checksum = "278f3d518e152219c994ce877758516bca5e118eaed6996192a774fb9fbf0788" dependencies = [ "indexmap 2.2.6", "serde", "serde_spanned", "toml_datetime", - "winnow 0.6.13", + "winnow 0.6.14", ] [[package]] @@ -14873,7 +14876,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -14918,7 +14921,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -15344,7 +15347,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", "wasm-bindgen-shared", ] @@ -15378,7 +15381,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -15866,9 +15869,9 @@ dependencies = [ [[package]] name = "wide" -version = "0.7.25" +version = "0.7.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2caba658a80831539b30698ae9862a72db6697dfdd7151e46920f5f2755c3ce2" +checksum = "901e8597c777fa042e9e245bd56c0dc4418c5db3f845b6ff94fbac732c6a0692" dependencies = [ "bytemuck", "safe_arch", @@ -16155,9 +16158,9 @@ dependencies = [ [[package]] name = "winnow" -version = "0.6.13" +version = "0.6.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59b5e5f6c299a3c7890b876a2a587f3115162487e704907d9b6cd29473052ba1" +checksum = "374ec40a2d767a3c1b4972d9475ecd557356637be906f2cb3f7fe17a6eb5e22f" dependencies = [ "memchr", ] @@ -16266,7 +16269,7 @@ dependencies = [ "Inflector", "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -16315,7 +16318,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] @@ -16335,7 +16338,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.71", + "syn 2.0.72", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 6b3cde02..8b2d0a8f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ members = [ "runtime/devnet", "runtime/testnet", "integration-tests", + "pallets/*", "primitives", "scripts/fund-dev-accounts", ] @@ -51,6 +52,7 @@ substrate-wasm-builder = "18.0.1" substrate-build-script-utils = "11.0.0" # Local +pallet-api = { path = "pallets/api", default-features = false } pop-runtime-devnet = { path = "runtime/devnet", default-features = true } # default-features=true required for `-p pop-node` builds pop-runtime-testnet = { path = "runtime/testnet", default-features = true } # default-features=true required for `-p pop-node` builds pop-runtime-common = { path = "runtime/common", default-features = false } diff --git a/node/src/command.rs b/node/src/command.rs index 98af5450..8ba956e1 100644 --- a/node/src/command.rs +++ b/node/src/command.rs @@ -64,18 +64,9 @@ impl RuntimeResolver for PathBuf { fn load_spec(id: &str) -> std::result::Result, String> { Ok(match id { - #[cfg(not(feature = "paseo"))] - "dev-rococo" => Box::new(chain_spec::development_config(Relay::RococoLocal)), - #[cfg(feature = "paseo")] - "dev-paseo" => Box::new(chain_spec::development_config(Relay::PaseoLocal)), - #[cfg(not(feature = "paseo"))] - "pop-rococo" => Box::new(chain_spec::testnet_config(Relay::Rococo)), - #[cfg(feature = "paseo")] - "pop-paseo" => Box::new(chain_spec::testnet_config(Relay::Paseo)), - #[cfg(feature = "paseo")] + "dev" | "dev-paseo" => Box::new(chain_spec::development_config(Relay::PaseoLocal)), + "test" | "pop-paseo" => Box::new(chain_spec::testnet_config(Relay::Paseo)), "" | "local" => Box::new(chain_spec::development_config(Relay::PaseoLocal)), - #[cfg(not(feature = "paseo"))] - "" | "local" => Box::new(chain_spec::development_config(Relay::RococoLocal)), path => { let path: PathBuf = path.into(); match path.runtime() { diff --git a/pallets/api/Cargo.toml b/pallets/api/Cargo.toml new file mode 100644 index 00000000..a813a09c --- /dev/null +++ b/pallets/api/Cargo.toml @@ -0,0 +1,55 @@ +[package] +name = "pallet-api" +authors.workspace = true +description = "Api pallet, enabling smart(er) contracts with the power of Polkadot" +edition.workspace = true +license.workspace = true +version = "0.1.0" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +codec.workspace = true +scale-info.workspace = true + +# Substrate +frame-benchmarking.workspace = true +frame-support.workspace = true +frame-system.workspace = true +pallet-assets.workspace = true +sp-runtime.workspace = true +sp-std.workspace = true + +[dev-dependencies] +pallet-balances.workspace = true +sp-core.workspace = true +sp-io.workspace = true + +[features] +default = ["std"] +runtime-benchmarks = [ + "frame-benchmarking/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "pallet-assets/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", +] +std = [ + "codec/std", + "frame-benchmarking/std", + "frame-support/std", + "frame-system/std", + "pallet-assets/std", + "pallet-balances/std", + "scale-info/std", + "sp-core/std", + "sp-io/std", + "sp-runtime/std", + "sp-std/std", +] +try-runtime = [ + "frame-support/try-runtime", + "frame-system/try-runtime", + "sp-runtime/try-runtime", +] diff --git a/pallets/api/src/fungibles/benchmarking.rs b/pallets/api/src/fungibles/benchmarking.rs new file mode 100644 index 00000000..d3d65b97 --- /dev/null +++ b/pallets/api/src/fungibles/benchmarking.rs @@ -0,0 +1,106 @@ +//! Benchmarking setup for pallet-api::fungibles + +use super::{AccountIdOf, AssetIdOf, AssetsInstanceOf, AssetsOf, BalanceOf, Call, Config, Pallet}; +use frame_benchmarking::{account, v2::*}; +use frame_support::{ + assert_ok, + traits::{ + fungibles::{ + approvals::{Inspect as ApprovalInspect, Mutate}, + Create, Inspect, + }, + Currency, + }, +}; +use frame_system::RawOrigin; +use sp_runtime::traits::Zero; + +const SEED: u32 = 1; + +// See if `generic_event` has been emitted. +fn assert_has_event( + generic_event: >>::RuntimeEvent, +) { + frame_system::Pallet::::assert_has_event(generic_event.into()); +} + +#[benchmarks( + where + > as Inspect<::AccountId>>::AssetId: Zero, +)] +mod benchmarks { + use super::*; + + // Parameter: + // - 'a': whether `approve_transfer` is required. + // - 'c': whether `cancel_approval` is required. + #[benchmark] + fn approve(a: Linear<0, 1>, c: Linear<0, 1>) -> Result<(), BenchmarkError> { + let asset_id = AssetIdOf::::zero(); + let min_balance = >::from(1u32); + let owner: AccountIdOf = account("Alice", 0, SEED); + let spender: AccountIdOf = account("Bob", 0, SEED); + let current_allowance = >::from(u32::MAX / 2); + T::Currency::make_free_balance_be(&owner, u32::MAX.into()); + // Set the `current_allowance`. + assert_ok!( as Create>>::create( + asset_id.clone(), + owner.clone(), + true, + min_balance + )); + assert_ok!( as Mutate>>::approve( + asset_id.clone(), + &owner, + &spender, + current_allowance, + )); + let approval_value = match (a, c) { + // Equal to the current allowance. + (0, 0) => current_allowance, + // Greater than the current allowance. + (1, 0) => >::from(u32::MAX), + // Zero. + (0, 1) => >::from(0u32), + // Smaller than the current allowance. + (1, 1) => >::from(u32::MAX / 4), + _ => unreachable!("values can only be 0 or 1"), + }; + + #[extrinsic_call] + _(RawOrigin::Signed(owner.clone()), asset_id.clone(), spender.clone(), approval_value); + + assert_eq!(AssetsOf::::allowance(asset_id.clone(), &owner, &spender), approval_value); + if c == 1 { + assert_has_event::( + pallet_assets::Event::ApprovalCancelled { + asset_id: asset_id.clone(), + owner: owner.clone(), + delegate: spender.clone(), + } + .into(), + ); + } + if a == 1 { + let amount = match c { + // When the allowance was cancelled and then approved with the new value. + 1 => approval_value, + // When the allowance was increased. + 0 => approval_value - current_allowance, + _ => unreachable!("`c` can only be 0 or 1"), + }; + assert_has_event::( + pallet_assets::Event::ApprovedTransfer { + asset_id, + source: owner, + delegate: spender, + amount, + } + .into(), + ); + } + Ok(()) + } + + impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test); +} diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs new file mode 100644 index 00000000..cd34664a --- /dev/null +++ b/pallets/api/src/fungibles/mod.rs @@ -0,0 +1,240 @@ +/// The fungibles pallet serves as a wrapper around the pallet_assets, offering a streamlined +/// interface for interacting with fungible assets. The goal is to provide a simplified, consistent +/// API that adheres to standards in the smart contract space. + +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; +#[cfg(test)] +mod tests; +pub mod weights; + +use frame_support::traits::fungibles::{metadata::Inspect as MetadataInspect, Inspect}; +pub use pallet::*; +use pallet_assets::WeightInfo as AssetsWeightInfoTrait; +use weights::WeightInfo; + +type AccountIdOf = ::AccountId; +type AssetIdOf = > as Inspect< + ::AccountId, +>>::AssetId; +type AssetIdParameterOf = >>::AssetIdParameter; +type AssetsOf = pallet_assets::Pallet>; +type AssetsInstanceOf = ::AssetsInstance; +type AssetsWeightInfoOf = >>::WeightInfo; +type BalanceOf = > as Inspect< + ::AccountId, +>>::Balance; + +#[frame_support::pallet] +pub mod pallet { + use super::*; + use frame_support::{ + dispatch::{DispatchResult, DispatchResultWithPostInfo, WithPostDispatchInfo}, + pallet_prelude::*, + traits::fungibles::approvals::Inspect as ApprovalInspect, + }; + use frame_system::pallet_prelude::*; + use sp_runtime::{ + traits::{StaticLookup, Zero}, + Saturating, + }; + use sp_std::vec::Vec; + + /// State reads for the fungibles api with required input. + #[derive(Encode, Decode, Debug, MaxEncodedLen)] + #[repr(u8)] + #[allow(clippy::unnecessary_cast)] + pub enum Read { + /// Total token supply for a given asset ID. + #[codec(index = 0)] + TotalSupply(AssetIdOf), + /// Account balance for a given asset ID. + #[codec(index = 1)] + BalanceOf { + /// The asset ID. + id: AssetIdOf, + /// The account ID of the owner. + owner: AccountIdOf, + }, + /// Allowance for a spender approved by an owner, for a given asset ID. + #[codec(index = 2)] + Allowance { + /// The asset ID. + id: AssetIdOf, + /// The account ID of the owner. + owner: AccountIdOf, + /// The account ID of the spender. + spender: AccountIdOf, + }, + /// Token name for a given asset ID. + #[codec(index = 8)] + TokenName(AssetIdOf), + /// Token symbol for a given asset ID. + #[codec(index = 9)] + TokenSymbol(AssetIdOf), + /// Token decimals for a given asset ID. + #[codec(index = 10)] + TokenDecimals(AssetIdOf), + } + + /// Configure the pallet by specifying the parameters and types on which it depends. + #[pallet::config] + pub trait Config: frame_system::Config + pallet_assets::Config { + /// The instance of pallet assets it is tightly coupled to. + type AssetsInstance; + /// Weight information for dispatchables in this pallet. + type WeightInfo: WeightInfo; + } + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::call] + impl Pallet { + /// Transfers `value` amount of tokens from the caller's account to account `to`, with additional + /// `data` in unspecified format. + /// + /// # Parameters + /// * `id` - The ID of the asset. + /// * `to` - The recipient account. + /// * `value` - The number of tokens to transfer. + #[pallet::call_index(3)] + #[pallet::weight(AssetsWeightInfoOf::::transfer_keep_alive())] + pub fn transfer( + origin: OriginFor, + id: AssetIdOf, + target: AccountIdOf, + amount: BalanceOf, + ) -> DispatchResult { + let target = T::Lookup::unlookup(target); + AssetsOf::::transfer_keep_alive(origin, id.into(), target, amount) + } + + /// Approves an account to spend a specified number of tokens on behalf of the caller. + /// + /// # Parameters + /// * `id` - The ID of the asset. + /// * `spender` - The account that is allowed to spend the tokens. + /// * `value` - The number of tokens to approve. + #[pallet::call_index(5)] + #[pallet::weight(::WeightInfo::approve(1, 1))] + pub fn approve( + origin: OriginFor, + id: AssetIdOf, + spender: AccountIdOf, + value: BalanceOf, + ) -> DispatchResultWithPostInfo { + let weight = |approve: u32, cancel: u32| -> Weight { + ::WeightInfo::approve(cancel, approve) + }; + let who = ensure_signed(origin.clone()).map_err(|e| e.with_weight(weight(0, 0)))?; + let current_allowance = AssetsOf::::allowance(id.clone(), &who, &spender); + let spender = T::Lookup::unlookup(spender); + let id: AssetIdParameterOf = id.into(); + + // If the new value is equal to the current allowance, do nothing. + let return_weight = if value == current_allowance { + weight(0, 0) + } + // If the new value is greater than the current allowance, approve the difference + // because `approve_transfer` works additively (see `pallet-assets`). + else if value > current_allowance { + AssetsOf::::approve_transfer( + origin, + id, + spender, + value.saturating_sub(current_allowance), + ) + .map_err(|e| e.with_weight(weight(1, 0)))?; + weight(1, 0) + } else { + // If the new value is less than the current allowance, cancel the approval and set the new value + AssetsOf::::cancel_approval(origin.clone(), id.clone(), spender.clone()) + .map_err(|e| e.with_weight(weight(0, 1)))?; + if value.is_zero() { + return Ok(Some(weight(0, 1)).into()); + } + AssetsOf::::approve_transfer(origin, id, spender, value)?; + weight(1, 1) + }; + Ok(Some(return_weight).into()) + } + + /// Increases the allowance of a spender. + /// + /// # Parameters + /// * `id` - The ID of the asset. + /// * `spender` - The account that is allowed to spend the tokens. + /// * `value` - The number of tokens to increase the allowance by. + #[pallet::call_index(6)] + #[pallet::weight(AssetsWeightInfoOf::::approve_transfer())] + pub fn increase_allowance( + origin: OriginFor, + id: AssetIdOf, + spender: AccountIdOf, + value: BalanceOf, + ) -> DispatchResult { + let spender = T::Lookup::unlookup(spender); + AssetsOf::::approve_transfer(origin, id.into(), spender, value) + } + } + + impl Pallet { + /// Returns the total token supply for a given asset ID. + /// + /// # Parameters + /// * `id` - The ID of the asset. + pub fn total_supply(id: AssetIdOf) -> BalanceOf { + AssetsOf::::total_supply(id) + } + + /// Returns the account balance for the specified `owner` for a given asset ID. Returns `0` if + /// the account is non-existent. + /// + /// # Parameters + /// * `id` - The ID of the asset. + /// * `owner` - The account whose balance is being queried. + pub fn balance_of(id: AssetIdOf, owner: &AccountIdOf) -> BalanceOf { + AssetsOf::::balance(id, owner) + } + + /// Returns the amount which `spender` is still allowed to withdraw from `owner` for a given + /// asset ID. Returns `0` if no allowance has been set. + /// + /// # Parameters + /// * `id` - The ID of the asset. + /// * `owner` - The account that owns the tokens. + /// * `spender` - The account that is allowed to spend the tokens. + pub fn allowance( + id: AssetIdOf, + owner: &AccountIdOf, + spender: &AccountIdOf, + ) -> BalanceOf { + AssetsOf::::allowance(id, owner, spender) + } + + /// Returns the token name for a given asset ID. + /// + /// # Parameters + /// * `id` - The ID of the asset. + pub fn token_name(id: AssetIdOf) -> Vec { + as MetadataInspect>>::name(id) + } + + /// Returns the token symbol for a given asset ID. + /// + /// # Parameters + /// * `id` - The ID of the asset. + pub fn token_symbol(id: AssetIdOf) -> Vec { + as MetadataInspect>>::symbol(id) + } + + /// Returns the token decimals for a given asset ID. + /// + /// # Parameters + /// * `id` - The ID of the asset. + pub fn token_decimals(id: AssetIdOf) -> u8 { + as MetadataInspect>>::decimals(id) + } + } +} diff --git a/pallets/api/src/fungibles/tests.rs b/pallets/api/src/fungibles/tests.rs new file mode 100644 index 00000000..dbfa0b34 --- /dev/null +++ b/pallets/api/src/fungibles/tests.rs @@ -0,0 +1,147 @@ +use crate::mock::*; +use frame_support::{ + assert_ok, + traits::fungibles::{approvals::Inspect, metadata::Inspect as MetadataInspect}, +}; + +const ASSET: u32 = 42; + +#[test] +fn transfer_works() { + new_test_ext().execute_with(|| { + let amount: Balance = 100 * UNIT; + create_asset_and_mint_to(ALICE, ASSET, ALICE, amount); + let balance_before_transfer = Assets::balance(ASSET, &BOB); + assert_ok!(Fungibles::transfer(signed(ALICE), ASSET, BOB, amount / 2)); + let balance_after_transfer = Assets::balance(ASSET, &BOB); + assert_eq!(balance_after_transfer, balance_before_transfer + amount / 2); + }); +} + +// Non-additive, sets new value. +#[test] +fn approve_works() { + new_test_ext().execute_with(|| { + let amount: Balance = 100 * UNIT; + create_asset_and_mint_to(ALICE, ASSET, ALICE, amount); + assert_eq!(0, Assets::allowance(ASSET, &ALICE, &BOB)); + assert_ok!(Fungibles::approve(signed(ALICE), ASSET, BOB, amount)); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount); + // Approves an amount to spend that is lower than the current allowance. + assert_ok!(Fungibles::approve(signed(ALICE), ASSET, BOB, amount / 2)); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount / 2); + // Approves an amount to spend that is higher than the current allowance. + assert_ok!(Fungibles::approve(signed(ALICE), ASSET, BOB, amount * 2)); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount * 2); + // Approves an amount to spend that is equal to the current allowance. + assert_ok!(Fungibles::approve(signed(ALICE), ASSET, BOB, amount * 2)); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount * 2); + // Sets allowance to zero. + assert_ok!(Fungibles::approve(signed(ALICE), ASSET, BOB, 0)); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), 0); + }); +} + +#[test] +fn increase_allowance_works() { + new_test_ext().execute_with(|| { + let amount: Balance = 100 * UNIT; + create_asset_and_mint_to(ALICE, ASSET, ALICE, amount); + assert_eq!(0, Assets::allowance(ASSET, &ALICE, &BOB)); + assert_ok!(Fungibles::increase_allowance(signed(ALICE), ASSET, BOB, amount)); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount); + // Additive. + assert_ok!(Fungibles::increase_allowance(signed(ALICE), ASSET, BOB, amount)); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount * 2); + }); +} + +#[test] +fn total_supply_works() { + new_test_ext().execute_with(|| { + create_asset_and_mint_to(ALICE, ASSET, ALICE, 100); + assert_eq!(Assets::total_supply(ASSET), Fungibles::total_supply(ASSET)); + }); +} + +#[test] +fn balance_of_works() { + new_test_ext().execute_with(|| { + create_asset_and_mint_to(ALICE, ASSET, ALICE, 100); + assert_eq!(Assets::balance(ASSET, ALICE), Fungibles::balance_of(ASSET, &ALICE)); + }); +} + +#[test] +fn allowance_works() { + new_test_ext().execute_with(|| { + create_asset_mint_and_approve(ALICE, ASSET, BOB, 100, ALICE, 50); + assert_eq!( + Assets::allowance(ASSET, &ALICE, &BOB), + Fungibles::allowance(ASSET, &ALICE, &BOB) + ); + }); +} + +#[test] +fn token_metadata_works() { + new_test_ext().execute_with(|| { + let name: Vec = vec![11, 12, 13]; + let symbol: Vec = vec![21, 22, 23]; + let decimals: u8 = 69; + create_asset_and_set_metadata(ALICE, ASSET, name.clone(), symbol.clone(), decimals); + assert_eq!(Assets::name(ASSET), Fungibles::token_name(ASSET)); + assert_eq!(Assets::symbol(ASSET), Fungibles::token_symbol(ASSET)); + assert_eq!(Assets::decimals(ASSET), Fungibles::token_decimals(ASSET)); + }); +} + +fn signed(account: AccountId) -> RuntimeOrigin { + RuntimeOrigin::signed(account) +} + +fn create_asset(owner: AccountId, asset_id: AssetId, min_balance: Balance) { + assert_ok!(Assets::create(signed(owner), asset_id, owner, min_balance)); +} + +fn mint_asset(owner: AccountId, asset_id: AssetId, to: AccountId, value: Balance) { + assert_ok!(Assets::mint(signed(owner), asset_id, to, value)); +} + +fn create_asset_and_mint_to(owner: AccountId, asset_id: AssetId, to: AccountId, value: Balance) { + create_asset(owner, asset_id, 1); + mint_asset(owner, asset_id, to, value) +} + +fn create_asset_mint_and_approve( + owner: AccountId, + asset_id: AssetId, + to: AccountId, + mint: Balance, + spender: AccountId, + approve: Balance, +) { + create_asset_and_mint_to(owner, asset_id, to, mint); + assert_ok!(Assets::approve_transfer(signed(to), asset_id, spender, approve,)); +} + +fn create_asset_and_set_metadata( + owner: AccountId, + asset_id: AssetId, + name: Vec, + symbol: Vec, + decimals: u8, +) { + assert_ok!(Assets::create(signed(owner), asset_id, owner, 100)); + set_metadata_asset(owner, asset_id, name, symbol, decimals); +} + +fn set_metadata_asset( + owner: AccountId, + asset_id: AssetId, + name: Vec, + symbol: Vec, + decimals: u8, +) { + assert_ok!(Assets::set_metadata(signed(owner), asset_id, name, symbol, decimals)); +} diff --git a/pallets/api/src/fungibles/weights.rs b/pallets/api/src/fungibles/weights.rs new file mode 100644 index 00000000..a6c31654 --- /dev/null +++ b/pallets/api/src/fungibles/weights.rs @@ -0,0 +1,94 @@ + +//! Autogenerated weights for `pallet_api::fungibles` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 33.0.0 +//! DATE: 2024-07-25, STEPS: `20`, REPEAT: `5`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `R0GUE`, CPU: `` +//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: `1024` + +// Executed Command: +// ./target/release/pop-node +// benchmark +// pallet +// --chain=dev +// --wasm-execution=compiled +// --pallet=pallet_api::fungibles +// --steps=20 +// --repeat=5 +// --json +// --template +// ./scripts/pallet-weights-template.hbs +// --output=./pallets/api/src/fungibles/weights.rs +// --extrinsic= + +#![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 `pallet_api::fungibles`. +pub trait WeightInfo { + fn approve(a: u32, c: u32, ) -> Weight; +} + +/// Weights for `pallet_api::fungibles` using the Substrate node and recommended hardware. +pub struct SubstrateWeight(PhantomData); +impl WeightInfo for SubstrateWeight { + /// Storage: `Assets::Approvals` (r:1 w:1) + /// Proof: `Assets::Approvals` (`max_values`: None, `max_size`: Some(148), added: 2623, mode: `MaxEncodedLen`) + /// Storage: `Assets::Asset` (r:1 w:1) + /// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) + /// The range of component `a` is `[0, 1]`. + /// The range of component `c` is `[0, 1]`. + fn approve(a: u32, c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `413 + c * (102 ±0)` + // Estimated: `3675 + c * (1797 ±0)` + // Minimum execution time: 35_000_000 picoseconds. + Weight::from_parts(1_207_482, 3675) + // Standard Error: 948_955 + .saturating_add(Weight::from_parts(34_649_659, 0).saturating_mul(a.into())) + // Standard Error: 948_955 + .saturating_add(Weight::from_parts(56_976_190, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 1797).saturating_mul(c.into())) + } +} + +// For backwards compatibility and tests. +impl WeightInfo for () { + /// Storage: `Assets::Approvals` (r:1 w:1) + /// Proof: `Assets::Approvals` (`max_values`: None, `max_size`: Some(148), added: 2623, mode: `MaxEncodedLen`) + /// Storage: `Assets::Asset` (r:1 w:1) + /// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) + /// The range of component `a` is `[0, 1]`. + /// The range of component `c` is `[0, 1]`. + fn approve(a: u32, c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `413 + c * (102 ±0)` + // Estimated: `3675 + c * (1797 ±0)` + // Minimum execution time: 35_000_000 picoseconds. + Weight::from_parts(1_207_482, 3675) + // Standard Error: 948_955 + .saturating_add(Weight::from_parts(34_649_659, 0).saturating_mul(a.into())) + // Standard Error: 948_955 + .saturating_add(Weight::from_parts(56_976_190, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 1797).saturating_mul(c.into())) + } +} + diff --git a/pallets/api/src/lib.rs b/pallets/api/src/lib.rs new file mode 100644 index 00000000..5cba0551 --- /dev/null +++ b/pallets/api/src/lib.rs @@ -0,0 +1,5 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +pub mod fungibles; +#[cfg(test)] +mod mock; diff --git a/pallets/api/src/mock.rs b/pallets/api/src/mock.rs new file mode 100644 index 00000000..f5d155ef --- /dev/null +++ b/pallets/api/src/mock.rs @@ -0,0 +1,123 @@ +use frame_support::{ + derive_impl, parameter_types, + traits::{AsEnsureOriginWithArg, ConstU128, ConstU32, Everything}, +}; +use frame_system::{EnsureRoot, EnsureSigned}; +use sp_core::H256; +use sp_runtime::{ + traits::{BlakeTwo256, IdentityLookup}, + BuildStorage, +}; + +type Block = frame_system::mocking::MockBlock; +pub(crate) type AccountId = u64; +pub(crate) type AssetId = u32; +pub(crate) type Balance = u128; + +// Configure a mock runtime to test the pallet. +frame_support::construct_runtime!( + pub enum Test + { + System: frame_system, + Assets: pallet_assets::, + Balances: pallet_balances, + Fungibles: crate::fungibles, + } +); + +parameter_types! { + pub const BlockHashCount: u64 = 250; + pub const SS58Prefix: u8 = 42; +} + +#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] +impl frame_system::Config for Test { + type BaseCallFilter = Everything; + type BlockWeights = (); + type BlockLength = (); + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type Nonce = u64; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = AccountId; + type Lookup = IdentityLookup; + type Block = Block; + 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 = SS58Prefix; + type OnSetCode = (); + type MaxConsumers = ConstU32<16>; +} + +impl pallet_balances::Config for Test { + type Balance = Balance; + type DustRemoval = (); + type RuntimeEvent = RuntimeEvent; + type ExistentialDeposit = ConstU128<1>; + type AccountStore = System; + type FreezeIdentifier = (); + type MaxFreezes = ConstU32<0>; + type WeightInfo = (); + type MaxLocks = (); + type MaxReserves = (); + type ReserveIdentifier = [u8; 8]; + type RuntimeHoldReason = RuntimeHoldReason; + type RuntimeFreezeReason = RuntimeFreezeReason; +} + +type AssetsInstance = pallet_assets::Instance1; +impl pallet_assets::Config for Test { + type RuntimeEvent = RuntimeEvent; + type Balance = Balance; + type RemoveItemsLimit = ConstU32<5>; + type AssetId = AssetId; + type AssetIdParameter = u32; + type Currency = Balances; + type CreateOrigin = AsEnsureOriginWithArg>; + type ForceOrigin = EnsureRoot; + type AssetDeposit = ConstU128<1>; + type AssetAccountDeposit = ConstU128<10>; + type MetadataDepositBase = ConstU128<1>; + type MetadataDepositPerByte = ConstU128<1>; + type ApprovalDeposit = ConstU128<1>; + type StringLimit = ConstU32<50>; + type Freezer = (); + type Extra = (); + type CallbackHandle = (); + type WeightInfo = (); + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkHelper = (); +} +impl crate::fungibles::Config for Test { + type AssetsInstance = AssetsInstance; + type WeightInfo = (); +} + +pub(crate) const ALICE: AccountId = 1; +pub(crate) const BOB: AccountId = 2; +pub(crate) const INIT_AMOUNT: Balance = 100_000_000 * UNIT; +pub(crate) const UNIT: Balance = 10_000_000_000; + +pub(crate) fn new_test_ext() -> sp_io::TestExternalities { + let mut t = frame_system::GenesisConfig::::default() + .build_storage() + .expect("Frame system builds valid default genesis config"); + + pallet_balances::GenesisConfig:: { + balances: vec![(ALICE, INIT_AMOUNT), (BOB, INIT_AMOUNT)], + } + .assimilate_storage(&mut t) + .expect("Pallet balances storage can be assimilated"); + + let mut ext = sp_io::TestExternalities::new(t); + ext.execute_with(|| System::set_block_number(1)); + ext +} diff --git a/pop-api/Cargo.toml b/pop-api/Cargo.toml index dc48ea8a..9946abf7 100644 --- a/pop-api/Cargo.toml +++ b/pop-api/Cargo.toml @@ -1,12 +1,11 @@ [package] name = "pop-api" -description = "Easily access the power of Polkadot via the Pop Network" +description = "Enabling smart(er) contracts with the power of Polkadot" license = "GPL-3.0-only" version = "0.0.0" edition = "2021" [dependencies] -enumflags2 = { version = "0.7.7" } ink = { version = "5.0.0", default-features = false } sp-io = { version = "31.0.0", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] } @@ -20,12 +19,9 @@ crate-type = ["rlib"] [features] default = ["std"] std = [ - "enumflags2/std", "ink/std", "pop-primitives/std", "sp-io/std", ] -assets = ["pop-primitives/assets"] -balances = [] -nfts = ["pop-primitives/nfts"] -cross-chain = ["pop-primitives/cross-chain"] +assets = [] +fungibles = ["assets"] diff --git a/pop-api/examples/fungibles/Cargo.toml b/pop-api/examples/fungibles/Cargo.toml index 565b0554..19d9ce12 100755 --- a/pop-api/examples/fungibles/Cargo.toml +++ b/pop-api/examples/fungibles/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [dependencies] ink = { version = "5.0.0", default-features = false } -pop-api = { path = "../../../pop-api", default-features = false, features = ["assets"] } +pop-api = { path = "../../../pop-api", default-features = false, features = ["fungibles"] } [lib] path = "lib.rs" diff --git a/pop-api/integration-tests/Cargo.toml b/pop-api/integration-tests/Cargo.toml index 94c0ba83..cc56630a 100644 --- a/pop-api/integration-tests/Cargo.toml +++ b/pop-api/integration-tests/Cargo.toml @@ -11,7 +11,7 @@ frame-system = { version = "29.0.0", default-features = false } pallet-balances = { version = "29.0.2", default-features = false } pallet-assets = { version = "30.0.0", default-features = false } pallet-contracts = { version = "28.0.0", default-features = false } -pop-primitives = { path = "../../primitives", default-features = false, features = ["assets"] } +pop-primitives = { path = "../../primitives", default-features = false } pop-runtime-devnet = { path = "../../runtime/devnet", default-features = false } sp-io = { version = "31.0.0", default-features = false } sp-runtime = { version = "32.0.0", default-features = false } diff --git a/pop-api/integration-tests/contracts/fungibles/Cargo.toml b/pop-api/integration-tests/contracts/fungibles/Cargo.toml new file mode 100755 index 00000000..7c322004 --- /dev/null +++ b/pop-api/integration-tests/contracts/fungibles/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "fungibles" +version = "0.1.0" +authors = ["[your_name] <[your_email]>"] +edition = "2021" + +[dependencies] +ink = { version = "5.0.0", default-features = false } +pop-api = { path = "../../../../pop-api", default-features = false, features = ["fungibles"] } + +[lib] +path = "lib.rs" + +[features] +default = ["std"] +std = [ + "ink/std", + "pop-api/std", +] +ink-as-dependency = [] +e2e-tests = [] diff --git a/pop-api/integration-tests/contracts/fungibles/lib.rs b/pop-api/integration-tests/contracts/fungibles/lib.rs new file mode 100755 index 00000000..1b42fec4 --- /dev/null +++ b/pop-api/integration-tests/contracts/fungibles/lib.rs @@ -0,0 +1,185 @@ +#![cfg_attr(not(feature = "std"), no_std, no_main)] + +/// Local Fungibles: +/// 1. PSP-22 Interface +/// 2. PSP-22 Metadata Interface +/// 3. Asset Management +/// +use ink::prelude::vec::Vec; +use pop_api::{ + assets::fungibles::{self as api}, + primitives::AssetId, + StatusCode, +}; + +pub type Result = core::result::Result; + +#[ink::contract] +mod fungibles { + use super::*; + + #[ink(storage)] + #[derive(Default)] + pub struct Fungibles; + + impl Fungibles { + #[ink(constructor, payable)] + pub fn new() -> Self { + ink::env::debug_println!("PopApiFungiblesExample::new"); + Default::default() + } + + /// 1. PSP-22 Interface: + /// - total_supply + /// - balance_of + /// - allowance + /// - transfer + /// - transfer_from + /// - approve + /// - increase_allowance + /// - decrease_allowance + + #[ink(message)] + pub fn total_supply(&self, id: AssetId) -> Result { + api::total_supply(id) + } + + #[ink(message)] + pub fn balance_of(&self, id: AssetId, owner: AccountId) -> Result { + api::balance_of(id, owner) + } + + #[ink(message)] + pub fn allowance( + &self, + id: AssetId, + owner: AccountId, + spender: AccountId, + ) -> Result { + api::allowance(id, owner, spender) + } + + #[ink(message)] + pub fn transfer(&self, id: AssetId, to: AccountId, value: Balance) -> Result<()> { + api::transfer(id, to, value) + } + + #[ink(message)] + pub fn transfer_from( + &self, + id: AssetId, + from: AccountId, + to: AccountId, + value: Balance, + // In the PSP-22 standard a `[u8]`, but the size needs to be known at compile time. + _data: Vec, + ) -> Result<()> { + api::transfer_from(id, from, to, value) + } + + #[ink(message)] + pub fn approve(&self, id: AssetId, spender: AccountId, value: Balance) -> Result<()> { + api::approve(id, spender, value) + } + + #[ink(message)] + pub fn increase_allowance( + &self, + id: AssetId, + spender: AccountId, + value: Balance, + ) -> Result<()> { + api::increase_allowance(id, spender, value) + } + + #[ink(message)] + pub fn decrease_allowance( + &self, + id: AssetId, + spender: AccountId, + value: Balance, + ) -> Result<()> { + api::decrease_allowance(id, spender, value) + } + + /// 2. PSP-22 Metadata Interface: + /// - token_name + /// - token_symbol + /// - token_decimals + + #[ink(message)] + pub fn token_name(&self, id: AssetId) -> Result> { + api::token_name(id) + } + + #[ink(message)] + pub fn token_symbol(&self, id: AssetId) -> Result> { + api::token_symbol(id) + } + + #[ink(message)] + pub fn token_decimals(&self, id: AssetId) -> Result { + api::token_decimals(id) + } + + // 3. Asset Management: + // - create + // - start_destroy + // - destroy_accounts + // - destroy_approvals + // - finish_destroy + // - set_metadata + // - clear_metadata + + // #[ink(message)] + // pub fn create(&self, id: AssetId, admin: AccountId, min_balance: Balance) -> Result<()> { + // ink::env::debug_println!( + // "PopApiFungiblesExample::create: id: {:?} admin: {:?} min_balance: {:?}", + // id, + // admin, + // min_balance, + // ); + // let result = api::create(id, admin, min_balance); + // ink::env::debug_println!("Result: {:?}", result); + // result.map_err(|e| e.into()) + // result + // } + + // #[ink(message)] + // pub fn set_metadata( + // &self, + // id: AssetId, + // name: Vec, + // symbol: Vec, + // decimals: u8, + // ) -> Result<()> { + // ink::env::debug_println!( + // "PopApiFungiblesExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}", + // id, + // name, + // symbol, + // decimals, + // ); + // let result = api::set_metadata(id, name, symbol, decimals); + // ink::env::debug_println!("Result: {:?}", result); + // // result.map_err(|e| e.into()) + // result + // } + // + // #[ink(message)] + // pub fn asset_exists(&self, id: AssetId) -> Result { + // // api::asset_exists(id).map_err(|e| e.into()) + // api::asset_exists(id) + // } + } + + #[cfg(test)] + mod tests { + use super::*; + + #[ink::test] + fn default_works() { + PopApiFungiblesExample::new(); + } + } +} diff --git a/pop-api/integration-tests/src/local_fungibles.rs b/pop-api/integration-tests/src/local_fungibles.rs index 8f0384c0..c62f0713 100644 --- a/pop-api/integration-tests/src/local_fungibles.rs +++ b/pop-api/integration-tests/src/local_fungibles.rs @@ -6,6 +6,7 @@ use pop_primitives::error::{ }; const ASSET_ID: AssetId = 1; +const CONTRACT: &str = "contracts/fungibles/target/ink/fungibles.wasm"; fn decoded(result: ExecReturnValue) -> T { match ::decode(&mut &result.data[2..]) { @@ -79,19 +80,16 @@ fn transfer( result } -fn transfer_from( +fn approve( addr: AccountId32, asset_id: AssetId, - from: Option, - to: Option, + spender: AccountId32, value: Balance, - data: &[u8], ) -> ExecReturnValue { - let function = function_selector("transfer_from"); - let params = - [function, asset_id.encode(), from.encode(), to.encode(), value.encode(), data.encode()] - .concat(); - bare_call(addr, params, 0).expect("should work") + let function = function_selector("approve"); + let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + result } fn increase_allowance( @@ -263,15 +261,10 @@ fn token_decimals_asset(asset_id: AssetId) -> u8 { /// - decrease_allowance #[test] -#[ignore] fn total_supply_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = instantiate( - "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", - INIT_VALUE, - vec![], - ); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); // No tokens in circulation. assert_eq!(Assets::total_supply(ASSET_ID), total_supply(addr.clone(), ASSET_ID)); @@ -285,12 +278,10 @@ fn total_supply_works() { } #[test] -#[ignore] fn balance_of_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = - instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); // No tokens in circulation. assert_eq!(Assets::balance(ASSET_ID, BOB), balance_of(addr.clone(), ASSET_ID, BOB)); @@ -304,12 +295,10 @@ fn balance_of_works() { } #[test] -#[ignore] fn allowance_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = - instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); // No tokens in circulation. assert_eq!( @@ -329,12 +318,10 @@ fn allowance_works() { } #[test] -#[ignore] fn transfer_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = - instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); let amount: Balance = 100 * UNIT; // Asset does not exist. @@ -362,11 +349,11 @@ fn transfer_works() { Module { index: 52, error: 0 }, ); // Successful transfer. - let bob_balance_before_mint = Assets::balance(asset, &BOB); + let balance_before_transfer = Assets::balance(asset, &BOB); let result = transfer(addr.clone(), asset, BOB, amount / 2); assert!(!result.did_revert(), "Contract reverted!"); - let bob_balance_after_mint = Assets::balance(asset, &BOB); - assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount / 2); + let balance_after_transfer = Assets::balance(asset, &BOB); + assert_eq!(balance_after_transfer, balance_before_transfer + amount / 2); // Transfer asset to account that does not exist. assert_eq!( decoded::(transfer(addr.clone(), asset, FERDIE, amount / 4)), @@ -382,82 +369,65 @@ fn transfer_works() { } #[test] -#[ignore] -fn transfer_from_works() { +fn approve_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = - instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); + let addr = instantiate(CONTRACT, 0, vec![]); let amount: Balance = 100 * UNIT; - // Asset does not exist. assert_eq!( - decoded::(transfer(addr.clone(), 1, BOB, amount,)), + decoded::(approve(addr.clone(), 0, BOB, amount)), Module { index: 52, error: 3 }, ); + let asset = create_asset_and_mint_to(ALICE, 0, addr.clone(), amount); + assert_eq!(decoded::(approve(addr.clone(), asset, BOB, amount)), ConsumerRemaining); + + let addr = instantiate(CONTRACT, INIT_VALUE, vec![1]); // Create asset with Alice as owner and mint `amount` to contract address. let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); // Asset is not live, i.e. frozen or being destroyed. freeze_asset(ALICE, asset); assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount,)), + decoded::(approve(addr.clone(), asset, BOB, amount)), Module { index: 52, error: 16 }, ); thaw_asset(ALICE, asset); - // Not enough balance. - assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), - Module { index: 52, error: 0 }, - ); - // Not enough balance due to ED. - assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount)), - Module { index: 52, error: 0 }, - ); - // Successful transfer. - let bob_balance_before_mint = Assets::balance(asset, &BOB); - let result = transfer(addr.clone(), asset, BOB, amount / 2); - assert!(!result.did_revert(), "Contract reverted!"); - let bob_balance_after_mint = Assets::balance(asset, &BOB); - assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount / 2); - // Transfer asset to account that does not exist. - assert_eq!( - decoded::(transfer(addr.clone(), asset, FERDIE, amount / 4)), - Token(CannotCreate) - ); + // Successful approvals: + assert_eq!(0, Assets::allowance(asset, &addr, &BOB)); + assert!(!approve(addr.clone(), asset, BOB, amount).did_revert(), "Contract reverted!"); + assert_eq!(Assets::allowance(asset, &addr, &BOB), amount); + // Non-additive, sets new value. + assert!(!approve(addr.clone(), asset, BOB, amount / 2).did_revert(), "Contract reverted!"); + assert_eq!(Assets::allowance(asset, &addr, &BOB), amount / 2); // Asset is not live, i.e. frozen or being destroyed. start_destroy_asset(ALICE, asset); assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), + decoded::(approve(addr.clone(), asset, BOB, amount)), Module { index: 52, error: 16 }, ); }); } #[test] -#[ignore] fn increase_allowance_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 0, vec![]); + let addr = instantiate(CONTRACT, 0, vec![]); let amount: Balance = 100 * UNIT; - let asset = 0; - create_asset_and_mint_to(ALICE, asset, addr.clone(), amount); + // Asset does not exist. assert_eq!( - decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), - ConsumerRemaining + decoded::(increase_allowance(addr.clone(), 0, BOB, amount)), + Module { index: 52, error: 3 }, ); - - let addr = - instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![1]); - // Asset does not exist. - let asset = 1; + let asset = create_asset_and_mint_to(ALICE, 0, addr.clone(), amount); assert_eq!( decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), - Module { index: 52, error: 3 }, + ConsumerRemaining ); + + let addr = instantiate(CONTRACT, INIT_VALUE, vec![1]); // Create asset with Alice as owner and mint `amount` to contract address. - create_asset_and_mint_to(ALICE, asset, addr.clone(), amount); + let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); // Asset is not live, i.e. frozen or being destroyed. freeze_asset(ALICE, asset); assert_eq!( @@ -465,7 +435,7 @@ fn increase_allowance_works() { Module { index: 52, error: 16 }, ); thaw_asset(ALICE, asset); - // Successful approval. + // Successful approvals: assert_eq!(0, Assets::allowance(asset, &addr, &BOB)); assert!( !increase_allowance(addr.clone(), asset, BOB, amount).did_revert(), @@ -493,15 +463,10 @@ fn increase_allowance_works() { /// - token_decimals #[test] -#[ignore] fn token_metadata_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); - let addr = instantiate( - "../../pop-api/examples/fungibles/target/ink/fungibles.wasm", - INIT_VALUE, - vec![], - ); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); let name: Vec = vec![11, 12, 13]; let symbol: Vec = vec![21, 22, 23]; @@ -536,7 +501,7 @@ fn token_metadata_works() { // new_test_ext().execute_with(|| { // let _ = env_logger::try_init(); // let addr = -// instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); +// instantiate(CONTRACT, INIT_VALUE, vec![]); // // // No tokens in circulation. // assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr.clone(), ASSET_ID)); @@ -553,7 +518,7 @@ fn token_metadata_works() { // new_test_ext().execute_with(|| { // let _ = env_logger::try_init(); // let addr = -// instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); +// instantiate(CONTRACT, INIT_VALUE, vec![]); // let amount: Balance = 100 * UNIT; // // // Asset does not exist. @@ -581,11 +546,11 @@ fn token_metadata_works() { // ); // thaw_asset(addr.clone(), asset); // // Successful mint. -// let bob_balance_before_mint = Assets::balance(asset, &BOB); +// let balance_before_mint = Assets::balance(asset, &BOB); // let result = transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8]); // assert!(!result.did_revert(), "Contract reverted!"); -// let bob_balance_after_mint = Assets::balance(asset, &BOB); -// assert_eq!(bob_balance_after_mint, bob_balance_before_mint + amount); +// let balance_after_mint = Assets::balance(asset, &BOB); +// assert_eq!(balance_after_mint, balance_before_mint + amount); // // Can not mint more tokens than Balance::MAX. // assert_eq!( // decoded::(transfer_from( @@ -613,14 +578,14 @@ fn token_metadata_works() { // new_test_ext().execute_with(|| { // let _ = env_logger::try_init(); // // Instantiate a contract without balance (relay token). -// let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 0, vec![0]); +// let addr = instantiate(CONTRACT, 0, vec![0]); // // No balance to pay for fees. // assert_eq!( // decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), // Module { index: 10, error: 2 }, // ); // // Instantiate a contract without balance (relay token). -// let addr = instantiate("../examples/fungibles/target/ink/fungibles.wasm", 100, vec![2]); +// let addr = instantiate(CONTRACT, 100, vec![2]); // // No balance to pay the deposit. // assert_eq!( // decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), @@ -628,7 +593,7 @@ fn token_metadata_works() { // ); // // Instantiate a contract with balance. // let addr = -// instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![1]); +// instantiate(CONTRACT, INIT_VALUE, vec![1]); // assert_eq!( // decoded::(create(addr.clone(), ASSET_ID, BOB, 0)), // Module { index: 52, error: 7 }, @@ -652,7 +617,7 @@ fn token_metadata_works() { // new_test_ext().execute_with(|| { // let _ = env_logger::try_init(); // let addr = -// instantiate("../examples/fungibles/target/ink/fungibles.wasm", INIT_VALUE, vec![]); +// instantiate(CONTRACT, INIT_VALUE, vec![]); // // create_asset(addr.clone(), ASSET_ID, 1); // let result = set_metadata(addr.clone(), ASSET_ID, vec![12], vec![12], 12); diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 86621c51..a984bb9e 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -1,17 +1,9 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -use ink::env::chain_extension::FromStatusCode; - use constants::DECODING_FAILED; - +use ink::env::chain_extension::FromStatusCode; #[cfg(feature = "assets")] pub use v0::assets; -#[cfg(feature = "balances")] -pub use v0::balances; -#[cfg(feature = "cross-chain")] -pub use v0::cross_chain; -#[cfg(feature = "nfts")] -pub use v0::nfts; pub mod primitives; pub mod v0; @@ -22,7 +14,8 @@ pub type Result = core::result::Result; mod constants { // Errors: pub(crate) const DECODING_FAILED: u32 = 255; - pub(crate) const MODULE_ERROR: u8 = 3; + // TODO: will be used in the future when the remaining fungibles features will be implemented. + pub(crate) const _MODULE_ERROR: u8 = 3; // Function IDs: pub(crate) const DISPATCH: u8 = 0; @@ -31,6 +24,7 @@ mod constants { // Modules: pub(crate) const ASSETS: u8 = 52; pub(crate) const BALANCES: u8 = 10; + pub(crate) const FUNGIBLES: u8 = 150; } /// Represents a status code returned by the runtime. diff --git a/pop-api/src/primitives.rs b/pop-api/src/primitives.rs index 33285044..a3d596a5 100644 --- a/pop-api/src/primitives.rs +++ b/pop-api/src/primitives.rs @@ -1,8 +1,5 @@ use ink::env::{DefaultEnvironment, Environment}; - pub use pop_primitives::*; pub(crate) type AccountId = ::AccountId; pub(crate) type Balance = ::Balance; -#[cfg(any(feature = "nfts", feature = "cross-chain"))] -type BlockNumber = ::BlockNumber; diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index 255e8502..42373cd7 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -1,26 +1,55 @@ -use ink::prelude::vec::Vec; +use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec, scale::Decode}; use crate::{ - assets, - constants::{ASSETS, BALANCES, MODULE_ERROR}, + constants::{ASSETS, BALANCES, DECODING_FAILED, DISPATCH, FUNGIBLES, READ_STATE}, primitives::{AccountId, AssetId, Balance}, + v0::V0, Result, StatusCode, }; +use constants::*; +pub use metadata::*; /// Local Fungibles: /// 1. PSP-22 Interface /// 2. PSP-22 Metadata Interface /// 3. Asset Management -/// 1. PSP-22 Interface: -/// - total_supply -/// - balance_of -/// - allowance -/// - transfer -/// - transfer_from -/// - approve -/// - increase_allowance -/// - decrease_allowance +mod constants { + /// 1. PSP-22 Interface: + /// - total_supply + pub const TOTAL_SUPPLY: u8 = 0; + /// - balance_of + pub const BALANCE_OF: u8 = 1; + /// - allowance + pub const ALLOWANCE: u8 = 2; + /// - transfer + pub(super) const TRANSFER: u8 = 3; + /// - transfer_from + pub(super) const TRANSFER_FROM: u8 = 4; + /// - approve + pub(super) const APPROVE: u8 = 5; + /// - increase_allowance + pub(super) const INCREASE_ALLOWANCE: u8 = 6; + /// - decrease_allowance + pub(super) const DECREASE_ALLOWANCE: u8 = 7; + + /// 2. PSP-22 Metadata Interface: + /// - token_name + pub const TOKEN_NAME: u8 = 8; + /// - token_symbol + pub const TOKEN_SYMBOL: u8 = 9; + /// - token_decimals + pub const TOKEN_DECIMALS: u8 = 10; + + // 3. Asset Management: + // - create + // - start_destroy + // - destroy_accounts + // - destroy_approvals + // - finish_destroy + // - set_metadata + // - clear_metadata +} /// Returns the total token supply for a given asset ID. /// @@ -31,7 +60,12 @@ use crate::{ /// The total supply of the token, or an error if the operation fails. #[inline] pub fn total_supply(id: AssetId) -> Result { - assets::total_supply(id) + ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, TOTAL_SUPPLY])) + .input::() + .output::>, true>() + .handle_error_code::() + .call(&(id)) + .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } /// Returns the account balance for the specified `owner` for a given asset ID. Returns `0` if @@ -45,7 +79,12 @@ pub fn total_supply(id: AssetId) -> Result { /// The balance of the specified account, or an error if the operation fails. #[inline] pub fn balance_of(id: AssetId, owner: AccountId) -> Result { - assets::balance_of(id, owner) + ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, BALANCE_OF])) + .input::<(AssetId, AccountId)>() + .output::>, true>() + .handle_error_code::() + .call(&(id, owner)) + .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } /// Returns the amount which `spender` is still allowed to withdraw from `owner` for a given @@ -60,7 +99,12 @@ pub fn balance_of(id: AssetId, owner: AccountId) -> Result { /// The remaining allowance, or an error if the operation fails. #[inline] pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { - assets::allowance(id, owner, spender) + ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, ALLOWANCE])) + .input::<(AssetId, AccountId, AccountId)>() + .output::>, true>() + .handle_error_code::() + .call(&(id, owner, spender)) + .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } /// Transfers `value` amount of tokens from the caller's account to account `to`, with additional @@ -74,8 +118,12 @@ pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result Result<()> { - assets::transfer_keep_alive(id, to, value) +pub fn transfer(id: AssetId, target: AccountId, amount: Balance) -> Result<()> { + ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, TRANSFER])) + .input::<(AssetId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, target, amount)) } /// Transfers `value` tokens on behalf of `from` to account `to` with additional `data` @@ -91,8 +139,12 @@ pub fn transfer(id: AssetId, to: AccountId, value: Balance) -> Result<()> { /// # Returns /// Returns `Ok(())` if successful, or an error if the transfer fails. #[inline] -pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, value: Balance) -> Result<()> { - assets::transfer_approved(id, from, to, value) +pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, amount: Balance) -> Result<()> { + ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, TRANSFER_FROM])) + .input::<(AssetId, AccountId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, from, to, amount)) } /// Approves an account to spend a specified number of tokens on behalf of the caller. @@ -105,9 +157,12 @@ pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, value: Balance /// # Returns /// Returns `Ok(())` if successful, or an error if the approval fails. #[inline] -pub fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { - assets::cancel_approval(id, spender)?; - assets::approve_transfer(id, spender, value) +pub fn approve(id: AssetId, spender: AccountId, amount: Balance) -> Result<()> { + ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, APPROVE])) + .input::<(AssetId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, spender, amount)) } /// Increases the allowance of a spender. @@ -121,7 +176,11 @@ pub fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { /// Returns `Ok(())` if successful, or an error if the operation fails. #[inline] pub fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { - assets::approve_transfer(id, spender, value) + ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, INCREASE_ALLOWANCE])) + .input::<(AssetId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, spender, value)) } /// Decreases the allowance of a spender. @@ -134,63 +193,69 @@ pub fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Re /// # Returns /// Returns `Ok(())` if successful, or an error if the operation fails. #[inline] -pub fn decrease_allowance(_id: AssetId, _spender: AccountId, _value: Balance) -> Result<()> { - // let allowance = assets::allowance(id, owner, spender)?; - // assets::cancel_approval(id, spender.clone())?; - // assets::approve_transfer(id, spender, value) - Ok(()) +pub fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { + ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, DECREASE_ALLOWANCE])) + .input::<(AssetId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, spender, value)) } -/// 2. PSP-22 Metadata Interface: -/// - token_name -/// - token_symbol -/// - token_decimals - -/// Returns the token name for a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// The name of the token as a byte vector, or an error if the operation fails. -#[inline] -pub fn token_name(id: AssetId) -> Result> { - assets::token_name(id) -} +pub mod metadata { + use super::*; + /// Returns the token name for a given asset ID. + /// + /// # Arguments + /// * `id` - The ID of the asset. + /// + /// # Returns + /// The name of the token as a byte vector, or an error if the operation fails. + #[inline] + pub fn token_name(id: AssetId) -> Result> { + ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, TOKEN_NAME])) + .input::() + .output::>, true>() + .handle_error_code::() + .call(&(id)) + .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) + } -/// Returns the token symbol for a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// The symbol of the token as a byte vector, or an error if the operation fails. -#[inline] -pub fn token_symbol(id: AssetId) -> Result> { - assets::token_symbol(id) -} + /// Returns the token symbol for a given asset ID. + /// + /// # Arguments + /// * `id` - The ID of the asset. + /// + /// # Returns + /// The symbol of the token as a byte vector, or an error if the operation fails. + #[inline] + pub fn token_symbol(id: AssetId) -> Result> { + ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, TOKEN_SYMBOL])) + .input::() + .output::>, true>() + .handle_error_code::() + .call(&(id)) + .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) + } -/// Returns the token decimals for a given asset ID. -/// -/// # Arguments -/// * `id` - The ID of the asset. -/// -/// # Returns -/// The number of decimals of the token as a byte vector, or an error if the operation fails. -#[inline] -pub fn token_decimals(id: AssetId) -> Result { - assets::token_decimals(id) + /// Returns the token decimals for a given asset ID. + /// + /// # Arguments + /// * `id` - The ID of the asset. + /// + /// # Returns + /// The number of decimals of the token as a byte vector, or an error if the operation fails. + #[inline] + pub fn token_decimals(id: AssetId) -> Result { + ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, TOKEN_DECIMALS])) + .input::() + .output::>, true>() + .handle_error_code::() + .call(&(id)) + .and_then(|v| ::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) + } } -// /// 3. Asset Management: -// /// - create -// /// - start_destroy -// /// - destroy_accounts -// /// - destroy_approvals -// /// - finish_destroy -// /// - set_metadata -// /// - clear_metadata -// +// pub asset_management { // /// Create a new token with a given asset ID. // /// // /// # Arguments @@ -200,9 +265,9 @@ pub fn token_decimals(id: AssetId) -> Result { // /// // /// # Returns // /// Returns `Ok(())` if successful, or an error if the creation fails. -// // pub fn create(id: AssetId, admin: impl Into>, min_balance: Balance) -> Result<()> { -// // assets::create(id, admin, min_balance) -// // } +// pub fn create(id: AssetId, admin: impl Into>, min_balance: Balance) -> Result<()> { +// Ok(()) +// } // // /// Start the process of destroying a token with a given asset ID. // /// @@ -211,11 +276,9 @@ pub fn token_decimals(id: AssetId) -> Result { // /// // /// # Returns // /// Returns `Ok(())` if successful, or an error if the operation fails. -// // fn start_destroy(id: AssetId) -> Result<()> { -// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::StartDestroy { -// // id: id.into(), -// // }))?) -// // } +// fn start_destroy(id: AssetId) -> Result<()> { +// Ok(()) +// } // // /// Destroy all accounts associated with a token with a given asset ID. // /// @@ -224,11 +287,9 @@ pub fn token_decimals(id: AssetId) -> Result { // /// // /// # Returns // /// Returns `Ok(())` if successful, or an error if the operation fails. -// // fn destroy_accounts(id: AssetId) -> Result<()> { -// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::DestroyAccounts { -// // id: id.into(), -// // }))?) -// // } +// fn destroy_accounts(id: AssetId) -> Result<()> { +// Ok(()) +// } // // /// Destroy all approvals associated with a token with a given asset ID. // /// @@ -237,11 +298,9 @@ pub fn token_decimals(id: AssetId) -> Result { // /// // /// # Returns // /// Returns `Ok(())` if successful, or an error if the operation fails. -// // fn destroy_approvals(id: AssetId) -> Result<()> { -// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::DestroyApprovals { -// // id: id.into(), -// // }))?) -// // } +// fn destroy_approvals(id: AssetId) -> Result<()> { +// Ok(()) +// } // // /// Complete the process of destroying a token with a given asset ID. // /// @@ -250,11 +309,9 @@ pub fn token_decimals(id: AssetId) -> Result { // /// // /// # Returns // /// Returns `Ok(())` if successful, or an error if the operation fails. -// // fn finish_destroy(id: AssetId) -> Result<()> { -// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::FinishDestroy { -// // id: id.into(), -// // }))?) -// // } +// fn finish_destroy(id: AssetId) -> Result<()> { +// Ok(()) +// } // // /// Set the metadata for a token with a given asset ID. // /// @@ -263,9 +320,9 @@ pub fn token_decimals(id: AssetId) -> Result { // /// // /// # Returns // /// Returns `Ok(())` if successful, or an error if the operation fails. -// // pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) -> Result<()> { -// // assets::set_metadata(id, name, symbol, decimals) -// // } +// pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) -> Result<()> { +// Ok(()) +// } // // /// Clear the metadata for a token with a given asset ID. // /// @@ -274,11 +331,10 @@ pub fn token_decimals(id: AssetId) -> Result { // /// // /// # Returns // /// Returns `Ok(())` if successful, or an error if the operation fails. -// // fn clear_metadata(id: AssetId) -> Result<()> { -// // Ok(dispatch(RuntimeCall::Assets(AssetsCall::ClearMetadata { -// // id: id.into(), -// // }))?) -// // } +// fn clear_metadata(id: AssetId) -> Result<()> { +// Ok(()) +// } +// } // // pub fn asset_exists(id: AssetId) -> Result { // assets::asset_exists(id) @@ -332,15 +388,15 @@ impl From for FungiblesError { let encoded = value.0.to_le_bytes(); match encoded { // Balances. - [MODULE_ERROR, BALANCES, 2, _] => FungiblesError::NoBalance, + [_, BALANCES, 2, _] => FungiblesError::NoBalance, // Assets. - [MODULE_ERROR, ASSETS, 0, _] => FungiblesError::NoAccount, - [MODULE_ERROR, ASSETS, 1, _] => FungiblesError::NoPermission, - [MODULE_ERROR, ASSETS, 2, _] => FungiblesError::Unknown, - [MODULE_ERROR, ASSETS, 3, _] => FungiblesError::InUse, - [MODULE_ERROR, ASSETS, 5, _] => FungiblesError::MinBalanceZero, - [MODULE_ERROR, ASSETS, 7, _] => FungiblesError::InsufficientAllowance, - [MODULE_ERROR, ASSETS, 10, _] => FungiblesError::AssetNotLive, + [_, ASSETS, 0, _] => FungiblesError::NoAccount, + [_, ASSETS, 1, _] => FungiblesError::NoPermission, + [_, ASSETS, 2, _] => FungiblesError::Unknown, + [_, ASSETS, 3, _] => FungiblesError::InUse, + [_, ASSETS, 5, _] => FungiblesError::MinBalanceZero, + [_, ASSETS, 7, _] => FungiblesError::InsufficientAllowance, + [_, ASSETS, 10, _] => FungiblesError::AssetNotLive, _ => FungiblesError::Other(value), } } @@ -407,7 +463,7 @@ mod tests { Corruption, Unavailable, RootNotAllowed, - UnknownFunctionCall, + UnknownCall, DecodingFailed, ]; for error in other_errors { diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index 00021fc0..197db710 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1,235 +1,2 @@ -use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec, scale::Decode}; - -use crate::{ - constants::{ASSETS, DECODING_FAILED, DISPATCH, READ_STATE}, - primitives::{AccountId, AssetId, Balance}, - v0::V0, - Result, StatusCode, -}; - +#[cfg(feature = "fungibles")] pub mod fungibles; - -/// [Pallet Assets](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/assets/src/lib.rs): -/// 1. Dispatchables -/// 2. Read state functions -/// -/// 1. Dispatchables within pallet assets (TrustBackedAssets instance): -/// - create -/// - start_destroy -/// - destroy_accounts -/// - destroy_approvals -/// - finish_destroy -/// - mint -/// - burn -/// - transfer -/// - transfer_keep_alive -const TRANSFER_KEEP_ALIVE: u8 = 9; -/// - set_metadata -/// - clear_metadata -/// - approve_transfer -const APPROVE_TRANSFER: u8 = 22; -/// - cancel_approval -const CANCEL_APPROVAL: u8 = 23; -/// - transfer_approved -const TRANSFER_APPROVED: u8 = 25; - -/// Issue a new class of fungible assets from a public origin. -// pub(crate) fn create( -// id: AssetId, -// admin: impl Into>, -// min_balance: Balance, -// ) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::Create { -// id: id.into(), -// admin: admin.into(), -// min_balance, -// })) -// } -// -// /// Start the process of destroying a fungible asset class. -// pub(crate) fn start_destroy(id: AssetId) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::StartDestroy { id: id.into() })) -// } -// -// /// Destroy all accounts associated with a given asset. -// pub(crate) fn destroy_accounts(id: AssetId) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::DestroyAccounts { id: id.into() })) -// } -// -// /// Destroy all approvals associated with a given asset up to the max (see runtime configuration Assets `RemoveItemsLimit`). -// pub(crate) fn destroy_approvals(id: AssetId) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::DestroyApprovals { id: id.into() })) -// } -// -// /// Complete destroying asset and unreserve currency. -// pub(crate) fn finish_destroy(id: AssetId) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::FinishDestroy { id: id.into() })) -// } - -// /// Mint assets of a particular class. -// pub(crate) fn mint( -// id: AssetId, -// beneficiary: impl Into>, -// amount: Balance, -// ) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::Mint { -// id: id.into(), -// beneficiary: beneficiary.into(), -// amount: Compact(amount), -// })) -// } -// -// /// Reduce the balance of `who` by as much as possible up to `amount` assets of `id`. -// pub(crate) fn burn(id: AssetId, who: impl Into>, amount: Balance) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::Burn { -// id: id.into(), -// who: who.into(), -// amount: Compact(amount), -// })) -// } - -/// Move some assets from the sender account to another, keeping the sender account alive. -#[inline] -pub fn transfer_keep_alive(id: AssetId, target: AccountId, amount: Balance) -> Result<()> { - ChainExtensionMethod::build(u32::from_le_bytes([ - V0, - DISPATCH, - ASSETS, - // E.D. is always respected with transferring tokens via the API. - TRANSFER_KEEP_ALIVE, - ])) - .input::<(AssetId, AccountId, Balance)>() - .output::, true>() - .handle_error_code::() - .call(&(id, target, amount)) -} - -// /// Set the metadata for an asset. -// pub(crate) fn set_metadata( -// id: AssetId, -// name: Vec, -// symbol: Vec, -// decimals: u8, -// ) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::SetMetadata { id: id.into(), name, symbol, decimals })) -// } - -// /// Clear the metadata for an asset. -// pub(crate) fn clear_metadata(id: AssetId) -> Result<()> { -// dispatch(RuntimeCall::Assets(AssetsCall::ClearMetadata { id: id.into() })) -// } - -/// Approve an amount of asset for transfer by a delegated third-party account. -#[inline] -pub fn approve_transfer(id: AssetId, delegate: AccountId, amount: Balance) -> Result<()> { - ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, ASSETS, APPROVE_TRANSFER])) - .input::<(AssetId, AccountId, Balance)>() - .output::, true>() - .handle_error_code::() - .call(&(id, delegate, amount)) -} - -/// Cancel all of some asset approved for delegated transfer by a third-party account. -#[inline] -pub fn cancel_approval(id: AssetId, delegate: AccountId) -> Result<()> { - ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, ASSETS, CANCEL_APPROVAL])) - .input::<(AssetId, AccountId)>() - .output::, true>() - .handle_error_code::() - .call(&(id, delegate)) -} - -/// Transfer some asset balance from a previously delegated account to some third-party -/// account. -#[inline] -pub fn transfer_approved( - id: AssetId, - from: AccountId, - to: AccountId, - amount: Balance, -) -> Result<()> { - ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, ASSETS, TRANSFER_APPROVED])) - .input::<(AssetId, AccountId, AccountId, Balance)>() - .output::, true>() - .handle_error_code::() - .call(&(id, from, to, amount)) -} - -/// 2. Read state functions: -/// - total_supply -const TOTAL_SUPPLY: u8 = 0; -/// - balance_of -const BALANCE_OF: u8 = 1; -/// - allowance -const ALLOWANCE: u8 = 2; -/// - token_name -const TOKEN_NAME: u8 = 3; -/// - token_symbol -const TOKEN_SYMBOL: u8 = 4; -/// - token_decimals -const TOKEN_DECIMALS: u8 = 5; -/// - asset_exists - -#[inline] -pub fn total_supply(id: AssetId) -> Result { - ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, ASSETS, TOTAL_SUPPLY])) - .input::() - .output::>, true>() - .handle_error_code::() - .call(&(id)) - .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) -} - -#[inline] -pub fn balance_of(id: AssetId, owner: AccountId) -> Result { - ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, ASSETS, BALANCE_OF])) - .input::<(AssetId, AccountId)>() - .output::>, true>() - .handle_error_code::() - .call(&(id, owner)) - .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) -} - -#[inline] -pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { - ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, ASSETS, ALLOWANCE])) - .input::<(AssetId, AccountId, AccountId)>() - .output::>, true>() - .handle_error_code::() - .call(&(id, owner, spender)) - .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) -} - -#[inline] -pub fn token_name(id: AssetId) -> Result> { - ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, ASSETS, TOKEN_NAME])) - .input::() - .output::>, true>() - .handle_error_code::() - .call(&(id)) - .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) -} -// -#[inline] -pub fn token_symbol(id: AssetId) -> Result> { - ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, ASSETS, TOKEN_SYMBOL])) - .input::() - .output::>, true>() - .handle_error_code::() - .call(&(id)) - .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) -} - -#[inline] -pub fn token_decimals(id: AssetId) -> Result { - ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, ASSETS, TOKEN_DECIMALS])) - .input::() - .output::>, true>() - .handle_error_code::() - .call(&(id)) - .and_then(|v| ::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) -} - -// pub(crate) fn asset_exists(id: AssetId) -> Result { -// state::read(RuntimeStateKeys::Assets(AssetsKeys::AssetExists(id))) -// } diff --git a/pop-api/src/v0/balances.rs b/pop-api/src/v0/balances.rs deleted file mode 100644 index bf029178..00000000 --- a/pop-api/src/v0/balances.rs +++ /dev/null @@ -1,88 +0,0 @@ -use crate::{ - dispatch, primitives::MultiAddress, v0::RuntimeCall, AccountId, PopApiError, - PopApiError::UnknownStatusCode, -}; - -type Result = core::result::Result; - -pub fn transfer_keep_alive( - dest: impl Into>, - value: u128, -) -> Result<()> { - Ok(dispatch(RuntimeCall::Balances(BalancesCall::TransferKeepAlive { - dest: dest.into(), - value, - }))?) -} - -#[derive(scale::Encode)] -#[allow(dead_code)] -pub(crate) enum BalancesCall { - #[codec(index = 3)] - TransferKeepAlive { - dest: MultiAddress, - #[codec(compact)] - value: u128, - }, -} - -#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum Error { - /// Vesting balance too high to send value. - VestingBalance, - /// Account liquidity restrictions prevent withdrawal. - LiquidityRestrictions, - /// Balance too low to send value. - InsufficientBalance, - /// Value too low to create account due to existential deposit. - ExistentialDeposit, - /// Transfer/payment would kill account. - Expendability, - /// A vesting schedule already exists for this account. - ExistingVestingSchedule, - /// Beneficiary account must pre-exist. - DeadAccount, - /// Number of named reserves exceed `MaxReserves`. - TooManyReserves, - /// Number of holds exceed `VariantCountOf`. - TooManyHolds, - /// Number of freezes exceed `MaxFreezes`. - TooManyFreezes, - /// The issuance cannot be modified since it is already deactivated. - IssuanceDeactivated, - /// The delta cannot be zero. - DeltaZero, -} - -impl TryFrom for Error { - type Error = PopApiError; - - fn try_from(status_code: u32) -> core::result::Result { - use Error::*; - match status_code { - 0 => Ok(VestingBalance), - 1 => Ok(LiquidityRestrictions), - 2 => Ok(InsufficientBalance), - 3 => Ok(ExistentialDeposit), - 4 => Ok(Expendability), - 5 => Ok(ExistingVestingSchedule), - 6 => Ok(DeadAccount), - 7 => Ok(TooManyReserves), - 8 => Ok(TooManyHolds), - 9 => Ok(TooManyFreezes), - 10 => Ok(IssuanceDeactivated), - 11 => Ok(DeltaZero), - _ => Err(UnknownStatusCode(status_code)), - } - } -} - -impl From for Error { - fn from(error: PopApiError) -> Self { - match error { - PopApiError::Balances(e) => e, - _ => panic!("expected balances error"), - } - } -} diff --git a/pop-api/src/v0/cross_chain/coretime.rs b/pop-api/src/v0/cross_chain/coretime.rs deleted file mode 100644 index 0039ed20..00000000 --- a/pop-api/src/v0/cross_chain/coretime.rs +++ /dev/null @@ -1,11 +0,0 @@ -use crate::{ - primitives::cross_chain::{CrossChainMessage, OnDemand, RelayChainMessage}, - send_xcm, -}; - -/// Send a cross-chain message to place a sport order for instantaneous coretime. -pub fn place_spot_order(max_amount: u128, para_id: u32) -> crate::cross_chain::Result<()> { - Ok(send_xcm(CrossChainMessage::Relay(RelayChainMessage::OnDemand( - OnDemand::PlaceOrderKeepAlive { max_amount, para_id }, - )))?) -} diff --git a/pop-api/src/v0/cross_chain/mod.rs b/pop-api/src/v0/cross_chain/mod.rs deleted file mode 100644 index 6732c119..00000000 --- a/pop-api/src/v0/cross_chain/mod.rs +++ /dev/null @@ -1,107 +0,0 @@ -pub mod coretime; - -use crate::{PopApiError::UnknownStatusCode, *}; - -type Result = core::result::Result; - -#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum Error { - /// The desired destination was unreachable, generally because there is a no way of routing - /// to it. - Unreachable, - /// There was some other issue (i.e. not to do with routing) in sending the message. - /// Perhaps a lack of space for buffering the message. - SendFailure, - /// The message execution fails the filter. - Filtered, - /// The message's weight could not be determined. - UnweighableMessage, - /// The destination `Location` provided cannot be inverted. - DestinationNotInvertible, - /// The assets to be sent are empty. - Empty, - /// Could not re-anchor the assets to declare the fees for the destination chain. - CannotReanchor, - /// Too many assets have been attempted for transfer. - TooManyAssets, - /// Origin is invalid for sending. - InvalidOrigin, - /// The version of the `Versioned` value used is not able to be interpreted. - BadVersion, - /// The given location could not be used (e.g. because it cannot be expressed in the - /// desired version of XCM). - BadLocation, - /// The referenced subscription could not be found. - NoSubscription, - /// The location is invalid since it already has a subscription from us. - AlreadySubscribed, - /// Could not check-out the assets for teleportation to the destination chain. - CannotCheckOutTeleport, - /// The owner does not own (all) of the asset that they wish to do the operation on. - LowBalance, - /// The asset owner has too many locks on the asset. - TooManyLocks, - /// The given account is not an identifiable sovereign account for any location. - AccountNotSovereign, - /// The operation required fees to be paid which the initiator could not meet. - FeesNotMet, - /// A remote lock with the corresponding data could not be found. - LockNotFound, - /// The unlock operation cannot succeed because there are still consumers of the lock. - InUse, - /// Invalid non-concrete asset. - InvalidAssetNotConcrete, - /// Invalid asset, reserve chain could not be determined for it. - InvalidAssetUnknownReserve, - /// Invalid asset, do not support remote asset reserves with different fees reserves. - InvalidAssetUnsupportedReserve, - /// Too many assets with different reserve locations have been attempted for transfer. - TooManyReserves, - /// Local XCM execution incomplete. - LocalExecutionIncomplete, -} - -impl TryFrom for Error { - type Error = PopApiError; - - fn try_from(status_code: u32) -> core::result::Result { - use Error::*; - match status_code { - 0 => Ok(Unreachable), - 1 => Ok(SendFailure), - 2 => Ok(Filtered), - 3 => Ok(UnweighableMessage), - 4 => Ok(DestinationNotInvertible), - 5 => Ok(Empty), - 6 => Ok(CannotReanchor), - 7 => Ok(TooManyAssets), - 8 => Ok(InvalidOrigin), - 9 => Ok(BadVersion), - 10 => Ok(BadLocation), - 11 => Ok(NoSubscription), - 12 => Ok(AlreadySubscribed), - 13 => Ok(CannotCheckOutTeleport), - 14 => Ok(LowBalance), - 15 => Ok(TooManyLocks), - 16 => Ok(AccountNotSovereign), - 17 => Ok(FeesNotMet), - 18 => Ok(LockNotFound), - 19 => Ok(InUse), - 20 => Ok(InvalidAssetNotConcrete), - 21 => Ok(InvalidAssetUnknownReserve), - 22 => Ok(InvalidAssetUnsupportedReserve), - 23 => Ok(TooManyReserves), - _ => Err(UnknownStatusCode(status_code)), - } - } -} - -impl From for Error { - fn from(error: PopApiError) -> Self { - match error { - PopApiError::Xcm(e) => e, - _ => panic!("expected xcm error"), - } - } -} diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index f7dab6b4..1c3642e1 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -2,12 +2,6 @@ use crate::{primitives::error::Error, StatusCode}; #[cfg(feature = "assets")] pub mod assets; -#[cfg(feature = "balances")] -pub mod balances; -#[cfg(feature = "cross-chain")] -pub mod cross_chain; -#[cfg(feature = "nfts")] -pub mod nfts; pub(crate) const V0: u8 = 0; diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs deleted file mode 100644 index 63b90a1f..00000000 --- a/pop-api/src/v0/nfts.rs +++ /dev/null @@ -1,883 +0,0 @@ -use super::RuntimeCall; -use crate::{PopApiError::UnknownStatusCode, *}; -use ink::prelude::vec::Vec; -use primitives::{ApprovalsLimit, BoundedBTreeMap, KeyLimit, MultiAddress}; -pub use primitives::{CollectionId, ItemId}; -use scale::Encode; -pub use types::*; - -type StringLimit = u32; -type MaxTips = u32; - -/// Issue a new collection of non-fungible items -pub fn create( - admin: impl Into>, - config: CollectionConfig, -) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::Create { admin: admin.into(), config }))?) -} - -/// Destroy a collection of fungible items. -pub fn destroy(collection: CollectionId) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::Destroy { collection }))?) -} - -/// Mint an item of a particular collection. -pub fn mint( - collection: CollectionId, - item: ItemId, - mint_to: impl Into>, -) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::Mint { - collection, - item, - mint_to: mint_to.into(), - witness_data: None, - }))?) -} - -/// Destroy a single item. -pub fn burn(collection: CollectionId, item: ItemId) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::Burn { collection, item }))?) -} - -/// Move an item from the sender account to another. -pub fn transfer( - collection: CollectionId, - item: ItemId, - dest: impl Into>, -) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::Transfer { collection, item, dest: dest.into() }))?) -} - -/// Re-evaluate the deposits on some items. -pub fn redeposit(collection: CollectionId, items: Vec) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::Redeposit { collection, items }))?) -} - -/// Change the Owner of a collection. -pub fn transfer_ownership( - collection: CollectionId, - new_owner: impl Into>, -) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::TransferOwnership { - collection, - new_owner: new_owner.into(), - }))?) -} - -/// Set (or reset) the acceptance of ownership for a particular account. -pub fn set_accept_ownership( - collection: CollectionId, - maybe_collection: Option, -) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::SetAcceptOwnership { collection, maybe_collection }))?) -} - -/// Set the maximum number of items a collection could have. -pub fn set_collection_max_supply(collection: CollectionId, max_supply: u32) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::SetCollectionMaxSupply { collection, max_supply }))?) -} - -/// Update mint settings. -pub fn update_mint_settings(collection: CollectionId, mint_settings: MintSettings) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::UpdateMintSettings { collection, mint_settings }))?) -} - -/// Get the owner of the item, if the item exists. -pub fn owner(collection: CollectionId, item: ItemId) -> Result> { - Ok(state::read(RuntimeStateKeys::Nfts(NftsKeys::Owner(collection, item)))?) -} - -/// Get the owner of the collection, if the collection exists. -pub fn collection_owner(collection: CollectionId) -> Result> { - Ok(state::read(RuntimeStateKeys::Nfts(NftsKeys::CollectionOwner(collection)))?) -} - -/// Get the details of a collection. -pub fn collection(collection: CollectionId) -> Result> { - Ok(state::read(RuntimeStateKeys::Nfts(NftsKeys::Collection(collection)))?) -} - -/// Get the details of an item. -pub fn item(collection: CollectionId, item: ItemId) -> Result> { - Ok(state::read(RuntimeStateKeys::Nfts(NftsKeys::Item(collection, item)))?) -} - -pub mod approvals { - use super::*; - - /// Approve an item to be transferred by a delegated third-party account. - pub fn approve_transfer( - collection: CollectionId, - item: ItemId, - delegate: impl Into>, - maybe_deadline: Option, - ) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::ApproveTransfer { - collection, - item, - delegate: delegate.into(), - maybe_deadline, - }))?) - } - - /// Cancel one of the transfer approvals for a specific item. - pub fn cancel_approval( - collection: CollectionId, - item: ItemId, - delegate: impl Into>, - ) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::CancelApproval { - collection, - item, - delegate: delegate.into(), - }))?) - } - - /// Cancel all the approvals of a specific item. - pub fn clear_all_transfer_approvals(collection: CollectionId, item: ItemId) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::ClearAllTransferApprovals { collection, item }))?) - } -} - -pub mod attributes { - use super::*; - - /// Approve item's attributes to be changed by a delegated third-party account. - pub fn approve_item_attribute( - collection: CollectionId, - item: ItemId, - delegate: impl Into>, - ) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::ApproveItemAttributes { - collection, - item, - delegate: delegate.into(), - }))?) - } - - /// Cancel the previously provided approval to change item's attributes. - pub fn cancel_item_attributes_approval( - collection: CollectionId, - item: ItemId, - delegate: impl Into>, - ) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::CancelItemAttributesApproval { - collection, - item, - delegate: delegate.into(), - }))?) - } - - /// Set an attribute for a collection or item. - pub fn set_attribute( - collection: CollectionId, - maybe_item: Option, - namespace: AttributeNamespace, - key: BoundedVec, - value: BoundedVec, - ) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::SetAttribute { - collection, - maybe_item, - namespace, - key, - value, - }))?) - } - - /// Clear an attribute for a collection or item. - pub fn clear_attribute( - collection: CollectionId, - maybe_item: Option, - namespace: AttributeNamespace, - key: BoundedVec, - ) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::ClearAttribute { - collection, - maybe_item, - namespace, - key, - }))?) - } - - /// Get the attribute value of `item` of `collection` corresponding to `key`. - pub fn attribute( - collection: CollectionId, - item: ItemId, - key: BoundedVec, - ) -> Result>> { - Ok(state::read(RuntimeStateKeys::Nfts(NftsKeys::Attribute(collection, item, key)))?) - } - - // /// Get the custom attribute value of `item` of `collection` corresponding to `key`. - // pub fn custom_attribute( - // account: AccountId, - // collection: CollectionId, - // item: ItemId, - // key: BoundedVec, - // ) -> Result>> { - // Ok(state::read(RuntimeStateKeys::Nfts(NftsKeys::CustomAttribute( - // account, collection, item, key, - // )))?) - // } - - /// Get the system attribute value of `item` of `collection` corresponding to `key` if - /// `item` is `Some`. Otherwise, returns the system attribute value of `collection` - /// corresponding to `key`. - pub fn system_attribute( - collection: CollectionId, - item: Option, - key: BoundedVec, - ) -> Result>> { - Ok(state::read(RuntimeStateKeys::Nfts(NftsKeys::SystemAttribute(collection, item, key)))?) - } - - /// Get the attribute value of `item` of `collection` corresponding to `key`. - pub fn collection_attribute( - collection: CollectionId, - key: BoundedVec, - ) -> Result>> { - Ok(state::read(RuntimeStateKeys::Nfts(NftsKeys::CollectionAttribute(collection, key)))?) - } -} - -pub mod locking { - use super::*; - - /// Disallows changing the metadata or attributes of the item. - pub fn lock_item_properties( - collection: CollectionId, - item: ItemId, - lock_metadata: bool, - lock_attributes: bool, - ) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::LockItemProperties { - collection, - item, - lock_metadata, - lock_attributes, - }))?) - } - - /// Disallow further unprivileged transfer of an item. - pub fn lock_item_transfer(collection: CollectionId, item: ItemId) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::LockItemTransfer { collection, item }))?) - } - - /// Re-allow unprivileged transfer of an item. - pub fn unlock_item_transfer(collection: CollectionId, item: ItemId) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::UnlockItemTransfer { collection, item }))?) - } - - /// Disallows specified settings for the whole collection. - pub fn lock_collection( - collection: CollectionId, - lock_settings: CollectionSettings, - ) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::LockCollection { collection, lock_settings }))?) - } -} - -pub mod metadata { - use super::*; - - /// Set the metadata for an item. - pub fn set_metadata( - collection: CollectionId, - item: ItemId, - data: BoundedVec, - ) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::SetMetadata { collection, item, data }))?) - } - - /// Clear the metadata for an item. - pub fn clear_metadata(collection: CollectionId, item: ItemId) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::ClearMetadata { collection, item }))?) - } - - /// Set the metadata for a collection. - pub fn set_collection_metadata( - collection: CollectionId, - data: BoundedVec, - ) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::SetCollectionMetadata { collection, data }))?) - } - - /// Clear the metadata for a collection. - pub fn clear_collection_metadata(collection: CollectionId) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::ClearCollectionMetadata { collection }))?) - } -} - -pub mod roles { - use super::*; - - /// Change the Issuer, Admin and Freezer of a collection. - pub fn set_team( - collection: CollectionId, - issuer: Option>>, - admin: Option>>, - freezer: Option>>, - ) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::SetTeam { - collection, - issuer: issuer.map(|i| i.into()), - admin: admin.map(|i| i.into()), - freezer: freezer.map(|i| i.into()), - }))?) - } -} - -pub mod trading { - use super::*; - - /// Allows to pay the tips. - pub fn pay_tips(tips: BoundedVec) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::PayTips { tips }))?) - } - - /// Set (or reset) the price for an item. - pub fn price(collection: CollectionId, item: ItemId, price: Option) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::SetPrice { collection, item, price }))?) - } - - /// Allows to buy an item if it's up for sale. - pub fn buy_item(collection: CollectionId, item: ItemId, bid_price: Balance) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::BuyItem { collection, item, bid_price }))?) - } - - pub mod swaps { - use super::*; - - /// Register a new atomic swap, declaring an intention to send an `item` in exchange for - /// `desired_item` from origin to target on the current chain. - pub fn create_swap( - offered_collection: CollectionId, - offered_item: ItemId, - desired_collection: CollectionId, - maybe_desired_item: Option, - maybe_price: Option, - duration: BlockNumber, - ) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::CreateSwap { - offered_collection, - offered_item, - desired_collection, - maybe_desired_item, - maybe_price, - duration, - }))?) - } - - /// Cancel an atomic swap. - pub fn cancel_swap(offered_collection: CollectionId, offered_item: ItemId) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::CancelSwap { - offered_collection, - offered_item, - }))?) - } - - /// Claim an atomic swap. - pub fn claim_swap( - send_collection: CollectionId, - send_item: ItemId, - receive_collection: CollectionId, - receive_item: ItemId, - ) -> Result<()> { - Ok(dispatch(RuntimeCall::Nfts(NftCalls::ClaimSwap { - send_collection, - send_item, - receive_collection, - receive_item, - }))?) - } - } -} - -#[derive(Encode)] -pub(crate) enum NftCalls { - #[codec(index = 0)] - Create { admin: MultiAddress, config: CollectionConfig }, - #[codec(index = 2)] - Destroy { collection: CollectionId }, - #[codec(index = 3)] - Mint { - collection: CollectionId, - item: ItemId, - mint_to: MultiAddress, - witness_data: Option<()>, - }, - #[codec(index = 5)] - Burn { collection: CollectionId, item: ItemId }, - #[codec(index = 6)] - Transfer { collection: CollectionId, item: ItemId, dest: MultiAddress }, - #[codec(index = 7)] - Redeposit { collection: CollectionId, items: Vec }, - #[codec(index = 8)] - LockItemTransfer { collection: CollectionId, item: ItemId }, - #[codec(index = 9)] - UnlockItemTransfer { collection: CollectionId, item: ItemId }, - #[codec(index = 10)] - LockCollection { collection: CollectionId, lock_settings: CollectionSettings }, - #[codec(index = 11)] - TransferOwnership { collection: CollectionId, new_owner: MultiAddress }, - #[codec(index = 12)] - SetTeam { - collection: CollectionId, - issuer: Option>, - admin: Option>, - freezer: Option>, - }, - #[codec(index = 15)] - ApproveTransfer { - collection: CollectionId, - item: ItemId, - delegate: MultiAddress, - maybe_deadline: Option, - }, - #[codec(index = 16)] - CancelApproval { collection: CollectionId, item: ItemId, delegate: MultiAddress }, - #[codec(index = 17)] - ClearAllTransferApprovals { collection: CollectionId, item: ItemId }, - #[codec(index = 18)] - LockItemProperties { - collection: CollectionId, - item: ItemId, - lock_metadata: bool, - lock_attributes: bool, - }, - #[codec(index = 19)] - SetAttribute { - collection: CollectionId, - maybe_item: Option, - namespace: AttributeNamespace, - key: BoundedVec, - value: BoundedVec, - }, - #[codec(index = 21)] - ClearAttribute { - collection: CollectionId, - maybe_item: Option, - namespace: AttributeNamespace, - key: BoundedVec, - }, - #[codec(index = 22)] - ApproveItemAttributes { - collection: CollectionId, - item: ItemId, - delegate: MultiAddress, - }, - #[codec(index = 23)] - CancelItemAttributesApproval { - collection: CollectionId, - item: ItemId, - delegate: MultiAddress, - }, - #[codec(index = 24)] - SetMetadata { collection: CollectionId, item: ItemId, data: BoundedVec }, - #[codec(index = 25)] - ClearMetadata { collection: CollectionId, item: ItemId }, - #[codec(index = 26)] - SetCollectionMetadata { collection: CollectionId, data: BoundedVec }, - #[codec(index = 27)] - ClearCollectionMetadata { collection: CollectionId }, - #[codec(index = 28)] - SetAcceptOwnership { collection: CollectionId, maybe_collection: Option }, - #[codec(index = 29)] - SetCollectionMaxSupply { collection: CollectionId, max_supply: u32 }, - #[codec(index = 30)] - UpdateMintSettings { collection: CollectionId, mint_settings: MintSettings }, - #[codec(index = 31)] - SetPrice { collection: CollectionId, item: ItemId, price: Option }, - #[codec(index = 32)] - BuyItem { collection: CollectionId, item: ItemId, bid_price: Balance }, - #[codec(index = 33)] - PayTips { tips: BoundedVec }, - #[codec(index = 34)] - CreateSwap { - offered_collection: CollectionId, - offered_item: ItemId, - desired_collection: CollectionId, - maybe_desired_item: Option, - maybe_price: Option, - duration: BlockNumber, - }, - #[codec(index = 35)] - CancelSwap { offered_collection: CollectionId, offered_item: ItemId }, - #[codec(index = 36)] - ClaimSwap { - send_collection: CollectionId, - send_item: ItemId, - receive_collection: CollectionId, - receive_item: ItemId, - }, -} - -#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum Error { - /// The signing account has no permission to do the operation. - NoPermission, - /// The given item ID is unknown. - UnknownCollection, - /// The item ID has already been used for an item. - AlreadyExists, - /// The approval had a deadline that expired, so the approval isn't valid anymore. - ApprovalExpired, - /// The owner turned out to be different to what was expected. - WrongOwner, - /// The witness data given does not match the current state of the chain. - BadWitness, - /// Collection ID is already taken. - CollectionIdInUse, - /// Items within that collection are non-transferable. - ItemsNonTransferable, - /// The provided account is not a delegate. - NotDelegate, - /// The delegate turned out to be different to what was expected. - WrongDelegate, - /// No approval exists that would allow the transfer. - Unapproved, - /// The named owner has not signed ownership acceptance of the collection. - Unaccepted, - /// The item is locked (non-transferable). - ItemLocked, - /// Item's attributes are locked. - LockedItemAttributes, - /// Collection's attributes are locked. - LockedCollectionAttributes, - /// Item's metadata is locked. - LockedItemMetadata, - /// Collection's metadata is locked. - LockedCollectionMetadata, - /// All items have been minted. - MaxSupplyReached, - /// The max supply is locked and can't be changed. - MaxSupplyLocked, - /// The provided max supply is less than the number of items a collection already has. - MaxSupplyTooSmall, - /// The given item ID is unknown. - UnknownItem, - /// Swap doesn't exist. - UnknownSwap, - /// The given item has no metadata set. - MetadataNotFound, - /// The provided attribute can't be found. - AttributeNotFound, - /// Item is not for sale. - NotForSale, - /// The provided bid is too low. - BidTooLow, - /// The item has reached its approval limit. - ReachedApprovalLimit, - /// The deadline has already expired. - DeadlineExpired, - /// The duration provided should be less than or equal to `MaxDeadlineDuration`. - WrongDuration, - /// The method is disabled by system settings. - MethodDisabled, - /// The provided setting can't be set. - WrongSetting, - /// Item's config already exists and should be equal to the provided one. - InconsistentItemConfig, - /// Config for a collection or an item can't be found. - NoConfig, - /// Some roles were not cleared. - RolesNotCleared, - /// Mint has not started yet. - MintNotStarted, - /// Mint has already ended. - MintEnded, - /// The provided Item was already used for claiming. - AlreadyClaimed, - /// The provided data is incorrect. - IncorrectData, - /// The extrinsic was sent by the wrong origin. - WrongOrigin, - /// The provided signature is incorrect. - WrongSignature, - /// The provided metadata might be too long. - IncorrectMetadata, - /// Can't set more attributes per one call. - MaxAttributesLimitReached, - /// The provided namespace isn't supported in this call. - WrongNamespace, - /// Can't delete non-empty collections. - CollectionNotEmpty, - /// The witness data should be provided. - WitnessRequired, -} - -impl TryFrom for Error { - type Error = PopApiError; - - fn try_from(status_code: u32) -> core::result::Result { - use Error::*; - match status_code { - 0 => Ok(NoPermission), - 1 => Ok(UnknownCollection), - 2 => Ok(AlreadyExists), - 3 => Ok(ApprovalExpired), - 4 => Ok(WrongOwner), - 5 => Ok(BadWitness), - 6 => Ok(CollectionIdInUse), - 7 => Ok(ItemsNonTransferable), - 8 => Ok(NotDelegate), - 9 => Ok(WrongDelegate), - 10 => Ok(Unapproved), - 11 => Ok(Unaccepted), - 12 => Ok(ItemLocked), - 13 => Ok(LockedItemAttributes), - 14 => Ok(LockedCollectionAttributes), - 15 => Ok(LockedItemMetadata), - 16 => Ok(LockedCollectionMetadata), - 17 => Ok(MaxSupplyReached), - 18 => Ok(MaxSupplyLocked), - 19 => Ok(MaxSupplyTooSmall), - 20 => Ok(UnknownItem), - 21 => Ok(UnknownSwap), - 22 => Ok(MetadataNotFound), - 23 => Ok(AttributeNotFound), - 24 => Ok(NotForSale), - 25 => Ok(BidTooLow), - 26 => Ok(ReachedApprovalLimit), - 27 => Ok(DeadlineExpired), - 28 => Ok(WrongDuration), - 29 => Ok(MethodDisabled), - 30 => Ok(WrongSetting), - 31 => Ok(InconsistentItemConfig), - 32 => Ok(NoConfig), - 33 => Ok(RolesNotCleared), - 34 => Ok(MintNotStarted), - 35 => Ok(MintEnded), - 36 => Ok(AlreadyClaimed), - 37 => Ok(IncorrectData), - 38 => Ok(WrongOrigin), - 39 => Ok(WrongSignature), - 40 => Ok(IncorrectMetadata), - 41 => Ok(MaxAttributesLimitReached), - 42 => Ok(WrongNamespace), - 43 => Ok(CollectionNotEmpty), - 44 => Ok(WitnessRequired), - _ => Err(UnknownStatusCode(status_code)), - } - } -} - -impl From for Error { - fn from(error: PopApiError) -> Self { - match error { - PopApiError::Nfts(e) => e, - _ => panic!("expected nfts error"), - } - } -} - -// Local implementations of pallet-nfts types -mod types { - use super::*; - use crate::{ - primitives::{CollectionId, ItemId}, - Balance, BlockNumber, - }; - pub use enumflags2::{bitflags, BitFlags}; - use scale::{Decode, EncodeLike, MaxEncodedLen}; - use scale_info::{build::Fields, meta_type, prelude::vec, Path, Type, TypeInfo, TypeParameter}; - - /// Attribute namespaces for non-fungible tokens. - #[derive(Encode)] - pub enum AttributeNamespace { - /// An attribute was set by the pallet. - Pallet, - /// An attribute was set by collection's owner. - CollectionOwner, - /// An attribute was set by item's owner. - ItemOwner, - /// An attribute was set by pre-approved account. - Account(AccountId), - } - - /// Collection's configuration. - #[derive(Encode)] - pub struct CollectionConfig { - /// Collection's settings. - pub settings: CollectionSettings, - /// Collection's max supply. - pub max_supply: Option, - /// Default settings each item will get during the mint. - pub mint_settings: MintSettings, - } - - /// Information about a collection. - #[derive(Decode, Debug, Encode, Eq, PartialEq)] - pub struct CollectionDetails { - /// Collection's owner. - pub owner: AccountId, - /// The total balance deposited by the owner for all the storage data associated with this - /// collection. Used by `destroy`. - pub owner_deposit: Balance, - /// The total number of outstanding items of this collection. - pub items: u32, - /// The total number of outstanding item metadata of this collection. - pub item_metadatas: u32, - /// The total number of outstanding item configs of this collection. - pub item_configs: u32, - /// The total number of attributes for this collection. - pub attributes: u32, - } - - /// Wrapper type for `BitFlags` that implements `Codec`. - pub struct CollectionSettings(pub BitFlags); - - impl_codec_bitflags!(CollectionSettings, u64, CollectionSetting); - - /// Support for up to 64 user-enabled features on a collection. - #[bitflags] - #[repr(u64)] - #[derive(Copy, Clone, Encode, TypeInfo)] - pub enum CollectionSetting { - /// Items in this collection are transferable. - TransferableItems, - /// The metadata of this collection can be modified. - UnlockedMetadata, - /// Attributes of this collection can be modified. - UnlockedAttributes, - /// The supply of this collection can be modified. - UnlockedMaxSupply, - /// When this isn't set then the deposit is required to hold the items of this collection. - DepositRequired, - } - - /// Information concerning the ownership of a single unique item. - #[derive(Decode, Debug, Encode, Eq, PartialEq)] - pub struct ItemDetails { - /// The owner of this item. - pub owner: AccountId, - /// The approved transferrer of this item, if one is set. - pub approvals: BoundedBTreeMap, ApprovalsLimit>, - /// The amount held in the pallet's default account for this item. Free-hold items will - /// have this as zero. - pub deposit: Balance, - } - - /// Support for up to 64 user-enabled features on an item. - #[bitflags] - #[repr(u64)] - #[derive(Copy, Clone, Encode, TypeInfo)] - pub enum ItemSetting { - /// This item is transferable. - Transferable, - /// The metadata of this item can be modified. - UnlockedMetadata, - /// Attributes of this item can be modified. - UnlockedAttributes, - } - - /// Wrapper type for `BitFlags` that implements `Codec`. - pub struct ItemSettings(pub BitFlags); - - impl_codec_bitflags!(ItemSettings, u64, ItemSetting); - - /// Information about the tip. - #[derive(Encode)] - pub struct ItemTip { - /// The collection of the item. - pub(super) collection: CollectionId, - /// An item of which the tip is sent for. - pub(super) item: ItemId, - /// A sender of the tip. - pub(super) receiver: AccountId, - /// An amount the sender is willing to tip. - pub(super) amount: Balance, - } - - /// Holds the information about minting. - #[derive(Encode)] - pub struct MintSettings { - /// Whether anyone can mint or if minters are restricted to some subset. - pub mint_type: MintType, - /// An optional price per mint. - pub price: Option, - /// When the mint starts. - pub start_block: Option, - /// When the mint ends. - pub end_block: Option, - /// Default settings each item will get during the mint. - pub default_item_settings: ItemSettings, - } - - /// Mint type. Can the NFT be created by anyone, or only the creator of the collection, - /// or only by wallets that already hold an NFT from a certain collection? - /// The ownership of a privately minted NFT is still publicly visible. - #[derive(Encode)] - pub enum MintType { - /// Only an `Issuer` could mint items. - Issuer, - /// Anyone could mint items. - Public, - /// Only holders of items in specified collection could mint new items. - HolderOf(CollectionId), - } - - /// Holds the details about the price. - #[derive(Encode)] - pub struct PriceWithDirection { - /// An amount. - pub(super) amount: Balance, - /// A direction (send or receive). - pub(super) direction: PriceDirection, - } - - /// Specifies whether the tokens will be sent or received. - #[derive(Encode)] - pub enum PriceDirection { - /// Tokens will be sent. - Send, - /// Tokens will be received. - Receive, - } - - macro_rules! impl_codec_bitflags { - ($wrapper:ty, $size:ty, $bitflag_enum:ty) => { - impl MaxEncodedLen for $wrapper { - fn max_encoded_len() -> usize { - <$size>::max_encoded_len() - } - } - impl Encode for $wrapper { - fn using_encoded R>(&self, f: F) -> R { - self.0.bits().using_encoded(f) - } - } - impl EncodeLike for $wrapper {} - impl Decode for $wrapper { - fn decode( - input: &mut I, - ) -> core::result::Result { - let field = <$size>::decode(input)?; - Ok(Self(BitFlags::from_bits(field as $size).map_err(|_| "invalid value")?)) - } - } - - impl TypeInfo for $wrapper { - type Identity = Self; - - fn type_info() -> Type { - Type::builder() - .path(Path::new("BitFlags", module_path!())) - .type_params(vec![TypeParameter::new( - "T", - Some(meta_type::<$bitflag_enum>()), - )]) - .composite( - Fields::unnamed() - .field(|f| f.ty::<$size>().type_name(stringify!($bitflag_enum))), - ) - } - } - }; - } - pub(crate) use impl_codec_bitflags; -} diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml index 5cbd6d6c..e7d55ffe 100644 --- a/primitives/Cargo.toml +++ b/primitives/Cargo.toml @@ -1,21 +1,17 @@ [package] name = "pop-primitives" +description = "Primitives crate for Pop" license = "GPL-3.0-only" version = "0.0.0" edition = "2021" [dependencies] -bounded-collections = { version = "0.1", default-features = false } -scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } -scale-info = { version = "2.10", default-features = false, features = ["derive"], optional = true } +codec.workspace = true +scale-info.workspace = true [features] default = ["std"] std = [ - "bounded-collections/std", - "scale/std", + "codec/std", "scale-info/std", -] -assets = [] -cross-chain = [] -nfts = [] +] \ No newline at end of file diff --git a/primitives/README.md b/primitives/README.md new file mode 100644 index 00000000..ded7918a --- /dev/null +++ b/primitives/README.md @@ -0,0 +1 @@ +Reserved crate for pop-primitives. \ No newline at end of file diff --git a/primitives/src/cross_chain.rs b/primitives/src/cross_chain.rs deleted file mode 100644 index 381e6a61..00000000 --- a/primitives/src/cross_chain.rs +++ /dev/null @@ -1,19 +0,0 @@ -use scale::{Decode, Encode, MaxEncodedLen}; - -#[derive(Encode, Decode, Debug, MaxEncodedLen)] -pub enum CrossChainMessage { - Relay(RelayChainMessage), -} - -#[derive(Encode, Decode, Debug, MaxEncodedLen)] -pub enum RelayChainMessage { - // Rococo index: https://github.com/paritytech/polkadot-sdk/blob/629506ce061db76d31d4f7a81f4a497752b27259/polkadot/runtime/rococo/src/lib.rs#L1423 - #[codec(index = 66)] - OnDemand(OnDemand), -} - -#[derive(Encode, Decode, Debug, MaxEncodedLen)] -pub enum OnDemand { - #[codec(index = 1)] - PlaceOrderKeepAlive { max_amount: u128, para_id: u32 }, -} diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 376b440d..a51661ea 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -1,37 +1,13 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -pub use bounded_collections::{BoundedBTreeMap, BoundedBTreeSet, BoundedVec}; -use scale::{Decode, Encode, MaxEncodedLen}; +use codec::{Decode, Encode}; #[cfg(feature = "std")] use scale_info::TypeInfo; pub use v0::error; -#[cfg(feature = "cross-chain")] -pub mod cross_chain; -pub mod storage_keys; - -/// An opaque 32-byte cryptographic identifier. -#[derive(Encode, Decode, Debug, MaxEncodedLen, Eq, PartialEq)] -#[cfg_attr(feature = "std", derive(TypeInfo))] -pub struct AccountId(pub [u8; 32]); - /// Identifier for the class of asset. pub type AssetId = u32; -#[cfg(feature = "nfts")] -pub mod nfts { - use bounded_collections::ConstU32; - - /// Id used for identifying non-fungible collections. - pub type CollectionId = u32; - /// Id used for identifying non-fungible items. - pub type ItemId = u32; - /// The maximum length of an attribute key. - pub type KeyLimit = ConstU32<64>; - /// The maximum approvals an item could have. - pub type ApprovalsLimit = ConstU32<20>; -} - pub mod v0 { use super::*; pub mod error { @@ -102,7 +78,7 @@ pub mod v0 { } /// Description of what went wrong when trying to complete an operation on a token. - #[derive(Encode, Decode, Clone, Debug, MaxEncodedLen, Eq, PartialEq, Ord, PartialOrd)] + #[derive(Encode, Decode, Debug, Eq, PartialEq)] #[cfg_attr(feature = "std", derive(TypeInfo))] pub enum TokenError { /// Funds are unavailable. diff --git a/primitives/src/storage_keys.rs b/primitives/src/storage_keys.rs deleted file mode 100644 index e42dbca0..00000000 --- a/primitives/src/storage_keys.rs +++ /dev/null @@ -1,56 +0,0 @@ -#[cfg(feature = "nfts")] -use super::nfts::*; -use super::*; - -#[derive(Encode, Decode, Debug, MaxEncodedLen)] -pub enum RuntimeStateKeys { - #[cfg(feature = "cross-chain")] - #[codec(index = 1)] - ParachainSystem(ParachainSystemKeys), - #[cfg(feature = "nfts")] - #[codec(index = 50)] - Nfts(NftsKeys), - #[cfg(feature = "assets")] - #[codec(index = 52)] - Assets(AssetsKeys), -} - -#[cfg(feature = "cross-chain")] -#[derive(Encode, Decode, Debug, MaxEncodedLen)] -pub enum ParachainSystemKeys { - /// Get the last relay chain block number seen by the parachain. - LastRelayChainBlockNumber, -} - -// https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/nfts/src/impl_nonfungibles.rs -#[cfg(feature = "nfts")] -#[derive(Encode, Decode, Debug, MaxEncodedLen)] -pub enum NftsKeys { - // Get the details of a collection. - Collection(CollectionId), - /// Get the owner of the collection, if the collection exists. - CollectionOwner(CollectionId), - // Get the details of an item. - Item(CollectionId, ItemId), - /// Get the owner of the item, if the item exists. - Owner(CollectionId, ItemId), - /// Get the attribute value of `item` of `collection` corresponding to `key`. - Attribute(CollectionId, ItemId, BoundedVec), - /// Get the system attribute value of `item` of `collection` corresponding to `key` - SystemAttribute(CollectionId, Option, BoundedVec), - /// Get the attribute value of `item` of `collection` corresponding to `key`. - CollectionAttribute(CollectionId, BoundedVec), -} - -/// The required input for state queries in pallet assets. -#[cfg(feature = "assets")] -#[derive(Encode, Decode, Debug, MaxEncodedLen)] -pub enum AssetsKeys { - TotalSupply(AssetId), - BalanceOf(AssetId, AccountId), - Allowance(AssetId, AccountId, AccountId), - TokenName(AssetId), - TokenSymbol(AssetId), - TokenDecimals(AssetId), - // AssetExists(AssetId), -} diff --git a/runtime/devnet/Cargo.toml b/runtime/devnet/Cargo.toml index 70e6e9c9..455a86fd 100644 --- a/runtime/devnet/Cargo.toml +++ b/runtime/devnet/Cargo.toml @@ -22,8 +22,9 @@ scale-info.workspace = true smallvec.workspace = true # Local -pop-primitives = { workspace = true, features = ["assets", "cross-chain", "nfts"] } -pop-runtime-common = { workspace = true, default-features = false } +pop-primitives.workspace = true +pop-runtime-common.workspace = true +pallet-api.workspace = true # Substrate frame-benchmarking.workspace = true @@ -89,7 +90,6 @@ parachain-info.workspace = true [dev-dependencies] env_logger = "0.11.2" -enumflags2 = "0.7.9" hex = "0.4.3" rand = "0.8.5" @@ -119,6 +119,7 @@ std = [ "pallet-balances/std", "pallet-collator-selection/std", "pallet-contracts/std", + "pallet-api/std", "pallet-message-queue/std", "pallet-multisig/std", "pallet-nft-fractionalization/std", @@ -169,6 +170,7 @@ runtime-benchmarks = [ "frame-system-benchmarking/runtime-benchmarks", "frame-system/runtime-benchmarks", "pallet-assets/runtime-benchmarks", + "pallet-api/runtime-benchmarks", "pallet-balances/runtime-benchmarks", "pallet-collator-selection/runtime-benchmarks", "pallet-contracts/runtime-benchmarks", diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs new file mode 100644 index 00000000..ae179e4a --- /dev/null +++ b/runtime/devnet/src/config/api.rs @@ -0,0 +1,34 @@ +use crate::{config::assets::TrustBackedAssetsInstance, fungibles, Runtime, RuntimeCall}; +use codec::{Decode, Encode, MaxEncodedLen}; +use frame_support::traits::Contains; + +/// A query of runtime state. +#[derive(Encode, Decode, Debug, MaxEncodedLen)] +#[repr(u8)] +pub enum RuntimeRead { + /// Fungible token queries. + #[codec(index = 150)] + Fungibles(fungibles::Read), +} + +impl fungibles::Config for Runtime { + type AssetsInstance = TrustBackedAssetsInstance; + type WeightInfo = fungibles::weights::SubstrateWeight; +} + +/// A type to identify allowed calls to the Runtime from contracts. Used by Pop API +pub struct AllowedApiCalls; + +impl Contains for AllowedApiCalls { + fn contains(c: &RuntimeCall) -> bool { + use fungibles::Call as FungiblesCall; + matches!( + c, + RuntimeCall::Fungibles( + FungiblesCall::transfer { .. } + | FungiblesCall::approve { .. } + | FungiblesCall::increase_allowance { .. } + ) + ) + } +} diff --git a/runtime/devnet/src/config/assets.rs b/runtime/devnet/src/config/assets.rs index 2c8ea952..78aed8b5 100644 --- a/runtime/devnet/src/config/assets.rs +++ b/runtime/devnet/src/config/assets.rs @@ -1,7 +1,3 @@ -use crate::{ - deposit, AccountId, Assets, Balance, Balances, BlockNumber, Nfts, Runtime, RuntimeEvent, - RuntimeHoldReason, DAYS, EXISTENTIAL_DEPOSIT, UNIT, -}; use frame_support::{ parameter_types, traits::{AsEnsureOriginWithArg, ConstU32}, @@ -12,6 +8,11 @@ use pallet_nfts::PalletFeatures; use parachains_common::{AssetIdForTrustBackedAssets, CollectionId, ItemId, Signature}; use sp_runtime::traits::Verify; +use crate::{ + deposit, AccountId, Assets, Balance, Balances, BlockNumber, Nfts, Runtime, RuntimeEvent, + RuntimeHoldReason, DAYS, EXISTENTIAL_DEPOSIT, UNIT, +}; + /// We allow root to execute privileged asset operations. pub type AssetsForceOrigin = EnsureRoot; diff --git a/runtime/devnet/src/config/mod.rs b/runtime/devnet/src/config/mod.rs index e0aaa3a1..f62ffa76 100644 --- a/runtime/devnet/src/config/mod.rs +++ b/runtime/devnet/src/config/mod.rs @@ -1,3 +1,4 @@ +pub(crate) mod api; pub mod assets; mod contracts; mod proxy; diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs index d2bd63e7..3aed89df 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions/mod.rs @@ -1,61 +1,42 @@ -use codec::{Compact, Decode, Encode}; -use cumulus_pallet_parachain_system::RelaychainDataProvider; -use frame_support::traits::{Contains, OriginTrait}; +mod v0; + +use crate::{ + config::{ + api::{AllowedApiCalls, RuntimeRead}, + assets::TrustBackedAssetsInstance, + }, + fungibles::{ + self, + Read::{self, *}, + }, + AccountId, RuntimeCall, RuntimeOrigin, +}; +use codec::{Decode, Encode}; use frame_support::{ dispatch::{GetDispatchInfo, RawOrigin}, pallet_prelude::*, - traits::{ - fungibles::{approvals::Inspect as ApprovalInspect, metadata::Inspect as MetadataInspect}, - nonfungibles_v2::Inspect as NonFungiblesInspect, - }, + traits::{Contains, OriginTrait}, }; use pallet_contracts::chain_extension::{ BufInBufOutState, ChainExtension, Environment, Ext, InitState, RetVal, }; +use pop_primitives::AssetId; use sp_core::crypto::UncheckedFrom; -use sp_runtime::{ - traits::{BlockNumberProvider, Dispatchable}, - DispatchError, MultiAddress, -}; -use sp_std::{boxed::Box, vec::Vec}; -use xcm::{ - latest::{prelude::*, OriginKind::SovereignAccount}, - VersionedXcm, -}; - -use crate::{ - config::assets::TrustBackedAssetsInstance, AccountId, AllowedApiCalls, Balance, Runtime, - RuntimeCall, RuntimeOrigin, UNIT, -}; -use pop_primitives::{ - cross_chain::CrossChainMessage, - nfts::{CollectionId, ItemId}, - storage_keys::{ - AssetsKeys::{self, *}, - NftsKeys, ParachainSystemKeys, RuntimeStateKeys, - }, - AssetId, -}; - -mod v0; +use sp_runtime::{traits::Dispatchable, DispatchError}; +use sp_std::vec::Vec; const LOG_TARGET: &str = "pop-api::extension"; -// Versions: -const V0: u8 = 0; type ContractSchedule = ::Schedule; #[derive(Default)] pub struct PopApiExtension; -// TODO: check removal or simplification of trait bounds. impl ChainExtension for PopApiExtension where T: pallet_contracts::Config - + pallet_xcm::Config + pallet_assets::Config - + pallet_nfts::Config - + cumulus_pallet_parachain_system::Config + + fungibles::Config + frame_system::Config< RuntimeOrigin = RuntimeOrigin, AccountId = AccountId, @@ -102,8 +83,6 @@ where FuncId::ReadState => { read_state::(&mut env, version, pallet_index, call_index, params) }, - // TODO - FuncId::SendXcm => send_xcm::(&mut env), } }, Err(e) => Err(e), @@ -121,7 +100,7 @@ fn dispatch( version: u8, pallet_index: u8, call_index: u8, - params: Vec, + mut params: Vec, ) -> Result<(), DispatchError> where T: frame_system::Config, @@ -129,10 +108,19 @@ where E: Ext, { const LOG_PREFIX: &str = " dispatch |"; - let call = construct_call(version, pallet_index, call_index, params)?; + + // Prefix params with version, pallet, index to simplify decoding. + params.insert(0, version); + params.insert(1, pallet_index); + params.insert(2, call_index); + let call = ::decode(&mut ¶ms[..]) + .map_err(|_| DispatchError::Other("DecodingFailed"))?; + // Contract is the origin by default. let origin: RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); - dispatch_call::(env, call, origin, LOG_PREFIX) + match call { + VersionedDispatch::V0(call) => dispatch_call::(env, call, origin, LOG_PREFIX), + } } fn dispatch_call( @@ -165,79 +153,33 @@ where } } -fn construct_call( - version: u8, - pallet_index: u8, - call_index: u8, - params: Vec, -) -> Result { - match pallet_index { - index if index == super::Assets::index() as u8 => { - let call = versioned_construct_assets_call(version, call_index, params)?; - Ok(RuntimeCall::Assets(call)) - }, - _ => Err(DispatchError::Other("UnknownFunctionId")), - } -} - -fn construct_key( - version: u8, - pallet_index: u8, - call_index: u8, - params: Vec, -) -> Result { - match pallet_index { - 52 => { - let key = versioned_construct_assets_key(version, call_index, params)?; - Ok(RuntimeStateKeys::Assets(key)) - }, - _ => Err(DispatchError::Other("UnknownFunctionId")), - } -} - -fn versioned_construct_assets_call( - version: u8, - call_index: u8, - params: Vec, -) -> Result, DispatchError> { - match version { - V0 => v0::assets::construct_assets_call(call_index, params), - _ => Err(DispatchError::Other("UnknownFunctionId")), - } -} - -fn versioned_construct_assets_key( - version: u8, - call_index: u8, - params: Vec, -) -> Result { - match version { - V0 => v0::assets::construct_assets_key(call_index, params), - _ => Err(DispatchError::Other("UnknownFunctionId")), - } -} - fn read_state( env: &mut Environment, version: u8, pallet_index: u8, call_index: u8, - params: Vec, + mut params: Vec, ) -> Result<(), DispatchError> where T: pallet_contracts::Config + pallet_assets::Config - + pallet_nfts::Config - + cumulus_pallet_parachain_system::Config + + fungibles::Config + frame_system::Config, E: Ext, { const LOG_PREFIX: &str = " read_state |"; - let key = construct_key(version, pallet_index, call_index, params)?; + + // Prefix params with version, pallet, index to simplify decoding. + params.insert(0, version); + params.insert(1, pallet_index); + params.insert(2, call_index); + let key = >::decode(&mut ¶ms[..]) + .map_err(|_| DispatchError::Other("DecodingFailed"))?; + let result = match key { - RuntimeStateKeys::Nfts(key) => read_nfts_state::(key, env), - RuntimeStateKeys::ParachainSystem(key) => read_parachain_system_state::(key, env), - RuntimeStateKeys::Assets(key) => read_assets_state::(key, env), + VersionedStateRead::V0(key) => match key { + RuntimeRead::Fungibles(key) => read_fungibles_state::(key, env), + }, }? .encode(); log::trace!( @@ -247,48 +189,20 @@ where env.write(&result, false, None) } -fn send_xcm(env: &mut Environment) -> Result<(), DispatchError> -where - T: pallet_contracts::Config - + frame_system::Config< - RuntimeOrigin = RuntimeOrigin, - AccountId = AccountId, - RuntimeCall = RuntimeCall, - >, - E: Ext, -{ - const LOG_PREFIX: &str = " send_xcm |"; - // Read the input as CrossChainMessage. - let xc_call: CrossChainMessage = env.read_as::()?; - // Determine the call to dispatch. - let (dest, message) = match xc_call { - CrossChainMessage::Relay(message) => { - let dest = Location::parent().into_versioned(); - let assets: Asset = (Here, 10 * UNIT).into(); - let beneficiary: Location = - AccountId32 { id: (env.ext().address().clone()).into(), network: None }.into(); - let message = Xcm::builder() - .withdraw_asset(assets.clone().into()) - .buy_execution(assets.clone(), Unlimited) - .transact( - SovereignAccount, - Weight::from_parts(250_000_000, 10_000), - message.encode().into(), - ) - .refund_surplus() - .deposit_asset(assets.into(), beneficiary) - .build(); - (dest, message) - }, - }; - // TODO: revisit to replace with signed contract origin - let origin: RuntimeOrigin = RawOrigin::Root.into(); - // Generate runtime call to dispatch. - let call = RuntimeCall::PolkadotXcm(pallet_xcm::Call::send { - dest: Box::new(dest), - message: Box::new(VersionedXcm::V4(message)), - }); - dispatch_call::(env, call, origin, LOG_PREFIX) +/// Wrapper to enable versioning of runtime state reads. +#[derive(Decode, Debug)] +enum VersionedStateRead { + /// Version zero of state reads. + #[codec(index = 0)] + V0(RuntimeRead), +} + +/// Wrapper to enable versioning of runtime calls. +#[derive(Decode, Debug)] +enum VersionedDispatch { + /// Version zero of dispatch calls. + #[codec(index = 0)] + V0(RuntimeCall), } // Converts a `DispatchError` to a `u32` status code based on the version of the API the contract uses. @@ -331,7 +245,7 @@ pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { // - Represents the second level of nesting in `DOUBLE_NESTED_ERRORS`. // - Byte 3: // - Unused or represents further nested information. - 0 => v0::error::handle_unknown_error(&mut encoded_error), + 0 => v0::handle_unknown_error(&mut encoded_error), _ => encoded_error = [254, 0, 0, 0], } u32::from_le_bytes(encoded_error) @@ -350,7 +264,6 @@ pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { pub enum FuncId { Dispatch, ReadState, - SendXcm, } impl TryFrom for FuncId { @@ -364,7 +277,6 @@ impl TryFrom for FuncId { let id = match func_id { 0 => Self::Dispatch, 1 => Self::ReadState, - 2 => Self::SendXcm, _ => { return Err(DispatchError::Other("UnknownFuncId")); }, @@ -373,122 +285,27 @@ impl TryFrom for FuncId { } } -fn read_parachain_system_state( - key: ParachainSystemKeys, - env: &mut Environment, -) -> Result, DispatchError> -where - T: pallet_contracts::Config + cumulus_pallet_parachain_system::Config, - E: Ext, -{ - match key { - ParachainSystemKeys::LastRelayChainBlockNumber => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(RelaychainDataProvider::::current_block_number().encode()) - }, - } -} - -fn read_nfts_state( - key: NftsKeys, - env: &mut Environment, -) -> Result, DispatchError> -where - T: pallet_contracts::Config + pallet_nfts::Config, - E: Ext, -{ - match key { - NftsKeys::Collection(collection) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Collection::::get(collection).encode()) - }, - NftsKeys::CollectionOwner(collection) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::collection_owner(collection).encode()) - }, - NftsKeys::Item(collection, item) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Item::::get(collection, item).encode()) - }, - NftsKeys::Owner(collection, item) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::owner(collection, item).encode()) - }, - NftsKeys::Attribute(collection, item, key) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::attribute(&collection, &item, &key).encode()) - }, - // NftsKeys::CustomAttribute(account, collection, item, key) => { - // env.charge_weight(T::DbWeight::get().reads(1_u64))?; - // Ok(pallet_nfts::Pallet::::custom_attribute(&account, &collection, &item, &key) - // .encode()) - // }, - NftsKeys::SystemAttribute(collection, item, key) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::system_attribute(&collection, item.as_ref(), &key) - .encode()) - }, - NftsKeys::CollectionAttribute(collection, key) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::collection_attribute(&collection, &key).encode()) - }, - } -} - -fn read_assets_state( - key: AssetsKeys, +fn read_fungibles_state( + key: Read, env: &mut Environment, ) -> Result, DispatchError> where T: pallet_contracts::Config - + pallet_assets::Config, + + pallet_assets::Config + + fungibles::Config, E: Ext, T: frame_system::Config, { + env.charge_weight(T::DbWeight::get().reads(1_u64))?; match key { - TotalSupply(id) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_assets::Pallet::::total_supply(id).encode()) - }, - BalanceOf(id, owner) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_assets::Pallet::::balance(id, &owner.0.into()) - .encode()) - }, - Allowance(id, owner, spender) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_assets::Pallet::::allowance( - id, - &owner.0.into(), - &spender.0.into(), - ) - .encode()) - }, - TokenName(id) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok( as MetadataInspect< - AccountId, - >>::name(id) - .encode()) + TotalSupply(id) => Ok(fungibles::Pallet::::total_supply(id).encode()), + BalanceOf { id, owner } => Ok(fungibles::Pallet::::balance_of(id, &owner).encode()), + Allowance { id, owner, spender } => { + Ok(fungibles::Pallet::::allowance(id, &owner, &spender).encode()) }, - TokenSymbol(id) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok( as MetadataInspect< - AccountId, - >>::symbol(id) - .encode()) - }, - TokenDecimals(id) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok( as MetadataInspect< - AccountId, - >>::decimals(id) - .encode()) - }, - // AssetsKeys::AssetExists(id) => { - // env.charge_weight(T::DbWeight::get().reads(1_u64))?; - // Ok(pallet_assets::Pallet::::asset_exists(id).encode()) - // }, + TokenName(id) => Ok(fungibles::Pallet::::token_name(id).encode()), + TokenSymbol(id) => Ok(fungibles::Pallet::::token_symbol(id).encode()), + TokenDecimals(id) => Ok(fungibles::Pallet::::token_decimals(id).encode()), } } @@ -497,6 +314,7 @@ mod tests { use super::*; use crate::{Assets, Runtime, System}; use sp_runtime::BuildStorage; + // Test ensuring `func_id()` and `ext_id()` work as expected, i.e. extracting the first two // bytes and the last two bytes, respectively, from a 4 byte array. #[test] @@ -673,501 +491,3 @@ mod tests { }); } } -// use enumflags2::BitFlags; -// use pallet_nfts::{CollectionConfig, CollectionSetting, CollectionSettings, MintSettings}; -// use parachains_common::CollectionId; -// { -// // NFT helper functions -// fn collection_config_from_disabled_settings( -// settings: BitFlags, -// ) -> CollectionConfig { -// CollectionConfig { -// settings: CollectionSettings::from_disabled(settings), -// max_supply: None, -// mint_settings: MintSettings::default(), -// } -// } -// -// fn default_collection_config() -> CollectionConfig { -// collection_config_from_disabled_settings(CollectionSetting::DepositRequired.into()) -// } -// -// #[test] -// #[ignore] -// fn dispatch_balance_transfer_from_contract_works() { -// new_test_ext().execute_with(|| { -// let _ = env_logger::try_init(); -// -// let (wasm_binary, _) = load_wasm_module::( -// "../../pop-api/examples/balance-transfer/target/ink/balance_transfer.wasm", -// ) -// .unwrap(); -// -// let init_value = 100 * UNIT; -// -// let result = Contracts::bare_instantiate( -// ALICE, -// init_value, -// GAS_LIMIT, -// None, -// Code::Upload(wasm_binary), -// function_selector("new"), -// vec![], -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// ) -// .result -// .unwrap(); -// -// assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); -// -// let addr = result.account_id; -// -// let function = function_selector("transfer_through_runtime"); -// let value_to_send: u128 = 10 * UNIT; -// let params = [function, BOB.encode(), value_to_send.encode()].concat(); -// -// let bob_balance_before = Balances::free_balance(&BOB); -// assert_eq!(bob_balance_before, INITIAL_AMOUNT); -// -// let result = Contracts::bare_call( -// ALICE, -// addr.clone(), -// 0, -// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), -// None, -// params, -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// pallet_contracts::Determinism::Enforced, -// ); -// -// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { -// log::debug!( -// "Contract debug buffer - {:?}", -// String::from_utf8(result.debug_message.clone()) -// ); -// log::debug!("result: {:?}", result); -// } -// -// // check for revert -// assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); -// -// let bob_balance_after = Balances::free_balance(&BOB); -// assert_eq!(bob_balance_before + value_to_send, bob_balance_after); -// }); -// } -// -// // Create a test for tesing create_nft_collection -// #[test] -// #[ignore] -// fn dispatch_nfts_create_nft_collection() { -// new_test_ext().execute_with(|| { -// let _ = env_logger::try_init(); -// -// let (wasm_binary, _) = load_wasm_module::( -// "../../pop-api/examples/nfts/target/ink/pop_api_nft_example.wasm", -// ) -// .unwrap(); -// -// let init_value = 100 * UNIT; -// -// let result = Contracts::bare_instantiate( -// ALICE, -// init_value, -// GAS_LIMIT, -// None, -// Code::Upload(wasm_binary), -// function_selector("new"), -// vec![], -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// ) -// .result -// .unwrap(); -// -// assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); -// -// let addr = result.account_id; -// -// let function = function_selector("create_nft_collection"); -// -// let params = [function].concat(); -// -// let result = Contracts::bare_call( -// ALICE, -// addr.clone(), -// 0, -// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), -// None, -// params, -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// pallet_contracts::Determinism::Enforced, -// ); -// -// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { -// log::debug!( -// "Contract debug buffer - {:?}", -// String::from_utf8(result.debug_message.clone()) -// ); -// log::debug!("result: {:?}", result); -// } -// -// // check that the nft collection was created -// assert_eq!(Nfts::collection_owner(0), Some(addr.clone().into())); -// -// // test reading the collection -// let function = function_selector("read_collection"); -// -// let params = [function, 0.encode()].concat(); -// -// let result = Contracts::bare_call( -// ALICE, -// addr.clone(), -// 0, -// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), -// None, -// params, -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// pallet_contracts::Determinism::Enforced, -// ); -// -// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { -// log::debug!( -// "Contract debug buffer - {:?}", -// String::from_utf8(result.debug_message.clone()) -// ); -// log::debug!("result: {:?}", result); -// } -// -// // assert that the collection was read successfully -// assert_eq!(result.result.clone().unwrap().data, vec![1, 1]); -// }); -// } -// -// #[test] -// #[ignore] -// fn dispatch_nfts_mint_from_contract_works() { -// new_test_ext().execute_with(|| { -// let _ = env_logger::try_init(); -// -// let (wasm_binary, _) = -// load_wasm_module::("../../pop-api/examples/nfts/target/ink/nfts.wasm") -// .unwrap(); -// -// let init_value = 100; -// -// let result = Contracts::bare_instantiate( -// ALICE, -// init_value, -// GAS_LIMIT, -// None, -// Code::Upload(wasm_binary), -// function_selector("new"), -// vec![], -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// ) -// .result -// .unwrap(); -// -// assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); -// -// let addr = result.account_id; -// -// let collection_id: u32 = 0; -// let item_id: u32 = 1; -// -// // create nft collection with contract as owner -// assert_eq!( -// Nfts::force_create( -// RuntimeOrigin::root(), -// addr.clone().into(), -// default_collection_config() -// ), -// Ok(()) -// ); -// -// assert_eq!(Nfts::collection_owner(collection_id), Some(addr.clone().into())); -// // assert that the item does not exist yet -// assert_eq!(Nfts::owner(collection_id, item_id), None); -// -// let function = function_selector("mint_through_runtime"); -// -// let params = -// [function, collection_id.encode(), item_id.encode(), BOB.encode()].concat(); -// -// let result = Contracts::bare_call( -// ALICE, -// addr.clone(), -// 0, -// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), -// None, -// params, -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// pallet_contracts::Determinism::Enforced, -// ); -// -// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { -// log::debug!( -// "Contract debug buffer - {:?}", -// String::from_utf8(result.debug_message.clone()) -// ); -// log::debug!("result: {:?}", result); -// } -// -// // check for revert -// assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); -// -// assert_eq!(Nfts::owner(collection_id, item_id), Some(BOB.into())); -// }); -// } -// -// #[test] -// #[ignore] -// fn nfts_mint_surfaces_error() { -// new_test_ext().execute_with(|| { -// let _ = env_logger::try_init(); -// -// let (wasm_binary, _) = -// load_wasm_module::("../../pop-api/examples/nfts/target/ink/nfts.wasm") -// .unwrap(); -// -// let init_value = 100; -// -// let result = Contracts::bare_instantiate( -// ALICE, -// init_value, -// GAS_LIMIT, -// None, -// Code::Upload(wasm_binary), -// function_selector("new"), -// vec![], -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// ) -// .result -// .unwrap(); -// -// assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); -// -// let addr = result.account_id; -// -// let collection_id: u32 = 0; -// let item_id: u32 = 1; -// -// let function = function_selector("mint_through_runtime"); -// -// let params = -// [function, collection_id.encode(), item_id.encode(), BOB.encode()].concat(); -// -// let result = Contracts::bare_call( -// ALICE, -// addr.clone(), -// 0, -// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), -// None, -// params, -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// pallet_contracts::Determinism::Enforced, -// ); -// -// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { -// log::debug!( -// "Contract debug buffer - {:?}", -// String::from_utf8(result.debug_message.clone()) -// ); -// log::debug!("result: {:?}", result); -// } -// -// // check for revert with expected error -// let result = result.result.unwrap(); -// assert!(result.did_revert()); -// }); -// } -// -// #[test] -// #[ignore] -// fn reading_last_relay_chain_block_number_works() { -// new_test_ext().execute_with(|| { -// let _ = env_logger::try_init(); -// -// let (wasm_binary, _) = load_wasm_module::( -// "../../pop-api/examples/read-runtime-state/target/ink/read_relay_blocknumber.wasm", -// ) -// .unwrap(); -// -// let init_value = 100; -// -// let contract = Contracts::bare_instantiate( -// ALICE, -// init_value, -// GAS_LIMIT, -// None, -// Code::Upload(wasm_binary), -// function_selector("new"), -// vec![], -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// ) -// .result -// .unwrap(); -// -// assert!(!contract.result.did_revert(), "deploying contract reverted {:?}", contract); -// -// let addr = contract.account_id; -// -// let function = function_selector("read_relay_block_number"); -// let params = [function].concat(); -// -// let result = Contracts::bare_call( -// ALICE, -// addr.clone(), -// 0, -// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), -// None, -// params, -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::UnsafeCollect, -// pallet_contracts::Determinism::Relaxed, -// ); -// -// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { -// log::debug!( -// "Contract debug buffer - {:?}", -// String::from_utf8(result.debug_message.clone()) -// ); -// log::debug!("result: {:?}", result); -// } -// -// // check for revert -// assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); -// }); -// } -// -// #[test] -// #[ignore] -// fn place_spot_order_from_contract_works() { -// new_test_ext().execute_with(|| { -// let _ = env_logger::try_init(); -// -// let (wasm_binary, _) = load_wasm_module::( -// "../../pop-api/examples/place-spot-order/target/ink/spot_order.wasm", -// ) -// .unwrap(); -// -// let init_value = 100 * UNIT; -// -// let result = Contracts::bare_instantiate( -// ALICE, -// init_value, -// GAS_LIMIT, -// None, -// Code::Upload(wasm_binary), -// function_selector("new"), -// vec![], -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// ) -// .result -// .unwrap(); -// -// assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); -// -// let addr = result.account_id; -// -// let function = function_selector("place_spot_order"); -// -// let max_amount = 1 * UNIT; -// let para_id = 2000; -// -// let params = [function, max_amount.encode(), para_id.encode()].concat(); -// -// let result = Contracts::bare_call( -// ALICE, -// addr.clone(), -// 0, -// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), -// None, -// params, -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// pallet_contracts::Determinism::Enforced, -// ); -// -// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { -// log::debug!( -// "Contract debug buffer - {:?}", -// String::from_utf8(result.debug_message.clone()) -// ); -// log::debug!("result: {:?}", result); -// } -// -// // check for revert -// assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); -// }); -// } -// -// #[test] -// #[ignore] -// fn allow_call_filter_blocks_call() { -// new_test_ext().execute_with(|| { -// let _ = env_logger::try_init(); -// -// let (wasm_binary, _) = load_wasm_module::( -// "../../tests/contracts/filtered-call/target/ink/pop_api_filtered_call.wasm", -// ) -// .unwrap(); -// -// let init_value = 100 * UNIT; -// -// let result = Contracts::bare_instantiate( -// ALICE, -// init_value, -// GAS_LIMIT, -// None, -// Code::Upload(wasm_binary), -// function_selector("new"), -// vec![], -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// ) -// .result -// .unwrap(); -// -// assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); -// -// let addr = result.account_id; -// -// let function = function_selector("get_filtered"); -// let params = [function].concat(); -// -// let result = Contracts::bare_call( -// ALICE, -// addr.clone(), -// 0, -// Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), -// None, -// params, -// DEBUG_OUTPUT, -// pallet_contracts::CollectEvents::Skip, -// pallet_contracts::Determinism::Enforced, -// ); -// -// if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { -// log::debug!( -// "Contract debug buffer - {:?}", -// String::from_utf8(result.debug_message.clone()) -// ); -// log::debug!("filtered result: {:?}", result); -// } -// -// // check for revert -// assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); -// }); -// } -// } diff --git a/runtime/devnet/src/extensions/v0/error.rs b/runtime/devnet/src/extensions/v0.rs similarity index 100% rename from runtime/devnet/src/extensions/v0/error.rs rename to runtime/devnet/src/extensions/v0.rs diff --git a/runtime/devnet/src/extensions/v0/assets.rs b/runtime/devnet/src/extensions/v0/assets.rs deleted file mode 100644 index c6b15b4e..00000000 --- a/runtime/devnet/src/extensions/v0/assets.rs +++ /dev/null @@ -1,76 +0,0 @@ -use crate::extensions::{ - AccountId as AccountId32, AssetId, - AssetsKeys::{self, *}, - Balance, Compact, Decode, DispatchError, MultiAddress, Runtime, TrustBackedAssetsInstance, -}; -use pop_primitives::AccountId; -use sp_std::vec::Vec; - -pub(crate) fn construct_assets_key( - call_index: u8, - params: Vec, -) -> Result { - match call_index { - 0 => { - let id = ::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(TotalSupply(id)) - }, - 1 => { - let (id, owner) = <(AssetId, AccountId)>::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(BalanceOf(id, owner)) - }, - 2 => { - let (id, owner, spender) = <(AssetId, AccountId, AccountId)>::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(Allowance(id, owner, spender)) - }, - 3 => { - let id = ::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(TokenName(id)) - }, - 4 => { - let id = ::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(TokenSymbol(id)) - }, - 5 => { - let id = ::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(TokenDecimals(id)) - }, - // other calls - _ => Err(DispatchError::Other("UnknownFunctionId")), - } -} - -pub(crate) fn construct_assets_call( - call_index: u8, - params: Vec, -) -> Result, DispatchError> { - match call_index { - 9 => { - let (id, target, amount) = <(AssetId, AccountId32, Balance)>::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(pallet_assets::Call::::transfer_keep_alive { - id: Compact(id), - target: MultiAddress::Id(target), - amount, - }) - }, - 22 => { - let (id, delegate, amount) = - <(AssetId, AccountId32, Balance)>::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(pallet_assets::Call::::approve_transfer { - id: Compact(id), - delegate: MultiAddress::Id(delegate), - amount, - }) - }, - // other calls - _ => Err(DispatchError::Other("UnknownFunctionId")), - } -} diff --git a/runtime/devnet/src/extensions/v0/mod.rs b/runtime/devnet/src/extensions/v0/mod.rs deleted file mode 100644 index 6406e08f..00000000 --- a/runtime/devnet/src/extensions/v0/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub(crate) mod assets; -pub(crate) mod error; diff --git a/runtime/devnet/src/lib.rs b/runtime/devnet/src/lib.rs index 9ab64043..23895310 100644 --- a/runtime/devnet/src/lib.rs +++ b/runtime/devnet/src/lib.rs @@ -73,6 +73,8 @@ use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}; // XCM Imports use xcm::latest::prelude::BodyId; +pub(crate) use pallet_api::fungibles; + /// Some way of identifying an account on the chain. We intentionally make it equivalent /// to the public key of our transaction signing scheme. pub type AccountId = <::Signer as IdentifyAccount>::AccountId; @@ -250,82 +252,6 @@ impl Contains for FilteredCalls { } } -/// A type to identify allowed calls to the Runtime from contracts. Used by Pop API -pub struct AllowedApiCalls; -impl Contains for crate::AllowedApiCalls { - fn contains(c: &RuntimeCall) -> bool { - use config::assets::AssetsCall; - use pallet_nfts::Call as NftsCall; - matches!( - c, - RuntimeCall::Balances(BalancesCall::transfer_keep_alive { .. }) - | RuntimeCall::Assets( - AssetsCall::create { .. } - | AssetsCall::start_destroy { .. } - | AssetsCall::destroy_accounts { .. } - | AssetsCall::destroy_approvals { .. } - | AssetsCall::finish_destroy { .. } - | AssetsCall::mint { .. } - | AssetsCall::burn { .. } - | AssetsCall::transfer { .. } - | AssetsCall::transfer_keep_alive { .. } - | AssetsCall::force_transfer { .. } - | AssetsCall::freeze { .. } - | AssetsCall::thaw { .. } - | AssetsCall::freeze_asset { .. } - | AssetsCall::thaw_asset { .. } - | AssetsCall::transfer_ownership { .. } - | AssetsCall::set_team { .. } - | AssetsCall::set_metadata { .. } - | AssetsCall::clear_metadata { .. } - | AssetsCall::approve_transfer { .. } - | AssetsCall::cancel_approval { .. } - | AssetsCall::force_cancel_approval { .. } - | AssetsCall::transfer_approved { .. } - | AssetsCall::touch { .. } - | AssetsCall::refund { .. } - | AssetsCall::set_min_balance { .. } - | AssetsCall::touch_other { .. } - | AssetsCall::refund_other { .. } - | AssetsCall::block { .. } - ) | RuntimeCall::Nfts( - NftsCall::create { .. } - | NftsCall::destroy { .. } - | NftsCall::mint { .. } - | NftsCall::burn { .. } - | NftsCall::transfer { .. } - | NftsCall::redeposit { .. } - | NftsCall::lock_item_transfer { .. } - | NftsCall::unlock_item_transfer { .. } - | NftsCall::lock_collection { .. } - | NftsCall::transfer_ownership { .. } - | NftsCall::set_team { .. } - | NftsCall::approve_transfer { .. } - | NftsCall::cancel_approval { .. } - | NftsCall::clear_all_transfer_approvals { .. } - | NftsCall::lock_item_properties { .. } - | NftsCall::set_attribute { .. } - | NftsCall::clear_attribute { .. } - | NftsCall::approve_item_attributes { .. } - | NftsCall::cancel_item_attributes_approval { .. } - | NftsCall::set_metadata { .. } - | NftsCall::clear_metadata { .. } - | NftsCall::set_collection_metadata { .. } - | NftsCall::clear_collection_metadata { .. } - | NftsCall::set_accept_ownership { .. } - | NftsCall::set_collection_max_supply { .. } - | NftsCall::update_mint_settings { .. } - | NftsCall::set_price { .. } - | NftsCall::buy_item { .. } - | NftsCall::pay_tips { .. } - | NftsCall::create_swap { .. } - | NftsCall::cancel_swap { .. } - | NftsCall::claim_swap { .. } - ) - ) - } -} - /// The default types are being injected by [`derive_impl`](`frame_support::derive_impl`) from /// [`ParaChainDefaultConfig`](`struct@frame_system::config_preludes::ParaChainDefaultConfig`), /// but overridden as needed. @@ -663,6 +589,9 @@ construct_runtime!( Nfts: pallet_nfts = 50, NftFractionalization: pallet_nft_fractionalization = 51, Assets: pallet_assets:: = 52, + + // Pop API + Fungibles: fungibles = 150, } ); @@ -670,6 +599,7 @@ construct_runtime!( mod benches { frame_benchmarking::define_benchmarks!( [frame_system, SystemBench::] + [pallet_api::fungibles, Fungibles] [pallet_balances, Balances] [pallet_session, SessionBench::] [pallet_timestamp, Timestamp] @@ -1030,3 +960,22 @@ cumulus_pallet_parachain_system::register_validate_block! { Runtime = Runtime, BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::, } + +#[cfg(test)] +mod tests { + use crate::Runtime; + use std::any::TypeId; + + // Ensures that the account id lookup does not perform any state reads. When this changes, + // `pallet_api::fungibles` dispatchables need to be re-evaluated. + #[test] + fn test_lookup_config() { + type ExpectedLookup = sp_runtime::traits::AccountIdLookup; + type ConfigLookup = ::Lookup; + + let expected_type_id = TypeId::of::(); + let config_type_id = TypeId::of::(); + + assert_eq!(config_type_id, expected_type_id); + } +} diff --git a/runtime/testnet/Cargo.toml b/runtime/testnet/Cargo.toml index b04c3102..51cab6d6 100644 --- a/runtime/testnet/Cargo.toml +++ b/runtime/testnet/Cargo.toml @@ -22,8 +22,8 @@ scale-info.workspace = true smallvec.workspace = true # Local -pop-primitives = { workspace = true, features = ["nfts", "cross-chain"] } -pop-runtime-common = { workspace = true, default-features = false } +pop-primitives.workspace = true +pop-runtime-common.workspace = true # Substrate frame-benchmarking.workspace = true @@ -90,7 +90,6 @@ parachain-info.workspace = true [dev-dependencies] env_logger = "0.11.2" hex = "0.4.3" -enumflags2 = "0.7.9" [features] default = ["std"] diff --git a/runtime/testnet/src/extensions.rs b/runtime/testnet/src/extensions.rs index bcb0f835..a6e309f9 100644 --- a/runtime/testnet/src/extensions.rs +++ b/runtime/testnet/src/extensions.rs @@ -1,22 +1,13 @@ -use cumulus_pallet_parachain_system::RelaychainDataProvider; +use frame_support::traits::{Contains, OriginTrait}; use frame_support::{ dispatch::{GetDispatchInfo, RawOrigin}, pallet_prelude::*, - traits::nonfungibles_v2::Inspect, - traits::{Contains, OriginTrait}, }; use pallet_contracts::chain_extension::{ BufInBufOutState, ChainExtension, ChargedAmount, Environment, Ext, InitState, RetVal, }; -use pop_primitives::{ - nfts::{CollectionId, ItemId}, - storage_keys::{NftsKeys, ParachainSystemKeys, RuntimeStateKeys}, -}; use sp_core::crypto::UncheckedFrom; -use sp_runtime::{ - traits::{BlockNumberProvider, Dispatchable}, - DispatchError, -}; +use sp_runtime::{traits::Dispatchable, DispatchError}; use sp_std::vec::Vec; use crate::{AccountId, AllowedApiCalls, RuntimeCall, RuntimeOrigin}; @@ -31,9 +22,6 @@ pub struct PopApiExtension; impl ChainExtension for PopApiExtension where T: pallet_contracts::Config - + pallet_xcm::Config - + pallet_nfts::Config - + cumulus_pallet_parachain_system::Config + frame_system::Config< RuntimeOrigin = RuntimeOrigin, AccountId = AccountId, @@ -44,7 +32,6 @@ where fn call(&mut self, env: Environment) -> Result where E: Ext, - T::AccountId: UncheckedFrom + AsRef<[u8]>, { log::debug!(target:LOG_TARGET, " extension called "); match v0::FuncId::try_from(env.func_id())? { @@ -101,8 +88,6 @@ fn dispatch_call( log_prefix: &str, ) -> Result<(), DispatchError> where - T: frame_system::Config, - RuntimeOrigin: From>, E: Ext, { let charged_dispatch_weight = env.charge_weight(call.get_dispatch_info().weight)?; @@ -157,8 +142,7 @@ where fn dispatch(env: Environment) -> Result<(), DispatchError> where - T: pallet_contracts::Config - + frame_system::Config, + T: pallet_contracts::Config, RuntimeOrigin: From>, E: Ext, { @@ -180,10 +164,7 @@ where fn read_state(env: Environment) -> Result<(), DispatchError> where - T: pallet_contracts::Config - + pallet_nfts::Config - + cumulus_pallet_parachain_system::Config - + frame_system::Config, + T: pallet_contracts::Config, E: Ext, { const LOG_PREFIX: &str = " read_state |"; @@ -199,16 +180,9 @@ where log::debug!(target:LOG_TARGET, "{} charged weight: {:?}", LOG_PREFIX, charged_weight); - let key: RuntimeStateKeys = env.read_as()?; - - let result = match key { - RuntimeStateKeys::Nfts(key) => read_nfts_state::(key, &mut env), - RuntimeStateKeys::ParachainSystem(key) => { - read_parachain_system_state::(key, &mut env) - }, - _ => Ok(Vec::default()), - }? - .encode(); + // TODO: always returning an empty vec. Chainextension will be refactored into one for both + // runtimes before pop api implementation gets merged into main. + let result = Vec::::default().encode(); log::trace!( target:LOG_TARGET, @@ -219,521 +193,3 @@ where DispatchError::Other("unable to write results to contract memory") }) } - -fn read_parachain_system_state( - key: ParachainSystemKeys, - env: &mut Environment, -) -> Result, DispatchError> -where - T: pallet_contracts::Config + cumulus_pallet_parachain_system::Config, - E: Ext, -{ - match key { - ParachainSystemKeys::LastRelayChainBlockNumber => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(RelaychainDataProvider::::current_block_number().encode()) - }, - } -} - -fn read_nfts_state( - key: NftsKeys, - env: &mut Environment, -) -> Result, DispatchError> -where - T: pallet_contracts::Config + pallet_nfts::Config, - E: Ext, -{ - match key { - NftsKeys::Collection(collection) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Collection::::get(collection).encode()) - }, - NftsKeys::CollectionOwner(collection) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::collection_owner(collection).encode()) - }, - NftsKeys::Item(collection, item) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Item::::get(collection, item).encode()) - }, - NftsKeys::Owner(collection, item) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::owner(collection, item).encode()) - }, - NftsKeys::Attribute(collection, item, key) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::attribute(&collection, &item, &key).encode()) - }, - // NftsKeys::CustomAttribute(account, collection, item, key) => { - // env.charge_weight(T::DbWeight::get().reads(1_u64))?; - // Ok(pallet_nfts::Pallet::::custom_attribute(&account, &collection, &item, &key) - // .encode()) - // }, - NftsKeys::SystemAttribute(collection, item, key) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::system_attribute(&collection, item.as_ref(), &key) - .encode()) - }, - NftsKeys::CollectionAttribute(collection, key) => { - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - Ok(pallet_nfts::Pallet::::collection_attribute(&collection, &key).encode()) - }, - } -} - -#[cfg(test)] -mod tests { - pub use super::*; - pub use crate::*; - use enumflags2::BitFlags; - pub use pallet_contracts::Code; - use pallet_nfts::{CollectionConfig, CollectionSetting, CollectionSettings, MintSettings}; - use parachains_common::CollectionId; - pub use sp_runtime::{traits::Hash, AccountId32}; - - const DEBUG_OUTPUT: pallet_contracts::DebugInfo = pallet_contracts::DebugInfo::UnsafeDebug; - - const ALICE: AccountId32 = AccountId32::new([1_u8; 32]); - const BOB: AccountId32 = AccountId32::new([2_u8; 32]); - const INITIAL_AMOUNT: u128 = 100_000 * UNIT; - const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024); - - fn new_test_ext() -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::::default() - .build_storage() - .expect("Frame system builds valid default genesis config"); - - pallet_balances::GenesisConfig:: { - balances: vec![(ALICE, INITIAL_AMOUNT), (BOB, INITIAL_AMOUNT)], - } - .assimilate_storage(&mut t) - .expect("Pallet balances storage can be assimilated"); - - let mut ext = sp_io::TestExternalities::new(t); - ext.execute_with(|| System::set_block_number(1)); - ext - } - - fn load_wasm_module(path: &str) -> std::io::Result<(Vec, ::Output)> - where - T: frame_system::Config, - { - let wasm_binary = std::fs::read(path)?; - let code_hash = T::Hashing::hash(&wasm_binary); - Ok((wasm_binary, code_hash)) - } - - fn function_selector(name: &str) -> Vec { - let hash = sp_io::hashing::blake2_256(name.as_bytes()); - [hash[0..4].to_vec()].concat() - } - - // NFT helper functions - fn collection_config_from_disabled_settings( - settings: BitFlags, - ) -> CollectionConfig { - CollectionConfig { - settings: CollectionSettings::from_disabled(settings), - max_supply: None, - mint_settings: MintSettings::default(), - } - } - - fn default_collection_config() -> CollectionConfig { - collection_config_from_disabled_settings(CollectionSetting::DepositRequired.into()) - } - - #[test] - #[ignore] - fn dispatch_balance_transfer_from_contract_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = load_wasm_module::( - "../../pop-api/examples/balance-transfer/target/ink/balance_transfer.wasm", - ) - .unwrap(); - - let init_value = 100 * UNIT; - - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - - let addr = result.account_id; - - let function = function_selector("transfer_through_runtime"); - let value_to_send: u128 = 10 * UNIT; - let params = [function, BOB.encode(), value_to_send.encode()].concat(); - - let bob_balance_before = Balances::free_balance(&BOB); - assert_eq!(bob_balance_before, INITIAL_AMOUNT); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // check for revert - assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); - - let bob_balance_after = Balances::free_balance(&BOB); - assert_eq!(bob_balance_before + value_to_send, bob_balance_after); - }); - } - - #[test] - #[ignore] - fn dispatch_nfts_mint_from_contract_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = - load_wasm_module::("../../pop-api/examples/nfts/target/ink/nfts.wasm") - .unwrap(); - - let init_value = 100; - - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - - let addr = result.account_id; - - let collection_id: u32 = 0; - let item_id: u32 = 1; - - // create nft collection with contract as owner - assert_eq!( - Nfts::force_create( - RuntimeOrigin::root(), - addr.clone().into(), - default_collection_config() - ), - Ok(()) - ); - - assert_eq!(Nfts::collection_owner(collection_id), Some(addr.clone().into())); - // assert that the item does not exist yet - assert_eq!(Nfts::owner(collection_id, item_id), None); - - let function = function_selector("mint_through_runtime"); - - let params = - [function, collection_id.encode(), item_id.encode(), BOB.encode()].concat(); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // check for revert - assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); - - assert_eq!(Nfts::owner(collection_id, item_id), Some(BOB.into())); - }); - } - - #[test] - #[ignore] - fn nfts_mint_surfaces_error() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = - load_wasm_module::("../../pop-api/examples/nfts/target/ink/nfts.wasm") - .unwrap(); - - let init_value = 100; - - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - - let addr = result.account_id; - - let collection_id: u32 = 0; - let item_id: u32 = 1; - - let function = function_selector("mint_through_runtime"); - - let params = - [function, collection_id.encode(), item_id.encode(), BOB.encode()].concat(); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // check for revert with expected error - let result = result.result.unwrap(); - assert!(result.did_revert()); - }); - } - - #[test] - #[ignore] - fn reading_last_relay_chain_block_number_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = load_wasm_module::( - "../../pop-api/examples/read-runtime-state/target/ink/read_relay_blocknumber.wasm", - ) - .unwrap(); - - let init_value = 100; - - let contract = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!contract.result.did_revert(), "deploying contract reverted {:?}", contract); - - let addr = contract.account_id; - - let function = function_selector("read_relay_block_number"); - let params = [function].concat(); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::UnsafeCollect, - pallet_contracts::Determinism::Relaxed, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // check for revert - assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); - }); - } - - #[test] - #[ignore] - fn place_spot_order_from_contract_fails() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = load_wasm_module::( - "../../pop-api/examples/place-spot-order/target/ink/spot_order.wasm", - ) - .unwrap(); - - let init_value = 100 * UNIT; - - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - - let addr = result.account_id; - - let function = function_selector("place_spot_order"); - - let max_amount = 1 * UNIT; - let para_id = 2000; - - let params = [function, max_amount.encode(), para_id.encode()].concat(); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("result: {:?}", result); - } - - // check for revert - assert!( - result.result.is_err(), - "Contract execution should have failed - unimplemented runtime call!" - ); - }); - } - - #[test] - #[ignore] - fn allow_call_filter_blocks_call() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - - let (wasm_binary, _) = load_wasm_module::( - "../../tests/contracts/filtered-call/target/ink/pop_api_filtered_call.wasm", - ) - .unwrap(); - - let init_value = 100 * UNIT; - - let result = Contracts::bare_instantiate( - ALICE, - init_value, - GAS_LIMIT, - None, - Code::Upload(wasm_binary), - function_selector("new"), - vec![], - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - ) - .result - .unwrap(); - - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - - let addr = result.account_id; - - let function = function_selector("get_filtered"); - let params = [function].concat(); - - let result = Contracts::bare_call( - ALICE, - addr.clone(), - 0, - Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), - None, - params, - DEBUG_OUTPUT, - pallet_contracts::CollectEvents::Skip, - pallet_contracts::Determinism::Enforced, - ); - - if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { - log::debug!( - "Contract debug buffer - {:?}", - String::from_utf8(result.debug_message.clone()) - ); - log::debug!("filtered result: {:?}", result); - } - - // check for revert - assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); - }); - } -} diff --git a/runtime/testnet/src/lib.rs b/runtime/testnet/src/lib.rs index c4178011..5573ef18 100644 --- a/runtime/testnet/src/lib.rs +++ b/runtime/testnet/src/lib.rs @@ -252,45 +252,8 @@ impl Contains for FilteredCalls { /// A type to identify allowed calls to the Runtime from contracts. Used by Pop API pub struct AllowedApiCalls; impl Contains for AllowedApiCalls { - fn contains(c: &RuntimeCall) -> bool { - use pallet_nfts::Call as NftsCall; - matches!( - c, - RuntimeCall::Balances(BalancesCall::transfer_keep_alive { .. }) - | RuntimeCall::Nfts( - NftsCall::create { .. } - | NftsCall::destroy { .. } - | NftsCall::mint { .. } | NftsCall::burn { .. } - | NftsCall::transfer { .. } - | NftsCall::redeposit { .. } - | NftsCall::lock_item_transfer { .. } - | NftsCall::unlock_item_transfer { .. } - | NftsCall::lock_collection { .. } - | NftsCall::transfer_ownership { .. } - | NftsCall::set_team { .. } - | NftsCall::approve_transfer { .. } - | NftsCall::cancel_approval { .. } - | NftsCall::clear_all_transfer_approvals { .. } - | NftsCall::lock_item_properties { .. } - | NftsCall::set_attribute { .. } - | NftsCall::clear_attribute { .. } - | NftsCall::approve_item_attributes { .. } - | NftsCall::cancel_item_attributes_approval { .. } - | NftsCall::set_metadata { .. } - | NftsCall::clear_metadata { .. } - | NftsCall::set_collection_metadata { .. } - | NftsCall::clear_collection_metadata { .. } - | NftsCall::set_accept_ownership { .. } - | NftsCall::set_collection_max_supply { .. } - | NftsCall::update_mint_settings { .. } - | NftsCall::set_price { .. } - | NftsCall::buy_item { .. } - | NftsCall::pay_tips { .. } - | NftsCall::create_swap { .. } - | NftsCall::cancel_swap { .. } - | NftsCall::claim_swap { .. } - ) - ) + fn contains(_c: &RuntimeCall) -> bool { + false } } diff --git a/scripts/pallet-weights-template.hbs b/scripts/pallet-weights-template.hbs new file mode 100644 index 00000000..9e1e5a46 --- /dev/null +++ b/scripts/pallet-weights-template.hbs @@ -0,0 +1,122 @@ +{{header}} +//! Autogenerated weights for `{{pallet}}` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION {{version}} +//! DATE: {{date}}, STEPS: `{{cmd.steps}}`, REPEAT: `{{cmd.repeat}}`, LOW RANGE: `{{cmd.lowest_range_values}}`, HIGH RANGE: `{{cmd.highest_range_values}}` +//! WORST CASE MAP SIZE: `{{cmd.worst_case_map_values}}` +//! HOSTNAME: `R0GUE`, CPU: `{{cpuname}}` +//! WASM-EXECUTION: `{{cmd.wasm_execution}}`, CHAIN: `{{cmd.chain}}`, DB CACHE: `{{cmd.db_cache}}` + +// Executed Command: +{{#each args as |arg|}} +// {{arg}} +{{/each}} + +#![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 `{{pallet}}`. +pub trait WeightInfo { + {{#each benchmarks as |benchmark|}} + fn {{benchmark.name~}} + ( + {{~#each benchmark.components as |c| ~}} + {{c.name}}: u32, {{/each~}} + ) -> Weight; + {{/each}} +} + +/// Weights for `{{pallet}}` using the Substrate node and recommended hardware. +pub struct SubstrateWeight(PhantomData); +{{#if (eq pallet "frame_system")}} +impl WeightInfo for SubstrateWeight { +{{else}} +impl WeightInfo for SubstrateWeight { +{{/if}} + {{#each benchmarks as |benchmark|}} + {{#each benchmark.comments as |comment|}} + /// {{comment}} + {{/each}} + {{#each benchmark.component_ranges as |range|}} + /// The range of component `{{range.name}}` is `[{{range.min}}, {{range.max}}]`. + {{/each}} + fn {{benchmark.name~}} + ( + {{~#each benchmark.components as |c| ~}} + {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} + ) -> Weight { + // Proof Size summary in bytes: + // Measured: `{{benchmark.base_recorded_proof_size}}{{#each benchmark.component_recorded_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}` + // Estimated: `{{benchmark.base_calculated_proof_size}}{{#each benchmark.component_calculated_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}` + // Minimum execution time: {{underscore benchmark.min_execution_time}}_000 picoseconds. + Weight::from_parts({{underscore benchmark.base_weight}}, {{benchmark.base_calculated_proof_size}}) + {{#each benchmark.component_weight as |cw|}} + // Standard Error: {{underscore cw.error}} + .saturating_add(Weight::from_parts({{underscore cw.slope}}, 0).saturating_mul({{cw.name}}.into())) + {{/each}} + {{#if (ne benchmark.base_reads "0")}} + .saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}}_u64)) + {{/if}} + {{#each benchmark.component_reads as |cr|}} + .saturating_add(T::DbWeight::get().reads(({{cr.slope}}_u64).saturating_mul({{cr.name}}.into()))) + {{/each}} + {{#if (ne benchmark.base_writes "0")}} + .saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}}_u64)) + {{/if}} + {{#each benchmark.component_writes as |cw|}} + .saturating_add(T::DbWeight::get().writes(({{cw.slope}}_u64).saturating_mul({{cw.name}}.into()))) + {{/each}} + {{#each benchmark.component_calculated_proof_size as |cp|}} + .saturating_add(Weight::from_parts(0, {{cp.slope}}).saturating_mul({{cp.name}}.into())) + {{/each}} + } + {{/each}} +} + +// For backwards compatibility and tests. +impl WeightInfo for () { + {{#each benchmarks as |benchmark|}} + {{#each benchmark.comments as |comment|}} + /// {{comment}} + {{/each}} + {{#each benchmark.component_ranges as |range|}} + /// The range of component `{{range.name}}` is `[{{range.min}}, {{range.max}}]`. + {{/each}} + fn {{benchmark.name~}} + ( + {{~#each benchmark.components as |c| ~}} + {{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} + ) -> Weight { + // Proof Size summary in bytes: + // Measured: `{{benchmark.base_recorded_proof_size}}{{#each benchmark.component_recorded_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}` + // Estimated: `{{benchmark.base_calculated_proof_size}}{{#each benchmark.component_calculated_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}` + // Minimum execution time: {{underscore benchmark.min_execution_time}}_000 picoseconds. + Weight::from_parts({{underscore benchmark.base_weight}}, {{benchmark.base_calculated_proof_size}}) + {{#each benchmark.component_weight as |cw|}} + // Standard Error: {{underscore cw.error}} + .saturating_add(Weight::from_parts({{underscore cw.slope}}, 0).saturating_mul({{cw.name}}.into())) + {{/each}} + {{#if (ne benchmark.base_reads "0")}} + .saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}}_u64)) + {{/if}} + {{#each benchmark.component_reads as |cr|}} + .saturating_add(RocksDbWeight::get().reads(({{cr.slope}}_u64).saturating_mul({{cr.name}}.into()))) + {{/each}} + {{#if (ne benchmark.base_writes "0")}} + .saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}}_u64)) + {{/if}} + {{#each benchmark.component_writes as |cw|}} + .saturating_add(RocksDbWeight::get().writes(({{cw.slope}}_u64).saturating_mul({{cw.name}}.into()))) + {{/each}} + {{#each benchmark.component_calculated_proof_size as |cp|}} + .saturating_add(Weight::from_parts(0, {{cp.slope}}).saturating_mul({{cp.name}}.into())) + {{/each}} + } + {{/each}} +} + From 4e4512ef091a9c6322eb17e697b2180c0e210f22 Mon Sep 17 00:00:00 2001 From: Tin Chung <56880684+chungquantin@users.noreply.github.com> Date: Fri, 26 Jul 2024 17:45:43 +0700 Subject: [PATCH 29/76] refactor: streamline error from the decoded method by returning `Result` (#130) --- .../integration-tests/src/local_fungibles.rs | 65 ++++++++++--------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/pop-api/integration-tests/src/local_fungibles.rs b/pop-api/integration-tests/src/local_fungibles.rs index c62f0713..6de57ca4 100644 --- a/pop-api/integration-tests/src/local_fungibles.rs +++ b/pop-api/integration-tests/src/local_fungibles.rs @@ -8,11 +8,9 @@ use pop_primitives::error::{ const ASSET_ID: AssetId = 1; const CONTRACT: &str = "contracts/fungibles/target/ink/fungibles.wasm"; -fn decoded(result: ExecReturnValue) -> T { - match ::decode(&mut &result.data[2..]) { - Ok(value) => value, - Err(_) => panic!("\nTest failed by trying to decode `{:?}` into `T`\n", result), - } +fn decoded(result: ExecReturnValue) -> Result { + ::decode(&mut &result.data[2..]) + .map_err(|_| format!("\nTest failed by trying to decode `{:?}` into `T`\n", result)) } // Call total_supply contract message. @@ -20,7 +18,7 @@ fn total_supply(addr: AccountId32, asset_id: AssetId) -> Balance { let function = function_selector("total_supply"); let params = [function, asset_id.encode()].concat(); let result = bare_call(addr, params, 0).expect("should work"); - decoded::(result) + decoded::(result).unwrap() } // Call balance_of contract message. @@ -28,7 +26,7 @@ fn balance_of(addr: AccountId32, asset_id: AssetId, owner: AccountId32) -> Balan let function = function_selector("balance_of"); let params = [function, asset_id.encode(), owner.encode()].concat(); let result = bare_call(addr, params, 0).expect("should work"); - decoded::(result) + decoded::(result).unwrap() } // Call allowance contract message. @@ -41,7 +39,7 @@ fn allowance( let function = function_selector("allowance"); let params = [function, asset_id.encode(), owner.encode(), spender.encode()].concat(); let result = bare_call(addr, params, 0).expect("should work"); - decoded::(result) + decoded::(result).unwrap() } // Call token_name contract message. @@ -49,7 +47,7 @@ fn token_name(addr: AccountId32, asset_id: AssetId) -> Vec { let function = function_selector("token_name"); let params = [function, asset_id.encode()].concat(); let result = bare_call(addr, params, 0).expect("should work"); - decoded::>(result) + decoded::>(result).unwrap() } // Call token_symbol contract message. @@ -57,7 +55,7 @@ fn token_symbol(addr: AccountId32, asset_id: AssetId) -> Vec { let function = function_selector("token_symbol"); let params = [function, asset_id.encode()].concat(); let result = bare_call(addr, params, 0).expect("should work"); - decoded::>(result) + decoded::>(result).unwrap() } // Call token_decimals contract message. @@ -65,7 +63,7 @@ fn token_decimals(addr: AccountId32, asset_id: AssetId) -> u8 { let function = function_selector("token_decimals"); let params = [function, asset_id.encode()].concat(); let result = bare_call(addr, params, 0).expect("should work"); - decoded::(result) + decoded::(result).unwrap() } fn transfer( @@ -327,7 +325,7 @@ fn transfer_works() { // Asset does not exist. assert_eq!( decoded::(transfer(addr.clone(), 1, BOB, amount,)), - Module { index: 52, error: 3 }, + Ok(Module { index: 52, error: 3 }), ); // Create asset with Alice as owner and mint `amount` to contract address. let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); @@ -335,18 +333,18 @@ fn transfer_works() { freeze_asset(ALICE, asset); assert_eq!( decoded::(transfer(addr.clone(), asset, BOB, amount,)), - Module { index: 52, error: 16 }, + Ok(Module { index: 52, error: 16 }), ); thaw_asset(ALICE, asset); // Not enough balance. assert_eq!( decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), - Module { index: 52, error: 0 }, + Ok(Module { index: 52, error: 0 }), ); // Not enough balance due to ED. assert_eq!( decoded::(transfer(addr.clone(), asset, BOB, amount)), - Module { index: 52, error: 0 }, + Ok(Module { index: 52, error: 0 }), ); // Successful transfer. let balance_before_transfer = Assets::balance(asset, &BOB); @@ -357,13 +355,13 @@ fn transfer_works() { // Transfer asset to account that does not exist. assert_eq!( decoded::(transfer(addr.clone(), asset, FERDIE, amount / 4)), - Token(CannotCreate) + Ok(Token(CannotCreate)) ); // Asset is not live, i.e. frozen or being destroyed. start_destroy_asset(ALICE, asset); assert_eq!( decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), - Module { index: 52, error: 16 }, + Ok(Module { index: 52, error: 16 }), ); }); } @@ -377,10 +375,13 @@ fn approve_works() { // Asset does not exist. assert_eq!( decoded::(approve(addr.clone(), 0, BOB, amount)), - Module { index: 52, error: 3 }, + Ok(Module { index: 52, error: 3 }), ); let asset = create_asset_and_mint_to(ALICE, 0, addr.clone(), amount); - assert_eq!(decoded::(approve(addr.clone(), asset, BOB, amount)), ConsumerRemaining); + assert_eq!( + decoded::(approve(addr.clone(), asset, BOB, amount)), + Ok(ConsumerRemaining) + ); let addr = instantiate(CONTRACT, INIT_VALUE, vec![1]); // Create asset with Alice as owner and mint `amount` to contract address. @@ -389,7 +390,7 @@ fn approve_works() { freeze_asset(ALICE, asset); assert_eq!( decoded::(approve(addr.clone(), asset, BOB, amount)), - Module { index: 52, error: 16 }, + Ok(Module { index: 52, error: 16 }), ); thaw_asset(ALICE, asset); // Successful approvals: @@ -403,7 +404,7 @@ fn approve_works() { start_destroy_asset(ALICE, asset); assert_eq!( decoded::(approve(addr.clone(), asset, BOB, amount)), - Module { index: 52, error: 16 }, + Ok(Module { index: 52, error: 16 }), ); }); } @@ -417,12 +418,12 @@ fn increase_allowance_works() { // Asset does not exist. assert_eq!( decoded::(increase_allowance(addr.clone(), 0, BOB, amount)), - Module { index: 52, error: 3 }, + Ok(Module { index: 52, error: 3 }), ); let asset = create_asset_and_mint_to(ALICE, 0, addr.clone(), amount); assert_eq!( decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), - ConsumerRemaining + Ok(ConsumerRemaining) ); let addr = instantiate(CONTRACT, INIT_VALUE, vec![1]); @@ -432,7 +433,7 @@ fn increase_allowance_works() { freeze_asset(ALICE, asset); assert_eq!( decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), - Module { index: 52, error: 16 }, + Ok(Module { index: 52, error: 16 }), ); thaw_asset(ALICE, asset); // Successful approvals: @@ -452,7 +453,7 @@ fn increase_allowance_works() { start_destroy_asset(ALICE, asset); assert_eq!( decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), - Module { index: 52, error: 16 }, + Ok(Module { index: 52, error: 16 }), ); }); } @@ -530,7 +531,7 @@ fn token_metadata_works() { // // Minting can only be done by the owner. // assert_eq!( // decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), -// Module { index: 52, error: 2 }, +// Ok(Module { index: 52, error: 2 }), // ); // // Minimum balance of an asset can not be zero. // assert_eq!( @@ -542,7 +543,7 @@ fn token_metadata_works() { // freeze_asset(addr.clone(), asset); // assert_eq!( // decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), -// Module { index: 52, error: 16 }, +// Ok(Module { index: 52, error: 16 }), // ); // thaw_asset(addr.clone(), asset); // // Successful mint. @@ -567,7 +568,7 @@ fn token_metadata_works() { // start_destroy_asset(addr.clone(), asset); // assert_eq!( // decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), -// Module { index: 52, error: 16 }, +// Ok(Module { index: 52, error: 16 }), // ); // }); // } @@ -582,27 +583,27 @@ fn token_metadata_works() { // // No balance to pay for fees. // assert_eq!( // decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), -// Module { index: 10, error: 2 }, +// Ok(Module { index: 10, error: 2 }), // ); // // Instantiate a contract without balance (relay token). // let addr = instantiate(CONTRACT, 100, vec![2]); // // No balance to pay the deposit. // assert_eq!( // decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), -// Module { index: 10, error: 2 }, +// Ok(Module { index: 10, error: 2 }), // ); // // Instantiate a contract with balance. // let addr = // instantiate(CONTRACT, INIT_VALUE, vec![1]); // assert_eq!( // decoded::(create(addr.clone(), ASSET_ID, BOB, 0)), -// Module { index: 52, error: 7 }, +// Ok(Module { index: 52, error: 7 }), // ); // create_asset(ALICE, ASSET_ID, 1); // // Asset ID is already taken. // assert_eq!( // decoded::(create(addr.clone(), ASSET_ID, BOB, 1)), -// Module { index: 52, error: 5 }, +// Ok(Module { index: 52, error: 5 }), // ); // // The minimal balance for an asset must be non zero. // let new_asset = 2; From 027cb85d9d6c97942500a75f528088f9c2402210 Mon Sep 17 00:00:00 2001 From: Tin Chung <56880684+chungquantin@users.noreply.github.com> Date: Sat, 27 Jul 2024 00:16:18 +0700 Subject: [PATCH 30/76] refactor: build chain extension method (#121) Co-authored-by: Daan van der Plas <93204684+Daanvdplas@users.noreply.github.com> Co-authored-by: Daanvdplas --- pop-api/src/lib.rs | 18 ++++++++++++++ pop-api/src/v0/assets/fungibles.rs | 38 +++++++++++++++++++++--------- pop-api/src/v0/assets/mod.rs | 1 + pop-api/src/v0/mod.rs | 26 +++++++++++++++++++- 4 files changed, 71 insertions(+), 12 deletions(-) diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index a984bb9e..9bf9d50b 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] +use ink::env::chain_extension::{ChainExtensionMethod, FromStatusCode}; + use constants::DECODING_FAILED; use ink::env::chain_extension::FromStatusCode; #[cfg(feature = "assets")] @@ -27,6 +29,22 @@ mod constants { pub(crate) const FUNGIBLES: u8 = 150; } +/// Helper method to build `ChainExtensionMethod`. +/// +/// Parameters: +/// - 'version': The version of the chain extension +/// - 'function': The ID of the function +/// - 'module': The index of the runtime module +/// - 'dispatchable': The index of the module dispatchable functions +fn build_extension_method( + version: u8, + function: u8, + module: u8, + dispatchable: u8, +) -> ChainExtensionMethod<(), (), (), false> { + ChainExtensionMethod::build(u32::from_le_bytes([version, function, module, dispatchable])) +} + /// Represents a status code returned by the runtime. /// /// `StatusCode` encapsulates a `u32` value that indicates the status of an operation performed diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index 42373cd7..68ce76b8 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -9,6 +9,22 @@ use crate::{ use constants::*; pub use metadata::*; +/// Helper method to build a dispatch call `ChainExtensionMethod` for fungibles `v0` +/// +/// Parameters: +/// - 'dispatchable': The index of the module dispatchable functions +fn build_dispatch(dispatchable: u8) -> ChainExtensionMethod<(), (), (), false> { + crate::v0::build_dispatch(FUNGIBLES, dispatchable) +} + +/// Helper method to build a dispatch call `ChainExtensionMethod` for fungibles `v0`` +/// +/// Parameters: +/// - 'state_query': The index of the runtime state query +fn build_read_state(state_query: u8) -> ChainExtensionMethod<(), (), (), false> { + crate::v0::build_read_state(FUNGIBLES, state_query) +} + /// Local Fungibles: /// 1. PSP-22 Interface /// 2. PSP-22 Metadata Interface @@ -60,7 +76,7 @@ mod constants { /// The total supply of the token, or an error if the operation fails. #[inline] pub fn total_supply(id: AssetId) -> Result { - ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, TOTAL_SUPPLY])) + build_read_state(TOTAL_SUPPLY) .input::() .output::>, true>() .handle_error_code::() @@ -79,7 +95,7 @@ pub fn total_supply(id: AssetId) -> Result { /// The balance of the specified account, or an error if the operation fails. #[inline] pub fn balance_of(id: AssetId, owner: AccountId) -> Result { - ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, BALANCE_OF])) + build_read_state(BALANCE_OF) .input::<(AssetId, AccountId)>() .output::>, true>() .handle_error_code::() @@ -99,7 +115,7 @@ pub fn balance_of(id: AssetId, owner: AccountId) -> Result { /// The remaining allowance, or an error if the operation fails. #[inline] pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { - ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, ALLOWANCE])) + build_read_state(ALLOWANCE) .input::<(AssetId, AccountId, AccountId)>() .output::>, true>() .handle_error_code::() @@ -119,7 +135,7 @@ pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result Result<()> { - ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, TRANSFER])) + build_dispatch(TRANSFER) .input::<(AssetId, AccountId, Balance)>() .output::, true>() .handle_error_code::() @@ -140,7 +156,7 @@ pub fn transfer(id: AssetId, target: AccountId, amount: Balance) -> Result<()> { /// Returns `Ok(())` if successful, or an error if the transfer fails. #[inline] pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, amount: Balance) -> Result<()> { - ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, TRANSFER_FROM])) + build_dispatch(TRANSFER_FROM) .input::<(AssetId, AccountId, AccountId, Balance)>() .output::, true>() .handle_error_code::() @@ -158,7 +174,7 @@ pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, amount: Balanc /// Returns `Ok(())` if successful, or an error if the approval fails. #[inline] pub fn approve(id: AssetId, spender: AccountId, amount: Balance) -> Result<()> { - ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, APPROVE])) + build_dispatch(APPROVE) .input::<(AssetId, AccountId, Balance)>() .output::, true>() .handle_error_code::() @@ -176,7 +192,7 @@ pub fn approve(id: AssetId, spender: AccountId, amount: Balance) -> Result<()> { /// Returns `Ok(())` if successful, or an error if the operation fails. #[inline] pub fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { - ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, INCREASE_ALLOWANCE])) + build_dispatch(INCREASE_ALLOWANCE) .input::<(AssetId, AccountId, Balance)>() .output::, true>() .handle_error_code::() @@ -194,7 +210,7 @@ pub fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Re /// Returns `Ok(())` if successful, or an error if the operation fails. #[inline] pub fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { - ChainExtensionMethod::build(u32::from_le_bytes([V0, DISPATCH, FUNGIBLES, DECREASE_ALLOWANCE])) + build_dispatch(DECREASE_ALLOWANCE) .input::<(AssetId, AccountId, Balance)>() .output::, true>() .handle_error_code::() @@ -212,7 +228,7 @@ pub mod metadata { /// The name of the token as a byte vector, or an error if the operation fails. #[inline] pub fn token_name(id: AssetId) -> Result> { - ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, TOKEN_NAME])) + build_read_state(TOKEN_NAME) .input::() .output::>, true>() .handle_error_code::() @@ -229,7 +245,7 @@ pub mod metadata { /// The symbol of the token as a byte vector, or an error if the operation fails. #[inline] pub fn token_symbol(id: AssetId) -> Result> { - ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, TOKEN_SYMBOL])) + build_read_state(TOKEN_SYMBOL) .input::() .output::>, true>() .handle_error_code::() @@ -246,7 +262,7 @@ pub mod metadata { /// The number of decimals of the token as a byte vector, or an error if the operation fails. #[inline] pub fn token_decimals(id: AssetId) -> Result { - ChainExtensionMethod::build(u32::from_le_bytes([V0, READ_STATE, FUNGIBLES, TOKEN_DECIMALS])) + build_read_state(TOKEN_DECIMALS) .input::() .output::>, true>() .handle_error_code::() diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index 197db710..c68c0181 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1,2 +1,3 @@ #[cfg(feature = "fungibles")] pub mod fungibles; + diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index 1c3642e1..0fd06c89 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -1,4 +1,10 @@ -use crate::{primitives::error::Error, StatusCode}; +use crate::{ + build_extension_method, + constants::{DISPATCH, READ_STATE}, + primitives::error::Error, + StatusCode, +}; +use ink::env::chain_extension::ChainExtensionMethod; #[cfg(feature = "assets")] pub mod assets; @@ -10,3 +16,21 @@ impl From for Error { value.0.into() } } + +/// Helper method to build a dispatch call `ChainExtensionMethod` +/// +/// Parameters: +/// - 'module': The index of the runtime module +/// - 'dispatchable': The index of the module dispatchable functions +fn build_dispatch(module: u8, dispatchable: u8) -> ChainExtensionMethod<(), (), (), false> { + build_extension_method(V0, DISPATCH, module, dispatchable) +} + +/// Helper method to build a dispatch call `ChainExtensionMethod` +/// +/// Parameters: +/// - 'module': The index of the runtime module +/// - 'state_query': The index of the runtime state query +fn build_read_state(module: u8, state_query: u8) -> ChainExtensionMethod<(), (), (), false> { + build_extension_method(V0, READ_STATE, module, state_query) +} From 30ff91ad13346b6931561a25917e89019dd3373c Mon Sep 17 00:00:00 2001 From: Tin Chung <56880684+chungquantin@users.noreply.github.com> Date: Sat, 27 Jul 2024 08:52:26 +0700 Subject: [PATCH 31/76] fix: clean up imports (#135) --- pop-api/src/lib.rs | 2 +- pop-api/src/v0/assets/fungibles.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 9bf9d50b..efa7ecb2 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -1,6 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -use ink::env::chain_extension::{ChainExtensionMethod, FromStatusCode}; +use ink::env::chain_extension::ChainExtensionMethod; use constants::DECODING_FAILED; use ink::env::chain_extension::FromStatusCode; diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index 68ce76b8..374b6e88 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -1,7 +1,7 @@ use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec, scale::Decode}; use crate::{ - constants::{ASSETS, BALANCES, DECODING_FAILED, DISPATCH, FUNGIBLES, READ_STATE}, + constants::{ASSETS, BALANCES, DECODING_FAILED, FUNGIBLES}, primitives::{AccountId, AssetId, Balance}, v0::V0, Result, StatusCode, From 9820e7fc0c8568cf1efad813b7464d09c13e676f Mon Sep 17 00:00:00 2001 From: Daan van der Plas <93204684+Daanvdplas@users.noreply.github.com> Date: Sun, 28 Jul 2024 12:07:55 +0200 Subject: [PATCH 32/76] refactor: remove read state encoding (#122) Co-authored-by: Frank Bell --- pallets/api/src/fungibles/mod.rs | 78 ++++++++----------------- pallets/api/src/fungibles/tests.rs | 20 ++++--- pop-api/src/lib.rs | 4 +- pop-api/src/v0/assets/fungibles.rs | 20 ++----- pop-api/src/v0/assets/mod.rs | 1 - runtime/devnet/src/config/api.rs | 35 ++++++++---- runtime/devnet/src/extensions/mod.rs | 85 ++++++++++++---------------- runtime/devnet/src/extensions/v0.rs | 4 +- 8 files changed, 106 insertions(+), 141 deletions(-) diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index cd34664a..32f9af67 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -180,61 +180,33 @@ pub mod pallet { } impl Pallet { - /// Returns the total token supply for a given asset ID. + /// Reads fungible asset state based on the provided value. /// - /// # Parameters - /// * `id` - The ID of the asset. - pub fn total_supply(id: AssetIdOf) -> BalanceOf { - AssetsOf::::total_supply(id) - } - - /// Returns the account balance for the specified `owner` for a given asset ID. Returns `0` if - /// the account is non-existent. + /// This function matches the value to determine the type of state query and returns the + /// encoded result. /// - /// # Parameters - /// * `id` - The ID of the asset. - /// * `owner` - The account whose balance is being queried. - pub fn balance_of(id: AssetIdOf, owner: &AccountIdOf) -> BalanceOf { - AssetsOf::::balance(id, owner) - } - - /// Returns the amount which `spender` is still allowed to withdraw from `owner` for a given - /// asset ID. Returns `0` if no allowance has been set. - /// - /// # Parameters - /// * `id` - The ID of the asset. - /// * `owner` - The account that owns the tokens. - /// * `spender` - The account that is allowed to spend the tokens. - pub fn allowance( - id: AssetIdOf, - owner: &AccountIdOf, - spender: &AccountIdOf, - ) -> BalanceOf { - AssetsOf::::allowance(id, owner, spender) - } - - /// Returns the token name for a given asset ID. - /// - /// # Parameters - /// * `id` - The ID of the asset. - pub fn token_name(id: AssetIdOf) -> Vec { - as MetadataInspect>>::name(id) - } - - /// Returns the token symbol for a given asset ID. - /// - /// # Parameters - /// * `id` - The ID of the asset. - pub fn token_symbol(id: AssetIdOf) -> Vec { - as MetadataInspect>>::symbol(id) - } - - /// Returns the token decimals for a given asset ID. - /// - /// # Parameters - /// * `id` - The ID of the asset. - pub fn token_decimals(id: AssetIdOf) -> u8 { - as MetadataInspect>>::decimals(id) + /// # Parameter + /// * `value` - An instance of `Read`, which specifies the type of state query and + /// the associated parameters. + pub fn read_state(value: Read) -> Vec { + use Read::*; + + match value { + TotalSupply(id) => AssetsOf::::total_supply(id).encode(), + BalanceOf { id, owner } => AssetsOf::::balance(id, owner).encode(), + Allowance { id, owner, spender } => { + AssetsOf::::allowance(id, &owner, &spender).encode() + }, + TokenName(id) => { + as MetadataInspect>>::name(id).encode() + }, + TokenSymbol(id) => { + as MetadataInspect>>::symbol(id).encode() + }, + TokenDecimals(id) => { + as MetadataInspect>>::decimals(id).encode() + }, + } } } } diff --git a/pallets/api/src/fungibles/tests.rs b/pallets/api/src/fungibles/tests.rs index dbfa0b34..c8de1020 100644 --- a/pallets/api/src/fungibles/tests.rs +++ b/pallets/api/src/fungibles/tests.rs @@ -1,4 +1,5 @@ -use crate::mock::*; +use crate::{fungibles::Read::*, mock::*}; +use codec::Encode; use frame_support::{ assert_ok, traits::fungibles::{approvals::Inspect, metadata::Inspect as MetadataInspect}, @@ -60,7 +61,7 @@ fn increase_allowance_works() { fn total_supply_works() { new_test_ext().execute_with(|| { create_asset_and_mint_to(ALICE, ASSET, ALICE, 100); - assert_eq!(Assets::total_supply(ASSET), Fungibles::total_supply(ASSET)); + assert_eq!(Assets::total_supply(ASSET).encode(), Fungibles::read_state(TotalSupply(ASSET))); }); } @@ -68,7 +69,10 @@ fn total_supply_works() { fn balance_of_works() { new_test_ext().execute_with(|| { create_asset_and_mint_to(ALICE, ASSET, ALICE, 100); - assert_eq!(Assets::balance(ASSET, ALICE), Fungibles::balance_of(ASSET, &ALICE)); + assert_eq!( + Assets::balance(ASSET, ALICE).encode(), + Fungibles::read_state(BalanceOf { id: ASSET, owner: ALICE }) + ); }); } @@ -77,8 +81,8 @@ fn allowance_works() { new_test_ext().execute_with(|| { create_asset_mint_and_approve(ALICE, ASSET, BOB, 100, ALICE, 50); assert_eq!( - Assets::allowance(ASSET, &ALICE, &BOB), - Fungibles::allowance(ASSET, &ALICE, &BOB) + Assets::allowance(ASSET, &ALICE, &BOB).encode(), + Fungibles::read_state(Allowance { id: ASSET, owner: ALICE, spender: BOB }) ); }); } @@ -90,9 +94,9 @@ fn token_metadata_works() { let symbol: Vec = vec![21, 22, 23]; let decimals: u8 = 69; create_asset_and_set_metadata(ALICE, ASSET, name.clone(), symbol.clone(), decimals); - assert_eq!(Assets::name(ASSET), Fungibles::token_name(ASSET)); - assert_eq!(Assets::symbol(ASSET), Fungibles::token_symbol(ASSET)); - assert_eq!(Assets::decimals(ASSET), Fungibles::token_decimals(ASSET)); + assert_eq!(Assets::name(ASSET).encode(), Fungibles::read_state(TokenName(ASSET))); + assert_eq!(Assets::symbol(ASSET).encode(), Fungibles::read_state(TokenSymbol(ASSET))); + assert_eq!(Assets::decimals(ASSET).encode(), Fungibles::read_state(TokenDecimals(ASSET))); }); } diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index efa7ecb2..7fcfc85b 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -1,9 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -use ink::env::chain_extension::ChainExtensionMethod; - use constants::DECODING_FAILED; -use ink::env::chain_extension::FromStatusCode; +use ink::env::chain_extension::{ChainExtensionMethod, FromStatusCode}; #[cfg(feature = "assets")] pub use v0::assets; diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index 374b6e88..0712a80b 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -1,12 +1,10 @@ -use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec, scale::Decode}; - use crate::{ - constants::{ASSETS, BALANCES, DECODING_FAILED, FUNGIBLES}, + constants::{ASSETS, BALANCES, FUNGIBLES}, primitives::{AccountId, AssetId, Balance}, - v0::V0, Result, StatusCode, }; use constants::*; +use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec}; pub use metadata::*; /// Helper method to build a dispatch call `ChainExtensionMethod` for fungibles `v0` @@ -78,10 +76,9 @@ mod constants { pub fn total_supply(id: AssetId) -> Result { build_read_state(TOTAL_SUPPLY) .input::() - .output::>, true>() + .output::, true>() .handle_error_code::() .call(&(id)) - .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } /// Returns the account balance for the specified `owner` for a given asset ID. Returns `0` if @@ -97,10 +94,9 @@ pub fn total_supply(id: AssetId) -> Result { pub fn balance_of(id: AssetId, owner: AccountId) -> Result { build_read_state(BALANCE_OF) .input::<(AssetId, AccountId)>() - .output::>, true>() + .output::, true>() .handle_error_code::() .call(&(id, owner)) - .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } /// Returns the amount which `spender` is still allowed to withdraw from `owner` for a given @@ -117,10 +113,9 @@ pub fn balance_of(id: AssetId, owner: AccountId) -> Result { pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { build_read_state(ALLOWANCE) .input::<(AssetId, AccountId, AccountId)>() - .output::>, true>() + .output::, true>() .handle_error_code::() .call(&(id, owner, spender)) - .and_then(|v| Balance::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } /// Transfers `value` amount of tokens from the caller's account to account `to`, with additional @@ -233,7 +228,6 @@ pub mod metadata { .output::>, true>() .handle_error_code::() .call(&(id)) - .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } /// Returns the token symbol for a given asset ID. @@ -250,7 +244,6 @@ pub mod metadata { .output::>, true>() .handle_error_code::() .call(&(id)) - .and_then(|v| >::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } /// Returns the token decimals for a given asset ID. @@ -264,10 +257,9 @@ pub mod metadata { pub fn token_decimals(id: AssetId) -> Result { build_read_state(TOKEN_DECIMALS) .input::() - .output::>, true>() + .output::, true>() .handle_error_code::() .call(&(id)) - .and_then(|v| ::decode(&mut &v[..]).map_err(|_e| StatusCode(DECODING_FAILED))) } } diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index c68c0181..197db710 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1,3 +1,2 @@ #[cfg(feature = "fungibles")] pub mod fungibles; - diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index ae179e4a..884128f2 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -11,24 +11,37 @@ pub enum RuntimeRead { Fungibles(fungibles::Read), } -impl fungibles::Config for Runtime { - type AssetsInstance = TrustBackedAssetsInstance; - type WeightInfo = fungibles::weights::SubstrateWeight; -} - -/// A type to identify allowed calls to the Runtime from contracts. Used by Pop API +/// A type to identify allowed calls to the Runtime from the API. pub struct AllowedApiCalls; impl Contains for AllowedApiCalls { + /// Allowed runtime calls from the API. fn contains(c: &RuntimeCall) -> bool { - use fungibles::Call as FungiblesCall; + use fungibles::Call::*; + matches!( + c, + RuntimeCall::Fungibles(transfer { .. } | approve { .. } | increase_allowance { .. }) + ) + } +} + +impl Contains> for AllowedApiCalls { + /// Allowed state queries from the API. + fn contains(c: &RuntimeRead) -> bool { + use fungibles::Read::*; matches!( c, - RuntimeCall::Fungibles( - FungiblesCall::transfer { .. } - | FungiblesCall::approve { .. } - | FungiblesCall::increase_allowance { .. } + RuntimeRead::Fungibles( + TotalSupply(..) + | BalanceOf { .. } | Allowance { .. } + | TokenName(..) | TokenSymbol(..) + | TokenDecimals(..) ) ) } } + +impl fungibles::Config for Runtime { + type AssetsInstance = TrustBackedAssetsInstance; + type WeightInfo = fungibles::weights::SubstrateWeight; +} diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs index 3aed89df..d3bbdd0b 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions/mod.rs @@ -5,10 +5,7 @@ use crate::{ api::{AllowedApiCalls, RuntimeRead}, assets::TrustBackedAssetsInstance, }, - fungibles::{ - self, - Read::{self, *}, - }, + fungibles::{self}, AccountId, RuntimeCall, RuntimeOrigin, }; use codec::{Decode, Encode}; @@ -26,6 +23,14 @@ use sp_runtime::{traits::Dispatchable, DispatchError}; use sp_std::vec::Vec; const LOG_TARGET: &str = "pop-api::extension"; +const DECODING_FAILED_ERROR: DispatchError = DispatchError::Other("DecodingFailed"); +// TODO: issue #93, we can also encode the `pop_primitives::Error::UnknownCall` which means we do use +// `Error` in the runtime and it should stay in primitives. Perhaps issue #91 will also influence +// here. Should be looked at together. +const DECODING_FAILED_ERROR_ENCODED: [u8; 4] = [255u8, 0, 0, 0]; +const UNKNOWN_CALL_ERROR: DispatchError = DispatchError::Other("UnknownCall"); +// TODO: see above. +const UNKNOWN_CALL_ERROR_ENCODED: [u8; 4] = [254u8, 0, 0, 0]; type ContractSchedule = ::Schedule; @@ -113,8 +118,7 @@ where params.insert(0, version); params.insert(1, pallet_index); params.insert(2, call_index); - let call = ::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; + let call = ::decode(&mut ¶ms[..]).map_err(|_| DECODING_FAILED_ERROR)?; // Contract is the origin by default. let origin: RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); @@ -169,19 +173,24 @@ where { const LOG_PREFIX: &str = " read_state |"; - // Prefix params with version, pallet, index to simplify decoding. + // Prefix params with version, pallet, index to simplify decoding, and decode parameters for + // reading state. params.insert(0, version); params.insert(1, pallet_index); params.insert(2, call_index); - let key = >::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; + let read = + >::decode(&mut ¶ms[..]).map_err(|_| DECODING_FAILED_ERROR)?; - let result = match key { - VersionedStateRead::V0(key) => match key { - RuntimeRead::Fungibles(key) => read_fungibles_state::(key, env), + // Charge weight for doing one storage read. + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + let result = match read { + VersionedStateRead::V0(read) => { + ensure!(AllowedApiCalls::contains(&read), UNKNOWN_CALL_ERROR); + match read { + RuntimeRead::Fungibles(key) => fungibles::Pallet::::read_state(key), + } }, - }? - .encode(); + }; log::trace!( target:LOG_TARGET, "{} result: {:?}.", LOG_PREFIX, result @@ -218,16 +227,18 @@ enum VersionedDispatch { // - `error`: The `DispatchError` encountered during contract execution. // - `version`: The version of the chain extension, used to determine the known errors. pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { - // "UnknownFunctionId" and "DecodingFailed" are mapped to specific errors in the API and will - // never change. - let mut encoded_error = match error { - DispatchError::Other("UnknownFunctionId") => Vec::from([254u8, 0, 0, 0]), - DispatchError::Other("DecodingFailed") => Vec::from([255u8, 0, 0, 0]), - _ => error.encode(), + let mut encoded_error: [u8; 4] = match error { + // "UnknownCall" and "DecodingFailed" are mapped to specific errors in the API and will + // never change. + UNKNOWN_CALL_ERROR => UNKNOWN_CALL_ERROR_ENCODED, + DECODING_FAILED_ERROR => DECODING_FAILED_ERROR_ENCODED, + _ => { + let mut encoded_error = error.encode(); + // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). + encoded_error.resize(4, 0); + encoded_error.try_into().expect("qed, resized to 4 bytes line above") + }, }; - // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). - encoded_error.resize(4, 0); - let mut encoded_error = encoded_error.try_into().expect("qed, resized to 4 bytes line above"); match version { // If an unknown variant of the `DispatchError` is detected the error needs to be converted // into the encoded value of `Error::Other`. This conversion is performed by shifting the bytes one @@ -246,7 +257,7 @@ pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { // - Byte 3: // - Unused or represents further nested information. 0 => v0::handle_unknown_error(&mut encoded_error), - _ => encoded_error = [254, 0, 0, 0], + _ => encoded_error = UNKNOWN_CALL_ERROR_ENCODED, } u32::from_le_bytes(encoded_error) } @@ -278,37 +289,13 @@ impl TryFrom for FuncId { 0 => Self::Dispatch, 1 => Self::ReadState, _ => { - return Err(DispatchError::Other("UnknownFuncId")); + return Err(UNKNOWN_CALL_ERROR); }, }; Ok(id) } } -fn read_fungibles_state( - key: Read, - env: &mut Environment, -) -> Result, DispatchError> -where - T: pallet_contracts::Config - + pallet_assets::Config - + fungibles::Config, - E: Ext, - T: frame_system::Config, -{ - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - match key { - TotalSupply(id) => Ok(fungibles::Pallet::::total_supply(id).encode()), - BalanceOf { id, owner } => Ok(fungibles::Pallet::::balance_of(id, &owner).encode()), - Allowance { id, owner, spender } => { - Ok(fungibles::Pallet::::allowance(id, &owner, &spender).encode()) - }, - TokenName(id) => Ok(fungibles::Pallet::::token_name(id).encode()), - TokenSymbol(id) => Ok(fungibles::Pallet::::token_symbol(id).encode()), - TokenDecimals(id) => Ok(fungibles::Pallet::::token_decimals(id).encode()), - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/runtime/devnet/src/extensions/v0.rs b/runtime/devnet/src/extensions/v0.rs index b26668f7..72760323 100644 --- a/runtime/devnet/src/extensions/v0.rs +++ b/runtime/devnet/src/extensions/v0.rs @@ -76,7 +76,7 @@ mod tests { DispatchError::Other(""), (Other { dispatch_error_index: 0, error_index: 0, error: 0 }), ), - (DispatchError::Other("UnknownFunctionId"), UnknownCall), + (DispatchError::Other("UnknownCall"), UnknownCall), (DispatchError::Other("DecodingFailed"), DecodingFailed), (DispatchError::CannotLookup, CannotLookup), (DispatchError::BadOrigin, BadOrigin), @@ -120,7 +120,7 @@ mod tests { DispatchError::Other("Random"), (Other { dispatch_error_index: 0, error_index: 0, error: 0 }), ), - (DispatchError::Other("UnknownFunctionId"), UnknownCall), + (DispatchError::Other("UnknownCall"), UnknownCall), (DispatchError::Other("DecodingFailed"), DecodingFailed), ]; for (dispatch_error, expected) in test_cases { From 5ac5a9a520dea12f8d8c14e1bd7cfeb57a3ec7bd Mon Sep 17 00:00:00 2001 From: Tin Chung <56880684+chungquantin@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:12:25 +0700 Subject: [PATCH 33/76] feat: transfer_from and decrease_allowance (#134) Co-authored-by: Daan van der Plas <93204684+Daanvdplas@users.noreply.github.com> --- pallets/api/src/fungibles/mod.rs | 80 ++++++++++-- pallets/api/src/fungibles/tests.rs | 37 ++++++ pallets/api/src/mock.rs | 3 +- .../integration-tests/src/local_fungibles.rs | 115 +++++++++++++++++- runtime/devnet/src/config/api.rs | 7 +- 5 files changed, 228 insertions(+), 14 deletions(-) diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index 32f9af67..9fb444f8 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -110,6 +110,28 @@ pub mod pallet { AssetsOf::::transfer_keep_alive(origin, id.into(), target, amount) } + /// Transfers `value` amount of tokens from the delegated account approved by the `owner` to + /// account `to`, with additional `data` in unspecified format. + /// + /// # Parameters + /// * `id` - The ID of the asset. + /// * `owner` - The account from which the asset balance will be withdrawn. + /// * `to` - The recipient account. + /// * `value` - The number of tokens to transfer. + #[pallet::call_index(4)] + #[pallet::weight(AssetsWeightInfoOf::::transfer_approved())] + pub fn transfer_from( + origin: OriginFor, + id: AssetIdOf, + owner: AccountIdOf, + target: AccountIdOf, + amount: BalanceOf, + ) -> DispatchResult { + let owner = T::Lookup::unlookup(owner); + let target = T::Lookup::unlookup(target); + AssetsOf::::transfer_approved(origin, id.into(), owner, target, amount) + } + /// Approves an account to spend a specified number of tokens on behalf of the caller. /// /// # Parameters @@ -124,17 +146,15 @@ pub mod pallet { spender: AccountIdOf, value: BalanceOf, ) -> DispatchResultWithPostInfo { - let weight = |approve: u32, cancel: u32| -> Weight { - ::WeightInfo::approve(cancel, approve) - }; - let who = ensure_signed(origin.clone()).map_err(|e| e.with_weight(weight(0, 0)))?; + let who = ensure_signed(origin.clone()) + .map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?; let current_allowance = AssetsOf::::allowance(id.clone(), &who, &spender); let spender = T::Lookup::unlookup(spender); let id: AssetIdParameterOf = id.into(); // If the new value is equal to the current allowance, do nothing. let return_weight = if value == current_allowance { - weight(0, 0) + Self::weight_approve(0, 0) } // If the new value is greater than the current allowance, approve the difference // because `approve_transfer` works additively (see `pallet-assets`). @@ -145,17 +165,17 @@ pub mod pallet { spender, value.saturating_sub(current_allowance), ) - .map_err(|e| e.with_weight(weight(1, 0)))?; - weight(1, 0) + .map_err(|e| e.with_weight(Self::weight_approve(1, 0)))?; + Self::weight_approve(1, 0) } else { // If the new value is less than the current allowance, cancel the approval and set the new value AssetsOf::::cancel_approval(origin.clone(), id.clone(), spender.clone()) - .map_err(|e| e.with_weight(weight(0, 1)))?; + .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; if value.is_zero() { - return Ok(Some(weight(0, 1)).into()); + return Ok(Some(Self::weight_approve(0, 1)).into()); } AssetsOf::::approve_transfer(origin, id, spender, value)?; - weight(1, 1) + Self::weight_approve(1, 1) }; Ok(Some(return_weight).into()) } @@ -177,6 +197,42 @@ pub mod pallet { let spender = T::Lookup::unlookup(spender); AssetsOf::::approve_transfer(origin, id.into(), spender, value) } + + /// Decreases the allowance of a spender. + /// + /// # Parameters + /// * `id` - The ID of the asset. + /// * `spender` - The account that is allowed to spend the tokens. + /// * `value` - The number of tokens to decrease the allowance by. + #[pallet::call_index(7)] + #[pallet::weight(::WeightInfo::approve(1, 1))] + pub fn decrease_allowance( + origin: OriginFor, + id: AssetIdOf, + spender: AccountIdOf, + value: BalanceOf, + ) -> DispatchResultWithPostInfo { + let who = ensure_signed(origin.clone()) + .map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?; + let mut current_allowance = AssetsOf::::allowance(id.clone(), &who, &spender); + let spender = T::Lookup::unlookup(spender); + let id: AssetIdParameterOf = id.into(); + + if value.is_zero() { + return Ok(Some(Self::weight_approve(0, 0)).into()); + } + + current_allowance.saturating_reduce(value); + // Cancel the aproval and set the new value if `current_allowance` is more than zero. + AssetsOf::::cancel_approval(origin.clone(), id.clone(), spender.clone()) + .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; + + if current_allowance.is_zero() { + return Ok(Some(Self::weight_approve(0, 1)).into()); + } + AssetsOf::::approve_transfer(origin, id, spender, current_allowance)?; + Ok(().into()) + } } impl Pallet { @@ -208,5 +264,9 @@ pub mod pallet { }, } } + + pub fn weight_approve(approve: u32, cancel: u32) -> Weight { + ::WeightInfo::approve(cancel, approve) + } } } diff --git a/pallets/api/src/fungibles/tests.rs b/pallets/api/src/fungibles/tests.rs index c8de1020..4377fc83 100644 --- a/pallets/api/src/fungibles/tests.rs +++ b/pallets/api/src/fungibles/tests.rs @@ -19,6 +19,43 @@ fn transfer_works() { }); } +#[test] +fn transfer_from_works() { + new_test_ext().execute_with(|| { + let amount: Balance = 100 * UNIT; + // Approve CHARLIE to transfer up to `amount` to BOB. + create_asset_mint_and_approve(ALICE, ASSET, ALICE, amount * 2, CHARLIE, amount / 2); + let transferred = amount / 2; + // Successfully call transfer from. + let alice_balance_before_transfer = Assets::balance(ASSET, &ALICE); + let balance_before_transfer = Assets::balance(ASSET, &BOB); + assert_ok!(Fungibles::transfer_from(signed(CHARLIE), ASSET, ALICE, BOB, transferred)); + let alice_balance_after_transfer = Assets::balance(ASSET, &ALICE); + let balance_after_transfer = Assets::balance(ASSET, &BOB); + // Check that BOB receives the `amount` and ALICE `amount` is spent successfully by CHARLIE. + assert_eq!(balance_after_transfer, balance_before_transfer + transferred); + assert_eq!(alice_balance_after_transfer, alice_balance_before_transfer - transferred); + }); +} + +#[test] +fn decrease_allowance_works() { + new_test_ext().execute_with(|| { + let amount: Balance = 100 * UNIT; + create_asset_mint_and_approve(ALICE, ASSET, ALICE, amount, BOB, amount); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount); + // Owner balance is not changed if decreased by zero. + assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, 0)); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount); + // Decrease allowance successfully. + assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, amount / 2 - 1 * UNIT)); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount / 2 + 1 * UNIT); + // Saturating if current allowance is decreased more than the owner balance. + assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, amount)); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), 0); + }); +} + // Non-additive, sets new value. #[test] fn approve_works() { diff --git a/pallets/api/src/mock.rs b/pallets/api/src/mock.rs index f5d155ef..b20e2635 100644 --- a/pallets/api/src/mock.rs +++ b/pallets/api/src/mock.rs @@ -103,6 +103,7 @@ impl crate::fungibles::Config for Test { pub(crate) const ALICE: AccountId = 1; pub(crate) const BOB: AccountId = 2; +pub(crate) const CHARLIE: AccountId = 3; pub(crate) const INIT_AMOUNT: Balance = 100_000_000 * UNIT; pub(crate) const UNIT: Balance = 10_000_000_000; @@ -112,7 +113,7 @@ pub(crate) fn new_test_ext() -> sp_io::TestExternalities { .expect("Frame system builds valid default genesis config"); pallet_balances::GenesisConfig:: { - balances: vec![(ALICE, INIT_AMOUNT), (BOB, INIT_AMOUNT)], + balances: vec![(ALICE, INIT_AMOUNT), (BOB, INIT_AMOUNT), (CHARLIE, INIT_AMOUNT)], } .assimilate_storage(&mut t) .expect("Pallet balances storage can be assimilated"); diff --git a/pop-api/integration-tests/src/local_fungibles.rs b/pop-api/integration-tests/src/local_fungibles.rs index 6de57ca4..15a644ad 100644 --- a/pop-api/integration-tests/src/local_fungibles.rs +++ b/pop-api/integration-tests/src/local_fungibles.rs @@ -78,6 +78,22 @@ fn transfer( result } +fn transfer_from( + addr: AccountId32, + asset_id: AssetId, + from: AccountId32, + to: AccountId32, + value: Balance, +) -> ExecReturnValue { + let function = function_selector("transfer_from"); + let data: Vec = vec![]; + let params = + [function, asset_id.encode(), from.encode(), to.encode(), value.encode(), data.encode()] + .concat(); + let result = bare_call(addr, params, 0).expect("should work"); + result +} + fn approve( addr: AccountId32, asset_id: AssetId, @@ -102,6 +118,18 @@ fn increase_allowance( result } +fn decrease_allowance( + addr: AccountId32, + asset_id: AssetId, + spender: AccountId32, + value: Balance, +) -> ExecReturnValue { + let function = function_selector("decrease_allowance"); + let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + result +} + // fn asset_exists(addr: AccountId32, asset_id: AssetId) -> bool { // let function = function_selector("asset_exists"); // let params = [function, asset_id.encode()].concat(); @@ -171,14 +199,15 @@ fn create_asset_mint_and_approve( mint: Balance, spender: AccountId32, approve: Balance, -) { - create_asset_and_mint_to(owner.clone(), asset_id, to.clone(), mint); +) -> AssetId { + let asset = create_asset_and_mint_to(owner.clone(), asset_id, to.clone(), mint); assert_ok!(Assets::approve_transfer( RuntimeOrigin::signed(to.into()), asset_id.into(), spender.into(), approve, )); + asset } // Freeze an asset. @@ -366,6 +395,52 @@ fn transfer_works() { }); } +#[test] +fn transfer_from_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + let amount: Balance = 100 * UNIT; + + // Asset does not exist. + assert_eq!( + decoded::(transfer_from(addr.clone(), 1, ALICE, BOB, amount / 2)), + Ok(Module { index: 52, error: 3 }), + ); + // Create asset with Alice as owner and mint `amount` to contract address. + let asset = create_asset_and_mint_to(ALICE, 1, ALICE, amount); + // Unapproved transfer. + assert_eq!( + decoded::(transfer_from(addr.clone(), asset, ALICE, BOB, amount / 2)), + Ok(Module { index: 52, error: 10 }) + ); + assert_ok!(Assets::approve_transfer( + RuntimeOrigin::signed(ALICE.into()), + asset.into(), + addr.clone().into(), + amount + 1 * UNIT, + )); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(ALICE, asset); + assert_eq!( + decoded::(transfer_from(addr.clone(), asset, ALICE, BOB, amount)), + Ok(Module { index: 52, error: 16 }), + ); + thaw_asset(ALICE, asset); + // Not enough balance. + assert_eq!( + decoded::(transfer_from(addr.clone(), asset, ALICE, BOB, amount + 1 * UNIT,)), + Ok(Module { index: 52, error: 0 }), + ); + // Successful transfer. + let balance_before_transfer = Assets::balance(asset, &BOB); + let result = transfer_from(addr.clone(), asset, ALICE, BOB, amount / 2); + assert!(!result.did_revert(), "Contract reverted!"); + let balance_after_transfer = Assets::balance(asset, &BOB); + assert_eq!(balance_after_transfer, balance_before_transfer + amount / 2); + }); +} + #[test] fn approve_works() { new_test_ext().execute_with(|| { @@ -458,6 +533,42 @@ fn increase_allowance_works() { }); } +#[test] +fn decrease_allowance_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + let amount: Balance = 100 * UNIT; + // Asset does not exist. + assert_eq!( + decoded::(decrease_allowance(addr.clone(), 0, BOB, amount)), + Ok(Module { index: 52, error: 3 }), + ); + // Create asset and mint to the address contract, delegate Bob to spend the `amount`. + let asset = + create_asset_mint_and_approve(addr.clone(), 0, addr.clone(), amount, BOB, amount); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(addr.clone(), asset); + assert_eq!( + decoded::(decrease_allowance(addr.clone(), asset, BOB, amount)), + Ok(Module { index: 52, error: 16 }), + ); + thaw_asset(addr.clone(), asset); + // Successfully decrease allowance. + let bob_allowance_before = Assets::allowance(asset, &addr, &BOB); + let result = decrease_allowance(addr.clone(), 0, BOB, amount / 2 - 1 * UNIT); + assert!(!result.did_revert(), "Contract reverted!"); + let bob_allowance_after = Assets::allowance(asset, &addr, &BOB); + assert_eq!(bob_allowance_before - bob_allowance_after, amount / 2 - 1 * UNIT); + // Asset is not live, i.e. frozen or being destroyed. + start_destroy_asset(addr.clone(), asset); + assert_eq!( + decoded::(decrease_allowance(addr.clone(), asset, BOB, amount)), + Ok(Module { index: 52, error: 16 }), + ); + }); +} + /// 2. PSP-22 Metadata Interface: /// - token_name /// - token_symbol diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index 884128f2..b3932a79 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -20,7 +20,12 @@ impl Contains for AllowedApiCalls { use fungibles::Call::*; matches!( c, - RuntimeCall::Fungibles(transfer { .. } | approve { .. } | increase_allowance { .. }) + RuntimeCall::Fungibles( + transfer { .. } + | transfer_from { .. } + | approve { .. } | increase_allowance { .. } + | decrease_allowance { .. } + ) ) } } From d8ac5bd433b04359c45987ca5c3d0e78855c59a8 Mon Sep 17 00:00:00 2001 From: Daan van der Plas <93204684+Daanvdplas@users.noreply.github.com> Date: Tue, 6 Aug 2024 14:45:07 +0200 Subject: [PATCH 34/76] refactor: api integration tests (#133) Co-authored-by: Frank Bell --- pallets/api/src/fungibles/mod.rs | 222 ++++-- pallets/api/src/fungibles/tests.rs | 129 ++- pop-api/examples/balance-transfer/lib.rs | 3 +- pop-api/examples/nfts/lib.rs | 49 +- pop-api/examples/place-spot-order/lib.rs | 14 +- pop-api/examples/read-runtime-state/lib.rs | 55 +- .../integration-tests/contracts/.gitignore | 9 + .../create_token_in_constructor/Cargo.toml | 21 + .../create_token_in_constructor/lib.rs | 46 ++ .../contracts/fungibles/lib.rs | 100 +-- .../integration-tests/src/fungibles/mod.rs | 546 +++++++++++++ .../integration-tests/src/fungibles/utils.rs | 360 +++++++++ pop-api/integration-tests/src/lib.rs | 2 +- .../integration-tests/src/local_fungibles.rs | 738 ------------------ pop-api/src/lib.rs | 18 +- pop-api/src/v0/assets/fungibles.rs | 344 ++++---- runtime/devnet/src/config/api.rs | 6 +- 17 files changed, 1559 insertions(+), 1103 deletions(-) create mode 100755 pop-api/integration-tests/contracts/.gitignore create mode 100755 pop-api/integration-tests/contracts/create_token_in_constructor/Cargo.toml create mode 100755 pop-api/integration-tests/contracts/create_token_in_constructor/lib.rs create mode 100644 pop-api/integration-tests/src/fungibles/mod.rs create mode 100644 pop-api/integration-tests/src/fungibles/utils.rs delete mode 100644 pop-api/integration-tests/src/local_fungibles.rs diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index 9fb444f8..eefad0e1 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -28,6 +28,7 @@ type BalanceOf = > as Inspect< #[frame_support::pallet] pub mod pallet { use super::*; + use core::cmp::Ordering::*; use frame_support::{ dispatch::{DispatchResult, DispatchResultWithPostInfo, WithPostDispatchInfo}, pallet_prelude::*, @@ -75,6 +76,9 @@ pub mod pallet { /// Token decimals for a given asset ID. #[codec(index = 10)] TokenDecimals(AssetIdOf), + /// Check if token with a given asset ID exists. + #[codec(index = 18)] + AssetExists(AssetIdOf), } /// Configure the pallet by specifying the parameters and types on which it depends. @@ -95,49 +99,49 @@ pub mod pallet { /// `data` in unspecified format. /// /// # Parameters - /// * `id` - The ID of the asset. - /// * `to` - The recipient account. - /// * `value` - The number of tokens to transfer. + /// - `id` - The ID of the asset. + /// - `to` - The recipient account. + /// - `value` - The number of tokens to transfer. #[pallet::call_index(3)] #[pallet::weight(AssetsWeightInfoOf::::transfer_keep_alive())] pub fn transfer( origin: OriginFor, id: AssetIdOf, - target: AccountIdOf, - amount: BalanceOf, + to: AccountIdOf, + value: BalanceOf, ) -> DispatchResult { - let target = T::Lookup::unlookup(target); - AssetsOf::::transfer_keep_alive(origin, id.into(), target, amount) + let to = T::Lookup::unlookup(to); + AssetsOf::::transfer_keep_alive(origin, id.into(), to, value) } - /// Transfers `value` amount of tokens from the delegated account approved by the `owner` to - /// account `to`, with additional `data` in unspecified format. + /// Transfers `value` amount tokens on behalf of `from` to account `to` with additional `data` + /// in unspecified format. /// /// # Parameters - /// * `id` - The ID of the asset. - /// * `owner` - The account from which the asset balance will be withdrawn. - /// * `to` - The recipient account. - /// * `value` - The number of tokens to transfer. + /// - `id` - The ID of the asset. + /// - `owner` - The account from which the asset balance will be withdrawn. + /// - `to` - The recipient account. + /// - `value` - The number of tokens to transfer. #[pallet::call_index(4)] #[pallet::weight(AssetsWeightInfoOf::::transfer_approved())] pub fn transfer_from( origin: OriginFor, id: AssetIdOf, - owner: AccountIdOf, - target: AccountIdOf, - amount: BalanceOf, + from: AccountIdOf, + to: AccountIdOf, + value: BalanceOf, ) -> DispatchResult { - let owner = T::Lookup::unlookup(owner); - let target = T::Lookup::unlookup(target); - AssetsOf::::transfer_approved(origin, id.into(), owner, target, amount) + let from = T::Lookup::unlookup(from); + let to = T::Lookup::unlookup(to); + AssetsOf::::transfer_approved(origin, id.into(), from, to, value) } /// Approves an account to spend a specified number of tokens on behalf of the caller. /// /// # Parameters - /// * `id` - The ID of the asset. - /// * `spender` - The account that is allowed to spend the tokens. - /// * `value` - The number of tokens to approve. + /// - `id` - The ID of the asset. + /// - `spender` - The account that is allowed to spend the tokens. + /// - `value` - The number of tokens to approve. #[pallet::call_index(5)] #[pallet::weight(::WeightInfo::approve(1, 1))] pub fn approve( @@ -152,30 +156,32 @@ pub mod pallet { let spender = T::Lookup::unlookup(spender); let id: AssetIdParameterOf = id.into(); - // If the new value is equal to the current allowance, do nothing. - let return_weight = if value == current_allowance { - Self::weight_approve(0, 0) - } - // If the new value is greater than the current allowance, approve the difference - // because `approve_transfer` works additively (see `pallet-assets`). - else if value > current_allowance { - AssetsOf::::approve_transfer( - origin, - id, - spender, - value.saturating_sub(current_allowance), - ) - .map_err(|e| e.with_weight(Self::weight_approve(1, 0)))?; - Self::weight_approve(1, 0) - } else { - // If the new value is less than the current allowance, cancel the approval and set the new value - AssetsOf::::cancel_approval(origin.clone(), id.clone(), spender.clone()) - .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; - if value.is_zero() { - return Ok(Some(Self::weight_approve(0, 1)).into()); - } - AssetsOf::::approve_transfer(origin, id, spender, value)?; - Self::weight_approve(1, 1) + let return_weight = match value.cmp(¤t_allowance) { + // If the new value is equal to the current allowance, do nothing. + Equal => Self::weight_approve(0, 0), + // If the new value is greater than the current allowance, approve the difference + // because `approve_transfer` works additively (see `pallet-assets`). + Greater => { + AssetsOf::::approve_transfer( + origin, + id, + spender, + value.saturating_sub(current_allowance), + ) + .map_err(|e| e.with_weight(Self::weight_approve(1, 0)))?; + Self::weight_approve(1, 0) + }, + // If the new value is less than the current allowance, cancel the approval and + // set the new value. + Less => { + AssetsOf::::cancel_approval(origin.clone(), id.clone(), spender.clone()) + .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; + if value.is_zero() { + return Ok(Some(Self::weight_approve(0, 1)).into()); + } + AssetsOf::::approve_transfer(origin, id, spender, value)?; + Self::weight_approve(1, 1) + }, }; Ok(Some(return_weight).into()) } @@ -183,9 +189,9 @@ pub mod pallet { /// Increases the allowance of a spender. /// /// # Parameters - /// * `id` - The ID of the asset. - /// * `spender` - The account that is allowed to spend the tokens. - /// * `value` - The number of tokens to increase the allowance by. + /// - `id` - The ID of the asset. + /// - `spender` - The account that is allowed to spend the tokens. + /// - `value` - The number of tokens to increase the allowance by. #[pallet::call_index(6)] #[pallet::weight(AssetsWeightInfoOf::::approve_transfer())] pub fn increase_allowance( @@ -201,9 +207,9 @@ pub mod pallet { /// Decreases the allowance of a spender. /// /// # Parameters - /// * `id` - The ID of the asset. - /// * `spender` - The account that is allowed to spend the tokens. - /// * `value` - The number of tokens to decrease the allowance by. + /// - `id` - The ID of the asset. + /// - `spender` - The account that is allowed to spend the tokens. + /// - `value` - The number of tokens to decrease the allowance by. #[pallet::call_index(7)] #[pallet::weight(::WeightInfo::approve(1, 1))] pub fn decrease_allowance( @@ -214,25 +220,118 @@ pub mod pallet { ) -> DispatchResultWithPostInfo { let who = ensure_signed(origin.clone()) .map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?; - let mut current_allowance = AssetsOf::::allowance(id.clone(), &who, &spender); + let current_allowance = AssetsOf::::allowance(id.clone(), &who, &spender); let spender = T::Lookup::unlookup(spender); let id: AssetIdParameterOf = id.into(); if value.is_zero() { return Ok(Some(Self::weight_approve(0, 0)).into()); } - - current_allowance.saturating_reduce(value); - // Cancel the aproval and set the new value if `current_allowance` is more than zero. + // Cancel the aproval and set the new value if `new_allowance` is more than zero. AssetsOf::::cancel_approval(origin.clone(), id.clone(), spender.clone()) .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; - - if current_allowance.is_zero() { + let new_allowance = current_allowance.saturating_sub(value); + if new_allowance.is_zero() { return Ok(Some(Self::weight_approve(0, 1)).into()); } - AssetsOf::::approve_transfer(origin, id, spender, current_allowance)?; + AssetsOf::::approve_transfer(origin, id, spender, new_allowance)?; Ok(().into()) } + + /// Create a new token with a given asset ID. + /// + /// # Parameters + /// - `id` - The ID of the asset. + /// - `admin` - The account that will administer the asset. + /// - `min_balance` - The minimum balance required for accounts holding this asset. + #[pallet::call_index(11)] + #[pallet::weight(AssetsWeightInfoOf::::create())] + pub fn create( + origin: OriginFor, + id: AssetIdOf, + admin: AccountIdOf, + min_balance: BalanceOf, + ) -> DispatchResult { + let admin = T::Lookup::unlookup(admin); + AssetsOf::::create(origin, id.into(), admin, min_balance) + } + + /// Start the process of destroying a token with a given asset ID. + /// + /// # Parameters + /// - `id` - The ID of the asset. + #[pallet::call_index(12)] + #[pallet::weight(AssetsWeightInfoOf::::start_destroy())] + pub fn start_destroy(origin: OriginFor, id: AssetIdOf) -> DispatchResult { + AssetsOf::::start_destroy(origin, id.into()) + } + + /// Set the metadata for a token with a given asset ID. + /// + /// # Parameters + /// - `id`: The identifier of the asset to update. + /// - `name`: The user friendly name of this asset. Limited in length by + /// `pallet_assets::Config::StringLimit`. + /// - `symbol`: The exchange symbol for this asset. Limited in length by + /// `pallet_assets::Config::StringLimit`. + /// - `decimals`: The number of decimals this asset uses to represent one unit. + #[pallet::call_index(16)] + #[pallet::weight(AssetsWeightInfoOf::::set_metadata(name.len() as u32, symbol.len() as u32))] + pub fn set_metadata( + origin: OriginFor, + id: AssetIdOf, + name: Vec, + symbol: Vec, + decimals: u8, + ) -> DispatchResult { + AssetsOf::::set_metadata(origin, id.into(), name, symbol, decimals) + } + + /// Clear the metadata for a token with a given asset ID. + /// + /// # Parameters + /// - `id` - The ID of the asset. + #[pallet::call_index(17)] + #[pallet::weight(AssetsWeightInfoOf::::clear_metadata())] + pub fn clear_metadata(origin: OriginFor, id: AssetIdOf) -> DispatchResult { + AssetsOf::::clear_metadata(origin, id.into()) + } + + /// Creates `value` amount tokens and assigns them to `account`, increasing the total supply. + /// + /// # Parameters + /// - `id` - The ID of the asset. + /// - `account` - The account to be credited with the created tokens. + /// - `value` - The number of tokens to mint. + #[pallet::call_index(19)] + #[pallet::weight(AssetsWeightInfoOf::::mint())] + pub fn mint( + origin: OriginFor, + id: AssetIdOf, + account: AccountIdOf, + value: BalanceOf, + ) -> DispatchResult { + let account = T::Lookup::unlookup(account); + AssetsOf::::mint(origin, id.into(), account, value) + } + + /// Destroys `value` amount tokens from `account`, reducing the total supply. + /// + /// # Parameters + /// - `id` - The ID of the asset. + /// - `account` - The account from which the tokens will be destroyed. + /// - `value` - The number of tokens to destroy. + #[pallet::call_index(20)] + #[pallet::weight(AssetsWeightInfoOf::::burn())] + pub fn burn( + origin: OriginFor, + id: AssetIdOf, + account: AccountIdOf, + value: BalanceOf, + ) -> DispatchResult { + let account = T::Lookup::unlookup(account); + AssetsOf::::burn(origin, id.into(), account, value) + } } impl Pallet { @@ -242,8 +341,8 @@ pub mod pallet { /// encoded result. /// /// # Parameter - /// * `value` - An instance of `Read`, which specifies the type of state query and - /// the associated parameters. + /// - `value` - An instance of `Read`, which specifies the type of state query and + /// the associated parameters. pub fn read_state(value: Read) -> Vec { use Read::*; @@ -262,6 +361,7 @@ pub mod pallet { TokenDecimals(id) => { as MetadataInspect>>::decimals(id).encode() }, + AssetExists(id) => AssetsOf::::asset_exists(id).encode(), } } diff --git a/pallets/api/src/fungibles/tests.rs b/pallets/api/src/fungibles/tests.rs index 4377fc83..d6cc87e0 100644 --- a/pallets/api/src/fungibles/tests.rs +++ b/pallets/api/src/fungibles/tests.rs @@ -2,7 +2,9 @@ use crate::{fungibles::Read::*, mock::*}; use codec::Encode; use frame_support::{ assert_ok, - traits::fungibles::{approvals::Inspect, metadata::Inspect as MetadataInspect}, + traits::fungibles::{ + approvals::Inspect as ApprovalInspect, metadata::Inspect as MetadataInspect, Inspect, + }, }; const ASSET: u32 = 42; @@ -38,24 +40,6 @@ fn transfer_from_works() { }); } -#[test] -fn decrease_allowance_works() { - new_test_ext().execute_with(|| { - let amount: Balance = 100 * UNIT; - create_asset_mint_and_approve(ALICE, ASSET, ALICE, amount, BOB, amount); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount); - // Owner balance is not changed if decreased by zero. - assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, 0)); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount); - // Decrease allowance successfully. - assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, amount / 2 - 1 * UNIT)); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount / 2 + 1 * UNIT); - // Saturating if current allowance is decreased more than the owner balance. - assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, amount)); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), 0); - }); -} - // Non-additive, sets new value. #[test] fn approve_works() { @@ -94,6 +78,99 @@ fn increase_allowance_works() { }); } +#[test] +fn decrease_allowance_works() { + new_test_ext().execute_with(|| { + let amount: Balance = 100 * UNIT; + create_asset_mint_and_approve(ALICE, ASSET, ALICE, amount, BOB, amount); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount); + // Owner balance is not changed if decreased by zero. + assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, 0)); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount); + // Decrease allowance successfully. + assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, amount / 2 - 1 * UNIT)); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount / 2 + 1 * UNIT); + // Saturating if current allowance is decreased more than the owner balance. + assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, amount)); + assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), 0); + }); +} + +#[test] +fn create_works() { + new_test_ext().execute_with(|| { + assert!(!Assets::asset_exists(ASSET)); + assert_ok!(Fungibles::create(signed(ALICE), ASSET, ALICE, 100)); + assert!(Assets::asset_exists(ASSET)); + }); +} + +#[test] +fn start_destroy_works() { + new_test_ext().execute_with(|| { + create_asset(ALICE, ASSET); + assert_ok!(Fungibles::start_destroy(signed(ALICE), ASSET)); + }); +} + +#[test] +fn set_metadata_works() { + new_test_ext().execute_with(|| { + let name = vec![42]; + let symbol = vec![42]; + let decimals = 42; + create_asset(ALICE, ASSET); + assert_ok!(Fungibles::set_metadata( + signed(ALICE), + ASSET, + name.clone(), + symbol.clone(), + decimals + )); + assert_eq!(Assets::name(ASSET), name); + assert_eq!(Assets::symbol(ASSET), symbol); + assert_eq!(Assets::decimals(ASSET), decimals); + }); +} + +#[test] +fn clear_metadata_works() { + new_test_ext().execute_with(|| { + let name = vec![42]; + let symbol = vec![42]; + let decimals = 42; + create_asset_and_set_metadata(ALICE, ASSET, name, symbol, decimals); + assert_ok!(Fungibles::clear_metadata(signed(ALICE), ASSET)); + assert_eq!(Assets::name(ASSET), Vec::::new()); + assert_eq!(Assets::symbol(ASSET), Vec::::new()); + assert_eq!(Assets::decimals(ASSET), 0u8); + }); +} + +#[test] +fn mint_works() { + new_test_ext().execute_with(|| { + let amount: Balance = 100 * UNIT; + create_asset(ALICE, ASSET); + let balance_before_mint = Assets::balance(ASSET, &BOB); + assert_ok!(Fungibles::mint(signed(ALICE), ASSET, BOB, amount)); + let balance_after_mint = Assets::balance(ASSET, &BOB); + assert_eq!(balance_after_mint, balance_before_mint + amount); + }); +} + +#[test] +fn burn_works() { + new_test_ext().execute_with(|| { + let amount: Balance = 100 * UNIT; + create_asset_and_mint_to(ALICE, ASSET, BOB, amount); + let balance_before_burn = Assets::balance(ASSET, &BOB); + assert_ok!(Fungibles::burn(signed(ALICE), ASSET, BOB, amount)); + let balance_after_burn = Assets::balance(ASSET, &BOB); + assert_eq!(balance_after_burn, balance_before_burn - amount); + }); +} + #[test] fn total_supply_works() { new_test_ext().execute_with(|| { @@ -137,12 +214,20 @@ fn token_metadata_works() { }); } +#[test] +fn asset_exists_works() { + new_test_ext().execute_with(|| { + create_asset(ALICE, ASSET); + assert_eq!(Assets::asset_exists(ASSET).encode(), Fungibles::read_state(AssetExists(ASSET))); + }); +} + fn signed(account: AccountId) -> RuntimeOrigin { RuntimeOrigin::signed(account) } -fn create_asset(owner: AccountId, asset_id: AssetId, min_balance: Balance) { - assert_ok!(Assets::create(signed(owner), asset_id, owner, min_balance)); +fn create_asset(owner: AccountId, asset_id: AssetId) { + assert_ok!(Assets::create(signed(owner), asset_id, owner, 1)); } fn mint_asset(owner: AccountId, asset_id: AssetId, to: AccountId, value: Balance) { @@ -150,7 +235,7 @@ fn mint_asset(owner: AccountId, asset_id: AssetId, to: AccountId, value: Balance } fn create_asset_and_mint_to(owner: AccountId, asset_id: AssetId, to: AccountId, value: Balance) { - create_asset(owner, asset_id, 1); + create_asset(owner, asset_id); mint_asset(owner, asset_id, to, value) } diff --git a/pop-api/examples/balance-transfer/lib.rs b/pop-api/examples/balance-transfer/lib.rs index 328a818b..d36dfa53 100755 --- a/pop-api/examples/balance-transfer/lib.rs +++ b/pop-api/examples/balance-transfer/lib.rs @@ -1,3 +1,4 @@ +// DEPRECATED #![cfg_attr(not(feature = "std"), no_std, no_main)] use pop_api::balances::*; @@ -131,4 +132,4 @@ mod balance_transfer { Ok(()) } } -} \ No newline at end of file +} diff --git a/pop-api/examples/nfts/lib.rs b/pop-api/examples/nfts/lib.rs index d47140e7..7920c179 100755 --- a/pop-api/examples/nfts/lib.rs +++ b/pop-api/examples/nfts/lib.rs @@ -1,3 +1,4 @@ +// DEPRECATED #![cfg_attr(not(feature = "std"), no_std, no_main)] use pop_api::nfts::*; @@ -33,27 +34,29 @@ mod nfts { } #[ink(message)] - pub fn create_nft_collection( &self ) -> Result<(), ContractError>{ + pub fn create_nft_collection(&self) -> Result<(), ContractError> { ink::env::debug_println!("Nfts::create_nft_collection: collection creation started."); - let admin = Self::env().caller(); - let item_settings = ItemSettings(BitFlags::from(ItemSetting::Transferable)); - - let mint_settings = MintSettings { - mint_type: MintType::Issuer, - price: Some(0), - start_block: Some(0), - end_block: Some(0), - default_item_settings: item_settings, - }; - - let config = CollectionConfig { - settings: CollectionSettings(BitFlags::from(CollectionSetting::TransferableItems)), - max_supply: None, - mint_settings, - }; - pop_api::nfts::create(admin, config)?; - ink::env::debug_println!("Nfts::create_nft_collection: collection created successfully."); - Ok(()) + let admin = Self::env().caller(); + let item_settings = ItemSettings(BitFlags::from(ItemSetting::Transferable)); + + let mint_settings = MintSettings { + mint_type: MintType::Issuer, + price: Some(0), + start_block: Some(0), + end_block: Some(0), + default_item_settings: item_settings, + }; + + let config = CollectionConfig { + settings: CollectionSettings(BitFlags::from(CollectionSetting::TransferableItems)), + max_supply: None, + mint_settings, + }; + pop_api::nfts::create(admin, config)?; + ink::env::debug_println!( + "Nfts::create_nft_collection: collection created successfully." + ); + Ok(()) } #[ink(message)] @@ -82,9 +85,7 @@ mod nfts { // check owner match owner(collection_id, item_id)? { Some(owner) if owner == receiver => { - ink::env::debug_println!( - "Nfts::mint success: minted item belongs to receiver" - ); + ink::env::debug_println!("Nfts::mint success: minted item belongs to receiver"); }, _ => { return Err(ContractError::NotOwner); @@ -113,4 +114,4 @@ mod nfts { Nfts::new(); } } -} \ No newline at end of file +} diff --git a/pop-api/examples/place-spot-order/lib.rs b/pop-api/examples/place-spot-order/lib.rs index f5e34f7f..669b9190 100755 --- a/pop-api/examples/place-spot-order/lib.rs +++ b/pop-api/examples/place-spot-order/lib.rs @@ -1,3 +1,4 @@ +// DEPRECATED #![cfg_attr(not(feature = "std"), no_std, no_main)] #[ink::contract(env = pop_api::Environment)] @@ -15,11 +16,7 @@ mod spot_order { } #[ink(message)] - pub fn place_spot_order( - &mut self, - max_amount: Balance, - para_id: u32, - ) { + pub fn place_spot_order(&mut self, max_amount: Balance, para_id: u32) { ink::env::debug_println!( "SpotOrder::place_spot_order: max_amount {:?} para_id: {:?} ", max_amount, @@ -28,10 +25,7 @@ mod spot_order { #[allow(unused_variables)] let res = pop_api::cross_chain::coretime::place_spot_order(max_amount, para_id); - ink::env::debug_println!( - "SpotOrder::place_spot_order: res {:?} ", - res, - ); + ink::env::debug_println!("SpotOrder::place_spot_order: res {:?} ", res,); ink::env::debug_println!("SpotOrder::place_spot_order end"); } @@ -46,4 +40,4 @@ mod spot_order { SpotOrder::new(); } } -} \ No newline at end of file +} diff --git a/pop-api/examples/read-runtime-state/lib.rs b/pop-api/examples/read-runtime-state/lib.rs index 05a44108..60ef70f0 100755 --- a/pop-api/examples/read-runtime-state/lib.rs +++ b/pop-api/examples/read-runtime-state/lib.rs @@ -1,35 +1,36 @@ +// DEPRECATED #![cfg_attr(not(feature = "std"), no_std, no_main)] #[ink::contract(env = pop_api::Environment)] mod read_relay_blocknumber { - use pop_api::primitives::storage_keys::{ - ParachainSystemKeys::LastRelayChainBlockNumber, RuntimeStateKeys::ParachainSystem, - }; + use pop_api::primitives::storage_keys::{ + ParachainSystemKeys::LastRelayChainBlockNumber, RuntimeStateKeys::ParachainSystem, + }; - #[ink(event)] - pub struct RelayBlockNumberRead { - value: BlockNumber, - } + #[ink(event)] + pub struct RelayBlockNumberRead { + value: BlockNumber, + } - #[ink(storage)] - #[derive(Default)] - pub struct ReadRelayBlockNumber; + #[ink(storage)] + #[derive(Default)] + pub struct ReadRelayBlockNumber; - impl ReadRelayBlockNumber { - #[ink(constructor, payable)] - pub fn new() -> Self { - ink::env::debug_println!("ReadRelayBlockNumber::new"); - Default::default() - } + impl ReadRelayBlockNumber { + #[ink(constructor, payable)] + pub fn new() -> Self { + ink::env::debug_println!("ReadRelayBlockNumber::new"); + Default::default() + } - #[ink(message)] - pub fn read_relay_block_number(&self) { - let result = - pop_api::state::read::(ParachainSystem(LastRelayChainBlockNumber)); - ink::env::debug_println!("Last relay block number read by contract: {:?}", result); - self.env().emit_event(RelayBlockNumberRead { - value: result.expect("Failed to read relay block number."), - }); - } - } -} \ No newline at end of file + #[ink(message)] + pub fn read_relay_block_number(&self) { + let result = + pop_api::state::read::(ParachainSystem(LastRelayChainBlockNumber)); + ink::env::debug_println!("Last relay block number read by contract: {:?}", result); + self.env().emit_event(RelayBlockNumberRead { + value: result.expect("Failed to read relay block number."), + }); + } + } +} diff --git a/pop-api/integration-tests/contracts/.gitignore b/pop-api/integration-tests/contracts/.gitignore new file mode 100755 index 00000000..d60800c8 --- /dev/null +++ b/pop-api/integration-tests/contracts/.gitignore @@ -0,0 +1,9 @@ +# Ignore build artifacts from the local tests sub-crate. +**/target/ + +# Ignore backup files creates by cargo fmt. +**/*.rs.bk + +# Remove Cargo.lock when creating an executable, leave it for libraries +# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock +**/Cargo.lock diff --git a/pop-api/integration-tests/contracts/create_token_in_constructor/Cargo.toml b/pop-api/integration-tests/contracts/create_token_in_constructor/Cargo.toml new file mode 100755 index 00000000..2c202715 --- /dev/null +++ b/pop-api/integration-tests/contracts/create_token_in_constructor/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "create_token_in_constructor" +version = "0.1.0" +authors = ["[your_name] <[your_email]>"] +edition = "2021" + +[dependencies] +ink = { version = "5.0.0", default-features = false } +pop-api = { path = "../../..", default-features = false, features = ["fungibles"] } + +[lib] +path = "lib.rs" + +[features] +default = ["std"] +std = [ + "ink/std", + "pop-api/std", +] +ink-as-dependency = [] +e2e-tests = [] diff --git a/pop-api/integration-tests/contracts/create_token_in_constructor/lib.rs b/pop-api/integration-tests/contracts/create_token_in_constructor/lib.rs new file mode 100755 index 00000000..e9e5d127 --- /dev/null +++ b/pop-api/integration-tests/contracts/create_token_in_constructor/lib.rs @@ -0,0 +1,46 @@ +#![cfg_attr(not(feature = "std"), no_std, no_main)] + +use ink::prelude::vec::Vec; +use pop_api::{ + assets::fungibles::{self as api}, + primitives::AssetId, + StatusCode, +}; + +pub type Result = core::result::Result; + +#[ink::contract] +mod create_token_in_constructor { + use super::*; + + #[ink(storage)] + pub struct Fungible { + id: AssetId, + } + + impl Fungible { + #[ink(constructor, payable)] + pub fn new(id: AssetId, min_balance: Balance) -> Result { + let contract = Self { id }; + // AccountId of the contract which will be set to the owner of the fungible token. + let owner = contract.env().account_id(); + api::create(id, owner, min_balance)?; + Ok(contract) + } + + #[ink(message)] + pub fn asset_exists(&self) -> Result { + api::asset_exists(self.id) + } + } + + #[cfg(test)] + mod tests { + use super::*; + + #[ink::test] + fn default_works() { + PopApiFungiblesExample::new(); + } + } +} diff --git a/pop-api/integration-tests/contracts/fungibles/lib.rs b/pop-api/integration-tests/contracts/fungibles/lib.rs index 1b42fec4..239d3a2d 100755 --- a/pop-api/integration-tests/contracts/fungibles/lib.rs +++ b/pop-api/integration-tests/contracts/fungibles/lib.rs @@ -122,55 +122,57 @@ mod fungibles { api::token_decimals(id) } - // 3. Asset Management: - // - create - // - start_destroy - // - destroy_accounts - // - destroy_approvals - // - finish_destroy - // - set_metadata - // - clear_metadata - - // #[ink(message)] - // pub fn create(&self, id: AssetId, admin: AccountId, min_balance: Balance) -> Result<()> { - // ink::env::debug_println!( - // "PopApiFungiblesExample::create: id: {:?} admin: {:?} min_balance: {:?}", - // id, - // admin, - // min_balance, - // ); - // let result = api::create(id, admin, min_balance); - // ink::env::debug_println!("Result: {:?}", result); - // result.map_err(|e| e.into()) - // result - // } - - // #[ink(message)] - // pub fn set_metadata( - // &self, - // id: AssetId, - // name: Vec, - // symbol: Vec, - // decimals: u8, - // ) -> Result<()> { - // ink::env::debug_println!( - // "PopApiFungiblesExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}", - // id, - // name, - // symbol, - // decimals, - // ); - // let result = api::set_metadata(id, name, symbol, decimals); - // ink::env::debug_println!("Result: {:?}", result); - // // result.map_err(|e| e.into()) - // result - // } - // - // #[ink(message)] - // pub fn asset_exists(&self, id: AssetId) -> Result { - // // api::asset_exists(id).map_err(|e| e.into()) - // api::asset_exists(id) - // } + /// 3. Asset Management: + /// - create + /// - start_destroy + /// - set_metadata + /// - clear_metadata + /// - asset_exists + + #[ink(message)] + pub fn create(&self, id: AssetId, admin: AccountId, min_balance: Balance) -> Result<()> { + api::create(id, admin, min_balance) + } + + #[ink(message)] + pub fn start_destroy(&self, id: AssetId) -> Result<()> { + api::start_destroy(id) + } + + #[ink(message)] + pub fn set_metadata( + &self, + id: AssetId, + name: Vec, + symbol: Vec, + decimals: u8, + ) -> Result<()> { + api::set_metadata(id, name, symbol, decimals) + } + + #[ink(message)] + pub fn clear_metadata(&self, id: AssetId) -> Result<()> { + api::clear_metadata(id) + } + + #[ink(message)] + pub fn asset_exists(&self, id: AssetId) -> Result { + api::asset_exists(id) + } + + /// 4. PSP-22 Mintable & Burnable Interface: + /// - mint + /// - burn + + #[ink(message)] + pub fn mint(&self, id: AssetId, account: AccountId, amount: Balance) -> Result<()> { + api::mint(id, account, amount) + } + + #[ink(message)] + pub fn burn(&self, id: AssetId, account: AccountId, amount: Balance) -> Result<()> { + api::burn(id, account, amount) + } } #[cfg(test)] diff --git a/pop-api/integration-tests/src/fungibles/mod.rs b/pop-api/integration-tests/src/fungibles/mod.rs new file mode 100644 index 00000000..0ca7d4f9 --- /dev/null +++ b/pop-api/integration-tests/src/fungibles/mod.rs @@ -0,0 +1,546 @@ +use super::*; +use pop_primitives::error::{ + ArithmeticError::*, + Error::{self, *}, + TokenError::*, +}; +use utils::*; + +mod utils; + +const ASSET_ID: AssetId = 1; +const CONTRACT: &str = "contracts/fungibles/target/ink/fungibles.wasm"; + +/// 1. PSP-22 Interface: +/// - total_supply +/// - balance_of +/// - allowance +/// - transfer +/// - transfer_from +/// - approve +/// - increase_allowance +/// - decrease_allowance + +#[test] +fn total_supply_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + + // No tokens in circulation. + assert_eq!(total_supply(addr.clone(), ASSET_ID), Ok(Assets::total_supply(ASSET_ID))); + assert_eq!(total_supply(addr.clone(), ASSET_ID), Ok(0)); + + // Tokens in circulation. + create_asset_and_mint_to(addr.clone(), ASSET_ID, BOB, 100); + assert_eq!(total_supply(addr.clone(), ASSET_ID), Ok(Assets::total_supply(ASSET_ID))); + assert_eq!(total_supply(addr, ASSET_ID), Ok(100)); + }); +} + +#[test] +fn balance_of_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + + // No tokens in circulation. + assert_eq!(balance_of(addr.clone(), ASSET_ID, BOB), Ok(Assets::balance(ASSET_ID, BOB))); + assert_eq!(balance_of(addr.clone(), ASSET_ID, BOB), Ok(0)); + + // Tokens in circulation. + create_asset_and_mint_to(addr.clone(), ASSET_ID, BOB, 100); + assert_eq!(balance_of(addr.clone(), ASSET_ID, BOB), Ok(Assets::balance(ASSET_ID, BOB))); + assert_eq!(balance_of(addr, ASSET_ID, BOB), Ok(100)); + }); +} + +#[test] +fn allowance_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + + // No tokens in circulation. + assert_eq!( + allowance(addr.clone(), ASSET_ID, BOB, ALICE), + Ok(Assets::allowance(ASSET_ID, &BOB, &ALICE)) + ); + assert_eq!(allowance(addr.clone(), ASSET_ID, BOB, ALICE), Ok(0)); + + // Tokens in circulation. + create_asset_mint_and_approve(addr.clone(), ASSET_ID, BOB, 100, ALICE, 50); + assert_eq!( + allowance(addr.clone(), ASSET_ID, BOB, ALICE), + Ok(Assets::allowance(ASSET_ID, &BOB, &ALICE)) + ); + assert_eq!(allowance(addr, ASSET_ID, BOB, ALICE), Ok(50)); + }); +} + +#[test] +fn transfer_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + let amount: Balance = 100 * UNIT; + + // Asset does not exist. + assert_eq!(transfer(addr.clone(), 1, BOB, amount), Err(Module { index: 52, error: 3 })); + // Create asset with Alice as owner and mint `amount` to contract address. + let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(ALICE, asset); + assert_eq!( + transfer(addr.clone(), asset, BOB, amount), + Err(Module { index: 52, error: 16 }) + ); + thaw_asset(ALICE, asset); + // Not enough balance. + assert_eq!( + transfer(addr.clone(), asset, BOB, amount + 1 * UNIT), + Err(Module { index: 52, error: 0 }) + ); + // Not enough balance due to ED. + assert_eq!(transfer(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 0 })); + // Successful transfer. + let balance_before_transfer = Assets::balance(asset, &BOB); + assert_ok!(transfer(addr.clone(), asset, BOB, amount / 2)); + let balance_after_transfer = Assets::balance(asset, &BOB); + assert_eq!(balance_after_transfer, balance_before_transfer + amount / 2); + // Transfer asset to account that does not exist. + assert_eq!(transfer(addr.clone(), asset, FERDIE, amount / 4), Err(Token(CannotCreate))); + // Asset is not live, i.e. frozen or being destroyed. + start_destroy_asset(ALICE, asset); + assert_eq!( + transfer(addr.clone(), asset, BOB, amount / 4), + Err(Module { index: 52, error: 16 }) + ); + }); +} + +#[test] +fn transfer_from_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + let amount: Balance = 100 * UNIT; + + // Asset does not exist. + assert_eq!( + transfer_from(addr.clone(), 1, ALICE, BOB, amount / 2), + Err(Module { index: 52, error: 3 }), + ); + // Create asset with Alice as owner and mint `amount` to contract address. + let asset = create_asset_and_mint_to(ALICE, 1, ALICE, amount); + // Unapproved transfer. + assert_eq!( + transfer_from(addr.clone(), asset, ALICE, BOB, amount / 2), + Err(Module { index: 52, error: 10 }) + ); + assert_ok!(Assets::approve_transfer( + RuntimeOrigin::signed(ALICE.into()), + asset.into(), + addr.clone().into(), + amount + 1 * UNIT, + )); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(ALICE, asset); + assert_eq!( + transfer_from(addr.clone(), asset, ALICE, BOB, amount), + Err(Module { index: 52, error: 16 }), + ); + thaw_asset(ALICE, asset); + // Not enough balance. + assert_eq!( + transfer_from(addr.clone(), asset, ALICE, BOB, amount + 1 * UNIT), + Err(Module { index: 52, error: 0 }), + ); + // Successful transfer. + let balance_before_transfer = Assets::balance(asset, &BOB); + assert_ok!(transfer_from(addr.clone(), asset, ALICE, BOB, amount / 2)); + let balance_after_transfer = Assets::balance(asset, &BOB); + assert_eq!(balance_after_transfer, balance_before_transfer + amount / 2); + }); +} + +#[test] +fn approve_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, 0, vec![]); + let amount: Balance = 100 * UNIT; + + // Asset does not exist. + assert_eq!(approve(addr.clone(), 0, BOB, amount), Err(Module { index: 52, error: 3 })); + let asset = create_asset_and_mint_to(ALICE, 0, addr.clone(), amount); + assert_eq!(approve(addr.clone(), asset, BOB, amount), Err(ConsumerRemaining)); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![1]); + // Create asset with Alice as owner and mint `amount` to contract address. + let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(ALICE, asset); + assert_eq!(approve(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 16 })); + thaw_asset(ALICE, asset); + // Successful approvals: + assert_eq!(0, Assets::allowance(asset, &addr, &BOB)); + assert_ok!(approve(addr.clone(), asset, BOB, amount)); + assert_eq!(Assets::allowance(asset, &addr, &BOB), amount); + // Non-additive, sets new value. + assert_ok!(approve(addr.clone(), asset, BOB, amount / 2)); + assert_eq!(Assets::allowance(asset, &addr, &BOB), amount / 2); + // Asset is not live, i.e. frozen or being destroyed. + start_destroy_asset(ALICE, asset); + assert_eq!(approve(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 16 })); + }); +} + +#[test] +fn increase_allowance_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let amount: Balance = 100 * UNIT; + // Instantiate a contract without balance - test `ConsumerRemaining. + let addr = instantiate(CONTRACT, 0, vec![]); + // Asset does not exist. + assert_eq!( + increase_allowance(addr.clone(), 0, BOB, amount), + Err(Module { index: 52, error: 3 }) + ); + let asset = create_asset_and_mint_to(ALICE, 0, addr.clone(), amount); + assert_eq!(increase_allowance(addr.clone(), asset, BOB, amount), Err(ConsumerRemaining)); + + // Instantiate a contract with balance. + let addr = instantiate(CONTRACT, INIT_VALUE, vec![1]); + // Create asset with Alice as owner and mint `amount` to contract address. + let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(ALICE, asset); + assert_eq!( + increase_allowance(addr.clone(), asset, BOB, amount), + Err(Module { index: 52, error: 16 }) + ); + thaw_asset(ALICE, asset); + // Successful approvals: + assert_eq!(0, Assets::allowance(asset, &addr, &BOB)); + assert_ok!(increase_allowance(addr.clone(), asset, BOB, amount)); + assert_eq!(Assets::allowance(asset, &addr, &BOB), amount); + // Additive. + assert_ok!(increase_allowance(addr.clone(), asset, BOB, amount)); + assert_eq!(Assets::allowance(asset, &addr, &BOB), amount * 2); + // Asset is not live, i.e. frozen or being destroyed. + start_destroy_asset(ALICE, asset); + assert_eq!( + increase_allowance(addr.clone(), asset, BOB, amount), + Err(Module { index: 52, error: 16 }) + ); + }); +} + +#[test] +fn decrease_allowance_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + let amount: Balance = 100 * UNIT; + + // Asset does not exist. + assert_eq!( + decrease_allowance(addr.clone(), 0, BOB, amount), + Err(Module { index: 52, error: 3 }), + ); + // Create asset and mint `amount` to contract address, then approve Bob to spend `amount`. + let asset = + create_asset_mint_and_approve(addr.clone(), 0, addr.clone(), amount, BOB, amount); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(addr.clone(), asset); + assert_eq!( + decrease_allowance(addr.clone(), asset, BOB, amount), + Err(Module { index: 52, error: 16 }), + ); + thaw_asset(addr.clone(), asset); + // Successfully decrease allowance. + let allowance_before = Assets::allowance(asset, &addr, &BOB); + assert_ok!(decrease_allowance(addr.clone(), 0, BOB, amount / 2 - 1 * UNIT)); + let allowance_after = Assets::allowance(asset, &addr, &BOB); + assert_eq!(allowance_before - allowance_after, amount / 2 - 1 * UNIT); + // Asset is not live, i.e. frozen or being destroyed. + start_destroy_asset(addr.clone(), asset); + assert_eq!( + decrease_allowance(addr.clone(), asset, BOB, amount), + Err(Module { index: 52, error: 16 }), + ); + }); +} + +/// 2. PSP-22 Metadata Interface: +/// - token_name +/// - token_symbol +/// - token_decimals + +#[test] +fn token_metadata_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + let name: Vec = vec![11, 12, 13]; + let symbol: Vec = vec![21, 22, 23]; + let decimals: u8 = 69; + + // Token does not exist. + assert_eq!(token_name(addr.clone(), ASSET_ID), Ok(token_name_asset(ASSET_ID))); + assert_eq!(token_name(addr.clone(), ASSET_ID), Ok(Vec::::new())); + assert_eq!(token_symbol(addr.clone(), ASSET_ID), Ok(token_symbol_asset(ASSET_ID))); + assert_eq!(token_symbol(addr.clone(), ASSET_ID), Ok(Vec::::new())); + assert_eq!(token_decimals(addr.clone(), ASSET_ID), Ok(token_decimals_asset(ASSET_ID))); + assert_eq!(token_decimals(addr.clone(), ASSET_ID), Ok(0)); + // Create Token. + create_asset_and_set_metadata( + addr.clone(), + ASSET_ID, + name.clone(), + symbol.clone(), + decimals, + ); + assert_eq!(token_name(addr.clone(), ASSET_ID), Ok(token_name_asset(ASSET_ID))); + assert_eq!(token_name(addr.clone(), ASSET_ID), Ok(name)); + assert_eq!(token_symbol(addr.clone(), ASSET_ID), Ok(token_symbol_asset(ASSET_ID))); + assert_eq!(token_symbol(addr.clone(), ASSET_ID), Ok(symbol)); + assert_eq!(token_decimals(addr.clone(), ASSET_ID), Ok(token_decimals_asset(ASSET_ID))); + assert_eq!(token_decimals(addr.clone(), ASSET_ID), Ok(decimals)); + }); +} + +/// 3. Asset Management: +/// - create +/// - start_destroy +/// - set_metadata +/// - clear_metadata +/// - asset_exists + +#[test] +fn create_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + // Instantiate a contract without balance for fees. + let addr = instantiate(CONTRACT, 0, vec![0]); + // No balance to pay for fees. + assert_eq!( + create(addr.clone(), ASSET_ID, addr.clone(), 1), + Err(Module { index: 10, error: 2 }), + ); + + // Instantiate a contract without balance for deposit. + let addr = instantiate(CONTRACT, 100, vec![1]); + // No balance to pay the deposit. + assert_eq!( + create(addr.clone(), ASSET_ID, addr.clone(), 1), + Err(Module { index: 10, error: 2 }), + ); + + // Instantiate a contract with enough balance. + let addr = instantiate(CONTRACT, INIT_VALUE, vec![2]); + assert_eq!(create(addr.clone(), ASSET_ID, BOB, 0), Err(Module { index: 52, error: 7 }),); + // The minimal balance for an asset must be non zero. + assert_eq!(create(addr.clone(), ASSET_ID, BOB, 0), Err(Module { index: 52, error: 7 }),); + // Create asset successfully. + assert_ok!(create(addr.clone(), ASSET_ID, BOB, 1)); + // Asset ID is already taken. + assert_eq!(create(addr.clone(), ASSET_ID, BOB, 1), Err(Module { index: 52, error: 5 }),); + }); +} + +// Testing a contract that creates an asset in the constructor. +#[test] +fn instantiate_and_create_fungible_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let contract = + "contracts/create_token_in_constructor/target/ink/create_token_in_constructor.wasm"; + // Asset already exists. + create_asset(ALICE, 0, 1); + assert_eq!( + instantiate_and_create_fungible(contract, 0, 1), + Err(Module { index: 52, error: 5 }) + ); + // Successfully create an asset when instantiating the contract. + assert_ok!(instantiate_and_create_fungible(contract, ASSET_ID, 1)); + assert!(Assets::asset_exists(ASSET_ID)); + }); +} + +#[test] +fn start_destroy_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![2]); + + // Asset does not exist. + assert_eq!(start_destroy(addr.clone(), ASSET_ID), Err(Module { index: 52, error: 3 }),); + // Create assets where contract is not the owner. + let asset = create_asset(ALICE, 0, 1); + // No Permission. + assert_eq!(start_destroy(addr.clone(), asset), Err(Module { index: 52, error: 2 }),); + let asset = create_asset(addr.clone(), ASSET_ID, 1); + assert_ok!(start_destroy(addr.clone(), asset)); + }); +} + +#[test] +fn set_metadata_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let name = vec![42]; + let symbol = vec![42]; + let decimals = 42u8; + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + + // Asset does not exist. + assert_eq!( + set_metadata(addr.clone(), ASSET_ID, vec![0], vec![0], 0u8), + Err(Module { index: 52, error: 3 }), + ); + // Create assets where contract is not the owner. + let asset = create_asset(ALICE, 0, 1); + // No Permission. + assert_eq!( + set_metadata(addr.clone(), asset, vec![0], vec![0], 0u8), + Err(Module { index: 52, error: 2 }), + ); + let asset = create_asset(addr.clone(), ASSET_ID, 1); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(addr.clone(), asset); + assert_eq!( + set_metadata(addr.clone(), ASSET_ID, vec![0], vec![0], 0u8), + Err(Module { index: 52, error: 16 }), + ); + thaw_asset(addr.clone(), asset); + // TODO: calling the below with a vector of length `100_000` errors in pallet contracts + // `OutputBufferTooSmall. Added to security analysis issue #131 to revisit. + // Set bad metadata - too large values. + assert_eq!( + set_metadata(addr.clone(), ASSET_ID, vec![0; 1000], vec![0; 1000], 0u8), + Err(Module { index: 52, error: 9 }), + ); + // Set metadata successfully. + assert_ok!(set_metadata(addr.clone(), ASSET_ID, name, symbol, decimals)); + // Asset is not live, i.e. frozen or being destroyed. + start_destroy_asset(addr.clone(), asset); + assert_eq!( + set_metadata(addr.clone(), ASSET_ID, vec![0], vec![0], 0), + Err(Module { index: 52, error: 16 }), + ); + }); +} + +#[test] +fn clear_metadata_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let name = vec![42]; + let symbol = vec![42]; + let decimals = 42u8; + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + + // Asset does not exist. + assert_eq!(clear_metadata(addr.clone(), 0), Err(Module { index: 52, error: 3 }),); + // Create assets where contract is not the owner. + let asset = create_asset_and_set_metadata(ALICE, 0, vec![0], vec![0], 0); + // No Permission. + assert_eq!(clear_metadata(addr.clone(), asset), Err(Module { index: 52, error: 2 }),); + let asset = create_asset(addr.clone(), ASSET_ID, 1); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(addr.clone(), asset); + assert_eq!(clear_metadata(addr.clone(), asset), Err(Module { index: 52, error: 16 }),); + thaw_asset(addr.clone(), asset); + // No metadata set. + assert_eq!(clear_metadata(addr.clone(), asset), Err(Module { index: 52, error: 3 }),); + set_metadata_asset(addr.clone(), asset, name, symbol, decimals); + // Clear metadata successfully. + assert_ok!(clear_metadata(addr.clone(), ASSET_ID)); + // Asset is not live, i.e. frozen or being destroyed. + start_destroy_asset(addr.clone(), asset); + assert_eq!( + set_metadata(addr.clone(), ASSET_ID, vec![0], vec![0], decimals), + Err(Module { index: 52, error: 16 }), + ); + }); +} + +#[test] +fn asset_exists_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + + // No tokens in circulation. + assert_eq!(asset_exists(addr.clone(), ASSET_ID), Ok(Assets::asset_exists(ASSET_ID))); + + // Tokens in circulation. + create_asset(addr.clone(), ASSET_ID, 1); + assert_eq!(asset_exists(addr.clone(), ASSET_ID), Ok(Assets::asset_exists(ASSET_ID))); + }); +} + +#[test] +fn mint_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + let amount: Balance = 100 * UNIT; + + // Asset does not exist. + assert_eq!(mint(addr.clone(), 1, BOB, amount), Err(Token(UnknownAsset))); + let asset = create_asset(ALICE, 1, 1); + // Minting can only be done by the owner. + assert_eq!(mint(addr.clone(), asset, BOB, 1), Err(Module { index: 52, error: 2 })); + let asset = create_asset(addr.clone(), 2, 2); + // Minimum balance of an asset can not be zero. + assert_eq!(mint(addr.clone(), asset, BOB, 1), Err(Token(BelowMinimum))); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(addr.clone(), asset); + assert_eq!(mint(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 16 })); + thaw_asset(addr.clone(), asset); + // Successful mint. + let balance_before_mint = Assets::balance(asset, &BOB); + assert_ok!(mint(addr.clone(), asset, BOB, amount)); + let balance_after_mint = Assets::balance(asset, &BOB); + assert_eq!(balance_after_mint, balance_before_mint + amount); + // Account can not hold more tokens than Balance::MAX. + assert_eq!(mint(addr.clone(), asset, BOB, Balance::MAX,), Err(Arithmetic(Overflow))); + // Asset is not live, i.e. frozen or being destroyed. + start_destroy_asset(addr.clone(), asset); + assert_eq!(mint(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 16 })); + }); +} + +#[test] +fn burn_works() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); + let amount: Balance = 100 * UNIT; + + // Asset does not exist. + assert_eq!(burn(addr.clone(), 1, BOB, amount), Err(Module { index: 52, error: 3 })); + let asset = create_asset(ALICE, 1, 1); + // Bob has no tokens and thus pallet assets doesn't know the account. + assert_eq!(burn(addr.clone(), asset, BOB, 1), Err(Module { index: 52, error: 1 })); + // Burning can only be done by the manager. + mint_asset(ALICE, asset, BOB, amount); + assert_eq!(burn(addr.clone(), asset, BOB, 1), Err(Module { index: 52, error: 2 })); + let asset = create_asset_and_mint_to(addr.clone(), 2, BOB, amount); + // Asset is not live, i.e. frozen or being destroyed. + freeze_asset(addr.clone(), asset); + assert_eq!(burn(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 16 })); + thaw_asset(addr.clone(), asset); + // Successful mint. + let balance_before_burn = Assets::balance(asset, &BOB); + assert_ok!(burn(addr.clone(), asset, BOB, amount)); + let balance_after_burn = Assets::balance(asset, &BOB); + assert_eq!(balance_after_burn, balance_before_burn - amount); + // Asset is not live, i.e. frozen or being destroyed. + start_destroy_asset(addr.clone(), asset); + assert_eq!(burn(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 16 })); + }); +} diff --git a/pop-api/integration-tests/src/fungibles/utils.rs b/pop-api/integration-tests/src/fungibles/utils.rs new file mode 100644 index 00000000..2f9be65c --- /dev/null +++ b/pop-api/integration-tests/src/fungibles/utils.rs @@ -0,0 +1,360 @@ +use super::*; + +pub(super) fn decoded(result: ExecReturnValue) -> Result { + ::decode(&mut &result.data[1..]).map_err(|_| result) +} + +pub(super) fn total_supply(addr: AccountId32, asset_id: AssetId) -> Result { + let function = function_selector("total_supply"); + let params = [function, asset_id.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn balance_of( + addr: AccountId32, + asset_id: AssetId, + owner: AccountId32, +) -> Result { + let function = function_selector("balance_of"); + let params = [function, asset_id.encode(), owner.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn allowance( + addr: AccountId32, + asset_id: AssetId, + owner: AccountId32, + spender: AccountId32, +) -> Result { + let function = function_selector("allowance"); + let params = [function, asset_id.encode(), owner.encode(), spender.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn token_name(addr: AccountId32, asset_id: AssetId) -> Result, Error> { + let function = function_selector("token_name"); + let params = [function, asset_id.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::, Error>>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn token_symbol(addr: AccountId32, asset_id: AssetId) -> Result, Error> { + let function = function_selector("token_symbol"); + let params = [function, asset_id.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::, Error>>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn token_decimals(addr: AccountId32, asset_id: AssetId) -> Result { + let function = function_selector("token_decimals"); + let params = [function, asset_id.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn asset_exists(addr: AccountId32, asset_id: AssetId) -> Result { + let function = function_selector("asset_exists"); + let params = [function, asset_id.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn transfer( + addr: AccountId32, + asset_id: AssetId, + to: AccountId32, + value: Balance, +) -> Result<(), Error> { + let function = function_selector("transfer"); + let params = [function, asset_id.encode(), to.encode(), value.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn transfer_from( + addr: AccountId32, + asset_id: AssetId, + from: AccountId32, + to: AccountId32, + value: Balance, +) -> Result<(), Error> { + let function = function_selector("transfer_from"); + let data: Vec = vec![]; + let params = + [function, asset_id.encode(), from.encode(), to.encode(), value.encode(), data.encode()] + .concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn approve( + addr: AccountId32, + asset_id: AssetId, + spender: AccountId32, + value: Balance, +) -> Result<(), Error> { + let function = function_selector("approve"); + let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn increase_allowance( + addr: AccountId32, + asset_id: AssetId, + spender: AccountId32, + value: Balance, +) -> Result<(), Error> { + let function = function_selector("increase_allowance"); + let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn decrease_allowance( + addr: AccountId32, + asset_id: AssetId, + spender: AccountId32, + value: Balance, +) -> Result<(), Error> { + let function = function_selector("decrease_allowance"); + let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn create( + addr: AccountId32, + asset_id: AssetId, + admin: AccountId32, + min_balance: Balance, +) -> Result<(), Error> { + let function = function_selector("create"); + let params = [function, asset_id.encode(), admin.encode(), min_balance.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn start_destroy(addr: AccountId32, asset_id: AssetId) -> Result<(), Error> { + let function = function_selector("start_destroy"); + let params = [function, asset_id.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + match decoded::>(result) { + Ok(x) => x, + Err(result) => panic!("Contract reverted: {:?}", result), + } +} + +pub(super) fn set_metadata( + addr: AccountId32, + asset_id: AssetId, + name: Vec, + symbol: Vec, + decimals: u8, +) -> Result<(), Error> { + let function = function_selector("set_metadata"); + let params = + [function, asset_id.encode(), name.encode(), symbol.encode(), decimals.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn clear_metadata(addr: AccountId32, asset_id: AssetId) -> Result<(), Error> { + let function = function_selector("clear_metadata"); + let params = [function, asset_id.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn mint( + addr: AccountId32, + asset_id: AssetId, + account: AccountId32, + amount: Balance, +) -> Result<(), Error> { + let function = function_selector("mint"); + let params = [function, asset_id.encode(), account.encode(), amount.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn burn( + addr: AccountId32, + asset_id: AssetId, + account: AccountId32, + amount: Balance, +) -> Result<(), Error> { + let function = function_selector("burn"); + let params = [function, asset_id.encode(), account.encode(), amount.encode()].concat(); + let result = bare_call(addr, params, 0).expect("should work"); + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} + +pub(super) fn create_asset(owner: AccountId32, asset_id: AssetId, min_balance: Balance) -> AssetId { + assert_ok!(Assets::create( + RuntimeOrigin::signed(owner.clone()), + asset_id.into(), + owner.into(), + min_balance + )); + asset_id +} + +pub(super) fn mint_asset( + owner: AccountId32, + asset_id: AssetId, + to: AccountId32, + value: Balance, +) -> AssetId { + assert_ok!(Assets::mint( + RuntimeOrigin::signed(owner.clone()), + asset_id.into(), + to.into(), + value + )); + asset_id +} + +pub(super) fn create_asset_and_mint_to( + owner: AccountId32, + asset_id: AssetId, + to: AccountId32, + value: Balance, +) -> AssetId { + create_asset(owner.clone(), asset_id, 1); + mint_asset(owner, asset_id, to, value) +} + +// Create an asset, mints to, and approves spender. +pub(super) fn create_asset_mint_and_approve( + owner: AccountId32, + asset_id: AssetId, + to: AccountId32, + mint: Balance, + spender: AccountId32, + approve: Balance, +) -> AssetId { + create_asset_and_mint_to(owner.clone(), asset_id, to.clone(), mint); + assert_ok!(Assets::approve_transfer( + RuntimeOrigin::signed(to.into()), + asset_id.into(), + spender.into(), + approve, + )); + asset_id +} + +// Freeze an asset. +pub(super) fn freeze_asset(owner: AccountId32, asset_id: AssetId) { + assert_ok!(Assets::freeze_asset(RuntimeOrigin::signed(owner.into()), asset_id.into())); +} + +// Thaw an asset. +pub(super) fn thaw_asset(owner: AccountId32, asset_id: AssetId) { + assert_ok!(Assets::thaw_asset(RuntimeOrigin::signed(owner.into()), asset_id.into())); +} + +// Start destroying an asset. +pub(super) fn start_destroy_asset(owner: AccountId32, asset_id: AssetId) { + assert_ok!(Assets::start_destroy(RuntimeOrigin::signed(owner.into()), asset_id.into())); +} + +// Create an asset and set metadata. +pub(super) fn create_asset_and_set_metadata( + owner: AccountId32, + asset_id: AssetId, + name: Vec, + symbol: Vec, + decimals: u8, +) -> AssetId { + assert_ok!(Assets::create( + RuntimeOrigin::signed(owner.clone()), + asset_id.into(), + owner.clone().into(), + 100 + )); + set_metadata_asset(owner, asset_id, name, symbol, decimals); + asset_id +} + +// Set metadata of an asset. +pub(super) fn set_metadata_asset( + owner: AccountId32, + asset_id: AssetId, + name: Vec, + symbol: Vec, + decimals: u8, +) { + assert_ok!(Assets::set_metadata( + RuntimeOrigin::signed(owner.into()), + asset_id.into(), + name, + symbol, + decimals + )); +} + +pub(super) fn token_name_asset(asset_id: AssetId) -> Vec { + as MetadataInspect>::name( + asset_id, + ) +} + +pub(super) fn token_symbol_asset(asset_id: AssetId) -> Vec { + as MetadataInspect>::symbol( + asset_id, + ) +} + +pub(super) fn token_decimals_asset(asset_id: AssetId) -> u8 { + as MetadataInspect>::decimals( + asset_id, + ) +} + +pub(super) fn instantiate_and_create_fungible( + contract: &str, + asset_id: AssetId, + min_balance: Balance, +) -> Result<(), Error> { + let function = function_selector("new"); + let input = [function, asset_id.encode(), min_balance.encode()].concat(); + let (wasm_binary, _) = + load_wasm_module::(contract).expect("could not read .wasm file"); + let result = Contracts::bare_instantiate( + ALICE, + INIT_VALUE, + GAS_LIMIT, + None, + Code::Upload(wasm_binary), + input, + vec![], + DEBUG_OUTPUT, + CollectEvents::Skip, + ) + .result + .expect("should work") + .result; + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) +} diff --git a/pop-api/integration-tests/src/lib.rs b/pop-api/integration-tests/src/lib.rs index ea5ccb05..b728fca6 100644 --- a/pop-api/integration-tests/src/lib.rs +++ b/pop-api/integration-tests/src/lib.rs @@ -16,7 +16,7 @@ use pop_runtime_devnet::{ UNIT, }; -mod local_fungibles; +mod fungibles; type AssetId = u32; type Balance = u128; diff --git a/pop-api/integration-tests/src/local_fungibles.rs b/pop-api/integration-tests/src/local_fungibles.rs deleted file mode 100644 index 15a644ad..00000000 --- a/pop-api/integration-tests/src/local_fungibles.rs +++ /dev/null @@ -1,738 +0,0 @@ -use super::*; -use pop_primitives::error::{ - ArithmeticError::*, - Error::{self, *}, - TokenError::*, -}; - -const ASSET_ID: AssetId = 1; -const CONTRACT: &str = "contracts/fungibles/target/ink/fungibles.wasm"; - -fn decoded(result: ExecReturnValue) -> Result { - ::decode(&mut &result.data[2..]) - .map_err(|_| format!("\nTest failed by trying to decode `{:?}` into `T`\n", result)) -} - -// Call total_supply contract message. -fn total_supply(addr: AccountId32, asset_id: AssetId) -> Balance { - let function = function_selector("total_supply"); - let params = [function, asset_id.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); - decoded::(result).unwrap() -} - -// Call balance_of contract message. -fn balance_of(addr: AccountId32, asset_id: AssetId, owner: AccountId32) -> Balance { - let function = function_selector("balance_of"); - let params = [function, asset_id.encode(), owner.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); - decoded::(result).unwrap() -} - -// Call allowance contract message. -fn allowance( - addr: AccountId32, - asset_id: AssetId, - owner: AccountId32, - spender: AccountId32, -) -> Balance { - let function = function_selector("allowance"); - let params = [function, asset_id.encode(), owner.encode(), spender.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); - decoded::(result).unwrap() -} - -// Call token_name contract message. -fn token_name(addr: AccountId32, asset_id: AssetId) -> Vec { - let function = function_selector("token_name"); - let params = [function, asset_id.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); - decoded::>(result).unwrap() -} - -// Call token_symbol contract message. -fn token_symbol(addr: AccountId32, asset_id: AssetId) -> Vec { - let function = function_selector("token_symbol"); - let params = [function, asset_id.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); - decoded::>(result).unwrap() -} - -// Call token_decimals contract message. -fn token_decimals(addr: AccountId32, asset_id: AssetId) -> u8 { - let function = function_selector("token_decimals"); - let params = [function, asset_id.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); - decoded::(result).unwrap() -} - -fn transfer( - addr: AccountId32, - asset_id: AssetId, - to: AccountId32, - value: Balance, -) -> ExecReturnValue { - let function = function_selector("transfer"); - let params = [function, asset_id.encode(), to.encode(), value.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); - result -} - -fn transfer_from( - addr: AccountId32, - asset_id: AssetId, - from: AccountId32, - to: AccountId32, - value: Balance, -) -> ExecReturnValue { - let function = function_selector("transfer_from"); - let data: Vec = vec![]; - let params = - [function, asset_id.encode(), from.encode(), to.encode(), value.encode(), data.encode()] - .concat(); - let result = bare_call(addr, params, 0).expect("should work"); - result -} - -fn approve( - addr: AccountId32, - asset_id: AssetId, - spender: AccountId32, - value: Balance, -) -> ExecReturnValue { - let function = function_selector("approve"); - let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); - result -} - -fn increase_allowance( - addr: AccountId32, - asset_id: AssetId, - spender: AccountId32, - value: Balance, -) -> ExecReturnValue { - let function = function_selector("increase_allowance"); - let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); - result -} - -fn decrease_allowance( - addr: AccountId32, - asset_id: AssetId, - spender: AccountId32, - value: Balance, -) -> ExecReturnValue { - let function = function_selector("decrease_allowance"); - let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); - result -} - -// fn asset_exists(addr: AccountId32, asset_id: AssetId) -> bool { -// let function = function_selector("asset_exists"); -// let params = [function, asset_id.encode()].concat(); -// let result = bare_call(addr, params, 0).expect("should work"); -// decoded::(result) -// } -// -// fn create( -// addr: AccountId32, -// asset_id: AssetId, -// admin: AccountId32, -// min_balance: Balance, -// ) -> ExecReturnValue { -// let function = function_selector("create"); -// let params = [function, asset_id.encode(), admin.encode(), min_balance.encode()].concat(); -// bare_call(addr, params, 0).expect("should work") -// } -// -// fn set_metadata( -// addr: AccountId32, -// asset_id: AssetId, -// name: Vec, -// symbol: Vec, -// decimals: u8, -// ) -> ExecReturnValue { -// let function = function_selector("set_metadata"); -// let params = -// [function, asset_id.encode(), name.encode(), symbol.encode(), decimals.encode()].concat(); -// bare_call(addr, params, 0).expect("should work") -// } - -fn create_asset(owner: AccountId32, asset_id: AssetId, min_balance: Balance) -> AssetId { - assert_ok!(Assets::create( - RuntimeOrigin::signed(owner.clone()), - asset_id.into(), - owner.into(), - min_balance - )); - asset_id -} - -fn mint_asset(owner: AccountId32, asset_id: AssetId, to: AccountId32, value: Balance) -> AssetId { - assert_ok!(Assets::mint( - RuntimeOrigin::signed(owner.clone()), - asset_id.into(), - to.into(), - value - )); - asset_id -} - -fn create_asset_and_mint_to( - owner: AccountId32, - asset_id: AssetId, - to: AccountId32, - value: Balance, -) -> AssetId { - create_asset(owner.clone(), asset_id, 1); - mint_asset(owner, asset_id, to, value) -} - -// Create an asset, mints to, and approves spender. -fn create_asset_mint_and_approve( - owner: AccountId32, - asset_id: AssetId, - to: AccountId32, - mint: Balance, - spender: AccountId32, - approve: Balance, -) -> AssetId { - let asset = create_asset_and_mint_to(owner.clone(), asset_id, to.clone(), mint); - assert_ok!(Assets::approve_transfer( - RuntimeOrigin::signed(to.into()), - asset_id.into(), - spender.into(), - approve, - )); - asset -} - -// Freeze an asset. -fn freeze_asset(owner: AccountId32, asset_id: AssetId) { - assert_ok!(Assets::freeze_asset(RuntimeOrigin::signed(owner.into()), asset_id.into())); -} - -// Thaw an asset. -fn thaw_asset(owner: AccountId32, asset_id: AssetId) { - assert_ok!(Assets::thaw_asset(RuntimeOrigin::signed(owner.into()), asset_id.into())); -} - -// Start destroying an asset. -fn start_destroy_asset(owner: AccountId32, asset_id: AssetId) { - assert_ok!(Assets::start_destroy(RuntimeOrigin::signed(owner.into()), asset_id.into())); -} - -// Create an asset and set metadata. -fn create_asset_and_set_metadata( - owner: AccountId32, - asset_id: AssetId, - name: Vec, - symbol: Vec, - decimals: u8, -) { - assert_ok!(Assets::create( - RuntimeOrigin::signed(owner.clone()), - asset_id.into(), - owner.clone().into(), - 100 - )); - set_metadata_asset(owner, asset_id, name, symbol, decimals); -} - -// Set metadata of an asset. -fn set_metadata_asset( - owner: AccountId32, - asset_id: AssetId, - name: Vec, - symbol: Vec, - decimals: u8, -) { - assert_ok!(Assets::set_metadata( - RuntimeOrigin::signed(owner.into()), - asset_id.into(), - name, - symbol, - decimals - )); -} - -fn token_name_asset(asset_id: AssetId) -> Vec { - as MetadataInspect>::name( - asset_id, - ) -} - -fn token_symbol_asset(asset_id: AssetId) -> Vec { - as MetadataInspect>::symbol( - asset_id, - ) -} - -fn token_decimals_asset(asset_id: AssetId) -> u8 { - as MetadataInspect>::decimals( - asset_id, - ) -} - -/// 1. PSP-22 Interface: -/// - total_supply -/// - balance_of -/// - allowance -/// - transfer -/// - transfer_from -/// - approve -/// - increase_allowance -/// - decrease_allowance - -#[test] -fn total_supply_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); - - // No tokens in circulation. - assert_eq!(Assets::total_supply(ASSET_ID), total_supply(addr.clone(), ASSET_ID)); - assert_eq!(0, total_supply(addr.clone(), ASSET_ID)); - - // Tokens in circulation. - create_asset_and_mint_to(addr.clone(), ASSET_ID, BOB, 100); - assert_eq!(Assets::total_supply(ASSET_ID), total_supply(addr.clone(), ASSET_ID)); - assert_eq!(100, total_supply(addr, ASSET_ID)); - }); -} - -#[test] -fn balance_of_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); - - // No tokens in circulation. - assert_eq!(Assets::balance(ASSET_ID, BOB), balance_of(addr.clone(), ASSET_ID, BOB)); - assert_eq!(0, balance_of(addr.clone(), ASSET_ID, BOB)); - - // Tokens in circulation. - create_asset_and_mint_to(addr.clone(), ASSET_ID, BOB, 100); - assert_eq!(Assets::balance(ASSET_ID, BOB), balance_of(addr.clone(), ASSET_ID, BOB)); - assert_eq!(100, balance_of(addr, ASSET_ID, BOB)); - }); -} - -#[test] -fn allowance_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); - - // No tokens in circulation. - assert_eq!( - Assets::allowance(ASSET_ID, &BOB, &ALICE), - allowance(addr.clone(), ASSET_ID, BOB, ALICE) - ); - assert_eq!(0, allowance(addr.clone(), ASSET_ID, BOB, ALICE)); - - // Tokens in circulation. - create_asset_mint_and_approve(addr.clone(), ASSET_ID, BOB, 100, ALICE, 50); - assert_eq!( - Assets::allowance(ASSET_ID, &BOB, &ALICE), - allowance(addr.clone(), ASSET_ID, BOB, ALICE) - ); - assert_eq!(50, allowance(addr, ASSET_ID, BOB, ALICE)); - }); -} - -#[test] -fn transfer_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); - let amount: Balance = 100 * UNIT; - - // Asset does not exist. - assert_eq!( - decoded::(transfer(addr.clone(), 1, BOB, amount,)), - Ok(Module { index: 52, error: 3 }), - ); - // Create asset with Alice as owner and mint `amount` to contract address. - let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); - // Asset is not live, i.e. frozen or being destroyed. - freeze_asset(ALICE, asset); - assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount,)), - Ok(Module { index: 52, error: 16 }), - ); - thaw_asset(ALICE, asset); - // Not enough balance. - assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount + 1 * UNIT)), - Ok(Module { index: 52, error: 0 }), - ); - // Not enough balance due to ED. - assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount)), - Ok(Module { index: 52, error: 0 }), - ); - // Successful transfer. - let balance_before_transfer = Assets::balance(asset, &BOB); - let result = transfer(addr.clone(), asset, BOB, amount / 2); - assert!(!result.did_revert(), "Contract reverted!"); - let balance_after_transfer = Assets::balance(asset, &BOB); - assert_eq!(balance_after_transfer, balance_before_transfer + amount / 2); - // Transfer asset to account that does not exist. - assert_eq!( - decoded::(transfer(addr.clone(), asset, FERDIE, amount / 4)), - Ok(Token(CannotCreate)) - ); - // Asset is not live, i.e. frozen or being destroyed. - start_destroy_asset(ALICE, asset); - assert_eq!( - decoded::(transfer(addr.clone(), asset, BOB, amount / 4)), - Ok(Module { index: 52, error: 16 }), - ); - }); -} - -#[test] -fn transfer_from_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); - let amount: Balance = 100 * UNIT; - - // Asset does not exist. - assert_eq!( - decoded::(transfer_from(addr.clone(), 1, ALICE, BOB, amount / 2)), - Ok(Module { index: 52, error: 3 }), - ); - // Create asset with Alice as owner and mint `amount` to contract address. - let asset = create_asset_and_mint_to(ALICE, 1, ALICE, amount); - // Unapproved transfer. - assert_eq!( - decoded::(transfer_from(addr.clone(), asset, ALICE, BOB, amount / 2)), - Ok(Module { index: 52, error: 10 }) - ); - assert_ok!(Assets::approve_transfer( - RuntimeOrigin::signed(ALICE.into()), - asset.into(), - addr.clone().into(), - amount + 1 * UNIT, - )); - // Asset is not live, i.e. frozen or being destroyed. - freeze_asset(ALICE, asset); - assert_eq!( - decoded::(transfer_from(addr.clone(), asset, ALICE, BOB, amount)), - Ok(Module { index: 52, error: 16 }), - ); - thaw_asset(ALICE, asset); - // Not enough balance. - assert_eq!( - decoded::(transfer_from(addr.clone(), asset, ALICE, BOB, amount + 1 * UNIT,)), - Ok(Module { index: 52, error: 0 }), - ); - // Successful transfer. - let balance_before_transfer = Assets::balance(asset, &BOB); - let result = transfer_from(addr.clone(), asset, ALICE, BOB, amount / 2); - assert!(!result.did_revert(), "Contract reverted!"); - let balance_after_transfer = Assets::balance(asset, &BOB); - assert_eq!(balance_after_transfer, balance_before_transfer + amount / 2); - }); -} - -#[test] -fn approve_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - let addr = instantiate(CONTRACT, 0, vec![]); - let amount: Balance = 100 * UNIT; - // Asset does not exist. - assert_eq!( - decoded::(approve(addr.clone(), 0, BOB, amount)), - Ok(Module { index: 52, error: 3 }), - ); - let asset = create_asset_and_mint_to(ALICE, 0, addr.clone(), amount); - assert_eq!( - decoded::(approve(addr.clone(), asset, BOB, amount)), - Ok(ConsumerRemaining) - ); - - let addr = instantiate(CONTRACT, INIT_VALUE, vec![1]); - // Create asset with Alice as owner and mint `amount` to contract address. - let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); - // Asset is not live, i.e. frozen or being destroyed. - freeze_asset(ALICE, asset); - assert_eq!( - decoded::(approve(addr.clone(), asset, BOB, amount)), - Ok(Module { index: 52, error: 16 }), - ); - thaw_asset(ALICE, asset); - // Successful approvals: - assert_eq!(0, Assets::allowance(asset, &addr, &BOB)); - assert!(!approve(addr.clone(), asset, BOB, amount).did_revert(), "Contract reverted!"); - assert_eq!(Assets::allowance(asset, &addr, &BOB), amount); - // Non-additive, sets new value. - assert!(!approve(addr.clone(), asset, BOB, amount / 2).did_revert(), "Contract reverted!"); - assert_eq!(Assets::allowance(asset, &addr, &BOB), amount / 2); - // Asset is not live, i.e. frozen or being destroyed. - start_destroy_asset(ALICE, asset); - assert_eq!( - decoded::(approve(addr.clone(), asset, BOB, amount)), - Ok(Module { index: 52, error: 16 }), - ); - }); -} - -#[test] -fn increase_allowance_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - let addr = instantiate(CONTRACT, 0, vec![]); - let amount: Balance = 100 * UNIT; - // Asset does not exist. - assert_eq!( - decoded::(increase_allowance(addr.clone(), 0, BOB, amount)), - Ok(Module { index: 52, error: 3 }), - ); - let asset = create_asset_and_mint_to(ALICE, 0, addr.clone(), amount); - assert_eq!( - decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), - Ok(ConsumerRemaining) - ); - - let addr = instantiate(CONTRACT, INIT_VALUE, vec![1]); - // Create asset with Alice as owner and mint `amount` to contract address. - let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); - // Asset is not live, i.e. frozen or being destroyed. - freeze_asset(ALICE, asset); - assert_eq!( - decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), - Ok(Module { index: 52, error: 16 }), - ); - thaw_asset(ALICE, asset); - // Successful approvals: - assert_eq!(0, Assets::allowance(asset, &addr, &BOB)); - assert!( - !increase_allowance(addr.clone(), asset, BOB, amount).did_revert(), - "Contract reverted!" - ); - assert_eq!(Assets::allowance(asset, &addr, &BOB), amount); - // Additive. - assert!( - !increase_allowance(addr.clone(), asset, BOB, amount).did_revert(), - "Contract reverted!" - ); - assert_eq!(Assets::allowance(asset, &addr, &BOB), amount * 2); - // Asset is not live, i.e. frozen or being destroyed. - start_destroy_asset(ALICE, asset); - assert_eq!( - decoded::(increase_allowance(addr.clone(), asset, BOB, amount)), - Ok(Module { index: 52, error: 16 }), - ); - }); -} - -#[test] -fn decrease_allowance_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); - let amount: Balance = 100 * UNIT; - // Asset does not exist. - assert_eq!( - decoded::(decrease_allowance(addr.clone(), 0, BOB, amount)), - Ok(Module { index: 52, error: 3 }), - ); - // Create asset and mint to the address contract, delegate Bob to spend the `amount`. - let asset = - create_asset_mint_and_approve(addr.clone(), 0, addr.clone(), amount, BOB, amount); - // Asset is not live, i.e. frozen or being destroyed. - freeze_asset(addr.clone(), asset); - assert_eq!( - decoded::(decrease_allowance(addr.clone(), asset, BOB, amount)), - Ok(Module { index: 52, error: 16 }), - ); - thaw_asset(addr.clone(), asset); - // Successfully decrease allowance. - let bob_allowance_before = Assets::allowance(asset, &addr, &BOB); - let result = decrease_allowance(addr.clone(), 0, BOB, amount / 2 - 1 * UNIT); - assert!(!result.did_revert(), "Contract reverted!"); - let bob_allowance_after = Assets::allowance(asset, &addr, &BOB); - assert_eq!(bob_allowance_before - bob_allowance_after, amount / 2 - 1 * UNIT); - // Asset is not live, i.e. frozen or being destroyed. - start_destroy_asset(addr.clone(), asset); - assert_eq!( - decoded::(decrease_allowance(addr.clone(), asset, BOB, amount)), - Ok(Module { index: 52, error: 16 }), - ); - }); -} - -/// 2. PSP-22 Metadata Interface: -/// - token_name -/// - token_symbol -/// - token_decimals - -#[test] -fn token_metadata_works() { - new_test_ext().execute_with(|| { - let _ = env_logger::try_init(); - let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); - - let name: Vec = vec![11, 12, 13]; - let symbol: Vec = vec![21, 22, 23]; - let decimals: u8 = 69; - // Token does not exist. - assert_eq!(token_name_asset(ASSET_ID), token_name(addr.clone(), ASSET_ID)); - assert_eq!(Vec::::new(), token_name(addr.clone(), ASSET_ID)); - assert_eq!(token_symbol_asset(ASSET_ID), token_symbol(addr.clone(), ASSET_ID)); - assert_eq!(Vec::::new(), token_symbol(addr.clone(), ASSET_ID)); - assert_eq!(token_decimals_asset(ASSET_ID), token_decimals(addr.clone(), ASSET_ID)); - assert_eq!(0, token_decimals(addr.clone(), ASSET_ID)); - - create_asset_and_set_metadata( - addr.clone(), - ASSET_ID, - name.clone(), - symbol.clone(), - decimals, - ); - assert_eq!(token_name_asset(ASSET_ID), token_name(addr.clone(), ASSET_ID)); - assert_eq!(name, token_name(addr.clone(), ASSET_ID)); - assert_eq!(token_symbol_asset(ASSET_ID), token_symbol(addr.clone(), ASSET_ID)); - assert_eq!(symbol, token_symbol(addr.clone(), ASSET_ID)); - assert_eq!(token_decimals_asset(ASSET_ID), token_decimals(addr.clone(), ASSET_ID)); - assert_eq!(decimals, token_decimals(addr.clone(), ASSET_ID)); - }); -} - -// #[test] -// #[ignore] -// fn asset_exists_works() { -// new_test_ext().execute_with(|| { -// let _ = env_logger::try_init(); -// let addr = -// instantiate(CONTRACT, INIT_VALUE, vec![]); -// -// // No tokens in circulation. -// assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr.clone(), ASSET_ID)); -// -// // Tokens in circulation. -// create_asset(addr.clone(), ASSET_ID, 1); -// assert_eq!(Assets::asset_exists(ASSET_ID), asset_exists(addr, ASSET_ID)); -// }); -// } - -// #[test] -// #[ignore] -// fn mint_works() { -// new_test_ext().execute_with(|| { -// let _ = env_logger::try_init(); -// let addr = -// instantiate(CONTRACT, INIT_VALUE, vec![]); -// let amount: Balance = 100 * UNIT; -// -// // Asset does not exist. -// assert_eq!( -// decoded::(transfer_from(addr.clone(), 1, None, Some(BOB), amount, &[0u8])), -// Token(UnknownAsset) -// ); -// let asset = create_asset(ALICE, 1, 2); -// // Minting can only be done by the owner. -// assert_eq!( -// decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), -// Ok(Module { index: 52, error: 2 }), -// ); -// // Minimum balance of an asset can not be zero. -// assert_eq!( -// decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), 1, &[0u8])), -// Token(BelowMinimum) -// ); -// let asset = create_asset(addr.clone(), 2, 2); -// // Asset is not live, i.e. frozen or being destroyed. -// freeze_asset(addr.clone(), asset); -// assert_eq!( -// decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), -// Ok(Module { index: 52, error: 16 }), -// ); -// thaw_asset(addr.clone(), asset); -// // Successful mint. -// let balance_before_mint = Assets::balance(asset, &BOB); -// let result = transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8]); -// assert!(!result.did_revert(), "Contract reverted!"); -// let balance_after_mint = Assets::balance(asset, &BOB); -// assert_eq!(balance_after_mint, balance_before_mint + amount); -// // Can not mint more tokens than Balance::MAX. -// assert_eq!( -// decoded::(transfer_from( -// addr.clone(), -// asset, -// None, -// Some(BOB), -// Balance::MAX, -// &[0u8] -// )), -// Arithmetic(Overflow) -// ); -// // Asset is not live, i.e. frozen or being destroyed. -// start_destroy_asset(addr.clone(), asset); -// assert_eq!( -// decoded::(transfer_from(addr.clone(), asset, None, Some(BOB), amount, &[0u8])), -// Ok(Module { index: 52, error: 16 }), -// ); -// }); -// } - -// #[test] -// #[ignore] -// fn create_works() { -// new_test_ext().execute_with(|| { -// let _ = env_logger::try_init(); -// // Instantiate a contract without balance (relay token). -// let addr = instantiate(CONTRACT, 0, vec![0]); -// // No balance to pay for fees. -// assert_eq!( -// decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), -// Ok(Module { index: 10, error: 2 }), -// ); -// // Instantiate a contract without balance (relay token). -// let addr = instantiate(CONTRACT, 100, vec![2]); -// // No balance to pay the deposit. -// assert_eq!( -// decoded::(create(addr.clone(), ASSET_ID, addr.clone(), 1)), -// Ok(Module { index: 10, error: 2 }), -// ); -// // Instantiate a contract with balance. -// let addr = -// instantiate(CONTRACT, INIT_VALUE, vec![1]); -// assert_eq!( -// decoded::(create(addr.clone(), ASSET_ID, BOB, 0)), -// Ok(Module { index: 52, error: 7 }), -// ); -// create_asset(ALICE, ASSET_ID, 1); -// // Asset ID is already taken. -// assert_eq!( -// decoded::(create(addr.clone(), ASSET_ID, BOB, 1)), -// Ok(Module { index: 52, error: 5 }), -// ); -// // The minimal balance for an asset must be non zero. -// let new_asset = 2; -// let result = create(addr.clone(), new_asset, BOB, 1); -// assert!(!result.did_revert(), "Contract reverted!"); -// }); -// } - -// #[test] -// #[ignore] -// fn set_metadata_works() { -// new_test_ext().execute_with(|| { -// let _ = env_logger::try_init(); -// let addr = -// instantiate(CONTRACT, INIT_VALUE, vec![]); -// -// create_asset(addr.clone(), ASSET_ID, 1); -// let result = set_metadata(addr.clone(), ASSET_ID, vec![12], vec![12], 12); -// assert!(!result.did_revert(), "Contract reverted!"); -// }); -// } diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 7fcfc85b..78a79f80 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -30,10 +30,10 @@ mod constants { /// Helper method to build `ChainExtensionMethod`. /// /// Parameters: -/// - 'version': The version of the chain extension -/// - 'function': The ID of the function -/// - 'module': The index of the runtime module -/// - 'dispatchable': The index of the module dispatchable functions +/// - 'version': The version of the chain extension. +/// - 'function': The ID of the function. +/// - 'module': The index of the runtime module. +/// - 'dispatchable': The index of the module dispatchable functions. fn build_extension_method( version: u8, function: u8, @@ -45,13 +45,13 @@ fn build_extension_method( /// Represents a status code returned by the runtime. /// -/// `StatusCode` encapsulates a `u32` value that indicates the status of an operation performed -/// by the runtime. It helps to communicate the success or failure of a Pop API call to the contract, +/// `StatusCode` encapsulates a `u32` value that indicates the status of an operation performed by +/// the runtime. It helps to communicate the success or failure of a Pop API call to the contract, /// providing a standardized way to handle errors. /// -/// This status code can be used to determine if an operation succeeded or if it encountered -/// an error. A `StatusCode` of `0` typically indicates success, while any other value represents -/// an error. +/// This status code can be used to determine if an operation succeeded or if it encountered an +/// error. A `StatusCode` of `0` typically indicates success, while any other value represents an +/// error. #[derive(Debug, Copy, Clone, PartialEq, Eq)] #[ink::scale_derive(Encode, Decode, TypeInfo)] pub struct StatusCode(pub u32); diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index 0712a80b..8600c439 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -3,22 +3,23 @@ use crate::{ primitives::{AccountId, AssetId, Balance}, Result, StatusCode, }; +pub use asset_management::*; use constants::*; use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec}; pub use metadata::*; -/// Helper method to build a dispatch call `ChainExtensionMethod` for fungibles `v0` +/// Helper method to build a dispatch call `ChainExtensionMethod` for fungibles `v0`. /// /// Parameters: -/// - 'dispatchable': The index of the module dispatchable functions +/// - 'dispatchable': The index of the module dispatchable functions. fn build_dispatch(dispatchable: u8) -> ChainExtensionMethod<(), (), (), false> { crate::v0::build_dispatch(FUNGIBLES, dispatchable) } -/// Helper method to build a dispatch call `ChainExtensionMethod` for fungibles `v0`` +/// Helper method to build a dispatch call `ChainExtensionMethod` for fungibles `v0`. /// /// Parameters: -/// - 'state_query': The index of the runtime state query +/// - 'state_query': The index of the runtime state query. fn build_read_state(state_query: u8) -> ChainExtensionMethod<(), (), (), false> { crate::v0::build_read_state(FUNGIBLES, state_query) } @@ -27,48 +28,40 @@ fn build_read_state(state_query: u8) -> ChainExtensionMethod<(), (), (), false> /// 1. PSP-22 Interface /// 2. PSP-22 Metadata Interface /// 3. Asset Management +/// 4. PSP-22 Mintable & Burnable Interface mod constants { /// 1. PSP-22 Interface: - /// - total_supply - pub const TOTAL_SUPPLY: u8 = 0; - /// - balance_of - pub const BALANCE_OF: u8 = 1; - /// - allowance - pub const ALLOWANCE: u8 = 2; - /// - transfer + pub(super) const TOTAL_SUPPLY: u8 = 0; + pub(super) const BALANCE_OF: u8 = 1; + pub(super) const ALLOWANCE: u8 = 2; pub(super) const TRANSFER: u8 = 3; - /// - transfer_from pub(super) const TRANSFER_FROM: u8 = 4; - /// - approve pub(super) const APPROVE: u8 = 5; - /// - increase_allowance pub(super) const INCREASE_ALLOWANCE: u8 = 6; - /// - decrease_allowance pub(super) const DECREASE_ALLOWANCE: u8 = 7; /// 2. PSP-22 Metadata Interface: - /// - token_name - pub const TOKEN_NAME: u8 = 8; - /// - token_symbol - pub const TOKEN_SYMBOL: u8 = 9; - /// - token_decimals - pub const TOKEN_DECIMALS: u8 = 10; - - // 3. Asset Management: - // - create - // - start_destroy - // - destroy_accounts - // - destroy_approvals - // - finish_destroy - // - set_metadata - // - clear_metadata + pub(super) const TOKEN_NAME: u8 = 8; + pub(super) const TOKEN_SYMBOL: u8 = 9; + pub(super) const TOKEN_DECIMALS: u8 = 10; + + /// 3. Asset Management: + pub(super) const CREATE: u8 = 11; + pub(super) const START_DESTROY: u8 = 12; + pub(super) const SET_METADATA: u8 = 16; + pub(super) const CLEAR_METADATA: u8 = 17; + pub(super) const ASSET_EXISTS: u8 = 18; + + /// 4. PSP-22 Mintable & Burnable interface: + pub(super) const MINT: u8 = 19; + pub(super) const BURN: u8 = 20; } /// Returns the total token supply for a given asset ID. /// -/// # Arguments -/// * `id` - The ID of the asset. +/// # Parameters +/// - `id` - The ID of the asset. /// /// # Returns /// The total supply of the token, or an error if the operation fails. @@ -84,9 +77,9 @@ pub fn total_supply(id: AssetId) -> Result { /// Returns the account balance for the specified `owner` for a given asset ID. Returns `0` if /// the account is non-existent. /// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `owner` - The account whose balance is being queried. +/// # Parameters +/// - `id` - The ID of the asset. +/// - `owner` - The account whose balance is being queried. /// /// # Returns /// The balance of the specified account, or an error if the operation fails. @@ -102,10 +95,10 @@ pub fn balance_of(id: AssetId, owner: AccountId) -> Result { /// Returns the amount which `spender` is still allowed to withdraw from `owner` for a given /// asset ID. Returns `0` if no allowance has been set. /// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `owner` - The account that owns the tokens. -/// * `spender` - The account that is allowed to spend the tokens. +/// # Parameters +/// - `id` - The ID of the asset. +/// - `owner` - The account that owns the tokens. +/// - `spender` - The account that is allowed to spend the tokens. /// /// # Returns /// The remaining allowance, or an error if the operation fails. @@ -121,67 +114,66 @@ pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result Result<()> { +pub fn transfer(id: AssetId, to: AccountId, value: Balance) -> Result<()> { build_dispatch(TRANSFER) .input::<(AssetId, AccountId, Balance)>() .output::, true>() .handle_error_code::() - .call(&(id, target, amount)) + .call(&(id, to, value)) } -/// Transfers `value` tokens on behalf of `from` to account `to` with additional `data` -/// in unspecified format. If `from` is equal to `None`, tokens will be minted to account `to`. If -/// `to` is equal to `None`, tokens will be burned from account `from`. +/// Transfers `value` amount tokens on behalf of `from` to account `to` with additional `data` +/// in unspecified format. /// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `from` - The account from which the tokens are transferred. -/// * `to` - The recipient account. -/// * `value` - The number of tokens to transfer. +/// # Parameters +/// - `id` - The ID of the asset. +/// - `from` - The account from which the tokens are transferred. +/// - `to` - The recipient account. +/// - `value` - The number of tokens to transfer. /// /// # Returns /// Returns `Ok(())` if successful, or an error if the transfer fails. #[inline] -pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, amount: Balance) -> Result<()> { +pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, value: Balance) -> Result<()> { build_dispatch(TRANSFER_FROM) .input::<(AssetId, AccountId, AccountId, Balance)>() .output::, true>() .handle_error_code::() - .call(&(id, from, to, amount)) + .call(&(id, from, to, value)) } /// Approves an account to spend a specified number of tokens on behalf of the caller. /// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `spender` - The account that is allowed to spend the tokens. -/// * `value` - The number of tokens to approve. +/// # Parameters +/// - `id` - The ID of the asset. +/// - `spender` - The account that is allowed to spend the tokens. +/// - `value` - The number of tokens to approve. /// /// # Returns /// Returns `Ok(())` if successful, or an error if the approval fails. #[inline] -pub fn approve(id: AssetId, spender: AccountId, amount: Balance) -> Result<()> { +pub fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { build_dispatch(APPROVE) .input::<(AssetId, AccountId, Balance)>() .output::, true>() .handle_error_code::() - .call(&(id, spender, amount)) + .call(&(id, spender, value)) } /// Increases the allowance of a spender. /// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `spender` - The account that is allowed to spend the tokens. -/// * `value` - The number of tokens to increase the allowance by. +/// # Parameters +/// - `id` - The ID of the asset. +/// - `spender` - The account that is allowed to spend the tokens. +/// - `value` - The number of tokens to increase the allowance by. /// /// # Returns /// Returns `Ok(())` if successful, or an error if the operation fails. @@ -196,10 +188,10 @@ pub fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Re /// Decreases the allowance of a spender. /// -/// # Arguments -/// * `id` - The ID of the asset. -/// * `spender` - The account that is allowed to spend the tokens. -/// * `value` - The number of tokens to decrease the allowance by. +/// # Parameters +/// - `id` - The ID of the asset. +/// - `spender` - The account that is allowed to spend the tokens. +/// - `value` - The number of tokens to decrease the allowance by. /// /// # Returns /// Returns `Ok(())` if successful, or an error if the operation fails. @@ -212,12 +204,47 @@ pub fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Re .call(&(id, spender, value)) } +/// Creates `value` amount tokens and assigns them to `account`, increasing the total supply. +/// +/// # Parameters +/// - `id` - The ID of the asset. +/// - `account` - The account to be credited with the created tokens. +/// - `value` - The number of tokens to mint. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +pub fn mint(id: AssetId, account: AccountId, value: Balance) -> Result<()> { + build_dispatch(MINT) + .input::<(AssetId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, account, value)) +} + +/// Destroys `value` amount tokens from `account`, reducing the total supply. +/// +/// # Parameters +/// - `id` - The ID of the asset. +/// - `account` - The account from which the tokens will be destroyed. +/// - `value` - The number of tokens to destroy. +/// +/// # Returns +/// Returns `Ok(())` if successful, or an error if the operation fails. +pub fn burn(id: AssetId, account: AccountId, value: Balance) -> Result<()> { + build_dispatch(BURN) + .input::<(AssetId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, account, value)) +} + pub mod metadata { use super::*; + /// Returns the token name for a given asset ID. /// - /// # Arguments - /// * `id` - The ID of the asset. + /// # Parameters + /// - `id` - The ID of the asset. /// /// # Returns /// The name of the token as a byte vector, or an error if the operation fails. @@ -232,8 +259,8 @@ pub mod metadata { /// Returns the token symbol for a given asset ID. /// - /// # Arguments - /// * `id` - The ID of the asset. + /// # Parameters + /// - `id` - The ID of the asset. /// /// # Returns /// The symbol of the token as a byte vector, or an error if the operation fails. @@ -248,11 +275,11 @@ pub mod metadata { /// Returns the token decimals for a given asset ID. /// - /// # Arguments - /// * `id` - The ID of the asset. + /// # Parameters + /// - `id` - The ID of the asset. /// /// # Returns - /// The number of decimals of the token as a byte vector, or an error if the operation fails. + /// The number of decimals of the token, or an error if the operation fails. #[inline] pub fn token_decimals(id: AssetId) -> Result { build_read_state(TOKEN_DECIMALS) @@ -263,90 +290,87 @@ pub mod metadata { } } -// pub asset_management { -// /// Create a new token with a given asset ID. -// /// -// /// # Arguments -// /// * `id` - The ID of the asset. -// /// * `admin` - The account that will administer the asset. -// /// * `min_balance` - The minimum balance required for accounts holding this asset. -// /// -// /// # Returns -// /// Returns `Ok(())` if successful, or an error if the creation fails. -// pub fn create(id: AssetId, admin: impl Into>, min_balance: Balance) -> Result<()> { -// Ok(()) -// } -// -// /// Start the process of destroying a token with a given asset ID. -// /// -// /// # Arguments -// /// * `id` - The ID of the asset. -// /// -// /// # Returns -// /// Returns `Ok(())` if successful, or an error if the operation fails. -// fn start_destroy(id: AssetId) -> Result<()> { -// Ok(()) -// } -// -// /// Destroy all accounts associated with a token with a given asset ID. -// /// -// /// # Arguments -// /// * `id` - The ID of the asset. -// /// -// /// # Returns -// /// Returns `Ok(())` if successful, or an error if the operation fails. -// fn destroy_accounts(id: AssetId) -> Result<()> { -// Ok(()) -// } -// -// /// Destroy all approvals associated with a token with a given asset ID. -// /// -// /// # Arguments -// /// * `id` - The ID of the asset. -// /// -// /// # Returns -// /// Returns `Ok(())` if successful, or an error if the operation fails. -// fn destroy_approvals(id: AssetId) -> Result<()> { -// Ok(()) -// } -// -// /// Complete the process of destroying a token with a given asset ID. -// /// -// /// # Arguments -// /// * `id` - The ID of the asset. -// /// -// /// # Returns -// /// Returns `Ok(())` if successful, or an error if the operation fails. -// fn finish_destroy(id: AssetId) -> Result<()> { -// Ok(()) -// } -// -// /// Set the metadata for a token with a given asset ID. -// /// -// /// # Arguments -// /// * `id` - The ID of the asset. -// /// -// /// # Returns -// /// Returns `Ok(())` if successful, or an error if the operation fails. -// pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) -> Result<()> { -// Ok(()) -// } -// -// /// Clear the metadata for a token with a given asset ID. -// /// -// /// # Arguments -// /// * `id` - The ID of the asset. -// /// -// /// # Returns -// /// Returns `Ok(())` if successful, or an error if the operation fails. -// fn clear_metadata(id: AssetId) -> Result<()> { -// Ok(()) -// } -// } -// -// pub fn asset_exists(id: AssetId) -> Result { -// assets::asset_exists(id) -// } +pub mod asset_management { + use super::*; + + /// Create a new token with a given asset ID. + /// + /// # Parameters + /// - `id` - The ID of the asset. + /// - `admin` - The account that will administer the asset. + /// - `min_balance` - The minimum balance required for accounts holding this asset. + /// + /// # Returns + /// Returns `Ok(())` if successful, or an error if the creation fails. + pub fn create(id: AssetId, admin: AccountId, min_balance: Balance) -> Result<()> { + build_dispatch(CREATE) + .input::<(AssetId, AccountId, Balance)>() + .output::, true>() + .handle_error_code::() + .call(&(id, admin, min_balance)) + } + + /// Start the process of destroying a token with a given asset ID. + /// + /// # Parameters + /// - `id` - The ID of the asset. + /// + /// # Returns + /// Returns `Ok(())` if successful, or an error if the operation fails. + pub fn start_destroy(id: AssetId) -> Result<()> { + build_dispatch(START_DESTROY) + .input::() + .output::, true>() + .handle_error_code::() + .call(&(id)) + } + + /// Set the metadata for a token with a given asset ID. + /// + /// # Parameters + /// - `id`: The identifier of the asset to update. + /// - `name`: The user friendly name of this asset. Limited in length by `StringLimit`. + /// - `symbol`: The exchange symbol for this asset. Limited in length by `StringLimit`. + /// - `decimals`: The number of decimals this asset uses to represent one unit. + /// + /// # Returns + /// Returns `Ok(())` if successful, or an error if the operation fails. + pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) -> Result<()> { + build_dispatch(SET_METADATA) + .input::<(AssetId, Vec, Vec, u8)>() + .output::, true>() + .handle_error_code::() + .call(&(id, name, symbol, decimals)) + } + + /// Clear the metadata for a token with a given asset ID. + /// + /// # Parameters + /// - `id` - The ID of the asset. + /// + /// # Returns + /// Returns `Ok(())` if successful, or an error if the operation fails. + pub fn clear_metadata(id: AssetId) -> Result<()> { + build_dispatch(CLEAR_METADATA) + .input::() + .output::, true>() + .handle_error_code::() + .call(&(id)) + } + + /// Checks if token with a given asset ID exists. + /// + /// # Parameters + /// - `id` - The ID of the asset. + #[inline] + pub fn asset_exists(id: AssetId) -> Result { + build_read_state(ASSET_EXISTS) + .input::() + .output::, true>() + .handle_error_code::() + .call(&(id)) + } +} /// Represents various errors related to local fungible assets in the Pop API. /// diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index b3932a79..bdff6bbb 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -25,6 +25,10 @@ impl Contains for AllowedApiCalls { | transfer_from { .. } | approve { .. } | increase_allowance { .. } | decrease_allowance { .. } + | create { .. } | set_metadata { .. } + | start_destroy { .. } + | clear_metadata { .. } + | mint { .. } | burn { .. } ) ) } @@ -40,7 +44,7 @@ impl Contains> for AllowedApiCalls { TotalSupply(..) | BalanceOf { .. } | Allowance { .. } | TokenName(..) | TokenSymbol(..) - | TokenDecimals(..) + | TokenDecimals(..) | AssetExists(..) ) ) } From 00c59032b3755d2cf8b3442095876612425116f9 Mon Sep 17 00:00:00 2001 From: Tin Chung <56880684+chungquantin@users.noreply.github.com> Date: Fri, 9 Aug 2024 18:37:37 +0700 Subject: [PATCH 35/76] refactor: remove bare_call code duplication in integration test (#168) --- .../integration-tests/src/fungibles/utils.rs | 91 +++++++------------ 1 file changed, 35 insertions(+), 56 deletions(-) diff --git a/pop-api/integration-tests/src/fungibles/utils.rs b/pop-api/integration-tests/src/fungibles/utils.rs index 2f9be65c..33a25f7a 100644 --- a/pop-api/integration-tests/src/fungibles/utils.rs +++ b/pop-api/integration-tests/src/fungibles/utils.rs @@ -1,13 +1,17 @@ use super::*; +fn do_bare_call(function: &str, addr: AccountId32, params: Vec) -> ExecReturnValue { + let function = function_selector(function); + let params = [function, params].concat(); + bare_call(addr, params, 0).expect("should work") +} + pub(super) fn decoded(result: ExecReturnValue) -> Result { ::decode(&mut &result.data[1..]).map_err(|_| result) } pub(super) fn total_supply(addr: AccountId32, asset_id: AssetId) -> Result { - let function = function_selector("total_supply"); - let params = [function, asset_id.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let result = do_bare_call("total_supply", addr, asset_id.encode()); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } @@ -17,9 +21,8 @@ pub(super) fn balance_of( asset_id: AssetId, owner: AccountId32, ) -> Result { - let function = function_selector("balance_of"); - let params = [function, asset_id.encode(), owner.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let params = [asset_id.encode(), owner.encode()].concat(); + let result = do_bare_call("balance_of", addr, params); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } @@ -30,41 +33,32 @@ pub(super) fn allowance( owner: AccountId32, spender: AccountId32, ) -> Result { - let function = function_selector("allowance"); - let params = [function, asset_id.encode(), owner.encode(), spender.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let params = [asset_id.encode(), owner.encode(), spender.encode()].concat(); + let result = do_bare_call("allowance", addr, params); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } pub(super) fn token_name(addr: AccountId32, asset_id: AssetId) -> Result, Error> { - let function = function_selector("token_name"); - let params = [function, asset_id.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let result = do_bare_call("token_name", addr, asset_id.encode()); decoded::, Error>>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } pub(super) fn token_symbol(addr: AccountId32, asset_id: AssetId) -> Result, Error> { - let function = function_selector("token_symbol"); - let params = [function, asset_id.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let result = do_bare_call("token_symbol", addr, asset_id.encode()); decoded::, Error>>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } pub(super) fn token_decimals(addr: AccountId32, asset_id: AssetId) -> Result { - let function = function_selector("token_decimals"); - let params = [function, asset_id.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let result = do_bare_call("token_decimals", addr, asset_id.encode()); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } pub(super) fn asset_exists(addr: AccountId32, asset_id: AssetId) -> Result { - let function = function_selector("asset_exists"); - let params = [function, asset_id.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let result = do_bare_call("asset_exists", addr, asset_id.encode()); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } @@ -75,9 +69,8 @@ pub(super) fn transfer( to: AccountId32, value: Balance, ) -> Result<(), Error> { - let function = function_selector("transfer"); - let params = [function, asset_id.encode(), to.encode(), value.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let params = [asset_id.encode(), to.encode(), value.encode()].concat(); + let result = do_bare_call("transfer", addr, params); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } @@ -89,12 +82,10 @@ pub(super) fn transfer_from( to: AccountId32, value: Balance, ) -> Result<(), Error> { - let function = function_selector("transfer_from"); let data: Vec = vec![]; let params = - [function, asset_id.encode(), from.encode(), to.encode(), value.encode(), data.encode()] - .concat(); - let result = bare_call(addr, params, 0).expect("should work"); + [asset_id.encode(), from.encode(), to.encode(), value.encode(), data.encode()].concat(); + let result = do_bare_call("transfer_from", addr, params); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } @@ -105,9 +96,8 @@ pub(super) fn approve( spender: AccountId32, value: Balance, ) -> Result<(), Error> { - let function = function_selector("approve"); - let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let params = [asset_id.encode(), spender.encode(), value.encode()].concat(); + let result = do_bare_call("approve", addr, params); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } @@ -118,9 +108,8 @@ pub(super) fn increase_allowance( spender: AccountId32, value: Balance, ) -> Result<(), Error> { - let function = function_selector("increase_allowance"); - let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let params = [asset_id.encode(), spender.encode(), value.encode()].concat(); + let result = do_bare_call("increase_allowance", addr, params); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } @@ -131,9 +120,8 @@ pub(super) fn decrease_allowance( spender: AccountId32, value: Balance, ) -> Result<(), Error> { - let function = function_selector("decrease_allowance"); - let params = [function, asset_id.encode(), spender.encode(), value.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let params = [asset_id.encode(), spender.encode(), value.encode()].concat(); + let result = do_bare_call("decrease_allowance", addr, params); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } @@ -144,17 +132,14 @@ pub(super) fn create( admin: AccountId32, min_balance: Balance, ) -> Result<(), Error> { - let function = function_selector("create"); - let params = [function, asset_id.encode(), admin.encode(), min_balance.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let params = [asset_id.encode(), admin.encode(), min_balance.encode()].concat(); + let result = do_bare_call("create", addr, params); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } pub(super) fn start_destroy(addr: AccountId32, asset_id: AssetId) -> Result<(), Error> { - let function = function_selector("start_destroy"); - let params = [function, asset_id.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let result = do_bare_call("start_destroy", addr, asset_id.encode()); match decoded::>(result) { Ok(x) => x, Err(result) => panic!("Contract reverted: {:?}", result), @@ -168,18 +153,14 @@ pub(super) fn set_metadata( symbol: Vec, decimals: u8, ) -> Result<(), Error> { - let function = function_selector("set_metadata"); - let params = - [function, asset_id.encode(), name.encode(), symbol.encode(), decimals.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let params = [asset_id.encode(), name.encode(), symbol.encode(), decimals.encode()].concat(); + let result = do_bare_call("set_metadata", addr, params); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } pub(super) fn clear_metadata(addr: AccountId32, asset_id: AssetId) -> Result<(), Error> { - let function = function_selector("clear_metadata"); - let params = [function, asset_id.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let result = do_bare_call("clear_metadata", addr, asset_id.encode()); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } @@ -190,9 +171,8 @@ pub(super) fn mint( account: AccountId32, amount: Balance, ) -> Result<(), Error> { - let function = function_selector("mint"); - let params = [function, asset_id.encode(), account.encode(), amount.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let params = [asset_id.encode(), account.encode(), amount.encode()].concat(); + let result = do_bare_call("mint", addr, params); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } @@ -203,9 +183,8 @@ pub(super) fn burn( account: AccountId32, amount: Balance, ) -> Result<(), Error> { - let function = function_selector("burn"); - let params = [function, asset_id.encode(), account.encode(), amount.encode()].concat(); - let result = bare_call(addr, params, 0).expect("should work"); + let params = [asset_id.encode(), account.encode(), amount.encode()].concat(); + let result = do_bare_call("burn", addr, params); decoded::>(result.clone()) .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) } From 9ebe1621081f0593bf93096bccf86e1a5b761317 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Tue, 13 Aug 2024 10:52:42 +0200 Subject: [PATCH 36/76] fix: compilation --- Cargo.lock | 1621 +++++++++++++++++++++++++++------------------------- 1 file changed, 831 insertions(+), 790 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 982705a3..7f033a20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,11 +23,11 @@ dependencies = [ [[package]] name = "addr2line" -version = "0.21.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" dependencies = [ - "gimli 0.28.1", + "gimli 0.29.0", ] [[package]] @@ -68,7 +68,7 @@ dependencies = [ "cipher 0.4.4", "ctr", "ghash", - "subtle 2.5.0", + "subtle 2.4.1", ] [[package]] @@ -77,7 +77,7 @@ version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" dependencies = [ - "getrandom 0.2.12", + "getrandom 0.2.15", "once_cell", "version_check", ] @@ -89,7 +89,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ "cfg-if", - "getrandom 0.2.12", + "getrandom 0.2.15", "once_cell", "version_check", "zerocopy", @@ -106,9 +106,9 @@ dependencies = [ [[package]] name = "allocator-api2" -version = "0.2.16" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" +checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" [[package]] name = "always-assert" @@ -142,47 +142,48 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.13" +version = "0.6.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", + "is_terminal_polyfill", "utf8parse", ] [[package]] name = "anstyle" -version = "1.0.6" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" [[package]] name = "anstyle-parse" -version = "0.2.3" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" -version = "1.0.2" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" dependencies = [ "windows-sys 0.52.0", ] [[package]] name = "anstyle-wincon" -version = "3.0.2" +version = "3.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" dependencies = [ "anstyle", "windows-sys 0.52.0", @@ -190,9 +191,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.81" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" [[package]] name = "approx" @@ -214,7 +215,7 @@ dependencies = [ "proc-macro-error", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -354,15 +355,15 @@ checksum = "f52f63c5c1316a16a4b35eaac8b76a98248961a533f061684cb2a7cb0eafb6c6" [[package]] name = "array-bytes" -version = "6.2.2" +version = "6.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f840fb7195bcfc5e17ea40c26e5ce6d5b9ce5d584466e17703209657e459ae0" +checksum = "5d5dde061bd34119e902bbb2d9b90c5692635cf59fb91d582c2b68043f1b8293" [[package]] name = "arrayref" -version = "0.3.7" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545" +checksum = "9d151e35f61089500b617991b791fc8bfd237ae50cd5950803758a179b41e67a" [[package]] name = "arrayvec" @@ -404,7 +405,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.109", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -427,7 +428,7 @@ checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9" [[package]] name = "asset-hub-paseo-runtime" version = "1.0.0" -source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#e7265b10b28d8b82c3146e72c98895dac2b55729" +source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#2fa66a19eef96bd2c079385057fab2564d09b751" dependencies = [ "assets-common", "bp-asset-hub-paseo", @@ -468,6 +469,7 @@ dependencies = [ "pallet-transaction-payment-rpc-runtime-api", "pallet-uniques", "pallet-utility", + "pallet-vesting", "pallet-xcm", "pallet-xcm-benchmarks", "pallet-xcm-bridge-hub-router", @@ -570,27 +572,25 @@ dependencies = [ [[package]] name = "async-channel" -version = "2.2.0" +version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f28243a43d821d11341ab73c80bed182dc015c514b951616cf79bd4af39af0c3" +checksum = "89b47800b0be77592da0afd425cc03468052844aff33b84e33cc696f64e77b6a" dependencies = [ "concurrent-queue", - "event-listener 5.2.0", - "event-listener-strategy 0.5.0", + "event-listener-strategy", "futures-core", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] name = "async-executor" -version = "1.8.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17ae5ebefcc48e7452b4987947920dac9450be1110cadf34d1b8c116bdbaf97c" +checksum = "d7ebdfa2ebdab6b1760375fa7d6f382b9f486eac35fc994625a00e89280bdbb7" dependencies = [ - "async-lock 3.3.0", "async-task", "concurrent-queue", - "fastrand 2.0.2", + "fastrand 2.1.0", "futures-lite 2.3.0", "slab", ] @@ -629,21 +629,21 @@ dependencies = [ [[package]] name = "async-io" -version = "2.3.2" +version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcccb0f599cfa2f8ace422d3555572f47424da5648a4382a9dd0310ff8210884" +checksum = "444b0228950ee6501b3568d3c93bf1176a1fdbc3b758dcd9475046d30f4dc7e8" dependencies = [ - "async-lock 3.3.0", + "async-lock 3.4.0", "cfg-if", "concurrent-queue", "futures-io", "futures-lite 2.3.0", "parking", - "polling 3.6.0", - "rustix 0.38.32", + "polling 3.7.3", + "rustix 0.38.34", "slab", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -657,13 +657,13 @@ dependencies = [ [[package]] name = "async-lock" -version = "3.3.0" +version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d034b430882f8381900d3fe6f0aaa3ad94f2cb4ac519b429692a1bc2dda4ae7b" +checksum = "ff6e472cdea888a4bd64f342f09b3f50e1886d32afe8df3d663c01140b811b18" dependencies = [ - "event-listener 4.0.3", - "event-listener-strategy 0.4.0", - "pin-project-lite 0.2.13", + "event-listener 5.3.1", + "event-listener-strategy", + "pin-project-lite 0.2.14", ] [[package]] @@ -690,43 +690,43 @@ dependencies = [ "cfg-if", "event-listener 3.1.0", "futures-lite 1.13.0", - "rustix 0.38.32", + "rustix 0.38.34", "windows-sys 0.48.0", ] [[package]] name = "async-signal" -version = "0.2.5" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e47d90f65a225c4527103a8d747001fc56e375203592b25ad103e1ca13124c5" +checksum = "637e00349800c0bdf8bfc21ebbc0b6524abea702b0da4168ac00d070d0c0b9f3" dependencies = [ - "async-io 2.3.2", - "async-lock 2.8.0", + "async-io 2.3.4", + "async-lock 3.4.0", "atomic-waker", "cfg-if", "futures-core", "futures-io", - "rustix 0.38.32", + "rustix 0.38.34", "signal-hook-registry", "slab", - "windows-sys 0.48.0", + "windows-sys 0.59.0", ] [[package]] name = "async-task" -version = "4.7.0" +version = "4.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbb36e985947064623dbd357f727af08ffd077f93d696782f3c56365fa2e2799" +checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" [[package]] name = "async-trait" -version = "0.1.79" +version = "0.1.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507401cad91ec6a857ed5513a2073c82a9b9048762b885bb98655b306964681" +checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -739,7 +739,7 @@ dependencies = [ "futures-sink", "futures-util", "memchr", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] @@ -756,22 +756,22 @@ checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" [[package]] name = "autocfg" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "backtrace" -version = "0.3.71" +version = "0.3.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d" +checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" dependencies = [ - "addr2line 0.21.0", + "addr2line 0.22.0", "cc", "cfg-if", "libc", "miniz_oxide", - "object 0.32.2", + "object 0.36.3", "rustc-demangle", ] @@ -845,13 +845,13 @@ dependencies = [ "lazy_static", "lazycell", "peeking_take_while", - "prettyplease 0.2.17", + "prettyplease 0.2.20", "proc-macro2", "quote", "regex", "rustc-hash", "shlex", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -881,9 +881,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] name = "bitvec" @@ -953,9 +953,9 @@ dependencies = [ [[package]] name = "blake3" -version = "1.5.1" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30cca6d3674597c30ddf2c587bf8d9d65c9a84d2326d941cc79c9842dfe0ef52" +checksum = "e9ec96fe9a81b5e365f9db71fe00edc4fe4ca2cc7dcb7861f0603012a7caa210" dependencies = [ "arrayref", "arrayvec 0.7.4", @@ -984,18 +984,15 @@ dependencies = [ [[package]] name = "blocking" -version = "1.5.1" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a37913e8dc4ddcc604f0c6d3bf2887c995153af3611de9e23c352b44c1b9118" +checksum = "703f41c54fc768e63e091340b424302bb1c29ef4aa0c7f10fe849dfb114d29ea" dependencies = [ - "async-channel 2.2.0", - "async-lock 3.3.0", + "async-channel 2.3.1", "async-task", - "fastrand 2.0.2", "futures-io", "futures-lite 2.3.0", "piper", - "tracing", ] [[package]] @@ -1022,7 +1019,7 @@ dependencies = [ [[package]] name = "bp-asset-hub-paseo" version = "1.0.0" -source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#e7265b10b28d8b82c3146e72c98895dac2b55729" +source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#2fa66a19eef96bd2c079385057fab2564d09b751" dependencies = [ "frame-support", "sp-std", @@ -1049,7 +1046,7 @@ dependencies = [ [[package]] name = "bp-bridge-hub-paseo" version = "1.0.0" -source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#e7265b10b28d8b82c3146e72c98895dac2b55729" +source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#2fa66a19eef96bd2c079385057fab2564d09b751" dependencies = [ "bp-bridge-hub-cumulus", "bp-messages", @@ -1066,9 +1063,9 @@ dependencies = [ [[package]] name = "bp-header-chain" -version = "0.8.0" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96157f586811969b3911d26cc79e02b28cfbecf859d96d7c12b6af10b9ea9350" +checksum = "1c4d2c457d5e18a5dbfe47a2ecd01f95036930a4a7ac0f3e47c2843bb067331b" dependencies = [ "bp-runtime", "finality-grandpa", @@ -1278,9 +1275,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.15.4" +version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" [[package]] name = "byte-slice-cast" @@ -1296,9 +1293,9 @@ checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" [[package]] name = "bytemuck" -version = "1.15.0" +version = "1.16.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d6d68c57235a3a081186990eca2867354726650f42f7516ca50c28d6281fd15" +checksum = "102087e286b4677862ea56cf8fc58bb2cdfa8725c40ffb80fe3a008eb7f2fc83" [[package]] name = "byteorder" @@ -1308,9 +1305,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.6.0" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" +checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" [[package]] name = "bzip2-sys" @@ -1335,9 +1332,9 @@ dependencies = [ [[package]] name = "camino" -version = "1.1.6" +version = "1.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c" +checksum = "e0ec6b951b160caa93cc0c7b209e5a3bff7aae9062213451ac99493cd844c239" dependencies = [ "serde", ] @@ -1359,7 +1356,7 @@ checksum = "eee4243f1f26fc7a42710e7439c149e2b10b05472f88090acce52632f231a73a" dependencies = [ "camino", "cargo-platform", - "semver 1.0.22", + "semver 1.0.23", "serde", "serde_json", "thiserror", @@ -1367,9 +1364,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.90" +version = "1.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" +checksum = "e9e8aabfac534be767c909e0690571677d49f41bd8465ae876fe043d52ba5292" dependencies = [ "jobserver", "libc", @@ -1386,9 +1383,9 @@ dependencies = [ [[package]] name = "cfg-expr" -version = "0.15.7" +version = "0.15.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa50868b64a9a6fda9d593ce778849ea8715cd2a3d2cc17ffdb4a2f2f2f1961d" +checksum = "d067ad48b8650848b989a59a86c6c36a995d02d2bf778d45c3c5d57bc2718f02" dependencies = [ "smallvec", ] @@ -1441,16 +1438,16 @@ dependencies = [ [[package]] name = "chrono" -version = "0.4.35" +version = "0.4.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8eaf5903dcbc0a39312feb77df2ff4c76387d591b9fc7b04a238dcf8bb62639a" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" dependencies = [ "android-tzdata", "iana-time-zone", "js-sys", "num-traits", "wasm-bindgen", - "windows-targets 0.52.4", + "windows-targets 0.52.6", ] [[package]] @@ -1497,9 +1494,9 @@ dependencies = [ [[package]] name = "clang-sys" -version = "1.7.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67523a3b4be3ce1989d607a828d036249522dd9c1c8de7f4dd2dae43a37369d1" +checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" dependencies = [ "glob", "libc", @@ -1508,9 +1505,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.4" +version = "4.5.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +checksum = "11d8838454fda655dafd3accb2b6e2bea645b9e4078abe84a22ceb947235c5cc" dependencies = [ "clap_builder", "clap_derive", @@ -1518,9 +1515,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.2" +version = "4.5.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +checksum = "216aec2b177652e3846684cbfe25c9964d18ec45234f0f5da5157b207ed1aab6" dependencies = [ "anstream", "anstyle", @@ -1531,21 +1528,21 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.4" +version = "4.5.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "clap_lex" -version = "0.7.0" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" [[package]] name = "coarsetime" @@ -1570,39 +1567,39 @@ dependencies = [ [[package]] name = "color-print" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a858372ff14bab9b1b30ea504f2a4bc534582aee3e42ba2d41d2a7baba63d5d" +checksum = "1ee543c60ff3888934877a5671f45494dd27ed4ba25c6670b9a7576b7ed7a8c0" dependencies = [ "color-print-proc-macro", ] [[package]] name = "color-print-proc-macro" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57e37866456a721d0a404439a1adae37a31be4e0055590d053dfe6981e05003f" +checksum = "77ff1a80c5f3cb1ca7c06ffdd71b6a6dd6d8f896c42141fbd43f50ed28dcdb93" dependencies = [ "nom", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.74", ] [[package]] name = "colorchoice" -version = "1.0.0" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" [[package]] name = "comfy-table" -version = "7.1.0" +version = "7.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c64043d6c7b7a4c58e39e7efccfdea7b93d885a795d0c054a69dbbf4dd52686" +checksum = "b34115915337defe99b2aff5c2ce6771e5fbc4079f4b506301f5cf394c8452f7" dependencies = [ - "strum 0.25.0", - "strum_macros 0.25.3", + "strum 0.26.3", + "strum_macros 0.26.4", "unicode-width", ] @@ -1614,9 +1611,9 @@ checksum = "2382f75942f4b3be3690fe4f86365e9c853c1587d6ee58212cebf6e2a9ccd101" [[package]] name = "concurrent-queue" -version = "2.4.0" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d16048cd947b08fa32c24458a22f5dc5e835264f689f4f5653210c69fd107363" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" dependencies = [ "crossbeam-utils", ] @@ -1655,7 +1652,7 @@ version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e" dependencies = [ - "getrandom 0.2.12", + "getrandom 0.2.15", "once_cell", "tiny-keccak", ] @@ -1696,9 +1693,9 @@ dependencies = [ [[package]] name = "core-foundation-sys" -version = "0.8.6" +version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] name = "core2" @@ -1730,9 +1727,9 @@ dependencies = [ [[package]] name = "cpufeatures" -version = "0.2.12" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +checksum = "51e852e6dc9a5bed1fae92dd2375037bf2b768725bf3be87811edee3249d09ad" dependencies = [ "libc", ] @@ -1837,9 +1834,9 @@ dependencies = [ [[package]] name = "crc32fast" -version = "1.4.0" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3855a8a784b474f333699ef2bbca9db2c4a1f6d9088a90a2d25b1eb53111eaa" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" dependencies = [ "cfg-if", ] @@ -1874,9 +1871,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.19" +version = "0.8.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" [[package]] name = "crunchy" @@ -1892,7 +1889,7 @@ checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ "generic-array 0.14.7", "rand_core 0.6.4", - "subtle 2.5.0", + "subtle 2.4.1", "zeroize", ] @@ -1924,17 +1921,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab" dependencies = [ "generic-array 0.14.7", - "subtle 2.5.0", + "subtle 2.4.1", ] [[package]] name = "crypto-mac" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25fab6889090c8133f3deb8f73ba3c65a7f456f66436fc012a1b1e272b1e103e" +checksum = "b1d1a86f49236c215f271d40892d5fc950490551400b02ef360692c29815c714" dependencies = [ "generic-array 0.14.7", - "subtle 2.5.0", + "subtle 2.4.1", ] [[package]] @@ -1975,7 +1972,7 @@ dependencies = [ "cumulus-primitives-core", "futures", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-node-primitives", "polkadot-node-subsystem", "polkadot-overseer", @@ -2088,7 +2085,7 @@ dependencies = [ "futures", "futures-timer", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-node-primitives", "polkadot-parachain-primitives", "polkadot-primitives", @@ -2252,7 +2249,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -2444,13 +2441,13 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c7718fe298d567adc44fae3dd7024418d6eff08264041e4b0544d1892861cd6" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "async-trait", "cumulus-primitives-core", "cumulus-relay-chain-interface", "cumulus-relay-chain-rpc-interface", "futures", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-availability-recovery", "polkadot-collator-protocol", "polkadot-core-primitives", @@ -2544,24 +2541,23 @@ dependencies = [ "byteorder", "digest 0.9.0", "rand_core 0.5.1", - "subtle 2.5.0", + "subtle 2.4.1", "zeroize", ] [[package]] name = "curve25519-dalek" -version = "4.1.2" +version = "4.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a677b8922c94e01bdbb12126b0bc852f00447528dee1782229af9c720c3f348" +checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" dependencies = [ "cfg-if", "cpufeatures", "curve25519-dalek-derive", "digest 0.10.7", "fiat-crypto", - "platforms", "rustc_version", - "subtle 2.5.0", + "subtle 2.4.1", "zeroize", ] @@ -2573,7 +2569,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -2591,9 +2587,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.120" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff4dc7287237dd438b926a81a1a5605dad33d286870e5eee2db17bf2bcd9e92a" +checksum = "273dcfd3acd4e1e276af13ed2a43eea7001318823e7a726a6b3ed39b4acc0b82" dependencies = [ "cc", "cxxbridge-flags", @@ -2603,9 +2599,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.120" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f47c6c8ad7c1a10d3ef0fe3ff6733f4db0d78f08ef0b13121543163ef327058b" +checksum = "d8b2766fbd92be34e9ed143898fce6c572dc009de39506ed6903e5a05b68914e" dependencies = [ "cc", "codespan-reporting", @@ -2613,37 +2609,37 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "cxxbridge-flags" -version = "1.0.120" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "701a1ac7a697e249cdd8dc026d7a7dafbfd0dbcd8bd24ec55889f2bc13dd6287" +checksum = "839fcd5e43464614ffaa989eaf1c139ef1f0c51672a1ed08023307fa1b909ccd" [[package]] name = "cxxbridge-macro" -version = "1.0.120" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b404f596046b0bb2d903a9c786b875a126261b52b7c3a64bbb66382c41c771df" +checksum = "4b2c1c1776b986979be68bb2285da855f8d8a35851a769fca8740df7c3d07877" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "data-encoding" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5" +checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" [[package]] name = "data-encoding-macro" -version = "0.1.14" +version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20c01c06f5f429efdf2bae21eb67c28b3df3cf85b7dd2d8ef09c0838dac5d33e" +checksum = "f1559b6cba622276d6d63706db152618eeb15b89b3e4041446b05876e352e639" dependencies = [ "data-encoding", "data-encoding-macro-internal", @@ -2651,9 +2647,9 @@ dependencies = [ [[package]] name = "data-encoding-macro-internal" -version = "0.1.12" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0047d07f2c89b17dd631c80450d69841a6b5d7fb17278cbc43d7e4cfcf2576f3" +checksum = "332d754c0af53bc87c108fed664d121ecf59207ec4196041f04d6ab9002ad33f" dependencies = [ "data-encoding", "syn 1.0.109", @@ -2661,9 +2657,9 @@ dependencies = [ [[package]] name = "der" -version = "0.7.8" +version = "0.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fffa369a668c8af7dbf8b5e56c9f744fbd399949ed171606040001947de40b1c" +checksum = "f55bf8e7b65898637379c1b74eb1551107c8294ed26d855ceb9fd1a09cfc9bc0" dependencies = [ "const-oid", "zeroize", @@ -2722,20 +2718,20 @@ checksum = "d65d7ce8132b7c0e54497a4d9a55a1c2a0912a0d786cf894472ba818fba45762" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "derive_more" -version = "0.99.17" +version = "0.99.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +checksum = "5f33878137e4dafd7fa914ad4e259e18a4e8e532b9617a2d0150262bf53abfce" dependencies = [ "convert_case", "proc-macro2", "quote", "rustc_version", - "syn 1.0.109", + "syn 2.0.74", ] [[package]] @@ -2771,7 +2767,7 @@ dependencies = [ "block-buffer 0.10.4", "const-oid", "crypto-common", - "subtle 2.5.0", + "subtle 2.4.1", ] [[package]] @@ -2818,13 +2814,13 @@ dependencies = [ [[package]] name = "displaydoc" -version = "0.2.4" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -2848,9 +2844,9 @@ dependencies = [ "proc-macro2", "quote", "regex", - "syn 2.0.55", + "syn 2.0.74", "termcolor", - "toml 0.8.12", + "toml 0.8.19", "walkdir", ] @@ -2862,9 +2858,9 @@ checksum = "1435fa1053d8b2fbbe9be7e97eca7f33d37b28409959813daefc1446a14247f1" [[package]] name = "downcast-rs" -version = "1.2.0" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" [[package]] name = "dtoa" @@ -2929,12 +2925,12 @@ version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871" dependencies = [ - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "ed25519", "rand_core 0.6.4", "serde", "sha2 0.10.8", - "subtle 2.5.0", + "subtle 2.4.1", "zeroize", ] @@ -2958,9 +2954,9 @@ version = "4.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d9ce6874da5d4415896cd45ffbc4d1cfc0c4f9c079427bd870742c30f2f65a9" dependencies = [ - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "ed25519", - "hashbrown 0.14.3", + "hashbrown 0.14.5", "hex", "rand_core 0.6.4", "sha2 0.10.8", @@ -2969,9 +2965,9 @@ dependencies = [ [[package]] name = "either" -version = "1.10.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "elliptic-curve" @@ -2988,7 +2984,7 @@ dependencies = [ "pkcs8", "rand_core 0.6.4", "sec1", - "subtle 2.5.0", + "subtle 2.4.1", "zeroize", ] @@ -3045,40 +3041,40 @@ dependencies = [ [[package]] name = "enumflags2" -version = "0.7.9" +version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3278c9d5fb675e0a51dabcf4c0d355f692b064171535ba72361be1528a9d8e8d" +checksum = "d232db7f5956f3f14313dc2f87985c58bd2c695ce124c8cdd984e08e15ac133d" dependencies = [ "enumflags2_derive", ] [[package]] name = "enumflags2_derive" -version = "0.7.9" +version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c785274071b1b420972453b306eeca06acf4633829db4223b58a2a8c5953bc4" +checksum = "de0d48a183585823424a4ce1aa132d174a6a81bd540895822eb4c8373a8e49e8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "enumn" -version = "0.1.13" +version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fd000fd6988e73bbe993ea3db9b1aa64906ab88766d654973924340c8cddb42" +checksum = "2f9ed6b3789237c8a0c1c505af1c7eb2c560df6186f01b098c3a1064ea532f38" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "env_filter" -version = "0.1.0" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" dependencies = [ "log", "regex", @@ -3099,9 +3095,9 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.11.3" +version = "0.11.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" dependencies = [ "anstream", "anstyle", @@ -3124,9 +3120,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" dependencies = [ "libc", "windows-sys 0.52.0", @@ -3187,49 +3183,28 @@ checksum = "d93877bcde0eb80ca09131a08d23f0a5c18a620b01db137dba666d18cd9b30c2" dependencies = [ "concurrent-queue", "parking", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] name = "event-listener" -version = "4.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67b215c49b2b248c855fb73579eb1f4f26c38ffdc12973e20e07b91d78d5646e" -dependencies = [ - "concurrent-queue", - "parking", - "pin-project-lite 0.2.13", -] - -[[package]] -name = "event-listener" -version = "5.2.0" +version = "5.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b5fb89194fa3cad959b833185b3063ba881dbfc7030680b314250779fb4cc91" +checksum = "6032be9bd27023a771701cc49f9f053c751055f71efb2e0ae5c15809093675ba" dependencies = [ "concurrent-queue", "parking", - "pin-project-lite 0.2.13", -] - -[[package]] -name = "event-listener-strategy" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "958e4d70b6d5e81971bebec42271ec641e7ff4e170a6fa605f2b8a8b65cb97d3" -dependencies = [ - "event-listener 4.0.3", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] name = "event-listener-strategy" -version = "0.5.0" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "feedafcaa9b749175d5ac357452a9d41ea2911da598fde46ce1fe02c37751291" +checksum = "0f214dc438f977e6d4e3500aaa277f5ad94ca83fbbd9b1a15713ce2344ccc5a1" dependencies = [ - "event-listener 5.2.0", - "pin-project-lite 0.2.13", + "event-listener 5.3.1", + "pin-project-lite 0.2.14", ] [[package]] @@ -3255,16 +3230,17 @@ dependencies = [ [[package]] name = "expander" -version = "2.1.0" +version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00e83c02035136f1592a47964ea60c05a50e4ed8b5892cfac197063850898d4d" +checksum = "e2c470c71d91ecbd179935b24170459e926382eaaa86b590b78814e180d8a8e2" dependencies = [ "blake2 0.10.6", + "file-guard", "fs-err", - "prettier-please", + "prettyplease 0.2.20", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -3284,9 +3260,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.0.2" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "658bd65b1cf4c852a3cc96f18a8ce7b5640f6b703f905c7d74532294c2a63984" +checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" [[package]] name = "fatality" @@ -3306,7 +3282,7 @@ checksum = "f5aa1e3ae159e592ad222dc90c5acbad632b527779ba88486abe92782ab268bd" dependencies = [ "expander 0.0.4", "indexmap 1.9.3", - "proc-macro-crate 1.3.1", + "proc-macro-crate 1.1.3", "proc-macro2", "quote", "syn 1.0.109", @@ -3330,14 +3306,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ded41244b729663b1e574f1b4fb731469f69f79c17667b5d776b16cda0479449" dependencies = [ "rand_core 0.6.4", - "subtle 2.5.0", + "subtle 2.4.1", ] [[package]] name = "fiat-crypto" -version = "0.2.7" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c007b1ae3abe1cb6f85a16305acd418b7ca6343b953633fee2b76d8f108b830f" +checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" + +[[package]] +name = "file-guard" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21ef72acf95ec3d7dbf61275be556299490a245f017cf084bd23b4f68cf9407c" +dependencies = [ + "libc", + "winapi", +] [[package]] name = "file-per-thread-logger" @@ -3351,14 +3337,14 @@ dependencies = [ [[package]] name = "filetime" -version = "0.2.23" +version = "0.2.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ee447700ac8aa0b2f2bd7bc4462ad686ba06baa6727ac149a2d6277f0d240fd" +checksum = "bf401df4a4e3872c4fe8151134cf483738e74b67fc934d6532c882b3d24a4550" dependencies = [ "cfg-if", "libc", - "redox_syscall 0.4.1", - "windows-sys 0.52.0", + "libredox", + "windows-sys 0.59.0", ] [[package]] @@ -3373,7 +3359,7 @@ dependencies = [ "log", "num-traits", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "scale-info", ] @@ -3397,9 +3383,9 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" [[package]] name = "flate2" -version = "1.0.28" +version = "1.0.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46303f565772937ffe1d394a4fac6f411c6013172fadde9dcdb1e147a086940e" +checksum = "7f211bbe8e69bbd0cfdea405084f128ae8b4aaa6b0b522fc8f2b009084797920" dependencies = [ "crc32fast", "libz-sys", @@ -3478,7 +3464,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "efe02c96362e3c7308cdea7545859f767194a1f3f00928f0e1357f4b8a0b3b2c" dependencies = [ "Inflector", - "array-bytes 6.2.2", + "array-bytes 6.2.3", "chrono", "clap", "comfy-table", @@ -3529,7 +3515,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -3587,7 +3573,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5bb1eec9eb46d3e016c95b2fa875118c04609f2150013c56a894cae00581e265" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "docify", "frame-support", "frame-system", @@ -3627,7 +3613,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8e52c84b611d2049d9253f83a62ab0f093e4be5c42a7ef42ea5bb16d6611e32" dependencies = [ "aquamarine", - "array-bytes 6.2.2", + "array-bytes 6.2.3", "bitflags 1.3.2", "docify", "environmental", @@ -3671,7 +3657,7 @@ dependencies = [ "Inflector", "cfg-expr", "derive-syn-parse 0.1.5", - "expander 2.1.0", + "expander 2.2.1", "frame-support-procedural-tools", "itertools 0.10.5", "macro_magic", @@ -3679,7 +3665,7 @@ dependencies = [ "proc-macro2", "quote", "sp-crypto-hashing", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -3692,7 +3678,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -3703,7 +3689,7 @@ checksum = "68672b9ec6fe72d259d3879dc212c5e42e977588cdac830c76f54d9f492aeb58" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -3791,7 +3777,7 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29f9df8a11882c4e3335eb2d18a0137c505d9ca927470b0cac9c6f0ae07d28f7" dependencies = [ - "rustix 0.38.32", + "rustix 0.38.34", "windows-sys 0.48.0", ] @@ -3861,7 +3847,7 @@ dependencies = [ "futures-io", "memchr", "parking", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "waker-fn", ] @@ -3871,11 +3857,11 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52527eb5074e35e9339c6b4e8d12600c7128b68fb25dcb9fa9dec18f7c25f3a5" dependencies = [ - "fastrand 2.0.2", + "fastrand 2.1.0", "futures-core", "futures-io", "parking", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] @@ -3886,7 +3872,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -3931,7 +3917,7 @@ dependencies = [ "futures-sink", "futures-task", "memchr", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "pin-utils", "slab", ] @@ -3988,9 +3974,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.12" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ "cfg-if", "libc", @@ -4030,9 +4016,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.28.1" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" [[package]] name = "glob" @@ -4048,7 +4034,7 @@ checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" dependencies = [ "ff", "rand_core 0.6.4", - "subtle 2.5.0", + "subtle 2.4.1", ] [[package]] @@ -4063,7 +4049,7 @@ dependencies = [ "futures-sink", "futures-util", "http", - "indexmap 2.2.6", + "indexmap 2.3.0", "slab", "tokio", "tokio-util", @@ -4119,9 +4105,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.14.3" +version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" dependencies = [ "ahash 0.8.11", "allocator-api2", @@ -4134,7 +4120,7 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8094feaf31ff591f651a2664fb9cfd92bba7a60ce3197265e9482ebe753c8f7" dependencies = [ - "hashbrown 0.14.3", + "hashbrown 0.14.5", ] [[package]] @@ -4155,6 +4141,12 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "hermit-abi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" + [[package]] name = "hex" version = "0.4.3" @@ -4192,7 +4184,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a2a2320eb7ec0ebe8da8f744d7812d9fc4cb4d09344ac01898dbcb6a20ae69b" dependencies = [ - "crypto-mac 0.11.0", + "crypto-mac 0.11.1", "digest 0.9.0", ] @@ -4255,7 +4247,7 @@ checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" dependencies = [ "bytes", "http", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", ] [[package]] @@ -4266,9 +4258,9 @@ checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f" [[package]] name = "httparse" -version = "1.8.0" +version = "1.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" +checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" [[package]] name = "httpdate" @@ -4284,9 +4276,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "hyper" -version = "0.14.28" +version = "0.14.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" +checksum = "a152ddd61dfaec7273fe8419ab357f33aee0d914c5f4efbf0d96fa749eea5ec9" dependencies = [ "bytes", "futures-channel", @@ -4298,8 +4290,8 @@ dependencies = [ "httparse", "httpdate", "itoa", - "pin-project-lite 0.2.13", - "socket2 0.5.6", + "pin-project-lite 0.2.14", + "socket2 0.5.7", "tokio", "tower-service", "tracing", @@ -4316,7 +4308,7 @@ dependencies = [ "http", "hyper", "log", - "rustls 0.21.10", + "rustls 0.21.12", "rustls-native-certs", "tokio", "tokio-rustls", @@ -4333,7 +4325,7 @@ dependencies = [ "iana-time-zone-haiku", "js-sys", "wasm-bindgen", - "windows-core", + "windows-core 0.52.0", ] [[package]] @@ -4382,7 +4374,7 @@ version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6b0422c86d7ce0e97169cc42e04ae643caf278874a7a3c87b8150a220dc7e1e" dependencies = [ - "async-io 2.3.2", + "async-io 2.3.4", "core-foundation", "fnv", "futures", @@ -4446,18 +4438,18 @@ dependencies = [ [[package]] name = "include_dir" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18762faeff7122e89e0857b02f7ce6fcc0d101d5e9ad2ad7846cc01d61b7f19e" +checksum = "923d117408f1e49d914f1a379a309cffe4f18c05cf4e3d12e613a15fc81bd0dd" dependencies = [ "include_dir_macros", ] [[package]] name = "include_dir_macros" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b139284b5cf57ecfa712bcc66950bb635b31aff41c188e8a4cfc758eca374a3f" +checksum = "7cab85a7ed0bd5f0e76d93846e0147172bed2e2d3f859bcc33a8d9699cad1a75" dependencies = [ "proc-macro2", "quote", @@ -4476,12 +4468,12 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.2.6" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +checksum = "de3fc2e30ba82dd1b3911c8de1ffc143c74a914a14e99514d7637e3099df5ea0" dependencies = [ "equivalent", - "hashbrown 0.14.3", + "hashbrown 0.14.5", ] [[package]] @@ -4514,9 +4506,9 @@ dependencies = [ [[package]] name = "instant" -version = "0.1.12" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" dependencies = [ "cfg-if", ] @@ -4574,7 +4566,7 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.9", "libc", "windows-sys 0.48.0", ] @@ -4591,7 +4583,7 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b58db92f96b720de98181bbbe63c831e87005ab460c1bf306eb2622b4707997f" dependencies = [ - "socket2 0.5.6", + "socket2 0.5.7", "widestring", "windows-sys 0.48.0", "winreg", @@ -4609,7 +4601,7 @@ version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.9", "libc", "windows-sys 0.52.0", ] @@ -4623,6 +4615,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itertools" version = "0.10.5" @@ -4641,26 +4639,35 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +dependencies = [ + "either", +] + [[package]] name = "itoa" -version = "1.0.10" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "jobserver" -version = "0.1.28" +version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab46a6e9526ddef3ae7f787c06f0f2600639ba80ea3eade3d8e670a2230f51d6" +checksum = "48d1dbcbbeb6a7fec7e059840aa538bd62aaccf972c7346c4d9d2059312853d0" dependencies = [ "libc", ] [[package]] name = "js-sys" -version = "0.3.69" +version = "0.3.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" +checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" dependencies = [ "wasm-bindgen", ] @@ -4715,7 +4722,7 @@ dependencies = [ "futures-util", "hyper", "jsonrpsee-types", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "rustc-hash", "serde", @@ -4753,7 +4760,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29110019693a4fa2dbda04876499d098fa16d70eba06b1e6e2b3f1b251419515" dependencies = [ "heck 0.4.1", - "proc-macro-crate 1.3.1", + "proc-macro-crate 1.1.3", "proc-macro2", "quote", "syn 1.0.109", @@ -4853,7 +4860,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf7a85fe66f9ff9cd74e169fdd2c94c6e1e74c412c99a73b4df3200b5d3760b2" dependencies = [ "kvdb", - "parking_lot 0.12.1", + "parking_lot 0.12.3", ] [[package]] @@ -4864,7 +4871,7 @@ checksum = "b644c70b92285f66bfc2032922a79000ea30af7bc2ab31902992a5dcb9b434f6" dependencies = [ "kvdb", "num_cpus", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "regex", "rocksdb", "smallvec", @@ -4872,9 +4879,9 @@ dependencies = [ [[package]] name = "landlock" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1530c5b973eeed4ac216af7e24baf5737645a6272e361f1fb95710678b67d9cc" +checksum = "9baa9eeb6e315942429397e617a190f4fdc696ef1ee0342939d641029cbb4ea7" dependencies = [ "enumflags2", "libc", @@ -4883,9 +4890,9 @@ dependencies = [ [[package]] name = "lazy_static" -version = "1.4.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "lazycell" @@ -4895,18 +4902,18 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.153" +version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "libloading" -version = "0.8.3" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" +checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" dependencies = [ "cfg-if", - "windows-targets 0.52.4", + "windows-targets 0.52.6", ] [[package]] @@ -4924,7 +4931,7 @@ dependencies = [ "bytes", "futures", "futures-timer", - "getrandom 0.2.12", + "getrandom 0.2.15", "instant", "libp2p-allow-block-list", "libp2p-connection-limits", @@ -4989,7 +4996,7 @@ dependencies = [ "multihash 0.17.0", "multistream-select", "once_cell", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "pin-project", "quick-protobuf", "rand", @@ -5009,7 +5016,7 @@ dependencies = [ "futures", "libp2p-core", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "smallvec", "trust-dns-resolver", ] @@ -5171,7 +5178,7 @@ dependencies = [ "libp2p-identity", "libp2p-tls", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "quinn-proto", "rand", "rustls 0.20.9", @@ -5287,7 +5294,7 @@ dependencies = [ "futures-rustls", "libp2p-core", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "quicksink", "rw-stream-sink", "soketto", @@ -5310,13 +5317,13 @@ dependencies = [ [[package]] name = "libredox" -version = "0.0.1" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "libc", - "redox_syscall 0.4.1", + "redox_syscall 0.5.3", ] [[package]] @@ -5361,7 +5368,7 @@ checksum = "5be9b9bb642d8522a44d533eab56c16c738301965504753b03ad1de3425d5451" dependencies = [ "crunchy", "digest 0.9.0", - "subtle 2.5.0", + "subtle 2.4.1", ] [[package]] @@ -5384,9 +5391,9 @@ dependencies = [ [[package]] name = "libz-sys" -version = "1.1.16" +version = "1.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e143b5e666b2695d28f6bca6497720813f699c9602dd7f5cac91008b8ada7f9" +checksum = "fdc53a7799a7496ebc9fd29f31f7df80e83c9bda5299768af5f9e59eeea74647" dependencies = [ "cc", "pkg-config", @@ -5440,9 +5447,9 @@ checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" [[package]] name = "linux-raw-sys" -version = "0.4.13" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] name = "lioness" @@ -5458,9 +5465,9 @@ dependencies = [ [[package]] name = "lock_api" -version = "0.4.11" +version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" dependencies = [ "autocfg", "scopeguard", @@ -5468,9 +5475,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.21" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "lru" @@ -5507,9 +5514,9 @@ dependencies = [ [[package]] name = "lz4" -version = "1.24.0" +version = "1.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e9e2dd86df36ce760a60f6ff6ad526f7ba1f14ba0356f8254fb6905e6494df1" +checksum = "958b4caa893816eea05507c20cfe47574a43d9a697138a7872990bba8a0ece68" dependencies = [ "libc", "lz4-sys", @@ -5517,9 +5524,9 @@ dependencies = [ [[package]] name = "lz4-sys" -version = "1.9.4" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57d27b317e207b10f69f5e75494119e391a96f48861ae870d1da6edac98ca900" +checksum = "109de74d5d2353660401699a4174a4ff23fcc649caf553df71933c7fb45ad868" dependencies = [ "cc", "libc", @@ -5536,50 +5543,50 @@ dependencies = [ [[package]] name = "macro_magic" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e03844fc635e92f3a0067e25fa4bf3e3dbf3f2927bf3aa01bb7bc8f1c428949d" +checksum = "cc33f9f0351468d26fbc53d9ce00a096c8522ecb42f19b50f34f2c422f76d21d" dependencies = [ "macro_magic_core", "macro_magic_macros", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "macro_magic_core" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "468155613a44cfd825f1fb0ffa532b018253920d404e6fca1e8d43155198a46d" +checksum = "1687dc887e42f352865a393acae7cf79d98fab6351cde1f58e9e057da89bf150" dependencies = [ "const-random", - "derive-syn-parse 0.1.5", + "derive-syn-parse 0.2.0", "macro_magic_core_macros", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "macro_magic_core_macros" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ea73aa640dc01d62a590d48c0c3521ed739d53b27f919b25c3551e233481654" +checksum = "b02abfe41815b5bd98dbd4260173db2c116dda171dc0fe7838cb206333b83308" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "macro_magic_macros" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef9d79ae96aaba821963320eb2b6e34d17df1e5a83d8a1985c29cc5be59577b3" +checksum = "73ea28ee64b88876bf45277ed9a5817c1817df061a74f2b988971a12570e5869" dependencies = [ "macro_magic_core", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -5620,9 +5627,9 @@ checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5" [[package]] name = "matrixmultiply" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7574c1cf36da4798ab73da5b215bbf444f50718207754cb522201d78d1cd0ff2" +checksum = "9380b911e3e96d10c1f415da0876389aaf1b56759054eeb0de7df940c456ba1a" dependencies = [ "autocfg", "rawpointer", @@ -5630,9 +5637,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.7.1" +version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "memfd" @@ -5640,7 +5647,7 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2cffa4ad52c6f791f4f8b15f0c05f9824b2ced1160e88cc393d64fff9a8ac64" dependencies = [ - "rustix 0.38.32", + "rustix 0.38.34", ] [[package]] @@ -5710,22 +5717,23 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "miniz_oxide" -version = "0.7.2" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" dependencies = [ "adler", ] [[package]] name = "mio" -version = "0.8.11" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" dependencies = [ + "hermit-abi 0.3.9", "libc", "wasi 0.11.0+wasi-snapshot-preview1", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] @@ -5739,16 +5747,16 @@ dependencies = [ "bitflags 1.3.2", "blake2 0.10.6", "c2-chacha", - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "either", "hashlink", "lioness", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "rand_chacha 0.3.1", "rand_distr", - "subtle 2.5.0", + "subtle 2.4.1", "thiserror", "zeroize", ] @@ -5857,7 +5865,7 @@ dependencies = [ "blake3", "core2", "digest 0.10.7", - "multihash-derive 0.8.0", + "multihash-derive 0.8.1", "sha2 0.10.8", "sha3", "unsigned-varint", @@ -5871,7 +5879,7 @@ checksum = "cfd8a792c1694c6da4f68db0a9d707c72bd260994da179e6030a5dcee00bb815" dependencies = [ "core2", "digest 0.10.7", - "multihash-derive 0.8.0", + "multihash-derive 0.8.1", "sha2 0.10.8", "unsigned-varint", ] @@ -5908,16 +5916,16 @@ dependencies = [ [[package]] name = "multihash-derive" -version = "0.8.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc076939022111618a5026d3be019fd8b366e76314538ff9a1b59ffbcbf98bcd" +checksum = "1d6d4752e6230d8ef7adf7bd5d8c4b1f6561c1014c5ba9a37445ccefe18aa1db" dependencies = [ - "proc-macro-crate 1.3.1", + "proc-macro-crate 1.1.3", "proc-macro-error", "proc-macro2", "quote", "syn 1.0.109", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -5933,16 +5941,16 @@ dependencies = [ [[package]] name = "multihash-derive-impl" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d38685e08adb338659871ecfc6ee47ba9b22dcc8abcf6975d379cc49145c3040" +checksum = "3958713ce794e12f7c6326fac9aa274c68d74c4881dd37b3e2662b8a2046bb19" dependencies = [ - "proc-macro-crate 1.3.1", + "proc-macro-crate 2.0.0", "proc-macro-error", "proc-macro2", "quote", - "syn 1.0.109", - "synstructure", + "syn 2.0.74", + "synstructure 0.13.1", ] [[package]] @@ -5967,9 +5975,9 @@ dependencies = [ [[package]] name = "nalgebra" -version = "0.32.4" +version = "0.32.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4541eb06dce09c0241ebbaab7102f0a01a0c8994afed2e5d0d66775016e25ac2" +checksum = "7b5c17de023a86f59ed79891b2e5d5a94c705dbe904a5b5c9c952ea6221b03e4" dependencies = [ "approx", "matrixmultiply", @@ -5983,13 +5991,13 @@ dependencies = [ [[package]] name = "nalgebra-macros" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91761aed67d03ad966ef783ae962ef9bbaca728d2dd7ceb7939ec110fffad998" +checksum = "254a5372af8fc138e36684761d3c0cdb758a4410e938babcff1c860ce14ddbfc" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.74", ] [[package]] @@ -6062,9 +6070,9 @@ dependencies = [ [[package]] name = "netlink-sys" -version = "0.8.5" +version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6471bf08e7ac0135876a9581bf3217ef0333c191c128d34878079f42ee150411" +checksum = "416060d346fbaf1f23f9512963e3e878f1a78e707cb699ba9215761754244307" dependencies = [ "bytes", "futures", @@ -6090,7 +6098,7 @@ version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "cfg-if", "libc", ] @@ -6131,20 +6139,19 @@ checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" [[package]] name = "num-bigint" -version = "0.4.4" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" dependencies = [ - "autocfg", "num-integer", "num-traits", ] [[package]] name = "num-complex" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" dependencies = [ "num-traits", ] @@ -6176,11 +6183,10 @@ dependencies = [ [[package]] name = "num-rational" -version = "0.4.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" dependencies = [ - "autocfg", "num-bigint", "num-integer", "num-traits", @@ -6188,9 +6194,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", "libm", @@ -6202,7 +6208,7 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.9", "libc", ] @@ -6226,9 +6232,9 @@ dependencies = [ [[package]] name = "object" -version = "0.32.2" +version = "0.36.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" +checksum = "27b64972346851a39438c60b341ebc01bba47464ae329e55cf343eb93964efd9" dependencies = [ "memchr", ] @@ -6274,9 +6280,9 @@ checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" [[package]] name = "orchestra" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2356622ffdfe72362a45a1e5e87bb113b8327e596e39b91f11f0ef4395c8da79" +checksum = "92829eef0328a3d1cd22a02c0e51deb92a5362df3e7d21a4e9bdc38934694e66" dependencies = [ "async-trait", "dyn-clonable", @@ -6291,15 +6297,15 @@ dependencies = [ [[package]] name = "orchestra-proc-macro" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eedb646674596266dc9bb2b5c7eea7c36b32ecc7777eba0d510196972d72c4fd" +checksum = "1344346d5af32c95bbddea91b18a88cc83eac394192d20ef2fc4c40a74332355" dependencies = [ - "expander 2.1.0", - "indexmap 2.2.6", + "expander 2.2.1", + "indexmap 2.3.0", "itertools 0.11.0", "petgraph", - "proc-macro-crate 1.3.1", + "proc-macro-crate 3.1.0", "proc-macro2", "quote", "syn 1.0.109", @@ -6560,7 +6566,7 @@ version = "29.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d334f24d3c0c016d16aa87d069485847d622e8ebebace18ec5cf56609ca3a67" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "binary-merkle-tree", "frame-support", "frame-system", @@ -6802,7 +6808,7 @@ checksum = "3163c6bc21b55a0ccb74c546ba784d9c9e69beb9240c059d28a3052f4cbce509" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -7470,7 +7476,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -7849,7 +7855,7 @@ dependencies = [ "log", "lz4", "memmap2 0.5.10", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "siphasher", "snap", @@ -7901,7 +7907,7 @@ dependencies = [ "impl-trait-for-tuples", "lru 0.8.1", "parity-util-mem-derive", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "primitive-types", "smallvec", "winapi", @@ -7915,7 +7921,7 @@ checksum = "f557c32c6d268a07c921471619c0295f5efad3a0e76d4f97a05c091a51d110b2" dependencies = [ "proc-macro2", "syn 1.0.109", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -7943,12 +7949,12 @@ dependencies = [ [[package]] name = "parking_lot" -version = "0.12.1" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" dependencies = [ "lock_api", - "parking_lot_core 0.9.9", + "parking_lot_core 0.9.10", ] [[package]] @@ -7967,15 +7973,15 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.9.9" +version = "0.9.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ "cfg-if", "libc", - "redox_syscall 0.4.1", + "redox_syscall 0.5.3", "smallvec", - "windows-targets 0.48.5", + "windows-targets 0.52.6", ] [[package]] @@ -7987,7 +7993,7 @@ checksum = "7924d1d0ad836f665c9065e26d016c673ece3993f30d340068b16f282afc1156" [[package]] name = "paseo-runtime" version = "1.2.5" -source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#e7265b10b28d8b82c3146e72c98895dac2b55729" +source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#2fa66a19eef96bd2c079385057fab2564d09b751" dependencies = [ "binary-merkle-tree", "frame-benchmarking", @@ -8082,7 +8088,7 @@ dependencies = [ [[package]] name = "paseo-runtime-constants" version = "1.0.0" -source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#e7265b10b28d8b82c3146e72c98895dac2b55729" +source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#2fa66a19eef96bd2c079385057fab2564d09b751" dependencies = [ "frame-support", "polkadot-primitives", @@ -8096,9 +8102,9 @@ dependencies = [ [[package]] name = "paste" -version = "1.0.14" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "pbkdf2" @@ -8106,7 +8112,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d95f5254224e617595d2cc3cc73ff0a5eaf2637519e25f03388154e9378b6ffa" dependencies = [ - "crypto-mac 0.11.0", + "crypto-mac 0.11.1", ] [[package]] @@ -8141,9 +8147,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pest" -version = "2.7.8" +version = "2.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56f8023d0fb78c8e03784ea1c7f3fa36e68a723138990b8d5a47d916b651e7a8" +checksum = "cd53dff83f26735fdc1ca837098ccf133605d794cdae66acfc2bfac3ec809d95" dependencies = [ "memchr", "thiserror", @@ -8152,9 +8158,9 @@ dependencies = [ [[package]] name = "pest_derive" -version = "2.7.8" +version = "2.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0d24f72393fd16ab6ac5738bc33cdb6a9aa73f8b902e8fe29cf4e67d7dd1026" +checksum = "2a548d2beca6773b1c244554d36fcf8548a8a58e74156968211567250e48e49a" dependencies = [ "pest", "pest_generator", @@ -8162,22 +8168,22 @@ dependencies = [ [[package]] name = "pest_generator" -version = "2.7.8" +version = "2.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdc17e2a6c7d0a492f0158d7a4bd66cc17280308bbaff78d5bef566dca35ab80" +checksum = "3c93a82e8d145725dcbaf44e5ea887c8a869efdcc28706df2d08c69e17077183" dependencies = [ "pest", "pest_meta", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "pest_meta" -version = "2.7.8" +version = "2.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "934cd7631c050f4674352a6e835d5f6711ffbfb9345c2fc0107155ac495ae293" +checksum = "a941429fea7e08bedec25e4f6785b6ffaacc6b755da98df5ef3e7dcf4a124c4f" dependencies = [ "once_cell", "pest", @@ -8186,12 +8192,12 @@ dependencies = [ [[package]] name = "petgraph" -version = "0.6.4" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" +checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" dependencies = [ "fixedbitset", - "indexmap 2.2.6", + "indexmap 2.3.0", ] [[package]] @@ -8211,7 +8217,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -8222,9 +8228,9 @@ checksum = "257b64915a082f7811703966789728173279bdebb956b143dbcd23f6f970a777" [[package]] name = "pin-project-lite" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" [[package]] name = "pin-utils" @@ -8234,12 +8240,12 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "piper" -version = "0.2.1" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "668d31b1c4eba19242f2088b2bf3316b82ca31082a8335764db4e083db7485d4" +checksum = "96c8c490f422ef9a4efd2cb5b42b76c8613d7e7dfc1caf667b8a3350a5acc066" dependencies = [ "atomic-waker", - "fastrand 2.0.2", + "fastrand 2.1.0", "futures-io", ] @@ -8259,12 +8265,6 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" -[[package]] -name = "platforms" -version = "3.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "626dec3cac7cc0e1577a2ec3fc496277ec2baa084bebad95bb6fdbfae235f84c" - [[package]] name = "polkadot-approval-distribution" version = "8.0.0" @@ -8426,7 +8426,7 @@ dependencies = [ "fatality", "futures", "futures-timer", - "indexmap 2.2.6", + "indexmap 2.3.0", "parity-scale-codec", "polkadot-erasure-coding", "polkadot-node-network-protocol", @@ -8492,7 +8492,7 @@ dependencies = [ "fatality", "futures", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-node-metrics", "polkadot-node-network-protocol", "polkadot-node-subsystem", @@ -8753,7 +8753,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3bbb1b5f4b966f21a0336e94c0a0222958d2f3cba451da1157af271d07f9748" dependencies = [ "always-assert", - "array-bytes 6.2.2", + "array-bytes 6.2.3", "blake3", "cfg-if", "futures", @@ -8851,7 +8851,7 @@ dependencies = [ "log", "mick-jaeger", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-node-primitives", "polkadot-primitives", "sc-network", @@ -8984,7 +8984,7 @@ dependencies = [ "kvdb", "parity-db", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "pin-project", "polkadot-node-jaeger", "polkadot-node-metrics", @@ -9015,7 +9015,7 @@ dependencies = [ "futures", "futures-timer", "orchestra", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-node-metrics", "polkadot-node-network-protocol", "polkadot-node-primitives", @@ -9250,7 +9250,7 @@ dependencies = [ "pallet-transaction-payment-rpc-runtime-api", "parity-db", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "polkadot-approval-distribution", "polkadot-availability-bitfield-distribution", "polkadot-availability-distribution", @@ -9353,7 +9353,7 @@ dependencies = [ "fatality", "futures", "futures-timer", - "indexmap 2.2.6", + "indexmap 2.3.0", "parity-scale-codec", "polkadot-node-network-protocol", "polkadot-node-primitives", @@ -9391,7 +9391,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6380dbe1fb03ecc74ad55d841cfc75480222d153ba69ddcb00977866cbdabdb8" dependencies = [ "polkavm-derive-impl", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -9403,7 +9403,7 @@ dependencies = [ "polkavm-common", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -9418,23 +9418,23 @@ dependencies = [ "concurrent-queue", "libc", "log", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "windows-sys 0.48.0", ] [[package]] name = "polling" -version = "3.6.0" +version = "3.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0c976a60b2d7e99d6f229e414670a9b85d13ac305cc6d1e9c134de58c5aaaf6" +checksum = "cc2790cd301dec6cd3b7a025e4815cf825724a51c98dccfe6a3e55f05ffb6511" dependencies = [ "cfg-if", "concurrent-queue", - "hermit-abi", - "pin-project-lite 0.2.13", - "rustix 0.38.32", + "hermit-abi 0.4.0", + "pin-project-lite 0.2.14", + "rustix 0.38.34", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -9557,7 +9557,7 @@ dependencies = [ "cumulus-primitives-aura", "cumulus-primitives-core", "cumulus-primitives-utility", - "env_logger 0.11.3", + "env_logger 0.11.5", "frame-benchmarking", "frame-executive", "frame-support", @@ -9631,7 +9631,7 @@ dependencies = [ "cumulus-primitives-aura", "cumulus-primitives-core", "cumulus-primitives-utility", - "env_logger 0.11.3", + "env_logger 0.11.5", "frame-benchmarking", "frame-executive", "frame-support", @@ -9693,9 +9693,9 @@ dependencies = [ [[package]] name = "portable-atomic" -version = "1.6.0" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" +checksum = "da544ee218f0d287a911e9c99a39a8c9bc8fcad3cb8db5959940044ecfc67265" [[package]] name = "powerfmt" @@ -9705,9 +9705,12 @@ checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" [[package]] name = "ppv-lite86" -version = "0.2.17" +version = "0.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] [[package]] name = "predicates" @@ -9725,35 +9728,25 @@ dependencies = [ [[package]] name = "predicates-core" -version = "1.0.6" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b794032607612e7abeb4db69adb4e33590fa6cf1149e95fd7cb00e634b92f174" +checksum = "ae8177bee8e75d6846599c6b9ff679ed51e882816914eec639944d7c9aa11931" [[package]] name = "predicates-tree" -version = "1.0.9" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "368ba315fb8c5052ab692e68a0eefec6ec57b23a36959c14496f0b0df2c0cecf" +checksum = "41b740d195ed3166cd147c8047ec98db0e22ec019eb8eeb76d343b795304fb13" dependencies = [ "predicates-core", "termtree", ] -[[package]] -name = "prettier-please" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22020dfcf177fcc7bf5deaf7440af371400c67c0de14c399938d8ed4fb4645d3" -dependencies = [ - "proc-macro2", - "syn 2.0.55", -] - [[package]] name = "prettyplease" -version = "0.1.11" +version = "0.1.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f28f53e8b192565862cf99343194579a022eb9c7dd3a8d03134734803c7b3125" +checksum = "6c8646e95016a7a6c4adea95bafa8a16baab64b583356217f2c85db4a39d9a86" dependencies = [ "proc-macro2", "syn 1.0.109", @@ -9761,12 +9754,12 @@ dependencies = [ [[package]] name = "prettyplease" -version = "0.2.17" +version = "0.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d3928fb5db768cb86f891ff014f0144589297e3c6a1aba6ed7cecfdace270c7" +checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e" dependencies = [ "proc-macro2", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -9802,12 +9795,21 @@ dependencies = [ [[package]] name = "proc-macro-crate" -version = "1.3.1" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" +checksum = "e17d47ce914bf4de440332250b0edd23ce48c005f59fab39d3335866b114f11a" dependencies = [ - "once_cell", - "toml_edit 0.19.15", + "thiserror", + "toml 0.5.11", +] + +[[package]] +name = "proc-macro-crate" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e8366a6159044a37876a2b9817124296703c586a5c92e2c53751fa06d8d43e8" +dependencies = [ + "toml_edit 0.20.7", ] [[package]] @@ -9851,29 +9853,29 @@ checksum = "834da187cfe638ae8abb0203f0b33e5ccdb02a28e7199f2f47b3e2754f50edca" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "proc-macro2" -version = "1.0.79" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] [[package]] name = "prometheus" -version = "0.13.3" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "449811d15fbdf5ceb5c1144416066429cf82316e2ec8ce0c1f6f8a02e7bbcf8c" +checksum = "3d33c28a30771f7f96db69893f78b857f7450d7e0237e9c8fc6427a81bae7ed1" dependencies = [ "cfg-if", "fnv", "lazy_static", "memchr", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "thiserror", ] @@ -9885,7 +9887,7 @@ checksum = "5d6fa99d535dd930d1249e6c79cb3c2915f9172a540fe2b02a4c8f9ca954721e" dependencies = [ "dtoa", "itoa", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "prometheus-client-derive-encode", ] @@ -9897,7 +9899,7 @@ checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -9912,12 +9914,12 @@ dependencies = [ [[package]] name = "prost" -version = "0.12.3" +version = "0.12.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "146c289cda302b98a28d40c8b3b90498d6e526dd24ac2ecea73e4e491685b94a" +checksum = "deb1435c188b76130da55f17a466d252ff7b1418b2ad3e037d127b94e3411f29" dependencies = [ "bytes", - "prost-derive 0.12.3", + "prost-derive 0.12.6", ] [[package]] @@ -9933,7 +9935,7 @@ dependencies = [ "log", "multimap", "petgraph", - "prettyplease 0.1.11", + "prettyplease 0.1.25", "prost 0.11.9", "prost-types", "regex", @@ -9957,15 +9959,15 @@ dependencies = [ [[package]] name = "prost-derive" -version = "0.12.3" +version = "0.12.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efb6c9a1dd1def8e2124d17e83a20af56f1570d6c2d2bd9e266ccb768df3840e" +checksum = "81bddcdb20abf9501610992b6759a4c888aef7d1a7247ef75e2404275ac24af1" dependencies = [ "anyhow", - "itertools 0.11.0", + "itertools 0.12.1", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -10045,9 +10047,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.35" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -10104,7 +10106,7 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ - "getrandom 0.2.12", + "getrandom 0.2.15", ] [[package]] @@ -10175,20 +10177,20 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.4.1" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" dependencies = [ - "bitflags 1.3.2", + "bitflags 2.6.0", ] [[package]] name = "redox_users" -version = "0.4.4" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4" +checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" dependencies = [ - "getrandom 0.2.12", + "getrandom 0.2.15", "libredox", "thiserror", ] @@ -10207,22 +10209,22 @@ dependencies = [ [[package]] name = "ref-cast" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4846d4c50d1721b1a3bef8af76924eef20d5e723647333798c1b519b3a9473f" +checksum = "ccf0a6f84d5f1d581da8b41b47ec8600871962f2a528115b542b362d4b744931" dependencies = [ "ref-cast-impl", ] [[package]] name = "ref-cast-impl" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fddb4f8d99b0a2ebafc65a87a69a7b9875e4b1ae1f00db265d300ef7f28bccc" +checksum = "bcc303e793d3734489387d205e9b186fac9c6cfacedd98cbb2e8a5943595f3e6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -10239,14 +10241,14 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.4" +version = "1.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" +checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.4.6", - "regex-syntax 0.8.2", + "regex-automata 0.4.7", + "regex-syntax 0.8.4", ] [[package]] @@ -10260,13 +10262,13 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" dependencies = [ "aho-corasick", "memchr", - "regex-syntax 0.8.2", + "regex-syntax 0.8.4", ] [[package]] @@ -10277,9 +10279,9 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] name = "regex-syntax" -version = "0.8.2" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" [[package]] name = "resolv-conf" @@ -10298,7 +10300,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2" dependencies = [ "hmac 0.12.1", - "subtle 2.5.0", + "subtle 2.4.1", ] [[package]] @@ -10324,7 +10326,7 @@ checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" dependencies = [ "cc", "cfg-if", - "getrandom 0.2.12", + "getrandom 0.2.15", "libc", "spin 0.9.8", "untrusted 0.9.0", @@ -10519,9 +10521,9 @@ dependencies = [ [[package]] name = "rustc-demangle" -version = "0.1.23" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "rustc-hash" @@ -10541,7 +10543,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" dependencies = [ - "semver 1.0.22", + "semver 1.0.23", ] [[package]] @@ -10583,14 +10585,14 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.32" +version = "0.38.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89" +checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "errno", "libc", - "linux-raw-sys 0.4.13", + "linux-raw-sys 0.4.14", "windows-sys 0.52.0", ] @@ -10608,9 +10610,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.21.10" +version = "0.21.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9d5a6813c0759e4609cd494e8e725babae6a2ca7b62a5536a13daaec6fcb7ba" +checksum = "3f56a14d1f48b391359b22f731fd4bd7e43c97f3c50eee276f3aa09c94784d3e" dependencies = [ "log", "ring 0.17.8", @@ -10651,9 +10653,9 @@ dependencies = [ [[package]] name = "rustversion" -version = "1.0.14" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" [[package]] name = "ruzstd" @@ -10679,15 +10681,15 @@ dependencies = [ [[package]] name = "ryu" -version = "1.0.17" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "safe_arch" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f398075ce1e6a179b46f51bd88d0598b92b00d3551f1a2d4ac49e771b56ac354" +checksum = "c3460605018fdc9612bce72735cba0d27efbcd9904780d44c7e3a9948f96148a" dependencies = [ "bytemuck", ] @@ -10728,7 +10730,7 @@ dependencies = [ "multihash 0.18.1", "multihash-codetable", "parity-scale-codec", - "prost 0.12.3", + "prost 0.12.6", "prost-build", "rand", "sc-client-api", @@ -10788,7 +10790,7 @@ version = "28.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f73880050f8b04fed7f6301279ef3899df13a3891bd06156d56f9a1c50fefba" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "docify", "log", "memmap2 0.9.4", @@ -10818,7 +10820,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -10827,7 +10829,7 @@ version = "0.37.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8a284c10ea92b1fe789b9f0e5815d393f3a1e3bf6a4adaa884f24e36143b83b" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "bip39", "chrono", "clap", @@ -10873,7 +10875,7 @@ dependencies = [ "futures", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-executor", "sc-transaction-pool-api", "sc-utils", @@ -10905,7 +10907,7 @@ dependencies = [ "log", "parity-db", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-client-api", "sc-state-db", "schnellru", @@ -10930,7 +10932,7 @@ dependencies = [ "libp2p-identity", "log", "mockall", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-client-api", "sc-utils", "serde", @@ -10988,7 +10990,7 @@ dependencies = [ "num-rational", "num-traits", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-client-api", "sc-consensus", "sc-consensus-epochs", @@ -11040,14 +11042,14 @@ version = "14.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e9ce3ee15eff7fa642791966d427f185184df3c7f4e58893705f3e7781da8ef5" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "async-channel 1.9.0", "async-trait", "fnv", "futures", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-client-api", "sc-consensus", "sc-network", @@ -11081,7 +11083,7 @@ dependencies = [ "jsonrpsee", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-consensus-beefy", "sc-rpc", "serde", @@ -11112,7 +11114,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ae91e5b5a120be4d13a59eaf94fd85d7c7af528482b8e21d861fa1167df3083" dependencies = [ "ahash 0.8.11", - "array-bytes 6.2.2", + "array-bytes 6.2.3", "async-trait", "dyn-clone", "finality-grandpa", @@ -11121,7 +11123,7 @@ dependencies = [ "futures-timer", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "sc-block-builder", "sc-chain-spec", @@ -11201,7 +11203,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa2ac6c356538d67987bbb867e11a12a84ba87250c70fd50005b6d74f570a4f7" dependencies = [ "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-executor-common", "sc-executor-wasmtime", "schnellru", @@ -11240,7 +11242,7 @@ dependencies = [ "cfg-if", "libc", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rustix 0.36.17", "sc-allocator", "sc-executor-common", @@ -11273,8 +11275,8 @@ version = "26.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cc4f6a558dd23e3bae2e9f195da822465258b9aaf211c34360d7f6efb944e54" dependencies = [ - "array-bytes 6.2.2", - "parking_lot 0.12.1", + "array-bytes 6.2.3", + "parking_lot 0.12.3", "serde_json", "sp-application-crypto", "sp-core", @@ -11299,7 +11301,7 @@ dependencies = [ "mixnet", "multiaddr", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-client-api", "sc-network", "sc-transaction-pool-api", @@ -11318,7 +11320,7 @@ version = "0.35.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f231c7d5e749ec428b4cfa669d759ae76cd3da4f50d7352a2d711acdc7532891" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "async-channel 1.9.0", "async-trait", "asynchronous-codec", @@ -11333,7 +11335,7 @@ dependencies = [ "log", "mockall", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "partial_sort", "pin-project", "rand", @@ -11367,7 +11369,7 @@ dependencies = [ "futures", "libp2p-identity", "log", - "prost 0.12.3", + "prost 0.12.6", "prost-build", "sc-client-api", "sc-network", @@ -11421,13 +11423,13 @@ version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38d84ef0b212c775f58e0304ec09166089f6b09afddf559b7c2b5702933b3be4" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "async-channel 1.9.0", "futures", "libp2p-identity", "log", "parity-scale-codec", - "prost 0.12.3", + "prost 0.12.6", "prost-build", "sc-client-api", "sc-network", @@ -11443,7 +11445,7 @@ version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9aa9377059deece4e7d419d9ec456f657268c0c603e1cf98df4a920f6da83461" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "async-channel 1.9.0", "async-trait", "fork-tree", @@ -11453,7 +11455,7 @@ dependencies = [ "log", "mockall", "parity-scale-codec", - "prost 0.12.3", + "prost 0.12.6", "prost-build", "sc-client-api", "sc-consensus", @@ -11480,7 +11482,7 @@ version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "16c9cad4baf348725bd82eadcd1747fc112ec49c76b863755ce79c588fa73fe4" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "futures", "libp2p", "log", @@ -11500,7 +11502,7 @@ version = "30.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aee89f2abd406356bfd688bd7a51155dc963259e4b752bb85d1f8a061a194fd" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "bytes", "fnv", "futures", @@ -11512,7 +11514,7 @@ dependencies = [ "num_cpus", "once_cell", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "sc-client-api", "sc-network", @@ -11549,7 +11551,7 @@ dependencies = [ "jsonrpsee", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-block-builder", "sc-chain-spec", "sc-client-api", @@ -11615,14 +11617,14 @@ version = "0.35.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7f10275c62296a785f6e2ac716521e3b6e0fae470416fdf86491cbbfcc2e23d" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "futures", "futures-util", "hex", "jsonrpsee", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-chain-spec", "sc-client-api", "sc-rpc", @@ -11654,7 +11656,7 @@ dependencies = [ "jsonrpsee", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "pin-project", "rand", "sc-chain-spec", @@ -11712,7 +11714,7 @@ checksum = "aa842052c41ad379eaecdfddc0d5c953d57e311ae688233f68f461b91d38da0a" dependencies = [ "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sp-core", ] @@ -11782,7 +11784,7 @@ dependencies = [ "futures", "libp2p", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "pin-project", "rand", "sc-utils", @@ -11805,7 +11807,7 @@ dependencies = [ "libc", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "regex", "rustc-hash", "sc-client-api", @@ -11832,7 +11834,7 @@ dependencies = [ "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -11847,7 +11849,7 @@ dependencies = [ "linked-hash-map", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sc-client-api", "sc-transaction-pool-api", "sc-utils", @@ -11891,16 +11893,16 @@ dependencies = [ "futures-timer", "lazy_static", "log", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "prometheus", "sp-arithmetic", ] [[package]] name = "scale-info" -version = "2.11.1" +version = "2.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "788745a868b0e751750388f4e6546eb921ef714a4317fa6954f7cde114eb2eb7" +checksum = "eca070c12893629e2cc820a9761bedf6ce1dcddc9852984d1dc734b8bd9bd024" dependencies = [ "bitvec", "cfg-if", @@ -11912,11 +11914,11 @@ dependencies = [ [[package]] name = "scale-info-derive" -version = "2.11.1" +version = "2.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dc2f4e8bc344b9fc3d5f74f72c2e55bfc38d28dc2ebc69c194a3df424e4d9ac" +checksum = "2d35494501194174bda522a32605929eefc9ecf7e0a326c26db1fdd85881eb62" dependencies = [ - "proc-macro-crate 1.3.1", + "proc-macro-crate 3.1.0", "proc-macro2", "quote", "syn 1.0.109", @@ -11933,9 +11935,9 @@ dependencies = [ [[package]] name = "schnellru" -version = "0.2.1" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "772575a524feeb803e5b0fcbc6dd9f367e579488197c94c6e4023aad2305774d" +checksum = "c9a8ef13a93c54d20580de1e5c413e624e53121d42fc7e2c11d10ef7f8b02367" dependencies = [ "ahash 0.8.11", "cfg-if", @@ -11967,13 +11969,13 @@ dependencies = [ "aead", "arrayref", "arrayvec 0.7.4", - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "getrandom_or_panic", "merlin", "rand_core 0.6.4", "serde_bytes", "sha2 0.10.8", - "subtle 2.5.0", + "subtle 2.4.1", "zeroize", ] @@ -12009,7 +12011,7 @@ dependencies = [ "der", "generic-array 0.14.7", "pkcs8", - "subtle 2.5.0", + "subtle 2.4.1", "zeroize", ] @@ -12051,11 +12053,11 @@ dependencies = [ [[package]] name = "security-framework" -version = "2.9.2" +version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de" +checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" dependencies = [ - "bitflags 1.3.2", + "bitflags 2.6.0", "core-foundation", "core-foundation-sys", "libc", @@ -12064,9 +12066,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.9.1" +version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e932934257d3b408ed8f30db49d85ea163bfe74961f017f405b025af298f0c7a" +checksum = "75da29fe9b9b08fe9d6b22b5b4bcbc75d8db3aa31e639aa56bb62e9d46bfceaf" dependencies = [ "core-foundation-sys", "libc", @@ -12083,9 +12085,9 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" dependencies = [ "serde", ] @@ -12098,9 +12100,9 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.197" +version = "1.0.207" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +checksum = "5665e14a49a4ea1b91029ba7d3bca9f299e1f7cfa194388ccc20f14743e784f2" dependencies = [ "serde_derive", ] @@ -12116,40 +12118,41 @@ dependencies = [ [[package]] name = "serde_bytes" -version = "0.11.14" +version = "0.11.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b8497c313fd43ab992087548117643f6fcd935cbf36f176ffda0aacf9591734" +checksum = "387cc504cb06bb40a96c8e04e951fe01854cf6bc921053c954e4a606d9675c6a" dependencies = [ "serde", ] [[package]] name = "serde_derive" -version = "1.0.197" +version = "1.0.207" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +checksum = "6aea2634c86b0e8ef2cfdc0c340baede54ec27b1e46febd7f80dffb2aa44a00e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "serde_json" -version = "1.0.114" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" +checksum = "66ad62847a56b3dba58cc891acd13884b9c61138d330c0d7b6181713d4fce38d" dependencies = [ "itoa", + "memchr", "ryu", "serde", ] [[package]] name = "serde_spanned" -version = "0.6.5" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d" dependencies = [ "serde", ] @@ -12229,9 +12232,9 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "signal-hook-registry" -version = "1.4.1" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" dependencies = [ "libc", ] @@ -12353,7 +12356,7 @@ dependencies = [ "fnv", "futures-lite 1.13.0", "futures-util", - "hashbrown 0.14.3", + "hashbrown 0.14.5", "hex", "hmac 0.12.1", "itertools 0.11.0", @@ -12402,13 +12405,13 @@ dependencies = [ "futures-channel", "futures-lite 1.13.0", "futures-util", - "hashbrown 0.14.3", + "hashbrown 0.14.5", "hex", "itertools 0.11.0", "log", "lru 0.11.1", "no-std-net", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "pin-project", "rand", "rand_chacha 0.3.1", @@ -12436,12 +12439,12 @@ dependencies = [ "aes-gcm", "blake2 0.10.6", "chacha20poly1305", - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "rand_core 0.6.4", "ring 0.17.8", "rustc_version", "sha2 0.10.8", - "subtle 2.5.0", + "subtle 2.4.1", ] [[package]] @@ -12456,9 +12459,9 @@ dependencies = [ [[package]] name = "snowbridge-beacon-primitives" -version = "0.1.0" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60a73ef707257064bc4ecce8323cdb7c30e8ecd1ce74aa89a6e82e81fa8b9970" +checksum = "5404af73550b39022e08e5500b30fba627e109a56407b7e80b08da2305b11bfe" dependencies = [ "byte-slice-cast", "frame-support", @@ -12481,9 +12484,9 @@ dependencies = [ [[package]] name = "snowbridge-core" -version = "0.1.1" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a3e2e3b94bfcfc8f363e21a6c5a1d3c67eb4592ada672c868a3236ad1dd563b" +checksum = "aed4ebefed4c40b9c00e9adf5f02ab2760a7a2dad8bf05110c0013a7a59f4097" dependencies = [ "ethabi-decode", "frame-support", @@ -12577,9 +12580,9 @@ dependencies = [ [[package]] name = "socket2" -version = "0.5.6" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05ffd9c0a93b7543e062e759284fcf5f5e3b098501104bfbdde4d404db792871" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" dependencies = [ "libc", "windows-sys 0.52.0", @@ -12632,11 +12635,11 @@ checksum = "0301e2f77afb450fbf2b093f8b324c7ad88cc82e5e69bd5dc8658a1f068b2a96" dependencies = [ "Inflector", "blake2 0.10.6", - "expander 2.1.0", + "expander 2.2.1", "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -12703,7 +12706,7 @@ dependencies = [ "futures", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "schnellru", "sp-api", "sp-consensus", @@ -12826,7 +12829,7 @@ version = "29.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c33c7a1568175250628567d50c4e1c54a6ac5bc1190413b9be29a9e810cbe73" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "bip39", "bitflags 1.3.2", "blake2 0.10.6", @@ -12843,7 +12846,7 @@ dependencies = [ "log", "merlin", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "paste", "primitive-types", "rand", @@ -12888,7 +12891,7 @@ checksum = "b85d0f1f1e44bd8617eb2a48203ee854981229e3e79e6f468c7175d5fd37489b" dependencies = [ "quote", "sp-crypto-hashing", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -12898,7 +12901,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "722cbecdbf5b94578137dbd07feb51e95f7de221be0c1ff4dcfe0bb4cd986929" dependencies = [ "kvdb", - "parking_lot 0.12.1", + "parking_lot 0.12.3", ] [[package]] @@ -12909,7 +12912,7 @@ checksum = "48d09fa0a5f7299fb81ee25ae3853d26200f7a348148aed6de76be905c007dbe" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -12995,7 +12998,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "444f2d53968b1ce5e908882710ff1f3873fcf3e95f59d57432daf685bbacb959" dependencies = [ "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "sp-core", "sp-externalities", "thiserror", @@ -13154,11 +13157,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfaf6e85b2ec12a4b99cd6d8d57d083e30c94b7f1b0d8f93547121495aae6f0c" dependencies = [ "Inflector", - "expander 2.1.0", + "expander 2.2.1", "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -13201,7 +13204,7 @@ dependencies = [ "hash-db", "log", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "smallvec", "sp-core", @@ -13221,7 +13224,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "309a9ae4e8134bbed8ffc510cf4d461a4a651f9250b556de782cedd876abe1ff" dependencies = [ "aes-gcm", - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "ed25519-dalek", "hkdf", "parity-scale-codec", @@ -13325,7 +13328,7 @@ dependencies = [ "memory-db", "nohash-hasher", "parity-scale-codec", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "scale-info", "schnellru", @@ -13365,7 +13368,7 @@ dependencies = [ "parity-scale-codec", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -13496,7 +13499,7 @@ version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48fa328b87de3466bc38cc9a07244c42c647b7755b81115e1dfeb47cc13fc6e6" dependencies = [ - "array-bytes 6.2.2", + "array-bytes 6.2.3", "bounded-collections", "derivative", "environmental", @@ -13597,15 +13600,15 @@ dependencies = [ "bitflags 1.3.2", "byteorder", "keccak", - "subtle 2.5.0", + "subtle 2.4.1", "zeroize", ] [[package]] name = "strsim" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "strum" @@ -13618,9 +13621,9 @@ dependencies = [ [[package]] name = "strum" -version = "0.25.0" +version = "0.26.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" +checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" [[package]] name = "strum_macros" @@ -13637,15 +13640,15 @@ dependencies = [ [[package]] name = "strum_macros" -version = "0.25.3" +version = "0.26.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23dc1fa9ac9c169a78ba62f0b841814b7abae11bdd047b9c58f893439e309ea0" +checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" dependencies = [ - "heck 0.4.1", + "heck 0.5.0", "proc-macro2", "quote", "rustversion", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -13746,7 +13749,7 @@ dependencies = [ "sp-maybe-compressed-blob", "strum 0.24.1", "tempfile", - "toml 0.8.12", + "toml 0.8.19", "walkdir", "wasm-opt", ] @@ -13759,9 +13762,9 @@ checksum = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" [[package]] name = "subtle" -version = "2.5.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" [[package]] name = "subtle-ng" @@ -13782,9 +13785,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.55" +version = "2.0.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "002a1b3dbf967edfafc32655d0f377ab0bb7b994aa1d32c8cc7e9b8bf3ebb8f0" +checksum = "1fceb41e3d546d0bd83421d3409b1460cc7444cd389341a4c880fe7a042cb3d7" dependencies = [ "proc-macro2", "quote", @@ -13803,6 +13806,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "synstructure" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.74", +] + [[package]] name = "system-configuration" version = "0.5.1" @@ -13827,7 +13841,7 @@ dependencies = [ [[package]] name = "system-parachains-constants" version = "1.0.0" -source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#e7265b10b28d8b82c3146e72c98895dac2b55729" +source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#2fa66a19eef96bd2c079385057fab2564d09b751" dependencies = [ "frame-support", "parachains-common", @@ -13846,20 +13860,21 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] name = "target-lexicon" -version = "0.12.14" +version = "0.12.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1fc403891a21bcfb7c37834ba66a547a8f402146eba7265b5a6d88059c9ff2f" +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" [[package]] name = "tempfile" -version = "3.10.1" +version = "3.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" +checksum = "04cbcdd0c794ebb0d4cf35e88edd2f7d2c4c3e9a5a6dab322839b321c6a87a64" dependencies = [ "cfg-if", - "fastrand 2.0.2", - "rustix 0.38.32", - "windows-sys 0.52.0", + "fastrand 2.1.0", + "once_cell", + "rustix 0.38.34", + "windows-sys 0.59.0", ] [[package]] @@ -13877,7 +13892,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" dependencies = [ - "rustix 0.38.32", + "rustix 0.38.34", "windows-sys 0.48.0", ] @@ -13889,9 +13904,9 @@ checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" [[package]] name = "thiserror" -version = "1.0.58" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" dependencies = [ "thiserror-impl", ] @@ -13913,18 +13928,18 @@ checksum = "e4c60d69f36615a077cc7663b9cb8e42275722d23e58a7fa3d2c7f2915d09d04" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "thiserror-impl" -version = "1.0.58" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -13988,9 +14003,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.34" +version = "0.3.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" dependencies = [ "deranged", "itoa", @@ -14009,9 +14024,9 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" dependencies = [ "num-conv", "time-core", @@ -14028,9 +14043,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.6.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" dependencies = [ "tinyvec_macros", ] @@ -14043,32 +14058,31 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.37.0" +version = "1.39.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787" +checksum = "daa4fb1bc778bd6f04cbfc4bb2d06a7396a8f299dc33ea1900cedaa316f467b1" dependencies = [ "backtrace", "bytes", "libc", "mio", - "num_cpus", - "parking_lot 0.12.1", - "pin-project-lite 0.2.13", + "parking_lot 0.12.3", + "pin-project-lite 0.2.14", "signal-hook-registry", - "socket2 0.5.6", + "socket2 0.5.7", "tokio-macros", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] name = "tokio-macros" -version = "2.2.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -14088,7 +14102,7 @@ version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" dependencies = [ - "rustls 0.21.10", + "rustls 0.21.12", "tokio", ] @@ -14099,24 +14113,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "267ac89e0bec6e691e5813911606935d77c476ff49024f98abcea3e7b15e37af" dependencies = [ "futures-core", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "tokio", "tokio-util", ] [[package]] name = "tokio-util" -version = "0.7.10" +version = "0.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15" +checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" dependencies = [ "bytes", "futures-core", "futures-io", "futures-sink", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "tokio", - "tracing", ] [[package]] @@ -14130,32 +14143,32 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.12" +version = "0.8.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3" +checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e" dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.9", + "toml_edit 0.22.20", ] [[package]] name = "toml_datetime" -version = "0.6.5" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" dependencies = [ "serde", ] [[package]] name = "toml_edit" -version = "0.19.15" +version = "0.20.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" +checksum = "70f427fce4d84c72b5b732388bf4a9f4531b53f74e2887e3ecb2481f68f66d81" dependencies = [ - "indexmap 2.2.6", + "indexmap 2.3.0", "toml_datetime", "winnow 0.5.40", ] @@ -14166,22 +14179,22 @@ version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" dependencies = [ - "indexmap 2.2.6", + "indexmap 2.3.0", "toml_datetime", "winnow 0.5.40", ] [[package]] name = "toml_edit" -version = "0.22.9" +version = "0.22.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e40bb779c5187258fd7aad0eb68cb8706a0a81fa712fbea808ab43c4b8374c4" +checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" dependencies = [ - "indexmap 2.2.6", + "indexmap 2.3.0", "serde", "serde_spanned", "toml_datetime", - "winnow 0.6.5", + "winnow 0.6.18", ] [[package]] @@ -14193,7 +14206,7 @@ dependencies = [ "futures-core", "futures-util", "pin-project", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "tower-layer", "tower-service", "tracing", @@ -14205,14 +14218,14 @@ version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "bytes", "futures-core", "futures-util", "http", "http-body", "http-range-header", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "tower-layer", "tower-service", ] @@ -14236,7 +14249,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ "log", - "pin-project-lite 0.2.13", + "pin-project-lite 0.2.14", "tracing-attributes", "tracing-core", ] @@ -14249,7 +14262,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -14290,11 +14303,11 @@ version = "5.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f074568687ffdfd0adb6005aa8d1d96840197f2c159f80471285f08694cf0ce" dependencies = [ - "expander 2.1.0", + "expander 2.2.1", "proc-macro-crate 3.1.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -14427,7 +14440,7 @@ dependencies = [ "ipconfig", "lazy_static", "lru-cache", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "resolv-conf", "smallvec", "thiserror", @@ -14544,9 +14557,9 @@ dependencies = [ [[package]] name = "unicode-width" -version = "0.1.11" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" +checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" [[package]] name = "unicode-xid" @@ -14561,7 +14574,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" dependencies = [ "crypto-common", - "subtle 2.5.0", + "subtle 2.4.1", ] [[package]] @@ -14590,9 +14603,9 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "url" -version = "2.5.0" +version = "2.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" dependencies = [ "form_urlencoded", "idna 0.5.0", @@ -14601,9 +14614,9 @@ dependencies = [ [[package]] name = "utf8parse" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "valuable" @@ -14619,9 +14632,9 @@ checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" [[package]] name = "version_check" -version = "0.9.4" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" [[package]] name = "void" @@ -14631,9 +14644,9 @@ checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" [[package]] name = "w3f-bls" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7335e4c132c28cc43caef6adb339789e599e39adbe78da0c4d547fad48cbc331" +checksum = "9c5da5fa2c6afa2c9158eaa7cd9aee249765eb32b5fb0c63ad8b9e79336a47ec" dependencies = [ "ark-bls12-377", "ark-bls12-381", @@ -14655,9 +14668,9 @@ dependencies = [ [[package]] name = "waker-fn" -version = "1.1.1" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3c4517f54858c779bbcbf228f4fca63d121bf85fbecb2dc578cdf4a39395690" +checksum = "317211a0dc0ceedd78fb2ca9a44aed3d7b9b26f81870d485c07122b4350673b7" [[package]] name = "walkdir" @@ -14701,34 +14714,35 @@ dependencies = [ [[package]] name = "wasm-bindgen" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" +checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" dependencies = [ "cfg-if", + "once_cell", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" +checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-futures" -version = "0.4.42" +version = "0.4.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" +checksum = "61e9300f63a621e96ed275155c108eb6f843b6a26d053f122ab69724559dc8ed" dependencies = [ "cfg-if", "js-sys", @@ -14738,9 +14752,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" +checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -14748,22 +14762,22 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" +checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" [[package]] name = "wasm-instrument" @@ -14776,9 +14790,9 @@ dependencies = [ [[package]] name = "wasm-opt" -version = "0.116.0" +version = "0.116.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc942673e7684671f0c5708fc18993569d184265fd5223bb51fc8e5b9b6cfd52" +checksum = "2fd87a4c135535ffed86123b6fb0f0a5a0bc89e50416c942c5f0662c645f679c" dependencies = [ "anyhow", "libc", @@ -14872,9 +14886,9 @@ dependencies = [ [[package]] name = "wasmparser-nostd" -version = "0.100.1" +version = "0.100.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9157cab83003221bfd385833ab587a039f5d6fa7304854042ba358a3b09e0724" +checksum = "d5a015fe95f3504a94bb1462c717aae75253e39b9dd6c3fb1062c934535c64aa" dependencies = [ "indexmap-nostd", ] @@ -15076,9 +15090,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.69" +version = "0.3.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" +checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0" dependencies = [ "js-sys", "wasm-bindgen", @@ -15105,15 +15119,16 @@ dependencies = [ [[package]] name = "westend-runtime" -version = "8.0.0" +version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db2a5cebb4c678a0d1291bb21f9d44ddebceae044b0fb5200fa3bed108a31595" +checksum = "b4aa5580861b05668a6af845aa271c4f699a2fc26646d524e5b0d9375fb0647e" dependencies = [ "binary-merkle-tree", "bitvec", "frame-benchmarking", "frame-election-provider-support", "frame-executive", + "frame-metadata-hash-extension", "frame-support", "frame-system", "frame-system-benchmarking", @@ -15236,14 +15251,14 @@ dependencies = [ "either", "home", "once_cell", - "rustix 0.38.32", + "rustix 0.38.34", ] [[package]] name = "wide" -version = "0.7.15" +version = "0.7.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89beec544f246e679fc25490e3f8e08003bc4bf612068f325120dad4cea02c1c" +checksum = "b828f995bf1e9622031f8009f8481a85406ce1f4d4588ff746d872043e855690" dependencies = [ "bytemuck", "safe_arch", @@ -15251,9 +15266,9 @@ dependencies = [ [[package]] name = "widestring" -version = "1.0.2" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "653f141f39ec16bba3c5abe400a0c60da7468261cc2cbf36805022876bc721a8" +checksum = "7219d36b6eac893fa81e84ebe06485e7dcbb616177469b142df14f1f4deb1311" [[package]] name = "winapi" @@ -15273,11 +15288,11 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.6" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "winapi", + "windows-sys 0.59.0", ] [[package]] @@ -15292,7 +15307,7 @@ version = "0.51.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9" dependencies = [ - "windows-core", + "windows-core 0.51.1", "windows-targets 0.48.5", ] @@ -15305,6 +15320,15 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-sys" version = "0.45.0" @@ -15329,7 +15353,16 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.4", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", ] [[package]] @@ -15364,17 +15397,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.4", - "windows_aarch64_msvc 0.52.4", - "windows_i686_gnu 0.52.4", - "windows_i686_msvc 0.52.4", - "windows_x86_64_gnu 0.52.4", - "windows_x86_64_gnullvm 0.52.4", - "windows_x86_64_msvc 0.52.4", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -15391,9 +15425,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" @@ -15409,9 +15443,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" @@ -15427,9 +15461,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.4" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" @@ -15445,9 +15485,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" @@ -15463,9 +15503,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" @@ -15481,9 +15521,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" @@ -15499,9 +15539,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" @@ -15514,9 +15554,9 @@ dependencies = [ [[package]] name = "winnow" -version = "0.6.5" +version = "0.6.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8" +checksum = "68a9bda4691f099d435ad181000724da8e5899daa10713c2d432552b9ccd3a6f" dependencies = [ "memchr", ] @@ -15557,7 +15597,7 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7e468321c81fb07fa7f4c636c3972b9100f0346e5b6a9f2bd0603a52f7ed277" dependencies = [ - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "rand_core 0.6.4", "serde", "zeroize", @@ -15625,7 +15665,7 @@ dependencies = [ "Inflector", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -15637,7 +15677,7 @@ dependencies = [ "futures", "log", "nohash-hasher", - "parking_lot 0.12.1", + "parking_lot 0.12.3", "rand", "static_assertions", ] @@ -15653,29 +15693,30 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.7.32" +version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" dependencies = [ + "byteorder", "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.7.32" +version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] name = "zeroize" -version = "1.7.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" dependencies = [ "zeroize_derive", ] @@ -15688,7 +15729,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.74", ] [[package]] @@ -15731,9 +15772,9 @@ dependencies = [ [[package]] name = "zstd-sys" -version = "2.0.9+zstd.1.5.5" +version = "2.0.13+zstd.1.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e16efa8a874a0481a574084d34cc26fdb3b99627480f785888deb6386506656" +checksum = "38ff0f21cfee8f97d94cef41359e0c89aa6113028ab0291aa8ca0038995a95aa" dependencies = [ "cc", "pkg-config", From 545bd334dac38a1b7bf28e5be6c7de7822672c8a Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Tue, 13 Aug 2024 13:33:12 +0200 Subject: [PATCH 37/76] fix: try-runtime error --- runtime/devnet/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/runtime/devnet/Cargo.toml b/runtime/devnet/Cargo.toml index 5f83d991..87dce79e 100644 --- a/runtime/devnet/Cargo.toml +++ b/runtime/devnet/Cargo.toml @@ -202,6 +202,7 @@ try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", "frame-try-runtime/try-runtime", + "pallet-api/try-runtime", "pallet-aura/try-runtime", "pallet-authorship/try-runtime", "pallet-assets/try-runtime", @@ -224,4 +225,4 @@ try-runtime = [ "parachain-info/try-runtime", "polkadot-runtime-common/try-runtime", "sp-runtime/try-runtime", -] \ No newline at end of file +] From 99d0f6d76aaac6d366ae62a68983ea0878840685 Mon Sep 17 00:00:00 2001 From: Tin Chung <56880684+chungquantin@users.noreply.github.com> Date: Tue, 13 Aug 2024 23:00:33 +0700 Subject: [PATCH 38/76] refactor: generic extension crate to de-duplicate code between runtimes (#163) --- Cargo.lock | 17 + Cargo.toml | 26 +- extension/Cargo.toml | 51 ++ extension/src/lib.rs | 323 ++++++++++++ extension/src/tests.rs | 135 +++++ .../src/extensions => extension/src}/v0.rs | 4 +- runtime/devnet/Cargo.toml | 2 + runtime/devnet/src/config/api.rs | 57 ++- runtime/devnet/src/config/contracts.rs | 7 +- runtime/devnet/src/extensions/mod.rs | 480 ------------------ runtime/devnet/src/lib.rs | 1 - 11 files changed, 583 insertions(+), 520 deletions(-) create mode 100644 extension/Cargo.toml create mode 100644 extension/src/lib.rs create mode 100644 extension/src/tests.rs rename {runtime/devnet/src/extensions => extension/src}/v0.rs (98%) delete mode 100644 runtime/devnet/src/extensions/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 7f033a20..78e5e1d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9460,6 +9460,22 @@ dependencies = [ "universal-hash", ] +[[package]] +name = "pop-chain-extension" +version = "0.1.0" +dependencies = [ + "frame-support", + "frame-system", + "log", + "pallet-contracts", + "parity-scale-codec", + "pop-primitives", + "rand", + "sp-core", + "sp-runtime", + "sp-std", +] + [[package]] name = "pop-node" version = "0.1.0-alpha" @@ -9594,6 +9610,7 @@ dependencies = [ "parity-scale-codec", "polkadot-parachain-primitives", "polkadot-runtime-common", + "pop-chain-extension", "pop-primitives", "pop-runtime-common", "rand", diff --git a/Cargo.toml b/Cargo.toml index d082e838..f037f6b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,18 +23,19 @@ members = [ "pallets/*", "primitives", ] -exclude = [ - "pop-api", - "tests/contracts" -] +exclude = ["pop-api", "tests/contracts"] resolver = "2" [workspace.dependencies] -codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [ + "derive", +] } hex-literal = "0.4.1" log = { version = "0.4.20", default-features = false } -scale-info = { version = "2.10.0", default-features = false, features = ["derive"] } +scale-info = { version = "2.10.0", default-features = false, features = [ + "derive", +] } smallvec = "1.11.0" serde = "1.0.195" clap = { version = "4.4.18", features = ["derive"] } @@ -52,7 +53,8 @@ substrate-build-script-utils = "11.0.0" # Local pallet-api = { path = "pallets/api", default-features = false } -pop-runtime-devnet = { path = "runtime/devnet", default-features = true } # default-features=true required for `-p pop-node` builds +pop-chain-extension = { path = "./extension", default-features = false } +pop-runtime-devnet = { path = "runtime/devnet", default-features = true } # default-features=true required for `-p pop-node` builds pop-runtime-testnet = { path = "runtime/testnet", default-features = true } # default-features=true required for `-p pop-node` builds pop-runtime-common = { path = "runtime/common", default-features = false } pop-primitives = { path = "./primitives", default-features = false } @@ -82,7 +84,9 @@ frame-system = { version = "29.0.0", default-features = false } frame-system-benchmarking = { version = "29.0.0", default-features = false } frame-system-rpc-runtime-api = { version = "27.0.0", default-features = false } frame-try-runtime = { version = "0.35.0", default-features = false } -pallet-aura = { version = "28.0.0", default-features = false, features = ["experimental"] } +pallet-aura = { version = "28.0.0", default-features = false, features = [ + "experimental", +] } pallet-authorship = { version = "29.0.0", default-features = false } pallet-assets = { version = "30.0.0", default-features = false } pallet-balances = { version = "29.0.2", default-features = false } @@ -139,7 +143,9 @@ xcm-executor = { package = "staging-xcm-executor", version = "8.0.2", default-fe # Cumulus asset-test-utils = { version = "8.0.1", default-features = false } cumulus-pallet-aura-ext = { version = "0.8.0", default-features = false } -cumulus-pallet-parachain-system = { version = "0.8.1", default-features = false, features = ["parameterized-consensus-hook"] } +cumulus-pallet-parachain-system = { version = "0.8.1", default-features = false, features = [ + "parameterized-consensus-hook", +] } cumulus-pallet-session-benchmarking = { version = "10.0.0", default-features = false } cumulus-pallet-xcm = { version = "0.8.0", default-features = false } cumulus-pallet-xcmp-queue = { version = "0.8.0", default-features = false } @@ -163,4 +169,4 @@ cumulus-client-service = "0.8.0" # Paseo asset-hub-paseo-runtime = { git = "https://github.com/paseo-network/runtimes/", tag = "v1.2.5-system-chains", default-features = false } paseo-runtime = { git = "https://github.com/paseo-network/runtimes/", tag = "v1.2.5-system-chains", default-features = false } -paseo-runtime-constants = { git = "https://github.com/paseo-network/runtimes/", tag = "v1.2.5-system-chains", default-features = false } \ No newline at end of file +paseo-runtime-constants = { git = "https://github.com/paseo-network/runtimes/", tag = "v1.2.5-system-chains", default-features = false } diff --git a/extension/Cargo.toml b/extension/Cargo.toml new file mode 100644 index 00000000..03b4221a --- /dev/null +++ b/extension/Cargo.toml @@ -0,0 +1,51 @@ +[package] +name = "pop-chain-extension" +version = "0.1.0" +authors.workspace = true +description.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true +edition.workspace = true +publish = false + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +codec.workspace = true +log.workspace = true + +# Local +pop-primitives.workspace = true + +# Substrate +frame-support.workspace = true +frame-system.workspace = true +pallet-contracts.workspace = true +sp-core.workspace = true +sp-runtime.workspace = true +sp-std.workspace = true + +[dev-dependencies] +rand = "0.8.5" + +[features] +default = ["std"] +std = [ + "log/std", + "codec/std", + "frame-support/std", + "frame-system/std", + "pallet-contracts/std", + "pop-primitives/std", + "sp-runtime/std", + "sp-core/std", + "sp-std/std", +] +runtime-benchmarks = [ + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "pallet-contracts/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", +] diff --git a/extension/src/lib.rs b/extension/src/lib.rs new file mode 100644 index 00000000..582f346c --- /dev/null +++ b/extension/src/lib.rs @@ -0,0 +1,323 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(test)] +mod tests; +mod v0; + +use codec::Encode; +use frame_support::{ + dispatch::{GetDispatchInfo, PostDispatchInfo}, + pallet_prelude::*, + traits::OriginTrait, +}; +use frame_system::RawOrigin; +use pallet_contracts::chain_extension::{ + BufInBufOutState, ChainExtension, Environment, Ext, InitState, RetVal, +}; +use sp_core::crypto::UncheckedFrom; +use sp_runtime::{traits::Dispatchable, DispatchError}; +use sp_std::vec::Vec; + +/// Logging target for categorizing messages from the Pop API extension module. +const LOG_TARGET: &str = "pop-api::extension"; + +const DECODING_FAILED_ERROR: DispatchError = DispatchError::Other("DecodingFailed"); +// TODO: issue #93, we can also encode the `pop_primitives::Error::UnknownCall` which means we do use +// `Error` in the runtime and it should stay in primitives. Perhaps issue #91 will also influence +// here. Should be looked at together. +const DECODING_FAILED_ERROR_ENCODED: [u8; 4] = [255u8, 0, 0, 0]; +const UNKNOWN_CALL_ERROR: DispatchError = DispatchError::Other("UnknownCall"); +// TODO: see above. +const UNKNOWN_CALL_ERROR_ENCODED: [u8; 4] = [254u8, 0, 0, 0]; + +type ContractSchedule = ::Schedule; + +/// Type of the state reader. +pub trait ReadState { + /// Query of the state read operations. + type StateQuery: Decode; + + /// Check if a state query is allowed. + fn contains(c: &Self::StateQuery) -> bool; + + /// Reads state using the provided query, returning the result as a byte vector. + fn read(read: Self::StateQuery) -> Vec; + + /// Decodes parameters into state query. + fn decode(params: &mut &[u8]) -> Result { + decode_checked(params) + } +} + +/// Type of the dispatch call filter. +pub trait CallFilter { + /// Query of the dispatch calls operations. + type Call: Decode; + + /// Check if runtime call is allowed. + fn contains(t: &Self::Call) -> bool; +} + +/// Pop API chain extension. +#[derive(Default)] +pub struct ApiExtension(PhantomData); + +impl ChainExtension for ApiExtension +where + T: pallet_contracts::Config + + frame_system::Config< + RuntimeCall: GetDispatchInfo + Dispatchable, + >, + T::AccountId: UncheckedFrom + AsRef<[u8]>, + // Bound the type by the two traits which need to be implemented by the runtime. + I: ReadState + CallFilter::RuntimeCall> + 'static, +{ + fn call>( + &mut self, + env: Environment, + ) -> Result { + log::debug!(target:LOG_TARGET, " extension called "); + let mut env = env.buf_in_buf_out(); + // Charge weight for making a call from a contract to the runtime. + // `debug_message` weight is a good approximation of the additional overhead of going + // from contract layer to substrate layer. + // reference: https://github.com/paritytech/ink-examples/blob/b8d2caa52cf4691e0ddd7c919e4462311deb5ad0/psp22-extension/runtime/psp22-extension-example.rs#L236 + let contract_host_weight = ContractSchedule::::get().host_fn_weights; + env.charge_weight(contract_host_weight.debug_message)?; + + let (version, function_id, pallet_index, call_index) = extract_env(&env); + + let result = match FuncId::try_from(function_id) { + // Read encoded parameters from buffer and calculate weight for reading `len` bytes`. + Ok(function_id) => { + // reference: https://github.com/paritytech/polkadot-sdk/blob/117a9433dac88d5ac00c058c9b39c511d47749d2/substrate/frame/contracts/src/wasm/runtime.rs#L267 + let len = env.in_len(); + env.charge_weight(contract_host_weight.return_per_byte.saturating_mul(len.into()))?; + let params = env.read(len)?; + match function_id { + FuncId::Dispatch => { + dispatch::(&mut env, version, pallet_index, call_index, params) + }, + FuncId::ReadState => { + read_state::(&mut env, version, pallet_index, call_index, params) + }, + } + }, + Err(e) => Err(e), + }; + + match result { + Ok(_) => Ok(RetVal::Converging(0)), + Err(e) => Ok(RetVal::Converging(convert_to_status_code(e, version))), + } + } +} + +/// Extract (version, function_id, pallet_index, call_index) from the payload bytes. +fn extract_env>(env: &Environment) -> (u8, u8, u8, u8) { + // Extract version and function_id from first two bytes. + let (version, function_id) = { + let bytes = env.func_id().to_le_bytes(); + (bytes[0], bytes[1]) + }; + // Extract pallet index and call / key index from last two bytes. + let (pallet_index, call_index) = { + let bytes = env.ext_id().to_le_bytes(); + (bytes[0], bytes[1]) + }; + + (version, function_id, pallet_index, call_index) +} + +fn read_state, StateReader: ReadState>( + env: &mut Environment, + version: u8, + pallet_index: u8, + call_index: u8, + mut params: Vec, +) -> Result<(), DispatchError> { + const LOG_PREFIX: &str = " read_state |"; + + // Prefix params with version, pallet, index to simplify decoding. + params.insert(0, version); + params.insert(1, pallet_index); + params.insert(2, call_index); + let (mut encoded_version, mut encoded_read) = (¶ms[..1], ¶ms[1..]); + let version = decode_checked::(&mut encoded_version)?; + + // Charge weight for doing one storage read. + env.charge_weight(T::DbWeight::get().reads(1_u64))?; + let result = match version { + VersionedStateRead::V0 => { + let read = StateReader::decode(&mut encoded_read)?; + ensure!(StateReader::contains(&read), UNKNOWN_CALL_ERROR); + StateReader::read(read) + }, + }; + log::trace!( + target:LOG_TARGET, + "{} result: {:?}.", LOG_PREFIX, result + ); + env.write(&result, false, None) +} + +fn dispatch( + env: &mut Environment, + version: u8, + pallet_index: u8, + call_index: u8, + mut params: Vec, +) -> Result<(), DispatchError> +where + T: frame_system::Config< + RuntimeCall: GetDispatchInfo + Dispatchable, + >, + E: Ext, + Filter: CallFilter::RuntimeCall> + 'static, +{ + const LOG_PREFIX: &str = " dispatch |"; + + // Prefix params with version, pallet, index to simplify decoding. + params.insert(0, version); + params.insert(1, pallet_index); + params.insert(2, call_index); + let call = decode_checked::>(&mut ¶ms[..])?; + // Contract is the origin by default. + let origin: T::RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); + match call { + VersionedDispatch::V0(call) => dispatch_call::(env, call, origin, LOG_PREFIX), + } +} + +/// Helper method to decode the byte data to a provided type and throws error if failed. +fn decode_checked(params: &mut &[u8]) -> Result { + T::decode(params).map_err(|_| DECODING_FAILED_ERROR) +} + +fn dispatch_call( + env: &mut Environment, + call: T::RuntimeCall, + mut origin: T::RuntimeOrigin, + log_prefix: &str, +) -> Result<(), DispatchError> +where + T: frame_system::Config< + RuntimeCall: GetDispatchInfo + Dispatchable, + >, + E: Ext, + Filter: CallFilter::RuntimeCall> + 'static, +{ + let charged_dispatch_weight = env.charge_weight(call.get_dispatch_info().weight)?; + log::debug!(target:LOG_TARGET, "{} Inputted RuntimeCall: {:?}", log_prefix, call); + origin.add_filter(Filter::contains); + match call.dispatch(origin) { + Ok(info) => { + log::debug!(target:LOG_TARGET, "{} success, actual weight: {:?}", log_prefix, info.actual_weight); + // Refund weight if the actual weight is less than the charged weight. + if let Some(actual_weight) = info.actual_weight { + env.adjust_weight(charged_dispatch_weight, actual_weight); + } + Ok(()) + }, + Err(err) => { + log::debug!(target:LOG_TARGET, "{} failed: error: {:?}", log_prefix, err.error); + Err(err.error) + }, + } +} + +/// Wrapper to enable versioning of runtime state reads. +#[derive(Decode, Debug)] +enum VersionedStateRead { + /// Version zero of state reads. + #[codec(index = 0)] + V0, +} + +/// Wrapper to enable versioning of runtime calls. +#[derive(Decode, Debug)] +enum VersionedDispatch { + /// Version zero of dispatch calls. + #[codec(index = 0)] + V0(RuntimeCall), +} + +/// Function identifiers used in the Pop API. +/// +/// The `FuncId` specifies the available functions that can be called through the Pop API. Each +/// variant corresponds to a specific functionality provided by the API, facilitating the +/// interaction between smart contracts and the runtime. +#[derive(Debug)] +pub enum FuncId { + /// Represents a function call to dispatch a runtime call. + Dispatch, + /// Represents a function call to read the state from the runtime. + ReadState, +} + +impl TryFrom for FuncId { + type Error = DispatchError; + + /// Attempts to convert a `u8` value to its corresponding `FuncId` variant. + /// + /// If the `u8` value does not match any known function identifier, it returns a + /// `DispatchError::Other` indicating an unknown function ID. + fn try_from(func_id: u8) -> Result { + let id = match func_id { + 0 => Self::Dispatch, + 1 => Self::ReadState, + _ => { + return Err(UNKNOWN_CALL_ERROR); + }, + }; + Ok(id) + } +} + +/// Converts a `DispatchError` to a `u32` status code based on the version of the API the contract uses. +/// The contract calling the chain extension can convert the status code to the descriptive `Error`. +/// +/// For `Error` see `pop_primitives::::error::Error`. +/// +/// The error encoding can vary per version, allowing for flexible and backward-compatible error handling. +/// As a result, contracts maintain compatibility across different versions of the runtime. +/// +/// # Parameters +/// +/// - `error`: The `DispatchError` encountered during contract execution. +/// - `version`: The version of the chain extension, used to determine the known errors. +pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { + let mut encoded_error: [u8; 4] = match error { + // "UnknownCall" and "DecodingFailed" are mapped to specific errors in the API and will + // never change. + UNKNOWN_CALL_ERROR => UNKNOWN_CALL_ERROR_ENCODED, + DECODING_FAILED_ERROR => DECODING_FAILED_ERROR_ENCODED, + _ => { + let mut encoded_error = error.encode(); + // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). + encoded_error.resize(4, 0); + encoded_error.try_into().expect("qed, resized to 4 bytes line above") + }, + }; + match version { + // If an unknown variant of the `DispatchError` is detected the error needs to be converted + // into the encoded value of `Error::Other`. This conversion is performed by shifting the bytes one + // position forward (discarding the last byte as it is not used) and setting the first byte to the + // encoded value of `Other` (0u8). This ensures the error is correctly categorized as an `Other` + // variant which provides all the necessary information to debug which error occurred in the runtime. + // + // Byte layout explanation: + // - Byte 0: index of the variant within `Error` + // - Byte 1: + // - Must be zero for `UNIT_ERRORS`. + // - Represents the nested error in `SINGLE_NESTED_ERRORS`. + // - Represents the first level of nesting in `DOUBLE_NESTED_ERRORS`. + // - Byte 2: + // - Represents the second level of nesting in `DOUBLE_NESTED_ERRORS`. + // - Byte 3: + // - Unused or represents further nested information. + 0 => v0::handle_unknown_error(&mut encoded_error), + _ => encoded_error = UNKNOWN_CALL_ERROR_ENCODED, + } + u32::from_le_bytes(encoded_error) +} diff --git a/extension/src/tests.rs b/extension/src/tests.rs new file mode 100644 index 00000000..a47feef9 --- /dev/null +++ b/extension/src/tests.rs @@ -0,0 +1,135 @@ +use codec::{Decode, Encode}; + +// Test ensuring `func_id()` and `ext_id()` work as expected, i.e. extracting the first two +// bytes and the last two bytes, respectively, from a 4 byte array. +#[test] +fn test_byte_extraction() { + use rand::Rng; + + // Helper functions + fn func_id(id: u32) -> u16 { + (id & 0x0000FFFF) as u16 + } + fn ext_id(id: u32) -> u16 { + (id >> 16) as u16 + } + + // Number of test iterations + let test_iterations = 1_000_000; + + // Create a random number generator + let mut rng = rand::thread_rng(); + + // Run the test for a large number of random 4-byte arrays + for _ in 0..test_iterations { + // Generate a random 4-byte array + let bytes: [u8; 4] = rng.gen(); + + // Convert the 4-byte array to a u32 value + let value = u32::from_le_bytes(bytes); + + // Extract the first two bytes (least significant 2 bytes) + let first_two_bytes = func_id(value); + + // Extract the last two bytes (most significant 2 bytes) + let last_two_bytes = ext_id(value); + + // Check if the first two bytes match the expected value + assert_eq!([bytes[0], bytes[1]], first_two_bytes.to_le_bytes()); + + // Check if the last two bytes match the expected value + assert_eq!([bytes[2], bytes[3]], last_two_bytes.to_le_bytes()); + } +} + +// Test showing all the different type of variants and its encoding. +#[test] +fn encoding_of_enum() { + #[derive(Debug, PartialEq, Encode, Decode)] + enum ComprehensiveEnum { + SimpleVariant, + DataVariant(u8), + NamedFields { w: u8 }, + NestedEnum(InnerEnum), + OptionVariant(Option), + VecVariant(Vec), + TupleVariant(u8, u8), + NestedStructVariant(NestedStruct), + NestedEnumStructVariant(NestedEnumStruct), + } + + #[derive(Debug, PartialEq, Encode, Decode)] + enum InnerEnum { + A, + B { inner_data: u8 }, + C(u8), + } + + #[derive(Debug, PartialEq, Encode, Decode)] + struct NestedStruct { + x: u8, + y: u8, + } + + #[derive(Debug, PartialEq, Encode, Decode)] + struct NestedEnumStruct { + inner_enum: InnerEnum, + } + + // Creating each possible variant for an enum. + let enum_simple = ComprehensiveEnum::SimpleVariant; + let enum_data = ComprehensiveEnum::DataVariant(42); + let enum_named = ComprehensiveEnum::NamedFields { w: 42 }; + let enum_nested = ComprehensiveEnum::NestedEnum(InnerEnum::B { inner_data: 42 }); + let enum_option = ComprehensiveEnum::OptionVariant(Some(42)); + let enum_vec = ComprehensiveEnum::VecVariant(vec![1, 2, 3, 4, 5]); + let enum_tuple = ComprehensiveEnum::TupleVariant(42, 42); + let enum_nested_struct = ComprehensiveEnum::NestedStructVariant(NestedStruct { x: 42, y: 42 }); + let enum_nested_enum_struct = ComprehensiveEnum::NestedEnumStructVariant(NestedEnumStruct { + inner_enum: InnerEnum::C(42), + }); + + // Encode and print each variant individually to see their encoded values. + println!("{:?} -> {:?}", enum_simple, enum_simple.encode()); + println!("{:?} -> {:?}", enum_data, enum_data.encode()); + println!("{:?} -> {:?}", enum_named, enum_named.encode()); + println!("{:?} -> {:?}", enum_nested, enum_nested.encode()); + println!("{:?} -> {:?}", enum_option, enum_option.encode()); + println!("{:?} -> {:?}", enum_vec, enum_vec.encode()); + println!("{:?} -> {:?}", enum_tuple, enum_tuple.encode()); + println!("{:?} -> {:?}", enum_nested_struct, enum_nested_struct.encode()); + println!("{:?} -> {:?}", enum_nested_enum_struct, enum_nested_enum_struct.encode()); +} + +#[test] +fn encoding_decoding_dispatch_error() { + use sp_runtime::{ArithmeticError, DispatchError, ModuleError, TokenError}; + + let error = DispatchError::Module(ModuleError { + index: 255, + error: [2, 0, 0, 0], + message: Some("error message"), + }); + let encoded = error.encode(); + let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); + assert_eq!(encoded, vec![3, 255, 2, 0, 0, 0]); + assert_eq!( + decoded, + // `message` is skipped for encoding. + DispatchError::Module(ModuleError { index: 255, error: [2, 0, 0, 0], message: None }) + ); + + // Example DispatchError::Token + let error = DispatchError::Token(TokenError::UnknownAsset); + let encoded = error.encode(); + let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); + assert_eq!(encoded, vec![7, 4]); + assert_eq!(decoded, error); + + // Example DispatchError::Arithmetic + let error = DispatchError::Arithmetic(ArithmeticError::Overflow); + let encoded = error.encode(); + let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); + assert_eq!(encoded, vec![8, 1]); + assert_eq!(decoded, error); +} diff --git a/runtime/devnet/src/extensions/v0.rs b/extension/src/v0.rs similarity index 98% rename from runtime/devnet/src/extensions/v0.rs rename to extension/src/v0.rs index 72760323..4c3536a4 100644 --- a/runtime/devnet/src/extensions/v0.rs +++ b/extension/src/v0.rs @@ -1,5 +1,5 @@ #[cfg(test)] -use crate::extensions::convert_to_status_code; +use crate::convert_to_status_code; pub(crate) fn handle_unknown_error(encoded_error: &mut [u8; 4]) { let unknown = match encoded_error[0] { @@ -106,7 +106,7 @@ mod tests { (DispatchError::RootNotAllowed, RootNotAllowed), ]; for (dispatch_error, expected) in test_cases { - let status_code = crate::extensions::convert_to_status_code(dispatch_error, 0); + let status_code = crate::convert_to_status_code(dispatch_error, 0); let error: Error = status_code.into(); assert_eq!(error, expected); } diff --git a/runtime/devnet/Cargo.toml b/runtime/devnet/Cargo.toml index 87dce79e..995af269 100644 --- a/runtime/devnet/Cargo.toml +++ b/runtime/devnet/Cargo.toml @@ -22,6 +22,7 @@ scale-info.workspace = true smallvec.workspace = true # Local +pop-chain-extension.workspace = true pop-primitives.workspace = true pop-runtime-common.workspace = true pallet-api.workspace = true @@ -139,6 +140,7 @@ std = [ "parachains-common/std", "polkadot-parachain-primitives/std", "polkadot-runtime-common/std", + "pop-chain-extension/std", "pop-primitives/std", "scale-info/std", "sp-api/std", diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index bdff6bbb..5f234cd4 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -1,22 +1,47 @@ use crate::{config::assets::TrustBackedAssetsInstance, fungibles, Runtime, RuntimeCall}; use codec::{Decode, Encode, MaxEncodedLen}; -use frame_support::traits::Contains; +use pop_chain_extension::{CallFilter, ReadState}; +use sp_std::vec::Vec; /// A query of runtime state. #[derive(Encode, Decode, Debug, MaxEncodedLen)] #[repr(u8)] -pub enum RuntimeRead { +pub enum RuntimeRead { /// Fungible token queries. #[codec(index = 150)] - Fungibles(fungibles::Read), + Fungibles(fungibles::Read), } -/// A type to identify allowed calls to the Runtime from the API. -pub struct AllowedApiCalls; +/// A struct that implement requirements for the Pop API chain extension. +#[derive(Default)] +pub struct Extension; +impl ReadState for Extension { + type StateQuery = RuntimeRead; -impl Contains for AllowedApiCalls { - /// Allowed runtime calls from the API. - fn contains(c: &RuntimeCall) -> bool { + fn contains(c: &Self::StateQuery) -> bool { + use fungibles::Read::*; + matches!( + c, + RuntimeRead::Fungibles( + TotalSupply(..) + | BalanceOf { .. } | Allowance { .. } + | TokenName(..) | TokenSymbol(..) + | TokenDecimals(..) | AssetExists(..) + ) + ) + } + + fn read(read: RuntimeRead) -> Vec { + match read { + RuntimeRead::Fungibles(key) => fungibles::Pallet::read_state(key), + } + } +} + +impl CallFilter for Extension { + type Call = RuntimeCall; + + fn contains(c: &Self::Call) -> bool { use fungibles::Call::*; matches!( c, @@ -34,22 +59,6 @@ impl Contains for AllowedApiCalls { } } -impl Contains> for AllowedApiCalls { - /// Allowed state queries from the API. - fn contains(c: &RuntimeRead) -> bool { - use fungibles::Read::*; - matches!( - c, - RuntimeRead::Fungibles( - TotalSupply(..) - | BalanceOf { .. } | Allowance { .. } - | TokenName(..) | TokenSymbol(..) - | TokenDecimals(..) | AssetExists(..) - ) - ) - } -} - impl fungibles::Config for Runtime { type AssetsInstance = TrustBackedAssetsInstance; type WeightInfo = fungibles::weights::SubstrateWeight; diff --git a/runtime/devnet/src/config/contracts.rs b/runtime/devnet/src/config/contracts.rs index 36d62f7f..5dc613b5 100644 --- a/runtime/devnet/src/config/contracts.rs +++ b/runtime/devnet/src/config/contracts.rs @@ -1,6 +1,7 @@ +use super::api::Extension; use crate::{ - deposit, extensions, Balance, Balances, BalancesCall, Perbill, Runtime, RuntimeCall, - RuntimeEvent, RuntimeHoldReason, Timestamp, + deposit, Balance, Balances, BalancesCall, Perbill, Runtime, RuntimeCall, RuntimeEvent, + RuntimeHoldReason, Timestamp, }; use frame_support::{ parameter_types, @@ -63,7 +64,7 @@ impl pallet_contracts::Config for Runtime { type CallStack = [pallet_contracts::Frame; 23]; type WeightPrice = pallet_transaction_payment::Pallet; type WeightInfo = pallet_contracts::weights::SubstrateWeight; - type ChainExtension = extensions::PopApiExtension; + type ChainExtension = pop_chain_extension::ApiExtension; type Schedule = Schedule; type AddressGenerator = pallet_contracts::DefaultAddressGenerator; // This node is geared towards development and testing of contracts. diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs deleted file mode 100644 index d3bbdd0b..00000000 --- a/runtime/devnet/src/extensions/mod.rs +++ /dev/null @@ -1,480 +0,0 @@ -mod v0; - -use crate::{ - config::{ - api::{AllowedApiCalls, RuntimeRead}, - assets::TrustBackedAssetsInstance, - }, - fungibles::{self}, - AccountId, RuntimeCall, RuntimeOrigin, -}; -use codec::{Decode, Encode}; -use frame_support::{ - dispatch::{GetDispatchInfo, RawOrigin}, - pallet_prelude::*, - traits::{Contains, OriginTrait}, -}; -use pallet_contracts::chain_extension::{ - BufInBufOutState, ChainExtension, Environment, Ext, InitState, RetVal, -}; -use pop_primitives::AssetId; -use sp_core::crypto::UncheckedFrom; -use sp_runtime::{traits::Dispatchable, DispatchError}; -use sp_std::vec::Vec; - -const LOG_TARGET: &str = "pop-api::extension"; -const DECODING_FAILED_ERROR: DispatchError = DispatchError::Other("DecodingFailed"); -// TODO: issue #93, we can also encode the `pop_primitives::Error::UnknownCall` which means we do use -// `Error` in the runtime and it should stay in primitives. Perhaps issue #91 will also influence -// here. Should be looked at together. -const DECODING_FAILED_ERROR_ENCODED: [u8; 4] = [255u8, 0, 0, 0]; -const UNKNOWN_CALL_ERROR: DispatchError = DispatchError::Other("UnknownCall"); -// TODO: see above. -const UNKNOWN_CALL_ERROR_ENCODED: [u8; 4] = [254u8, 0, 0, 0]; - -type ContractSchedule = ::Schedule; - -#[derive(Default)] -pub struct PopApiExtension; - -impl ChainExtension for PopApiExtension -where - T: pallet_contracts::Config - + pallet_assets::Config - + fungibles::Config - + frame_system::Config< - RuntimeOrigin = RuntimeOrigin, - AccountId = AccountId, - RuntimeCall = RuntimeCall, - >, - T::AccountId: UncheckedFrom + AsRef<[u8]>, -{ - fn call(&mut self, env: Environment) -> Result - where - E: Ext, - { - log::debug!(target:LOG_TARGET, " extension called "); - let mut env = env.buf_in_buf_out(); - // Charge weight for making a call from a contract to the runtime. - // `debug_message` weight is a good approximation of the additional overhead of going - // from contract layer to substrate layer. - // reference: https://github.com/paritytech/ink-examples/blob/b8d2caa52cf4691e0ddd7c919e4462311deb5ad0/psp22-extension/runtime/psp22-extension-example.rs#L236 - let contract_host_weight = ContractSchedule::::get().host_fn_weights; - env.charge_weight(contract_host_weight.debug_message)?; - - // Extract version and function_id from first two bytes. - let (version, function_id) = { - let bytes = env.func_id().to_le_bytes(); - (bytes[0], bytes[1]) - }; - // Extract pallet index and call / key index from last two bytes. - let (pallet_index, call_index) = { - let bytes = env.ext_id().to_le_bytes(); - (bytes[0], bytes[1]) - }; - - let result = match FuncId::try_from(function_id) { - Ok(function_id) => { - // Read encoded parameters from buffer and calculate weight for reading `len` bytes`. - // reference: https://github.com/paritytech/polkadot-sdk/blob/117a9433dac88d5ac00c058c9b39c511d47749d2/substrate/frame/contracts/src/wasm/runtime.rs#L267 - let len = env.in_len(); - env.charge_weight(contract_host_weight.return_per_byte.saturating_mul(len.into()))?; - let params = env.read(len)?; - log::debug!(target: LOG_TARGET, "Read input successfully"); - match function_id { - FuncId::Dispatch => { - dispatch::(&mut env, version, pallet_index, call_index, params) - }, - FuncId::ReadState => { - read_state::(&mut env, version, pallet_index, call_index, params) - }, - } - }, - Err(e) => Err(e), - }; - - match result { - Ok(_) => Ok(RetVal::Converging(0)), - Err(e) => Ok(RetVal::Converging(convert_to_status_code(e, version))), - } - } -} - -fn dispatch( - env: &mut Environment, - version: u8, - pallet_index: u8, - call_index: u8, - mut params: Vec, -) -> Result<(), DispatchError> -where - T: frame_system::Config, - RuntimeOrigin: From>, - E: Ext, -{ - const LOG_PREFIX: &str = " dispatch |"; - - // Prefix params with version, pallet, index to simplify decoding. - params.insert(0, version); - params.insert(1, pallet_index); - params.insert(2, call_index); - let call = ::decode(&mut ¶ms[..]).map_err(|_| DECODING_FAILED_ERROR)?; - - // Contract is the origin by default. - let origin: RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); - match call { - VersionedDispatch::V0(call) => dispatch_call::(env, call, origin, LOG_PREFIX), - } -} - -fn dispatch_call( - env: &mut Environment, - call: RuntimeCall, - mut origin: RuntimeOrigin, - log_prefix: &str, -) -> Result<(), DispatchError> -where - T: frame_system::Config, - RuntimeOrigin: From>, - E: Ext, -{ - let charged_dispatch_weight = env.charge_weight(call.get_dispatch_info().weight)?; - log::debug!(target:LOG_TARGET, "{} Inputted RuntimeCall: {:?}", log_prefix, call); - origin.add_filter(AllowedApiCalls::contains); - match call.dispatch(origin) { - Ok(info) => { - log::debug!(target:LOG_TARGET, "{} success, actual weight: {:?}", log_prefix, info.actual_weight); - // Refund weight if the actual weight is less than the charged weight. - if let Some(actual_weight) = info.actual_weight { - env.adjust_weight(charged_dispatch_weight, actual_weight); - } - Ok(()) - }, - Err(err) => { - log::debug!(target:LOG_TARGET, "{} failed: error: {:?}", log_prefix, err.error); - Err(err.error) - }, - } -} - -fn read_state( - env: &mut Environment, - version: u8, - pallet_index: u8, - call_index: u8, - mut params: Vec, -) -> Result<(), DispatchError> -where - T: pallet_contracts::Config - + pallet_assets::Config - + fungibles::Config - + frame_system::Config, - E: Ext, -{ - const LOG_PREFIX: &str = " read_state |"; - - // Prefix params with version, pallet, index to simplify decoding, and decode parameters for - // reading state. - params.insert(0, version); - params.insert(1, pallet_index); - params.insert(2, call_index); - let read = - >::decode(&mut ¶ms[..]).map_err(|_| DECODING_FAILED_ERROR)?; - - // Charge weight for doing one storage read. - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - let result = match read { - VersionedStateRead::V0(read) => { - ensure!(AllowedApiCalls::contains(&read), UNKNOWN_CALL_ERROR); - match read { - RuntimeRead::Fungibles(key) => fungibles::Pallet::::read_state(key), - } - }, - }; - log::trace!( - target:LOG_TARGET, - "{} result: {:?}.", LOG_PREFIX, result - ); - env.write(&result, false, None) -} - -/// Wrapper to enable versioning of runtime state reads. -#[derive(Decode, Debug)] -enum VersionedStateRead { - /// Version zero of state reads. - #[codec(index = 0)] - V0(RuntimeRead), -} - -/// Wrapper to enable versioning of runtime calls. -#[derive(Decode, Debug)] -enum VersionedDispatch { - /// Version zero of dispatch calls. - #[codec(index = 0)] - V0(RuntimeCall), -} - -// Converts a `DispatchError` to a `u32` status code based on the version of the API the contract uses. -// The contract calling the chain extension can convert the status code to the descriptive `Error`. -// -// For `Error` see `pop_primitives::::error::Error`. -// -// The error encoding can vary per version, allowing for flexible and backward-compatible error handling. -// As a result, contracts maintain compatibility across different versions of the runtime. -// -// # Parameters -// -// - `error`: The `DispatchError` encountered during contract execution. -// - `version`: The version of the chain extension, used to determine the known errors. -pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { - let mut encoded_error: [u8; 4] = match error { - // "UnknownCall" and "DecodingFailed" are mapped to specific errors in the API and will - // never change. - UNKNOWN_CALL_ERROR => UNKNOWN_CALL_ERROR_ENCODED, - DECODING_FAILED_ERROR => DECODING_FAILED_ERROR_ENCODED, - _ => { - let mut encoded_error = error.encode(); - // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). - encoded_error.resize(4, 0); - encoded_error.try_into().expect("qed, resized to 4 bytes line above") - }, - }; - match version { - // If an unknown variant of the `DispatchError` is detected the error needs to be converted - // into the encoded value of `Error::Other`. This conversion is performed by shifting the bytes one - // position forward (discarding the last byte as it is not used) and setting the first byte to the - // encoded value of `Other` (0u8). This ensures the error is correctly categorized as an `Other` - // variant which provides all the necessary information to debug which error occurred in the runtime. - // - // Byte layout explanation: - // - Byte 0: index of the variant within `Error` - // - Byte 1: - // - Must be zero for `UNIT_ERRORS`. - // - Represents the nested error in `SINGLE_NESTED_ERRORS`. - // - Represents the first level of nesting in `DOUBLE_NESTED_ERRORS`. - // - Byte 2: - // - Represents the second level of nesting in `DOUBLE_NESTED_ERRORS`. - // - Byte 3: - // - Unused or represents further nested information. - 0 => v0::handle_unknown_error(&mut encoded_error), - _ => encoded_error = UNKNOWN_CALL_ERROR_ENCODED, - } - u32::from_le_bytes(encoded_error) -} - -/// Function identifiers used in the Pop API. -/// -/// The `FuncId` specifies the available functions that can be called through the Pop API. Each -/// variant corresponds to a specific functionality provided by the API, facilitating the -/// interaction between smart contracts and the runtime. -/// -/// - `Dispatch`: Represents a function call to dispatch a runtime call. -/// - `ReadState`: Represents a function call to read the state from the runtime. -/// - `SendXcm`: Represents a function call to send an XCM message. -#[derive(Debug)] -pub enum FuncId { - Dispatch, - ReadState, -} - -impl TryFrom for FuncId { - type Error = DispatchError; - - /// Attempts to convert a `u8` value to its corresponding `FuncId` variant. - /// - /// If the `u8` value does not match any known function identifier, it returns a - /// `DispatchError::Other` indicating an unknown function ID. - fn try_from(func_id: u8) -> Result { - let id = match func_id { - 0 => Self::Dispatch, - 1 => Self::ReadState, - _ => { - return Err(UNKNOWN_CALL_ERROR); - }, - }; - Ok(id) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::{Assets, Runtime, System}; - use sp_runtime::BuildStorage; - - // Test ensuring `func_id()` and `ext_id()` work as expected, i.e. extracting the first two - // bytes and the last two bytes, respectively, from a 4 byte array. - #[test] - fn test_byte_extraction() { - use rand::Rng; - - // Helper functions - fn func_id(id: u32) -> u16 { - (id & 0x0000FFFF) as u16 - } - fn ext_id(id: u32) -> u16 { - (id >> 16) as u16 - } - - // Number of test iterations - let test_iterations = 1_000_000; - - // Create a random number generator - let mut rng = rand::thread_rng(); - - // Run the test for a large number of random 4-byte arrays - for _ in 0..test_iterations { - // Generate a random 4-byte array - let bytes: [u8; 4] = rng.gen(); - - // Convert the 4-byte array to a u32 value - let value = u32::from_le_bytes(bytes); - - // Extract the first two bytes (least significant 2 bytes) - let first_two_bytes = func_id(value); - - // Extract the last two bytes (most significant 2 bytes) - let last_two_bytes = ext_id(value); - - // Check if the first two bytes match the expected value - assert_eq!([bytes[0], bytes[1]], first_two_bytes.to_le_bytes()); - - // Check if the last two bytes match the expected value - assert_eq!([bytes[2], bytes[3]], last_two_bytes.to_le_bytes()); - } - } - - // Test showing all the different type of variants and its encoding. - #[test] - fn encoding_of_enum() { - #[derive(Debug, PartialEq, Encode, Decode)] - enum ComprehensiveEnum { - SimpleVariant, - DataVariant(u8), - NamedFields { w: u8 }, - NestedEnum(InnerEnum), - OptionVariant(Option), - VecVariant(Vec), - TupleVariant(u8, u8), - NestedStructVariant(NestedStruct), - NestedEnumStructVariant(NestedEnumStruct), - } - - #[derive(Debug, PartialEq, Encode, Decode)] - enum InnerEnum { - A, - B { inner_data: u8 }, - C(u8), - } - - #[derive(Debug, PartialEq, Encode, Decode)] - struct NestedStruct { - x: u8, - y: u8, - } - - #[derive(Debug, PartialEq, Encode, Decode)] - struct NestedEnumStruct { - inner_enum: InnerEnum, - } - - // Creating each possible variant for an enum. - let enum_simple = ComprehensiveEnum::SimpleVariant; - let enum_data = ComprehensiveEnum::DataVariant(42); - let enum_named = ComprehensiveEnum::NamedFields { w: 42 }; - let enum_nested = ComprehensiveEnum::NestedEnum(InnerEnum::B { inner_data: 42 }); - let enum_option = ComprehensiveEnum::OptionVariant(Some(42)); - let enum_vec = ComprehensiveEnum::VecVariant(vec![1, 2, 3, 4, 5]); - let enum_tuple = ComprehensiveEnum::TupleVariant(42, 42); - let enum_nested_struct = - ComprehensiveEnum::NestedStructVariant(NestedStruct { x: 42, y: 42 }); - let enum_nested_enum_struct = - ComprehensiveEnum::NestedEnumStructVariant(NestedEnumStruct { - inner_enum: InnerEnum::C(42), - }); - - // Encode and print each variant individually to see their encoded values. - println!("{:?} -> {:?}", enum_simple, enum_simple.encode()); - println!("{:?} -> {:?}", enum_data, enum_data.encode()); - println!("{:?} -> {:?}", enum_named, enum_named.encode()); - println!("{:?} -> {:?}", enum_nested, enum_nested.encode()); - println!("{:?} -> {:?}", enum_option, enum_option.encode()); - println!("{:?} -> {:?}", enum_vec, enum_vec.encode()); - println!("{:?} -> {:?}", enum_tuple, enum_tuple.encode()); - println!("{:?} -> {:?}", enum_nested_struct, enum_nested_struct.encode()); - println!("{:?} -> {:?}", enum_nested_enum_struct, enum_nested_enum_struct.encode()); - } - - fn new_test_ext() -> sp_io::TestExternalities { - let t = frame_system::GenesisConfig::::default() - .build_storage() - .expect("Frame system builds valid default genesis config"); - let mut ext = sp_io::TestExternalities::new(t); - ext.execute_with(|| System::set_block_number(1)); - ext - } - - #[test] - fn encoding_decoding_dispatch_error() { - use sp_runtime::{ArithmeticError, DispatchError, ModuleError, TokenError}; - - new_test_ext().execute_with(|| { - let error = DispatchError::Module(ModuleError { - index: 255, - error: [2, 0, 0, 0], - message: Some("error message"), - }); - let encoded = error.encode(); - let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); - assert_eq!(encoded, vec![3, 255, 2, 0, 0, 0]); - assert_eq!( - decoded, - // `message` is skipped for encoding. - DispatchError::Module(ModuleError { - index: 255, - error: [2, 0, 0, 0], - message: None - }) - ); - - // Example pallet assets Error into ModuleError. - let index = <::PalletInfo as frame_support::traits::PalletInfo>::index::< - Assets, - >() - .expect("Every active module has an index in the runtime; qed") as u8; - let mut error = - pallet_assets::Error::NotFrozen::.encode(); - error.resize(MAX_MODULE_ERROR_ENCODED_SIZE, 0); - let error = DispatchError::Module(ModuleError { - index, - error: TryInto::try_into(error).expect("should work"), - message: None, - }); - let encoded = error.encode(); - let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); - assert_eq!(encoded, vec![3, 52, 18, 0, 0, 0]); - assert_eq!( - decoded, - DispatchError::Module(ModuleError { - index: 52, - error: [18, 0, 0, 0], - message: None - }) - ); - - // Example DispatchError::Token - let error = DispatchError::Token(TokenError::UnknownAsset); - let encoded = error.encode(); - let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); - assert_eq!(encoded, vec![7, 4]); - assert_eq!(decoded, error); - - // Example DispatchError::Arithmetic - let error = DispatchError::Arithmetic(ArithmeticError::Overflow); - let encoded = error.encode(); - let decoded = DispatchError::decode(&mut &encoded[..]).unwrap(); - assert_eq!(encoded, vec![8, 1]); - assert_eq!(decoded, error); - }); - } -} diff --git a/runtime/devnet/src/lib.rs b/runtime/devnet/src/lib.rs index 130fbb9a..4256ac01 100644 --- a/runtime/devnet/src/lib.rs +++ b/runtime/devnet/src/lib.rs @@ -8,7 +8,6 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); // Public due to integration tests crate. pub mod config; -mod extensions; mod weights; use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases; From 8b4f595fc745804301162a5ed5845837ee524601 Mon Sep 17 00:00:00 2001 From: Daan van der Plas <93204684+Daanvdplas@users.noreply.github.com> Date: Wed, 14 Aug 2024 10:00:49 +0200 Subject: [PATCH 39/76] feat: api events (#153) Co-authored-by: Frank Bell --- pallets/api/src/fungibles/mod.rs | 182 +++++++++++++++++++------ pallets/api/src/fungibles/tests.rs | 211 ++++++++++++++++++----------- pallets/api/src/mock.rs | 1 + pop-api/src/v0/assets/fungibles.rs | 85 ++++++++++++ runtime/devnet/src/config/api.rs | 5 +- 5 files changed, 359 insertions(+), 125 deletions(-) diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index eefad0e1..7aa9ac00 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -84,6 +84,8 @@ pub mod pallet { /// Configure the pallet by specifying the parameters and types on which it depends. #[pallet::config] pub trait Config: frame_system::Config + pallet_assets::Config { + /// Because this pallet emits events, it depends on the runtime's definition of an event. + type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// The instance of pallet assets it is tightly coupled to. type AssetsInstance; /// Weight information for dispatchables in this pallet. @@ -93,6 +95,43 @@ pub mod pallet { #[pallet::pallet] pub struct Pallet(_); + /// The events that can be emitted. + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + /// Event emitted when allowance by `owner` to `spender` changes. + Approval { + /// The ID of the asset. + id: AssetIdOf, + /// Account providing allowance. + owner: AccountIdOf, + /// Allowance beneficiary. + spender: AccountIdOf, + /// New allowance amount. + value: BalanceOf, + }, + /// Event emitted when transfer of tokens occurs. + Transfer { + /// The ID of the asset. + id: AssetIdOf, + /// Transfer sender. `None` in case of minting new tokens. + from: Option>, + /// Transfer recipient. `None` in case of burning tokens. + to: Option>, + /// Amount of tokens transferred (or minted/burned). + value: BalanceOf, + }, + /// Event emitted when a token class is created. + Create { + /// The ID of the asset. + id: AssetIdOf, + /// Creator of the asset. + creator: AccountIdOf, + /// Admin of the asset. + admin: AccountIdOf, + }, + } + #[pallet::call] impl Pallet { /// Transfers `value` amount of tokens from the caller's account to account `to`, with additional @@ -110,8 +149,15 @@ pub mod pallet { to: AccountIdOf, value: BalanceOf, ) -> DispatchResult { - let to = T::Lookup::unlookup(to); - AssetsOf::::transfer_keep_alive(origin, id.into(), to, value) + AssetsOf::::transfer_keep_alive( + origin.clone(), + id.clone().into(), + T::Lookup::unlookup(to.clone()), + value, + )?; + let from = ensure_signed(origin)?; + Self::deposit_event(Event::Transfer { id, from: Some(from), to: Some(to), value }); + Ok(()) } /// Transfers `value` amount tokens on behalf of `from` to account `to` with additional `data` @@ -119,7 +165,7 @@ pub mod pallet { /// /// # Parameters /// - `id` - The ID of the asset. - /// - `owner` - The account from which the asset balance will be withdrawn. + /// - `from` - The account from which the asset balance will be withdrawn. /// - `to` - The recipient account. /// - `value` - The number of tokens to transfer. #[pallet::call_index(4)] @@ -131,9 +177,15 @@ pub mod pallet { to: AccountIdOf, value: BalanceOf, ) -> DispatchResult { - let from = T::Lookup::unlookup(from); - let to = T::Lookup::unlookup(to); - AssetsOf::::transfer_approved(origin, id.into(), from, to, value) + AssetsOf::::transfer_approved( + origin, + id.clone().into(), + T::Lookup::unlookup(from.clone()), + T::Lookup::unlookup(to.clone()), + value, + )?; + Self::deposit_event(Event::Transfer { id, from: Some(from), to: Some(to), value }); + Ok(()) } /// Approves an account to spend a specified number of tokens on behalf of the caller. @@ -150,13 +202,11 @@ pub mod pallet { spender: AccountIdOf, value: BalanceOf, ) -> DispatchResultWithPostInfo { - let who = ensure_signed(origin.clone()) + let owner = ensure_signed(origin.clone()) .map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?; - let current_allowance = AssetsOf::::allowance(id.clone(), &who, &spender); - let spender = T::Lookup::unlookup(spender); - let id: AssetIdParameterOf = id.into(); + let current_allowance = AssetsOf::::allowance(id.clone(), &owner, &spender); - let return_weight = match value.cmp(¤t_allowance) { + let weight = match value.cmp(¤t_allowance) { // If the new value is equal to the current allowance, do nothing. Equal => Self::weight_approve(0, 0), // If the new value is greater than the current allowance, approve the difference @@ -164,8 +214,8 @@ pub mod pallet { Greater => { AssetsOf::::approve_transfer( origin, - id, - spender, + id.clone().into(), + T::Lookup::unlookup(spender.clone()), value.saturating_sub(current_allowance), ) .map_err(|e| e.with_weight(Self::weight_approve(1, 0)))?; @@ -174,16 +224,24 @@ pub mod pallet { // If the new value is less than the current allowance, cancel the approval and // set the new value. Less => { - AssetsOf::::cancel_approval(origin.clone(), id.clone(), spender.clone()) - .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; + let id_param: AssetIdParameterOf = id.clone().into(); + let spender_source = T::Lookup::unlookup(spender.clone()); + AssetsOf::::cancel_approval( + origin.clone(), + id_param.clone(), + spender_source.clone(), + ) + .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; if value.is_zero() { - return Ok(Some(Self::weight_approve(0, 1)).into()); + Self::weight_approve(0, 1) + } else { + AssetsOf::::approve_transfer(origin, id_param, spender_source, value)?; + Self::weight_approve(1, 1) } - AssetsOf::::approve_transfer(origin, id, spender, value)?; - Self::weight_approve(1, 1) }, }; - Ok(Some(return_weight).into()) + Self::deposit_event(Event::Approval { id, owner, spender, value }); + Ok(Some(weight).into()) } /// Increases the allowance of a spender. @@ -193,15 +251,25 @@ pub mod pallet { /// - `spender` - The account that is allowed to spend the tokens. /// - `value` - The number of tokens to increase the allowance by. #[pallet::call_index(6)] - #[pallet::weight(AssetsWeightInfoOf::::approve_transfer())] + #[pallet::weight(::WeightInfo::approve(1, 0))] pub fn increase_allowance( origin: OriginFor, id: AssetIdOf, spender: AccountIdOf, value: BalanceOf, - ) -> DispatchResult { - let spender = T::Lookup::unlookup(spender); - AssetsOf::::approve_transfer(origin, id.into(), spender, value) + ) -> DispatchResultWithPostInfo { + let owner = ensure_signed(origin.clone()) + .map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?; + AssetsOf::::approve_transfer( + origin, + id.clone().into(), + T::Lookup::unlookup(spender.clone()), + value, + ) + .map_err(|e| e.with_weight(AssetsWeightInfoOf::::approve_transfer()))?; + let value = AssetsOf::::allowance(id.clone(), &owner, &spender); + Self::deposit_event(Event::Approval { id, owner, spender, value }); + Ok(().into()) } /// Decreases the allowance of a spender. @@ -218,24 +286,31 @@ pub mod pallet { spender: AccountIdOf, value: BalanceOf, ) -> DispatchResultWithPostInfo { - let who = ensure_signed(origin.clone()) + let owner = ensure_signed(origin.clone()) .map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?; - let current_allowance = AssetsOf::::allowance(id.clone(), &who, &spender); - let spender = T::Lookup::unlookup(spender); - let id: AssetIdParameterOf = id.into(); - if value.is_zero() { return Ok(Some(Self::weight_approve(0, 0)).into()); } - // Cancel the aproval and set the new value if `new_allowance` is more than zero. - AssetsOf::::cancel_approval(origin.clone(), id.clone(), spender.clone()) - .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; + let current_allowance = AssetsOf::::allowance(id.clone(), &owner, &spender); + let spender_source = T::Lookup::unlookup(spender.clone()); + let id_param: AssetIdParameterOf = id.clone().into(); + + // Cancel the approval and set the new value if `new_allowance` is more than zero. + AssetsOf::::cancel_approval( + origin.clone(), + id_param.clone(), + spender_source.clone(), + ) + .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; let new_allowance = current_allowance.saturating_sub(value); - if new_allowance.is_zero() { - return Ok(Some(Self::weight_approve(0, 1)).into()); - } - AssetsOf::::approve_transfer(origin, id, spender, new_allowance)?; - Ok(().into()) + let weight = if new_allowance.is_zero() { + Self::weight_approve(0, 1) + } else { + AssetsOf::::approve_transfer(origin, id_param, spender_source, new_allowance)?; + Self::weight_approve(1, 1) + }; + Self::deposit_event(Event::Approval { id, owner, spender, value: new_allowance }); + Ok(Some(weight).into()) } /// Create a new token with a given asset ID. @@ -252,8 +327,15 @@ pub mod pallet { admin: AccountIdOf, min_balance: BalanceOf, ) -> DispatchResult { - let admin = T::Lookup::unlookup(admin); - AssetsOf::::create(origin, id.into(), admin, min_balance) + let creator = ensure_signed(origin.clone())?; + AssetsOf::::create( + origin, + id.clone().into(), + T::Lookup::unlookup(admin.clone()), + min_balance, + )?; + Self::deposit_event(Event::Create { id, creator, admin }); + Ok(()) } /// Start the process of destroying a token with a given asset ID. @@ -297,7 +379,7 @@ pub mod pallet { AssetsOf::::clear_metadata(origin, id.into()) } - /// Creates `value` amount tokens and assigns them to `account`, increasing the total supply. + /// Creates `value` amount of tokens and assigns them to `account`, increasing the total supply. /// /// # Parameters /// - `id` - The ID of the asset. @@ -311,11 +393,17 @@ pub mod pallet { account: AccountIdOf, value: BalanceOf, ) -> DispatchResult { - let account = T::Lookup::unlookup(account); - AssetsOf::::mint(origin, id.into(), account, value) + AssetsOf::::mint( + origin, + id.clone().into(), + T::Lookup::unlookup(account.clone()), + value, + )?; + Self::deposit_event(Event::Transfer { id, from: None, to: Some(account), value }); + Ok(()) } - /// Destroys `value` amount tokens from `account`, reducing the total supply. + /// Destroys `value` amount of tokens from `account`, reducing the total supply. /// /// # Parameters /// - `id` - The ID of the asset. @@ -329,8 +417,14 @@ pub mod pallet { account: AccountIdOf, value: BalanceOf, ) -> DispatchResult { - let account = T::Lookup::unlookup(account); - AssetsOf::::burn(origin, id.into(), account, value) + AssetsOf::::burn( + origin, + id.clone().into(), + T::Lookup::unlookup(account.clone()), + value, + )?; + Self::deposit_event(Event::Transfer { id, from: Some(account), to: None, value }); + Ok(()) } } diff --git a/pallets/api/src/fungibles/tests.rs b/pallets/api/src/fungibles/tests.rs index d6cc87e0..ca2a85c9 100644 --- a/pallets/api/src/fungibles/tests.rs +++ b/pallets/api/src/fungibles/tests.rs @@ -2,6 +2,7 @@ use crate::{fungibles::Read::*, mock::*}; use codec::Encode; use frame_support::{ assert_ok, + sp_runtime::traits::Zero, traits::fungibles::{ approvals::Inspect as ApprovalInspect, metadata::Inspect as MetadataInspect, Inspect, }, @@ -9,34 +10,45 @@ use frame_support::{ const ASSET: u32 = 42; +type Event = crate::fungibles::Event; + #[test] fn transfer_works() { new_test_ext().execute_with(|| { - let amount: Balance = 100 * UNIT; - create_asset_and_mint_to(ALICE, ASSET, ALICE, amount); - let balance_before_transfer = Assets::balance(ASSET, &BOB); - assert_ok!(Fungibles::transfer(signed(ALICE), ASSET, BOB, amount / 2)); - let balance_after_transfer = Assets::balance(ASSET, &BOB); - assert_eq!(balance_after_transfer, balance_before_transfer + amount / 2); + let value: Balance = 100 * UNIT; + let id = ASSET; + let from = Some(ALICE); + let to = Some(BOB); + + create_asset_and_mint_to(ALICE, id, ALICE, value * 2); + let balance_before_transfer = Assets::balance(id, &BOB); + assert_ok!(Fungibles::transfer(signed(ALICE), id, BOB, value)); + let balance_after_transfer = Assets::balance(id, &BOB); + assert_eq!(balance_after_transfer, balance_before_transfer + value); + System::assert_last_event(Event::Transfer { id, from, to, value }.into()); }); } #[test] fn transfer_from_works() { new_test_ext().execute_with(|| { - let amount: Balance = 100 * UNIT; - // Approve CHARLIE to transfer up to `amount` to BOB. - create_asset_mint_and_approve(ALICE, ASSET, ALICE, amount * 2, CHARLIE, amount / 2); - let transferred = amount / 2; + let value: Balance = 100 * UNIT; + let id = ASSET; + let from = Some(ALICE); + let to = Some(BOB); + + // Approve CHARLIE to transfer up to `value` to BOB. + create_asset_mint_and_approve(ALICE, id, ALICE, value * 2, CHARLIE, value); // Successfully call transfer from. - let alice_balance_before_transfer = Assets::balance(ASSET, &ALICE); - let balance_before_transfer = Assets::balance(ASSET, &BOB); - assert_ok!(Fungibles::transfer_from(signed(CHARLIE), ASSET, ALICE, BOB, transferred)); - let alice_balance_after_transfer = Assets::balance(ASSET, &ALICE); - let balance_after_transfer = Assets::balance(ASSET, &BOB); - // Check that BOB receives the `amount` and ALICE `amount` is spent successfully by CHARLIE. - assert_eq!(balance_after_transfer, balance_before_transfer + transferred); - assert_eq!(alice_balance_after_transfer, alice_balance_before_transfer - transferred); + let alice_balance_before_transfer = Assets::balance(id, &ALICE); + let bob_balance_before_transfer = Assets::balance(id, &BOB); + assert_ok!(Fungibles::transfer_from(signed(CHARLIE), id, ALICE, BOB, value)); + let alice_balance_after_transfer = Assets::balance(id, &ALICE); + let bob_balance_after_transfer = Assets::balance(id, &BOB); + // Check that BOB receives the `value` and ALICE `amount` is spent successfully by CHARLIE. + assert_eq!(bob_balance_after_transfer, bob_balance_before_transfer + value); + assert_eq!(alice_balance_after_transfer, alice_balance_before_transfer - value); + System::assert_last_event(Event::Transfer { id, from, to, value }.into()); }); } @@ -44,130 +56,169 @@ fn transfer_from_works() { #[test] fn approve_works() { new_test_ext().execute_with(|| { - let amount: Balance = 100 * UNIT; - create_asset_and_mint_to(ALICE, ASSET, ALICE, amount); - assert_eq!(0, Assets::allowance(ASSET, &ALICE, &BOB)); - assert_ok!(Fungibles::approve(signed(ALICE), ASSET, BOB, amount)); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount); - // Approves an amount to spend that is lower than the current allowance. - assert_ok!(Fungibles::approve(signed(ALICE), ASSET, BOB, amount / 2)); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount / 2); - // Approves an amount to spend that is higher than the current allowance. - assert_ok!(Fungibles::approve(signed(ALICE), ASSET, BOB, amount * 2)); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount * 2); - // Approves an amount to spend that is equal to the current allowance. - assert_ok!(Fungibles::approve(signed(ALICE), ASSET, BOB, amount * 2)); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount * 2); + let value: Balance = 100 * UNIT; + let id = ASSET; + let owner = ALICE; + let spender = BOB; + + create_asset_and_mint_to(ALICE, id, ALICE, value); + assert_eq!(0, Assets::allowance(id, &ALICE, &BOB)); + assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value)); + assert_eq!(Assets::allowance(id, &ALICE, &BOB), value); + System::assert_last_event(Event::Approval { id, owner, spender, value }.into()); + // Approves an value to spend that is lower than the current allowance. + assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value / 2)); + assert_eq!(Assets::allowance(id, &ALICE, &BOB), value / 2); + System::assert_last_event(Event::Approval { id, owner, spender, value: value / 2 }.into()); + // Approves an value to spend that is higher than the current allowance. + assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value * 2)); + assert_eq!(Assets::allowance(id, &ALICE, &BOB), value * 2); + System::assert_last_event(Event::Approval { id, owner, spender, value: value * 2 }.into()); + // Approves an value to spend that is equal to the current allowance. + assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value * 2)); + assert_eq!(Assets::allowance(id, &ALICE, &BOB), value * 2); + System::assert_last_event(Event::Approval { id, owner, spender, value: value * 2 }.into()); // Sets allowance to zero. - assert_ok!(Fungibles::approve(signed(ALICE), ASSET, BOB, 0)); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), 0); + assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, 0)); + assert_eq!(Assets::allowance(id, &ALICE, &BOB), 0); + System::assert_last_event(Event::Approval { id, owner, spender, value: 0 }.into()); }); } #[test] fn increase_allowance_works() { new_test_ext().execute_with(|| { - let amount: Balance = 100 * UNIT; - create_asset_and_mint_to(ALICE, ASSET, ALICE, amount); - assert_eq!(0, Assets::allowance(ASSET, &ALICE, &BOB)); - assert_ok!(Fungibles::increase_allowance(signed(ALICE), ASSET, BOB, amount)); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount); + let value: Balance = 100 * UNIT; + let id = ASSET; + let owner = ALICE; + let spender = BOB; + + create_asset_and_mint_to(ALICE, id, ALICE, value); + assert_eq!(0, Assets::allowance(id, &ALICE, &BOB)); + assert_ok!(Fungibles::increase_allowance(signed(ALICE), id, BOB, value)); + assert_eq!(Assets::allowance(id, &ALICE, &BOB), value); + System::assert_last_event(Event::Approval { id, owner, spender, value }.into()); // Additive. - assert_ok!(Fungibles::increase_allowance(signed(ALICE), ASSET, BOB, amount)); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount * 2); + assert_ok!(Fungibles::increase_allowance(signed(ALICE), id, BOB, value)); + assert_eq!(Assets::allowance(id, &ALICE, &BOB), value * 2); + System::assert_last_event(Event::Approval { id, owner, spender, value: value * 2 }.into()); }); } #[test] fn decrease_allowance_works() { new_test_ext().execute_with(|| { - let amount: Balance = 100 * UNIT; - create_asset_mint_and_approve(ALICE, ASSET, ALICE, amount, BOB, amount); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount); + let value: Balance = 100 * UNIT; + let id = ASSET; + let owner = ALICE; + let spender = BOB; + + create_asset_mint_and_approve(ALICE, id, ALICE, value, BOB, value); + assert_eq!(Assets::allowance(id, &ALICE, &BOB), value); // Owner balance is not changed if decreased by zero. - assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, 0)); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount); + assert_ok!(Fungibles::decrease_allowance(signed(ALICE), id, BOB, 0)); + assert_eq!(Assets::allowance(id, &ALICE, &BOB), value); // Decrease allowance successfully. - assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, amount / 2 - 1 * UNIT)); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount / 2 + 1 * UNIT); + assert_ok!(Fungibles::decrease_allowance(signed(ALICE), id, BOB, value / 2)); + assert_eq!(Assets::allowance(id, &ALICE, &BOB), value / 2); + System::assert_last_event(Event::Approval { id, owner, spender, value: value / 2 }.into()); // Saturating if current allowance is decreased more than the owner balance. - assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, amount)); - assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), 0); + assert_ok!(Fungibles::decrease_allowance(signed(ALICE), id, BOB, value)); + assert_eq!(Assets::allowance(id, &ALICE, &BOB), 0); + System::assert_last_event(Event::Approval { id, owner, spender, value: 0 }.into()); }); } #[test] fn create_works() { new_test_ext().execute_with(|| { - assert!(!Assets::asset_exists(ASSET)); - assert_ok!(Fungibles::create(signed(ALICE), ASSET, ALICE, 100)); - assert!(Assets::asset_exists(ASSET)); + let id = ASSET; + let creator = ALICE; + let admin = ALICE; + + assert!(!Assets::asset_exists(id)); + assert_ok!(Fungibles::create(signed(creator), id, admin, 100)); + assert!(Assets::asset_exists(id)); + System::assert_last_event(Event::Create { id, creator, admin }.into()); }); } #[test] fn start_destroy_works() { new_test_ext().execute_with(|| { - create_asset(ALICE, ASSET); - assert_ok!(Fungibles::start_destroy(signed(ALICE), ASSET)); + let id = ASSET; + + create_asset(ALICE, id); + assert_ok!(Fungibles::start_destroy(signed(ALICE), id)); }); } #[test] fn set_metadata_works() { new_test_ext().execute_with(|| { + let id = ASSET; let name = vec![42]; let symbol = vec![42]; let decimals = 42; - create_asset(ALICE, ASSET); + + create_asset(ALICE, id); assert_ok!(Fungibles::set_metadata( signed(ALICE), - ASSET, + id, name.clone(), symbol.clone(), decimals )); - assert_eq!(Assets::name(ASSET), name); - assert_eq!(Assets::symbol(ASSET), symbol); - assert_eq!(Assets::decimals(ASSET), decimals); + assert_eq!(Assets::name(id), name); + assert_eq!(Assets::symbol(id), symbol); + assert_eq!(Assets::decimals(id), decimals); }); } #[test] fn clear_metadata_works() { new_test_ext().execute_with(|| { - let name = vec![42]; - let symbol = vec![42]; - let decimals = 42; - create_asset_and_set_metadata(ALICE, ASSET, name, symbol, decimals); - assert_ok!(Fungibles::clear_metadata(signed(ALICE), ASSET)); - assert_eq!(Assets::name(ASSET), Vec::::new()); - assert_eq!(Assets::symbol(ASSET), Vec::::new()); - assert_eq!(Assets::decimals(ASSET), 0u8); + let id = ASSET; + + create_asset_and_set_metadata(ALICE, id, vec![42], vec![42], 42); + assert_ok!(Fungibles::clear_metadata(signed(ALICE), id)); + assert!(Assets::name(id).is_empty()); + assert!(Assets::symbol(id).is_empty()); + assert!(Assets::decimals(id).is_zero()); }); } #[test] fn mint_works() { new_test_ext().execute_with(|| { - let amount: Balance = 100 * UNIT; - create_asset(ALICE, ASSET); - let balance_before_mint = Assets::balance(ASSET, &BOB); - assert_ok!(Fungibles::mint(signed(ALICE), ASSET, BOB, amount)); - let balance_after_mint = Assets::balance(ASSET, &BOB); - assert_eq!(balance_after_mint, balance_before_mint + amount); + let value: Balance = 100 * UNIT; + let id = ASSET; + let from = None; + let to = Some(BOB); + + create_asset(ALICE, id); + let balance_before_mint = Assets::balance(id, &BOB); + assert_ok!(Fungibles::mint(signed(ALICE), id, BOB, value)); + let balance_after_mint = Assets::balance(id, &BOB); + assert_eq!(balance_after_mint, balance_before_mint + value); + System::assert_last_event(Event::Transfer { id, from, to, value }.into()); }); } #[test] fn burn_works() { new_test_ext().execute_with(|| { - let amount: Balance = 100 * UNIT; - create_asset_and_mint_to(ALICE, ASSET, BOB, amount); - let balance_before_burn = Assets::balance(ASSET, &BOB); - assert_ok!(Fungibles::burn(signed(ALICE), ASSET, BOB, amount)); - let balance_after_burn = Assets::balance(ASSET, &BOB); - assert_eq!(balance_after_burn, balance_before_burn - amount); + let value: Balance = 100 * UNIT; + let id = ASSET; + let from = Some(BOB); + let to = None; + + create_asset_and_mint_to(ALICE, id, BOB, value); + let balance_before_burn = Assets::balance(id, &BOB); + assert_ok!(Fungibles::burn(signed(ALICE), id, BOB, value)); + let balance_after_burn = Assets::balance(id, &BOB); + assert_eq!(balance_after_burn, balance_before_burn - value); + System::assert_last_event(Event::Transfer { id, from, to, value }.into()); }); } diff --git a/pallets/api/src/mock.rs b/pallets/api/src/mock.rs index b20e2635..77e17394 100644 --- a/pallets/api/src/mock.rs +++ b/pallets/api/src/mock.rs @@ -97,6 +97,7 @@ impl pallet_assets::Config for Test { type BenchmarkHelper = (); } impl crate::fungibles::Config for Test { + type RuntimeEvent = RuntimeEvent; type AssetsInstance = AssetsInstance; type WeightInfo = (); } diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index 8600c439..323db2b6 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -58,6 +58,91 @@ mod constants { pub(super) const BURN: u8 = 20; } +/// A set of events for use in smart contracts interacting with the fungibles API. +/// +/// The `Transfer` and `Approval` events conform to the PSP-22 standard. The other events +/// (`Create`, `StartDestroy`, `SetMetadata`, `ClearMetadata`) are provided for convenience. +/// +/// These events are not emitted by the API itself but can be used in your contracts to +/// track asset operations. Be mindful of the costs associated with emitting events. +/// +/// For more details, refer to [ink! events](https://use.ink/basics/events). +pub mod events { + use super::*; + + /// Event emitted when allowance by `owner` to `spender` changes. + #[ink::event] + pub struct Approval { + /// Account providing allowance. + #[ink(topic)] + pub owner: AccountId, + /// Allowance beneficiary. + #[ink(topic)] + pub spender: AccountId, + /// New allowance amount. + pub value: u128, + } + + /// Event emitted when transfer of tokens occurs. + #[ink::event] + pub struct Transfer { + /// Transfer sender. `None` in case of minting new tokens. + #[ink(topic)] + pub from: Option, + /// Transfer recipient. `None` in case of burning tokens. + #[ink(topic)] + pub to: Option, + /// Amount of tokens transferred (or minted/burned). + pub value: u128, + } + + /// Event emitted when a token class is created. + #[ink::event] + pub struct Create { + /// The ID of the asset. + #[ink(topic)] + pub id: AssetId, + /// Creator of the asset. + #[ink(topic)] + pub creator: AccountId, + /// Admin of the asset. + #[ink(topic)] + pub admin: AccountId, + } + + /// Event emitted when a asset is in the process of being destroyed. + #[ink::event] + pub struct StartDestroy { + /// The ID of the asset. + #[ink(topic)] + pub id: AssetId, + } + + /// Event emitted when new metadata is set for an asset. + #[ink::event] + pub struct SetMetadata { + /// The ID of the asset created. + #[ink(topic)] + pub id: AssetId, + /// The name of the asset. + #[ink(topic)] + pub name: Vec, + /// The symbol of the asset. + #[ink(topic)] + pub symbol: Vec, + /// The decimals of the asset. + pub decimals: u8, + } + + /// Event emitted when metadata is cleared for a token. + #[ink::event] + pub struct ClearMetadata { + /// The ID of the asset. + #[ink(topic)] + pub id: AssetId, + } +} + /// Returns the total token supply for a given asset ID. /// /// # Parameters diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index 5f234cd4..4a404c99 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -1,4 +1,6 @@ -use crate::{config::assets::TrustBackedAssetsInstance, fungibles, Runtime, RuntimeCall}; +use crate::{ + config::assets::TrustBackedAssetsInstance, fungibles, Runtime, RuntimeCall, RuntimeEvent, +}; use codec::{Decode, Encode, MaxEncodedLen}; use pop_chain_extension::{CallFilter, ReadState}; use sp_std::vec::Vec; @@ -60,6 +62,7 @@ impl CallFilter for Extension { } impl fungibles::Config for Runtime { + type RuntimeEvent = RuntimeEvent; type AssetsInstance = TrustBackedAssetsInstance; type WeightInfo = fungibles::weights::SubstrateWeight; } From f13b39cb85417830aa7951fab5a29e5b407812f2 Mon Sep 17 00:00:00 2001 From: Daan van der Plas <93204684+Daanvdplas@users.noreply.github.com> Date: Fri, 16 Aug 2024 10:27:06 +0200 Subject: [PATCH 40/76] docs: pop api (#190) Co-authored-by: Frank Bell <60948618+evilrobot-01@users.noreply.github.com> --- pop-api/src/lib.rs | 23 +++++--- pop-api/src/v0/assets/fungibles.rs | 91 ++++++++---------------------- pop-api/src/v0/assets/mod.rs | 1 + pop-api/src/v0/mod.rs | 21 +++---- 4 files changed, 53 insertions(+), 83 deletions(-) diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 78a79f80..cc785c14 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -1,3 +1,10 @@ +//! The `pop-api` crate provides an API for smart contracts to interact with the Pop Network runtime. +//! +//! This crate abstracts away complexities to deliver a streamlined developer experience while supporting +//! multiple API versions to ensure backward compatibility. It is designed with a focus on stability, +//! future-proofing, and storage efficiency, allowing developers to easily integrate powerful runtime +//! features into their contracts without unnecessary overhead. + #![cfg_attr(not(feature = "std"), no_std, no_main)] use constants::DECODING_FAILED; @@ -5,7 +12,9 @@ use ink::env::chain_extension::{ChainExtensionMethod, FromStatusCode}; #[cfg(feature = "assets")] pub use v0::assets; +/// Module providing primitives types. pub mod primitives; +/// The first version of the API. pub mod v0; /// A result type used by the API, with the `StatusCode` as the error type. @@ -27,13 +36,13 @@ mod constants { pub(crate) const FUNGIBLES: u8 = 150; } -/// Helper method to build `ChainExtensionMethod`. -/// -/// Parameters: -/// - 'version': The version of the chain extension. -/// - 'function': The ID of the function. -/// - 'module': The index of the runtime module. -/// - 'dispatchable': The index of the module dispatchable functions. +// Helper method to build a dispatch call or a call to read state. +// +// Parameters: +// - 'version': The version of the chain extension. +// - 'function': The ID of the function. +// - 'module': The index of the runtime module. +// - 'dispatchable': The index of the module dispatchable functions. fn build_extension_method( version: u8, function: u8, diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index 323db2b6..c881f823 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -1,3 +1,11 @@ +//! The `fungibles` module provides an API for interacting and managing fungible assets on Pop Network. +//! +//! The API includes the following interfaces: +//! 1. PSP-22 +//! 2. PSP-22 Metadata +//! 3. Asset Management +//! 4. PSP-22 Mintable & Burnable + use crate::{ constants::{ASSETS, BALANCES, FUNGIBLES}, primitives::{AccountId, AssetId, Balance}, @@ -8,28 +16,22 @@ use constants::*; use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec}; pub use metadata::*; -/// Helper method to build a dispatch call `ChainExtensionMethod` for fungibles `v0`. -/// -/// Parameters: -/// - 'dispatchable': The index of the module dispatchable functions. +// Helper method to build a dispatch call. +// +// Parameters: +// - 'dispatchable': The index of the dispatchable function within the module. fn build_dispatch(dispatchable: u8) -> ChainExtensionMethod<(), (), (), false> { crate::v0::build_dispatch(FUNGIBLES, dispatchable) } -/// Helper method to build a dispatch call `ChainExtensionMethod` for fungibles `v0`. -/// -/// Parameters: -/// - 'state_query': The index of the runtime state query. +// Helper method to build a call to read state. +// +// Parameters: +// - 'state_query': The index of the runtime state query. fn build_read_state(state_query: u8) -> ChainExtensionMethod<(), (), (), false> { crate::v0::build_read_state(FUNGIBLES, state_query) } -/// Local Fungibles: -/// 1. PSP-22 Interface -/// 2. PSP-22 Metadata Interface -/// 3. Asset Management -/// 4. PSP-22 Mintable & Burnable Interface - mod constants { /// 1. PSP-22 Interface: pub(super) const TOTAL_SUPPLY: u8 = 0; @@ -147,9 +149,6 @@ pub mod events { /// /// # Parameters /// - `id` - The ID of the asset. -/// -/// # Returns -/// The total supply of the token, or an error if the operation fails. #[inline] pub fn total_supply(id: AssetId) -> Result { build_read_state(TOTAL_SUPPLY) @@ -165,9 +164,6 @@ pub fn total_supply(id: AssetId) -> Result { /// # Parameters /// - `id` - The ID of the asset. /// - `owner` - The account whose balance is being queried. -/// -/// # Returns -/// The balance of the specified account, or an error if the operation fails. #[inline] pub fn balance_of(id: AssetId, owner: AccountId) -> Result { build_read_state(BALANCE_OF) @@ -184,9 +180,6 @@ pub fn balance_of(id: AssetId, owner: AccountId) -> Result { /// - `id` - The ID of the asset. /// - `owner` - The account that owns the tokens. /// - `spender` - The account that is allowed to spend the tokens. -/// -/// # Returns -/// The remaining allowance, or an error if the operation fails. #[inline] pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result { build_read_state(ALLOWANCE) @@ -203,9 +196,6 @@ pub fn allowance(id: AssetId, owner: AccountId, spender: AccountId) -> Result Result<()> { build_dispatch(TRANSFER) @@ -223,9 +213,6 @@ pub fn transfer(id: AssetId, to: AccountId, value: Balance) -> Result<()> { /// - `from` - The account from which the tokens are transferred. /// - `to` - The recipient account. /// - `value` - The number of tokens to transfer. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the transfer fails. #[inline] pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, value: Balance) -> Result<()> { build_dispatch(TRANSFER_FROM) @@ -241,9 +228,6 @@ pub fn transfer_from(id: AssetId, from: AccountId, to: AccountId, value: Balance /// - `id` - The ID of the asset. /// - `spender` - The account that is allowed to spend the tokens. /// - `value` - The number of tokens to approve. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the approval fails. #[inline] pub fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { build_dispatch(APPROVE) @@ -259,9 +243,6 @@ pub fn approve(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { /// - `id` - The ID of the asset. /// - `spender` - The account that is allowed to spend the tokens. /// - `value` - The number of tokens to increase the allowance by. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. #[inline] pub fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { build_dispatch(INCREASE_ALLOWANCE) @@ -277,9 +258,6 @@ pub fn increase_allowance(id: AssetId, spender: AccountId, value: Balance) -> Re /// - `id` - The ID of the asset. /// - `spender` - The account that is allowed to spend the tokens. /// - `value` - The number of tokens to decrease the allowance by. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. #[inline] pub fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Result<()> { build_dispatch(DECREASE_ALLOWANCE) @@ -295,9 +273,7 @@ pub fn decrease_allowance(id: AssetId, spender: AccountId, value: Balance) -> Re /// - `id` - The ID of the asset. /// - `account` - The account to be credited with the created tokens. /// - `value` - The number of tokens to mint. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. +#[inline] pub fn mint(id: AssetId, account: AccountId, value: Balance) -> Result<()> { build_dispatch(MINT) .input::<(AssetId, AccountId, Balance)>() @@ -312,9 +288,7 @@ pub fn mint(id: AssetId, account: AccountId, value: Balance) -> Result<()> { /// - `id` - The ID of the asset. /// - `account` - The account from which the tokens will be destroyed. /// - `value` - The number of tokens to destroy. -/// -/// # Returns -/// Returns `Ok(())` if successful, or an error if the operation fails. +#[inline] pub fn burn(id: AssetId, account: AccountId, value: Balance) -> Result<()> { build_dispatch(BURN) .input::<(AssetId, AccountId, Balance)>() @@ -323,6 +297,7 @@ pub fn burn(id: AssetId, account: AccountId, value: Balance) -> Result<()> { .call(&(id, account, value)) } +/// The PSP-22 Metadata interface for querying metadata. pub mod metadata { use super::*; @@ -330,9 +305,6 @@ pub mod metadata { /// /// # Parameters /// - `id` - The ID of the asset. - /// - /// # Returns - /// The name of the token as a byte vector, or an error if the operation fails. #[inline] pub fn token_name(id: AssetId) -> Result> { build_read_state(TOKEN_NAME) @@ -346,9 +318,6 @@ pub mod metadata { /// /// # Parameters /// - `id` - The ID of the asset. - /// - /// # Returns - /// The symbol of the token as a byte vector, or an error if the operation fails. #[inline] pub fn token_symbol(id: AssetId) -> Result> { build_read_state(TOKEN_SYMBOL) @@ -362,9 +331,6 @@ pub mod metadata { /// /// # Parameters /// - `id` - The ID of the asset. - /// - /// # Returns - /// The number of decimals of the token, or an error if the operation fails. #[inline] pub fn token_decimals(id: AssetId) -> Result { build_read_state(TOKEN_DECIMALS) @@ -375,7 +341,8 @@ pub mod metadata { } } -pub mod asset_management { +/// The interface for creating, managing and destroying fungible assets. +pub mod management { use super::*; /// Create a new token with a given asset ID. @@ -384,9 +351,7 @@ pub mod asset_management { /// - `id` - The ID of the asset. /// - `admin` - The account that will administer the asset. /// - `min_balance` - The minimum balance required for accounts holding this asset. - /// - /// # Returns - /// Returns `Ok(())` if successful, or an error if the creation fails. + #[inline] pub fn create(id: AssetId, admin: AccountId, min_balance: Balance) -> Result<()> { build_dispatch(CREATE) .input::<(AssetId, AccountId, Balance)>() @@ -399,9 +364,7 @@ pub mod asset_management { /// /// # Parameters /// - `id` - The ID of the asset. - /// - /// # Returns - /// Returns `Ok(())` if successful, or an error if the operation fails. + #[inline] pub fn start_destroy(id: AssetId) -> Result<()> { build_dispatch(START_DESTROY) .input::() @@ -417,9 +380,7 @@ pub mod asset_management { /// - `name`: The user friendly name of this asset. Limited in length by `StringLimit`. /// - `symbol`: The exchange symbol for this asset. Limited in length by `StringLimit`. /// - `decimals`: The number of decimals this asset uses to represent one unit. - /// - /// # Returns - /// Returns `Ok(())` if successful, or an error if the operation fails. + #[inline] pub fn set_metadata(id: AssetId, name: Vec, symbol: Vec, decimals: u8) -> Result<()> { build_dispatch(SET_METADATA) .input::<(AssetId, Vec, Vec, u8)>() @@ -432,9 +393,7 @@ pub mod asset_management { /// /// # Parameters /// - `id` - The ID of the asset. - /// - /// # Returns - /// Returns `Ok(())` if successful, or an error if the operation fails. + #[inline] pub fn clear_metadata(id: AssetId) -> Result<()> { build_dispatch(CLEAR_METADATA) .input::() diff --git a/pop-api/src/v0/assets/mod.rs b/pop-api/src/v0/assets/mod.rs index 197db710..2d5ae236 100644 --- a/pop-api/src/v0/assets/mod.rs +++ b/pop-api/src/v0/assets/mod.rs @@ -1,2 +1,3 @@ +/// APIs for fungible assets. #[cfg(feature = "fungibles")] pub mod fungibles; diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index 0fd06c89..a4824327 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -6,6 +6,7 @@ use crate::{ }; use ink::env::chain_extension::ChainExtensionMethod; +/// APIs for asset-related use cases. #[cfg(feature = "assets")] pub mod assets; @@ -17,20 +18,20 @@ impl From for Error { } } -/// Helper method to build a dispatch call `ChainExtensionMethod` -/// -/// Parameters: -/// - 'module': The index of the runtime module -/// - 'dispatchable': The index of the module dispatchable functions +// Helper method to build a dispatch call. +// +// Parameters: +// - 'module': The index of the runtime module. +// - 'dispatchable': The index of the module dispatchable functions. fn build_dispatch(module: u8, dispatchable: u8) -> ChainExtensionMethod<(), (), (), false> { build_extension_method(V0, DISPATCH, module, dispatchable) } -/// Helper method to build a dispatch call `ChainExtensionMethod` -/// -/// Parameters: -/// - 'module': The index of the runtime module -/// - 'state_query': The index of the runtime state query +// Helper method to build a call to read state. +// +// Parameters: +// - 'module': The index of the runtime module. +// - 'state_query': The index of the runtime state query. fn build_read_state(module: u8, state_query: u8) -> ChainExtensionMethod<(), (), (), false> { build_extension_method(V0, READ_STATE, module, state_query) } From 7c942847cc6b95c5fa6fef7033ba2978f7fe236d Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Fri, 16 Aug 2024 14:10:46 +0200 Subject: [PATCH 41/76] refactor: extension --- extension/src/lib.rs | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/extension/src/lib.rs b/extension/src/lib.rs index 582f346c..650f73f8 100644 --- a/extension/src/lib.rs +++ b/extension/src/lib.rs @@ -1,9 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(test)] -mod tests; -mod v0; - use codec::Encode; use frame_support::{ dispatch::{GetDispatchInfo, PostDispatchInfo}, @@ -18,6 +14,10 @@ use sp_core::crypto::UncheckedFrom; use sp_runtime::{traits::Dispatchable, DispatchError}; use sp_std::vec::Vec; +#[cfg(test)] +mod tests; +mod v0; + /// Logging target for categorizing messages from the Pop API extension module. const LOG_TARGET: &str = "pop-api::extension"; @@ -113,7 +113,7 @@ where } } -/// Extract (version, function_id, pallet_index, call_index) from the payload bytes. +/// Extract discriminators (version, function_id, pallet_index, call_index) from the encoded call. fn extract_env>(env: &Environment) -> (u8, u8, u8, u8) { // Extract version and function_id from first two bytes. let (version, function_id) = { @@ -136,8 +136,6 @@ fn read_state, StateReader: ReadState>( call_index: u8, mut params: Vec, ) -> Result<(), DispatchError> { - const LOG_PREFIX: &str = " read_state |"; - // Prefix params with version, pallet, index to simplify decoding. params.insert(0, version); params.insert(1, pallet_index); @@ -156,7 +154,7 @@ fn read_state, StateReader: ReadState>( }; log::trace!( target:LOG_TARGET, - "{} result: {:?}.", LOG_PREFIX, result + "{} result: {:?}.", " read_state |", result ); env.write(&result, false, None) } @@ -175,8 +173,6 @@ where E: Ext, Filter: CallFilter::RuntimeCall> + 'static, { - const LOG_PREFIX: &str = " dispatch |"; - // Prefix params with version, pallet, index to simplify decoding. params.insert(0, version); params.insert(1, pallet_index); @@ -185,7 +181,7 @@ where // Contract is the origin by default. let origin: T::RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); match call { - VersionedDispatch::V0(call) => dispatch_call::(env, call, origin, LOG_PREFIX), + VersionedDispatch::V0(call) => dispatch_call::(env, call, origin), } } @@ -198,7 +194,6 @@ fn dispatch_call( env: &mut Environment, call: T::RuntimeCall, mut origin: T::RuntimeOrigin, - log_prefix: &str, ) -> Result<(), DispatchError> where T: frame_system::Config< @@ -207,12 +202,14 @@ where E: Ext, Filter: CallFilter::RuntimeCall> + 'static, { + const LOG_PREFIX: &str = " dispatch |"; + let charged_dispatch_weight = env.charge_weight(call.get_dispatch_info().weight)?; - log::debug!(target:LOG_TARGET, "{} Inputted RuntimeCall: {:?}", log_prefix, call); + log::debug!(target:LOG_TARGET, "{} Inputted RuntimeCall: {:?}", LOG_PREFIX, call); origin.add_filter(Filter::contains); match call.dispatch(origin) { Ok(info) => { - log::debug!(target:LOG_TARGET, "{} success, actual weight: {:?}", log_prefix, info.actual_weight); + log::debug!(target:LOG_TARGET, "{} success, actual weight: {:?}", LOG_PREFIX, info.actual_weight); // Refund weight if the actual weight is less than the charged weight. if let Some(actual_weight) = info.actual_weight { env.adjust_weight(charged_dispatch_weight, actual_weight); @@ -220,7 +217,11 @@ where Ok(()) }, Err(err) => { - log::debug!(target:LOG_TARGET, "{} failed: error: {:?}", log_prefix, err.error); + log::debug!(target:LOG_TARGET, "{} failed: error: {:?}", LOG_PREFIX, err.error); + // Refund weight if the actual weight is less than the charged weight. + if let Some(actual_weight) = err.post_info.actual_weight { + env.adjust_weight(charged_dispatch_weight, actual_weight); + } Err(err.error) }, } @@ -275,7 +276,7 @@ impl TryFrom for FuncId { } /// Converts a `DispatchError` to a `u32` status code based on the version of the API the contract uses. -/// The contract calling the chain extension can convert the status code to the descriptive `Error`. +/// The contract calling the chain extension can optionally convert the status code to the descriptive `Error`. /// /// For `Error` see `pop_primitives::::error::Error`. /// From 5934fa8d623462303f032e0a80097eeaf591d62e Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Fri, 16 Aug 2024 14:36:55 +0200 Subject: [PATCH 42/76] fix: remove old api code from deprecated examples --- pop-api/examples/balance-transfer/lib.rs | 2 +- pop-api/examples/nfts/lib.rs | 2 +- pop-api/examples/place-spot-order/lib.rs | 2 +- pop-api/examples/read-runtime-state/lib.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pop-api/examples/balance-transfer/lib.rs b/pop-api/examples/balance-transfer/lib.rs index d36dfa53..e75c15b9 100755 --- a/pop-api/examples/balance-transfer/lib.rs +++ b/pop-api/examples/balance-transfer/lib.rs @@ -15,7 +15,7 @@ impl From for ContractError { } } -#[ink::contract(env = pop_api::Environment)] +#[ink::contract] mod balance_transfer { use super::*; diff --git a/pop-api/examples/nfts/lib.rs b/pop-api/examples/nfts/lib.rs index 7920c179..0cd0f313 100755 --- a/pop-api/examples/nfts/lib.rs +++ b/pop-api/examples/nfts/lib.rs @@ -18,7 +18,7 @@ impl From for ContractError { } } -#[ink::contract(env = pop_api::Environment)] +#[ink::contract] mod nfts { use super::*; diff --git a/pop-api/examples/place-spot-order/lib.rs b/pop-api/examples/place-spot-order/lib.rs index 669b9190..965917d1 100755 --- a/pop-api/examples/place-spot-order/lib.rs +++ b/pop-api/examples/place-spot-order/lib.rs @@ -1,7 +1,7 @@ // DEPRECATED #![cfg_attr(not(feature = "std"), no_std, no_main)] -#[ink::contract(env = pop_api::Environment)] +#[ink::contract] mod spot_order { #[ink(storage)] diff --git a/pop-api/examples/read-runtime-state/lib.rs b/pop-api/examples/read-runtime-state/lib.rs index 60ef70f0..092e9f2f 100755 --- a/pop-api/examples/read-runtime-state/lib.rs +++ b/pop-api/examples/read-runtime-state/lib.rs @@ -1,7 +1,7 @@ // DEPRECATED #![cfg_attr(not(feature = "std"), no_std, no_main)] -#[ink::contract(env = pop_api::Environment)] +#[ink::contract] mod read_relay_blocknumber { use pop_api::primitives::storage_keys::{ ParachainSystemKeys::LastRelayChainBlockNumber, RuntimeStateKeys::ParachainSystem, From f2ef7a184c67a80e14291786360a7b115b837c75 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Fri, 16 Aug 2024 14:59:40 +0200 Subject: [PATCH 43/76] refactor: runtime devnet --- Cargo.lock | 1 - runtime/devnet/Cargo.toml | 1 - runtime/devnet/src/config/mod.rs | 4 ++-- runtime/devnet/src/lib.rs | 4 ++-- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 78e5e1d1..3407a210 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9613,7 +9613,6 @@ dependencies = [ "pop-chain-extension", "pop-primitives", "pop-runtime-common", - "rand", "scale-info", "smallvec", "sp-api", diff --git a/runtime/devnet/Cargo.toml b/runtime/devnet/Cargo.toml index 995af269..3d2f20d0 100644 --- a/runtime/devnet/Cargo.toml +++ b/runtime/devnet/Cargo.toml @@ -92,7 +92,6 @@ parachain-info.workspace = true [dev-dependencies] env_logger = "0.11.2" hex = "0.4.3" -rand = "0.8.5" [features] default = ["std"] diff --git a/runtime/devnet/src/config/mod.rs b/runtime/devnet/src/config/mod.rs index f62ffa76..1dcd44da 100644 --- a/runtime/devnet/src/config/mod.rs +++ b/runtime/devnet/src/config/mod.rs @@ -1,5 +1,5 @@ -pub(crate) mod api; -pub mod assets; +mod api; +mod assets; mod contracts; mod proxy; // Public due to integration tests crate. diff --git a/runtime/devnet/src/lib.rs b/runtime/devnet/src/lib.rs index 4256ac01..e1ef6c6d 100644 --- a/runtime/devnet/src/lib.rs +++ b/runtime/devnet/src/lib.rs @@ -72,7 +72,7 @@ use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}; // XCM Imports use xcm::latest::prelude::BodyId; -pub(crate) use pallet_api::fungibles; +use pallet_api::fungibles; /// Some way of identifying an account on the chain. We intentionally make it equivalent /// to the public key of our transaction signing scheme. @@ -590,7 +590,7 @@ construct_runtime!( mod benches { frame_benchmarking::define_benchmarks!( [frame_system, SystemBench::] - [pallet_api::fungibles, Fungibles] + [fungibles, Fungibles] [pallet_balances, Balances] [pallet_session, SessionBench::] [pallet_timestamp, Timestamp] From b3dd9935cffabc5ccac2a92fe8355822b3275158 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Fri, 16 Aug 2024 15:56:50 +0200 Subject: [PATCH 44/76] refactor: fungibles pallet --- pallets/api/Cargo.toml | 2 +- pallets/api/src/fungibles/mod.rs | 232 +++++++++++++++-------------- pallets/api/src/fungibles/tests.rs | 202 +++++++++++++------------ 3 files changed, 226 insertions(+), 210 deletions(-) diff --git a/pallets/api/Cargo.toml b/pallets/api/Cargo.toml index a813a09c..f9a807a3 100644 --- a/pallets/api/Cargo.toml +++ b/pallets/api/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "pallet-api" authors.workspace = true -description = "Api pallet, enabling smart(er) contracts with the power of Polkadot" +description = "API pallet, enabling smart(er) contracts with the power of Polkadot" edition.workspace = true license.workspace = true version = "0.1.0" diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index 7aa9ac00..6ff8ac87 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -1,6 +1,11 @@ -/// The fungibles pallet serves as a wrapper around the pallet_assets, offering a streamlined -/// interface for interacting with fungible assets. The goal is to provide a simplified, consistent -/// API that adheres to standards in the smart contract space. +//! The fungibles pallet offers a streamlined interface for interacting with fungible assets. The +//! goal is to provide a simplified, consistent API that adheres to standards in the smart contract +//! space. + +use frame_support::traits::fungibles::{metadata::Inspect as MetadataInspect, Inspect}; +pub use pallet::*; +use pallet_assets::WeightInfo as AssetsWeightInfoTrait; +use weights::WeightInfo; #[cfg(feature = "runtime-benchmarks")] mod benchmarking; @@ -8,11 +13,6 @@ mod benchmarking; mod tests; pub mod weights; -use frame_support::traits::fungibles::{metadata::Inspect as MetadataInspect, Inspect}; -pub use pallet::*; -use pallet_assets::WeightInfo as AssetsWeightInfoTrait; -use weights::WeightInfo; - type AccountIdOf = ::AccountId; type AssetIdOf = > as Inspect< ::AccountId, @@ -41,42 +41,42 @@ pub mod pallet { }; use sp_std::vec::Vec; - /// State reads for the fungibles api with required input. + /// State reads for the fungibles API with required input. #[derive(Encode, Decode, Debug, MaxEncodedLen)] #[repr(u8)] #[allow(clippy::unnecessary_cast)] pub enum Read { - /// Total token supply for a given asset ID. + /// Total token supply for a specified asset. #[codec(index = 0)] TotalSupply(AssetIdOf), - /// Account balance for a given asset ID. + /// Account balance for a specified `asset` and `owner`. #[codec(index = 1)] BalanceOf { - /// The asset ID. - id: AssetIdOf, - /// The account ID of the owner. + /// The asset. + asset: AssetIdOf, + /// The owner of the asset. owner: AccountIdOf, }, - /// Allowance for a spender approved by an owner, for a given asset ID. + /// Allowance for a `spender` approved by an `owner`, for a specified `asset`. #[codec(index = 2)] Allowance { - /// The asset ID. - id: AssetIdOf, - /// The account ID of the owner. + /// The asset. + asset: AssetIdOf, + /// The owner of the asset. owner: AccountIdOf, - /// The account ID of the spender. + /// The spender with an allowance. spender: AccountIdOf, }, - /// Token name for a given asset ID. + /// Name of the specified asset. #[codec(index = 8)] TokenName(AssetIdOf), - /// Token symbol for a given asset ID. + /// Symbol for the specified asset. #[codec(index = 9)] TokenSymbol(AssetIdOf), - /// Token decimals for a given asset ID. + /// Decimals for the specified asset. #[codec(index = 10)] TokenDecimals(AssetIdOf), - /// Check if token with a given asset ID exists. + /// Check if a specified asset exists. #[codec(index = 18)] AssetExists(AssetIdOf), } @@ -101,70 +101,68 @@ pub mod pallet { pub enum Event { /// Event emitted when allowance by `owner` to `spender` changes. Approval { - /// The ID of the asset. - id: AssetIdOf, - /// Account providing allowance. + /// The asset. + asset: AssetIdOf, + /// The owner providing the allowance. owner: AccountIdOf, - /// Allowance beneficiary. + /// The beneficiary of the allowance. spender: AccountIdOf, - /// New allowance amount. + /// The new allowance amount. value: BalanceOf, }, - /// Event emitted when transfer of tokens occurs. + /// Event emitted when an asset transfer occurs. Transfer { - /// The ID of the asset. - id: AssetIdOf, - /// Transfer sender. `None` in case of minting new tokens. + /// The asset. + asset: AssetIdOf, + /// The source of the transfer. `None` when minting. from: Option>, - /// Transfer recipient. `None` in case of burning tokens. + /// The recipient of the transfer. `None` when burning. to: Option>, - /// Amount of tokens transferred (or minted/burned). + /// The amount transferred (or minted/burned). value: BalanceOf, }, - /// Event emitted when a token class is created. + /// Event emitted when an asset is created. Create { - /// The ID of the asset. + /// The asset identifier. id: AssetIdOf, - /// Creator of the asset. + /// The creator of the asset. creator: AccountIdOf, - /// Admin of the asset. + /// The administrator of the asset. admin: AccountIdOf, }, } #[pallet::call] impl Pallet { - /// Transfers `value` amount of tokens from the caller's account to account `to`, with additional - /// `data` in unspecified format. + /// Transfers `value` amount of tokens from the caller's account to account `to`. /// /// # Parameters - /// - `id` - The ID of the asset. + /// - `asset` - The asset to transfer. /// - `to` - The recipient account. /// - `value` - The number of tokens to transfer. #[pallet::call_index(3)] #[pallet::weight(AssetsWeightInfoOf::::transfer_keep_alive())] pub fn transfer( origin: OriginFor, - id: AssetIdOf, + asset: AssetIdOf, to: AccountIdOf, value: BalanceOf, ) -> DispatchResult { + let from = ensure_signed(origin.clone())?; AssetsOf::::transfer_keep_alive( - origin.clone(), - id.clone().into(), + origin, + asset.clone().into(), T::Lookup::unlookup(to.clone()), value, )?; - let from = ensure_signed(origin)?; - Self::deposit_event(Event::Transfer { id, from: Some(from), to: Some(to), value }); + Self::deposit_event(Event::Transfer { asset, from: Some(from), to: Some(to), value }); Ok(()) } - /// Transfers `value` amount tokens on behalf of `from` to account `to` with additional `data` - /// in unspecified format. + /// Transfers `value` amount tokens on behalf of `from` to account `to`. /// /// # Parameters - /// - `id` - The ID of the asset. + /// - `asset` - The asset to transfer. /// - `from` - The account from which the asset balance will be withdrawn. /// - `to` - The recipient account. /// - `value` - The number of tokens to transfer. @@ -172,39 +170,39 @@ pub mod pallet { #[pallet::weight(AssetsWeightInfoOf::::transfer_approved())] pub fn transfer_from( origin: OriginFor, - id: AssetIdOf, + asset: AssetIdOf, from: AccountIdOf, to: AccountIdOf, value: BalanceOf, ) -> DispatchResult { AssetsOf::::transfer_approved( origin, - id.clone().into(), + asset.clone().into(), T::Lookup::unlookup(from.clone()), T::Lookup::unlookup(to.clone()), value, )?; - Self::deposit_event(Event::Transfer { id, from: Some(from), to: Some(to), value }); + Self::deposit_event(Event::Transfer { asset, from: Some(from), to: Some(to), value }); Ok(()) } - /// Approves an account to spend a specified number of tokens on behalf of the caller. + /// Approves `spender` to spend `value` amount of tokens on behalf of the caller. /// /// # Parameters - /// - `id` - The ID of the asset. + /// - `asset` - The asset to approve. /// - `spender` - The account that is allowed to spend the tokens. /// - `value` - The number of tokens to approve. #[pallet::call_index(5)] #[pallet::weight(::WeightInfo::approve(1, 1))] pub fn approve( origin: OriginFor, - id: AssetIdOf, + asset: AssetIdOf, spender: AccountIdOf, value: BalanceOf, ) -> DispatchResultWithPostInfo { let owner = ensure_signed(origin.clone()) .map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?; - let current_allowance = AssetsOf::::allowance(id.clone(), &owner, &spender); + let current_allowance = AssetsOf::::allowance(asset.clone(), &owner, &spender); let weight = match value.cmp(¤t_allowance) { // If the new value is equal to the current allowance, do nothing. @@ -214,7 +212,7 @@ pub mod pallet { Greater => { AssetsOf::::approve_transfer( origin, - id.clone().into(), + asset.clone().into(), T::Lookup::unlookup(spender.clone()), value.saturating_sub(current_allowance), ) @@ -224,37 +222,42 @@ pub mod pallet { // If the new value is less than the current allowance, cancel the approval and // set the new value. Less => { - let id_param: AssetIdParameterOf = id.clone().into(); + let asset_param: AssetIdParameterOf = asset.clone().into(); let spender_source = T::Lookup::unlookup(spender.clone()); AssetsOf::::cancel_approval( origin.clone(), - id_param.clone(), + asset_param.clone(), spender_source.clone(), ) .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; if value.is_zero() { Self::weight_approve(0, 1) } else { - AssetsOf::::approve_transfer(origin, id_param, spender_source, value)?; + AssetsOf::::approve_transfer( + origin, + asset_param, + spender_source, + value, + )?; Self::weight_approve(1, 1) } }, }; - Self::deposit_event(Event::Approval { id, owner, spender, value }); + Self::deposit_event(Event::Approval { asset, owner, spender, value }); Ok(Some(weight).into()) } - /// Increases the allowance of a spender. + /// Increases the allowance of `spender` by `value` amount of tokens. /// /// # Parameters - /// - `id` - The ID of the asset. + /// - `asset` - The asset to have an allowance increased. /// - `spender` - The account that is allowed to spend the tokens. /// - `value` - The number of tokens to increase the allowance by. #[pallet::call_index(6)] #[pallet::weight(::WeightInfo::approve(1, 0))] pub fn increase_allowance( origin: OriginFor, - id: AssetIdOf, + asset: AssetIdOf, spender: AccountIdOf, value: BalanceOf, ) -> DispatchResultWithPostInfo { @@ -262,27 +265,27 @@ pub mod pallet { .map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?; AssetsOf::::approve_transfer( origin, - id.clone().into(), + asset.clone().into(), T::Lookup::unlookup(spender.clone()), value, ) .map_err(|e| e.with_weight(AssetsWeightInfoOf::::approve_transfer()))?; - let value = AssetsOf::::allowance(id.clone(), &owner, &spender); - Self::deposit_event(Event::Approval { id, owner, spender, value }); + let value = AssetsOf::::allowance(asset.clone(), &owner, &spender); + Self::deposit_event(Event::Approval { asset, owner, spender, value }); Ok(().into()) } - /// Decreases the allowance of a spender. + /// Decreases the allowance of a `spender` by `value` amount of tokens. /// /// # Parameters - /// - `id` - The ID of the asset. + /// - `asset` - The asset to have an allowance decreased. /// - `spender` - The account that is allowed to spend the tokens. /// - `value` - The number of tokens to decrease the allowance by. #[pallet::call_index(7)] #[pallet::weight(::WeightInfo::approve(1, 1))] pub fn decrease_allowance( origin: OriginFor, - id: AssetIdOf, + asset: AssetIdOf, spender: AccountIdOf, value: BalanceOf, ) -> DispatchResultWithPostInfo { @@ -291,14 +294,14 @@ pub mod pallet { if value.is_zero() { return Ok(Some(Self::weight_approve(0, 0)).into()); } - let current_allowance = AssetsOf::::allowance(id.clone(), &owner, &spender); + let current_allowance = AssetsOf::::allowance(asset.clone(), &owner, &spender); let spender_source = T::Lookup::unlookup(spender.clone()); - let id_param: AssetIdParameterOf = id.clone().into(); + let asset_param: AssetIdParameterOf = asset.clone().into(); // Cancel the approval and set the new value if `new_allowance` is more than zero. AssetsOf::::cancel_approval( origin.clone(), - id_param.clone(), + asset_param.clone(), spender_source.clone(), ) .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; @@ -306,17 +309,22 @@ pub mod pallet { let weight = if new_allowance.is_zero() { Self::weight_approve(0, 1) } else { - AssetsOf::::approve_transfer(origin, id_param, spender_source, new_allowance)?; + AssetsOf::::approve_transfer( + origin, + asset_param, + spender_source, + new_allowance, + )?; Self::weight_approve(1, 1) }; - Self::deposit_event(Event::Approval { id, owner, spender, value: new_allowance }); + Self::deposit_event(Event::Approval { asset, owner, spender, value: new_allowance }); Ok(Some(weight).into()) } - /// Create a new token with a given asset ID. + /// Create a new token with a given identifier. /// /// # Parameters - /// - `id` - The ID of the asset. + /// - `id` - The identifier of the asset. /// - `admin` - The account that will administer the asset. /// - `min_balance` - The minimum balance required for accounts holding this asset. #[pallet::call_index(11)] @@ -338,92 +346,90 @@ pub mod pallet { Ok(()) } - /// Start the process of destroying a token with a given asset ID. + /// Start the process of destroying a token. /// /// # Parameters - /// - `id` - The ID of the asset. + /// - `asset` - The asset to be destroyed. #[pallet::call_index(12)] #[pallet::weight(AssetsWeightInfoOf::::start_destroy())] - pub fn start_destroy(origin: OriginFor, id: AssetIdOf) -> DispatchResult { - AssetsOf::::start_destroy(origin, id.into()) + pub fn start_destroy(origin: OriginFor, asset: AssetIdOf) -> DispatchResult { + AssetsOf::::start_destroy(origin, asset.into()) } - /// Set the metadata for a token with a given asset ID. + /// Set the metadata for a token. /// /// # Parameters - /// - `id`: The identifier of the asset to update. - /// - `name`: The user friendly name of this asset. Limited in length by - /// `pallet_assets::Config::StringLimit`. - /// - `symbol`: The exchange symbol for this asset. Limited in length by - /// `pallet_assets::Config::StringLimit`. + /// - `asset`: The asset to update. + /// - `name`: The user friendly name of this asset. + /// - `symbol`: The exchange symbol for this asset. /// - `decimals`: The number of decimals this asset uses to represent one unit. #[pallet::call_index(16)] #[pallet::weight(AssetsWeightInfoOf::::set_metadata(name.len() as u32, symbol.len() as u32))] pub fn set_metadata( origin: OriginFor, - id: AssetIdOf, + asset: AssetIdOf, name: Vec, symbol: Vec, decimals: u8, ) -> DispatchResult { - AssetsOf::::set_metadata(origin, id.into(), name, symbol, decimals) + AssetsOf::::set_metadata(origin, asset.into(), name, symbol, decimals) } - /// Clear the metadata for a token with a given asset ID. + /// Clear the metadata for a token. /// /// # Parameters - /// - `id` - The ID of the asset. + /// - `asset` - The asset to update. #[pallet::call_index(17)] #[pallet::weight(AssetsWeightInfoOf::::clear_metadata())] - pub fn clear_metadata(origin: OriginFor, id: AssetIdOf) -> DispatchResult { - AssetsOf::::clear_metadata(origin, id.into()) + pub fn clear_metadata(origin: OriginFor, asset: AssetIdOf) -> DispatchResult { + AssetsOf::::clear_metadata(origin, asset.into()) } /// Creates `value` amount of tokens and assigns them to `account`, increasing the total supply. /// /// # Parameters - /// - `id` - The ID of the asset. + /// - `asset` - The asset to mint. /// - `account` - The account to be credited with the created tokens. /// - `value` - The number of tokens to mint. #[pallet::call_index(19)] #[pallet::weight(AssetsWeightInfoOf::::mint())] pub fn mint( origin: OriginFor, - id: AssetIdOf, + asset: AssetIdOf, account: AccountIdOf, value: BalanceOf, ) -> DispatchResult { AssetsOf::::mint( origin, - id.clone().into(), + asset.clone().into(), T::Lookup::unlookup(account.clone()), value, )?; - Self::deposit_event(Event::Transfer { id, from: None, to: Some(account), value }); + Self::deposit_event(Event::Transfer { asset, from: None, to: Some(account), value }); Ok(()) } /// Destroys `value` amount of tokens from `account`, reducing the total supply. /// /// # Parameters - /// - `id` - The ID of the asset. + /// - `asset` - the asset to burn. /// - `account` - The account from which the tokens will be destroyed. /// - `value` - The number of tokens to destroy. #[pallet::call_index(20)] #[pallet::weight(AssetsWeightInfoOf::::burn())] pub fn burn( origin: OriginFor, - id: AssetIdOf, + asset: AssetIdOf, account: AccountIdOf, value: BalanceOf, ) -> DispatchResult { AssetsOf::::burn( origin, - id.clone().into(), + asset.clone().into(), T::Lookup::unlookup(account.clone()), value, )?; - Self::deposit_event(Event::Transfer { id, from: Some(account), to: None, value }); + Self::deposit_event(Event::Transfer { asset, from: Some(account), to: None, value }); Ok(()) } } @@ -441,25 +447,25 @@ pub mod pallet { use Read::*; match value { - TotalSupply(id) => AssetsOf::::total_supply(id).encode(), - BalanceOf { id, owner } => AssetsOf::::balance(id, owner).encode(), - Allowance { id, owner, spender } => { - AssetsOf::::allowance(id, &owner, &spender).encode() + TotalSupply(asset) => AssetsOf::::total_supply(asset).encode(), + BalanceOf { asset, owner } => AssetsOf::::balance(asset, owner).encode(), + Allowance { asset, owner, spender } => { + AssetsOf::::allowance(asset, &owner, &spender).encode() }, - TokenName(id) => { - as MetadataInspect>>::name(id).encode() + TokenName(asset) => { + as MetadataInspect>>::name(asset).encode() }, - TokenSymbol(id) => { - as MetadataInspect>>::symbol(id).encode() + TokenSymbol(asset) => { + as MetadataInspect>>::symbol(asset).encode() }, - TokenDecimals(id) => { - as MetadataInspect>>::decimals(id).encode() + TokenDecimals(asset) => { + as MetadataInspect>>::decimals(asset).encode() }, - AssetExists(id) => AssetsOf::::asset_exists(id).encode(), + AssetExists(asset) => AssetsOf::::asset_exists(asset).encode(), } } - pub fn weight_approve(approve: u32, cancel: u32) -> Weight { + fn weight_approve(approve: u32, cancel: u32) -> Weight { ::WeightInfo::approve(cancel, approve) } } diff --git a/pallets/api/src/fungibles/tests.rs b/pallets/api/src/fungibles/tests.rs index ca2a85c9..9610139e 100644 --- a/pallets/api/src/fungibles/tests.rs +++ b/pallets/api/src/fungibles/tests.rs @@ -16,16 +16,16 @@ type Event = crate::fungibles::Event; fn transfer_works() { new_test_ext().execute_with(|| { let value: Balance = 100 * UNIT; - let id = ASSET; + let asset = ASSET; let from = Some(ALICE); let to = Some(BOB); - create_asset_and_mint_to(ALICE, id, ALICE, value * 2); - let balance_before_transfer = Assets::balance(id, &BOB); - assert_ok!(Fungibles::transfer(signed(ALICE), id, BOB, value)); - let balance_after_transfer = Assets::balance(id, &BOB); + create_asset_and_mint_to(ALICE, asset, ALICE, value * 2); + let balance_before_transfer = Assets::balance(asset, &BOB); + assert_ok!(Fungibles::transfer(signed(ALICE), asset, BOB, value)); + let balance_after_transfer = Assets::balance(asset, &BOB); assert_eq!(balance_after_transfer, balance_before_transfer + value); - System::assert_last_event(Event::Transfer { id, from, to, value }.into()); + System::assert_last_event(Event::Transfer { asset, from, to, value }.into()); }); } @@ -33,22 +33,22 @@ fn transfer_works() { fn transfer_from_works() { new_test_ext().execute_with(|| { let value: Balance = 100 * UNIT; - let id = ASSET; + let asset = ASSET; let from = Some(ALICE); let to = Some(BOB); // Approve CHARLIE to transfer up to `value` to BOB. - create_asset_mint_and_approve(ALICE, id, ALICE, value * 2, CHARLIE, value); + create_asset_mint_and_approve(ALICE, asset, ALICE, value * 2, CHARLIE, value); // Successfully call transfer from. - let alice_balance_before_transfer = Assets::balance(id, &ALICE); - let bob_balance_before_transfer = Assets::balance(id, &BOB); - assert_ok!(Fungibles::transfer_from(signed(CHARLIE), id, ALICE, BOB, value)); - let alice_balance_after_transfer = Assets::balance(id, &ALICE); - let bob_balance_after_transfer = Assets::balance(id, &BOB); + let alice_balance_before_transfer = Assets::balance(asset, &ALICE); + let bob_balance_before_transfer = Assets::balance(asset, &BOB); + assert_ok!(Fungibles::transfer_from(signed(CHARLIE), asset, ALICE, BOB, value)); + let alice_balance_after_transfer = Assets::balance(asset, &ALICE); + let bob_balance_after_transfer = Assets::balance(asset, &BOB); // Check that BOB receives the `value` and ALICE `amount` is spent successfully by CHARLIE. assert_eq!(bob_balance_after_transfer, bob_balance_before_transfer + value); assert_eq!(alice_balance_after_transfer, alice_balance_before_transfer - value); - System::assert_last_event(Event::Transfer { id, from, to, value }.into()); + System::assert_last_event(Event::Transfer { asset, from, to, value }.into()); }); } @@ -57,31 +57,37 @@ fn transfer_from_works() { fn approve_works() { new_test_ext().execute_with(|| { let value: Balance = 100 * UNIT; - let id = ASSET; + let asset = ASSET; let owner = ALICE; let spender = BOB; - create_asset_and_mint_to(ALICE, id, ALICE, value); - assert_eq!(0, Assets::allowance(id, &ALICE, &BOB)); - assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value)); - assert_eq!(Assets::allowance(id, &ALICE, &BOB), value); - System::assert_last_event(Event::Approval { id, owner, spender, value }.into()); + create_asset_and_mint_to(ALICE, asset, ALICE, value); + assert_eq!(0, Assets::allowance(asset, &ALICE, &BOB)); + assert_ok!(Fungibles::approve(signed(ALICE), asset, BOB, value)); + assert_eq!(Assets::allowance(asset, &ALICE, &BOB), value); + System::assert_last_event(Event::Approval { asset, owner, spender, value }.into()); // Approves an value to spend that is lower than the current allowance. - assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value / 2)); - assert_eq!(Assets::allowance(id, &ALICE, &BOB), value / 2); - System::assert_last_event(Event::Approval { id, owner, spender, value: value / 2 }.into()); + assert_ok!(Fungibles::approve(signed(ALICE), asset, BOB, value / 2)); + assert_eq!(Assets::allowance(asset, &ALICE, &BOB), value / 2); + System::assert_last_event( + Event::Approval { asset, owner, spender, value: value / 2 }.into(), + ); // Approves an value to spend that is higher than the current allowance. - assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value * 2)); - assert_eq!(Assets::allowance(id, &ALICE, &BOB), value * 2); - System::assert_last_event(Event::Approval { id, owner, spender, value: value * 2 }.into()); + assert_ok!(Fungibles::approve(signed(ALICE), asset, BOB, value * 2)); + assert_eq!(Assets::allowance(asset, &ALICE, &BOB), value * 2); + System::assert_last_event( + Event::Approval { asset, owner, spender, value: value * 2 }.into(), + ); // Approves an value to spend that is equal to the current allowance. - assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value * 2)); - assert_eq!(Assets::allowance(id, &ALICE, &BOB), value * 2); - System::assert_last_event(Event::Approval { id, owner, spender, value: value * 2 }.into()); + assert_ok!(Fungibles::approve(signed(ALICE), asset, BOB, value * 2)); + assert_eq!(Assets::allowance(asset, &ALICE, &BOB), value * 2); + System::assert_last_event( + Event::Approval { asset, owner, spender, value: value * 2 }.into(), + ); // Sets allowance to zero. - assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, 0)); - assert_eq!(Assets::allowance(id, &ALICE, &BOB), 0); - System::assert_last_event(Event::Approval { id, owner, spender, value: 0 }.into()); + assert_ok!(Fungibles::approve(signed(ALICE), asset, BOB, 0)); + assert_eq!(Assets::allowance(asset, &ALICE, &BOB), 0); + System::assert_last_event(Event::Approval { asset, owner, spender, value: 0 }.into()); }); } @@ -89,19 +95,21 @@ fn approve_works() { fn increase_allowance_works() { new_test_ext().execute_with(|| { let value: Balance = 100 * UNIT; - let id = ASSET; + let asset = ASSET; let owner = ALICE; let spender = BOB; - create_asset_and_mint_to(ALICE, id, ALICE, value); - assert_eq!(0, Assets::allowance(id, &ALICE, &BOB)); - assert_ok!(Fungibles::increase_allowance(signed(ALICE), id, BOB, value)); - assert_eq!(Assets::allowance(id, &ALICE, &BOB), value); - System::assert_last_event(Event::Approval { id, owner, spender, value }.into()); + create_asset_and_mint_to(ALICE, asset, ALICE, value); + assert_eq!(0, Assets::allowance(asset, &ALICE, &BOB)); + assert_ok!(Fungibles::increase_allowance(signed(ALICE), asset, BOB, value)); + assert_eq!(Assets::allowance(asset, &ALICE, &BOB), value); + System::assert_last_event(Event::Approval { asset, owner, spender, value }.into()); // Additive. - assert_ok!(Fungibles::increase_allowance(signed(ALICE), id, BOB, value)); - assert_eq!(Assets::allowance(id, &ALICE, &BOB), value * 2); - System::assert_last_event(Event::Approval { id, owner, spender, value: value * 2 }.into()); + assert_ok!(Fungibles::increase_allowance(signed(ALICE), asset, BOB, value)); + assert_eq!(Assets::allowance(asset, &ALICE, &BOB), value * 2); + System::assert_last_event( + Event::Approval { asset, owner, spender, value: value * 2 }.into(), + ); }); } @@ -109,23 +117,25 @@ fn increase_allowance_works() { fn decrease_allowance_works() { new_test_ext().execute_with(|| { let value: Balance = 100 * UNIT; - let id = ASSET; + let asset = ASSET; let owner = ALICE; let spender = BOB; - create_asset_mint_and_approve(ALICE, id, ALICE, value, BOB, value); - assert_eq!(Assets::allowance(id, &ALICE, &BOB), value); + create_asset_mint_and_approve(ALICE, asset, ALICE, value, BOB, value); + assert_eq!(Assets::allowance(asset, &ALICE, &BOB), value); // Owner balance is not changed if decreased by zero. - assert_ok!(Fungibles::decrease_allowance(signed(ALICE), id, BOB, 0)); - assert_eq!(Assets::allowance(id, &ALICE, &BOB), value); + assert_ok!(Fungibles::decrease_allowance(signed(ALICE), asset, BOB, 0)); + assert_eq!(Assets::allowance(asset, &ALICE, &BOB), value); // Decrease allowance successfully. - assert_ok!(Fungibles::decrease_allowance(signed(ALICE), id, BOB, value / 2)); - assert_eq!(Assets::allowance(id, &ALICE, &BOB), value / 2); - System::assert_last_event(Event::Approval { id, owner, spender, value: value / 2 }.into()); + assert_ok!(Fungibles::decrease_allowance(signed(ALICE), asset, BOB, value / 2)); + assert_eq!(Assets::allowance(asset, &ALICE, &BOB), value / 2); + System::assert_last_event( + Event::Approval { asset, owner, spender, value: value / 2 }.into(), + ); // Saturating if current allowance is decreased more than the owner balance. - assert_ok!(Fungibles::decrease_allowance(signed(ALICE), id, BOB, value)); - assert_eq!(Assets::allowance(id, &ALICE, &BOB), 0); - System::assert_last_event(Event::Approval { id, owner, spender, value: 0 }.into()); + assert_ok!(Fungibles::decrease_allowance(signed(ALICE), asset, BOB, value)); + assert_eq!(Assets::allowance(asset, &ALICE, &BOB), 0); + System::assert_last_event(Event::Approval { asset, owner, spender, value: 0 }.into()); }); } @@ -146,45 +156,45 @@ fn create_works() { #[test] fn start_destroy_works() { new_test_ext().execute_with(|| { - let id = ASSET; + let asset = ASSET; - create_asset(ALICE, id); - assert_ok!(Fungibles::start_destroy(signed(ALICE), id)); + create_asset(ALICE, asset); + assert_ok!(Fungibles::start_destroy(signed(ALICE), asset)); }); } #[test] fn set_metadata_works() { new_test_ext().execute_with(|| { - let id = ASSET; + let asset = ASSET; let name = vec![42]; let symbol = vec![42]; let decimals = 42; - create_asset(ALICE, id); + create_asset(ALICE, asset); assert_ok!(Fungibles::set_metadata( signed(ALICE), - id, + asset, name.clone(), symbol.clone(), decimals )); - assert_eq!(Assets::name(id), name); - assert_eq!(Assets::symbol(id), symbol); - assert_eq!(Assets::decimals(id), decimals); + assert_eq!(Assets::name(asset), name); + assert_eq!(Assets::symbol(asset), symbol); + assert_eq!(Assets::decimals(asset), decimals); }); } #[test] fn clear_metadata_works() { new_test_ext().execute_with(|| { - let id = ASSET; + let asset = ASSET; - create_asset_and_set_metadata(ALICE, id, vec![42], vec![42], 42); - assert_ok!(Fungibles::clear_metadata(signed(ALICE), id)); - assert!(Assets::name(id).is_empty()); - assert!(Assets::symbol(id).is_empty()); - assert!(Assets::decimals(id).is_zero()); + create_asset_and_set_metadata(ALICE, asset, vec![42], vec![42], 42); + assert_ok!(Fungibles::clear_metadata(signed(ALICE), asset)); + assert!(Assets::name(asset).is_empty()); + assert!(Assets::symbol(asset).is_empty()); + assert!(Assets::decimals(asset).is_zero()); }); } @@ -192,16 +202,16 @@ fn clear_metadata_works() { fn mint_works() { new_test_ext().execute_with(|| { let value: Balance = 100 * UNIT; - let id = ASSET; + let asset = ASSET; let from = None; let to = Some(BOB); - create_asset(ALICE, id); - let balance_before_mint = Assets::balance(id, &BOB); - assert_ok!(Fungibles::mint(signed(ALICE), id, BOB, value)); - let balance_after_mint = Assets::balance(id, &BOB); + create_asset(ALICE, asset); + let balance_before_mint = Assets::balance(asset, &BOB); + assert_ok!(Fungibles::mint(signed(ALICE), asset, BOB, value)); + let balance_after_mint = Assets::balance(asset, &BOB); assert_eq!(balance_after_mint, balance_before_mint + value); - System::assert_last_event(Event::Transfer { id, from, to, value }.into()); + System::assert_last_event(Event::Transfer { asset, from, to, value }.into()); }); } @@ -209,16 +219,16 @@ fn mint_works() { fn burn_works() { new_test_ext().execute_with(|| { let value: Balance = 100 * UNIT; - let id = ASSET; + let asset = ASSET; let from = Some(BOB); let to = None; - create_asset_and_mint_to(ALICE, id, BOB, value); - let balance_before_burn = Assets::balance(id, &BOB); - assert_ok!(Fungibles::burn(signed(ALICE), id, BOB, value)); - let balance_after_burn = Assets::balance(id, &BOB); + create_asset_and_mint_to(ALICE, asset, BOB, value); + let balance_before_burn = Assets::balance(asset, &BOB); + assert_ok!(Fungibles::burn(signed(ALICE), asset, BOB, value)); + let balance_after_burn = Assets::balance(asset, &BOB); assert_eq!(balance_after_burn, balance_before_burn - value); - System::assert_last_event(Event::Transfer { id, from, to, value }.into()); + System::assert_last_event(Event::Transfer { asset, from, to, value }.into()); }); } @@ -236,7 +246,7 @@ fn balance_of_works() { create_asset_and_mint_to(ALICE, ASSET, ALICE, 100); assert_eq!( Assets::balance(ASSET, ALICE).encode(), - Fungibles::read_state(BalanceOf { id: ASSET, owner: ALICE }) + Fungibles::read_state(BalanceOf { asset: ASSET, owner: ALICE }) ); }); } @@ -247,7 +257,7 @@ fn allowance_works() { create_asset_mint_and_approve(ALICE, ASSET, BOB, 100, ALICE, 50); assert_eq!( Assets::allowance(ASSET, &ALICE, &BOB).encode(), - Fungibles::read_state(Allowance { id: ASSET, owner: ALICE, spender: BOB }) + Fungibles::read_state(Allowance { asset: ASSET, owner: ALICE, spender: BOB }) ); }); } @@ -277,48 +287,48 @@ fn signed(account: AccountId) -> RuntimeOrigin { RuntimeOrigin::signed(account) } -fn create_asset(owner: AccountId, asset_id: AssetId) { - assert_ok!(Assets::create(signed(owner), asset_id, owner, 1)); +fn create_asset(owner: AccountId, asset_asset: AssetId) { + assert_ok!(Assets::create(signed(owner), asset_asset, owner, 1)); } -fn mint_asset(owner: AccountId, asset_id: AssetId, to: AccountId, value: Balance) { - assert_ok!(Assets::mint(signed(owner), asset_id, to, value)); +fn mint_asset(owner: AccountId, asset_asset: AssetId, to: AccountId, value: Balance) { + assert_ok!(Assets::mint(signed(owner), asset_asset, to, value)); } -fn create_asset_and_mint_to(owner: AccountId, asset_id: AssetId, to: AccountId, value: Balance) { - create_asset(owner, asset_id); - mint_asset(owner, asset_id, to, value) +fn create_asset_and_mint_to(owner: AccountId, asset_asset: AssetId, to: AccountId, value: Balance) { + create_asset(owner, asset_asset); + mint_asset(owner, asset_asset, to, value) } fn create_asset_mint_and_approve( owner: AccountId, - asset_id: AssetId, + asset_asset: AssetId, to: AccountId, mint: Balance, spender: AccountId, approve: Balance, ) { - create_asset_and_mint_to(owner, asset_id, to, mint); - assert_ok!(Assets::approve_transfer(signed(to), asset_id, spender, approve,)); + create_asset_and_mint_to(owner, asset_asset, to, mint); + assert_ok!(Assets::approve_transfer(signed(to), asset_asset, spender, approve,)); } fn create_asset_and_set_metadata( owner: AccountId, - asset_id: AssetId, + asset_asset: AssetId, name: Vec, symbol: Vec, decimals: u8, ) { - assert_ok!(Assets::create(signed(owner), asset_id, owner, 100)); - set_metadata_asset(owner, asset_id, name, symbol, decimals); + assert_ok!(Assets::create(signed(owner), asset_asset, owner, 100)); + set_metadata_asset(owner, asset_asset, name, symbol, decimals); } fn set_metadata_asset( owner: AccountId, - asset_id: AssetId, + asset_asset: AssetId, name: Vec, symbol: Vec, decimals: u8, ) { - assert_ok!(Assets::set_metadata(signed(owner), asset_id, name, symbol, decimals)); + assert_ok!(Assets::set_metadata(signed(owner), asset_asset, name, symbol, decimals)); } From 08115984fb33da05dbd735532f28bd1f464d99ef Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Mon, 19 Aug 2024 09:29:44 +0200 Subject: [PATCH 45/76] style: fix tests asset_asset parameter --- pallets/api/src/fungibles/tests.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pallets/api/src/fungibles/tests.rs b/pallets/api/src/fungibles/tests.rs index 9610139e..d881a3c1 100644 --- a/pallets/api/src/fungibles/tests.rs +++ b/pallets/api/src/fungibles/tests.rs @@ -287,48 +287,48 @@ fn signed(account: AccountId) -> RuntimeOrigin { RuntimeOrigin::signed(account) } -fn create_asset(owner: AccountId, asset_asset: AssetId) { - assert_ok!(Assets::create(signed(owner), asset_asset, owner, 1)); +fn create_asset(owner: AccountId, asset: AssetId) { + assert_ok!(Assets::create(signed(owner), asset, owner, 1)); } -fn mint_asset(owner: AccountId, asset_asset: AssetId, to: AccountId, value: Balance) { - assert_ok!(Assets::mint(signed(owner), asset_asset, to, value)); +fn mint_asset(owner: AccountId, asset: AssetId, to: AccountId, value: Balance) { + assert_ok!(Assets::mint(signed(owner), asset, to, value)); } -fn create_asset_and_mint_to(owner: AccountId, asset_asset: AssetId, to: AccountId, value: Balance) { - create_asset(owner, asset_asset); - mint_asset(owner, asset_asset, to, value) +fn create_asset_and_mint_to(owner: AccountId, asset: AssetId, to: AccountId, value: Balance) { + create_asset(owner, asset); + mint_asset(owner, asset, to, value) } fn create_asset_mint_and_approve( owner: AccountId, - asset_asset: AssetId, + asset: AssetId, to: AccountId, mint: Balance, spender: AccountId, approve: Balance, ) { - create_asset_and_mint_to(owner, asset_asset, to, mint); - assert_ok!(Assets::approve_transfer(signed(to), asset_asset, spender, approve,)); + create_asset_and_mint_to(owner, asset, to, mint); + assert_ok!(Assets::approve_transfer(signed(to), asset, spender, approve,)); } fn create_asset_and_set_metadata( owner: AccountId, - asset_asset: AssetId, + asset: AssetId, name: Vec, symbol: Vec, decimals: u8, ) { - assert_ok!(Assets::create(signed(owner), asset_asset, owner, 100)); - set_metadata_asset(owner, asset_asset, name, symbol, decimals); + assert_ok!(Assets::create(signed(owner), asset, owner, 100)); + set_metadata_asset(owner, asset, name, symbol, decimals); } fn set_metadata_asset( owner: AccountId, - asset_asset: AssetId, + asset: AssetId, name: Vec, symbol: Vec, decimals: u8, ) { - assert_ok!(Assets::set_metadata(signed(owner), asset_asset, name, symbol, decimals)); + assert_ok!(Assets::set_metadata(signed(owner), asset, name, symbol, decimals)); } From 6482a437ecd3028a63b307591369b990e3487612 Mon Sep 17 00:00:00 2001 From: Tin Chung <56880684+chungquantin@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:06:50 +0700 Subject: [PATCH 46/76] fix: invalid imported crates & crate visibility (#214) --- pop-api/src/v0/assets/fungibles.rs | 2 +- runtime/devnet/src/config/mod.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pop-api/src/v0/assets/fungibles.rs b/pop-api/src/v0/assets/fungibles.rs index c881f823..4f270e6b 100644 --- a/pop-api/src/v0/assets/fungibles.rs +++ b/pop-api/src/v0/assets/fungibles.rs @@ -11,9 +11,9 @@ use crate::{ primitives::{AccountId, AssetId, Balance}, Result, StatusCode, }; -pub use asset_management::*; use constants::*; use ink::{env::chain_extension::ChainExtensionMethod, prelude::vec::Vec}; +pub use management::*; pub use metadata::*; // Helper method to build a dispatch call. diff --git a/runtime/devnet/src/config/mod.rs b/runtime/devnet/src/config/mod.rs index 1dcd44da..1ef83bc1 100644 --- a/runtime/devnet/src/config/mod.rs +++ b/runtime/devnet/src/config/mod.rs @@ -1,5 +1,6 @@ mod api; -mod assets; +// Public due to pop api integration tests crate. +pub mod assets; mod contracts; mod proxy; // Public due to integration tests crate. From 09f4106aa34ae590eecce4ecaba04428fd67586a Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Sun, 18 Aug 2024 22:48:04 +0100 Subject: [PATCH 47/76] refactor: generic extension --- Cargo.lock | 1 + Cargo.toml | 1 + extension/Cargo.toml | 27 +-- extension/src/lib.rs | 1 + extension/src/reboot.rs | 243 +++++++++++++++++++++++++ runtime/devnet/src/config/api.rs | 121 ++++++++++++ runtime/devnet/src/config/contracts.rs | 4 +- 7 files changed, 383 insertions(+), 15 deletions(-) create mode 100644 extension/src/reboot.rs diff --git a/Cargo.lock b/Cargo.lock index 3407a210..50196ef3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9466,6 +9466,7 @@ version = "0.1.0" dependencies = [ "frame-support", "frame-system", + "impl-trait-for-tuples", "log", "pallet-contracts", "parity-scale-codec", diff --git a/Cargo.toml b/Cargo.toml index f037f6b7..dbb90150 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features = "derive", ] } hex-literal = "0.4.1" +impl-trait-for-tuples = "0.2.2" log = { version = "0.4.20", default-features = false } scale-info = { version = "2.10.0", default-features = false, features = [ "derive", diff --git a/extension/Cargo.toml b/extension/Cargo.toml index 03b4221a..b625dcf2 100644 --- a/extension/Cargo.toml +++ b/extension/Cargo.toml @@ -14,6 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec.workspace = true +impl-trait-for-tuples.workspace = true log.workspace = true # Local @@ -33,19 +34,19 @@ rand = "0.8.5" [features] default = ["std"] std = [ - "log/std", - "codec/std", - "frame-support/std", - "frame-system/std", - "pallet-contracts/std", - "pop-primitives/std", - "sp-runtime/std", - "sp-core/std", - "sp-std/std", + "log/std", + "codec/std", + "frame-support/std", + "frame-system/std", + "pallet-contracts/std", + "pop-primitives/std", + "sp-runtime/std", + "sp-core/std", + "sp-std/std", ] runtime-benchmarks = [ - "frame-support/runtime-benchmarks", - "frame-system/runtime-benchmarks", - "pallet-contracts/runtime-benchmarks", - "sp-runtime/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "pallet-contracts/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", ] diff --git a/extension/src/lib.rs b/extension/src/lib.rs index 650f73f8..44fb13be 100644 --- a/extension/src/lib.rs +++ b/extension/src/lib.rs @@ -14,6 +14,7 @@ use sp_core::crypto::UncheckedFrom; use sp_runtime::{traits::Dispatchable, DispatchError}; use sp_std::vec::Vec; +pub mod reboot; #[cfg(test)] mod tests; mod v0; diff --git a/extension/src/reboot.rs b/extension/src/reboot.rs new file mode 100644 index 00000000..420e5598 --- /dev/null +++ b/extension/src/reboot.rs @@ -0,0 +1,243 @@ +use crate::{DECODING_FAILED_ERROR, UNKNOWN_CALL_ERROR}; +use codec::Decode; +use frame_support::{ + dispatch::{GetDispatchInfo, PostDispatchInfo, RawOrigin}, + ensure, + traits::{Contains, OriginTrait}, + weights::Weight, +}; +pub use functions::{ + builders::{Builder, DefaultBuilder, PrefixBuilder}, + matching::{FirstByte, FunctionIdMatcher, Matches}, + DispatchCall, Function, ReadState, +}; +use pallet_contracts::{ + chain_extension::{ + BufInBufOutState, ChainExtension, Environment, Ext, InitState, Result, RetVal, + RetVal::Converging, + }, + Config, +}; +use sp_core::Get; +use sp_runtime::traits::Dispatchable; +use std::marker::PhantomData; + +#[derive(Default)] +pub struct ApiExtension(PhantomData); +impl ChainExtension for ApiExtension +where + C: Config + + frame_system::Config< + RuntimeCall: GetDispatchInfo + Dispatchable, + >, + F: Functions> + 'static, +{ + fn call>(&mut self, env: Environment) -> Result { + let mut env = env.buf_in_buf_out(); + + let contract_host_weight = ::Schedule::get().host_fn_weights; + env.charge_weight(contract_host_weight.debug_message)?; + + F::Function::execute::(env) + } +} + +// Simple trait to allow configuration of chain extension functions (workaround for Default requirement) +pub trait Functions { + type Function: Function; +} + +// Trait to be implemented for type handling a read of runtime state +pub trait RuntimeRead { + fn weight(&self) -> Weight; + fn read(self) -> Vec; +} + +mod functions { + use super::*; + use builders::Builder; + use matching::Matches; + + // A chain extension function. + pub trait Function { + type C: Config; + fn execute>(env: Environment) -> Result; + } + + #[impl_trait_for_tuples::impl_for_tuples(1, 3)] + #[tuple_types_custom_trait_bound(Function + Matches)] + impl Function for Tuple { + for_tuples!( where #( Tuple: Function )* ); + type C = C; + fn execute>(env: Environment) -> Result { + for_tuples!( #( + if Tuple::matches(env.func_id()) { + return Tuple::execute(env) + } + )* ); + + Err(UNKNOWN_CALL_ERROR) + } + } + + // Function implementation for dispatching a call. + pub struct DispatchCall(PhantomData<(C, B, D, Matcher, Filter)>); + impl< + C: Config + + frame_system::Config< + RuntimeCall: GetDispatchInfo + Dispatchable, + >, + B: Builder, + Dispatch: Decode + Into<::RuntimeCall>, + Matcher: Matches, + Filter: Contains<::RuntimeCall> + 'static, + > Function for DispatchCall + { + type C = C; + + fn execute>(mut env: Environment) -> Result { + // Build call + let call = B::build(&mut env)?.into(); + // Charge weight before dispatch + let charged_weight = env.charge_weight(call.get_dispatch_info().weight)?; + // Ensure call allowed + let mut origin: C::RuntimeOrigin = + RawOrigin::Signed(env.ext().address().clone()).into(); + origin.add_filter(Filter::contains); + + let (result, weight) = match call.dispatch(origin) { + Ok(info) => (Ok(()), info.actual_weight), + Err(err) => (Err(err.error), err.post_info.actual_weight), + }; + // Adjust post-dispatch weight + if let Some(actual_weight) = weight { + env.adjust_weight(charged_weight, actual_weight); + } + result.map(|_| Converging(0)) + } + } + + impl Matches for DispatchCall { + fn matches(func_id: u16) -> bool { + M::matches(func_id) + } + } + + // Function implementation for reading state. + pub struct ReadState(PhantomData<(C, B, R, RR, M, F)>); + impl< + C: Config, + B: Builder, + R: Decode + Into, + RR: RuntimeRead, + Matcher: Matches, + Filter: Contains, + > Function for ReadState + { + type C = C; + + fn execute>(mut env: Environment) -> Result { + // Build call + let read = B::build(&mut env)?.into(); + + // Charge weight before read + env.charge_weight(read.weight())?; + // Ensure call allowed + // TODO: use filtered error so easier to determine if it is decoding error or a filter blocking the call + ensure!(Filter::contains(&read), UNKNOWN_CALL_ERROR); + // TODO: check remaining parameters (allow_skip, weight per byte) + env.write(&read.read(), false, None)?; + Ok(Converging(0)) + } + } + + impl Matches for ReadState { + fn matches(func_id: u16) -> bool { + M::matches(func_id) + } + } + + pub mod builders { + use super::*; + + // Trait for building some output (call/read) from the environment. + pub trait Builder { + type C: Config; + type Output: Decode; + fn build>( + env: &mut Environment, + ) -> Result; + } + + // Default implementation which charges appropriate weight before building the output. + pub struct DefaultBuilder(PhantomData<(C, O)>); + impl Builder for DefaultBuilder { + type C = C; + type Output = Output; + + fn build>( + env: &mut Environment, + ) -> Result { + let contract_host_weight = ::Schedule::get().host_fn_weights; + let len = env.in_len(); + env.charge_weight(contract_host_weight.return_per_byte.saturating_mul(len.into()))?; + let params = env.read(len)?; + Output::decode(&mut ¶ms[..]).map_err(|_| DECODING_FAILED_ERROR) + } + } + + // Implementation which charges appropriate weight before building the output, prefixed with additional data. + pub struct PrefixBuilder(PhantomData<(C, O)>); + impl Builder for PrefixBuilder { + type C = C; + type Output = Output; + + fn build>( + env: &mut Environment, + ) -> Result { + let contract_host_weight = ::Schedule::get().host_fn_weights; + let len = env.in_len(); + env.charge_weight(contract_host_weight.return_per_byte.saturating_mul(len.into()))?; + let mut params = env.read(len)?; + + // TODO: refactor out into own type, merging default and prefix into a single implementation with optional prefix handling + let version = env.func_id().to_le_bytes()[1]; + let (pallet_index, call_index) = { + let bytes = env.ext_id().to_le_bytes(); + (bytes[0], bytes[1]) + }; + params.insert(0, version); + params.insert(1, pallet_index); + params.insert(2, call_index); + + Output::decode(&mut ¶ms[..]).map_err(|_| DECODING_FAILED_ERROR) + } + } + } + + pub mod matching { + use super::*; + + // Simple trait for determining of a function matches some identifier + pub trait Matches { + fn matches(func_id: u16) -> bool; + } + + // Default implementation + pub struct FunctionIdMatcher(PhantomData); + impl> Matches for FunctionIdMatcher { + fn matches(function_id: u16) -> bool { + function_id == T::get() + } + } + + // Implementation which matches on the first byte only + pub struct FirstByte(PhantomData); + impl> Matches for FirstByte { + fn matches(function_id: u16) -> bool { + let bytes = function_id.to_le_bytes(); + bytes[0] == T::get() + } + } + } +} diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index 4a404c99..0fd3d5d4 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -66,3 +66,124 @@ impl fungibles::Config for Runtime { type AssetsInstance = TrustBackedAssetsInstance; type WeightInfo = fungibles::weights::SubstrateWeight; } + +pub(crate) mod reboot { + use crate::{config::api::RuntimeRead, fungibles, Runtime, RuntimeCall}; + use codec::Decode; + use cumulus_primitives_core::Weight; + use frame_support::traits::Contains; + use pop_chain_extension::reboot::{RuntimeRead as Read, *}; + use sp_core::ConstU8; + + #[derive(Decode, Debug)] + pub enum VersionedRuntimeCall { + #[codec(index = 0)] + V0(RuntimeCall), + } + + impl Into for VersionedRuntimeCall { + fn into(self) -> RuntimeCall { + // Allows mapping from some previous runtime call shape to a current valid runtime call + match self { + VersionedRuntimeCall::V0(call) => call, + } + } + } + + #[derive(Decode, Debug)] + pub enum VersionedRuntimeRead { + #[codec(index = 0)] + V0(RuntimeRead), + } + + impl Into for VersionedRuntimeRead { + fn into(self) -> RuntimeRead { + // Allows mapping from some previous runtime call shape to a current valid runtime read + match self { + VersionedRuntimeRead::V0(read) => read, + } + } + } + + impl Read for RuntimeRead { + fn weight(&self) -> Weight { + // TODO: defer to relevant pallet - e.g. RuntimeRead::Fungibles(key) => fungibles::Pallet::read_weight(key), + ::DbWeight::get().reads(1_u64) + } + + fn read(self) -> Vec { + match self { + RuntimeRead::Fungibles(key) => fungibles::Pallet::read_state(key), + } + } + } + + pub struct Filter; + + impl Contains for Filter { + fn contains(c: &RuntimeCall) -> bool { + use fungibles::Call::*; + matches!( + c, + RuntimeCall::Fungibles( + transfer { .. } + | transfer_from { .. } | approve { .. } + | increase_allowance { .. } + | decrease_allowance { .. } + | create { .. } | set_metadata { .. } + | start_destroy { .. } | clear_metadata { .. } + | mint { .. } | burn { .. } + ) + ) + } + } + + impl Contains for Filter { + fn contains(r: &RuntimeRead) -> bool { + use crate::fungibles::Read::*; + matches!( + r, + RuntimeRead::Fungibles( + TotalSupply(..) + | BalanceOf { .. } | Allowance { .. } + | TokenName(..) | TokenSymbol(..) + | TokenDecimals(..) | AssetExists(..) + ) + ) + } + } + + #[derive(Default)] + pub struct Functions; + impl pop_chain_extension::reboot::Functions for Functions { + // Configure the functions available + type Function = ( + // Dispatching calls + DispatchCall< + Runtime, + // Use bytes from func_id() + ext_id() to prefix the encoded input bytes to determine the versioned dispatch + PrefixBuilder, + // Type for versioning runtime calls + VersionedRuntimeCall, + // Use first byte of func_id to match to this function, with value of zero. + FirstByte>, + // Filtering of allowed calls + Filter, + >, + // Reading state + ReadState< + Runtime, + // Use bytes from func_id() + ext_id() to prefix the encoded input bytes to determine the versioned read + PrefixBuilder, + // Type for versioning runtime reads + VersionedRuntimeRead, + // The current runtime reads available + RuntimeRead, + // Use first byte of func_id to match to this function, with value of one. + FirstByte>, + // Filtering of allowed reads + Filter, + >, + ); + } +} diff --git a/runtime/devnet/src/config/contracts.rs b/runtime/devnet/src/config/contracts.rs index 5dc613b5..16f1f7de 100644 --- a/runtime/devnet/src/config/contracts.rs +++ b/runtime/devnet/src/config/contracts.rs @@ -1,4 +1,4 @@ -use super::api::Extension; +use super::api::reboot::Functions; use crate::{ deposit, Balance, Balances, BalancesCall, Perbill, Runtime, RuntimeCall, RuntimeEvent, RuntimeHoldReason, Timestamp, @@ -64,7 +64,7 @@ impl pallet_contracts::Config for Runtime { type CallStack = [pallet_contracts::Frame; 23]; type WeightPrice = pallet_transaction_payment::Pallet; type WeightInfo = pallet_contracts::weights::SubstrateWeight; - type ChainExtension = pop_chain_extension::ApiExtension; + type ChainExtension = pop_chain_extension::reboot::ApiExtension; type Schedule = Schedule; type AddressGenerator = pallet_contracts::DefaultAddressGenerator; // This node is geared towards development and testing of contracts. From 2a83e8ae46ac6f5ef4603383a97b27715fbf48e1 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Mon, 19 Aug 2024 21:28:56 +0100 Subject: [PATCH 48/76] refactor: builder to decoder --- extension/src/reboot.rs | 195 ++++++++++++++++++------------- runtime/devnet/src/config/api.rs | 19 ++- 2 files changed, 119 insertions(+), 95 deletions(-) diff --git a/extension/src/reboot.rs b/extension/src/reboot.rs index 420e5598..6b5c2dce 100644 --- a/extension/src/reboot.rs +++ b/extension/src/reboot.rs @@ -1,5 +1,5 @@ use crate::{DECODING_FAILED_ERROR, UNKNOWN_CALL_ERROR}; -use codec::Decode; +use codec::Decode as _; use frame_support::{ dispatch::{GetDispatchInfo, PostDispatchInfo, RawOrigin}, ensure, @@ -7,8 +7,8 @@ use frame_support::{ weights::Weight, }; pub use functions::{ - builders::{Builder, DefaultBuilder, PrefixBuilder}, - matching::{FirstByte, FunctionIdMatcher, Matches}, + decoding::{Decode, Decodes, Processor}, + matching::{FirstByteOfFunctionId, FunctionIdMatcher, Matches}, DispatchCall, Function, ReadState, }; use pallet_contracts::{ @@ -30,7 +30,7 @@ where + frame_system::Config< RuntimeCall: GetDispatchInfo + Dispatchable, >, - F: Functions> + 'static, + F: Functions> + 'static, { fn call>(&mut self, env: Environment) -> Result { let mut env = env.buf_in_buf_out(); @@ -55,21 +55,25 @@ pub trait RuntimeRead { mod functions { use super::*; - use builders::Builder; + use decoding::Decode; use matching::Matches; - // A chain extension function. + /// A chain extension function. pub trait Function { - type C: Config; - fn execute>(env: Environment) -> Result; + type Runtime: Config; + fn execute>( + env: Environment, + ) -> Result; } #[impl_trait_for_tuples::impl_for_tuples(1, 3)] #[tuple_types_custom_trait_bound(Function + Matches)] - impl Function for Tuple { - for_tuples!( where #( Tuple: Function )* ); - type C = C; - fn execute>(env: Environment) -> Result { + impl Function for Tuple { + for_tuples!( where #( Tuple: Function )* ); + type Runtime = Runtime; + fn execute>( + env: Environment, + ) -> Result { for_tuples!( #( if Tuple::matches(env.func_id()) { return Tuple::execute(env) @@ -81,27 +85,28 @@ mod functions { } // Function implementation for dispatching a call. - pub struct DispatchCall(PhantomData<(C, B, D, Matcher, Filter)>); + pub struct DispatchCall(PhantomData<(R, D, M, F)>); impl< - C: Config + Runtime: Config + frame_system::Config< RuntimeCall: GetDispatchInfo + Dispatchable, >, - B: Builder, - Dispatch: Decode + Into<::RuntimeCall>, + Decoder: Decode::RuntimeCall>>, Matcher: Matches, - Filter: Contains<::RuntimeCall> + 'static, - > Function for DispatchCall + Filter: Contains<::RuntimeCall> + 'static, + > Function for DispatchCall { - type C = C; + type Runtime = Runtime; - fn execute>(mut env: Environment) -> Result { + fn execute>( + mut env: Environment, + ) -> Result { // Build call - let call = B::build(&mut env)?.into(); + let call = Decoder::decode(&mut env)?.into(); // Charge weight before dispatch let charged_weight = env.charge_weight(call.get_dispatch_info().weight)?; // Ensure call allowed - let mut origin: C::RuntimeOrigin = + let mut origin: Runtime::RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); origin.add_filter(Filter::contains); @@ -117,29 +122,29 @@ mod functions { } } - impl Matches for DispatchCall { + impl Matches for DispatchCall { fn matches(func_id: u16) -> bool { M::matches(func_id) } } // Function implementation for reading state. - pub struct ReadState(PhantomData<(C, B, R, RR, M, F)>); + pub struct ReadState(PhantomData<(R, D, RR, M, F)>); impl< - C: Config, - B: Builder, - R: Decode + Into, - RR: RuntimeRead, + Runtime: Config, + Decoder: Decode>, + Read: RuntimeRead, Matcher: Matches, - Filter: Contains, - > Function for ReadState + Filter: Contains, + > Function for ReadState { - type C = C; - - fn execute>(mut env: Environment) -> Result { - // Build call - let read = B::build(&mut env)?.into(); + type Runtime = Runtime; + fn execute>( + mut env: Environment, + ) -> Result { + // Build runtime read + let read = Decoder::decode(&mut env)?.into(); // Charge weight before read env.charge_weight(read.weight())?; // Ensure call allowed @@ -151,74 +156,72 @@ mod functions { } } - impl Matches for ReadState { + impl Matches for ReadState { fn matches(func_id: u16) -> bool { M::matches(func_id) } } - pub mod builders { + /// Provides functionality for processing/decoding data read from contract memory. + pub mod decoding { use super::*; + use pallet_contracts::chain_extension::{BufIn, State}; + use sp_runtime::DispatchError; - // Trait for building some output (call/read) from the environment. - pub trait Builder { - type C: Config; - type Output: Decode; - fn build>( - env: &mut Environment, - ) -> Result; - } + /// Trait for decoding data read from contract memory. + pub trait Decode { + /// The output type to be decoded. + type Output: codec::Decode; + /// An optional processor, for performing any additional processing before decoding. + type Processor: Processor; - // Default implementation which charges appropriate weight before building the output. - pub struct DefaultBuilder(PhantomData<(C, O)>); - impl Builder for DefaultBuilder { - type C = C; - type Output = Output; + /// The error to return if decoding fails. + const ERROR: DispatchError = DECODING_FAILED_ERROR; - fn build>( - env: &mut Environment, - ) -> Result { - let contract_host_weight = ::Schedule::get().host_fn_weights; + /// Decodes data read from contract memory. + /// + /// # Parameters + /// - `env` - The current execution environment. + fn decode(env: &mut Environment) -> Result { + // Charge appropriate weight prior to decoding. + let contract_host_weight = ::Schedule::get().host_fn_weights; let len = env.in_len(); env.charge_weight(contract_host_weight.return_per_byte.saturating_mul(len.into()))?; - let params = env.read(len)?; - Output::decode(&mut ¶ms[..]).map_err(|_| DECODING_FAILED_ERROR) + // Read input supplied by contract. + let mut input = env.read(len)?; + // Perform any additional processing required. Any implementation is expected to charge weight as appropriate. + Self::Processor::process(&mut input, env); + // Finally decode and return. + Self::Output::decode(&mut &input[..]).map_err(|_| Self::ERROR) } } - // Implementation which charges appropriate weight before building the output, prefixed with additional data. - pub struct PrefixBuilder(PhantomData<(C, O)>); - impl Builder for PrefixBuilder { - type C = C; + /// Default implementation for decoding data read from contract memory. + pub struct Decodes(PhantomData<(O, P)>); + impl Decode for Decodes { type Output = Output; + type Processor = Processor_; + } - fn build>( - env: &mut Environment, - ) -> Result { - let contract_host_weight = ::Schedule::get().host_fn_weights; - let len = env.in_len(); - env.charge_weight(contract_host_weight.return_per_byte.saturating_mul(len.into()))?; - let mut params = env.read(len)?; - - // TODO: refactor out into own type, merging default and prefix into a single implementation with optional prefix handling - let version = env.func_id().to_le_bytes()[1]; - let (pallet_index, call_index) = { - let bytes = env.ext_id().to_le_bytes(); - (bytes[0], bytes[1]) - }; - params.insert(0, version); - params.insert(1, pallet_index); - params.insert(2, call_index); - - Output::decode(&mut ¶ms[..]).map_err(|_| DECODING_FAILED_ERROR) - } + /// Trait for processing a value based on additional information available from the environment. + pub trait Processor { + /// Processes the provided value. + /// + /// # Parameters + /// - `value` - The value to be processed. + /// - `env` - The current execution environment. + fn process(value: &mut Vec, env: &mut Environment); + } + + impl Processor for () { + fn process(_value: &mut Vec, _env: &mut Environment) {} } } pub mod matching { use super::*; - // Simple trait for determining of a function matches some identifier + // Simple trait for determining if a `Function` matches a function identifier pub trait Matches { fn matches(func_id: u16) -> bool; } @@ -231,9 +234,9 @@ mod functions { } } - // Implementation which matches on the first byte only - pub struct FirstByte(PhantomData); - impl> Matches for FirstByte { + // Implementation which matches on the first byte of a function identifier only + pub struct FirstByteOfFunctionId(PhantomData); + impl> Matches for FirstByteOfFunctionId { fn matches(function_id: u16) -> bool { let bytes = function_id.to_le_bytes(); bytes[0] == T::get() @@ -241,3 +244,27 @@ mod functions { } } } + +pub mod pop_api { + use super::functions::decoding::Processor; + use pallet_contracts::chain_extension::{Environment, Ext, State}; + + // Use bytes from func_id() + ext_id() to prefix the encoded input bytes to determine the versioned output + // TODO: implementation is technically specific to pop-api so should be moved elsewhere - e.g. pallet-api + pub struct Prepender; + impl Processor for Prepender { + fn process(value: &mut Vec, env: &mut Environment) { + // TODO: revisit the ordering based on specced standard + // Resolve version, pallet and call index from environment + let version = env.func_id().to_le_bytes()[1]; + let (pallet_index, call_index) = { + let bytes = env.ext_id().to_le_bytes(); + (bytes[0], bytes[1]) + }; + // Prepend bytes + value.insert(0, version); + value.insert(1, pallet_index); + value.insert(2, call_index); + } + } +} diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index 0fd3d5d4..210cd974 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -72,9 +72,12 @@ pub(crate) mod reboot { use codec::Decode; use cumulus_primitives_core::Weight; use frame_support::traits::Contains; - use pop_chain_extension::reboot::{RuntimeRead as Read, *}; + use pop_chain_extension::reboot::{pop_api::*, RuntimeRead as Read, *}; use sp_core::ConstU8; + // Use bytes from func_id() + ext_id() to prefix the encoded input bytes to determine the versioned output + type Decodes = pop_chain_extension::reboot::Decodes; + #[derive(Decode, Debug)] pub enum VersionedRuntimeCall { #[codec(index = 0)] @@ -161,26 +164,20 @@ pub(crate) mod reboot { // Dispatching calls DispatchCall< Runtime, - // Use bytes from func_id() + ext_id() to prefix the encoded input bytes to determine the versioned dispatch - PrefixBuilder, - // Type for versioning runtime calls - VersionedRuntimeCall, + Decodes, // Use first byte of func_id to match to this function, with value of zero. - FirstByte>, + FirstByteOfFunctionId>, // Filtering of allowed calls Filter, >, // Reading state ReadState< Runtime, - // Use bytes from func_id() + ext_id() to prefix the encoded input bytes to determine the versioned read - PrefixBuilder, - // Type for versioning runtime reads - VersionedRuntimeRead, + Decodes, // The current runtime reads available RuntimeRead, // Use first byte of func_id to match to this function, with value of one. - FirstByte>, + FirstByteOfFunctionId>, // Filtering of allowed reads Filter, >, From 02a126e0d309f12dc9e0bcb17104efb507bf306d Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Mon, 19 Aug 2024 23:54:04 +0100 Subject: [PATCH 49/76] refactor: improve api --- extension/src/reboot.rs | 195 ++++++++++++++----------- runtime/devnet/src/config/api.rs | 54 +++---- runtime/devnet/src/config/contracts.rs | 4 +- 3 files changed, 137 insertions(+), 116 deletions(-) diff --git a/extension/src/reboot.rs b/extension/src/reboot.rs index 6b5c2dce..610a88c2 100644 --- a/extension/src/reboot.rs +++ b/extension/src/reboot.rs @@ -7,44 +7,44 @@ use frame_support::{ weights::Weight, }; pub use functions::{ - decoding::{Decode, Decodes, Processor}, - matching::{FirstByteOfFunctionId, FunctionIdMatcher, Matches}, - DispatchCall, Function, ReadState, + matching::{FunctionIdMatcher, Matches}, + Decode, Decodes, DispatchCall, Function, Processor, ReadState, }; -use pallet_contracts::{ - chain_extension::{ - BufInBufOutState, ChainExtension, Environment, Ext, InitState, Result, RetVal, - RetVal::Converging, - }, - Config, +use pallet_contracts::chain_extension::{ + ChainExtension, Environment, Ext, InitState, Result, RetVal, RetVal::Converging, }; use sp_core::Get; use sp_runtime::traits::Dispatchable; use std::marker::PhantomData; +type Schedule = ::Schedule; + +/// A configurable chain extension. #[derive(Default)] -pub struct ApiExtension(PhantomData); -impl ChainExtension for ApiExtension +pub struct Extension(PhantomData); +impl ChainExtension for Extension where - C: Config + Config: pallet_contracts::Config + frame_system::Config< RuntimeCall: GetDispatchInfo + Dispatchable, >, - F: Functions> + 'static, + F: self::Config> + 'static, { - fn call>(&mut self, env: Environment) -> Result { + /// Call the chain extension logic. + /// + /// # Parameters + /// - `env`: Access to the remaining arguments and the execution environment. + fn call>(&mut self, env: Environment) -> Result { let mut env = env.buf_in_buf_out(); - - let contract_host_weight = ::Schedule::get().host_fn_weights; - env.charge_weight(contract_host_weight.debug_message)?; - - F::Function::execute::(env) + env.charge_weight(Schedule::::get().host_fn_weights.debug_message)?; + F::Functions::execute(env) } } -// Simple trait to allow configuration of chain extension functions (workaround for Default requirement) -pub trait Functions { - type Function: Function; +/// Trait for configuration of the chain extension. +pub trait Config { + /// The function(s) available with the chain extension. + type Functions: Function; } // Trait to be implemented for type handling a read of runtime state @@ -55,24 +55,31 @@ pub trait RuntimeRead { mod functions { use super::*; - use decoding::Decode; + pub use decoding::{Decode, Decodes, Processor}; use matching::Matches; + use pallet_contracts::chain_extension::{BufIn, BufOut}; /// A chain extension function. pub trait Function { - type Runtime: Config; - fn execute>( - env: Environment, + /// The configuration of the contracts module. + type Config: pallet_contracts::Config; + + /// Executes the function. + /// + /// # Parameters + /// - `env` - The current execution environment. + fn execute, S: BufIn + BufOut>( + env: Environment, ) -> Result; } #[impl_trait_for_tuples::impl_for_tuples(1, 3)] #[tuple_types_custom_trait_bound(Function + Matches)] - impl Function for Tuple { - for_tuples!( where #( Tuple: Function )* ); - type Runtime = Runtime; - fn execute>( - env: Environment, + impl Function for Tuple { + for_tuples!( where #( Tuple: Function )* ); + type Config = Runtime; + fn execute, S: BufIn + BufOut>( + env: Environment, ) -> Result { for_tuples!( #( if Tuple::matches(env.func_id()) { @@ -84,86 +91,95 @@ mod functions { } } - // Function implementation for dispatching a call. - pub struct DispatchCall(PhantomData<(R, D, M, F)>); + /// A function for dispatching a runtime call. + pub struct DispatchCall(PhantomData<(C, D, M, F)>); impl< - Runtime: Config + Config: pallet_contracts::Config + frame_system::Config< RuntimeCall: GetDispatchInfo + Dispatchable, >, - Decoder: Decode::RuntimeCall>>, + Decoder: Decode::RuntimeCall>>, Matcher: Matches, - Filter: Contains<::RuntimeCall> + 'static, - > Function for DispatchCall + Filter: Contains<::RuntimeCall> + 'static, + > Function for DispatchCall { - type Runtime = Runtime; + /// The configuration of the contracts module. + type Config = Config; - fn execute>( - mut env: Environment, + /// Executes the function. + /// + /// # Parameters + /// - `env` - The current execution environment. + fn execute, S: BufIn + BufOut>( + mut env: Environment, ) -> Result { - // Build call + // Decode runtime call let call = Decoder::decode(&mut env)?.into(); // Charge weight before dispatch - let charged_weight = env.charge_weight(call.get_dispatch_info().weight)?; + let dispatch_info = call.get_dispatch_info(); + let charged = env.charge_weight(dispatch_info.weight)?; // Ensure call allowed - let mut origin: Runtime::RuntimeOrigin = + let mut origin: Config::RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); origin.add_filter(Filter::contains); - - let (result, weight) = match call.dispatch(origin) { - Ok(info) => (Ok(()), info.actual_weight), - Err(err) => (Err(err.error), err.post_info.actual_weight), - }; - // Adjust post-dispatch weight - if let Some(actual_weight) = weight { - env.adjust_weight(charged_weight, actual_weight); - } - result.map(|_| Converging(0)) + // Dispatch call + let result = call.dispatch(origin); + // Adjust weight + let weight = frame_support::dispatch::extract_actual_weight(&result, &dispatch_info); + env.adjust_weight(charged, weight); + result.map(|_| Converging(0)).map_err(|e| e.error) } } - impl Matches for DispatchCall { + impl Matches for DispatchCall { fn matches(func_id: u16) -> bool { M::matches(func_id) } } - // Function implementation for reading state. - pub struct ReadState(PhantomData<(R, D, RR, M, F)>); + /// A function for reading runtime state. + pub struct ReadState(PhantomData<(C, R, D, M, F)>); impl< - Runtime: Config, - Decoder: Decode>, + Config: pallet_contracts::Config, Read: RuntimeRead, + Decoder: Decode>, Matcher: Matches, Filter: Contains, - > Function for ReadState + > Function for ReadState { - type Runtime = Runtime; + /// The configuration of the contracts module. + type Config = Config; - fn execute>( - mut env: Environment, + /// Executes the function. + /// + /// # Parameters + /// - `env` - The current execution environment. + fn execute, S: BufIn + BufOut>( + mut env: Environment, ) -> Result { - // Build runtime read + // Decode runtime read let read = Decoder::decode(&mut env)?.into(); // Charge weight before read env.charge_weight(read.weight())?; - // Ensure call allowed - // TODO: use filtered error so easier to determine if it is decoding error or a filter blocking the call - ensure!(Filter::contains(&read), UNKNOWN_CALL_ERROR); - // TODO: check remaining parameters (allow_skip, weight per byte) - env.write(&read.read(), false, None)?; + // Ensure read allowed + ensure!(Filter::contains(&read), frame_system::Error::::CallFiltered); + // TODO: check parameters (allow_skip, weight_per_byte) + env.write( + &read.read(), + false, + Some(Schedule::::get().host_fn_weights.input_per_byte), + )?; Ok(Converging(0)) } } - impl Matches for ReadState { + impl Matches for ReadState { fn matches(func_id: u16) -> bool { M::matches(func_id) } } - /// Provides functionality for processing/decoding data read from contract memory. - pub mod decoding { + mod decoding { use super::*; use pallet_contracts::chain_extension::{BufIn, State}; use sp_runtime::DispatchError; @@ -184,9 +200,13 @@ mod functions { /// - `env` - The current execution environment. fn decode(env: &mut Environment) -> Result { // Charge appropriate weight prior to decoding. - let contract_host_weight = ::Schedule::get().host_fn_weights; let len = env.in_len(); - env.charge_weight(contract_host_weight.return_per_byte.saturating_mul(len.into()))?; + env.charge_weight( + Schedule::::get() + .host_fn_weights + .return_per_byte + .saturating_mul(len.into()), + )?; // Read input supplied by contract. let mut input = env.read(len)?; // Perform any additional processing required. Any implementation is expected to charge weight as appropriate. @@ -233,24 +253,22 @@ mod functions { function_id == T::get() } } - - // Implementation which matches on the first byte of a function identifier only - pub struct FirstByteOfFunctionId(PhantomData); - impl> Matches for FirstByteOfFunctionId { - fn matches(function_id: u16) -> bool { - let bytes = function_id.to_le_bytes(); - bytes[0] == T::get() - } - } } } +// TODO: below implementations are technically specific to pop-api so should be moved elsewhere - e.g. pallet-api pub mod pop_api { - use super::functions::decoding::Processor; + use super::{Decodes, Matches, Processor}; + use core::marker::PhantomData; use pallet_contracts::chain_extension::{Environment, Ext, State}; + use sp_core::Get; + + pub type PopApi = super::Extension; + + // Use bytes from func_id() + ext_id() to prefix the encoded input bytes to determine the versioned output + pub type DecodesAs = Decodes; // Use bytes from func_id() + ext_id() to prefix the encoded input bytes to determine the versioned output - // TODO: implementation is technically specific to pop-api so should be moved elsewhere - e.g. pallet-api pub struct Prepender; impl Processor for Prepender { fn process(value: &mut Vec, env: &mut Environment) { @@ -267,4 +285,13 @@ pub mod pop_api { value.insert(2, call_index); } } + + // Implementation which matches on the first byte of a function identifier only + pub struct FirstByteOfFunctionId(PhantomData); + impl> Matches for FirstByteOfFunctionId { + fn matches(function_id: u16) -> bool { + let bytes = function_id.to_le_bytes(); + bytes[0] == T::get() + } + } } diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index 210cd974..7141127c 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -75,8 +75,30 @@ pub(crate) mod reboot { use pop_chain_extension::reboot::{pop_api::*, RuntimeRead as Read, *}; use sp_core::ConstU8; - // Use bytes from func_id() + ext_id() to prefix the encoded input bytes to determine the versioned output - type Decodes = pop_chain_extension::reboot::Decodes; + pub type PopApi = Extension; + + #[derive(Default)] + pub struct Config; + impl pop_chain_extension::reboot::Config for Config { + // Configure the functions available + type Functions = ( + // Dispatching calls + DispatchCall< + Runtime, + DecodesAs, + FirstByteOfFunctionId>, + Filter, + >, + // Reading state + ReadState< + Runtime, + RuntimeRead, + DecodesAs, + FirstByteOfFunctionId>, + Filter, + >, + ); + } #[derive(Decode, Debug)] pub enum VersionedRuntimeCall { @@ -155,32 +177,4 @@ pub(crate) mod reboot { ) } } - - #[derive(Default)] - pub struct Functions; - impl pop_chain_extension::reboot::Functions for Functions { - // Configure the functions available - type Function = ( - // Dispatching calls - DispatchCall< - Runtime, - Decodes, - // Use first byte of func_id to match to this function, with value of zero. - FirstByteOfFunctionId>, - // Filtering of allowed calls - Filter, - >, - // Reading state - ReadState< - Runtime, - Decodes, - // The current runtime reads available - RuntimeRead, - // Use first byte of func_id to match to this function, with value of one. - FirstByteOfFunctionId>, - // Filtering of allowed reads - Filter, - >, - ); - } } diff --git a/runtime/devnet/src/config/contracts.rs b/runtime/devnet/src/config/contracts.rs index 16f1f7de..e0db4334 100644 --- a/runtime/devnet/src/config/contracts.rs +++ b/runtime/devnet/src/config/contracts.rs @@ -1,4 +1,4 @@ -use super::api::reboot::Functions; +use super::api::reboot::PopApi; use crate::{ deposit, Balance, Balances, BalancesCall, Perbill, Runtime, RuntimeCall, RuntimeEvent, RuntimeHoldReason, Timestamp, @@ -64,7 +64,7 @@ impl pallet_contracts::Config for Runtime { type CallStack = [pallet_contracts::Frame; 23]; type WeightPrice = pallet_transaction_payment::Pallet; type WeightInfo = pallet_contracts::weights::SubstrateWeight; - type ChainExtension = pop_chain_extension::reboot::ApiExtension; + type ChainExtension = PopApi; type Schedule = Schedule; type AddressGenerator = pallet_contracts::DefaultAddressGenerator; // This node is geared towards development and testing of contracts. From f36fc8783d900caf705686343e152a85698bd9b3 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 20 Aug 2024 00:01:37 +0100 Subject: [PATCH 50/76] refactor: readable --- extension/src/reboot.rs | 19 +++++++++++-------- runtime/devnet/src/config/api.rs | 6 ++++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/extension/src/reboot.rs b/extension/src/reboot.rs index 610a88c2..7f4d4608 100644 --- a/extension/src/reboot.rs +++ b/extension/src/reboot.rs @@ -8,7 +8,7 @@ use frame_support::{ }; pub use functions::{ matching::{FunctionIdMatcher, Matches}, - Decode, Decodes, DispatchCall, Function, Processor, ReadState, + Decode, Decodes, DispatchCall, Function, Processor, ReadState, Readable, }; use pallet_contracts::chain_extension::{ ChainExtension, Environment, Ext, InitState, Result, RetVal, RetVal::Converging, @@ -47,12 +47,6 @@ pub trait Config { type Functions: Function; } -// Trait to be implemented for type handling a read of runtime state -pub trait RuntimeRead { - fn weight(&self) -> Weight; - fn read(self) -> Vec; -} - mod functions { use super::*; pub use decoding::{Decode, Decodes, Processor}; @@ -141,7 +135,7 @@ mod functions { pub struct ReadState(PhantomData<(C, R, D, M, F)>); impl< Config: pallet_contracts::Config, - Read: RuntimeRead, + Read: Readable, Decoder: Decode>, Matcher: Matches, Filter: Contains, @@ -179,6 +173,15 @@ mod functions { } } + /// Trait to be implemented for type handling a read of runtime state + pub trait Readable { + /// Determines the weight of the read, used to charge the appropriate weight before the read is performed. + fn weight(&self) -> Weight; + + /// Performs the read and returns the result. + fn read(self) -> Vec; + } + mod decoding { use super::*; use pallet_contracts::chain_extension::{BufIn, State}; diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index 7141127c..f1431ecc 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -72,7 +72,7 @@ pub(crate) mod reboot { use codec::Decode; use cumulus_primitives_core::Weight; use frame_support::traits::Contains; - use pop_chain_extension::reboot::{pop_api::*, RuntimeRead as Read, *}; + use pop_chain_extension::reboot::{pop_api::*, *}; use sp_core::ConstU8; pub type PopApi = Extension; @@ -130,12 +130,14 @@ pub(crate) mod reboot { } } - impl Read for RuntimeRead { + impl Readable for RuntimeRead { + /// Determines the weight of the read, used to charge the appropriate weight before the read is performed. fn weight(&self) -> Weight { // TODO: defer to relevant pallet - e.g. RuntimeRead::Fungibles(key) => fungibles::Pallet::read_weight(key), ::DbWeight::get().reads(1_u64) } + /// Performs the read and returns the result. fn read(self) -> Vec { match self { RuntimeRead::Fungibles(key) => fungibles::Pallet::read_state(key), From c2a657944d86b1ccca5236575d904bf8efde9955 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 20 Aug 2024 00:25:05 +0100 Subject: [PATCH 51/76] refactor: improve matching --- extension/src/reboot.rs | 51 ++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/extension/src/reboot.rs b/extension/src/reboot.rs index 7f4d4608..5a04c9e8 100644 --- a/extension/src/reboot.rs +++ b/extension/src/reboot.rs @@ -7,8 +7,8 @@ use frame_support::{ weights::Weight, }; pub use functions::{ - matching::{FunctionIdMatcher, Matches}, - Decode, Decodes, DispatchCall, Function, Processor, ReadState, Readable, + Decode, Decodes, DispatchCall, Equals, Function, FunctionId, Matches, Processor, ReadState, + Readable, }; use pallet_contracts::chain_extension::{ ChainExtension, Environment, Ext, InitState, Result, RetVal, RetVal::Converging, @@ -50,7 +50,7 @@ pub trait Config { mod functions { use super::*; pub use decoding::{Decode, Decodes, Processor}; - use matching::Matches; + pub use matching::{Equals, FunctionId, Matches}; use pallet_contracts::chain_extension::{BufIn, BufOut}; /// A chain extension function. @@ -76,7 +76,7 @@ mod functions { env: Environment, ) -> Result { for_tuples!( #( - if Tuple::matches(env.func_id()) { + if Tuple::matches(env.ext_id(), env.func_id()) { return Tuple::execute(env) } )* ); @@ -126,8 +126,8 @@ mod functions { } impl Matches for DispatchCall { - fn matches(func_id: u16) -> bool { - M::matches(func_id) + fn matches(ext_id: u16, func_id: u16) -> bool { + M::matches(ext_id, func_id) } } @@ -168,8 +168,8 @@ mod functions { } impl Matches for ReadState { - fn matches(func_id: u16) -> bool { - M::matches(func_id) + fn matches(ext_id: u16, func_id: u16) -> bool { + M::matches(ext_id, func_id) } } @@ -241,19 +241,32 @@ mod functions { } } - pub mod matching { + mod matching { use super::*; - // Simple trait for determining if a `Function` matches a function identifier + /// Trait for matching a function. pub trait Matches { - fn matches(func_id: u16) -> bool; + /// Determines whether a function is a match. + /// + /// # Parameters + /// - `ext_id` - The specified chain extension identifier. + /// - `func_id` - The specified function identifier. + fn matches(ext_id: u16, func_id: u16) -> bool; + } + + /// Matches on an extension and function identifier. + pub struct Equals(PhantomData<(E, F)>); + impl, F: Get> Matches for Equals { + fn matches(ext_id: u16, func_id: u16) -> bool { + ext_id == E::get() && func_id == F::get() + } } - // Default implementation - pub struct FunctionIdMatcher(PhantomData); - impl> Matches for FunctionIdMatcher { - fn matches(function_id: u16) -> bool { - function_id == T::get() + /// Matches on a function identifier only. + pub struct FunctionId(PhantomData); + impl> Matches for FunctionId { + fn matches(_ext_id: u16, func_id: u16) -> bool { + func_id == T::get() } } } @@ -289,11 +302,11 @@ pub mod pop_api { } } - // Implementation which matches on the first byte of a function identifier only + /// Matches on the first byte of a function identifier only. pub struct FirstByteOfFunctionId(PhantomData); impl> Matches for FirstByteOfFunctionId { - fn matches(function_id: u16) -> bool { - let bytes = function_id.to_le_bytes(); + fn matches(_ext_id: u16, func_id: u16) -> bool { + let bytes = func_id.to_le_bytes(); bytes[0] == T::get() } } From 8d48ebb727ef2c217ed9b309dc3878dd7ba4e22c Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 20 Aug 2024 01:18:18 +0100 Subject: [PATCH 52/76] refactor: porting logging and comments --- extension/src/reboot.rs | 64 ++++++++++++++++++++++---------- runtime/devnet/src/config/api.rs | 11 +++++- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/extension/src/reboot.rs b/extension/src/reboot.rs index 5a04c9e8..28d22649 100644 --- a/extension/src/reboot.rs +++ b/extension/src/reboot.rs @@ -22,22 +22,27 @@ type Schedule = ::Schedule; /// A configurable chain extension. #[derive(Default)] pub struct Extension(PhantomData); -impl ChainExtension for Extension +impl ChainExtension for Extension where - Config: pallet_contracts::Config + Runtime: pallet_contracts::Config + frame_system::Config< RuntimeCall: GetDispatchInfo + Dispatchable, >, - F: self::Config> + 'static, + Config: self::Config> + 'static, { /// Call the chain extension logic. /// /// # Parameters /// - `env`: Access to the remaining arguments and the execution environment. - fn call>(&mut self, env: Environment) -> Result { + fn call>(&mut self, env: Environment) -> Result { + log::debug!(target: Config::LOG_TARGET, " extension called "); let mut env = env.buf_in_buf_out(); - env.charge_weight(Schedule::::get().host_fn_weights.debug_message)?; - F::Functions::execute(env) + // Charge weight for making a call from a contract to the runtime. + // `debug_message` weight is a good approximation of the additional overhead of going from contract layer to substrate layer. + // reference: https://github.com/paritytech/ink-examples/blob/b8d2caa52cf4691e0ddd7c919e4462311deb5ad0/psp22-extension/runtime/psp22-extension-example.rs#L236 + env.charge_weight(Schedule::::get().host_fn_weights.debug_message)?; + // Execute the function + Config::Functions::execute(env) } } @@ -45,6 +50,9 @@ where pub trait Config { /// The function(s) available with the chain extension. type Functions: Function; + + /// The log target. + const LOG_TARGET: &'static str; } mod functions { @@ -75,12 +83,15 @@ mod functions { fn execute, S: BufIn + BufOut>( env: Environment, ) -> Result { + // Attempts to match a specified extension/function identifier to its corresponding function, as configured by the runtime. for_tuples!( #( - if Tuple::matches(env.ext_id(), env.func_id()) { - return Tuple::execute(env) - } - )* ); + if Tuple::matches(env.ext_id(), env.func_id()) { + return Tuple::execute(env) + } + )* ); + // Otherwise returns `DispatchError::Other` indicating an unmatched request. + // TODO: improve error so we can determine if its an invalid function vs unknown runtime call/read Err(UNKNOWN_CALL_ERROR) } } @@ -107,20 +118,27 @@ mod functions { fn execute, S: BufIn + BufOut>( mut env: Environment, ) -> Result { - // Decode runtime call + const LOG_PREFIX: &str = " dispatch |"; + + // Decode runtime call. let call = Decoder::decode(&mut env)?.into(); - // Charge weight before dispatch + // TODO: log::debug!(target:LOG_TARGET, "{} Inputted RuntimeCall: {:?}", LOG_PREFIX, call); + // Charge weight before dispatch. let dispatch_info = call.get_dispatch_info(); let charged = env.charge_weight(dispatch_info.weight)?; - // Ensure call allowed + // TODO: log::trace! dispatch_info and charged + // Contract is the origin by default. let mut origin: Config::RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); + // Ensure call allowed. origin.add_filter(Filter::contains); - // Dispatch call + // Dispatch call. let result = call.dispatch(origin); - // Adjust weight + // TODO: log::debug!(target:LOG_TARGET, "{} result, actual weight: {:?}", LOG_PREFIX, info.actual_weight); + // Adjust weight. let weight = frame_support::dispatch::extract_actual_weight(&result, &dispatch_info); env.adjust_weight(charged, weight); + // TODO: conversion of error to 'versioned' status code result.map(|_| Converging(0)).map_err(|e| e.error) } } @@ -151,18 +169,25 @@ mod functions { fn execute, S: BufIn + BufOut>( mut env: Environment, ) -> Result { + const LOG_PREFIX: &str = " dispatch |"; + // Decode runtime read let read = Decoder::decode(&mut env)?.into(); + // TODO: log::debug!(target:LOG_TARGET, "{} Inputted RuntimeRead: {:?}", LOG_PREFIX, read); // Charge weight before read - env.charge_weight(read.weight())?; + let _charged = env.charge_weight(read.weight())?; + // TODO: log::trace! charged // Ensure read allowed ensure!(Filter::contains(&read), frame_system::Error::::CallFiltered); + let result = read.read(); + // TODO: log::debug!(target:LOG_TARGET, "{} result: {:?}", LOG_PREFIX, result); // TODO: check parameters (allow_skip, weight_per_byte) env.write( - &read.read(), + &result, false, Some(Schedule::::get().host_fn_weights.input_per_byte), )?; + // TODO: conversion of error to 'versioned' status code Ok(Converging(0)) } } @@ -202,7 +227,8 @@ mod functions { /// # Parameters /// - `env` - The current execution environment. fn decode(env: &mut Environment) -> Result { - // Charge appropriate weight prior to decoding. + // Charge appropriate weight, based on input length, prior to decoding. + // reference: https://github.com/paritytech/polkadot-sdk/blob/117a9433dac88d5ac00c058c9b39c511d47749d2/substrate/frame/contracts/src/wasm/runtime.rs#L267 let len = env.in_len(); env.charge_weight( Schedule::::get() @@ -210,7 +236,7 @@ mod functions { .return_per_byte .saturating_mul(len.into()), )?; - // Read input supplied by contract. + // Read encoded input supplied by contract for buffer. let mut input = env.read(len)?; // Perform any additional processing required. Any implementation is expected to charge weight as appropriate. Self::Processor::process(&mut input, env); diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index f1431ecc..ddebb2f2 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -80,7 +80,10 @@ pub(crate) mod reboot { #[derive(Default)] pub struct Config; impl pop_chain_extension::reboot::Config for Config { - // Configure the functions available + /// Functions used by the Pop API + /// . + /// Each function corresponds to specific functionality provided by the API, facilitating the + // interaction between smart contracts and the runtime. type Functions = ( // Dispatching calls DispatchCall< @@ -98,10 +101,14 @@ pub(crate) mod reboot { Filter, >, ); + + const LOG_TARGET: &'static str = "pop-api::extension"; } + /// Versioned runtime calls. #[derive(Decode, Debug)] pub enum VersionedRuntimeCall { + /// Version zero of runtime calls. #[codec(index = 0)] V0(RuntimeCall), } @@ -115,8 +122,10 @@ pub(crate) mod reboot { } } + /// Versioned runtime state reads. #[derive(Decode, Debug)] pub enum VersionedRuntimeRead { + /// Version zero of runtime state reads. #[codec(index = 0)] V0(RuntimeRead), } From f4db433185a080d0e4bfd8ab6a781b0e12e6e3fa Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 20 Aug 2024 01:42:03 +0100 Subject: [PATCH 53/76] refactor: relocate pop-api specifics to api pallet --- Cargo.lock | 1 + extension/src/reboot.rs | 43 ++------------------------ pallets/api/Cargo.toml | 3 ++ pallets/api/src/extension.rs | 36 +++++++++++++++++++++ pallets/api/src/lib.rs | 3 ++ runtime/devnet/src/config/api.rs | 6 ++-- runtime/devnet/src/config/contracts.rs | 4 +-- 7 files changed, 49 insertions(+), 47 deletions(-) create mode 100644 pallets/api/src/extension.rs diff --git a/Cargo.lock b/Cargo.lock index 50196ef3..76cf4b5b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6330,6 +6330,7 @@ dependencies = [ "pallet-assets", "pallet-balances", "parity-scale-codec", + "pop-chain-extension", "scale-info", "sp-core", "sp-io", diff --git a/extension/src/reboot.rs b/extension/src/reboot.rs index 28d22649..b00b2b7b 100644 --- a/extension/src/reboot.rs +++ b/extension/src/reboot.rs @@ -11,8 +11,9 @@ pub use functions::{ Readable, }; use pallet_contracts::chain_extension::{ - ChainExtension, Environment, Ext, InitState, Result, RetVal, RetVal::Converging, + ChainExtension, InitState, Result, RetVal, RetVal::Converging, }; +pub use pallet_contracts::chain_extension::{Environment, Ext, State}; use sp_core::Get; use sp_runtime::traits::Dispatchable; use std::marker::PhantomData; @@ -297,43 +298,3 @@ mod functions { } } } - -// TODO: below implementations are technically specific to pop-api so should be moved elsewhere - e.g. pallet-api -pub mod pop_api { - use super::{Decodes, Matches, Processor}; - use core::marker::PhantomData; - use pallet_contracts::chain_extension::{Environment, Ext, State}; - use sp_core::Get; - - pub type PopApi = super::Extension; - - // Use bytes from func_id() + ext_id() to prefix the encoded input bytes to determine the versioned output - pub type DecodesAs = Decodes; - - // Use bytes from func_id() + ext_id() to prefix the encoded input bytes to determine the versioned output - pub struct Prepender; - impl Processor for Prepender { - fn process(value: &mut Vec, env: &mut Environment) { - // TODO: revisit the ordering based on specced standard - // Resolve version, pallet and call index from environment - let version = env.func_id().to_le_bytes()[1]; - let (pallet_index, call_index) = { - let bytes = env.ext_id().to_le_bytes(); - (bytes[0], bytes[1]) - }; - // Prepend bytes - value.insert(0, version); - value.insert(1, pallet_index); - value.insert(2, call_index); - } - } - - /// Matches on the first byte of a function identifier only. - pub struct FirstByteOfFunctionId(PhantomData); - impl> Matches for FirstByteOfFunctionId { - fn matches(_ext_id: u16, func_id: u16) -> bool { - let bytes = func_id.to_le_bytes(); - bytes[0] == T::get() - } - } -} diff --git a/pallets/api/Cargo.toml b/pallets/api/Cargo.toml index f9a807a3..c03c68f5 100644 --- a/pallets/api/Cargo.toml +++ b/pallets/api/Cargo.toml @@ -13,6 +13,9 @@ targets = ["x86_64-unknown-linux-gnu"] codec.workspace = true scale-info.workspace = true +# Local +pop-chain-extension.workspace = true + # Substrate frame-benchmarking.workspace = true frame-support.workspace = true diff --git a/pallets/api/src/extension.rs b/pallets/api/src/extension.rs new file mode 100644 index 00000000..6cb8fc8a --- /dev/null +++ b/pallets/api/src/extension.rs @@ -0,0 +1,36 @@ +use core::marker::PhantomData; +use frame_support::traits::Get; +pub use pop_chain_extension::reboot::{Config, DispatchCall, ReadState, Readable}; +use pop_chain_extension::reboot::{Decodes, Environment, Ext, Matches, Processor, State}; + +pub type Extension = pop_chain_extension::reboot::Extension; + +/// Decodes output by prepending bytes from ext_id() + func_id() +pub type DecodesAs = Decodes; + +/// Prepends bytes from ext_id() + func_id() to prefix the encoded input bytes to determine the versioned output +pub struct Prepender; +impl Processor for Prepender { + fn process(value: &mut Vec, env: &mut Environment) { + // TODO: revisit the ordering based on specced standard + // Resolve version, pallet and call index from environment + let version = env.func_id().to_le_bytes()[1]; + let (pallet_index, call_index) = { + let bytes = env.ext_id().to_le_bytes(); + (bytes[0], bytes[1]) + }; + // Prepend bytes + value.insert(0, version); + value.insert(1, pallet_index); + value.insert(2, call_index); + } +} + +/// Matches on the first byte of a function identifier only. +pub struct FirstByteOfFunctionId(PhantomData); +impl> Matches for FirstByteOfFunctionId { + fn matches(_ext_id: u16, func_id: u16) -> bool { + let bytes = func_id.to_le_bytes(); + bytes[0] == T::get() + } +} diff --git a/pallets/api/src/lib.rs b/pallets/api/src/lib.rs index 5cba0551..6e94609b 100644 --- a/pallets/api/src/lib.rs +++ b/pallets/api/src/lib.rs @@ -1,5 +1,8 @@ #![cfg_attr(not(feature = "std"), no_std)] +pub use extension::Extension; + +pub mod extension; pub mod fungibles; #[cfg(test)] mod mock; diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index ddebb2f2..ca6da01a 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -72,14 +72,12 @@ pub(crate) mod reboot { use codec::Decode; use cumulus_primitives_core::Weight; use frame_support::traits::Contains; - use pop_chain_extension::reboot::{pop_api::*, *}; + use pallet_api::extension::*; use sp_core::ConstU8; - pub type PopApi = Extension; - #[derive(Default)] pub struct Config; - impl pop_chain_extension::reboot::Config for Config { + impl pallet_api::extension::Config for Config { /// Functions used by the Pop API /// . /// Each function corresponds to specific functionality provided by the API, facilitating the diff --git a/runtime/devnet/src/config/contracts.rs b/runtime/devnet/src/config/contracts.rs index e0db4334..b2878f52 100644 --- a/runtime/devnet/src/config/contracts.rs +++ b/runtime/devnet/src/config/contracts.rs @@ -1,4 +1,4 @@ -use super::api::reboot::PopApi; +use super::api::reboot::Config; use crate::{ deposit, Balance, Balances, BalancesCall, Perbill, Runtime, RuntimeCall, RuntimeEvent, RuntimeHoldReason, Timestamp, @@ -64,7 +64,7 @@ impl pallet_contracts::Config for Runtime { type CallStack = [pallet_contracts::Frame; 23]; type WeightPrice = pallet_transaction_payment::Pallet; type WeightInfo = pallet_contracts::weights::SubstrateWeight; - type ChainExtension = PopApi; + type ChainExtension = pallet_api::Extension; type Schedule = Schedule; type AddressGenerator = pallet_contracts::DefaultAddressGenerator; // This node is geared towards development and testing of contracts. From f2af0ecdeb7cfa15b98c30b6da6368bafe7f001e Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 20 Aug 2024 01:49:14 +0100 Subject: [PATCH 54/76] refactor: address clippy warnings --- runtime/devnet/src/config/api.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index ca6da01a..3a594dd9 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -111,10 +111,10 @@ pub(crate) mod reboot { V0(RuntimeCall), } - impl Into for VersionedRuntimeCall { - fn into(self) -> RuntimeCall { + impl From for RuntimeCall { + fn from(value: VersionedRuntimeCall) -> Self { // Allows mapping from some previous runtime call shape to a current valid runtime call - match self { + match value { VersionedRuntimeCall::V0(call) => call, } } @@ -128,10 +128,10 @@ pub(crate) mod reboot { V0(RuntimeRead), } - impl Into for VersionedRuntimeRead { - fn into(self) -> RuntimeRead { + impl From for RuntimeRead { + fn from(value: VersionedRuntimeRead) -> Self { // Allows mapping from some previous runtime call shape to a current valid runtime read - match self { + match value { VersionedRuntimeRead::V0(read) => read, } } From 025db4bbb01c6dffdb7692be10e4878948834218 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 20 Aug 2024 02:32:21 +0100 Subject: [PATCH 55/76] refactor: replace extension --- extension/src/decoding.rs | 57 ++++ extension/src/error.rs | 59 +++++ extension/src/functions.rs | 137 ++++++++++ extension/src/lib.rs | 345 ++++--------------------- extension/src/matching.rs | 27 ++ extension/src/reboot.rs | 300 --------------------- pallets/api/src/extension.rs | 10 +- runtime/devnet/src/config/api.rs | 209 ++++++--------- runtime/devnet/src/config/contracts.rs | 4 +- 9 files changed, 418 insertions(+), 730 deletions(-) create mode 100644 extension/src/decoding.rs create mode 100644 extension/src/error.rs create mode 100644 extension/src/functions.rs create mode 100644 extension/src/matching.rs delete mode 100644 extension/src/reboot.rs diff --git a/extension/src/decoding.rs b/extension/src/decoding.rs new file mode 100644 index 00000000..da4d37b7 --- /dev/null +++ b/extension/src/decoding.rs @@ -0,0 +1,57 @@ +use super::*; +use pallet_contracts::chain_extension::{BufIn, State}; +use sp_runtime::DispatchError; + +/// Trait for decoding data read from contract memory. +pub trait Decode { + /// The output type to be decoded. + type Output: codec::Decode; + /// An optional processor, for performing any additional processing before decoding. + type Processor: Processor; + + /// The error to return if decoding fails. + const ERROR: DispatchError = DECODING_FAILED_ERROR; + + /// Decodes data read from contract memory. + /// + /// # Parameters + /// - `env` - The current execution environment. + fn decode(env: &mut Environment) -> Result { + // Charge appropriate weight, based on input length, prior to decoding. + // reference: https://github.com/paritytech/polkadot-sdk/blob/117a9433dac88d5ac00c058c9b39c511d47749d2/substrate/frame/contracts/src/wasm/runtime.rs#L267 + let len = env.in_len(); + env.charge_weight( + Schedule::::get() + .host_fn_weights + .return_per_byte + .saturating_mul(len.into()), + )?; + // Read encoded input supplied by contract for buffer. + let mut input = env.read(len)?; + // Perform any additional processing required. Any implementation is expected to charge weight as appropriate. + Self::Processor::process(&mut input, env); + // Finally decode and return. + Self::Output::decode(&mut &input[..]).map_err(|_| Self::ERROR) + } +} + +/// Default implementation for decoding data read from contract memory. +pub struct Decodes(PhantomData<(O, P)>); +impl Decode for Decodes { + type Output = Output; + type Processor = Processor_; +} + +/// Trait for processing a value based on additional information available from the environment. +pub trait Processor { + /// Processes the provided value. + /// + /// # Parameters + /// - `value` - The value to be processed. + /// - `env` - The current execution environment. + fn process(value: &mut Vec, env: &mut Environment); +} + +impl Processor for () { + fn process(_value: &mut Vec, _env: &mut Environment) {} +} diff --git a/extension/src/error.rs b/extension/src/error.rs new file mode 100644 index 00000000..a5da0c0a --- /dev/null +++ b/extension/src/error.rs @@ -0,0 +1,59 @@ +use codec::Encode; +use sp_runtime::DispatchError; + +pub(crate) const DECODING_FAILED_ERROR: DispatchError = DispatchError::Other("DecodingFailed"); +// TODO: issue #93, we can also encode the `pop_primitives::Error::UnknownCall` which means we do use +// `Error` in the runtime and it should stay in primitives. Perhaps issue #91 will also influence +// here. Should be looked at together. +const DECODING_FAILED_ERROR_ENCODED: [u8; 4] = [255u8, 0, 0, 0]; +pub(crate) const UNKNOWN_CALL_ERROR: DispatchError = DispatchError::Other("UnknownCall"); +// TODO: see above. +const UNKNOWN_CALL_ERROR_ENCODED: [u8; 4] = [254u8, 0, 0, 0]; + +/// Converts a `DispatchError` to a `u32` status code based on the version of the API the contract uses. +/// The contract calling the chain extension can optionally convert the status code to the descriptive `Error`. +/// +/// For `Error` see `pop_primitives::::error::Error`. +/// +/// The error encoding can vary per version, allowing for flexible and backward-compatible error handling. +/// As a result, contracts maintain compatibility across different versions of the runtime. +/// +/// # Parameters +/// +/// - `error`: The `DispatchError` encountered during contract execution. +/// - `version`: The version of the chain extension, used to determine the known errors. +pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { + let mut encoded_error: [u8; 4] = match error { + // "UnknownCall" and "DecodingFailed" are mapped to specific errors in the API and will + // never change. + UNKNOWN_CALL_ERROR => UNKNOWN_CALL_ERROR_ENCODED, + DECODING_FAILED_ERROR => DECODING_FAILED_ERROR_ENCODED, + _ => { + let mut encoded_error = error.encode(); + // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). + encoded_error.resize(4, 0); + encoded_error.try_into().expect("qed, resized to 4 bytes line above") + }, + }; + match version { + // If an unknown variant of the `DispatchError` is detected the error needs to be converted + // into the encoded value of `Error::Other`. This conversion is performed by shifting the bytes one + // position forward (discarding the last byte as it is not used) and setting the first byte to the + // encoded value of `Other` (0u8). This ensures the error is correctly categorized as an `Other` + // variant which provides all the necessary information to debug which error occurred in the runtime. + // + // Byte layout explanation: + // - Byte 0: index of the variant within `Error` + // - Byte 1: + // - Must be zero for `UNIT_ERRORS`. + // - Represents the nested error in `SINGLE_NESTED_ERRORS`. + // - Represents the first level of nesting in `DOUBLE_NESTED_ERRORS`. + // - Byte 2: + // - Represents the second level of nesting in `DOUBLE_NESTED_ERRORS`. + // - Byte 3: + // - Unused or represents further nested information. + 0 => crate::v0::handle_unknown_error(&mut encoded_error), + _ => encoded_error = UNKNOWN_CALL_ERROR_ENCODED, + } + u32::from_le_bytes(encoded_error) +} diff --git a/extension/src/functions.rs b/extension/src/functions.rs new file mode 100644 index 00000000..ae7697b0 --- /dev/null +++ b/extension/src/functions.rs @@ -0,0 +1,137 @@ +use super::*; +pub use decoding::{Decode, Decodes, Processor}; +pub use matching::{Equals, FunctionId, Matches}; +use pallet_contracts::chain_extension::{BufIn, BufOut}; + +/// A chain extension function. +pub trait Function { + /// The configuration of the contracts module. + type Config: pallet_contracts::Config; + + /// Executes the function. + /// + /// # Parameters + /// - `env` - The current execution environment. + fn execute, S: BufIn + BufOut>( + env: Environment, + ) -> Result; +} + +#[impl_trait_for_tuples::impl_for_tuples(1, 3)] +#[tuple_types_custom_trait_bound(Function + Matches)] +impl Function for Tuple { + for_tuples!( where #( Tuple: Function )* ); + type Config = Runtime; + fn execute, S: BufIn + BufOut>( + env: Environment, + ) -> Result { + // Attempts to match a specified extension/function identifier to its corresponding function, as configured by the runtime. + for_tuples!( #( + if Tuple::matches(env.ext_id(), env.func_id()) { + return Tuple::execute(env) + } + )* ); + + // Otherwise returns `DispatchError::Other` indicating an unmatched request. + // TODO: improve error so we can determine if its an invalid function vs unknown runtime call/read + Err(UNKNOWN_CALL_ERROR) + } +} + +/// A function for dispatching a runtime call. +pub struct DispatchCall(PhantomData<(C, D, M, F)>); +impl< + Config: pallet_contracts::Config + + frame_system::Config< + RuntimeCall: GetDispatchInfo + Dispatchable, + >, + Decoder: Decode::RuntimeCall>>, + Matcher: Matches, + Filter: Contains<::RuntimeCall> + 'static, + > Function for DispatchCall +{ + /// The configuration of the contracts module. + type Config = Config; + + /// Executes the function. + /// + /// # Parameters + /// - `env` - The current execution environment. + fn execute, S: BufIn + BufOut>( + mut env: Environment, + ) -> Result { + const LOG_PREFIX: &str = " dispatch |"; + + // Decode runtime call. + let call = Decoder::decode(&mut env)?.into(); + // TODO: log::debug!(target:LOG_TARGET, "{} Inputted RuntimeCall: {:?}", LOG_PREFIX, call); + // Charge weight before dispatch. + let dispatch_info = call.get_dispatch_info(); + let charged = env.charge_weight(dispatch_info.weight)?; + // TODO: log::trace! dispatch_info and charged + // Contract is the origin by default. + let mut origin: Config::RuntimeOrigin = + RawOrigin::Signed(env.ext().address().clone()).into(); + // Ensure call allowed. + origin.add_filter(Filter::contains); + // Dispatch call. + let result = call.dispatch(origin); + // TODO: log::debug!(target:LOG_TARGET, "{} result, actual weight: {:?}", LOG_PREFIX, info.actual_weight); + // Adjust weight. + let weight = frame_support::dispatch::extract_actual_weight(&result, &dispatch_info); + env.adjust_weight(charged, weight); + // TODO: conversion of error to 'versioned' status code + result.map(|_| Converging(0)).map_err(|e| e.error) + } +} + +impl Matches for DispatchCall { + fn matches(ext_id: u16, func_id: u16) -> bool { + M::matches(ext_id, func_id) + } +} + +/// A function for reading runtime state. +pub struct ReadState(PhantomData<(C, R, D, M, F)>); +impl< + Config: pallet_contracts::Config, + Read: Readable, + Decoder: Decode>, + Matcher: Matches, + Filter: Contains, + > Function for ReadState +{ + /// The configuration of the contracts module. + type Config = Config; + + /// Executes the function. + /// + /// # Parameters + /// - `env` - The current execution environment. + fn execute, S: BufIn + BufOut>( + mut env: Environment, + ) -> Result { + const LOG_PREFIX: &str = " dispatch |"; + + // Decode runtime read + let read = Decoder::decode(&mut env)?.into(); + // TODO: log::debug!(target:LOG_TARGET, "{} Inputted RuntimeRead: {:?}", LOG_PREFIX, read); + // Charge weight before read + let _charged = env.charge_weight(read.weight())?; + // TODO: log::trace! charged + // Ensure read allowed + ensure!(Filter::contains(&read), frame_system::Error::::CallFiltered); + let result = read.read(); + // TODO: log::debug!(target:LOG_TARGET, "{} result: {:?}", LOG_PREFIX, result); + // TODO: check parameters (allow_skip, weight_per_byte) + env.write(&result, false, Some(Schedule::::get().host_fn_weights.input_per_byte))?; + // TODO: conversion of error to 'versioned' status code + Ok(Converging(0)) + } +} + +impl Matches for ReadState { + fn matches(ext_id: u16, func_id: u16) -> bool { + M::matches(ext_id, func_id) + } +} diff --git a/extension/src/lib.rs b/extension/src/lib.rs index 44fb13be..6dafe868 100644 --- a/extension/src/lib.rs +++ b/extension/src/lib.rs @@ -1,325 +1,76 @@ #![cfg_attr(not(feature = "std"), no_std)] -use codec::Encode; +use crate::error::{DECODING_FAILED_ERROR, UNKNOWN_CALL_ERROR}; +use codec::Decode as _; use frame_support::{ - dispatch::{GetDispatchInfo, PostDispatchInfo}, - pallet_prelude::*, - traits::OriginTrait, + dispatch::{GetDispatchInfo, PostDispatchInfo, RawOrigin}, + ensure, + traits::{Contains, OriginTrait}, + weights::Weight, +}; +pub use functions::{ + Decode, Decodes, DispatchCall, Equals, Function, FunctionId, Matches, Processor, ReadState, }; -use frame_system::RawOrigin; use pallet_contracts::chain_extension::{ - BufInBufOutState, ChainExtension, Environment, Ext, InitState, RetVal, + ChainExtension, InitState, Result, RetVal, RetVal::Converging, }; -use sp_core::crypto::UncheckedFrom; -use sp_runtime::{traits::Dispatchable, DispatchError}; -use sp_std::vec::Vec; - -pub mod reboot; +pub use pallet_contracts::chain_extension::{Environment, Ext, State}; +use sp_core::Get; +use sp_runtime::traits::Dispatchable; +use std::marker::PhantomData; + +mod decoding; +#[allow(dead_code)] +mod error; +mod functions; +mod matching; #[cfg(test)] mod tests; mod v0; -/// Logging target for categorizing messages from the Pop API extension module. -const LOG_TARGET: &str = "pop-api::extension"; - -const DECODING_FAILED_ERROR: DispatchError = DispatchError::Other("DecodingFailed"); -// TODO: issue #93, we can also encode the `pop_primitives::Error::UnknownCall` which means we do use -// `Error` in the runtime and it should stay in primitives. Perhaps issue #91 will also influence -// here. Should be looked at together. -const DECODING_FAILED_ERROR_ENCODED: [u8; 4] = [255u8, 0, 0, 0]; -const UNKNOWN_CALL_ERROR: DispatchError = DispatchError::Other("UnknownCall"); -// TODO: see above. -const UNKNOWN_CALL_ERROR_ENCODED: [u8; 4] = [254u8, 0, 0, 0]; - -type ContractSchedule = ::Schedule; - -/// Type of the state reader. -pub trait ReadState { - /// Query of the state read operations. - type StateQuery: Decode; - - /// Check if a state query is allowed. - fn contains(c: &Self::StateQuery) -> bool; +type Schedule = ::Schedule; - /// Reads state using the provided query, returning the result as a byte vector. - fn read(read: Self::StateQuery) -> Vec; - - /// Decodes parameters into state query. - fn decode(params: &mut &[u8]) -> Result { - decode_checked(params) - } -} - -/// Type of the dispatch call filter. -pub trait CallFilter { - /// Query of the dispatch calls operations. - type Call: Decode; - - /// Check if runtime call is allowed. - fn contains(t: &Self::Call) -> bool; -} - -/// Pop API chain extension. +/// A configurable chain extension. #[derive(Default)] -pub struct ApiExtension(PhantomData); - -impl ChainExtension for ApiExtension +pub struct Extension(PhantomData); +impl ChainExtension for Extension where - T: pallet_contracts::Config + Runtime: pallet_contracts::Config + frame_system::Config< RuntimeCall: GetDispatchInfo + Dispatchable, >, - T::AccountId: UncheckedFrom + AsRef<[u8]>, - // Bound the type by the two traits which need to be implemented by the runtime. - I: ReadState + CallFilter::RuntimeCall> + 'static, + Config: self::Config> + 'static, { - fn call>( - &mut self, - env: Environment, - ) -> Result { - log::debug!(target:LOG_TARGET, " extension called "); + /// Call the chain extension logic. + /// + /// # Parameters + /// - `env`: Access to the remaining arguments and the execution environment. + fn call>(&mut self, env: Environment) -> Result { + log::debug!(target: Config::LOG_TARGET, " extension called "); let mut env = env.buf_in_buf_out(); // Charge weight for making a call from a contract to the runtime. - // `debug_message` weight is a good approximation of the additional overhead of going - // from contract layer to substrate layer. + // `debug_message` weight is a good approximation of the additional overhead of going from contract layer to substrate layer. // reference: https://github.com/paritytech/ink-examples/blob/b8d2caa52cf4691e0ddd7c919e4462311deb5ad0/psp22-extension/runtime/psp22-extension-example.rs#L236 - let contract_host_weight = ContractSchedule::::get().host_fn_weights; - env.charge_weight(contract_host_weight.debug_message)?; - - let (version, function_id, pallet_index, call_index) = extract_env(&env); - - let result = match FuncId::try_from(function_id) { - // Read encoded parameters from buffer and calculate weight for reading `len` bytes`. - Ok(function_id) => { - // reference: https://github.com/paritytech/polkadot-sdk/blob/117a9433dac88d5ac00c058c9b39c511d47749d2/substrate/frame/contracts/src/wasm/runtime.rs#L267 - let len = env.in_len(); - env.charge_weight(contract_host_weight.return_per_byte.saturating_mul(len.into()))?; - let params = env.read(len)?; - match function_id { - FuncId::Dispatch => { - dispatch::(&mut env, version, pallet_index, call_index, params) - }, - FuncId::ReadState => { - read_state::(&mut env, version, pallet_index, call_index, params) - }, - } - }, - Err(e) => Err(e), - }; - - match result { - Ok(_) => Ok(RetVal::Converging(0)), - Err(e) => Ok(RetVal::Converging(convert_to_status_code(e, version))), - } + env.charge_weight(Schedule::::get().host_fn_weights.debug_message)?; + // Execute the function + Config::Functions::execute(env) } } -/// Extract discriminators (version, function_id, pallet_index, call_index) from the encoded call. -fn extract_env>(env: &Environment) -> (u8, u8, u8, u8) { - // Extract version and function_id from first two bytes. - let (version, function_id) = { - let bytes = env.func_id().to_le_bytes(); - (bytes[0], bytes[1]) - }; - // Extract pallet index and call / key index from last two bytes. - let (pallet_index, call_index) = { - let bytes = env.ext_id().to_le_bytes(); - (bytes[0], bytes[1]) - }; +/// Trait for configuration of the chain extension. +pub trait Config { + /// The function(s) available with the chain extension. + type Functions: Function; - (version, function_id, pallet_index, call_index) + /// The log target. + const LOG_TARGET: &'static str; } -fn read_state, StateReader: ReadState>( - env: &mut Environment, - version: u8, - pallet_index: u8, - call_index: u8, - mut params: Vec, -) -> Result<(), DispatchError> { - // Prefix params with version, pallet, index to simplify decoding. - params.insert(0, version); - params.insert(1, pallet_index); - params.insert(2, call_index); - let (mut encoded_version, mut encoded_read) = (¶ms[..1], ¶ms[1..]); - let version = decode_checked::(&mut encoded_version)?; +/// Trait to be implemented for type handling a read of runtime state +pub trait Readable { + /// Determines the weight of the read, used to charge the appropriate weight before the read is performed. + fn weight(&self) -> Weight; - // Charge weight for doing one storage read. - env.charge_weight(T::DbWeight::get().reads(1_u64))?; - let result = match version { - VersionedStateRead::V0 => { - let read = StateReader::decode(&mut encoded_read)?; - ensure!(StateReader::contains(&read), UNKNOWN_CALL_ERROR); - StateReader::read(read) - }, - }; - log::trace!( - target:LOG_TARGET, - "{} result: {:?}.", " read_state |", result - ); - env.write(&result, false, None) -} - -fn dispatch( - env: &mut Environment, - version: u8, - pallet_index: u8, - call_index: u8, - mut params: Vec, -) -> Result<(), DispatchError> -where - T: frame_system::Config< - RuntimeCall: GetDispatchInfo + Dispatchable, - >, - E: Ext, - Filter: CallFilter::RuntimeCall> + 'static, -{ - // Prefix params with version, pallet, index to simplify decoding. - params.insert(0, version); - params.insert(1, pallet_index); - params.insert(2, call_index); - let call = decode_checked::>(&mut ¶ms[..])?; - // Contract is the origin by default. - let origin: T::RuntimeOrigin = RawOrigin::Signed(env.ext().address().clone()).into(); - match call { - VersionedDispatch::V0(call) => dispatch_call::(env, call, origin), - } -} - -/// Helper method to decode the byte data to a provided type and throws error if failed. -fn decode_checked(params: &mut &[u8]) -> Result { - T::decode(params).map_err(|_| DECODING_FAILED_ERROR) -} - -fn dispatch_call( - env: &mut Environment, - call: T::RuntimeCall, - mut origin: T::RuntimeOrigin, -) -> Result<(), DispatchError> -where - T: frame_system::Config< - RuntimeCall: GetDispatchInfo + Dispatchable, - >, - E: Ext, - Filter: CallFilter::RuntimeCall> + 'static, -{ - const LOG_PREFIX: &str = " dispatch |"; - - let charged_dispatch_weight = env.charge_weight(call.get_dispatch_info().weight)?; - log::debug!(target:LOG_TARGET, "{} Inputted RuntimeCall: {:?}", LOG_PREFIX, call); - origin.add_filter(Filter::contains); - match call.dispatch(origin) { - Ok(info) => { - log::debug!(target:LOG_TARGET, "{} success, actual weight: {:?}", LOG_PREFIX, info.actual_weight); - // Refund weight if the actual weight is less than the charged weight. - if let Some(actual_weight) = info.actual_weight { - env.adjust_weight(charged_dispatch_weight, actual_weight); - } - Ok(()) - }, - Err(err) => { - log::debug!(target:LOG_TARGET, "{} failed: error: {:?}", LOG_PREFIX, err.error); - // Refund weight if the actual weight is less than the charged weight. - if let Some(actual_weight) = err.post_info.actual_weight { - env.adjust_weight(charged_dispatch_weight, actual_weight); - } - Err(err.error) - }, - } -} - -/// Wrapper to enable versioning of runtime state reads. -#[derive(Decode, Debug)] -enum VersionedStateRead { - /// Version zero of state reads. - #[codec(index = 0)] - V0, -} - -/// Wrapper to enable versioning of runtime calls. -#[derive(Decode, Debug)] -enum VersionedDispatch { - /// Version zero of dispatch calls. - #[codec(index = 0)] - V0(RuntimeCall), -} - -/// Function identifiers used in the Pop API. -/// -/// The `FuncId` specifies the available functions that can be called through the Pop API. Each -/// variant corresponds to a specific functionality provided by the API, facilitating the -/// interaction between smart contracts and the runtime. -#[derive(Debug)] -pub enum FuncId { - /// Represents a function call to dispatch a runtime call. - Dispatch, - /// Represents a function call to read the state from the runtime. - ReadState, -} - -impl TryFrom for FuncId { - type Error = DispatchError; - - /// Attempts to convert a `u8` value to its corresponding `FuncId` variant. - /// - /// If the `u8` value does not match any known function identifier, it returns a - /// `DispatchError::Other` indicating an unknown function ID. - fn try_from(func_id: u8) -> Result { - let id = match func_id { - 0 => Self::Dispatch, - 1 => Self::ReadState, - _ => { - return Err(UNKNOWN_CALL_ERROR); - }, - }; - Ok(id) - } -} - -/// Converts a `DispatchError` to a `u32` status code based on the version of the API the contract uses. -/// The contract calling the chain extension can optionally convert the status code to the descriptive `Error`. -/// -/// For `Error` see `pop_primitives::::error::Error`. -/// -/// The error encoding can vary per version, allowing for flexible and backward-compatible error handling. -/// As a result, contracts maintain compatibility across different versions of the runtime. -/// -/// # Parameters -/// -/// - `error`: The `DispatchError` encountered during contract execution. -/// - `version`: The version of the chain extension, used to determine the known errors. -pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { - let mut encoded_error: [u8; 4] = match error { - // "UnknownCall" and "DecodingFailed" are mapped to specific errors in the API and will - // never change. - UNKNOWN_CALL_ERROR => UNKNOWN_CALL_ERROR_ENCODED, - DECODING_FAILED_ERROR => DECODING_FAILED_ERROR_ENCODED, - _ => { - let mut encoded_error = error.encode(); - // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). - encoded_error.resize(4, 0); - encoded_error.try_into().expect("qed, resized to 4 bytes line above") - }, - }; - match version { - // If an unknown variant of the `DispatchError` is detected the error needs to be converted - // into the encoded value of `Error::Other`. This conversion is performed by shifting the bytes one - // position forward (discarding the last byte as it is not used) and setting the first byte to the - // encoded value of `Other` (0u8). This ensures the error is correctly categorized as an `Other` - // variant which provides all the necessary information to debug which error occurred in the runtime. - // - // Byte layout explanation: - // - Byte 0: index of the variant within `Error` - // - Byte 1: - // - Must be zero for `UNIT_ERRORS`. - // - Represents the nested error in `SINGLE_NESTED_ERRORS`. - // - Represents the first level of nesting in `DOUBLE_NESTED_ERRORS`. - // - Byte 2: - // - Represents the second level of nesting in `DOUBLE_NESTED_ERRORS`. - // - Byte 3: - // - Unused or represents further nested information. - 0 => v0::handle_unknown_error(&mut encoded_error), - _ => encoded_error = UNKNOWN_CALL_ERROR_ENCODED, - } - u32::from_le_bytes(encoded_error) + /// Performs the read and returns the result. + fn read(self) -> Vec; } diff --git a/extension/src/matching.rs b/extension/src/matching.rs new file mode 100644 index 00000000..95fe1ca3 --- /dev/null +++ b/extension/src/matching.rs @@ -0,0 +1,27 @@ +use super::*; + +/// Trait for matching a function. +pub trait Matches { + /// Determines whether a function is a match. + /// + /// # Parameters + /// - `ext_id` - The specified chain extension identifier. + /// - `func_id` - The specified function identifier. + fn matches(ext_id: u16, func_id: u16) -> bool; +} + +/// Matches on an extension and function identifier. +pub struct Equals(PhantomData<(E, F)>); +impl, F: Get> Matches for Equals { + fn matches(ext_id: u16, func_id: u16) -> bool { + ext_id == E::get() && func_id == F::get() + } +} + +/// Matches on a function identifier only. +pub struct FunctionId(PhantomData); +impl> Matches for FunctionId { + fn matches(_ext_id: u16, func_id: u16) -> bool { + func_id == T::get() + } +} diff --git a/extension/src/reboot.rs b/extension/src/reboot.rs deleted file mode 100644 index b00b2b7b..00000000 --- a/extension/src/reboot.rs +++ /dev/null @@ -1,300 +0,0 @@ -use crate::{DECODING_FAILED_ERROR, UNKNOWN_CALL_ERROR}; -use codec::Decode as _; -use frame_support::{ - dispatch::{GetDispatchInfo, PostDispatchInfo, RawOrigin}, - ensure, - traits::{Contains, OriginTrait}, - weights::Weight, -}; -pub use functions::{ - Decode, Decodes, DispatchCall, Equals, Function, FunctionId, Matches, Processor, ReadState, - Readable, -}; -use pallet_contracts::chain_extension::{ - ChainExtension, InitState, Result, RetVal, RetVal::Converging, -}; -pub use pallet_contracts::chain_extension::{Environment, Ext, State}; -use sp_core::Get; -use sp_runtime::traits::Dispatchable; -use std::marker::PhantomData; - -type Schedule = ::Schedule; - -/// A configurable chain extension. -#[derive(Default)] -pub struct Extension(PhantomData); -impl ChainExtension for Extension -where - Runtime: pallet_contracts::Config - + frame_system::Config< - RuntimeCall: GetDispatchInfo + Dispatchable, - >, - Config: self::Config> + 'static, -{ - /// Call the chain extension logic. - /// - /// # Parameters - /// - `env`: Access to the remaining arguments and the execution environment. - fn call>(&mut self, env: Environment) -> Result { - log::debug!(target: Config::LOG_TARGET, " extension called "); - let mut env = env.buf_in_buf_out(); - // Charge weight for making a call from a contract to the runtime. - // `debug_message` weight is a good approximation of the additional overhead of going from contract layer to substrate layer. - // reference: https://github.com/paritytech/ink-examples/blob/b8d2caa52cf4691e0ddd7c919e4462311deb5ad0/psp22-extension/runtime/psp22-extension-example.rs#L236 - env.charge_weight(Schedule::::get().host_fn_weights.debug_message)?; - // Execute the function - Config::Functions::execute(env) - } -} - -/// Trait for configuration of the chain extension. -pub trait Config { - /// The function(s) available with the chain extension. - type Functions: Function; - - /// The log target. - const LOG_TARGET: &'static str; -} - -mod functions { - use super::*; - pub use decoding::{Decode, Decodes, Processor}; - pub use matching::{Equals, FunctionId, Matches}; - use pallet_contracts::chain_extension::{BufIn, BufOut}; - - /// A chain extension function. - pub trait Function { - /// The configuration of the contracts module. - type Config: pallet_contracts::Config; - - /// Executes the function. - /// - /// # Parameters - /// - `env` - The current execution environment. - fn execute, S: BufIn + BufOut>( - env: Environment, - ) -> Result; - } - - #[impl_trait_for_tuples::impl_for_tuples(1, 3)] - #[tuple_types_custom_trait_bound(Function + Matches)] - impl Function for Tuple { - for_tuples!( where #( Tuple: Function )* ); - type Config = Runtime; - fn execute, S: BufIn + BufOut>( - env: Environment, - ) -> Result { - // Attempts to match a specified extension/function identifier to its corresponding function, as configured by the runtime. - for_tuples!( #( - if Tuple::matches(env.ext_id(), env.func_id()) { - return Tuple::execute(env) - } - )* ); - - // Otherwise returns `DispatchError::Other` indicating an unmatched request. - // TODO: improve error so we can determine if its an invalid function vs unknown runtime call/read - Err(UNKNOWN_CALL_ERROR) - } - } - - /// A function for dispatching a runtime call. - pub struct DispatchCall(PhantomData<(C, D, M, F)>); - impl< - Config: pallet_contracts::Config - + frame_system::Config< - RuntimeCall: GetDispatchInfo + Dispatchable, - >, - Decoder: Decode::RuntimeCall>>, - Matcher: Matches, - Filter: Contains<::RuntimeCall> + 'static, - > Function for DispatchCall - { - /// The configuration of the contracts module. - type Config = Config; - - /// Executes the function. - /// - /// # Parameters - /// - `env` - The current execution environment. - fn execute, S: BufIn + BufOut>( - mut env: Environment, - ) -> Result { - const LOG_PREFIX: &str = " dispatch |"; - - // Decode runtime call. - let call = Decoder::decode(&mut env)?.into(); - // TODO: log::debug!(target:LOG_TARGET, "{} Inputted RuntimeCall: {:?}", LOG_PREFIX, call); - // Charge weight before dispatch. - let dispatch_info = call.get_dispatch_info(); - let charged = env.charge_weight(dispatch_info.weight)?; - // TODO: log::trace! dispatch_info and charged - // Contract is the origin by default. - let mut origin: Config::RuntimeOrigin = - RawOrigin::Signed(env.ext().address().clone()).into(); - // Ensure call allowed. - origin.add_filter(Filter::contains); - // Dispatch call. - let result = call.dispatch(origin); - // TODO: log::debug!(target:LOG_TARGET, "{} result, actual weight: {:?}", LOG_PREFIX, info.actual_weight); - // Adjust weight. - let weight = frame_support::dispatch::extract_actual_weight(&result, &dispatch_info); - env.adjust_weight(charged, weight); - // TODO: conversion of error to 'versioned' status code - result.map(|_| Converging(0)).map_err(|e| e.error) - } - } - - impl Matches for DispatchCall { - fn matches(ext_id: u16, func_id: u16) -> bool { - M::matches(ext_id, func_id) - } - } - - /// A function for reading runtime state. - pub struct ReadState(PhantomData<(C, R, D, M, F)>); - impl< - Config: pallet_contracts::Config, - Read: Readable, - Decoder: Decode>, - Matcher: Matches, - Filter: Contains, - > Function for ReadState - { - /// The configuration of the contracts module. - type Config = Config; - - /// Executes the function. - /// - /// # Parameters - /// - `env` - The current execution environment. - fn execute, S: BufIn + BufOut>( - mut env: Environment, - ) -> Result { - const LOG_PREFIX: &str = " dispatch |"; - - // Decode runtime read - let read = Decoder::decode(&mut env)?.into(); - // TODO: log::debug!(target:LOG_TARGET, "{} Inputted RuntimeRead: {:?}", LOG_PREFIX, read); - // Charge weight before read - let _charged = env.charge_weight(read.weight())?; - // TODO: log::trace! charged - // Ensure read allowed - ensure!(Filter::contains(&read), frame_system::Error::::CallFiltered); - let result = read.read(); - // TODO: log::debug!(target:LOG_TARGET, "{} result: {:?}", LOG_PREFIX, result); - // TODO: check parameters (allow_skip, weight_per_byte) - env.write( - &result, - false, - Some(Schedule::::get().host_fn_weights.input_per_byte), - )?; - // TODO: conversion of error to 'versioned' status code - Ok(Converging(0)) - } - } - - impl Matches for ReadState { - fn matches(ext_id: u16, func_id: u16) -> bool { - M::matches(ext_id, func_id) - } - } - - /// Trait to be implemented for type handling a read of runtime state - pub trait Readable { - /// Determines the weight of the read, used to charge the appropriate weight before the read is performed. - fn weight(&self) -> Weight; - - /// Performs the read and returns the result. - fn read(self) -> Vec; - } - - mod decoding { - use super::*; - use pallet_contracts::chain_extension::{BufIn, State}; - use sp_runtime::DispatchError; - - /// Trait for decoding data read from contract memory. - pub trait Decode { - /// The output type to be decoded. - type Output: codec::Decode; - /// An optional processor, for performing any additional processing before decoding. - type Processor: Processor; - - /// The error to return if decoding fails. - const ERROR: DispatchError = DECODING_FAILED_ERROR; - - /// Decodes data read from contract memory. - /// - /// # Parameters - /// - `env` - The current execution environment. - fn decode(env: &mut Environment) -> Result { - // Charge appropriate weight, based on input length, prior to decoding. - // reference: https://github.com/paritytech/polkadot-sdk/blob/117a9433dac88d5ac00c058c9b39c511d47749d2/substrate/frame/contracts/src/wasm/runtime.rs#L267 - let len = env.in_len(); - env.charge_weight( - Schedule::::get() - .host_fn_weights - .return_per_byte - .saturating_mul(len.into()), - )?; - // Read encoded input supplied by contract for buffer. - let mut input = env.read(len)?; - // Perform any additional processing required. Any implementation is expected to charge weight as appropriate. - Self::Processor::process(&mut input, env); - // Finally decode and return. - Self::Output::decode(&mut &input[..]).map_err(|_| Self::ERROR) - } - } - - /// Default implementation for decoding data read from contract memory. - pub struct Decodes(PhantomData<(O, P)>); - impl Decode for Decodes { - type Output = Output; - type Processor = Processor_; - } - - /// Trait for processing a value based on additional information available from the environment. - pub trait Processor { - /// Processes the provided value. - /// - /// # Parameters - /// - `value` - The value to be processed. - /// - `env` - The current execution environment. - fn process(value: &mut Vec, env: &mut Environment); - } - - impl Processor for () { - fn process(_value: &mut Vec, _env: &mut Environment) {} - } - } - - mod matching { - use super::*; - - /// Trait for matching a function. - pub trait Matches { - /// Determines whether a function is a match. - /// - /// # Parameters - /// - `ext_id` - The specified chain extension identifier. - /// - `func_id` - The specified function identifier. - fn matches(ext_id: u16, func_id: u16) -> bool; - } - - /// Matches on an extension and function identifier. - pub struct Equals(PhantomData<(E, F)>); - impl, F: Get> Matches for Equals { - fn matches(ext_id: u16, func_id: u16) -> bool { - ext_id == E::get() && func_id == F::get() - } - } - - /// Matches on a function identifier only. - pub struct FunctionId(PhantomData); - impl> Matches for FunctionId { - fn matches(_ext_id: u16, func_id: u16) -> bool { - func_id == T::get() - } - } - } -} diff --git a/pallets/api/src/extension.rs b/pallets/api/src/extension.rs index 6cb8fc8a..0be3cc43 100644 --- a/pallets/api/src/extension.rs +++ b/pallets/api/src/extension.rs @@ -1,9 +1,9 @@ use core::marker::PhantomData; use frame_support::traits::Get; -pub use pop_chain_extension::reboot::{Config, DispatchCall, ReadState, Readable}; -use pop_chain_extension::reboot::{Decodes, Environment, Ext, Matches, Processor, State}; +pub use pop_chain_extension::{Config, DispatchCall, ReadState, Readable}; +use pop_chain_extension::{Decodes, Environment, Ext, Matches, Processor, State}; -pub type Extension = pop_chain_extension::reboot::Extension; +pub type Extension = pop_chain_extension::Extension; /// Decodes output by prepending bytes from ext_id() + func_id() pub type DecodesAs = Decodes; @@ -27,8 +27,8 @@ impl Processor for Prepender { } /// Matches on the first byte of a function identifier only. -pub struct FirstByteOfFunctionId(PhantomData); -impl> Matches for FirstByteOfFunctionId { +pub struct IdentifiedByFirstByteOfFunctionId(PhantomData); +impl> Matches for IdentifiedByFirstByteOfFunctionId { fn matches(_ext_id: u16, func_id: u16) -> bool { let bytes = func_id.to_le_bytes(); bytes[0] == T::get() diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index 3a594dd9..835504ff 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -1,12 +1,17 @@ use crate::{ config::assets::TrustBackedAssetsInstance, fungibles, Runtime, RuntimeCall, RuntimeEvent, }; -use codec::{Decode, Encode, MaxEncodedLen}; -use pop_chain_extension::{CallFilter, ReadState}; -use sp_std::vec::Vec; +use codec::Decode; +use cumulus_primitives_core::Weight; +use filtering::*; +use frame_support::traits::Contains; +use pallet_api::extension::*; +pub(crate) use pallet_api::Extension; +use sp_core::ConstU8; +use versioning::*; /// A query of runtime state. -#[derive(Encode, Decode, Debug, MaxEncodedLen)] +#[derive(Decode, Debug)] #[repr(u8)] pub enum RuntimeRead { /// Fungible token queries. @@ -14,94 +19,96 @@ pub enum RuntimeRead { Fungibles(fungibles::Read), } -/// A struct that implement requirements for the Pop API chain extension. -#[derive(Default)] -pub struct Extension; -impl ReadState for Extension { - type StateQuery = RuntimeRead; - - fn contains(c: &Self::StateQuery) -> bool { - use fungibles::Read::*; - matches!( - c, - RuntimeRead::Fungibles( - TotalSupply(..) - | BalanceOf { .. } | Allowance { .. } - | TokenName(..) | TokenSymbol(..) - | TokenDecimals(..) | AssetExists(..) - ) - ) +impl Readable for RuntimeRead { + /// Determines the weight of the read, used to charge the appropriate weight before the read is performed. + fn weight(&self) -> Weight { + // TODO: defer to relevant pallet - e.g. RuntimeRead::Fungibles(key) => fungibles::Pallet::read_weight(key), + ::DbWeight::get().reads(1_u64) } - fn read(read: RuntimeRead) -> Vec { - match read { + /// Performs the read and returns the result. + fn read(self) -> Vec { + match self { RuntimeRead::Fungibles(key) => fungibles::Pallet::read_state(key), } } } -impl CallFilter for Extension { - type Call = RuntimeCall; - - fn contains(c: &Self::Call) -> bool { - use fungibles::Call::*; - matches!( - c, - RuntimeCall::Fungibles( - transfer { .. } - | transfer_from { .. } - | approve { .. } | increase_allowance { .. } - | decrease_allowance { .. } - | create { .. } | set_metadata { .. } - | start_destroy { .. } - | clear_metadata { .. } - | mint { .. } | burn { .. } - ) - ) - } -} - impl fungibles::Config for Runtime { type RuntimeEvent = RuntimeEvent; type AssetsInstance = TrustBackedAssetsInstance; type WeightInfo = fungibles::weights::SubstrateWeight; } -pub(crate) mod reboot { - use crate::{config::api::RuntimeRead, fungibles, Runtime, RuntimeCall}; - use codec::Decode; - use cumulus_primitives_core::Weight; - use frame_support::traits::Contains; - use pallet_api::extension::*; - use sp_core::ConstU8; +#[derive(Default)] +pub struct Config; +impl pallet_api::extension::Config for Config { + /// Functions used by the Pop API. + /// + /// Each function corresponds to specific functionality provided by the API, facilitating the + /// interaction between smart contracts and the runtime. + type Functions = ( + // Dispatching calls + DispatchCall< + Runtime, + DecodesAs, + IdentifiedByFirstByteOfFunctionId>, + Filter, + >, + // Reading state + ReadState< + Runtime, + RuntimeRead, + DecodesAs, + IdentifiedByFirstByteOfFunctionId>, + Filter, + >, + ); + + const LOG_TARGET: &'static str = "pop-api::extension"; +} + +mod filtering { + use super::*; - #[derive(Default)] - pub struct Config; - impl pallet_api::extension::Config for Config { - /// Functions used by the Pop API - /// . - /// Each function corresponds to specific functionality provided by the API, facilitating the - // interaction between smart contracts and the runtime. - type Functions = ( - // Dispatching calls - DispatchCall< - Runtime, - DecodesAs, - FirstByteOfFunctionId>, - Filter, - >, - // Reading state - ReadState< - Runtime, - RuntimeRead, - DecodesAs, - FirstByteOfFunctionId>, - Filter, - >, - ); + pub struct Filter; + + impl Contains for Filter { + fn contains(c: &RuntimeCall) -> bool { + use fungibles::Call::*; + matches!( + c, + RuntimeCall::Fungibles( + transfer { .. } + | transfer_from { .. } | approve { .. } + | increase_allowance { .. } + | decrease_allowance { .. } + | create { .. } | set_metadata { .. } + | start_destroy { .. } | clear_metadata { .. } + | mint { .. } | burn { .. } + ) + ) + } + } - const LOG_TARGET: &'static str = "pop-api::extension"; + impl Contains for Filter { + fn contains(r: &RuntimeRead) -> bool { + use fungibles::Read::*; + matches!( + r, + RuntimeRead::Fungibles( + TotalSupply(..) + | BalanceOf { .. } | Allowance { .. } + | TokenName(..) | TokenSymbol(..) + | TokenDecimals(..) | AssetExists(..) + ) + ) + } } +} + +mod versioning { + use super::*; /// Versioned runtime calls. #[derive(Decode, Debug)] @@ -136,54 +143,4 @@ pub(crate) mod reboot { } } } - - impl Readable for RuntimeRead { - /// Determines the weight of the read, used to charge the appropriate weight before the read is performed. - fn weight(&self) -> Weight { - // TODO: defer to relevant pallet - e.g. RuntimeRead::Fungibles(key) => fungibles::Pallet::read_weight(key), - ::DbWeight::get().reads(1_u64) - } - - /// Performs the read and returns the result. - fn read(self) -> Vec { - match self { - RuntimeRead::Fungibles(key) => fungibles::Pallet::read_state(key), - } - } - } - - pub struct Filter; - - impl Contains for Filter { - fn contains(c: &RuntimeCall) -> bool { - use fungibles::Call::*; - matches!( - c, - RuntimeCall::Fungibles( - transfer { .. } - | transfer_from { .. } | approve { .. } - | increase_allowance { .. } - | decrease_allowance { .. } - | create { .. } | set_metadata { .. } - | start_destroy { .. } | clear_metadata { .. } - | mint { .. } | burn { .. } - ) - ) - } - } - - impl Contains for Filter { - fn contains(r: &RuntimeRead) -> bool { - use crate::fungibles::Read::*; - matches!( - r, - RuntimeRead::Fungibles( - TotalSupply(..) - | BalanceOf { .. } | Allowance { .. } - | TokenName(..) | TokenSymbol(..) - | TokenDecimals(..) | AssetExists(..) - ) - ) - } - } } diff --git a/runtime/devnet/src/config/contracts.rs b/runtime/devnet/src/config/contracts.rs index b2878f52..67a2f33c 100644 --- a/runtime/devnet/src/config/contracts.rs +++ b/runtime/devnet/src/config/contracts.rs @@ -1,4 +1,4 @@ -use super::api::reboot::Config; +use super::api::{self, Config}; use crate::{ deposit, Balance, Balances, BalancesCall, Perbill, Runtime, RuntimeCall, RuntimeEvent, RuntimeHoldReason, Timestamp, @@ -64,7 +64,7 @@ impl pallet_contracts::Config for Runtime { type CallStack = [pallet_contracts::Frame; 23]; type WeightPrice = pallet_transaction_payment::Pallet; type WeightInfo = pallet_contracts::weights::SubstrateWeight; - type ChainExtension = pallet_api::Extension; + type ChainExtension = api::Extension; type Schedule = Schedule; type AddressGenerator = pallet_contracts::DefaultAddressGenerator; // This node is geared towards development and testing of contracts. From 6b7553d3f8c9fe2dfaa0df576449418bf389d114 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 20 Aug 2024 03:07:15 +0100 Subject: [PATCH 56/76] fix: align with existing encoding convention --- pallets/api/src/extension.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/api/src/extension.rs b/pallets/api/src/extension.rs index 0be3cc43..0914d638 100644 --- a/pallets/api/src/extension.rs +++ b/pallets/api/src/extension.rs @@ -14,7 +14,7 @@ impl Processor for Prepender { fn process(value: &mut Vec, env: &mut Environment) { // TODO: revisit the ordering based on specced standard // Resolve version, pallet and call index from environment - let version = env.func_id().to_le_bytes()[1]; + let version = env.func_id().to_le_bytes()[0]; let (pallet_index, call_index) = { let bytes = env.ext_id().to_le_bytes(); (bytes[0], bytes[1]) @@ -31,6 +31,6 @@ pub struct IdentifiedByFirstByteOfFunctionId(PhantomData); impl> Matches for IdentifiedByFirstByteOfFunctionId { fn matches(_ext_id: u16, func_id: u16) -> bool { let bytes = func_id.to_le_bytes(); - bytes[0] == T::get() + bytes[1] == T::get() } } From 65387226fca3933f2ed918fb5b2044070f23641a Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 20 Aug 2024 11:13:43 +0100 Subject: [PATCH 57/76] refactor: improve logging --- Cargo.lock | 1 + extension/src/decoding.rs | 27 ++++++++++++++------ extension/src/functions.rs | 42 +++++++++++++++++--------------- extension/src/lib.rs | 14 +++++++++-- pallets/api/Cargo.toml | 1 + pallets/api/src/extension.rs | 24 +++++++++++++++--- runtime/devnet/src/config/api.rs | 8 +++--- 7 files changed, 81 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 76cf4b5b..fc530082 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6327,6 +6327,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "log", "pallet-assets", "pallet-balances", "parity-scale-codec", diff --git a/extension/src/decoding.rs b/extension/src/decoding.rs index da4d37b7..6ea7e998 100644 --- a/extension/src/decoding.rs +++ b/extension/src/decoding.rs @@ -11,6 +11,8 @@ pub trait Decode { /// The error to return if decoding fails. const ERROR: DispatchError = DECODING_FAILED_ERROR; + /// The log target. + const LOG_TARGET: &'static str; /// Decodes data read from contract memory. /// @@ -20,14 +22,15 @@ pub trait Decode { // Charge appropriate weight, based on input length, prior to decoding. // reference: https://github.com/paritytech/polkadot-sdk/blob/117a9433dac88d5ac00c058c9b39c511d47749d2/substrate/frame/contracts/src/wasm/runtime.rs#L267 let len = env.in_len(); - env.charge_weight( - Schedule::::get() - .host_fn_weights - .return_per_byte - .saturating_mul(len.into()), - )?; + let weight = Schedule::::get() + .host_fn_weights + .return_per_byte + .saturating_mul(len.into()); + env.charge_weight(weight)?; + log::debug!(target: Self::LOG_TARGET, "pre-decode weight charged: len={len}, weight={weight}"); // Read encoded input supplied by contract for buffer. let mut input = env.read(len)?; + log::debug!(target: Self::LOG_TARGET, "input read: input={input:?}"); // Perform any additional processing required. Any implementation is expected to charge weight as appropriate. Self::Processor::process(&mut input, env); // Finally decode and return. @@ -36,14 +39,20 @@ pub trait Decode { } /// Default implementation for decoding data read from contract memory. -pub struct Decodes(PhantomData<(O, P)>); -impl Decode for Decodes { +pub struct Decodes(PhantomData<(O, P, L)>); +impl Decode + for Decodes +{ type Output = Output; type Processor = Processor_; + const LOG_TARGET: &'static str = Logger::LOG_TARGET; } /// Trait for processing a value based on additional information available from the environment. pub trait Processor { + /// The log target. + const LOG_TARGET: &'static str; + /// Processes the provided value. /// /// # Parameters @@ -53,5 +62,7 @@ pub trait Processor { } impl Processor for () { + const LOG_TARGET: &'static str = ""; + fn process(_value: &mut Vec, _env: &mut Environment) {} } diff --git a/extension/src/functions.rs b/extension/src/functions.rs index ae7697b0..b75b229b 100644 --- a/extension/src/functions.rs +++ b/extension/src/functions.rs @@ -1,4 +1,5 @@ use super::*; +use core::fmt::Debug; pub use decoding::{Decode, Decodes, Processor}; pub use matching::{Equals, FunctionId, Matches}; use pallet_contracts::chain_extension::{BufIn, BufOut}; @@ -39,7 +40,7 @@ impl Function for Tuple { } /// A function for dispatching a runtime call. -pub struct DispatchCall(PhantomData<(C, D, M, F)>); +pub struct DispatchCall(PhantomData<(C, D, M, F, L)>); impl< Config: pallet_contracts::Config + frame_system::Config< @@ -48,7 +49,8 @@ impl< Decoder: Decode::RuntimeCall>>, Matcher: Matches, Filter: Contains<::RuntimeCall> + 'static, - > Function for DispatchCall + Logger: LogTarget, + > Function for DispatchCall { /// The configuration of the contracts module. type Config = Config; @@ -60,46 +62,48 @@ impl< fn execute, S: BufIn + BufOut>( mut env: Environment, ) -> Result { - const LOG_PREFIX: &str = " dispatch |"; - // Decode runtime call. let call = Decoder::decode(&mut env)?.into(); - // TODO: log::debug!(target:LOG_TARGET, "{} Inputted RuntimeCall: {:?}", LOG_PREFIX, call); + log::debug!(target: Logger::LOG_TARGET, "decoded: call={call:?}"); // Charge weight before dispatch. let dispatch_info = call.get_dispatch_info(); + log::debug!(target: Logger::LOG_TARGET, "pre-dispatch info: dispatch_info={dispatch_info:?}"); let charged = env.charge_weight(dispatch_info.weight)?; - // TODO: log::trace! dispatch_info and charged + log::debug!(target: Logger::LOG_TARGET, "pre-dispatch weight charged: charged={charged:?}"); // Contract is the origin by default. - let mut origin: Config::RuntimeOrigin = - RawOrigin::Signed(env.ext().address().clone()).into(); + let origin = RawOrigin::Signed(env.ext().address().clone()); + log::debug!(target: Logger::LOG_TARGET, "contract origin: origin={origin:?}"); + let mut origin: Config::RuntimeOrigin = origin.into(); // Ensure call allowed. origin.add_filter(Filter::contains); // Dispatch call. let result = call.dispatch(origin); - // TODO: log::debug!(target:LOG_TARGET, "{} result, actual weight: {:?}", LOG_PREFIX, info.actual_weight); + log::debug!(target: Logger::LOG_TARGET, "dispatched: result={result:?}"); // Adjust weight. let weight = frame_support::dispatch::extract_actual_weight(&result, &dispatch_info); env.adjust_weight(charged, weight); + log::debug!(target: Logger::LOG_TARGET, "weight adjusted: weight={weight:?}"); // TODO: conversion of error to 'versioned' status code result.map(|_| Converging(0)).map_err(|e| e.error) } } -impl Matches for DispatchCall { +impl Matches for DispatchCall { fn matches(ext_id: u16, func_id: u16) -> bool { M::matches(ext_id, func_id) } } /// A function for reading runtime state. -pub struct ReadState(PhantomData<(C, R, D, M, F)>); +pub struct ReadState(PhantomData<(C, R, D, M, F, L)>); impl< Config: pallet_contracts::Config, - Read: Readable, + Read: Readable + Debug, Decoder: Decode>, Matcher: Matches, Filter: Contains, - > Function for ReadState + Logger: LogTarget, + > Function for ReadState { /// The configuration of the contracts module. type Config = Config; @@ -111,18 +115,16 @@ impl< fn execute, S: BufIn + BufOut>( mut env: Environment, ) -> Result { - const LOG_PREFIX: &str = " dispatch |"; - // Decode runtime read let read = Decoder::decode(&mut env)?.into(); - // TODO: log::debug!(target:LOG_TARGET, "{} Inputted RuntimeRead: {:?}", LOG_PREFIX, read); + log::debug!(target: Logger::LOG_TARGET, "decoded: read={read:?}"); // Charge weight before read - let _charged = env.charge_weight(read.weight())?; - // TODO: log::trace! charged + let charged = env.charge_weight(read.weight())?; + log::trace!(target: Logger::LOG_TARGET, "pre-read weight charged: charged={charged:?}"); // Ensure read allowed ensure!(Filter::contains(&read), frame_system::Error::::CallFiltered); let result = read.read(); - // TODO: log::debug!(target:LOG_TARGET, "{} result: {:?}", LOG_PREFIX, result); + log::debug!(target: Logger::LOG_TARGET, "read: result={result:?}"); // TODO: check parameters (allow_skip, weight_per_byte) env.write(&result, false, Some(Schedule::::get().host_fn_weights.input_per_byte))?; // TODO: conversion of error to 'versioned' status code @@ -130,7 +132,7 @@ impl< } } -impl Matches for ReadState { +impl Matches for ReadState { fn matches(ext_id: u16, func_id: u16) -> bool { M::matches(ext_id, func_id) } diff --git a/extension/src/lib.rs b/extension/src/lib.rs index 6dafe868..a92f5624 100644 --- a/extension/src/lib.rs +++ b/extension/src/lib.rs @@ -46,7 +46,7 @@ where /// # Parameters /// - `env`: Access to the remaining arguments and the execution environment. fn call>(&mut self, env: Environment) -> Result { - log::debug!(target: Config::LOG_TARGET, " extension called "); + log::trace!(target: Config::LOG_TARGET, "extension called"); let mut env = env.buf_in_buf_out(); // Charge weight for making a call from a contract to the runtime. // `debug_message` weight is a good approximation of the additional overhead of going from contract layer to substrate layer. @@ -66,7 +66,7 @@ pub trait Config { const LOG_TARGET: &'static str; } -/// Trait to be implemented for type handling a read of runtime state +/// Trait to be implemented for type handling a read of runtime state. pub trait Readable { /// Determines the weight of the read, used to charge the appropriate weight before the read is performed. fn weight(&self) -> Weight; @@ -74,3 +74,13 @@ pub trait Readable { /// Performs the read and returns the result. fn read(self) -> Vec; } + +/// Trait to enable specification of a log target. +pub trait LogTarget { + /// The log target. + const LOG_TARGET: &'static str; +} + +impl LogTarget for () { + const LOG_TARGET: &'static str = "pop-chain-extension"; +} diff --git a/pallets/api/Cargo.toml b/pallets/api/Cargo.toml index c03c68f5..32085c5c 100644 --- a/pallets/api/Cargo.toml +++ b/pallets/api/Cargo.toml @@ -11,6 +11,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec.workspace = true +log.workspace = true scale-info.workspace = true # Local diff --git a/pallets/api/src/extension.rs b/pallets/api/src/extension.rs index 0914d638..e7ee826a 100644 --- a/pallets/api/src/extension.rs +++ b/pallets/api/src/extension.rs @@ -1,16 +1,21 @@ use core::marker::PhantomData; use frame_support::traits::Get; pub use pop_chain_extension::{Config, DispatchCall, ReadState, Readable}; -use pop_chain_extension::{Decodes, Environment, Ext, Matches, Processor, State}; +use pop_chain_extension::{Decodes, Environment, Ext, LogTarget, Matches, Processor, State}; -pub type Extension = pop_chain_extension::Extension; +/// The logging target for the chain extension. +pub const LOG_TARGET: &str = "pop-api::extension"; +/// The chain extension used by the API. +pub type Extension = pop_chain_extension::Extension; /// Decodes output by prepending bytes from ext_id() + func_id() -pub type DecodesAs = Decodes; +pub type DecodesAs = Decodes; /// Prepends bytes from ext_id() + func_id() to prefix the encoded input bytes to determine the versioned output pub struct Prepender; impl Processor for Prepender { + const LOG_TARGET: &'static str = "pop-api::extension::processor"; + fn process(value: &mut Vec, env: &mut Environment) { // TODO: revisit the ordering based on specced standard // Resolve version, pallet and call index from environment @@ -23,6 +28,7 @@ impl Processor for Prepender { value.insert(0, version); value.insert(1, pallet_index); value.insert(2, call_index); + log::debug!(target: Self::LOG_TARGET, "prepender: version={version}, module={pallet_index}, call={call_index}") } } @@ -34,3 +40,15 @@ impl> Matches for IdentifiedByFirstByteOfFunctionId { bytes[1] == T::get() } } + +/// A log target for dispatched calls. +pub struct DispatchCallLogTarget; +impl LogTarget for DispatchCallLogTarget { + const LOG_TARGET: &'static str = "pop-api::extension::dispatch"; +} + +/// A log target for state reads. +pub struct ReadStateLogTarget; +impl LogTarget for ReadStateLogTarget { + const LOG_TARGET: &'static str = "pop-api::extension::read-state"; +} diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index 835504ff..b397c64f 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -51,21 +51,23 @@ impl pallet_api::extension::Config for Config { // Dispatching calls DispatchCall< Runtime, - DecodesAs, + DecodesAs, IdentifiedByFirstByteOfFunctionId>, Filter, + DispatchCallLogTarget, >, // Reading state ReadState< Runtime, RuntimeRead, - DecodesAs, + DecodesAs, IdentifiedByFirstByteOfFunctionId>, Filter, + ReadStateLogTarget, >, ); - const LOG_TARGET: &'static str = "pop-api::extension"; + const LOG_TARGET: &'static str = LOG_TARGET; } mod filtering { From c1b183a5e57d406ef926933a8374d18f536a2478 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 20 Aug 2024 12:45:37 +0100 Subject: [PATCH 58/76] refactor: improve matching api --- extension/src/functions.rs | 10 +++++----- extension/src/matching.rs | 15 +++++++-------- pallets/api/src/extension.rs | 4 ++-- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/extension/src/functions.rs b/extension/src/functions.rs index b75b229b..e87f9bc6 100644 --- a/extension/src/functions.rs +++ b/extension/src/functions.rs @@ -28,7 +28,7 @@ impl Function for Tuple { ) -> Result { // Attempts to match a specified extension/function identifier to its corresponding function, as configured by the runtime. for_tuples!( #( - if Tuple::matches(env.ext_id(), env.func_id()) { + if Tuple::matches(&env) { return Tuple::execute(env) } )* ); @@ -89,8 +89,8 @@ impl< } impl Matches for DispatchCall { - fn matches(ext_id: u16, func_id: u16) -> bool { - M::matches(ext_id, func_id) + fn matches(env: &Environment) -> bool { + M::matches(env) } } @@ -133,7 +133,7 @@ impl< } impl Matches for ReadState { - fn matches(ext_id: u16, func_id: u16) -> bool { - M::matches(ext_id, func_id) + fn matches(env: &Environment) -> bool { + M::matches(env) } } diff --git a/extension/src/matching.rs b/extension/src/matching.rs index 95fe1ca3..3b3aa6f4 100644 --- a/extension/src/matching.rs +++ b/extension/src/matching.rs @@ -5,23 +5,22 @@ pub trait Matches { /// Determines whether a function is a match. /// /// # Parameters - /// - `ext_id` - The specified chain extension identifier. - /// - `func_id` - The specified function identifier. - fn matches(ext_id: u16, func_id: u16) -> bool; + /// - `env` - The current execution environment. + fn matches(env: &Environment) -> bool; } /// Matches on an extension and function identifier. pub struct Equals(PhantomData<(E, F)>); -impl, F: Get> Matches for Equals { - fn matches(ext_id: u16, func_id: u16) -> bool { - ext_id == E::get() && func_id == F::get() +impl, FuncId: Get> Matches for Equals { + fn matches(env: &Environment) -> bool { + env.ext_id() == ExtId::get() && env.func_id() == FuncId::get() } } /// Matches on a function identifier only. pub struct FunctionId(PhantomData); impl> Matches for FunctionId { - fn matches(_ext_id: u16, func_id: u16) -> bool { - func_id == T::get() + fn matches(env: &Environment) -> bool { + env.func_id() == T::get() } } diff --git a/pallets/api/src/extension.rs b/pallets/api/src/extension.rs index e7ee826a..48cd4c11 100644 --- a/pallets/api/src/extension.rs +++ b/pallets/api/src/extension.rs @@ -35,8 +35,8 @@ impl Processor for Prepender { /// Matches on the first byte of a function identifier only. pub struct IdentifiedByFirstByteOfFunctionId(PhantomData); impl> Matches for IdentifiedByFirstByteOfFunctionId { - fn matches(_ext_id: u16, func_id: u16) -> bool { - let bytes = func_id.to_le_bytes(); + fn matches(env: &Environment) -> bool { + let bytes = env.func_id().to_le_bytes(); bytes[1] == T::get() } } From 6def916f858c9c8ce077d56347d3674a9ebc57be Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 20 Aug 2024 13:48:36 +0100 Subject: [PATCH 59/76] refactor: error conversion --- extension/src/decoding.rs | 9 +- extension/src/error.rs | 59 ----- extension/src/functions.rs | 17 +- extension/src/lib.rs | 35 ++- extension/src/v0.rs | 298 ----------------------- pallets/api/src/extension.rs | 4 +- runtime/devnet/src/config/api.rs | 401 ++++++++++++++++++++++++++++++- 7 files changed, 443 insertions(+), 380 deletions(-) delete mode 100644 extension/src/error.rs delete mode 100644 extension/src/v0.rs diff --git a/extension/src/decoding.rs b/extension/src/decoding.rs index 6ea7e998..cb9aade5 100644 --- a/extension/src/decoding.rs +++ b/extension/src/decoding.rs @@ -10,7 +10,7 @@ pub trait Decode { type Processor: Processor; /// The error to return if decoding fails. - const ERROR: DispatchError = DECODING_FAILED_ERROR; + const ERROR: DispatchError; /// The log target. const LOG_TARGET: &'static str; @@ -39,12 +39,13 @@ pub trait Decode { } /// Default implementation for decoding data read from contract memory. -pub struct Decodes(PhantomData<(O, P, L)>); -impl Decode - for Decodes +pub struct Decodes(PhantomData<(O, E, P, L)>); +impl Decode + for Decodes { type Output = Output; type Processor = Processor_; + const ERROR: DispatchError = Error::ERROR; const LOG_TARGET: &'static str = Logger::LOG_TARGET; } diff --git a/extension/src/error.rs b/extension/src/error.rs deleted file mode 100644 index a5da0c0a..00000000 --- a/extension/src/error.rs +++ /dev/null @@ -1,59 +0,0 @@ -use codec::Encode; -use sp_runtime::DispatchError; - -pub(crate) const DECODING_FAILED_ERROR: DispatchError = DispatchError::Other("DecodingFailed"); -// TODO: issue #93, we can also encode the `pop_primitives::Error::UnknownCall` which means we do use -// `Error` in the runtime and it should stay in primitives. Perhaps issue #91 will also influence -// here. Should be looked at together. -const DECODING_FAILED_ERROR_ENCODED: [u8; 4] = [255u8, 0, 0, 0]; -pub(crate) const UNKNOWN_CALL_ERROR: DispatchError = DispatchError::Other("UnknownCall"); -// TODO: see above. -const UNKNOWN_CALL_ERROR_ENCODED: [u8; 4] = [254u8, 0, 0, 0]; - -/// Converts a `DispatchError` to a `u32` status code based on the version of the API the contract uses. -/// The contract calling the chain extension can optionally convert the status code to the descriptive `Error`. -/// -/// For `Error` see `pop_primitives::::error::Error`. -/// -/// The error encoding can vary per version, allowing for flexible and backward-compatible error handling. -/// As a result, contracts maintain compatibility across different versions of the runtime. -/// -/// # Parameters -/// -/// - `error`: The `DispatchError` encountered during contract execution. -/// - `version`: The version of the chain extension, used to determine the known errors. -pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { - let mut encoded_error: [u8; 4] = match error { - // "UnknownCall" and "DecodingFailed" are mapped to specific errors in the API and will - // never change. - UNKNOWN_CALL_ERROR => UNKNOWN_CALL_ERROR_ENCODED, - DECODING_FAILED_ERROR => DECODING_FAILED_ERROR_ENCODED, - _ => { - let mut encoded_error = error.encode(); - // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). - encoded_error.resize(4, 0); - encoded_error.try_into().expect("qed, resized to 4 bytes line above") - }, - }; - match version { - // If an unknown variant of the `DispatchError` is detected the error needs to be converted - // into the encoded value of `Error::Other`. This conversion is performed by shifting the bytes one - // position forward (discarding the last byte as it is not used) and setting the first byte to the - // encoded value of `Other` (0u8). This ensures the error is correctly categorized as an `Other` - // variant which provides all the necessary information to debug which error occurred in the runtime. - // - // Byte layout explanation: - // - Byte 0: index of the variant within `Error` - // - Byte 1: - // - Must be zero for `UNIT_ERRORS`. - // - Represents the nested error in `SINGLE_NESTED_ERRORS`. - // - Represents the first level of nesting in `DOUBLE_NESTED_ERRORS`. - // - Byte 2: - // - Represents the second level of nesting in `DOUBLE_NESTED_ERRORS`. - // - Byte 3: - // - Unused or represents further nested information. - 0 => crate::v0::handle_unknown_error(&mut encoded_error), - _ => encoded_error = UNKNOWN_CALL_ERROR_ENCODED, - } - u32::from_le_bytes(encoded_error) -} diff --git a/extension/src/functions.rs b/extension/src/functions.rs index e87f9bc6..c67450da 100644 --- a/extension/src/functions.rs +++ b/extension/src/functions.rs @@ -14,7 +14,7 @@ pub trait Function { /// # Parameters /// - `env` - The current execution environment. fn execute, S: BufIn + BufOut>( - env: Environment, + env: &mut Environment, ) -> Result; } @@ -24,7 +24,7 @@ impl Function for Tuple { for_tuples!( where #( Tuple: Function )* ); type Config = Runtime; fn execute, S: BufIn + BufOut>( - env: Environment, + env: &mut Environment, ) -> Result { // Attempts to match a specified extension/function identifier to its corresponding function, as configured by the runtime. for_tuples!( #( @@ -33,9 +33,8 @@ impl Function for Tuple { } )* ); - // Otherwise returns `DispatchError::Other` indicating an unmatched request. - // TODO: improve error so we can determine if its an invalid function vs unknown runtime call/read - Err(UNKNOWN_CALL_ERROR) + // Otherwise returns error indicating an unmatched request. + Err(pallet_contracts::Error::::NoChainExtension.into()) } } @@ -60,10 +59,10 @@ impl< /// # Parameters /// - `env` - The current execution environment. fn execute, S: BufIn + BufOut>( - mut env: Environment, + env: &mut Environment, ) -> Result { // Decode runtime call. - let call = Decoder::decode(&mut env)?.into(); + let call = Decoder::decode(env)?.into(); log::debug!(target: Logger::LOG_TARGET, "decoded: call={call:?}"); // Charge weight before dispatch. let dispatch_info = call.get_dispatch_info(); @@ -113,10 +112,10 @@ impl< /// # Parameters /// - `env` - The current execution environment. fn execute, S: BufIn + BufOut>( - mut env: Environment, + env: &mut Environment, ) -> Result { // Decode runtime read - let read = Decoder::decode(&mut env)?.into(); + let read = Decoder::decode(env)?.into(); log::debug!(target: Logger::LOG_TARGET, "decoded: read={read:?}"); // Charge weight before read let charged = env.charge_weight(read.weight())?; diff --git a/extension/src/lib.rs b/extension/src/lib.rs index a92f5624..91669fe1 100644 --- a/extension/src/lib.rs +++ b/extension/src/lib.rs @@ -1,6 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std)] -use crate::error::{DECODING_FAILED_ERROR, UNKNOWN_CALL_ERROR}; use codec::Decode as _; use frame_support::{ dispatch::{GetDispatchInfo, PostDispatchInfo, RawOrigin}, @@ -16,17 +15,14 @@ use pallet_contracts::chain_extension::{ }; pub use pallet_contracts::chain_extension::{Environment, Ext, State}; use sp_core::Get; -use sp_runtime::traits::Dispatchable; +use sp_runtime::{traits::Dispatchable, DispatchError}; use std::marker::PhantomData; mod decoding; -#[allow(dead_code)] -mod error; mod functions; mod matching; #[cfg(test)] mod tests; -mod v0; type Schedule = ::Schedule; @@ -53,7 +49,10 @@ where // reference: https://github.com/paritytech/ink-examples/blob/b8d2caa52cf4691e0ddd7c919e4462311deb5ad0/psp22-extension/runtime/psp22-extension-example.rs#L236 env.charge_weight(Schedule::::get().host_fn_weights.debug_message)?; // Execute the function - Config::Functions::execute(env) + match Config::Functions::execute(&mut env) { + Ok(r) => Ok(r), + Err(e) => Config::Error::convert(e, env), + } } } @@ -61,6 +60,8 @@ where pub trait Config { /// The function(s) available with the chain extension. type Functions: Function; + /// Optional error conversion. + type Error: ErrorConverter; /// The log target. const LOG_TARGET: &'static str; @@ -84,3 +85,25 @@ pub trait LogTarget { impl LogTarget for () { const LOG_TARGET: &'static str = "pop-chain-extension"; } + +/// Trait for error conversion. +pub trait ErrorConverter { + /// Converts the provided error. + /// + /// # Parameters + /// - `error` - The error to be converted. + /// - `env` - The current execution environment. + fn convert(error: DispatchError, env: Environment) -> Result; +} + +impl ErrorConverter for () { + fn convert(error: DispatchError, _env: Environment) -> Result { + Err(error) + } +} + +/// Trait for providing an error type. +pub trait ErrorProvider { + /// An error to return. + const ERROR: DispatchError; +} diff --git a/extension/src/v0.rs b/extension/src/v0.rs deleted file mode 100644 index 4c3536a4..00000000 --- a/extension/src/v0.rs +++ /dev/null @@ -1,298 +0,0 @@ -#[cfg(test)] -use crate::convert_to_status_code; - -pub(crate) fn handle_unknown_error(encoded_error: &mut [u8; 4]) { - let unknown = match encoded_error[0] { - code if UNIT_ERRORS.contains(&code) => nested_errors(&encoded_error[1..], None), - // Single nested errors with a limit in their nesting. - // - // `TokenError`: has ten variants - translated to a limit of nine. - 7 => nested_errors(&encoded_error[1..], Some(9)), - // `ArithmeticError`: has 3 variants - translated to a limit of two. - 8 => nested_errors(&encoded_error[1..], Some(2)), - // `TransactionalError`: has 2 variants - translated to a limit of one. - 9 => nested_errors(&encoded_error[1..], Some(1)), - code if DOUBLE_NESTED_ERRORS.contains(&code) => nested_errors(&encoded_error[3..], None), - _ => true, - }; - if unknown { - encoded_error[..].rotate_right(1); - encoded_error[0] = 0u8; - } -} - -// Unit `Error` variants. -// (variant: index): -// - CannotLookup: 1, -// - BadOrigin: 2, -// - ConsumerRemaining: 4, -// - NoProviders: 5, -// - TooManyConsumers: 6, -// - Exhausted: 10, -// - Corruption: 11, -// - Unavailable: 12, -// - RootNotAllowed: 13, -// - UnknownFunctionId: 254, -// - DecodingFailed: 255, -const UNIT_ERRORS: [u8; 11] = [1, 2, 4, 5, 6, 10, 11, 12, 13, 254, 255]; - -#[cfg(test)] -const SINGLE_NESTED_ERRORS: [u8; 3] = [7, 8, 9]; - -// Double nested `Error` variants -// (variant: index): -// - Module: 3, -const DOUBLE_NESTED_ERRORS: [u8; 1] = [3]; - -// Checks for unknown nested errors within the `DispatchError`. -// - For single nested errors with a limit, it verifies if the nested value exceeds the limit. -// - For other nested errors, it checks if any subsequent bytes are non-zero. -// -// `nested_error` - The slice of bytes representing the nested error. -// `limit` - An optional limit for single nested errors. -fn nested_errors(nested_error: &[u8], limit: Option) -> bool { - match limit { - Some(l) => nested_error[0] > l || nested_error[1..].iter().any(|&x| x != 0u8), - None => nested_error.iter().any(|&x| x != 0u8), - } -} - -#[cfg(test)] -mod tests { - use super::*; - use pop_primitives::error::{ - ArithmeticError::*, - Error::{self, *}, - TokenError::*, - TransactionalError::*, - }; - use sp_runtime::DispatchError; - - // Compare all the different `DispatchError` variants with the expected `Error`. - #[test] - fn dispatch_error_to_error() { - let test_cases = vec![ - ( - DispatchError::Other(""), - (Other { dispatch_error_index: 0, error_index: 0, error: 0 }), - ), - (DispatchError::Other("UnknownCall"), UnknownCall), - (DispatchError::Other("DecodingFailed"), DecodingFailed), - (DispatchError::CannotLookup, CannotLookup), - (DispatchError::BadOrigin, BadOrigin), - ( - DispatchError::Module(sp_runtime::ModuleError { - index: 1, - error: [2, 0, 0, 0], - message: Some("hallo"), - }), - Module { index: 1, error: 2 }, - ), - (DispatchError::ConsumerRemaining, ConsumerRemaining), - (DispatchError::NoProviders, NoProviders), - (DispatchError::TooManyConsumers, TooManyConsumers), - (DispatchError::Token(sp_runtime::TokenError::BelowMinimum), Token(BelowMinimum)), - ( - DispatchError::Arithmetic(sp_runtime::ArithmeticError::Overflow), - Arithmetic(Overflow), - ), - ( - DispatchError::Transactional(sp_runtime::TransactionalError::LimitReached), - Transactional(LimitReached), - ), - (DispatchError::Exhausted, Exhausted), - (DispatchError::Corruption, Corruption), - (DispatchError::Unavailable, Unavailable), - (DispatchError::RootNotAllowed, RootNotAllowed), - ]; - for (dispatch_error, expected) in test_cases { - let status_code = crate::convert_to_status_code(dispatch_error, 0); - let error: Error = status_code.into(); - assert_eq!(error, expected); - } - } - - // Compare all the different `DispatchError::Other` possibilities with the expected `Error`. - #[test] - fn other_error() { - let test_cases = vec![ - ( - DispatchError::Other("Random"), - (Other { dispatch_error_index: 0, error_index: 0, error: 0 }), - ), - (DispatchError::Other("UnknownCall"), UnknownCall), - (DispatchError::Other("DecodingFailed"), DecodingFailed), - ]; - for (dispatch_error, expected) in test_cases { - let status_code = convert_to_status_code(dispatch_error, 0); - let error: Error = status_code.into(); - assert_eq!(error, expected); - } - } - - // Compare all the different `DispatchError::Module` nesting possibilities, which can not be - // handled, with the expected `Error`. See `double_nested_error_variants` fourth byte nesting. - #[test] - fn test_module_error() { - let test_cases = vec![ - DispatchError::Module(sp_runtime::ModuleError { - index: 1, - error: [2, 2, 0, 0], - message: Some("Random"), - }), - DispatchError::Module(sp_runtime::ModuleError { - index: 1, - error: [2, 2, 2, 0], - message: Some("Random"), - }), - DispatchError::Module(sp_runtime::ModuleError { - index: 1, - error: [2, 2, 2, 2], - message: Some("Random"), - }), - ]; - for dispatch_error in test_cases { - let status_code = convert_to_status_code(dispatch_error, 0); - let error: Error = status_code.into(); - assert_eq!(error, Other { dispatch_error_index: 3, error_index: 1, error: 2 }); - } - } - - // Converts 4 bytes into `Error` and handles unknown errors (used in `convert_to_status_code`). - fn into_error(mut error_bytes: [u8; 4]) -> Error { - handle_unknown_error(&mut error_bytes); - u32::from_le_bytes(error_bytes).into() - } - - // Tests the `handle_unknown_error` for `Error`, version 0. - // - // Unit variants: - // If the encoded value indicates a nested `Error` which is known by V0 as a - // unit variant, the encoded value is converted into `Error::Other`. - // - // Example: the error `BadOrigin` (encoded: `[2, 0, 0, 0]`) with a non-zero value for one - // of the bytes [1..4]: `[2, 0, 1, 0]` is converted into `[0, 2, 0, 1]` (shifting the bits - // one forward). This is decoded to `Error::Other { dispatch_error: 2, index: 0, error: 1 }`. - #[test] - fn unit_error_variants() { - let errors = vec![ - CannotLookup, - BadOrigin, - ConsumerRemaining, - NoProviders, - TooManyConsumers, - Exhausted, - Corruption, - Unavailable, - RootNotAllowed, - UnknownCall, - DecodingFailed, - ]; - // Compare an `Error`, which is converted from an encoded value, with the expected `Error`. - for (i, &error_code) in UNIT_ERRORS.iter().enumerate() { - // No nesting and unit variant correctly returned. - assert_eq!(into_error([error_code, 0, 0, 0]), errors[i]); - // Unexpected second byte nested. - assert_eq!( - into_error([error_code, 1, 0, 0]), - (Other { dispatch_error_index: error_code, error_index: 1, error: 0 }), - ); - // Unexpected third byte nested. - assert_eq!( - into_error([error_code, 1, 1, 0]), - (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }), - ); - // Unexpected fourth byte nested. - assert_eq!( - into_error([error_code, 1, 1, 1]), - (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }), - ); - } - } - - // Single nested variants: - // If the encoded value indicates a double nested `Error` which is known by V0 - // as a single nested variant, the encoded value is converted into `Error::Other`. - // - // Example: the error `Arithmetic(Overflow)` (encoded: `[8, 1, 0, 0]`) with a non-zero - // value for one of the bytes [2..4]: `[8, 1, 1, 0]` is converted into `[0, 8, 1, 1]`. This is - // decoded to `Error::Other { dispatch_error: 8, index: 1, error: 1 }`. - #[test] - fn single_nested_error_variants() { - let errors = vec![ - [Token(FundsUnavailable), Token(OnlyProvider)], - [Arithmetic(Underflow), Arithmetic(Overflow)], - [Transactional(LimitReached), Transactional(NoLayer)], - ]; - // Compare an `Error`, which is converted from an encoded value, with the expected `Error`. - for (i, &error_code) in SINGLE_NESTED_ERRORS.iter().enumerate() { - // No nested and single nested variant correctly returned. - assert_eq!(into_error([error_code, 0, 0, 0]), errors[i][0]); - assert_eq!(into_error([error_code, 1, 0, 0]), errors[i][1]); - // Unexpected third byte nested. - assert_eq!( - into_error([error_code, 1, 1, 0]), - (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }), - ); - // Unexpected fourth byte nested. - assert_eq!( - into_error([error_code, 1, 1, 1]), - Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, - ); - } - } - - #[test] - fn single_nested_unknown_variants() { - // Unknown `TokenError` variant. - assert_eq!( - into_error([7, 10, 0, 0]), - Other { dispatch_error_index: 7, error_index: 10, error: 0 } - ); - // Unknown `Arithmetic` variant. - assert_eq!( - into_error([8, 3, 0, 0]), - Other { dispatch_error_index: 8, error_index: 3, error: 0 } - ); - // Unknown `Transactional` variant. - assert_eq!( - into_error([9, 2, 0, 0]), - Other { dispatch_error_index: 9, error_index: 2, error: 0 } - ); - } - - // Double nested variants: - // If the encoded value indicates a triple nested `Error` which is known by V0 - // as a double nested variant, the encoded value is converted into `Error::Other`. - // - // Example: the error `Module { index: 10, error 5 }` (encoded: `[3, 10, 5, 0]`) with a non-zero - // value for the last byte: `[3, 10, 5, 3]` is converted into `[0, 3, 10, 5]`. This is - // decoded to `Error::Other { dispatch_error: 3, index: 10, error: 5 }`. - #[test] - fn double_nested_error_variants() { - // Compare an `Error`, which is converted from an encoded value, with the expected `Error`. - // No nesting and unit variant correctly returned. - assert_eq!(into_error([3, 0, 0, 0]), Module { index: 0, error: 0 }); - // Allowed single nesting and variant correctly returned. - assert_eq!(into_error([3, 1, 0, 0]), Module { index: 1, error: 0 }); - // Allowed double nesting and variant correctly returned. - assert_eq!(into_error([3, 1, 1, 0]), Module { index: 1, error: 1 }); - // Unexpected fourth byte nested. - assert_eq!( - into_error([3, 1, 1, 1]), - Other { dispatch_error_index: 3, error_index: 1, error: 1 }, - ); - } - - #[test] - fn test_random_encoded_values() { - assert_eq!( - into_error([100, 100, 100, 100]), - Other { dispatch_error_index: 100, error_index: 100, error: 100 } - ); - assert_eq!( - into_error([200, 200, 200, 200]), - Other { dispatch_error_index: 200, error_index: 200, error: 200 } - ); - } -} diff --git a/pallets/api/src/extension.rs b/pallets/api/src/extension.rs index 48cd4c11..8ac6a4d6 100644 --- a/pallets/api/src/extension.rs +++ b/pallets/api/src/extension.rs @@ -1,6 +1,6 @@ use core::marker::PhantomData; use frame_support::traits::Get; -pub use pop_chain_extension::{Config, DispatchCall, ReadState, Readable}; +pub use pop_chain_extension::{Config, DispatchCall, ErrorConverter, ReadState, Readable}; use pop_chain_extension::{Decodes, Environment, Ext, LogTarget, Matches, Processor, State}; /// The logging target for the chain extension. @@ -9,7 +9,7 @@ pub const LOG_TARGET: &str = "pop-api::extension"; /// The chain extension used by the API. pub type Extension = pop_chain_extension::Extension; /// Decodes output by prepending bytes from ext_id() + func_id() -pub type DecodesAs = Decodes; +pub type DecodesAs = Decodes; /// Prepends bytes from ext_id() + func_id() to prefix the encoded input bytes to determine the versioned output pub struct Prepender; diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index b397c64f..e2afd469 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -51,7 +51,7 @@ impl pallet_api::extension::Config for Config { // Dispatching calls DispatchCall< Runtime, - DecodesAs, + DecodesAs, IdentifiedByFirstByteOfFunctionId>, Filter, DispatchCallLogTarget, @@ -60,12 +60,14 @@ impl pallet_api::extension::Config for Config { ReadState< Runtime, RuntimeRead, - DecodesAs, + DecodesAs, IdentifiedByFirstByteOfFunctionId>, Filter, ReadStateLogTarget, >, ); + /// Ensure errors are versioned. + type Error = VersionedErrorConverter; const LOG_TARGET: &'static str = LOG_TARGET; } @@ -111,6 +113,12 @@ mod filtering { mod versioning { use super::*; + use codec::Encode; + use pallet_contracts::chain_extension::{ + Environment, Ext, Result, RetVal, RetVal::Converging, State, + }; + use pop_chain_extension::ErrorProvider; + use sp_runtime::DispatchError; /// Versioned runtime calls. #[derive(Decode, Debug)] @@ -145,4 +153,393 @@ mod versioning { } } } + + pub struct VersionedErrorConverter; + impl ErrorConverter for VersionedErrorConverter { + fn convert( + error: DispatchError, + env: Environment, + ) -> Result { + Ok(Converging(error::convert_to_status_code(error, env.func_id().to_le_bytes()[0]))) + } + } + + pub struct DecodingFailedError; + impl ErrorProvider for DecodingFailedError { + const ERROR: DispatchError = error::DECODING_FAILED_ERROR; + } + + // TODO: refactor to move components to pallet-api + mod error { + use super::*; + + pub(crate) const DECODING_FAILED_ERROR: DispatchError = + DispatchError::Other("DecodingFailed"); + // TODO: issue #93, we can also encode the `pop_primitives::Error::UnknownCall` which means we do use + // `Error` in the runtime and it should stay in primitives. Perhaps issue #91 will also influence + // here. Should be looked at together. + const DECODING_FAILED_ERROR_ENCODED: [u8; 4] = [255u8, 0, 0, 0]; + pub(crate) const UNKNOWN_CALL_ERROR: DispatchError = DispatchError::Other("UnknownCall"); + // TODO: see above. + const UNKNOWN_CALL_ERROR_ENCODED: [u8; 4] = [254u8, 0, 0, 0]; + + /// Converts a `DispatchError` to a `u32` status code based on the version of the API the contract uses. + /// The contract calling the chain extension can optionally convert the status code to the descriptive `Error`. + /// + /// For `Error` see `pop_primitives::::error::Error`. + /// + /// The error encoding can vary per version, allowing for flexible and backward-compatible error handling. + /// As a result, contracts maintain compatibility across different versions of the runtime. + /// + /// # Parameters + /// + /// - `error`: The `DispatchError` encountered during contract execution. + /// - `version`: The version of the chain extension, used to determine the known errors. + pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { + let mut encoded_error: [u8; 4] = match error { + // "UnknownCall" and "DecodingFailed" are mapped to specific errors in the API and will + // never change. + UNKNOWN_CALL_ERROR => UNKNOWN_CALL_ERROR_ENCODED, + DECODING_FAILED_ERROR => DECODING_FAILED_ERROR_ENCODED, + _ => { + let mut encoded_error = error.encode(); + // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). + encoded_error.resize(4, 0); + encoded_error.try_into().expect("qed, resized to 4 bytes line above") + }, + }; + match version { + // If an unknown variant of the `DispatchError` is detected the error needs to be converted + // into the encoded value of `Error::Other`. This conversion is performed by shifting the bytes one + // position forward (discarding the last byte as it is not used) and setting the first byte to the + // encoded value of `Other` (0u8). This ensures the error is correctly categorized as an `Other` + // variant which provides all the necessary information to debug which error occurred in the runtime. + // + // Byte layout explanation: + // - Byte 0: index of the variant within `Error` + // - Byte 1: + // - Must be zero for `UNIT_ERRORS`. + // - Represents the nested error in `SINGLE_NESTED_ERRORS`. + // - Represents the first level of nesting in `DOUBLE_NESTED_ERRORS`. + // - Byte 2: + // - Represents the second level of nesting in `DOUBLE_NESTED_ERRORS`. + // - Byte 3: + // - Unused or represents further nested information. + 0 => v0::handle_unknown_error(&mut encoded_error), + _ => encoded_error = UNKNOWN_CALL_ERROR_ENCODED, + } + u32::from_le_bytes(encoded_error) + } + + mod v0 { + #[cfg(test)] + use super::convert_to_status_code; + + pub(crate) fn handle_unknown_error(encoded_error: &mut [u8; 4]) { + let unknown = match encoded_error[0] { + code if UNIT_ERRORS.contains(&code) => nested_errors(&encoded_error[1..], None), + // Single nested errors with a limit in their nesting. + // + // `TokenError`: has ten variants - translated to a limit of nine. + 7 => nested_errors(&encoded_error[1..], Some(9)), + // `ArithmeticError`: has 3 variants - translated to a limit of two. + 8 => nested_errors(&encoded_error[1..], Some(2)), + // `TransactionalError`: has 2 variants - translated to a limit of one. + 9 => nested_errors(&encoded_error[1..], Some(1)), + code if DOUBLE_NESTED_ERRORS.contains(&code) => { + nested_errors(&encoded_error[3..], None) + }, + _ => true, + }; + if unknown { + encoded_error[..].rotate_right(1); + encoded_error[0] = 0u8; + } + } + + // Unit `Error` variants. + // (variant: index): + // - CannotLookup: 1, + // - BadOrigin: 2, + // - ConsumerRemaining: 4, + // - NoProviders: 5, + // - TooManyConsumers: 6, + // - Exhausted: 10, + // - Corruption: 11, + // - Unavailable: 12, + // - RootNotAllowed: 13, + // - UnknownFunctionId: 254, + // - DecodingFailed: 255, + const UNIT_ERRORS: [u8; 11] = [1, 2, 4, 5, 6, 10, 11, 12, 13, 254, 255]; + + #[cfg(test)] + const SINGLE_NESTED_ERRORS: [u8; 3] = [7, 8, 9]; + + // Double nested `Error` variants + // (variant: index): + // - Module: 3, + const DOUBLE_NESTED_ERRORS: [u8; 1] = [3]; + + // Checks for unknown nested errors within the `DispatchError`. + // - For single nested errors with a limit, it verifies if the nested value exceeds the limit. + // - For other nested errors, it checks if any subsequent bytes are non-zero. + // + // `nested_error` - The slice of bytes representing the nested error. + // `limit` - An optional limit for single nested errors. + fn nested_errors(nested_error: &[u8], limit: Option) -> bool { + match limit { + Some(l) => nested_error[0] > l || nested_error[1..].iter().any(|&x| x != 0u8), + None => nested_error.iter().any(|&x| x != 0u8), + } + } + + #[cfg(test)] + mod tests { + use super::*; + use pop_primitives::error::{ + ArithmeticError::*, + Error::{self, *}, + TokenError::*, + TransactionalError::*, + }; + use sp_runtime::DispatchError; + + // Compare all the different `DispatchError` variants with the expected `Error`. + #[test] + fn dispatch_error_to_error() { + let test_cases = vec![ + ( + DispatchError::Other(""), + (Other { dispatch_error_index: 0, error_index: 0, error: 0 }), + ), + (DispatchError::Other("UnknownCall"), UnknownCall), + (DispatchError::Other("DecodingFailed"), DecodingFailed), + (DispatchError::CannotLookup, CannotLookup), + (DispatchError::BadOrigin, BadOrigin), + ( + DispatchError::Module(sp_runtime::ModuleError { + index: 1, + error: [2, 0, 0, 0], + message: Some("hallo"), + }), + Module { index: 1, error: 2 }, + ), + (DispatchError::ConsumerRemaining, ConsumerRemaining), + (DispatchError::NoProviders, NoProviders), + (DispatchError::TooManyConsumers, TooManyConsumers), + ( + DispatchError::Token(sp_runtime::TokenError::BelowMinimum), + Token(BelowMinimum), + ), + ( + DispatchError::Arithmetic(sp_runtime::ArithmeticError::Overflow), + Arithmetic(Overflow), + ), + ( + DispatchError::Transactional( + sp_runtime::TransactionalError::LimitReached, + ), + Transactional(LimitReached), + ), + (DispatchError::Exhausted, Exhausted), + (DispatchError::Corruption, Corruption), + (DispatchError::Unavailable, Unavailable), + (DispatchError::RootNotAllowed, RootNotAllowed), + ]; + for (dispatch_error, expected) in test_cases { + let status_code = convert_to_status_code(dispatch_error, 0); + let error: Error = status_code.into(); + assert_eq!(error, expected); + } + } + + // Compare all the different `DispatchError::Other` possibilities with the expected `Error`. + #[test] + fn other_error() { + let test_cases = vec![ + ( + DispatchError::Other("Random"), + (Other { dispatch_error_index: 0, error_index: 0, error: 0 }), + ), + (DispatchError::Other("UnknownCall"), UnknownCall), + (DispatchError::Other("DecodingFailed"), DecodingFailed), + ]; + for (dispatch_error, expected) in test_cases { + let status_code = convert_to_status_code(dispatch_error, 0); + let error: Error = status_code.into(); + assert_eq!(error, expected); + } + } + + // Compare all the different `DispatchError::Module` nesting possibilities, which can not be + // handled, with the expected `Error`. See `double_nested_error_variants` fourth byte nesting. + #[test] + fn test_module_error() { + let test_cases = vec![ + DispatchError::Module(sp_runtime::ModuleError { + index: 1, + error: [2, 2, 0, 0], + message: Some("Random"), + }), + DispatchError::Module(sp_runtime::ModuleError { + index: 1, + error: [2, 2, 2, 0], + message: Some("Random"), + }), + DispatchError::Module(sp_runtime::ModuleError { + index: 1, + error: [2, 2, 2, 2], + message: Some("Random"), + }), + ]; + for dispatch_error in test_cases { + let status_code = convert_to_status_code(dispatch_error, 0); + let error: Error = status_code.into(); + assert_eq!( + error, + Other { dispatch_error_index: 3, error_index: 1, error: 2 } + ); + } + } + + // Converts 4 bytes into `Error` and handles unknown errors (used in `convert_to_status_code`). + fn into_error(mut error_bytes: [u8; 4]) -> Error { + handle_unknown_error(&mut error_bytes); + u32::from_le_bytes(error_bytes).into() + } + + // Tests the `handle_unknown_error` for `Error`, version 0. + // + // Unit variants: + // If the encoded value indicates a nested `Error` which is known by V0 as a + // unit variant, the encoded value is converted into `Error::Other`. + // + // Example: the error `BadOrigin` (encoded: `[2, 0, 0, 0]`) with a non-zero value for one + // of the bytes [1..4]: `[2, 0, 1, 0]` is converted into `[0, 2, 0, 1]` (shifting the bits + // one forward). This is decoded to `Error::Other { dispatch_error: 2, index: 0, error: 1 }`. + #[test] + fn unit_error_variants() { + let errors = vec![ + CannotLookup, + BadOrigin, + ConsumerRemaining, + NoProviders, + TooManyConsumers, + Exhausted, + Corruption, + Unavailable, + RootNotAllowed, + UnknownCall, + DecodingFailed, + ]; + // Compare an `Error`, which is converted from an encoded value, with the expected `Error`. + for (i, &error_code) in UNIT_ERRORS.iter().enumerate() { + // No nesting and unit variant correctly returned. + assert_eq!(into_error([error_code, 0, 0, 0]), errors[i]); + // Unexpected second byte nested. + assert_eq!( + into_error([error_code, 1, 0, 0]), + (Other { dispatch_error_index: error_code, error_index: 1, error: 0 }), + ); + // Unexpected third byte nested. + assert_eq!( + into_error([error_code, 1, 1, 0]), + (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }), + ); + // Unexpected fourth byte nested. + assert_eq!( + into_error([error_code, 1, 1, 1]), + (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }), + ); + } + } + + // Single nested variants: + // If the encoded value indicates a double nested `Error` which is known by V0 + // as a single nested variant, the encoded value is converted into `Error::Other`. + // + // Example: the error `Arithmetic(Overflow)` (encoded: `[8, 1, 0, 0]`) with a non-zero + // value for one of the bytes [2..4]: `[8, 1, 1, 0]` is converted into `[0, 8, 1, 1]`. This is + // decoded to `Error::Other { dispatch_error: 8, index: 1, error: 1 }`. + #[test] + fn single_nested_error_variants() { + let errors = vec![ + [Token(FundsUnavailable), Token(OnlyProvider)], + [Arithmetic(Underflow), Arithmetic(Overflow)], + [Transactional(LimitReached), Transactional(NoLayer)], + ]; + // Compare an `Error`, which is converted from an encoded value, with the expected `Error`. + for (i, &error_code) in SINGLE_NESTED_ERRORS.iter().enumerate() { + // No nested and single nested variant correctly returned. + assert_eq!(into_error([error_code, 0, 0, 0]), errors[i][0]); + assert_eq!(into_error([error_code, 1, 0, 0]), errors[i][1]); + // Unexpected third byte nested. + assert_eq!( + into_error([error_code, 1, 1, 0]), + (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }), + ); + // Unexpected fourth byte nested. + assert_eq!( + into_error([error_code, 1, 1, 1]), + Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, + ); + } + } + + #[test] + fn single_nested_unknown_variants() { + // Unknown `TokenError` variant. + assert_eq!( + into_error([7, 10, 0, 0]), + Other { dispatch_error_index: 7, error_index: 10, error: 0 } + ); + // Unknown `Arithmetic` variant. + assert_eq!( + into_error([8, 3, 0, 0]), + Other { dispatch_error_index: 8, error_index: 3, error: 0 } + ); + // Unknown `Transactional` variant. + assert_eq!( + into_error([9, 2, 0, 0]), + Other { dispatch_error_index: 9, error_index: 2, error: 0 } + ); + } + + // Double nested variants: + // If the encoded value indicates a triple nested `Error` which is known by V0 + // as a double nested variant, the encoded value is converted into `Error::Other`. + // + // Example: the error `Module { index: 10, error 5 }` (encoded: `[3, 10, 5, 0]`) with a non-zero + // value for the last byte: `[3, 10, 5, 3]` is converted into `[0, 3, 10, 5]`. This is + // decoded to `Error::Other { dispatch_error: 3, index: 10, error: 5 }`. + #[test] + fn double_nested_error_variants() { + // Compare an `Error`, which is converted from an encoded value, with the expected `Error`. + // No nesting and unit variant correctly returned. + assert_eq!(into_error([3, 0, 0, 0]), Module { index: 0, error: 0 }); + // Allowed single nesting and variant correctly returned. + assert_eq!(into_error([3, 1, 0, 0]), Module { index: 1, error: 0 }); + // Allowed double nesting and variant correctly returned. + assert_eq!(into_error([3, 1, 1, 0]), Module { index: 1, error: 1 }); + // Unexpected fourth byte nested. + assert_eq!( + into_error([3, 1, 1, 1]), + Other { dispatch_error_index: 3, error_index: 1, error: 1 }, + ); + } + + #[test] + fn test_random_encoded_values() { + assert_eq!( + into_error([100, 100, 100, 100]), + Other { dispatch_error_index: 100, error_index: 100, error: 100 } + ); + assert_eq!( + into_error([200, 200, 200, 200]), + Other { dispatch_error_index: 200, error_index: 200, error: 200 } + ); + } + } + } + } } From 50a8ae1ee8f018934c44c578b2bb2a7ce62a2acb Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 20 Aug 2024 13:52:34 +0100 Subject: [PATCH 60/76] refactor: remove irrelevant comments --- extension/src/functions.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/extension/src/functions.rs b/extension/src/functions.rs index c67450da..bf1e327b 100644 --- a/extension/src/functions.rs +++ b/extension/src/functions.rs @@ -82,7 +82,6 @@ impl< let weight = frame_support::dispatch::extract_actual_weight(&result, &dispatch_info); env.adjust_weight(charged, weight); log::debug!(target: Logger::LOG_TARGET, "weight adjusted: weight={weight:?}"); - // TODO: conversion of error to 'versioned' status code result.map(|_| Converging(0)).map_err(|e| e.error) } } @@ -126,7 +125,6 @@ impl< log::debug!(target: Logger::LOG_TARGET, "read: result={result:?}"); // TODO: check parameters (allow_skip, weight_per_byte) env.write(&result, false, Some(Schedule::::get().host_fn_weights.input_per_byte))?; - // TODO: conversion of error to 'versioned' status code Ok(Converging(0)) } } From 6bd77e0b35867c068f6a47b104d373df0b9595d2 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 20 Aug 2024 15:05:20 +0100 Subject: [PATCH 61/76] fix: type resolution and dependencies --- Cargo.lock | 2 -- extension/Cargo.toml | 7 ------- extension/src/decoding.rs | 1 + extension/src/lib.rs | 3 ++- 4 files changed, 3 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fc530082..eb88df85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9472,8 +9472,6 @@ dependencies = [ "log", "pallet-contracts", "parity-scale-codec", - "pop-primitives", - "rand", "sp-core", "sp-runtime", "sp-std", diff --git a/extension/Cargo.toml b/extension/Cargo.toml index b625dcf2..657aaf26 100644 --- a/extension/Cargo.toml +++ b/extension/Cargo.toml @@ -17,9 +17,6 @@ codec.workspace = true impl-trait-for-tuples.workspace = true log.workspace = true -# Local -pop-primitives.workspace = true - # Substrate frame-support.workspace = true frame-system.workspace = true @@ -28,9 +25,6 @@ sp-core.workspace = true sp-runtime.workspace = true sp-std.workspace = true -[dev-dependencies] -rand = "0.8.5" - [features] default = ["std"] std = [ @@ -39,7 +33,6 @@ std = [ "frame-support/std", "frame-system/std", "pallet-contracts/std", - "pop-primitives/std", "sp-runtime/std", "sp-core/std", "sp-std/std", diff --git a/extension/src/decoding.rs b/extension/src/decoding.rs index cb9aade5..c5011958 100644 --- a/extension/src/decoding.rs +++ b/extension/src/decoding.rs @@ -1,6 +1,7 @@ use super::*; use pallet_contracts::chain_extension::{BufIn, State}; use sp_runtime::DispatchError; +use sp_std::vec::Vec; /// Trait for decoding data read from contract memory. pub trait Decode { diff --git a/extension/src/lib.rs b/extension/src/lib.rs index 91669fe1..ccc11e24 100644 --- a/extension/src/lib.rs +++ b/extension/src/lib.rs @@ -1,6 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] use codec::Decode as _; +use core::marker::PhantomData; use frame_support::{ dispatch::{GetDispatchInfo, PostDispatchInfo, RawOrigin}, ensure, @@ -16,7 +17,7 @@ use pallet_contracts::chain_extension::{ pub use pallet_contracts::chain_extension::{Environment, Ext, State}; use sp_core::Get; use sp_runtime::{traits::Dispatchable, DispatchError}; -use std::marker::PhantomData; +use sp_std::vec::Vec; mod decoding; mod functions; From bce8eefd8e7399291f7160a33a9f8b84d7bc70b3 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 20 Aug 2024 15:20:22 +0100 Subject: [PATCH 62/76] fix: missing import --- pallets/api/src/extension.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/pallets/api/src/extension.rs b/pallets/api/src/extension.rs index 8ac6a4d6..00712305 100644 --- a/pallets/api/src/extension.rs +++ b/pallets/api/src/extension.rs @@ -2,6 +2,7 @@ use core::marker::PhantomData; use frame_support::traits::Get; pub use pop_chain_extension::{Config, DispatchCall, ErrorConverter, ReadState, Readable}; use pop_chain_extension::{Decodes, Environment, Ext, LogTarget, Matches, Processor, State}; +use sp_std::vec::Vec; /// The logging target for the chain extension. pub const LOG_TARGET: &str = "pop-api::extension"; From 2dcf75ad72c9e08c5fba7bb8c228d1897d36bf7a Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Wed, 21 Aug 2024 14:31:00 +0100 Subject: [PATCH 63/76] refactor: error handling --- extension/src/lib.rs | 6 +- pallets/api/src/extension.rs | 19 +- .../integration-tests/src/fungibles/mod.rs | 127 +++-- pop-api/src/v0/mod.rs | 2 +- primitives/src/lib.rs | 57 +- runtime/devnet/src/config/api.rs | 520 +++++------------- 6 files changed, 272 insertions(+), 459 deletions(-) diff --git a/extension/src/lib.rs b/extension/src/lib.rs index ccc11e24..17267cd8 100644 --- a/extension/src/lib.rs +++ b/extension/src/lib.rs @@ -11,10 +11,8 @@ use frame_support::{ pub use functions::{ Decode, Decodes, DispatchCall, Equals, Function, FunctionId, Matches, Processor, ReadState, }; -use pallet_contracts::chain_extension::{ - ChainExtension, InitState, Result, RetVal, RetVal::Converging, -}; -pub use pallet_contracts::chain_extension::{Environment, Ext, State}; +use pallet_contracts::chain_extension::{ChainExtension, InitState, RetVal::Converging}; +pub use pallet_contracts::chain_extension::{Environment, Ext, Result, RetVal, State}; use sp_core::Get; use sp_runtime::{traits::Dispatchable, DispatchError}; use sp_std::vec::Vec; diff --git a/pallets/api/src/extension.rs b/pallets/api/src/extension.rs index 00712305..a1f38969 100644 --- a/pallets/api/src/extension.rs +++ b/pallets/api/src/extension.rs @@ -1,7 +1,10 @@ use core::marker::PhantomData; use frame_support::traits::Get; -pub use pop_chain_extension::{Config, DispatchCall, ErrorConverter, ReadState, Readable}; -use pop_chain_extension::{Decodes, Environment, Ext, LogTarget, Matches, Processor, State}; +pub use pop_chain_extension::{Config, DispatchCall, ErrorProvider, ReadState, Readable}; +use pop_chain_extension::{ + Decodes, Environment, Ext, LogTarget, Matches, Processor, Result, RetVal, State, +}; +use sp_runtime::DispatchError; use sp_std::vec::Vec; /// The logging target for the chain extension. @@ -53,3 +56,15 @@ pub struct ReadStateLogTarget; impl LogTarget for ReadStateLogTarget { const LOG_TARGET: &'static str = "pop-api::extension::read-state"; } + +/// Conversion of a `DispatchError` to return value. +pub struct ErrorConverter(PhantomData); +impl + Into> pop_chain_extension::ErrorConverter + for ErrorConverter +{ + fn convert(error: DispatchError, env: Environment) -> Result { + // Defer to versioned error converter + let error: Error = (error, env.func_id().to_le_bytes()[0]).into(); + Ok(RetVal::Converging(error.into())) + } +} diff --git a/pop-api/integration-tests/src/fungibles/mod.rs b/pop-api/integration-tests/src/fungibles/mod.rs index 0ca7d4f9..d1c0e2ae 100644 --- a/pop-api/integration-tests/src/fungibles/mod.rs +++ b/pop-api/integration-tests/src/fungibles/mod.rs @@ -1,9 +1,5 @@ use super::*; -use pop_primitives::error::{ - ArithmeticError::*, - Error::{self, *}, - TokenError::*, -}; +use pop_primitives::{ArithmeticError::*, AssetId, Error::*, TokenError::*, *}; use utils::*; mod utils; @@ -86,23 +82,29 @@ fn transfer_works() { let amount: Balance = 100 * UNIT; // Asset does not exist. - assert_eq!(transfer(addr.clone(), 1, BOB, amount), Err(Module { index: 52, error: 3 })); + assert_eq!( + transfer(addr.clone(), 1, BOB, amount), + Err(Module { index: 52, error: [3, 0] }) + ); // Create asset with Alice as owner and mint `amount` to contract address. let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); // Asset is not live, i.e. frozen or being destroyed. freeze_asset(ALICE, asset); assert_eq!( transfer(addr.clone(), asset, BOB, amount), - Err(Module { index: 52, error: 16 }) + Err(Module { index: 52, error: [16, 0] }) ); thaw_asset(ALICE, asset); // Not enough balance. assert_eq!( transfer(addr.clone(), asset, BOB, amount + 1 * UNIT), - Err(Module { index: 52, error: 0 }) + Err(Module { index: 52, error: [0, 0] }) ); // Not enough balance due to ED. - assert_eq!(transfer(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 0 })); + assert_eq!( + transfer(addr.clone(), asset, BOB, amount), + Err(Module { index: 52, error: [0, 0] }) + ); // Successful transfer. let balance_before_transfer = Assets::balance(asset, &BOB); assert_ok!(transfer(addr.clone(), asset, BOB, amount / 2)); @@ -114,7 +116,7 @@ fn transfer_works() { start_destroy_asset(ALICE, asset); assert_eq!( transfer(addr.clone(), asset, BOB, amount / 4), - Err(Module { index: 52, error: 16 }) + Err(Module { index: 52, error: [16, 0] }) ); }); } @@ -129,14 +131,14 @@ fn transfer_from_works() { // Asset does not exist. assert_eq!( transfer_from(addr.clone(), 1, ALICE, BOB, amount / 2), - Err(Module { index: 52, error: 3 }), + Err(Module { index: 52, error: [3, 0] }), ); // Create asset with Alice as owner and mint `amount` to contract address. let asset = create_asset_and_mint_to(ALICE, 1, ALICE, amount); // Unapproved transfer. assert_eq!( transfer_from(addr.clone(), asset, ALICE, BOB, amount / 2), - Err(Module { index: 52, error: 10 }) + Err(Module { index: 52, error: [10, 0] }) ); assert_ok!(Assets::approve_transfer( RuntimeOrigin::signed(ALICE.into()), @@ -148,13 +150,13 @@ fn transfer_from_works() { freeze_asset(ALICE, asset); assert_eq!( transfer_from(addr.clone(), asset, ALICE, BOB, amount), - Err(Module { index: 52, error: 16 }), + Err(Module { index: 52, error: [16, 0] }), ); thaw_asset(ALICE, asset); // Not enough balance. assert_eq!( transfer_from(addr.clone(), asset, ALICE, BOB, amount + 1 * UNIT), - Err(Module { index: 52, error: 0 }), + Err(Module { index: 52, error: [0, 0] }), ); // Successful transfer. let balance_before_transfer = Assets::balance(asset, &BOB); @@ -172,7 +174,7 @@ fn approve_works() { let amount: Balance = 100 * UNIT; // Asset does not exist. - assert_eq!(approve(addr.clone(), 0, BOB, amount), Err(Module { index: 52, error: 3 })); + assert_eq!(approve(addr.clone(), 0, BOB, amount), Err(Module { index: 52, error: [3, 0] })); let asset = create_asset_and_mint_to(ALICE, 0, addr.clone(), amount); assert_eq!(approve(addr.clone(), asset, BOB, amount), Err(ConsumerRemaining)); let addr = instantiate(CONTRACT, INIT_VALUE, vec![1]); @@ -180,7 +182,10 @@ fn approve_works() { let asset = create_asset_and_mint_to(ALICE, 1, addr.clone(), amount); // Asset is not live, i.e. frozen or being destroyed. freeze_asset(ALICE, asset); - assert_eq!(approve(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 16 })); + assert_eq!( + approve(addr.clone(), asset, BOB, amount), + Err(Module { index: 52, error: [16, 0] }) + ); thaw_asset(ALICE, asset); // Successful approvals: assert_eq!(0, Assets::allowance(asset, &addr, &BOB)); @@ -191,7 +196,10 @@ fn approve_works() { assert_eq!(Assets::allowance(asset, &addr, &BOB), amount / 2); // Asset is not live, i.e. frozen or being destroyed. start_destroy_asset(ALICE, asset); - assert_eq!(approve(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 16 })); + assert_eq!( + approve(addr.clone(), asset, BOB, amount), + Err(Module { index: 52, error: [16, 0] }) + ); }); } @@ -205,7 +213,7 @@ fn increase_allowance_works() { // Asset does not exist. assert_eq!( increase_allowance(addr.clone(), 0, BOB, amount), - Err(Module { index: 52, error: 3 }) + Err(Module { index: 52, error: [3, 0] }) ); let asset = create_asset_and_mint_to(ALICE, 0, addr.clone(), amount); assert_eq!(increase_allowance(addr.clone(), asset, BOB, amount), Err(ConsumerRemaining)); @@ -218,7 +226,7 @@ fn increase_allowance_works() { freeze_asset(ALICE, asset); assert_eq!( increase_allowance(addr.clone(), asset, BOB, amount), - Err(Module { index: 52, error: 16 }) + Err(Module { index: 52, error: [16, 0] }) ); thaw_asset(ALICE, asset); // Successful approvals: @@ -232,7 +240,7 @@ fn increase_allowance_works() { start_destroy_asset(ALICE, asset); assert_eq!( increase_allowance(addr.clone(), asset, BOB, amount), - Err(Module { index: 52, error: 16 }) + Err(Module { index: 52, error: [16, 0] }) ); }); } @@ -247,7 +255,7 @@ fn decrease_allowance_works() { // Asset does not exist. assert_eq!( decrease_allowance(addr.clone(), 0, BOB, amount), - Err(Module { index: 52, error: 3 }), + Err(Module { index: 52, error: [3, 0] }), ); // Create asset and mint `amount` to contract address, then approve Bob to spend `amount`. let asset = @@ -256,7 +264,7 @@ fn decrease_allowance_works() { freeze_asset(addr.clone(), asset); assert_eq!( decrease_allowance(addr.clone(), asset, BOB, amount), - Err(Module { index: 52, error: 16 }), + Err(Module { index: 52, error: [16, 0] }), ); thaw_asset(addr.clone(), asset); // Successfully decrease allowance. @@ -268,7 +276,7 @@ fn decrease_allowance_works() { start_destroy_asset(addr.clone(), asset); assert_eq!( decrease_allowance(addr.clone(), asset, BOB, amount), - Err(Module { index: 52, error: 16 }), + Err(Module { index: 52, error: [16, 0] }), ); }); } @@ -327,7 +335,7 @@ fn create_works() { // No balance to pay for fees. assert_eq!( create(addr.clone(), ASSET_ID, addr.clone(), 1), - Err(Module { index: 10, error: 2 }), + Err(Module { index: 10, error: [2, 0] }), ); // Instantiate a contract without balance for deposit. @@ -335,18 +343,27 @@ fn create_works() { // No balance to pay the deposit. assert_eq!( create(addr.clone(), ASSET_ID, addr.clone(), 1), - Err(Module { index: 10, error: 2 }), + Err(Module { index: 10, error: [2, 0] }), ); // Instantiate a contract with enough balance. let addr = instantiate(CONTRACT, INIT_VALUE, vec![2]); - assert_eq!(create(addr.clone(), ASSET_ID, BOB, 0), Err(Module { index: 52, error: 7 }),); + assert_eq!( + create(addr.clone(), ASSET_ID, BOB, 0), + Err(Module { index: 52, error: [7, 0] }), + ); // The minimal balance for an asset must be non zero. - assert_eq!(create(addr.clone(), ASSET_ID, BOB, 0), Err(Module { index: 52, error: 7 }),); + assert_eq!( + create(addr.clone(), ASSET_ID, BOB, 0), + Err(Module { index: 52, error: [7, 0] }), + ); // Create asset successfully. assert_ok!(create(addr.clone(), ASSET_ID, BOB, 1)); // Asset ID is already taken. - assert_eq!(create(addr.clone(), ASSET_ID, BOB, 1), Err(Module { index: 52, error: 5 }),); + assert_eq!( + create(addr.clone(), ASSET_ID, BOB, 1), + Err(Module { index: 52, error: [5, 0] }), + ); }); } @@ -361,7 +378,7 @@ fn instantiate_and_create_fungible_works() { create_asset(ALICE, 0, 1); assert_eq!( instantiate_and_create_fungible(contract, 0, 1), - Err(Module { index: 52, error: 5 }) + Err(Module { index: 52, error: [5, 0] }) ); // Successfully create an asset when instantiating the contract. assert_ok!(instantiate_and_create_fungible(contract, ASSET_ID, 1)); @@ -376,11 +393,11 @@ fn start_destroy_works() { let addr = instantiate(CONTRACT, INIT_VALUE, vec![2]); // Asset does not exist. - assert_eq!(start_destroy(addr.clone(), ASSET_ID), Err(Module { index: 52, error: 3 }),); + assert_eq!(start_destroy(addr.clone(), ASSET_ID), Err(Module { index: 52, error: [3, 0] }),); // Create assets where contract is not the owner. let asset = create_asset(ALICE, 0, 1); // No Permission. - assert_eq!(start_destroy(addr.clone(), asset), Err(Module { index: 52, error: 2 }),); + assert_eq!(start_destroy(addr.clone(), asset), Err(Module { index: 52, error: [2, 0] }),); let asset = create_asset(addr.clone(), ASSET_ID, 1); assert_ok!(start_destroy(addr.clone(), asset)); }); @@ -398,21 +415,21 @@ fn set_metadata_works() { // Asset does not exist. assert_eq!( set_metadata(addr.clone(), ASSET_ID, vec![0], vec![0], 0u8), - Err(Module { index: 52, error: 3 }), + Err(Module { index: 52, error: [3, 0] }), ); // Create assets where contract is not the owner. let asset = create_asset(ALICE, 0, 1); // No Permission. assert_eq!( set_metadata(addr.clone(), asset, vec![0], vec![0], 0u8), - Err(Module { index: 52, error: 2 }), + Err(Module { index: 52, error: [2, 0] }), ); let asset = create_asset(addr.clone(), ASSET_ID, 1); // Asset is not live, i.e. frozen or being destroyed. freeze_asset(addr.clone(), asset); assert_eq!( set_metadata(addr.clone(), ASSET_ID, vec![0], vec![0], 0u8), - Err(Module { index: 52, error: 16 }), + Err(Module { index: 52, error: [16, 0] }), ); thaw_asset(addr.clone(), asset); // TODO: calling the below with a vector of length `100_000` errors in pallet contracts @@ -420,7 +437,7 @@ fn set_metadata_works() { // Set bad metadata - too large values. assert_eq!( set_metadata(addr.clone(), ASSET_ID, vec![0; 1000], vec![0; 1000], 0u8), - Err(Module { index: 52, error: 9 }), + Err(Module { index: 52, error: [9, 0] }), ); // Set metadata successfully. assert_ok!(set_metadata(addr.clone(), ASSET_ID, name, symbol, decimals)); @@ -428,7 +445,7 @@ fn set_metadata_works() { start_destroy_asset(addr.clone(), asset); assert_eq!( set_metadata(addr.clone(), ASSET_ID, vec![0], vec![0], 0), - Err(Module { index: 52, error: 16 }), + Err(Module { index: 52, error: [16, 0] }), ); }); } @@ -443,18 +460,18 @@ fn clear_metadata_works() { let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); // Asset does not exist. - assert_eq!(clear_metadata(addr.clone(), 0), Err(Module { index: 52, error: 3 }),); + assert_eq!(clear_metadata(addr.clone(), 0), Err(Module { index: 52, error: [3, 0] }),); // Create assets where contract is not the owner. let asset = create_asset_and_set_metadata(ALICE, 0, vec![0], vec![0], 0); // No Permission. - assert_eq!(clear_metadata(addr.clone(), asset), Err(Module { index: 52, error: 2 }),); + assert_eq!(clear_metadata(addr.clone(), asset), Err(Module { index: 52, error: [2, 0] }),); let asset = create_asset(addr.clone(), ASSET_ID, 1); // Asset is not live, i.e. frozen or being destroyed. freeze_asset(addr.clone(), asset); - assert_eq!(clear_metadata(addr.clone(), asset), Err(Module { index: 52, error: 16 }),); + assert_eq!(clear_metadata(addr.clone(), asset), Err(Module { index: 52, error: [16, 0] }),); thaw_asset(addr.clone(), asset); // No metadata set. - assert_eq!(clear_metadata(addr.clone(), asset), Err(Module { index: 52, error: 3 }),); + assert_eq!(clear_metadata(addr.clone(), asset), Err(Module { index: 52, error: [3, 0] }),); set_metadata_asset(addr.clone(), asset, name, symbol, decimals); // Clear metadata successfully. assert_ok!(clear_metadata(addr.clone(), ASSET_ID)); @@ -462,7 +479,7 @@ fn clear_metadata_works() { start_destroy_asset(addr.clone(), asset); assert_eq!( set_metadata(addr.clone(), ASSET_ID, vec![0], vec![0], decimals), - Err(Module { index: 52, error: 16 }), + Err(Module { index: 52, error: [16, 0] }), ); }); } @@ -493,13 +510,16 @@ fn mint_works() { assert_eq!(mint(addr.clone(), 1, BOB, amount), Err(Token(UnknownAsset))); let asset = create_asset(ALICE, 1, 1); // Minting can only be done by the owner. - assert_eq!(mint(addr.clone(), asset, BOB, 1), Err(Module { index: 52, error: 2 })); + assert_eq!(mint(addr.clone(), asset, BOB, 1), Err(Module { index: 52, error: [2, 0] })); let asset = create_asset(addr.clone(), 2, 2); // Minimum balance of an asset can not be zero. assert_eq!(mint(addr.clone(), asset, BOB, 1), Err(Token(BelowMinimum))); // Asset is not live, i.e. frozen or being destroyed. freeze_asset(addr.clone(), asset); - assert_eq!(mint(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 16 })); + assert_eq!( + mint(addr.clone(), asset, BOB, amount), + Err(Module { index: 52, error: [16, 0] }) + ); thaw_asset(addr.clone(), asset); // Successful mint. let balance_before_mint = Assets::balance(asset, &BOB); @@ -510,7 +530,10 @@ fn mint_works() { assert_eq!(mint(addr.clone(), asset, BOB, Balance::MAX,), Err(Arithmetic(Overflow))); // Asset is not live, i.e. frozen or being destroyed. start_destroy_asset(addr.clone(), asset); - assert_eq!(mint(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 16 })); + assert_eq!( + mint(addr.clone(), asset, BOB, amount), + Err(Module { index: 52, error: [16, 0] }) + ); }); } @@ -522,17 +545,20 @@ fn burn_works() { let amount: Balance = 100 * UNIT; // Asset does not exist. - assert_eq!(burn(addr.clone(), 1, BOB, amount), Err(Module { index: 52, error: 3 })); + assert_eq!(burn(addr.clone(), 1, BOB, amount), Err(Module { index: 52, error: [3, 0] })); let asset = create_asset(ALICE, 1, 1); // Bob has no tokens and thus pallet assets doesn't know the account. - assert_eq!(burn(addr.clone(), asset, BOB, 1), Err(Module { index: 52, error: 1 })); + assert_eq!(burn(addr.clone(), asset, BOB, 1), Err(Module { index: 52, error: [1, 0] })); // Burning can only be done by the manager. mint_asset(ALICE, asset, BOB, amount); - assert_eq!(burn(addr.clone(), asset, BOB, 1), Err(Module { index: 52, error: 2 })); + assert_eq!(burn(addr.clone(), asset, BOB, 1), Err(Module { index: 52, error: [2, 0] })); let asset = create_asset_and_mint_to(addr.clone(), 2, BOB, amount); // Asset is not live, i.e. frozen or being destroyed. freeze_asset(addr.clone(), asset); - assert_eq!(burn(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 16 })); + assert_eq!( + burn(addr.clone(), asset, BOB, amount), + Err(Module { index: 52, error: [16, 0] }) + ); thaw_asset(addr.clone(), asset); // Successful mint. let balance_before_burn = Assets::balance(asset, &BOB); @@ -541,6 +567,9 @@ fn burn_works() { assert_eq!(balance_after_burn, balance_before_burn - amount); // Asset is not live, i.e. frozen or being destroyed. start_destroy_asset(addr.clone(), asset); - assert_eq!(burn(addr.clone(), asset, BOB, amount), Err(Module { index: 52, error: 16 })); + assert_eq!( + burn(addr.clone(), asset, BOB, amount), + Err(Module { index: 52, error: [16, 0] }) + ); }); } diff --git a/pop-api/src/v0/mod.rs b/pop-api/src/v0/mod.rs index a4824327..55732e2d 100644 --- a/pop-api/src/v0/mod.rs +++ b/pop-api/src/v0/mod.rs @@ -1,7 +1,7 @@ use crate::{ build_extension_method, constants::{DISPATCH, READ_STATE}, - primitives::error::Error, + primitives::Error, StatusCode, }; use ink::env::chain_extension::ChainExtensionMethod; diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index a51661ea..30187d8e 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -3,14 +3,16 @@ use codec::{Decode, Encode}; #[cfg(feature = "std")] use scale_info::TypeInfo; -pub use v0::error; +pub use v0::*; /// Identifier for the class of asset. pub type AssetId = u32; pub mod v0 { use super::*; - pub mod error { + pub use error::*; + + mod error { use super::*; /// Reason why a Pop API call failed. @@ -19,25 +21,20 @@ pub mod v0 { #[repr(u8)] #[allow(clippy::unnecessary_cast)] pub enum Error { - /// An unknown error occurred. This variant captures any unexpected errors that the - /// contract cannot specifically handle. It is useful for cases where there are breaking - /// changes in the runtime or when an error falls outside the predefined categories. The - /// variant includes: - /// - /// - `dispatch_error_index`: The index within the `DispatchError`. - /// - `error_index`: The index within the `DispatchError` variant (e.g. a `TokenError`). - /// - `error`: The specific error code or sub-index, providing additional context (e.g. - /// `error` in `ModuleError`). - Other { dispatch_error_index: u8, error_index: u8, error: u8 } = 0, - /// Failed to lookup some data. + /// Some error occurred. + Other = 0, + /// Failed to look up some data. CannotLookup = 1, /// A bad origin. BadOrigin = 2, /// A custom error in a module. - /// - /// - `index`: The pallet index. - /// - `error`: The error within the pallet. - Module { index: u8, error: u8 } = 3, + Module { + /// The pallet index. + index: u8, + /// The error within the pallet. + // Supports a single level of nested error only, due to status code type size constraints. + error: [u8; 2], + } = 3, /// At least one consumer is remaining so the account cannot be destroyed. ConsumerRemaining = 4, /// There are no providers so the account cannot be created. @@ -59,10 +56,19 @@ pub mod v0 { Unavailable = 12, /// Root origin is not allowed. RootNotAllowed = 13, - /// Unknown call. - UnknownCall = 254, /// Decoding failed. - DecodingFailed = 255, + DecodingFailed = 254, + /// An unknown error occurred. This variant captures any unexpected errors that the + /// contract cannot specifically handle. It is useful for cases where there are breaking + /// changes in the runtime or when an error falls outside the predefined categories. + Unknown { + /// The index within the `DispatchError`. + dispatch_error_index: u8, + /// The index within the `DispatchError` variant (e.g. a `TokenError`). + error_index: u8, + /// The specific error code or sub-index, providing additional context (e.g. `error` in `ModuleError`). + error: u8, + } = 255, } impl From for Error { @@ -77,6 +83,17 @@ pub mod v0 { } } + impl From for u32 { + fn from(value: Error) -> Self { + let mut encoded_error = value.encode(); + // Resize the encoded value to 4 bytes in order to decode the value into a u32 (4 bytes). + encoded_error.resize(4, 0); + u32::from_le_bytes( + encoded_error.try_into().expect("qed, resized to 4 bytes line above"), + ) + } + } + /// Description of what went wrong when trying to complete an operation on a token. #[derive(Encode, Decode, Debug, Eq, PartialEq)] #[cfg_attr(feature = "std", derive(TypeInfo))] diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs index e2afd469..6bc3ce6d 100644 --- a/runtime/devnet/src/config/api.rs +++ b/runtime/devnet/src/config/api.rs @@ -8,6 +8,7 @@ use frame_support::traits::Contains; use pallet_api::extension::*; pub(crate) use pallet_api::Extension; use sp_core::ConstU8; +use sp_runtime::DispatchError; use versioning::*; /// A query of runtime state. @@ -67,11 +68,17 @@ impl pallet_api::extension::Config for Config { >, ); /// Ensure errors are versioned. - type Error = VersionedErrorConverter; + type Error = ErrorConverter; const LOG_TARGET: &'static str = LOG_TARGET; } +/// Specification of error to be returned when decoding fails. +pub struct DecodingFailedError; +impl ErrorProvider for DecodingFailedError { + const ERROR: DispatchError = DispatchError::Other("DecodingFailed"); +} + mod filtering { use super::*; @@ -113,12 +120,7 @@ mod filtering { mod versioning { use super::*; - use codec::Encode; - use pallet_contracts::chain_extension::{ - Environment, Ext, Result, RetVal, RetVal::Converging, State, - }; - use pop_chain_extension::ErrorProvider; - use sp_runtime::DispatchError; + use sp_runtime::ModuleError; /// Versioned runtime calls. #[derive(Decode, Debug)] @@ -147,398 +149,150 @@ mod versioning { impl From for RuntimeRead { fn from(value: VersionedRuntimeRead) -> Self { - // Allows mapping from some previous runtime call shape to a current valid runtime read + // Allows mapping from some previous runtime read shape to a current valid runtime read match value { VersionedRuntimeRead::V0(read) => read, } } } - pub struct VersionedErrorConverter; - impl ErrorConverter for VersionedErrorConverter { - fn convert( - error: DispatchError, - env: Environment, - ) -> Result { - Ok(Converging(error::convert_to_status_code(error, env.func_id().to_le_bytes()[0]))) - } - } - - pub struct DecodingFailedError; - impl ErrorProvider for DecodingFailedError { - const ERROR: DispatchError = error::DECODING_FAILED_ERROR; + /// Versioned errors. + pub enum VersionedError { + /// Version zero of errors. + V0(pop_primitives::v0::Error), } - // TODO: refactor to move components to pallet-api - mod error { - use super::*; - - pub(crate) const DECODING_FAILED_ERROR: DispatchError = - DispatchError::Other("DecodingFailed"); - // TODO: issue #93, we can also encode the `pop_primitives::Error::UnknownCall` which means we do use - // `Error` in the runtime and it should stay in primitives. Perhaps issue #91 will also influence - // here. Should be looked at together. - const DECODING_FAILED_ERROR_ENCODED: [u8; 4] = [255u8, 0, 0, 0]; - pub(crate) const UNKNOWN_CALL_ERROR: DispatchError = DispatchError::Other("UnknownCall"); - // TODO: see above. - const UNKNOWN_CALL_ERROR_ENCODED: [u8; 4] = [254u8, 0, 0, 0]; - - /// Converts a `DispatchError` to a `u32` status code based on the version of the API the contract uses. - /// The contract calling the chain extension can optionally convert the status code to the descriptive `Error`. - /// - /// For `Error` see `pop_primitives::::error::Error`. - /// - /// The error encoding can vary per version, allowing for flexible and backward-compatible error handling. - /// As a result, contracts maintain compatibility across different versions of the runtime. - /// - /// # Parameters - /// - /// - `error`: The `DispatchError` encountered during contract execution. - /// - `version`: The version of the chain extension, used to determine the known errors. - pub(crate) fn convert_to_status_code(error: DispatchError, version: u8) -> u32 { - let mut encoded_error: [u8; 4] = match error { - // "UnknownCall" and "DecodingFailed" are mapped to specific errors in the API and will - // never change. - UNKNOWN_CALL_ERROR => UNKNOWN_CALL_ERROR_ENCODED, - DECODING_FAILED_ERROR => DECODING_FAILED_ERROR_ENCODED, - _ => { - let mut encoded_error = error.encode(); - // Resize the encoded value to 4 bytes in order to decode the value in a u32 (4 bytes). - encoded_error.resize(4, 0); - encoded_error.try_into().expect("qed, resized to 4 bytes line above") - }, - }; + impl From<(DispatchError, u8)> for VersionedError { + fn from(value: (DispatchError, u8)) -> Self { + let (error, version) = value; match version { - // If an unknown variant of the `DispatchError` is detected the error needs to be converted - // into the encoded value of `Error::Other`. This conversion is performed by shifting the bytes one - // position forward (discarding the last byte as it is not used) and setting the first byte to the - // encoded value of `Other` (0u8). This ensures the error is correctly categorized as an `Other` - // variant which provides all the necessary information to debug which error occurred in the runtime. - // - // Byte layout explanation: - // - Byte 0: index of the variant within `Error` - // - Byte 1: - // - Must be zero for `UNIT_ERRORS`. - // - Represents the nested error in `SINGLE_NESTED_ERRORS`. - // - Represents the first level of nesting in `DOUBLE_NESTED_ERRORS`. - // - Byte 2: - // - Represents the second level of nesting in `DOUBLE_NESTED_ERRORS`. - // - Byte 3: - // - Unused or represents further nested information. - 0 => v0::handle_unknown_error(&mut encoded_error), - _ => encoded_error = UNKNOWN_CALL_ERROR_ENCODED, + // Allows mapping from current `DispatchError` to a specific version of `Error` + 0 => VersionedError::V0(V0Error::from(error).0), + // TODO: should never occur due to version processing/validation when request received + _ => unimplemented!(), } - u32::from_le_bytes(encoded_error) } + } - mod v0 { - #[cfg(test)] - use super::convert_to_status_code; - - pub(crate) fn handle_unknown_error(encoded_error: &mut [u8; 4]) { - let unknown = match encoded_error[0] { - code if UNIT_ERRORS.contains(&code) => nested_errors(&encoded_error[1..], None), - // Single nested errors with a limit in their nesting. - // - // `TokenError`: has ten variants - translated to a limit of nine. - 7 => nested_errors(&encoded_error[1..], Some(9)), - // `ArithmeticError`: has 3 variants - translated to a limit of two. - 8 => nested_errors(&encoded_error[1..], Some(2)), - // `TransactionalError`: has 2 variants - translated to a limit of one. - 9 => nested_errors(&encoded_error[1..], Some(1)), - code if DOUBLE_NESTED_ERRORS.contains(&code) => { - nested_errors(&encoded_error[3..], None) - }, - _ => true, - }; - if unknown { - encoded_error[..].rotate_right(1); - encoded_error[0] = 0u8; - } - } - - // Unit `Error` variants. - // (variant: index): - // - CannotLookup: 1, - // - BadOrigin: 2, - // - ConsumerRemaining: 4, - // - NoProviders: 5, - // - TooManyConsumers: 6, - // - Exhausted: 10, - // - Corruption: 11, - // - Unavailable: 12, - // - RootNotAllowed: 13, - // - UnknownFunctionId: 254, - // - DecodingFailed: 255, - const UNIT_ERRORS: [u8; 11] = [1, 2, 4, 5, 6, 10, 11, 12, 13, 254, 255]; - - #[cfg(test)] - const SINGLE_NESTED_ERRORS: [u8; 3] = [7, 8, 9]; - - // Double nested `Error` variants - // (variant: index): - // - Module: 3, - const DOUBLE_NESTED_ERRORS: [u8; 1] = [3]; - - // Checks for unknown nested errors within the `DispatchError`. - // - For single nested errors with a limit, it verifies if the nested value exceeds the limit. - // - For other nested errors, it checks if any subsequent bytes are non-zero. - // - // `nested_error` - The slice of bytes representing the nested error. - // `limit` - An optional limit for single nested errors. - fn nested_errors(nested_error: &[u8], limit: Option) -> bool { - match limit { - Some(l) => nested_error[0] > l || nested_error[1..].iter().any(|&x| x != 0u8), - None => nested_error.iter().any(|&x| x != 0u8), - } + impl From for u32 { + fn from(value: VersionedError) -> Self { + match value { + VersionedError::V0(error) => error.into(), } + } + } - #[cfg(test)] - mod tests { - use super::*; - use pop_primitives::error::{ - ArithmeticError::*, - Error::{self, *}, - TokenError::*, - TransactionalError::*, - }; - use sp_runtime::DispatchError; - - // Compare all the different `DispatchError` variants with the expected `Error`. - #[test] - fn dispatch_error_to_error() { - let test_cases = vec![ - ( - DispatchError::Other(""), - (Other { dispatch_error_index: 0, error_index: 0, error: 0 }), - ), - (DispatchError::Other("UnknownCall"), UnknownCall), - (DispatchError::Other("DecodingFailed"), DecodingFailed), - (DispatchError::CannotLookup, CannotLookup), - (DispatchError::BadOrigin, BadOrigin), - ( - DispatchError::Module(sp_runtime::ModuleError { - index: 1, - error: [2, 0, 0, 0], - message: Some("hallo"), - }), - Module { index: 1, error: 2 }, - ), - (DispatchError::ConsumerRemaining, ConsumerRemaining), - (DispatchError::NoProviders, NoProviders), - (DispatchError::TooManyConsumers, TooManyConsumers), - ( - DispatchError::Token(sp_runtime::TokenError::BelowMinimum), - Token(BelowMinimum), - ), - ( - DispatchError::Arithmetic(sp_runtime::ArithmeticError::Overflow), - Arithmetic(Overflow), - ), - ( - DispatchError::Transactional( - sp_runtime::TransactionalError::LimitReached, - ), - Transactional(LimitReached), - ), - (DispatchError::Exhausted, Exhausted), - (DispatchError::Corruption, Corruption), - (DispatchError::Unavailable, Unavailable), - (DispatchError::RootNotAllowed, RootNotAllowed), - ]; - for (dispatch_error, expected) in test_cases { - let status_code = convert_to_status_code(dispatch_error, 0); - let error: Error = status_code.into(); - assert_eq!(error, expected); - } - } - - // Compare all the different `DispatchError::Other` possibilities with the expected `Error`. - #[test] - fn other_error() { - let test_cases = vec![ - ( - DispatchError::Other("Random"), - (Other { dispatch_error_index: 0, error_index: 0, error: 0 }), - ), - (DispatchError::Other("UnknownCall"), UnknownCall), - (DispatchError::Other("DecodingFailed"), DecodingFailed), - ]; - for (dispatch_error, expected) in test_cases { - let status_code = convert_to_status_code(dispatch_error, 0); - let error: Error = status_code.into(); - assert_eq!(error, expected); - } - } - - // Compare all the different `DispatchError::Module` nesting possibilities, which can not be - // handled, with the expected `Error`. See `double_nested_error_variants` fourth byte nesting. - #[test] - fn test_module_error() { - let test_cases = vec![ - DispatchError::Module(sp_runtime::ModuleError { - index: 1, - error: [2, 2, 0, 0], - message: Some("Random"), - }), - DispatchError::Module(sp_runtime::ModuleError { - index: 1, - error: [2, 2, 2, 0], - message: Some("Random"), - }), - DispatchError::Module(sp_runtime::ModuleError { - index: 1, - error: [2, 2, 2, 2], - message: Some("Random"), - }), - ]; - for dispatch_error in test_cases { - let status_code = convert_to_status_code(dispatch_error, 0); - let error: Error = status_code.into(); - assert_eq!( - error, - Other { dispatch_error_index: 3, error_index: 1, error: 2 } - ); - } - } - - // Converts 4 bytes into `Error` and handles unknown errors (used in `convert_to_status_code`). - fn into_error(mut error_bytes: [u8; 4]) -> Error { - handle_unknown_error(&mut error_bytes); - u32::from_le_bytes(error_bytes).into() - } - - // Tests the `handle_unknown_error` for `Error`, version 0. - // - // Unit variants: - // If the encoded value indicates a nested `Error` which is known by V0 as a - // unit variant, the encoded value is converted into `Error::Other`. - // - // Example: the error `BadOrigin` (encoded: `[2, 0, 0, 0]`) with a non-zero value for one - // of the bytes [1..4]: `[2, 0, 1, 0]` is converted into `[0, 2, 0, 1]` (shifting the bits - // one forward). This is decoded to `Error::Other { dispatch_error: 2, index: 0, error: 1 }`. - #[test] - fn unit_error_variants() { - let errors = vec![ - CannotLookup, - BadOrigin, - ConsumerRemaining, - NoProviders, - TooManyConsumers, - Exhausted, - Corruption, - Unavailable, - RootNotAllowed, - UnknownCall, - DecodingFailed, - ]; - // Compare an `Error`, which is converted from an encoded value, with the expected `Error`. - for (i, &error_code) in UNIT_ERRORS.iter().enumerate() { - // No nesting and unit variant correctly returned. - assert_eq!(into_error([error_code, 0, 0, 0]), errors[i]); - // Unexpected second byte nested. - assert_eq!( - into_error([error_code, 1, 0, 0]), - (Other { dispatch_error_index: error_code, error_index: 1, error: 0 }), - ); - // Unexpected third byte nested. - assert_eq!( - into_error([error_code, 1, 1, 0]), - (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }), - ); - // Unexpected fourth byte nested. - assert_eq!( - into_error([error_code, 1, 1, 1]), - (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }), - ); - } - } - - // Single nested variants: - // If the encoded value indicates a double nested `Error` which is known by V0 - // as a single nested variant, the encoded value is converted into `Error::Other`. - // - // Example: the error `Arithmetic(Overflow)` (encoded: `[8, 1, 0, 0]`) with a non-zero - // value for one of the bytes [2..4]: `[8, 1, 1, 0]` is converted into `[0, 8, 1, 1]`. This is - // decoded to `Error::Other { dispatch_error: 8, index: 1, error: 1 }`. - #[test] - fn single_nested_error_variants() { - let errors = vec![ - [Token(FundsUnavailable), Token(OnlyProvider)], - [Arithmetic(Underflow), Arithmetic(Overflow)], - [Transactional(LimitReached), Transactional(NoLayer)], - ]; - // Compare an `Error`, which is converted from an encoded value, with the expected `Error`. - for (i, &error_code) in SINGLE_NESTED_ERRORS.iter().enumerate() { - // No nested and single nested variant correctly returned. - assert_eq!(into_error([error_code, 0, 0, 0]), errors[i][0]); - assert_eq!(into_error([error_code, 1, 0, 0]), errors[i][1]); - // Unexpected third byte nested. - assert_eq!( - into_error([error_code, 1, 1, 0]), - (Other { dispatch_error_index: error_code, error_index: 1, error: 1 }), - ); - // Unexpected fourth byte nested. - assert_eq!( - into_error([error_code, 1, 1, 1]), - Other { dispatch_error_index: error_code, error_index: 1, error: 1 }, - ); + struct V0Error(pop_primitives::v0::Error); + impl From for V0Error { + fn from(error: DispatchError) -> Self { + use pop_primitives::v0::*; + use sp_runtime::{ArithmeticError::*, TokenError::*, TransactionalError::*}; + use DispatchError::*; + // Mappings exist here to avoid taking a dependency of sp_runtime on pop-primitives + Self(match error { + Other(_message) => { + // Note: lossy conversion: message not used due to returned contract status code size limitation + Error::Other + }, + CannotLookup => Error::CannotLookup, + BadOrigin => Error::BadOrigin, + Module(error) => { + // Note: message not used + let ModuleError { index, error, message: _message } = error; + // Map pallet-contracts::Error::DecodingFailed to Error::DecodingFailed + if index as usize + == ::index() + && error == [11, 0, 0, 0] + { + Error::DecodingFailed + } else { + // Note: lossy conversion of error value due to returned contract status code size limitation + Error::Module { index, error: [error[0], error[1]] } } - } - - #[test] - fn single_nested_unknown_variants() { - // Unknown `TokenError` variant. - assert_eq!( - into_error([7, 10, 0, 0]), - Other { dispatch_error_index: 7, error_index: 10, error: 0 } - ); - // Unknown `Arithmetic` variant. - assert_eq!( - into_error([8, 3, 0, 0]), - Other { dispatch_error_index: 8, error_index: 3, error: 0 } - ); - // Unknown `Transactional` variant. - assert_eq!( - into_error([9, 2, 0, 0]), - Other { dispatch_error_index: 9, error_index: 2, error: 0 } - ); - } - - // Double nested variants: - // If the encoded value indicates a triple nested `Error` which is known by V0 - // as a double nested variant, the encoded value is converted into `Error::Other`. - // - // Example: the error `Module { index: 10, error 5 }` (encoded: `[3, 10, 5, 0]`) with a non-zero - // value for the last byte: `[3, 10, 5, 3]` is converted into `[0, 3, 10, 5]`. This is - // decoded to `Error::Other { dispatch_error: 3, index: 10, error: 5 }`. - #[test] - fn double_nested_error_variants() { - // Compare an `Error`, which is converted from an encoded value, with the expected `Error`. - // No nesting and unit variant correctly returned. - assert_eq!(into_error([3, 0, 0, 0]), Module { index: 0, error: 0 }); - // Allowed single nesting and variant correctly returned. - assert_eq!(into_error([3, 1, 0, 0]), Module { index: 1, error: 0 }); - // Allowed double nesting and variant correctly returned. - assert_eq!(into_error([3, 1, 1, 0]), Module { index: 1, error: 1 }); - // Unexpected fourth byte nested. - assert_eq!( - into_error([3, 1, 1, 1]), - Other { dispatch_error_index: 3, error_index: 1, error: 1 }, - ); - } + }, + ConsumerRemaining => Error::ConsumerRemaining, + NoProviders => Error::NoProviders, + TooManyConsumers => Error::TooManyConsumers, + Token(error) => Error::Token(match error { + FundsUnavailable => TokenError::FundsUnavailable, + OnlyProvider => TokenError::OnlyProvider, + BelowMinimum => TokenError::BelowMinimum, + CannotCreate => TokenError::CannotCreate, + UnknownAsset => TokenError::UnknownAsset, + Frozen => TokenError::Frozen, + Unsupported => TokenError::Unsupported, + CannotCreateHold => TokenError::CannotCreateHold, + NotExpendable => TokenError::NotExpendable, + Blocked => TokenError::Blocked, + }), + Arithmetic(error) => Error::Arithmetic(match error { + Underflow => ArithmeticError::Underflow, + Overflow => ArithmeticError::Overflow, + DivisionByZero => ArithmeticError::DivisionByZero, + }), + Transactional(error) => Error::Transactional(match error { + LimitReached => TransactionalError::LimitReached, + NoLayer => TransactionalError::NoLayer, + }), + Exhausted => Error::Exhausted, + Corruption => Error::Corruption, + Unavailable => Error::Unavailable, + RootNotAllowed => Error::RootNotAllowed, + }) + } + } - #[test] - fn test_random_encoded_values() { - assert_eq!( - into_error([100, 100, 100, 100]), - Other { dispatch_error_index: 100, error_index: 100, error: 100 } - ); - assert_eq!( - into_error([200, 200, 200, 200]), - Other { dispatch_error_index: 200, error_index: 200, error: 200 } - ); - } + #[cfg(test)] + mod tests { + use super::*; + use pop_primitives::{ArithmeticError::*, Error, TokenError::*, TransactionalError::*}; + use sp_runtime::ModuleError; + use DispatchError::*; + + // Compare all the different `DispatchError` variants with the expected `Error`. + #[test] + fn dispatch_error_to_error() { + let test_cases = vec![ + (Other(""), (Error::Other)), + (Other("UnknownCall"), Error::Other), + (Other("DecodingFailed"), Error::Other), + (Other("Random"), (Error::Other)), + (CannotLookup, Error::CannotLookup), + (BadOrigin, Error::BadOrigin), + ( + Module(ModuleError { index: 1, error: [2, 0, 0, 0], message: Some("hallo") }), + Error::Module { index: 1, error: [2, 0] }, + ), + ( + Module(ModuleError { index: 1, error: [2, 2, 0, 0], message: Some("hallo") }), + Error::Module { index: 1, error: [2, 2] }, + ), + ( + Module(ModuleError { index: 1, error: [2, 2, 2, 0], message: Some("hallo") }), + Error::Module { index: 1, error: [2, 2] }, + ), + ( + Module(ModuleError { index: 1, error: [2, 2, 2, 4], message: Some("hallo") }), + Error::Module { index: 1, error: [2, 2] }, + ), + (pallet_contracts::Error::::DecodingFailed.into(), Error::DecodingFailed), + (ConsumerRemaining, Error::ConsumerRemaining), + (NoProviders, Error::NoProviders), + (TooManyConsumers, Error::TooManyConsumers), + (Token(sp_runtime::TokenError::BelowMinimum), Error::Token(BelowMinimum)), + (Arithmetic(sp_runtime::ArithmeticError::Overflow), Error::Arithmetic(Overflow)), + ( + Transactional(sp_runtime::TransactionalError::LimitReached), + Error::Transactional(LimitReached), + ), + (Exhausted, Error::Exhausted), + (Corruption, Error::Corruption), + (Unavailable, Error::Unavailable), + (RootNotAllowed, Error::RootNotAllowed), + ]; + for (dispatch_error, expected) in test_cases { + let error = V0Error::from(dispatch_error).0; + assert_eq!(error, expected); } } } From 052e36dc36b8e9b9e193342d8a28f9a4832d1c76 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Wed, 21 Aug 2024 14:36:30 +0100 Subject: [PATCH 64/76] build(deps): restore dev dependency --- Cargo.lock | 1 + extension/Cargo.toml | 3 +++ 2 files changed, 4 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index eb88df85..3a159557 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9472,6 +9472,7 @@ dependencies = [ "log", "pallet-contracts", "parity-scale-codec", + "rand", "sp-core", "sp-runtime", "sp-std", diff --git a/extension/Cargo.toml b/extension/Cargo.toml index 657aaf26..960febb3 100644 --- a/extension/Cargo.toml +++ b/extension/Cargo.toml @@ -25,6 +25,9 @@ sp-core.workspace = true sp-runtime.workspace = true sp-std.workspace = true +[dev-dependencies] +rand = "0.8.5" + [features] default = ["std"] std = [ From 9b8b589769d1f2350a8d1c2a41221a631a9b52db Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Wed, 21 Aug 2024 14:46:31 +0100 Subject: [PATCH 65/76] fix: standardise on decoding failed error --- extension/src/functions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension/src/functions.rs b/extension/src/functions.rs index bf1e327b..06bb7948 100644 --- a/extension/src/functions.rs +++ b/extension/src/functions.rs @@ -34,7 +34,7 @@ impl Function for Tuple { )* ); // Otherwise returns error indicating an unmatched request. - Err(pallet_contracts::Error::::NoChainExtension.into()) + Err(pallet_contracts::Error::::DecodingFailed.into()) } } From b0450fd0f0723049db74cc4313ec3375d44ef2c9 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Wed, 21 Aug 2024 16:09:07 +0100 Subject: [PATCH 66/76] refactor: separate versioning --- runtime/devnet/src/config/api.rs | 299 -------------------- runtime/devnet/src/config/api/mod.rs | 121 ++++++++ runtime/devnet/src/config/api/versioning.rs | 177 ++++++++++++ 3 files changed, 298 insertions(+), 299 deletions(-) delete mode 100644 runtime/devnet/src/config/api.rs create mode 100644 runtime/devnet/src/config/api/mod.rs create mode 100644 runtime/devnet/src/config/api/versioning.rs diff --git a/runtime/devnet/src/config/api.rs b/runtime/devnet/src/config/api.rs deleted file mode 100644 index 6bc3ce6d..00000000 --- a/runtime/devnet/src/config/api.rs +++ /dev/null @@ -1,299 +0,0 @@ -use crate::{ - config::assets::TrustBackedAssetsInstance, fungibles, Runtime, RuntimeCall, RuntimeEvent, -}; -use codec::Decode; -use cumulus_primitives_core::Weight; -use filtering::*; -use frame_support::traits::Contains; -use pallet_api::extension::*; -pub(crate) use pallet_api::Extension; -use sp_core::ConstU8; -use sp_runtime::DispatchError; -use versioning::*; - -/// A query of runtime state. -#[derive(Decode, Debug)] -#[repr(u8)] -pub enum RuntimeRead { - /// Fungible token queries. - #[codec(index = 150)] - Fungibles(fungibles::Read), -} - -impl Readable for RuntimeRead { - /// Determines the weight of the read, used to charge the appropriate weight before the read is performed. - fn weight(&self) -> Weight { - // TODO: defer to relevant pallet - e.g. RuntimeRead::Fungibles(key) => fungibles::Pallet::read_weight(key), - ::DbWeight::get().reads(1_u64) - } - - /// Performs the read and returns the result. - fn read(self) -> Vec { - match self { - RuntimeRead::Fungibles(key) => fungibles::Pallet::read_state(key), - } - } -} - -impl fungibles::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type AssetsInstance = TrustBackedAssetsInstance; - type WeightInfo = fungibles::weights::SubstrateWeight; -} - -#[derive(Default)] -pub struct Config; -impl pallet_api::extension::Config for Config { - /// Functions used by the Pop API. - /// - /// Each function corresponds to specific functionality provided by the API, facilitating the - /// interaction between smart contracts and the runtime. - type Functions = ( - // Dispatching calls - DispatchCall< - Runtime, - DecodesAs, - IdentifiedByFirstByteOfFunctionId>, - Filter, - DispatchCallLogTarget, - >, - // Reading state - ReadState< - Runtime, - RuntimeRead, - DecodesAs, - IdentifiedByFirstByteOfFunctionId>, - Filter, - ReadStateLogTarget, - >, - ); - /// Ensure errors are versioned. - type Error = ErrorConverter; - - const LOG_TARGET: &'static str = LOG_TARGET; -} - -/// Specification of error to be returned when decoding fails. -pub struct DecodingFailedError; -impl ErrorProvider for DecodingFailedError { - const ERROR: DispatchError = DispatchError::Other("DecodingFailed"); -} - -mod filtering { - use super::*; - - pub struct Filter; - - impl Contains for Filter { - fn contains(c: &RuntimeCall) -> bool { - use fungibles::Call::*; - matches!( - c, - RuntimeCall::Fungibles( - transfer { .. } - | transfer_from { .. } | approve { .. } - | increase_allowance { .. } - | decrease_allowance { .. } - | create { .. } | set_metadata { .. } - | start_destroy { .. } | clear_metadata { .. } - | mint { .. } | burn { .. } - ) - ) - } - } - - impl Contains for Filter { - fn contains(r: &RuntimeRead) -> bool { - use fungibles::Read::*; - matches!( - r, - RuntimeRead::Fungibles( - TotalSupply(..) - | BalanceOf { .. } | Allowance { .. } - | TokenName(..) | TokenSymbol(..) - | TokenDecimals(..) | AssetExists(..) - ) - ) - } - } -} - -mod versioning { - use super::*; - use sp_runtime::ModuleError; - - /// Versioned runtime calls. - #[derive(Decode, Debug)] - pub enum VersionedRuntimeCall { - /// Version zero of runtime calls. - #[codec(index = 0)] - V0(RuntimeCall), - } - - impl From for RuntimeCall { - fn from(value: VersionedRuntimeCall) -> Self { - // Allows mapping from some previous runtime call shape to a current valid runtime call - match value { - VersionedRuntimeCall::V0(call) => call, - } - } - } - - /// Versioned runtime state reads. - #[derive(Decode, Debug)] - pub enum VersionedRuntimeRead { - /// Version zero of runtime state reads. - #[codec(index = 0)] - V0(RuntimeRead), - } - - impl From for RuntimeRead { - fn from(value: VersionedRuntimeRead) -> Self { - // Allows mapping from some previous runtime read shape to a current valid runtime read - match value { - VersionedRuntimeRead::V0(read) => read, - } - } - } - - /// Versioned errors. - pub enum VersionedError { - /// Version zero of errors. - V0(pop_primitives::v0::Error), - } - - impl From<(DispatchError, u8)> for VersionedError { - fn from(value: (DispatchError, u8)) -> Self { - let (error, version) = value; - match version { - // Allows mapping from current `DispatchError` to a specific version of `Error` - 0 => VersionedError::V0(V0Error::from(error).0), - // TODO: should never occur due to version processing/validation when request received - _ => unimplemented!(), - } - } - } - - impl From for u32 { - fn from(value: VersionedError) -> Self { - match value { - VersionedError::V0(error) => error.into(), - } - } - } - - struct V0Error(pop_primitives::v0::Error); - impl From for V0Error { - fn from(error: DispatchError) -> Self { - use pop_primitives::v0::*; - use sp_runtime::{ArithmeticError::*, TokenError::*, TransactionalError::*}; - use DispatchError::*; - // Mappings exist here to avoid taking a dependency of sp_runtime on pop-primitives - Self(match error { - Other(_message) => { - // Note: lossy conversion: message not used due to returned contract status code size limitation - Error::Other - }, - CannotLookup => Error::CannotLookup, - BadOrigin => Error::BadOrigin, - Module(error) => { - // Note: message not used - let ModuleError { index, error, message: _message } = error; - // Map pallet-contracts::Error::DecodingFailed to Error::DecodingFailed - if index as usize - == ::index() - && error == [11, 0, 0, 0] - { - Error::DecodingFailed - } else { - // Note: lossy conversion of error value due to returned contract status code size limitation - Error::Module { index, error: [error[0], error[1]] } - } - }, - ConsumerRemaining => Error::ConsumerRemaining, - NoProviders => Error::NoProviders, - TooManyConsumers => Error::TooManyConsumers, - Token(error) => Error::Token(match error { - FundsUnavailable => TokenError::FundsUnavailable, - OnlyProvider => TokenError::OnlyProvider, - BelowMinimum => TokenError::BelowMinimum, - CannotCreate => TokenError::CannotCreate, - UnknownAsset => TokenError::UnknownAsset, - Frozen => TokenError::Frozen, - Unsupported => TokenError::Unsupported, - CannotCreateHold => TokenError::CannotCreateHold, - NotExpendable => TokenError::NotExpendable, - Blocked => TokenError::Blocked, - }), - Arithmetic(error) => Error::Arithmetic(match error { - Underflow => ArithmeticError::Underflow, - Overflow => ArithmeticError::Overflow, - DivisionByZero => ArithmeticError::DivisionByZero, - }), - Transactional(error) => Error::Transactional(match error { - LimitReached => TransactionalError::LimitReached, - NoLayer => TransactionalError::NoLayer, - }), - Exhausted => Error::Exhausted, - Corruption => Error::Corruption, - Unavailable => Error::Unavailable, - RootNotAllowed => Error::RootNotAllowed, - }) - } - } - - #[cfg(test)] - mod tests { - use super::*; - use pop_primitives::{ArithmeticError::*, Error, TokenError::*, TransactionalError::*}; - use sp_runtime::ModuleError; - use DispatchError::*; - - // Compare all the different `DispatchError` variants with the expected `Error`. - #[test] - fn dispatch_error_to_error() { - let test_cases = vec![ - (Other(""), (Error::Other)), - (Other("UnknownCall"), Error::Other), - (Other("DecodingFailed"), Error::Other), - (Other("Random"), (Error::Other)), - (CannotLookup, Error::CannotLookup), - (BadOrigin, Error::BadOrigin), - ( - Module(ModuleError { index: 1, error: [2, 0, 0, 0], message: Some("hallo") }), - Error::Module { index: 1, error: [2, 0] }, - ), - ( - Module(ModuleError { index: 1, error: [2, 2, 0, 0], message: Some("hallo") }), - Error::Module { index: 1, error: [2, 2] }, - ), - ( - Module(ModuleError { index: 1, error: [2, 2, 2, 0], message: Some("hallo") }), - Error::Module { index: 1, error: [2, 2] }, - ), - ( - Module(ModuleError { index: 1, error: [2, 2, 2, 4], message: Some("hallo") }), - Error::Module { index: 1, error: [2, 2] }, - ), - (pallet_contracts::Error::::DecodingFailed.into(), Error::DecodingFailed), - (ConsumerRemaining, Error::ConsumerRemaining), - (NoProviders, Error::NoProviders), - (TooManyConsumers, Error::TooManyConsumers), - (Token(sp_runtime::TokenError::BelowMinimum), Error::Token(BelowMinimum)), - (Arithmetic(sp_runtime::ArithmeticError::Overflow), Error::Arithmetic(Overflow)), - ( - Transactional(sp_runtime::TransactionalError::LimitReached), - Error::Transactional(LimitReached), - ), - (Exhausted, Error::Exhausted), - (Corruption, Error::Corruption), - (Unavailable, Error::Unavailable), - (RootNotAllowed, Error::RootNotAllowed), - ]; - for (dispatch_error, expected) in test_cases { - let error = V0Error::from(dispatch_error).0; - assert_eq!(error, expected); - } - } - } -} diff --git a/runtime/devnet/src/config/api/mod.rs b/runtime/devnet/src/config/api/mod.rs new file mode 100644 index 00000000..6f0f50e2 --- /dev/null +++ b/runtime/devnet/src/config/api/mod.rs @@ -0,0 +1,121 @@ +use crate::{ + config::assets::TrustBackedAssetsInstance, fungibles, Runtime, RuntimeCall, RuntimeEvent, +}; +use codec::Decode; +use cumulus_primitives_core::Weight; +use filtering::*; +use frame_support::traits::Contains; +use pallet_api::extension::*; +pub(crate) use pallet_api::Extension; +use sp_core::ConstU8; +use sp_runtime::DispatchError; +use versioning::*; + +mod versioning; + +/// A query of runtime state. +#[derive(Decode, Debug)] +#[repr(u8)] +pub enum RuntimeRead { + /// Fungible token queries. + #[codec(index = 150)] + Fungibles(fungibles::Read), +} + +impl Readable for RuntimeRead { + /// Determines the weight of the read, used to charge the appropriate weight before the read is performed. + fn weight(&self) -> Weight { + // TODO: defer to relevant pallet - e.g. RuntimeRead::Fungibles(key) => fungibles::Pallet::read_weight(key), + ::DbWeight::get().reads(1_u64) + } + + /// Performs the read and returns the result. + fn read(self) -> Vec { + match self { + RuntimeRead::Fungibles(key) => fungibles::Pallet::read_state(key), + } + } +} + +impl fungibles::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type AssetsInstance = TrustBackedAssetsInstance; + type WeightInfo = fungibles::weights::SubstrateWeight; +} + +#[derive(Default)] +pub struct Config; +impl pallet_api::extension::Config for Config { + /// Functions used by the Pop API. + /// + /// Each function corresponds to specific functionality provided by the API, facilitating the + /// interaction between smart contracts and the runtime. + type Functions = ( + // Dispatching calls + DispatchCall< + Runtime, + DecodesAs, + IdentifiedByFirstByteOfFunctionId>, + Filter, + DispatchCallLogTarget, + >, + // Reading state + ReadState< + Runtime, + RuntimeRead, + DecodesAs, + IdentifiedByFirstByteOfFunctionId>, + Filter, + ReadStateLogTarget, + >, + ); + /// Ensure errors are versioned. + type Error = ErrorConverter; + + const LOG_TARGET: &'static str = LOG_TARGET; +} + +/// Specification of error to be returned when decoding fails. +pub struct DecodingFailedError; +impl ErrorProvider for DecodingFailedError { + const ERROR: DispatchError = DispatchError::Other("DecodingFailed"); +} + +mod filtering { + use super::*; + + pub struct Filter; + + impl Contains for Filter { + fn contains(c: &RuntimeCall) -> bool { + use fungibles::Call::*; + matches!( + c, + RuntimeCall::Fungibles( + transfer { .. } + | transfer_from { .. } | approve { .. } + | increase_allowance { .. } + | decrease_allowance { .. } + | create { .. } | set_metadata { .. } + | start_destroy { .. } | clear_metadata { .. } + | mint { .. } | burn { .. } + ) + ) + } + } + + impl Contains for Filter { + fn contains(r: &RuntimeRead) -> bool { + use fungibles::Read::*; + matches!( + r, + RuntimeRead::Fungibles( + TotalSupply(..) + | BalanceOf { .. } | Allowance { .. } + | TokenName(..) | TokenSymbol(..) + | TokenDecimals(..) | AssetExists(..) + ) + ) + } + } +} diff --git a/runtime/devnet/src/config/api/versioning.rs b/runtime/devnet/src/config/api/versioning.rs new file mode 100644 index 00000000..30e832b6 --- /dev/null +++ b/runtime/devnet/src/config/api/versioning.rs @@ -0,0 +1,177 @@ +use super::*; +use sp_runtime::ModuleError; + +/// Versioned runtime calls. +#[derive(Decode, Debug)] +pub enum VersionedRuntimeCall { + /// Version zero of runtime calls. + #[codec(index = 0)] + V0(RuntimeCall), +} + +impl From for RuntimeCall { + fn from(value: VersionedRuntimeCall) -> Self { + // Allows mapping from some previous runtime call shape to a current valid runtime call + match value { + VersionedRuntimeCall::V0(call) => call, + } + } +} + +/// Versioned runtime state reads. +#[derive(Decode, Debug)] +pub enum VersionedRuntimeRead { + /// Version zero of runtime state reads. + #[codec(index = 0)] + V0(RuntimeRead), +} + +impl From for RuntimeRead { + fn from(value: VersionedRuntimeRead) -> Self { + // Allows mapping from some previous runtime read shape to a current valid runtime read + match value { + VersionedRuntimeRead::V0(read) => read, + } + } +} + +/// Versioned errors. +pub enum VersionedError { + /// Version zero of errors. + V0(pop_primitives::v0::Error), +} + +impl From<(DispatchError, u8)> for VersionedError { + fn from(value: (DispatchError, u8)) -> Self { + let (error, version) = value; + match version { + // Allows mapping from current `DispatchError` to a specific version of `Error` + 0 => VersionedError::V0(V0Error::from(error).0), + // TODO: should never occur due to version processing/validation when request received + _ => unimplemented!(), + } + } +} + +impl From for u32 { + fn from(value: VersionedError) -> Self { + match value { + VersionedError::V0(error) => error.into(), + } + } +} + +struct V0Error(pop_primitives::v0::Error); +impl From for V0Error { + fn from(error: DispatchError) -> Self { + use pop_primitives::v0::*; + use sp_runtime::{ArithmeticError::*, TokenError::*, TransactionalError::*}; + use DispatchError::*; + // Mappings exist here to avoid taking a dependency of sp_runtime on pop-primitives + Self(match error { + Other(_message) => { + // Note: lossy conversion: message not used due to returned contract status code size limitation + Error::Other + }, + CannotLookup => Error::CannotLookup, + BadOrigin => Error::BadOrigin, + Module(error) => { + // Note: message not used + let ModuleError { index, error, message: _message } = error; + // Map pallet-contracts::Error::DecodingFailed to Error::DecodingFailed + if index as usize + == ::index() + && error == [11, 0, 0, 0] + { + Error::DecodingFailed + } else { + // Note: lossy conversion of error value due to returned contract status code size limitation + Error::Module { index, error: [error[0], error[1]] } + } + }, + ConsumerRemaining => Error::ConsumerRemaining, + NoProviders => Error::NoProviders, + TooManyConsumers => Error::TooManyConsumers, + Token(error) => Error::Token(match error { + FundsUnavailable => TokenError::FundsUnavailable, + OnlyProvider => TokenError::OnlyProvider, + BelowMinimum => TokenError::BelowMinimum, + CannotCreate => TokenError::CannotCreate, + UnknownAsset => TokenError::UnknownAsset, + Frozen => TokenError::Frozen, + Unsupported => TokenError::Unsupported, + CannotCreateHold => TokenError::CannotCreateHold, + NotExpendable => TokenError::NotExpendable, + Blocked => TokenError::Blocked, + }), + Arithmetic(error) => Error::Arithmetic(match error { + Underflow => ArithmeticError::Underflow, + Overflow => ArithmeticError::Overflow, + DivisionByZero => ArithmeticError::DivisionByZero, + }), + Transactional(error) => Error::Transactional(match error { + LimitReached => TransactionalError::LimitReached, + NoLayer => TransactionalError::NoLayer, + }), + Exhausted => Error::Exhausted, + Corruption => Error::Corruption, + Unavailable => Error::Unavailable, + RootNotAllowed => Error::RootNotAllowed, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use pop_primitives::{ArithmeticError::*, Error, TokenError::*, TransactionalError::*}; + use sp_runtime::ModuleError; + use DispatchError::*; + + // Compare all the different `DispatchError` variants with the expected `Error`. + #[test] + fn dispatch_error_to_error() { + let test_cases = vec![ + (Other(""), (Error::Other)), + (Other("UnknownCall"), Error::Other), + (Other("DecodingFailed"), Error::Other), + (Other("Random"), (Error::Other)), + (CannotLookup, Error::CannotLookup), + (BadOrigin, Error::BadOrigin), + ( + Module(ModuleError { index: 1, error: [2, 0, 0, 0], message: Some("hallo") }), + Error::Module { index: 1, error: [2, 0] }, + ), + ( + Module(ModuleError { index: 1, error: [2, 2, 0, 0], message: Some("hallo") }), + Error::Module { index: 1, error: [2, 2] }, + ), + ( + Module(ModuleError { index: 1, error: [2, 2, 2, 0], message: Some("hallo") }), + Error::Module { index: 1, error: [2, 2] }, + ), + ( + Module(ModuleError { index: 1, error: [2, 2, 2, 4], message: Some("hallo") }), + Error::Module { index: 1, error: [2, 2] }, + ), + (pallet_contracts::Error::::DecodingFailed.into(), Error::DecodingFailed), + (ConsumerRemaining, Error::ConsumerRemaining), + (NoProviders, Error::NoProviders), + (TooManyConsumers, Error::TooManyConsumers), + (Token(sp_runtime::TokenError::BelowMinimum), Error::Token(BelowMinimum)), + (Arithmetic(sp_runtime::ArithmeticError::Overflow), Error::Arithmetic(Overflow)), + ( + Transactional(sp_runtime::TransactionalError::LimitReached), + Error::Transactional(LimitReached), + ), + (Exhausted, Error::Exhausted), + (Corruption, Error::Corruption), + (Unavailable, Error::Unavailable), + (RootNotAllowed, Error::RootNotAllowed), + ]; + for (dispatch_error, expected) in test_cases { + let error = V0Error::from(dispatch_error).0; + assert_eq!(error, expected); + } + } +} From eda7ca8f22e9a569012a9da95d84a3787927598d Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Wed, 21 Aug 2024 16:31:45 +0100 Subject: [PATCH 67/76] refactor: reuse decoding failed error --- extension/src/decoding.rs | 16 ++++++++++------ extension/src/lib.rs | 10 ++++++---- pallets/api/src/extension.rs | 2 +- runtime/devnet/src/config/api/mod.rs | 14 ++++++-------- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/extension/src/decoding.rs b/extension/src/decoding.rs index c5011958..4197745c 100644 --- a/extension/src/decoding.rs +++ b/extension/src/decoding.rs @@ -9,9 +9,9 @@ pub trait Decode { type Output: codec::Decode; /// An optional processor, for performing any additional processing before decoding. type Processor: Processor; - /// The error to return if decoding fails. - const ERROR: DispatchError; + type Error: Get; + /// The log target. const LOG_TARGET: &'static str; @@ -35,18 +35,22 @@ pub trait Decode { // Perform any additional processing required. Any implementation is expected to charge weight as appropriate. Self::Processor::process(&mut input, env); // Finally decode and return. - Self::Output::decode(&mut &input[..]).map_err(|_| Self::ERROR) + Self::Output::decode(&mut &input[..]).map_err(|_| Self::Error::get()) } } /// Default implementation for decoding data read from contract memory. pub struct Decodes(PhantomData<(O, E, P, L)>); -impl Decode - for Decodes +impl< + Output: codec::Decode, + Error: Get, + Processor_: Processor, + Logger: LogTarget, + > Decode for Decodes { type Output = Output; type Processor = Processor_; - const ERROR: DispatchError = Error::ERROR; + type Error = Error; const LOG_TARGET: &'static str = Logger::LOG_TARGET; } diff --git a/extension/src/lib.rs b/extension/src/lib.rs index 17267cd8..49596a6b 100644 --- a/extension/src/lib.rs +++ b/extension/src/lib.rs @@ -101,8 +101,10 @@ impl ErrorConverter for () { } } -/// Trait for providing an error type. -pub trait ErrorProvider { - /// An error to return. - const ERROR: DispatchError; +/// Error to be returned when decoding fails. +pub struct DecodingFailed(PhantomData); +impl Get for DecodingFailed { + fn get() -> DispatchError { + pallet_contracts::Error::::DecodingFailed.into() + } } diff --git a/pallets/api/src/extension.rs b/pallets/api/src/extension.rs index a1f38969..c584f41c 100644 --- a/pallets/api/src/extension.rs +++ b/pallets/api/src/extension.rs @@ -1,6 +1,6 @@ use core::marker::PhantomData; use frame_support::traits::Get; -pub use pop_chain_extension::{Config, DispatchCall, ErrorProvider, ReadState, Readable}; +pub use pop_chain_extension::{Config, DecodingFailed, DispatchCall, ReadState, Readable}; use pop_chain_extension::{ Decodes, Environment, Ext, LogTarget, Matches, Processor, Result, RetVal, State, }; diff --git a/runtime/devnet/src/config/api/mod.rs b/runtime/devnet/src/config/api/mod.rs index 6f0f50e2..d680673c 100644 --- a/runtime/devnet/src/config/api/mod.rs +++ b/runtime/devnet/src/config/api/mod.rs @@ -13,6 +13,10 @@ use versioning::*; mod versioning; +type DecodingFailedError = DecodingFailed; +type DecodesAs = + pallet_api::extension::DecodesAs; + /// A query of runtime state. #[derive(Decode, Debug)] #[repr(u8)] @@ -54,7 +58,7 @@ impl pallet_api::extension::Config for Config { // Dispatching calls DispatchCall< Runtime, - DecodesAs, + DecodesAs, IdentifiedByFirstByteOfFunctionId>, Filter, DispatchCallLogTarget, @@ -63,7 +67,7 @@ impl pallet_api::extension::Config for Config { ReadState< Runtime, RuntimeRead, - DecodesAs, + DecodesAs, IdentifiedByFirstByteOfFunctionId>, Filter, ReadStateLogTarget, @@ -75,12 +79,6 @@ impl pallet_api::extension::Config for Config { const LOG_TARGET: &'static str = LOG_TARGET; } -/// Specification of error to be returned when decoding fails. -pub struct DecodingFailedError; -impl ErrorProvider for DecodingFailedError { - const ERROR: DispatchError = DispatchError::Other("DecodingFailed"); -} - mod filtering { use super::*; From e70b83d0c0eb4eb13a66f88b7b25d514e5f31668 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Wed, 21 Aug 2024 16:33:14 +0100 Subject: [PATCH 68/76] fix: import --- runtime/devnet/src/config/api/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/devnet/src/config/api/mod.rs b/runtime/devnet/src/config/api/mod.rs index d680673c..a76323d5 100644 --- a/runtime/devnet/src/config/api/mod.rs +++ b/runtime/devnet/src/config/api/mod.rs @@ -9,6 +9,7 @@ use pallet_api::extension::*; pub(crate) use pallet_api::Extension; use sp_core::ConstU8; use sp_runtime::DispatchError; +use sp_std::vec::Vec; use versioning::*; mod versioning; From da145b9ae5b5629b2609a51ba69643bdd22577b4 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Wed, 21 Aug 2024 16:37:15 +0100 Subject: [PATCH 69/76] refactor: collapse module --- runtime/devnet/src/config/api/mod.rs | 61 +++++++++++++--------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/runtime/devnet/src/config/api/mod.rs b/runtime/devnet/src/config/api/mod.rs index a76323d5..c0d397c3 100644 --- a/runtime/devnet/src/config/api/mod.rs +++ b/runtime/devnet/src/config/api/mod.rs @@ -3,7 +3,6 @@ use crate::{ }; use codec::Decode; use cumulus_primitives_core::Weight; -use filtering::*; use frame_support::traits::Contains; use pallet_api::extension::*; pub(crate) use pallet_api::Extension; @@ -80,41 +79,39 @@ impl pallet_api::extension::Config for Config { const LOG_TARGET: &'static str = LOG_TARGET; } -mod filtering { - use super::*; +/// Filters used by the chain extension. +pub struct Filter; - pub struct Filter; - - impl Contains for Filter { - fn contains(c: &RuntimeCall) -> bool { - use fungibles::Call::*; - matches!( - c, - RuntimeCall::Fungibles( - transfer { .. } - | transfer_from { .. } | approve { .. } - | increase_allowance { .. } - | decrease_allowance { .. } - | create { .. } | set_metadata { .. } - | start_destroy { .. } | clear_metadata { .. } - | mint { .. } | burn { .. } - ) +impl Contains for Filter { + fn contains(c: &RuntimeCall) -> bool { + use fungibles::Call::*; + matches!( + c, + RuntimeCall::Fungibles( + transfer { .. } + | transfer_from { .. } + | approve { .. } | increase_allowance { .. } + | decrease_allowance { .. } + | create { .. } | set_metadata { .. } + | start_destroy { .. } + | clear_metadata { .. } + | mint { .. } | burn { .. } ) - } + ) } +} - impl Contains for Filter { - fn contains(r: &RuntimeRead) -> bool { - use fungibles::Read::*; - matches!( - r, - RuntimeRead::Fungibles( - TotalSupply(..) - | BalanceOf { .. } | Allowance { .. } - | TokenName(..) | TokenSymbol(..) - | TokenDecimals(..) | AssetExists(..) - ) +impl Contains for Filter { + fn contains(r: &RuntimeRead) -> bool { + use fungibles::Read::*; + matches!( + r, + RuntimeRead::Fungibles( + TotalSupply(..) + | BalanceOf { .. } | Allowance { .. } + | TokenName(..) | TokenSymbol(..) + | TokenDecimals(..) | AssetExists(..) ) - } + ) } } From 89d6dd8da61bf0d8dc96b0643cf7ccb47ad6d123 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Thu, 22 Aug 2024 12:31:52 +0100 Subject: [PATCH 70/76] refactor: support read result versioning --- extension/src/decoding.rs | 33 ++------- extension/src/functions.rs | 28 ++++++-- extension/src/lib.rs | 56 ++++++++++++++- pallets/api/src/extension.rs | 71 ++++++++++++++++--- pallets/api/src/fungibles/mod.rs | 78 +++++++++++++++++++++ pallets/api/src/lib.rs | 21 ++++++ runtime/devnet/src/config/api/mod.rs | 38 ++++++++-- runtime/devnet/src/config/api/versioning.rs | 37 +++++++++- 8 files changed, 311 insertions(+), 51 deletions(-) diff --git a/extension/src/decoding.rs b/extension/src/decoding.rs index 4197745c..7ad035ed 100644 --- a/extension/src/decoding.rs +++ b/extension/src/decoding.rs @@ -1,5 +1,5 @@ use super::*; -use pallet_contracts::chain_extension::{BufIn, State}; +use pallet_contracts::chain_extension::BufIn; use sp_runtime::DispatchError; use sp_std::vec::Vec; @@ -7,8 +7,8 @@ use sp_std::vec::Vec; pub trait Decode { /// The output type to be decoded. type Output: codec::Decode; - /// An optional processor, for performing any additional processing before decoding. - type Processor: Processor; + /// An optional processor, for performing any additional processing on data read from the contract before decoding. + type Processor: Processor>; /// The error to return if decoding fails. type Error: Get; @@ -33,7 +33,7 @@ pub trait Decode { let mut input = env.read(len)?; log::debug!(target: Self::LOG_TARGET, "input read: input={input:?}"); // Perform any additional processing required. Any implementation is expected to charge weight as appropriate. - Self::Processor::process(&mut input, env); + input = Self::Processor::process(input, env); // Finally decode and return. Self::Output::decode(&mut &input[..]).map_err(|_| Self::Error::get()) } @@ -44,31 +44,12 @@ pub struct Decodes(PhantomData<(O, E, P, L)>); impl< Output: codec::Decode, Error: Get, - Processor_: Processor, + ValueProcessor: Processor>, Logger: LogTarget, - > Decode for Decodes + > Decode for Decodes { type Output = Output; - type Processor = Processor_; + type Processor = ValueProcessor; type Error = Error; const LOG_TARGET: &'static str = Logger::LOG_TARGET; } - -/// Trait for processing a value based on additional information available from the environment. -pub trait Processor { - /// The log target. - const LOG_TARGET: &'static str; - - /// Processes the provided value. - /// - /// # Parameters - /// - `value` - The value to be processed. - /// - `env` - The current execution environment. - fn process(value: &mut Vec, env: &mut Environment); -} - -impl Processor for () { - const LOG_TARGET: &'static str = ""; - - fn process(_value: &mut Vec, _env: &mut Environment) {} -} diff --git a/extension/src/functions.rs b/extension/src/functions.rs index 06bb7948..0f181bd6 100644 --- a/extension/src/functions.rs +++ b/extension/src/functions.rs @@ -1,6 +1,6 @@ use super::*; use core::fmt::Debug; -pub use decoding::{Decode, Decodes, Processor}; +pub use decoding::{Decode, Decodes}; pub use matching::{Equals, FunctionId, Matches}; use pallet_contracts::chain_extension::{BufIn, BufOut}; @@ -93,15 +93,18 @@ impl Matches for DispatchCall { } /// A function for reading runtime state. -pub struct ReadState(PhantomData<(C, R, D, M, F, L)>); +pub struct ReadState::Result>, L = ()>( + PhantomData<(C, R, D, M, F, RC, L)>, +); impl< Config: pallet_contracts::Config, Read: Readable + Debug, Decoder: Decode>, Matcher: Matches, Filter: Contains, + ResultConverter: Converter>>, Logger: LogTarget, - > Function for ReadState + > Function for ReadState { /// The configuration of the contracts module. type Config = Config; @@ -123,14 +126,31 @@ impl< ensure!(Filter::contains(&read), frame_system::Error::::CallFiltered); let result = read.read(); log::debug!(target: Logger::LOG_TARGET, "read: result={result:?}"); + // Perform any final conversion. Any implementation is expected to charge weight as appropriate. + let result = ResultConverter::convert(result, env).into(); // TODO: check parameters (allow_skip, weight_per_byte) env.write(&result, false, Some(Schedule::::get().host_fn_weights.input_per_byte))?; Ok(Converging(0)) } } -impl Matches for ReadState { +impl Matches for ReadState { fn matches(env: &Environment) -> bool { M::matches(env) } } + +/// A default converter, for converting (encoding) from some type into a byte array. +pub struct DefaultConverter(PhantomData); +impl>> Converter for DefaultConverter { + type Source = T; + type Target = Vec; + const LOG_TARGET: &'static str = ""; + + fn convert( + value: Self::Source, + _env: &mut Environment, + ) -> Self::Target { + value.into() + } +} diff --git a/extension/src/lib.rs b/extension/src/lib.rs index 49596a6b..1280540e 100644 --- a/extension/src/lib.rs +++ b/extension/src/lib.rs @@ -9,13 +9,14 @@ use frame_support::{ weights::Weight, }; pub use functions::{ - Decode, Decodes, DispatchCall, Equals, Function, FunctionId, Matches, Processor, ReadState, + Decode, Decodes, DispatchCall, Equals, Function, FunctionId, Matches, ReadState, }; use pallet_contracts::chain_extension::{ChainExtension, InitState, RetVal::Converging}; pub use pallet_contracts::chain_extension::{Environment, Ext, Result, RetVal, State}; use sp_core::Get; use sp_runtime::{traits::Dispatchable, DispatchError}; use sp_std::vec::Vec; +use std::fmt::Debug; mod decoding; mod functions; @@ -66,13 +67,16 @@ pub trait Config { const LOG_TARGET: &'static str; } -/// Trait to be implemented for type handling a read of runtime state. +/// Trait to be implemented for a type handling a read of runtime state. pub trait Readable { + /// The corresponding type carrying the result of the runtime state read. + type Result: Debug; + /// Determines the weight of the read, used to charge the appropriate weight before the read is performed. fn weight(&self) -> Weight; /// Performs the read and returns the result. - fn read(self) -> Vec; + fn read(self) -> Self::Result; } /// Trait to enable specification of a log target. @@ -87,6 +91,9 @@ impl LogTarget for () { /// Trait for error conversion. pub trait ErrorConverter { + /// The log target. + const LOG_TARGET: &'static str; + /// Converts the provided error. /// /// # Parameters @@ -96,6 +103,8 @@ pub trait ErrorConverter { } impl ErrorConverter for () { + const LOG_TARGET: &'static str = "pop-chain-extension::converters::error"; + fn convert(error: DispatchError, _env: Environment) -> Result { Err(error) } @@ -108,3 +117,44 @@ impl Get for DecodingFailed { pallet_contracts::Error::::DecodingFailed.into() } } + +/// Trait for processing a value based on additional information available from the environment. +pub trait Processor { + /// The type of value to be processed. + type Value; + + /// The log target. + const LOG_TARGET: &'static str; + + /// Processes the provided value. + /// + /// # Parameters + /// - `value` - The value to be processed. + /// - `env` - The current execution environment. + fn process(value: Self::Value, env: &mut Environment) -> Self::Value; +} + +impl Processor for () { + type Value = (); + const LOG_TARGET: &'static str = ""; + fn process(value: Self::Value, _env: &mut Environment) -> Self::Value { + value + } +} + +/// Trait for converting a value based on additional information available from the environment. +pub trait Converter { + /// The type of value to be converted. + type Source; + /// The target type. + type Target; + /// The log target. + const LOG_TARGET: &'static str; + + /// Converts the provided value. + /// + /// # Parameters + /// - `value` - The value to be converted. + /// - `env` - The current execution environment. + fn convert(value: Self::Source, env: &mut Environment) -> Self::Target; +} diff --git a/pallets/api/src/extension.rs b/pallets/api/src/extension.rs index c584f41c..b65a7056 100644 --- a/pallets/api/src/extension.rs +++ b/pallets/api/src/extension.rs @@ -1,8 +1,8 @@ -use core::marker::PhantomData; +use core::{fmt::Debug, marker::PhantomData}; use frame_support::traits::Get; pub use pop_chain_extension::{Config, DecodingFailed, DispatchCall, ReadState, Readable}; use pop_chain_extension::{ - Decodes, Environment, Ext, LogTarget, Matches, Processor, Result, RetVal, State, + Converter, Decodes, Environment, Ext, LogTarget, Matches, Processor, Result, RetVal, State, }; use sp_runtime::DispatchError; use sp_std::vec::Vec; @@ -18,9 +18,21 @@ pub type DecodesAs = Decodes; + + /// The log target. const LOG_TARGET: &'static str = "pop-api::extension::processor"; - fn process(value: &mut Vec, env: &mut Environment) { + /// Processes the provided value. + /// + /// # Parameters + /// - `value` - The value to be processed. + /// - `env` - The current execution environment. + fn process( + mut value: Self::Value, + env: &mut Environment, + ) -> Self::Value { // TODO: revisit the ordering based on specced standard // Resolve version, pallet and call index from environment let version = env.func_id().to_le_bytes()[0]; @@ -32,7 +44,8 @@ impl Processor for Prepender { value.insert(0, version); value.insert(1, pallet_index); value.insert(2, call_index); - log::debug!(target: Self::LOG_TARGET, "prepender: version={version}, module={pallet_index}, call={call_index}") + log::debug!(target: Self::LOG_TARGET, "prepender: version={version}, module={pallet_index}, call={call_index}"); + value } } @@ -57,14 +70,52 @@ impl LogTarget for ReadStateLogTarget { const LOG_TARGET: &'static str = "pop-api::extension::read-state"; } -/// Conversion of a `DispatchError` to return value. -pub struct ErrorConverter(PhantomData); -impl + Into> pop_chain_extension::ErrorConverter - for ErrorConverter +/// Conversion of a `DispatchError` to a versioned error. +pub struct VersionedErrorConverter(PhantomData); +impl + Into + Debug> pop_chain_extension::ErrorConverter + for VersionedErrorConverter { + /// The log target. + const LOG_TARGET: &'static str = "pop-api::extension::converters::versioned-error"; + + /// Converts the provided error. + /// + /// # Parameters + /// - `error` - The error to be converted. + /// - `env` - The current execution environment. fn convert(error: DispatchError, env: Environment) -> Result { - // Defer to versioned error converter - let error: Error = (error, env.func_id().to_le_bytes()[0]).into(); + // Defer to supplied versioned error conversion type + let version = env.func_id().to_le_bytes()[0]; + log::debug!(target: Self::LOG_TARGET, "versioned error converter: error={error:?}, version={version}"); + let error: Error = (error, version).into(); + log::debug!(target: Self::LOG_TARGET, "versioned error converter: converted error={error:?}"); Ok(RetVal::Converging(error.into())) } } + +/// Conversion of a read result to a versioned read result. +pub struct VersionedResultConverter(PhantomData<(S, T)>); +impl + Debug> Converter + for VersionedResultConverter +{ + /// The type of value to be converted. + type Source = Source; + /// The target type. + type Target = Target; + /// The log target. + const LOG_TARGET: &'static str = "pop-api::extension::converters::versioned-result"; + + /// Converts the provided value. + /// + /// # Parameters + /// - `value` - The value to be converted. + /// - `env` - The current execution environment. + fn convert(value: Self::Source, env: &mut Environment) -> Self::Target { + // Defer to supplied versioned result conversion type + let version = env.func_id().to_le_bytes()[0]; + log::debug!(target: Self::LOG_TARGET, "versioned result converter: result={value:?}, version={version}"); + let converted: Target = (value, version).into(); + log::debug!(target: Self::LOG_TARGET, "versioned result converter: converted result={converted:?}"); + converted.into() + } +} diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index 6ff8ac87..50e53121 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -81,6 +81,40 @@ pub mod pallet { AssetExists(AssetIdOf), } + /// Results of state reads for the fungibles API. + #[derive(Debug)] + pub enum Result { + /// Total token supply for a specified asset. + TotalSupply(BalanceOf), + /// Account balance for a specified `asset` and `owner`. + BalanceOf(BalanceOf), + /// Allowance for a `spender` approved by an `owner`, for a specified `asset`. + Allowance(BalanceOf), + /// Name of the specified asset. + TokenName(Vec), + /// Symbol for the specified asset. + TokenSymbol(Vec), + /// Decimals for the specified asset. + TokenDecimals(u8), + /// Whether the specified asset exists. + AssetExists(bool), + } + + impl Result { + /// Encodes the result. + pub fn encode(&self) -> Vec { + match self { + Result::TotalSupply(result) => result.encode(), + Result::BalanceOf(result) => result.encode(), + Result::Allowance(result) => result.encode(), + Result::TokenName(result) => result.encode(), + Result::TokenSymbol(result) => result.encode(), + Result::TokenDecimals(result) => result.encode(), + Result::AssetExists(result) => result.encode(), + } + } + } + /// Configure the pallet by specifying the parameters and types on which it depends. #[pallet::config] pub trait Config: frame_system::Config + pallet_assets::Config { @@ -443,6 +477,7 @@ pub mod pallet { /// # Parameter /// - `value` - An instance of `Read`, which specifies the type of state query and /// the associated parameters. + #[deprecated] pub fn read_state(value: Read) -> Vec { use Read::*; @@ -469,4 +504,47 @@ pub mod pallet { ::WeightInfo::approve(cancel, approve) } } + + impl crate::Read for Pallet { + /// The type of read requested. + type Read = Read; + /// The type or result returned. + type Result = Result; + + /// Determines the weight of the requested read, used to charge the appropriate weight before the read is performed. + /// + /// # Parameters + /// - `request` - The read request. + fn weight(_request: &Self::Read) -> Weight { + // TODO: match on request and return benchmarked weight + T::DbWeight::get().reads(1_u64) + } + + /// Performs the requested read and returns the result. + /// + /// # Parameters + /// - `request` - The read request. + fn read(request: Self::Read) -> Self::Result { + use Read::*; + match request { + TotalSupply(asset) => Result::TotalSupply(AssetsOf::::total_supply(asset)), + BalanceOf { asset, owner } => { + Result::BalanceOf(AssetsOf::::balance(asset, owner)) + }, + Allowance { asset, owner, spender } => { + Result::Allowance(AssetsOf::::allowance(asset, &owner, &spender)) + }, + TokenName(asset) => { + Result::TokenName( as MetadataInspect>>::name(asset)) + }, + TokenSymbol(asset) => Result::TokenSymbol( as MetadataInspect< + AccountIdOf, + >>::symbol(asset)), + TokenDecimals(asset) => Result::TokenDecimals( as MetadataInspect< + AccountIdOf, + >>::decimals(asset)), + AssetExists(asset) => Result::AssetExists(AssetsOf::::asset_exists(asset)), + } + } + } } diff --git a/pallets/api/src/lib.rs b/pallets/api/src/lib.rs index 6e94609b..b3cc533e 100644 --- a/pallets/api/src/lib.rs +++ b/pallets/api/src/lib.rs @@ -1,8 +1,29 @@ #![cfg_attr(not(feature = "std"), no_std)] pub use extension::Extension; +use frame_support::pallet_prelude::Weight; pub mod extension; pub mod fungibles; #[cfg(test)] mod mock; + +/// Trait for performing reads of runtime state. +pub trait Read { + /// The type of read requested. + type Read; + /// The type or result returned. + type Result; + + /// Determines the weight of the requested read, used to charge the appropriate weight before the read is performed. + /// + /// # Parameters + /// - `request` - The read request. + fn weight(read: &Self::Read) -> Weight; + + /// Performs the requested read and returns the result. + /// + /// # Parameters + /// - `request` - The read request. + fn read(request: Self::Read) -> Self::Result; +} diff --git a/runtime/devnet/src/config/api/mod.rs b/runtime/devnet/src/config/api/mod.rs index c0d397c3..883d253c 100644 --- a/runtime/devnet/src/config/api/mod.rs +++ b/runtime/devnet/src/config/api/mod.rs @@ -6,6 +6,7 @@ use cumulus_primitives_core::Weight; use frame_support::traits::Contains; use pallet_api::extension::*; pub(crate) use pallet_api::Extension; +use pallet_api::Read; use sp_core::ConstU8; use sp_runtime::DispatchError; use sp_std::vec::Vec; @@ -27,16 +28,36 @@ pub enum RuntimeRead { } impl Readable for RuntimeRead { + /// The corresponding type carrying the result of the query for runtime state. + type Result = RuntimeResult; + /// Determines the weight of the read, used to charge the appropriate weight before the read is performed. fn weight(&self) -> Weight { - // TODO: defer to relevant pallet - e.g. RuntimeRead::Fungibles(key) => fungibles::Pallet::read_weight(key), - ::DbWeight::get().reads(1_u64) + match self { + RuntimeRead::Fungibles(key) => fungibles::Pallet::weight(key), + } } /// Performs the read and returns the result. - fn read(self) -> Vec { + fn read(self) -> Self::Result { + match self { + RuntimeRead::Fungibles(key) => RuntimeResult::Fungibles(fungibles::Pallet::read(key)), + } + } +} + +/// The result of a runtime state read. +#[derive(Debug)] +pub enum RuntimeResult { + /// Fungible token read results. + Fungibles(fungibles::Result), +} + +impl RuntimeResult { + /// Encodes the result. + fn encode(&self) -> Vec { match self { - RuntimeRead::Fungibles(key) => fungibles::Pallet::read_state(key), + RuntimeResult::Fungibles(result) => result.encode(), } } } @@ -58,7 +79,9 @@ impl pallet_api::extension::Config for Config { // Dispatching calls DispatchCall< Runtime, + // Decode as a versioned runtime call DecodesAs, + // Function 0 IdentifiedByFirstByteOfFunctionId>, Filter, DispatchCallLogTarget, @@ -67,15 +90,20 @@ impl pallet_api::extension::Config for Config { ReadState< Runtime, RuntimeRead, + // Decode as a versioned runtime read DecodesAs, + // Function 1 IdentifiedByFirstByteOfFunctionId>, Filter, + // Convert the result of a read into the expected versioned result + VersionedResultConverter, ReadStateLogTarget, >, ); /// Ensure errors are versioned. - type Error = ErrorConverter; + type Error = VersionedErrorConverter; + /// The log target. const LOG_TARGET: &'static str = LOG_TARGET; } diff --git a/runtime/devnet/src/config/api/versioning.rs b/runtime/devnet/src/config/api/versioning.rs index 30e832b6..a25a0f38 100644 --- a/runtime/devnet/src/config/api/versioning.rs +++ b/runtime/devnet/src/config/api/versioning.rs @@ -1,6 +1,8 @@ use super::*; use sp_runtime::ModuleError; +type Version = u8; + /// Versioned runtime calls. #[derive(Decode, Debug)] pub enum VersionedRuntimeCall { @@ -35,17 +37,46 @@ impl From for RuntimeRead { } } +/// Versioned runtime state read results. +#[derive(Debug)] +pub enum VersionedRuntimeResult { + /// Version zero of runtime read results. + V0(RuntimeResult), +} + +impl From<(RuntimeResult, Version)> for VersionedRuntimeResult { + fn from(value: (RuntimeResult, Version)) -> Self { + let (result, version) = value; + match version { + // Allows mapping from current `RuntimeResult` to a specific/prior version + 0 => VersionedRuntimeResult::V0(result), + // TODO: should never occur due to version processing/validation when request received + _ => unimplemented!(), + } + } +} + +impl From for Vec { + fn from(result: VersionedRuntimeResult) -> Self { + match result { + // Simply unwrap and return the encoded result + VersionedRuntimeResult::V0(result) => result.encode(), + } + } +} + /// Versioned errors. +#[derive(Debug)] pub enum VersionedError { /// Version zero of errors. V0(pop_primitives::v0::Error), } -impl From<(DispatchError, u8)> for VersionedError { - fn from(value: (DispatchError, u8)) -> Self { +impl From<(DispatchError, Version)> for VersionedError { + fn from(value: (DispatchError, Version)) -> Self { let (error, version) = value; match version { - // Allows mapping from current `DispatchError` to a specific version of `Error` + // Allows mapping from current `DispatchError` to a specific/prior version of `Error` 0 => VersionedError::V0(V0Error::from(error).0), // TODO: should never occur due to version processing/validation when request received _ => unimplemented!(), From d26790c5870b00bdb25482ae0e2a8449f1381f0c Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Thu, 22 Aug 2024 12:54:56 +0100 Subject: [PATCH 71/76] fix: resolve type clash --- pallets/api/src/fungibles/mod.rs | 43 ++++++++++++++-------------- runtime/devnet/src/config/api/mod.rs | 2 +- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index 50e53121..24c8b342 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -83,7 +83,7 @@ pub mod pallet { /// Results of state reads for the fungibles API. #[derive(Debug)] - pub enum Result { + pub enum ReadResult { /// Total token supply for a specified asset. TotalSupply(BalanceOf), /// Account balance for a specified `asset` and `owner`. @@ -100,17 +100,18 @@ pub mod pallet { AssetExists(bool), } - impl Result { + impl ReadResult { /// Encodes the result. pub fn encode(&self) -> Vec { + use ReadResult::*; match self { - Result::TotalSupply(result) => result.encode(), - Result::BalanceOf(result) => result.encode(), - Result::Allowance(result) => result.encode(), - Result::TokenName(result) => result.encode(), - Result::TokenSymbol(result) => result.encode(), - Result::TokenDecimals(result) => result.encode(), - Result::AssetExists(result) => result.encode(), + TotalSupply(result) => result.encode(), + BalanceOf(result) => result.encode(), + Allowance(result) => result.encode(), + TokenName(result) => result.encode(), + TokenSymbol(result) => result.encode(), + TokenDecimals(result) => result.encode(), + AssetExists(result) => result.encode(), } } } @@ -509,7 +510,7 @@ pub mod pallet { /// The type of read requested. type Read = Read; /// The type or result returned. - type Result = Result; + type Result = ReadResult; /// Determines the weight of the requested read, used to charge the appropriate weight before the read is performed. /// @@ -527,23 +528,23 @@ pub mod pallet { fn read(request: Self::Read) -> Self::Result { use Read::*; match request { - TotalSupply(asset) => Result::TotalSupply(AssetsOf::::total_supply(asset)), + TotalSupply(asset) => ReadResult::TotalSupply(AssetsOf::::total_supply(asset)), BalanceOf { asset, owner } => { - Result::BalanceOf(AssetsOf::::balance(asset, owner)) + ReadResult::BalanceOf(AssetsOf::::balance(asset, owner)) }, Allowance { asset, owner, spender } => { - Result::Allowance(AssetsOf::::allowance(asset, &owner, &spender)) + ReadResult::Allowance(AssetsOf::::allowance(asset, &owner, &spender)) }, - TokenName(asset) => { - Result::TokenName( as MetadataInspect>>::name(asset)) - }, - TokenSymbol(asset) => Result::TokenSymbol( as MetadataInspect< + TokenName(asset) => ReadResult::TokenName( as MetadataInspect< AccountIdOf, - >>::symbol(asset)), - TokenDecimals(asset) => Result::TokenDecimals( as MetadataInspect< + >>::name(asset)), + TokenSymbol(asset) => ReadResult::TokenSymbol( as MetadataInspect< AccountIdOf, - >>::decimals(asset)), - AssetExists(asset) => Result::AssetExists(AssetsOf::::asset_exists(asset)), + >>::symbol(asset)), + TokenDecimals(asset) => ReadResult::TokenDecimals( + as MetadataInspect>>::decimals(asset), + ), + AssetExists(asset) => ReadResult::AssetExists(AssetsOf::::asset_exists(asset)), } } } diff --git a/runtime/devnet/src/config/api/mod.rs b/runtime/devnet/src/config/api/mod.rs index 883d253c..e244f656 100644 --- a/runtime/devnet/src/config/api/mod.rs +++ b/runtime/devnet/src/config/api/mod.rs @@ -50,7 +50,7 @@ impl Readable for RuntimeRead { #[derive(Debug)] pub enum RuntimeResult { /// Fungible token read results. - Fungibles(fungibles::Result), + Fungibles(fungibles::ReadResult), } impl RuntimeResult { From 22a683ad6b187c2f79bae1c4e8881b95030b8025 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Thu, 22 Aug 2024 13:30:24 +0100 Subject: [PATCH 72/76] fix: update import --- extension/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/extension/src/lib.rs b/extension/src/lib.rs index 1280540e..2a244541 100644 --- a/extension/src/lib.rs +++ b/extension/src/lib.rs @@ -1,7 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] use codec::Decode as _; -use core::marker::PhantomData; +use core::{fmt::Debug, marker::PhantomData}; use frame_support::{ dispatch::{GetDispatchInfo, PostDispatchInfo, RawOrigin}, ensure, @@ -16,7 +16,6 @@ pub use pallet_contracts::chain_extension::{Environment, Ext, Result, RetVal, St use sp_core::Get; use sp_runtime::{traits::Dispatchable, DispatchError}; use sp_std::vec::Vec; -use std::fmt::Debug; mod decoding; mod functions; From 702b1588ebdda5791ccf3db4e3d8375ccb2857d8 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Fri, 23 Aug 2024 16:54:56 +0700 Subject: [PATCH 73/76] feat: add pallet-revive --- Cargo.lock | 3175 +++++++----- Cargo.toml | 31 + pallets/contracts/Cargo.toml | 128 + pallets/contracts/README.md | 160 + pallets/contracts/build.rs | 72 + pallets/contracts/fixtures/Cargo.toml | 24 + pallets/contracts/fixtures/build.rs | 355 ++ pallets/contracts/fixtures/build/Cargo.toml | 18 + .../account_reentrance_count_call.rs | 39 + .../contracts/fixtures/contracts/balance.rs | 36 + pallets/contracts/fixtures/contracts/call.rs | 49 + .../fixtures/contracts/call_return_code.rs | 56 + .../fixtures/contracts/call_runtime.rs | 42 + .../contracts/call_runtime_and_call.rs | 53 + .../contracts/call_with_flags_and_value.rs | 51 + .../fixtures/contracts/call_with_limit.rs | 52 + .../fixtures/contracts/caller_contract.rs | 145 + .../fixtures/contracts/caller_is_origin_n.rs | 38 + .../fixtures/contracts/chain_extension.rs | 42 + .../contracts/chain_extension_temp_storage.rs | 65 + .../fixtures/contracts/common/Cargo.toml | 11 + .../fixtures/contracts/common/src/lib.rs | 161 + .../contracts/create_storage_and_call.rs | 58 + .../create_storage_and_instantiate.rs | 58 + .../create_transient_storage_and_call.rs | 56 + .../fixtures/contracts/crypto_hashes.rs | 84 + .../contracts/debug_message_invalid_utf8.rs | 33 + .../debug_message_logging_disabled.rs | 33 + .../fixtures/contracts/debug_message_works.rs | 33 + .../fixtures/contracts/delegate_call.rs | 49 + .../fixtures/contracts/delegate_call_lib.rs | 49 + .../contracts/delegate_call_simple.rs | 36 + .../contracts/destroy_and_transfer.rs | 86 + pallets/contracts/fixtures/contracts/drain.rs | 44 + pallets/contracts/fixtures/contracts/dummy.rs | 28 + .../fixtures/contracts/ecdsa_recover.rs | 44 + .../contracts/event_and_return_on_deploy.rs | 36 + .../fixtures/contracts/event_size.rs | 37 + .../fixtures/contracts/float_instruction.rs | 34 + .../contracts/instantiate_return_code.rs | 52 + .../contracts/locking_delegate_dependency.rs | 68 + .../fixtures/contracts/multi_store.rs | 43 + .../contracts/new_set_code_hash_contract.rs | 32 + .../fixtures/contracts/ok_trap_revert.rs | 44 + .../fixtures/contracts/read_only_call.rs | 50 + .../contracts/fixtures/contracts/recurse.rs | 53 + .../contracts/reentrance_count_call.rs | 57 + .../reentrance_count_delegated_call.rs | 53 + .../fixtures/contracts/return_with_data.rs | 47 + .../fixtures/contracts/run_out_of_gas.rs | 32 + .../fixtures/contracts/self_destruct.rs | 55 + .../contracts/self_destructing_constructor.rs | 32 + .../fixtures/contracts/set_code_hash.rs | 37 + .../fixtures/contracts/set_empty_storage.rs | 32 + .../contracts/set_transient_storage.rs | 42 + .../fixtures/contracts/sr25519_verify.rs | 48 + .../contracts/fixtures/contracts/storage.rs | 63 + .../fixtures/contracts/storage_size.rs | 45 + .../fixtures/contracts/store_call.rs | 41 + .../fixtures/contracts/store_deploy.rs | 41 + .../contracts/transfer_return_code.rs | 38 + .../fixtures/contracts/transient_storage.rs | 58 + .../fixtures/contracts/xcm_execute.rs | 40 + .../contracts/fixtures/contracts/xcm_send.rs | 42 + pallets/contracts/fixtures/src/lib.rs | 45 + pallets/contracts/mock-network/Cargo.toml | 87 + pallets/contracts/mock-network/src/lib.rs | 152 + pallets/contracts/mock-network/src/mocks.rs | 18 + .../mock-network/src/mocks/msg_queue.rs | 186 + .../src/mocks/relay_message_queue.rs | 52 + .../contracts/mock-network/src/parachain.rs | 346 ++ .../src/parachain/contracts_config.rs | 33 + .../contracts/mock-network/src/primitives.rs | 23 + .../contracts/mock-network/src/relay_chain.rs | 239 + pallets/contracts/mock-network/src/tests.rs | 200 + pallets/contracts/proc-macro/Cargo.toml | 20 + pallets/contracts/proc-macro/src/lib.rs | 923 ++++ pallets/contracts/src/address.rs | 68 + .../src/benchmarking/call_builder.rs | 236 + pallets/contracts/src/benchmarking/code.rs | 364 ++ pallets/contracts/src/benchmarking/mod.rs | 2074 ++++++++ pallets/contracts/src/benchmarking/sandbox.rs | 91 + pallets/contracts/src/chain_extension.rs | 495 ++ pallets/contracts/src/debug.rs | 112 + pallets/contracts/src/exec.rs | 4284 ++++++++++++++++ pallets/contracts/src/gas.rs | 399 ++ pallets/contracts/src/lib.rs | 1986 ++++++++ pallets/contracts/src/migration.rs | 658 +++ pallets/contracts/src/migration/v09.rs | 148 + pallets/contracts/src/migration/v10.rs | 322 ++ pallets/contracts/src/migration/v11.rs | 136 + pallets/contracts/src/migration/v12.rs | 351 ++ pallets/contracts/src/migration/v13.rs | 136 + pallets/contracts/src/migration/v14.rs | 274 + pallets/contracts/src/migration/v15.rs | 332 ++ pallets/contracts/src/migration/v16.rs | 106 + pallets/contracts/src/primitives.rs | 252 + pallets/contracts/src/schedule.rs | 149 + pallets/contracts/src/storage.rs | 480 ++ pallets/contracts/src/storage/meter.rs | 908 ++++ pallets/contracts/src/test_utils.rs | 30 + pallets/contracts/src/test_utils/builder.rs | 220 + pallets/contracts/src/tests.rs | 4396 +++++++++++++++++ pallets/contracts/src/tests/pallet_dummy.rs | 53 + pallets/contracts/src/tests/test_debug.rs | 229 + pallets/contracts/src/transient_storage.rs | 698 +++ pallets/contracts/src/wasm/mod.rs | 3917 +++++++++++++++ pallets/contracts/src/wasm/prepare.rs | 873 ++++ pallets/contracts/src/wasm/runtime.rs | 2549 ++++++++++ pallets/contracts/src/weights.rs | 2120 ++++++++ pallets/contracts/uapi/Cargo.toml | 28 + pallets/contracts/uapi/src/flags.rs | 81 + pallets/contracts/uapi/src/host.rs | 888 ++++ pallets/contracts/uapi/src/host/riscv32.rs | 327 ++ pallets/contracts/uapi/src/host/wasm32.rs | 915 ++++ pallets/contracts/uapi/src/lib.rs | 138 + 116 files changed, 39772 insertions(+), 1121 deletions(-) create mode 100644 pallets/contracts/Cargo.toml create mode 100644 pallets/contracts/README.md create mode 100644 pallets/contracts/build.rs create mode 100644 pallets/contracts/fixtures/Cargo.toml create mode 100644 pallets/contracts/fixtures/build.rs create mode 100644 pallets/contracts/fixtures/build/Cargo.toml create mode 100644 pallets/contracts/fixtures/contracts/account_reentrance_count_call.rs create mode 100644 pallets/contracts/fixtures/contracts/balance.rs create mode 100644 pallets/contracts/fixtures/contracts/call.rs create mode 100644 pallets/contracts/fixtures/contracts/call_return_code.rs create mode 100644 pallets/contracts/fixtures/contracts/call_runtime.rs create mode 100644 pallets/contracts/fixtures/contracts/call_runtime_and_call.rs create mode 100644 pallets/contracts/fixtures/contracts/call_with_flags_and_value.rs create mode 100644 pallets/contracts/fixtures/contracts/call_with_limit.rs create mode 100644 pallets/contracts/fixtures/contracts/caller_contract.rs create mode 100644 pallets/contracts/fixtures/contracts/caller_is_origin_n.rs create mode 100644 pallets/contracts/fixtures/contracts/chain_extension.rs create mode 100644 pallets/contracts/fixtures/contracts/chain_extension_temp_storage.rs create mode 100644 pallets/contracts/fixtures/contracts/common/Cargo.toml create mode 100644 pallets/contracts/fixtures/contracts/common/src/lib.rs create mode 100644 pallets/contracts/fixtures/contracts/create_storage_and_call.rs create mode 100644 pallets/contracts/fixtures/contracts/create_storage_and_instantiate.rs create mode 100644 pallets/contracts/fixtures/contracts/create_transient_storage_and_call.rs create mode 100644 pallets/contracts/fixtures/contracts/crypto_hashes.rs create mode 100644 pallets/contracts/fixtures/contracts/debug_message_invalid_utf8.rs create mode 100644 pallets/contracts/fixtures/contracts/debug_message_logging_disabled.rs create mode 100644 pallets/contracts/fixtures/contracts/debug_message_works.rs create mode 100644 pallets/contracts/fixtures/contracts/delegate_call.rs create mode 100644 pallets/contracts/fixtures/contracts/delegate_call_lib.rs create mode 100644 pallets/contracts/fixtures/contracts/delegate_call_simple.rs create mode 100644 pallets/contracts/fixtures/contracts/destroy_and_transfer.rs create mode 100644 pallets/contracts/fixtures/contracts/drain.rs create mode 100644 pallets/contracts/fixtures/contracts/dummy.rs create mode 100644 pallets/contracts/fixtures/contracts/ecdsa_recover.rs create mode 100644 pallets/contracts/fixtures/contracts/event_and_return_on_deploy.rs create mode 100644 pallets/contracts/fixtures/contracts/event_size.rs create mode 100644 pallets/contracts/fixtures/contracts/float_instruction.rs create mode 100644 pallets/contracts/fixtures/contracts/instantiate_return_code.rs create mode 100644 pallets/contracts/fixtures/contracts/locking_delegate_dependency.rs create mode 100644 pallets/contracts/fixtures/contracts/multi_store.rs create mode 100644 pallets/contracts/fixtures/contracts/new_set_code_hash_contract.rs create mode 100644 pallets/contracts/fixtures/contracts/ok_trap_revert.rs create mode 100644 pallets/contracts/fixtures/contracts/read_only_call.rs create mode 100644 pallets/contracts/fixtures/contracts/recurse.rs create mode 100644 pallets/contracts/fixtures/contracts/reentrance_count_call.rs create mode 100644 pallets/contracts/fixtures/contracts/reentrance_count_delegated_call.rs create mode 100644 pallets/contracts/fixtures/contracts/return_with_data.rs create mode 100644 pallets/contracts/fixtures/contracts/run_out_of_gas.rs create mode 100644 pallets/contracts/fixtures/contracts/self_destruct.rs create mode 100644 pallets/contracts/fixtures/contracts/self_destructing_constructor.rs create mode 100644 pallets/contracts/fixtures/contracts/set_code_hash.rs create mode 100644 pallets/contracts/fixtures/contracts/set_empty_storage.rs create mode 100644 pallets/contracts/fixtures/contracts/set_transient_storage.rs create mode 100644 pallets/contracts/fixtures/contracts/sr25519_verify.rs create mode 100644 pallets/contracts/fixtures/contracts/storage.rs create mode 100644 pallets/contracts/fixtures/contracts/storage_size.rs create mode 100644 pallets/contracts/fixtures/contracts/store_call.rs create mode 100644 pallets/contracts/fixtures/contracts/store_deploy.rs create mode 100644 pallets/contracts/fixtures/contracts/transfer_return_code.rs create mode 100644 pallets/contracts/fixtures/contracts/transient_storage.rs create mode 100644 pallets/contracts/fixtures/contracts/xcm_execute.rs create mode 100644 pallets/contracts/fixtures/contracts/xcm_send.rs create mode 100644 pallets/contracts/fixtures/src/lib.rs create mode 100644 pallets/contracts/mock-network/Cargo.toml create mode 100644 pallets/contracts/mock-network/src/lib.rs create mode 100644 pallets/contracts/mock-network/src/mocks.rs create mode 100644 pallets/contracts/mock-network/src/mocks/msg_queue.rs create mode 100644 pallets/contracts/mock-network/src/mocks/relay_message_queue.rs create mode 100644 pallets/contracts/mock-network/src/parachain.rs create mode 100644 pallets/contracts/mock-network/src/parachain/contracts_config.rs create mode 100644 pallets/contracts/mock-network/src/primitives.rs create mode 100644 pallets/contracts/mock-network/src/relay_chain.rs create mode 100644 pallets/contracts/mock-network/src/tests.rs create mode 100644 pallets/contracts/proc-macro/Cargo.toml create mode 100644 pallets/contracts/proc-macro/src/lib.rs create mode 100644 pallets/contracts/src/address.rs create mode 100644 pallets/contracts/src/benchmarking/call_builder.rs create mode 100644 pallets/contracts/src/benchmarking/code.rs create mode 100644 pallets/contracts/src/benchmarking/mod.rs create mode 100644 pallets/contracts/src/benchmarking/sandbox.rs create mode 100644 pallets/contracts/src/chain_extension.rs create mode 100644 pallets/contracts/src/debug.rs create mode 100644 pallets/contracts/src/exec.rs create mode 100644 pallets/contracts/src/gas.rs create mode 100644 pallets/contracts/src/lib.rs create mode 100644 pallets/contracts/src/migration.rs create mode 100644 pallets/contracts/src/migration/v09.rs create mode 100644 pallets/contracts/src/migration/v10.rs create mode 100644 pallets/contracts/src/migration/v11.rs create mode 100644 pallets/contracts/src/migration/v12.rs create mode 100644 pallets/contracts/src/migration/v13.rs create mode 100644 pallets/contracts/src/migration/v14.rs create mode 100644 pallets/contracts/src/migration/v15.rs create mode 100644 pallets/contracts/src/migration/v16.rs create mode 100644 pallets/contracts/src/primitives.rs create mode 100644 pallets/contracts/src/schedule.rs create mode 100644 pallets/contracts/src/storage.rs create mode 100644 pallets/contracts/src/storage/meter.rs create mode 100644 pallets/contracts/src/test_utils.rs create mode 100644 pallets/contracts/src/test_utils/builder.rs create mode 100644 pallets/contracts/src/tests.rs create mode 100644 pallets/contracts/src/tests/pallet_dummy.rs create mode 100644 pallets/contracts/src/tests/test_debug.rs create mode 100644 pallets/contracts/src/transient_storage.rs create mode 100644 pallets/contracts/src/wasm/mod.rs create mode 100644 pallets/contracts/src/wasm/prepare.rs create mode 100644 pallets/contracts/src/wasm/runtime.rs create mode 100644 pallets/contracts/src/weights.rs create mode 100644 pallets/contracts/uapi/Cargo.toml create mode 100644 pallets/contracts/uapi/src/flags.rs create mode 100644 pallets/contracts/uapi/src/host.rs create mode 100644 pallets/contracts/uapi/src/host/riscv32.rs create mode 100644 pallets/contracts/uapi/src/host/wasm32.rs create mode 100644 pallets/contracts/uapi/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 3407a210..38a48c9e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -274,7 +274,7 @@ dependencies = [ "num-bigint", "num-traits", "paste", - "rustc_version", + "rustc_version 0.4.0", "zeroize", ] @@ -444,8 +444,8 @@ dependencies = [ "frame-benchmarking", "frame-executive", "frame-metadata-hash-extension", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", @@ -482,21 +482,21 @@ dependencies = [ "primitive-types", "scale-info", "snowbridge-router-primitives", - "sp-api", + "sp-api 27.0.1", "sp-block-builder", "sp-consensus-aura", - "sp-core", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-core 29.0.0", + "sp-genesis-builder 0.8.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", "sp-std", - "sp-storage", + "sp-storage 20.0.0", "sp-transaction-pool", - "sp-version", - "sp-weights", + "sp-version 30.0.0", + "sp-weights 28.0.0", "staging-parachain-info", "staging-xcm", "staging-xcm-builder", @@ -514,8 +514,8 @@ dependencies = [ "cumulus-pallet-parachain-system", "cumulus-pallet-xcmp-queue", "cumulus-primitives-core", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "pallet-assets", "pallet-balances", "pallet-collator-selection", @@ -525,8 +525,8 @@ dependencies = [ "parachains-common", "parachains-runtimes-test-utils", "parity-scale-codec", - "sp-io", - "sp-runtime", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", "staging-parachain-info", "staging-xcm", @@ -542,7 +542,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65e90021d772c2dd82d45fd085e05a2cb5866464d4c7421ac6a8007733b350bb" dependencies = [ "cumulus-primitives-core", - "frame-support", + "frame-support 29.0.2", "impl-trait-for-tuples", "log", "pallet-asset-conversion", @@ -550,8 +550,8 @@ dependencies = [ "parachains-common", "parity-scale-codec", "scale-info", - "sp-api", - "sp-runtime", + "sp-api 27.0.1", + "sp-runtime 32.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -860,19 +860,35 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93f2635620bf0b9d4576eb7bb9a38a55df78bd1205d26fa994b25911a69f212f" dependencies = [ - "bitcoin_hashes", + "bitcoin_hashes 0.11.0", "rand", "rand_core 0.6.4", "serde", "unicode-normalization", ] +[[package]] +name = "bitcoin-internals" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9425c3bf7089c983facbae04de54513cce73b41c7f9ff8c845b54e7bc64ebbfb" + [[package]] name = "bitcoin_hashes" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90064b8dee6815a6470d60bad07bbbaee885c0e12d04177138fa3291a01b7bc4" +[[package]] +name = "bitcoin_hashes" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1930a4dabfebb8d7d9992db18ebe3ae2876f0a305fab206fd168df931ede293b" +dependencies = [ + "bitcoin-internals", + "hex-conservative", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -1021,7 +1037,7 @@ name = "bp-asset-hub-paseo" version = "1.0.0" source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#2fa66a19eef96bd2c079385057fab2564d09b751" dependencies = [ - "frame-support", + "frame-support 29.0.2", "sp-std", "staging-xcm", "system-parachains-constants", @@ -1036,10 +1052,10 @@ dependencies = [ "bp-messages", "bp-polkadot-core", "bp-runtime", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "polkadot-primitives", - "sp-api", + "sp-api 27.0.1", "sp-std", ] @@ -1051,12 +1067,12 @@ dependencies = [ "bp-bridge-hub-cumulus", "bp-messages", "bp-runtime", - "frame-support", + "frame-support 29.0.2", "parity-scale-codec", "scale-info", "snowbridge-core", - "sp-api", - "sp-runtime", + "sp-api 27.0.1", + "sp-runtime 32.0.0", "sp-std", "staging-xcm", ] @@ -1069,13 +1085,13 @@ checksum = "1c4d2c457d5e18a5dbfe47a2ecd01f95036930a4a7ac0f3e47c2843bb067331b" dependencies = [ "bp-runtime", "finality-grandpa", - "frame-support", + "frame-support 29.0.2", "parity-scale-codec", "scale-info", "serde", "sp-consensus-grandpa", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -1087,11 +1103,11 @@ checksum = "cf43a49ea13d4c2f141481b6cbff85a197c47fe6aec1f5af21e40b68e8fd02fd" dependencies = [ "bp-header-chain", "bp-runtime", - "frame-support", + "frame-support 29.0.2", "parity-scale-codec", "scale-info", "serde", - "sp-core", + "sp-core 29.0.0", "sp-std", ] @@ -1104,12 +1120,12 @@ dependencies = [ "bp-header-chain", "bp-polkadot-core", "bp-runtime", - "frame-support", + "frame-support 29.0.2", "impl-trait-for-tuples", "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -1121,14 +1137,14 @@ checksum = "b862e8dcccc9a3fafb58a1735bc205b7663d3335d7b3dd942503b98f28d6b067" dependencies = [ "bp-messages", "bp-runtime", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "parity-util-mem", "scale-info", "serde", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -1140,10 +1156,10 @@ checksum = "74a4b0e2771227611fe9e6a2c37ba2bf7408cf2385a9eb2f44e6096bb0e616ec" dependencies = [ "bp-messages", "bp-runtime", - "frame-support", + "frame-support 29.0.2", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -1153,8 +1169,8 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b29668fffbc3e4a7ad789b498424ed6d8a313f93544a090bbaaef8a1f7fd243" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "hash-db", "impl-trait-for-tuples", "log", @@ -1162,13 +1178,13 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", - "sp-state-machine", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "sp-std", - "sp-trie", - "trie-db", + "sp-trie 30.0.0", + "trie-db 0.28.0", ] [[package]] @@ -1184,12 +1200,12 @@ dependencies = [ "ed25519-dalek", "finality-grandpa", "parity-scale-codec", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-grandpa", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", - "sp-trie", + "sp-trie 30.0.0", ] [[package]] @@ -1209,8 +1225,8 @@ checksum = "86ff4abe93be7bc1663adc41817b1aa3476fbec953ce361537419924310d5dd4" dependencies = [ "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", ] [[package]] @@ -1227,8 +1243,8 @@ dependencies = [ "bp-runtime", "bp-xcm-bridge-hub", "bp-xcm-bridge-hub-router", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "hash-db", "log", "pallet-bridge-grandpa", @@ -1239,12 +1255,12 @@ dependencies = [ "pallet-utility", "parity-scale-codec", "scale-info", - "sp-api", - "sp-core", - "sp-io", - "sp-runtime", + "sp-api 27.0.1", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", - "sp-trie", + "sp-trie 30.0.0", "staging-xcm", "staging-xcm-builder", ] @@ -1758,7 +1774,7 @@ dependencies = [ "gimli 0.27.3", "hashbrown 0.13.2", "log", - "regalloc2", + "regalloc2 0.6.1", "smallvec", "target-lexicon", ] @@ -1956,8 +1972,8 @@ dependencies = [ "sc-client-api", "sc-service", "sp-blockchain", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "url", ] @@ -1978,10 +1994,10 @@ dependencies = [ "polkadot-overseer", "polkadot-primitives", "sc-client-api", - "sp-api", + "sp-api 27.0.1", "sp-consensus", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "tracing", ] @@ -2012,17 +2028,17 @@ dependencies = [ "sc-consensus-slots", "sc-telemetry", "schnellru", - "sp-api", - "sp-application-crypto", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", "sp-block-builder", "sp-blockchain", "sp-consensus", "sp-consensus-aura", - "sp-core", - "sp-inherents", - "sp-keystore", - "sp-runtime", - "sp-state-machine", + "sp-core 29.0.0", + "sp-inherents 27.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "sp-timestamp", "substrate-prometheus-endpoint", "tracing", @@ -2050,10 +2066,10 @@ dependencies = [ "sp-blockchain", "sp-consensus", "sp-consensus-slots", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-timestamp", - "sp-trie", + "sp-trie 30.0.0", "substrate-prometheus-endpoint", "tracing", ] @@ -2068,9 +2084,9 @@ dependencies = [ "async-trait", "cumulus-primitives-parachain-inherent", "sp-consensus", - "sp-inherents", - "sp-runtime", - "sp-state-machine", + "sp-inherents 27.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "thiserror", ] @@ -2092,9 +2108,9 @@ dependencies = [ "sc-client-api", "sp-blockchain", "sp-consensus", - "sp-core", - "sp-runtime", - "sp-state-machine", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "tracing", ] @@ -2112,14 +2128,14 @@ dependencies = [ "parity-scale-codec", "sc-client-api", "scale-info", - "sp-api", + "sp-api 27.0.1", "sp-crypto-hashing", - "sp-inherents", - "sp-runtime", - "sp-state-machine", + "sp-inherents 27.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "sp-std", - "sp-storage", - "sp-trie", + "sp-storage 20.0.0", + "sp-trie 30.0.0", "tracing", ] @@ -2144,7 +2160,7 @@ dependencies = [ "sc-consensus", "sp-consensus", "sp-maybe-compressed-blob", - "sp-runtime", + "sp-runtime 32.0.0", "tracing", ] @@ -2177,11 +2193,11 @@ dependencies = [ "sc-telemetry", "sc-transaction-pool", "sc-utils", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", "sp-consensus", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-transaction-pool", ] @@ -2192,15 +2208,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e8e78b18548ae3454bc8a46e2bc2e3f521ea547844cbaecc9344d4741f4b1ef" dependencies = [ "cumulus-pallet-parachain-system", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "pallet-aura", "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-aura", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -2217,8 +2233,8 @@ dependencies = [ "cumulus-primitives-proof-size-hostfunction", "environmental", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "impl-trait-for-tuples", "log", "pallet-message-queue", @@ -2227,17 +2243,17 @@ dependencies = [ "polkadot-runtime-common", "polkadot-runtime-parachains", "scale-info", - "sp-core", - "sp-externalities", - "sp-inherents", - "sp-io", - "sp-runtime", - "sp-state-machine", + "sp-core 29.0.0", + "sp-externalities 0.26.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "sp-std", - "sp-trie", - "sp-version", + "sp-trie 30.0.0", + "sp-version 30.0.0", "staging-xcm", - "trie-db", + "trie-db 0.28.0", ] [[package]] @@ -2259,11 +2275,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f3259f743f70f39baa3abf2d9d8de864e18120465f8731b99bef039a3bf9329" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "pallet-session", "parity-scale-codec", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -2274,12 +2290,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e802291060763f8d1176bf808da97aafe5afe7351f62bb093c317c1d35c5cee" dependencies = [ "cumulus-primitives-core", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", "staging-xcm", ] @@ -2294,17 +2310,17 @@ dependencies = [ "bp-xcm-bridge-hub-router", "cumulus-primitives-core", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-message-queue", "parity-scale-codec", "polkadot-runtime-common", "polkadot-runtime-parachains", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", "staging-xcm", "staging-xcm-executor", @@ -2319,9 +2335,9 @@ dependencies = [ "parity-scale-codec", "polkadot-core-primitives", "polkadot-primitives", - "sp-api", + "sp-api 27.0.1", "sp-consensus-aura", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -2336,10 +2352,10 @@ dependencies = [ "polkadot-parachain-primitives", "polkadot-primitives", "scale-info", - "sp-api", - "sp-runtime", + "sp-api 27.0.1", + "sp-runtime 32.0.0", "sp-std", - "sp-trie", + "sp-trie 30.0.0", "staging-xcm", ] @@ -2353,10 +2369,10 @@ dependencies = [ "cumulus-primitives-core", "parity-scale-codec", "scale-info", - "sp-core", - "sp-inherents", + "sp-core 29.0.0", + "sp-inherents 27.0.0", "sp-std", - "sp-trie", + "sp-trie 30.0.0", ] [[package]] @@ -2365,9 +2381,9 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1b74f9141190b9f4bf96a947ade46da64097b77f1ebfa8d611c81724250e119" dependencies = [ - "sp-externalities", - "sp-runtime-interface", - "sp-trie", + "sp-externalities 0.26.0", + "sp-runtime-interface 25.0.0", + "sp-trie 30.0.0", ] [[package]] @@ -2377,14 +2393,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e65466e56d642f979b556d098a03755ae51972fff5fa0f9b1cdcfdb3df062ea3" dependencies = [ "cumulus-primitives-core", - "frame-support", + "frame-support 29.0.2", "log", "pallet-asset-conversion", "parity-scale-codec", "polkadot-runtime-common", "polkadot-runtime-parachains", - "sp-io", - "sp-runtime", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -2409,11 +2425,11 @@ dependencies = [ "sc-sysinfo", "sc-telemetry", "sc-tracing", - "sp-api", + "sp-api 27.0.1", "sp-consensus", - "sp-core", - "sp-runtime", - "sp-state-machine", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", ] [[package]] @@ -2429,9 +2445,9 @@ dependencies = [ "parity-scale-codec", "polkadot-overseer", "sc-client-api", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", - "sp-state-machine", + "sp-state-machine 0.36.0", "thiserror", ] @@ -2467,11 +2483,11 @@ dependencies = [ "sc-service", "sc-tracing", "sc-utils", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", "sp-consensus", "sp-consensus-babe", - "sp-runtime", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "tokio", "tracing", @@ -2502,14 +2518,14 @@ dependencies = [ "serde_json", "smoldot", "smoldot-light", - "sp-api", + "sp-api 27.0.1", "sp-authority-discovery", "sp-consensus-babe", - "sp-core", - "sp-runtime", - "sp-state-machine", - "sp-storage", - "sp-version", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-storage 20.0.0", + "sp-version 30.0.0", "thiserror", "tokio", "tokio-util", @@ -2526,10 +2542,10 @@ dependencies = [ "cumulus-primitives-core", "parity-scale-codec", "polkadot-primitives", - "sp-runtime", - "sp-state-machine", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "sp-std", - "sp-trie", + "sp-trie 30.0.0", ] [[package]] @@ -2556,7 +2572,7 @@ dependencies = [ "curve25519-dalek-derive", "digest 0.10.7", "fiat-crypto", - "rustc_version", + "rustc_version 0.4.0", "subtle 2.4.1", "zeroize", ] @@ -2730,10 +2746,16 @@ dependencies = [ "convert_case", "proc-macro2", "quote", - "rustc_version", + "rustc_version 0.4.0", "syn 2.0.74", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "difflib" version = "0.4.0" @@ -2905,6 +2927,7 @@ dependencies = [ "digest 0.10.7", "elliptic-curve", "rfc6979", + "serdect", "signature", "spki", ] @@ -2984,6 +3007,7 @@ dependencies = [ "pkcs8", "rand_core 0.6.4", "sec1", + "serdect", "subtle 2.4.1", "zeroize", ] @@ -3000,7 +3024,7 @@ dependencies = [ "cumulus-pallet-parachain-system", "cumulus-pallet-xcmp-queue", "cumulus-primitives-core", - "frame-support", + "frame-support 29.0.2", "pallet-assets", "pallet-balances", "pallet-bridge-messages", @@ -3015,8 +3039,8 @@ dependencies = [ "sc-consensus-grandpa", "sp-authority-discovery", "sp-consensus-babe", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "staging-xcm", "xcm-emulator", ] @@ -3249,6 +3273,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + [[package]] name = "fastrand" version = "1.9.0" @@ -3437,23 +3467,23 @@ version = "29.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4090659c6aaa3c4d5b6c6ec909b4b0a25dec10ad92aad5f729efa8d5bd4d806a" dependencies = [ - "frame-support", - "frame-support-procedural", - "frame-system", + "frame-support 29.0.2", + "frame-support-procedural 24.0.0", + "frame-system 29.0.0", "linregress", "log", "parity-scale-codec", "paste", "scale-info", "serde", - "sp-api", - "sp-application-crypto", - "sp-core", - "sp-io", - "sp-runtime", - "sp-runtime-interface", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-runtime-interface 25.0.0", "sp-std", - "sp-storage", + "sp-storage 20.0.0", "static_assertions", ] @@ -3469,8 +3499,8 @@ dependencies = [ "clap", "comfy-table", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "gethostname", "handlebars", "itertools 0.10.5", @@ -3489,19 +3519,19 @@ dependencies = [ "sc-sysinfo", "serde", "serde_json", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-database", - "sp-externalities", - "sp-inherents", - "sp-io", - "sp-keystore", - "sp-runtime", - "sp-state-machine", - "sp-storage", - "sp-trie", - "sp-wasm-interface", + "sp-externalities 0.26.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-storage 20.0.0", + "sp-trie 30.0.0", + "sp-wasm-interface 20.0.0", "thiserror", "thousands", ] @@ -3525,14 +3555,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87da19ee99e6473cd057ead84337d20011fe5e299c6750e88e43b8b7963b8852" dependencies = [ "frame-election-provider-solution-type", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-core", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", "sp-npos-elections", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -3542,17 +3572,17 @@ version = "29.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09bff9574ee2dcc349f646e1d2faadf76afd688c2ea1bbac5e4a0e19a0c19c59" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "frame-try-runtime", "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", - "sp-tracing", + "sp-tracing 16.0.0", ] [[package]] @@ -3575,12 +3605,12 @@ checksum = "5bb1eec9eb46d3e016c95b2fa875118c04609f2150013c56a894cae00581e265" dependencies = [ "array-bytes 6.2.3", "docify", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -3595,11 +3625,11 @@ dependencies = [ "log", "parity-scale-codec", "serde", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-io", - "sp-runtime", - "sp-state-machine", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "spinners", "substrate-rpc-client", "tokio", @@ -3618,7 +3648,49 @@ dependencies = [ "docify", "environmental", "frame-metadata", - "frame-support-procedural", + "frame-support-procedural 24.0.0", + "impl-trait-for-tuples", + "k256", + "log", + "macro_magic", + "parity-scale-codec", + "paste", + "scale-info", + "serde", + "serde_json", + "smallvec", + "sp-api 27.0.1", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-crypto-hashing-proc-macro", + "sp-debug-derive", + "sp-genesis-builder 0.8.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", + "sp-metadata-ir 0.6.0", + "sp-runtime 32.0.0", + "sp-staking 27.0.0", + "sp-state-machine 0.36.0", + "sp-std", + "sp-tracing 16.0.0", + "sp-weights 28.0.0", + "static_assertions", + "tt-call", +] + +[[package]] +name = "frame-support" +version = "37.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cae973c331b7f52ba18435713f9ed02bac20bd4fdedaaad57445d82f05eb9d" +dependencies = [ + "aquamarine", + "array-bytes 6.2.3", + "bitflags 1.3.2", + "docify", + "environmental", + "frame-metadata", + "frame-support-procedural 30.0.2", "impl-trait-for-tuples", "k256", "log", @@ -3629,21 +3701,21 @@ dependencies = [ "serde", "serde_json", "smallvec", - "sp-api", - "sp-arithmetic", - "sp-core", + "sp-api 34.0.0", + "sp-arithmetic 26.0.0", + "sp-core 34.0.0", "sp-crypto-hashing-proc-macro", "sp-debug-derive", - "sp-genesis-builder", - "sp-inherents", - "sp-io", - "sp-metadata-ir", - "sp-runtime", - "sp-staking", - "sp-state-machine", - "sp-std", - "sp-tracing", - "sp-weights", + "sp-genesis-builder 0.15.0", + "sp-inherents 34.0.0", + "sp-io 38.0.0", + "sp-metadata-ir 0.7.0", + "sp-runtime 39.0.0", + "sp-staking 34.0.0", + "sp-state-machine 0.43.0", + "sp-std", + "sp-tracing 17.0.0", + "sp-weights 31.0.0", "static_assertions", "tt-call", ] @@ -3658,7 +3730,7 @@ dependencies = [ "cfg-expr", "derive-syn-parse 0.1.5", "expander 2.2.1", - "frame-support-procedural-tools", + "frame-support-procedural-tools 10.0.0", "itertools 0.10.5", "macro_magic", "proc-macro-warning", @@ -3668,13 +3740,46 @@ dependencies = [ "syn 2.0.74", ] +[[package]] +name = "frame-support-procedural" +version = "30.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e4662a809f559aea6234bd90940fa29df583a3c8124a3cf923f66a0d21126b7" +dependencies = [ + "Inflector", + "cfg-expr", + "derive-syn-parse 0.2.0", + "expander 2.2.1", + "frame-support-procedural-tools 13.0.0", + "itertools 0.11.0", + "macro_magic", + "proc-macro-warning", + "proc-macro2", + "quote", + "sp-crypto-hashing", + "syn 2.0.74", +] + [[package]] name = "frame-support-procedural-tools" version = "10.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3363df38464c47a73eb521a4f648bfcc7537a82d70347ef8af3f73b6d019e910" dependencies = [ - "frame-support-procedural-tools-derive", + "frame-support-procedural-tools-derive 11.0.0", + "proc-macro-crate 3.1.0", + "proc-macro2", + "quote", + "syn 2.0.74", +] + +[[package]] +name = "frame-support-procedural-tools" +version = "13.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bead15a320be1764cdd50458c4cfacb23e0cee65f64f500f8e34136a94c7eeca" +dependencies = [ + "frame-support-procedural-tools-derive 12.0.0", "proc-macro-crate 3.1.0", "proc-macro2", "quote", @@ -3692,6 +3797,17 @@ dependencies = [ "syn 2.0.74", ] +[[package]] +name = "frame-support-procedural-tools-derive" +version = "12.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed971c6435503a099bdac99fe4c5bea08981709e5b5a0a8535a1856f48561191" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.74", +] + [[package]] name = "frame-system" version = "29.0.0" @@ -3700,17 +3816,38 @@ checksum = "5bc20a793c3cec0b11165c1075fe11a255b2491f3eef8230bb3073cb296e7383" dependencies = [ "cfg-if", "docify", - "frame-support", + "frame-support 29.0.2", + "log", + "parity-scale-codec", + "scale-info", + "serde", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std", + "sp-version 30.0.0", + "sp-weights 28.0.0", +] + +[[package]] +name = "frame-system" +version = "37.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "043790fff021477061b207fd6b33743793b63fc64a583358956787229d039717" +dependencies = [ + "cfg-if", + "docify", + "frame-support 37.0.0", "log", "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 34.0.0", + "sp-io 38.0.0", + "sp-runtime 39.0.0", "sp-std", - "sp-version", - "sp-weights", + "sp-version 37.0.0", + "sp-weights 31.0.0", ] [[package]] @@ -3720,12 +3857,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac47ee48fee3a0b49c9ab9ee68997dee3733776a355f780cf2858449cf495d69" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -3736,7 +3873,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c1b20433c3c76b56ce905ed971631ec8c34fa64cf6c20e590afe46455fc0cc8" dependencies = [ "parity-scale-codec", - "sp-api", + "sp-api 27.0.1", ] [[package]] @@ -3745,10 +3882,10 @@ version = "0.35.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eab87d07bc2f9a2160b818d1b7506c303b3b28b6a8a5f01dc5e2641390450b5" dependencies = [ - "frame-support", + "frame-support 29.0.2", "parity-scale-codec", - "sp-api", - "sp-runtime", + "sp-api 27.0.1", + "sp-runtime 32.0.0", "sp-std", ] @@ -4009,11 +4146,21 @@ version = "0.27.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" dependencies = [ - "fallible-iterator", + "fallible-iterator 0.2.0", "indexmap 1.9.3", "stable_deref_trait", ] +[[package]] +name = "gimli" +version = "0.28.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +dependencies = [ + "fallible-iterator 0.3.0", + "stable_deref_trait", +] + [[package]] name = "gimli" version = "0.29.0" @@ -4153,6 +4300,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hex-conservative" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "212ab92002354b4819390025006c897e8140934349e8635c9b077f47b4dcbd20" + [[package]] name = "hex-literal" version = "0.4.1" @@ -4536,7 +4689,7 @@ dependencies = [ "asset-test-utils", "cumulus-primitives-core", "emulated-integration-tests-common", - "frame-support", + "frame-support 29.0.2", "pallet-assets", "pallet-balances", "pallet-message-queue", @@ -4553,8 +4706,8 @@ dependencies = [ "sp-consensus-babe", "sp-consensus-beefy", "sp-consensus-grandpa", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "staging-xcm", "staging-xcm-executor", "tracing-subscriber 0.3.18", @@ -4826,6 +4979,7 @@ dependencies = [ "ecdsa", "elliptic-curve", "once_cell", + "serdect", "sha2 0.10.8", ] @@ -4900,6 +5054,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" +[[package]] +name = "leb128" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" + [[package]] name = "libc" version = "0.2.155" @@ -5772,13 +5932,13 @@ dependencies = [ "parity-scale-codec", "sc-client-api", "sc-offchain", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", "sp-consensus", "sp-consensus-beefy", - "sp-core", + "sp-core 29.0.0", "sp-mmr-primitives", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -5790,11 +5950,11 @@ dependencies = [ "jsonrpsee", "parity-scale-codec", "serde", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-mmr-primitives", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -5824,6 +5984,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "multi-stash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "685a9ac4b61f4e728e1d2c6a7844609c16527aeb5e6c865915c08e619c16410f" + [[package]] name = "multiaddr" version = "0.17.1" @@ -6162,6 +6328,17 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.74", +] + [[package]] name = "num-format" version = "0.4.4" @@ -6230,6 +6407,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "object" +version = "0.32.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" +dependencies = [ + "memchr", +] + [[package]] name = "object" version = "0.36.3" @@ -6325,15 +6511,15 @@ name = "pallet-api" version = "0.1.0" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "pallet-assets", "pallet-balances", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -6344,15 +6530,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4079f12db3cf98daa717337ab5b7e5ef15aa3bec3b497f501dc715d129b500da" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-api", - "sp-arithmetic", - "sp-core", - "sp-io", - "sp-runtime", + "sp-api 27.0.1", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -6362,13 +6548,13 @@ version = "11.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2019e84d65bf6c6105edb61cd6b6f4c6d9a1b347e05d9380e92b0dcf2a29fd7" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "pallet-asset-conversion", "pallet-transaction-payment", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -6379,12 +6565,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "571ce57fd846911041749832b46a8c2b01f0b79ffebcd7585e3973865607036d" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -6395,15 +6581,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed783679921ad8b96807d683d320c314e305753b230d5c04dc713bab7aca64c" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "pallet-transaction-payment", "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -6414,13 +6600,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46728a98a910af13f6a77033dd053456650773bb7adc71e0ba845bff7e31b33e" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -6430,15 +6616,15 @@ version = "28.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a611bef3c8cf281e41a43f32a4153260bdc8b7b61b901e65c7a4442529224e11" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-aura", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -6448,14 +6634,14 @@ version = "29.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7cd9a381c613e6538638391fb51f353fd13b16f849d0d1ac66a388326bd456f1" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "pallet-session", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-authority-discovery", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -6465,12 +6651,12 @@ version = "29.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d83773e731a1760f99684b09961ed7b92acafe335f36f08ebb8313d3b9c72e2" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "impl-trait-for-tuples", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -6481,21 +6667,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3f2020c52667a650d64e84a4bbb63388e25bc1c9bc872a8243d03bfcb285049" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-authorship", "pallet-session", "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-babe", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-session", - "sp-staking", + "sp-staking 27.0.0", "sp-std", ] @@ -6509,17 +6695,17 @@ dependencies = [ "docify", "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-balances", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", - "sp-tracing", + "sp-tracing 16.0.0", ] [[package]] @@ -6530,12 +6716,12 @@ checksum = "a9a54b5d0c7c4c3731883d6b1ac18aff44db20c3d0a3470c8861001a17afdc85" dependencies = [ "docify", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -6545,8 +6731,8 @@ version = "29.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4bedd80e9d8b196f31ea134efd271fdc1b8380ca3aa2d8af6ea8b5a0dc4fa460" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-authorship", "pallet-session", @@ -6554,9 +6740,9 @@ dependencies = [ "scale-info", "serde", "sp-consensus-beefy", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", - "sp-staking", + "sp-staking 27.0.0", "sp-std", ] @@ -6568,8 +6754,8 @@ checksum = "7d334f24d3c0c016d16aa87d069485847d622e8ebebace18ec5cf56609ca3a67" dependencies = [ "array-bytes 6.2.3", "binary-merkle-tree", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-beefy", "pallet-mmr", @@ -6577,12 +6763,12 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-api", + "sp-api 27.0.1", "sp-consensus-beefy", - "sp-core", - "sp-io", - "sp-runtime", - "sp-state-machine", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "sp-std", ] @@ -6593,15 +6779,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4765879e96676c13cdbed746d66fd59dcde1e9e65fda1f064fa2fffa3bc5d597" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-treasury", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -6616,15 +6802,15 @@ dependencies = [ "bp-test-utils", "finality-grandpa", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", "sp-consensus-grandpa", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", - "sp-trie", + "sp-trie 30.0.0", ] [[package]] @@ -6636,13 +6822,13 @@ dependencies = [ "bp-messages", "bp-runtime", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "num-traits", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -6657,15 +6843,15 @@ dependencies = [ "bp-polkadot-core", "bp-runtime", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-bridge-grandpa", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", - "sp-trie", + "sp-trie 30.0.0", ] [[package]] @@ -6678,14 +6864,14 @@ dependencies = [ "bp-relayers", "bp-runtime", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-bridge-messages", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-runtime", + "sp-arithmetic 24.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -6697,14 +6883,14 @@ checksum = "574c52fd629191c374c24a18036acac008ea92142309e5dd05e7f03149a667c3" dependencies = [ "bitvec", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-core", - "sp-runtime", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -6715,16 +6901,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00fd06f2d719f5bb16ab3e836c6b053bbd92631ba694f8c2bf810013b2548167" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-bounties", "pallet-treasury", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -6735,8 +6921,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a36858c4275b7d19671b321e95f545e07c9643f97dffed1b333774cb391a4456" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-authorship", "pallet-balances", @@ -6744,8 +6930,8 @@ dependencies = [ "parity-scale-codec", "rand", "scale-info", - "sp-runtime", - "sp-staking", + "sp-runtime 32.0.0", + "sp-staking 27.0.0", "sp-std", ] @@ -6756,15 +6942,60 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c362a0b8f30895c15ecc7d8c24b0d94bb586c4b9bbd37ac8053b4629d9cc80b" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", + "log", + "parity-scale-codec", + "scale-info", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-std", +] + +[[package]] +name = "pallet-contracts" +version = "27.0.0" +dependencies = [ + "array-bytes 6.2.3", + "assert_matches", + "bitflags 1.3.2", + "environmental", + "frame-benchmarking", + "frame-support 29.0.2", + "frame-system 29.0.0", + "impl-trait-for-tuples", "log", + "pallet-assets", + "pallet-balances", + "pallet-contracts-fixtures", + "pallet-contracts-proc-macro 18.0.0", + "pallet-contracts-uapi 5.0.0", + "pallet-insecure-randomness-collective-flip", + "pallet-message-queue", + "pallet-proxy", + "pallet-timestamp", + "pallet-utility", "parity-scale-codec", + "paste", + "pretty_assertions", + "rand", + "rand_pcg", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "serde", + "smallvec", + "sp-api 27.0.1", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "sp-std", + "sp-tracing 17.0.0", + "staging-xcm", + "staging-xcm-builder", + "wasm-instrument", + "wasmi 0.32.3", + "wat", ] [[package]] @@ -6776,28 +7007,51 @@ dependencies = [ "bitflags 1.3.2", "environmental", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "impl-trait-for-tuples", "log", "pallet-balances", - "pallet-contracts-proc-macro", - "pallet-contracts-uapi", + "pallet-contracts-proc-macro 19.0.0", + "pallet-contracts-uapi 6.0.0", "parity-scale-codec", "rand", "rand_pcg", "scale-info", "serde", "smallvec", - "sp-api", - "sp-core", - "sp-io", - "sp-runtime", + "sp-api 27.0.1", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", "wasm-instrument", - "wasmi", + "wasmi 0.31.2", +] + +[[package]] +name = "pallet-contracts-fixtures" +version = "1.0.0" +dependencies = [ + "anyhow", + "frame-system 29.0.0", + "parity-wasm", + "polkavm-linker", + "sp-runtime 32.0.0", + "tempfile", + "toml 0.8.19", + "twox-hash", +] + +[[package]] +name = "pallet-contracts-proc-macro" +version = "18.0.0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.74", ] [[package]] @@ -6811,6 +7065,17 @@ dependencies = [ "syn 2.0.74", ] +[[package]] +name = "pallet-contracts-uapi" +version = "5.0.0" +dependencies = [ + "bitflags 1.3.2", + "parity-scale-codec", + "paste", + "polkavm-derive 0.9.1", + "scale-info", +] + [[package]] name = "pallet-contracts-uapi" version = "6.0.0" @@ -6820,7 +7085,7 @@ dependencies = [ "bitflags 1.3.2", "parity-scale-codec", "paste", - "polkavm-derive", + "polkavm-derive 0.5.0", "scale-info", ] @@ -6832,13 +7097,13 @@ checksum = "6aee3a8b6fcde893f862993f9d45eb0fcd492dde0967fd56ef78d79fc7b53dc0" dependencies = [ "assert_matches", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", "serde", - "sp-io", - "sp-runtime", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -6849,15 +7114,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa781d632063087bcd3ff46eb1a668f15647ab116f1c8a7c573b7168f62d72c3" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -6869,18 +7134,18 @@ checksum = "b54d1d3fe9ae61a144d581147e699b7c3009169de0019a0f87cca0bed82681e7" dependencies = [ "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-election-provider-support-benchmarking", "parity-scale-codec", "rand", "scale-info", - "sp-arithmetic", - "sp-core", - "sp-io", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", "sp-npos-elections", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", "strum 0.24.1", ] @@ -6893,10 +7158,10 @@ checksum = "46ec87816a1e32a1ab6deececa99e21e6684b111efe87b11b8298328dbbefd01" dependencies = [ "frame-benchmarking", "frame-election-provider-support", - "frame-system", + "frame-system 29.0.0", "parity-scale-codec", "sp-npos-elections", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -6907,16 +7172,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "10cb0158cc7461fda5db04c5791d0df34635bec37181763aca449bade677d12d" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", + "sp-core 29.0.0", + "sp-io 31.0.0", "sp-npos-elections", - "sp-runtime", - "sp-staking", + "sp-runtime 32.0.0", + "sp-staking 27.0.0", "sp-std", ] @@ -6929,14 +7194,14 @@ dependencies = [ "docify", "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", - "sp-staking", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-staking 27.0.0", "sp-std", ] @@ -6947,20 +7212,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a5b20be8592eed7ebca2ee661fc43450088552ebe0bd483d7b101cf5968ab12d" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-authorship", "pallet-session", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-grandpa", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-session", - "sp-staking", + "sp-staking 27.0.0", "sp-std", ] @@ -6972,13 +7237,13 @@ checksum = "452bba25325b7f0148eeecbde13e7c26dfb677ad46b3f160b359d7643b44c94b" dependencies = [ "enumflags2", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -6989,17 +7254,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "598ea5c87351edc953d1f455f32ff456cf2f1daf7bbada1f1e03be8e384852ab" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-authorship", "parity-scale-codec", "scale-info", - "sp-application-crypto", - "sp-core", - "sp-io", - "sp-runtime", - "sp-staking", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-staking 27.0.0", "sp-std", ] @@ -7010,17 +7275,31 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e880ebdb429ca76fb400b1b361ed7fce018a5ea2fc2da4764de5156fffdfa73" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", + "sp-core 29.0.0", + "sp-io 31.0.0", "sp-keyring", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] +[[package]] +name = "pallet-insecure-randomness-collective-flip" +version = "25.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e0875af2d12eb49d57c00f37cfbfba458033c10cfe87114318746381300a0e" +dependencies = [ + "frame-support 37.0.0", + "frame-system 37.1.0", + "parity-scale-codec", + "safe-mix", + "scale-info", + "sp-runtime 39.0.0", +] + [[package]] name = "pallet-membership" version = "29.0.0" @@ -7028,14 +7307,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad901cdf3de23daf23ff8b092ab318b13faebfc1aa4d84263f2fdc84feaf3e9b" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7047,17 +7326,17 @@ checksum = "9ccb23dee70b184a214d729db550117a0965a69107d466d35181d60a6feede38" dependencies = [ "environmental", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-core", - "sp-io", - "sp-runtime", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", - "sp-weights", + "sp-weights 28.0.0", ] [[package]] @@ -7067,15 +7346,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6f1f23a70764dad2b4094d8be12ebbb82df210f2e80dd36fa941a5ac191c6cd" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", + "sp-core 29.0.0", + "sp-io 31.0.0", "sp-mmr-primitives", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -7086,13 +7365,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "176f6a5c170185f892a047c0ae189bc52eb390f2c0b94d4261ed0ebc7f82a548" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7103,14 +7382,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4225c31beb3a10235dd165c78f340c344ee78f6ebccd7c99d62a71fb76d2e39" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-assets", "pallet-nfts", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -7122,14 +7401,14 @@ checksum = "d3a8978bd9c43ac5ebaa7a26e5bd0c130b037d7cde97189e1a62fa64e5ee1ef1" dependencies = [ "enumflags2", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7141,7 +7420,7 @@ checksum = "c412ca82207d43e651ef80a3be837220b82ad0d6c3174922c369ef301ea0e5af" dependencies = [ "pallet-nfts", "parity-scale-codec", - "sp-api", + "sp-api 27.0.1", "sp-std", ] @@ -7152,13 +7431,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a64a0e80dec2c60d5962dd249061a47dc4356db440f26cdec50b8acaded1d3" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-core", - "sp-runtime", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7168,18 +7447,18 @@ version = "26.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b62091305ec7426e71c3da2b0944c2df5a804109ee4d2e8f4fe34865e049f8ac" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-balances", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", - "sp-staking", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-staking 27.0.0", "sp-std", - "sp-tracing", + "sp-tracing 16.0.0", ] [[package]] @@ -7190,16 +7469,16 @@ checksum = "18a1eba3078e2492cad15e4695f90eb3fc570386d9f71f8b81f709c7123fc6b5" dependencies = [ "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "pallet-bags-list", "pallet-nomination-pools", "pallet-staking", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-runtime-interface", - "sp-staking", + "sp-runtime 32.0.0", + "sp-runtime-interface 25.0.0", + "sp-staking 27.0.0", "sp-std", ] @@ -7211,7 +7490,7 @@ checksum = "bc5b35e6c471a669437b987ff02e11e2283412c9ebaeec5334dec3f73bcea652" dependencies = [ "pallet-nomination-pools", "parity-scale-codec", - "sp-api", + "sp-api 27.0.1", "sp-std", ] @@ -7221,15 +7500,15 @@ version = "28.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b5bcfdc4f6032d7570929094fd459de12d840c440c395fb4d365d679e13eda" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-balances", "parity-scale-codec", "scale-info", "serde", - "sp-runtime", - "sp-staking", + "sp-runtime 32.0.0", + "sp-staking 27.0.0", "sp-std", ] @@ -7241,8 +7520,8 @@ checksum = "bbc33e3086c19235cb903cbbbde1bc1c4f428519ad4c23446dc84c75d0061582" dependencies = [ "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-babe", "pallet-balances", @@ -7253,8 +7532,8 @@ dependencies = [ "pallet-staking", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-staking", + "sp-runtime 32.0.0", + "sp-staking 27.0.0", "sp-std", ] @@ -7265,14 +7544,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7344a30c304771beb90aec34604100185e47cdc0366e268ad18922de602a0c7e" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7283,12 +7562,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7aa31a0b91e8060b808c3e3407e4578a5e94503b174b9e99769147b24fb2c56" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7299,16 +7578,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3733dbfc44d8f5e1a08287a9064e5794e9d0e92b1bd68cdad2e22202b1964528" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "impl-trait-for-tuples", "log", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-core", - "sp-io", - "sp-runtime", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7319,12 +7598,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "797b554ddc87082c18223440d61a81cf35ccab6573321ce473a099e7a709a760" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7336,15 +7615,15 @@ checksum = "da850889e7101b63cadb980b7f39df67feb6d63bc6092769b9b708e9eb596db1" dependencies = [ "assert_matches", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", "serde", - "sp-arithmetic", - "sp-io", - "sp-runtime", + "sp-arithmetic 24.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7354,13 +7633,13 @@ version = "5.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59171cbf2b823c13685b1b80dd3e1e84425680ff4e006d8016f8c14d2ec44974" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7372,15 +7651,15 @@ checksum = "45e2a4ebe6a5f98b14a26deed8d7a1ea28bb2c2d3ad4d6dc129a725523a2042d" dependencies = [ "docify", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", - "sp-weights", + "sp-weights 28.0.0", ] [[package]] @@ -7389,21 +7668,21 @@ version = "29.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7412ac59247b300feee53709f7009a23d1c6f8c70528599f48f44e102d896d03" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "impl-trait-for-tuples", "log", "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-session", - "sp-staking", - "sp-state-machine", + "sp-staking 27.0.0", + "sp-state-machine 0.36.0", "sp-std", - "sp-trie", + "sp-trie 30.0.0", ] [[package]] @@ -7413,13 +7692,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9c2731415381020db1e78db8b40207f8423a16099e78f2fde599cbcb57ea8db" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "pallet-session", "pallet-staking", "parity-scale-codec", "rand", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", "sp-std", ] @@ -7431,15 +7710,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dba64f96619c25ae7a0b41f4a5111c2d3102e8b8c6cbce80ece6955e825f9de2" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "rand_chacha 0.2.2", "scale-info", - "sp-arithmetic", - "sp-io", - "sp-runtime", + "sp-arithmetic 24.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7451,8 +7730,8 @@ checksum = "061b00814eb794a40df4eca7972a7c67b26473cd85cc7c54f5816ae49ad6e11b" dependencies = [ "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-authorship", "pallet-session", @@ -7460,10 +7739,10 @@ dependencies = [ "rand_chacha 0.2.2", "scale-info", "serde", - "sp-application-crypto", - "sp-io", - "sp-runtime", - "sp-staking", + "sp-application-crypto 31.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-staking 27.0.0", "sp-std", ] @@ -7486,7 +7765,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "505d45e08bad052f55fb51f00a6b6244d23ee46ffdc8091f6cddf4e3a880319d" dependencies = [ "log", - "sp-arithmetic", + "sp-arithmetic 24.0.0", ] [[package]] @@ -7496,8 +7775,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e47c73850103db30b61ef170107afe1ef0dab6905c495bd6dfb57b3c1dd81bc7" dependencies = [ "parity-scale-codec", - "sp-api", - "sp-staking", + "sp-api 27.0.1", + "sp-staking 27.0.0", ] [[package]] @@ -7507,14 +7786,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d76e52dedc146b7a9c3b7c5a6ff4c4c442a8ab8cc58ec30e90e1e98cdc51ad34" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7526,12 +7805,12 @@ checksum = "b6d02f7855d411913e77e57126f4a8b8a32d90d9bf47d0b747e367a1301729c3" dependencies = [ "docify", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7543,16 +7822,16 @@ checksum = "c1b8810ddfb254c7fb8cd7698229cce513d309a43ff117b38798dae6120f477b" dependencies = [ "docify", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-inherents", - "sp-io", - "sp-runtime", + "sp-inherents 27.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", - "sp-storage", + "sp-storage 20.0.0", "sp-timestamp", ] @@ -7563,16 +7842,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ca4b9921c9e9b59e8eeb64677ba6ec49743ef5fe98e0b63f77411b2b9f6cc99" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-treasury", "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7582,14 +7861,14 @@ version = "29.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5ba71f06f09e955b80dc313c333be3f8d9e8505b051558e0b7af4806b13310" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7602,12 +7881,12 @@ dependencies = [ "jsonrpsee", "pallet-transaction-payment-rpc-runtime-api", "parity-scale-codec", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-rpc", - "sp-runtime", - "sp-weights", + "sp-runtime 32.0.0", + "sp-weights 28.0.0", ] [[package]] @@ -7618,9 +7897,9 @@ checksum = "c78bcba80c7c61712b98a6b5640975ebd25ceb688c18e975af78a0fac81785b0" dependencies = [ "pallet-transaction-payment", "parity-scale-codec", - "sp-api", - "sp-runtime", - "sp-weights", + "sp-api 27.0.1", + "sp-runtime 32.0.0", + "sp-weights 28.0.0", ] [[package]] @@ -7631,15 +7910,15 @@ checksum = "3eca44990d0d759213744f2d1f6fe1fadec1079a3e4e4da40556d6b4e42abbcd" dependencies = [ "docify", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "impl-trait-for-tuples", "pallet-balances", "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7650,12 +7929,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac9449d6e2cdcc4456466eff97a065c43dde678620551f5fd79072dec3b9f560" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -7666,13 +7945,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "954f15b98c3fdebb763bb5cea4ec6803fd180d540ec5b07a9fcb2c118251d52c" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -7683,12 +7962,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4525f3038cdf078fea39d913c563ca626f09a615e7724f0c9eac97743c75ff44" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -7699,12 +7978,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da0ad4ce05688bdddcdb682cbed2f3edff0ee5349f0b745ebacc27d179582432" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-api", - "sp-runtime", + "sp-api 27.0.1", + "sp-runtime 32.0.0", "sp-std", ] @@ -7716,16 +7995,16 @@ checksum = "ba9138b04168b07b1aff4a2079f5514753c31dddba40e5fb471b9cda7da27ad6" dependencies = [ "bounded-collections", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-balances", "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -7739,13 +8018,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c10e1c92086ce2069a3d2387d9431f48660b6ec92054c4d0a4e30a9f54e7ad3" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -7760,13 +8039,13 @@ checksum = "bd5bd3947da7f031c86904f12b6690bbecd2efa122906a8dd838499150fe4322" dependencies = [ "bp-xcm-bridge-hub-router", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -7780,8 +8059,8 @@ checksum = "711a4c073e7c83aac7e414ba16c7c641d6d9e22e6d32f9775ff35b2464ffd7ff" dependencies = [ "cumulus-primitives-core", "cumulus-primitives-utility", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-asset-tx-payment", "pallet-assets", @@ -7794,9 +8073,9 @@ dependencies = [ "polkadot-primitives", "scale-info", "sp-consensus-aura", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", "staging-parachain-info", "staging-xcm", @@ -7815,8 +8094,8 @@ dependencies = [ "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", "cumulus-test-relay-sproof-builder", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "pallet-balances", "pallet-collator-selection", "pallet-session", @@ -7824,17 +8103,30 @@ dependencies = [ "parity-scale-codec", "polkadot-parachain-primitives", "sp-consensus-aura", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", - "sp-tracing", + "sp-tracing 16.0.0", "staging-parachain-info", "staging-xcm", "staging-xcm-executor", "substrate-wasm-builder", ] +[[package]] +name = "parity-bip39" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e69bf016dc406eff7d53a7d3f7cf1c2e72c82b9088aac1118591e36dd2cd3e9" +dependencies = [ + "bitcoin_hashes 0.13.0", + "rand", + "rand_core 0.6.4", + "serde", + "unicode-normalization", +] + [[package]] name = "parity-bytes" version = "0.1.2" @@ -8000,8 +8292,8 @@ dependencies = [ "frame-election-provider-support", "frame-executive", "frame-metadata-hash-extension", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", @@ -8058,27 +8350,27 @@ dependencies = [ "polkadot-runtime-common", "polkadot-runtime-parachains", "scale-info", - "sp-api", - "sp-application-crypto", - "sp-arithmetic", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", + "sp-arithmetic 24.0.0", "sp-authority-discovery", "sp-block-builder", "sp-consensus-babe", "sp-consensus-beefy", - "sp-core", + "sp-core 29.0.0", "sp-debug-derive", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-genesis-builder 0.8.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", "sp-npos-elections", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", - "sp-staking", + "sp-staking 27.0.0", "sp-std", - "sp-storage", + "sp-storage 20.0.0", "sp-transaction-pool", - "sp-version", + "sp-version 30.0.0", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -8090,16 +8382,27 @@ name = "paseo-runtime-constants" version = "1.0.0" source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#2fa66a19eef96bd2c079385057fab2564d09b751" dependencies = [ - "frame-support", + "frame-support 29.0.2", "polkadot-primitives", "polkadot-runtime-common", "smallvec", - "sp-core", - "sp-runtime", - "sp-weights", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-weights 28.0.0", "staging-xcm-builder", ] +[[package]] +name = "password-hash" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "346f04948ba92c43e8469c1ee6736c7563d71012b17d40745260fe106aac2166" +dependencies = [ + "base64ct", + "rand_core 0.6.4", + "subtle 2.4.1", +] + [[package]] name = "paste" version = "1.0.15" @@ -8122,6 +8425,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2" dependencies = [ "digest 0.10.7", + "password-hash", ] [[package]] @@ -8321,8 +8625,8 @@ dependencies = [ "polkadot-primitives", "rand", "schnellru", - "sp-core", - "sp-keystore", + "sp-core 29.0.0", + "sp-keystore 0.35.0", "thiserror", "tracing-gum", ] @@ -8371,8 +8675,8 @@ dependencies = [ "sc-storage-monitor", "sc-sysinfo", "sc-tracing", - "sp-core", - "sp-io", + "sp-core 29.0.0", + "sp-io 31.0.0", "sp-keyring", "sp-maybe-compressed-blob", "substrate-build-script-utils", @@ -8395,9 +8699,9 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "sp-core", - "sp-keystore", - "sp-runtime", + "sp-core 29.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "thiserror", "tokio-util", "tracing-gum", @@ -8411,8 +8715,8 @@ checksum = "b6a08e4e014c853b252ecbbe3ccd67b2d33d78e46988d309b8cccf4ac06e25ef" dependencies = [ "parity-scale-codec", "scale-info", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -8436,8 +8740,8 @@ dependencies = [ "polkadot-primitives", "sc-network", "schnellru", - "sp-application-crypto", - "sp-keystore", + "sp-application-crypto 31.0.0", + "sp-keystore 0.35.0", "thiserror", "tracing-gum", ] @@ -8452,8 +8756,8 @@ dependencies = [ "polkadot-node-primitives", "polkadot-primitives", "reed-solomon-novelpoly", - "sp-core", - "sp-trie", + "sp-core 29.0.0", + "sp-trie 30.0.0", "thiserror", ] @@ -8473,10 +8777,10 @@ dependencies = [ "rand_chacha 0.3.1", "sc-network", "sc-network-common", - "sp-application-crypto", - "sp-core", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-keystore", + "sp-keystore 0.35.0", "tracing-gum", ] @@ -8517,7 +8821,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "sp-core", + "sp-core 29.0.0", "sp-maybe-compressed-blob", "thiserror", "tracing-gum", @@ -8549,10 +8853,10 @@ dependencies = [ "sc-keystore", "schnellru", "schnorrkel 0.11.4", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus", "sp-consensus-slots", - "sp-runtime", + "sp-runtime 32.0.0", "thiserror", "tracing-gum", ] @@ -8596,7 +8900,7 @@ dependencies = [ "polkadot-primitives", "polkadot-statement-table", "schnellru", - "sp-keystore", + "sp-keystore 0.35.0", "thiserror", "tracing-gum", ] @@ -8611,7 +8915,7 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "sp-keystore", + "sp-keystore 0.35.0", "thiserror", "tracing-gum", "wasm-timer", @@ -8705,7 +9009,7 @@ dependencies = [ "polkadot-overseer", "polkadot-primitives", "sp-blockchain", - "sp-inherents", + "sp-inherents 27.0.0", "thiserror", "tracing-gum", ] @@ -8771,9 +9075,9 @@ dependencies = [ "polkadot-primitives", "rand", "slotmap", - "sp-core", + "sp-core 29.0.0", "sp-maybe-compressed-blob", - "sp-wasm-interface", + "sp-wasm-interface 20.0.0", "tempfile", "thiserror", "tokio", @@ -8792,7 +9096,7 @@ dependencies = [ "polkadot-node-subsystem-util", "polkadot-overseer", "polkadot-primitives", - "sp-keystore", + "sp-keystore 0.35.0", "thiserror", "tracing-gum", ] @@ -8816,11 +9120,11 @@ dependencies = [ "sc-executor-common", "sc-executor-wasmtime", "seccompiler", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-externalities", - "sp-io", - "sp-tracing", + "sp-externalities 0.26.0", + "sp-io 31.0.0", + "sp-tracing 16.0.0", "thiserror", "tracing-gum", ] @@ -8855,7 +9159,7 @@ dependencies = [ "polkadot-node-primitives", "polkadot-primitives", "sc-network", - "sp-core", + "sp-core 29.0.0", "thiserror", "tokio", ] @@ -8919,12 +9223,12 @@ dependencies = [ "polkadot-primitives", "schnorrkel 0.11.4", "serde", - "sp-application-crypto", + "sp-application-crypto 31.0.0", "sp-consensus-babe", - "sp-core", - "sp-keystore", + "sp-core 29.0.0", + "sp-keystore 0.35.0", "sp-maybe-compressed-blob", - "sp-runtime", + "sp-runtime 32.0.0", "thiserror", "zstd 0.12.4", ] @@ -8960,11 +9264,11 @@ dependencies = [ "sc-network", "sc-transaction-pool-api", "smallvec", - "sp-api", + "sp-api 27.0.1", "sp-authority-discovery", "sp-blockchain", "sp-consensus-babe", - "sp-runtime", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -8998,9 +9302,9 @@ dependencies = [ "rand", "sc-client-api", "schnellru", - "sp-application-crypto", - "sp-core", - "sp-keystore", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", + "sp-keystore 0.35.0", "thiserror", "tracing-gum", ] @@ -9022,8 +9326,8 @@ dependencies = [ "polkadot-node-subsystem-types", "polkadot-primitives", "sc-client-api", - "sp-api", - "sp-core", + "sp-api 27.0.1", + "sp-core 29.0.0", "tikv-jemalloc-ctl", "tracing-gum", ] @@ -9040,10 +9344,10 @@ dependencies = [ "polkadot-core-primitives", "scale-info", "serde", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", - "sp-weights", + "sp-weights 28.0.0", ] [[package]] @@ -9060,17 +9364,17 @@ dependencies = [ "polkadot-parachain-primitives", "scale-info", "serde", - "sp-api", - "sp-application-crypto", - "sp-arithmetic", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", + "sp-arithmetic 24.0.0", "sp-authority-discovery", "sp-consensus-slots", - "sp-core", - "sp-inherents", - "sp-io", - "sp-keystore", - "sp-runtime", - "sp-staking", + "sp-core 29.0.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", + "sp-staking 27.0.0", "sp-std", ] @@ -9097,13 +9401,13 @@ dependencies = [ "sc-rpc-spec-v2", "sc-sync-state-rpc", "sc-transaction-pool-api", - "sp-api", + "sp-api 27.0.1", "sp-block-builder", "sp-blockchain", "sp-consensus", "sp-consensus-babe", - "sp-keystore", - "sp-runtime", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "substrate-frame-rpc-system", "substrate-state-trie-migration-rpc", ] @@ -9117,8 +9421,8 @@ dependencies = [ "bitvec", "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "impl-trait-for-tuples", "libsecp256k1", "log", @@ -9145,14 +9449,14 @@ dependencies = [ "serde", "serde_derive", "slot-range-helper", - "sp-api", - "sp-core", - "sp-inherents", - "sp-io", + "sp-api 27.0.1", + "sp-core 29.0.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", "sp-npos-elections", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", - "sp-staking", + "sp-staking 27.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -9171,7 +9475,7 @@ dependencies = [ "parity-scale-codec", "polkadot-primitives", "sp-std", - "sp-tracing", + "sp-tracing 16.0.0", ] [[package]] @@ -9184,8 +9488,8 @@ dependencies = [ "bitvec", "derive_more", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "impl-trait-for-tuples", "log", "pallet-authority-discovery", @@ -9208,16 +9512,16 @@ dependencies = [ "rustc-hex", "scale-info", "serde", - "sp-api", - "sp-application-crypto", - "sp-arithmetic", - "sp-core", - "sp-inherents", - "sp-io", - "sp-keystore", - "sp-runtime", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "sp-session", - "sp-staking", + "sp-staking 27.0.0", "sp-std", "staging-xcm", "staging-xcm-executor", @@ -9233,8 +9537,8 @@ dependencies = [ "async-trait", "frame-benchmarking", "frame-benchmarking-cli", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "frame-system-rpc-runtime-api", "futures", "hex-literal", @@ -9313,7 +9617,7 @@ dependencies = [ "schnellru", "serde", "serde_json", - "sp-api", + "sp-api 27.0.1", "sp-authority-discovery", "sp-block-builder", "sp-blockchain", @@ -9321,21 +9625,21 @@ dependencies = [ "sp-consensus-babe", "sp-consensus-beefy", "sp-consensus-grandpa", - "sp-core", - "sp-inherents", - "sp-io", + "sp-core 29.0.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", "sp-keyring", - "sp-keystore", + "sp-keystore 0.35.0", "sp-mmr-primitives", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", - "sp-state-machine", - "sp-storage", + "sp-state-machine 0.36.0", + "sp-storage 20.0.0", "sp-timestamp", "sp-transaction-pool", - "sp-version", - "sp-weights", + "sp-version 30.0.0", + "sp-weights 28.0.0", "substrate-prometheus-endpoint", "thiserror", "tracing-gum", @@ -9360,8 +9664,8 @@ dependencies = [ "polkadot-node-subsystem", "polkadot-node-subsystem-util", "polkadot-primitives", - "sp-keystore", - "sp-staking", + "sp-keystore 0.35.0", + "sp-staking 27.0.0", "thiserror", "tracing-gum", ] @@ -9374,7 +9678,7 @@ checksum = "de5e010da3c6a65d8f263d0f825a04d995ffc8a37f886f674fcbbc73bf158d01" dependencies = [ "parity-scale-codec", "polkadot-primitives", - "sp-core", + "sp-core 29.0.0", "tracing-gum", ] @@ -9384,55 +9688,107 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "88b4e215c80fe876147f3d58158d5dfeae7dabdd6047e175af77095b78d0035c" +[[package]] +name = "polkavm-common" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d9428a5cfcc85c5d7b9fc4b6a18c4b802d0173d768182a51cc7751640f08b92" + [[package]] name = "polkavm-derive" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6380dbe1fb03ecc74ad55d841cfc75480222d153ba69ddcb00977866cbdabdb8" dependencies = [ - "polkavm-derive-impl", + "polkavm-derive-impl 0.5.0", "syn 2.0.74", ] +[[package]] +name = "polkavm-derive" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae8c4bea6f3e11cd89bb18bcdddac10bd9a24015399bd1c485ad68a985a19606" +dependencies = [ + "polkavm-derive-impl-macro", +] + [[package]] name = "polkavm-derive-impl" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc8211b3365bbafb2fb32057d68b0e1ca55d079f5cf6f9da9b98079b94b3987d" dependencies = [ - "polkavm-common", + "polkavm-common 0.5.0", "proc-macro2", "quote", "syn 2.0.74", ] [[package]] -name = "polling" -version = "2.8.0" +name = "polkavm-derive-impl" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce" +checksum = "5c4fdfc49717fb9a196e74a5d28e0bc764eb394a2c803eb11133a31ac996c60c" dependencies = [ - "autocfg", - "bitflags 1.3.2", - "cfg-if", - "concurrent-queue", - "libc", - "log", - "pin-project-lite 0.2.14", - "windows-sys 0.48.0", + "polkavm-common 0.9.0", + "proc-macro2", + "quote", + "syn 2.0.74", ] [[package]] -name = "polling" -version = "3.7.3" +name = "polkavm-derive-impl-macro" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc2790cd301dec6cd3b7a025e4815cf825724a51c98dccfe6a3e55f05ffb6511" +checksum = "8ba81f7b5faac81e528eb6158a6f3c9e0bb1008e0ffa19653bc8dea925ecb429" dependencies = [ - "cfg-if", - "concurrent-queue", - "hermit-abi 0.4.0", - "pin-project-lite 0.2.14", - "rustix 0.38.34", + "polkavm-derive-impl 0.9.0", + "syn 2.0.74", +] + +[[package]] +name = "polkavm-linker" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c7be503e60cf56c0eb785f90aaba4b583b36bff00e93997d93fef97f9553c39" +dependencies = [ + "gimli 0.28.1", + "hashbrown 0.14.5", + "log", + "object 0.32.2", + "polkavm-common 0.9.0", + "regalloc2 0.9.3", + "rustc-demangle", +] + +[[package]] +name = "polling" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce" +dependencies = [ + "autocfg", + "bitflags 1.3.2", + "cfg-if", + "concurrent-queue", + "libc", + "log", + "pin-project-lite 0.2.14", + "windows-sys 0.48.0", +] + +[[package]] +name = "polling" +version = "3.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc2790cd301dec6cd3b7a025e4815cf825724a51c98dccfe6a3e55f05ffb6511" +dependencies = [ + "cfg-if", + "concurrent-queue", + "hermit-abi 0.4.0", + "pin-project-lite 0.2.14", + "rustix 0.38.34", "tracing", "windows-sys 0.59.0", ] @@ -9464,15 +9820,15 @@ dependencies = [ name = "pop-chain-extension" version = "0.1.0" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", - "pallet-contracts", + "pallet-contracts 28.0.0", "parity-scale-codec", "pop-primitives", "rand", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -9522,15 +9878,15 @@ dependencies = [ "sc-transaction-pool-api", "serde", "serde_json", - "sp-api", + "sp-api 27.0.1", "sp-block-builder", "sp-blockchain", "sp-consensus-aura", - "sp-core", - "sp-io", - "sp-keystore", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-keystore 0.35.0", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", "sp-timestamp", "sp-transaction-pool", @@ -9552,12 +9908,12 @@ dependencies = [ name = "pop-runtime-common" version = "0.0.0" dependencies = [ - "frame-support", + "frame-support 29.0.2", "parachains-common", "parity-scale-codec", "polkadot-primitives", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -9576,8 +9932,8 @@ dependencies = [ "env_logger 0.11.5", "frame-benchmarking", "frame-executive", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", @@ -9590,7 +9946,7 @@ dependencies = [ "pallet-authorship", "pallet-balances", "pallet-collator-selection", - "pallet-contracts", + "pallet-contracts 28.0.0", "pallet-message-queue", "pallet-multisig", "pallet-nft-fractionalization", @@ -9615,19 +9971,19 @@ dependencies = [ "pop-runtime-common", "scale-info", "smallvec", - "sp-api", + "sp-api 27.0.1", "sp-block-builder", "sp-consensus-aura", - "sp-core", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-core 29.0.0", + "sp-genesis-builder 0.8.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", "sp-std", "sp-transaction-pool", - "sp-version", + "sp-version 30.0.0", "staging-parachain-info", "staging-xcm", "staging-xcm-builder", @@ -9650,8 +10006,8 @@ dependencies = [ "env_logger 0.11.5", "frame-benchmarking", "frame-executive", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", @@ -9663,7 +10019,7 @@ dependencies = [ "pallet-authorship", "pallet-balances", "pallet-collator-selection", - "pallet-contracts", + "pallet-contracts 28.0.0", "pallet-message-queue", "pallet-multisig", "pallet-nft-fractionalization", @@ -9687,19 +10043,19 @@ dependencies = [ "pop-runtime-common", "scale-info", "smallvec", - "sp-api", + "sp-api 27.0.1", "sp-block-builder", "sp-consensus-aura", - "sp-core", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-core 29.0.0", + "sp-genesis-builder 0.8.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", "sp-std", "sp-transaction-pool", - "sp-version", + "sp-version 30.0.0", "staging-parachain-info", "staging-xcm", "staging-xcm-builder", @@ -9758,6 +10114,16 @@ dependencies = [ "termtree", ] +[[package]] +name = "pretty_assertions" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "prettyplease" version = "0.1.25" @@ -10255,6 +10621,19 @@ dependencies = [ "smallvec", ] +[[package]] +name = "regalloc2" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad156d539c879b7a24a363a2016d77961786e71f48f2e2fc8302a92abd2429a6" +dependencies = [ + "hashbrown 0.13.2", + "log", + "rustc-hash", + "slice-group-by", + "smallvec", +] + [[package]] name = "regex" version = "1.10.6" @@ -10388,8 +10767,8 @@ dependencies = [ "frame-benchmarking", "frame-executive", "frame-metadata-hash-extension", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", @@ -10450,25 +10829,25 @@ dependencies = [ "serde", "serde_derive", "smallvec", - "sp-api", - "sp-arithmetic", + "sp-api 27.0.1", + "sp-arithmetic 24.0.0", "sp-authority-discovery", "sp-block-builder", "sp-consensus-babe", "sp-consensus-beefy", - "sp-core", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-core 29.0.0", + "sp-genesis-builder 0.8.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", "sp-mmr-primitives", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", - "sp-staking", + "sp-staking 27.0.0", "sp-std", - "sp-storage", + "sp-storage 20.0.0", "sp-transaction-pool", - "sp-version", + "sp-version 30.0.0", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -10482,13 +10861,13 @@ version = "8.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b45c21ccb0f8777512a65510c106aeee4b59682944b9a5cb31cd7b8ed4ccb47" dependencies = [ - "frame-support", + "frame-support 29.0.2", "polkadot-primitives", "polkadot-runtime-common", "smallvec", - "sp-core", - "sp-runtime", - "sp-weights", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-weights 28.0.0", "staging-xcm", "staging-xcm-builder", ] @@ -10553,6 +10932,15 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver 0.9.0", +] + [[package]] name = "rustc_version" version = "0.4.0" @@ -10701,6 +11089,15 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "safe-mix" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d3d055a2582e6b00ed7a31c1524040aa391092bf636328350813f3a0605215c" +dependencies = [ + "rustc_version 0.2.3", +] + [[package]] name = "safe_arch" version = "0.7.2" @@ -10726,8 +11123,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "357127c91373ed6d1ae582f6e3300ab5b13bcde43bbf270a891f44194ef48b70" dependencies = [ "log", - "sp-core", - "sp-wasm-interface", + "sp-core 29.0.0", + "sp-wasm-interface 20.0.0", "thiserror", ] @@ -10751,12 +11148,12 @@ dependencies = [ "rand", "sc-client-api", "sc-network", - "sp-api", + "sp-api 27.0.1", "sp-authority-discovery", "sp-blockchain", - "sp-core", - "sp-keystore", - "sp-runtime", + "sp-core 29.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -10775,12 +11172,12 @@ dependencies = [ "sc-proposer-metrics", "sc-telemetry", "sc-transaction-pool-api", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", "sp-consensus", - "sp-core", - "sp-inherents", - "sp-runtime", + "sp-core 29.0.0", + "sp-inherents 27.0.0", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", ] @@ -10791,13 +11188,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e8b0640994965c6ff3afa13242d95a61611b83da21fd86ac2b1ebd03e241a02" dependencies = [ "parity-scale-codec", - "sp-api", + "sp-api 27.0.1", "sp-block-builder", "sp-blockchain", - "sp-core", - "sp-inherents", - "sp-runtime", - "sp-trie", + "sp-core 29.0.0", + "sp-inherents 27.0.0", + "sp-runtime 32.0.0", + "sp-trie 30.0.0", ] [[package]] @@ -10819,12 +11216,12 @@ dependencies = [ "serde", "serde_json", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-genesis-builder", - "sp-io", - "sp-runtime", - "sp-state-machine", + "sp-genesis-builder 0.8.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", ] [[package]] @@ -10871,12 +11268,12 @@ dependencies = [ "serde", "serde_json", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-keyring", - "sp-keystore", + "sp-keystore 0.35.0", "sp-panic-handler", - "sp-runtime", - "sp-version", + "sp-runtime 32.0.0", + "sp-version 30.0.0", "thiserror", "tokio", ] @@ -10895,17 +11292,17 @@ dependencies = [ "sc-executor", "sc-transaction-pool-api", "sc-utils", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", "sp-consensus", - "sp-core", + "sp-core 29.0.0", "sp-database", - "sp-externalities", - "sp-runtime", - "sp-state-machine", + "sp-externalities 0.26.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "sp-statement-store", - "sp-storage", - "sp-trie", + "sp-storage 20.0.0", + "sp-trie 30.0.0", "substrate-prometheus-endpoint", ] @@ -10927,13 +11324,13 @@ dependencies = [ "sc-client-api", "sc-state-db", "schnellru", - "sp-arithmetic", + "sp-arithmetic 24.0.0", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-database", - "sp-runtime", - "sp-state-machine", - "sp-trie", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-trie 30.0.0", ] [[package]] @@ -10952,12 +11349,12 @@ dependencies = [ "sc-client-api", "sc-utils", "serde", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", "sp-consensus", - "sp-core", - "sp-runtime", - "sp-state-machine", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -10977,17 +11374,17 @@ dependencies = [ "sc-consensus", "sc-consensus-slots", "sc-telemetry", - "sp-api", - "sp-application-crypto", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", "sp-block-builder", "sp-blockchain", "sp-consensus", "sp-consensus-aura", "sp-consensus-slots", - "sp-core", - "sp-inherents", - "sp-keystore", - "sp-runtime", + "sp-core 29.0.0", + "sp-inherents 27.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -11013,18 +11410,18 @@ dependencies = [ "sc-consensus-slots", "sc-telemetry", "sc-transaction-pool-api", - "sp-api", - "sp-application-crypto", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", "sp-block-builder", "sp-blockchain", "sp-consensus", "sp-consensus-babe", "sp-consensus-slots", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-inherents", - "sp-keystore", - "sp-runtime", + "sp-inherents 27.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -11041,14 +11438,14 @@ dependencies = [ "sc-consensus-epochs", "sc-rpc-api", "serde", - "sp-api", - "sp-application-crypto", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", "sp-blockchain", "sp-consensus", "sp-consensus-babe", - "sp-core", - "sp-keystore", - "sp-runtime", + "sp-core 29.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "thiserror", ] @@ -11072,17 +11469,17 @@ dependencies = [ "sc-network-gossip", "sc-network-sync", "sc-utils", - "sp-api", - "sp-application-crypto", - "sp-arithmetic", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", + "sp-arithmetic 24.0.0", "sp-blockchain", "sp-consensus", "sp-consensus-beefy", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-keystore", + "sp-keystore 0.35.0", "sp-mmr-primitives", - "sp-runtime", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", "tokio", @@ -11104,8 +11501,8 @@ dependencies = [ "sc-rpc", "serde", "sp-consensus-beefy", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "thiserror", ] @@ -11120,7 +11517,7 @@ dependencies = [ "sc-client-api", "sc-consensus", "sp-blockchain", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -11153,16 +11550,16 @@ dependencies = [ "sc-transaction-pool-api", "sc-utils", "serde_json", - "sp-api", - "sp-application-crypto", - "sp-arithmetic", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", + "sp-arithmetic 24.0.0", "sp-blockchain", "sp-consensus", "sp-consensus-grandpa", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-keystore", - "sp-runtime", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -11183,8 +11580,8 @@ dependencies = [ "sc-rpc", "serde", "sp-blockchain", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "thiserror", ] @@ -11202,14 +11599,14 @@ dependencies = [ "sc-client-api", "sc-consensus", "sc-telemetry", - "sp-arithmetic", + "sp-arithmetic 24.0.0", "sp-blockchain", "sp-consensus", "sp-consensus-slots", - "sp-core", - "sp-inherents", - "sp-runtime", - "sp-state-machine", + "sp-core 29.0.0", + "sp-inherents 27.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", ] [[package]] @@ -11223,15 +11620,15 @@ dependencies = [ "sc-executor-common", "sc-executor-wasmtime", "schnellru", - "sp-api", - "sp-core", - "sp-externalities", - "sp-io", + "sp-api 27.0.1", + "sp-core 29.0.0", + "sp-externalities 0.26.0", + "sp-io 31.0.0", "sp-panic-handler", - "sp-runtime-interface", - "sp-trie", - "sp-version", - "sp-wasm-interface", + "sp-runtime-interface 25.0.0", + "sp-trie 30.0.0", + "sp-version 30.0.0", + "sp-wasm-interface 20.0.0", "tracing", ] @@ -11243,7 +11640,7 @@ checksum = "07498138dee3ddf2c71299ca372d8449880bb3a8a8a299a483094e9c26b0823e" dependencies = [ "sc-allocator", "sp-maybe-compressed-blob", - "sp-wasm-interface", + "sp-wasm-interface 20.0.0", "thiserror", "wasm-instrument", ] @@ -11262,8 +11659,8 @@ dependencies = [ "rustix 0.36.17", "sc-allocator", "sc-executor-common", - "sp-runtime-interface", - "sp-wasm-interface", + "sp-runtime-interface 25.0.0", + "sp-wasm-interface 20.0.0", "wasmtime", ] @@ -11282,7 +11679,7 @@ dependencies = [ "sc-network-common", "sc-network-sync", "sp-blockchain", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -11294,9 +11691,9 @@ dependencies = [ "array-bytes 6.2.3", "parking_lot 0.12.3", "serde_json", - "sp-application-crypto", - "sp-core", - "sp-keystore", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", + "sp-keystore 0.35.0", "thiserror", ] @@ -11321,12 +11718,12 @@ dependencies = [ "sc-client-api", "sc-network", "sc-transaction-pool-api", - "sp-api", + "sp-api 27.0.1", "sp-consensus", - "sp-core", - "sp-keystore", + "sp-core 29.0.0", + "sp-keystore 0.35.0", "sp-mixnet", - "sp-runtime", + "sp-runtime 32.0.0", "thiserror", ] @@ -11361,10 +11758,10 @@ dependencies = [ "serde", "serde_json", "smallvec", - "sp-arithmetic", + "sp-arithmetic 24.0.0", "sp-blockchain", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", "tokio", @@ -11390,7 +11787,7 @@ dependencies = [ "sc-client-api", "sc-network", "sp-blockchain", - "sp-runtime", + "sp-runtime 32.0.0", "thiserror", "unsigned-varint", ] @@ -11410,7 +11807,7 @@ dependencies = [ "sc-consensus", "sp-consensus", "sp-consensus-grandpa", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -11428,7 +11825,7 @@ dependencies = [ "sc-network-common", "sc-network-sync", "schnellru", - "sp-runtime", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "tracing", ] @@ -11450,8 +11847,8 @@ dependencies = [ "sc-client-api", "sc-network", "sp-blockchain", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "thiserror", ] @@ -11480,12 +11877,12 @@ dependencies = [ "sc-utils", "schnellru", "smallvec", - "sp-arithmetic", + "sp-arithmetic 24.0.0", "sp-blockchain", "sp-consensus", "sp-consensus-grandpa", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", "thiserror", "tokio", @@ -11508,7 +11905,7 @@ dependencies = [ "sc-network-sync", "sc-utils", "sp-consensus", - "sp-runtime", + "sp-runtime 32.0.0", "substrate-prometheus-endpoint", ] @@ -11537,12 +11934,12 @@ dependencies = [ "sc-network-common", "sc-transaction-pool-api", "sc-utils", - "sp-api", - "sp-core", - "sp-externalities", - "sp-keystore", + "sp-api 27.0.1", + "sp-core 29.0.0", + "sp-externalities 0.26.0", + "sp-keystore 0.35.0", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "threadpool", "tracing", ] @@ -11577,16 +11974,16 @@ dependencies = [ "sc-transaction-pool-api", "sc-utils", "serde_json", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", - "sp-core", - "sp-keystore", + "sp-core 29.0.0", + "sp-keystore 0.35.0", "sp-offchain", "sp-rpc", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", "sp-statement-store", - "sp-version", + "sp-version 30.0.0", "tokio", ] @@ -11604,10 +12001,10 @@ dependencies = [ "scale-info", "serde", "serde_json", - "sp-core", + "sp-core 29.0.0", "sp-rpc", - "sp-runtime", - "sp-version", + "sp-runtime 32.0.0", + "sp-version 30.0.0", "thiserror", ] @@ -11647,12 +12044,12 @@ dependencies = [ "sc-transaction-pool-api", "sc-utils", "serde", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-rpc", - "sp-runtime", - "sp-version", + "sp-runtime 32.0.0", + "sp-version 30.0.0", "thiserror", "tokio", "tokio-stream", @@ -11699,20 +12096,20 @@ dependencies = [ "sc-utils", "serde", "serde_json", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", "sp-consensus", - "sp-core", - "sp-externalities", - "sp-keystore", - "sp-runtime", + "sp-core 29.0.0", + "sp-externalities 0.26.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "sp-session", - "sp-state-machine", - "sp-storage", + "sp-state-machine 0.36.0", + "sp-storage 20.0.0", "sp-transaction-pool", "sp-transaction-storage-proof", - "sp-trie", - "sp-version", + "sp-trie 30.0.0", + "sp-version 30.0.0", "static_init", "substrate-prometheus-endpoint", "tempfile", @@ -11731,7 +12128,7 @@ dependencies = [ "log", "parity-scale-codec", "parking_lot 0.12.3", - "sp-core", + "sp-core 29.0.0", ] [[package]] @@ -11743,7 +12140,7 @@ dependencies = [ "clap", "fs4", "log", - "sp-core", + "sp-core 29.0.0", "thiserror", "tokio", ] @@ -11764,7 +12161,7 @@ dependencies = [ "serde", "serde_json", "sp-blockchain", - "sp-runtime", + "sp-runtime 32.0.0", "thiserror", ] @@ -11784,9 +12181,9 @@ dependencies = [ "sc-telemetry", "serde", "serde_json", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-io", + "sp-io 31.0.0", "sp-std", ] @@ -11829,12 +12226,12 @@ dependencies = [ "sc-client-api", "sc-tracing-proc-macro", "serde", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-rpc", - "sp-runtime", - "sp-tracing", + "sp-runtime 32.0.0", + "sp-tracing 16.0.0", "thiserror", "tracing", "tracing-log 0.1.4", @@ -11870,12 +12267,12 @@ dependencies = [ "sc-transaction-pool-api", "sc-utils", "serde", - "sp-api", + "sp-api 27.0.1", "sp-blockchain", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-runtime", - "sp-tracing", + "sp-runtime 32.0.0", + "sp-tracing 16.0.0", "sp-transaction-pool", "substrate-prometheus-endpoint", "thiserror", @@ -11893,8 +12290,8 @@ dependencies = [ "parity-scale-codec", "serde", "sp-blockchain", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "thiserror", ] @@ -11911,7 +12308,7 @@ dependencies = [ "log", "parking_lot 0.12.3", "prometheus", - "sp-arithmetic", + "sp-arithmetic 24.0.0", ] [[package]] @@ -12027,6 +12424,7 @@ dependencies = [ "der", "generic-array 0.14.7", "pkcs8", + "serdect", "subtle 2.4.1", "zeroize", ] @@ -12099,6 +12497,15 @@ dependencies = [ "semver-parser", ] +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + [[package]] name = "semver" version = "1.0.23" @@ -12173,6 +12580,16 @@ dependencies = [ "serde", ] +[[package]] +name = "serdect" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a84f14a19e9a014bb9f4512488d9829a68e04ecabffb0f9904cd1ace94598177" +dependencies = [ + "base16ct", + "serde", +] + [[package]] name = "sha-1" version = "0.9.8" @@ -12314,7 +12731,7 @@ dependencies = [ "enumn", "parity-scale-codec", "paste", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -12399,7 +12816,7 @@ dependencies = [ "smallvec", "soketto", "twox-hash", - "wasmi", + "wasmi 0.31.2", "x25519-dalek 2.0.1", "zeroize", ] @@ -12458,7 +12875,7 @@ dependencies = [ "curve25519-dalek 4.1.3", "rand_core 0.6.4", "ring 0.17.8", - "rustc_version", + "rustc_version 0.4.0", "sha2 0.10.8", "subtle 2.4.1", ] @@ -12480,8 +12897,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5404af73550b39022e08e5500b30fba627e109a56407b7e80b08da2305b11bfe" dependencies = [ "byte-slice-cast", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "hex", "parity-scale-codec", "rlp", @@ -12489,9 +12906,9 @@ dependencies = [ "serde", "snowbridge-ethereum", "snowbridge-milagro-bls", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", "ssz_rs", "ssz_rs_derive", @@ -12505,18 +12922,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aed4ebefed4c40b9c00e9adf5f02ab2760a7a2dad8bf05110c0013a7a59f4097" dependencies = [ "ethabi-decode", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "hex-literal", "parity-scale-codec", "polkadot-parachain-primitives", "scale-info", "serde", "snowbridge-beacon-primitives", - "sp-arithmetic", - "sp-core", - "sp-io", - "sp-runtime", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -12539,9 +12956,9 @@ dependencies = [ "scale-info", "serde", "serde-big-array", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -12567,17 +12984,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee5cc8e156f033971c5435676be92ab6f70a926b3497ca9c28c0dde9697b8da9" dependencies = [ "ethabi-decode", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "hex-literal", "log", "parity-scale-codec", "scale-info", "serde", "snowbridge-core", - "sp-core", - "sp-io", - "sp-runtime", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -12631,15 +13048,38 @@ dependencies = [ "log", "parity-scale-codec", "scale-info", - "sp-api-proc-macro", - "sp-core", - "sp-externalities", - "sp-metadata-ir", - "sp-runtime", - "sp-state-machine", + "sp-api-proc-macro 15.0.1", + "sp-core 29.0.0", + "sp-externalities 0.26.0", + "sp-metadata-ir 0.6.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "sp-std", - "sp-trie", - "sp-version", + "sp-trie 30.0.0", + "sp-version 30.0.0", + "thiserror", +] + +[[package]] +name = "sp-api" +version = "34.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbce492e0482134128b7729ea36f5ef1a9f9b4de2d48ff8dde7b5e464e28ce75" +dependencies = [ + "docify", + "hash-db", + "log", + "parity-scale-codec", + "scale-info", + "sp-api-proc-macro 20.0.0", + "sp-core 34.0.0", + "sp-externalities 0.29.0", + "sp-metadata-ir 0.7.0", + "sp-runtime 39.0.0", + "sp-runtime-interface 28.0.0", + "sp-state-machine 0.43.0", + "sp-trie 37.0.0", + "sp-version 37.0.0", "thiserror", ] @@ -12658,6 +13098,21 @@ dependencies = [ "syn 2.0.74", ] +[[package]] +name = "sp-api-proc-macro" +version = "20.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9aadf9e97e694f0e343978aa632938c5de309cbcc8afed4136cb71596737278" +dependencies = [ + "Inflector", + "blake2 0.10.6", + "expander 2.2.1", + "proc-macro-crate 3.1.0", + "proc-macro2", + "quote", + "syn 2.0.74", +] + [[package]] name = "sp-application-crypto" version = "31.0.0" @@ -12667,11 +13122,24 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-io", + "sp-core 29.0.0", + "sp-io 31.0.0", "sp-std", ] +[[package]] +name = "sp-application-crypto" +version = "38.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d8133012faa5f75b2f0b1619d9f720c1424ac477152c143e5f7dbde2fe1a958" +dependencies = [ + "parity-scale-codec", + "scale-info", + "serde", + "sp-core 34.0.0", + "sp-io 38.0.0", +] + [[package]] name = "sp-arithmetic" version = "24.0.0" @@ -12687,6 +13155,22 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "sp-arithmetic" +version = "26.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46d0d0a4c591c421d3231ddd5e27d828618c24456d51445d21a1f79fcee97c23" +dependencies = [ + "docify", + "integer-sqrt", + "num-traits", + "parity-scale-codec", + "scale-info", + "serde", + "sp-std", + "static_assertions", +] + [[package]] name = "sp-authority-discovery" version = "27.0.0" @@ -12695,9 +13179,9 @@ checksum = "c92b177c72b5d2973c36d60f6ef942d791d9fd91eae8b08c71882e4118d4fbfc" dependencies = [ "parity-scale-codec", "scale-info", - "sp-api", - "sp-application-crypto", - "sp-runtime", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -12707,9 +13191,9 @@ version = "27.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b36ce171caa7eb2bbe682c089f755fdefa71d3702e4fb1ba30d10146aef99d5" dependencies = [ - "sp-api", - "sp-inherents", - "sp-runtime", + "sp-api 27.0.1", + "sp-inherents 27.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -12724,11 +13208,11 @@ dependencies = [ "parity-scale-codec", "parking_lot 0.12.3", "schnellru", - "sp-api", + "sp-api 27.0.1", "sp-consensus", "sp-database", - "sp-runtime", - "sp-state-machine", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "thiserror", ] @@ -12741,10 +13225,10 @@ dependencies = [ "async-trait", "futures", "log", - "sp-core", - "sp-inherents", - "sp-runtime", - "sp-state-machine", + "sp-core 29.0.0", + "sp-inherents 27.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "thiserror", ] @@ -12757,11 +13241,11 @@ dependencies = [ "async-trait", "parity-scale-codec", "scale-info", - "sp-api", - "sp-application-crypto", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", "sp-consensus-slots", - "sp-inherents", - "sp-runtime", + "sp-inherents 27.0.0", + "sp-runtime 32.0.0", "sp-std", "sp-timestamp", ] @@ -12776,12 +13260,12 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-api", - "sp-application-crypto", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", "sp-consensus-slots", - "sp-core", - "sp-inherents", - "sp-runtime", + "sp-core 29.0.0", + "sp-inherents 27.0.0", + "sp-runtime 32.0.0", "sp-std", "sp-timestamp", ] @@ -12796,13 +13280,13 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-api", - "sp-application-crypto", - "sp-core", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-io", + "sp-io 31.0.0", "sp-mmr-primitives", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", "strum 0.24.1", ] @@ -12818,11 +13302,11 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-api", - "sp-application-crypto", - "sp-core", - "sp-keystore", - "sp-runtime", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -12873,12 +13357,59 @@ dependencies = [ "serde", "sp-crypto-hashing", "sp-debug-derive", - "sp-externalities", - "sp-runtime-interface", + "sp-externalities 0.26.0", + "sp-runtime-interface 25.0.0", + "sp-std", + "sp-storage 20.0.0", + "ss58-registry", + "substrate-bip39 0.4.6", + "thiserror", + "tracing", + "w3f-bls", + "zeroize", +] + +[[package]] +name = "sp-core" +version = "34.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c961a5e33fb2962fa775c044ceba43df9c6f917e2c35d63bfe23738468fa76a7" +dependencies = [ + "array-bytes 6.2.3", + "bitflags 1.3.2", + "blake2 0.10.6", + "bounded-collections", + "bs58 0.5.1", + "dyn-clonable", + "ed25519-zebra 4.0.3", + "futures", + "hash-db", + "hash256-std-hasher", + "impl-serde", + "itertools 0.11.0", + "k256", + "libsecp256k1", + "log", + "merlin", + "parity-bip39", + "parity-scale-codec", + "parking_lot 0.12.3", + "paste", + "primitive-types", + "rand", + "scale-info", + "schnorrkel 0.11.4", + "secp256k1", + "secrecy", + "serde", + "sp-crypto-hashing", + "sp-debug-derive", + "sp-externalities 0.29.0", + "sp-runtime-interface 28.0.0", "sp-std", - "sp-storage", + "sp-storage 21.0.0", "ss58-registry", - "substrate-bip39", + "substrate-bip39 0.6.0", "thiserror", "tracing", "w3f-bls", @@ -12940,7 +13471,18 @@ dependencies = [ "environmental", "parity-scale-codec", "sp-std", - "sp-storage", + "sp-storage 20.0.0", +] + +[[package]] +name = "sp-externalities" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a904407d61cb94228c71b55a9d3708e9d6558991f9e83bd42bd91df37a159d30" +dependencies = [ + "environmental", + "parity-scale-codec", + "sp-storage 21.0.0", ] [[package]] @@ -12950,11 +13492,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd865540ec19479c7349b584ccd78cc34c3f3a628a2a69dbb6365ceec36295ee" dependencies = [ "serde_json", - "sp-api", - "sp-runtime", + "sp-api 27.0.1", + "sp-runtime 32.0.0", "sp-std", ] +[[package]] +name = "sp-genesis-builder" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "182812315871732372325f0ad42bb8b85a093a06dd77ae1eec997259f4c32aef" +dependencies = [ + "parity-scale-codec", + "scale-info", + "serde_json", + "sp-api 34.0.0", + "sp-runtime 39.0.0", +] + [[package]] name = "sp-inherents" version = "27.0.0" @@ -12965,11 +13520,25 @@ dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", "thiserror", ] +[[package]] +name = "sp-inherents" +version = "34.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afffbddc380d99a90c459ba1554bbbc01d62e892de9f1485af6940b89c4c0d57" +dependencies = [ + "async-trait", + "impl-trait-for-tuples", + "parity-scale-codec", + "scale-info", + "sp-runtime 39.0.0", + "thiserror", +] + [[package]] name = "sp-io" version = "31.0.0" @@ -12983,15 +13552,42 @@ dependencies = [ "parity-scale-codec", "rustversion", "secp256k1", - "sp-core", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-externalities", - "sp-keystore", - "sp-runtime-interface", - "sp-state-machine", + "sp-externalities 0.26.0", + "sp-keystore 0.35.0", + "sp-runtime-interface 25.0.0", + "sp-state-machine 0.36.0", "sp-std", - "sp-tracing", - "sp-trie", + "sp-tracing 16.0.0", + "sp-trie 30.0.0", + "tracing", + "tracing-core", +] + +[[package]] +name = "sp-io" +version = "38.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ef7eb561bb4839cc8424ce58c5ea236cbcca83f26fcc0426d8decfe8aa97d4" +dependencies = [ + "bytes", + "docify", + "ed25519-dalek", + "libsecp256k1", + "log", + "parity-scale-codec", + "polkavm-derive 0.9.1", + "rustversion", + "secp256k1", + "sp-core 34.0.0", + "sp-crypto-hashing", + "sp-externalities 0.29.0", + "sp-keystore 0.40.0", + "sp-runtime-interface 28.0.0", + "sp-state-machine 0.43.0", + "sp-tracing 17.0.0", + "sp-trie 37.0.0", "tracing", "tracing-core", ] @@ -13002,8 +13598,8 @@ version = "32.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cf0a2f881958466fc92bc9b39bbc2c0d815ded4a21f8f953372b0ac2e11b02" dependencies = [ - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "strum 0.24.1", ] @@ -13015,11 +13611,23 @@ checksum = "444f2d53968b1ce5e908882710ff1f3873fcf3e95f59d57432daf685bbacb959" dependencies = [ "parity-scale-codec", "parking_lot 0.12.3", - "sp-core", - "sp-externalities", + "sp-core 29.0.0", + "sp-externalities 0.26.0", "thiserror", ] +[[package]] +name = "sp-keystore" +version = "0.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0248b4d784cb4a01472276928977121fa39d977a5bb24793b6b15e64b046df42" +dependencies = [ + "parity-scale-codec", + "parking_lot 0.12.3", + "sp-core 34.0.0", + "sp-externalities 0.29.0", +] + [[package]] name = "sp-maybe-compressed-blob" version = "11.0.0" @@ -13042,6 +13650,17 @@ dependencies = [ "sp-std", ] +[[package]] +name = "sp-metadata-ir" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a616fa51350b35326682a472ee8e6ba742fdacb18babac38ecd46b3e05ead869" +dependencies = [ + "frame-metadata", + "parity-scale-codec", + "scale-info", +] + [[package]] name = "sp-mixnet" version = "0.5.0" @@ -13050,8 +13669,8 @@ checksum = "7bebd44b915c65aeb7e7eeaea466aba3b27cdd915c83ea83d4643c54f21ffbbf" dependencies = [ "parity-scale-codec", "scale-info", - "sp-api", - "sp-application-crypto", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", "sp-std", ] @@ -13066,10 +13685,10 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-api", - "sp-core", + "sp-api 27.0.1", + "sp-core 29.0.0", "sp-debug-derive", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", "thiserror", ] @@ -13083,9 +13702,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-arithmetic", - "sp-core", - "sp-runtime", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", ] @@ -13095,9 +13714,9 @@ version = "27.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d83b955dce0b6d143bec3f60571311168f362b1c16cf044da7037a407b66c19" dependencies = [ - "sp-api", - "sp-core", - "sp-runtime", + "sp-api 27.0.1", + "sp-core 29.0.0", + "sp-runtime 32.0.0", ] [[package]] @@ -13119,7 +13738,7 @@ checksum = "9af4b73fe7ddd88b1641cca90048c4e525e721763199e6fd29c4f590884f4d16" dependencies = [ "rustc-hash", "serde", - "sp-core", + "sp-core 29.0.0", ] [[package]] @@ -13139,12 +13758,39 @@ dependencies = [ "scale-info", "serde", "simple-mermaid", - "sp-application-crypto", - "sp-arithmetic", - "sp-core", - "sp-io", + "sp-application-crypto 31.0.0", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", "sp-std", - "sp-weights", + "sp-weights 28.0.0", +] + +[[package]] +name = "sp-runtime" +version = "39.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5641385c2cd8e2252aacf35e0aff2f236331dfaea8dc11c5a4ec6bb36544450" +dependencies = [ + "docify", + "either", + "hash256-std-hasher", + "impl-trait-for-tuples", + "log", + "num-traits", + "parity-scale-codec", + "paste", + "rand", + "scale-info", + "serde", + "simple-mermaid", + "sp-application-crypto 38.0.0", + "sp-arithmetic 26.0.0", + "sp-core 34.0.0", + "sp-io 38.0.0", + "sp-std", + "sp-weights 31.0.0", + "tracing", ] [[package]] @@ -13157,12 +13803,32 @@ dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", "primitive-types", - "sp-externalities", - "sp-runtime-interface-proc-macro", + "sp-externalities 0.26.0", + "sp-runtime-interface-proc-macro 17.0.0", + "sp-std", + "sp-storage 20.0.0", + "sp-tracing 16.0.0", + "sp-wasm-interface 20.0.0", + "static_assertions", +] + +[[package]] +name = "sp-runtime-interface" +version = "28.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985eb981f40c689c6a0012c937b68ed58dabb4341d06f2dfe4dfd5ed72fa4017" +dependencies = [ + "bytes", + "impl-trait-for-tuples", + "parity-scale-codec", + "polkavm-derive 0.9.1", + "primitive-types", + "sp-externalities 0.29.0", + "sp-runtime-interface-proc-macro 18.0.0", "sp-std", - "sp-storage", - "sp-tracing", - "sp-wasm-interface", + "sp-storage 21.0.0", + "sp-tracing 17.0.0", + "sp-wasm-interface 21.0.0", "static_assertions", ] @@ -13180,6 +13846,20 @@ dependencies = [ "syn 2.0.74", ] +[[package]] +name = "sp-runtime-interface-proc-macro" +version = "18.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0195f32c628fee3ce1dfbbf2e7e52a30ea85f3589da9fe62a8b816d70fc06294" +dependencies = [ + "Inflector", + "expander 2.2.1", + "proc-macro-crate 3.1.0", + "proc-macro2", + "quote", + "syn 2.0.74", +] + [[package]] name = "sp-session" version = "28.0.0" @@ -13188,11 +13868,11 @@ checksum = "3b86531090cc04d2ab3535df07146258e2fb3ab6257b0a77ef14aa08282c3d4a" dependencies = [ "parity-scale-codec", "scale-info", - "sp-api", - "sp-core", - "sp-keystore", - "sp-runtime", - "sp-staking", + "sp-api 27.0.1", + "sp-core 29.0.0", + "sp-keystore 0.35.0", + "sp-runtime 32.0.0", + "sp-staking 27.0.0", "sp-std", ] @@ -13206,11 +13886,25 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", "sp-std", ] +[[package]] +name = "sp-staking" +version = "34.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "143a764cacbab58347d8b2fd4c8909031fb0888d7b02a0ec9fa44f81f780d732" +dependencies = [ + "impl-trait-for-tuples", + "parity-scale-codec", + "scale-info", + "serde", + "sp-core 34.0.0", + "sp-runtime 39.0.0", +] + [[package]] name = "sp-state-machine" version = "0.36.0" @@ -13223,14 +13917,35 @@ dependencies = [ "parking_lot 0.12.3", "rand", "smallvec", - "sp-core", - "sp-externalities", + "sp-core 29.0.0", + "sp-externalities 0.26.0", "sp-panic-handler", "sp-std", - "sp-trie", + "sp-trie 30.0.0", + "thiserror", + "tracing", + "trie-db 0.28.0", +] + +[[package]] +name = "sp-state-machine" +version = "0.43.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "930104d6ae882626e8880d9b1578da9300655d337a3ffb45e130c608b6c89660" +dependencies = [ + "hash-db", + "log", + "parity-scale-codec", + "parking_lot 0.12.3", + "rand", + "smallvec", + "sp-core 34.0.0", + "sp-externalities 0.29.0", + "sp-panic-handler", + "sp-trie 37.0.0", "thiserror", "tracing", - "trie-db", + "trie-db 0.29.1", ] [[package]] @@ -13247,13 +13962,13 @@ dependencies = [ "rand", "scale-info", "sha2 0.10.8", - "sp-api", - "sp-application-crypto", - "sp-core", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-externalities", - "sp-runtime", - "sp-runtime-interface", + "sp-externalities 0.26.0", + "sp-runtime 32.0.0", + "sp-runtime-interface 25.0.0", "sp-std", "thiserror", "x25519-dalek 2.0.1", @@ -13279,6 +13994,19 @@ dependencies = [ "sp-std", ] +[[package]] +name = "sp-storage" +version = "21.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99c82989b3a4979a7e1ad848aad9f5d0b4388f1f454cc131766526601ab9e8f8" +dependencies = [ + "impl-serde", + "parity-scale-codec", + "ref-cast", + "serde", + "sp-debug-derive", +] + [[package]] name = "sp-timestamp" version = "27.0.0" @@ -13287,8 +14015,8 @@ checksum = "249cd06624f2edb53b25af528ab216a508dc9d0870e158b43caac3a97e86699f" dependencies = [ "async-trait", "parity-scale-codec", - "sp-inherents", - "sp-runtime", + "sp-inherents 27.0.0", + "sp-runtime 32.0.0", "sp-std", "thiserror", ] @@ -13306,14 +14034,26 @@ dependencies = [ "tracing-subscriber 0.2.25", ] +[[package]] +name = "sp-tracing" +version = "17.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90b3decf116db9f1dfaf1f1597096b043d0e12c952d3bcdc018c6d6b77deec7e" +dependencies = [ + "parity-scale-codec", + "tracing", + "tracing-core", + "tracing-subscriber 0.2.25", +] + [[package]] name = "sp-transaction-pool" version = "27.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9742861c5330bdcb42856a6eed3d3745b58ee1c92ca4c9260032ff4e6c387165" dependencies = [ - "sp-api", - "sp-runtime", + "sp-api 27.0.1", + "sp-runtime 32.0.0", ] [[package]] @@ -13325,11 +14065,11 @@ dependencies = [ "async-trait", "parity-scale-codec", "scale-info", - "sp-core", - "sp-inherents", - "sp-runtime", + "sp-core 29.0.0", + "sp-inherents 27.0.0", + "sp-runtime 32.0.0", "sp-std", - "sp-trie", + "sp-trie 30.0.0", ] [[package]] @@ -13348,12 +14088,36 @@ dependencies = [ "rand", "scale-info", "schnellru", - "sp-core", - "sp-externalities", + "sp-core 29.0.0", + "sp-externalities 0.26.0", "sp-std", "thiserror", "tracing", - "trie-db", + "trie-db 0.28.0", + "trie-root", +] + +[[package]] +name = "sp-trie" +version = "37.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6282aef9f4b6ecd95a67a45bcdb67a71f4a4155c09a53c10add4ffe823db18cd" +dependencies = [ + "ahash 0.8.11", + "hash-db", + "lazy_static", + "memory-db", + "nohash-hasher", + "parity-scale-codec", + "parking_lot 0.12.3", + "rand", + "scale-info", + "schnellru", + "sp-core 34.0.0", + "sp-externalities 0.29.0", + "thiserror", + "tracing", + "trie-db 0.29.1", "trie-root", ] @@ -13369,9 +14133,27 @@ dependencies = [ "scale-info", "serde", "sp-crypto-hashing-proc-macro", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", - "sp-version-proc-macro", + "sp-version-proc-macro 13.0.0", + "thiserror", +] + +[[package]] +name = "sp-version" +version = "37.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d521a405707b5be561367cd3d442ff67588993de24062ce3adefcf8437ee9fe1" +dependencies = [ + "impl-serde", + "parity-scale-codec", + "parity-wasm", + "scale-info", + "serde", + "sp-crypto-hashing-proc-macro", + "sp-runtime 39.0.0", + "sp-std", + "sp-version-proc-macro 14.0.0", "thiserror", ] @@ -13387,6 +14169,18 @@ dependencies = [ "syn 2.0.74", ] +[[package]] +name = "sp-version-proc-macro" +version = "14.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aee8f6730641a65fcf0c8f9b1e448af4b3bb083d08058b47528188bccc7b7a7" +dependencies = [ + "parity-scale-codec", + "proc-macro2", + "quote", + "syn 2.0.74", +] + [[package]] name = "sp-wasm-interface" version = "20.0.0" @@ -13401,6 +14195,17 @@ dependencies = [ "wasmtime", ] +[[package]] +name = "sp-wasm-interface" +version = "21.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b04b919e150b4736d85089d49327eab65507deb1485eec929af69daa2278eb3" +dependencies = [ + "impl-trait-for-tuples", + "log", + "parity-scale-codec", +] + [[package]] name = "sp-weights" version = "28.0.0" @@ -13412,11 +14217,26 @@ dependencies = [ "scale-info", "serde", "smallvec", - "sp-arithmetic", + "sp-arithmetic 24.0.0", "sp-debug-derive", "sp-std", ] +[[package]] +name = "sp-weights" +version = "31.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93cdaf72a1dad537bbb130ba4d47307ebe5170405280ed1aa31fa712718a400e" +dependencies = [ + "bounded-collections", + "parity-scale-codec", + "scale-info", + "serde", + "smallvec", + "sp-arithmetic 26.0.0", + "sp-debug-derive", +] + [[package]] name = "spin" version = "0.5.2" @@ -13501,11 +14321,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da7dc139d104f676a18c13380a09c3f72d59450a7471116387cbf8cb5f845a0e" dependencies = [ "cumulus-primitives-core", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 32.0.0", "sp-std", ] @@ -13524,7 +14344,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-weights", + "sp-weights 28.0.0", "xcm-procedural", ] @@ -13534,19 +14354,19 @@ version = "8.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b7447c38be3ca9fb21c7434de2243aa6ac74acde8944cda7bb6e2a4f765801" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "impl-trait-for-tuples", "log", "pallet-transaction-payment", "parity-scale-codec", "polkadot-parachain-primitives", "scale-info", - "sp-arithmetic", - "sp-io", - "sp-runtime", + "sp-arithmetic 24.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", - "sp-weights", + "sp-weights 28.0.0", "staging-xcm", "staging-xcm-executor", ] @@ -13559,17 +14379,17 @@ checksum = "74b5c5f2a1d610c5e20e5fae2680c9a28380f305afafeed62f341bfbce57b79a" dependencies = [ "environmental", "frame-benchmarking", - "frame-support", + "frame-support 29.0.2", "impl-trait-for-tuples", "log", "parity-scale-codec", "scale-info", - "sp-arithmetic", - "sp-core", - "sp-io", - "sp-runtime", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", - "sp-weights", + "sp-weights 28.0.0", "staging-xcm", ] @@ -13607,6 +14427,17 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "string-interner" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c6a0d765f5807e98a091107bae0a56ea3799f66a5de47b2c84c94a39c09974e" +dependencies = [ + "cfg-if", + "hashbrown 0.14.5", + "serde", +] + [[package]] name = "strobe-rs" version = "0.8.1" @@ -13680,6 +14511,19 @@ dependencies = [ "zeroize", ] +[[package]] +name = "substrate-bip39" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca58ffd742f693dc13d69bdbb2e642ae239e0053f6aab3b104252892f856700a" +dependencies = [ + "hmac 0.12.1", + "pbkdf2 0.12.2", + "schnorrkel 0.11.4", + "sha2 0.10.8", + "zeroize", +] + [[package]] name = "substrate-build-script-utils" version = "11.0.0" @@ -13699,11 +14543,11 @@ dependencies = [ "parity-scale-codec", "sc-rpc-api", "sc-transaction-pool-api", - "sp-api", + "sp-api 27.0.1", "sp-block-builder", "sp-blockchain", - "sp-core", - "sp-runtime", + "sp-core 29.0.0", + "sp-runtime 32.0.0", ] [[package]] @@ -13730,7 +14574,7 @@ dependencies = [ "log", "sc-rpc-api", "serde", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -13744,11 +14588,11 @@ dependencies = [ "sc-client-api", "sc-rpc-api", "serde", - "sp-core", - "sp-runtime", - "sp-state-machine", - "sp-trie", - "trie-db", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", + "sp-trie 30.0.0", + "trie-db 0.28.0", ] [[package]] @@ -13859,13 +14703,13 @@ name = "system-parachains-constants" version = "1.0.0" source = "git+https://github.com/paseo-network/runtimes/?tag=v1.2.5-system-chains#2fa66a19eef96bd2c079385057fab2564d09b751" dependencies = [ - "frame-support", + "frame-support 29.0.2", "parachains-common", "paseo-runtime-constants", "polkadot-core-primitives", "polkadot-primitives", "smallvec", - "sp-runtime", + "sp-runtime 32.0.0", ] [[package]] @@ -14410,6 +15254,18 @@ dependencies = [ "smallvec", ] +[[package]] +name = "trie-db" +version = "0.29.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c992b4f40c234a074d48a757efeabb1a6be88af84c0c23f7ca158950cb0ae7f" +dependencies = [ + "hash-db", + "log", + "rustc-hex", + "smallvec", +] + [[package]] name = "trie-root" version = "0.18.0" @@ -14488,22 +15344,22 @@ dependencies = [ "sc-executor", "serde", "serde_json", - "sp-api", + "sp-api 27.0.1", "sp-consensus-aura", "sp-consensus-babe", - "sp-core", + "sp-core 29.0.0", "sp-debug-derive", - "sp-externalities", - "sp-inherents", - "sp-io", - "sp-keystore", + "sp-externalities 0.26.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", + "sp-keystore 0.35.0", "sp-rpc", - "sp-runtime", - "sp-state-machine", + "sp-runtime 32.0.0", + "sp-state-machine 0.36.0", "sp-timestamp", "sp-transaction-storage-proof", - "sp-version", - "sp-weights", + "sp-version 30.0.0", + "sp-weights 28.0.0", "substrate-rpc-client", "zstd 0.12.4", ] @@ -14795,6 +15651,15 @@ version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" +[[package]] +name = "wasm-encoder" +version = "0.216.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04c23aebea22c8a75833ae08ed31ccc020835b12a41999e58c31464271b94a88" +dependencies = [ + "leb128", +] + [[package]] name = "wasm-instrument" version = "0.4.0" @@ -14868,7 +15733,24 @@ dependencies = [ "smallvec", "spin 0.9.8", "wasmi_arena", - "wasmi_core", + "wasmi_core 0.13.0", + "wasmparser-nostd", +] + +[[package]] +name = "wasmi" +version = "0.32.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50386c99b9c32bd2ed71a55b6dd4040af2580530fae8bdb9a6576571a80d0cca" +dependencies = [ + "arrayvec 0.7.4", + "multi-stash", + "num-derive", + "num-traits", + "smallvec", + "spin 0.9.8", + "wasmi_collections", + "wasmi_core 0.32.3", "wasmparser-nostd", ] @@ -14878,6 +15760,17 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "104a7f73be44570cac297b3035d76b169d6599637631cf37a1703326a0727073" +[[package]] +name = "wasmi_collections" +version = "0.32.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c128c039340ffd50d4195c3f8ce31aac357f06804cfc494c8b9508d4b30dca4" +dependencies = [ + "ahash 0.8.11", + "hashbrown 0.14.5", + "string-interner", +] + [[package]] name = "wasmi_core" version = "0.13.0" @@ -14890,6 +15783,18 @@ dependencies = [ "paste", ] +[[package]] +name = "wasmi_core" +version = "0.32.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a23b3a7f6c8c3ceeec6b83531ee61f0013c56e51cbf2b14b0f213548b23a4b41" +dependencies = [ + "downcast-rs", + "libm", + "num-traits", + "paste", +] + [[package]] name = "wasmparser" version = "0.102.0" @@ -15104,6 +16009,28 @@ dependencies = [ "wasmparser", ] +[[package]] +name = "wast" +version = "216.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7eb1f2eecd913fdde0dc6c3439d0f24530a98ac6db6cb3d14d92a5328554a08" +dependencies = [ + "bumpalo", + "leb128", + "memchr", + "unicode-width", + "wasm-encoder", +] + +[[package]] +name = "wat" +version = "1.216.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac0409090fb5154f95fb5ba3235675fd9e579e731524d63b6a2f653e1280c82a" +dependencies = [ + "wast", +] + [[package]] name = "web-sys" version = "0.3.70" @@ -15145,8 +16072,8 @@ dependencies = [ "frame-election-provider-support", "frame-executive", "frame-metadata-hash-extension", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", @@ -15213,27 +16140,27 @@ dependencies = [ "serde", "serde_derive", "smallvec", - "sp-api", - "sp-application-crypto", - "sp-arithmetic", + "sp-api 27.0.1", + "sp-application-crypto 31.0.0", + "sp-arithmetic 24.0.0", "sp-authority-discovery", "sp-block-builder", "sp-consensus-babe", "sp-consensus-beefy", - "sp-core", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-core 29.0.0", + "sp-genesis-builder 0.8.0", + "sp-inherents 27.0.0", + "sp-io 31.0.0", "sp-mmr-primitives", "sp-npos-elections", "sp-offchain", - "sp-runtime", + "sp-runtime 32.0.0", "sp-session", - "sp-staking", + "sp-staking 27.0.0", "sp-std", - "sp-storage", + "sp-storage 20.0.0", "sp-transaction-pool", - "sp-version", + "sp-version 30.0.0", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -15247,13 +16174,13 @@ version = "8.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b080c193714605ce1033311d85035247adca170181cd68a3ad7e3ca87755a14" dependencies = [ - "frame-support", + "frame-support 29.0.2", "polkadot-primitives", "polkadot-runtime-common", "smallvec", - "sp-core", - "sp-runtime", - "sp-weights", + "sp-core 29.0.0", + "sp-runtime 32.0.0", + "sp-weights 28.0.0", "staging-xcm", "staging-xcm-builder", ] @@ -15648,8 +16575,8 @@ dependencies = [ "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", "cumulus-test-relay-sproof-builder", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "impl-trait-for-tuples", "lazy_static", "log", @@ -15661,13 +16588,13 @@ dependencies = [ "polkadot-parachain-primitives", "polkadot-primitives", "polkadot-runtime-parachains", - "sp-arithmetic", - "sp-core", + "sp-arithmetic 24.0.0", + "sp-core 29.0.0", "sp-crypto-hashing", - "sp-io", - "sp-runtime", + "sp-io 31.0.0", + "sp-runtime 32.0.0", "sp-std", - "sp-tracing", + "sp-tracing 16.0.0", "staging-xcm", "staging-xcm-executor", ] @@ -15698,6 +16625,12 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" + [[package]] name = "yasna" version = "0.5.2" diff --git a/Cargo.toml b/Cargo.toml index f037f6b7..d783370a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,37 @@ subxt = "0.34.0" subxt-signer = "0.34.0" tokio = { version = "1.36", features = ["macros", "time", "rt-multi-thread"] } +# Experimental pallet-revive dependencies +anyhow = { version = "1.0.81" } +array-bytes = { version = "6.2.2", default-features = false } +assert_matches = { version = "1.5.0" } +bitflags = { version = "1.3.2" } +environmental = { version = "1.1.4", default-features = false } +impl-trait-for-tuples = { version = "0.2.2" } +pallet-revive = { path = "pallets/contracts", default-features = false } +pallet-contracts-fixtures = { path = "pallets/contracts/fixtures", default-features = false } +pallet-contracts-mock-network = { default-features = false, path = "pallets/contracts/mock-network" } +pallet-contracts-proc-macro = { path = "pallets/contracts/proc-macro", default-features = false } +pallet-contracts-uapi = { path = "pallets/contracts/uapi", default-features = false } +pallet-insecure-randomness-collective-flip = { version = "25.0.0", default-features = false } +pretty_assertions = { version = "1.3.0" } +proc-macro2 = { version = "1.0.64" } +polkavm-derive = "0.9.1" +polkavm-linker = "0.9.2" +parity-wasm = { version = "0.45.0" } +paste = { version = "1.0.15", default-features = false } +rand = { version = "0.8.5", default-features = false } +rand_pcg = { version = "0.3.1" } +wasm-instrument = { version = "0.4", default-features = false } +wasmi = { version = "0.32.3", default-features = false } +wat = { version = "1.0.0" } +sp-tracing = { version = "17.0.0", default-features = false } +syn = { version = "2.0.53" } +quote = { version = "1.0.36" } +tempfile = { version = "3.8.1" } +toml = { version = "0.8.8" } +twox-hash = { version = "1.6.3", default-features = false } + # Build substrate-wasm-builder = "18.0.1" substrate-build-script-utils = "11.0.0" diff --git a/pallets/contracts/Cargo.toml b/pallets/contracts/Cargo.toml new file mode 100644 index 00000000..9d0c77f9 --- /dev/null +++ b/pallets/contracts/Cargo.toml @@ -0,0 +1,128 @@ +[package] +name = "pallet-contracts" +version = "27.0.0" +authors.workspace = true +edition.workspace = true +build = "build.rs" +license = "Apache-2.0" +homepage.workspace = true +repository.workspace = true +description = "FRAME pallet for WASM contracts" +readme = "README.md" +include = ["CHANGELOG.md", "README.md", "benchmarks/**", "build.rs", "src/**/*"] + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +paste = { workspace = true } +bitflags = { workspace = true } +codec = { features = ["derive", "max-encoded-len"], workspace = true } +scale-info = { features = ["derive"], workspace = true } +log = { workspace = true } +serde = { optional = true, features = [ + "derive", +], workspace = true, default-features = true } +smallvec = { features = ["const_generics"], workspace = true } +wasmi = { workspace = true } +impl-trait-for-tuples = { workspace = true } + +# Only used in benchmarking to generate contract code +wasm-instrument = { optional = true, workspace = true } +rand = { optional = true, workspace = true } +rand_pcg = { optional = true, workspace = true } + +# Substrate Dependencies +environmental = { workspace = true } +frame-benchmarking = { optional = true, workspace = true } +frame-support = { workspace = true } +frame-system = { workspace = true } +pallet-balances = { optional = true, workspace = true } +pallet-contracts-uapi = { workspace = true, default-features = true } +pallet-contracts-proc-macro = { workspace = true, default-features = true } +sp-api = { workspace = true } +sp-core = { workspace = true } +sp-io = { workspace = true } +sp-runtime = { workspace = true } +sp-std = { workspace = true } + +xcm = { workspace = true } +xcm-builder = { workspace = true } + +[dev-dependencies] +array-bytes = { workspace = true, default-features = true } +assert_matches = { workspace = true } +pretty_assertions = { workspace = true } +wat = { workspace = true } +pallet-contracts-fixtures = { workspace = true } + +# Polkadot Dependencies +xcm-builder = { workspace = true, default-features = true } + +# Substrate Dependencies +pallet-balances = { workspace = true, default-features = true } +pallet-timestamp = { workspace = true, default-features = true } +pallet-message-queue = { workspace = true, default-features = true } +pallet-insecure-randomness-collective-flip = { workspace = true, default-features = true } +pallet-utility = { workspace = true, default-features = true } +pallet-assets = { workspace = true, default-features = true } +pallet-proxy = { workspace = true, default-features = true } +sp-keystore = { workspace = true, default-features = true } +sp-tracing = { workspace = true, default-features = true } + +[features] +default = ["std"] +std = [ + "codec/std", + "environmental/std", + "frame-benchmarking?/std", + "frame-support/std", + "frame-system/std", + "log/std", + "pallet-balances?/std", + "pallet-insecure-randomness-collective-flip/std", + "pallet-proxy/std", + "pallet-timestamp/std", + "pallet-utility/std", + "rand?/std", + "scale-info/std", + "serde", + "sp-api/std", + "sp-core/std", + "sp-io/std", + "sp-keystore/std", + "sp-runtime/std", + "sp-std/std", + "wasm-instrument?/std", + "wasmi/std", + "xcm-builder/std", + "xcm/std", +] +runtime-benchmarks = [ + "frame-benchmarking/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "pallet-assets/runtime-benchmarks", + "pallet-balances/runtime-benchmarks", + "pallet-message-queue/runtime-benchmarks", + "pallet-proxy/runtime-benchmarks", + "pallet-timestamp/runtime-benchmarks", + "pallet-utility/runtime-benchmarks", + "rand", + "rand_pcg", + "sp-runtime/runtime-benchmarks", + "wasm-instrument", + "xcm-builder/runtime-benchmarks", +] +try-runtime = [ + "frame-support/try-runtime", + "frame-system/try-runtime", + "pallet-assets/try-runtime", + "pallet-balances/try-runtime", + "pallet-insecure-randomness-collective-flip/try-runtime", + "pallet-message-queue/try-runtime", + "pallet-proxy/try-runtime", + "pallet-timestamp/try-runtime", + "pallet-utility/try-runtime", + "sp-runtime/try-runtime", +] diff --git a/pallets/contracts/README.md b/pallets/contracts/README.md new file mode 100644 index 00000000..6440f14b --- /dev/null +++ b/pallets/contracts/README.md @@ -0,0 +1,160 @@ +# Contracts Module + +The Contracts module provides functionality for the runtime to deploy and execute WebAssembly smart-contracts. + +- [`Call`](https://paritytech.github.io/substrate/master/pallet_contracts/pallet/enum.Call.html) +- [`Config`](https://paritytech.github.io/substrate/master/pallet_contracts/pallet/trait.Config.html) +- [`Error`](https://paritytech.github.io/substrate/master/pallet_contracts/pallet/enum.Error.html) +- [`Event`](https://paritytech.github.io/substrate/master/pallet_contracts/pallet/enum.Event.html) + +## Overview + +This module extends accounts based on the [`frame_support::traits::fungible`] traits to have smart-contract +functionality. It can be used with other modules that implement accounts based on [`frame_support::traits::fungible`]. +These "smart-contract accounts" have the ability to instantiate smart-contracts and make calls to other contract and +non-contract accounts. + +The smart-contract code is stored once, and later retrievable via its `code_hash`. This means that multiple +smart-contracts can be instantiated from the same `code`, without replicating the code each time. + +When a smart-contract is called, its associated code is retrieved via the code hash and gets executed. This call can +alter the storage entries of the smart-contract account, instantiate new smart-contracts, or call other smart-contracts. + +Finally, when an account is reaped, its associated code and storage of the smart-contract account will also be deleted. + +### Weight + +Senders must specify a [`Weight`](https://paritytech.github.io/substrate/master/sp_weights/struct.Weight.html) limit +with every call, as all instructions invoked by the smart-contract require weight. Unused weight is refunded after the +call, regardless of the execution outcome. + +If the weight limit is reached, then all calls and state changes (including balance transfers) are only reverted at the +current call's contract level. For example, if contract A calls B and B runs out of weight mid-call, then all of B's +calls are reverted. Assuming correct error handling by contract A, A's other calls and state changes still persist. + +One `ref_time` `Weight` is defined as one picosecond of execution time on the runtime's reference machine. + +### Revert Behaviour + +Contract call failures are not cascading. When failures occur in a sub-call, they do not "bubble up", and the call will +only revert at the specific contract level. For example, if contract A calls contract B, and B fails, A can decide how +to handle that failure, either proceeding or reverting A's changes. + +### Off-chain Execution + +In general, a contract execution needs to be deterministic so that all nodes come to the same conclusion when executing +it. To that end we disallow any instructions that could cause indeterminism. Most notable are any floating point +arithmetic. That said, sometimes contracts are executed off-chain and hence are not subject to consensus. If code is +only executed by a single node and implicitly trusted by other actors is such a case. Trusted execution environments +come to mind. To that end we allow the execution of indeterministic code for off-chain usages with the following +constraints: + +1. No contract can ever be instantiated from an indeterministic code. The only way to execute the code is to use a +delegate call from a deterministic contract. +2. The code that wants to use this feature needs to depend on `pallet-contracts` and use +[`bare_call()`](https://paritytech.github.io/substrate/master/pallet_contracts/pallet/struct.Pallet.html#method.bare_call) +directly. This makes sure that by default `pallet-contracts` does not expose any indeterminism. + +#### How to use + +An indeterministic code can be deployed on-chain by passing `Determinism::Relaxed` to +[`upload_code()`](https://paritytech.github.io/substrate/master/pallet_contracts/pallet/struct.Pallet.html#method.upload_code). +A deterministic contract can then delegate call into it if and only if it is ran by using +[`bare_call()`](https://paritytech.github.io/substrate/master/pallet_contracts/pallet/struct.Pallet.html#method.bare_call) +and passing +[`Determinism::Relaxed`](https://paritytech.github.io/substrate/master/pallet_contracts/enum.Determinism.html#variant.Relaxed) +to it. **Never use this argument when the contract is called from an on-chain transaction.** + +## Interface + +### Dispatchable functions + +Those are documented in the [reference +documentation](https://paritytech.github.io/substrate/master/pallet_contracts/index.html#dispatchable-functions). + +### Interface exposed to contracts + +Each contract is one WebAssembly module that looks like this: + +```wat +(module + ;; Invoked by pallet-contracts when a contract is instantiated. + ;; No arguments and empty return type. + (func (export "deploy")) + + ;; Invoked by pallet-contracts when a contract is called. + ;; No arguments and empty return type. + (func (export "call")) + + ;; If a contract uses memory it must be imported. Memory is optional. + ;; The maximum allowed memory size depends on the pallet-contracts configuration. + (import "env" "memory" (memory 1 1)) + + ;; This is one of many functions that can be imported and is implemented by pallet-contracts. + ;; This function is used to copy the result buffer and flags back to the caller. + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) +) +``` + +The documentation of all importable functions can be found +[here](https://paritytech.github.io/substrate/master/pallet_contracts/api_doc/trait.Current.html). + +## Usage + +This module executes WebAssembly smart contracts. These can potentially be written in any language that compiles to +Wasm. However, using a language that specifically targets this module will make things a lot easier. One such language +is [`ink!`](https://use.ink). It enables writing WebAssembly-based smart-contracts in the Rust programming language. + +## Debugging + +Contracts can emit messages to the client when called as RPC through the +[`debug_message`](https://paritytech.github.io/substrate/master/pallet_contracts/api_doc/trait.Current.html#tymethod.debug_message) +API. This is exposed in [ink!](https://use.ink) via +[`ink_env::debug_message()`](https://paritytech.github.io/ink/ink_env/fn.debug_message.html). + +Those messages are gathered into an internal buffer and sent to the RPC client. It is up to the individual client if +and how those messages are presented to the user. + +This buffer is also printed as a debug message. In order to see these messages on the node console the log level for the +`runtime::contracts` target needs to be raised to at least the `debug` level. However, those messages are easy to +overlook because of the noise generated by block production. A good starting point for observing them on the console is +using this command line in the root directory of the Substrate repository: + +```bash +cargo run --release -- --dev -lerror,runtime::contracts=debug +``` + +This raises the log level of `runtime::contracts` to `debug` and all other targets to `error` in order to prevent them +from spamming the console. + +`--dev`: Use a dev chain spec `--tmp`: Use temporary storage for chain data (the chain state is deleted on exit) + +## Host function tracing + +For contract authors, it can be a helpful debugging tool to see which host functions are called, with which arguments, +and what the result was. + +In order to see these messages on the node console, the log level for the `runtime::contracts::strace` target needs to +be raised to the `trace` level. + +Example: + +```bash +cargo run --release -- --dev -lerror,runtime::contracts::strace=trace,runtime::contracts=debug +``` + +## Unstable Interfaces + +Driven by the desire to have an iterative approach in developing new contract interfaces this pallet contains the +concept of an unstable interface. Akin to the rust nightly compiler it allows us to add new interfaces but mark them as +unstable so that contract languages can experiment with them and give feedback before we stabilize those. + +In order to access interfaces marked as `#[unstable]` in [`runtime.rs`](src/wasm/runtime.rs) one need to set +`pallet_contracts::Config::UnsafeUnstableInterface` to `ConstU32`. **It should be obvious that any production +runtime should never be compiled with this feature: In addition to be subject to change or removal those interfaces +might not have proper weights associated with them and are therefore considered unsafe**. + +New interfaces are generally added as unstable and might go through several iterations before they are promoted to a +stable interface. + +License: Apache-2.0 diff --git a/pallets/contracts/build.rs b/pallets/contracts/build.rs new file mode 100644 index 00000000..83d5d368 --- /dev/null +++ b/pallets/contracts/build.rs @@ -0,0 +1,72 @@ +// This file is part of Substrate. + +// 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 std::io::Write; + +/// Get the latest migration version. +/// +/// Find the highest version number from the available migration files. +/// Each migration file should follow the naming convention `vXX.rs`, where `XX` is the version +/// number. +fn get_latest_version() -> u16 { + std::fs::read_dir("src/migration") + .expect("Folder `src/migration` not found.") + .filter_map(|entry| { + let file_name = entry.as_ref().ok()?.file_name(); + let file_name = file_name.to_str()?; + if file_name.starts_with('v') && file_name.ends_with(".rs") { + let version = &file_name[1..&file_name.len() - 3]; + let version = version.parse::().ok()?; + + // Ensure that the version matches the one defined in the file. + let path = entry.unwrap().path(); + let file_content = std::fs::read_to_string(&path).ok()?; + assert!( + file_content.contains(&format!("const VERSION: u16 = {}", version)), + "Invalid MigrationStep::VERSION in {:?}", + path + ); + + return Some(version) + } + None + }) + .max() + .expect("Failed to find any files matching the 'src/migration/vxx.rs' pattern.") +} + +/// Generates a module that exposes the latest migration version, and the benchmark migrations type. +fn main() -> Result<(), Box> { + let out_dir = std::env::var("OUT_DIR")?; + let path = std::path::Path::new(&out_dir).join("migration_codegen.rs"); + let mut f = std::fs::File::create(path)?; + let version = get_latest_version(); + write!( + f, + " + pub mod codegen {{ + use crate::NoopMigration; + /// The latest migration version, pulled from the latest migration file. + pub const LATEST_MIGRATION_VERSION: u16 = {version}; + /// The Migration Steps used for benchmarking the migration framework. + pub type BenchMigrations = (NoopMigration<{}>, NoopMigration<{version}>); + }}", + version - 1, + )?; + + Ok(()) +} diff --git a/pallets/contracts/fixtures/Cargo.toml b/pallets/contracts/fixtures/Cargo.toml new file mode 100644 index 00000000..625c6ebc --- /dev/null +++ b/pallets/contracts/fixtures/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "pallet-contracts-fixtures" +publish = false +version = "1.0.0" +authors.workspace = true +edition.workspace = true +license.workspace = true +description = "Fixtures for testing contracts pallet." + +[dependencies] +frame-system = { workspace = true, default-features = true } +sp-runtime = { workspace = true, default-features = true } +anyhow = { workspace = true } + +[build-dependencies] +parity-wasm = { workspace = true } +tempfile = { workspace = true } +toml = { workspace = true } +twox-hash = { workspace = true, default-features = true } +polkavm-linker = { workspace = true, optional = true } +anyhow = { workspace = true } + +[features] +riscv = ["polkavm-linker"] diff --git a/pallets/contracts/fixtures/build.rs b/pallets/contracts/fixtures/build.rs new file mode 100644 index 00000000..baaeaf03 --- /dev/null +++ b/pallets/contracts/fixtures/build.rs @@ -0,0 +1,355 @@ +// This file is part of Substrate. + +// 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. + +//! Compile contracts to wasm and RISC-V binaries. +use anyhow::{bail, Context, Result}; +use parity_wasm::elements::{deserialize_file, serialize_to_file, Internal}; +use std::{ + env, fs, + hash::Hasher, + path::{Path, PathBuf}, + process::Command, +}; +use twox_hash::XxHash32; + +/// Read the file at `path` and return its hash as a hex string. +fn file_hash(path: &Path) -> String { + let data = fs::read(path).expect("file exists; qed"); + let mut hasher = XxHash32::default(); + hasher.write(&data); + hasher.write(include_bytes!("build.rs")); + let hash = hasher.finish(); + format!("{:x}", hash) +} + +/// A contract entry. +struct Entry { + /// The path to the contract source file. + path: PathBuf, + /// The hash of the contract source file. + hash: String, +} + +impl Entry { + /// Create a new contract entry from the given path. + fn new(path: PathBuf) -> Self { + let hash = file_hash(&path); + Self { path, hash } + } + + /// Return the path to the contract source file. + fn path(&self) -> &str { + self.path.to_str().expect("path is valid unicode; qed") + } + + /// Return the name of the contract. + fn name(&self) -> &str { + self.path + .file_stem() + .expect("file exits; qed") + .to_str() + .expect("name is valid unicode; qed") + } + + /// Return whether the contract has already been compiled. + fn is_cached(&self, out_dir: &Path) -> bool { + out_dir.join(self.name()).join(&self.hash).exists() + } + + /// Update the cache file for the contract. + fn update_cache(&self, out_dir: &Path) -> Result<()> { + let cache_dir = out_dir.join(self.name()); + + // clear the cache dir if it exists + if cache_dir.exists() { + fs::remove_dir_all(&cache_dir)?; + } + + // re-populate the cache dir with the new hash + fs::create_dir_all(&cache_dir)?; + fs::write(out_dir.join(&self.hash), "")?; + Ok(()) + } + + /// Return the name of the output wasm file. + fn out_wasm_filename(&self) -> String { + format!("{}.wasm", self.name()) + } + + /// Return the name of the RISC-V polkavm file. + #[cfg(feature = "riscv")] + fn out_riscv_filename(&self) -> String { + format!("{}.polkavm", self.name()) + } +} + +/// Collect all contract entries from the given source directory. +/// Contracts that have already been compiled are filtered out. +fn collect_entries(contracts_dir: &Path, out_dir: &Path) -> Vec { + fs::read_dir(contracts_dir) + .expect("src dir exists; qed") + .filter_map(|file| { + let path = file.expect("file exists; qed").path(); + if path.extension().map_or(true, |ext| ext != "rs") { + return None + } + + let entry = Entry::new(path); + if entry.is_cached(out_dir) { + None + } else { + Some(entry) + } + }) + .collect::>() +} + +/// Create a `Cargo.toml` to compile the given contract entries. +fn create_cargo_toml<'a>( + fixtures_dir: &Path, + root_cargo_toml: &Path, + entries: impl Iterator, + output_dir: &Path, +) -> Result<()> { + let root_toml: toml::Value = toml::from_str(&fs::read_to_string(root_cargo_toml)?)?; + let mut cargo_toml: toml::Value = toml::from_str(include_str!("./build/Cargo.toml"))?; + let mut set_dep = |name, path| -> Result<()> { + cargo_toml["dependencies"][name]["path"] = toml::Value::String( + fixtures_dir.join(path).canonicalize()?.to_str().unwrap().to_string(), + ); + Ok(()) + }; + set_dep("uapi", "../uapi")?; + set_dep("common", "./contracts/common")?; + cargo_toml["dependencies"]["polkavm-derive"]["version"] = + root_toml["workspace"]["dependencies"]["polkavm-derive"].clone(); + + cargo_toml["bin"] = toml::Value::Array( + entries + .map(|entry| { + let name = entry.name(); + let path = entry.path(); + toml::Value::Table(toml::toml! { + name = name + path = path + }) + }) + .collect::>(), + ); + + let cargo_toml = toml::to_string_pretty(&cargo_toml)?; + fs::write(output_dir.join("Cargo.toml"), cargo_toml).map_err(Into::into) +} + +/// Invoke `cargo fmt` to check that fixtures files are formatted. +fn invoke_cargo_fmt<'a>( + config_path: &Path, + files: impl Iterator, + contract_dir: &Path, +) -> Result<()> { + // If rustfmt is not installed, skip the check. + if !Command::new("rustup") + .args(["nightly-2024-04-10", "run", "rustfmt", "--version"]) + .output() + .map_or(false, |o| o.status.success()) + { + return Ok(()) + } + + let fmt_res = Command::new("rustup") + .args(["nightly-2024-04-10", "run", "rustfmt", "--check", "--config-path"]) + .arg(config_path) + .args(files) + .output() + .expect("failed to execute process"); + + if fmt_res.status.success() { + return Ok(()) + } + + let stdout = String::from_utf8_lossy(&fmt_res.stdout); + let stderr = String::from_utf8_lossy(&fmt_res.stderr); + eprintln!("{}\n{}", stdout, stderr); + eprintln!( + "Fixtures files are not formatted.\n + Please run `rustup nightly-2024-04-10 run rustfmt --config-path {} {}/*.rs`", + config_path.display(), + contract_dir.display() + ); + + anyhow::bail!("Fixtures files are not formatted") +} + +/// Build contracts for wasm. +fn invoke_wasm_build(current_dir: &Path) -> Result<()> { + let encoded_rustflags = [ + "-Clink-arg=-zstack-size=65536", + "-Clink-arg=--import-memory", + "-Clinker-plugin-lto", + "-Ctarget-cpu=mvp", + "-Dwarnings", + ] + .join("\x1f"); + + let build_res = Command::new(env::var("CARGO")?) + .current_dir(current_dir) + .env("CARGO_TARGET_DIR", current_dir.join("target").display().to_string()) + .env("CARGO_ENCODED_RUSTFLAGS", encoded_rustflags) + .args(["build", "--release", "--target=wasm32-unknown-unknown"]) + .output() + .expect("failed to execute process"); + + if build_res.status.success() { + return Ok(()) + } + + let stderr = String::from_utf8_lossy(&build_res.stderr); + eprintln!("{}", stderr); + bail!("Failed to build wasm contracts"); +} + +/// Post-process the compiled wasm contracts. +fn post_process_wasm(input_path: &Path, output_path: &Path) -> Result<()> { + let mut module = + deserialize_file(input_path).with_context(|| format!("Failed to read {:?}", input_path))?; + if let Some(section) = module.export_section_mut() { + section.entries_mut().retain(|entry| { + matches!(entry.internal(), Internal::Function(_)) && + (entry.field() == "call" || entry.field() == "deploy") + }); + } + + serialize_to_file(output_path, module).map_err(Into::into) +} + +/// Build contracts for RISC-V. +#[cfg(feature = "riscv")] +fn invoke_riscv_build(current_dir: &Path) -> Result<()> { + let encoded_rustflags = [ + "-Crelocation-model=pie", + "-Clink-arg=--emit-relocs", + "-Clink-arg=--export-dynamic-symbol=__polkavm_symbol_export_hack__*", + ] + .join("\x1f"); + + let build_res = Command::new(env::var("CARGO")?) + .current_dir(current_dir) + .env_clear() + .env("PATH", env::var("PATH").unwrap_or_default()) + .env("CARGO_ENCODED_RUSTFLAGS", encoded_rustflags) + .env("RUSTUP_TOOLCHAIN", "rve-nightly") + .env("RUSTUP_HOME", env::var("RUSTUP_HOME").unwrap_or_default()) + .args(["build", "--release", "--target=riscv32ema-unknown-none-elf"]) + .output() + .expect("failed to execute process"); + + if build_res.status.success() { + return Ok(()) + } + + let stderr = String::from_utf8_lossy(&build_res.stderr); + + if stderr.contains("'rve-nightly' is not installed") { + eprintln!("RISC-V toolchain is not installed.\nDownload and install toolchain from https://github.com/paritytech/rustc-rv32e-toolchain."); + eprintln!("{}", stderr); + } else { + eprintln!("{}", stderr); + } + + bail!("Failed to build contracts"); +} +/// Post-process the compiled wasm contracts. +#[cfg(feature = "riscv")] +fn post_process_riscv(input_path: &Path, output_path: &Path) -> Result<()> { + let mut config = polkavm_linker::Config::default(); + config.set_strip(true); + let orig = fs::read(input_path).with_context(|| format!("Failed to read {:?}", input_path))?; + let linked = polkavm_linker::program_from_elf(config, orig.as_ref()) + .map_err(|err| anyhow::format_err!("Failed to link polkavm program: {}", err))?; + fs::write(output_path, linked.as_bytes()).map_err(Into::into) +} + +/// Write the compiled contracts to the given output directory. +fn write_output(build_dir: &Path, out_dir: &Path, entries: Vec) -> Result<()> { + for entry in entries { + let wasm_output = entry.out_wasm_filename(); + post_process_wasm( + &build_dir.join("target/wasm32-unknown-unknown/release").join(&wasm_output), + &out_dir.join(&wasm_output), + )?; + + #[cfg(feature = "riscv")] + post_process_riscv( + &build_dir.join("target/riscv32ema-unknown-none-elf/release").join(entry.name()), + &out_dir.join(entry.out_riscv_filename()), + )?; + + entry.update_cache(out_dir)?; + } + + Ok(()) +} + +/// Returns the root path of the wasm workspace. +fn find_workspace_root(current_dir: &Path) -> Option { + let mut current_dir = current_dir.to_path_buf(); + + while current_dir.parent().is_some() { + if current_dir.join("Cargo.toml").exists() { + let cargo_toml_contents = + std::fs::read_to_string(current_dir.join("Cargo.toml")).ok()?; + if cargo_toml_contents.contains("[workspace]") { + return Some(current_dir); + } + } + + current_dir.pop(); + } + + None +} + +fn main() -> Result<()> { + let fixtures_dir: PathBuf = env::var("CARGO_MANIFEST_DIR")?.into(); + let contracts_dir = fixtures_dir.join("contracts"); + let out_dir: PathBuf = env::var("OUT_DIR")?.into(); + let workspace_root = find_workspace_root(&fixtures_dir).expect("workspace root exists; qed"); + let root_cargo_toml = workspace_root.join("Cargo.toml"); + + let entries = collect_entries(&contracts_dir, &out_dir); + if entries.is_empty() { + return Ok(()) + } + + let tmp_dir = tempfile::tempdir()?; + let tmp_dir_path = tmp_dir.path(); + + create_cargo_toml(&fixtures_dir, &root_cargo_toml, entries.iter(), tmp_dir.path())?; + invoke_cargo_fmt( + &workspace_root.join(".rustfmt.toml"), + entries.iter().map(|entry| &entry.path as _), + &contracts_dir, + )?; + + invoke_wasm_build(tmp_dir_path)?; + + #[cfg(feature = "riscv")] + invoke_riscv_build(tmp_dir_path)?; + + write_output(tmp_dir_path, &out_dir, entries)?; + Ok(()) +} diff --git a/pallets/contracts/fixtures/build/Cargo.toml b/pallets/contracts/fixtures/build/Cargo.toml new file mode 100644 index 00000000..ba487a2b --- /dev/null +++ b/pallets/contracts/fixtures/build/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "contracts" +version = "0.6.3" +edition = "2021" + +# Binary targets are injected dynamically by the build script. +[[bin]] + +# All paths or versions are injected dynamically by the build script. +[dependencies] +uapi = { package = 'pallet-contracts-uapi', path = "", default-features = false } +common = { package = 'pallet-contracts-fixtures-common', path = "" } +polkavm-derive = { version = "" } + +[profile.release] +opt-level = 3 +lto = true +codegen-units = 1 diff --git a/pallets/contracts/fixtures/contracts/account_reentrance_count_call.rs b/pallets/contracts/fixtures/contracts/account_reentrance_count_call.rs new file mode 100644 index 00000000..9da4eb6d --- /dev/null +++ b/pallets/contracts/fixtures/contracts/account_reentrance_count_call.rs @@ -0,0 +1,39 @@ +// This file is part of Substrate. + +// 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. + +//! This fixture tests if account_reentrance_count works as expected. +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(callee: [u8; 32],); + + #[allow(deprecated)] + let reentrance_count = api::account_reentrance_count(callee); + + // Return the reentrance count. + api::return_value(uapi::ReturnFlags::empty(), &reentrance_count.to_le_bytes()); +} diff --git a/pallets/contracts/fixtures/contracts/balance.rs b/pallets/contracts/fixtures/contracts/balance.rs new file mode 100644 index 00000000..4011b837 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/balance.rs @@ -0,0 +1,36 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::output; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + // Initialize buffer with 1s so that we can check that it is overwritten. + output!(balance, [1u8; 8], api::balance,); + + // Assert that the balance is 0. + assert_eq!(&[0u8; 8], balance); +} diff --git a/pallets/contracts/fixtures/contracts/call.rs b/pallets/contracts/fixtures/contracts/call.rs new file mode 100644 index 00000000..535745ff --- /dev/null +++ b/pallets/contracts/fixtures/contracts/call.rs @@ -0,0 +1,49 @@ +// This file is part of Substrate. + +// 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. + +//! This calls another contract as passed as its account id. +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + callee_input: [u8; 4], + callee_addr: [u8; 32], + ); + + // Call the callee + api::call_v2( + uapi::CallFlags::empty(), + callee_addr, + 0u64, // How much ref_time to devote for the execution. 0 = all. + 0u64, // How much proof_size to devote for the execution. 0 = all. + None, // No deposit limit. + &0u64.to_le_bytes(), // Value transferred to the contract. + callee_input, + None, + ) + .unwrap(); +} diff --git a/pallets/contracts/fixtures/contracts/call_return_code.rs b/pallets/contracts/fixtures/contracts/call_return_code.rs new file mode 100644 index 00000000..3d5a1073 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/call_return_code.rs @@ -0,0 +1,56 @@ +// This file is part of Substrate. + +// 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. + +//! This calls the supplied dest and transfers 100 balance during this call and copies +//! the return code of this call to the output buffer. +//! It also forwards its input to the callee. +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + 100, + callee_addr: [u8; 32], + input: [u8], + ); + + // Call the callee + let err_code = match api::call_v2( + uapi::CallFlags::empty(), + callee_addr, + 0u64, // How much ref_time to devote for the execution. 0 = all. + 0u64, // How much proof_size to devote for the execution. 0 = all. + None, // No deposit limit. + &100u64.to_le_bytes(), // Value transferred to the contract. + input, + None, + ) { + Ok(_) => 0u32, + Err(code) => code as u32, + }; + + api::return_value(uapi::ReturnFlags::empty(), &err_code.to_le_bytes()); +} diff --git a/pallets/contracts/fixtures/contracts/call_runtime.rs b/pallets/contracts/fixtures/contracts/call_runtime.rs new file mode 100644 index 00000000..2b132398 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/call_runtime.rs @@ -0,0 +1,42 @@ +// This file is part of Substrate. + +// 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. + +//! This passes its input to `call_runtime` and returns the return value to its caller. +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + // Fixture calls should fit into 100 bytes. + input!(100, call: [u8], ); + + // Use the call passed as input to call the runtime. + let err_code = match api::call_runtime(call) { + Ok(_) => 0u32, + Err(code) => code as u32, + }; + + api::return_value(uapi::ReturnFlags::empty(), &err_code.to_le_bytes()); +} diff --git a/pallets/contracts/fixtures/contracts/call_runtime_and_call.rs b/pallets/contracts/fixtures/contracts/call_runtime_and_call.rs new file mode 100644 index 00000000..1321d36d --- /dev/null +++ b/pallets/contracts/fixtures/contracts/call_runtime_and_call.rs @@ -0,0 +1,53 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + 512, + callee_input: [u8; 4], + callee_addr: [u8; 32], + call: [u8], + ); + + // Use the call passed as input to call the runtime. + api::call_runtime(call).unwrap(); + + // Call the callee + api::call_v2( + uapi::CallFlags::empty(), + callee_addr, + 0u64, // How much ref_time to devote for the execution. 0 = all. + 0u64, // How much proof_size to devote for the execution. 0 = all. + None, // No deposit limit. + &0u64.to_le_bytes(), // Value transferred to the contract. + callee_input, + None, + ) + .unwrap(); +} diff --git a/pallets/contracts/fixtures/contracts/call_with_flags_and_value.rs b/pallets/contracts/fixtures/contracts/call_with_flags_and_value.rs new file mode 100644 index 00000000..16a85eff --- /dev/null +++ b/pallets/contracts/fixtures/contracts/call_with_flags_and_value.rs @@ -0,0 +1,51 @@ +// This file is part of Substrate. + +// 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. + +//! This fixture calls the account_id with the flags and value. +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + 256, + callee_addr: [u8; 32], + flags: u32, + value: u64, + forwarded_input: [u8], + ); + + api::call_v2( + uapi::CallFlags::from_bits(flags).unwrap(), + callee_addr, + 0u64, // How much ref_time to devote for the execution. 0 = all. + 0u64, // How much proof_size to devote for the execution. 0 = all. + None, // No deposit limit. + &value.to_le_bytes(), // Value transferred to the contract. + forwarded_input, + None, + ) + .unwrap(); +} diff --git a/pallets/contracts/fixtures/contracts/call_with_limit.rs b/pallets/contracts/fixtures/contracts/call_with_limit.rs new file mode 100644 index 00000000..f0851f32 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/call_with_limit.rs @@ -0,0 +1,52 @@ +// This file is part of Substrate. + +// 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. + +//! This fixture calls the account_id with the 2D Weight limit. +//! It returns the result of the call as output data. +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + 256, + callee_addr: [u8; 32], + ref_time: u64, + proof_size: u64, + forwarded_input: [u8], + ); + + api::call_v2( + uapi::CallFlags::empty(), + callee_addr, + ref_time, + proof_size, + None, // No deposit limit. + &0u64.to_le_bytes(), // value transferred to the contract. + forwarded_input, + None, + ) + .unwrap(); +} diff --git a/pallets/contracts/fixtures/contracts/caller_contract.rs b/pallets/contracts/fixtures/contracts/caller_contract.rs new file mode 100644 index 00000000..fffdb66a --- /dev/null +++ b/pallets/contracts/fixtures/contracts/caller_contract.rs @@ -0,0 +1,145 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api, ReturnErrorCode}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(code_hash: [u8; 32],); + + // The value to transfer on instantiation and calls. Chosen to be greater than existential + // deposit. + let value = 32768u64.to_le_bytes(); + let salt = [0u8; 0]; + + // Callee will use the first 4 bytes of the input to return an exit status. + let input = [0u8, 1, 34, 51, 68, 85, 102, 119]; + let reverted_input = [1u8, 34, 51, 68, 85, 102, 119]; + + // Fail to deploy the contract since it returns a non-zero exit status. + let res = api::instantiate_v2( + code_hash, + 0u64, // How much ref_time weight to devote for the execution. 0 = all. + 0u64, // How much proof_size weight to devote for the execution. 0 = all. + None, // No deposit limit. + &value, + &reverted_input, + None, + None, + &salt, + ); + assert!(matches!(res, Err(ReturnErrorCode::CalleeReverted))); + + // Fail to deploy the contract due to insufficient ref_time weight. + let res = api::instantiate_v2( + code_hash, 1u64, // too little ref_time weight + 0u64, // How much proof_size weight to devote for the execution. 0 = all. + None, // No deposit limit. + &value, &input, None, None, &salt, + ); + assert!(matches!(res, Err(ReturnErrorCode::CalleeTrapped))); + + // Fail to deploy the contract due to insufficient proof_size weight. + let res = api::instantiate_v2( + code_hash, 0u64, // How much ref_time weight to devote for the execution. 0 = all. + 1u64, // Too little proof_size weight + None, // No deposit limit. + &value, &input, None, None, &salt, + ); + assert!(matches!(res, Err(ReturnErrorCode::CalleeTrapped))); + + // Deploy the contract successfully. + let mut callee = [0u8; 32]; + let callee = &mut &mut callee[..]; + + api::instantiate_v2( + code_hash, + 0u64, // How much ref_time weight to devote for the execution. 0 = all. + 0u64, // How much proof_size weight to devote for the execution. 0 = all. + None, // No deposit limit. + &value, + &input, + Some(callee), + None, + &salt, + ) + .unwrap(); + assert_eq!(callee.len(), 32); + + // Call the new contract and expect it to return failing exit code. + let res = api::call_v2( + uapi::CallFlags::empty(), + callee, + 0u64, // How much ref_time weight to devote for the execution. 0 = all. + 0u64, // How much proof_size weight to devote for the execution. 0 = all. + None, // No deposit limit. + &value, + &reverted_input, + None, + ); + assert!(matches!(res, Err(ReturnErrorCode::CalleeReverted))); + + // Fail to call the contract due to insufficient ref_time weight. + let res = api::call_v2( + uapi::CallFlags::empty(), + callee, + 1u64, // Too little ref_time weight. + 0u64, // How much proof_size weight to devote for the execution. 0 = all. + None, // No deposit limit. + &value, + &input, + None, + ); + assert!(matches!(res, Err(ReturnErrorCode::CalleeTrapped))); + + // Fail to call the contract due to insufficient proof_size weight. + let res = api::call_v2( + uapi::CallFlags::empty(), + callee, + 0u64, // How much ref_time weight to devote for the execution. 0 = all. + 1u64, // too little proof_size weight + None, // No deposit limit. + &value, + &input, + None, + ); + assert!(matches!(res, Err(ReturnErrorCode::CalleeTrapped))); + + // Call the contract successfully. + let mut output = [0u8; 4]; + api::call_v2( + uapi::CallFlags::empty(), + callee, + 0u64, // How much ref_time weight to devote for the execution. 0 = all. + 0u64, // How much proof_size weight to devote for the execution. 0 = all. + None, // No deposit limit. + &value, + &input, + Some(&mut &mut output[..]), + ) + .unwrap(); + assert_eq!(&output, &input[4..]) +} diff --git a/pallets/contracts/fixtures/contracts/caller_is_origin_n.rs b/pallets/contracts/fixtures/contracts/caller_is_origin_n.rs new file mode 100644 index 00000000..fd6f5980 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/caller_is_origin_n.rs @@ -0,0 +1,38 @@ +// This file is part of Substrate. + +// 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. + +//! This fixture calls caller_is_origin `n` times. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(n: u32, ); + + for _ in 0..n { + let _ = api::caller_is_origin(); + } +} diff --git a/pallets/contracts/fixtures/contracts/chain_extension.rs b/pallets/contracts/fixtures/contracts/chain_extension.rs new file mode 100644 index 00000000..474df00d --- /dev/null +++ b/pallets/contracts/fixtures/contracts/chain_extension.rs @@ -0,0 +1,42 @@ +// This file is part of Substrate. + +// 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. + +//! Call chain extension by passing through input and output of this contract. +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(input, 8, func_id: u32,); + + // the chain extension passes through the input and returns it as output + let mut output_buffer = [0u8; 32]; + let output = &mut &mut output_buffer[0..input.len()]; + + let ret_id = api::call_chain_extension(func_id, input, Some(output)); + assert_eq!(ret_id, func_id); + + api::return_value(uapi::ReturnFlags::empty(), output); +} diff --git a/pallets/contracts/fixtures/contracts/chain_extension_temp_storage.rs b/pallets/contracts/fixtures/contracts/chain_extension_temp_storage.rs new file mode 100644 index 00000000..2e15fb02 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/chain_extension_temp_storage.rs @@ -0,0 +1,65 @@ +// This file is part of Substrate. + +// 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. + +//! Call chain extension two times with the specified func_ids +//! It then calls itself once +#![no_std] +#![no_main] + +use common::{input, output}; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + input, + func_id1: u32, + func_id2: u32, + stop_recurse: u8, + ); + + api::call_chain_extension(func_id1, input, None); + api::call_chain_extension(func_id2, input, None); + + if stop_recurse == 0 { + // Setup next call + input[0..4].copy_from_slice(&((3 << 16) | 2u32).to_le_bytes()); + input[4..8].copy_from_slice(&((3 << 16) | 3u32).to_le_bytes()); + input[8] = 1u8; + + // Read the contract address. + output!(addr, [0u8; 32], api::address,); + + // call self + api::call_v2( + uapi::CallFlags::ALLOW_REENTRY, + addr, + 0u64, // How much ref_time to devote for the execution. 0 = all. + 0u64, // How much proof_size to devote for the execution. 0 = all. + None, // No deposit limit. + &0u64.to_le_bytes(), // Value transferred to the contract. + input, + None, + ) + .unwrap(); + } +} diff --git a/pallets/contracts/fixtures/contracts/common/Cargo.toml b/pallets/contracts/fixtures/contracts/common/Cargo.toml new file mode 100644 index 00000000..296f408d --- /dev/null +++ b/pallets/contracts/fixtures/contracts/common/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "pallet-contracts-fixtures-common" +publish = false +version = "1.0.0" +authors.workspace = true +edition.workspace = true +license.workspace = true +description = "Common utilities for pallet-contracts-fixtures." + +[dependencies] +uapi = { package = 'pallet-contracts-uapi', path = "../../../uapi", default-features = false } diff --git a/pallets/contracts/fixtures/contracts/common/src/lib.rs b/pallets/contracts/fixtures/contracts/common/src/lib.rs new file mode 100644 index 00000000..80e1f543 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/common/src/lib.rs @@ -0,0 +1,161 @@ +// This file is part of Substrate. + +// 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. +#![no_std] + +pub use uapi::{HostFn, HostFnImpl as api}; + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + #[cfg(target_arch = "wasm32")] + core::arch::wasm32::unreachable(); + + #[cfg(target_arch = "riscv32")] + // Safety: The unimp instruction is guaranteed to trap + unsafe { + core::arch::asm!("unimp"); + core::hint::unreachable_unchecked(); + } +} + +/// Utility macro to read input passed to a contract. +/// +/// Example: +/// +/// ``` +/// input$!( +/// var1: u32, // [0, 4) var1 decoded as u32 +/// var2: [u8; 32], // [4, 36) var2 decoded as a [u8] slice +/// var3: u8, // [36, 37) var3 decoded as a u8 +/// ); +/// +/// // Input and size can be specified as well: +/// input$!( +/// input, // input buffer (optional) +/// 512, // input size (optional) +/// var4: u32, // [0, 4) var4 decoded as u32 +/// var5: [u8], // [4, ..) var5 decoded as a [u8] slice +/// ); +/// ``` +#[macro_export] +macro_rules! input { + (@inner $input:expr, $cursor:expr,) => {}; + (@size $size:expr, ) => { $size }; + + // Match a u8 variable. + // e.g input!(var1: u8, ); + (@inner $input:expr, $cursor:expr, $var:ident: u8, $($rest:tt)*) => { + let $var = $input[$cursor]; + input!(@inner $input, $cursor + 1, $($rest)*); + }; + + // Size of u8 variable. + (@size $size:expr, $var:ident: u8, $($rest:tt)*) => { + input!(@size $size + 1, $($rest)*) + }; + + // Match a u64 variable. + // e.g input!(var1: u64, ); + (@inner $input:expr, $cursor:expr, $var:ident: u64, $($rest:tt)*) => { + let $var = u64::from_le_bytes($input[$cursor..$cursor + 8].try_into().unwrap()); + input!(@inner $input, $cursor + 8, $($rest)*); + }; + + // Size of u64 variable. + (@size $size:expr, $var:ident: u64, $($rest:tt)*) => { + input!(@size $size + 8, $($rest)*) + }; + + // Match a u32 variable. + // e.g input!(var1: u32, ); + (@inner $input:expr, $cursor:expr, $var:ident: u32, $($rest:tt)*) => { + let $var = u32::from_le_bytes($input[$cursor..$cursor + 4].try_into().unwrap()); + input!(@inner $input, $cursor + 4, $($rest)*); + }; + + // Size of u32 variable. + (@size $size:expr, $var:ident: u32, $($rest:tt)*) => { + input!(@size $size + 4, $($rest)*) + }; + + // Match a u8 slice with the remaining bytes. + // e.g input!(512, var1: [u8; 32], var2: [u8], ); + (@inner $input:expr, $cursor:expr, $var:ident: [u8],) => { + let $var = &$input[$cursor..]; + }; + + // Match a u8 slice of the given size. + // e.g input!(var1: [u8; 32], ); + (@inner $input:expr, $cursor:expr, $var:ident: [u8; $n:expr], $($rest:tt)*) => { + let $var = &$input[$cursor..$cursor+$n]; + input!(@inner $input, $cursor + $n, $($rest)*); + }; + + // Size of a u8 slice. + (@size $size:expr, $var:ident: [u8; $n:expr], $($rest:tt)*) => { + input!(@size $size + $n, $($rest)*) + }; + + // Entry point, with the buffer and it's size specified first. + // e.g input!(buffer, 512, var1: u32, var2: [u8], ); + ($buffer:ident, $size:expr, $($rest:tt)*) => { + let mut $buffer = [0u8; $size]; + let $buffer = &mut &mut $buffer[..]; + $crate::api::input($buffer); + input!(@inner $buffer, 0, $($rest)*); + }; + + // Entry point, with the name of the buffer specified and size of the input buffer computed. + // e.g input!(buffer, var1: u32, var2: u64, ); + ($buffer: ident, $($rest:tt)*) => { + input!($buffer, input!(@size 0, $($rest)*), $($rest)*); + }; + + // Entry point, with the size of the input buffer computed. + // e.g input!(var1: u32, var2: u64, ); + ($($rest:tt)*) => { + input!(buffer, $($rest)*); + }; +} + +/// Utility macro to invoke a host function that expect a `output: &mut &mut [u8]` as last argument. +/// +/// Example: +/// ``` +/// // call `api::caller` and store the output in `caller` +/// output!(caller, [0u8; 32], api::caller,); +/// +/// // call `api::get_storage` and store the output in `address` +/// output!(address, [0u8; 32], api::get_storage, &[1u8; 32]); +/// ``` +#[macro_export] +macro_rules! output { + ($output: ident, $buffer: expr, $host_fn:path, $($arg:expr),*) => { + let mut $output = $buffer; + let $output = &mut &mut $output[..]; + $host_fn($($arg,)* $output); + }; +} + +/// Similar to `output!` but unwraps the result. +#[macro_export] +macro_rules! unwrap_output { + ($output: ident, $buffer: expr, $host_fn:path, $($arg:expr),*) => { + let mut $output = $buffer; + let $output = &mut &mut $output[..]; + $host_fn($($arg,)* $output).unwrap(); + }; +} diff --git a/pallets/contracts/fixtures/contracts/create_storage_and_call.rs b/pallets/contracts/fixtures/contracts/create_storage_and_call.rs new file mode 100644 index 00000000..f8ce0ff4 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/create_storage_and_call.rs @@ -0,0 +1,58 @@ +// This file is part of Substrate. + +// 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. + +//! This calls another contract as passed as its account id. It also creates some storage. +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + buffer, + input: [u8; 4], + callee: [u8; 32], + deposit_limit: [u8; 8], + ); + + // create 4 byte of storage before calling + api::set_storage(buffer, &[1u8; 4]); + + // Call the callee + api::call_v2( + uapi::CallFlags::empty(), + callee, + 0u64, // How much ref_time weight to devote for the execution. 0 = all. + 0u64, // How much proof_size weight to devote for the execution. 0 = all. + Some(deposit_limit), + &0u64.to_le_bytes(), // Value transferred to the contract. + input, + None, + ) + .unwrap(); + + // create 8 byte of storage after calling + // item of 12 bytes because we override 4 bytes + api::set_storage(buffer, &[1u8; 12]); +} diff --git a/pallets/contracts/fixtures/contracts/create_storage_and_instantiate.rs b/pallets/contracts/fixtures/contracts/create_storage_and_instantiate.rs new file mode 100644 index 00000000..fa3b9000 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/create_storage_and_instantiate.rs @@ -0,0 +1,58 @@ +// This file is part of Substrate. + +// 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. + +//! This instantiates another contract and passes some input to its constructor. +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + input: [u8; 4], + code_hash: [u8; 32], + deposit_limit: [u8; 8], + ); + + let value = 10_000u64.to_le_bytes(); + let salt = [0u8; 0]; + let mut address = [0u8; 32]; + let address = &mut &mut address[..]; + + api::instantiate_v2( + code_hash, + 0u64, // How much ref_time weight to devote for the execution. 0 = all. + 0u64, // How much proof_size weight to devote for the execution. 0 = all. + Some(deposit_limit), + &value, + input, + Some(address), + None, + &salt, + ) + .unwrap(); + + // Return the deployed contract address. + api::return_value(uapi::ReturnFlags::empty(), address); +} diff --git a/pallets/contracts/fixtures/contracts/create_transient_storage_and_call.rs b/pallets/contracts/fixtures/contracts/create_transient_storage_and_call.rs new file mode 100644 index 00000000..6bafee55 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/create_transient_storage_and_call.rs @@ -0,0 +1,56 @@ +// This file is part of Substrate. + +// 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. + +//! This calls another contract as passed as its account id. It also creates some transient storage. +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + buffer, + len: u32, + input: [u8; 4], + callee: [u8; 32], + ); + + let data = [0u8; 16 * 1024]; + let value = &data[..len as usize]; + #[allow(deprecated)] + api::set_transient_storage(buffer, value); + + // Call the callee + api::call_v2( + uapi::CallFlags::empty(), + callee, + 0u64, // How much ref_time weight to devote for the execution. 0 = all. + 0u64, // How much proof_size weight to devote for the execution. 0 = all. + None, + &0u64.to_le_bytes(), // Value transferred to the contract. + input, + None, + ) + .unwrap(); +} diff --git a/pallets/contracts/fixtures/contracts/crypto_hashes.rs b/pallets/contracts/fixtures/contracts/crypto_hashes.rs new file mode 100644 index 00000000..35cc03f1 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/crypto_hashes.rs @@ -0,0 +1,84 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +/// Called by the tests. +/// +/// The `call` function expects data in a certain format in the input buffer. +/// +/// 1. The first byte encodes an identifier for the crypto hash function under test. (*) +/// 2. The rest encodes the input data that is directly fed into the crypto hash function chosen in +/// 1. +/// +/// The `deploy` function then computes the chosen crypto hash function +/// given the input and puts the result into the output buffer. +/// After contract execution the test driver then asserts that the returned +/// values are equal to the expected bytes for the input and chosen hash +/// function. +/// +/// (*) The possible value for the crypto hash identifiers can be found below: +/// +/// | value | Algorithm | Bit Width | +/// |-------|-----------|-----------| +/// | 0 | SHA2 | 256 | +/// | 1 | KECCAK | 256 | +/// | 2 | BLAKE2 | 256 | +/// | 3 | BLAKE2 | 128 | +/// --------------------------------- + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + 256, + chosen_hash_fn: u8, + input: [u8], + ); + + match chosen_hash_fn { + 1 => { + let mut output = [0u8; 32]; + api::hash_sha2_256(input, &mut output); + api::return_value(uapi::ReturnFlags::empty(), &output); + }, + 2 => { + let mut output = [0u8; 32]; + api::hash_keccak_256(input, &mut output); + api::return_value(uapi::ReturnFlags::empty(), &output); + }, + 3 => { + let mut output = [0u8; 32]; + api::hash_blake2_256(input, &mut output); + api::return_value(uapi::ReturnFlags::empty(), &output); + }, + 4 => { + let mut output = [0u8; 16]; + api::hash_blake2_128(input, &mut output); + api::return_value(uapi::ReturnFlags::empty(), &output); + }, + _ => panic!("unknown crypto hash function identifier"), + } +} diff --git a/pallets/contracts/fixtures/contracts/debug_message_invalid_utf8.rs b/pallets/contracts/fixtures/contracts/debug_message_invalid_utf8.rs new file mode 100644 index 00000000..6c850a9e --- /dev/null +++ b/pallets/contracts/fixtures/contracts/debug_message_invalid_utf8.rs @@ -0,0 +1,33 @@ +// This file is part of Substrate. + +// 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. + +//! Emit a debug message with an invalid utf-8 code. +#![no_std] +#![no_main] + +extern crate common; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + api::debug_message(b"\xFC").unwrap(); +} diff --git a/pallets/contracts/fixtures/contracts/debug_message_logging_disabled.rs b/pallets/contracts/fixtures/contracts/debug_message_logging_disabled.rs new file mode 100644 index 00000000..b9f62adb --- /dev/null +++ b/pallets/contracts/fixtures/contracts/debug_message_logging_disabled.rs @@ -0,0 +1,33 @@ +// This file is part of Substrate. + +// 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. + +//! Emit a "Hello World!" debug message but assume that logging is disabled. +#![no_std] +#![no_main] + +extern crate common; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + api::debug_message(b"Hello World!").unwrap(); +} diff --git a/pallets/contracts/fixtures/contracts/debug_message_works.rs b/pallets/contracts/fixtures/contracts/debug_message_works.rs new file mode 100644 index 00000000..3a250950 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/debug_message_works.rs @@ -0,0 +1,33 @@ +// This file is part of Substrate. + +// 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. + +//! Emit a "Hello World!" debug message. +#![no_std] +#![no_main] + +extern crate common; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + api::debug_message(b"Hello World!").unwrap(); +} diff --git a/pallets/contracts/fixtures/contracts/delegate_call.rs b/pallets/contracts/fixtures/contracts/delegate_call.rs new file mode 100644 index 00000000..f109e8a6 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/delegate_call.rs @@ -0,0 +1,49 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(code_hash: [u8; 32],); + + let mut key = [0u8; 32]; + key[0] = 1u8; + + let mut value = [0u8; 32]; + let value = &mut &mut value[..]; + value[0] = 2u8; + + api::set_storage(&key, value); + api::get_storage(&key, value).unwrap(); + assert!(value[0] == 2u8); + + let input = [0u8; 0]; + api::delegate_call(uapi::CallFlags::empty(), code_hash, &input, None).unwrap(); + + api::get_storage(&[1u8], value).unwrap(); + assert!(value[0] == 1u8); +} diff --git a/pallets/contracts/fixtures/contracts/delegate_call_lib.rs b/pallets/contracts/fixtures/contracts/delegate_call_lib.rs new file mode 100644 index 00000000..197b396c --- /dev/null +++ b/pallets/contracts/fixtures/contracts/delegate_call_lib.rs @@ -0,0 +1,49 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::output; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + let mut key = [0u8; 32]; + key[0] = 1u8; + + // Place a value in storage. + let mut value = [0u8; 32]; + let value = &mut &mut value[..]; + value[0] = 1u8; + api::set_storage(&key, value); + + // Assert that `value_transferred` is equal to the value + // passed to the `caller` contract: 1337. + output!(value_transferred, [0u8; 8], api::value_transferred,); + let value_transferred = u64::from_le_bytes(value_transferred[..].try_into().unwrap()); + assert_eq!(value_transferred, 1337); + + // Assert that ALICE is the caller of the contract. + output!(caller, [0u8; 32], api::caller,); + assert_eq!(&caller[..], &[1u8; 32]); +} diff --git a/pallets/contracts/fixtures/contracts/delegate_call_simple.rs b/pallets/contracts/fixtures/contracts/delegate_call_simple.rs new file mode 100644 index 00000000..cf3351c5 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/delegate_call_simple.rs @@ -0,0 +1,36 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(code_hash: [u8; 32],); + + // Delegate call into passed code hash. + let input = [0u8; 0]; + api::delegate_call(uapi::CallFlags::empty(), code_hash, &input, None).unwrap(); +} diff --git a/pallets/contracts/fixtures/contracts/destroy_and_transfer.rs b/pallets/contracts/fixtures/contracts/destroy_and_transfer.rs new file mode 100644 index 00000000..62fb63b5 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/destroy_and_transfer.rs @@ -0,0 +1,86 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +const ADDRESS_KEY: [u8; 32] = [0u8; 32]; +const VALUE: [u8; 8] = [0, 0, 1u8, 0, 0, 0, 0, 0]; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() { + input!(code_hash: [u8; 32],); + + let input = [0u8; 0]; + let mut address = [0u8; 32]; + let address = &mut &mut address[..]; + let salt = [71u8, 17u8]; + + api::instantiate_v2( + code_hash, + 0u64, // How much ref_time weight to devote for the execution. 0 = all. + 0u64, // How much proof_size weight to devote for the execution. 0 = all. + None, // No deposit limit. + &VALUE, + &input, + Some(address), + None, + &salt, + ) + .unwrap(); + + // Return the deployed contract address. + api::set_storage(&ADDRESS_KEY, address); +} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + let mut callee_addr = [0u8; 32]; + let callee_addr = &mut &mut callee_addr[..]; + api::get_storage(&ADDRESS_KEY, callee_addr).unwrap(); + + // Calling the destination contract with non-empty input data should fail. + let res = api::call_v2( + uapi::CallFlags::empty(), + callee_addr, + 0u64, // How much ref_time weight to devote for the execution. 0 = all. + 0u64, // How much proof_size weight to devote for the execution. 0 = all. + None, // No deposit limit. + &VALUE, + &[0u8; 1], + None, + ); + assert!(matches!(res, Err(uapi::ReturnErrorCode::CalleeTrapped))); + + // Call the destination contract regularly, forcing it to self-destruct. + api::call_v2( + uapi::CallFlags::empty(), + callee_addr, + 0u64, // How much ref_time weight to devote for the execution. 0 = all. + 0u64, // How much proof_size weight to devote for the execution. 0 = all. + None, // No deposit limit. + &VALUE, + &[0u8; 0], + None, + ) + .unwrap(); +} diff --git a/pallets/contracts/fixtures/contracts/drain.rs b/pallets/contracts/fixtures/contracts/drain.rs new file mode 100644 index 00000000..f5c8681c --- /dev/null +++ b/pallets/contracts/fixtures/contracts/drain.rs @@ -0,0 +1,44 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::output; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + output!(balance, [0u8; 8], api::balance,); + let balance = u64::from_le_bytes(balance[..].try_into().unwrap()); + + output!(minimum_balance, [0u8; 8], api::minimum_balance,); + let minimum_balance = u64::from_le_bytes(minimum_balance[..].try_into().unwrap()); + + // Make the transferred value exceed the balance by adding the minimum balance. + let balance = balance + minimum_balance; + + // Try to self-destruct by sending more balance to the 0 address. + // The call will fail because a contract transfer has a keep alive requirement. + let res = api::transfer(&[0u8; 32], &balance.to_le_bytes()); + assert!(matches!(res, Err(uapi::ReturnErrorCode::TransferFailed))); +} diff --git a/pallets/contracts/fixtures/contracts/dummy.rs b/pallets/contracts/fixtures/contracts/dummy.rs new file mode 100644 index 00000000..bde0d15e --- /dev/null +++ b/pallets/contracts/fixtures/contracts/dummy.rs @@ -0,0 +1,28 @@ +// This file is part of Substrate. + +// 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. +#![no_std] +#![no_main] + +extern crate common; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() {} diff --git a/pallets/contracts/fixtures/contracts/ecdsa_recover.rs b/pallets/contracts/fixtures/contracts/ecdsa_recover.rs new file mode 100644 index 00000000..0f28ca2c --- /dev/null +++ b/pallets/contracts/fixtures/contracts/ecdsa_recover.rs @@ -0,0 +1,44 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + signature: [u8; 65], + hash: [u8; 32], + ); + + let mut output = [0u8; 33]; + api::ecdsa_recover( + &signature[..].try_into().unwrap(), + &hash[..].try_into().unwrap(), + &mut output, + ) + .unwrap(); + api::return_value(uapi::ReturnFlags::empty(), &output); +} diff --git a/pallets/contracts/fixtures/contracts/event_and_return_on_deploy.rs b/pallets/contracts/fixtures/contracts/event_and_return_on_deploy.rs new file mode 100644 index 00000000..9186835d --- /dev/null +++ b/pallets/contracts/fixtures/contracts/event_and_return_on_deploy.rs @@ -0,0 +1,36 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +extern crate common; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() { + let buffer = [1u8, 2, 3, 4]; + api::deposit_event(&[0u8; 0], &buffer); + api::return_value(uapi::ReturnFlags::empty(), &buffer); +} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + unreachable!() +} diff --git a/pallets/contracts/fixtures/contracts/event_size.rs b/pallets/contracts/fixtures/contracts/event_size.rs new file mode 100644 index 00000000..e95130d3 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/event_size.rs @@ -0,0 +1,37 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(len: u32,); + + let buffer = [0u8; 16 * 1024 + 1]; + let data = &buffer[..len as usize]; + + api::deposit_event(&[0u8; 0], data); +} diff --git a/pallets/contracts/fixtures/contracts/float_instruction.rs b/pallets/contracts/fixtures/contracts/float_instruction.rs new file mode 100644 index 00000000..b1eaaf85 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/float_instruction.rs @@ -0,0 +1,34 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +extern crate common; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() {} + +#[no_mangle] +pub extern "C" fn add(a: f32, b: f32) -> f32 { + a + b +} diff --git a/pallets/contracts/fixtures/contracts/instantiate_return_code.rs b/pallets/contracts/fixtures/contracts/instantiate_return_code.rs new file mode 100644 index 00000000..de194abe --- /dev/null +++ b/pallets/contracts/fixtures/contracts/instantiate_return_code.rs @@ -0,0 +1,52 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(buffer, 36, code_hash: [u8; 32],); + let input = &buffer[32..]; + + let err_code = match api::instantiate_v2( + code_hash, + 0u64, // How much ref_time weight to devote for the execution. 0 = all. + 0u64, /* How much proof_size weight to devote for the execution. 0 = + * all. */ + None, // No deposit limit. + &10_000u64.to_le_bytes(), // Value to transfer. + input, + None, + None, + &[0u8; 0], // Empty salt. + ) { + Ok(_) => 0u32, + Err(code) => code as u32, + }; + + // Exit with success and take transfer return code to the output buffer. + api::return_value(uapi::ReturnFlags::empty(), &err_code.to_le_bytes()); +} diff --git a/pallets/contracts/fixtures/contracts/locking_delegate_dependency.rs b/pallets/contracts/fixtures/contracts/locking_delegate_dependency.rs new file mode 100644 index 00000000..bb76c942 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/locking_delegate_dependency.rs @@ -0,0 +1,68 @@ +// This file is part of Substrate. + +// 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. + +//! This contract tests the behavior of locking / unlocking delegate_dependencies when delegate +//! calling into a contract. +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +const ALICE: [u8; 32] = [1u8; 32]; + +/// Load input data and perform the action specified by the input. +/// If `delegate_call` is true, then delegate call into the contract. +fn load_input(delegate_call: bool) { + input!( + action: u32, + code_hash: [u8; 32], + ); + + match action { + // 1 = Lock delegate dependency + 1 => { + api::lock_delegate_dependency(code_hash); + }, + // 2 = Unlock delegate dependency + 2 => { + api::unlock_delegate_dependency(code_hash); + }, + // 3 = Terminate + 3 => { + api::terminate_v1(&ALICE); + }, + // Everything else is a noop + _ => {}, + } + + if delegate_call { + api::delegate_call(uapi::CallFlags::empty(), code_hash, &[], None).unwrap(); + } +} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() { + load_input(false); +} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + load_input(true); +} diff --git a/pallets/contracts/fixtures/contracts/multi_store.rs b/pallets/contracts/fixtures/contracts/multi_store.rs new file mode 100644 index 00000000..a78115f0 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/multi_store.rs @@ -0,0 +1,43 @@ +// This file is part of Substrate. + +// 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. + +//! Does two stores to two separate storage items +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + size1: u32, + size2: u32, + ); + + let buffer = [0u8; 16 * 1024]; + + // Place a values in storage sizes are specified in the input buffer. + // We don't care about the contents of the storage item. + api::set_storage(&[1u8; 32], &buffer[0..size1 as _]); + api::set_storage(&[2u8; 32], &buffer[0..size2 as _]); +} diff --git a/pallets/contracts/fixtures/contracts/new_set_code_hash_contract.rs b/pallets/contracts/fixtures/contracts/new_set_code_hash_contract.rs new file mode 100644 index 00000000..2a59b6e3 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/new_set_code_hash_contract.rs @@ -0,0 +1,32 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +extern crate common; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + api::return_value(uapi::ReturnFlags::empty(), &2u32.to_le_bytes()); +} diff --git a/pallets/contracts/fixtures/contracts/ok_trap_revert.rs b/pallets/contracts/fixtures/contracts/ok_trap_revert.rs new file mode 100644 index 00000000..55115f86 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/ok_trap_revert.rs @@ -0,0 +1,44 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() { + ok_trap_revert(); +} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + ok_trap_revert(); +} + +#[no_mangle] +fn ok_trap_revert() { + input!(buffer, 4,); + match buffer.first().unwrap_or(&0) { + 1 => api::return_value(uapi::ReturnFlags::REVERT, &[0u8; 0]), + 2 => panic!(), + _ => {}, + }; +} diff --git a/pallets/contracts/fixtures/contracts/read_only_call.rs b/pallets/contracts/fixtures/contracts/read_only_call.rs new file mode 100644 index 00000000..524fe50b --- /dev/null +++ b/pallets/contracts/fixtures/contracts/read_only_call.rs @@ -0,0 +1,50 @@ +// This file is part of Substrate. + +// 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. + +// This fixture tests if read-only call works as expected. +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + 256, + callee_addr: [u8; 32], + callee_input: [u8], + ); + + // Call the callee + api::call_v2( + uapi::CallFlags::READ_ONLY, + callee_addr, + 0u64, // How much ref_time to devote for the execution. 0 = all. + 0u64, // How much proof_size to devote for the execution. 0 = all. + None, // No deposit limit. + &0u64.to_le_bytes(), // Value transferred to the contract. + callee_input, + None, + ) + .unwrap(); +} diff --git a/pallets/contracts/fixtures/contracts/recurse.rs b/pallets/contracts/fixtures/contracts/recurse.rs new file mode 100644 index 00000000..b1ded608 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/recurse.rs @@ -0,0 +1,53 @@ +// This file is part of Substrate. + +// 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. + +//! This fixture calls itself as many times as passed as argument. + +#![no_std] +#![no_main] + +use common::{input, output}; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(calls_left: u32, ); + + // own address + output!(addr, [0u8; 32], api::address,); + + if calls_left == 0 { + return + } + + api::call_v2( + uapi::CallFlags::ALLOW_REENTRY, + addr, + 0u64, // How much ref_time to devote for the execution. 0 = all. + 0u64, // How much deposit_limit to devote for the execution. 0 = all. + None, // No deposit limit. + &0u64.to_le_bytes(), // Value transferred to the contract. + &(calls_left - 1).to_le_bytes(), + None, + ) + .unwrap(); +} diff --git a/pallets/contracts/fixtures/contracts/reentrance_count_call.rs b/pallets/contracts/fixtures/contracts/reentrance_count_call.rs new file mode 100644 index 00000000..0acfe017 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/reentrance_count_call.rs @@ -0,0 +1,57 @@ +// This file is part of Substrate. + +// 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. + +// This fixture tests if account_reentrance_count works as expected. +#![no_std] +#![no_main] + +use common::{input, output}; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(expected_reentrance_count: u32,); + + // Read the contract address. + output!(addr, [0u8; 32], api::address,); + + #[allow(deprecated)] + let reentrance_count = api::reentrance_count(); + assert_eq!(reentrance_count, expected_reentrance_count); + + // Re-enter 5 times in a row and assert that the reentrant counter works as expected. + if expected_reentrance_count != 5 { + let count = (expected_reentrance_count + 1).to_le_bytes(); + + api::call_v2( + uapi::CallFlags::ALLOW_REENTRY, + addr, + 0u64, // How much ref_time to devote for the execution. 0 = all. + 0u64, // How much proof_size to devote for the execution. 0 = all. + None, // No deposit limit. + &0u64.to_le_bytes(), // Value transferred to the contract. + &count, + None, + ) + .unwrap(); + } +} diff --git a/pallets/contracts/fixtures/contracts/reentrance_count_delegated_call.rs b/pallets/contracts/fixtures/contracts/reentrance_count_delegated_call.rs new file mode 100644 index 00000000..9baf9503 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/reentrance_count_delegated_call.rs @@ -0,0 +1,53 @@ +// This file is part of Substrate. + +// 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. + +// This fixture tests if account_reentrance_count works as expected. +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + input, + code_hash: [u8; 32], + call_stack_height: u32, + ); + + let call_stack_height = call_stack_height + 1; + + #[allow(deprecated)] + let reentrance_count = api::reentrance_count(); + + // Reentrance count stays 0. + assert_eq!(reentrance_count, 0); + + // Re-enter 5 times in a row and assert that the reentrant counter works as expected. + if call_stack_height != 5 { + let mut input = [0u8; 36]; + input[0..32].copy_from_slice(code_hash); + input[32..36].copy_from_slice(&call_stack_height.to_le_bytes()); + api::delegate_call(uapi::CallFlags::empty(), code_hash, &input, None).unwrap(); + } +} diff --git a/pallets/contracts/fixtures/contracts/return_with_data.rs b/pallets/contracts/fixtures/contracts/return_with_data.rs new file mode 100644 index 00000000..26f74edb --- /dev/null +++ b/pallets/contracts/fixtures/contracts/return_with_data.rs @@ -0,0 +1,47 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() { + call(); +} + +/// Reads the first byte as the exit status and copy all but the first 4 bytes of the input as +/// output data. +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + input, 128, + exit_status: [u8; 4], + output: [u8], + ); + + // Burn some PoV, clear_storage consumes some PoV as in order to clear the storage we need to we + // need to read its size first. + api::clear_storage_v1(b""); + + let exit_status = uapi::ReturnFlags::from_bits(exit_status[0] as u32).unwrap(); + api::return_value(exit_status, output); +} diff --git a/pallets/contracts/fixtures/contracts/run_out_of_gas.rs b/pallets/contracts/fixtures/contracts/run_out_of_gas.rs new file mode 100644 index 00000000..11eaaa7c --- /dev/null +++ b/pallets/contracts/fixtures/contracts/run_out_of_gas.rs @@ -0,0 +1,32 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +extern crate common; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + #[allow(clippy::empty_loop)] + loop {} +} diff --git a/pallets/contracts/fixtures/contracts/self_destruct.rs b/pallets/contracts/fixtures/contracts/self_destruct.rs new file mode 100644 index 00000000..d3836d2d --- /dev/null +++ b/pallets/contracts/fixtures/contracts/self_destruct.rs @@ -0,0 +1,55 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::{input, output}; +use uapi::{HostFn, HostFnImpl as api}; + +const DJANGO: [u8; 32] = [4u8; 32]; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + // If the input data is not empty, then recursively call self with empty input data. + // This should trap instead of self-destructing since a contract cannot be removed, while it's + // in the execution stack. If the recursive call traps, then trap here as well. + input!(input, 4,); + + if !input.is_empty() { + output!(addr, [0u8; 32], api::address,); + api::call_v2( + uapi::CallFlags::ALLOW_REENTRY, + addr, + 0u64, // How much ref_time to devote for the execution. 0 = all. + 0u64, // How much proof_size to devote for the execution. 0 = all. + None, // No deposit limit. + &0u64.to_le_bytes(), // Value to transfer. + &[0u8; 0], + None, + ) + .unwrap(); + } else { + // Try to terminate and give balance to django. + api::terminate_v1(&DJANGO); + } +} diff --git a/pallets/contracts/fixtures/contracts/self_destructing_constructor.rs b/pallets/contracts/fixtures/contracts/self_destructing_constructor.rs new file mode 100644 index 00000000..97b6759b --- /dev/null +++ b/pallets/contracts/fixtures/contracts/self_destructing_constructor.rs @@ -0,0 +1,32 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +extern crate common; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() { + api::terminate_v1(&[0u8; 32]); +} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() {} diff --git a/pallets/contracts/fixtures/contracts/set_code_hash.rs b/pallets/contracts/fixtures/contracts/set_code_hash.rs new file mode 100644 index 00000000..e3cf4bec --- /dev/null +++ b/pallets/contracts/fixtures/contracts/set_code_hash.rs @@ -0,0 +1,37 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(addr: [u8; 32],); + api::set_code_hash(addr).unwrap(); + + // we return 1 after setting new code_hash + // next `call` will NOT return this value, because contract code has been changed + api::return_value(uapi::ReturnFlags::empty(), &1u32.to_le_bytes()); +} diff --git a/pallets/contracts/fixtures/contracts/set_empty_storage.rs b/pallets/contracts/fixtures/contracts/set_empty_storage.rs new file mode 100644 index 00000000..e7366630 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/set_empty_storage.rs @@ -0,0 +1,32 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +extern crate common; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + api::set_storage(&[0u8; 32], &[0u8; 4]); +} diff --git a/pallets/contracts/fixtures/contracts/set_transient_storage.rs b/pallets/contracts/fixtures/contracts/set_transient_storage.rs new file mode 100644 index 00000000..e4fde083 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/set_transient_storage.rs @@ -0,0 +1,42 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(len: u32, ); + + let buffer = [0u8; 16 * 1024]; + let data = &buffer[..len as usize]; + + // Place a garbage value in the transient storage, with the size specified by the call input. + let mut key = [0u8; 32]; + key[0] = 1; + + #[allow(deprecated)] + api::set_transient_storage(&key, data); +} diff --git a/pallets/contracts/fixtures/contracts/sr25519_verify.rs b/pallets/contracts/fixtures/contracts/sr25519_verify.rs new file mode 100644 index 00000000..8920ce0d --- /dev/null +++ b/pallets/contracts/fixtures/contracts/sr25519_verify.rs @@ -0,0 +1,48 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + signature: [u8; 64], + pub_key: [u8; 32], + msg: [u8; 11], + ); + + let exit_status = match api::sr25519_verify( + &signature.try_into().unwrap(), + msg, + &pub_key.try_into().unwrap(), + ) { + Ok(_) => 0u32, + Err(code) => code as u32, + }; + + // Exit with success and take transfer return code to the output buffer. + api::return_value(uapi::ReturnFlags::empty(), &exit_status.to_le_bytes()); +} diff --git a/pallets/contracts/fixtures/contracts/storage.rs b/pallets/contracts/fixtures/contracts/storage.rs new file mode 100644 index 00000000..6fa97224 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/storage.rs @@ -0,0 +1,63 @@ +// This file is part of Substrate. + +// 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. + +//! This contract tests the storage APIs. It sets and clears storage values using the different +//! versions of the storage APIs. +#![no_std] +#![no_main] + +use common::unwrap_output; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + const KEY: [u8; 32] = [1u8; 32]; + const VALUE_1: [u8; 4] = [1u8; 4]; + const VALUE_2: [u8; 4] = [2u8; 4]; + const VALUE_3: [u8; 4] = [3u8; 4]; + + api::set_storage(&KEY, &VALUE_1); + assert_eq!(api::contains_storage(&KEY), Some(VALUE_1.len() as _)); + unwrap_output!(val, [0u8; 4], api::get_storage, &KEY); + assert_eq!(**val, VALUE_1); + + let existing = api::set_storage_v1(&KEY, &VALUE_2); + assert_eq!(existing, Some(VALUE_1.len() as _)); + unwrap_output!(val, [0u8; 4], api::get_storage, &KEY); + assert_eq!(**val, VALUE_2); + + api::clear_storage(&KEY); + assert_eq!(api::contains_storage(&KEY), None); + + let existing = api::set_storage_v2(&KEY, &VALUE_3); + assert_eq!(existing, None); + assert_eq!(api::contains_storage_v1(&KEY), Some(VALUE_1.len() as _)); + unwrap_output!(val, [0u8; 32], api::get_storage_v1, &KEY); + assert_eq!(**val, VALUE_3); + + api::clear_storage_v1(&KEY); + assert_eq!(api::contains_storage_v1(&KEY), None); + let existing = api::set_storage_v2(&KEY, &VALUE_3); + assert_eq!(existing, None); + unwrap_output!(val, [0u8; 32], api::take_storage, &KEY); + assert_eq!(**val, VALUE_3); +} diff --git a/pallets/contracts/fixtures/contracts/storage_size.rs b/pallets/contracts/fixtures/contracts/storage_size.rs new file mode 100644 index 00000000..744ffe85 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/storage_size.rs @@ -0,0 +1,45 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(len: u32, ); + + let mut buffer = [0u8; 16 * 1024 + 1]; + let data = &buffer[..len as usize]; + + // Place a garbage value in storage, the size of which is specified by the call input. + let mut key = [0u8; 32]; + key[0] = 1; + + api::set_storage(&key, data); + + let data = &mut &mut buffer[..]; + api::get_storage(&key, data).unwrap(); + assert_eq!(data.len(), len as usize); +} diff --git a/pallets/contracts/fixtures/contracts/store_call.rs b/pallets/contracts/fixtures/contracts/store_call.rs new file mode 100644 index 00000000..d20d811c --- /dev/null +++ b/pallets/contracts/fixtures/contracts/store_call.rs @@ -0,0 +1,41 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(len: u32, ); + + let buffer = [0u8; 16 * 1024 + 1]; + let data = &buffer[..len as usize]; + + // Place a garbage value in storage, the size of which is specified by the call input. + let mut key = [0u8; 32]; + key[0] = 1; + + api::set_storage(&key, data); +} diff --git a/pallets/contracts/fixtures/contracts/store_deploy.rs b/pallets/contracts/fixtures/contracts/store_deploy.rs new file mode 100644 index 00000000..26f3b86b --- /dev/null +++ b/pallets/contracts/fixtures/contracts/store_deploy.rs @@ -0,0 +1,41 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() { + input!(len: u32, ); + + let buffer = [0u8; 16 * 1024 + 1]; + let data = &buffer[..len as usize]; + + // place a garbage value in storage, the size of which is specified by the call input. + let mut key = [0u8; 32]; + key[0] = 1; + + api::set_storage(&key, data); +} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() {} diff --git a/pallets/contracts/fixtures/contracts/transfer_return_code.rs b/pallets/contracts/fixtures/contracts/transfer_return_code.rs new file mode 100644 index 00000000..d3f6a1dd --- /dev/null +++ b/pallets/contracts/fixtures/contracts/transfer_return_code.rs @@ -0,0 +1,38 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +extern crate common; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + let ret_code = match api::transfer(&[0u8; 32], &100u64.to_le_bytes()) { + Ok(_) => 0u32, + Err(code) => code as u32, + }; + + // Exit with success and take transfer return code to the output buffer. + api::return_value(uapi::ReturnFlags::empty(), &ret_code.to_le_bytes()); +} diff --git a/pallets/contracts/fixtures/contracts/transient_storage.rs b/pallets/contracts/fixtures/contracts/transient_storage.rs new file mode 100644 index 00000000..c797e178 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/transient_storage.rs @@ -0,0 +1,58 @@ +// This file is part of Substrate. + +// 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. + +//! This contract tests the transient storage APIs. +#![no_std] +#![no_main] + +use common::unwrap_output; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + const KEY: [u8; 32] = [1u8; 32]; + const VALUE_1: [u8; 4] = [1u8; 4]; + const VALUE_2: [u8; 4] = [2u8; 4]; + const VALUE_3: [u8; 4] = [3u8; 4]; + + #[allow(deprecated)] + { + let existing = api::set_transient_storage(&KEY, &VALUE_1); + assert_eq!(existing, None); + assert_eq!(api::contains_transient_storage(&KEY), Some(VALUE_1.len() as _)); + unwrap_output!(val, [0u8; 4], api::get_transient_storage, &KEY); + assert_eq!(**val, VALUE_1); + + let existing = api::set_transient_storage(&KEY, &VALUE_2); + assert_eq!(existing, Some(VALUE_1.len() as _)); + unwrap_output!(val, [0u8; 4], api::get_transient_storage, &KEY); + assert_eq!(**val, VALUE_2); + + api::clear_transient_storage(&KEY); + assert_eq!(api::contains_transient_storage(&KEY), None); + + let existing = api::set_transient_storage(&KEY, &VALUE_3); + assert_eq!(existing, None); + unwrap_output!(val, [0u8; 32], api::take_transient_storage, &KEY); + assert_eq!(**val, VALUE_3); + } +} diff --git a/pallets/contracts/fixtures/contracts/xcm_execute.rs b/pallets/contracts/fixtures/contracts/xcm_execute.rs new file mode 100644 index 00000000..1d570ffe --- /dev/null +++ b/pallets/contracts/fixtures/contracts/xcm_execute.rs @@ -0,0 +1,40 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!(512, msg: [u8],); + + #[allow(deprecated)] + let err_code = match api::xcm_execute(msg) { + Ok(_) => 0u32, + Err(code) => code as u32, + }; + + api::return_value(uapi::ReturnFlags::empty(), &err_code.to_le_bytes()); +} diff --git a/pallets/contracts/fixtures/contracts/xcm_send.rs b/pallets/contracts/fixtures/contracts/xcm_send.rs new file mode 100644 index 00000000..6d4629e7 --- /dev/null +++ b/pallets/contracts/fixtures/contracts/xcm_send.rs @@ -0,0 +1,42 @@ +// This file is part of Substrate. + +// 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. + +#![no_std] +#![no_main] + +use common::input; +use uapi::{HostFn, HostFnImpl as api}; + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn deploy() {} + +#[no_mangle] +#[polkavm_derive::polkavm_export] +pub extern "C" fn call() { + input!( + 512, + dest: [u8; 3], + msg: [u8], + ); + + let mut message_id = [0u8; 32]; + + #[allow(deprecated)] + api::xcm_send(dest, msg, &mut message_id).unwrap(); + api::return_value(uapi::ReturnFlags::empty(), &message_id); +} diff --git a/pallets/contracts/fixtures/src/lib.rs b/pallets/contracts/fixtures/src/lib.rs new file mode 100644 index 00000000..56a8e232 --- /dev/null +++ b/pallets/contracts/fixtures/src/lib.rs @@ -0,0 +1,45 @@ +// This file is part of Substrate. + +// 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 sp_runtime::traits::Hash; +use std::{fs, path::PathBuf}; + +/// Load a given wasm module and returns a wasm binary contents along with it's hash. +/// Use the legacy compile_module as fallback, if the rust fixture does not exist yet. +pub fn compile_module( + fixture_name: &str, +) -> anyhow::Result<(Vec, ::Output)> +where + T: frame_system::Config, +{ + let out_dir: PathBuf = env!("OUT_DIR").into(); + let fixture_path = out_dir.join(format!("{fixture_name}.wasm")); + let binary = fs::read(fixture_path)?; + let code_hash = T::Hashing::hash(&binary); + Ok((binary, code_hash)) +} + +#[cfg(test)] +mod test { + #[test] + fn out_dir_should_have_compiled_mocks() { + let out_dir: std::path::PathBuf = env!("OUT_DIR").into(); + assert!(out_dir.join("dummy.wasm").exists()); + #[cfg(feature = "riscv")] + assert!(out_dir.join("dummy.polkavm").exists()); + } +} diff --git a/pallets/contracts/mock-network/Cargo.toml b/pallets/contracts/mock-network/Cargo.toml new file mode 100644 index 00000000..810304c2 --- /dev/null +++ b/pallets/contracts/mock-network/Cargo.toml @@ -0,0 +1,87 @@ +[package] +name = "pallet-contracts-mock-network" +version = "3.0.0" +authors.workspace = true +edition.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true +description = "A mock network for testing pallet-contracts" + +[dependencies] +codec = { features = ["derive", "max-encoded-len"], workspace = true } + +frame-support = { workspace = true } +frame-system = { workspace = true } +pallet-assets = { workspace = true, default-features = true } +pallet-balances = { workspace = true, default-features = true } +pallet-contracts = { workspace = true, default-features = true } +pallet-contracts-uapi = { workspace = true } +pallet-contracts-proc-macro = { workspace = true, default-features = true } +pallet-insecure-randomness-collective-flip = { workspace = true, default-features = true } +pallet-message-queue = { workspace = true, default-features = true } +pallet-proxy = { workspace = true, default-features = true } +pallet-timestamp = { workspace = true, default-features = true } +pallet-utility = { workspace = true, default-features = true } +pallet-xcm = { workspace = true } +polkadot-parachain-primitives = { workspace = true, default-features = true } +polkadot-primitives = { workspace = true, default-features = true } +polkadot-runtime-parachains = { workspace = true, default-features = true } +scale-info = { features = ["derive"], workspace = true } +sp-api = { workspace = true } +sp-core = { workspace = true } +sp-io = { workspace = true } +sp-keystore = { workspace = true, default-features = true } +sp-runtime = { workspace = true } +sp-tracing = { workspace = true, default-features = true } +xcm = { workspace = true } +xcm-builder = { workspace = true, default-features = true } +xcm-executor = { workspace = true } +xcm-simulator = { workspace = true, default-features = true } + +[dev-dependencies] +assert_matches = { workspace = true } +pretty_assertions = { workspace = true } +pallet-contracts-fixtures = { workspace = true } + +[features] +default = ["std"] +std = [ + "codec/std", + "frame-support/std", + "frame-system/std", + "pallet-balances/std", + "pallet-contracts/std", + "pallet-insecure-randomness-collective-flip/std", + "pallet-proxy/std", + "pallet-timestamp/std", + "pallet-utility/std", + "pallet-xcm/std", + "scale-info/std", + "sp-api/std", + "sp-core/std", + "sp-io/std", + "sp-keystore/std", + "sp-runtime/std", + "xcm-executor/std", + "xcm/std", +] + +runtime-benchmarks = [ + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "pallet-assets/runtime-benchmarks", + "pallet-balances/runtime-benchmarks", + "pallet-contracts/runtime-benchmarks", + "pallet-message-queue/runtime-benchmarks", + "pallet-proxy/runtime-benchmarks", + "pallet-timestamp/runtime-benchmarks", + "pallet-utility/runtime-benchmarks", + "pallet-xcm/runtime-benchmarks", + "polkadot-parachain-primitives/runtime-benchmarks", + "polkadot-primitives/runtime-benchmarks", + "polkadot-runtime-parachains/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", + "xcm-builder/runtime-benchmarks", + "xcm-executor/runtime-benchmarks", +] diff --git a/pallets/contracts/mock-network/src/lib.rs b/pallets/contracts/mock-network/src/lib.rs new file mode 100644 index 00000000..34cc95f2 --- /dev/null +++ b/pallets/contracts/mock-network/src/lib.rs @@ -0,0 +1,152 @@ +// Copyright 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 . + +pub mod mocks; +pub mod parachain; +pub mod primitives; +pub mod relay_chain; + +#[cfg(test)] +mod tests; + +use crate::primitives::{AccountId, UNITS}; +pub use pallet_contracts::test_utils::{ALICE, BOB}; +use sp_runtime::BuildStorage; +use xcm::latest::prelude::*; +use xcm_executor::traits::ConvertLocation; +pub use xcm_simulator::TestExt; +use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain}; + +// Accounts +pub const ADMIN: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([0u8; 32]); + +// Balances +pub const INITIAL_BALANCE: u128 = 1_000_000_000 * UNITS; + +decl_test_parachain! { + pub struct ParaA { + Runtime = parachain::Runtime, + XcmpMessageHandler = parachain::MsgQueue, + DmpMessageHandler = parachain::MsgQueue, + new_ext = para_ext(1), + } +} + +decl_test_relay_chain! { + pub struct Relay { + Runtime = relay_chain::Runtime, + RuntimeCall = relay_chain::RuntimeCall, + RuntimeEvent = relay_chain::RuntimeEvent, + XcmConfig = relay_chain::XcmConfig, + MessageQueue = relay_chain::MessageQueue, + System = relay_chain::System, + new_ext = relay_ext(), + } +} + +decl_test_network! { + pub struct MockNet { + relay_chain = Relay, + parachains = vec![ + (1, ParaA), + ], + } +} + +pub fn relay_sovereign_account_id() -> AccountId { + let location: Location = (Parent,).into(); + parachain::SovereignAccountOf::convert_location(&location).unwrap() +} + +pub fn parachain_sovereign_account_id(para: u32) -> AccountId { + let location: Location = (Parachain(para),).into(); + relay_chain::SovereignAccountOf::convert_location(&location).unwrap() +} + +pub fn parachain_account_sovereign_account_id( + para: u32, + who: sp_runtime::AccountId32, +) -> AccountId { + let location: Location = ( + Parachain(para), + AccountId32 { network: Some(relay_chain::RelayNetwork::get()), id: who.into() }, + ) + .into(); + relay_chain::SovereignAccountOf::convert_location(&location).unwrap() +} + +pub fn para_ext(para_id: u32) -> sp_io::TestExternalities { + use parachain::{MsgQueue, Runtime, System}; + + let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); + + pallet_balances::GenesisConfig:: { + balances: vec![ + (ALICE, INITIAL_BALANCE), + (relay_sovereign_account_id(), INITIAL_BALANCE), + (BOB, INITIAL_BALANCE), + ], + } + .assimilate_storage(&mut t) + .unwrap(); + + pallet_assets::GenesisConfig:: { + assets: vec![ + (0u128, ADMIN, false, 1u128), // Create derivative asset for relay's native token + ], + metadata: Default::default(), + accounts: vec![ + (0u128, ALICE, INITIAL_BALANCE), + (0u128, relay_sovereign_account_id(), INITIAL_BALANCE), + ], + next_asset_id: None, + } + .assimilate_storage(&mut t) + .unwrap(); + + let mut ext = sp_io::TestExternalities::new(t); + ext.execute_with(|| { + sp_tracing::try_init_simple(); + System::set_block_number(1); + MsgQueue::set_para_id(para_id.into()); + }); + ext +} + +pub fn relay_ext() -> sp_io::TestExternalities { + use relay_chain::{Runtime, System}; + + let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); + + pallet_balances::GenesisConfig:: { + balances: vec![ + (ALICE, INITIAL_BALANCE), + (parachain_sovereign_account_id(1), INITIAL_BALANCE), + (parachain_account_sovereign_account_id(1, ALICE), INITIAL_BALANCE), + ], + } + .assimilate_storage(&mut t) + .unwrap(); + + let mut ext = sp_io::TestExternalities::new(t); + ext.execute_with(|| { + System::set_block_number(1); + }); + ext +} + +pub type ParachainPalletXcm = pallet_xcm::Pallet; +pub type ParachainBalances = pallet_balances::Pallet; diff --git a/pallets/contracts/mock-network/src/mocks.rs b/pallets/contracts/mock-network/src/mocks.rs new file mode 100644 index 00000000..bf3baec7 --- /dev/null +++ b/pallets/contracts/mock-network/src/mocks.rs @@ -0,0 +1,18 @@ +// Copyright 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 . + +pub mod msg_queue; +pub mod relay_message_queue; diff --git a/pallets/contracts/mock-network/src/mocks/msg_queue.rs b/pallets/contracts/mock-network/src/mocks/msg_queue.rs new file mode 100644 index 00000000..6e922c16 --- /dev/null +++ b/pallets/contracts/mock-network/src/mocks/msg_queue.rs @@ -0,0 +1,186 @@ +// Copyright 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 . + +//! Parachain runtime mock. + +use codec::{Decode, Encode}; + +use frame_support::weights::Weight; +use polkadot_parachain_primitives::primitives::{ + DmpMessageHandler, Id as ParaId, XcmpMessageFormat, XcmpMessageHandler, +}; +use polkadot_primitives::BlockNumber as RelayBlockNumber; +use sp_runtime::traits::{Get, Hash}; + +use xcm::{latest::prelude::*, VersionedXcm}; + +#[frame_support::pallet] +pub mod pallet { + use super::*; + use frame_support::pallet_prelude::*; + + #[pallet::config] + pub trait Config: frame_system::Config { + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + type XcmExecutor: ExecuteXcm; + } + + #[pallet::call] + impl Pallet {} + + #[pallet::pallet] + #[pallet::without_storage_info] + pub struct Pallet(_); + + #[pallet::storage] + pub(super) type ParachainId = StorageValue<_, ParaId, ValueQuery>; + + #[pallet::storage] + /// A queue of received DMP messages + pub(super) type ReceivedDmp = StorageValue<_, Vec>, ValueQuery>; + + impl Get for Pallet { + fn get() -> ParaId { + ParachainId::::get() + } + } + + pub type MessageId = [u8; 32]; + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + /// Some XCM was executed OK. + Success(Option), + /// Some XCM failed. + Fail(Option, XcmError), + /// Bad XCM version used. + BadVersion(Option), + /// Bad XCM format used. + BadFormat(Option), + + // DMP + /// Downward message is invalid XCM. + InvalidFormat(MessageId), + /// Downward message is unsupported version of XCM. + UnsupportedVersion(MessageId), + /// Downward message executed with the given outcome. + ExecutedDownward(MessageId, Outcome), + } + + impl Pallet { + pub fn set_para_id(para_id: ParaId) { + ParachainId::::put(para_id); + } + + pub fn parachain_id() -> ParaId { + ParachainId::::get() + } + + pub fn received_dmp() -> Vec> { + ReceivedDmp::::get() + } + + fn handle_xcmp_message( + sender: ParaId, + _sent_at: RelayBlockNumber, + xcm: VersionedXcm, + max_weight: Weight, + ) -> Result { + let hash = Encode::using_encoded(&xcm, T::Hashing::hash); + let mut message_hash = Encode::using_encoded(&xcm, sp_io::hashing::blake2_256); + let (result, event) = match Xcm::::try_from(xcm) { + Ok(xcm) => { + let location = (Parent, Parachain(sender.into())); + match T::XcmExecutor::prepare_and_execute( + location, + xcm, + &mut message_hash, + max_weight, + Weight::zero(), + ) { + Outcome::Error { error } => (Err(error), Event::Fail(Some(hash), error)), + Outcome::Complete { used } => (Ok(used), Event::Success(Some(hash))), + // As far as the caller is concerned, this was dispatched without error, so + // we just report the weight used. + Outcome::Incomplete { used, error } => + (Ok(used), Event::Fail(Some(hash), error)), + } + }, + Err(()) => (Err(XcmError::UnhandledXcmVersion), Event::BadVersion(Some(hash))), + }; + Self::deposit_event(event); + result + } + } + + impl XcmpMessageHandler for Pallet { + fn handle_xcmp_messages<'a, I: Iterator>( + iter: I, + max_weight: Weight, + ) -> Weight { + for (sender, sent_at, data) in iter { + let mut data_ref = data; + let _ = XcmpMessageFormat::decode(&mut data_ref) + .expect("Simulator encodes with versioned xcm format; qed"); + + let mut remaining_fragments = data_ref; + while !remaining_fragments.is_empty() { + if let Ok(xcm) = + VersionedXcm::::decode(&mut remaining_fragments) + { + let _ = Self::handle_xcmp_message(sender, sent_at, xcm, max_weight); + } else { + debug_assert!(false, "Invalid incoming XCMP message data"); + } + } + } + max_weight + } + } + + impl DmpMessageHandler for Pallet { + fn handle_dmp_messages( + iter: impl Iterator)>, + limit: Weight, + ) -> Weight { + for (_i, (_sent_at, data)) in iter.enumerate() { + let mut id = sp_io::hashing::blake2_256(&data[..]); + let maybe_versioned = VersionedXcm::::decode(&mut &data[..]); + match maybe_versioned { + Err(_) => { + Self::deposit_event(Event::InvalidFormat(id)); + }, + Ok(versioned) => match Xcm::try_from(versioned) { + Err(()) => Self::deposit_event(Event::UnsupportedVersion(id)), + Ok(x) => { + let outcome = T::XcmExecutor::prepare_and_execute( + Parent, + x.clone(), + &mut id, + limit, + Weight::zero(), + ); + ReceivedDmp::::append(x); + Self::deposit_event(Event::ExecutedDownward(id, outcome)); + }, + }, + } + } + limit + } + } +} diff --git a/pallets/contracts/mock-network/src/mocks/relay_message_queue.rs b/pallets/contracts/mock-network/src/mocks/relay_message_queue.rs new file mode 100644 index 00000000..14099965 --- /dev/null +++ b/pallets/contracts/mock-network/src/mocks/relay_message_queue.rs @@ -0,0 +1,52 @@ +// Copyright 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 frame_support::{parameter_types, weights::Weight}; +use xcm::latest::prelude::*; +use xcm_simulator::{ + AggregateMessageOrigin, ProcessMessage, ProcessMessageError, UmpQueueId, WeightMeter, +}; + +use crate::relay_chain::{RuntimeCall, XcmConfig}; + +parameter_types! { + /// Amount of weight that can be spent per block to service messages. + pub MessageQueueServiceWeight: Weight = Weight::from_parts(1_000_000_000, 1_000_000); + pub const MessageQueueHeapSize: u32 = 65_536; + pub const MessageQueueMaxStale: u32 = 16; +} + +/// Message processor to handle any messages that were enqueued into the `MessageQueue` pallet. +pub struct MessageProcessor; +impl ProcessMessage for MessageProcessor { + type Origin = AggregateMessageOrigin; + + fn process_message( + message: &[u8], + origin: Self::Origin, + meter: &mut WeightMeter, + id: &mut [u8; 32], + ) -> Result { + let para = match origin { + AggregateMessageOrigin::Ump(UmpQueueId::Para(para)) => para, + }; + xcm_builder::ProcessXcmMessage::< + Junction, + xcm_executor::XcmExecutor, + RuntimeCall, + >::process_message(message, Junction::Parachain(para.into()), meter, id) + } +} diff --git a/pallets/contracts/mock-network/src/parachain.rs b/pallets/contracts/mock-network/src/parachain.rs new file mode 100644 index 00000000..3579b46e --- /dev/null +++ b/pallets/contracts/mock-network/src/parachain.rs @@ -0,0 +1,346 @@ +// Copyright 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 . + +//! Parachain runtime mock. + +mod contracts_config; +use crate::{ + mocks::msg_queue::pallet as mock_msg_queue, + primitives::{AccountId, AssetIdForAssets, Balance}, +}; +use core::marker::PhantomData; +use frame_support::{ + construct_runtime, derive_impl, parameter_types, + traits::{AsEnsureOriginWithArg, Contains, ContainsPair, Everything, EverythingBut, Nothing}, + weights::{ + constants::{WEIGHT_PROOF_SIZE_PER_MB, WEIGHT_REF_TIME_PER_SECOND}, + Weight, + }, +}; +use frame_system::{EnsureRoot, EnsureSigned}; +use pallet_xcm::XcmPassthrough; +use sp_core::{ConstU32, ConstU64, H256}; +use sp_runtime::traits::{Get, IdentityLookup, MaybeEquivalence}; + +use xcm::latest::prelude::*; +use xcm_builder::{ + AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowTopLevelPaidExecutionFrom, + ConvertedConcreteId, EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, + FrameTransactionalProcessor, FungibleAdapter, FungiblesAdapter, IsConcrete, NativeAsset, + NoChecking, ParentAsSuperuser, ParentIsPreset, SignedAccountId32AsNative, SignedToAccountId32, + SovereignSignedViaLocation, WithComputedOrigin, +}; +use xcm_executor::{traits::JustTry, Config, XcmExecutor}; + +pub type SovereignAccountOf = + (AccountId32Aliases, ParentIsPreset); + +#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] +impl frame_system::Config for Runtime { + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type Nonce = u64; + type Block = Block; + type Hash = H256; + type Hashing = ::sp_runtime::traits::BlakeTwo256; + type AccountId = AccountId; + type Lookup = IdentityLookup; + type RuntimeEvent = RuntimeEvent; + type BlockWeights = (); + type BlockLength = (); + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = pallet_balances::AccountData; + type OnNewAccount = (); + type OnKilledAccount = (); + type DbWeight = (); + type BaseCallFilter = Everything; + type SystemWeightInfo = (); + type SS58Prefix = (); + type OnSetCode = (); + type MaxConsumers = ConstU32<16>; +} + +parameter_types! { + pub ExistentialDeposit: Balance = 1; + pub const MaxLocks: u32 = 50; + pub const MaxReserves: u32 = 50; +} + +impl pallet_balances::Config for Runtime { + type AccountStore = System; + type Balance = Balance; + type DustRemoval = (); + type ExistentialDeposit = ExistentialDeposit; + type FreezeIdentifier = (); + type MaxFreezes = ConstU32<0>; + type MaxLocks = MaxLocks; + type MaxReserves = MaxReserves; + type ReserveIdentifier = [u8; 8]; + type RuntimeEvent = RuntimeEvent; + type RuntimeHoldReason = RuntimeHoldReason; + type RuntimeFreezeReason = RuntimeFreezeReason; + type WeightInfo = (); +} + +parameter_types! { + pub const AssetDeposit: u128 = 1_000_000; + pub const MetadataDepositBase: u128 = 1_000_000; + pub const MetadataDepositPerByte: u128 = 100_000; + pub const AssetAccountDeposit: u128 = 1_000_000; + pub const ApprovalDeposit: u128 = 1_000_000; + pub const AssetsStringLimit: u32 = 50; + pub const RemoveItemsLimit: u32 = 50; +} + +impl pallet_assets::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type Balance = Balance; + type AssetId = AssetIdForAssets; + type Currency = Balances; + type CreateOrigin = AsEnsureOriginWithArg>; + type ForceOrigin = EnsureRoot; + type AssetDeposit = AssetDeposit; + type MetadataDepositBase = MetadataDepositBase; + type MetadataDepositPerByte = MetadataDepositPerByte; + type AssetAccountDeposit = AssetAccountDeposit; + type ApprovalDeposit = ApprovalDeposit; + type StringLimit = AssetsStringLimit; + type Freezer = (); + type Extra = (); + type WeightInfo = (); + type RemoveItemsLimit = RemoveItemsLimit; + type AssetIdParameter = AssetIdForAssets; + type CallbackHandle = (); + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkHelper = (); +} + +parameter_types! { + pub const ReservedXcmpWeight: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_div(4), 0); + pub const ReservedDmpWeight: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_div(4), 0); +} + +parameter_types! { + pub const KsmLocation: Location = Location::parent(); + pub const TokenLocation: Location = Here.into_location(); + pub const RelayNetwork: NetworkId = ByGenesis([0; 32]); + pub UniversalLocation: InteriorLocation = [GlobalConsensus(RelayNetwork::get()), Parachain(MsgQueue::parachain_id().into())].into(); +} + +pub type XcmOriginToCallOrigin = ( + SovereignSignedViaLocation, + ParentAsSuperuser, + SignedAccountId32AsNative, + XcmPassthrough, +); + +parameter_types! { + pub const XcmInstructionWeight: Weight = Weight::from_parts(1_000, 1_000); + pub TokensPerSecondPerMegabyte: (AssetId, u128, u128) = (AssetId(Parent.into()), 1_000_000_000_000, 1024 * 1024); + pub const MaxInstructions: u32 = 100; + pub const MaxAssetsIntoHolding: u32 = 64; + pub ForeignPrefix: Location = (Parent,).into(); + pub CheckingAccount: AccountId = PolkadotXcm::check_account(); + pub TrustedLockPairs: (Location, AssetFilter) = + (Parent.into(), Wild(AllOf { id: AssetId(Parent.into()), fun: WildFungible })); +} + +pub fn estimate_message_fee(number_of_instructions: u64) -> u128 { + let weight = estimate_weight(number_of_instructions); + + estimate_fee_for_weight(weight) +} + +pub fn estimate_weight(number_of_instructions: u64) -> Weight { + XcmInstructionWeight::get().saturating_mul(number_of_instructions) +} + +pub fn estimate_fee_for_weight(weight: Weight) -> u128 { + let (_, units_per_second, units_per_mb) = TokensPerSecondPerMegabyte::get(); + + units_per_second * (weight.ref_time() as u128) / (WEIGHT_REF_TIME_PER_SECOND as u128) + + units_per_mb * (weight.proof_size() as u128) / (WEIGHT_PROOF_SIZE_PER_MB as u128) +} + +pub type LocalBalancesTransactor = + FungibleAdapter, SovereignAccountOf, AccountId, ()>; + +pub struct FromLocationToAsset(PhantomData<(Location, AssetId)>); +impl MaybeEquivalence + for FromLocationToAsset +{ + fn convert(value: &Location) -> Option { + match value.unpack() { + (1, []) => Some(0 as AssetIdForAssets), + (1, [Parachain(para_id)]) => Some(*para_id as AssetIdForAssets), + _ => None, + } + } + + fn convert_back(_id: &AssetIdForAssets) -> Option { + None + } +} + +pub type ForeignAssetsTransactor = FungiblesAdapter< + Assets, + ConvertedConcreteId< + AssetIdForAssets, + Balance, + FromLocationToAsset, + JustTry, + >, + SovereignAccountOf, + AccountId, + NoChecking, + CheckingAccount, +>; + +/// Means for transacting assets on this chain +pub type AssetTransactors = (LocalBalancesTransactor, ForeignAssetsTransactor); + +pub struct ParentRelay; +impl Contains for ParentRelay { + fn contains(location: &Location) -> bool { + location.contains_parents_only(1) + } +} +pub struct ThisParachain; +impl Contains for ThisParachain { + fn contains(location: &Location) -> bool { + matches!(location.unpack(), (0, [Junction::AccountId32 { .. }])) + } +} + +pub type XcmRouter = crate::ParachainXcmRouter; + +pub type Barrier = ( + xcm_builder::AllowUnpaidExecutionFrom, + WithComputedOrigin< + (AllowExplicitUnpaidExecutionFrom, AllowTopLevelPaidExecutionFrom), + UniversalLocation, + ConstU32<1>, + >, +); + +parameter_types! { + pub NftCollectionOne: AssetFilter + = Wild(AllOf { fun: WildNonFungible, id: AssetId((Parent, GeneralIndex(1)).into()) }); + pub NftCollectionOneForRelay: (AssetFilter, Location) + = (NftCollectionOne::get(), Parent.into()); + pub RelayNativeAsset: AssetFilter = Wild(AllOf { fun: WildFungible, id: AssetId((Parent, Here).into()) }); + pub RelayNativeAssetForRelay: (AssetFilter, Location) = (RelayNativeAsset::get(), Parent.into()); +} +pub type TrustedTeleporters = + (xcm_builder::Case, xcm_builder::Case); +pub type TrustedReserves = EverythingBut>; + +pub struct XcmConfig; +impl Config for XcmConfig { + type RuntimeCall = RuntimeCall; + type XcmSender = XcmRouter; + type AssetTransactor = AssetTransactors; + type OriginConverter = XcmOriginToCallOrigin; + type IsReserve = (NativeAsset, TrustedReserves); + type IsTeleporter = TrustedTeleporters; + type UniversalLocation = UniversalLocation; + type Barrier = Barrier; + type Weigher = FixedWeightBounds; + type Trader = FixedRateOfFungible; + type ResponseHandler = PolkadotXcm; + type AssetTrap = PolkadotXcm; + type AssetLocker = PolkadotXcm; + type AssetExchanger = (); + type AssetClaims = PolkadotXcm; + type SubscriptionService = PolkadotXcm; + type PalletInstancesInfo = AllPalletsWithSystem; + type FeeManager = (); + type MaxAssetsIntoHolding = MaxAssetsIntoHolding; + type MessageExporter = (); + type UniversalAliases = Nothing; + type CallDispatcher = RuntimeCall; + type SafeCallFilter = Everything; + type Aliasers = Nothing; + type TransactionalProcessor = FrameTransactionalProcessor; + type HrmpNewChannelOpenRequestHandler = (); + type HrmpChannelAcceptedHandler = (); + type HrmpChannelClosingHandler = (); + type XcmRecorder = PolkadotXcm; +} + +impl mock_msg_queue::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type XcmExecutor = XcmExecutor; +} + +pub type LocalOriginToLocation = SignedToAccountId32; + +pub struct TrustedLockerCase(PhantomData); +impl> ContainsPair for TrustedLockerCase { + fn contains(origin: &Location, asset: &Asset) -> bool { + let (o, a) = T::get(); + a.matches(asset) && &o == origin + } +} + +impl pallet_xcm::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type SendXcmOrigin = EnsureXcmOrigin; + type XcmRouter = XcmRouter; + type ExecuteXcmOrigin = EnsureXcmOrigin; + type XcmExecuteFilter = Everything; + type XcmExecutor = XcmExecutor; + type XcmTeleportFilter = Nothing; + type XcmReserveTransferFilter = Everything; + type Weigher = FixedWeightBounds; + type UniversalLocation = UniversalLocation; + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100; + type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion; + type Currency = Balances; + type CurrencyMatcher = IsConcrete; + type TrustedLockers = TrustedLockerCase; + type SovereignAccountOf = SovereignAccountOf; + type MaxLockers = ConstU32<8>; + type MaxRemoteLockConsumers = ConstU32<0>; + type RemoteLockConsumerIdentifier = (); + type WeightInfo = pallet_xcm::TestWeightInfo; + type AdminOrigin = EnsureRoot; +} + +type Block = frame_system::mocking::MockBlock; + +impl pallet_timestamp::Config for Runtime { + type Moment = u64; + type OnTimestampSet = (); + type MinimumPeriod = ConstU64<1>; + type WeightInfo = (); +} + +construct_runtime!( + pub enum Runtime + { + System: frame_system, + Balances: pallet_balances, + Timestamp: pallet_timestamp, + MsgQueue: mock_msg_queue, + PolkadotXcm: pallet_xcm, + Contracts: pallet_contracts, + Assets: pallet_assets, + } +); diff --git a/pallets/contracts/mock-network/src/parachain/contracts_config.rs b/pallets/contracts/mock-network/src/parachain/contracts_config.rs new file mode 100644 index 00000000..bf3c00b3 --- /dev/null +++ b/pallets/contracts/mock-network/src/parachain/contracts_config.rs @@ -0,0 +1,33 @@ +// Copyright 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::{Balances, Runtime, RuntimeCall, RuntimeEvent}; +use crate::parachain::RuntimeHoldReason; +use frame_support::{derive_impl, parameter_types}; + +parameter_types! { + pub Schedule: pallet_contracts::Schedule = Default::default(); +} + +#[derive_impl(pallet_contracts::config_preludes::TestDefaultConfig)] +impl pallet_contracts::Config for Runtime { + type AddressGenerator = pallet_contracts::DefaultAddressGenerator; + type CallStack = [pallet_contracts::Frame; 5]; + type Currency = Balances; + type Schedule = Schedule; + type Time = super::Timestamp; + type Xcm = pallet_xcm::Pallet; +} diff --git a/pallets/contracts/mock-network/src/primitives.rs b/pallets/contracts/mock-network/src/primitives.rs new file mode 100644 index 00000000..efc42772 --- /dev/null +++ b/pallets/contracts/mock-network/src/primitives.rs @@ -0,0 +1,23 @@ +// 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 . + +pub type Balance = u128; + +pub const UNITS: Balance = 10_000_000_000; +pub const CENTS: Balance = UNITS / 100; // 100_000_000 + +pub type AccountId = sp_runtime::AccountId32; +pub type AssetIdForAssets = u128; diff --git a/pallets/contracts/mock-network/src/relay_chain.rs b/pallets/contracts/mock-network/src/relay_chain.rs new file mode 100644 index 00000000..8829fff3 --- /dev/null +++ b/pallets/contracts/mock-network/src/relay_chain.rs @@ -0,0 +1,239 @@ +// Copyright 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 . + +//! Relay chain runtime mock. + +use frame_support::{ + construct_runtime, derive_impl, parameter_types, + traits::{Contains, Everything, Nothing}, + weights::Weight, +}; + +use frame_system::EnsureRoot; +use sp_core::{ConstU32, H256}; +use sp_runtime::traits::IdentityLookup; + +use polkadot_parachain_primitives::primitives::Id as ParaId; +use polkadot_runtime_parachains::{configuration, origin, shared}; +use xcm::latest::prelude::*; +use xcm_builder::{ + AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowSubscriptionsFrom, + AllowTopLevelPaidExecutionFrom, ChildParachainAsNative, ChildParachainConvertsVia, + ChildSystemParachainAsSuperuser, DescribeAllTerminal, DescribeFamily, FixedRateOfFungible, + FixedWeightBounds, FrameTransactionalProcessor, FungibleAdapter, HashedDescription, IsConcrete, + SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, WithComputedOrigin, +}; +use xcm_executor::{Config, XcmExecutor}; + +use super::{ + mocks::relay_message_queue::*, + primitives::{AccountId, Balance}, +}; + +#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] +impl frame_system::Config for Runtime { + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type Block = Block; + type Nonce = u64; + type Hash = H256; + type Hashing = ::sp_runtime::traits::BlakeTwo256; + type AccountId = AccountId; + type Lookup = IdentityLookup; + type RuntimeEvent = RuntimeEvent; + type BlockWeights = (); + type BlockLength = (); + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = pallet_balances::AccountData; + type OnNewAccount = (); + type OnKilledAccount = (); + type DbWeight = (); + type BaseCallFilter = Everything; + type SystemWeightInfo = (); + type SS58Prefix = (); + type OnSetCode = (); + type MaxConsumers = ConstU32<16>; +} + +parameter_types! { + pub ExistentialDeposit: Balance = 1; + pub const MaxLocks: u32 = 50; + pub const MaxReserves: u32 = 50; +} + +impl pallet_balances::Config for Runtime { + type MaxLocks = MaxLocks; + type Balance = Balance; + type RuntimeEvent = RuntimeEvent; + type DustRemoval = (); + type ExistentialDeposit = ExistentialDeposit; + type AccountStore = System; + type WeightInfo = (); + type MaxReserves = MaxReserves; + type ReserveIdentifier = [u8; 8]; + type FreezeIdentifier = (); + type MaxFreezes = ConstU32<0>; + type RuntimeHoldReason = RuntimeHoldReason; + type RuntimeFreezeReason = RuntimeFreezeReason; +} + +impl shared::Config for Runtime { + type DisabledValidators = (); +} + +impl configuration::Config for Runtime { + type WeightInfo = configuration::TestWeightInfo; +} + +parameter_types! { + pub RelayNetwork: NetworkId = ByGenesis([0; 32]); + pub const TokenLocation: Location = Here.into_location(); + pub UniversalLocation: InteriorLocation = RelayNetwork::get().into(); + pub UnitWeightCost: u64 = 1_000; +} + +pub type SovereignAccountOf = ( + HashedDescription>, + AccountId32Aliases, + ChildParachainConvertsVia, +); + +pub type LocalBalancesTransactor = + FungibleAdapter, SovereignAccountOf, AccountId, ()>; + +pub type AssetTransactors = LocalBalancesTransactor; + +type LocalOriginConverter = ( + SovereignSignedViaLocation, + ChildParachainAsNative, + SignedAccountId32AsNative, + ChildSystemParachainAsSuperuser, +); + +parameter_types! { + pub const XcmInstructionWeight: Weight = Weight::from_parts(1_000, 1_000); + pub TokensPerSecondPerMegabyte: (AssetId, u128, u128) = + (AssetId(TokenLocation::get()), 1_000_000_000_000, 1024 * 1024); + pub const MaxInstructions: u32 = 100; + pub const MaxAssetsIntoHolding: u32 = 64; +} + +pub struct ChildrenParachains; +impl Contains for ChildrenParachains { + fn contains(location: &Location) -> bool { + matches!(location.unpack(), (0, [Parachain(_)])) + } +} + +pub type XcmRouter = crate::RelayChainXcmRouter; +pub type Barrier = WithComputedOrigin< + ( + AllowExplicitUnpaidExecutionFrom, + AllowTopLevelPaidExecutionFrom, + AllowSubscriptionsFrom, + ), + UniversalLocation, + ConstU32<1>, +>; + +pub struct XcmConfig; +impl Config for XcmConfig { + type RuntimeCall = RuntimeCall; + type XcmSender = XcmRouter; + type AssetTransactor = AssetTransactors; + type OriginConverter = LocalOriginConverter; + type IsReserve = (); + type IsTeleporter = (); + type UniversalLocation = UniversalLocation; + type Barrier = Barrier; + type Weigher = FixedWeightBounds; + type Trader = FixedRateOfFungible; + type ResponseHandler = XcmPallet; + type AssetTrap = XcmPallet; + type AssetLocker = XcmPallet; + type AssetExchanger = (); + type AssetClaims = XcmPallet; + type SubscriptionService = XcmPallet; + type PalletInstancesInfo = AllPalletsWithSystem; + type FeeManager = (); + type MaxAssetsIntoHolding = MaxAssetsIntoHolding; + type MessageExporter = (); + type UniversalAliases = Nothing; + type CallDispatcher = RuntimeCall; + type SafeCallFilter = Everything; + type Aliasers = Nothing; + type TransactionalProcessor = FrameTransactionalProcessor; + type HrmpNewChannelOpenRequestHandler = (); + type HrmpChannelAcceptedHandler = (); + type HrmpChannelClosingHandler = (); + type XcmRecorder = XcmPallet; +} + +pub type LocalOriginToLocation = SignedToAccountId32; + +impl pallet_xcm::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type SendXcmOrigin = xcm_builder::EnsureXcmOrigin; + type XcmRouter = XcmRouter; + type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin; + type XcmExecuteFilter = Everything; + type XcmExecutor = XcmExecutor; + type XcmTeleportFilter = Everything; + type XcmReserveTransferFilter = Everything; + type Weigher = FixedWeightBounds; + type UniversalLocation = UniversalLocation; + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100; + type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion; + type Currency = Balances; + type CurrencyMatcher = IsConcrete; + type TrustedLockers = (); + type SovereignAccountOf = SovereignAccountOf; + type MaxLockers = ConstU32<8>; + type MaxRemoteLockConsumers = ConstU32<0>; + type RemoteLockConsumerIdentifier = (); + type WeightInfo = pallet_xcm::TestWeightInfo; + type AdminOrigin = EnsureRoot; +} + +impl origin::Config for Runtime {} + +type Block = frame_system::mocking::MockBlock; + +impl pallet_message_queue::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type Size = u32; + type HeapSize = MessageQueueHeapSize; + type MaxStale = MessageQueueMaxStale; + type ServiceWeight = MessageQueueServiceWeight; + type IdleMaxServiceWeight = (); + type MessageProcessor = MessageProcessor; + type QueueChangeHandler = (); + type WeightInfo = (); + type QueuePausedQuery = (); +} + +construct_runtime!( + pub enum Runtime { + System: frame_system, + Balances: pallet_balances, + ParasOrigin: origin, + XcmPallet: pallet_xcm, + MessageQueue: pallet_message_queue, + } +); diff --git a/pallets/contracts/mock-network/src/tests.rs b/pallets/contracts/mock-network/src/tests.rs new file mode 100644 index 00000000..48a94e17 --- /dev/null +++ b/pallets/contracts/mock-network/src/tests.rs @@ -0,0 +1,200 @@ +// This file is part of Substrate. + +// 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::{ + parachain::{self, Runtime}, + parachain_account_sovereign_account_id, + primitives::{AccountId, CENTS}, + relay_chain, MockNet, ParaA, ParachainBalances, Relay, ALICE, BOB, INITIAL_BALANCE, +}; +use codec::{Decode, Encode}; +use frame_support::traits::{fungibles::Mutate, Currency}; +use pallet_contracts::{test_utils::builder::*, Code}; +use pallet_contracts_fixtures::compile_module; +use pallet_contracts_uapi::ReturnErrorCode; +use xcm::{v4::prelude::*, VersionedLocation, VersionedXcm}; +use xcm_simulator::TestExt; + +macro_rules! assert_return_code { + ( $x:expr , $y:expr $(,)? ) => {{ + assert_eq!(u32::from_le_bytes($x.data[..].try_into().unwrap()), $y as u32); + }}; +} + +fn bare_call(dest: sp_runtime::AccountId32) -> BareCallBuilder { + BareCallBuilder::::bare_call(ALICE, dest) +} + +/// Instantiate the tests contract, and fund it with some balance and assets. +fn instantiate_test_contract(name: &str) -> AccountId { + let (wasm, _) = compile_module::(name).unwrap(); + + // Instantiate contract. + let contract_addr = ParaA::execute_with(|| { + BareInstantiateBuilder::::bare_instantiate(ALICE, Code::Upload(wasm)) + .build_and_unwrap_account_id() + }); + + // Funds contract account with some balance and assets. + ParaA::execute_with(|| { + parachain::Balances::make_free_balance_be(&contract_addr, INITIAL_BALANCE); + parachain::Assets::mint_into(0u32.into(), &contract_addr, INITIAL_BALANCE).unwrap(); + }); + Relay::execute_with(|| { + let sovereign_account = parachain_account_sovereign_account_id(1u32, contract_addr.clone()); + relay_chain::Balances::make_free_balance_be(&sovereign_account, INITIAL_BALANCE); + }); + + contract_addr +} + +#[test] +fn test_xcm_execute() { + MockNet::reset(); + + let contract_addr = instantiate_test_contract("xcm_execute"); + + // Execute XCM instructions through the contract. + ParaA::execute_with(|| { + let amount: u128 = 10 * CENTS; + let assets: Asset = (Here, amount).into(); + let beneficiary = AccountId32 { network: None, id: BOB.clone().into() }; + + // The XCM used to transfer funds to Bob. + let message: Xcm<()> = Xcm::builder_unsafe() + .withdraw_asset(assets.clone()) + .deposit_asset(assets, beneficiary) + .build(); + + let result = bare_call(contract_addr.clone()) + .data(VersionedXcm::V4(message).encode()) + .build(); + + assert_eq!(result.gas_consumed, result.gas_required); + assert_return_code!(&result.result.unwrap(), ReturnErrorCode::Success); + + // Check if the funds are subtracted from the account of Alice and added to the account of + // Bob. + let initial = INITIAL_BALANCE; + assert_eq!(ParachainBalances::free_balance(BOB), initial + amount); + assert_eq!(ParachainBalances::free_balance(&contract_addr), initial - amount); + }); +} + +#[test] +fn test_xcm_execute_incomplete() { + MockNet::reset(); + + let contract_addr = instantiate_test_contract("xcm_execute"); + let amount = 10 * CENTS; + + // Execute XCM instructions through the contract. + ParaA::execute_with(|| { + let assets: Asset = (Here, amount).into(); + let beneficiary = AccountId32 { network: None, id: BOB.clone().into() }; + + // The XCM used to transfer funds to Bob. + let message: Xcm<()> = Xcm::builder_unsafe() + .withdraw_asset(assets.clone()) + // This will fail as the contract does not have enough balance to complete both + // withdrawals. + .withdraw_asset((Here, INITIAL_BALANCE)) + .buy_execution(assets.clone(), Unlimited) + .deposit_asset(assets, beneficiary) + .build(); + + let result = bare_call(contract_addr.clone()) + .data(VersionedXcm::V4(message).encode()) + .build(); + + assert_eq!(result.gas_consumed, result.gas_required); + assert_return_code!(&result.result.unwrap(), ReturnErrorCode::XcmExecutionFailed); + + assert_eq!(ParachainBalances::free_balance(BOB), INITIAL_BALANCE); + assert_eq!(ParachainBalances::free_balance(&contract_addr), INITIAL_BALANCE - amount); + }); +} + +#[test] +fn test_xcm_execute_reentrant_call() { + MockNet::reset(); + + let contract_addr = instantiate_test_contract("xcm_execute"); + + ParaA::execute_with(|| { + let transact_call = parachain::RuntimeCall::Contracts(pallet_contracts::Call::call { + dest: contract_addr.clone(), + gas_limit: 1_000_000.into(), + storage_deposit_limit: None, + data: vec![], + value: 0u128, + }); + + // The XCM used to transfer funds to Bob. + let message: Xcm = Xcm::builder_unsafe() + .transact(OriginKind::Native, 1_000_000_000, transact_call.encode()) + .expect_transact_status(MaybeErrorCode::Success) + .build(); + + let result = bare_call(contract_addr.clone()) + .data(VersionedXcm::V4(message).encode()) + .build_and_unwrap_result(); + + assert_return_code!(&result, ReturnErrorCode::XcmExecutionFailed); + + // Funds should not change hands as the XCM transact failed. + assert_eq!(ParachainBalances::free_balance(BOB), INITIAL_BALANCE); + }); +} + +#[test] +fn test_xcm_send() { + MockNet::reset(); + let contract_addr = instantiate_test_contract("xcm_send"); + let amount = 1_000 * CENTS; + let fee = parachain::estimate_message_fee(4); // Accounts for the `DescendOrigin` instruction added by `send_xcm` + + // Send XCM instructions through the contract, to transfer some funds from the contract + // derivative account to Alice on the relay chain. + ParaA::execute_with(|| { + let dest = VersionedLocation::V4(Parent.into()); + let assets: Asset = (Here, amount).into(); + let beneficiary = AccountId32 { network: None, id: ALICE.clone().into() }; + + let message: Xcm<()> = Xcm::builder() + .withdraw_asset(assets.clone()) + .buy_execution((Here, fee), Unlimited) + .deposit_asset(assets, beneficiary) + .build(); + + let result = bare_call(contract_addr.clone()) + .data((dest, VersionedXcm::V4(message)).encode()) + .build_and_unwrap_result(); + + let mut data = &result.data[..]; + XcmHash::decode(&mut data).expect("Failed to decode xcm_send message_id"); + }); + + Relay::execute_with(|| { + let derived_contract_addr = ¶chain_account_sovereign_account_id(1, contract_addr); + assert_eq!( + INITIAL_BALANCE - amount, + relay_chain::Balances::free_balance(derived_contract_addr) + ); + assert_eq!(INITIAL_BALANCE + amount - fee, relay_chain::Balances::free_balance(ALICE)); + }); +} diff --git a/pallets/contracts/proc-macro/Cargo.toml b/pallets/contracts/proc-macro/Cargo.toml new file mode 100644 index 00000000..a97e6e73 --- /dev/null +++ b/pallets/contracts/proc-macro/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "pallet-contracts-proc-macro" +version = "18.0.0" +authors.workspace = true +edition.workspace = true +license = "Apache-2.0" +homepage.workspace = true +repository.workspace = true +description = "Procedural macros used in pallet_contracts" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[lib] +proc-macro = true + +[dependencies] +proc-macro2 = { workspace = true } +quote = { workspace = true } +syn = { features = ["full"], workspace = true } diff --git a/pallets/contracts/proc-macro/src/lib.rs b/pallets/contracts/proc-macro/src/lib.rs new file mode 100644 index 00000000..84ea7de0 --- /dev/null +++ b/pallets/contracts/proc-macro/src/lib.rs @@ -0,0 +1,923 @@ +// This file is part of Substrate. + +// 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. + +//! Procedural macros used in the contracts module. +//! +//! Most likely you should use the [`#[define_env]`][`macro@define_env`] attribute macro which hides +//! boilerplate of defining external environment for a wasm module. + +use core::cmp::Reverse; +use proc_macro::TokenStream; +use proc_macro2::{Span, TokenStream as TokenStream2}; +use quote::{quote, quote_spanned, ToTokens}; +use syn::{ + parse_macro_input, punctuated::Punctuated, spanned::Spanned, token::Comma, Data, DeriveInput, + Fields, FnArg, Ident, +}; + +/// This derives `Debug` for a struct where each field must be of some numeric type. +/// It interprets each field as its represents some weight and formats it as times so that +/// it is readable by humans. +#[proc_macro_derive(WeightDebug)] +pub fn derive_weight_debug(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeriveInput); + let name = &input.ident; + let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); + let data = if let Data::Struct(data) = &input.data { + data + } else { + return quote_spanned! { + name.span() => + compile_error!("WeightDebug is only supported for structs."); + } + .into() + }; + + let fields = match &data.fields { + Fields::Named(fields) => { + let recurse = fields.named.iter().filter_map(|f| { + let name = f.ident.as_ref()?; + if name.to_string().starts_with('_') { + return None + } + let ret = quote_spanned! { f.span() => + formatter.field(stringify!(#name), &HumanWeight(self.#name)); + }; + Some(ret) + }); + quote! { + #( #recurse )* + } + }, + Fields::Unnamed(fields) => quote_spanned! { + fields.span() => + compile_error!("Unnamed fields are not supported") + }, + Fields::Unit => quote!(), + }; + + let tokens = quote! { + impl #impl_generics ::core::fmt::Debug for #name #ty_generics #where_clause { + fn fmt(&self, formatter: &mut ::core::fmt::Formatter<'_>) -> core::fmt::Result { + use ::sp_runtime::{FixedPointNumber, FixedU128 as Fixed}; + use ::core::{fmt, write}; + + struct HumanWeight(Weight); + + impl fmt::Debug for HumanWeight { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.0.ref_time() > 1_000_000_000 { + write!( + formatter, + "{} ms, {} bytes", + Fixed::saturating_from_rational(self.0.ref_time(), 1_000_000_000).into_inner() / Fixed::accuracy(), + self.0.proof_size() + ) + } else if self.0.ref_time() > 1_000_000 { + write!( + formatter, + "{} µs, {} bytes", + Fixed::saturating_from_rational(self.0.ref_time(), 1_000_000).into_inner() / Fixed::accuracy(), + self.0.proof_size() + ) + } else if self.0.ref_time() > 1_000 { + write!( + formatter, + "{} ns, {} bytes", + Fixed::saturating_from_rational(self.0.ref_time(), 1_000).into_inner() / Fixed::accuracy(), + self.0.proof_size() + ) + } else { + write!(formatter, "{} ps, {} bytes", self.0.ref_time(), self.0.proof_size()) + } + } + } + + let mut formatter = formatter.debug_struct(stringify!(#name)); + #fields + formatter.finish() + } + } + }; + + tokens.into() +} + +/// Parsed environment definition. +struct EnvDef { + host_funcs: Vec, +} + +/// Parsed host function definition. +struct HostFn { + item: syn::ItemFn, + version: u8, + name: String, + returns: HostFnReturn, + is_stable: bool, + alias_to: Option, + /// Formulating the predicate inverted makes the expression using it simpler. + not_deprecated: bool, + cfg: Option, +} + +enum HostFnReturn { + Unit, + U32, + U64, + ReturnCode, +} + +impl HostFnReturn { + fn to_wasm_sig(&self) -> TokenStream2 { + let ok = match self { + Self::Unit => quote! { () }, + Self::U32 | Self::ReturnCode => quote! { ::core::primitive::u32 }, + Self::U64 => quote! { ::core::primitive::u64 }, + }; + quote! { + ::core::result::Result<#ok, ::wasmi::Error> + } + } +} + +impl ToTokens for HostFn { + fn to_tokens(&self, tokens: &mut TokenStream2) { + self.item.to_tokens(tokens); + } +} + +impl HostFn { + pub fn try_from(mut item: syn::ItemFn) -> syn::Result { + let err = |span, msg| { + let msg = format!("Invalid host function definition.\n{}", msg); + syn::Error::new(span, msg) + }; + + // process attributes + let msg = + "Only #[version()], #[unstable], #[prefixed_alias], #[cfg], #[mutating] and #[deprecated] attributes are allowed."; + let span = item.span(); + let mut attrs = item.attrs.clone(); + attrs.retain(|a| !a.path().is_ident("doc")); + let mut maybe_version = None; + let mut is_stable = true; + let mut alias_to = None; + let mut not_deprecated = true; + let mut mutating = false; + let mut cfg = None; + while let Some(attr) = attrs.pop() { + let ident = attr.path().get_ident().ok_or(err(span, msg))?.to_string(); + match ident.as_str() { + "version" => { + if maybe_version.is_some() { + return Err(err(span, "#[version] can only be specified once")) + } + maybe_version = + Some(attr.parse_args::().and_then(|lit| lit.base10_parse())?); + }, + "unstable" => { + if !is_stable { + return Err(err(span, "#[unstable] can only be specified once")) + } + is_stable = false; + }, + "prefixed_alias" => { + alias_to = Some(item.sig.ident.to_string()); + item.sig.ident = syn::Ident::new( + &format!("seal_{}", &item.sig.ident.to_string()), + item.sig.ident.span(), + ); + }, + "deprecated" => { + if !not_deprecated { + return Err(err(span, "#[deprecated] can only be specified once")) + } + not_deprecated = false; + }, + "mutating" => { + if mutating { + return Err(err(span, "#[mutating] can only be specified once")) + } + mutating = true; + }, + "cfg" => { + if cfg.is_some() { + return Err(err(span, "#[cfg] can only be specified once")) + } + cfg = Some(attr); + }, + id => return Err(err(span, &format!("Unsupported attribute \"{id}\". {msg}"))), + } + } + + if mutating { + let stmt = syn::parse_quote! { + if ctx.ext().is_read_only() { + return Err(Error::::StateChangeDenied.into()); + } + }; + item.block.stmts.insert(0, stmt); + } + + let name = item.sig.ident.to_string(); + + if !(is_stable || not_deprecated) { + return Err(err(span, "#[deprecated] is mutually exclusive with #[unstable]")) + } + + // process arguments: The first and second args are treated differently (ctx, memory) + // they must exist and be `ctx: _` and `memory: _`. + let msg = "Every function must start with two inferred parameters: ctx: _ and memory: _"; + let special_args = item + .sig + .inputs + .iter() + .take(2) + .enumerate() + .map(|(i, arg)| is_valid_special_arg(i, arg)) + .fold(0u32, |acc, valid| if valid { acc + 1 } else { acc }); + + if special_args != 2 { + return Err(err(span, msg)) + } + + // process return type + let msg = r#"Should return one of the following: + - Result<(), TrapReason>, + - Result, + - Result, + - Result"#; + let ret_ty = match item.clone().sig.output { + syn::ReturnType::Type(_, ty) => Ok(ty.clone()), + _ => Err(err(span, &msg)), + }?; + match *ret_ty { + syn::Type::Path(tp) => { + let result = &tp.path.segments.last().ok_or(err(span, &msg))?; + let (id, span) = (result.ident.to_string(), result.ident.span()); + id.eq(&"Result".to_string()).then_some(()).ok_or(err(span, &msg))?; + + match &result.arguments { + syn::PathArguments::AngleBracketed(group) => { + if group.args.len() != 2 { + return Err(err(span, &msg)) + }; + + let arg2 = group.args.last().ok_or(err(span, &msg))?; + + let err_ty = match arg2 { + syn::GenericArgument::Type(ty) => Ok(ty.clone()), + _ => Err(err(arg2.span(), &msg)), + }?; + + match err_ty { + syn::Type::Path(tp) => Ok(tp + .path + .segments + .first() + .ok_or(err(arg2.span(), &msg))? + .ident + .to_string()), + _ => Err(err(tp.span(), &msg)), + }? + .eq("TrapReason") + .then_some(()) + .ok_or(err(span, &msg))?; + + let arg1 = group.args.first().ok_or(err(span, &msg))?; + let ok_ty = match arg1 { + syn::GenericArgument::Type(ty) => Ok(ty.clone()), + _ => Err(err(arg1.span(), &msg)), + }?; + let ok_ty_str = match ok_ty { + syn::Type::Path(tp) => Ok(tp + .path + .segments + .first() + .ok_or(err(arg1.span(), &msg))? + .ident + .to_string()), + syn::Type::Tuple(tt) => { + if !tt.elems.is_empty() { + return Err(err(arg1.span(), &msg)) + }; + Ok("()".to_string()) + }, + _ => Err(err(ok_ty.span(), &msg)), + }?; + let returns = match ok_ty_str.as_str() { + "()" => Ok(HostFnReturn::Unit), + "u32" => Ok(HostFnReturn::U32), + "u64" => Ok(HostFnReturn::U64), + "ReturnErrorCode" => Ok(HostFnReturn::ReturnCode), + _ => Err(err(arg1.span(), &msg)), + }?; + + Ok(Self { + item, + version: maybe_version.unwrap_or_default(), + name, + returns, + is_stable, + alias_to, + not_deprecated, + cfg, + }) + }, + _ => Err(err(span, &msg)), + } + }, + _ => Err(err(span, &msg)), + } + } + + fn module(&self) -> String { + format!("seal{}", self.version) + } +} + +impl EnvDef { + pub fn try_from(item: syn::ItemMod) -> syn::Result { + let span = item.span(); + let err = |msg| syn::Error::new(span, msg); + let items = &item + .content + .as_ref() + .ok_or(err("Invalid environment definition, expected `mod` to be inlined."))? + .1; + + let extract_fn = |i: &syn::Item| match i { + syn::Item::Fn(i_fn) => Some(i_fn.clone()), + _ => None, + }; + + let selector = |a: &syn::Attribute| a.path().is_ident("prefixed_alias"); + + let aliases = items + .iter() + .filter_map(extract_fn) + .filter(|i| i.attrs.iter().any(selector)) + .map(|i| HostFn::try_from(i)); + + let host_funcs = items + .iter() + .filter_map(extract_fn) + .map(|mut i| { + i.attrs.retain(|i| !selector(i)); + i + }) + .map(|i| HostFn::try_from(i)) + .chain(aliases) + .collect::, _>>()?; + + Ok(Self { host_funcs }) + } +} + +fn is_valid_special_arg(idx: usize, arg: &FnArg) -> bool { + let FnArg::Typed(pat) = arg else { return false }; + let ident = if let syn::Pat::Ident(ref ident) = *pat.pat { &ident.ident } else { return false }; + let name_ok = match idx { + 0 => ident == "ctx" || ident == "_ctx", + 1 => ident == "memory" || ident == "_memory", + _ => false, + }; + if !name_ok { + return false + } + matches!(*pat.ty, syn::Type::Infer(_)) +} + +fn expand_func_doc(func: &HostFn) -> TokenStream2 { + // Remove auxiliary args: `ctx: _` and `memory: _` + let func_decl = { + let mut sig = func.item.sig.clone(); + sig.inputs = sig + .inputs + .iter() + .skip(2) + .map(|p| p.clone()) + .collect::>(); + sig.to_token_stream() + }; + let func_doc = { + let func_docs = if let Some(origin_fn) = &func.alias_to { + let alias_doc = format!( + "This is just an alias function to [`{0}()`][`Self::{0}`] with backwards-compatible prefixed identifier.", + origin_fn, + ); + quote! { #[doc = #alias_doc] } + } else { + let docs = func.item.attrs.iter().filter(|a| a.path().is_ident("doc")).map(|d| { + let docs = d.to_token_stream(); + quote! { #docs } + }); + quote! { #( #docs )* } + }; + let deprecation_notice = if !func.not_deprecated { + let warning = "\n # Deprecated\n\n \ + This function is deprecated and will be removed in future versions.\n \ + No new code or contracts with this API can be deployed."; + quote! { #[doc = #warning] } + } else { + quote! {} + }; + let import_notice = { + let info = format!( + "\n# Wasm Import Statement\n```wat\n(import \"seal{}\" \"{}\" (func ...))\n```", + func.version, func.name, + ); + quote! { #[doc = #info] } + }; + let unstable_notice = if !func.is_stable { + let warning = "\n # Unstable\n\n \ + This function is unstable and it is a subject to change (or removal) in the future.\n \ + Do not deploy a contract using it to a production chain."; + quote! { #[doc = #warning] } + } else { + quote! {} + }; + quote! { + #deprecation_notice + #func_docs + #import_notice + #unstable_notice + } + }; + quote! { + #func_doc + #func_decl; + } +} + +/// Expands documentation for host functions. +fn expand_docs(def: &EnvDef) -> TokenStream2 { + // Create the `Current` trait with only the newest versions + // we sort so that only the newest versions make it into `docs` + let mut current_docs = std::collections::HashMap::new(); + let mut funcs: Vec<_> = def.host_funcs.iter().filter(|f| f.alias_to.is_none()).collect(); + funcs.sort_unstable_by_key(|func| Reverse(func.version)); + for func in funcs { + if current_docs.contains_key(&func.name) { + continue + } + current_docs.insert(func.name.clone(), expand_func_doc(&func)); + } + let current_docs = current_docs.values(); + + // Create the `legacy` module with all functions + // Maps from version to list of functions that have this version + let mut legacy_doc = std::collections::BTreeMap::>::new(); + for func in def.host_funcs.iter() { + legacy_doc.entry(func.version).or_default().push(expand_func_doc(&func)); + } + let legacy_doc = legacy_doc.into_iter().map(|(version, funcs)| { + let doc = format!("All functions available in the **seal{}** module", version); + let version = Ident::new(&format!("Version{version}"), Span::call_site()); + quote! { + #[doc = #doc] + pub trait #version { + #( #funcs )* + } + } + }); + + quote! { + /// Contains only the latest version of each function. + /// + /// In reality there are more functions available but they are all obsolete: When a function + /// is updated a new **version** is added and the old versions stays available as-is. + /// We only list the newest version here. Some functions are available under additional + /// names (aliases) for historic reasons which are omitted here. + /// + /// If you want an overview of all the functions available to a contact all you need + /// to look at is this trait. It contains only the latest version of each + /// function and no aliases. If you are writing a contract(language) from scratch + /// this is where you should look at. + pub trait Current { + #( #current_docs )* + } + #( #legacy_doc )* + } +} + +/// Expands environment definition. +/// Should generate source code for: +/// - implementations of the host functions to be added to the wasm runtime environment (see +/// `expand_impls()`). +fn expand_env(def: &EnvDef, docs: bool) -> TokenStream2 { + let impls = expand_impls(def); + let docs = docs.then_some(expand_docs(def)).unwrap_or(TokenStream2::new()); + let stable_api_count = def.host_funcs.iter().filter(|f| f.is_stable).count(); + + quote! { + pub struct Env; + + #[cfg(test)] + pub const STABLE_API_COUNT: usize = #stable_api_count; + + #impls + /// Documentation of the API (host functions) available to contracts. + /// + /// The `Current` trait might be the most useful doc to look at. The versioned + /// traits only exist for reference: If trying to find out if a specific version of + /// `pallet-contracts` contains a certain function. + /// + /// # Note + /// + /// This module is not meant to be used by any code. Rather, it is meant to be + /// consumed by humans through rustdoc. + #[cfg(doc)] + pub mod api_doc { + use super::{TrapReason, ReturnErrorCode}; + #docs + } + } +} + +/// Generates for every host function: +/// - real implementation, to register it in the contract execution environment; +/// - dummy implementation, to be used as mocks for contract validation step. +fn expand_impls(def: &EnvDef) -> TokenStream2 { + let impls = expand_functions(def, ExpandMode::Impl); + let dummy_impls = expand_functions(def, ExpandMode::MockImpl); + let bench_impls = expand_functions(def, ExpandMode::BenchImpl); + + quote! { + impl<'a, E: Ext> crate::wasm::Environment> for Env + { + fn define( + store: &mut ::wasmi::Store>, + linker: &mut ::wasmi::Linker>, + allow_unstable: AllowUnstableInterface, + allow_deprecated: AllowDeprecatedInterface, + ) -> Result<(),::wasmi::errors::LinkerError> { + #impls + Ok(()) + } + } + + #[cfg(feature = "runtime-benchmarks")] + pub struct BenchEnv(::core::marker::PhantomData); + + #[cfg(feature = "runtime-benchmarks")] + impl BenchEnv { + #bench_impls + } + + impl crate::wasm::Environment<()> for Env + { + fn define( + store: &mut ::wasmi::Store<()>, + linker: &mut ::wasmi::Linker<()>, + allow_unstable: AllowUnstableInterface, + allow_deprecated: AllowDeprecatedInterface, + ) -> Result<(), ::wasmi::errors::LinkerError> { + #dummy_impls + Ok(()) + } + } + } +} + +enum ExpandMode { + Impl, + BenchImpl, + MockImpl, +} + +impl ExpandMode { + fn expand_blocks(&self) -> bool { + match *self { + ExpandMode::Impl | ExpandMode::BenchImpl => true, + ExpandMode::MockImpl => false, + } + } + + fn host_state(&self) -> TokenStream2 { + match *self { + ExpandMode::Impl | ExpandMode::BenchImpl => quote! { crate::wasm::runtime::Runtime }, + ExpandMode::MockImpl => quote! { () }, + } + } +} + +fn expand_functions(def: &EnvDef, expand_mode: ExpandMode) -> TokenStream2 { + let impls = def.host_funcs.iter().map(|f| { + // skip the context and memory argument + let params = f.item.sig.inputs.iter().skip(2); + let module = f.module(); + let cfg = &f.cfg; + let name = &f.name; + let body = &f.item.block; + let wasm_output = f.returns.to_wasm_sig(); + let output = &f.item.sig.output; + let is_stable = f.is_stable; + let not_deprecated = f.not_deprecated; + + // wrapped host function body call with host function traces + // see https://github.com/paritytech/polkadot-sdk/tree/master/substrate/frame/contracts#host-function-tracing + let wrapped_body_with_trace = { + let trace_fmt_args = params.clone().filter_map(|arg| match arg { + syn::FnArg::Receiver(_) => None, + syn::FnArg::Typed(p) => { + match *p.pat.clone() { + syn::Pat::Ident(ref pat_ident) => Some(pat_ident.ident.clone()), + _ => None, + } + }, + }); + + let params_fmt_str = trace_fmt_args.clone().map(|s| format!("{s}: {{:?}}")).collect::>().join(", "); + let trace_fmt_str = format!("{}::{}({}) = {{:?}}\n", module, name, params_fmt_str); + + quote! { + let result = #body; + if ::log::log_enabled!(target: "runtime::contracts::strace", ::log::Level::Trace) { + use core::fmt::Write; + let mut w = sp_std::Writer::default(); + let _ = core::write!(&mut w, #trace_fmt_str, #( #trace_fmt_args, )* result); + let msg = core::str::from_utf8(&w.inner()).unwrap_or_default(); + ctx.ext().append_debug_buffer(msg); + } + result + } + }; + + // If we don't expand blocks (implementing for `()`) we change a few things: + // - We replace any code by unreachable! + // - Allow unused variables as the code that uses is not expanded + // - We don't need to map the error as we simply panic if they code would ever be executed + let expand_blocks = expand_mode.expand_blocks(); + let inner = match expand_mode { + ExpandMode::Impl => { + quote! { || #output { + let (memory, ctx) = __caller__ + .data() + .memory() + .expect("Memory must be set when setting up host data; qed") + .data_and_store_mut(&mut __caller__); + #wrapped_body_with_trace + } } + }, + ExpandMode::BenchImpl => { + let body = &body.stmts; + quote!{ + #(#body)* + } + }, + ExpandMode::MockImpl => { + quote! { || -> #wasm_output { + // This is part of the implementation for `Environment<()>` which is not + // meant to be actually executed. It is only for validation which will + // never call host functions. + ::core::unreachable!() + } } + }, + }; + + let into_host = if expand_blocks { + quote! { + |reason| { + ::wasmi::Error::host(reason) + } + } + } else { + quote! { + |reason| { reason } + } + }; + let allow_unused = if expand_blocks { + quote! { } + } else { + quote! { #[allow(unused_variables)] } + }; + let sync_gas_before = if expand_blocks { + quote! { + // Write gas from wasmi into pallet-contracts before entering the host function. + let __gas_left_before__ = { + let fuel = + __caller__.get_fuel().expect("Fuel metering is enabled; qed"); + __caller__ + .data_mut() + .ext() + .gas_meter_mut() + .sync_from_executor(fuel) + .map_err(TrapReason::from) + .map_err(#into_host)? + }; + + // Charge gas for host function execution. + __caller__.data_mut().charge_gas(crate::wasm::RuntimeCosts::HostFn) + .map_err(TrapReason::from) + .map_err(#into_host)?; + } + } else { + quote! { } + }; + // Write gas from pallet-contracts into wasmi after leaving the host function. + let sync_gas_after = if expand_blocks { + quote! { + let fuel = __caller__ + .data_mut() + .ext() + .gas_meter_mut() + .sync_to_executor(__gas_left_before__) + .map_err(|err| { + let err = TrapReason::from(err); + wasmi::Error::host(err) + })?; + __caller__ + .set_fuel(fuel.into()) + .expect("Fuel metering is enabled; qed"); + } + } else { + quote! { } + }; + + match expand_mode { + ExpandMode::BenchImpl => { + let name = Ident::new(&format!("{module}_{name}"), Span::call_site()); + quote! { + pub fn #name(ctx: &mut crate::wasm::Runtime, memory: &mut [u8], #(#params),*) #output { + #inner + } + } + }, + _ => { + let host_state = expand_mode.host_state(); + quote! { + // We need to allow all interfaces when runtime benchmarks are performed because + // we generate the weights even when those interfaces are not enabled. This + // is necessary as the decision whether we allow unstable or deprecated functions + // is a decision made at runtime. Generation of the weights happens statically. + #cfg + if ::core::cfg!(feature = "runtime-benchmarks") || + ((#is_stable || __allow_unstable__) && (#not_deprecated || __allow_deprecated__)) + { + #allow_unused + linker.define(#module, #name, ::wasmi::Func::wrap(&mut*store, |mut __caller__: ::wasmi::Caller<#host_state>, #( #params, )*| -> #wasm_output { + #sync_gas_before + let mut func = #inner; + let result = func().map_err(#into_host).map(::core::convert::Into::into); + #sync_gas_after + result + }))?; + } + } + }, + } + }); + + match expand_mode { + ExpandMode::BenchImpl => { + quote! { + #( #impls )* + } + }, + _ => quote! { + let __allow_unstable__ = matches!(allow_unstable, AllowUnstableInterface::Yes); + let __allow_deprecated__ = matches!(allow_deprecated, AllowDeprecatedInterface::Yes); + #( #impls )* + }, + } +} + +/// Defines a host functions set that can be imported by contract wasm code. +/// +/// **NB**: Be advised that all functions defined by this macro +/// will panic if called with unexpected arguments. +/// +/// It's up to you as the user of this macro to check signatures of wasm code to be executed +/// and reject the code if any imported function has a mismatched signature. +/// +/// ## Example +/// +/// ```nocompile +/// #[define_env] +/// pub mod some_env { +/// fn foo(ctx: _, memory: _, key_ptr: u32, value_ptr: u32, value_len: u32) -> Result<(), TrapReason> { +/// ctx.some_host_fn(KeyType::Fix, key_ptr, value_ptr, value_len).map(|_| ()) +/// } +/// } +/// ``` +/// This example will expand to the `foo()` defined in the wasm module named `seal0`. This is +/// because the module `seal0` is the default when no module is specified. +/// +/// To define a host function in `seal2` and `seal3` modules, it should be annotated with the +/// appropriate attribute as follows: +/// +/// ## Example +/// +/// ```nocompile +/// #[define_env] +/// pub mod some_env { +/// #[version(2)] +/// fn foo(ctx: _, memory: _, key_ptr: u32, value_ptr: u32, value_len: u32) -> Result { +/// ctx.some_host_fn(KeyType::Fix, key_ptr, value_ptr, value_len).map(|_| ()) +/// } +/// +/// #[version(3)] +/// #[unstable] +/// fn bar(ctx: _, memory: _, key_ptr: u32, value_ptr: u32, value_len: u32) -> Result { +/// ctx.some_host_fn(KeyType::Fix, key_ptr, value_ptr, value_len).map(|_| ()) +/// } +/// } +/// ``` +/// The function `bar` is additionally annotated with `unstable` which removes it from the stable +/// interface. Check out the README to learn about unstable functions. +/// +/// In legacy versions of pallet_contracts, it was a naming convention that all host functions had +/// to be named with the `seal_` prefix. For the sake of backwards compatibility, each host function +/// now can get a such prefix-named alias function generated by marking it by the +/// `#[prefixed_alias]` attribute: +/// +/// ## Example +/// +/// ```nocompile +/// #[define_env] +/// pub mod some_env { +/// #[version(1)] +/// #[prefixed_alias] +/// fn foo(ctx: _, memory: _, key_ptr: u32, value_ptr: u32, value_len: u32) -> Result { +/// ctx.some_host_fn(KeyType::Fix, key_ptr, value_ptr, value_len).map(|_| ()) +/// } +/// +/// #[version(42)] +/// fn bar(ctx: _, memory: _, key_ptr: u32, value_ptr: u32, value_len: u32) -> Result { +/// ctx.some_host_fn(KeyType::Fix, key_ptr, value_ptr, value_len).map(|_| ()) +/// } +/// } +/// ``` +/// +/// In this example, the following host functions will be generated by the macro: +/// - `foo()` in module `seal1`, +/// - `seal_foo()` in module `seal1`, +/// - `bar()` in module `seal42`. +/// +/// Only following return types are allowed for the host functions defined with the macro: +/// - `Result<(), TrapReason>`, +/// - `Result`, +/// - `Result`. +/// +/// The macro expands to `pub struct Env` declaration, with the following traits implementations: +/// - `pallet_contracts::wasm::Environment> where E: Ext` +/// - `pallet_contracts::wasm::Environment<()>` +/// +/// The implementation on `()` can be used in places where no `Ext` exists, yet. This is useful +/// when only checking whether a code can be instantiated without actually executing any code. +/// +/// # Generating Documentation +/// +/// Passing `doc` attribute to the macro (like `#[define_env(doc)]`) will make it also expand +/// additional `pallet_contracts::api_doc::seal0`, `pallet_contracts::api_doc::seal1`, +/// `...` modules each having its `Api` trait containing functions holding documentation for every +/// host function defined by the macro. +/// +/// # Deprecated Interfaces +/// +/// An interface can be annotated with `#[deprecated]`. It is mutually exclusive with `#[unstable]`. +/// Deprecated interfaces have the following properties: +/// - New contract codes utilizing those interfaces cannot be uploaded. +/// - New contracts from existing codes utilizing those interfaces cannot be instantiated. +/// - Existing contracts containing those interfaces still work. +/// +/// Those interfaces will eventually be removed. +/// +/// To build up these docs, run: +/// +/// ```nocompile +/// cargo doc +/// ``` +#[proc_macro_attribute] +pub fn define_env(attr: TokenStream, item: TokenStream) -> TokenStream { + if !attr.is_empty() && !(attr.to_string() == "doc".to_string()) { + let msg = r#"Invalid `define_env` attribute macro: expected either no attributes or a single `doc` attribute: + - `#[define_env]` + - `#[define_env(doc)]`"#; + let span = TokenStream2::from(attr).span(); + return syn::Error::new(span, msg).to_compile_error().into() + } + + let item = syn::parse_macro_input!(item as syn::ItemMod); + + match EnvDef::try_from(item) { + Ok(mut def) => expand_env(&mut def, !attr.is_empty()).into(), + Err(e) => e.to_compile_error().into(), + } +} diff --git a/pallets/contracts/src/address.rs b/pallets/contracts/src/address.rs new file mode 100644 index 00000000..5758daf7 --- /dev/null +++ b/pallets/contracts/src/address.rs @@ -0,0 +1,68 @@ +// This file is part of Substrate. + +// 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. + +//! Functions that deal with address derivation. + +use crate::{CodeHash, Config}; +use codec::{Decode, Encode}; +use sp_runtime::traits::{Hash, TrailingZeroInput}; + +/// Provides the contract address generation method. +/// +/// See [`DefaultAddressGenerator`] for the default implementation. +/// +/// # Note for implementors +/// +/// 1. Make sure that there are no collisions, different inputs never lead to the same output. +/// 2. Make sure that the same inputs lead to the same output. +pub trait AddressGenerator { + /// The address of a contract based on the given instantiate parameters. + /// + /// Changing the formular for an already deployed chain is fine as long as no collisions + /// with the old formular. Changes only affect existing contracts. + fn contract_address( + deploying_address: &T::AccountId, + code_hash: &CodeHash, + input_data: &[u8], + salt: &[u8], + ) -> T::AccountId; +} + +/// Default address generator. +/// +/// This is the default address generator used by contract instantiation. Its result +/// is only dependent on its inputs. It can therefore be used to reliably predict the +/// address of a contract. This is akin to the formula of eth's CREATE2 opcode. There +/// is no CREATE equivalent because CREATE2 is strictly more powerful. +/// Formula: +/// `hash("contract_addr_v1" ++ deploying_address ++ code_hash ++ input_data ++ salt)` +pub struct DefaultAddressGenerator; + +impl AddressGenerator for DefaultAddressGenerator { + /// Formula: `hash("contract_addr_v1" ++ deploying_address ++ code_hash ++ input_data ++ salt)` + fn contract_address( + deploying_address: &T::AccountId, + code_hash: &CodeHash, + input_data: &[u8], + salt: &[u8], + ) -> T::AccountId { + let entropy = (b"contract_addr_v1", deploying_address, code_hash, input_data, salt) + .using_encoded(T::Hashing::hash); + Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref())) + .expect("infinite length input; no invalid inputs for type; qed") + } +} diff --git a/pallets/contracts/src/benchmarking/call_builder.rs b/pallets/contracts/src/benchmarking/call_builder.rs new file mode 100644 index 00000000..5833639d --- /dev/null +++ b/pallets/contracts/src/benchmarking/call_builder.rs @@ -0,0 +1,236 @@ +// This file is part of Substrate. + +// 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::{ + benchmarking::{Contract, WasmModule}, + exec::{Ext, Key, Stack}, + storage::meter::Meter, + transient_storage::MeterEntry, + wasm::Runtime, + BalanceOf, Config, DebugBufferVec, Determinism, Error, ExecReturnValue, GasMeter, Origin, + Schedule, TypeInfo, WasmBlob, Weight, +}; +use alloc::{vec, vec::Vec}; +use codec::{Encode, HasCompact}; +use core::fmt::Debug; +use frame_benchmarking::benchmarking; +use sp_core::Get; + +type StackExt<'a, T> = Stack<'a, T, WasmBlob>; + +/// A prepared contract call ready to be executed. +pub struct PreparedCall<'a, T: Config> { + func: wasmi::Func, + store: wasmi::Store>>, +} + +impl<'a, T: Config> PreparedCall<'a, T> { + pub fn call(mut self) -> ExecReturnValue { + let result = self.func.call(&mut self.store, &[], &mut []); + WasmBlob::::process_result(self.store, result).unwrap() + } +} + +/// A builder used to prepare a contract call. +pub struct CallSetup { + contract: Contract, + dest: T::AccountId, + origin: Origin, + gas_meter: GasMeter, + storage_meter: Meter, + schedule: Schedule, + value: BalanceOf, + debug_message: Option>, + determinism: Determinism, + data: Vec, + transient_storage_size: u32, +} + +impl Default for CallSetup +where + T: Config + pallet_balances::Config, + as HasCompact>::Type: Clone + Eq + PartialEq + Debug + TypeInfo + Encode, +{ + fn default() -> Self { + Self::new(WasmModule::dummy()) + } +} + +impl CallSetup +where + T: Config + pallet_balances::Config, + as HasCompact>::Type: Clone + Eq + PartialEq + Debug + TypeInfo + Encode, +{ + /// Setup a new call for the given module. + pub fn new(module: WasmModule) -> Self { + let contract = Contract::::new(module.clone(), vec![]).unwrap(); + let dest = contract.account_id.clone(); + let origin = Origin::from_account_id(contract.caller.clone()); + + let storage_meter = Meter::new(&origin, None, 0u32.into()).unwrap(); + + // Whitelist contract account, as it is already accounted for in the call benchmark + benchmarking::add_to_whitelist( + frame_system::Account::::hashed_key_for(&contract.account_id).into(), + ); + + // Whitelist the contract's contractInfo as it is already accounted for in the call + // benchmark + benchmarking::add_to_whitelist( + crate::ContractInfoOf::::hashed_key_for(&contract.account_id).into(), + ); + + Self { + contract, + dest, + origin, + gas_meter: GasMeter::new(Weight::MAX), + storage_meter, + schedule: T::Schedule::get(), + value: 0u32.into(), + debug_message: None, + determinism: Determinism::Enforced, + data: vec![], + transient_storage_size: 0, + } + } + + /// Set the meter's storage deposit limit. + pub fn set_storage_deposit_limit(&mut self, balance: BalanceOf) { + self.storage_meter = Meter::new(&self.origin, Some(balance), 0u32.into()).unwrap(); + } + + /// Set the call's origin. + pub fn set_origin(&mut self, origin: Origin) { + self.origin = origin; + } + + /// Set the contract's balance. + pub fn set_balance(&mut self, value: BalanceOf) { + self.contract.set_balance(value); + } + + /// Set the call's input data. + pub fn set_data(&mut self, value: Vec) { + self.data = value; + } + + /// Set the transient storage size. + pub fn set_transient_storage_size(&mut self, size: u32) { + self.transient_storage_size = size; + } + + /// Set the debug message. + pub fn enable_debug_message(&mut self) { + self.debug_message = Some(Default::default()); + } + + /// Get the debug message. + pub fn debug_message(&self) -> Option> { + self.debug_message.clone() + } + + /// Get the call's input data. + pub fn data(&self) -> Vec { + self.data.clone() + } + + /// Get the call's contract. + pub fn contract(&self) -> Contract { + self.contract.clone() + } + + /// Build the call stack. + pub fn ext(&mut self) -> (StackExt<'_, T>, WasmBlob) { + let mut ext = StackExt::bench_new_call( + self.dest.clone(), + self.origin.clone(), + &mut self.gas_meter, + &mut self.storage_meter, + &self.schedule, + self.value, + self.debug_message.as_mut(), + self.determinism, + ); + if self.transient_storage_size > 0 { + Self::with_transient_storage(&mut ext.0, self.transient_storage_size).unwrap(); + } + ext + } + + /// Prepare a call to the module. + pub fn prepare_call<'a>( + ext: &'a mut StackExt<'a, T>, + module: WasmBlob, + input: Vec, + ) -> PreparedCall<'a, T> { + let (func, store) = module.bench_prepare_call(ext, input); + PreparedCall { func, store } + } + + /// Add transient_storage + fn with_transient_storage(ext: &mut StackExt, size: u32) -> Result<(), &'static str> { + let &MeterEntry { amount, limit } = ext.transient_storage().meter().current(); + ext.transient_storage().meter().current_mut().limit = size; + for i in 1u32.. { + let mut key_data = i.to_le_bytes().to_vec(); + while key_data.last() == Some(&0) { + key_data.pop(); + } + let key = Key::::try_from_var(key_data).unwrap(); + if let Err(e) = ext.set_transient_storage(&key, Some(Vec::new()), false) { + // Restore previous settings. + ext.transient_storage().meter().current_mut().limit = limit; + ext.transient_storage().meter().current_mut().amount = amount; + if e == Error::::OutOfTransientStorage.into() { + break; + } else { + return Err("Initialization of the transient storage failed"); + } + } + } + Ok(()) + } +} + +#[macro_export] +macro_rules! memory( + ($($bytes:expr,)*) => { + vec![] + .into_iter() + $(.chain($bytes))* + .collect::>() + }; +); + +#[macro_export] +macro_rules! build_runtime( + ($runtime:ident, $memory:ident: [$($segment:expr,)*]) => { + $crate::build_runtime!($runtime, _contract, $memory: [$($segment,)*]); + }; + ($runtime:ident, $contract:ident, $memory:ident: [$($bytes:expr,)*]) => { + $crate::build_runtime!($runtime, $contract); + let mut $memory = $crate::memory!($($bytes,)*); + }; + ($runtime:ident, $contract:ident) => { + let mut setup = CallSetup::::default(); + let $contract = setup.contract(); + let input = setup.data(); + let (mut ext, _) = setup.ext(); + let mut $runtime = crate::wasm::Runtime::new(&mut ext, input); + }; +); diff --git a/pallets/contracts/src/benchmarking/code.rs b/pallets/contracts/src/benchmarking/code.rs new file mode 100644 index 00000000..1473022b --- /dev/null +++ b/pallets/contracts/src/benchmarking/code.rs @@ -0,0 +1,364 @@ +// This file is part of Substrate. + +// 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. + +//! Functions to procedurally construct contract code used for benchmarking. +//! +//! In order to be able to benchmark events that are triggered by contract execution +//! (API calls into seal, individual instructions), we need to generate contracts that +//! perform those events. Because those contracts can get very big we cannot simply define +//! them as text (.wat) as this will be too slow and consume too much memory. Therefore +//! we define this simple definition of a contract that can be passed to `create_code` that +//! compiles it down into a `WasmModule` that can be used as a contract's code. + +use crate::Config; +use alloc::{borrow::ToOwned, vec, vec::Vec}; +use frame_support::traits::Get; +use sp_runtime::{traits::Hash, Saturating}; +use wasm_instrument::parity_wasm::{ + builder, + elements::{ + self, BlockType, CustomSection, FuncBody, Instruction, Instructions, Local, Section, + ValueType, + }, +}; + +/// The location where to put the generated code. +pub enum Location { + /// Generate all code into the `call` exported function. + Call, + /// Generate all code into the `deploy` exported function. + Deploy, +} + +/// Pass to `create_code` in order to create a compiled `WasmModule`. +/// +/// This exists to have a more declarative way to describe a wasm module than to use +/// parity-wasm directly. It is tailored to fit the structure of contracts that are +/// needed for benchmarking. +#[derive(Default)] +pub struct ModuleDefinition { + /// Imported memory attached to the module. No memory is imported if `None`. + pub memory: Option, + /// Initializers for the imported memory. + pub data_segments: Vec, + /// Creates the supplied amount of i64 mutable globals initialized with random values. + pub num_globals: u32, + /// List of functions that the module should import. They start with index 0. + pub imported_functions: Vec, + /// Function body of the exported `deploy` function. Body is empty if `None`. + /// Its index is `imported_functions.len()`. + pub deploy_body: Option, + /// Function body of the exported `call` function. Body is empty if `None`. + /// Its index is `imported_functions.len() + 1`. + pub call_body: Option, + /// Function body of a non-exported function with index `imported_functions.len() + 2`. + pub aux_body: Option, + /// The amount of I64 arguments the aux function should have. + pub aux_arg_num: u32, + /// Create a table containing function pointers. + pub table: Option, + /// Create a section named "dummy" of the specified size. This is useful in order to + /// benchmark the overhead of loading and storing codes of specified sizes. The dummy + /// section only contributes to the size of the contract but does not affect execution. + pub dummy_section: u32, +} + +pub struct TableSegment { + /// How many elements should be created inside the table. + pub num_elements: u32, + /// The function index with which all table elements should be initialized. + pub function_index: u32, +} + +pub struct DataSegment { + pub offset: u32, + pub value: Vec, +} + +#[derive(Clone)] +pub struct ImportedMemory { + pub min_pages: u32, + pub max_pages: u32, +} + +impl ImportedMemory { + pub fn max() -> Self { + let pages = max_pages::(); + Self { min_pages: pages, max_pages: pages } + } +} + +pub struct ImportedFunction { + pub module: &'static str, + pub name: &'static str, + pub params: Vec, + pub return_type: Option, +} + +/// A wasm module ready to be put on chain. +#[derive(Clone)] +pub struct WasmModule { + pub code: Vec, + pub hash: ::Output, + pub memory: Option, +} + +impl From for WasmModule { + fn from(def: ModuleDefinition) -> Self { + // internal functions start at that offset. + let func_offset = u32::try_from(def.imported_functions.len()).unwrap(); + + // Every contract must export "deploy" and "call" functions. + let mut contract = builder::module() + // deploy function (first internal function) + .function() + .signature() + .build() + .with_body( + def.deploy_body + .unwrap_or_else(|| FuncBody::new(Vec::new(), Instructions::empty())), + ) + .build() + // call function (second internal function) + .function() + .signature() + .build() + .with_body( + def.call_body + .unwrap_or_else(|| FuncBody::new(Vec::new(), Instructions::empty())), + ) + .build() + .export() + .field("deploy") + .internal() + .func(func_offset) + .build() + .export() + .field("call") + .internal() + .func(func_offset + 1) + .build(); + + // If specified we add an additional internal function + if let Some(body) = def.aux_body { + let mut signature = contract.function().signature(); + for _ in 0..def.aux_arg_num { + signature = signature.with_param(ValueType::I64); + } + contract = signature.build().with_body(body).build(); + } + + // Grant access to linear memory. + // Every contract module is required to have an imported memory. + // If no memory is specified in the passed ModuleDefinition, then + // default to (1, 1). + let (init, max) = if let Some(memory) = &def.memory { + (memory.min_pages, Some(memory.max_pages)) + } else { + (1, Some(1)) + }; + + contract = contract.import().path("env", "memory").external().memory(init, max).build(); + + // Import supervisor functions. They start with idx 0. + for func in def.imported_functions { + let sig = builder::signature() + .with_params(func.params) + .with_results(func.return_type) + .build_sig(); + let sig = contract.push_signature(sig); + contract = contract + .import() + .module(func.module) + .field(func.name) + .with_external(elements::External::Function(sig)) + .build(); + } + + // Initialize memory + for data in def.data_segments { + contract = contract + .data() + .offset(Instruction::I32Const(data.offset as i32)) + .value(data.value) + .build() + } + + // Add global variables + if def.num_globals > 0 { + use rand::{distributions::Standard, prelude::*}; + let rng = rand_pcg::Pcg32::seed_from_u64(3112244599778833558); + for val in rng.sample_iter(Standard).take(def.num_globals as usize) { + contract = contract + .global() + .value_type() + .i64() + .mutable() + .init_expr(Instruction::I64Const(val)) + .build() + } + } + + // Add function pointer table + if let Some(table) = def.table { + contract = contract + .table() + .with_min(table.num_elements) + .with_max(Some(table.num_elements)) + .with_element(0, vec![table.function_index; table.num_elements as usize]) + .build(); + } + + // Add the dummy section + if def.dummy_section > 0 { + contract = contract.with_section(Section::Custom(CustomSection::new( + "dummy".to_owned(), + vec![42; def.dummy_section as usize], + ))); + } + + let code = contract.build().into_bytes().unwrap(); + let hash = T::Hashing::hash(&code); + Self { code: code.into(), hash, memory: def.memory } + } +} + +impl WasmModule { + /// Creates a wasm module with an empty `call` and `deploy` function and nothing else. + pub fn dummy() -> Self { + ModuleDefinition::default().into() + } + + /// Same as `dummy` but with maximum sized linear memory and a dummy section of specified size. + pub fn dummy_with_bytes(dummy_bytes: u32) -> Self { + // We want the module to have the size `dummy_bytes`. + // This is not completely correct as the overhead grows when the contract grows + // because of variable length integer encoding. However, it is good enough to be that + // close for benchmarking purposes. + let module_overhead = 65; + ModuleDefinition { + memory: Some(ImportedMemory::max::()), + dummy_section: dummy_bytes.saturating_sub(module_overhead), + ..Default::default() + } + .into() + } + + /// Creates a wasm module of `target_bytes` size. Used to benchmark the performance of + /// `instantiate_with_code` for different sizes of wasm modules. The generated module maximizes + /// instrumentation runtime by nesting blocks as deeply as possible given the byte budget. + /// `code_location`: Whether to place the code into `deploy` or `call`. + pub fn sized(target_bytes: u32, code_location: Location, use_float: bool) -> Self { + use self::elements::Instruction::{End, GetLocal, If, Return}; + // Base size of a contract is 63 bytes and each expansion adds 6 bytes. + // We do one expansion less to account for the code section and function body + // size fields inside the binary wasm module representation which are leb128 encoded + // and therefore grow in size when the contract grows. We are not allowed to overshoot + // because of the maximum code size that is enforced by `instantiate_with_code`. + let mut expansions = (target_bytes.saturating_sub(63) / 6).saturating_sub(1); + const EXPANSION: [Instruction; 4] = [GetLocal(0), If(BlockType::NoResult), Return, End]; + let mut locals = vec![Local::new(1, ValueType::I32)]; + if use_float { + locals.push(Local::new(1, ValueType::F32)); + locals.push(Local::new(2, ValueType::F32)); + locals.push(Local::new(3, ValueType::F32)); + expansions.saturating_dec(); + } + let mut module = + ModuleDefinition { memory: Some(ImportedMemory::max::()), ..Default::default() }; + let body = Some(body::repeated_with_locals(&locals, expansions, &EXPANSION)); + match code_location { + Location::Call => module.call_body = body, + Location::Deploy => module.deploy_body = body, + } + module.into() + } + + /// Creates a wasm module that calls the imported function `noop` `repeat` times. + pub fn noop(repeat: u32) -> Self { + let pages = max_pages::(); + ModuleDefinition { + memory: Some(ImportedMemory::max::()), + imported_functions: vec![ImportedFunction { + module: "seal0", + name: "noop", + params: vec![], + return_type: None, + }], + // Write the output buffer size. The output size will be overwritten by the + // supervisor with the real size when calling the getter. Since this size does not + // change between calls it suffices to start with an initial value and then just + // leave as whatever value was written there. + data_segments: vec![DataSegment { + offset: 0, + value: (pages * 64 * 1024 - 4).to_le_bytes().to_vec(), + }], + call_body: Some(body::repeated( + repeat, + &[ + Instruction::Call(0), // call the imported function + ], + )), + ..Default::default() + } + .into() + } +} + +/// Mechanisms to generate a function body that can be used inside a `ModuleDefinition`. +pub mod body { + use super::*; + + pub fn repeated(repetitions: u32, instructions: &[Instruction]) -> FuncBody { + repeated_with_locals(&[], repetitions, instructions) + } + + pub fn repeated_with_locals( + locals: &[Local], + repetitions: u32, + instructions: &[Instruction], + ) -> FuncBody { + let instructions = Instructions::new( + instructions + .iter() + .cycle() + .take(instructions.len() * usize::try_from(repetitions).unwrap()) + .cloned() + .chain(core::iter::once(Instruction::End)) + .collect(), + ); + FuncBody::new(locals.to_vec(), instructions) + } + + pub fn repeated_with_locals_using( + locals: &[Local], + repetitions: u32, + mut f: impl FnMut() -> [Instruction; N], + ) -> FuncBody { + let mut instructions = Vec::new(); + for _ in 0..repetitions { + instructions.extend(f()); + } + instructions.push(Instruction::End); + FuncBody::new(locals.to_vec(), Instructions::new(instructions)) + } +} + +/// The maximum amount of pages any contract is allowed to have according to the current `Schedule`. +pub fn max_pages() -> u32 { + T::Schedule::get().limits.memory_pages +} diff --git a/pallets/contracts/src/benchmarking/mod.rs b/pallets/contracts/src/benchmarking/mod.rs new file mode 100644 index 00000000..669279f1 --- /dev/null +++ b/pallets/contracts/src/benchmarking/mod.rs @@ -0,0 +1,2074 @@ +// This file is part of Substrate. + +// 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. + +//! Benchmarks for the contracts pallet +#![cfg(feature = "runtime-benchmarks")] + +mod call_builder; +mod code; +mod sandbox; +use self::{ + call_builder::CallSetup, + code::{body, ImportedMemory, Location, ModuleDefinition, WasmModule}, + sandbox::Sandbox, +}; +use crate::{ + exec::{Key, SeedOf}, + migration::{ + codegen::LATEST_MIGRATION_VERSION, v09, v10, v11, v12, v13, v14, v15, v16, MigrationStep, + }, + storage::WriteOutcome, + wasm::BenchEnv, + Pallet as Contracts, *, +}; +use alloc::{vec, vec::Vec}; +use codec::{Encode, MaxEncodedLen}; +use frame_benchmarking::v2::*; +use frame_support::{ + self, assert_ok, + pallet_prelude::StorageVersion, + storage::child, + traits::{fungible::InspectHold, Currency}, + weights::{Weight, WeightMeter}, +}; +use frame_system::RawOrigin; +use pallet_balances; +use pallet_contracts_uapi::{CallFlags, ReturnErrorCode}; +use sp_runtime::traits::{Bounded, Hash}; +use wasm_instrument::parity_wasm::elements::{Instruction, Local, ValueType}; + +/// How many runs we do per API benchmark. +/// +/// This is picked more or less arbitrary. We experimented with different numbers until +/// the results appeared to be stable. Reducing the number would speed up the benchmarks +/// but might make the results less precise. +const API_BENCHMARK_RUNS: u32 = 1600; + +/// How many runs we do per instruction benchmark. +/// +/// Same rationale as for [`API_BENCHMARK_RUNS`]. The number is bigger because instruction +/// benchmarks are faster. +const INSTR_BENCHMARK_RUNS: u32 = 5000; + +/// Number of layers in a Radix16 unbalanced trie. +const UNBALANCED_TRIE_LAYERS: u32 = 20; + +/// An instantiated and deployed contract. +#[derive(Clone)] +struct Contract { + caller: T::AccountId, + account_id: T::AccountId, + addr: AccountIdLookupOf, + value: BalanceOf, +} + +impl Contract +where + T: Config + pallet_balances::Config, + as HasCompact>::Type: Clone + Eq + PartialEq + Debug + TypeInfo + Encode, +{ + /// Create new contract and use a default account id as instantiator. + fn new(module: WasmModule, data: Vec) -> Result, &'static str> { + Self::with_index(0, module, data) + } + + /// Create new contract and use an account id derived from the supplied index as instantiator. + fn with_index( + index: u32, + module: WasmModule, + data: Vec, + ) -> Result, &'static str> { + Self::with_caller(account("instantiator", index, 0), module, data) + } + + /// Create new contract and use the supplied `caller` as instantiator. + fn with_caller( + caller: T::AccountId, + module: WasmModule, + data: Vec, + ) -> Result, &'static str> { + let value = Pallet::::min_balance(); + T::Currency::set_balance(&caller, caller_funding::()); + let salt = vec![0xff]; + let addr = Contracts::::contract_address(&caller, &module.hash, &data, &salt); + + Contracts::::store_code_raw(module.code, caller.clone())?; + Contracts::::instantiate( + RawOrigin::Signed(caller.clone()).into(), + value, + Weight::MAX, + None, + module.hash, + data, + salt, + )?; + + let result = + Contract { caller, account_id: addr.clone(), addr: T::Lookup::unlookup(addr), value }; + + ContractInfoOf::::insert(&result.account_id, result.info()?); + + Ok(result) + } + + /// Create a new contract with the supplied storage item count and size each. + fn with_storage( + code: WasmModule, + stor_num: u32, + stor_size: u32, + ) -> Result { + let contract = Contract::::new(code, vec![])?; + let storage_items = (0..stor_num) + .map(|i| { + let hash = T::Hashing::hash_of(&i) + .as_ref() + .try_into() + .map_err(|_| "Hash too big for storage key")?; + Ok((hash, vec![42u8; stor_size as usize])) + }) + .collect::, &'static str>>()?; + contract.store(&storage_items)?; + Ok(contract) + } + + /// Store the supplied storage items into this contracts storage. + fn store(&self, items: &Vec<([u8; 32], Vec)>) -> Result<(), &'static str> { + let info = self.info()?; + for item in items { + info.write(&Key::Fix(item.0), Some(item.1.clone()), None, false) + .map_err(|_| "Failed to write storage to restoration dest")?; + } + >::insert(&self.account_id, info); + Ok(()) + } + + /// Create a new contract with the specified unbalanced storage trie. + fn with_unbalanced_storage_trie(code: WasmModule, key: &[u8]) -> Result { + if (key.len() as u32) < (UNBALANCED_TRIE_LAYERS + 1) / 2 { + return Err("Key size too small to create the specified trie"); + } + + let value = vec![16u8; T::Schedule::get().limits.payload_len as usize]; + let contract = Contract::::new(code, vec![])?; + let info = contract.info()?; + let child_trie_info = info.child_trie_info(); + child::put_raw(&child_trie_info, &key, &value); + for l in 0..UNBALANCED_TRIE_LAYERS { + let pos = l as usize / 2; + let mut key_new = key.to_vec(); + for i in 0u8..16 { + key_new[pos] = if l % 2 == 0 { + (key_new[pos] & 0xF0) | i + } else { + (key_new[pos] & 0x0F) | (i << 4) + }; + + if key == &key_new { + continue + } + child::put_raw(&child_trie_info, &key_new, &value); + } + } + Ok(contract) + } + + /// Get the `ContractInfo` of the `addr` or an error if it no longer exists. + fn address_info(addr: &T::AccountId) -> Result, &'static str> { + ContractInfoOf::::get(addr).ok_or("Expected contract to exist at this point.") + } + + /// Get the `ContractInfo` of this contract or an error if it no longer exists. + fn info(&self) -> Result, &'static str> { + Self::address_info(&self.account_id) + } + + /// Set the balance of the contract to the supplied amount. + fn set_balance(&self, balance: BalanceOf) { + T::Currency::set_balance(&self.account_id, balance); + } + + /// Returns `true` iff all storage entries related to code storage exist. + fn code_exists(hash: &CodeHash) -> bool { + >::contains_key(hash) && >::contains_key(&hash) + } + + /// Returns `true` iff no storage entry related to code storage exist. + fn code_removed(hash: &CodeHash) -> bool { + !>::contains_key(hash) && !>::contains_key(&hash) + } +} + +/// The funding that each account that either calls or instantiates contracts is funded with. +fn caller_funding() -> BalanceOf { + // Minting can overflow, so we can't abuse of the funding. This value happens to be big enough, + // but not too big to make the total supply overflow. + BalanceOf::::max_value() / 10_000u32.into() +} + +#[benchmarks( + where + as codec::HasCompact>::Type: Clone + Eq + PartialEq + core::fmt::Debug + scale_info::TypeInfo + codec::Encode, + T: Config + pallet_balances::Config, + BalanceOf: From< as Currency>::Balance>, + as Currency>::Balance: From>, +)] +mod benchmarks { + use super::*; + + // The base weight consumed on processing contracts deletion queue. + #[benchmark(pov_mode = Measured)] + fn on_process_deletion_queue_batch() { + #[block] + { + ContractInfo::::process_deletion_queue_batch(&mut WeightMeter::new()) + } + } + + #[benchmark(skip_meta, pov_mode = Measured)] + fn on_initialize_per_trie_key(k: Linear<0, 1024>) -> Result<(), BenchmarkError> { + let instance = Contract::::with_storage( + WasmModule::dummy(), + k, + T::Schedule::get().limits.payload_len, + )?; + instance.info()?.queue_trie_for_deletion(); + + #[block] + { + ContractInfo::::process_deletion_queue_batch(&mut WeightMeter::new()) + } + + Ok(()) + } + + // This benchmarks the v9 migration step (update codeStorage). + #[benchmark(pov_mode = Measured)] + fn v9_migration_step(c: Linear<0, { T::MaxCodeLen::get() }>) { + v09::store_old_dummy_code::(c as usize); + let mut m = v09::Migration::::default(); + #[block] + { + m.step(&mut WeightMeter::new()); + } + } + + // This benchmarks the v10 migration step (use dedicated deposit_account). + #[benchmark(pov_mode = Measured)] + fn v10_migration_step() -> Result<(), BenchmarkError> { + let contract = + >::with_caller(whitelisted_caller(), WasmModule::dummy(), vec![])?; + + v10::store_old_contract_info::>( + contract.account_id.clone(), + contract.info()?, + ); + let mut m = v10::Migration::>::default(); + + #[block] + { + m.step(&mut WeightMeter::new()); + } + + Ok(()) + } + + // This benchmarks the v11 migration step (Don't rely on reserved balances keeping an account + // alive). + #[benchmark(pov_mode = Measured)] + fn v11_migration_step(k: Linear<0, 1024>) { + v11::fill_old_queue::(k as usize); + let mut m = v11::Migration::::default(); + + #[block] + { + m.step(&mut WeightMeter::new()); + } + } + + // This benchmarks the v12 migration step (Move `OwnerInfo` to `CodeInfo`, + // add `determinism` field to the latter, clear `CodeStorage` + // and repay deposits). + #[benchmark(pov_mode = Measured)] + fn v12_migration_step(c: Linear<0, { T::MaxCodeLen::get() }>) { + v12::store_old_dummy_code::>( + c as usize, + account::("account", 0, 0), + ); + let mut m = v12::Migration::>::default(); + + #[block] + { + m.step(&mut WeightMeter::new()); + } + } + + // This benchmarks the v13 migration step (Add delegate_dependencies field). + #[benchmark(pov_mode = Measured)] + fn v13_migration_step() -> Result<(), BenchmarkError> { + let contract = + >::with_caller(whitelisted_caller(), WasmModule::dummy(), vec![])?; + + v13::store_old_contract_info::(contract.account_id.clone(), contract.info()?); + let mut m = v13::Migration::::default(); + + #[block] + { + m.step(&mut WeightMeter::new()); + } + Ok(()) + } + + // This benchmarks the v14 migration step (Move code owners' reserved balance to be held + // instead). + #[benchmark(pov_mode = Measured)] + fn v14_migration_step() { + let account = account::("account", 0, 0); + T::Currency::set_balance(&account, caller_funding::()); + v14::store_dummy_code::>(account); + let mut m = v14::Migration::>::default(); + + #[block] + { + m.step(&mut WeightMeter::new()); + } + } + + // This benchmarks the v15 migration step (remove deposit account). + #[benchmark(pov_mode = Measured)] + fn v15_migration_step() -> Result<(), BenchmarkError> { + let contract = + >::with_caller(whitelisted_caller(), WasmModule::dummy(), vec![])?; + + v15::store_old_contract_info::(contract.account_id.clone(), contract.info()?); + let mut m = v15::Migration::::default(); + + #[block] + { + m.step(&mut WeightMeter::new()); + } + + Ok(()) + } + + // This benchmarks the v16 migration step (Remove ED from base_deposit). + #[benchmark(pov_mode = Measured)] + fn v16_migration_step() -> Result<(), BenchmarkError> { + let contract = + >::with_caller(whitelisted_caller(), WasmModule::dummy(), vec![])?; + + let info = contract.info()?; + let base_deposit = v16::store_old_contract_info::(contract.account_id.clone(), &info); + let mut m = v16::Migration::::default(); + + #[block] + { + m.step(&mut WeightMeter::new()); + } + let ed = Pallet::::min_balance(); + let info = v16::ContractInfoOf::::get(&contract.account_id).unwrap(); + assert_eq!(info.storage_base_deposit, base_deposit - ed); + Ok(()) + } + + // This benchmarks the weight of executing Migration::migrate to execute a noop migration. + #[benchmark(pov_mode = Measured)] + fn migration_noop() { + let version = LATEST_MIGRATION_VERSION; + StorageVersion::new(version).put::>(); + #[block] + { + Migration::::migrate(&mut WeightMeter::new()); + } + assert_eq!(StorageVersion::get::>(), version); + } + + // This benchmarks the weight of dispatching migrate to execute 1 `NoopMigration` + #[benchmark(pov_mode = Measured)] + fn migrate() { + let latest_version = LATEST_MIGRATION_VERSION; + StorageVersion::new(latest_version - 2).put::>(); + as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade(); + + #[extrinsic_call] + _(RawOrigin::Signed(whitelisted_caller()), Weight::MAX); + + assert_eq!(StorageVersion::get::>(), latest_version - 1); + } + + // This benchmarks the weight of running on_runtime_upgrade when there are no migration in + // progress. + #[benchmark(pov_mode = Measured)] + fn on_runtime_upgrade_noop() { + let latest_version = LATEST_MIGRATION_VERSION; + StorageVersion::new(latest_version).put::>(); + #[block] + { + as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade(); + } + assert!(MigrationInProgress::::get().is_none()); + } + + // This benchmarks the weight of running on_runtime_upgrade when there is a migration in + // progress. + #[benchmark(pov_mode = Measured)] + fn on_runtime_upgrade_in_progress() { + let latest_version = LATEST_MIGRATION_VERSION; + StorageVersion::new(latest_version - 2).put::>(); + let v = vec![42u8].try_into().ok(); + MigrationInProgress::::set(v.clone()); + #[block] + { + as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade(); + } + assert!(MigrationInProgress::::get().is_some()); + assert_eq!(MigrationInProgress::::get(), v); + } + + // This benchmarks the weight of running on_runtime_upgrade when there is a migration to + // process. + #[benchmark(pov_mode = Measured)] + fn on_runtime_upgrade() { + let latest_version = LATEST_MIGRATION_VERSION; + StorageVersion::new(latest_version - 2).put::>(); + #[block] + { + as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade(); + } + assert!(MigrationInProgress::::get().is_some()); + } + + // This benchmarks the overhead of loading a code of size `c` byte from storage and into + // the sandbox. This does **not** include the actual execution for which the gas meter + // is responsible. This is achieved by generating all code to the `deploy` function + // which is in the wasm module but not executed on `call`. + // The results are supposed to be used as `call_with_code_per_byte(c) - + // call_with_code_per_byte(0)`. + #[benchmark(pov_mode = Measured)] + fn call_with_code_per_byte( + c: Linear<0, { T::MaxCodeLen::get() }>, + ) -> Result<(), BenchmarkError> { + let instance = Contract::::with_caller( + whitelisted_caller(), + WasmModule::sized(c, Location::Deploy, false), + vec![], + )?; + let value = Pallet::::min_balance(); + let callee = instance.addr; + + #[extrinsic_call] + call(RawOrigin::Signed(instance.caller.clone()), callee, value, Weight::MAX, None, vec![]); + + Ok(()) + } + + // `c`: Size of the code in bytes. + // `i`: Size of the input in bytes. + // `s`: Size of the salt in bytes. + #[benchmark(pov_mode = Measured)] + fn instantiate_with_code( + c: Linear<0, { T::MaxCodeLen::get() }>, + i: Linear<0, { code::max_pages::() * 64 * 1024 }>, + s: Linear<0, { code::max_pages::() * 64 * 1024 }>, + ) { + let input = vec![42u8; i as usize]; + let salt = vec![42u8; s as usize]; + let value = Pallet::::min_balance(); + let caller = whitelisted_caller(); + T::Currency::set_balance(&caller, caller_funding::()); + let WasmModule { code, hash, .. } = WasmModule::::sized(c, Location::Call, false); + let origin = RawOrigin::Signed(caller.clone()); + let addr = Contracts::::contract_address(&caller, &hash, &input, &salt); + #[extrinsic_call] + _(origin, value, Weight::MAX, None, code, input, salt); + + let deposit = + T::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &addr); + // uploading the code reserves some balance in the callers account + let code_deposit = + T::Currency::balance_on_hold(&HoldReason::CodeUploadDepositReserve.into(), &caller); + assert_eq!( + T::Currency::balance(&caller), + caller_funding::() - value - deposit - code_deposit - Pallet::::min_balance(), + ); + // contract has the full value + assert_eq!(T::Currency::balance(&addr), value + Pallet::::min_balance()); + } + + // `i`: Size of the input in bytes. + // `s`: Size of the salt in bytes. + #[benchmark(pov_mode = Measured)] + fn instantiate( + i: Linear<0, { code::max_pages::() * 64 * 1024 }>, + s: Linear<0, { code::max_pages::() * 64 * 1024 }>, + ) -> Result<(), BenchmarkError> { + let input = vec![42u8; i as usize]; + let salt = vec![42u8; s as usize]; + let value = Pallet::::min_balance(); + let caller = whitelisted_caller(); + T::Currency::set_balance(&caller, caller_funding::()); + let WasmModule { code, hash, .. } = WasmModule::::dummy(); + let addr = Contracts::::contract_address(&caller, &hash, &input, &salt); + Contracts::::store_code_raw(code, caller.clone())?; + + #[extrinsic_call] + _(RawOrigin::Signed(caller.clone()), value, Weight::MAX, None, hash, input, salt); + + let deposit = + T::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &addr); + // value was removed from the caller + assert_eq!( + T::Currency::balance(&caller), + caller_funding::() - value - deposit - Pallet::::min_balance(), + ); + // contract has the full value + assert_eq!(T::Currency::balance(&addr), value + Pallet::::min_balance()); + + Ok(()) + } + + // We just call a dummy contract to measure the overhead of the call extrinsic. + // The size of the data has no influence on the costs of this extrinsic as long as the contract + // won't call `seal_input` in its constructor to copy the data to contract memory. + // The dummy contract used here does not do this. The costs for the data copy is billed as + // part of `seal_input`. The costs for invoking a contract of a specific size are not part + // of this benchmark because we cannot know the size of the contract when issuing a call + // transaction. See `call_with_code_per_byte` for this. + #[benchmark(pov_mode = Measured)] + fn call() -> Result<(), BenchmarkError> { + let data = vec![42u8; 1024]; + let instance = + Contract::::with_caller(whitelisted_caller(), WasmModule::dummy(), vec![])?; + let value = Pallet::::min_balance(); + let origin = RawOrigin::Signed(instance.caller.clone()); + let callee = instance.addr.clone(); + let before = T::Currency::balance(&instance.account_id); + #[extrinsic_call] + _(origin, callee, value, Weight::MAX, None, data); + let deposit = T::Currency::balance_on_hold( + &HoldReason::StorageDepositReserve.into(), + &instance.account_id, + ); + // value and value transferred via call should be removed from the caller + assert_eq!( + T::Currency::balance(&instance.caller), + caller_funding::() - instance.value - value - deposit - Pallet::::min_balance(), + ); + // contract should have received the value + assert_eq!(T::Currency::balance(&instance.account_id), before + value); + // contract should still exist + instance.info()?; + + Ok(()) + } + + // This constructs a contract that is maximal expensive to instrument. + // It creates a maximum number of metering blocks per byte. + // `c`: Size of the code in bytes. + #[benchmark(pov_mode = Measured)] + fn upload_code_determinism_enforced(c: Linear<0, { T::MaxCodeLen::get() }>) { + let caller = whitelisted_caller(); + T::Currency::set_balance(&caller, caller_funding::()); + let WasmModule { code, hash, .. } = WasmModule::::sized(c, Location::Call, false); + let origin = RawOrigin::Signed(caller.clone()); + #[extrinsic_call] + upload_code(origin, code, None, Determinism::Enforced); + // uploading the code reserves some balance in the callers account + assert!(T::Currency::total_balance_on_hold(&caller) > 0u32.into()); + assert!(>::code_exists(&hash)); + } + + // Uploading code with [`Determinism::Relaxed`] should be more expensive than uploading code + // with [`Determinism::Enforced`], as we always try to save the code with + // [`Determinism::Enforced`] first. + #[benchmark(pov_mode = Measured)] + fn upload_code_determinism_relaxed(c: Linear<0, { T::MaxCodeLen::get() }>) { + let caller = whitelisted_caller(); + T::Currency::set_balance(&caller, caller_funding::()); + let WasmModule { code, hash, .. } = WasmModule::::sized(c, Location::Call, true); + let origin = RawOrigin::Signed(caller.clone()); + #[extrinsic_call] + upload_code(origin, code, None, Determinism::Relaxed); + assert!(T::Currency::total_balance_on_hold(&caller) > 0u32.into()); + assert!(>::code_exists(&hash)); + // Ensure that the benchmark follows the most expensive path, i.e., the code is saved with + assert_eq!(CodeInfoOf::::get(&hash).unwrap().determinism(), Determinism::Relaxed); + } + + // Removing code does not depend on the size of the contract because all the information + // needed to verify the removal claim (refcount, owner) is stored in a separate storage + // item (`CodeInfoOf`). + #[benchmark(pov_mode = Measured)] + fn remove_code() -> Result<(), BenchmarkError> { + let caller = whitelisted_caller(); + T::Currency::set_balance(&caller, caller_funding::()); + let WasmModule { code, hash, .. } = WasmModule::::dummy(); + let origin = RawOrigin::Signed(caller.clone()); + let uploaded = + >::bare_upload_code(caller.clone(), code, None, Determinism::Enforced)?; + assert_eq!(uploaded.code_hash, hash); + assert_eq!(uploaded.deposit, T::Currency::total_balance_on_hold(&caller)); + assert!(>::code_exists(&hash)); + #[extrinsic_call] + _(origin, hash); + // removing the code should have unreserved the deposit + assert_eq!(T::Currency::total_balance_on_hold(&caller), 0u32.into()); + assert!(>::code_removed(&hash)); + Ok(()) + } + + #[benchmark(pov_mode = Measured)] + fn set_code() -> Result<(), BenchmarkError> { + let instance = + >::with_caller(whitelisted_caller(), WasmModule::dummy(), vec![])?; + // we just add some bytes so that the code hash is different + let WasmModule { code, hash, .. } = >::dummy_with_bytes(128); + >::store_code_raw(code, instance.caller.clone())?; + let callee = instance.addr.clone(); + assert_ne!(instance.info()?.code_hash, hash); + #[extrinsic_call] + _(RawOrigin::Root, callee, hash); + assert_eq!(instance.info()?.code_hash, hash); + Ok(()) + } + + #[benchmark(pov_mode = Measured)] + fn noop_host_fn(r: Linear<0, API_BENCHMARK_RUNS>) { + let mut setup = CallSetup::::new(WasmModule::noop(r)); + let (mut ext, module) = setup.ext(); + let func = CallSetup::::prepare_call(&mut ext, module, vec![]); + #[block] + { + func.call(); + } + } + + #[benchmark(pov_mode = Measured)] + fn seal_caller() { + let len = ::max_encoded_len() as u32; + build_runtime!(runtime, memory: [len.to_le_bytes(), vec![0u8; len as _], ]); + + let result; + #[block] + { + result = BenchEnv::seal0_caller(&mut runtime, &mut memory, 4, 0); + } + + assert_ok!(result); + assert_eq!( + &::decode(&mut &memory[4..]).unwrap(), + runtime.ext().caller().account_id().unwrap() + ); + } + + #[benchmark(pov_mode = Measured)] + fn seal_is_contract() { + let Contract { account_id, .. } = + Contract::::with_index(1, WasmModule::dummy(), vec![]).unwrap(); + + build_runtime!(runtime, memory: [account_id.encode(), ]); + + let result; + #[block] + { + result = BenchEnv::seal0_is_contract(&mut runtime, &mut memory, 0); + } + + assert_eq!(result.unwrap(), 1); + } + + #[benchmark(pov_mode = Measured)] + fn seal_code_hash() { + let contract = Contract::::with_index(1, WasmModule::dummy(), vec![]).unwrap(); + let len = as MaxEncodedLen>::max_encoded_len() as u32; + build_runtime!(runtime, memory: [len.to_le_bytes(), vec![0u8; len as _], contract.account_id.encode(), ]); + + let result; + #[block] + { + result = BenchEnv::seal0_code_hash(&mut runtime, &mut memory, 4 + len, 4, 0); + } + + assert_ok!(result); + assert_eq!( + as Decode>::decode(&mut &memory[4..]).unwrap(), + contract.info().unwrap().code_hash + ); + } + + #[benchmark(pov_mode = Measured)] + fn seal_own_code_hash() { + let len = as MaxEncodedLen>::max_encoded_len() as u32; + build_runtime!(runtime, contract, memory: [len.to_le_bytes(), vec![0u8; len as _], ]); + let result; + #[block] + { + result = BenchEnv::seal0_own_code_hash(&mut runtime, &mut memory, 4, 0); + } + + assert_ok!(result); + assert_eq!( + as Decode>::decode(&mut &memory[4..]).unwrap(), + contract.info().unwrap().code_hash + ); + } + + #[benchmark(pov_mode = Measured)] + fn seal_caller_is_origin() { + build_runtime!(runtime, memory: []); + + let result; + #[block] + { + result = BenchEnv::seal0_caller_is_origin(&mut runtime, &mut memory); + } + assert_eq!(result.unwrap(), 1u32); + } + + #[benchmark(pov_mode = Measured)] + fn seal_caller_is_root() { + let mut setup = CallSetup::::default(); + setup.set_origin(Origin::Root); + let (mut ext, _) = setup.ext(); + let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![]); + + let result; + #[block] + { + result = BenchEnv::seal0_caller_is_root(&mut runtime, &mut [0u8; 0]); + } + assert_eq!(result.unwrap(), 1u32); + } + + #[benchmark(pov_mode = Measured)] + fn seal_address() { + let len = as MaxEncodedLen>::max_encoded_len() as u32; + build_runtime!(runtime, memory: [len.to_le_bytes(), vec![0u8; len as _], ]); + + let result; + #[block] + { + result = BenchEnv::seal0_address(&mut runtime, &mut memory, 4, 0); + } + assert_ok!(result); + assert_eq!( + &::decode(&mut &memory[4..]).unwrap(), + runtime.ext().address() + ); + } + + #[benchmark(pov_mode = Measured)] + fn seal_gas_left() { + // use correct max_encoded_len when new version of parity-scale-codec is released + let len = 18u32; + assert!(::max_encoded_len() as u32 != len); + build_runtime!(runtime, memory: [32u32.to_le_bytes(), vec![0u8; len as _], ]); + + let result; + #[block] + { + result = BenchEnv::seal1_gas_left(&mut runtime, &mut memory, 4, 0); + } + assert_ok!(result); + assert_eq!( + ::decode(&mut &memory[4..]).unwrap(), + runtime.ext().gas_meter().gas_left() + ); + } + + #[benchmark(pov_mode = Measured)] + fn seal_balance() { + let len = ::max_encoded_len() as u32; + build_runtime!(runtime, memory: [len.to_le_bytes(), vec![0u8; len as _], ]); + let result; + #[block] + { + result = BenchEnv::seal0_seal_balance(&mut runtime, &mut memory, 4, 0); + } + assert_ok!(result); + assert_eq!( + ::decode(&mut &memory[4..]).unwrap(), + runtime.ext().balance().into() + ); + } + + #[benchmark(pov_mode = Measured)] + fn seal_value_transferred() { + let len = ::max_encoded_len() as u32; + build_runtime!(runtime, memory: [len.to_le_bytes(), vec![0u8; len as _], ]); + let result; + #[block] + { + result = BenchEnv::seal0_value_transferred(&mut runtime, &mut memory, 4, 0); + } + assert_ok!(result); + assert_eq!( + ::decode(&mut &memory[4..]).unwrap(), + runtime.ext().value_transferred().into() + ); + } + + #[benchmark(pov_mode = Measured)] + fn seal_minimum_balance() { + let len = ::max_encoded_len() as u32; + build_runtime!(runtime, memory: [len.to_le_bytes(), vec![0u8; len as _], ]); + let result; + #[block] + { + result = BenchEnv::seal0_minimum_balance(&mut runtime, &mut memory, 4, 0); + } + assert_ok!(result); + assert_eq!( + ::decode(&mut &memory[4..]).unwrap(), + runtime.ext().minimum_balance().into() + ); + } + + #[benchmark(pov_mode = Measured)] + fn seal_block_number() { + let len = as MaxEncodedLen>::max_encoded_len() as u32; + build_runtime!(runtime, memory: [len.to_le_bytes(), vec![0u8; len as _], ]); + let result; + #[block] + { + result = BenchEnv::seal0_seal_block_number(&mut runtime, &mut memory, 4, 0); + } + assert_ok!(result); + assert_eq!( + >::decode(&mut &memory[4..]).unwrap(), + runtime.ext().block_number() + ); + } + + #[benchmark(pov_mode = Measured)] + fn seal_now() { + let len = as MaxEncodedLen>::max_encoded_len() as u32; + build_runtime!(runtime, memory: [len.to_le_bytes(), vec![0u8; len as _], ]); + let result; + #[block] + { + result = BenchEnv::seal0_seal_now(&mut runtime, &mut memory, 4, 0); + } + assert_ok!(result); + assert_eq!(>::decode(&mut &memory[4..]).unwrap(), *runtime.ext().now()); + } + + #[benchmark(pov_mode = Measured)] + fn seal_weight_to_fee() { + let len = ::max_encoded_len() as u32; + build_runtime!(runtime, memory: [len.to_le_bytes(), vec![0u8; len as _], ]); + let weight = Weight::from_parts(500_000, 300_000); + let result; + #[block] + { + result = BenchEnv::seal1_weight_to_fee( + &mut runtime, + &mut memory, + weight.ref_time(), + weight.proof_size(), + 4, + 0, + ); + } + assert_ok!(result); + assert_eq!( + >::decode(&mut &memory[4..]).unwrap(), + runtime.ext().get_weight_price(weight) + ); + } + + #[benchmark(pov_mode = Measured)] + fn seal_input(n: Linear<0, { code::max_pages::() * 64 * 1024 - 4 }>) { + let mut setup = CallSetup::::default(); + let (mut ext, _) = setup.ext(); + let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![42u8; n as usize]); + let mut memory = memory!(n.to_le_bytes(), vec![0u8; n as usize],); + let result; + #[block] + { + result = BenchEnv::seal0_input(&mut runtime, &mut memory, 4, 0); + } + assert_ok!(result); + assert_eq!(&memory[4..], &vec![42u8; n as usize]); + } + + #[benchmark(pov_mode = Measured)] + fn seal_return(n: Linear<0, { code::max_pages::() * 64 * 1024 - 4 }>) { + build_runtime!(runtime, memory: [n.to_le_bytes(), vec![42u8; n as usize], ]); + + let result; + #[block] + { + result = BenchEnv::seal0_seal_return(&mut runtime, &mut memory, 0, 0, n); + } + + assert!(matches!( + result, + Err(crate::wasm::TrapReason::Return(crate::wasm::ReturnData { .. })) + )); + } + + #[benchmark(pov_mode = Measured)] + fn seal_terminate( + n: Linear<0, { T::MaxDelegateDependencies::get() }>, + ) -> Result<(), BenchmarkError> { + let beneficiary = account::("beneficiary", 0, 0); + let caller = whitelisted_caller(); + + build_runtime!(runtime, memory: [beneficiary.encode(),]); + + T::Currency::set_balance(&caller, caller_funding::()); + + (0..n).for_each(|i| { + let new_code = WasmModule::::dummy_with_bytes(65 + i); + Contracts::::store_code_raw(new_code.code, caller.clone()).unwrap(); + runtime.ext().lock_delegate_dependency(new_code.hash).unwrap(); + }); + + let result; + #[block] + { + result = BenchEnv::seal1_terminate(&mut runtime, &mut memory, 0); + } + + assert!(matches!(result, Err(crate::wasm::TrapReason::Termination))); + + Ok(()) + } + + // We benchmark only for the maximum subject length. We assume that this is some lowish + // number (< 1 KB). Therefore we are not overcharging too much in case a smaller subject is + // used. + #[benchmark(pov_mode = Measured)] + fn seal_random() { + let subject_len = T::Schedule::get().limits.subject_len; + assert!(subject_len < 1024); + + let output_len = + <(SeedOf, BlockNumberFor) as MaxEncodedLen>::max_encoded_len() as u32; + + build_runtime!(runtime, memory: [ + output_len.to_le_bytes(), + vec![42u8; subject_len as _], + vec![0u8; output_len as _], + ]); + + let result; + #[block] + { + result = BenchEnv::seal0_random( + &mut runtime, + &mut memory, + 4, // subject_ptr + subject_len, // subject_len + subject_len + 4, // output_ptr + 0, // output_len_ptr + ); + } + + assert_ok!(result); + assert_ok!(<(SeedOf, BlockNumberFor)>::decode(&mut &memory[subject_len as _..])); + } + + // Benchmark the overhead that topics generate. + // `t`: Number of topics + // `n`: Size of event payload in bytes + #[benchmark(pov_mode = Measured)] + fn seal_deposit_event( + t: Linear<0, { T::Schedule::get().limits.event_topics }>, + n: Linear<0, { T::Schedule::get().limits.payload_len }>, + ) { + let topics = (0..t).map(|i| T::Hashing::hash_of(&i)).collect::>().encode(); + let topics_len = topics.len() as u32; + + build_runtime!(runtime, memory: [ + n.to_le_bytes(), + topics, + vec![0u8; n as _], + ]); + + let result; + #[block] + { + result = BenchEnv::seal0_deposit_event( + &mut runtime, + &mut memory, + 4, // topics_ptr + topics_len, // topics_len + 4 + topics_len, // data_ptr + 0, // data_len + ); + } + + assert_ok!(result); + } + + // Benchmark debug_message call + // Whereas this function is used in RPC mode only, it still should be secured + // against an excessive use. + // + // i: size of input in bytes up to maximum allowed contract memory or maximum allowed debug + // buffer size, whichever is less. + #[benchmark] + fn seal_debug_message( + i: Linear< + 0, + { + (T::Schedule::get().limits.memory_pages * 64 * 1024) + .min(T::MaxDebugBufferLen::get()) + }, + >, + ) { + let mut setup = CallSetup::::default(); + setup.enable_debug_message(); + let (mut ext, _) = setup.ext(); + let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![]); + // Fill memory with printable ASCII bytes. + let mut memory = (0..i).zip((32..127).cycle()).map(|i| i.1).collect::>(); + + let result; + #[block] + { + result = BenchEnv::seal0_debug_message(&mut runtime, &mut memory, 0, i); + } + assert_ok!(result); + assert_eq!(setup.debug_message().unwrap().len() as u32, i); + } + + #[benchmark(skip_meta, pov_mode = Measured)] + fn get_storage_empty() -> Result<(), BenchmarkError> { + let max_key_len = T::MaxStorageKeyLen::get(); + let key = vec![0u8; max_key_len as usize]; + let max_value_len = T::Schedule::get().limits.payload_len as usize; + let value = vec![1u8; max_value_len]; + + let instance = Contract::::new(WasmModule::dummy(), vec![])?; + let info = instance.info()?; + let child_trie_info = info.child_trie_info(); + info.bench_write_raw(&key, Some(value.clone()), false) + .map_err(|_| "Failed to write to storage during setup.")?; + + let result; + #[block] + { + result = child::get_raw(&child_trie_info, &key); + } + + assert_eq!(result, Some(value)); + Ok(()) + } + + #[benchmark(skip_meta, pov_mode = Measured)] + fn get_storage_full() -> Result<(), BenchmarkError> { + let max_key_len = T::MaxStorageKeyLen::get(); + let key = vec![0u8; max_key_len as usize]; + let max_value_len = T::Schedule::get().limits.payload_len; + let value = vec![1u8; max_value_len as usize]; + + let instance = Contract::::with_unbalanced_storage_trie(WasmModule::dummy(), &key)?; + let info = instance.info()?; + let child_trie_info = info.child_trie_info(); + info.bench_write_raw(&key, Some(value.clone()), false) + .map_err(|_| "Failed to write to storage during setup.")?; + + let result; + #[block] + { + result = child::get_raw(&child_trie_info, &key); + } + + assert_eq!(result, Some(value)); + Ok(()) + } + + #[benchmark(skip_meta, pov_mode = Measured)] + fn set_storage_empty() -> Result<(), BenchmarkError> { + let max_key_len = T::MaxStorageKeyLen::get(); + let key = vec![0u8; max_key_len as usize]; + let max_value_len = T::Schedule::get().limits.payload_len as usize; + let value = vec![1u8; max_value_len]; + + let instance = Contract::::new(WasmModule::dummy(), vec![])?; + let info = instance.info()?; + let child_trie_info = info.child_trie_info(); + info.bench_write_raw(&key, Some(vec![42u8; max_value_len]), false) + .map_err(|_| "Failed to write to storage during setup.")?; + + let val = Some(value.clone()); + let result; + #[block] + { + result = info.bench_write_raw(&key, val, true); + } + + assert_ok!(result); + assert_eq!(child::get_raw(&child_trie_info, &key).unwrap(), value); + Ok(()) + } + + #[benchmark(skip_meta, pov_mode = Measured)] + fn set_storage_full() -> Result<(), BenchmarkError> { + let max_key_len = T::MaxStorageKeyLen::get(); + let key = vec![0u8; max_key_len as usize]; + let max_value_len = T::Schedule::get().limits.payload_len; + let value = vec![1u8; max_value_len as usize]; + + let instance = Contract::::with_unbalanced_storage_trie(WasmModule::dummy(), &key)?; + let info = instance.info()?; + let child_trie_info = info.child_trie_info(); + info.bench_write_raw(&key, Some(vec![42u8; max_value_len as usize]), false) + .map_err(|_| "Failed to write to storage during setup.")?; + + let val = Some(value.clone()); + let result; + #[block] + { + result = info.bench_write_raw(&key, val, true); + } + + assert_ok!(result); + assert_eq!(child::get_raw(&child_trie_info, &key).unwrap(), value); + Ok(()) + } + + // n: new byte size + // o: old byte size + #[benchmark(skip_meta, pov_mode = Measured)] + fn seal_set_storage( + n: Linear<0, { T::Schedule::get().limits.payload_len }>, + o: Linear<0, { T::Schedule::get().limits.payload_len }>, + ) -> Result<(), BenchmarkError> { + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + let value = vec![1u8; n as usize]; + + build_runtime!(runtime, instance, memory: [ key.to_vec(), value.clone(), ]); + let info = instance.info()?; + + info.write(&key, Some(vec![42u8; o as usize]), None, false) + .map_err(|_| "Failed to write to storage during setup.")?; + + let result; + #[block] + { + result = BenchEnv::seal2_set_storage( + &mut runtime, + &mut memory, + 0, // key_ptr + max_key_len, // key_len + max_key_len, // value_ptr + n, // value_len + ); + } + + assert_ok!(result); + assert_eq!(info.read(&key).unwrap(), value); + Ok(()) + } + + #[benchmark(skip_meta, pov_mode = Measured)] + fn seal_clear_storage( + n: Linear<0, { T::Schedule::get().limits.payload_len }>, + ) -> Result<(), BenchmarkError> { + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + build_runtime!(runtime, instance, memory: [ key.to_vec(), ]); + let info = instance.info()?; + + info.write(&key, Some(vec![42u8; n as usize]), None, false) + .map_err(|_| "Failed to write to storage during setup.")?; + + let result; + #[block] + { + result = BenchEnv::seal1_clear_storage(&mut runtime, &mut memory, 0, max_key_len); + } + + assert_ok!(result); + assert!(info.read(&key).is_none()); + Ok(()) + } + + #[benchmark(skip_meta, pov_mode = Measured)] + fn seal_get_storage( + n: Linear<0, { T::Schedule::get().limits.payload_len }>, + ) -> Result<(), BenchmarkError> { + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + build_runtime!(runtime, instance, memory: [ key.to_vec(), n.to_le_bytes(), vec![0u8; n as _], ]); + let info = instance.info()?; + + info.write(&key, Some(vec![42u8; n as usize]), None, false) + .map_err(|_| "Failed to write to storage during setup.")?; + + let out_ptr = max_key_len + 4; + let result; + #[block] + { + result = BenchEnv::seal1_get_storage( + &mut runtime, + &mut memory, + 0, // key_ptr + max_key_len, // key_len + out_ptr, // out_ptr + max_key_len, // out_len_ptr + ); + } + + assert_ok!(result); + assert_eq!(&info.read(&key).unwrap(), &memory[out_ptr as usize..]); + Ok(()) + } + + #[benchmark(skip_meta, pov_mode = Measured)] + fn seal_contains_storage( + n: Linear<0, { T::Schedule::get().limits.payload_len }>, + ) -> Result<(), BenchmarkError> { + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + build_runtime!(runtime, instance, memory: [ key.to_vec(), ]); + let info = instance.info()?; + + info.write(&key, Some(vec![42u8; n as usize]), None, false) + .map_err(|_| "Failed to write to storage during setup.")?; + + let result; + #[block] + { + result = BenchEnv::seal1_contains_storage(&mut runtime, &mut memory, 0, max_key_len); + } + + assert_eq!(result.unwrap(), n); + Ok(()) + } + + #[benchmark(skip_meta, pov_mode = Measured)] + fn seal_take_storage( + n: Linear<0, { T::Schedule::get().limits.payload_len }>, + ) -> Result<(), BenchmarkError> { + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + build_runtime!(runtime, instance, memory: [ key.to_vec(), n.to_le_bytes(), vec![0u8; n as _], ]); + let info = instance.info()?; + + let value = vec![42u8; n as usize]; + info.write(&key, Some(value.clone()), None, false) + .map_err(|_| "Failed to write to storage during setup.")?; + + let out_ptr = max_key_len + 4; + let result; + #[block] + { + result = BenchEnv::seal0_take_storage( + &mut runtime, + &mut memory, + 0, // key_ptr + max_key_len, // key_len + out_ptr, // out_ptr + max_key_len, // out_len_ptr + ); + } + + assert_ok!(result); + assert!(&info.read(&key).is_none()); + assert_eq!(&value, &memory[out_ptr as usize..]); + Ok(()) + } + + // We use both full and empty benchmarks here instead of benchmarking transient_storage + // (BTreeMap) directly. This approach is necessary because benchmarking this BTreeMap is very + // slow. Additionally, we use linear regression for our benchmarks, and the BTreeMap's log(n) + // complexity can introduce approximation errors. + #[benchmark(pov_mode = Ignored)] + fn set_transient_storage_empty() -> Result<(), BenchmarkError> { + let max_value_len = T::Schedule::get().limits.payload_len; + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + let value = Some(vec![42u8; max_value_len as _]); + let mut setup = CallSetup::::default(); + let (mut ext, _) = setup.ext(); + let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![]); + runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX; + let result; + #[block] + { + result = runtime.ext().set_transient_storage(&key, value, false); + } + + assert_eq!(result, Ok(WriteOutcome::New)); + assert_eq!(runtime.ext().get_transient_storage(&key), Some(vec![42u8; max_value_len as _])); + Ok(()) + } + + #[benchmark(pov_mode = Ignored)] + fn set_transient_storage_full() -> Result<(), BenchmarkError> { + let max_value_len = T::Schedule::get().limits.payload_len; + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + let value = Some(vec![42u8; max_value_len as _]); + let mut setup = CallSetup::::default(); + setup.set_transient_storage_size(T::MaxTransientStorageSize::get()); + let (mut ext, _) = setup.ext(); + let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![]); + runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX; + let result; + #[block] + { + result = runtime.ext().set_transient_storage(&key, value, false); + } + + assert_eq!(result, Ok(WriteOutcome::New)); + assert_eq!(runtime.ext().get_transient_storage(&key), Some(vec![42u8; max_value_len as _])); + Ok(()) + } + + #[benchmark(pov_mode = Ignored)] + fn get_transient_storage_empty() -> Result<(), BenchmarkError> { + let max_value_len = T::Schedule::get().limits.payload_len; + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + + let mut setup = CallSetup::::default(); + let (mut ext, _) = setup.ext(); + let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![]); + runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX; + runtime + .ext() + .set_transient_storage(&key, Some(vec![42u8; max_value_len as _]), false) + .map_err(|_| "Failed to write to transient storage during setup.")?; + let result; + #[block] + { + result = runtime.ext().get_transient_storage(&key); + } + + assert_eq!(result, Some(vec![42u8; max_value_len as _])); + Ok(()) + } + + #[benchmark(pov_mode = Ignored)] + fn get_transient_storage_full() -> Result<(), BenchmarkError> { + let max_value_len = T::Schedule::get().limits.payload_len; + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + + let mut setup = CallSetup::::default(); + setup.set_transient_storage_size(T::MaxTransientStorageSize::get()); + let (mut ext, _) = setup.ext(); + let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![]); + runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX; + runtime + .ext() + .set_transient_storage(&key, Some(vec![42u8; max_value_len as _]), false) + .map_err(|_| "Failed to write to transient storage during setup.")?; + let result; + #[block] + { + result = runtime.ext().get_transient_storage(&key); + } + + assert_eq!(result, Some(vec![42u8; max_value_len as _])); + Ok(()) + } + + // The weight of journal rollbacks should be taken into account when setting storage. + #[benchmark(pov_mode = Ignored)] + fn rollback_transient_storage() -> Result<(), BenchmarkError> { + let max_value_len = T::Schedule::get().limits.payload_len; + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + + let mut setup = CallSetup::::default(); + setup.set_transient_storage_size(T::MaxTransientStorageSize::get()); + let (mut ext, _) = setup.ext(); + let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![]); + runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX; + runtime.ext().transient_storage().start_transaction(); + runtime + .ext() + .set_transient_storage(&key, Some(vec![42u8; max_value_len as _]), false) + .map_err(|_| "Failed to write to transient storage during setup.")?; + #[block] + { + runtime.ext().transient_storage().rollback_transaction(); + } + + assert_eq!(runtime.ext().get_transient_storage(&key), None); + Ok(()) + } + + // n: new byte size + // o: old byte size + #[benchmark(pov_mode = Measured)] + fn seal_set_transient_storage( + n: Linear<0, { T::Schedule::get().limits.payload_len }>, + o: Linear<0, { T::Schedule::get().limits.payload_len }>, + ) -> Result<(), BenchmarkError> { + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + let value = vec![1u8; n as usize]; + build_runtime!(runtime, memory: [ key.to_vec(), value.clone(), ]); + runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX; + runtime + .ext() + .set_transient_storage(&key, Some(vec![42u8; o as usize]), false) + .map_err(|_| "Failed to write to transient storage during setup.")?; + + let result; + #[block] + { + result = BenchEnv::seal0_set_transient_storage( + &mut runtime, + &mut memory, + 0, // key_ptr + max_key_len, // key_len + max_key_len, // value_ptr + n, // value_len + ); + } + + assert_ok!(result); + assert_eq!(runtime.ext().get_transient_storage(&key).unwrap(), value); + Ok(()) + } + + #[benchmark(pov_mode = Measured)] + fn seal_clear_transient_storage( + n: Linear<0, { T::Schedule::get().limits.payload_len }>, + ) -> Result<(), BenchmarkError> { + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + build_runtime!(runtime, memory: [ key.to_vec(), ]); + runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX; + runtime + .ext() + .set_transient_storage(&key, Some(vec![42u8; n as usize]), false) + .map_err(|_| "Failed to write to transient storage during setup.")?; + + let result; + #[block] + { + result = + BenchEnv::seal0_clear_transient_storage(&mut runtime, &mut memory, 0, max_key_len); + } + + assert_ok!(result); + assert!(runtime.ext().get_transient_storage(&key).is_none()); + Ok(()) + } + + #[benchmark(pov_mode = Measured)] + fn seal_get_transient_storage( + n: Linear<0, { T::Schedule::get().limits.payload_len }>, + ) -> Result<(), BenchmarkError> { + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + build_runtime!(runtime, memory: [ key.to_vec(), n.to_le_bytes(), vec![0u8; n as _], ]); + runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX; + runtime + .ext() + .set_transient_storage(&key, Some(vec![42u8; n as usize]), false) + .map_err(|_| "Failed to write to transient storage during setup.")?; + + let out_ptr = max_key_len + 4; + let result; + #[block] + { + result = BenchEnv::seal0_get_transient_storage( + &mut runtime, + &mut memory, + 0, // key_ptr + max_key_len, // key_len + out_ptr, // out_ptr + max_key_len, // out_len_ptr + ); + } + + assert_ok!(result); + assert_eq!( + &runtime.ext().get_transient_storage(&key).unwrap(), + &memory[out_ptr as usize..] + ); + Ok(()) + } + + #[benchmark(pov_mode = Measured)] + fn seal_contains_transient_storage( + n: Linear<0, { T::Schedule::get().limits.payload_len }>, + ) -> Result<(), BenchmarkError> { + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + build_runtime!(runtime, memory: [ key.to_vec(), ]); + runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX; + runtime + .ext() + .set_transient_storage(&key, Some(vec![42u8; n as usize]), false) + .map_err(|_| "Failed to write to transient storage during setup.")?; + + let result; + #[block] + { + result = BenchEnv::seal0_contains_transient_storage( + &mut runtime, + &mut memory, + 0, + max_key_len, + ); + } + + assert_eq!(result.unwrap(), n); + Ok(()) + } + + #[benchmark(pov_mode = Measured)] + fn seal_take_transient_storage( + n: Linear<0, { T::Schedule::get().limits.payload_len }>, + ) -> Result<(), BenchmarkError> { + let n = T::Schedule::get().limits.payload_len; + let max_key_len = T::MaxStorageKeyLen::get(); + let key = Key::::try_from_var(vec![0u8; max_key_len as usize]) + .map_err(|_| "Key has wrong length")?; + build_runtime!(runtime, memory: [ key.to_vec(), n.to_le_bytes(), vec![0u8; n as _], ]); + runtime.ext().transient_storage().meter().current_mut().limit = u32::MAX; + let value = vec![42u8; n as usize]; + runtime + .ext() + .set_transient_storage(&key, Some(value.clone()), false) + .map_err(|_| "Failed to write to transient storage during setup.")?; + + let out_ptr = max_key_len + 4; + let result; + #[block] + { + result = BenchEnv::seal0_take_transient_storage( + &mut runtime, + &mut memory, + 0, // key_ptr + max_key_len, // key_len + out_ptr, // out_ptr + max_key_len, // out_len_ptr + ); + } + + assert_ok!(result); + assert!(&runtime.ext().get_transient_storage(&key).is_none()); + assert_eq!(&value, &memory[out_ptr as usize..]); + Ok(()) + } + + // We transfer to unique accounts. + #[benchmark(pov_mode = Measured)] + fn seal_transfer() { + let account = account::("receiver", 0, 0); + let value = Pallet::::min_balance(); + assert!(value > 0u32.into()); + + let mut setup = CallSetup::::default(); + setup.set_balance(value); + let (mut ext, _) = setup.ext(); + let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![]); + + let account_bytes = account.encode(); + let account_len = account_bytes.len() as u32; + let value_bytes = value.encode(); + let value_len = value_bytes.len() as u32; + let mut memory = memory!(account_bytes, value_bytes,); + + let result; + #[block] + { + result = BenchEnv::seal0_transfer( + &mut runtime, + &mut memory, + 0, // account_ptr + account_len, + account_len, + value_len, + ); + } + + assert_ok!(result); + } + + // t: with or without some value to transfer + // i: size of the input data + #[benchmark(pov_mode = Measured)] + fn seal_call(t: Linear<0, 1>, i: Linear<0, { code::max_pages::() * 64 * 1024 }>) { + let Contract { account_id: callee, .. } = + Contract::::with_index(1, WasmModule::dummy(), vec![]).unwrap(); + let callee_bytes = callee.encode(); + let callee_len = callee_bytes.len() as u32; + + let value: BalanceOf = t.into(); + let value_bytes = value.encode(); + + let deposit: BalanceOf = (u32::MAX - 100).into(); + let deposit_bytes = deposit.encode(); + let deposit_len = deposit_bytes.len() as u32; + + let mut setup = CallSetup::::default(); + setup.set_storage_deposit_limit(deposit); + setup.set_data(vec![42; i as usize]); + setup.set_origin(Origin::from_account_id(setup.contract().account_id.clone())); + + let (mut ext, _) = setup.ext(); + let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![]); + let mut memory = memory!(callee_bytes, deposit_bytes, value_bytes,); + + let result; + #[block] + { + result = BenchEnv::seal2_call( + &mut runtime, + &mut memory, + CallFlags::CLONE_INPUT.bits(), // flags + 0, // callee_ptr + 0, // ref_time_limit + 0, // proof_size_limit + callee_len, // deposit_ptr + callee_len + deposit_len, // value_ptr + 0, // input_data_ptr + 0, // input_data_len + SENTINEL, // output_ptr + 0, // output_len_ptr + ); + } + + assert_ok!(result); + } + + #[benchmark(pov_mode = Measured)] + fn seal_delegate_call() -> Result<(), BenchmarkError> { + let hash = Contract::::with_index(1, WasmModule::dummy(), vec![])?.info()?.code_hash; + + let mut setup = CallSetup::::default(); + setup.set_origin(Origin::from_account_id(setup.contract().account_id.clone())); + + let (mut ext, _) = setup.ext(); + let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![]); + let mut memory = memory!(hash.encode(),); + + let result; + #[block] + { + result = BenchEnv::seal0_delegate_call( + &mut runtime, + &mut memory, + 0, // flags + 0, // code_hash_ptr + 0, // input_data_ptr + 0, // input_data_len + SENTINEL, // output_ptr + 0, + ); + } + + assert_ok!(result); + Ok(()) + } + + // t: value to transfer + // i: size of input in bytes + // s: size of salt in bytes + #[benchmark(pov_mode = Measured)] + fn seal_instantiate( + i: Linear<0, { (code::max_pages::() - 1) * 64 * 1024 }>, + s: Linear<0, { (code::max_pages::() - 1) * 64 * 1024 }>, + ) -> Result<(), BenchmarkError> { + let hash = Contract::::with_index(1, WasmModule::dummy(), vec![])?.info()?.code_hash; + let hash_bytes = hash.encode(); + let hash_len = hash_bytes.len() as u32; + + let value: BalanceOf = 1u32.into(); + let value_bytes = value.encode(); + let value_len = value_bytes.len() as u32; + + let deposit: BalanceOf = 0u32.into(); + let deposit_bytes = deposit.encode(); + let deposit_len = deposit_bytes.len() as u32; + + let mut setup = CallSetup::::default(); + setup.set_origin(Origin::from_account_id(setup.contract().account_id.clone())); + setup.set_balance(value + (Pallet::::min_balance() * 2u32.into())); + + let account_id = &setup.contract().account_id.clone(); + let (mut ext, _) = setup.ext(); + let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![]); + + let input = vec![42u8; i as _]; + let salt = vec![42u8; s as _]; + let addr = Contracts::::contract_address(&account_id, &hash, &input, &salt); + let mut memory = memory!(hash_bytes, deposit_bytes, value_bytes, input, salt,); + + let mut offset = { + let mut current = 0u32; + move |after: u32| { + current += after; + current + } + }; + + assert!(ContractInfoOf::::get(&addr).is_none()); + + let result; + #[block] + { + result = BenchEnv::seal2_instantiate( + &mut runtime, + &mut memory, + 0, // code_hash_ptr + 0, // ref_time_limit + 0, // proof_size_limit + offset(hash_len), // deposit_ptr + offset(deposit_len), // value_ptr + offset(value_len), // input_data_ptr + i, // input_data_len + SENTINEL, // address_ptr + 0, // address_len_ptr + SENTINEL, // output_ptr + 0, // output_len_ptr + offset(i), // salt_ptr + s, // salt_len + ); + } + + assert_ok!(result); + assert!(ContractInfoOf::::get(&addr).is_some()); + assert_eq!(T::Currency::balance(&addr), Pallet::::min_balance() + value); + Ok(()) + } + + // `n`: Input to hash in bytes + #[benchmark(pov_mode = Measured)] + fn seal_hash_sha2_256(n: Linear<0, { code::max_pages::() * 64 * 1024 }>) { + build_runtime!(runtime, memory: [[0u8; 32], vec![0u8; n as usize], ]); + + let result; + #[block] + { + result = BenchEnv::seal0_hash_sha2_256(&mut runtime, &mut memory, 32, n, 0); + } + assert_eq!(sp_io::hashing::sha2_256(&memory[32..]), &memory[0..32]); + assert_ok!(result); + } + + // `n`: Input to hash in bytes + #[benchmark(pov_mode = Measured)] + fn seal_hash_keccak_256(n: Linear<0, { code::max_pages::() * 64 * 1024 }>) { + build_runtime!(runtime, memory: [[0u8; 32], vec![0u8; n as usize], ]); + + let result; + #[block] + { + result = BenchEnv::seal0_hash_keccak_256(&mut runtime, &mut memory, 32, n, 0); + } + assert_eq!(sp_io::hashing::keccak_256(&memory[32..]), &memory[0..32]); + assert_ok!(result); + } + + // `n`: Input to hash in bytes + #[benchmark(pov_mode = Measured)] + fn seal_hash_blake2_256(n: Linear<0, { code::max_pages::() * 64 * 1024 }>) { + build_runtime!(runtime, memory: [[0u8; 32], vec![0u8; n as usize], ]); + + let result; + #[block] + { + result = BenchEnv::seal0_hash_blake2_256(&mut runtime, &mut memory, 32, n, 0); + } + assert_eq!(sp_io::hashing::blake2_256(&memory[32..]), &memory[0..32]); + assert_ok!(result); + } + + // `n`: Input to hash in bytes + #[benchmark(pov_mode = Measured)] + fn seal_hash_blake2_128(n: Linear<0, { code::max_pages::() * 64 * 1024 }>) { + build_runtime!(runtime, memory: [[0u8; 16], vec![0u8; n as usize], ]); + + let result; + #[block] + { + result = BenchEnv::seal0_hash_blake2_128(&mut runtime, &mut memory, 16, n, 0); + } + assert_eq!(sp_io::hashing::blake2_128(&memory[16..]), &memory[0..16]); + assert_ok!(result); + } + + // `n`: Message input length to verify in bytes. + // need some buffer so the code size does not exceed the max code size. + #[benchmark(pov_mode = Measured)] + fn seal_sr25519_verify(n: Linear<0, { T::MaxCodeLen::get() - 255 }>) { + let message = (0..n).zip((32u8..127u8).cycle()).map(|(_, c)| c).collect::>(); + let message_len = message.len() as u32; + + let key_type = sp_core::crypto::KeyTypeId(*b"code"); + let pub_key = sp_io::crypto::sr25519_generate(key_type, None); + let sig = + sp_io::crypto::sr25519_sign(key_type, &pub_key, &message).expect("Generates signature"); + let sig = AsRef::<[u8; 64]>::as_ref(&sig).to_vec(); + let sig_len = sig.len() as u32; + + build_runtime!(runtime, memory: [sig, pub_key.to_vec(), message, ]); + + let result; + #[block] + { + result = BenchEnv::seal0_sr25519_verify( + &mut runtime, + &mut memory, + 0, // signature_ptr + sig_len, // pub_key_ptr + message_len, // message_len + sig_len + pub_key.len() as u32, // message_ptr + ); + } + + assert_eq!(result.unwrap(), ReturnErrorCode::Success); + } + + #[benchmark(pov_mode = Measured)] + fn seal_ecdsa_recover() { + let message_hash = sp_io::hashing::blake2_256("Hello world".as_bytes()); + let key_type = sp_core::crypto::KeyTypeId(*b"code"); + let signature = { + let pub_key = sp_io::crypto::ecdsa_generate(key_type, None); + let sig = sp_io::crypto::ecdsa_sign_prehashed(key_type, &pub_key, &message_hash) + .expect("Generates signature"); + AsRef::<[u8; 65]>::as_ref(&sig).to_vec() + }; + + build_runtime!(runtime, memory: [signature, message_hash, [0u8; 33], ]); + + let result; + #[block] + { + result = BenchEnv::seal0_ecdsa_recover( + &mut runtime, + &mut memory, + 0, // signature_ptr + 65, // message_hash_ptr + 65 + 32, // output_ptr + ); + } + + assert_eq!(result.unwrap(), ReturnErrorCode::Success); + } + + // Only calling the function itself for the list of + // generated different ECDSA keys. + // This is a slow call: We reduce the number of runs. + #[benchmark(pov_mode = Measured)] + fn seal_ecdsa_to_eth_address() { + let key_type = sp_core::crypto::KeyTypeId(*b"code"); + let pub_key_bytes = sp_io::crypto::ecdsa_generate(key_type, None).0; + build_runtime!(runtime, memory: [[0u8; 20], pub_key_bytes,]); + + let result; + #[block] + { + result = BenchEnv::seal0_ecdsa_to_eth_address( + &mut runtime, + &mut memory, + 20, // key_ptr + 0, // output_ptr + ); + } + + assert_ok!(result); + assert_eq!(&memory[..20], runtime.ext().ecdsa_to_eth_address(&pub_key_bytes).unwrap()); + } + + #[benchmark(pov_mode = Measured)] + fn seal_set_code_hash() -> Result<(), BenchmarkError> { + let code_hash = + Contract::::with_index(1, WasmModule::dummy(), vec![])?.info()?.code_hash; + + build_runtime!(runtime, memory: [ code_hash.encode(),]); + + let result; + #[block] + { + result = BenchEnv::seal0_set_code_hash(&mut runtime, &mut memory, 0); + } + + assert_ok!(result); + Ok(()) + } + + #[benchmark(pov_mode = Measured)] + fn lock_delegate_dependency() -> Result<(), BenchmarkError> { + let code_hash = Contract::::with_index(1, WasmModule::dummy_with_bytes(1), vec![])? + .info()? + .code_hash; + + build_runtime!(runtime, memory: [ code_hash.encode(),]); + + let result; + #[block] + { + result = BenchEnv::seal0_lock_delegate_dependency(&mut runtime, &mut memory, 0); + } + + assert_ok!(result); + Ok(()) + } + + #[benchmark] + fn unlock_delegate_dependency() -> Result<(), BenchmarkError> { + let code_hash = Contract::::with_index(1, WasmModule::dummy_with_bytes(1), vec![])? + .info()? + .code_hash; + + build_runtime!(runtime, memory: [ code_hash.encode(),]); + BenchEnv::seal0_lock_delegate_dependency(&mut runtime, &mut memory, 0).unwrap(); + + let result; + #[block] + { + result = BenchEnv::seal0_unlock_delegate_dependency(&mut runtime, &mut memory, 0); + } + + assert_ok!(result); + Ok(()) + } + + #[benchmark(pov_mode = Measured)] + fn seal_reentrance_count() { + build_runtime!(runtime, memory: []); + let result; + #[block] + { + result = BenchEnv::seal0_reentrance_count(&mut runtime, &mut memory) + } + + assert_eq!(result.unwrap(), 0); + } + + #[benchmark(pov_mode = Measured)] + fn seal_account_reentrance_count() { + let Contract { account_id, .. } = + Contract::::with_index(1, WasmModule::dummy(), vec![]).unwrap(); + build_runtime!(runtime, memory: [account_id.encode(),]); + + let result; + #[block] + { + result = BenchEnv::seal0_account_reentrance_count(&mut runtime, &mut memory, 0); + } + + assert_eq!(result.unwrap(), 0); + } + + #[benchmark(pov_mode = Measured)] + fn seal_instantiation_nonce() { + build_runtime!(runtime, memory: []); + + let result; + #[block] + { + result = BenchEnv::seal0_instantiation_nonce(&mut runtime, &mut memory); + } + + assert_eq!(result.unwrap(), 1); + } + + // We load `i64` values from random linear memory locations and store the loaded + // values back into yet another random linear memory location. + // The random addresses are uniformly distributed across the entire span of the linear memory. + // We do this to enforce random memory accesses which are particularly expensive. + // + // The combination of this computation is our weight base `w_base`. + #[benchmark(pov_mode = Ignored)] + fn instr_i64_load_store(r: Linear<0, INSTR_BENCHMARK_RUNS>) -> Result<(), BenchmarkError> { + use rand::prelude::*; + + // We do not need to be secure here. Fixed seed allows for deterministic results. + let mut rng = rand_pcg::Pcg32::seed_from_u64(8446744073709551615); + + let memory = ImportedMemory::max::(); + let bytes_per_page = 65536; + let bytes_per_memory = memory.max_pages * bytes_per_page; + let mut sbox = Sandbox::from(&WasmModule::::from(ModuleDefinition { + memory: Some(memory), + call_body: Some(body::repeated_with_locals_using( + &[Local::new(1, ValueType::I64)], + r, + || { + // Instruction sequence to load a `i64` from linear memory + // at a random memory location and store it back into another + // location of the linear memory. + let c0: i32 = rng.gen_range(0..bytes_per_memory as i32); + let c1: i32 = rng.gen_range(0..bytes_per_memory as i32); + [ + Instruction::I32Const(c0), // address for `i64.load_8s` + Instruction::I64Load8S(0, 0), + Instruction::SetLocal(0), /* temporarily store value loaded in + * `i64.load_8s` */ + Instruction::I32Const(c1), // address for `i64.store8` + Instruction::GetLocal(0), // value to be stores in `i64.store8` + Instruction::I64Store8(0, 0), + ] + }, + )), + ..Default::default() + })); + #[block] + { + sbox.invoke(); + } + Ok(()) + } + + // This is no benchmark. It merely exist to have an easy way to pretty print the currently + // configured `Schedule` during benchmark development. Check the README on how to print this. + #[benchmark(extra, pov_mode = Ignored)] + fn print_schedule() -> Result<(), BenchmarkError> { + let max_weight = ::BlockWeights::get().max_block; + let (weight_per_key, key_budget) = + ContractInfo::::deletion_budget(&mut WeightMeter::with_limit(max_weight)); + let schedule = T::Schedule::get(); + log::info!(target: LOG_TARGET, " + {schedule:#?} + ############################################### + Lazy deletion weight per key: {weight_per_key} + Lazy deletion keys per block: {key_budget} + "); + #[block] + {} + + Err(BenchmarkError::Skip) + } + + impl_benchmark_test_suite!( + Contracts, + crate::tests::ExtBuilder::default().build(), + crate::tests::Test, + ); +} diff --git a/pallets/contracts/src/benchmarking/sandbox.rs b/pallets/contracts/src/benchmarking/sandbox.rs new file mode 100644 index 00000000..1bcf0c40 --- /dev/null +++ b/pallets/contracts/src/benchmarking/sandbox.rs @@ -0,0 +1,91 @@ +// This file is part of Substrate. + +// 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. + +/// ! For instruction benchmarking we do not instantiate a full contract but merely the +/// ! sandbox to execute the Wasm code. This is because we do not need the full +/// ! environment that provides the seal interface as imported functions. +use super::{code::WasmModule, Config}; +use crate::wasm::{ + AllowDeprecatedInterface, AllowUnstableInterface, Determinism, Environment, LoadedModule, + LoadingMode, WasmBlob, +}; +use sp_core::Get; +use wasmi::{errors::LinkerError, CompilationMode, Func, Linker, StackLimits, Store}; + +/// Minimal execution environment without any imported functions. +pub struct Sandbox { + entry_point: Func, + store: Store<()>, +} + +impl Sandbox { + /// Invoke the `call` function of a contract code and panic on any execution error. + pub fn invoke(&mut self) { + self.entry_point.call(&mut self.store, &[], &mut []).unwrap(); + } +} + +impl From<&WasmModule> for Sandbox { + /// Creates an instance from the supplied module. + /// Sets the execution engine fuel level to `u64::MAX`. + fn from(module: &WasmModule) -> Self { + let contract = LoadedModule::new::( + &module.code, + Determinism::Relaxed, + Some(StackLimits::default()), + LoadingMode::Checked, + CompilationMode::Eager, + ) + .expect("Failed to load Wasm module"); + + let (mut store, _memory, instance) = WasmBlob::::instantiate::( + contract, + (), + &::Schedule::get(), + // We are testing with an empty environment anyways + AllowDeprecatedInterface::No, + ) + .expect("Failed to create benchmarking Sandbox instance"); + + // Set fuel for wasmi execution. + store + .set_fuel(u64::MAX) + .expect("We've set up engine to fuel consuming mode; qed"); + + let entry_point = instance + .start(&mut store) + .unwrap() + .get_export(&store, "call") + .unwrap() + .into_func() + .unwrap(); + Self { entry_point, store } + } +} + +struct EmptyEnv; + +impl Environment<()> for EmptyEnv { + fn define( + _: &mut Store<()>, + _: &mut Linker<()>, + _: AllowUnstableInterface, + _: AllowDeprecatedInterface, + ) -> Result<(), LinkerError> { + Ok(()) + } +} diff --git a/pallets/contracts/src/chain_extension.rs b/pallets/contracts/src/chain_extension.rs new file mode 100644 index 00000000..b9bb451f --- /dev/null +++ b/pallets/contracts/src/chain_extension.rs @@ -0,0 +1,495 @@ +// This file is part of Substrate. + +// 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. + +//! A mechanism for runtime authors to augment the functionality of contracts. +//! +//! The runtime is able to call into any contract and retrieve the result using +//! [`bare_call`](crate::Pallet::bare_call). This already allows customization of runtime +//! behaviour by user generated code (contracts). However, often it is more straightforward +//! to allow the reverse behaviour: The contract calls into the runtime. We call the latter +//! one a "chain extension" because it allows the chain to extend the set of functions that are +//! callable by a contract. +//! +//! In order to create a chain extension the runtime author implements the [`ChainExtension`] +//! trait and declares it in this pallet's [configuration Trait](crate::Config). All types +//! required for this endeavour are defined or re-exported in this module. There is an +//! implementation on `()` which can be used to signal that no chain extension is available. +//! +//! # Using multiple chain extensions +//! +//! Often there is a need for having multiple chain extensions. This is often the case when +//! some generally useful off-the-shelf extensions should be included. To have multiple chain +//! extensions they can be put into a tuple which is then passed to [`Config::ChainExtension`] like +//! this `type Extensions = (ExtensionA, ExtensionB)`. +//! +//! However, only extensions implementing [`RegisteredChainExtension`] can be put into a tuple. +//! This is because the [`RegisteredChainExtension::ID`] is used to decide which of those extensions +//! should be used when the contract calls a chain extensions. Extensions which are generally +//! useful should claim their `ID` with [the registry](https://github.com/paritytech/chainextension-registry) +//! so that no collisions with other vendors will occur. +//! +//! **Chain specific extensions must use the reserved `ID = 0` so that they can't be registered with +//! the registry.** +//! +//! # Security +//! +//! The chain author alone is responsible for the security of the chain extension. +//! This includes avoiding the exposure of exploitable functions and charging the +//! appropriate amount of weight. In order to do so benchmarks must be written and the +//! [`charge_weight`](Environment::charge_weight) function must be called **before** +//! carrying out any action that causes the consumption of the chargeable weight. +//! It cannot be overstated how delicate of a process the creation of a chain extension +//! is. Check whether using [`bare_call`](crate::Pallet::bare_call) suffices for the +//! use case at hand. +//! +//! # Benchmarking +//! +//! The builtin contract callable functions that pallet-contracts provides all have +//! benchmarks that determine the correct weight that an invocation of these functions +//! induces. In order to be able to charge the correct weight for the functions defined +//! by a chain extension benchmarks must be written, too. In the near future this crate +//! will provide the means for easier creation of those specialized benchmarks. +//! +//! # Example +//! +//! The ink-examples repository maintains an +//! [end-to-end example](https://github.com/paritytech/ink-examples/tree/main/rand-extension) +//! on how to use a chain extension in order to provide new features to ink! contracts. + +use crate::{ + wasm::{Runtime, RuntimeCosts}, + Error, +}; +use alloc::vec::Vec; +use codec::{Decode, MaxEncodedLen}; +use core::marker::PhantomData; +use frame_support::weights::Weight; +use sp_runtime::DispatchError; + +pub use crate::{exec::Ext, gas::ChargedAmount, storage::meter::Diff, Config}; +pub use frame_system::Config as SysConfig; +pub use pallet_contracts_uapi::ReturnFlags; + +/// Result that returns a [`DispatchError`] on error. +pub type Result = core::result::Result; + +/// A trait used to extend the set of contract callable functions. +/// +/// In order to create a custom chain extension this trait must be implemented and supplied +/// to the pallet contracts configuration trait as the associated type of the same name. +/// Consult the [module documentation](self) for a general explanation of chain extensions. +/// +/// # Lifetime +/// +/// The extension will be [`Default`] initialized at the beginning of each call +/// (**not** per call stack) and dropped afterwards. Hence any value held inside the extension +/// can be used as a per-call scratch buffer. +pub trait ChainExtension { + /// Call the chain extension logic. + /// + /// This is the only function that needs to be implemented in order to write a + /// chain extensions. It is called whenever a contract calls the `seal_call_chain_extension` + /// imported wasm function. + /// + /// # Parameters + /// - `env`: Access to the remaining arguments and the execution environment. + /// + /// # Return + /// + /// In case of `Err` the contract execution is immediately suspended and the passed error + /// is returned to the caller. Otherwise the value of [`RetVal`] determines the exit + /// behaviour. + /// + /// # Note + /// + /// The [`Self::call`] can be invoked within a read-only context, where any state-changing calls + /// are disallowed. This information can be obtained using `env.ext().is_read_only()`. It is + /// crucial for the implementer to handle this scenario appropriately. + fn call>(&mut self, env: Environment) -> Result; + + /// Determines whether chain extensions are enabled for this chain. + /// + /// The default implementation returns `true`. Therefore it is not necessary to overwrite + /// this function when implementing a chain extension. In case of `false` the deployment of + /// a contract that references `seal_call_chain_extension` will be denied and calling this + /// function will return [`NoChainExtension`](Error::NoChainExtension) without first calling + /// into [`call`](Self::call). + fn enabled() -> bool { + true + } +} + +/// A [`ChainExtension`] that can be composed with other extensions using a tuple. +/// +/// An extension that implements this trait can be put in a tuple in order to have multiple +/// extensions available. The tuple implementation routes requests based on the first two +/// most significant bytes of the `id` passed to `call`. +/// +/// If this extensions is to be used by multiple runtimes consider +/// [registering it](https://github.com/paritytech/chainextension-registry) to ensure that there +/// are no collisions with other vendors. +/// +/// # Note +/// +/// Currently, we support tuples of up to ten registered chain extensions. If more chain extensions +/// are needed consider opening an issue. +pub trait RegisteredChainExtension: ChainExtension { + /// The extensions globally unique identifier. + const ID: u16; +} + +#[impl_trait_for_tuples::impl_for_tuples(10)] +#[tuple_types_custom_trait_bound(RegisteredChainExtension)] +impl ChainExtension for Tuple { + fn call>(&mut self, mut env: Environment) -> Result { + for_tuples!( + #( + if (Tuple::ID == env.ext_id()) && Tuple::enabled() { + return Tuple.call(env); + } + )* + ); + Err(Error::::NoChainExtension.into()) + } + + fn enabled() -> bool { + for_tuples!( + #( + if Tuple::enabled() { + return true; + } + )* + ); + false + } +} + +/// Determines the exit behaviour and return value of a chain extension. +pub enum RetVal { + /// The chain extensions returns the supplied value to its calling contract. + Converging(u32), + /// The control does **not** return to the calling contract. + /// + /// Use this to stop the execution of the contract when the chain extension returns. + /// The semantic is the same as for calling `seal_return`: The control returns to + /// the caller of the currently executing contract yielding the supplied buffer and + /// flags. + Diverging { flags: ReturnFlags, data: Vec }, +} + +/// Grants the chain extension access to its parameters and execution environment. +/// +/// It uses [typestate programming](https://docs.rust-embedded.org/book/static-guarantees/typestate-programming.html) +/// to enforce the correct usage of the parameters passed to the chain extension. +pub struct Environment<'a, 'b, E: Ext, S: State> { + /// The actual data of this type. + inner: Inner<'a, 'b, E>, + /// `S` is only used in the type system but never as value. + phantom: PhantomData, +} + +/// Functions that are available in every state of this type. +impl<'a, 'b, E: Ext, S: State> Environment<'a, 'b, E, S> { + /// The function id within the `id` passed by a contract. + /// + /// It returns the two least significant bytes of the `id` passed by a contract as the other + /// two bytes represent the chain extension itself (the code which is calling this function). + pub fn func_id(&self) -> u16 { + (self.inner.id & 0x0000FFFF) as u16 + } + + /// The chain extension id within the `id` passed by a contract. + /// + /// It returns the two most significant bytes of the `id` passed by a contract which represent + /// the chain extension itself (the code which is calling this function). + pub fn ext_id(&self) -> u16 { + (self.inner.id >> 16) as u16 + } + + /// Charge the passed `amount` of weight from the overall limit. + /// + /// It returns `Ok` when there the remaining weight budget is larger than the passed + /// `weight`. It returns `Err` otherwise. In this case the chain extension should + /// abort the execution and pass through the error. + /// + /// The returned value can be used to with [`Self::adjust_weight`]. Other than that + /// it has no purpose. + /// + /// # Note + /// + /// Weight is synonymous with gas in substrate. + pub fn charge_weight(&mut self, amount: Weight) -> Result { + self.inner.runtime.charge_gas(RuntimeCosts::ChainExtension(amount)) + } + + /// Adjust a previously charged amount down to its actual amount. + /// + /// This is when a maximum a priori amount was charged and then should be partially + /// refunded to match the actual amount. + pub fn adjust_weight(&mut self, charged: ChargedAmount, actual_weight: Weight) { + self.inner + .runtime + .adjust_gas(charged, RuntimeCosts::ChainExtension(actual_weight)) + } + + /// Grants access to the execution environment of the current contract call. + /// + /// Consult the functions on the returned type before re-implementing those functions. + pub fn ext(&mut self) -> &mut E { + self.inner.runtime.ext() + } +} + +/// Functions that are only available in the initial state of this type. +/// +/// Those are the functions that determine how the arguments to the chain extensions +/// should be consumed. +impl<'a, 'b, E: Ext> Environment<'a, 'b, E, InitState> { + /// Creates a new environment for consumption by a chain extension. + /// + /// It is only available to this crate because only the wasm runtime module needs to + /// ever create this type. Chain extensions merely consume it. + pub(crate) fn new( + runtime: &'a mut Runtime<'b, E>, + memory: &'a mut [u8], + id: u32, + input_ptr: u32, + input_len: u32, + output_ptr: u32, + output_len_ptr: u32, + ) -> Self { + Environment { + inner: Inner { runtime, memory, id, input_ptr, input_len, output_ptr, output_len_ptr }, + phantom: PhantomData, + } + } + + /// Use all arguments as integer values. + pub fn only_in(self) -> Environment<'a, 'b, E, OnlyInState> { + Environment { inner: self.inner, phantom: PhantomData } + } + + /// Use input arguments as integer and output arguments as pointer to a buffer. + pub fn prim_in_buf_out(self) -> Environment<'a, 'b, E, PrimInBufOutState> { + Environment { inner: self.inner, phantom: PhantomData } + } + + /// Use input and output arguments as pointers to a buffer. + pub fn buf_in_buf_out(self) -> Environment<'a, 'b, E, BufInBufOutState> { + Environment { inner: self.inner, phantom: PhantomData } + } +} + +/// Functions to use the input arguments as integers. +impl<'a, 'b, E: Ext, S: PrimIn> Environment<'a, 'b, E, S> { + /// The `input_ptr` argument. + pub fn val0(&self) -> u32 { + self.inner.input_ptr + } + + /// The `input_len` argument. + pub fn val1(&self) -> u32 { + self.inner.input_len + } +} + +/// Functions to use the output arguments as integers. +impl<'a, 'b, E: Ext, S: PrimOut> Environment<'a, 'b, E, S> { + /// The `output_ptr` argument. + pub fn val2(&self) -> u32 { + self.inner.output_ptr + } + + /// The `output_len_ptr` argument. + pub fn val3(&self) -> u32 { + self.inner.output_len_ptr + } +} + +/// Functions to use the input arguments as pointer to a buffer. +impl<'a, 'b, E: Ext, S: BufIn> Environment<'a, 'b, E, S> { + /// Reads `min(max_len, in_len)` from contract memory. + /// + /// This does **not** charge any weight. The caller must make sure that the an + /// appropriate amount of weight is charged **before** reading from contract memory. + /// The reason for that is that usually the costs for reading data and processing + /// said data cannot be separated in a benchmark. Therefore a chain extension would + /// charge the overall costs either using `max_len` (worst case approximation) or using + /// [`in_len()`](Self::in_len). + pub fn read(&self, max_len: u32) -> Result> { + self.inner.runtime.read_sandbox_memory( + self.inner.memory, + self.inner.input_ptr, + self.inner.input_len.min(max_len), + ) + } + + /// Reads `min(buffer.len(), in_len) from contract memory. + /// + /// This takes a mutable pointer to a buffer fills it with data and shrinks it to + /// the size of the actual data. Apart from supporting pre-allocated buffers it is + /// equivalent to to [`read()`](Self::read). + pub fn read_into(&self, buffer: &mut &mut [u8]) -> Result<()> { + let len = buffer.len(); + let sliced = { + let buffer = core::mem::take(buffer); + &mut buffer[..len.min(self.inner.input_len as usize)] + }; + self.inner.runtime.read_sandbox_memory_into_buf( + self.inner.memory, + self.inner.input_ptr, + sliced, + )?; + *buffer = sliced; + Ok(()) + } + + /// Reads and decodes a type with a size fixed at compile time from contract memory. + /// + /// This function is secure and recommended for all input types of fixed size + /// as long as the cost of reading the memory is included in the overall already charged + /// weight of the chain extension. This should usually be the case when fixed input types + /// are used. + pub fn read_as(&mut self) -> Result { + self.inner + .runtime + .read_sandbox_memory_as(self.inner.memory, self.inner.input_ptr) + } + + /// Reads and decodes a type with a dynamic size from contract memory. + /// + /// Make sure to include `len` in your weight calculations. + pub fn read_as_unbounded(&mut self, len: u32) -> Result { + self.inner.runtime.read_sandbox_memory_as_unbounded( + self.inner.memory, + self.inner.input_ptr, + len, + ) + } + + /// The length of the input as passed in as `input_len`. + /// + /// A chain extension would use this value to calculate the dynamic part of its + /// weight. For example a chain extension that calculates the hash of some passed in + /// bytes would use `in_len` to charge the costs of hashing that amount of bytes. + /// This also subsumes the act of copying those bytes as a benchmarks measures both. + pub fn in_len(&self) -> u32 { + self.inner.input_len + } +} + +/// Functions to use the output arguments as pointer to a buffer. +impl<'a, 'b, E: Ext, S: BufOut> Environment<'a, 'b, E, S> { + /// Write the supplied buffer to contract memory. + /// + /// If the contract supplied buffer is smaller than the passed `buffer` an `Err` is returned. + /// If `allow_skip` is set to true the contract is allowed to skip the copying of the buffer + /// by supplying the guard value of `pallet-contracts::SENTINEL` as `out_ptr`. The + /// `weight_per_byte` is only charged when the write actually happens and is not skipped or + /// failed due to a too small output buffer. + pub fn write( + &mut self, + buffer: &[u8], + allow_skip: bool, + weight_per_byte: Option, + ) -> Result<()> { + self.inner.runtime.write_sandbox_output( + self.inner.memory, + self.inner.output_ptr, + self.inner.output_len_ptr, + buffer, + allow_skip, + |len| { + weight_per_byte.map(|w| RuntimeCosts::ChainExtension(w.saturating_mul(len.into()))) + }, + ) + } +} + +/// The actual data of an `Environment`. +/// +/// All data is put into this struct to easily pass it around as part of the typestate +/// pattern. Also it creates the opportunity to box this struct in the future in case it +/// gets too large. +struct Inner<'a, 'b, E: Ext> { + /// The runtime contains all necessary functions to interact with the running contract. + runtime: &'a mut Runtime<'b, E>, + /// Reference to the contracts memory. + memory: &'a mut [u8], + /// Verbatim argument passed to `seal_call_chain_extension`. + id: u32, + /// Verbatim argument passed to `seal_call_chain_extension`. + input_ptr: u32, + /// Verbatim argument passed to `seal_call_chain_extension`. + input_len: u32, + /// Verbatim argument passed to `seal_call_chain_extension`. + output_ptr: u32, + /// Verbatim argument passed to `seal_call_chain_extension`. + output_len_ptr: u32, +} + +/// Any state of an [`Environment`] implements this trait. +/// See [typestate programming](https://docs.rust-embedded.org/book/static-guarantees/typestate-programming.html). +pub trait State: sealed::Sealed {} + +/// A state that uses primitive inputs. +pub trait PrimIn: State {} + +/// A state that uses primitive outputs. +pub trait PrimOut: State {} + +/// A state that uses a buffer as input. +pub trait BufIn: State {} + +/// A state that uses a buffer as output. +pub trait BufOut: State {} + +/// The initial state of an [`Environment`]. +pub enum InitState {} + +/// A state that uses all arguments as primitive inputs. +pub enum OnlyInState {} + +/// A state that uses two arguments as primitive inputs and the other two as buffer output. +pub enum PrimInBufOutState {} + +/// Uses a buffer for input and a buffer for output. +pub enum BufInBufOutState {} + +mod sealed { + use super::*; + + /// Trait to prevent users from implementing `State` for anything else. + pub trait Sealed {} + + impl Sealed for InitState {} + impl Sealed for OnlyInState {} + impl Sealed for PrimInBufOutState {} + impl Sealed for BufInBufOutState {} + + impl State for InitState {} + impl State for OnlyInState {} + impl State for PrimInBufOutState {} + impl State for BufInBufOutState {} + + impl PrimIn for OnlyInState {} + impl PrimOut for OnlyInState {} + impl PrimIn for PrimInBufOutState {} + impl BufOut for PrimInBufOutState {} + impl BufIn for BufInBufOutState {} + impl BufOut for BufInBufOutState {} +} diff --git a/pallets/contracts/src/debug.rs b/pallets/contracts/src/debug.rs new file mode 100644 index 00000000..6cdca7aa --- /dev/null +++ b/pallets/contracts/src/debug.rs @@ -0,0 +1,112 @@ +// This file is part of Substrate. + +// 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. + +pub use crate::{ + exec::{ExecResult, ExportedFunction}, + primitives::ExecReturnValue, +}; +use crate::{Config, LOG_TARGET}; + +/// Umbrella trait for all interfaces that serves for debugging. +pub trait Debugger: Tracing + CallInterceptor {} + +impl Debugger for V where V: Tracing + CallInterceptor {} + +/// Defines methods to capture contract calls, enabling external observers to +/// measure, trace, and react to contract interactions. +pub trait Tracing { + /// The type of [`CallSpan`] that is created by this trait. + type CallSpan: CallSpan; + + /// Creates a new call span to encompass the upcoming contract execution. + /// + /// This method should be invoked just before the execution of a contract and + /// marks the beginning of a traceable span of execution. + /// + /// # Arguments + /// + /// * `contract_address` - The address of the contract that is about to be executed. + /// * `entry_point` - Describes whether the call is the constructor or a regular call. + /// * `input_data` - The raw input data of the call. + fn new_call_span( + contract_address: &T::AccountId, + entry_point: ExportedFunction, + input_data: &[u8], + ) -> Self::CallSpan; +} + +/// Defines a span of execution for a contract call. +pub trait CallSpan { + /// Called just after the execution of a contract. + /// + /// # Arguments + /// + /// * `output` - The raw output of the call. + fn after_call(self, output: &ExecReturnValue); +} + +impl Tracing for () { + type CallSpan = (); + + fn new_call_span( + contract_address: &T::AccountId, + entry_point: ExportedFunction, + input_data: &[u8], + ) { + log::trace!(target: LOG_TARGET, "call {entry_point:?} account: {contract_address:?}, input_data: {input_data:?}") + } +} + +impl CallSpan for () { + fn after_call(self, output: &ExecReturnValue) { + log::trace!(target: LOG_TARGET, "call result {output:?}") + } +} + +/// Provides an interface for intercepting contract calls. +pub trait CallInterceptor { + /// Allows to intercept contract calls and decide whether they should be executed or not. + /// If the call is intercepted, the mocked result of the call is returned. + /// + /// # Arguments + /// + /// * `contract_address` - The address of the contract that is about to be executed. + /// * `entry_point` - Describes whether the call is the constructor or a regular call. + /// * `input_data` - The raw input data of the call. + /// + /// # Expected behavior + /// + /// This method should return: + /// * `Some(ExecResult)` - if the call should be intercepted and the mocked result of the call + /// is returned. + /// * `None` - otherwise, i.e. the call should be executed normally. + fn intercept_call( + contract_address: &T::AccountId, + entry_point: &ExportedFunction, + input_data: &[u8], + ) -> Option; +} + +impl CallInterceptor for () { + fn intercept_call( + _contract_address: &T::AccountId, + _entry_point: &ExportedFunction, + _input_data: &[u8], + ) -> Option { + None + } +} diff --git a/pallets/contracts/src/exec.rs b/pallets/contracts/src/exec.rs new file mode 100644 index 00000000..31e0bf50 --- /dev/null +++ b/pallets/contracts/src/exec.rs @@ -0,0 +1,4284 @@ +// This file is part of Substrate. + +// 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::{ + debug::{CallInterceptor, CallSpan, Tracing}, + gas::GasMeter, + primitives::{ExecReturnValue, StorageDeposit}, + storage::{self, meter::Diff, WriteOutcome}, + transient_storage::TransientStorage, + BalanceOf, CodeHash, CodeInfo, CodeInfoOf, Config, ContractInfo, ContractInfoOf, + DebugBufferVec, Determinism, Error, Event, Nonce, Origin, Pallet as Contracts, Schedule, + LOG_TARGET, +}; +use alloc::vec::Vec; +use core::{fmt::Debug, marker::PhantomData, mem}; +use frame_support::{ + crypto::ecdsa::ECDSAExt, + dispatch::{DispatchResult, DispatchResultWithPostInfo}, + ensure, + storage::{with_transaction, TransactionOutcome}, + traits::{ + fungible::{Inspect, Mutate}, + tokens::{Fortitude, Preservation}, + Contains, OriginTrait, Randomness, Time, + }, + weights::Weight, + Blake2_128Concat, BoundedVec, StorageHasher, +}; +use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin}; +use smallvec::{Array, SmallVec}; +use sp_core::{ + ecdsa::Public as ECDSAPublic, + sr25519::{Public as SR25519Public, Signature as SR25519Signature}, + Get, +}; +use sp_io::{crypto::secp256k1_ecdsa_recover_compressed, hashing::blake2_256}; +use sp_runtime::{ + traits::{Convert, Dispatchable, Zero}, + DispatchError, +}; + +pub type AccountIdOf = ::AccountId; +pub type MomentOf = <::Time as Time>::Moment; +pub type SeedOf = ::Hash; +pub type ExecResult = Result; + +/// A type that represents a topic of an event. At the moment a hash is used. +pub type TopicOf = ::Hash; + +/// Type for variable sized storage key. Used for transparent hashing. +type VarSizedKey = BoundedVec::MaxStorageKeyLen>; + +/// Combined key type for both fixed and variable sized storage keys. +pub enum Key { + /// Variant for fixed sized keys. + Fix([u8; 32]), + /// Variant for variable sized keys. + Var(VarSizedKey), +} + +impl Key { + /// Copies self into a new vec. + pub fn to_vec(&self) -> Vec { + match self { + Key::Fix(v) => v.to_vec(), + Key::Var(v) => v.to_vec(), + } + } + + pub fn hash(&self) -> Vec { + match self { + Key::Fix(v) => blake2_256(v.as_slice()).to_vec(), + Key::Var(v) => Blake2_128Concat::hash(v.as_slice()), + } + } + + pub fn try_from_fix(v: Vec) -> Result> { + <[u8; 32]>::try_from(v).map(Self::Fix) + } + + pub fn try_from_var(v: Vec) -> Result> { + VarSizedKey::::try_from(v).map(Self::Var) + } +} + +/// Origin of the error. +/// +/// Call or instantiate both called into other contracts and pass through errors happening +/// in those to the caller. This enum is for the caller to distinguish whether the error +/// happened during the execution of the callee or in the current execution context. +#[derive(Copy, Clone, PartialEq, Eq, Debug, codec::Decode, codec::Encode)] +pub enum ErrorOrigin { + /// Caller error origin. + /// + /// The error happened in the current execution context rather than in the one + /// of the contract that is called into. + Caller, + /// The error happened during execution of the called contract. + Callee, +} + +/// Error returned by contract execution. +#[derive(Copy, Clone, PartialEq, Eq, Debug, codec::Decode, codec::Encode)] +pub struct ExecError { + /// The reason why the execution failed. + pub error: DispatchError, + /// Origin of the error. + pub origin: ErrorOrigin, +} + +impl> From for ExecError { + fn from(error: T) -> Self { + Self { error: error.into(), origin: ErrorOrigin::Caller } + } +} + +/// An interface that provides access to the external environment in which the +/// smart-contract is executed. +/// +/// This interface is specialized to an account of the executing code, so all +/// operations are implicitly performed on that account. +/// +/// # Note +/// +/// This trait is sealed and cannot be implemented by downstream crates. +pub trait Ext: sealing::Sealed { + type T: Config; + + /// Call (possibly transferring some amount of funds) into the specified account. + /// + /// Returns the code size of the called contract. + fn call( + &mut self, + gas_limit: Weight, + deposit_limit: BalanceOf, + to: AccountIdOf, + value: BalanceOf, + input_data: Vec, + allows_reentry: bool, + read_only: bool, + ) -> Result; + + /// Execute code in the current frame. + /// + /// Returns the code size of the called contract. + fn delegate_call( + &mut self, + code: CodeHash, + input_data: Vec, + ) -> Result; + + /// Instantiate a contract from the given code. + /// + /// Returns the original code size of the called contract. + /// The newly created account will be associated with `code`. `value` specifies the amount of + /// value transferred from the caller to the newly created account. + fn instantiate( + &mut self, + gas_limit: Weight, + deposit_limit: BalanceOf, + code: CodeHash, + value: BalanceOf, + input_data: Vec, + salt: &[u8], + ) -> Result<(AccountIdOf, ExecReturnValue), ExecError>; + + /// Transfer all funds to `beneficiary` and delete the contract. + /// + /// Since this function removes the self contract eagerly, if succeeded, no further actions + /// should be performed on this `Ext` instance. + /// + /// This function will fail if the same contract is present on the contract + /// call stack. + fn terminate(&mut self, beneficiary: &AccountIdOf) -> DispatchResult; + + /// Transfer some amount of funds into the specified account. + fn transfer(&mut self, to: &AccountIdOf, value: BalanceOf) -> DispatchResult; + + /// Returns the storage entry of the executing account by the given `key`. + /// + /// Returns `None` if the `key` wasn't previously set by `set_storage` or + /// was deleted. + fn get_storage(&mut self, key: &Key) -> Option>; + + /// Returns `Some(len)` (in bytes) if a storage item exists at `key`. + /// + /// Returns `None` if the `key` wasn't previously set by `set_storage` or + /// was deleted. + fn get_storage_size(&mut self, key: &Key) -> Option; + + /// Sets the storage entry by the given key to the specified value. If `value` is `None` then + /// the storage entry is deleted. + fn set_storage( + &mut self, + key: &Key, + value: Option>, + take_old: bool, + ) -> Result; + + /// Returns the transient storage entry of the executing account for the given `key`. + /// + /// Returns `None` if the `key` wasn't previously set by `set_transient_storage` or + /// was deleted. + fn get_transient_storage(&self, key: &Key) -> Option>; + + /// Returns `Some(len)` (in bytes) if a transient storage item exists at `key`. + /// + /// Returns `None` if the `key` wasn't previously set by `set_transient_storage` or + /// was deleted. + fn get_transient_storage_size(&self, key: &Key) -> Option; + + /// Sets the transient storage entry for the given key to the specified value. If `value` is + /// `None` then the storage entry is deleted. + fn set_transient_storage( + &mut self, + key: &Key, + value: Option>, + take_old: bool, + ) -> Result; + + /// Returns the caller. + fn caller(&self) -> Origin; + + /// Check if a contract lives at the specified `address`. + fn is_contract(&self, address: &AccountIdOf) -> bool; + + /// Returns the code hash of the contract for the given `address`. + /// + /// Returns `None` if the `address` does not belong to a contract. + fn code_hash(&self, address: &AccountIdOf) -> Option>; + + /// Returns the code hash of the contract being executed. + fn own_code_hash(&mut self) -> &CodeHash; + + /// Check if the caller of the current contract is the origin of the whole call stack. + /// + /// This can be checked with `is_contract(self.caller())` as well. + /// However, this function does not require any storage lookup and therefore uses less weight. + fn caller_is_origin(&self) -> bool; + + /// Check if the caller is origin, and this origin is root. + fn caller_is_root(&self) -> bool; + + /// Returns a reference to the account id of the current contract. + fn address(&self) -> &AccountIdOf; + + /// Returns the balance of the current contract. + /// + /// The `value_transferred` is already added. + fn balance(&self) -> BalanceOf; + + /// Returns the value transferred along with this call. + fn value_transferred(&self) -> BalanceOf; + + /// Returns a reference to the timestamp of the current block + fn now(&self) -> &MomentOf; + + /// Returns the minimum balance that is required for creating an account. + fn minimum_balance(&self) -> BalanceOf; + + /// Returns a random number for the current block with the given subject. + fn random(&self, subject: &[u8]) -> (SeedOf, BlockNumberFor); + + /// Deposit an event with the given topics. + /// + /// There should not be any duplicates in `topics`. + fn deposit_event(&mut self, topics: Vec>, data: Vec); + + /// Returns the current block number. + fn block_number(&self) -> BlockNumberFor; + + /// Returns the maximum allowed size of a storage item. + fn max_value_size(&self) -> u32; + + /// Returns the price for the specified amount of weight. + fn get_weight_price(&self, weight: Weight) -> BalanceOf; + + /// Get a reference to the schedule used by the current call. + fn schedule(&self) -> &Schedule; + + /// Get an immutable reference to the nested gas meter. + fn gas_meter(&self) -> &GasMeter; + + /// Get a mutable reference to the nested gas meter. + fn gas_meter_mut(&mut self) -> &mut GasMeter; + + /// Charges `diff` from the meter. + fn charge_storage(&mut self, diff: &Diff); + + /// Append a string to the debug buffer. + /// + /// It is added as-is without any additional new line. + /// + /// This is a no-op if debug message recording is disabled which is always the case + /// when the code is executing on-chain. + /// + /// Returns `true` if debug message recording is enabled. Otherwise `false` is returned. + fn append_debug_buffer(&mut self, msg: &str) -> bool; + + /// Returns `true` if debug message recording is enabled. Otherwise `false` is returned. + fn debug_buffer_enabled(&self) -> bool; + + /// Call some dispatchable and return the result. + fn call_runtime(&self, call: ::RuntimeCall) -> DispatchResultWithPostInfo; + + /// Recovers ECDSA compressed public key based on signature and message hash. + fn ecdsa_recover(&self, signature: &[u8; 65], message_hash: &[u8; 32]) -> Result<[u8; 33], ()>; + + /// Verify a sr25519 signature. + fn sr25519_verify(&self, signature: &[u8; 64], message: &[u8], pub_key: &[u8; 32]) -> bool; + + /// Returns Ethereum address from the ECDSA compressed public key. + fn ecdsa_to_eth_address(&self, pk: &[u8; 33]) -> Result<[u8; 20], ()>; + + /// Tests sometimes need to modify and inspect the contract info directly. + #[cfg(any(test, feature = "runtime-benchmarks"))] + fn contract_info(&mut self) -> &mut ContractInfo; + + /// Get a mutable reference to the transient storage. + /// Useful in benchmarks when it is sometimes necessary to modify and inspect the transient + /// storage directly. + #[cfg(feature = "runtime-benchmarks")] + fn transient_storage(&mut self) -> &mut TransientStorage; + + /// Sets new code hash for existing contract. + fn set_code_hash(&mut self, hash: CodeHash) -> DispatchResult; + + /// Returns the number of times the currently executing contract exists on the call stack in + /// addition to the calling instance. A value of 0 means no reentrancy. + fn reentrance_count(&self) -> u32; + + /// Returns the number of times the specified contract exists on the call stack. Delegated calls + /// are not calculated as separate entrance. + /// A value of 0 means it does not exist on the call stack. + fn account_reentrance_count(&self, account_id: &AccountIdOf) -> u32; + + /// Returns a nonce that is incremented for every instantiated contract. + fn nonce(&mut self) -> u64; + + /// Increment the reference count of a of a stored code by one. + /// + /// # Errors + /// + /// [`Error::CodeNotFound`] is returned if no stored code found having the specified + /// `code_hash`. + fn increment_refcount(code_hash: CodeHash) -> DispatchResult; + + /// Decrement the reference count of a stored code by one. + /// + /// # Note + /// + /// A contract whose reference count dropped to zero isn't automatically removed. A + /// `remove_code` transaction must be submitted by the original uploader to do so. + fn decrement_refcount(code_hash: CodeHash); + + /// Adds a delegate dependency to [`ContractInfo`]'s `delegate_dependencies` field. + /// + /// This ensures that the delegated contract is not removed while it is still in use. It + /// increases the reference count of the code hash and charges a fraction (see + /// [`Config::CodeHashLockupDepositPercent`]) of the code deposit. + /// + /// # Errors + /// + /// - [`Error::MaxDelegateDependenciesReached`] + /// - [`Error::CannotAddSelfAsDelegateDependency`] + /// - [`Error::DelegateDependencyAlreadyExists`] + fn lock_delegate_dependency(&mut self, code_hash: CodeHash) -> DispatchResult; + + /// Removes a delegate dependency from [`ContractInfo`]'s `delegate_dependencies` field. + /// + /// This is the counterpart of [`Self::lock_delegate_dependency`]. It decreases the reference + /// count and refunds the deposit that was charged by [`Self::lock_delegate_dependency`]. + /// + /// # Errors + /// + /// - [`Error::DelegateDependencyNotFound`] + fn unlock_delegate_dependency(&mut self, code_hash: &CodeHash) -> DispatchResult; + + /// Returns the number of locked delegate dependencies. + /// + /// Note: Requires &mut self to access the contract info. + fn locked_delegate_dependencies_count(&mut self) -> usize; + + /// Check if running in read-only context. + fn is_read_only(&self) -> bool; +} + +/// Describes the different functions that can be exported by an [`Executable`]. +#[derive( + Copy, + Clone, + PartialEq, + Eq, + sp_core::RuntimeDebug, + codec::Decode, + codec::Encode, + codec::MaxEncodedLen, + scale_info::TypeInfo, +)] +pub enum ExportedFunction { + /// The constructor function which is executed on deployment of a contract. + Constructor, + /// The function which is executed when a contract is called. + Call, +} + +/// A trait that represents something that can be executed. +/// +/// In the on-chain environment this would be represented by a wasm module. This trait exists in +/// order to be able to mock the wasm logic for testing. +pub trait Executable: Sized { + /// Load the executable from storage. + /// + /// # Note + /// Charges size base load weight from the gas meter. + fn from_storage( + code_hash: CodeHash, + gas_meter: &mut GasMeter, + ) -> Result; + + /// Execute the specified exported function and return the result. + /// + /// When the specified function is `Constructor` the executable is stored and its + /// refcount incremented. + /// + /// # Note + /// + /// This functions expects to be executed in a storage transaction that rolls back + /// all of its emitted storage changes. + fn execute>( + self, + ext: &mut E, + function: &ExportedFunction, + input_data: Vec, + ) -> ExecResult; + + /// The code info of the executable. + fn code_info(&self) -> &CodeInfo; + + /// The code hash of the executable. + fn code_hash(&self) -> &CodeHash; + + /// Size of the contract code in bytes. + fn code_len(&self) -> u32; + + /// The code does not contain any instructions which could lead to indeterminism. + fn is_deterministic(&self) -> bool; +} + +/// The complete call stack of a contract execution. +/// +/// The call stack is initiated by either a signed origin or one of the contract RPC calls. +/// This type implements `Ext` and by that exposes the business logic of contract execution to +/// the runtime module which interfaces with the contract (the wasm blob) itself. +pub struct Stack<'a, T: Config, E> { + /// The origin that initiated the call stack. It could either be a Signed plain account that + /// holds an account id or Root. + /// + /// # Note + /// + /// Please note that it is possible that the id of a Signed origin belongs to a contract rather + /// than a plain account when being called through one of the contract RPCs where the + /// client can freely choose the origin. This usually makes no sense but is still possible. + origin: Origin, + /// The cost schedule used when charging from the gas meter. + schedule: &'a Schedule, + /// The gas meter where costs are charged to. + gas_meter: &'a mut GasMeter, + /// The storage meter makes sure that the storage deposit limit is obeyed. + storage_meter: &'a mut storage::meter::Meter, + /// The timestamp at the point of call stack instantiation. + timestamp: MomentOf, + /// The block number at the time of call stack instantiation. + block_number: BlockNumberFor, + /// The nonce is cached here when accessed. It is written back when the call stack + /// finishes executing. Please refer to [`Nonce`] to a description of + /// the nonce itself. + nonce: Option, + /// The actual call stack. One entry per nested contract called/instantiated. + /// This does **not** include the [`Self::first_frame`]. + frames: SmallVec, + /// Statically guarantee that each call stack has at least one frame. + first_frame: Frame, + /// A text buffer used to output human readable information. + /// + /// All the bytes added to this field should be valid UTF-8. The buffer has no defined + /// structure and is intended to be shown to users as-is for debugging purposes. + debug_message: Option<&'a mut DebugBufferVec>, + /// The determinism requirement of this call stack. + determinism: Determinism, + /// Transient storage used to store data, which is kept for the duration of a transaction. + transient_storage: TransientStorage, + /// No executable is held by the struct but influences its behaviour. + _phantom: PhantomData, +} + +/// Represents one entry in the call stack. +/// +/// For each nested contract call or instantiate one frame is created. It holds specific +/// information for the said call and caches the in-storage `ContractInfo` data structure. +/// +/// # Note +/// +/// This is an internal data structure. It is exposed to the public for the sole reason +/// of specifying [`Config::CallStack`]. +pub struct Frame { + /// The account id of the executing contract. + account_id: T::AccountId, + /// The cached in-storage data of the contract. + contract_info: CachedContract, + /// The amount of balance transferred by the caller as part of the call. + value_transferred: BalanceOf, + /// Determines whether this is a call or instantiate frame. + entry_point: ExportedFunction, + /// The gas meter capped to the supplied gas limit. + nested_gas: GasMeter, + /// The storage meter for the individual call. + nested_storage: storage::meter::NestedMeter, + /// If `false` the contract enabled its defense against reentrance attacks. + allows_reentry: bool, + /// If `true` subsequent calls cannot modify storage. + read_only: bool, + /// The caller of the currently executing frame which was spawned by `delegate_call`. + delegate_caller: Option>, +} + +/// Used in a delegate call frame arguments in order to override the executable and caller. +struct DelegatedCall { + /// The executable which is run instead of the contracts own `executable`. + executable: E, + /// The caller of the contract. + caller: Origin, +} + +/// Parameter passed in when creating a new `Frame`. +/// +/// It determines whether the new frame is for a call or an instantiate. +enum FrameArgs<'a, T: Config, E> { + Call { + /// The account id of the contract that is to be called. + dest: T::AccountId, + /// If `None` the contract info needs to be reloaded from storage. + cached_info: Option>, + /// This frame was created by `seal_delegate_call` and hence uses different code than + /// what is stored at [`Self::Call::dest`]. Its caller ([`DelegatedCall::caller`]) is the + /// account which called the caller contract + delegated_call: Option>, + }, + Instantiate { + /// The contract or signed origin which instantiates the new contract. + sender: T::AccountId, + /// The nonce that should be used to derive a new trie id for the contract. + nonce: u64, + /// The executable whose `deploy` function is run. + executable: E, + /// A salt used in the contract address derivation of the new contract. + salt: &'a [u8], + /// The input data is used in the contract address derivation of the new contract. + input_data: &'a [u8], + }, +} + +/// Describes the different states of a contract as contained in a `Frame`. +enum CachedContract { + /// The cached contract is up to date with the in-storage value. + Cached(ContractInfo), + /// A recursive call into the same contract did write to the contract info. + /// + /// In this case the cached contract is stale and needs to be reloaded from storage. + Invalidated, + /// The current contract executed `terminate` and removed the contract. + /// + /// In this case a reload is neither allowed nor possible. Please note that recursive + /// calls cannot remove a contract as this is checked and denied. + Terminated, +} + +impl CachedContract { + /// Return `Some(ContractInfo)` if the contract is in cached state. `None` otherwise. + fn into_contract(self) -> Option> { + if let CachedContract::Cached(contract) = self { + Some(contract) + } else { + None + } + } + + /// Return `Some(&mut ContractInfo)` if the contract is in cached state. `None` otherwise. + fn as_contract(&mut self) -> Option<&mut ContractInfo> { + if let CachedContract::Cached(contract) = self { + Some(contract) + } else { + None + } + } +} + +impl Frame { + /// Return the `contract_info` of the current contract. + fn contract_info(&mut self) -> &mut ContractInfo { + self.contract_info.get(&self.account_id) + } + + /// Terminate and return the `contract_info` of the current contract. + /// + /// # Note + /// + /// Under no circumstances the contract is allowed to access the `contract_info` after + /// a call to this function. This would constitute a programming error in the exec module. + fn terminate(&mut self) -> ContractInfo { + self.contract_info.terminate(&self.account_id) + } +} + +/// Extract the contract info after loading it from storage. +/// +/// This assumes that `load` was executed before calling this macro. +macro_rules! get_cached_or_panic_after_load { + ($c:expr) => {{ + if let CachedContract::Cached(contract) = $c { + contract + } else { + panic!( + "It is impossible to remove a contract that is on the call stack;\ + See implementations of terminate;\ + Therefore fetching a contract will never fail while using an account id + that is currently active on the call stack;\ + qed" + ); + } + }}; +} + +/// Same as [`Stack::top_frame`]. +/// +/// We need this access as a macro because sometimes hiding the lifetimes behind +/// a function won't work out. +macro_rules! top_frame { + ($stack:expr) => { + $stack.frames.last().unwrap_or(&$stack.first_frame) + }; +} + +/// Same as [`Stack::top_frame_mut`]. +/// +/// We need this access as a macro because sometimes hiding the lifetimes behind +/// a function won't work out. +macro_rules! top_frame_mut { + ($stack:expr) => { + $stack.frames.last_mut().unwrap_or(&mut $stack.first_frame) + }; +} + +impl CachedContract { + /// Load the `contract_info` from storage if necessary. + fn load(&mut self, account_id: &T::AccountId) { + if let CachedContract::Invalidated = self { + let contract = >::get(&account_id); + if let Some(contract) = contract { + *self = CachedContract::Cached(contract); + } + } + } + + /// Return the cached contract_info. + fn get(&mut self, account_id: &T::AccountId) -> &mut ContractInfo { + self.load(account_id); + get_cached_or_panic_after_load!(self) + } + + /// Terminate and return the contract info. + fn terminate(&mut self, account_id: &T::AccountId) -> ContractInfo { + self.load(account_id); + get_cached_or_panic_after_load!(mem::replace(self, Self::Terminated)) + } +} + +impl<'a, T, E> Stack<'a, T, E> +where + T: Config, + E: Executable, +{ + /// Create and run a new call stack by calling into `dest`. + /// + /// # Note + /// + /// `debug_message` should only ever be set to `Some` when executing as an RPC because + /// it adds allocations and could be abused to drive the runtime into an OOM panic. + /// + /// # Return Value + /// + /// Result<(ExecReturnValue, CodeSize), (ExecError, CodeSize)> + pub fn run_call( + origin: Origin, + dest: T::AccountId, + gas_meter: &'a mut GasMeter, + storage_meter: &'a mut storage::meter::Meter, + schedule: &'a Schedule, + value: BalanceOf, + input_data: Vec, + debug_message: Option<&'a mut DebugBufferVec>, + determinism: Determinism, + ) -> Result { + let (mut stack, executable) = Self::new( + FrameArgs::Call { dest, cached_info: None, delegated_call: None }, + origin, + gas_meter, + storage_meter, + schedule, + value, + debug_message, + determinism, + )?; + stack.run(executable, input_data) + } + + /// Create and run a new call stack by instantiating a new contract. + /// + /// # Note + /// + /// `debug_message` should only ever be set to `Some` when executing as an RPC because + /// it adds allocations and could be abused to drive the runtime into an OOM panic. + /// + /// # Return Value + /// + /// Result<(NewContractAccountId, ExecReturnValue), ExecError)> + pub fn run_instantiate( + origin: T::AccountId, + executable: E, + gas_meter: &'a mut GasMeter, + storage_meter: &'a mut storage::meter::Meter, + schedule: &'a Schedule, + value: BalanceOf, + input_data: Vec, + salt: &[u8], + debug_message: Option<&'a mut DebugBufferVec>, + ) -> Result<(T::AccountId, ExecReturnValue), ExecError> { + let (mut stack, executable) = Self::new( + FrameArgs::Instantiate { + sender: origin.clone(), + nonce: >::get().wrapping_add(1), + executable, + salt, + input_data: input_data.as_ref(), + }, + Origin::from_account_id(origin), + gas_meter, + storage_meter, + schedule, + value, + debug_message, + Determinism::Enforced, + )?; + let account_id = stack.top_frame().account_id.clone(); + stack.run(executable, input_data).map(|ret| (account_id, ret)) + } + + #[cfg(feature = "runtime-benchmarks")] + pub fn bench_new_call( + dest: T::AccountId, + origin: Origin, + gas_meter: &'a mut GasMeter, + storage_meter: &'a mut storage::meter::Meter, + schedule: &'a Schedule, + value: BalanceOf, + debug_message: Option<&'a mut DebugBufferVec>, + determinism: Determinism, + ) -> (Self, E) { + Self::new( + FrameArgs::Call { dest, cached_info: None, delegated_call: None }, + origin, + gas_meter, + storage_meter, + schedule, + value, + debug_message, + determinism, + ) + .unwrap() + } + + /// Create a new call stack. + fn new( + args: FrameArgs, + origin: Origin, + gas_meter: &'a mut GasMeter, + storage_meter: &'a mut storage::meter::Meter, + schedule: &'a Schedule, + value: BalanceOf, + debug_message: Option<&'a mut DebugBufferVec>, + determinism: Determinism, + ) -> Result<(Self, E), ExecError> { + let (first_frame, executable, nonce) = Self::new_frame( + args, + value, + gas_meter, + Weight::zero(), + storage_meter, + BalanceOf::::zero(), + determinism, + false, + )?; + + let stack = Self { + origin, + schedule, + gas_meter, + storage_meter, + timestamp: T::Time::now(), + block_number: >::block_number(), + nonce, + first_frame, + frames: Default::default(), + debug_message, + determinism, + transient_storage: TransientStorage::new(T::MaxTransientStorageSize::get()), + _phantom: Default::default(), + }; + + Ok((stack, executable)) + } + + /// Construct a new frame. + /// + /// This does not take `self` because when constructing the first frame `self` is + /// not initialized, yet. + fn new_frame( + frame_args: FrameArgs, + value_transferred: BalanceOf, + gas_meter: &mut GasMeter, + gas_limit: Weight, + storage_meter: &mut storage::meter::GenericMeter, + deposit_limit: BalanceOf, + determinism: Determinism, + read_only: bool, + ) -> Result<(Frame, E, Option), ExecError> { + let (account_id, contract_info, executable, delegate_caller, entry_point, nonce) = + match frame_args { + FrameArgs::Call { dest, cached_info, delegated_call } => { + let contract = if let Some(contract) = cached_info { + contract + } else { + >::get(&dest).ok_or(>::ContractNotFound)? + }; + + let (executable, delegate_caller) = + if let Some(DelegatedCall { executable, caller }) = delegated_call { + (executable, Some(caller)) + } else { + (E::from_storage(contract.code_hash, gas_meter)?, None) + }; + + (dest, contract, executable, delegate_caller, ExportedFunction::Call, None) + }, + FrameArgs::Instantiate { sender, nonce, executable, salt, input_data } => { + let account_id = Contracts::::contract_address( + &sender, + &executable.code_hash(), + input_data, + salt, + ); + let contract = ContractInfo::new(&account_id, nonce, *executable.code_hash())?; + ( + account_id, + contract, + executable, + None, + ExportedFunction::Constructor, + Some(nonce), + ) + }, + }; + + // `Relaxed` will only be ever set in case of off-chain execution. + // Instantiations are never allowed even when executing off-chain. + if !(executable.is_deterministic() || + (matches!(determinism, Determinism::Relaxed) && + matches!(entry_point, ExportedFunction::Call))) + { + return Err(Error::::Indeterministic.into()) + } + + let frame = Frame { + delegate_caller, + value_transferred, + contract_info: CachedContract::Cached(contract_info), + account_id, + entry_point, + nested_gas: gas_meter.nested(gas_limit), + nested_storage: storage_meter.nested(deposit_limit), + allows_reentry: true, + read_only, + }; + + Ok((frame, executable, nonce)) + } + + /// Create a subsequent nested frame. + fn push_frame( + &mut self, + frame_args: FrameArgs, + value_transferred: BalanceOf, + gas_limit: Weight, + deposit_limit: BalanceOf, + read_only: bool, + ) -> Result { + if self.frames.len() == T::CallStack::size() { + return Err(Error::::MaxCallDepthReached.into()) + } + + // We need to make sure that changes made to the contract info are not discarded. + // See the `in_memory_changes_not_discarded` test for more information. + // We do not store on instantiate because we do not allow to call into a contract + // from its own constructor. + let frame = self.top_frame(); + if let (CachedContract::Cached(contract), ExportedFunction::Call) = + (&frame.contract_info, frame.entry_point) + { + >::insert(frame.account_id.clone(), contract.clone()); + } + + let frame = top_frame_mut!(self); + let nested_gas = &mut frame.nested_gas; + let nested_storage = &mut frame.nested_storage; + let (frame, executable, _) = Self::new_frame( + frame_args, + value_transferred, + nested_gas, + gas_limit, + nested_storage, + deposit_limit, + self.determinism, + read_only, + )?; + self.frames.push(frame); + Ok(executable) + } + + /// Run the current (top) frame. + /// + /// This can be either a call or an instantiate. + fn run(&mut self, executable: E, input_data: Vec) -> Result { + let frame = self.top_frame(); + let entry_point = frame.entry_point; + let delegated_code_hash = + if frame.delegate_caller.is_some() { Some(*executable.code_hash()) } else { None }; + + self.transient_storage.start_transaction(); + + let do_transaction = || { + // We need to charge the storage deposit before the initial transfer so that + // it can create the account in case the initial transfer is < ed. + if entry_point == ExportedFunction::Constructor { + // Root origin can't be used to instantiate a contract, so it is safe to assume that + // if we reached this point the origin has an associated account. + let origin = &self.origin.account_id()?; + let frame = top_frame_mut!(self); + frame.nested_storage.charge_instantiate( + origin, + &frame.account_id, + frame.contract_info.get(&frame.account_id), + executable.code_info(), + )?; + } + + // Every non delegate call or instantiate also optionally transfers the balance. + self.initial_transfer()?; + + let contract_address = &top_frame!(self).account_id; + + let call_span = T::Debug::new_call_span(contract_address, entry_point, &input_data); + + let output = T::Debug::intercept_call(contract_address, &entry_point, &input_data) + .unwrap_or_else(|| { + executable + .execute(self, &entry_point, input_data) + .map_err(|e| ExecError { error: e.error, origin: ErrorOrigin::Callee }) + })?; + + call_span.after_call(&output); + + // Avoid useless work that would be reverted anyways. + if output.did_revert() { + return Ok(output) + } + + // Storage limit is normally enforced as late as possible (when the last frame returns) + // so that the ordering of storage accesses does not matter. + // (However, if a special limit was set for a sub-call, it should be enforced right + // after the sub-call returned. See below for this case of enforcement). + if self.frames.is_empty() { + let frame = &mut self.first_frame; + frame.contract_info.load(&frame.account_id); + let contract = frame.contract_info.as_contract(); + frame.nested_storage.enforce_limit(contract)?; + } + + let frame = self.top_frame(); + let account_id = &frame.account_id.clone(); + match (entry_point, delegated_code_hash) { + (ExportedFunction::Constructor, _) => { + // It is not allowed to terminate a contract inside its constructor. + if matches!(frame.contract_info, CachedContract::Terminated) { + return Err(Error::::TerminatedInConstructor.into()) + } + + // If a special limit was set for the sub-call, we enforce it here. + // This is needed because contract constructor might write to storage. + // The sub-call will be rolled back in case the limit is exhausted. + let frame = self.top_frame_mut(); + let contract = frame.contract_info.as_contract(); + frame.nested_storage.enforce_subcall_limit(contract)?; + + let caller = self.caller().account_id()?.clone(); + + // Deposit an instantiation event. + Contracts::::deposit_event(Event::Instantiated { + deployer: caller, + contract: account_id.clone(), + }); + }, + (ExportedFunction::Call, Some(code_hash)) => { + Contracts::::deposit_event(Event::DelegateCalled { + contract: account_id.clone(), + code_hash, + }); + }, + (ExportedFunction::Call, None) => { + // If a special limit was set for the sub-call, we enforce it here. + // The sub-call will be rolled back in case the limit is exhausted. + let frame = self.top_frame_mut(); + let contract = frame.contract_info.as_contract(); + frame.nested_storage.enforce_subcall_limit(contract)?; + + let caller = self.caller(); + Contracts::::deposit_event(Event::Called { + caller: caller.clone(), + contract: account_id.clone(), + }); + }, + } + + Ok(output) + }; + + // All changes performed by the contract are executed under a storage transaction. + // This allows for roll back on error. Changes to the cached contract_info are + // committed or rolled back when popping the frame. + // + // `with_transactional` may return an error caused by a limit in the + // transactional storage depth. + let transaction_outcome = + with_transaction(|| -> TransactionOutcome> { + let output = do_transaction(); + match &output { + Ok(result) if !result.did_revert() => + TransactionOutcome::Commit(Ok((true, output))), + _ => TransactionOutcome::Rollback(Ok((false, output))), + } + }); + + let (success, output) = match transaction_outcome { + // `with_transactional` executed successfully, and we have the expected output. + Ok((success, output)) => (success, output), + // `with_transactional` returned an error, and we propagate that error and note no state + // has changed. + Err(error) => (false, Err(error.into())), + }; + + if success { + self.transient_storage.commit_transaction(); + } else { + self.transient_storage.rollback_transaction(); + } + + self.pop_frame(success); + output + } + + /// Remove the current (top) frame from the stack. + /// + /// This is called after running the current frame. It commits cached values to storage + /// and invalidates all stale references to it that might exist further down the call stack. + fn pop_frame(&mut self, persist: bool) { + // Revert changes to the nonce in case of a failed instantiation. + if !persist && self.top_frame().entry_point == ExportedFunction::Constructor { + self.nonce.as_mut().map(|c| *c = c.wrapping_sub(1)); + } + + // Pop the current frame from the stack and return it in case it needs to interact + // with duplicates that might exist on the stack. + // A `None` means that we are returning from the `first_frame`. + let frame = self.frames.pop(); + + // Both branches do essentially the same with the exception. The difference is that + // the else branch does consume the hardcoded `first_frame`. + if let Some(mut frame) = frame { + let account_id = &frame.account_id; + let prev = top_frame_mut!(self); + + prev.nested_gas.absorb_nested(frame.nested_gas); + + // Only gas counter changes are persisted in case of a failure. + if !persist { + return + } + + // Record the storage meter changes of the nested call into the parent meter. + // If the dropped frame's contract wasn't terminated we update the deposit counter + // in its contract info. The load is necessary to pull it from storage in case + // it was invalidated. + frame.contract_info.load(account_id); + let mut contract = frame.contract_info.into_contract(); + prev.nested_storage.absorb(frame.nested_storage, account_id, contract.as_mut()); + + // In case the contract wasn't terminated we need to persist changes made to it. + if let Some(contract) = contract { + // optimization: Predecessor is the same contract. + // We can just copy the contract into the predecessor without a storage write. + // This is possible when there is no other contract in-between that could + // trigger a rollback. + if prev.account_id == *account_id { + prev.contract_info = CachedContract::Cached(contract); + return + } + + // Predecessor is a different contract: We persist the info and invalidate the first + // stale cache we find. This triggers a reload from storage on next use. We skip(1) + // because that case is already handled by the optimization above. Only the first + // cache needs to be invalidated because that one will invalidate the next cache + // when it is popped from the stack. + >::insert(account_id, contract); + if let Some(c) = self.frames_mut().skip(1).find(|f| f.account_id == *account_id) { + c.contract_info = CachedContract::Invalidated; + } + } + } else { + if let Some((msg, false)) = self.debug_message.as_ref().map(|m| (m, m.is_empty())) { + log::debug!( + target: LOG_TARGET, + "Execution finished with debug buffer: {}", + core::str::from_utf8(msg).unwrap_or(""), + ); + } + self.gas_meter.absorb_nested(mem::take(&mut self.first_frame.nested_gas)); + if !persist { + return + } + let mut contract = self.first_frame.contract_info.as_contract(); + self.storage_meter.absorb( + mem::take(&mut self.first_frame.nested_storage), + &self.first_frame.account_id, + contract.as_deref_mut(), + ); + if let Some(contract) = contract { + >::insert(&self.first_frame.account_id, contract); + } + if let Some(nonce) = self.nonce { + >::set(nonce); + } + } + } + + /// Transfer some funds from `from` to `to`. + fn transfer( + preservation: Preservation, + from: &T::AccountId, + to: &T::AccountId, + value: BalanceOf, + ) -> DispatchResult { + if !value.is_zero() && from != to { + T::Currency::transfer(from, to, value, preservation) + .map_err(|_| Error::::TransferFailed)?; + } + Ok(()) + } + + // The transfer as performed by a call or instantiate. + fn initial_transfer(&self) -> DispatchResult { + let frame = self.top_frame(); + + // If it is a delegate call, then we've already transferred tokens in the + // last non-delegate frame. + if frame.delegate_caller.is_some() { + return Ok(()) + } + + let value = frame.value_transferred; + + // Get the account id from the caller. + // If the caller is root there is no account to transfer from, and therefore we can't take + // any `value` other than 0. + let caller = match self.caller() { + Origin::Signed(caller) => caller, + Origin::Root if value.is_zero() => return Ok(()), + Origin::Root => return DispatchError::RootNotAllowed.into(), + }; + Self::transfer(Preservation::Preserve, &caller, &frame.account_id, value) + } + + /// Reference to the current (top) frame. + fn top_frame(&self) -> &Frame { + top_frame!(self) + } + + /// Mutable reference to the current (top) frame. + fn top_frame_mut(&mut self) -> &mut Frame { + top_frame_mut!(self) + } + + /// Iterator over all frames. + /// + /// The iterator starts with the top frame and ends with the root frame. + fn frames(&self) -> impl Iterator> { + core::iter::once(&self.first_frame).chain(&self.frames).rev() + } + + /// Same as `frames` but with a mutable reference as iterator item. + fn frames_mut(&mut self) -> impl Iterator> { + core::iter::once(&mut self.first_frame).chain(&mut self.frames).rev() + } + + /// Returns whether the current contract is on the stack multiple times. + fn is_recursive(&self) -> bool { + let account_id = &self.top_frame().account_id; + self.frames().skip(1).any(|f| &f.account_id == account_id) + } + + /// Returns whether the specified contract allows to be reentered right now. + fn allows_reentry(&self, id: &AccountIdOf) -> bool { + !self.frames().any(|f| &f.account_id == id && !f.allows_reentry) + } + + /// Increments and returns the next nonce. Pulls it from storage if it isn't in cache. + fn next_nonce(&mut self) -> u64 { + let next = self.nonce().wrapping_add(1); + self.nonce = Some(next); + next + } +} + +impl<'a, T, E> Ext for Stack<'a, T, E> +where + T: Config, + E: Executable, +{ + type T = T; + + fn call( + &mut self, + gas_limit: Weight, + deposit_limit: BalanceOf, + to: T::AccountId, + value: BalanceOf, + input_data: Vec, + allows_reentry: bool, + read_only: bool, + ) -> Result { + // Before pushing the new frame: Protect the caller contract against reentrancy attacks. + // It is important to do this before calling `allows_reentry` so that a direct recursion + // is caught by it. + self.top_frame_mut().allows_reentry = allows_reentry; + + let try_call = || { + if !self.allows_reentry(&to) { + return Err(>::ReentranceDenied.into()) + } + + // We ignore instantiate frames in our search for a cached contract. + // Otherwise it would be possible to recursively call a contract from its own + // constructor: We disallow calling not fully constructed contracts. + let cached_info = self + .frames() + .find(|f| f.entry_point == ExportedFunction::Call && f.account_id == to) + .and_then(|f| match &f.contract_info { + CachedContract::Cached(contract) => Some(contract.clone()), + _ => None, + }); + let executable = self.push_frame( + FrameArgs::Call { dest: to, cached_info, delegated_call: None }, + value, + gas_limit, + deposit_limit, + // Enable read-only access if requested; cannot disable it if already set. + read_only || self.is_read_only(), + )?; + self.run(executable, input_data) + }; + + // We need to make sure to reset `allows_reentry` even on failure. + let result = try_call(); + + // Protection is on a per call basis. + self.top_frame_mut().allows_reentry = true; + + result + } + + fn delegate_call( + &mut self, + code_hash: CodeHash, + input_data: Vec, + ) -> Result { + let executable = E::from_storage(code_hash, self.gas_meter_mut())?; + let top_frame = self.top_frame_mut(); + let contract_info = top_frame.contract_info().clone(); + let account_id = top_frame.account_id.clone(); + let value = top_frame.value_transferred; + let executable = self.push_frame( + FrameArgs::Call { + dest: account_id, + cached_info: Some(contract_info), + delegated_call: Some(DelegatedCall { executable, caller: self.caller().clone() }), + }, + value, + Weight::zero(), + BalanceOf::::zero(), + self.is_read_only(), + )?; + self.run(executable, input_data) + } + + fn instantiate( + &mut self, + gas_limit: Weight, + deposit_limit: BalanceOf, + code_hash: CodeHash, + value: BalanceOf, + input_data: Vec, + salt: &[u8], + ) -> Result<(AccountIdOf, ExecReturnValue), ExecError> { + let executable = E::from_storage(code_hash, self.gas_meter_mut())?; + let nonce = self.next_nonce(); + let executable = self.push_frame( + FrameArgs::Instantiate { + sender: self.top_frame().account_id.clone(), + nonce, + executable, + salt, + input_data: input_data.as_ref(), + }, + value, + gas_limit, + deposit_limit, + self.is_read_only(), + )?; + let account_id = self.top_frame().account_id.clone(); + self.run(executable, input_data).map(|ret| (account_id, ret)) + } + + fn terminate(&mut self, beneficiary: &AccountIdOf) -> DispatchResult { + if self.is_recursive() { + return Err(Error::::TerminatedWhileReentrant.into()) + } + let frame = self.top_frame_mut(); + let info = frame.terminate(); + frame.nested_storage.terminate(&info, beneficiary.clone()); + + info.queue_trie_for_deletion(); + ContractInfoOf::::remove(&frame.account_id); + Self::decrement_refcount(info.code_hash); + + for (code_hash, deposit) in info.delegate_dependencies() { + Self::decrement_refcount(*code_hash); + frame + .nested_storage + .charge_deposit(frame.account_id.clone(), StorageDeposit::Refund(*deposit)); + } + + Contracts::::deposit_event(Event::Terminated { + contract: frame.account_id.clone(), + beneficiary: beneficiary.clone(), + }); + Ok(()) + } + + fn transfer(&mut self, to: &T::AccountId, value: BalanceOf) -> DispatchResult { + Self::transfer(Preservation::Preserve, &self.top_frame().account_id, to, value) + } + + fn get_storage(&mut self, key: &Key) -> Option> { + self.top_frame_mut().contract_info().read(key) + } + + fn get_storage_size(&mut self, key: &Key) -> Option { + self.top_frame_mut().contract_info().size(key.into()) + } + + fn set_storage( + &mut self, + key: &Key, + value: Option>, + take_old: bool, + ) -> Result { + let frame = self.top_frame_mut(); + frame.contract_info.get(&frame.account_id).write( + key.into(), + value, + Some(&mut frame.nested_storage), + take_old, + ) + } + + fn get_transient_storage(&self, key: &Key) -> Option> { + self.transient_storage.read(self.address(), key) + } + + fn get_transient_storage_size(&self, key: &Key) -> Option { + self.transient_storage.read(self.address(), key).map(|value| value.len() as _) + } + + fn set_transient_storage( + &mut self, + key: &Key, + value: Option>, + take_old: bool, + ) -> Result { + let account_id = self.address().clone(); + self.transient_storage.write(&account_id, key, value, take_old) + } + + fn address(&self) -> &T::AccountId { + &self.top_frame().account_id + } + + fn caller(&self) -> Origin { + if let Some(caller) = &self.top_frame().delegate_caller { + caller.clone() + } else { + self.frames() + .nth(1) + .map(|f| Origin::from_account_id(f.account_id.clone())) + .unwrap_or(self.origin.clone()) + } + } + + fn is_contract(&self, address: &T::AccountId) -> bool { + ContractInfoOf::::contains_key(&address) + } + + fn code_hash(&self, address: &T::AccountId) -> Option> { + >::get(&address).map(|contract| contract.code_hash) + } + + fn own_code_hash(&mut self) -> &CodeHash { + &self.top_frame_mut().contract_info().code_hash + } + + fn caller_is_origin(&self) -> bool { + self.origin == self.caller() + } + + fn caller_is_root(&self) -> bool { + // if the caller isn't origin, then it can't be root. + self.caller_is_origin() && self.origin == Origin::Root + } + + fn balance(&self) -> BalanceOf { + T::Currency::reducible_balance( + &self.top_frame().account_id, + Preservation::Preserve, + Fortitude::Polite, + ) + } + + fn value_transferred(&self) -> BalanceOf { + self.top_frame().value_transferred + } + + fn random(&self, subject: &[u8]) -> (SeedOf, BlockNumberFor) { + T::Randomness::random(subject) + } + + fn now(&self) -> &MomentOf { + &self.timestamp + } + + fn minimum_balance(&self) -> BalanceOf { + T::Currency::minimum_balance() + } + + fn deposit_event(&mut self, topics: Vec, data: Vec) { + Contracts::::deposit_indexed_event( + topics, + Event::ContractEmitted { contract: self.top_frame().account_id.clone(), data }, + ); + } + + fn block_number(&self) -> BlockNumberFor { + self.block_number + } + + fn max_value_size(&self) -> u32 { + self.schedule.limits.payload_len + } + + fn get_weight_price(&self, weight: Weight) -> BalanceOf { + T::WeightPrice::convert(weight) + } + + fn schedule(&self) -> &Schedule { + self.schedule + } + + fn gas_meter(&self) -> &GasMeter { + &self.top_frame().nested_gas + } + + fn gas_meter_mut(&mut self) -> &mut GasMeter { + &mut self.top_frame_mut().nested_gas + } + + fn charge_storage(&mut self, diff: &Diff) { + self.top_frame_mut().nested_storage.charge(diff) + } + + fn debug_buffer_enabled(&self) -> bool { + self.debug_message.is_some() + } + + fn append_debug_buffer(&mut self, msg: &str) -> bool { + if let Some(buffer) = &mut self.debug_message { + buffer + .try_extend(&mut msg.bytes()) + .map_err(|_| { + log::debug!( + target: LOG_TARGET, + "Debug buffer (of {} bytes) exhausted!", + DebugBufferVec::::bound(), + ) + }) + .ok(); + true + } else { + false + } + } + + fn call_runtime(&self, call: ::RuntimeCall) -> DispatchResultWithPostInfo { + let mut origin: T::RuntimeOrigin = RawOrigin::Signed(self.address().clone()).into(); + origin.add_filter(T::CallFilter::contains); + call.dispatch(origin) + } + + fn ecdsa_recover(&self, signature: &[u8; 65], message_hash: &[u8; 32]) -> Result<[u8; 33], ()> { + secp256k1_ecdsa_recover_compressed(signature, message_hash).map_err(|_| ()) + } + + fn sr25519_verify(&self, signature: &[u8; 64], message: &[u8], pub_key: &[u8; 32]) -> bool { + sp_io::crypto::sr25519_verify( + &SR25519Signature::from(*signature), + message, + &SR25519Public::from(*pub_key), + ) + } + + fn ecdsa_to_eth_address(&self, pk: &[u8; 33]) -> Result<[u8; 20], ()> { + ECDSAPublic::from(*pk).to_eth_address() + } + + #[cfg(any(test, feature = "runtime-benchmarks"))] + fn contract_info(&mut self) -> &mut ContractInfo { + self.top_frame_mut().contract_info() + } + + #[cfg(feature = "runtime-benchmarks")] + fn transient_storage(&mut self) -> &mut TransientStorage { + &mut self.transient_storage + } + + fn set_code_hash(&mut self, hash: CodeHash) -> DispatchResult { + let frame = top_frame_mut!(self); + if !E::from_storage(hash, &mut frame.nested_gas)?.is_deterministic() { + return Err(>::Indeterministic.into()) + } + + let info = frame.contract_info(); + + let prev_hash = info.code_hash; + info.code_hash = hash; + + let code_info = CodeInfoOf::::get(hash).ok_or(Error::::CodeNotFound)?; + + let old_base_deposit = info.storage_base_deposit(); + let new_base_deposit = info.update_base_deposit(&code_info); + let deposit = StorageDeposit::Charge(new_base_deposit) + .saturating_sub(&StorageDeposit::Charge(old_base_deposit)); + + frame.nested_storage.charge_deposit(frame.account_id.clone(), deposit); + + Self::increment_refcount(hash)?; + Self::decrement_refcount(prev_hash); + Contracts::::deposit_event(Event::ContractCodeUpdated { + contract: frame.account_id.clone(), + new_code_hash: hash, + old_code_hash: prev_hash, + }); + Ok(()) + } + + fn reentrance_count(&self) -> u32 { + let id: &AccountIdOf = &self.top_frame().account_id; + self.account_reentrance_count(id).saturating_sub(1) + } + + fn account_reentrance_count(&self, account_id: &AccountIdOf) -> u32 { + self.frames() + .filter(|f| f.delegate_caller.is_none() && &f.account_id == account_id) + .count() as u32 + } + + fn nonce(&mut self) -> u64 { + if let Some(current) = self.nonce { + current + } else { + let current = >::get(); + self.nonce = Some(current); + current + } + } + + fn increment_refcount(code_hash: CodeHash) -> DispatchResult { + >::mutate(code_hash, |existing| -> Result<(), DispatchError> { + if let Some(info) = existing { + *info.refcount_mut() = info.refcount().saturating_add(1); + Ok(()) + } else { + Err(Error::::CodeNotFound.into()) + } + }) + } + + fn decrement_refcount(code_hash: CodeHash) { + >::mutate(code_hash, |existing| { + if let Some(info) = existing { + *info.refcount_mut() = info.refcount().saturating_sub(1); + } + }); + } + + fn lock_delegate_dependency(&mut self, code_hash: CodeHash) -> DispatchResult { + let frame = self.top_frame_mut(); + let info = frame.contract_info.get(&frame.account_id); + ensure!(code_hash != info.code_hash, Error::::CannotAddSelfAsDelegateDependency); + + let code_info = CodeInfoOf::::get(code_hash).ok_or(Error::::CodeNotFound)?; + let deposit = T::CodeHashLockupDepositPercent::get().mul_ceil(code_info.deposit()); + + info.lock_delegate_dependency(code_hash, deposit)?; + Self::increment_refcount(code_hash)?; + frame + .nested_storage + .charge_deposit(frame.account_id.clone(), StorageDeposit::Charge(deposit)); + Ok(()) + } + + fn unlock_delegate_dependency(&mut self, code_hash: &CodeHash) -> DispatchResult { + let frame = self.top_frame_mut(); + let info = frame.contract_info.get(&frame.account_id); + + let deposit = info.unlock_delegate_dependency(code_hash)?; + Self::decrement_refcount(*code_hash); + frame + .nested_storage + .charge_deposit(frame.account_id.clone(), StorageDeposit::Refund(deposit)); + Ok(()) + } + + fn locked_delegate_dependencies_count(&mut self) -> usize { + self.top_frame_mut().contract_info().delegate_dependencies_count() + } + + fn is_read_only(&self) -> bool { + self.top_frame().read_only + } +} + +mod sealing { + use super::*; + + pub trait Sealed {} + + impl<'a, T: Config, E> Sealed for Stack<'a, T, E> {} + + #[cfg(test)] + impl Sealed for crate::wasm::MockExt {} + + #[cfg(test)] + impl Sealed for &mut crate::wasm::MockExt {} +} + +/// These tests exercise the executive layer. +/// +/// In these tests the VM/loader are mocked. Instead of dealing with wasm bytecode they use simple +/// closures. This allows you to tackle executive logic more thoroughly without writing a +/// wasm VM code. +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + exec::ExportedFunction::*, + gas::GasMeter, + tests::{ + test_utils::{get_balance, place_contract, set_balance}, + ExtBuilder, RuntimeCall, RuntimeEvent as MetaEvent, Test, TestFilter, ALICE, BOB, + CHARLIE, GAS_LIMIT, + }, + Error, + }; + use assert_matches::assert_matches; + use codec::{Decode, Encode}; + use frame_support::{assert_err, assert_ok, parameter_types}; + use frame_system::{EventRecord, Phase}; + use pallet_contracts_uapi::ReturnFlags; + use pretty_assertions::assert_eq; + use sp_runtime::{traits::Hash, DispatchError}; + use std::{cell::RefCell, collections::hash_map::HashMap, rc::Rc}; + + type System = frame_system::Pallet; + + type MockStack<'a> = Stack<'a, Test, MockExecutable>; + + parameter_types! { + static Loader: MockLoader = MockLoader::default(); + } + + fn events() -> Vec> { + System::events() + .into_iter() + .filter_map(|meta| match meta.event { + MetaEvent::Contracts(contract_event) => Some(contract_event), + _ => None, + }) + .collect() + } + + struct MockCtx<'a> { + ext: &'a mut MockStack<'a>, + input_data: Vec, + } + + #[derive(Clone)] + struct MockExecutable { + func: Rc Fn(MockCtx<'a>, &Self) -> ExecResult + 'static>, + func_type: ExportedFunction, + code_hash: CodeHash, + code_info: CodeInfo, + } + + #[derive(Default, Clone)] + pub struct MockLoader { + map: HashMap, MockExecutable>, + counter: u64, + } + + impl MockLoader { + fn code_hashes() -> Vec> { + Loader::get().map.keys().copied().collect() + } + + fn insert( + func_type: ExportedFunction, + f: impl Fn(MockCtx, &MockExecutable) -> ExecResult + 'static, + ) -> CodeHash { + Loader::mutate(|loader| { + // Generate code hashes as monotonically increasing values. + let hash = ::Hash::from_low_u64_be(loader.counter); + loader.counter += 1; + loader.map.insert( + hash, + MockExecutable { + func: Rc::new(f), + func_type, + code_hash: hash, + code_info: CodeInfo::::new(ALICE), + }, + ); + hash + }) + } + } + + impl Executable for MockExecutable { + fn from_storage( + code_hash: CodeHash, + _gas_meter: &mut GasMeter, + ) -> Result { + Loader::mutate(|loader| { + loader.map.get(&code_hash).cloned().ok_or(Error::::CodeNotFound.into()) + }) + } + + fn execute>( + self, + ext: &mut E, + function: &ExportedFunction, + input_data: Vec, + ) -> ExecResult { + if let &Constructor = function { + E::increment_refcount(self.code_hash).unwrap(); + } + // # Safety + // + // We know that we **always** call execute with a `MockStack` in this test. + // + // # Note + // + // The transmute is necessary because `execute` has to be generic over all + // `E: Ext`. However, `MockExecutable` can't be generic over `E` as it would + // constitute a cycle. + let ext = unsafe { mem::transmute(ext) }; + if function == &self.func_type { + (self.func)(MockCtx { ext, input_data }, &self) + } else { + exec_success() + } + } + + fn code_hash(&self) -> &CodeHash { + &self.code_hash + } + + fn code_info(&self) -> &CodeInfo { + &self.code_info + } + + fn code_len(&self) -> u32 { + 0 + } + + fn is_deterministic(&self) -> bool { + true + } + } + + fn exec_success() -> ExecResult { + Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() }) + } + + fn exec_trapped() -> ExecResult { + Err(ExecError { error: >::ContractTrapped.into(), origin: ErrorOrigin::Callee }) + } + + #[test] + fn it_works() { + parameter_types! { + static TestData: Vec = vec![0]; + } + + let value = Default::default(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let exec_ch = MockLoader::insert(Call, |_ctx, _executable| { + TestData::mutate(|data| data.push(1)); + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, exec_ch); + let mut storage_meter = + storage::meter::Meter::new(&Origin::from_account_id(ALICE), Some(0), value) + .unwrap(); + + assert_matches!( + MockStack::run_call( + Origin::from_account_id(ALICE), + BOB, + &mut gas_meter, + &mut storage_meter, + &schedule, + value, + vec![], + None, + Determinism::Enforced, + ), + Ok(_) + ); + }); + + assert_eq!(TestData::get(), vec![0, 1]); + } + + #[test] + fn transfer_works() { + // This test verifies that a contract is able to transfer + // some funds to another account. + let origin = ALICE; + let dest = BOB; + + ExtBuilder::default().build().execute_with(|| { + set_balance(&origin, 100); + set_balance(&dest, 0); + + MockStack::transfer(Preservation::Preserve, &origin, &dest, 55).unwrap(); + + assert_eq!(get_balance(&origin), 45); + assert_eq!(get_balance(&dest), 55); + }); + } + + #[test] + fn correct_transfer_on_call() { + let origin = ALICE; + let dest = BOB; + let value = 55; + + let success_ch = MockLoader::insert(Call, move |ctx, _| { + assert_eq!(ctx.ext.value_transferred(), value); + Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() }) + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&dest, success_ch); + set_balance(&origin, 100); + let balance = get_balance(&dest); + let contract_origin = Origin::from_account_id(origin.clone()); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), value).unwrap(); + + let _ = MockStack::run_call( + contract_origin.clone(), + dest.clone(), + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + value, + vec![], + None, + Determinism::Enforced, + ) + .unwrap(); + + assert_eq!(get_balance(&origin), 100 - value); + assert_eq!(get_balance(&dest), balance + value); + }); + } + + #[test] + fn correct_transfer_on_delegate_call() { + let origin = ALICE; + let dest = BOB; + let value = 35; + + let success_ch = MockLoader::insert(Call, move |ctx, _| { + assert_eq!(ctx.ext.value_transferred(), value); + Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() }) + }); + + let delegate_ch = MockLoader::insert(Call, move |ctx, _| { + assert_eq!(ctx.ext.value_transferred(), value); + let _ = ctx.ext.delegate_call(success_ch, Vec::new())?; + Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() }) + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&dest, delegate_ch); + set_balance(&origin, 100); + let balance = get_balance(&dest); + let contract_origin = Origin::from_account_id(origin.clone()); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 55).unwrap(); + + let _ = MockStack::run_call( + contract_origin.clone(), + dest.clone(), + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + value, + vec![], + None, + Determinism::Enforced, + ) + .unwrap(); + + assert_eq!(get_balance(&origin), 100 - value); + assert_eq!(get_balance(&dest), balance + value); + }); + } + + #[test] + fn changes_are_reverted_on_failing_call() { + // This test verifies that changes are reverted on a call which fails (or equally, returns + // a non-zero status code). + let origin = ALICE; + let dest = BOB; + + let return_ch = MockLoader::insert(Call, |_, _| { + Ok(ExecReturnValue { flags: ReturnFlags::REVERT, data: Vec::new() }) + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&dest, return_ch); + set_balance(&origin, 100); + let balance = get_balance(&dest); + let contract_origin = Origin::from_account_id(origin.clone()); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 55).unwrap(); + + let output = MockStack::run_call( + contract_origin.clone(), + dest.clone(), + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 55, + vec![], + None, + Determinism::Enforced, + ) + .unwrap(); + + assert!(output.did_revert()); + assert_eq!(get_balance(&origin), 100); + assert_eq!(get_balance(&dest), balance); + }); + } + + #[test] + fn balance_too_low() { + // This test verifies that a contract can't send value if it's + // balance is too low. + let origin = ALICE; + let dest = BOB; + + ExtBuilder::default().build().execute_with(|| { + set_balance(&origin, 0); + + let result = MockStack::transfer(Preservation::Preserve, &origin, &dest, 100); + + assert_eq!(result, Err(Error::::TransferFailed.into())); + assert_eq!(get_balance(&origin), 0); + assert_eq!(get_balance(&dest), 0); + }); + } + + #[test] + fn output_is_returned_on_success() { + // Verifies that if a contract returns data with a successful exit status, this data + // is returned from the execution context. + let origin = ALICE; + let dest = BOB; + let return_ch = MockLoader::insert(Call, |_, _| { + Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: vec![1, 2, 3, 4] }) + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + let contract_origin = Origin::from_account_id(origin); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + place_contract(&BOB, return_ch); + + let result = MockStack::run_call( + contract_origin, + dest, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced, + ); + + let output = result.unwrap(); + assert!(!output.did_revert()); + assert_eq!(output.data, vec![1, 2, 3, 4]); + }); + } + + #[test] + fn output_is_returned_on_failure() { + // Verifies that if a contract returns data with a failing exit status, this data + // is returned from the execution context. + let origin = ALICE; + let dest = BOB; + let return_ch = MockLoader::insert(Call, |_, _| { + Ok(ExecReturnValue { flags: ReturnFlags::REVERT, data: vec![1, 2, 3, 4] }) + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, return_ch); + let contract_origin = Origin::from_account_id(origin); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + + let result = MockStack::run_call( + contract_origin, + dest, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced, + ); + + let output = result.unwrap(); + assert!(output.did_revert()); + assert_eq!(output.data, vec![1, 2, 3, 4]); + }); + } + + #[test] + fn input_data_to_call() { + let input_data_ch = MockLoader::insert(Call, |ctx, _| { + assert_eq!(ctx.input_data, &[1, 2, 3, 4]); + exec_success() + }); + + // This one tests passing the input data into a contract via call. + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, input_data_ch); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![1, 2, 3, 4], + None, + Determinism::Enforced, + ); + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn input_data_to_instantiate() { + let input_data_ch = MockLoader::insert(Constructor, |ctx, _| { + assert_eq!(ctx.input_data, &[1, 2, 3, 4]); + exec_success() + }); + + // This one tests passing the input data into a contract via instantiate. + ExtBuilder::default() + .with_code_hashes(MockLoader::code_hashes()) + .build() + .execute_with(|| { + let schedule = ::Schedule::get(); + let min_balance = ::Currency::minimum_balance(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let executable = + MockExecutable::from_storage(input_data_ch, &mut gas_meter).unwrap(); + set_balance(&ALICE, min_balance * 10_000); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, None, min_balance).unwrap(); + + let result = MockStack::run_instantiate( + ALICE, + executable, + &mut gas_meter, + &mut storage_meter, + &schedule, + min_balance, + vec![1, 2, 3, 4], + &[], + None, + ); + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn max_depth() { + // This test verifies that when we reach the maximal depth creation of an + // yet another context fails. + parameter_types! { + static ReachedBottom: bool = false; + } + let value = Default::default(); + let recurse_ch = MockLoader::insert(Call, |ctx, _| { + // Try to call into yourself. + let r = ctx.ext.call( + Weight::zero(), + BalanceOf::::zero(), + BOB, + 0, + vec![], + true, + false, + ); + + ReachedBottom::mutate(|reached_bottom| { + if !*reached_bottom { + // We are first time here, it means we just reached bottom. + // Verify that we've got proper error and set `reached_bottom`. + assert_eq!(r, Err(Error::::MaxCallDepthReached.into())); + *reached_bottom = true; + } else { + // We just unwinding stack here. + assert_matches!(r, Ok(_)); + } + }); + + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + set_balance(&BOB, 1); + place_contract(&BOB, recurse_ch); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), value).unwrap(); + + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + value, + vec![], + None, + Determinism::Enforced, + ); + + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn caller_returns_proper_values() { + let origin = ALICE; + let dest = BOB; + + parameter_types! { + static WitnessedCallerBob: Option> = None; + static WitnessedCallerCharlie: Option> = None; + } + + let bob_ch = MockLoader::insert(Call, |ctx, _| { + // Record the caller for bob. + WitnessedCallerBob::mutate(|caller| { + *caller = Some(ctx.ext.caller().account_id().unwrap().clone()) + }); + + // Call into CHARLIE contract. + assert_matches!( + ctx.ext.call( + Weight::zero(), + BalanceOf::::zero(), + CHARLIE, + 0, + vec![], + true, + false + ), + Ok(_) + ); + exec_success() + }); + let charlie_ch = MockLoader::insert(Call, |ctx, _| { + // Record the caller for charlie. + WitnessedCallerCharlie::mutate(|caller| { + *caller = Some(ctx.ext.caller().account_id().unwrap().clone()) + }); + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&dest, bob_ch); + place_contract(&CHARLIE, charlie_ch); + let contract_origin = Origin::from_account_id(origin.clone()); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + + let result = MockStack::run_call( + contract_origin.clone(), + dest.clone(), + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced, + ); + + assert_matches!(result, Ok(_)); + }); + + assert_eq!(WitnessedCallerBob::get(), Some(origin)); + assert_eq!(WitnessedCallerCharlie::get(), Some(dest)); + } + + #[test] + fn is_contract_returns_proper_values() { + let bob_ch = MockLoader::insert(Call, |ctx, _| { + // Verify that BOB is a contract + assert!(ctx.ext.is_contract(&BOB)); + // Verify that ALICE is not a contract + assert!(!ctx.ext.is_contract(&ALICE)); + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, bob_ch); + + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced, + ); + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn code_hash_returns_proper_values() { + let code_bob = MockLoader::insert(Call, |ctx, _| { + // ALICE is not a contract and hence they do not have a code_hash + assert!(ctx.ext.code_hash(&ALICE).is_none()); + // BOB is a contract and hence it has a code_hash + assert!(ctx.ext.code_hash(&BOB).is_some()); + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, code_bob); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + // ALICE (not contract) -> BOB (contract) + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![0], + None, + Determinism::Enforced, + ); + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn own_code_hash_returns_proper_values() { + let bob_ch = MockLoader::insert(Call, |ctx, _| { + let code_hash = ctx.ext.code_hash(&BOB).unwrap(); + assert_eq!(*ctx.ext.own_code_hash(), code_hash); + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, bob_ch); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + // ALICE (not contract) -> BOB (contract) + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![0], + None, + Determinism::Enforced, + ); + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn caller_is_origin_returns_proper_values() { + let code_charlie = MockLoader::insert(Call, |ctx, _| { + // BOB is not the origin of the stack call + assert!(!ctx.ext.caller_is_origin()); + exec_success() + }); + + let code_bob = MockLoader::insert(Call, |ctx, _| { + // ALICE is the origin of the call stack + assert!(ctx.ext.caller_is_origin()); + // BOB calls CHARLIE + ctx.ext + .call(Weight::zero(), BalanceOf::::zero(), CHARLIE, 0, vec![], true, false) + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, code_bob); + place_contract(&CHARLIE, code_charlie); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + // ALICE -> BOB (caller is origin) -> CHARLIE (caller is not origin) + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![0], + None, + Determinism::Enforced, + ); + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn root_caller_succeeds() { + let code_bob = MockLoader::insert(Call, |ctx, _| { + // root is the origin of the call stack. + assert!(ctx.ext.caller_is_root()); + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, code_bob); + let contract_origin = Origin::Root; + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + // root -> BOB (caller is root) + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![0], + None, + Determinism::Enforced, + ); + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn root_caller_does_not_succeed_when_value_not_zero() { + let code_bob = MockLoader::insert(Call, |ctx, _| { + // root is the origin of the call stack. + assert!(ctx.ext.caller_is_root()); + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, code_bob); + let contract_origin = Origin::Root; + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + // root -> BOB (caller is root) + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 1, + vec![0], + None, + Determinism::Enforced, + ); + assert_matches!(result, Err(_)); + }); + } + + #[test] + fn root_caller_succeeds_with_consecutive_calls() { + let code_charlie = MockLoader::insert(Call, |ctx, _| { + // BOB is not root, even though the origin is root. + assert!(!ctx.ext.caller_is_root()); + exec_success() + }); + + let code_bob = MockLoader::insert(Call, |ctx, _| { + // root is the origin of the call stack. + assert!(ctx.ext.caller_is_root()); + // BOB calls CHARLIE. + ctx.ext + .call(Weight::zero(), BalanceOf::::zero(), CHARLIE, 0, vec![], true, false) + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, code_bob); + place_contract(&CHARLIE, code_charlie); + let contract_origin = Origin::Root; + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + // root -> BOB (caller is root) -> CHARLIE (caller is not root) + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![0], + None, + Determinism::Enforced, + ); + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn address_returns_proper_values() { + let bob_ch = MockLoader::insert(Call, |ctx, _| { + // Verify that address matches BOB. + assert_eq!(*ctx.ext.address(), BOB); + + // Call into charlie contract. + assert_matches!( + ctx.ext.call( + Weight::zero(), + BalanceOf::::zero(), + CHARLIE, + 0, + vec![], + true, + false + ), + Ok(_) + ); + exec_success() + }); + let charlie_ch = MockLoader::insert(Call, |ctx, _| { + assert_eq!(*ctx.ext.address(), CHARLIE); + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, bob_ch); + place_contract(&CHARLIE, charlie_ch); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced, + ); + + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn refuse_instantiate_with_value_below_existential_deposit() { + let dummy_ch = MockLoader::insert(Constructor, |_, _| exec_success()); + + ExtBuilder::default().existential_deposit(15).build().execute_with(|| { + let schedule = ::Schedule::get(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let executable = MockExecutable::from_storage(dummy_ch, &mut gas_meter).unwrap(); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + + assert_matches!( + MockStack::run_instantiate( + ALICE, + executable, + &mut gas_meter, + &mut storage_meter, + &schedule, + 0, // <- zero value + vec![], + &[], + None, + ), + Err(_) + ); + }); + } + + #[test] + fn instantiation_work_with_success_output() { + let dummy_ch = MockLoader::insert(Constructor, |_, _| { + Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: vec![80, 65, 83, 83] }) + }); + + ExtBuilder::default() + .with_code_hashes(MockLoader::code_hashes()) + .existential_deposit(15) + .build() + .execute_with(|| { + let schedule = ::Schedule::get(); + let min_balance = ::Currency::minimum_balance(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let executable = MockExecutable::from_storage(dummy_ch, &mut gas_meter).unwrap(); + set_balance(&ALICE, min_balance * 1000); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = storage::meter::Meter::new( + &contract_origin, + Some(min_balance * 100), + min_balance, + ) + .unwrap(); + + let instantiated_contract_address = assert_matches!( + MockStack::run_instantiate( + ALICE, + executable, + &mut gas_meter, + &mut storage_meter, + &schedule, + min_balance, + vec![], + &[], + None, + ), + Ok((address, ref output)) if output.data == vec![80, 65, 83, 83] => address + ); + + // Check that the newly created account has the expected code hash and + // there are instantiation event. + assert_eq!( + ContractInfo::::load_code_hash(&instantiated_contract_address).unwrap(), + dummy_ch + ); + assert_eq!( + &events(), + &[Event::Instantiated { + deployer: ALICE, + contract: instantiated_contract_address + }] + ); + }); + } + + #[test] + fn instantiation_fails_with_failing_output() { + let dummy_ch = MockLoader::insert(Constructor, |_, _| { + Ok(ExecReturnValue { flags: ReturnFlags::REVERT, data: vec![70, 65, 73, 76] }) + }); + + ExtBuilder::default() + .with_code_hashes(MockLoader::code_hashes()) + .existential_deposit(15) + .build() + .execute_with(|| { + let schedule = ::Schedule::get(); + let min_balance = ::Currency::minimum_balance(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let executable = MockExecutable::from_storage(dummy_ch, &mut gas_meter).unwrap(); + set_balance(&ALICE, min_balance * 1000); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = storage::meter::Meter::new( + &contract_origin, + Some(min_balance * 100), + min_balance, + ) + .unwrap(); + + let instantiated_contract_address = assert_matches!( + MockStack::run_instantiate( + ALICE, + executable, + &mut gas_meter, + &mut storage_meter, + &schedule, + min_balance, + vec![], + &[], + None, + ), + Ok((address, ref output)) if output.data == vec![70, 65, 73, 76] => address + ); + + // Check that the account has not been created. + assert!( + ContractInfo::::load_code_hash(&instantiated_contract_address).is_none() + ); + assert!(events().is_empty()); + }); + } + + #[test] + fn instantiation_from_contract() { + let dummy_ch = MockLoader::insert(Call, |_, _| exec_success()); + let instantiated_contract_address = Rc::new(RefCell::new(None::>)); + let instantiator_ch = MockLoader::insert(Call, { + let instantiated_contract_address = Rc::clone(&instantiated_contract_address); + move |ctx, _| { + // Instantiate a contract and save it's address in `instantiated_contract_address`. + let (address, output) = ctx + .ext + .instantiate( + Weight::zero(), + BalanceOf::::zero(), + dummy_ch, + ::Currency::minimum_balance(), + vec![], + &[48, 49, 50], + ) + .unwrap(); + + *instantiated_contract_address.borrow_mut() = address.into(); + Ok(output) + } + }); + + ExtBuilder::default() + .with_code_hashes(MockLoader::code_hashes()) + .existential_deposit(15) + .build() + .execute_with(|| { + let schedule = ::Schedule::get(); + let min_balance = ::Currency::minimum_balance(); + set_balance(&ALICE, min_balance * 100); + place_contract(&BOB, instantiator_ch); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = storage::meter::Meter::new( + &contract_origin, + Some(min_balance * 10), + min_balance * 10, + ) + .unwrap(); + + assert_matches!( + MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + min_balance * 10, + vec![], + None, + Determinism::Enforced, + ), + Ok(_) + ); + + let instantiated_contract_address = + instantiated_contract_address.borrow().as_ref().unwrap().clone(); + + // Check that the newly created account has the expected code hash and + // there are instantiation event. + assert_eq!( + ContractInfo::::load_code_hash(&instantiated_contract_address).unwrap(), + dummy_ch + ); + assert_eq!( + &events(), + &[ + Event::Instantiated { + deployer: BOB, + contract: instantiated_contract_address + }, + Event::Called { caller: Origin::from_account_id(ALICE), contract: BOB }, + ] + ); + }); + } + + #[test] + fn instantiation_traps() { + let dummy_ch = MockLoader::insert(Constructor, |_, _| Err("It's a trap!".into())); + let instantiator_ch = MockLoader::insert(Call, { + move |ctx, _| { + // Instantiate a contract and save it's address in `instantiated_contract_address`. + assert_matches!( + ctx.ext.instantiate( + Weight::zero(), + BalanceOf::::zero(), + dummy_ch, + ::Currency::minimum_balance(), + vec![], + &[], + ), + Err(ExecError { + error: DispatchError::Other("It's a trap!"), + origin: ErrorOrigin::Callee, + }) + ); + + exec_success() + } + }); + + ExtBuilder::default() + .with_code_hashes(MockLoader::code_hashes()) + .existential_deposit(15) + .build() + .execute_with(|| { + let schedule = ::Schedule::get(); + set_balance(&ALICE, 1000); + set_balance(&BOB, 100); + place_contract(&BOB, instantiator_ch); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(200), 0).unwrap(); + + assert_matches!( + MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced, + ), + Ok(_) + ); + + // The contract wasn't instantiated so we don't expect to see an instantiation + // event here. + assert_eq!( + &events(), + &[Event::Called { caller: Origin::from_account_id(ALICE), contract: BOB },] + ); + }); + } + + #[test] + fn termination_from_instantiate_fails() { + let terminate_ch = MockLoader::insert(Constructor, |ctx, _| { + ctx.ext.terminate(&ALICE).unwrap(); + exec_success() + }); + + ExtBuilder::default() + .with_code_hashes(MockLoader::code_hashes()) + .existential_deposit(15) + .build() + .execute_with(|| { + let schedule = ::Schedule::get(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let executable = + MockExecutable::from_storage(terminate_ch, &mut gas_meter).unwrap(); + set_balance(&ALICE, 10_000); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, None, 100).unwrap(); + + assert_eq!( + MockStack::run_instantiate( + ALICE, + executable, + &mut gas_meter, + &mut storage_meter, + &schedule, + 100, + vec![], + &[], + None, + ), + Err(Error::::TerminatedInConstructor.into()) + ); + + assert_eq!(&events(), &[]); + }); + } + + #[test] + fn in_memory_changes_not_discarded() { + // Call stack: BOB -> CHARLIE (trap) -> BOB' (success) + // This tests verifies some edge case of the contract info cache: + // We change some value in our contract info before calling into a contract + // that calls into ourself. This triggers a case where BOBs contract info + // is written to storage and invalidated by the successful execution of BOB'. + // The trap of CHARLIE reverts the storage changes to BOB. When the root BOB regains + // control it reloads its contract info from storage. We check that changes that + // are made before calling into CHARLIE are not discarded. + let code_bob = MockLoader::insert(Call, |ctx, _| { + if ctx.input_data[0] == 0 { + let info = ctx.ext.contract_info(); + assert_eq!(info.storage_byte_deposit, 0); + info.storage_byte_deposit = 42; + assert_eq!( + ctx.ext.call( + Weight::zero(), + BalanceOf::::zero(), + CHARLIE, + 0, + vec![], + true, + false + ), + exec_trapped() + ); + assert_eq!(ctx.ext.contract_info().storage_byte_deposit, 42); + } + exec_success() + }); + let code_charlie = MockLoader::insert(Call, |ctx, _| { + assert!(ctx + .ext + .call(Weight::zero(), BalanceOf::::zero(), BOB, 0, vec![99], true, false) + .is_ok()); + exec_trapped() + }); + + // This one tests passing the input data into a contract via call. + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, code_bob); + place_contract(&CHARLIE, code_charlie); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![0], + None, + Determinism::Enforced, + ); + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn recursive_call_during_constructor_fails() { + let code = MockLoader::insert(Constructor, |ctx, _| { + assert_matches!( + ctx.ext.call(Weight::zero(), BalanceOf::::zero(), ctx.ext.address().clone(), 0, vec![], true, false), + Err(ExecError{error, ..}) if error == >::ContractNotFound.into() + ); + exec_success() + }); + + // This one tests passing the input data into a contract via instantiate. + ExtBuilder::default() + .with_code_hashes(MockLoader::code_hashes()) + .build() + .execute_with(|| { + let schedule = ::Schedule::get(); + let min_balance = ::Currency::minimum_balance(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let executable = MockExecutable::from_storage(code, &mut gas_meter).unwrap(); + set_balance(&ALICE, min_balance * 10_000); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, None, min_balance).unwrap(); + + let result = MockStack::run_instantiate( + ALICE, + executable, + &mut gas_meter, + &mut storage_meter, + &schedule, + min_balance, + vec![], + &[], + None, + ); + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn printing_works() { + let code_hash = MockLoader::insert(Call, |ctx, _| { + ctx.ext.append_debug_buffer("This is a test"); + ctx.ext.append_debug_buffer("More text"); + exec_success() + }); + + let mut debug_buffer = DebugBufferVec::::try_from(Vec::new()).unwrap(); + + ExtBuilder::default().build().execute_with(|| { + let min_balance = ::Currency::minimum_balance(); + let schedule = ::Schedule::get(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + set_balance(&ALICE, min_balance * 10); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + MockStack::run_call( + contract_origin, + BOB, + &mut gas_meter, + &mut storage_meter, + &schedule, + 0, + vec![], + Some(&mut debug_buffer), + Determinism::Enforced, + ) + .unwrap(); + }); + + assert_eq!(&String::from_utf8(debug_buffer.to_vec()).unwrap(), "This is a testMore text"); + } + + #[test] + fn printing_works_on_fail() { + let code_hash = MockLoader::insert(Call, |ctx, _| { + ctx.ext.append_debug_buffer("This is a test"); + ctx.ext.append_debug_buffer("More text"); + exec_trapped() + }); + + let mut debug_buffer = DebugBufferVec::::try_from(Vec::new()).unwrap(); + + ExtBuilder::default().build().execute_with(|| { + let min_balance = ::Currency::minimum_balance(); + let schedule = ::Schedule::get(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + set_balance(&ALICE, min_balance * 10); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + let result = MockStack::run_call( + contract_origin, + BOB, + &mut gas_meter, + &mut storage_meter, + &schedule, + 0, + vec![], + Some(&mut debug_buffer), + Determinism::Enforced, + ); + assert!(result.is_err()); + }); + + assert_eq!(&String::from_utf8(debug_buffer.to_vec()).unwrap(), "This is a testMore text"); + } + + #[test] + fn debug_buffer_is_limited() { + let code_hash = MockLoader::insert(Call, move |ctx, _| { + ctx.ext.append_debug_buffer("overflowing bytes"); + exec_success() + }); + + // Pre-fill the buffer almost up to its limit, leaving not enough space to the message + let debug_buf_before = + DebugBufferVec::::try_from(vec![0u8; DebugBufferVec::::bound() - 5]) + .unwrap(); + let mut debug_buf_after = debug_buf_before.clone(); + + ExtBuilder::default().build().execute_with(|| { + let schedule: Schedule = ::Schedule::get(); + let min_balance = ::Currency::minimum_balance(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + set_balance(&ALICE, min_balance * 10); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + MockStack::run_call( + contract_origin, + BOB, + &mut gas_meter, + &mut storage_meter, + &schedule, + 0, + vec![], + Some(&mut debug_buf_after), + Determinism::Enforced, + ) + .unwrap(); + assert_eq!(debug_buf_before, debug_buf_after); + }); + } + + #[test] + fn call_reentry_direct_recursion() { + // call the contract passed as input with disabled reentry + let code_bob = MockLoader::insert(Call, |ctx, _| { + let dest = Decode::decode(&mut ctx.input_data.as_ref()).unwrap(); + ctx.ext + .call(Weight::zero(), BalanceOf::::zero(), dest, 0, vec![], false, false) + }); + + let code_charlie = MockLoader::insert(Call, |_, _| exec_success()); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, code_bob); + place_contract(&CHARLIE, code_charlie); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + + // Calling another contract should succeed + assert_ok!(MockStack::run_call( + contract_origin.clone(), + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + CHARLIE.encode(), + None, + Determinism::Enforced + )); + + // Calling into oneself fails + assert_err!( + MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + BOB.encode(), + None, + Determinism::Enforced + ) + .map_err(|e| e.error), + >::ReentranceDenied, + ); + }); + } + + #[test] + fn call_deny_reentry() { + let code_bob = MockLoader::insert(Call, |ctx, _| { + if ctx.input_data[0] == 0 { + ctx.ext.call( + Weight::zero(), + BalanceOf::::zero(), + CHARLIE, + 0, + vec![], + false, + false, + ) + } else { + exec_success() + } + }); + + // call BOB with input set to '1' + let code_charlie = MockLoader::insert(Call, |ctx, _| { + ctx.ext + .call(Weight::zero(), BalanceOf::::zero(), BOB, 0, vec![1], true, false) + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, code_bob); + place_contract(&CHARLIE, code_charlie); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + + // BOB -> CHARLIE -> BOB fails as BOB denies reentry. + assert_err!( + MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![0], + None, + Determinism::Enforced + ) + .map_err(|e| e.error), + >::ReentranceDenied, + ); + }); + } + + #[test] + fn call_runtime_works() { + let code_hash = MockLoader::insert(Call, |ctx, _| { + let call = RuntimeCall::System(frame_system::Call::remark_with_event { + remark: b"Hello World".to_vec(), + }); + ctx.ext.call_runtime(call).unwrap(); + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let min_balance = ::Currency::minimum_balance(); + let schedule = ::Schedule::get(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + set_balance(&ALICE, min_balance * 10); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + System::reset_events(); + MockStack::run_call( + contract_origin, + BOB, + &mut gas_meter, + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced, + ) + .unwrap(); + + let remark_hash = ::Hashing::hash(b"Hello World"); + assert_eq!( + System::events(), + vec![ + EventRecord { + phase: Phase::Initialization, + event: MetaEvent::System(frame_system::Event::Remarked { + sender: BOB, + hash: remark_hash + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: MetaEvent::Contracts(crate::Event::Called { + caller: Origin::from_account_id(ALICE), + contract: BOB, + }), + topics: vec![], + }, + ] + ); + }); + } + + #[test] + fn call_runtime_filter() { + let code_hash = MockLoader::insert(Call, |ctx, _| { + use frame_system::Call as SysCall; + use pallet_balances::Call as BalanceCall; + use pallet_utility::Call as UtilCall; + + // remark should still be allowed + let allowed_call = + RuntimeCall::System(SysCall::remark_with_event { remark: b"Hello".to_vec() }); + + // transfers are disallowed by the `TestFiler` (see below) + let forbidden_call = RuntimeCall::Balances(BalanceCall::transfer_allow_death { + dest: CHARLIE, + value: 22, + }); + + // simple cases: direct call + assert_err!( + ctx.ext.call_runtime(forbidden_call.clone()), + frame_system::Error::::CallFiltered + ); + + // as part of a patch: return is OK (but it interrupted the batch) + assert_ok!(ctx.ext.call_runtime(RuntimeCall::Utility(UtilCall::batch { + calls: vec![allowed_call.clone(), forbidden_call, allowed_call] + })),); + + // the transfer wasn't performed + assert_eq!(get_balance(&CHARLIE), 0); + + exec_success() + }); + + TestFilter::set_filter(|call| match call { + RuntimeCall::Balances(pallet_balances::Call::transfer_allow_death { .. }) => false, + _ => true, + }); + + ExtBuilder::default().build().execute_with(|| { + let min_balance = ::Currency::minimum_balance(); + let schedule = ::Schedule::get(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + set_balance(&ALICE, min_balance * 10); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + System::reset_events(); + MockStack::run_call( + contract_origin, + BOB, + &mut gas_meter, + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced, + ) + .unwrap(); + + let remark_hash = ::Hashing::hash(b"Hello"); + assert_eq!( + System::events(), + vec![ + EventRecord { + phase: Phase::Initialization, + event: MetaEvent::System(frame_system::Event::Remarked { + sender: BOB, + hash: remark_hash + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: MetaEvent::Utility(pallet_utility::Event::ItemCompleted), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: MetaEvent::Utility(pallet_utility::Event::BatchInterrupted { + index: 1, + error: frame_system::Error::::CallFiltered.into() + },), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: MetaEvent::Contracts(crate::Event::Called { + caller: Origin::from_account_id(ALICE), + contract: BOB, + }), + topics: vec![], + }, + ] + ); + }); + } + + #[test] + fn nonce() { + let fail_code = MockLoader::insert(Constructor, |_, _| exec_trapped()); + let success_code = MockLoader::insert(Constructor, |_, _| exec_success()); + let succ_fail_code = MockLoader::insert(Constructor, move |ctx, _| { + ctx.ext + .instantiate( + Weight::zero(), + BalanceOf::::zero(), + fail_code, + ctx.ext.minimum_balance() * 100, + vec![], + &[], + ) + .ok(); + exec_success() + }); + let succ_succ_code = MockLoader::insert(Constructor, move |ctx, _| { + let (account_id, _) = ctx + .ext + .instantiate( + Weight::zero(), + BalanceOf::::zero(), + success_code, + ctx.ext.minimum_balance() * 100, + vec![], + &[], + ) + .unwrap(); + + // a plain call should not influence the account counter + ctx.ext + .call( + Weight::zero(), + BalanceOf::::zero(), + account_id, + 0, + vec![], + false, + false, + ) + .unwrap(); + + exec_success() + }); + + ExtBuilder::default() + .with_code_hashes(MockLoader::code_hashes()) + .build() + .execute_with(|| { + let schedule = ::Schedule::get(); + let min_balance = ::Currency::minimum_balance(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + let fail_executable = + MockExecutable::from_storage(fail_code, &mut gas_meter).unwrap(); + let success_executable = + MockExecutable::from_storage(success_code, &mut gas_meter).unwrap(); + let succ_fail_executable = + MockExecutable::from_storage(succ_fail_code, &mut gas_meter).unwrap(); + let succ_succ_executable = + MockExecutable::from_storage(succ_succ_code, &mut gas_meter).unwrap(); + set_balance(&ALICE, min_balance * 10_000); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, None, min_balance * 100).unwrap(); + + MockStack::run_instantiate( + ALICE, + fail_executable, + &mut gas_meter, + &mut storage_meter, + &schedule, + min_balance * 100, + vec![], + &[], + None, + ) + .ok(); + assert_eq!(>::get(), 0); + + assert_ok!(MockStack::run_instantiate( + ALICE, + success_executable, + &mut gas_meter, + &mut storage_meter, + &schedule, + min_balance * 100, + vec![], + &[], + None, + )); + assert_eq!(>::get(), 1); + + assert_ok!(MockStack::run_instantiate( + ALICE, + succ_fail_executable, + &mut gas_meter, + &mut storage_meter, + &schedule, + min_balance * 200, + vec![], + &[], + None, + )); + assert_eq!(>::get(), 2); + + assert_ok!(MockStack::run_instantiate( + ALICE, + succ_succ_executable, + &mut gas_meter, + &mut storage_meter, + &schedule, + min_balance * 200, + vec![], + &[], + None, + )); + assert_eq!(>::get(), 4); + }); + } + + #[test] + fn set_storage_works() { + let code_hash = MockLoader::insert(Call, |ctx, _| { + // Write + assert_eq!( + ctx.ext.set_storage(&Key::Fix([1; 32]), Some(vec![1, 2, 3]), false), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_storage(&Key::Fix([2; 32]), Some(vec![4, 5, 6]), true), + Ok(WriteOutcome::New) + ); + assert_eq!(ctx.ext.set_storage(&Key::Fix([3; 32]), None, false), Ok(WriteOutcome::New)); + assert_eq!(ctx.ext.set_storage(&Key::Fix([4; 32]), None, true), Ok(WriteOutcome::New)); + assert_eq!( + ctx.ext.set_storage(&Key::Fix([5; 32]), Some(vec![]), false), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_storage(&Key::Fix([6; 32]), Some(vec![]), true), + Ok(WriteOutcome::New) + ); + + // Overwrite + assert_eq!( + ctx.ext.set_storage(&Key::Fix([1; 32]), Some(vec![42]), false), + Ok(WriteOutcome::Overwritten(3)) + ); + assert_eq!( + ctx.ext.set_storage(&Key::Fix([2; 32]), Some(vec![48]), true), + Ok(WriteOutcome::Taken(vec![4, 5, 6])) + ); + assert_eq!(ctx.ext.set_storage(&Key::Fix([3; 32]), None, false), Ok(WriteOutcome::New)); + assert_eq!(ctx.ext.set_storage(&Key::Fix([4; 32]), None, true), Ok(WriteOutcome::New)); + assert_eq!( + ctx.ext.set_storage(&Key::Fix([5; 32]), Some(vec![]), false), + Ok(WriteOutcome::Overwritten(0)) + ); + assert_eq!( + ctx.ext.set_storage(&Key::Fix([6; 32]), Some(vec![]), true), + Ok(WriteOutcome::Taken(vec![])) + ); + + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let min_balance = ::Currency::minimum_balance(); + let schedule = ::Schedule::get(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + set_balance(&ALICE, min_balance * 1000); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = storage::meter::Meter::new(&contract_origin, None, 0).unwrap(); + assert_ok!(MockStack::run_call( + contract_origin, + BOB, + &mut gas_meter, + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced + )); + }); + } + + #[test] + fn set_storage_varsized_key_works() { + let code_hash = MockLoader::insert(Call, |ctx, _| { + // Write + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([1; 64].to_vec()).unwrap(), + Some(vec![1, 2, 3]), + false + ), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([2; 19].to_vec()).unwrap(), + Some(vec![4, 5, 6]), + true + ), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([3; 19].to_vec()).unwrap(), + None, + false + ), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([4; 64].to_vec()).unwrap(), + None, + true + ), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([5; 30].to_vec()).unwrap(), + Some(vec![]), + false + ), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([6; 128].to_vec()).unwrap(), + Some(vec![]), + true + ), + Ok(WriteOutcome::New) + ); + + // Overwrite + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([1; 64].to_vec()).unwrap(), + Some(vec![42, 43, 44]), + false + ), + Ok(WriteOutcome::Overwritten(3)) + ); + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([2; 19].to_vec()).unwrap(), + Some(vec![48]), + true + ), + Ok(WriteOutcome::Taken(vec![4, 5, 6])) + ); + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([3; 19].to_vec()).unwrap(), + None, + false + ), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([4; 64].to_vec()).unwrap(), + None, + true + ), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([5; 30].to_vec()).unwrap(), + Some(vec![]), + false + ), + Ok(WriteOutcome::Overwritten(0)) + ); + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([6; 128].to_vec()).unwrap(), + Some(vec![]), + true + ), + Ok(WriteOutcome::Taken(vec![])) + ); + + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let min_balance = ::Currency::minimum_balance(); + let schedule = ::Schedule::get(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + set_balance(&ALICE, min_balance * 1000); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = storage::meter::Meter::new(&contract_origin, None, 0).unwrap(); + assert_ok!(MockStack::run_call( + contract_origin, + BOB, + &mut gas_meter, + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced + )); + }); + } + + #[test] + fn get_storage_works() { + let code_hash = MockLoader::insert(Call, |ctx, _| { + assert_eq!( + ctx.ext.set_storage(&Key::Fix([1; 32]), Some(vec![1, 2, 3]), false), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_storage(&Key::Fix([2; 32]), Some(vec![]), false), + Ok(WriteOutcome::New) + ); + assert_eq!(ctx.ext.get_storage(&Key::Fix([1; 32])), Some(vec![1, 2, 3])); + assert_eq!(ctx.ext.get_storage(&Key::Fix([2; 32])), Some(vec![])); + assert_eq!(ctx.ext.get_storage(&Key::Fix([3; 32])), None); + + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let min_balance = ::Currency::minimum_balance(); + let schedule = ::Schedule::get(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + set_balance(&ALICE, min_balance * 1000); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = storage::meter::Meter::new(&contract_origin, None, 0).unwrap(); + assert_ok!(MockStack::run_call( + contract_origin, + BOB, + &mut gas_meter, + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced + )); + }); + } + + #[test] + fn get_storage_size_works() { + let code_hash = MockLoader::insert(Call, |ctx, _| { + assert_eq!( + ctx.ext.set_storage(&Key::Fix([1; 32]), Some(vec![1, 2, 3]), false), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_storage(&Key::Fix([2; 32]), Some(vec![]), false), + Ok(WriteOutcome::New) + ); + assert_eq!(ctx.ext.get_storage_size(&Key::Fix([1; 32])), Some(3)); + assert_eq!(ctx.ext.get_storage_size(&Key::Fix([2; 32])), Some(0)); + assert_eq!(ctx.ext.get_storage_size(&Key::Fix([3; 32])), None); + + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let min_balance = ::Currency::minimum_balance(); + let schedule = ::Schedule::get(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + set_balance(&ALICE, min_balance * 1000); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = storage::meter::Meter::new(&contract_origin, None, 0).unwrap(); + assert_ok!(MockStack::run_call( + contract_origin, + BOB, + &mut gas_meter, + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced + )); + }); + } + + #[test] + fn get_storage_varsized_key_works() { + let code_hash = MockLoader::insert(Call, |ctx, _| { + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([1; 19].to_vec()).unwrap(), + Some(vec![1, 2, 3]), + false + ), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([2; 16].to_vec()).unwrap(), + Some(vec![]), + false + ), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.get_storage(&Key::::try_from_var([1; 19].to_vec()).unwrap()), + Some(vec![1, 2, 3]) + ); + assert_eq!( + ctx.ext.get_storage(&Key::::try_from_var([2; 16].to_vec()).unwrap()), + Some(vec![]) + ); + assert_eq!( + ctx.ext.get_storage(&Key::::try_from_var([3; 8].to_vec()).unwrap()), + None + ); + + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let min_balance = ::Currency::minimum_balance(); + let schedule = ::Schedule::get(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + set_balance(&ALICE, min_balance * 1000); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = storage::meter::Meter::new(&contract_origin, None, 0).unwrap(); + assert_ok!(MockStack::run_call( + contract_origin, + BOB, + &mut gas_meter, + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced + )); + }); + } + + #[test] + fn get_storage_size_varsized_key_works() { + let code_hash = MockLoader::insert(Call, |ctx, _| { + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([1; 19].to_vec()).unwrap(), + Some(vec![1, 2, 3]), + false + ), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_storage( + &Key::::try_from_var([2; 16].to_vec()).unwrap(), + Some(vec![]), + false + ), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.get_storage_size(&Key::::try_from_var([1; 19].to_vec()).unwrap()), + Some(3) + ); + assert_eq!( + ctx.ext.get_storage_size(&Key::::try_from_var([2; 16].to_vec()).unwrap()), + Some(0) + ); + assert_eq!( + ctx.ext.get_storage_size(&Key::::try_from_var([3; 8].to_vec()).unwrap()), + None + ); + + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let min_balance = ::Currency::minimum_balance(); + let schedule = ::Schedule::get(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + set_balance(&ALICE, min_balance * 1000); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = storage::meter::Meter::new(&contract_origin, None, 0).unwrap(); + assert_ok!(MockStack::run_call( + contract_origin, + BOB, + &mut gas_meter, + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced + )); + }); + } + + #[test] + fn set_transient_storage_works() { + let code_hash = MockLoader::insert(Call, |ctx, _| { + // Write + assert_eq!( + ctx.ext.set_transient_storage(&Key::Fix([1; 32]), Some(vec![1, 2, 3]), false), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_transient_storage(&Key::Fix([2; 32]), Some(vec![4, 5, 6]), true), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_transient_storage(&Key::Fix([3; 32]), None, false), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_transient_storage(&Key::Fix([4; 32]), None, true), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_transient_storage(&Key::Fix([5; 32]), Some(vec![]), false), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_transient_storage(&Key::Fix([6; 32]), Some(vec![]), true), + Ok(WriteOutcome::New) + ); + + // Overwrite + assert_eq!( + ctx.ext.set_transient_storage(&Key::Fix([1; 32]), Some(vec![42]), false), + Ok(WriteOutcome::Overwritten(3)) + ); + assert_eq!( + ctx.ext.set_transient_storage(&Key::Fix([2; 32]), Some(vec![48]), true), + Ok(WriteOutcome::Taken(vec![4, 5, 6])) + ); + assert_eq!( + ctx.ext.set_transient_storage(&Key::Fix([3; 32]), None, false), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_transient_storage(&Key::Fix([4; 32]), None, true), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_transient_storage(&Key::Fix([5; 32]), Some(vec![]), false), + Ok(WriteOutcome::Overwritten(0)) + ); + assert_eq!( + ctx.ext.set_transient_storage(&Key::Fix([6; 32]), Some(vec![]), true), + Ok(WriteOutcome::Taken(vec![])) + ); + + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = storage::meter::Meter::new(&contract_origin, None, 0).unwrap(); + assert_ok!(MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced + )); + }); + } + + #[test] + fn get_transient_storage_works() { + // Call stack: BOB -> CHARLIE(success) -> BOB' (success) + let storage_key_1 = &Key::Fix([1; 32]); + let storage_key_2 = &Key::Fix([2; 32]); + let storage_key_3 = &Key::Fix([3; 32]); + let code_bob = MockLoader::insert(Call, |ctx, _| { + if ctx.input_data[0] == 0 { + assert_eq!( + ctx.ext.set_transient_storage(storage_key_1, Some(vec![1, 2]), false), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.call( + Weight::zero(), + BalanceOf::::zero(), + CHARLIE, + 0, + vec![], + true, + false, + ), + exec_success() + ); + assert_eq!(ctx.ext.get_transient_storage(storage_key_1), Some(vec![3])); + assert_eq!(ctx.ext.get_transient_storage(storage_key_2), Some(vec![])); + assert_eq!(ctx.ext.get_transient_storage(storage_key_3), None); + } else { + assert_eq!( + ctx.ext.set_transient_storage(storage_key_1, Some(vec![3]), true), + Ok(WriteOutcome::Taken(vec![1, 2])) + ); + assert_eq!( + ctx.ext.set_transient_storage(storage_key_2, Some(vec![]), false), + Ok(WriteOutcome::New) + ); + } + exec_success() + }); + let code_charlie = MockLoader::insert(Call, |ctx, _| { + assert!(ctx + .ext + .call(Weight::zero(), BalanceOf::::zero(), BOB, 0, vec![99], true, false) + .is_ok()); + // CHARLIE can not read BOB`s storage. + assert_eq!(ctx.ext.get_transient_storage(storage_key_1), None); + exec_success() + }); + + // This one tests passing the input data into a contract via call. + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, code_bob); + place_contract(&CHARLIE, code_charlie); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![0], + None, + Determinism::Enforced, + ); + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn get_transient_storage_size_works() { + let storage_key_1 = &Key::Fix([1; 32]); + let storage_key_2 = &Key::Fix([2; 32]); + let storage_key_3 = &Key::Fix([3; 32]); + let code_hash = MockLoader::insert(Call, |ctx, _| { + assert_eq!( + ctx.ext.set_transient_storage(storage_key_1, Some(vec![1, 2, 3]), false), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.set_transient_storage(storage_key_2, Some(vec![]), false), + Ok(WriteOutcome::New) + ); + assert_eq!(ctx.ext.get_transient_storage_size(storage_key_1), Some(3)); + assert_eq!(ctx.ext.get_transient_storage_size(storage_key_2), Some(0)); + assert_eq!(ctx.ext.get_transient_storage_size(storage_key_3), None); + + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + assert_ok!(MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced + )); + }); + } + + #[test] + fn rollback_transient_storage_works() { + // Call stack: BOB -> CHARLIE (trap) -> BOB' (success) + let storage_key = &Key::Fix([1; 32]); + let code_bob = MockLoader::insert(Call, |ctx, _| { + if ctx.input_data[0] == 0 { + assert_eq!( + ctx.ext.set_transient_storage(storage_key, Some(vec![1, 2]), false), + Ok(WriteOutcome::New) + ); + assert_eq!( + ctx.ext.call( + Weight::zero(), + BalanceOf::::zero(), + CHARLIE, + 0, + vec![], + true, + false + ), + exec_trapped() + ); + assert_eq!(ctx.ext.get_transient_storage(storage_key), Some(vec![1, 2])); + } else { + let overwritten_length = ctx.ext.get_transient_storage_size(storage_key).unwrap(); + assert_eq!( + ctx.ext.set_transient_storage(storage_key, Some(vec![3]), false), + Ok(WriteOutcome::Overwritten(overwritten_length)) + ); + assert_eq!(ctx.ext.get_transient_storage(storage_key), Some(vec![3])); + } + exec_success() + }); + let code_charlie = MockLoader::insert(Call, |ctx, _| { + assert!(ctx + .ext + .call(Weight::zero(), BalanceOf::::zero(), BOB, 0, vec![99], true, false) + .is_ok()); + exec_trapped() + }); + + // This one tests passing the input data into a contract via call. + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, code_bob); + place_contract(&CHARLIE, code_charlie); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![0], + None, + Determinism::Enforced, + ); + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn ecdsa_to_eth_address_returns_proper_value() { + let bob_ch = MockLoader::insert(Call, |ctx, _| { + let pubkey_compressed = array_bytes::hex2array_unchecked( + "028db55b05db86c0b1786ca49f095d76344c9e6056b2f02701a7e7f3c20aabfd91", + ); + assert_eq!( + ctx.ext.ecdsa_to_eth_address(&pubkey_compressed).unwrap(), + array_bytes::hex2array_unchecked::<_, 20>( + "09231da7b19A016f9e576d23B16277062F4d46A8" + ) + ); + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, bob_ch); + + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced, + ); + assert_matches!(result, Ok(_)); + }); + } + + #[test] + fn nonce_api_works() { + let fail_code = MockLoader::insert(Constructor, |_, _| exec_trapped()); + let success_code = MockLoader::insert(Constructor, |_, _| exec_success()); + let code_hash = MockLoader::insert(Call, move |ctx, _| { + // It is set to one when this contract was instantiated by `place_contract` + assert_eq!(ctx.ext.nonce(), 1); + // Should not change without any instantiation in-between + assert_eq!(ctx.ext.nonce(), 1); + // Should not change with a failed instantiation + assert_err!( + ctx.ext.instantiate( + Weight::zero(), + BalanceOf::::zero(), + fail_code, + 0, + vec![], + &[], + ), + ExecError { + error: >::ContractTrapped.into(), + origin: ErrorOrigin::Callee + } + ); + assert_eq!(ctx.ext.nonce(), 1); + // Successful instantiation increments + ctx.ext + .instantiate( + Weight::zero(), + BalanceOf::::zero(), + success_code, + 0, + vec![], + &[], + ) + .unwrap(); + assert_eq!(ctx.ext.nonce(), 2); + exec_success() + }); + + ExtBuilder::default() + .with_code_hashes(MockLoader::code_hashes()) + .build() + .execute_with(|| { + let min_balance = ::Currency::minimum_balance(); + let schedule = ::Schedule::get(); + let mut gas_meter = GasMeter::::new(GAS_LIMIT); + set_balance(&ALICE, min_balance * 1000); + place_contract(&BOB, code_hash); + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, None, 0).unwrap(); + assert_ok!(MockStack::run_call( + contract_origin, + BOB, + &mut gas_meter, + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced + )); + }); + } + + /// This works even though random interface is deprecated, as the check to ban deprecated + /// functions happens in the wasm stack which is mocked for exec tests. + #[test] + fn randomness_works() { + let subject = b"nice subject".as_ref(); + let code_hash = MockLoader::insert(Call, move |ctx, _| { + let rand = ::Randomness::random(subject); + assert_eq!(rand, ctx.ext.random(subject)); + exec_success() + }); + + ExtBuilder::default().build().execute_with(|| { + let schedule = ::Schedule::get(); + place_contract(&BOB, code_hash); + + let contract_origin = Origin::from_account_id(ALICE); + let mut storage_meter = + storage::meter::Meter::new(&contract_origin, Some(0), 0).unwrap(); + let result = MockStack::run_call( + contract_origin, + BOB, + &mut GasMeter::::new(GAS_LIMIT), + &mut storage_meter, + &schedule, + 0, + vec![], + None, + Determinism::Enforced, + ); + assert_matches!(result, Ok(_)); + }); + } +} diff --git a/pallets/contracts/src/gas.rs b/pallets/contracts/src/gas.rs new file mode 100644 index 00000000..f8c97e25 --- /dev/null +++ b/pallets/contracts/src/gas.rs @@ -0,0 +1,399 @@ +// This file is part of Substrate. + +// 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::{exec::ExecError, Config, Error}; +use core::marker::PhantomData; +use frame_support::{ + dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, PostDispatchInfo}, + weights::Weight, + DefaultNoBound, +}; +use sp_core::Get; +use sp_runtime::{traits::Zero, DispatchError}; + +#[cfg(test)] +use std::{any::Any, fmt::Debug}; + +#[derive(Debug, PartialEq, Eq)] +pub struct ChargedAmount(Weight); + +impl ChargedAmount { + pub fn amount(&self) -> Weight { + self.0 + } +} + +/// Meter for syncing the gas between the executor and the gas meter. +#[derive(DefaultNoBound)] +struct EngineMeter { + fuel: u64, + _phantom: PhantomData, +} + +impl EngineMeter { + /// Create a meter with the given fuel limit. + fn new(limit: Weight) -> Self { + Self { + fuel: limit.ref_time().saturating_div(T::Schedule::get().ref_time_by_fuel()), + _phantom: PhantomData, + } + } + + /// Set the fuel left to the given value. + /// Returns the amount of Weight consumed since the last update. + fn set_fuel(&mut self, fuel: u64) -> Weight { + let consumed = self + .fuel + .saturating_sub(fuel) + .saturating_mul(T::Schedule::get().ref_time_by_fuel()); + self.fuel = fuel; + Weight::from_parts(consumed, 0) + } + + /// Charge the given amount of gas. + /// Returns the amount of fuel left. + fn charge_ref_time(&mut self, ref_time: u64) -> Result { + let amount = ref_time + .checked_div(T::Schedule::get().ref_time_by_fuel()) + .ok_or(Error::::InvalidSchedule)?; + + self.fuel.checked_sub(amount).ok_or_else(|| Error::::OutOfGas)?; + Ok(Syncable(self.fuel)) + } +} + +/// Used to capture the gas left before entering a host function. +/// +/// Has to be consumed in order to sync back the gas after leaving the host function. +#[must_use] +pub struct RefTimeLeft(u64); + +/// Resource that needs to be synced to the executor. +/// +/// Wrapped to make sure that the resource will be synced back the the executor. +#[must_use] +pub struct Syncable(u64); + +impl From for u64 { + fn from(from: Syncable) -> u64 { + from.0 + } +} + +#[cfg(not(test))] +pub trait TestAuxiliaries {} +#[cfg(not(test))] +impl TestAuxiliaries for T {} + +#[cfg(test)] +pub trait TestAuxiliaries: Any + Debug + PartialEq + Eq {} +#[cfg(test)] +impl TestAuxiliaries for T {} + +/// This trait represents a token that can be used for charging `GasMeter`. +/// There is no other way of charging it. +/// +/// Implementing type is expected to be super lightweight hence `Copy` (`Clone` is added +/// for consistency). If inlined there should be no observable difference compared +/// to a hand-written code. +pub trait Token: Copy + Clone + TestAuxiliaries { + /// Return the amount of gas that should be taken by this token. + /// + /// This function should be really lightweight and must not fail. It is not + /// expected that implementors will query the storage or do any kinds of heavy operations. + /// + /// That said, implementors of this function still can run into overflows + /// while calculating the amount. In this case it is ok to use saturating operations + /// since on overflow they will return `max_value` which should consume all gas. + fn weight(&self) -> Weight; + + /// Returns true if this token is expected to influence the lowest gas limit. + fn influence_lowest_gas_limit(&self) -> bool { + true + } +} + +/// A wrapper around a type-erased trait object of what used to be a `Token`. +#[cfg(test)] +pub struct ErasedToken { + pub description: String, + pub token: Box, +} + +#[derive(DefaultNoBound)] +pub struct GasMeter { + gas_limit: Weight, + /// Amount of gas left from initial gas limit. Can reach zero. + gas_left: Weight, + /// Due to `adjust_gas` and `nested` the `gas_left` can temporarily dip below its final value. + gas_left_lowest: Weight, + /// The amount of resources that was consumed by the execution engine. + /// We have to track it separately in order to avoid the loss of precision that happens when + /// converting from ref_time to the execution engine unit. + engine_meter: EngineMeter, + _phantom: PhantomData, + #[cfg(test)] + tokens: Vec, +} + +impl GasMeter { + pub fn new(gas_limit: Weight) -> Self { + GasMeter { + gas_limit, + gas_left: gas_limit, + gas_left_lowest: gas_limit, + engine_meter: EngineMeter::new(gas_limit), + _phantom: PhantomData, + #[cfg(test)] + tokens: Vec::new(), + } + } + + /// Create a new gas meter by removing gas from the current meter. + /// + /// # Note + /// + /// Passing `0` as amount is interpreted as "all remaining gas". + pub fn nested(&mut self, amount: Weight) -> Self { + let amount = Weight::from_parts( + if amount.ref_time().is_zero() { + self.gas_left().ref_time() + } else { + amount.ref_time() + }, + if amount.proof_size().is_zero() { + self.gas_left().proof_size() + } else { + amount.proof_size() + }, + ) + .min(self.gas_left); + self.gas_left -= amount; + GasMeter::new(amount) + } + + /// Absorb the remaining gas of a nested meter after we are done using it. + pub fn absorb_nested(&mut self, nested: Self) { + self.gas_left_lowest = (self.gas_left + nested.gas_limit) + .saturating_sub(nested.gas_required()) + .min(self.gas_left_lowest); + self.gas_left += nested.gas_left; + } + + /// Account for used gas. + /// + /// Amount is calculated by the given `token`. + /// + /// Returns `OutOfGas` if there is not enough gas or addition of the specified + /// amount of gas has lead to overflow. + /// + /// NOTE that amount isn't consumed if there is not enough gas. This is considered + /// safe because we always charge gas before performing any resource-spending action. + #[inline] + pub fn charge>(&mut self, token: Tok) -> Result { + #[cfg(test)] + { + // Unconditionally add the token to the storage. + let erased_tok = + ErasedToken { description: format!("{:?}", token), token: Box::new(token) }; + self.tokens.push(erased_tok); + } + let amount = token.weight(); + // It is OK to not charge anything on failure because we always charge _before_ we perform + // any action + self.gas_left = self.gas_left.checked_sub(&amount).ok_or_else(|| Error::::OutOfGas)?; + Ok(ChargedAmount(amount)) + } + + /// Adjust a previously charged amount down to its actual amount. + /// + /// This is when a maximum a priori amount was charged and then should be partially + /// refunded to match the actual amount. + pub fn adjust_gas>(&mut self, charged_amount: ChargedAmount, token: Tok) { + if token.influence_lowest_gas_limit() { + self.gas_left_lowest = self.gas_left_lowest(); + } + let adjustment = charged_amount.0.saturating_sub(token.weight()); + self.gas_left = self.gas_left.saturating_add(adjustment).min(self.gas_limit); + } + + /// Hand over the gas metering responsibility from the executor to this meter. + /// + /// Needs to be called when entering a host function to update this meter with the + /// gas that was tracked by the executor. It tracks the latest seen total value + /// in order to compute the delta that needs to be charged. + pub fn sync_from_executor(&mut self, engine_fuel: u64) -> Result { + let weight_consumed = self.engine_meter.set_fuel(engine_fuel); + self.gas_left + .checked_reduce(weight_consumed) + .ok_or_else(|| Error::::OutOfGas)?; + Ok(RefTimeLeft(self.gas_left.ref_time())) + } + + /// Hand over the gas metering responsibility from this meter to the executor. + /// + /// Needs to be called when leaving a host function in order to calculate how much + /// gas needs to be charged from the **executor**. It updates the last seen executor + /// total value so that it is correct when `sync_from_executor` is called the next time. + /// + /// It is important that this does **not** actually sync with the executor. That has + /// to be done by the caller. + pub fn sync_to_executor(&mut self, before: RefTimeLeft) -> Result { + let ref_time_consumed = before.0.saturating_sub(self.gas_left().ref_time()); + self.engine_meter.charge_ref_time(ref_time_consumed) + } + + /// Returns the amount of gas that is required to run the same call. + /// + /// This can be different from `gas_spent` because due to `adjust_gas` the amount of + /// spent gas can temporarily drop and be refunded later. + pub fn gas_required(&self) -> Weight { + self.gas_limit.saturating_sub(self.gas_left_lowest()) + } + + /// Returns how much gas was spent + pub fn gas_consumed(&self) -> Weight { + self.gas_limit.saturating_sub(self.gas_left) + } + + /// Returns how much gas left from the initial budget. + pub fn gas_left(&self) -> Weight { + self.gas_left + } + + /// Turn this GasMeter into a DispatchResult that contains the actually used gas. + pub fn into_dispatch_result( + self, + result: Result, + base_weight: Weight, + ) -> DispatchResultWithPostInfo + where + E: Into, + { + let post_info = PostDispatchInfo { + actual_weight: Some(self.gas_consumed().saturating_add(base_weight)), + pays_fee: Default::default(), + }; + + result + .map(|_| post_info) + .map_err(|e| DispatchErrorWithPostInfo { post_info, error: e.into().error }) + } + + fn gas_left_lowest(&self) -> Weight { + self.gas_left_lowest.min(self.gas_left) + } + + #[cfg(test)] + pub fn tokens(&self) -> &[ErasedToken] { + &self.tokens + } +} + +#[cfg(test)] +mod tests { + use super::{GasMeter, Token, Weight}; + use crate::tests::Test; + + /// A simple utility macro that helps to match against a + /// list of tokens. + macro_rules! match_tokens { + ($tokens_iter:ident,) => { + }; + ($tokens_iter:ident, $x:expr, $($rest:tt)*) => { + { + let next = ($tokens_iter).next().unwrap(); + let pattern = $x; + + // Note that we don't specify the type name directly in this macro, + // we only have some expression $x of some type. At the same time, we + // have an iterator of Box and to downcast we need to specify + // the type which we want downcast to. + // + // So what we do is we assign `_pattern_typed_next_ref` to a variable which has + // the required type. + // + // Then we make `_pattern_typed_next_ref = token.downcast_ref()`. This makes + // rustc infer the type `T` (in `downcast_ref`) to be the same as in $x. + + let mut _pattern_typed_next_ref = &pattern; + _pattern_typed_next_ref = match next.token.downcast_ref() { + Some(p) => { + assert_eq!(p, &pattern); + p + } + None => { + panic!("expected type {} got {}", stringify!($x), next.description); + } + }; + } + + match_tokens!($tokens_iter, $($rest)*); + }; + } + + /// A trivial token that charges the specified number of gas units. + #[derive(Copy, Clone, PartialEq, Eq, Debug)] + struct SimpleToken(u64); + impl Token for SimpleToken { + fn weight(&self) -> Weight { + Weight::from_parts(self.0, 0) + } + } + + #[test] + fn it_works() { + let gas_meter = GasMeter::::new(Weight::from_parts(50000, 0)); + assert_eq!(gas_meter.gas_left(), Weight::from_parts(50000, 0)); + } + + #[test] + fn tracing() { + let mut gas_meter = GasMeter::::new(Weight::from_parts(50000, 0)); + assert!(!gas_meter.charge(SimpleToken(1)).is_err()); + + let mut tokens = gas_meter.tokens().iter(); + match_tokens!(tokens, SimpleToken(1),); + } + + // This test makes sure that nothing can be executed if there is no gas. + #[test] + fn refuse_to_execute_anything_if_zero() { + let mut gas_meter = GasMeter::::new(Weight::zero()); + assert!(gas_meter.charge(SimpleToken(1)).is_err()); + } + + // Make sure that the gas meter does not charge in case of overcharge + #[test] + fn overcharge_does_not_charge() { + let mut gas_meter = GasMeter::::new(Weight::from_parts(200, 0)); + + // The first charge is should lead to OOG. + assert!(gas_meter.charge(SimpleToken(300)).is_err()); + + // The gas meter should still contain the full 200. + assert!(gas_meter.charge(SimpleToken(200)).is_ok()); + } + + // Charging the exact amount that the user paid for should be + // possible. + #[test] + fn charge_exact_amount() { + let mut gas_meter = GasMeter::::new(Weight::from_parts(25, 0)); + assert!(!gas_meter.charge(SimpleToken(25)).is_err()); + } +} diff --git a/pallets/contracts/src/lib.rs b/pallets/contracts/src/lib.rs new file mode 100644 index 00000000..7bb5b46c --- /dev/null +++ b/pallets/contracts/src/lib.rs @@ -0,0 +1,1986 @@ +// This file is part of Substrate. + +// 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. + +//! # Contracts Pallet +//! +//! The Contracts module provides functionality for the runtime to deploy and execute WebAssembly +//! smart-contracts. +//! +//! - [`Config`] +//! - [`Call`] +//! +//! ## Overview +//! +//! This module extends accounts based on the [`frame_support::traits::fungible`] traits to have +//! smart-contract functionality. It can be used with other modules that implement accounts based on +//! the [`frame_support::traits::fungible`] traits. These "smart-contract accounts" have the ability +//! to instantiate smart-contracts and make calls to other contract and non-contract accounts. +//! +//! The smart-contract code is stored once, and later retrievable via its hash. +//! This means that multiple smart-contracts can be instantiated from the same hash, without +//! replicating the code each time. +//! +//! When a smart-contract is called, its associated code is retrieved via the code hash and gets +//! executed. This call can alter the storage entries of the smart-contract account, instantiate new +//! smart-contracts, or call other smart-contracts. +//! +//! Finally, when an account is reaped, its associated code and storage of the smart-contract +//! account will also be deleted. +//! +//! ### Weight +//! +//! Senders must specify a [`Weight`] limit with every call, as all instructions invoked by the +//! smart-contract require weight. Unused weight is refunded after the call, regardless of the +//! execution outcome. +//! +//! If the weight limit is reached, then all calls and state changes (including balance transfers) +//! are only reverted at the current call's contract level. For example, if contract A calls B and B +//! runs out of gas mid-call, then all of B's calls are reverted. Assuming correct error handling by +//! contract A, A's other calls and state changes still persist. +//! +//! ### Notable Scenarios +//! +//! Contract call failures are not always cascading. When failures occur in a sub-call, they do not +//! "bubble up", and the call will only revert at the specific contract level. For example, if +//! contract A calls contract B, and B fails, A can decide how to handle that failure, either +//! proceeding or reverting A's changes. +//! +//! ## Interface +//! +//! ### Dispatchable functions +//! +//! * [`Pallet::instantiate_with_code`] - Deploys a new contract from the supplied Wasm binary, +//! optionally transferring +//! some balance. This instantiates a new smart contract account with the supplied code and +//! calls its constructor to initialize the contract. +//! * [`Pallet::instantiate`] - The same as `instantiate_with_code` but instead of uploading new +//! code an existing `code_hash` is supplied. +//! * [`Pallet::call`] - Makes a call to an account, optionally transferring some balance. +//! * [`Pallet::upload_code`] - Uploads new code without instantiating a contract from it. +//! * [`Pallet::remove_code`] - Removes the stored code and refunds the deposit to its owner. Only +//! allowed to code owner. +//! * [`Pallet::set_code`] - Changes the code of an existing contract. Only allowed to `Root` +//! origin. +//! * [`Pallet::migrate`] - Runs migration steps of current multi-block migration in priority, +//! before [`Hooks::on_idle`][frame_support::traits::Hooks::on_idle] activates. +//! +//! ## Usage +//! +//! * [`ink!`](https://use.ink) is language that enables writing Wasm-based smart contracts in plain +//! Rust. + +#![allow(rustdoc::private_intra_doc_links)] +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(feature = "runtime-benchmarks", recursion_limit = "1024")] + +extern crate alloc; +mod address; +mod benchmarking; +mod exec; +mod gas; +mod primitives; +pub use primitives::*; + +mod schedule; +mod storage; +mod transient_storage; +mod wasm; + +pub mod chain_extension; +pub mod debug; +pub mod migration; +pub mod test_utils; +pub mod weights; + +#[cfg(test)] +mod tests; +use crate::{ + exec::{ + AccountIdOf, ErrorOrigin, ExecError, Executable, Ext, Key, MomentOf, Stack as ExecStack, + }, + gas::GasMeter, + storage::{meter::Meter as StorageMeter, ContractInfo, DeletionQueueManager}, + wasm::{CodeInfo, RuntimeCosts, WasmBlob}, +}; +use codec::{Codec, Decode, Encode, HasCompact, MaxEncodedLen}; +use core::fmt::Debug; +use environmental::*; +use frame_support::{ + dispatch::{GetDispatchInfo, Pays, PostDispatchInfo, RawOrigin, WithPostDispatchInfo}, + ensure, + traits::{ + fungible::{Inspect, Mutate, MutateHold}, + ConstU32, Contains, Get, Randomness, Time, + }, + weights::{Weight, WeightMeter}, + BoundedVec, DefaultNoBound, RuntimeDebugNoBound, +}; +use frame_system::{ + ensure_signed, + pallet_prelude::{BlockNumberFor, OriginFor}, + EventRecord, Pallet as System, +}; +use scale_info::TypeInfo; +use smallvec::Array; +use sp_runtime::{ + traits::{BadOrigin, Convert, Dispatchable, Saturating, StaticLookup, Zero}, + DispatchError, RuntimeDebug, +}; + +pub use crate::{ + address::{AddressGenerator, DefaultAddressGenerator}, + debug::Tracing, + exec::Frame, + migration::{MigrateSequence, Migration, NoopMigration}, + pallet::*, + schedule::{InstructionWeights, Limits, Schedule}, + wasm::Determinism, +}; +pub use weights::WeightInfo; + +#[cfg(doc)] +pub use crate::wasm::api_doc; + +type CodeHash = ::Hash; +type TrieId = BoundedVec>; +type BalanceOf = + <::Currency as Inspect<::AccountId>>::Balance; +type CodeVec = BoundedVec::MaxCodeLen>; +type AccountIdLookupOf = <::Lookup as StaticLookup>::Source; +type DebugBufferVec = BoundedVec::MaxDebugBufferLen>; +type EventRecordOf = + EventRecord<::RuntimeEvent, ::Hash>; + +/// The old weight type. +/// +/// This is a copy of the [`frame_support::weights::OldWeight`] type since the contracts pallet +/// needs to support it indefinitely. +type OldWeight = u64; + +/// Used as a sentinel value when reading and writing contract memory. +/// +/// It is usually used to signal `None` to a contract when only a primitive is allowed +/// and we don't want to go through encoding a full Rust type. Using `u32::Max` is a safe +/// sentinel because contracts are never allowed to use such a large amount of resources +/// that this value makes sense for a memory location or length. +const SENTINEL: u32 = u32::MAX; + +/// The target that is used for the log output emitted by this crate. +/// +/// Hence you can use this target to selectively increase the log level for this crate. +/// +/// Example: `RUST_LOG=runtime::contracts=debug my_code --dev` +const LOG_TARGET: &str = "runtime::contracts"; + +/// Wrapper around `PhantomData` to prevent it being filtered by `scale-info`. +/// +/// `scale-info` filters out `PhantomData` fields because usually we are only interested +/// in sized types. However, when trying to communicate **types** as opposed to **values** +/// we want to have those zero sized types be included. +#[derive(Encode, Decode, DefaultNoBound, TypeInfo)] +#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +pub struct EnvironmentType(PhantomData); + +/// List of all runtime configurable types that are used in the communication between +/// `pallet-contracts` and any given contract. +/// +/// Since those types are configurable they can vary between +/// chains all using `pallet-contracts`. Hence we need a mechanism to communicate those types +/// in a way that can be consumed by offchain tooling. +/// +/// This type only exists in order to appear in the metadata where it can be read by +/// offchain tooling. +#[derive(Encode, Decode, DefaultNoBound, TypeInfo)] +#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +#[scale_info(skip_type_params(T))] +pub struct Environment { + account_id: EnvironmentType>, + balance: EnvironmentType>, + hash: EnvironmentType<::Hash>, + hasher: EnvironmentType<::Hashing>, + timestamp: EnvironmentType>, + block_number: EnvironmentType>, +} + +/// Defines the current version of the HostFn APIs. +/// This is used to communicate the available APIs in pallet-contracts. +/// +/// The version is bumped any time a new HostFn is added or stabilized. +#[derive(Encode, Decode, TypeInfo)] +pub struct ApiVersion(u16); +impl Default for ApiVersion { + fn default() -> Self { + Self(4) + } +} + +#[test] +fn api_version_is_up_to_date() { + assert_eq!( + 111, + crate::wasm::STABLE_API_COUNT, + "Stable API count has changed. Bump the returned value of ApiVersion::default() and update the test." + ); +} + +#[frame_support::pallet] +pub mod pallet { + use super::*; + use crate::debug::Debugger; + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use sp_runtime::Perbill; + + /// The in-code storage version. + pub(crate) const STORAGE_VERSION: StorageVersion = StorageVersion::new(16); + + #[pallet::pallet] + #[pallet::storage_version(STORAGE_VERSION)] + pub struct Pallet(_); + + #[pallet::config(with_default)] + pub trait Config: frame_system::Config { + /// The time implementation used to supply timestamps to contracts through `seal_now`. + type Time: Time; + + /// The generator used to supply randomness to contracts through `seal_random`. + /// + /// # Deprecated + /// + /// Codes using the randomness functionality cannot be uploaded. Neither can contracts + /// be instantiated from existing codes that use this deprecated functionality. It will + /// be removed eventually. Hence for new `pallet-contracts` deployments it is okay + /// to supply a dummy implementation for this type (because it is never used). + #[pallet::no_default_bounds] + type Randomness: Randomness>; + + /// The fungible in which fees are paid and contract balances are held. + #[pallet::no_default] + type Currency: Inspect + + Mutate + + MutateHold; + + /// The overarching event type. + #[pallet::no_default_bounds] + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + + /// The overarching call type. + #[pallet::no_default_bounds] + type RuntimeCall: Dispatchable + + GetDispatchInfo + + codec::Decode + + IsType<::RuntimeCall>; + + /// Overarching hold reason. + #[pallet::no_default_bounds] + type RuntimeHoldReason: From; + + /// Filter that is applied to calls dispatched by contracts. + /// + /// Use this filter to control which dispatchables are callable by contracts. + /// This is applied in **addition** to [`frame_system::Config::BaseCallFilter`]. + /// It is recommended to treat this as a whitelist. + /// + /// # Stability + /// + /// The runtime **must** make sure that all dispatchables that are callable by + /// contracts remain stable. In addition [`Self::RuntimeCall`] itself must remain stable. + /// This means that no existing variants are allowed to switch their positions. + /// + /// # Note + /// + /// Note that dispatchables that are called via contracts do not spawn their + /// own wasm instance for each call (as opposed to when called via a transaction). + /// Therefore please make sure to be restrictive about which dispatchables are allowed + /// in order to not introduce a new DoS vector like memory allocation patterns that can + /// be exploited to drive the runtime into a panic. + /// + /// This filter does not apply to XCM transact calls. To impose restrictions on XCM transact + /// calls, you must configure them separately within the XCM pallet itself. + #[pallet::no_default_bounds] + type CallFilter: Contains<::RuntimeCall>; + + /// Used to answer contracts' queries regarding the current weight price. This is **not** + /// used to calculate the actual fee and is only for informational purposes. + #[pallet::no_default_bounds] + type WeightPrice: Convert>; + + /// Describes the weights of the dispatchables of this module and is also used to + /// construct a default cost schedule. + type WeightInfo: WeightInfo; + + /// Type that allows the runtime authors to add new host functions for a contract to call. + #[pallet::no_default_bounds] + type ChainExtension: chain_extension::ChainExtension + Default; + + /// Cost schedule and limits. + #[pallet::constant] + #[pallet::no_default] + type Schedule: Get>; + + /// The type of the call stack determines the maximum nesting depth of contract calls. + /// + /// The allowed depth is `CallStack::size() + 1`. + /// Therefore a size of `0` means that a contract cannot use call or instantiate. + /// In other words only the origin called "root contract" is allowed to execute then. + /// + /// This setting along with [`MaxCodeLen`](#associatedtype.MaxCodeLen) directly affects + /// memory usage of your runtime. + #[pallet::no_default] + type CallStack: Array>; + + /// The amount of balance a caller has to pay for each byte of storage. + /// + /// # Note + /// + /// Changing this value for an existing chain might need a storage migration. + #[pallet::constant] + #[pallet::no_default_bounds] + type DepositPerByte: Get>; + + /// Fallback value to limit the storage deposit if it's not being set by the caller. + #[pallet::constant] + #[pallet::no_default_bounds] + type DefaultDepositLimit: Get>; + + /// The amount of balance a caller has to pay for each storage item. + /// + /// # Note + /// + /// Changing this value for an existing chain might need a storage migration. + #[pallet::constant] + #[pallet::no_default_bounds] + type DepositPerItem: Get>; + + /// The percentage of the storage deposit that should be held for using a code hash. + /// Instantiating a contract, or calling [`chain_extension::Ext::lock_delegate_dependency`] + /// protects the code from being removed. In order to prevent abuse these actions are + /// protected with a percentage of the code deposit. + #[pallet::constant] + type CodeHashLockupDepositPercent: Get; + + /// The address generator used to generate the addresses of contracts. + #[pallet::no_default_bounds] + type AddressGenerator: AddressGenerator; + + /// The maximum length of a contract code in bytes. + /// + /// The value should be chosen carefully taking into the account the overall memory limit + /// your runtime has, as well as the [maximum allowed callstack + /// depth](#associatedtype.CallStack). Look into the `integrity_test()` for some insights. + #[pallet::constant] + type MaxCodeLen: Get; + + /// The maximum allowable length in bytes for storage keys. + #[pallet::constant] + type MaxStorageKeyLen: Get; + + /// The maximum size of the transient storage in bytes. + /// This includes keys, values, and previous entries used for storage rollback. + #[pallet::constant] + type MaxTransientStorageSize: Get; + + /// The maximum number of delegate_dependencies that a contract can lock with + /// [`chain_extension::Ext::lock_delegate_dependency`]. + #[pallet::constant] + type MaxDelegateDependencies: Get; + + /// Make contract callable functions marked as `#[unstable]` available. + /// + /// Contracts that use `#[unstable]` functions won't be able to be uploaded unless + /// this is set to `true`. This is only meant for testnets and dev nodes in order to + /// experiment with new features. + /// + /// # Warning + /// + /// Do **not** set to `true` on productions chains. + #[pallet::constant] + type UnsafeUnstableInterface: Get; + + /// The maximum length of the debug buffer in bytes. + #[pallet::constant] + type MaxDebugBufferLen: Get; + + /// Origin allowed to upload code. + /// + /// By default, it is safe to set this to `EnsureSigned`, allowing anyone to upload contract + /// code. + #[pallet::no_default_bounds] + type UploadOrigin: EnsureOrigin; + + /// Origin allowed to instantiate code. + /// + /// # Note + /// + /// This is not enforced when a contract instantiates another contract. The + /// [`Self::UploadOrigin`] should make sure that no code is deployed that does unwanted + /// instantiations. + /// + /// By default, it is safe to set this to `EnsureSigned`, allowing anyone to instantiate + /// contract code. + #[pallet::no_default_bounds] + type InstantiateOrigin: EnsureOrigin; + + /// The sequence of migration steps that will be applied during a migration. + /// + /// # Examples + /// ``` + /// use pallet_contracts::migration::{v10, v11}; + /// # struct Runtime {}; + /// # struct Currency {}; + /// type Migrations = (v10::Migration, v11::Migration); + /// ``` + /// + /// If you have a single migration step, you can use a tuple with a single element: + /// ``` + /// use pallet_contracts::migration::v10; + /// # struct Runtime {}; + /// # struct Currency {}; + /// type Migrations = (v10::Migration,); + /// ``` + type Migrations: MigrateSequence; + + /// # Note + /// For most production chains, it's recommended to use the `()` implementation of this + /// trait. This implementation offers additional logging when the log target + /// "runtime::contracts" is set to trace. + #[pallet::no_default_bounds] + type Debug: Debugger; + + /// Type that bundles together all the runtime configurable interface types. + /// + /// This is not a real config. We just mention the type here as constant so that + /// its type appears in the metadata. Only valid value is `()`. + #[pallet::constant] + #[pallet::no_default_bounds] + type Environment: Get>; + + /// The version of the HostFn APIs that are available in the runtime. + /// + /// Only valid value is `()`. + #[pallet::constant] + #[pallet::no_default_bounds] + type ApiVersion: Get; + + /// A type that exposes XCM APIs, allowing contracts to interact with other parachains, and + /// execute XCM programs. + #[pallet::no_default_bounds] + type Xcm: xcm_builder::Controller< + OriginFor, + ::RuntimeCall, + BlockNumberFor, + >; + } + + /// Container for different types that implement [`DefaultConfig`]` of this pallet. + pub mod config_preludes { + use super::*; + use frame_support::{ + derive_impl, + traits::{ConstBool, ConstU32}, + }; + use frame_system::EnsureSigned; + use sp_core::parameter_types; + + type AccountId = sp_runtime::AccountId32; + type Balance = u64; + const UNITS: Balance = 10_000_000_000; + const CENTS: Balance = UNITS / 100; + + const fn deposit(items: u32, bytes: u32) -> Balance { + items as Balance * 1 * CENTS + (bytes as Balance) * 1 * CENTS + } + + parameter_types! { + pub const DepositPerItem: Balance = deposit(1, 0); + pub const DepositPerByte: Balance = deposit(0, 1); + pub const DefaultDepositLimit: Balance = deposit(1024, 1024 * 1024); + pub const CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(0); + pub const MaxDelegateDependencies: u32 = 32; + } + + /// A type providing default configurations for this pallet in testing environment. + pub struct TestDefaultConfig; + + impl Randomness for TestDefaultConfig { + fn random(_subject: &[u8]) -> (Output, BlockNumber) { + unimplemented!("No default `random` implementation in `TestDefaultConfig`, provide a custom `T::Randomness` type.") + } + } + + impl Time for TestDefaultConfig { + type Moment = u64; + fn now() -> Self::Moment { + unimplemented!("No default `now` implementation in `TestDefaultConfig` provide a custom `T::Time` type.") + } + } + + impl> Convert for TestDefaultConfig { + fn convert(w: Weight) -> T { + w.ref_time().into() + } + } + + #[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)] + impl frame_system::DefaultConfig for TestDefaultConfig {} + + #[frame_support::register_default_impl(TestDefaultConfig)] + impl DefaultConfig for TestDefaultConfig { + #[inject_runtime_type] + type RuntimeEvent = (); + + #[inject_runtime_type] + type RuntimeHoldReason = (); + + #[inject_runtime_type] + type RuntimeCall = (); + + type AddressGenerator = DefaultAddressGenerator; + type CallFilter = (); + type ChainExtension = (); + type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent; + type DefaultDepositLimit = DefaultDepositLimit; + type DepositPerByte = DepositPerByte; + type DepositPerItem = DepositPerItem; + type MaxCodeLen = ConstU32<{ 123 * 1024 }>; + type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>; + type MaxDelegateDependencies = MaxDelegateDependencies; + type MaxStorageKeyLen = ConstU32<128>; + type MaxTransientStorageSize = ConstU32<{ 1 * 1024 * 1024 }>; + type Migrations = (); + type Time = Self; + type Randomness = Self; + type UnsafeUnstableInterface = ConstBool; + type UploadOrigin = EnsureSigned; + type InstantiateOrigin = EnsureSigned; + type WeightInfo = (); + type WeightPrice = Self; + type Debug = (); + type Environment = (); + type ApiVersion = (); + type Xcm = (); + } + } + + #[pallet::hooks] + impl Hooks> for Pallet { + fn on_idle(_block: BlockNumberFor, limit: Weight) -> Weight { + use migration::MigrateResult::*; + let mut meter = WeightMeter::with_limit(limit); + + loop { + match Migration::::migrate(&mut meter) { + // There is not enough weight to perform a migration. + // We can't do anything more, so we return the used weight. + NoMigrationPerformed | InProgress { steps_done: 0 } => return meter.consumed(), + // Migration is still in progress, we can start the next step. + InProgress { .. } => continue, + // Either no migration is in progress, or we are done with all migrations, we + // can do some more other work with the remaining weight. + Completed | NoMigrationInProgress => break, + } + } + + ContractInfo::::process_deletion_queue_batch(&mut meter); + meter.consumed() + } + + fn integrity_test() { + Migration::::integrity_test(); + + // Total runtime memory limit + let max_runtime_mem: u32 = T::Schedule::get().limits.runtime_memory; + // Memory limits for a single contract: + // Value stack size: 1Mb per contract, default defined in wasmi + const MAX_STACK_SIZE: u32 = 1024 * 1024; + // Heap limit is normally 16 mempages of 64kb each = 1Mb per contract + let max_heap_size = T::Schedule::get().limits.max_memory_size(); + // Max call depth is CallStack::size() + 1 + let max_call_depth = u32::try_from(T::CallStack::size().saturating_add(1)) + .expect("CallStack size is too big"); + // Transient storage uses a BTreeMap, which has overhead compared to the raw size of + // key-value data. To ensure safety, a margin of 2x the raw key-value size is used. + let max_transient_storage_size = T::MaxTransientStorageSize::get() + .checked_mul(2) + .expect("MaxTransientStorageSize is too large"); + // Check that given configured `MaxCodeLen`, runtime heap memory limit can't be broken. + // + // In worst case, the decoded Wasm contract code would be `x16` times larger than the + // encoded one. This is because even a single-byte wasm instruction has 16-byte size in + // wasmi. This gives us `MaxCodeLen*16` safety margin. + // + // Next, the pallet keeps the Wasm blob for each + // contract, hence we add up `MaxCodeLen` to the safety margin. + // + // The inefficiencies of the freeing-bump allocator + // being used in the client for the runtime memory allocations, could lead to possible + // memory allocations for contract code grow up to `x4` times in some extreme cases, + // which gives us total multiplier of `17*4` for `MaxCodeLen`. + // + // That being said, for every contract executed in runtime, at least `MaxCodeLen*17*4` + // memory should be available. Note that maximum allowed heap memory and stack size per + // each contract (stack frame) should also be counted. + // + // The pallet holds transient storage with a size up to `max_transient_storage_size`. + // + // Finally, we allow 50% of the runtime memory to be utilized by the contracts call + // stack, keeping the rest for other facilities, such as PoV, etc. + // + // This gives us the following formula: + // + // `(MaxCodeLen * 17 * 4 + MAX_STACK_SIZE + max_heap_size) * max_call_depth + + // max_transient_storage_size < max_runtime_mem/2` + // + // Hence the upper limit for the `MaxCodeLen` can be defined as follows: + let code_len_limit = max_runtime_mem + .saturating_div(2) + .saturating_sub(max_transient_storage_size) + .saturating_div(max_call_depth) + .saturating_sub(max_heap_size) + .saturating_sub(MAX_STACK_SIZE) + .saturating_div(17 * 4); + + assert!( + T::MaxCodeLen::get() < code_len_limit, + "Given `CallStack` height {:?}, `MaxCodeLen` should be set less than {:?} \ + (current value is {:?}), to avoid possible runtime oom issues.", + max_call_depth, + code_len_limit, + T::MaxCodeLen::get(), + ); + + // Debug buffer should at least be large enough to accommodate a simple error message + const MIN_DEBUG_BUF_SIZE: u32 = 256; + assert!( + T::MaxDebugBufferLen::get() > MIN_DEBUG_BUF_SIZE, + "Debug buffer should have minimum size of {} (current setting is {})", + MIN_DEBUG_BUF_SIZE, + T::MaxDebugBufferLen::get(), + ); + + // Validators are configured to be able to use more memory than block builders. This is + // because in addition to `max_runtime_mem` they need to hold additional data in + // memory: PoV in multiple copies (1x encoded + 2x decoded) and all storage which + // includes emitted events. The assumption is that storage/events size + // can be a maximum of half of the validator runtime memory - max_runtime_mem. + let max_block_ref_time = T::BlockWeights::get() + .get(DispatchClass::Normal) + .max_total + .unwrap_or_else(|| T::BlockWeights::get().max_block) + .ref_time(); + let max_payload_size = T::Schedule::get().limits.payload_len; + let max_key_size = + Key::::try_from_var(alloc::vec![0u8; T::MaxStorageKeyLen::get() as usize]) + .expect("Key of maximal size shall be created") + .hash() + .len() as u32; + + // We can use storage to store items using the available block ref_time with the + // `set_storage` host function. + let max_storage_size: u32 = ((max_block_ref_time / + (>::weight(&RuntimeCosts::SetStorage { + new_bytes: max_payload_size, + old_bytes: 0, + }) + .ref_time())) + .saturating_mul(max_payload_size.saturating_add(max_key_size) as u64)) + .try_into() + .expect("Storage size too big"); + + let max_validator_runtime_mem: u32 = T::Schedule::get().limits.validator_runtime_memory; + let storage_size_limit = max_validator_runtime_mem.saturating_sub(max_runtime_mem) / 2; + + assert!( + max_storage_size < storage_size_limit, + "Maximal storage size {} exceeds the storage limit {}", + max_storage_size, + storage_size_limit + ); + + // We can use storage to store events using the available block ref_time with the + // `deposit_event` host function. The overhead of stored events, which is around 100B, + // is not taken into account to simplify calculations, as it does not change much. + let max_events_size: u32 = ((max_block_ref_time / + (>::weight(&RuntimeCosts::DepositEvent { + num_topic: 0, + len: max_payload_size, + }) + .ref_time())) + .saturating_mul(max_payload_size as u64)) + .try_into() + .expect("Events size too big"); + + assert!( + max_events_size < storage_size_limit, + "Maximal events size {} exceeds the events limit {}", + max_events_size, + storage_size_limit + ); + } + } + + #[pallet::call] + impl Pallet + where + as HasCompact>::Type: Clone + Eq + PartialEq + Debug + TypeInfo + Encode, + { + /// Deprecated version if [`Self::call`] for use in an in-storage `Call`. + #[pallet::call_index(0)] + #[pallet::weight(T::WeightInfo::call().saturating_add(>::compat_weight_limit(*gas_limit)))] + #[allow(deprecated)] + #[deprecated(note = "1D weight is used in this extrinsic, please migrate to `call`")] + pub fn call_old_weight( + origin: OriginFor, + dest: AccountIdLookupOf, + #[pallet::compact] value: BalanceOf, + #[pallet::compact] gas_limit: OldWeight, + storage_deposit_limit: Option< as codec::HasCompact>::Type>, + data: Vec, + ) -> DispatchResultWithPostInfo { + Self::call( + origin, + dest, + value, + >::compat_weight_limit(gas_limit), + storage_deposit_limit, + data, + ) + } + + /// Deprecated version if [`Self::instantiate_with_code`] for use in an in-storage `Call`. + #[pallet::call_index(1)] + #[pallet::weight( + T::WeightInfo::instantiate_with_code(code.len() as u32, data.len() as u32, salt.len() as u32) + .saturating_add(>::compat_weight_limit(*gas_limit)) + )] + #[allow(deprecated)] + #[deprecated( + note = "1D weight is used in this extrinsic, please migrate to `instantiate_with_code`" + )] + pub fn instantiate_with_code_old_weight( + origin: OriginFor, + #[pallet::compact] value: BalanceOf, + #[pallet::compact] gas_limit: OldWeight, + storage_deposit_limit: Option< as codec::HasCompact>::Type>, + code: Vec, + data: Vec, + salt: Vec, + ) -> DispatchResultWithPostInfo { + Self::instantiate_with_code( + origin, + value, + >::compat_weight_limit(gas_limit), + storage_deposit_limit, + code, + data, + salt, + ) + } + + /// Deprecated version if [`Self::instantiate`] for use in an in-storage `Call`. + #[pallet::call_index(2)] + #[pallet::weight( + T::WeightInfo::instantiate(data.len() as u32, salt.len() as u32).saturating_add(>::compat_weight_limit(*gas_limit)) + )] + #[allow(deprecated)] + #[deprecated(note = "1D weight is used in this extrinsic, please migrate to `instantiate`")] + pub fn instantiate_old_weight( + origin: OriginFor, + #[pallet::compact] value: BalanceOf, + #[pallet::compact] gas_limit: OldWeight, + storage_deposit_limit: Option< as codec::HasCompact>::Type>, + code_hash: CodeHash, + data: Vec, + salt: Vec, + ) -> DispatchResultWithPostInfo { + Self::instantiate( + origin, + value, + >::compat_weight_limit(gas_limit), + storage_deposit_limit, + code_hash, + data, + salt, + ) + } + + /// Upload new `code` without instantiating a contract from it. + /// + /// If the code does not already exist a deposit is reserved from the caller + /// and unreserved only when [`Self::remove_code`] is called. The size of the reserve + /// depends on the size of the supplied `code`. + /// + /// If the code already exists in storage it will still return `Ok` and upgrades + /// the in storage version to the current + /// [`InstructionWeights::version`](InstructionWeights). + /// + /// - `determinism`: If this is set to any other value but [`Determinism::Enforced`] then + /// the only way to use this code is to delegate call into it from an offchain execution. + /// Set to [`Determinism::Enforced`] if in doubt. + /// + /// # Note + /// + /// Anyone can instantiate a contract from any uploaded code and thus prevent its removal. + /// To avoid this situation a constructor could employ access control so that it can + /// only be instantiated by permissioned entities. The same is true when uploading + /// through [`Self::instantiate_with_code`]. + /// + /// Use [`Determinism::Relaxed`] exclusively for non-deterministic code. If the uploaded + /// code is deterministic, specifying [`Determinism::Relaxed`] will be disregarded and + /// result in higher gas costs. + #[pallet::call_index(3)] + #[pallet::weight( + match determinism { + Determinism::Enforced => T::WeightInfo::upload_code_determinism_enforced(code.len() as u32), + Determinism::Relaxed => T::WeightInfo::upload_code_determinism_relaxed(code.len() as u32), + } + )] + pub fn upload_code( + origin: OriginFor, + code: Vec, + storage_deposit_limit: Option< as codec::HasCompact>::Type>, + determinism: Determinism, + ) -> DispatchResult { + Migration::::ensure_migrated()?; + let origin = T::UploadOrigin::ensure_origin(origin)?; + Self::bare_upload_code(origin, code, storage_deposit_limit.map(Into::into), determinism) + .map(|_| ()) + } + + /// Remove the code stored under `code_hash` and refund the deposit to its owner. + /// + /// A code can only be removed by its original uploader (its owner) and only if it is + /// not used by any contract. + #[pallet::call_index(4)] + #[pallet::weight(T::WeightInfo::remove_code())] + pub fn remove_code( + origin: OriginFor, + code_hash: CodeHash, + ) -> DispatchResultWithPostInfo { + Migration::::ensure_migrated()?; + let origin = ensure_signed(origin)?; + >::remove(&origin, code_hash)?; + // we waive the fee because removing unused code is beneficial + Ok(Pays::No.into()) + } + + /// Privileged function that changes the code of an existing contract. + /// + /// This takes care of updating refcounts and all other necessary operations. Returns + /// an error if either the `code_hash` or `dest` do not exist. + /// + /// # Note + /// + /// This does **not** change the address of the contract in question. This means + /// that the contract address is no longer derived from its code hash after calling + /// this dispatchable. + #[pallet::call_index(5)] + #[pallet::weight(T::WeightInfo::set_code())] + pub fn set_code( + origin: OriginFor, + dest: AccountIdLookupOf, + code_hash: CodeHash, + ) -> DispatchResult { + Migration::::ensure_migrated()?; + ensure_root(origin)?; + let dest = T::Lookup::lookup(dest)?; + >::try_mutate(&dest, |contract| { + let contract = if let Some(contract) = contract { + contract + } else { + return Err(>::ContractNotFound.into()) + }; + >>::increment_refcount(code_hash)?; + >>::decrement_refcount(contract.code_hash); + Self::deposit_event(Event::ContractCodeUpdated { + contract: dest.clone(), + new_code_hash: code_hash, + old_code_hash: contract.code_hash, + }); + contract.code_hash = code_hash; + Ok(()) + }) + } + + /// Makes a call to an account, optionally transferring some balance. + /// + /// # Parameters + /// + /// * `dest`: Address of the contract to call. + /// * `value`: The balance to transfer from the `origin` to `dest`. + /// * `gas_limit`: The gas limit enforced when executing the constructor. + /// * `storage_deposit_limit`: The maximum amount of balance that can be charged from the + /// caller to pay for the storage consumed. + /// * `data`: The input data to pass to the contract. + /// + /// * If the account is a smart-contract account, the associated code will be + /// executed and any value will be transferred. + /// * If the account is a regular account, any value will be transferred. + /// * If no account exists and the call value is not less than `existential_deposit`, + /// a regular account will be created and any value will be transferred. + #[pallet::call_index(6)] + #[pallet::weight(T::WeightInfo::call().saturating_add(*gas_limit))] + pub fn call( + origin: OriginFor, + dest: AccountIdLookupOf, + #[pallet::compact] value: BalanceOf, + gas_limit: Weight, + storage_deposit_limit: Option< as codec::HasCompact>::Type>, + data: Vec, + ) -> DispatchResultWithPostInfo { + Migration::::ensure_migrated()?; + let common = CommonInput { + origin: Origin::from_runtime_origin(origin)?, + value, + data, + gas_limit: gas_limit.into(), + storage_deposit_limit: storage_deposit_limit.map(Into::into), + debug_message: None, + }; + let dest = T::Lookup::lookup(dest)?; + let mut output = + CallInput:: { dest, determinism: Determinism::Enforced }.run_guarded(common); + if let Ok(retval) = &output.result { + if retval.did_revert() { + output.result = Err(>::ContractReverted.into()); + } + } + output.gas_meter.into_dispatch_result(output.result, T::WeightInfo::call()) + } + + /// Instantiates a new contract from the supplied `code` optionally transferring + /// some balance. + /// + /// This dispatchable has the same effect as calling [`Self::upload_code`] + + /// [`Self::instantiate`]. Bundling them together provides efficiency gains. Please + /// also check the documentation of [`Self::upload_code`]. + /// + /// # Parameters + /// + /// * `value`: The balance to transfer from the `origin` to the newly created contract. + /// * `gas_limit`: The gas limit enforced when executing the constructor. + /// * `storage_deposit_limit`: The maximum amount of balance that can be charged/reserved + /// from the caller to pay for the storage consumed. + /// * `code`: The contract code to deploy in raw bytes. + /// * `data`: The input data to pass to the contract constructor. + /// * `salt`: Used for the address derivation. See [`Pallet::contract_address`]. + /// + /// Instantiation is executed as follows: + /// + /// - The supplied `code` is deployed, and a `code_hash` is created for that code. + /// - If the `code_hash` already exists on the chain the underlying `code` will be shared. + /// - The destination address is computed based on the sender, code_hash and the salt. + /// - The smart-contract account is created at the computed address. + /// - The `value` is transferred to the new account. + /// - The `deploy` function is executed in the context of the newly-created account. + #[pallet::call_index(7)] + #[pallet::weight( + T::WeightInfo::instantiate_with_code(code.len() as u32, data.len() as u32, salt.len() as u32) + .saturating_add(*gas_limit) + )] + pub fn instantiate_with_code( + origin: OriginFor, + #[pallet::compact] value: BalanceOf, + gas_limit: Weight, + storage_deposit_limit: Option< as codec::HasCompact>::Type>, + code: Vec, + data: Vec, + salt: Vec, + ) -> DispatchResultWithPostInfo { + Migration::::ensure_migrated()?; + + // These two origins will usually be the same; however, we treat them as separate since + // it is possible for the `Success` value of `UploadOrigin` and `InstantiateOrigin` to + // differ. + let upload_origin = T::UploadOrigin::ensure_origin(origin.clone())?; + let instantiate_origin = T::InstantiateOrigin::ensure_origin(origin)?; + + let code_len = code.len() as u32; + + let (module, upload_deposit) = Self::try_upload_code( + upload_origin, + code, + storage_deposit_limit.clone().map(Into::into), + Determinism::Enforced, + None, + )?; + + // Reduces the storage deposit limit by the amount that was reserved for the upload. + let storage_deposit_limit = + storage_deposit_limit.map(|limit| limit.into().saturating_sub(upload_deposit)); + + let data_len = data.len() as u32; + let salt_len = salt.len() as u32; + let common = CommonInput { + origin: Origin::from_account_id(instantiate_origin), + value, + data, + gas_limit, + storage_deposit_limit, + debug_message: None, + }; + + let mut output = + InstantiateInput:: { code: WasmCode::Wasm(module), salt }.run_guarded(common); + if let Ok(retval) = &output.result { + if retval.1.did_revert() { + output.result = Err(>::ContractReverted.into()); + } + } + + output.gas_meter.into_dispatch_result( + output.result.map(|(_address, output)| output), + T::WeightInfo::instantiate_with_code(code_len, data_len, salt_len), + ) + } + + /// Instantiates a contract from a previously deployed wasm binary. + /// + /// This function is identical to [`Self::instantiate_with_code`] but without the + /// code deployment step. Instead, the `code_hash` of an on-chain deployed wasm binary + /// must be supplied. + #[pallet::call_index(8)] + #[pallet::weight( + T::WeightInfo::instantiate(data.len() as u32, salt.len() as u32).saturating_add(*gas_limit) + )] + pub fn instantiate( + origin: OriginFor, + #[pallet::compact] value: BalanceOf, + gas_limit: Weight, + storage_deposit_limit: Option< as codec::HasCompact>::Type>, + code_hash: CodeHash, + data: Vec, + salt: Vec, + ) -> DispatchResultWithPostInfo { + Migration::::ensure_migrated()?; + let origin = T::InstantiateOrigin::ensure_origin(origin)?; + let data_len = data.len() as u32; + let salt_len = salt.len() as u32; + let common = CommonInput { + origin: Origin::from_account_id(origin), + value, + data, + gas_limit, + storage_deposit_limit: storage_deposit_limit.map(Into::into), + debug_message: None, + }; + let mut output = InstantiateInput:: { code: WasmCode::CodeHash(code_hash), salt } + .run_guarded(common); + if let Ok(retval) = &output.result { + if retval.1.did_revert() { + output.result = Err(>::ContractReverted.into()); + } + } + output.gas_meter.into_dispatch_result( + output.result.map(|(_address, output)| output), + T::WeightInfo::instantiate(data_len, salt_len), + ) + } + + /// When a migration is in progress, this dispatchable can be used to run migration steps. + /// Calls that contribute to advancing the migration have their fees waived, as it's helpful + /// for the chain. Note that while the migration is in progress, the pallet will also + /// leverage the `on_idle` hooks to run migration steps. + #[pallet::call_index(9)] + #[pallet::weight(T::WeightInfo::migrate().saturating_add(*weight_limit))] + pub fn migrate(origin: OriginFor, weight_limit: Weight) -> DispatchResultWithPostInfo { + use migration::MigrateResult::*; + ensure_signed(origin)?; + + let weight_limit = weight_limit.saturating_add(T::WeightInfo::migrate()); + let mut meter = WeightMeter::with_limit(weight_limit); + let result = Migration::::migrate(&mut meter); + + match result { + Completed => Ok(PostDispatchInfo { + actual_weight: Some(meter.consumed()), + pays_fee: Pays::No, + }), + InProgress { steps_done, .. } if steps_done > 0 => Ok(PostDispatchInfo { + actual_weight: Some(meter.consumed()), + pays_fee: Pays::No, + }), + InProgress { .. } => Ok(PostDispatchInfo { + actual_weight: Some(meter.consumed()), + pays_fee: Pays::Yes, + }), + NoMigrationInProgress | NoMigrationPerformed => { + let err: DispatchError = >::NoMigrationPerformed.into(); + Err(err.with_weight(meter.consumed())) + }, + } + } + } + + #[pallet::event] + pub enum Event { + /// Contract deployed by address at the specified address. + Instantiated { deployer: T::AccountId, contract: T::AccountId }, + + /// Contract has been removed. + /// + /// # Note + /// + /// The only way for a contract to be removed and emitting this event is by calling + /// `seal_terminate`. + Terminated { + /// The contract that was terminated. + contract: T::AccountId, + /// The account that received the contracts remaining balance + beneficiary: T::AccountId, + }, + + /// Code with the specified hash has been stored. + CodeStored { code_hash: T::Hash, deposit_held: BalanceOf, uploader: T::AccountId }, + + /// A custom event emitted by the contract. + ContractEmitted { + /// The contract that emitted the event. + contract: T::AccountId, + /// Data supplied by the contract. Metadata generated during contract compilation + /// is needed to decode it. + data: Vec, + }, + + /// A code with the specified hash was removed. + CodeRemoved { code_hash: T::Hash, deposit_released: BalanceOf, remover: T::AccountId }, + + /// A contract's code was updated. + ContractCodeUpdated { + /// The contract that has been updated. + contract: T::AccountId, + /// New code hash that was set for the contract. + new_code_hash: T::Hash, + /// Previous code hash of the contract. + old_code_hash: T::Hash, + }, + + /// A contract was called either by a plain account or another contract. + /// + /// # Note + /// + /// Please keep in mind that like all events this is only emitted for successful + /// calls. This is because on failure all storage changes including events are + /// rolled back. + Called { + /// The caller of the `contract`. + caller: Origin, + /// The contract that was called. + contract: T::AccountId, + }, + + /// A contract delegate called a code hash. + /// + /// # Note + /// + /// Please keep in mind that like all events this is only emitted for successful + /// calls. This is because on failure all storage changes including events are + /// rolled back. + DelegateCalled { + /// The contract that performed the delegate call and hence in whose context + /// the `code_hash` is executed. + contract: T::AccountId, + /// The code hash that was delegate called. + code_hash: CodeHash, + }, + + /// Some funds have been transferred and held as storage deposit. + StorageDepositTransferredAndHeld { + from: T::AccountId, + to: T::AccountId, + amount: BalanceOf, + }, + + /// Some storage deposit funds have been transferred and released. + StorageDepositTransferredAndReleased { + from: T::AccountId, + to: T::AccountId, + amount: BalanceOf, + }, + } + + #[pallet::error] + pub enum Error { + /// Invalid schedule supplied, e.g. with zero weight of a basic operation. + InvalidSchedule, + /// Invalid combination of flags supplied to `seal_call` or `seal_delegate_call`. + InvalidCallFlags, + /// The executed contract exhausted its gas limit. + OutOfGas, + /// The output buffer supplied to a contract API call was too small. + OutputBufferTooSmall, + /// Performing the requested transfer failed. Probably because there isn't enough + /// free balance in the sender's account. + TransferFailed, + /// Performing a call was denied because the calling depth reached the limit + /// of what is specified in the schedule. + MaxCallDepthReached, + /// No contract was found at the specified address. + ContractNotFound, + /// The code supplied to `instantiate_with_code` exceeds the limit specified in the + /// current schedule. + CodeTooLarge, + /// No code could be found at the supplied code hash. + CodeNotFound, + /// No code info could be found at the supplied code hash. + CodeInfoNotFound, + /// A buffer outside of sandbox memory was passed to a contract API function. + OutOfBounds, + /// Input passed to a contract API function failed to decode as expected type. + DecodingFailed, + /// Contract trapped during execution. + ContractTrapped, + /// The size defined in `T::MaxValueSize` was exceeded. + ValueTooLarge, + /// Termination of a contract is not allowed while the contract is already + /// on the call stack. Can be triggered by `seal_terminate`. + TerminatedWhileReentrant, + /// `seal_call` forwarded this contracts input. It therefore is no longer available. + InputForwarded, + /// The subject passed to `seal_random` exceeds the limit. + RandomSubjectTooLong, + /// The amount of topics passed to `seal_deposit_events` exceeds the limit. + TooManyTopics, + /// The chain does not provide a chain extension. Calling the chain extension results + /// in this error. Note that this usually shouldn't happen as deploying such contracts + /// is rejected. + NoChainExtension, + /// Failed to decode the XCM program. + XCMDecodeFailed, + /// A contract with the same AccountId already exists. + DuplicateContract, + /// A contract self destructed in its constructor. + /// + /// This can be triggered by a call to `seal_terminate`. + TerminatedInConstructor, + /// A call tried to invoke a contract that is flagged as non-reentrant. + /// The only other cause is that a call from a contract into the runtime tried to call back + /// into `pallet-contracts`. This would make the whole pallet reentrant with regard to + /// contract code execution which is not supported. + ReentranceDenied, + /// A contract attempted to invoke a state modifying API while being in read-only mode. + StateChangeDenied, + /// Origin doesn't have enough balance to pay the required storage deposits. + StorageDepositNotEnoughFunds, + /// More storage was created than allowed by the storage deposit limit. + StorageDepositLimitExhausted, + /// Code removal was denied because the code is still in use by at least one contract. + CodeInUse, + /// The contract ran to completion but decided to revert its storage changes. + /// Please note that this error is only returned from extrinsics. When called directly + /// or via RPC an `Ok` will be returned. In this case the caller needs to inspect the flags + /// to determine whether a reversion has taken place. + ContractReverted, + /// The contract's code was found to be invalid during validation. + /// + /// The most likely cause of this is that an API was used which is not supported by the + /// node. This happens if an older node is used with a new version of ink!. Try updating + /// your node to the newest available version. + /// + /// A more detailed error can be found on the node console if debug messages are enabled + /// by supplying `-lruntime::contracts=debug`. + CodeRejected, + /// An indeterministic code was used in a context where this is not permitted. + Indeterministic, + /// A pending migration needs to complete before the extrinsic can be called. + MigrationInProgress, + /// Migrate dispatch call was attempted but no migration was performed. + NoMigrationPerformed, + /// The contract has reached its maximum number of delegate dependencies. + MaxDelegateDependenciesReached, + /// The dependency was not found in the contract's delegate dependencies. + DelegateDependencyNotFound, + /// The contract already depends on the given delegate dependency. + DelegateDependencyAlreadyExists, + /// Can not add a delegate dependency to the code hash of the contract itself. + CannotAddSelfAsDelegateDependency, + /// Can not add more data to transient storage. + OutOfTransientStorage, + } + + /// A reason for the pallet contracts placing a hold on funds. + #[pallet::composite_enum] + pub enum HoldReason { + /// The Pallet has reserved it for storing code on-chain. + CodeUploadDepositReserve, + /// The Pallet has reserved it for storage deposit. + StorageDepositReserve, + } + + /// A mapping from a contract's code hash to its code. + #[pallet::storage] + pub(crate) type PristineCode = StorageMap<_, Identity, CodeHash, CodeVec>; + + /// A mapping from a contract's code hash to its code info. + #[pallet::storage] + pub(crate) type CodeInfoOf = StorageMap<_, Identity, CodeHash, CodeInfo>; + + /// This is a **monotonic** counter incremented on contract instantiation. + /// + /// This is used in order to generate unique trie ids for contracts. + /// The trie id of a new contract is calculated from hash(account_id, nonce). + /// The nonce is required because otherwise the following sequence would lead to + /// a possible collision of storage: + /// + /// 1. Create a new contract. + /// 2. Terminate the contract. + /// 3. Immediately recreate the contract with the same account_id. + /// + /// This is bad because the contents of a trie are deleted lazily and there might be + /// storage of the old instantiation still in it when the new contract is created. Please + /// note that we can't replace the counter by the block number because the sequence above + /// can happen in the same block. We also can't keep the account counter in memory only + /// because storage is the only way to communicate across different extrinsics in the + /// same block. + /// + /// # Note + /// + /// Do not use it to determine the number of contracts. It won't be decremented if + /// a contract is destroyed. + #[pallet::storage] + pub(crate) type Nonce = StorageValue<_, u64, ValueQuery>; + + /// The code associated with a given account. + /// + /// TWOX-NOTE: SAFE since `AccountId` is a secure hash. + #[pallet::storage] + pub(crate) type ContractInfoOf = + StorageMap<_, Twox64Concat, T::AccountId, ContractInfo>; + + /// Evicted contracts that await child trie deletion. + /// + /// Child trie deletion is a heavy operation depending on the amount of storage items + /// stored in said trie. Therefore this operation is performed lazily in `on_idle`. + #[pallet::storage] + pub(crate) type DeletionQueue = StorageMap<_, Twox64Concat, u32, TrieId>; + + /// A pair of monotonic counters used to track the latest contract marked for deletion + /// and the latest deleted contract in queue. + #[pallet::storage] + pub(crate) type DeletionQueueCounter = + StorageValue<_, DeletionQueueManager, ValueQuery>; + + /// A migration can span across multiple blocks. This storage defines a cursor to track the + /// progress of the migration, enabling us to resume from the last completed position. + #[pallet::storage] + pub(crate) type MigrationInProgress = + StorageValue<_, migration::Cursor, OptionQuery>; +} + +/// The type of origins supported by the contracts pallet. +#[derive(Clone, Encode, Decode, PartialEq, TypeInfo, RuntimeDebugNoBound)] +pub enum Origin { + Root, + Signed(T::AccountId), +} + +impl Origin { + /// Creates a new Signed Caller from an AccountId. + pub fn from_account_id(account_id: T::AccountId) -> Self { + Origin::Signed(account_id) + } + /// Creates a new Origin from a `RuntimeOrigin`. + pub fn from_runtime_origin(o: OriginFor) -> Result { + match o.into() { + Ok(RawOrigin::Root) => Ok(Self::Root), + Ok(RawOrigin::Signed(t)) => Ok(Self::Signed(t)), + _ => Err(BadOrigin.into()), + } + } + /// Returns the AccountId of a Signed Origin or an error if the origin is Root. + pub fn account_id(&self) -> Result<&T::AccountId, DispatchError> { + match self { + Origin::Signed(id) => Ok(id), + Origin::Root => Err(DispatchError::RootNotAllowed), + } + } +} + +/// Context of a contract invocation. +struct CommonInput<'a, T: Config> { + origin: Origin, + value: BalanceOf, + data: Vec, + gas_limit: Weight, + storage_deposit_limit: Option>, + debug_message: Option<&'a mut DebugBufferVec>, +} + +/// Input specific to a call into contract. +struct CallInput { + dest: T::AccountId, + determinism: Determinism, +} + +/// Reference to an existing code hash or a new wasm module. +enum WasmCode { + Wasm(WasmBlob), + CodeHash(CodeHash), +} + +/// Input specific to a contract instantiation invocation. +struct InstantiateInput { + code: WasmCode, + salt: Vec, +} + +/// Determines whether events should be collected during execution. +#[derive( + Copy, Clone, PartialEq, Eq, RuntimeDebug, Decode, Encode, MaxEncodedLen, scale_info::TypeInfo, +)] +pub enum CollectEvents { + /// Collect events. + /// + /// # Note + /// + /// Events should only be collected when called off-chain, as this would otherwise + /// collect all the Events emitted in the block so far and put them into the PoV. + /// + /// **Never** use this mode for on-chain execution. + UnsafeCollect, + /// Skip event collection. + Skip, +} + +/// Determines whether debug messages will be collected. +#[derive( + Copy, Clone, PartialEq, Eq, RuntimeDebug, Decode, Encode, MaxEncodedLen, scale_info::TypeInfo, +)] +pub enum DebugInfo { + /// Collect debug messages. + /// # Note + /// + /// This should only ever be set to `UnsafeDebug` when executing as an RPC because + /// it adds allocations and could be abused to drive the runtime into an OOM panic. + UnsafeDebug, + /// Skip collection of debug messages. + Skip, +} + +/// Return type of private helper functions. +struct InternalOutput { + /// The gas meter that was used to execute the call. + gas_meter: GasMeter, + /// The storage deposit used by the call. + storage_deposit: StorageDeposit>, + /// The result of the call. + result: Result, +} + +// Set up a global reference to the boolean flag used for the re-entrancy guard. +environmental!(executing_contract: bool); + +/// Helper trait to wrap contract execution entry points into a single function +/// [`Invokable::run_guarded`]. +trait Invokable: Sized { + /// What is returned as a result of a successful invocation. + type Output; + + /// Single entry point to contract execution. + /// Downstream execution flow is branched by implementations of [`Invokable`] trait: + /// + /// - [`InstantiateInput::run`] runs contract instantiation, + /// - [`CallInput::run`] runs contract call. + /// + /// We enforce a re-entrancy guard here by initializing and checking a boolean flag through a + /// global reference. + fn run_guarded(self, common: CommonInput) -> InternalOutput { + let gas_limit = common.gas_limit; + + // Check whether the origin is allowed here. The logic of the access rules + // is in the `ensure_origin`, this could vary for different implementations of this + // trait. For example, some actions might not allow Root origin as they could require an + // AccountId associated with the origin. + if let Err(e) = self.ensure_origin(common.origin.clone()) { + return InternalOutput { + gas_meter: GasMeter::new(gas_limit), + storage_deposit: Default::default(), + result: Err(ExecError { error: e.into(), origin: ErrorOrigin::Caller }), + } + } + + executing_contract::using_once(&mut false, || { + executing_contract::with(|f| { + // Fail if already entered contract execution + if *f { + return Err(()) + } + // We are entering contract execution + *f = true; + Ok(()) + }) + .expect("Returns `Ok` if called within `using_once`. It is syntactically obvious that this is the case; qed") + .map_or_else( + |_| InternalOutput { + gas_meter: GasMeter::new(gas_limit), + storage_deposit: Default::default(), + result: Err(ExecError { + error: >::ReentranceDenied.into(), + origin: ErrorOrigin::Caller, + }), + }, + // Enter contract call. + |_| self.run(common, GasMeter::new(gas_limit)), + ) + }) + } + + /// Method that does the actual call to a contract. It can be either a call to a deployed + /// contract or a instantiation of a new one. + /// + /// Called by dispatchables and public functions through the [`Invokable::run_guarded`]. + fn run(self, common: CommonInput, gas_meter: GasMeter) + -> InternalOutput; + + /// This method ensures that the given `origin` is allowed to invoke the current `Invokable`. + /// + /// Called by dispatchables and public functions through the [`Invokable::run_guarded`]. + fn ensure_origin(&self, origin: Origin) -> Result<(), DispatchError>; +} + +impl Invokable for CallInput { + type Output = ExecReturnValue; + + fn run( + self, + common: CommonInput, + mut gas_meter: GasMeter, + ) -> InternalOutput { + let CallInput { dest, determinism } = self; + let CommonInput { origin, value, data, debug_message, .. } = common; + let mut storage_meter = + match StorageMeter::new(&origin, common.storage_deposit_limit, common.value) { + Ok(meter) => meter, + Err(err) => + return InternalOutput { + result: Err(err.into()), + gas_meter, + storage_deposit: Default::default(), + }, + }; + let schedule = T::Schedule::get(); + let result = ExecStack::>::run_call( + origin.clone(), + dest.clone(), + &mut gas_meter, + &mut storage_meter, + &schedule, + value, + data.clone(), + debug_message, + determinism, + ); + + match storage_meter.try_into_deposit(&origin) { + Ok(storage_deposit) => InternalOutput { gas_meter, storage_deposit, result }, + Err(err) => InternalOutput { + gas_meter, + storage_deposit: Default::default(), + result: Err(err.into()), + }, + } + } + + fn ensure_origin(&self, _origin: Origin) -> Result<(), DispatchError> { + Ok(()) + } +} + +impl Invokable for InstantiateInput { + type Output = (AccountIdOf, ExecReturnValue); + + fn run( + self, + common: CommonInput, + mut gas_meter: GasMeter, + ) -> InternalOutput { + let mut storage_deposit = Default::default(); + let try_exec = || { + let schedule = T::Schedule::get(); + let InstantiateInput { salt, .. } = self; + let CommonInput { origin: contract_origin, .. } = common; + let origin = contract_origin.account_id()?; + + let executable = match self.code { + WasmCode::Wasm(module) => module, + WasmCode::CodeHash(code_hash) => WasmBlob::from_storage(code_hash, &mut gas_meter)?, + }; + + let contract_origin = Origin::from_account_id(origin.clone()); + let mut storage_meter = + StorageMeter::new(&contract_origin, common.storage_deposit_limit, common.value)?; + let CommonInput { value, data, debug_message, .. } = common; + let result = ExecStack::>::run_instantiate( + origin.clone(), + executable, + &mut gas_meter, + &mut storage_meter, + &schedule, + value, + data.clone(), + &salt, + debug_message, + ); + + storage_deposit = storage_meter.try_into_deposit(&contract_origin)?; + result + }; + InternalOutput { result: try_exec(), gas_meter, storage_deposit } + } + + fn ensure_origin(&self, origin: Origin) -> Result<(), DispatchError> { + match origin { + Origin::Signed(_) => Ok(()), + Origin::Root => Err(DispatchError::RootNotAllowed), + } + } +} + +macro_rules! ensure_no_migration_in_progress { + () => { + if Migration::::in_progress() { + return ContractResult { + gas_consumed: Zero::zero(), + gas_required: Zero::zero(), + storage_deposit: Default::default(), + debug_message: Vec::new(), + result: Err(Error::::MigrationInProgress.into()), + events: None, + } + } + }; +} + +impl Pallet { + /// Perform a call to a specified contract. + /// + /// This function is similar to [`Self::call`], but doesn't perform any address lookups + /// and better suitable for calling directly from Rust. + /// + /// # Note + /// + /// If `debug` is set to `DebugInfo::UnsafeDebug` it returns additional human readable debugging + /// information. + /// + /// If `collect_events` is set to `CollectEvents::UnsafeCollect` it collects all the Events + /// emitted in the block so far and the ones emitted during the execution of this contract. + pub fn bare_call( + origin: T::AccountId, + dest: T::AccountId, + value: BalanceOf, + gas_limit: Weight, + storage_deposit_limit: Option>, + data: Vec, + debug: DebugInfo, + collect_events: CollectEvents, + determinism: Determinism, + ) -> ContractExecResult, EventRecordOf> { + ensure_no_migration_in_progress!(); + + let mut debug_message = if matches!(debug, DebugInfo::UnsafeDebug) { + Some(DebugBufferVec::::default()) + } else { + None + }; + let origin = Origin::from_account_id(origin); + let common = CommonInput { + origin, + value, + data, + gas_limit, + storage_deposit_limit, + debug_message: debug_message.as_mut(), + }; + let output = CallInput:: { dest, determinism }.run_guarded(common); + let events = if matches!(collect_events, CollectEvents::UnsafeCollect) { + Some(System::::read_events_no_consensus().map(|e| *e).collect()) + } else { + None + }; + + ContractExecResult { + result: output.result.map_err(|r| r.error), + gas_consumed: output.gas_meter.gas_consumed(), + gas_required: output.gas_meter.gas_required(), + storage_deposit: output.storage_deposit, + debug_message: debug_message.unwrap_or_default().to_vec(), + events, + } + } + + /// Instantiate a new contract. + /// + /// This function is similar to [`Self::instantiate`], but doesn't perform any address lookups + /// and better suitable for calling directly from Rust. + /// + /// It returns the execution result, account id and the amount of used weight. + /// + /// # Note + /// + /// If `debug` is set to `DebugInfo::UnsafeDebug` it returns additional human readable debugging + /// information. + /// + /// If `collect_events` is set to `CollectEvents::UnsafeCollect` it collects all the Events + /// emitted in the block so far. + pub fn bare_instantiate( + origin: T::AccountId, + value: BalanceOf, + gas_limit: Weight, + mut storage_deposit_limit: Option>, + code: Code>, + data: Vec, + salt: Vec, + debug: DebugInfo, + collect_events: CollectEvents, + ) -> ContractInstantiateResult, EventRecordOf> { + ensure_no_migration_in_progress!(); + + let mut debug_message = if debug == DebugInfo::UnsafeDebug { + Some(DebugBufferVec::::default()) + } else { + None + }; + // collect events if CollectEvents is UnsafeCollect + let events = || { + if collect_events == CollectEvents::UnsafeCollect { + Some(System::::read_events_no_consensus().map(|e| *e).collect()) + } else { + None + } + }; + + let (code, upload_deposit): (WasmCode, BalanceOf) = match code { + Code::Upload(code) => { + let result = Self::try_upload_code( + origin.clone(), + code, + storage_deposit_limit.map(Into::into), + Determinism::Enforced, + debug_message.as_mut(), + ); + + let (module, deposit) = match result { + Ok(result) => result, + Err(error) => + return ContractResult { + gas_consumed: Zero::zero(), + gas_required: Zero::zero(), + storage_deposit: Default::default(), + debug_message: debug_message.unwrap_or(Default::default()).into(), + result: Err(error), + events: events(), + }, + }; + + storage_deposit_limit = + storage_deposit_limit.map(|l| l.saturating_sub(deposit.into())); + (WasmCode::Wasm(module), deposit) + }, + Code::Existing(hash) => (WasmCode::CodeHash(hash), Default::default()), + }; + + let common = CommonInput { + origin: Origin::from_account_id(origin), + value, + data, + gas_limit, + storage_deposit_limit, + debug_message: debug_message.as_mut(), + }; + + let output = InstantiateInput:: { code, salt }.run_guarded(common); + ContractInstantiateResult { + result: output + .result + .map(|(account_id, result)| InstantiateReturnValue { result, account_id }) + .map_err(|e| e.error), + gas_consumed: output.gas_meter.gas_consumed(), + gas_required: output.gas_meter.gas_required(), + storage_deposit: output + .storage_deposit + .saturating_add(&StorageDeposit::Charge(upload_deposit)), + debug_message: debug_message.unwrap_or_default().to_vec(), + events: events(), + } + } + + /// Upload new code without instantiating a contract from it. + /// + /// This function is similar to [`Self::upload_code`], but doesn't perform any address lookups + /// and better suitable for calling directly from Rust. + pub fn bare_upload_code( + origin: T::AccountId, + code: Vec, + storage_deposit_limit: Option>, + determinism: Determinism, + ) -> CodeUploadResult, BalanceOf> { + Migration::::ensure_migrated()?; + let (module, deposit) = + Self::try_upload_code(origin, code, storage_deposit_limit, determinism, None)?; + Ok(CodeUploadReturnValue { code_hash: *module.code_hash(), deposit }) + } + + /// Uploads new code and returns the Wasm blob and deposit amount collected. + fn try_upload_code( + origin: T::AccountId, + code: Vec, + storage_deposit_limit: Option>, + determinism: Determinism, + mut debug_message: Option<&mut DebugBufferVec>, + ) -> Result<(WasmBlob, BalanceOf), DispatchError> { + let schedule = T::Schedule::get(); + let mut module = + WasmBlob::from_code(code, &schedule, origin, determinism).map_err(|(err, msg)| { + debug_message.as_mut().map(|d| d.try_extend(msg.bytes())); + err + })?; + let deposit = module.store_code()?; + if let Some(storage_deposit_limit) = storage_deposit_limit { + ensure!(storage_deposit_limit >= deposit, >::StorageDepositLimitExhausted); + } + + Ok((module, deposit)) + } + + /// Query storage of a specified contract under a specified key. + pub fn get_storage(address: T::AccountId, key: Vec) -> GetStorageResult { + if Migration::::in_progress() { + return Err(ContractAccessError::MigrationInProgress) + } + let contract_info = + ContractInfoOf::::get(&address).ok_or(ContractAccessError::DoesntExist)?; + + let maybe_value = contract_info.read( + &Key::::try_from_var(key) + .map_err(|_| ContractAccessError::KeyDecodingFailed)? + .into(), + ); + Ok(maybe_value) + } + + /// Determine the address of a contract. + /// + /// This is the address generation function used by contract instantiation. See + /// [`DefaultAddressGenerator`] for the default implementation. + pub fn contract_address( + deploying_address: &T::AccountId, + code_hash: &CodeHash, + input_data: &[u8], + salt: &[u8], + ) -> T::AccountId { + T::AddressGenerator::contract_address(deploying_address, code_hash, input_data, salt) + } + + /// Returns the code hash of the contract specified by `account` ID. + pub fn code_hash(account: &AccountIdOf) -> Option> { + ContractInfo::::load_code_hash(account) + } + + /// Store code for benchmarks which does not validate the code. + #[cfg(feature = "runtime-benchmarks")] + fn store_code_raw( + code: Vec, + owner: T::AccountId, + ) -> frame_support::dispatch::DispatchResult { + let schedule = T::Schedule::get(); + WasmBlob::::from_code_unchecked(code, &schedule, owner)?.store_code()?; + Ok(()) + } + + /// Deposit a pallet contracts event. + fn deposit_event(event: Event) { + >::deposit_event(::RuntimeEvent::from(event)) + } + + /// Deposit a pallet contracts indexed event. + fn deposit_indexed_event(topics: Vec, event: Event) { + >::deposit_event_indexed( + &topics, + ::RuntimeEvent::from(event).into(), + ) + } + + /// Return the existential deposit of [`Config::Currency`]. + fn min_balance() -> BalanceOf { + >>::minimum_balance() + } + + /// Convert gas_limit from 1D Weight to a 2D Weight. + /// + /// Used by backwards compatible extrinsics. We cannot just set the proof_size weight limit to + /// zero or an old `Call` will just fail with OutOfGas. + fn compat_weight_limit(gas_limit: OldWeight) -> Weight { + Weight::from_parts(gas_limit, u64::from(T::MaxCodeLen::get()) * 2) + } +} + +sp_api::decl_runtime_apis! { + /// The API used to dry-run contract interactions. + #[api_version(2)] + pub trait ContractsApi where + AccountId: Codec, + Balance: Codec, + BlockNumber: Codec, + Hash: Codec, + EventRecord: Codec, + { + /// Perform a call from a specified account to a given contract. + /// + /// See [`crate::Pallet::bare_call`]. + fn call( + origin: AccountId, + dest: AccountId, + value: Balance, + gas_limit: Option, + storage_deposit_limit: Option, + input_data: Vec, + ) -> ContractExecResult; + + /// Instantiate a new contract. + /// + /// See `[crate::Pallet::bare_instantiate]`. + fn instantiate( + origin: AccountId, + value: Balance, + gas_limit: Option, + storage_deposit_limit: Option, + code: Code, + data: Vec, + salt: Vec, + ) -> ContractInstantiateResult; + + /// Upload new code without instantiating a contract from it. + /// + /// See [`crate::Pallet::bare_upload_code`]. + fn upload_code( + origin: AccountId, + code: Vec, + storage_deposit_limit: Option, + determinism: Determinism, + ) -> CodeUploadResult; + + /// Query a given storage key in a given contract. + /// + /// Returns `Ok(Some(Vec))` if the storage value exists under the given key in the + /// specified account and `Ok(None)` if it doesn't. If the account specified by the address + /// doesn't exist, or doesn't have a contract then `Err` is returned. + fn get_storage( + address: AccountId, + key: Vec, + ) -> GetStorageResult; + } +} diff --git a/pallets/contracts/src/migration.rs b/pallets/contracts/src/migration.rs new file mode 100644 index 00000000..29ac74d0 --- /dev/null +++ b/pallets/contracts/src/migration.rs @@ -0,0 +1,658 @@ +// This file is part of Substrate. + +// 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. + +//! Multi-block Migration framework for pallet-contracts. +//! +//! This module allows us to define a migration as a sequence of [`MigrationStep`]s that can be +//! executed across multiple blocks. +//! +//! # Usage +//! +//! A migration step is defined under `src/migration/vX.rs`, where `X` is the version number. +//! For example, `vX.rs` defines a migration from version `X - 1` to version `X`. +//! +//! ## Example: +//! +//! To configure a migration to `v11` for a runtime using `v10` of pallet-contracts on the chain, +//! you would set the `Migrations` type as follows: +//! +//! ``` +//! use pallet_contracts::migration::{v10, v11}; +//! # pub enum Runtime {}; +//! # struct Currency; +//! type Migrations = (v10::Migration, v11::Migration); +//! ``` +//! +//! ## Notes: +//! +//! - Migrations should always be tested with `try-runtime` before being deployed. +//! - By testing with `try-runtime` against a live network, you ensure that all migration steps work +//! and that you have included the required steps. +//! +//! ## Low Level / Implementation Details +//! +//! When a migration starts and [`OnRuntimeUpgrade::on_runtime_upgrade`] is called, instead of +//! performing the actual migration, we set a custom storage item [`MigrationInProgress`]. +//! This storage item defines a [`Cursor`] for the current migration. +//! +//! If the [`MigrationInProgress`] storage item exists, it means a migration is in progress, and its +//! value holds a cursor for the current migration step. These migration steps are executed during +//! [`Hooks::on_idle`] or when the [`Pallet::migrate`] dispatchable is +//! called. +//! +//! While the migration is in progress, all dispatchables except `migrate`, are blocked, and returns +//! a `MigrationInProgress` error. + +pub mod v09; +pub mod v10; +pub mod v11; +pub mod v12; +pub mod v13; +pub mod v14; +pub mod v15; +pub mod v16; +include!(concat!(env!("OUT_DIR"), "/migration_codegen.rs")); + +use crate::{weights::WeightInfo, Config, Error, MigrationInProgress, Pallet, Weight, LOG_TARGET}; +use codec::{Codec, Decode}; +use core::marker::PhantomData; +use frame_support::{ + pallet_prelude::*, + traits::{ConstU32, OnRuntimeUpgrade}, + weights::WeightMeter, +}; +use sp_runtime::Saturating; + +#[cfg(feature = "try-runtime")] +use alloc::vec::Vec; +#[cfg(feature = "try-runtime")] +use sp_runtime::TryRuntimeError; + +const PROOF_ENCODE: &str = "Tuple::max_encoded_len() < Cursor::max_encoded_len()` is verified in `Self::integrity_test()`; qed"; +const PROOF_DECODE: &str = + "We encode to the same type in this trait only. No other code touches this item; qed"; + +fn invalid_version(version: StorageVersion) -> ! { + panic!("Required migration {version:?} not supported by this runtime. This is a bug."); +} + +/// The cursor used to encode the position (usually the last iterated key) of the current migration +/// step. +pub type Cursor = BoundedVec>; + +/// IsFinished describes whether a migration is finished or not. +pub enum IsFinished { + Yes, + No, +} + +/// A trait that allows to migrate storage from one version to another. +/// +/// The migration is done in steps. The migration is finished when +/// `step()` returns `IsFinished::Yes`. +pub trait MigrationStep: Codec + MaxEncodedLen + Default { + /// Returns the version of the migration. + const VERSION: u16; + + /// Returns the maximum weight that can be consumed in a single step. + fn max_step_weight() -> Weight; + + /// Process one step of the migration. + /// + /// Returns whether the migration is finished. + fn step(&mut self, meter: &mut WeightMeter) -> IsFinished; + + /// Verify that the migration step fits into `Cursor`, and that `max_step_weight` is not greater + /// than `max_block_weight`. + fn integrity_test(max_block_weight: Weight) { + if Self::max_step_weight().any_gt(max_block_weight) { + panic!( + "Invalid max_step_weight for Migration {}. Value should be lower than {}", + Self::VERSION, + max_block_weight + ); + } + + let len = ::max_encoded_len(); + let max = Cursor::bound(); + if len > max { + panic!( + "Migration {} has size {} which is bigger than the maximum of {}", + Self::VERSION, + len, + max, + ); + } + } + + /// Execute some pre-checks prior to running the first step of this migration. + #[cfg(feature = "try-runtime")] + fn pre_upgrade_step() -> Result, TryRuntimeError> { + Ok(Vec::new()) + } + + /// Execute some post-checks after running the last step of this migration. + #[cfg(feature = "try-runtime")] + fn post_upgrade_step(_state: Vec) -> Result<(), TryRuntimeError> { + Ok(()) + } +} + +/// A noop migration that can be used when there is no migration to be done for a given version. +#[doc(hidden)] +#[derive(frame_support::DefaultNoBound, Encode, Decode, MaxEncodedLen)] +pub struct NoopMigration; + +impl MigrationStep for NoopMigration { + const VERSION: u16 = N; + fn max_step_weight() -> Weight { + Weight::zero() + } + fn step(&mut self, _meter: &mut WeightMeter) -> IsFinished { + log::debug!(target: LOG_TARGET, "Noop migration for version {}", N); + IsFinished::Yes + } +} + +mod private { + use crate::migration::MigrationStep; + pub trait Sealed {} + #[impl_trait_for_tuples::impl_for_tuples(10)] + #[tuple_types_custom_trait_bound(MigrationStep)] + impl Sealed for Tuple {} +} + +/// Defines a sequence of migrations. +/// +/// The sequence must be defined by a tuple of migrations, each of which must implement the +/// `MigrationStep` trait. Migrations must be ordered by their versions with no gaps. +pub trait MigrateSequence: private::Sealed { + /// Returns the range of versions that this migrations sequence can handle. + /// Migrations must be ordered by their versions with no gaps. + /// + /// The following code will fail to compile: + /// + /// ```compile_fail + /// # use pallet_contracts::{NoopMigration, MigrateSequence}; + /// let _ = <(NoopMigration<1>, NoopMigration<3>)>::VERSION_RANGE; + /// ``` + /// The following code will compile: + /// ``` + /// # use pallet_contracts::{NoopMigration, MigrateSequence}; + /// let _ = <(NoopMigration<1>, NoopMigration<2>)>::VERSION_RANGE; + /// ``` + const VERSION_RANGE: (u16, u16); + + /// Returns the default cursor for the given version. + fn new(version: StorageVersion) -> Cursor; + + #[cfg(feature = "try-runtime")] + fn pre_upgrade_step(_version: StorageVersion) -> Result, TryRuntimeError> { + Ok(Vec::new()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade_step(_version: StorageVersion, _state: Vec) -> Result<(), TryRuntimeError> { + Ok(()) + } + + /// Execute the migration step until the available weight is consumed. + fn steps(version: StorageVersion, cursor: &[u8], meter: &mut WeightMeter) -> StepResult; + + /// Verify that the migration step fits into `Cursor`, and that `max_step_weight` is not greater + /// than `max_block_weight`. + fn integrity_test(max_block_weight: Weight); + + /// Returns whether migrating from `in_storage` to `target` is supported. + /// + /// A migration is supported if `VERSION_RANGE` is (in_storage + 1, target). + fn is_upgrade_supported(in_storage: StorageVersion, target: StorageVersion) -> bool { + let (low, high) = Self::VERSION_RANGE; + target == high && in_storage + 1 == low + } +} + +/// Performs all necessary migrations based on `StorageVersion`. +/// +/// If `TEST_ALL_STEPS == true` and `try-runtime` is enabled, this will run all the migrations +/// inside `on_runtime_upgrade`. This should be set to false in tests that want to ensure the step +/// by step migration works. +pub struct Migration(PhantomData); + +#[cfg(feature = "try-runtime")] +impl Migration { + fn run_all_steps() -> Result<(), TryRuntimeError> { + let mut meter = &mut WeightMeter::new(); + let name = >::name(); + loop { + let in_progress_version = >::on_chain_storage_version() + 1; + let state = T::Migrations::pre_upgrade_step(in_progress_version)?; + let before = meter.consumed(); + let status = Self::migrate(&mut meter); + log::info!( + target: LOG_TARGET, + "{name}: Migration step {:?} weight = {}", + in_progress_version, + meter.consumed() - before + ); + T::Migrations::post_upgrade_step(in_progress_version, state)?; + if matches!(status, MigrateResult::Completed) { + break + } + } + + let name = >::name(); + log::info!(target: LOG_TARGET, "{name}: Migration steps weight = {}", meter.consumed()); + Ok(()) + } +} + +impl OnRuntimeUpgrade for Migration { + fn on_runtime_upgrade() -> Weight { + let name = >::name(); + let in_code_version = >::in_code_storage_version(); + let on_chain_version = >::on_chain_storage_version(); + + if on_chain_version == in_code_version { + log::warn!( + target: LOG_TARGET, + "{name}: No Migration performed storage_version = latest_version = {:?}", + &on_chain_version + ); + return T::WeightInfo::on_runtime_upgrade_noop() + } + + // In case a migration is already in progress we create the next migration + // (if any) right when the current one finishes. + if Self::in_progress() { + log::warn!( + target: LOG_TARGET, + "{name}: Migration already in progress {:?}", + &on_chain_version + ); + + return T::WeightInfo::on_runtime_upgrade_in_progress() + } + + log::info!( + target: LOG_TARGET, + "{name}: Upgrading storage from {on_chain_version:?} to {in_code_version:?}.", + ); + + let cursor = T::Migrations::new(on_chain_version + 1); + MigrationInProgress::::set(Some(cursor)); + + #[cfg(feature = "try-runtime")] + if TEST_ALL_STEPS { + Self::run_all_steps().unwrap(); + } + + T::WeightInfo::on_runtime_upgrade() + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, TryRuntimeError> { + // We can't really do much here as our migrations do not happen during the runtime upgrade. + // Instead, we call the migrations `pre_upgrade` and `post_upgrade` hooks when we iterate + // over our migrations. + let on_chain_version = >::on_chain_storage_version(); + let in_code_version = >::in_code_storage_version(); + + if on_chain_version == in_code_version { + return Ok(Default::default()) + } + + log::debug!( + target: LOG_TARGET, + "Requested migration of {} from {:?}(on-chain storage version) to {:?}(in-code storage version)", + >::name(), on_chain_version, in_code_version + ); + + ensure!( + T::Migrations::is_upgrade_supported(on_chain_version, in_code_version), + "Unsupported upgrade: VERSION_RANGE should be (on-chain storage version + 1, in-code storage version)" + ); + + Ok(Default::default()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), TryRuntimeError> { + if !TEST_ALL_STEPS { + return Ok(()) + } + + log::info!(target: LOG_TARGET, "=== POST UPGRADE CHECKS ==="); + + // Ensure that the hashing algorithm is correct for each storage map. + if let Some(hash) = crate::CodeInfoOf::::iter_keys().next() { + crate::CodeInfoOf::::get(hash).expect("CodeInfo exists for hash; qed"); + } + if let Some(hash) = crate::PristineCode::::iter_keys().next() { + crate::PristineCode::::get(hash).expect("PristineCode exists for hash; qed"); + } + if let Some(account_id) = crate::ContractInfoOf::::iter_keys().next() { + crate::ContractInfoOf::::get(account_id) + .expect("ContractInfo exists for account_id; qed"); + } + if let Some(nonce) = crate::DeletionQueue::::iter_keys().next() { + crate::DeletionQueue::::get(nonce).expect("DeletionQueue exists for nonce; qed"); + } + + Ok(()) + } +} + +/// The result of running the migration. +#[derive(Debug, PartialEq)] +pub enum MigrateResult { + /// No migration was performed + NoMigrationPerformed, + /// No migration currently in progress + NoMigrationInProgress, + /// A migration is in progress + InProgress { steps_done: u32 }, + /// All migrations are completed + Completed, +} + +/// The result of running a migration step. +#[derive(Debug, PartialEq)] +pub enum StepResult { + InProgress { cursor: Cursor, steps_done: u32 }, + Completed { steps_done: u32 }, +} + +impl Migration { + /// Verify that each migration's step of the [`Config::Migrations`] sequence fits into + /// `Cursor`. + pub(crate) fn integrity_test() { + let max_weight = ::BlockWeights::get().max_block; + T::Migrations::integrity_test(max_weight) + } + + /// Execute the multi-step migration. + /// Returns whether or not a migration is in progress + pub(crate) fn migrate(mut meter: &mut WeightMeter) -> MigrateResult { + let name = >::name(); + + if meter.try_consume(T::WeightInfo::migrate()).is_err() { + return MigrateResult::NoMigrationPerformed + } + + MigrationInProgress::::mutate_exists(|progress| { + let Some(cursor_before) = progress.as_mut() else { + meter.consume(T::WeightInfo::migration_noop()); + return MigrateResult::NoMigrationInProgress + }; + + // if a migration is running it is always upgrading to the next version + let storage_version = >::on_chain_storage_version(); + let in_progress_version = storage_version + 1; + + log::info!( + target: LOG_TARGET, + "{name}: Migrating from {:?} to {:?},", + storage_version, + in_progress_version, + ); + + let result = + match T::Migrations::steps(in_progress_version, cursor_before.as_ref(), &mut meter) + { + StepResult::InProgress { cursor, steps_done } => { + *progress = Some(cursor); + MigrateResult::InProgress { steps_done } + }, + StepResult::Completed { steps_done } => { + in_progress_version.put::>(); + if >::in_code_storage_version() != in_progress_version { + log::info!( + target: LOG_TARGET, + "{name}: Next migration is {:?},", + in_progress_version + 1 + ); + *progress = Some(T::Migrations::new(in_progress_version + 1)); + MigrateResult::InProgress { steps_done } + } else { + log::info!( + target: LOG_TARGET, + "{name}: All migrations done. At version {:?},", + in_progress_version + ); + *progress = None; + MigrateResult::Completed + } + }, + }; + + result + }) + } + + pub(crate) fn ensure_migrated() -> DispatchResult { + if Self::in_progress() { + Err(Error::::MigrationInProgress.into()) + } else { + Ok(()) + } + } + + pub(crate) fn in_progress() -> bool { + MigrationInProgress::::exists() + } +} + +#[impl_trait_for_tuples::impl_for_tuples(10)] +#[tuple_types_custom_trait_bound(MigrationStep)] +impl MigrateSequence for Tuple { + const VERSION_RANGE: (u16, u16) = { + let mut versions: (u16, u16) = (0, 0); + for_tuples!( + #( + match versions { + (0, 0) => { + versions = (Tuple::VERSION, Tuple::VERSION); + }, + (min_version, last_version) if Tuple::VERSION == last_version + 1 => { + versions = (min_version, Tuple::VERSION); + }, + _ => panic!("Migrations must be ordered by their versions with no gaps.") + } + )* + ); + versions + }; + + fn new(version: StorageVersion) -> Cursor { + for_tuples!( + #( + if version == Tuple::VERSION { + return Tuple::default().encode().try_into().expect(PROOF_ENCODE) + } + )* + ); + invalid_version(version) + } + + #[cfg(feature = "try-runtime")] + /// Execute the pre-checks of the step associated with this version. + fn pre_upgrade_step(version: StorageVersion) -> Result, TryRuntimeError> { + for_tuples!( + #( + if version == Tuple::VERSION { + return Tuple::pre_upgrade_step() + } + )* + ); + invalid_version(version) + } + + #[cfg(feature = "try-runtime")] + /// Execute the post-checks of the step associated with this version. + fn post_upgrade_step(version: StorageVersion, state: Vec) -> Result<(), TryRuntimeError> { + for_tuples!( + #( + if version == Tuple::VERSION { + return Tuple::post_upgrade_step(state) + } + )* + ); + invalid_version(version) + } + + fn steps(version: StorageVersion, mut cursor: &[u8], meter: &mut WeightMeter) -> StepResult { + for_tuples!( + #( + if version == Tuple::VERSION { + let mut migration = ::decode(&mut cursor) + .expect(PROOF_DECODE); + let max_weight = Tuple::max_step_weight(); + let mut steps_done = 0; + while meter.can_consume(max_weight) { + steps_done.saturating_accrue(1); + if matches!(migration.step(meter), IsFinished::Yes) { + return StepResult::Completed{ steps_done } + } + } + return StepResult::InProgress{cursor: migration.encode().try_into().expect(PROOF_ENCODE), steps_done } + } + )* + ); + invalid_version(version) + } + + fn integrity_test(max_block_weight: Weight) { + for_tuples!( + #( + Tuple::integrity_test(max_block_weight); + )* + ); + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::{ + migration::codegen::LATEST_MIGRATION_VERSION, + tests::{ExtBuilder, Test}, + }; + + #[derive(Default, Encode, Decode, MaxEncodedLen)] + struct MockMigration { + // MockMigration needs `N` steps to finish + count: u16, + } + + impl MigrationStep for MockMigration { + const VERSION: u16 = N; + fn max_step_weight() -> Weight { + Weight::from_all(1) + } + fn step(&mut self, meter: &mut WeightMeter) -> IsFinished { + assert!(self.count != N); + self.count += 1; + meter.consume(Weight::from_all(1)); + if self.count == N { + IsFinished::Yes + } else { + IsFinished::No + } + } + } + + #[test] + fn test_storage_version_matches_last_migration_file() { + assert_eq!(StorageVersion::new(LATEST_MIGRATION_VERSION), crate::pallet::STORAGE_VERSION); + } + + #[test] + fn version_range_works() { + let range = <(MockMigration<1>, MockMigration<2>)>::VERSION_RANGE; + assert_eq!(range, (1, 2)); + } + + #[test] + fn is_upgrade_supported_works() { + type Migrations = (MockMigration<9>, MockMigration<10>, MockMigration<11>); + assert!(Migrations::is_upgrade_supported(StorageVersion::new(8), StorageVersion::new(11))); + assert!(!Migrations::is_upgrade_supported(StorageVersion::new(9), StorageVersion::new(11))); + assert!(!Migrations::is_upgrade_supported(StorageVersion::new(8), StorageVersion::new(12))); + } + + #[test] + fn steps_works() { + type Migrations = (MockMigration<2>, MockMigration<3>); + let version = StorageVersion::new(2); + let mut cursor = Migrations::new(version); + + let mut meter = WeightMeter::with_limit(Weight::from_all(1)); + let result = Migrations::steps(version, &cursor, &mut meter); + cursor = alloc::vec![1u8, 0].try_into().unwrap(); + assert_eq!(result, StepResult::InProgress { cursor: cursor.clone(), steps_done: 1 }); + assert_eq!(meter.consumed(), Weight::from_all(1)); + + let mut meter = WeightMeter::with_limit(Weight::from_all(1)); + assert_eq!( + Migrations::steps(version, &cursor, &mut meter), + StepResult::Completed { steps_done: 1 } + ); + } + + #[test] + fn no_migration_in_progress_works() { + type TestMigration = Migration; + + ExtBuilder::default().build().execute_with(|| { + assert_eq!(StorageVersion::get::>(), LATEST_MIGRATION_VERSION); + assert_eq!( + TestMigration::migrate(&mut WeightMeter::new()), + MigrateResult::NoMigrationInProgress + ) + }); + } + + #[test] + fn migration_works() { + type TestMigration = Migration; + + ExtBuilder::default() + .set_storage_version(LATEST_MIGRATION_VERSION - 2) + .build() + .execute_with(|| { + assert_eq!(StorageVersion::get::>(), LATEST_MIGRATION_VERSION - 2); + TestMigration::on_runtime_upgrade(); + for (version, status) in [ + (LATEST_MIGRATION_VERSION - 1, MigrateResult::InProgress { steps_done: 1 }), + (LATEST_MIGRATION_VERSION, MigrateResult::Completed), + ] { + assert_eq!(TestMigration::migrate(&mut WeightMeter::new()), status); + assert_eq!( + >::on_chain_storage_version(), + StorageVersion::new(version) + ); + } + + assert_eq!( + TestMigration::migrate(&mut WeightMeter::new()), + MigrateResult::NoMigrationInProgress + ); + assert_eq!(StorageVersion::get::>(), LATEST_MIGRATION_VERSION); + }); + } +} diff --git a/pallets/contracts/src/migration/v09.rs b/pallets/contracts/src/migration/v09.rs new file mode 100644 index 00000000..6a673951 --- /dev/null +++ b/pallets/contracts/src/migration/v09.rs @@ -0,0 +1,148 @@ +// This file is part of Substrate. + +// 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. + +//! Update `CodeStorage` with the new `determinism` field. + +use crate::{ + migration::{IsFinished, MigrationStep}, + weights::WeightInfo, + CodeHash, Config, Determinism, Pallet, Weight, LOG_TARGET, +}; +use alloc::vec::Vec; +use codec::{Decode, Encode}; +use frame_support::{ + pallet_prelude::*, storage_alias, weights::WeightMeter, DefaultNoBound, Identity, +}; +#[cfg(feature = "try-runtime")] +use sp_runtime::TryRuntimeError; + +mod v8 { + use super::*; + + #[derive(Encode, Decode)] + pub struct PrefabWasmModule { + #[codec(compact)] + pub instruction_weights_version: u32, + #[codec(compact)] + pub initial: u32, + #[codec(compact)] + pub maximum: u32, + pub code: Vec, + } + + #[storage_alias] + pub type CodeStorage = + StorageMap, Identity, CodeHash, PrefabWasmModule>; +} + +#[cfg(feature = "runtime-benchmarks")] +pub fn store_old_dummy_code(len: usize) { + use sp_runtime::traits::Hash; + let module = v8::PrefabWasmModule { + instruction_weights_version: 0, + initial: 0, + maximum: 0, + code: alloc::vec![42u8; len], + }; + let hash = T::Hashing::hash(&module.code); + v8::CodeStorage::::insert(hash, module); +} + +#[derive(Encode, Decode)] +struct PrefabWasmModule { + #[codec(compact)] + pub instruction_weights_version: u32, + #[codec(compact)] + pub initial: u32, + #[codec(compact)] + pub maximum: u32, + pub code: Vec, + pub determinism: Determinism, +} + +#[storage_alias] +type CodeStorage = StorageMap, Identity, CodeHash, PrefabWasmModule>; + +#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)] +pub struct Migration { + last_code_hash: Option>, +} + +impl MigrationStep for Migration { + const VERSION: u16 = 9; + + fn max_step_weight() -> Weight { + T::WeightInfo::v9_migration_step(T::MaxCodeLen::get()) + } + + fn step(&mut self, meter: &mut WeightMeter) -> IsFinished { + let mut iter = if let Some(last_key) = self.last_code_hash.take() { + v8::CodeStorage::::iter_from(v8::CodeStorage::::hashed_key_for(last_key)) + } else { + v8::CodeStorage::::iter() + }; + + if let Some((key, old)) = iter.next() { + log::debug!(target: LOG_TARGET, "Migrating contract code {:?}", key); + let len = old.code.len() as u32; + let module = PrefabWasmModule { + instruction_weights_version: old.instruction_weights_version, + initial: old.initial, + maximum: old.maximum, + code: old.code, + determinism: Determinism::Enforced, + }; + CodeStorage::::insert(key, module); + self.last_code_hash = Some(key); + meter.consume(T::WeightInfo::v9_migration_step(len)); + IsFinished::No + } else { + log::debug!(target: LOG_TARGET, "No more contracts code to migrate"); + meter.consume(T::WeightInfo::v9_migration_step(0)); + IsFinished::Yes + } + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade_step() -> Result, TryRuntimeError> { + let sample: Vec<_> = v8::CodeStorage::::iter().take(100).collect(); + + log::debug!(target: LOG_TARGET, "Taking sample of {} contract codes", sample.len()); + Ok(sample.encode()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade_step(state: Vec) -> Result<(), TryRuntimeError> { + let sample = , v8::PrefabWasmModule)> as Decode>::decode(&mut &state[..]) + .expect("pre_upgrade_step provides a valid state; qed"); + + log::debug!(target: LOG_TARGET, "Validating sample of {} contract codes", sample.len()); + for (code_hash, old) in sample { + let module = CodeStorage::::get(&code_hash).unwrap(); + ensure!( + module.instruction_weights_version == old.instruction_weights_version, + "invalid instruction weights version" + ); + ensure!(module.determinism == Determinism::Enforced, "invalid determinism"); + ensure!(module.initial == old.initial, "invalid initial"); + ensure!(module.maximum == old.maximum, "invalid maximum"); + ensure!(module.code == old.code, "invalid code"); + } + + Ok(()) + } +} diff --git a/pallets/contracts/src/migration/v10.rs b/pallets/contracts/src/migration/v10.rs new file mode 100644 index 00000000..23e7fd23 --- /dev/null +++ b/pallets/contracts/src/migration/v10.rs @@ -0,0 +1,322 @@ +// This file is part of Substrate. + +// 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. + +//! Don't rely on reserved balances keeping an account alive +//! See . + +use crate::{ + exec::AccountIdOf, + migration::{IsFinished, MigrationStep}, + weights::WeightInfo, + CodeHash, Config, Pallet, TrieId, Weight, LOG_TARGET, +}; +use codec::{Decode, Encode}; +use core::{ + cmp::{max, min}, + ops::Deref, +}; +use frame_support::{ + pallet_prelude::*, + storage_alias, + traits::{ + tokens::{fungible::Inspect, Fortitude::Polite, Preservation::Preserve}, + ExistenceRequirement, ReservableCurrency, + }, + weights::WeightMeter, + DefaultNoBound, +}; +use sp_core::hexdisplay::HexDisplay; +use sp_runtime::{ + traits::{Hash, TrailingZeroInput, Zero}, + Perbill, Saturating, +}; + +#[cfg(feature = "try-runtime")] +use alloc::vec::Vec; +#[cfg(feature = "try-runtime")] +use sp_runtime::TryRuntimeError; + +mod v9 { + use super::*; + + pub type BalanceOf = ::AccountId, + >>::Balance; + + #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] + #[scale_info(skip_type_params(T, OldCurrency))] + pub struct ContractInfo + where + OldCurrency: ReservableCurrency<::AccountId>, + { + pub trie_id: TrieId, + pub code_hash: CodeHash, + pub storage_bytes: u32, + pub storage_items: u32, + pub storage_byte_deposit: BalanceOf, + pub storage_item_deposit: BalanceOf, + pub storage_base_deposit: BalanceOf, + } + + #[storage_alias] + pub type ContractInfoOf = StorageMap< + Pallet, + Twox64Concat, + ::AccountId, + ContractInfo, + >; +} + +#[cfg(feature = "runtime-benchmarks")] +pub fn store_old_contract_info( + account: T::AccountId, + info: crate::ContractInfo, +) where + OldCurrency: ReservableCurrency<::AccountId> + 'static, +{ + let info = v9::ContractInfo { + trie_id: info.trie_id, + code_hash: info.code_hash, + storage_bytes: Default::default(), + storage_items: Default::default(), + storage_byte_deposit: Default::default(), + storage_item_deposit: Default::default(), + storage_base_deposit: Default::default(), + }; + v9::ContractInfoOf::::insert(account, info); +} + +#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)] +#[scale_info(skip_type_params(T))] +pub struct DepositAccount(AccountIdOf); + +impl Deref for DepositAccount { + type Target = AccountIdOf; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] +#[scale_info(skip_type_params(T, OldCurrency))] +pub struct ContractInfo +where + OldCurrency: ReservableCurrency<::AccountId>, +{ + pub trie_id: TrieId, + deposit_account: DepositAccount, + pub code_hash: CodeHash, + storage_bytes: u32, + storage_items: u32, + pub storage_byte_deposit: v9::BalanceOf, + storage_item_deposit: v9::BalanceOf, + storage_base_deposit: v9::BalanceOf, +} + +#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)] +pub struct Migration { + last_account: Option, + _phantom: PhantomData<(T, OldCurrency)>, +} + +#[storage_alias] +type ContractInfoOf = StorageMap< + Pallet, + Twox64Concat, + ::AccountId, + ContractInfo, +>; + +/// Formula: `hash("contract_depo_v1" ++ contract_addr)` +fn deposit_address( + contract_addr: &::AccountId, +) -> ::AccountId { + let entropy = (b"contract_depo_v1", contract_addr) + .using_encoded(::Hashing::hash); + Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref())) + .expect("infinite length input; no invalid inputs for type; qed") +} + +impl MigrationStep for Migration +where + OldCurrency: ReservableCurrency<::AccountId> + + Inspect<::AccountId, Balance = v9::BalanceOf>, +{ + const VERSION: u16 = 10; + + fn max_step_weight() -> Weight { + T::WeightInfo::v10_migration_step() + } + + fn step(&mut self, meter: &mut WeightMeter) -> IsFinished { + let mut iter = if let Some(last_account) = self.last_account.take() { + v9::ContractInfoOf::::iter_from( + v9::ContractInfoOf::::hashed_key_for(last_account), + ) + } else { + v9::ContractInfoOf::::iter() + }; + + if let Some((account, contract)) = iter.next() { + let min_balance = ::AccountId, + >>::minimum_balance(); + log::debug!(target: LOG_TARGET, "Account: 0x{} ", HexDisplay::from(&account.encode())); + + // Get the new deposit account address + let deposit_account: DepositAccount = DepositAccount(deposit_address::(&account)); + + // Calculate the existing deposit, that should be reserved on the contract account + let old_deposit = contract + .storage_base_deposit + .saturating_add(contract.storage_item_deposit) + .saturating_add(contract.storage_byte_deposit); + + // Unreserve the existing deposit + // Note we can't use repatriate_reserve, because it only works with existing accounts + let remaining = OldCurrency::unreserve(&account, old_deposit); + if !remaining.is_zero() { + log::warn!( + target: LOG_TARGET, + "Partially unreserved. Remaining {:?} out of {:?} asked", + remaining, + old_deposit + ); + } + + // Attempt to transfer the old deposit to the deposit account. + let amount = old_deposit + .saturating_sub(min_balance) + .min(OldCurrency::reducible_balance(&account, Preserve, Polite)); + + let new_deposit = OldCurrency::transfer( + &account, + &deposit_account, + amount, + ExistenceRequirement::KeepAlive, + ) + .map(|_| { + log::debug!( + target: LOG_TARGET, + "Transferred deposit ({:?}) to deposit account", + amount + ); + amount + }) + // If it fails we fallback to minting the ED. + .unwrap_or_else(|err| { + log::error!( + target: LOG_TARGET, + "Failed to transfer the base deposit, reason: {:?}", + err + ); + let _ = OldCurrency::deposit_creating(&deposit_account, min_balance); + min_balance + }); + + // Calculate the new base_deposit to store in the contract: + // Ideally, it should be the same as the old one + // Ideally, it should be at least 2xED (for the contract and deposit accounts). + // It can't be more than the `new_deposit`. + let new_base_deposit = min( + max(contract.storage_base_deposit, min_balance.saturating_add(min_balance)), + new_deposit, + ); + + // Calculate the ratio to adjust storage_byte and storage_item deposits. + let new_deposit_without_base = new_deposit.saturating_sub(new_base_deposit); + let old_deposit_without_base = + old_deposit.saturating_sub(contract.storage_base_deposit); + let ratio = Perbill::from_rational(new_deposit_without_base, old_deposit_without_base); + + // Calculate the new storage deposits based on the ratio + let storage_byte_deposit = ratio.mul_ceil(contract.storage_byte_deposit); + let storage_item_deposit = ratio.mul_ceil(contract.storage_item_deposit); + + // Recalculate the new base deposit, instead of using new_base_deposit to avoid rounding + // errors + let storage_base_deposit = new_deposit + .saturating_sub(storage_byte_deposit) + .saturating_sub(storage_item_deposit); + + let new_contract_info = ContractInfo { + trie_id: contract.trie_id, + deposit_account, + code_hash: contract.code_hash, + storage_bytes: contract.storage_bytes, + storage_items: contract.storage_items, + storage_byte_deposit, + storage_item_deposit, + storage_base_deposit, + }; + + ContractInfoOf::::insert(&account, new_contract_info); + + // Store last key for next migration step + self.last_account = Some(account); + + meter.consume(T::WeightInfo::v10_migration_step()); + IsFinished::No + } else { + log::debug!(target: LOG_TARGET, "Done Migrating contract info"); + meter.consume(T::WeightInfo::v10_migration_step()); + IsFinished::Yes + } + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade_step() -> Result, TryRuntimeError> { + let sample: Vec<_> = v9::ContractInfoOf::::iter().take(10).collect(); + + log::debug!(target: LOG_TARGET, "Taking sample of {} contracts", sample.len()); + Ok(sample.encode()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade_step(state: Vec) -> Result<(), TryRuntimeError> { + let sample = )> as Decode>::decode( + &mut &state[..], + ) + .expect("pre_upgrade_step provides a valid state; qed"); + + log::debug!(target: LOG_TARGET, "Validating sample of {} contracts", sample.len()); + for (account, old_contract) in sample { + log::debug!(target: LOG_TARGET, "==="); + log::debug!(target: LOG_TARGET, "Account: 0x{} ", HexDisplay::from(&account.encode())); + let contract = ContractInfoOf::::get(&account).unwrap(); + ensure!(old_contract.trie_id == contract.trie_id, "invalid trie_id"); + ensure!(old_contract.code_hash == contract.code_hash, "invalid code_hash"); + ensure!(old_contract.storage_bytes == contract.storage_bytes, "invalid storage_bytes"); + ensure!(old_contract.storage_items == contract.storage_items, "invalid storage_items"); + + let deposit = >::total_balance( + &contract.deposit_account, + ); + ensure!( + deposit == + contract + .storage_base_deposit + .saturating_add(contract.storage_item_deposit) + .saturating_add(contract.storage_byte_deposit), + "deposit mismatch" + ); + } + + Ok(()) + } +} diff --git a/pallets/contracts/src/migration/v11.rs b/pallets/contracts/src/migration/v11.rs new file mode 100644 index 00000000..bd128e22 --- /dev/null +++ b/pallets/contracts/src/migration/v11.rs @@ -0,0 +1,136 @@ +// This file is part of Substrate. + +// 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. + +//! Overflowing bounded DeletionQueue. +//! See . + +use crate::{ + migration::{IsFinished, MigrationStep}, + weights::WeightInfo, + Config, Pallet, TrieId, Weight, LOG_TARGET, +}; +use alloc::vec::Vec; +use codec::{Decode, Encode}; +use core::marker::PhantomData; +use frame_support::{pallet_prelude::*, storage_alias, weights::WeightMeter, DefaultNoBound}; +#[cfg(feature = "try-runtime")] +use sp_runtime::TryRuntimeError; + +mod v10 { + use super::*; + + #[derive(Encode, Decode, TypeInfo, MaxEncodedLen)] + pub struct DeletedContract { + pub(crate) trie_id: TrieId, + } + + #[storage_alias] + pub type DeletionQueue = StorageValue, Vec>; +} + +#[derive(Encode, Decode, TypeInfo, MaxEncodedLen, DefaultNoBound, Clone)] +#[scale_info(skip_type_params(T))] +pub struct DeletionQueueManager { + insert_counter: u32, + delete_counter: u32, + _phantom: PhantomData, +} + +#[cfg(any(feature = "runtime-benchmarks", feature = "try-runtime"))] +pub fn fill_old_queue(len: usize) { + let queue: Vec = + core::iter::repeat_with(|| v10::DeletedContract { trie_id: Default::default() }) + .take(len) + .collect(); + v10::DeletionQueue::::set(Some(queue)); +} + +#[storage_alias] +type DeletionQueue = StorageMap, Twox64Concat, u32, TrieId>; + +#[storage_alias] +type DeletionQueueCounter = StorageValue, DeletionQueueManager, ValueQuery>; + +#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)] +pub struct Migration { + _phantom: PhantomData, +} + +impl MigrationStep for Migration { + const VERSION: u16 = 11; + + // It would be more correct to make our use the now removed [DeletionQueueDepth](https://github.com/paritytech/substrate/pull/13702/files#diff-70e9723e9db62816e35f6f885b6770a8449c75a6c2733e9fa7a245fe52c4656c) + // but in practice the queue is always empty, so 128 is a good enough approximation for not + // underestimating the weight of our migration. + fn max_step_weight() -> Weight { + T::WeightInfo::v11_migration_step(128) + } + + fn step(&mut self, meter: &mut WeightMeter) -> IsFinished { + let Some(old_queue) = v10::DeletionQueue::::take() else { + meter.consume(T::WeightInfo::v11_migration_step(0)); + return IsFinished::Yes + }; + let len = old_queue.len(); + + log::debug!( + target: LOG_TARGET, + "Migrating deletion queue with {} deleted contracts", + old_queue.len() + ); + + if !old_queue.is_empty() { + let mut queue = DeletionQueueManager::::default(); + for contract in old_queue { + >::insert(queue.insert_counter, contract.trie_id); + queue.insert_counter += 1; + } + + >::set(queue); + } + + meter.consume(T::WeightInfo::v11_migration_step(len as u32)); + IsFinished::Yes + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade_step() -> Result, TryRuntimeError> { + let old_queue = v10::DeletionQueue::::take().unwrap_or_default(); + + if old_queue.is_empty() { + let len = 10u32; + log::debug!( + target: LOG_TARGET, + "Injecting {len} entries to deletion queue to test migration" + ); + fill_old_queue::(len as usize); + return Ok(len.encode()) + } + + Ok((old_queue.len() as u32).encode()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade_step(state: Vec) -> Result<(), TryRuntimeError> { + let len = ::decode(&mut &state[..]) + .expect("pre_upgrade_step provides a valid state; qed"); + let counter = >::get(); + ensure!(counter.insert_counter == len, "invalid insert counter"); + ensure!(counter.delete_counter == 0, "invalid delete counter"); + Ok(()) + } +} diff --git a/pallets/contracts/src/migration/v12.rs b/pallets/contracts/src/migration/v12.rs new file mode 100644 index 00000000..3c33591e --- /dev/null +++ b/pallets/contracts/src/migration/v12.rs @@ -0,0 +1,351 @@ +// This file is part of Substrate. + +// 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. + +//! Move `OwnerInfo` to `CodeInfo`, add `determinism` field to the latter, clear `CodeStorage` and +//! repay deposits. + +use crate::{ + migration::{IsFinished, MigrationStep}, + weights::WeightInfo, + AccountIdOf, BalanceOf, CodeHash, Config, Determinism, Pallet, Weight, LOG_TARGET, +}; +use alloc::vec::Vec; +use codec::{Decode, Encode}; +use frame_support::{ + pallet_prelude::*, storage_alias, traits::ReservableCurrency, weights::WeightMeter, + DefaultNoBound, Identity, +}; +use scale_info::prelude::format; +use sp_core::hexdisplay::HexDisplay; +#[cfg(feature = "try-runtime")] +use sp_runtime::TryRuntimeError; +use sp_runtime::{traits::Zero, FixedPointNumber, FixedU128, Saturating}; + +mod v11 { + use super::*; + + pub type BalanceOf = ::AccountId, + >>::Balance; + + #[derive(Encode, Decode, scale_info::TypeInfo, MaxEncodedLen)] + #[codec(mel_bound())] + #[scale_info(skip_type_params(T, OldCurrency))] + pub struct OwnerInfo + where + OldCurrency: ReservableCurrency<::AccountId>, + { + pub owner: AccountIdOf, + #[codec(compact)] + pub deposit: BalanceOf, + #[codec(compact)] + pub refcount: u64, + } + + #[derive(Encode, Decode, scale_info::TypeInfo)] + #[codec(mel_bound())] + #[scale_info(skip_type_params(T))] + pub struct PrefabWasmModule { + #[codec(compact)] + pub instruction_weights_version: u32, + #[codec(compact)] + pub initial: u32, + #[codec(compact)] + pub maximum: u32, + pub code: Vec, + pub determinism: Determinism, + } + + #[storage_alias] + pub type OwnerInfoOf = + StorageMap, Identity, CodeHash, OwnerInfo>; + + #[storage_alias] + pub type CodeStorage = + StorageMap, Identity, CodeHash, PrefabWasmModule>; +} + +#[derive(Encode, Decode, scale_info::TypeInfo, MaxEncodedLen)] +#[codec(mel_bound())] +#[scale_info(skip_type_params(T, OldCurrency))] +pub struct CodeInfo +where + OldCurrency: ReservableCurrency<::AccountId>, +{ + owner: AccountIdOf, + #[codec(compact)] + deposit: v11::BalanceOf, + #[codec(compact)] + refcount: u64, + determinism: Determinism, + code_len: u32, +} + +#[storage_alias] +pub type CodeInfoOf = + StorageMap, Identity, CodeHash, CodeInfo>; + +#[storage_alias] +pub type PristineCode = StorageMap, Identity, CodeHash, Vec>; + +#[cfg(feature = "runtime-benchmarks")] +pub fn store_old_dummy_code(len: usize, account: T::AccountId) +where + OldCurrency: ReservableCurrency<::AccountId> + 'static, +{ + use sp_runtime::traits::Hash; + + let code = alloc::vec![42u8; len]; + let hash = T::Hashing::hash(&code); + PristineCode::::insert(hash, code.clone()); + + let module = v11::PrefabWasmModule { + instruction_weights_version: Default::default(), + initial: Default::default(), + maximum: Default::default(), + code, + determinism: Determinism::Enforced, + }; + v11::CodeStorage::::insert(hash, module); + + let info = v11::OwnerInfo { owner: account, deposit: u32::MAX.into(), refcount: u64::MAX }; + v11::OwnerInfoOf::::insert(hash, info); +} + +#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)] +pub struct Migration +where + OldCurrency: ReservableCurrency<::AccountId>, + OldCurrency::Balance: From>, +{ + last_code_hash: Option>, + _phantom: PhantomData, +} + +impl MigrationStep for Migration +where + OldCurrency: ReservableCurrency<::AccountId> + 'static, + OldCurrency::Balance: From>, +{ + const VERSION: u16 = 12; + + fn max_step_weight() -> Weight { + T::WeightInfo::v12_migration_step(T::MaxCodeLen::get()) + } + + fn step(&mut self, meter: &mut WeightMeter) -> IsFinished { + let mut iter = if let Some(last_key) = self.last_code_hash.take() { + v11::OwnerInfoOf::::iter_from( + v11::OwnerInfoOf::::hashed_key_for(last_key), + ) + } else { + v11::OwnerInfoOf::::iter() + }; + if let Some((hash, old_info)) = iter.next() { + log::debug!(target: LOG_TARGET, "Migrating OwnerInfo for code_hash {:?}", hash); + + let module = v11::CodeStorage::::take(hash) + .expect(format!("No PrefabWasmModule found for code_hash: {:?}", hash).as_str()); + + let code_len = module.code.len(); + // We print this to measure the impact of the migration. + // Storage removed: deleted PrefabWasmModule's encoded len. + // Storage added: determinism field encoded len (as all other CodeInfo fields are the + // same as in the deleted OwnerInfo). + log::debug!(target: LOG_TARGET, "Storage removed: 1 item, {} bytes", &code_len,); + + // Storage usage prices could change over time, and accounts who uploaded their + // contracts code before the storage deposits where introduced, had not been ever + // charged with any deposit for that (see migration v6). + // + // This is why deposit to be refunded here is calculated as follows: + // + // 1. Calculate the deposit amount for storage before the migration, given current + // prices. + // 2. Given current reserved deposit amount, calculate the correction factor. + // 3. Calculate the deposit amount for storage after the migration, given current + // prices. + // 4. Calculate real deposit amount to be reserved after the migration. + let price_per_byte = T::DepositPerByte::get(); + let price_per_item = T::DepositPerItem::get(); + let bytes_before = module + .encoded_size() + .saturating_add(code_len) + .saturating_add(v11::OwnerInfo::::max_encoded_len()) + as u32; + let items_before = 3u32; + let deposit_expected_before = price_per_byte + .saturating_mul(bytes_before.into()) + .saturating_add(price_per_item.saturating_mul(items_before.into())); + let ratio = FixedU128::checked_from_rational(old_info.deposit, deposit_expected_before) + .unwrap_or_default() + .min(FixedU128::from_u32(1)); + let bytes_after = + code_len.saturating_add(CodeInfo::::max_encoded_len()) as u32; + let items_after = 2u32; + let deposit_expected_after = price_per_byte + .saturating_mul(bytes_after.into()) + .saturating_add(price_per_item.saturating_mul(items_after.into())); + let deposit = ratio.saturating_mul_int(deposit_expected_after); + + let info = CodeInfo:: { + determinism: module.determinism, + owner: old_info.owner, + deposit: deposit.into(), + refcount: old_info.refcount, + code_len: code_len as u32, + }; + + let amount = old_info.deposit.saturating_sub(info.deposit); + if !amount.is_zero() { + OldCurrency::unreserve(&info.owner, amount); + log::debug!( + target: LOG_TARGET, + "Deposit refunded: {:?} Balance, to: {:?}", + &amount, + HexDisplay::from(&info.owner.encode()) + ); + } else { + log::warn!( + target: LOG_TARGET, + "new deposit: {:?} >= old deposit: {:?}", + &info.deposit, + &old_info.deposit + ); + } + CodeInfoOf::::insert(hash, info); + + self.last_code_hash = Some(hash); + + meter.consume(T::WeightInfo::v12_migration_step(code_len as u32)); + IsFinished::No + } else { + log::debug!(target: LOG_TARGET, "No more OwnerInfo to migrate"); + meter.consume(T::WeightInfo::v12_migration_step(0)); + IsFinished::Yes + } + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade_step() -> Result, TryRuntimeError> { + let len = 100; + log::debug!(target: LOG_TARGET, "Taking sample of {} OwnerInfo(s)", len); + let sample: Vec<_> = v11::OwnerInfoOf::::iter() + .take(len) + .map(|(k, v)| { + let module = v11::CodeStorage::::get(k) + .expect("No PrefabWasmModule found for code_hash: {:?}"); + let info: CodeInfo = CodeInfo { + determinism: module.determinism, + deposit: v.deposit, + refcount: v.refcount, + owner: v.owner, + code_len: module.code.len() as u32, + }; + (k, info) + }) + .collect(); + + let storage: u32 = + v11::CodeStorage::::iter().map(|(_k, v)| v.encoded_size() as u32).sum(); + let mut deposit: v11::BalanceOf = Default::default(); + v11::OwnerInfoOf::::iter().for_each(|(_k, v)| deposit += v.deposit); + + Ok((sample, deposit, storage).encode()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade_step(state: Vec) -> Result<(), TryRuntimeError> { + let state = <( + Vec<(CodeHash, CodeInfo)>, + v11::BalanceOf, + u32, + ) as Decode>::decode(&mut &state[..]) + .unwrap(); + + log::debug!(target: LOG_TARGET, "Validating state of {} Codeinfo(s)", state.0.len()); + for (hash, old) in state.0 { + let info = CodeInfoOf::::get(&hash) + .expect(format!("CodeInfo for code_hash {:?} not found!", hash).as_str()); + ensure!(info.determinism == old.determinism, "invalid determinism"); + ensure!(info.owner == old.owner, "invalid owner"); + ensure!(info.refcount == old.refcount, "invalid refcount"); + } + + if let Some((k, _)) = v11::CodeStorage::::iter().next() { + log::warn!( + target: LOG_TARGET, + "CodeStorage is still NOT empty, found code_hash: {:?}", + k + ); + } else { + log::debug!(target: LOG_TARGET, "CodeStorage is empty."); + } + if let Some((k, _)) = v11::OwnerInfoOf::::iter().next() { + log::warn!( + target: LOG_TARGET, + "OwnerInfoOf is still NOT empty, found code_hash: {:?}", + k + ); + } else { + log::debug!(target: LOG_TARGET, "OwnerInfoOf is empty."); + } + + let mut deposit: v11::BalanceOf = Default::default(); + let mut items = 0u32; + let mut storage_info = 0u32; + CodeInfoOf::::iter().for_each(|(_k, v)| { + deposit += v.deposit; + items += 1; + storage_info += v.encoded_size() as u32; + }); + let mut storage_code = 0u32; + PristineCode::::iter().for_each(|(_k, v)| { + storage_code += v.len() as u32; + }); + let (_, old_deposit, storage_module) = state; + // CodeInfoOf::max_encoded_len == OwnerInfoOf::max_encoded_len + 1 + // I.e. code info adds up 1 byte per record. + let info_bytes_added = items; + // We removed 1 PrefabWasmModule, and added 1 byte of determinism flag, per contract code. + let storage_removed = storage_module.saturating_sub(info_bytes_added); + // module+code+info - bytes + let storage_was = storage_module + .saturating_add(storage_code) + .saturating_add(storage_info) + .saturating_sub(info_bytes_added); + // We removed 1 storage item (PrefabWasmMod) for every stored contract code (was stored 3 + // items per code). + let items_removed = items; + log::info!( + target: LOG_TARGET, + "Storage freed, bytes: {} (of {}), items: {} (of {})", + storage_removed, + storage_was, + items_removed, + items_removed * 3, + ); + log::info!( + target: LOG_TARGET, + "Deposits returned, total: {:?} Balance (of {:?} Balance)", + old_deposit.saturating_sub(deposit), + old_deposit, + ); + + Ok(()) + } +} diff --git a/pallets/contracts/src/migration/v13.rs b/pallets/contracts/src/migration/v13.rs new file mode 100644 index 00000000..d1888b33 --- /dev/null +++ b/pallets/contracts/src/migration/v13.rs @@ -0,0 +1,136 @@ +// This file is part of Substrate. + +// 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. + +//! Add `delegate_dependencies` to `ContractInfo`. +//! See . + +use crate::{ + migration::{IsFinished, MigrationStep}, + weights::WeightInfo, + AccountIdOf, BalanceOf, CodeHash, Config, Pallet, TrieId, Weight, LOG_TARGET, +}; +use codec::{Decode, Encode}; +use frame_support::{pallet_prelude::*, storage_alias, weights::WeightMeter, DefaultNoBound}; +use sp_runtime::BoundedBTreeMap; + +mod v12 { + use super::*; + + #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] + #[scale_info(skip_type_params(T))] + pub struct ContractInfo { + pub trie_id: TrieId, + pub deposit_account: AccountIdOf, + pub code_hash: CodeHash, + pub storage_bytes: u32, + pub storage_items: u32, + pub storage_byte_deposit: BalanceOf, + pub storage_item_deposit: BalanceOf, + pub storage_base_deposit: BalanceOf, + } + + #[storage_alias] + pub type ContractInfoOf = StorageMap< + Pallet, + Twox64Concat, + ::AccountId, + ContractInfo, + >; +} + +#[cfg(feature = "runtime-benchmarks")] +pub fn store_old_contract_info(account: T::AccountId, info: crate::ContractInfo) { + use sp_runtime::traits::{Hash, TrailingZeroInput}; + let entropy = (b"contract_depo_v1", account.clone()).using_encoded(T::Hashing::hash); + let deposit_account = Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref())) + .expect("infinite length input; no invalid inputs for type; qed"); + let info = v12::ContractInfo { + trie_id: info.trie_id.clone(), + deposit_account, + code_hash: info.code_hash, + storage_bytes: Default::default(), + storage_items: Default::default(), + storage_byte_deposit: Default::default(), + storage_item_deposit: Default::default(), + storage_base_deposit: Default::default(), + }; + v12::ContractInfoOf::::insert(account, info); +} + +#[storage_alias] +pub type ContractInfoOf = + StorageMap, Twox64Concat, ::AccountId, ContractInfo>; + +#[derive(Encode, Decode, CloneNoBound, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] +#[scale_info(skip_type_params(T))] +pub struct ContractInfo { + trie_id: TrieId, + deposit_account: AccountIdOf, + code_hash: CodeHash, + storage_bytes: u32, + storage_items: u32, + storage_byte_deposit: BalanceOf, + storage_item_deposit: BalanceOf, + storage_base_deposit: BalanceOf, + delegate_dependencies: BoundedBTreeMap, BalanceOf, T::MaxDelegateDependencies>, +} + +#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)] +pub struct Migration { + last_account: Option, +} + +impl MigrationStep for Migration { + const VERSION: u16 = 13; + + fn max_step_weight() -> Weight { + T::WeightInfo::v13_migration_step() + } + + fn step(&mut self, meter: &mut WeightMeter) -> IsFinished { + let mut iter = if let Some(last_account) = self.last_account.take() { + v12::ContractInfoOf::::iter_from(v12::ContractInfoOf::::hashed_key_for( + last_account, + )) + } else { + v12::ContractInfoOf::::iter() + }; + + if let Some((key, old)) = iter.next() { + log::debug!(target: LOG_TARGET, "Migrating contract {:?}", key); + let info = ContractInfo { + trie_id: old.trie_id, + deposit_account: old.deposit_account, + code_hash: old.code_hash, + storage_bytes: old.storage_bytes, + storage_items: old.storage_items, + storage_byte_deposit: old.storage_byte_deposit, + storage_item_deposit: old.storage_item_deposit, + storage_base_deposit: old.storage_base_deposit, + delegate_dependencies: Default::default(), + }; + ContractInfoOf::::insert(key.clone(), info); + self.last_account = Some(key); + meter.consume(T::WeightInfo::v13_migration_step()); + IsFinished::No + } else { + log::debug!(target: LOG_TARGET, "No more contracts to migrate"); + meter.consume(T::WeightInfo::v13_migration_step()); + IsFinished::Yes + } + } +} diff --git a/pallets/contracts/src/migration/v14.rs b/pallets/contracts/src/migration/v14.rs new file mode 100644 index 00000000..11336fe2 --- /dev/null +++ b/pallets/contracts/src/migration/v14.rs @@ -0,0 +1,274 @@ +// This file is part of Substrate. + +// 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. + +//! Update the code owner balance, make the code upload deposit balance to be held instead of +//! reserved. Since [`Currency`](frame_support::traits::Currency) has been +//! [deprecated](https://github.com/paritytech/substrate/pull/12951), we need the deposits to be +//! handled by the [`frame_support::traits::fungible`] traits. + +use crate::{ + exec::AccountIdOf, + migration::{IsFinished, MigrationStep}, + weights::WeightInfo, + BalanceOf, CodeHash, Config, Determinism, HoldReason, Pallet, Weight, LOG_TARGET, +}; +#[cfg(feature = "try-runtime")] +use alloc::collections::btree_map::BTreeMap; +use codec::{Decode, Encode}; +#[cfg(feature = "try-runtime")] +use environmental::Vec; +#[cfg(feature = "try-runtime")] +use frame_support::traits::fungible::{Inspect, InspectHold}; +use frame_support::{ + pallet_prelude::*, + storage_alias, + traits::{fungible::MutateHold, ReservableCurrency}, + weights::WeightMeter, + DefaultNoBound, +}; +use sp_core::hexdisplay::HexDisplay; +#[cfg(feature = "try-runtime")] +use sp_runtime::TryRuntimeError; +use sp_runtime::{traits::Zero, Saturating}; + +mod v13 { + use super::*; + + pub type BalanceOf = ::AccountId, + >>::Balance; + + #[derive(Encode, Decode, scale_info::TypeInfo, MaxEncodedLen)] + #[codec(mel_bound())] + #[scale_info(skip_type_params(T, OldCurrency))] + pub struct CodeInfo + where + T: Config, + OldCurrency: ReservableCurrency<::AccountId>, + { + pub owner: AccountIdOf, + #[codec(compact)] + pub deposit: v13::BalanceOf, + #[codec(compact)] + pub refcount: u64, + pub determinism: Determinism, + pub code_len: u32, + } + + #[storage_alias] + pub type CodeInfoOf = + StorageMap, Identity, CodeHash, CodeInfo>; +} + +#[cfg(feature = "runtime-benchmarks")] +pub fn store_dummy_code(account: T::AccountId) +where + T: Config, + OldCurrency: ReservableCurrency<::AccountId> + 'static, +{ + use alloc::vec; + use sp_runtime::traits::Hash; + + let len = T::MaxCodeLen::get(); + let code = vec![42u8; len as usize]; + let hash = T::Hashing::hash(&code); + + let info = v13::CodeInfo { + owner: account, + deposit: 10_000u32.into(), + refcount: u64::MAX, + determinism: Determinism::Enforced, + code_len: len, + }; + v13::CodeInfoOf::::insert(hash, info); +} + +#[cfg(feature = "try-runtime")] +#[derive(Encode, Decode)] +/// Accounts for the balance allocation of a code owner. +struct BalanceAllocation +where + T: Config, + OldCurrency: ReservableCurrency<::AccountId>, +{ + /// Total reserved balance as code upload deposit for the owner. + reserved: v13::BalanceOf, + /// Total balance of the owner. + total: v13::BalanceOf, +} + +#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)] +pub struct Migration +where + T: Config, + OldCurrency: ReservableCurrency<::AccountId>, +{ + last_code_hash: Option>, + _phantom: PhantomData<(T, OldCurrency)>, +} + +impl MigrationStep for Migration +where + T: Config, + OldCurrency: 'static + ReservableCurrency<::AccountId>, + BalanceOf: From, +{ + const VERSION: u16 = 14; + + fn max_step_weight() -> Weight { + T::WeightInfo::v14_migration_step() + } + + fn step(&mut self, meter: &mut WeightMeter) -> IsFinished { + let mut iter = if let Some(last_hash) = self.last_code_hash.take() { + v13::CodeInfoOf::::iter_from( + v13::CodeInfoOf::::hashed_key_for(last_hash), + ) + } else { + v13::CodeInfoOf::::iter() + }; + + if let Some((hash, code_info)) = iter.next() { + log::debug!(target: LOG_TARGET, "Migrating code upload deposit for 0x{:?}", HexDisplay::from(&code_info.owner.encode())); + + let remaining = OldCurrency::unreserve(&code_info.owner, code_info.deposit); + + if remaining > Zero::zero() { + log::warn!( + target: LOG_TARGET, + "Code owner's account 0x{:?} for code {:?} has some non-unreservable deposit {:?} from a total of {:?} that will remain in reserved.", + HexDisplay::from(&code_info.owner.encode()), + hash, + remaining, + code_info.deposit + ); + } + + let unreserved = code_info.deposit.saturating_sub(remaining); + let amount = BalanceOf::::from(unreserved); + + log::debug!( + target: LOG_TARGET, + "Holding {:?} on the code owner's account 0x{:?} for code {:?}.", + amount, + HexDisplay::from(&code_info.owner.encode()), + hash, + ); + + T::Currency::hold( + &HoldReason::CodeUploadDepositReserve.into(), + &code_info.owner, + amount, + ) + .unwrap_or_else(|err| { + log::error!( + target: LOG_TARGET, + "Failed to hold {:?} from the code owner's account 0x{:?} for code {:?}, reason: {:?}.", + amount, + HexDisplay::from(&code_info.owner.encode()), + hash, + err + ); + }); + + self.last_code_hash = Some(hash); + meter.consume(T::WeightInfo::v14_migration_step()); + IsFinished::No + } else { + log::debug!(target: LOG_TARGET, "No more code upload deposit to migrate"); + meter.consume(T::WeightInfo::v14_migration_step()); + IsFinished::Yes + } + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade_step() -> Result, TryRuntimeError> { + let info: Vec<_> = v13::CodeInfoOf::::iter().collect(); + + let mut owner_balance_allocation = + BTreeMap::, BalanceAllocation>::new(); + + // Calculates the balance allocation by accumulating the code upload deposits of all codes + // owned by an owner. + for (_, code_info) in info { + owner_balance_allocation + .entry(code_info.owner.clone()) + .and_modify(|alloc| { + alloc.reserved = alloc.reserved.saturating_add(code_info.deposit); + }) + .or_insert(BalanceAllocation { + reserved: code_info.deposit, + total: OldCurrency::total_balance(&code_info.owner), + }); + } + + Ok(owner_balance_allocation.encode()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade_step(state: Vec) -> Result<(), TryRuntimeError> { + let owner_balance_allocation = + , BalanceAllocation> as Decode>::decode( + &mut &state[..], + ) + .expect("pre_upgrade_step provides a valid state; qed"); + + let mut total_held: BalanceOf = Zero::zero(); + let count = owner_balance_allocation.len(); + for (owner, old_balance_allocation) in owner_balance_allocation { + let held = + T::Currency::balance_on_hold(&HoldReason::CodeUploadDepositReserve.into(), &owner); + log::debug!( + target: LOG_TARGET, + "Validating code upload deposit for owner 0x{:?}, reserved: {:?}, held: {:?}", + HexDisplay::from(&owner.encode()), + old_balance_allocation.reserved, + held + ); + ensure!(held == old_balance_allocation.reserved.into(), "Held amount mismatch"); + + log::debug!( + target: LOG_TARGET, + "Validating total balance for owner 0x{:?}, new: {:?}, old: {:?}", + HexDisplay::from(&owner.encode()), + T::Currency::total_balance(&owner), + old_balance_allocation.total + ); + ensure!( + T::Currency::total_balance(&owner) == + BalanceOf::::decode(&mut &old_balance_allocation.total.encode()[..]) + .unwrap(), + "Balance mismatch " + ); + total_held += held; + } + + log::info!( + target: LOG_TARGET, + "Code owners processed: {:?}.", + count + ); + + log::info!( + target: LOG_TARGET, + "Total held amount for code upload deposit: {:?}", + total_held + ); + + Ok(()) + } +} diff --git a/pallets/contracts/src/migration/v15.rs b/pallets/contracts/src/migration/v15.rs new file mode 100644 index 00000000..11f07282 --- /dev/null +++ b/pallets/contracts/src/migration/v15.rs @@ -0,0 +1,332 @@ +// This file is part of Substrate. + +// 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. + +//! Move contracts' _reserved_ balance from the `deposit_account` to be _held_ in the contract's +//! account instead. Since [`Currency`](frame_support::traits::Currency) has been +//! [deprecated](https://github.com/paritytech/substrate/pull/12951), we need the deposits to be +//! handled by the [`frame_support::traits::fungible`] traits instead. For this transfer the +//! balance from the deposit account to the contract's account and hold it in there. +//! Then the deposit account is not needed anymore and we can get rid of it. + +use crate::{ + migration::{IsFinished, MigrationStep}, + weights::WeightInfo, + AccountIdOf, BalanceOf, CodeHash, Config, HoldReason, Pallet, TrieId, Weight, LOG_TARGET, +}; +#[cfg(feature = "try-runtime")] +use alloc::vec::Vec; +#[cfg(feature = "try-runtime")] +use frame_support::traits::fungible::InspectHold; +use frame_support::{ + pallet_prelude::*, + storage_alias, + traits::{ + fungible::{Mutate, MutateHold}, + tokens::{fungible::Inspect, Fortitude, Preservation}, + }, + weights::WeightMeter, + BoundedBTreeMap, DefaultNoBound, +}; +use frame_system::Pallet as System; +use sp_core::hexdisplay::HexDisplay; +#[cfg(feature = "try-runtime")] +use sp_runtime::TryRuntimeError; +use sp_runtime::{traits::Zero, Saturating}; + +mod v14 { + use super::*; + + #[derive( + Encode, Decode, CloneNoBound, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen, + )] + #[scale_info(skip_type_params(T))] + pub struct ContractInfo { + pub trie_id: TrieId, + pub deposit_account: AccountIdOf, + pub code_hash: CodeHash, + pub storage_bytes: u32, + pub storage_items: u32, + pub storage_byte_deposit: BalanceOf, + pub storage_item_deposit: BalanceOf, + pub storage_base_deposit: BalanceOf, + pub delegate_dependencies: + BoundedBTreeMap, BalanceOf, T::MaxDelegateDependencies>, + } + + #[storage_alias] + pub type ContractInfoOf = StorageMap< + Pallet, + Twox64Concat, + ::AccountId, + ContractInfo, + >; +} + +#[cfg(feature = "runtime-benchmarks")] +pub fn store_old_contract_info(account: T::AccountId, info: crate::ContractInfo) { + use sp_runtime::traits::{Hash, TrailingZeroInput}; + let entropy = (b"contract_depo_v1", account.clone()).using_encoded(T::Hashing::hash); + let deposit_account = Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref())) + .expect("infinite length input; no invalid inputs for type; qed"); + let info = v14::ContractInfo { + trie_id: info.trie_id.clone(), + deposit_account, + code_hash: info.code_hash, + storage_bytes: Default::default(), + storage_items: Default::default(), + storage_byte_deposit: info.storage_byte_deposit, + storage_item_deposit: Default::default(), + storage_base_deposit: info.storage_base_deposit(), + delegate_dependencies: info.delegate_dependencies().clone(), + }; + v14::ContractInfoOf::::insert(account, info); +} + +#[derive(Encode, Decode, CloneNoBound, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] +#[scale_info(skip_type_params(T))] +struct ContractInfo { + pub trie_id: TrieId, + pub code_hash: CodeHash, + pub storage_bytes: u32, + pub storage_items: u32, + pub storage_byte_deposit: BalanceOf, + pub storage_item_deposit: BalanceOf, + pub storage_base_deposit: BalanceOf, + pub delegate_dependencies: + BoundedBTreeMap, BalanceOf, T::MaxDelegateDependencies>, +} + +#[storage_alias] +type ContractInfoOf = + StorageMap, Twox64Concat, ::AccountId, ContractInfo>; + +#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)] +pub struct Migration { + last_account: Option, +} + +impl MigrationStep for Migration { + const VERSION: u16 = 15; + + fn max_step_weight() -> Weight { + T::WeightInfo::v15_migration_step() + } + + fn step(&mut self, meter: &mut WeightMeter) -> IsFinished { + let mut iter = if let Some(last_account) = self.last_account.take() { + v14::ContractInfoOf::::iter_from(v14::ContractInfoOf::::hashed_key_for( + last_account, + )) + } else { + v14::ContractInfoOf::::iter() + }; + + if let Some((account, old_contract)) = iter.next() { + let deposit_account = &old_contract.deposit_account; + System::::dec_consumers(deposit_account); + + // Get the deposit balance to transfer. + let total_deposit_balance = T::Currency::total_balance(deposit_account); + let reducible_deposit_balance = T::Currency::reducible_balance( + deposit_account, + Preservation::Expendable, + Fortitude::Force, + ); + + if total_deposit_balance > reducible_deposit_balance { + // This should never happen, as by design all balance in the deposit account is + // storage deposit and therefore reducible after decrementing the consumer + // reference. + log::warn!( + target: LOG_TARGET, + "Deposit account 0x{:?} for contract 0x{:?} has some non-reducible balance {:?} from a total of {:?} that will remain in there.", + HexDisplay::from(&deposit_account.encode()), + HexDisplay::from(&account.encode()), + total_deposit_balance.saturating_sub(reducible_deposit_balance), + total_deposit_balance + ); + } + + // Move balance reserved from the deposit account back to the contract account. + // Let the deposit account die. + log::debug!( + target: LOG_TARGET, + "Transferring {:?} from the deposit account 0x{:?} to the contract 0x{:?}.", + reducible_deposit_balance, + HexDisplay::from(&deposit_account.encode()), + HexDisplay::from(&account.encode()) + ); + let transferred_deposit_balance = T::Currency::transfer( + deposit_account, + &account, + reducible_deposit_balance, + Preservation::Expendable, + ) + .unwrap_or_else(|err| { + log::error!( + target: LOG_TARGET, + "Failed to transfer {:?} from the deposit account 0x{:?} to the contract 0x{:?}, reason: {:?}.", + reducible_deposit_balance, + HexDisplay::from(&deposit_account.encode()), + HexDisplay::from(&account.encode()), + err + ); + Zero::zero() + }); + + // Hold the reserved balance. + if transferred_deposit_balance == Zero::zero() { + log::warn!( + target: LOG_TARGET, + "No balance to hold as storage deposit on the contract 0x{:?}.", + HexDisplay::from(&account.encode()) + ); + } else { + log::debug!( + target: LOG_TARGET, + "Holding {:?} as storage deposit on the contract 0x{:?}.", + transferred_deposit_balance, + HexDisplay::from(&account.encode()) + ); + + T::Currency::hold( + &HoldReason::StorageDepositReserve.into(), + &account, + transferred_deposit_balance, + ) + .unwrap_or_else(|err| { + log::error!( + target: LOG_TARGET, + "Failed to hold {:?} as storage deposit on the contract 0x{:?}, reason: {:?}.", + transferred_deposit_balance, + HexDisplay::from(&account.encode()), + err + ); + }); + } + + log::debug!(target: LOG_TARGET, "==="); + let info = ContractInfo { + trie_id: old_contract.trie_id, + code_hash: old_contract.code_hash, + storage_bytes: old_contract.storage_bytes, + storage_items: old_contract.storage_items, + storage_byte_deposit: old_contract.storage_byte_deposit, + storage_item_deposit: old_contract.storage_item_deposit, + storage_base_deposit: old_contract.storage_base_deposit, + delegate_dependencies: old_contract.delegate_dependencies, + }; + ContractInfoOf::::insert(account.clone(), info); + + // Store last key for next migration step + self.last_account = Some(account); + + meter.consume(T::WeightInfo::v15_migration_step()); + IsFinished::No + } else { + log::info!(target: LOG_TARGET, "Done Migrating Storage Deposits."); + meter.consume(T::WeightInfo::v15_migration_step()); + IsFinished::Yes + } + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade_step() -> Result, TryRuntimeError> { + let sample: Vec<_> = v14::ContractInfoOf::::iter().take(100).collect(); + + log::debug!(target: LOG_TARGET, "Taking sample of {} contracts", sample.len()); + + let state: Vec<(T::AccountId, v14::ContractInfo, BalanceOf, BalanceOf)> = sample + .iter() + .map(|(account, contract)| { + ( + account.clone(), + contract.clone(), + T::Currency::total_balance(&account), + T::Currency::total_balance(&contract.deposit_account), + ) + }) + .collect(); + + Ok(state.encode()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade_step(state: Vec) -> Result<(), TryRuntimeError> { + let sample = + , BalanceOf, BalanceOf)> as Decode>::decode( + &mut &state[..], + ) + .expect("pre_upgrade_step provides a valid state; qed"); + + log::debug!(target: LOG_TARGET, "Validating sample of {} contracts", sample.len()); + for (account, old_contract, old_account_balance, old_deposit_balance) in sample { + log::debug!(target: LOG_TARGET, "==="); + log::debug!(target: LOG_TARGET, "Account: 0x{} ", HexDisplay::from(&account.encode())); + + let on_hold = + T::Currency::balance_on_hold(&HoldReason::StorageDepositReserve.into(), &account); + let account_balance = T::Currency::total_balance(&account); + + log::debug!( + target: LOG_TARGET, + "Validating balances match. Old deposit account's balance: {:?}. Contract's on hold: {:?}. Old contract's total balance: {:?}, Contract's total balance: {:?}.", + old_deposit_balance, + on_hold, + old_account_balance, + account_balance + ); + ensure!( + old_account_balance.saturating_add(old_deposit_balance) == account_balance, + "total balance mismatch" + ); + ensure!(old_deposit_balance == on_hold, "deposit mismatch"); + ensure!( + !System::::account_exists(&old_contract.deposit_account), + "deposit account still exists" + ); + + let migration_contract_info = ContractInfoOf::::try_get(&account).unwrap(); + let crate_contract_info = crate::ContractInfoOf::::try_get(&account).unwrap(); + ensure!( + migration_contract_info.trie_id == crate_contract_info.trie_id, + "trie_id mismatch" + ); + ensure!( + migration_contract_info.code_hash == crate_contract_info.code_hash, + "code_hash mismatch" + ); + ensure!( + migration_contract_info.storage_byte_deposit == + crate_contract_info.storage_byte_deposit, + "storage_byte_deposit mismatch" + ); + ensure!( + migration_contract_info.storage_base_deposit == + crate_contract_info.storage_base_deposit(), + "storage_base_deposit mismatch" + ); + ensure!( + &migration_contract_info.delegate_dependencies == + crate_contract_info.delegate_dependencies(), + "delegate_dependencies mismatch" + ); + } + + Ok(()) + } +} diff --git a/pallets/contracts/src/migration/v16.rs b/pallets/contracts/src/migration/v16.rs new file mode 100644 index 00000000..3d5b2d2a --- /dev/null +++ b/pallets/contracts/src/migration/v16.rs @@ -0,0 +1,106 @@ +// This file is part of Substrate. + +// 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. + +//! Remove ED from storage base deposit. +//! See . + +use crate::{ + migration::{IsFinished, MigrationStep}, + weights::WeightInfo, + BalanceOf, CodeHash, Config, Pallet, TrieId, Weight, WeightMeter, LOG_TARGET, +}; +use codec::{Decode, Encode}; +use frame_support::{pallet_prelude::*, storage_alias, DefaultNoBound}; +use sp_runtime::{BoundedBTreeMap, Saturating}; + +#[cfg(feature = "runtime-benchmarks")] +pub fn store_old_contract_info( + account: T::AccountId, + info: &crate::ContractInfo, +) -> BalanceOf { + let storage_base_deposit = Pallet::::min_balance() + 1u32.into(); + ContractInfoOf::::insert( + account, + ContractInfo { + trie_id: info.trie_id.clone(), + code_hash: info.code_hash, + storage_bytes: Default::default(), + storage_items: Default::default(), + storage_byte_deposit: Default::default(), + storage_item_deposit: Default::default(), + storage_base_deposit, + delegate_dependencies: Default::default(), + }, + ); + + storage_base_deposit +} + +#[storage_alias] +pub type ContractInfoOf = + StorageMap, Twox64Concat, ::AccountId, ContractInfo>; + +#[derive(Encode, Decode, CloneNoBound, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] +#[scale_info(skip_type_params(T))] +pub struct ContractInfo { + trie_id: TrieId, + code_hash: CodeHash, + storage_bytes: u32, + storage_items: u32, + storage_byte_deposit: BalanceOf, + storage_item_deposit: BalanceOf, + pub storage_base_deposit: BalanceOf, + delegate_dependencies: BoundedBTreeMap, BalanceOf, T::MaxDelegateDependencies>, +} + +#[derive(Encode, Decode, MaxEncodedLen, DefaultNoBound)] +pub struct Migration { + last_account: Option, +} + +impl MigrationStep for Migration { + const VERSION: u16 = 16; + + fn max_step_weight() -> Weight { + T::WeightInfo::v16_migration_step() + } + + fn step(&mut self, meter: &mut WeightMeter) -> IsFinished { + let mut iter = if let Some(last_account) = self.last_account.take() { + ContractInfoOf::::iter_keys_from(ContractInfoOf::::hashed_key_for(last_account)) + } else { + ContractInfoOf::::iter_keys() + }; + + if let Some(key) = iter.next() { + log::debug!(target: LOG_TARGET, "Migrating contract {:?}", key); + ContractInfoOf::::mutate(key.clone(), |info| { + let ed = Pallet::::min_balance(); + let mut updated_info = info.take().expect("Item exists; qed"); + updated_info.storage_base_deposit.saturating_reduce(ed); + *info = Some(updated_info); + }); + self.last_account = Some(key); + meter.consume(T::WeightInfo::v16_migration_step()); + IsFinished::No + } else { + log::debug!(target: LOG_TARGET, "No more contracts to migrate"); + meter.consume(T::WeightInfo::v16_migration_step()); + IsFinished::Yes + } + } +} diff --git a/pallets/contracts/src/primitives.rs b/pallets/contracts/src/primitives.rs new file mode 100644 index 00000000..622a69f5 --- /dev/null +++ b/pallets/contracts/src/primitives.rs @@ -0,0 +1,252 @@ +// This file is part of Substrate. + +// 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. + +//! A crate that hosts a common definitions that are relevant for the pallet-contracts. + +use alloc::vec::Vec; +use codec::{Decode, Encode, MaxEncodedLen}; +use frame_support::weights::Weight; +use pallet_contracts_uapi::ReturnFlags; +use scale_info::TypeInfo; +use sp_runtime::{ + traits::{Saturating, Zero}, + DispatchError, RuntimeDebug, +}; + +/// Result type of a `bare_call` or `bare_instantiate` call as well as `ContractsApi::call` and +/// `ContractsApi::instantiate`. +/// +/// It contains the execution result together with some auxiliary information. +/// +/// #Note +/// +/// It has been extended to include `events` at the end of the struct while not bumping the +/// `ContractsApi` version. Therefore when SCALE decoding a `ContractResult` its trailing data +/// should be ignored to avoid any potential compatibility issues. +#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)] +pub struct ContractResult { + /// How much weight was consumed during execution. + pub gas_consumed: Weight, + /// How much weight is required as gas limit in order to execute this call. + /// + /// This value should be used to determine the weight limit for on-chain execution. + /// + /// # Note + /// + /// This can only different from [`Self::gas_consumed`] when weight pre charging + /// is used. Currently, only `seal_call_runtime` makes use of pre charging. + /// Additionally, any `seal_call` or `seal_instantiate` makes use of pre-charging + /// when a non-zero `gas_limit` argument is supplied. + pub gas_required: Weight, + /// How much balance was paid by the origin into the contract's deposit account in order to + /// pay for storage. + /// + /// The storage deposit is never actually charged from the origin in case of [`Self::result`] + /// is `Err`. This is because on error all storage changes are rolled back including the + /// payment of the deposit. + pub storage_deposit: StorageDeposit, + /// An optional debug message. This message is only filled when explicitly requested + /// by the code that calls into the contract. Otherwise it is empty. + /// + /// The contained bytes are valid UTF-8. This is not declared as `String` because + /// this type is not allowed within the runtime. + /// + /// Clients should not make any assumptions about the format of the buffer. + /// They should just display it as-is. It is **not** only a collection of log lines + /// provided by a contract but a formatted buffer with different sections. + /// + /// # Note + /// + /// The debug message is never generated during on-chain execution. It is reserved for + /// RPC calls. + pub debug_message: Vec, + /// The execution result of the wasm code. + pub result: R, + /// The events that were emitted during execution. It is an option as event collection is + /// optional. + pub events: Option>, +} + +/// Result type of a `bare_call` call as well as `ContractsApi::call`. +pub type ContractExecResult = + ContractResult, Balance, EventRecord>; + +/// Result type of a `bare_instantiate` call as well as `ContractsApi::instantiate`. +pub type ContractInstantiateResult = + ContractResult, DispatchError>, Balance, EventRecord>; + +/// Result type of a `bare_code_upload` call. +pub type CodeUploadResult = + Result, DispatchError>; + +/// Result type of a `get_storage` call. +pub type GetStorageResult = Result>, ContractAccessError>; + +/// The possible errors that can happen querying the storage of a contract. +#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, MaxEncodedLen, RuntimeDebug, TypeInfo)] +pub enum ContractAccessError { + /// The given address doesn't point to a contract. + DoesntExist, + /// Storage key cannot be decoded from the provided input data. + KeyDecodingFailed, + /// Storage is migrating. Try again later. + MigrationInProgress, +} + +/// Output of a contract call or instantiation which ran to completion. +#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)] +pub struct ExecReturnValue { + /// Flags passed along by `seal_return`. Empty when `seal_return` was never called. + pub flags: ReturnFlags, + /// Buffer passed along by `seal_return`. Empty when `seal_return` was never called. + pub data: Vec, +} + +impl ExecReturnValue { + /// The contract did revert all storage changes. + pub fn did_revert(&self) -> bool { + self.flags.contains(ReturnFlags::REVERT) + } +} + +/// The result of a successful contract instantiation. +#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)] +pub struct InstantiateReturnValue { + /// The output of the called constructor. + pub result: ExecReturnValue, + /// The account id of the new contract. + pub account_id: AccountId, +} + +/// The result of successfully uploading a contract. +#[derive(Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen, RuntimeDebug, TypeInfo)] +pub struct CodeUploadReturnValue { + /// The key under which the new code is stored. + pub code_hash: CodeHash, + /// The deposit that was reserved at the caller. Is zero when the code already existed. + pub deposit: Balance, +} + +/// Reference to an existing code hash or a new wasm module. +#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)] +pub enum Code { + /// A wasm module as raw bytes. + Upload(Vec), + /// The code hash of an on-chain wasm blob. + Existing(Hash), +} + +/// The amount of balance that was either charged or refunded in order to pay for storage. +#[derive( + Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, RuntimeDebug, TypeInfo, +)] +pub enum StorageDeposit { + /// The transaction reduced storage consumption. + /// + /// This means that the specified amount of balance was transferred from the involved + /// deposit accounts to the origin. + Refund(Balance), + /// The transaction increased storage consumption. + /// + /// This means that the specified amount of balance was transferred from the origin + /// to the involved deposit accounts. + Charge(Balance), +} + +impl Default for StorageDeposit { + fn default() -> Self { + Self::Charge(Zero::zero()) + } +} + +impl StorageDeposit { + /// Returns how much balance is charged or `0` in case of a refund. + pub fn charge_or_zero(&self) -> Balance { + match self { + Self::Charge(amount) => *amount, + Self::Refund(_) => Zero::zero(), + } + } + + pub fn is_zero(&self) -> bool { + match self { + Self::Charge(amount) => amount.is_zero(), + Self::Refund(amount) => amount.is_zero(), + } + } +} + +impl StorageDeposit +where + Balance: Saturating + Ord + Copy, +{ + /// This is essentially a saturating signed add. + pub fn saturating_add(&self, rhs: &Self) -> Self { + use StorageDeposit::*; + match (self, rhs) { + (Charge(lhs), Charge(rhs)) => Charge(lhs.saturating_add(*rhs)), + (Refund(lhs), Refund(rhs)) => Refund(lhs.saturating_add(*rhs)), + (Charge(lhs), Refund(rhs)) => + if lhs >= rhs { + Charge(lhs.saturating_sub(*rhs)) + } else { + Refund(rhs.saturating_sub(*lhs)) + }, + (Refund(lhs), Charge(rhs)) => + if lhs > rhs { + Refund(lhs.saturating_sub(*rhs)) + } else { + Charge(rhs.saturating_sub(*lhs)) + }, + } + } + + /// This is essentially a saturating signed sub. + pub fn saturating_sub(&self, rhs: &Self) -> Self { + use StorageDeposit::*; + match (self, rhs) { + (Charge(lhs), Refund(rhs)) => Charge(lhs.saturating_add(*rhs)), + (Refund(lhs), Charge(rhs)) => Refund(lhs.saturating_add(*rhs)), + (Charge(lhs), Charge(rhs)) => + if lhs >= rhs { + Charge(lhs.saturating_sub(*rhs)) + } else { + Refund(rhs.saturating_sub(*lhs)) + }, + (Refund(lhs), Refund(rhs)) => + if lhs > rhs { + Refund(lhs.saturating_sub(*rhs)) + } else { + Charge(rhs.saturating_sub(*lhs)) + }, + } + } + + /// If the amount of deposit (this type) is constrained by a `limit` this calculates how + /// much balance (if any) is still available from this limit. + /// + /// # Note + /// + /// In case of a refund the return value can be larger than `limit`. + pub fn available(&self, limit: &Balance) -> Balance { + use StorageDeposit::*; + match self { + Charge(amount) => limit.saturating_sub(*amount), + Refund(amount) => limit.saturating_add(*amount), + } + } +} diff --git a/pallets/contracts/src/schedule.rs b/pallets/contracts/src/schedule.rs new file mode 100644 index 00000000..80b8c54b --- /dev/null +++ b/pallets/contracts/src/schedule.rs @@ -0,0 +1,149 @@ +// This file is part of Substrate. + +// 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. + +//! This module contains the cost schedule and supporting code that constructs a +//! sane default schedule from a `WeightInfo` implementation. + +use crate::{weights::WeightInfo, Config}; + +use codec::{Decode, Encode}; +use core::marker::PhantomData; +use frame_support::DefaultNoBound; +use scale_info::TypeInfo; +#[cfg(feature = "std")] +use serde::{Deserialize, Serialize}; + +/// Definition of the cost schedule and other parameterizations for the wasm vm. +/// +/// Its [`Default`] implementation is the designated way to initialize this type. It uses +/// the benchmarked information supplied by [`Config::WeightInfo`]. All of its fields are +/// public and can therefore be modified. For example in order to change some of the limits +/// and set a custom instruction weight version the following code could be used: +/// ```rust +/// use pallet_contracts::{Schedule, Limits, InstructionWeights, Config}; +/// +/// fn create_schedule() -> Schedule { +/// Schedule { +/// limits: Limits { +/// memory_pages: 16, +/// .. Default::default() +/// }, +/// instruction_weights: InstructionWeights { +/// .. Default::default() +/// }, +/// .. Default::default() +/// } +/// } +/// ``` +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "std", serde(bound(serialize = "", deserialize = "")))] +#[cfg_attr(feature = "runtime-benchmarks", derive(frame_support::DebugNoBound))] +#[derive(Clone, Encode, Decode, PartialEq, Eq, DefaultNoBound, TypeInfo)] +#[scale_info(skip_type_params(T))] +pub struct Schedule { + /// Describes the upper limits on various metrics. + pub limits: Limits, + + /// The weights for individual wasm instructions. + pub instruction_weights: InstructionWeights, +} + +impl Schedule { + /// Returns the reference time per engine fuel. + pub fn ref_time_by_fuel(&self) -> u64 { + self.instruction_weights.base as u64 + } +} + +/// Describes the upper limits on various metrics. +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "runtime-benchmarks", derive(Debug))] +#[derive(Clone, Encode, Decode, PartialEq, Eq, TypeInfo)] +pub struct Limits { + /// The maximum number of topics supported by an event. + pub event_topics: u32, + + /// Maximum number of memory pages allowed for a contract. + pub memory_pages: u32, + + /// The maximum length of a subject in bytes used for PRNG generation. + pub subject_len: u32, + + /// The maximum size of a storage value and event payload in bytes. + pub payload_len: u32, + + /// The maximum node runtime memory. This is for integrity checks only and does not affect the + /// real setting. + pub runtime_memory: u32, + + /// The maximum validator node runtime memory. This is for integrity checks only and does not + /// affect the real setting. + pub validator_runtime_memory: u32, + + /// The additional ref_time added to the `deposit_event` host function call per event data + /// byte. + pub event_ref_time: u64, +} + +impl Limits { + /// The maximum memory size in bytes that a contract can occupy. + pub fn max_memory_size(&self) -> u32 { + self.memory_pages * 64 * 1024 + } +} + +/// Gas metering of Wasm executed instructions is being done on the engine side. +/// This struct holds a reference value used to gas units scaling between host and engine. +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "runtime-benchmarks", derive(frame_support::DebugNoBound))] +#[derive(Clone, Encode, Decode, PartialEq, Eq, TypeInfo)] +#[scale_info(skip_type_params(T))] +pub struct InstructionWeights { + /// Base instruction `ref_time` Weight. + /// Should match to wasmi's `1` fuel (see ). + pub base: u32, + /// The type parameter is used in the default implementation. + #[codec(skip)] + pub _phantom: PhantomData, +} + +impl Default for Limits { + fn default() -> Self { + Self { + event_topics: 4, + memory_pages: 16, + subject_len: 32, + payload_len: 16 * 1024, + runtime_memory: 1024 * 1024 * 128, + validator_runtime_memory: 1024 * 1024 * 512, + event_ref_time: 60_000, + } + } +} + +impl Default for InstructionWeights { + /// We execute 6 different instructions therefore we have to divide the actual + /// computed gas costs by 6 to have a rough estimate as to how expensive each + /// single executed instruction is going to be. + fn default() -> Self { + let instr_cost = T::WeightInfo::instr_i64_load_store(1) + .saturating_sub(T::WeightInfo::instr_i64_load_store(0)) + .ref_time() as u32; + let base = instr_cost / 6; + Self { base, _phantom: PhantomData } + } +} diff --git a/pallets/contracts/src/storage.rs b/pallets/contracts/src/storage.rs new file mode 100644 index 00000000..c0201266 --- /dev/null +++ b/pallets/contracts/src/storage.rs @@ -0,0 +1,480 @@ +// This file is part of Substrate. + +// 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. + +//! This module contains routines for accessing and altering a contract related state. + +pub mod meter; + +use crate::{ + exec::{AccountIdOf, Key}, + weights::WeightInfo, + BalanceOf, CodeHash, CodeInfo, Config, ContractInfoOf, DeletionQueue, DeletionQueueCounter, + Error, TrieId, SENTINEL, +}; +use alloc::vec::Vec; +use codec::{Decode, Encode, MaxEncodedLen}; +use core::marker::PhantomData; +use frame_support::{ + storage::child::{self, ChildInfo}, + weights::{Weight, WeightMeter}, + CloneNoBound, DefaultNoBound, +}; +use scale_info::TypeInfo; +use sp_core::Get; +use sp_io::KillStorageResult; +use sp_runtime::{ + traits::{Hash, Saturating, Zero}, + BoundedBTreeMap, DispatchError, DispatchResult, RuntimeDebug, +}; + +use self::meter::Diff; + +/// Information for managing an account and its sub trie abstraction. +/// This is the required info to cache for an account. +#[derive(Encode, Decode, CloneNoBound, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] +#[scale_info(skip_type_params(T))] +pub struct ContractInfo { + /// Unique ID for the subtree encoded as a bytes vector. + pub trie_id: TrieId, + /// The code associated with a given account. + pub code_hash: CodeHash, + /// How many bytes of storage are accumulated in this contract's child trie. + storage_bytes: u32, + /// How many items of storage are accumulated in this contract's child trie. + storage_items: u32, + /// This records to how much deposit the accumulated `storage_bytes` amount to. + pub storage_byte_deposit: BalanceOf, + /// This records to how much deposit the accumulated `storage_items` amount to. + storage_item_deposit: BalanceOf, + /// This records how much deposit is put down in order to pay for the contract itself. + /// + /// We need to store this information separately so it is not used when calculating any refunds + /// since the base deposit can only ever be refunded on contract termination. + storage_base_deposit: BalanceOf, + /// Map of code hashes and deposit balances. + /// + /// Tracks the code hash and deposit held for locking delegate dependencies. Dependencies added + /// to the map can not be removed from the chain state and can be safely used for delegate + /// calls. + delegate_dependencies: BoundedBTreeMap, BalanceOf, T::MaxDelegateDependencies>, +} + +impl ContractInfo { + /// Constructs a new contract info **without** writing it to storage. + /// + /// This returns an `Err` if an contract with the supplied `account` already exists + /// in storage. + pub fn new( + account: &AccountIdOf, + nonce: u64, + code_hash: CodeHash, + ) -> Result { + if >::contains_key(account) { + return Err(Error::::DuplicateContract.into()) + } + + let trie_id = { + let buf = (account, nonce).using_encoded(T::Hashing::hash); + buf.as_ref() + .to_vec() + .try_into() + .expect("Runtime uses a reasonable hash size. Hence sizeof(T::Hash) <= 128; qed") + }; + + let contract = Self { + trie_id, + code_hash, + storage_bytes: 0, + storage_items: 0, + storage_byte_deposit: Zero::zero(), + storage_item_deposit: Zero::zero(), + storage_base_deposit: Zero::zero(), + delegate_dependencies: Default::default(), + }; + + Ok(contract) + } + + /// Returns the number of locked delegate dependencies. + pub fn delegate_dependencies_count(&self) -> usize { + self.delegate_dependencies.len() + } + + /// Associated child trie unique id is built from the hash part of the trie id. + pub fn child_trie_info(&self) -> ChildInfo { + ChildInfo::new_default(self.trie_id.as_ref()) + } + + /// The deposit paying for the accumulated storage generated within the contract's child trie. + pub fn extra_deposit(&self) -> BalanceOf { + self.storage_byte_deposit.saturating_add(self.storage_item_deposit) + } + + /// Same as [`Self::extra_deposit`] but including the base deposit. + pub fn total_deposit(&self) -> BalanceOf { + self.extra_deposit().saturating_add(self.storage_base_deposit) + } + + /// Returns the storage base deposit of the contract. + pub fn storage_base_deposit(&self) -> BalanceOf { + self.storage_base_deposit + } + + /// Reads a storage kv pair of a contract. + /// + /// The read is performed from the `trie_id` only. The `address` is not necessary. If the + /// contract doesn't store under the given `key` `None` is returned. + pub fn read(&self, key: &Key) -> Option> { + child::get_raw(&self.child_trie_info(), key.hash().as_slice()) + } + + /// Returns `Some(len)` (in bytes) if a storage item exists at `key`. + /// + /// Returns `None` if the `key` wasn't previously set by `set_storage` or + /// was deleted. + pub fn size(&self, key: &Key) -> Option { + child::len(&self.child_trie_info(), key.hash().as_slice()) + } + + /// Update a storage entry into a contract's kv storage. + /// + /// If the `new_value` is `None` then the kv pair is removed. If `take` is true + /// a [`WriteOutcome::Taken`] is returned instead of a [`WriteOutcome::Overwritten`]. + /// + /// This function also records how much storage was created or removed if a `storage_meter` + /// is supplied. It should only be absent for testing or benchmarking code. + pub fn write( + &self, + key: &Key, + new_value: Option>, + storage_meter: Option<&mut meter::NestedMeter>, + take: bool, + ) -> Result { + let hashed_key = key.hash(); + self.write_raw(&hashed_key, new_value, storage_meter, take) + } + + /// Update a storage entry into a contract's kv storage. + /// Function used in benchmarks, which can simulate prefix collision in keys. + #[cfg(feature = "runtime-benchmarks")] + pub fn bench_write_raw( + &self, + key: &[u8], + new_value: Option>, + take: bool, + ) -> Result { + self.write_raw(key, new_value, None, take) + } + + fn write_raw( + &self, + key: &[u8], + new_value: Option>, + storage_meter: Option<&mut meter::NestedMeter>, + take: bool, + ) -> Result { + let child_trie_info = &self.child_trie_info(); + let (old_len, old_value) = if take { + let val = child::get_raw(child_trie_info, key); + (val.as_ref().map(|v| v.len() as u32), val) + } else { + (child::len(child_trie_info, key), None) + }; + + if let Some(storage_meter) = storage_meter { + let mut diff = meter::Diff::default(); + match (old_len, new_value.as_ref().map(|v| v.len() as u32)) { + (Some(old_len), Some(new_len)) => + if new_len > old_len { + diff.bytes_added = new_len - old_len; + } else { + diff.bytes_removed = old_len - new_len; + }, + (None, Some(new_len)) => { + diff.bytes_added = new_len; + diff.items_added = 1; + }, + (Some(old_len), None) => { + diff.bytes_removed = old_len; + diff.items_removed = 1; + }, + (None, None) => (), + } + storage_meter.charge(&diff); + } + + match &new_value { + Some(new_value) => child::put_raw(child_trie_info, key, new_value), + None => child::kill(child_trie_info, key), + } + + Ok(match (old_len, old_value) { + (None, _) => WriteOutcome::New, + (Some(old_len), None) => WriteOutcome::Overwritten(old_len), + (Some(_), Some(old_value)) => WriteOutcome::Taken(old_value), + }) + } + + /// Sets and returns the contract base deposit. + /// + /// The base deposit is updated when the `code_hash` of the contract changes, as it depends on + /// the deposit paid to upload the contract's code. + pub fn update_base_deposit(&mut self, code_info: &CodeInfo) -> BalanceOf { + let info_deposit = + Diff { bytes_added: self.encoded_size() as u32, items_added: 1, ..Default::default() } + .update_contract::(None) + .charge_or_zero(); + + // Instantiating the contract prevents its code to be deleted, therefore the base deposit + // includes a fraction (`T::CodeHashLockupDepositPercent`) of the original storage deposit + // to prevent abuse. + let upload_deposit = T::CodeHashLockupDepositPercent::get().mul_ceil(code_info.deposit()); + + let deposit = info_deposit.saturating_add(upload_deposit); + self.storage_base_deposit = deposit; + deposit + } + + /// Adds a new delegate dependency to the contract. + /// The `amount` is the amount of funds that will be reserved for the dependency. + /// + /// Returns an error if the maximum number of delegate_dependencies is reached or if + /// the delegate dependency already exists. + pub fn lock_delegate_dependency( + &mut self, + code_hash: CodeHash, + amount: BalanceOf, + ) -> DispatchResult { + self.delegate_dependencies + .try_insert(code_hash, amount) + .map_err(|_| Error::::MaxDelegateDependenciesReached)? + .map_or(Ok(()), |_| Err(Error::::DelegateDependencyAlreadyExists)) + .map_err(Into::into) + } + + /// Removes the delegate dependency from the contract and returns the deposit held for this + /// dependency. + /// + /// Returns an error if the entry doesn't exist. + pub fn unlock_delegate_dependency( + &mut self, + code_hash: &CodeHash, + ) -> Result, DispatchError> { + self.delegate_dependencies + .remove(code_hash) + .ok_or(Error::::DelegateDependencyNotFound.into()) + } + + /// Returns the delegate_dependencies of the contract. + pub fn delegate_dependencies( + &self, + ) -> &BoundedBTreeMap, BalanceOf, T::MaxDelegateDependencies> { + &self.delegate_dependencies + } + + /// Push a contract's trie to the deletion queue for lazy removal. + /// + /// You must make sure that the contract is also removed when queuing the trie for deletion. + pub fn queue_trie_for_deletion(&self) { + DeletionQueueManager::::load().insert(self.trie_id.clone()); + } + + /// Calculates the weight that is necessary to remove one key from the trie and how many + /// of those keys can be deleted from the deletion queue given the supplied weight limit. + pub fn deletion_budget(meter: &WeightMeter) -> (Weight, u32) { + let base_weight = T::WeightInfo::on_process_deletion_queue_batch(); + let weight_per_key = T::WeightInfo::on_initialize_per_trie_key(1) - + T::WeightInfo::on_initialize_per_trie_key(0); + + // `weight_per_key` being zero makes no sense and would constitute a failure to + // benchmark properly. We opt for not removing any keys at all in this case. + let key_budget = meter + .limit() + .saturating_sub(base_weight) + .checked_div_per_component(&weight_per_key) + .unwrap_or(0) as u32; + + (weight_per_key, key_budget) + } + + /// Delete as many items from the deletion queue possible within the supplied weight limit. + pub fn process_deletion_queue_batch(meter: &mut WeightMeter) { + if meter.try_consume(T::WeightInfo::on_process_deletion_queue_batch()).is_err() { + return + }; + + let mut queue = >::load(); + if queue.is_empty() { + return; + } + + let (weight_per_key, budget) = Self::deletion_budget(&meter); + let mut remaining_key_budget = budget; + while remaining_key_budget > 0 { + let Some(entry) = queue.next() else { break }; + + #[allow(deprecated)] + let outcome = child::kill_storage( + &ChildInfo::new_default(&entry.trie_id), + Some(remaining_key_budget), + ); + + match outcome { + // This happens when our budget wasn't large enough to remove all keys. + KillStorageResult::SomeRemaining(keys_removed) => { + remaining_key_budget.saturating_reduce(keys_removed); + break + }, + KillStorageResult::AllRemoved(keys_removed) => { + entry.remove(); + // charge at least one key even if none were removed. + remaining_key_budget = remaining_key_budget.saturating_sub(keys_removed.max(1)); + }, + }; + } + + meter.consume(weight_per_key.saturating_mul(u64::from(budget - remaining_key_budget))) + } + + /// Returns the code hash of the contract specified by `account` ID. + pub fn load_code_hash(account: &AccountIdOf) -> Option> { + >::get(account).map(|i| i.code_hash) + } +} + +/// Information about what happened to the pre-existing value when calling [`ContractInfo::write`]. +#[cfg_attr(any(test, feature = "runtime-benchmarks"), derive(Debug, PartialEq))] +pub enum WriteOutcome { + /// No value existed at the specified key. + New, + /// A value of the returned length was overwritten. + Overwritten(u32), + /// The returned value was taken out of storage before being overwritten. + /// + /// This is only returned when specifically requested because it causes additional work + /// depending on the size of the pre-existing value. When not requested [`Self::Overwritten`] + /// is returned instead. + Taken(Vec), +} + +impl WriteOutcome { + /// Extracts the size of the overwritten value or `0` if there + /// was no value in storage. + pub fn old_len(&self) -> u32 { + match self { + Self::New => 0, + Self::Overwritten(len) => *len, + Self::Taken(value) => value.len() as u32, + } + } + + /// Extracts the size of the overwritten value or `SENTINEL` if there + /// was no value in storage. + /// + /// # Note + /// + /// We cannot use `0` as sentinel value because there could be a zero sized + /// storage entry which is different from a non existing one. + pub fn old_len_with_sentinel(&self) -> u32 { + match self { + Self::New => SENTINEL, + Self::Overwritten(len) => *len, + Self::Taken(value) => value.len() as u32, + } + } +} + +/// Manage the removal of contracts storage that are marked for deletion. +/// +/// When a contract is deleted by calling `seal_terminate` it becomes inaccessible +/// immediately, but the deletion of the storage items it has accumulated is performed +/// later by pulling the contract from the queue in the `on_idle` hook. +#[derive(Encode, Decode, TypeInfo, MaxEncodedLen, DefaultNoBound, Clone)] +#[scale_info(skip_type_params(T))] +pub struct DeletionQueueManager { + /// Counter used as a key for inserting a new deleted contract in the queue. + /// The counter is incremented after each insertion. + insert_counter: u32, + /// The index used to read the next element to be deleted in the queue. + /// The counter is incremented after each deletion. + delete_counter: u32, + + _phantom: PhantomData, +} + +/// View on a contract that is marked for deletion. +struct DeletionQueueEntry<'a, T: Config> { + /// the trie id of the contract to delete. + trie_id: TrieId, + + /// A mutable reference on the queue so that the contract can be removed, and none can be added + /// or read in the meantime. + queue: &'a mut DeletionQueueManager, +} + +impl<'a, T: Config> DeletionQueueEntry<'a, T> { + /// Remove the contract from the deletion queue. + fn remove(self) { + >::remove(self.queue.delete_counter); + self.queue.delete_counter = self.queue.delete_counter.wrapping_add(1); + >::set(self.queue.clone()); + } +} + +impl DeletionQueueManager { + /// Load the `DeletionQueueCounter`, so we can perform read or write operations on the + /// DeletionQueue storage. + fn load() -> Self { + >::get() + } + + /// Returns `true` if the queue contains no elements. + fn is_empty(&self) -> bool { + self.insert_counter.wrapping_sub(self.delete_counter) == 0 + } + + /// Insert a contract in the deletion queue. + fn insert(&mut self, trie_id: TrieId) { + >::insert(self.insert_counter, trie_id); + self.insert_counter = self.insert_counter.wrapping_add(1); + >::set(self.clone()); + } + + /// Fetch the next contract to be deleted. + /// + /// Note: + /// we use the delete counter to get the next value to read from the queue and thus don't pay + /// the cost of an extra call to `sp_io::storage::next_key` to lookup the next entry in the map + fn next(&mut self) -> Option> { + if self.is_empty() { + return None + } + + let entry = >::get(self.delete_counter); + entry.map(|trie_id| DeletionQueueEntry { trie_id, queue: self }) + } +} + +#[cfg(test)] +impl DeletionQueueManager { + pub fn from_test_values(insert_counter: u32, delete_counter: u32) -> Self { + Self { insert_counter, delete_counter, _phantom: Default::default() } + } + pub fn as_test_tuple(&self) -> (u32, u32) { + (self.insert_counter, self.delete_counter) + } +} diff --git a/pallets/contracts/src/storage/meter.rs b/pallets/contracts/src/storage/meter.rs new file mode 100644 index 00000000..951cb259 --- /dev/null +++ b/pallets/contracts/src/storage/meter.rs @@ -0,0 +1,908 @@ +// This file is part of Substrate. + +// 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. + +//! This module contains functions to meter the storage deposit. + +use crate::{ + storage::ContractInfo, AccountIdOf, BalanceOf, CodeInfo, Config, Error, Event, HoldReason, + Inspect, Origin, Pallet, StorageDeposit as Deposit, System, LOG_TARGET, +}; + +use alloc::vec::Vec; +use core::{fmt::Debug, marker::PhantomData}; +use frame_support::{ + ensure, + traits::{ + fungible::{Mutate, MutateHold}, + tokens::{ + Fortitude, Fortitude::Polite, Precision, Preservation, Restriction, WithdrawConsequence, + }, + Get, + }, + DefaultNoBound, RuntimeDebugNoBound, +}; +use sp_runtime::{ + traits::{Saturating, Zero}, + DispatchError, FixedPointNumber, FixedU128, +}; + +/// Deposit that uses the native fungible's balance type. +pub type DepositOf = Deposit>; + +/// A production root storage meter that actually charges from its origin. +pub type Meter = RawMeter; + +/// A production nested storage meter that actually charges from its origin. +pub type NestedMeter = RawMeter; + +/// A production storage meter that actually charges from its origin. +/// +/// This can be used where we want to be generic over the state (Root vs. Nested). +pub type GenericMeter = RawMeter; + +/// A trait that allows to decouple the metering from the charging of balance. +/// +/// This mostly exists for testing so that the charging can be mocked. +pub trait Ext { + /// This checks whether `origin` is able to afford the storage deposit limit. + /// + /// It is necessary to do this check beforehand so that the charge won't fail later on. + /// + /// `origin`: The origin of the call stack from which is responsible for putting down a deposit. + /// `limit`: The limit with which the meter was constructed. + /// `min_leftover`: How much `free_balance` in addition to the existential deposit (ed) should + /// be left inside the `origin` account. + /// + /// Returns the limit that should be used by the meter. If origin can't afford the `limit` + /// it returns `Err`. + fn check_limit( + origin: &T::AccountId, + limit: Option>, + min_leftover: BalanceOf, + ) -> Result, DispatchError>; + /// This is called to inform the implementer that some balance should be charged due to + /// some interaction of the `origin` with a `contract`. + /// + /// The balance transfer can either flow from `origin` to `contract` or the other way + /// around depending on whether `amount` constitutes a `Charge` or a `Refund`. + /// It should be used in combination with `check_limit` to check that no more balance than this + /// limit is ever charged. + fn charge( + origin: &T::AccountId, + contract: &T::AccountId, + amount: &DepositOf, + state: &ContractState, + ) -> Result<(), DispatchError>; +} + +/// This [`Ext`] is used for actual on-chain execution when balance needs to be charged. +/// +/// It uses [`frame_support::traits::fungible::Mutate`] in order to do accomplish the reserves. +pub enum ReservingExt {} + +/// Used to implement a type state pattern for the meter. +/// +/// It is sealed and cannot be implemented outside of this module. +pub trait State: private::Sealed {} + +/// State parameter that constitutes a meter that is in its root state. +#[derive(Default, Debug)] +pub struct Root; + +/// State parameter that constitutes a meter that is in its nested state. +/// Its value indicates whether the nested meter has its own limit. +#[derive(DefaultNoBound, RuntimeDebugNoBound)] +pub enum Nested { + #[default] + DerivedLimit, + OwnLimit, +} + +impl State for Root {} +impl State for Nested {} + +/// A type that allows the metering of consumed or freed storage of a single contract call stack. +#[derive(DefaultNoBound, RuntimeDebugNoBound)] +pub struct RawMeter { + /// The limit of how much balance this meter is allowed to consume. + limit: BalanceOf, + /// The amount of balance that was used in this meter and all of its already absorbed children. + total_deposit: DepositOf, + /// The amount of storage changes that were recorded in this meter alone. + own_contribution: Contribution, + /// List of charges that should be applied at the end of a contract stack execution. + /// + /// We only have one charge per contract hence the size of this vector is + /// limited by the maximum call depth. + charges: Vec>, + /// We store the nested state to determine if it has a special limit for sub-call. + nested: S, + /// Type parameter only used in impls. + _phantom: PhantomData, +} + +/// This type is used to describe a storage change when charging from the meter. +#[derive(Default, RuntimeDebugNoBound)] +pub struct Diff { + /// How many bytes were added to storage. + pub bytes_added: u32, + /// How many bytes were removed from storage. + pub bytes_removed: u32, + /// How many storage items were added to storage. + pub items_added: u32, + /// How many storage items were removed from storage. + pub items_removed: u32, +} + +impl Diff { + /// Calculate how much of a charge or refund results from applying the diff and store it + /// in the passed `info` if any. + /// + /// # Note + /// + /// In case `None` is passed for `info` only charges are calculated. This is because refunds + /// are calculated pro rata of the existing storage within a contract and hence need extract + /// this information from the passed `info`. + pub fn update_contract(&self, info: Option<&mut ContractInfo>) -> DepositOf { + let per_byte = T::DepositPerByte::get(); + let per_item = T::DepositPerItem::get(); + let bytes_added = self.bytes_added.saturating_sub(self.bytes_removed); + let items_added = self.items_added.saturating_sub(self.items_removed); + let mut bytes_deposit = Deposit::Charge(per_byte.saturating_mul((bytes_added).into())); + let mut items_deposit = Deposit::Charge(per_item.saturating_mul((items_added).into())); + + // Without any contract info we can only calculate diffs which add storage + let info = if let Some(info) = info { + info + } else { + debug_assert_eq!(self.bytes_removed, 0); + debug_assert_eq!(self.items_removed, 0); + return bytes_deposit.saturating_add(&items_deposit) + }; + + // Refunds are calculated pro rata based on the accumulated storage within the contract + let bytes_removed = self.bytes_removed.saturating_sub(self.bytes_added); + let items_removed = self.items_removed.saturating_sub(self.items_added); + let ratio = FixedU128::checked_from_rational(bytes_removed, info.storage_bytes) + .unwrap_or_default() + .min(FixedU128::from_u32(1)); + bytes_deposit = bytes_deposit + .saturating_add(&Deposit::Refund(ratio.saturating_mul_int(info.storage_byte_deposit))); + let ratio = FixedU128::checked_from_rational(items_removed, info.storage_items) + .unwrap_or_default() + .min(FixedU128::from_u32(1)); + items_deposit = items_deposit + .saturating_add(&Deposit::Refund(ratio.saturating_mul_int(info.storage_item_deposit))); + + // We need to update the contract info structure with the new deposits + info.storage_bytes = + info.storage_bytes.saturating_add(bytes_added).saturating_sub(bytes_removed); + info.storage_items = + info.storage_items.saturating_add(items_added).saturating_sub(items_removed); + match &bytes_deposit { + Deposit::Charge(amount) => + info.storage_byte_deposit = info.storage_byte_deposit.saturating_add(*amount), + Deposit::Refund(amount) => + info.storage_byte_deposit = info.storage_byte_deposit.saturating_sub(*amount), + } + match &items_deposit { + Deposit::Charge(amount) => + info.storage_item_deposit = info.storage_item_deposit.saturating_add(*amount), + Deposit::Refund(amount) => + info.storage_item_deposit = info.storage_item_deposit.saturating_sub(*amount), + } + + bytes_deposit.saturating_add(&items_deposit) + } +} + +impl Diff { + fn saturating_add(&self, rhs: &Self) -> Self { + Self { + bytes_added: self.bytes_added.saturating_add(rhs.bytes_added), + bytes_removed: self.bytes_removed.saturating_add(rhs.bytes_removed), + items_added: self.items_added.saturating_add(rhs.items_added), + items_removed: self.items_removed.saturating_add(rhs.items_removed), + } + } +} + +/// The state of a contract. +/// +/// In case of termination the beneficiary is indicated. +#[derive(RuntimeDebugNoBound, Clone, PartialEq, Eq)] +pub enum ContractState { + Alive, + Terminated { beneficiary: AccountIdOf }, +} + +/// Records information to charge or refund a plain account. +/// +/// All the charges are deferred to the end of a whole call stack. Reason is that by doing +/// this we can do all the refunds before doing any charge. This way a plain account can use +/// more deposit than it has balance as along as it is covered by a refund. This +/// essentially makes the order of storage changes irrelevant with regard to the deposit system. +/// The only exception is when a special (tougher) deposit limit is specified for a cross-contract +/// call. In that case the limit is enforced once the call is returned, rolling it back if +/// exhausted. +#[derive(RuntimeDebugNoBound, Clone)] +struct Charge { + contract: T::AccountId, + amount: DepositOf, + state: ContractState, +} + +/// Records the storage changes of a storage meter. +#[derive(RuntimeDebugNoBound)] +enum Contribution { + /// The contract the meter belongs to is alive and accumulates changes using a [`Diff`]. + Alive(Diff), + /// The meter was checked against its limit using [`RawMeter::enforce_limit`] at the end of + /// its execution. In this process the [`Diff`] was converted into a [`Deposit`]. + Checked(DepositOf), + /// The contract was terminated. In this process the [`Diff`] was converted into a [`Deposit`] + /// in order to calculate the refund. Upon termination the `reducible_balance` in the + /// contract's account is transferred to the [`beneficiary`]. + Terminated { deposit: DepositOf, beneficiary: AccountIdOf }, +} + +impl Contribution { + /// See [`Diff::update_contract`]. + fn update_contract(&self, info: Option<&mut ContractInfo>) -> DepositOf { + match self { + Self::Alive(diff) => diff.update_contract::(info), + Self::Terminated { deposit, beneficiary: _ } | Self::Checked(deposit) => + deposit.clone(), + } + } +} + +impl Default for Contribution { + fn default() -> Self { + Self::Alive(Default::default()) + } +} + +/// Functions that apply to all states. +impl RawMeter +where + T: Config, + E: Ext, + S: State + Default + Debug, +{ + /// Create a new child that has its `limit`. + /// Passing `0` as the limit is interpreted as to take whatever is remaining from its parent. + /// + /// This is called whenever a new subcall is initiated in order to track the storage + /// usage for this sub call separately. This is necessary because we want to exchange balance + /// with the current contract we are interacting with. + pub fn nested(&self, limit: BalanceOf) -> RawMeter { + debug_assert!(matches!(self.contract_state(), ContractState::Alive)); + // If a special limit is specified higher than it is available, + // we want to enforce the lesser limit to the nested meter, to fail in the sub-call. + let limit = self.available().min(limit); + if limit.is_zero() { + RawMeter { limit: self.available(), ..Default::default() } + } else { + RawMeter { limit, nested: Nested::OwnLimit, ..Default::default() } + } + } + + /// Absorb a child that was spawned to handle a sub call. + /// + /// This should be called whenever a sub call comes to its end and it is **not** reverted. + /// This does the actual balance transfer from/to `origin` and `contract` based on the + /// overall storage consumption of the call. It also updates the supplied contract info. + /// + /// In case a contract reverted the child meter should just be dropped in order to revert + /// any changes it recorded. + /// + /// # Parameters + /// + /// - `absorbed`: The child storage meter that should be absorbed. + /// - `origin`: The origin that spawned the original root meter. + /// - `contract`: The contract's account that this sub call belongs to. + /// - `info`: The info of the contract in question. `None` if the contract was terminated. + pub fn absorb( + &mut self, + absorbed: RawMeter, + contract: &T::AccountId, + info: Option<&mut ContractInfo>, + ) { + let own_deposit = absorbed.own_contribution.update_contract(info); + self.total_deposit = self + .total_deposit + .saturating_add(&absorbed.total_deposit) + .saturating_add(&own_deposit); + self.charges.extend_from_slice(&absorbed.charges); + if !own_deposit.is_zero() { + self.charges.push(Charge { + contract: contract.clone(), + amount: own_deposit, + state: absorbed.contract_state(), + }); + } + } + + /// The amount of balance that is still available from the original `limit`. + fn available(&self) -> BalanceOf { + self.total_deposit.available(&self.limit) + } + + /// Returns the state of the currently executed contract. + fn contract_state(&self) -> ContractState { + match &self.own_contribution { + Contribution::Terminated { deposit: _, beneficiary } => + ContractState::Terminated { beneficiary: beneficiary.clone() }, + _ => ContractState::Alive, + } + } +} + +/// Functions that only apply to the root state. +impl RawMeter +where + T: Config, + E: Ext, +{ + /// Create new storage meter for the specified `origin` and `limit`. + /// + /// This tries to [`Ext::check_limit`] on `origin` and fails if this is not possible. + pub fn new( + origin: &Origin, + limit: Option>, + min_leftover: BalanceOf, + ) -> Result { + // Check the limit only if the origin is not root. + return match origin { + Origin::Root => Ok(Self { + limit: limit.unwrap_or(T::DefaultDepositLimit::get()), + ..Default::default() + }), + Origin::Signed(o) => { + let limit = E::check_limit(o, limit, min_leftover)?; + Ok(Self { limit, ..Default::default() }) + }, + } + } + + /// The total amount of deposit that should change hands as result of the execution + /// that this meter was passed into. This will also perform all the charges accumulated + /// in the whole contract stack. + /// + /// This drops the root meter in order to make sure it is only called when the whole + /// execution did finish. + pub fn try_into_deposit(self, origin: &Origin) -> Result, DispatchError> { + // Only refund or charge deposit if the origin is not root. + let origin = match origin { + Origin::Root => return Ok(Deposit::Charge(Zero::zero())), + Origin::Signed(o) => o, + }; + for charge in self.charges.iter().filter(|c| matches!(c.amount, Deposit::Refund(_))) { + E::charge(origin, &charge.contract, &charge.amount, &charge.state)?; + } + for charge in self.charges.iter().filter(|c| matches!(c.amount, Deposit::Charge(_))) { + E::charge(origin, &charge.contract, &charge.amount, &charge.state)?; + } + Ok(self.total_deposit) + } +} + +/// Functions that only apply to the nested state. +impl RawMeter +where + T: Config, + E: Ext, +{ + /// Charges `diff` from the meter. + pub fn charge(&mut self, diff: &Diff) { + match &mut self.own_contribution { + Contribution::Alive(own) => *own = own.saturating_add(diff), + _ => panic!("Charge is never called after termination; qed"), + }; + } + + /// Adds a deposit charge. + /// + /// Use this method instead of [`Self::charge`] when the charge is not the result of a storage + /// change. This is the case when a `delegate_dependency` is added or removed, or when the + /// `code_hash` is updated. [`Self::charge`] cannot be used here because we keep track of the + /// deposit charge separately from the storage charge. + pub fn charge_deposit(&mut self, contract: T::AccountId, amount: DepositOf) { + self.total_deposit = self.total_deposit.saturating_add(&amount); + self.charges.push(Charge { contract, amount, state: ContractState::Alive }); + } + + /// Charges from `origin` a storage deposit for contract instantiation. + /// + /// This immediately transfers the balance in order to create the account. + pub fn charge_instantiate( + &mut self, + origin: &T::AccountId, + contract: &T::AccountId, + contract_info: &mut ContractInfo, + code_info: &CodeInfo, + ) -> Result<(), DispatchError> { + debug_assert!(matches!(self.contract_state(), ContractState::Alive)); + + // We need to make sure that the contract's account exists. + let ed = Pallet::::min_balance(); + self.total_deposit = Deposit::Charge(ed); + T::Currency::transfer(origin, contract, ed, Preservation::Preserve)?; + + // A consumer is added at account creation and removed it on termination, otherwise the + // runtime could remove the account. As long as a contract exists its account must exist. + // With the consumer, a correct runtime cannot remove the account. + System::::inc_consumers(contract)?; + + let deposit = contract_info.update_base_deposit(&code_info); + let deposit = Deposit::Charge(deposit); + + self.charge_deposit(contract.clone(), deposit); + Ok(()) + } + + /// Call to tell the meter that the currently executing contract was terminated. + /// + /// This will manipulate the meter so that all storage deposit accumulated in + /// `contract_info` will be refunded to the `origin` of the meter. And the free + /// (`reducible_balance`) will be sent to the `beneficiary`. + pub fn terminate(&mut self, info: &ContractInfo, beneficiary: T::AccountId) { + debug_assert!(matches!(self.contract_state(), ContractState::Alive)); + self.own_contribution = Contribution::Terminated { + deposit: Deposit::Refund(info.total_deposit()), + beneficiary, + }; + } + + /// [`Self::charge`] does not enforce the storage limit since we want to do this check as late + /// as possible to allow later refunds to offset earlier charges. + /// + /// # Note + /// + /// We normally need to call this **once** for every call stack and not for every cross contract + /// call. However, if a dedicated limit is specified for a sub-call, this needs to be called + /// once the sub-call has returned. For this, the [`Self::enforce_subcall_limit`] wrapper is + /// used. + pub fn enforce_limit( + &mut self, + info: Option<&mut ContractInfo>, + ) -> Result<(), DispatchError> { + let deposit = self.own_contribution.update_contract(info); + let total_deposit = self.total_deposit.saturating_add(&deposit); + // We don't want to override a `Terminated` with a `Checked`. + if matches!(self.contract_state(), ContractState::Alive) { + self.own_contribution = Contribution::Checked(deposit); + } + if let Deposit::Charge(amount) = total_deposit { + if amount > self.limit { + return Err(>::StorageDepositLimitExhausted.into()) + } + } + Ok(()) + } + + /// This is a wrapper around [`Self::enforce_limit`] to use on the exit from a sub-call to + /// enforce its special limit if needed. + pub fn enforce_subcall_limit( + &mut self, + info: Option<&mut ContractInfo>, + ) -> Result<(), DispatchError> { + match self.nested { + Nested::OwnLimit => self.enforce_limit(info), + Nested::DerivedLimit => Ok(()), + } + } +} + +impl Ext for ReservingExt { + fn check_limit( + origin: &T::AccountId, + limit: Option>, + min_leftover: BalanceOf, + ) -> Result, DispatchError> { + // We are sending the `min_leftover` and the `min_balance` from the origin + // account as part of a contract call. Hence origin needs to have those left over + // as free balance after accounting for all deposits. + let max = T::Currency::reducible_balance(origin, Preservation::Preserve, Polite) + .saturating_sub(min_leftover) + .saturating_sub(Pallet::::min_balance()); + let default = max.min(T::DefaultDepositLimit::get()); + let limit = limit.unwrap_or(default); + ensure!( + limit <= max && + matches!(T::Currency::can_withdraw(origin, limit), WithdrawConsequence::Success), + >::StorageDepositNotEnoughFunds, + ); + Ok(limit) + } + + fn charge( + origin: &T::AccountId, + contract: &T::AccountId, + amount: &DepositOf, + state: &ContractState, + ) -> Result<(), DispatchError> { + match amount { + Deposit::Charge(amount) | Deposit::Refund(amount) if amount.is_zero() => return Ok(()), + Deposit::Charge(amount) => { + // This could fail if the `origin` does not have enough liquidity. Ideally, though, + // this should have been checked before with `check_limit`. + T::Currency::transfer_and_hold( + &HoldReason::StorageDepositReserve.into(), + origin, + contract, + *amount, + Precision::Exact, + Preservation::Preserve, + Fortitude::Polite, + )?; + + Pallet::::deposit_event(Event::StorageDepositTransferredAndHeld { + from: origin.clone(), + to: contract.clone(), + amount: *amount, + }); + }, + Deposit::Refund(amount) => { + let transferred = T::Currency::transfer_on_hold( + &HoldReason::StorageDepositReserve.into(), + contract, + origin, + *amount, + Precision::BestEffort, + Restriction::Free, + Fortitude::Polite, + )?; + + Pallet::::deposit_event(Event::StorageDepositTransferredAndReleased { + from: contract.clone(), + to: origin.clone(), + amount: transferred, + }); + + if transferred < *amount { + // This should never happen, if it does it means that there is a bug in the + // runtime logic. In the rare case this happens we try to refund as much as we + // can, thus the `Precision::BestEffort`. + log::error!( + target: LOG_TARGET, + "Failed to repatriate full storage deposit {:?} from contract {:?} to origin {:?}. Transferred {:?}.", + amount, contract, origin, transferred, + ); + } + }, + } + if let ContractState::::Terminated { beneficiary } = state { + System::::dec_consumers(&contract); + // Whatever is left in the contract is sent to the termination beneficiary. + T::Currency::transfer( + &contract, + &beneficiary, + T::Currency::reducible_balance(&contract, Preservation::Expendable, Polite), + Preservation::Expendable, + )?; + } + Ok(()) + } +} + +mod private { + pub trait Sealed {} + impl Sealed for super::Root {} + impl Sealed for super::Nested {} +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + exec::AccountIdOf, + tests::{Test, ALICE, BOB, CHARLIE}, + }; + use frame_support::parameter_types; + use pretty_assertions::assert_eq; + + type TestMeter = RawMeter; + + parameter_types! { + static TestExtTestValue: TestExt = Default::default(); + } + + #[derive(Debug, PartialEq, Eq, Clone)] + struct LimitCheck { + origin: AccountIdOf, + limit: BalanceOf, + min_leftover: BalanceOf, + } + + #[derive(Debug, PartialEq, Eq, Clone)] + struct Charge { + origin: AccountIdOf, + contract: AccountIdOf, + amount: DepositOf, + state: ContractState, + } + + #[derive(Default, Debug, PartialEq, Eq, Clone)] + pub struct TestExt { + limit_checks: Vec, + charges: Vec, + } + + impl TestExt { + fn clear(&mut self) { + self.limit_checks.clear(); + self.charges.clear(); + } + } + + impl Ext for TestExt { + fn check_limit( + origin: &AccountIdOf, + limit: Option>, + min_leftover: BalanceOf, + ) -> Result, DispatchError> { + let limit = limit.unwrap_or(42); + TestExtTestValue::mutate(|ext| { + ext.limit_checks + .push(LimitCheck { origin: origin.clone(), limit, min_leftover }) + }); + Ok(limit) + } + + fn charge( + origin: &AccountIdOf, + contract: &AccountIdOf, + amount: &DepositOf, + state: &ContractState, + ) -> Result<(), DispatchError> { + TestExtTestValue::mutate(|ext| { + ext.charges.push(Charge { + origin: origin.clone(), + contract: contract.clone(), + amount: amount.clone(), + state: state.clone(), + }) + }); + Ok(()) + } + } + + fn clear_ext() { + TestExtTestValue::mutate(|ext| ext.clear()) + } + + struct ChargingTestCase { + origin: Origin, + deposit: DepositOf, + expected: TestExt, + } + + #[derive(Default)] + struct StorageInfo { + bytes: u32, + items: u32, + bytes_deposit: BalanceOf, + items_deposit: BalanceOf, + } + + fn new_info(info: StorageInfo) -> ContractInfo { + ContractInfo:: { + trie_id: Default::default(), + code_hash: Default::default(), + storage_bytes: info.bytes, + storage_items: info.items, + storage_byte_deposit: info.bytes_deposit, + storage_item_deposit: info.items_deposit, + storage_base_deposit: Default::default(), + delegate_dependencies: Default::default(), + } + } + + #[test] + fn new_reserves_balance_works() { + clear_ext(); + + TestMeter::new(&Origin::from_account_id(ALICE), Some(1_000), 0).unwrap(); + + assert_eq!( + TestExtTestValue::get(), + TestExt { + limit_checks: vec![LimitCheck { origin: ALICE, limit: 1_000, min_leftover: 0 }], + ..Default::default() + } + ) + } + + #[test] + fn empty_charge_works() { + clear_ext(); + + let mut meter = TestMeter::new(&Origin::from_account_id(ALICE), Some(1_000), 0).unwrap(); + assert_eq!(meter.available(), 1_000); + + // an empty charge does not create a `Charge` entry + let mut nested0 = meter.nested(BalanceOf::::zero()); + nested0.charge(&Default::default()); + meter.absorb(nested0, &BOB, None); + + assert_eq!( + TestExtTestValue::get(), + TestExt { + limit_checks: vec![LimitCheck { origin: ALICE, limit: 1_000, min_leftover: 0 }], + ..Default::default() + } + ) + } + + #[test] + fn charging_works() { + let test_cases = vec![ + ChargingTestCase { + origin: Origin::::from_account_id(ALICE), + deposit: Deposit::Refund(28), + expected: TestExt { + limit_checks: vec![LimitCheck { origin: ALICE, limit: 100, min_leftover: 0 }], + charges: vec![ + Charge { + origin: ALICE, + contract: CHARLIE, + amount: Deposit::Refund(10), + state: ContractState::Alive, + }, + Charge { + origin: ALICE, + contract: CHARLIE, + amount: Deposit::Refund(20), + state: ContractState::Alive, + }, + Charge { + origin: ALICE, + contract: BOB, + amount: Deposit::Charge(2), + state: ContractState::Alive, + }, + ], + }, + }, + ChargingTestCase { + origin: Origin::::Root, + deposit: Deposit::Charge(0), + expected: TestExt { limit_checks: vec![], charges: vec![] }, + }, + ]; + + for test_case in test_cases { + clear_ext(); + + let mut meter = TestMeter::new(&test_case.origin, Some(100), 0).unwrap(); + assert_eq!(meter.available(), 100); + + let mut nested0_info = new_info(StorageInfo { + bytes: 100, + items: 5, + bytes_deposit: 100, + items_deposit: 10, + }); + let mut nested0 = meter.nested(BalanceOf::::zero()); + nested0.charge(&Diff { + bytes_added: 108, + bytes_removed: 5, + items_added: 1, + items_removed: 2, + }); + nested0.charge(&Diff { bytes_removed: 99, ..Default::default() }); + + let mut nested1_info = new_info(StorageInfo { + bytes: 100, + items: 10, + bytes_deposit: 100, + items_deposit: 20, + }); + let mut nested1 = nested0.nested(BalanceOf::::zero()); + nested1.charge(&Diff { items_removed: 5, ..Default::default() }); + nested0.absorb(nested1, &CHARLIE, Some(&mut nested1_info)); + + let mut nested2_info = new_info(StorageInfo { + bytes: 100, + items: 7, + bytes_deposit: 100, + items_deposit: 20, + }); + let mut nested2 = nested0.nested(BalanceOf::::zero()); + nested2.charge(&Diff { items_removed: 7, ..Default::default() }); + nested0.absorb(nested2, &CHARLIE, Some(&mut nested2_info)); + + nested0.enforce_limit(Some(&mut nested0_info)).unwrap(); + meter.absorb(nested0, &BOB, Some(&mut nested0_info)); + + assert_eq!(meter.try_into_deposit(&test_case.origin).unwrap(), test_case.deposit); + + assert_eq!(nested0_info.extra_deposit(), 112); + assert_eq!(nested1_info.extra_deposit(), 110); + assert_eq!(nested2_info.extra_deposit(), 100); + + assert_eq!(TestExtTestValue::get(), test_case.expected) + } + } + + #[test] + fn termination_works() { + let test_cases = vec![ + ChargingTestCase { + origin: Origin::::from_account_id(ALICE), + deposit: Deposit::Refund(108), + expected: TestExt { + limit_checks: vec![LimitCheck { origin: ALICE, limit: 1_000, min_leftover: 0 }], + charges: vec![ + Charge { + origin: ALICE, + contract: CHARLIE, + amount: Deposit::Refund(120), + state: ContractState::Terminated { beneficiary: CHARLIE }, + }, + Charge { + origin: ALICE, + contract: BOB, + amount: Deposit::Charge(12), + state: ContractState::Alive, + }, + ], + }, + }, + ChargingTestCase { + origin: Origin::::Root, + deposit: Deposit::Charge(0), + expected: TestExt { limit_checks: vec![], charges: vec![] }, + }, + ]; + + for test_case in test_cases { + clear_ext(); + + let mut meter = TestMeter::new(&test_case.origin, Some(1_000), 0).unwrap(); + assert_eq!(meter.available(), 1_000); + + let mut nested0 = meter.nested(BalanceOf::::zero()); + nested0.charge(&Diff { + bytes_added: 5, + bytes_removed: 1, + items_added: 3, + items_removed: 1, + }); + nested0.charge(&Diff { items_added: 2, ..Default::default() }); + + let mut nested1_info = new_info(StorageInfo { + bytes: 100, + items: 10, + bytes_deposit: 100, + items_deposit: 20, + }); + let mut nested1 = nested0.nested(BalanceOf::::zero()); + nested1.charge(&Diff { items_removed: 5, ..Default::default() }); + nested1.charge(&Diff { bytes_added: 20, ..Default::default() }); + nested1.terminate(&nested1_info, CHARLIE); + nested0.enforce_limit(Some(&mut nested1_info)).unwrap(); + nested0.absorb(nested1, &CHARLIE, None); + + meter.absorb(nested0, &BOB, None); + assert_eq!(meter.try_into_deposit(&test_case.origin).unwrap(), test_case.deposit); + assert_eq!(TestExtTestValue::get(), test_case.expected) + } + } +} diff --git a/pallets/contracts/src/test_utils.rs b/pallets/contracts/src/test_utils.rs new file mode 100644 index 00000000..564b2d2e --- /dev/null +++ b/pallets/contracts/src/test_utils.rs @@ -0,0 +1,30 @@ +// This file is part of Substrate. + +// 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. + +//! Shared utilities for testing contracts. +//! This is not part of the tests module because it is made public for other crates to use. +#![cfg(feature = "std")] +use frame_support::weights::Weight; +pub use sp_runtime::AccountId32; + +pub const ALICE: AccountId32 = AccountId32::new([1u8; 32]); +pub const BOB: AccountId32 = AccountId32::new([2u8; 32]); +pub const CHARLIE: AccountId32 = AccountId32::new([3u8; 32]); +pub const DJANGO: AccountId32 = AccountId32::new([4u8; 32]); + +pub const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024); +pub mod builder; diff --git a/pallets/contracts/src/test_utils/builder.rs b/pallets/contracts/src/test_utils/builder.rs new file mode 100644 index 00000000..94540eca --- /dev/null +++ b/pallets/contracts/src/test_utils/builder.rs @@ -0,0 +1,220 @@ +// This file is part of Substrate. + +// 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 super::GAS_LIMIT; +use crate::{ + AccountIdLookupOf, AccountIdOf, BalanceOf, Code, CodeHash, CollectEvents, Config, + ContractExecResult, ContractInstantiateResult, DebugInfo, Determinism, EventRecordOf, + ExecReturnValue, InstantiateReturnValue, OriginFor, Pallet, Weight, +}; +use codec::{Encode, HasCompact}; +use core::fmt::Debug; +use frame_support::pallet_prelude::DispatchResultWithPostInfo; +use paste::paste; +use scale_info::TypeInfo; + +/// Helper macro to generate a builder for contract API calls. +macro_rules! builder { + // Entry point to generate a builder for the given method. + ( + $method:ident($($field:ident: $type:ty,)*) -> $result:ty; + $($extra:item)* + ) => { + paste!{ + builder!([< $method:camel Builder >], $method($($field: $type,)* ) -> $result; $($extra)*); + } + }; + // Generate the builder struct and its methods. + ( + $name:ident, + $method:ident($($field:ident: $type:ty,)*) -> $result:ty; + $($extra:item)* + ) => { + #[doc = concat!("A builder to construct a ", stringify!($method), " call")] + pub struct $name { + $($field: $type,)* + } + + #[allow(dead_code)] + impl $name + where + as HasCompact>::Type: Clone + Eq + PartialEq + Debug + TypeInfo + Encode, + { + $( + #[doc = concat!("Set the ", stringify!($field))] + pub fn $field(mut self, value: $type) -> Self { + self.$field = value; + self + } + )* + + #[doc = concat!("Build the ", stringify!($method), " call")] + pub fn build(self) -> $result { + Pallet::::$method( + $(self.$field,)* + ) + } + + $($extra)* + } + } +} + +builder!( + instantiate_with_code( + origin: OriginFor, + value: BalanceOf, + gas_limit: Weight, + storage_deposit_limit: Option< as codec::HasCompact>::Type>, + code: Vec, + data: Vec, + salt: Vec, + ) -> DispatchResultWithPostInfo; + + /// Create an [`InstantiateWithCodeBuilder`] with default values. + pub fn instantiate_with_code(origin: OriginFor, code: Vec) -> Self { + Self { + origin: origin, + value: 0u32.into(), + gas_limit: GAS_LIMIT, + storage_deposit_limit: None, + code, + data: vec![], + salt: vec![], + } + } +); + +builder!( + instantiate( + origin: OriginFor, + value: BalanceOf, + gas_limit: Weight, + storage_deposit_limit: Option< as codec::HasCompact>::Type>, + code_hash: CodeHash, + data: Vec, + salt: Vec, + ) -> DispatchResultWithPostInfo; + + /// Create an [`InstantiateBuilder`] with default values. + pub fn instantiate(origin: OriginFor, code_hash: CodeHash) -> Self { + Self { + origin, + value: 0u32.into(), + gas_limit: GAS_LIMIT, + storage_deposit_limit: None, + code_hash, + data: vec![], + salt: vec![], + } + } +); + +builder!( + bare_instantiate( + origin: AccountIdOf, + value: BalanceOf, + gas_limit: Weight, + storage_deposit_limit: Option>, + code: Code>, + data: Vec, + salt: Vec, + debug: DebugInfo, + collect_events: CollectEvents, + ) -> ContractInstantiateResult, BalanceOf, EventRecordOf>; + + /// Build the instantiate call and unwrap the result. + pub fn build_and_unwrap_result(self) -> InstantiateReturnValue> { + self.build().result.unwrap() + } + + /// Build the instantiate call and unwrap the account id. + pub fn build_and_unwrap_account_id(self) -> AccountIdOf { + self.build().result.unwrap().account_id + } + + pub fn bare_instantiate(origin: AccountIdOf, code: Code>) -> Self { + Self { + origin, + value: 0u32.into(), + gas_limit: GAS_LIMIT, + storage_deposit_limit: None, + code, + data: vec![], + salt: vec![], + debug: DebugInfo::Skip, + collect_events: CollectEvents::Skip, + } + } +); + +builder!( + call( + origin: OriginFor, + dest: AccountIdLookupOf, + value: BalanceOf, + gas_limit: Weight, + storage_deposit_limit: Option< as codec::HasCompact>::Type>, + data: Vec, + ) -> DispatchResultWithPostInfo; + + /// Create a [`CallBuilder`] with default values. + pub fn call(origin: OriginFor, dest: AccountIdLookupOf) -> Self { + CallBuilder { + origin, + dest, + value: 0u32.into(), + gas_limit: GAS_LIMIT, + storage_deposit_limit: None, + data: vec![], + } + } +); + +builder!( + bare_call( + origin: AccountIdOf, + dest: AccountIdOf, + value: BalanceOf, + gas_limit: Weight, + storage_deposit_limit: Option>, + data: Vec, + debug: DebugInfo, + collect_events: CollectEvents, + determinism: Determinism, + ) -> ContractExecResult, EventRecordOf>; + + /// Build the call and unwrap the result. + pub fn build_and_unwrap_result(self) -> ExecReturnValue { + self.build().result.unwrap() + } + + /// Create a [`BareCallBuilder`] with default values. + pub fn bare_call(origin: AccountIdOf, dest: AccountIdOf) -> Self { + Self { + origin, + dest, + value: 0u32.into(), + gas_limit: GAS_LIMIT, + storage_deposit_limit: None, + data: vec![], + debug: DebugInfo::Skip, + collect_events: CollectEvents::Skip, + determinism: Determinism::Enforced, + } + } +); diff --git a/pallets/contracts/src/tests.rs b/pallets/contracts/src/tests.rs new file mode 100644 index 00000000..c3b6e327 --- /dev/null +++ b/pallets/contracts/src/tests.rs @@ -0,0 +1,4396 @@ +// This file is part of Substrate. + +// 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. + +mod pallet_dummy; +mod test_debug; + +use self::{ + test_debug::TestDebug, + test_utils::{ensure_stored, expected_deposit}, +}; +use crate::{ + self as pallet_contracts, + chain_extension::{ + ChainExtension, Environment, Ext, InitState, RegisteredChainExtension, + Result as ExtensionResult, RetVal, ReturnFlags, + }, + exec::{Frame, Key}, + migration::codegen::LATEST_MIGRATION_VERSION, + primitives::CodeUploadReturnValue, + storage::DeletionQueueManager, + tests::test_utils::{get_contract, get_contract_checked}, + wasm::{Determinism, LoadingMode, ReturnErrorCode as RuntimeReturnCode}, + weights::WeightInfo, + Array, BalanceOf, Code, CodeHash, CodeInfoOf, CollectEvents, Config, ContractInfo, + ContractInfoOf, DebugInfo, DefaultAddressGenerator, DeletionQueueCounter, Error, HoldReason, + MigrationInProgress, Origin, Pallet, PristineCode, Schedule, +}; +use assert_matches::assert_matches; +use codec::{Decode, Encode}; +use frame_support::{ + assert_err, assert_err_ignore_postinfo, assert_err_with_weight, assert_noop, assert_ok, + derive_impl, + dispatch::{DispatchErrorWithPostInfo, PostDispatchInfo}, + pallet_prelude::EnsureOrigin, + parameter_types, + storage::child, + traits::{ + fungible::{BalancedHold, Inspect, Mutate, MutateHold}, + tokens::Preservation, + ConstU32, ConstU64, Contains, OnIdle, OnInitialize, StorageVersion, + }, + weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight, WeightMeter}, +}; +use frame_system::{EventRecord, Phase}; +use pallet_contracts_fixtures::compile_module; +use pretty_assertions::{assert_eq, assert_ne}; +use sp_core::ByteArray; +use sp_io::hashing::blake2_256; +use sp_keystore::{testing::MemoryKeystore, KeystoreExt}; +use sp_runtime::{ + testing::H256, + traits::{BlakeTwo256, Convert, IdentityLookup}, + AccountId32, BuildStorage, DispatchError, Perbill, TokenError, +}; + +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test + { + System: frame_system, + Balances: pallet_balances, + Timestamp: pallet_timestamp, + Randomness: pallet_insecure_randomness_collective_flip, + Utility: pallet_utility, + Contracts: pallet_contracts, + Proxy: pallet_proxy, + Dummy: pallet_dummy + } +); + +macro_rules! assert_return_code { + ( $x:expr , $y:expr $(,)? ) => {{ + assert_eq!(u32::from_le_bytes($x.data[..].try_into().unwrap()), $y as u32); + }}; +} + +macro_rules! assert_refcount { + ( $code_hash:expr , $should:expr $(,)? ) => {{ + let is = crate::CodeInfoOf::::get($code_hash).map(|m| m.refcount()).unwrap(); + assert_eq!(is, $should); + }}; +} + +pub mod test_utils { + use super::{Contracts, DepositPerByte, DepositPerItem, Test}; + use crate::{ + exec::AccountIdOf, BalanceOf, CodeHash, CodeInfo, CodeInfoOf, Config, ContractInfo, + ContractInfoOf, Nonce, PristineCode, + }; + use codec::{Encode, MaxEncodedLen}; + use frame_support::traits::fungible::{InspectHold, Mutate}; + + pub fn place_contract(address: &AccountIdOf, code_hash: CodeHash) { + let nonce = >::mutate(|counter| { + *counter += 1; + *counter + }); + set_balance(address, Contracts::min_balance() * 10); + >::insert(code_hash, CodeInfo::new(address.clone())); + let contract = >::new(&address, nonce, code_hash).unwrap(); + >::insert(address, contract); + } + pub fn set_balance(who: &AccountIdOf, amount: u64) { + let _ = ::Currency::set_balance(who, amount); + } + pub fn get_balance(who: &AccountIdOf) -> u64 { + ::Currency::free_balance(who) + } + pub fn get_balance_on_hold( + reason: &::RuntimeHoldReason, + who: &AccountIdOf, + ) -> u64 { + ::Currency::balance_on_hold(reason.into(), who) + } + pub fn get_contract(addr: &AccountIdOf) -> ContractInfo { + get_contract_checked(addr).unwrap() + } + pub fn get_contract_checked(addr: &AccountIdOf) -> Option> { + ContractInfoOf::::get(addr) + } + pub fn get_code_deposit(code_hash: &CodeHash) -> BalanceOf { + crate::CodeInfoOf::::get(code_hash).unwrap().deposit() + } + pub fn contract_info_storage_deposit( + addr: &::AccountId, + ) -> BalanceOf { + let contract_info = self::get_contract(&addr); + let info_size = contract_info.encoded_size() as u64; + DepositPerByte::get() + .saturating_mul(info_size) + .saturating_add(DepositPerItem::get()) + } + pub fn expected_deposit(code_len: usize) -> u64 { + // For code_info, the deposit for max_encoded_len is taken. + let code_info_len = CodeInfo::::max_encoded_len() as u64; + // Calculate deposit to be reserved. + // We add 2 storage items: one for code, other for code_info + DepositPerByte::get().saturating_mul(code_len as u64 + code_info_len) + + DepositPerItem::get().saturating_mul(2) + } + pub fn ensure_stored(code_hash: CodeHash) -> usize { + // Assert that code_info is stored + assert!(CodeInfoOf::::contains_key(&code_hash)); + // Assert that contract code is stored, and get its size. + PristineCode::::try_get(&code_hash).unwrap().len() + } +} + +mod builder { + use super::Test; + use crate::{ + test_utils::{builder::*, AccountId32, ALICE}, + tests::RuntimeOrigin, + AccountIdLookupOf, Code, CodeHash, + }; + + pub fn bare_instantiate(code: Code>) -> BareInstantiateBuilder { + BareInstantiateBuilder::::bare_instantiate(ALICE, code) + } + + pub fn bare_call(dest: AccountId32) -> BareCallBuilder { + BareCallBuilder::::bare_call(ALICE, dest) + } + + pub fn instantiate_with_code(code: Vec) -> InstantiateWithCodeBuilder { + InstantiateWithCodeBuilder::::instantiate_with_code( + RuntimeOrigin::signed(ALICE), + code, + ) + } + + pub fn instantiate(code_hash: CodeHash) -> InstantiateBuilder { + InstantiateBuilder::::instantiate(RuntimeOrigin::signed(ALICE), code_hash) + } + + pub fn call(dest: AccountIdLookupOf) -> CallBuilder { + CallBuilder::::call(RuntimeOrigin::signed(ALICE), dest) + } +} + +impl Test { + pub fn set_unstable_interface(unstable_interface: bool) { + UNSTABLE_INTERFACE.with(|v| *v.borrow_mut() = unstable_interface); + } +} + +parameter_types! { + static TestExtensionTestValue: TestExtension = Default::default(); +} + +#[derive(Clone)] +pub struct TestExtension { + enabled: bool, + last_seen_buffer: Vec, + last_seen_input_len: u32, +} + +#[derive(Default)] +pub struct RevertingExtension; + +#[derive(Default)] +pub struct DisabledExtension; + +#[derive(Default)] +pub struct TempStorageExtension { + storage: u32, +} + +impl TestExtension { + fn disable() { + TestExtensionTestValue::mutate(|e| e.enabled = false) + } + + fn last_seen_buffer() -> Vec { + TestExtensionTestValue::get().last_seen_buffer.clone() + } + + fn last_seen_input_len() -> u32 { + TestExtensionTestValue::get().last_seen_input_len + } +} + +impl Default for TestExtension { + fn default() -> Self { + Self { enabled: true, last_seen_buffer: vec![], last_seen_input_len: 0 } + } +} + +impl ChainExtension for TestExtension { + fn call(&mut self, env: Environment) -> ExtensionResult + where + E: Ext, + { + let func_id = env.func_id(); + let id = env.ext_id() as u32 | func_id as u32; + match func_id { + 0 => { + let mut env = env.buf_in_buf_out(); + let input = env.read(8)?; + env.write(&input, false, None)?; + TestExtensionTestValue::mutate(|e| e.last_seen_buffer = input); + Ok(RetVal::Converging(id)) + }, + 1 => { + let env = env.only_in(); + TestExtensionTestValue::mutate(|e| e.last_seen_input_len = env.val1()); + Ok(RetVal::Converging(id)) + }, + 2 => { + let mut env = env.buf_in_buf_out(); + let mut enc = &env.read(9)?[4..8]; + let weight = Weight::from_parts( + u32::decode(&mut enc).map_err(|_| Error::::ContractTrapped)?.into(), + 0, + ); + env.charge_weight(weight)?; + Ok(RetVal::Converging(id)) + }, + 3 => Ok(RetVal::Diverging { flags: ReturnFlags::REVERT, data: vec![42, 99] }), + _ => { + panic!("Passed unknown id to test chain extension: {}", func_id); + }, + } + } + + fn enabled() -> bool { + TestExtensionTestValue::get().enabled + } +} + +impl RegisteredChainExtension for TestExtension { + const ID: u16 = 0; +} + +impl ChainExtension for RevertingExtension { + fn call(&mut self, _env: Environment) -> ExtensionResult + where + E: Ext, + { + Ok(RetVal::Diverging { flags: ReturnFlags::REVERT, data: vec![0x4B, 0x1D] }) + } + + fn enabled() -> bool { + TestExtensionTestValue::get().enabled + } +} + +impl RegisteredChainExtension for RevertingExtension { + const ID: u16 = 1; +} + +impl ChainExtension for DisabledExtension { + fn call(&mut self, _env: Environment) -> ExtensionResult + where + E: Ext, + { + panic!("Disabled chain extensions are never called") + } + + fn enabled() -> bool { + false + } +} + +impl RegisteredChainExtension for DisabledExtension { + const ID: u16 = 2; +} + +impl ChainExtension for TempStorageExtension { + fn call(&mut self, env: Environment) -> ExtensionResult + where + E: Ext, + { + let func_id = env.func_id(); + match func_id { + 0 => self.storage = 42, + 1 => assert_eq!(self.storage, 42, "Storage is preserved inside the same call."), + 2 => { + assert_eq!(self.storage, 0, "Storage is different for different calls."); + self.storage = 99; + }, + 3 => assert_eq!(self.storage, 99, "Storage is preserved inside the same call."), + _ => { + panic!("Passed unknown id to test chain extension: {}", func_id); + }, + } + Ok(RetVal::Converging(0)) + } + + fn enabled() -> bool { + TestExtensionTestValue::get().enabled + } +} + +impl RegisteredChainExtension for TempStorageExtension { + const ID: u16 = 3; +} + +parameter_types! { + pub BlockWeights: frame_system::limits::BlockWeights = + frame_system::limits::BlockWeights::simple_max( + Weight::from_parts(2u64 * WEIGHT_REF_TIME_PER_SECOND, u64::MAX), + ); + pub static ExistentialDeposit: u64 = 1; +} + +#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] +impl frame_system::Config for Test { + type Block = Block; + type AccountId = AccountId32; + type Lookup = IdentityLookup; + type AccountData = pallet_balances::AccountData; +} + +impl pallet_insecure_randomness_collective_flip::Config for Test {} + +#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)] +impl pallet_balances::Config for Test { + type ExistentialDeposit = ExistentialDeposit; + type ReserveIdentifier = [u8; 8]; + type AccountStore = System; +} + +#[derive_impl(pallet_timestamp::config_preludes::TestDefaultConfig)] +impl pallet_timestamp::Config for Test {} + +impl pallet_utility::Config for Test { + type RuntimeEvent = RuntimeEvent; + type RuntimeCall = RuntimeCall; + type PalletsOrigin = OriginCaller; + type WeightInfo = (); +} + +impl pallet_proxy::Config for Test { + type RuntimeEvent = RuntimeEvent; + type RuntimeCall = RuntimeCall; + type Currency = Balances; + type ProxyType = (); + type ProxyDepositBase = ConstU64<1>; + type ProxyDepositFactor = ConstU64<1>; + type MaxProxies = ConstU32<32>; + type WeightInfo = (); + type MaxPending = ConstU32<32>; + type CallHasher = BlakeTwo256; + type AnnouncementDepositBase = ConstU64<1>; + type AnnouncementDepositFactor = ConstU64<1>; +} + +impl pallet_dummy::Config for Test {} + +parameter_types! { + pub MySchedule: Schedule = { + let schedule = >::default(); + schedule + }; + pub static DepositPerByte: BalanceOf = 1; + pub const DepositPerItem: BalanceOf = 2; + pub static MaxDelegateDependencies: u32 = 32; + pub static MaxTransientStorageSize: u32 = 4 * 1024; + + pub static CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(0); + // We need this one set high enough for running benchmarks. + pub static DefaultDepositLimit: BalanceOf = 10_000_000; +} + +impl Convert> for Test { + fn convert(w: Weight) -> BalanceOf { + w.ref_time() + } +} + +/// A filter whose filter function can be swapped at runtime. +pub struct TestFilter; + +#[derive(Clone)] +pub struct Filters { + filter: fn(&RuntimeCall) -> bool, +} + +impl Default for Filters { + fn default() -> Self { + Filters { filter: (|_| true) } + } +} + +parameter_types! { + static CallFilter: Filters = Default::default(); +} + +impl TestFilter { + pub fn set_filter(filter: fn(&RuntimeCall) -> bool) { + CallFilter::mutate(|fltr| fltr.filter = filter); + } +} + +impl Contains for TestFilter { + fn contains(call: &RuntimeCall) -> bool { + (CallFilter::get().filter)(call) + } +} + +parameter_types! { + pub static UploadAccount: Option<::AccountId> = None; + pub static InstantiateAccount: Option<::AccountId> = None; +} + +pub struct EnsureAccount(core::marker::PhantomData<(T, A)>); +impl>>> + EnsureOrigin<::RuntimeOrigin> for EnsureAccount +where + ::AccountId: From, +{ + type Success = T::AccountId; + + fn try_origin(o: T::RuntimeOrigin) -> Result { + let who = as EnsureOrigin<_>>::try_origin(o.clone())?; + if matches!(A::get(), Some(a) if who != a) { + return Err(o) + } + + Ok(who) + } + + #[cfg(feature = "runtime-benchmarks")] + fn try_successful_origin() -> Result { + Err(()) + } +} +parameter_types! { + pub static UnstableInterface: bool = true; +} + +#[derive_impl(crate::config_preludes::TestDefaultConfig)] +impl Config for Test { + type Time = Timestamp; + type Randomness = Randomness; + type Currency = Balances; + type CallFilter = TestFilter; + type CallStack = [Frame; 5]; + type ChainExtension = + (TestExtension, DisabledExtension, RevertingExtension, TempStorageExtension); + type Schedule = MySchedule; + type DepositPerByte = DepositPerByte; + type DepositPerItem = DepositPerItem; + type DefaultDepositLimit = DefaultDepositLimit; + type AddressGenerator = DefaultAddressGenerator; + type UnsafeUnstableInterface = UnstableInterface; + type UploadOrigin = EnsureAccount; + type InstantiateOrigin = EnsureAccount; + type Migrations = crate::migration::codegen::BenchMigrations; + type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent; + type MaxDelegateDependencies = MaxDelegateDependencies; + type MaxTransientStorageSize = MaxTransientStorageSize; + type Debug = TestDebug; +} + +pub const ALICE: AccountId32 = AccountId32::new([1u8; 32]); +pub const BOB: AccountId32 = AccountId32::new([2u8; 32]); +pub const CHARLIE: AccountId32 = AccountId32::new([3u8; 32]); +pub const DJANGO: AccountId32 = AccountId32::new([4u8; 32]); + +pub const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024); + +pub struct ExtBuilder { + existential_deposit: u64, + storage_version: Option, + code_hashes: Vec>, +} + +impl Default for ExtBuilder { + fn default() -> Self { + Self { + existential_deposit: ExistentialDeposit::get(), + storage_version: None, + code_hashes: vec![], + } + } +} + +impl ExtBuilder { + pub fn existential_deposit(mut self, existential_deposit: u64) -> Self { + self.existential_deposit = existential_deposit; + self + } + pub fn with_code_hashes(mut self, code_hashes: Vec>) -> Self { + self.code_hashes = code_hashes; + self + } + pub fn set_associated_consts(&self) { + EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit); + } + pub fn set_storage_version(mut self, version: u16) -> Self { + self.storage_version = Some(StorageVersion::new(version)); + self + } + pub fn build(self) -> sp_io::TestExternalities { + sp_tracing::try_init_simple(); + self.set_associated_consts(); + let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); + pallet_balances::GenesisConfig:: { balances: vec![] } + .assimilate_storage(&mut t) + .unwrap(); + let mut ext = sp_io::TestExternalities::new(t); + ext.register_extension(KeystoreExt::new(MemoryKeystore::new())); + ext.execute_with(|| { + use frame_support::traits::OnGenesis; + + Pallet::::on_genesis(); + if let Some(storage_version) = self.storage_version { + storage_version.put::>(); + } + System::set_block_number(1) + }); + ext.execute_with(|| { + for code_hash in self.code_hashes { + CodeInfoOf::::insert(code_hash, crate::CodeInfo::new(ALICE)); + } + }); + ext + } +} + +fn initialize_block(number: u64) { + System::reset_events(); + System::initialize(&number, &[0u8; 32].into(), &Default::default()); +} + +struct ExtensionInput<'a> { + extension_id: u16, + func_id: u16, + extra: &'a [u8], +} + +impl<'a> ExtensionInput<'a> { + fn to_vec(&self) -> Vec { + ((self.extension_id as u32) << 16 | (self.func_id as u32)) + .to_le_bytes() + .iter() + .chain(self.extra) + .cloned() + .collect() + } +} + +impl<'a> From> for Vec { + fn from(input: ExtensionInput) -> Vec { + input.to_vec() + } +} + +impl Default for Origin { + fn default() -> Self { + Self::Signed(ALICE) + } +} +// Perform a call to a plain account. +// The actual transfer fails because we can only call contracts. +// Then we check that at least the base costs where charged (no runtime gas costs.) +#[test] +fn calling_plain_account_fails() { + ExtBuilder::default().build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 100_000_000); + let base_cost = <::WeightInfo as WeightInfo>::call(); + + assert_eq!( + builder::call(BOB).build(), + Err(DispatchErrorWithPostInfo { + error: Error::::ContractNotFound.into(), + post_info: PostDispatchInfo { + actual_weight: Some(base_cost), + pays_fee: Default::default(), + }, + }) + ); + }); +} + +#[test] +fn migration_on_idle_hooks_works() { + // Defines expectations of how many migration steps can be done given the weight limit. + let tests = [ + (Weight::zero(), LATEST_MIGRATION_VERSION - 2), + (::WeightInfo::migrate() + 1.into(), LATEST_MIGRATION_VERSION - 1), + (Weight::MAX, LATEST_MIGRATION_VERSION), + ]; + + for (weight, expected_version) in tests { + ExtBuilder::default() + .set_storage_version(LATEST_MIGRATION_VERSION - 2) + .build() + .execute_with(|| { + MigrationInProgress::::set(Some(Default::default())); + Contracts::on_idle(System::block_number(), weight); + assert_eq!(StorageVersion::get::>(), expected_version); + }); + } +} + +#[test] +fn migration_in_progress_works() { + let (wasm, code_hash) = compile_module::("dummy").unwrap(); + + ExtBuilder::default().existential_deposit(1).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + MigrationInProgress::::set(Some(Default::default())); + + assert_err!( + Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + vec![], + None, + Determinism::Enforced + ), + Error::::MigrationInProgress, + ); + assert_err!( + Contracts::remove_code(RuntimeOrigin::signed(ALICE), code_hash), + Error::::MigrationInProgress, + ); + assert_err!( + Contracts::set_code(RuntimeOrigin::signed(ALICE), BOB.clone(), code_hash), + Error::::MigrationInProgress, + ); + assert_err_ignore_postinfo!(builder::call(BOB).build(), Error::::MigrationInProgress); + assert_err_ignore_postinfo!( + builder::instantiate_with_code(wasm).value(100_000).build(), + Error::::MigrationInProgress, + ); + assert_err_ignore_postinfo!( + builder::instantiate(code_hash).value(100_000).build(), + Error::::MigrationInProgress, + ); + }); +} + +#[test] +fn instantiate_and_call_and_deposit_event() { + let (wasm, code_hash) = compile_module::("event_and_return_on_deploy").unwrap(); + + ExtBuilder::default().existential_deposit(1).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let min_balance = Contracts::min_balance(); + let value = 100; + + // We determine the storage deposit limit after uploading because it depends on ALICEs free + // balance which is changed by uploading a module. + assert_ok!(Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm, + None, + Determinism::Enforced + )); + + // Drop previous events + initialize_block(2); + + // Check at the end to get hash on error easily + let addr = builder::bare_instantiate(Code::Existing(code_hash)) + .value(value) + .build_and_unwrap_account_id(); + assert!(ContractInfoOf::::contains_key(&addr)); + + assert_eq!( + System::events(), + vec![ + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::System(frame_system::Event::NewAccount { + account: addr.clone() + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Endowed { + account: addr.clone(), + free_balance: min_balance, + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Transfer { + from: ALICE, + to: addr.clone(), + amount: min_balance, + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Transfer { + from: ALICE, + to: addr.clone(), + amount: value, + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::ContractEmitted { + contract: addr.clone(), + data: vec![1, 2, 3, 4] + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::Instantiated { + deployer: ALICE, + contract: addr.clone() + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts( + pallet_contracts::Event::StorageDepositTransferredAndHeld { + from: ALICE, + to: addr.clone(), + amount: test_utils::contract_info_storage_deposit(&addr), + } + ), + topics: vec![], + }, + ] + ); + }); +} + +#[test] +fn deposit_event_max_value_limit() { + let (wasm, _code_hash) = compile_module::("event_size").unwrap(); + + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + // Create + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(30_000) + .build_and_unwrap_account_id(); + + // Call contract with allowed storage value. + assert_ok!(builder::call(addr.clone()) + .gas_limit(GAS_LIMIT.set_ref_time(GAS_LIMIT.ref_time() * 2)) // we are copying a huge buffer, + .data(::Schedule::get().limits.payload_len.encode()) + .build()); + + // Call contract with too large a storage value. + assert_err_ignore_postinfo!( + builder::call(addr) + .data((::Schedule::get().limits.payload_len + 1).encode()) + .build(), + Error::::ValueTooLarge, + ); + }); +} + +// Fail out of fuel (ref_time weight) in the engine. +#[test] +fn run_out_of_fuel_engine() { + let (wasm, _code_hash) = compile_module::("run_out_of_gas").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(100 * min_balance) + .build_and_unwrap_account_id(); + + // Call the contract with a fixed gas limit. It must run out of gas because it just + // loops forever. + assert_err_ignore_postinfo!( + builder::call(addr) + .gas_limit(Weight::from_parts(1_000_000_000_000, u64::MAX)) + .build(), + Error::::OutOfGas, + ); + }); +} + +// Fail out of fuel (ref_time weight) in the host. +#[test] +fn run_out_of_fuel_host() { + let (code, _hash) = compile_module::("chain_extension").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + + let addr = builder::bare_instantiate(Code::Upload(code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + let gas_limit = Weight::from_parts(u32::MAX as u64, GAS_LIMIT.proof_size()); + + // Use chain extension to charge more ref_time than it is available. + let result = builder::bare_call(addr.clone()) + .gas_limit(gas_limit) + .data(ExtensionInput { extension_id: 0, func_id: 2, extra: &u32::MAX.encode() }.into()) + .build() + .result; + assert_err!(result, >::OutOfGas); + }); +} + +#[test] +fn gas_syncs_work() { + let (code, _code_hash) = compile_module::("caller_is_origin_n").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let addr = builder::bare_instantiate(Code::Upload(code)).build_and_unwrap_account_id(); + + let result = builder::bare_call(addr.clone()).data(0u32.encode()).build(); + assert_ok!(result.result); + let engine_consumed_noop = result.gas_consumed.ref_time(); + + let result = builder::bare_call(addr.clone()).data(1u32.encode()).build(); + assert_ok!(result.result); + let gas_consumed_once = result.gas_consumed.ref_time(); + let host_consumed_once = ::WeightInfo::seal_caller_is_origin().ref_time(); + let engine_consumed_once = gas_consumed_once - host_consumed_once - engine_consumed_noop; + + let result = builder::bare_call(addr).data(2u32.encode()).build(); + assert_ok!(result.result); + let gas_consumed_twice = result.gas_consumed.ref_time(); + let host_consumed_twice = host_consumed_once * 2; + let engine_consumed_twice = gas_consumed_twice - host_consumed_twice - engine_consumed_noop; + + // Second contract just repeats first contract's instructions twice. + // If runtime syncs gas with the engine properly, this should pass. + assert_eq!(engine_consumed_twice, engine_consumed_once * 2); + }); +} + +/// Check that contracts with the same account id have different trie ids. +/// Check the `Nonce` storage item for more information. +#[test] +fn instantiate_unique_trie_id() { + let (wasm, code_hash) = compile_module::("self_destruct").unwrap(); + + ExtBuilder::default().existential_deposit(500).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + Contracts::upload_code(RuntimeOrigin::signed(ALICE), wasm, None, Determinism::Enforced) + .unwrap(); + + // Instantiate the contract and store its trie id for later comparison. + let addr = + builder::bare_instantiate(Code::Existing(code_hash)).build_and_unwrap_account_id(); + let trie_id = get_contract(&addr).trie_id; + + // Try to instantiate it again without termination should yield an error. + assert_err_ignore_postinfo!( + builder::instantiate(code_hash).build(), + >::DuplicateContract, + ); + + // Terminate the contract. + assert_ok!(builder::call(addr.clone()).build()); + + // Re-Instantiate after termination. + assert_ok!(builder::instantiate(code_hash).build()); + + // Trie ids shouldn't match or we might have a collision + assert_ne!(trie_id, get_contract(&addr).trie_id); + }); +} + +#[test] +fn storage_work() { + let (code, _code_hash) = compile_module::("storage").unwrap(); + + ExtBuilder::default().build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let min_balance = Contracts::min_balance(); + let addr = builder::bare_instantiate(Code::Upload(code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + builder::bare_call(addr).build_and_unwrap_result(); + }); +} + +#[test] +fn storage_max_value_limit() { + let (wasm, _code_hash) = compile_module::("storage_size").unwrap(); + + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + // Create + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(30_000) + .build_and_unwrap_account_id(); + get_contract(&addr); + + // Call contract with allowed storage value. + assert_ok!(builder::call(addr.clone()) + .gas_limit(GAS_LIMIT.set_ref_time(GAS_LIMIT.ref_time() * 2)) // we are copying a huge buffer + .data(::Schedule::get().limits.payload_len.encode()) + .build()); + + // Call contract with too large a storage value. + assert_err_ignore_postinfo!( + builder::call(addr) + .data((::Schedule::get().limits.payload_len + 1).encode()) + .build(), + Error::::ValueTooLarge, + ); + }); +} + +#[test] +fn transient_storage_work() { + let (code, _code_hash) = compile_module::("transient_storage").unwrap(); + + ExtBuilder::default().build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let min_balance = Contracts::min_balance(); + let addr = builder::bare_instantiate(Code::Upload(code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + builder::bare_call(addr).build_and_unwrap_result(); + }); +} + +#[test] +fn transient_storage_limit_in_call() { + let (wasm_caller, _code_hash_caller) = + compile_module::("create_transient_storage_and_call").unwrap(); + let (wasm_callee, _code_hash_callee) = compile_module::("set_transient_storage").unwrap(); + ExtBuilder::default().build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let min_balance = Contracts::min_balance(); + + // Create both contracts: Constructors do nothing. + let addr_caller = builder::bare_instantiate(Code::Upload(wasm_caller)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + let addr_callee = builder::bare_instantiate(Code::Upload(wasm_callee)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + let storage_value_size = 1000; + MaxTransientStorageSize::set(4 * 1024); + // Call contracts with storage values within the limit. + // Caller and Callee contracts each set a transient storage value of size 1000. + assert_ok!(builder::call(addr_caller.clone()) + .data((storage_value_size, storage_value_size, &addr_callee).encode()) + .build(),); + + MaxTransientStorageSize::set(512); + // Call a contract with a storage value that is too large. + // Limit exceeded in the caller contract. + assert_err_ignore_postinfo!( + builder::call(addr_caller.clone()) + .data((storage_value_size, storage_value_size, &addr_callee).encode()) + .build(), + >::OutOfTransientStorage, + ); + + MaxTransientStorageSize::set(1536); + // Call a contract with a storage value that is too large. + // Limit exceeded in the callee contract. + assert_err_ignore_postinfo!( + builder::call(addr_caller) + .data((storage_value_size, storage_value_size, &addr_callee).encode()) + .build(), + >::ContractTrapped + ); + }); +} + +#[test] +fn deploy_and_call_other_contract() { + let (caller_wasm, _caller_code_hash) = compile_module::("caller_contract").unwrap(); + let (callee_wasm, callee_code_hash) = compile_module::("return_with_data").unwrap(); + + ExtBuilder::default().existential_deposit(1).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + + // Create + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let caller_addr = builder::bare_instantiate(Code::Upload(caller_wasm)) + .value(100_000) + .build_and_unwrap_account_id(); + Contracts::bare_upload_code(ALICE, callee_wasm, None, Determinism::Enforced).unwrap(); + + let callee_addr = Contracts::contract_address( + &caller_addr, + &callee_code_hash, + &[0, 1, 34, 51, 68, 85, 102, 119], // hard coded in wasm + &[], + ); + + // Drop previous events + initialize_block(2); + + // Call BOB contract, which attempts to instantiate and call the callee contract and + // makes various assertions on the results from those calls. + assert_ok!(builder::call(caller_addr.clone()) + .data(callee_code_hash.as_ref().to_vec()) + .build()); + + assert_eq!( + System::events(), + vec![ + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::System(frame_system::Event::NewAccount { + account: callee_addr.clone() + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Endowed { + account: callee_addr.clone(), + free_balance: min_balance, + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Transfer { + from: ALICE, + to: callee_addr.clone(), + amount: min_balance, + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Transfer { + from: caller_addr.clone(), + to: callee_addr.clone(), + amount: 32768 // hardcoded in wasm + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::Instantiated { + deployer: caller_addr.clone(), + contract: callee_addr.clone(), + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Transfer { + from: caller_addr.clone(), + to: callee_addr.clone(), + amount: 32768, + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::Called { + caller: Origin::from_account_id(caller_addr.clone()), + contract: callee_addr.clone(), + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::Called { + caller: Origin::from_account_id(ALICE), + contract: caller_addr.clone(), + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts( + pallet_contracts::Event::StorageDepositTransferredAndHeld { + from: ALICE, + to: callee_addr.clone(), + amount: test_utils::contract_info_storage_deposit(&callee_addr), + } + ), + topics: vec![], + }, + ] + ); + }); +} + +#[test] +fn delegate_call() { + let (caller_wasm, _caller_code_hash) = compile_module::("delegate_call").unwrap(); + let (callee_wasm, callee_code_hash) = compile_module::("delegate_call_lib").unwrap(); + + ExtBuilder::default().existential_deposit(500).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Instantiate the 'caller' + let caller_addr = builder::bare_instantiate(Code::Upload(caller_wasm)) + .value(300_000) + .build_and_unwrap_account_id(); + // Only upload 'callee' code + assert_ok!(Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + callee_wasm, + Some(codec::Compact(100_000)), + Determinism::Enforced, + )); + + assert_ok!(builder::call(caller_addr.clone()) + .value(1337) + .data(callee_code_hash.as_ref().to_vec()) + .build()); + }); +} + +#[test] +fn track_check_uncheck_module_call() { + let (wasm, code_hash) = compile_module::("dummy").unwrap(); + ExtBuilder::default().build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + Contracts::bare_upload_code(ALICE, wasm, None, Determinism::Enforced).unwrap(); + builder::bare_instantiate(Code::Existing(code_hash)).build_and_unwrap_result(); + }); + + // It should have recorded 1 `Checked` for the upload and 1 `Unchecked` for the instantiate. + let record = crate::wasm::tracker::LOADED_MODULE.with(|stack| stack.borrow().clone()); + assert_eq!(record, vec![LoadingMode::Checked, LoadingMode::Unchecked]); +} + +#[test] +fn transfer_expendable_cannot_kill_account() { + let (wasm, _code_hash) = compile_module::("dummy").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Instantiate the BOB contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(1_000) + .build_and_unwrap_account_id(); + + // Check that the BOB contract has been instantiated. + get_contract(&addr); + + let total_balance = ::Currency::total_balance(&addr); + + assert_eq!( + test_utils::get_balance_on_hold(&HoldReason::StorageDepositReserve.into(), &addr), + test_utils::contract_info_storage_deposit(&addr) + ); + + // Some ot the total balance is held, so it can't be transferred. + assert_err!( + <::Currency as Mutate>::transfer( + &addr, + &ALICE, + total_balance, + Preservation::Expendable, + ), + TokenError::FundsUnavailable, + ); + + assert_eq!(::Currency::total_balance(&addr), total_balance); + }); +} + +#[test] +fn cannot_self_destruct_through_draining() { + let (wasm, _code_hash) = compile_module::("drain").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let value = 1_000; + let min_balance = Contracts::min_balance(); + + // Instantiate the BOB contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(value) + .build_and_unwrap_account_id(); + + // Check that the BOB contract has been instantiated. + get_contract(&addr); + + // Call BOB which makes it send all funds to the zero address + // The contract code asserts that the transfer fails with the correct error code + assert_ok!(builder::call(addr.clone()).build()); + + // Make sure the account wasn't remove by sending all free balance away. + assert_eq!( + ::Currency::total_balance(&addr), + value + test_utils::contract_info_storage_deposit(&addr) + min_balance, + ); + }); +} + +#[test] +fn cannot_self_destruct_through_storage_refund_after_price_change() { + let (wasm, _code_hash) = compile_module::("store_call").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let min_balance = Contracts::min_balance(); + + // Instantiate the BOB contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)).build_and_unwrap_account_id(); + + let info_deposit = test_utils::contract_info_storage_deposit(&addr); + + // Check that the contract has been instantiated and has the minimum balance + assert_eq!(get_contract(&addr).total_deposit(), info_deposit); + assert_eq!(get_contract(&addr).extra_deposit(), 0); + assert_eq!(::Currency::total_balance(&addr), info_deposit + min_balance); + + // Create 100 bytes of storage with a price of per byte and a single storage item of price 2 + assert_ok!(builder::call(addr.clone()).data(100u32.to_le_bytes().to_vec()).build()); + assert_eq!(get_contract(&addr).total_deposit(), info_deposit + 102); + + // Increase the byte price and trigger a refund. This should not have any influence because + // the removal is pro rata and exactly those 100 bytes should have been removed. + DEPOSIT_PER_BYTE.with(|c| *c.borrow_mut() = 500); + assert_ok!(builder::call(addr.clone()).data(0u32.to_le_bytes().to_vec()).build()); + + // Make sure the account wasn't removed by the refund + assert_eq!( + ::Currency::total_balance(&addr), + get_contract(&addr).total_deposit() + min_balance, + ); + assert_eq!(get_contract(&addr).extra_deposit(), 2); + }); +} + +#[test] +fn cannot_self_destruct_while_live() { + let (wasm, _code_hash) = compile_module::("self_destruct").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Instantiate the BOB contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(100_000) + .build_and_unwrap_account_id(); + + // Check that the BOB contract has been instantiated. + get_contract(&addr); + + // Call BOB with input data, forcing it make a recursive call to itself to + // self-destruct, resulting in a trap. + assert_err_ignore_postinfo!( + builder::call(addr.clone()).data(vec![0]).build(), + Error::::ContractTrapped, + ); + + // Check that BOB is still there. + get_contract(&addr); + }); +} + +#[test] +fn self_destruct_works() { + let (wasm, code_hash) = compile_module::("self_destruct").unwrap(); + ExtBuilder::default().existential_deposit(1_000).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let _ = ::Currency::set_balance(&DJANGO, 1_000_000); + let min_balance = Contracts::min_balance(); + + // Instantiate the BOB contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(100_000) + .build_and_unwrap_account_id(); + + // Check that the BOB contract has been instantiated. + let _ = get_contract(&addr); + + let info_deposit = test_utils::contract_info_storage_deposit(&addr); + + // Drop all previous events + initialize_block(2); + + // Call BOB without input data which triggers termination. + assert_matches!(builder::call(addr.clone()).build(), Ok(_)); + + // Check that code is still there but refcount dropped to zero. + assert_refcount!(&code_hash, 0); + + // Check that account is gone + assert!(get_contract_checked(&addr).is_none()); + assert_eq!(::Currency::total_balance(&addr), 0); + + // Check that the beneficiary (django) got remaining balance. + assert_eq!( + ::Currency::free_balance(DJANGO), + 1_000_000 + 100_000 + min_balance + ); + + // Check that the Alice is missing Django's benefit. Within ALICE's total balance there's + // also the code upload deposit held. + assert_eq!( + ::Currency::total_balance(&ALICE), + 1_000_000 - (100_000 + min_balance) + ); + + pretty_assertions::assert_eq!( + System::events(), + vec![ + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::Terminated { + contract: addr.clone(), + beneficiary: DJANGO + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::Called { + caller: Origin::from_account_id(ALICE), + contract: addr.clone(), + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts( + pallet_contracts::Event::StorageDepositTransferredAndReleased { + from: addr.clone(), + to: ALICE, + amount: info_deposit, + } + ), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::System(frame_system::Event::KilledAccount { + account: addr.clone() + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Transfer { + from: addr.clone(), + to: DJANGO, + amount: 100_000 + min_balance, + }), + topics: vec![], + }, + ], + ); + }); +} + +// This tests that one contract cannot prevent another from self-destructing by sending it +// additional funds after it has been drained. +#[test] +fn destroy_contract_and_transfer_funds() { + let (callee_wasm, callee_code_hash) = compile_module::("self_destruct").unwrap(); + let (caller_wasm, _caller_code_hash) = compile_module::("destroy_and_transfer").unwrap(); + + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + // Create code hash for bob to instantiate + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + Contracts::bare_upload_code(ALICE, callee_wasm, None, Determinism::Enforced).unwrap(); + + // This deploys the BOB contract, which in turn deploys the CHARLIE contract during + // construction. + let addr_bob = builder::bare_instantiate(Code::Upload(caller_wasm)) + .value(200_000) + .data(callee_code_hash.as_ref().to_vec()) + .build_and_unwrap_account_id(); + + // Check that the CHARLIE contract has been instantiated. + let addr_charlie = + Contracts::contract_address(&addr_bob, &callee_code_hash, &[], &[0x47, 0x11]); + get_contract(&addr_charlie); + + // Call BOB, which calls CHARLIE, forcing CHARLIE to self-destruct. + assert_ok!(builder::call(addr_bob).data(addr_charlie.encode()).build()); + + // Check that CHARLIE has moved on to the great beyond (ie. died). + assert!(get_contract_checked(&addr_charlie).is_none()); + }); +} + +#[test] +fn cannot_self_destruct_in_constructor() { + let (wasm, _) = compile_module::("self_destructing_constructor").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Fail to instantiate the BOB because the constructor calls seal_terminate. + assert_err_ignore_postinfo!( + builder::instantiate_with_code(wasm).value(100_000).build(), + Error::::TerminatedInConstructor, + ); + }); +} + +#[test] +fn crypto_hashes() { + let (wasm, _code_hash) = compile_module::("crypto_hashes").unwrap(); + + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Instantiate the CRYPTO_HASHES contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(100_000) + .build_and_unwrap_account_id(); + // Perform the call. + let input = b"_DEAD_BEEF"; + use sp_io::hashing::*; + // Wraps a hash function into a more dynamic form usable for testing. + macro_rules! dyn_hash_fn { + ($name:ident) => { + Box::new(|input| $name(input).as_ref().to_vec().into_boxed_slice()) + }; + } + // All hash functions and their associated output byte lengths. + let test_cases: &[(Box Box<[u8]>>, usize)] = &[ + (dyn_hash_fn!(sha2_256), 32), + (dyn_hash_fn!(keccak_256), 32), + (dyn_hash_fn!(blake2_256), 32), + (dyn_hash_fn!(blake2_128), 16), + ]; + // Test the given hash functions for the input: "_DEAD_BEEF" + for (n, (hash_fn, expected_size)) in test_cases.iter().enumerate() { + // We offset data in the contract tables by 1. + let mut params = vec![(n + 1) as u8]; + params.extend_from_slice(input); + let result = builder::bare_call(addr.clone()).data(params).build_and_unwrap_result(); + assert!(!result.did_revert()); + let expected = hash_fn(input.as_ref()); + assert_eq!(&result.data[..*expected_size], &*expected); + } + }) +} + +#[test] +fn transfer_return_code() { + let (wasm, _code_hash) = compile_module::("transfer_return_code").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + // Contract has only the minimal balance so any transfer will fail. + ::Currency::set_balance(&addr, min_balance); + let result = builder::bare_call(addr.clone()).build_and_unwrap_result(); + assert_return_code!(result, RuntimeReturnCode::TransferFailed); + }); +} + +#[test] +fn call_return_code() { + let (caller_code, _caller_hash) = compile_module::("call_return_code").unwrap(); + let (callee_code, _callee_hash) = compile_module::("ok_trap_revert").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + let _ = ::Currency::set_balance(&CHARLIE, 1000 * min_balance); + + let addr_bob = builder::bare_instantiate(Code::Upload(caller_code)) + .value(min_balance * 100) + .data(vec![0]) + .build_and_unwrap_account_id(); + ::Currency::set_balance(&addr_bob, min_balance); + + // Contract calls into Django which is no valid contract + let result = builder::bare_call(addr_bob.clone()) + .data(AsRef::<[u8]>::as_ref(&DJANGO).to_vec()) + .build_and_unwrap_result(); + assert_return_code!(result, RuntimeReturnCode::NotCallable); + + let addr_django = builder::bare_instantiate(Code::Upload(callee_code)) + .origin(CHARLIE) + .value(min_balance * 100) + .data(vec![0]) + .build_and_unwrap_account_id(); + ::Currency::set_balance(&addr_django, min_balance); + + // Contract has only the minimal balance so any transfer will fail. + let result = builder::bare_call(addr_bob.clone()) + .data( + AsRef::<[u8]>::as_ref(&addr_django) + .iter() + .chain(&0u32.to_le_bytes()) + .cloned() + .collect(), + ) + .build_and_unwrap_result(); + assert_return_code!(result, RuntimeReturnCode::TransferFailed); + + // Contract has enough balance but callee reverts because "1" is passed. + ::Currency::set_balance(&addr_bob, min_balance + 1000); + let result = builder::bare_call(addr_bob.clone()) + .data( + AsRef::<[u8]>::as_ref(&addr_django) + .iter() + .chain(&1u32.to_le_bytes()) + .cloned() + .collect(), + ) + .build_and_unwrap_result(); + assert_return_code!(result, RuntimeReturnCode::CalleeReverted); + + // Contract has enough balance but callee traps because "2" is passed. + let result = builder::bare_call(addr_bob) + .data( + AsRef::<[u8]>::as_ref(&addr_django) + .iter() + .chain(&2u32.to_le_bytes()) + .cloned() + .collect(), + ) + .build_and_unwrap_result(); + assert_return_code!(result, RuntimeReturnCode::CalleeTrapped); + }); +} + +#[test] +fn instantiate_return_code() { + let (caller_code, _caller_hash) = compile_module::("instantiate_return_code").unwrap(); + let (callee_code, callee_hash) = compile_module::("ok_trap_revert").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + let _ = ::Currency::set_balance(&CHARLIE, 1000 * min_balance); + let callee_hash = callee_hash.as_ref().to_vec(); + + assert_ok!(builder::instantiate_with_code(callee_code).value(min_balance * 100).build()); + + let addr = builder::bare_instantiate(Code::Upload(caller_code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + // Contract has only the minimal balance so any transfer will fail. + ::Currency::set_balance(&addr, min_balance); + let result = builder::bare_call(addr.clone()) + .data(callee_hash.clone()) + .build_and_unwrap_result(); + assert_return_code!(result, RuntimeReturnCode::TransferFailed); + + // Contract has enough balance but the passed code hash is invalid + ::Currency::set_balance(&addr, min_balance + 10_000); + let result = builder::bare_call(addr.clone()).data(vec![0; 33]).build_and_unwrap_result(); + assert_return_code!(result, RuntimeReturnCode::CodeNotFound); + + // Contract has enough balance but callee reverts because "1" is passed. + let result = builder::bare_call(addr.clone()) + .data(callee_hash.iter().chain(&1u32.to_le_bytes()).cloned().collect()) + .build_and_unwrap_result(); + assert_return_code!(result, RuntimeReturnCode::CalleeReverted); + + // Contract has enough balance but callee traps because "2" is passed. + let result = builder::bare_call(addr) + .data(callee_hash.iter().chain(&2u32.to_le_bytes()).cloned().collect()) + .build_and_unwrap_result(); + assert_return_code!(result, RuntimeReturnCode::CalleeTrapped); + }); +} + +#[test] +fn disabled_chain_extension_wont_deploy() { + let (code, _hash) = compile_module::("chain_extension").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + TestExtension::disable(); + assert_err_ignore_postinfo!( + builder::instantiate_with_code(code).value(3 * min_balance).build(), + >::CodeRejected, + ); + }); +} + +#[test] +fn disabled_chain_extension_errors_on_call() { + let (code, _hash) = compile_module::("chain_extension").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + let addr = builder::bare_instantiate(Code::Upload(code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + TestExtension::disable(); + assert_err_ignore_postinfo!( + builder::call(addr.clone()).build(), + Error::::CodeRejected, + ); + }); +} + +#[test] +fn chain_extension_works() { + let (code, _hash) = compile_module::("chain_extension").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + let addr = builder::bare_instantiate(Code::Upload(code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + // 0 = read input buffer and pass it through as output + let input: Vec = ExtensionInput { extension_id: 0, func_id: 0, extra: &[99] }.into(); + let result = builder::bare_call(addr.clone()).data(input.clone()).build(); + assert_eq!(TestExtension::last_seen_buffer(), input); + assert_eq!(result.result.unwrap().data, input); + + // 1 = treat inputs as integer primitives and store the supplied integers + builder::bare_call(addr.clone()) + .data(ExtensionInput { extension_id: 0, func_id: 1, extra: &[] }.into()) + .build_and_unwrap_result(); + assert_eq!(TestExtension::last_seen_input_len(), 4); + + // 2 = charge some extra weight (amount supplied in the fifth byte) + let result = builder::bare_call(addr.clone()) + .data(ExtensionInput { extension_id: 0, func_id: 2, extra: &0u32.encode() }.into()) + .build(); + assert_ok!(result.result); + let gas_consumed = result.gas_consumed; + let result = builder::bare_call(addr.clone()) + .data(ExtensionInput { extension_id: 0, func_id: 2, extra: &42u32.encode() }.into()) + .build(); + assert_ok!(result.result); + assert_eq!(result.gas_consumed.ref_time(), gas_consumed.ref_time() + 42); + let result = builder::bare_call(addr.clone()) + .data(ExtensionInput { extension_id: 0, func_id: 2, extra: &95u32.encode() }.into()) + .build(); + assert_ok!(result.result); + assert_eq!(result.gas_consumed.ref_time(), gas_consumed.ref_time() + 95); + + // 3 = diverging chain extension call that sets flags to 0x1 and returns a fixed buffer + let result = builder::bare_call(addr.clone()) + .data(ExtensionInput { extension_id: 0, func_id: 3, extra: &[] }.into()) + .build_and_unwrap_result(); + assert_eq!(result.flags, ReturnFlags::REVERT); + assert_eq!(result.data, vec![42, 99]); + + // diverging to second chain extension that sets flags to 0x1 and returns a fixed buffer + // We set the MSB part to 1 (instead of 0) which routes the request into the second + // extension + let result = builder::bare_call(addr.clone()) + .data(ExtensionInput { extension_id: 1, func_id: 0, extra: &[] }.into()) + .build_and_unwrap_result(); + assert_eq!(result.flags, ReturnFlags::REVERT); + assert_eq!(result.data, vec![0x4B, 0x1D]); + + // Diverging to third chain extension that is disabled + // We set the MSB part to 2 (instead of 0) which routes the request into the third extension + assert_err_ignore_postinfo!( + builder::call(addr.clone()) + .data(ExtensionInput { extension_id: 2, func_id: 0, extra: &[] }.into()) + .build(), + Error::::NoChainExtension, + ); + }); +} + +#[test] +fn chain_extension_temp_storage_works() { + let (code, _hash) = compile_module::("chain_extension_temp_storage").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + let addr = builder::bare_instantiate(Code::Upload(code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + // Call func 0 and func 1 back to back. + let stop_recursion = 0u8; + let mut input: Vec = ExtensionInput { extension_id: 3, func_id: 0, extra: &[] }.into(); + input.extend_from_slice( + ExtensionInput { extension_id: 3, func_id: 1, extra: &[stop_recursion] } + .to_vec() + .as_ref(), + ); + + assert_ok!(builder::bare_call(addr.clone()).data(input.clone()).build().result); + }) +} + +#[test] +fn lazy_removal_works() { + let (code, _hash) = compile_module::("self_destruct").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + + let addr = builder::bare_instantiate(Code::Upload(code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + let info = get_contract(&addr); + let trie = &info.child_trie_info(); + + // Put value into the contracts child trie + child::put(trie, &[99], &42); + + // Terminate the contract + assert_ok!(builder::call(addr.clone()).build()); + + // Contract info should be gone + assert!(!>::contains_key(&addr)); + + // But value should be still there as the lazy removal did not run, yet. + assert_matches!(child::get(trie, &[99]), Some(42)); + + // Run the lazy removal + Contracts::on_idle(System::block_number(), Weight::MAX); + + // Value should be gone now + assert_matches!(child::get::(trie, &[99]), None); + }); +} + +#[test] +fn lazy_batch_removal_works() { + let (code, _hash) = compile_module::("self_destruct").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + let mut tries: Vec = vec![]; + + for i in 0..3u8 { + let addr = builder::bare_instantiate(Code::Upload(code.clone())) + .value(min_balance * 100) + .salt(vec![i]) + .build_and_unwrap_account_id(); + + let info = get_contract(&addr); + let trie = &info.child_trie_info(); + + // Put value into the contracts child trie + child::put(trie, &[99], &42); + + // Terminate the contract. Contract info should be gone, but value should be still there + // as the lazy removal did not run, yet. + assert_ok!(builder::call(addr.clone()).build()); + + assert!(!>::contains_key(&addr)); + assert_matches!(child::get(trie, &[99]), Some(42)); + + tries.push(trie.clone()) + } + + // Run single lazy removal + Contracts::on_idle(System::block_number(), Weight::MAX); + + // The single lazy removal should have removed all queued tries + for trie in tries.iter() { + assert_matches!(child::get::(trie, &[99]), None); + } + }); +} + +#[test] +fn lazy_removal_partial_remove_works() { + let (code, _hash) = compile_module::("self_destruct").unwrap(); + + // We create a contract with some extra keys above the weight limit + let extra_keys = 7u32; + let mut meter = WeightMeter::with_limit(Weight::from_parts(5_000_000_000, 100 * 1024)); + let (weight_per_key, max_keys) = ContractInfo::::deletion_budget(&meter); + let vals: Vec<_> = (0..max_keys + extra_keys) + .map(|i| (blake2_256(&i.encode()), (i as u32), (i as u32).encode())) + .collect(); + + let mut ext = ExtBuilder::default().existential_deposit(50).build(); + + let trie = ext.execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + + let addr = builder::bare_instantiate(Code::Upload(code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + let info = get_contract(&addr); + + // Put value into the contracts child trie + for val in &vals { + info.write(&Key::Fix(val.0), Some(val.2.clone()), None, false).unwrap(); + } + >::insert(&addr, info.clone()); + + // Terminate the contract + assert_ok!(builder::call(addr.clone()).build()); + + // Contract info should be gone + assert!(!>::contains_key(&addr)); + + let trie = info.child_trie_info(); + + // But value should be still there as the lazy removal did not run, yet. + for val in &vals { + assert_eq!(child::get::(&trie, &blake2_256(&val.0)), Some(val.1)); + } + + trie.clone() + }); + + // The lazy removal limit only applies to the backend but not to the overlay. + // This commits all keys from the overlay to the backend. + ext.commit_all().unwrap(); + + ext.execute_with(|| { + // Run the lazy removal + ContractInfo::::process_deletion_queue_batch(&mut meter); + + // Weight should be exhausted because we could not even delete all keys + assert!(!meter.can_consume(weight_per_key)); + + let mut num_deleted = 0u32; + let mut num_remaining = 0u32; + + for val in &vals { + match child::get::(&trie, &blake2_256(&val.0)) { + None => num_deleted += 1, + Some(x) if x == val.1 => num_remaining += 1, + Some(_) => panic!("Unexpected value in contract storage"), + } + } + + // All but one key is removed + assert_eq!(num_deleted + num_remaining, vals.len() as u32); + assert_eq!(num_deleted, max_keys); + assert_eq!(num_remaining, extra_keys); + }); +} + +#[test] +fn lazy_removal_does_no_run_on_low_remaining_weight() { + let (code, _hash) = compile_module::("self_destruct").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + + let addr = builder::bare_instantiate(Code::Upload(code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + let info = get_contract(&addr); + let trie = &info.child_trie_info(); + + // Put value into the contracts child trie + child::put(trie, &[99], &42); + + // Terminate the contract + assert_ok!(builder::call(addr.clone()).build()); + + // Contract info should be gone + assert!(!>::contains_key(&addr)); + + // But value should be still there as the lazy removal did not run, yet. + assert_matches!(child::get(trie, &[99]), Some(42)); + + // Assign a remaining weight which is too low for a successful deletion of the contract + let low_remaining_weight = + <::WeightInfo as WeightInfo>::on_process_deletion_queue_batch(); + + // Run the lazy removal + Contracts::on_idle(System::block_number(), low_remaining_weight); + + // Value should still be there, since remaining weight was too low for removal + assert_matches!(child::get::(trie, &[99]), Some(42)); + + // Run the lazy removal while deletion_queue is not full + Contracts::on_initialize(System::block_number()); + + // Value should still be there, since deletion_queue was not full + assert_matches!(child::get::(trie, &[99]), Some(42)); + + // Run on_idle with max remaining weight, this should remove the value + Contracts::on_idle(System::block_number(), Weight::MAX); + + // Value should be gone + assert_matches!(child::get::(trie, &[99]), None); + }); +} + +#[test] +fn lazy_removal_does_not_use_all_weight() { + let (code, _hash) = compile_module::("self_destruct").unwrap(); + + let mut meter = WeightMeter::with_limit(Weight::from_parts(5_000_000_000, 100 * 1024)); + let mut ext = ExtBuilder::default().existential_deposit(50).build(); + + let (trie, vals, weight_per_key) = ext.execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + + let addr = builder::bare_instantiate(Code::Upload(code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + let info = get_contract(&addr); + let (weight_per_key, max_keys) = ContractInfo::::deletion_budget(&meter); + assert!(max_keys > 0); + + // We create a contract with one less storage item than we can remove within the limit + let vals: Vec<_> = (0..max_keys - 1) + .map(|i| (blake2_256(&i.encode()), (i as u32), (i as u32).encode())) + .collect(); + + // Put value into the contracts child trie + for val in &vals { + info.write(&Key::Fix(val.0), Some(val.2.clone()), None, false).unwrap(); + } + >::insert(&addr, info.clone()); + + // Terminate the contract + assert_ok!(builder::call(addr.clone()).build()); + + // Contract info should be gone + assert!(!>::contains_key(&addr)); + + let trie = info.child_trie_info(); + + // But value should be still there as the lazy removal did not run, yet. + for val in &vals { + assert_eq!(child::get::(&trie, &blake2_256(&val.0)), Some(val.1)); + } + + (trie, vals, weight_per_key) + }); + + // The lazy removal limit only applies to the backend but not to the overlay. + // This commits all keys from the overlay to the backend. + ext.commit_all().unwrap(); + + ext.execute_with(|| { + // Run the lazy removal + ContractInfo::::process_deletion_queue_batch(&mut meter); + let base_weight = + <::WeightInfo as WeightInfo>::on_process_deletion_queue_batch(); + assert_eq!(meter.consumed(), weight_per_key.mul(vals.len() as _) + base_weight); + + // All the keys are removed + for val in vals { + assert_eq!(child::get::(&trie, &blake2_256(&val.0)), None); + } + }); +} + +#[test] +fn deletion_queue_ring_buffer_overflow() { + let (code, _hash) = compile_module::("self_destruct").unwrap(); + let mut ext = ExtBuilder::default().existential_deposit(50).build(); + + // setup the deletion queue with custom counters + ext.execute_with(|| { + let queue = DeletionQueueManager::from_test_values(u32::MAX - 1, u32::MAX - 1); + >::set(queue); + }); + + // commit the changes to the storage + ext.commit_all().unwrap(); + + ext.execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + let mut tries: Vec = vec![]; + + // add 3 contracts to the deletion queue + for i in 0..3u8 { + let addr = builder::bare_instantiate(Code::Upload(code.clone())) + .value(min_balance * 100) + .salt(vec![i]) + .build_and_unwrap_account_id(); + + let info = get_contract(&addr); + let trie = &info.child_trie_info(); + + // Put value into the contracts child trie + child::put(trie, &[99], &42); + + // Terminate the contract. Contract info should be gone, but value should be still + // there as the lazy removal did not run, yet. + assert_ok!(builder::call(addr.clone()).build()); + + assert!(!>::contains_key(&addr)); + assert_matches!(child::get(trie, &[99]), Some(42)); + + tries.push(trie.clone()) + } + + // Run single lazy removal + Contracts::on_idle(System::block_number(), Weight::MAX); + + // The single lazy removal should have removed all queued tries + for trie in tries.iter() { + assert_matches!(child::get::(trie, &[99]), None); + } + + // insert and delete counter values should go from u32::MAX - 1 to 1 + assert_eq!(>::get().as_test_tuple(), (1, 1)); + }) +} +#[test] +fn refcounter() { + let (wasm, code_hash) = compile_module::("self_destruct").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let min_balance = Contracts::min_balance(); + + // Create two contracts with the same code and check that they do in fact share it. + let addr0 = builder::bare_instantiate(Code::Upload(wasm.clone())) + .value(min_balance * 100) + .salt(vec![0]) + .build_and_unwrap_account_id(); + let addr1 = builder::bare_instantiate(Code::Upload(wasm.clone())) + .value(min_balance * 100) + .salt(vec![1]) + .build_and_unwrap_account_id(); + assert_refcount!(code_hash, 2); + + // Sharing should also work with the usual instantiate call + let addr2 = builder::bare_instantiate(Code::Existing(code_hash)) + .value(min_balance * 100) + .salt(vec![2]) + .build_and_unwrap_account_id(); + assert_refcount!(code_hash, 3); + + // Terminating one contract should decrement the refcount + assert_ok!(builder::call(addr0).build()); + assert_refcount!(code_hash, 2); + + // remove another one + assert_ok!(builder::call(addr1).build()); + assert_refcount!(code_hash, 1); + + // Pristine code should still be there + PristineCode::::get(code_hash).unwrap(); + + // remove the last contract + assert_ok!(builder::call(addr2).build()); + assert_refcount!(code_hash, 0); + + // refcount is `0` but code should still exists because it needs to be removed manually + assert!(crate::PristineCode::::contains_key(&code_hash)); + }); +} + +#[test] +fn debug_message_works() { + let (wasm, _code_hash) = compile_module::("debug_message_works").unwrap(); + + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(30_000) + .build_and_unwrap_account_id(); + let result = builder::bare_call(addr).debug(DebugInfo::UnsafeDebug).build(); + + assert_matches!(result.result, Ok(_)); + assert_eq!(std::str::from_utf8(&result.debug_message).unwrap(), "Hello World!"); + }); +} + +#[test] +fn debug_message_logging_disabled() { + let (wasm, _code_hash) = compile_module::("debug_message_logging_disabled").unwrap(); + + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(30_000) + .build_and_unwrap_account_id(); + // disable logging by passing `false` + let result = builder::bare_call(addr.clone()).build(); + assert_matches!(result.result, Ok(_)); + // the dispatchables always run without debugging + assert_ok!(Contracts::call(RuntimeOrigin::signed(ALICE), addr, 0, GAS_LIMIT, None, vec![])); + assert!(result.debug_message.is_empty()); + }); +} + +#[test] +fn debug_message_invalid_utf8() { + let (wasm, _code_hash) = compile_module::("debug_message_invalid_utf8").unwrap(); + + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(30_000) + .build_and_unwrap_account_id(); + let result = builder::bare_call(addr).debug(DebugInfo::UnsafeDebug).build(); + assert_ok!(result.result); + assert!(result.debug_message.is_empty()); + }); +} + +#[test] +fn gas_estimation_for_subcalls() { + let (caller_code, _caller_hash) = compile_module::("call_with_limit").unwrap(); + let (call_runtime_code, _caller_hash) = compile_module::("call_runtime").unwrap(); + let (dummy_code, _callee_hash) = compile_module::("dummy").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 2_000 * min_balance); + + let addr_caller = builder::bare_instantiate(Code::Upload(caller_code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + let addr_dummy = builder::bare_instantiate(Code::Upload(dummy_code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + let addr_call_runtime = builder::bare_instantiate(Code::Upload(call_runtime_code)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + // Run the test for all of those weight limits for the subcall + let weights = [ + Weight::zero(), + GAS_LIMIT, + GAS_LIMIT * 2, + GAS_LIMIT / 5, + Weight::from_parts(0, GAS_LIMIT.proof_size()), + Weight::from_parts(GAS_LIMIT.ref_time(), 0), + ]; + + // This call is passed to the sub call in order to create a large `required_weight` + let runtime_call = RuntimeCall::Dummy(pallet_dummy::Call::overestimate_pre_charge { + pre_charge: Weight::from_parts(10_000_000_000, 512 * 1024), + actual_weight: Weight::from_parts(1, 1), + }) + .encode(); + + // Encodes which contract should be sub called with which input + let sub_calls: [(&[u8], Vec<_>, bool); 2] = [ + (addr_dummy.as_ref(), vec![], false), + (addr_call_runtime.as_ref(), runtime_call, true), + ]; + + for weight in weights { + for (sub_addr, sub_input, out_of_gas_in_subcall) in &sub_calls { + let input: Vec = sub_addr + .iter() + .cloned() + .chain(weight.ref_time().to_le_bytes()) + .chain(weight.proof_size().to_le_bytes()) + .chain(sub_input.clone()) + .collect(); + + // Call in order to determine the gas that is required for this call + let result = builder::bare_call(addr_caller.clone()).data(input.clone()).build(); + assert_ok!(&result.result); + + // If the out of gas happens in the subcall the caller contract + // will just trap. Otherwise we would need to forward an error + // code to signal that the sub contract ran out of gas. + let error: DispatchError = if *out_of_gas_in_subcall { + assert!(result.gas_required.all_gt(result.gas_consumed)); + >::ContractTrapped.into() + } else { + assert_eq!(result.gas_required, result.gas_consumed); + >::OutOfGas.into() + }; + + // Make the same call using the estimated gas. Should succeed. + assert_ok!( + builder::bare_call(addr_caller.clone()) + .gas_limit(result.gas_required) + .storage_deposit_limit(Some(result.storage_deposit.charge_or_zero())) + .data(input.clone()) + .build() + .result + ); + + // Check that it fails with too little ref_time + assert_err!( + builder::bare_call(addr_caller.clone()) + .gas_limit(result.gas_required.sub_ref_time(1)) + .storage_deposit_limit(Some(result.storage_deposit.charge_or_zero())) + .data(input.clone()) + .build() + .result, + error, + ); + + // Check that it fails with too little proof_size + assert_err!( + builder::bare_call(addr_caller.clone()) + .gas_limit(result.gas_required.sub_proof_size(1)) + .storage_deposit_limit(Some(result.storage_deposit.charge_or_zero())) + .data(input) + .build() + .result, + error, + ); + } + } + }); +} + +#[test] +fn gas_estimation_call_runtime() { + let (caller_code, _caller_hash) = compile_module::("call_runtime").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + let _ = ::Currency::set_balance(&CHARLIE, 1000 * min_balance); + + let addr_caller = builder::bare_instantiate(Code::Upload(caller_code)) + .value(min_balance * 100) + .salt(vec![0]) + .build_and_unwrap_account_id(); + + // Call something trivial with a huge gas limit so that we can observe the effects + // of pre-charging. This should create a difference between consumed and required. + let call = RuntimeCall::Dummy(pallet_dummy::Call::overestimate_pre_charge { + pre_charge: Weight::from_parts(10_000_000, 1_000), + actual_weight: Weight::from_parts(100, 100), + }); + let result = builder::bare_call(addr_caller.clone()).data(call.encode()).build(); + // contract encodes the result of the dispatch runtime + let outcome = u32::decode(&mut result.result.unwrap().data.as_ref()).unwrap(); + assert_eq!(outcome, 0); + assert!(result.gas_required.all_gt(result.gas_consumed)); + + // Make the same call using the required gas. Should succeed. + assert_ok!( + builder::bare_call(addr_caller) + .gas_limit(result.gas_required) + .data(call.encode()) + .build() + .result + ); + }); +} + +#[test] +fn call_runtime_reentrancy_guarded() { + let (caller_code, _caller_hash) = compile_module::("call_runtime").unwrap(); + let (callee_code, _callee_hash) = compile_module::("dummy").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + let _ = ::Currency::set_balance(&CHARLIE, 1000 * min_balance); + + let addr_caller = builder::bare_instantiate(Code::Upload(caller_code)) + .value(min_balance * 100) + .salt(vec![0]) + .build_and_unwrap_account_id(); + + let addr_callee = builder::bare_instantiate(Code::Upload(callee_code)) + .value(min_balance * 100) + .salt(vec![1]) + .build_and_unwrap_account_id(); + + // Call pallet_contracts call() dispatchable + let call = RuntimeCall::Contracts(crate::Call::call { + dest: addr_callee, + value: 0, + gas_limit: GAS_LIMIT / 3, + storage_deposit_limit: None, + data: vec![], + }); + + // Call runtime to re-enter back to contracts engine by + // calling dummy contract + let result = builder::bare_call(addr_caller.clone()) + .data(call.encode()) + .build_and_unwrap_result(); + // Call to runtime should fail because of the re-entrancy guard + assert_return_code!(result, RuntimeReturnCode::CallRuntimeFailed); + }); +} + +#[test] +fn ecdsa_recover() { + let (wasm, _code_hash) = compile_module::("ecdsa_recover").unwrap(); + + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Instantiate the ecdsa_recover contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(100_000) + .build_and_unwrap_account_id(); + + #[rustfmt::skip] + let signature: [u8; 65] = [ + 161, 234, 203, 74, 147, 96, 51, 212, 5, 174, 231, 9, 142, 48, 137, 201, + 162, 118, 192, 67, 239, 16, 71, 216, 125, 86, 167, 139, 70, 7, 86, 241, + 33, 87, 154, 251, 81, 29, 160, 4, 176, 239, 88, 211, 244, 232, 232, 52, + 211, 234, 100, 115, 230, 47, 80, 44, 152, 166, 62, 50, 8, 13, 86, 175, + 28, + ]; + #[rustfmt::skip] + let message_hash: [u8; 32] = [ + 162, 28, 244, 179, 96, 76, 244, 178, 188, 83, 230, 248, 143, 106, 77, 117, + 239, 95, 244, 171, 65, 95, 62, 153, 174, 166, 182, 28, 130, 73, 196, 208 + ]; + #[rustfmt::skip] + const EXPECTED_COMPRESSED_PUBLIC_KEY: [u8; 33] = [ + 2, 121, 190, 102, 126, 249, 220, 187, 172, 85, 160, 98, 149, 206, 135, 11, + 7, 2, 155, 252, 219, 45, 206, 40, 217, 89, 242, 129, 91, 22, 248, 23, + 152, + ]; + let mut params = vec![]; + params.extend_from_slice(&signature); + params.extend_from_slice(&message_hash); + assert!(params.len() == 65 + 32); + let result = builder::bare_call(addr.clone()).data(params).build_and_unwrap_result(); + assert!(!result.did_revert()); + assert_eq!(result.data, EXPECTED_COMPRESSED_PUBLIC_KEY); + }) +} + +#[test] +fn bare_instantiate_returns_events() { + let (wasm, _code_hash) = compile_module::("transfer_return_code").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + + let result = builder::bare_instantiate(Code::Upload(wasm)) + .value(min_balance * 100) + .collect_events(CollectEvents::UnsafeCollect) + .build(); + + let events = result.events.unwrap(); + assert!(!events.is_empty()); + assert_eq!(events, System::events()); + }); +} + +#[test] +fn bare_instantiate_does_not_return_events() { + let (wasm, _code_hash) = compile_module::("transfer_return_code").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + + let result = builder::bare_instantiate(Code::Upload(wasm)).value(min_balance * 100).build(); + + let events = result.events; + assert!(!System::events().is_empty()); + assert!(events.is_none()); + }); +} + +#[test] +fn bare_call_returns_events() { + let (wasm, _code_hash) = compile_module::("transfer_return_code").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + let result = builder::bare_call(addr.clone()) + .collect_events(CollectEvents::UnsafeCollect) + .build(); + + let events = result.events.unwrap(); + assert_return_code!(&result.result.unwrap(), RuntimeReturnCode::Success); + assert!(!events.is_empty()); + assert_eq!(events, System::events()); + }); +} + +#[test] +fn bare_call_does_not_return_events() { + let (wasm, _code_hash) = compile_module::("transfer_return_code").unwrap(); + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let min_balance = Contracts::min_balance(); + let _ = ::Currency::set_balance(&ALICE, 1000 * min_balance); + + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(min_balance * 100) + .build_and_unwrap_account_id(); + + let result = builder::bare_call(addr.clone()).build(); + + let events = result.events; + assert_return_code!(&result.result.unwrap(), RuntimeReturnCode::Success); + assert!(!System::events().is_empty()); + assert!(events.is_none()); + }); +} + +#[test] +fn sr25519_verify() { + let (wasm, _code_hash) = compile_module::("sr25519_verify").unwrap(); + + ExtBuilder::default().existential_deposit(50).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Instantiate the sr25519_verify contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(100_000) + .build_and_unwrap_account_id(); + + let call_with = |message: &[u8; 11]| { + // Alice's signature for "hello world" + #[rustfmt::skip] + let signature: [u8; 64] = [ + 184, 49, 74, 238, 78, 165, 102, 252, 22, 92, 156, 176, 124, 118, 168, 116, 247, + 99, 0, 94, 2, 45, 9, 170, 73, 222, 182, 74, 60, 32, 75, 64, 98, 174, 69, 55, 83, + 85, 180, 98, 208, 75, 231, 57, 205, 62, 4, 105, 26, 136, 172, 17, 123, 99, 90, 255, + 228, 54, 115, 63, 30, 207, 205, 131, + ]; + + // Alice's public key + #[rustfmt::skip] + let public_key: [u8; 32] = [ + 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, + 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, + ]; + + let mut params = vec![]; + params.extend_from_slice(&signature); + params.extend_from_slice(&public_key); + params.extend_from_slice(message); + + builder::bare_call(addr.clone()).data(params).build_and_unwrap_result() + }; + + // verification should succeed for "hello world" + assert_return_code!(call_with(&b"hello world"), RuntimeReturnCode::Success); + + // verification should fail for other messages + assert_return_code!(call_with(&b"hello worlD"), RuntimeReturnCode::Sr25519VerifyFailed); + }); +} + +#[test] +fn failed_deposit_charge_should_roll_back_call() { + let (wasm_caller, _) = compile_module::("call_runtime_and_call").unwrap(); + let (wasm_callee, _) = compile_module::("store_call").unwrap(); + const ED: u64 = 200; + + let execute = || { + ExtBuilder::default().existential_deposit(ED).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Instantiate both contracts. + let addr_caller = builder::bare_instantiate(Code::Upload(wasm_caller.clone())) + .build_and_unwrap_account_id(); + let addr_callee = builder::bare_instantiate(Code::Upload(wasm_callee.clone())) + .build_and_unwrap_account_id(); + + // Give caller proxy access to Alice. + assert_ok!(Proxy::add_proxy(RuntimeOrigin::signed(ALICE), addr_caller.clone(), (), 0)); + + // Create a Proxy call that will attempt to transfer away Alice's balance. + let transfer_call = + Box::new(RuntimeCall::Balances(pallet_balances::Call::transfer_allow_death { + dest: CHARLIE, + value: pallet_balances::Pallet::::free_balance(&ALICE) - 2 * ED, + })); + + // Wrap the transfer call in a proxy call. + let transfer_proxy_call = RuntimeCall::Proxy(pallet_proxy::Call::proxy { + real: ALICE, + force_proxy_type: Some(()), + call: transfer_call, + }); + + let data = ( + (ED - DepositPerItem::get()) as u32, // storage length + addr_callee, + transfer_proxy_call, + ); + + builder::call(addr_caller).data(data.encode()).build() + }) + }; + + // With a low enough deposit per byte, the call should succeed. + let result = execute().unwrap(); + + // Bump the deposit per byte to a high value to trigger a FundsUnavailable error. + DEPOSIT_PER_BYTE.with(|c| *c.borrow_mut() = 20); + assert_err_with_weight!(execute(), TokenError::FundsUnavailable, result.actual_weight); +} + +#[test] +fn upload_code_works() { + let (wasm, code_hash) = compile_module::("dummy").unwrap(); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Drop previous events + initialize_block(2); + + assert!(!PristineCode::::contains_key(&code_hash)); + + assert_ok!(Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm, + Some(codec::Compact(1_000)), + Determinism::Enforced, + )); + // Ensure the contract was stored and get expected deposit amount to be reserved. + let deposit_expected = expected_deposit(ensure_stored(code_hash)); + + assert_eq!( + System::events(), + vec![EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::CodeStored { + code_hash, + deposit_held: deposit_expected, + uploader: ALICE + }), + topics: vec![], + },] + ); + }); +} + +#[test] +fn upload_code_limit_too_low() { + let (wasm, _code_hash) = compile_module::("dummy").unwrap(); + let deposit_expected = expected_deposit(wasm.len()); + let deposit_insufficient = deposit_expected.saturating_sub(1); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Drop previous events + initialize_block(2); + + assert_noop!( + Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm, + Some(codec::Compact(deposit_insufficient)), + Determinism::Enforced + ), + >::StorageDepositLimitExhausted, + ); + + assert_eq!(System::events(), vec![]); + }); +} + +#[test] +fn upload_code_not_enough_balance() { + let (wasm, _code_hash) = compile_module::("dummy").unwrap(); + let deposit_expected = expected_deposit(wasm.len()); + let deposit_insufficient = deposit_expected.saturating_sub(1); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, deposit_insufficient); + + // Drop previous events + initialize_block(2); + + assert_noop!( + Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm, + Some(codec::Compact(1_000)), + Determinism::Enforced + ), + >::StorageDepositNotEnoughFunds, + ); + + assert_eq!(System::events(), vec![]); + }); +} + +#[test] +fn remove_code_works() { + let (wasm, code_hash) = compile_module::("dummy").unwrap(); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Drop previous events + initialize_block(2); + + assert_ok!(Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm, + Some(codec::Compact(1_000)), + Determinism::Enforced, + )); + // Ensure the contract was stored and get expected deposit amount to be reserved. + let deposit_expected = expected_deposit(ensure_stored(code_hash)); + + assert_ok!(Contracts::remove_code(RuntimeOrigin::signed(ALICE), code_hash)); + assert_eq!( + System::events(), + vec![ + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::CodeStored { + code_hash, + deposit_held: deposit_expected, + uploader: ALICE + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::CodeRemoved { + code_hash, + deposit_released: deposit_expected, + remover: ALICE + }), + topics: vec![], + }, + ] + ); + }); +} + +#[test] +fn remove_code_wrong_origin() { + let (wasm, code_hash) = compile_module::("dummy").unwrap(); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Drop previous events + initialize_block(2); + + assert_ok!(Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm, + Some(codec::Compact(1_000)), + Determinism::Enforced, + )); + // Ensure the contract was stored and get expected deposit amount to be reserved. + let deposit_expected = expected_deposit(ensure_stored(code_hash)); + + assert_noop!( + Contracts::remove_code(RuntimeOrigin::signed(BOB), code_hash), + sp_runtime::traits::BadOrigin, + ); + + assert_eq!( + System::events(), + vec![EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::CodeStored { + code_hash, + deposit_held: deposit_expected, + uploader: ALICE + }), + topics: vec![], + },] + ); + }); +} + +#[test] +fn remove_code_in_use() { + let (wasm, code_hash) = compile_module::("dummy").unwrap(); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + assert_ok!(builder::instantiate_with_code(wasm).build()); + + // Drop previous events + initialize_block(2); + + assert_noop!( + Contracts::remove_code(RuntimeOrigin::signed(ALICE), code_hash), + >::CodeInUse, + ); + + assert_eq!(System::events(), vec![]); + }); +} + +#[test] +fn remove_code_not_found() { + let (_wasm, code_hash) = compile_module::("dummy").unwrap(); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Drop previous events + initialize_block(2); + + assert_noop!( + Contracts::remove_code(RuntimeOrigin::signed(ALICE), code_hash), + >::CodeNotFound, + ); + + assert_eq!(System::events(), vec![]); + }); +} + +#[test] +fn instantiate_with_zero_balance_works() { + let (wasm, code_hash) = compile_module::("dummy").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let min_balance = Contracts::min_balance(); + + // Drop previous events + initialize_block(2); + + // Instantiate the BOB contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)).build_and_unwrap_account_id(); + + // Ensure the contract was stored and get expected deposit amount to be reserved. + let deposit_expected = expected_deposit(ensure_stored(code_hash)); + + // Make sure the account exists even though no free balance was send + assert_eq!(::Currency::free_balance(&addr), min_balance); + assert_eq!( + ::Currency::total_balance(&addr), + min_balance + test_utils::contract_info_storage_deposit(&addr) + ); + + assert_eq!( + System::events(), + vec![ + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::CodeStored { + code_hash, + deposit_held: deposit_expected, + uploader: ALICE + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::System(frame_system::Event::NewAccount { + account: addr.clone(), + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Endowed { + account: addr.clone(), + free_balance: min_balance, + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Transfer { + from: ALICE, + to: addr.clone(), + amount: min_balance, + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::Instantiated { + deployer: ALICE, + contract: addr.clone(), + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts( + pallet_contracts::Event::StorageDepositTransferredAndHeld { + from: ALICE, + to: addr.clone(), + amount: test_utils::contract_info_storage_deposit(&addr), + } + ), + topics: vec![], + }, + ] + ); + }); +} + +#[test] +fn instantiate_with_below_existential_deposit_works() { + let (wasm, code_hash) = compile_module::("dummy").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let min_balance = Contracts::min_balance(); + let value = 50; + + // Drop previous events + initialize_block(2); + + // Instantiate the BOB contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(value) + .build_and_unwrap_account_id(); + + // Ensure the contract was stored and get expected deposit amount to be reserved. + let deposit_expected = expected_deposit(ensure_stored(code_hash)); + // Make sure the account exists even though not enough free balance was send + assert_eq!(::Currency::free_balance(&addr), min_balance + value); + assert_eq!( + ::Currency::total_balance(&addr), + min_balance + value + test_utils::contract_info_storage_deposit(&addr) + ); + + assert_eq!( + System::events(), + vec![ + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::CodeStored { + code_hash, + deposit_held: deposit_expected, + uploader: ALICE + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::System(frame_system::Event::NewAccount { + account: addr.clone() + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Endowed { + account: addr.clone(), + free_balance: min_balance, + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Transfer { + from: ALICE, + to: addr.clone(), + amount: min_balance, + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Transfer { + from: ALICE, + to: addr.clone(), + amount: 50, + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::Instantiated { + deployer: ALICE, + contract: addr.clone(), + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts( + pallet_contracts::Event::StorageDepositTransferredAndHeld { + from: ALICE, + to: addr.clone(), + amount: test_utils::contract_info_storage_deposit(&addr), + } + ), + topics: vec![], + }, + ] + ); + }); +} + +#[test] +fn storage_deposit_works() { + let (wasm, _code_hash) = compile_module::("multi_store").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + let addr = builder::bare_instantiate(Code::Upload(wasm)).build_and_unwrap_account_id(); + + let mut deposit = test_utils::contract_info_storage_deposit(&addr); + + // Drop previous events + initialize_block(2); + + // Create storage + assert_ok!(builder::call(addr.clone()) + .value(42) + .data((1_000u32, 5_000u32).encode()) + .build()); + // 4 is for creating 2 storage items + let charged0 = 4 + 1_000 + 5_000; + deposit += charged0; + assert_eq!(get_contract(&addr).total_deposit(), deposit); + + // Add more storage (but also remove some) + assert_ok!(builder::call(addr.clone()).data((2_000u32, 4_900u32).encode()).build()); + let charged1 = 1_000 - 100; + deposit += charged1; + assert_eq!(get_contract(&addr).total_deposit(), deposit); + + // Remove more storage (but also add some) + assert_ok!(builder::call(addr.clone()).data((2_100u32, 900u32).encode()).build()); + // -1 for numeric instability + let refunded0 = 4_000 - 100 - 1; + deposit -= refunded0; + assert_eq!(get_contract(&addr).total_deposit(), deposit); + + assert_eq!( + System::events(), + vec![ + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Balances(pallet_balances::Event::Transfer { + from: ALICE, + to: addr.clone(), + amount: 42, + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::Called { + caller: Origin::from_account_id(ALICE), + contract: addr.clone(), + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts( + pallet_contracts::Event::StorageDepositTransferredAndHeld { + from: ALICE, + to: addr.clone(), + amount: charged0, + } + ), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::Called { + caller: Origin::from_account_id(ALICE), + contract: addr.clone(), + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts( + pallet_contracts::Event::StorageDepositTransferredAndHeld { + from: ALICE, + to: addr.clone(), + amount: charged1, + } + ), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::Called { + caller: Origin::from_account_id(ALICE), + contract: addr.clone(), + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts( + pallet_contracts::Event::StorageDepositTransferredAndReleased { + from: addr.clone(), + to: ALICE, + amount: refunded0, + } + ), + topics: vec![], + }, + ] + ); + }); +} + +#[test] +fn storage_deposit_callee_works() { + let (wasm_caller, _code_hash_caller) = compile_module::("call").unwrap(); + let (wasm_callee, _code_hash_callee) = compile_module::("store_call").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let min_balance = Contracts::min_balance(); + + // Create both contracts: Constructors do nothing. + let addr_caller = + builder::bare_instantiate(Code::Upload(wasm_caller)).build_and_unwrap_account_id(); + let addr_callee = + builder::bare_instantiate(Code::Upload(wasm_callee)).build_and_unwrap_account_id(); + + assert_ok!(builder::call(addr_caller).data((100u32, &addr_callee).encode()).build()); + + let callee = get_contract(&addr_callee); + let deposit = DepositPerByte::get() * 100 + DepositPerItem::get() * 1; + + assert_eq!(test_utils::get_balance(&addr_callee), min_balance); + assert_eq!( + callee.total_deposit(), + deposit + test_utils::contract_info_storage_deposit(&addr_callee) + ); + }); +} + +#[test] +fn set_code_extrinsic() { + let (wasm, code_hash) = compile_module::("dummy").unwrap(); + let (new_wasm, new_code_hash) = compile_module::("crypto_hashes").unwrap(); + + assert_ne!(code_hash, new_code_hash); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + let addr = builder::bare_instantiate(Code::Upload(wasm)).build_and_unwrap_account_id(); + + assert_ok!(Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + new_wasm, + None, + Determinism::Enforced + )); + + // Drop previous events + initialize_block(2); + + assert_eq!(get_contract(&addr).code_hash, code_hash); + assert_refcount!(&code_hash, 1); + assert_refcount!(&new_code_hash, 0); + + // only root can execute this extrinsic + assert_noop!( + Contracts::set_code(RuntimeOrigin::signed(ALICE), addr.clone(), new_code_hash), + sp_runtime::traits::BadOrigin, + ); + assert_eq!(get_contract(&addr).code_hash, code_hash); + assert_refcount!(&code_hash, 1); + assert_refcount!(&new_code_hash, 0); + assert_eq!(System::events(), vec![]); + + // contract must exist + assert_noop!( + Contracts::set_code(RuntimeOrigin::root(), BOB, new_code_hash), + >::ContractNotFound, + ); + assert_eq!(get_contract(&addr).code_hash, code_hash); + assert_refcount!(&code_hash, 1); + assert_refcount!(&new_code_hash, 0); + assert_eq!(System::events(), vec![]); + + // new code hash must exist + assert_noop!( + Contracts::set_code(RuntimeOrigin::root(), addr.clone(), Default::default()), + >::CodeNotFound, + ); + assert_eq!(get_contract(&addr).code_hash, code_hash); + assert_refcount!(&code_hash, 1); + assert_refcount!(&new_code_hash, 0); + assert_eq!(System::events(), vec![]); + + // successful call + assert_ok!(Contracts::set_code(RuntimeOrigin::root(), addr.clone(), new_code_hash)); + assert_eq!(get_contract(&addr).code_hash, new_code_hash); + assert_refcount!(&code_hash, 0); + assert_refcount!(&new_code_hash, 1); + assert_eq!( + System::events(), + vec![EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(pallet_contracts::Event::ContractCodeUpdated { + contract: addr.clone(), + new_code_hash, + old_code_hash: code_hash, + }), + topics: vec![], + },] + ); + }); +} + +#[test] +fn slash_cannot_kill_account() { + let (wasm, _code_hash) = compile_module::("dummy").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let value = 700; + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let min_balance = Contracts::min_balance(); + + let addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(value) + .build_and_unwrap_account_id(); + + // Drop previous events + initialize_block(2); + + let info_deposit = test_utils::contract_info_storage_deposit(&addr); + + assert_eq!( + test_utils::get_balance_on_hold(&HoldReason::StorageDepositReserve.into(), &addr), + info_deposit + ); + + assert_eq!( + ::Currency::total_balance(&addr), + info_deposit + value + min_balance + ); + + // Try to destroy the account of the contract by slashing the total balance. + // The account does not get destroyed because slashing only affects the balance held under + // certain `reason`. Slashing can for example happen if the contract takes part in staking. + let _ = ::Currency::slash( + &HoldReason::StorageDepositReserve.into(), + &addr, + ::Currency::total_balance(&addr), + ); + + // Slashing only removed the balance held. + assert_eq!(::Currency::total_balance(&addr), value + min_balance); + }); +} + +#[test] +fn contract_reverted() { + let (wasm, code_hash) = compile_module::("return_with_data").unwrap(); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let flags = ReturnFlags::REVERT; + let buffer = [4u8, 8, 15, 16, 23, 42]; + let input = (flags.bits(), buffer).encode(); + + // We just upload the code for later use + assert_ok!(Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm.clone(), + None, + Determinism::Enforced + )); + + // Calling extrinsic: revert leads to an error + assert_err_ignore_postinfo!( + builder::instantiate(code_hash).data(input.clone()).build(), + >::ContractReverted, + ); + + // Calling extrinsic: revert leads to an error + assert_err_ignore_postinfo!( + builder::instantiate_with_code(wasm).data(input.clone()).build(), + >::ContractReverted, + ); + + // Calling directly: revert leads to success but the flags indicate the error + // This is just a different way of transporting the error that allows the read out + // the `data` which is only there on success. Obviously, the contract isn't + // instantiated. + let result = builder::bare_instantiate(Code::Existing(code_hash)) + .data(input.clone()) + .build_and_unwrap_result(); + assert_eq!(result.result.flags, flags); + assert_eq!(result.result.data, buffer); + assert!(!>::contains_key(result.account_id)); + + // Pass empty flags and therefore successfully instantiate the contract for later use. + let addr = builder::bare_instantiate(Code::Existing(code_hash)) + .data(ReturnFlags::empty().bits().encode()) + .build_and_unwrap_account_id(); + + // Calling extrinsic: revert leads to an error + assert_err_ignore_postinfo!( + builder::call(addr.clone()).data(input.clone()).build(), + >::ContractReverted, + ); + + // Calling directly: revert leads to success but the flags indicate the error + let result = builder::bare_call(addr.clone()).data(input).build_and_unwrap_result(); + assert_eq!(result.flags, flags); + assert_eq!(result.data, buffer); + }); +} + +#[test] +fn set_code_hash() { + let (wasm, code_hash) = compile_module::("set_code_hash").unwrap(); + let (new_wasm, new_code_hash) = compile_module::("new_set_code_hash_contract").unwrap(); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Instantiate the 'caller' + let contract_addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(300_000) + .build_and_unwrap_account_id(); + // upload new code + assert_ok!(Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + new_wasm.clone(), + None, + Determinism::Enforced + )); + + System::reset_events(); + + // First call sets new code_hash and returns 1 + let result = builder::bare_call(contract_addr.clone()) + .data(new_code_hash.as_ref().to_vec()) + .debug(DebugInfo::UnsafeDebug) + .build_and_unwrap_result(); + assert_return_code!(result, 1); + + // Second calls new contract code that returns 2 + let result = builder::bare_call(contract_addr.clone()) + .debug(DebugInfo::UnsafeDebug) + .build_and_unwrap_result(); + assert_return_code!(result, 2); + + // Checking for the last event only + assert_eq!( + &System::events(), + &[ + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::ContractCodeUpdated { + contract: contract_addr.clone(), + new_code_hash, + old_code_hash: code_hash, + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::Called { + caller: Origin::from_account_id(ALICE), + contract: contract_addr.clone(), + }), + topics: vec![], + }, + EventRecord { + phase: Phase::Initialization, + event: RuntimeEvent::Contracts(crate::Event::Called { + caller: Origin::from_account_id(ALICE), + contract: contract_addr.clone(), + }), + topics: vec![], + }, + ], + ); + }); +} + +#[test] +fn storage_deposit_limit_is_enforced() { + let (wasm, _code_hash) = compile_module::("store_call").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let min_balance = Contracts::min_balance(); + + // Setting insufficient storage_deposit should fail. + assert_err!( + builder::bare_instantiate(Code::Upload(wasm.clone())) + // expected deposit is 2 * ed + 3 for the call + .storage_deposit_limit(Some((2 * min_balance + 3 - 1).into())) + .build() + .result, + >::StorageDepositLimitExhausted, + ); + + // Instantiate the BOB contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)).build_and_unwrap_account_id(); + + let info_deposit = test_utils::contract_info_storage_deposit(&addr); + // Check that the BOB contract has been instantiated and has the minimum balance + assert_eq!(get_contract(&addr).total_deposit(), info_deposit); + assert_eq!(::Currency::total_balance(&addr), info_deposit + min_balance); + + // Create 1 byte of storage with a price of per byte, + // setting insufficient deposit limit, as it requires 3 Balance: + // 2 for the item added + 1 for the new storage item. + assert_err_ignore_postinfo!( + builder::call(addr.clone()) + .storage_deposit_limit(Some(codec::Compact(2))) + .data(1u32.to_le_bytes().to_vec()) + .build(), + >::StorageDepositLimitExhausted, + ); + + // To check that deposit limit fallbacks to DefaultDepositLimit, + // we customize it here. + DEFAULT_DEPOSIT_LIMIT.with(|c| *c.borrow_mut() = 3); + + // Create 1 byte of storage, should cost 3 Balance: + // 2 for the item added + 1 for the new storage item. + // Should pass as it fallbacks to DefaultDepositLimit. + assert_ok!(builder::call(addr.clone()).data(1u32.to_le_bytes().to_vec()).build()); + + // Use 4 more bytes of the storage for the same item, which requires 4 Balance. + // Should fail as DefaultDepositLimit is 3 and hence isn't enough. + assert_err_ignore_postinfo!( + builder::call(addr.clone()).data(5u32.to_le_bytes().to_vec()).build(), + >::StorageDepositLimitExhausted, + ); + }); +} + +#[test] +fn deposit_limit_in_nested_calls() { + let (wasm_caller, _code_hash_caller) = + compile_module::("create_storage_and_call").unwrap(); + let (wasm_callee, _code_hash_callee) = compile_module::("store_call").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Create both contracts: Constructors do nothing. + let addr_caller = + builder::bare_instantiate(Code::Upload(wasm_caller)).build_and_unwrap_account_id(); + let addr_callee = + builder::bare_instantiate(Code::Upload(wasm_callee)).build_and_unwrap_account_id(); + + // Create 100 bytes of storage with a price of per byte + // This is 100 Balance + 2 Balance for the item + assert_ok!(builder::call(addr_callee.clone()) + .storage_deposit_limit(Some(codec::Compact(102))) + .data(100u32.to_le_bytes().to_vec()) + .build()); + + // We do not remove any storage but add a storage item of 12 bytes in the caller + // contract. This would cost 12 + 2 = 14 Balance. + // The nested call doesn't get a special limit, which is set by passing 0 to it. + // This should fail as the specified parent's limit is less than the cost: 13 < + // 14. + assert_err_ignore_postinfo!( + builder::call(addr_caller.clone()) + .storage_deposit_limit(Some(codec::Compact(13))) + .data((100u32, &addr_callee, 0u64).encode()) + .build(), + >::StorageDepositLimitExhausted, + ); + // Now we specify the parent's limit high enough to cover the caller's storage additions. + // However, we use a single byte more in the callee, hence the storage deposit should be 15 + // Balance. + // The nested call doesn't get a special limit, which is set by passing 0 to it. + // This should fail as the specified parent's limit is less than the cost: 14 + // < 15. + assert_err_ignore_postinfo!( + builder::call(addr_caller.clone()) + .storage_deposit_limit(Some(codec::Compact(14))) + .data((101u32, &addr_callee, 0u64).encode()) + .build(), + >::StorageDepositLimitExhausted, + ); + + // Now we specify the parent's limit high enough to cover both the caller's and callee's + // storage additions. However, we set a special deposit limit of 1 Balance for the nested + // call. This should fail as callee adds up 2 bytes to the storage, meaning that the nested + // call should have a deposit limit of at least 2 Balance. The sub-call should be rolled + // back, which is covered by the next test case. + assert_err_ignore_postinfo!( + builder::call(addr_caller.clone()) + .storage_deposit_limit(Some(codec::Compact(16))) + .data((102u32, &addr_callee, 1u64).encode()) + .build(), + >::StorageDepositLimitExhausted, + ); + + // Refund in the callee contract but not enough to cover the 14 Balance required by the + // caller. Note that if previous sub-call wouldn't roll back, this call would pass making + // the test case fail. We don't set a special limit for the nested call here. + assert_err_ignore_postinfo!( + builder::call(addr_caller.clone()) + .storage_deposit_limit(Some(codec::Compact(0))) + .data((87u32, &addr_callee, 0u64).encode()) + .build(), + >::StorageDepositLimitExhausted, + ); + + let _ = ::Currency::set_balance(&ALICE, 1_000); + + // Require more than the sender's balance. + // We don't set a special limit for the nested call. + assert_err_ignore_postinfo!( + builder::call(addr_caller.clone()) + .data((1200u32, &addr_callee, 1u64).encode()) + .build(), + >::StorageDepositLimitExhausted, + ); + + // Same as above but allow for the additional deposit of 1 Balance in parent. + // We set the special deposit limit of 1 Balance for the nested call, which isn't + // enforced as callee frees up storage. This should pass. + assert_ok!(builder::call(addr_caller.clone()) + .storage_deposit_limit(Some(codec::Compact(1))) + .data((87u32, &addr_callee, 1u64).encode()) + .build()); + }); +} + +#[test] +fn deposit_limit_in_nested_instantiate() { + let (wasm_caller, _code_hash_caller) = + compile_module::("create_storage_and_instantiate").unwrap(); + let (wasm_callee, code_hash_callee) = compile_module::("store_deploy").unwrap(); + const ED: u64 = 5; + ExtBuilder::default().existential_deposit(ED).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let _ = ::Currency::set_balance(&BOB, 1_000_000); + // Create caller contract + let addr_caller = builder::bare_instantiate(Code::Upload(wasm_caller)) + .value(10_000u64) // this balance is later passed to the deployed contract + .build_and_unwrap_account_id(); + // Deploy a contract to get its occupied storage size + let addr = builder::bare_instantiate(Code::Upload(wasm_callee)) + .data(vec![0, 0, 0, 0]) + .build_and_unwrap_account_id(); + + let callee_info_len = ContractInfoOf::::get(&addr).unwrap().encoded_size() as u64; + + // We don't set a special deposit limit for the nested instantiation. + // + // The deposit limit set for the parent is insufficient for the instantiation, which + // requires: + // - callee_info_len + 2 for storing the new contract info, + // - ED for deployed contract account, + // - 2 for the storage item of 0 bytes being created in the callee constructor + // or (callee_info_len + 2 + ED + 2) Balance in total. + // + // Provided the limit is set to be 1 Balance less, + // this call should fail on the return from the caller contract. + assert_err_ignore_postinfo!( + builder::call(addr_caller.clone()) + .origin(RuntimeOrigin::signed(BOB)) + .storage_deposit_limit(Some(codec::Compact(callee_info_len + 2 + ED + 1))) + .data((0u32, &code_hash_callee, 0u64).encode()) + .build(), + >::StorageDepositLimitExhausted, + ); + // The charges made on instantiation should be rolled back. + assert_eq!(::Currency::free_balance(&BOB), 1_000_000); + + // Now we give enough limit for the instantiation itself, but require for 1 more storage + // byte in the constructor. Hence +1 Balance to the limit is needed. This should fail on the + // return from constructor. + assert_err_ignore_postinfo!( + builder::call(addr_caller.clone()) + .origin(RuntimeOrigin::signed(BOB)) + .storage_deposit_limit(Some(codec::Compact(callee_info_len + 2 + ED + 2))) + .data((1u32, &code_hash_callee, 0u64).encode()) + .build(), + >::StorageDepositLimitExhausted, + ); + // The charges made on the instantiation should be rolled back. + assert_eq!(::Currency::free_balance(&BOB), 1_000_000); + + // Now we set enough limit in parent call, but an insufficient limit for child instantiate. + // This should fail during the charging for the instantiation in + // `RawMeter::charge_instantiate()` + assert_err_ignore_postinfo!( + builder::call(addr_caller.clone()) + .origin(RuntimeOrigin::signed(BOB)) + .storage_deposit_limit(Some(codec::Compact(callee_info_len + 2 + ED + 2))) + .data((0u32, &code_hash_callee, callee_info_len + 2 + ED + 1).encode()) + .build(), + >::StorageDepositLimitExhausted, + ); + // The charges made on the instantiation should be rolled back. + assert_eq!(::Currency::free_balance(&BOB), 1_000_000); + + // Same as above but requires for single added storage + // item of 1 byte to be covered by the limit, which implies 3 more Balance. + // Now we set enough limit for the parent call, but insufficient limit for child + // instantiate. This should fail right after the constructor execution. + assert_err_ignore_postinfo!( + builder::call(addr_caller.clone()) + .origin(RuntimeOrigin::signed(BOB)) + .storage_deposit_limit(Some(codec::Compact(callee_info_len + 2 + ED + 3))) // enough parent limit + .data((1u32, &code_hash_callee, callee_info_len + 2 + ED + 2).encode()) + .build(), + >::StorageDepositLimitExhausted, + ); + // The charges made on the instantiation should be rolled back. + assert_eq!(::Currency::free_balance(&BOB), 1_000_000); + + // Set enough deposit limit for the child instantiate. This should succeed. + let result = builder::bare_call(addr_caller.clone()) + .origin(BOB) + .storage_deposit_limit(Some(codec::Compact(callee_info_len + 2 + ED + 4).into())) + .data((1u32, &code_hash_callee, callee_info_len + 2 + ED + 3).encode()) + .build(); + + let returned = result.result.unwrap(); + // All balance of the caller except ED has been transferred to the callee. + // No deposit has been taken from it. + assert_eq!(::Currency::free_balance(&addr_caller), ED); + // Get address of the deployed contract. + let addr_callee = AccountId32::from_slice(&returned.data[0..32]).unwrap(); + // 10_000 should be sent to callee from the caller contract, plus ED to be sent from the + // origin. + assert_eq!(::Currency::free_balance(&addr_callee), 10_000 + ED); + // The origin should be charged with: + // - callee instantiation deposit = (callee_info_len + 2) + // - callee account ED + // - for writing an item of 1 byte to storage = 3 Balance + assert_eq!( + ::Currency::free_balance(&BOB), + 1_000_000 - (callee_info_len + 2 + ED + 3) + ); + // Check that deposit due to be charged still includes these 3 Balance + assert_eq!(result.storage_deposit.charge_or_zero(), (callee_info_len + 2 + ED + 3)) + }); +} + +#[test] +fn deposit_limit_honors_liquidity_restrictions() { + let (wasm, _code_hash) = compile_module::("store_call").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let bobs_balance = 1_000; + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let _ = ::Currency::set_balance(&BOB, bobs_balance); + let min_balance = Contracts::min_balance(); + + // Instantiate the BOB contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)).build_and_unwrap_account_id(); + + let info_deposit = test_utils::contract_info_storage_deposit(&addr); + // Check that the contract has been instantiated and has the minimum balance + assert_eq!(get_contract(&addr).total_deposit(), info_deposit); + assert_eq!(::Currency::total_balance(&addr), info_deposit + min_balance); + + // check that the hold is honored + ::Currency::hold( + &HoldReason::CodeUploadDepositReserve.into(), + &BOB, + bobs_balance - min_balance, + ) + .unwrap(); + assert_err_ignore_postinfo!( + builder::call(addr.clone()) + .origin(RuntimeOrigin::signed(BOB)) + .storage_deposit_limit(Some(codec::Compact(200))) + .data(100u32.to_le_bytes().to_vec()) + .build(), + >::StorageDepositNotEnoughFunds, + ); + assert_eq!(::Currency::free_balance(&BOB), min_balance); + }); +} + +#[test] +fn deposit_limit_honors_existential_deposit() { + let (wasm, _code_hash) = compile_module::("store_call").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let _ = ::Currency::set_balance(&BOB, 1_000); + let min_balance = Contracts::min_balance(); + + // Instantiate the BOB contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)).build_and_unwrap_account_id(); + + let info_deposit = test_utils::contract_info_storage_deposit(&addr); + + // Check that the contract has been instantiated and has the minimum balance + assert_eq!(get_contract(&addr).total_deposit(), info_deposit); + assert_eq!(::Currency::total_balance(&addr), min_balance + info_deposit); + + // check that the deposit can't bring the account below the existential deposit + assert_err_ignore_postinfo!( + builder::call(addr.clone()) + .origin(RuntimeOrigin::signed(BOB)) + .storage_deposit_limit(Some(codec::Compact(900))) + .data(100u32.to_le_bytes().to_vec()) + .build(), + >::StorageDepositNotEnoughFunds, + ); + assert_eq!(::Currency::free_balance(&BOB), 1_000); + }); +} + +#[test] +fn deposit_limit_honors_min_leftover() { + let (wasm, _code_hash) = compile_module::("store_call").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + let _ = ::Currency::set_balance(&BOB, 1_000); + let min_balance = Contracts::min_balance(); + + // Instantiate the BOB contract. + let addr = builder::bare_instantiate(Code::Upload(wasm)).build_and_unwrap_account_id(); + + let info_deposit = test_utils::contract_info_storage_deposit(&addr); + + // Check that the contract has been instantiated and has the minimum balance and the storage + // deposit + assert_eq!(get_contract(&addr).total_deposit(), info_deposit); + assert_eq!(::Currency::total_balance(&addr), info_deposit + min_balance); + + // check that the minimum leftover (value send) is considered + assert_err_ignore_postinfo!( + builder::call(addr.clone()) + .origin(RuntimeOrigin::signed(BOB)) + .value(400) + .storage_deposit_limit(Some(codec::Compact(500))) + .data(100u32.to_le_bytes().to_vec()) + .build(), + >::StorageDepositNotEnoughFunds, + ); + assert_eq!(::Currency::free_balance(&BOB), 1_000); + }); +} + +#[test] +fn upload_should_enforce_deterministic_mode_when_possible() { + let upload = |fixture, determinism| { + let (wasm, code_hash) = compile_module::(fixture).unwrap(); + ExtBuilder::default() + .build() + .execute_with(|| -> Result { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + Contracts::bare_upload_code(ALICE, wasm, None, determinism)?; + let info = CodeInfoOf::::get(code_hash).unwrap(); + Ok(info.determinism()) + }) + }; + + assert_eq!(upload("dummy", Determinism::Enforced), Ok(Determinism::Enforced)); + assert_eq!(upload("dummy", Determinism::Relaxed), Ok(Determinism::Enforced)); + assert_eq!(upload("float_instruction", Determinism::Relaxed), Ok(Determinism::Relaxed)); + assert!(upload("float_instruction", Determinism::Enforced).is_err()); +} + +#[test] +fn cannot_instantiate_indeterministic_code() { + let (wasm, code_hash) = compile_module::("float_instruction").unwrap(); + let (caller_wasm, _) = compile_module::("instantiate_return_code").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Try to instantiate directly from code + assert_err_ignore_postinfo!( + builder::instantiate_with_code(wasm.clone()).build(), + >::CodeRejected, + ); + assert_err!( + builder::bare_instantiate(Code::Upload(wasm.clone())).build().result, + >::CodeRejected, + ); + + // Try to upload a non-deterministic code as deterministic + assert_err!( + Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm.clone(), + None, + Determinism::Enforced + ), + >::CodeRejected, + ); + + // Try to instantiate from already stored indeterministic code hash + assert_ok!(Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm, + None, + Determinism::Relaxed, + )); + + assert_err_ignore_postinfo!( + builder::instantiate(code_hash).build(), + >::Indeterministic, + ); + assert_err!( + builder::bare_instantiate(Code::Existing(code_hash)).build().result, + >::Indeterministic, + ); + + // Deploy contract which instantiates another contract + let addr = + builder::bare_instantiate(Code::Upload(caller_wasm)).build_and_unwrap_account_id(); + + // Try to instantiate `code_hash` from another contract in deterministic mode + assert_err!( + builder::bare_call(addr.clone()).data(code_hash.encode()).build().result, + >::Indeterministic, + ); + + // Instantiations are not allowed even in non-determinism mode + assert_err!( + builder::bare_call(addr.clone()).data(code_hash.encode()).build().result, + >::Indeterministic, + ); + }); +} + +#[test] +fn cannot_set_code_indeterministic_code() { + let (wasm, code_hash) = compile_module::("float_instruction").unwrap(); + let (caller_wasm, _) = compile_module::("set_code_hash").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Put the non-deterministic contract on-chain + assert_ok!(Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm, + None, + Determinism::Relaxed, + )); + + // Create the contract that will call `seal_set_code_hash` + let caller_addr = + builder::bare_instantiate(Code::Upload(caller_wasm)).build_and_unwrap_account_id(); + + // We do not allow to set the code hash to a non-deterministic wasm + assert_err!( + builder::bare_call(caller_addr.clone()).data(code_hash.encode()).build().result, + >::Indeterministic, + ); + }); +} + +#[test] +fn delegate_call_indeterministic_code() { + let (wasm, code_hash) = compile_module::("float_instruction").unwrap(); + let (caller_wasm, _) = compile_module::("delegate_call_simple").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Put the non-deterministic contract on-chain + assert_ok!(Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm, + None, + Determinism::Relaxed, + )); + + // Create the contract that will call `seal_delegate_call` + let caller_addr = + builder::bare_instantiate(Code::Upload(caller_wasm)).build_and_unwrap_account_id(); + + // The delegate call will fail in deterministic mode + assert_err!( + builder::bare_call(caller_addr.clone()).data(code_hash.encode()).build().result, + >::Indeterministic, + ); + + // The delegate call will work on non-deterministic mode + assert_ok!( + builder::bare_call(caller_addr.clone()) + .data(code_hash.encode()) + .determinism(Determinism::Relaxed) + .build() + .result + ); + }); +} + +#[test] +fn locking_delegate_dependency_works() { + // set hash lock up deposit to 30%, to test deposit calculation. + CODE_HASH_LOCKUP_DEPOSIT_PERCENT.with(|c| *c.borrow_mut() = Perbill::from_percent(30)); + MAX_DELEGATE_DEPENDENCIES.with(|c| *c.borrow_mut() = 1); + + let (wasm_caller, self_code_hash) = + compile_module::("locking_delegate_dependency").unwrap(); + let (wasm_callee, code_hash) = compile_module::("dummy").unwrap(); + let (wasm_other, other_code_hash) = compile_module::("call").unwrap(); + + // Define inputs with various actions to test locking / unlocking delegate_dependencies. + // See the contract for more details. + let noop_input = (0u32, code_hash); + let lock_delegate_dependency_input = (1u32, code_hash); + let unlock_delegate_dependency_input = (2u32, code_hash); + let terminate_input = (3u32, code_hash); + + // Instantiate the caller contract with the given input. + let instantiate = |input: &(u32, H256)| { + builder::bare_instantiate(Code::Upload(wasm_caller.clone())) + .data(input.encode()) + .build() + }; + + // Call contract with the given input. + let call = |addr_caller: &AccountId32, input: &(u32, H256)| { + builder::bare_call(addr_caller.clone()).data(input.encode()).build() + }; + const ED: u64 = 2000; + ExtBuilder::default().existential_deposit(ED).build().execute_with(|| { + let _ = Balances::set_balance(&ALICE, 1_000_000); + + // Instantiate with lock_delegate_dependency should fail since the code is not yet on chain. + assert_err!( + instantiate(&lock_delegate_dependency_input).result, + Error::::CodeNotFound + ); + + // Upload the delegated code. + let CodeUploadReturnValue { deposit, .. } = + Contracts::bare_upload_code(ALICE, wasm_callee.clone(), None, Determinism::Enforced) + .unwrap(); + + // Instantiate should now work. + let addr_caller = instantiate(&lock_delegate_dependency_input).result.unwrap().account_id; + + // There should be a dependency and a deposit. + let contract = test_utils::get_contract(&addr_caller); + + let dependency_deposit = &CodeHashLockupDepositPercent::get().mul_ceil(deposit); + assert_eq!(contract.delegate_dependencies().get(&code_hash), Some(dependency_deposit)); + assert_eq!( + test_utils::get_balance_on_hold( + &HoldReason::StorageDepositReserve.into(), + &addr_caller + ), + dependency_deposit + contract.storage_base_deposit() + ); + + // Removing the code should fail, since we have added a dependency. + assert_err!( + Contracts::remove_code(RuntimeOrigin::signed(ALICE), code_hash), + >::CodeInUse + ); + + // Locking an already existing dependency should fail. + assert_err!( + call(&addr_caller, &lock_delegate_dependency_input).result, + Error::::DelegateDependencyAlreadyExists + ); + + // Locking self should fail. + assert_err!( + call(&addr_caller, &(1u32, self_code_hash)).result, + Error::::CannotAddSelfAsDelegateDependency + ); + + // Locking more than the maximum allowed delegate_dependencies should fail. + Contracts::bare_upload_code(ALICE, wasm_other, None, Determinism::Enforced).unwrap(); + assert_err!( + call(&addr_caller, &(1u32, other_code_hash)).result, + Error::::MaxDelegateDependenciesReached + ); + + // Unlocking dependency should work. + assert_ok!(call(&addr_caller, &unlock_delegate_dependency_input).result); + + // Dependency should be removed, and deposit should be returned. + let contract = test_utils::get_contract(&addr_caller); + assert!(contract.delegate_dependencies().is_empty()); + assert_eq!( + test_utils::get_balance_on_hold( + &HoldReason::StorageDepositReserve.into(), + &addr_caller + ), + contract.storage_base_deposit() + ); + + // Removing a nonexistent dependency should fail. + assert_err!( + call(&addr_caller, &unlock_delegate_dependency_input).result, + Error::::DelegateDependencyNotFound + ); + + // Locking a dependency with a storage limit too low should fail. + DEFAULT_DEPOSIT_LIMIT.with(|c| *c.borrow_mut() = dependency_deposit - 1); + assert_err!( + call(&addr_caller, &lock_delegate_dependency_input).result, + Error::::StorageDepositLimitExhausted + ); + + // Since we unlocked the dependency we should now be able to remove the code. + assert_ok!(Contracts::remove_code(RuntimeOrigin::signed(ALICE), code_hash)); + + // Calling should fail since the delegated contract is not on chain anymore. + assert_err!(call(&addr_caller, &noop_input).result, Error::::ContractTrapped); + + // Restore initial deposit limit and add the dependency back. + DEFAULT_DEPOSIT_LIMIT.with(|c| *c.borrow_mut() = 10_000_000); + Contracts::bare_upload_code(ALICE, wasm_callee, None, Determinism::Enforced).unwrap(); + call(&addr_caller, &lock_delegate_dependency_input).result.unwrap(); + + // Call terminate should work, and return the deposit. + let balance_before = test_utils::get_balance(&ALICE); + assert_ok!(call(&addr_caller, &terminate_input).result); + assert_eq!( + test_utils::get_balance(&ALICE), + ED + balance_before + contract.storage_base_deposit() + dependency_deposit + ); + + // Terminate should also remove the dependency, so we can remove the code. + assert_ok!(Contracts::remove_code(RuntimeOrigin::signed(ALICE), code_hash)); + }); +} + +#[test] +fn native_dependency_deposit_works() { + let (wasm, code_hash) = compile_module::("set_code_hash").unwrap(); + let (dummy_wasm, dummy_code_hash) = compile_module::("dummy").unwrap(); + + // Set hash lock up deposit to 30%, to test deposit calculation. + CODE_HASH_LOCKUP_DEPOSIT_PERCENT.with(|c| *c.borrow_mut() = Perbill::from_percent(30)); + + // Test with both existing and uploaded code + for code in [Code::Upload(wasm.clone()), Code::Existing(code_hash)] { + ExtBuilder::default().build().execute_with(|| { + let _ = Balances::set_balance(&ALICE, 1_000_000); + let lockup_deposit_percent = CodeHashLockupDepositPercent::get(); + + // Upload the dummy contract, + Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + dummy_wasm.clone(), + None, + Determinism::Enforced, + ) + .unwrap(); + + // Upload `set_code_hash` contracts if using Code::Existing. + let add_upload_deposit = match code { + Code::Existing(_) => { + Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm.clone(), + None, + Determinism::Enforced, + ) + .unwrap(); + false + }, + Code::Upload(_) => true, + }; + + // Instantiate the set_code_hash contract. + let res = builder::bare_instantiate(code).build(); + + let addr = res.result.unwrap().account_id; + let base_deposit = test_utils::contract_info_storage_deposit(&addr); + let upload_deposit = test_utils::get_code_deposit(&code_hash); + let extra_deposit = add_upload_deposit.then(|| upload_deposit).unwrap_or_default(); + + // Check initial storage_deposit + // The base deposit should be: contract_info_storage_deposit + 30% * deposit + let deposit = + extra_deposit + base_deposit + lockup_deposit_percent.mul_ceil(upload_deposit); + + assert_eq!(res.storage_deposit.charge_or_zero(), deposit + Contracts::min_balance()); + + // call set_code_hash + builder::bare_call(addr.clone()) + .data(dummy_code_hash.encode()) + .build_and_unwrap_result(); + + // Check updated storage_deposit + let code_deposit = test_utils::get_code_deposit(&dummy_code_hash); + let deposit = base_deposit + lockup_deposit_percent.mul_ceil(code_deposit); + assert_eq!(test_utils::get_contract(&addr).storage_base_deposit(), deposit); + + assert_eq!( + test_utils::get_balance_on_hold(&HoldReason::StorageDepositReserve.into(), &addr), + deposit + ); + }); + } +} + +#[test] +fn reentrance_count_works_with_call() { + let (wasm, _code_hash) = compile_module::("reentrance_count_call").unwrap(); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + let contract_addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(300_000) + .build_and_unwrap_account_id(); + + // passing reentrant count to the input + let input = 0.encode(); + + builder::bare_call(contract_addr) + .data(input) + .debug(DebugInfo::UnsafeDebug) + .build_and_unwrap_result(); + }); +} + +#[test] +fn reentrance_count_works_with_delegated_call() { + let (wasm, code_hash) = compile_module::("reentrance_count_delegated_call").unwrap(); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + let contract_addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(300_000) + .build_and_unwrap_account_id(); + + // adding a callstack height to the input + let input = (code_hash, 1).encode(); + + builder::bare_call(contract_addr.clone()) + .data(input) + .debug(DebugInfo::UnsafeDebug) + .build_and_unwrap_result(); + }); +} + +#[test] +fn account_reentrance_count_works() { + let (wasm, _code_hash) = compile_module::("account_reentrance_count_call").unwrap(); + let (wasm_reentrance_count, _code_hash_reentrance_count) = + compile_module::("reentrance_count_call").unwrap(); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + let contract_addr = builder::bare_instantiate(Code::Upload(wasm)) + .value(300_000) + .build_and_unwrap_account_id(); + + let another_contract_addr = builder::bare_instantiate(Code::Upload(wasm_reentrance_count)) + .value(300_000) + .build_and_unwrap_account_id(); + + let result1 = builder::bare_call(contract_addr.clone()) + .data(contract_addr.encode()) + .debug(DebugInfo::UnsafeDebug) + .build_and_unwrap_result(); + + let result2 = builder::bare_call(contract_addr.clone()) + .data(another_contract_addr.encode()) + .debug(DebugInfo::UnsafeDebug) + .build_and_unwrap_result(); + + assert_eq!(result1.data, 1.encode()); + assert_eq!(result2.data, 0.encode()); + }); +} + +#[test] +fn root_cannot_upload_code() { + let (wasm, _) = compile_module::("dummy").unwrap(); + + ExtBuilder::default().build().execute_with(|| { + assert_noop!( + Contracts::upload_code(RuntimeOrigin::root(), wasm, None, Determinism::Enforced), + DispatchError::BadOrigin, + ); + }); +} + +#[test] +fn root_cannot_remove_code() { + let (_, code_hash) = compile_module::("dummy").unwrap(); + + ExtBuilder::default().build().execute_with(|| { + assert_noop!( + Contracts::remove_code(RuntimeOrigin::root(), code_hash), + DispatchError::BadOrigin, + ); + }); +} + +#[test] +fn signed_cannot_set_code() { + let (_, code_hash) = compile_module::("dummy").unwrap(); + + ExtBuilder::default().build().execute_with(|| { + assert_noop!( + Contracts::set_code(RuntimeOrigin::signed(ALICE), BOB, code_hash), + DispatchError::BadOrigin, + ); + }); +} + +#[test] +fn none_cannot_call_code() { + ExtBuilder::default().build().execute_with(|| { + assert_noop!( + builder::call(BOB).origin(RuntimeOrigin::none()).build(), + DispatchError::BadOrigin, + ); + }); +} + +#[test] +fn root_can_call() { + let (wasm, _) = compile_module::("dummy").unwrap(); + + ExtBuilder::default().existential_deposit(100).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + let addr = builder::bare_instantiate(Code::Upload(wasm)).build_and_unwrap_account_id(); + + // Call the contract. + assert_ok!(builder::call(addr.clone()).origin(RuntimeOrigin::root()).build()); + }); +} + +#[test] +fn root_cannot_instantiate_with_code() { + let (wasm, _) = compile_module::("dummy").unwrap(); + + ExtBuilder::default().build().execute_with(|| { + assert_err_ignore_postinfo!( + builder::instantiate_with_code(wasm).origin(RuntimeOrigin::root()).build(), + DispatchError::BadOrigin + ); + }); +} + +#[test] +fn root_cannot_instantiate() { + let (_, code_hash) = compile_module::("dummy").unwrap(); + + ExtBuilder::default().build().execute_with(|| { + assert_err_ignore_postinfo!( + builder::instantiate(code_hash).origin(RuntimeOrigin::root()).build(), + DispatchError::BadOrigin + ); + }); +} + +#[test] +fn only_upload_origin_can_upload() { + let (wasm, _) = compile_module::("dummy").unwrap(); + UploadAccount::set(Some(ALICE)); + ExtBuilder::default().build().execute_with(|| { + let _ = Balances::set_balance(&ALICE, 1_000_000); + let _ = Balances::set_balance(&BOB, 1_000_000); + + assert_err!( + Contracts::upload_code( + RuntimeOrigin::root(), + wasm.clone(), + None, + Determinism::Enforced, + ), + DispatchError::BadOrigin + ); + + assert_err!( + Contracts::upload_code( + RuntimeOrigin::signed(BOB), + wasm.clone(), + None, + Determinism::Enforced, + ), + DispatchError::BadOrigin + ); + + // Only alice is allowed to upload contract code. + assert_ok!(Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm.clone(), + None, + Determinism::Enforced, + )); + }); +} + +#[test] +fn only_instantiation_origin_can_instantiate() { + let (code, code_hash) = compile_module::("dummy").unwrap(); + InstantiateAccount::set(Some(ALICE)); + ExtBuilder::default().build().execute_with(|| { + let _ = Balances::set_balance(&ALICE, 1_000_000); + let _ = Balances::set_balance(&BOB, 1_000_000); + + assert_err_ignore_postinfo!( + builder::instantiate_with_code(code.clone()) + .origin(RuntimeOrigin::root()) + .build(), + DispatchError::BadOrigin + ); + + assert_err_ignore_postinfo!( + builder::instantiate_with_code(code.clone()) + .origin(RuntimeOrigin::signed(BOB)) + .build(), + DispatchError::BadOrigin + ); + + // Only Alice can instantiate + assert_ok!(builder::instantiate_with_code(code).build()); + + // Bob cannot instantiate with either `instantiate_with_code` or `instantiate`. + assert_err_ignore_postinfo!( + builder::instantiate(code_hash).origin(RuntimeOrigin::signed(BOB)).build(), + DispatchError::BadOrigin + ); + }); +} + +#[test] +fn balance_api_returns_free_balance() { + let (wasm, _code_hash) = compile_module::("balance").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Instantiate the BOB contract without any extra balance. + let addr = + builder::bare_instantiate(Code::Upload(wasm.to_vec())).build_and_unwrap_account_id(); + + let value = 0; + // Call BOB which makes it call the balance runtime API. + // The contract code asserts that the returned balance is 0. + assert_ok!(builder::call(addr.clone()).value(value).build()); + + let value = 1; + // Calling with value will trap the contract. + assert_err_ignore_postinfo!( + builder::call(addr.clone()).value(value).build(), + >::ContractTrapped + ); + }); +} + +#[test] +fn gas_consumed_is_linear_for_nested_calls() { + let (code, _code_hash) = compile_module::("recurse").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + let addr = builder::bare_instantiate(Code::Upload(code)).build_and_unwrap_account_id(); + + let max_call_depth = ::CallStack::size() as u32; + let [gas_0, gas_1, gas_2, gas_max] = { + [0u32, 1u32, 2u32, max_call_depth] + .iter() + .map(|i| { + let result = builder::bare_call(addr.clone()).data(i.encode()).build(); + assert_ok!(result.result); + result.gas_consumed + }) + .collect::>() + .try_into() + .unwrap() + }; + + let gas_per_recursion = gas_2.checked_sub(&gas_1).unwrap(); + assert_eq!(gas_max, gas_0 + gas_per_recursion * max_call_depth as u64); + }); +} + +#[test] +fn read_only_call_cannot_store() { + let (wasm_caller, _code_hash_caller) = compile_module::("read_only_call").unwrap(); + let (wasm_callee, _code_hash_callee) = compile_module::("store_call").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Create both contracts: Constructors do nothing. + let addr_caller = + builder::bare_instantiate(Code::Upload(wasm_caller)).build_and_unwrap_account_id(); + let addr_callee = + builder::bare_instantiate(Code::Upload(wasm_callee)).build_and_unwrap_account_id(); + + // Read-only call fails when modifying storage. + assert_err_ignore_postinfo!( + builder::call(addr_caller).data((&addr_callee, 100u32).encode()).build(), + >::ContractTrapped + ); + }); +} + +#[test] +fn read_only_call_cannot_transfer() { + let (wasm_caller, _code_hash_caller) = + compile_module::("call_with_flags_and_value").unwrap(); + let (wasm_callee, _code_hash_callee) = compile_module::("dummy").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Create both contracts: Constructors do nothing. + let addr_caller = + builder::bare_instantiate(Code::Upload(wasm_caller)).build_and_unwrap_account_id(); + let addr_callee = + builder::bare_instantiate(Code::Upload(wasm_callee)).build_and_unwrap_account_id(); + + // Read-only call fails when a non-zero value is set. + assert_err_ignore_postinfo!( + builder::call(addr_caller) + .data( + (addr_callee, pallet_contracts_uapi::CallFlags::READ_ONLY.bits(), 100u64) + .encode() + ) + .build(), + >::StateChangeDenied + ); + }); +} + +#[test] +fn read_only_subsequent_call_cannot_store() { + let (wasm_read_only_caller, _code_hash_caller) = + compile_module::("read_only_call").unwrap(); + let (wasm_caller, _code_hash_caller) = + compile_module::("call_with_flags_and_value").unwrap(); + let (wasm_callee, _code_hash_callee) = compile_module::("store_call").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Create contracts: Constructors do nothing. + let addr_caller = builder::bare_instantiate(Code::Upload(wasm_read_only_caller)) + .build_and_unwrap_account_id(); + let addr_subsequent_caller = + builder::bare_instantiate(Code::Upload(wasm_caller)).build_and_unwrap_account_id(); + let addr_callee = + builder::bare_instantiate(Code::Upload(wasm_callee)).build_and_unwrap_account_id(); + + // Subsequent call input. + let input = (&addr_callee, pallet_contracts_uapi::CallFlags::empty().bits(), 0u64, 100u32); + + // Read-only call fails when modifying storage. + assert_err_ignore_postinfo!( + builder::call(addr_caller) + .data((&addr_subsequent_caller, input).encode()) + .build(), + >::ContractTrapped + ); + }); +} + +#[test] +fn read_only_call_works() { + let (wasm_caller, _code_hash_caller) = compile_module::("read_only_call").unwrap(); + let (wasm_callee, _code_hash_callee) = compile_module::("dummy").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = ::Currency::set_balance(&ALICE, 1_000_000); + + // Create both contracts: Constructors do nothing. + let addr_caller = + builder::bare_instantiate(Code::Upload(wasm_caller)).build_and_unwrap_account_id(); + let addr_callee = + builder::bare_instantiate(Code::Upload(wasm_callee)).build_and_unwrap_account_id(); + + assert_ok!(builder::call(addr_caller.clone()).data(addr_callee.encode()).build()); + }); +} diff --git a/pallets/contracts/src/tests/pallet_dummy.rs b/pallets/contracts/src/tests/pallet_dummy.rs new file mode 100644 index 00000000..2af8475d --- /dev/null +++ b/pallets/contracts/src/tests/pallet_dummy.rs @@ -0,0 +1,53 @@ +// This file is part of Substrate. + +// 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. + +pub use pallet::*; + +#[frame_support::pallet(dev_mode)] +pub mod pallet { + use frame_support::{ + dispatch::{Pays, PostDispatchInfo}, + ensure, + pallet_prelude::DispatchResultWithPostInfo, + weights::Weight, + }; + use frame_system::pallet_prelude::*; + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config {} + + #[pallet::call] + impl Pallet { + /// Dummy function that overcharges the predispatch weight, allowing us to test the correct + /// values of [`ContractResult::gas_consumed`] and [`ContractResult::gas_required`] in + /// tests. + #[pallet::call_index(1)] + #[pallet::weight(*pre_charge)] + pub fn overestimate_pre_charge( + origin: OriginFor, + pre_charge: Weight, + actual_weight: Weight, + ) -> DispatchResultWithPostInfo { + ensure_signed(origin)?; + ensure!(pre_charge.any_gt(actual_weight), "pre_charge must be > actual_weight"); + Ok(PostDispatchInfo { actual_weight: Some(actual_weight), pays_fee: Pays::Yes }) + } + } +} diff --git a/pallets/contracts/src/tests/test_debug.rs b/pallets/contracts/src/tests/test_debug.rs new file mode 100644 index 00000000..c9b6557b --- /dev/null +++ b/pallets/contracts/src/tests/test_debug.rs @@ -0,0 +1,229 @@ +// This file is part of Substrate. + +// 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 super::*; +use crate::{ + debug::{CallInterceptor, CallSpan, ExecResult, ExportedFunction, Tracing}, + primitives::ExecReturnValue, + AccountIdOf, +}; +use frame_support::traits::Currency; +use pretty_assertions::assert_eq; +use std::cell::RefCell; + +#[derive(Clone, PartialEq, Eq, Debug)] +struct DebugFrame { + contract_account: AccountId32, + call: ExportedFunction, + input: Vec, + result: Option>, +} + +thread_local! { + static DEBUG_EXECUTION_TRACE: RefCell> = RefCell::new(Vec::new()); + static INTERCEPTED_ADDRESS: RefCell> = RefCell::new(None); +} + +pub struct TestDebug; +pub struct TestCallSpan { + contract_account: AccountId32, + call: ExportedFunction, + input: Vec, +} + +impl Tracing for TestDebug { + type CallSpan = TestCallSpan; + + fn new_call_span( + contract_account: &AccountIdOf, + entry_point: ExportedFunction, + input_data: &[u8], + ) -> TestCallSpan { + DEBUG_EXECUTION_TRACE.with(|d| { + d.borrow_mut().push(DebugFrame { + contract_account: contract_account.clone(), + call: entry_point, + input: input_data.to_vec(), + result: None, + }) + }); + TestCallSpan { + contract_account: contract_account.clone(), + call: entry_point, + input: input_data.to_vec(), + } + } +} + +impl CallInterceptor for TestDebug { + fn intercept_call( + contract_address: &::AccountId, + _entry_point: &ExportedFunction, + _input_data: &[u8], + ) -> Option { + INTERCEPTED_ADDRESS.with(|i| { + if i.borrow().as_ref() == Some(contract_address) { + Some(Ok(ExecReturnValue { flags: ReturnFlags::REVERT, data: vec![] })) + } else { + None + } + }) + } +} + +impl CallSpan for TestCallSpan { + fn after_call(self, output: &ExecReturnValue) { + DEBUG_EXECUTION_TRACE.with(|d| { + d.borrow_mut().push(DebugFrame { + contract_account: self.contract_account, + call: self.call, + input: self.input, + result: Some(output.data.clone()), + }) + }); + } +} + +#[test] +fn debugging_works() { + let (wasm_caller, _) = compile_module::("call").unwrap(); + let (wasm_callee, _) = compile_module::("store_call").unwrap(); + + fn current_stack() -> Vec { + DEBUG_EXECUTION_TRACE.with(|stack| stack.borrow().clone()) + } + + fn deploy(wasm: Vec) -> AccountId32 { + Contracts::bare_instantiate( + ALICE, + 0, + GAS_LIMIT, + None, + Code::Upload(wasm), + vec![], + vec![], + DebugInfo::Skip, + CollectEvents::Skip, + ) + .result + .unwrap() + .account_id + } + + fn constructor_frame(contract_account: &AccountId32, after: bool) -> DebugFrame { + DebugFrame { + contract_account: contract_account.clone(), + call: ExportedFunction::Constructor, + input: vec![], + result: if after { Some(vec![]) } else { None }, + } + } + + fn call_frame(contract_account: &AccountId32, args: Vec, after: bool) -> DebugFrame { + DebugFrame { + contract_account: contract_account.clone(), + call: ExportedFunction::Call, + input: args, + result: if after { Some(vec![]) } else { None }, + } + } + + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = Balances::deposit_creating(&ALICE, 1_000_000); + + assert_eq!(current_stack(), vec![]); + + let addr_caller = deploy(wasm_caller); + let addr_callee = deploy(wasm_callee); + + assert_eq!( + current_stack(), + vec![ + constructor_frame(&addr_caller, false), + constructor_frame(&addr_caller, true), + constructor_frame(&addr_callee, false), + constructor_frame(&addr_callee, true), + ] + ); + + let main_args = (100u32, &addr_callee.clone()).encode(); + let inner_args = (100u32).encode(); + + assert_ok!(Contracts::call( + RuntimeOrigin::signed(ALICE), + addr_caller.clone(), + 0, + GAS_LIMIT, + None, + main_args.clone() + )); + + let stack_top = current_stack()[4..].to_vec(); + assert_eq!( + stack_top, + vec![ + call_frame(&addr_caller, main_args.clone(), false), + call_frame(&addr_callee, inner_args.clone(), false), + call_frame(&addr_callee, inner_args, true), + call_frame(&addr_caller, main_args, true), + ] + ); + }); +} + +#[test] +fn call_interception_works() { + let (wasm, _) = compile_module::("dummy").unwrap(); + + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = Balances::deposit_creating(&ALICE, 1_000_000); + + let account_id = Contracts::bare_instantiate( + ALICE, + 0, + GAS_LIMIT, + None, + Code::Upload(wasm), + vec![], + // some salt to ensure that the address of this contract is unique among all tests + vec![0x41, 0x41, 0x41, 0x41], + DebugInfo::Skip, + CollectEvents::Skip, + ) + .result + .unwrap() + .account_id; + + // no interception yet + assert_ok!(Contracts::call( + RuntimeOrigin::signed(ALICE), + account_id.clone(), + 0, + GAS_LIMIT, + None, + vec![], + )); + + // intercept calls to this contract + INTERCEPTED_ADDRESS.with(|i| *i.borrow_mut() = Some(account_id.clone())); + + assert_err_ignore_postinfo!( + Contracts::call(RuntimeOrigin::signed(ALICE), account_id, 0, GAS_LIMIT, None, vec![],), + >::ContractReverted, + ); + }); +} diff --git a/pallets/contracts/src/transient_storage.rs b/pallets/contracts/src/transient_storage.rs new file mode 100644 index 00000000..c795a966 --- /dev/null +++ b/pallets/contracts/src/transient_storage.rs @@ -0,0 +1,698 @@ +// This file is part of Substrate. + +// 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. + +//! This module contains routines for accessing and altering a contract transient storage. + +use crate::{ + exec::{AccountIdOf, Key}, + storage::WriteOutcome, + Config, Error, +}; +use codec::Encode; +use core::marker::PhantomData; +use frame_support::DefaultNoBound; +use sp_runtime::{DispatchError, DispatchResult, Saturating}; +use sp_std::{collections::btree_map::BTreeMap, mem, vec::Vec}; + +/// Meter entry tracks transaction allocations. +#[derive(Default, Debug)] +pub struct MeterEntry { + /// Allocations made in the current transaction. + pub amount: u32, + /// Allocations limit in the current transaction. + pub limit: u32, +} + +impl MeterEntry { + /// Create a new entry. + fn new(limit: u32) -> Self { + Self { limit, amount: Default::default() } + } + + /// Check if the allocated amount exceeds the limit. + fn exceeds_limit(&self, amount: u32) -> bool { + self.amount.saturating_add(amount) > self.limit + } + + /// Absorb the allocation amount of the nested entry into the current entry. + fn absorb(&mut self, rhs: Self) { + self.amount.saturating_accrue(rhs.amount) + } +} + +// The storage meter enforces a limit for each transaction, +// which is calculated as free_storage * (1 - 1/16) for each subsequent frame. +#[derive(DefaultNoBound)] +pub struct StorageMeter { + nested_meters: Vec, + root_meter: MeterEntry, + _phantom: PhantomData, +} + +impl StorageMeter { + const STORAGE_FRACTION_DENOMINATOR: u32 = 16; + /// Create a new storage allocation meter. + fn new(memory_limit: u32) -> Self { + Self { root_meter: MeterEntry::new(memory_limit), ..Default::default() } + } + + /// Charge the allocated amount of transaction storage from the meter. + fn charge(&mut self, amount: u32) -> DispatchResult { + let meter = self.current_mut(); + if meter.exceeds_limit(amount) { + return Err(Error::::OutOfTransientStorage.into()); + } + meter.amount.saturating_accrue(amount); + Ok(()) + } + + /// Revert a transaction meter. + fn revert(&mut self) { + self.nested_meters.pop().expect( + "A call to revert a meter must be preceded by a corresponding call to start a meter; + the code within this crate makes sure that this is always the case; qed", + ); + } + + /// Start a transaction meter. + fn start(&mut self) { + let meter = self.current(); + let mut transaction_limit = meter.limit.saturating_sub(meter.amount); + if !self.nested_meters.is_empty() { + // Allow use of (1 - 1/STORAGE_FRACTION_DENOMINATOR) of free storage for subsequent + // calls. + transaction_limit.saturating_reduce( + transaction_limit.saturating_div(Self::STORAGE_FRACTION_DENOMINATOR), + ); + } + + self.nested_meters.push(MeterEntry::new(transaction_limit)); + } + + /// Commit a transaction meter. + fn commit(&mut self) { + let transaction_meter = self.nested_meters.pop().expect( + "A call to commit a meter must be preceded by a corresponding call to start a meter; + the code within this crate makes sure that this is always the case; qed", + ); + self.current_mut().absorb(transaction_meter) + } + + /// The total allocated amount of memory. + #[cfg(test)] + fn total_amount(&self) -> u32 { + self.nested_meters + .iter() + .fold(self.root_meter.amount, |acc, e| acc.saturating_add(e.amount)) + } + + /// A mutable reference to the current meter entry. + pub fn current_mut(&mut self) -> &mut MeterEntry { + self.nested_meters.last_mut().unwrap_or(&mut self.root_meter) + } + + /// A reference to the current meter entry. + pub fn current(&self) -> &MeterEntry { + self.nested_meters.last().unwrap_or(&self.root_meter) + } +} + +/// An entry representing a journal change. +struct JournalEntry { + key: Vec, + prev_value: Option>, +} + +impl JournalEntry { + /// Create a new change. + fn new(key: Vec, prev_value: Option>) -> Self { + Self { key, prev_value } + } + + /// Revert the change. + fn revert(self, storage: &mut Storage) { + storage.write(&self.key, self.prev_value); + } +} + +/// A journal containing transient storage modifications. +struct Journal(Vec); + +impl Journal { + /// Create a new journal. + fn new() -> Self { + Self(Default::default()) + } + + /// Add a change to the journal. + fn push(&mut self, entry: JournalEntry) { + self.0.push(entry); + } + + /// Length of the journal. + fn len(&self) -> usize { + self.0.len() + } + + /// Roll back all journal changes until the chackpoint + fn rollback(&mut self, storage: &mut Storage, checkpoint: usize) { + self.0.drain(checkpoint..).rev().for_each(|entry| entry.revert(storage)); + } +} + +/// Storage for maintaining the current transaction state. +#[derive(Default)] +struct Storage(BTreeMap, Vec>); + +impl Storage { + /// Read the storage entry. + fn read(&self, key: &Vec) -> Option> { + self.0.get(key).cloned() + } + + /// Write the storage entry. + fn write(&mut self, key: &Vec, value: Option>) -> Option> { + if let Some(value) = value { + // Insert storage entry. + self.0.insert(key.clone(), value) + } else { + // Remove storage entry. + self.0.remove(key) + } + } +} + +/// Transient storage behaves almost identically to regular storage but is discarded after each +/// transaction. It consists of a `BTreeMap` for the current state, a journal of all changes, and a +/// list of checkpoints. On entry to the `start_transaction` function, a marker (checkpoint) is +/// added to the list. New values are written to the current state, and the previous value is +/// recorded in the journal (`write`). When the `commit_transaction` function is called, the marker +/// to the journal index (checkpoint) of when that call was entered is discarded. +/// On `rollback_transaction`, all entries are reverted up to the last checkpoint. +pub struct TransientStorage { + // The storage and journal size is limited by the storage meter. + storage: Storage, + journal: Journal, + // The size of the StorageMeter is limited by the stack depth. + meter: StorageMeter, + // The size of the checkpoints is limited by the stack depth. + checkpoints: Vec, +} + +impl TransientStorage { + /// Create new transient storage with the supplied memory limit. + pub fn new(memory_limit: u32) -> Self { + TransientStorage { + storage: Default::default(), + journal: Journal::new(), + checkpoints: Default::default(), + meter: StorageMeter::new(memory_limit), + } + } + + /// Read the storage value. If the entry does not exist, `None` is returned. + pub fn read(&self, account: &AccountIdOf, key: &Key) -> Option> { + self.storage.read(&Self::storage_key(&account.encode(), &key.hash())) + } + + /// Write a value to storage. + /// + /// If the `value` is `None`, then the entry is removed. If `take` is true, + /// a [`WriteOutcome::Taken`] is returned instead of a [`WriteOutcome::Overwritten`]. + /// If the entry did not exist, [`WriteOutcome::New`] is returned. + pub fn write( + &mut self, + account: &AccountIdOf, + key: &Key, + value: Option>, + take: bool, + ) -> Result { + let key = Self::storage_key(&account.encode(), &key.hash()); + let prev_value = self.storage.read(&key); + // Skip if the same value is being set. + if prev_value != value { + // Calculate the allocation size. + if let Some(value) = &value { + // Charge the key, value and journal entry. + // If a new value is written, a new journal entry is created. The previous value is + // moved to the journal along with its key, and the new value is written to + // storage. + let key_len = key.capacity(); + let mut amount = value + .capacity() + .saturating_add(key_len) + .saturating_add(mem::size_of::()); + if prev_value.is_none() { + // Charge a new storage entry. + // If there was no previous value, a new entry is added to storage (BTreeMap) + // containing a Vec for the key and a Vec for the value. The value was already + // included in the amount. + amount.saturating_accrue(key_len.saturating_add(mem::size_of::>())); + } + self.meter.charge(amount as _)?; + } + self.storage.write(&key, value); + // Update the journal. + self.journal.push(JournalEntry::new(key, prev_value.clone())); + } + + Ok(match (take, prev_value) { + (_, None) => WriteOutcome::New, + (false, Some(prev_value)) => WriteOutcome::Overwritten(prev_value.len() as _), + (true, Some(prev_value)) => WriteOutcome::Taken(prev_value), + }) + } + + /// Start a new nested transaction. + /// + /// This allows to either commit or roll back all changes that are made after this call. + /// For every transaction there must be a matching call to either `rollback_transaction` + /// or `commit_transaction`. + pub fn start_transaction(&mut self) { + self.meter.start(); + self.checkpoints.push(self.journal.len()); + } + + /// Rollback the last transaction started by `start_transaction`. + /// + /// Any changes made during that transaction are discarded. + /// + /// # Panics + /// + /// Will panic if there is no open transaction. + pub fn rollback_transaction(&mut self) { + let checkpoint = self + .checkpoints + .pop() + .expect( + "A call to rollback_transaction must be preceded by a corresponding call to start_transaction; + the code within this crate makes sure that this is always the case; qed" + ); + self.meter.revert(); + self.journal.rollback(&mut self.storage, checkpoint); + } + + /// Commit the last transaction started by `start_transaction`. + /// + /// Any changes made during that transaction are committed. + /// + /// # Panics + /// + /// Will panic if there is no open transaction. + pub fn commit_transaction(&mut self) { + self.checkpoints + .pop() + .expect( + "A call to commit_transaction must be preceded by a corresponding call to start_transaction; + the code within this crate makes sure that this is always the case; qed" + ); + self.meter.commit(); + } + + /// The storage allocation meter used for transaction metering. + #[cfg(any(test, feature = "runtime-benchmarks"))] + pub fn meter(&mut self) -> &mut StorageMeter { + return &mut self.meter + } + + fn storage_key(account: &[u8], key: &[u8]) -> Vec { + let mut storage_key = Vec::with_capacity(account.len() + key.len()); + storage_key.extend_from_slice(&account); + storage_key.extend_from_slice(&key); + storage_key + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + tests::{Test, ALICE, BOB, CHARLIE}, + Error, + }; + use core::u32::MAX; + + // Calculate the allocation size for the given entry. + fn allocation_size( + account: &AccountIdOf, + key: &Key, + value: Option>, + ) -> u32 { + let mut storage: TransientStorage = TransientStorage::::new(MAX); + storage + .write(account, key, value, false) + .expect("Could not write to transient storage."); + storage.meter().current().amount + } + + #[test] + fn read_write_works() { + let mut storage: TransientStorage = TransientStorage::::new(2048); + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false), + Ok(WriteOutcome::New) + ); + assert_eq!( + storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![2]), true), + Ok(WriteOutcome::New) + ); + assert_eq!( + storage.write(&BOB, &Key::Fix([3; 32]), Some(vec![3]), false), + Ok(WriteOutcome::New) + ); + assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), Some(vec![1])); + assert_eq!(storage.read(&ALICE, &Key::Fix([2; 32])), Some(vec![2])); + assert_eq!(storage.read(&BOB, &Key::Fix([3; 32])), Some(vec![3])); + // Overwrite values. + assert_eq!( + storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![4, 5]), false), + Ok(WriteOutcome::Overwritten(1)) + ); + assert_eq!( + storage.write(&BOB, &Key::Fix([3; 32]), Some(vec![6, 7]), true), + Ok(WriteOutcome::Taken(vec![3])) + ); + assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), Some(vec![1])); + assert_eq!(storage.read(&ALICE, &Key::Fix([2; 32])), Some(vec![4, 5])); + assert_eq!(storage.read(&BOB, &Key::Fix([3; 32])), Some(vec![6, 7])); + + // Check for an empty value. + assert_eq!( + storage.write(&BOB, &Key::Fix([3; 32]), Some(vec![]), true), + Ok(WriteOutcome::Taken(vec![6, 7])) + ); + assert_eq!(storage.read(&BOB, &Key::Fix([3; 32])), Some(vec![])); + + assert_eq!( + storage.write(&BOB, &Key::Fix([3; 32]), None, true), + Ok(WriteOutcome::Taken(vec![])) + ); + assert_eq!(storage.read(&BOB, &Key::Fix([3; 32])), None); + } + + #[test] + fn read_write_with_var_sized_keys_works() { + let mut storage = TransientStorage::::new(2048); + assert_eq!( + storage.write( + &ALICE, + &Key::::try_from_var([1; 64].to_vec()).unwrap(), + Some(vec![1]), + false + ), + Ok(WriteOutcome::New) + ); + assert_eq!( + storage.write( + &BOB, + &Key::::try_from_var([2; 64].to_vec()).unwrap(), + Some(vec![2, 3]), + false + ), + Ok(WriteOutcome::New) + ); + assert_eq!( + storage.read(&ALICE, &Key::::try_from_var([1; 64].to_vec()).unwrap()), + Some(vec![1]) + ); + assert_eq!( + storage.read(&BOB, &Key::::try_from_var([2; 64].to_vec()).unwrap()), + Some(vec![2, 3]) + ); + // Overwrite values. + assert_eq!( + storage.write( + &ALICE, + &Key::::try_from_var([1; 64].to_vec()).unwrap(), + Some(vec![4, 5]), + false + ), + Ok(WriteOutcome::Overwritten(1)) + ); + assert_eq!( + storage.read(&ALICE, &Key::::try_from_var([1; 64].to_vec()).unwrap()), + Some(vec![4, 5]) + ); + } + + #[test] + fn rollback_transaction_works() { + let mut storage = TransientStorage::::new(1024); + + storage.start_transaction(); + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false), + Ok(WriteOutcome::New) + ); + storage.rollback_transaction(); + assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), None) + } + + #[test] + fn commit_transaction_works() { + let mut storage = TransientStorage::::new(1024); + + storage.start_transaction(); + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false), + Ok(WriteOutcome::New) + ); + storage.commit_transaction(); + assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), Some(vec![1])) + } + + #[test] + fn overwrite_and_commmit_transaction_works() { + let mut storage = TransientStorage::::new(1024); + storage.start_transaction(); + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false), + Ok(WriteOutcome::New) + ); + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1, 2]), false), + Ok(WriteOutcome::Overwritten(1)) + ); + storage.commit_transaction(); + assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), Some(vec![1, 2])) + } + + #[test] + fn rollback_in_nested_transaction_works() { + let mut storage = TransientStorage::::new(1024); + storage.start_transaction(); + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false), + Ok(WriteOutcome::New) + ); + storage.start_transaction(); + assert_eq!( + storage.write(&BOB, &Key::Fix([1; 32]), Some(vec![1]), false), + Ok(WriteOutcome::New) + ); + storage.rollback_transaction(); + storage.commit_transaction(); + assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), Some(vec![1])); + assert_eq!(storage.read(&BOB, &Key::Fix([1; 32])), None) + } + + #[test] + fn commit_in_nested_transaction_works() { + let mut storage = TransientStorage::::new(1024); + storage.start_transaction(); + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false), + Ok(WriteOutcome::New) + ); + storage.start_transaction(); + assert_eq!( + storage.write(&BOB, &Key::Fix([1; 32]), Some(vec![2]), false), + Ok(WriteOutcome::New) + ); + storage.start_transaction(); + assert_eq!( + storage.write(&CHARLIE, &Key::Fix([1; 32]), Some(vec![3]), false), + Ok(WriteOutcome::New) + ); + storage.commit_transaction(); + storage.commit_transaction(); + storage.commit_transaction(); + assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), Some(vec![1])); + assert_eq!(storage.read(&BOB, &Key::Fix([1; 32])), Some(vec![2])); + assert_eq!(storage.read(&CHARLIE, &Key::Fix([1; 32])), Some(vec![3])); + } + + #[test] + fn rollback_all_transactions_works() { + let mut storage = TransientStorage::::new(1024); + storage.start_transaction(); + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1]), false), + Ok(WriteOutcome::New) + ); + storage.start_transaction(); + assert_eq!( + storage.write(&BOB, &Key::Fix([1; 32]), Some(vec![2]), false), + Ok(WriteOutcome::New) + ); + storage.start_transaction(); + assert_eq!( + storage.write(&CHARLIE, &Key::Fix([1; 32]), Some(vec![3]), false), + Ok(WriteOutcome::New) + ); + storage.commit_transaction(); + storage.commit_transaction(); + storage.rollback_transaction(); + assert_eq!(storage.read(&ALICE, &Key::Fix([1; 32])), None); + assert_eq!(storage.read(&BOB, &Key::Fix([1; 32])), None); + assert_eq!(storage.read(&CHARLIE, &Key::Fix([1; 32])), None); + } + + #[test] + fn metering_transactions_works() { + let size = allocation_size(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096])); + let mut storage = TransientStorage::::new(size * 2); + storage.start_transaction(); + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false), + Ok(WriteOutcome::New) + ); + let limit = storage.meter().current().limit; + storage.commit_transaction(); + + storage.start_transaction(); + assert_eq!(storage.meter().current().limit, limit - size); + assert_eq!(storage.meter().current().limit - storage.meter().current().amount, size); + assert_eq!( + storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![1u8; 4096]), false), + Ok(WriteOutcome::New) + ); + assert_eq!(storage.meter().current().amount, size); + storage.commit_transaction(); + assert_eq!(storage.meter().total_amount(), size * 2); + } + + #[test] + fn metering_nested_transactions_works() { + let size = allocation_size(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096])); + let mut storage = TransientStorage::::new(size * 3); + + storage.start_transaction(); + let limit = storage.meter().current().limit; + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false), + Ok(WriteOutcome::New) + ); + storage.start_transaction(); + assert_eq!(storage.meter().total_amount(), size); + assert!(storage.meter().current().limit < limit - size); + assert_eq!( + storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![1u8; 4096]), false), + Ok(WriteOutcome::New) + ); + storage.commit_transaction(); + assert_eq!(storage.meter().current().limit, limit); + assert_eq!(storage.meter().total_amount(), storage.meter().current().amount); + storage.commit_transaction(); + } + + #[test] + fn metering_transaction_fails() { + let size = allocation_size(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096])); + let mut storage = TransientStorage::::new(size - 1); + storage.start_transaction(); + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false), + Err(Error::::OutOfTransientStorage.into()) + ); + assert_eq!(storage.meter.current().amount, 0); + storage.commit_transaction(); + assert_eq!(storage.meter.total_amount(), 0); + } + + #[test] + fn metering_nested_transactions_fails() { + let size = allocation_size(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096])); + let mut storage = TransientStorage::::new(size * 2); + + storage.start_transaction(); + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false), + Ok(WriteOutcome::New) + ); + storage.start_transaction(); + assert_eq!( + storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![1u8; 4096]), false), + Err(Error::::OutOfTransientStorage.into()) + ); + storage.commit_transaction(); + storage.commit_transaction(); + assert_eq!(storage.meter.total_amount(), size); + } + + #[test] + fn metering_nested_transaction_with_rollback_works() { + let size = allocation_size(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096])); + let mut storage = TransientStorage::::new(size * 2); + + storage.start_transaction(); + let limit = storage.meter.current().limit; + storage.start_transaction(); + assert_eq!( + storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![1u8; 4096]), false), + Ok(WriteOutcome::New) + ); + storage.rollback_transaction(); + + assert_eq!(storage.meter.total_amount(), 0); + assert_eq!(storage.meter.current().limit, limit); + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false), + Ok(WriteOutcome::New) + ); + let amount = storage.meter().current().amount; + assert_eq!(storage.meter().total_amount(), amount); + storage.commit_transaction(); + } + + #[test] + fn metering_with_rollback_works() { + let size = allocation_size(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096])); + let mut storage = TransientStorage::::new(size * 5); + + storage.start_transaction(); + assert_eq!( + storage.write(&ALICE, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false), + Ok(WriteOutcome::New) + ); + let amount = storage.meter.total_amount(); + storage.start_transaction(); + assert_eq!( + storage.write(&ALICE, &Key::Fix([2; 32]), Some(vec![1u8; 4096]), false), + Ok(WriteOutcome::New) + ); + storage.start_transaction(); + assert_eq!( + storage.write(&BOB, &Key::Fix([1; 32]), Some(vec![1u8; 4096]), false), + Ok(WriteOutcome::New) + ); + storage.commit_transaction(); + storage.rollback_transaction(); + assert_eq!(storage.meter.total_amount(), amount); + storage.commit_transaction(); + } +} diff --git a/pallets/contracts/src/wasm/mod.rs b/pallets/contracts/src/wasm/mod.rs new file mode 100644 index 00000000..f4ee7645 --- /dev/null +++ b/pallets/contracts/src/wasm/mod.rs @@ -0,0 +1,3917 @@ +// This file is part of Substrate. + +// 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. + +//! This module provides a means for executing contracts +//! represented in wasm. + +mod prepare; +mod runtime; + +#[cfg(doc)] +pub use crate::wasm::runtime::api_doc; + +#[cfg(test)] +pub use { + crate::wasm::{prepare::tracker, runtime::ReturnErrorCode}, + runtime::STABLE_API_COUNT, + tests::MockExt, +}; + +#[cfg(feature = "runtime-benchmarks")] +pub use crate::wasm::runtime::{BenchEnv, ReturnData, TrapReason}; + +pub use crate::wasm::{ + prepare::{LoadedModule, LoadingMode}, + runtime::{ + AllowDeprecatedInterface, AllowUnstableInterface, Environment, Runtime, RuntimeCosts, + }, +}; + +use crate::{ + exec::{ExecResult, Executable, ExportedFunction, Ext}, + gas::{GasMeter, Token}, + weights::WeightInfo, + AccountIdOf, BadOrigin, BalanceOf, CodeHash, CodeInfoOf, CodeVec, Config, Error, Event, + HoldReason, Pallet, PristineCode, Schedule, Weight, LOG_TARGET, +}; +use alloc::vec::Vec; +use codec::{Decode, Encode, MaxEncodedLen}; +use frame_support::{ + dispatch::DispatchResult, + ensure, + traits::{fungible::MutateHold, tokens::Precision::BestEffort}, +}; +use sp_core::Get; +use sp_runtime::{DispatchError, RuntimeDebug}; +use wasmi::{CompilationMode, InstancePre, Linker, Memory, MemoryType, StackLimits, Store}; + +const BYTES_PER_PAGE: usize = 64 * 1024; + +/// Validated Wasm module ready for execution. +/// This data structure is immutable once created and stored. +#[derive(Encode, Decode, scale_info::TypeInfo)] +#[codec(mel_bound())] +#[scale_info(skip_type_params(T))] +pub struct WasmBlob { + code: CodeVec, + // This isn't needed for contract execution and is not stored alongside it. + #[codec(skip)] + code_info: CodeInfo, + // This is for not calculating the hash every time we need it. + #[codec(skip)] + code_hash: CodeHash, +} + +/// Contract code related data, such as: +/// +/// - owner of the contract, i.e. account uploaded its code, +/// - storage deposit amount, +/// - reference count, +/// - determinism marker. +/// +/// It is stored in a separate storage entry to avoid loading the code when not necessary. +#[derive(Clone, Encode, Decode, scale_info::TypeInfo, MaxEncodedLen)] +#[codec(mel_bound())] +#[scale_info(skip_type_params(T))] +pub struct CodeInfo { + /// The account that has uploaded the contract code and hence is allowed to remove it. + owner: AccountIdOf, + /// The amount of balance that was deposited by the owner in order to store it on-chain. + #[codec(compact)] + deposit: BalanceOf, + /// The number of instantiated contracts that use this as their code. + #[codec(compact)] + refcount: u64, + /// Marks if the code might contain non-deterministic features and is therefore never allowed + /// to be run on-chain. Specifically, such a code can never be instantiated into a contract + /// and can just be used through a delegate call. + determinism: Determinism, + /// length of the code in bytes. + code_len: u32, +} + +/// Defines the required determinism level of a wasm blob when either running or uploading code. +#[derive( + Clone, Copy, Encode, Decode, scale_info::TypeInfo, MaxEncodedLen, RuntimeDebug, PartialEq, Eq, +)] +pub enum Determinism { + /// The execution should be deterministic and hence no indeterministic instructions are + /// allowed. + /// + /// Dispatchables always use this mode in order to make on-chain execution deterministic. + Enforced, + /// Allow calling or uploading an indeterministic code. + /// + /// This is only possible when calling into `pallet-contracts` directly via + /// [`crate::Pallet::bare_call`]. + /// + /// # Note + /// + /// **Never** use this mode for on-chain execution. + Relaxed, +} + +impl ExportedFunction { + /// The wasm export name for the function. + fn identifier(&self) -> &str { + match self { + Self::Constructor => "deploy", + Self::Call => "call", + } + } +} + +/// Cost of code loading from storage. +#[cfg_attr(test, derive(Debug, PartialEq, Eq))] +#[derive(Clone, Copy)] +struct CodeLoadToken(u32); + +impl Token for CodeLoadToken { + fn weight(&self) -> Weight { + T::WeightInfo::call_with_code_per_byte(self.0) + .saturating_sub(T::WeightInfo::call_with_code_per_byte(0)) + } +} + +impl WasmBlob { + /// Create the module by checking the `code`. + pub fn from_code( + code: Vec, + schedule: &Schedule, + owner: AccountIdOf, + determinism: Determinism, + ) -> Result { + prepare::prepare::( + code.try_into().map_err(|_| (>::CodeTooLarge.into(), ""))?, + schedule, + owner, + determinism, + ) + } + + /// Remove the code from storage and refund the deposit to its owner. + /// + /// Applies all necessary checks before removing the code. + pub fn remove(origin: &T::AccountId, code_hash: CodeHash) -> DispatchResult { + >::try_mutate_exists(&code_hash, |existing| { + if let Some(code_info) = existing { + ensure!(code_info.refcount == 0, >::CodeInUse); + ensure!(&code_info.owner == origin, BadOrigin); + let _ = T::Currency::release( + &HoldReason::CodeUploadDepositReserve.into(), + &code_info.owner, + code_info.deposit, + BestEffort, + ); + let deposit_released = code_info.deposit; + let remover = code_info.owner.clone(); + + *existing = None; + >::remove(&code_hash); + >::deposit_event(Event::CodeRemoved { + code_hash, + deposit_released, + remover, + }); + Ok(()) + } else { + Err(>::CodeNotFound.into()) + } + }) + } + + /// Creates and returns an instance of the supplied code. + /// + /// This is either used for later executing a contract or for validation of a contract. + /// When validating we pass `()` as `host_state`. Please note that such a dummy instance must + /// **never** be called/executed, since it will panic the executor. + pub fn instantiate( + contract: LoadedModule, + host_state: H, + schedule: &Schedule, + allow_deprecated: AllowDeprecatedInterface, + ) -> Result<(Store, Memory, InstancePre), &'static str> + where + E: Environment, + { + let mut store = Store::new(&contract.engine, host_state); + let mut linker = Linker::new(&contract.engine); + E::define( + &mut store, + &mut linker, + if T::UnsafeUnstableInterface::get() { + AllowUnstableInterface::Yes + } else { + AllowUnstableInterface::No + }, + allow_deprecated, + ) + .map_err(|_| "can't define host functions to Linker")?; + + // Query wasmi for memory limits specified in the module's import entry. + let memory_limits = contract.scan_imports::(schedule)?; + // Here we allocate this memory in the _store_. It allocates _inital_ value, but allows it + // to grow up to maximum number of memory pages, if necessary. + let qed = "We checked the limits versus our Schedule, + which specifies the max amount of memory pages + well below u16::MAX; qed"; + let memory = Memory::new( + &mut store, + MemoryType::new(memory_limits.0, Some(memory_limits.1)).expect(qed), + ) + .expect(qed); + + linker + .define("env", "memory", memory) + .expect("We just created the Linker. It has no definitions with this name; qed"); + + let instance = linker.instantiate(&mut store, &contract.module).map_err(|err| { + log::debug!(target: LOG_TARGET, "failed to instantiate module: {:?}", err); + "can't instantiate module with provided definitions" + })?; + + Ok((store, memory, instance)) + } + + /// Puts the module blob into storage, and returns the deposit collected for the storage. + pub fn store_code(&mut self) -> Result, Error> { + let code_hash = *self.code_hash(); + >::mutate(code_hash, |stored_code_info| { + match stored_code_info { + // Contract code is already stored in storage. Nothing to be done here. + Some(_) => Ok(Default::default()), + // Upload a new contract code. + // We need to store the code and its code_info, and collect the deposit. + // This `None` case happens only with freshly uploaded modules. This means that + // the `owner` is always the origin of the current transaction. + None => { + let deposit = self.code_info.deposit; + T::Currency::hold( + &HoldReason::CodeUploadDepositReserve.into(), + &self.code_info.owner, + deposit, + ) + .map_err(|_| >::StorageDepositNotEnoughFunds)?; + + self.code_info.refcount = 0; + >::insert(code_hash, &self.code); + *stored_code_info = Some(self.code_info.clone()); + >::deposit_event(Event::CodeStored { + code_hash, + deposit_held: deposit, + uploader: self.code_info.owner.clone(), + }); + Ok(deposit) + }, + } + }) + } + + /// Create the module without checking the passed code. + /// + /// # Note + /// + /// This is useful for benchmarking where we don't want validation of the module to skew + /// our results. This also does not collect any deposit from the `owner`. Also useful + /// during testing when we want to deploy codes that do not pass the instantiation checks. + #[cfg(any(test, feature = "runtime-benchmarks"))] + pub fn from_code_unchecked( + code: Vec, + schedule: &Schedule, + owner: T::AccountId, + ) -> Result { + prepare::benchmarking::prepare(code, schedule, owner) + } +} + +impl CodeInfo { + #[cfg(test)] + pub fn new(owner: T::AccountId) -> Self { + CodeInfo { + owner, + deposit: Default::default(), + refcount: 0, + code_len: 0, + determinism: Determinism::Enforced, + } + } + + /// Returns the determinism of the module. + pub fn determinism(&self) -> Determinism { + self.determinism + } + + /// Returns reference count of the module. + pub fn refcount(&self) -> u64 { + self.refcount + } + + /// Return mutable reference to the refcount of the module. + pub fn refcount_mut(&mut self) -> &mut u64 { + &mut self.refcount + } + + /// Returns the deposit of the module. + pub fn deposit(&self) -> BalanceOf { + self.deposit + } +} + +use crate::{ExecError, ExecReturnValue}; +use wasmi::Func; +enum InstanceOrExecReturn<'a, E: Ext> { + Instance((Func, Store>)), + ExecReturn(ExecReturnValue), +} + +type PreExecResult<'a, E> = Result, ExecError>; + +impl WasmBlob { + /// Sync the frame's gas meter with the engine's one. + pub fn process_result>( + mut store: Store>, + result: Result<(), wasmi::Error>, + ) -> ExecResult { + let engine_fuel = store.get_fuel().expect("Fuel metering is enabled; qed"); + let gas_meter = store.data_mut().ext().gas_meter_mut(); + let _ = gas_meter.sync_from_executor(engine_fuel)?; + store.into_data().to_execution_result(result) + } + + #[cfg(feature = "runtime-benchmarks")] + pub fn bench_prepare_call>( + self, + ext: &mut E, + input_data: Vec, + ) -> (Func, Store>) { + use InstanceOrExecReturn::*; + match Self::prepare_execute( + self, + Runtime::new(ext, input_data), + &ExportedFunction::Call, + CompilationMode::Eager, + ) + .expect("Benchmark should provide valid module") + { + Instance((func, store)) => (func, store), + ExecReturn(_) => panic!("Expected Instance"), + } + } + + fn prepare_execute<'a, E: Ext>( + self, + runtime: Runtime<'a, E>, + function: &'a ExportedFunction, + compilation_mode: CompilationMode, + ) -> PreExecResult<'a, E> { + let code = self.code.as_slice(); + // Instantiate the Wasm module to the engine. + let schedule = ::Schedule::get(); + + let contract = LoadedModule::new::( + &code, + self.code_info.determinism, + Some(StackLimits::default()), + LoadingMode::Unchecked, + compilation_mode, + ) + .map_err(|err| { + log::debug!(target: LOG_TARGET, "failed to create wasmi module: {err:?}"); + Error::::CodeRejected + })?; + + let (mut store, memory, instance) = Self::instantiate::( + contract, + runtime, + &schedule, + match function { + ExportedFunction::Call => AllowDeprecatedInterface::Yes, + ExportedFunction::Constructor => AllowDeprecatedInterface::No, + }, + ) + .map_err(|msg| { + log::debug!(target: LOG_TARGET, "failed to instantiate code to wasmi: {}", msg); + Error::::CodeRejected + })?; + store.data_mut().set_memory(memory); + + // Set fuel limit for the wasmi execution. + // We normalize it by the base instruction weight, as its cost in wasmi engine is `1`. + let fuel_limit = store + .data_mut() + .ext() + .gas_meter_mut() + .gas_left() + .ref_time() + .checked_div(T::Schedule::get().ref_time_by_fuel()) + .ok_or(Error::::InvalidSchedule)?; + store + .set_fuel(fuel_limit) + .expect("We've set up engine to fuel consuming mode; qed"); + + // Start function should already see the correct refcount in case it will be ever inspected. + if let &ExportedFunction::Constructor = function { + E::increment_refcount(self.code_hash)?; + } + + // Any abort in start function (includes `return` + `terminate`) will make us skip the + // call into the subsequent exported function. This means that calling `return` returns data + // from the whole contract execution. + match instance.start(&mut store) { + Ok(instance) => { + let exported_func = instance + .get_export(&store, function.identifier()) + .and_then(|export| export.into_func()) + .ok_or_else(|| { + log::error!(target: LOG_TARGET, "failed to find entry point"); + Error::::CodeRejected + })?; + + Ok(InstanceOrExecReturn::Instance((exported_func, store))) + }, + Err(err) => Self::process_result(store, Err(err)).map(InstanceOrExecReturn::ExecReturn), + } + } +} + +impl Executable for WasmBlob { + fn from_storage( + code_hash: CodeHash, + gas_meter: &mut GasMeter, + ) -> Result { + let code_info = >::get(code_hash).ok_or(Error::::CodeNotFound)?; + gas_meter.charge(CodeLoadToken(code_info.code_len))?; + let code = >::get(code_hash).ok_or(Error::::CodeNotFound)?; + Ok(Self { code, code_info, code_hash }) + } + + fn execute>( + self, + ext: &mut E, + function: &ExportedFunction, + input_data: Vec, + ) -> ExecResult { + use InstanceOrExecReturn::*; + match Self::prepare_execute( + self, + Runtime::new(ext, input_data), + function, + CompilationMode::Lazy, + )? { + Instance((func, mut store)) => { + let result = func.call(&mut store, &[], &mut []); + Self::process_result(store, result) + }, + ExecReturn(exec_return) => Ok(exec_return), + } + } + + fn code_hash(&self) -> &CodeHash { + &self.code_hash + } + + fn code_info(&self) -> &CodeInfo { + &self.code_info + } + + fn code_len(&self) -> u32 { + self.code.len() as u32 + } + + fn is_deterministic(&self) -> bool { + matches!(self.code_info.determinism, Determinism::Enforced) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + exec::{AccountIdOf, ErrorOrigin, ExecError, Executable, Ext, Key, SeedOf}, + gas::GasMeter, + primitives::ExecReturnValue, + storage::WriteOutcome, + tests::{RuntimeCall, Test, ALICE, BOB}, + transient_storage::TransientStorage, + BalanceOf, CodeHash, Error, Origin, Pallet as Contracts, + }; + use assert_matches::assert_matches; + use frame_support::{ + assert_err, assert_ok, dispatch::DispatchResultWithPostInfo, weights::Weight, + }; + use frame_system::pallet_prelude::BlockNumberFor; + use pallet_contracts_uapi::ReturnFlags; + use pretty_assertions::assert_eq; + use sp_core::H256; + use sp_runtime::DispatchError; + use std::{ + borrow::BorrowMut, + cell::RefCell, + collections::{ + hash_map::{Entry, HashMap}, + HashSet, + }, + }; + + #[derive(Debug, PartialEq, Eq)] + struct InstantiateEntry { + code_hash: H256, + value: u64, + data: Vec, + gas_left: u64, + salt: Vec, + } + + #[derive(Debug, PartialEq, Eq)] + struct TerminationEntry { + beneficiary: AccountIdOf, + } + + #[derive(Debug, PartialEq, Eq)] + struct TransferEntry { + to: AccountIdOf, + value: u64, + } + + #[derive(Debug, PartialEq, Eq)] + struct CallEntry { + to: AccountIdOf, + value: u64, + data: Vec, + allows_reentry: bool, + read_only: bool, + } + + #[derive(Debug, PartialEq, Eq)] + struct CallCodeEntry { + code_hash: H256, + data: Vec, + } + + pub struct MockExt { + storage: HashMap, Vec>, + transient_storage: TransientStorage, + instantiates: Vec, + terminations: Vec, + calls: Vec, + code_calls: Vec, + transfers: Vec, + // (topics, data) + events: Vec<(Vec, Vec)>, + runtime_calls: RefCell>, + schedule: Schedule, + gas_meter: GasMeter, + debug_buffer: Vec, + ecdsa_recover: RefCell>, + sr25519_verify: RefCell, [u8; 32])>>, + code_hashes: Vec>, + caller: Origin, + delegate_dependencies: RefCell>>, + } + + /// The call is mocked and just returns this hardcoded value. + fn call_return_data() -> Vec { + vec![0xDE, 0xAD, 0xBE, 0xEF] + } + + impl Default for MockExt { + fn default() -> Self { + Self { + code_hashes: Default::default(), + storage: Default::default(), + transient_storage: TransientStorage::new(1024 * 1024), + instantiates: Default::default(), + terminations: Default::default(), + calls: Default::default(), + code_calls: Default::default(), + transfers: Default::default(), + events: Default::default(), + runtime_calls: Default::default(), + schedule: Default::default(), + gas_meter: GasMeter::new(Weight::from_parts(10_000_000_000, 10 * 1024 * 1024)), + debug_buffer: Default::default(), + ecdsa_recover: Default::default(), + caller: Default::default(), + sr25519_verify: Default::default(), + delegate_dependencies: Default::default(), + } + } + } + + impl Ext for MockExt { + type T = Test; + + fn call( + &mut self, + _gas_limit: Weight, + _deposit_limit: BalanceOf, + to: AccountIdOf, + value: u64, + data: Vec, + allows_reentry: bool, + read_only: bool, + ) -> Result { + self.calls.push(CallEntry { to, value, data, allows_reentry, read_only }); + Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: call_return_data() }) + } + fn delegate_call( + &mut self, + code_hash: CodeHash, + data: Vec, + ) -> Result { + self.code_calls.push(CallCodeEntry { code_hash, data }); + Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: call_return_data() }) + } + fn instantiate( + &mut self, + gas_limit: Weight, + _deposit_limit: BalanceOf, + code_hash: CodeHash, + value: u64, + data: Vec, + salt: &[u8], + ) -> Result<(AccountIdOf, ExecReturnValue), ExecError> { + self.instantiates.push(InstantiateEntry { + code_hash, + value, + data: data.to_vec(), + gas_left: gas_limit.ref_time(), + salt: salt.to_vec(), + }); + Ok(( + Contracts::::contract_address(&ALICE, &code_hash, &data, salt), + ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() }, + )) + } + fn set_code_hash(&mut self, hash: CodeHash) -> DispatchResult { + self.code_hashes.push(hash); + Ok(()) + } + fn transfer(&mut self, to: &AccountIdOf, value: u64) -> DispatchResult { + self.transfers.push(TransferEntry { to: to.clone(), value }); + Ok(()) + } + fn terminate(&mut self, beneficiary: &AccountIdOf) -> DispatchResult { + self.terminations.push(TerminationEntry { beneficiary: beneficiary.clone() }); + Ok(()) + } + fn get_storage(&mut self, key: &Key) -> Option> { + self.storage.get(&key.to_vec()).cloned() + } + fn get_storage_size(&mut self, key: &Key) -> Option { + self.storage.get(&key.to_vec()).map(|val| val.len() as u32) + } + fn set_storage( + &mut self, + key: &Key, + value: Option>, + take_old: bool, + ) -> Result { + let key = key.to_vec(); + let entry = self.storage.entry(key.clone()); + let result = match (entry, take_old) { + (Entry::Vacant(_), _) => WriteOutcome::New, + (Entry::Occupied(entry), false) => + WriteOutcome::Overwritten(entry.remove().len() as u32), + (Entry::Occupied(entry), true) => WriteOutcome::Taken(entry.remove()), + }; + if let Some(value) = value { + self.storage.insert(key, value); + } + Ok(result) + } + fn get_transient_storage(&self, key: &Key) -> Option> { + self.transient_storage.read(self.address(), key) + } + fn get_transient_storage_size(&self, key: &Key) -> Option { + self.transient_storage.read(self.address(), key).map(|value| value.len() as _) + } + fn set_transient_storage( + &mut self, + key: &Key, + value: Option>, + take_old: bool, + ) -> Result { + let account_id = self.address().clone(); + self.transient_storage.write(&account_id, key, value, take_old) + } + fn caller(&self) -> Origin { + self.caller.clone() + } + fn is_contract(&self, _address: &AccountIdOf) -> bool { + true + } + fn code_hash(&self, _address: &AccountIdOf) -> Option> { + Some(H256::from_slice(&[0x11; 32])) + } + fn own_code_hash(&mut self) -> &CodeHash { + const HASH: H256 = H256::repeat_byte(0x10); + &HASH + } + fn caller_is_origin(&self) -> bool { + false + } + fn caller_is_root(&self) -> bool { + &self.caller == &Origin::Root + } + fn address(&self) -> &AccountIdOf { + &BOB + } + fn balance(&self) -> u64 { + 228 + } + fn value_transferred(&self) -> u64 { + 1337 + } + fn now(&self) -> &u64 { + &1111 + } + fn minimum_balance(&self) -> u64 { + 666 + } + fn random(&self, subject: &[u8]) -> (SeedOf, BlockNumberFor) { + (H256::from_slice(subject), 42) + } + fn deposit_event(&mut self, topics: Vec, data: Vec) { + self.events.push((topics, data)) + } + fn block_number(&self) -> u64 { + 121 + } + fn max_value_size(&self) -> u32 { + 16_384 + } + fn get_weight_price(&self, weight: Weight) -> BalanceOf { + BalanceOf::::from(1312_u32) + .saturating_mul(weight.ref_time().into()) + .saturating_add( + BalanceOf::::from(103_u32).saturating_mul(weight.proof_size()), + ) + } + fn schedule(&self) -> &Schedule { + &self.schedule + } + fn gas_meter(&self) -> &GasMeter { + &self.gas_meter + } + fn gas_meter_mut(&mut self) -> &mut GasMeter { + &mut self.gas_meter + } + fn charge_storage(&mut self, _diff: &crate::storage::meter::Diff) {} + + fn debug_buffer_enabled(&self) -> bool { + true + } + fn append_debug_buffer(&mut self, msg: &str) -> bool { + self.debug_buffer.extend(msg.as_bytes()); + true + } + fn call_runtime( + &self, + call: ::RuntimeCall, + ) -> DispatchResultWithPostInfo { + self.runtime_calls.borrow_mut().push(call); + Ok(Default::default()) + } + fn ecdsa_recover( + &self, + signature: &[u8; 65], + message_hash: &[u8; 32], + ) -> Result<[u8; 33], ()> { + self.ecdsa_recover.borrow_mut().push((*signature, *message_hash)); + Ok([3; 33]) + } + fn sr25519_verify(&self, signature: &[u8; 64], message: &[u8], pub_key: &[u8; 32]) -> bool { + self.sr25519_verify.borrow_mut().push((*signature, message.to_vec(), *pub_key)); + true + } + fn contract_info(&mut self) -> &mut crate::ContractInfo { + unimplemented!() + } + #[cfg(feature = "runtime-benchmarks")] + fn transient_storage(&mut self) -> &mut TransientStorage { + unimplemented!() + } + fn ecdsa_to_eth_address(&self, _pk: &[u8; 33]) -> Result<[u8; 20], ()> { + Ok([2u8; 20]) + } + fn reentrance_count(&self) -> u32 { + 12 + } + fn account_reentrance_count(&self, _account_id: &AccountIdOf) -> u32 { + 12 + } + fn nonce(&mut self) -> u64 { + 995 + } + fn increment_refcount(_code_hash: CodeHash) -> DispatchResult { + Ok(()) + } + fn decrement_refcount(_code_hash: CodeHash) {} + fn lock_delegate_dependency(&mut self, code: CodeHash) -> DispatchResult { + self.delegate_dependencies.borrow_mut().insert(code); + Ok(()) + } + fn unlock_delegate_dependency(&mut self, code: &CodeHash) -> DispatchResult { + self.delegate_dependencies.borrow_mut().remove(code); + Ok(()) + } + + fn locked_delegate_dependencies_count(&mut self) -> usize { + self.delegate_dependencies.borrow().len() + } + + fn is_read_only(&self) -> bool { + false + } + } + + /// Execute the supplied code. + /// + /// Not used directly but through the wrapper functions defined below. + fn execute_internal>( + wat: &str, + input_data: Vec, + mut ext: E, + entry_point: &ExportedFunction, + unstable_interface: bool, + skip_checks: bool, + ) -> ExecResult { + type RuntimeConfig = ::T; + RuntimeConfig::set_unstable_interface(unstable_interface); + let wasm = wat::parse_str(wat).unwrap(); + let executable = if skip_checks { + WasmBlob::::from_code_unchecked( + wasm, + ext.borrow_mut().schedule(), + ALICE, + )? + } else { + WasmBlob::::from_code( + wasm, + ext.borrow_mut().schedule(), + ALICE, + Determinism::Enforced, + ) + .map_err(|err| err.0)? + }; + executable.execute(ext.borrow_mut(), entry_point, input_data) + } + + /// Execute the `call` function within the supplied code. + fn execute>(wat: &str, input_data: Vec, ext: E) -> ExecResult { + execute_internal(wat, input_data, ext, &ExportedFunction::Call, true, false) + } + + /// Execute the `deploy` function within the supplied code. + fn execute_instantiate>( + wat: &str, + input_data: Vec, + ext: E, + ) -> ExecResult { + execute_internal(wat, input_data, ext, &ExportedFunction::Constructor, true, false) + } + + /// Execute the supplied code with disabled unstable functions. + /// + /// In our test config unstable functions are disabled so that we can test them. + /// In order to test that code using them is properly rejected we temporarily disable + /// them when this test is run. + #[cfg(not(feature = "runtime-benchmarks"))] + fn execute_no_unstable>( + wat: &str, + input_data: Vec, + ext: E, + ) -> ExecResult { + execute_internal(wat, input_data, ext, &ExportedFunction::Call, false, false) + } + + /// Execute code without validating it first. + /// + /// This is mainly useful in order to test code which uses deprecated functions. Those + /// would fail when validating the code. + fn execute_unvalidated>( + wat: &str, + input_data: Vec, + ext: E, + ) -> ExecResult { + execute_internal(wat, input_data, ext, &ExportedFunction::Call, false, true) + } + + /// Execute instantiation entry point of code without validating it first. + /// + /// Same as `execute_unvalidated` except that the `deploy` entry point is ran. + #[cfg(not(feature = "runtime-benchmarks"))] + fn execute_instantiate_unvalidated>( + wat: &str, + input_data: Vec, + ext: E, + ) -> ExecResult { + execute_internal(wat, input_data, ext, &ExportedFunction::Constructor, false, true) + } + + const CODE_TRANSFER: &str = r#" +(module + ;; seal_transfer( + ;; account_ptr: u32, + ;; account_len: u32, + ;; value_ptr: u32, + ;; value_len: u32, + ;;) -> u32 + (import "seal0" "seal_transfer" (func $seal_transfer (param i32 i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + (func (export "call") + (drop + (call $seal_transfer + (i32.const 4) ;; Pointer to "account" address. + (i32.const 32) ;; Length of "account" address. + (i32.const 36) ;; Pointer to the buffer with value to transfer + (i32.const 8) ;; Length of the buffer with value to transfer. + ) + ) + ) + (func (export "deploy")) + + ;; Destination AccountId (ALICE) + (data (i32.const 4) + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + ) + + ;; Amount of value to transfer. + ;; Represented by u64 (8 bytes long) in little endian. + (data (i32.const 36) "\99\00\00\00\00\00\00\00") +) +"#; + + #[test] + fn contract_transfer() { + let mut mock_ext = MockExt::default(); + assert_ok!(execute(CODE_TRANSFER, vec![], &mut mock_ext)); + + assert_eq!(&mock_ext.transfers, &[TransferEntry { to: ALICE, value: 153 }]); + } + + const CODE_CALL: &str = r#" +(module + ;; seal_call( + ;; callee_ptr: u32, + ;; callee_len: u32, + ;; gas: u64, + ;; value_ptr: u32, + ;; value_len: u32, + ;; input_data_ptr: u32, + ;; input_data_len: u32, + ;; output_ptr: u32, + ;; output_len_ptr: u32 + ;;) -> u32 + (import "seal0" "seal_call" (func $seal_call (param i32 i32 i64 i32 i32 i32 i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + (func (export "call") + (drop + (call $seal_call + (i32.const 4) ;; Pointer to "callee" address. + (i32.const 32) ;; Length of "callee" address. + (i64.const 0) ;; How much gas to devote for the execution. 0 = all. + (i32.const 36) ;; Pointer to the buffer with value to transfer + (i32.const 8) ;; Length of the buffer with value to transfer. + (i32.const 44) ;; Pointer to input data buffer address + (i32.const 4) ;; Length of input data buffer + (i32.const 4294967295) ;; u32 max value is the sentinel value: do not copy output + (i32.const 0) ;; Length is ignored in this case + ) + ) + ) + (func (export "deploy")) + + ;; Destination AccountId (ALICE) + (data (i32.const 4) + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + ) + + ;; Amount of value to transfer. + ;; Represented by u64 (8 bytes long) in little endian. + (data (i32.const 36) "\06\00\00\00\00\00\00\00") + + (data (i32.const 44) "\01\02\03\04") +) +"#; + + #[test] + fn contract_call() { + let mut mock_ext = MockExt::default(); + assert_ok!(execute(CODE_CALL, vec![], &mut mock_ext)); + + assert_eq!( + &mock_ext.calls, + &[CallEntry { + to: ALICE, + value: 6, + data: vec![1, 2, 3, 4], + allows_reentry: true, + read_only: false + }] + ); + } + + #[test] + fn contract_delegate_call() { + const CODE: &str = r#" +(module + ;; seal_delegate_call( + ;; flags: u32, + ;; code_hash_ptr: u32, + ;; input_data_ptr: u32, + ;; input_data_len: u32, + ;; output_ptr: u32, + ;; output_len_ptr: u32 + ;;) -> u32 + (import "seal0" "seal_delegate_call" (func $seal_delegate_call (param i32 i32 i32 i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + (func (export "call") + (drop + (call $seal_delegate_call + (i32.const 0) ;; No flags are set + (i32.const 4) ;; Pointer to "callee" code_hash. + (i32.const 36) ;; Pointer to input data buffer address + (i32.const 4) ;; Length of input data buffer + (i32.const 4294967295) ;; u32 max value is the sentinel value: do not copy output + (i32.const 0) ;; Length is ignored in this case + ) + ) + ) + (func (export "deploy")) + + ;; Callee code_hash + (data (i32.const 4) + "\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11" + "\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11" + ) + + (data (i32.const 36) "\01\02\03\04") +) +"#; + let mut mock_ext = MockExt::default(); + assert_ok!(execute(CODE, vec![], &mut mock_ext)); + + assert_eq!( + &mock_ext.code_calls, + &[CallCodeEntry { code_hash: [0x11; 32].into(), data: vec![1, 2, 3, 4] }] + ); + } + + #[test] + fn contract_call_forward_input() { + const CODE: &str = r#" +(module + (import "seal1" "seal_call" (func $seal_call (param i32 i32 i64 i32 i32 i32 i32 i32) (result i32))) + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "env" "memory" (memory 1 1)) + (func (export "call") + (drop + (call $seal_call + (i32.const 1) ;; Set FORWARD_INPUT bit + (i32.const 4) ;; Pointer to "callee" address. + (i64.const 0) ;; How much gas to devote for the execution. 0 = all. + (i32.const 36) ;; Pointer to the buffer with value to transfer + (i32.const 44) ;; Pointer to input data buffer address + (i32.const 4) ;; Length of input data buffer + (i32.const 4294967295) ;; u32 max value is the sentinel value: do not copy output + (i32.const 0) ;; Length is ignored in this case + ) + ) + + ;; triggers a trap because we already forwarded the input + (call $seal_input (i32.const 1) (i32.const 44)) + ) + + (func (export "deploy")) + + ;; Destination AccountId (ALICE) + (data (i32.const 4) + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + ) + + ;; Amount of value to transfer. + ;; Represented by u64 (8 bytes long) in little endian. + (data (i32.const 36) "\2A\00\00\00\00\00\00\00") + + ;; The input is ignored because we forward our own input + (data (i32.const 44) "\01\02\03\04") +) +"#; + let mut mock_ext = MockExt::default(); + let input = vec![0xff, 0x2a, 0x99, 0x88]; + assert_err!(execute(CODE, input.clone(), &mut mock_ext), >::InputForwarded,); + + assert_eq!( + &mock_ext.calls, + &[CallEntry { + to: ALICE, + value: 0x2a, + data: input, + allows_reentry: false, + read_only: false + }] + ); + } + + #[test] + fn contract_call_clone_input() { + const CODE: &str = r#" +(module + (import "seal1" "seal_call" (func $seal_call (param i32 i32 i64 i32 i32 i32 i32 i32) (result i32))) + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + (func (export "call") + (drop + (call $seal_call + (i32.const 11) ;; Set FORWARD_INPUT | CLONE_INPUT | ALLOW_REENTRY bits + (i32.const 4) ;; Pointer to "callee" address. + (i64.const 0) ;; How much gas to devote for the execution. 0 = all. + (i32.const 36) ;; Pointer to the buffer with value to transfer + (i32.const 44) ;; Pointer to input data buffer address + (i32.const 4) ;; Length of input data buffer + (i32.const 4294967295) ;; u32 max value is the sentinel value: do not copy output + (i32.const 0) ;; Length is ignored in this case + ) + ) + + ;; works because the input was cloned + (call $seal_input (i32.const 0) (i32.const 44)) + + ;; return the input to caller for inspection + (call $seal_return (i32.const 0) (i32.const 0) (i32.load (i32.const 44))) + ) + + (func (export "deploy")) + + ;; Destination AccountId (ALICE) + (data (i32.const 4) + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + ) + + ;; Amount of value to transfer. + ;; Represented by u64 (8 bytes long) in little endian. + (data (i32.const 36) "\2A\00\00\00\00\00\00\00") + + ;; The input is ignored because we forward our own input + (data (i32.const 44) "\01\02\03\04") +) +"#; + let mut mock_ext = MockExt::default(); + let input = vec![0xff, 0x2a, 0x99, 0x88]; + let result = execute(CODE, input.clone(), &mut mock_ext).unwrap(); + assert_eq!(result.data, input); + assert_eq!( + &mock_ext.calls, + &[CallEntry { + to: ALICE, + value: 0x2a, + data: input, + allows_reentry: true, + read_only: false + }] + ); + } + + #[test] + fn contract_call_tail_call() { + const CODE: &str = r#" +(module + (import "seal1" "seal_call" (func $seal_call (param i32 i32 i64 i32 i32 i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + (func (export "call") + (drop + (call $seal_call + (i32.const 5) ;; Set FORWARD_INPUT | TAIL_CALL bit + (i32.const 4) ;; Pointer to "callee" address. + (i64.const 0) ;; How much gas to devote for the execution. 0 = all. + (i32.const 36) ;; Pointer to the buffer with value to transfer + (i32.const 0) ;; Pointer to input data buffer address + (i32.const 0) ;; Length of input data buffer + (i32.const 4294967295) ;; u32 max value is the sentinel value: do not copy output + (i32.const 0) ;; Length is ignored in this case + ) + ) + + ;; a tail call never returns + (unreachable) + ) + + (func (export "deploy")) + + ;; Destination AccountId (ALICE) + (data (i32.const 4) + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + ) + + ;; Amount of value to transfer. + ;; Represented by u64 (8 bytes long) in little endian. + (data (i32.const 36) "\2A\00\00\00\00\00\00\00") +) +"#; + let mut mock_ext = MockExt::default(); + let input = vec![0xff, 0x2a, 0x99, 0x88]; + let result = execute(CODE, input.clone(), &mut mock_ext).unwrap(); + assert_eq!(result.data, call_return_data()); + assert_eq!( + &mock_ext.calls, + &[CallEntry { + to: ALICE, + value: 0x2a, + data: input, + allows_reentry: false, + read_only: false + }] + ); + } + + #[test] + fn contains_storage_works() { + const CODE: &str = r#" +(module + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "seal1" "contains_storage" (func $contains_storage (param i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + + + ;; size of input buffer + ;; [0, 4) size of input buffer (128+32 = 160 bytes = 0xA0) + (data (i32.const 0) "\A0") + + ;; [4, 164) input buffer + + (func (export "call") + ;; Receive key + (call $seal_input + (i32.const 4) ;; Where we take input and store it + (i32.const 0) ;; Where we take and store the length of the data + ) + ;; Call seal_clear_storage and save what it returns at 0 + (i32.store (i32.const 0) + (call $contains_storage + (i32.const 8) ;; key_ptr + (i32.load (i32.const 4)) ;; key_len + ) + ) + (call $seal_return + (i32.const 0) ;; flags + (i32.const 0) ;; returned value + (i32.const 4) ;; length of returned value + ) + ) + + (func (export "deploy")) +) +"#; + + let mut ext = MockExt::default(); + ext.set_storage( + &Key::::try_from_var([1u8; 64].to_vec()).unwrap(), + Some(vec![42u8]), + false, + ) + .unwrap(); + ext.set_storage( + &Key::::try_from_var([2u8; 19].to_vec()).unwrap(), + Some(vec![]), + false, + ) + .unwrap(); + + //value does not exist (wrong key length) + let input = (63, [1u8; 64]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + // sentinel returned + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), crate::SENTINEL); + + // value exists + let input = (64, [1u8; 64]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + // true as u32 returned + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), 1); + // getter does not remove the value from storage + assert_eq!(ext.storage.get(&[1u8; 64].to_vec()).unwrap(), &[42u8]); + + // value exists (test for 0 sized) + let input = (19, [2u8; 19]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + // true as u32 returned + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), 0); + // getter does not remove the value from storage + assert_eq!(ext.storage.get(&[2u8; 19].to_vec()).unwrap(), &([] as [u8; 0])); + } + + const CODE_INSTANTIATE: &str = r#" +(module + ;; seal_instantiate( + ;; code_ptr: u32, + ;; code_len: u32, + ;; gas: u64, + ;; value_ptr: u32, + ;; value_len: u32, + ;; input_data_ptr: u32, + ;; input_data_len: u32, + ;; input_data_len: u32, + ;; address_ptr: u32, + ;; address_len_ptr: u32, + ;; output_ptr: u32, + ;; output_len_ptr: u32 + ;; ) -> u32 + (import "seal0" "seal_instantiate" (func $seal_instantiate + (param i32 i32 i64 i32 i32 i32 i32 i32 i32 i32 i32 i32 i32) (result i32) + )) + (import "env" "memory" (memory 1 1)) + (func (export "call") + (drop + (call $seal_instantiate + (i32.const 16) ;; Pointer to `code_hash` + (i32.const 32) ;; Length of `code_hash` + (i64.const 0) ;; How much gas to devote for the execution. 0 = all. + (i32.const 4) ;; Pointer to the buffer with value to transfer + (i32.const 8) ;; Length of the buffer with value to transfer + (i32.const 12) ;; Pointer to input data buffer address + (i32.const 4) ;; Length of input data buffer + (i32.const 4294967295) ;; u32 max value is the sentinel value: do not copy address + (i32.const 0) ;; Length is ignored in this case + (i32.const 4294967295) ;; u32 max value is the sentinel value: do not copy output + (i32.const 0) ;; Length is ignored in this case + (i32.const 0) ;; salt_ptr + (i32.const 4) ;; salt_len + ) + ) + ) + (func (export "deploy")) + + ;; Salt + (data (i32.const 0) "\42\43\44\45") + ;; Amount of value to transfer. + ;; Represented by u64 (8 bytes long) in little endian. + (data (i32.const 4) "\03\00\00\00\00\00\00\00") + ;; Input data to pass to the contract being instantiated. + (data (i32.const 12) "\01\02\03\04") + ;; Hash of code. + (data (i32.const 16) + "\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11" + "\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11" + ) +) +"#; + + #[test] + fn contract_instantiate() { + let mut mock_ext = MockExt::default(); + assert_ok!(execute(CODE_INSTANTIATE, vec![], &mut mock_ext)); + + assert_matches!( + &mock_ext.instantiates[..], + [InstantiateEntry { + code_hash, + value: 3, + data, + gas_left: _, + salt, + }] if + code_hash == &[0x11; 32].into() && + data == &vec![1, 2, 3, 4] && + salt == &vec![0x42, 0x43, 0x44, 0x45] + ); + } + + const CODE_TERMINATE: &str = r#" +(module + ;; seal_terminate( + ;; beneficiary_ptr: u32, + ;; beneficiary_len: u32, + ;; ) + (import "seal0" "seal_terminate" (func $seal_terminate (param i32 i32))) + (import "env" "memory" (memory 1 1)) + (func (export "call") + (call $seal_terminate + (i32.const 4) ;; Pointer to "beneficiary" address. + (i32.const 32) ;; Length of "beneficiary" address. + ) + ) + (func (export "deploy")) + + ;; Beneficiary AccountId to transfer the funds. + (data (i32.const 4) + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + ) +) +"#; + + #[test] + fn contract_terminate() { + let mut mock_ext = MockExt::default(); + execute(CODE_TERMINATE, vec![], &mut mock_ext).unwrap(); + + assert_eq!(&mock_ext.terminations, &[TerminationEntry { beneficiary: ALICE }]); + } + + const CODE_TRANSFER_LIMITED_GAS: &str = r#" +(module + ;; seal_call( + ;; callee_ptr: u32, + ;; callee_len: u32, + ;; gas: u64, + ;; value_ptr: u32, + ;; value_len: u32, + ;; input_data_ptr: u32, + ;; input_data_len: u32, + ;; output_ptr: u32, + ;; output_len_ptr: u32 + ;;) -> u32 + (import "seal0" "seal_call" (func $seal_call (param i32 i32 i64 i32 i32 i32 i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + (func (export "call") + (drop + (call $seal_call + (i32.const 4) ;; Pointer to "callee" address. + (i32.const 32) ;; Length of "callee" address. + (i64.const 228) ;; How much gas to devote for the execution. + (i32.const 36) ;; Pointer to the buffer with value to transfer + (i32.const 8) ;; Length of the buffer with value to transfer. + (i32.const 44) ;; Pointer to input data buffer address + (i32.const 4) ;; Length of input data buffer + (i32.const 4294967295) ;; u32 max value is the sentinel value: do not copy output + (i32.const 0) ;; Length is ignored in this cas + ) + ) + ) + (func (export "deploy")) + + ;; Destination AccountId to transfer the funds. + (data (i32.const 4) + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + ) + ;; Amount of value to transfer. + ;; Represented by u64 (8 bytes long) in little endian. + (data (i32.const 36) "\06\00\00\00\00\00\00\00") + + (data (i32.const 44) "\01\02\03\04") +) +"#; + + #[test] + fn contract_call_limited_gas() { + let mut mock_ext = MockExt::default(); + assert_ok!(execute(&CODE_TRANSFER_LIMITED_GAS, vec![], &mut mock_ext)); + + assert_eq!( + &mock_ext.calls, + &[CallEntry { + to: ALICE, + value: 6, + data: vec![1, 2, 3, 4], + allows_reentry: true, + read_only: false + }] + ); + } + + const CODE_ECDSA_RECOVER: &str = r#" +(module + ;; seal_ecdsa_recover( + ;; signature_ptr: u32, + ;; message_hash_ptr: u32, + ;; output_ptr: u32 + ;; ) -> u32 + (import "seal0" "seal_ecdsa_recover" (func $seal_ecdsa_recover (param i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + (func (export "call") + (drop + (call $seal_ecdsa_recover + (i32.const 36) ;; Pointer to signature. + (i32.const 4) ;; Pointer to message hash. + (i32.const 36) ;; Pointer for output - public key. + ) + ) + ) + (func (export "deploy")) + + ;; Hash of message. + (data (i32.const 4) + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + ) + ;; Signature + (data (i32.const 36) + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01" + ) +) +"#; + + #[test] + fn contract_ecdsa_recover() { + let mut mock_ext = MockExt::default(); + assert_ok!(execute(&CODE_ECDSA_RECOVER, vec![], &mut mock_ext)); + assert_eq!(mock_ext.ecdsa_recover.into_inner(), [([1; 65], [1; 32])]); + } + + #[test] + fn contract_ecdsa_to_eth_address() { + /// calls `seal_ecdsa_to_eth_address` for the constant and ensures the result equals the + /// expected one. + const CODE_ECDSA_TO_ETH_ADDRESS: &str = r#" +(module + (import "seal0" "seal_ecdsa_to_eth_address" (func $seal_ecdsa_to_eth_address (param i32 i32) (result i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + (func (export "call") + ;; fill the buffer with the eth address. + (call $seal_ecdsa_to_eth_address (i32.const 0) (i32.const 0)) + + ;; Return the contents of the buffer + (call $seal_return + (i32.const 0) + (i32.const 0) + (i32.const 20) + ) + + ;; seal_return doesn't return, so this is effectively unreachable. + (unreachable) + ) + (func (export "deploy")) +) +"#; + + let output = execute(CODE_ECDSA_TO_ETH_ADDRESS, vec![], MockExt::default()).unwrap(); + assert_eq!( + output, + ExecReturnValue { flags: ReturnFlags::empty(), data: [0x02; 20].to_vec() } + ); + } + + #[test] + fn contract_sr25519() { + const CODE_SR25519: &str = r#" +(module + (import "seal0" "sr25519_verify" (func $sr25519_verify (param i32 i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + (func (export "call") + (drop + (call $sr25519_verify + (i32.const 0) ;; Pointer to signature. + (i32.const 64) ;; Pointer to public key. + (i32.const 16) ;; message length. + (i32.const 96) ;; Pointer to message. + ) + ) + ) + (func (export "deploy")) + + ;; Signature (64 bytes) + (data (i32.const 0) + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + ) + + ;; public key (32 bytes) + (data (i32.const 64) + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + ) + + ;; message. (16 bytes) + (data (i32.const 96) + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + ) +) +"#; + let mut mock_ext = MockExt::default(); + assert_ok!(execute(&CODE_SR25519, vec![], &mut mock_ext)); + assert_eq!(mock_ext.sr25519_verify.into_inner(), [([1; 64], [1; 16].to_vec(), [1; 32])]); + } + + const CODE_GET_STORAGE: &str = r#" +(module + (import "seal0" "seal_get_storage" (func $seal_get_storage (param i32 i32 i32) (result i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; [0, 32) key for get storage + (data (i32.const 0) + "\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11" + "\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11" + ) + + ;; [32, 36) buffer size = 4k in little endian + (data (i32.const 32) "\00\10") + + ;; [36; inf) buffer where the result is copied + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + (local $buf_size i32) + + ;; Load a storage value into contract memory. + (call $assert + (i32.eq + (call $seal_get_storage + (i32.const 0) ;; The pointer to the storage key to fetch + (i32.const 36) ;; Pointer to the output buffer + (i32.const 32) ;; Pointer to the size of the buffer + ) + + ;; Return value 0 means that the value is found and there were + ;; no errors. + (i32.const 0) + ) + ) + + ;; Find out the size of the buffer + (local.set $buf_size + (i32.load (i32.const 32)) + ) + + ;; Return the contents of the buffer + (call $seal_return + (i32.const 0) + (i32.const 36) + (local.get $buf_size) + ) + + ;; env:seal_return doesn't return, so this is effectively unreachable. + (unreachable) + ) + + (func (export "deploy")) +) +"#; + + #[test] + fn get_storage_puts_data_into_buf() { + let mut mock_ext = MockExt::default(); + mock_ext.storage.insert([0x11; 32].to_vec(), [0x22; 32].to_vec()); + + let output = execute(CODE_GET_STORAGE, vec![], mock_ext).unwrap(); + + assert_eq!( + output, + ExecReturnValue { flags: ReturnFlags::empty(), data: [0x22; 32].to_vec() } + ); + } + + /// calls `seal_caller` and compares the result with the constant (ALICE's address part). + const CODE_CALLER: &str = r#" +(module + (import "seal0" "seal_caller" (func $seal_caller (param i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; size of our buffer is 32 bytes + (data (i32.const 32) "\20") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + ;; fill the buffer with the caller. + (call $seal_caller (i32.const 0) (i32.const 32)) + + ;; assert len == 32 + (call $assert + (i32.eq + (i32.load (i32.const 32)) + (i32.const 32) + ) + ) + + ;; assert that the first 8 bytes are the beginning of "ALICE" + (call $assert + (i64.eq + (i64.load (i32.const 0)) + (i64.const 0x0101010101010101) + ) + ) + ) + + (func (export "deploy")) +) +"#; + + #[test] + fn caller() { + assert_ok!(execute(CODE_CALLER, vec![], MockExt::default())); + } + + #[test] + fn caller_traps_when_no_account_id() { + let mut ext = MockExt::default(); + ext.caller = Origin::Root; + assert_eq!( + execute(CODE_CALLER, vec![], ext), + Err(ExecError { error: DispatchError::RootNotAllowed, origin: ErrorOrigin::Caller }) + ); + } + + /// calls `seal_address` and compares the result with the constant (BOB's address part). + const CODE_ADDRESS: &str = r#" +(module + (import "seal0" "seal_address" (func $seal_address (param i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; size of our buffer is 32 bytes + (data (i32.const 32) "\20") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + ;; fill the buffer with the self address. + (call $seal_address (i32.const 0) (i32.const 32)) + + ;; assert size == 32 + (call $assert + (i32.eq + (i32.load (i32.const 32)) + (i32.const 32) + ) + ) + + ;; assert that the first 8 bytes are the beginning of "BOB" + (call $assert + (i64.eq + (i64.load (i32.const 0)) + (i64.const 0x0202020202020202) + ) + ) + ) + + (func (export "deploy")) +) +"#; + + #[test] + fn address() { + assert_ok!(execute(CODE_ADDRESS, vec![], MockExt::default())); + } + + const CODE_BALANCE: &str = r#" +(module + (import "seal0" "seal_balance" (func $seal_balance (param i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; size of our buffer is 32 bytes + (data (i32.const 32) "\20") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + ;; This stores the balance in the buffer + (call $seal_balance (i32.const 0) (i32.const 32)) + + ;; assert len == 8 + (call $assert + (i32.eq + (i32.load (i32.const 32)) + (i32.const 8) + ) + ) + + ;; assert that contents of the buffer is equal to the i64 value of 228. + (call $assert + (i64.eq + (i64.load (i32.const 0)) + (i64.const 228) + ) + ) + ) + (func (export "deploy")) +) +"#; + + #[test] + fn balance() { + assert_ok!(execute(CODE_BALANCE, vec![], MockExt::default())); + } + + const CODE_GAS_PRICE: &str = r#" +(module + (import "seal1" "weight_to_fee" (func $seal_weight_to_fee (param i64 i64 i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; size of our buffer is 32 bytes + (data (i32.const 32) "\20") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + ;; This stores the gas price in the buffer + (call $seal_weight_to_fee (i64.const 2) (i64.const 1) (i32.const 0) (i32.const 32)) + + ;; assert len == 8 + (call $assert + (i32.eq + (i32.load (i32.const 32)) + (i32.const 8) + ) + ) + + ;; assert that contents of the buffer is equal to the i64 value of 2 * 1312 + 103 = 2727. + (call $assert + (i64.eq + (i64.load (i32.const 0)) + (i64.const 2727) + ) + ) + ) + (func (export "deploy")) +) +"#; + + #[test] + fn gas_price() { + assert_ok!(execute(CODE_GAS_PRICE, vec![], MockExt::default())); + } + + const CODE_GAS_LEFT: &str = r#" +(module + (import "seal1" "gas_left" (func $seal_gas_left (param i32 i32))) + (import "seal0" "clear_storage" (func $clear_storage (param i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; Make output buffer size 20 bytes + (data (i32.const 20) "\14") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + ;; Burn some PoV, clear_storage consumes some PoV as in order to clear the storage we need to we need to read its size first. + (call $clear_storage (i32.const 0)) + + ;; This stores the weight left to the buffer + (call $seal_gas_left (i32.const 0) (i32.const 20)) + + ;; Assert len <= 16 (max encoded Weight len) + (call $assert + (i32.le_u + (i32.load (i32.const 20)) + (i32.const 16) + ) + ) + + ;; Burn some PoV, clear_storage consumes some PoV as in order to clear the storage we need to we need to read its size first. + (call $clear_storage (i32.const 0)) + + ;; Return weight left and its encoded value len + (call $seal_return (i32.const 0) (i32.const 0) (i32.load (i32.const 20))) + + (unreachable) + ) + (func (export "deploy")) +) +"#; + + #[test] + fn gas_left() { + let mut ext = MockExt::default(); + let gas_limit = ext.gas_meter.gas_left(); + + let output = execute(CODE_GAS_LEFT, vec![], &mut ext).unwrap(); + + let weight_left = Weight::decode(&mut &*output.data).unwrap(); + let actual_left = ext.gas_meter.gas_left(); + + assert!(weight_left.all_lt(gas_limit), "gas_left must be less than initial"); + assert!(weight_left.all_gt(actual_left), "gas_left must be greater than final"); + } + + const CODE_VALUE_TRANSFERRED: &str = r#" +(module + (import "seal0" "seal_value_transferred" (func $seal_value_transferred (param i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; size of our buffer is 32 bytes + (data (i32.const 32) "\20") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + ;; This stores the value transferred in the buffer + (call $seal_value_transferred (i32.const 0) (i32.const 32)) + + ;; assert len == 8 + (call $assert + (i32.eq + (i32.load (i32.const 32)) + (i32.const 8) + ) + ) + + ;; assert that contents of the buffer is equal to the i64 value of 1337. + (call $assert + (i64.eq + (i64.load (i32.const 0)) + (i64.const 1337) + ) + ) + ) + (func (export "deploy")) +) +"#; + + #[test] + fn value_transferred() { + assert_ok!(execute(CODE_VALUE_TRANSFERRED, vec![], MockExt::default())); + } + + const START_FN_DOES_RUN: &str = r#" +(module + (import "seal0" "seal_deposit_event" (func $seal_deposit_event (param i32 i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + (start $start) + (func $start + (call $seal_deposit_event + (i32.const 0) ;; Pointer to the start of topics buffer + (i32.const 0) ;; The length of the topics buffer. + (i32.const 0) ;; Pointer to the start of the data buffer + (i32.const 13) ;; Length of the buffer + ) + ) + + (func (export "call")) + + (func (export "deploy")) + + (data (i32.const 0) "\00\01\2A\00\00\00\00\00\00\00\E5\14\00") +) +"#; + + #[test] + fn start_fn_does_run_on_call() { + let mut ext = MockExt::default(); + execute(START_FN_DOES_RUN, vec![], &mut ext).unwrap(); + assert_eq!( + ext.events[0].1, + [0x00_u8, 0x01, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe5, 0x14, 0x00] + ); + } + + #[test] + fn start_fn_does_run_on_deploy() { + let mut ext = MockExt::default(); + execute_instantiate(START_FN_DOES_RUN, vec![], &mut ext).unwrap(); + assert_eq!( + ext.events[0].1, + [0x00_u8, 0x01, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe5, 0x14, 0x00] + ); + } + + const CODE_TIMESTAMP_NOW: &str = r#" +(module + (import "seal0" "seal_now" (func $seal_now (param i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; size of our buffer is 32 bytes + (data (i32.const 32) "\20") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + ;; This stores the block timestamp in the buffer + (call $seal_now (i32.const 0) (i32.const 32)) + + ;; assert len == 8 + (call $assert + (i32.eq + (i32.load (i32.const 32)) + (i32.const 8) + ) + ) + + ;; assert that contents of the buffer is equal to the i64 value of 1111. + (call $assert + (i64.eq + (i64.load (i32.const 0)) + (i64.const 1111) + ) + ) + ) + (func (export "deploy")) +) +"#; + + const CODE_TIMESTAMP_NOW_UNPREFIXED: &str = r#" +(module + (import "seal0" "now" (func $now (param i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; size of our buffer is 32 bytes + (data (i32.const 32) "\20") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + ;; This stores the block timestamp in the buffer + (call $now (i32.const 0) (i32.const 32)) + + ;; assert len == 8 + (call $assert + (i32.eq + (i32.load (i32.const 32)) + (i32.const 8) + ) + ) + + ;; assert that contents of the buffer is equal to the i64 value of 1111. + (call $assert + (i64.eq + (i64.load (i32.const 0)) + (i64.const 1111) + ) + ) + ) + (func (export "deploy")) +) +"#; + + #[test] + fn now() { + assert_ok!(execute(CODE_TIMESTAMP_NOW, vec![], MockExt::default())); + assert_ok!(execute(CODE_TIMESTAMP_NOW_UNPREFIXED, vec![], MockExt::default())); + } + + const CODE_MINIMUM_BALANCE: &str = r#" +(module + (import "seal0" "seal_minimum_balance" (func $seal_minimum_balance (param i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; size of our buffer is 32 bytes + (data (i32.const 32) "\20") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + (call $seal_minimum_balance (i32.const 0) (i32.const 32)) + + ;; assert len == 8 + (call $assert + (i32.eq + (i32.load (i32.const 32)) + (i32.const 8) + ) + ) + + ;; assert that contents of the buffer is equal to the i64 value of 666. + (call $assert + (i64.eq + (i64.load (i32.const 0)) + (i64.const 666) + ) + ) + ) + (func (export "deploy")) +) +"#; + + #[test] + fn minimum_balance() { + assert_ok!(execute(CODE_MINIMUM_BALANCE, vec![], MockExt::default())); + } + + const CODE_RANDOM: &str = r#" +(module + (import "seal0" "seal_random" (func $seal_random (param i32 i32 i32 i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; [0,128) is reserved for the result of PRNG. + + ;; the subject used for the PRNG. [128,160) + (data (i32.const 128) + "\00\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F" + "\00\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F" + ) + + ;; size of our buffer is 128 bytes + (data (i32.const 160) "\80") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + ;; This stores the block random seed in the buffer + (call $seal_random + (i32.const 128) ;; Pointer in memory to the start of the subject buffer + (i32.const 32) ;; The subject buffer's length + (i32.const 0) ;; Pointer to the output buffer + (i32.const 160) ;; Pointer to the output buffer length + ) + + ;; assert len == 32 + (call $assert + (i32.eq + (i32.load (i32.const 160)) + (i32.const 32) + ) + ) + + ;; return the random data + (call $seal_return + (i32.const 0) + (i32.const 0) + (i32.const 32) + ) + ) + (func (export "deploy")) +) +"#; + + #[test] + fn random() { + let output = execute_unvalidated(CODE_RANDOM, vec![], MockExt::default()).unwrap(); + + // The mock ext just returns the same data that was passed as the subject. + assert_eq!( + output, + ExecReturnValue { + flags: ReturnFlags::empty(), + data: array_bytes::hex_into_unchecked( + "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F" + ) + }, + ); + } + + const CODE_RANDOM_V1: &str = r#" +(module + (import "seal1" "seal_random" (func $seal_random (param i32 i32 i32 i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; [0,128) is reserved for the result of PRNG. + + ;; the subject used for the PRNG. [128,160) + (data (i32.const 128) + "\00\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F" + "\00\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F" + ) + + ;; size of our buffer is 128 bytes + (data (i32.const 160) "\80") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + ;; This stores the block random seed in the buffer + (call $seal_random + (i32.const 128) ;; Pointer in memory to the start of the subject buffer + (i32.const 32) ;; The subject buffer's length + (i32.const 0) ;; Pointer to the output buffer + (i32.const 160) ;; Pointer to the output buffer length + ) + + ;; assert len == 32 + (call $assert + (i32.eq + (i32.load (i32.const 160)) + (i32.const 40) + ) + ) + + ;; return the random data + (call $seal_return + (i32.const 0) + (i32.const 0) + (i32.const 40) + ) + ) + (func (export "deploy")) +) +"#; + + #[test] + fn random_v1() { + let output = execute_unvalidated(CODE_RANDOM_V1, vec![], MockExt::default()).unwrap(); + + // The mock ext just returns the same data that was passed as the subject. + assert_eq!( + output, + ExecReturnValue { + flags: ReturnFlags::empty(), + data: ( + array_bytes::hex2array_unchecked::<_, 32>( + "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F" + ), + 42u64, + ) + .encode() + }, + ); + } + + const CODE_DEPOSIT_EVENT: &str = r#" +(module + (import "seal0" "seal_deposit_event" (func $seal_deposit_event (param i32 i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + (func (export "call") + (call $seal_deposit_event + (i32.const 32) ;; Pointer to the start of topics buffer + (i32.const 33) ;; The length of the topics buffer. + (i32.const 8) ;; Pointer to the start of the data buffer + (i32.const 13) ;; Length of the buffer + ) + ) + (func (export "deploy")) + + (data (i32.const 8) "\00\01\2A\00\00\00\00\00\00\00\E5\14\00") + + ;; Encoded Vec>, the buffer has length of 33 bytes. + (data (i32.const 32) "\04\33\33\33\33\33\33\33\33\33\33\33\33\33\33\33\33\33\33\33\33\33\33\33" + "\33\33\33\33\33\33\33\33\33") +) +"#; + + #[test] + fn deposit_event() { + let mut mock_ext = MockExt::default(); + assert_ok!(execute(CODE_DEPOSIT_EVENT, vec![], &mut mock_ext)); + + assert_eq!( + mock_ext.events, + vec![( + vec![H256::repeat_byte(0x33)], + vec![0x00, 0x01, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe5, 0x14, 0x00] + )] + ); + + assert!(mock_ext.gas_meter.gas_left().ref_time() > 0); + } + + const CODE_DEPOSIT_EVENT_DUPLICATES: &str = r#" +(module + (import "seal0" "seal_deposit_event" (func $seal_deposit_event (param i32 i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + (func (export "call") + (call $seal_deposit_event + (i32.const 32) ;; Pointer to the start of topics buffer + (i32.const 129) ;; The length of the topics buffer. + (i32.const 8) ;; Pointer to the start of the data buffer + (i32.const 13) ;; Length of the buffer + ) + ) + (func (export "deploy")) + + (data (i32.const 8) "\00\01\2A\00\00\00\00\00\00\00\E5\14\00") + + ;; Encoded Vec>, the buffer has length of 129 bytes. + (data (i32.const 32) "\10" +"\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" +"\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02" +"\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" +"\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04") +) +"#; + + /// Checks that the runtime allows duplicate topics. + #[test] + fn deposit_event_duplicates_allowed() { + let mut mock_ext = MockExt::default(); + assert_ok!(execute(CODE_DEPOSIT_EVENT_DUPLICATES, vec![], &mut mock_ext,)); + + assert_eq!( + mock_ext.events, + vec![( + vec![ + H256::repeat_byte(0x01), + H256::repeat_byte(0x02), + H256::repeat_byte(0x01), + H256::repeat_byte(0x04) + ], + vec![0x00, 0x01, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe5, 0x14, 0x00] + )] + ); + } + + const CODE_DEPOSIT_EVENT_MAX_TOPICS: &str = r#" +(module + (import "seal0" "seal_deposit_event" (func $seal_deposit_event (param i32 i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + (func (export "call") + (call $seal_deposit_event + (i32.const 32) ;; Pointer to the start of topics buffer + (i32.const 161) ;; The length of the topics buffer. + (i32.const 8) ;; Pointer to the start of the data buffer + (i32.const 13) ;; Length of the buffer + ) + ) + (func (export "deploy")) + + (data (i32.const 8) "\00\01\2A\00\00\00\00\00\00\00\E5\14\00") + + ;; Encoded Vec>, the buffer has length of 161 bytes. + (data (i32.const 32) "\14" +"\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" +"\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02" +"\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03\03" +"\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04\04" +"\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05") +) +"#; + + /// Checks that the runtime traps if there are more than `max_topic_events` topics. + #[test] + fn deposit_event_max_topics() { + assert_eq!( + execute(CODE_DEPOSIT_EVENT_MAX_TOPICS, vec![], MockExt::default(),), + Err(ExecError { + error: Error::::TooManyTopics.into(), + origin: ErrorOrigin::Caller, + }) + ); + } + + /// calls `seal_block_number` compares the result with the constant 121. + const CODE_BLOCK_NUMBER: &str = r#" +(module + (import "seal0" "seal_block_number" (func $seal_block_number (param i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; size of our buffer is 32 bytes + (data (i32.const 32) "\20") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + ;; This stores the block height in the buffer + (call $seal_block_number (i32.const 0) (i32.const 32)) + + ;; assert len == 8 + (call $assert + (i32.eq + (i32.load (i32.const 32)) + (i32.const 8) + ) + ) + + ;; assert that contents of the buffer is equal to the i64 value of 121. + (call $assert + (i64.eq + (i64.load (i32.const 0)) + (i64.const 121) + ) + ) + ) + + (func (export "deploy")) +) +"#; + + #[test] + fn block_number() { + let _ = execute(CODE_BLOCK_NUMBER, vec![], MockExt::default()).unwrap(); + } + + const CODE_RETURN_WITH_DATA: &str = r#" +(module + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + (data (i32.const 32) "\20") + + ;; Deploy routine is the same as call. + (func (export "deploy") + (call $call) + ) + + ;; Call reads the first 4 bytes (LE) as the exit status and returns the rest as output data. + (func $call (export "call") + ;; Copy input data this contract memory. + (call $seal_input + (i32.const 0) ;; Pointer where to store input + (i32.const 32) ;; Pointer to the length of the buffer + ) + + ;; Copy all but the first 4 bytes of the input data as the output data. + (call $seal_return + (i32.load (i32.const 0)) + (i32.const 4) + (i32.sub (i32.load (i32.const 32)) (i32.const 4)) + ) + (unreachable) + ) +) +"#; + + #[test] + fn seal_return_with_success_status() { + let output = execute( + CODE_RETURN_WITH_DATA, + array_bytes::hex2bytes_unchecked("00000000445566778899"), + MockExt::default(), + ) + .unwrap(); + + assert_eq!( + output, + ExecReturnValue { + flags: ReturnFlags::empty(), + data: array_bytes::hex2bytes_unchecked("445566778899"), + } + ); + assert!(!output.did_revert()); + } + + #[test] + fn return_with_revert_status() { + let output = execute( + CODE_RETURN_WITH_DATA, + array_bytes::hex2bytes_unchecked("010000005566778899"), + MockExt::default(), + ) + .unwrap(); + + assert_eq!( + output, + ExecReturnValue { + flags: ReturnFlags::REVERT, + data: array_bytes::hex2bytes_unchecked("5566778899"), + } + ); + assert!(output.did_revert()); + } + + const CODE_OUT_OF_BOUNDS_ACCESS: &str = r#" +(module + (import "seal0" "seal_terminate" (func $seal_terminate (param i32 i32))) + (import "env" "memory" (memory 1 1)) + + (func (export "deploy")) + + (func (export "call") + (call $seal_terminate + (i32.const 65536) ;; Pointer to "account" address (out of bound). + (i32.const 8) ;; Length of "account" address. + ) + ) +) +"#; + + #[test] + fn contract_out_of_bounds_access() { + let mut mock_ext = MockExt::default(); + let result = execute(CODE_OUT_OF_BOUNDS_ACCESS, vec![], &mut mock_ext); + + assert_eq!( + result, + Err(ExecError { + error: Error::::DecodingFailed.into(), + origin: ErrorOrigin::Caller, + }) + ); + } + + const CODE_DECODE_FAILURE: &str = r#" +(module + (import "seal0" "seal_terminate" (func $seal_terminate (param i32 i32))) + (import "env" "memory" (memory 1 1)) + + (func (export "deploy")) + + (func (export "call") + (call $seal_terminate + (i32.const 0) ;; Pointer to "account" address. + (i32.const 4) ;; Length of "account" address (too small -> decode fail). + ) + ) +) +"#; + + #[test] + fn contract_decode_length_ignored() { + let mut mock_ext = MockExt::default(); + let result = execute(CODE_DECODE_FAILURE, vec![], &mut mock_ext); + // AccountID implements `MaxEncodeLen` and therefore the supplied length is + // no longer needed nor used to determine how much is read from contract memory. + assert_ok!(result); + } + + #[test] + fn debug_message_works() { + const CODE_DEBUG_MESSAGE: &str = r#" +(module + (import "seal0" "seal_debug_message" (func $seal_debug_message (param i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + + (data (i32.const 0) "Hello World!") + + (func (export "call") + (call $seal_debug_message + (i32.const 0) ;; Pointer to the text buffer + (i32.const 12) ;; The size of the buffer + ) + drop + ) + + (func (export "deploy")) +) +"#; + let mut ext = MockExt::default(); + execute(CODE_DEBUG_MESSAGE, vec![], &mut ext).unwrap(); + + assert_eq!(std::str::from_utf8(&ext.debug_buffer).unwrap(), "Hello World!"); + } + + #[test] + fn debug_message_invalid_utf8_fails() { + const CODE_DEBUG_MESSAGE_FAIL: &str = r#" +(module + (import "seal0" "seal_debug_message" (func $seal_debug_message (param i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + + (data (i32.const 0) "\fc") + + (func (export "call") + (call $seal_debug_message + (i32.const 0) ;; Pointer to the text buffer + (i32.const 1) ;; The size of the buffer + ) + drop + ) + + (func (export "deploy")) +) +"#; + let mut ext = MockExt::default(); + let result = execute(CODE_DEBUG_MESSAGE_FAIL, vec![], &mut ext); + assert_ok!(result); + assert!(ext.debug_buffer.is_empty()); + } + + const CODE_CALL_RUNTIME: &str = r#" +(module + (import "seal0" "call_runtime" (func $call_runtime (param i32 i32) (result i32))) + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; 0x1000 = 4k in little endian + ;; size of input buffer + (data (i32.const 0) "\00\10") + + (func (export "call") + ;; Receive the encoded call + (call $seal_input + (i32.const 4) ;; Pointer to the input buffer + (i32.const 0) ;; Size of the length buffer + ) + ;; Just use the call passed as input and store result to memory + (i32.store (i32.const 0) + (call $call_runtime + (i32.const 4) ;; Pointer where the call is stored + (i32.load (i32.const 0)) ;; Size of the call + ) + ) + (call $seal_return + (i32.const 0) ;; flags + (i32.const 0) ;; returned value + (i32.const 4) ;; length of returned value + ) + ) + + (func (export "deploy")) +) +"#; + + #[test] + fn call_runtime_works() { + let call = + RuntimeCall::System(frame_system::Call::remark { remark: b"Hello World".to_vec() }); + let mut ext = MockExt::default(); + let result = execute(CODE_CALL_RUNTIME, call.encode(), &mut ext).unwrap(); + assert_eq!(*ext.runtime_calls.borrow(), vec![call]); + // 0 = ReturnCode::Success + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), 0); + } + + #[test] + fn call_runtime_panics_on_invalid_call() { + let mut ext = MockExt::default(); + let result = execute(CODE_CALL_RUNTIME, vec![0x42], &mut ext); + assert_eq!( + result, + Err(ExecError { + error: Error::::DecodingFailed.into(), + origin: ErrorOrigin::Caller, + }) + ); + assert_eq!(*ext.runtime_calls.borrow(), vec![]); + } + + #[test] + fn set_storage_works() { + const CODE: &str = r#" +(module + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "seal2" "set_storage" (func $set_storage (param i32 i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + + ;; [0, 4) size of input buffer + ;; 4k in little endian + (data (i32.const 0) "\00\10") + + ;; [4, 4100) input buffer + + (func (export "call") + ;; Receive (key ++ value_to_write) + (call $seal_input + (i32.const 4) ;; Pointer to the input buffer + (i32.const 0) ;; Size of the input buffer + ) + ;; Store the passed value to the passed key and store result to memory + (i32.store (i32.const 168) + (call $set_storage + (i32.const 8) ;; key_ptr + (i32.load (i32.const 4)) ;; key_len + (i32.add ;; value_ptr = 8 + key_len + (i32.const 8) + (i32.load (i32.const 4))) + (i32.sub ;; value_len (input_size - (key_len + key_len_len)) + (i32.load (i32.const 0)) + (i32.add + (i32.load (i32.const 4)) + (i32.const 4) + ) + ) + ) + ) + (call $seal_return + (i32.const 0) ;; flags + (i32.const 168) ;; ptr to returned value + (i32.const 4) ;; length of returned value + ) + ) + + (func (export "deploy")) +) +"#; + + let mut ext = MockExt::default(); + + // value did not exist before -> sentinel returned + let input = (32, [1u8; 32], [42u8, 48]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), crate::SENTINEL); + assert_eq!(ext.storage.get(&[1u8; 32].to_vec()).unwrap(), &[42u8, 48]); + + // value do exist -> length of old value returned + let input = (32, [1u8; 32], [0u8; 0]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), 2); + assert_eq!(ext.storage.get(&[1u8; 32].to_vec()).unwrap(), &[0u8; 0]); + + // value do exist -> length of old value returned (test for zero sized val) + let input = (32, [1u8; 32], [99u8]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), 0); + assert_eq!(ext.storage.get(&[1u8; 32].to_vec()).unwrap(), &[99u8]); + } + + #[test] + fn get_storage_works() { + const CODE: &str = r#" +(module + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "seal1" "get_storage" (func $get_storage (param i32 i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + + ;; [0, 4) size of input buffer (160 bytes as we copy the key+len here) + (data (i32.const 0) "\A0") + + ;; [4, 8) size of output buffer + ;; 4k in little endian + (data (i32.const 4) "\00\10") + + ;; [8, 168) input buffer + ;; [168, 4264) output buffer + + (func (export "call") + ;; Receive (key ++ value_to_write) + (call $seal_input + (i32.const 8) ;; Pointer to the input buffer + (i32.const 0) ;; Size of the input buffer + ) + ;; Load a storage value and result of this call into the output buffer + (i32.store (i32.const 168) + (call $get_storage + (i32.const 12) ;; key_ptr + (i32.load (i32.const 8)) ;; key_len + (i32.const 172) ;; Pointer to the output buffer + (i32.const 4) ;; Pointer to the size of the buffer + ) + ) + (call $seal_return + (i32.const 0) ;; flags + (i32.const 168) ;; output buffer ptr + (i32.add ;; length: output size + 4 (retval) + (i32.load (i32.const 4)) + (i32.const 4) + ) + ) + ) + + (func (export "deploy")) +) +"#; + + let mut ext = MockExt::default(); + + ext.set_storage( + &Key::::try_from_var([1u8; 64].to_vec()).unwrap(), + Some(vec![42u8]), + false, + ) + .unwrap(); + + ext.set_storage( + &Key::::try_from_var([2u8; 19].to_vec()).unwrap(), + Some(vec![]), + false, + ) + .unwrap(); + + // value does not exist + let input = (63, [1u8; 64]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!( + u32::from_le_bytes(result.data[0..4].try_into().unwrap()), + ReturnErrorCode::KeyNotFound as u32 + ); + + // value exists + let input = (64, [1u8; 64]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!( + u32::from_le_bytes(result.data[0..4].try_into().unwrap()), + ReturnErrorCode::Success as u32 + ); + assert_eq!(ext.storage.get(&[1u8; 64].to_vec()).unwrap(), &[42u8]); + assert_eq!(&result.data[4..], &[42u8]); + + // value exists (test for 0 sized) + let input = (19, [2u8; 19]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!( + u32::from_le_bytes(result.data[0..4].try_into().unwrap()), + ReturnErrorCode::Success as u32 + ); + assert_eq!(ext.storage.get(&[2u8; 19].to_vec()), Some(&vec![])); + assert_eq!(&result.data[4..], &([] as [u8; 0])); + } + + #[test] + fn clear_storage_works() { + const CODE: &str = r#" +(module + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "seal1" "clear_storage" (func $clear_storage (param i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + + ;; size of input buffer + ;; [0, 4) size of input buffer (128+32 = 160 bytes = 0xA0) + (data (i32.const 0) "\A0") + + ;; [4, 164) input buffer + + (func (export "call") + ;; Receive key + (call $seal_input + (i32.const 4) ;; Where we take input and store it + (i32.const 0) ;; Where we take and store the length of thedata + ) + ;; Call seal_clear_storage and save what it returns at 0 + (i32.store (i32.const 0) + (call $clear_storage + (i32.const 8) ;; key_ptr + (i32.load (i32.const 4)) ;; key_len + ) + ) + (call $seal_return + (i32.const 0) ;; flags + (i32.const 0) ;; returned value + (i32.const 4) ;; length of returned value + ) + ) + + (func (export "deploy")) +) +"#; + + let mut ext = MockExt::default(); + + ext.set_storage( + &Key::::try_from_var([1u8; 64].to_vec()).unwrap(), + Some(vec![42u8]), + false, + ) + .unwrap(); + ext.set_storage( + &Key::::try_from_var([2u8; 19].to_vec()).unwrap(), + Some(vec![]), + false, + ) + .unwrap(); + + // value did not exist + let input = (32, [3u8; 32]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + // sentinel returned + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), crate::SENTINEL); + assert_eq!(ext.storage.get(&[3u8; 32].to_vec()), None); + + // value did exist + let input = (64, [1u8; 64]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + // length returned + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), 1); + // value cleared + assert_eq!(ext.storage.get(&[1u8; 64].to_vec()), None); + + //value did not exist (wrong key length) + let input = (63, [1u8; 64]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + // sentinel returned + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), crate::SENTINEL); + assert_eq!(ext.storage.get(&[1u8; 64].to_vec()), None); + + // value exists + let input = (19, [2u8; 19]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + // length returned (test for 0 sized) + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), 0); + // value cleared + assert_eq!(ext.storage.get(&[2u8; 19].to_vec()), None); + } + + #[test] + fn take_storage_works() { + const CODE: &str = r#" +(module + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "seal0" "take_storage" (func $take_storage (param i32 i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + + ;; [0, 4) size of input buffer (160 bytes as we copy the key+len here) + (data (i32.const 0) "\A0") + + ;; [4, 8) size of output buffer + ;; 4k in little endian + (data (i32.const 4) "\00\10") + + ;; [8, 168) input buffer + ;; [168, 4264) output buffer + + (func (export "call") + ;; Receive key + (call $seal_input + (i32.const 8) ;; Pointer to the input buffer + (i32.const 0) ;; Size of the length buffer + ) + + ;; Load a storage value and result of this call into the output buffer + (i32.store (i32.const 168) + (call $take_storage + (i32.const 12) ;; key_ptr + (i32.load (i32.const 8)) ;; key_len + (i32.const 172) ;; Pointer to the output buffer + (i32.const 4) ;; Pointer to the size of the buffer + ) + ) + + ;; Return the contents of the buffer + (call $seal_return + (i32.const 0) ;; flags + (i32.const 168) ;; output buffer ptr + (i32.add ;; length: storage size + 4 (retval) + (i32.load (i32.const 4)) + (i32.const 4) + ) + ) + ) + + (func (export "deploy")) +) +"#; + + let mut ext = MockExt::default(); + + ext.set_storage( + &Key::::try_from_var([1u8; 64].to_vec()).unwrap(), + Some(vec![42u8]), + false, + ) + .unwrap(); + + ext.set_storage( + &Key::::try_from_var([2u8; 19].to_vec()).unwrap(), + Some(vec![]), + false, + ) + .unwrap(); + + // value does not exist -> error returned + let input = (63, [1u8; 64]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!( + u32::from_le_bytes(result.data[0..4].try_into().unwrap()), + ReturnErrorCode::KeyNotFound as u32 + ); + + // value did exist -> value returned + let input = (64, [1u8; 64]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!( + u32::from_le_bytes(result.data[0..4].try_into().unwrap()), + ReturnErrorCode::Success as u32 + ); + assert_eq!(ext.storage.get(&[1u8; 64].to_vec()), None); + assert_eq!(&result.data[4..], &[42u8]); + + // value did exist -> length returned (test for 0 sized) + let input = (19, [2u8; 19]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!( + u32::from_le_bytes(result.data[0..4].try_into().unwrap()), + ReturnErrorCode::Success as u32 + ); + assert_eq!(ext.storage.get(&[2u8; 19].to_vec()), None); + assert_eq!(&result.data[4..], &[0u8; 0]); + } + + #[test] + fn set_transient_storage_works() { + const CODE: &str = r#" +(module + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "seal0" "set_transient_storage" (func $set_transient_storage (param i32 i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + + ;; [0, 4) size of input buffer + ;; 4k in little endian + (data (i32.const 0) "\00\10") + + ;; [4, 4100) input buffer + + (func (export "call") + ;; Receive (key ++ value_to_write) + (call $seal_input + (i32.const 4) ;; Pointer to the input buffer + (i32.const 0) ;; Size of the input buffer + ) + ;; Store the passed value to the passed key and store result to memory + (i32.store (i32.const 168) + (call $set_transient_storage + (i32.const 8) ;; key_ptr + (i32.load (i32.const 4)) ;; key_len + (i32.add ;; value_ptr = 8 + key_len + (i32.const 8) + (i32.load (i32.const 4))) + (i32.sub ;; value_len (input_size - (key_len + key_len_len)) + (i32.load (i32.const 0)) + (i32.add + (i32.load (i32.const 4)) + (i32.const 4) + ) + ) + ) + ) + (call $seal_return + (i32.const 0) ;; flags + (i32.const 168) ;; ptr to returned value + (i32.const 4) ;; length of returned value + ) + ) + + (func (export "deploy")) +) +"#; + + let mut ext = MockExt::default(); + + // value did not exist before -> sentinel returned + let input = (32, [1u8; 32], [42u8, 48]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), crate::SENTINEL); + assert_eq!( + ext.get_transient_storage(&Key::::try_from_var([1u8; 32].to_vec()).unwrap()), + Some(vec![42, 48]) + ); + + // value do exist -> length of old value returned + let input = (32, [1u8; 32], [0u8; 0]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), 2); + assert_eq!( + ext.get_transient_storage(&Key::::try_from_var([1u8; 32].to_vec()).unwrap()), + Some(vec![]) + ); + + // value do exist -> length of old value returned (test for zero sized val) + let input = (32, [1u8; 32], [99u8]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), 0); + assert_eq!( + ext.get_transient_storage(&Key::::try_from_var([1u8; 32].to_vec()).unwrap()), + Some(vec![99]) + ); + } + + #[test] + fn get_transient_storage_works() { + const CODE: &str = r#" +(module + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "seal0" "get_transient_storage" (func $get_transient_storage (param i32 i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + + ;; [0, 4) size of input buffer (160 bytes as we copy the key+len here) + (data (i32.const 0) "\A0") + + ;; [4, 8) size of output buffer + ;; 4k in little endian + (data (i32.const 4) "\00\10") + + ;; [8, 168) input buffer + ;; [168, 4264) output buffer + + (func (export "call") + ;; Receive (key ++ value_to_write) + (call $seal_input + (i32.const 8) ;; Pointer to the input buffer + (i32.const 0) ;; Size of the input buffer + ) + ;; Load a storage value and result of this call into the output buffer + (i32.store (i32.const 168) + (call $get_transient_storage + (i32.const 12) ;; key_ptr + (i32.load (i32.const 8)) ;; key_len + (i32.const 172) ;; Pointer to the output buffer + (i32.const 4) ;; Pointer to the size of the buffer + ) + ) + (call $seal_return + (i32.const 0) ;; flags + (i32.const 168) ;; output buffer ptr + (i32.add ;; length: output size + 4 (retval) + (i32.load (i32.const 4)) + (i32.const 4) + ) + ) + ) + + (func (export "deploy")) +) +"#; + + let mut ext = MockExt::default(); + + assert_ok!(ext.set_transient_storage( + &Key::::try_from_var([1u8; 64].to_vec()).unwrap(), + Some(vec![42u8]), + false + )); + assert_ok!(ext.set_transient_storage( + &Key::::try_from_var([2u8; 19].to_vec()).unwrap(), + Some(vec![]), + false + )); + + // value does not exist + let input = (63, [1u8; 64]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!( + u32::from_le_bytes(result.data[0..4].try_into().unwrap()), + ReturnErrorCode::KeyNotFound as u32 + ); + + // value exists + let input = (64, [1u8; 64]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!( + u32::from_le_bytes(result.data[0..4].try_into().unwrap()), + ReturnErrorCode::Success as u32 + ); + assert_eq!(&result.data[4..], &[42u8]); + + // value exists (test for 0 sized) + let input = (19, [2u8; 19]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!( + u32::from_le_bytes(result.data[0..4].try_into().unwrap()), + ReturnErrorCode::Success as u32 + ); + assert_eq!(&result.data[4..], &([] as [u8; 0])); + } + + #[test] + fn clear_transient_storage_works() { + const CODE: &str = r#" +(module + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "seal0" "clear_transient_storage" (func $clear_transient_storage (param i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + + ;; size of input buffer + ;; [0, 4) size of input buffer (128+32 = 160 bytes = 0xA0) + (data (i32.const 0) "\A0") + + ;; [4, 164) input buffer + + (func (export "call") + ;; Receive key + (call $seal_input + (i32.const 4) ;; Where we take input and store it + (i32.const 0) ;; Where we take and store the length of thedata + ) + ;; Call seal_clear_storage and save what it returns at 0 + (i32.store (i32.const 0) + (call $clear_transient_storage + (i32.const 8) ;; key_ptr + (i32.load (i32.const 4)) ;; key_len + ) + ) + (call $seal_return + (i32.const 0) ;; flags + (i32.const 0) ;; returned value + (i32.const 4) ;; length of returned value + ) + ) + + (func (export "deploy")) +) +"#; + + let mut ext = MockExt::default(); + + assert_ok!(ext.set_transient_storage( + &Key::::try_from_var([1u8; 64].to_vec()).unwrap(), + Some(vec![42u8]), + false + )); + + // value did not exist + let input = (32, [3u8; 32]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + // sentinel returned + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), crate::SENTINEL); + + // value did exist + let input = (64, [1u8; 64]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + // length returned + assert_eq!(u32::from_le_bytes(result.data.try_into().unwrap()), 1); + // value cleared + assert_eq!( + ext.get_transient_storage(&Key::::try_from_var([1u8; 64].to_vec()).unwrap()), + None + ); + } + + #[test] + fn take_transient_storage_works() { + const CODE: &str = r#" +(module + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "seal0" "take_transient_storage" (func $take_transient_storage (param i32 i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + + ;; [0, 4) size of input buffer (160 bytes as we copy the key+len here) + (data (i32.const 0) "\A0") + + ;; [4, 8) size of output buffer + ;; 4k in little endian + (data (i32.const 4) "\00\10") + + ;; [8, 168) input buffer + ;; [168, 4264) output buffer + + (func (export "call") + ;; Receive key + (call $seal_input + (i32.const 8) ;; Pointer to the input buffer + (i32.const 0) ;; Size of the length buffer + ) + + ;; Load a storage value and result of this call into the output buffer + (i32.store (i32.const 168) + (call $take_transient_storage + (i32.const 12) ;; key_ptr + (i32.load (i32.const 8)) ;; key_len + (i32.const 172) ;; Pointer to the output buffer + (i32.const 4) ;; Pointer to the size of the buffer + ) + ) + + ;; Return the contents of the buffer + (call $seal_return + (i32.const 0) ;; flags + (i32.const 168) ;; output buffer ptr + (i32.add ;; length: storage size + 4 (retval) + (i32.load (i32.const 4)) + (i32.const 4) + ) + ) + ) + + (func (export "deploy")) +) +"#; + + let mut ext = MockExt::default(); + + assert_ok!(ext.set_transient_storage( + &Key::::try_from_var([1u8; 64].to_vec()).unwrap(), + Some(vec![42u8]), + false + )); + assert_ok!(ext.set_transient_storage( + &Key::::try_from_var([2u8; 19].to_vec()).unwrap(), + Some(vec![]), + false + )); + + // value does not exist -> error returned + let input = (63, [1u8; 64]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!( + u32::from_le_bytes(result.data[0..4].try_into().unwrap()), + ReturnErrorCode::KeyNotFound as u32 + ); + + // value did exist -> value returned + let input = (64, [1u8; 64]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!( + u32::from_le_bytes(result.data[0..4].try_into().unwrap()), + ReturnErrorCode::Success as u32 + ); + assert_eq!( + ext.get_transient_storage(&Key::::try_from_var([1u8; 64].to_vec()).unwrap()), + None + ); + assert_eq!(&result.data[4..], &[42u8]); + + // value did exist -> length returned (test for 0 sized) + let input = (19, [2u8; 19]).encode(); + let result = execute(CODE, input, &mut ext).unwrap(); + assert_eq!( + u32::from_le_bytes(result.data[0..4].try_into().unwrap()), + ReturnErrorCode::Success as u32 + ); + assert_eq!( + ext.get_transient_storage(&Key::::try_from_var([2u8; 19].to_vec()).unwrap()), + None + ); + assert_eq!(&result.data[4..], &[0u8; 0]); + } + + #[test] + fn is_contract_works() { + const CODE_IS_CONTRACT: &str = r#" +;; This runs `is_contract` check on zero account address +(module + (import "seal0" "seal_is_contract" (func $seal_is_contract (param i32) (result i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; [0, 32) zero-adress + (data (i32.const 0) + "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00" + "\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00" + ) + + ;; [32, 36) here we store the return code of the `seal_is_contract` + + (func (export "deploy")) + + (func (export "call") + (i32.store + (i32.const 32) + (call $seal_is_contract + (i32.const 0) ;; ptr to destination address + ) + ) + ;; exit with success and take `seal_is_contract` return code to the output buffer + (call $seal_return (i32.const 0) (i32.const 32) (i32.const 4)) + ) +) +"#; + let output = execute(CODE_IS_CONTRACT, vec![], MockExt::default()).unwrap(); + + // The mock ext just always returns 1u32 (`true`). + assert_eq!(output, ExecReturnValue { flags: ReturnFlags::empty(), data: 1u32.encode() },); + } + + #[test] + fn code_hash_works() { + /// calls `seal_code_hash` and compares the result with the constant. + const CODE_CODE_HASH: &str = r#" +(module + (import "seal0" "seal_code_hash" (func $seal_code_hash (param i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + + ;; size of our buffer is 32 bytes + (data (i32.const 32) "\20") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + ;; fill the buffer with the code hash. + (call $seal_code_hash + (i32.const 0) ;; input: address_ptr (before call) + (i32.const 0) ;; output: code_hash_ptr (after call) + (i32.const 32) ;; same 32 bytes length for input and output + ) + + ;; assert size == 32 + (call $assert + (i32.eq + (i32.load (i32.const 32)) + (i32.const 32) + ) + ) + + ;; assert that the first 8 bytes are "1111111111111111" + (call $assert + (i64.eq + (i64.load (i32.const 0)) + (i64.const 0x1111111111111111) + ) + ) + drop + ) + + (func (export "deploy")) +) +"#; + assert_ok!(execute(CODE_CODE_HASH, vec![], MockExt::default())); + } + + #[test] + fn own_code_hash_works() { + /// calls `seal_own_code_hash` and compares the result with the constant. + const CODE_OWN_CODE_HASH: &str = r#" +(module + (import "seal0" "seal_own_code_hash" (func $seal_own_code_hash (param i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; size of our buffer is 32 bytes + (data (i32.const 32) "\20") + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + + (func (export "call") + ;; fill the buffer with the code hash + (call $seal_own_code_hash + (i32.const 0) ;; output: code_hash_ptr + (i32.const 32) ;; 32 bytes length of code_hash output + ) + + ;; assert size == 32 + (call $assert + (i32.eq + (i32.load (i32.const 32)) + (i32.const 32) + ) + ) + + ;; assert that the first 8 bytes are "1010101010101010" + (call $assert + (i64.eq + (i64.load (i32.const 0)) + (i64.const 0x1010101010101010) + ) + ) + ) + + (func (export "deploy")) +) +"#; + assert_ok!(execute(CODE_OWN_CODE_HASH, vec![], MockExt::default())); + } + + #[test] + fn caller_is_origin_works() { + const CODE_CALLER_IS_ORIGIN: &str = r#" +;; This runs `caller_is_origin` check on zero account address +(module + (import "seal0" "seal_caller_is_origin" (func $seal_caller_is_origin (result i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; [0, 4) here the return code of the `seal_caller_is_origin` will be stored + ;; we initialize it with non-zero value to be sure that it's being overwritten below + (data (i32.const 0) "\10\10\10\10") + + (func (export "deploy")) + + (func (export "call") + (i32.store + (i32.const 0) + (call $seal_caller_is_origin) + ) + ;; exit with success and take `seal_caller_is_origin` return code to the output buffer + (call $seal_return (i32.const 0) (i32.const 0) (i32.const 4)) + ) +) +"#; + let output = execute(CODE_CALLER_IS_ORIGIN, vec![], MockExt::default()).unwrap(); + + // The mock ext just always returns 0u32 (`false`) + assert_eq!(output, ExecReturnValue { flags: ReturnFlags::empty(), data: 0u32.encode() },); + } + + #[test] + fn caller_is_root_works() { + const CODE_CALLER_IS_ROOT: &str = r#" +;; This runs `caller_is_root` check on zero account address +(module + (import "seal0" "caller_is_root" (func $caller_is_root (result i32))) + (import "seal0" "seal_return" (func $seal_return (param i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + ;; [0, 4) here the return code of the `caller_is_root` will be stored + ;; we initialize it with non-zero value to be sure that it's being overwritten below + (data (i32.const 0) "\10\10\10\10") + + (func (export "deploy")) + + (func (export "call") + (i32.store + (i32.const 0) + (call $caller_is_root) + ) + ;; exit with success and take `caller_is_root` return code to the output buffer + (call $seal_return (i32.const 0) (i32.const 0) (i32.const 4)) + ) +) +"#; + // The default `caller` is ALICE. Therefore not root. + let output = execute(CODE_CALLER_IS_ROOT, vec![], MockExt::default()).unwrap(); + assert_eq!(output, ExecReturnValue { flags: ReturnFlags::empty(), data: 0u32.encode() },); + + // The caller is forced to be root instead of using the default ALICE. + let output = execute( + CODE_CALLER_IS_ROOT, + vec![], + MockExt { caller: Origin::Root, ..MockExt::default() }, + ) + .unwrap(); + assert_eq!(output, ExecReturnValue { flags: ReturnFlags::empty(), data: 1u32.encode() },); + } + + #[test] + fn set_code_hash() { + const CODE: &str = r#" +(module + (import "seal0" "seal_set_code_hash" (func $seal_set_code_hash (param i32) (result i32))) + (import "env" "memory" (memory 1 1)) + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + (func (export "call") + (local $exit_code i32) + (local.set $exit_code + (call $seal_set_code_hash (i32.const 0)) + ) + (call $assert + (i32.eq (local.get $exit_code) (i32.const 0)) ;; ReturnCode::Success + ) + ) + + (func (export "deploy")) + + ;; Hash of code. + (data (i32.const 0) + "\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11" + "\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11\11" + ) +) +"#; + + let mut mock_ext = MockExt::default(); + execute(CODE, [0u8; 32].encode(), &mut mock_ext).unwrap(); + + assert_eq!(mock_ext.code_hashes.pop().unwrap(), H256::from_slice(&[17u8; 32])); + } + + #[test] + fn reentrance_count_works() { + const CODE: &str = r#" +(module + (import "seal0" "reentrance_count" (func $reentrance_count (result i32))) + (import "env" "memory" (memory 1 1)) + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + (func (export "call") + (local $return_val i32) + (local.set $return_val + (call $reentrance_count) + ) + (call $assert + (i32.eq (local.get $return_val) (i32.const 12)) + ) + ) + + (func (export "deploy")) +) +"#; + + let mut mock_ext = MockExt::default(); + execute(CODE, vec![], &mut mock_ext).unwrap(); + } + + #[test] + fn account_reentrance_count_works() { + const CODE: &str = r#" +(module + (import "seal0" "account_reentrance_count" (func $account_reentrance_count (param i32) (result i32))) + (import "env" "memory" (memory 1 1)) + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + (func (export "call") + (local $return_val i32) + (local.set $return_val + (call $account_reentrance_count (i32.const 0)) + ) + (call $assert + (i32.eq (local.get $return_val) (i32.const 12)) + ) + ) + + (func (export "deploy")) +) +"#; + + let mut mock_ext = MockExt::default(); + execute(CODE, vec![], &mut mock_ext).unwrap(); + } + + #[test] + fn instantiation_nonce_works() { + const CODE: &str = r#" +(module + (import "seal0" "instantiation_nonce" (func $nonce (result i64))) + (import "env" "memory" (memory 1 1)) + + (func $assert (param i32) + (block $ok + (br_if $ok + (local.get 0) + ) + (unreachable) + ) + ) + (func (export "call") + (call $assert + (i64.eq (call $nonce) (i64.const 995)) + ) + ) + (func (export "deploy")) +) +"#; + + let mut mock_ext = MockExt::default(); + execute(CODE, vec![], &mut mock_ext).unwrap(); + } + + /// This test check that an unstable interface cannot be deployed. In case of runtime + /// benchmarks we always allow unstable interfaces. This is why this test does not + /// work when this feature is enabled. + #[cfg(not(feature = "runtime-benchmarks"))] + #[test] + fn cannot_deploy_unstable() { + const CANNOT_DEPLOY_UNSTABLE: &str = r#" +(module + (import "seal0" "reentrance_count" (func $reentrance_count (result i32))) + (import "env" "memory" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) +) +"#; + assert_err!( + execute_no_unstable(CANNOT_DEPLOY_UNSTABLE, vec![], MockExt::default()), + >::CodeRejected, + ); + assert_ok!(execute(CANNOT_DEPLOY_UNSTABLE, vec![], MockExt::default())); + } + + /// The random interface is deprecated and hence new contracts using it should not be deployed. + /// In case of runtime benchmarks we always allow deprecated interfaces. This is why this + /// test doesn't work if this feature is enabled. + #[cfg(not(feature = "runtime-benchmarks"))] + #[test] + fn cannot_deploy_deprecated() { + const CODE_RANDOM_0: &str = r#" +(module + (import "seal0" "seal_random" (func $seal_random (param i32 i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) +) + "#; + const CODE_RANDOM_1: &str = r#" +(module + (import "seal1" "seal_random" (func $seal_random (param i32 i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) +) + "#; + const CODE_RANDOM_2: &str = r#" +(module + (import "seal0" "random" (func $seal_random (param i32 i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) +) + "#; + const CODE_RANDOM_3: &str = r#" +(module + (import "seal1" "random" (func $seal_random (param i32 i32 i32 i32))) + (import "env" "memory" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) +) + "#; + + assert_ok!(execute_unvalidated(CODE_RANDOM_0, vec![], MockExt::default())); + assert_err!( + execute_instantiate_unvalidated(CODE_RANDOM_0, vec![], MockExt::default()), + >::CodeRejected, + ); + assert_err!( + execute(CODE_RANDOM_0, vec![], MockExt::default()), + >::CodeRejected, + ); + + assert_ok!(execute_unvalidated(CODE_RANDOM_1, vec![], MockExt::default())); + assert_err!( + execute_instantiate_unvalidated(CODE_RANDOM_1, vec![], MockExt::default()), + >::CodeRejected, + ); + assert_err!( + execute(CODE_RANDOM_1, vec![], MockExt::default()), + >::CodeRejected, + ); + + assert_ok!(execute_unvalidated(CODE_RANDOM_2, vec![], MockExt::default())); + assert_err!( + execute_instantiate_unvalidated(CODE_RANDOM_2, vec![], MockExt::default()), + >::CodeRejected, + ); + assert_err!( + execute(CODE_RANDOM_2, vec![], MockExt::default()), + >::CodeRejected, + ); + + assert_ok!(execute_unvalidated(CODE_RANDOM_3, vec![], MockExt::default())); + assert_err!( + execute_instantiate_unvalidated(CODE_RANDOM_3, vec![], MockExt::default()), + >::CodeRejected, + ); + assert_err!( + execute(CODE_RANDOM_3, vec![], MockExt::default()), + >::CodeRejected, + ); + } + + #[test] + fn lock_unlock_delegate_dependency() { + const CODE_LOCK_UNLOCK_DELEGATE_DEPENDENCY: &str = r#" +(module + (import "seal0" "lock_delegate_dependency" (func $lock_delegate_dependency (param i32))) + (import "seal0" "unlock_delegate_dependency" (func $unlock_delegate_dependency (param i32))) + (import "env" "memory" (memory 1 1)) + (func (export "call") + (call $lock_delegate_dependency (i32.const 0)) + (call $lock_delegate_dependency (i32.const 32)) + (call $unlock_delegate_dependency (i32.const 32)) + ) + (func (export "deploy")) + + ;; hash1 (32 bytes) + (data (i32.const 0) + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + "\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01" + ) + + ;; hash2 (32 bytes) + (data (i32.const 32) + "\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02" + "\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02\02" + ) +) +"#; + let mut mock_ext = MockExt::default(); + assert_ok!(execute(&CODE_LOCK_UNLOCK_DELEGATE_DEPENDENCY, vec![], &mut mock_ext)); + let delegate_dependencies: Vec<_> = + mock_ext.delegate_dependencies.into_inner().into_iter().collect(); + assert_eq!(delegate_dependencies.len(), 1); + assert_eq!(delegate_dependencies[0].as_bytes(), [1; 32]); + } + + // This test checks that [`Runtime::read_sandbox_memory_as`] works, when the decoded type has a + // max_len greater than the memory size, but the decoded data fits into the memory. + #[test] + fn read_sandbox_memory_as_works_with_max_len_out_of_bounds_but_fitting_actual_data() { + use frame_support::BoundedVec; + use sp_core::ConstU32; + + let mut ext = MockExt::default(); + let runtime = Runtime::new(&mut ext, vec![]); + let data = vec![1u8, 2, 3]; + let memory = data.encode(); + let decoded: BoundedVec> = + runtime.read_sandbox_memory_as(&memory, 0u32).unwrap(); + assert_eq!(decoded.into_inner(), data); + } + + #[test] + fn run_out_of_gas_in_start_fn() { + const CODE: &str = r#" +(module + (import "env" "memory" (memory 1 1)) + (start $start) + (func $start + (loop $inf (br $inf)) ;; just run out of gas + (unreachable) + ) + (func (export "call")) + (func (export "deploy")) +) +"#; + let mut mock_ext = MockExt::default(); + assert_err!(execute(&CODE, vec![], &mut mock_ext), >::OutOfGas); + } +} diff --git a/pallets/contracts/src/wasm/prepare.rs b/pallets/contracts/src/wasm/prepare.rs new file mode 100644 index 00000000..93fe3080 --- /dev/null +++ b/pallets/contracts/src/wasm/prepare.rs @@ -0,0 +1,873 @@ +// This file is part of Substrate. + +// 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. + +//! This module takes care of loading, checking and preprocessing of a +//! wasm module before execution. It also extracts some essential information +//! from a module. + +use crate::{ + chain_extension::ChainExtension, + storage::meter::Diff, + wasm::{ + runtime::AllowDeprecatedInterface, CodeInfo, Determinism, Environment, WasmBlob, + BYTES_PER_PAGE, + }, + AccountIdOf, CodeVec, Config, Error, Schedule, LOG_TARGET, +}; +#[cfg(any(test, feature = "runtime-benchmarks"))] +use alloc::vec::Vec; +use codec::MaxEncodedLen; +use sp_runtime::{traits::Hash, DispatchError}; +use wasmi::{ + core::ValType as WasmiValueType, CompilationMode, Config as WasmiConfig, Engine, ExternType, + Module, StackLimits, +}; + +/// Imported memory must be located inside this module. The reason for hardcoding is that current +/// compiler toolchains might not support specifying other modules than "env" for memory imports. +pub const IMPORT_MODULE_MEMORY: &str = "env"; + +/// The inner deserialized module is valid and contains only allowed WebAssembly features. +/// This is checked by loading it into wasmi interpreter `engine`. +pub struct LoadedModule { + pub module: Module, + pub engine: Engine, +} + +#[derive(PartialEq, Debug, Clone)] +pub enum LoadingMode { + Checked, + Unchecked, +} + +#[cfg(test)] +pub mod tracker { + use core::cell::RefCell; + thread_local! { + pub static LOADED_MODULE: RefCell> = RefCell::new(Vec::new()); + } +} + +impl LoadedModule { + /// Creates a new instance of `LoadedModule`. + /// + /// The inner Wasm module is checked not to have restricted WebAssembly proposals. + /// Returns `Err` if the `code` cannot be deserialized or if it contains an invalid module. + pub fn new( + code: &[u8], + determinism: Determinism, + stack_limits: Option, + loading_mode: LoadingMode, + compilation_mode: CompilationMode, + ) -> Result { + // NOTE: wasmi does not support unstable WebAssembly features. The module is implicitly + // checked for not having those ones when creating `wasmi::Module` below. + let mut config = WasmiConfig::default(); + config + .wasm_multi_value(false) + .wasm_mutable_global(false) + .wasm_sign_extension(true) + .wasm_bulk_memory(false) + .wasm_reference_types(false) + .wasm_tail_call(false) + .wasm_extended_const(false) + .wasm_saturating_float_to_int(false) + .floats(matches!(determinism, Determinism::Relaxed)) + .compilation_mode(compilation_mode) + .consume_fuel(true); + + if let Some(stack_limits) = stack_limits { + config.set_stack_limits(stack_limits); + } + + let engine = Engine::new(&config); + + let module = match loading_mode { + LoadingMode::Checked => Module::new(&engine, code), + // Safety: The code has been validated, Therefore we know that it's a valid binary. + LoadingMode::Unchecked => unsafe { Module::new_unchecked(&engine, code) }, + } + .map_err(|err| { + log::debug!(target: LOG_TARGET, "Module creation failed: {:?}", err); + "Can't load the module into wasmi!" + })?; + + #[cfg(test)] + tracker::LOADED_MODULE.with(|t| t.borrow_mut().push(loading_mode)); + + // Return a `LoadedModule` instance with + // __valid__ module. + Ok(LoadedModule { module, engine }) + } + + /// Check that the module has required exported functions. For now + /// these are just entrypoints: + /// + /// - 'call' + /// - 'deploy' + /// + /// Any other exports are not allowed. + fn scan_exports(&self) -> Result<(), &'static str> { + let mut deploy_found = false; + let mut call_found = false; + let module = &self.module; + let exports = module.exports(); + + for export in exports { + match export.ty() { + ExternType::Func(ft) => { + match export.name() { + "call" => call_found = true, + "deploy" => deploy_found = true, + _ => + return Err( + "unknown function export: expecting only deploy and call functions", + ), + } + // Check the signature. + // Both "call" and "deploy" have the () -> () function type. + // We still support () -> (i32) for backwards compatibility. + if !(ft.params().is_empty() && + (ft.results().is_empty() || ft.results() == [WasmiValueType::I32])) + { + return Err("entry point has wrong signature") + } + }, + ExternType::Memory(_) => return Err("memory export is forbidden"), + ExternType::Global(_) => return Err("global export is forbidden"), + ExternType::Table(_) => return Err("table export is forbidden"), + } + } + + if !deploy_found { + return Err("deploy function isn't exported") + } + if !call_found { + return Err("call function isn't exported") + } + + Ok(()) + } + + /// Scan an import section if any. + /// + /// This makes sure that: + /// - The import section looks as we expect it from a contract. + /// - The limits of the memory type declared by the contract comply with the Schedule. + /// + /// Returns the checked memory limits back to caller. + /// + /// This method fails if: + /// + /// - Memory import not found in the module. + /// - Tables or globals found among imports. + /// - `call_chain_extension` host function is imported, while chain extensions are disabled. + /// + /// NOTE that only single memory instance is allowed for contract modules, which is enforced by + /// this check combined with multi_memory proposal gets disabled in the engine. + pub fn scan_imports( + &self, + schedule: &Schedule, + ) -> Result<(u32, u32), &'static str> { + let module = &self.module; + let imports = module.imports(); + let mut memory_limits = None; + + for import in imports { + match *import.ty() { + ExternType::Table(_) => return Err("Cannot import tables"), + ExternType::Global(_) => return Err("Cannot import globals"), + ExternType::Func(_) => { + let _ = import.ty().func().ok_or("expected a function")?; + + if !::ChainExtension::enabled() && + (import.name().as_bytes() == b"seal_call_chain_extension" || + import.name().as_bytes() == b"call_chain_extension") + { + return Err("Module uses chain extensions but chain extensions are disabled") + } + }, + ExternType::Memory(mt) => { + if import.module().as_bytes() != IMPORT_MODULE_MEMORY.as_bytes() { + return Err("Invalid module for imported memory") + } + if import.name().as_bytes() != b"memory" { + return Err("Memory import must have the field name 'memory'") + } + if memory_limits.is_some() { + return Err("Multiple memory imports defined") + } + // Parse memory limits defaulting it to (0,0). + // Any access to it will then lead to out of bounds trap. + let (initial, maximum) = ( + mt.initial_pages().to_bytes().unwrap_or(0).saturating_div(BYTES_PER_PAGE) + as u32, + mt.maximum_pages().map_or(schedule.limits.memory_pages, |p| { + p.to_bytes().unwrap_or(0).saturating_div(BYTES_PER_PAGE) as u32 + }), + ); + if initial > maximum { + return Err( + "Requested initial number of memory pages should not exceed the requested maximum", + ) + } + if maximum > schedule.limits.memory_pages { + return Err("Maximum number of memory pages should not exceed the maximum configured in the Schedule") + } + + memory_limits = Some((initial, maximum)); + continue + }, + } + } + + memory_limits.ok_or("No memory import found in the module") + } +} + +/// Check that given `code` satisfies constraints required for the contract Wasm module. +/// This includes two groups of checks: +/// +/// 1. General engine-side validation makes sure the module is consistent and does not contain +/// forbidden WebAssembly features. +/// 2. Additional checks which are specific to smart contracts eligible for this pallet. +fn validate( + code: &[u8], + schedule: &Schedule, + determinism: &mut Determinism, +) -> Result<(), (DispatchError, &'static str)> +where + E: Environment<()>, + T: Config, +{ + let module = (|| { + // We don't actually ever execute this instance so we can get away with a minimal stack + // which reduces the amount of memory that needs to be zeroed. + let stack_limits = Some(StackLimits::new(1, 1, 0).expect("initial <= max; qed")); + + // We check that the module is generally valid, + // and does not have restricted WebAssembly features, here. + let contract_module = match *determinism { + Determinism::Relaxed => + if let Ok(module) = LoadedModule::new::( + code, + Determinism::Enforced, + stack_limits, + LoadingMode::Checked, + CompilationMode::Eager, + ) { + *determinism = Determinism::Enforced; + module + } else { + LoadedModule::new::( + code, + Determinism::Relaxed, + None, + LoadingMode::Checked, + CompilationMode::Eager, + )? + }, + Determinism::Enforced => LoadedModule::new::( + code, + Determinism::Enforced, + stack_limits, + LoadingMode::Checked, + CompilationMode::Eager, + )?, + }; + + // The we check that module satisfies constraints the pallet puts on contracts. + contract_module.scan_exports()?; + contract_module.scan_imports::(schedule)?; + Ok(contract_module) + })() + .map_err(|msg: &str| { + log::debug!(target: LOG_TARGET, "New code rejected on validation: {}", msg); + (Error::::CodeRejected.into(), msg) + })?; + + // This will make sure that the module can be actually run within wasmi: + // + // - It doesn't use any unknown imports. + // - It doesn't explode the wasmi bytecode generation. + WasmBlob::::instantiate::(module, (), schedule, AllowDeprecatedInterface::No) + .map_err(|err| { + log::debug!(target: LOG_TARGET, "{err}"); + (Error::::CodeRejected.into(), "New code rejected on wasmi instantiation!") + })?; + + Ok(()) +} + +/// Validates the given binary `code` is a valid Wasm module satisfying following constraints: +/// +/// - The module doesn't export any memory. +/// - The module does imports memory, which limits lay within the limits permitted by the +/// `schedule`. +/// - All imported functions from the external environment match defined by `env` module. +/// +/// Also constructs contract `code_info` by calculating the storage deposit. +pub fn prepare( + code: CodeVec, + schedule: &Schedule, + owner: AccountIdOf, + mut determinism: Determinism, +) -> Result, (DispatchError, &'static str)> +where + E: Environment<()>, + T: Config, +{ + validate::(code.as_ref(), schedule, &mut determinism)?; + + // Calculate deposit for storing contract code and `code_info` in two different storage items. + let code_len = code.len() as u32; + let bytes_added = code_len.saturating_add(>::max_encoded_len() as u32); + let deposit = Diff { bytes_added, items_added: 2, ..Default::default() } + .update_contract::(None) + .charge_or_zero(); + let code_info = CodeInfo { owner, deposit, determinism, refcount: 0, code_len }; + let code_hash = T::Hashing::hash(&code); + + Ok(WasmBlob { code, code_info, code_hash }) +} + +/// Alternate (possibly unsafe) preparation functions used only for benchmarking and testing. +/// +/// For benchmarking we need to construct special contracts that might not pass our +/// sanity checks. We hide functions allowing this behind a feature that is only set during +/// benchmarking or testing to prevent usage in production code. +#[cfg(any(test, feature = "runtime-benchmarks"))] +pub mod benchmarking { + use super::*; + + /// Prepare function that does not perform export section checks on the passed in code. + pub fn prepare( + code: Vec, + schedule: &Schedule, + owner: AccountIdOf, + ) -> Result, DispatchError> { + let determinism = Determinism::Enforced; + let contract_module = LoadedModule::new::( + &code, + determinism, + None, + LoadingMode::Checked, + CompilationMode::Eager, + )?; + let _ = contract_module.scan_imports::(schedule)?; + let code: CodeVec = code.try_into().map_err(|_| >::CodeTooLarge)?; + let code_info = CodeInfo { + owner, + // this is a helper function for benchmarking which skips deposit collection + deposit: Default::default(), + refcount: 0, + code_len: code.len() as u32, + determinism, + }; + let code_hash = T::Hashing::hash(&code); + + Ok(WasmBlob { code, code_info, code_hash }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + exec::Ext, + schedule::Limits, + tests::{Test, ALICE}, + }; + use pallet_contracts_proc_macro::define_env; + use std::fmt; + + impl fmt::Debug for WasmBlob { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "ContractCode {{ .. }}") + } + } + + /// Using unreachable statements triggers unreachable warnings in the generated code + #[allow(unreachable_code)] + mod env { + use super::*; + use crate::wasm::runtime::{AllowDeprecatedInterface, AllowUnstableInterface, TrapReason}; + + // Define test environment for tests. We need ImportSatisfyCheck + // implementation from it. So actual implementations doesn't matter. + #[define_env] + pub mod test_env { + fn panic(_ctx: _, _memory: _) -> Result<(), TrapReason> { + Ok(()) + } + + // gas is an implementation defined function and a contract can't import it. + fn gas(_ctx: _, _memory: _, _amount: u64) -> Result<(), TrapReason> { + Ok(()) + } + + fn nop(_ctx: _, _memory: _, _unused: u64) -> Result<(), TrapReason> { + Ok(()) + } + + // new version of nop with other data type for argument + #[version(1)] + fn nop(_ctx: _, _memory: _, _unused: i32) -> Result<(), TrapReason> { + Ok(()) + } + } + } + + macro_rules! prepare_test { + ($name:ident, $wat:expr, $($expected:tt)*) => { + #[test] + fn $name() { + let wasm = wat::parse_str($wat).unwrap().try_into().unwrap(); + let schedule = Schedule { + limits: Limits { + memory_pages: 16, + .. Default::default() + }, + .. Default::default() + }; + let r = prepare::( + wasm, + &schedule, + ALICE, + Determinism::Enforced, + ); + assert_matches::assert_matches!(r.map_err(|(_, msg)| msg), $($expected)*); + } + }; + } + + prepare_test!( + no_floats, + r#" + (module + (func (export "call") + (drop + (f32.add + (f32.const 0) + (f32.const 1) + ) + ) + ) + (func (export "deploy")) + )"#, + Err("Can't load the module into wasmi!") + ); + + mod memories { + use super::*; + + prepare_test!( + memory_with_one_page, + r#" + (module + (import "env" "memory" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Ok(_) + ); + + prepare_test!( + internal_memory_declaration, + r#" + (module + (memory 1 1) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("No memory import found in the module") + ); + + prepare_test!( + no_memory_import, + r#" + (module + ;; no memory imported + + (func (export "call")) + (func (export "deploy")) + )"#, + Err("No memory import found in the module") + ); + + prepare_test!( + initial_exceeds_maximum, + r#" + (module + (import "env" "memory" (memory 16 1)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Can't load the module into wasmi!") + ); + + prepare_test!( + requested_maximum_valid, + r#" + (module + (import "env" "memory" (memory 1 16)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Ok(_) + ); + + prepare_test!( + requested_maximum_exceeds_configured_maximum, + r#" + (module + (import "env" "memory" (memory 1 17)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Maximum number of memory pages should not exceed the maximum configured in the Schedule") + ); + + prepare_test!( + field_name_not_memory, + r#" + (module + (import "env" "forgetit" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Memory import must have the field name 'memory'") + ); + + prepare_test!( + multiple_memory_imports, + r#" + (module + (import "env" "memory" (memory 1 1)) + (import "env" "memory" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Can't load the module into wasmi!") + ); + + prepare_test!( + table_import, + r#" + (module + (import "seal0" "table" (table 1 anyfunc)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Cannot import tables") + ); + + prepare_test!( + global_import, + r#" + (module + (global $g (import "seal0" "global") i32) + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Cannot import globals") + ); + } + + mod imports { + use super::*; + + prepare_test!( + can_import_legit_function, + r#" + (module + (import "seal0" "nop" (func (param i64))) + (import "env" "memory" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Ok(_) + ); + + // memory is in "env" and not in "seal0" + prepare_test!( + memory_not_in_seal0, + r#" + (module + (import "seal0" "memory" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Invalid module for imported memory") + ); + + // Memory is in "env" and not in some arbitrary module + prepare_test!( + memory_not_in_arbitrary_module, + r#" + (module + (import "any_module" "memory" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Invalid module for imported memory") + ); + + prepare_test!( + function_in_other_module_works, + r#" + (module + (import "seal1" "nop" (func (param i32))) + (import "env" "memory" (memory 1 1)) + + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Ok(_) + ); + + prepare_test!( + wrong_signature, + r#" + (module + (import "seal0" "input" (func (param i64))) + (import "env" "memory" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("New code rejected on wasmi instantiation!") + ); + + prepare_test!( + unknown_func_name, + r#" + (module + (import "seal0" "unknown_func" (func)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("No memory import found in the module") + ); + + // Try to import function from not a "seal*" module. + prepare_test!( + try_import_from_wrong_module, + r#" + (module + (import "env" "panic" (func)) + (import "env" "memory" (memory 1 1)) + + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("New code rejected on wasmi instantiation!") + ); + } + + mod entrypoints { + use super::*; + + prepare_test!( + it_works, + r#" + (module + (import "env" "memory" (memory 1 1)) + (func (export "call")) + (func (export "deploy")) + ) + "#, + Ok(_) + ); + + prepare_test!( + signed_extension_works, + r#" + (module + (import "env" "memory" (memory 1 1)) + (func (export "deploy")) + (func (export "call")) + (func (param i32) (result i32) + local.get 0 + i32.extend8_s + ) + ) + "#, + Ok(_) + ); + + prepare_test!( + omit_memory, + r#" + (module + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("No memory import found in the module") + ); + + prepare_test!( + omit_deploy, + r#" + (module + (func (export "call")) + ) + "#, + Err("deploy function isn't exported") + ); + + prepare_test!( + omit_call, + r#" + (module + (func (export "deploy")) + ) + "#, + Err("call function isn't exported") + ); + + // Try to use imported function as an entry point. + // This is allowed. + prepare_test!( + try_sneak_export_as_entrypoint, + r#" + (module + (import "seal0" "panic" (func)) + (import "env" "memory" (memory 1 1)) + + (func (export "deploy")) + + (export "call" (func 0)) + ) + "#, + Ok(_) + ); + + // Try to use global as an entry point. + prepare_test!( + try_sneak_export_as_global, + r#" + (module + (func (export "deploy")) + (global (export "call") i32 (i32.const 0)) + ) + "#, + Err("global export is forbidden") + ); + + prepare_test!( + wrong_signature, + r#" + (module + (func (export "deploy")) + (func (export "call") (param i32)) + ) + "#, + Err("entry point has wrong signature") + ); + + prepare_test!( + unknown_exports, + r#" + (module + (func (export "call")) + (func (export "deploy")) + (func (export "whatevs")) + ) + "#, + Err("unknown function export: expecting only deploy and call functions") + ); + + prepare_test!( + global_float, + r#" + (module + (global $x f32 (f32.const 0)) + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Can't load the module into wasmi!") + ); + + prepare_test!( + local_float, + r#" + (module + (func $foo (local f32)) + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Can't load the module into wasmi!") + ); + + prepare_test!( + param_float, + r#" + (module + (func $foo (param f32)) + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Can't load the module into wasmi!") + ); + + prepare_test!( + result_float, + r#" + (module + (func $foo (result f32) (f32.const 0)) + (func (export "call")) + (func (export "deploy")) + ) + "#, + Err("Can't load the module into wasmi!") + ); + } +} diff --git a/pallets/contracts/src/wasm/runtime.rs b/pallets/contracts/src/wasm/runtime.rs new file mode 100644 index 00000000..984e5712 --- /dev/null +++ b/pallets/contracts/src/wasm/runtime.rs @@ -0,0 +1,2549 @@ +// This file is part of Substrate. + +// 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. + +//! Environment definition of the wasm smart-contract runtime. + +use crate::{ + exec::{ExecError, ExecResult, Ext, Key, TopicOf}, + gas::{ChargedAmount, Token}, + primitives::ExecReturnValue, + weights::WeightInfo, + BalanceOf, CodeHash, Config, DebugBufferVec, Error, SENTINEL, +}; +use alloc::{boxed::Box, vec, vec::Vec}; +use codec::{Decode, DecodeLimit, Encode, MaxEncodedLen}; +use core::fmt; +use frame_support::{ + dispatch::DispatchInfo, ensure, pallet_prelude::DispatchResultWithPostInfo, parameter_types, + traits::Get, weights::Weight, +}; +use pallet_contracts_proc_macro::define_env; +use pallet_contracts_uapi::{CallFlags, ReturnFlags}; +use sp_io::hashing::{blake2_128, blake2_256, keccak_256, sha2_256}; +use sp_runtime::{ + traits::{Bounded, Zero}, + DispatchError, RuntimeDebug, +}; +use wasmi::{core::HostError, errors::LinkerError, Linker, Memory, Store}; + +type CallOf = ::RuntimeCall; + +/// The maximum nesting depth a contract can use when encoding types. +const MAX_DECODE_NESTING: u32 = 256; + +/// Passed to [`Environment`] to determine whether it should expose deprecated interfaces. +pub enum AllowDeprecatedInterface { + /// No deprecated interfaces are exposed. + No, + /// Deprecated interfaces are exposed. + Yes, +} + +/// Passed to [`Environment`] to determine whether it should expose unstable interfaces. +pub enum AllowUnstableInterface { + /// No unstable interfaces are exposed. + No, + /// Unstable interfaces are exposed. + Yes, +} + +/// Trait implemented by the [`define_env`](pallet_contracts_proc_macro::define_env) macro for the +/// emitted `Env` struct. +pub trait Environment { + /// Adds all declared functions to the supplied [`Linker`](wasmi::Linker) and + /// [`Store`](wasmi::Store). + fn define( + store: &mut Store, + linker: &mut Linker, + allow_unstable: AllowUnstableInterface, + allow_deprecated: AllowDeprecatedInterface, + ) -> Result<(), LinkerError>; +} + +/// Type of a storage key. +enum KeyType { + /// Legacy fix sized key `[u8;32]`. + Fix, + /// Variable sized key used in transparent hashing, + /// cannot be larger than MaxStorageKeyLen. + Var(u32), +} + +pub use pallet_contracts_uapi::ReturnErrorCode; + +parameter_types! { + /// Getter types used by [`crate::api_doc::Current::call_runtime`] + const CallRuntimeFailed: ReturnErrorCode = ReturnErrorCode::CallRuntimeFailed; + /// Getter types used by [`crate::api_doc::Current::xcm_execute`] + const XcmExecutionFailed: ReturnErrorCode = ReturnErrorCode::XcmExecutionFailed; +} + +impl From for ReturnErrorCode { + fn from(from: ExecReturnValue) -> Self { + if from.flags.contains(ReturnFlags::REVERT) { + Self::CalleeReverted + } else { + Self::Success + } + } +} + +/// The data passed through when a contract uses `seal_return`. +#[derive(RuntimeDebug)] +pub struct ReturnData { + /// The flags as passed through by the contract. They are still unchecked and + /// will later be parsed into a `ReturnFlags` bitflags struct. + flags: u32, + /// The output buffer passed by the contract as return data. + data: Vec, +} + +/// Enumerates all possible reasons why a trap was generated. +/// +/// This is either used to supply the caller with more information about why an error +/// occurred (the SupervisorError variant). +/// The other case is where the trap does not constitute an error but rather was invoked +/// as a quick way to terminate the application (all other variants). +#[derive(RuntimeDebug)] +pub enum TrapReason { + /// The supervisor trapped the contract because of an error condition occurred during + /// execution in privileged code. + SupervisorError(DispatchError), + /// Signals that trap was generated in response to call `seal_return` host function. + Return(ReturnData), + /// Signals that a trap was generated in response to a successful call to the + /// `seal_terminate` host function. + Termination, +} + +impl> From for TrapReason { + fn from(from: T) -> Self { + Self::SupervisorError(from.into()) + } +} + +impl fmt::Display for TrapReason { + fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + Ok(()) + } +} + +impl HostError for TrapReason {} + +#[cfg_attr(test, derive(Debug, PartialEq, Eq))] +#[derive(Copy, Clone)] +pub enum RuntimeCosts { + /// Base Weight of calling a host function. + HostFn, + /// Weight charged for copying data from the sandbox. + CopyFromContract(u32), + /// Weight charged for copying data to the sandbox. + CopyToContract(u32), + /// Weight of calling `seal_caller`. + Caller, + /// Weight of calling `seal_is_contract`. + IsContract, + /// Weight of calling `seal_code_hash`. + CodeHash, + /// Weight of calling `seal_own_code_hash`. + OwnCodeHash, + /// Weight of calling `seal_caller_is_origin`. + CallerIsOrigin, + /// Weight of calling `caller_is_root`. + CallerIsRoot, + /// Weight of calling `seal_address`. + Address, + /// Weight of calling `seal_gas_left`. + GasLeft, + /// Weight of calling `seal_balance`. + Balance, + /// Weight of calling `seal_value_transferred`. + ValueTransferred, + /// Weight of calling `seal_minimum_balance`. + MinimumBalance, + /// Weight of calling `seal_block_number`. + BlockNumber, + /// Weight of calling `seal_now`. + Now, + /// Weight of calling `seal_weight_to_fee`. + WeightToFee, + /// Weight of calling `seal_terminate`, passing the number of locked dependencies. + Terminate(u32), + /// Weight of calling `seal_random`. It includes the weight for copying the subject. + Random, + /// Weight of calling `seal_deposit_event` with the given number of topics and event size. + DepositEvent { num_topic: u32, len: u32 }, + /// Weight of calling `seal_debug_message` per byte of passed message. + DebugMessage(u32), + /// Weight of calling `seal_set_storage` for the given storage item sizes. + SetStorage { old_bytes: u32, new_bytes: u32 }, + /// Weight of calling `seal_clear_storage` per cleared byte. + ClearStorage(u32), + /// Weight of calling `seal_contains_storage` per byte of the checked item. + ContainsStorage(u32), + /// Weight of calling `seal_get_storage` with the specified size in storage. + GetStorage(u32), + /// Weight of calling `seal_take_storage` for the given size. + TakeStorage(u32), + /// Weight of calling `seal_set_transient_storage` for the given storage item sizes. + SetTransientStorage { old_bytes: u32, new_bytes: u32 }, + /// Weight of calling `seal_clear_transient_storage` per cleared byte. + ClearTransientStorage(u32), + /// Weight of calling `seal_contains_transient_storage` per byte of the checked item. + ContainsTransientStorage(u32), + /// Weight of calling `seal_get_transient_storage` with the specified size in storage. + GetTransientStorage(u32), + /// Weight of calling `seal_take_transient_storage` for the given size. + TakeTransientStorage(u32), + /// Weight of calling `seal_transfer`. + Transfer, + /// Base weight of calling `seal_call`. + CallBase, + /// Weight of calling `seal_delegate_call` for the given input size. + DelegateCallBase, + /// Weight of the transfer performed during a call. + CallTransferSurcharge, + /// Weight per byte that is cloned by supplying the `CLONE_INPUT` flag. + CallInputCloned(u32), + /// Weight of calling `seal_instantiate` for the given input length and salt. + Instantiate { input_data_len: u32, salt_len: u32 }, + /// Weight of calling `seal_hash_sha_256` for the given input size. + HashSha256(u32), + /// Weight of calling `seal_hash_keccak_256` for the given input size. + HashKeccak256(u32), + /// Weight of calling `seal_hash_blake2_256` for the given input size. + HashBlake256(u32), + /// Weight of calling `seal_hash_blake2_128` for the given input size. + HashBlake128(u32), + /// Weight of calling `seal_ecdsa_recover`. + EcdsaRecovery, + /// Weight of calling `seal_sr25519_verify` for the given input size. + Sr25519Verify(u32), + /// Weight charged by a chain extension through `seal_call_chain_extension`. + ChainExtension(Weight), + /// Weight charged for calling into the runtime. + CallRuntime(Weight), + /// Weight charged for calling xcm_execute. + CallXcmExecute(Weight), + /// Weight of calling `seal_set_code_hash` + SetCodeHash, + /// Weight of calling `ecdsa_to_eth_address` + EcdsaToEthAddress, + /// Weight of calling `reentrance_count` + ReentranceCount, + /// Weight of calling `account_reentrance_count` + AccountReentranceCount, + /// Weight of calling `instantiation_nonce` + InstantiationNonce, + /// Weight of calling `lock_delegate_dependency` + LockDelegateDependency, + /// Weight of calling `unlock_delegate_dependency` + UnlockDelegateDependency, +} + +/// For functions that modify storage, benchmarks are performed with one item in the +/// storage. To account for the worst-case scenario, the weight of the overhead of +/// writing to or reading from full storage is included. For transient storage writes, +/// the rollback weight is added to reflect the worst-case scenario for this operation. +macro_rules! cost_storage { + (write_transient, $name:ident $(, $arg:expr )*) => { + T::WeightInfo::$name($( $arg ),*) + .saturating_add(T::WeightInfo::rollback_transient_storage()) + .saturating_add(T::WeightInfo::set_transient_storage_full() + .saturating_sub(T::WeightInfo::set_transient_storage_empty())) + }; + + (read_transient, $name:ident $(, $arg:expr )*) => { + T::WeightInfo::$name($( $arg ),*) + .saturating_add(T::WeightInfo::get_transient_storage_full() + .saturating_sub(T::WeightInfo::get_transient_storage_empty())) + }; + + (write, $name:ident $(, $arg:expr )*) => { + T::WeightInfo::$name($( $arg ),*) + .saturating_add(T::WeightInfo::set_storage_full() + .saturating_sub(T::WeightInfo::set_storage_empty())) + }; + + (read, $name:ident $(, $arg:expr )*) => { + T::WeightInfo::$name($( $arg ),*) + .saturating_add(T::WeightInfo::get_storage_full() + .saturating_sub(T::WeightInfo::get_storage_empty())) + }; +} + +macro_rules! cost_args { + // cost_args!(name, a, b, c) -> T::WeightInfo::name(a, b, c).saturating_sub(T::WeightInfo::name(0, 0, 0)) + ($name:ident, $( $arg: expr ),+) => { + (T::WeightInfo::$name($( $arg ),+).saturating_sub(cost_args!(@call_zero $name, $( $arg ),+))) + }; + // Transform T::WeightInfo::name(a, b, c) into T::WeightInfo::name(0, 0, 0) + (@call_zero $name:ident, $( $arg:expr ),*) => { + T::WeightInfo::$name($( cost_args!(@replace_token $arg) ),*) + }; + // Replace the token with 0. + (@replace_token $_in:tt) => { 0 }; +} + +impl Token for RuntimeCosts { + fn influence_lowest_gas_limit(&self) -> bool { + match self { + &Self::CallXcmExecute(_) => false, + _ => true, + } + } + + fn weight(&self) -> Weight { + use self::RuntimeCosts::*; + match *self { + HostFn => cost_args!(noop_host_fn, 1), + CopyToContract(len) => T::WeightInfo::seal_input(len), + CopyFromContract(len) => T::WeightInfo::seal_return(len), + Caller => T::WeightInfo::seal_caller(), + IsContract => T::WeightInfo::seal_is_contract(), + CodeHash => T::WeightInfo::seal_code_hash(), + OwnCodeHash => T::WeightInfo::seal_own_code_hash(), + CallerIsOrigin => T::WeightInfo::seal_caller_is_origin(), + CallerIsRoot => T::WeightInfo::seal_caller_is_root(), + Address => T::WeightInfo::seal_address(), + GasLeft => T::WeightInfo::seal_gas_left(), + Balance => T::WeightInfo::seal_balance(), + ValueTransferred => T::WeightInfo::seal_value_transferred(), + MinimumBalance => T::WeightInfo::seal_minimum_balance(), + BlockNumber => T::WeightInfo::seal_block_number(), + Now => T::WeightInfo::seal_now(), + WeightToFee => T::WeightInfo::seal_weight_to_fee(), + Terminate(locked_dependencies) => T::WeightInfo::seal_terminate(locked_dependencies), + Random => T::WeightInfo::seal_random(), + // Given a 2-second block time and hardcoding a `ref_time` of 60,000 picoseconds per + // byte (event_ref_time), the max allocation size is 32MB per block. + DepositEvent { num_topic, len } => T::WeightInfo::seal_deposit_event(num_topic, len) + .saturating_add(Weight::from_parts( + T::Schedule::get().limits.event_ref_time.saturating_mul(len.into()), + 0, + )), + DebugMessage(len) => T::WeightInfo::seal_debug_message(len), + SetStorage { new_bytes, old_bytes } => + cost_storage!(write, seal_set_storage, new_bytes, old_bytes), + ClearStorage(len) => cost_storage!(write, seal_clear_storage, len), + ContainsStorage(len) => cost_storage!(read, seal_contains_storage, len), + GetStorage(len) => cost_storage!(read, seal_get_storage, len), + TakeStorage(len) => cost_storage!(write, seal_take_storage, len), + SetTransientStorage { new_bytes, old_bytes } => + cost_storage!(write_transient, seal_set_transient_storage, new_bytes, old_bytes), + ClearTransientStorage(len) => + cost_storage!(write_transient, seal_clear_transient_storage, len), + ContainsTransientStorage(len) => + cost_storage!(read_transient, seal_contains_transient_storage, len), + GetTransientStorage(len) => + cost_storage!(read_transient, seal_get_transient_storage, len), + TakeTransientStorage(len) => + cost_storage!(write_transient, seal_take_transient_storage, len), + Transfer => T::WeightInfo::seal_transfer(), + CallBase => T::WeightInfo::seal_call(0, 0), + DelegateCallBase => T::WeightInfo::seal_delegate_call(), + CallTransferSurcharge => cost_args!(seal_call, 1, 0), + CallInputCloned(len) => cost_args!(seal_call, 0, len), + Instantiate { input_data_len, salt_len } => + T::WeightInfo::seal_instantiate(input_data_len, salt_len), + HashSha256(len) => T::WeightInfo::seal_hash_sha2_256(len), + HashKeccak256(len) => T::WeightInfo::seal_hash_keccak_256(len), + HashBlake256(len) => T::WeightInfo::seal_hash_blake2_256(len), + HashBlake128(len) => T::WeightInfo::seal_hash_blake2_128(len), + EcdsaRecovery => T::WeightInfo::seal_ecdsa_recover(), + Sr25519Verify(len) => T::WeightInfo::seal_sr25519_verify(len), + ChainExtension(weight) | CallRuntime(weight) | CallXcmExecute(weight) => weight, + SetCodeHash => T::WeightInfo::seal_set_code_hash(), + EcdsaToEthAddress => T::WeightInfo::seal_ecdsa_to_eth_address(), + ReentranceCount => T::WeightInfo::seal_reentrance_count(), + AccountReentranceCount => T::WeightInfo::seal_account_reentrance_count(), + InstantiationNonce => T::WeightInfo::seal_instantiation_nonce(), + LockDelegateDependency => T::WeightInfo::lock_delegate_dependency(), + UnlockDelegateDependency => T::WeightInfo::unlock_delegate_dependency(), + } + } +} + +/// Same as [`Runtime::charge_gas`]. +/// +/// We need this access as a macro because sometimes hiding the lifetimes behind +/// a function won't work out. +macro_rules! charge_gas { + ($runtime:expr, $costs:expr) => {{ + $runtime.ext.gas_meter_mut().charge($costs) + }}; +} + +/// The kind of call that should be performed. +enum CallType { + /// Execute another instantiated contract + Call { callee_ptr: u32, value_ptr: u32, deposit_ptr: u32, weight: Weight }, + /// Execute deployed code in the context (storage, account ID, value) of the caller contract + DelegateCall { code_hash_ptr: u32 }, +} + +impl CallType { + fn cost(&self) -> RuntimeCosts { + match self { + CallType::Call { .. } => RuntimeCosts::CallBase, + CallType::DelegateCall { .. } => RuntimeCosts::DelegateCallBase, + } + } +} + +/// This is only appropriate when writing out data of constant size that does not depend on user +/// input. In this case the costs for this copy was already charged as part of the token at +/// the beginning of the API entry point. +fn already_charged(_: u32) -> Option { + None +} + +/// Can only be used for one call. +pub struct Runtime<'a, E: Ext + 'a> { + ext: &'a mut E, + input_data: Option>, + memory: Option, + chain_extension: Option::ChainExtension>>, +} + +impl<'a, E: Ext + 'a> Runtime<'a, E> { + pub fn new(ext: &'a mut E, input_data: Vec) -> Self { + Runtime { + ext, + input_data: Some(input_data), + memory: None, + chain_extension: Some(Box::new(Default::default())), + } + } + + pub fn memory(&self) -> Option { + self.memory + } + + pub fn set_memory(&mut self, memory: Memory) { + self.memory = Some(memory); + } + + /// Converts the sandbox result and the runtime state into the execution outcome. + pub fn to_execution_result(self, sandbox_result: Result<(), wasmi::Error>) -> ExecResult { + use wasmi::{ + core::TrapCode, + errors::{ErrorKind, FuelError}, + }; + use TrapReason::*; + + let Err(error) = sandbox_result else { + // Contract returned from main function -> no data was returned. + return Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() }) + }; + if let ErrorKind::Fuel(FuelError::OutOfFuel) = error.kind() { + // `OutOfGas` when host asks engine to consume more than left in the _store_. + // We should never get this case, as gas meter is being charged (and hence raises error) + // first. + return Err(Error::::OutOfGas.into()) + } + match error.as_trap_code() { + Some(TrapCode::OutOfFuel) => { + // `OutOfGas` during engine execution. + return Err(Error::::OutOfGas.into()) + }, + Some(_trap_code) => { + // Otherwise the trap came from the contract itself. + return Err(Error::::ContractTrapped.into()) + }, + None => {}, + } + // If we encoded a reason then it is some abort generated by a host function. + if let Some(reason) = &error.downcast_ref::() { + match &reason { + Return(ReturnData { flags, data }) => { + let flags = + ReturnFlags::from_bits(*flags).ok_or(Error::::InvalidCallFlags)?; + return Ok(ExecReturnValue { flags, data: data.to_vec() }) + }, + Termination => + return Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() }), + SupervisorError(error) => return Err((*error).into()), + } + } + + // Any other error is returned only if instantiation or linking failed (i.e. + // wasm binary tried to import a function that is not provided by the host). + // This shouldn't happen because validation process ought to reject such binaries. + // + // Because panics are really undesirable in the runtime code, we treat this as + // a trap for now. Eventually, we might want to revisit this. + log::debug!("Code rejected: {:?}", error); + Err(Error::::CodeRejected.into()) + } + + /// Get a mutable reference to the inner `Ext`. + /// + /// This is mainly for the chain extension to have access to the environment the + /// contract is executing in. + pub fn ext(&mut self) -> &mut E { + self.ext + } + + /// Charge the gas meter with the specified token. + /// + /// Returns `Err(HostError)` if there is not enough gas. + pub fn charge_gas(&mut self, costs: RuntimeCosts) -> Result { + charge_gas!(self, costs) + } + + /// Adjust a previously charged amount down to its actual amount. + /// + /// This is when a maximum a priori amount was charged and then should be partially + /// refunded to match the actual amount. + pub fn adjust_gas(&mut self, charged: ChargedAmount, actual_costs: RuntimeCosts) { + self.ext.gas_meter_mut().adjust_gas(charged, actual_costs); + } + + /// Charge, Run and adjust gas, for executing the given dispatchable. + fn call_dispatchable>( + &mut self, + dispatch_info: DispatchInfo, + runtime_cost: impl Fn(Weight) -> RuntimeCosts, + run: impl FnOnce(&mut Self) -> DispatchResultWithPostInfo, + ) -> Result { + use frame_support::dispatch::extract_actual_weight; + let charged = self.charge_gas(runtime_cost(dispatch_info.weight))?; + let result = run(self); + let actual_weight = extract_actual_weight(&result, &dispatch_info); + self.adjust_gas(charged, runtime_cost(actual_weight)); + match result { + Ok(_) => Ok(ReturnErrorCode::Success), + Err(e) => { + if self.ext.debug_buffer_enabled() { + self.ext.append_debug_buffer("call failed with: "); + self.ext.append_debug_buffer(e.into()); + }; + Ok(ErrorReturnCode::get()) + }, + } + } + + /// Read designated chunk from the sandbox memory. + /// + /// Returns `Err` if one of the following conditions occurs: + /// + /// - requested buffer is not within the bounds of the sandbox memory. + pub fn read_sandbox_memory( + &self, + memory: &[u8], + ptr: u32, + len: u32, + ) -> Result, DispatchError> { + ensure!(len <= self.ext.schedule().limits.max_memory_size(), Error::::OutOfBounds); + let mut buf = vec![0u8; len as usize]; + self.read_sandbox_memory_into_buf(memory, ptr, buf.as_mut_slice())?; + Ok(buf) + } + + /// Read designated chunk from the sandbox memory into the supplied buffer. + /// + /// Returns `Err` if one of the following conditions occurs: + /// + /// - requested buffer is not within the bounds of the sandbox memory. + pub fn read_sandbox_memory_into_buf( + &self, + memory: &[u8], + ptr: u32, + buf: &mut [u8], + ) -> Result<(), DispatchError> { + let ptr = ptr as usize; + let bound_checked = + memory.get(ptr..ptr + buf.len()).ok_or_else(|| Error::::OutOfBounds)?; + buf.copy_from_slice(bound_checked); + Ok(()) + } + + /// Reads and decodes a type with a size fixed at compile time from contract memory. + /// + /// # Note + /// + /// The weight of reading a fixed value is included in the overall weight of any + /// contract callable function. + pub fn read_sandbox_memory_as( + &self, + memory: &[u8], + ptr: u32, + ) -> Result { + let ptr = ptr as usize; + let mut bound_checked = memory.get(ptr..).ok_or_else(|| Error::::OutOfBounds)?; + + let decoded = D::decode_with_depth_limit(MAX_DECODE_NESTING, &mut bound_checked) + .map_err(|_| DispatchError::from(Error::::DecodingFailed))?; + Ok(decoded) + } + + /// Read designated chunk from the sandbox memory and attempt to decode into the specified type. + /// + /// Returns `Err` if one of the following conditions occurs: + /// + /// - requested buffer is not within the bounds of the sandbox memory. + /// - the buffer contents cannot be decoded as the required type. + /// + /// # Note + /// + /// There must be an extra benchmark for determining the influence of `len` with + /// regard to the overall weight. + pub fn read_sandbox_memory_as_unbounded( + &self, + memory: &[u8], + ptr: u32, + len: u32, + ) -> Result { + let ptr = ptr as usize; + let mut bound_checked = + memory.get(ptr..ptr + len as usize).ok_or_else(|| Error::::OutOfBounds)?; + + let decoded = D::decode_all_with_depth_limit(MAX_DECODE_NESTING, &mut bound_checked) + .map_err(|_| DispatchError::from(Error::::DecodingFailed))?; + + Ok(decoded) + } + + /// Write the given buffer and its length to the designated locations in sandbox memory and + /// charge gas according to the token returned by `create_token`. + // + /// `out_ptr` is the location in sandbox memory where `buf` should be written to. + /// `out_len_ptr` is an in-out location in sandbox memory. It is read to determine the + /// length of the buffer located at `out_ptr`. If that buffer is large enough the actual + /// `buf.len()` is written to this location. + /// + /// If `out_ptr` is set to the sentinel value of `SENTINEL` and `allow_skip` is true the + /// operation is skipped and `Ok` is returned. This is supposed to help callers to make copying + /// output optional. For example to skip copying back the output buffer of an `seal_call` + /// when the caller is not interested in the result. + /// + /// `create_token` can optionally instruct this function to charge the gas meter with the token + /// it returns. `create_token` receives the variable amount of bytes that are about to be copied + /// by this function. + /// + /// In addition to the error conditions of `write_sandbox_memory` this functions returns + /// `Err` if the size of the buffer located at `out_ptr` is too small to fit `buf`. + pub fn write_sandbox_output( + &mut self, + memory: &mut [u8], + out_ptr: u32, + out_len_ptr: u32, + buf: &[u8], + allow_skip: bool, + create_token: impl FnOnce(u32) -> Option, + ) -> Result<(), DispatchError> { + if allow_skip && out_ptr == SENTINEL { + return Ok(()) + } + + let buf_len = buf.len() as u32; + let len: u32 = self.read_sandbox_memory_as(memory, out_len_ptr)?; + + if len < buf_len { + return Err(Error::::OutputBufferTooSmall.into()) + } + + if let Some(costs) = create_token(buf_len) { + self.charge_gas(costs)?; + } + + self.write_sandbox_memory(memory, out_ptr, buf)?; + self.write_sandbox_memory(memory, out_len_ptr, &buf_len.encode()) + } + + /// Write the given buffer to the designated location in the sandbox memory. + /// + /// Returns `Err` if one of the following conditions occurs: + /// + /// - designated area is not within the bounds of the sandbox memory. + fn write_sandbox_memory( + &self, + memory: &mut [u8], + ptr: u32, + buf: &[u8], + ) -> Result<(), DispatchError> { + let ptr = ptr as usize; + let bound_checked = + memory.get_mut(ptr..ptr + buf.len()).ok_or_else(|| Error::::OutOfBounds)?; + bound_checked.copy_from_slice(buf); + Ok(()) + } + + /// Computes the given hash function on the supplied input. + /// + /// Reads from the sandboxed input buffer into an intermediate buffer. + /// Returns the result directly to the output buffer of the sandboxed memory. + /// + /// It is the callers responsibility to provide an output buffer that + /// is large enough to hold the expected amount of bytes returned by the + /// chosen hash function. + /// + /// # Note + /// + /// The `input` and `output` buffers may overlap. + fn compute_hash_on_intermediate_buffer( + &self, + memory: &mut [u8], + hash_fn: F, + input_ptr: u32, + input_len: u32, + output_ptr: u32, + ) -> Result<(), DispatchError> + where + F: FnOnce(&[u8]) -> R, + R: AsRef<[u8]>, + { + // Copy input into supervisor memory. + let input = self.read_sandbox_memory(memory, input_ptr, input_len)?; + // Compute the hash on the input buffer using the given hash function. + let hash = hash_fn(&input); + // Write the resulting hash back into the sandboxed output buffer. + self.write_sandbox_memory(memory, output_ptr, hash.as_ref())?; + Ok(()) + } + + /// Fallible conversion of `DispatchError` to `ReturnErrorCode`. + fn err_into_return_code(from: DispatchError) -> Result { + use ReturnErrorCode::*; + + let transfer_failed = Error::::TransferFailed.into(); + let no_code = Error::::CodeNotFound.into(); + let not_found = Error::::ContractNotFound.into(); + + match from { + x if x == transfer_failed => Ok(TransferFailed), + x if x == no_code => Ok(CodeNotFound), + x if x == not_found => Ok(NotCallable), + err => Err(err), + } + } + + /// Fallible conversion of a `ExecResult` to `ReturnErrorCode`. + fn exec_into_return_code(from: ExecResult) -> Result { + use crate::exec::ErrorOrigin::Callee; + + let ExecError { error, origin } = match from { + Ok(retval) => return Ok(retval.into()), + Err(err) => err, + }; + + match (error, origin) { + (_, Callee) => Ok(ReturnErrorCode::CalleeTrapped), + (err, _) => Self::err_into_return_code(err), + } + } + fn decode_key( + &self, + memory: &[u8], + key_type: KeyType, + key_ptr: u32, + ) -> Result, TrapReason> { + let res = match key_type { + KeyType::Fix => { + let key = self.read_sandbox_memory(memory, key_ptr, 32u32)?; + Key::try_from_fix(key) + }, + KeyType::Var(len) => { + ensure!( + len <= <::T as Config>::MaxStorageKeyLen::get(), + Error::::DecodingFailed + ); + let key = self.read_sandbox_memory(memory, key_ptr, len)?; + Key::try_from_var(key) + }, + }; + + res.map_err(|_| Error::::DecodingFailed.into()) + } + + fn set_storage( + &mut self, + memory: &[u8], + key_type: KeyType, + key_ptr: u32, + value_ptr: u32, + value_len: u32, + ) -> Result { + let max_size = self.ext.max_value_size(); + let charged = self + .charge_gas(RuntimeCosts::SetStorage { new_bytes: value_len, old_bytes: max_size })?; + if value_len > max_size { + return Err(Error::::ValueTooLarge.into()) + } + let key = self.decode_key(memory, key_type, key_ptr)?; + let value = Some(self.read_sandbox_memory(memory, value_ptr, value_len)?); + let write_outcome = self.ext.set_storage(&key, value, false)?; + + self.adjust_gas( + charged, + RuntimeCosts::SetStorage { new_bytes: value_len, old_bytes: write_outcome.old_len() }, + ); + Ok(write_outcome.old_len_with_sentinel()) + } + + fn clear_storage( + &mut self, + memory: &[u8], + key_type: KeyType, + key_ptr: u32, + ) -> Result { + let charged = self.charge_gas(RuntimeCosts::ClearStorage(self.ext.max_value_size()))?; + let key = self.decode_key(memory, key_type, key_ptr)?; + let outcome = self.ext.set_storage(&key, None, false)?; + + self.adjust_gas(charged, RuntimeCosts::ClearStorage(outcome.old_len())); + Ok(outcome.old_len_with_sentinel()) + } + + fn get_storage( + &mut self, + memory: &mut [u8], + key_type: KeyType, + key_ptr: u32, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result { + let charged = self.charge_gas(RuntimeCosts::GetStorage(self.ext.max_value_size()))?; + let key = self.decode_key(memory, key_type, key_ptr)?; + let outcome = self.ext.get_storage(&key); + + if let Some(value) = outcome { + self.adjust_gas(charged, RuntimeCosts::GetStorage(value.len() as u32)); + self.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &value, + false, + already_charged, + )?; + Ok(ReturnErrorCode::Success) + } else { + self.adjust_gas(charged, RuntimeCosts::GetStorage(0)); + Ok(ReturnErrorCode::KeyNotFound) + } + } + + fn contains_storage( + &mut self, + memory: &[u8], + key_type: KeyType, + key_ptr: u32, + ) -> Result { + let charged = self.charge_gas(RuntimeCosts::ContainsStorage(self.ext.max_value_size()))?; + let key = self.decode_key(memory, key_type, key_ptr)?; + let outcome = self.ext.get_storage_size(&key); + + self.adjust_gas(charged, RuntimeCosts::ContainsStorage(outcome.unwrap_or(0))); + Ok(outcome.unwrap_or(SENTINEL)) + } + + fn set_transient_storage( + &mut self, + memory: &[u8], + key_type: KeyType, + key_ptr: u32, + value_ptr: u32, + value_len: u32, + ) -> Result { + let max_size = self.ext.max_value_size(); + let charged = self.charge_gas(RuntimeCosts::SetTransientStorage { + new_bytes: value_len, + old_bytes: max_size, + })?; + if value_len > max_size { + return Err(Error::::ValueTooLarge.into()) + } + let key = self.decode_key(memory, key_type, key_ptr)?; + let value = Some(self.read_sandbox_memory(memory, value_ptr, value_len)?); + let write_outcome = self.ext.set_transient_storage(&key, value, false)?; + self.adjust_gas( + charged, + RuntimeCosts::SetTransientStorage { + new_bytes: value_len, + old_bytes: write_outcome.old_len(), + }, + ); + Ok(write_outcome.old_len_with_sentinel()) + } + + fn clear_transient_storage( + &mut self, + memory: &[u8], + key_type: KeyType, + key_ptr: u32, + ) -> Result { + let charged = + self.charge_gas(RuntimeCosts::ClearTransientStorage(self.ext.max_value_size()))?; + let key = self.decode_key(memory, key_type, key_ptr)?; + let outcome = self.ext.set_transient_storage(&key, None, false)?; + + self.adjust_gas(charged, RuntimeCosts::ClearTransientStorage(outcome.old_len())); + Ok(outcome.old_len_with_sentinel()) + } + + fn get_transient_storage( + &mut self, + memory: &mut [u8], + key_type: KeyType, + key_ptr: u32, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result { + let charged = + self.charge_gas(RuntimeCosts::GetTransientStorage(self.ext.max_value_size()))?; + let key = self.decode_key(memory, key_type, key_ptr)?; + let outcome = self.ext.get_transient_storage(&key); + + if let Some(value) = outcome { + self.adjust_gas(charged, RuntimeCosts::GetTransientStorage(value.len() as u32)); + self.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &value, + false, + already_charged, + )?; + Ok(ReturnErrorCode::Success) + } else { + self.adjust_gas(charged, RuntimeCosts::GetTransientStorage(0)); + Ok(ReturnErrorCode::KeyNotFound) + } + } + + fn contains_transient_storage( + &mut self, + memory: &[u8], + key_type: KeyType, + key_ptr: u32, + ) -> Result { + let charged = + self.charge_gas(RuntimeCosts::ContainsTransientStorage(self.ext.max_value_size()))?; + let key = self.decode_key(memory, key_type, key_ptr)?; + let outcome = self.ext.get_transient_storage_size(&key); + + self.adjust_gas(charged, RuntimeCosts::ContainsTransientStorage(outcome.unwrap_or(0))); + Ok(outcome.unwrap_or(SENTINEL)) + } + + fn take_transient_storage( + &mut self, + memory: &mut [u8], + key_type: KeyType, + key_ptr: u32, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result { + let charged = + self.charge_gas(RuntimeCosts::TakeTransientStorage(self.ext.max_value_size()))?; + let key = self.decode_key(memory, key_type, key_ptr)?; + if let crate::storage::WriteOutcome::Taken(value) = + self.ext.set_transient_storage(&key, None, true)? + { + self.adjust_gas(charged, RuntimeCosts::TakeTransientStorage(value.len() as u32)); + self.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &value, + false, + already_charged, + )?; + Ok(ReturnErrorCode::Success) + } else { + self.adjust_gas(charged, RuntimeCosts::TakeTransientStorage(0)); + Ok(ReturnErrorCode::KeyNotFound) + } + } + + fn call( + &mut self, + memory: &mut [u8], + flags: CallFlags, + call_type: CallType, + input_data_ptr: u32, + input_data_len: u32, + output_ptr: u32, + output_len_ptr: u32, + ) -> Result { + self.charge_gas(call_type.cost())?; + + let input_data = if flags.contains(CallFlags::CLONE_INPUT) { + let input = self.input_data.as_ref().ok_or(Error::::InputForwarded)?; + charge_gas!(self, RuntimeCosts::CallInputCloned(input.len() as u32))?; + input.clone() + } else if flags.contains(CallFlags::FORWARD_INPUT) { + self.input_data.take().ok_or(Error::::InputForwarded)? + } else { + self.charge_gas(RuntimeCosts::CopyFromContract(input_data_len))?; + self.read_sandbox_memory(memory, input_data_ptr, input_data_len)? + }; + + let call_outcome = match call_type { + CallType::Call { callee_ptr, value_ptr, deposit_ptr, weight } => { + let callee: <::T as frame_system::Config>::AccountId = + self.read_sandbox_memory_as(memory, callee_ptr)?; + let deposit_limit: BalanceOf<::T> = if deposit_ptr == SENTINEL { + BalanceOf::<::T>::zero() + } else { + self.read_sandbox_memory_as(memory, deposit_ptr)? + }; + let read_only = flags.contains(CallFlags::READ_ONLY); + let value: BalanceOf<::T> = + self.read_sandbox_memory_as(memory, value_ptr)?; + if value > 0u32.into() { + // If the call value is non-zero and state change is not allowed, issue an + // error. + if read_only || self.ext.is_read_only() { + return Err(Error::::StateChangeDenied.into()); + } + self.charge_gas(RuntimeCosts::CallTransferSurcharge)?; + } + self.ext.call( + weight, + deposit_limit, + callee, + value, + input_data, + flags.contains(CallFlags::ALLOW_REENTRY), + read_only, + ) + }, + CallType::DelegateCall { code_hash_ptr } => { + if flags.intersects(CallFlags::ALLOW_REENTRY | CallFlags::READ_ONLY) { + return Err(Error::::InvalidCallFlags.into()) + } + let code_hash = self.read_sandbox_memory_as(memory, code_hash_ptr)?; + self.ext.delegate_call(code_hash, input_data) + }, + }; + + // `TAIL_CALL` only matters on an `OK` result. Otherwise the call stack comes to + // a halt anyways without anymore code being executed. + if flags.contains(CallFlags::TAIL_CALL) { + if let Ok(return_value) = call_outcome { + return Err(TrapReason::Return(ReturnData { + flags: return_value.flags.bits(), + data: return_value.data, + })) + } + } + + if let Ok(output) = &call_outcome { + self.write_sandbox_output( + memory, + output_ptr, + output_len_ptr, + &output.data, + true, + |len| Some(RuntimeCosts::CopyToContract(len)), + )?; + } + Ok(Runtime::::exec_into_return_code(call_outcome)?) + } + + fn instantiate( + &mut self, + memory: &mut [u8], + code_hash_ptr: u32, + weight: Weight, + deposit_ptr: u32, + value_ptr: u32, + input_data_ptr: u32, + input_data_len: u32, + address_ptr: u32, + address_len_ptr: u32, + output_ptr: u32, + output_len_ptr: u32, + salt_ptr: u32, + salt_len: u32, + ) -> Result { + self.charge_gas(RuntimeCosts::Instantiate { input_data_len, salt_len })?; + let deposit_limit: BalanceOf<::T> = if deposit_ptr == SENTINEL { + BalanceOf::<::T>::zero() + } else { + self.read_sandbox_memory_as(memory, deposit_ptr)? + }; + let value: BalanceOf<::T> = self.read_sandbox_memory_as(memory, value_ptr)?; + let code_hash: CodeHash<::T> = + self.read_sandbox_memory_as(memory, code_hash_ptr)?; + let input_data = self.read_sandbox_memory(memory, input_data_ptr, input_data_len)?; + let salt = self.read_sandbox_memory(memory, salt_ptr, salt_len)?; + let instantiate_outcome = + self.ext.instantiate(weight, deposit_limit, code_hash, value, input_data, &salt); + if let Ok((address, output)) = &instantiate_outcome { + if !output.flags.contains(ReturnFlags::REVERT) { + self.write_sandbox_output( + memory, + address_ptr, + address_len_ptr, + &address.encode(), + true, + already_charged, + )?; + } + self.write_sandbox_output( + memory, + output_ptr, + output_len_ptr, + &output.data, + true, + |len| Some(RuntimeCosts::CopyToContract(len)), + )?; + } + Ok(Runtime::::exec_into_return_code(instantiate_outcome.map(|(_, retval)| retval))?) + } + + fn terminate(&mut self, memory: &[u8], beneficiary_ptr: u32) -> Result<(), TrapReason> { + let count = self.ext.locked_delegate_dependencies_count() as _; + self.charge_gas(RuntimeCosts::Terminate(count))?; + + let beneficiary: <::T as frame_system::Config>::AccountId = + self.read_sandbox_memory_as(memory, beneficiary_ptr)?; + self.ext.terminate(&beneficiary)?; + Err(TrapReason::Termination) + } +} + +// This is the API exposed to contracts. +// +// # Note +// +// Any input that leads to a out of bound error (reading or writing) or failing to decode +// data passed to the supervisor will lead to a trap. This is not documented explicitly +// for every function. +#[define_env(doc)] +pub mod env { + + /// Noop function used to benchmark the time it takes to execute an empty function. + #[cfg(feature = "runtime-benchmarks")] + #[unstable] + fn noop(ctx: _, memory: _) -> Result<(), TrapReason> { + Ok(()) + } + + /// Set the value at the given key in the contract storage. + /// See [`pallet_contracts_uapi::HostFn::set_storage`] + #[prefixed_alias] + #[mutating] + fn set_storage( + ctx: _, + memory: _, + key_ptr: u32, + value_ptr: u32, + value_len: u32, + ) -> Result<(), TrapReason> { + ctx.set_storage(memory, KeyType::Fix, key_ptr, value_ptr, value_len).map(|_| ()) + } + + /// Set the value at the given key in the contract storage. + /// See [`pallet_contracts_uapi::HostFn::set_storage_v1`] + #[version(1)] + #[prefixed_alias] + #[mutating] + fn set_storage( + ctx: _, + memory: _, + key_ptr: u32, + value_ptr: u32, + value_len: u32, + ) -> Result { + ctx.set_storage(memory, KeyType::Fix, key_ptr, value_ptr, value_len) + } + + /// Set the value at the given key in the contract storage. + /// See [`pallet_contracts_uapi::HostFn::set_storage_v2`] + #[version(2)] + #[prefixed_alias] + #[mutating] + fn set_storage( + ctx: _, + memory: _, + key_ptr: u32, + key_len: u32, + value_ptr: u32, + value_len: u32, + ) -> Result { + ctx.set_storage(memory, KeyType::Var(key_len), key_ptr, value_ptr, value_len) + } + + /// Clear the value at the given key in the contract storage. + /// See [`pallet_contracts_uapi::HostFn::clear_storage`] + #[prefixed_alias] + #[mutating] + fn clear_storage(ctx: _, memory: _, key_ptr: u32) -> Result<(), TrapReason> { + ctx.clear_storage(memory, KeyType::Fix, key_ptr).map(|_| ()) + } + + /// Clear the value at the given key in the contract storage. + /// See [`pallet_contracts_uapi::HostFn::clear_storage_v1`] + #[version(1)] + #[prefixed_alias] + #[mutating] + fn clear_storage(ctx: _, memory: _, key_ptr: u32, key_len: u32) -> Result { + ctx.clear_storage(memory, KeyType::Var(key_len), key_ptr) + } + + /// Retrieve the value under the given key from storage. + /// See [`pallet_contracts_uapi::HostFn::get_storage`] + #[prefixed_alias] + fn get_storage( + ctx: _, + memory: _, + key_ptr: u32, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result { + ctx.get_storage(memory, KeyType::Fix, key_ptr, out_ptr, out_len_ptr) + } + + /// Retrieve the value under the given key from storage. + /// See [`pallet_contracts_uapi::HostFn::get_storage_v1`] + #[version(1)] + #[prefixed_alias] + fn get_storage( + ctx: _, + memory: _, + key_ptr: u32, + key_len: u32, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result { + ctx.get_storage(memory, KeyType::Var(key_len), key_ptr, out_ptr, out_len_ptr) + } + + /// Checks whether there is a value stored under the given key. + /// See [`pallet_contracts_uapi::HostFn::contains_storage`] + #[prefixed_alias] + fn contains_storage(ctx: _, memory: _, key_ptr: u32) -> Result { + ctx.contains_storage(memory, KeyType::Fix, key_ptr) + } + + /// Checks whether there is a value stored under the given key. + /// See [`pallet_contracts_uapi::HostFn::contains_storage_v1`] + #[version(1)] + #[prefixed_alias] + fn contains_storage(ctx: _, memory: _, key_ptr: u32, key_len: u32) -> Result { + ctx.contains_storage(memory, KeyType::Var(key_len), key_ptr) + } + + /// Retrieve and remove the value under the given key from storage. + /// See [`pallet_contracts_uapi::HostFn::take_storage`] + #[prefixed_alias] + #[mutating] + fn take_storage( + ctx: _, + memory: _, + key_ptr: u32, + key_len: u32, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result { + let charged = ctx.charge_gas(RuntimeCosts::TakeStorage(ctx.ext.max_value_size()))?; + ensure!( + key_len <= <::T as Config>::MaxStorageKeyLen::get(), + Error::::DecodingFailed + ); + let key = ctx.read_sandbox_memory(memory, key_ptr, key_len)?; + if let crate::storage::WriteOutcome::Taken(value) = ctx.ext.set_storage( + &Key::::try_from_var(key).map_err(|_| Error::::DecodingFailed)?, + None, + true, + )? { + ctx.adjust_gas(charged, RuntimeCosts::TakeStorage(value.len() as u32)); + ctx.write_sandbox_output(memory, out_ptr, out_len_ptr, &value, false, already_charged)?; + Ok(ReturnErrorCode::Success) + } else { + ctx.adjust_gas(charged, RuntimeCosts::TakeStorage(0)); + Ok(ReturnErrorCode::KeyNotFound) + } + } + + /// Set the value at the given key in the contract transient storage. + #[unstable] + fn set_transient_storage( + ctx: _, + memory: _, + key_ptr: u32, + key_len: u32, + value_ptr: u32, + value_len: u32, + ) -> Result { + ctx.set_transient_storage(memory, KeyType::Var(key_len), key_ptr, value_ptr, value_len) + } + + /// Clear the value at the given key in the contract storage. + #[unstable] + fn clear_transient_storage( + ctx: _, + memory: _, + key_ptr: u32, + key_len: u32, + ) -> Result { + ctx.clear_transient_storage(memory, KeyType::Var(key_len), key_ptr) + } + + /// Retrieve the value under the given key from transient storage. + #[unstable] + fn get_transient_storage( + ctx: _, + memory: _, + key_ptr: u32, + key_len: u32, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result { + ctx.get_transient_storage(memory, KeyType::Var(key_len), key_ptr, out_ptr, out_len_ptr) + } + + /// Checks whether there is a value stored under the given key in transient storage. + #[unstable] + fn contains_transient_storage( + ctx: _, + memory: _, + key_ptr: u32, + key_len: u32, + ) -> Result { + ctx.contains_transient_storage(memory, KeyType::Var(key_len), key_ptr) + } + + /// Retrieve and remove the value under the given key from transient storage. + #[unstable] + fn take_transient_storage( + ctx: _, + memory: _, + key_ptr: u32, + key_len: u32, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result { + ctx.take_transient_storage(memory, KeyType::Var(key_len), key_ptr, out_ptr, out_len_ptr) + } + + /// Transfer some value to another account. + /// See [`pallet_contracts_uapi::HostFn::transfer`]. + #[prefixed_alias] + #[mutating] + fn transfer( + ctx: _, + memory: _, + account_ptr: u32, + _account_len: u32, + value_ptr: u32, + _value_len: u32, + ) -> Result { + ctx.charge_gas(RuntimeCosts::Transfer)?; + let callee: <::T as frame_system::Config>::AccountId = + ctx.read_sandbox_memory_as(memory, account_ptr)?; + let value: BalanceOf<::T> = ctx.read_sandbox_memory_as(memory, value_ptr)?; + let result = ctx.ext.transfer(&callee, value); + match result { + Ok(()) => Ok(ReturnErrorCode::Success), + Err(err) => { + let code = Runtime::::err_into_return_code(err)?; + Ok(code) + }, + } + } + + /// Make a call to another contract. + /// + /// # Note + /// + /// The values `_callee_len` and `_value_len` are ignored because the encoded sizes of those + /// types are fixed through [`codec::MaxEncodedLen`]. The fields exist for backwards + /// compatibility. Consider switching to the newest version of this function. + #[prefixed_alias] + fn call( + ctx: _, + memory: _, + callee_ptr: u32, + _callee_len: u32, + gas: u64, + value_ptr: u32, + _value_len: u32, + input_data_ptr: u32, + input_data_len: u32, + output_ptr: u32, + output_len_ptr: u32, + ) -> Result { + ctx.call( + memory, + CallFlags::ALLOW_REENTRY, + CallType::Call { + callee_ptr, + value_ptr, + deposit_ptr: SENTINEL, + weight: Weight::from_parts(gas, 0), + }, + input_data_ptr, + input_data_len, + output_ptr, + output_len_ptr, + ) + } + + /// Make a call to another contract. + /// See [`pallet_contracts_uapi::HostFn::call_v1`]. + #[version(1)] + #[prefixed_alias] + fn call( + ctx: _, + memory: _, + flags: u32, + callee_ptr: u32, + gas: u64, + value_ptr: u32, + input_data_ptr: u32, + input_data_len: u32, + output_ptr: u32, + output_len_ptr: u32, + ) -> Result { + ctx.call( + memory, + CallFlags::from_bits(flags).ok_or(Error::::InvalidCallFlags)?, + CallType::Call { + callee_ptr, + value_ptr, + deposit_ptr: SENTINEL, + weight: Weight::from_parts(gas, 0), + }, + input_data_ptr, + input_data_len, + output_ptr, + output_len_ptr, + ) + } + + /// Make a call to another contract. + /// See [`pallet_contracts_uapi::HostFn::call_v2`]. + #[version(2)] + fn call( + ctx: _, + memory: _, + flags: u32, + callee_ptr: u32, + ref_time_limit: u64, + proof_size_limit: u64, + deposit_ptr: u32, + value_ptr: u32, + input_data_ptr: u32, + input_data_len: u32, + output_ptr: u32, + output_len_ptr: u32, + ) -> Result { + ctx.call( + memory, + CallFlags::from_bits(flags).ok_or(Error::::InvalidCallFlags)?, + CallType::Call { + callee_ptr, + value_ptr, + deposit_ptr, + weight: Weight::from_parts(ref_time_limit, proof_size_limit), + }, + input_data_ptr, + input_data_len, + output_ptr, + output_len_ptr, + ) + } + + /// Execute code in the context (storage, caller, value) of the current contract. + /// See [`pallet_contracts_uapi::HostFn::delegate_call`]. + #[prefixed_alias] + fn delegate_call( + ctx: _, + memory: _, + flags: u32, + code_hash_ptr: u32, + input_data_ptr: u32, + input_data_len: u32, + output_ptr: u32, + output_len_ptr: u32, + ) -> Result { + ctx.call( + memory, + CallFlags::from_bits(flags).ok_or(Error::::InvalidCallFlags)?, + CallType::DelegateCall { code_hash_ptr }, + input_data_ptr, + input_data_len, + output_ptr, + output_len_ptr, + ) + } + + /// Instantiate a contract with the specified code hash. + /// See [`pallet_contracts_uapi::HostFn::instantiate`]. + /// + /// # Note + /// + /// The values `_code_hash_len` and `_value_len` are ignored because the encoded sizes + /// of those types are fixed through [`codec::MaxEncodedLen`]. The fields exist + /// for backwards compatibility. Consider switching to the newest version of this function. + #[prefixed_alias] + #[mutating] + fn instantiate( + ctx: _, + memory: _, + code_hash_ptr: u32, + _code_hash_len: u32, + gas: u64, + value_ptr: u32, + _value_len: u32, + input_data_ptr: u32, + input_data_len: u32, + address_ptr: u32, + address_len_ptr: u32, + output_ptr: u32, + output_len_ptr: u32, + salt_ptr: u32, + salt_len: u32, + ) -> Result { + ctx.instantiate( + memory, + code_hash_ptr, + Weight::from_parts(gas, 0), + SENTINEL, + value_ptr, + input_data_ptr, + input_data_len, + address_ptr, + address_len_ptr, + output_ptr, + output_len_ptr, + salt_ptr, + salt_len, + ) + } + + /// Instantiate a contract with the specified code hash. + /// See [`pallet_contracts_uapi::HostFn::instantiate_v1`]. + #[version(1)] + #[prefixed_alias] + #[mutating] + fn instantiate( + ctx: _, + memory: _, + code_hash_ptr: u32, + gas: u64, + value_ptr: u32, + input_data_ptr: u32, + input_data_len: u32, + address_ptr: u32, + address_len_ptr: u32, + output_ptr: u32, + output_len_ptr: u32, + salt_ptr: u32, + salt_len: u32, + ) -> Result { + ctx.instantiate( + memory, + code_hash_ptr, + Weight::from_parts(gas, 0), + SENTINEL, + value_ptr, + input_data_ptr, + input_data_len, + address_ptr, + address_len_ptr, + output_ptr, + output_len_ptr, + salt_ptr, + salt_len, + ) + } + + /// Instantiate a contract with the specified code hash. + /// See [`pallet_contracts_uapi::HostFn::instantiate_v2`]. + #[version(2)] + #[mutating] + fn instantiate( + ctx: _, + memory: _, + code_hash_ptr: u32, + ref_time_limit: u64, + proof_size_limit: u64, + deposit_ptr: u32, + value_ptr: u32, + input_data_ptr: u32, + input_data_len: u32, + address_ptr: u32, + address_len_ptr: u32, + output_ptr: u32, + output_len_ptr: u32, + salt_ptr: u32, + salt_len: u32, + ) -> Result { + ctx.instantiate( + memory, + code_hash_ptr, + Weight::from_parts(ref_time_limit, proof_size_limit), + deposit_ptr, + value_ptr, + input_data_ptr, + input_data_len, + address_ptr, + address_len_ptr, + output_ptr, + output_len_ptr, + salt_ptr, + salt_len, + ) + } + + /// Remove the calling account and transfer remaining balance. + /// See [`pallet_contracts_uapi::HostFn::terminate`]. + /// + /// # Note + /// + /// The value `_beneficiary_len` is ignored because the encoded sizes + /// this type is fixed through `[`MaxEncodedLen`]. The field exist for backwards + /// compatibility. Consider switching to the newest version of this function. + #[prefixed_alias] + #[mutating] + fn terminate( + ctx: _, + memory: _, + beneficiary_ptr: u32, + _beneficiary_len: u32, + ) -> Result<(), TrapReason> { + ctx.terminate(memory, beneficiary_ptr) + } + + /// Remove the calling account and transfer remaining **free** balance. + /// See [`pallet_contracts_uapi::HostFn::terminate_v1`]. + #[version(1)] + #[prefixed_alias] + #[mutating] + fn terminate(ctx: _, memory: _, beneficiary_ptr: u32) -> Result<(), TrapReason> { + ctx.terminate(memory, beneficiary_ptr) + } + + /// Stores the input passed by the caller into the supplied buffer. + /// See [`pallet_contracts_uapi::HostFn::input`]. + #[prefixed_alias] + fn input(ctx: _, memory: _, out_ptr: u32, out_len_ptr: u32) -> Result<(), TrapReason> { + if let Some(input) = ctx.input_data.take() { + ctx.write_sandbox_output(memory, out_ptr, out_len_ptr, &input, false, |len| { + Some(RuntimeCosts::CopyToContract(len)) + })?; + ctx.input_data = Some(input); + Ok(()) + } else { + Err(Error::::InputForwarded.into()) + } + } + + /// Cease contract execution and save a data buffer as a result of the execution. + /// See [`pallet_contracts_uapi::HostFn::return_value`]. + fn seal_return( + ctx: _, + memory: _, + flags: u32, + data_ptr: u32, + data_len: u32, + ) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::CopyFromContract(data_len))?; + Err(TrapReason::Return(ReturnData { + flags, + data: ctx.read_sandbox_memory(memory, data_ptr, data_len)?, + })) + } + + /// Stores the address of the caller into the supplied buffer. + /// See [`pallet_contracts_uapi::HostFn::caller`]. + #[prefixed_alias] + fn caller(ctx: _, memory: _, out_ptr: u32, out_len_ptr: u32) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::Caller)?; + let caller = ctx.ext.caller().account_id()?.clone(); + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &caller.encode(), + false, + already_charged, + )?) + } + + /// Checks whether a specified address belongs to a contract. + /// See [`pallet_contracts_uapi::HostFn::is_contract`]. + #[prefixed_alias] + fn is_contract(ctx: _, memory: _, account_ptr: u32) -> Result { + ctx.charge_gas(RuntimeCosts::IsContract)?; + let address: <::T as frame_system::Config>::AccountId = + ctx.read_sandbox_memory_as(memory, account_ptr)?; + + Ok(ctx.ext.is_contract(&address) as u32) + } + + /// Retrieve the code hash for a specified contract address. + /// See [`pallet_contracts_uapi::HostFn::code_hash`]. + #[prefixed_alias] + fn code_hash( + ctx: _, + memory: _, + account_ptr: u32, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result { + ctx.charge_gas(RuntimeCosts::CodeHash)?; + let address: <::T as frame_system::Config>::AccountId = + ctx.read_sandbox_memory_as(memory, account_ptr)?; + if let Some(value) = ctx.ext.code_hash(&address) { + ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &value.encode(), + false, + already_charged, + )?; + Ok(ReturnErrorCode::Success) + } else { + Ok(ReturnErrorCode::KeyNotFound) + } + } + + /// Retrieve the code hash of the currently executing contract. + /// See [`pallet_contracts_uapi::HostFn::own_code_hash`]. + #[prefixed_alias] + fn own_code_hash(ctx: _, memory: _, out_ptr: u32, out_len_ptr: u32) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::OwnCodeHash)?; + let code_hash_encoded = &ctx.ext.own_code_hash().encode(); + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + code_hash_encoded, + false, + already_charged, + )?) + } + + /// Checks whether the caller of the current contract is the origin of the whole call stack. + /// See [`pallet_contracts_uapi::HostFn::caller_is_origin`]. + #[prefixed_alias] + fn caller_is_origin(ctx: _, _memory: _) -> Result { + ctx.charge_gas(RuntimeCosts::CallerIsOrigin)?; + Ok(ctx.ext.caller_is_origin() as u32) + } + + /// Checks whether the caller of the current contract is root. + /// See [`pallet_contracts_uapi::HostFn::caller_is_root`]. + fn caller_is_root(ctx: _, _memory: _) -> Result { + ctx.charge_gas(RuntimeCosts::CallerIsRoot)?; + Ok(ctx.ext.caller_is_root() as u32) + } + + /// Stores the address of the current contract into the supplied buffer. + /// See [`pallet_contracts_uapi::HostFn::address`]. + #[prefixed_alias] + fn address(ctx: _, memory: _, out_ptr: u32, out_len_ptr: u32) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::Address)?; + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &ctx.ext.address().encode(), + false, + already_charged, + )?) + } + + /// Stores the price for the specified amount of gas into the supplied buffer. + /// See [`pallet_contracts_uapi::HostFn::weight_to_fee`]. + #[prefixed_alias] + fn weight_to_fee( + ctx: _, + memory: _, + gas: u64, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result<(), TrapReason> { + let gas = Weight::from_parts(gas, 0); + ctx.charge_gas(RuntimeCosts::WeightToFee)?; + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &ctx.ext.get_weight_price(gas).encode(), + false, + already_charged, + )?) + } + + /// Stores the price for the specified amount of weight into the supplied buffer. + /// See [`pallet_contracts_uapi::HostFn::weight_to_fee_v1`]. + #[version(1)] + #[unstable] + fn weight_to_fee( + ctx: _, + memory: _, + ref_time_limit: u64, + proof_size_limit: u64, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result<(), TrapReason> { + let weight = Weight::from_parts(ref_time_limit, proof_size_limit); + ctx.charge_gas(RuntimeCosts::WeightToFee)?; + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &ctx.ext.get_weight_price(weight).encode(), + false, + already_charged, + )?) + } + + /// Stores the weight left into the supplied buffer. + /// See [`pallet_contracts_uapi::HostFn::gas_left`]. + #[prefixed_alias] + fn gas_left(ctx: _, memory: _, out_ptr: u32, out_len_ptr: u32) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::GasLeft)?; + let gas_left = &ctx.ext.gas_meter().gas_left().ref_time().encode(); + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + gas_left, + false, + already_charged, + )?) + } + + /// Stores the amount of weight left into the supplied buffer. + /// See [`pallet_contracts_uapi::HostFn::gas_left_v1`]. + #[version(1)] + #[unstable] + fn gas_left(ctx: _, memory: _, out_ptr: u32, out_len_ptr: u32) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::GasLeft)?; + let gas_left = &ctx.ext.gas_meter().gas_left().encode(); + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + gas_left, + false, + already_charged, + )?) + } + + /// Stores the *free* balance of the current account into the supplied buffer. + /// See [`pallet_contracts_uapi::HostFn::balance`]. + #[prefixed_alias] + fn balance(ctx: _, memory: _, out_ptr: u32, out_len_ptr: u32) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::Balance)?; + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &ctx.ext.balance().encode(), + false, + already_charged, + )?) + } + + /// Stores the value transferred along with this call/instantiate into the supplied buffer. + /// See [`pallet_contracts_uapi::HostFn::value_transferred`]. + #[prefixed_alias] + fn value_transferred( + ctx: _, + memory: _, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::ValueTransferred)?; + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &ctx.ext.value_transferred().encode(), + false, + already_charged, + )?) + } + + /// Stores a random number for the current block and the given subject into the supplied buffer. + /// + /// The value is stored to linear memory at the address pointed to by `out_ptr`. + /// `out_len_ptr` must point to a u32 value that describes the available space at + /// `out_ptr`. This call overwrites it with the size of the value. If the available + /// space at `out_ptr` is less than the size of the value a trap is triggered. + /// + /// The data is encoded as `T::Hash`. + #[prefixed_alias] + #[deprecated] + fn random( + ctx: _, + memory: _, + subject_ptr: u32, + subject_len: u32, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::Random)?; + if subject_len > ctx.ext.schedule().limits.subject_len { + return Err(Error::::RandomSubjectTooLong.into()) + } + let subject_buf = ctx.read_sandbox_memory(memory, subject_ptr, subject_len)?; + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &ctx.ext.random(&subject_buf).0.encode(), + false, + already_charged, + )?) + } + + /// Stores a random number for the current block and the given subject into the supplied buffer. + /// + /// The value is stored to linear memory at the address pointed to by `out_ptr`. + /// `out_len_ptr` must point to a u32 value that describes the available space at + /// `out_ptr`. This call overwrites it with the size of the value. If the available + /// space at `out_ptr` is less than the size of the value a trap is triggered. + /// + /// The data is encoded as (T::Hash, frame_system::pallet_prelude::BlockNumberFor::). + /// + /// # Changes from v0 + /// + /// In addition to the seed it returns the block number since which it was determinable + /// by chain observers. + /// + /// # Note + /// + /// The returned seed should only be used to distinguish commitments made before + /// the returned block number. If the block number is too early (i.e. commitments were + /// made afterwards), then ensure no further commitments may be made and repeatedly + /// call this on later blocks until the block number returned is later than the latest + /// commitment. + #[version(1)] + #[prefixed_alias] + #[deprecated] + fn random( + ctx: _, + memory: _, + subject_ptr: u32, + subject_len: u32, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::Random)?; + if subject_len > ctx.ext.schedule().limits.subject_len { + return Err(Error::::RandomSubjectTooLong.into()) + } + let subject_buf = ctx.read_sandbox_memory(memory, subject_ptr, subject_len)?; + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &ctx.ext.random(&subject_buf).encode(), + false, + already_charged, + )?) + } + + /// Load the latest block timestamp into the supplied buffer + /// See [`pallet_contracts_uapi::HostFn::now`]. + #[prefixed_alias] + fn now(ctx: _, memory: _, out_ptr: u32, out_len_ptr: u32) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::Now)?; + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &ctx.ext.now().encode(), + false, + already_charged, + )?) + } + + /// Stores the minimum balance (a.k.a. existential deposit) into the supplied buffer. + /// See [`pallet_contracts_uapi::HostFn::minimum_balance`]. + #[prefixed_alias] + fn minimum_balance( + ctx: _, + memory: _, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::MinimumBalance)?; + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &ctx.ext.minimum_balance().encode(), + false, + already_charged, + )?) + } + + /// Stores the tombstone deposit into the supplied buffer. + /// + /// The value is stored to linear memory at the address pointed to by `out_ptr`. + /// `out_len_ptr` must point to a u32 value that describes the available space at + /// `out_ptr`. This call overwrites it with the size of the value. If the available + /// space at `out_ptr` is less than the size of the value a trap is triggered. + /// + /// # Note + /// + /// There is no longer a tombstone deposit. This function always returns `0`. + #[prefixed_alias] + #[deprecated] + fn tombstone_deposit( + ctx: _, + memory: _, + out_ptr: u32, + out_len_ptr: u32, + ) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::Balance)?; + let deposit = >::zero().encode(); + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &deposit, + false, + already_charged, + )?) + } + + /// Was used to restore the given destination contract sacrificing the caller. + /// + /// # Note + /// + /// The state rent functionality was removed. This is stub only exists for + /// backwards compatibility + #[prefixed_alias] + #[deprecated] + fn restore_to( + ctx: _, + memory: _, + _dest_ptr: u32, + _dest_len: u32, + _code_hash_ptr: u32, + _code_hash_len: u32, + _rent_allowance_ptr: u32, + _rent_allowance_len: u32, + _delta_ptr: u32, + _delta_count: u32, + ) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::DebugMessage(0))?; + Ok(()) + } + + /// Was used to restore the given destination contract sacrificing the caller. + /// + /// # Note + /// + /// The state rent functionality was removed. This is stub only exists for + /// backwards compatibility + #[version(1)] + #[prefixed_alias] + #[deprecated] + fn restore_to( + ctx: _, + memory: _, + _dest_ptr: u32, + _code_hash_ptr: u32, + _rent_allowance_ptr: u32, + _delta_ptr: u32, + _delta_count: u32, + ) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::DebugMessage(0))?; + Ok(()) + } + + /// Was used to set rent allowance of the contract. + /// + /// # Note + /// + /// The state rent functionality was removed. This is stub only exists for + /// backwards compatibility. + #[prefixed_alias] + #[deprecated] + fn set_rent_allowance( + ctx: _, + memory: _, + _value_ptr: u32, + _value_len: u32, + ) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::DebugMessage(0))?; + Ok(()) + } + + /// Was used to set rent allowance of the contract. + /// + /// # Note + /// + /// The state rent functionality was removed. This is stub only exists for + /// backwards compatibility. + #[version(1)] + #[prefixed_alias] + #[deprecated] + fn set_rent_allowance(ctx: _, _memory: _, _value_ptr: u32) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::DebugMessage(0))?; + Ok(()) + } + + /// Was used to store the rent allowance into the supplied buffer. + /// + /// # Note + /// + /// The state rent functionality was removed. This is stub only exists for + /// backwards compatibility. + #[prefixed_alias] + #[deprecated] + fn rent_allowance(ctx: _, memory: _, out_ptr: u32, out_len_ptr: u32) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::Balance)?; + let rent_allowance = >::max_value().encode(); + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &rent_allowance, + false, + already_charged, + )?) + } + + /// Deposit a contract event with the data buffer and optional list of topics. + /// See [pallet_contracts_uapi::HostFn::deposit_event] + #[prefixed_alias] + #[mutating] + fn deposit_event( + ctx: _, + memory: _, + topics_ptr: u32, + topics_len: u32, + data_ptr: u32, + data_len: u32, + ) -> Result<(), TrapReason> { + let num_topic = topics_len + .checked_div(core::mem::size_of::>() as u32) + .ok_or("Zero sized topics are not allowed")?; + ctx.charge_gas(RuntimeCosts::DepositEvent { num_topic, len: data_len })?; + if data_len > ctx.ext.max_value_size() { + return Err(Error::::ValueTooLarge.into()) + } + + let topics: Vec::T>> = match topics_len { + 0 => Vec::new(), + _ => ctx.read_sandbox_memory_as_unbounded(memory, topics_ptr, topics_len)?, + }; + + // If there are more than `event_topics`, then trap. + if topics.len() > ctx.ext.schedule().limits.event_topics as usize { + return Err(Error::::TooManyTopics.into()) + } + + let event_data = ctx.read_sandbox_memory(memory, data_ptr, data_len)?; + + ctx.ext.deposit_event(topics, event_data); + + Ok(()) + } + + /// Stores the current block number of the current contract into the supplied buffer. + /// See [`pallet_contracts_uapi::HostFn::block_number`]. + #[prefixed_alias] + fn block_number(ctx: _, memory: _, out_ptr: u32, out_len_ptr: u32) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::BlockNumber)?; + Ok(ctx.write_sandbox_output( + memory, + out_ptr, + out_len_ptr, + &ctx.ext.block_number().encode(), + false, + already_charged, + )?) + } + + /// Computes the SHA2 256-bit hash on the given input buffer. + /// See [`pallet_contracts_uapi::HostFn::hash_sha2_256`]. + #[prefixed_alias] + fn hash_sha2_256( + ctx: _, + memory: _, + input_ptr: u32, + input_len: u32, + output_ptr: u32, + ) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::HashSha256(input_len))?; + Ok(ctx.compute_hash_on_intermediate_buffer( + memory, sha2_256, input_ptr, input_len, output_ptr, + )?) + } + + /// Computes the KECCAK 256-bit hash on the given input buffer. + /// See [`pallet_contracts_uapi::HostFn::hash_keccak_256`]. + #[prefixed_alias] + fn hash_keccak_256( + ctx: _, + memory: _, + input_ptr: u32, + input_len: u32, + output_ptr: u32, + ) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::HashKeccak256(input_len))?; + Ok(ctx.compute_hash_on_intermediate_buffer( + memory, keccak_256, input_ptr, input_len, output_ptr, + )?) + } + + /// Computes the BLAKE2 256-bit hash on the given input buffer. + /// See [`pallet_contracts_uapi::HostFn::hash_blake2_256`]. + #[prefixed_alias] + fn hash_blake2_256( + ctx: _, + memory: _, + input_ptr: u32, + input_len: u32, + output_ptr: u32, + ) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::HashBlake256(input_len))?; + Ok(ctx.compute_hash_on_intermediate_buffer( + memory, blake2_256, input_ptr, input_len, output_ptr, + )?) + } + + /// Computes the BLAKE2 128-bit hash on the given input buffer. + /// See [`pallet_contracts_uapi::HostFn::hash_blake2_128`]. + #[prefixed_alias] + fn hash_blake2_128( + ctx: _, + memory: _, + input_ptr: u32, + input_len: u32, + output_ptr: u32, + ) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::HashBlake128(input_len))?; + Ok(ctx.compute_hash_on_intermediate_buffer( + memory, blake2_128, input_ptr, input_len, output_ptr, + )?) + } + + /// Call into the chain extension provided by the chain if any. + /// See [`pallet_contracts_uapi::HostFn::call_chain_extension`]. + #[prefixed_alias] + fn call_chain_extension( + ctx: _, + memory: _, + id: u32, + input_ptr: u32, + input_len: u32, + output_ptr: u32, + output_len_ptr: u32, + ) -> Result { + use crate::chain_extension::{ChainExtension, Environment, RetVal}; + if !::ChainExtension::enabled() { + return Err(Error::::NoChainExtension.into()) + } + let mut chain_extension = ctx.chain_extension.take().expect( + "Constructor initializes with `Some`. This is the only place where it is set to `None`.\ + It is always reset to `Some` afterwards. qed" + ); + let env = + Environment::new(ctx, memory, id, input_ptr, input_len, output_ptr, output_len_ptr); + let ret = match chain_extension.call(env)? { + RetVal::Converging(val) => Ok(val), + RetVal::Diverging { flags, data } => + Err(TrapReason::Return(ReturnData { flags: flags.bits(), data })), + }; + ctx.chain_extension = Some(chain_extension); + ret + } + + /// Emit a custom debug message. + /// + /// No newlines are added to the supplied message. + /// Specifying invalid UTF-8 just drops the message with no trap. + /// + /// This is a no-op if debug message recording is disabled which is always the case + /// when the code is executing on-chain. The message is interpreted as UTF-8 and + /// appended to the debug buffer which is then supplied to the calling RPC client. + /// + /// # Note + /// + /// Even though no action is taken when debug message recording is disabled there is still + /// a non trivial overhead (and weight cost) associated with calling this function. Contract + /// languages should remove calls to this function (either at runtime or compile time) when + /// not being executed as an RPC. For example, they could allow users to disable logging + /// through compile time flags (cargo features) for on-chain deployment. Additionally, the + /// return value of this function can be cached in order to prevent further calls at runtime. + #[prefixed_alias] + fn debug_message( + ctx: _, + memory: _, + str_ptr: u32, + str_len: u32, + ) -> Result { + let str_len = str_len.min(DebugBufferVec::::bound() as u32); + ctx.charge_gas(RuntimeCosts::DebugMessage(str_len))?; + if ctx.ext.append_debug_buffer("") { + let data = ctx.read_sandbox_memory(memory, str_ptr, str_len)?; + if let Some(msg) = core::str::from_utf8(&data).ok() { + ctx.ext.append_debug_buffer(msg); + } + } + Ok(ReturnErrorCode::Success) + } + + /// Call some dispatchable of the runtime. + /// See [`frame_support::traits::call_runtime`]. + #[mutating] + fn call_runtime( + ctx: _, + memory: _, + call_ptr: u32, + call_len: u32, + ) -> Result { + use frame_support::dispatch::GetDispatchInfo; + ctx.charge_gas(RuntimeCosts::CopyFromContract(call_len))?; + let call: ::RuntimeCall = + ctx.read_sandbox_memory_as_unbounded(memory, call_ptr, call_len)?; + ctx.call_dispatchable::( + call.get_dispatch_info(), + RuntimeCosts::CallRuntime, + |ctx| ctx.ext.call_runtime(call), + ) + } + + /// Execute an XCM program locally, using the contract's address as the origin. + /// See [`pallet_contracts_uapi::HostFn::execute_xcm`]. + #[mutating] + fn xcm_execute( + ctx: _, + memory: _, + msg_ptr: u32, + msg_len: u32, + ) -> Result { + use frame_support::dispatch::DispatchInfo; + use xcm::VersionedXcm; + use xcm_builder::{ExecuteController, ExecuteControllerWeightInfo}; + + ctx.charge_gas(RuntimeCosts::CopyFromContract(msg_len))?; + let message: VersionedXcm> = + ctx.read_sandbox_memory_as_unbounded(memory, msg_ptr, msg_len)?; + + let execute_weight = + <::Xcm as ExecuteController<_, _>>::WeightInfo::execute(); + let weight = ctx.ext.gas_meter().gas_left().max(execute_weight); + let dispatch_info = DispatchInfo { weight, ..Default::default() }; + + ctx.call_dispatchable::( + dispatch_info, + RuntimeCosts::CallXcmExecute, + |ctx| { + let origin = crate::RawOrigin::Signed(ctx.ext.address().clone()).into(); + let weight_used = <::Xcm>::execute( + origin, + Box::new(message), + weight.saturating_sub(execute_weight), + )?; + + Ok(Some(weight_used.saturating_add(execute_weight)).into()) + }, + ) + } + + /// Send an XCM program from the contract to the specified destination. + /// See [`pallet_contracts_uapi::HostFn::send_xcm`]. + #[mutating] + fn xcm_send( + ctx: _, + memory: _, + dest_ptr: u32, + msg_ptr: u32, + msg_len: u32, + output_ptr: u32, + ) -> Result { + use xcm::{VersionedLocation, VersionedXcm}; + use xcm_builder::{SendController, SendControllerWeightInfo}; + + ctx.charge_gas(RuntimeCosts::CopyFromContract(msg_len))?; + let dest: VersionedLocation = ctx.read_sandbox_memory_as(memory, dest_ptr)?; + + let message: VersionedXcm<()> = + ctx.read_sandbox_memory_as_unbounded(memory, msg_ptr, msg_len)?; + let weight = <::Xcm as SendController<_>>::WeightInfo::send(); + ctx.charge_gas(RuntimeCosts::CallRuntime(weight))?; + let origin = crate::RawOrigin::Signed(ctx.ext.address().clone()).into(); + + match <::Xcm>::send(origin, dest.into(), message.into()) { + Ok(message_id) => { + ctx.write_sandbox_memory(memory, output_ptr, &message_id.encode())?; + Ok(ReturnErrorCode::Success) + }, + Err(e) => { + if ctx.ext.append_debug_buffer("") { + ctx.ext.append_debug_buffer("seal0::xcm_send failed with: "); + ctx.ext.append_debug_buffer(e.into()); + }; + Ok(ReturnErrorCode::XcmSendFailed) + }, + } + } + + /// Recovers the ECDSA public key from the given message hash and signature. + /// See [`pallet_contracts_uapi::HostFn::ecdsa_recover`]. + #[prefixed_alias] + fn ecdsa_recover( + ctx: _, + memory: _, + signature_ptr: u32, + message_hash_ptr: u32, + output_ptr: u32, + ) -> Result { + ctx.charge_gas(RuntimeCosts::EcdsaRecovery)?; + + let mut signature: [u8; 65] = [0; 65]; + ctx.read_sandbox_memory_into_buf(memory, signature_ptr, &mut signature)?; + let mut message_hash: [u8; 32] = [0; 32]; + ctx.read_sandbox_memory_into_buf(memory, message_hash_ptr, &mut message_hash)?; + + let result = ctx.ext.ecdsa_recover(&signature, &message_hash); + + match result { + Ok(pub_key) => { + // Write the recovered compressed ecdsa public key back into the sandboxed output + // buffer. + ctx.write_sandbox_memory(memory, output_ptr, pub_key.as_ref())?; + + Ok(ReturnErrorCode::Success) + }, + Err(_) => Ok(ReturnErrorCode::EcdsaRecoveryFailed), + } + } + + /// Verify a sr25519 signature + /// See [`pallet_contracts_uapi::HostFn::sr25519_verify`]. + fn sr25519_verify( + ctx: _, + memory: _, + signature_ptr: u32, + pub_key_ptr: u32, + message_len: u32, + message_ptr: u32, + ) -> Result { + ctx.charge_gas(RuntimeCosts::Sr25519Verify(message_len))?; + + let mut signature: [u8; 64] = [0; 64]; + ctx.read_sandbox_memory_into_buf(memory, signature_ptr, &mut signature)?; + + let mut pub_key: [u8; 32] = [0; 32]; + ctx.read_sandbox_memory_into_buf(memory, pub_key_ptr, &mut pub_key)?; + + let message: Vec = ctx.read_sandbox_memory(memory, message_ptr, message_len)?; + + if ctx.ext.sr25519_verify(&signature, &message, &pub_key) { + Ok(ReturnErrorCode::Success) + } else { + Ok(ReturnErrorCode::Sr25519VerifyFailed) + } + } + + /// Replace the contract code at the specified address with new code. + /// See [`pallet_contracts_uapi::HostFn::set_code_hash`]. + #[prefixed_alias] + #[mutating] + fn set_code_hash(ctx: _, memory: _, code_hash_ptr: u32) -> Result { + ctx.charge_gas(RuntimeCosts::SetCodeHash)?; + let code_hash: CodeHash<::T> = + ctx.read_sandbox_memory_as(memory, code_hash_ptr)?; + match ctx.ext.set_code_hash(code_hash) { + Err(err) => { + let code = Runtime::::err_into_return_code(err)?; + Ok(code) + }, + Ok(()) => Ok(ReturnErrorCode::Success), + } + } + + /// Calculates Ethereum address from the ECDSA compressed public key and stores + /// See [`pallet_contracts_uapi::HostFn::ecdsa_to_eth_address`]. + #[prefixed_alias] + fn ecdsa_to_eth_address( + ctx: _, + memory: _, + key_ptr: u32, + out_ptr: u32, + ) -> Result { + ctx.charge_gas(RuntimeCosts::EcdsaToEthAddress)?; + let mut compressed_key: [u8; 33] = [0; 33]; + ctx.read_sandbox_memory_into_buf(memory, key_ptr, &mut compressed_key)?; + let result = ctx.ext.ecdsa_to_eth_address(&compressed_key); + match result { + Ok(eth_address) => { + ctx.write_sandbox_memory(memory, out_ptr, eth_address.as_ref())?; + Ok(ReturnErrorCode::Success) + }, + Err(_) => Ok(ReturnErrorCode::EcdsaRecoveryFailed), + } + } + + /// Returns the number of times the currently executing contract exists on the call stack in + /// addition to the calling instance. + /// See [`pallet_contracts_uapi::HostFn::reentrance_count`]. + #[unstable] + fn reentrance_count(ctx: _, memory: _) -> Result { + ctx.charge_gas(RuntimeCosts::ReentranceCount)?; + Ok(ctx.ext.reentrance_count()) + } + + /// Returns the number of times specified contract exists on the call stack. Delegated calls are + /// not counted as separate calls. + /// See [`pallet_contracts_uapi::HostFn::account_reentrance_count`]. + #[unstable] + fn account_reentrance_count(ctx: _, memory: _, account_ptr: u32) -> Result { + ctx.charge_gas(RuntimeCosts::AccountReentranceCount)?; + let account_id: <::T as frame_system::Config>::AccountId = + ctx.read_sandbox_memory_as(memory, account_ptr)?; + Ok(ctx.ext.account_reentrance_count(&account_id)) + } + + /// Returns a nonce that is unique per contract instantiation. + /// See [`pallet_contracts_uapi::HostFn::instantiation_nonce`]. + fn instantiation_nonce(ctx: _, _memory: _) -> Result { + ctx.charge_gas(RuntimeCosts::InstantiationNonce)?; + Ok(ctx.ext.nonce()) + } + + /// Adds a new delegate dependency to the contract. + /// See [`pallet_contracts_uapi::HostFn::lock_delegate_dependency`]. + #[mutating] + fn lock_delegate_dependency(ctx: _, memory: _, code_hash_ptr: u32) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::LockDelegateDependency)?; + let code_hash = ctx.read_sandbox_memory_as(memory, code_hash_ptr)?; + ctx.ext.lock_delegate_dependency(code_hash)?; + Ok(()) + } + + /// Removes the delegate dependency from the contract. + /// see [`pallet_contracts_uapi::HostFn::unlock_delegate_dependency`]. + #[mutating] + fn unlock_delegate_dependency(ctx: _, memory: _, code_hash_ptr: u32) -> Result<(), TrapReason> { + ctx.charge_gas(RuntimeCosts::UnlockDelegateDependency)?; + let code_hash = ctx.read_sandbox_memory_as(memory, code_hash_ptr)?; + ctx.ext.unlock_delegate_dependency(&code_hash)?; + Ok(()) + } +} diff --git a/pallets/contracts/src/weights.rs b/pallets/contracts/src/weights.rs new file mode 100644 index 00000000..25b36fc4 --- /dev/null +++ b/pallets/contracts/src/weights.rs @@ -0,0 +1,2120 @@ +// This file is part of Substrate. + +// 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 `pallet_contracts` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 32.0.0 +//! DATE: 2024-07-17, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `runner-yaoqqom-project-674-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz` +//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: `1024` + +// Executed Command: +// target/production/substrate-node +// 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=pallet_contracts +// --chain=dev +// --header=./substrate/HEADER-APACHE2 +// --output=./substrate/frame/contracts/src/weights.rs +// --template=./substrate/.maintain/frame-weight-template.hbs + +#![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 `pallet_contracts`. +pub trait WeightInfo { + fn on_process_deletion_queue_batch() -> Weight; + fn on_initialize_per_trie_key(k: u32, ) -> Weight; + fn v9_migration_step(c: u32, ) -> Weight; + fn v10_migration_step() -> Weight; + fn v11_migration_step(k: u32, ) -> Weight; + fn v12_migration_step(c: u32, ) -> Weight; + fn v13_migration_step() -> Weight; + fn v14_migration_step() -> Weight; + fn v15_migration_step() -> Weight; + fn v16_migration_step() -> Weight; + fn migration_noop() -> Weight; + fn migrate() -> Weight; + fn on_runtime_upgrade_noop() -> Weight; + fn on_runtime_upgrade_in_progress() -> Weight; + fn on_runtime_upgrade() -> Weight; + fn call_with_code_per_byte(c: u32, ) -> Weight; + fn instantiate_with_code(c: u32, i: u32, s: u32, ) -> Weight; + fn instantiate(i: u32, s: u32, ) -> Weight; + fn call() -> Weight; + fn upload_code_determinism_enforced(c: u32, ) -> Weight; + fn upload_code_determinism_relaxed(c: u32, ) -> Weight; + fn remove_code() -> Weight; + fn set_code() -> Weight; + fn noop_host_fn(r: u32, ) -> Weight; + fn seal_caller() -> Weight; + fn seal_is_contract() -> Weight; + fn seal_code_hash() -> Weight; + fn seal_own_code_hash() -> Weight; + fn seal_caller_is_origin() -> Weight; + fn seal_caller_is_root() -> Weight; + fn seal_address() -> Weight; + fn seal_gas_left() -> Weight; + fn seal_balance() -> Weight; + fn seal_value_transferred() -> Weight; + fn seal_minimum_balance() -> Weight; + fn seal_block_number() -> Weight; + fn seal_now() -> Weight; + fn seal_weight_to_fee() -> Weight; + fn seal_input(n: u32, ) -> Weight; + fn seal_return(n: u32, ) -> Weight; + fn seal_terminate(n: u32, ) -> Weight; + fn seal_random() -> Weight; + fn seal_deposit_event(t: u32, n: u32, ) -> Weight; + fn seal_debug_message(i: u32, ) -> Weight; + fn get_storage_empty() -> Weight; + fn get_storage_full() -> Weight; + fn set_storage_empty() -> Weight; + fn set_storage_full() -> Weight; + fn seal_set_storage(n: u32, o: u32, ) -> Weight; + fn seal_clear_storage(n: u32, ) -> Weight; + fn seal_get_storage(n: u32, ) -> Weight; + fn seal_contains_storage(n: u32, ) -> Weight; + fn seal_take_storage(n: u32, ) -> Weight; + fn set_transient_storage_empty() -> Weight; + fn set_transient_storage_full() -> Weight; + fn get_transient_storage_empty() -> Weight; + fn get_transient_storage_full() -> Weight; + fn rollback_transient_storage() -> Weight; + fn seal_set_transient_storage(n: u32, o: u32, ) -> Weight; + fn seal_clear_transient_storage(n: u32, ) -> Weight; + fn seal_get_transient_storage(n: u32, ) -> Weight; + fn seal_contains_transient_storage(n: u32, ) -> Weight; + fn seal_take_transient_storage(n: u32, ) -> Weight; + fn seal_transfer() -> Weight; + fn seal_call(t: u32, i: u32, ) -> Weight; + fn seal_delegate_call() -> Weight; + fn seal_instantiate(i: u32, s: u32, ) -> Weight; + fn seal_hash_sha2_256(n: u32, ) -> Weight; + fn seal_hash_keccak_256(n: u32, ) -> Weight; + fn seal_hash_blake2_256(n: u32, ) -> Weight; + fn seal_hash_blake2_128(n: u32, ) -> Weight; + fn seal_sr25519_verify(n: u32, ) -> Weight; + fn seal_ecdsa_recover() -> Weight; + fn seal_ecdsa_to_eth_address() -> Weight; + fn seal_set_code_hash() -> Weight; + fn lock_delegate_dependency() -> Weight; + fn unlock_delegate_dependency() -> Weight; + fn seal_reentrance_count() -> Weight; + fn seal_account_reentrance_count() -> Weight; + fn seal_instantiation_nonce() -> Weight; + fn instr_i64_load_store(r: u32, ) -> Weight; +} + +/// Weights for `pallet_contracts` using the Substrate node and recommended hardware. +pub struct SubstrateWeight(PhantomData); +impl WeightInfo for SubstrateWeight { + /// Storage: `Contracts::DeletionQueueCounter` (r:1 w:0) + /// Proof: `Contracts::DeletionQueueCounter` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + fn on_process_deletion_queue_batch() -> Weight { + // Proof Size summary in bytes: + // Measured: `142` + // Estimated: `1627` + // Minimum execution time: 1_915_000 picoseconds. + Weight::from_parts(1_986_000, 1627) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `k` is `[0, 1024]`. + fn on_initialize_per_trie_key(k: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `452 + k * (69 ±0)` + // Estimated: `442 + k * (70 ±0)` + // Minimum execution time: 11_103_000 picoseconds. + Weight::from_parts(11_326_000, 442) + // Standard Error: 2_291 + .saturating_add(Weight::from_parts(1_196_329, 0).saturating_mul(k.into())) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(k.into()))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(k.into()))) + .saturating_add(Weight::from_parts(0, 70).saturating_mul(k.into())) + } + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc553022fca90611ba8b7942f8bdb3b97f6580` (r:2 w:1) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc553022fca90611ba8b7942f8bdb3b97f6580` (r:2 w:1) + /// The range of component `c` is `[0, 125952]`. + fn v9_migration_step(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `211 + c * (1 ±0)` + // Estimated: `6149 + c * (1 ±0)` + // Minimum execution time: 7_783_000 picoseconds. + Weight::from_parts(4_462_075, 6149) + // Standard Error: 5 + .saturating_add(Weight::from_parts(1_634, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(c.into())) + } + /// Storage: `Contracts::ContractInfoOf` (r:2 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:0) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + fn v10_migration_step() -> Weight { + // Proof Size summary in bytes: + // Measured: `510` + // Estimated: `6450` + // Minimum execution time: 15_971_000 picoseconds. + Weight::from_parts(16_730_000, 6450) + .saturating_add(T::DbWeight::get().reads(3_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::DeletionQueue` (r:1 w:1025) + /// Proof: `Contracts::DeletionQueue` (`max_values`: None, `max_size`: Some(142), added: 2617, mode: `Measured`) + /// Storage: `Contracts::DeletionQueueCounter` (r:0 w:1) + /// Proof: `Contracts::DeletionQueueCounter` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// The range of component `k` is `[0, 1024]`. + fn v11_migration_step(k: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `171 + k * (1 ±0)` + // Estimated: `3635 + k * (1 ±0)` + // Minimum execution time: 3_149_000 picoseconds. + Weight::from_parts(3_264_000, 3635) + // Standard Error: 559 + .saturating_add(Weight::from_parts(1_111_209, 0).saturating_mul(k.into())) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(k.into()))) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(k.into())) + } + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc553053f13fd319a03c211337c76e0fe776df` (r:2 w:0) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc553053f13fd319a03c211337c76e0fe776df` (r:2 w:0) + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc553022fca90611ba8b7942f8bdb3b97f6580` (r:1 w:1) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc553022fca90611ba8b7942f8bdb3b97f6580` (r:1 w:1) + /// Storage: `System::Account` (r:1 w:0) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:0 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// The range of component `c` is `[0, 125952]`. + fn v12_migration_step(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `325 + c * (1 ±0)` + // Estimated: `6263 + c * (1 ±0)` + // Minimum execution time: 15_072_000 picoseconds. + Weight::from_parts(15_721_891, 6263) + // Standard Error: 2 + .saturating_add(Weight::from_parts(428, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(4_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(c.into())) + } + /// Storage: `Contracts::ContractInfoOf` (r:2 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + fn v13_migration_step() -> Weight { + // Proof Size summary in bytes: + // Measured: `440` + // Estimated: `6380` + // Minimum execution time: 12_047_000 picoseconds. + Weight::from_parts(12_500_000, 6380) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::CodeInfoOf` (r:2 w:0) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// Storage: `Balances::Holds` (r:1 w:0) + /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) + fn v14_migration_step() -> Weight { + // Proof Size summary in bytes: + // Measured: `352` + // Estimated: `6292` + // Minimum execution time: 47_488_000 picoseconds. + Weight::from_parts(48_482_000, 6292) + .saturating_add(T::DbWeight::get().reads(4_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::ContractInfoOf` (r:2 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `System::Account` (r:2 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + fn v15_migration_step() -> Weight { + // Proof Size summary in bytes: + // Measured: `594` + // Estimated: `6534` + // Minimum execution time: 52_801_000 picoseconds. + Weight::from_parts(54_230_000, 6534) + .saturating_add(T::DbWeight::get().reads(4_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) + } + /// Storage: `Contracts::ContractInfoOf` (r:2 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + fn v16_migration_step() -> Weight { + // Proof Size summary in bytes: + // Measured: `409` + // Estimated: `6349` + // Minimum execution time: 11_618_000 picoseconds. + Weight::from_parts(12_068_000, 6349) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:1) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + fn migration_noop() -> Weight { + // Proof Size summary in bytes: + // Measured: `142` + // Estimated: `1627` + // Minimum execution time: 2_131_000 picoseconds. + Weight::from_parts(2_255_000, 1627) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:1) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:1) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:1) + fn migrate() -> Weight { + // Proof Size summary in bytes: + // Measured: `166` + // Estimated: `3631` + // Minimum execution time: 10_773_000 picoseconds. + Weight::from_parts(11_118_000, 3631) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) + } + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:0) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:0) + fn on_runtime_upgrade_noop() -> Weight { + // Proof Size summary in bytes: + // Measured: `142` + // Estimated: `3607` + // Minimum execution time: 4_371_000 picoseconds. + Weight::from_parts(4_624_000, 3607) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:0) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:0) + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + fn on_runtime_upgrade_in_progress() -> Weight { + // Proof Size summary in bytes: + // Measured: `167` + // Estimated: `3632` + // Minimum execution time: 5_612_000 picoseconds. + Weight::from_parts(5_838_000, 3632) + .saturating_add(T::DbWeight::get().reads(2_u64)) + } + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:0) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:0) + /// Storage: `Contracts::MigrationInProgress` (r:1 w:1) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + fn on_runtime_upgrade() -> Weight { + // Proof Size summary in bytes: + // Measured: `142` + // Estimated: `3607` + // Minimum execution time: 5_487_000 picoseconds. + Weight::from_parts(5_693_000, 3607) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:0) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// Storage: `Timestamp::Now` (r:1 w:0) + /// Proof: `Timestamp::Now` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// The range of component `c` is `[0, 125952]`. + fn call_with_code_per_byte(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `800 + c * (1 ±0)` + // Estimated: `4266 + c * (1 ±0)` + // Minimum execution time: 247_545_000 picoseconds. + Weight::from_parts(268_016_699, 4266) + // Standard Error: 4 + .saturating_add(Weight::from_parts(700, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(6_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(c.into())) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Balances::Holds` (r:2 w:2) + /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) + /// Storage: `Contracts::Nonce` (r:1 w:1) + /// Proof: `Contracts::Nonce` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `Timestamp::Now` (r:1 w:0) + /// Proof: `Timestamp::Now` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:0 w:1) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// The range of component `c` is `[0, 125952]`. + /// The range of component `i` is `[0, 1048576]`. + /// The range of component `s` is `[0, 1048576]`. + fn instantiate_with_code(c: u32, i: u32, s: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `323` + // Estimated: `6262` + // Minimum execution time: 4_396_772_000 picoseconds. + Weight::from_parts(235_107_907, 6262) + // Standard Error: 185 + .saturating_add(Weight::from_parts(53_843, 0).saturating_mul(c.into())) + // Standard Error: 22 + .saturating_add(Weight::from_parts(2_143, 0).saturating_mul(i.into())) + // Standard Error: 22 + .saturating_add(Weight::from_parts(2_210, 0).saturating_mul(s.into())) + .saturating_add(T::DbWeight::get().reads(8_u64)) + .saturating_add(T::DbWeight::get().writes(7_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// Storage: `Contracts::Nonce` (r:1 w:1) + /// Proof: `Contracts::Nonce` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `Timestamp::Now` (r:1 w:0) + /// Proof: `Timestamp::Now` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// Storage: `Balances::Holds` (r:1 w:1) + /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) + /// The range of component `i` is `[0, 1048576]`. + /// The range of component `s` is `[0, 1048576]`. + fn instantiate(i: u32, s: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `560` + // Estimated: `4017` + // Minimum execution time: 2_240_868_000 picoseconds. + Weight::from_parts(2_273_668_000, 4017) + // Standard Error: 32 + .saturating_add(Weight::from_parts(934, 0).saturating_mul(i.into())) + // Standard Error: 32 + .saturating_add(Weight::from_parts(920, 0).saturating_mul(s.into())) + .saturating_add(T::DbWeight::get().reads(8_u64)) + .saturating_add(T::DbWeight::get().writes(5_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:0) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// Storage: `Timestamp::Now` (r:1 w:0) + /// Proof: `Timestamp::Now` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + fn call() -> Weight { + // Proof Size summary in bytes: + // Measured: `826` + // Estimated: `4291` + // Minimum execution time: 165_067_000 picoseconds. + Weight::from_parts(168_582_000, 4291) + .saturating_add(T::DbWeight::get().reads(6_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Balances::Holds` (r:1 w:1) + /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:0 w:1) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// The range of component `c` is `[0, 125952]`. + fn upload_code_determinism_enforced(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `142` + // Estimated: `3607` + // Minimum execution time: 229_454_000 picoseconds. + Weight::from_parts(251_495_551, 3607) + // Standard Error: 71 + .saturating_add(Weight::from_parts(51_428, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(3_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Balances::Holds` (r:1 w:1) + /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:0 w:1) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// The range of component `c` is `[0, 125952]`. + fn upload_code_determinism_relaxed(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `142` + // Estimated: `3607` + // Minimum execution time: 240_390_000 picoseconds. + Weight::from_parts(273_854_266, 3607) + // Standard Error: 243 + .saturating_add(Weight::from_parts(51_836, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(3_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Balances::Holds` (r:1 w:1) + /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:0 w:1) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + fn remove_code() -> Weight { + // Proof Size summary in bytes: + // Measured: `315` + // Estimated: `3780` + // Minimum execution time: 39_374_000 picoseconds. + Weight::from_parts(40_247_000, 3780) + .saturating_add(T::DbWeight::get().reads(3_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:2 w:2) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + fn set_code() -> Weight { + // Proof Size summary in bytes: + // Measured: `552` + // Estimated: `6492` + // Minimum execution time: 24_473_000 picoseconds. + Weight::from_parts(25_890_000, 6492) + .saturating_add(T::DbWeight::get().reads(4_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + } + /// The range of component `r` is `[0, 1600]`. + fn noop_host_fn(r: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 8_528_000 picoseconds. + Weight::from_parts(9_301_010, 0) + // Standard Error: 98 + .saturating_add(Weight::from_parts(53_173, 0).saturating_mul(r.into())) + } + fn seal_caller() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 643_000 picoseconds. + Weight::from_parts(678_000, 0) + } + /// Storage: `Contracts::ContractInfoOf` (r:1 w:0) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + fn seal_is_contract() -> Weight { + // Proof Size summary in bytes: + // Measured: `354` + // Estimated: `3819` + // Minimum execution time: 6_107_000 picoseconds. + Weight::from_parts(6_235_000, 3819) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } + /// Storage: `Contracts::ContractInfoOf` (r:1 w:0) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + fn seal_code_hash() -> Weight { + // Proof Size summary in bytes: + // Measured: `447` + // Estimated: `3912` + // Minimum execution time: 7_316_000 picoseconds. + Weight::from_parts(7_653_000, 3912) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } + fn seal_own_code_hash() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 721_000 picoseconds. + Weight::from_parts(764_000, 0) + } + fn seal_caller_is_origin() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 369_000 picoseconds. + Weight::from_parts(417_000, 0) + } + fn seal_caller_is_root() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 318_000 picoseconds. + Weight::from_parts(349_000, 0) + } + fn seal_address() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 590_000 picoseconds. + Weight::from_parts(628_000, 0) + } + fn seal_gas_left() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 660_000 picoseconds. + Weight::from_parts(730_000, 0) + } + fn seal_balance() -> Weight { + // Proof Size summary in bytes: + // Measured: `140` + // Estimated: `0` + // Minimum execution time: 4_361_000 picoseconds. + Weight::from_parts(4_577_000, 0) + } + fn seal_value_transferred() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 560_000 picoseconds. + Weight::from_parts(603_000, 0) + } + fn seal_minimum_balance() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 561_000 picoseconds. + Weight::from_parts(610_000, 0) + } + fn seal_block_number() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 557_000 picoseconds. + Weight::from_parts(583_000, 0) + } + fn seal_now() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 550_000 picoseconds. + Weight::from_parts(602_000, 0) + } + /// Storage: `TransactionPayment::NextFeeMultiplier` (r:1 w:0) + /// Proof: `TransactionPayment::NextFeeMultiplier` (`max_values`: Some(1), `max_size`: Some(16), added: 511, mode: `Measured`) + fn seal_weight_to_fee() -> Weight { + // Proof Size summary in bytes: + // Measured: `67` + // Estimated: `1552` + // Minimum execution time: 4_065_000 picoseconds. + Weight::from_parts(4_291_000, 1552) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } + /// The range of component `n` is `[0, 1048572]`. + fn seal_input(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 487_000 picoseconds. + Weight::from_parts(517_000, 0) + // Standard Error: 3 + .saturating_add(Weight::from_parts(301, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 1048572]`. + fn seal_return(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 318_000 picoseconds. + Weight::from_parts(372_000, 0) + // Standard Error: 10 + .saturating_add(Weight::from_parts(411, 0).saturating_mul(n.into())) + } + /// Storage: `Contracts::DeletionQueueCounter` (r:1 w:1) + /// Proof: `Contracts::DeletionQueueCounter` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:33 w:33) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::DeletionQueue` (r:0 w:1) + /// Proof: `Contracts::DeletionQueue` (`max_values`: None, `max_size`: Some(142), added: 2617, mode: `Measured`) + /// The range of component `n` is `[0, 32]`. + fn seal_terminate(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `319 + n * (78 ±0)` + // Estimated: `3784 + n * (2553 ±0)` + // Minimum execution time: 13_251_000 picoseconds. + Weight::from_parts(15_257_892, 3784) + // Standard Error: 7_089 + .saturating_add(Weight::from_parts(3_443_907, 0).saturating_mul(n.into())) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(n.into()))) + .saturating_add(T::DbWeight::get().writes(3_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(n.into()))) + .saturating_add(Weight::from_parts(0, 2553).saturating_mul(n.into())) + } + /// Storage: `RandomnessCollectiveFlip::RandomMaterial` (r:1 w:0) + /// Proof: `RandomnessCollectiveFlip::RandomMaterial` (`max_values`: Some(1), `max_size`: Some(2594), added: 3089, mode: `Measured`) + fn seal_random() -> Weight { + // Proof Size summary in bytes: + // Measured: `76` + // Estimated: `1561` + // Minimum execution time: 3_434_000 picoseconds. + Weight::from_parts(3_605_000, 1561) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } + /// Storage: `System::EventTopics` (r:4 w:4) + /// Proof: `System::EventTopics` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `t` is `[0, 4]`. + /// The range of component `n` is `[0, 16384]`. + fn seal_deposit_event(t: u32, n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `990 + t * (2475 ±0)` + // Minimum execution time: 3_668_000 picoseconds. + Weight::from_parts(3_999_591, 990) + // Standard Error: 5_767 + .saturating_add(Weight::from_parts(2_011_090, 0).saturating_mul(t.into())) + // Standard Error: 1 + .saturating_add(Weight::from_parts(12, 0).saturating_mul(n.into())) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(t.into()))) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(t.into()))) + .saturating_add(Weight::from_parts(0, 2475).saturating_mul(t.into())) + } + /// The range of component `i` is `[0, 1048576]`. + fn seal_debug_message(i: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 443_000 picoseconds. + Weight::from_parts(472_000, 0) + // Standard Error: 10 + .saturating_add(Weight::from_parts(1_207, 0).saturating_mul(i.into())) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn get_storage_empty() -> Weight { + // Proof Size summary in bytes: + // Measured: `16618` + // Estimated: `16618` + // Minimum execution time: 13_752_000 picoseconds. + Weight::from_parts(14_356_000, 16618) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn get_storage_full() -> Weight { + // Proof Size summary in bytes: + // Measured: `26628` + // Estimated: `26628` + // Minimum execution time: 43_444_000 picoseconds. + Weight::from_parts(45_087_000, 26628) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn set_storage_empty() -> Weight { + // Proof Size summary in bytes: + // Measured: `16618` + // Estimated: `16618` + // Minimum execution time: 15_616_000 picoseconds. + Weight::from_parts(16_010_000, 16618) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn set_storage_full() -> Weight { + // Proof Size summary in bytes: + // Measured: `26628` + // Estimated: `26628` + // Minimum execution time: 47_020_000 picoseconds. + Weight::from_parts(50_152_000, 26628) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `n` is `[0, 16384]`. + /// The range of component `o` is `[0, 16384]`. + fn seal_set_storage(n: u32, o: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `250 + o * (1 ±0)` + // Estimated: `249 + o * (1 ±0)` + // Minimum execution time: 8_824_000 picoseconds. + Weight::from_parts(8_915_233, 249) + // Standard Error: 1 + .saturating_add(Weight::from_parts(255, 0).saturating_mul(n.into())) + // Standard Error: 1 + .saturating_add(Weight::from_parts(39, 0).saturating_mul(o.into())) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(o.into())) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `n` is `[0, 16384]`. + fn seal_clear_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `248 + n * (1 ±0)` + // Estimated: `248 + n * (1 ±0)` + // Minimum execution time: 7_133_000 picoseconds. + Weight::from_parts(7_912_778, 248) + // Standard Error: 1 + .saturating_add(Weight::from_parts(88, 0).saturating_mul(n.into())) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(n.into())) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `n` is `[0, 16384]`. + fn seal_get_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `248 + n * (1 ±0)` + // Estimated: `248 + n * (1 ±0)` + // Minimum execution time: 6_746_000 picoseconds. + Weight::from_parts(7_647_236, 248) + // Standard Error: 2 + .saturating_add(Weight::from_parts(603, 0).saturating_mul(n.into())) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(n.into())) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `n` is `[0, 16384]`. + fn seal_contains_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `248 + n * (1 ±0)` + // Estimated: `248 + n * (1 ±0)` + // Minimum execution time: 6_247_000 picoseconds. + Weight::from_parts(6_952_661, 248) + // Standard Error: 1 + .saturating_add(Weight::from_parts(77, 0).saturating_mul(n.into())) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(n.into())) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `n` is `[0, 16384]`. + fn seal_take_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `248 + n * (1 ±0)` + // Estimated: `248 + n * (1 ±0)` + // Minimum execution time: 7_428_000 picoseconds. + Weight::from_parts(8_384_015, 248) + // Standard Error: 2 + .saturating_add(Weight::from_parts(625, 0).saturating_mul(n.into())) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(n.into())) + } + fn set_transient_storage_empty() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_478_000 picoseconds. + Weight::from_parts(1_533_000, 0) + } + fn set_transient_storage_full() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 2_485_000 picoseconds. + Weight::from_parts(2_728_000, 0) + } + fn get_transient_storage_empty() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 3_195_000 picoseconds. + Weight::from_parts(3_811_000, 0) + } + fn get_transient_storage_full() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 3_902_000 picoseconds. + Weight::from_parts(4_118_000, 0) + } + fn rollback_transient_storage() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_571_000 picoseconds. + Weight::from_parts(1_662_000, 0) + } + /// The range of component `n` is `[0, 16384]`. + /// The range of component `o` is `[0, 16384]`. + fn seal_set_transient_storage(n: u32, o: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 5_250_000 picoseconds. + Weight::from_parts(2_465_568, 0) + // Standard Error: 0 + .saturating_add(Weight::from_parts(201, 0).saturating_mul(n.into())) + // Standard Error: 0 + .saturating_add(Weight::from_parts(223, 0).saturating_mul(o.into())) + } + /// The range of component `n` is `[0, 16384]`. + fn seal_clear_transient_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 2_012_000 picoseconds. + Weight::from_parts(2_288_004, 0) + // Standard Error: 3 + .saturating_add(Weight::from_parts(239, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 16384]`. + fn seal_get_transient_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_906_000 picoseconds. + Weight::from_parts(2_121_040, 0) + // Standard Error: 0 + .saturating_add(Weight::from_parts(225, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 16384]`. + fn seal_contains_transient_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_736_000 picoseconds. + Weight::from_parts(1_954_728, 0) + // Standard Error: 0 + .saturating_add(Weight::from_parts(111, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 16384]`. + fn seal_take_transient_storage(_n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 7_872_000 picoseconds. + Weight::from_parts(8_125_644, 0) + } + fn seal_transfer() -> Weight { + // Proof Size summary in bytes: + // Measured: `140` + // Estimated: `0` + // Minimum execution time: 8_489_000 picoseconds. + Weight::from_parts(8_791_000, 0) + } + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:0) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// The range of component `t` is `[0, 1]`. + /// The range of component `i` is `[0, 1048576]`. + fn seal_call(t: u32, i: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `620 + t * (280 ±0)` + // Estimated: `4085 + t * (2182 ±0)` + // Minimum execution time: 122_759_000 picoseconds. + Weight::from_parts(120_016_020, 4085) + // Standard Error: 173_118 + .saturating_add(Weight::from_parts(42_848_338, 0).saturating_mul(t.into())) + // Standard Error: 0 + .saturating_add(Weight::from_parts(6, 0).saturating_mul(i.into())) + .saturating_add(T::DbWeight::get().reads(3_u64)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(t.into()))) + .saturating_add(T::DbWeight::get().writes(1_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(t.into()))) + .saturating_add(Weight::from_parts(0, 2182).saturating_mul(t.into())) + } + /// Storage: `Contracts::CodeInfoOf` (r:1 w:0) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + fn seal_delegate_call() -> Weight { + // Proof Size summary in bytes: + // Measured: `430` + // Estimated: `3895` + // Minimum execution time: 111_566_000 picoseconds. + Weight::from_parts(115_083_000, 3895) + .saturating_add(T::DbWeight::get().reads(2_u64)) + } + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// Storage: `Contracts::Nonce` (r:1 w:0) + /// Proof: `Contracts::Nonce` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// The range of component `i` is `[0, 983040]`. + /// The range of component `s` is `[0, 983040]`. + fn seal_instantiate(i: u32, s: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `676` + // Estimated: `4132` + // Minimum execution time: 1_871_402_000 picoseconds. + Weight::from_parts(1_890_038_000, 4132) + // Standard Error: 24 + .saturating_add(Weight::from_parts(581, 0).saturating_mul(i.into())) + // Standard Error: 24 + .saturating_add(Weight::from_parts(915, 0).saturating_mul(s.into())) + .saturating_add(T::DbWeight::get().reads(5_u64)) + .saturating_add(T::DbWeight::get().writes(3_u64)) + } + /// The range of component `n` is `[0, 1048576]`. + fn seal_hash_sha2_256(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 966_000 picoseconds. + Weight::from_parts(9_599_151, 0) + // Standard Error: 1 + .saturating_add(Weight::from_parts(1_336, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 1048576]`. + fn seal_hash_keccak_256(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_416_000 picoseconds. + Weight::from_parts(10_964_255, 0) + // Standard Error: 1 + .saturating_add(Weight::from_parts(3_593, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 1048576]`. + fn seal_hash_blake2_256(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 821_000 picoseconds. + Weight::from_parts(6_579_283, 0) + // Standard Error: 0 + .saturating_add(Weight::from_parts(1_466, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 1048576]`. + fn seal_hash_blake2_128(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 773_000 picoseconds. + Weight::from_parts(10_990_209, 0) + // Standard Error: 1 + .saturating_add(Weight::from_parts(1_457, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 125697]`. + fn seal_sr25519_verify(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 43_195_000 picoseconds. + Weight::from_parts(41_864_855, 0) + // Standard Error: 9 + .saturating_add(Weight::from_parts(5_154, 0).saturating_mul(n.into())) + } + fn seal_ecdsa_recover() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 47_747_000 picoseconds. + Weight::from_parts(49_219_000, 0) + } + fn seal_ecdsa_to_eth_address() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 12_854_000 picoseconds. + Weight::from_parts(12_962_000, 0) + } + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + fn seal_set_code_hash() -> Weight { + // Proof Size summary in bytes: + // Measured: `430` + // Estimated: `3895` + // Minimum execution time: 17_868_000 picoseconds. + Weight::from_parts(18_486_000, 3895) + .saturating_add(T::DbWeight::get().reads(2_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + fn lock_delegate_dependency() -> Weight { + // Proof Size summary in bytes: + // Measured: `355` + // Estimated: `3820` + // Minimum execution time: 8_393_000 picoseconds. + Weight::from_parts(8_640_000, 3820) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `MaxEncodedLen`) + fn unlock_delegate_dependency() -> Weight { + // Proof Size summary in bytes: + // Measured: `355` + // Estimated: `3558` + // Minimum execution time: 7_489_000 picoseconds. + Weight::from_parts(7_815_000, 3558) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + fn seal_reentrance_count() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 299_000 picoseconds. + Weight::from_parts(339_000, 0) + } + fn seal_account_reentrance_count() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 324_000 picoseconds. + Weight::from_parts(380_000, 0) + } + /// Storage: `Contracts::Nonce` (r:1 w:0) + /// Proof: `Contracts::Nonce` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + fn seal_instantiation_nonce() -> Weight { + // Proof Size summary in bytes: + // Measured: `219` + // Estimated: `1704` + // Minimum execution time: 2_768_000 picoseconds. + Weight::from_parts(3_025_000, 1704) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } + /// The range of component `r` is `[0, 5000]`. + fn instr_i64_load_store(r: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 766_000 picoseconds. + Weight::from_parts(722_169, 0) + // Standard Error: 10 + .saturating_add(Weight::from_parts(7_191, 0).saturating_mul(r.into())) + } +} + +// For backwards compatibility and tests. +impl WeightInfo for () { + /// Storage: `Contracts::DeletionQueueCounter` (r:1 w:0) + /// Proof: `Contracts::DeletionQueueCounter` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + fn on_process_deletion_queue_batch() -> Weight { + // Proof Size summary in bytes: + // Measured: `142` + // Estimated: `1627` + // Minimum execution time: 1_915_000 picoseconds. + Weight::from_parts(1_986_000, 1627) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `k` is `[0, 1024]`. + fn on_initialize_per_trie_key(k: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `452 + k * (69 ±0)` + // Estimated: `442 + k * (70 ±0)` + // Minimum execution time: 11_103_000 picoseconds. + Weight::from_parts(11_326_000, 442) + // Standard Error: 2_291 + .saturating_add(Weight::from_parts(1_196_329, 0).saturating_mul(k.into())) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(k.into()))) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(k.into()))) + .saturating_add(Weight::from_parts(0, 70).saturating_mul(k.into())) + } + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc553022fca90611ba8b7942f8bdb3b97f6580` (r:2 w:1) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc553022fca90611ba8b7942f8bdb3b97f6580` (r:2 w:1) + /// The range of component `c` is `[0, 125952]`. + fn v9_migration_step(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `211 + c * (1 ±0)` + // Estimated: `6149 + c * (1 ±0)` + // Minimum execution time: 7_783_000 picoseconds. + Weight::from_parts(4_462_075, 6149) + // Standard Error: 5 + .saturating_add(Weight::from_parts(1_634, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(c.into())) + } + /// Storage: `Contracts::ContractInfoOf` (r:2 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:0) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + fn v10_migration_step() -> Weight { + // Proof Size summary in bytes: + // Measured: `510` + // Estimated: `6450` + // Minimum execution time: 15_971_000 picoseconds. + Weight::from_parts(16_730_000, 6450) + .saturating_add(RocksDbWeight::get().reads(3_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::DeletionQueue` (r:1 w:1025) + /// Proof: `Contracts::DeletionQueue` (`max_values`: None, `max_size`: Some(142), added: 2617, mode: `Measured`) + /// Storage: `Contracts::DeletionQueueCounter` (r:0 w:1) + /// Proof: `Contracts::DeletionQueueCounter` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// The range of component `k` is `[0, 1024]`. + fn v11_migration_step(k: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `171 + k * (1 ±0)` + // Estimated: `3635 + k * (1 ±0)` + // Minimum execution time: 3_149_000 picoseconds. + Weight::from_parts(3_264_000, 3635) + // Standard Error: 559 + .saturating_add(Weight::from_parts(1_111_209, 0).saturating_mul(k.into())) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(k.into()))) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(k.into())) + } + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc553053f13fd319a03c211337c76e0fe776df` (r:2 w:0) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc553053f13fd319a03c211337c76e0fe776df` (r:2 w:0) + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc553022fca90611ba8b7942f8bdb3b97f6580` (r:1 w:1) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc553022fca90611ba8b7942f8bdb3b97f6580` (r:1 w:1) + /// Storage: `System::Account` (r:1 w:0) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:0 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// The range of component `c` is `[0, 125952]`. + fn v12_migration_step(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `325 + c * (1 ±0)` + // Estimated: `6263 + c * (1 ±0)` + // Minimum execution time: 15_072_000 picoseconds. + Weight::from_parts(15_721_891, 6263) + // Standard Error: 2 + .saturating_add(Weight::from_parts(428, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(c.into())) + } + /// Storage: `Contracts::ContractInfoOf` (r:2 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + fn v13_migration_step() -> Weight { + // Proof Size summary in bytes: + // Measured: `440` + // Estimated: `6380` + // Minimum execution time: 12_047_000 picoseconds. + Weight::from_parts(12_500_000, 6380) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::CodeInfoOf` (r:2 w:0) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// Storage: `Balances::Holds` (r:1 w:0) + /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) + fn v14_migration_step() -> Weight { + // Proof Size summary in bytes: + // Measured: `352` + // Estimated: `6292` + // Minimum execution time: 47_488_000 picoseconds. + Weight::from_parts(48_482_000, 6292) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::ContractInfoOf` (r:2 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `System::Account` (r:2 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + fn v15_migration_step() -> Weight { + // Proof Size summary in bytes: + // Measured: `594` + // Estimated: `6534` + // Minimum execution time: 52_801_000 picoseconds. + Weight::from_parts(54_230_000, 6534) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + } + /// Storage: `Contracts::ContractInfoOf` (r:2 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + fn v16_migration_step() -> Weight { + // Proof Size summary in bytes: + // Measured: `409` + // Estimated: `6349` + // Minimum execution time: 11_618_000 picoseconds. + Weight::from_parts(12_068_000, 6349) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:1) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + fn migration_noop() -> Weight { + // Proof Size summary in bytes: + // Measured: `142` + // Estimated: `1627` + // Minimum execution time: 2_131_000 picoseconds. + Weight::from_parts(2_255_000, 1627) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:1) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:1) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:1) + fn migrate() -> Weight { + // Proof Size summary in bytes: + // Measured: `166` + // Estimated: `3631` + // Minimum execution time: 10_773_000 picoseconds. + Weight::from_parts(11_118_000, 3631) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + } + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:0) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:0) + fn on_runtime_upgrade_noop() -> Weight { + // Proof Size summary in bytes: + // Measured: `142` + // Estimated: `3607` + // Minimum execution time: 4_371_000 picoseconds. + Weight::from_parts(4_624_000, 3607) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:0) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:0) + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + fn on_runtime_upgrade_in_progress() -> Weight { + // Proof Size summary in bytes: + // Measured: `167` + // Estimated: `3632` + // Minimum execution time: 5_612_000 picoseconds. + Weight::from_parts(5_838_000, 3632) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + } + /// Storage: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:0) + /// Proof: UNKNOWN KEY `0x4342193e496fab7ec59d615ed0dc55304e7b9012096b41c4eb3aaf947f6ea429` (r:1 w:0) + /// Storage: `Contracts::MigrationInProgress` (r:1 w:1) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + fn on_runtime_upgrade() -> Weight { + // Proof Size summary in bytes: + // Measured: `142` + // Estimated: `3607` + // Minimum execution time: 5_487_000 picoseconds. + Weight::from_parts(5_693_000, 3607) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:0) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// Storage: `Timestamp::Now` (r:1 w:0) + /// Proof: `Timestamp::Now` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// The range of component `c` is `[0, 125952]`. + fn call_with_code_per_byte(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `800 + c * (1 ±0)` + // Estimated: `4266 + c * (1 ±0)` + // Minimum execution time: 247_545_000 picoseconds. + Weight::from_parts(268_016_699, 4266) + // Standard Error: 4 + .saturating_add(Weight::from_parts(700, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(6_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(c.into())) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Balances::Holds` (r:2 w:2) + /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) + /// Storage: `Contracts::Nonce` (r:1 w:1) + /// Proof: `Contracts::Nonce` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `Timestamp::Now` (r:1 w:0) + /// Proof: `Timestamp::Now` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:0 w:1) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// The range of component `c` is `[0, 125952]`. + /// The range of component `i` is `[0, 1048576]`. + /// The range of component `s` is `[0, 1048576]`. + fn instantiate_with_code(c: u32, i: u32, s: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `323` + // Estimated: `6262` + // Minimum execution time: 4_396_772_000 picoseconds. + Weight::from_parts(235_107_907, 6262) + // Standard Error: 185 + .saturating_add(Weight::from_parts(53_843, 0).saturating_mul(c.into())) + // Standard Error: 22 + .saturating_add(Weight::from_parts(2_143, 0).saturating_mul(i.into())) + // Standard Error: 22 + .saturating_add(Weight::from_parts(2_210, 0).saturating_mul(s.into())) + .saturating_add(RocksDbWeight::get().reads(8_u64)) + .saturating_add(RocksDbWeight::get().writes(7_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// Storage: `Contracts::Nonce` (r:1 w:1) + /// Proof: `Contracts::Nonce` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `Timestamp::Now` (r:1 w:0) + /// Proof: `Timestamp::Now` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// Storage: `Balances::Holds` (r:1 w:1) + /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) + /// The range of component `i` is `[0, 1048576]`. + /// The range of component `s` is `[0, 1048576]`. + fn instantiate(i: u32, s: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `560` + // Estimated: `4017` + // Minimum execution time: 2_240_868_000 picoseconds. + Weight::from_parts(2_273_668_000, 4017) + // Standard Error: 32 + .saturating_add(Weight::from_parts(934, 0).saturating_mul(i.into())) + // Standard Error: 32 + .saturating_add(Weight::from_parts(920, 0).saturating_mul(s.into())) + .saturating_add(RocksDbWeight::get().reads(8_u64)) + .saturating_add(RocksDbWeight::get().writes(5_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:0) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// Storage: `Timestamp::Now` (r:1 w:0) + /// Proof: `Timestamp::Now` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + fn call() -> Weight { + // Proof Size summary in bytes: + // Measured: `826` + // Estimated: `4291` + // Minimum execution time: 165_067_000 picoseconds. + Weight::from_parts(168_582_000, 4291) + .saturating_add(RocksDbWeight::get().reads(6_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Balances::Holds` (r:1 w:1) + /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:0 w:1) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// The range of component `c` is `[0, 125952]`. + fn upload_code_determinism_enforced(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `142` + // Estimated: `3607` + // Minimum execution time: 229_454_000 picoseconds. + Weight::from_parts(251_495_551, 3607) + // Standard Error: 71 + .saturating_add(Weight::from_parts(51_428, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(3_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Balances::Holds` (r:1 w:1) + /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:0 w:1) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// The range of component `c` is `[0, 125952]`. + fn upload_code_determinism_relaxed(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `142` + // Estimated: `3607` + // Minimum execution time: 240_390_000 picoseconds. + Weight::from_parts(273_854_266, 3607) + // Standard Error: 243 + .saturating_add(Weight::from_parts(51_836, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(3_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Balances::Holds` (r:1 w:1) + /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:0 w:1) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + fn remove_code() -> Weight { + // Proof Size summary in bytes: + // Measured: `315` + // Estimated: `3780` + // Minimum execution time: 39_374_000 picoseconds. + Weight::from_parts(40_247_000, 3780) + .saturating_add(RocksDbWeight::get().reads(3_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } + /// Storage: `Contracts::MigrationInProgress` (r:1 w:0) + /// Proof: `Contracts::MigrationInProgress` (`max_values`: Some(1), `max_size`: Some(1026), added: 1521, mode: `Measured`) + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:2 w:2) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + fn set_code() -> Weight { + // Proof Size summary in bytes: + // Measured: `552` + // Estimated: `6492` + // Minimum execution time: 24_473_000 picoseconds. + Weight::from_parts(25_890_000, 6492) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } + /// The range of component `r` is `[0, 1600]`. + fn noop_host_fn(r: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 8_528_000 picoseconds. + Weight::from_parts(9_301_010, 0) + // Standard Error: 98 + .saturating_add(Weight::from_parts(53_173, 0).saturating_mul(r.into())) + } + fn seal_caller() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 643_000 picoseconds. + Weight::from_parts(678_000, 0) + } + /// Storage: `Contracts::ContractInfoOf` (r:1 w:0) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + fn seal_is_contract() -> Weight { + // Proof Size summary in bytes: + // Measured: `354` + // Estimated: `3819` + // Minimum execution time: 6_107_000 picoseconds. + Weight::from_parts(6_235_000, 3819) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } + /// Storage: `Contracts::ContractInfoOf` (r:1 w:0) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + fn seal_code_hash() -> Weight { + // Proof Size summary in bytes: + // Measured: `447` + // Estimated: `3912` + // Minimum execution time: 7_316_000 picoseconds. + Weight::from_parts(7_653_000, 3912) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } + fn seal_own_code_hash() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 721_000 picoseconds. + Weight::from_parts(764_000, 0) + } + fn seal_caller_is_origin() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 369_000 picoseconds. + Weight::from_parts(417_000, 0) + } + fn seal_caller_is_root() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 318_000 picoseconds. + Weight::from_parts(349_000, 0) + } + fn seal_address() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 590_000 picoseconds. + Weight::from_parts(628_000, 0) + } + fn seal_gas_left() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 660_000 picoseconds. + Weight::from_parts(730_000, 0) + } + fn seal_balance() -> Weight { + // Proof Size summary in bytes: + // Measured: `140` + // Estimated: `0` + // Minimum execution time: 4_361_000 picoseconds. + Weight::from_parts(4_577_000, 0) + } + fn seal_value_transferred() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 560_000 picoseconds. + Weight::from_parts(603_000, 0) + } + fn seal_minimum_balance() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 561_000 picoseconds. + Weight::from_parts(610_000, 0) + } + fn seal_block_number() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 557_000 picoseconds. + Weight::from_parts(583_000, 0) + } + fn seal_now() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 550_000 picoseconds. + Weight::from_parts(602_000, 0) + } + /// Storage: `TransactionPayment::NextFeeMultiplier` (r:1 w:0) + /// Proof: `TransactionPayment::NextFeeMultiplier` (`max_values`: Some(1), `max_size`: Some(16), added: 511, mode: `Measured`) + fn seal_weight_to_fee() -> Weight { + // Proof Size summary in bytes: + // Measured: `67` + // Estimated: `1552` + // Minimum execution time: 4_065_000 picoseconds. + Weight::from_parts(4_291_000, 1552) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } + /// The range of component `n` is `[0, 1048572]`. + fn seal_input(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 487_000 picoseconds. + Weight::from_parts(517_000, 0) + // Standard Error: 3 + .saturating_add(Weight::from_parts(301, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 1048572]`. + fn seal_return(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 318_000 picoseconds. + Weight::from_parts(372_000, 0) + // Standard Error: 10 + .saturating_add(Weight::from_parts(411, 0).saturating_mul(n.into())) + } + /// Storage: `Contracts::DeletionQueueCounter` (r:1 w:1) + /// Proof: `Contracts::DeletionQueueCounter` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:33 w:33) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::DeletionQueue` (r:0 w:1) + /// Proof: `Contracts::DeletionQueue` (`max_values`: None, `max_size`: Some(142), added: 2617, mode: `Measured`) + /// The range of component `n` is `[0, 32]`. + fn seal_terminate(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `319 + n * (78 ±0)` + // Estimated: `3784 + n * (2553 ±0)` + // Minimum execution time: 13_251_000 picoseconds. + Weight::from_parts(15_257_892, 3784) + // Standard Error: 7_089 + .saturating_add(Weight::from_parts(3_443_907, 0).saturating_mul(n.into())) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(n.into()))) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(n.into()))) + .saturating_add(Weight::from_parts(0, 2553).saturating_mul(n.into())) + } + /// Storage: `RandomnessCollectiveFlip::RandomMaterial` (r:1 w:0) + /// Proof: `RandomnessCollectiveFlip::RandomMaterial` (`max_values`: Some(1), `max_size`: Some(2594), added: 3089, mode: `Measured`) + fn seal_random() -> Weight { + // Proof Size summary in bytes: + // Measured: `76` + // Estimated: `1561` + // Minimum execution time: 3_434_000 picoseconds. + Weight::from_parts(3_605_000, 1561) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } + /// Storage: `System::EventTopics` (r:4 w:4) + /// Proof: `System::EventTopics` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `t` is `[0, 4]`. + /// The range of component `n` is `[0, 16384]`. + fn seal_deposit_event(t: u32, n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `990 + t * (2475 ±0)` + // Minimum execution time: 3_668_000 picoseconds. + Weight::from_parts(3_999_591, 990) + // Standard Error: 5_767 + .saturating_add(Weight::from_parts(2_011_090, 0).saturating_mul(t.into())) + // Standard Error: 1 + .saturating_add(Weight::from_parts(12, 0).saturating_mul(n.into())) + .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(t.into()))) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(t.into()))) + .saturating_add(Weight::from_parts(0, 2475).saturating_mul(t.into())) + } + /// The range of component `i` is `[0, 1048576]`. + fn seal_debug_message(i: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 443_000 picoseconds. + Weight::from_parts(472_000, 0) + // Standard Error: 10 + .saturating_add(Weight::from_parts(1_207, 0).saturating_mul(i.into())) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn get_storage_empty() -> Weight { + // Proof Size summary in bytes: + // Measured: `16618` + // Estimated: `16618` + // Minimum execution time: 13_752_000 picoseconds. + Weight::from_parts(14_356_000, 16618) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn get_storage_full() -> Weight { + // Proof Size summary in bytes: + // Measured: `26628` + // Estimated: `26628` + // Minimum execution time: 43_444_000 picoseconds. + Weight::from_parts(45_087_000, 26628) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn set_storage_empty() -> Weight { + // Proof Size summary in bytes: + // Measured: `16618` + // Estimated: `16618` + // Minimum execution time: 15_616_000 picoseconds. + Weight::from_parts(16_010_000, 16618) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + fn set_storage_full() -> Weight { + // Proof Size summary in bytes: + // Measured: `26628` + // Estimated: `26628` + // Minimum execution time: 47_020_000 picoseconds. + Weight::from_parts(50_152_000, 26628) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `n` is `[0, 16384]`. + /// The range of component `o` is `[0, 16384]`. + fn seal_set_storage(n: u32, o: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `250 + o * (1 ±0)` + // Estimated: `249 + o * (1 ±0)` + // Minimum execution time: 8_824_000 picoseconds. + Weight::from_parts(8_915_233, 249) + // Standard Error: 1 + .saturating_add(Weight::from_parts(255, 0).saturating_mul(n.into())) + // Standard Error: 1 + .saturating_add(Weight::from_parts(39, 0).saturating_mul(o.into())) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(o.into())) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `n` is `[0, 16384]`. + fn seal_clear_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `248 + n * (1 ±0)` + // Estimated: `248 + n * (1 ±0)` + // Minimum execution time: 7_133_000 picoseconds. + Weight::from_parts(7_912_778, 248) + // Standard Error: 1 + .saturating_add(Weight::from_parts(88, 0).saturating_mul(n.into())) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(n.into())) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `n` is `[0, 16384]`. + fn seal_get_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `248 + n * (1 ±0)` + // Estimated: `248 + n * (1 ±0)` + // Minimum execution time: 6_746_000 picoseconds. + Weight::from_parts(7_647_236, 248) + // Standard Error: 2 + .saturating_add(Weight::from_parts(603, 0).saturating_mul(n.into())) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(n.into())) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `n` is `[0, 16384]`. + fn seal_contains_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `248 + n * (1 ±0)` + // Estimated: `248 + n * (1 ±0)` + // Minimum execution time: 6_247_000 picoseconds. + Weight::from_parts(6_952_661, 248) + // Standard Error: 1 + .saturating_add(Weight::from_parts(77, 0).saturating_mul(n.into())) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(n.into())) + } + /// Storage: `Skipped::Metadata` (r:0 w:0) + /// Proof: `Skipped::Metadata` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `n` is `[0, 16384]`. + fn seal_take_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `248 + n * (1 ±0)` + // Estimated: `248 + n * (1 ±0)` + // Minimum execution time: 7_428_000 picoseconds. + Weight::from_parts(8_384_015, 248) + // Standard Error: 2 + .saturating_add(Weight::from_parts(625, 0).saturating_mul(n.into())) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + .saturating_add(Weight::from_parts(0, 1).saturating_mul(n.into())) + } + fn set_transient_storage_empty() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_478_000 picoseconds. + Weight::from_parts(1_533_000, 0) + } + fn set_transient_storage_full() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 2_485_000 picoseconds. + Weight::from_parts(2_728_000, 0) + } + fn get_transient_storage_empty() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 3_195_000 picoseconds. + Weight::from_parts(3_811_000, 0) + } + fn get_transient_storage_full() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 3_902_000 picoseconds. + Weight::from_parts(4_118_000, 0) + } + fn rollback_transient_storage() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_571_000 picoseconds. + Weight::from_parts(1_662_000, 0) + } + /// The range of component `n` is `[0, 16384]`. + /// The range of component `o` is `[0, 16384]`. + fn seal_set_transient_storage(n: u32, o: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 5_250_000 picoseconds. + Weight::from_parts(2_465_568, 0) + // Standard Error: 0 + .saturating_add(Weight::from_parts(201, 0).saturating_mul(n.into())) + // Standard Error: 0 + .saturating_add(Weight::from_parts(223, 0).saturating_mul(o.into())) + } + /// The range of component `n` is `[0, 16384]`. + fn seal_clear_transient_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 2_012_000 picoseconds. + Weight::from_parts(2_288_004, 0) + // Standard Error: 3 + .saturating_add(Weight::from_parts(239, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 16384]`. + fn seal_get_transient_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_906_000 picoseconds. + Weight::from_parts(2_121_040, 0) + // Standard Error: 0 + .saturating_add(Weight::from_parts(225, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 16384]`. + fn seal_contains_transient_storage(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_736_000 picoseconds. + Weight::from_parts(1_954_728, 0) + // Standard Error: 0 + .saturating_add(Weight::from_parts(111, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 16384]`. + fn seal_take_transient_storage(_n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 7_872_000 picoseconds. + Weight::from_parts(8_125_644, 0) + } + fn seal_transfer() -> Weight { + // Proof Size summary in bytes: + // Measured: `140` + // Estimated: `0` + // Minimum execution time: 8_489_000 picoseconds. + Weight::from_parts(8_791_000, 0) + } + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `Contracts::CodeInfoOf` (r:1 w:0) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// The range of component `t` is `[0, 1]`. + /// The range of component `i` is `[0, 1048576]`. + fn seal_call(t: u32, i: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `620 + t * (280 ±0)` + // Estimated: `4085 + t * (2182 ±0)` + // Minimum execution time: 122_759_000 picoseconds. + Weight::from_parts(120_016_020, 4085) + // Standard Error: 173_118 + .saturating_add(Weight::from_parts(42_848_338, 0).saturating_mul(t.into())) + // Standard Error: 0 + .saturating_add(Weight::from_parts(6, 0).saturating_mul(i.into())) + .saturating_add(RocksDbWeight::get().reads(3_u64)) + .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(t.into()))) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(t.into()))) + .saturating_add(Weight::from_parts(0, 2182).saturating_mul(t.into())) + } + /// Storage: `Contracts::CodeInfoOf` (r:1 w:0) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + fn seal_delegate_call() -> Weight { + // Proof Size summary in bytes: + // Measured: `430` + // Estimated: `3895` + // Minimum execution time: 111_566_000 picoseconds. + Weight::from_parts(115_083_000, 3895) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + } + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + /// Storage: `Contracts::Nonce` (r:1 w:0) + /// Proof: `Contracts::Nonce` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + /// Storage: `Contracts::ContractInfoOf` (r:1 w:1) + /// Proof: `Contracts::ContractInfoOf` (`max_values`: None, `max_size`: Some(1795), added: 4270, mode: `Measured`) + /// Storage: `System::Account` (r:1 w:1) + /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) + /// The range of component `i` is `[0, 983040]`. + /// The range of component `s` is `[0, 983040]`. + fn seal_instantiate(i: u32, s: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `676` + // Estimated: `4132` + // Minimum execution time: 1_871_402_000 picoseconds. + Weight::from_parts(1_890_038_000, 4132) + // Standard Error: 24 + .saturating_add(Weight::from_parts(581, 0).saturating_mul(i.into())) + // Standard Error: 24 + .saturating_add(Weight::from_parts(915, 0).saturating_mul(s.into())) + .saturating_add(RocksDbWeight::get().reads(5_u64)) + .saturating_add(RocksDbWeight::get().writes(3_u64)) + } + /// The range of component `n` is `[0, 1048576]`. + fn seal_hash_sha2_256(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 966_000 picoseconds. + Weight::from_parts(9_599_151, 0) + // Standard Error: 1 + .saturating_add(Weight::from_parts(1_336, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 1048576]`. + fn seal_hash_keccak_256(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_416_000 picoseconds. + Weight::from_parts(10_964_255, 0) + // Standard Error: 1 + .saturating_add(Weight::from_parts(3_593, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 1048576]`. + fn seal_hash_blake2_256(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 821_000 picoseconds. + Weight::from_parts(6_579_283, 0) + // Standard Error: 0 + .saturating_add(Weight::from_parts(1_466, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 1048576]`. + fn seal_hash_blake2_128(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 773_000 picoseconds. + Weight::from_parts(10_990_209, 0) + // Standard Error: 1 + .saturating_add(Weight::from_parts(1_457, 0).saturating_mul(n.into())) + } + /// The range of component `n` is `[0, 125697]`. + fn seal_sr25519_verify(n: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 43_195_000 picoseconds. + Weight::from_parts(41_864_855, 0) + // Standard Error: 9 + .saturating_add(Weight::from_parts(5_154, 0).saturating_mul(n.into())) + } + fn seal_ecdsa_recover() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 47_747_000 picoseconds. + Weight::from_parts(49_219_000, 0) + } + fn seal_ecdsa_to_eth_address() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 12_854_000 picoseconds. + Weight::from_parts(12_962_000, 0) + } + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + /// Storage: `Contracts::PristineCode` (r:1 w:0) + /// Proof: `Contracts::PristineCode` (`max_values`: None, `max_size`: Some(125988), added: 128463, mode: `Measured`) + fn seal_set_code_hash() -> Weight { + // Proof Size summary in bytes: + // Measured: `430` + // Estimated: `3895` + // Minimum execution time: 17_868_000 picoseconds. + Weight::from_parts(18_486_000, 3895) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `Measured`) + fn lock_delegate_dependency() -> Weight { + // Proof Size summary in bytes: + // Measured: `355` + // Estimated: `3820` + // Minimum execution time: 8_393_000 picoseconds. + Weight::from_parts(8_640_000, 3820) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + /// Storage: `Contracts::CodeInfoOf` (r:1 w:1) + /// Proof: `Contracts::CodeInfoOf` (`max_values`: None, `max_size`: Some(93), added: 2568, mode: `MaxEncodedLen`) + fn unlock_delegate_dependency() -> Weight { + // Proof Size summary in bytes: + // Measured: `355` + // Estimated: `3558` + // Minimum execution time: 7_489_000 picoseconds. + Weight::from_parts(7_815_000, 3558) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + fn seal_reentrance_count() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 299_000 picoseconds. + Weight::from_parts(339_000, 0) + } + fn seal_account_reentrance_count() -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 324_000 picoseconds. + Weight::from_parts(380_000, 0) + } + /// Storage: `Contracts::Nonce` (r:1 w:0) + /// Proof: `Contracts::Nonce` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `Measured`) + fn seal_instantiation_nonce() -> Weight { + // Proof Size summary in bytes: + // Measured: `219` + // Estimated: `1704` + // Minimum execution time: 2_768_000 picoseconds. + Weight::from_parts(3_025_000, 1704) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } + /// The range of component `r` is `[0, 5000]`. + fn instr_i64_load_store(r: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 766_000 picoseconds. + Weight::from_parts(722_169, 0) + // Standard Error: 10 + .saturating_add(Weight::from_parts(7_191, 0).saturating_mul(r.into())) + } +} diff --git a/pallets/contracts/uapi/Cargo.toml b/pallets/contracts/uapi/Cargo.toml new file mode 100644 index 00000000..6744532d --- /dev/null +++ b/pallets/contracts/uapi/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "pallet-contracts-uapi" +version = "5.0.0" +authors.workspace = true +edition.workspace = true +license = "Apache-2.0" +homepage.workspace = true +repository.workspace = true +description = "Exposes all the host functions that a contract can import." + +[dependencies] +paste = { workspace = true } +bitflags = { workspace = true } +scale-info = { features = ["derive"], optional = true, workspace = true } +codec = { features = [ + "derive", + "max-encoded-len", +], optional = true, workspace = true } + +[target.'cfg(target_arch = "riscv32")'.dependencies] +polkavm-derive = { workspace = true } + +[package.metadata.docs.rs] +default-target = ["wasm32-unknown-unknown"] + +[features] +default = ["scale"] +scale = ["dep:codec", "scale-info"] diff --git a/pallets/contracts/uapi/src/flags.rs b/pallets/contracts/uapi/src/flags.rs new file mode 100644 index 00000000..9ad105b8 --- /dev/null +++ b/pallets/contracts/uapi/src/flags.rs @@ -0,0 +1,81 @@ +// This file is part of Substrate. + +// 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 bitflags::bitflags; + +bitflags! { + /// Flags used by a contract to customize exit behaviour. + #[cfg_attr(feature = "scale", derive(codec::Encode, codec::Decode, scale_info::TypeInfo))] + pub struct ReturnFlags: u32 { + /// If this bit is set all changes made by the contract execution are rolled back. + const REVERT = 0x0000_0001; + } +} + +bitflags! { + /// Flags used to change the behaviour of `seal_call` and `seal_delegate_call`. + pub struct CallFlags: u32 { + /// Forward the input of current function to the callee. + /// + /// Supplied input pointers are ignored when set. + /// + /// # Note + /// + /// A forwarding call will consume the current contracts input. Any attempt to + /// access the input after this call returns will lead to [`Error::InputForwarded`]. + /// It does not matter if this is due to calling `seal_input` or trying another + /// forwarding call. Consider using [`Self::CLONE_INPUT`] in order to preserve + /// the input. + const FORWARD_INPUT = 0b0000_0001; + /// Identical to [`Self::FORWARD_INPUT`] but without consuming the input. + /// + /// This adds some additional weight costs to the call. + /// + /// # Note + /// + /// This implies [`Self::FORWARD_INPUT`] and takes precedence when both are set. + const CLONE_INPUT = 0b0000_0010; + /// Do not return from the call but rather return the result of the callee to the + /// callers caller. + /// + /// # Note + /// + /// This makes the current contract completely transparent to its caller by replacing + /// this contracts potential output by the callee ones. Any code after `seal_call` + /// can be safely considered unreachable. + const TAIL_CALL = 0b0000_0100; + /// Allow the callee to reenter into the current contract. + /// + /// Without this flag any reentrancy into the current contract that originates from + /// the callee (or any of its callees) is denied. This includes the first callee: + /// You cannot call into yourself with this flag set. + /// + /// # Note + /// + /// For `seal_delegate_call` should be always unset, otherwise + /// [`Error::InvalidCallFlags`] is returned. + const ALLOW_REENTRY = 0b0000_1000; + /// Indicates that the callee is restricted from modifying the state during call execution, + /// equivalent to Ethereum's STATICCALL. + /// + /// # Note + /// + /// For `seal_delegate_call` should be always unset, otherwise + /// [`Error::InvalidCallFlags`] is returned. + const READ_ONLY = 0b0001_0000; + } +} diff --git a/pallets/contracts/uapi/src/host.rs b/pallets/contracts/uapi/src/host.rs new file mode 100644 index 00000000..51f0cd7e --- /dev/null +++ b/pallets/contracts/uapi/src/host.rs @@ -0,0 +1,888 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// +// 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::{CallFlags, Result, ReturnFlags}; +use paste::paste; + +#[cfg(target_arch = "wasm32")] +mod wasm32; + +#[cfg(target_arch = "riscv32")] +mod riscv32; + +macro_rules! hash_fn { + ( $name:ident, $bytes:literal ) => { + paste! { + #[doc = "Computes the " $name " " $bytes "-bit hash on the given input buffer."] + #[doc = "\n# Notes\n"] + #[doc = "- The `input` and `output` buffer may overlap."] + #[doc = "- The output buffer is expected to hold at least " $bytes " bits."] + #[doc = "- It is the callers responsibility to provide an output buffer that is large enough to hold the expected amount of bytes returned by the hash function."] + #[doc = "\n# Parameters\n"] + #[doc = "- `input`: The input data buffer."] + #[doc = "- `output`: The output buffer to write the hash result to."] + fn [](input: &[u8], output: &mut [u8; $bytes]); + } + }; +} + +// TODO remove cfg once used by all targets +#[cfg(target_arch = "wasm32")] +#[inline(always)] +fn extract_from_slice(output: &mut &mut [u8], new_len: usize) { + debug_assert!(new_len <= output.len()); + let tmp = core::mem::take(output); + *output = &mut tmp[..new_len]; +} + +#[cfg(target_arch = "wasm32")] +#[inline(always)] +fn ptr_len_or_sentinel(data: &mut Option<&mut &mut [u8]>) -> (*mut u8, u32) { + match data { + Some(ref mut data) => (data.as_mut_ptr(), data.len() as _), + None => (crate::SENTINEL as _, 0), + } +} + +#[cfg(target_arch = "wasm32")] +#[inline(always)] +fn ptr_or_sentinel(data: &Option<&[u8]>) -> *const u8 { + match data { + Some(ref data) => data.as_ptr(), + None => crate::SENTINEL as _, + } +} + +/// Implements [`HostFn`] for each supported target architecture. +pub enum HostFnImpl {} + +/// Defines all the host apis implemented by both wasm and RISC-V vms. +pub trait HostFn: private::Sealed { + /// Returns the number of times specified contract exists on the call stack. Delegated calls are + /// not counted as separate calls. + /// + /// # Parameters + /// + /// - `account`: The contract address. Should be decodable as an `T::AccountId`. Traps + /// otherwise. + /// + /// # Return + /// + /// Returns the number of times specified contract exists on the call stack. + #[deprecated( + note = "Unstable function. Behaviour can change without further notice. Use only for testing." + )] + fn account_reentrance_count(account: &[u8]) -> u32; + + /// Stores the address of the current contract into the supplied buffer. + /// + /// If the available space in `output` is less than the size of the value a trap is triggered. + /// + /// # Parameters + /// + /// - `output`: A reference to the output data buffer to write the address. + fn address(output: &mut &mut [u8]); + + /// Lock a new delegate dependency to the contract. + /// + /// Traps if the maximum number of delegate_dependencies is reached or if + /// the delegate dependency already exists. + /// + /// # Parameters + /// + /// - `code_hash`: The code hash of the dependency. Should be decodable as an `T::Hash`. Traps + /// otherwise. + fn lock_delegate_dependency(code_hash: &[u8]); + + /// Stores the *free* balance of the current account into the supplied buffer. + /// + /// If the available space in `output` is less than the size of the value a trap is triggered. + /// + /// # Parameters + /// + /// - `output`: A reference to the output data buffer to write the balance. + fn balance(output: &mut &mut [u8]); + + /// Stores the current block number of the current contract into the supplied buffer. + /// + /// If the available space in `output` is less than the size of the value a trap is triggered. + /// + /// # Parameters + /// + /// - `output`: A reference to the output data buffer to write the block number. + fn block_number(output: &mut &mut [u8]); + + /// Make a call to another contract. + /// + /// This is equivalent to calling the newer version of this function with + /// `flags` set to [`CallFlags::ALLOW_REENTRY`]. See the newer version for documentation. + #[deprecated(note = "Deprecated, use newer version instead")] + fn call( + callee: &[u8], + gas: u64, + value: &[u8], + input_data: &[u8], + output: Option<&mut &mut [u8]>, + ) -> Result; + + /// Make a call to another contract. + /// + /// Equivalent to the newer [`Self::call_v2`] version but works with + /// *ref_time* Weight only + #[deprecated(note = "Deprecated, use newer version instead")] + fn call_v1( + flags: CallFlags, + callee: &[u8], + gas: u64, + value: &[u8], + input_data: &[u8], + output: Option<&mut &mut [u8]>, + ) -> Result; + + /// Call (possibly transferring some amount of funds) into the specified account. + /// + /// # Parameters + /// + /// - `flags`: See [`CallFlags`] for a documentation of the supported flags. + /// - `callee`: The address of the callee. Should be decodable as an `T::AccountId`. Traps + /// otherwise. + /// - `ref_time_limit`: how much *ref_time* Weight to devote to the execution. + /// - `proof_size_limit`: how much *proof_size* Weight to devote to the execution. + /// - `deposit`: The storage deposit limit for instantiation. Should be decodable as a + /// `Option`. Traps otherwise. Passing `None` means setting no specific limit for + /// the call, which implies storage usage up to the limit of the parent call. + /// - `value`: The value to transfer into the contract. Should be decodable as a `T::Balance`. + /// Traps otherwise. + /// - `input`: The input data buffer used to call the contract. + /// - `output`: A reference to the output data buffer to write the call output buffer. If `None` + /// is provided then the output buffer is not copied. + /// + /// # Errors + /// + /// An error means that the call wasn't successful output buffer is returned unless + /// stated otherwise. + /// + /// - [CalleeReverted][`crate::ReturnErrorCode::CalleeReverted]: Output buffer is returned. + /// - [CalleeTrapped][`crate::ReturnErrorCode::CalleeTrapped] + /// - [TransferFailed][`crate::ReturnErrorCode::TransferFailed] + /// - [NotCallable][`crate::ReturnErrorCode::NotCallable] + fn call_v2( + flags: CallFlags, + callee: &[u8], + ref_time_limit: u64, + proof_size_limit: u64, + deposit: Option<&[u8]>, + value: &[u8], + input_data: &[u8], + output: Option<&mut &mut [u8]>, + ) -> Result; + + /// Call into the chain extension provided by the chain if any. + /// + /// Handling of the input values is up to the specific chain extension and so is the + /// return value. The extension can decide to use the inputs as primitive inputs or as + /// in/out arguments by interpreting them as pointers. Any caller of this function + /// must therefore coordinate with the chain that it targets. + /// + /// # Note + /// + /// If no chain extension exists the contract will trap with the `NoChainExtension` + /// module error. + /// + /// # Parameters + /// + /// - `func_id`: The function id of the chain extension. + /// - `input`: The input data buffer. + /// - `output`: A reference to the output data buffer to write the call output buffer. If `None` + /// is provided then the output buffer is not copied. + /// + /// # Return + /// + /// The chain extension returned value, if executed successfully. + fn call_chain_extension(func_id: u32, input: &[u8], output: Option<&mut &mut [u8]>) -> u32; + + /// Call some dispatchable of the runtime. + /// + /// # Parameters + /// + /// - `call`: The call data. + /// + /// # Return + /// + /// Returns `Error::Success` when the dispatchable was successfully executed and + /// returned `Ok`. When the dispatchable was executed but returned an error + /// `Error::CallRuntimeFailed` is returned. The full error is not + /// provided because it is not guaranteed to be stable. + /// + /// # Comparison with `ChainExtension` + /// + /// Just as a chain extension this API allows the runtime to extend the functionality + /// of contracts. While making use of this function is generally easier it cannot be + /// used in all cases. Consider writing a chain extension if you need to do perform + /// one of the following tasks: + /// + /// - Return data. + /// - Provide functionality **exclusively** to contracts. + /// - Provide custom weights. + /// - Avoid the need to keep the `Call` data structure stable. + fn call_runtime(call: &[u8]) -> Result; + + /// Stores the address of the caller into the supplied buffer. + /// + /// If the available space in `output` is less than the size of the value a trap is triggered. + /// + /// If this is a top-level call (i.e. initiated by an extrinsic) the origin address of the + /// extrinsic will be returned. Otherwise, if this call is initiated by another contract then + /// the address of the contract will be returned. + /// + /// If there is no address associated with the caller (e.g. because the caller is root) then + /// it traps with `BadOrigin`. + /// + /// # Parameters + /// + /// - `output`: A reference to the output data buffer to write the caller address. + fn caller(output: &mut &mut [u8]); + + /// Checks whether the caller of the current contract is the origin of the whole call stack. + /// + /// Prefer this over [`is_contract()`][`Self::is_contract`] when checking whether your contract + /// is being called by a contract or a plain account. The reason is that it performs better + /// since it does not need to do any storage lookups. + /// + /// # Return + /// + /// A return value of `true` indicates that this contract is being called by a plain account + /// and `false` indicates that the caller is another contract. + fn caller_is_origin() -> bool; + + /// Checks whether the caller of the current contract is root. + /// + /// Note that only the origin of the call stack can be root. Hence this function returning + /// `true` implies that the contract is being called by the origin. + /// + /// A return value of `true` indicates that this contract is being called by a root origin, + /// and `false` indicates that the caller is a signed origin. + fn caller_is_root() -> u32; + + /// Clear the value at the given key in the contract storage. + /// + /// Equivalent to the newer [`Self::clear_storage_v1`] version with + /// the exception of the return type. Still a valid thing to call when not interested in the + /// return value. + fn clear_storage(key: &[u8]); + + /// Clear the value at the given key in the contract storage. + /// + /// # Parameters + /// + /// - `key`: The storage key. + /// + /// # Return + /// + /// Returns the size of the pre-existing value at the specified key if any. + fn clear_storage_v1(key: &[u8]) -> Option; + + /// Clear the value at the given key in the contract transient storage. + /// + /// # Parameters + /// + /// - `key`: The storage key. + /// + /// # Return + /// + /// Returns the size of the pre-existing value at the specified key if any. + #[deprecated( + note = "Unstable function. Behaviour can change without further notice. Use only for testing." + )] + fn clear_transient_storage(key: &[u8]) -> Option; + + /// Retrieve the code hash for a specified contract address. + /// + /// # Parameters + /// + /// - `account_id`: The address of the contract.Should be decodable as an `T::AccountId`. Traps + /// otherwise. + /// - `output`: A reference to the output data buffer to write the code hash. + /// + /// + /// # Errors + /// + /// - [CodeNotFound][`crate::ReturnErrorCode::CodeNotFound] + fn code_hash(account_id: &[u8], output: &mut [u8]) -> Result; + + /// Checks whether there is a value stored under the given key. + /// + /// This version is to be used with a fixed sized storage key. For runtimes supporting + /// transparent hashing, please use the newer version of this function. + fn contains_storage(key: &[u8]) -> Option; + + /// Checks whether there is a value stored under the given key. + /// + /// The key length must not exceed the maximum defined by the contracts module parameter. + /// + /// # Parameters + /// - `key`: The storage key. + /// + /// # Return + /// + /// Returns the size of the pre-existing value at the specified key if any. + fn contains_storage_v1(key: &[u8]) -> Option; + + /// Checks whether there is a value stored under the given key in transient storage. + /// + /// The key length must not exceed the maximum defined by the contracts module parameter. + /// + /// # Parameters + /// - `key`: The storage key. + /// + /// # Return + /// + /// Returns the size of the pre-existing value at the specified key if any. + #[deprecated( + note = "Unstable function. Behaviour can change without further notice. Use only for testing." + )] + fn contains_transient_storage(key: &[u8]) -> Option; + + /// Emit a custom debug message. + /// + /// No newlines are added to the supplied message. + /// Specifying invalid UTF-8 just drops the message with no trap. + /// + /// This is a no-op if debug message recording is disabled which is always the case + /// when the code is executing on-chain. The message is interpreted as UTF-8 and + /// appended to the debug buffer which is then supplied to the calling RPC client. + /// + /// # Note + /// + /// Even though no action is taken when debug message recording is disabled there is still + /// a non trivial overhead (and weight cost) associated with calling this function. Contract + /// languages should remove calls to this function (either at runtime or compile time) when + /// not being executed as an RPC. For example, they could allow users to disable logging + /// through compile time flags (cargo features) for on-chain deployment. Additionally, the + /// return value of this function can be cached in order to prevent further calls at runtime. + fn debug_message(str: &[u8]) -> Result; + + /// Execute code in the context (storage, caller, value) of the current contract. + /// + /// Reentrancy protection is always disabled since the callee is allowed + /// to modify the callers storage. This makes going through a reentrancy attack + /// unnecessary for the callee when it wants to exploit the caller. + /// + /// # Parameters + /// + /// - `flags`: See [`CallFlags`] for a documentation of the supported flags. + /// - `code_hash`: The hash of the code to be executed. + /// - `input`: The input data buffer used to call the contract. + /// - `output`: A reference to the output data buffer to write the call output buffer. If `None` + /// is provided then the output buffer is not copied. + /// + /// # Errors + /// + /// An error means that the call wasn't successful and no output buffer is returned unless + /// stated otherwise. + /// + /// - [CalleeReverted][`crate::ReturnErrorCode::CalleeReverted]: Output buffer is returned. + /// - [CalleeTrapped][`crate::ReturnErrorCode::CalleeTrapped] + /// - [CodeNotFound][`crate::ReturnErrorCode::CodeNotFound] + fn delegate_call( + flags: CallFlags, + code_hash: &[u8], + input_data: &[u8], + output: Option<&mut &mut [u8]>, + ) -> Result; + + /// Deposit a contract event with the data buffer and optional list of topics. There is a limit + /// on the maximum number of topics specified by `event_topics`. + /// + /// There should not be any duplicates in `topics`. + /// + /// # Parameters + /// + /// - `topics`: The topics list encoded as `Vec`. It can't contain duplicates. + fn deposit_event(topics: &[u8], data: &[u8]); + + /// Recovers the ECDSA public key from the given message hash and signature. + /// + /// Writes the public key into the given output buffer. + /// Assumes the secp256k1 curve. + /// + /// # Parameters + /// + /// - `signature`: The signature bytes. + /// - `message_hash`: The message hash bytes. + /// - `output`: A reference to the output data buffer to write the public key. + /// + /// # Errors + /// + /// - [EcdsaRecoveryFailed][`crate::ReturnErrorCode::EcdsaRecoveryFailed] + fn ecdsa_recover( + signature: &[u8; 65], + message_hash: &[u8; 32], + output: &mut [u8; 33], + ) -> Result; + + /// Calculates Ethereum address from the ECDSA compressed public key and stores + /// it into the supplied buffer. + /// + /// # Parameters + /// + /// - `pubkey`: The public key bytes. + /// - `output`: A reference to the output data buffer to write the address. + /// + /// # Errors + /// + /// - [EcdsaRecoveryFailed][`crate::ReturnErrorCode::EcdsaRecoveryFailed] + fn ecdsa_to_eth_address(pubkey: &[u8; 33], output: &mut [u8; 20]) -> Result; + + /// Stores the weight left into the supplied buffer. + /// + /// Equivalent to the newer [`Self::gas_left_v1`] version but + /// works with *ref_time* Weight only. + fn gas_left(out: &mut &mut [u8]); + + /// Stores the amount of weight left into the supplied buffer. + /// The data is encoded as Weight. + /// + /// If the available space in `output` is less than the size of the value a trap is triggered. + /// + /// # Parameters + /// + /// - `output`: A reference to the output data buffer to write the weight left. + #[deprecated( + note = "Unstable function. Behaviour can change without further notice. Use only for testing." + )] + fn gas_left_v1(output: &mut &mut [u8]); + + /// Retrieve the value under the given key from storage. + /// + /// This version is to be used with a fixed sized storage key. For runtimes supporting + /// transparent hashing, please use the newer version of this function. + fn get_storage(key: &[u8], output: &mut &mut [u8]) -> Result; + + /// Retrieve the value under the given key from storage. + /// + /// The key length must not exceed the maximum defined by the contracts module parameter. + /// + /// # Parameters + /// - `key`: The storage key. + /// - `output`: A reference to the output data buffer to write the storage entry. + /// + /// # Errors + /// + /// [KeyNotFound][`crate::ReturnErrorCode::KeyNotFound] + fn get_storage_v1(key: &[u8], output: &mut &mut [u8]) -> Result; + + /// Retrieve the value under the given key from transient storage. + /// + /// The key length must not exceed the maximum defined by the contracts module parameter. + /// + /// # Parameters + /// - `key`: The storage key. + /// - `output`: A reference to the output data buffer to write the storage entry. + /// + /// # Errors + /// + /// [KeyNotFound][`crate::ReturnErrorCode::KeyNotFound] + #[deprecated( + note = "Unstable function. Behaviour can change without further notice. Use only for testing." + )] + fn get_transient_storage(key: &[u8], output: &mut &mut [u8]) -> Result; + + hash_fn!(sha2_256, 32); + hash_fn!(keccak_256, 32); + hash_fn!(blake2_256, 32); + hash_fn!(blake2_128, 16); + + /// Stores the input passed by the caller into the supplied buffer. + /// + /// # Note + /// + /// This function traps if: + /// - the input is larger than the available space. + /// - the input was previously forwarded by a [`call()`][`Self::call()`]. + /// + /// # Parameters + /// + /// - `output`: A reference to the output data buffer to write the input data. + fn input(output: &mut &mut [u8]); + + /// Instantiate a contract with the specified code hash. + /// + /// Equivalent to the newer [`Self::instantiate_v2`] version but works + /// with *ref_time* Weight only. + #[deprecated(note = "Deprecated, use newer version instead")] + fn instantiate_v1( + code_hash: &[u8], + gas: u64, + value: &[u8], + input: &[u8], + address: Option<&mut &mut [u8]>, + output: Option<&mut &mut [u8]>, + salt: &[u8], + ) -> Result; + + /// Instantiate a contract with the specified code hash. + /// + /// This function creates an account and executes the constructor defined in the code specified + /// by the code hash. + /// + /// # Parameters + /// + /// - `code_hash`: The hash of the code to be instantiated. + /// - `ref_time_limit`: how much *ref_time* Weight to devote to the execution. + /// - `proof_size_limit`: how much *proof_size* Weight to devote to the execution. + /// - `deposit`: The storage deposit limit for instantiation. Should be decodable as a + /// `Option`. Traps otherwise. Passing `None` means setting no specific limit for + /// the call, which implies storage usage up to the limit of the parent call. + /// - `value`: The value to transfer into the contract. Should be decodable as a `T::Balance`. + /// Traps otherwise. + /// - `input`: The input data buffer. + /// - `address`: A reference to the address buffer to write the address of the contract. If + /// `None` is provided then the output buffer is not copied. + /// - `output`: A reference to the return value buffer to write the constructor output buffer. + /// If `None` is provided then the output buffer is not copied. + /// - `salt`: The salt bytes to use for this instantiation. + /// + /// # Errors + /// + /// Please consult the [ReturnErrorCode][`crate::ReturnErrorCode`] enum declaration for more + /// information on those errors. Here we only note things specific to this function. + /// + /// An error means that the account wasn't created and no address or output buffer + /// is returned unless stated otherwise. + /// + /// - [CalleeReverted][`crate::ReturnErrorCode::CalleeReverted]: Output buffer is returned. + /// - [CalleeTrapped][`crate::ReturnErrorCode::CalleeTrapped] + /// - [TransferFailed][`crate::ReturnErrorCode::TransferFailed] + /// - [CodeNotFound][`crate::ReturnErrorCode::CodeNotFound] + fn instantiate_v2( + code_hash: &[u8], + ref_time_limit: u64, + proof_size_limit: u64, + deposit: Option<&[u8]>, + value: &[u8], + input: &[u8], + address: Option<&mut &mut [u8]>, + output: Option<&mut &mut [u8]>, + salt: &[u8], + ) -> Result; + + /// Returns a nonce that is unique per contract instantiation. + /// + /// The nonce is incremented for each successful contract instantiation. This is a + /// sensible default salt for contract instantiations. + fn instantiation_nonce() -> u64; + + /// Checks whether a specified address belongs to a contract. + /// + /// # Parameters + /// + /// - `account_id`: The address to check. Should be decodable as an `T::AccountId`. Traps + /// otherwise. + /// + /// # Return + /// + /// Returns `true` if the address belongs to a contract. + fn is_contract(account_id: &[u8]) -> bool; + + /// Stores the minimum balance (a.k.a. existential deposit) into the supplied buffer. + /// The data is encoded as `T::Balance`. + /// + /// If the available space in `output` is less than the size of the value a trap is triggered. + /// + /// # Parameters + /// + /// - `output`: A reference to the output data buffer to write the minimum balance. + fn minimum_balance(output: &mut &mut [u8]); + + /// Retrieve the code hash of the currently executing contract. + /// + /// # Parameters + /// + /// - `output`: A reference to the output data buffer to write the code hash. + fn own_code_hash(output: &mut [u8]); + + /// Load the latest block timestamp into the supplied buffer + /// + /// If the available space in `output` is less than the size of the value a trap is triggered. + /// + /// # Parameters + /// + /// - `output`: A reference to the output data buffer to write the timestamp. + fn now(output: &mut &mut [u8]); + + /// Returns the number of times the currently executing contract exists on the call stack in + /// addition to the calling instance. + /// + /// # Return + /// + /// Returns `0` when there is no reentrancy. + #[deprecated( + note = "Unstable function. Behaviour can change without further notice. Use only for testing." + )] + fn reentrance_count() -> u32; + + /// Removes the delegate dependency from the contract. + /// + /// Traps if the delegate dependency does not exist. + /// + /// # Parameters + /// + /// - `code_hash`: The code hash of the dependency. Should be decodable as an `T::Hash`. Traps + /// otherwise. + fn unlock_delegate_dependency(code_hash: &[u8]); + + /// Cease contract execution and save a data buffer as a result of the execution. + /// + /// This function never returns as it stops execution of the caller. + /// This is the only way to return a data buffer to the caller. Returning from + /// execution without calling this function is equivalent to calling: + /// ```nocompile + /// return_value(ReturnFlags::empty(), &[]) + /// ``` + /// + /// Using an unnamed non empty `ReturnFlags` triggers a trap. + /// + /// # Parameters + /// + /// - `flags`: Flag used to signal special return conditions to the supervisor. See + /// [`ReturnFlags`] for a documentation of the supported flags. + /// - `return_value`: The return value buffer. + fn return_value(flags: ReturnFlags, return_value: &[u8]) -> !; + + /// Replace the contract code at the specified address with new code. + /// + /// # Note + /// + /// There are a couple of important considerations which must be taken into account when + /// using this API: + /// + /// 1. The storage at the code address will remain untouched. This means that contract + /// developers must ensure that the storage layout of the new code is compatible with that of + /// the old code. + /// + /// 2. Contracts using this API can't be assumed as having deterministic addresses. Said another + /// way, when using this API you lose the guarantee that an address always identifies a specific + /// code hash. + /// + /// 3. If a contract calls into itself after changing its code the new call would use + /// the new code. However, if the original caller panics after returning from the sub call it + /// would revert the changes made by [`set_code_hash()`][`Self::set_code_hash`] and the next + /// caller would use the old code. + /// + /// # Parameters + /// + /// - `code_hash`: The hash of the new code. Should be decodable as an `T::Hash`. Traps + /// otherwise. + /// + /// # Errors + /// + /// - [CodeNotFound][`crate::ReturnErrorCode::CodeNotFound] + fn set_code_hash(code_hash: &[u8]) -> Result; + + /// Set the value at the given key in the contract storage. + /// + /// Equivalent to [`Self::set_storage_v1`] version with the + /// exception of the return type. Still a valid thing to call for fixed sized storage key, when + /// not interested in the return value. + fn set_storage(key: &[u8], value: &[u8]); + + /// Set the value at the given key in the contract storage. + /// + /// This version is to be used with a fixed sized storage key. For runtimes supporting + /// transparent hashing, please use the newer version of this function. + fn set_storage_v1(key: &[u8], value: &[u8]) -> Option; + + /// Set the value at the given key in the contract storage. + /// + /// The key and value lengths must not exceed the maximums defined by the contracts module + /// parameters. + /// + /// # Parameters + /// + /// - `key`: The storage key. + /// - `encoded_value`: The storage value. + /// + /// # Return + /// + /// Returns the size of the pre-existing value at the specified key if any. + fn set_storage_v2(key: &[u8], value: &[u8]) -> Option; + + /// Set the value at the given key in the contract transient storage. + /// + /// The key and value lengths must not exceed the maximums defined by the contracts module + /// parameters. + /// + /// # Parameters + /// + /// - `key`: The storage key. + /// - `encoded_value`: The storage value. + /// + /// # Return + /// + /// Returns the size of the pre-existing value at the specified key if any. + #[deprecated( + note = "Unstable function. Behaviour can change without further notice. Use only for testing." + )] + fn set_transient_storage(key: &[u8], value: &[u8]) -> Option; + + /// Verify a sr25519 signature + /// + /// # Parameters + /// + /// - `signature`: The signature bytes. + /// - `message`: The message bytes. + /// + /// # Errors + /// + /// - [Sr25519VerifyFailed][`crate::ReturnErrorCode::Sr25519VerifyFailed] + fn sr25519_verify(signature: &[u8; 64], message: &[u8], pub_key: &[u8; 32]) -> Result; + + /// Retrieve and remove the value under the given key from storage. + /// + /// # Parameters + /// - `key`: The storage key. + /// - `output`: A reference to the output data buffer to write the storage entry. + /// + /// # Errors + /// + /// [KeyNotFound][`crate::ReturnErrorCode::KeyNotFound] + fn take_storage(key: &[u8], output: &mut &mut [u8]) -> Result; + + /// Retrieve and remove the value under the given key from transient storage. + /// + /// # Parameters + /// - `key`: The storage key. + /// - `output`: A reference to the output data buffer to write the storage entry. + /// + /// # Errors + /// + /// [KeyNotFound][`crate::ReturnErrorCode::KeyNotFound] + #[deprecated( + note = "Unstable function. Behaviour can change without further notice. Use only for testing." + )] + fn take_transient_storage(key: &[u8], output: &mut &mut [u8]) -> Result; + + /// Transfer some amount of funds into the specified account. + /// + /// # Parameters + /// + /// - `account_id`: The address of the account to transfer funds to. Should be decodable as an + /// `T::AccountId`. Traps otherwise. + /// - `value`: The value to transfer. Should be decodable as a `T::Balance`. Traps otherwise. + /// + /// # Errors + /// + /// - [TransferFailed][`crate::ReturnErrorCode::TransferFailed] + fn transfer(account_id: &[u8], value: &[u8]) -> Result; + + /// Remove the calling account and transfer remaining balance. + /// + /// This is equivalent to calling the newer version of this function + #[deprecated(note = "Deprecated, use newer version instead")] + fn terminate(beneficiary: &[u8]) -> !; + + /// Remove the calling account and transfer remaining **free** balance. + /// + /// This function never returns. Either the termination was successful and the + /// execution of the destroyed contract is halted. Or it failed during the termination + /// which is considered fatal and results in a trap + rollback. + /// + /// # Parameters + /// + /// - `beneficiary`: The address of the beneficiary account, Should be decodable as an + /// `T::AccountId`. + /// + /// # Traps + /// + /// - The contract is live i.e is already on the call stack. + /// - Failed to send the balance to the beneficiary. + /// - The deletion queue is full. + fn terminate_v1(beneficiary: &[u8]) -> !; + + /// Stores the value transferred along with this call/instantiate into the supplied buffer. + /// The data is encoded as `T::Balance`. + /// + /// If the available space in `output` is less than the size of the value a trap is triggered. + /// + /// # Parameters + /// + /// - `output`: A reference to the output data buffer to write the transferred value. + fn value_transferred(output: &mut &mut [u8]); + + /// Stores the price for the specified amount of gas into the supplied buffer. + /// + /// Equivalent to the newer [`Self::weight_to_fee_v1`] version but + /// works with *ref_time* Weight only. It is recommended to switch to the latest version, once + /// it's stabilized. + fn weight_to_fee(gas: u64, output: &mut &mut [u8]); + + /// Stores the price for the specified amount of gas into the supplied buffer. + /// The data is encoded as `T::Balance`. + /// + /// If the available space in `output` is less than the size of the value a trap is triggered. + /// + /// # Parameters + /// + /// - `ref_time_limit`: The *ref_time* Weight limit to query the price for. + /// - `proof_size_limit`: The *proof_size* Weight limit to query the price for. + /// - `output`: A reference to the output data buffer to write the price. + #[deprecated( + note = "Unstable function. Behaviour can change without further notice. Use only for testing." + )] + fn weight_to_fee_v1(ref_time_limit: u64, proof_size_limit: u64, output: &mut &mut [u8]); + + /// Execute an XCM program locally, using the contract's address as the origin. + /// This is equivalent to dispatching `pallet_xcm::execute` through call_runtime, except that + /// the function is called directly instead of being dispatched. + /// + /// # Parameters + /// + /// - `msg`: The message, should be decodable as a [VersionedXcm](https://paritytech.github.io/polkadot-sdk/master/staging_xcm/enum.VersionedXcm.html), + /// traps otherwise. + /// - `output`: A reference to the output data buffer to write the [Outcome](https://paritytech.github.io/polkadot-sdk/master/staging_xcm/v3/enum.Outcome.html) + /// + /// # Return + /// + /// Returns `Error::Success` when the XCM execution attempt is successful. When the XCM + /// execution fails, `ReturnCode::XcmExecutionFailed` is returned + #[deprecated( + note = "Unstable function. Behaviour can change without further notice. Use only for testing." + )] + fn xcm_execute(msg: &[u8]) -> Result; + + /// Send an XCM program from the contract to the specified destination. + /// This is equivalent to dispatching `pallet_xcm::send` through `call_runtime`, except that + /// the function is called directly instead of being dispatched. + /// + /// # Parameters + /// + /// - `dest`: The XCM destination, should be decodable as [VersionedLocation](https://paritytech.github.io/polkadot-sdk/master/staging_xcm/enum.VersionedLocation.html), + /// traps otherwise. + /// - `msg`: The message, should be decodable as a [VersionedXcm](https://paritytech.github.io/polkadot-sdk/master/staging_xcm/enum.VersionedXcm.html), + /// traps otherwise. + /// + /// # Return + /// + /// Returns `ReturnCode::Success` when the message was successfully sent. When the XCM + /// execution fails, `ReturnErrorCode::XcmSendFailed` is returned. + #[deprecated( + note = "Unstable function. Behaviour can change without further notice. Use only for testing." + )] + fn xcm_send(dest: &[u8], msg: &[u8], output: &mut [u8; 32]) -> Result; +} + +mod private { + pub trait Sealed {} + impl Sealed for super::HostFnImpl {} +} diff --git a/pallets/contracts/uapi/src/host/riscv32.rs b/pallets/contracts/uapi/src/host/riscv32.rs new file mode 100644 index 00000000..35552023 --- /dev/null +++ b/pallets/contracts/uapi/src/host/riscv32.rs @@ -0,0 +1,327 @@ +#![allow(unused_variables, unused_mut)] +// Copyright (C) Parity Technologies (UK) Ltd. +// +// 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. +// TODO: bring up to date with wasm32.rs + +use super::{CallFlags, HostFn, HostFnImpl, Result}; +use crate::ReturnFlags; + +/// A macro to implement all Host functions with a signature of `fn(&mut &mut [u8])`. +/// +/// Example: +/// ```nocompile +// impl_wrapper_for! { +// () => [gas_left], +// (v1) => [gas_left], +// } +// ``` +// +// Expands to: +// ```nocompile +// fn gas_left(output: &mut &mut [u8]) { +// unsafe { sys::gas_left(...); } +// } +// fn gas_left_v1(output: &mut &mut [u8]) { +// unsafe { sys::v1::gas_left(...); } +// } +// ``` +macro_rules! impl_wrapper_for { + (@impl_fn $( $mod:ident )::*, $suffix_sep: literal, $suffix:tt, $name:ident) => { + paste::paste! { + fn [<$name $suffix_sep $suffix>](output: &mut &mut [u8]) { + todo!() + } + } + }; + + () => {}; + + (($mod:ident) => [$( $name:ident),*], $($tail:tt)*) => { + $(impl_wrapper_for!(@impl_fn sys::$mod, "_", $mod, $name);)* + impl_wrapper_for!($($tail)*); + }; + + (() => [$( $name:ident),*], $($tail:tt)*) => { + $(impl_wrapper_for!(@impl_fn sys, "", "", $name);)* + impl_wrapper_for!($($tail)*); + }; +} + +/// A macro to implement all the hash functions Apis. +macro_rules! impl_hash_fn { + ( $name:ident, $bytes_result:literal ) => { + paste::item! { + fn [](input: &[u8], output: &mut [u8; $bytes_result]) { + todo!() + } + } + }; +} + +/// A macro to implement the get_storage functions. +macro_rules! impl_get_storage { + ($fn_name:ident, $sys_get_storage:path) => { + fn $fn_name(key: &[u8], output: &mut &mut [u8]) -> Result { + todo!() + } + }; +} + +impl HostFn for HostFnImpl { + fn instantiate_v1( + code_hash: &[u8], + gas: u64, + value: &[u8], + input: &[u8], + mut address: Option<&mut &mut [u8]>, + mut output: Option<&mut &mut [u8]>, + salt: &[u8], + ) -> Result { + todo!() + } + + fn instantiate_v2( + code_hash: &[u8], + ref_time_limit: u64, + proof_size_limit: u64, + deposit: Option<&[u8]>, + value: &[u8], + input: &[u8], + mut address: Option<&mut &mut [u8]>, + mut output: Option<&mut &mut [u8]>, + salt: &[u8], + ) -> Result { + todo!() + } + + fn call( + callee: &[u8], + gas: u64, + value: &[u8], + input_data: &[u8], + mut output: Option<&mut &mut [u8]>, + ) -> Result { + todo!() + } + + fn call_v1( + flags: CallFlags, + callee: &[u8], + gas: u64, + value: &[u8], + input_data: &[u8], + mut output: Option<&mut &mut [u8]>, + ) -> Result { + todo!() + } + + fn call_v2( + flags: CallFlags, + callee: &[u8], + ref_time_limit: u64, + proof_size_limit: u64, + deposit: Option<&[u8]>, + value: &[u8], + input_data: &[u8], + mut output: Option<&mut &mut [u8]>, + ) -> Result { + todo!() + } + + fn caller_is_root() -> u32 { + todo!() + } + + fn delegate_call( + flags: CallFlags, + code_hash: &[u8], + input: &[u8], + mut output: Option<&mut &mut [u8]>, + ) -> Result { + todo!() + } + + fn transfer(account_id: &[u8], value: &[u8]) -> Result { + todo!() + } + + fn deposit_event(topics: &[u8], data: &[u8]) { + todo!() + } + + fn set_storage(key: &[u8], value: &[u8]) { + todo!() + } + + fn set_storage_v1(key: &[u8], encoded_value: &[u8]) -> Option { + todo!() + } + + fn set_storage_v2(key: &[u8], encoded_value: &[u8]) -> Option { + todo!() + } + + fn set_transient_storage(key: &[u8], encoded_value: &[u8]) -> Option { + todo!() + } + + fn clear_storage(key: &[u8]) { + todo!() + } + + fn clear_storage_v1(key: &[u8]) -> Option { + todo!() + } + + fn clear_transient_storage(key: &[u8]) -> Option { + todo!() + } + + impl_get_storage!(get_storage, sys::get_storage); + impl_get_storage!(get_storage_v1, sys::v1::get_storage); + + fn get_transient_storage(key: &[u8], output: &mut &mut [u8]) -> Result { + todo!() + } + + fn take_storage(key: &[u8], output: &mut &mut [u8]) -> Result { + todo!() + } + + fn take_transient_storage(key: &[u8], output: &mut &mut [u8]) -> Result { + todo!() + } + + fn contains_storage(key: &[u8]) -> Option { + todo!() + } + + fn contains_storage_v1(key: &[u8]) -> Option { + todo!() + } + + fn contains_transient_storage(key: &[u8]) -> Option { + todo!() + } + + fn terminate(beneficiary: &[u8]) -> ! { + todo!() + } + + fn terminate_v1(beneficiary: &[u8]) -> ! { + todo!() + } + + fn call_chain_extension(func_id: u32, input: &[u8], output: Option<&mut &mut [u8]>) -> u32 { + todo!() + } + + fn input(output: &mut &mut [u8]) { + todo!() + } + + fn return_value(flags: ReturnFlags, return_value: &[u8]) -> ! { + todo!() + } + + fn call_runtime(call: &[u8]) -> Result { + todo!() + } + + fn debug_message(str: &[u8]) -> Result { + todo!() + } + + impl_wrapper_for! { + () => [caller, block_number, address, balance, gas_left, value_transferred, now, minimum_balance], + (v1) => [gas_left], + } + + fn weight_to_fee(gas: u64, output: &mut &mut [u8]) { + todo!() + } + + fn weight_to_fee_v1(ref_time_limit: u64, proof_size_limit: u64, output: &mut &mut [u8]) { + todo!() + } + + impl_hash_fn!(sha2_256, 32); + impl_hash_fn!(keccak_256, 32); + impl_hash_fn!(blake2_256, 32); + impl_hash_fn!(blake2_128, 16); + + fn ecdsa_recover( + signature: &[u8; 65], + message_hash: &[u8; 32], + output: &mut [u8; 33], + ) -> Result { + todo!() + } + + fn ecdsa_to_eth_address(pubkey: &[u8; 33], output: &mut [u8; 20]) -> Result { + todo!() + } + + fn sr25519_verify(signature: &[u8; 64], message: &[u8], pub_key: &[u8; 32]) -> Result { + todo!() + } + + fn is_contract(account_id: &[u8]) -> bool { + todo!() + } + + fn caller_is_origin() -> bool { + todo!() + } + + fn set_code_hash(code_hash: &[u8]) -> Result { + todo!() + } + + fn code_hash(account_id: &[u8], output: &mut [u8]) -> Result { + todo!() + } + + fn own_code_hash(output: &mut [u8]) { + todo!() + } + + fn account_reentrance_count(account: &[u8]) -> u32 { + todo!() + } + + fn lock_delegate_dependency(code_hash: &[u8]) { + todo!() + } + + fn unlock_delegate_dependency(code_hash: &[u8]) { + todo!() + } + + fn instantiation_nonce() -> u64 { + todo!() + } + + fn reentrance_count() -> u32 { + todo!() + } + + fn xcm_execute(msg: &[u8]) -> Result { + todo!() + } + + fn xcm_send(dest: &[u8], msg: &[u8], output: &mut [u8; 32]) -> Result { + todo!() + } +} diff --git a/pallets/contracts/uapi/src/host/wasm32.rs b/pallets/contracts/uapi/src/host/wasm32.rs new file mode 100644 index 00000000..55600bc3 --- /dev/null +++ b/pallets/contracts/uapi/src/host/wasm32.rs @@ -0,0 +1,915 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// +// 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 super::{ + extract_from_slice, ptr_len_or_sentinel, ptr_or_sentinel, CallFlags, HostFn, HostFnImpl, Result, +}; +use crate::{ReturnCode, ReturnFlags}; + +mod sys { + use super::ReturnCode; + + #[link(wasm_import_module = "seal0")] + extern "C" { + pub fn account_reentrance_count(account_ptr: *const u8) -> u32; + + pub fn lock_delegate_dependency(code_hash_ptr: *const u8); + + pub fn address(output_ptr: *mut u8, output_len_ptr: *mut u32); + + pub fn balance(output_ptr: *mut u8, output_len_ptr: *mut u32); + + pub fn block_number(output_ptr: *mut u8, output_len_ptr: *mut u32); + + pub fn call( + callee_ptr: *const u8, + callee_len: u32, + gas: u64, + value_ptr: *const u8, + value_len: u32, + input_data_ptr: *const u8, + input_data_len: u32, + output_ptr: *mut u8, + output_len_ptr: *mut u32, + ) -> ReturnCode; + + pub fn call_chain_extension( + func_id: u32, + input_ptr: *const u8, + input_len: u32, + output_ptr: *mut u8, + output_len_ptr: *mut u32, + ) -> ReturnCode; + + pub fn call_runtime(call_ptr: *const u8, call_len: u32) -> ReturnCode; + + pub fn caller(output_ptr: *mut u8, output_len_ptr: *mut u32); + + pub fn caller_is_origin() -> ReturnCode; + + pub fn caller_is_root() -> ReturnCode; + + pub fn clear_storage(key_ptr: *const u8); + + pub fn clear_transient_storage(key_ptr: *const u8, key_len: u32) -> ReturnCode; + + pub fn code_hash( + account_id_ptr: *const u8, + output_ptr: *mut u8, + output_len_ptr: *mut u32, + ) -> ReturnCode; + + pub fn contains_storage(key_ptr: *const u8) -> ReturnCode; + + pub fn contains_transient_storage(key_ptr: *const u8, key_len: u32) -> ReturnCode; + + pub fn debug_message(str_ptr: *const u8, str_len: u32) -> ReturnCode; + + pub fn delegate_call( + flags: u32, + code_hash_ptr: *const u8, + input_data_ptr: *const u8, + input_data_len: u32, + output_ptr: *mut u8, + output_len_ptr: *mut u32, + ) -> ReturnCode; + + pub fn deposit_event( + topics_ptr: *const u8, + topics_len: u32, + data_ptr: *const u8, + data_len: u32, + ); + + pub fn ecdsa_recover( + signature_ptr: *const u8, + message_hash_ptr: *const u8, + output_ptr: *mut u8, + ) -> ReturnCode; + + pub fn ecdsa_to_eth_address(public_key_ptr: *const u8, output_ptr: *mut u8) -> ReturnCode; + + pub fn gas_left(output_ptr: *mut u8, output_len_ptr: *mut u32); + + pub fn get_storage( + key_ptr: *const u8, + out_ptr: *mut u8, + out_len_ptr: *mut u32, + ) -> ReturnCode; + + pub fn get_transient_storage( + key_ptr: *const u8, + key_len: u32, + out_ptr: *mut u8, + out_len_ptr: *mut u32, + ) -> ReturnCode; + + pub fn hash_blake2_128(input_ptr: *const u8, input_len: u32, output_ptr: *mut u8); + + pub fn hash_blake2_256(input_ptr: *const u8, input_len: u32, output_ptr: *mut u8); + + pub fn hash_keccak_256(input_ptr: *const u8, input_len: u32, output_ptr: *mut u8); + + pub fn hash_sha2_256(input_ptr: *const u8, input_len: u32, output_ptr: *mut u8); + + pub fn input(buf_ptr: *mut u8, buf_len_ptr: *mut u32); + + pub fn instantiation_nonce() -> u64; + + pub fn is_contract(account_id_ptr: *const u8) -> ReturnCode; + + pub fn minimum_balance(output_ptr: *mut u8, output_len_ptr: *mut u32); + + pub fn now(output_ptr: *mut u8, output_len_ptr: *mut u32); + + pub fn own_code_hash(output_ptr: *mut u8, output_len_ptr: *mut u32); + + pub fn reentrance_count() -> u32; + + pub fn unlock_delegate_dependency(code_hash_ptr: *const u8); + + pub fn seal_return(flags: u32, data_ptr: *const u8, data_len: u32) -> !; + + pub fn set_code_hash(code_hash_ptr: *const u8) -> ReturnCode; + + pub fn set_storage(key_ptr: *const u8, value_ptr: *const u8, value_len: u32); + + pub fn set_transient_storage( + key_ptr: *const u8, + key_len: u32, + value_ptr: *const u8, + value_len: u32, + ) -> ReturnCode; + + pub fn sr25519_verify( + signature_ptr: *const u8, + public_key_ptr: *const u8, + message_len: u32, + message_ptr: *const u8, + ) -> ReturnCode; + + pub fn take_storage( + key_ptr: *const u8, + key_len: u32, + out_ptr: *mut u8, + out_len_ptr: *mut u32, + ) -> ReturnCode; + + pub fn take_transient_storage( + key_ptr: *const u8, + key_len: u32, + out_ptr: *mut u8, + out_len_ptr: *mut u32, + ) -> ReturnCode; + + pub fn terminate(beneficiary_ptr: *const u8) -> !; + + pub fn transfer( + account_id_ptr: *const u8, + account_id_len: u32, + transferred_value_ptr: *const u8, + transferred_value_len: u32, + ) -> ReturnCode; + + pub fn value_transferred(output_ptr: *mut u8, output_len_ptr: *mut u32); + + pub fn weight_to_fee(gas: u64, output_ptr: *mut u8, output_len_ptr: *mut u32); + + pub fn xcm_execute(msg_ptr: *const u8, msg_len: u32) -> ReturnCode; + + pub fn xcm_send( + dest_ptr: *const u8, + msg_ptr: *const u8, + msg_len: u32, + output_ptr: *mut u8, + ) -> ReturnCode; + } + + pub mod v1 { + use crate::ReturnCode; + + #[link(wasm_import_module = "seal1")] + extern "C" { + pub fn call( + flags: u32, + callee_ptr: *const u8, + gas: u64, + transferred_value_ptr: *const u8, + input_data_ptr: *const u8, + input_data_len: u32, + output_ptr: *mut u8, + output_len_ptr: *mut u32, + ) -> ReturnCode; + + pub fn clear_storage(key_ptr: *const u8, key_len: u32) -> ReturnCode; + + pub fn contains_storage(key_ptr: *const u8, key_len: u32) -> ReturnCode; + + pub fn gas_left(output_ptr: *mut u8, output_len_ptr: *mut u32); + + pub fn get_storage( + key_ptr: *const u8, + key_len: u32, + out_ptr: *mut u8, + out_len_ptr: *mut u32, + ) -> ReturnCode; + + pub fn instantiate( + code_hash_ptr: *const u8, + gas: u64, + value_ptr: *const u8, + input_ptr: *const u8, + input_len: u32, + address_ptr: *mut u8, + address_len_ptr: *mut u32, + output_ptr: *mut u8, + output_len_ptr: *mut u32, + salt_ptr: *const u8, + salt_len: u32, + ) -> ReturnCode; + + pub fn set_storage( + key_ptr: *const u8, + value_ptr: *const u8, + value_len: u32, + ) -> ReturnCode; + + pub fn terminate(beneficiary_ptr: *const u8) -> !; + + pub fn weight_to_fee( + ref_time_limit: u64, + proof_size_limit: u64, + output_ptr: *mut u8, + output_len_ptr: *mut u32, + ); + } + } + + pub mod v2 { + use crate::ReturnCode; + + #[link(wasm_import_module = "seal2")] + extern "C" { + pub fn call( + flags: u32, + callee_ptr: *const u8, + ref_time_limit: u64, + proof_size_limit: u64, + deposit_ptr: *const u8, + transferred_value_ptr: *const u8, + input_data_ptr: *const u8, + input_data_len: u32, + output_ptr: *mut u8, + output_len_ptr: *mut u32, + ) -> ReturnCode; + + pub fn instantiate( + code_hash_ptr: *const u8, + ref_time_limit: u64, + proof_size_limit: u64, + deposit_ptr: *const u8, + value_ptr: *const u8, + input_ptr: *const u8, + input_len: u32, + address_ptr: *mut u8, + address_len_ptr: *mut u32, + output_ptr: *mut u8, + output_len_ptr: *mut u32, + salt_ptr: *const u8, + salt_len: u32, + ) -> ReturnCode; + + pub fn set_storage( + key_ptr: *const u8, + key_len: u32, + value_ptr: *const u8, + value_len: u32, + ) -> ReturnCode; + } + } +} + +/// A macro to implement all Host functions with a signature of `fn(&mut &mut [u8])`. +/// +/// Example: +/// ```nocompile +// impl_wrapper_for! { +// () => [gas_left], +// (v1) => [gas_left], +// } +// ``` +// +// Expands to: +// ```nocompile +// fn gas_left(output: &mut &mut [u8]) { +// unsafe { sys::gas_left(...); } +// } +// fn gas_left_v1(output: &mut &mut [u8]) { +// unsafe { sys::v1::gas_left(...); } +// } +// ``` +macro_rules! impl_wrapper_for { + (@impl_fn $( $mod:ident )::*, $suffix_sep: literal, $suffix:tt, $name:ident) => { + paste::paste! { + fn [<$name $suffix_sep $suffix>](output: &mut &mut [u8]) { + let mut output_len = output.len() as u32; + unsafe { + $( $mod )::*::$name(output.as_mut_ptr(), &mut output_len); + } + extract_from_slice(output, output_len as usize) + } + } + }; + + () => {}; + + (($mod:ident) => [$( $name:ident),*], $($tail:tt)*) => { + $(impl_wrapper_for!(@impl_fn sys::$mod, "_", $mod, $name);)* + impl_wrapper_for!($($tail)*); + }; + + (() => [$( $name:ident),*], $($tail:tt)*) => { + $(impl_wrapper_for!(@impl_fn sys, "", "", $name);)* + impl_wrapper_for!($($tail)*); + }; +} + +/// A macro to implement all the hash functions Apis. +macro_rules! impl_hash_fn { + ( $name:ident, $bytes_result:literal ) => { + paste::item! { + fn [](input: &[u8], output: &mut [u8; $bytes_result]) { + unsafe { + sys::[]( + input.as_ptr(), + input.len() as u32, + output.as_mut_ptr(), + ) + } + } + } + }; +} + +impl HostFn for HostFnImpl { + #[inline(always)] + fn instantiate_v1( + code_hash: &[u8], + gas: u64, + value: &[u8], + input: &[u8], + mut address: Option<&mut &mut [u8]>, + mut output: Option<&mut &mut [u8]>, + salt: &[u8], + ) -> Result { + let (address_ptr, mut address_len) = ptr_len_or_sentinel(&mut address); + let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output); + let ret_code = unsafe { + sys::v1::instantiate( + code_hash.as_ptr(), + gas, + value.as_ptr(), + input.as_ptr(), + input.len() as u32, + address_ptr, + &mut address_len, + output_ptr, + &mut output_len, + salt.as_ptr(), + salt.len() as u32, + ) + }; + + if let Some(ref mut address) = address { + extract_from_slice(address, address_len as usize); + } + if let Some(ref mut output) = output { + extract_from_slice(output, output_len as usize); + } + ret_code.into() + } + + fn instantiate_v2( + code_hash: &[u8], + ref_time_limit: u64, + proof_size_limit: u64, + deposit: Option<&[u8]>, + value: &[u8], + input: &[u8], + mut address: Option<&mut &mut [u8]>, + mut output: Option<&mut &mut [u8]>, + salt: &[u8], + ) -> Result { + let (address_ptr, mut address_len) = ptr_len_or_sentinel(&mut address); + let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output); + let deposit_ptr = ptr_or_sentinel(&deposit); + + let ret_code = { + unsafe { + sys::v2::instantiate( + code_hash.as_ptr(), + ref_time_limit, + proof_size_limit, + deposit_ptr, + value.as_ptr(), + input.as_ptr(), + input.len() as u32, + address_ptr, + &mut address_len, + output_ptr, + &mut output_len, + salt.as_ptr(), + salt.len() as u32, + ) + } + }; + + if let Some(ref mut address) = address { + extract_from_slice(address, address_len as usize); + } + + if let Some(ref mut output) = output { + extract_from_slice(output, output_len as usize); + } + + ret_code.into() + } + + #[inline(always)] + fn call( + callee: &[u8], + gas: u64, + value: &[u8], + input_data: &[u8], + mut output: Option<&mut &mut [u8]>, + ) -> Result { + let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output); + let ret_code = { + unsafe { + sys::call( + callee.as_ptr(), + callee.len() as u32, + gas, + value.as_ptr(), + value.len() as u32, + input_data.as_ptr(), + input_data.len() as u32, + output_ptr, + &mut output_len, + ) + } + }; + + if let Some(ref mut output) = output { + extract_from_slice(output, output_len as usize); + } + + ret_code.into() + } + + #[inline(always)] + fn call_v1( + flags: CallFlags, + callee: &[u8], + gas: u64, + value: &[u8], + input_data: &[u8], + mut output: Option<&mut &mut [u8]>, + ) -> Result { + let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output); + let ret_code = { + unsafe { + sys::v1::call( + flags.bits(), + callee.as_ptr(), + gas, + value.as_ptr(), + input_data.as_ptr(), + input_data.len() as u32, + output_ptr, + &mut output_len, + ) + } + }; + + if let Some(ref mut output) = output { + extract_from_slice(output, output_len as usize); + } + + ret_code.into() + } + + fn call_v2( + flags: CallFlags, + callee: &[u8], + ref_time_limit: u64, + proof_size_limit: u64, + deposit: Option<&[u8]>, + value: &[u8], + input_data: &[u8], + mut output: Option<&mut &mut [u8]>, + ) -> Result { + let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output); + let deposit_ptr = ptr_or_sentinel(&deposit); + let ret_code = { + unsafe { + sys::v2::call( + flags.bits(), + callee.as_ptr(), + ref_time_limit, + proof_size_limit, + deposit_ptr, + value.as_ptr(), + input_data.as_ptr(), + input_data.len() as u32, + output_ptr, + &mut output_len, + ) + } + }; + + if let Some(ref mut output) = output { + extract_from_slice(output, output_len as usize); + } + + ret_code.into() + } + + fn caller_is_root() -> u32 { + unsafe { sys::caller_is_root() }.into_u32() + } + + #[inline(always)] + fn delegate_call( + flags: CallFlags, + code_hash: &[u8], + input: &[u8], + mut output: Option<&mut &mut [u8]>, + ) -> Result { + let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output); + let ret_code = { + unsafe { + sys::delegate_call( + flags.bits(), + code_hash.as_ptr(), + input.as_ptr(), + input.len() as u32, + output_ptr, + &mut output_len, + ) + } + }; + + if let Some(ref mut output) = output { + extract_from_slice(output, output_len as usize); + } + + ret_code.into() + } + + fn transfer(account_id: &[u8], value: &[u8]) -> Result { + let ret_code = unsafe { + sys::transfer( + account_id.as_ptr(), + account_id.len() as u32, + value.as_ptr(), + value.len() as u32, + ) + }; + ret_code.into() + } + + fn deposit_event(topics: &[u8], data: &[u8]) { + unsafe { + sys::deposit_event( + topics.as_ptr(), + topics.len() as u32, + data.as_ptr(), + data.len() as u32, + ) + } + } + + fn set_storage(key: &[u8], value: &[u8]) { + unsafe { sys::set_storage(key.as_ptr(), value.as_ptr(), value.len() as u32) }; + } + + fn set_storage_v1(key: &[u8], encoded_value: &[u8]) -> Option { + let ret_code = unsafe { + sys::v1::set_storage(key.as_ptr(), encoded_value.as_ptr(), encoded_value.len() as u32) + }; + ret_code.into() + } + + fn set_storage_v2(key: &[u8], encoded_value: &[u8]) -> Option { + let ret_code = unsafe { + sys::v2::set_storage( + key.as_ptr(), + key.len() as u32, + encoded_value.as_ptr(), + encoded_value.len() as u32, + ) + }; + ret_code.into() + } + + fn set_transient_storage(key: &[u8], encoded_value: &[u8]) -> Option { + let ret_code = unsafe { + sys::set_transient_storage( + key.as_ptr(), + key.len() as u32, + encoded_value.as_ptr(), + encoded_value.len() as u32, + ) + }; + ret_code.into() + } + + fn clear_storage(key: &[u8]) { + unsafe { sys::clear_storage(key.as_ptr()) }; + } + + fn clear_storage_v1(key: &[u8]) -> Option { + let ret_code = unsafe { sys::v1::clear_storage(key.as_ptr(), key.len() as u32) }; + ret_code.into() + } + + fn clear_transient_storage(key: &[u8]) -> Option { + let ret_code = unsafe { sys::clear_transient_storage(key.as_ptr(), key.len() as u32) }; + ret_code.into() + } + + #[inline(always)] + fn get_storage(key: &[u8], output: &mut &mut [u8]) -> Result { + let mut output_len = output.len() as u32; + let ret_code = + { unsafe { sys::get_storage(key.as_ptr(), output.as_mut_ptr(), &mut output_len) } }; + extract_from_slice(output, output_len as usize); + ret_code.into() + } + + #[inline(always)] + fn get_storage_v1(key: &[u8], output: &mut &mut [u8]) -> Result { + let mut output_len = output.len() as u32; + let ret_code = { + unsafe { + sys::v1::get_storage( + key.as_ptr(), + key.len() as u32, + output.as_mut_ptr(), + &mut output_len, + ) + } + }; + extract_from_slice(output, output_len as usize); + ret_code.into() + } + + #[inline(always)] + fn get_transient_storage(key: &[u8], output: &mut &mut [u8]) -> Result { + let mut output_len = output.len() as u32; + let ret_code = { + unsafe { + sys::get_transient_storage( + key.as_ptr(), + key.len() as u32, + output.as_mut_ptr(), + &mut output_len, + ) + } + }; + extract_from_slice(output, output_len as usize); + ret_code.into() + } + + #[inline(always)] + fn take_storage(key: &[u8], output: &mut &mut [u8]) -> Result { + let mut output_len = output.len() as u32; + let ret_code = { + unsafe { + sys::take_storage( + key.as_ptr(), + key.len() as u32, + output.as_mut_ptr(), + &mut output_len, + ) + } + }; + extract_from_slice(output, output_len as usize); + ret_code.into() + } + + #[inline(always)] + fn take_transient_storage(key: &[u8], output: &mut &mut [u8]) -> Result { + let mut output_len = output.len() as u32; + let ret_code = { + unsafe { + sys::take_transient_storage( + key.as_ptr(), + key.len() as u32, + output.as_mut_ptr(), + &mut output_len, + ) + } + }; + extract_from_slice(output, output_len as usize); + ret_code.into() + } + + fn debug_message(str: &[u8]) -> Result { + let ret_code = unsafe { sys::debug_message(str.as_ptr(), str.len() as u32) }; + ret_code.into() + } + + fn contains_storage(key: &[u8]) -> Option { + let ret_code = unsafe { sys::contains_storage(key.as_ptr()) }; + ret_code.into() + } + + fn contains_storage_v1(key: &[u8]) -> Option { + let ret_code = unsafe { sys::v1::contains_storage(key.as_ptr(), key.len() as u32) }; + ret_code.into() + } + + fn contains_transient_storage(key: &[u8]) -> Option { + let ret_code = unsafe { sys::contains_transient_storage(key.as_ptr(), key.len() as u32) }; + ret_code.into() + } + + fn terminate(beneficiary: &[u8]) -> ! { + unsafe { sys::terminate(beneficiary.as_ptr()) } + } + + fn terminate_v1(beneficiary: &[u8]) -> ! { + unsafe { sys::v1::terminate(beneficiary.as_ptr()) } + } + + fn call_chain_extension(func_id: u32, input: &[u8], mut output: Option<&mut &mut [u8]>) -> u32 { + let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output); + let ret_code = { + unsafe { + sys::call_chain_extension( + func_id, + input.as_ptr(), + input.len() as u32, + output_ptr, + &mut output_len, + ) + } + }; + + if let Some(ref mut output) = output { + extract_from_slice(output, output_len as usize); + } + ret_code.into_u32() + } + + #[inline(always)] + fn input(output: &mut &mut [u8]) { + let mut output_len = output.len() as u32; + { + unsafe { sys::input(output.as_mut_ptr(), &mut output_len) }; + } + extract_from_slice(output, output_len as usize); + } + + fn return_value(flags: ReturnFlags, return_value: &[u8]) -> ! { + unsafe { sys::seal_return(flags.bits(), return_value.as_ptr(), return_value.len() as u32) } + } + + fn call_runtime(call: &[u8]) -> Result { + let ret_code = unsafe { sys::call_runtime(call.as_ptr(), call.len() as u32) }; + ret_code.into() + } + + impl_wrapper_for! { + () => [caller, block_number, address, balance, gas_left, value_transferred, now, minimum_balance], + (v1) => [gas_left], + } + + #[inline(always)] + fn weight_to_fee(gas: u64, output: &mut &mut [u8]) { + let mut output_len = output.len() as u32; + { + unsafe { sys::weight_to_fee(gas, output.as_mut_ptr(), &mut output_len) }; + } + extract_from_slice(output, output_len as usize); + } + + fn weight_to_fee_v1(ref_time_limit: u64, proof_size_limit: u64, output: &mut &mut [u8]) { + let mut output_len = output.len() as u32; + { + unsafe { + sys::v1::weight_to_fee( + ref_time_limit, + proof_size_limit, + output.as_mut_ptr(), + &mut output_len, + ) + }; + } + extract_from_slice(output, output_len as usize); + } + + impl_hash_fn!(sha2_256, 32); + impl_hash_fn!(keccak_256, 32); + impl_hash_fn!(blake2_256, 32); + impl_hash_fn!(blake2_128, 16); + + fn ecdsa_recover( + signature: &[u8; 65], + message_hash: &[u8; 32], + output: &mut [u8; 33], + ) -> Result { + let ret_code = unsafe { + sys::ecdsa_recover(signature.as_ptr(), message_hash.as_ptr(), output.as_mut_ptr()) + }; + ret_code.into() + } + + fn ecdsa_to_eth_address(pubkey: &[u8; 33], output: &mut [u8; 20]) -> Result { + let ret_code = unsafe { sys::ecdsa_to_eth_address(pubkey.as_ptr(), output.as_mut_ptr()) }; + ret_code.into() + } + + fn sr25519_verify(signature: &[u8; 64], message: &[u8], pub_key: &[u8; 32]) -> Result { + let ret_code = unsafe { + sys::sr25519_verify( + signature.as_ptr(), + pub_key.as_ptr(), + message.len() as u32, + message.as_ptr(), + ) + }; + ret_code.into() + } + + fn is_contract(account_id: &[u8]) -> bool { + let ret_val = unsafe { sys::is_contract(account_id.as_ptr()) }; + ret_val.into_bool() + } + + fn caller_is_origin() -> bool { + let ret_val = unsafe { sys::caller_is_origin() }; + ret_val.into_bool() + } + + fn set_code_hash(code_hash: &[u8]) -> Result { + let ret_val = unsafe { sys::set_code_hash(code_hash.as_ptr()) }; + ret_val.into() + } + + fn code_hash(account_id: &[u8], output: &mut [u8]) -> Result { + let mut output_len = output.len() as u32; + let ret_val = + unsafe { sys::code_hash(account_id.as_ptr(), output.as_mut_ptr(), &mut output_len) }; + ret_val.into() + } + + fn own_code_hash(output: &mut [u8]) { + let mut output_len = output.len() as u32; + unsafe { sys::own_code_hash(output.as_mut_ptr(), &mut output_len) } + } + + fn account_reentrance_count(account: &[u8]) -> u32 { + unsafe { sys::account_reentrance_count(account.as_ptr()) } + } + + fn lock_delegate_dependency(code_hash: &[u8]) { + unsafe { sys::lock_delegate_dependency(code_hash.as_ptr()) } + } + + fn unlock_delegate_dependency(code_hash: &[u8]) { + unsafe { sys::unlock_delegate_dependency(code_hash.as_ptr()) } + } + + fn instantiation_nonce() -> u64 { + unsafe { sys::instantiation_nonce() } + } + + fn reentrance_count() -> u32 { + unsafe { sys::reentrance_count() } + } + + fn xcm_execute(msg: &[u8]) -> Result { + let ret_code = unsafe { sys::xcm_execute(msg.as_ptr(), msg.len() as _) }; + ret_code.into() + } + + fn xcm_send(dest: &[u8], msg: &[u8], output: &mut [u8; 32]) -> Result { + let ret_code = unsafe { + sys::xcm_send(dest.as_ptr(), msg.as_ptr(), msg.len() as _, output.as_mut_ptr()) + }; + ret_code.into() + } +} diff --git a/pallets/contracts/uapi/src/lib.rs b/pallets/contracts/uapi/src/lib.rs new file mode 100644 index 00000000..83877c6e --- /dev/null +++ b/pallets/contracts/uapi/src/lib.rs @@ -0,0 +1,138 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// +// 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. + +//! External C API to communicate with substrate contracts runtime module. +//! +//! Refer to substrate FRAME contract module for more documentation. + +#![no_std] + +mod flags; +pub use flags::*; + +#[cfg(any(target_arch = "wasm32", target_arch = "riscv32"))] +mod host; + +#[cfg(any(target_arch = "wasm32", target_arch = "riscv32"))] +pub use host::*; + +macro_rules! define_error_codes { + ( + $( + $( #[$attr:meta] )* + $name:ident = $discr:literal, + )* + ) => { + /// Every error that can be returned to a contract when it calls any of the host functions. + #[derive(Debug, PartialEq, Eq)] + #[repr(u8)] + pub enum ReturnErrorCode { + /// API call successful. + Success = 0, + $( + $( #[$attr] )* + $name = $discr, + )* + /// Returns if an unknown error was received from the host module. + Unknown, + } + + impl From for Result { + fn from(return_code: ReturnCode) -> Self { + match return_code.0 { + 0 => Ok(()), + $( + $discr => Err(ReturnErrorCode::$name), + )* + _ => Err(ReturnErrorCode::Unknown), + } + } + } + }; +} + +impl From for u32 { + fn from(code: ReturnErrorCode) -> u32 { + code as u32 + } +} + +define_error_codes! { + /// The called function trapped and has its state changes reverted. + /// In this case no output buffer is returned. + /// Can only be returned from `call` and `instantiate`. + CalleeTrapped = 1, + /// The called function ran to completion but decided to revert its state. + /// An output buffer is returned when one was supplied. + /// Can only be returned from `call` and `instantiate`. + CalleeReverted = 2, + /// The passed key does not exist in storage. + KeyNotFound = 3, + /// Deprecated and no longer returned: There is only the minimum balance. + _BelowSubsistenceThreshold = 4, + /// Transfer failed for other not further specified reason. Most probably + /// reserved or locked balance of the sender that was preventing the transfer. + TransferFailed = 5, + /// Deprecated and no longer returned: Endowment is no longer required. + _EndowmentTooLow = 6, + /// No code could be found at the supplied code hash. + CodeNotFound = 7, + /// The account that was called is no contract. + NotCallable = 8, + /// The call to `debug_message` had no effect because debug message + /// recording was disabled. + LoggingDisabled = 9, + /// The call dispatched by `call_runtime` was executed but returned an error. + CallRuntimeFailed = 10, + /// ECDSA public key recovery failed. Most probably wrong recovery id or signature. + EcdsaRecoveryFailed = 11, + /// sr25519 signature verification failed. + Sr25519VerifyFailed = 12, + /// The `xcm_execute` call failed. + XcmExecutionFailed = 13, + /// The `xcm_send` call failed. + XcmSendFailed = 14, +} + +/// The raw return code returned by the host side. +#[repr(transparent)] +pub struct ReturnCode(u32); + +/// Used as a sentinel value when reading and writing contract memory. +/// +/// We use this value to signal `None` to a contract when only a primitive is +/// allowed and we don't want to go through encoding a full Rust type. +/// Using `u32::Max` is a safe sentinel because contracts are never +/// allowed to use such a large amount of resources. So this value doesn't +/// make sense for a memory location or length. +const SENTINEL: u32 = u32::MAX; + +impl From for Option { + fn from(code: ReturnCode) -> Self { + (code.0 < SENTINEL).then_some(code.0) + } +} + +impl ReturnCode { + /// Returns the raw underlying `u32` representation. + pub fn into_u32(self) -> u32 { + self.0 + } + /// Returns the underlying `u32` converted into `bool`. + pub fn into_bool(self) -> bool { + self.0.ne(&0) + } +} + +type Result = core::result::Result<(), ReturnErrorCode>; From cf27913d0a0182d97ecb8e7ca05e9fe90bb92d81 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Fri, 23 Aug 2024 17:06:17 +0700 Subject: [PATCH 74/76] fix: toml file --- Cargo.lock | 8 ++++---- Cargo.toml | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9f67e3bf..74d037ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6511,8 +6511,8 @@ name = "pallet-api" version = "0.1.0" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "log", "pallet-assets", "pallet-balances", @@ -9822,8 +9822,8 @@ dependencies = [ name = "pop-chain-extension" version = "0.1.0" dependencies = [ - "frame-support", - "frame-system", + "frame-support 29.0.2", + "frame-system 29.0.0", "impl-trait-for-tuples", "log", "pallet-contracts 28.0.0", diff --git a/Cargo.toml b/Cargo.toml index 3c50197b..8cc27842 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,7 +54,6 @@ array-bytes = { version = "6.2.2", default-features = false } assert_matches = { version = "1.5.0" } bitflags = { version = "1.3.2" } environmental = { version = "1.1.4", default-features = false } -impl-trait-for-tuples = { version = "0.2.2" } pallet-revive = { path = "pallets/contracts", default-features = false } pallet-contracts-fixtures = { path = "pallets/contracts/fixtures", default-features = false } pallet-contracts-mock-network = { default-features = false, path = "pallets/contracts/mock-network" } From faa6ea86fa519a4dd96a444be9177c71f946b142 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Fri, 23 Aug 2024 17:45:23 +0700 Subject: [PATCH 75/76] fix: merge main --- Cargo.lock | 2119 +++++++++++++++++------------ Cargo.toml | 98 +- extension/src/decoding.rs | 6 +- extension/src/functions.rs | 6 +- extension/src/lib.rs | 11 +- runtime/devnet/src/lib.rs | 4 + runtime/testnet/src/extensions.rs | 1 - 7 files changed, 1349 insertions(+), 896 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6290e4a6..8ee9d319 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -274,7 +274,7 @@ dependencies = [ "num-bigint", "num-traits", "paste", - "rustc_version", + "rustc_version 0.4.0", "zeroize", ] @@ -480,8 +480,8 @@ dependencies = [ "frame-benchmarking", "frame-executive", "frame-metadata-hash-extension", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", @@ -520,20 +520,20 @@ dependencies = [ "scale-info", "serde_json", "snowbridge-router-primitives", - "sp-api", + "sp-api 33.0.0", "sp-block-builder", "sp-consensus-aura", "sp-core", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-genesis-builder 0.14.0", + "sp-inherents 33.0.0", + "sp-io 37.0.0", "sp-offchain", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", "sp-std", "sp-storage", "sp-transaction-pool", - "sp-version", + "sp-version 36.0.0", "sp-weights", "staging-parachain-info", "staging-xcm", @@ -553,8 +553,8 @@ dependencies = [ "cumulus-pallet-parachain-system", "cumulus-pallet-xcmp-queue", "cumulus-primitives-core", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "pallet-assets", "pallet-balances", "pallet-collator-selection", @@ -565,8 +565,8 @@ dependencies = [ "parachains-common", "parachains-runtimes-test-utils", "parity-scale-codec", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "staging-parachain-info", "staging-xcm", @@ -582,7 +582,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4e2360c96927aa33b3fef7190eabf2aa4129fe3505c11dfa860ada0f27fd1b1" dependencies = [ "cumulus-primitives-core", - "frame-support", + "frame-support 36.0.0", "impl-trait-for-tuples", "log", "pallet-asset-conversion", @@ -590,8 +590,8 @@ dependencies = [ "parachains-common", "parity-scale-codec", "scale-info", - "sp-api", - "sp-runtime", + "sp-api 33.0.0", + "sp-runtime 38.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -1064,18 +1064,6 @@ dependencies = [ "piper", ] -[[package]] -name = "bounded-collections" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca548b6163b872067dc5eb82fd130c56881435e30367d2073594a3d9744120dd" -dependencies = [ - "log", - "parity-scale-codec", - "scale-info", - "serde", -] - [[package]] name = "bounded-collections" version = "0.2.0" @@ -1103,7 +1091,7 @@ version = "1.0.0" source = "git+https://github.com/polkadot-fellows/runtimes#f42acab60edf4d6ded4d9e99b1a8fbacded85669" dependencies = [ "bp-xcm-bridge-hub-router", - "frame-support", + "frame-support 36.0.0", "parity-scale-codec", "scale-info", "sp-std", @@ -1117,7 +1105,7 @@ version = "1.0.0" source = "git+https://github.com/polkadot-fellows/runtimes#f42acab60edf4d6ded4d9e99b1a8fbacded85669" dependencies = [ "bp-xcm-bridge-hub-router", - "frame-support", + "frame-support 36.0.0", "parity-scale-codec", "scale-info", "sp-std", @@ -1134,10 +1122,10 @@ dependencies = [ "bp-messages", "bp-polkadot-core", "bp-runtime", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "polkadot-primitives", - "sp-api", + "sp-api 33.0.0", "sp-std", ] @@ -1149,11 +1137,11 @@ dependencies = [ "bp-bridge-hub-cumulus", "bp-messages", "bp-runtime", - "frame-support", + "frame-support 36.0.0", "kusama-runtime-constants", "polkadot-runtime-constants", - "sp-api", - "sp-runtime", + "sp-api 33.0.0", + "sp-runtime 38.0.0", "sp-std", "system-parachains-constants", ] @@ -1167,12 +1155,12 @@ dependencies = [ "bp-messages", "bp-polkadot-bulletin", "bp-runtime", - "frame-support", + "frame-support 36.0.0", "kusama-runtime-constants", "polkadot-runtime-constants", "snowbridge-core", - "sp-api", - "sp-runtime", + "sp-api 33.0.0", + "sp-runtime 38.0.0", "sp-std", "staging-xcm", "system-parachains-constants", @@ -1186,13 +1174,13 @@ checksum = "57cac4b71008e46d43e346476ed1be85cf7b505efacee17dad84d687344bf1b1" dependencies = [ "bp-runtime", "finality-grandpa", - "frame-support", + "frame-support 36.0.0", "parity-scale-codec", "scale-info", "serde", "sp-consensus-grandpa", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -1204,7 +1192,7 @@ checksum = "f97eec00a98efeb052ac9fc9676d9fccf5acd19e3b18530f3d72af1a1faf21ec" dependencies = [ "bp-header-chain", "bp-runtime", - "frame-support", + "frame-support 36.0.0", "parity-scale-codec", "scale-info", "serde", @@ -1221,12 +1209,12 @@ dependencies = [ "bp-header-chain", "bp-polkadot-core", "bp-runtime", - "frame-support", + "frame-support 36.0.0", "impl-trait-for-tuples", "parity-scale-codec", "scale-info", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -1240,12 +1228,12 @@ dependencies = [ "bp-messages", "bp-polkadot-core", "bp-runtime", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", - "sp-api", - "sp-runtime", + "sp-api 33.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -1257,14 +1245,14 @@ checksum = "6ef2272823ecfee580c00f6542dfcab3ec7abdb00857af853429736847c3a2d9" dependencies = [ "bp-messages", "bp-runtime", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "parity-util-mem", "scale-info", "serde", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -1276,10 +1264,10 @@ checksum = "5a589f5bb70baa4377a798823be752042aa6c220d51afc559716667e29b0203d" dependencies = [ "bp-messages", "bp-runtime", - "frame-support", + "frame-support 36.0.0", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -1289,8 +1277,8 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904644c23b437dde65741f3148067624ed0b4d8360f68adf9e92273aeb970814" dependencies = [ - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "hash-db", "impl-trait-for-tuples", "log", @@ -1299,11 +1287,11 @@ dependencies = [ "scale-info", "serde", "sp-core", - "sp-io", - "sp-runtime", - "sp-state-machine", + "sp-io 37.0.0", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "sp-std", - "sp-trie", + "sp-trie 36.0.0", "trie-db", ] @@ -1320,12 +1308,12 @@ dependencies = [ "ed25519-dalek", "finality-grandpa", "parity-scale-codec", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-consensus-grandpa", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", - "sp-trie", + "sp-trie 36.0.0", ] [[package]] @@ -1346,7 +1334,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", ] [[package]] @@ -1363,8 +1351,8 @@ dependencies = [ "bp-runtime", "bp-xcm-bridge-hub", "bp-xcm-bridge-hub-router", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "hash-db", "log", "pallet-bridge-grandpa", @@ -1375,12 +1363,12 @@ dependencies = [ "pallet-utility", "parity-scale-codec", "scale-info", - "sp-api", + "sp-api 33.0.0", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", - "sp-trie", + "sp-trie 36.0.0", "staging-xcm", "staging-xcm-builder", "tuplex", @@ -2125,7 +2113,7 @@ dependencies = [ "sc-service", "sp-blockchain", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "url", ] @@ -2146,10 +2134,10 @@ dependencies = [ "polkadot-overseer", "polkadot-primitives", "sc-client-api", - "sp-api", + "sp-api 33.0.0", "sp-consensus", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "tracing", ] @@ -2181,17 +2169,17 @@ dependencies = [ "sc-consensus-slots", "sc-telemetry", "schnellru", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-block-builder", "sp-blockchain", "sp-consensus", "sp-consensus-aura", "sp-core", - "sp-inherents", + "sp-inherents 33.0.0", "sp-keystore", - "sp-runtime", - "sp-state-machine", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "sp-timestamp", "substrate-prometheus-endpoint", "tracing", @@ -2220,10 +2208,10 @@ dependencies = [ "sp-consensus", "sp-consensus-slots", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-timestamp", - "sp-trie", - "sp-version", + "sp-trie 36.0.0", + "sp-version 36.0.0", "substrate-prometheus-endpoint", "tracing", ] @@ -2238,9 +2226,9 @@ dependencies = [ "async-trait", "cumulus-primitives-parachain-inherent", "sp-consensus", - "sp-inherents", - "sp-runtime", - "sp-state-machine", + "sp-inherents 33.0.0", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "thiserror", ] @@ -2261,13 +2249,13 @@ dependencies = [ "polkadot-parachain-primitives", "polkadot-primitives", "sc-client-api", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-consensus", "sp-core", - "sp-runtime", - "sp-state-machine", - "sp-version", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", + "sp-version 36.0.0", "tracing", ] @@ -2285,14 +2273,14 @@ dependencies = [ "parity-scale-codec", "sc-client-api", "scale-info", - "sp-api", + "sp-api 33.0.0", "sp-crypto-hashing", - "sp-inherents", - "sp-runtime", - "sp-state-machine", + "sp-inherents 33.0.0", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "sp-std", "sp-storage", - "sp-trie", + "sp-trie 36.0.0", "tracing", ] @@ -2315,11 +2303,11 @@ dependencies = [ "rand", "sc-client-api", "sc-consensus", - "sp-api", + "sp-api 33.0.0", "sp-consensus", "sp-maybe-compressed-blob", - "sp-runtime", - "sp-version", + "sp-runtime 38.0.0", + "sp-version 36.0.0", "tracing", ] @@ -2352,12 +2340,12 @@ dependencies = [ "sc-telemetry", "sc-transaction-pool", "sc-utils", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-consensus", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-transaction-pool", ] @@ -2368,15 +2356,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c5e8af48090936c45483d489ee681acb54277763586b53fa3dbd17173aa474fc" dependencies = [ "cumulus-pallet-parachain-system", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "pallet-aura", "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-consensus-aura", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -2393,8 +2381,8 @@ dependencies = [ "cumulus-primitives-proof-size-hostfunction", "environmental", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "impl-trait-for-tuples", "log", "pallet-message-queue", @@ -2405,13 +2393,13 @@ dependencies = [ "scale-info", "sp-core", "sp-externalities", - "sp-inherents", - "sp-io", - "sp-runtime", - "sp-state-machine", + "sp-inherents 33.0.0", + "sp-io 37.0.0", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "sp-std", - "sp-trie", - "sp-version", + "sp-trie 36.0.0", + "sp-version 36.0.0", "staging-xcm", "staging-xcm-builder", "trie-db", @@ -2436,11 +2424,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "506daacefa861aa2909b64f26e76495ce029227fd8355b97e074cc1d5dc54ab2" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "pallet-session", "parity-scale-codec", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -2451,12 +2439,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d5224285f60e5159bab549f458079d606a7f95ef779def8b89f1a244dc7cf81" dependencies = [ "cumulus-primitives-core", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "staging-xcm", ] @@ -2467,12 +2455,12 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0adf5409618b21e754fef0ac70f257878d22d61c48fdeefcab666835dcb8e0f0" dependencies = [ - "bounded-collections 0.2.0", + "bounded-collections", "bp-xcm-bridge-hub-router", "cumulus-primitives-core", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-message-queue", "parity-scale-codec", @@ -2480,8 +2468,8 @@ dependencies = [ "polkadot-runtime-parachains", "scale-info", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -2497,9 +2485,9 @@ dependencies = [ "parity-scale-codec", "polkadot-core-primitives", "polkadot-primitives", - "sp-api", + "sp-api 33.0.0", "sp-consensus-aura", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -2514,10 +2502,10 @@ dependencies = [ "polkadot-parachain-primitives", "polkadot-primitives", "scale-info", - "sp-api", - "sp-runtime", + "sp-api 33.0.0", + "sp-runtime 38.0.0", "sp-std", - "sp-trie", + "sp-trie 36.0.0", "staging-xcm", ] @@ -2532,11 +2520,11 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", - "sp-inherents", - "sp-runtime", - "sp-state-machine", + "sp-inherents 33.0.0", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "sp-std", - "sp-trie", + "sp-trie 36.0.0", ] [[package]] @@ -2547,7 +2535,7 @@ checksum = "9f973d2a7262c90e48dcd42062bcb1e0fbf48bbcdac4ea6df3d85212d8d8be5d" dependencies = [ "sp-externalities", "sp-runtime-interface", - "sp-trie", + "sp-trie 36.0.0", ] [[package]] @@ -2559,12 +2547,12 @@ dependencies = [ "cumulus-primitives-core", "cumulus-primitives-proof-size-hostfunction", "docify", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -2575,14 +2563,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05742c520065e3870d419683113ed7f6d35de66f0c80af6828e7878d1bb0ea94" dependencies = [ "cumulus-primitives-core", - "frame-support", + "frame-support 36.0.0", "log", "pallet-asset-conversion", "parity-scale-codec", "polkadot-runtime-common", "polkadot-runtime-parachains", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -2607,11 +2595,11 @@ dependencies = [ "sc-sysinfo", "sc-telemetry", "sc-tracing", - "sp-api", + "sp-api 33.0.0", "sp-consensus", "sp-core", - "sp-runtime", - "sp-state-machine", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", ] [[package]] @@ -2627,10 +2615,10 @@ dependencies = [ "parity-scale-codec", "polkadot-overseer", "sc-client-api", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", - "sp-state-machine", - "sp-version", + "sp-state-machine 0.42.0", + "sp-version 36.0.0", "thiserror", ] @@ -2667,11 +2655,11 @@ dependencies = [ "sc-service", "sc-tracing", "sc-utils", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-consensus", "sp-consensus-babe", - "sp-runtime", + "sp-runtime 38.0.0", "substrate-prometheus-endpoint", "tokio", "tracing", @@ -2702,14 +2690,14 @@ dependencies = [ "serde_json", "smoldot", "smoldot-light", - "sp-api", + "sp-api 33.0.0", "sp-authority-discovery", "sp-consensus-babe", "sp-core", - "sp-runtime", - "sp-state-machine", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "sp-storage", - "sp-version", + "sp-version 36.0.0", "thiserror", "tokio", "tokio-util", @@ -2726,10 +2714,10 @@ dependencies = [ "cumulus-primitives-core", "parity-scale-codec", "polkadot-primitives", - "sp-runtime", - "sp-state-machine", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "sp-std", - "sp-trie", + "sp-trie 36.0.0", ] [[package]] @@ -2744,7 +2732,7 @@ dependencies = [ "digest 0.10.7", "fiat-crypto", "platforms", - "rustc_version", + "rustc_version 0.4.0", "subtle 2.5.0", "zeroize", ] @@ -2914,17 +2902,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "derive-syn-parse" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e79116f119dd1dba1abf1f3405f03b9b0e79a27a3883864bfebded8a3dc768cd" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "derive-syn-parse" version = "0.2.0" @@ -2945,10 +2922,16 @@ dependencies = [ "convert_case", "proc-macro2", "quote", - "rustc_version", + "rustc_version 0.4.0", "syn 2.0.66", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "difflib" version = "0.4.0" @@ -3054,7 +3037,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a081e51fb188742f5a7a1164ad752121abcb22874b21e2c3b0dd040c515fdad" dependencies = [ "common-path", - "derive-syn-parse 0.2.0", + "derive-syn-parse", "once_cell", "proc-macro2", "quote", @@ -3203,7 +3186,7 @@ dependencies = [ "cumulus-pallet-parachain-system", "cumulus-pallet-xcmp-queue", "cumulus-primitives-core", - "frame-support", + "frame-support 36.0.0", "pallet-assets", "pallet-balances", "pallet-bridge-messages", @@ -3220,7 +3203,7 @@ dependencies = [ "sp-consensus-babe", "sp-consensus-beefy", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "staging-xcm", "xcm-emulator", ] @@ -3660,20 +3643,20 @@ version = "36.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "709b26657ebbba53dc7bb616577375ca462b20fef1b00e8d9b20d2435e87f7bc" dependencies = [ - "frame-support", + "frame-support 36.0.0", "frame-support-procedural", - "frame-system", + "frame-system 36.1.0", "linregress", "log", "parity-scale-codec", "paste", "scale-info", "serde", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-runtime-interface", "sp-std", "sp-storage", @@ -3692,8 +3675,8 @@ dependencies = [ "clap", "comfy-table", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "gethostname", "handlebars", "itertools 0.11.0", @@ -3713,19 +3696,19 @@ dependencies = [ "sc-sysinfo", "serde", "serde_json", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-core", "sp-database", "sp-externalities", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-genesis-builder 0.14.0", + "sp-inherents 33.0.0", + "sp-io 37.0.0", "sp-keystore", - "sp-runtime", - "sp-state-machine", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "sp-storage", - "sp-trie", + "sp-trie 36.0.0", "sp-wasm-interface", "thiserror", "thousands", @@ -3750,14 +3733,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1ec289ebad5e601bb165cf7eb6ec2179ae34280ee310d0710a3111d4f8f8f94" dependencies = [ "frame-election-provider-solution-type", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", "sp-arithmetic", "sp-core", "sp-npos-elections", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -3768,15 +3751,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d878830330eaa9e8b886279c338556b05702d0059989cb51cfb226b70bf3fa4" dependencies = [ "aquamarine", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "frame-try-runtime", "log", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "sp-tracing", ] @@ -3801,12 +3784,12 @@ checksum = "cf37fc730bf4b51e82a34c6357eebe32c04dbacf6525e0a7b9726f6a17ec9427" dependencies = [ "array-bytes", "docify", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", ] [[package]] @@ -3832,18 +3815,60 @@ dependencies = [ "serde", "serde_json", "smallvec", - "sp-api", + "sp-api 33.0.0", "sp-arithmetic", "sp-core", "sp-crypto-hashing-proc-macro", "sp-debug-derive", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-genesis-builder 0.14.0", + "sp-inherents 33.0.0", + "sp-io 37.0.0", "sp-metadata-ir", - "sp-runtime", - "sp-staking", - "sp-state-machine", + "sp-runtime 38.0.0", + "sp-staking 33.0.0", + "sp-state-machine 0.42.0", + "sp-std", + "sp-tracing", + "sp-weights", + "static_assertions", + "tt-call", +] + +[[package]] +name = "frame-support" +version = "37.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cae973c331b7f52ba18435713f9ed02bac20bd4fdedaaad57445d82f05eb9d" +dependencies = [ + "aquamarine", + "array-bytes", + "bitflags 1.3.2", + "docify", + "environmental", + "frame-metadata", + "frame-support-procedural", + "impl-trait-for-tuples", + "k256", + "log", + "macro_magic", + "parity-scale-codec", + "paste", + "scale-info", + "serde", + "serde_json", + "smallvec", + "sp-api 34.0.0", + "sp-arithmetic", + "sp-core", + "sp-crypto-hashing-proc-macro", + "sp-debug-derive", + "sp-genesis-builder 0.15.0", + "sp-inherents 34.0.0", + "sp-io 38.0.0", + "sp-metadata-ir", + "sp-runtime 39.0.0", + "sp-staking 34.0.0", + "sp-state-machine 0.43.0", "sp-std", "sp-tracing", "sp-weights", @@ -3859,7 +3884,7 @@ checksum = "fd94af68373e179c32c360b3c280497a9cf0f45a4f47f0ee6539a6c6c9cf2343" dependencies = [ "Inflector", "cfg-expr", - "derive-syn-parse 0.2.0", + "derive-syn-parse", "expander", "frame-support-procedural-tools", "itertools 0.11.0", @@ -3903,16 +3928,37 @@ checksum = "64d6a0e7bb6503facdcc6f8e19c83cd0bfc8bbbd268522b1a50e107dfc6b972d" dependencies = [ "cfg-if", "docify", - "frame-support", + "frame-support 36.0.0", "log", "parity-scale-codec", "scale-info", "serde", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", - "sp-version", + "sp-version 36.0.0", + "sp-weights", +] + +[[package]] +name = "frame-system" +version = "37.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "043790fff021477061b207fd6b33743793b63fc64a583358956787229d039717" +dependencies = [ + "cfg-if", + "docify", + "frame-support 37.0.0", + "log", + "parity-scale-codec", + "scale-info", + "serde", + "sp-core", + "sp-io 38.0.0", + "sp-runtime 39.0.0", + "sp-std", + "sp-version 37.0.0", "sp-weights", ] @@ -3923,12 +3969,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "15afc91c7780e18274dcea58ed1edb700c48d10e086a9785e3f6708099cd3250" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -3939,7 +3985,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c9e9e2b7b85e451e367f4fb85ff3295bd039e17f64de1906154d3976e2638ee8" dependencies = [ "parity-scale-codec", - "sp-api", + "sp-api 33.0.0", ] [[package]] @@ -3948,10 +3994,10 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae6ba8b36a52775ad39ccfb45ff4ad814c3cb45ec74d0a4271889e00bd791c6c" dependencies = [ - "frame-support", + "frame-support 36.0.0", "parity-scale-codec", - "sp-api", - "sp-runtime", + "sp-api 33.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -4983,7 +5029,7 @@ dependencies = [ "asset-test-utils", "cumulus-primitives-core", "emulated-integration-tests-common", - "frame-support", + "frame-support 36.0.0", "pallet-assets", "pallet-balances", "pallet-message-queue", @@ -5001,7 +5047,7 @@ dependencies = [ "sp-consensus-beefy", "sp-consensus-grandpa", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "staging-xcm", "staging-xcm-executor", "tracing-subscriber 0.3.18", @@ -5297,12 +5343,12 @@ name = "kusama-runtime-constants" version = "1.0.0" source = "git+https://github.com/polkadot-fellows/runtimes#f42acab60edf4d6ded4d9e99b1a8fbacded85669" dependencies = [ - "frame-support", + "frame-support 36.0.0", "polkadot-primitives", "polkadot-runtime-common", "smallvec", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-weights", "staging-xcm-builder", ] @@ -5363,6 +5409,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" +[[package]] +name = "leb128" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" + [[package]] name = "libc" version = "0.2.155" @@ -6107,9 +6159,9 @@ dependencies = [ [[package]] name = "macro_magic" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e03844fc635e92f3a0067e25fa4bf3e3dbf3f2927bf3aa01bb7bc8f1c428949d" +checksum = "cc33f9f0351468d26fbc53d9ce00a096c8522ecb42f19b50f34f2c422f76d21d" dependencies = [ "macro_magic_core", "macro_magic_macros", @@ -6119,12 +6171,12 @@ dependencies = [ [[package]] name = "macro_magic_core" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "468155613a44cfd825f1fb0ffa532b018253920d404e6fca1e8d43155198a46d" +checksum = "1687dc887e42f352865a393acae7cf79d98fab6351cde1f58e9e057da89bf150" dependencies = [ "const-random", - "derive-syn-parse 0.1.5", + "derive-syn-parse", "macro_magic_core_macros", "proc-macro2", "quote", @@ -6133,9 +6185,9 @@ dependencies = [ [[package]] name = "macro_magic_core_macros" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ea73aa640dc01d62a590d48c0c3521ed739d53b27f919b25c3551e233481654" +checksum = "b02abfe41815b5bd98dbd4260173db2c116dda171dc0fe7838cb206333b83308" dependencies = [ "proc-macro2", "quote", @@ -6144,9 +6196,9 @@ dependencies = [ [[package]] name = "macro_magic_macros" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef9d79ae96aaba821963320eb2b6e34d17df1e5a83d8a1985c29cc5be59577b3" +checksum = "73ea28ee64b88876bf45277ed9a5817c1817df061a74f2b988971a12570e5869" dependencies = [ "macro_magic_core", "quote", @@ -6343,13 +6395,13 @@ dependencies = [ "parity-scale-codec", "sc-client-api", "sc-offchain", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-consensus", "sp-consensus-beefy", "sp-core", "sp-mmr-primitives", - "sp-runtime", + "sp-runtime 38.0.0", ] [[package]] @@ -6361,11 +6413,11 @@ dependencies = [ "jsonrpsee", "parity-scale-codec", "serde", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-core", "sp-mmr-primitives", - "sp-runtime", + "sp-runtime 38.0.0", ] [[package]] @@ -7026,6 +7078,25 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" +[[package]] +name = "pallet-api" +version = "0.1.0" +dependencies = [ + "frame-benchmarking", + "frame-support 36.0.0", + "frame-system 36.1.0", + "log", + "pallet-assets", + "pallet-balances", + "parity-scale-codec", + "pop-chain-extension", + "scale-info", + "sp-core", + "sp-io 37.0.0", + "sp-runtime 38.0.0", + "sp-std", +] + [[package]] name = "pallet-asset-conversion" version = "18.0.0" @@ -7033,16 +7104,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f726ebb59401c1844a4a8703047bdafcd99a1827cd5d8b2c82abeb8948a7f25b" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", - "sp-api", + "sp-api 33.0.0", "sp-arithmetic", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -7052,13 +7123,13 @@ version = "18.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0fde03a96382f4dbe37ef95cb4ef7aade7c0be410cb6c888eda911c94af3eaf" dependencies = [ - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "pallet-asset-conversion", "pallet-transaction-payment", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7069,12 +7140,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e806842bec955190ec64f8b2179f74f5355137c4cadf04f3269e6196cd19caf9" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7085,15 +7156,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "100a180dfbf30a1c872100ec2dae8a61c0f5e8b3f2d3a5cbb34093826293e2ab" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "pallet-transaction-payment", "parity-scale-codec", "scale-info", "serde", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -7104,14 +7175,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f79ef6a7763fc08177f014052469ee12aefcdad0d99a747372360c2f648d2cc4" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "impl-trait-for-tuples", "log", "parity-scale-codec", "scale-info", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7121,15 +7192,15 @@ version = "35.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0861b2a1ad6526948567bb59a3fdc4c7f02ee79b07be8b931a544350ec35ab0c" dependencies = [ - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-consensus-aura", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7139,14 +7210,14 @@ version = "36.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed2c3666a476132f5846fe4d5e1961a923a58a0f54d873d84566f24ffaa3684f" dependencies = [ - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "pallet-session", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-authority-discovery", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7156,12 +7227,12 @@ version = "36.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38885846dbcf03b025fdbd7edb3649046dbc68fa0b419ffe8837ef853a10d31f" dependencies = [ - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "impl-trait-for-tuples", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7172,21 +7243,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b23d2d814e3cb793659fcf84533f66fdf0ed9cccb66cb2225851f482843ed096" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-authorship", "pallet-session", "pallet-timestamp", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-consensus-babe", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-session", - "sp-staking", + "sp-staking 33.0.0", "sp-std", ] @@ -7200,15 +7271,15 @@ dependencies = [ "docify", "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-balances", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "sp-tracing", ] @@ -7221,12 +7292,12 @@ checksum = "6878e240962d3887f0e0654ac343a18845adb95ad493c9d4d5e803c015d4a4c3" dependencies = [ "docify", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7236,8 +7307,8 @@ version = "36.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "715dfcd1bf3f1f37af6335d4eb3cef921e746ac54721e2258c4fd968b61eb009" dependencies = [ - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-authorship", "pallet-session", @@ -7245,9 +7316,9 @@ dependencies = [ "scale-info", "serde", "sp-consensus-beefy", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", - "sp-staking", + "sp-staking 33.0.0", "sp-std", ] @@ -7259,8 +7330,8 @@ checksum = "01d70c6f872eb3f2635355ccbea944a4f9ea411c0aa25f6f1a15219e8da11ad2" dependencies = [ "array-bytes", "binary-merkle-tree", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-beefy", "pallet-mmr", @@ -7268,12 +7339,12 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-api", + "sp-api 33.0.0", "sp-consensus-beefy", "sp-core", - "sp-io", - "sp-runtime", - "sp-state-machine", + "sp-io 37.0.0", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "sp-std", ] @@ -7284,15 +7355,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0566499e74ba4b7ccbd1b667eef0dab76ca28402a8d501e22b73a363717b05a9" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-treasury", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -7307,15 +7378,15 @@ dependencies = [ "bp-test-utils", "finality-grandpa", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", "sp-consensus-grandpa", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", - "sp-trie", + "sp-trie 36.0.0", ] [[package]] @@ -7327,13 +7398,13 @@ dependencies = [ "bp-messages", "bp-runtime", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "num-traits", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7348,15 +7419,15 @@ dependencies = [ "bp-polkadot-core", "bp-runtime", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-bridge-grandpa", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", - "sp-trie", + "sp-trie 36.0.0", ] [[package]] @@ -7369,14 +7440,14 @@ dependencies = [ "bp-relayers", "bp-runtime", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-bridge-messages", "parity-scale-codec", "scale-info", "sp-arithmetic", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7388,15 +7459,15 @@ checksum = "cd0d652c399b6ed776ee3322e60f40e323f86b413719d7696eddb8f64c368ac0" dependencies = [ "bitvec", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", - "sp-api", + "sp-api 33.0.0", "sp-arithmetic", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7407,16 +7478,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38e351f103ebbdd1eb095da8c2379caccc82ebc59a740c2731693d2204286b83" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-bounties", "pallet-treasury", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -7427,8 +7498,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f660cc09f2f277a3976da2eef856b5c725ab7ad1192902ef7f4e4bafd992f04f" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-authorship", "pallet-balances", @@ -7436,8 +7507,8 @@ dependencies = [ "parity-scale-codec", "rand", "scale-info", - "sp-runtime", - "sp-staking", + "sp-runtime 38.0.0", + "sp-staking 33.0.0", "sp-std", ] @@ -7448,17 +7519,62 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "771bf7f6c76c3ea5e965fee0bf1d8a8c79c8c52d75ead65ed3c4d385f333756f" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] +[[package]] +name = "pallet-contracts" +version = "27.0.0" +dependencies = [ + "array-bytes", + "assert_matches", + "bitflags 1.3.2", + "environmental", + "frame-benchmarking", + "frame-support 36.0.0", + "frame-system 36.1.0", + "impl-trait-for-tuples", + "log", + "pallet-assets", + "pallet-balances", + "pallet-contracts-fixtures", + "pallet-contracts-proc-macro 18.0.0", + "pallet-contracts-uapi 5.0.0", + "pallet-insecure-randomness-collective-flip", + "pallet-message-queue", + "pallet-proxy", + "pallet-timestamp", + "pallet-utility", + "parity-scale-codec", + "paste", + "pretty_assertions", + "rand", + "rand_pcg", + "scale-info", + "serde", + "smallvec", + "sp-api 33.0.0", + "sp-core", + "sp-io 37.0.0", + "sp-keystore", + "sp-runtime 38.0.0", + "sp-std", + "sp-tracing", + "staging-xcm", + "staging-xcm-builder", + "wasm-instrument", + "wasmi 0.32.3", + "wat", +] + [[package]] name = "pallet-contracts" version = "35.0.0" @@ -7468,13 +7584,13 @@ dependencies = [ "bitflags 1.3.2", "environmental", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "impl-trait-for-tuples", "log", "pallet-balances", - "pallet-contracts-proc-macro", - "pallet-contracts-uapi", + "pallet-contracts-proc-macro 23.0.1", + "pallet-contracts-uapi 11.0.0", "parity-scale-codec", "paste", "rand", @@ -7482,10 +7598,10 @@ dependencies = [ "scale-info", "serde", "smallvec", - "sp-api", + "sp-api 33.0.0", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -7493,6 +7609,29 @@ dependencies = [ "wasmi 0.32.3", ] +[[package]] +name = "pallet-contracts-fixtures" +version = "1.0.0" +dependencies = [ + "anyhow", + "frame-system 36.1.0", + "parity-wasm", + "polkavm-linker", + "sp-runtime 38.0.0", + "tempfile", + "toml 0.8.14", + "twox-hash", +] + +[[package]] +name = "pallet-contracts-proc-macro" +version = "18.0.0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] + [[package]] name = "pallet-contracts-proc-macro" version = "23.0.1" @@ -7504,6 +7643,17 @@ dependencies = [ "syn 2.0.66", ] +[[package]] +name = "pallet-contracts-uapi" +version = "5.0.0" +dependencies = [ + "bitflags 1.3.2", + "parity-scale-codec", + "paste", + "polkavm-derive", + "scale-info", +] + [[package]] name = "pallet-contracts-uapi" version = "11.0.0" @@ -7525,13 +7675,13 @@ checksum = "9033f0d23500bbc39298fd50c07b89a2f2d9f07300139b4df8005995ef683875" dependencies = [ "assert_matches", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", "serde", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -7541,12 +7691,12 @@ version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0596ec5ab55e02b1b5637b3ec2b99027d036fe97a1ab4733ae105474dfa727cf" dependencies = [ - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-staking", + "sp-runtime 38.0.0", + "sp-staking 33.0.0", "sp-std", ] @@ -7557,15 +7707,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ccd68a2bf5f2dfda2b810cbe1a779492d4c2e99338989fede4389d412ae325b" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", "serde", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -7577,8 +7727,8 @@ checksum = "bd1090fdc6ccdd8ff08c60000c970428baaaf0b33e7a6b01a91ec8b697a650a3" dependencies = [ "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-election-provider-support-benchmarking", "parity-scale-codec", @@ -7586,9 +7736,9 @@ dependencies = [ "scale-info", "sp-arithmetic", "sp-core", - "sp-io", + "sp-io 37.0.0", "sp-npos-elections", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", "strum 0.26.2", ] @@ -7601,10 +7751,10 @@ checksum = "93475989d2f6900caf8f1c847a55d909295c156525a7510c5f1dde176ec7c714" dependencies = [ "frame-benchmarking", "frame-election-provider-support", - "frame-system", + "frame-system 36.1.0", "parity-scale-codec", "sp-npos-elections", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7615,16 +7765,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9320d95c95e2d4d3ee24c9292b4ee8562ecb724b985613cfa7f274912bad2c9d" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", + "sp-io 37.0.0", "sp-npos-elections", - "sp-runtime", - "sp-staking", + "sp-runtime 38.0.0", + "sp-staking 33.0.0", "sp-std", ] @@ -7637,14 +7787,14 @@ dependencies = [ "docify", "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", - "sp-staking", + "sp-io 37.0.0", + "sp-runtime 38.0.0", + "sp-staking 33.0.0", "sp-std", ] @@ -7655,20 +7805,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8244b686d5cae6a8af1557ed0f49db08f812f0e7942a8d2da554b4da8a69daf0" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-authorship", "pallet-session", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-consensus-grandpa", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-session", - "sp-staking", + "sp-staking 33.0.0", "sp-std", ] @@ -7680,13 +7830,13 @@ checksum = "4555795a3e0e3aa49ea432b7afecb9c71a7db8793a99c68bd8dd3a52a12571f3" dependencies = [ "enumflags2", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -7697,17 +7847,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fa761292e95020304b58b50e5187f8bb82f557c8c2d013e3c96ab41d611873b0" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-authorship", "parity-scale-codec", "scale-info", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-core", - "sp-io", - "sp-runtime", - "sp-staking", + "sp-io 37.0.0", + "sp-runtime 38.0.0", + "sp-staking 33.0.0", "sp-std", ] @@ -7718,17 +7868,31 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b183880ad5efae06afe6066e76f2bac5acf67f34b3cfab7352ceec46accf4b45" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", + "sp-io 37.0.0", "sp-keyring", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] +[[package]] +name = "pallet-insecure-randomness-collective-flip" +version = "25.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e0875af2d12eb49d57c00f37cfbfba458033c10cfe87114318746381300a0e" +dependencies = [ + "frame-support 37.0.0", + "frame-system 37.1.0", + "parity-scale-codec", + "safe-mix", + "scale-info", + "sp-runtime 39.0.0", +] + [[package]] name = "pallet-membership" version = "36.0.0" @@ -7736,14 +7900,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34006cf047f47edbef33874cc64895918e2c5d7562795209068d5fb388c53a30" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -7755,15 +7919,15 @@ checksum = "20e65a37881d1998546254a5e50a1f768b3f82deabe774e750f4ea95aba8030c" dependencies = [ "environmental", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", "sp-arithmetic", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "sp-weights", ] @@ -7775,15 +7939,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8ccec82827413f031689fef4c714fdb0213d58c7a6e208d33f5eab80483770" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", + "sp-io 37.0.0", "sp-mmr-primitives", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7794,13 +7958,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be58483d827602eb8353ecf36aed65c857f0974db5d27981831e5ebf853040bd" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -7811,14 +7975,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dcaa330221f60feaf3b23d495cccc3bf2a3d6254c596b3c032273c2b46d4078" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-assets", "pallet-nfts", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7830,14 +7994,14 @@ checksum = "3e1cd476809de3840e19091a083d5a79178af1f108ad489706e1f9e04c8836a4" dependencies = [ "enumflags2", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -7849,7 +8013,7 @@ checksum = "b0ca7a0446d2d3c27f726a016c6366218df2e0bfef9ed35886b252cfa9757f6c" dependencies = [ "pallet-nfts", "parity-scale-codec", - "sp-api", + "sp-api 33.0.0", "sp-std", ] @@ -7860,13 +8024,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e77cba0e15749c8de2be65efffa51e02bd051b4e6fcf23360d43c3b6a859187c" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", "sp-arithmetic", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7876,16 +8040,16 @@ version = "33.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "36f8c994eb7298a394b58f98afd520b521b5d46f6f39eade4657eeaac9962471" dependencies = [ - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-balances", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", - "sp-runtime", - "sp-staking", + "sp-io 37.0.0", + "sp-runtime 38.0.0", + "sp-staking 33.0.0", "sp-std", "sp-tracing", ] @@ -7898,17 +8062,17 @@ checksum = "39ee599f2861e55fc6113c01e9b14d6e85fda46bac36a906b5dd5a951fa0455c" dependencies = [ "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "pallet-bags-list", "pallet-delegated-staking", "pallet-nomination-pools", "pallet-staking", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", "sp-runtime-interface", - "sp-staking", + "sp-staking 33.0.0", "sp-std", ] @@ -7920,7 +8084,7 @@ checksum = "2906899d8f029780f0d9da77b90ae86f42bcfda5ac402c931406cd84852012ed" dependencies = [ "pallet-nomination-pools", "parity-scale-codec", - "sp-api", + "sp-api 33.0.0", "sp-std", ] @@ -7930,15 +8094,15 @@ version = "35.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4859e7bb2af46d2e0f137c2f777adf39f0e5d4d188226158d599f1cfcfb76b9e" dependencies = [ - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-balances", "parity-scale-codec", "scale-info", "serde", - "sp-runtime", - "sp-staking", + "sp-runtime 38.0.0", + "sp-staking 33.0.0", "sp-std", ] @@ -7950,8 +8114,8 @@ checksum = "4351b0edafcdf3240f0471c638b39d2c981bde9d17c0172536a0aa3b7c3097ef" dependencies = [ "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-babe", "pallet-balances", @@ -7962,8 +8126,8 @@ dependencies = [ "pallet-staking", "parity-scale-codec", "scale-info", - "sp-runtime", - "sp-staking", + "sp-runtime 38.0.0", + "sp-staking 33.0.0", "sp-std", ] @@ -7975,14 +8139,14 @@ checksum = "58d9a81a93202105a660e6aa3d3f81638bdd109ca0497f3e528529cd52d034db" dependencies = [ "docify", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "paste", "scale-info", "serde", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -7993,14 +8157,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68ac726abc5b1bcd6c8f783514b8e1a48be32c7d15e0b263e4bc28cc1e4e7763" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -8011,12 +8175,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4e12680e176607815a78a0cd10a52af50790292cb950404f30a885e2a7229e9" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -8027,16 +8191,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "862ea8d386ed5737e859470c43cbfd9652c81398cad29e03ae7846c21aaee4c6" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "impl-trait-for-tuples", "log", "parity-scale-codec", "scale-info", "sp-arithmetic", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -8047,12 +8211,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b24d4131bc79fee0b07550136ca6329faa84c1c3e76ae62a74aef6b1da0b95b4" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -8064,15 +8228,15 @@ checksum = "b2c906a9c4573eb58de4134ec7180bf12c6769df2b9859dae8adcbc5fce78add" dependencies = [ "assert_matches", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", "serde", "sp-arithmetic", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -8082,13 +8246,13 @@ version = "12.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fa61642f7bdc1a393798aa1ff67bb8c29f8f184b6fce165e1079010d446a1e29" dependencies = [ - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -8100,13 +8264,13 @@ checksum = "b170d6aa191197d3f50b1193925546972ffc394376ead4d2739eb40909b73c85" dependencies = [ "docify", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "sp-weights", ] @@ -8117,21 +8281,21 @@ version = "36.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c92b24c911c2cfa5351616edc7f2f93427ea6f4f95efdb13f0f5d51997939c3" dependencies = [ - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "impl-trait-for-tuples", "log", "pallet-timestamp", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-session", - "sp-staking", - "sp-state-machine", + "sp-staking 33.0.0", + "sp-state-machine 0.42.0", "sp-std", - "sp-trie", + "sp-trie 36.0.0", ] [[package]] @@ -8141,13 +8305,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd02aaf5f10734670346677042ece94fae20dcd5436eafeb9b429d8d6d5b6385" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "pallet-session", "pallet-staking", "parity-scale-codec", "rand", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", "sp-std", ] @@ -8159,15 +8323,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "66b60b1d726532317f9965bab4995aa49b73f9b7ca3b9a0f75d158bd84686c5f" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "rand_chacha", "scale-info", "sp-arithmetic", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -8179,8 +8343,8 @@ checksum = "fbebdb060417654f215fc6f03675e5f44cfc83837d9e523e1b8fd9a4a2e1bdc2" dependencies = [ "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-authorship", "pallet-session", @@ -8188,10 +8352,10 @@ dependencies = [ "rand_chacha", "scale-info", "serde", - "sp-application-crypto", - "sp-io", - "sp-runtime", - "sp-staking", + "sp-application-crypto 37.0.0", + "sp-io 37.0.0", + "sp-runtime 38.0.0", + "sp-staking 33.0.0", "sp-std", ] @@ -8224,8 +8388,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3350ef1795b832f4adc464e88fb6d44827bd3f98701b0b0bbee495267b444a92" dependencies = [ "parity-scale-codec", - "sp-api", - "sp-staking", + "sp-api 33.0.0", + "sp-staking 33.0.0", ] [[package]] @@ -8235,14 +8399,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e07f8626f4ff62ac79d6ad0bd01fab7645897ce35706ddb95fa084e75be9306d" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -8254,12 +8418,12 @@ checksum = "1bd2a8797c1bb3d3897b4f87a7716111da5eeb8561345277b6e6d70349ec8b35" dependencies = [ "docify", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -8271,14 +8435,14 @@ checksum = "ae789d344be857679b0b98b28a67c747119724847f81d704d3fd03ee13fb6841" dependencies = [ "docify", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", - "sp-inherents", - "sp-io", - "sp-runtime", + "sp-inherents 33.0.0", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "sp-storage", "sp-timestamp", @@ -8291,16 +8455,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7dfec7872ee9e071209ae860094569745e8bd47564bacdba739256ee52cf78c" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-treasury", "parity-scale-codec", "scale-info", "serde", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -8310,14 +8474,14 @@ version = "36.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74fb6114223c8d967c3c2f21cbc845e8ea604ff7e21a8e59d119d5a9257ba886" dependencies = [ - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", "serde", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -8330,11 +8494,11 @@ dependencies = [ "jsonrpsee", "pallet-transaction-payment-rpc-runtime-api", "parity-scale-codec", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-core", "sp-rpc", - "sp-runtime", + "sp-runtime 38.0.0", "sp-weights", ] @@ -8346,8 +8510,8 @@ checksum = "f4bad1700ad7eb5ab254189e1df894d1d16b3626a3c4b9c45259ec4d9efc262c" dependencies = [ "pallet-transaction-payment", "parity-scale-codec", - "sp-api", - "sp-runtime", + "sp-api 33.0.0", + "sp-runtime 38.0.0", "sp-weights", ] @@ -8359,15 +8523,15 @@ checksum = "9c502615bb4fdd02856a131cb2a612ad40c26435ec938f65f11cae4ff230812b" dependencies = [ "docify", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "impl-trait-for-tuples", "pallet-balances", "parity-scale-codec", "scale-info", "serde", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -8378,12 +8542,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a59e8599a8c19908e934645f845b5cb546cef1f08745319db7e5b9c24f9e0e4" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -8394,13 +8558,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3238fe6ad00da6a137be115904c39cab97eb5c7f03da0bb1a20de1bef03f0c71" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -8411,12 +8575,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78f7f0f4fe5e1d851e85d81e5e73b6f929f0c35af786ce8be9c9e3363717c136" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -8427,12 +8591,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e4f27640279229eb73fde0cb06e98b799305e6b0bc724f4dfbef2001ab4ad00" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", - "sp-api", - "sp-runtime", + "sp-api 33.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -8442,18 +8606,18 @@ version = "15.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe7409458b7fedc5c7d46459da154ccc2dc22a843ce08e8ab6c1743ef5cf972c" dependencies = [ - "bounded-collections 0.2.0", + "bounded-collections", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-balances", "parity-scale-codec", "scale-info", "serde", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -8468,13 +8632,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f177a171203cc0bec3cff1bdd5d3b926abfbd0ecf347e044b147194e664f717" dependencies = [ "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -8489,13 +8653,13 @@ checksum = "f48bd38d4061a51f263f4c08021e66100e16cbda9978fba163d2544637b31dab" dependencies = [ "bp-xcm-bridge-hub-router", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "parity-scale-codec", "scale-info", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -8509,8 +8673,8 @@ checksum = "9319e656eebdf161666e54a4d8e24f73137f702f01600247f7be650bc4d46167" dependencies = [ "cumulus-primitives-core", "cumulus-primitives-utility", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "log", "pallet-asset-tx-payment", "pallet-assets", @@ -8524,8 +8688,8 @@ dependencies = [ "scale-info", "sp-consensus-aura", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "staging-parachain-info", "staging-xcm", @@ -8544,8 +8708,8 @@ dependencies = [ "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", "cumulus-test-relay-sproof-builder", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "pallet-balances", "pallet-collator-selection", "pallet-session", @@ -8555,8 +8719,8 @@ dependencies = [ "polkadot-parachain-primitives", "sp-consensus-aura", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "sp-tracing", "staging-parachain-info", @@ -9013,10 +9177,10 @@ dependencies = [ "sc-sysinfo", "sc-tracing", "sp-core", - "sp-io", + "sp-io 37.0.0", "sp-keyring", "sp-maybe-compressed-blob", - "sp-runtime", + "sp-runtime 38.0.0", "substrate-build-script-utils", "thiserror", ] @@ -9038,7 +9202,7 @@ dependencies = [ "polkadot-primitives", "sp-core", "sp-keystore", - "sp-runtime", + "sp-runtime 38.0.0", "thiserror", "tokio-util", "tracing-gum", @@ -9053,7 +9217,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -9077,7 +9241,7 @@ dependencies = [ "polkadot-primitives", "sc-network", "schnellru", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-keystore", "thiserror", "tracing-gum", @@ -9094,7 +9258,7 @@ dependencies = [ "polkadot-primitives", "reed-solomon-novelpoly", "sp-core", - "sp-trie", + "sp-trie 36.0.0", "thiserror", ] @@ -9114,7 +9278,7 @@ dependencies = [ "rand_chacha", "sc-network", "sc-network-common", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-core", "sp-crypto-hashing", "sp-keystore", @@ -9190,10 +9354,10 @@ dependencies = [ "sc-keystore", "schnellru", "schnorrkel 0.11.4", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-consensus", "sp-consensus-slots", - "sp-runtime", + "sp-runtime 38.0.0", "thiserror", "tracing-gum", ] @@ -9346,7 +9510,7 @@ dependencies = [ "polkadot-overseer", "polkadot-primitives", "sp-blockchain", - "sp-inherents", + "sp-inherents 33.0.0", "thiserror", "tracing-gum", ] @@ -9456,7 +9620,7 @@ dependencies = [ "sp-core", "sp-crypto-hashing", "sp-externalities", - "sp-io", + "sp-io 37.0.0", "sp-tracing", "thiserror", "tracing-gum", @@ -9539,7 +9703,7 @@ dependencies = [ "sc-authority-discovery", "sc-network", "sc-network-types", - "sp-runtime", + "sp-runtime 38.0.0", "strum 0.26.2", "thiserror", "tracing-gum", @@ -9559,12 +9723,12 @@ dependencies = [ "polkadot-primitives", "schnorrkel 0.11.4", "serde", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-consensus-babe", "sp-core", "sp-keystore", "sp-maybe-compressed-blob", - "sp-runtime", + "sp-runtime 38.0.0", "thiserror", "zstd 0.12.4", ] @@ -9602,11 +9766,11 @@ dependencies = [ "sc-network-types", "sc-transaction-pool-api", "smallvec", - "sp-api", + "sp-api 33.0.0", "sp-authority-discovery", "sp-blockchain", "sp-consensus-babe", - "sp-runtime", + "sp-runtime 38.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -9641,7 +9805,7 @@ dependencies = [ "rand", "sc-client-api", "schnellru", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-core", "sp-keystore", "thiserror", @@ -9665,7 +9829,7 @@ dependencies = [ "polkadot-node-subsystem-types", "polkadot-primitives", "sc-client-api", - "sp-api", + "sp-api 33.0.0", "sp-core", "tikv-jemalloc-ctl", "tracing-gum", @@ -9677,14 +9841,14 @@ version = "13.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f61070d0ff28f596890def0e0d03c231860796130b2a43e293106fa86a50c9a9" dependencies = [ - "bounded-collections 0.2.0", + "bounded-collections", "derive_more", "parity-scale-codec", "polkadot-core-primitives", "scale-info", "serde", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", "sp-weights", ] @@ -9703,17 +9867,17 @@ dependencies = [ "polkadot-parachain-primitives", "scale-info", "serde", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-arithmetic", "sp-authority-discovery", "sp-consensus-slots", "sp-core", - "sp-inherents", - "sp-io", + "sp-inherents 33.0.0", + "sp-io 37.0.0", "sp-keystore", - "sp-runtime", - "sp-staking", + "sp-runtime 38.0.0", + "sp-staking 33.0.0", "sp-std", ] @@ -9740,15 +9904,15 @@ dependencies = [ "sc-rpc-spec-v2", "sc-sync-state-rpc", "sc-transaction-pool-api", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-block-builder", "sp-blockchain", "sp-consensus", "sp-consensus-babe", "sp-consensus-beefy", "sp-keystore", - "sp-runtime", + "sp-runtime 38.0.0", "substrate-frame-rpc-system", "substrate-state-trie-migration-rpc", ] @@ -9763,8 +9927,8 @@ dependencies = [ "frame-election-provider-support", "frame-executive", "frame-metadata-hash-extension", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", @@ -9823,8 +9987,8 @@ dependencies = [ "polkadot-runtime-parachains", "scale-info", "serde_json", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-arithmetic", "sp-authority-discovery", "sp-block-builder", @@ -9832,18 +9996,18 @@ dependencies = [ "sp-consensus-beefy", "sp-core", "sp-debug-derive", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-genesis-builder 0.14.0", + "sp-inherents 33.0.0", + "sp-io 37.0.0", "sp-npos-elections", "sp-offchain", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", - "sp-staking", + "sp-staking 33.0.0", "sp-std", "sp-storage", "sp-transaction-pool", - "sp-version", + "sp-version 36.0.0", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -9860,8 +10024,8 @@ dependencies = [ "bitvec", "frame-benchmarking", "frame-election-provider-support", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "impl-trait-for-tuples", "libsecp256k1", "log", @@ -9888,14 +10052,14 @@ dependencies = [ "serde", "serde_derive", "slot-range-helper", - "sp-api", + "sp-api 33.0.0", "sp-core", - "sp-inherents", - "sp-io", + "sp-inherents 33.0.0", + "sp-io 37.0.0", "sp-npos-elections", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", - "sp-staking", + "sp-staking 33.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -9908,12 +10072,12 @@ name = "polkadot-runtime-constants" version = "1.0.0" source = "git+https://github.com/polkadot-fellows/runtimes#f42acab60edf4d6ded4d9e99b1a8fbacded85669" dependencies = [ - "frame-support", + "frame-support 36.0.0", "polkadot-primitives", "polkadot-runtime-common", "smallvec", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-weights", "staging-xcm-builder", ] @@ -9942,8 +10106,8 @@ dependencies = [ "bitvec", "derive_more", "frame-benchmarking", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "impl-trait-for-tuples", "log", "pallet-authority-discovery", @@ -9965,16 +10129,16 @@ dependencies = [ "rand_chacha", "scale-info", "serde", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-arithmetic", "sp-core", - "sp-inherents", - "sp-io", + "sp-inherents 33.0.0", + "sp-io 37.0.0", "sp-keystore", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", - "sp-staking", + "sp-staking 33.0.0", "sp-std", "staging-xcm", "staging-xcm-executor", @@ -9990,8 +10154,8 @@ dependencies = [ "async-trait", "frame-benchmarking", "frame-benchmarking-cli", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "frame-system-rpc-runtime-api", "futures", "hex-literal", @@ -10069,7 +10233,7 @@ dependencies = [ "schnellru", "serde", "serde_json", - "sp-api", + "sp-api 33.0.0", "sp-authority-discovery", "sp-block-builder", "sp-blockchain", @@ -10078,19 +10242,19 @@ dependencies = [ "sp-consensus-beefy", "sp-consensus-grandpa", "sp-core", - "sp-inherents", - "sp-io", + "sp-inherents 33.0.0", + "sp-io 37.0.0", "sp-keyring", "sp-keystore", "sp-mmr-primitives", "sp-offchain", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", - "sp-state-machine", + "sp-state-machine 0.42.0", "sp-storage", "sp-timestamp", "sp-transaction-pool", - "sp-version", + "sp-version 36.0.0", "sp-weights", "staging-xcm", "substrate-prometheus-endpoint", @@ -10119,7 +10283,7 @@ dependencies = [ "polkadot-node-subsystem-util", "polkadot-primitives", "sp-keystore", - "sp-staking", + "sp-staking 33.0.0", "thiserror", "tracing-gum", ] @@ -10273,6 +10437,22 @@ dependencies = [ "universal-hash", ] +[[package]] +name = "pop-chain-extension" +version = "0.1.0" +dependencies = [ + "frame-support 36.0.0", + "frame-system 36.1.0", + "impl-trait-for-tuples", + "log", + "pallet-contracts 35.0.0", + "parity-scale-codec", + "rand", + "sp-core", + "sp-runtime 38.0.0", + "sp-std", +] + [[package]] name = "pop-node" version = "0.2.0-alpha" @@ -10319,15 +10499,15 @@ dependencies = [ "sc-transaction-pool-api", "serde", "serde_json", - "sp-api", + "sp-api 33.0.0", "sp-block-builder", "sp-blockchain", "sp-consensus-aura", "sp-core", - "sp-io", + "sp-io 37.0.0", "sp-keystore", "sp-offchain", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", "sp-timestamp", "sp-transaction-pool", @@ -10341,7 +10521,6 @@ dependencies = [ name = "pop-primitives" version = "0.0.0" dependencies = [ - "bounded-collections 0.1.9", "parity-scale-codec", "scale-info", ] @@ -10350,12 +10529,12 @@ dependencies = [ name = "pop-runtime-common" version = "0.0.0" dependencies = [ - "frame-support", + "frame-support 36.0.0", "parachains-common", "parity-scale-codec", "polkadot-primitives", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -10372,25 +10551,25 @@ dependencies = [ "cumulus-primitives-core", "cumulus-primitives-storage-weight-reclaim", "cumulus-primitives-utility", - "enumflags2", "env_logger 0.11.5", "frame-benchmarking", "frame-executive", "frame-metadata-hash-extension", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", "hex", "hex-literal", "log", + "pallet-api", "pallet-assets", "pallet-aura", "pallet-authorship", "pallet-balances", "pallet-collator-selection", - "pallet-contracts", + "pallet-contracts 35.0.0", "pallet-message-queue", "pallet-multisig", "pallet-nft-fractionalization", @@ -10410,23 +10589,24 @@ dependencies = [ "parity-scale-codec", "polkadot-parachain-primitives", "polkadot-runtime-common", + "pop-chain-extension", "pop-primitives", "pop-runtime-common", "scale-info", "smallvec", - "sp-api", + "sp-api 33.0.0", "sp-block-builder", "sp-consensus-aura", "sp-core", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-genesis-builder 0.14.0", + "sp-inherents 33.0.0", + "sp-io 37.0.0", "sp-offchain", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", "sp-std", "sp-transaction-pool", - "sp-version", + "sp-version 36.0.0", "staging-parachain-info", "staging-xcm", "staging-xcm-builder", @@ -10447,13 +10627,12 @@ dependencies = [ "cumulus-primitives-core", "cumulus-primitives-storage-weight-reclaim", "cumulus-primitives-utility", - "enumflags2", "env_logger 0.11.5", "frame-benchmarking", "frame-executive", "frame-metadata-hash-extension", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", @@ -10465,7 +10644,7 @@ dependencies = [ "pallet-authorship", "pallet-balances", "pallet-collator-selection", - "pallet-contracts", + "pallet-contracts 35.0.0", "pallet-message-queue", "pallet-multisig", "pallet-nft-fractionalization", @@ -10489,19 +10668,19 @@ dependencies = [ "pop-runtime-common", "scale-info", "smallvec", - "sp-api", + "sp-api 33.0.0", "sp-block-builder", "sp-consensus-aura", "sp-core", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-genesis-builder 0.14.0", + "sp-inherents 33.0.0", + "sp-io 37.0.0", "sp-offchain", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", "sp-std", "sp-transaction-pool", - "sp-version", + "sp-version 36.0.0", "staging-parachain-info", "staging-xcm", "staging-xcm-builder", @@ -10567,6 +10746,16 @@ dependencies = [ "termtree", ] +[[package]] +name = "pretty_assertions" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "prettyplease" version = "0.1.25" @@ -11316,8 +11505,8 @@ dependencies = [ "frame-benchmarking", "frame-executive", "frame-metadata-hash-extension", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", @@ -11379,7 +11568,7 @@ dependencies = [ "serde_derive", "serde_json", "smallvec", - "sp-api", + "sp-api 33.0.0", "sp-arithmetic", "sp-authority-discovery", "sp-block-builder", @@ -11387,18 +11576,18 @@ dependencies = [ "sp-consensus-beefy", "sp-consensus-grandpa", "sp-core", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-genesis-builder 0.14.0", + "sp-inherents 33.0.0", + "sp-io 37.0.0", "sp-mmr-primitives", "sp-offchain", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", - "sp-staking", + "sp-staking 33.0.0", "sp-std", "sp-storage", "sp-transaction-pool", - "sp-version", + "sp-version 36.0.0", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -11413,12 +11602,12 @@ version = "15.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2ef330dc0657ac9e4ff93ff320e2ee1a120493bceb91010c7ef7b08fe8e27950" dependencies = [ - "frame-support", + "frame-support 36.0.0", "polkadot-primitives", "polkadot-runtime-common", "smallvec", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-weights", "staging-xcm", "staging-xcm-builder", @@ -11484,6 +11673,15 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver 0.9.0", +] + [[package]] name = "rustc_version" version = "0.4.0" @@ -11713,6 +11911,15 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "safe-mix" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d3d055a2582e6b00ed7a31c1524040aa391092bf636328350813f3a0605215c" +dependencies = [ + "rustc_version 0.2.3", +] + [[package]] name = "safe_arch" version = "0.7.2" @@ -11764,12 +11971,12 @@ dependencies = [ "sc-client-api", "sc-network", "sc-network-types", - "sp-api", + "sp-api 33.0.0", "sp-authority-discovery", "sp-blockchain", "sp-core", "sp-keystore", - "sp-runtime", + "sp-runtime 38.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -11788,12 +11995,12 @@ dependencies = [ "sc-proposer-metrics", "sc-telemetry", "sc-transaction-pool-api", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-consensus", "sp-core", - "sp-inherents", - "sp-runtime", + "sp-inherents 33.0.0", + "sp-runtime 38.0.0", "substrate-prometheus-endpoint", ] @@ -11804,13 +12011,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6345fb862e10aaa7d88d6689a7c247448c40ae465253c83566dc76a17ec1426" dependencies = [ "parity-scale-codec", - "sp-api", + "sp-api 33.0.0", "sp-block-builder", "sp-blockchain", "sp-core", - "sp-inherents", - "sp-runtime", - "sp-trie", + "sp-inherents 33.0.0", + "sp-runtime 38.0.0", + "sp-trie 36.0.0", ] [[package]] @@ -11834,10 +12041,10 @@ dependencies = [ "sp-blockchain", "sp-core", "sp-crypto-hashing", - "sp-genesis-builder", - "sp-io", - "sp-runtime", - "sp-state-machine", + "sp-genesis-builder 0.14.0", + "sp-io 37.0.0", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "sp-tracing", ] @@ -11889,8 +12096,8 @@ dependencies = [ "sp-keyring", "sp-keystore", "sp-panic-handler", - "sp-runtime", - "sp-version", + "sp-runtime 38.0.0", + "sp-version 36.0.0", "thiserror", "tokio", ] @@ -11909,17 +12116,17 @@ dependencies = [ "sc-executor", "sc-transaction-pool-api", "sc-utils", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-consensus", "sp-core", "sp-database", "sp-externalities", - "sp-runtime", - "sp-state-machine", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "sp-statement-store", "sp-storage", - "sp-trie", + "sp-trie 36.0.0", "substrate-prometheus-endpoint", ] @@ -11945,9 +12152,9 @@ dependencies = [ "sp-blockchain", "sp-core", "sp-database", - "sp-runtime", - "sp-state-machine", - "sp-trie", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", + "sp-trie 36.0.0", ] [[package]] @@ -11965,12 +12172,12 @@ dependencies = [ "sc-network-types", "sc-utils", "serde", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-consensus", "sp-core", - "sp-runtime", - "sp-state-machine", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -11990,17 +12197,17 @@ dependencies = [ "sc-consensus", "sc-consensus-slots", "sc-telemetry", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-block-builder", "sp-blockchain", "sp-consensus", "sp-consensus-aura", "sp-consensus-slots", "sp-core", - "sp-inherents", + "sp-inherents 33.0.0", "sp-keystore", - "sp-runtime", + "sp-runtime 38.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -12026,8 +12233,8 @@ dependencies = [ "sc-consensus-slots", "sc-telemetry", "sc-transaction-pool-api", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-block-builder", "sp-blockchain", "sp-consensus", @@ -12035,9 +12242,9 @@ dependencies = [ "sp-consensus-slots", "sp-core", "sp-crypto-hashing", - "sp-inherents", + "sp-inherents 33.0.0", "sp-keystore", - "sp-runtime", + "sp-runtime 38.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -12054,14 +12261,14 @@ dependencies = [ "sc-consensus-epochs", "sc-rpc-api", "serde", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-blockchain", "sp-consensus", "sp-consensus-babe", "sp-core", "sp-keystore", - "sp-runtime", + "sp-runtime 38.0.0", "thiserror", ] @@ -12086,8 +12293,8 @@ dependencies = [ "sc-network-sync", "sc-network-types", "sc-utils", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-arithmetic", "sp-blockchain", "sp-consensus", @@ -12095,7 +12302,7 @@ dependencies = [ "sp-core", "sp-crypto-hashing", "sp-keystore", - "sp-runtime", + "sp-runtime 38.0.0", "substrate-prometheus-endpoint", "thiserror", "tokio", @@ -12116,10 +12323,10 @@ dependencies = [ "sc-consensus-beefy", "sc-rpc", "serde", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-consensus-beefy", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "thiserror", ] @@ -12134,7 +12341,7 @@ dependencies = [ "sc-client-api", "sc-consensus", "sp-blockchain", - "sp-runtime", + "sp-runtime 38.0.0", ] [[package]] @@ -12168,8 +12375,8 @@ dependencies = [ "sc-transaction-pool-api", "sc-utils", "serde_json", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-arithmetic", "sp-blockchain", "sp-consensus", @@ -12177,7 +12384,7 @@ dependencies = [ "sp-core", "sp-crypto-hashing", "sp-keystore", - "sp-runtime", + "sp-runtime 38.0.0", "substrate-prometheus-endpoint", "thiserror", ] @@ -12199,7 +12406,7 @@ dependencies = [ "serde", "sp-blockchain", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "thiserror", ] @@ -12222,9 +12429,9 @@ dependencies = [ "sp-consensus", "sp-consensus-slots", "sp-core", - "sp-inherents", - "sp-runtime", - "sp-state-machine", + "sp-inherents 33.0.0", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", ] [[package]] @@ -12239,14 +12446,14 @@ dependencies = [ "sc-executor-polkavm", "sc-executor-wasmtime", "schnellru", - "sp-api", + "sp-api 33.0.0", "sp-core", "sp-externalities", - "sp-io", + "sp-io 37.0.0", "sp-panic-handler", "sp-runtime-interface", - "sp-trie", - "sp-version", + "sp-trie 36.0.0", + "sp-version 36.0.0", "sp-wasm-interface", "tracing", ] @@ -12311,7 +12518,7 @@ dependencies = [ "sc-network-common", "sc-network-sync", "sp-blockchain", - "sp-runtime", + "sp-runtime 38.0.0", ] [[package]] @@ -12323,7 +12530,7 @@ dependencies = [ "array-bytes", "parking_lot 0.12.3", "serde_json", - "sp-application-crypto", + "sp-application-crypto 37.0.0", "sp-core", "sp-keystore", "thiserror", @@ -12350,12 +12557,12 @@ dependencies = [ "sc-network", "sc-network-types", "sc-transaction-pool-api", - "sp-api", + "sp-api 33.0.0", "sp-consensus", "sp-core", "sp-keystore", "sp-mixnet", - "sp-runtime", + "sp-runtime 38.0.0", "thiserror", ] @@ -12400,7 +12607,7 @@ dependencies = [ "sp-arithmetic", "sp-blockchain", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "substrate-prometheus-endpoint", "thiserror", "tokio", @@ -12427,7 +12634,7 @@ dependencies = [ "sc-network-types", "sp-consensus", "sp-consensus-grandpa", - "sp-runtime", + "sp-runtime 38.0.0", ] [[package]] @@ -12445,7 +12652,7 @@ dependencies = [ "sc-network-sync", "sc-network-types", "schnellru", - "sp-runtime", + "sp-runtime 38.0.0", "substrate-prometheus-endpoint", "tracing", ] @@ -12468,7 +12675,7 @@ dependencies = [ "sc-network-types", "sp-blockchain", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "thiserror", ] @@ -12503,7 +12710,7 @@ dependencies = [ "sp-consensus", "sp-consensus-grandpa", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "substrate-prometheus-endpoint", "thiserror", "tokio", @@ -12526,7 +12733,7 @@ dependencies = [ "sc-network-types", "sc-utils", "sp-consensus", - "sp-runtime", + "sp-runtime 38.0.0", "substrate-prometheus-endpoint", ] @@ -12573,12 +12780,12 @@ dependencies = [ "sc-network-types", "sc-transaction-pool-api", "sc-utils", - "sp-api", + "sp-api 33.0.0", "sp-core", "sp-externalities", "sp-keystore", "sp-offchain", - "sp-runtime", + "sp-runtime 38.0.0", "threadpool", "tracing", ] @@ -12613,16 +12820,16 @@ dependencies = [ "sc-transaction-pool-api", "sc-utils", "serde_json", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-core", "sp-keystore", "sp-offchain", "sp-rpc", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", "sp-statement-store", - "sp-version", + "sp-version 36.0.0", "tokio", ] @@ -12642,8 +12849,8 @@ dependencies = [ "serde_json", "sp-core", "sp-rpc", - "sp-runtime", - "sp-version", + "sp-runtime 38.0.0", + "sp-version 36.0.0", "thiserror", ] @@ -12692,12 +12899,12 @@ dependencies = [ "sc-utils", "schnellru", "serde", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-core", "sp-rpc", - "sp-runtime", - "sp-version", + "sp-runtime 38.0.0", + "sp-version 36.0.0", "thiserror", "tokio", "tokio-stream", @@ -12745,20 +12952,20 @@ dependencies = [ "schnellru", "serde", "serde_json", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-consensus", "sp-core", "sp-externalities", "sp-keystore", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", - "sp-state-machine", + "sp-state-machine 0.42.0", "sp-storage", "sp-transaction-pool", "sp-transaction-storage-proof", - "sp-trie", - "sp-version", + "sp-trie 36.0.0", + "sp-version 36.0.0", "static_init", "substrate-prometheus-endpoint", "tempfile", @@ -12810,7 +13017,7 @@ dependencies = [ "serde", "serde_json", "sp-blockchain", - "sp-runtime", + "sp-runtime 38.0.0", "thiserror", ] @@ -12832,7 +13039,7 @@ dependencies = [ "serde_json", "sp-core", "sp-crypto-hashing", - "sp-io", + "sp-io 37.0.0", "sp-std", ] @@ -12876,11 +13083,11 @@ dependencies = [ "sc-client-api", "sc-tracing-proc-macro", "serde", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-core", "sp-rpc", - "sp-runtime", + "sp-runtime 38.0.0", "sp-tracing", "thiserror", "tracing", @@ -12917,11 +13124,11 @@ dependencies = [ "sc-transaction-pool-api", "sc-utils", "serde", - "sp-api", + "sp-api 33.0.0", "sp-blockchain", "sp-core", "sp-crypto-hashing", - "sp-runtime", + "sp-runtime 38.0.0", "sp-tracing", "sp-transaction-pool", "substrate-prometheus-endpoint", @@ -12941,7 +13148,7 @@ dependencies = [ "serde", "sp-blockchain", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "thiserror", ] @@ -13192,6 +13399,15 @@ dependencies = [ "semver-parser", ] +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + [[package]] name = "semver" version = "1.0.23" @@ -13452,7 +13668,7 @@ dependencies = [ "enumn", "parity-scale-codec", "paste", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -13596,7 +13812,7 @@ dependencies = [ "curve25519-dalek", "rand_core", "ring 0.17.8", - "rustc_version", + "rustc_version 0.4.0", "sha2 0.10.8", "subtle 2.5.0", ] @@ -13618,7 +13834,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0ad61e3ab1c48d4c8060c7ef8571c5b6007df26687e8dbfdb6c857d840cfd2c" dependencies = [ "byte-slice-cast", - "frame-support", + "frame-support 36.0.0", "hex", "parity-scale-codec", "rlp", @@ -13627,8 +13843,8 @@ dependencies = [ "snowbridge-ethereum", "snowbridge-milagro-bls", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "ssz_rs", "ssz_rs_derive", @@ -13641,8 +13857,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "668cd71582305168ed51cb0357a4b4ea814c68c7db3898a9ba4d492f712c54e1" dependencies = [ "ethabi-decode", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "hex-literal", "parity-scale-codec", "polkadot-parachain-primitives", @@ -13651,8 +13867,8 @@ dependencies = [ "snowbridge-beacon-primitives", "sp-arithmetic", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "staging-xcm", "staging-xcm-builder", @@ -13674,8 +13890,8 @@ dependencies = [ "scale-info", "serde", "serde-big-array", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", ] @@ -13700,15 +13916,15 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e8e6707ced1308d763117bfe68f85e3f22fcdca7987b32e438c0485570f6ac7" dependencies = [ - "frame-support", + "frame-support 36.0.0", "hex-literal", "log", "parity-scale-codec", "scale-info", "snowbridge-core", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "staging-xcm", "staging-xcm-executor", @@ -13779,12 +13995,35 @@ dependencies = [ "sp-core", "sp-externalities", "sp-metadata-ir", - "sp-runtime", + "sp-runtime 38.0.0", "sp-runtime-interface", - "sp-state-machine", + "sp-state-machine 0.42.0", "sp-std", - "sp-trie", - "sp-version", + "sp-trie 36.0.0", + "sp-version 36.0.0", + "thiserror", +] + +[[package]] +name = "sp-api" +version = "34.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbce492e0482134128b7729ea36f5ef1a9f9b4de2d48ff8dde7b5e464e28ce75" +dependencies = [ + "docify", + "hash-db", + "log", + "parity-scale-codec", + "scale-info", + "sp-api-proc-macro", + "sp-core", + "sp-externalities", + "sp-metadata-ir", + "sp-runtime 39.0.0", + "sp-runtime-interface", + "sp-state-machine 0.43.0", + "sp-trie 37.0.0", + "sp-version 37.0.0", "thiserror", ] @@ -13813,10 +14052,23 @@ dependencies = [ "scale-info", "serde", "sp-core", - "sp-io", + "sp-io 37.0.0", "sp-std", ] +[[package]] +name = "sp-application-crypto" +version = "38.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d8133012faa5f75b2f0b1619d9f720c1424ac477152c143e5f7dbde2fe1a958" +dependencies = [ + "parity-scale-codec", + "scale-info", + "serde", + "sp-core", + "sp-io 38.0.0", +] + [[package]] name = "sp-arithmetic" version = "26.0.0" @@ -13841,9 +14093,9 @@ checksum = "6a4a1e45abc3277f18484ee0b0f9808e4206eb696ad38500c892c72f33480d69" dependencies = [ "parity-scale-codec", "scale-info", - "sp-api", - "sp-application-crypto", - "sp-runtime", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", + "sp-runtime 38.0.0", ] [[package]] @@ -13852,9 +14104,9 @@ version = "33.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2cf199dc4f9f77abd3fd91c409759118159ce6ffcd8bc90b229b684ccc8c981f" dependencies = [ - "sp-api", - "sp-inherents", - "sp-runtime", + "sp-api 33.0.0", + "sp-inherents 33.0.0", + "sp-runtime 38.0.0", ] [[package]] @@ -13868,11 +14120,11 @@ dependencies = [ "parity-scale-codec", "parking_lot 0.12.3", "schnellru", - "sp-api", + "sp-api 33.0.0", "sp-consensus", "sp-database", - "sp-runtime", - "sp-state-machine", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "thiserror", ] @@ -13886,9 +14138,9 @@ dependencies = [ "futures", "log", "sp-core", - "sp-inherents", - "sp-runtime", - "sp-state-machine", + "sp-inherents 33.0.0", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", "thiserror", ] @@ -13901,11 +14153,11 @@ dependencies = [ "async-trait", "parity-scale-codec", "scale-info", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-consensus-slots", - "sp-inherents", - "sp-runtime", + "sp-inherents 33.0.0", + "sp-runtime 38.0.0", "sp-timestamp", ] @@ -13919,12 +14171,12 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-consensus-slots", "sp-core", - "sp-inherents", - "sp-runtime", + "sp-inherents 33.0.0", + "sp-runtime 38.0.0", "sp-timestamp", ] @@ -13938,14 +14190,14 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-core", "sp-crypto-hashing", - "sp-io", + "sp-io 37.0.0", "sp-keystore", "sp-mmr-primitives", - "sp-runtime", + "sp-runtime 38.0.0", "strum 0.26.2", ] @@ -13960,11 +14212,11 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-core", "sp-keystore", - "sp-runtime", + "sp-runtime 38.0.0", ] [[package]] @@ -13988,7 +14240,7 @@ dependencies = [ "array-bytes", "bitflags 1.3.2", "blake2 0.10.6", - "bounded-collections 0.2.0", + "bounded-collections", "bs58 0.5.1", "dyn-clonable", "ed25519-zebra", @@ -14092,8 +14344,21 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde_json", - "sp-api", - "sp-runtime", + "sp-api 33.0.0", + "sp-runtime 38.0.0", +] + +[[package]] +name = "sp-genesis-builder" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "182812315871732372325f0ad42bb8b85a093a06dd77ae1eec997259f4c32aef" +dependencies = [ + "parity-scale-codec", + "scale-info", + "serde_json", + "sp-api 34.0.0", + "sp-runtime 39.0.0", ] [[package]] @@ -14106,7 +14371,21 @@ dependencies = [ "impl-trait-for-tuples", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", + "thiserror", +] + +[[package]] +name = "sp-inherents" +version = "34.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afffbddc380d99a90c459ba1554bbbc01d62e892de9f1485af6940b89c4c0d57" +dependencies = [ + "async-trait", + "impl-trait-for-tuples", + "parity-scale-codec", + "scale-info", + "sp-runtime 39.0.0", "thiserror", ] @@ -14129,10 +14408,37 @@ dependencies = [ "sp-externalities", "sp-keystore", "sp-runtime-interface", - "sp-state-machine", + "sp-state-machine 0.42.0", "sp-std", "sp-tracing", - "sp-trie", + "sp-trie 36.0.0", + "tracing", + "tracing-core", +] + +[[package]] +name = "sp-io" +version = "38.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ef7eb561bb4839cc8424ce58c5ea236cbcca83f26fcc0426d8decfe8aa97d4" +dependencies = [ + "bytes", + "docify", + "ed25519-dalek", + "libsecp256k1", + "log", + "parity-scale-codec", + "polkavm-derive", + "rustversion", + "secp256k1", + "sp-core", + "sp-crypto-hashing", + "sp-externalities", + "sp-keystore", + "sp-runtime-interface", + "sp-state-machine 0.43.0", + "sp-tracing", + "sp-trie 37.0.0", "tracing", "tracing-core", ] @@ -14144,7 +14450,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b03536e1ff3ec2bd8181eeaa26c0d682ebdcbd01548a055cf591077188b8c3f0" dependencies = [ "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "strum 0.26.2", ] @@ -14189,8 +14495,8 @@ checksum = "2f65a570519da820ce3dc35053497a65f9fbd3f5a7dc81fa03078ca263e9311e" dependencies = [ "parity-scale-codec", "scale-info", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", ] [[package]] @@ -14204,10 +14510,10 @@ dependencies = [ "polkadot-ckb-merkle-mountain-range", "scale-info", "serde", - "sp-api", + "sp-api 33.0.0", "sp-core", "sp-debug-derive", - "sp-runtime", + "sp-runtime 38.0.0", "thiserror", ] @@ -14222,7 +14528,7 @@ dependencies = [ "serde", "sp-arithmetic", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", ] [[package]] @@ -14231,9 +14537,9 @@ version = "33.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cbe721c367760bddf10fcfa24fb48edd64c442f71db971f043c8ac73f51aa6e9" dependencies = [ - "sp-api", + "sp-api 33.0.0", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", ] [[package]] @@ -14276,12 +14582,39 @@ dependencies = [ "scale-info", "serde", "simple-mermaid", - "sp-application-crypto", + "sp-application-crypto 37.0.0", + "sp-arithmetic", + "sp-core", + "sp-io 37.0.0", + "sp-std", + "sp-weights", +] + +[[package]] +name = "sp-runtime" +version = "39.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5641385c2cd8e2252aacf35e0aff2f236331dfaea8dc11c5a4ec6bb36544450" +dependencies = [ + "docify", + "either", + "hash256-std-hasher", + "impl-trait-for-tuples", + "log", + "num-traits", + "parity-scale-codec", + "paste", + "rand", + "scale-info", + "serde", + "simple-mermaid", + "sp-application-crypto 38.0.0", "sp-arithmetic", "sp-core", - "sp-io", + "sp-io 38.0.0", "sp-std", "sp-weights", + "tracing", ] [[package]] @@ -14326,11 +14659,11 @@ checksum = "4daf2e40ffc7e7e8de08efb860eb9534faf614a49c53dc282f430faedb4aed13" dependencies = [ "parity-scale-codec", "scale-info", - "sp-api", + "sp-api 33.0.0", "sp-core", "sp-keystore", - "sp-runtime", - "sp-staking", + "sp-runtime 38.0.0", + "sp-staking 33.0.0", ] [[package]] @@ -14344,7 +14677,21 @@ dependencies = [ "scale-info", "serde", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", +] + +[[package]] +name = "sp-staking" +version = "34.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "143a764cacbab58347d8b2fd4c8909031fb0888d7b02a0ec9fa44f81f780d732" +dependencies = [ + "impl-trait-for-tuples", + "parity-scale-codec", + "scale-info", + "serde", + "sp-core", + "sp-runtime 39.0.0", ] [[package]] @@ -14362,7 +14709,28 @@ dependencies = [ "sp-core", "sp-externalities", "sp-panic-handler", - "sp-trie", + "sp-trie 36.0.0", + "thiserror", + "tracing", + "trie-db", +] + +[[package]] +name = "sp-state-machine" +version = "0.43.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "930104d6ae882626e8880d9b1578da9300655d337a3ffb45e130c608b6c89660" +dependencies = [ + "hash-db", + "log", + "parity-scale-codec", + "parking_lot 0.12.3", + "rand", + "smallvec", + "sp-core", + "sp-externalities", + "sp-panic-handler", + "sp-trie 37.0.0", "thiserror", "tracing", "trie-db", @@ -14382,12 +14750,12 @@ dependencies = [ "rand", "scale-info", "sha2 0.10.8", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-core", "sp-crypto-hashing", "sp-externalities", - "sp-runtime", + "sp-runtime 38.0.0", "sp-runtime-interface", "thiserror", "x25519-dalek", @@ -14420,8 +14788,8 @@ checksum = "78becf144a76f6fd108dfe94a90e20a185b38c0b310dc5482328196143c8266b" dependencies = [ "async-trait", "parity-scale-codec", - "sp-inherents", - "sp-runtime", + "sp-inherents 33.0.0", + "sp-runtime 38.0.0", "thiserror", ] @@ -14443,8 +14811,8 @@ version = "33.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3c9d1604aadc15b70e95f4388d0b1aa380215520b7ddfd372531a6d8262269c" dependencies = [ - "sp-api", - "sp-runtime", + "sp-api 33.0.0", + "sp-runtime 38.0.0", ] [[package]] @@ -14457,9 +14825,9 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", - "sp-inherents", - "sp-runtime", - "sp-trie", + "sp-inherents 33.0.0", + "sp-runtime 38.0.0", + "sp-trie 36.0.0", ] [[package]] @@ -14486,6 +14854,30 @@ dependencies = [ "trie-root", ] +[[package]] +name = "sp-trie" +version = "37.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6282aef9f4b6ecd95a67a45bcdb67a71f4a4155c09a53c10add4ffe823db18cd" +dependencies = [ + "ahash 0.8.11", + "hash-db", + "lazy_static", + "memory-db", + "nohash-hasher", + "parity-scale-codec", + "parking_lot 0.12.3", + "rand", + "scale-info", + "schnellru", + "sp-core", + "sp-externalities", + "thiserror", + "tracing", + "trie-db", + "trie-root", +] + [[package]] name = "sp-version" version = "36.0.0" @@ -14498,7 +14890,25 @@ dependencies = [ "scale-info", "serde", "sp-crypto-hashing-proc-macro", - "sp-runtime", + "sp-runtime 38.0.0", + "sp-std", + "sp-version-proc-macro", + "thiserror", +] + +[[package]] +name = "sp-version" +version = "37.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d521a405707b5be561367cd3d442ff67588993de24062ce3adefcf8437ee9fe1" +dependencies = [ + "impl-serde", + "parity-scale-codec", + "parity-wasm", + "scale-info", + "serde", + "sp-crypto-hashing-proc-macro", + "sp-runtime 39.0.0", "sp-std", "sp-version-proc-macro", "thiserror", @@ -14535,7 +14945,7 @@ version = "31.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93cdaf72a1dad537bbb130ba4d47307ebe5170405280ed1aa31fa712718a400e" dependencies = [ - "bounded-collections 0.2.0", + "bounded-collections", "parity-scale-codec", "scale-info", "serde", @@ -14626,11 +15036,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd00d586b0dac4f42736bdd0ad52213a891b240e011ea82b38938263dd821c25" dependencies = [ "cumulus-primitives-core", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "parity-scale-codec", "scale-info", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", ] @@ -14641,7 +15051,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2b7b5f531c6bf9629514ef8e5fda0e9e80dd84516957f710940d0e01d3fb36c" dependencies = [ "array-bytes", - "bounded-collections 0.2.0", + "bounded-collections", "derivative", "environmental", "impl-trait-for-tuples", @@ -14659,8 +15069,8 @@ version = "15.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "847fa2afe1bed2751eaabf7b91fa4043037947f17653d7cc59ea202cc44c6bb8" dependencies = [ - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "impl-trait-for-tuples", "log", "pallet-transaction-payment", @@ -14668,8 +15078,8 @@ dependencies = [ "polkadot-parachain-primitives", "scale-info", "sp-arithmetic", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "sp-weights", "staging-xcm", @@ -14684,15 +15094,15 @@ checksum = "26b98d8219449eaf02e71a7edf1a14b14d4c713dd01d9df66fde1ce30dba4d6d" dependencies = [ "environmental", "frame-benchmarking", - "frame-support", + "frame-support 36.0.0", "impl-trait-for-tuples", "log", "parity-scale-codec", "scale-info", "sp-arithmetic", "sp-core", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "sp-weights", "staging-xcm", @@ -14843,11 +15253,11 @@ dependencies = [ "parity-scale-codec", "sc-rpc-api", "sc-transaction-pool-api", - "sp-api", + "sp-api 33.0.0", "sp-block-builder", "sp-blockchain", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", ] [[package]] @@ -14875,9 +15285,9 @@ dependencies = [ "sc-rpc-api", "serde", "sp-core", - "sp-runtime", - "sp-state-machine", - "sp-trie", + "sp-runtime 38.0.0", + "sp-state-machine 0.42.0", + "sp-trie 36.0.0", "trie-db", ] @@ -14899,10 +15309,10 @@ dependencies = [ "polkavm-linker", "sc-executor", "sp-core", - "sp-io", + "sp-io 37.0.0", "sp-maybe-compressed-blob", "sp-tracing", - "sp-version", + "sp-version 36.0.0", "strum 0.26.2", "tempfile", "toml 0.8.14", @@ -14999,7 +15409,7 @@ name = "system-parachains-constants" version = "1.0.0" source = "git+https://github.com/polkadot-fellows/runtimes#f42acab60edf4d6ded4d9e99b1a8fbacded85669" dependencies = [ - "frame-support", + "frame-support 36.0.0", "kusama-runtime-constants", "parachains-common", "polkadot-core-primitives", @@ -15007,7 +15417,7 @@ dependencies = [ "polkadot-runtime-constants", "smallvec", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-std", "staging-xcm", ] @@ -15989,6 +16399,15 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +[[package]] +name = "wasm-encoder" +version = "0.216.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04c23aebea22c8a75833ae08ed31ccc020835b12a41999e58c31464271b94a88" +dependencies = [ + "leb128", +] + [[package]] name = "wasm-instrument" version = "0.4.0" @@ -16338,6 +16757,28 @@ dependencies = [ "wasmparser", ] +[[package]] +name = "wast" +version = "216.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7eb1f2eecd913fdde0dc6c3439d0f24530a98ac6db6cb3d14d92a5328554a08" +dependencies = [ + "bumpalo", + "leb128", + "memchr", + "unicode-width", + "wasm-encoder", +] + +[[package]] +name = "wat" +version = "1.216.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac0409090fb5154f95fb5ba3235675fd9e579e731524d63b6a2f653e1280c82a" +dependencies = [ + "wast", +] + [[package]] name = "web-sys" version = "0.3.69" @@ -16385,8 +16826,8 @@ dependencies = [ "frame-election-provider-support", "frame-executive", "frame-metadata-hash-extension", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", @@ -16452,27 +16893,27 @@ dependencies = [ "serde", "serde_derive", "smallvec", - "sp-api", - "sp-application-crypto", + "sp-api 33.0.0", + "sp-application-crypto 37.0.0", "sp-arithmetic", "sp-authority-discovery", "sp-block-builder", "sp-consensus-babe", "sp-consensus-beefy", "sp-core", - "sp-genesis-builder", - "sp-inherents", - "sp-io", + "sp-genesis-builder 0.14.0", + "sp-inherents 33.0.0", + "sp-io 37.0.0", "sp-mmr-primitives", "sp-npos-elections", "sp-offchain", - "sp-runtime", + "sp-runtime 38.0.0", "sp-session", - "sp-staking", + "sp-staking 33.0.0", "sp-std", "sp-storage", "sp-transaction-pool", - "sp-version", + "sp-version 36.0.0", "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", @@ -16487,12 +16928,12 @@ version = "15.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c7a91c27c398b11f7633cc2382cbba53b02e7196ebe8fff13c170e54a54e9d8" dependencies = [ - "frame-support", + "frame-support 36.0.0", "polkadot-primitives", "polkadot-runtime-common", "smallvec", "sp-core", - "sp-runtime", + "sp-runtime 38.0.0", "sp-weights", "staging-xcm", "staging-xcm-builder", @@ -16902,8 +17343,8 @@ dependencies = [ "cumulus-primitives-core", "cumulus-primitives-parachain-inherent", "cumulus-test-relay-sproof-builder", - "frame-support", - "frame-system", + "frame-support 36.0.0", + "frame-system 36.1.0", "impl-trait-for-tuples", "lazy_static", "log", @@ -16918,8 +17359,8 @@ dependencies = [ "sp-arithmetic", "sp-core", "sp-crypto-hashing", - "sp-io", - "sp-runtime", + "sp-io 37.0.0", + "sp-runtime 38.0.0", "sp-std", "sp-tracing", "staging-xcm", @@ -16944,10 +17385,10 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "30fffcd9128a46abd836c37dd001c2cbe122aeb8904cd7b9bac8358564fb7b56" dependencies = [ - "frame-support", + "frame-support 36.0.0", "parity-scale-codec", "scale-info", - "sp-api", + "sp-api 33.0.0", "sp-std", "sp-weights", "staging-xcm", @@ -16984,6 +17425,12 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" + [[package]] name = "yasna" version = "0.5.2" @@ -17146,4 +17593,4 @@ checksum = "75652c55c0b6f3e6f12eb786fe1bc960396bf05a1eb3bf1f3691c3610ac2e6d4" dependencies = [ "cc", "pkg-config", -] \ No newline at end of file +] diff --git a/Cargo.toml b/Cargo.toml index c9f716d1..8533cb49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,19 +2,20 @@ panic = "unwind" [profile.production] +codegen-units = 1 inherits = "release" lto = true -codegen-units = 1 [workspace.package] authors = ["R0GUE "] +description = "Pop Network makes it easy for smart contract developers to use the Power of Polkadot." edition = "2021" homepage = "https://r0gue.io" license = "Unlicense" repository = "https://github.com/r0gue-io/pop-node/" -description = "Pop Network makes it easy for smart contract developers to use the Power of Polkadot." [workspace] +exclude = ["pop-api", "tests/contracts"] members = [ "node", "runtime/devnet", @@ -23,30 +24,33 @@ members = [ "pallets/*", "primitives", ] -exclude = ["pop-api", "tests/contracts"] resolver = "2" [workspace.dependencies] +clap = { version = "4.4.18", features = ["derive"] } codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false, features = [ "derive", ] } -impl-trait-for-tuples = "0.2.2" +futures = "0.3.28" hex-literal = "0.4.1" +impl-trait-for-tuples = "0.2.2" +jsonrpsee = { version = "0.23.2", features = ["server"] } log = { version = "0.4.21", default-features = false } scale-info = { version = "2.11.1", default-features = false, features = [ "derive", ] } -smallvec = "1.11.2" serde = "1.0.197" -clap = { version = "4.4.18", features = ["derive"] } -jsonrpsee = { version = "0.23.2", features = ["server"] } -futures = "0.3.28" serde_json = "1.0.114" -tracing-subscriber = { version = "0.3", default-features = false } +smallvec = "1.11.2" subxt = "0.34.0" subxt-signer = "0.34.0" -tokio = { version = "1.36", features = ["macros", "time", "rt-multi-thread"] } +tokio = { version = "1.36", features = ["macros", "rt-multi-thread", "time"] } +tracing-subscriber = { version = "0.3", default-features = false } + +# Build +substrate-build-script-utils = "11.0.0" +substrate-wasm-builder = "23.0.0" # Experimental pallet-revive dependencies anyhow = { version = "1.0.81" } @@ -78,45 +82,27 @@ tempfile = { version = "3.8.1" } toml = { version = "0.8.8" } twox-hash = { version = "1.6.3", default-features = false } -# Build -substrate-build-script-utils = "11.0.0" -substrate-wasm-builder = "23.0.0" - # Local +pallet-api = { path = "pallets/api", default-features = false } +pop-chain-extension = { path = "./extension", default-features = false } pop-runtime-devnet = { path = "runtime/devnet", default-features = true } # default-features=true required for `-p pop-node` builds pop-runtime-testnet = { path = "runtime/testnet", default-features = true } # default-features=true required for `-p pop-node` builds pop-runtime-common = { path = "runtime/common", default-features = false } pop-primitives = { path = "./primitives", default-features = false } # Substrate -sc-basic-authorship = "0.42.0" -sc-chain-spec = "35.0.0" -sc-cli = "0.44.0" -sc-client-api = "35.1.0" -sc-offchain = "37.0.0" -sc-consensus = "0.41.0" -sc-executor = "0.39.0" -sc-network = "0.42.0" -sc-network-sync = "0.41.0" -sc-rpc = "37.0.0" -sc-service = "0.43.0" -sc-sysinfo = "35.0.0" -sc-telemetry = "22.0.0" -sc-tracing = "35.0.0" -sc-transaction-pool = "35.0.0" -sc-transaction-pool-api = "35.0.0" frame-benchmarking = { version = "36.0.0", default-features = false } frame-benchmarking-cli = "40.0.0" frame-executive = { version = "36.0.0", default-features = false } +frame-metadata-hash-extension = { version = "0.4.0", default-features = false } frame-support = { version = "36.0.0", default-features = false } frame-system = { version = "36.1.0", default-features = false } frame-system-benchmarking = { version = "36.0.0", default-features = false } frame-system-rpc-runtime-api = { version = "33.0.0", default-features = false } frame-try-runtime = { version = "0.42.0", default-features = false } -frame-metadata-hash-extension = { version = "0.4.0", default-features = false } +pallet-assets = { version = "37.0.0", default-features = false } pallet-aura = { version = "35.0.0", default-features = false } pallet-authorship = { version = "36.0.0", default-features = false } -pallet-assets = { version = "37.0.0", default-features = false } pallet-balances = { version = "37.0.0", default-features = false } pallet-contracts = { version = "35.0.0", default-features = false } pallet-message-queue = { version = "39.0.0", default-features = false } @@ -135,6 +121,22 @@ pallet-transaction-payment-rpc = "38.0.0" pallet-transaction-payment-rpc-runtime-api = { version = "36.0.0", default-features = false } pallet-utility = { version = "36.0.0", default-features = false } prometheus-endpoint = { version = "0.17.0", default-features = false, package = "substrate-prometheus-endpoint" } +sc-basic-authorship = "0.42.0" +sc-chain-spec = "35.0.0" +sc-cli = "0.44.0" +sc-client-api = "35.1.0" +sc-consensus = "0.41.0" +sc-executor = "0.39.0" +sc-network = "0.42.0" +sc-network-sync = "0.41.0" +sc-offchain = "37.0.0" +sc-rpc = "37.0.0" +sc-service = "0.43.0" +sc-sysinfo = "35.0.0" +sc-telemetry = "22.0.0" +sc-tracing = "35.0.0" +sc-transaction-pool = "35.0.0" +sc-transaction-pool-api = "35.0.0" sp-api = { version = "33.0.0", default-features = false } sp-authority-discovery = { version = "33.0.0", default-features = false } sp-block-builder = { version = "33.0.0", default-features = false } @@ -144,28 +146,28 @@ sp-consensus-babe = { version = "0.39.0", default-features = false } sp-consensus-beefy = { version = "20.0.0", default-features = false } sp-consensus-grandpa = { version = "20.0.0", default-features = false } sp-core = { version = "34.0.0", default-features = false } -sp-keystore = "0.40.0" -sp-io = { version = "37.0.0", default-features = false } sp-genesis-builder = { version = "0.14.0", default-features = false } sp-inherents = { version = "33.0.0", default-features = false } +sp-io = { version = "37.0.0", default-features = false } +sp-keystore = "0.40.0" sp-offchain = { version = "33.0.0", default-features = false } sp-runtime = { version = "38.0.0", default-features = false } -sp-timestamp = "33.0.0" -substrate-frame-rpc-system = "36.0.0" sp-session = { version = "34.0.0", default-features = false } sp-std = { version = "14.0.0", default-features = false } +sp-timestamp = "33.0.0" sp-transaction-pool = { version = "33.0.0", default-features = false } sp-version = { version = "36.0.0", default-features = false } +substrate-frame-rpc-system = "36.0.0" # Polkadot pallet-xcm = { version = "15.0.0", default-features = false } polkadot-cli = "15.0.0" polkadot-parachain-primitives = { version = "13.0.0", default-features = false } -polkadot-runtime-parachains = { version = "15.0.3", default-features = false } polkadot-primitives = { version = "14.0.0", default-features = false } polkadot-runtime-common = { version = "15.0.0", default-features = false } -rococo-runtime-constants = { version = "15.0.0", default-features = false } +polkadot-runtime-parachains = { version = "15.0.3", default-features = false } rococo-runtime = { version = "15.0.0", default-features = false } +rococo-runtime-constants = { version = "15.0.0", default-features = false } xcm = { version = "14.0.3", package = "staging-xcm", default-features = false } xcm-builder = { version = "15.0.0", package = "staging-xcm-builder", default-features = false } xcm-executor = { version = "15.0.0", package = "staging-xcm-executor", default-features = false } @@ -173,6 +175,13 @@ xcm-executor = { version = "15.0.0", package = "staging-xcm-executor", default-f # Cumulus asset-hub-rococo-runtime = { version = "0.19.0", default-features = false } asset-test-utils = { version = "15.0.0", default-features = false } +color-print = "0.3.4" +cumulus-client-cli = "0.15.0" +cumulus-client-collator = "0.15.0" +cumulus-client-consensus-aura = "0.15.0" +cumulus-client-consensus-common = "0.15.0" +cumulus-client-consensus-proposer = "0.14.0" +cumulus-client-service = "0.15.0" cumulus-pallet-aura-ext = { version = "0.15.0", default-features = false } cumulus-pallet-parachain-system = { version = "0.15.0", default-features = false } cumulus-pallet-session-benchmarking = { version = "17.0.0", default-features = false } @@ -180,21 +189,14 @@ cumulus-pallet-xcm = { version = "0.15.0", default-features = false } cumulus-pallet-xcmp-queue = { version = "0.15.0", default-features = false } cumulus-primitives-aura = { version = "0.14.0", default-features = false } cumulus-primitives-core = { version = "0.14.0", default-features = false } +cumulus-primitives-parachain-inherent = "0.14.0" cumulus-primitives-storage-weight-reclaim = { version = "6.0.2", default-features = false } cumulus-primitives-utility = { version = "0.15.0", default-features = false } +cumulus-relay-chain-interface = "0.15.0" emulated-integration-tests-common = { version = "11.0.0", default-features = false } pallet-collator-selection = { version = "17.0.0", default-features = false } -parachains-common = { version = "15.0.0", default-features = false } parachain-info = { version = "0.15.0", package = "staging-parachain-info", default-features = false } -cumulus-primitives-parachain-inherent = "0.14.0" -cumulus-relay-chain-interface = "0.15.0" -color-print = "0.3.4" -cumulus-client-cli = "0.15.0" -cumulus-client-collator = "0.15.0" -cumulus-client-consensus-aura = "0.15.0" -cumulus-client-consensus-common = "0.15.0" -cumulus-client-consensus-proposer = "0.14.0" -cumulus-client-service = "0.15.0" +parachains-common = { version = "15.0.0", default-features = false } # TODO: Paseo (note: using polkadot as stopgap until paseo updated to polkadot sdk v1.14.0) asset-hub-paseo-runtime = { git = "https://github.com/polkadot-fellows/runtimes", default-features = false, package = "asset-hub-polkadot-runtime" } diff --git a/extension/src/decoding.rs b/extension/src/decoding.rs index 7ad035ed..eb9e21b5 100644 --- a/extension/src/decoding.rs +++ b/extension/src/decoding.rs @@ -1,5 +1,6 @@ use super::*; use pallet_contracts::chain_extension::BufIn; +use pallet_contracts::WeightInfo; use sp_runtime::DispatchError; use sp_std::vec::Vec; @@ -23,10 +24,7 @@ pub trait Decode { // Charge appropriate weight, based on input length, prior to decoding. // reference: https://github.com/paritytech/polkadot-sdk/blob/117a9433dac88d5ac00c058c9b39c511d47749d2/substrate/frame/contracts/src/wasm/runtime.rs#L267 let len = env.in_len(); - let weight = Schedule::::get() - .host_fn_weights - .return_per_byte - .saturating_mul(len.into()); + let weight = ::WeightInfo::seal_return(len); env.charge_weight(weight)?; log::debug!(target: Self::LOG_TARGET, "pre-decode weight charged: len={len}, weight={weight}"); // Read encoded input supplied by contract for buffer. diff --git a/extension/src/functions.rs b/extension/src/functions.rs index 0f181bd6..8c859d35 100644 --- a/extension/src/functions.rs +++ b/extension/src/functions.rs @@ -3,6 +3,7 @@ use core::fmt::Debug; pub use decoding::{Decode, Decodes}; pub use matching::{Equals, FunctionId, Matches}; use pallet_contracts::chain_extension::{BufIn, BufOut}; +use pallet_contracts::WeightInfo; /// A chain extension function. pub trait Function { @@ -128,8 +129,9 @@ impl< log::debug!(target: Logger::LOG_TARGET, "read: result={result:?}"); // Perform any final conversion. Any implementation is expected to charge weight as appropriate. let result = ResultConverter::convert(result, env).into(); - // TODO: check parameters (allow_skip, weight_per_byte) - env.write(&result, false, Some(Schedule::::get().host_fn_weights.input_per_byte))?; + // To be conservative, we charge the weight for reading the input bytes of a fixed-size type. + let base_weight: Weight = Config::WeightInfo::seal_return(env.in_len()); + env.write(&result, false, Some(base_weight))?; Ok(Converging(0)) } } diff --git a/extension/src/lib.rs b/extension/src/lib.rs index 2a244541..fc3127ca 100644 --- a/extension/src/lib.rs +++ b/extension/src/lib.rs @@ -13,6 +13,7 @@ pub use functions::{ }; use pallet_contracts::chain_extension::{ChainExtension, InitState, RetVal::Converging}; pub use pallet_contracts::chain_extension::{Environment, Ext, Result, RetVal, State}; +use pallet_contracts::WeightInfo; use sp_core::Get; use sp_runtime::{traits::Dispatchable, DispatchError}; use sp_std::vec::Vec; @@ -23,8 +24,6 @@ mod matching; #[cfg(test)] mod tests; -type Schedule = ::Schedule; - /// A configurable chain extension. #[derive(Default)] pub struct Extension(PhantomData); @@ -43,10 +42,12 @@ where fn call>(&mut self, env: Environment) -> Result { log::trace!(target: Config::LOG_TARGET, "extension called"); let mut env = env.buf_in_buf_out(); - // Charge weight for making a call from a contract to the runtime. - // `debug_message` weight is a good approximation of the additional overhead of going from contract layer to substrate layer. + // debug_message weight is a good approximation of the additional overhead of going + // from contract layer to substrate layer. // reference: https://github.com/paritytech/ink-examples/blob/b8d2caa52cf4691e0ddd7c919e4462311deb5ad0/psp22-extension/runtime/psp22-extension-example.rs#L236 - env.charge_weight(Schedule::::get().host_fn_weights.debug_message)?; + // TODO: Temporary value for testing purpose + let overhead: Weight = Runtime::WeightInfo::seal_random(); + env.charge_weight(overhead)?; // Execute the function match Config::Functions::execute(&mut env) { Ok(r) => Ok(r), diff --git a/runtime/devnet/src/lib.rs b/runtime/devnet/src/lib.rs index 3f0064ba..e43e9d8d 100644 --- a/runtime/devnet/src/lib.rs +++ b/runtime/devnet/src/lib.rs @@ -626,6 +626,10 @@ mod runtime { pub type NftFractionalization = pallet_nft_fractionalization::Pallet; #[runtime::pallet_index(52)] pub type Assets = pallet_assets::Pallet; + + // Pop API + #[runtime::pallet_index(150)] + pub type Fungibles = pallet_api::fungibles::Pallet; } #[cfg(feature = "runtime-benchmarks")] diff --git a/runtime/testnet/src/extensions.rs b/runtime/testnet/src/extensions.rs index 5a999acc..5b960785 100644 --- a/runtime/testnet/src/extensions.rs +++ b/runtime/testnet/src/extensions.rs @@ -7,7 +7,6 @@ use pallet_contracts::chain_extension::{ BufInBufOutState, ChainExtension, ChargedAmount, Environment, Ext, InitState, RetVal, }; use pallet_contracts::WeightInfo; -use pop_primitives::storage_keys::RuntimeStateKeys; use sp_core::crypto::UncheckedFrom; use sp_runtime::{traits::Dispatchable, DispatchError}; use sp_std::vec::Vec; From a6bd2fb1aad5d3c286a89b5732c6bc06d4448ed7 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Fri, 23 Aug 2024 19:28:03 +0700 Subject: [PATCH 76/76] fix: remove pallet-contract, rename pallet-revive --- Cargo.lock | 68 ++------------------------ Cargo.toml | 4 +- runtime/devnet/src/config/contracts.rs | 1 + 3 files changed, 8 insertions(+), 65 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8ee9d319..37c63293 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7546,8 +7546,8 @@ dependencies = [ "pallet-assets", "pallet-balances", "pallet-contracts-fixtures", - "pallet-contracts-proc-macro 18.0.0", - "pallet-contracts-uapi 5.0.0", + "pallet-contracts-proc-macro", + "pallet-contracts-uapi", "pallet-insecure-randomness-collective-flip", "pallet-message-queue", "pallet-proxy", @@ -7575,40 +7575,6 @@ dependencies = [ "wat", ] -[[package]] -name = "pallet-contracts" -version = "35.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e6989ac82690f981959b0d38ac6d6d52fc06bf00a035548d62b9a2e9c220376" -dependencies = [ - "bitflags 1.3.2", - "environmental", - "frame-benchmarking", - "frame-support 36.0.0", - "frame-system 36.1.0", - "impl-trait-for-tuples", - "log", - "pallet-balances", - "pallet-contracts-proc-macro 23.0.1", - "pallet-contracts-uapi 11.0.0", - "parity-scale-codec", - "paste", - "rand", - "rand_pcg", - "scale-info", - "serde", - "smallvec", - "sp-api 33.0.0", - "sp-core", - "sp-io 37.0.0", - "sp-runtime 38.0.0", - "sp-std", - "staging-xcm", - "staging-xcm-builder", - "wasm-instrument", - "wasmi 0.32.3", -] - [[package]] name = "pallet-contracts-fixtures" version = "1.0.0" @@ -7632,17 +7598,6 @@ dependencies = [ "syn 2.0.66", ] -[[package]] -name = "pallet-contracts-proc-macro" -version = "23.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94226cbd48516b7c310eb5dae8d50798c1ce73a7421dc0977c55b7fc2237a283" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.66", -] - [[package]] name = "pallet-contracts-uapi" version = "5.0.0" @@ -7654,19 +7609,6 @@ dependencies = [ "scale-info", ] -[[package]] -name = "pallet-contracts-uapi" -version = "11.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1330375dcced95509e3cca7ef6b1c3fac648df995b86d39467d082ba981dc46" -dependencies = [ - "bitflags 1.3.2", - "parity-scale-codec", - "paste", - "polkavm-derive", - "scale-info", -] - [[package]] name = "pallet-conviction-voting" version = "36.0.0" @@ -10445,7 +10387,7 @@ dependencies = [ "frame-system 36.1.0", "impl-trait-for-tuples", "log", - "pallet-contracts 35.0.0", + "pallet-contracts", "parity-scale-codec", "rand", "sp-core", @@ -10569,7 +10511,7 @@ dependencies = [ "pallet-authorship", "pallet-balances", "pallet-collator-selection", - "pallet-contracts 35.0.0", + "pallet-contracts", "pallet-message-queue", "pallet-multisig", "pallet-nft-fractionalization", @@ -10644,7 +10586,7 @@ dependencies = [ "pallet-authorship", "pallet-balances", "pallet-collator-selection", - "pallet-contracts 35.0.0", + "pallet-contracts", "pallet-message-queue", "pallet-multisig", "pallet-nft-fractionalization", diff --git a/Cargo.toml b/Cargo.toml index 8533cb49..46739fbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,7 +58,7 @@ array-bytes = { version = "6.2.2", default-features = false } assert_matches = { version = "1.5.0" } bitflags = { version = "1.3.2" } environmental = { version = "1.1.4", default-features = false } -pallet-revive = { path = "pallets/contracts", default-features = false } +pallet-contracts = { path = "pallets/contracts", default-features = false } pallet-contracts-fixtures = { path = "pallets/contracts/fixtures", default-features = false } pallet-contracts-mock-network = { default-features = false, path = "pallets/contracts/mock-network" } pallet-contracts-proc-macro = { path = "pallets/contracts/proc-macro", default-features = false } @@ -104,7 +104,7 @@ pallet-assets = { version = "37.0.0", default-features = false } pallet-aura = { version = "35.0.0", default-features = false } pallet-authorship = { version = "36.0.0", default-features = false } pallet-balances = { version = "37.0.0", default-features = false } -pallet-contracts = { version = "35.0.0", default-features = false } +# pallet-contracts = { version = "35.0.0", default-features = false } pallet-message-queue = { version = "39.0.0", default-features = false } pallet-multisig = { version = "36.0.0", default-features = false } pallet-nft-fractionalization = { version = "18.0.0", default-features = false } diff --git a/runtime/devnet/src/config/contracts.rs b/runtime/devnet/src/config/contracts.rs index 73bfd5cd..f3a721be 100644 --- a/runtime/devnet/src/config/contracts.rs +++ b/runtime/devnet/src/config/contracts.rs @@ -85,6 +85,7 @@ impl pallet_contracts::Config for Runtime { type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent; type MaxDelegateDependencies = ConstU32<32>; type RuntimeHoldReason = RuntimeHoldReason; + type MaxTransientStorageSize = (); type ApiVersion = (); type Environment = ();