From 7039ee9cb30bfad5b53dfb86afff612581ba3153 Mon Sep 17 00:00:00 2001 From: joii Date: Mon, 15 Jul 2024 11:35:34 +0800 Subject: [PATCH] Support Debug contract with native --- Cargo.lock | 12 + crates/tests/Cargo.toml | 3 + crates/tests/src/testtool.rs | 8 +- crates/tests/test-contract/Cargo.lock | 560 +++++++++++++++ .../contracts/test-contract/Cargo.toml | 3 + .../contracts/test-contract/src/lib.rs | 6 + .../test-contract-dbg/.gitignore | 1 + .../test-contract-dbg/Cargo.lock | 679 ++++++++++++++++++ .../test-contract-dbg/Cargo.toml | 18 + .../test-contract-dbg/src/lib.rs | 11 + crates/testtool/Cargo.toml | 5 + crates/testtool/src/context.rs | 125 +++- 12 files changed, 1428 insertions(+), 3 deletions(-) create mode 100644 crates/tests/test-contract/contracts/test-contract/src/lib.rs create mode 100644 crates/tests/test-contract/contracts/test-contract/test-contract-dbg/.gitignore create mode 100644 crates/tests/test-contract/contracts/test-contract/test-contract-dbg/Cargo.lock create mode 100644 crates/tests/test-contract/contracts/test-contract/test-contract-dbg/Cargo.toml create mode 100644 crates/tests/test-contract/contracts/test-contract/test-contract-dbg/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 90bf829..add57a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -654,7 +654,9 @@ dependencies = [ "ckb-types", "ckb-verification", "lazy_static", + "libloading", "rand 0.8.5", + "serde_json", ] [[package]] @@ -1539,6 +1541,16 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "libloading" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e310b3a6b5907f99202fcdb4960ff45b93735d7c7d96b760fcff8db2dc0e103d" +dependencies = [ + "cfg-if 1.0.0", + "windows-targets 0.52.4", +] + [[package]] name = "libm" version = "0.2.8" diff --git a/crates/tests/Cargo.toml b/crates/tests/Cargo.toml index 0975d8a..cb2fc15 100644 --- a/crates/tests/Cargo.toml +++ b/crates/tests/Cargo.toml @@ -6,6 +6,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +simulator = [ "ckb-testtool/simulator" ] + [dependencies] anyhow = "1.0" ckb-testtool = { path = "../testtool" } diff --git a/crates/tests/src/testtool.rs b/crates/tests/src/testtool.rs index d82970b..8591b9f 100644 --- a/crates/tests/src/testtool.rs +++ b/crates/tests/src/testtool.rs @@ -176,7 +176,7 @@ fn test_load_header() { .build(), CellOutput::new_builder() .capacity(500u64.pack()) - .lock(lock_script) + .lock(lock_script.clone()) .build(), ]; @@ -214,6 +214,12 @@ fn test_load_header() { .build(); let tx = context.complete_tx(tx); + #[cfg(feature = "simulator")] + let context = context.set_simulator( + lock_script.code_hash(), + "crates/tests/test-contract/build/debug/libtest_contract_dbg.so", + ); + // run context .verify_tx(&tx, MAX_CYCLES) diff --git a/crates/tests/test-contract/Cargo.lock b/crates/tests/test-contract/Cargo.lock index 6d0f313..887523d 100644 --- a/crates/tests/test-contract/Cargo.lock +++ b/crates/tests/test-contract/Cargo.lock @@ -2,18 +2,49 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anyhow" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" + +[[package]] +name = "bit-vec" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" + [[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 = "buddy-alloc" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c6aa666383df80e31d5e162998ed05880695bb377f2a98210978e586f006aaec" +[[package]] +name = "bytes" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" +dependencies = [ + "serde", +] + [[package]] name = "cc" version = "1.0.67" @@ -26,6 +57,66 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "ckb-channel" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82c904fe3e29474e4d09e8e2775a472456242f8faf18edeeb8c71502710daa38" +dependencies = [ + "crossbeam-channel", +] + +[[package]] +name = "ckb-constant" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7d562e7156216f00c5bf55c8f4c86bc25f9ec0603fcfa6df9b66747462a031a" + +[[package]] +name = "ckb-error" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8418901ea0d15a8e26255bec0b71be5c59056858f51968894796f77f7eefa3e" +dependencies = [ + "anyhow", + "ckb-occupied-capacity", + "derive_more", + "thiserror", +] + +[[package]] +name = "ckb-fixed-hash" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1254aafda84e9abc85301e9e4c5b920c00a9bff0d42248fccf921b340ec5f13e" +dependencies = [ + "ckb-fixed-hash-core", + "ckb-fixed-hash-macros", +] + +[[package]] +name = "ckb-fixed-hash-core" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ffc2850ac8b5643c65913185020de747b290b447d1ee1fd59a2cfdf1a7ceb4f" +dependencies = [ + "faster-hex", + "serde", + "thiserror", +] + +[[package]] +name = "ckb-fixed-hash-macros" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baa5a19a7d18caa5a3a65e66506e28943e95696e286df44457a77692319de429" +dependencies = [ + "ckb-fixed-hash-core", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "ckb-gen-types" version = "0.112.1" @@ -33,8 +124,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a12020d50dd3757cde0fdc88d3837b7a2ab503fe38bd11be86ddace11318c77" dependencies = [ "cfg-if", + "ckb-error", + "ckb-fixed-hash", "ckb-hash", + "ckb-occupied-capacity", "molecule", + "numext-fixed-uint", ] [[package]] @@ -44,6 +139,80 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25af660fc8746f7c756444e6aa47ede9a874206563b6a1ce1b230a5b86519392" dependencies = [ "blake2b-ref", + "blake2b-rs", +] + +[[package]] +name = "ckb-jsonrpc-types" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06798b5bacd7fd4125a34f1c9ebf45be59d50aff0ada003bb8b1c6634a447c73" +dependencies = [ + "ckb-types", + "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-mock-tx-types" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accabfa378c927109844630e4c90e0d7e7e6d6a9174c232f194f490b09e4b757" +dependencies = [ + "ckb-jsonrpc-types", + "ckb-traits", + "ckb-types", + "serde", +] + +[[package]] +name = "ckb-occupied-capacity" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be7132362f12a0495b3b24218afb4e774f9dc0d9254173d2444ec4ac1447461e" +dependencies = [ + "ckb-occupied-capacity-core", + "ckb-occupied-capacity-macros", +] + +[[package]] +name = "ckb-occupied-capacity-core" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4ebe8725d6ee4feb9d376e9a600b2c78cc1dbde5aa754c9a47de871ab917635" +dependencies = [ + "serde", +] + +[[package]] +name = "ckb-occupied-capacity-macros" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201a21d35a377b76440ce12ca62b39e548d019a6330fbdcb40ac0f73fe976cb3" +dependencies = [ + "ckb-occupied-capacity-core", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ckb-rational" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5134dae6f59b1c10457d2a5def081865407e756dd42dd4c7aaf5d6c6bc92b6d1" +dependencies = [ + "numext-fixed-uint", + "serde", ] [[package]] @@ -55,22 +224,359 @@ dependencies = [ "buddy-alloc", "cc", "ckb-gen-types", + "ckb-x64-simulator", "gcd", ] +[[package]] +name = "ckb-traits" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3396b8f328bc76bdfd9bc14ddd984a07d08120cd4e661ba1dc23fecb3a7607a3" +dependencies = [ + "ckb-types", +] + +[[package]] +name = "ckb-types" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2df7c7410664bc917f1b663c574ec7c0e25711fe4f63eb0043c47c197afb0875" +dependencies = [ + "bit-vec", + "bytes", + "ckb-channel", + "ckb-constant", + "ckb-error", + "ckb-fixed-hash", + "ckb-gen-types", + "ckb-hash", + "ckb-merkle-mountain-range", + "ckb-occupied-capacity", + "ckb-rational", + "derive_more", + "golomb-coded-set", + "merkle-cbt", + "molecule", + "numext-fixed-uint", + "once_cell", + "paste", +] + +[[package]] +name = "ckb-x64-simulator" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10ecef7cf93eb5175519b335d31db6d51cdf55276d7fbb8955bbd10e26eb847" +dependencies = [ + "cc", + "ckb-mock-tx-types", + "ckb-types", + "faster-hex", + "lazy_static", + "libc", + "serde", + "serde_derive", + "serde_json", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + +[[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.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f33878137e4dafd7fa914ad4e259e18a4e8e532b9617a2d0150262bf53abfce" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.71", +] + +[[package]] +name = "faster-hex" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51e2ce894d53b295cf97b05685aa077950ff3e8541af83217fc720a6437169f8" + [[package]] name = "gcd" version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d758ba1b47b00caf47f24925c0074ecb20d6dfcffe7f6d53395c0465674841a" +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "golomb-coded-set" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812f314a99fb5b7f0f9d0a8388539578f83f3aca6a65f588b8dbeefb731e2f98" +dependencies = [ + "siphasher", +] + +[[package]] +name = "heapsize" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1679e6ea370dee694f91f1dc469bf94cf8f52051d147aec3e1f9497c6fc22461" +dependencies = [ + "winapi", +] + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[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.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[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" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd9767ab5e5f2ea40f71ff4c8bdb633c50509052e093c2fdd0e390a749dfa3" dependencies = [ + "bytes", "cfg-if", + "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", + "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 = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[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", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core", +] + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "serde" +version = "1.0.204" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.204" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.71", +] + +[[package]] +name = "serde_json" +version = "1.0.120" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" +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 = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", ] [[package]] @@ -79,3 +585,57 @@ version = "0.1.0" dependencies = [ "ckb-std", ] + +[[package]] +name = "thiserror" +version = "1.0.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2675633b1499176c2dff06b0856a27976a8f9d436737b4cf4f312d4d91d8bbb" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d20468752b09f49e909e55a5d338caa8bedf615594e9d80bc4c565d30faf798c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.71", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/crates/tests/test-contract/contracts/test-contract/Cargo.toml b/crates/tests/test-contract/contracts/test-contract/Cargo.toml index 4be7ae6..3852b7a 100644 --- a/crates/tests/test-contract/contracts/test-contract/Cargo.toml +++ b/crates/tests/test-contract/contracts/test-contract/Cargo.toml @@ -5,5 +5,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +simulator = [ "ckb-std/simulator" ] + [dependencies] ckb-std = "0.15.3" diff --git a/crates/tests/test-contract/contracts/test-contract/src/lib.rs b/crates/tests/test-contract/contracts/test-contract/src/lib.rs new file mode 100644 index 0000000..ba66e49 --- /dev/null +++ b/crates/tests/test-contract/contracts/test-contract/src/lib.rs @@ -0,0 +1,6 @@ +#![cfg_attr(not(feature = "simulator"), no_std)] + +#[cfg(feature = "simulator")] +pub mod entry; +#[cfg(feature = "simulator")] +pub mod error; diff --git a/crates/tests/test-contract/contracts/test-contract/test-contract-dbg/.gitignore b/crates/tests/test-contract/contracts/test-contract/test-contract-dbg/.gitignore new file mode 100644 index 0000000..1de5659 --- /dev/null +++ b/crates/tests/test-contract/contracts/test-contract/test-contract-dbg/.gitignore @@ -0,0 +1 @@ +target \ No newline at end of file diff --git a/crates/tests/test-contract/contracts/test-contract/test-contract-dbg/Cargo.lock b/crates/tests/test-contract/contracts/test-contract/test-contract-dbg/Cargo.lock new file mode 100644 index 0000000..a52a3b8 --- /dev/null +++ b/crates/tests/test-contract/contracts/test-contract/test-contract-dbg/Cargo.lock @@ -0,0 +1,679 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" + +[[package]] +name = "bit-vec" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" + +[[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 = "buddy-alloc" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f0d2da64a6a895d5a7e0724882825d50f83c13396b1b9f1878e19a024bab395" + +[[package]] +name = "bytes" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" +dependencies = [ + "serde", +] + +[[package]] +name = "cc" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47de7e88bbbd467951ae7f5a6f34f70d1b4d9cfce53d5fd70f74ebe118b3db56" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "ckb-channel" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82c904fe3e29474e4d09e8e2775a472456242f8faf18edeeb8c71502710daa38" +dependencies = [ + "crossbeam-channel", +] + +[[package]] +name = "ckb-constant" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7d562e7156216f00c5bf55c8f4c86bc25f9ec0603fcfa6df9b66747462a031a" + +[[package]] +name = "ckb-error" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8418901ea0d15a8e26255bec0b71be5c59056858f51968894796f77f7eefa3e" +dependencies = [ + "anyhow", + "ckb-occupied-capacity", + "derive_more", + "thiserror", +] + +[[package]] +name = "ckb-fixed-hash" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1254aafda84e9abc85301e9e4c5b920c00a9bff0d42248fccf921b340ec5f13e" +dependencies = [ + "ckb-fixed-hash-core", + "ckb-fixed-hash-macros", +] + +[[package]] +name = "ckb-fixed-hash-core" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ffc2850ac8b5643c65913185020de747b290b447d1ee1fd59a2cfdf1a7ceb4f" +dependencies = [ + "faster-hex", + "serde", + "thiserror", +] + +[[package]] +name = "ckb-fixed-hash-macros" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baa5a19a7d18caa5a3a65e66506e28943e95696e286df44457a77692319de429" +dependencies = [ + "ckb-fixed-hash-core", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ckb-gen-types" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a12020d50dd3757cde0fdc88d3837b7a2ab503fe38bd11be86ddace11318c77" +dependencies = [ + "cfg-if", + "ckb-error", + "ckb-fixed-hash", + "ckb-hash 0.112.1", + "ckb-occupied-capacity", + "molecule", + "numext-fixed-uint", +] + +[[package]] +name = "ckb-gen-types" +version = "0.116.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a0f2d0f4224507a027d25d64824dd0dc8d367c8b5bead30289eaffe1381a7fb" +dependencies = [ + "cfg-if", + "ckb-hash 0.116.1", + "molecule", +] + +[[package]] +name = "ckb-hash" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25af660fc8746f7c756444e6aa47ede9a874206563b6a1ce1b230a5b86519392" +dependencies = [ + "blake2b-ref", + "blake2b-rs", +] + +[[package]] +name = "ckb-hash" +version = "0.116.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5754bc49cf76a7e8829fe6a7cf1eea1284cbca9777b521f072c76d6ae28d303" +dependencies = [ + "blake2b-ref", +] + +[[package]] +name = "ckb-jsonrpc-types" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06798b5bacd7fd4125a34f1c9ebf45be59d50aff0ada003bb8b1c6634a447c73" +dependencies = [ + "ckb-types", + "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-mock-tx-types" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accabfa378c927109844630e4c90e0d7e7e6d6a9174c232f194f490b09e4b757" +dependencies = [ + "ckb-jsonrpc-types", + "ckb-traits", + "ckb-types", + "serde", +] + +[[package]] +name = "ckb-occupied-capacity" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be7132362f12a0495b3b24218afb4e774f9dc0d9254173d2444ec4ac1447461e" +dependencies = [ + "ckb-occupied-capacity-core", + "ckb-occupied-capacity-macros", +] + +[[package]] +name = "ckb-occupied-capacity-core" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4ebe8725d6ee4feb9d376e9a600b2c78cc1dbde5aa754c9a47de871ab917635" +dependencies = [ + "serde", +] + +[[package]] +name = "ckb-occupied-capacity-macros" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201a21d35a377b76440ce12ca62b39e548d019a6330fbdcb40ac0f73fe976cb3" +dependencies = [ + "ckb-occupied-capacity-core", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ckb-rational" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5134dae6f59b1c10457d2a5def081865407e756dd42dd4c7aaf5d6c6bc92b6d1" +dependencies = [ + "numext-fixed-uint", + "serde", +] + +[[package]] +name = "ckb-std" +version = "0.15.3" +dependencies = [ + "buddy-alloc", + "cc", + "ckb-gen-types 0.116.1", + "ckb-x64-simulator 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gcd", +] + +[[package]] +name = "ckb-std" +version = "0.15.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9357d9464ed36a8e542a7d588e894aff25ccb0cdd66221ac94e76bc69ca3ad55" +replace = "ckb-std 0.15.3" + +[[package]] +name = "ckb-traits" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3396b8f328bc76bdfd9bc14ddd984a07d08120cd4e661ba1dc23fecb3a7607a3" +dependencies = [ + "ckb-types", +] + +[[package]] +name = "ckb-types" +version = "0.112.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2df7c7410664bc917f1b663c574ec7c0e25711fe4f63eb0043c47c197afb0875" +dependencies = [ + "bit-vec", + "bytes", + "ckb-channel", + "ckb-constant", + "ckb-error", + "ckb-fixed-hash", + "ckb-gen-types 0.112.1", + "ckb-hash 0.112.1", + "ckb-merkle-mountain-range", + "ckb-occupied-capacity", + "ckb-rational", + "derive_more", + "golomb-coded-set", + "merkle-cbt", + "molecule", + "numext-fixed-uint", + "once_cell", + "paste", +] + +[[package]] +name = "ckb-x64-simulator" +version = "0.8.0" +dependencies = [ + "cc", + "ckb-mock-tx-types", + "ckb-types", + "faster-hex", + "lazy_static", + "libc", + "serde", + "serde_derive", + "serde_json", +] + +[[package]] +name = "ckb-x64-simulator" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10ecef7cf93eb5175519b335d31db6d51cdf55276d7fbb8955bbd10e26eb847" +replace = "ckb-x64-simulator 0.8.0" + +[[package]] +name = "crossbeam-channel" +version = "0.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + +[[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.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f33878137e4dafd7fa914ad4e259e18a4e8e532b9617a2d0150262bf53abfce" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.71", +] + +[[package]] +name = "faster-hex" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51e2ce894d53b295cf97b05685aa077950ff3e8541af83217fc720a6437169f8" + +[[package]] +name = "gcd" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d758ba1b47b00caf47f24925c0074ecb20d6dfcffe7f6d53395c0465674841a" + +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "golomb-coded-set" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812f314a99fb5b7f0f9d0a8388539578f83f3aca6a65f588b8dbeefb731e2f98" +dependencies = [ + "siphasher", +] + +[[package]] +name = "heapsize" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1679e6ea370dee694f91f1dc469bf94cf8f52051d147aec3e1f9497c6fc22461" +dependencies = [ + "winapi", +] + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[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.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd9767ab5e5f2ea40f71ff4c8bdb633c50509052e093c2fdd0e390a749dfa3" +dependencies = [ + "bytes", + "cfg-if", + "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", + "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 = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[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", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core", +] + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "serde" +version = "1.0.204" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.204" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.71", +] + +[[package]] +name = "serde_json" +version = "1.0.120" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" +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 = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "test-contract" +version = "0.1.0" +dependencies = [ + "ckb-std 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "test-contract-dbg" +version = "0.1.0" +dependencies = [ + "ckb-std 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)", + "test-contract", +] + +[[package]] +name = "thiserror" +version = "1.0.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2675633b1499176c2dff06b0856a27976a8f9d436737b4cf4f312d4d91d8bbb" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d20468752b09f49e909e55a5d338caa8bedf615594e9d80bc4c565d30faf798c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.71", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/crates/tests/test-contract/contracts/test-contract/test-contract-dbg/Cargo.toml b/crates/tests/test-contract/contracts/test-contract/test-contract-dbg/Cargo.toml new file mode 100644 index 0000000..6d05f33 --- /dev/null +++ b/crates/tests/test-contract/contracts/test-contract/test-contract-dbg/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "test-contract-dbg" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dependencies] +test-contract = { path = "../", features = ["simulator"] } +ckb-std = { version = "0.15.3", features = ["simulator"] } + +[lib] +crate-type = ["cdylib"] + +[workspace] + +[replace] +"ckb-std:0.15.3" = { path = "/home/joii/code/ckb-std" } +"ckb-x64-simulator:0.8.0" = { path = "/home/joii/code/ckb-x64-simulator" } \ No newline at end of file diff --git a/crates/tests/test-contract/contracts/test-contract/test-contract-dbg/src/lib.rs b/crates/tests/test-contract/contracts/test-contract/test-contract-dbg/src/lib.rs new file mode 100644 index 0000000..5a55719 --- /dev/null +++ b/crates/tests/test-contract/contracts/test-contract/test-contract-dbg/src/lib.rs @@ -0,0 +1,11 @@ +ckb_std::entry_simulator!(program_entry); +// ckb_std::default_alloc!(); + +/// program entry +fn program_entry() -> i8 { + // Call main function and return error code + match test_contract::entry::main() { + Ok(_) => 0, + Err(err) => err as i8, + } +} diff --git a/crates/testtool/Cargo.toml b/crates/testtool/Cargo.toml index 686edbf..483a89a 100644 --- a/crates/testtool/Cargo.toml +++ b/crates/testtool/Cargo.toml @@ -9,6 +9,9 @@ repository = "https://github.com/nervosnetwork/capsule" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +simulator = [ "libloading", "serde_json" ] + [dependencies] ckb-always-success-script = "0.0.1" rand = "0.8" @@ -24,3 +27,5 @@ ckb-resource = "0.116.1" ckb-verification = "0.116.1" ckb-traits = "0.116.1" ckb-mock-tx-types = "0.116.1" +libloading = { version = "0.8.4", optional = true } +serde_json = { version = "1.0", optional = true } \ No newline at end of file diff --git a/crates/testtool/src/context.rs b/crates/testtool/src/context.rs index 26cfce0..02c2228 100644 --- a/crates/testtool/src/context.rs +++ b/crates/testtool/src/context.rs @@ -61,6 +61,8 @@ pub struct Context { pub cells_by_type_hash: HashMap, capture_debug: bool, captured_messages: Arc>>, + #[cfg(feature = "simulator")] + simulator_hash_map: HashMap, } impl Context { @@ -355,6 +357,7 @@ impl Context { } /// Verify the transaction in CKB-VM + #[cfg(not(feature = "simulator"))] pub fn verify_tx(&self, tx: &TransactionView, max_cycles: u64) -> Result { self.verify_tx_consensus(tx)?; let resolved_tx = self.build_resolved_tx(tx); @@ -389,6 +392,124 @@ impl Context { verifier.verify(max_cycles) } + #[cfg(feature = "simulator")] + pub fn set_simulator(mut self, code_hash: Byte32, path: &str) -> Self { + assert!(std::path::PathBuf::from(path).is_file()); + self.simulator_hash_map.insert(code_hash, path.to_string()); + self + } + + #[cfg(feature = "simulator")] + pub fn verify_tx(&self, tx: &TransactionView, max_cycles: u64) -> Result { + self.verify_tx_consensus(tx)?; + let resolved_tx = self.build_resolved_tx(tx); + let consensus = ConsensusBuilder::default() + .hardfork_switch(HardForks { + ckb2021: CKB2021::new_dev_default(), + ckb2023: CKB2023::new_dev_default(), + }) + .build(); + let tip = HeaderBuilder::default().number(0.pack()).build(); + let tx_verify_env = TxVerifyEnv::new_submit(&tip); + let mut verifier = TransactionScriptsVerifier::new( + Arc::new(resolved_tx), + self.clone(), + Arc::new(consensus), + Arc::new(tx_verify_env), + ); + if self.capture_debug { + let captured_messages = self.captured_messages.clone(); + verifier.set_debug_printer(move |id, message| { + let msg = Message { + id: id.clone(), + message: message.to_string(), + }; + captured_messages.lock().unwrap().push(msg); + }); + } else { + verifier.set_debug_printer(|_id, msg| { + println!("[contract debug] {}", msg); + }); + } + + /// An argument passed to this program. + #[repr(transparent)] + pub struct Arg(*const core::ffi::c_char); + type CkbMainFunc<'a> = libloading::Symbol< + 'a, + unsafe extern "C" fn( + argc: core::ffi::c_int, + // Arg is the same as *const c_char ABI wise. + argv: *const Arg, + // tx: *const core::ffi::c_char, + // tx_len: core::ffi::c_int, + ) -> i8, + >; + + let mut cycles: Cycle = 0; + + let mut index = 0; + for (hash, group) in verifier.groups() { + let code_hash = group.script.code_hash(); + let use_cycles = match self.simulator_hash_map.get(&code_hash) { + Some(sim_path) => { + // + let tmp_dir = std::env::temp_dir().join("ckb-simulator-debugger"); + if !tmp_dir.exists() { + std::fs::create_dir(tmp_dir.clone()) + .expect("create tmp dir: ckb-simulator-debugger"); + } + + let running_setup = tmp_dir.join("ckb_running_setup.json"); + + let setup = format!( + "{{\"is_lock_script\": {}, \"is_output\": false, \"script_index\": {}, \"vm_version\": 1, \"native_binaries\": {{}} }}", + group.group_type == ckb_script::ScriptGroupType::Lock, + index, + ); + std::fs::write(&running_setup, setup).expect("write setup"); + std::env::set_var("CKB_RUNNING_SETUP", running_setup.to_str().unwrap()); + + let tx_file = tmp_dir.join("ckb_running_tx.json"); + let dump_tx = self.dump_tx(&tx)?; + + let tx_json = serde_json::to_string(&dump_tx).expect("dump tx to string"); + std::fs::write(&tx_file, tx_json).expect("write setup"); + + std::env::set_var("CKB_TX_FILE", tx_file.to_str().unwrap()); + + unsafe { + if let Ok(lib) = libloading::Library::new(sim_path) { + if let Ok(func) = lib.get(b"__ckb_std_main") { + let func: CkbMainFunc = func; + let rc = { func(0, [].as_ptr()) }; + assert!(rc == 0, "run simulator failed"); + } + } + } + index += 1; + 0 + } + None => { + index += 1; + group.script.code_hash(); + verifier + .verify_single(group.group_type, hash, max_cycles) + .map_err(|e| { + #[cfg(feature = "logging")] + logging::on_script_error(_hash, &self.hash(), &e); + e.source(group) + })? + } + }; + let r = cycles.overflowing_add(use_cycles); + assert!(!r.1, "cycles overflow"); + cycles = r.0; + } + + Ok(cycles) + } + /// Dump the transaction in mock transaction format, so we can offload it to ckb debugger pub fn dump_tx(&self, tx: &TransactionView) -> Result { let rtx = self.build_resolved_tx(tx); @@ -414,7 +535,7 @@ impl Context { .build(), output: dep.cell_output.clone(), data: dep.mem_cell_data.clone().unwrap(), - header: None, + header: dep.transaction_info.clone().map(|info| info.block_hash), }); } for dep in rtx.resolved_dep_groups.iter() { @@ -425,7 +546,7 @@ impl Context { .build(), output: dep.cell_output.clone(), data: dep.mem_cell_data.clone().unwrap(), - header: None, + header: dep.transaction_info.clone().map(|info| info.block_hash), }); } let mut header_deps = Vec::with_capacity(rtx.transaction.header_deps().len());