Skip to content

Commit

Permalink
Handle fill nonces in the wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
azarovh committed Feb 26, 2025
1 parent b1787fa commit 3516aff
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 36 deletions.
5 changes: 2 additions & 3 deletions api-server/api-server-common/src/storage/storage_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,9 @@ pub struct Order {

impl Order {
pub fn fill(self, fill_amount_in_ask_currency: Amount) -> Self {
// FIXME: use initial balance
let filled_amount = orders_accounting::calculate_filled_amount(
self.ask_balance,
self.give_balance,
self.initially_asked,
self.initially_given,
fill_amount_in_ask_currency,
)
.expect("must succeed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,16 +391,19 @@ fn check_order_inputs_outputs(
.1
.orders_version();

if let OrdersVersion::V1 = orders_version {
// Forbidding fills with zero ensures that tx has utxo and therefor is unique.
// Unique txs cannot be replayed.
ensure!(
*fill > Amount::ZERO,
CheckTransactionError::AttemptToFillOrderWithZero(
*id,
tx.transaction().get_id()
)
);
match orders_version {
OrdersVersion::V0 => { /*do nothing */ }
OrdersVersion::V1 => {
// Forbidding fills with zero ensures that tx has utxo and therefor is unique.
// Unique txs cannot be replayed.
ensure!(
*fill > Amount::ZERO,
CheckTransactionError::AttemptToFillOrderWithZero(
*id,
tx.transaction().get_id()
)
);
}
}
}
},
Expand Down
2 changes: 0 additions & 2 deletions common/src/chain/transaction/account_outpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,10 @@ pub enum AccountCommand {
ChangeTokenAuthority(TokenId, Destination),
// Close an order and withdraw all remaining funds from both give and ask balances.
// Only the address specified as `conclude_key` can authorize this command.
// FIXME: add original balances here so that they are included into the signature
#[codec(index = 6)]
ConcludeOrder(OrderId),
// Satisfy an order completely or partially.
// Second parameter is an amount provided to fill an order which corresponds to order's ask currency.
// FIXME: for fill order inputs there should be createorder utxo in the signature
#[codec(index = 7)]
FillOrder(OrderId, Amount, Destination),
// Change token metadata uri
Expand Down
2 changes: 1 addition & 1 deletion common/src/chain/upgrades/chainstate_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub enum FrozenTokensValidationVersion {
pub enum OrdersVersion {
/// Initial orders implementation
V0,
// FIXME: document changes
/// Calculate fill amount based on original balances; ignore nonce for fill operations
V1,
}

Expand Down
2 changes: 1 addition & 1 deletion mempool/src/pool/tx_pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ impl<M: MemoryUsageEstimator> TxPool<M> {
) {
let result = self.store.drop_tx_and_descendants(tx_id, reason).try_for_each(|entry| {
self.tx_verifier
.disconnect_transaction(&source, entry.transaction())
.disconnect_transaction(source, entry.transaction())
.map_err(|err| (*entry.tx_id(), err))
});

Expand Down
16 changes: 5 additions & 11 deletions wallet/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,10 +1122,9 @@ impl Account {
self.get_new_address(db_tx, KeyPurpose::ReceiveFunds)?.1.into_object()
};

// FIXME: use initial balance
let filled_amount = orders_accounting::calculate_filled_amount(
order_info.ask_balance,
order_info.give_balance,
order_info.initially_asked.amount(),
order_info.initially_given.amount(),
fill_amount_in_ask_currency,
)
.ok_or(WalletError::CalculateOrderFilledAmountFailed(order_id))?;
Expand All @@ -1135,13 +1134,9 @@ impl Account {
};
let outputs = vec![TxOutput::Transfer(output_value, output_destination.clone())];

let nonce = order_info
.nonce
.map_or(Some(AccountNonce::new(0)), |n| n.increment())
.ok_or(WalletError::OrderNonceOverflow(order_id))?;
let request = SendRequest::new().with_outputs(outputs).with_inputs_and_destinations([(
TxInput::AccountCommand(
nonce,
AccountNonce::new(0), // nonce will be ignored so just put 0
AccountCommand::FillOrder(
order_id,
fill_amount_in_ask_currency,
Expand Down Expand Up @@ -2501,10 +2496,9 @@ fn group_preselected_inputs(
.and_then(|info| info.get(order_id))
.ok_or(WalletError::OrderInfoMissing(*order_id))?;

// FIXME: use initial balance
let filled_amount = orders_accounting::calculate_filled_amount(
order_info.ask_balance,
order_info.give_balance,
order_info.initially_asked.amount(),
order_info.initially_given.amount(),
*fill_amount_in_ask_currency,
)
.ok_or(WalletError::CalculateOrderFilledAmountFailed(*order_id))?;
Expand Down
18 changes: 12 additions & 6 deletions wallet/src/account/output_cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,7 @@ impl OutputCache {
self.token_issuance.insert(*token_id, data);
}
}
AccountCommand::ConcludeOrder(order_id)
| AccountCommand::FillOrder(order_id, _, _) => {
AccountCommand::ConcludeOrder(order_id) => {
if !already_present {
if let Some(data) = self.orders.get_mut(order_id) {
Self::update_order_state(
Expand All @@ -993,6 +992,9 @@ impl OutputCache {
}
}
}
AccountCommand::FillOrder(_, _, _) => {
// fill order cannot have any descendants
}
},
}
}
Expand Down Expand Up @@ -1125,14 +1127,16 @@ impl OutputCache {
data.unconfirmed_txs.remove(tx_id);
}
}
AccountCommand::ConcludeOrder(order_id)
| AccountCommand::FillOrder(order_id, _, _) => {
AccountCommand::ConcludeOrder(order_id) => {
if let Some(data) = self.orders.get_mut(order_id) {
data.last_nonce = nonce.decrement();
data.last_parent =
find_parent(&self.unconfirmed_descendants, tx_id.clone());
}
}
AccountCommand::FillOrder(_, _, _) => {
// fill order cannot have any descendants
}
},
}
}
Expand Down Expand Up @@ -1425,8 +1429,7 @@ impl OutputCache {
data.unconfirmed_txs.remove(&tx_id.into());
}
}
AccountCommand::ConcludeOrder(order_id)
| AccountCommand::FillOrder(order_id, _, _) => {
AccountCommand::ConcludeOrder(order_id) => {
if let Some(data) = self.orders.get_mut(order_id) {
data.last_nonce = nonce.decrement();
data.last_parent = find_parent(
Expand All @@ -1435,6 +1438,9 @@ impl OutputCache {
);
}
}
AccountCommand::FillOrder(_, _, _) => {
// fill order cannot have any descendants
}
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions wallet/src/wallet/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5892,7 +5892,7 @@ fn create_order_fill_completely_conclude(#[case] seed: Seed) {
},
give_balance: Amount::ZERO,
ask_balance: Amount::ZERO,
nonce: Some(AccountNonce::new(1)),
nonce: None,
};
let conclude_order_tx = wallet1
.create_conclude_order_tx(
Expand Down Expand Up @@ -6146,7 +6146,7 @@ fn create_order_fill_partially_conclude(#[case] seed: Seed) {
},
give_balance: (sell_amount - Amount::from_atoms(100)).unwrap(),
ask_balance: (token_amount_to_mint - Amount::from_atoms(10)).unwrap(),
nonce: Some(AccountNonce::new(0)),
nonce: None,
};

let conclude_order_tx = wallet1
Expand Down

0 comments on commit 3516aff

Please sign in to comment.