From 17f640336f01949624e6993de2b3c28fdb86044b Mon Sep 17 00:00:00 2001 From: nacadmin Date: Fri, 27 Feb 2026 19:58:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=A8=E7=B3=BB=E7=BB=9F=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=8A=95=E8=B5=84=E9=A3=8E=E9=99=A9=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E6=A8=AA=E5=B9=85=20-=20NAC=E6=98=AF=E5=85=AC=E9=93=BE?= =?UTF-8?q?=E9=9D=9E=E6=8A=95=E8=B5=84=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nac-sdk/Cargo.lock | 1584 +++++++++++++++-- nac-sdk/src/genesis_config.rs | 144 ++ nac-sdk/src/lib.rs | 3 + nac-udm/src/l1_protocol/layer_router.rs | 22 +- .../l1_protocol/layer_router.rs.bak.20260227 | 626 +++++++ .../l1_protocol/state_shard_optimization.rs | 2 +- .../state_shard_optimization.rs.bak.20260227 | 561 ++++++ 7 files changed, 2740 insertions(+), 202 deletions(-) create mode 100644 nac-sdk/src/genesis_config.rs create mode 100644 nac-udm/src/l1_protocol/layer_router.rs.bak.20260227 create mode 100644 nac-udm/src/l1_protocol/state_shard_optimization.rs.bak.20260227 diff --git a/nac-sdk/Cargo.lock b/nac-sdk/Cargo.lock index 215673b..eb6a7eb 100644 --- a/nac-sdk/Cargo.lock +++ b/nac-sdk/Cargo.lock @@ -2,6 +2,41 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common", + "generic-array", +] + +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "aes-gcm" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ghash", + "subtle", +] + [[package]] name = "aho-corasick" version = "1.1.4" @@ -11,6 +46,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "android_system_properties" version = "0.1.5" @@ -20,56 +61,6 @@ dependencies = [ "libc", ] -[[package]] -name = "anstream" -version = "0.6.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" -dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "is_terminal_polyfill", - "utf8parse", -] - -[[package]] -name = "anstyle" -version = "1.0.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" - -[[package]] -name = "anstyle-parse" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" -dependencies = [ - "utf8parse", -] - -[[package]] -name = "anstyle-query" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" -dependencies = [ - "windows-sys 0.61.2", -] - -[[package]] -name = "anstyle-wincon" -version = "3.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" -dependencies = [ - "anstyle", - "once_cell_polyfill", - "windows-sys 0.61.2", -] - [[package]] name = "anyhow" version = "1.0.101" @@ -98,6 +89,48 @@ dependencies = [ "serde_json", ] +[[package]] +name = "async-io" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456b8a8feb6f42d237746d4b3e9a178494627745c3c56c6ea55d92ba50d026fc" +dependencies = [ + "autocfg", + "cfg-if", + "concurrent-queue", + "futures-io", + "futures-lite", + "parking", + "polling", + "rustix", + "slab", + "windows-sys 0.61.2", +] + +[[package]] +name = "async-trait" +version = "0.1.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "asynchronous-codec" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a860072022177f903e59730004fb5dc13db9275b79bb2aef7ba8ce831956c233" +dependencies = [ + "bytes", + "futures-sink", + "futures-util", + "memchr", + "pin-project-lite", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -110,6 +143,22 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" +[[package]] +name = "base-x" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cbbc9d0964165b47557570cce6c952866c2678457aca742aafc9fb771d30270" + +[[package]] +name = "base256emoji" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e9430d9a245a77c92176e649af6e275f20839a48389859d1661e9a128d077c" +dependencies = [ + "const-str", + "match-lookup", +] + [[package]] name = "base64" version = "0.21.7" @@ -155,6 +204,15 @@ dependencies = [ "wyz", ] +[[package]] +name = "blake2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" +dependencies = [ + "digest", +] + [[package]] name = "blake3" version = "1.8.3" @@ -178,6 +236,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bs58" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf88ba1141d185c399bee5288d850d63b8369520c1eafc32a0430b5b6c287bf4" +dependencies = [ + "tinyvec", +] + [[package]] name = "bumpalo" version = "3.19.1" @@ -218,6 +285,30 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "chacha20" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "chacha20poly1305" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10cd79432192d1c0f4e1a0fef9527696cc039165d729fb41b3f4f4f354c2dc35" +dependencies = [ + "aead", + "chacha20", + "cipher", + "poly1305", + "zeroize", +] + [[package]] name = "chrono" version = "0.4.43" @@ -233,10 +324,15 @@ dependencies = [ ] [[package]] -name = "colorchoice" -version = "1.0.4" +name = "cipher" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", + "zeroize", +] [[package]] name = "colored" @@ -247,12 +343,27 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "const-oid" version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" +[[package]] +name = "const-str" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f421161cb492475f1661ddc9815a745a1c894592070661180fdec3d4872e9c3" + [[package]] name = "const_format" version = "0.2.35" @@ -295,6 +406,15 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "core2" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b49ba7ef1ad6107f8824dbe97de947cbaac53c44e7f9756a1fba0d37c1eec505" +dependencies = [ + "memchr", +] + [[package]] name = "cpufeatures" version = "0.2.17" @@ -304,6 +424,12 @@ dependencies = [ "libc", ] +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + [[package]] name = "crunchy" version = "0.2.4" @@ -317,9 +443,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ "generic-array", + "rand_core 0.6.4", "typenum", ] +[[package]] +name = "ctr" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" +dependencies = [ + "cipher", +] + [[package]] name = "curve25519-dalek" version = "4.1.3" @@ -347,12 +483,45 @@ dependencies = [ "syn", ] +[[package]] +name = "dashmap" +version = "5.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" +dependencies = [ + "cfg-if", + "hashbrown 0.14.5", + "lock_api", + "once_cell", + "parking_lot_core", +] + [[package]] name = "data-encoding" version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7a1e2f27636f116493b8b860f5546edb47c8d8f8ea73e1d2a20be88e28d1fea" +[[package]] +name = "data-encoding-macro" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8142a83c17aa9461d637e649271eae18bf2edd00e91f2e105df36c3c16355bdb" +dependencies = [ + "data-encoding", + "data-encoding-macro-internal", +] + +[[package]] +name = "data-encoding-macro-internal" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ab67060fc6b8ef687992d439ca0fa36e7ed17e9a0b16b25b601e8757df720de" +dependencies = [ + "data-encoding", + "syn", +] + [[package]] name = "der" version = "0.7.10" @@ -371,6 +540,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", "crypto-common", + "subtle", ] [[package]] @@ -384,6 +554,12 @@ dependencies = [ "syn", ] +[[package]] +name = "dtoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c3cf4824e2d5f025c7b531afcb2325364084a16806f6d47fbc1f5fbd9960590" + [[package]] name = "ed25519" version = "2.2.3" @@ -408,6 +584,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + [[package]] name = "encoding_rs" version = "0.8.35" @@ -418,26 +600,15 @@ dependencies = [ ] [[package]] -name = "env_filter" -version = "0.1.4" +name = "enum-as-inner" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bf3c259d255ca70051b30e2e95b5446cdb8949ac4cd22c0d7fd634d89f568e2" +checksum = "a1e6a265c649f3f5979b601d26f1d05ada116434c87741c9493cb56218f76cbc" dependencies = [ - "log", - "regex", -] - -[[package]] -name = "env_logger" -version = "0.11.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13c863f0904021b108aa8b2f55046443e6b1ebde8fd4a15c399893aae4fa069f" -dependencies = [ - "anstream", - "anstyle", - "env_filter", - "jiff", - "log", + "heck", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -486,6 +657,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + [[package]] name = "form_urlencoded" version = "1.2.2" @@ -501,6 +678,31 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" +[[package]] +name = "futures" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-bounded" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91f328e7fb845fc832912fb6a34f40cf6d1888c92f974d1893a54e97b5ff542e" +dependencies = [ + "futures-timer", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.31" @@ -508,6 +710,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -516,6 +719,45 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", + "num_cpus", +] + +[[package]] +name = "futures-io" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718" + +[[package]] +name = "futures-lite" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f78e10609fe0e0b3f4157ffab1876319b5b0db102a2c60dc4626306dc46b44ad" +dependencies = [ + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "futures-macro" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "futures-sink" version = "0.3.31" @@ -528,15 +770,36 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" +[[package]] +name = "futures-ticker" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9763058047f713632a52e916cc7f6a4b3fc6e9fc1ff8c5b1dc49e5a89041682e" +dependencies = [ + "futures", + "futures-timer", + "instant", +] + +[[package]] +name = "futures-timer" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f288b0a4f20f9a56b5d1da57e2227c661b7b16168e2f72365f57b63326e29b24" + [[package]] name = "futures-util" version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" dependencies = [ + "futures-channel", "futures-core", + "futures-io", + "futures-macro", "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", "slab", @@ -575,6 +838,16 @@ dependencies = [ "wasip2", ] +[[package]] +name = "ghash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d8a4362ccb29cb0b265253fb0a2728f592895ee6854fd9bc13f2ffda266ff1" +dependencies = [ + "opaque-debug", + "polyval", +] + [[package]] name = "h2" version = "0.3.27" @@ -613,18 +886,95 @@ dependencies = [ "tracing", ] +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", +] + [[package]] name = "hashbrown" version = "0.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hermit-abi" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" + [[package]] name = "hex" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hex_fmt" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b07f60793ff0a4d9cef0f18e63b5357e06209987153a64648c972c1e5aff336f" + +[[package]] +name = "hickory-proto" +version = "0.24.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92652067c9ce6f66ce53cc38d1169daa36e6e7eb7dd3b63b5103bd9d97117248" +dependencies = [ + "async-trait", + "cfg-if", + "data-encoding", + "enum-as-inner", + "futures-channel", + "futures-io", + "futures-util", + "idna", + "ipnet", + "once_cell", + "rand 0.8.5", + "socket2 0.5.10", + "thiserror 1.0.69", + "tinyvec", + "tracing", + "url", +] + +[[package]] +name = "hkdf" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7" +dependencies = [ + "hmac", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + [[package]] name = "http" version = "0.2.12" @@ -747,9 +1097,9 @@ dependencies = [ "futures-util", "http 0.2.12", "hyper 0.14.32", - "rustls 0.21.12", + "rustls", "tokio", - "tokio-rustls 0.24.1", + "tokio-rustls", ] [[package]] @@ -778,7 +1128,7 @@ dependencies = [ "js-sys", "log", "wasm-bindgen", - "windows-core", + "windows-core 0.62.2", ] [[package]] @@ -892,6 +1242,38 @@ dependencies = [ "icu_properties", ] +[[package]] +name = "if-addrs" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cabb0019d51a643781ff15c9c8a3e5dedc365c47211270f4e8f82812fedd8f0a" +dependencies = [ + "libc", + "windows-sys 0.48.0", +] + +[[package]] +name = "if-watch" +version = "3.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdf9d64cfcf380606e64f9a0bcf493616b65331199f984151a6fa11a7b3cde38" +dependencies = [ + "async-io", + "core-foundation", + "fnv", + "futures", + "if-addrs", + "ipnet", + "log", + "netlink-packet-core", + "netlink-packet-route", + "netlink-proto", + "netlink-sys", + "rtnetlink", + "system-configuration 0.6.1", + "windows", +] + [[package]] name = "impl-codec" version = "0.6.0" @@ -919,7 +1301,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.16.1", +] + +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + +[[package]] +name = "instant" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" +dependencies = [ + "cfg-if", ] [[package]] @@ -928,42 +1328,12 @@ version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" -[[package]] -name = "is_terminal_polyfill" -version = "1.70.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" - [[package]] name = "itoa" version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" -[[package]] -name = "jiff" -version = "0.2.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d89a5b5e10d5a9ad6e5d1f4bd58225f655d6fe9767575a5e8ac5a6fe64e04495" -dependencies = [ - "jiff-static", - "log", - "portable-atomic", - "portable-atomic-util", - "serde_core", -] - -[[package]] -name = "jiff-static" -version = "0.2.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff7a39c8862fc1369215ccf0a8f12dd4598c7f6484704359f0351bd617034dbf" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "js-sys" version = "0.3.85" @@ -983,12 +1353,300 @@ dependencies = [ "cpufeatures", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.180" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" +[[package]] +name = "libp2p" +version = "0.53.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "681fb3f183edfbedd7a57d32ebe5dcdc0b9f94061185acf3c30249349cc6fc99" +dependencies = [ + "bytes", + "either", + "futures", + "futures-timer", + "getrandom 0.2.17", + "instant", + "libp2p-allow-block-list", + "libp2p-connection-limits", + "libp2p-core", + "libp2p-gossipsub", + "libp2p-identity", + "libp2p-kad", + "libp2p-mdns", + "libp2p-metrics", + "libp2p-noise", + "libp2p-swarm", + "libp2p-tcp", + "libp2p-yamux", + "multiaddr", + "pin-project", + "rw-stream-sink", + "thiserror 1.0.69", +] + +[[package]] +name = "libp2p-allow-block-list" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "107b238b794cb83ab53b74ad5dcf7cca3200899b72fe662840cfb52f5b0a32e6" +dependencies = [ + "libp2p-core", + "libp2p-identity", + "libp2p-swarm", + "void", +] + +[[package]] +name = "libp2p-connection-limits" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7cd50a78ccfada14de94cbacd3ce4b0138157f376870f13d3a8422cd075b4fd" +dependencies = [ + "libp2p-core", + "libp2p-identity", + "libp2p-swarm", + "void", +] + +[[package]] +name = "libp2p-core" +version = "0.41.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5a8920cbd8540059a01950c1e5c96ea8d89eb50c51cd366fc18bdf540a6e48f" +dependencies = [ + "either", + "fnv", + "futures", + "futures-timer", + "libp2p-identity", + "multiaddr", + "multihash", + "multistream-select", + "once_cell", + "parking_lot", + "pin-project", + "quick-protobuf", + "rand 0.8.5", + "rw-stream-sink", + "smallvec", + "thiserror 1.0.69", + "tracing", + "unsigned-varint 0.8.0", + "void", + "web-time", +] + +[[package]] +name = "libp2p-gossipsub" +version = "0.46.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d665144a616dadebdc5fff186b1233488cdcd8bfb1223218ff084b6d052c94f7" +dependencies = [ + "asynchronous-codec", + "base64", + "byteorder", + "bytes", + "either", + "fnv", + "futures", + "futures-ticker", + "getrandom 0.2.17", + "hex_fmt", + "instant", + "libp2p-core", + "libp2p-identity", + "libp2p-swarm", + "prometheus-client", + "quick-protobuf", + "quick-protobuf-codec", + "rand 0.8.5", + "regex", + "sha2", + "smallvec", + "tracing", + "void", +] + +[[package]] +name = "libp2p-identity" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0c7892c221730ba55f7196e98b0b8ba5e04b4155651736036628e9f73ed6fc3" +dependencies = [ + "bs58", + "ed25519-dalek", + "hkdf", + "multihash", + "quick-protobuf", + "rand 0.8.5", + "sha2", + "thiserror 2.0.18", + "tracing", + "zeroize", +] + +[[package]] +name = "libp2p-kad" +version = "0.45.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cc5767727d062c4eac74dd812c998f0e488008e82cce9c33b463d38423f9ad2" +dependencies = [ + "arrayvec", + "asynchronous-codec", + "bytes", + "either", + "fnv", + "futures", + "futures-bounded", + "futures-timer", + "instant", + "libp2p-core", + "libp2p-identity", + "libp2p-swarm", + "quick-protobuf", + "quick-protobuf-codec", + "rand 0.8.5", + "sha2", + "smallvec", + "thiserror 1.0.69", + "tracing", + "uint", + "void", +] + +[[package]] +name = "libp2p-mdns" +version = "0.45.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49007d9a339b3e1d7eeebc4d67c05dbf23d300b7d091193ec2d3f26802d7faf2" +dependencies = [ + "data-encoding", + "futures", + "hickory-proto", + "if-watch", + "libp2p-core", + "libp2p-identity", + "libp2p-swarm", + "rand 0.8.5", + "smallvec", + "socket2 0.5.10", + "tracing", + "void", +] + +[[package]] +name = "libp2p-metrics" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdac91ae4f291046a3b2660c039a2830c931f84df2ee227989af92f7692d3357" +dependencies = [ + "futures", + "instant", + "libp2p-core", + "libp2p-gossipsub", + "libp2p-identity", + "libp2p-kad", + "libp2p-swarm", + "pin-project", + "prometheus-client", +] + +[[package]] +name = "libp2p-noise" +version = "0.44.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecd0545ce077f6ea5434bcb76e8d0fe942693b4380aaad0d34a358c2bd05793" +dependencies = [ + "asynchronous-codec", + "bytes", + "curve25519-dalek", + "futures", + "libp2p-core", + "libp2p-identity", + "multiaddr", + "multihash", + "once_cell", + "quick-protobuf", + "rand 0.8.5", + "sha2", + "snow", + "static_assertions", + "thiserror 1.0.69", + "tracing", + "x25519-dalek", + "zeroize", +] + +[[package]] +name = "libp2p-swarm" +version = "0.44.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80cae6cb75f89dbca53862f9ebe0b9f463aa7b302762fcfaafb9e51dcc9b0f7e" +dependencies = [ + "either", + "fnv", + "futures", + "futures-timer", + "instant", + "libp2p-core", + "libp2p-identity", + "lru", + "multistream-select", + "once_cell", + "rand 0.8.5", + "smallvec", + "tracing", + "void", +] + +[[package]] +name = "libp2p-tcp" +version = "0.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b2460fc2748919adff99ecbc1aab296e4579e41f374fb164149bd2c9e529d4c" +dependencies = [ + "futures", + "futures-timer", + "if-watch", + "libc", + "libp2p-core", + "libp2p-identity", + "socket2 0.5.10", + "tracing", +] + +[[package]] +name = "libp2p-yamux" +version = "0.45.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd5265f6b80f94d48a3963541aad183cc598a645755d2f1805a373e41e0716b" +dependencies = [ + "either", + "futures", + "libp2p-core", + "thiserror 1.0.69", + "tracing", + "yamux 0.12.1", + "yamux 0.13.9", +] + +[[package]] +name = "linux-raw-sys" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" + [[package]] name = "litemap" version = "0.8.1" @@ -1010,6 +1668,26 @@ version = "0.4.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" +[[package]] +name = "lru" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38" +dependencies = [ + "hashbrown 0.15.5", +] + +[[package]] +name = "match-lookup" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "757aee279b8bdbb9f9e676796fd459e4207a1f986e87886700abf589f5abf771" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "memchr" version = "2.8.0" @@ -1059,22 +1737,170 @@ dependencies = [ ] [[package]] -name = "nac-sdk" -version = "2.0.0" +name = "multiaddr" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe6351f60b488e04c1d21bc69e56b89cb3f5e8f5d22557d6e8031bdfd79b6961" +dependencies = [ + "arrayref", + "byteorder", + "data-encoding", + "libp2p-identity", + "multibase", + "multihash", + "percent-encoding", + "serde", + "static_assertions", + "unsigned-varint 0.8.0", + "url", +] + +[[package]] +name = "multibase" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8694bb4835f452b0e3bb06dbebb1d6fc5385b6ca1caf2e55fd165c042390ec77" +dependencies = [ + "base-x", + "base256emoji", + "data-encoding", + "data-encoding-macro", +] + +[[package]] +name = "multihash" +version = "0.19.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b430e7953c29dd6a09afc29ff0bb69c6e306329ee6794700aee27b76a1aea8d" +dependencies = [ + "core2", + "unsigned-varint 0.8.0", +] + +[[package]] +name = "multistream-select" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea0df8e5eec2298a62b326ee4f0d7fe1a6b90a09dfcf9df37b38f947a8c42f19" +dependencies = [ + "bytes", + "futures", + "log", + "pin-project", + "smallvec", + "unsigned-varint 0.7.2", +] + +[[package]] +name = "nac-cbpp" +version = "1.0.0" +dependencies = [ + "anyhow", + "chrono", + "hex", + "nac-upgrade-framework", + "rand 0.8.5", + "serde", + "serde_json", + "sha2", + "sha3", + "thiserror 1.0.69", + "tokio", +] + +[[package]] +name = "nac-constitution-state" +version = "0.1.0" +dependencies = [ + "nac-udm", + "nac-upgrade-framework", + "serde", + "serde_json", +] + +[[package]] +name = "nac-csnp-l0" +version = "0.1.0" +dependencies = [ + "blake3", + "dashmap", + "libp2p", + "lru", + "nac-udm", + "nac-upgrade-framework", + "serde", + "serde_json", + "thiserror 1.0.69", + "tokio", + "tracing", +] + +[[package]] +name = "nac-csnp-l1" +version = "0.1.0" +dependencies = [ + "nac-udm", + "nac-upgrade-framework", + "serde", + "serde_json", + "thiserror 1.0.69", +] + +[[package]] +name = "nac-lens" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "blake3", + "bytes", + "ed25519-dalek", + "futures", + "nac-constitution-state", + "nac-csnp-l0", + "nac-csnp-l1", + "nac-udm", + "nac-upgrade-framework", + "serde", + "serde_json", + "sha2", + "thiserror 1.0.69", + "tokio", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "nac-nvm" +version = "1.0.0" +dependencies = [ + "anyhow", + "hex", + "nac-upgrade-framework", + "serde", + "serde_json", + "sha3", + "thiserror 1.0.69", +] + +[[package]] +name = "nac-sdk" +version = "2.1.0" dependencies = [ "anyhow", - "base64", "bincode", "blake3", - "chrono", "ed25519-dalek", - "env_logger", "hex", "js-sys", "log", "mockito", + "nac-cbpp", + "nac-lens", + "nac-nvm", "nac-udm", "nac-upgrade-framework", + "rand 0.8.5", "reqwest", "serde", "serde_json", @@ -1083,6 +1909,8 @@ dependencies = [ "tokio", "tokio-test", "tokio-tungstenite", + "tracing", + "tungstenite", "wasm-bindgen", "wasm-bindgen-futures", "web-sys", @@ -1120,6 +1948,94 @@ dependencies = [ "thiserror 1.0.69", ] +[[package]] +name = "netlink-packet-core" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72724faf704479d67b388da142b186f916188505e7e0b26719019c525882eda4" +dependencies = [ + "anyhow", + "byteorder", + "netlink-packet-utils", +] + +[[package]] +name = "netlink-packet-route" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053998cea5a306971f88580d0829e90f270f940befd7cf928da179d4187a5a66" +dependencies = [ + "anyhow", + "bitflags 1.3.2", + "byteorder", + "libc", + "netlink-packet-core", + "netlink-packet-utils", +] + +[[package]] +name = "netlink-packet-utils" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ede8a08c71ad5a95cdd0e4e52facd37190977039a4704eb82a283f713747d34" +dependencies = [ + "anyhow", + "byteorder", + "paste", + "thiserror 1.0.69", +] + +[[package]] +name = "netlink-proto" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72452e012c2f8d612410d89eea01e2d9b56205274abb35d53f60200b2ec41d60" +dependencies = [ + "bytes", + "futures", + "log", + "netlink-packet-core", + "netlink-sys", + "thiserror 2.0.18", +] + +[[package]] +name = "netlink-sys" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd6c30ed10fa69cc491d491b85cc971f6bdeb8e7367b7cde2ee6cc878d583fae" +dependencies = [ + "bytes", + "libc", + "log", +] + +[[package]] +name = "nix" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b" +dependencies = [ + "bitflags 1.3.2", + "cfg-if", + "libc", +] + +[[package]] +name = "nohash-hasher" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451" + +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -1129,6 +2045,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "once_cell" version = "1.21.3" @@ -1136,10 +2062,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] -name = "once_cell_polyfill" -version = "1.70.2" +name = "opaque-debug" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" [[package]] name = "parity-scale-codec" @@ -1169,6 +2095,12 @@ dependencies = [ "syn", ] +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + [[package]] name = "parking_lot" version = "0.12.5" @@ -1192,12 +2124,38 @@ dependencies = [ "windows-link", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "percent-encoding" version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "pin-project" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677f1add503faace112b9f1373e43e9e054bfdd22ff1a63c1bc485eaec6a6a8a" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e918e4ff8c4549eb882f14b3a4bc8c8bc93de829416eacf579f1207a8fbf861" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "pin-project-lite" version = "0.2.16" @@ -1221,18 +2179,40 @@ dependencies = [ ] [[package]] -name = "portable-atomic" -version = "1.13.1" +name = "polling" +version = "3.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" +checksum = "5d0e4f59085d47d8241c88ead0f274e8a0cb551f3625263c05eb8dd897c34218" +dependencies = [ + "cfg-if", + "concurrent-queue", + "hermit-abi", + "pin-project-lite", + "rustix", + "windows-sys 0.61.2", +] [[package]] -name = "portable-atomic-util" -version = "0.2.5" +name = "poly1305" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a9db96d7fa8782dd8c15ce32ffe8680bbd1e978a43bf51a34d39483540495f5" +checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf" dependencies = [ - "portable-atomic", + "cpufeatures", + "opaque-debug", + "universal-hash", +] + +[[package]] +name = "polyval" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" +dependencies = [ + "cfg-if", + "cpufeatures", + "opaque-debug", + "universal-hash", ] [[package]] @@ -1282,6 +2262,51 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "prometheus-client" +version = "0.22.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "504ee9ff529add891127c4827eb481bd69dc0ebc72e9a682e187db4caa60c3ca" +dependencies = [ + "dtoa", + "itoa", + "parking_lot", + "prometheus-client-derive-encode", +] + +[[package]] +name = "prometheus-client-derive-encode" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "quick-protobuf" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d6da84cc204722a989e01ba2f6e1e276e190f22263d0cb6ce8526fcdb0d2e1f" +dependencies = [ + "byteorder", +] + +[[package]] +name = "quick-protobuf-codec" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15a0580ab32b169745d7a39db2ba969226ca16738931be152a3209b409de2474" +dependencies = [ + "asynchronous-codec", + "bytes", + "quick-protobuf", + "thiserror 1.0.69", + "unsigned-varint 0.8.0", +] + [[package]] name = "quote" version = "1.0.44" @@ -1423,21 +2448,21 @@ dependencies = [ "once_cell", "percent-encoding", "pin-project-lite", - "rustls 0.21.12", + "rustls", "rustls-pemfile", "serde", "serde_json", "serde_urlencoded", "sync_wrapper", - "system-configuration", + "system-configuration 0.5.1", "tokio", - "tokio-rustls 0.24.1", + "tokio-rustls", "tower-service", "url", "wasm-bindgen", "wasm-bindgen-futures", "web-sys", - "webpki-roots 0.25.4", + "webpki-roots", "winreg", ] @@ -1455,6 +2480,23 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rtnetlink" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a552eb82d19f38c3beed3f786bd23aa434ceb9ac43ab44419ca6d67a7e186c0" +dependencies = [ + "futures", + "log", + "netlink-packet-core", + "netlink-packet-route", + "netlink-packet-utils", + "netlink-proto", + "netlink-sys", + "nix", + "thiserror 1.0.69", +] + [[package]] name = "rustc-hex" version = "2.1.0" @@ -1470,6 +2512,19 @@ dependencies = [ "semver", ] +[[package]] +name = "rustix" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146c9e247ccc180c1f61615433868c99f3de3ae256a30a43b49f67c2d9171f34" +dependencies = [ + "bitflags 2.10.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + [[package]] name = "rustls" version = "0.21.12" @@ -1478,24 +2533,10 @@ checksum = "3f56a14d1f48b391359b22f731fd4bd7e43c97f3c50eee276f3aa09c94784d3e" dependencies = [ "log", "ring", - "rustls-webpki 0.101.7", + "rustls-webpki", "sct", ] -[[package]] -name = "rustls" -version = "0.22.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf4ef73721ac7bcd79b2b315da7779d8fc09718c6b3d2d1b2d94850eb8c18432" -dependencies = [ - "log", - "ring", - "rustls-pki-types", - "rustls-webpki 0.102.8", - "subtle", - "zeroize", -] - [[package]] name = "rustls-pemfile" version = "1.0.4" @@ -1505,15 +2546,6 @@ dependencies = [ "base64", ] -[[package]] -name = "rustls-pki-types" -version = "1.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be040f8b0a225e40375822a563fa9524378b9d63112f53e19ffff34df5d33fdd" -dependencies = [ - "zeroize", -] - [[package]] name = "rustls-webpki" version = "0.101.7" @@ -1524,23 +2556,23 @@ dependencies = [ "untrusted", ] -[[package]] -name = "rustls-webpki" -version = "0.102.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9" -dependencies = [ - "ring", - "rustls-pki-types", - "untrusted", -] - [[package]] name = "rustversion" version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "rw-stream-sink" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8c9026ff5d2f23da5e45bbc283f156383001bfb09c4e44256d02c1a685fe9a1" +dependencies = [ + "futures", + "pin-project", + "static_assertions", +] + [[package]] name = "ryu" version = "1.0.22" @@ -1656,6 +2688,15 @@ dependencies = [ "keccak", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shlex" version = "1.3.0" @@ -1699,6 +2740,23 @@ version = "1.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" +[[package]] +name = "snow" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "850948bee068e713b8ab860fe1adc4d109676ab4c3b621fd8147f06b261f2f85" +dependencies = [ + "aes-gcm", + "blake2", + "chacha20poly1305", + "curve25519-dalek", + "rand_core 0.6.4", + "ring", + "rustc_version", + "sha2", + "subtle", +] + [[package]] name = "socket2" version = "0.5.10" @@ -1783,7 +2841,18 @@ checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" dependencies = [ "bitflags 1.3.2", "core-foundation", - "system-configuration-sys", + "system-configuration-sys 0.5.0", +] + +[[package]] +name = "system-configuration" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" +dependencies = [ + "bitflags 2.10.0", + "core-foundation", + "system-configuration-sys 0.6.0", ] [[package]] @@ -1796,6 +2865,16 @@ dependencies = [ "libc", ] +[[package]] +name = "system-configuration-sys" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "tap" version = "1.0.1" @@ -1842,6 +2921,15 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +dependencies = [ + "cfg-if", +] + [[package]] name = "tinystr" version = "0.8.2" @@ -1852,6 +2940,21 @@ dependencies = [ "zerovec", ] +[[package]] +name = "tinyvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa5fdc3bce6191a1dbc8c02d5c8bffcf557bafa17c124c5264a458f1b0613fa" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.49.0" @@ -1886,18 +2989,7 @@ version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" dependencies = [ - "rustls 0.21.12", - "tokio", -] - -[[package]] -name = "tokio-rustls" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "775e0c0f0adb3a2f22a00c4745d728b479985fc15ee7ca6a2608388c5569860f" -dependencies = [ - "rustls 0.22.4", - "rustls-pki-types", + "rustls", "tokio", ] @@ -1931,12 +3023,8 @@ checksum = "c83b561d025642014097b66e6c1bb422783339e0909e4429cde4749d1990bc38" dependencies = [ "futures-util", "log", - "rustls 0.22.4", - "rustls-pki-types", "tokio", - "tokio-rustls 0.25.0", "tungstenite", - "webpki-roots 0.26.11", ] [[package]] @@ -1995,9 +3083,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" dependencies = [ "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tracing-core" version = "0.1.36" @@ -2005,6 +3105,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", ] [[package]] @@ -2026,8 +3152,6 @@ dependencies = [ "httparse", "log", "rand 0.8.5", - "rustls 0.22.4", - "rustls-pki-types", "sha1", "thiserror 1.0.69", "url", @@ -2064,6 +3188,28 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common", + "subtle", +] + +[[package]] +name = "unsigned-varint" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6889a77d49f1f013504cec6bf97a2c730394adedaeb1deb5ea08949a50541105" + +[[package]] +name = "unsigned-varint" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb066959b24b5196ae73cb057f45598450d2c5f71460e98c49b738086eff9c06" + [[package]] name = "untrusted" version = "0.9.0" @@ -2095,10 +3241,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" [[package]] -name = "utf8parse" -version = "0.2.2" +name = "valuable" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" [[package]] name = "version_check" @@ -2106,6 +3252,12 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + [[package]] name = "want" version = "0.3.1" @@ -2199,6 +3351,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "webpki-roots" version = "0.25.4" @@ -2206,21 +3368,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" [[package]] -name = "webpki-roots" -version = "0.26.11" +name = "windows" +version = "0.53.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9" +checksum = "efc5cf48f83140dcaab716eeaea345f9e93d0018fb81162753a3f76c3397b538" dependencies = [ - "webpki-roots 1.0.6", + "windows-core 0.53.0", + "windows-targets 0.52.6", ] [[package]] -name = "webpki-roots" -version = "1.0.6" +name = "windows-core" +version = "0.53.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22cfaf3c063993ff62e73cb4311efde4db1efb31ab78a3e5c457939ad5cc0bed" +checksum = "9dcc5b895a6377f1ab9fa55acedab1fd5ac0db66ad1e6c7f47e28a22e446a5dd" dependencies = [ - "rustls-pki-types", + "windows-result 0.1.2", + "windows-targets 0.52.6", ] [[package]] @@ -2232,7 +3396,7 @@ dependencies = [ "windows-implement", "windows-interface", "windows-link", - "windows-result", + "windows-result 0.4.1", "windows-strings", ] @@ -2264,6 +3428,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-result" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-result" version = "0.4.1" @@ -2556,6 +3729,37 @@ dependencies = [ "zeroize", ] +[[package]] +name = "yamux" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed0164ae619f2dc144909a9f082187ebb5893693d8c0196e8085283ccd4b776" +dependencies = [ + "futures", + "log", + "nohash-hasher", + "parking_lot", + "pin-project", + "rand 0.8.5", + "static_assertions", +] + +[[package]] +name = "yamux" +version = "0.13.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c650efd29044140aa63caaf80129996a9e2659a2ab7045a7e061807d02fc8549" +dependencies = [ + "futures", + "log", + "nohash-hasher", + "parking_lot", + "pin-project", + "rand 0.9.2", + "static_assertions", + "web-time", +] + [[package]] name = "yoke" version = "0.8.1" diff --git a/nac-sdk/src/genesis_config.rs b/nac-sdk/src/genesis_config.rs new file mode 100644 index 0000000..a585754 --- /dev/null +++ b/nac-sdk/src/genesis_config.rs @@ -0,0 +1,144 @@ +//! NAC 公链创世配置 +//! +//! 定义 NAC 公链的核心参数,包括代币发行总量、区块参数等。 +//! +//! # NAC 代币经济模型 +//! - $NAC 原生代币发行总量:10 亿枚(1,000,000,000),固定总量,永不增发 +//! - XTZH 稳定币:锚定 SDR + 黄金储备,按需发行 +//! - XIC 治理代币:已在 BSC 发行(BEP-20),通过跨链桥与 NAC 主网互通 + +/// NAC 公链创世配置 +pub struct GenesisConfig { + /// 链 ID(NAC 主网:0x4E4143 = "NAC" ASCII) + pub chain_id: u64, + /// 链名称 + pub chain_name: &'static str, + /// 链符号 + pub chain_symbol: &'static str, +} + +/// NAC 原生代币配置 +pub struct NacTokenConfig { + /// 代币名称 + pub name: &'static str, + /// 代币符号 + pub symbol: &'static str, + /// 精度(小数位数) + pub decimals: u8, + /// 发行总量(固定,永不增发) + pub total_supply: u64, + /// 发行总量(带精度的完整表示,18位小数) + pub total_supply_wei: &'static str, +} + +/// XTZH 稳定币配置 +pub struct XtzhConfig { + /// 代币名称 + pub name: &'static str, + /// 代币符号 + pub symbol: &'static str, + /// 精度 + pub decimals: u8, + /// 锚定机制:SDR + 黄金储备 + pub peg_mechanism: &'static str, +} + +/// XIC 治理代币配置 +pub struct XicConfig { + /// 代币名称 + pub name: &'static str, + /// 代币符号 + pub symbol: &'static str, + /// BSC 合约地址(BEP-20) + pub bsc_contract: &'static str, + /// 跨链桥锚定机制:1:1 永久锚定 + pub bridge_mechanism: &'static str, +} + +/// NAC 主网创世配置(单例) +pub const MAINNET_GENESIS: GenesisConfig = GenesisConfig { + chain_id: 0x4E4143, // "NAC" in ASCII + chain_name: "New Asset Chain Mainnet", + chain_symbol: "NAC", +}; + +/// $NAC 原生代币配置 +/// 发行总量:10 亿枚,固定总量,永不增发 +pub const NAC_TOKEN: NacTokenConfig = NacTokenConfig { + name: "NAC Token", + symbol: "NAC", + decimals: 18, + total_supply: 1_000_000_000, // 10 亿枚,固定总量,永不增发 + total_supply_wei: "1000000000000000000000000000", // 1e27 (10亿 × 10^18) +}; + +/// XTZH 稳定币配置 +pub const XTZH_TOKEN: XtzhConfig = XtzhConfig { + name: "XTZH Stable Token", + symbol: "XTZH", + decimals: 18, + peg_mechanism: "SDR + Gold Reserve (黄金储备锚定 SDR 模型)", +}; + +/// XIC 治理代币配置 +pub const XIC_TOKEN: XicConfig = XicConfig { + name: "XIC Governance Token", + symbol: "XIC", + bsc_contract: "0x59ff34dd59680a7125782b1f6df2a86ed46f5a24", + bridge_mechanism: "BSC-XIC to NAC-XIC 1:1 永久锚定(5/7 多签验证)", +}; + +/// CBPP 区块参数 +pub struct CbppParams { + /// 空块最小大小(字节) + pub min_block_size_bytes: u64, + /// 空块最小大小(KB) + pub min_block_size_kb: u64, + /// 出块间隔(秒,每节点) + pub block_interval_per_node_secs: u64, + /// 共识协议 + pub consensus: &'static str, + /// 网络协议 + pub network: &'static str, + /// 虚拟机 + pub vm: &'static str, + /// 数据透镜协议 + pub lens: &'static str, +} + +/// CBPP 主网参数 +pub const CBPP_MAINNET: CbppParams = CbppParams { + min_block_size_bytes: 10240, // 10 KB + min_block_size_kb: 10, + block_interval_per_node_secs: 60, + consensus: "CBPP (Constitutional Block Production Protocol)", + network: "CSNP (Constitutional Secure Network Protocol)", + vm: "NVM (NAC Virtual Machine)", + lens: "NAC Lens (NAC Chain Data Lens)", +}; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_nac_token_total_supply() { + // $NAC 发行总量:10 亿枚,固定总量,永不增发 + assert_eq!(NAC_TOKEN.total_supply, 1_000_000_000); + assert_eq!(NAC_TOKEN.symbol, "NAC"); + assert_eq!(NAC_TOKEN.decimals, 18); + } + + #[test] + fn test_chain_id() { + // NAC 主网链 ID:0x4E4143("NAC" ASCII) + assert_eq!(MAINNET_GENESIS.chain_id, 0x4E4143); + } + + #[test] + fn test_cbpp_block_params() { + // 空块最小 10KB + assert_eq!(CBPP_MAINNET.min_block_size_kb, 10); + assert_eq!(CBPP_MAINNET.min_block_size_bytes, 10240); + } +} diff --git a/nac-sdk/src/lib.rs b/nac-sdk/src/lib.rs index d975cab..41f9010 100644 --- a/nac-sdk/src/lib.rs +++ b/nac-sdk/src/lib.rs @@ -66,3 +66,6 @@ mod tests { // 导出适配器模块 pub mod adapters; + +/// NAC 公链创世配置(代币发行、区块参数) +pub mod genesis_config; diff --git a/nac-udm/src/l1_protocol/layer_router.rs b/nac-udm/src/l1_protocol/layer_router.rs index 1ab48fc..911678b 100644 --- a/nac-udm/src/l1_protocol/layer_router.rs +++ b/nac-udm/src/l1_protocol/layer_router.rs @@ -437,8 +437,8 @@ mod tests { #[test] fn test_layer_message_creation() { - let sender = Address::from_slice(&[1u8; 20]).unwrap(); - let receiver = Address::from_slice(&[2u8; 20]).unwrap(); + let sender = Address::from_slice(&[1u8; 32]).unwrap(); + let receiver = Address::from_slice(&[2u8; 32]).unwrap(); let message = LayerMessage::new( LayerMessageType::Request, @@ -476,7 +476,7 @@ mod tests { #[test] fn test_message_queue() { let mut queue = LayerMessageQueue::new(); - let sender = Address::from_slice(&[1u8; 20]).unwrap(); + let sender = Address::from_slice(&[1u8; 32]).unwrap(); // 添加低优先级消息 let message1 = LayerMessage::new( @@ -512,8 +512,8 @@ mod tests { #[test] fn test_layer_router_send() { let mut router = LayerRouter::new(Layer::L1); - let sender = Address::from_slice(&[1u8; 20]).unwrap(); - let receiver = Address::from_slice(&[2u8; 20]).unwrap(); + let sender = Address::from_slice(&[1u8; 32]).unwrap(); + let receiver = Address::from_slice(&[2u8; 32]).unwrap(); let result = router.send_message( LayerMessageType::Request, @@ -533,8 +533,8 @@ mod tests { #[test] fn test_layer_router_receive() { let mut router = LayerRouter::new(Layer::L2); - let sender = Address::from_slice(&[1u8; 20]).unwrap(); - let receiver = Address::from_slice(&[2u8; 20]).unwrap(); + let sender = Address::from_slice(&[1u8; 32]).unwrap(); + let receiver = Address::from_slice(&[2u8; 32]).unwrap(); // 创建从L1发往L2的消息 let message = LayerMessage::new( @@ -557,8 +557,8 @@ mod tests { #[test] fn test_layer_router_forward() { let mut router = LayerRouter::new(Layer::L1); - let sender = Address::from_slice(&[1u8; 20]).unwrap(); - let receiver = Address::from_slice(&[2u8; 20]).unwrap(); + let sender = Address::from_slice(&[1u8; 32]).unwrap(); + let receiver = Address::from_slice(&[2u8; 32]).unwrap(); // 创建从L0发往L2的消息(需要经过L1转发) let message = LayerMessage::new( @@ -594,8 +594,8 @@ mod tests { #[test] fn test_process_messages() { let mut router = LayerRouter::new(Layer::L1); - let sender = Address::from_slice(&[1u8; 20]).unwrap(); - let receiver = Address::from_slice(&[2u8; 20]).unwrap(); + let sender = Address::from_slice(&[1u8; 32]).unwrap(); + let receiver = Address::from_slice(&[2u8; 32]).unwrap(); // 发送2条消息 router.send_message( diff --git a/nac-udm/src/l1_protocol/layer_router.rs.bak.20260227 b/nac-udm/src/l1_protocol/layer_router.rs.bak.20260227 new file mode 100644 index 0000000..1ab48fc --- /dev/null +++ b/nac-udm/src/l1_protocol/layer_router.rs.bak.20260227 @@ -0,0 +1,626 @@ +///! NAC层间路由器 +///! +///! 管理L0-L1-L2-L3层间的通信和路由 +///! +///! **NAC四层架构**: +///! - L0: 基础层(DAG + CBPP共识 + OPN) +///! - L1: 协议层(NVM + ACC + GNACS + 碎片化) +///! - L2: 宪法层(宪法收据 + CEE + 合规验证) +///! - L3: 应用层(业务应用 + UI + API) + +use crate::primitives::{Address, Hash}; +use std::collections::{HashMap, VecDeque}; +use serde::{Deserialize, Serialize}; + +/// NAC层级 +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)] +/// Layer +pub enum Layer { + /// L0: 基础层 + L0, + /// L1: 协议层 + L1, + /// L2: 宪法层 + L2, + /// L3: 应用层 + L3, +} + +impl Layer { + /// 获取层级编号 + pub fn level(&self) -> u8 { + match self { + Layer::L0 => 0, + Layer::L1 => 1, + Layer::L2 => 2, + Layer::L3 => 3, + } + } + + /// 从层级编号创建 + pub fn from_level(level: u8) -> Option { + match level { + 0 => Some(Layer::L0), + 1 => Some(Layer::L1), + 2 => Some(Layer::L2), + 3 => Some(Layer::L3), + _ => None, + } + } + + /// 是否可以直接通信 + pub fn can_communicate_with(&self, other: &Layer) -> bool { + // 相邻层可以直接通信 + let diff = (self.level() as i8 - other.level() as i8).abs(); + diff <= 1 + } +} + +/// 层间消息类型 +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] +/// LayerMessageType +pub enum LayerMessageType { + /// 请求 + Request, + /// 响应 + Response, + /// 通知 + Notification, + /// 事件 + Event, +} + +/// 层间消息 +#[derive(Debug, Clone, Serialize, Deserialize)] +/// LayerMessage +pub struct LayerMessage { + /// 消息ID + pub message_id: Hash, + /// 消息类型 + pub message_type: LayerMessageType, + /// 源层 + pub source_layer: Layer, + /// 目标层 + pub target_layer: Layer, + /// 发送者地址 + pub sender: Address, + /// 接收者地址(None表示广播) + pub receiver: Option
, + /// 消息负载 + pub payload: Vec, + /// 时间戳 + pub timestamp: u64, + /// 优先级(0-255,数字越大优先级越高) + pub priority: u8, + /// 路由路径(经过的层) + pub route_path: Vec, +} + +impl LayerMessage { + /// 创建新的层间消息 + pub fn new( + message_type: LayerMessageType, + source_layer: Layer, + target_layer: Layer, + sender: Address, + receiver: Option
, + payload: Vec, + timestamp: u64, + priority: u8, + ) -> Self { + // 计算消息ID + let mut data = Vec::new(); + data.push(source_layer.level()); + data.push(target_layer.level()); + data.extend_from_slice(sender.as_bytes()); + if let Some(ref r) = receiver { + data.extend_from_slice(r.as_bytes()); + } + data.extend_from_slice(&payload); + data.extend_from_slice(×tamp.to_le_bytes()); + + let message_id = Hash::sha3_384(&data); + + Self { + message_id, + message_type, + source_layer, + target_layer, + sender, + receiver, + payload, + timestamp, + priority, + route_path: vec![source_layer], + } + } + + /// 添加路由路径 + pub fn add_to_route(&mut self, layer: Layer) { + self.route_path.push(layer); + } + + /// 是否到达目标层 + pub fn has_reached_target(&self) -> bool { + self.route_path.last() == Some(&self.target_layer) + } +} + +/// 层间路由表 +struct RoutingTable { + /// 路由规则(source_layer, target_layer) => next_layer + routes: HashMap<(Layer, Layer), Layer>, +} + +impl RoutingTable { + /// 创建新的路由表 + fn new() -> Self { + let mut routes = HashMap::new(); + + // 初始化默认路由规则 + // L0 <-> L1 直接通信 + routes.insert((Layer::L0, Layer::L1), Layer::L1); + routes.insert((Layer::L1, Layer::L0), Layer::L0); + + // L1 <-> L2 直接通信 + routes.insert((Layer::L1, Layer::L2), Layer::L2); + routes.insert((Layer::L2, Layer::L1), Layer::L1); + + // L2 <-> L3 直接通信 + routes.insert((Layer::L2, Layer::L3), Layer::L3); + routes.insert((Layer::L3, Layer::L2), Layer::L2); + + // L0 -> L2 经过 L1 + routes.insert((Layer::L0, Layer::L2), Layer::L1); + + // L0 -> L3 经过 L1 + routes.insert((Layer::L0, Layer::L3), Layer::L1); + + // L1 -> L3 经过 L2 + routes.insert((Layer::L1, Layer::L3), Layer::L2); + + // L3 -> L1 经过 L2 + routes.insert((Layer::L3, Layer::L1), Layer::L2); + + // L3 -> L0 经过 L2 + routes.insert((Layer::L3, Layer::L0), Layer::L2); + + // L2 -> L0 经过 L1 + routes.insert((Layer::L2, Layer::L0), Layer::L1); + + Self { routes } + } + + /// 获取下一跳层级 + fn get_next_hop(&self, current_layer: Layer, target_layer: Layer) -> Option { + if current_layer == target_layer { + return Some(target_layer); + } + + self.routes.get(&(current_layer, target_layer)).copied() + } +} + +/// 层间消息队列 +struct LayerMessageQueue { + /// 消息队列(按优先级排序) + queue: VecDeque, +} + +impl LayerMessageQueue { + /// 创建新的消息队列 + fn new() -> Self { + Self { + queue: VecDeque::new(), + } + } + + /// 添加消息 + fn enqueue(&mut self, message: LayerMessage) { + // 按优先级插入(优先级高的在前) + let pos = self.queue + .iter() + .position(|m| m.priority < message.priority) + .unwrap_or(self.queue.len()); + + self.queue.insert(pos, message); + } + + /// 取出消息 + fn dequeue(&mut self) -> Option { + self.queue.pop_front() + } + + /// 获取队列长度 + fn len(&self) -> usize { + self.queue.len() + } + + /// 是否为空 + #[allow(dead_code)] + fn is_empty(&self) -> bool { + self.queue.is_empty() + } +} + +/// 层间路由器 +pub struct LayerRouter { + /// 当前层级 + current_layer: Layer, + /// 路由表 + routing_table: RoutingTable, + /// 待发送消息队列 + outbound_queue: LayerMessageQueue, + /// 已接收消息队列 + inbound_queue: LayerMessageQueue, + /// 消息处理器(message_type => handler_id) + message_handlers: HashMap>, + /// 统计信息 + stats: RouterStats, +} + +impl LayerRouter { + /// 创建新的层间路由器 + pub fn new(current_layer: Layer) -> Self { + Self { + current_layer, + routing_table: RoutingTable::new(), + outbound_queue: LayerMessageQueue::new(), + inbound_queue: LayerMessageQueue::new(), + message_handlers: HashMap::new(), + stats: RouterStats::default(), + } + } + + /// 发送消息 + pub fn send_message( + &mut self, + message_type: LayerMessageType, + target_layer: Layer, + sender: Address, + receiver: Option
, + payload: Vec, + timestamp: u64, + priority: u8, + ) -> Result { + // 创建消息 + let message = LayerMessage::new( + message_type, + self.current_layer, + target_layer, + sender, + receiver, + payload, + timestamp, + priority, + ); + + let message_id = message.message_id.clone(); + + // 加入发送队列 + self.outbound_queue.enqueue(message); + self.stats.messages_sent += 1; + + Ok(message_id) + } + + /// 接收消息 + pub fn receive_message(&mut self, mut message: LayerMessage) -> Result<(), String> { + // 验证消息是否应该到达当前层 + let next_hop = self.routing_table + .get_next_hop( + *message.route_path.last().unwrap(), + message.target_layer, + ) + .ok_or("No route found")?; + + if next_hop != self.current_layer { + return Err(format!( + "Message should be routed to {:?}, not {:?}", + next_hop, self.current_layer + )); + } + + // 添加到路由路径 + message.add_to_route(self.current_layer); + + // 如果到达目标层,加入接收队列 + if message.has_reached_target() { + self.inbound_queue.enqueue(message); + self.stats.messages_received += 1; + } else { + // 否则继续转发 + self.outbound_queue.enqueue(message); + self.stats.messages_forwarded += 1; + } + + Ok(()) + } + + /// 处理待发送消息 + pub fn process_outbound_messages(&mut self) -> Vec { + let mut processed = Vec::new(); + + while let Some(message) = self.outbound_queue.dequeue() { + processed.push(message); + } + + processed + } + + /// 处理已接收消息 + pub fn process_inbound_messages(&mut self) -> Vec { + let mut processed = Vec::new(); + + while let Some(message) = self.inbound_queue.dequeue() { + processed.push(message); + } + + processed + } + + /// 注册消息处理器 + pub fn register_handler(&mut self, message_type: LayerMessageType, handler_id: String) { + self.message_handlers + .entry(message_type) + .or_insert_with(Vec::new) + .push(handler_id); + } + + /// 获取消息处理器 + pub fn get_handlers(&self, message_type: &LayerMessageType) -> Option<&Vec> { + self.message_handlers.get(message_type) + } + + /// 获取统计信息 + pub fn get_stats(&self) -> &RouterStats { + &self.stats + } + + /// 获取待发送消息数量 + pub fn outbound_count(&self) -> usize { + self.outbound_queue.len() + } + + /// 获取已接收消息数量 + pub fn inbound_count(&self) -> usize { + self.inbound_queue.len() + } +} + +/// 路由器统计信息 +#[derive(Debug, Clone, Default)] +/// RouterStats +pub struct RouterStats { + /// 已发送消息数 + pub messages_sent: u64, + /// 已接收消息数 + pub messages_received: u64, + /// 已转发消息数 + pub messages_forwarded: u64, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_layer_level() { + assert_eq!(Layer::L0.level(), 0); + assert_eq!(Layer::L1.level(), 1); + assert_eq!(Layer::L2.level(), 2); + assert_eq!(Layer::L3.level(), 3); + } + + #[test] + fn test_layer_from_level() { + assert_eq!(Layer::from_level(0), Some(Layer::L0)); + assert_eq!(Layer::from_level(1), Some(Layer::L1)); + assert_eq!(Layer::from_level(2), Some(Layer::L2)); + assert_eq!(Layer::from_level(3), Some(Layer::L3)); + assert_eq!(Layer::from_level(4), None); + } + + #[test] + fn test_layer_communication() { + // 相邻层可以直接通信 + assert!(Layer::L0.can_communicate_with(&Layer::L1)); + assert!(Layer::L1.can_communicate_with(&Layer::L0)); + assert!(Layer::L1.can_communicate_with(&Layer::L2)); + assert!(Layer::L2.can_communicate_with(&Layer::L3)); + + // 非相邻层不能直接通信 + assert!(!Layer::L0.can_communicate_with(&Layer::L2)); + assert!(!Layer::L0.can_communicate_with(&Layer::L3)); + assert!(!Layer::L1.can_communicate_with(&Layer::L3)); + } + + #[test] + fn test_layer_message_creation() { + let sender = Address::from_slice(&[1u8; 20]).unwrap(); + let receiver = Address::from_slice(&[2u8; 20]).unwrap(); + + let message = LayerMessage::new( + LayerMessageType::Request, + Layer::L1, + Layer::L2, + sender, + Some(receiver), + vec![1, 2, 3], + 1000, + 10, + ); + + assert_eq!(message.source_layer, Layer::L1); + assert_eq!(message.target_layer, Layer::L2); + assert_eq!(message.priority, 10); + assert_eq!(message.route_path, vec![Layer::L1]); + assert!(!message.has_reached_target()); + } + + #[test] + fn test_routing_table() { + let table = RoutingTable::new(); + + // 直接通信 + assert_eq!(table.get_next_hop(Layer::L0, Layer::L1), Some(Layer::L1)); + assert_eq!(table.get_next_hop(Layer::L1, Layer::L2), Some(Layer::L2)); + + // 间接通信(L0 -> L2 经过 L1) + assert_eq!(table.get_next_hop(Layer::L0, Layer::L2), Some(Layer::L1)); + + // 间接通信(L0 -> L3 经过 L1) + assert_eq!(table.get_next_hop(Layer::L0, Layer::L3), Some(Layer::L1)); + } + + #[test] + fn test_message_queue() { + let mut queue = LayerMessageQueue::new(); + let sender = Address::from_slice(&[1u8; 20]).unwrap(); + + // 添加低优先级消息 + let message1 = LayerMessage::new( + LayerMessageType::Request, + Layer::L1, + Layer::L2, + sender.clone(), + None, + vec![1], + 1000, + 5, // 低优先级 + ); + queue.enqueue(message1); + + // 添加高优先级消息 + let message2 = LayerMessage::new( + LayerMessageType::Request, + Layer::L1, + Layer::L2, + sender, + None, + vec![2], + 1001, + 10, // 高优先级 + ); + queue.enqueue(message2.clone()); + + // 高优先级消息应该先出队 + let dequeued = queue.dequeue().unwrap(); + assert_eq!(dequeued.message_id, message2.message_id); + } + + #[test] + fn test_layer_router_send() { + let mut router = LayerRouter::new(Layer::L1); + let sender = Address::from_slice(&[1u8; 20]).unwrap(); + let receiver = Address::from_slice(&[2u8; 20]).unwrap(); + + let result = router.send_message( + LayerMessageType::Request, + Layer::L2, + sender, + Some(receiver), + vec![1, 2, 3], + 1000, + 10, + ); + + assert!(result.is_ok()); + assert_eq!(router.outbound_count(), 1); + assert_eq!(router.get_stats().messages_sent, 1); + } + + #[test] + fn test_layer_router_receive() { + let mut router = LayerRouter::new(Layer::L2); + let sender = Address::from_slice(&[1u8; 20]).unwrap(); + let receiver = Address::from_slice(&[2u8; 20]).unwrap(); + + // 创建从L1发往L2的消息 + let message = LayerMessage::new( + LayerMessageType::Request, + Layer::L1, + Layer::L2, + sender, + Some(receiver), + vec![1, 2, 3], + 1000, + 10, + ); + + let result = router.receive_message(message); + assert!(result.is_ok()); + assert_eq!(router.inbound_count(), 1); + assert_eq!(router.get_stats().messages_received, 1); + } + + #[test] + fn test_layer_router_forward() { + let mut router = LayerRouter::new(Layer::L1); + let sender = Address::from_slice(&[1u8; 20]).unwrap(); + let receiver = Address::from_slice(&[2u8; 20]).unwrap(); + + // 创建从L0发往L2的消息(需要经过L1转发) + let message = LayerMessage::new( + LayerMessageType::Request, + Layer::L0, + Layer::L2, + sender, + Some(receiver), + vec![1, 2, 3], + 1000, + 10, + ); + + let result = router.receive_message(message); + assert!(result.is_ok()); + assert_eq!(router.outbound_count(), 1); // 转发到发送队列 + assert_eq!(router.get_stats().messages_forwarded, 1); + } + + #[test] + fn test_message_handler_registration() { + let mut router = LayerRouter::new(Layer::L1); + + router.register_handler(LayerMessageType::Request, "handler1".to_string()); + router.register_handler(LayerMessageType::Request, "handler2".to_string()); + + let handlers = router.get_handlers(&LayerMessageType::Request).unwrap(); + assert_eq!(handlers.len(), 2); + assert!(handlers.contains(&"handler1".to_string())); + assert!(handlers.contains(&"handler2".to_string())); + } + + #[test] + fn test_process_messages() { + let mut router = LayerRouter::new(Layer::L1); + let sender = Address::from_slice(&[1u8; 20]).unwrap(); + let receiver = Address::from_slice(&[2u8; 20]).unwrap(); + + // 发送2条消息 + router.send_message( + LayerMessageType::Request, + Layer::L2, + sender.clone(), + Some(receiver.clone()), + vec![1], + 1000, + 10, + ).unwrap(); + + router.send_message( + LayerMessageType::Request, + Layer::L2, + sender, + Some(receiver), + vec![2], + 1001, + 5, + ).unwrap(); + + // 处理待发送消息 + let processed = router.process_outbound_messages(); + assert_eq!(processed.len(), 2); + assert_eq!(router.outbound_count(), 0); + } +} diff --git a/nac-udm/src/l1_protocol/state_shard_optimization.rs b/nac-udm/src/l1_protocol/state_shard_optimization.rs index 8159a6f..8674816 100644 --- a/nac-udm/src/l1_protocol/state_shard_optimization.rs +++ b/nac-udm/src/l1_protocol/state_shard_optimization.rs @@ -490,7 +490,7 @@ mod tests { #[test] fn test_optimizer_hot_account_tracking() { let mut optimizer = StateShardOptimizer::new(1, 3600, 1024 * 1024); - let address = Address::from_slice(&[1u8; 20]).unwrap(); + let address = Address::from_slice(&[1u8; 32]).unwrap(); // 记录大量活动 for i in 0..1001 { diff --git a/nac-udm/src/l1_protocol/state_shard_optimization.rs.bak.20260227 b/nac-udm/src/l1_protocol/state_shard_optimization.rs.bak.20260227 new file mode 100644 index 0000000..8159a6f --- /dev/null +++ b/nac-udm/src/l1_protocol/state_shard_optimization.rs.bak.20260227 @@ -0,0 +1,561 @@ +///! 状态分片优化模块 +///! +///! 优化分片状态存储和访问性能 +///! +///! **NAC原生设计原则**: +///! - 使用Asset(资产),不是Token +///! - 使用Certificate(证书),不是Contract +///! - 使用SHA3-384哈希,不是SHA256/Keccak256 +///! - 通过CBPP共识协调 + +use crate::primitives::{Address, Hash}; +use std::collections::HashMap; +use serde::{Deserialize, Serialize}; + +/// 状态分片优化器 +#[derive(Debug, Clone)] +/// StateShardOptimizer +pub struct StateShardOptimizer { + /// 分片ID + shard_id: u64, + /// 状态缓存 + state_cache: HashMap, + /// 热点账户追踪 + hot_accounts: HashMap, + /// 冷数据归档阈值(秒) + cold_threshold: u64, + /// 缓存大小限制(字节) + max_cache_size: u64, + /// 当前缓存大小 + current_cache_size: u64, + /// 优化统计 + stats: OptimizationStats, +} + +/// 状态条目 +#[derive(Debug, Clone, Serialize, Deserialize)] +/// StateEntry +pub struct StateEntry { + /// 状态键 + pub key: Hash, + /// 状态值 + pub value: Vec, + /// 最后访问时间 + pub last_access: u64, + /// 访问次数 + pub access_count: u64, + /// 是否为热点数据 + pub is_hot: bool, + /// 数据大小(字节) + pub size: u64, +} + +impl StateEntry { + /// 创建新的状态条目 + pub fn new(key: Hash, value: Vec, timestamp: u64) -> Self { + let size = value.len() as u64; + Self { + key, + value, + last_access: timestamp, + access_count: 1, + is_hot: false, + size, + } + } + + /// 更新访问信息 + pub fn update_access(&mut self, timestamp: u64) { + self.last_access = timestamp; + self.access_count += 1; + + // 访问次数超过阈值标记为热点 + if self.access_count > 100 { + self.is_hot = true; + } + } + + /// 检查是否为冷数据 + pub fn is_cold(&self, current_time: u64, threshold: u64) -> bool { + current_time - self.last_access > threshold + } +} + +/// 热点账户统计 +#[derive(Debug, Clone, Serialize, Deserialize)] +/// HotAccountStats +pub struct HotAccountStats { + /// 账户地址 + pub address: Address, + /// 读取次数 + pub read_count: u64, + /// 写入次数 + pub write_count: u64, + /// 最后活动时间 + pub last_activity: u64, + /// 平均交易大小 + pub avg_tx_size: u64, + /// 是否为高频账户 + pub is_high_frequency: bool, +} + +impl HotAccountStats { + /// 创建新的热点账户统计 + pub fn new(address: Address, timestamp: u64) -> Self { + Self { + address, + read_count: 0, + write_count: 0, + last_activity: timestamp, + avg_tx_size: 0, + is_high_frequency: false, + } + } + + /// 记录读取操作 + pub fn record_read(&mut self, timestamp: u64) { + self.read_count += 1; + self.last_activity = timestamp; + self.update_frequency(); + } + + /// 记录写入操作 + pub fn record_write(&mut self, timestamp: u64, tx_size: u64) { + self.write_count += 1; + self.last_activity = timestamp; + + // 更新平均交易大小 + let total_count = self.read_count + self.write_count; + self.avg_tx_size = (self.avg_tx_size * (total_count - 1) + tx_size) / total_count; + + self.update_frequency(); + } + + /// 更新频率标记 + fn update_frequency(&mut self) { + // 读写总次数超过1000标记为高频 + if self.read_count + self.write_count > 1000 { + self.is_high_frequency = true; + } + } +} + +/// 优化统计 +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +/// OptimizationStats +pub struct OptimizationStats { + /// 缓存命中次数 + pub cache_hits: u64, + /// 缓存未命中次数 + pub cache_misses: u64, + /// 冷数据归档次数 + pub cold_data_archived: u64, + /// 热点数据提升次数 + pub hot_data_promoted: u64, + /// 缓存驱逐次数 + pub cache_evictions: u64, + /// 总状态访问次数 + pub total_accesses: u64, +} + +impl OptimizationStats { + /// 计算缓存命中率 + pub fn cache_hit_rate(&self) -> f64 { + if self.total_accesses == 0 { + return 0.0; + } + self.cache_hits as f64 / self.total_accesses as f64 + } +} + +impl StateShardOptimizer { + /// 创建新的状态分片优化器 + pub fn new(shard_id: u64, cold_threshold: u64, max_cache_size: u64) -> Self { + Self { + shard_id, + state_cache: HashMap::new(), + hot_accounts: HashMap::new(), + cold_threshold, + max_cache_size, + current_cache_size: 0, + stats: OptimizationStats::default(), + } + } + + /// 读取状态 + pub fn read_state(&mut self, key: &Hash, current_time: u64) -> Option> { + self.stats.total_accesses += 1; + + if let Some(entry) = self.state_cache.get_mut(key) { + // 缓存命中 + self.stats.cache_hits += 1; + entry.update_access(current_time); + Some(entry.value.clone()) + } else { + // 缓存未命中 + self.stats.cache_misses += 1; + None + } + } + + /// 写入状态 + pub fn write_state(&mut self, key: Hash, value: Vec, current_time: u64) -> Result<(), String> { + let entry_size = value.len() as u64; + + // 检查缓存大小限制 + if self.current_cache_size + entry_size > self.max_cache_size { + // 驱逐冷数据 + self.evict_cold_data(current_time)?; + } + + // 创建或更新状态条目 + if let Some(existing) = self.state_cache.get_mut(&key) { + self.current_cache_size -= existing.size; + existing.value = value; + existing.size = entry_size; + existing.update_access(current_time); + self.current_cache_size += entry_size; + } else { + let entry = StateEntry::new(key.clone(), value, current_time); + self.state_cache.insert(key, entry); + self.current_cache_size += entry_size; + } + + Ok(()) + } + + /// 驱逐冷数据 + pub fn evict_cold_data(&mut self, current_time: u64) -> Result<(), String> { + let mut cold_keys = Vec::new(); + + // 查找冷数据 + for (key, entry) in &self.state_cache { + if entry.is_cold(current_time, self.cold_threshold) && !entry.is_hot { + cold_keys.push(key.clone()); + } + } + + if cold_keys.is_empty() { + return Err("No cold data to evict".to_string()); + } + + // 驱逐冷数据 + for key in cold_keys { + if let Some(entry) = self.state_cache.remove(&key) { + self.current_cache_size -= entry.size; + self.stats.cache_evictions += 1; + self.stats.cold_data_archived += 1; + } + } + + Ok(()) + } + + /// 提升热点数据 + pub fn promote_hot_data(&mut self, key: &Hash) -> Result<(), String> { + if let Some(entry) = self.state_cache.get_mut(key) { + if !entry.is_hot { + entry.is_hot = true; + self.stats.hot_data_promoted += 1; + } + Ok(()) + } else { + Err("State entry not found".to_string()) + } + } + + /// 记录账户活动 + pub fn record_account_activity( + &mut self, + address: Address, + is_write: bool, + tx_size: u64, + timestamp: u64, + ) { + let stats = self.hot_accounts + .entry(address.clone()) + .or_insert_with(|| HotAccountStats::new(address, timestamp)); + + if is_write { + stats.record_write(timestamp, tx_size); + } else { + stats.record_read(timestamp); + } + } + + /// 获取热点账户列表 + pub fn get_hot_accounts(&self) -> Vec
{ + self.hot_accounts + .values() + .filter(|stats| stats.is_high_frequency) + .map(|stats| stats.address.clone()) + .collect() + } + + /// 获取优化统计 + pub fn get_stats(&self) -> OptimizationStats { + self.stats.clone() + } + + /// 获取缓存使用率 + pub fn get_cache_usage(&self) -> f64 { + if self.max_cache_size == 0 { + return 0.0; + } + self.current_cache_size as f64 / self.max_cache_size as f64 + } + + /// 清理过期数据 + pub fn cleanup_expired(&mut self, current_time: u64) -> usize { + let mut expired_keys = Vec::new(); + + // 查找过期数据(超过冷数据阈值的2倍) + let expiry_threshold = self.cold_threshold * 2; + for (key, entry) in &self.state_cache { + if current_time - entry.last_access > expiry_threshold { + expired_keys.push(key.clone()); + } + } + + let count = expired_keys.len(); + + // 删除过期数据 + for key in expired_keys { + if let Some(entry) = self.state_cache.remove(&key) { + self.current_cache_size -= entry.size; + } + } + + count + } + + /// 优化分片状态 + pub fn optimize(&mut self, current_time: u64) -> Result { + let initial_cache_size = self.current_cache_size; + let initial_entry_count = self.state_cache.len(); + + // 1. 清理过期数据 + let expired_count = self.cleanup_expired(current_time); + + // 2. 驱逐冷数据(如果缓存使用率超过80%) + let evicted_count = if self.get_cache_usage() > 0.8 { + let before = self.state_cache.len(); + self.evict_cold_data(current_time)?; + before - self.state_cache.len() + } else { + 0 + }; + + // 3. 提升热点数据 + let hot_keys: Vec = self.state_cache + .iter() + .filter(|(_, entry)| entry.access_count > 50 && !entry.is_hot) + .map(|(key, _)| key.clone()) + .collect(); + + for key in &hot_keys { + let _ = self.promote_hot_data(key); + } + + Ok(OptimizationReport { + shard_id: self.shard_id, + expired_count, + evicted_count, + promoted_count: hot_keys.len(), + space_freed: initial_cache_size - self.current_cache_size, + entries_before: initial_entry_count, + entries_after: self.state_cache.len(), + cache_hit_rate: self.stats.cache_hit_rate(), + }) + } +} + +/// 优化报告 +#[derive(Debug, Clone, Serialize, Deserialize)] +/// OptimizationReport +pub struct OptimizationReport { + /// 分片ID + pub shard_id: u64, + /// 清理的过期数据数量 + pub expired_count: usize, + /// 驱逐的冷数据数量 + pub evicted_count: usize, + /// 提升的热点数据数量 + pub promoted_count: usize, + /// 释放的空间(字节) + pub space_freed: u64, + /// 优化前条目数 + pub entries_before: usize, + /// 优化后条目数 + pub entries_after: usize, + /// 缓存命中率 + pub cache_hit_rate: f64, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_state_entry_creation() { + let key = Hash::from_slice(&[1u8; 32]).unwrap(); + let value = vec![1, 2, 3, 4]; + let entry = StateEntry::new(key, value.clone(), 1000); + + assert_eq!(entry.value, value); + assert_eq!(entry.last_access, 1000); + assert_eq!(entry.access_count, 1); + assert!(!entry.is_hot); + assert_eq!(entry.size, 4); + } + + #[test] + fn test_state_entry_hot_promotion() { + let key = Hash::from_slice(&[1u8; 32]).unwrap(); + let value = vec![1, 2, 3]; + let mut entry = StateEntry::new(key, value, 1000); + + // 访问101次应该变为热点 + for i in 0..101 { + entry.update_access(1000 + i); + } + + assert!(entry.is_hot); + assert_eq!(entry.access_count, 102); // 初始1次 + 101次 + } + + #[test] + fn test_state_entry_cold_detection() { + let key = Hash::from_slice(&[1u8; 32]).unwrap(); + let value = vec![1, 2, 3]; + let entry = StateEntry::new(key, value, 1000); + + // 在阈值内不是冷数据 + assert!(!entry.is_cold(1500, 1000)); + + // 超过阈值是冷数据 + assert!(entry.is_cold(2001, 1000)); + } + + #[test] + fn test_optimizer_read_write() { + let mut optimizer = StateShardOptimizer::new(1, 3600, 1024 * 1024); + let key = Hash::from_slice(&[1u8; 32]).unwrap(); + let value = vec![1, 2, 3, 4]; + + // 写入状态 + assert!(optimizer.write_state(key.clone(), value.clone(), 1000).is_ok()); + + // 读取状态 + let read_value = optimizer.read_state(&key, 1001); + assert_eq!(read_value, Some(value)); + + // 验证统计 + assert_eq!(optimizer.stats.cache_hits, 1); + assert_eq!(optimizer.stats.total_accesses, 1); + } + + #[test] + fn test_optimizer_cache_miss() { + let mut optimizer = StateShardOptimizer::new(1, 3600, 1024 * 1024); + let key = Hash::from_slice(&[1u8; 32]).unwrap(); + + // 读取不存在的状态 + let result = optimizer.read_state(&key, 1000); + assert!(result.is_none()); + + // 验证统计 + assert_eq!(optimizer.stats.cache_misses, 1); + assert_eq!(optimizer.stats.total_accesses, 1); + } + + #[test] + fn test_optimizer_evict_cold_data() { + let mut optimizer = StateShardOptimizer::new(1, 1000, 1024); + + // 写入一些数据 + for i in 0..5 { + let key = Hash::from_slice(&[i; 32]).unwrap(); + let value = vec![i; 100]; + optimizer.write_state(key, value, 1000 + i as u64).unwrap(); + } + + // 等待一段时间后驱逐冷数据 + assert!(optimizer.evict_cold_data(3000).is_ok()); + + // 验证统计 + assert!(optimizer.stats.cold_data_archived > 0); + } + + #[test] + fn test_optimizer_hot_account_tracking() { + let mut optimizer = StateShardOptimizer::new(1, 3600, 1024 * 1024); + let address = Address::from_slice(&[1u8; 20]).unwrap(); + + // 记录大量活动 + for i in 0..1001 { + optimizer.record_account_activity(address.clone(), i % 2 == 0, 100, 1000 + i); + } + + // 验证热点账户 + let hot_accounts = optimizer.get_hot_accounts(); + assert_eq!(hot_accounts.len(), 1); + assert_eq!(hot_accounts[0], address); + } + + #[test] + fn test_optimizer_cache_usage() { + let mut optimizer = StateShardOptimizer::new(1, 3600, 1000); + + // 写入500字节数据 + let key = Hash::from_slice(&[1u8; 32]).unwrap(); + let value = vec![1u8; 500]; + optimizer.write_state(key, value, 1000).unwrap(); + + // 验证缓存使用率 + assert_eq!(optimizer.get_cache_usage(), 0.5); + } + + #[test] + fn test_optimizer_cleanup_expired() { + let mut optimizer = StateShardOptimizer::new(1, 1000, 1024 * 1024); + + // 写入一些数据 + for i in 0..5 { + let key = Hash::from_slice(&[i; 32]).unwrap(); + let value = vec![i; 10]; + optimizer.write_state(key, value, 1000).unwrap(); + } + + // 清理过期数据(超过2000秒) + let expired = optimizer.cleanup_expired(4000); + assert_eq!(expired, 5); + } + + #[test] + fn test_optimizer_full_optimization() { + let mut optimizer = StateShardOptimizer::new(1, 1000, 1024 * 1024); + + // 写入一些数据 + for i in 0..10 { + let key = Hash::from_slice(&[i; 32]).unwrap(); + let value = vec![i; 100]; + optimizer.write_state(key, value, 1000 + i as u64).unwrap(); + } + + // 访问部分数据使其成为热点(但不超过100次,避免自动标记为hot) + for i in 0..3 { + let key = Hash::from_slice(&[i; 32]).unwrap(); + for _ in 0..50 { + optimizer.read_state(&key, 2000); + } + } + + // 执行优化 + let report = optimizer.optimize(4000).unwrap(); + + assert_eq!(report.shard_id, 1); + assert!(report.promoted_count > 0); + assert!(report.cache_hit_rate > 0.0); + } +}