From e3c04162d37915dd1cabc3dcd996fa95b893b476 Mon Sep 17 00:00:00 2001 From: dylan Date: Wed, 31 Jan 2024 15:22:42 +0800 Subject: [PATCH 1/2] add taker tests --- Cargo.lock | 69 ---------- contracts/dex-lock/src/entry.rs | 6 +- contracts/dex-lock/src/helper.rs | 2 +- tests/Cargo.toml | 5 +- tests/src/helper.rs | 27 ++++ tests/src/lib.rs | 4 + tests/src/taker_tests.rs | 217 +++++++++++++++++++++++++++++++ 7 files changed, 254 insertions(+), 76 deletions(-) create mode 100644 tests/src/helper.rs create mode 100644 tests/src/taker_tests.rs diff --git a/Cargo.lock b/Cargo.lock index bcd7d76..0e85f7f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -639,21 +639,6 @@ dependencies = [ "miniz_oxide", ] -[[package]] -name = "foreign-types" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" -dependencies = [ - "foreign-types-shared", -] - -[[package]] -name = "foreign-types-shared" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" - [[package]] name = "getrandom" version = "0.1.16" @@ -894,45 +879,6 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" -[[package]] -name = "openssl" -version = "0.10.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b102428fd03bc5edf97f62620f7298614c45cedf287c271e7ed450bbaf83f2e1" -dependencies = [ - "bitflags", - "cfg-if", - "foreign-types", - "libc", - "once_cell", - "openssl-macros", - "openssl-sys", -] - -[[package]] -name = "openssl-macros" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b501e44f11665960c7e7fcf062c7d96a14ade4aa98116c004b2e37b5be7d736c" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "openssl-sys" -version = "0.9.80" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23bbbf7854cd45b83958ebe919f0e8e516793727652e27fda10a8384cfc790b7" -dependencies = [ - "autocfg", - "cc", - "libc", - "pkg-config", - "vcpkg", -] - [[package]] name = "parking_lot" version = "0.12.1" @@ -1000,12 +946,6 @@ dependencies = [ "siphasher", ] -[[package]] -name = "pkg-config" -version = "0.3.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" - [[package]] name = "plain" version = "0.2.3" @@ -1287,12 +1227,9 @@ dependencies = [ name = "tests" version = "0.1.0" dependencies = [ - "blake2b-rs", "ckb-testtool", "hex", - "openssl", "rand 0.8.5", - "rand_core 0.6.4", ] [[package]] @@ -1330,12 +1267,6 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" -[[package]] -name = "vcpkg" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" - [[package]] name = "version_check" version = "0.9.4" diff --git a/contracts/dex-lock/src/entry.rs b/contracts/dex-lock/src/entry.rs index ce50f0b..dc068d4 100644 --- a/contracts/dex-lock/src/entry.rs +++ b/contracts/dex-lock/src/entry.rs @@ -22,8 +22,10 @@ pub fn main() -> Result<(), Error> { if args.owner_lock != output_lock { return Err(Error::DexOwnerLockNotMatch); } - let output_capacity = load_cell_capacity(dex_index, Source::Output)?; - if args.total_value > output_capacity.into() { + + let dex_input_capacity = load_cell_capacity(dex_index, Source::Input)? as u128; + let output_capacity = load_cell_capacity(dex_index, Source::Output)? as u128; + if (args.total_value + dex_input_capacity) > output_capacity { return Err(Error::DexTotalValueNotMatch); } diff --git a/contracts/dex-lock/src/helper.rs b/contracts/dex-lock/src/helper.rs index f9d3937..8f2c961 100644 --- a/contracts/dex-lock/src/helper.rs +++ b/contracts/dex-lock/src/helper.rs @@ -29,7 +29,7 @@ impl DexArgs { } let owner_size = u32::from_le_bytes(parse_array::<4>(&data[0..4])?) as usize; let required_size = owner_size + 17; - if data.len() < (required_size + 17) { + if data.len() < required_size { return Err(Error::LockArgsInvalid); } diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 1615458..08ae6a8 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -5,8 +5,5 @@ edition = "2021" [dependencies] ckb-testtool = "0.10.1" -rand_core = "0.6.3" hex = "0.4" -blake2b-rs = "0.2" -rand = "0.8.5" -openssl = "0.10.38" \ No newline at end of file +rand = "0.8.5" \ No newline at end of file diff --git a/tests/src/helper.rs b/tests/src/helper.rs new file mode 100644 index 0000000..da5ec2a --- /dev/null +++ b/tests/src/helper.rs @@ -0,0 +1,27 @@ +#![allow(dead_code)] + +use ckb_testtool::ckb_types::{packed::Script, prelude::Entity}; + +#[derive(Debug, Clone)] +pub struct DexArgs { + pub owner_lock: Script, + pub setup: u8, + pub total_value: u128, + pub receiver_lock: Option