Auto merge of #3657 - Eirik0:3395-sapling-benchmarks, r=str4d
Update zcbenchmarks to include sapling data Closes #3395 This PR adds a benchmark named `trydecryptsaplingnotes` which is intended to be similar to `trydecryptnotes`. It also adds a benchmark `incnotewitnessessapling` which is similar to `incnotewitnesses`. As a side note, while looking for examples to follow I ran in to a fair amount of setup, which I wanted to be able to reuse, repeated across several tests. I pulled some of that logic in to a utility functions and refactored the existing tests using that setup.
This commit is contained in:
commit
52fbc1ce66
|
@ -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 @@ protected:
|
|||
|
||||
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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
// Returns a valid but empty mutable transaction at block height 1.
|
||||
|
|
|
@ -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);
|
||||
RegtestActivateSapling();
|
||||
|
||||
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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(checktransaction_tests, OversizeSaplingTxns) {
|
||||
SelectParams(CBaseChainParams::REGTEST);
|
||||
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE);
|
||||
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE);
|
||||
RegtestActivateSapling();
|
||||
|
||||
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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
// Test a v1 transaction which has a malformed header, perhaps modified in-flight
|
||||
|
|
|
@ -3,15 +3,15 @@
|
|||
#include <zcash/Address.hpp>
|
||||
#include <zcash/zip32.h>
|
||||
|
||||
#include "utiltest.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(Keys, EncodeAndDecodeSapling)
|
||||
{
|
||||
SelectParams(CBaseChainParams::MAIN);
|
||||
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
|
||||
HDSeed seed(rawSeed);
|
||||
auto m = libzcash::SaplingExtendedSpendingKey::Master(seed);
|
||||
auto m = GetTestMasterSaplingSpendingKey();
|
||||
|
||||
for (uint32_t i = 0; i < 1000; i++) {
|
||||
auto sk = m.Derive(i);
|
||||
|
|
|
@ -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<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
|
||||
HDSeed seed(rawSeed);
|
||||
auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed);
|
||||
auto sk = GetTestMasterSaplingSpendingKey();
|
||||
auto fvk = sk.expsk.full_viewing_key();
|
||||
auto ivk = fvk.in_viewing_key();
|
||||
auto addr = sk.DefaultAddress();
|
||||
|
|
|
@ -6,23 +6,18 @@
|
|||
#include "pubkey.h"
|
||||
#include "rpc/protocol.h"
|
||||
#include "transaction_builder.h"
|
||||
#include "utiltest.h"
|
||||
#include "zcash/Address.hpp"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
static const std::string tSecretRegtest = "cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN";
|
||||
|
||||
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 = RegtestActivateSapling();
|
||||
|
||||
CBasicKeyStore keystore;
|
||||
CKey tsk = DecodeSecret(tSecretRegtest);
|
||||
keystore.AddKey(tsk);
|
||||
CKey tsk = AddTestCKeyToKeyStore(keystore);
|
||||
auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID());
|
||||
|
||||
auto sk_from = libzcash::SaplingSpendingKey::random();
|
||||
|
@ -87,8 +82,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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
TEST(TransactionBuilder, ThrowsOnTransparentInputWithoutKeyStore)
|
||||
|
@ -124,37 +118,27 @@ 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 = RegtestActivateSapling();
|
||||
|
||||
// Generate dummy Sapling address
|
||||
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;
|
||||
CKey tsk = DecodeSecret(tSecretRegtest);
|
||||
keystore.AddKey(tsk);
|
||||
CKey tsk = AddTestCKeyToKeyStore(keystore);
|
||||
auto tkeyid = tsk.GetPubKey().GetID();
|
||||
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
|
||||
|
@ -165,7 +149,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
|
||||
|
@ -173,29 +157,19 @@ 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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
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 = RegtestActivateSapling();
|
||||
|
||||
// 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();
|
||||
|
@ -204,8 +178,7 @@ TEST(TransactionBuilder, ChangeOutput)
|
|||
|
||||
// Set up dummy transparent address
|
||||
CBasicKeyStore keystore;
|
||||
CKey tsk = DecodeSecret(tSecretRegtest);
|
||||
keystore.AddKey(tsk);
|
||||
CKey tsk = AddTestCKeyToKeyStore(keystore);
|
||||
auto tkeyid = tsk.GetPubKey().GetID();
|
||||
auto scriptPubKey = GetScriptForDestination(tkeyid);
|
||||
CTxDestination taddr = tkeyid;
|
||||
|
@ -221,7 +194,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);
|
||||
|
@ -264,36 +237,26 @@ TEST(TransactionBuilder, ChangeOutput)
|
|||
}
|
||||
|
||||
// Revert to default
|
||||
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT);
|
||||
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
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 = RegtestActivateSapling();
|
||||
|
||||
// Generate dummy Sapling address
|
||||
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);
|
||||
|
@ -307,8 +270,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();
|
||||
|
||||
|
@ -321,8 +284,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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
TEST(TransactionBuilder, CheckSaplingTxVersion)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
|
||||
HDSeed seed(rawSeed);
|
||||
auto m = libzcash::SaplingExtendedSpendingKey::Master(seed);
|
||||
auto m = GetTestMasterSaplingSpendingKey();
|
||||
|
||||
for (uint32_t i = 0; i < 1000; i++) {
|
||||
auto sk = m.Derive(i);
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "wallet/asyncrpcoperation_shieldcoinbase.h"
|
||||
|
||||
#include "init.h"
|
||||
#include "utiltest.h"
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
|
@ -562,9 +563,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importexport)
|
|||
pwalletMain->GetSaplingPaymentAddresses(saplingAddrs);
|
||||
BOOST_CHECK(saplingAddrs.empty());
|
||||
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
|
||||
HDSeed seed(rawSeed);
|
||||
auto m = libzcash::SaplingExtendedSpendingKey::Master(seed);
|
||||
auto m = GetTestMasterSaplingSpendingKey();
|
||||
|
||||
// verify import and export key
|
||||
for (int i = 0; i < n1; i++) {
|
||||
|
@ -1250,9 +1249,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);
|
||||
RegtestActivateSapling();
|
||||
|
||||
LOCK(pwalletMain->cs_wallet);
|
||||
|
||||
|
@ -1345,8 +1342,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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -5,13 +5,16 @@
|
|||
#include "utiltest.h"
|
||||
|
||||
#include "consensus/upgrades.h"
|
||||
#include "transaction_builder.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
CWalletTx GetValidReceive(ZCJoinSplit& params,
|
||||
const libzcash::SproutSpendingKey& sk, CAmount value,
|
||||
bool randomInputs,
|
||||
int32_t version /* = 2 */) {
|
||||
// Sprout
|
||||
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);
|
||||
|
@ -47,6 +50,9 @@ CWalletTx GetValidReceive(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;
|
||||
|
@ -70,9 +76,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 +90,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;
|
||||
|
@ -151,3 +158,58 @@ CWalletTx GetValidSpend(ZCJoinSplit& params,
|
|||
CWalletTx wtx {NULL, tx};
|
||||
return wtx;
|
||||
}
|
||||
|
||||
// Sapling
|
||||
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 RegtestDeactivateSapling() {
|
||||
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT);
|
||||
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT);
|
||||
}
|
||||
|
||||
libzcash::SaplingExtendedSpendingKey GetTestMasterSaplingSpendingKey() {
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
|
||||
HDSeed seed(rawSeed);
|
||||
return libzcash::SaplingExtendedSpendingKey::Master(seed);
|
||||
}
|
||||
|
||||
CKey AddTestCKeyToKeyStore(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);
|
||||
uint256 cm = note.cm().get();
|
||||
SaplingMerkleTree tree;
|
||||
tree.append(cm);
|
||||
return { note, tree };
|
||||
}
|
||||
|
||||
CWalletTx GetValidSaplingReceive(const Consensus::Params& consensusParams,
|
||||
CBasicKeyStore& keyStore,
|
||||
const libzcash::SaplingExtendedSpendingKey &sk,
|
||||
CAmount value) {
|
||||
// From taddr
|
||||
CKey tsk = AddTestCKeyToKeyStore(keyStore);
|
||||
auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID());
|
||||
// To zaddr
|
||||
auto fvk = sk.expsk.full_viewing_key();
|
||||
auto pa = sk.DefaultAddress();
|
||||
|
||||
auto builder = TransactionBuilder(consensusParams, 1, &keyStore);
|
||||
builder.SetFee(0);
|
||||
builder.AddTransparentInput(COutPoint(), scriptPubKey, value);
|
||||
builder.AddSaplingOutput(fvk.ovk, pa, value, {});
|
||||
|
||||
CTransaction tx = builder.Build().GetTxOrThrow();
|
||||
CWalletTx wtx {NULL, tx};
|
||||
return wtx;
|
||||
}
|
||||
|
|
|
@ -2,18 +2,54 @@
|
|||
// 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 "key_io.h"
|
||||
#include "wallet/wallet.h"
|
||||
#include "zcash/JoinSplit.hpp"
|
||||
#include "zcash/Note.hpp"
|
||||
#include "zcash/NoteEncryption.hpp"
|
||||
#include "zcash/zip32.h"
|
||||
|
||||
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);
|
||||
// Sprout
|
||||
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);
|
||||
|
||||
// Sapling
|
||||
static const std::string T_SECRET_REGTEST = "cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN";
|
||||
|
||||
struct TestSaplingNote {
|
||||
libzcash::SaplingNote note;
|
||||
SaplingMerkleTree tree;
|
||||
};
|
||||
|
||||
const Consensus::Params& RegtestActivateSapling();
|
||||
|
||||
void RegtestDeactivateSapling();
|
||||
|
||||
libzcash::SaplingExtendedSpendingKey GetTestMasterSaplingSpendingKey();
|
||||
|
||||
CKey AddTestCKeyToKeyStore(CBasicKeyStore& keyStore);
|
||||
|
||||
/**
|
||||
* Generate a dummy SaplingNote and a SaplingMerkleTree with that note's commitment.
|
||||
*/
|
||||
TestSaplingNote GetTestSaplingNote(const libzcash::SaplingPaymentAddress& pa, CAmount value);
|
||||
|
||||
CWalletTx GetValidSaplingReceive(const Consensus::Params& consensusParams,
|
||||
CBasicKeyStore& keyStore,
|
||||
const libzcash::SaplingExtendedSpendingKey &sk,
|
||||
CAmount value);
|
||||
|
||||
#endif // ZCASH_UTIL_TEST_H
|
||||
|
|
|
@ -25,8 +25,6 @@ ACTION(ThrowLogicError) {
|
|||
throw std::logic_error("Boom");
|
||||
}
|
||||
|
||||
static const std::string tSecretRegtest = "cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN";
|
||||
|
||||
class MockWalletDB {
|
||||
public:
|
||||
MOCK_METHOD0(TxnBegin, bool());
|
||||
|
@ -73,18 +71,18 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
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<SaplingOutPoint> SetSaplingNoteData(CWalletTx& wtx) {
|
||||
|
@ -103,8 +101,8 @@ std::pair<JSOutPoint, SaplingOutPoint> 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 +142,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 +170,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 +228,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 +275,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 +339,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());
|
||||
|
||||
|
@ -356,16 +354,11 @@ 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 = RegtestActivateSapling();
|
||||
|
||||
TestWallet wallet;
|
||||
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
|
||||
HDSeed seed(rawSeed);
|
||||
auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed);
|
||||
auto sk = GetTestMasterSaplingSpendingKey();
|
||||
auto expsk = sk.expsk;
|
||||
auto fvk = expsk.full_viewing_key();
|
||||
auto ivk = fvk.in_viewing_key();
|
||||
|
@ -411,8 +404,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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
TEST(WalletTests, SetSproutInvalidNoteAddrsInCWalletTx) {
|
||||
|
@ -451,8 +443,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(
|
||||
|
@ -476,33 +468,22 @@ 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 = RegtestActivateSapling();
|
||||
|
||||
TestWallet wallet;
|
||||
|
||||
// Generate dummy Sapling address
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
|
||||
HDSeed seed(rawSeed);
|
||||
auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed);
|
||||
auto sk = GetTestMasterSaplingSpendingKey();
|
||||
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
|
||||
|
@ -512,14 +493,13 @@ 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());
|
||||
|
||||
// Revert to default
|
||||
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT);
|
||||
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
TEST(WalletTests, FindMySproutNotes) {
|
||||
|
@ -529,8 +509,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 +537,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 +563,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();
|
||||
|
||||
|
@ -610,17 +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) {
|
||||
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 = RegtestActivateSapling();
|
||||
|
||||
TestWallet wallet;
|
||||
|
||||
// Generate Sapling address
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
|
||||
HDSeed seed(rawSeed);
|
||||
auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed);
|
||||
auto sk = GetTestMasterSaplingSpendingKey();
|
||||
auto expsk = sk.expsk;
|
||||
auto fvk = expsk.full_viewing_key();
|
||||
auto ivk = fvk.in_viewing_key();
|
||||
|
@ -728,8 +703,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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
TEST(WalletTests, SproutNullifierIsSpent) {
|
||||
|
@ -738,8 +712,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 +721,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));
|
||||
|
||||
|
@ -773,41 +747,30 @@ 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 = RegtestActivateSapling();
|
||||
|
||||
TestWallet wallet;
|
||||
|
||||
// Generate dummy Sapling address
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
|
||||
HDSeed seed(rawSeed);
|
||||
auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed);
|
||||
auto sk = GetTestMasterSaplingSpendingKey();
|
||||
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();
|
||||
|
||||
|
@ -837,8 +800,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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
TEST(WalletTests, NavigateFromSproutNullifierToNote) {
|
||||
|
@ -847,8 +809,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;
|
||||
|
@ -868,41 +830,30 @@ 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 = RegtestActivateSapling();
|
||||
|
||||
TestWallet wallet;
|
||||
|
||||
// Generate dummy Sapling address
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
|
||||
HDSeed seed(rawSeed);
|
||||
auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed);
|
||||
auto sk = GetTestMasterSaplingSpendingKey();
|
||||
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();
|
||||
|
||||
|
@ -941,7 +892,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
|
||||
|
@ -967,8 +918,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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
TEST(WalletTests, SpentSproutNoteIsFromMe) {
|
||||
|
@ -977,10 +927,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));
|
||||
|
@ -1001,17 +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) {
|
||||
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 = RegtestActivateSapling();
|
||||
|
||||
TestWallet wallet;
|
||||
|
||||
// Generate Sapling address
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
|
||||
HDSeed seed(rawSeed);
|
||||
auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed);
|
||||
auto sk = GetTestMasterSaplingSpendingKey();
|
||||
auto expsk = sk.expsk;
|
||||
auto fvk = expsk.full_viewing_key();
|
||||
auto ivk = fvk.in_viewing_key();
|
||||
|
@ -1142,8 +1087,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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
TEST(WalletTests, CachedWitnessesEmptyChain) {
|
||||
|
@ -1152,9 +1096,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 +1177,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 +1286,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 +1424,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 +1484,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;
|
||||
|
@ -1625,8 +1569,7 @@ TEST(WalletTests, SetBestChainIgnoresTxsWithoutShieldedData) {
|
|||
CBlockLocator loc;
|
||||
|
||||
// Set up transparent address
|
||||
CKey tsk = DecodeSecret(tSecretRegtest);
|
||||
wallet.AddKey(tsk);
|
||||
CKey tsk = AddTestCKeyToKeyStore(wallet);
|
||||
auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID());
|
||||
|
||||
// Set up a Sprout address
|
||||
|
@ -1642,16 +1585,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 +1652,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 +1683,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;
|
||||
|
@ -1782,46 +1725,35 @@ 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 = RegtestActivateSapling();
|
||||
|
||||
TestWallet wallet;
|
||||
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
|
||||
HDSeed seed(rawSeed);
|
||||
auto m = libzcash::SaplingExtendedSpendingKey::Master(seed);
|
||||
auto m = GetTestMasterSaplingSpendingKey();
|
||||
|
||||
// Generate dummy Sapling address
|
||||
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));
|
||||
|
||||
|
@ -1846,15 +1778,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;
|
||||
|
@ -1864,7 +1796,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,
|
||||
|
@ -1892,15 +1824,14 @@ 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);
|
||||
mapBlockIndex.erase(blockHash);
|
||||
|
||||
// Revert to default
|
||||
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT);
|
||||
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
TEST(WalletTests, MarkAffectedSproutTransactionsDirty) {
|
||||
|
@ -1909,11 +1840,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};
|
||||
|
@ -1935,17 +1866,12 @@ 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 = RegtestActivateSapling();
|
||||
|
||||
TestWallet wallet;
|
||||
|
||||
// Generate Sapling address
|
||||
std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
|
||||
HDSeed seed(rawSeed);
|
||||
auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed);
|
||||
auto sk = GetTestMasterSaplingSpendingKey();
|
||||
auto expsk = sk.expsk;
|
||||
auto fvk = expsk.full_viewing_key();
|
||||
auto ivk = fvk.in_viewing_key();
|
||||
|
@ -1956,8 +1882,7 @@ TEST(WalletTests, MarkAffectedSaplingTransactionsDirty) {
|
|||
|
||||
// Set up transparent address
|
||||
CBasicKeyStore keystore;
|
||||
CKey tsk = DecodeSecret(tSecretRegtest);
|
||||
keystore.AddKey(tsk);
|
||||
CKey tsk = AddTestCKeyToKeyStore(keystore);
|
||||
auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID());
|
||||
|
||||
// Generate shielding tx from transparent to Sapling
|
||||
|
@ -2048,8 +1973,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);
|
||||
RegtestDeactivateSapling();
|
||||
}
|
||||
|
||||
TEST(WalletTests, SproutNoteLocking) {
|
||||
|
@ -2058,8 +1982,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};
|
||||
|
|
|
@ -2777,11 +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_notes(nAddrs));
|
||||
int nKeys = params[2].get_int();
|
||||
sample_times.push_back(benchmark_try_decrypt_sprout_notes(nKeys));
|
||||
} else if (benchmarktype == "trydecryptsaplingnotes") {
|
||||
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_sprout_note_witnesses(nTxs));
|
||||
} else if (benchmarktype == "incsaplingnotewitnesses") {
|
||||
int nTxs = params[2].get_int();
|
||||
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");
|
||||
|
|
|
@ -280,48 +280,89 @@ double benchmark_large_tx(size_t nInputs)
|
|||
return timer_stop(tv_start);
|
||||
}
|
||||
|
||||
double benchmark_try_decrypt_notes(size_t nAddrs)
|
||||
// The two benchmarks, try_decrypt_sprout_notes and try_decrypt_sapling_notes,
|
||||
// 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.
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
auto nd = wallet.FindMySproutNotes(tx);
|
||||
auto noteDataMap = wallet.FindMySproutNotes(tx);
|
||||
|
||||
assert(noteDataMap.empty());
|
||||
return timer_stop(tv_start);
|
||||
}
|
||||
|
||||
double benchmark_increment_note_witnesses(size_t nTxs)
|
||||
double benchmark_try_decrypt_sapling_notes(size_t nKeys)
|
||||
{
|
||||
// Set params
|
||||
auto consensusParams = Params().GetConsensus();
|
||||
|
||||
auto masterKey = GetTestMasterSaplingSpendingKey();
|
||||
|
||||
CWallet wallet;
|
||||
|
||||
for (int i = 0; i < nKeys; i++) {
|
||||
auto sk = masterKey.Derive(i);
|
||||
wallet.AddSaplingSpendingKey(sk, sk.DefaultAddress());
|
||||
}
|
||||
|
||||
// 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;
|
||||
timer_start(tv_start);
|
||||
auto noteDataMapAndAddressesToAdd = wallet.FindMySaplingNotes(tx);
|
||||
assert(noteDataMapAndAddressesToAdd.first.empty());
|
||||
return timer_stop(tv_start);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
double benchmark_increment_sprout_note_witnesses(size_t nTxs)
|
||||
{
|
||||
auto consensusParams = Params().GetConsensus();
|
||||
|
||||
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 = GetValidReceive(*pzcashParams, sk, 10, true);
|
||||
auto note = GetNote(*pzcashParams, sk, wtx, 0, 1);
|
||||
auto nullifier = note.nullifier(sk);
|
||||
|
||||
mapSproutNoteData_t noteData;
|
||||
JSOutPoint jsoutpt {wtx.GetHash(), 0, 1};
|
||||
SproutNoteData nd {sk.address(), nullifier};
|
||||
noteData[jsoutpt] = nd;
|
||||
|
||||
wtx.SetSproutNoteData(noteData);
|
||||
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;
|
||||
|
||||
|
@ -332,19 +373,73 @@ 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 nullifier = note.nullifier(sk);
|
||||
|
||||
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 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 = 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();
|
||||
|
||||
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_sapling_note_witnesses(size_t nTxs)
|
||||
{
|
||||
auto consensusParams = Params().GetConsensus();
|
||||
|
||||
CWallet wallet;
|
||||
SproutMerkleTree sproutTree;
|
||||
SaplingMerkleTree saplingTree;
|
||||
|
||||
auto saplingSpendingKey = GetTestMasterSaplingSpendingKey();
|
||||
wallet.AddSaplingSpendingKey(saplingSpendingKey, saplingSpendingKey.DefaultAddress());
|
||||
|
||||
// First block
|
||||
CBlock block1;
|
||||
for (int i = 0; i < nTxs; ++i) {
|
||||
auto wtx = CreateSaplingTxWithNoteData(consensusParams, wallet, saplingSpendingKey);
|
||||
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 saplingTx = CreateSaplingTxWithNoteData(consensusParams, wallet, saplingSpendingKey);
|
||||
wallet.AddToWallet(saplingTx, true, NULL);
|
||||
block1.vtx.push_back(saplingTx);
|
||||
}
|
||||
|
||||
CBlockIndex index2(block2);
|
||||
index2.nHeight = 2;
|
||||
|
||||
|
@ -355,16 +450,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 GetAnchorAt(const uint256 &rt, SproutMerkleTree &tree) const {
|
||||
if (rt == t.root()) {
|
||||
tree = t;
|
||||
bool GetSproutAnchorAt(const uint256 &rt, SproutMerkleTree &tree) const {
|
||||
if (rt == sproutTree.root()) {
|
||||
tree = sproutTree;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GetSaplingAnchorAt(const uint256 &rt, SaplingMerkleTree &tree) const {
|
||||
if (rt == saplingTree.root()) {
|
||||
tree = saplingTree;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -374,20 +486,37 @@ public:
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,8 +13,10 @@ extern std::vector<double> 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_increment_note_witnesses(size_t nTxs);
|
||||
extern double benchmark_try_decrypt_sprout_notes(size_t nAddrs);
|
||||
extern double benchmark_try_decrypt_sapling_notes(size_t nAddrs);
|
||||
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();
|
||||
|
|
Loading…
Reference in New Issue