Skip to content

Commit

Permalink
Consensus: check total size of all shielded txes inside a block
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Dec 14, 2020
1 parent 3097832 commit 62fceb4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet,
vecPriority.pop_back();

// Size limits
unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
unsigned int nTxSize = tx.GetTotalSize();
if (nBlockSize + nTxSize >= nBlockMaxSize)
continue;

Expand Down
17 changes: 16 additions & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2680,6 +2680,16 @@ bool CheckColdStakeFreeOutput(const CTransaction& tx, const int nHeight)
return true;
}

// cumulative size of all shielded txes inside a block
static unsigned int GetTotalShieldedTxSize(const CBlock& block)
{
unsigned int nSizeShielded = 0;
for (const auto& tx : block.vtx) {
if (tx->IsShieldedTx()) nSizeShielded += tx->GetTotalSize();
}
return nSizeShielded;
}

bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckSig)
{
if (block.fChecked)
Expand Down Expand Up @@ -2713,9 +2723,14 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo

// Size limits
unsigned int nMaxBlockSize = MAX_BLOCK_SIZE_CURRENT;
if (block.vtx.empty() || block.vtx.size() > nMaxBlockSize || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION) > nMaxBlockSize)
const unsigned int nBlockSize = ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION);
if (block.vtx.empty() || block.vtx.size() > nMaxBlockSize || nBlockSize > nMaxBlockSize)
return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed");

// Check shielded txes limits (no need to check if the block size is already under 750kB)
if (nBlockSize > MAX_BLOCK_SHIELDED_TXES_SIZE && GetTotalShieldedTxSize(block) > MAX_BLOCK_SHIELDED_TXES_SIZE)
return state.DoS(100, false, REJECT_INVALID, "bad-blk-shielded-size", false, "shielded size limits failed");

// First transaction must be coinbase, the rest must not be
if (block.vtx.empty() || !block.vtx[0]->IsCoinBase())
return state.DoS(100, false, REJECT_INVALID, "bad-cb-missing", false, "first tx is not coinbase");
Expand Down

0 comments on commit 62fceb4

Please sign in to comment.