From 6ad4db225386a748d9da2825b61e2389cfeba672 Mon Sep 17 00:00:00 2001 From: Taylor Hornby Date: Wed, 13 Apr 2016 16:52:24 -0600 Subject: [PATCH] Add check that vpubs are not both nonzero and test it. --- src/Makefile.gtest.include | 15 +++++++++++---- src/gtest/test_checktransaction.cpp | 26 ++++++++++++++++++++++++++ src/main.cpp | 20 +++++++++++++++----- 3 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 src/gtest/test_checktransaction.cpp diff --git a/src/Makefile.gtest.include b/src/Makefile.gtest.include index 8aa30e5f2..69873799f 100644 --- a/src/Makefile.gtest.include +++ b/src/Makefile.gtest.include @@ -3,11 +3,18 @@ bin_PROGRAMS += zcash-gtest # tool for generating our public parameters zcash_gtest_SOURCES = \ gtest/main.cpp \ - gtest/test_tautology.cpp + gtest/test_tautology.cpp \ + gtest/test_checktransaction.cpp -zcash_gtest_LDADD = \ - -lgtest \ - $(LIBBITCOIN_UTIL) +zcash_gtest_LDADD = -lgtest $(LIBBITCOIN_SERVER) $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL) $(LIBBITCOIN_CRYPTO) $(LIBBITCOIN_UNIVALUE) $(LIBLEVELDB) $(LIBMEMENV) \ + $(BOOST_LIBS) $(BOOST_UNIT_TEST_FRAMEWORK_LIB) $(LIBSECP256K1) +if ENABLE_WALLET +zcash_gtest_LDADD += $(LIBBITCOIN_WALLET) +endif + +zcash_gtest_LDADD += $(LIBBITCOIN_CONSENSUS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(LIBZEROCASH) $(LIBZEROCASH_LIBS) + +zcash_gtest_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) -static zcash-gtest_check: zcash-gtest FORCE ./zcash-gtest diff --git a/src/gtest/test_checktransaction.cpp b/src/gtest/test_checktransaction.cpp new file mode 100644 index 000000000..48d8d5e7f --- /dev/null +++ b/src/gtest/test_checktransaction.cpp @@ -0,0 +1,26 @@ +#include + +#include "main.h" +#include "primitives/transaction.h" +#include "consensus/validation.h" + +TEST(checktransaction_tests, check_vpub_not_both_nonzero) { + CMutableTransaction tx; + tx.nVersion = 2; + + { + // Ensure that values within the pour are well-formed. + CMutableTransaction newTx(tx); + CValidationState state; + state.SetPerformPourVerification(false); // don't verify the snark + + newTx.vpour.push_back(CPourTx()); + + CPourTx *pourtx = &newTx.vpour[0]; + pourtx->vpub_old = 1; + pourtx->vpub_new = 1; + + EXPECT_FALSE(CheckTransaction(newTx, state)); + EXPECT_EQ(state.GetRejectReason(), "bad-txns-vpubs-both-nonzero"); + } +} diff --git a/src/main.cpp b/src/main.cpp index 116a937c6..fe0969281 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -881,26 +881,36 @@ bool CheckTransaction(const CTransaction& tx, CValidationState &state) // Ensure that pour values are well-formed BOOST_FOREACH(const CPourTx& pour, tx.vpour) { - if (pour.vpub_old < 0) + if (pour.vpub_old < 0) { return state.DoS(100, error("CheckTransaction(): pour.vpub_old negative"), REJECT_INVALID, "bad-txns-vpub_old-negative"); + } - if (pour.vpub_new < 0) + if (pour.vpub_new < 0) { return state.DoS(100, error("CheckTransaction(): pour.vpub_new negative"), REJECT_INVALID, "bad-txns-vpub_new-negative"); + } - if (pour.vpub_old > MAX_MONEY) + if (pour.vpub_old > MAX_MONEY) { return state.DoS(100, error("CheckTransaction(): pour.vpub_old too high"), REJECT_INVALID, "bad-txns-vpub_old-toolarge"); + } - if (pour.vpub_new > MAX_MONEY) + if (pour.vpub_new > MAX_MONEY) { return state.DoS(100, error("CheckTransaction(): pour.vpub_new too high"), REJECT_INVALID, "bad-txns-vpub_new-toolarge"); + } + + if (pour.vpub_new != 0 && pour.vpub_old != 0) { + return state.DoS(100, error("CheckTransaction(): pour.vpub_new and pour.vpub_old both nonzero"), + REJECT_INVALID, "bad-txns-vpubs-both-nonzero"); + } nValueOut += pour.vpub_new; - if (!MoneyRange(nValueOut)) + if (!MoneyRange(nValueOut)) { return state.DoS(100, error("CheckTransaction(): txout total out of range"), REJECT_INVALID, "bad-txns-txouttotal-toolarge"); + } } -- 2.42.0