Skip to content

Commit

Permalink
[Cleanup][Tests] Fix chainparams-change in librust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed May 17, 2021
1 parent ff4cee0 commit 4734a84
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 121 deletions.
5 changes: 5 additions & 0 deletions src/test/librust/sapling_test_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
8 changes: 8 additions & 0 deletions src/test/librust/sapling_test_fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
109 changes: 45 additions & 64 deletions src/test/librust/sapling_wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -296,15 +292,13 @@ BOOST_AUTO_TEST_CASE(GetConflictedSaplingNotes) {
BOOST_CHECK(std::set<uint256>({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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 4734a84

Please sign in to comment.