Commit 9cf8d1df authored by Ivan Jose Lozano Fernandez's avatar Ivan Jose Lozano Fernandez Committed by Daniel Connolly

Refactoring. Removing all the ScriptException subclasses. Instead, we are now...

Refactoring. Removing all the ScriptException subclasses. Instead, we are now using the "ScriptError" where all the possible causes for the Script failure can be stored and organized. This way, the Script failure always throws an ScriptException (easier to managed). This is how it's been implementrd in bitcoinJ (reference).
parent 75dbca30
......@@ -21,146 +21,28 @@
package org.bitcoinj.core;
import com.sun.xml.internal.bind.v2.TODO;
import org.bitcoinj.script.ScriptError;
public class ScriptException extends VerificationException {
public ScriptException(String msg) {
private ScriptError err = null;
public ScriptException(ScriptError err, String msg) {
super(msg);
this.err = err;
}
public ScriptException(String msg, Exception e) {
public ScriptException(ScriptError err, String msg, Exception e) {
super(msg, e);
this.err = err;
}
public static class BadOpcodeException extends ScriptException {
public BadOpcodeException() { super("an illegal opcode is present in the script"); }
}
public static class DisabledOpcodeException extends ScriptException {
public DisabledOpcodeException() { super("script includes a disabled opcode"); }
}
public static class DiscourageUpgradableNopsException extends ScriptException {
public DiscourageUpgradableNopsException() { super("script used a reserved opcode"); }
}
public static class DivByZeroException extends ScriptException {
public DivByZeroException() { super("divide by zero error"); }
}
public static class EqualVerifyException extends ScriptException {
public EqualVerifyException() { super("OP_EQUALVERIFY failed, non-equal operands"); }
}
public static class EvalFalseException extends ScriptException {
public EvalFalseException() { super("script evaluated false"); }
}
public static class ImpossibleEncoding extends ScriptException {
public ImpossibleEncoding() { super("the encoding is not possible"); }
}
public static class InvalidAltStackOperationException extends ScriptException {
public InvalidAltStackOperationException() { super("the operation was invalid given the contents of the altstack"); }
}
public static class InvalidNumberRangeException extends ScriptException {
public InvalidNumberRangeException() { super("operand is not a number in the valid range"); }
}
public static class InvalidStackOperationException extends ScriptException {
/* some possible causes:
* - not enough values on the stack for the operation
*/
public InvalidStackOperationException() { super("the operation was invalid given the contents of the stack"); }
}
public static class ModByZeroException extends ScriptException {
public ModByZeroException() { super("modulo by zero error"); }
}
public static class NonStandardScriptException extends ScriptException {
public NonStandardScriptException() { super("script is not a recognized standard script"); }
}
public static class NullDummyException extends ScriptException {
public NullDummyException() { super("CHECKMULTISIG with non-null nulldummy"); }
}
public static class NumEqualVerifyException extends ScriptException {
public NumEqualVerifyException() { super("OP_NUMEQUALVERIFY failed, non-equal operands"); }
}
public static class OpCountException extends ScriptException {
public OpCountException() { super("script contains too many opcodes"); }
}
public static class OperandSizeException extends ScriptException {
public OperandSizeException() { super("invalid operand size"); }
}
public static class OpReturnException extends ScriptException {
public OpReturnException() { super("the script called OP_RETURN"); }
}
public static class OpVerifyFailed extends ScriptException {
public OpVerifyFailed() { super("the VERIFY failed"); }
}
public static class PubKeyCountException extends ScriptException {
public PubKeyCountException() { super("there are too many, or not enough, public keys"); }
}
public static class PushSizeException extends ScriptException {
/* some possible causes:
* - result of OP_CAT would be too large
* - result of NUM2BIN would be too large
*/
public PushSizeException() { super("attempted to push value on the stack that was too large"); }
}
public static class ScriptSizeException extends ScriptException {
public ScriptSizeException() { super("the script is too large"); }
}
public static class SigCountException extends ScriptException {
public SigCountException() { super("sig count out of range"); }
}
public static class SigPushOnlyException extends ScriptException {
public SigPushOnlyException() { super("attempted to spend a P2SH scriptPubKey with a script that contained script ops"); }
}
public static class SplitRangeException extends ScriptException {
public SplitRangeException() { super("invalid OP_SPLIT range"); }
}
public static class StackSizeException extends ScriptException {
public StackSizeException() { super("stack is, or would be, too large"); }
}
public static class UnbalancedConditionalException extends ScriptException {
public UnbalancedConditionalException() { super("the script contains an unbalanced conditional"); }
}
public static class MinimalDataException extends ScriptException {
public MinimalDataException() {super("PushData operation not compliant to Minimal data. A more specific opCode should be used.");}
}
public static class MinimalIfException extends ScriptException {
public MinimalIfException() {super("Top of the Stack does NOT meet the MINIMALIF requirements");}
}
public static class NullFailException extends ScriptException {
public NullFailException() {super("NULLFAIL-compliant");}
public ScriptException(String msg) {
super(msg);
}
public static class UnsatisfiedLocktime extends ScriptException {
public UnsatisfiedLocktime(String msg) {super(msg);}
public ScriptError getError() {
return err;
}
public static class NegativeLocktime extends ScriptException {
public NegativeLocktime() {super("Negative locktime");}
}
}
......@@ -93,6 +93,7 @@ public class VerificationException extends RuntimeException {
}
}
public static class UnexpectedCoinbaseInput extends VerificationException {
public UnexpectedCoinbaseInput() {
super("Coinbase input as input in non-coinbase transaction");
......
/*
* Copyright 2017 Nicola Atzei
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bitcoinj.script;
import java.util.HashMap;
import java.util.Map;
public enum ScriptError {
SCRIPT_ERR_OK("OK"),
SCRIPT_ERR_UNKNOWN_ERROR("UNKNOWN_ERROR"),
SCRIPT_ERR_EVAL_FALSE("EVAL_FALSE"),
SCRIPT_ERR_OP_RETURN("OP_RETURN"),
SCRIPT_ERR_STANDARD("STANDARD_SCRIPT"),
/* Max sizes */
SCRIPT_ERR_SCRIPT_SIZE("SCRIPT_SIZE"),
SCRIPT_ERR_PUSH_SIZE("PUSH_SIZE"),
SCRIPT_ERR_OP_COUNT("OP_COUNT"),
SCRIPT_ERR_STACK_SIZE("STACK_SIZE"),
SCRIPT_ERR_SIG_COUNT("SIG_COUNT"),
SCRIPT_ERR_PUBKEY_COUNT("PUBKEY_COUNT"),
SCRIPT_ER_OPERAND_SIZE("OPERAND_SIZE"),
/* Failed verify operations */
SCRIPT_ERR_VERIFY("VERIFY"),
SCRIPT_ERR_EQUALVERIFY("EQUALVERIFY"),
SCRIPT_ERR_CHECKMULTISIGVERIFY("CHECKMULTISIGVERIFY"),
SCRIPT_ERR_CHECKSIGVERIFY("CHECKSIGVERIFY"),
SCRIPT_ERR_NUMEQUALVERIFY("NUMEQUALVERIFY"),
/* Logical/Format/Canonical errors */
SCRIPT_ERR_BAD_OPCODE("BAD_OPCODE"),
SCRIPT_ERR_DISABLED_OPCODE("DISABLED_OPCODE"),
SCRIPT_ERR_INVALID_STACK_OPERATION("INVALID_STACK_OPERATION"),
SCRIPT_ERR_INVALID_ALTSTACK_OPERATION("INVALID_ALTSTACK_OPERATION"),
SCRIPT_ERR_UNBALANCED_CONDITIONAL("UNBALANCED_CONDITIONAL"),
/* CHECKLOCKTIMEVERIFY and CHECKSEQUENCEVERIFY */
SCRIPT_ERR_NEGATIVE_LOCKTIME("NEGATIVE_LOCKTIME"),
SCRIPT_ERR_UNSATISFIED_LOCKTIME("UNSATISFIED_LOCKTIME"),
/* Malleability */
SCRIPT_ERR_SIG_HASHTYPE("SIG_HASHTYPE"),
SCRIPT_ERR_SIG_DER("SIG_DER"),
SCRIPT_ERR_MINIMALDATA("MINIMALDATA"),
SCRIPT_ERR_SIG_PUSHONLY("SIG_PUSHONLY"),
SCRIPT_ERR_SIG_HIGH_S("SIG_HIGH_S"),
SCRIPT_ERR_SIG_NULLDUMMY("SIG_NULLDUMMY"),
SCRIPT_ERR_PUBKEYTYPE("PUBKEYTYPE"),
SCRIPT_ERR_CLEANSTACK("CLEANSTACK"),
SCRIPT_ERR_MINIMALIF("MINIMALIF"),
SCRIPT_ERR_SIG_NULLFAIL("NULLFAIL"),
/* softfork safeness */
SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS("DISCOURAGE_UPGRADABLE_NOPS"),
SCRIPT_ERR_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM("DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM"),
/* segregated witness */
SCRIPT_ERR_WITNESS_PROGRAM_WRONG_LENGTH("WITNESS_PROGRAM_WRONG_LENGTH"),
SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY("WITNESS_PROGRAM_WITNESS_EMPTY"),
SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH("WITNESS_PROGRAM_MISMATCH"),
SCRIPT_ERR_WITNESS_MALLEATED("WITNESS_MALLEATED"),
SCRIPT_ERR_WITNESS_MALLEATED_P2SH("WITNESS_MALLEATED_P2SH"),
SCRIPT_ERR_WITNESS_UNEXPECTED("WITNESS_UNEXPECTED"),
SCRIPT_ERR_WITNESS_PUBKEYTYPE("WITNESS_PUBKEYTYPE"),
SCRIPT_ERR_ERROR_COUNT("ERROR_COUNT"),
/* Other Errors */
SCRIPT_ERR_DIV_BY_ZERO("DIV_BY_ZERO"),
SCRIPT_ERR_MOD_BY_ZERO("MOD_BY_ZERO"),
SCRIPT_ERR_SPLIT_RANGE("SPLIT_RANGE"),
SCRIPT_ERR_IMPOSSIBLE_ENCODING("IMPOSSIBLE_ENCODING"),
SCRIPT_ERR_INVALID_NUMBER_RANGE("INVALID_NUMBER_RANGE"),
SCRIPT_ERR_FORKID("ILLEGAL_FORKID");
private final String mnemonic;
private static final Map<String, ScriptError> mnemonicToScriptErrorMap;
private ScriptError(String name) {
this.mnemonic = name;
}
static {
mnemonicToScriptErrorMap = new HashMap<>();
for (ScriptError err : ScriptError.values()) {
mnemonicToScriptErrorMap.put(err.getMnemonic(), err);
}
}
public String getMnemonic() {
return mnemonic;
}
public static ScriptError fromMnemonic(String name) {
ScriptError err = mnemonicToScriptErrorMap.get(name);
if (err == null)
throw new IllegalArgumentException(name + " is not a valid name");
return err;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment