Extract input standardness check into a separate callable method.

This commit is contained in:
Mike Hearn 2014-10-01 14:14:34 +02:00
parent c5a564e152
commit 5fc98d2c07
2 changed files with 25 additions and 6 deletions

View File

@ -18,6 +18,7 @@
package org.bitcoinj.core;
import org.bitcoinj.script.Script;
import org.bitcoinj.wallet.DefaultRiskAnalysis;
import org.bitcoinj.wallet.KeyBag;
import org.bitcoinj.wallet.RedeemData;
@ -455,6 +456,17 @@ public class TransactionInput extends ChildMessage implements Serializable {
return new TransactionInput(params, null, bitcoinSerialize(), 0);
}
/**
* <p>Returns either RuleViolation.NONE if the input is standard, or which rule makes it non-standard if so.
* The "IsStandard" rules control whether the default Bitcoin Core client blocks relay of a tx / refuses to mine it,
* however, non-standard transactions can still be included in blocks and will be accepted as valid if so.</p>
*
* <p>This method simply calls <tt>DefaultRiskAnalysis.isInputStandard(this)</tt>.</p>
*/
public DefaultRiskAnalysis.RuleViolation isStandard() {
return DefaultRiskAnalysis.isInputStandard(this);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -142,18 +142,25 @@ public class DefaultRiskAnalysis implements RiskAnalysis {
final List<TransactionInput> inputs = tx.getInputs();
for (int i = 0; i < inputs.size(); i++) {
TransactionInput input = inputs.get(i);
for (ScriptChunk chunk : input.getScriptSig().getChunks()) {
if (chunk.data != null && !chunk.isShortestPossiblePushData()) {
log.warn("TX considered non-standard due to input {} having a longer than necessary data push: {}",
i, chunk);
return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
}
RuleViolation violation = isInputStandard(input);
if (violation != RuleViolation.NONE) {
log.warn("TX considered non-standard due to input {} violating rule {}", i, violation);
return violation;
}
}
return RuleViolation.NONE;
}
/** Checks if the given input passes some of the AreInputsStandard checks. Not complete. */
public static RuleViolation isInputStandard(TransactionInput input) {
for (ScriptChunk chunk : input.getScriptSig().getChunks()) {
if (chunk.data != null && !chunk.isShortestPossiblePushData())
return RuleViolation.SHORTEST_POSSIBLE_PUSHDATA;
}
return RuleViolation.NONE;
}
private Result analyzeIsStandard() {
// The IsStandard rules don't apply on testnet, because they're just a safety mechanism and we don't want to
// crush innovation with valueless test coins.