From b465967a2ea15a11220c82c956722d8c77b8ed93 Mon Sep 17 00:00:00 2001 From: bayk Date: Fri, 30 Aug 2024 10:58:26 -0700 Subject: [PATCH] if you set max_outputs to 2 on init_send_tx you get a panic. Fix #34 --- api/src/owner_rpc_v2.rs | 30 +++++++++++++++++++++++++++++ libwallet/src/internal/selection.rs | 10 ++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/api/src/owner_rpc_v2.rs b/api/src/owner_rpc_v2.rs index 4f8ef964a..3c611a7ff 100644 --- a/api/src/owner_rpc_v2.rs +++ b/api/src/owner_rpc_v2.rs @@ -717,6 +717,36 @@ pub trait OwnerRpcV2: Sync + Send { } # "# # ,false, 4, false, false, false, false, false); + # + # // Testing low 'max_outputs' value. Expecting to get error that slate is too large + # grin_wallet_api::doctest_helper_json_rpc_owner_assert_response!( + # r#" + { + "jsonrpc": "2.0", + "method": "init_send_tx", + "params": { + "args": { + "amount": "20000000", + "minimum_confirmations": 2, + "max_outputs": 2 + } + }, + "id": 1 + } + # "# + # , + # r#" + { + "id": 1, + "jsonrpc": "2.0", + "result": { + "Err": { + "TooLargeSlate": 2 + } + } + } + # "# + # ,false, 4, false, false, false, false, false); ``` */ diff --git a/libwallet/src/internal/selection.rs b/libwallet/src/internal/selection.rs index fbceed20d..ef839d7ab 100644 --- a/libwallet/src/internal/selection.rs +++ b/libwallet/src/internal/selection.rs @@ -864,10 +864,12 @@ where // amount, the wallet should allow going over it to satisfy what the user // wants to send. So the wallet considers max_outputs more of a soft limit. if eligible.len() > max_outputs { - for window in eligible.windows(max_outputs) { - let windowed_eligibles = window.to_vec(); - if let Some(outputs) = select_from(amount, select_all, windowed_eligibles) { - return (max_available, outputs); + if max_outputs > 0 { + for window in eligible.windows(max_outputs) { + let windowed_eligibles = window.to_vec(); + if let Some(outputs) = select_from(amount, select_all, windowed_eligibles) { + return (max_available, outputs); + } } } // Not exist in any window of which total amount >= amount.