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

Remint tests for same serial in one block and forged sigma mint desti… #551

Merged
merged 1 commit into from
Jul 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions src/test/remint_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ BOOST_AUTO_TEST_CASE(remint_basic_test)
string stringError;
CZerocoinState *zerocoinState = CZerocoinState::GetZerocoinState();

CWalletDB walletdb(pwalletMain->strWalletFile);
std::list<CZerocoinEntry> zcEntries;
decltype(zerocoinState->usedCoinSerials) tempSerials;

pwalletMain->SetBroadcastTransactions(true);

// Mint 1 XZC zerocoin and remint it on wrong fork
Expand All @@ -56,8 +60,58 @@ BOOST_AUTO_TEST_CASE(remint_basic_test)
// Get to the sigma portion
for (int i=0; i<200; i++)
CreateAndProcessBlock({}, scriptPubKey);

CWalletTx remintOnWrongForkTx;
BOOST_CHECK_MESSAGE(pwalletMain->CreateZerocoinToSigmaRemintModel(stringError, ZEROCOIN_TX_VERSION_2, (libzerocoin::CoinDenomination)1, &remintOnWrongForkTx), stringError + " - Remint failed");

// try to double-remint one coin in a single block
walletdb.ListPubCoin(zcEntries);
for (auto &zcEntry : zcEntries) {
if (zcEntry.IsUsedForRemint) {
zcEntry.IsUsed = zcEntry.IsUsedForRemint = false;
walletdb.WriteZerocoinEntry(zcEntry);
}
}
BOOST_CHECK_MESSAGE(pwalletMain->CreateZerocoinToSigmaRemintModel(stringError, ZEROCOIN_TX_VERSION_2, (libzerocoin::CoinDenomination)1), stringError + " - Remint failed");
// Mempool should contain two remint transactions both having the same serial
BOOST_CHECK(mempool.size() == 2);

// Try to form a block - should fail
try {
CreateAndProcessBlock({}, scriptPubKey);
BOOST_FAIL("Block is created despite having double spend in it");
}
catch (std::runtime_error &err) {
BOOST_CHECK(strstr(err.what(), "TestBlockValidity") != nullptr);
}

// clear the mempool
mempool.clear();

// Try to change destination sigma mint for the remint transaction. Should fail because metadata signature is wrong
CWalletTx txCopy = remintOnWrongForkTx;
sigma::Params *sigmaParams = sigma::Params::get_default();

sigma::PrivateCoin fakeCoin(sigmaParams, sigma::CoinDenomination::SIGMA_DENOM_1, ZEROCOIN_TX_VERSION_3);
sigma::PublicCoin fakePubCoin = fakeCoin.getPublicCoin();
BOOST_CHECK(fakePubCoin.validate());

CScript sigmaMintScript;
sigmaMintScript << OP_SIGMAMINT;
std::vector<unsigned char> vch = fakePubCoin.getValue().getvch();
sigmaMintScript.insert(sigmaMintScript.end(), vch.begin(), vch.end());

CTxOut sigmaTxOut;
sigmaTxOut.scriptPubKey = sigmaMintScript;
sigma::DenominationToInteger(sigma::CoinDenomination::SIGMA_DENOM_1, sigmaTxOut.nValue);
txCopy.vout[0] = sigmaTxOut;

pwalletMain->CommitTransaction(txCopy);
BOOST_CHECK_MESSAGE(mempool.size() == 0, "Transaction is accepted despite having forged destination");

pwalletMain->CommitTransaction(remintOnWrongForkTx);
BOOST_CHECK(mempool.size() == 1);

CreateAndProcessBlock({}, scriptPubKey);

// Invalidate chain
Expand Down Expand Up @@ -112,14 +166,13 @@ BOOST_AUTO_TEST_CASE(remint_basic_test)

// try double spend by modifying wallet and list of spent serials
//Temporary disable usedCoinSerials check to force double spend in mempool
CWalletDB walletdb(pwalletMain->strWalletFile);
for (const Bignum &serial: zerocoinState->usedCoinSerials) {
CZerocoinSpendEntry spendEntry;
spendEntry.coinSerial = serial;
walletdb.EraseCoinSpendSerialEntry(spendEntry);
}

std::list<CZerocoinEntry> zcEntries;
zcEntries.clear();
walletdb.ListPubCoin(zcEntries);
for (auto &zcEntry : zcEntries) {
if (zcEntry.IsUsedForRemint) {
Expand All @@ -128,7 +181,7 @@ BOOST_AUTO_TEST_CASE(remint_basic_test)
}
}

auto tempSerials = zerocoinState->usedCoinSerials;
tempSerials = zerocoinState->usedCoinSerials;
zerocoinState->usedCoinSerials.clear();

// Retry remint of 1. Should pass for now
Expand Down