MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / IsWitnessStandard

Function IsWitnessStandard

src/policy/policy.cpp:265–352  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

263}
264
265bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
266{
267 if (tx.IsCoinBase())
268 return true; // Coinbases are skipped
269
270 for (unsigned int i = 0; i < tx.vin.size(); i++)
271 {
272 // We don't care if witness for this input is empty, since it must not be bloated.
273 // If the script is invalid without witness, it would be caught sooner or later during validation.
274 if (tx.vin[i].scriptWitness.IsNull())
275 continue;
276
277 const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
278
279 // get the scriptPubKey corresponding to this input:
280 CScript prevScript = prev.scriptPubKey;
281
282 // witness stuffing detected
283 if (prevScript.IsPayToAnchor()) {
284 return false;
285 }
286
287 bool p2sh = false;
288 if (prevScript.IsPayToScriptHash()) {
289 std::vector <std::vector<unsigned char> > stack;
290 // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
291 // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
292 // If the check fails at this stage, we know that this txid must be a bad one.
293 if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
294 return false;
295 if (stack.empty())
296 return false;
297 prevScript = CScript(stack.back().begin(), stack.back().end());
298 p2sh = true;
299 }
300
301 int witnessversion = 0;
302 std::vector<unsigned char> witnessprogram;
303
304 // Non-witness program must not be associated with any witness
305 if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
306 return false;
307
308 // Check P2WSH standard limits
309 if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
310 if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
311 return false;
312 size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
313 if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
314 return false;
315 for (unsigned int j = 0; j < sizeWitnessStack; j++) {
316 if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
317 return false;
318 }
319 }
320
321 // Check policy limits for Taproot spends:
322 // - MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE limit for stack item size

Callers 3

PreChecksMethod · 0.85
TestCoinsViewFunction · 0.85
transaction.cppFile · 0.85

Calls 13

EvalScriptFunction · 0.85
IsPayToAnchorMethod · 0.80
IsPayToScriptHashMethod · 0.80
CScriptClass · 0.70
IsCoinBaseMethod · 0.45
sizeMethod · 0.45
IsNullMethod · 0.45
emptyMethod · 0.45
beginMethod · 0.45
backMethod · 0.45
endMethod · 0.45

Tested by 1

TestCoinsViewFunction · 0.68