From 4734a8465ec5e83b52998452a66774cee1b2e6e8 Mon Sep 17 00:00:00 2001 From: random-zebra Date: Mon, 17 May 2021 14:53:59 +0200 Subject: [PATCH] [Cleanup][Tests] Fix chainparams-change in librust tests --- src/test/librust/sapling_test_fixture.cpp | 5 + src/test/librust/sapling_test_fixture.h | 8 ++ src/test/librust/sapling_wallet_tests.cpp | 109 ++++++++---------- .../librust/transaction_builder_tests.cpp | 46 ++------ src/test/librust/utiltest.cpp | 10 -- src/test/librust/utiltest.h | 3 - .../test/wallet_shielded_balances_tests.cpp | 12 +- 7 files changed, 72 insertions(+), 121 deletions(-) diff --git a/src/test/librust/sapling_test_fixture.cpp b/src/test/librust/sapling_test_fixture.cpp index 740c70341feef..ea03ff84c8637 100644 --- a/src/test/librust/sapling_test_fixture.cpp +++ b/src/test/librust/sapling_test_fixture.cpp @@ -13,3 +13,8 @@ SaplingTestingSetup::SaplingTestingSetup(const std::string& chainName) : Testing SaplingTestingSetup::~SaplingTestingSetup() { } + +SaplingRegTestingSetup::SaplingRegTestingSetup() : SaplingTestingSetup(CBaseChainParams::REGTEST) +{ + UpdateNetworkUpgradeParameters(Consensus::UPGRADE_V5_0, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); +} diff --git a/src/test/librust/sapling_test_fixture.h b/src/test/librust/sapling_test_fixture.h index 59ae6927b46d6..962d479ce2792 100644 --- a/src/test/librust/sapling_test_fixture.h +++ b/src/test/librust/sapling_test_fixture.h @@ -16,5 +16,13 @@ struct SaplingTestingSetup : public TestingSetup ~SaplingTestingSetup(); }; +/** + * Regtest setup with sapling always active + */ +struct SaplingRegTestingSetup : public SaplingTestingSetup +{ + SaplingRegTestingSetup(); +}; + #endif //PIVX_SAPLING_TEST_FIXTURE_H diff --git a/src/test/librust/sapling_wallet_tests.cpp b/src/test/librust/sapling_wallet_tests.cpp index 4fac832cbbdc3..36c8681a94bf6 100644 --- a/src/test/librust/sapling_wallet_tests.cpp +++ b/src/test/librust/sapling_wallet_tests.cpp @@ -70,10 +70,10 @@ uint256 GetWitnessesAndAnchors(CWallet& wallet, return saplingAnchor; } -BOOST_FIXTURE_TEST_SUITE(sapling_wallet_tests, WalletTestingSetup) +BOOST_FIXTURE_TEST_SUITE(sapling_wallet_tests, WalletRegTestingSetup) BOOST_AUTO_TEST_CASE(SetSaplingNoteAddrsInCWalletTx) { - auto consensusParams = RegtestActivateSapling(); + auto consensusParams = Params().GetConsensus(); CWallet& wallet = *pwalletMain; LOCK(wallet.cs_wallet); @@ -124,9 +124,6 @@ BOOST_AUTO_TEST_CASE(SetSaplingNoteAddrsInCWalletTx) { BOOST_CHECK(nullifier == wtx.mapSaplingNoteData[op].nullifier); BOOST_CHECK(nd.witnessHeight == wtx.mapSaplingNoteData[op].witnessHeight); BOOST_CHECK(witness == wtx.mapSaplingNoteData[op].witnesses.front()); - - // Revert to default - RegtestDeactivateSapling(); } // Cannot add note data for an index which does not exist in tx.vShieldedOutput @@ -142,8 +139,9 @@ BOOST_AUTO_TEST_CASE(SetInvalidSaplingNoteDataInCWalletTx) { BOOST_CHECK_THROW(wtx.SetSaplingNoteData(noteData), std::logic_error); } -BOOST_AUTO_TEST_CASE(FindMySaplingNotes) { - auto consensusParams = RegtestActivateSapling(); +BOOST_AUTO_TEST_CASE(FindMySaplingNotes) +{ + auto consensusParams = Params().GetConsensus(); CWallet& wallet = *pwalletMain; LOCK(wallet.cs_wallet); @@ -175,14 +173,12 @@ BOOST_AUTO_TEST_CASE(FindMySaplingNotes) { BOOST_CHECK(wallet.HaveSaplingSpendingKey(extfvk)); noteMap = wallet.GetSaplingScriptPubKeyMan()->FindMySaplingNotes(*wtx.tx).first; BOOST_CHECK_EQUAL(2, noteMap.size()); - - // Revert to default - RegtestDeactivateSapling(); } // Generate note A and spend to create note B, from which we spend to create two conflicting transactions -BOOST_AUTO_TEST_CASE(GetConflictedSaplingNotes) { - auto consensusParams = RegtestActivateSapling(); +BOOST_AUTO_TEST_CASE(GetConflictedSaplingNotes) +{ + auto consensusParams = Params().GetConsensus(); CWallet& wallet = *pwalletMain; LOCK2(cs_main, wallet.cs_wallet); @@ -296,15 +292,13 @@ BOOST_AUTO_TEST_CASE(GetConflictedSaplingNotes) { BOOST_CHECK(std::set({hash2, hash3}) == c3); // Tear down - chainActive.SetTip(NULL); + chainActive.SetTip(nullptr); mapBlockIndex.erase(blockHash); - - // Revert to default - RegtestDeactivateSapling(); } -BOOST_AUTO_TEST_CASE(SaplingNullifierIsSpent) { - auto consensusParams = RegtestActivateSapling(); +BOOST_AUTO_TEST_CASE(SaplingNullifierIsSpent) +{ + auto consensusParams = Params().GetConsensus(); CWallet& wallet = *pwalletMain; LOCK2(cs_main, wallet.cs_wallet); @@ -357,15 +351,13 @@ BOOST_AUTO_TEST_CASE(SaplingNullifierIsSpent) { BOOST_CHECK(wallet.GetSaplingScriptPubKeyMan()->IsSaplingSpent(nullifier)); // Tear down - chainActive.SetTip(NULL); + chainActive.SetTip(nullptr); mapBlockIndex.erase(blockHash); - - // Revert to default - RegtestDeactivateSapling(); } -BOOST_AUTO_TEST_CASE(NavigateFromSaplingNullifierToNote) { - auto consensusParams = RegtestActivateSapling(); +BOOST_AUTO_TEST_CASE(NavigateFromSaplingNullifierToNote) +{ + auto consensusParams = Params().GetConsensus(); CWallet& wallet = *pwalletMain; LOCK2(cs_main, wallet.cs_wallet); @@ -454,16 +446,14 @@ BOOST_AUTO_TEST_CASE(NavigateFromSaplingNullifierToNote) { } // Tear down - chainActive.SetTip(NULL); + chainActive.SetTip(nullptr); mapBlockIndex.erase(blockHash); - - // Revert to default - RegtestDeactivateSapling(); } // Create note A, spend A to create note B, spend and verify note B is from me. -BOOST_AUTO_TEST_CASE(SpentSaplingNoteIsFromMe) { - auto consensusParams = RegtestActivateSapling(); +BOOST_AUTO_TEST_CASE(SpentSaplingNoteIsFromMe) +{ + auto consensusParams = Params().GetConsensus(); CWallet& wallet = *pwalletMain; LOCK2(cs_main, wallet.cs_wallet); @@ -601,16 +591,14 @@ BOOST_AUTO_TEST_CASE(SpentSaplingNoteIsFromMe) { BOOST_CHECK(wallet.GetSaplingScriptPubKeyMan()->mapSaplingNullifiersToNotes.count(nullifier2)); // Tear down - chainActive.SetTip(NULL); + chainActive.SetTip(nullptr); mapBlockIndex.erase(blockHash); mapBlockIndex.erase(blockHash2); - - // Revert to default - RegtestDeactivateSapling(); } -BOOST_AUTO_TEST_CASE(CachedWitnessesEmptyChain) { - auto consensusParams = RegtestActivateSapling(); +BOOST_AUTO_TEST_CASE(CachedWitnessesEmptyChain) +{ + auto consensusParams = Params().GetConsensus(); CWallet& wallet = *pwalletMain; { @@ -646,13 +634,11 @@ BOOST_AUTO_TEST_CASE(CachedWitnessesEmptyChain) { // Until zcash#1302 is implemented, this should triggger an assertion BOOST_CHECK_THROW(wallet.DecrementNoteWitnesses(&index), std::runtime_error); - - // Revert to default - RegtestDeactivateSapling(); } -BOOST_AUTO_TEST_CASE(CachedWitnessesChainTip) { - auto consensusParams = RegtestActivateSapling(); +BOOST_AUTO_TEST_CASE(CachedWitnessesChainTip) +{ + auto consensusParams = Params().GetConsensus(); libzcash::SaplingExtendedSpendingKey sk = GetTestMasterSaplingSpendingKey(); CWallet& wallet = *pwalletMain; @@ -730,9 +716,9 @@ BOOST_AUTO_TEST_CASE(CachedWitnessesChainTip) { } } -BOOST_AUTO_TEST_CASE(CachedWitnessesDecrementFirst) { - auto consensusParams = RegtestActivateSapling(); - +BOOST_AUTO_TEST_CASE(CachedWitnessesDecrementFirst) +{ + auto consensusParams = Params().GetConsensus(); libzcash::SaplingExtendedSpendingKey sk = GetTestMasterSaplingSpendingKey(); CWallet& wallet = *pwalletMain; { @@ -798,8 +784,9 @@ BOOST_AUTO_TEST_CASE(CachedWitnessesDecrementFirst) { } } -BOOST_AUTO_TEST_CASE(CachedWitnessesCleanIndex) { - auto consensusParams = RegtestActivateSapling(); +BOOST_AUTO_TEST_CASE(CachedWitnessesCleanIndex) +{ + auto consensusParams = Params().GetConsensus(); libzcash::SaplingExtendedSpendingKey sk = GetTestMasterSaplingSpendingKey(); CWallet& wallet = *pwalletMain; @@ -874,8 +861,9 @@ BOOST_AUTO_TEST_CASE(CachedWitnessesCleanIndex) { } } -BOOST_AUTO_TEST_CASE(ClearNoteWitnessCache) { - auto consensusParams = RegtestActivateSapling(); +BOOST_AUTO_TEST_CASE(ClearNoteWitnessCache) +{ + auto consensusParams = Params().GetConsensus(); libzcash::SaplingExtendedSpendingKey sk = GetTestMasterSaplingSpendingKey(); CWallet& wallet = *pwalletMain; @@ -922,8 +910,9 @@ BOOST_AUTO_TEST_CASE(ClearNoteWitnessCache) { BOOST_CHECK_EQUAL(0, wallet.GetSaplingScriptPubKeyMan()->nWitnessCacheSize); } -BOOST_AUTO_TEST_CASE(UpdatedSaplingNoteData) { - auto consensusParams = RegtestActivateSapling(); +BOOST_AUTO_TEST_CASE(UpdatedSaplingNoteData) +{ + auto consensusParams = Params().GetConsensus(); CWallet& wallet = *pwalletMain; // Need to lock cs_main for now due the lock ordering. future: revamp all of this function to only lock where is needed. @@ -1028,15 +1017,13 @@ BOOST_AUTO_TEST_CASE(UpdatedSaplingNoteData) { BOOST_CHECK(wtx.mapSaplingNoteData[sop1].witnesses.front() == testNote.tree.witness()); // Tear down - chainActive.SetTip(NULL); + chainActive.SetTip(nullptr); mapBlockIndex.erase(blockHash); - - // Revert to default - RegtestDeactivateSapling(); } -BOOST_AUTO_TEST_CASE(MarkAffectedSaplingTransactionsDirty) { - auto consensusParams = RegtestActivateSapling(); +BOOST_AUTO_TEST_CASE(MarkAffectedSaplingTransactionsDirty) +{ + auto consensusParams = Params().GetConsensus(); CWallet& wallet = *pwalletMain; LOCK2(cs_main, wallet.cs_wallet); @@ -1140,16 +1127,13 @@ BOOST_AUTO_TEST_CASE(MarkAffectedSaplingTransactionsDirty) { BOOST_CHECK(!wallet.mapWallet.at(hash).IsAmountCached(CWalletTx::AmountType::DEBIT, ISMINE_SPENDABLE)); // Tear down - chainActive.SetTip(NULL); + chainActive.SetTip(nullptr); mapBlockIndex.erase(blockHash); - - // Revert to default - RegtestDeactivateSapling(); } BOOST_AUTO_TEST_CASE(GetNotes) { - auto consensusParams = RegtestActivateSapling(); + auto consensusParams = Params().GetConsensus(); CWallet& wallet = *pwalletMain; libzcash::SaplingPaymentAddress pk; @@ -1238,9 +1222,6 @@ BOOST_AUTO_TEST_CASE(GetNotes) LOCK(cs_main); chainActive.SetTip(nullptr); mapBlockIndex.erase(blockHash); - - // Revert to default - RegtestDeactivateSapling(); } // TODO: Back port WriteWitnessCache & SetBestChainIgnoresTxsWithoutShieldedData test cases. diff --git a/src/test/librust/transaction_builder_tests.cpp b/src/test/librust/transaction_builder_tests.cpp index 17aed96c8058e..55c5180c60660 100644 --- a/src/test/librust/transaction_builder_tests.cpp +++ b/src/test/librust/transaction_builder_tests.cpp @@ -13,11 +13,11 @@ #include #include -BOOST_FIXTURE_TEST_SUITE(sapling_transaction_builder_tests, SaplingTestingSetup) +BOOST_FIXTURE_TEST_SUITE(sapling_transaction_builder_tests, SaplingRegTestingSetup) BOOST_AUTO_TEST_CASE(TransparentToSapling) { - auto consensusParams = RegtestActivateSapling(); + auto consensusParams = Params().GetConsensus(); CBasicKeyStore keystore; CKey tsk = AddTestCKeyToKeyStore(keystore); @@ -49,13 +49,11 @@ BOOST_AUTO_TEST_CASE(TransparentToSapling) CValidationState state; BOOST_CHECK(SaplingValidation::ContextualCheckTransaction(tx, state, Params(), 2, true, false)); BOOST_CHECK_EQUAL(state.GetRejectReason(), ""); - - // Revert to default - RegtestDeactivateSapling(); } -BOOST_AUTO_TEST_CASE(SaplingToSapling) { - auto consensusParams = RegtestActivateSapling(); +BOOST_AUTO_TEST_CASE(SaplingToSapling) +{ + auto consensusParams = Params().GetConsensus(); auto sk = libzcash::SaplingSpendingKey::random(); auto expsk = sk.expanded_spending_key(); @@ -101,45 +99,33 @@ BOOST_AUTO_TEST_CASE(SaplingToSapling) { BOOST_CHECK_EQUAL(tx2.sapData->valueBalance, 10000000); BOOST_CHECK(SaplingValidation::ContextualCheckTransaction(tx2, state, Params(), 3, true, false)); BOOST_CHECK_EQUAL(state.GetRejectReason(), ""); - - // Revert to default - RegtestDeactivateSapling(); } BOOST_AUTO_TEST_CASE(ThrowsOnTransparentInputWithoutKeyStore) { - SelectParams(CBaseChainParams::REGTEST); - auto consensusParams = Params().GetConsensus(); - - auto builder = TransactionBuilder(consensusParams, 1); + auto builder = TransactionBuilder(Params().GetConsensus(), 1); BOOST_CHECK_THROW(builder.AddTransparentInput(COutPoint(), CScript(), 1), std::runtime_error); } BOOST_AUTO_TEST_CASE(RejectsInvalidTransparentOutput) { - SelectParams(CBaseChainParams::REGTEST); - auto consensusParams = Params().GetConsensus(); - // Default CTxDestination type is an invalid address CTxDestination taddr; - auto builder = TransactionBuilder(consensusParams, 1); + auto builder = TransactionBuilder(Params().GetConsensus(), 1); BOOST_CHECK_THROW(builder.AddTransparentOutput(taddr, 50), std::runtime_error); } BOOST_AUTO_TEST_CASE(RejectsInvalidTransparentChangeAddress) { - SelectParams(CBaseChainParams::REGTEST); - auto consensusParams = Params().GetConsensus(); - // Default CTxDestination type is an invalid address CTxDestination taddr; - auto builder = TransactionBuilder(consensusParams, 1); + auto builder = TransactionBuilder(Params().GetConsensus(), 1); BOOST_CHECK_THROW(builder.SendChangeTo(taddr), std::runtime_error); } BOOST_AUTO_TEST_CASE(FailsWithNegativeChange) { - auto consensusParams = RegtestActivateSapling(); + auto consensusParams = Params().GetConsensus(); // Generate dummy Sapling address auto sk = libzcash::SaplingSpendingKey::random(); @@ -179,14 +165,11 @@ BOOST_AUTO_TEST_CASE(FailsWithNegativeChange) // Succeeds if there is sufficient input builder.AddTransparentInput(COutPoint(), scriptPubKey, 10000); BOOST_CHECK(builder.Build().IsTx()); - - // Revert to default - RegtestDeactivateSapling(); } BOOST_AUTO_TEST_CASE(ChangeOutput) { - auto consensusParams = RegtestActivateSapling(); + auto consensusParams = Params().GetConsensus(); // Generate dummy Sapling address auto sk = libzcash::SaplingSpendingKey::random(); @@ -260,14 +243,11 @@ BOOST_AUTO_TEST_CASE(ChangeOutput) BOOST_CHECK_EQUAL(tx.sapData->valueBalance, 0); BOOST_CHECK_EQUAL(tx.vout[0].nValue, 15000000); } - - // Revert to default - RegtestDeactivateSapling(); } BOOST_AUTO_TEST_CASE(SetFee) { - auto consensusParams = RegtestActivateSapling(); + auto consensusParams = Params().GetConsensus(); // Generate dummy Sapling address auto sk = libzcash::SaplingSpendingKey::random(); @@ -292,14 +272,10 @@ BOOST_AUTO_TEST_CASE(SetFee) BOOST_CHECK_EQUAL(tx.sapData->vShieldedOutput.size(), 2); BOOST_CHECK_EQUAL(tx.sapData->valueBalance, COIN); } - - // Revert to default - RegtestDeactivateSapling(); } BOOST_AUTO_TEST_CASE(CheckSaplingTxVersion) { - SelectParams(CBaseChainParams::REGTEST); auto consensusParams = Params().GetConsensus(); auto sk = libzcash::SaplingSpendingKey::random(); diff --git a/src/test/librust/utiltest.cpp b/src/test/librust/utiltest.cpp index caf2511daf1b1..5864fea63bcb2 100644 --- a/src/test/librust/utiltest.cpp +++ b/src/test/librust/utiltest.cpp @@ -13,16 +13,6 @@ static const std::string T_SECRET_REGTEST = "cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN"; -const Consensus::Params& RegtestActivateSapling() { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_V5_0, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - return Params().GetConsensus(); -} - -void RegtestDeactivateSapling() { - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_V5_0, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); -} - libzcash::SaplingExtendedSpendingKey GetTestMasterSaplingSpendingKey() { std::vector> rawSeed(32); HDSeed seed(rawSeed); diff --git a/src/test/librust/utiltest.h b/src/test/librust/utiltest.h index 4f6e8412015f4..f58f806275d53 100644 --- a/src/test/librust/utiltest.h +++ b/src/test/librust/utiltest.h @@ -28,9 +28,6 @@ struct TransparentInput { CAmount amount; }; -const Consensus::Params& RegtestActivateSapling(); - -void RegtestDeactivateSapling(); libzcash::SaplingExtendedSpendingKey GetTestMasterSaplingSpendingKey(); diff --git a/src/wallet/test/wallet_shielded_balances_tests.cpp b/src/wallet/test/wallet_shielded_balances_tests.cpp index 8ab4fe8c23064..237663b3ced93 100644 --- a/src/wallet/test/wallet_shielded_balances_tests.cpp +++ b/src/wallet/test/wallet_shielded_balances_tests.cpp @@ -134,7 +134,7 @@ BOOST_AUTO_TEST_CASE(GetShieldedSimpleCachedCreditAndDebit) //////// Credit //////// /////////////////////// - auto consensusParams = RegtestActivateSapling(); + auto consensusParams = Params().GetConsensus(); // Main wallet CWallet &wallet = *pwalletMain; @@ -191,9 +191,6 @@ BOOST_AUTO_TEST_CASE(GetShieldedSimpleCachedCreditAndDebit) // Checks that the only shielded output of this tx is change. BOOST_CHECK(wallet.GetSaplingScriptPubKeyMan()->IsNoteSaplingChange( SaplingOutPoint(wtxDebitUpdated.GetHash(), 0), pa)); - - // Revert to default - RegtestDeactivateSapling(); } libzcash::SaplingPaymentAddress getNewDummyShieldedAddress() @@ -237,7 +234,7 @@ CWalletTx& buildTxAndLoadToWallet(CWallet& wallet, const libzcash::SaplingExtend */ BOOST_AUTO_TEST_CASE(VerifyShieldedToRemoteShieldedCachedBalance) { - auto consensusParams = RegtestActivateSapling(); + auto consensusParams = Params().GetConsensus(); // Main wallet CWallet &wallet = *pwalletMain; @@ -280,9 +277,6 @@ BOOST_AUTO_TEST_CASE(VerifyShieldedToRemoteShieldedCachedBalance) // Plus, change should be same and be cached as well BOOST_CHECK_EQUAL(wtxDebitUpdated.GetShieldedChange(), expectedShieldedChange); BOOST_CHECK(wtxDebitUpdated.fShieldedChangeCached); - - // Revert to default - RegtestDeactivateSapling(); } struct FakeBlock @@ -320,7 +314,7 @@ FakeBlock SimpleFakeMine(CWalletTx& wtx, SaplingMerkleTree& currentTree, CWallet */ BOOST_AUTO_TEST_CASE(GetShieldedAvailableCredit) { - auto consensusParams = RegtestActivateSapling(); + auto consensusParams = Params().GetConsensus(); // Main wallet CWallet &wallet = *pwalletMain;