Convert all StackErrX to ErrStackX.

This commit changes all stack errors from the form of StackErrX to
ErrStackX which is the expected form for standard Go code.
This commit is contained in:
Dave Collins 2014-10-12 16:40:09 -05:00
parent 37ad7042b6
commit c0c3d860d6
8 changed files with 565 additions and 565 deletions

2
doc.go
View file

@ -32,7 +32,7 @@ what conditions must be met in order to spend bitcoins.
Errors
Errors returned by this package are of the form btcscript.StackErrX where X
Errors returned by this package are of the form btcscript.ErrStackX where X
indicates the specific error. See Variables in the package documentation for a
full list.
*/

File diff suppressed because it is too large Load diff

View file

@ -939,23 +939,23 @@ func (pop *parsedOpcode) conditional() bool {
func (pop *parsedOpcode) exec(s *Script) error {
// Disabled opcodes are ``fail on program counter''.
if pop.disabled() {
return StackErrOpDisabled
return ErrStackOpDisabled
}
// Always-illegal opcodes are ``fail on program counter''.
if pop.alwaysIllegal() {
return StackErrReservedOpcode
return ErrStackReservedOpcode
}
// Note that this includes OP_RESERVED which counts as a push operation.
if pop.opcode.value > OP_16 {
s.numOps++
if s.numOps > MaxOpsPerScript {
return StackErrTooManyOperations
return ErrStackTooManyOperations
}
} else if len(pop.data) > MaxScriptElementSize {
return StackErrElementTooBig
return ErrStackElementTooBig
}
// If we are not a conditional opcode and we aren't executing, then
@ -1012,7 +1012,7 @@ func (pop *parsedOpcode) bytes() ([]byte, error) {
retbytes[0] = pop.opcode.value
if pop.opcode.length == 1 {
if len(pop.data) != 0 {
return nil, StackErrInvalidOpcode
return nil, ErrStackInvalidOpcode
}
return retbytes, nil
}
@ -1041,7 +1041,7 @@ func (pop *parsedOpcode) bytes() ([]byte, error) {
retbytes = append(retbytes, pop.data...)
if len(retbytes) != nbytes {
return nil, StackErrInvalidOpcode
return nil, ErrStackInvalidOpcode
}
return retbytes, nil
@ -1050,16 +1050,16 @@ func (pop *parsedOpcode) bytes() ([]byte, error) {
// opcode implementation functions from here
func opcodeDisabled(op *parsedOpcode, s *Script) error {
return StackErrOpDisabled
return ErrStackOpDisabled
}
func opcodeReserved(op *parsedOpcode, s *Script) error {
return StackErrReservedOpcode
return ErrStackReservedOpcode
}
// Recognised opcode, but for bitcoind internal use only.
func opcodeInvalid(op *parsedOpcode, s *Script) error {
return StackErrInvalidOpcode
return ErrStackInvalidOpcode
}
func opcodeFalse(op *parsedOpcode, s *Script) error {
@ -1141,7 +1141,7 @@ func opcodeNotIf(op *parsedOpcode, s *Script) error {
func opcodeElse(op *parsedOpcode, s *Script) error {
if len(s.condStack) < 2 {
// intial true cannot be toggled, only pushed conditionals
return StackErrNoIf
return ErrStackNoIf
}
switch s.condStack[0] {
@ -1160,7 +1160,7 @@ func opcodeElse(op *parsedOpcode, s *Script) error {
func opcodeEndif(op *parsedOpcode, s *Script) error {
if len(s.condStack) < 2 {
// intial true cannot be popped, only pushed conditionals
return StackErrNoIf
return ErrStackNoIf
}
stk := make([]int, len(s.condStack)-1, len(s.condStack)-1)
@ -1176,13 +1176,13 @@ func opcodeVerify(op *parsedOpcode, s *Script) error {
}
if verified != true {
return StackErrVerifyFailed
return ErrStackVerifyFailed
}
return nil
}
func opcodeReturn(op *parsedOpcode, s *Script) error {
return StackErrEarlyReturn
return ErrStackEarlyReturn
}
func opcodeToAltStack(op *parsedOpcode, s *Script) error {
@ -1839,16 +1839,16 @@ func opcodeCheckMultiSig(op *parsedOpcode, s *Script) error {
}
// XXX arbitrary limits
// nore more than 20 pubkeyhs, or 201 operations
// nore more than 20 pubkeys, or 201 operations
// PopInt promises that the int returned is 32 bit.
npk := int(numPubkeys.Int64())
if npk < 0 || npk > MaxPubKeysPerMultiSig {
return StackErrTooManyPubkeys
return ErrStackTooManyPubkeys
}
s.numOps += npk
if s.numOps > MaxOpsPerScript {
return StackErrTooManyOperations
return ErrStackTooManyOperations
}
pubKeyStrings := make([][]byte, npk)
pubKeys := make([]*btcec.PublicKey, npk)

File diff suppressed because it is too large Load diff

130
script.go
View file

@ -21,110 +21,110 @@ import (
)
var (
// StackErrShortScript is returned if the script has an opcode that is
// ErrStackShortScript is returned if the script has an opcode that is
// too long for the length of the script.
StackErrShortScript = errors.New("execute past end of script")
ErrStackShortScript = errors.New("execute past end of script")
// StackErrLongScript is returned if the script has an opcode that is
// ErrStackLongScript is returned if the script has an opcode that is
// too long for the length of the script.
StackErrLongScript = errors.New("script is longer than maximum allowed")
ErrStackLongScript = errors.New("script is longer than maximum allowed")
// StackErrUnderflow is returned if an opcode requires more items on the
// stack than is present.
StackErrUnderflow = errors.New("stack underflow")
// ErrStackUnderflow is returned if an opcode requires more items on the
// stack than is present.f
ErrStackUnderflow = errors.New("stack underflow")
// StackErrInvalidArgs is returned if the argument for an opcode is out
// ErrStackInvalidArgs is returned if the argument for an opcode is out
// of acceptable range.
StackErrInvalidArgs = errors.New("invalid argument")
ErrStackInvalidArgs = errors.New("invalid argument")
// StackErrOpDisabled is returned when a disabled opcode is encountered
// ErrStackOpDisabled is returned when a disabled opcode is encountered
// in the script.
StackErrOpDisabled = errors.New("Disabled Opcode")
ErrStackOpDisabled = errors.New("Disabled Opcode")
// StackErrVerifyFailed is returned when one of the OP_VERIFY or
// ErrStackVerifyFailed is returned when one of the OP_VERIFY or
// OP_*VERIFY instructions is executed and the conditions fails.
StackErrVerifyFailed = errors.New("Verify failed")
ErrStackVerifyFailed = errors.New("Verify failed")
// StackErrNumberTooBig is returned when the argument for an opcode that
// ErrStackNumberTooBig is returned when the argument for an opcode that
// should be an offset is obviously far too large.
StackErrNumberTooBig = errors.New("number too big")
ErrStackNumberTooBig = errors.New("number too big")
// StackErrInvalidOpcode is returned when an opcode marked as invalid or
// ErrStackInvalidOpcode is returned when an opcode marked as invalid or
// a completely undefined opcode is encountered.
StackErrInvalidOpcode = errors.New("Invalid Opcode")
ErrStackInvalidOpcode = errors.New("Invalid Opcode")
// StackErrReservedOpcode is returned when an opcode marked as reserved
// ErrStackReservedOpcode is returned when an opcode marked as reserved
// is encountered.
StackErrReservedOpcode = errors.New("Reserved Opcode")
ErrStackReservedOpcode = errors.New("Reserved Opcode")
// StackErrEarlyReturn is returned when OP_RETURN is executed in the
// ErrStackEarlyReturn is returned when OP_RETURN is executed in the
// script.
StackErrEarlyReturn = errors.New("Script returned early")
ErrStackEarlyReturn = errors.New("Script returned early")
// StackErrNoIf is returned if an OP_ELSE or OP_ENDIF is encountered
// ErrStackNoIf is returned if an OP_ELSE or OP_ENDIF is encountered
// without first having an OP_IF or OP_NOTIF in the script.
StackErrNoIf = errors.New("OP_ELSE or OP_ENDIF with no matching OP_IF")
ErrStackNoIf = errors.New("OP_ELSE or OP_ENDIF with no matching OP_IF")
// StackErrMissingEndif is returned if the end of a script is reached
// ErrStackMissingEndif is returned if the end of a script is reached
// without and OP_ENDIF to correspond to a conditional expression.
StackErrMissingEndif = fmt.Errorf("execute fail, in conditional execution")
ErrStackMissingEndif = fmt.Errorf("execute fail, in conditional execution")
// StackErrTooManyPubkeys is returned if an OP_CHECKMULTISIG is
// ErrStackTooManyPubkeys is returned if an OP_CHECKMULTISIG is
// encountered with more than MaxPubKeysPerMultiSig pubkeys present.
StackErrTooManyPubkeys = errors.New("Invalid pubkey count in OP_CHECKMULTISIG")
ErrStackTooManyPubkeys = errors.New("Invalid pubkey count in OP_CHECKMULTISIG")
// StackErrTooManyOperations is returned if a script has more than
// ErrStackTooManyOperations is returned if a script has more than
// MaxOpsPerScript opcodes that do not push data.
StackErrTooManyOperations = errors.New("Too many operations in script")
ErrStackTooManyOperations = errors.New("Too many operations in script")
// StackErrElementTooBig is returned if the size of an element to be
// ErrStackElementTooBig is returned if the size of an element to be
// pushed to the stack is over MaxScriptElementSize.
StackErrElementTooBig = errors.New("Element in script too large")
ErrStackElementTooBig = errors.New("Element in script too large")
// StackErrUnknownAddress is returned when ScriptToAddrHash does not
// ErrStackUnknownAddress is returned when ScriptToAddrHash does not
// recognise the pattern of the script and thus can not find the address
// for payment.
StackErrUnknownAddress = errors.New("non-recognised address")
ErrStackUnknownAddress = errors.New("non-recognised address")
// StackErrScriptFailed is returned when at the end of a script the
// ErrStackScriptFailed is returned when at the end of a script the
// boolean on top of the stack is false signifying that the script has
// failed.
StackErrScriptFailed = errors.New("execute fail, fail on stack")
ErrStackScriptFailed = errors.New("execute fail, fail on stack")
// StackErrScriptUnfinished is returned when CheckErrorCondition is
// ErrStackScriptUnfinished is returned when CheckErrorCondition is
// called on a script that has not finished executing.
StackErrScriptUnfinished = errors.New("Error check when script unfinished")
ErrStackScriptUnfinished = errors.New("Error check when script unfinished")
// StackErrEmpyStack is returned when the stack is empty at the end of
// ErrStackEmptyStack is returned when the stack is empty at the end of
// execution. Normal operation requires that a boolean is on top of the
// stack when the scripts have finished executing.
StackErrEmptyStack = errors.New("Stack empty at end of execution")
ErrStackEmptyStack = errors.New("Stack empty at end of execution")
// StackErrP2SHNonPushOnly is returned when a Pay-to-Script-Hash
// ErrStackP2SHNonPushOnly is returned when a Pay-to-Script-Hash
// transaction is encountered and the ScriptSig does operations other
// than push data (in violation of bip16).
StackErrP2SHNonPushOnly = errors.New("pay to script hash with non " +
ErrStackP2SHNonPushOnly = errors.New("pay to script hash with non " +
"pushonly input")
// StackErrInvalidParseType is an internal error returned from
// ErrStackInvalidParseType is an internal error returned from
// ScriptToAddrHash ony if the internal data tables are wrong.
StackErrInvalidParseType = errors.New("internal error: invalid parsetype found")
ErrStackInvalidParseType = errors.New("internal error: invalid parsetype found")
// StackErrInvalidAddrOffset is an internal error returned from
// ErrStackInvalidAddrOffset is an internal error returned from
// ScriptToAddrHash ony if the internal data tables are wrong.
StackErrInvalidAddrOffset = errors.New("internal error: invalid offset found")
ErrStackInvalidAddrOffset = errors.New("internal error: invalid offset found")
// StackErrInvalidIndex is returned when an out-of-bounds index was
// ErrStackInvalidIndex is returned when an out-of-bounds index was
// passed to a function.
StackErrInvalidIndex = errors.New("Invalid script index")
ErrStackInvalidIndex = errors.New("Invalid script index")
// StackErrNonPushOnly is returned when ScriptInfo is called with a
// ErrStackNonPushOnly is returned when ScriptInfo is called with a
// pkScript that peforms operations other that pushing data to the stack.
StackErrNonPushOnly = errors.New("SigScript is non pushonly")
ErrStackNonPushOnly = errors.New("SigScript is non pushonly")
// StackErrOverflow is returned when stack and altstack combined depth
// ErrStackOverflow is returned when stack and altstack combined depth
// is over the limit.
StackErrOverflow = errors.New("Stacks overflowed")
ErrStackOverflow = errors.New("Stacks overflowed")
)
const (
@ -421,7 +421,7 @@ func parseScriptTemplate(script []byte, opcodemap map[byte]*opcode) ([]parsedOpc
instr := script[i]
op, ok := opcodemap[instr]
if !ok {
return retScript, StackErrInvalidOpcode
return retScript, ErrStackInvalidOpcode
}
pop := parsedOpcode{opcode: op}
// parse data out of instruction.
@ -431,7 +431,7 @@ func parseScriptTemplate(script []byte, opcodemap map[byte]*opcode) ([]parsedOpc
i++
case op.length > 1:
if len(script[i:]) < op.length {
return retScript, StackErrShortScript
return retScript, ErrStackShortScript
}
// slice out the data.
pop.data = script[i+1 : i+op.length]
@ -441,7 +441,7 @@ func parseScriptTemplate(script []byte, opcodemap map[byte]*opcode) ([]parsedOpc
off := i + 1
if len(script[off:]) < -op.length {
return retScript, StackErrShortScript
return retScript, ErrStackShortScript
}
// Next -length bytes are little endian length of data.
@ -466,7 +466,7 @@ func parseScriptTemplate(script []byte, opcodemap map[byte]*opcode) ([]parsedOpc
// Disallow entries that do not fit script or were
// sign extended.
if int(l) > len(script[off:]) || int(l) < 0 {
return retScript, StackErrShortScript
return retScript, ErrStackShortScript
}
pop.data = script[off : off+int(l)]
i += 1 - op.length + int(l)
@ -528,7 +528,7 @@ func NewScript(scriptSig []byte, scriptPubKey []byte, txidx int, tx *btcwire.Msg
m.scripts = make([][]parsedOpcode, len(scripts))
for i, scr := range scripts {
if len(scr) > maxScriptSize {
return nil, StackErrLongScript
return nil, ErrStackLongScript
}
var err error
m.scripts[i], err = parseScript(scr)
@ -551,7 +551,7 @@ func NewScript(scriptSig []byte, scriptPubKey []byte, txidx int, tx *btcwire.Msg
// if we are pay to scripthash then we only accept input
// scripts that push data
if !isPushOnly(m.scripts[0]) {
return nil, StackErrP2SHNonPushOnly
return nil, ErrStackP2SHNonPushOnly
}
m.bip16 = true
}
@ -611,10 +611,10 @@ func (s *Script) CheckErrorCondition() (err error) {
// Check we are actually done. if pc is past the end of script array
// then we have run out of scripts to run.
if s.scriptidx < len(s.scripts) {
return StackErrScriptUnfinished
return ErrStackScriptUnfinished
}
if s.dstack.Depth() < 1 {
return StackErrEmptyStack
return ErrStackEmptyStack
}
v, err := s.dstack.PopBool()
if err == nil && v == false {
@ -625,7 +625,7 @@ func (s *Script) CheckErrorCondition() (err error) {
return fmt.Sprintf("scripts failed: script0: %s\n"+
"script1: %s", dis0, dis1)
}))
err = StackErrScriptFailed
err = ErrStackScriptFailed
}
return err
}
@ -649,7 +649,7 @@ func (m *Script) Step() (done bool, err error) {
}
if m.dstack.Depth()+m.astack.Depth() > maxStackSize {
return false, StackErrOverflow
return false, ErrStackOverflow
}
// prepare for next instruction
@ -657,7 +657,7 @@ func (m *Script) Step() (done bool, err error) {
if m.scriptoff >= len(m.scripts[m.scriptidx]) {
// Illegal to have an `if' that straddles two scripts.
if err == nil && len(m.condStack) != 1 {
return false, StackErrMissingEndif
return false, ErrStackMissingEndif
}
// alt stack doesn't persist.
@ -728,7 +728,7 @@ func (m *Script) validPC() error {
// ``idx''. Where 0 is the scriptSig and 1 is the scriptPubKey.
func (m *Script) DisasmScript(idx int) (disstr string, err error) {
if idx >= len(m.scripts) {
return "", StackErrInvalidIndex
return "", ErrStackInvalidIndex
}
for i := range m.scripts[idx] {
disstr = disstr + m.disasm(idx, i) + "\n"
@ -1561,7 +1561,7 @@ func CalcScriptInfo(sigscript, pkscript []byte, bip16 bool) (*ScriptInfo, error)
// Can't have a pkScript that doesn't just push data.
if !isPushOnly(sigPops) {
return nil, StackErrNonPushOnly
return nil, ErrStackNonPushOnly
}
si.ExpectedInputs = expectedInputs(pkPops, si.PkScriptClass)
@ -1622,7 +1622,7 @@ func CalcMultiSigStats(script []byte) (int, int, error) {
// items must be on the stack per:
// OP_1 PUBKEY OP_1 OP_CHECKMULTISIG
if len(pops) < 4 {
return 0, 0, StackErrUnderflow
return 0, 0, ErrStackUnderflow
}
numSigs := asSmallInt(pops[0].opcode)

View file

@ -326,7 +326,7 @@ var txTests = []txTest{
0x12, 0xa3, btcscript.OP_CHECKSIG,
},
idx: 0,
err: btcscript.StackErrScriptFailed,
err: btcscript.ErrStackScriptFailed,
nSigOps: 1,
scriptInfo: btcscript.ScriptInfo{
PkScriptClass: btcscript.PubKeyTy,
@ -1082,7 +1082,7 @@ var txTests = []txTest{
idx: 1,
bip16: false,
nSigOps: 0, // multisig is in the pkScript!
scriptInfoErr: btcscript.StackErrNonPushOnly,
scriptInfoErr: btcscript.ErrStackNonPushOnly,
},
// same as previous but with one byte changed to make signature fail
{
@ -1204,9 +1204,9 @@ var txTests = []txTest{
},
idx: 1,
bip16: false,
err: btcscript.StackErrScriptFailed,
err: btcscript.ErrStackScriptFailed,
nSigOps: 0, // multisig is in the pkScript!
scriptInfoErr: btcscript.StackErrNonPushOnly,
scriptInfoErr: btcscript.ErrStackNonPushOnly,
},
// taken from tx b2d93dfd0b2c1a380e55e76a8d9cb3075dec9f4474e9485be008c337fd62c1f7
// on testnet
@ -1391,7 +1391,7 @@ var txTests = []txTest{
btcscript.OP_EQUAL,
},
idx: 0,
err: btcscript.StackErrScriptFailed,
err: btcscript.ErrStackScriptFailed,
bip16: true,
nSigOps: 0, // no signature ops in the pushed script.
scriptInfo: btcscript.ScriptInfo{
@ -1456,9 +1456,9 @@ var txTests = []txTest{
btcscript.OP_EQUAL,
},
idx: 0,
err: btcscript.StackErrShortScript,
err: btcscript.ErrStackShortScript,
bip16: true,
scriptInfoErr: btcscript.StackErrShortScript,
scriptInfoErr: btcscript.ErrStackShortScript,
},
{
// sigscript changed so to be non pushonly.
@ -1519,10 +1519,10 @@ var txTests = []txTest{
btcscript.OP_EQUAL,
},
idx: 0,
parseErr: btcscript.StackErrP2SHNonPushOnly,
parseErr: btcscript.ErrStackP2SHNonPushOnly,
bip16: true,
nSigOps: 0, // no signature ops in the pushed script.
scriptInfoErr: btcscript.StackErrNonPushOnly,
scriptInfoErr: btcscript.ErrStackNonPushOnly,
},
{
// sigscript changed so to be non pushonly.
@ -1663,7 +1663,7 @@ func TestGetPreciseSignOps(t *testing.T) {
{
name: "scriptSig doesn't parse",
scriptSig: []byte{btcscript.OP_PUSHDATA1, 2},
err: btcscript.StackErrShortScript,
err: btcscript.ErrStackShortScript,
},
{
name: "scriptSig isn't push only",
@ -1686,7 +1686,7 @@ func TestGetPreciseSignOps(t *testing.T) {
name: "pushed script doesn't parse",
scriptSig: []byte{btcscript.OP_DATA_2,
btcscript.OP_PUSHDATA1, 2},
err: btcscript.StackErrShortScript,
err: btcscript.ErrStackShortScript,
},
}
// The signature in the p2sh script is nonsensical for the tests since
@ -1766,7 +1766,7 @@ func TestScriptInfo(t *testing.T) {
0xc4, 0xf5, 0x9c,
},
bip16: true,
scriptInfoErr: btcscript.StackErrShortScript,
scriptInfoErr: btcscript.ErrStackShortScript,
},
{
name: "sigScript doesn't parse",
@ -1786,7 +1786,7 @@ func TestScriptInfo(t *testing.T) {
0xc4, 0xf5, 0x9c, 0x74, btcscript.OP_EQUAL,
},
bip16: true,
scriptInfoErr: btcscript.StackErrShortScript,
scriptInfoErr: btcscript.ErrStackShortScript,
},
{
// Invented scripts, the hashes do not match
@ -1945,13 +1945,13 @@ var removeOpcodeTests = []removeOpcodeTest{
name: "invalid length (insruction)",
before: []byte{btcscript.OP_PUSHDATA1},
remove: btcscript.OP_CODESEPARATOR,
err: btcscript.StackErrShortScript,
err: btcscript.ErrStackShortScript,
},
{
name: "invalid length (data)",
before: []byte{btcscript.OP_PUSHDATA1, 255, 254},
remove: btcscript.OP_CODESEPARATOR,
err: btcscript.StackErrShortScript,
err: btcscript.ErrStackShortScript,
},
}
@ -2083,13 +2083,13 @@ var removeOpcodeByDataTests = []removeOpcodeByDataTest{
name: "invalid length (instruction)",
before: []byte{btcscript.OP_PUSHDATA1},
remove: []byte{1, 2, 3, 4},
err: btcscript.StackErrShortScript,
err: btcscript.ErrStackShortScript,
},
{
name: "invalid length (data)",
before: []byte{btcscript.OP_PUSHDATA1, 255, 254},
remove: []byte{1, 2, 3, 4},
err: btcscript.StackErrShortScript,
err: btcscript.ErrStackShortScript,
},
}
@ -2493,7 +2493,7 @@ func TestCheckErrorCondition(t *testing.T) {
}
err = engine.CheckErrorCondition()
if err != btcscript.StackErrScriptUnfinished {
if err != btcscript.ErrStackScriptUnfinished {
t.Errorf("got unexepected error %v on %dth iteration",
err, i)
return
@ -4638,7 +4638,7 @@ func TestCalcMultiSigStats(t *testing.T) {
0x71, 0x05, 0xcd, 0x6a, 0x82, 0x8e, 0x03, 0x90,
0x9a, 0x67, 0x96, 0x2e, 0x0e, 0xa1, 0xf6, 0x1d,
},
expected: btcscript.StackErrShortScript,
expected: btcscript.ErrStackShortScript,
},
{
name: "stack underflow",
@ -4653,7 +4653,7 @@ func TestCalcMultiSigStats(t *testing.T) {
0xeb, 0x64, 0x9f, 0x6b, 0xc3, 0xf4, 0xce, 0xf3,
0x08,
},
expected: btcscript.StackErrUnderflow,
expected: btcscript.ErrStackUnderflow,
},
{
name: "multisig script",

View file

@ -14,7 +14,7 @@ import (
func asInt(v []byte) (*big.Int, error) {
// Only 32bit numbers allowed.
if len(v) > 4 {
return nil, StackErrNumberTooBig
return nil, ErrStackNumberTooBig
}
if len(v) == 0 {
return big.NewInt(0), nil
@ -149,7 +149,7 @@ func (s *Stack) PopBool() (bool, error) {
func (s *Stack) PeekByteArray(idx int) (so []byte, err error) {
sz := len(s.stk)
if idx < 0 || idx >= sz {
return nil, StackErrUnderflow
return nil, ErrStackUnderflow
}
return s.stk[sz-idx-1], nil
}
@ -177,7 +177,7 @@ func (s *Stack) PeekBool(idx int) (i bool, err error) {
func (s *Stack) nipN(idx int) (so []byte, err error) {
sz := len(s.stk)
if idx < 0 || idx > sz-1 {
err = StackErrUnderflow
err = ErrStackUnderflow
return
}
so = s.stk[sz-idx-1]
@ -231,7 +231,7 @@ func (s *Stack) Depth() (sz int) {
// DropN(2): 1,2,3 -> 1
func (s *Stack) DropN(n int) error {
if n < 1 {
return StackErrInvalidArgs
return ErrStackInvalidArgs
}
for ; n > 0; n-- {
_, err := s.PopByteArray()
@ -248,7 +248,7 @@ func (s *Stack) DropN(n int) error {
// DupN(2): 1,2,3 -> 1,2,3,2,3
func (s *Stack) DupN(n int) error {
if n < 1 {
return StackErrInvalidArgs
return ErrStackInvalidArgs
}
// Iteratively duplicate the value n-1 down the stack n times.
// this leaves us with an in-order duplicate of the top N items on the
@ -268,7 +268,7 @@ func (s *Stack) DupN(n int) error {
// RotN(1): 1,2,3 -> 2,3,1
func (s *Stack) RotN(n int) error {
if n < 1 {
return StackErrInvalidArgs
return ErrStackInvalidArgs
}
entry := 3*n - 1
// Nip the 3n-1th item from the stack to the top n times to rotate
@ -290,7 +290,7 @@ func (s *Stack) RotN(n int) error {
// SwapN(2): 1,2,3,4 -> 3,4,1,2
func (s *Stack) SwapN(n int) error {
if n < 1 {
return StackErrInvalidArgs
return ErrStackInvalidArgs
}
entry := 2*n - 1
for i := n; i > 0; i-- {
@ -311,7 +311,7 @@ func (s *Stack) SwapN(n int) error {
// OverN(2): 1,2,3,4 -> 1,2,3,4,1,2
func (s *Stack) OverN(n int) error {
if n < 1 {
return StackErrInvalidArgs
return ErrStackInvalidArgs
}
// Copy 2n-1th entry to top of the stack
entry := 2*n - 1

View file

@ -39,7 +39,7 @@ var stackTests = []stackTest{
_, err := stack.PeekByteArray(5)
return err
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -49,7 +49,7 @@ var stackTests = []stackTest{
_, err := stack.PeekInt(5)
return err
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -59,7 +59,7 @@ var stackTests = []stackTest{
_, err := stack.PeekBool(5)
return err
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -121,7 +121,7 @@ var stackTests = []stackTest{
}
return nil
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -169,7 +169,7 @@ var stackTests = []stackTest{
return nil
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -411,7 +411,7 @@ var stackTests = []stackTest{
return nil
},
btcscript.StackErrInvalidArgs,
btcscript.ErrStackInvalidArgs,
[][]byte{},
},
{
@ -425,7 +425,7 @@ var stackTests = []stackTest{
return nil
},
btcscript.StackErrInvalidArgs,
btcscript.ErrStackInvalidArgs,
[][]byte{},
},
{
@ -439,7 +439,7 @@ var stackTests = []stackTest{
return nil
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -453,7 +453,7 @@ var stackTests = []stackTest{
return nil
},
btcscript.StackErrInvalidArgs,
btcscript.ErrStackInvalidArgs,
[][]byte{},
},
{
@ -602,7 +602,7 @@ var stackTests = []stackTest{
// bite off more than we can chew
return stack.NipN(3)
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{{2}, {3}},
},
{
@ -612,7 +612,7 @@ var stackTests = []stackTest{
// bite off more than we can chew
return stack.NipN(3)
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{{2}, {3}},
},
{
@ -630,7 +630,7 @@ var stackTests = []stackTest{
func(stack *btcscript.Stack) error {
return stack.Tuck()
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -639,7 +639,7 @@ var stackTests = []stackTest{
func(stack *btcscript.Stack) error {
return stack.Tuck()
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -684,7 +684,7 @@ var stackTests = []stackTest{
func(stack *btcscript.Stack) error {
return stack.DropN(5)
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -693,7 +693,7 @@ var stackTests = []stackTest{
func(stack *btcscript.Stack) error {
return stack.DropN(0)
},
btcscript.StackErrInvalidArgs,
btcscript.ErrStackInvalidArgs,
[][]byte{},
},
{
@ -720,7 +720,7 @@ var stackTests = []stackTest{
func(stack *btcscript.Stack) error {
return stack.RotN(1)
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -729,7 +729,7 @@ var stackTests = []stackTest{
func(stack *btcscript.Stack) error {
return stack.RotN(0)
},
btcscript.StackErrInvalidArgs,
btcscript.ErrStackInvalidArgs,
[][]byte{},
},
{
@ -756,7 +756,7 @@ var stackTests = []stackTest{
func(stack *btcscript.Stack) error {
return stack.SwapN(1)
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -765,7 +765,7 @@ var stackTests = []stackTest{
func(stack *btcscript.Stack) error {
return stack.SwapN(0)
},
btcscript.StackErrInvalidArgs,
btcscript.ErrStackInvalidArgs,
[][]byte{},
},
{
@ -792,7 +792,7 @@ var stackTests = []stackTest{
func(stack *btcscript.Stack) error {
return stack.OverN(1)
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -801,7 +801,7 @@ var stackTests = []stackTest{
func(stack *btcscript.Stack) error {
return stack.OverN(0)
},
btcscript.StackErrInvalidArgs,
btcscript.ErrStackInvalidArgs,
[][]byte{},
},
{
@ -828,7 +828,7 @@ var stackTests = []stackTest{
func(stack *btcscript.Stack) error {
return stack.PickN(1)
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -855,7 +855,7 @@ var stackTests = []stackTest{
func(stack *btcscript.Stack) error {
return stack.RollN(1)
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
{
@ -958,7 +958,7 @@ var stackTests = []stackTest{
_, err := stack.PopInt()
return err
},
btcscript.StackErrUnderflow,
btcscript.ErrStackUnderflow,
[][]byte{},
},
}