Skip to content

Commit

Permalink
rebase 5.3.X [PATCH 070/142] fixfees on grin-wallet (mimblewimble#526)
Browse files Browse the repository at this point in the history
Note: MWC implementation is more simple, because sync with updated node was done before and many issues was addressed at that time. Also, the default base fee is not changed because for MWC we don't change the calculation fee formula.
  • Loading branch information
bayk committed Aug 1, 2024
1 parent 589b393 commit 0a46c31
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
8 changes: 7 additions & 1 deletion config/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ fn comments() -> HashMap<String, String> {
"
.to_string(),
);

retval.insert(
"accept_fee_base".to_string(),
"
#Minimum acceptable fee per unit of transaction weight
"
.to_string(),
);
retval.insert(
"[logging]".to_string(),
"
Expand Down
15 changes: 15 additions & 0 deletions config/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ pub struct WalletConfig {
/// Key: <coin>_[main|test]_[1|2]
/// Value: url
pub swap_electrumx_addr: Option<BTreeMap<String, String>>,
/// Scaling factor from transaction weight to transaction fee
/// should match accept_fee_base parameter in grin-server
pub accept_fee_base: Option<u64>,
}

impl Default for WalletConfig {
Expand Down Expand Up @@ -138,6 +141,7 @@ impl Default for WalletConfig {
.map(|i| (i.0.to_string(), i.1.to_string()))
.collect::<BTreeMap<String, String>>(),
),
accept_fee_base: None,
}
}
}
Expand All @@ -153,6 +157,11 @@ impl WalletConfig {
3420
}

/// Default TX base fee, same that we have now (1 milli MWC)
pub fn default_accept_fee_base() -> u64 {
1_000_000
}

/// Use value from config file, defaulting to sensible value if missing.
pub fn owner_api_listen_port(&self) -> u16 {
self.owner_api_listen_port
Expand All @@ -170,6 +179,12 @@ impl WalletConfig {
.clone()
.unwrap_or(GRIN_WALLET_DIR.to_string())
}

/// Accept fee base
pub fn accept_fee_base(&self) -> u64 {
self.accept_fee_base
.unwrap_or_else(|| WalletConfig::default_accept_fee_base())
}
}

/// Error type wrapping config errors.
Expand Down
3 changes: 3 additions & 0 deletions controller/tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ fn basic_transaction_api(test_dir: &'static str) -> Result<(), wallet::Error> {
slate = sender_api.finalize_tx(m, &slate)?;

// 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) (2 * 4 + 1 - 7)*1m = 2m = 2000000
assert_eq!(slate.tx_or_err()?.kernels().len(), 1);
assert_eq!(
slate
Expand Down Expand Up @@ -272,6 +273,7 @@ fn basic_transaction_api(test_dir: &'static str) -> Result<(), wallet::Error> {
};
let est = sender_api.init_send_tx(m, &init_args, 1)?;
assert_eq!(est.amount, 10 * core::consensus::MWC_FIRST_GROUP_REWARD);
// fees for 5 inputs, 2 outputs, 1 kernel 2*4 + 1 - 5 = 4m
assert_eq!(est.fee, 4_000_000);

let init_args = InitTxArgs {
Expand All @@ -286,6 +288,7 @@ fn basic_transaction_api(test_dir: &'static str) -> Result<(), wallet::Error> {
};
let est = sender_api.init_send_tx(m, &init_args, 1)?;
assert_eq!(est.amount, core::consensus::MWC_FIRST_GROUP_REWARD * 3);
// fees for 3 inputs, 2 outputs, 1 kernel 2*4+1-3 = 6m
assert_eq!(est.fee, 6_000_000);

Ok(())
Expand Down
5 changes: 3 additions & 2 deletions impls/src/test_framework/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ fn create_block_with_reward(
reward_output: Output,
reward_kernel: TxKernel,
) -> core::core::Block {
let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter().unwrap());
let next_header_info =
consensus::next_difficulty(prev.height + 1, chain.difficulty_iter().unwrap());
let mut b = core::core::Block::new(
&prev,
txs,
Expand Down Expand Up @@ -173,7 +174,7 @@ where
K: keychain::Keychain + 'a,
{
// build block fees
let fee_amt = txs.iter().map(|tx| tx.fee(prev.height)).sum();
let fee_amt = txs.iter().map(|tx| tx.fee(prev.height + 1)).sum();
let block_fees = BlockFees {
fees: fee_amt,
key_id: None,
Expand Down
2 changes: 2 additions & 0 deletions src/bin/grin-wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ fn real_main() -> i32 {
.clone(),
);

global::init_global_accept_fee_base(config.members.as_ref().unwrap().wallet.accept_fee_base());

let wallet_config = config.clone().members.unwrap().wallet;

if let Some(base_fee) = &wallet_config.base_fee {
Expand Down

0 comments on commit 0a46c31

Please sign in to comment.