diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 538f4f6..cc9ccc7 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -19,7 +19,7 @@ jobs: uses: actions/checkout@v4 - name: Install Rust Toolchain run: | - rustup toolchain install ${{ env.RUST_TOOLCHAIN }} --profile minimal --component rustfmt + rustup toolchain install ${{ env.RUST_TOOLCHAIN }} --no-self-update --profile minimal --component rustfmt rustup override set ${{ env.RUST_TOOLCHAIN }} - name: Format Check run: make fmt @@ -31,7 +31,7 @@ jobs: uses: actions/checkout@v4 - name: Install Rust Toolchain run: | - rustup toolchain install ${{ env.RUST_TOOLCHAIN }} --profile minimal --component clippy + rustup toolchain install ${{ env.RUST_TOOLCHAIN }} --no-self-update --profile minimal --component clippy rustup override set ${{ env.RUST_TOOLCHAIN }} - name: Lint Check run: make clippy @@ -51,7 +51,7 @@ jobs: submodules: recursive - name: Install Rust Toolchain run: | - rustup toolchain install ${{ env.RUST_TOOLCHAIN }} --profile minimal + rustup toolchain install ${{ env.RUST_TOOLCHAIN }} --no-self-update --profile minimal rustup override set ${{ env.RUST_TOOLCHAIN }} - name: Install cargo-nextest uses: taiki-e/install-action@nextest diff --git a/Cargo.lock b/Cargo.lock index 3e47157..4665593 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "anyhow" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" + [[package]] name = "arrayvec" version = "0.7.4" @@ -71,6 +77,12 @@ version = "0.10.0-beta" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98f7eed2b2781a6f0b5c903471d48e15f56fb4e1165df8a9a2337fd1a59d45ea" +[[package]] +name = "bit-vec" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" + [[package]] name = "bitcoin" version = "0.31.1" @@ -120,6 +132,22 @@ dependencies = [ "wyz", ] +[[package]] +name = "blake2b-ref" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "294d17c72e0ba59fad763caa112368d0672083779cdebbb97164f4bb4c1e339a" + +[[package]] +name = "blake2b-rs" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89a8565807f21b913288968e391819e7f9b2f0f46c7b89549c051cccf3a2771" +dependencies = [ + "cc", + "cty", +] + [[package]] name = "byte-slice-cast" version = "1.2.2" @@ -137,12 +165,15 @@ name = "bytes" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" +dependencies = [ + "serde", +] [[package]] name = "cc" -version = "1.0.88" +version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02f341c093d19155a6e41631ce5971aac4e9a868262212153124c15fa22d1cdc" +checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b" [[package]] name = "cfg-if" @@ -168,14 +199,125 @@ version = "0.1.0" dependencies = [ "bitcoin", "bitcoin_hashes", - "ckb-merkle-mountain-range", + "ckb-jsonrpc-types", + "ckb-merkle-mountain-range 0.6.0", + "ckb-types", "env_logger", "log", "molecule", "primitive-types", + "serde_json", "walkdir", ] +[[package]] +name = "ckb-channel" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "307485ec657db250f98f7654599058de359be9f6188b8d6a29edc52c156bcb5a" +dependencies = [ + "crossbeam-channel", +] + +[[package]] +name = "ckb-constant" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1489b30f003277fff4b2f89670e1ebd10d8a4284a5e13c45a2464a00f58ff1bd" + +[[package]] +name = "ckb-error" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59f5fea5d6ad1f7c347d145272b283f35c6256e931f45144a41c2e1e764eec32" +dependencies = [ + "anyhow", + "ckb-occupied-capacity", + "derive_more", + "thiserror", +] + +[[package]] +name = "ckb-fixed-hash" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e1d1561f0a909985a835f1ffab69d1f0b55a0321fe9e75c3268251365b60ad6" +dependencies = [ + "ckb-fixed-hash-core", + "ckb-fixed-hash-macros", +] + +[[package]] +name = "ckb-fixed-hash-core" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65b1e88dc63f4fd64f3c3581724938d2a992c0038f59cc2434db53951178fe90" +dependencies = [ + "ckb_schemars", + "faster-hex", + "serde", + "thiserror", +] + +[[package]] +name = "ckb-fixed-hash-macros" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960fa8310e61bccf99c7c4899d1d1df99fc3350859191d755b8a63261f9a0d23" +dependencies = [ + "ckb-fixed-hash-core", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ckb-gen-types" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cac74cd083a771306a29500fa7a4fef91e888694723be7982f036e589430c99" +dependencies = [ + "cfg-if", + "ckb-error", + "ckb-fixed-hash", + "ckb-hash", + "ckb-occupied-capacity", + "molecule", + "numext-fixed-uint", +] + +[[package]] +name = "ckb-hash" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37a80b274a236255963e7a9fe73eea35623a46de7f2d8548278f024b5eb0a7c7" +dependencies = [ + "blake2b-ref", + "blake2b-rs", +] + +[[package]] +name = "ckb-jsonrpc-types" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdef235fa01419cbf54333941e02ad9a1755659e1acd79c15764649320eb0aa7" +dependencies = [ + "ckb-types", + "ckb_schemars", + "faster-hex", + "serde", + "serde_json", +] + +[[package]] +name = "ckb-merkle-mountain-range" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56ccb671c5921be8a84686e6212ca184cb1d7c51cadcdbfcbd1cc3f042f5dfb8" +dependencies = [ + "cfg-if", +] + [[package]] name = "ckb-merkle-mountain-range" version = "0.6.0" @@ -185,6 +327,96 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "ckb-occupied-capacity" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4034365568826f4bb2483b39f1e286d3e842c23eaab0c0b4145d487337b34911" +dependencies = [ + "ckb-occupied-capacity-core", + "ckb-occupied-capacity-macros", +] + +[[package]] +name = "ckb-occupied-capacity-core" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953d774dd942d7e5c0b631e8d7ebf798fcfbba2e692b51ab7f96a0cda0071bb7" +dependencies = [ + "serde", +] + +[[package]] +name = "ckb-occupied-capacity-macros" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcd747b05e99438fc607041b969244e94c0c741a41084cc55561dca869b4cdbb" +dependencies = [ + "ckb-occupied-capacity-core", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ckb-rational" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e507ad0c40d59c08eba650ab5dc19303951a43d1ff47e882ed3f3aa33f60c7c2" +dependencies = [ + "numext-fixed-uint", + "serde", +] + +[[package]] +name = "ckb-types" +version = "0.115.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "779edcd07b81c098da005620c8384682fa35f9903ab05861b4048fbfb5b412a2" +dependencies = [ + "bit-vec", + "bytes", + "ckb-channel", + "ckb-constant", + "ckb-error", + "ckb-fixed-hash", + "ckb-gen-types", + "ckb-hash", + "ckb-merkle-mountain-range 0.5.2", + "ckb-occupied-capacity", + "ckb-rational", + "derive_more", + "golomb-coded-set", + "merkle-cbt", + "molecule", + "numext-fixed-uint", + "once_cell", + "paste", +] + +[[package]] +name = "ckb_schemars" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f21f99fca82a4eb8708e406e99246987b087ecc1e1babeece1a0b1d5238b1750" +dependencies = [ + "ckb_schemars_derive", + "dyn-clone", + "serde", + "serde_json", +] + +[[package]] +name = "ckb_schemars_derive" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c813b4fadbdd9f33b1cf02a1ddfa9537d955c8d2fbe150d1fc1684dbf78e73" +dependencies = [ + "proc-macro2", + "quote", + "serde_derive_internals", + "syn 1.0.109", +] + [[package]] name = "colorchoice" version = "1.0.0" @@ -200,12 +432,50 @@ dependencies = [ "memchr", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab3db02a9c5b5121e1e42fbdb1aeb65f5e02624cc58c43f2884c6ccac0b82f95" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" + [[package]] name = "crunchy" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" +[[package]] +name = "cty" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" + +[[package]] +name = "derive_more" +version = "0.99.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "dyn-clone" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" + [[package]] name = "env_filter" version = "0.1.0" @@ -248,7 +518,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534" dependencies = [ "byteorder", - "rand", + "rand 0.8.5", "rustc-hex", "static_assertions", ] @@ -259,6 +529,17 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.9.0+wasi-snapshot-preview1", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -267,7 +548,16 @@ checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", +] + +[[package]] +name = "golomb-coded-set" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812f314a99fb5b7f0f9d0a8388539578f83f3aca6a65f588b8dbeefb731e2f98" +dependencies = [ + "siphasher", ] [[package]] @@ -276,6 +566,15 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +[[package]] +name = "heapsize" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1679e6ea370dee694f91f1dc469bf94cf8f52051d147aec3e1f9497c6fc22461" +dependencies = [ + "winapi", +] + [[package]] name = "hex" version = "0.4.3" @@ -333,6 +632,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + [[package]] name = "libc" version = "0.2.153" @@ -351,6 +656,15 @@ version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +[[package]] +name = "merkle-cbt" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "171d2f700835121c3b04ccf0880882987a050fd5c7ae88148abf537d33dd3a56" +dependencies = [ + "cfg-if", +] + [[package]] name = "molecule" version = "0.7.5" @@ -362,6 +676,58 @@ dependencies = [ "faster-hex", ] +[[package]] +name = "numext-constructor" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "621fe0f044729f810c6815cdd77e8f5e0cd803ce4f6a38380ebfc1322af98661" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "numext-fixed-uint" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c68c76f96d589d1009a666c5072f37f3114d682696505f2cf445f27766c7d70" +dependencies = [ + "numext-fixed-uint-core", + "numext-fixed-uint-hack", +] + +[[package]] +name = "numext-fixed-uint-core" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6aab1d6457b97b49482f22a92f0f58a2f39bdd7f3b2f977eae67e8bc206aa980" +dependencies = [ + "heapsize", + "numext-constructor", + "rand 0.7.3", + "serde", + "thiserror", +] + +[[package]] +name = "numext-fixed-uint-hack" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0200f8d55c36ec1b6a8cf810115be85d4814f045e0097dfd50033ba25adb4c9e" +dependencies = [ + "numext-fixed-uint-core", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + [[package]] name = "parity-scale-codec" version = "3.6.9" @@ -388,6 +754,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "paste" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -439,6 +811,19 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom 0.1.16", + "libc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc", +] + [[package]] name = "rand" version = "0.8.5" @@ -446,8 +831,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", - "rand_chacha", - "rand_core", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core 0.5.1", ] [[package]] @@ -457,7 +852,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom 0.1.16", ] [[package]] @@ -466,7 +870,16 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ - "getrandom", + "getrandom 0.2.12", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core 0.5.1", ] [[package]] @@ -504,6 +917,12 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" +[[package]] +name = "ryu" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" + [[package]] name = "same-file" version = "1.0.6" @@ -553,6 +972,34 @@ dependencies = [ "syn 2.0.51", ] +[[package]] +name = "serde_derive_internals" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85bf8229e7920a9f636479437026331ce11aa132b4dde37d121944a44d6e5f3c" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "serde_json" +version = "1.0.116" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + [[package]] name = "static_assertions" version = "1.1.0" @@ -658,6 +1105,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/tests/data b/tests/data index 83704f0..5fe47d2 160000 --- a/tests/data +++ b/tests/data @@ -1 +1 @@ -Subproject commit 83704f0df0fce17ed8d65bac98514524d654f88a +Subproject commit 5fe47d2fa6296de64e2a358ea814755aa62741c8 diff --git a/verifier/Cargo.toml b/verifier/Cargo.toml index f4db923..595e754 100644 --- a/verifier/Cargo.toml +++ b/verifier/Cargo.toml @@ -17,7 +17,10 @@ ckb-mmr = { version = "0.6", default-features = false, package = "ckb-merkle-mou log = { version = "0.4", optional = true } [dev-dependencies] +ckb-jsonrpc-types = "0.115.0" +ckb-types = "0.115.0" env_logger = "0.11" +serde_json = "1.0" walkdir = "2.4" [features] diff --git a/verifier/src/tests/mod.rs b/verifier/src/tests/mod.rs index 9fa854a..388e1a4 100644 --- a/verifier/src/tests/mod.rs +++ b/verifier/src/tests/mod.rs @@ -6,6 +6,7 @@ use log::LevelFilter; mod bitcoin; pub(crate) mod data; +pub(crate) mod testnet; pub(crate) mod utilities; pub(crate) fn setup() { diff --git a/verifier/src/tests/testnet.rs b/verifier/src/tests/testnet.rs new file mode 100644 index 0000000..b77d97e --- /dev/null +++ b/verifier/src/tests/testnet.rs @@ -0,0 +1,72 @@ +use std::fs::read_to_string; + +use alloc::format; +use ckb_jsonrpc_types::TransactionView; +use ckb_types::packed::WitnessArgs; +use serde_json::from_str as from_json_str; + +use crate::{ + molecule::prelude::*, + tests, + types::{ + core, + packed::{SpvClient, SpvUpdate}, + prelude::*, + }, +}; + +// This case shows that: +// - For the main network, `header.bits` should be the same as `new_info.1`. +// - But for the test network, this may not be the case. +// To run this test, use the following command: +// `cargo test --package ckb-bitcoin-spv-verifier --lib -- tests::testnet::testnet_verify_new_client_error --exact --show-output` +// Upon running the test, you should expect to see an ERROR output in the log similar to the following: +// [2024-04-30T10:43:45Z ERROR ckb_bitcoin_spv_verifier::types::extension::packed] failed: invalid difficulty for header-2588542, expect 422451157 but got 486604799 +#[test] +fn testnet_verify_new_client_error() { + verify_new_client_common( + "tx-0422-error-check-header-target-adjust-info.json", + 1, // cell_dep_index + ); +} + +#[test] +fn testnet_tx_verify_new_client_normal() { + verify_new_client_common( + "tx-0xb5b4a8f31b330d0686fc589b73e8c9c98365a8010bec4719d157671a8c2d7be1.json", + 2, // cell_dep_index + ); +} + +fn verify_new_client_common(tx_file: &str, cell_dep_index: usize) { + tests::setup(); + + let path = tests::data::find_bin_file("testnet", tx_file); + let tx = read_to_string(path).unwrap(); + let tx: TransactionView = from_json_str(&tx).unwrap(); + + let witnesses = tx.inner.witnesses; + let witness_args = WitnessArgs::from_slice(witnesses[0].as_bytes()).unwrap(); + let spv_update_bin = witness_args.output_type().to_opt().unwrap().raw_data(); + let spv_update = SpvUpdate::from_slice(&spv_update_bin).unwrap(); + + let client_bin = tx.inner.outputs_data[1].clone(); + let client = SpvClient::from_slice(client_bin.as_bytes()).unwrap(); + + let cell_dep = tx.inner.cell_deps[cell_dep_index].out_point.clone(); + let path = tests::data::find_bin_file( + "testnet", + format!("tx-0x{}.json", cell_dep.tx_hash).as_str(), + ); + let previous_tx = read_to_string(path).unwrap(); + let previous_tx: TransactionView = from_json_str(&previous_tx).unwrap(); + let cell_dep_data_bin = &previous_tx.inner.outputs_data[cell_dep.index.value() as usize]; + let cell_dep_client = SpvClient::from_slice(cell_dep_data_bin.as_bytes()).unwrap(); + + let mut cell_dep_client: core::SpvClient = cell_dep_client.unpack(); + cell_dep_client.id = client.id().into(); + let input_client = cell_dep_client.pack(); + let ret = input_client.verify_new_client(&client, spv_update, 128); + + assert!(ret.is_ok()); +}