Skip to content

Commit

Permalink
Refactor transaction creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
sproxet committed Nov 11, 2023
1 parent 92a3d42 commit be2ea19
Show file tree
Hide file tree
Showing 21 changed files with 2,137 additions and 408 deletions.
3 changes: 2 additions & 1 deletion src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ BITCOIN_TESTS += \
wallet/test/lelantus_tests.cpp \
wallet/test/sigma_tests.cpp \
wallet/test/mnemonic_tests.cpp \
wallet/test/txbuilder_tests.cpp
wallet/test/sigmatxbuilder_tests.cpp \
wallet/test/createtransaction_tests.cpp
endif

test_test_bitcoin_LDADD = $(LIBBITCOIN_SERVER) -ltor
Expand Down
2 changes: 1 addition & 1 deletion src/elysium/elysium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2291,7 +2291,7 @@ int elysium::WalletTxBuilder(
switch (inputMode) {
case InputMode::NORMAL:
// Ask the wallet to create the transaction (note mining fee determined by Bitcoin Core params)
if (!pwalletMain->CreateTransaction(vecRecipients, wtxNew, reserveKey, nFeeRet, nChangePosInOut, strFailReason, &coinControl)) {
if (!pwalletMain->CreateTransaction(vecRecipients, wtxNew, &reserveKey, nFeeRet, nChangePosInOut, strFailReason, &coinControl)) {
PrintToLog("%s: ERROR: wallet transaction creation failed: %s\n", __func__, strFailReason);
return MP_ERR_CREATE_TX;
}
Expand Down
6 changes: 3 additions & 3 deletions src/llmq/quorums_instantsend.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ class CInstantSendDb
class CInstantSendManager : public CRecoveredSigsListener
{
private:
CCriticalSection cs;
CInstantSendDb db;

std::thread workThread;
CThreadInterrupt workInterrupt;

Expand Down Expand Up @@ -113,6 +110,9 @@ class CInstantSendManager : public CRecoveredSigsListener
std::atomic_bool isNewInstantSendEnabled{false};

public:
CCriticalSection cs;
CInstantSendDb db;

CInstantSendManager(CDBWrapper& _llmqDb);
~CInstantSendManager();

Expand Down
2 changes: 1 addition & 1 deletion src/primitives/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class CTxOut
public:
CAmount nValue;
CScript scriptPubKey;
int nRounds;
int nRounds = -10;

CTxOut()
{
Expand Down
2 changes: 1 addition & 1 deletion src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact

CWalletTx *newTx = transaction.getTransaction();
CReserveKey *keyChange = transaction.getPossibleKeyChange();
bool fCreated = wallet->CreateTransaction(vecSend, *newTx, *keyChange, nFeeRequired, nChangePosRet, strFailReason, coinControl);
bool fCreated = wallet->CreateTransaction(vecSend, *newTx, keyChange, nFeeRequired, nChangePosRet, strFailReason, coinControl);
transaction.setTransactionFee(nFeeRequired);
if (fSubtractFeeFromAmount && fCreated)
transaction.reassignAmounts(nChangePosRet);
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/rpcevo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static void FundSpecialTx(CWallet* pwallet, CMutableTransaction& tx, const Speci
int nChangePos = -1;
std::string strFailReason;

if (!pwallet->CreateTransaction(vecSend, wtx, reservekey, nFee, nChangePos, strFailReason, &coinControl, false, tx.vExtraPayload.size())) {
if (!pwallet->CreateTransaction(vecSend, wtx, &reservekey, nFee, nChangePos, strFailReason, &coinControl, false, tx.vExtraPayload.size())) {
throw JSONRPCError(RPC_INTERNAL_ERROR, strFailReason);
}

Expand Down
10 changes: 10 additions & 0 deletions src/script/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
return subscript.GetSigOpCount(true);
}

bool CScript::IsPayToPublicKey() const {
if (size() != 35) return false;

opcodetype opcode;
const_iterator pc = begin();
GetOp(pc, opcode);
GetOp(pc, opcode);
return opcode == OP_CHECKSIG;
}

bool CScript::IsNormalPaymentScript() const
{
if(this->size() != 25) return false;
Expand Down
1 change: 1 addition & 0 deletions src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ class CScript : public CScriptBase
unsigned int GetSigOpCount(const CScript& scriptSig) const;
bool IsNormalPaymentScript() const;

bool IsPayToPublicKey() const;
bool IsPayToPublicKeyHash() const;

bool IsPayToScriptHash() const;
Expand Down
6 changes: 3 additions & 3 deletions src/sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "utilstrencodings.h"

#include <stdio.h>

#include <set>
#include <boost/foreach.hpp>
#include <boost/thread.hpp>

Expand Down Expand Up @@ -57,7 +57,7 @@ struct CLockLocation {

typedef std::vector<std::pair<void*, CLockLocation> > LockStack;
typedef std::map<std::pair<void*, void*>, LockStack> LockOrders;
typedef std::set<std::pair<void*, void*> > InvLockOrders;
typedef std::set<std::pair<void*, void*>> InvLockOrders;

struct LockData {
// Very ugly hack: as the global constructs and destructors run single
Expand Down Expand Up @@ -164,7 +164,7 @@ void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLi
if (i.first == cs) {
fprintf(stderr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
abort();
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/test_bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ TestingSetup::TestingSetup(const std::string& chainName, std::string suf) : Basi

// Init HD mint

// Create new keyUser and set as default key
// generate a new master key
pwalletMain->GenerateNewMnemonic();

CPubKey masterPubKey = pwalletMain->GenerateNewHDMasterKey();
pwalletMain->SetHDMasterKey(masterPubKey);
CPubKey newDefaultKey;
Expand Down
1 change: 1 addition & 0 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C
if (pfMissingInputs) {
*pfMissingInputs = true;
}
LogPrintf("%s(): Couldn't find input %s-%d for tx %s\n", __func__, txin.prevout.hash.GetHex(), txin.prevout.n, tx.GetHash().GetHex());
return false; // fMissingInputs and !state.IsInvalid() is used to detect this condition, don't set state.Invalid()
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/wallet/coincontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ enum class CoinType
ONLY_1000 = 5, // find znode outputs including locked ones (use with caution)
ONLY_PRIVATESEND_COLLATERAL = 6,
ONLY_MINTS = 7,
WITH_MINTS = 8
WITH_MINTS = 8,
WITH_1000 = 9
};

/** Coin Control Features. */
class CCoinControl
{
public:
std::set<COutPoint> setSelected;

CTxDestination destChange;
//! If false, allows unselected inputs, but requires all selected inputs be used
bool fAllowOtherInputs;
Expand All @@ -40,6 +43,10 @@ class CCoinControl
int nConfirmTarget;
//! Controls which types of coins are allowed to be used (default: ALL_COINS)
CoinType nCoinType;
//! No more than this number of inputs may be used.
size_t nMaxInputs;
// The generated transaction may not be over this size.
size_t nMaxSize;

CCoinControl()
{
Expand All @@ -58,6 +65,8 @@ class CCoinControl
fOverrideFeeRate = false;
nConfirmTarget = 0;
nCoinType = CoinType::ALL_COINS;
nMaxInputs = 0;
nMaxSize = 0;
}

bool HasSelected() const
Expand Down Expand Up @@ -89,9 +98,6 @@ class CCoinControl
{
vOutpoints.assign(setSelected.begin(), setSelected.end());
}

private:
std::set<COutPoint> setSelected;
};

#endif // BITCOIN_WALLET_COINCONTROL_H
4 changes: 2 additions & 2 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ static void SendMoney(CWallet * const pwallet, const CTxDestination &address, CA
int nChangePosRet = -1;
CRecipient recipient = {scriptPubKey, nValue, fSubtractFeeFromAmount};
vecSend.push_back(recipient);
if (!pwallet->CreateTransaction(vecSend, wtxNew, reservekey, nFeeRequired, nChangePosRet, strError)) {
if (!pwallet->CreateTransaction(vecSend, wtxNew, &reservekey, nFeeRequired, nChangePosRet, strError)) {
if (!fSubtractFeeFromAmount && nValue + nFeeRequired > curBalance)
strError = strprintf("Error: This transaction requires a transaction fee of at least %s", FormatMoney(nFeeRequired));
throw JSONRPCError(RPC_WALLET_ERROR, strError);
Expand Down Expand Up @@ -1156,7 +1156,7 @@ UniValue sendmany(const JSONRPCRequest& request)
CAmount nFeeRequired = 0;
int nChangePosRet = -1;
std::string strFailReason;
bool fCreated = pwallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired, nChangePosRet, strFailReason);
bool fCreated = pwallet->CreateTransaction(vecSend, wtx, &keyChange, nFeeRequired, nChangePosRet, strFailReason);
if (!fCreated)
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason);
CValidationState state;
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/sigmaspendbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <stdexcept>
#include <tuple>

class SigmaSpendSigner : public InputSigner
class SigmaSpendSigner : public SigmaTxBuilderInputSigner
{
public:
const sigma::PrivateCoin coin;
Expand Down Expand Up @@ -108,7 +108,7 @@ static std::unique_ptr<SigmaSpendSigner> CreateSigner(const CSigmaEntry& coin)
}

SigmaSpendBuilder::SigmaSpendBuilder(CWallet& wallet, CHDMintWallet& mintWallet, const CCoinControl *coinControl) :
TxBuilder(wallet),
SigmaTxBuilderSuperclass(wallet),
mintWallet(mintWallet)
{
cs_main.lock();
Expand All @@ -129,7 +129,7 @@ SigmaSpendBuilder::~SigmaSpendBuilder()
cs_main.unlock();
}

CAmount SigmaSpendBuilder::GetInputs(std::vector<std::unique_ptr<InputSigner>>& signers, CAmount required)
CAmount SigmaSpendBuilder::GetInputs(std::vector<std::unique_ptr<SigmaTxBuilderInputSigner>>& signers, CAmount required)
{
// get coins to spend

Expand Down
4 changes: 2 additions & 2 deletions src/wallet/sigmaspendbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <vector>

class SigmaSpendBuilder : public TxBuilder
class SigmaSpendBuilder : public SigmaTxBuilderSuperclass
{
public:
std::vector<CSigmaEntry> selected;
Expand All @@ -19,7 +19,7 @@ class SigmaSpendBuilder : public TxBuilder
~SigmaSpendBuilder() override;

protected:
CAmount GetInputs(std::vector<std::unique_ptr<InputSigner>>& signers, CAmount required) override;
CAmount GetInputs(std::vector<std::unique_ptr<SigmaTxBuilderInputSigner>>& signers, CAmount required) override;
// remint change
CAmount GetChanges(std::vector<CTxOut>& outputs, CAmount amount, CWalletDB& walletdb) override;

Expand Down
Loading

0 comments on commit be2ea19

Please sign in to comment.