diff --git a/src/block_proof.cpp b/src/block_proof.cpp index 2a73c862a0..33611b203a 100644 --- a/src/block_proof.cpp +++ b/src/block_proof.cpp @@ -19,21 +19,18 @@ bool CheckChallenge(const CBlockHeader& block, const CBlockIndex& indexLast, con } } -void ResetChallenge(CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params& params) +static bool CheckProofGeneric(const CBlockHeader& block, const uint32_t max_block_signature_size, const CScript& challenge, const CScript& scriptSig, const CScriptWitness& witness) { - block.proof.challenge = indexLast.proof.challenge; -} - -static bool CheckProofGeneric(const CBlockHeader& block, const Consensus::Params& params, const CScript& challenge) -{ - if (block.GetHash() == params.hashGenesisBlock) - return true; - - if (block.proof.solution.size() > params.max_block_signature_size) { + // scriptSig or witness will be nonempty, but not both, so just compare both limits + if (scriptSig.size() > max_block_signature_size) { return false; } - // Some anti-DoS flags, though consensus.max_block_signature_size caps the possible + if (witness.GetSerializedSize() > max_block_signature_size) { + return false; + } + + // Some anti-DoS flags, though max_block_signature_size caps the possible // danger in malleation of the block witness data. unsigned int proof_flags = SCRIPT_VERIFY_P2SH // For cleanstack evalution under segwit flag | SCRIPT_VERIFY_STRICTENC // Minimally-sized DER sigs @@ -42,15 +39,20 @@ static bool CheckProofGeneric(const CBlockHeader& block, const Consensus::Params | SCRIPT_VERIFY_MINIMALDATA // Pushes are minimally-sized | SCRIPT_VERIFY_SIGPUSHONLY // Witness is push-only | SCRIPT_VERIFY_LOW_S // Stop easiest signature fiddling - | SCRIPT_VERIFY_WITNESS // Required for cleanstack eval in VerifyScript + | SCRIPT_VERIFY_WITNESS // Witness and to enforce cleanstack | SCRIPT_NO_SIGHASH_BYTE; // non-Check(Multi)Sig signatures will not have sighash byte - return GenericVerifyScript(block.proof.solution, challenge, proof_flags, block); + return GenericVerifyScript(scriptSig, witness, challenge, proof_flags, block); } bool CheckProof(const CBlockHeader& block, const Consensus::Params& params) { if (g_signed_blocks) { - return CheckProofGeneric(block, params, params.signblockscript); + const DynaFedParams& d_params = block.m_dyna_params; + if (d_params.IsNull()) { + return CheckProofGeneric(block, params.max_block_signature_size, params.signblockscript, block.proof.solution, CScriptWitness()); + } else { + return CheckProofGeneric(block, d_params.m_current.m_sbs_wit_limit, d_params.m_current.m_signblockscript, CScript(), block.m_signblock_witness); + } } else { return CheckProofOfWork(block.GetHash(), block.nBits, params); } @@ -58,10 +60,15 @@ bool CheckProof(const CBlockHeader& block, const Consensus::Params& params) bool CheckProofSignedParent(const CBlockHeader& block, const Consensus::Params& params) { - return CheckProofGeneric(block, params, params.parent_chain_signblockscript); -} - -void ResetProof(CBlockHeader& block) -{ - block.proof.solution.clear(); + const DynaFedParams& d_params = block.m_dyna_params; + if (d_params.IsNull()) { + return CheckProofGeneric(block, params.max_block_signature_size, params.parent_chain_signblockscript, block.proof.solution, CScriptWitness()); + } else { + // Dynamic federations means we cannot validate the signer set + // at least without tracking the parent chain more directly. + // Note that we do not even serialize dynamic federation block witness data + // currently for merkle proofs which is the only context in which + // this function is currently used. + return true; + } } diff --git a/src/block_proof.h b/src/block_proof.h index 256d57d5f8..273699f37f 100644 --- a/src/block_proof.h +++ b/src/block_proof.h @@ -20,8 +20,6 @@ class CScript; /** Check on header proof, depending on chain type, PoW or signed **/ bool CheckProof(const CBlockHeader& block, const Consensus::Params&); bool CheckProofSignedParent(const CBlockHeader& block, const Consensus::Params&); -void ResetProof(CBlockHeader& block); bool CheckChallenge(const CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params&); -void ResetChallenge(CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params&); #endif // BITCOIN_BLOCK_PROOF_H diff --git a/src/miner.cpp b/src/miner.cpp index 20e31a5fec..b19f2a8768 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -25,13 +25,21 @@ #include #include -// ELEMENTS -#include // ResetProof, ResetChallenge #include #include #include +void ResetChallenge(CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params& params) +{ + block.proof.challenge = indexLast.proof.challenge; +} + +void ResetProof(CBlockHeader& block) +{ + block.proof.solution.clear(); +} + int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev) { int64_t nOldTime = pblock->nTime; @@ -161,7 +169,6 @@ std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& sc // Pad block weight by block proof fields (including upper-bound of signature) nBlockWeight += chainparams.GetConsensus().signblockscript.size() * WITNESS_SCALE_FACTOR; nBlockWeight += chainparams.GetConsensus().max_block_signature_size * WITNESS_SCALE_FACTOR; - // Reset block proof ResetProof(*pblock); ResetChallenge(*pblock, *pindexPrev, chainparams.GetConsensus()); } diff --git a/src/script/generic.hpp b/src/script/generic.hpp index 3b81a520bc..c9e7949f38 100644 --- a/src/script/generic.hpp +++ b/src/script/generic.hpp @@ -45,9 +45,9 @@ public: }; template -bool GenericVerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, unsigned int flags, const T& data) +bool GenericVerifyScript(const CScript& scriptSig, const CScriptWitness& witness, const CScript& scriptPubKey, unsigned int flags, const T& data) { - return VerifyScript(scriptSig, scriptPubKey, NULL, flags, SimpleSignatureChecker(SerializeHash(data))); + return VerifyScript(scriptSig, scriptPubKey, &witness, flags, SimpleSignatureChecker(SerializeHash(data))); } template diff --git a/src/script/script.cpp b/src/script/script.cpp index 386f9a19a5..a24433e92d 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -335,6 +335,11 @@ std::string CScriptWitness::ToString() const return ret + ")"; } +uint32_t CScriptWitness::GetSerializedSize() const +{ + return ::GetSerializeSize(stack, 0); +} + bool CScript::HasValidOps() const { CScript::const_iterator it = begin(); diff --git a/src/script/script.h b/src/script/script.h index bab3b62d06..d004d41785 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -603,6 +603,8 @@ struct CScriptWitness void SetNull() { stack.clear(); stack.shrink_to_fit(); } std::string ToString() const; + + uint32_t GetSerializedSize() const; }; class CReserveScript