Skip to content

Commit

Permalink
Merge pull request #32 from SmartArray/tests/wallet_tests_3
Browse files Browse the repository at this point in the history
test: correct wallet_tests
  • Loading branch information
gto90 authored Apr 12, 2021
2 parents e70d45a + 3001119 commit 1cb4d6b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ class CRegTestParams : public CChainParams {
consensus.nTargetTimespan = 0.10 * 24 * 60 * 60; // 2.4 hours
consensus.nTargetSpacing = 60; // 60 seconds
consensus.nInterval = consensus.nTargetTimespan / consensus.nTargetSpacing;
consensus.nDiffChangeTarget = 67; // DigiShield Hard Fork Block BIP34Height 67,200
consensus.nDiffChangeTarget = 167; // DigiShield Hard Fork Block BIP34Height 67,200

// Old 1% monthly DGB Reward before 15 secon block change
consensus.patchBlockRewardDuration = 10; //10080; - No longer used
Expand Down
11 changes: 11 additions & 0 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,17 @@ void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpda
}
}

void RegenerateCommitments(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
{
CMutableTransaction tx{*block.vtx.at(0)};
tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
block.vtx.at(0) = MakeTransactionRef(tx);

GenerateCoinbaseCommitment(block, pindexPrev, Params().GetConsensus());

block.hashMerkleRoot = BlockMerkleRoot(block);
}

void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
{
// Update nExtraNonce
Expand Down
3 changes: 3 additions & 0 deletions src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ class BlockAssembler
int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set &mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
};

/** Regenerate Witness Commitments */
void RegenerateCommitments(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams);

/** Modify the extranonce in a block */
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev, int algo);
Expand Down
14 changes: 12 additions & 2 deletions src/test/test_digibyte.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,30 @@ TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>&

// Replace mempool-selected txns with just coinbase plus passed-in txns:
block.vtx.resize(1);
for (const CMutableTransaction& tx : txns)
for (const CMutableTransaction& tx : txns) {
block.vtx.push_back(MakeTransactionRef(tx));
}

// IncrementExtraNonce creates a valid coinbase and merkleRoot
{
LOCK(cs_main);
unsigned int extraNonce = 0;
RegenerateCommitments(block, chainActive.Tip(), chainparams.GetConsensus());
IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
}

while (!CheckProofOfWork(GetPoWAlgoHash(block), block.nBits, chainparams.GetConsensus())) ++block.nNonce;

std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
if (!ProcessNewBlock(chainparams, shared_pblock, true, nullptr)) {
throw new std::runtime_error("Could not process new test block");
// Uncomment this to catch potential errors. Use lldb/gdb to break on std::runtime_error.
// Previously, if ProcessNewBlock failed, the return code wasn't checked at all, that is,
// as a developer you had to manually step throught the code to see where it was broken.
// In our case, the merkle tree (witness) commitments weren't updated properly,
// which caused a rejection of the block. If `wallet_tests` ever fail again,
// uncommenting this line might help with the investigation.

// throw new std::runtime_error("Could not process new test block");
}

CBlock result = block;
Expand Down
15 changes: 0 additions & 15 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3275,21 +3275,6 @@ bool IsNullDummyEnabled(const CBlockIndex* pindexPrev, const Consensus::Params&
return (VersionBitsState(pindexPrev, params, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == ThresholdState::ACTIVE);
}

// Compute at which vout of the block's coinbase transaction the witness
// commitment occurs, or -1 if not found.
static int GetWitnessCommitmentIndex(const CBlock& block)
{
int commitpos = -1;
if (!block.vtx.empty()) {
for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
if (block.vtx[0]->vout[o].scriptPubKey.size() >= 38 && block.vtx[0]->vout[o].scriptPubKey[0] == OP_RETURN && block.vtx[0]->vout[o].scriptPubKey[1] == 0x24 && block.vtx[0]->vout[o].scriptPubKey[2] == 0xaa && block.vtx[0]->vout[o].scriptPubKey[3] == 0x21 && block.vtx[0]->vout[o].scriptPubKey[4] == 0xa9 && block.vtx[0]->vout[o].scriptPubKey[5] == 0xed) {
commitpos = o;
}
}
}
return commitpos;
}

