Use a visitor for handling -mineraddress config option

This makes it easier to add UA support.
This commit is contained in:
Jack Grigg 2021-06-30 11:20:59 +01:00
parent 22ffee8da6
commit 990bc46b2b
3 changed files with 26 additions and 10 deletions

View File

@ -1093,10 +1093,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
if (mapArgs.count("-mineraddress")) {
CTxDestination addr = keyIO.DecodeDestination(mapArgs["-mineraddress"]);
if (!IsValidDestination(addr)) {
// Try a Sapling address
// Try a payment address
auto zaddr = keyIO.DecodePaymentAddress(mapArgs["-mineraddress"]);
if (!IsValidPaymentAddress(zaddr) ||
std::get_if<libzcash::SaplingPaymentAddress>(&zaddr) == nullptr)
if (!std::visit(IsValidMinerAddress(), std::visit(ExtractMinerAddress(), zaddr)))
{
return InitError(strprintf(
_("Invalid address for -mineraddress=<addr>: '%s' (must be a Sapling or transparent address)"),

View File

@ -696,12 +696,10 @@ void GetMinerAddress(MinerAddress &minerAddress)
mAddr->reserveScript = CScript() << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
minerAddress = mAddr;
} else {
// Try a Sapling address
auto zaddr = keyIO.DecodePaymentAddress(mAddrArg);
if (IsValidPaymentAddress(zaddr)) {
if (std::get_if<libzcash::SaplingPaymentAddress>(&zaddr) != nullptr) {
minerAddress = std::get<libzcash::SaplingPaymentAddress>(zaddr);
}
// Try a payment address
auto zaddr = std::visit(ExtractMinerAddress(), keyIO.DecodePaymentAddress(mAddrArg));
if (std::visit(IsValidMinerAddress(), zaddr)) {
minerAddress = zaddr;
}
}
}

View File

@ -29,7 +29,26 @@ public:
friend bool operator<(const InvalidMinerAddress &a, const InvalidMinerAddress &b) { return true; }
};
typedef std::variant<InvalidMinerAddress, libzcash::SaplingPaymentAddress, boost::shared_ptr<CReserveScript>> MinerAddress;
typedef std::variant<
InvalidMinerAddress,
libzcash::SaplingPaymentAddress,
boost::shared_ptr<CReserveScript>> MinerAddress;
class ExtractMinerAddress
{
public:
ExtractMinerAddress() {}
MinerAddress operator()(const libzcash::InvalidEncoding &invalid) const {
return InvalidMinerAddress();
}
MinerAddress operator()(const libzcash::SproutPaymentAddress &addr) const {
return InvalidMinerAddress();
}
MinerAddress operator()(const libzcash::SaplingPaymentAddress &addr) const {
return addr;
}
};
class KeepMinerAddress
{