Skip to content

Commit

Permalink
add cancel tests
Browse files Browse the repository at this point in the history
  • Loading branch information
duanyytop committed Jan 31, 2024
1 parent e3c0416 commit fdf2a40
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 39 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ jobs:

- name: Install Capsule
env:
CAPSULE_VERSION: v0.10.1
CAPSULE_VERSION: v0.10.3
run: |
sudo curl -OL https://github.com/nervosnetwork/capsule/releases/download/${CAPSULE_VERSION}/capsule_${CAPSULE_VERSION}_x86_64-linux.tar.gz && sudo tar xf capsule_${CAPSULE_VERSION}_x86_64-linux.tar.gz && echo `pwd`/capsule_${CAPSULE_VERSION}_x86_64-linux >> $GITHUB_PATH
- name: Build contracts
run: make build-release
run: make build

- name: Test contracts
run: make test-release
run: make test
51 changes: 21 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion capsule.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "0.10.1"
version = "0.10.3"
deployment = "deployment.toml"

[[contracts]]
Expand Down
2 changes: 1 addition & 1 deletion contracts/dex-lock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ version = "0.1.0"
edition = "2021"

[dependencies]
ckb-std = "0.15.0"
ckb-std = "0.14.0"
3 changes: 2 additions & 1 deletion contracts/dex-lock/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
};
use ckb_std::{
ckb_constants::Source,
ckb_types::prelude::Entity,
high_level::{load_cell_capacity, load_cell_lock},
};

