Skip to content

Commit

Permalink
✨ Verify claim from
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Feb 9, 2024
1 parent 581dd65 commit d739d52
Show file tree
Hide file tree
Showing 3 changed files with 498 additions and 20 deletions.
54 changes: 54 additions & 0 deletions contracts/__tests__/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,60 @@ where
spec
}

pub fn create_claim_spec<F>(
deposit_header: HeaderView,
withdraw_header: HeaderView,
claim_builder: F,
) -> CustomTxSpec
where
F: Fn(&mut CustomTxSpec) -> Claim,
{
let mut spec = CustomTxSpec::default();
let claim = claim_builder(&mut spec);

let deposit_header_hash = deposit_header.hash();
let deposit_block_number = pack_uint64(deposit_header.number());
let withdraw_header_hash = withdraw_header.hash();

spec.inner.context.insert_header(deposit_header);
spec.inner.context.insert_header(withdraw_header);
spec.inner.context.link_cell_with_block(
spec.inner.dao_input_out_point.clone(),
withdraw_header_hash.clone(),
0,
);

let witnesses = vec![
packed::WitnessArgs::new_builder()
.input_type(Some(Bytes::from(0u64.to_le_bytes().to_vec())).pack())
.build()
.as_bytes(),
Bytes::new(),
spec.inner.pack_dao_operations(vec![], vec![], vec![claim]),
];
let header_deps = vec![deposit_header_hash, withdraw_header_hash];

let dao_type_script = spec.inner.dao_type_script.clone();
spec.on_new_input_spec(move |cell| CellSpec {
output: cell.output.type_(Some(dao_type_script.clone()).pack()),
data: deposit_block_number.as_bytes(),
..cell
});
spec.on_new_output_spec(|cell| CellSpec {
output: cell.output.type_(packed::ScriptOpt::default()),
data: Bytes::new(),
..cell
});

spec.on_new_tx_builder(move |b| {
b.set_witnesses(vec![])
.witnesses(witnesses.clone().pack())
.header_deps(header_deps.clone())
});

spec
}

