Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wallet] panic: 'attempt to subtract with overflow' when sending small quantities #396 #603

Merged
merged 3 commits into from
Jan 12, 2018
Merged
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
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