Expand All @@ -19,7 +20,7 @@ pub fn main() -> Result<(), Error> {
// to the seller's lock script
let dex_index = position_dex_lock_in_inputs()?;
let output_lock = load_cell_lock(dex_index, Source::Output)?;
if args.owner_lock != output_lock {
if args.owner_lock.as_slice() != output_lock.as_slice() {
return Err(Error::DexOwnerLockNotMatch);
}

Expand Down
5 changes: 3 additions & 2 deletions contracts/dex-lock/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ impl DexArgs {
pub fn position_dex_lock_in_inputs() -> Result<usize, Error> {
let current_lock = load_script()?;
QueryIter::new(load_cell_lock, Source::Input)
.position(|lock| lock == current_lock)
.position(|lock| lock.as_slice() == current_lock.as_slice())
.ok_or(Error::IndexOutOfBound)
}

pub fn inputs_contain_owner_cell(args: &DexArgs) -> bool {
QueryIter::new(load_cell_lock, Source::Input).any(|lock| lock == args.owner_lock)
QueryIter::new(load_cell_lock, Source::Input)
.any(|lock| lock.as_slice() == args.owner_lock.as_slice())
}
2 changes: 1 addition & 1 deletion tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ version = "0.1.0"
edition = "2021"

[dependencies]
ckb-testtool = "0.10.1"
ckb-testtool = "0.10"
hex = "0.4"
rand = "0.8.5"
188 changes: 188 additions & 0 deletions tests/src/cancel_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
use self::helper::DexArgs;

use super::*;
use ckb_testtool::builtin::ALWAYS_SUCCESS;
use ckb_testtool::ckb_hash::blake2b_256;
use ckb_testtool::ckb_types::{
bytes::Bytes,
core::{TransactionBuilder, TransactionView},
packed::*,
prelude::*,
};
use ckb_testtool::context::Context;
use rand::{thread_rng, Rng};

const MAX_CYCLES: u64 = 70_000_000;

// error numbers
const LOCK_ARGS_INVALID: i8 = 5;
const DEX_TOTAL_VALUE_NOT_MATCH: i8 = 7;

#[derive(PartialEq, Eq, Clone, Copy)]
enum DexError {
NoError,
LockArgsInvalid,
DexTotalValueNotMatch,
}

fn create_test_context(error: DexError) -> (Context, TransactionView) {
// deploy contract
let mut context = Context::default();
let dex_bin: Bytes = Loader::default().load_binary("dex-lock");
let dex_out_point = context.deploy_cell(dex_bin);
let dex_dep = CellDep::new_builder()
.out_point(dex_out_point.clone())
.build();

let always_success_code_hash = blake2b_256(ALWAYS_SUCCESS.clone());
let always_success_out_point = context.deploy_cell(ALWAYS_SUCCESS.clone());
let always_success_dep = CellDep::new_builder()
.out_point(always_success_out_point.clone())
.build();

let mut rng = thread_rng();
let owner_lock = context
.build_script(
&always_success_out_point,
rng.gen::<[u8; 20]>().to_vec().into(),
)
.expect("script");

let dex_args1 = DexArgs {
owner_lock: owner_lock.clone(),
setup: 0u8,
total_value: 1234_5678_0000u128,
receiver_lock: None,
unit_type_hash: None,
};
let dex_lock_script1 = context
.build_script(&dex_out_point, dex_args1.to_vec().into())
.expect("script");

let dex_args2 = DexArgs {
owner_lock: owner_lock.clone(),
setup: 0u8,
total_value: 9_8765_0000_1234u128,
receiver_lock: None,
unit_type_hash: None,
};
let mut dex_args2_vec = dex_args2.to_vec();
if error == DexError::LockArgsInvalid {
dex_args2_vec.reverse();
}
let dex_lock_script2 = context
.build_script(&dex_out_point, dex_args2_vec.into())
.expect("script");

let asset_type = ScriptBuilder::default()
.code_hash(Byte32::from_slice(&always_success_code_hash).unwrap())
.hash_type(Byte::from(2u8))
.build();

let asset_amount1 = Bytes::try_from(1000_0000_0000u128.to_le_bytes().to_vec()).unwrap();
let input_out_point1 = context.create_cell(
CellOutput::new_builder()
.capacity(300_0000_0000u64.pack())
.lock(dex_lock_script1)
.type_(Some(asset_type.clone()).pack())
.build(),
asset_amount1,
);
let asset_amount2 = Bytes::try_from(3456_0000_0000u128.to_le_bytes().to_vec()).unwrap();
let input_out_point2 = context.create_cell(
CellOutput::new_builder()
.capacity(240_0000_0000u64.pack())
.lock(dex_lock_script2)
.type_(Some(asset_type.clone()).pack())
.build(),
asset_amount2,
);
let input_out_point3 = context.create_cell(
CellOutput::new_builder()
.capacity(100_0000_0000u64.pack())
.lock(owner_lock.clone())
.build(),
Bytes::new(),
);

let mut inputs = vec![
CellInput::new_builder()
.previous_output(input_out_point1)
.build(),
CellInput::new_builder()
.previous_output(input_out_point2)
.build(),
];
if error != DexError::DexTotalValueNotMatch {
inputs.push(
CellInput::new_builder()
.previous_output(input_out_point3)
.build(),
)
}

let output_asset_amount = Bytes::try_from(
(1000_0000_0000u128 + 3456_0000_0000u128)
.to_le_bytes()
.to_vec(),
)
.unwrap();
let outputs = vec![
CellOutput::new_builder()
.capacity(240_0000_0000u64.pack())
.lock(owner_lock.clone())
.type_(Some(asset_type).pack())
.build(),
CellOutput::new_builder()
.capacity(400_0000_0000u64.pack())
.lock(owner_lock)
.build(),
];

let outputs_data = vec![output_asset_amount, Bytes::new()];

let mut witnesses = vec![];
for _ in 0..inputs.len() {
witnesses.push(Bytes::new())
}

let cell_deps = vec![always_success_dep, dex_dep];
// build transaction
let tx = TransactionBuilder::default()
.inputs(inputs)
.outputs(outputs)
.outputs_data(outputs_data.pack())
.cell_deps(cell_deps)
.witnesses(witnesses.pack())
.build();
let tx = context.complete_tx(tx);

// sign
(context, tx)
}

#[test]
fn test_dex_cancel_order_success() {
let (context, tx) = create_test_context(DexError::NoError);
// run
let cycles = context
.verify_tx(&tx, MAX_CYCLES)
.expect("pass verification");
println!("consume cycles: {}", cycles);
}

#[test]
fn test_dex_cancel_order_lock_args_error() {
let (context, tx) = create_test_context(DexError::LockArgsInvalid);
// run
let err = context.verify_tx(&tx, MAX_CYCLES).unwrap_err();
assert_script_error(err, LOCK_ARGS_INVALID);
}

#[test]
fn test_dex_cancel_order_owner_lock_not_match_error() {
let (context, tx) = create_test_context(DexError::DexTotalValueNotMatch);
// run
let err = context.verify_tx(&tx, MAX_CYCLES).unwrap_err();
assert_script_error(err, DEX_TOTAL_VALUE_NOT_MATCH);
}
2 changes: 2 additions & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::fs;
use std::path::PathBuf;
use std::str::FromStr;

#[cfg(test)]
mod cancel_tests;
mod helper;
#[cfg(test)]
mod taker_tests;
Expand Down

0 comments on commit fdf2a40

Please sign in to comment.