Skip to content

Commit

Permalink
Segwit light for spending coins.
Browse files Browse the repository at this point in the history
Handle segwit-light properly when spending coins, either for staking
or normal transactions in the wallet.  Depending on when a transaction
was confirmed, we need to make sure to include the right outpoint (with
txid or bare txid) in the transaction spending an output.  This is done
through a custom UTXO hasher used in the wallet, which specifically
works for CWalletTx.
  • Loading branch information
domob1812 committed Jun 23, 2021
1 parent 6ec2d9f commit 431bd69
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
6 changes: 3 additions & 3 deletions divi/src/StakableCoin.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#ifndef STAKABLE_COIN_H
#define STAKABLE_COIN_H
#include <merkletx.h>
#include <uint256.h>
#include <primitives/transaction.h>
struct StakableCoin
{
const CTransaction* tx;
const CMerkleTx* tx;
COutPoint utxo;
uint256 blockHashOfFirstConfirmation;

explicit StakableCoin(
const CTransaction& txIn,
const CMerkleTx& txIn,
const COutPoint& utxoIn,
uint256 blockHashIn
): tx(&txIn)
Expand Down
2 changes: 1 addition & 1 deletion divi/src/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ bool CheckProofOfStakeContextAndRecoverStakingData(
uint256 hashBlock;
CTransaction txPrev;
if (!GetTransaction(txin.prevout.hash, txPrev, hashBlock, true))
return error("CheckProofOfStake() : INFO: read txPrev failed");
return error("CheckProofOfStake() : INFO: read txPrev failed for %s", txin.prevout.hash.GetHex());

const CScript &kernelScript = txPrev.vout[txin.prevout.n].scriptPubKey;

Expand Down
2 changes: 2 additions & 0 deletions divi/src/primitives/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class CTransaction

CTransaction& operator=(const CTransaction& tx);

virtual ~CTransaction() = default;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
Expand Down
36 changes: 30 additions & 6 deletions divi/src/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,28 +180,52 @@ bool WriteTxToDisk(const CWallet* walletPtr, const CWalletTx& transactionToWrite
namespace
{

/** Dummy UTXO hasher for the wallet. For now, this just always returns
* the normal txid, but we will later change it to return the proper hash
* for a WalletTx. */
/** UTXO hasher for wallet transactions. It uses the transaction's known block
* hash (from CMerkleTx) to determine the activation state of segwit light. */
class WalletUtxoHasher : public TransactionUtxoHasher
{

private:

const CChain& chainActive_;

public:

WalletUtxoHasher(const CChain& chain)
: chainActive_(chain)
{}

OutputHash GetUtxoHash(const CTransaction& tx) const override
{
return OutputHash(tx.GetHash());
const CMerkleTx* mtx = dynamic_cast<const CMerkleTx*>(&tx);
assert(mtx != nullptr);

const CBlockIndex* pindexBlock = nullptr;
const int depth = mtx->GetNumberOfBlockConfirmations(pindexBlock);

/* If the transaction is not yet confirmed in a block, we use the current
tip to determine the segwit-light activation status. This is not
perfect around the activation time, but there is nothing we can do
in that case anyway. Mempool and wallet discourage spending unconfirmed
outputs around the segwit-light fork anyway. */
if (depth <= 0)
pindexBlock = chainActive_.Tip();

assert(pindexBlock != nullptr);
const ActivationState as(pindexBlock);

return OutputHash(as.IsActive(Fork::SegwitLight) ? mtx->GetBareTxid() : mtx->GetHash());
}

};

} // anonymous namespace
}

CWallet::CWallet(const CChain& chain, const BlockMap& blockMap
): cs_wallet()
, transactionRecord_(new WalletTransactionRecord(cs_wallet,strWalletFile) )
, outputTracker_( new SpentOutputTracker(*transactionRecord_) )
, utxoHasher(new WalletUtxoHasher() )
, utxoHasher(new WalletUtxoHasher(chain) )
, chainActive_(chain)
, mapBlockIndex_(blockMap)
, orderedTransactionIndex()
Expand Down

0 comments on commit 431bd69

Please sign in to comment.