From 15ec5525e316d41dfe110263062a97b7530c6be0 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 26 Apr 2018 14:18:36 +0100 Subject: [PATCH] Raise 100kB transaction size limit from Sapling activation Closes #2864. --- src/consensus/consensus.h | 3 ++- src/gtest/test_checktransaction.cpp | 41 ++++++++++++++++++++++++++--- src/main.cpp | 11 +++++++- src/wallet/rpcwallet.cpp | 34 +++++++++++++++++------- src/wallet/wallet.cpp | 7 ++++- 5 files changed, 81 insertions(+), 15 deletions(-) diff --git a/src/consensus/consensus.h b/src/consensus/consensus.h index 1efaf99ea..112fa626a 100644 --- a/src/consensus/consensus.h +++ b/src/consensus/consensus.h @@ -23,7 +23,8 @@ static const unsigned int MAX_BLOCK_SIZE = 2000000; /** The maximum allowed number of signature check operations in a block (network rule) */ static const unsigned int MAX_BLOCK_SIGOPS = 20000; /** The maximum size of a transaction (network rule) */ -static const unsigned int MAX_TX_SIZE = 100000; +static const unsigned int MAX_TX_SIZE_BEFORE_SAPLING = 100000; +static const unsigned int MAX_TX_SIZE = MAX_BLOCK_SIZE; /** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */ static const int COINBASE_MATURITY = 100; /** The minimum value which is invalid for expiry height, used by CTransaction and CMutableTransaction */ diff --git a/src/gtest/test_checktransaction.cpp b/src/gtest/test_checktransaction.cpp index d7527dbac..bc60209cb 100644 --- a/src/gtest/test_checktransaction.cpp +++ b/src/gtest/test_checktransaction.cpp @@ -43,6 +43,7 @@ public: MOCK_CONST_METHOD0(GetRejectReason, std::string()); }; +void CreateJoinSplitSignature(CMutableTransaction& mtx, uint32_t consensusBranchId); CMutableTransaction GetValidTransaction() { uint32_t consensusBranchId = SPROUT_BRANCH_ID; @@ -63,7 +64,11 @@ CMutableTransaction GetValidTransaction() { mtx.vjoinsplit[1].nullifiers.at(0) = uint256S("0000000000000000000000000000000000000000000000000000000000000002"); mtx.vjoinsplit[1].nullifiers.at(1) = uint256S("0000000000000000000000000000000000000000000000000000000000000003"); + CreateJoinSplitSignature(mtx, consensusBranchId); + return mtx; +} +void CreateJoinSplitSignature(CMutableTransaction& mtx, uint32_t consensusBranchId) { // Generate an ephemeral keypair. uint256 joinSplitPubKey; unsigned char joinSplitPrivKey[crypto_sign_SECRETKEYBYTES]; @@ -86,7 +91,6 @@ CMutableTransaction GetValidTransaction() { dataToBeSigned.begin(), 32, joinSplitPrivKey ) == 0); - return mtx; } TEST(checktransaction_tests, valid_transaction) { @@ -129,7 +133,8 @@ TEST(checktransaction_tests, bad_txns_vout_empty) { CheckTransactionWithoutProofVerification(tx, state); } -TEST(checktransaction_tests, bad_txns_oversize) { +TEST(checktransaction_tests, BadTxnsOversize) { + SelectParams(CBaseChainParams::REGTEST); CMutableTransaction mtx = GetValidTransaction(); mtx.vin[0].scriptSig = CScript(); @@ -153,9 +158,39 @@ TEST(checktransaction_tests, bad_txns_oversize) { CTransaction tx(mtx); ASSERT_EQ(::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION), 100202); + // Passes non-contextual checks... MockCValidationState state; + EXPECT_TRUE(CheckTransactionWithoutProofVerification(tx, state)); + + // ... but fails contextual ones! EXPECT_CALL(state, DoS(100, false, REJECT_INVALID, "bad-txns-oversize", false)).Times(1); - CheckTransactionWithoutProofVerification(tx, state); + EXPECT_FALSE(ContextualCheckTransaction(tx, state, 1, 100)); + } + + { + // But should be fine again once Sapling activates! + UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); + UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); + + mtx.fOverwintered = true; + mtx.nVersionGroupId = SAPLING_VERSION_GROUP_ID; + mtx.nVersion = SAPLING_TX_VERSION; + + // Change the proof types (which requires re-signing the JoinSplit data) + mtx.vjoinsplit[0].proof = libzcash::GrothProof(); + mtx.vjoinsplit[1].proof = libzcash::GrothProof(); + CreateJoinSplitSignature(mtx, NetworkUpgradeInfo[Consensus::UPGRADE_SAPLING].nBranchId); + + CTransaction tx(mtx); + EXPECT_EQ(::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION), 103713); + + MockCValidationState state; + EXPECT_TRUE(CheckTransactionWithoutProofVerification(tx, state)); + 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); } } diff --git a/src/main.cpp b/src/main.cpp index 27d045135..2824f015b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -943,6 +943,15 @@ bool ContextualCheckTransaction(const CTransaction& tx, CValidationState &state, } } + // Rules that apply before Sapling: + if (!saplingActive) { + // Size limits + BOOST_STATIC_ASSERT(MAX_BLOCK_SIZE > MAX_TX_SIZE_BEFORE_SAPLING); // sanity + if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) > MAX_TX_SIZE_BEFORE_SAPLING) + return state.DoS(100, error("ContextualCheckTransaction(): size limits failed"), + REJECT_INVALID, "bad-txns-oversize"); + } + if (!(tx.IsCoinBase() || tx.vjoinsplit.empty())) { auto consensusBranchId = CurrentEpochBranchId(nHeight, Params().GetConsensus()); // Empty output script. @@ -1051,7 +1060,7 @@ bool CheckTransactionWithoutProofVerification(const CTransaction& tx, CValidatio REJECT_INVALID, "bad-txns-vout-empty"); // Size limits - BOOST_STATIC_ASSERT(MAX_BLOCK_SIZE > MAX_TX_SIZE); // sanity + BOOST_STATIC_ASSERT(MAX_BLOCK_SIZE >= MAX_TX_SIZE); // sanity if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) > MAX_TX_SIZE) return state.DoS(100, error("CheckTransaction(): size limits failed"), REJECT_INVALID, "bad-txns-oversize"); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 23bfea02c..7c3d840b6 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -3489,6 +3489,7 @@ UniValue z_getoperationstatus_IMPL(const UniValue& params, bool fRemoveFinishedO // If input notes are small, we might actually require more than one joinsplit per zaddr output. // For now though, we assume we use one joinsplit per zaddr output (and the second output note is change). // We reduce the result by 1 to ensure there is room for non-joinsplit CTransaction data. +#define Z_SENDMANY_MAX_ZADDR_OUTPUTS_BEFORE_SAPLING ((MAX_TX_SIZE_BEFORE_SAPLING / GetSerializeSize(JSDescription(), SER_NETWORK, PROTOCOL_VERSION)) - 1) #define Z_SENDMANY_MAX_ZADDR_OUTPUTS ((MAX_TX_SIZE / GetSerializeSize(JSDescription(), SER_NETWORK, PROTOCOL_VERSION)) - 1) // transaction.h comment: spending taddr output requires CTxIn >= 148 bytes and typical taddr txout is 34 bytes @@ -3620,8 +3621,16 @@ UniValue z_sendmany(const UniValue& params, bool fHelp) nTotalOut += nAmount; } + int nextBlockHeight = chainActive.Height() + 1; + size_t max_zaddr_outputs = Z_SENDMANY_MAX_ZADDR_OUTPUTS; + unsigned int max_tx_size = MAX_TX_SIZE; + if (!NetworkUpgradeActive(nextBlockHeight, Params().GetConsensus(), Consensus::UPGRADE_SAPLING)) { + max_zaddr_outputs = Z_SENDMANY_MAX_ZADDR_OUTPUTS_BEFORE_SAPLING; + max_tx_size = MAX_TX_SIZE_BEFORE_SAPLING; + } + // Check the number of zaddr outputs does not exceed the limit. - if (zaddrRecipients.size() > Z_SENDMANY_MAX_ZADDR_OUTPUTS) { + if (zaddrRecipients.size() > max_zaddr_outputs) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, too many zaddr outputs"); } @@ -3640,8 +3649,8 @@ UniValue z_sendmany(const UniValue& params, bool fHelp) txsize += CTXOUT_REGULAR_SIZE; // There will probably be taddr change } txsize += CTXOUT_REGULAR_SIZE * taddrRecipients.size(); - if (txsize > MAX_TX_SIZE) { - throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Too many outputs, size of raw transaction would be larger than limit of %d bytes", MAX_TX_SIZE )); + if (txsize > max_tx_size) { + throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Too many outputs, size of raw transaction would be larger than limit of %d bytes", max_tx_size )); } // Minimum confirmations @@ -3677,7 +3686,6 @@ UniValue z_sendmany(const UniValue& params, bool fHelp) UniValue contextInfo = o; // Contextual transaction we will build on - int nextBlockHeight = chainActive.Height() + 1; CMutableTransaction contextualTx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nextBlockHeight); bool isShielded = !fromTaddr || zaddrRecipients.size() > 0; if (contextualTx.nVersion == 1 && isShielded) { @@ -3725,7 +3733,7 @@ UniValue z_shieldcoinbase(const UniValue& params, bool fHelp) "\nby the caller. If the limit parameter is set to zero, and Overwinter is not yet active, the -mempooltxinputlimit" "\noption will determine the number of uxtos. Any limit is constrained by the consensus rule defining a maximum" "\ntransaction size of " - + strprintf("%d bytes.", MAX_TX_SIZE) + + strprintf("%d bytes before Sapling, and %d bytes once Sapling activates.", MAX_TX_SIZE_BEFORE_SAPLING, MAX_TX_SIZE) + HelpRequiringPassphrase() + "\n" "\nArguments:\n" "1. \"fromaddress\" (string, required) The address is a taddr or \"*\" for all taddrs belonging to the wallet.\n" @@ -3789,6 +3797,10 @@ UniValue z_shieldcoinbase(const UniValue& params, bool fHelp) int nextBlockHeight = chainActive.Height() + 1; bool overwinterActive = NetworkUpgradeActive(nextBlockHeight, Params().GetConsensus(), Consensus::UPGRADE_OVERWINTER); + unsigned int max_tx_size = MAX_TX_SIZE; + if (!NetworkUpgradeActive(nextBlockHeight, Params().GetConsensus(), Consensus::UPGRADE_SAPLING)) { + max_tx_size = MAX_TX_SIZE_BEFORE_SAPLING; + } // Prepare to get coinbase utxos std::vector inputs; @@ -3833,7 +3845,7 @@ UniValue z_shieldcoinbase(const UniValue& params, bool fHelp) if (!maxedOutFlag) { size_t increase = (boost::get(&address) != nullptr) ? CTXIN_SPEND_P2SH_SIZE : CTXIN_SPEND_DUST_SIZE; - if (estimatedTxSize + increase >= MAX_TX_SIZE || + if (estimatedTxSize + increase >= max_tx_size || (mempoolLimit > 0 && utxoCounter > mempoolLimit)) { maxedOutFlag = true; @@ -3928,7 +3940,7 @@ UniValue z_mergetoaddress(const UniValue& params, bool fHelp) "\n\nThe number of UTXOs and notes selected for merging can be limited by the caller. If the transparent limit" "\nparameter is set to zero, and Overwinter is not yet active, the -mempooltxinputlimit option will determine the" "\nnumber of UTXOs. Any limit is constrained by the consensus rule defining a maximum transaction size of " - + strprintf("%d bytes.", MAX_TX_SIZE) + + strprintf("%d bytes before Sapling, and %d bytes once Sapling activates.", MAX_TX_SIZE_BEFORE_SAPLING, MAX_TX_SIZE) + HelpRequiringPassphrase() + "\n" "\nArguments:\n" "1. fromaddresses (string, required) A JSON array with addresses.\n" @@ -4081,6 +4093,10 @@ UniValue z_mergetoaddress(const UniValue& params, bool fHelp) int nextBlockHeight = chainActive.Height() + 1; bool overwinterActive = NetworkUpgradeActive(nextBlockHeight, Params().GetConsensus(), Consensus::UPGRADE_OVERWINTER); + unsigned int max_tx_size = MAX_TX_SIZE; + if (!NetworkUpgradeActive(nextBlockHeight, Params().GetConsensus(), Consensus::UPGRADE_SAPLING)) { + max_tx_size = MAX_TX_SIZE_BEFORE_SAPLING; + } // Prepare to get UTXOs and notes std::vector utxoInputs; @@ -4125,7 +4141,7 @@ UniValue z_mergetoaddress(const UniValue& params, bool fHelp) if (!maxedOutUTXOsFlag) { size_t increase = (boost::get(&address) != nullptr) ? CTXIN_SPEND_P2SH_SIZE : CTXIN_SPEND_DUST_SIZE; - if (estimatedTxSize + increase >= MAX_TX_SIZE || + if (estimatedTxSize + increase >= max_tx_size || (mempoolLimit > 0 && utxoCounter > mempoolLimit)) { maxedOutUTXOsFlag = true; @@ -4157,7 +4173,7 @@ UniValue z_mergetoaddress(const UniValue& params, bool fHelp) // If we haven't added any notes yet and the merge is to a // z-address, we have already accounted for the first JoinSplit. size_t increase = (noteInputs.empty() && !isToZaddr) || (noteInputs.size() % 2 == 0) ? JOINSPLIT_SIZE : 0; - if (estimatedTxSize + increase >= MAX_TX_SIZE || + if (estimatedTxSize + increase >= max_tx_size || (nNoteLimit > 0 && noteCounter > nNoteLimit)) { maxedOutNotesFlag = true; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 3db3e26c3..0f2fd649d 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2589,6 +2589,11 @@ bool CWallet::CreateTransaction(const vector& vecSend, CWalletTx& wt txNew.nExpiryHeight = nextBlockHeight + expiryDelta; } } + + unsigned int max_tx_size = MAX_TX_SIZE; + if (!NetworkUpgradeActive(nextBlockHeight, Params().GetConsensus(), Consensus::UPGRADE_SAPLING)) { + max_tx_size = MAX_TX_SIZE_BEFORE_SAPLING; + } // Discourage fee sniping. // @@ -2828,7 +2833,7 @@ bool CWallet::CreateTransaction(const vector& vecSend, CWalletTx& wt *static_cast(&wtxNew) = CTransaction(txNew); // Limit size - if (nBytes >= MAX_TX_SIZE) + if (nBytes >= max_tx_size) { strFailReason = _("Transaction too large"); return false;