Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions chainstate/test-framework/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,16 @@ pub fn calculate_fill_order(
.unwrap()
}

pub fn order_min_non_zero_fill_amount(
/// For orders v1, return the minimal fill amount that would result in a non-zero filled amount.
/// For orders v0 return 1.
pub fn order_min_accepted_non_zero_fill_amount(
tf: &TestFramework,
order_id: &OrderId,
orders_version: OrdersVersion,
) -> Amount {
match orders_version {
// Note: in orders v0 even direct zero fills are allowed.
// However, this function is supposed to only return non-zero amounts.
OrdersVersion::V0 => Amount::from_atoms(1),

// In orders v1, the fill amount must be big enough so that the filled amount is non-zero.
OrdersVersion::V1 => {
let db_tx = tf.storage.transaction_ro().unwrap();
let orders_db = OrdersAccountingDB::new(&db_tx);
Expand All @@ -321,6 +320,35 @@ pub fn order_min_non_zero_fill_amount(
}
}

/// Return the minimal fill amount that would result in a non-zero filled amount.
/// Note: since orders v0 use the current balance for the filled amount calculation, this function
/// should only be used to generate the first fill of the v0 order in the block under construction.
pub fn order_min_amount_for_non_zero_fill(
tf: &TestFramework,
order_id: &OrderId,
orders_version: OrdersVersion,
) -> Amount {
let db_tx = tf.storage.transaction_ro().unwrap();
let orders_db = OrdersAccountingDB::new(&db_tx);

match orders_version {
OrdersVersion::V0 => {
let ask_balance = orders_db.get_ask_balance(order_id).unwrap().into_atoms();
let give_balance = orders_db.get_give_balance(order_id).unwrap().into_atoms();

Amount::from_atoms(ask_balance.div_ceil(give_balance))
}

OrdersVersion::V1 => {
let order_data = orders_db.get_order_data(order_id).unwrap().unwrap();
let original_ask = order_data.ask().amount().into_atoms();
let original_give = order_data.give().amount().into_atoms();

Amount::from_atoms(original_ask.div_ceil(original_give))
}
}
}

/// Split an u128 value into the specified number of "randomish" parts (the min part size is half
/// the average part size).
pub fn split_u128(rng: &mut impl CryptoRng, amount: u128, parts_count: usize) -> Vec<u128> {
Expand Down
32 changes: 15 additions & 17 deletions chainstate/test-suite/src/tests/input_commitments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,17 +389,14 @@ fn order_fill(#[case] seed: Seed, #[case] orders_version: OrdersVersion) {
let order_fill_tx = TransactionBuilder::new()
.add_input(coins_outpoint.into(), InputWitness::NoSignature(None))
.add_input(fill_order_input, InputWitness::NoSignature(None))
.add_output(TxOutput::Transfer(
OutputValue::TokenV1(token_id, filled_amount),
Destination::AnyoneCanSpend,
))
.add_output(TxOutput::Transfer(
OutputValue::Coin(coins_left),
coins_owner_dest.clone(),
))
.add_token_transfer_output_if_non_zero(token_id, filled_amount, Destination::AnyoneCanSpend)
.build();
let order_fill_tx_id = order_fill_tx.transaction().get_id();
let coins_outpoint = UtxoOutPoint::new(order_fill_tx_id.into(), 1);
let coins_outpoint = UtxoOutPoint::new(order_fill_tx_id.into(), 0);

tf.make_block_builder()
.add_transaction(order_fill_tx)
Expand All @@ -418,15 +415,19 @@ fn order_fill(#[case] seed: Seed, #[case] orders_version: OrdersVersion) {
let fill_order_input =
make_fill_order_input(orders_version, AccountNonce::new(1), &order_id, fill_amount);

let tx = Transaction::new(
0,
vec![coins_outpoint.clone().into(), fill_order_input],
vec![TxOutput::Transfer(
OutputValue::TokenV1(token_id, filled_amount),
let tx = TransactionBuilder::new()
.add_input(
coins_outpoint.clone().into(),
InputWitness::NoSignature(None),
)
.add_input(fill_order_input, InputWitness::NoSignature(None))
.add_token_transfer_output_if_non_zero(
token_id,
filled_amount,
Destination::AnyoneCanSpend,
)],
)
.unwrap();
)
.build()
.take_transaction();

let coins_utxo = tf.utxo(&coins_outpoint).take_output();
let bad_coins_utxo = {
Expand Down Expand Up @@ -676,14 +677,11 @@ fn order_conclude(#[case] seed: Seed, #[case] orders_version: OrdersVersion) {
let order_fill_tx = TransactionBuilder::new()
.add_input(coins_outpoint.into(), InputWitness::NoSignature(None))
.add_input(fill_order_input, InputWitness::NoSignature(None))
.add_output(TxOutput::Transfer(
OutputValue::TokenV1(token_id, filled_amount),
Destination::AnyoneCanSpend,
))
.add_output(TxOutput::Transfer(
OutputValue::Coin(coins_left),
Destination::AnyoneCanSpend,
))
.add_token_transfer_output_if_non_zero(token_id, filled_amount, Destination::AnyoneCanSpend)
.build();

tf.make_block_builder()
Expand Down
Loading
Loading