void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
{
int commitpos = GetWitnessCommitmentIndex(block);
Expand Down
27 changes: 27 additions & 0 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ struct ChainTxData;
struct PrecomputedTransactionData;
struct LockPoints;

/** Index marker for when no witness commitment is present in a coinbase transaction. */
static constexpr int NO_WITNESS_COMMITMENT{-1};

/** Minimum size of a witness commitment structure. Defined in BIP 141. **/
static constexpr size_t MINIMUM_WITNESS_COMMITMENT{38};

/** Default for -whitelistrelay. */
static const bool DEFAULT_WHITELISTRELAY = true;
/** Default for -whitelistforcerelay. */
Expand Down Expand Up @@ -507,6 +513,27 @@ bool DumpMempool();
/** Load the mempool from disk. */
bool LoadMempool();

/** Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found */
inline int GetWitnessCommitmentIndex(const CBlock& block)
{
int commitpos = NO_WITNESS_COMMITMENT;
if (!block.vtx.empty()) {
for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
const CTxOut& vout = block.vtx[0]->vout[o];
if (vout.scriptPubKey.size() >= MINIMUM_WITNESS_COMMITMENT &&
vout.scriptPubKey[0] == OP_RETURN &&
vout.scriptPubKey[1] == 0x24 &&
vout.scriptPubKey[2] == 0xaa &&
vout.scriptPubKey[3] == 0x21 &&
vout.scriptPubKey[4] == 0xa9 &&
vout.scriptPubKey[5] == 0xed) {
commitpos = o;
}
}
}
return commitpos;
}

//! Check whether the block associated with this index entry is pruned or not.
inline bool IsBlockPruned(const CBlockIndex* pblockindex)
{
Expand Down
10 changes: 5 additions & 5 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
WalletRescanReserver reserver(&wallet);
reserver.reserve();
BOOST_CHECK_EQUAL(nullBlock, wallet.ScanForWalletTransactions(oldTip, nullptr, reserver));
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 100 * COIN);
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 2 * 72000 * COIN);
}

// Prune the older block file.
Expand All @@ -68,7 +68,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
WalletRescanReserver reserver(&wallet);
reserver.reserve();
BOOST_CHECK_EQUAL(oldTip, wallet.ScanForWalletTransactions(oldTip, nullptr, reserver));
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 50 * COIN);
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 72000 * COIN);
}

// Verify importmulti RPC returns failure for a key whose creation time is
Expand Down Expand Up @@ -196,7 +196,7 @@ BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
// credit amount is calculated.
wtx.MarkDirty();
wallet.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), 50*COIN);
BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), 72000*COIN);
}

static int64_t AddTx(CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
Expand Down Expand Up @@ -328,13 +328,13 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
BOOST_CHECK_EQUAL(list.begin()->second.size(), 1U);

// Check initial balance from one mature coinbase transaction.
BOOST_CHECK_EQUAL(50 * COIN, wallet->GetAvailableBalance());
BOOST_CHECK_EQUAL(72000 * COIN, wallet->GetAvailableBalance());

// Add a transaction creating a change address, and confirm ListCoins still
// returns the coin associated with the change address underneath the
// coinbaseKey pubkey, even though the change address has a different
// pubkey.
AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, false /* subtract fee */});
AddTx(CRecipient{GetScriptForRawPubKey({}), 2 * COIN, false /* subtract fee */});
list = wallet->ListCoins();
BOOST_CHECK_EQUAL(list.size(), 1U);
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
Expand Down

0 comments on commit 1cb4d6b

Please sign in to comment.