Skip to content

Commit

Permalink
[wallet] panic: 'attempt to subtract with overflow' when sending smal…
Browse files Browse the repository at this point in the history
…l quantities #396 (#603)

* Fund calculation inconsistent (#582)

* [wallet] panic: 'attempt to subtract with overflow' when sending small quantities (#396)
  • Loading branch information
heunglee authored and yeastplume committed Jan 12, 2018
1 parent 31f91dc commit 5edc63f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/bin/grin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,14 @@ fn wallet_command(wallet_args: &ArgMatches, global_config: GlobalConfig) {
amount_to_hr_string(available),
);
}
Err(wallet::Error::FeeExceedsAmount {sender_amount, recipient_fee}) => {
error!(
LOGGER,
"Recipient rejected the transfer because transaction fee ({}) exceeded amount ({}).",
amount_to_hr_string(recipient_fee),
amount_to_hr_string(sender_amount)
);
}
Err(e) => {
error!(LOGGER, "Tx not sent: {:?}", e);
}
Expand Down
28 changes: 27 additions & 1 deletion wallet/src/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use serde_json;

use api;
use core::consensus::reward;
use core::core::{build, Block, Output, Transaction, TxKernel};
use core::core::{build, Block, Output, Transaction, TxKernel, amount_to_hr_string};
use core::ser;
use keychain::{Identifier, Keychain};
use types::*;
Expand Down Expand Up @@ -66,6 +66,19 @@ fn handle_sender_initiation(
});
}

if fee > amount {
info!(
LOGGER,
"Rejected the transfer because transaction fee ({}) exceeds received amount ({}).",
amount_to_hr_string(fee),
amount_to_hr_string(amount)
);
return Err(Error::FeeExceedsAmount {
sender_amount: amount,
recipient_fee: fee,
});
}

let out_amount = amount - fee;

//First step is just to get the excess sum of the outputs we're participating in
Expand Down Expand Up @@ -310,6 +323,19 @@ fn build_final_transaction(
});
}

if fee > amount {
info!(
LOGGER,
"Rejected the transfer because transaction fee ({}) exceeds received amount ({}).",
amount_to_hr_string(fee),
amount_to_hr_string(amount)
);
return Err(Error::FeeExceedsAmount {
sender_amount: amount,
recipient_fee: fee,
});
}

let out_amount = amount - fee;

// Get output we created in earlier step
Expand Down
24 changes: 21 additions & 3 deletions wallet/src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use api;
use client;
use checker;
use core::core::{build, Transaction};
use core::core::{build, Transaction, amount_to_hr_string};
use core::ser;
use keychain::{BlindingFactor, Identifier, Keychain};
use receiver::TxWrapper;
Expand Down Expand Up @@ -97,7 +97,16 @@ pub fn issue_send_tx(
debug!(LOGGER, "Posting partial transaction to {}", url);
let res = client::send_partial_tx(&url, &partial_tx);
if let Err(e) = res {
error!(LOGGER, "Communication with receiver failed on SenderInitiation send. Aborting transaction");
match e {
Error::FeeExceedsAmount {sender_amount, recipient_fee} =>
error!(
LOGGER,
"Recipient rejected the transfer because transaction fee ({}) exceeded amount ({}).",
amount_to_hr_string(recipient_fee),
amount_to_hr_string(sender_amount)
),
_ => error!(LOGGER, "Communication with receiver failed on SenderInitiation send. Aborting transaction"),
}
rollback_wallet()?;
return Err(e);
}
Expand All @@ -124,7 +133,16 @@ pub fn issue_send_tx(
// And send again
let res = client::send_partial_tx(&url, &partial_tx);
if let Err(e) = res {
error!(LOGGER, "Communication with receiver failed on SenderConfirmation send. Aborting transaction");
match e {
Error::FeeExceedsAmount {sender_amount, recipient_fee} =>
error!(
LOGGER,
"Recipient rejected the transfer because transaction fee ({}) exceeded amount ({}).",
amount_to_hr_string(recipient_fee),
amount_to_hr_string(sender_amount)
),
_ => error!(LOGGER, "Communication with receiver failed on SenderConfirmation send. Aborting transaction"),
}
rollback_wallet()?;
return Err(e);
}
Expand Down
1 change: 1 addition & 0 deletions wallet/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub fn tx_fee(input_len: usize, output_len: usize, base_fee: Option<u64>) -> u64
pub enum Error {
NotEnoughFunds(u64),
FeeDispute { sender_fee: u64, recipient_fee: u64 },
FeeExceedsAmount { sender_amount: u64, recipient_fee: u64 },
Keychain(keychain::Error),
Transaction(transaction::Error),
Secp(secp::Error),
Expand Down

0 comments on commit 5edc63f

Please sign in to comment.