pub fn new_header_builder(number: u64, epoch_length: u64) -> HeaderBuilder {
let epoch_number = number / epoch_length;
let index = number % epoch_length;
Expand Down
318 changes: 303 additions & 15 deletions contracts/__tests__/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,8 @@ fn test_dao_withdraw_not_found() {

#[test]
fn test_dao_withdraw_not_coverred() {
let mut spec = create_withdraw_spec(new_header_builder(1, 100).build(), None, |spec| {
Withdraw::new_builder()
.cell_pointer(OutPoint::new_unchecked(non_existing_out_point().as_bytes()))
.from(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.to(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.deposit_info(
DepositInfo::new_builder()
.amount(pack_capacity(DEFAULT_CAPACITY))
.build(),
)
.build()
let mut spec = create_withdraw_spec(new_header_builder(1, 100).build(), None, |_spec| {
Withdraw::default()
});

let witnesses = vec![
Expand Down Expand Up @@ -718,3 +705,304 @@ fn test_withdraw_waiting_milliseconds_boundary_cases() {
DAO_CYCLE * ESITMATED_EPOCH_DURATION - ESITMATED_EPOCH_DURATION / 10,
);
}

#[test]
fn test_dao_claim_not_found() {
let mut spec = create_claim_spec(
new_header_builder(1, 100).build(),
new_header_builder(2, 100).build(),
|_spec| {
Claim::new_builder()
.cell_pointer(OutPoint::new_unchecked(non_existing_out_point().as_bytes()))
.build()
},
);

let tx = build_tx(&mut spec);
assert_tx_error(&spec.inner.context, &tx, ErrorCode::NotFound, MAX_CYCLES);
}

#[test]
fn test_dao_claim_not_coverred() {
let mut spec = create_claim_spec(
new_header_builder(1, 100).build(),
new_header_builder(2, 100).build(),
|_spec| Claim::default(),
);

let witnesses = vec![
Bytes::new(),
Bytes::new(),
spec.inner
.pack_dao_data(DaoActionData::default().as_bytes()),
];
spec.on_new_tx_builder(move |b| b.set_witnesses(vec![]).witnesses(witnesses.clone().pack()));

let tx = build_tx(&mut spec);
assert_tx_error(&spec.inner.context, &tx, ErrorCode::NotCoverred, MAX_CYCLES);
}

#[test]
fn test_dao_claim_from_not_match() {
let mut spec = create_claim_spec(
new_header_builder(1, 100).build(),
new_header_builder(2, 100).build(),
|spec| {
Claim::new_builder()
.cell_pointer(OutPoint::new_unchecked(
spec.inner.dao_input_out_point.as_bytes(),
))
.build()
},
);

let tx = build_tx(&mut spec);
assert_tx_error(&spec.inner.context, &tx, ErrorCode::NotMatched, MAX_CYCLES);
}

#[test]
fn test_dao_claim_amount_not_match() {
let mut spec = create_claim_spec(
new_header_builder(1, 100).build(),
new_header_builder(2, 100).build(),
|spec| {
Claim::new_builder()
.cell_pointer(OutPoint::new_unchecked(
spec.inner.dao_input_out_point.as_bytes(),
))
.from(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.build()
},
);

let tx = build_tx(&mut spec);
assert_tx_error(&spec.inner.context, &tx, ErrorCode::NotMatched, MAX_CYCLES);
}

#[test]
fn test_dao_claim_deposit_block_number_not_match() {
let mut spec = create_claim_spec(
new_header_builder(1, 100).build(),
new_header_builder(2, 100).build(),
|spec| {
Claim::new_builder()
.cell_pointer(OutPoint::new_unchecked(
spec.inner.dao_input_out_point.as_bytes(),
))
.from(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.deposit_info(
DepositInfo::new_builder()
.amount(pack_capacity(DEFAULT_CAPACITY))
.build(),
)
.build()
},
);

let tx = build_tx(&mut spec);
assert_tx_error(&spec.inner.context, &tx, ErrorCode::NotMatched, MAX_CYCLES);
}

#[test]
fn test_dao_claim_deposit_timestamp_not_match() {
let mut spec = create_claim_spec(
new_header_builder(1, 100).timestamp(1.pack()).build(),
new_header_builder(2, 100).build(),
|spec| {
Claim::new_builder()
.cell_pointer(OutPoint::new_unchecked(
spec.inner.dao_input_out_point.as_bytes(),
))
.from(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.to(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.deposit_info(
DepositInfo::new_builder()
.deposit_block_number(pack_uint64(1))
.amount(pack_capacity(DEFAULT_CAPACITY))
.build(),
)
.build()
},
);

let tx = build_tx(&mut spec);
assert_tx_error(&spec.inner.context, &tx, ErrorCode::NotMatched, MAX_CYCLES);
}

#[test]
fn test_dao_claim_larger_withdraw_block_number() {
let mut spec = create_claim_spec(
new_header_builder(1, 100).build(),
new_header_builder(1, 100).build(),
|spec| {
Claim::new_builder()
.cell_pointer(OutPoint::new_unchecked(
spec.inner.dao_input_out_point.as_bytes(),
))
.from(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.to(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.deposit_info(
DepositInfo::new_builder()
.deposit_block_number(pack_uint64(1))
.amount(pack_capacity(DEFAULT_CAPACITY))
.build(),
)
.withdraw_info(WithdrawInfo::new_builder().build())
.build()
},
);

let tx = build_tx(&mut spec);
assert_tx_error(&spec.inner.context, &tx, ErrorCode::NotMatched, MAX_CYCLES);
}

#[test]
fn test_dao_claim_withdraw_block_number_not_matched() {
let mut spec = create_claim_spec(
new_header_builder(1, 100).build(),
new_header_builder(2, 100).build(),
|spec| {
Claim::new_builder()
.cell_pointer(OutPoint::new_unchecked(
spec.inner.dao_input_out_point.as_bytes(),
))
.from(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.to(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.deposit_info(
DepositInfo::new_builder()
.deposit_block_number(pack_uint64(1))
.amount(pack_capacity(DEFAULT_CAPACITY))
.build(),
)
.withdraw_info(WithdrawInfo::new_builder().build())
.build()
},
);

let tx = build_tx(&mut spec);
assert_tx_error(&spec.inner.context, &tx, ErrorCode::NotMatched, MAX_CYCLES);
}

#[test]
fn test_dao_claim_withdraw_timestamp_not_matched() {
let mut spec = create_claim_spec(
new_header_builder(1, 100).build(),
new_header_builder(2, 100).timestamp(1.pack()).build(),
|spec| {
Claim::new_builder()
.cell_pointer(OutPoint::new_unchecked(
spec.inner.dao_input_out_point.as_bytes(),
))
.from(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.to(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.deposit_info(
DepositInfo::new_builder()
.deposit_block_number(pack_uint64(1))
.amount(pack_capacity(DEFAULT_CAPACITY))
.build(),
)
.withdraw_info(
WithdrawInfo::new_builder()
.withdraw_block_number(pack_uint64(2))
.build(),
)
.build()
},
);

let tx = build_tx(&mut spec);
assert_tx_error(&spec.inner.context, &tx, ErrorCode::NotMatched, MAX_CYCLES);
}

#[test]
fn test_dao_claim_componsation_amount_not_matched() {
let mut spec = create_claim_spec(
new_header_builder(1, 100).dao(pack_ar(100)).build(),
new_header_builder(2, 100).dao(pack_ar(110)).build(),
|spec| {
Claim::new_builder()
.cell_pointer(OutPoint::new_unchecked(
spec.inner.dao_input_out_point.as_bytes(),
))
.from(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.to(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.deposit_info(
DepositInfo::new_builder()
.deposit_block_number(pack_uint64(1))
.amount(pack_capacity(DEFAULT_CAPACITY))
.build(),
)
.withdraw_info(
WithdrawInfo::new_builder()
.withdraw_block_number(pack_uint64(2))
.build(),
)
.build()
},
);

let tx = build_tx(&mut spec);
assert_tx_error(&spec.inner.context, &tx, ErrorCode::NotMatched, MAX_CYCLES);
}

#[test]
fn test_dao_claim_pass() {
let mut spec = create_claim_spec(
new_header_builder(1, 100).dao(pack_ar(100)).build(),
new_header_builder(2, 100).dao(pack_ar(110)).build(),
|spec| {
Claim::new_builder()
.cell_pointer(OutPoint::new_unchecked(
spec.inner.dao_input_out_point.as_bytes(),
))
.from(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.to(Address::new_unchecked(
spec.inner.alice_lock_script.as_bytes(),
))
.deposit_info(
DepositInfo::new_builder()
.deposit_block_number(pack_uint64(1))
.amount(pack_capacity(DEFAULT_CAPACITY))
.build(),
)
.withdraw_info(
WithdrawInfo::new_builder()
.withdraw_block_number(pack_uint64(2))
.componsation_amount(pack_capacity(
(DEFAULT_CAPACITY - DAO_INPUT_OCCUPIED_CAPACITY) / 10,
))
.build(),
)
.build()
},
);

let tx = build_tx(&mut spec);
verify_and_dump_failed_tx(&spec.inner.context, &tx, MAX_CYCLES).expect("pass");
}
Loading

0 comments on commit d739d52

Please sign in to comment.