-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Bitcoin]: UTXO selection and dust improvements (#3675)
* [Bitcoin]: Add Fixed Dust threshold to SigningInput * Filter out Dust UTXOs before actual UTXO selecting * [Bitcoin]: Check the Change output to be at least Dust threshold * [Bitcoin]: Add Fixed Dust threshold to SigningInput * Filter out Dust UTXOs before actual UTXO selecting * [Bitcoin]: Fix iOS test * [Bitcoin]: Fix comments * [Bitcoin]: Update KMP WC * [Bitcoin]: Return Error_not_enough_utxos if max amount requested, but total amount is dust
- Loading branch information
1 parent
f8ccbc9
commit 7c71119
Showing
15 changed files
with
552 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Copyright © 2017 Trust Wallet. | ||
|
||
#include "DustCalculator.h" | ||
|
||
namespace TW::Bitcoin { | ||
|
||
FixedDustCalculator::FixedDustCalculator(Amount fixed) noexcept | ||
: fixedDustAmount(fixed) { | ||
} | ||
|
||
Amount FixedDustCalculator::dustAmount([[maybe_unused]] Amount byteFee) noexcept { | ||
return fixedDustAmount; | ||
} | ||
|
||
LegacyDustCalculator::LegacyDustCalculator(TWCoinType coinType) noexcept | ||
: feeCalculator(getFeeCalculator(coinType, false)) { | ||
} | ||
|
||
Amount LegacyDustCalculator::dustAmount([[maybe_unused]] Amount byteFee) noexcept { | ||
return feeCalculator.calculateSingleInput(byteFee); | ||
} | ||
|
||
DustCalculatorShared getDustCalculator(const Proto::SigningInput& input) { | ||
if (input.disable_dust_filter()) { | ||
return std::make_shared<FixedDustCalculator>(0); | ||
} | ||
|
||
if (input.has_fixed_dust_threshold()) { | ||
return std::make_shared<FixedDustCalculator>(input.fixed_dust_threshold()); | ||
} | ||
|
||
const auto coinType = static_cast<TWCoinType>(input.coin_type()); | ||
return std::make_shared<LegacyDustCalculator>(coinType); | ||
} | ||
|
||
} // namespace TW::Bitcoin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Copyright © 2017 Trust Wallet. | ||
|
||
#pragma once | ||
|
||
#include "Amount.h" | ||
#include "FeeCalculator.h" | ||
#include "proto/Bitcoin.pb.h" | ||
|
||
#include <memory> | ||
#include <TrustWalletCore/TWCoinType.h> | ||
|
||
namespace TW::Bitcoin { | ||
|
||
/// Interface for transaction dust amount calculator. | ||
struct DustCalculator { | ||
virtual ~DustCalculator() noexcept = default; | ||
|
||
/// Returns a Dust threshold of a transaction UTXO or output. | ||
virtual Amount dustAmount(Amount byteFee) noexcept = 0; | ||
}; | ||
|
||
/// Always returns a fixed Dust amount specified in the signing request. | ||
class FixedDustCalculator final: public DustCalculator { | ||
public: | ||
explicit FixedDustCalculator(Amount fixed) noexcept; | ||
|
||
Amount dustAmount([[maybe_unused]] Amount byteFee) noexcept override; | ||
|
||
private: | ||
Amount fixedDustAmount {0}; | ||
}; | ||
|
||
/// Legacy Dust filter implementation using [`FeeCalculator::calculateSingleInput`]. | ||
/// Depends on a coin type, sats/Byte fee. | ||
class LegacyDustCalculator final: public DustCalculator { | ||
public: | ||
explicit LegacyDustCalculator(TWCoinType coinType) noexcept; | ||
|
||
Amount dustAmount(Amount byteFee) noexcept override; | ||
|
||
private: | ||
const FeeCalculator& feeCalculator; | ||
}; | ||
|
||
using DustCalculatorShared = std::shared_ptr<DustCalculator>; | ||
|
||
DustCalculatorShared getDustCalculator(const Proto::SigningInput& input); | ||
|
||
} // namespace TW::Bitcoin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.