Skip to content

Commit

Permalink
script: Remove magic numbers
Browse files Browse the repository at this point in the history
This adds two new constants, MAX_OPS_PER_SCRIPT and
MAX_PUBKEYS_PER_MULTISIG.
  • Loading branch information
dajohi committed Oct 15, 2015
1 parent d78a880 commit b48da5c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/script/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, un
return set_error(serror, SCRIPT_ERR_PUSH_SIZE);

// Note how OP_RESERVED does not count towards the opcode limit.
if (opcode > OP_16 && ++nOpCount > 201)
if (opcode > OP_16 && ++nOpCount > MAX_OPS_PER_SCRIPT)
return set_error(serror, SCRIPT_ERR_OP_COUNT);

if (opcode == OP_CAT ||
Expand Down Expand Up @@ -869,10 +869,10 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, un
return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);

int nKeysCount = CScriptNum(stacktop(-i), fRequireMinimal).getint();
if (nKeysCount < 0 || nKeysCount > 20)
if (nKeysCount < 0 || nKeysCount > MAX_PUBKEYS_PER_MULTISIG)
return set_error(serror, SCRIPT_ERR_PUBKEY_COUNT);
nOpCount += nKeysCount;
if (nOpCount > 201)
if (nOpCount > MAX_OPS_PER_SCRIPT)
return set_error(serror, SCRIPT_ERR_OP_COUNT);
int ikey = ++i;
i += nKeysCount;
Expand Down
2 changes: 1 addition & 1 deletion src/script/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ unsigned int CScript::GetSigOpCount(bool fAccurate) const
if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
n += DecodeOP_N(lastOpcode);
else
n += 20;
n += MAX_PUBKEYS_PER_MULTISIG;
}
lastOpcode = opcode;
}
Expand Down
9 changes: 8 additions & 1 deletion src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
#include <string>
#include <vector>

static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520; // bytes
// Maximum number of bytes pushable to the stack
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520;

// Maximum number of non-push operations per script
static const int MAX_OPS_PER_SCRIPT = 201;

// Maximum number of public keys per multisig
static const int MAX_PUBKEYS_PER_MULTISIG = 20;

// Threshold for nLockTime: below this value it is interpreted as block number,
// otherwise as UNIX timestamp.
Expand Down

0 comments on commit b48da5c

Please sign in to comment.