Skip to content
Open
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
20 changes: 9 additions & 11 deletions lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import '../../crypto_currency/crypto_currency.dart';
import '../../models/tx_data.dart';
import '../intermediate/bip39_wallet.dart';
import '../supporting/mimblewimblecoin_wallet_info_extension.dart';
import '../supporting/mimblewimblecoin_wallet_math.dart';
import '../supporting/restore_progress.dart';

class MimblewimblecoinWallet extends Bip39Wallet {
Expand Down Expand Up @@ -614,9 +615,15 @@ class MimblewimblecoinWallet extends Bip39Wallet {
int satoshiAmount, {
bool ifErrorEstimateFee = false,
}) async {
final int available = info.cachedBalance.spendable.raw.toInt();
// With nothing to spend, libmwc treats amount == available as send all,
// retries with amount minus the required fee and aborts the app on the
// negative amount. There is no fee to estimate, so skip the call.
if (shouldSkipMwcFeeEstimate(amount: satoshiAmount, available: available)) {
return 0;
}
final wallet = await _ensureWalletOpen();
try {
final available = info.cachedBalance.spendable.raw.toInt();
final transactionFees = await libMwc.getTransactionFees(
wallet: wallet,
amount: satoshiAmount,
Expand Down Expand Up @@ -837,16 +844,7 @@ class MimblewimblecoinWallet extends Bip39Wallet {
}

int _calculateRestoreHeightFrom({required DateTime date}) {
final int secondsSinceEpoch = date.millisecondsSinceEpoch ~/ 1000;
const int mimblewimblecoinFirstBlock = 1573462800;
const double overestimateSecondsPerBlock = 61;
final int chosenSeconds = secondsSinceEpoch - mimblewimblecoinFirstBlock;
final int approximateHeight = chosenSeconds ~/ overestimateSecondsPerBlock;
int height = approximateHeight;
if (height < 0) {
height = 0;
}
return height;
return estimateMwcRestoreHeight(date);
}

// ============== Overrides ==================================================
Expand Down
11 changes: 11 additions & 0 deletions lib/wallets/wallet/supporting/mimblewimblecoin_wallet_math.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
bool shouldSkipMwcFeeEstimate({required int amount, required int available}) =>
amount <= 0 || available <= 0;

int estimateMwcRestoreHeight(DateTime date) {
final int secondsSinceEpoch = date.millisecondsSinceEpoch ~/ 1000;
const int mimblewimblecoinFirstBlock = 1573462801;
const double overestimateSecondsPerBlock = 61;
final int chosenSeconds = secondsSinceEpoch - mimblewimblecoinFirstBlock;
final int approximateHeight = chosenSeconds ~/ overestimateSecondsPerBlock;
return approximateHeight < 0 ? 0 : approximateHeight;
}
33 changes: 33 additions & 0 deletions test/wallets/mimblewimblecoin_wallet_math_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:stackwallet/wallets/wallet/supporting/mimblewimblecoin_wallet_math.dart';

void main() {
test("MWC fee estimation is skipped only for unusable inputs", () {
expect(shouldSkipMwcFeeEstimate(amount: 0, available: 1), isTrue);
expect(shouldSkipMwcFeeEstimate(amount: -1, available: 1), isTrue);
expect(shouldSkipMwcFeeEstimate(amount: 1, available: 0), isTrue);
expect(shouldSkipMwcFeeEstimate(amount: 1, available: -1), isTrue);
expect(shouldSkipMwcFeeEstimate(amount: 1, available: 1), isFalse);
});

test("MWC restore height uses its exact first-block timestamp", () {
final genesis = DateTime.fromMillisecondsSinceEpoch(
1573462801 * 1000,
isUtc: true,
);

expect(estimateMwcRestoreHeight(genesis), 0);
expect(
estimateMwcRestoreHeight(genesis.add(const Duration(seconds: 60))),
0,
);
expect(
estimateMwcRestoreHeight(genesis.add(const Duration(seconds: 61))),
1,
);
expect(
estimateMwcRestoreHeight(genesis.subtract(const Duration(days: 1))),
0,
);
});
}
Loading