diff --git a/api-server/api-server-common/src/storage/storage_api/mod.rs b/api-server/api-server-common/src/storage/storage_api/mod.rs index 0fe92ef04..a9547aa79 100644 --- a/api-server/api-server-common/src/storage/storage_api/mod.rs +++ b/api-server/api-server-common/src/storage/storage_api/mod.rs @@ -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"); diff --git a/chainstate/tx-verifier/src/transaction_verifier/check_transaction.rs b/chainstate/tx-verifier/src/transaction_verifier/check_transaction.rs index a5b89c186..c9f787442 100644 --- a/chainstate/tx-verifier/src/transaction_verifier/check_transaction.rs +++ b/chainstate/tx-verifier/src/transaction_verifier/check_transaction.rs @@ -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() + ) + ); + } } } }, diff --git a/common/src/chain/transaction/account_outpoint.rs b/common/src/chain/transaction/account_outpoint.rs index f0f9a347d..5428dce51 100644 --- a/common/src/chain/transaction/account_outpoint.rs +++ b/common/src/chain/transaction/account_outpoint.rs @@ -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 diff --git a/common/src/chain/upgrades/chainstate_upgrade.rs b/common/src/chain/upgrades/chainstate_upgrade.rs index 50aa9ad7b..a792f2152 100644 --- a/common/src/chain/upgrades/chainstate_upgrade.rs +++ b/common/src/chain/upgrades/chainstate_upgrade.rs @@ -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, } diff --git a/mempool/src/pool/tx_pool/mod.rs b/mempool/src/pool/tx_pool/mod.rs index bafc8f4d6..7a71683bb 100644 --- a/mempool/src/pool/tx_pool/mod.rs +++ b/mempool/src/pool/tx_pool/mod.rs @@ -665,7 +665,7 @@ impl TxPool { ) { 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)) }); diff --git a/wallet/src/account/mod.rs b/wallet/src/account/mod.rs index 19a697970..b749eba9c 100644 --- a/wallet/src/account/mod.rs +++ b/wallet/src/account/mod.rs @@ -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))?; @@ -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, @@ -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))?; diff --git a/wallet/src/account/output_cache/mod.rs b/wallet/src/account/output_cache/mod.rs index 1430605d2..18ec6b29a 100644 --- a/wallet/src/account/output_cache/mod.rs +++ b/wallet/src/account/output_cache/mod.rs @@ -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( @@ -993,6 +992,9 @@ impl OutputCache { } } } + AccountCommand::FillOrder(_, _, _) => { + // fill order cannot have any descendants + } }, } } @@ -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 + } }, } } @@ -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( @@ -1435,6 +1438,9 @@ impl OutputCache { ); } } + AccountCommand::FillOrder(_, _, _) => { + // fill order cannot have any descendants + } }, } } diff --git a/wallet/src/wallet/tests.rs b/wallet/src/wallet/tests.rs index d9b3ab5a8..dfa50e71b 100644 --- a/wallet/src/wallet/tests.rs +++ b/wallet/src/wallet/tests.rs @@ -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( @@ -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