txscript: Add exported opcode name to value map.

This commit exports a new map named OpcodeByName which can be used to
lookup an opcode value given a human-readable opcode name.

It also modifies the test function which does short form parsing to use
the new map instead of the internal array.

Closes #267.
This commit is contained in:
Dave Collins 2015-04-21 01:40:59 -05:00
parent d251208f1f
commit d66593bbfd
2 changed files with 22 additions and 8 deletions

View file

@ -1819,3 +1819,18 @@ func opcodeCheckMultiSigVerify(op *parsedOpcode, vm *Engine) error {
} }
return err return err
} }
// OpcodeByName is a map that can be used to lookup an opcode by its
// human-readable name (OP_CHECKMULTISIG, OP_CHECKSIG, etc).
var OpcodeByName = make(map[string]byte)
func init() {
// Initialize the opcode name to value map using the contents of the
// opcode array. Also add entries for "OP_FALSE" and "OP_TRUE" since
// they are aliases for "OP_0" and "OP_1", respectively.
for _, op := range opcodeArray {
OpcodeByName[op.name] = op.value
}
OpcodeByName["OP_FALSE"] = OP_FALSE
OpcodeByName["OP_TRUE"] = OP_TRUE
}

View file

@ -46,7 +46,7 @@ func parseHex(tok string) ([]byte, error) {
// parseShortForm parses a string as as used in the Bitcoin Core reference tests // parseShortForm parses a string as as used in the Bitcoin Core reference tests
// into the script it came from. // into the script it came from.
func parseShortForm(script string) ([]byte, error) { func parseShortForm(script string) ([]byte, error) {
ops := make(map[string]*opcode) ops := make(map[string]byte)
// the format used for these tests is pretty simple if ad-hoc: // the format used for these tests is pretty simple if ad-hoc:
// - opcodes other than the push opcodes and unknown are present as // - opcodes other than the push opcodes and unknown are present as
@ -56,16 +56,15 @@ func parseShortForm(script string) ([]byte, error) {
// 0x14 is OP_DATA_20) // 0x14 is OP_DATA_20)
// - single quoted strings are pushed as data. // - single quoted strings are pushed as data.
// - anything else is an error. // - anything else is an error.
for i := range opcodeArray { for opcodeName, opcodeValue := range OpcodeByName {
op := &opcodeArray[i] if opcodeValue < OP_NOP && opcodeValue != OP_RESERVED {
if op.value < OP_NOP && op.value != OP_RESERVED {
continue continue
} }
if strings.Contains(op.name, "OP_UNKNOWN") { if strings.Contains(opcodeName, "OP_UNKNOWN") {
continue continue
} }
ops[op.name] = op ops[opcodeName] = opcodeValue
ops[strings.TrimPrefix(op.name, "OP_")] = op ops[strings.TrimPrefix(opcodeName, "OP_")] = opcodeValue
} }
// do once, build map. // do once, build map.
@ -90,7 +89,7 @@ func parseShortForm(script string) ([]byte, error) {
tok[0] == '\'' && tok[len(tok)-1] == '\'' { tok[0] == '\'' && tok[len(tok)-1] == '\'' {
builder.AddFullData([]byte(tok[1 : len(tok)-1])) builder.AddFullData([]byte(tok[1 : len(tok)-1]))
} else if opcode, ok := ops[tok]; ok { } else if opcode, ok := ops[tok]; ok {
builder.AddOp(opcode.value) builder.AddOp(opcode)
} else { } else {
return nil, fmt.Errorf("bad token \"%s\"", tok) return nil, fmt.Errorf("bad token \"%s\"", tok)
} }