From bb6b14322952b2970e65d1ef305990c040a2242c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Cerni=C4=8D?= Date: Thu, 11 Jul 2024 18:04:56 +0200 Subject: [PATCH 01/11] resolve dirty lock file & setup test coverage for pallet-storage-provider --- .vscode/settings.json | 4 +- Cargo.lock | 412 ++++++++++++++++++++++-------------------- Justfile | 6 + 3 files changed, 225 insertions(+), 197 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 7458a6a98..02b56871b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,8 @@ { + "rust-analyzer.rustfmt.extraArgs": ["+nightly"], "coverage-gutters.coverageBaseDir": "coverage", - "coverage-gutters.coverageFileNames": [ + "coverage-gutters.coverageFileNames": [ + "pallet-storage-provider.lcov.info", "pallet-market.lcov.info", "mater.lcov.info" ] diff --git a/Cargo.lock b/Cargo.lock index c2b349761..29be9aa5b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,7 +68,7 @@ dependencies = [ "cipher 0.4.4", "ctr", "ghash", - "subtle 2.5.0", + "subtle 2.6.0", ] [[package]] @@ -167,9 +167,9 @@ dependencies = [ [[package]] name = "anstyle-query" -version = "1.0.3" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" dependencies = [ "windows-sys 0.52.0", ] @@ -552,7 +552,7 @@ dependencies = [ "proc-macro2", "quote", "syn 1.0.109", - "synstructure", + "synstructure 0.12.6", ] [[package]] @@ -663,7 +663,7 @@ dependencies = [ "futures-io", "futures-lite 2.3.0", "parking", - "polling 3.7.1", + "polling 3.7.2", "rustix 0.38.34", "slab", "tracing", @@ -751,9 +751,9 @@ dependencies = [ [[package]] name = "async-signal" -version = "0.2.7" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "329972aa325176e89114919f2a80fdae4f4c040f66a370b1a1159c6c0f94e7aa" +checksum = "794f185324c2f00e771cd9f1ae8b5ac68be2ca7abb129a87afd6e86d228bc54d" dependencies = [ "async-io 2.3.3", "async-lock 3.4.0", @@ -851,7 +851,7 @@ dependencies = [ "http 1.1.0", "http-body 1.0.0", "http-body-util", - "hyper 1.3.1", + "hyper 1.4.1", "hyper-util", "itoa", "matchit", @@ -908,16 +908,16 @@ dependencies = [ [[package]] name = "backtrace" -version = "0.3.72" +version = "0.3.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17c6a35df3749d2e8bb1b7b21a976d82b15548788d2735b9d82f329268f71a11" +checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" dependencies = [ "addr2line 0.22.0", "cc", "cfg-if", "libc", "miniz_oxide", - "object 0.35.0", + "object 0.36.0", "rustc-demangle", ] @@ -1265,9 +1265,9 @@ checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" [[package]] name = "bytemuck" -version = "1.16.0" +version = "1.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78834c15cb5d5efe3452d58b1e8ba890dd62d21907f867f383358198e56ebca5" +checksum = "b236fc92302c97ed75b38da1f4917b5cdda4984745740f153a5d3059e48d725e" [[package]] name = "byteorder" @@ -1351,9 +1351,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.98" +version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41c270e7540d725e65ac7f1b212ac8ce349719624d7bcff99f8e2e488e8cf03f" +checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695" dependencies = [ "jobserver", "libc", @@ -1546,9 +1546,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.4" +version = "4.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +checksum = "5db83dced34638ad474f39f250d7fea9598bdd239eaced1bdf45d597da0f433f" dependencies = [ "clap_builder", "clap_derive", @@ -1556,9 +1556,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.2" +version = "4.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +checksum = "f7e204572485eb3fbf28f871612191521df159bc3e15a9f5064c66dba3a8c05f" dependencies = [ "anstream", "anstyle", @@ -1569,9 +1569,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.4" +version = "4.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +checksum = "c780290ccf4fb26629baa7a1081e68ced113f1d3ec302fa5948f1c381ebf06c6" dependencies = [ "heck 0.5.0", "proc-macro2", @@ -1581,9 +1581,9 @@ dependencies = [ [[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 = "cli-primitives" @@ -1654,7 +1654,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b34115915337defe99b2aff5c2ce6771e5fbc4079f4b506301f5cf394c8452f7" dependencies = [ "strum 0.26.2", - "strum_macros 0.26.3", + "strum_macros 0.26.4", "unicode-width", ] @@ -2013,7 +2013,7 @@ checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" dependencies = [ "generic-array 0.14.7", "rand_core 0.6.4", - "subtle 2.5.0", + "subtle 2.6.0", "zeroize", ] @@ -2045,7 +2045,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.0", ] [[package]] @@ -2653,24 +2653,23 @@ dependencies = [ "byteorder", "digest 0.9.0", "rand_core 0.5.1", - "subtle 2.5.0", + "subtle 2.6.0", "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.0", "zeroize", ] @@ -2700,9 +2699,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.122" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb497fad022245b29c2a0351df572e2d67c1046bcef2260ebc022aec81efea82" +checksum = "273dcfd3acd4e1e276af13ed2a43eea7001318823e7a726a6b3ed39b4acc0b82" dependencies = [ "cc", "cxxbridge-flags", @@ -2712,9 +2711,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.122" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9327c7f9fbd6329a200a5d4aa6f674c60ab256525ff0084b52a889d4e4c60cee" +checksum = "d8b2766fbd92be34e9ed143898fce6c572dc009de39506ed6903e5a05b68914e" dependencies = [ "cc", "codespan-reporting", @@ -2727,15 +2726,15 @@ dependencies = [ [[package]] name = "cxxbridge-flags" -version = "1.0.122" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "688c799a4a846f1c0acb9f36bb9c6272d9b3d9457f3633c7753c6057270df13c" +checksum = "839fcd5e43464614ffaa989eaf1c139ef1f0c51672a1ed08023307fa1b909ccd" [[package]] name = "cxxbridge-macro" -version = "1.0.122" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "928bc249a7e3cd554fd2e8e08a426e9670c50bbfc9a621653cfa9accc9641783" +checksum = "4b2c1c1776b986979be68bb2285da855f8d8a35851a769fca8740df7c3d07877" dependencies = [ "proc-macro2", "quote", @@ -2930,15 +2929,15 @@ dependencies = [ [[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.66", ] [[package]] @@ -2974,7 +2973,7 @@ dependencies = [ "block-buffer 0.10.4", "const-oid", "crypto-common", - "subtle 2.5.0", + "subtle 2.6.0", ] [[package]] @@ -3069,7 +3068,7 @@ dependencies = [ "regex", "syn 2.0.66", "termcolor", - "toml 0.8.13", + "toml 0.8.14", "walkdir", ] @@ -3172,12 +3171,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 2.2.3", "rand_core 0.6.4", "serde", "sha2 0.10.8", - "subtle 2.5.0", + "subtle 2.6.0", "zeroize", ] @@ -3187,7 +3186,7 @@ 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 2.2.3", "hashbrown 0.14.5", "hex", @@ -3218,7 +3217,7 @@ dependencies = [ "rand_core 0.6.4", "sec1", "serdect", - "subtle 2.5.0", + "subtle 2.6.0", "zeroize", ] @@ -3263,18 +3262,18 @@ 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", @@ -3409,13 +3408,14 @@ 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.66", @@ -3489,7 +3489,7 @@ 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.0", ] [[package]] @@ -3511,6 +3511,16 @@ version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" 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" version = "0.1.6" @@ -4261,7 +4271,7 @@ checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" dependencies = [ "ff", "rand_core 0.6.4", - "subtle 2.5.0", + "subtle 2.6.0", ] [[package]] @@ -4375,6 +4385,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" @@ -4516,9 +4532,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" @@ -4534,9 +4550,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "hyper" -version = "0.14.28" +version = "0.14.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" +checksum = "f361cde2f109281a220d4307746cdfd5ee3f410da58a70377762396775634b33" dependencies = [ "bytes", "futures-channel", @@ -4558,9 +4574,9 @@ dependencies = [ [[package]] name = "hyper" -version = "1.3.1" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe575dd17d0862a9a33781c8c4696a55c320909004a67a00fb286ba8b1bc496d" +checksum = "50dfd22e0e76d0f662d429a5f80fcaf3855009297eab6a0a9f8543834744ba05" dependencies = [ "bytes", "futures-channel", @@ -4583,7 +4599,7 @@ checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" dependencies = [ "futures-util", "http 0.2.12", - "hyper 0.14.28", + "hyper 0.14.29", "log", "rustls 0.21.12", "rustls-native-certs 0.6.3", @@ -4593,15 +4609,15 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b875924a60b96e5d7b9ae7b066540b1dd1cbd90d1828f54c92e02a283351c56" +checksum = "3ab92f4f49ee4fb4f997c784b7a2e0fa70050211e0b6a287f898c3c9785ca956" dependencies = [ "bytes", "futures-util", "http 1.1.0", "http-body 1.0.0", - "hyper 1.3.1", + "hyper 1.4.1", "pin-project-lite 0.2.14", "tokio", ] @@ -4726,18 +4742,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", @@ -4819,7 +4835,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", ] @@ -4877,7 +4893,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", ] @@ -4997,7 +5013,7 @@ dependencies = [ "beef", "futures-timer", "futures-util", - "hyper 0.14.28", + "hyper 0.14.29", "jsonrpsee-types", "parking_lot 0.12.3", "pin-project", @@ -5018,7 +5034,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ccf93fc4a0bfe05d851d37d7c32b7f370fe94336b52a2f0efc5f1981895c2e5" dependencies = [ "async-trait", - "hyper 0.14.28", + "hyper 0.14.29", "hyper-rustls", "jsonrpsee-core", "jsonrpsee-types", @@ -5052,7 +5068,7 @@ checksum = "12d8b6a9674422a8572e0b0abb12feeb3f2aeda86528c80d0350c2bd0923ab41" dependencies = [ "futures-util", "http 0.2.12", - "hyper 0.14.28", + "hyper 0.14.29", "jsonrpsee-core", "jsonrpsee-types", "pin-project", @@ -5648,7 +5664,7 @@ checksum = "5be9b9bb642d8522a44d533eab56c16c738301965504753b03ad1de3425d5451" dependencies = [ "crunchy", "digest 0.9.0", - "subtle 2.5.0", + "subtle 2.6.0", ] [[package]] @@ -5849,9 +5865,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", @@ -5859,9 +5875,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", @@ -5990,9 +6006,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" @@ -6076,9 +6092,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "miniz_oxide" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87dfd01fe195c66b572b37921ad8803d010623c0aca821bea2302239d155cdae" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" dependencies = [ "adler", ] @@ -6105,7 +6121,7 @@ dependencies = [ "bitflags 1.3.2", "blake2 0.10.6", "c2-chacha", - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "either", "hashlink", "lioness", @@ -6114,7 +6130,7 @@ dependencies = [ "rand 0.8.5", "rand_chacha 0.3.1", "rand_distr", - "subtle 2.5.0", + "subtle 2.6.0", "thiserror", "zeroize", ] @@ -6265,7 +6281,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 0.7.2", @@ -6282,7 +6298,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 0.7.2", @@ -6322,16 +6338,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]] @@ -6347,16 +6363,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.66", + "synstructure 0.13.1", ] [[package]] @@ -6381,9 +6397,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", @@ -6655,7 +6671,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", ] @@ -6682,9 +6698,9 @@ dependencies = [ [[package]] name = "object" -version = "0.35.0" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8ec7ab813848ba4522158d5517a6093db1ded27575b070f4177b8d12b41db5e" +checksum = "576dfe1fc8f9df304abb159d767a29d0476f7750fbf8aa7ad07816004a207434" dependencies = [ "memchr", ] @@ -6756,9 +6772,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-src" -version = "300.3.0+3.3.0" +version = "300.3.1+3.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eba8804a1c5765b18c4b3f907e6897ebabeedebc9830e1a0046c4a4cf44663e1" +checksum = "7259953d42a81bf137fbbd73bd30a8e1914d6dce43c2b90ed575783a22608b91" dependencies = [ "cc", ] @@ -8192,7 +8208,7 @@ checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ "cfg-if", "libc", - "redox_syscall 0.5.1", + "redox_syscall 0.5.2", "smallvec", "windows-targets 0.52.5", ] @@ -8211,7 +8227,7 @@ checksum = "346f04948ba92c43e8469c1ee6736c7563d71012b17d40745260fe106aac2166" dependencies = [ "base64ct", "rand_core 0.6.4", - "subtle 2.5.0", + "subtle 2.6.0", ] [[package]] @@ -8371,12 +8387,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 = "plotters" version = "0.3.6" @@ -9762,13 +9772,13 @@ dependencies = [ [[package]] name = "polling" -version = "3.7.1" +version = "3.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e6a007746f34ed64099e88783b0ae369eaa3da6392868ba262e2af9b8fbaea1" +checksum = "a3ed00ed3fbf728b5816498ecd316d1716eecaced9c0c8d2c5a6740ca214985b" dependencies = [ "cfg-if", "concurrent-queue", - "hermit-abi", + "hermit-abi 0.4.0", "pin-project-lite 0.2.14", "rustix 0.38.34", "tracing", @@ -9856,21 +9866,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.66", -] - [[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", @@ -9928,12 +9928,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]] @@ -10059,7 +10068,7 @@ dependencies = [ "log", "multimap", "petgraph", - "prettyplease 0.1.11", + "prettyplease 0.1.25", "prost 0.11.9", "prost-types 0.11.9", "regex", @@ -10433,9 +10442,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "469052894dcb553421e483e4209ee581a45100d31b4018de03e5a7ad86374a7e" +checksum = "c82cf8cff14456045f55ec4241383baeff27af886adb72ffb2162f99911de0fd" dependencies = [ "bitflags 2.5.0", ] @@ -10510,14 +10519,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]] @@ -10531,13 +10540,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]] @@ -10548,9 +10557,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" @@ -10569,7 +10578,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.0", ] [[package]] @@ -10907,7 +10916,7 @@ dependencies = [ "ring 0.17.8", "rustls-pki-types", "rustls-webpki 0.102.4", - "subtle 2.5.0", + "subtle 2.6.0", "zeroize", ] @@ -11029,9 +11038,9 @@ 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", ] @@ -11842,7 +11851,7 @@ dependencies = [ "fnv", "futures", "futures-timer", - "hyper 0.14.28", + "hyper 0.14.29", "hyper-rustls", "libp2p", "log", @@ -11937,7 +11946,7 @@ dependencies = [ "futures", "governor", "http 0.2.12", - "hyper 0.14.28", + "hyper 0.14.29", "ip_network", "jsonrpsee", "log", @@ -12288,7 +12297,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82ab7e60e2d9c8d47105f44527b26f04418e5e624ffc034f6b4a86c0ba19c5bf" dependencies = [ "darling 0.14.4", - "proc-macro-crate 1.3.1", + "proc-macro-crate 1.1.3", "proc-macro2", "quote", "syn 1.0.109", @@ -12409,13 +12418,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.0", "zeroize", ] @@ -12479,7 +12488,7 @@ dependencies = [ "generic-array 0.14.7", "pkcs8", "serdect", - "subtle 2.5.0", + "subtle 2.6.0", "zeroize", ] @@ -13090,12 +13099,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.0", ] [[package]] @@ -13404,7 +13413,7 @@ dependencies = [ [[package]] name = "sp-crypto-ec-utils" version = "0.10.0" -source = "git+https://github.com/paritytech/polkadot-sdk#f66e693a6befef0956a3129254fbe568247c9c57" +source = "git+https://github.com/paritytech/polkadot-sdk#74decbbdf22a7b109209448307563c6f3d62abac" dependencies = [ "ark-bls12-377", "ark-bls12-377-ext", @@ -13480,7 +13489,7 @@ dependencies = [ [[package]] name = "sp-debug-derive" version = "14.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#f66e693a6befef0956a3129254fbe568247c9c57" +source = "git+https://github.com/paritytech/polkadot-sdk#74decbbdf22a7b109209448307563c6f3d62abac" dependencies = [ "proc-macro2", "quote", @@ -13500,7 +13509,7 @@ dependencies = [ [[package]] name = "sp-externalities" version = "0.25.0" -source = "git+https://github.com/paritytech/polkadot-sdk#f66e693a6befef0956a3129254fbe568247c9c57" +source = "git+https://github.com/paritytech/polkadot-sdk#74decbbdf22a7b109209448307563c6f3d62abac" dependencies = [ "environmental", "parity-scale-codec", @@ -13716,7 +13725,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface" version = "24.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#f66e693a6befef0956a3129254fbe568247c9c57" +source = "git+https://github.com/paritytech/polkadot-sdk#74decbbdf22a7b109209448307563c6f3d62abac" dependencies = [ "bytes", "impl-trait-for-tuples", @@ -13748,7 +13757,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface-proc-macro" version = "17.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#f66e693a6befef0956a3129254fbe568247c9c57" +source = "git+https://github.com/paritytech/polkadot-sdk#74decbbdf22a7b109209448307563c6f3d62abac" dependencies = [ "Inflector", "expander", @@ -13811,7 +13820,7 @@ version = "10.0.0" source = "git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-v1.13.0#d5160c1d567cc73c7df6c816d41e21aa3adb188d" dependencies = [ "aes-gcm", - "curve25519-dalek 4.1.2", + "curve25519-dalek 4.1.3", "ed25519-dalek 2.1.1", "hkdf", "parity-scale-codec", @@ -13837,7 +13846,7 @@ source = "git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-v1.13.0#d5 [[package]] name = "sp-std" version = "14.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#f66e693a6befef0956a3129254fbe568247c9c57" +source = "git+https://github.com/paritytech/polkadot-sdk#74decbbdf22a7b109209448307563c6f3d62abac" [[package]] name = "sp-storage" @@ -13854,7 +13863,7 @@ dependencies = [ [[package]] name = "sp-storage" version = "19.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#f66e693a6befef0956a3129254fbe568247c9c57" +source = "git+https://github.com/paritytech/polkadot-sdk#74decbbdf22a7b109209448307563c6f3d62abac" dependencies = [ "impl-serde", "parity-scale-codec", @@ -13889,7 +13898,7 @@ dependencies = [ [[package]] name = "sp-tracing" version = "16.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#f66e693a6befef0956a3129254fbe568247c9c57" +source = "git+https://github.com/paritytech/polkadot-sdk#74decbbdf22a7b109209448307563c6f3d62abac" dependencies = [ "parity-scale-codec", "tracing", @@ -13986,7 +13995,7 @@ dependencies = [ [[package]] name = "sp-wasm-interface" version = "20.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#f66e693a6befef0956a3129254fbe568247c9c57" +source = "git+https://github.com/paritytech/polkadot-sdk#74decbbdf22a7b109209448307563c6f3d62abac" dependencies = [ "impl-trait-for-tuples", "log", @@ -14197,7 +14206,7 @@ dependencies = [ "bitflags 1.3.2", "byteorder", "keccak", - "subtle 2.5.0", + "subtle 2.6.0", "zeroize", ] @@ -14225,7 +14234,7 @@ version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d8cec3501a5194c432b2b7976db6b7d10ec95c253208b45f83f7136aa985e29" dependencies = [ - "strum_macros 0.26.3", + "strum_macros 0.26.4", ] [[package]] @@ -14243,9 +14252,9 @@ dependencies = [ [[package]] name = "strum_macros" -version = "0.26.3" +version = "0.26.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7993a8e3a9e88a00351486baae9522c91b123a088f76469e5bd5cc17198ea87" +checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" dependencies = [ "heck 0.5.0", "proc-macro2", @@ -14295,7 +14304,7 @@ name = "substrate-prometheus-endpoint" version = "0.17.0" source = "git+https://github.com/paritytech/polkadot-sdk?tag=polkadot-v1.13.0#d5160c1d567cc73c7df6c816d41e21aa3adb188d" dependencies = [ - "hyper 0.14.28", + "hyper 0.14.29", "log", "prometheus", "thiserror", @@ -14333,7 +14342,7 @@ dependencies = [ "sp-maybe-compressed-blob", "strum 0.26.2", "tempfile", - "toml 0.8.13", + "toml 0.8.14", "walkdir", "wasm-opt", ] @@ -14346,9 +14355,9 @@ checksum = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" [[package]] name = "subtle" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" +checksum = "0d0208408ba0c3df17ed26eb06992cb1a1268d41b2c0e12e65203fbe3972cee5" [[package]] name = "subtle-ng" @@ -14553,6 +14562,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.66", +] + [[package]] name = "system-configuration" version = "0.5.1" @@ -14890,14 +14910,14 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.13" +version = "0.8.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4e43f8cc456c9704c851ae29c67e17ef65d2c30017c17a9765b89c382dc8bba" +checksum = "6f49eb2ab21d2f26bd6db7bf383edc527a7ebaee412d17af4d40fdccd442f335" dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.13", + "toml_edit 0.22.14", ] [[package]] @@ -14911,9 +14931,9 @@ dependencies = [ [[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", "toml_datetime", @@ -14933,15 +14953,15 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.13" +version = "0.22.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c127785850e8c20836d49732ae6abfa47616e60bf9d9f57c43c250361a9db96c" +checksum = "f21c7aaf97f1bd9ca9d4f9e73b0a6c74bd5afef56f2bc931943a6e1c37e04e38" dependencies = [ "indexmap 2.2.6", "serde", "serde_spanned", "toml_datetime", - "winnow 0.6.9", + "winnow 0.6.13", ] [[package]] @@ -15307,9 +15327,9 @@ dependencies = [ [[package]] name = "unicode-width" -version = "0.1.12" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68f5e5f3158ecfd4b8ff6fe086db7c8467a2dfdac97fe420f2b7c4aa97af66d6" +checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" [[package]] name = "unicode-xid" @@ -15324,7 +15344,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" dependencies = [ "crypto-common", - "subtle 2.5.0", + "subtle 2.6.0", ] [[package]] @@ -15360,9 +15380,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", @@ -15377,9 +15397,9 @@ checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" [[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 = "uuid" @@ -16027,9 +16047,9 @@ dependencies = [ [[package]] name = "wide" -version = "0.7.21" +version = "0.7.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd8dc749a1b03f3c255a3064a4f5c0ee5ed09b7c6bc6d4525d31f779cd74d7fc" +checksum = "8a040b111774ab63a19ef46bbc149398ab372b4ccdcfd719e9814dbd7dfd76c8" dependencies = [ "bytemuck", "safe_arch", @@ -16331,9 +16351,9 @@ dependencies = [ [[package]] name = "winnow" -version = "0.6.9" +version = "0.6.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86c949fede1d13936a99f14fafd3e76fd642b556dd2ce96287fbe2e0151bfac6" +checksum = "59b5e5f6c299a3c7890b876a2a587f3115162487e704907d9b6cd29473052ba1" dependencies = [ "memchr", ] @@ -16374,7 +16394,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", @@ -16550,9 +16570,9 @@ dependencies = [ [[package]] name = "zstd-sys" -version = "2.0.10+zstd.1.5.6" +version = "2.0.11+zstd.1.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c253a4914af5bafc8fa8c86ee400827e83cf6ec01195ec1f1ed8441bf00d65aa" +checksum = "75652c55c0b6f3e6f12eb786fe1bc960396bf05a1eb3bf1f3691c3610ac2e6d4" dependencies = [ "cc", "pkg-config", diff --git a/Justfile b/Justfile index 92746e099..5d344f544 100644 --- a/Justfile +++ b/Justfile @@ -15,6 +15,12 @@ release: lint testnet: release zombienet -p native spawn zombienet/local-testnet.toml +pallet-storage-provider-coverage: + mkdir -p coverage + cargo llvm-cov -p pallet-storage-provider --ignore-filename-regex "(mock|test)" + cargo llvm-cov -p pallet-storage-provider report --ignore-filename-regex "(mock|test)" --html --output-dir coverage/pallet-storage-provider + cargo llvm-cov -p pallet-storage-provider report --ignore-filename-regex "(mock|test)" --lcov --output-path coverage/pallet-storage-provider.lcov.info + # Must be in sync with .vscode/settings.json and have extension Coverage Gutters to display it in VS Code. market-coverage: mkdir -p coverage From 07ee2cb1ca9911ec91823916ec92a2851b03828f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Cerni=C4=8D?= Date: Thu, 11 Jul 2024 18:04:57 +0200 Subject: [PATCH 02/11] tests: storage-provider pallet --- pallets/storage-provider/src/lib.rs | 11 +- pallets/storage-provider/src/mock.rs | 7 +- pallets/storage-provider/src/proofs.rs | 1 + .../storage-provider/src/storage_provider.rs | 1 + pallets/storage-provider/src/test.rs | 1025 ++++++++++++----- primitives/proofs/src/traits.rs | 3 + primitives/proofs/src/types.rs | 2 + 7 files changed, 781 insertions(+), 269 deletions(-) diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index 205562e75..e1bb05e18 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -165,8 +165,8 @@ pub mod pallet { InvalidCid, /// Emitted when a sector fails to activate CouldNotActivateSector, - /// Emitted when a prove commit is sent after the dealine - /// These precommits will be cleaned up in the hook + /// Emitted when a prove commit is sent after the deadline. + /// These pre-commits will be cleaned up in the hook ProveCommitAfterDeadline, } @@ -258,7 +258,7 @@ pub mod pallet { Ok(()) } - /// Allows the SP to submit proof for their precomitted sectors. + /// Allows the storage providers to submit proof for their pre-committed sectors. // TODO(@aidan46, no-ref, 2024-06-24): Add functionality to allow for batch pre commit // TODO(@aidan46, no-ref, 2024-06-24): Actually check proof, currently the proof validation is stubbed out. pub fn prove_commit_sector( @@ -269,10 +269,14 @@ pub mod pallet { let sp = StorageProviders::::try_get(&owner) .map_err(|_| Error::::StorageProviderNotFound)?; let sector_number = sector.sector_number; + + // TODO(no-ref,@cernicc,11/07/2024): This check can be removed. The + // sector number is already checked in the pre-commit ensure!( sector_number <= SECTORS_MAX.into(), Error::::InvalidSector ); + let precommit = sp .get_pre_committed_sector(sector_number) .map_err(|_| Error::::InvalidSector)?; @@ -360,6 +364,7 @@ pub mod pallet { ); Ok(()) } + /// Calculate the required pre commit deposit amount fn calculate_pre_commit_deposit() -> BalanceOf { 1u32.into() // TODO(@aidan46, #106, 2024-06-24): Set a logical value or calculation diff --git a/pallets/storage-provider/src/mock.rs b/pallets/storage-provider/src/mock.rs index 60cd6dbb9..7f3e953e1 100644 --- a/pallets/storage-provider/src/mock.rs +++ b/pallets/storage-provider/src/mock.rs @@ -104,10 +104,13 @@ pub fn account(name: &str) -> AccountIdOf { pub const ALICE: &'static str = "//Alice"; pub const BOB: &'static str = "//Bob"; +pub const CHARLIE: &'static str = "//Charlie"; + +/// Initial funds of all accounts. pub const INITIAL_FUNDS: u64 = 100; // Build genesis storage according to the mock runtime. -pub fn new_test_ext() -> sp_io::TestExternalities { +pub fn new_test_ext(starting_block_number: u64) -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::::default() .build_storage() .unwrap() @@ -121,7 +124,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities { .assimilate_storage(&mut t) .unwrap(); let mut ext = sp_io::TestExternalities::new(t); - ext.execute_with(|| System::set_block_number(1)); + ext.execute_with(|| System::set_block_number(starting_block_number)); ext } diff --git a/pallets/storage-provider/src/proofs.rs b/pallets/storage-provider/src/proofs.rs index a8109c0be..a0d90ac06 100644 --- a/pallets/storage-provider/src/proofs.rs +++ b/pallets/storage-provider/src/proofs.rs @@ -12,6 +12,7 @@ pub struct PoStProof { pub proof_bytes: BoundedVec>, // Arbitrary length } +/// Error type for proof operations. #[derive(Debug)] pub enum ProofError { Conversion, diff --git a/pallets/storage-provider/src/storage_provider.rs b/pallets/storage-provider/src/storage_provider.rs index 0eaa49ea9..8fc94c0c0 100644 --- a/pallets/storage-provider/src/storage_provider.rs +++ b/pallets/storage-provider/src/storage_provider.rs @@ -119,6 +119,7 @@ where } } +/// Errors that can occur while interacting with the storage provider state. #[derive(RuntimeDebug)] pub enum StorageProviderError { /// Happens when an SP tries to pre-commit more sectors than SECTOR_MAX. diff --git a/pallets/storage-provider/src/test.rs b/pallets/storage-provider/src/test.rs index 240303e54..da1ef5206 100644 --- a/pallets/storage-provider/src/test.rs +++ b/pallets/storage-provider/src/test.rs @@ -19,284 +19,781 @@ use crate::{ #[test] fn initial_state() { - new_test_ext().execute_with(|| { + new_test_ext(1).execute_with(|| { assert!(!StorageProviders::::contains_key(account(ALICE))); assert!(!StorageProviders::::contains_key(account(BOB))); }) } -/// Tests if storage provider registration is successful. -#[test] -fn register_sp() { - new_test_ext().execute_with(|| { - let peer_id = "storage_provider_1".as_bytes().to_vec(); - let peer_id = BoundedVec::try_from(peer_id).unwrap(); - let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; - let expected_sector_size = window_post_type.sector_size(); - let expected_partition_sectors = window_post_type.window_post_partitions_sector(); - let expected_sp_info = StorageProviderInfo::new(peer_id.clone(), window_post_type); - // Register BOB as a storage provider. - assert_ok!(StorageProvider::register_storage_provider( - RuntimeOrigin::signed(account(BOB)), - peer_id.clone(), - window_post_type, - )); - assert!(StorageProviders::::contains_key(account(BOB))); - // `unwrap()` should be safe because of the above check. - let sp_bob = StorageProviders::::get(account(BOB)).unwrap(); - // Check that storage provider information is correct. - assert_eq!(sp_bob.info.peer_id, peer_id); - assert_eq!(sp_bob.info.window_post_proof_type, window_post_type); - assert_eq!(sp_bob.info.sector_size, expected_sector_size); - assert_eq!( - sp_bob.info.window_post_partition_sectors, - expected_partition_sectors - ); - // Check that pre commit sectors are empty. - assert!(sp_bob.pre_committed_sectors.is_empty()); - // Check that no pre commit deposit is made - assert_eq!(sp_bob.pre_commit_deposits, 0); - // Check that sectors are empty. - assert!(sp_bob.sectors.is_empty()); - // Check that the event triggered - assert_eq!( - events(), - [RuntimeEvent::StorageProvider( - Event::::StorageProviderRegistered { - owner: account(BOB), - info: expected_sp_info, - }, - )] - ); - }) -} +mod storage_provider_registration { + use sp_runtime::DispatchError; -/// Check that double registration fails -#[test] -fn double_register_sp() { - new_test_ext().execute_with(|| { - let peer_id = "storage_provider_1".as_bytes().to_vec(); - let peer_id = BoundedVec::try_from(peer_id).unwrap(); - let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; - // Register BOB as a storage provider. - assert_ok!(StorageProvider::register_storage_provider( - RuntimeOrigin::signed(account(BOB)), - peer_id.clone(), - window_post_type, - )); - assert!(StorageProviders::::contains_key(account(BOB))); - // Try to register BOB again. Should fail - assert_noop!( - StorageProvider::register_storage_provider( + use super::*; + + /// Tests if storage provider registration is successful. + #[test] + fn successful_registration() { + new_test_ext(1).execute_with(|| { + let peer_id = "storage_provider_1".as_bytes().to_vec(); + let peer_id = BoundedVec::try_from(peer_id).unwrap(); + let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; + let expected_sector_size = window_post_type.sector_size(); + let expected_partition_sectors = window_post_type.window_post_partitions_sector(); + let expected_sp_info = StorageProviderInfo::new(peer_id.clone(), window_post_type); + + // Register BOB as a storage provider. + assert_ok!(StorageProvider::register_storage_provider( RuntimeOrigin::signed(account(BOB)), peer_id.clone(), window_post_type, - ), - Error::::StorageProviderExists - ); - }); + )); + assert!(StorageProviders::::contains_key(account(BOB))); + + // `unwrap()` should be safe because of the above check. + let sp_bob = StorageProviders::::get(account(BOB)).unwrap(); + // Check that storage provider information is correct. + assert_eq!(sp_bob.info.peer_id, peer_id); + assert_eq!(sp_bob.info.window_post_proof_type, window_post_type); + assert_eq!(sp_bob.info.sector_size, expected_sector_size); + assert_eq!( + sp_bob.info.window_post_partition_sectors, + expected_partition_sectors + ); + // Check that pre commit sectors are empty. + assert!(sp_bob.pre_committed_sectors.is_empty()); + // Check that no pre commit deposit is made + assert_eq!(sp_bob.pre_commit_deposits, 0); + // Check that sectors are empty. + assert!(sp_bob.sectors.is_empty()); + // Check that the event triggered + assert_eq!( + events(), + [RuntimeEvent::StorageProvider( + Event::::StorageProviderRegistered { + owner: account(BOB), + info: expected_sp_info, + }, + )] + ); + }) + } + + #[test] + fn fails_should_be_signed() { + new_test_ext(1).execute_with(|| { + let peer_id = "storage_provider_1".as_bytes().to_vec(); + let peer_id = BoundedVec::try_from(peer_id).unwrap(); + let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; + + assert_noop!( + StorageProvider::register_storage_provider( + RuntimeOrigin::none(), + peer_id.clone(), + window_post_type, + ), + DispatchError::BadOrigin + ); + }); + } + + #[test] + fn fails_double_register() { + new_test_ext(1).execute_with(|| { + let peer_id = "storage_provider_1".as_bytes().to_vec(); + let peer_id = BoundedVec::try_from(peer_id).unwrap(); + let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; + // Register BOB as a storage provider. + assert_ok!(StorageProvider::register_storage_provider( + RuntimeOrigin::signed(account(BOB)), + peer_id.clone(), + window_post_type, + )); + assert!(StorageProviders::::contains_key(account(BOB))); + // Try to register BOB again. Should fail + assert_noop!( + StorageProvider::register_storage_provider( + RuntimeOrigin::signed(account(BOB)), + peer_id.clone(), + window_post_type, + ), + Error::::StorageProviderExists + ); + }); + } } -#[test] -fn pre_commit_sector() { - new_test_ext().execute_with(|| { - let peer_id = "storage_provider_1".as_bytes().to_vec(); - let peer_id = BoundedVec::try_from(peer_id).unwrap(); - let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; - // Register ALICE as a storage provider. - assert_ok!(StorageProvider::register_storage_provider( - RuntimeOrigin::signed(account(ALICE)), - peer_id.clone(), - window_post_type, - )); - assert!(StorageProviders::::contains_key(account(ALICE))); - // Check that the event triggered - assert_eq!( - events(), - [RuntimeEvent::StorageProvider( - Event::::StorageProviderRegistered { - owner: account(ALICE), - info: StorageProviderInfo::new(peer_id, window_post_type), - }, - )] - ); - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: 1, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0, 1], - expiration: YEARS, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; - // Check starting balance - assert_eq!(Balances::free_balance(account(ALICE)), 100); - // Run pre commit extrinsic - StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account(ALICE)), sector.clone()) +mod pre_commit_sector { + use sp_runtime::DispatchError; + + use crate::sector::SECTORS_MAX; + + use super::*; + + #[test] + fn successfully_precommited() { + new_test_ext(1).execute_with(|| { + // Register ALICE as a storage provider. + let account = account(ALICE); + register_storage_provider(account.clone()); + + // Sector to be pre-committed. + let sector = SectorPreCommitInfo { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number: 1, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0, 1], + expiration: YEARS, + unsealed_cid: cid_of("unsealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + // Check starting balance + assert_eq!(Balances::free_balance(account.clone()), 100); + // Run pre commit extrinsic + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account.clone()), + sector.clone(), + ) .expect("Pre commit failed"); - // Check that the event triggered - assert_eq!( - events(), - [ - RuntimeEvent::Balances(pallet_balances::Event::::Reserved { - who: account(ALICE), - amount: 1 - },), - RuntimeEvent::StorageProvider(Event::::SectorPreCommitted { - owner: account(ALICE), - sector: sector.clone(), - }) - ] - ); - let sp_alice = StorageProviders::::get(account(ALICE)) - .expect("SP Alice should be present because of the pre-check"); - - assert!(sp_alice.sectors.is_empty()); // not yet proven - assert!(!sp_alice.pre_committed_sectors.is_empty()); - assert_eq!(sp_alice.pre_commit_deposits, 1); - assert_eq!(Balances::free_balance(account(ALICE)), 99); - }); + // Check that the event triggered + assert_eq!( + events(), + [ + RuntimeEvent::Balances(pallet_balances::Event::::Reserved { + who: account.clone(), + amount: 1 + },), + RuntimeEvent::StorageProvider(Event::::SectorPreCommitted { + owner: account.clone(), + sector: sector.clone(), + }) + ] + ); + let sp_alice = StorageProviders::::get(account.clone()) + .expect("SP Alice should be present because of the pre-check"); + + assert!(sp_alice.sectors.is_empty()); // not yet proven + assert!(!sp_alice.pre_committed_sectors.is_empty()); + assert_eq!(sp_alice.pre_commit_deposits, 1); + assert_eq!(Balances::free_balance(account), 99); + }); + } + + #[test] + fn fails_should_be_signed() { + new_test_ext(1).execute_with(|| { + // Sector to be pre-committed + let sector = SectorPreCommitInfo { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number: SECTORS_MAX as u64 + 1, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0, 1], + expiration: YEARS, + unsealed_cid: cid_of("unsealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector(RuntimeOrigin::none(), sector.clone()), + DispatchError::BadOrigin, + ); + }); + } + + #[test] + fn fails_storage_provider_not_found() { + new_test_ext(1).execute_with(|| { + // Sector to be pre-committed + let sector = SectorPreCommitInfo { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number: 1, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0, 1], + expiration: YEARS, + unsealed_cid: cid_of("unsealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(ALICE)), + sector.clone() + ), + Error::::StorageProviderNotFound, + ); + }); + } + + #[test] + fn fails_sector_number_already_used() { + new_test_ext(1).execute_with(|| { + // Register ALICE as a storage provider. + let account = account(ALICE); + register_storage_provider(account.clone()); + + // Sector to be pre-committed + let sector = SectorPreCommitInfo { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number: 1, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0, 1], + expiration: YEARS, + unsealed_cid: cid_of("unsealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + // Run pre commit extrinsic + assert_ok!(StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account.clone()), + sector.clone() + )); + // Run same extrinsic, this should fail + assert_noop!( + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account.clone()), + sector.clone() + ), + Error::::SectorNumberAlreadyUsed, + ); + }); + } + + #[test] + fn fails_invalid_sector() { + new_test_ext(1).execute_with(|| { + // Register ALICE as a storage provider. + let account = account(ALICE); + register_storage_provider(account.clone()); + + // Sector to be pre-committed + let sector = SectorPreCommitInfo { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number: SECTORS_MAX as u64 + 1, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0, 1], + expiration: YEARS, + unsealed_cid: cid_of("unsealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account), sector.clone()), + Error::::InvalidSector, + ); + }); + } + + #[test] + fn fails_invalid_cid() { + new_test_ext(1).execute_with(|| { + // Register ALICE as a storage provider. + let account = account(ALICE); + register_storage_provider(account.clone()); + + // Sector to be pre-committed + let sector = SectorPreCommitInfo { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number: 1, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0, 1], + expiration: YEARS, + // Wrong cid set + unsealed_cid: BoundedVec::new(), + }; + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account), sector.clone()), + Error::::InvalidCid, + ); + }); + } + + #[test] + fn fails_expiration_before_activation() { + new_test_ext(1000).execute_with(|| { + // Register ALICE as a storage provider. + let account = account(ALICE); + register_storage_provider(account.clone()); + + // Sector to be pre-committed + let sector = SectorPreCommitInfo { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number: 1, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0, 1], + expiration: 1000, + unsealed_cid: cid_of("unsealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account), sector.clone()), + Error::::ExpirationBeforeActivation, + ); + }); + } + + #[test] + fn fails_expiration_too_soon() { + let current_height = 1000; + + new_test_ext(current_height).execute_with(|| { + // Register ALICE as a storage provider. + let account = account(ALICE); + register_storage_provider(account.clone()); + + // Sector to be pre-committed + let sector = SectorPreCommitInfo { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number: 1, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0, 1], + // Set expiration to be in the next block after the maximum + // allowed activation. + expiration: current_height + MaxProveCommitDuration::get() + 1, + unsealed_cid: cid_of("unsealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account), sector.clone()), + Error::::ExpirationTooSoon, + ); + }); + } + + #[test] + fn fails_expiration_too_long() { + let current_height = 1000; + + new_test_ext(current_height).execute_with(|| { + // Register ALICE as a storage provider. + let account = account(ALICE); + register_storage_provider(account.clone()); + + // Sector to be pre-committed + let sector = SectorPreCommitInfo { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number: 1, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0, 1], + // Set expiration to be in the next block after the maximum + // allowed + expiration: current_height + MaxSectorExpirationExtension::get() + 1, + unsealed_cid: cid_of("unsealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account), sector.clone()), + Error::::ExpirationTooLong, + ); + }); + } + + // TODO(no-ref,@cernicc,11/07/2024): Based on the current setup I can't get + // this test to pass. That is because the `SectorMaximumLifetime` is longer + // then the bound for `ExpirationTooLong`. Is the test wrong? is the + // implementation wrong? + // + // #[test] + // fn fails_max_sector_lifetime_exceeded() { + // let current_height = 1000; + + // new_test_ext(current_height).execute_with(|| { + // // Register ALICE as a storage provider. + // let account = account(ALICE); + // register_storage_provider(account.clone()); + + // // Sector to be pre-committed + // let sector = SectorPreCommitInfo { + // seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + // sector_number: 1, + // sealed_cid: cid_of("sealed_cid") + // .to_bytes() + // .try_into() + // .expect("hash is always 32 bytes"), + // deal_ids: bounded_vec![0, 1], + // // Set expiration to be in the next block after the maximum + // // allowed + // expiration: current_height + MaxProveCommitDuration::get() + SectorMaximumLifetime::get(), + // unsealed_cid: cid_of("unsealed_cid") + // .to_bytes() + // .try_into() + // .expect("hash is always 32 bytes"), + // }; + + // // Run pre commit extrinsic + // assert_noop!( + // StorageProvider::pre_commit_sector( + // RuntimeOrigin::signed(account), + // sector.clone() + // ), + // Error::::MaxSectorLifetimeExceeded, + // ); + // }); + // } } -#[test] -fn pre_commit_sector_fails_when_precommited_twice() { - new_test_ext().execute_with(|| { - let peer_id = "storage_provider_1".as_bytes().to_vec(); - let peer_id = BoundedVec::try_from(peer_id).unwrap(); - let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; - // Register ALICE as a storage provider. - assert_ok!(StorageProvider::register_storage_provider( - RuntimeOrigin::signed(account(ALICE)), - peer_id.clone(), - window_post_type, - )); - assert!(StorageProviders::::contains_key(account(ALICE))); - // Check that the event triggered - assert_eq!( - events(), - [RuntimeEvent::StorageProvider( - Event::::StorageProviderRegistered { - owner: account(ALICE), - info: StorageProviderInfo::new(peer_id, window_post_type), - }, - )] - ); - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: 1, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0, 1], - expiration: YEARS, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; - // Run pre commit extrinsic - assert_ok!(StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(ALICE)), - sector.clone() - )); - // Run same extrinsic, this should fail - assert_noop!( - StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(ALICE)), +mod prove_commit_sector { + use sp_runtime::DispatchError; + + use crate::sector::SECTORS_MAX; + + use super::*; + + #[test] + fn successfully_prove_sector() { + new_test_ext(1).execute_with(|| { + // Setup accounts + let storage_provider = ALICE; + let storage_client = BOB; + + // Register storage provider + register_storage_provider(account(storage_provider)); + + // Add balance to the market pallet + let _ = Market::add_balance(RuntimeOrigin::signed(account(storage_provider)), 60); + let _ = Market::add_balance(RuntimeOrigin::signed(account(storage_client)), 70); + + // Generate a deal proposal + let deal_proposal = DealProposalBuilder::default() + .client(storage_client) + .provider(storage_provider) + .signed(storage_client); + + // Publish the deal proposal + assert_ok!(Market::publish_storage_deals( + RuntimeOrigin::signed(account(storage_provider)), + bounded_vec![deal_proposal], + )); + + // Sector to be pre-committed and proven + let sector_number = 1; + + // Sector data + let sector = SectorPreCommitInfo { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0], + expiration: YEARS, + unsealed_cid: cid_of("unsealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + // Run pre commit extrinsic + assert_ok!(StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + )); + + // flush the events + events(); + + // Test prove commits + let sector = ProveCommitSector { + sector_number, + proof: cid_of("prove_commit") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + assert_ok!(StorageProvider::prove_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector + )); + assert_eq!( + events(), + [ + RuntimeEvent::Market(pallet_market::Event::DealActivated { + deal_id: 0, + client: account(storage_client), + provider: account(storage_provider) + }), + RuntimeEvent::StorageProvider(Event::::SectorProven { + owner: account(storage_provider), + sector_number: sector_number + }) + ] + ); + + // check that the funds are still locked + assert_eq!(Balances::free_balance(account(storage_provider)), 39); + let sp_state = StorageProviders::::get(account(storage_provider)) + .expect("Should be able to get providers info"); + // check that the sector has been activated + assert!(!sp_state.sectors.is_empty()); + assert!(sp_state.sectors.contains_key(§or_number)); + }); + } + + #[test] + fn fails_should_be_signed() { + new_test_ext(1).execute_with(|| { + // Sector to be pre-committed + let sector = SectorPreCommitInfo { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number: SECTORS_MAX as u64 + 1, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0, 1], + expiration: YEARS, + unsealed_cid: cid_of("unsealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector(RuntimeOrigin::none(), sector.clone()), + DispatchError::BadOrigin, + ); + }); + } + + #[test] + fn fails_storage_provider_not_found() { + new_test_ext(1).execute_with(|| { + let storage_provider = ALICE; + let storage_client = BOB; + let sector_number = 1; + + // Register storage provider + register_storage_provider(account(storage_provider)); + + // Add balance to the market pallet + let _ = Market::add_balance(RuntimeOrigin::signed(account(storage_provider)), 60); + let _ = Market::add_balance(RuntimeOrigin::signed(account(storage_client)), 70); + + // Generate a deal proposal + let deal_proposal = DealProposalBuilder::default() + .client(storage_client) + .provider(storage_provider) + .signed(storage_client); + + // Publish the deal proposal + assert_ok!(Market::publish_storage_deals( + RuntimeOrigin::signed(account(storage_provider)), + bounded_vec![deal_proposal], + )); + + // Sector data + let sector = SectorPreCommitInfo { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0], + expiration: YEARS, + unsealed_cid: cid_of("unsealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + // Run pre commit extrinsic + assert_ok!(StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), sector.clone() - ), - Error::::SectorNumberAlreadyUsed, - ); - }); + )); + + // Test prove commits + let sector = ProveCommitSector { + sector_number, + proof: cid_of("prove_commit") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + assert_noop!( + StorageProvider::prove_commit_sector( + RuntimeOrigin::signed(account(CHARLIE)), + sector + ), + Error::::StorageProviderNotFound, + ); + }); + } + + #[test] + fn fails_storage_precommit_missing() { + new_test_ext(1).execute_with(|| { + let storage_provider = ALICE; + let sector_number = 1; + + // Register storage provider + register_storage_provider(account(storage_provider)); + + // Test prove commits + let sector = ProveCommitSector { + sector_number, + proof: cid_of("prove_commit") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + assert_noop!( + StorageProvider::prove_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector + ), + Error::::InvalidSector, + ); + }); + } + + #[test] + fn fails_prove_commit_after_deadline() { + // Block number at which the precommit is made + let precommit_at_block_number = 1; + // Block number at which the prove commit is made. + let proving_at_block_number = precommit_at_block_number + MaxProveCommitDuration::get(); + + new_test_ext(precommit_at_block_number).execute_with(|| { + let storage_provider = ALICE; + let storage_client = BOB; + let sector_number = 1; + + // Register storage provider + register_storage_provider(account(storage_provider)); + + // Add balance to the market pallet + let _ = Market::add_balance(RuntimeOrigin::signed(account(storage_provider)), 60); + let _ = Market::add_balance(RuntimeOrigin::signed(account(storage_client)), 70); + + // Generate a deal proposal + let deal_proposal = DealProposalBuilder::default() + .client(storage_client) + .provider(storage_provider) + .signed(storage_client); + + // Publish the deal proposal + assert_ok!(Market::publish_storage_deals( + RuntimeOrigin::signed(account(storage_provider)), + bounded_vec![deal_proposal], + )); + + // Sector data + let sector = SectorPreCommitInfo { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0], + expiration: YEARS, + unsealed_cid: cid_of("unsealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + // Run pre commit extrinsic + assert_ok!(StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + )); + + // Test prove commits + let sector = ProveCommitSector { + sector_number, + proof: cid_of("prove_commit") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + }; + + System::set_block_number(proving_at_block_number); + assert_noop!( + StorageProvider::prove_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector + ), + Error::::ProveCommitAfterDeadline, + ); + }); + } } -#[test] -fn prove_commit_sector() { - new_test_ext().execute_with(|| { - let _ = Market::add_balance(RuntimeOrigin::signed(account(ALICE)), 60); - let _ = Market::add_balance(RuntimeOrigin::signed(account(BOB)), 70); - let deal_proposal = DealProposalBuilder::default() - .client(BOB) - .provider(ALICE) - .signed(BOB); - assert_ok!(Market::publish_storage_deals( - RuntimeOrigin::signed(account(ALICE)), - bounded_vec![deal_proposal], - )); - let peer_id = "storage_provider_1".as_bytes().to_vec(); - let peer_id = BoundedVec::try_from(peer_id).unwrap(); - let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; - let sector_number = 1; - // Register ALICE as a storage provider. - assert_ok!(StorageProvider::register_storage_provider( - RuntimeOrigin::signed(account(ALICE)), - peer_id.clone(), - window_post_type, - )); - assert!(StorageProviders::::contains_key(account(ALICE))); - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0], - expiration: YEARS, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; - // Run pre commit extrinsic - assert_ok!(StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(ALICE)), - sector.clone() - )); - // check that the deposit has been reserved. - assert_eq!(Balances::free_balance(account(ALICE)), 39); - // flush the events - events(); - // Test prove commits - let sector = ProveCommitSector { - sector_number, - proof: cid_of("prove_commit") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; - assert_ok!(StorageProvider::prove_commit_sector( - RuntimeOrigin::signed(account(ALICE)), - sector - )); - assert_eq!( - events(), - [ - RuntimeEvent::Market(pallet_market::Event::DealActivated { - deal_id: 0, - client: account(BOB), - provider: account(ALICE) - }), - RuntimeEvent::StorageProvider(Event::::SectorProven { - owner: account(ALICE), - sector_number: sector_number - }) - ] - ); - // check that the funds are still locked - assert_eq!(Balances::free_balance(account(ALICE)), 39); - let sp_state = StorageProviders::::get(account(ALICE)) - .expect("Should be able to get ALICE info"); - // check that the sector has been activated - assert!(!sp_state.sectors.is_empty()); - assert!(sp_state.sectors.contains_key(§or_number)); - }); +/// Register account as a provider. +fn register_storage_provider(account: AccountIdOf) { + let peer_id = "storage_provider_1".as_bytes().to_vec(); + let peer_id = BoundedVec::try_from(peer_id).unwrap(); + let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; + + // Register account as a storage provider. + let _ = StorageProvider::register_storage_provider( + RuntimeOrigin::signed(account), + peer_id.clone(), + window_post_type, + ); + + // Remove any events that were triggered during registration. + System::reset_events(); } /// Builder to simplify writing complex tests of [`DealProposal`]. @@ -335,12 +832,12 @@ impl Default for DealProposalBuilder { } impl DealProposalBuilder { - pub fn client(mut self, client: &'static str) -> Self { + pub fn client(mut self, client: &str) -> Self { self.client = account(client); self } - pub fn provider(mut self, provider: &'static str) -> Self { + pub fn provider(mut self, provider: &str) -> Self { self.provider = account(provider); self } @@ -360,7 +857,7 @@ impl DealProposalBuilder { } } - pub fn signed(self, by: &'static str) -> ClientDealProposalOf { + pub fn signed(self, by: &str) -> ClientDealProposalOf { let built = self.unsigned(); let signed = sign_proposal(by, built); signed diff --git a/primitives/proofs/src/traits.rs b/primitives/proofs/src/traits.rs index dd53c6236..50c86a5e4 100644 --- a/primitives/proofs/src/traits.rs +++ b/primitives/proofs/src/traits.rs @@ -7,6 +7,9 @@ use crate::types::{DealId, RegisteredSealProof, SectorNumber}; /// Size of a CID with a 512-bit multihash — i.e. the default CID size. const CID_SIZE_IN_BYTES: u32 = 64; +// TODO(no-ref,@cernicc,11/07/2024): Refactor to a new-type. SectorId should +// always be a valid CID. That should be checked before initializing it. +// /// The CID (in bytes) of a given sector. pub type SectorId = BoundedVec>; diff --git a/primitives/proofs/src/types.rs b/primitives/proofs/src/types.rs index 2bbfb6e8c..c02537433 100644 --- a/primitives/proofs/src/types.rs +++ b/primitives/proofs/src/types.rs @@ -4,6 +4,8 @@ use sp_runtime::RuntimeDebug; pub type DealId = u64; +// TODO(no-ref,@cernicc,11/07/2024): Refactor to new type. Sector number should +// always be between 0 and SECTORS_MAX (32 << 20). pub type SectorNumber = u64; #[allow(non_camel_case_types)] From dfe8cceda1aff103b372d0a5917a037b60e214bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Cerni=C4=8D?= Date: Fri, 12 Jul 2024 11:09:03 +0200 Subject: [PATCH 03/11] fix: some changes based on the pr suggestions --- pallets/storage-provider/src/lib.rs | 14 +-- pallets/storage-provider/src/mock.rs | 32 ++++++- pallets/storage-provider/src/test.rs | 138 ++++++++++++--------------- 3 files changed, 99 insertions(+), 85 deletions(-) diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index e1bb05e18..3011e6b62 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -270,19 +270,13 @@ pub mod pallet { .map_err(|_| Error::::StorageProviderNotFound)?; let sector_number = sector.sector_number; - // TODO(no-ref,@cernicc,11/07/2024): This check can be removed. The - // sector number is already checked in the pre-commit - ensure!( - sector_number <= SECTORS_MAX.into(), - Error::::InvalidSector - ); - let precommit = sp .get_pre_committed_sector(sector_number) .map_err(|_| Error::::InvalidSector)?; let current_block = >::block_number(); let prove_commit_due = precommit.pre_commit_block_number + T::MaxProveCommitDuration::get(); + ensure!( current_block < prove_commit_due, Error::::ProveCommitAfterDeadline @@ -291,8 +285,10 @@ pub mod pallet { validate_seal_proof(&precommit.info.seal_proof, sector.proof), Error::::InvalidProofType, ); + let new_sector = SectorOnChainInfo::from_pre_commit(precommit.info.clone(), current_block); + StorageProviders::::try_mutate(&owner, |maybe_sp| -> DispatchResult { let sp = maybe_sp .as_mut() @@ -303,16 +299,20 @@ pub mod pallet { .map_err(|_| Error::::CouldNotRemoveSector)?; Ok(()) })?; + let mut sector_deals = BoundedVec::new(); sector_deals .try_push(precommit.into()) .map_err(|_| Error::::CouldNotActivateSector)?; + let deal_amount = sector_deals.len(); T::Market::activate_deals(&owner, sector_deals, deal_amount > 0)?; + Self::deposit_event(Event::SectorProven { owner, sector_number, }); + Ok(()) } } diff --git a/pallets/storage-provider/src/mock.rs b/pallets/storage-provider/src/mock.rs index 7f3e953e1..df74199f4 100644 --- a/pallets/storage-provider/src/mock.rs +++ b/pallets/storage-provider/src/mock.rs @@ -1,6 +1,6 @@ use cid::Cid; use frame_support::{ - derive_impl, pallet_prelude::ConstU32, parameter_types, sp_runtime::BoundedVec, PalletId, + derive_impl, pallet_prelude::ConstU32, parameter_types, sp_runtime::BoundedVec, traits::Hooks, PalletId }; use multihash_codetable::{Code, MultihashDigest}; use sp_core::Pair; @@ -109,12 +109,21 @@ pub const CHARLIE: &'static str = "//Charlie"; /// Initial funds of all accounts. pub const INITIAL_FUNDS: u64 = 100; +// Build genesis storage according to the mock runtime. Specify the starting +// block number. +pub fn new_test_ext_with_block(starting_block_number: u64) -> sp_io::TestExternalities { + let mut ext = new_test_ext(); + ext.execute_with(|| System::set_block_number(starting_block_number)); + ext +} + // Build genesis storage according to the mock runtime. -pub fn new_test_ext(starting_block_number: u64) -> sp_io::TestExternalities { +pub fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::::default() .build_storage() .unwrap() .into(); + pallet_balances::GenesisConfig:: { balances: vec![ (account(ALICE), INITIAL_FUNDS), @@ -123,8 +132,9 @@ pub fn new_test_ext(starting_block_number: u64) -> sp_io::TestExternalities { } .assimilate_storage(&mut t) .unwrap(); + let mut ext = sp_io::TestExternalities::new(t); - ext.execute_with(|| System::set_block_number(starting_block_number)); + ext.execute_with(|| System::set_block_number(1)); ext } @@ -140,3 +150,19 @@ pub fn events() -> Vec { pub fn cid_of(data: &str) -> cid::Cid { Cid::new_v1(CID_CODEC, Code::Blake2b256.digest(data.as_bytes())) } + +/// Run until a particular block. +/// +/// Stolen't from: +pub fn run_to_block(n: u64) { + while System::block_number() < n { + if System::block_number() > 1 { + StorageProvider::on_finalize(System::block_number()); + System::on_finalize(System::block_number()); + } + + System::set_block_number(System::block_number() + 1); + System::on_initialize(System::block_number()); + StorageProvider::on_initialize(System::block_number()); + } +} diff --git a/pallets/storage-provider/src/test.rs b/pallets/storage-provider/src/test.rs index da1ef5206..940961d44 100644 --- a/pallets/storage-provider/src/test.rs +++ b/pallets/storage-provider/src/test.rs @@ -19,7 +19,7 @@ use crate::{ #[test] fn initial_state() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { assert!(!StorageProviders::::contains_key(account(ALICE))); assert!(!StorageProviders::::contains_key(account(BOB))); }) @@ -33,7 +33,7 @@ mod storage_provider_registration { /// Tests if storage provider registration is successful. #[test] fn successful_registration() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { let peer_id = "storage_provider_1".as_bytes().to_vec(); let peer_id = BoundedVec::try_from(peer_id).unwrap(); let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; @@ -80,7 +80,7 @@ mod storage_provider_registration { #[test] fn fails_should_be_signed() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { let peer_id = "storage_provider_1".as_bytes().to_vec(); let peer_id = BoundedVec::try_from(peer_id).unwrap(); let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; @@ -98,7 +98,7 @@ mod storage_provider_registration { #[test] fn fails_double_register() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { let peer_id = "storage_provider_1".as_bytes().to_vec(); let peer_id = BoundedVec::try_from(peer_id).unwrap(); let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; @@ -131,10 +131,10 @@ mod pre_commit_sector { #[test] fn successfully_precommited() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { // Register ALICE as a storage provider. - let account = account(ALICE); - register_storage_provider(account.clone()); + let storage_provider = ALICE; + register_storage_provider(account(storage_provider)); // Sector to be pre-committed. let sector = SectorPreCommitInfo { @@ -153,10 +153,10 @@ mod pre_commit_sector { }; // Check starting balance - assert_eq!(Balances::free_balance(account.clone()), 100); + assert_eq!(Balances::free_balance(account(storage_provider)), 100); // Run pre commit extrinsic StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account.clone()), + RuntimeOrigin::signed(account(storage_provider)), sector.clone(), ) .expect("Pre commit failed"); @@ -165,32 +165,32 @@ mod pre_commit_sector { events(), [ RuntimeEvent::Balances(pallet_balances::Event::::Reserved { - who: account.clone(), + who: account(storage_provider), amount: 1 },), RuntimeEvent::StorageProvider(Event::::SectorPreCommitted { - owner: account.clone(), + owner: account(storage_provider), sector: sector.clone(), }) ] ); - let sp_alice = StorageProviders::::get(account.clone()) + let sp_alice = StorageProviders::::get(account(storage_provider)) .expect("SP Alice should be present because of the pre-check"); assert!(sp_alice.sectors.is_empty()); // not yet proven assert!(!sp_alice.pre_committed_sectors.is_empty()); assert_eq!(sp_alice.pre_commit_deposits, 1); - assert_eq!(Balances::free_balance(account), 99); + assert_eq!(Balances::free_balance(account(storage_provider)), 99); }); } #[test] fn fails_should_be_signed() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { // Sector to be pre-committed let sector = SectorPreCommitInfo { seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: SECTORS_MAX as u64 + 1, + sector_number: 1, sealed_cid: cid_of("sealed_cid") .to_bytes() .try_into() @@ -213,7 +213,7 @@ mod pre_commit_sector { #[test] fn fails_storage_provider_not_found() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { // Sector to be pre-committed let sector = SectorPreCommitInfo { seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, @@ -243,10 +243,10 @@ mod pre_commit_sector { #[test] fn fails_sector_number_already_used() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { // Register ALICE as a storage provider. - let account = account(ALICE); - register_storage_provider(account.clone()); + let storage_provider = ALICE; + register_storage_provider(account(storage_provider)); // Sector to be pre-committed let sector = SectorPreCommitInfo { @@ -265,13 +265,13 @@ mod pre_commit_sector { }; // Run pre commit extrinsic assert_ok!(StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account.clone()), + RuntimeOrigin::signed(account(storage_provider)), sector.clone() )); // Run same extrinsic, this should fail assert_noop!( StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account.clone()), + RuntimeOrigin::signed(account(storage_provider)), sector.clone() ), Error::::SectorNumberAlreadyUsed, @@ -281,10 +281,10 @@ mod pre_commit_sector { #[test] fn fails_invalid_sector() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { // Register ALICE as a storage provider. - let account = account(ALICE); - register_storage_provider(account.clone()); + let storage_provider = ALICE; + register_storage_provider(account(storage_provider)); // Sector to be pre-committed let sector = SectorPreCommitInfo { @@ -304,7 +304,7 @@ mod pre_commit_sector { // Run pre commit extrinsic assert_noop!( - StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account), sector.clone()), + StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account(storage_provider)), sector.clone()), Error::::InvalidSector, ); }); @@ -312,10 +312,10 @@ mod pre_commit_sector { #[test] fn fails_invalid_cid() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { // Register ALICE as a storage provider. - let account = account(ALICE); - register_storage_provider(account.clone()); + let storage_provider = ALICE; + register_storage_provider(account(storage_provider)); // Sector to be pre-committed let sector = SectorPreCommitInfo { @@ -333,7 +333,7 @@ mod pre_commit_sector { // Run pre commit extrinsic assert_noop!( - StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account), sector.clone()), + StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account(storage_provider)), sector.clone()), Error::::InvalidCid, ); }); @@ -341,10 +341,10 @@ mod pre_commit_sector { #[test] fn fails_expiration_before_activation() { - new_test_ext(1000).execute_with(|| { + new_test_ext_with_block(1000).execute_with(|| { // Register ALICE as a storage provider. - let account = account(ALICE); - register_storage_provider(account.clone()); + let storage_provider = ALICE; + register_storage_provider(account(&storage_provider)); // Sector to be pre-committed let sector = SectorPreCommitInfo { @@ -364,7 +364,7 @@ mod pre_commit_sector { // Run pre commit extrinsic assert_noop!( - StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account), sector.clone()), + StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account(storage_provider)), sector.clone()), Error::::ExpirationBeforeActivation, ); }); @@ -374,10 +374,10 @@ mod pre_commit_sector { fn fails_expiration_too_soon() { let current_height = 1000; - new_test_ext(current_height).execute_with(|| { + new_test_ext_with_block(current_height).execute_with(|| { // Register ALICE as a storage provider. - let account = account(ALICE); - register_storage_provider(account.clone()); + let storage_provider = ALICE; + register_storage_provider(account(storage_provider)); // Sector to be pre-committed let sector = SectorPreCommitInfo { @@ -397,9 +397,8 @@ mod pre_commit_sector { .expect("hash is always 32 bytes"), }; - // Run pre commit extrinsic assert_noop!( - StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account), sector.clone()), + StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account(storage_provider)), sector.clone()), Error::::ExpirationTooSoon, ); }); @@ -409,10 +408,10 @@ mod pre_commit_sector { fn fails_expiration_too_long() { let current_height = 1000; - new_test_ext(current_height).execute_with(|| { + new_test_ext_with_block(current_height).execute_with(|| { // Register ALICE as a storage provider. - let account = account(ALICE); - register_storage_provider(account.clone()); + let storage_provider = ALICE; + register_storage_provider(account(storage_provider)); // Sector to be pre-committed let sector = SectorPreCommitInfo { @@ -434,7 +433,7 @@ mod pre_commit_sector { // Run pre commit extrinsic assert_noop!( - StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account), sector.clone()), + StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account(storage_provider)), sector.clone()), Error::::ExpirationTooLong, ); }); @@ -451,8 +450,8 @@ mod pre_commit_sector { // new_test_ext(current_height).execute_with(|| { // // Register ALICE as a storage provider. - // let account = account(ALICE); - // register_storage_provider(account.clone()); + // let storage_provider = account(ALICE); + // register_storage_provider(account(storage_provider)); // // Sector to be pre-committed // let sector = SectorPreCommitInfo { @@ -475,7 +474,7 @@ mod pre_commit_sector { // // Run pre commit extrinsic // assert_noop!( // StorageProvider::pre_commit_sector( - // RuntimeOrigin::signed(account), + // RuntimeOrigin::signed(account(storage_provider)), // sector.clone() // ), // Error::::MaxSectorLifetimeExceeded, @@ -493,7 +492,7 @@ mod prove_commit_sector { #[test] fn successfully_prove_sector() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { // Setup accounts let storage_provider = ALICE; let storage_client = BOB; @@ -502,8 +501,8 @@ mod prove_commit_sector { register_storage_provider(account(storage_provider)); // Add balance to the market pallet - let _ = Market::add_balance(RuntimeOrigin::signed(account(storage_provider)), 60); - let _ = Market::add_balance(RuntimeOrigin::signed(account(storage_client)), 70); + assert_ok!(Market::add_balance(RuntimeOrigin::signed(account(storage_provider)), 60)); + assert_ok!(Market::add_balance(RuntimeOrigin::signed(account(storage_client)), 70)); // Generate a deal proposal let deal_proposal = DealProposalBuilder::default() @@ -548,10 +547,7 @@ mod prove_commit_sector { // Test prove commits let sector = ProveCommitSector { sector_number, - proof: cid_of("prove_commit") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), + proof: bounded_vec![0xd, 0xe, 0xa, 0xd], }; assert_ok!(StorageProvider::prove_commit_sector( @@ -585,7 +581,7 @@ mod prove_commit_sector { #[test] fn fails_should_be_signed() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { // Sector to be pre-committed let sector = SectorPreCommitInfo { seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, @@ -612,7 +608,7 @@ mod prove_commit_sector { #[test] fn fails_storage_provider_not_found() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { let storage_provider = ALICE; let storage_client = BOB; let sector_number = 1; @@ -621,8 +617,8 @@ mod prove_commit_sector { register_storage_provider(account(storage_provider)); // Add balance to the market pallet - let _ = Market::add_balance(RuntimeOrigin::signed(account(storage_provider)), 60); - let _ = Market::add_balance(RuntimeOrigin::signed(account(storage_client)), 70); + assert_ok!(Market::add_balance(RuntimeOrigin::signed(account(storage_provider)), 60)); + assert_ok!(Market::add_balance(RuntimeOrigin::signed(account(storage_client)), 70)); // Generate a deal proposal let deal_proposal = DealProposalBuilder::default() @@ -661,10 +657,7 @@ mod prove_commit_sector { // Test prove commits let sector = ProveCommitSector { sector_number, - proof: cid_of("prove_commit") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), + proof: bounded_vec![0xd, 0xe, 0xa, 0xd], }; assert_noop!( @@ -679,7 +672,7 @@ mod prove_commit_sector { #[test] fn fails_storage_precommit_missing() { - new_test_ext(1).execute_with(|| { + new_test_ext().execute_with(|| { let storage_provider = ALICE; let sector_number = 1; @@ -689,10 +682,7 @@ mod prove_commit_sector { // Test prove commits let sector = ProveCommitSector { sector_number, - proof: cid_of("prove_commit") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), + proof: bounded_vec![0xd, 0xe, 0xa, 0xd], }; assert_noop!( @@ -712,7 +702,7 @@ mod prove_commit_sector { // Block number at which the prove commit is made. let proving_at_block_number = precommit_at_block_number + MaxProveCommitDuration::get(); - new_test_ext(precommit_at_block_number).execute_with(|| { + new_test_ext_with_block(precommit_at_block_number).execute_with(|| { let storage_provider = ALICE; let storage_client = BOB; let sector_number = 1; @@ -721,8 +711,8 @@ mod prove_commit_sector { register_storage_provider(account(storage_provider)); // Add balance to the market pallet - let _ = Market::add_balance(RuntimeOrigin::signed(account(storage_provider)), 60); - let _ = Market::add_balance(RuntimeOrigin::signed(account(storage_client)), 70); + assert_ok!(Market::add_balance(RuntimeOrigin::signed(account(storage_provider)), 60)); + assert_ok!(Market::add_balance(RuntimeOrigin::signed(account(storage_client)), 70)); // Generate a deal proposal let deal_proposal = DealProposalBuilder::default() @@ -761,13 +751,11 @@ mod prove_commit_sector { // Test prove commits let sector = ProveCommitSector { sector_number, - proof: cid_of("prove_commit") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), + proof: bounded_vec![0xd, 0xe, 0xa, 0xd], }; - System::set_block_number(proving_at_block_number); + run_to_block(proving_at_block_number); + assert_noop!( StorageProvider::prove_commit_sector( RuntimeOrigin::signed(account(storage_provider)), @@ -786,11 +774,11 @@ fn register_storage_provider(account: AccountIdOf) { let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; // Register account as a storage provider. - let _ = StorageProvider::register_storage_provider( + assert_ok!(StorageProvider::register_storage_provider( RuntimeOrigin::signed(account), peer_id.clone(), window_post_type, - ); + )); // Remove any events that were triggered during registration. System::reset_events(); From c7ae72251e30bcfef557e105df796ef2863cecbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Cerni=C4=8D?= Date: Fri, 12 Jul 2024 12:45:01 +0200 Subject: [PATCH 04/11] test: add SectorPreCommitInfoBuilder builder --- pallets/storage-provider/src/mock.rs | 3 +- pallets/storage-provider/src/test.rs | 434 ++++++++++++++------------- 2 files changed, 223 insertions(+), 214 deletions(-) diff --git a/pallets/storage-provider/src/mock.rs b/pallets/storage-provider/src/mock.rs index df74199f4..bc60f64e7 100644 --- a/pallets/storage-provider/src/mock.rs +++ b/pallets/storage-provider/src/mock.rs @@ -1,6 +1,7 @@ use cid::Cid; use frame_support::{ - derive_impl, pallet_prelude::ConstU32, parameter_types, sp_runtime::BoundedVec, traits::Hooks, PalletId + derive_impl, pallet_prelude::ConstU32, parameter_types, sp_runtime::BoundedVec, traits::Hooks, + PalletId, }; use multihash_codetable::{Code, MultihashDigest}; use sp_core::Pair; diff --git a/pallets/storage-provider/src/test.rs b/pallets/storage-provider/src/test.rs index 940961d44..430a48070 100644 --- a/pallets/storage-provider/src/test.rs +++ b/pallets/storage-provider/src/test.rs @@ -6,7 +6,9 @@ use frame_support::{ }; use frame_system::pallet_prelude::BlockNumberFor; use pallet_market::{BalanceOf, ClientDealProposal, DealProposal, DealState}; -use primitives_proofs::{RegisteredPoStProof, RegisteredSealProof}; +use primitives_proofs::{ + DealId, RegisteredPoStProof, RegisteredSealProof, SectorId, SectorNumber, MAX_DEALS_PER_SECTOR, +}; use sp_core::Pair; use sp_runtime::MultiSignature; @@ -102,6 +104,7 @@ mod storage_provider_registration { let peer_id = "storage_provider_1".as_bytes().to_vec(); let peer_id = BoundedVec::try_from(peer_id).unwrap(); let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; + // Register BOB as a storage provider. assert_ok!(StorageProvider::register_storage_provider( RuntimeOrigin::signed(account(BOB)), @@ -137,30 +140,24 @@ mod pre_commit_sector { register_storage_provider(account(storage_provider)); // Sector to be pre-committed. - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: 1, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0, 1], - expiration: YEARS, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); // Check starting balance assert_eq!(Balances::free_balance(account(storage_provider)), 100); + // Run pre commit extrinsic StorageProvider::pre_commit_sector( RuntimeOrigin::signed(account(storage_provider)), sector.clone(), ) .expect("Pre commit failed"); - // Check that the event triggered + + // Check that the events were triggered assert_eq!( events(), [ @@ -174,6 +171,7 @@ mod pre_commit_sector { }) ] ); + let sp_alice = StorageProviders::::get(account(storage_provider)) .expect("SP Alice should be present because of the pre-check"); @@ -188,20 +186,12 @@ mod pre_commit_sector { fn fails_should_be_signed() { new_test_ext().execute_with(|| { // Sector to be pre-committed - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: 1, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0, 1], - expiration: YEARS, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); // Run pre commit extrinsic assert_noop!( @@ -215,20 +205,12 @@ mod pre_commit_sector { fn fails_storage_provider_not_found() { new_test_ext().execute_with(|| { // Sector to be pre-committed - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: 1, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0, 1], - expiration: YEARS, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); // Run pre commit extrinsic assert_noop!( @@ -249,20 +231,13 @@ mod pre_commit_sector { register_storage_provider(account(storage_provider)); // Sector to be pre-committed - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: 1, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0, 1], - expiration: YEARS, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); + // Run pre commit extrinsic assert_ok!(StorageProvider::pre_commit_sector( RuntimeOrigin::signed(account(storage_provider)), @@ -287,24 +262,19 @@ mod pre_commit_sector { register_storage_provider(account(storage_provider)); // Sector to be pre-committed - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: SECTORS_MAX as u64 + 1, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0, 1], - expiration: YEARS, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(SECTORS_MAX as u64 + 1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); // Run pre commit extrinsic assert_noop!( - StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account(storage_provider)), sector.clone()), + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + ), Error::::InvalidSector, ); }); @@ -318,22 +288,22 @@ mod pre_commit_sector { register_storage_provider(account(storage_provider)); // Sector to be pre-committed - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: 1, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0, 1], - expiration: YEARS, - // Wrong cid set - unsealed_cid: BoundedVec::new(), - }; + let mut sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); + + // Setting the wrong unseal cid on the sector + sector.unsealed_cid = BoundedVec::new(); // Run pre commit extrinsic assert_noop!( - StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account(storage_provider)), sector.clone()), + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + ), Error::::InvalidCid, ); }); @@ -347,24 +317,20 @@ mod pre_commit_sector { register_storage_provider(account(&storage_provider)); // Sector to be pre-committed - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: 1, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0, 1], - expiration: 1000, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .expiration(1000) + .build(); // Run pre commit extrinsic assert_noop!( - StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account(storage_provider)), sector.clone()), + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + ), Error::::ExpirationBeforeActivation, ); }); @@ -380,25 +346,21 @@ mod pre_commit_sector { register_storage_provider(account(storage_provider)); // Sector to be pre-committed - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: 1, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0, 1], + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") // Set expiration to be in the next block after the maximum // allowed activation. - expiration: current_height + MaxProveCommitDuration::get() + 1, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; + .expiration(current_height + MaxProveCommitDuration::get() + 1) + .build(); assert_noop!( - StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account(storage_provider)), sector.clone()), + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + ), Error::::ExpirationTooSoon, ); }); @@ -414,26 +376,22 @@ mod pre_commit_sector { register_storage_provider(account(storage_provider)); // Sector to be pre-committed - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: 1, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0, 1], + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") // Set expiration to be in the next block after the maximum // allowed - expiration: current_height + MaxSectorExpirationExtension::get() + 1, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; + .expiration(current_height + MaxSectorExpirationExtension::get() + 1) + .build(); // Run pre commit extrinsic assert_noop!( - StorageProvider::pre_commit_sector(RuntimeOrigin::signed(account(storage_provider)), sector.clone()), + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + ), Error::::ExpirationTooLong, ); }); @@ -448,28 +406,19 @@ mod pre_commit_sector { // fn fails_max_sector_lifetime_exceeded() { // let current_height = 1000; - // new_test_ext(current_height).execute_with(|| { + // new_test_ext_with_block(current_height).execute_with(|| { // // Register ALICE as a storage provider. - // let storage_provider = account(ALICE); + // let storage_provider = ALICE; // register_storage_provider(account(storage_provider)); // // Sector to be pre-committed - // let sector = SectorPreCommitInfo { - // seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - // sector_number: 1, - // sealed_cid: cid_of("sealed_cid") - // .to_bytes() - // .try_into() - // .expect("hash is always 32 bytes"), - // deal_ids: bounded_vec![0, 1], - // // Set expiration to be in the next block after the maximum - // // allowed - // expiration: current_height + MaxProveCommitDuration::get() + SectorMaximumLifetime::get(), - // unsealed_cid: cid_of("unsealed_cid") - // .to_bytes() - // .try_into() - // .expect("hash is always 32 bytes"), - // }; + // let sector = SectorPreCommitInfoBuilder::default() + // .sector_number(1) + // .deals([0, 1]) + // .sealed_cid("sealed_cid") + // .unsealed_cid("unsealed_cid") + // .expiration(current_height + MaxProveCommitDuration::get() + SectorMaximumLifetime::get()) + // .build(); // // Run pre commit extrinsic // assert_noop!( @@ -486,8 +435,6 @@ mod pre_commit_sector { mod prove_commit_sector { use sp_runtime::DispatchError; - use crate::sector::SECTORS_MAX; - use super::*; #[test] @@ -501,8 +448,14 @@ mod prove_commit_sector { register_storage_provider(account(storage_provider)); // Add balance to the market pallet - assert_ok!(Market::add_balance(RuntimeOrigin::signed(account(storage_provider)), 60)); - assert_ok!(Market::add_balance(RuntimeOrigin::signed(account(storage_client)), 70)); + assert_ok!(Market::add_balance( + RuntimeOrigin::signed(account(storage_provider)), + 60 + )); + assert_ok!(Market::add_balance( + RuntimeOrigin::signed(account(storage_client)), + 70 + )); // Generate a deal proposal let deal_proposal = DealProposalBuilder::default() @@ -520,20 +473,12 @@ mod prove_commit_sector { let sector_number = 1; // Sector data - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0], - expiration: YEARS, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(sector_number) + .deals([0]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); // Run pre commit extrinsic assert_ok!(StorageProvider::pre_commit_sector( @@ -573,6 +518,7 @@ mod prove_commit_sector { assert_eq!(Balances::free_balance(account(storage_provider)), 39); let sp_state = StorageProviders::::get(account(storage_provider)) .expect("Should be able to get providers info"); + // check that the sector has been activated assert!(!sp_state.sectors.is_empty()); assert!(sp_state.sectors.contains_key(§or_number)); @@ -583,20 +529,12 @@ mod prove_commit_sector { fn fails_should_be_signed() { new_test_ext().execute_with(|| { // Sector to be pre-committed - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: SECTORS_MAX as u64 + 1, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0, 1], - expiration: YEARS, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); // Run pre commit extrinsic assert_noop!( @@ -617,8 +555,14 @@ mod prove_commit_sector { register_storage_provider(account(storage_provider)); // Add balance to the market pallet - assert_ok!(Market::add_balance(RuntimeOrigin::signed(account(storage_provider)), 60)); - assert_ok!(Market::add_balance(RuntimeOrigin::signed(account(storage_client)), 70)); + assert_ok!(Market::add_balance( + RuntimeOrigin::signed(account(storage_provider)), + 60 + )); + assert_ok!(Market::add_balance( + RuntimeOrigin::signed(account(storage_client)), + 70 + )); // Generate a deal proposal let deal_proposal = DealProposalBuilder::default() @@ -633,20 +577,12 @@ mod prove_commit_sector { )); // Sector data - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0], - expiration: YEARS, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(sector_number) + .deals([0]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); // Run pre commit extrinsic assert_ok!(StorageProvider::pre_commit_sector( @@ -711,8 +647,14 @@ mod prove_commit_sector { register_storage_provider(account(storage_provider)); // Add balance to the market pallet - assert_ok!(Market::add_balance(RuntimeOrigin::signed(account(storage_provider)), 60)); - assert_ok!(Market::add_balance(RuntimeOrigin::signed(account(storage_client)), 70)); + assert_ok!(Market::add_balance( + RuntimeOrigin::signed(account(storage_provider)), + 60 + )); + assert_ok!(Market::add_balance( + RuntimeOrigin::signed(account(storage_client)), + 70 + )); // Generate a deal proposal let deal_proposal = DealProposalBuilder::default() @@ -727,20 +669,12 @@ mod prove_commit_sector { )); // Sector data - let sector = SectorPreCommitInfo { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number, - sealed_cid: cid_of("sealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - deal_ids: bounded_vec![0], - expiration: YEARS, - unsealed_cid: cid_of("unsealed_cid") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - }; + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(sector_number) + .deals([0]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); // Run pre commit extrinsic assert_ok!(StorageProvider::pre_commit_sector( @@ -784,6 +718,80 @@ fn register_storage_provider(account: AccountIdOf) { System::reset_events(); } +struct SectorPreCommitInfoBuilder { + seal_proof: RegisteredSealProof, + sector_number: Option, + sealed_cid: Option, + deal_ids: Option>>, + expiration: u64, + unsealed_cid: Option, +} + +impl Default for SectorPreCommitInfoBuilder { + fn default() -> Self { + Self { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number: None, + sealed_cid: None, + deal_ids: None, + expiration: YEARS, + unsealed_cid: None, + } + } +} + +impl SectorPreCommitInfoBuilder { + pub fn sector_number(mut self, sector_number: u64) -> Self { + self.sector_number = Some(sector_number); + self + } + + pub fn deals(mut self, deal_ids: I) -> Self + where + I: IntoIterator, + { + let deal_ids_vec = deal_ids.into_iter().collect::>(); + self.deal_ids = Some(BoundedVec::try_from(deal_ids_vec).unwrap()); + self + } + + pub fn sealed_cid(mut self, data: &str) -> Self { + self.sealed_cid = Some( + cid_of(data) + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + ); + self + } + + pub fn unsealed_cid(mut self, data: &str) -> Self { + self.unsealed_cid = Some( + cid_of(data) + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + ); + self + } + + pub fn expiration(mut self, expiration: u64) -> Self { + self.expiration = expiration; + self + } + + pub fn build(self) -> SectorPreCommitInfo { + SectorPreCommitInfo { + seal_proof: self.seal_proof, + sector_number: self.sector_number.expect("sector number is required"), + sealed_cid: self.sealed_cid.expect("sealed cid is required"), + deal_ids: self.deal_ids.expect("deal ids are required"), + expiration: self.expiration, + unsealed_cid: self.unsealed_cid.expect("unsealed cid is required"), + } + } +} + /// Builder to simplify writing complex tests of [`DealProposal`]. /// Exclusively uses [`Test`] for simplification purposes. struct DealProposalBuilder { From 86e7efc85d6c322128bff157f81e842d30ee3586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Cerni=C4=8D?= Date: Fri, 12 Jul 2024 12:53:59 +0200 Subject: [PATCH 05/11] fix: add task to the comment --- primitives/proofs/src/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/proofs/src/types.rs b/primitives/proofs/src/types.rs index c02537433..98e9bd68a 100644 --- a/primitives/proofs/src/types.rs +++ b/primitives/proofs/src/types.rs @@ -4,7 +4,7 @@ use sp_runtime::RuntimeDebug; pub type DealId = u64; -// TODO(no-ref,@cernicc,11/07/2024): Refactor to new type. Sector number should +// TODO(#129,@cernicc,11/07/2024): Refactor to new type. Sector number should // always be between 0 and SECTORS_MAX (32 << 20). pub type SectorNumber = u64; From 0a1836eecffca06fb1330ee200538d65f2f83171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Cerni=C4=8D?= Date: Fri, 12 Jul 2024 13:48:24 +0200 Subject: [PATCH 06/11] test: simplify the test --- pallets/storage-provider/src/mock.rs | 1 - pallets/storage-provider/src/test.rs | 88 ++++++++++++++-------------- 2 files changed, 44 insertions(+), 45 deletions(-) diff --git a/pallets/storage-provider/src/mock.rs b/pallets/storage-provider/src/mock.rs index bc60f64e7..398dc1385 100644 --- a/pallets/storage-provider/src/mock.rs +++ b/pallets/storage-provider/src/mock.rs @@ -105,7 +105,6 @@ pub fn account(name: &str) -> AccountIdOf { pub const ALICE: &'static str = "//Alice"; pub const BOB: &'static str = "//Bob"; -pub const CHARLIE: &'static str = "//Charlie"; /// Initial funds of all accounts. pub const INITIAL_FUNDS: u64 = 100; diff --git a/pallets/storage-provider/src/test.rs b/pallets/storage-provider/src/test.rs index 430a48070..4d1693ed3 100644 --- a/pallets/storage-provider/src/test.rs +++ b/pallets/storage-provider/src/test.rs @@ -547,58 +547,58 @@ mod prove_commit_sector { #[test] fn fails_storage_provider_not_found() { new_test_ext().execute_with(|| { - let storage_provider = ALICE; - let storage_client = BOB; - let sector_number = 1; - - // Register storage provider - register_storage_provider(account(storage_provider)); - - // Add balance to the market pallet - assert_ok!(Market::add_balance( - RuntimeOrigin::signed(account(storage_provider)), - 60 - )); - assert_ok!(Market::add_balance( - RuntimeOrigin::signed(account(storage_client)), - 70 - )); - - // Generate a deal proposal - let deal_proposal = DealProposalBuilder::default() - .client(storage_client) - .provider(storage_provider) - .signed(storage_client); - - // Publish the deal proposal - assert_ok!(Market::publish_storage_deals( - RuntimeOrigin::signed(account(storage_provider)), - bounded_vec![deal_proposal], - )); - - // Sector data - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(sector_number) - .deals([0]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); - - // Run pre commit extrinsic - assert_ok!(StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector.clone() - )); + // let storage_provider = ALICE; + // let storage_client = BOB; + // let sector_number = 1; + + // // Register storage provider + // register_storage_provider(account(storage_provider)); + + // // Add balance to the market pallet + // assert_ok!(Market::add_balance( + // RuntimeOrigin::signed(account(storage_provider)), + // 60 + // )); + // assert_ok!(Market::add_balance( + // RuntimeOrigin::signed(account(storage_client)), + // 70 + // )); + + // // Generate a deal proposal + // let deal_proposal = DealProposalBuilder::default() + // .client(storage_client) + // .provider(storage_provider) + // .signed(storage_client); + + // // Publish the deal proposal + // assert_ok!(Market::publish_storage_deals( + // RuntimeOrigin::signed(account(storage_provider)), + // bounded_vec![deal_proposal], + // )); + + // // Sector data + // let sector = SectorPreCommitInfoBuilder::default() + // .sector_number(sector_number) + // .deals([0]) + // .sealed_cid("sealed_cid") + // .unsealed_cid("unsealed_cid") + // .build(); + + // // Run pre commit extrinsic + // assert_ok!(StorageProvider::pre_commit_sector( + // RuntimeOrigin::signed(account(storage_provider)), + // sector.clone() + // )); // Test prove commits let sector = ProveCommitSector { - sector_number, + sector_number: 1, proof: bounded_vec![0xd, 0xe, 0xa, 0xd], }; assert_noop!( StorageProvider::prove_commit_sector( - RuntimeOrigin::signed(account(CHARLIE)), + RuntimeOrigin::signed(account(ALICE)), sector ), Error::::StorageProviderNotFound, From 5557b0a7c7bbf31a4758bb5e7c0aa8955eedb52f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Cerni=C4=8D?= Date: Fri, 12 Jul 2024 15:30:26 +0200 Subject: [PATCH 07/11] test: move to a separate tests folder --- pallets/storage-provider/src/lib.rs | 7 +- pallets/storage-provider/src/mock.rs | 168 ---- pallets/storage-provider/src/test.rs | 884 ------------------ pallets/storage-provider/src/tests/mod.rs | 352 +++++++ .../src/tests/pre_commit_sector.rs | 318 +++++++ .../src/tests/prove_commit_sector.rs | 233 +++++ pallets/storage-provider/src/tests/state.rs | 13 + .../tests/storage_provider_registration.rs | 102 ++ 8 files changed, 1020 insertions(+), 1057 deletions(-) delete mode 100644 pallets/storage-provider/src/mock.rs delete mode 100644 pallets/storage-provider/src/test.rs create mode 100644 pallets/storage-provider/src/tests/mod.rs create mode 100644 pallets/storage-provider/src/tests/pre_commit_sector.rs create mode 100644 pallets/storage-provider/src/tests/prove_commit_sector.rs create mode 100644 pallets/storage-provider/src/tests/state.rs create mode 100644 pallets/storage-provider/src/tests/storage_provider_registration.rs diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index 3011e6b62..2b09c1926 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -16,10 +16,7 @@ pub use pallet::{Config, Pallet}; mod benchmarks; #[cfg(test)] -mod mock; - -#[cfg(test)] -mod test; +mod tests; mod proofs; mod sector; @@ -166,7 +163,7 @@ pub mod pallet { /// Emitted when a sector fails to activate CouldNotActivateSector, /// Emitted when a prove commit is sent after the deadline. - /// These pre-commits will be cleaned up in the hook + /// These pre-commits will be cleaned up in the hook. ProveCommitAfterDeadline, } diff --git a/pallets/storage-provider/src/mock.rs b/pallets/storage-provider/src/mock.rs deleted file mode 100644 index 398dc1385..000000000 --- a/pallets/storage-provider/src/mock.rs +++ /dev/null @@ -1,168 +0,0 @@ -use cid::Cid; -use frame_support::{ - derive_impl, pallet_prelude::ConstU32, parameter_types, sp_runtime::BoundedVec, traits::Hooks, - PalletId, -}; -use multihash_codetable::{Code, MultihashDigest}; -use sp_core::Pair; -use sp_runtime::{ - traits::{ConstU64, IdentifyAccount, IdentityLookup, Verify}, - BuildStorage, MultiSignature, MultiSigner, -}; - -use crate::{self as pallet_storage_provider, pallet::CID_CODEC}; - -type Block = frame_system::mocking::MockBlock; - -type BlockNumber = u64; - -const MILLISECS_PER_BLOCK: u64 = 12000; -const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK; -const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber); -const HOURS: BlockNumber = MINUTES * 60; -const DAYS: BlockNumber = HOURS * 24; -pub const YEARS: BlockNumber = DAYS * 365; - -frame_support::construct_runtime!( - pub enum Test { - System: frame_system, - Balances: pallet_balances, - StorageProvider: pallet_storage_provider::pallet, - Market: pallet_market, - } -); - -pub type Signature = MultiSignature; -pub type AccountPublic = ::Signer; -pub type AccountId = ::AccountId; - -#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] -impl frame_system::Config for Test { - type Block = Block; - type AccountData = pallet_balances::AccountData; - type AccountId = AccountId; - type Lookup = IdentityLookup; -} - -#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)] -impl pallet_balances::Config for Test { - type AccountStore = System; -} - -parameter_types! { - pub const MarketPalletId: PalletId = PalletId(*b"spMarket"); -} - -impl pallet_market::Config for Test { - type RuntimeEvent = RuntimeEvent; - type PalletId = MarketPalletId; - type Currency = Balances; - type OffchainSignature = Signature; - type OffchainPublic = AccountPublic; - type MaxDeals = ConstU32<32>; - type BlocksPerDay = ConstU64<1>; - type MinDealDuration = ConstU64<1>; - type MaxDealDuration = ConstU64<30>; - type MaxDealsPerBlock = ConstU32<32>; -} - -parameter_types! { - pub const WpostProvingPeriod: BlockNumber = DAYS; - // Half an hour (=48 per day) - // 30 * 60 = 30 minutes - // SLOT_DURATION is in milliseconds thats why we / 1000 - pub const WpostChallengeWindow: BlockNumber = 30 * 60 / (SLOT_DURATION as BlockNumber / 1000); - pub const MinSectorExpiration: BlockNumber = 180 * DAYS; - pub const MaxSectorExpirationExtension: BlockNumber = 1278 * DAYS; - pub const SectorMaximumLifetime: BlockNumber = YEARS * 5; - pub const MaxProveCommitDuration: BlockNumber = (30 * DAYS) + 150; -} - -impl pallet_storage_provider::Config for Test { - type RuntimeEvent = RuntimeEvent; - type PeerId = BoundedVec>; // Arbitrary length - type Currency = Balances; - type Market = Market; - type WPoStProvingPeriod = WpostProvingPeriod; - type WPoStChallengeWindow = WpostChallengeWindow; - type MinSectorExpiration = MinSectorExpiration; - type MaxSectorExpirationExtension = MaxSectorExpirationExtension; - type SectorMaximumLifetime = SectorMaximumLifetime; - type MaxProveCommitDuration = MaxProveCommitDuration; -} - -pub type AccountIdOf = ::AccountId; - -pub fn key_pair(name: &str) -> sp_core::sr25519::Pair { - sp_core::sr25519::Pair::from_string(name, None).unwrap() -} - -pub fn account(name: &str) -> AccountIdOf { - let user_pair = key_pair(name); - let signer = MultiSigner::Sr25519(user_pair.public()); - signer.into_account() -} - -pub const ALICE: &'static str = "//Alice"; -pub const BOB: &'static str = "//Bob"; - -/// Initial funds of all accounts. -pub const INITIAL_FUNDS: u64 = 100; - -// Build genesis storage according to the mock runtime. Specify the starting -// block number. -pub fn new_test_ext_with_block(starting_block_number: u64) -> sp_io::TestExternalities { - let mut ext = new_test_ext(); - ext.execute_with(|| System::set_block_number(starting_block_number)); - ext -} - -// Build genesis storage according to the mock runtime. -pub fn new_test_ext() -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::::default() - .build_storage() - .unwrap() - .into(); - - pallet_balances::GenesisConfig:: { - balances: vec![ - (account(ALICE), INITIAL_FUNDS), - (account(BOB), INITIAL_FUNDS), - ], - } - .assimilate_storage(&mut t) - .unwrap(); - - let mut ext = sp_io::TestExternalities::new(t); - ext.execute_with(|| System::set_block_number(1)); - ext -} - -pub fn events() -> Vec { - let evt = System::events() - .into_iter() - .map(|evt| evt.event) - .collect::>(); - System::reset_events(); - evt -} - -pub fn cid_of(data: &str) -> cid::Cid { - Cid::new_v1(CID_CODEC, Code::Blake2b256.digest(data.as_bytes())) -} - -/// Run until a particular block. -/// -/// Stolen't from: -pub fn run_to_block(n: u64) { - while System::block_number() < n { - if System::block_number() > 1 { - StorageProvider::on_finalize(System::block_number()); - System::on_finalize(System::block_number()); - } - - System::set_block_number(System::block_number() + 1); - System::on_initialize(System::block_number()); - StorageProvider::on_initialize(System::block_number()); - } -} diff --git a/pallets/storage-provider/src/test.rs b/pallets/storage-provider/src/test.rs deleted file mode 100644 index 4d1693ed3..000000000 --- a/pallets/storage-provider/src/test.rs +++ /dev/null @@ -1,884 +0,0 @@ -use codec::Encode; -use frame_support::{ - assert_noop, assert_ok, - sp_runtime::{bounded_vec, BoundedVec}, - traits::ConstU32, -}; -use frame_system::pallet_prelude::BlockNumberFor; -use pallet_market::{BalanceOf, ClientDealProposal, DealProposal, DealState}; -use primitives_proofs::{ - DealId, RegisteredPoStProof, RegisteredSealProof, SectorId, SectorNumber, MAX_DEALS_PER_SECTOR, -}; -use sp_core::Pair; -use sp_runtime::MultiSignature; - -use crate::{ - mock::*, - pallet::{Error, Event, StorageProviders}, - sector::{ProveCommitSector, SectorPreCommitInfo}, - storage_provider::StorageProviderInfo, -}; - -#[test] -fn initial_state() { - new_test_ext().execute_with(|| { - assert!(!StorageProviders::::contains_key(account(ALICE))); - assert!(!StorageProviders::::contains_key(account(BOB))); - }) -} - -mod storage_provider_registration { - use sp_runtime::DispatchError; - - use super::*; - - /// Tests if storage provider registration is successful. - #[test] - fn successful_registration() { - new_test_ext().execute_with(|| { - let peer_id = "storage_provider_1".as_bytes().to_vec(); - let peer_id = BoundedVec::try_from(peer_id).unwrap(); - let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; - let expected_sector_size = window_post_type.sector_size(); - let expected_partition_sectors = window_post_type.window_post_partitions_sector(); - let expected_sp_info = StorageProviderInfo::new(peer_id.clone(), window_post_type); - - // Register BOB as a storage provider. - assert_ok!(StorageProvider::register_storage_provider( - RuntimeOrigin::signed(account(BOB)), - peer_id.clone(), - window_post_type, - )); - assert!(StorageProviders::::contains_key(account(BOB))); - - // `unwrap()` should be safe because of the above check. - let sp_bob = StorageProviders::::get(account(BOB)).unwrap(); - // Check that storage provider information is correct. - assert_eq!(sp_bob.info.peer_id, peer_id); - assert_eq!(sp_bob.info.window_post_proof_type, window_post_type); - assert_eq!(sp_bob.info.sector_size, expected_sector_size); - assert_eq!( - sp_bob.info.window_post_partition_sectors, - expected_partition_sectors - ); - // Check that pre commit sectors are empty. - assert!(sp_bob.pre_committed_sectors.is_empty()); - // Check that no pre commit deposit is made - assert_eq!(sp_bob.pre_commit_deposits, 0); - // Check that sectors are empty. - assert!(sp_bob.sectors.is_empty()); - // Check that the event triggered - assert_eq!( - events(), - [RuntimeEvent::StorageProvider( - Event::::StorageProviderRegistered { - owner: account(BOB), - info: expected_sp_info, - }, - )] - ); - }) - } - - #[test] - fn fails_should_be_signed() { - new_test_ext().execute_with(|| { - let peer_id = "storage_provider_1".as_bytes().to_vec(); - let peer_id = BoundedVec::try_from(peer_id).unwrap(); - let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; - - assert_noop!( - StorageProvider::register_storage_provider( - RuntimeOrigin::none(), - peer_id.clone(), - window_post_type, - ), - DispatchError::BadOrigin - ); - }); - } - - #[test] - fn fails_double_register() { - new_test_ext().execute_with(|| { - let peer_id = "storage_provider_1".as_bytes().to_vec(); - let peer_id = BoundedVec::try_from(peer_id).unwrap(); - let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; - - // Register BOB as a storage provider. - assert_ok!(StorageProvider::register_storage_provider( - RuntimeOrigin::signed(account(BOB)), - peer_id.clone(), - window_post_type, - )); - assert!(StorageProviders::::contains_key(account(BOB))); - // Try to register BOB again. Should fail - assert_noop!( - StorageProvider::register_storage_provider( - RuntimeOrigin::signed(account(BOB)), - peer_id.clone(), - window_post_type, - ), - Error::::StorageProviderExists - ); - }); - } -} - -mod pre_commit_sector { - use sp_runtime::DispatchError; - - use crate::sector::SECTORS_MAX; - - use super::*; - - #[test] - fn successfully_precommited() { - new_test_ext().execute_with(|| { - // Register ALICE as a storage provider. - let storage_provider = ALICE; - register_storage_provider(account(storage_provider)); - - // Sector to be pre-committed. - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); - - // Check starting balance - assert_eq!(Balances::free_balance(account(storage_provider)), 100); - - // Run pre commit extrinsic - StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector.clone(), - ) - .expect("Pre commit failed"); - - // Check that the events were triggered - assert_eq!( - events(), - [ - RuntimeEvent::Balances(pallet_balances::Event::::Reserved { - who: account(storage_provider), - amount: 1 - },), - RuntimeEvent::StorageProvider(Event::::SectorPreCommitted { - owner: account(storage_provider), - sector: sector.clone(), - }) - ] - ); - - let sp_alice = StorageProviders::::get(account(storage_provider)) - .expect("SP Alice should be present because of the pre-check"); - - assert!(sp_alice.sectors.is_empty()); // not yet proven - assert!(!sp_alice.pre_committed_sectors.is_empty()); - assert_eq!(sp_alice.pre_commit_deposits, 1); - assert_eq!(Balances::free_balance(account(storage_provider)), 99); - }); - } - - #[test] - fn fails_should_be_signed() { - new_test_ext().execute_with(|| { - // Sector to be pre-committed - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); - - // Run pre commit extrinsic - assert_noop!( - StorageProvider::pre_commit_sector(RuntimeOrigin::none(), sector.clone()), - DispatchError::BadOrigin, - ); - }); - } - - #[test] - fn fails_storage_provider_not_found() { - new_test_ext().execute_with(|| { - // Sector to be pre-committed - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); - - // Run pre commit extrinsic - assert_noop!( - StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(ALICE)), - sector.clone() - ), - Error::::StorageProviderNotFound, - ); - }); - } - - #[test] - fn fails_sector_number_already_used() { - new_test_ext().execute_with(|| { - // Register ALICE as a storage provider. - let storage_provider = ALICE; - register_storage_provider(account(storage_provider)); - - // Sector to be pre-committed - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); - - // Run pre commit extrinsic - assert_ok!(StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector.clone() - )); - // Run same extrinsic, this should fail - assert_noop!( - StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector.clone() - ), - Error::::SectorNumberAlreadyUsed, - ); - }); - } - - #[test] - fn fails_invalid_sector() { - new_test_ext().execute_with(|| { - // Register ALICE as a storage provider. - let storage_provider = ALICE; - register_storage_provider(account(storage_provider)); - - // Sector to be pre-committed - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(SECTORS_MAX as u64 + 1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); - - // Run pre commit extrinsic - assert_noop!( - StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector.clone() - ), - Error::::InvalidSector, - ); - }); - } - - #[test] - fn fails_invalid_cid() { - new_test_ext().execute_with(|| { - // Register ALICE as a storage provider. - let storage_provider = ALICE; - register_storage_provider(account(storage_provider)); - - // Sector to be pre-committed - let mut sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); - - // Setting the wrong unseal cid on the sector - sector.unsealed_cid = BoundedVec::new(); - - // Run pre commit extrinsic - assert_noop!( - StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector.clone() - ), - Error::::InvalidCid, - ); - }); - } - - #[test] - fn fails_expiration_before_activation() { - new_test_ext_with_block(1000).execute_with(|| { - // Register ALICE as a storage provider. - let storage_provider = ALICE; - register_storage_provider(account(&storage_provider)); - - // Sector to be pre-committed - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .expiration(1000) - .build(); - - // Run pre commit extrinsic - assert_noop!( - StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector.clone() - ), - Error::::ExpirationBeforeActivation, - ); - }); - } - - #[test] - fn fails_expiration_too_soon() { - let current_height = 1000; - - new_test_ext_with_block(current_height).execute_with(|| { - // Register ALICE as a storage provider. - let storage_provider = ALICE; - register_storage_provider(account(storage_provider)); - - // Sector to be pre-committed - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - // Set expiration to be in the next block after the maximum - // allowed activation. - .expiration(current_height + MaxProveCommitDuration::get() + 1) - .build(); - - assert_noop!( - StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector.clone() - ), - Error::::ExpirationTooSoon, - ); - }); - } - - #[test] - fn fails_expiration_too_long() { - let current_height = 1000; - - new_test_ext_with_block(current_height).execute_with(|| { - // Register ALICE as a storage provider. - let storage_provider = ALICE; - register_storage_provider(account(storage_provider)); - - // Sector to be pre-committed - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - // Set expiration to be in the next block after the maximum - // allowed - .expiration(current_height + MaxSectorExpirationExtension::get() + 1) - .build(); - - // Run pre commit extrinsic - assert_noop!( - StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector.clone() - ), - Error::::ExpirationTooLong, - ); - }); - } - - // TODO(no-ref,@cernicc,11/07/2024): Based on the current setup I can't get - // this test to pass. That is because the `SectorMaximumLifetime` is longer - // then the bound for `ExpirationTooLong`. Is the test wrong? is the - // implementation wrong? - // - // #[test] - // fn fails_max_sector_lifetime_exceeded() { - // let current_height = 1000; - - // new_test_ext_with_block(current_height).execute_with(|| { - // // Register ALICE as a storage provider. - // let storage_provider = ALICE; - // register_storage_provider(account(storage_provider)); - - // // Sector to be pre-committed - // let sector = SectorPreCommitInfoBuilder::default() - // .sector_number(1) - // .deals([0, 1]) - // .sealed_cid("sealed_cid") - // .unsealed_cid("unsealed_cid") - // .expiration(current_height + MaxProveCommitDuration::get() + SectorMaximumLifetime::get()) - // .build(); - - // // Run pre commit extrinsic - // assert_noop!( - // StorageProvider::pre_commit_sector( - // RuntimeOrigin::signed(account(storage_provider)), - // sector.clone() - // ), - // Error::::MaxSectorLifetimeExceeded, - // ); - // }); - // } -} - -mod prove_commit_sector { - use sp_runtime::DispatchError; - - use super::*; - - #[test] - fn successfully_prove_sector() { - new_test_ext().execute_with(|| { - // Setup accounts - let storage_provider = ALICE; - let storage_client = BOB; - - // Register storage provider - register_storage_provider(account(storage_provider)); - - // Add balance to the market pallet - assert_ok!(Market::add_balance( - RuntimeOrigin::signed(account(storage_provider)), - 60 - )); - assert_ok!(Market::add_balance( - RuntimeOrigin::signed(account(storage_client)), - 70 - )); - - // Generate a deal proposal - let deal_proposal = DealProposalBuilder::default() - .client(storage_client) - .provider(storage_provider) - .signed(storage_client); - - // Publish the deal proposal - assert_ok!(Market::publish_storage_deals( - RuntimeOrigin::signed(account(storage_provider)), - bounded_vec![deal_proposal], - )); - - // Sector to be pre-committed and proven - let sector_number = 1; - - // Sector data - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(sector_number) - .deals([0]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); - - // Run pre commit extrinsic - assert_ok!(StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector.clone() - )); - - // flush the events - events(); - - // Test prove commits - let sector = ProveCommitSector { - sector_number, - proof: bounded_vec![0xd, 0xe, 0xa, 0xd], - }; - - assert_ok!(StorageProvider::prove_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector - )); - assert_eq!( - events(), - [ - RuntimeEvent::Market(pallet_market::Event::DealActivated { - deal_id: 0, - client: account(storage_client), - provider: account(storage_provider) - }), - RuntimeEvent::StorageProvider(Event::::SectorProven { - owner: account(storage_provider), - sector_number: sector_number - }) - ] - ); - - // check that the funds are still locked - assert_eq!(Balances::free_balance(account(storage_provider)), 39); - let sp_state = StorageProviders::::get(account(storage_provider)) - .expect("Should be able to get providers info"); - - // check that the sector has been activated - assert!(!sp_state.sectors.is_empty()); - assert!(sp_state.sectors.contains_key(§or_number)); - }); - } - - #[test] - fn fails_should_be_signed() { - new_test_ext().execute_with(|| { - // Sector to be pre-committed - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); - - // Run pre commit extrinsic - assert_noop!( - StorageProvider::pre_commit_sector(RuntimeOrigin::none(), sector.clone()), - DispatchError::BadOrigin, - ); - }); - } - - #[test] - fn fails_storage_provider_not_found() { - new_test_ext().execute_with(|| { - // let storage_provider = ALICE; - // let storage_client = BOB; - // let sector_number = 1; - - // // Register storage provider - // register_storage_provider(account(storage_provider)); - - // // Add balance to the market pallet - // assert_ok!(Market::add_balance( - // RuntimeOrigin::signed(account(storage_provider)), - // 60 - // )); - // assert_ok!(Market::add_balance( - // RuntimeOrigin::signed(account(storage_client)), - // 70 - // )); - - // // Generate a deal proposal - // let deal_proposal = DealProposalBuilder::default() - // .client(storage_client) - // .provider(storage_provider) - // .signed(storage_client); - - // // Publish the deal proposal - // assert_ok!(Market::publish_storage_deals( - // RuntimeOrigin::signed(account(storage_provider)), - // bounded_vec![deal_proposal], - // )); - - // // Sector data - // let sector = SectorPreCommitInfoBuilder::default() - // .sector_number(sector_number) - // .deals([0]) - // .sealed_cid("sealed_cid") - // .unsealed_cid("unsealed_cid") - // .build(); - - // // Run pre commit extrinsic - // assert_ok!(StorageProvider::pre_commit_sector( - // RuntimeOrigin::signed(account(storage_provider)), - // sector.clone() - // )); - - // Test prove commits - let sector = ProveCommitSector { - sector_number: 1, - proof: bounded_vec![0xd, 0xe, 0xa, 0xd], - }; - - assert_noop!( - StorageProvider::prove_commit_sector( - RuntimeOrigin::signed(account(ALICE)), - sector - ), - Error::::StorageProviderNotFound, - ); - }); - } - - #[test] - fn fails_storage_precommit_missing() { - new_test_ext().execute_with(|| { - let storage_provider = ALICE; - let sector_number = 1; - - // Register storage provider - register_storage_provider(account(storage_provider)); - - // Test prove commits - let sector = ProveCommitSector { - sector_number, - proof: bounded_vec![0xd, 0xe, 0xa, 0xd], - }; - - assert_noop!( - StorageProvider::prove_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector - ), - Error::::InvalidSector, - ); - }); - } - - #[test] - fn fails_prove_commit_after_deadline() { - // Block number at which the precommit is made - let precommit_at_block_number = 1; - // Block number at which the prove commit is made. - let proving_at_block_number = precommit_at_block_number + MaxProveCommitDuration::get(); - - new_test_ext_with_block(precommit_at_block_number).execute_with(|| { - let storage_provider = ALICE; - let storage_client = BOB; - let sector_number = 1; - - // Register storage provider - register_storage_provider(account(storage_provider)); - - // Add balance to the market pallet - assert_ok!(Market::add_balance( - RuntimeOrigin::signed(account(storage_provider)), - 60 - )); - assert_ok!(Market::add_balance( - RuntimeOrigin::signed(account(storage_client)), - 70 - )); - - // Generate a deal proposal - let deal_proposal = DealProposalBuilder::default() - .client(storage_client) - .provider(storage_provider) - .signed(storage_client); - - // Publish the deal proposal - assert_ok!(Market::publish_storage_deals( - RuntimeOrigin::signed(account(storage_provider)), - bounded_vec![deal_proposal], - )); - - // Sector data - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(sector_number) - .deals([0]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); - - // Run pre commit extrinsic - assert_ok!(StorageProvider::pre_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector.clone() - )); - - // Test prove commits - let sector = ProveCommitSector { - sector_number, - proof: bounded_vec![0xd, 0xe, 0xa, 0xd], - }; - - run_to_block(proving_at_block_number); - - assert_noop!( - StorageProvider::prove_commit_sector( - RuntimeOrigin::signed(account(storage_provider)), - sector - ), - Error::::ProveCommitAfterDeadline, - ); - }); - } -} - -/// Register account as a provider. -fn register_storage_provider(account: AccountIdOf) { - let peer_id = "storage_provider_1".as_bytes().to_vec(); - let peer_id = BoundedVec::try_from(peer_id).unwrap(); - let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; - - // Register account as a storage provider. - assert_ok!(StorageProvider::register_storage_provider( - RuntimeOrigin::signed(account), - peer_id.clone(), - window_post_type, - )); - - // Remove any events that were triggered during registration. - System::reset_events(); -} - -struct SectorPreCommitInfoBuilder { - seal_proof: RegisteredSealProof, - sector_number: Option, - sealed_cid: Option, - deal_ids: Option>>, - expiration: u64, - unsealed_cid: Option, -} - -impl Default for SectorPreCommitInfoBuilder { - fn default() -> Self { - Self { - seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: None, - sealed_cid: None, - deal_ids: None, - expiration: YEARS, - unsealed_cid: None, - } - } -} - -impl SectorPreCommitInfoBuilder { - pub fn sector_number(mut self, sector_number: u64) -> Self { - self.sector_number = Some(sector_number); - self - } - - pub fn deals(mut self, deal_ids: I) -> Self - where - I: IntoIterator, - { - let deal_ids_vec = deal_ids.into_iter().collect::>(); - self.deal_ids = Some(BoundedVec::try_from(deal_ids_vec).unwrap()); - self - } - - pub fn sealed_cid(mut self, data: &str) -> Self { - self.sealed_cid = Some( - cid_of(data) - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - ); - self - } - - pub fn unsealed_cid(mut self, data: &str) -> Self { - self.unsealed_cid = Some( - cid_of(data) - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - ); - self - } - - pub fn expiration(mut self, expiration: u64) -> Self { - self.expiration = expiration; - self - } - - pub fn build(self) -> SectorPreCommitInfo { - SectorPreCommitInfo { - seal_proof: self.seal_proof, - sector_number: self.sector_number.expect("sector number is required"), - sealed_cid: self.sealed_cid.expect("sealed cid is required"), - deal_ids: self.deal_ids.expect("deal ids are required"), - expiration: self.expiration, - unsealed_cid: self.unsealed_cid.expect("unsealed cid is required"), - } - } -} - -/// Builder to simplify writing complex tests of [`DealProposal`]. -/// Exclusively uses [`Test`] for simplification purposes. -struct DealProposalBuilder { - piece_cid: BoundedVec>, - piece_size: u64, - client: AccountIdOf, - provider: AccountIdOf, - label: BoundedVec>, - start_block: u64, - end_block: u64, - storage_price_per_block: u64, - provider_collateral: u64, - state: DealState, -} - -impl Default for DealProposalBuilder { - fn default() -> Self { - Self { - piece_cid: cid_of("polka-storage-data") - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - piece_size: 18, - client: account(BOB), - provider: account(ALICE), - label: bounded_vec![0xb, 0xe, 0xe, 0xf], - start_block: 100, - end_block: 110, - storage_price_per_block: 5, - provider_collateral: 25, - state: DealState::Published, - } - } -} - -impl DealProposalBuilder { - pub fn client(mut self, client: &str) -> Self { - self.client = account(client); - self - } - - pub fn provider(mut self, provider: &str) -> Self { - self.provider = account(provider); - self - } - - pub fn unsigned(self) -> DealProposalOf { - DealProposalOf:: { - piece_cid: self.piece_cid, - piece_size: self.piece_size, - client: self.client, - provider: self.provider, - label: self.label, - start_block: self.start_block, - end_block: self.end_block, - storage_price_per_block: self.storage_price_per_block, - provider_collateral: self.provider_collateral, - state: self.state, - } - } - - pub fn signed(self, by: &str) -> ClientDealProposalOf { - let built = self.unsigned(); - let signed = sign_proposal(by, built); - signed - } -} - -type DealProposalOf = - DealProposal<::AccountId, BalanceOf, BlockNumberFor>; - -type ClientDealProposalOf = ClientDealProposal< - ::AccountId, - BalanceOf, - BlockNumberFor, - MultiSignature, ->; - -fn sign(pair: &sp_core::sr25519::Pair, bytes: &[u8]) -> MultiSignature { - MultiSignature::Sr25519(pair.sign(bytes)) -} - -fn sign_proposal(client: &str, proposal: DealProposalOf) -> ClientDealProposalOf { - let alice_pair = key_pair(client); - let client_signature = sign(&alice_pair, &Encode::encode(&proposal)); - ClientDealProposal { - proposal, - client_signature, - } -} diff --git a/pallets/storage-provider/src/tests/mod.rs b/pallets/storage-provider/src/tests/mod.rs new file mode 100644 index 000000000..6fe2aba45 --- /dev/null +++ b/pallets/storage-provider/src/tests/mod.rs @@ -0,0 +1,352 @@ +use cid::Cid; +use codec::Encode; +use frame_support::{ + assert_ok, derive_impl, pallet_prelude::ConstU32, parameter_types, sp_runtime::BoundedVec, + traits::Hooks, PalletId, +}; +use frame_system::pallet_prelude::BlockNumberFor; +use multihash_codetable::{Code, MultihashDigest}; +use pallet_market::{BalanceOf, ClientDealProposal, DealProposal, DealState}; +use primitives_proofs::{ + DealId, RegisteredPoStProof, RegisteredSealProof, SectorId, SectorNumber, MAX_DEALS_PER_SECTOR, +}; +use sp_core::{bounded_vec, Pair}; +use sp_runtime::{ + traits::{ConstU64, IdentifyAccount, IdentityLookup, Verify}, + BuildStorage, MultiSignature, MultiSigner, +}; + +use crate::{self as pallet_storage_provider, pallet::CID_CODEC, sector::SectorPreCommitInfo}; + +mod pre_commit_sector; +mod prove_commit_sector; +mod state; +mod storage_provider_registration; + +type Block = frame_system::mocking::MockBlock; +type BlockNumber = u64; + +const MILLISECS_PER_BLOCK: u64 = 12000; +const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK; +const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber); +const HOURS: BlockNumber = MINUTES * 60; +const DAYS: BlockNumber = HOURS * 24; +const YEARS: BlockNumber = DAYS * 365; + +frame_support::construct_runtime!( + pub enum Test { + System: frame_system, + Balances: pallet_balances, + StorageProvider: pallet_storage_provider::pallet, + Market: pallet_market, + } +); + +pub type Signature = MultiSignature; +pub type AccountPublic = ::Signer; +pub type AccountId = ::AccountId; + +#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] +impl frame_system::Config for Test { + type Block = Block; + type AccountData = pallet_balances::AccountData; + type AccountId = AccountId; + type Lookup = IdentityLookup; +} + +#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)] +impl pallet_balances::Config for Test { + type AccountStore = System; +} + +parameter_types! { + pub const MarketPalletId: PalletId = PalletId(*b"spMarket"); +} + +impl pallet_market::Config for Test { + type RuntimeEvent = RuntimeEvent; + type PalletId = MarketPalletId; + type Currency = Balances; + type OffchainSignature = Signature; + type OffchainPublic = AccountPublic; + type MaxDeals = ConstU32<32>; + type BlocksPerDay = ConstU64<1>; + type MinDealDuration = ConstU64<1>; + type MaxDealDuration = ConstU64<30>; + type MaxDealsPerBlock = ConstU32<32>; +} + +parameter_types! { + pub const WpostProvingPeriod: BlockNumber = DAYS; + // Half an hour (=48 per day) + // 30 * 60 = 30 minutes + // SLOT_DURATION is in milliseconds thats why we / 1000 + pub const WpostChallengeWindow: BlockNumber = 30 * 60 / (SLOT_DURATION as BlockNumber / 1000); + pub const MinSectorExpiration: BlockNumber = 180 * DAYS; + pub const MaxSectorExpirationExtension: BlockNumber = 1278 * DAYS; + pub const SectorMaximumLifetime: BlockNumber = YEARS * 5; + pub const MaxProveCommitDuration: BlockNumber = (30 * DAYS) + 150; +} + +impl pallet_storage_provider::Config for Test { + type RuntimeEvent = RuntimeEvent; + type PeerId = BoundedVec>; // Arbitrary length + type Currency = Balances; + type Market = Market; + type WPoStProvingPeriod = WpostProvingPeriod; + type WPoStChallengeWindow = WpostChallengeWindow; + type MinSectorExpiration = MinSectorExpiration; + type MaxSectorExpirationExtension = MaxSectorExpirationExtension; + type SectorMaximumLifetime = SectorMaximumLifetime; + type MaxProveCommitDuration = MaxProveCommitDuration; +} + +type AccountIdOf = ::AccountId; + +type DealProposalOf = + DealProposal<::AccountId, BalanceOf, BlockNumberFor>; + +type ClientDealProposalOf = ClientDealProposal< + ::AccountId, + BalanceOf, + BlockNumberFor, + MultiSignature, +>; + +const ALICE: &'static str = "//Alice"; +const BOB: &'static str = "//Bob"; + +/// Initial funds of all accounts. +const INITIAL_FUNDS: u64 = 100; + +// Build genesis storage according to the mock runtime. +fn new_test_ext() -> sp_io::TestExternalities { + let mut t = frame_system::GenesisConfig::::default() + .build_storage() + .unwrap() + .into(); + + pallet_balances::GenesisConfig:: { + balances: vec![ + (account(ALICE), INITIAL_FUNDS), + (account(BOB), INITIAL_FUNDS), + ], + } + .assimilate_storage(&mut t) + .unwrap(); + + let mut ext = sp_io::TestExternalities::new(t); + ext.execute_with(|| System::set_block_number(1)); + ext +} + +fn events() -> Vec { + let evt = System::events() + .into_iter() + .map(|evt| evt.event) + .collect::>(); + System::reset_events(); + evt +} + +fn cid_of(data: &str) -> cid::Cid { + Cid::new_v1(CID_CODEC, Code::Blake2b256.digest(data.as_bytes())) +} + +fn sign(pair: &sp_core::sr25519::Pair, bytes: &[u8]) -> MultiSignature { + MultiSignature::Sr25519(pair.sign(bytes)) +} + +fn sign_proposal(client: &str, proposal: DealProposalOf) -> ClientDealProposalOf { + let alice_pair = key_pair(client); + let client_signature = sign(&alice_pair, &Encode::encode(&proposal)); + ClientDealProposal { + proposal, + client_signature, + } +} + +fn key_pair(name: &str) -> sp_core::sr25519::Pair { + sp_core::sr25519::Pair::from_string(name, None).unwrap() +} + +fn account(name: &str) -> AccountIdOf { + let user_pair = key_pair(name); + let signer = MultiSigner::Sr25519(user_pair.public()); + signer.into_account() +} + +/// Run until a particular block. +/// +/// Stolen't from: +fn run_to_block(n: u64) { + while System::block_number() < n { + if System::block_number() > 1 { + StorageProvider::on_finalize(System::block_number()); + System::on_finalize(System::block_number()); + } + + System::set_block_number(System::block_number() + 1); + System::on_initialize(System::block_number()); + StorageProvider::on_initialize(System::block_number()); + } +} + +/// Register account as a provider. +fn register_storage_provider(account: AccountIdOf) { + let peer_id = "storage_provider_1".as_bytes().to_vec(); + let peer_id = BoundedVec::try_from(peer_id).unwrap(); + let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; + + // Register account as a storage provider. + assert_ok!(StorageProvider::register_storage_provider( + RuntimeOrigin::signed(account), + peer_id.clone(), + window_post_type, + )); + + // Remove any events that were triggered during registration. + System::reset_events(); +} + +struct SectorPreCommitInfoBuilder { + seal_proof: RegisteredSealProof, + sector_number: Option, + sealed_cid: Option, + deal_ids: Option>>, + expiration: u64, + unsealed_cid: Option, +} + +impl Default for SectorPreCommitInfoBuilder { + fn default() -> Self { + Self { + seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, + sector_number: None, + sealed_cid: None, + deal_ids: None, + expiration: YEARS, + unsealed_cid: None, + } + } +} + +impl SectorPreCommitInfoBuilder { + pub fn sector_number(mut self, sector_number: u64) -> Self { + self.sector_number = Some(sector_number); + self + } + + pub fn deals(mut self, deal_ids: I) -> Self + where + I: IntoIterator, + { + let deal_ids_vec = deal_ids.into_iter().collect::>(); + self.deal_ids = Some(BoundedVec::try_from(deal_ids_vec).unwrap()); + self + } + + pub fn sealed_cid(mut self, data: &str) -> Self { + self.sealed_cid = Some( + cid_of(data) + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + ); + self + } + + pub fn unsealed_cid(mut self, data: &str) -> Self { + self.unsealed_cid = Some( + cid_of(data) + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + ); + self + } + + pub fn expiration(mut self, expiration: u64) -> Self { + self.expiration = expiration; + self + } + + pub fn build(self) -> SectorPreCommitInfo { + SectorPreCommitInfo { + seal_proof: self.seal_proof, + sector_number: self.sector_number.expect("sector number is required"), + sealed_cid: self.sealed_cid.expect("sealed cid is required"), + deal_ids: self.deal_ids.expect("deal ids are required"), + expiration: self.expiration, + unsealed_cid: self.unsealed_cid.expect("unsealed cid is required"), + } + } +} + +/// Builder to simplify writing complex tests of [`DealProposal`]. +/// Exclusively uses [`Test`] for simplification purposes. +struct DealProposalBuilder { + piece_cid: BoundedVec>, + piece_size: u64, + client: AccountIdOf, + provider: AccountIdOf, + label: BoundedVec>, + start_block: u64, + end_block: u64, + storage_price_per_block: u64, + provider_collateral: u64, + state: DealState, +} + +impl Default for DealProposalBuilder { + fn default() -> Self { + Self { + piece_cid: cid_of("polka-storage-data") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + piece_size: 18, + client: account(BOB), + provider: account(ALICE), + label: bounded_vec![0xb, 0xe, 0xe, 0xf], + start_block: 100, + end_block: 110, + storage_price_per_block: 5, + provider_collateral: 25, + state: DealState::Published, + } + } +} + +impl DealProposalBuilder { + pub fn client(mut self, client: &str) -> Self { + self.client = account(client); + self + } + + pub fn provider(mut self, provider: &str) -> Self { + self.provider = account(provider); + self + } + + pub fn unsigned(self) -> DealProposalOf { + DealProposalOf:: { + piece_cid: self.piece_cid, + piece_size: self.piece_size, + client: self.client, + provider: self.provider, + label: self.label, + start_block: self.start_block, + end_block: self.end_block, + storage_price_per_block: self.storage_price_per_block, + provider_collateral: self.provider_collateral, + state: self.state, + } + } + + pub fn signed(self, by: &str) -> ClientDealProposalOf { + let built = self.unsigned(); + let signed = sign_proposal(by, built); + signed + } +} diff --git a/pallets/storage-provider/src/tests/pre_commit_sector.rs b/pallets/storage-provider/src/tests/pre_commit_sector.rs new file mode 100644 index 000000000..2b33da84b --- /dev/null +++ b/pallets/storage-provider/src/tests/pre_commit_sector.rs @@ -0,0 +1,318 @@ +use frame_support::{assert_noop, assert_ok}; +use sp_runtime::{BoundedVec, DispatchError}; + +use super::new_test_ext; +use crate::{ + pallet::{Error, Event, StorageProviders}, + sector::SECTORS_MAX, + tests::{ + account, events, register_storage_provider, run_to_block, Balances, MaxProveCommitDuration, + MaxSectorExpirationExtension, RuntimeEvent, RuntimeOrigin, SectorPreCommitInfoBuilder, + StorageProvider, Test, ALICE, + }, +}; + +#[test] +fn successfully_precommited() { + new_test_ext().execute_with(|| { + // Register ALICE as a storage provider. + let storage_provider = ALICE; + register_storage_provider(account(storage_provider)); + + // Sector to be pre-committed. + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); + + // Check starting balance + assert_eq!(Balances::free_balance(account(storage_provider)), 100); + + // Run pre commit extrinsic + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone(), + ) + .expect("Pre commit failed"); + + // Check that the events were triggered + assert_eq!( + events(), + [ + RuntimeEvent::Balances(pallet_balances::Event::::Reserved { + who: account(storage_provider), + amount: 1 + },), + RuntimeEvent::StorageProvider(Event::::SectorPreCommitted { + owner: account(storage_provider), + sector: sector.clone(), + }) + ] + ); + + let sp_alice = StorageProviders::::get(account(storage_provider)) + .expect("SP Alice should be present because of the pre-check"); + + assert!(sp_alice.sectors.is_empty()); // not yet proven + assert!(!sp_alice.pre_committed_sectors.is_empty()); + assert_eq!(sp_alice.pre_commit_deposits, 1); + assert_eq!(Balances::free_balance(account(storage_provider)), 99); + }); +} + +#[test] +fn fails_should_be_signed() { + new_test_ext().execute_with(|| { + // Sector to be pre-committed + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector(RuntimeOrigin::none(), sector.clone()), + DispatchError::BadOrigin, + ); + }); +} + +#[test] +fn fails_storage_provider_not_found() { + new_test_ext().execute_with(|| { + // Sector to be pre-committed + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(ALICE)), + sector.clone() + ), + Error::::StorageProviderNotFound, + ); + }); +} + +#[test] +fn fails_sector_number_already_used() { + new_test_ext().execute_with(|| { + // Register ALICE as a storage provider. + let storage_provider = ALICE; + register_storage_provider(account(storage_provider)); + + // Sector to be pre-committed + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); + + // Run pre commit extrinsic + assert_ok!(StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + )); + // Run same extrinsic, this should fail + assert_noop!( + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + ), + Error::::SectorNumberAlreadyUsed, + ); + }); +} + +#[test] +fn fails_invalid_sector() { + new_test_ext().execute_with(|| { + // Register ALICE as a storage provider. + let storage_provider = ALICE; + register_storage_provider(account(storage_provider)); + + // Sector to be pre-committed + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(SECTORS_MAX as u64 + 1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + ), + Error::::InvalidSector, + ); + }); +} + +#[test] +fn fails_invalid_cid() { + new_test_ext().execute_with(|| { + // Register ALICE as a storage provider. + let storage_provider = ALICE; + register_storage_provider(account(storage_provider)); + + // Sector to be pre-committed + let mut sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); + + // Setting the wrong unseal cid on the sector + sector.unsealed_cid = BoundedVec::new(); + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + ), + Error::::InvalidCid, + ); + }); +} + +#[test] +fn fails_expiration_before_activation() { + new_test_ext().execute_with(|| { + run_to_block(1000); + + // Register ALICE as a storage provider. + let storage_provider = ALICE; + register_storage_provider(account(&storage_provider)); + + // Sector to be pre-committed + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .expiration(1000) + .build(); + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + ), + Error::::ExpirationBeforeActivation, + ); + }); +} + +#[test] +fn fails_expiration_too_soon() { + let current_height = 1000; + + new_test_ext().execute_with(|| { + run_to_block(current_height); + + // Register ALICE as a storage provider. + let storage_provider = ALICE; + register_storage_provider(account(storage_provider)); + + // Sector to be pre-committed + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + // Set expiration to be in the next block after the maximum + // allowed activation. + .expiration(current_height + MaxProveCommitDuration::get() + 1) + .build(); + + assert_noop!( + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + ), + Error::::ExpirationTooSoon, + ); + }); +} + +#[test] +fn fails_expiration_too_long() { + let current_height = 1000; + + new_test_ext().execute_with(|| { + run_to_block(current_height); + + // Register ALICE as a storage provider. + let storage_provider = ALICE; + register_storage_provider(account(storage_provider)); + + // Sector to be pre-committed + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + // Set expiration to be in the next block after the maximum + // allowed + .expiration(current_height + MaxSectorExpirationExtension::get() + 1) + .build(); + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + ), + Error::::ExpirationTooLong, + ); + }); +} + +// TODO(no-ref,@cernicc,11/07/2024): Based on the current setup I can't get +// this test to pass. That is because the `SectorMaximumLifetime` is longer +// then the bound for `ExpirationTooLong`. Is the test wrong? is the +// implementation wrong? +// +// #[test] +// fn fails_max_sector_lifetime_exceeded() { +// let current_height = 1000; + +// new_test_ext_with_block(current_height).execute_with(|| { +// // Register ALICE as a storage provider. +// let storage_provider = ALICE; +// register_storage_provider(account(storage_provider)); + +// // Sector to be pre-committed +// let sector = SectorPreCommitInfoBuilder::default() +// .sector_number(1) +// .deals([0, 1]) +// .sealed_cid("sealed_cid") +// .unsealed_cid("unsealed_cid") +// .expiration(current_height + MaxProveCommitDuration::get() + SectorMaximumLifetime::get()) +// .build(); + +// // Run pre commit extrinsic +// assert_noop!( +// StorageProvider::pre_commit_sector( +// RuntimeOrigin::signed(account(storage_provider)), +// sector.clone() +// ), +// Error::::MaxSectorLifetimeExceeded, +// ); +// }); +// } diff --git a/pallets/storage-provider/src/tests/prove_commit_sector.rs b/pallets/storage-provider/src/tests/prove_commit_sector.rs new file mode 100644 index 000000000..446523214 --- /dev/null +++ b/pallets/storage-provider/src/tests/prove_commit_sector.rs @@ -0,0 +1,233 @@ +use frame_support::{assert_noop, assert_ok}; +use sp_core::bounded_vec; +use sp_runtime::DispatchError; + +use super::{new_test_ext, MaxProveCommitDuration}; +use crate::{ + pallet::{Error, Event, StorageProviders}, + sector::ProveCommitSector, + tests::{ + account, events, register_storage_provider, run_to_block, Balances, DealProposalBuilder, + Market, RuntimeEvent, RuntimeOrigin, SectorPreCommitInfoBuilder, StorageProvider, System, + Test, ALICE, BOB, + }, +}; + +#[test] +fn successfully_prove_sector() { + new_test_ext().execute_with(|| { + // Setup accounts + let storage_provider = ALICE; + let storage_client = BOB; + + // Register storage provider + register_storage_provider(account(storage_provider)); + + // Add balance to the market pallet + assert_ok!(Market::add_balance( + RuntimeOrigin::signed(account(storage_provider)), + 60 + )); + assert_ok!(Market::add_balance( + RuntimeOrigin::signed(account(storage_client)), + 70 + )); + + // Generate a deal proposal + let deal_proposal = DealProposalBuilder::default() + .client(storage_client) + .provider(storage_provider) + .signed(storage_client); + + // Publish the deal proposal + assert_ok!(Market::publish_storage_deals( + RuntimeOrigin::signed(account(storage_provider)), + bounded_vec![deal_proposal], + )); + + // Sector to be pre-committed and proven + let sector_number = 1; + + // Sector data + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(sector_number) + .deals([0]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); + + // Run pre commit extrinsic + assert_ok!(StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + )); + + // Remove any events that were triggered until now. + System::reset_events(); + + // Test prove commits + let sector = ProveCommitSector { + sector_number, + proof: bounded_vec![0xd, 0xe, 0xa, 0xd], + }; + + assert_ok!(StorageProvider::prove_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector + )); + assert_eq!( + events(), + [ + RuntimeEvent::Market(pallet_market::Event::DealActivated { + deal_id: 0, + client: account(storage_client), + provider: account(storage_provider) + }), + RuntimeEvent::StorageProvider(Event::::SectorProven { + owner: account(storage_provider), + sector_number: sector_number + }) + ] + ); + + // check that the funds are still locked + assert_eq!(Balances::free_balance(account(storage_provider)), 39); + let sp_state = StorageProviders::::get(account(storage_provider)) + .expect("Should be able to get providers info"); + + // check that the sector has been activated + assert!(!sp_state.sectors.is_empty()); + assert!(sp_state.sectors.contains_key(§or_number)); + }); +} + +#[test] +fn fails_should_be_signed() { + new_test_ext().execute_with(|| { + // Sector to be pre-committed + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(1) + .deals([0, 1]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); + + // Run pre commit extrinsic + assert_noop!( + StorageProvider::pre_commit_sector(RuntimeOrigin::none(), sector.clone()), + DispatchError::BadOrigin, + ); + }); +} + +#[test] +fn fails_storage_provider_not_found() { + new_test_ext().execute_with(|| { + // Test prove commits + let sector = ProveCommitSector { + sector_number: 1, + proof: bounded_vec![0xd, 0xe, 0xa, 0xd], + }; + + assert_noop!( + StorageProvider::prove_commit_sector(RuntimeOrigin::signed(account(ALICE)), sector), + Error::::StorageProviderNotFound, + ); + }); +} + +#[test] +fn fails_storage_precommit_missing() { + new_test_ext().execute_with(|| { + let storage_provider = ALICE; + let sector_number = 1; + + // Register storage provider + register_storage_provider(account(storage_provider)); + + // Test prove commits + let sector = ProveCommitSector { + sector_number, + proof: bounded_vec![0xd, 0xe, 0xa, 0xd], + }; + + assert_noop!( + StorageProvider::prove_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector + ), + Error::::InvalidSector, + ); + }); +} + +#[test] +fn fails_prove_commit_after_deadline() { + // Block number at which the precommit is made + let precommit_at_block_number = 1; + // Block number at which the prove commit is made. + let proving_at_block_number = precommit_at_block_number + MaxProveCommitDuration::get(); + + new_test_ext().execute_with(|| { + run_to_block(precommit_at_block_number); + + let storage_provider = ALICE; + let storage_client = BOB; + let sector_number = 1; + + // Register storage provider + register_storage_provider(account(storage_provider)); + + // Add balance to the market pallet + assert_ok!(Market::add_balance( + RuntimeOrigin::signed(account(storage_provider)), + 60 + )); + assert_ok!(Market::add_balance( + RuntimeOrigin::signed(account(storage_client)), + 70 + )); + + // Generate a deal proposal + let deal_proposal = DealProposalBuilder::default() + .client(storage_client) + .provider(storage_provider) + .signed(storage_client); + + // Publish the deal proposal + assert_ok!(Market::publish_storage_deals( + RuntimeOrigin::signed(account(storage_provider)), + bounded_vec![deal_proposal], + )); + + // Sector data + let sector = SectorPreCommitInfoBuilder::default() + .sector_number(sector_number) + .deals([0]) + .sealed_cid("sealed_cid") + .unsealed_cid("unsealed_cid") + .build(); + + // Run pre commit extrinsic + assert_ok!(StorageProvider::pre_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector.clone() + )); + + // Test prove commits + let sector = ProveCommitSector { + sector_number, + proof: bounded_vec![0xd, 0xe, 0xa, 0xd], + }; + + run_to_block(proving_at_block_number); + + assert_noop!( + StorageProvider::prove_commit_sector( + RuntimeOrigin::signed(account(storage_provider)), + sector + ), + Error::::ProveCommitAfterDeadline, + ); + }); +} diff --git a/pallets/storage-provider/src/tests/state.rs b/pallets/storage-provider/src/tests/state.rs new file mode 100644 index 000000000..da68874c8 --- /dev/null +++ b/pallets/storage-provider/src/tests/state.rs @@ -0,0 +1,13 @@ +use super::new_test_ext; +use crate::{ + pallet::StorageProviders, + tests::{account, Test, ALICE, BOB}, +}; + +#[test] +fn initial_state() { + new_test_ext().execute_with(|| { + assert!(!StorageProviders::::contains_key(account(ALICE))); + assert!(!StorageProviders::::contains_key(account(BOB))); + }) +} diff --git a/pallets/storage-provider/src/tests/storage_provider_registration.rs b/pallets/storage-provider/src/tests/storage_provider_registration.rs new file mode 100644 index 000000000..9fd150ac1 --- /dev/null +++ b/pallets/storage-provider/src/tests/storage_provider_registration.rs @@ -0,0 +1,102 @@ +use frame_support::{assert_noop, assert_ok}; +use primitives_proofs::RegisteredPoStProof; +use sp_runtime::{BoundedVec, DispatchError}; + +use super::new_test_ext; +use crate::{ + pallet::{Error, Event, StorageProviders}, + storage_provider::StorageProviderInfo, + tests::{account, events, RuntimeEvent, RuntimeOrigin, StorageProvider, Test, BOB}, +}; + +/// Tests if storage provider registration is successful. +#[test] +fn successful_registration() { + new_test_ext().execute_with(|| { + let peer_id = "storage_provider_1".as_bytes().to_vec(); + let peer_id = BoundedVec::try_from(peer_id).unwrap(); + let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; + let expected_sector_size = window_post_type.sector_size(); + let expected_partition_sectors = window_post_type.window_post_partitions_sector(); + let expected_sp_info = StorageProviderInfo::new(peer_id.clone(), window_post_type); + + // Register BOB as a storage provider. + assert_ok!(StorageProvider::register_storage_provider( + RuntimeOrigin::signed(account(BOB)), + peer_id.clone(), + window_post_type, + )); + assert!(StorageProviders::::contains_key(account(BOB))); + + // `unwrap()` should be safe because of the above check. + let sp_bob = StorageProviders::::get(account(BOB)).unwrap(); + // Check that storage provider information is correct. + assert_eq!(sp_bob.info.peer_id, peer_id); + assert_eq!(sp_bob.info.window_post_proof_type, window_post_type); + assert_eq!(sp_bob.info.sector_size, expected_sector_size); + assert_eq!( + sp_bob.info.window_post_partition_sectors, + expected_partition_sectors + ); + // Check that pre commit sectors are empty. + assert!(sp_bob.pre_committed_sectors.is_empty()); + // Check that no pre commit deposit is made + assert_eq!(sp_bob.pre_commit_deposits, 0); + // Check that sectors are empty. + assert!(sp_bob.sectors.is_empty()); + // Check that the event triggered + assert_eq!( + events(), + [RuntimeEvent::StorageProvider( + Event::::StorageProviderRegistered { + owner: account(BOB), + info: expected_sp_info, + }, + )] + ); + }) +} + +#[test] +fn fails_should_be_signed() { + new_test_ext().execute_with(|| { + let peer_id = "storage_provider_1".as_bytes().to_vec(); + let peer_id = BoundedVec::try_from(peer_id).unwrap(); + let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; + + assert_noop!( + StorageProvider::register_storage_provider( + RuntimeOrigin::none(), + peer_id.clone(), + window_post_type, + ), + DispatchError::BadOrigin + ); + }); +} + +#[test] +fn fails_double_register() { + new_test_ext().execute_with(|| { + let peer_id = "storage_provider_1".as_bytes().to_vec(); + let peer_id = BoundedVec::try_from(peer_id).unwrap(); + let window_post_type = RegisteredPoStProof::StackedDRGWindow2KiBV1P1; + + // Register BOB as a storage provider. + assert_ok!(StorageProvider::register_storage_provider( + RuntimeOrigin::signed(account(BOB)), + peer_id.clone(), + window_post_type, + )); + assert!(StorageProviders::::contains_key(account(BOB))); + // Try to register BOB again. Should fail + assert_noop!( + StorageProvider::register_storage_provider( + RuntimeOrigin::signed(account(BOB)), + peer_id.clone(), + window_post_type, + ), + Error::::StorageProviderExists + ); + }); +} From 18eab0b369744552dbbb57669f42bf75e8646b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Cerni=C4=8D?= Date: Mon, 15 Jul 2024 09:54:26 +0200 Subject: [PATCH 08/11] fix: SectorPreCommitInfoBuilder default impl --- pallets/storage-provider/src/tests/mod.rs | 54 +++++++++--------- .../src/tests/pre_commit_sector.rs | 56 ++----------------- .../src/tests/prove_commit_sector.rs | 11 +--- 3 files changed, 35 insertions(+), 86 deletions(-) diff --git a/pallets/storage-provider/src/tests/mod.rs b/pallets/storage-provider/src/tests/mod.rs index 6fe2aba45..d2af5277b 100644 --- a/pallets/storage-provider/src/tests/mod.rs +++ b/pallets/storage-provider/src/tests/mod.rs @@ -211,29 +211,35 @@ fn register_storage_provider(account: AccountIdOf) { struct SectorPreCommitInfoBuilder { seal_proof: RegisteredSealProof, - sector_number: Option, - sealed_cid: Option, - deal_ids: Option>>, + sector_number: SectorNumber, + sealed_cid: SectorId, + deal_ids: BoundedVec>, expiration: u64, - unsealed_cid: Option, + unsealed_cid: SectorId, } impl Default for SectorPreCommitInfoBuilder { fn default() -> Self { Self { seal_proof: RegisteredSealProof::StackedDRG2KiBV1P1, - sector_number: None, - sealed_cid: None, - deal_ids: None, + sector_number: 1, + sealed_cid: cid_of("sealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), + deal_ids: bounded_vec![0, 1], expiration: YEARS, - unsealed_cid: None, + unsealed_cid: cid_of("unsealed_cid") + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"), } } } impl SectorPreCommitInfoBuilder { pub fn sector_number(mut self, sector_number: u64) -> Self { - self.sector_number = Some(sector_number); + self.sector_number = sector_number; self } @@ -242,27 +248,23 @@ impl SectorPreCommitInfoBuilder { I: IntoIterator, { let deal_ids_vec = deal_ids.into_iter().collect::>(); - self.deal_ids = Some(BoundedVec::try_from(deal_ids_vec).unwrap()); + self.deal_ids = BoundedVec::try_from(deal_ids_vec).unwrap(); self } pub fn sealed_cid(mut self, data: &str) -> Self { - self.sealed_cid = Some( - cid_of(data) - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - ); + self.sealed_cid = cid_of(data) + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"); self } pub fn unsealed_cid(mut self, data: &str) -> Self { - self.unsealed_cid = Some( - cid_of(data) - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"), - ); + self.unsealed_cid = cid_of(data) + .to_bytes() + .try_into() + .expect("hash is always 32 bytes"); self } @@ -274,11 +276,11 @@ impl SectorPreCommitInfoBuilder { pub fn build(self) -> SectorPreCommitInfo { SectorPreCommitInfo { seal_proof: self.seal_proof, - sector_number: self.sector_number.expect("sector number is required"), - sealed_cid: self.sealed_cid.expect("sealed cid is required"), - deal_ids: self.deal_ids.expect("deal ids are required"), + sector_number: self.sector_number, + sealed_cid: self.sealed_cid, + deal_ids: self.deal_ids, expiration: self.expiration, - unsealed_cid: self.unsealed_cid.expect("unsealed cid is required"), + unsealed_cid: self.unsealed_cid, } } } diff --git a/pallets/storage-provider/src/tests/pre_commit_sector.rs b/pallets/storage-provider/src/tests/pre_commit_sector.rs index 2b33da84b..36d07840c 100644 --- a/pallets/storage-provider/src/tests/pre_commit_sector.rs +++ b/pallets/storage-provider/src/tests/pre_commit_sector.rs @@ -20,12 +20,7 @@ fn successfully_precommited() { register_storage_provider(account(storage_provider)); // Sector to be pre-committed. - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); + let sector = SectorPreCommitInfoBuilder::default().build(); // Check starting balance assert_eq!(Balances::free_balance(account(storage_provider)), 100); @@ -66,12 +61,7 @@ fn successfully_precommited() { fn fails_should_be_signed() { new_test_ext().execute_with(|| { // Sector to be pre-committed - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); + let sector = SectorPreCommitInfoBuilder::default().build(); // Run pre commit extrinsic assert_noop!( @@ -85,12 +75,7 @@ fn fails_should_be_signed() { fn fails_storage_provider_not_found() { new_test_ext().execute_with(|| { // Sector to be pre-committed - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); + let sector = SectorPreCommitInfoBuilder::default().build(); // Run pre commit extrinsic assert_noop!( @@ -111,12 +96,7 @@ fn fails_sector_number_already_used() { register_storage_provider(account(storage_provider)); // Sector to be pre-committed - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); + let sector = SectorPreCommitInfoBuilder::default().build(); // Run pre commit extrinsic assert_ok!(StorageProvider::pre_commit_sector( @@ -127,7 +107,7 @@ fn fails_sector_number_already_used() { assert_noop!( StorageProvider::pre_commit_sector( RuntimeOrigin::signed(account(storage_provider)), - sector.clone() + sector ), Error::::SectorNumberAlreadyUsed, ); @@ -144,9 +124,6 @@ fn fails_invalid_sector() { // Sector to be pre-committed let sector = SectorPreCommitInfoBuilder::default() .sector_number(SECTORS_MAX as u64 + 1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") .build(); // Run pre commit extrinsic @@ -168,12 +145,7 @@ fn fails_invalid_cid() { register_storage_provider(account(storage_provider)); // Sector to be pre-committed - let mut sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); + let mut sector = SectorPreCommitInfoBuilder::default().build(); // Setting the wrong unseal cid on the sector sector.unsealed_cid = BoundedVec::new(); @@ -200,10 +172,6 @@ fn fails_expiration_before_activation() { // Sector to be pre-committed let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") .expiration(1000) .build(); @@ -231,10 +199,6 @@ fn fails_expiration_too_soon() { // Sector to be pre-committed let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") // Set expiration to be in the next block after the maximum // allowed activation. .expiration(current_height + MaxProveCommitDuration::get() + 1) @@ -263,10 +227,6 @@ fn fails_expiration_too_long() { // Sector to be pre-committed let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") // Set expiration to be in the next block after the maximum // allowed .expiration(current_height + MaxSectorExpirationExtension::get() + 1) @@ -299,10 +259,6 @@ fn fails_expiration_too_long() { // // Sector to be pre-committed // let sector = SectorPreCommitInfoBuilder::default() -// .sector_number(1) -// .deals([0, 1]) -// .sealed_cid("sealed_cid") -// .unsealed_cid("unsealed_cid") // .expiration(current_height + MaxProveCommitDuration::get() + SectorMaximumLifetime::get()) // .build(); diff --git a/pallets/storage-provider/src/tests/prove_commit_sector.rs b/pallets/storage-provider/src/tests/prove_commit_sector.rs index 446523214..006bc471d 100644 --- a/pallets/storage-provider/src/tests/prove_commit_sector.rs +++ b/pallets/storage-provider/src/tests/prove_commit_sector.rs @@ -52,8 +52,6 @@ fn successfully_prove_sector() { let sector = SectorPreCommitInfoBuilder::default() .sector_number(sector_number) .deals([0]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") .build(); // Run pre commit extrinsic @@ -105,12 +103,7 @@ fn successfully_prove_sector() { fn fails_should_be_signed() { new_test_ext().execute_with(|| { // Sector to be pre-committed - let sector = SectorPreCommitInfoBuilder::default() - .sector_number(1) - .deals([0, 1]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") - .build(); + let sector = SectorPreCommitInfoBuilder::default().build(); // Run pre commit extrinsic assert_noop!( @@ -204,8 +197,6 @@ fn fails_prove_commit_after_deadline() { let sector = SectorPreCommitInfoBuilder::default() .sector_number(sector_number) .deals([0]) - .sealed_cid("sealed_cid") - .unsealed_cid("unsealed_cid") .build(); // Run pre commit extrinsic From 03be46d8e03435ca5b8d5f5a1a43da83bee50614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Cerni=C4=8D?= Date: Mon, 15 Jul 2024 10:08:40 +0200 Subject: [PATCH 09/11] fix: some changes --- pallets/storage-provider/src/tests/mod.rs | 24 ++----------------- .../src/tests/prove_commit_sector.rs | 4 ++-- primitives/proofs/src/traits.rs | 3 +-- 3 files changed, 5 insertions(+), 26 deletions(-) diff --git a/pallets/storage-provider/src/tests/mod.rs b/pallets/storage-provider/src/tests/mod.rs index d2af5277b..df70eb741 100644 --- a/pallets/storage-provider/src/tests/mod.rs +++ b/pallets/storage-provider/src/tests/mod.rs @@ -243,28 +243,8 @@ impl SectorPreCommitInfoBuilder { self } - pub fn deals(mut self, deal_ids: I) -> Self - where - I: IntoIterator, - { - let deal_ids_vec = deal_ids.into_iter().collect::>(); - self.deal_ids = BoundedVec::try_from(deal_ids_vec).unwrap(); - self - } - - pub fn sealed_cid(mut self, data: &str) -> Self { - self.sealed_cid = cid_of(data) - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"); - self - } - - pub fn unsealed_cid(mut self, data: &str) -> Self { - self.unsealed_cid = cid_of(data) - .to_bytes() - .try_into() - .expect("hash is always 32 bytes"); + pub fn deals(mut self, deal_ids: Vec) -> Self { + self.deal_ids = BoundedVec::try_from(deal_ids).unwrap(); self } diff --git a/pallets/storage-provider/src/tests/prove_commit_sector.rs b/pallets/storage-provider/src/tests/prove_commit_sector.rs index 006bc471d..940306bad 100644 --- a/pallets/storage-provider/src/tests/prove_commit_sector.rs +++ b/pallets/storage-provider/src/tests/prove_commit_sector.rs @@ -51,7 +51,7 @@ fn successfully_prove_sector() { // Sector data let sector = SectorPreCommitInfoBuilder::default() .sector_number(sector_number) - .deals([0]) + .deals(vec![0]) .build(); // Run pre commit extrinsic @@ -196,7 +196,7 @@ fn fails_prove_commit_after_deadline() { // Sector data let sector = SectorPreCommitInfoBuilder::default() .sector_number(sector_number) - .deals([0]) + .deals(vec![0]) .build(); // Run pre commit extrinsic diff --git a/primitives/proofs/src/traits.rs b/primitives/proofs/src/traits.rs index 50c86a5e4..c66dfd3b6 100644 --- a/primitives/proofs/src/traits.rs +++ b/primitives/proofs/src/traits.rs @@ -7,9 +7,8 @@ use crate::types::{DealId, RegisteredSealProof, SectorNumber}; /// Size of a CID with a 512-bit multihash — i.e. the default CID size. const CID_SIZE_IN_BYTES: u32 = 64; -// TODO(no-ref,@cernicc,11/07/2024): Refactor to a new-type. SectorId should +// TODO(#129,@cernicc,11/07/2024): Refactor to a new-type. SectorId should // always be a valid CID. That should be checked before initializing it. -// /// The CID (in bytes) of a given sector. pub type SectorId = BoundedVec>; From b1194927a3200dbf9adbc5fbf472d412aa4c7bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Cerni=C4=8D?= Date: Mon, 15 Jul 2024 11:06:31 +0200 Subject: [PATCH 10/11] fix: import format --- pallets/storage-provider/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index 2b09c1926..ce7ad6e41 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -40,7 +40,9 @@ pub mod pallet { traits::{Currency, ReservableCurrency}, }; use frame_system::{ensure_signed, pallet_prelude::*, Config as SystemConfig}; - use primitives_proofs::{Market, RegisteredPoStProof, RegisteredSealProof, SectorNumber}; + use primitives_proofs::{ + Market, RegisteredPoStProof, RegisteredSealProof, SectorDeal, SectorNumber, + }; use scale_info::TypeInfo; use crate::{ From 06ff7ecb6352f3718d6a1276532168f5885546f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Cerni=C4=8D?= Date: Mon, 15 Jul 2024 11:27:24 +0200 Subject: [PATCH 11/11] fix: build --- pallets/storage-provider/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pallets/storage-provider/src/lib.rs b/pallets/storage-provider/src/lib.rs index ce7ad6e41..2b09c1926 100644 --- a/pallets/storage-provider/src/lib.rs +++ b/pallets/storage-provider/src/lib.rs @@ -40,9 +40,7 @@ pub mod pallet { traits::{Currency, ReservableCurrency}, }; use frame_system::{ensure_signed, pallet_prelude::*, Config as SystemConfig}; - use primitives_proofs::{ - Market, RegisteredPoStProof, RegisteredSealProof, SectorDeal, SectorNumber, - }; + use primitives_proofs::{Market, RegisteredPoStProof, RegisteredSealProof, SectorNumber}; use scale_info::TypeInfo; use crate::{