You might be a king or a little street zsweeper, but sooner or later you will dance with the reaper

This commit is contained in:
Duke Leto
2022-08-24 23:38:19 -04:00
parent afd3f93e2e
commit c6e5b07a59
22 changed files with 146 additions and 9 deletions

View File

@@ -231,6 +231,7 @@ BITCOIN_CORE_H = \
version.h \
wallet/asyncrpcoperation_mergetoaddress.h \
wallet/asyncrpcoperation_saplingconsolidation.h \
wallet/asyncrpcoperation_sweep.h \
wallet/asyncrpcoperation_sendmany.h \
wallet/asyncrpcoperation_shieldcoinbase.h \
wallet/crypter.h \
@@ -344,6 +345,7 @@ libbitcoin_wallet_a_SOURCES = \
zcbenchmarks.h \
wallet/asyncrpcoperation_mergetoaddress.cpp \
wallet/asyncrpcoperation_saplingconsolidation.cpp \
wallet/asyncrpcoperation_sweep.cpp \
wallet/asyncrpcoperation_sendmany.cpp \
wallet/asyncrpcoperation_shieldcoinbase.cpp \
wallet/crypter.cpp \

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2021 The Hush developers
// Copyright (c) 2014 The Bitcoin Core developers
// Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2021 The Hush developers
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the GPLv3 software license, see the accompanying

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2022 The Hush developers
// Copyright (c) 2018 The Zcash developers
// Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
@@ -17,8 +18,8 @@
* *
******************************************************************************/
#ifndef ZCASH_CONSENSUS_UPGRADES_H
#define ZCASH_CONSENSUS_UPGRADES_H
#ifndef HUSH_CONSENSUS_UPGRADES_H
#define HUSH_CONSENSUS_UPGRADES_H
#include "consensus/params.h"
@@ -114,4 +115,4 @@ boost::optional<int> NextActivationHeight(
int nHeight,
const Consensus::Params& params);
#endif // ZCASH_CONSENSUS_UPGRADES_H
#endif // HUSH_CONSENSUS_UPGRADES_H

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2022 The Hush developers
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the GPLv3 software license, see the accompanying

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2021 The Hush developers
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2021 The Hush developers
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2021 The Hush developers
#include "fs.h"
namespace fsbridge {

View File

@@ -1,3 +1,6 @@
// Copyright (c) 2016-2021 The Hush developers
// Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
/******************************************************************************
* Copyright © 2014-2019 The SuperNET Developers. *
* *

View File

@@ -59,6 +59,7 @@
#include "wallet/wallet.h"
#include "wallet/walletdb.h"
#include "wallet/asyncrpcoperation_saplingconsolidation.h"
#include "wallet/asyncrpcoperation_sweep.h"
#endif
#include <stdint.h>
#include <stdio.h>
@@ -461,6 +462,16 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += HelpMessageOpt("-consolidation", _("Enable auto Sapling note consolidation (default: false)"));
strUsage += HelpMessageOpt("-consolidatesaplingaddress=<zaddr>", _("Specify Sapling Address to Consolidate. (default: all)"));
strUsage += HelpMessageOpt("-consolidationtxfee", strprintf(_("Fee amount in Puposhis used send consolidation transactions. (default %i)"), DEFAULT_CONSOLIDATION_FEE));
strUsage += HelpMessageOpt("-zsweep", _("Enable zaddr sweeping, automatically move all shielded funds to a one address once per X blocks"));
strUsage += HelpMessageOpt("-zsweepaddress=<zaddr>", _("Specify the shielded address where swept funds will be sent)"));
strUsage += HelpMessageOpt("-zsweepfee", strprintf(_("Fee amount in puposhis used send sweep transactions. (default %i)"), DEFAULT_SWEEP_FEE));
strUsage += HelpMessageOpt("-zsweepinterval", strprintf(_("Sweep shielded funds every X blocks (default %i)"), 5));
strUsage += HelpMessageOpt("-zsweepmaxinputs", strprintf(_("Maximum number of shielded inputs to sweep per transaction (default %i)"), 8));
// By default we only allow sweeping to the current wallet which must have the spending key of the sweep zaddr
// This hopefully will make it harder for people to accidentally sweep funds to a wrong zaddr and lose funds
strUsage += HelpMessageOpt("-zsweepexternal", _("Enable sweeping to an external wallet (default false)"));
strUsage += HelpMessageOpt("-deletetx", _("Enable Old Transaction Deletion"));
strUsage += HelpMessageOpt("-deleteinterval", strprintf(_("Delete transaction every <n> blocks during inital block download (default: %i)"), DEFAULT_TX_DELETE_INTERVAL));
strUsage += HelpMessageOpt("-keeptxnum", strprintf(_("Keep the last <n> transactions (default: %i)"), DEFAULT_TX_RETENTION_LASTTX));
@@ -2054,6 +2065,53 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
}
}
//Set Sweep
pwalletMain->fSweepEnabled = GetBoolArg("-zsweep", false);
if (pwalletMain->fSweepEnabled) {
fSweepTxFee = GetArg("-zsweepfee", DEFAULT_SWEEP_FEE);
fSweepMapUsed = !mapMultiArgs["-zsweepaddress"].empty();
//Validate Sapling Addresses
vector<string>& vSweep = mapMultiArgs["-zsweepaddress"];
if (vSweep.size() != 1) {
return InitError("A single zsweep address must be specified.");
}
for (int i = 0; i < vSweep.size(); i++) {
// LogPrintf("Sweep Address: %s\n", vSweep[i]);
auto zSweep = DecodePaymentAddress(vSweep[i]);
if (!IsValidPaymentAddress(zSweep)) {
return InitError("Invalid zsweep address");
}
auto hasSpendingKey = boost::apply_visitor(HaveSpendingKeyForPaymentAddress(pwalletMain), zSweep);
auto allowSweepToExternalWallet = GetArg("-zsweepexternal", false);
if (!hasSpendingKey) {
if (allowSweepToExternalWallet) {
LogPrintf("%s: sweeping funds to a zaddr in an external wallet\n", __func__);
} else {
return InitError("Wallet must have the spending key of zsweep address");
}
}
}
if (pwalletMain->fSaplingConsolidationEnabled) {
//Validate 1 Consolidation address only that matches the sweep address
vector<string>& vaddresses = mapMultiArgs["-consolidatesaplingaddress"];
if (vaddresses.size() == 0) {
fConsolidationMapUsed = true;
mapMultiArgs["-consolidatesaplingaddress"] = vSweep;
} else {
for (int i = 0; i < vaddresses.size(); i++) {
if (vSweep[0] != vaddresses[i]) {
return InitError("Consolidation can only be used on the sweep address when sweep is enabled.");
}
}
}
}
}
//Set Transaction Deletion Options
fTxDeleteEnabled = GetBoolArg("-deletetx", false);
fTxConflictDeleteEnabled = GetBoolArg("-deleteconflicttx", true);

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2021 The Hush developers
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Copyright (c) 2017 The Zcash developers
// Distributed under the GPLv3 software license, see the accompanying

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2021 The Hush developers
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the GPLv3 software license, see the accompanying

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2021 The Hush developers
// Copyright (c) 2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the GPLv3 software license, see the accompanying

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2021 The Hush developers
// Copyright (c) 2015 The Bitcoin Core developers
// Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html

View File

@@ -104,6 +104,11 @@ bool TransactionBuilder::AddOpRetLast()
return true;
}
void TransactionBuilder::SetExpiryHeight(int nHeight)
{
this->mtx.nExpiryHeight = nHeight;
}
void TransactionBuilder::AddOpRet(CScript &s)
{
opReturn.emplace(CScript(s));

View File

@@ -77,6 +77,7 @@ public:
TransactionBuilder(const Consensus::Params& consensusParams, int nHeight, CKeyStore* keyStore = nullptr);
void SetFee(CAmount fee);
void SetExpiryHeight(int nHeight);
// Returns false if the anchor does not match the anchor used by
// previously-added Sapling spends.

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2021 The Hush developers
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the GPLv3 software license, see the accompanying
@@ -19,9 +20,7 @@
******************************************************************************/
#include "uint256.h"
#include "utilstrencodings.h"
#include <stdio.h>
#include <string.h>

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2021 The Hush developers
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the GPLv3 software license, see the accompanying

View File

@@ -1,3 +1,4 @@
// Copyright (c) 2016-2021 The Hush developers
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the GPLv3 software license, see the accompanying

View File

@@ -159,7 +159,7 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() {
amountConsolidated += amountToSend;
auto builder = TransactionBuilder(consensusParams, targetHeight_, pwalletMain);
//builder.SetExpiryHeight(targetHeight_ + CONSOLIDATION_EXPIRY_DELTA);
builder.SetExpiryHeight(targetHeight_ + CONSOLIDATION_EXPIRY_DELTA);
auto actualAmountToSend = amountToSend < fConsolidationTxFee ? 0 : amountToSend - fConsolidationTxFee;
LogPrintf("%s: %s Beginning to create transaction with Sapling output amount=%s\n", __func__, opid, FormatMoney(actualAmountToSend));
@@ -235,7 +235,7 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() {
break;
}
if(pwalletMain->CommitConsolidationTx(tx)) {
if(pwalletMain->CommitAutomatedTx(tx)) {
LogPrint("zrpcunsafe", "%s: Committed consolidation transaction with txid=%s\n",opid, tx.GetHash().ToString());
amountConsolidated += actualAmountToSend;
consolidationTxIds.push_back(tx.GetHash().ToString());

View File

@@ -38,6 +38,7 @@
#include "crypter.h"
#include "coins.h"
#include "wallet/asyncrpcoperation_saplingconsolidation.h"
#include "wallet/asyncrpcoperation_sweep.h"
#include "zcash/zip32.h"
#include "cc/CCinclude.h"
#include <assert.h>
@@ -483,6 +484,9 @@ void CWallet::ChainTip(const CBlockIndex *pindex,
if (fSaplingConsolidationEnabled) {
RunSaplingConsolidation(pindex->GetHeight());
}
if (fSweepEnabled) {
RunSaplingSweep(pindex->GetHeight());
}
if (fTxDeleteEnabled) {
DeleteWalletTransactions(pindex);
}
@@ -501,6 +505,43 @@ void CWallet::ChainTip(const CBlockIndex *pindex,
}
}
void CWallet::RunSaplingSweep(int blockHeight) {
// Sapling is always active since height=1 of HUSH+HSCs
// if (!NetworkUpgradeActive(blockHeight, Params().GetConsensus(), Consensus::UPGRADE_SAPLING)) {
// return;
// }
AssertLockHeld(cs_wallet);
if (!fSweepEnabled) {
return;
}
if (nextSweep > blockHeight) {
return;
}
//Don't Run if consolidation will run soon.
if (fSaplingConsolidationEnabled && nextConsolidation - 15 <= blockHeight) {
return;
}
//Don't Run While consolidation is running.
if (fConsolidationRunning) {
return;
}
fSweepRunning = true;
std::shared_ptr<AsyncRPCQueue> q = getAsyncRPCQueue();
std::shared_ptr<AsyncRPCOperation> lastOperation = q->getOperationForId(saplingSweepOperationId);
if (lastOperation != nullptr) {
lastOperation->cancel();
}
pendingSaplingSweepTxs.clear();
std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_sweep(blockHeight + 5));
saplingSweepOperationId = operation->getId();
q->addOperation(operation);
}
void CWallet::RunSaplingConsolidation(int blockHeight) {
if (!NetworkUpgradeActive(blockHeight, Params().GetConsensus(), Consensus::UPGRADE_SAPLING)) {
return;
@@ -512,6 +553,10 @@ void CWallet::RunSaplingConsolidation(int blockHeight) {
return;
}
if (fSweepRunning) {
return;
}
int consolidateInterval = rand() % 5 + 5;
if(fZdebug)
@@ -530,7 +575,7 @@ void CWallet::RunSaplingConsolidation(int blockHeight) {
}
}
bool CWallet::CommitConsolidationTx(const CTransaction& tx) {
bool CWallet::CommitAutomatedTx(const CTransaction& tx) {
CWalletTx wtx(this, tx);
CReserveKey reservekey(pwalletMain);
fprintf(stderr,"%s: %s\n",__func__,tx.ToString().c_str());

View File

@@ -785,6 +785,9 @@ private:
std::vector<CTransaction> pendingSaplingConsolidationTxs;
AsyncRPCOperationId saplingConsolidationOperationId;
std::vector<CTransaction> pendingSaplingSweepTxs;
AsyncRPCOperationId saplingSweepOperationId;
void AddToTransparentSpends(const COutPoint& outpoint, const uint256& wtxid);
void AddToSaplingSpends(const uint256& nullifier, const uint256& wtxid);
void AddToSpends(const uint256& wtxid);
@@ -797,7 +800,13 @@ public:
*/
int64_t nWitnessCacheSize;
bool needsRescan = false;
int nextConsolidation = 0;
bool fSaplingConsolidationEnabled = false;
bool fConsolidationRunning = false;
bool fSweepEnabled = false;
bool fSweepRunning = false;
int nextSweep = 0;
void ClearNoteWitnessCache();
@@ -1181,12 +1190,14 @@ public:
CAmount GetCredit(const CTransaction& tx, int32_t voutNum, const isminefilter& filter) const;
CAmount GetCredit(const CTransaction& tx, const isminefilter& filter) const;
CAmount GetChange(const CTransaction& tx) const;
void RunSaplingSweep(int blockHeight);
void ChainTip(
const CBlockIndex *pindex,
const CBlock *pblock,
boost::optional<std::pair<SproutMerkleTree, SaplingMerkleTree>> added);
void RunSaplingConsolidation(int blockHeight);
bool CommitConsolidationTx(const CTransaction& tx);
bool CommitAutomatedTx(const CTransaction& tx);
/** Saves witness caches and best block locator to disk. */
void SetBestChain(const CBlockLocator& loc);
std::set<std::pair<libzcash::PaymentAddress, uint256>> GetNullifiersForAddresses(const std::set<libzcash::PaymentAddress> & addresses);