fixfees on grin-wallet (#526)

* change fee to fee_fields in various structs and fix tests for new fee calculation

* set up accept_fee_base param in grin-wallet.toml

* fix bug and adopt new tx_fee interface

* Fix doctests

* Remove left over debug statement

* revert field name feef to fee to remain slate compatible

* use into() for some literal fee_fields

* add height arguments to FeeFields methods where necessary

* Fix tests

* Rename fee_fields -> fee on Context

* Rename fee_fields -> fee on TxLogEntry

* Update to grin master

* replace fee(0) by fee(2*YEAR_HEIGHT) to get fee masking

* Fix compilation error and revert Cargo lock

* fix wrong height passed into next_difficulty

Co-authored-by: Jasper van der Maarel <j@sper.dev>
This commit is contained in:
John Tromp
2020-11-26 20:46:03 +01:00
committed by GitHub
co-authored by Jasper van der Maarel
parent 429db61bb7
commit 8334304c16
21 changed files with 196 additions and 140 deletions
+4 -3
View File
@@ -17,6 +17,7 @@
use crate::api::TLSConfig;
use crate::apiwallet::{try_slatepack_sync_workflow, Owner};
use crate::config::{TorConfig, WalletConfig, WALLET_CONFIG_FILE_NAME};
use crate::core::core::FeeFields;
use crate::core::{core, global};
use crate::error::{Error, ErrorKind};
use crate::impls::PathToSlatepack;
@@ -277,7 +278,7 @@ where
let mut slate = Slate::blank(2, false);
controller::owner_single_use(None, keychain_mask, Some(owner_api), |api, m| {
if args.estimate_selection_strategies {
let strategies = vec!["smallest", "all"]
let strategies: Vec<(&str, u64, FeeFields)> = vec!["smallest", "all"]
.into_iter()
.map(|strategy| {
let init_args = InitTxArgs {
@@ -291,7 +292,7 @@ where
..Default::default()
};
let slate = api.init_send_tx(m, init_args).unwrap();
(strategy, slate.amount, slate.fee)
(strategy, slate.amount, slate.fee_fields)
})
.collect();
display::estimate(args.amount, strategies, dark_scheme);
@@ -848,7 +849,7 @@ where
..Default::default()
};
let slate = api.init_send_tx(m, init_args).unwrap();
(strategy, slate.amount, slate.fee)
(strategy, slate.amount, slate.fee_fields)
})
.collect();
display::estimate(slate.amount, strategies, dark_scheme);
+10 -8
View File
@@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::core::consensus::YEAR_HEIGHT;
use crate::core::core::FeeFields;
use crate::core::core::{self, amount_to_hr_string};
use crate::core::global;
use crate::libwallet::{
@@ -189,7 +191,7 @@ pub fn txs(
let amount_debited_str = core::amount_to_hr_string(t.amount_debited, true);
let amount_credited_str = core::amount_to_hr_string(t.amount_credited, true);
let fee = match t.fee {
Some(f) => format!("{}", core::amount_to_hr_string(f, true)),
Some(f) => format!("{}", core::amount_to_hr_string(f.fee(cur_height), true)),
None => "None".to_owned(),
};
let net_diff = if t.amount_credited >= t.amount_debited {
@@ -393,9 +395,9 @@ pub fn info(
pub fn estimate(
amount: u64,
strategies: Vec<(
&str, // strategy
u64, // total amount to be locked
u64, // fee
&str, // strategy
u64, // total amount to be locked
FeeFields, // fee
)>,
dark_background_color_scheme: bool,
) {
@@ -412,17 +414,17 @@ pub fn estimate(
bMG->"Will be locked",
]);
for (strategy, total, fee) in strategies {
for (strategy, total, fee_fields) in strategies {
if dark_background_color_scheme {
table.add_row(row![
bFC->strategy,
FR->amount_to_hr_string(fee, false),
FR->amount_to_hr_string(fee_fields.fee(2*YEAR_HEIGHT), false), // apply fee mask past HF4
FY->amount_to_hr_string(total, false),
]);
} else {
table.add_row(row![
bFD->strategy,
FR->amount_to_hr_string(fee, false),
FR->amount_to_hr_string(fee_fields.fee(2*YEAR_HEIGHT), false), // apply fee mask past HF4
FY->amount_to_hr_string(total, false),
]);
}
@@ -484,7 +486,7 @@ pub fn payment_proof(tx: &TxLogEntry) -> Result<(), Error> {
None => "None".to_owned(),
};
let fee = match tx.fee {
Some(f) => f,
Some(f) => f.fee(2 * YEAR_HEIGHT), // apply fee mask past HF4
None => 0,
};
let amount = if tx.amount_credited >= tx.amount_debited {
+1 -1
View File
@@ -73,7 +73,7 @@ fn no_change_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// Mine into wallet 1
let _ = test_framework::award_blocks_to_wallet(&chain, wallet1.clone(), mask1, 4, false);
let fee = core::libtx::tx_fee(1, 1, 1, None);
let fee = core::libtx::tx_fee(1, 1, 1);
// send a single block's worth of transactions with minimal strategy
let mut slate = Slate::blank(2, false);
+10 -6
View File
@@ -24,6 +24,7 @@ use self::core::core::transaction;
use self::core::global;
use self::libwallet::{InitTxArgs, OutputStatus, Slate, SlateState};
use impls::test_framework::{self, LocalWalletClient};
use std::convert::TryInto;
use std::sync::atomic::Ordering;
use std::thread;
use std::time::Duration;
@@ -124,6 +125,7 @@ fn basic_transaction_api(test_dir: &'static str) -> Result<(), libwallet::Error>
assert_eq!(slate.state, SlateState::Standard3);
// Check we have a single kernel and that it is a Plain kernel (no lock_height).
// fees for 7 inputs, 2 outputs, 1 kernel (weight 52)
assert_eq!(slate.tx_or_err()?.kernels().len(), 1);
assert_eq!(
slate
@@ -132,7 +134,9 @@ fn basic_transaction_api(test_dir: &'static str) -> Result<(), libwallet::Error>
.first()
.map(|k| k.features)
.unwrap(),
transaction::KernelFeatures::Plain { fee: 2000000 }
transaction::KernelFeatures::Plain {
fee: 26_000_000.into()
}
);
Ok(())
@@ -147,7 +151,6 @@ fn basic_transaction_api(test_dir: &'static str) -> Result<(), libwallet::Error>
wallet1_info.last_confirmed_height as usize - cm as usize,
2,
1,
None,
);
// we should have a transaction entry for this slate
let tx = txs.iter().find(|t| t.tx_slate_id == Some(slate.id));
@@ -157,7 +160,7 @@ fn basic_transaction_api(test_dir: &'static str) -> Result<(), libwallet::Error>
assert!(tx.confirmation_ts.is_none());
assert_eq!(tx.amount_debited - tx.amount_credited, fee + amount);
println!("tx: {:?}", tx);
assert_eq!(Some(fee), tx.fee);
assert_eq!(Some(fee.try_into().unwrap()), tx.fee);
Ok(())
})?;
@@ -194,7 +197,6 @@ fn basic_transaction_api(test_dir: &'static str) -> Result<(), libwallet::Error>
wallet1_info.last_confirmed_height as usize - 1 - cm as usize,
2,
1,
None,
);
assert!(wallet1_refreshed);
// wallet 1 received fees, so amount should be the same
@@ -269,7 +271,8 @@ fn basic_transaction_api(test_dir: &'static str) -> Result<(), libwallet::Error>
};
let est = sender_api.init_send_tx(m, init_args)?;
assert_eq!(est.amount, 600_000_000_000);
assert_eq!(est.fee, 4_000_000);
// fees for 5 inputs, 2 outputs, 1 kernel (weight 50)
assert_eq!(est.fee_fields.fee(0), 25_000_000);
let init_args = InitTxArgs {
src_acct_name: None,
@@ -283,7 +286,8 @@ fn basic_transaction_api(test_dir: &'static str) -> Result<(), libwallet::Error>
};
let est = sender_api.init_send_tx(m, init_args)?;
assert_eq!(est.amount, 180_000_000_000);
assert_eq!(est.fee, 6_000_000);
// fees for 3 inputs, 2 outputs, 1 kernel (weight 48)
assert_eq!(est.fee_fields.fee(0), 24_000_000);
Ok(())
})?;