From 8f4103670171f2095892ad2cba41305a11965a2b Mon Sep 17 00:00:00 2001 From: Eirik Ogilvie-Wigley Date: Tue, 6 Nov 2018 12:30:06 -0700 Subject: [PATCH 01/12] Rename methods to include Sprout --- src/gtest/test_validation.cpp | 4 +- src/utiltest.cpp | 22 +++--- src/utiltest.h | 22 +++--- src/wallet/gtest/test_wallet.cpp | 120 +++++++++++++++---------------- src/zcbenchmarks.cpp | 10 +-- 5 files changed, 91 insertions(+), 87 deletions(-) diff --git a/src/gtest/test_validation.cpp b/src/gtest/test_validation.cpp index fb6b296ec96..4e937d04266 100644 --- a/src/gtest/test_validation.cpp +++ b/src/gtest/test_validation.cpp @@ -91,14 +91,14 @@ TEST(Validation, ReceivedBlockTransactions) { // Create a fake genesis block CBlock block1; - block1.vtx.push_back(GetValidReceive(*params, sk, 5, true)); + block1.vtx.push_back(GetValidSproutReceive(*params, sk, 5, true)); block1.hashMerkleRoot = block1.BuildMerkleTree(); CBlockIndex fakeIndex1 {block1}; // Create a fake child block CBlock block2; block2.hashPrevBlock = block1.GetHash(); - block2.vtx.push_back(GetValidReceive(*params, sk, 10, true)); + block2.vtx.push_back(GetValidSproutReceive(*params, sk, 10, true)); block2.hashMerkleRoot = block2.BuildMerkleTree(); CBlockIndex fakeIndex2 {block2}; fakeIndex2.pprev = &fakeIndex1; diff --git a/src/utiltest.cpp b/src/utiltest.cpp index 898f2c4c968..68d7f881e04 100644 --- a/src/utiltest.cpp +++ b/src/utiltest.cpp @@ -8,10 +8,11 @@ #include -CWalletTx GetValidReceive(ZCJoinSplit& params, - const libzcash::SproutSpendingKey& sk, CAmount value, - bool randomInputs, - int32_t version /* = 2 */) { +CWalletTx GetValidSproutReceive(ZCJoinSplit& params, + const libzcash::SproutSpendingKey& sk, + CAmount value, + bool randomInputs, + int32_t version /* = 2 */) { CMutableTransaction mtx; mtx.nVersion = version; mtx.vin.resize(2); @@ -70,9 +71,9 @@ CWalletTx GetValidReceive(ZCJoinSplit& params, return wtx; } -libzcash::SproutNote GetNote(ZCJoinSplit& params, - const libzcash::SproutSpendingKey& sk, - const CTransaction& tx, size_t js, size_t n) { +libzcash::SproutNote GetSproutNote(ZCJoinSplit& params, + const libzcash::SproutSpendingKey& sk, + const CTransaction& tx, size_t js, size_t n) { ZCNoteDecryption decryptor {sk.receiving_key()}; auto hSig = tx.vjoinsplit[js].h_sig(params, tx.joinSplitPubKey); auto note_pt = libzcash::SproutNotePlaintext::decrypt( @@ -84,9 +85,10 @@ libzcash::SproutNote GetNote(ZCJoinSplit& params, return note_pt.note(sk.address()); } -CWalletTx GetValidSpend(ZCJoinSplit& params, - const libzcash::SproutSpendingKey& sk, - const libzcash::SproutNote& note, CAmount value) { +CWalletTx GetValidSproutSpend(ZCJoinSplit& params, + const libzcash::SproutSpendingKey& sk, + const libzcash::SproutNote& note, + CAmount value) { CMutableTransaction mtx; mtx.vout.resize(2); mtx.vout[0].nValue = value; diff --git a/src/utiltest.h b/src/utiltest.h index 327dc7be49f..744344f4c14 100644 --- a/src/utiltest.h +++ b/src/utiltest.h @@ -7,13 +7,15 @@ #include "zcash/Note.hpp" #include "zcash/NoteEncryption.hpp" -CWalletTx GetValidReceive(ZCJoinSplit& params, - const libzcash::SproutSpendingKey& sk, CAmount value, - bool randomInputs, - int32_t version = 2); -libzcash::SproutNote GetNote(ZCJoinSplit& params, - const libzcash::SproutSpendingKey& sk, - const CTransaction& tx, size_t js, size_t n); -CWalletTx GetValidSpend(ZCJoinSplit& params, - const libzcash::SproutSpendingKey& sk, - const libzcash::SproutNote& note, CAmount value); +CWalletTx GetValidSproutReceive(ZCJoinSplit& params, + const libzcash::SproutSpendingKey& sk, + CAmount value, + bool randomInputs, + int32_t version = 2); +libzcash::SproutNote GetSproutNote(ZCJoinSplit& params, + const libzcash::SproutSpendingKey& sk, + const CTransaction& tx, size_t js, size_t n); +CWalletTx GetValidSproutSpend(ZCJoinSplit& params, + const libzcash::SproutSpendingKey& sk, + const libzcash::SproutNote& note, + CAmount value); diff --git a/src/wallet/gtest/test_wallet.cpp b/src/wallet/gtest/test_wallet.cpp index 0385d00ee88..cf52dc3929a 100644 --- a/src/wallet/gtest/test_wallet.cpp +++ b/src/wallet/gtest/test_wallet.cpp @@ -73,18 +73,18 @@ class TestWallet : public CWallet { } }; -CWalletTx GetValidReceive(const libzcash::SproutSpendingKey& sk, CAmount value, bool randomInputs, int32_t version = 2) { - return GetValidReceive(*params, sk, value, randomInputs, version); +CWalletTx GetValidSproutReceive(const libzcash::SproutSpendingKey& sk, CAmount value, bool randomInputs, int32_t version = 2) { + return GetValidSproutReceive(*params, sk, value, randomInputs, version); } -libzcash::SproutNote GetNote(const libzcash::SproutSpendingKey& sk, +libzcash::SproutNote GetSproutNote(const libzcash::SproutSpendingKey& sk, const CTransaction& tx, size_t js, size_t n) { - return GetNote(*params, sk, tx, js, n); + return GetSproutNote(*params, sk, tx, js, n); } -CWalletTx GetValidSpend(const libzcash::SproutSpendingKey& sk, +CWalletTx GetValidSproutSpend(const libzcash::SproutSpendingKey& sk, const libzcash::SproutNote& note, CAmount value) { - return GetValidSpend(*params, sk, note, value); + return GetValidSproutSpend(*params, sk, note, value); } std::vector SetSaplingNoteData(CWalletTx& wtx) { @@ -103,8 +103,8 @@ std::pair CreateValidBlock(TestWallet& wallet, CBlock& block, SproutMerkleTree& sproutTree, SaplingMerkleTree& saplingTree) { - auto wtx = GetValidReceive(sk, 50, true, 4); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 50, true, 4); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); mapSproutNoteData_t noteData; @@ -144,8 +144,8 @@ TEST(WalletTests, SetupDatadirLocationRunAsFirstTest) { TEST(WalletTests, SproutNoteDataSerialisation) { auto sk = libzcash::SproutSpendingKey::random(); - auto wtx = GetValidReceive(sk, 10, true); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); mapSproutNoteData_t noteData; @@ -172,8 +172,8 @@ TEST(WalletTests, FindUnspentSproutNotes) { auto sk = libzcash::SproutSpendingKey::random(); wallet.AddSproutSpendingKey(sk); - auto wtx = GetValidReceive(sk, 10, true); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); mapSproutNoteData_t noteData; @@ -230,7 +230,7 @@ TEST(WalletTests, FindUnspentSproutNotes) { // Let's spend the note. - auto wtx2 = GetValidSpend(sk, note, 5); + auto wtx2 = GetValidSproutSpend(sk, note, 5); wallet.AddToWallet(wtx2, true, NULL); EXPECT_FALSE(wallet.IsSproutSpent(nullifier)); @@ -277,8 +277,8 @@ TEST(WalletTests, FindUnspentSproutNotes) { // Let's receive a new note CWalletTx wtx3; { - auto wtx = GetValidReceive(sk, 20, true); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 20, true); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); mapSproutNoteData_t noteData; @@ -341,8 +341,8 @@ TEST(WalletTests, FindUnspentSproutNotes) { TEST(WalletTests, SetSproutNoteAddrsInCWalletTx) { auto sk = libzcash::SproutSpendingKey::random(); - auto wtx = GetValidReceive(sk, 10, true); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); EXPECT_EQ(0, wtx.mapSproutNoteData.size()); @@ -451,8 +451,8 @@ TEST(WalletTests, GetSproutNoteNullifier) { auto address = sk.address(); auto dec = ZCNoteDecryption(sk.receiving_key()); - auto wtx = GetValidReceive(sk, 10, true); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); auto hSig = wtx.vjoinsplit[0].h_sig( @@ -529,8 +529,8 @@ TEST(WalletTests, FindMySproutNotes) { auto sk2 = libzcash::SproutSpendingKey::random(); wallet.AddSproutSpendingKey(sk2); - auto wtx = GetValidReceive(sk, 10, true); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); auto noteMap = wallet.FindMySproutNotes(wtx); @@ -557,8 +557,8 @@ TEST(WalletTests, FindMySproutNotesInEncryptedWallet) { ASSERT_TRUE(wallet.EncryptKeys(vMasterKey)); - auto wtx = GetValidReceive(sk, 10, true); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); auto noteMap = wallet.FindMySproutNotes(wtx); @@ -583,12 +583,12 @@ TEST(WalletTests, GetConflictedSproutNotes) { auto sk = libzcash::SproutSpendingKey::random(); wallet.AddSproutSpendingKey(sk); - auto wtx = GetValidReceive(sk, 10, true); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); - auto wtx2 = GetValidSpend(sk, note, 5); - auto wtx3 = GetValidSpend(sk, note, 10); + auto wtx2 = GetValidSproutSpend(sk, note, 5); + auto wtx3 = GetValidSproutSpend(sk, note, 10); auto hash2 = wtx2.GetHash(); auto hash3 = wtx3.GetHash(); @@ -738,8 +738,8 @@ TEST(WalletTests, SproutNullifierIsSpent) { auto sk = libzcash::SproutSpendingKey::random(); wallet.AddSproutSpendingKey(sk); - auto wtx = GetValidReceive(sk, 10, true); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); EXPECT_FALSE(wallet.IsSproutSpent(nullifier)); @@ -747,7 +747,7 @@ TEST(WalletTests, SproutNullifierIsSpent) { wallet.AddToWallet(wtx, true, NULL); EXPECT_FALSE(wallet.IsSproutSpent(nullifier)); - auto wtx2 = GetValidSpend(sk, note, 5); + auto wtx2 = GetValidSproutSpend(sk, note, 5); wallet.AddToWallet(wtx2, true, NULL); EXPECT_FALSE(wallet.IsSproutSpent(nullifier)); @@ -847,8 +847,8 @@ TEST(WalletTests, NavigateFromSproutNullifierToNote) { auto sk = libzcash::SproutSpendingKey::random(); wallet.AddSproutSpendingKey(sk); - auto wtx = GetValidReceive(sk, 10, true); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); mapSproutNoteData_t noteData; @@ -977,10 +977,10 @@ TEST(WalletTests, SpentSproutNoteIsFromMe) { auto sk = libzcash::SproutSpendingKey::random(); wallet.AddSproutSpendingKey(sk); - auto wtx = GetValidReceive(sk, 10, true); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); - auto wtx2 = GetValidSpend(sk, note, 5); + auto wtx2 = GetValidSproutSpend(sk, note, 5); EXPECT_FALSE(wallet.IsFromMe(wtx)); EXPECT_FALSE(wallet.IsFromMe(wtx2)); @@ -1152,9 +1152,9 @@ TEST(WalletTests, CachedWitnessesEmptyChain) { auto sk = libzcash::SproutSpendingKey::random(); wallet.AddSproutSpendingKey(sk); - auto wtx = GetValidReceive(sk, 10, true, 4); - auto note = GetNote(sk, wtx, 0, 0); - auto note2 = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true, 4); + auto note = GetSproutNote(sk, wtx, 0, 0); + auto note2 = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); auto nullifier2 = note2.nullifier(sk); @@ -1233,8 +1233,8 @@ TEST(WalletTests, CachedWitnessesChainTip) { { // Second transaction - auto wtx = GetValidReceive(sk, 50, true, 4); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 50, true, 4); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); mapSproutNoteData_t sproutNoteData; @@ -1342,8 +1342,8 @@ TEST(WalletTests, CachedWitnessesDecrementFirst) { { // Third transaction - never mined - auto wtx = GetValidReceive(sk, 20, true, 4); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 20, true, 4); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); mapSproutNoteData_t noteData; @@ -1480,9 +1480,9 @@ TEST(WalletTests, ClearNoteWitnessCache) { auto sk = libzcash::SproutSpendingKey::random(); wallet.AddSproutSpendingKey(sk); - auto wtx = GetValidReceive(sk, 10, true, 4); + auto wtx = GetValidSproutReceive(sk, 10, true, 4); auto hash = wtx.GetHash(); - auto note = GetNote(sk, wtx, 0, 0); + auto note = GetSproutNote(sk, wtx, 0, 0); auto nullifier = note.nullifier(sk); mapSproutNoteData_t noteData; @@ -1540,8 +1540,8 @@ TEST(WalletTests, WriteWitnessCache) { auto sk = libzcash::SproutSpendingKey::random(); wallet.AddSproutSpendingKey(sk); - auto wtx = GetValidReceive(sk, 10, true); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); mapSproutNoteData_t noteData; @@ -1642,16 +1642,16 @@ TEST(WalletTests, SetBestChainIgnoresTxsWithoutShieldedData) { wallet.AddToWallet(wtxTransparent, true, nullptr); // Generate a Sprout transaction that is ours - auto wtxSprout = GetValidReceive(sk, 10, true); + auto wtxSprout = GetValidSproutReceive(sk, 10, true); auto noteMap = wallet.FindMySproutNotes(wtxSprout); wtxSprout.SetSproutNoteData(noteMap); wallet.AddToWallet(wtxSprout, true, nullptr); // Generate a Sprout transaction that only involves our transparent address auto sk2 = libzcash::SproutSpendingKey::random(); - auto wtxInput = GetValidReceive(sk2, 10, true); - auto note = GetNote(sk2, wtxInput, 0, 0); - auto wtxTmp = GetValidSpend(sk2, note, 5); + auto wtxInput = GetValidSproutReceive(sk2, 10, true); + auto note = GetSproutNote(sk2, wtxInput, 0, 0); + auto wtxTmp = GetValidSproutSpend(sk2, note, 5); CMutableTransaction mtx {wtxTmp}; mtx.vout[0].scriptPubKey = scriptPubKey; CWalletTx wtxSproutTransparent {nullptr, mtx}; @@ -1709,8 +1709,8 @@ TEST(WalletTests, UpdateSproutNullifierNoteMap) { ASSERT_TRUE(wallet.EncryptKeys(vMasterKey)); - auto wtx = GetValidReceive(sk, 10, true); - auto note = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); // Pretend that we called FindMySproutNotes while the wallet was locked @@ -1740,9 +1740,9 @@ TEST(WalletTests, UpdatedSproutNoteData) { auto sk = libzcash::SproutSpendingKey::random(); wallet.AddSproutSpendingKey(sk); - auto wtx = GetValidReceive(sk, 10, true); - auto note = GetNote(sk, wtx, 0, 0); - auto note2 = GetNote(sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto note = GetSproutNote(sk, wtx, 0, 0); + auto note2 = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); auto nullifier2 = note2.nullifier(sk); auto wtx2 = wtx; @@ -1909,11 +1909,11 @@ TEST(WalletTests, MarkAffectedSproutTransactionsDirty) { auto sk = libzcash::SproutSpendingKey::random(); wallet.AddSproutSpendingKey(sk); - auto wtx = GetValidReceive(sk, 10, true); + auto wtx = GetValidSproutReceive(sk, 10, true); auto hash = wtx.GetHash(); - auto note = GetNote(sk, wtx, 0, 1); + auto note = GetSproutNote(sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); - auto wtx2 = GetValidSpend(sk, note, 5); + auto wtx2 = GetValidSproutSpend(sk, note, 5); mapSproutNoteData_t noteData; JSOutPoint jsoutpt {hash, 0, 1}; @@ -2058,8 +2058,8 @@ TEST(WalletTests, SproutNoteLocking) { auto sk = libzcash::SproutSpendingKey::random(); wallet.AddSproutSpendingKey(sk); - auto wtx = GetValidReceive(sk, 10, true); - auto wtx2 = GetValidReceive(sk, 10, true); + auto wtx = GetValidSproutReceive(sk, 10, true); + auto wtx2 = GetValidSproutReceive(sk, 10, true); JSOutPoint jsoutpt {wtx.GetHash(), 0, 0}; JSOutPoint jsoutpt2 {wtx2.GetHash(),0, 0}; diff --git a/src/zcbenchmarks.cpp b/src/zcbenchmarks.cpp index c15df36a061..35758506356 100644 --- a/src/zcbenchmarks.cpp +++ b/src/zcbenchmarks.cpp @@ -289,7 +289,7 @@ double benchmark_try_decrypt_notes(size_t nAddrs) } auto sk = libzcash::SproutSpendingKey::random(); - auto tx = GetValidReceive(*pzcashParams, sk, 10, true); + auto tx = GetValidSproutReceive(*pzcashParams, sk, 10, true); struct timeval tv_start; timer_start(tv_start); @@ -309,8 +309,8 @@ double benchmark_increment_note_witnesses(size_t nTxs) // First block CBlock block1; for (int i = 0; i < nTxs; i++) { - auto wtx = GetValidReceive(*pzcashParams, sk, 10, true); - auto note = GetNote(*pzcashParams, sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(*pzcashParams, sk, 10, true); + auto note = GetSproutNote(*pzcashParams, sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); mapSproutNoteData_t noteData; @@ -332,8 +332,8 @@ double benchmark_increment_note_witnesses(size_t nTxs) CBlock block2; block2.hashPrevBlock = block1.GetHash(); { - auto wtx = GetValidReceive(*pzcashParams, sk, 10, true); - auto note = GetNote(*pzcashParams, sk, wtx, 0, 1); + auto wtx = GetValidSproutReceive(*pzcashParams, sk, 10, true); + auto note = GetSproutNote(*pzcashParams, sk, wtx, 0, 1); auto nullifier = note.nullifier(sk); mapSproutNoteData_t noteData; From 89e75c8c0d653ce52cf39feb1ffd80dcf604ebaf Mon Sep 17 00:00:00 2001 From: Eirik Ogilvie-Wigley Date: Tue, 6 Nov 2018 13:29:45 -0700 Subject: [PATCH 02/12] Add benchmark for decrypting sapling notes --- src/utiltest.cpp | 26 ++++++++++++++++++++++++++ src/utiltest.h | 7 +++++++ src/wallet/rpcwallet.cpp | 5 ++++- src/zcbenchmarks.cpp | 38 +++++++++++++++++++++++++++++++++++++- src/zcbenchmarks.h | 3 ++- 5 files changed, 76 insertions(+), 3 deletions(-) diff --git a/src/utiltest.cpp b/src/utiltest.cpp index 68d7f881e04..ce64ea1d5ec 100644 --- a/src/utiltest.cpp +++ b/src/utiltest.cpp @@ -5,6 +5,7 @@ #include "utiltest.h" #include "consensus/upgrades.h" +#include "transaction_builder.h" #include @@ -153,3 +154,28 @@ CWalletTx GetValidSproutSpend(ZCJoinSplit& params, CWalletTx wtx {NULL, tx}; return wtx; } + +CWalletTx GetValidSaplingTx(const Consensus::Params& consensusParams, + const libzcash::SaplingExtendedSpendingKey &sk, + CAmount value) { + auto expsk = sk.expsk; + auto fvk = expsk.full_viewing_key(); + auto pk = sk.DefaultAddress(); + + // Generate dummy Sapling note + libzcash::SaplingNote note(pk, value); + auto cm = note.cm().get(); + SaplingMerkleTree tree; + tree.append(cm); + auto anchor = tree.root(); + auto witness = tree.witness(); + + auto builder = TransactionBuilder(consensusParams, 1); + builder.SetFee(0); + assert(builder.AddSaplingSpend(expsk, note, anchor, witness)); + builder.AddSaplingOutput(fvk.ovk, pk, value, {}); + + CTransaction tx = builder.Build().get(); + CWalletTx wtx {NULL, tx}; + return wtx; +} diff --git a/src/utiltest.h b/src/utiltest.h index 744344f4c14..283742604ca 100644 --- a/src/utiltest.h +++ b/src/utiltest.h @@ -6,7 +6,9 @@ #include "zcash/JoinSplit.hpp" #include "zcash/Note.hpp" #include "zcash/NoteEncryption.hpp" +#include "zcash/zip32.h" +// Sprout CWalletTx GetValidSproutReceive(ZCJoinSplit& params, const libzcash::SproutSpendingKey& sk, CAmount value, @@ -19,3 +21,8 @@ CWalletTx GetValidSproutSpend(ZCJoinSplit& params, const libzcash::SproutSpendingKey& sk, const libzcash::SproutNote& note, CAmount value); + +// Sapling +CWalletTx GetValidSaplingTx(const Consensus::Params& consensusParams, + const libzcash::SaplingExtendedSpendingKey &sk, + CAmount value); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index f593f942ee5..051a70734f1 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2778,7 +2778,10 @@ UniValue zc_benchmark(const UniValue& params, bool fHelp) sample_times.push_back(benchmark_large_tx(nInputs)); } else if (benchmarktype == "trydecryptnotes") { int nAddrs = params[2].get_int(); - sample_times.push_back(benchmark_try_decrypt_notes(nAddrs)); + sample_times.push_back(benchmark_try_decrypt_sprout_notes(nAddrs)); + } else if (benchmarktype == "trydecryptsaplingnotes") { + int nAddrs = params[2].get_int(); + sample_times.push_back(benchmark_try_decrypt_sapling_notes(nAddrs)); } else if (benchmarktype == "incnotewitnesses") { int nTxs = params[2].get_int(); sample_times.push_back(benchmark_increment_note_witnesses(nTxs)); diff --git a/src/zcbenchmarks.cpp b/src/zcbenchmarks.cpp index 35758506356..b8b574b23dd 100644 --- a/src/zcbenchmarks.cpp +++ b/src/zcbenchmarks.cpp @@ -280,7 +280,7 @@ double benchmark_large_tx(size_t nInputs) return timer_stop(tv_start); } -double benchmark_try_decrypt_notes(size_t nAddrs) +double benchmark_try_decrypt_sprout_notes(size_t nAddrs) { CWallet wallet; for (int i = 0; i < nAddrs; i++) { @@ -297,6 +297,42 @@ double benchmark_try_decrypt_notes(size_t nAddrs) return timer_stop(tv_start); } +double benchmark_try_decrypt_sapling_notes(size_t nAddrs) +{ + // Set params + SelectParams(CBaseChainParams::REGTEST); + UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); + UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); + auto consensusParams = Params().GetConsensus(); + + std::vector> rawSeed(32); + HDSeed seed(rawSeed); + auto masterKey = libzcash::SaplingExtendedSpendingKey::Master(seed); + + CWallet wallet; + wallet.AddSaplingSpendingKey(masterKey, masterKey.DefaultAddress()); + + int i; + for (i = 0; i < nAddrs; i++) { + auto sk = masterKey.Derive(i); + wallet.AddSaplingSpendingKey(sk, sk.DefaultAddress()); + } + + auto sk = masterKey.Derive(i); + auto tx = GetValidSaplingTx(consensusParams, sk, 10); + + struct timeval tv_start; + timer_start(tv_start); + auto saplingNoteDataAndAddressesToAdd = wallet.FindMySaplingNotes(tx); + double tv_stop = timer_stop(tv_start); + + // Revert to default + UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + + return tv_stop; +} + double benchmark_increment_note_witnesses(size_t nTxs) { CWallet wallet; diff --git a/src/zcbenchmarks.h b/src/zcbenchmarks.h index 36382383810..7d511fdcdee 100644 --- a/src/zcbenchmarks.h +++ b/src/zcbenchmarks.h @@ -13,7 +13,8 @@ extern std::vector benchmark_solve_equihash_threaded(int nThreads); extern double benchmark_verify_joinsplit(const JSDescription &joinsplit); extern double benchmark_verify_equihash(); extern double benchmark_large_tx(size_t nInputs); -extern double benchmark_try_decrypt_notes(size_t nAddrs); +extern double benchmark_try_decrypt_sprout_notes(size_t nAddrs); +extern double benchmark_try_decrypt_sapling_notes(size_t nAddrs); extern double benchmark_increment_note_witnesses(size_t nTxs); extern double benchmark_connectblock_slow(); extern double benchmark_sendtoaddress(CAmount amount); From c3e2de59590c130a033164b78b657fcb222134b7 Mon Sep 17 00:00:00 2001 From: Eirik Ogilvie-Wigley Date: Tue, 6 Nov 2018 15:53:04 -0700 Subject: [PATCH 03/12] Move reusable Sapling test setup to utiltest --- src/gtest/test_checkblock.cpp | 4 +- src/gtest/test_checktransaction.cpp | 17 +++---- src/gtest/test_transaction_builder.cpp | 33 ++++--------- src/test/rpc_wallet_tests.cpp | 8 ++-- src/utiltest.cpp | 16 +++++++ src/utiltest.h | 4 ++ src/wallet/gtest/test_wallet.cpp | 64 +++++++------------------- src/zcbenchmarks.cpp | 8 +--- 8 files changed, 58 insertions(+), 96 deletions(-) diff --git a/src/gtest/test_checkblock.cpp b/src/gtest/test_checkblock.cpp index a7a45fa1208..dec0e8b00a6 100644 --- a/src/gtest/test_checkblock.cpp +++ b/src/gtest/test_checkblock.cpp @@ -3,6 +3,7 @@ #include "consensus/validation.h" #include "main.h" +#include "utiltest.h" #include "zcash/Proof.hpp" class MockCValidationState : public CValidationState { @@ -75,8 +76,7 @@ class ContextualCheckBlockTest : public ::testing::Test { virtual void TearDown() { // Revert to test default. No-op on mainnet params. - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } // Returns a valid but empty mutable transaction at block height 1. diff --git a/src/gtest/test_checktransaction.cpp b/src/gtest/test_checktransaction.cpp index c6f66cc64cf..28b19897e8a 100644 --- a/src/gtest/test_checktransaction.cpp +++ b/src/gtest/test_checktransaction.cpp @@ -5,6 +5,7 @@ #include "main.h" #include "primitives/transaction.h" #include "consensus/validation.h" +#include "utiltest.h" extern ZCJoinSplit* params; @@ -171,8 +172,7 @@ TEST(checktransaction_tests, BadTxnsOversize) { { // But should be fine again once Sapling activates! - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); + ActivateSapling(); mtx.fOverwintered = true; mtx.nVersionGroupId = SAPLING_VERSION_GROUP_ID; @@ -191,15 +191,12 @@ TEST(checktransaction_tests, BadTxnsOversize) { EXPECT_TRUE(ContextualCheckTransaction(tx, state, 1, 100)); // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } } TEST(checktransaction_tests, OversizeSaplingTxns) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); + ActivateSapling(); CMutableTransaction mtx = GetValidTransaction(); mtx.fOverwintered = true; @@ -252,8 +249,7 @@ TEST(checktransaction_tests, OversizeSaplingTxns) { } // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } TEST(checktransaction_tests, bad_txns_vout_negative) { @@ -969,8 +965,7 @@ TEST(checktransaction_tests, OverwinteredContextualCreateTx) { } // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } // Test a v1 transaction which has a malformed header, perhaps modified in-flight diff --git a/src/gtest/test_transaction_builder.cpp b/src/gtest/test_transaction_builder.cpp index 0f658075e5c..b20bef3e058 100644 --- a/src/gtest/test_transaction_builder.cpp +++ b/src/gtest/test_transaction_builder.cpp @@ -6,6 +6,7 @@ #include "pubkey.h" #include "rpc/protocol.h" #include "transaction_builder.h" +#include "utiltest.h" #include "zcash/Address.hpp" #include @@ -15,10 +16,7 @@ static const std::string tSecretRegtest = "cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotP TEST(TransactionBuilder, Invoke) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - auto consensusParams = Params().GetConsensus(); + auto consensusParams = ActivateSapling(); CBasicKeyStore keystore; CKey tsk = DecodeSecret(tSecretRegtest); @@ -87,8 +85,7 @@ TEST(TransactionBuilder, Invoke) EXPECT_EQ(state.GetRejectReason(), ""); // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } TEST(TransactionBuilder, ThrowsOnTransparentInputWithoutKeyStore) @@ -124,10 +121,7 @@ TEST(TransactionBuilder, RejectsInvalidTransparentChangeAddress) TEST(TransactionBuilder, FailsWithNegativeChange) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - auto consensusParams = Params().GetConsensus(); + auto consensusParams = ActivateSapling(); // Generate dummy Sapling address auto sk = libzcash::SaplingSpendingKey::random(); @@ -173,16 +167,12 @@ TEST(TransactionBuilder, FailsWithNegativeChange) EXPECT_TRUE(builder.Build().IsTx()); // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } TEST(TransactionBuilder, ChangeOutput) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - auto consensusParams = Params().GetConsensus(); + auto consensusParams = ActivateSapling(); // Generate dummy Sapling address auto sk = libzcash::SaplingSpendingKey::random(); @@ -264,16 +254,12 @@ TEST(TransactionBuilder, ChangeOutput) } // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } TEST(TransactionBuilder, SetFee) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - auto consensusParams = Params().GetConsensus(); + auto consensusParams = ActivateSapling(); // Generate dummy Sapling address auto sk = libzcash::SaplingSpendingKey::random(); @@ -321,8 +307,7 @@ TEST(TransactionBuilder, SetFee) } // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } TEST(TransactionBuilder, CheckSaplingTxVersion) diff --git a/src/test/rpc_wallet_tests.cpp b/src/test/rpc_wallet_tests.cpp index 418d4843d7b..8fa886bea98 100644 --- a/src/test/rpc_wallet_tests.cpp +++ b/src/test/rpc_wallet_tests.cpp @@ -20,6 +20,7 @@ #include "wallet/asyncrpcoperation_shieldcoinbase.h" #include "init.h" +#include "utiltest.h" #include #include @@ -1250,9 +1251,7 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals) BOOST_AUTO_TEST_CASE(rpc_z_sendmany_taddr_to_sapling) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); + ActivateSapling(); LOCK(pwalletMain->cs_wallet); @@ -1345,8 +1344,7 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_taddr_to_sapling) mapArgs.erase("-experimentalfeatures"); // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } diff --git a/src/utiltest.cpp b/src/utiltest.cpp index ce64ea1d5ec..06b1fa969e3 100644 --- a/src/utiltest.cpp +++ b/src/utiltest.cpp @@ -9,6 +9,7 @@ #include +// Sprout CWalletTx GetValidSproutReceive(ZCJoinSplit& params, const libzcash::SproutSpendingKey& sk, CAmount value, @@ -155,6 +156,21 @@ CWalletTx GetValidSproutSpend(ZCJoinSplit& params, return wtx; } +// Sapling +const Consensus::Params& ActivateSapling() { + SelectParams(CBaseChainParams::REGTEST); + UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); + UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); + return Params().GetConsensus(); +} + +void DeactivateSapling() { + UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + // Consider: Should we be doing the following? + // SelectParams(CBaseChainParams::MAIN); +} + CWalletTx GetValidSaplingTx(const Consensus::Params& consensusParams, const libzcash::SaplingExtendedSpendingKey &sk, CAmount value) { diff --git a/src/utiltest.h b/src/utiltest.h index 283742604ca..e5a1981764a 100644 --- a/src/utiltest.h +++ b/src/utiltest.h @@ -23,6 +23,10 @@ CWalletTx GetValidSproutSpend(ZCJoinSplit& params, CAmount value); // Sapling +const Consensus::Params& ActivateSapling(); + +void DeactivateSapling(); + CWalletTx GetValidSaplingTx(const Consensus::Params& consensusParams, const libzcash::SaplingExtendedSpendingKey &sk, CAmount value); diff --git a/src/wallet/gtest/test_wallet.cpp b/src/wallet/gtest/test_wallet.cpp index cf52dc3929a..325a9987fdc 100644 --- a/src/wallet/gtest/test_wallet.cpp +++ b/src/wallet/gtest/test_wallet.cpp @@ -356,10 +356,7 @@ TEST(WalletTests, SetSproutNoteAddrsInCWalletTx) { } TEST(WalletTests, SetSaplingNoteAddrsInCWalletTx) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - auto consensusParams = Params().GetConsensus(); + auto consensusParams = ActivateSapling(); TestWallet wallet; @@ -411,8 +408,7 @@ TEST(WalletTests, SetSaplingNoteAddrsInCWalletTx) { EXPECT_TRUE(witness == wtx.mapSaplingNoteData[op].witnesses.front()); // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } TEST(WalletTests, SetSproutInvalidNoteAddrsInCWalletTx) { @@ -476,10 +472,7 @@ TEST(WalletTests, GetSproutNoteNullifier) { } TEST(WalletTests, FindMySaplingNotes) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - auto consensusParams = Params().GetConsensus(); + auto consensusParams = ActivateSapling(); TestWallet wallet; @@ -518,8 +511,7 @@ TEST(WalletTests, FindMySaplingNotes) { EXPECT_EQ(2, noteMap.size()); // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } TEST(WalletTests, FindMySproutNotes) { @@ -610,10 +602,7 @@ TEST(WalletTests, GetConflictedSproutNotes) { // Generate note A and spend to create note B, from which we spend to create two conflicting transactions TEST(WalletTests, GetConflictedSaplingNotes) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - auto consensusParams = Params().GetConsensus(); + auto consensusParams = ActivateSapling(); TestWallet wallet; @@ -728,8 +717,7 @@ TEST(WalletTests, GetConflictedSaplingNotes) { mapBlockIndex.erase(blockHash); // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } TEST(WalletTests, SproutNullifierIsSpent) { @@ -773,10 +761,7 @@ TEST(WalletTests, SproutNullifierIsSpent) { } TEST(WalletTests, SaplingNullifierIsSpent) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - auto consensusParams = Params().GetConsensus(); + auto consensusParams = ActivateSapling(); TestWallet wallet; @@ -837,8 +822,7 @@ TEST(WalletTests, SaplingNullifierIsSpent) { mapBlockIndex.erase(blockHash); // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } TEST(WalletTests, NavigateFromSproutNullifierToNote) { @@ -868,10 +852,7 @@ TEST(WalletTests, NavigateFromSproutNullifierToNote) { } TEST(WalletTests, NavigateFromSaplingNullifierToNote) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - auto consensusParams = Params().GetConsensus(); + auto consensusParams = ActivateSapling(); TestWallet wallet; @@ -967,8 +948,7 @@ TEST(WalletTests, NavigateFromSaplingNullifierToNote) { mapBlockIndex.erase(blockHash); // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } TEST(WalletTests, SpentSproutNoteIsFromMe) { @@ -1001,10 +981,7 @@ TEST(WalletTests, SpentSproutNoteIsFromMe) { // Create note A, spend A to create note B, spend and verify note B is from me. TEST(WalletTests, SpentSaplingNoteIsFromMe) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - auto consensusParams = Params().GetConsensus(); + auto consensusParams = ActivateSapling(); TestWallet wallet; @@ -1142,8 +1119,7 @@ TEST(WalletTests, SpentSaplingNoteIsFromMe) { mapBlockIndex.erase(blockHash2); // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } TEST(WalletTests, CachedWitnessesEmptyChain) { @@ -1782,10 +1758,7 @@ TEST(WalletTests, UpdatedSproutNoteData) { } TEST(WalletTests, UpdatedSaplingNoteData) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - auto consensusParams = Params().GetConsensus(); + auto consensusParams = ActivateSapling(); TestWallet wallet; @@ -1899,8 +1872,7 @@ TEST(WalletTests, UpdatedSaplingNoteData) { mapBlockIndex.erase(blockHash); // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } TEST(WalletTests, MarkAffectedSproutTransactionsDirty) { @@ -1935,10 +1907,7 @@ TEST(WalletTests, MarkAffectedSproutTransactionsDirty) { } TEST(WalletTests, MarkAffectedSaplingTransactionsDirty) { - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - auto consensusParams = Params().GetConsensus(); + auto consensusParams = ActivateSapling(); TestWallet wallet; @@ -2048,8 +2017,7 @@ TEST(WalletTests, MarkAffectedSaplingTransactionsDirty) { mapBlockIndex.erase(blockHash); // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); } TEST(WalletTests, SproutNoteLocking) { diff --git a/src/zcbenchmarks.cpp b/src/zcbenchmarks.cpp index b8b574b23dd..89eed958906 100644 --- a/src/zcbenchmarks.cpp +++ b/src/zcbenchmarks.cpp @@ -300,10 +300,7 @@ double benchmark_try_decrypt_sprout_notes(size_t nAddrs) double benchmark_try_decrypt_sapling_notes(size_t nAddrs) { // Set params - SelectParams(CBaseChainParams::REGTEST); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); - auto consensusParams = Params().GetConsensus(); + auto consensusParams = ActivateSapling(); std::vector> rawSeed(32); HDSeed seed(rawSeed); @@ -327,8 +324,7 @@ double benchmark_try_decrypt_sapling_notes(size_t nAddrs) double tv_stop = timer_stop(tv_start); // Revert to default - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); + DeactivateSapling(); return tv_stop; } From 9eadcb3eca55973048b5aa91d2bb5cdf2d1e3d1b Mon Sep 17 00:00:00 2001 From: Eirik Ogilvie-Wigley Date: Tue, 6 Nov 2018 16:57:10 -0700 Subject: [PATCH 04/12] Move test SaplingNote creation to utiltest --- src/gtest/test_transaction_builder.cpp | 44 ++++---------- src/utiltest.cpp | 25 ++++---- src/utiltest.h | 10 ++++ src/wallet/gtest/test_wallet.cpp | 82 +++++++++----------------- 4 files changed, 66 insertions(+), 95 deletions(-) diff --git a/src/gtest/test_transaction_builder.cpp b/src/gtest/test_transaction_builder.cpp index b20bef3e058..3fa78d75610 100644 --- a/src/gtest/test_transaction_builder.cpp +++ b/src/gtest/test_transaction_builder.cpp @@ -127,7 +127,7 @@ TEST(TransactionBuilder, FailsWithNegativeChange) auto sk = libzcash::SaplingSpendingKey::random(); auto expsk = sk.expanded_spending_key(); auto fvk = sk.full_viewing_key(); - auto pk = sk.default_address(); + auto pa = sk.default_address(); // Set up dummy transparent address CBasicKeyStore keystore; @@ -137,18 +137,12 @@ TEST(TransactionBuilder, FailsWithNegativeChange) auto scriptPubKey = GetScriptForDestination(tkeyid); CTxDestination taddr = tkeyid; - // Generate dummy Sapling note - libzcash::SaplingNote note(pk, 59999); - auto cm = note.cm().value(); - SaplingMerkleTree tree; - tree.append(cm); - auto anchor = tree.root(); - auto witness = tree.witness(); + auto testNote = GetTestSaplingNote(pa, 59999); // Fail if there is only a Sapling output // 0.0005 z-ZEC out, 0.0001 t-ZEC fee auto builder = TransactionBuilder(consensusParams, 1); - builder.AddSaplingOutput(fvk.ovk, pk, 50000, {}); + builder.AddSaplingOutput(fvk.ovk, pa, 50000, {}); EXPECT_EQ("Change cannot be negative", builder.Build().GetError()); // Fail if there is only a transparent output @@ -159,7 +153,7 @@ TEST(TransactionBuilder, FailsWithNegativeChange) // Fails if there is insufficient input // 0.0005 t-ZEC out, 0.0001 t-ZEC fee, 0.00059999 z-ZEC in - builder.AddSaplingSpend(expsk, note, anchor, witness); + builder.AddSaplingSpend(expsk, testNote.note, testNote.tree.root(), testNote.tree.witness()); EXPECT_EQ("Change cannot be negative", builder.Build().GetError()); // Succeeds if there is sufficient input @@ -177,15 +171,9 @@ TEST(TransactionBuilder, ChangeOutput) // Generate dummy Sapling address auto sk = libzcash::SaplingSpendingKey::random(); auto expsk = sk.expanded_spending_key(); - auto pk = sk.default_address(); + auto pa = sk.default_address(); - // Generate dummy Sapling note - libzcash::SaplingNote note(pk, 25000); - auto cm = note.cm().value(); - SaplingMerkleTree tree; - tree.append(cm); - auto anchor = tree.root(); - auto witness = tree.witness(); + auto testNote = GetTestSaplingNote(pa, 25000); // Generate change Sapling address auto sk2 = libzcash::SaplingSpendingKey::random(); @@ -211,7 +199,7 @@ TEST(TransactionBuilder, ChangeOutput) { auto builder = TransactionBuilder(consensusParams, 1, &keystore); builder.AddTransparentInput(COutPoint(), scriptPubKey, 25000); - builder.AddSaplingSpend(expsk, note, anchor, witness); + builder.AddSaplingSpend(expsk, testNote.note, testNote.tree.root(), testNote.tree.witness()); auto tx = builder.Build().GetTxOrThrow(); EXPECT_EQ(tx.vin.size(), 1); @@ -265,21 +253,15 @@ TEST(TransactionBuilder, SetFee) auto sk = libzcash::SaplingSpendingKey::random(); auto expsk = sk.expanded_spending_key(); auto fvk = sk.full_viewing_key(); - auto pk = sk.default_address(); + auto pa = sk.default_address(); - // Generate dummy Sapling note - libzcash::SaplingNote note(pk, 50000); - auto cm = note.cm().value(); - SaplingMerkleTree tree; - tree.append(cm); - auto anchor = tree.root(); - auto witness = tree.witness(); + auto testNote = GetTestSaplingNote(pa, 50000); // Default fee { auto builder = TransactionBuilder(consensusParams, 1); - builder.AddSaplingSpend(expsk, note, anchor, witness); - builder.AddSaplingOutput(fvk.ovk, pk, 25000, {}); + builder.AddSaplingSpend(expsk, testNote.note, testNote.tree.root(), testNote.tree.witness()); + builder.AddSaplingOutput(fvk.ovk, pa, 25000, {}); auto tx = builder.Build().GetTxOrThrow(); EXPECT_EQ(tx.vin.size(), 0); @@ -293,8 +275,8 @@ TEST(TransactionBuilder, SetFee) // Configured fee { auto builder = TransactionBuilder(consensusParams, 1); - builder.AddSaplingSpend(expsk, note, anchor, witness); - builder.AddSaplingOutput(fvk.ovk, pk, 25000, {}); + builder.AddSaplingSpend(expsk, testNote.note, testNote.tree.root(), testNote.tree.witness()); + builder.AddSaplingOutput(fvk.ovk, pa, 25000, {}); builder.SetFee(20000); auto tx = builder.Build().GetTxOrThrow(); diff --git a/src/utiltest.cpp b/src/utiltest.cpp index 06b1fa969e3..ac02d5a68f6 100644 --- a/src/utiltest.cpp +++ b/src/utiltest.cpp @@ -171,27 +171,30 @@ void DeactivateSapling() { // SelectParams(CBaseChainParams::MAIN); } +TestSaplingNote GetTestSaplingNote(const libzcash::SaplingPaymentAddress& pa, CAmount value) { + // Generate dummy Sapling note + libzcash::SaplingNote note(pa, value); + uint256 cm = note.cm().get(); + SaplingMerkleTree tree; + tree.append(cm); + return { note, tree }; +} + CWalletTx GetValidSaplingTx(const Consensus::Params& consensusParams, const libzcash::SaplingExtendedSpendingKey &sk, CAmount value) { auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); - auto pk = sk.DefaultAddress(); + auto pa = sk.DefaultAddress(); - // Generate dummy Sapling note - libzcash::SaplingNote note(pk, value); - auto cm = note.cm().get(); - SaplingMerkleTree tree; - tree.append(cm); - auto anchor = tree.root(); - auto witness = tree.witness(); + auto testNote = GetTestSaplingNote(pa, value); auto builder = TransactionBuilder(consensusParams, 1); builder.SetFee(0); - assert(builder.AddSaplingSpend(expsk, note, anchor, witness)); - builder.AddSaplingOutput(fvk.ovk, pk, value, {}); + builder.AddSaplingSpend(expsk, testNote.note, testNote.tree.root(), testNote.tree.witness()); + builder.AddSaplingOutput(fvk.ovk, pa, value, {}); - CTransaction tx = builder.Build().get(); + CTransaction tx = builder.Build().GetTxOrThrow(); CWalletTx wtx {NULL, tx}; return wtx; } diff --git a/src/utiltest.h b/src/utiltest.h index e5a1981764a..6207e3accc9 100644 --- a/src/utiltest.h +++ b/src/utiltest.h @@ -23,10 +23,20 @@ CWalletTx GetValidSproutSpend(ZCJoinSplit& params, CAmount value); // Sapling +struct TestSaplingNote { + libzcash::SaplingNote note; + SaplingMerkleTree tree; +}; + const Consensus::Params& ActivateSapling(); void DeactivateSapling(); +/** + * Generate a dummy SaplingNote and a SaplingMerkleTree with that note's commitment. + */ +TestSaplingNote GetTestSaplingNote(const libzcash::SaplingPaymentAddress& pa, CAmount value); + CWalletTx GetValidSaplingTx(const Consensus::Params& consensusParams, const libzcash::SaplingExtendedSpendingKey &sk, CAmount value); diff --git a/src/wallet/gtest/test_wallet.cpp b/src/wallet/gtest/test_wallet.cpp index 325a9987fdc..a980429521c 100644 --- a/src/wallet/gtest/test_wallet.cpp +++ b/src/wallet/gtest/test_wallet.cpp @@ -482,20 +482,14 @@ TEST(WalletTests, FindMySaplingNotes) { auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); - auto pk = sk.DefaultAddress(); + auto pa = sk.DefaultAddress(); - // Generate dummy Sapling note - libzcash::SaplingNote note(pk, 50000); - auto cm = note.cm().get(); - SaplingMerkleTree tree; - tree.append(cm); - auto anchor = tree.root(); - auto witness = tree.witness(); + auto testNote = GetTestSaplingNote(pa, 50000); // Generate transaction auto builder = TransactionBuilder(consensusParams, 1); - builder.AddSaplingSpend(expsk, note, anchor, witness); - builder.AddSaplingOutput(fvk.ovk, pk, 25000, {}); + builder.AddSaplingSpend(expsk, testNote.note, testNote.tree.root(), testNote.tree.witness()); + builder.AddSaplingOutput(fvk.ovk, pa, 25000, {}); auto tx = builder.Build().GetTxOrThrow(); // No Sapling notes can be found in tx which does not belong to the wallet @@ -505,7 +499,7 @@ TEST(WalletTests, FindMySaplingNotes) { EXPECT_EQ(0, noteMap.size()); // Add spending key to wallet, so Sapling notes can be found - ASSERT_TRUE(wallet.AddSaplingZKey(sk, pk)); + ASSERT_TRUE(wallet.AddSaplingZKey(sk, pa)); ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk)); noteMap = wallet.FindMySaplingNotes(wtx).first; EXPECT_EQ(2, noteMap.size()); @@ -771,28 +765,22 @@ TEST(WalletTests, SaplingNullifierIsSpent) { auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); - auto pk = sk.DefaultAddress(); + auto pa = sk.DefaultAddress(); - // Generate dummy Sapling note - libzcash::SaplingNote note(pk, 50000); - auto cm = note.cm().get(); - SaplingMerkleTree tree; - tree.append(cm); - auto anchor = tree.root(); - auto witness = tree.witness(); + auto testNote = GetTestSaplingNote(pa, 50000); // Generate transaction auto builder = TransactionBuilder(consensusParams, 1); - builder.AddSaplingSpend(expsk, note, anchor, witness); - builder.AddSaplingOutput(fvk.ovk, pk, 25000, {}); + builder.AddSaplingSpend(expsk, testNote.note, testNote.tree.root(), testNote.tree.witness()); + builder.AddSaplingOutput(fvk.ovk, pa, 25000, {}); auto tx = builder.Build().GetTxOrThrow(); CWalletTx wtx {&wallet, tx}; - ASSERT_TRUE(wallet.AddSaplingZKey(sk, pk)); + ASSERT_TRUE(wallet.AddSaplingZKey(sk, pa)); ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk)); // Manually compute the nullifier based on the known position - auto nf = note.nullifier(fvk, witness.position()); + auto nf = testNote.note.nullifier(fvk, testNote.tree.witness().position()); ASSERT_TRUE(nf); uint256 nullifier = nf.get(); @@ -862,28 +850,22 @@ TEST(WalletTests, NavigateFromSaplingNullifierToNote) { auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); - auto pk = sk.DefaultAddress(); + auto pa = sk.DefaultAddress(); - // Generate dummy Sapling note - libzcash::SaplingNote note(pk, 50000); - auto cm = note.cm().get(); - SaplingMerkleTree saplingTree; - saplingTree.append(cm); - auto anchor = saplingTree.root(); - auto witness = saplingTree.witness(); + auto testNote = GetTestSaplingNote(pa, 50000); // Generate transaction auto builder = TransactionBuilder(consensusParams, 1); - builder.AddSaplingSpend(expsk, note, anchor, witness); - builder.AddSaplingOutput(fvk.ovk, pk, 25000, {}); + builder.AddSaplingSpend(expsk, testNote.note, testNote.tree.root(), testNote.tree.witness()); + builder.AddSaplingOutput(fvk.ovk, pa, 25000, {}); auto tx = builder.Build().GetTxOrThrow(); CWalletTx wtx {&wallet, tx}; - ASSERT_TRUE(wallet.AddSaplingZKey(sk, pk)); + ASSERT_TRUE(wallet.AddSaplingZKey(sk, pa)); ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk)); // Manually compute the nullifier based on the expected position - auto nf = note.nullifier(fvk, witness.position()); + auto nf = testNote.note.nullifier(fvk, testNote.tree.witness().position()); ASSERT_TRUE(nf); uint256 nullifier = nf.get(); @@ -922,7 +904,7 @@ TEST(WalletTests, NavigateFromSaplingNullifierToNote) { } // Simulate receiving new block and ChainTip signal - wallet.IncrementNoteWitnesses(&fakeIndex, &block, sproutTree, saplingTree); + wallet.IncrementNoteWitnesses(&fakeIndex, &block, sproutTree, testNote.tree); wallet.UpdateSaplingNullifierNoteMapForBlock(&block); // Retrieve the updated wtx from wallet @@ -1770,31 +1752,25 @@ TEST(WalletTests, UpdatedSaplingNoteData) { auto sk = m.Derive(0); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); - auto pk = sk.DefaultAddress(); + auto pa = sk.DefaultAddress(); // Generate dummy recipient Sapling address auto sk2 = m.Derive(1); auto expsk2 = sk2.expsk; auto fvk2 = expsk2.full_viewing_key(); - auto pk2 = sk2.DefaultAddress(); + auto pa2 = sk2.DefaultAddress(); - // Generate dummy Sapling note - libzcash::SaplingNote note(pk, 50000); - auto cm = note.cm().get(); - SaplingMerkleTree saplingTree; - saplingTree.append(cm); - auto anchor = saplingTree.root(); - auto witness = saplingTree.witness(); + auto testNote = GetTestSaplingNote(pa, 50000); // Generate transaction auto builder = TransactionBuilder(consensusParams, 1); - builder.AddSaplingSpend(expsk, note, anchor, witness); - builder.AddSaplingOutput(fvk.ovk, pk2, 25000, {}); + builder.AddSaplingSpend(expsk, testNote.note, testNote.tree.root(), testNote.tree.witness()); + builder.AddSaplingOutput(fvk.ovk, pa2, 25000, {}); auto tx = builder.Build().GetTxOrThrow(); // Wallet contains fvk1 but not fvk2 CWalletTx wtx {&wallet, tx}; - ASSERT_TRUE(wallet.AddSaplingZKey(sk, pk)); + ASSERT_TRUE(wallet.AddSaplingZKey(sk, pa)); ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk)); ASSERT_FALSE(wallet.HaveSaplingSpendingKey(fvk2)); @@ -1819,15 +1795,15 @@ TEST(WalletTests, UpdatedSaplingNoteData) { wallet.AddToWallet(wtx, true, NULL); // Simulate receiving new block and ChainTip signal - wallet.IncrementNoteWitnesses(&fakeIndex, &block, sproutTree, saplingTree); + wallet.IncrementNoteWitnesses(&fakeIndex, &block, sproutTree, testNote.tree); wallet.UpdateSaplingNullifierNoteMapForBlock(&block); // Retrieve the updated wtx from wallet uint256 hash = wtx.GetHash(); wtx = wallet.mapWallet[hash]; - // Now lets add key fvk2 so wallet can find the payment note sent to pk2 - ASSERT_TRUE(wallet.AddSaplingZKey(sk2, pk2)); + // Now lets add key fvk2 so wallet can find the payment note sent to pa2 + ASSERT_TRUE(wallet.AddSaplingZKey(sk2, pa2)); ASSERT_TRUE(wallet.HaveSaplingSpendingKey(fvk2)); CWalletTx wtx2 = wtx; auto saplingNoteData2 = wallet.FindMySaplingNotes(wtx2).first; @@ -1837,7 +1813,7 @@ TEST(WalletTests, UpdatedSaplingNoteData) { // The payment note has not been witnessed yet, so let's fake the witness. SaplingOutPoint sop0(wtx2.GetHash(), 0); SaplingOutPoint sop1(wtx2.GetHash(), 1); - wtx2.mapSaplingNoteData[sop0].witnesses.push_front(saplingTree.witness()); + wtx2.mapSaplingNoteData[sop0].witnesses.push_front(testNote.tree.witness()); wtx2.mapSaplingNoteData[sop0].witnessHeight = 0; // The txs are different as wtx is aware of just the change output, @@ -1865,7 +1841,7 @@ TEST(WalletTests, UpdatedSaplingNoteData) { EXPECT_EQ(wtx.mapSaplingNoteData[sop0].witnesses.front(), wtx2.mapSaplingNoteData[sop0].witnesses.front()); // wtx2 never had its change output witnessed even though it has been in wtx EXPECT_EQ(0, wtx2.mapSaplingNoteData[sop1].witnesses.size()); - EXPECT_EQ(wtx.mapSaplingNoteData[sop1].witnesses.front(), saplingTree.witness()); + EXPECT_EQ(wtx.mapSaplingNoteData[sop1].witnesses.front(), testNote.tree.witness()); // Tear down chainActive.SetTip(NULL); From 1534671fb0d03d1b80e850643377113866fc2d42 Mon Sep 17 00:00:00 2001 From: Eirik Ogilvie-Wigley Date: Tue, 6 Nov 2018 21:36:07 -0700 Subject: [PATCH 05/12] Add test method for generating master Sapling extended spending keys --- src/gtest/test_keys.cpp | 6 +++--- src/gtest/test_keystore.cpp | 5 ++--- src/test/key_tests.cpp | 5 ++--- src/test/rpc_wallet_tests.cpp | 4 +--- src/utiltest.cpp | 8 +++++++- src/utiltest.h | 2 ++ src/wallet/gtest/test_wallet.cpp | 32 ++++++++------------------------ src/zcbenchmarks.cpp | 4 +--- 8 files changed, 26 insertions(+), 40 deletions(-) diff --git a/src/gtest/test_keys.cpp b/src/gtest/test_keys.cpp index 4b4ff8817f2..988b2bfda08 100644 --- a/src/gtest/test_keys.cpp +++ b/src/gtest/test_keys.cpp @@ -3,15 +3,15 @@ #include #include +#include "utiltest.h" + #include TEST(Keys, EncodeAndDecodeSapling) { SelectParams(CBaseChainParams::MAIN); - std::vector> rawSeed(32); - HDSeed seed(rawSeed); - auto m = libzcash::SaplingExtendedSpendingKey::Master(seed); + auto m = GetMasterSaplingSpendingKey(); for (uint32_t i = 0; i < 1000; i++) { auto sk = m.Derive(i); diff --git a/src/gtest/test_keystore.cpp b/src/gtest/test_keystore.cpp index ccf9cb9babd..9874c59ad74 100644 --- a/src/gtest/test_keystore.cpp +++ b/src/gtest/test_keystore.cpp @@ -7,6 +7,7 @@ #ifdef ENABLE_WALLET #include "wallet/crypter.h" #endif +#include "utiltest.h" #include "zcash/Address.hpp" #include "zcash/zip32.h" @@ -198,9 +199,7 @@ TEST(keystore_tests, StoreAndRetrieveSaplingSpendingKey) { libzcash::SaplingFullViewingKey fvkOut; libzcash::SaplingIncomingViewingKey ivkOut; - std::vector> rawSeed(32); - HDSeed seed(rawSeed); - auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed); + auto sk = GetMasterSaplingSpendingKey(); auto fvk = sk.expsk.full_viewing_key(); auto ivk = fvk.in_viewing_key(); auto addr = sk.DefaultAddress(); diff --git a/src/test/key_tests.cpp b/src/test/key_tests.cpp index 220d6a9b7f7..bb9371103b9 100644 --- a/src/test/key_tests.cpp +++ b/src/test/key_tests.cpp @@ -10,6 +10,7 @@ #include "uint256.h" #include "util.h" #include "utilstrencodings.h" +#include "utiltest.h" #include "test/test_bitcoin.h" #include "zcash/Address.hpp" @@ -225,9 +226,7 @@ BOOST_AUTO_TEST_CASE(zs_address_test) { SelectParams(CBaseChainParams::REGTEST); - std::vector> rawSeed(32); - HDSeed seed(rawSeed); - auto m = libzcash::SaplingExtendedSpendingKey::Master(seed); + auto m = GetMasterSaplingSpendingKey(); for (uint32_t i = 0; i < 1000; i++) { auto sk = m.Derive(i); diff --git a/src/test/rpc_wallet_tests.cpp b/src/test/rpc_wallet_tests.cpp index 8fa886bea98..dc9c36e0ba5 100644 --- a/src/test/rpc_wallet_tests.cpp +++ b/src/test/rpc_wallet_tests.cpp @@ -563,9 +563,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importexport) pwalletMain->GetSaplingPaymentAddresses(saplingAddrs); BOOST_CHECK(saplingAddrs.empty()); - std::vector> rawSeed(32); - HDSeed seed(rawSeed); - auto m = libzcash::SaplingExtendedSpendingKey::Master(seed); + auto m = GetMasterSaplingSpendingKey(); // verify import and export key for (int i = 0; i < n1; i++) { diff --git a/src/utiltest.cpp b/src/utiltest.cpp index ac02d5a68f6..c8bf6d5067b 100644 --- a/src/utiltest.cpp +++ b/src/utiltest.cpp @@ -171,6 +171,12 @@ void DeactivateSapling() { // SelectParams(CBaseChainParams::MAIN); } +libzcash::SaplingExtendedSpendingKey GetMasterSaplingSpendingKey() { + std::vector> rawSeed(32); + HDSeed seed(rawSeed); + return libzcash::SaplingExtendedSpendingKey::Master(seed); +} + TestSaplingNote GetTestSaplingNote(const libzcash::SaplingPaymentAddress& pa, CAmount value) { // Generate dummy Sapling note libzcash::SaplingNote note(pa, value); @@ -188,7 +194,7 @@ CWalletTx GetValidSaplingTx(const Consensus::Params& consensusParams, auto pa = sk.DefaultAddress(); auto testNote = GetTestSaplingNote(pa, value); - + auto builder = TransactionBuilder(consensusParams, 1); builder.SetFee(0); builder.AddSaplingSpend(expsk, testNote.note, testNote.tree.root(), testNote.tree.witness()); diff --git a/src/utiltest.h b/src/utiltest.h index 6207e3accc9..86628f1d8ff 100644 --- a/src/utiltest.h +++ b/src/utiltest.h @@ -32,6 +32,8 @@ const Consensus::Params& ActivateSapling(); void DeactivateSapling(); +libzcash::SaplingExtendedSpendingKey GetMasterSaplingSpendingKey(); + /** * Generate a dummy SaplingNote and a SaplingMerkleTree with that note's commitment. */ diff --git a/src/wallet/gtest/test_wallet.cpp b/src/wallet/gtest/test_wallet.cpp index a980429521c..0bc17cc8c19 100644 --- a/src/wallet/gtest/test_wallet.cpp +++ b/src/wallet/gtest/test_wallet.cpp @@ -360,9 +360,7 @@ TEST(WalletTests, SetSaplingNoteAddrsInCWalletTx) { TestWallet wallet; - std::vector> rawSeed(32); - HDSeed seed(rawSeed); - auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed); + auto sk = GetMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto ivk = fvk.in_viewing_key(); @@ -477,9 +475,7 @@ TEST(WalletTests, FindMySaplingNotes) { TestWallet wallet; // Generate dummy Sapling address - std::vector> rawSeed(32); - HDSeed seed(rawSeed); - auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed); + auto sk = GetMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto pa = sk.DefaultAddress(); @@ -601,9 +597,7 @@ TEST(WalletTests, GetConflictedSaplingNotes) { TestWallet wallet; // Generate Sapling address - std::vector> rawSeed(32); - HDSeed seed(rawSeed); - auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed); + auto sk = GetMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto ivk = fvk.in_viewing_key(); @@ -760,9 +754,7 @@ TEST(WalletTests, SaplingNullifierIsSpent) { TestWallet wallet; // Generate dummy Sapling address - std::vector> rawSeed(32); - HDSeed seed(rawSeed); - auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed); + auto sk = GetMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto pa = sk.DefaultAddress(); @@ -845,9 +837,7 @@ TEST(WalletTests, NavigateFromSaplingNullifierToNote) { TestWallet wallet; // Generate dummy Sapling address - std::vector> rawSeed(32); - HDSeed seed(rawSeed); - auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed); + auto sk = GetMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto pa = sk.DefaultAddress(); @@ -968,9 +958,7 @@ TEST(WalletTests, SpentSaplingNoteIsFromMe) { TestWallet wallet; // Generate Sapling address - std::vector> rawSeed(32); - HDSeed seed(rawSeed); - auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed); + auto sk = GetMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto ivk = fvk.in_viewing_key(); @@ -1744,9 +1732,7 @@ TEST(WalletTests, UpdatedSaplingNoteData) { TestWallet wallet; - std::vector> rawSeed(32); - HDSeed seed(rawSeed); - auto m = libzcash::SaplingExtendedSpendingKey::Master(seed); + auto m = GetMasterSaplingSpendingKey(); // Generate dummy Sapling address auto sk = m.Derive(0); @@ -1888,9 +1874,7 @@ TEST(WalletTests, MarkAffectedSaplingTransactionsDirty) { TestWallet wallet; // Generate Sapling address - std::vector> rawSeed(32); - HDSeed seed(rawSeed); - auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed); + auto sk = GetMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto ivk = fvk.in_viewing_key(); diff --git a/src/zcbenchmarks.cpp b/src/zcbenchmarks.cpp index 89eed958906..f9567abf83d 100644 --- a/src/zcbenchmarks.cpp +++ b/src/zcbenchmarks.cpp @@ -302,9 +302,7 @@ double benchmark_try_decrypt_sapling_notes(size_t nAddrs) // Set params auto consensusParams = ActivateSapling(); - std::vector> rawSeed(32); - HDSeed seed(rawSeed); - auto masterKey = libzcash::SaplingExtendedSpendingKey::Master(seed); + auto masterKey = GetMasterSaplingSpendingKey(); CWallet wallet; wallet.AddSaplingSpendingKey(masterKey, masterKey.DefaultAddress()); From edac39b8fdfe2621b26e9458e0e2b9209a03017d Mon Sep 17 00:00:00 2001 From: Eirik Ogilvie-Wigley Date: Tue, 6 Nov 2018 23:36:05 -0700 Subject: [PATCH 06/12] Include Sapling transactions in increment note witness benchmark --- src/zcbenchmarks.cpp | 91 ++++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 25 deletions(-) diff --git a/src/zcbenchmarks.cpp b/src/zcbenchmarks.cpp index f9567abf83d..08d46235653 100644 --- a/src/zcbenchmarks.cpp +++ b/src/zcbenchmarks.cpp @@ -327,31 +327,72 @@ double benchmark_try_decrypt_sapling_notes(size_t nAddrs) return tv_stop; } +CWalletTx CreateSproutTxWithNoteData(const libzcash::SproutSpendingKey& sk) { + auto wtx = GetValidSproutReceive(*pzcashParams, sk, 10, true); + auto note = GetSproutNote(*pzcashParams, sk, wtx, 0, 1); + auto nullifier = note.nullifier(sk); + + mapSproutNoteData_t noteDataMap; + JSOutPoint jsoutpt {wtx.GetHash(), 0, 1}; + SproutNoteData nd {sk.address(), nullifier}; + noteDataMap[jsoutpt] = nd; + + wtx.SetSproutNoteData(noteDataMap); + + return wtx; +} + +CWalletTx CreateSaplingTxWithNoteData(const Consensus::Params& consensusParams, + const libzcash::SaplingExtendedSpendingKey &sk) { + auto wtx = GetValidSaplingTx(consensusParams, sk, 10); + auto testNote = GetTestSaplingNote(sk.DefaultAddress(), 10); + auto fvk = sk.expsk.full_viewing_key(); + auto nullifier = testNote.note.nullifier(fvk, testNote.tree.witness().position()).get(); + + mapSaplingNoteData_t noteDataMap; + SaplingOutPoint outPoint {wtx.GetHash(), 0}; + auto ivk = fvk.in_viewing_key(); + SaplingNoteData noteData {ivk, nullifier}; + noteDataMap[outPoint] = noteData; + + wtx.SetSaplingNoteData(noteDataMap); + + return wtx; +} + double benchmark_increment_note_witnesses(size_t nTxs) { + auto consensusParams = ActivateSapling(); + CWallet wallet; SproutMerkleTree sproutTree; SaplingMerkleTree saplingTree; - auto sk = libzcash::SproutSpendingKey::random(); - wallet.AddSproutSpendingKey(sk); + auto sproutSpendingKey = libzcash::SproutSpendingKey::random(); + wallet.AddSproutSpendingKey(sproutSpendingKey); - // First block - CBlock block1; - for (int i = 0; i < nTxs; i++) { - auto wtx = GetValidSproutReceive(*pzcashParams, sk, 10, true); - auto note = GetSproutNote(*pzcashParams, sk, wtx, 0, 1); - auto nullifier = note.nullifier(sk); + auto saplingSpendingKey = GetMasterSaplingSpendingKey(); + wallet.AddSaplingSpendingKey(saplingSpendingKey, saplingSpendingKey.DefaultAddress()); - mapSproutNoteData_t noteData; - JSOutPoint jsoutpt {wtx.GetHash(), 0, 1}; - SproutNoteData nd {sk.address(), nullifier}; - noteData[jsoutpt] = nd; + // Half Sprout and half Sapling txs (+1 for Sapling if nTxs is odd) + size_t numSproutTxs = nTxs / 2; + size_t numSaplingTxs = nTxs - numSproutTxs; - wtx.SetSproutNoteData(noteData); + // First block + CBlock block1; + // Sprout + for (int i = 0; i < numSproutTxs; ++i) { + auto wtx = CreateSproutTxWithNoteData(sproutSpendingKey); + wallet.AddToWallet(wtx, true, NULL); + block1.vtx.push_back(wtx); + } + // Sapling + for (int i = 0; i < numSaplingTxs; ++i) { + auto wtx = CreateSaplingTxWithNoteData(consensusParams, saplingSpendingKey); wallet.AddToWallet(wtx, true, NULL); block1.vtx.push_back(wtx); } + CBlockIndex index1(block1); index1.nHeight = 1; @@ -362,26 +403,26 @@ double benchmark_increment_note_witnesses(size_t nTxs) CBlock block2; block2.hashPrevBlock = block1.GetHash(); { - auto wtx = GetValidSproutReceive(*pzcashParams, sk, 10, true); - auto note = GetSproutNote(*pzcashParams, sk, wtx, 0, 1); - auto nullifier = note.nullifier(sk); + auto sproutTx = CreateSproutTxWithNoteData(sproutSpendingKey); + wallet.AddToWallet(sproutTx, true, NULL); + block2.vtx.push_back(sproutTx); - mapSproutNoteData_t noteData; - JSOutPoint jsoutpt {wtx.GetHash(), 0, 1}; - SproutNoteData nd {sk.address(), nullifier}; - noteData[jsoutpt] = nd; - - wtx.SetSproutNoteData(noteData); - wallet.AddToWallet(wtx, true, NULL); - block2.vtx.push_back(wtx); + auto saplingTx = CreateSaplingTxWithNoteData(consensusParams, saplingSpendingKey); + wallet.AddToWallet(saplingTx, true, NULL); + block1.vtx.push_back(saplingTx); } + CBlockIndex index2(block2); index2.nHeight = 2; struct timeval tv_start; timer_start(tv_start); wallet.ChainTip(&index2, &block2, sproutTree, saplingTree, true); - return timer_stop(tv_start); + double tv_stop = timer_stop(tv_start); + + DeactivateSapling(); + + return tv_stop; } // Fake the input of a given block From b27ac486e4e888eb7452a1a1838fe5ff42bf3794 Mon Sep 17 00:00:00 2001 From: Eirik Ogilvie-Wigley Date: Tue, 6 Nov 2018 23:47:33 -0700 Subject: [PATCH 07/12] Prevent header from being included multiple times --- src/utiltest.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/utiltest.h b/src/utiltest.h index 86628f1d8ff..d14dca260d8 100644 --- a/src/utiltest.h +++ b/src/utiltest.h @@ -2,6 +2,9 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#ifndef ZCASH_UTIL_TEST_H +#define ZCASH_UTIL_TEST_H + #include "wallet/wallet.h" #include "zcash/JoinSplit.hpp" #include "zcash/Note.hpp" @@ -42,3 +45,5 @@ TestSaplingNote GetTestSaplingNote(const libzcash::SaplingPaymentAddress& pa, CA CWalletTx GetValidSaplingTx(const Consensus::Params& consensusParams, const libzcash::SaplingExtendedSpendingKey &sk, CAmount value); + +#endif // ZCASH_UTIL_TEST_H From a6a17c45b82cdfba482b06623ca626d896488a5f Mon Sep 17 00:00:00 2001 From: Eirik Ogilvie-Wigley Date: Wed, 7 Nov 2018 13:44:57 -0700 Subject: [PATCH 08/12] benchmarks do not require updating network parameters --- src/utiltest.cpp | 2 -- src/zcbenchmarks.cpp | 17 ++++------------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/src/utiltest.cpp b/src/utiltest.cpp index c8bf6d5067b..8f1e64b27bd 100644 --- a/src/utiltest.cpp +++ b/src/utiltest.cpp @@ -167,8 +167,6 @@ const Consensus::Params& ActivateSapling() { void DeactivateSapling() { UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); - // Consider: Should we be doing the following? - // SelectParams(CBaseChainParams::MAIN); } libzcash::SaplingExtendedSpendingKey GetMasterSaplingSpendingKey() { diff --git a/src/zcbenchmarks.cpp b/src/zcbenchmarks.cpp index 08d46235653..3fb38688c97 100644 --- a/src/zcbenchmarks.cpp +++ b/src/zcbenchmarks.cpp @@ -300,7 +300,7 @@ double benchmark_try_decrypt_sprout_notes(size_t nAddrs) double benchmark_try_decrypt_sapling_notes(size_t nAddrs) { // Set params - auto consensusParams = ActivateSapling(); + auto consensusParams = Params().GetConsensus(); auto masterKey = GetMasterSaplingSpendingKey(); @@ -319,12 +319,7 @@ double benchmark_try_decrypt_sapling_notes(size_t nAddrs) struct timeval tv_start; timer_start(tv_start); auto saplingNoteDataAndAddressesToAdd = wallet.FindMySaplingNotes(tx); - double tv_stop = timer_stop(tv_start); - - // Revert to default - DeactivateSapling(); - - return tv_stop; + return timer_stop(tv_start); } CWalletTx CreateSproutTxWithNoteData(const libzcash::SproutSpendingKey& sk) { @@ -362,7 +357,7 @@ CWalletTx CreateSaplingTxWithNoteData(const Consensus::Params& consensusParams, double benchmark_increment_note_witnesses(size_t nTxs) { - auto consensusParams = ActivateSapling(); + auto consensusParams = Params().GetConsensus(); CWallet wallet; SproutMerkleTree sproutTree; @@ -418,11 +413,7 @@ double benchmark_increment_note_witnesses(size_t nTxs) struct timeval tv_start; timer_start(tv_start); wallet.ChainTip(&index2, &block2, sproutTree, saplingTree, true); - double tv_stop = timer_stop(tv_start); - - DeactivateSapling(); - - return tv_stop; + return timer_stop(tv_start); } // Fake the input of a given block From 010481c7e5af3187e2b3670d3df4f4227324f493 Mon Sep 17 00:00:00 2001 From: Eirik Ogilvie-Wigley Date: Wed, 7 Nov 2018 19:05:25 -0700 Subject: [PATCH 09/12] FakeCoinsViewDB can inherit directly from CCoinsView --- src/zcbenchmarks.cpp | 54 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/src/zcbenchmarks.cpp b/src/zcbenchmarks.cpp index 3fb38688c97..d6b161b1a16 100644 --- a/src/zcbenchmarks.cpp +++ b/src/zcbenchmarks.cpp @@ -417,16 +417,33 @@ double benchmark_increment_note_witnesses(size_t nTxs) } // Fake the input of a given block -class FakeCoinsViewDB : public CCoinsViewDB { +// This class is based on the class CCoinsViewDB, but with limited functionality. +// The construtor and the functions `GetCoins` and `HaveCoins` come directly from +// CCoinsViewDB, but the rest are either mocks and/or don't really do anything. +class FakeCoinsViewDB : public CCoinsView { + // The following constant is a duplicate of the one found in txdb.cpp + static const char DB_COINS = 'c'; + + CDBWrapper db; + uint256 hash; - SproutMerkleTree t; + SproutMerkleTree sproutTree; + SaplingMerkleTree saplingTree; public: - FakeCoinsViewDB(std::string dbName, uint256& hash) : CCoinsViewDB(dbName, 100, false, false), hash(hash) {} + FakeCoinsViewDB(std::string dbName, uint256& hash) : db(GetDataDir() / dbName, 100, false, false), hash(hash) {} + + bool GetSproutAnchorAt(const uint256 &rt, SproutMerkleTree &tree) const { + if (rt == sproutTree.root()) { + tree = sproutTree; + return true; + } + return false; + } - bool GetAnchorAt(const uint256 &rt, SproutMerkleTree &tree) const { - if (rt == t.root()) { - tree = t; + bool GetSaplingAnchorAt(const uint256 &rt, SaplingMerkleTree &tree) const { + if (rt == saplingTree.root()) { + tree = saplingTree; return true; } return false; @@ -436,20 +453,37 @@ class FakeCoinsViewDB : public CCoinsViewDB { return false; } + bool GetCoins(const uint256 &txid, CCoins &coins) const { + return db.Read(std::make_pair(DB_COINS, txid), coins); + } + + bool HaveCoins(const uint256 &txid) const { + return db.Exists(std::make_pair(DB_COINS, txid)); + } + uint256 GetBestBlock() const { return hash; } - uint256 GetBestAnchor() const { - return t.root(); + uint256 GetBestAnchor(ShieldedType type) const { + switch (type) { + case SPROUT: + return sproutTree.root(); + case SAPLING: + return saplingTree.root(); + default: + throw new std::runtime_error("Unknown shielded type"); + } } bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, - const uint256 &hashAnchor, + const uint256 &hashSproutAnchor, + const uint256 &hashSaplingAnchor, CAnchorsSproutMap &mapSproutAnchors, + CAnchorsSaplingMap &mapSaplingAnchors, CNullifiersMap &mapSproutNullifiers, - CNullifiersMap& mapSaplingNullifiers) { + CNullifiersMap &mapSaplingNullifiers) { return false; } From 275bd8f67a79eb3130b6c5a943fb437c858fcefe Mon Sep 17 00:00:00 2001 From: Eirik Ogilvie-Wigley Date: Mon, 12 Nov 2018 17:48:04 -0700 Subject: [PATCH 10/12] Add a method for generating a test CKey --- src/gtest/test_transaction_builder.cpp | 11 +++-------- src/utiltest.cpp | 6 ++++++ src/utiltest.h | 5 +++++ src/wallet/gtest/test_wallet.cpp | 8 ++------ 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/gtest/test_transaction_builder.cpp b/src/gtest/test_transaction_builder.cpp index 3fa78d75610..0ef8690210c 100644 --- a/src/gtest/test_transaction_builder.cpp +++ b/src/gtest/test_transaction_builder.cpp @@ -12,15 +12,12 @@ #include #include -static const std::string tSecretRegtest = "cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN"; - TEST(TransactionBuilder, Invoke) { auto consensusParams = ActivateSapling(); CBasicKeyStore keystore; - CKey tsk = DecodeSecret(tSecretRegtest); - keystore.AddKey(tsk); + CKey tsk = AddCKeyToKeyStore(keystore); auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID()); auto sk_from = libzcash::SaplingSpendingKey::random(); @@ -131,8 +128,7 @@ TEST(TransactionBuilder, FailsWithNegativeChange) // Set up dummy transparent address CBasicKeyStore keystore; - CKey tsk = DecodeSecret(tSecretRegtest); - keystore.AddKey(tsk); + CKey tsk = AddCKeyToKeyStore(keystore); auto tkeyid = tsk.GetPubKey().GetID(); auto scriptPubKey = GetScriptForDestination(tkeyid); CTxDestination taddr = tkeyid; @@ -182,8 +178,7 @@ TEST(TransactionBuilder, ChangeOutput) // Set up dummy transparent address CBasicKeyStore keystore; - CKey tsk = DecodeSecret(tSecretRegtest); - keystore.AddKey(tsk); + CKey tsk = AddCKeyToKeyStore(keystore); auto tkeyid = tsk.GetPubKey().GetID(); auto scriptPubKey = GetScriptForDestination(tkeyid); CTxDestination taddr = tkeyid; diff --git a/src/utiltest.cpp b/src/utiltest.cpp index 8f1e64b27bd..2e5d94796c7 100644 --- a/src/utiltest.cpp +++ b/src/utiltest.cpp @@ -175,6 +175,12 @@ libzcash::SaplingExtendedSpendingKey GetMasterSaplingSpendingKey() { return libzcash::SaplingExtendedSpendingKey::Master(seed); } +CKey AddCKeyToKeyStore(CBasicKeyStore& keyStore) { + CKey tsk = DecodeSecret(T_SECRET_REGTEST); + keyStore.AddKey(tsk); + return tsk; +} + TestSaplingNote GetTestSaplingNote(const libzcash::SaplingPaymentAddress& pa, CAmount value) { // Generate dummy Sapling note libzcash::SaplingNote note(pa, value); diff --git a/src/utiltest.h b/src/utiltest.h index d14dca260d8..0dd0972bc1c 100644 --- a/src/utiltest.h +++ b/src/utiltest.h @@ -5,6 +5,7 @@ #ifndef ZCASH_UTIL_TEST_H #define ZCASH_UTIL_TEST_H +#include "key_io.h" #include "wallet/wallet.h" #include "zcash/JoinSplit.hpp" #include "zcash/Note.hpp" @@ -26,6 +27,8 @@ CWalletTx GetValidSproutSpend(ZCJoinSplit& params, CAmount value); // Sapling +static const std::string T_SECRET_REGTEST = "cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN"; + struct TestSaplingNote { libzcash::SaplingNote note; SaplingMerkleTree tree; @@ -37,6 +40,8 @@ void DeactivateSapling(); libzcash::SaplingExtendedSpendingKey GetMasterSaplingSpendingKey(); +CKey AddCKeyToKeyStore(CBasicKeyStore& keyStore); + /** * Generate a dummy SaplingNote and a SaplingMerkleTree with that note's commitment. */ diff --git a/src/wallet/gtest/test_wallet.cpp b/src/wallet/gtest/test_wallet.cpp index 0bc17cc8c19..8ab52ddd7c5 100644 --- a/src/wallet/gtest/test_wallet.cpp +++ b/src/wallet/gtest/test_wallet.cpp @@ -25,8 +25,6 @@ ACTION(ThrowLogicError) { throw std::logic_error("Boom"); } -static const std::string tSecretRegtest = "cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN"; - class MockWalletDB { public: MOCK_METHOD0(TxnBegin, bool()); @@ -1571,8 +1569,7 @@ TEST(WalletTests, SetBestChainIgnoresTxsWithoutShieldedData) { CBlockLocator loc; // Set up transparent address - CKey tsk = DecodeSecret(tSecretRegtest); - wallet.AddKey(tsk); + CKey tsk = AddCKeyToKeyStore(wallet); auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID()); // Set up a Sprout address @@ -1885,8 +1882,7 @@ TEST(WalletTests, MarkAffectedSaplingTransactionsDirty) { // Set up transparent address CBasicKeyStore keystore; - CKey tsk = DecodeSecret(tSecretRegtest); - keystore.AddKey(tsk); + CKey tsk = AddCKeyToKeyStore(keystore); auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID()); // Generate shielding tx from transparent to Sapling From 3e5cc59c2ea00f74e9a2d1ef6b60830d5ac5fb3e Mon Sep 17 00:00:00 2001 From: Eirik Ogilvie-Wigley Date: Mon, 12 Nov 2018 19:02:53 -0700 Subject: [PATCH 11/12] Change to t->z transaction and create separate benchmark for sapling --- src/utiltest.cpp | 23 ++++++---- src/utiltest.h | 7 +-- src/wallet/rpcwallet.cpp | 13 +++--- src/zcbenchmarks.cpp | 95 +++++++++++++++++++++++++++------------- src/zcbenchmarks.h | 3 +- 5 files changed, 92 insertions(+), 49 deletions(-) diff --git a/src/utiltest.cpp b/src/utiltest.cpp index 2e5d94796c7..f7a69b8de28 100644 --- a/src/utiltest.cpp +++ b/src/utiltest.cpp @@ -50,6 +50,9 @@ CWalletTx GetValidSproutReceive(ZCJoinSplit& params, inputs, outputs, 2*value, 0, false}; mtx.vjoinsplit.push_back(jsdesc); + // Consider: The following is a bit misleading (given the name of this function) + // and should perhaps be changed, but currently a few tests in test_wallet.cpp + // depend on this happening. if (version >= 4) { // Shielded Output OutputDescription od; @@ -190,18 +193,20 @@ TestSaplingNote GetTestSaplingNote(const libzcash::SaplingPaymentAddress& pa, CA return { note, tree }; } -CWalletTx GetValidSaplingTx(const Consensus::Params& consensusParams, - const libzcash::SaplingExtendedSpendingKey &sk, - CAmount value) { - auto expsk = sk.expsk; - auto fvk = expsk.full_viewing_key(); +CWalletTx GetValidSaplingReceive(const Consensus::Params& consensusParams, + CBasicKeyStore& keyStore, + const libzcash::SaplingExtendedSpendingKey &sk, + CAmount value) { + // From taddr + CKey tsk = AddCKeyToKeyStore(keyStore); + auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID()); + // To zaddr + auto fvk = sk.expsk.full_viewing_key(); auto pa = sk.DefaultAddress(); - auto testNote = GetTestSaplingNote(pa, value); - - auto builder = TransactionBuilder(consensusParams, 1); + auto builder = TransactionBuilder(consensusParams, 1, &keyStore); builder.SetFee(0); - builder.AddSaplingSpend(expsk, testNote.note, testNote.tree.root(), testNote.tree.witness()); + builder.AddTransparentInput(COutPoint(), scriptPubKey, value); builder.AddSaplingOutput(fvk.ovk, pa, value, {}); CTransaction tx = builder.Build().GetTxOrThrow(); diff --git a/src/utiltest.h b/src/utiltest.h index 0dd0972bc1c..828bf2bcad8 100644 --- a/src/utiltest.h +++ b/src/utiltest.h @@ -47,8 +47,9 @@ CKey AddCKeyToKeyStore(CBasicKeyStore& keyStore); */ TestSaplingNote GetTestSaplingNote(const libzcash::SaplingPaymentAddress& pa, CAmount value); -CWalletTx GetValidSaplingTx(const Consensus::Params& consensusParams, - const libzcash::SaplingExtendedSpendingKey &sk, - CAmount value); +CWalletTx GetValidSaplingReceive(const Consensus::Params& consensusParams, + CBasicKeyStore& keyStore, + const libzcash::SaplingExtendedSpendingKey &sk, + CAmount value); #endif // ZCASH_UTIL_TEST_H diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 051a70734f1..e2c558ace2f 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2777,14 +2777,17 @@ UniValue zc_benchmark(const UniValue& params, bool fHelp) } sample_times.push_back(benchmark_large_tx(nInputs)); } else if (benchmarktype == "trydecryptnotes") { - int nAddrs = params[2].get_int(); - sample_times.push_back(benchmark_try_decrypt_sprout_notes(nAddrs)); + int nKeys = params[2].get_int(); + sample_times.push_back(benchmark_try_decrypt_sprout_notes(nKeys)); } else if (benchmarktype == "trydecryptsaplingnotes") { - int nAddrs = params[2].get_int(); - sample_times.push_back(benchmark_try_decrypt_sapling_notes(nAddrs)); + int nKeys = params[2].get_int(); + sample_times.push_back(benchmark_try_decrypt_sapling_notes(nKeys)); } else if (benchmarktype == "incnotewitnesses") { int nTxs = params[2].get_int(); - sample_times.push_back(benchmark_increment_note_witnesses(nTxs)); + sample_times.push_back(benchmark_increment_note_witnesses_sprout(nTxs)); + } else if (benchmarktype == "incnotewitnessessapling") { + int nTxs = params[2].get_int(); + sample_times.push_back(benchmark_increment_note_witnesses_sapling(nTxs)); } else if (benchmarktype == "connectblockslow") { if (Params().NetworkIDString() != "regtest") { throw JSONRPCError(RPC_TYPE_ERROR, "Benchmark must be run in regtest mode"); diff --git a/src/zcbenchmarks.cpp b/src/zcbenchmarks.cpp index d6b161b1a16..b178081e3a9 100644 --- a/src/zcbenchmarks.cpp +++ b/src/zcbenchmarks.cpp @@ -280,10 +280,15 @@ double benchmark_large_tx(size_t nInputs) return timer_stop(tv_start); } -double benchmark_try_decrypt_sprout_notes(size_t nAddrs) +// The two benchmarks, try_decrypt_sprout_notes and try_decrypt_sapling_notes, +// are checking a worst-case scenarios. In both we add n keys to a wallet, +// create a transaction using a key not in our original list of n, and then +// check that the transaction is not associated with any of the keys in our +// wallet. We call assert(...) to ensure that this is true. +double benchmark_try_decrypt_sprout_notes(size_t nKeys) { CWallet wallet; - for (int i = 0; i < nAddrs; i++) { + for (int i = 0; i < nKeys; i++) { auto sk = libzcash::SproutSpendingKey::random(); wallet.AddSproutSpendingKey(sk); } @@ -293,11 +298,13 @@ double benchmark_try_decrypt_sprout_notes(size_t nAddrs) struct timeval tv_start; timer_start(tv_start); - auto nd = wallet.FindMySproutNotes(tx); + auto noteDataMap = wallet.FindMySproutNotes(tx); + + assert(noteDataMap.empty()); return timer_stop(tv_start); } -double benchmark_try_decrypt_sapling_notes(size_t nAddrs) +double benchmark_try_decrypt_sapling_notes(size_t nKeys) { // Set params auto consensusParams = Params().GetConsensus(); @@ -305,20 +312,20 @@ double benchmark_try_decrypt_sapling_notes(size_t nAddrs) auto masterKey = GetMasterSaplingSpendingKey(); CWallet wallet; - wallet.AddSaplingSpendingKey(masterKey, masterKey.DefaultAddress()); int i; - for (i = 0; i < nAddrs; i++) { + for (i = 0; i < nKeys; i++) { auto sk = masterKey.Derive(i); wallet.AddSaplingSpendingKey(sk, sk.DefaultAddress()); } auto sk = masterKey.Derive(i); - auto tx = GetValidSaplingTx(consensusParams, sk, 10); + auto tx = GetValidSaplingReceive(consensusParams, wallet, sk, 10); struct timeval tv_start; timer_start(tv_start); - auto saplingNoteDataAndAddressesToAdd = wallet.FindMySaplingNotes(tx); + auto noteDataMapAndAddressesToAdd = wallet.FindMySaplingNotes(tx); + assert(noteDataMapAndAddressesToAdd.first.empty()); return timer_stop(tv_start); } @@ -337,9 +344,53 @@ CWalletTx CreateSproutTxWithNoteData(const libzcash::SproutSpendingKey& sk) { return wtx; } +double benchmark_increment_note_witnesses_sprout(size_t nTxs) +{ + auto consensusParams = Params().GetConsensus(); + + CWallet wallet; + SproutMerkleTree sproutTree; + SaplingMerkleTree saplingTree; + + auto sproutSpendingKey = libzcash::SproutSpendingKey::random(); + wallet.AddSproutSpendingKey(sproutSpendingKey); + + // First block + CBlock block1; + for (int i = 0; i < nTxs; ++i) { + auto wtx = CreateSproutTxWithNoteData(sproutSpendingKey); + wallet.AddToWallet(wtx, true, NULL); + block1.vtx.push_back(wtx); + } + + CBlockIndex index1(block1); + index1.nHeight = 1; + + // Increment to get transactions witnessed + wallet.ChainTip(&index1, &block1, sproutTree, saplingTree, true); + + // Second block + CBlock block2; + block2.hashPrevBlock = block1.GetHash(); + { + auto sproutTx = CreateSproutTxWithNoteData(sproutSpendingKey); + wallet.AddToWallet(sproutTx, true, NULL); + block2.vtx.push_back(sproutTx); + } + + CBlockIndex index2(block2); + index2.nHeight = 2; + + struct timeval tv_start; + timer_start(tv_start); + wallet.ChainTip(&index2, &block2, sproutTree, saplingTree, true); + return timer_stop(tv_start); +} + CWalletTx CreateSaplingTxWithNoteData(const Consensus::Params& consensusParams, + CBasicKeyStore& keyStore, const libzcash::SaplingExtendedSpendingKey &sk) { - auto wtx = GetValidSaplingTx(consensusParams, sk, 10); + auto wtx = GetValidSaplingReceive(consensusParams, keyStore, sk, 10); auto testNote = GetTestSaplingNote(sk.DefaultAddress(), 10); auto fvk = sk.expsk.full_viewing_key(); auto nullifier = testNote.note.nullifier(fvk, testNote.tree.witness().position()).get(); @@ -355,7 +406,7 @@ CWalletTx CreateSaplingTxWithNoteData(const Consensus::Params& consensusParams, return wtx; } -double benchmark_increment_note_witnesses(size_t nTxs) +double benchmark_increment_note_witnesses_sapling(size_t nTxs) { auto consensusParams = Params().GetConsensus(); @@ -363,27 +414,13 @@ double benchmark_increment_note_witnesses(size_t nTxs) SproutMerkleTree sproutTree; SaplingMerkleTree saplingTree; - auto sproutSpendingKey = libzcash::SproutSpendingKey::random(); - wallet.AddSproutSpendingKey(sproutSpendingKey); - auto saplingSpendingKey = GetMasterSaplingSpendingKey(); wallet.AddSaplingSpendingKey(saplingSpendingKey, saplingSpendingKey.DefaultAddress()); - // Half Sprout and half Sapling txs (+1 for Sapling if nTxs is odd) - size_t numSproutTxs = nTxs / 2; - size_t numSaplingTxs = nTxs - numSproutTxs; - // First block CBlock block1; - // Sprout - for (int i = 0; i < numSproutTxs; ++i) { - auto wtx = CreateSproutTxWithNoteData(sproutSpendingKey); - wallet.AddToWallet(wtx, true, NULL); - block1.vtx.push_back(wtx); - } - // Sapling - for (int i = 0; i < numSaplingTxs; ++i) { - auto wtx = CreateSaplingTxWithNoteData(consensusParams, saplingSpendingKey); + for (int i = 0; i < nTxs; ++i) { + auto wtx = CreateSaplingTxWithNoteData(consensusParams, wallet, saplingSpendingKey); wallet.AddToWallet(wtx, true, NULL); block1.vtx.push_back(wtx); } @@ -398,11 +435,7 @@ double benchmark_increment_note_witnesses(size_t nTxs) CBlock block2; block2.hashPrevBlock = block1.GetHash(); { - auto sproutTx = CreateSproutTxWithNoteData(sproutSpendingKey); - wallet.AddToWallet(sproutTx, true, NULL); - block2.vtx.push_back(sproutTx); - - auto saplingTx = CreateSaplingTxWithNoteData(consensusParams, saplingSpendingKey); + auto saplingTx = CreateSaplingTxWithNoteData(consensusParams, wallet, saplingSpendingKey); wallet.AddToWallet(saplingTx, true, NULL); block1.vtx.push_back(saplingTx); } diff --git a/src/zcbenchmarks.h b/src/zcbenchmarks.h index 7d511fdcdee..257b2fe9ed4 100644 --- a/src/zcbenchmarks.h +++ b/src/zcbenchmarks.h @@ -15,7 +15,8 @@ extern double benchmark_verify_equihash(); extern double benchmark_large_tx(size_t nInputs); extern double benchmark_try_decrypt_sprout_notes(size_t nAddrs); extern double benchmark_try_decrypt_sapling_notes(size_t nAddrs); -extern double benchmark_increment_note_witnesses(size_t nTxs); +extern double benchmark_increment_note_witnesses_sprout(size_t nTxs); +extern double benchmark_increment_note_witnesses_sapling(size_t nTxs); extern double benchmark_connectblock_slow(); extern double benchmark_sendtoaddress(CAmount amount); extern double benchmark_loadwallet(); From 8a1d19307bae2ca627b62e916f345ba1d060ab84 Mon Sep 17 00:00:00 2001 From: Eirik Ogilvie-Wigley Date: Tue, 29 Jan 2019 21:18:10 -0700 Subject: [PATCH 12/12] Renaming and other minor cleanup --- src/gtest/test_checkblock.cpp | 2 +- src/gtest/test_checktransaction.cpp | 10 ++--- src/gtest/test_keys.cpp | 2 +- src/gtest/test_keystore.cpp | 2 +- src/gtest/test_transaction_builder.cpp | 22 +++++------ src/test/key_tests.cpp | 2 +- src/test/rpc_wallet_tests.cpp | 6 +-- src/utiltest.cpp | 10 ++--- src/utiltest.h | 8 ++-- src/wallet/gtest/test_wallet.cpp | 52 +++++++++++++------------- src/wallet/rpcwallet.cpp | 6 +-- src/zcbenchmarks.cpp | 16 ++++---- src/zcbenchmarks.h | 4 +- 13 files changed, 71 insertions(+), 71 deletions(-) diff --git a/src/gtest/test_checkblock.cpp b/src/gtest/test_checkblock.cpp index dec0e8b00a6..2919e1314d7 100644 --- a/src/gtest/test_checkblock.cpp +++ b/src/gtest/test_checkblock.cpp @@ -76,7 +76,7 @@ class ContextualCheckBlockTest : public ::testing::Test { virtual void TearDown() { // Revert to test default. No-op on mainnet params. - DeactivateSapling(); + RegtestDeactivateSapling(); } // Returns a valid but empty mutable transaction at block height 1. diff --git a/src/gtest/test_checktransaction.cpp b/src/gtest/test_checktransaction.cpp index 28b19897e8a..cf9d7744168 100644 --- a/src/gtest/test_checktransaction.cpp +++ b/src/gtest/test_checktransaction.cpp @@ -172,7 +172,7 @@ TEST(checktransaction_tests, BadTxnsOversize) { { // But should be fine again once Sapling activates! - ActivateSapling(); + RegtestActivateSapling(); mtx.fOverwintered = true; mtx.nVersionGroupId = SAPLING_VERSION_GROUP_ID; @@ -191,12 +191,12 @@ TEST(checktransaction_tests, BadTxnsOversize) { EXPECT_TRUE(ContextualCheckTransaction(tx, state, 1, 100)); // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } } TEST(checktransaction_tests, OversizeSaplingTxns) { - ActivateSapling(); + RegtestActivateSapling(); CMutableTransaction mtx = GetValidTransaction(); mtx.fOverwintered = true; @@ -249,7 +249,7 @@ TEST(checktransaction_tests, OversizeSaplingTxns) { } // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } TEST(checktransaction_tests, bad_txns_vout_negative) { @@ -965,7 +965,7 @@ TEST(checktransaction_tests, OverwinteredContextualCreateTx) { } // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } // Test a v1 transaction which has a malformed header, perhaps modified in-flight diff --git a/src/gtest/test_keys.cpp b/src/gtest/test_keys.cpp index 988b2bfda08..fba275f597b 100644 --- a/src/gtest/test_keys.cpp +++ b/src/gtest/test_keys.cpp @@ -11,7 +11,7 @@ TEST(Keys, EncodeAndDecodeSapling) { SelectParams(CBaseChainParams::MAIN); - auto m = GetMasterSaplingSpendingKey(); + auto m = GetTestMasterSaplingSpendingKey(); for (uint32_t i = 0; i < 1000; i++) { auto sk = m.Derive(i); diff --git a/src/gtest/test_keystore.cpp b/src/gtest/test_keystore.cpp index 9874c59ad74..7dee41f9823 100644 --- a/src/gtest/test_keystore.cpp +++ b/src/gtest/test_keystore.cpp @@ -199,7 +199,7 @@ TEST(keystore_tests, StoreAndRetrieveSaplingSpendingKey) { libzcash::SaplingFullViewingKey fvkOut; libzcash::SaplingIncomingViewingKey ivkOut; - auto sk = GetMasterSaplingSpendingKey(); + auto sk = GetTestMasterSaplingSpendingKey(); auto fvk = sk.expsk.full_viewing_key(); auto ivk = fvk.in_viewing_key(); auto addr = sk.DefaultAddress(); diff --git a/src/gtest/test_transaction_builder.cpp b/src/gtest/test_transaction_builder.cpp index 0ef8690210c..72aab05f717 100644 --- a/src/gtest/test_transaction_builder.cpp +++ b/src/gtest/test_transaction_builder.cpp @@ -14,10 +14,10 @@ TEST(TransactionBuilder, Invoke) { - auto consensusParams = ActivateSapling(); + auto consensusParams = RegtestActivateSapling(); CBasicKeyStore keystore; - CKey tsk = AddCKeyToKeyStore(keystore); + CKey tsk = AddTestCKeyToKeyStore(keystore); auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID()); auto sk_from = libzcash::SaplingSpendingKey::random(); @@ -82,7 +82,7 @@ TEST(TransactionBuilder, Invoke) EXPECT_EQ(state.GetRejectReason(), ""); // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } TEST(TransactionBuilder, ThrowsOnTransparentInputWithoutKeyStore) @@ -118,7 +118,7 @@ TEST(TransactionBuilder, RejectsInvalidTransparentChangeAddress) TEST(TransactionBuilder, FailsWithNegativeChange) { - auto consensusParams = ActivateSapling(); + auto consensusParams = RegtestActivateSapling(); // Generate dummy Sapling address auto sk = libzcash::SaplingSpendingKey::random(); @@ -128,7 +128,7 @@ TEST(TransactionBuilder, FailsWithNegativeChange) // Set up dummy transparent address CBasicKeyStore keystore; - CKey tsk = AddCKeyToKeyStore(keystore); + CKey tsk = AddTestCKeyToKeyStore(keystore); auto tkeyid = tsk.GetPubKey().GetID(); auto scriptPubKey = GetScriptForDestination(tkeyid); CTxDestination taddr = tkeyid; @@ -157,12 +157,12 @@ TEST(TransactionBuilder, FailsWithNegativeChange) EXPECT_TRUE(builder.Build().IsTx()); // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } TEST(TransactionBuilder, ChangeOutput) { - auto consensusParams = ActivateSapling(); + auto consensusParams = RegtestActivateSapling(); // Generate dummy Sapling address auto sk = libzcash::SaplingSpendingKey::random(); @@ -178,7 +178,7 @@ TEST(TransactionBuilder, ChangeOutput) // Set up dummy transparent address CBasicKeyStore keystore; - CKey tsk = AddCKeyToKeyStore(keystore); + CKey tsk = AddTestCKeyToKeyStore(keystore); auto tkeyid = tsk.GetPubKey().GetID(); auto scriptPubKey = GetScriptForDestination(tkeyid); CTxDestination taddr = tkeyid; @@ -237,12 +237,12 @@ TEST(TransactionBuilder, ChangeOutput) } // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } TEST(TransactionBuilder, SetFee) { - auto consensusParams = ActivateSapling(); + auto consensusParams = RegtestActivateSapling(); // Generate dummy Sapling address auto sk = libzcash::SaplingSpendingKey::random(); @@ -284,7 +284,7 @@ TEST(TransactionBuilder, SetFee) } // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } TEST(TransactionBuilder, CheckSaplingTxVersion) diff --git a/src/test/key_tests.cpp b/src/test/key_tests.cpp index bb9371103b9..64d7ade52d6 100644 --- a/src/test/key_tests.cpp +++ b/src/test/key_tests.cpp @@ -226,7 +226,7 @@ BOOST_AUTO_TEST_CASE(zs_address_test) { SelectParams(CBaseChainParams::REGTEST); - auto m = GetMasterSaplingSpendingKey(); + auto m = GetTestMasterSaplingSpendingKey(); for (uint32_t i = 0; i < 1000; i++) { auto sk = m.Derive(i); diff --git a/src/test/rpc_wallet_tests.cpp b/src/test/rpc_wallet_tests.cpp index dc9c36e0ba5..bfcc8f88d5e 100644 --- a/src/test/rpc_wallet_tests.cpp +++ b/src/test/rpc_wallet_tests.cpp @@ -563,7 +563,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importexport) pwalletMain->GetSaplingPaymentAddresses(saplingAddrs); BOOST_CHECK(saplingAddrs.empty()); - auto m = GetMasterSaplingSpendingKey(); + auto m = GetTestMasterSaplingSpendingKey(); // verify import and export key for (int i = 0; i < n1; i++) { @@ -1249,7 +1249,7 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals) BOOST_AUTO_TEST_CASE(rpc_z_sendmany_taddr_to_sapling) { - ActivateSapling(); + RegtestActivateSapling(); LOCK(pwalletMain->cs_wallet); @@ -1342,7 +1342,7 @@ BOOST_AUTO_TEST_CASE(rpc_z_sendmany_taddr_to_sapling) mapArgs.erase("-experimentalfeatures"); // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } diff --git a/src/utiltest.cpp b/src/utiltest.cpp index f7a69b8de28..3c16e73e769 100644 --- a/src/utiltest.cpp +++ b/src/utiltest.cpp @@ -160,25 +160,25 @@ CWalletTx GetValidSproutSpend(ZCJoinSplit& params, } // Sapling -const Consensus::Params& ActivateSapling() { +const Consensus::Params& RegtestActivateSapling() { SelectParams(CBaseChainParams::REGTEST); UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); return Params().GetConsensus(); } -void DeactivateSapling() { +void RegtestDeactivateSapling() { UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); } -libzcash::SaplingExtendedSpendingKey GetMasterSaplingSpendingKey() { +libzcash::SaplingExtendedSpendingKey GetTestMasterSaplingSpendingKey() { std::vector> rawSeed(32); HDSeed seed(rawSeed); return libzcash::SaplingExtendedSpendingKey::Master(seed); } -CKey AddCKeyToKeyStore(CBasicKeyStore& keyStore) { +CKey AddTestCKeyToKeyStore(CBasicKeyStore& keyStore) { CKey tsk = DecodeSecret(T_SECRET_REGTEST); keyStore.AddKey(tsk); return tsk; @@ -198,7 +198,7 @@ CWalletTx GetValidSaplingReceive(const Consensus::Params& consensusParams, const libzcash::SaplingExtendedSpendingKey &sk, CAmount value) { // From taddr - CKey tsk = AddCKeyToKeyStore(keyStore); + CKey tsk = AddTestCKeyToKeyStore(keyStore); auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID()); // To zaddr auto fvk = sk.expsk.full_viewing_key(); diff --git a/src/utiltest.h b/src/utiltest.h index 828bf2bcad8..1a91eefe2f0 100644 --- a/src/utiltest.h +++ b/src/utiltest.h @@ -34,13 +34,13 @@ struct TestSaplingNote { SaplingMerkleTree tree; }; -const Consensus::Params& ActivateSapling(); +const Consensus::Params& RegtestActivateSapling(); -void DeactivateSapling(); +void RegtestDeactivateSapling(); -libzcash::SaplingExtendedSpendingKey GetMasterSaplingSpendingKey(); +libzcash::SaplingExtendedSpendingKey GetTestMasterSaplingSpendingKey(); -CKey AddCKeyToKeyStore(CBasicKeyStore& keyStore); +CKey AddTestCKeyToKeyStore(CBasicKeyStore& keyStore); /** * Generate a dummy SaplingNote and a SaplingMerkleTree with that note's commitment. diff --git a/src/wallet/gtest/test_wallet.cpp b/src/wallet/gtest/test_wallet.cpp index 8ab52ddd7c5..8308243abdd 100644 --- a/src/wallet/gtest/test_wallet.cpp +++ b/src/wallet/gtest/test_wallet.cpp @@ -354,11 +354,11 @@ TEST(WalletTests, SetSproutNoteAddrsInCWalletTx) { } TEST(WalletTests, SetSaplingNoteAddrsInCWalletTx) { - auto consensusParams = ActivateSapling(); + auto consensusParams = RegtestActivateSapling(); TestWallet wallet; - auto sk = GetMasterSaplingSpendingKey(); + auto sk = GetTestMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto ivk = fvk.in_viewing_key(); @@ -404,7 +404,7 @@ TEST(WalletTests, SetSaplingNoteAddrsInCWalletTx) { EXPECT_TRUE(witness == wtx.mapSaplingNoteData[op].witnesses.front()); // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } TEST(WalletTests, SetSproutInvalidNoteAddrsInCWalletTx) { @@ -468,12 +468,12 @@ TEST(WalletTests, GetSproutNoteNullifier) { } TEST(WalletTests, FindMySaplingNotes) { - auto consensusParams = ActivateSapling(); + auto consensusParams = RegtestActivateSapling(); TestWallet wallet; // Generate dummy Sapling address - auto sk = GetMasterSaplingSpendingKey(); + auto sk = GetTestMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto pa = sk.DefaultAddress(); @@ -499,7 +499,7 @@ TEST(WalletTests, FindMySaplingNotes) { EXPECT_EQ(2, noteMap.size()); // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } TEST(WalletTests, FindMySproutNotes) { @@ -590,12 +590,12 @@ TEST(WalletTests, GetConflictedSproutNotes) { // Generate note A and spend to create note B, from which we spend to create two conflicting transactions TEST(WalletTests, GetConflictedSaplingNotes) { - auto consensusParams = ActivateSapling(); + auto consensusParams = RegtestActivateSapling(); TestWallet wallet; // Generate Sapling address - auto sk = GetMasterSaplingSpendingKey(); + auto sk = GetTestMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto ivk = fvk.in_viewing_key(); @@ -703,7 +703,7 @@ TEST(WalletTests, GetConflictedSaplingNotes) { mapBlockIndex.erase(blockHash); // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } TEST(WalletTests, SproutNullifierIsSpent) { @@ -747,12 +747,12 @@ TEST(WalletTests, SproutNullifierIsSpent) { } TEST(WalletTests, SaplingNullifierIsSpent) { - auto consensusParams = ActivateSapling(); + auto consensusParams = RegtestActivateSapling(); TestWallet wallet; // Generate dummy Sapling address - auto sk = GetMasterSaplingSpendingKey(); + auto sk = GetTestMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto pa = sk.DefaultAddress(); @@ -800,7 +800,7 @@ TEST(WalletTests, SaplingNullifierIsSpent) { mapBlockIndex.erase(blockHash); // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } TEST(WalletTests, NavigateFromSproutNullifierToNote) { @@ -830,12 +830,12 @@ TEST(WalletTests, NavigateFromSproutNullifierToNote) { } TEST(WalletTests, NavigateFromSaplingNullifierToNote) { - auto consensusParams = ActivateSapling(); + auto consensusParams = RegtestActivateSapling(); TestWallet wallet; // Generate dummy Sapling address - auto sk = GetMasterSaplingSpendingKey(); + auto sk = GetTestMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto pa = sk.DefaultAddress(); @@ -918,7 +918,7 @@ TEST(WalletTests, NavigateFromSaplingNullifierToNote) { mapBlockIndex.erase(blockHash); // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } TEST(WalletTests, SpentSproutNoteIsFromMe) { @@ -951,12 +951,12 @@ TEST(WalletTests, SpentSproutNoteIsFromMe) { // Create note A, spend A to create note B, spend and verify note B is from me. TEST(WalletTests, SpentSaplingNoteIsFromMe) { - auto consensusParams = ActivateSapling(); + auto consensusParams = RegtestActivateSapling(); TestWallet wallet; // Generate Sapling address - auto sk = GetMasterSaplingSpendingKey(); + auto sk = GetTestMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto ivk = fvk.in_viewing_key(); @@ -1087,7 +1087,7 @@ TEST(WalletTests, SpentSaplingNoteIsFromMe) { mapBlockIndex.erase(blockHash2); // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } TEST(WalletTests, CachedWitnessesEmptyChain) { @@ -1569,7 +1569,7 @@ TEST(WalletTests, SetBestChainIgnoresTxsWithoutShieldedData) { CBlockLocator loc; // Set up transparent address - CKey tsk = AddCKeyToKeyStore(wallet); + CKey tsk = AddTestCKeyToKeyStore(wallet); auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID()); // Set up a Sprout address @@ -1725,11 +1725,11 @@ TEST(WalletTests, UpdatedSproutNoteData) { } TEST(WalletTests, UpdatedSaplingNoteData) { - auto consensusParams = ActivateSapling(); + auto consensusParams = RegtestActivateSapling(); TestWallet wallet; - auto m = GetMasterSaplingSpendingKey(); + auto m = GetTestMasterSaplingSpendingKey(); // Generate dummy Sapling address auto sk = m.Derive(0); @@ -1831,7 +1831,7 @@ TEST(WalletTests, UpdatedSaplingNoteData) { mapBlockIndex.erase(blockHash); // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } TEST(WalletTests, MarkAffectedSproutTransactionsDirty) { @@ -1866,12 +1866,12 @@ TEST(WalletTests, MarkAffectedSproutTransactionsDirty) { } TEST(WalletTests, MarkAffectedSaplingTransactionsDirty) { - auto consensusParams = ActivateSapling(); + auto consensusParams = RegtestActivateSapling(); TestWallet wallet; // Generate Sapling address - auto sk = GetMasterSaplingSpendingKey(); + auto sk = GetTestMasterSaplingSpendingKey(); auto expsk = sk.expsk; auto fvk = expsk.full_viewing_key(); auto ivk = fvk.in_viewing_key(); @@ -1882,7 +1882,7 @@ TEST(WalletTests, MarkAffectedSaplingTransactionsDirty) { // Set up transparent address CBasicKeyStore keystore; - CKey tsk = AddCKeyToKeyStore(keystore); + CKey tsk = AddTestCKeyToKeyStore(keystore); auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID()); // Generate shielding tx from transparent to Sapling @@ -1973,7 +1973,7 @@ TEST(WalletTests, MarkAffectedSaplingTransactionsDirty) { mapBlockIndex.erase(blockHash); // Revert to default - DeactivateSapling(); + RegtestDeactivateSapling(); } TEST(WalletTests, SproutNoteLocking) { diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index e2c558ace2f..4275f897f85 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2784,10 +2784,10 @@ UniValue zc_benchmark(const UniValue& params, bool fHelp) sample_times.push_back(benchmark_try_decrypt_sapling_notes(nKeys)); } else if (benchmarktype == "incnotewitnesses") { int nTxs = params[2].get_int(); - sample_times.push_back(benchmark_increment_note_witnesses_sprout(nTxs)); - } else if (benchmarktype == "incnotewitnessessapling") { + sample_times.push_back(benchmark_increment_sprout_note_witnesses(nTxs)); + } else if (benchmarktype == "incsaplingnotewitnesses") { int nTxs = params[2].get_int(); - sample_times.push_back(benchmark_increment_note_witnesses_sapling(nTxs)); + sample_times.push_back(benchmark_increment_sapling_note_witnesses(nTxs)); } else if (benchmarktype == "connectblockslow") { if (Params().NetworkIDString() != "regtest") { throw JSONRPCError(RPC_TYPE_ERROR, "Benchmark must be run in regtest mode"); diff --git a/src/zcbenchmarks.cpp b/src/zcbenchmarks.cpp index b178081e3a9..f7a266e09ff 100644 --- a/src/zcbenchmarks.cpp +++ b/src/zcbenchmarks.cpp @@ -281,7 +281,7 @@ double benchmark_large_tx(size_t nInputs) } // The two benchmarks, try_decrypt_sprout_notes and try_decrypt_sapling_notes, -// are checking a worst-case scenarios. In both we add n keys to a wallet, +// are checking worst-case scenarios. In both we add n keys to a wallet, // create a transaction using a key not in our original list of n, and then // check that the transaction is not associated with any of the keys in our // wallet. We call assert(...) to ensure that this is true. @@ -309,17 +309,17 @@ double benchmark_try_decrypt_sapling_notes(size_t nKeys) // Set params auto consensusParams = Params().GetConsensus(); - auto masterKey = GetMasterSaplingSpendingKey(); + auto masterKey = GetTestMasterSaplingSpendingKey(); CWallet wallet; - int i; - for (i = 0; i < nKeys; i++) { + for (int i = 0; i < nKeys; i++) { auto sk = masterKey.Derive(i); wallet.AddSaplingSpendingKey(sk, sk.DefaultAddress()); } - auto sk = masterKey.Derive(i); + // Generate a key that has not been added to the wallet + auto sk = masterKey.Derive(nKeys); auto tx = GetValidSaplingReceive(consensusParams, wallet, sk, 10); struct timeval tv_start; @@ -344,7 +344,7 @@ CWalletTx CreateSproutTxWithNoteData(const libzcash::SproutSpendingKey& sk) { return wtx; } -double benchmark_increment_note_witnesses_sprout(size_t nTxs) +double benchmark_increment_sprout_note_witnesses(size_t nTxs) { auto consensusParams = Params().GetConsensus(); @@ -406,7 +406,7 @@ CWalletTx CreateSaplingTxWithNoteData(const Consensus::Params& consensusParams, return wtx; } -double benchmark_increment_note_witnesses_sapling(size_t nTxs) +double benchmark_increment_sapling_note_witnesses(size_t nTxs) { auto consensusParams = Params().GetConsensus(); @@ -414,7 +414,7 @@ double benchmark_increment_note_witnesses_sapling(size_t nTxs) SproutMerkleTree sproutTree; SaplingMerkleTree saplingTree; - auto saplingSpendingKey = GetMasterSaplingSpendingKey(); + auto saplingSpendingKey = GetTestMasterSaplingSpendingKey(); wallet.AddSaplingSpendingKey(saplingSpendingKey, saplingSpendingKey.DefaultAddress()); // First block diff --git a/src/zcbenchmarks.h b/src/zcbenchmarks.h index 257b2fe9ed4..721251eb58f 100644 --- a/src/zcbenchmarks.h +++ b/src/zcbenchmarks.h @@ -15,8 +15,8 @@ extern double benchmark_verify_equihash(); extern double benchmark_large_tx(size_t nInputs); extern double benchmark_try_decrypt_sprout_notes(size_t nAddrs); extern double benchmark_try_decrypt_sapling_notes(size_t nAddrs); -extern double benchmark_increment_note_witnesses_sprout(size_t nTxs); -extern double benchmark_increment_note_witnesses_sapling(size_t nTxs); +extern double benchmark_increment_sprout_note_witnesses(size_t nTxs); +extern double benchmark_increment_sapling_note_witnesses(size_t nTxs); extern double benchmark_connectblock_slow(); extern double benchmark_sendtoaddress(CAmount amount); extern double benchmark_loadwallet();