Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update zcbenchmarks to include sapling data #3657

Merged
merged 12 commits into from Feb 26, 2019
4 changes: 2 additions & 2 deletions src/gtest/test_checkblock.cpp
Expand Up @@ -3,6 +3,7 @@

#include "consensus/validation.h"
#include "main.h"
#include "utiltest.h"
#include "zcash/Proof.hpp"

class MockCValidationState : public CValidationState {
Expand Down Expand Up @@ -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);
RegtestDeactivateSapling();
}

// Returns a valid but empty mutable transaction at block height 1.
Expand Down
17 changes: 6 additions & 11 deletions src/gtest/test_checktransaction.cpp
Expand Up @@ -5,6 +5,7 @@
#include "main.h"
#include "primitives/transaction.h"
#include "consensus/validation.h"
#include "utiltest.h"

extern ZCJoinSplit* params;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/gtest/test_keys.cpp
Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions src/gtest/test_keystore.cpp
Expand Up @@ -7,6 +7,7 @@
#ifdef ENABLE_WALLET
#include "wallet/crypter.h"
#endif
#include "utiltest.h"
#include "zcash/Address.hpp"
#include "zcash/zip32.h"

Expand Down Expand Up @@ -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();
Expand Down
88 changes: 25 additions & 63 deletions src/gtest/test_transaction_builder.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -165,37 +149,27 @@ 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
builder.AddTransparentInput(COutPoint(), scriptPubKey, 1);
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();
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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();

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/gtest/test_validation.cpp
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/test/key_tests.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 4 additions & 8 deletions src/test/rpc_wallet_tests.cpp
Expand Up @@ -20,6 +20,7 @@
#include "wallet/asyncrpcoperation_shieldcoinbase.h"

#include "init.h"
#include "utiltest.h"

#include <array>
#include <chrono>
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
}


Expand Down