Directly call RegtestActivate* in gtests

Co-authored by Jack Grigg (jack@electriccoin.co)
This commit is contained in:
therealyingtong 2020-07-06 13:34:45 +08:00
parent 1020254b6a
commit dde5cc87b7
4 changed files with 42 additions and 62 deletions

View File

@ -25,16 +25,10 @@ public:
TEST(NoteEncryption, NotePlaintext)
{
SelectParams(CBaseChainParams::REGTEST);
int overwinterActivationHeight = 5;
int saplingActivationHeight = 30;
int canopyActivationHeight = 70;
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, overwinterActivationHeight);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, saplingActivationHeight);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_CANOPY, canopyActivationHeight);
auto params = Params().GetConsensus();
std::vector<libzcash::Zip212Enabled> zip_212_enabled = {libzcash::Zip212Enabled::BeforeZip212, libzcash::Zip212Enabled::AfterZip212};
int decryptionHeights[] = {saplingActivationHeight, canopyActivationHeight};
const Consensus::Params& (*activations [])() = {RegtestActivateSapling, RegtestActivateCanopy};
void (*deactivations [])() = {RegtestDeactivateSapling, RegtestDeactivateCanopy};
using namespace libzcash;
auto xsk = SaplingSpendingKey(uint256()).expanded_spending_key();
@ -49,6 +43,8 @@ TEST(NoteEncryption, NotePlaintext)
}
for (int ver = 0; ver < zip_212_enabled.size(); ver++){
auto params = (*activations[ver])();
SaplingNote note(addr, 39393, zip_212_enabled[ver]);
auto cmu_opt = note.cmu();
if (!cmu_opt) {
@ -71,7 +67,7 @@ TEST(NoteEncryption, NotePlaintext)
// Try to decrypt with incorrect commitment
ASSERT_FALSE(SaplingNotePlaintext::decrypt(
params,
decryptionHeights[ver],
1,
ct,
ivk,
epk,
@ -81,7 +77,7 @@ TEST(NoteEncryption, NotePlaintext)
// Try to decrypt with correct commitment
auto foo = SaplingNotePlaintext::decrypt(
params,
decryptionHeights[ver],
1,
ct,
ivk,
epk,
@ -149,7 +145,7 @@ TEST(NoteEncryption, NotePlaintext)
ASSERT_FALSE(
SaplingNotePlaintext::decrypt(
params,
decryptionHeights[ver],
1,
ct,
epk,
decrypted_out_ct_unwrapped.esk,
@ -161,7 +157,7 @@ TEST(NoteEncryption, NotePlaintext)
// Test sender can decrypt the note ciphertext.
foo = SaplingNotePlaintext::decrypt(
params,
decryptionHeights[ver],
1,
ct,
epk,
decrypted_out_ct_unwrapped.esk,
@ -179,12 +175,9 @@ TEST(NoteEncryption, NotePlaintext)
ASSERT_TRUE(bar.memo() == pt.memo());
ASSERT_TRUE(bar.d == pt.d);
ASSERT_TRUE(bar.rcm() == pt.rcm());
}
// Revert to test default
RegtestDeactivateCanopy();
RegtestDeactivateHeartwood();
RegtestDeactivateSapling();
(*deactivations[ver])();
}
}
TEST(NoteEncryption, RejectsInvalidNoteZip212Enabled)

View File

@ -264,6 +264,10 @@ const Consensus::Params& RegtestActivateCanopy(bool updatePow, int canopyActivat
return Params().GetConsensus();
}
const Consensus::Params& RegtestActivateCanopy() {
return RegtestActivateCanopy(false, Consensus::NetworkUpgrade::ALWAYS_ACTIVE);
}
void RegtestDeactivateCanopy() {
UpdateRegtestPow(0, 0, uint256S("0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f"));
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_CANOPY, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT);

View File

@ -54,6 +54,7 @@ const Consensus::Params& RegtestActivateHeartwood(bool updatePow, int heartwoodA
void RegtestDeactivateHeartwood();
const Consensus::Params& RegtestActivateCanopy(bool updatePow, int canopyActivationHeight = Consensus::NetworkUpgrade::ALWAYS_ACTIVE);
const Consensus::Params& RegtestActivateCanopy();
void RegtestDeactivateCanopy();

View File

@ -377,18 +377,14 @@ TEST(WalletTests, SetSproutNoteAddrsInCWalletTx) {
TEST(WalletTests, SetSaplingNoteAddrsInCWalletTx) {
SelectParams(CBaseChainParams::REGTEST);
int overwinterActivationHeight = 5;
int saplingActivationHeight = 30;
int canopyActivationHeight = 70;
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, overwinterActivationHeight);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, saplingActivationHeight);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_CANOPY, canopyActivationHeight);
auto consensusParams = Params().GetConsensus();
std::vector<libzcash::Zip212Enabled> zip_212_enabled = {libzcash::Zip212Enabled::BeforeZip212, libzcash::Zip212Enabled::AfterZip212};
int builderHeights[] = {saplingActivationHeight, canopyActivationHeight};
const Consensus::Params& (*activations [])() = {RegtestActivateSapling, RegtestActivateCanopy};
void (*deactivations [])() = {RegtestDeactivateSapling, RegtestDeactivateCanopy};
for (int ver = 0; ver < zip_212_enabled.size(); ver++) {
auto consensusParams = (*activations[ver])();
TestWallet wallet;
LOCK(wallet.cs_wallet);
@ -409,7 +405,7 @@ TEST(WalletTests, SetSaplingNoteAddrsInCWalletTx) {
ASSERT_TRUE(nf);
uint256 nullifier = nf.get();
auto builder = TransactionBuilder(consensusParams, builderHeights[ver]);
auto builder = TransactionBuilder(consensusParams, 1);
builder.AddSaplingSpend(expsk, note, anchor, witness);
builder.AddSaplingOutput(fvk.ovk, pk, 50000, {});
builder.SetFee(0);
@ -436,11 +432,9 @@ TEST(WalletTests, SetSaplingNoteAddrsInCWalletTx) {
EXPECT_EQ(nullifier, wtx.mapSaplingNoteData[op].nullifier);
EXPECT_EQ(nd.witnessHeight, wtx.mapSaplingNoteData[op].witnessHeight);
EXPECT_TRUE(witness == wtx.mapSaplingNoteData[op].witnesses.front());
}
// Revert to default
RegtestDeactivateCanopy();
RegtestDeactivateSapling();
(*deactivations[ver])();
}
}
TEST(WalletTests, SetSproutInvalidNoteAddrsInCWalletTx) {
@ -652,18 +646,14 @@ 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);
int overwinterActivationHeight = 5;
int saplingActivationHeight = 30;
int canopyActivationHeight = 70;
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, overwinterActivationHeight);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, saplingActivationHeight);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_CANOPY, canopyActivationHeight);
auto consensusParams = Params().GetConsensus();
std::vector<libzcash::Zip212Enabled> zip_212_enabled = {libzcash::Zip212Enabled::BeforeZip212, libzcash::Zip212Enabled::AfterZip212};
int builderHeights[] = {saplingActivationHeight, canopyActivationHeight};
const Consensus::Params& (*activations [])() = {RegtestActivateSapling, RegtestActivateCanopy};
void (*deactivations [])() = {RegtestDeactivateSapling, RegtestDeactivateCanopy};
for (int ver = 0; ver < zip_212_enabled.size(); ver++) {
auto consensusParams = (*activations[ver])();
TestWallet wallet;
LOCK2(cs_main, wallet.cs_wallet);
@ -686,7 +676,7 @@ TEST(WalletTests, GetConflictedSaplingNotes) {
auto witness = saplingTree.witness();
// Generate tx to create output note B
auto builder = TransactionBuilder(consensusParams, builderHeights[ver]);
auto builder = TransactionBuilder(consensusParams, 1);
builder.AddSaplingSpend(expsk, note, anchor, witness);
builder.AddSaplingOutput(extfvk.fvk.ovk, pk, 35000, {});
auto tx = builder.Build().GetTxOrThrow();
@ -706,7 +696,7 @@ TEST(WalletTests, GetConflictedSaplingNotes) {
EXPECT_EQ(0, chainActive.Height());
// Simulate SyncTransaction which calls AddToWalletIfInvolvingMe
auto saplingNoteData = wallet.FindMySaplingNotes(wtx, builderHeights[ver]).first;
auto saplingNoteData = wallet.FindMySaplingNotes(wtx, 1).first;
ASSERT_TRUE(saplingNoteData.size() > 0);
wtx.SetSaplingNoteData(saplingNoteData);
wtx.SetMerkleBranch(block);
@ -742,13 +732,13 @@ TEST(WalletTests, GetConflictedSaplingNotes) {
anchor = saplingTree.root();
// Create transaction to spend note B
auto builder2 = TransactionBuilder(consensusParams, builderHeights[ver] + 1);
auto builder2 = TransactionBuilder(consensusParams, 2);
builder2.AddSaplingSpend(expsk, note2, anchor, spend_note_witness);
builder2.AddSaplingOutput(extfvk.fvk.ovk, pk, 20000, {});
auto tx2 = builder2.Build().GetTxOrThrow();
// Create conflicting transaction which also spends note B
auto builder3 = TransactionBuilder(consensusParams, builderHeights[ver] + 1);
auto builder3 = TransactionBuilder(consensusParams, 2);
builder3.AddSaplingSpend(expsk, note2, anchor, spend_note_witness);
builder3.AddSaplingOutput(extfvk.fvk.ovk, pk, 19999, {});
auto tx3 = builder3.Build().GetTxOrThrow();
@ -776,11 +766,9 @@ TEST(WalletTests, GetConflictedSaplingNotes) {
// Tear down
chainActive.SetTip(NULL);
mapBlockIndex.erase(blockHash);
}
// Revert to test default
RegtestDeactivateCanopy();
RegtestActivateSapling();
(*deactivations[ver])();
}
}
TEST(WalletTests, SproutNullifierIsSpent) {
@ -1034,18 +1022,14 @@ 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);
int overwinterActivationHeight = 5;
int saplingActivationHeight = 30;
int canopyActivationHeight = 70;
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, overwinterActivationHeight);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, saplingActivationHeight);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_CANOPY, canopyActivationHeight);
auto consensusParams = Params().GetConsensus();
std::vector<libzcash::Zip212Enabled> zip_212_enabled = {libzcash::Zip212Enabled::BeforeZip212, libzcash::Zip212Enabled::AfterZip212};
int builderHeights[] = {saplingActivationHeight, canopyActivationHeight};
const Consensus::Params& (*activations [])() = {RegtestActivateSapling, RegtestActivateCanopy};
void (*deactivations [])() = {RegtestDeactivateSapling, RegtestDeactivateCanopy};
for (int ver = 0; ver < zip_212_enabled.size(); ver++) {
auto consensusParams = (*activations[ver])();
TestWallet wallet;
LOCK2(cs_main, wallet.cs_wallet);
@ -1065,7 +1049,7 @@ TEST(WalletTests, SpentSaplingNoteIsFromMe) {
auto witness = saplingTree.witness();
// Generate transaction, which sends funds to note B
auto builder = TransactionBuilder(consensusParams, builderHeights[ver]);
auto builder = TransactionBuilder(consensusParams, 1);
builder.AddSaplingSpend(expsk, note, anchor, witness);
builder.AddSaplingOutput(extfvk.fvk.ovk, pk, 25000, {});
auto tx = builder.Build().GetTxOrThrow();
@ -1087,7 +1071,7 @@ TEST(WalletTests, SpentSaplingNoteIsFromMe) {
EXPECT_TRUE(chainActive.Contains(&fakeIndex));
EXPECT_EQ(0, chainActive.Height());
auto saplingNoteData = wallet.FindMySaplingNotes(wtx, builderHeights[ver]).first;
auto saplingNoteData = wallet.FindMySaplingNotes(wtx, 1).first;
ASSERT_TRUE(saplingNoteData.size() > 0);
wtx.SetSaplingNoteData(saplingNoteData);
wtx.SetMerkleBranch(block);
@ -1137,7 +1121,7 @@ TEST(WalletTests, SpentSaplingNoteIsFromMe) {
anchor = saplingTree.root();
// Create transaction to spend note B
auto builder2 = TransactionBuilder(consensusParams, builderHeights[ver] + 1);
auto builder2 = TransactionBuilder(consensusParams, 2);
builder2.AddSaplingSpend(expsk, note2, anchor, spend_note_witness);
builder2.AddSaplingOutput(extfvk.fvk.ovk, pk, 12500, {});
auto tx2 = builder2.Build().GetTxOrThrow();
@ -1164,7 +1148,7 @@ TEST(WalletTests, SpentSaplingNoteIsFromMe) {
EXPECT_TRUE(chainActive.Contains(&fakeIndex2));
EXPECT_EQ(1, chainActive.Height());
auto saplingNoteData2 = wallet.FindMySaplingNotes(wtx2, builderHeights[ver]).first;
auto saplingNoteData2 = wallet.FindMySaplingNotes(wtx2, 2).first;
ASSERT_TRUE(saplingNoteData2.size() > 0);
wtx2.SetSaplingNoteData(saplingNoteData2);
wtx2.SetMerkleBranch(block2);
@ -1181,11 +1165,9 @@ TEST(WalletTests, SpentSaplingNoteIsFromMe) {
chainActive.SetTip(NULL);
mapBlockIndex.erase(blockHash);
mapBlockIndex.erase(blockHash2);
}
// Revert to test default
RegtestDeactivateCanopy();
RegtestActivateSapling();
(*deactivations[ver])();
}
}
TEST(WalletTests, CachedWitnessesEmptyChain) {