script: switch to CScriptNum usage for scripts
This commit is contained in:
parent
48d8eb1847
commit
27bff74e39
2 changed files with 28 additions and 67 deletions
|
@ -25,22 +25,13 @@ typedef vector<unsigned char> valtype;
|
||||||
static const valtype vchFalse(0);
|
static const valtype vchFalse(0);
|
||||||
static const valtype vchZero(0);
|
static const valtype vchZero(0);
|
||||||
static const valtype vchTrue(1, 1);
|
static const valtype vchTrue(1, 1);
|
||||||
static const CBigNum bnZero(0);
|
static const CScriptNum bnZero(0);
|
||||||
static const CBigNum bnOne(1);
|
static const CScriptNum bnOne(1);
|
||||||
static const CBigNum bnFalse(0);
|
static const CScriptNum bnFalse(0);
|
||||||
static const CBigNum bnTrue(1);
|
static const CScriptNum bnTrue(1);
|
||||||
static const size_t nMaxNumSize = 4;
|
|
||||||
|
|
||||||
bool CheckSig(vector<unsigned char> vchSig, const vector<unsigned char> &vchPubKey, const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, int flags);
|
bool CheckSig(vector<unsigned char> vchSig, const vector<unsigned char> &vchPubKey, const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, int flags);
|
||||||
|
|
||||||
CBigNum CastToBigNum(const valtype& vch)
|
|
||||||
{
|
|
||||||
if (vch.size() > nMaxNumSize)
|
|
||||||
throw runtime_error("CastToBigNum() : overflow");
|
|
||||||
// Get rid of extra leading zeros
|
|
||||||
return CBigNum(CBigNum(vch).getvch());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CastToBool(const valtype& vch)
|
bool CastToBool(const valtype& vch)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < vch.size(); i++)
|
for (unsigned int i = 0; i < vch.size(); i++)
|
||||||
|
@ -306,7 +297,6 @@ bool IsCanonicalSignature(const valtype &vchSig, unsigned int flags) {
|
||||||
|
|
||||||
bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType)
|
bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType)
|
||||||
{
|
{
|
||||||
CAutoBN_CTX pctx;
|
|
||||||
CScript::const_iterator pc = script.begin();
|
CScript::const_iterator pc = script.begin();
|
||||||
CScript::const_iterator pend = script.end();
|
CScript::const_iterator pend = script.end();
|
||||||
CScript::const_iterator pbegincodehash = script.begin();
|
CScript::const_iterator pbegincodehash = script.begin();
|
||||||
|
@ -380,7 +370,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
|
||||||
case OP_16:
|
case OP_16:
|
||||||
{
|
{
|
||||||
// ( -- value)
|
// ( -- value)
|
||||||
CBigNum bn((int)opcode - (int)(OP_1 - 1));
|
CScriptNum bn((int)opcode - (int)(OP_1 - 1));
|
||||||
stack.push_back(bn.getvch());
|
stack.push_back(bn.getvch());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -556,7 +546,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
|
||||||
case OP_DEPTH:
|
case OP_DEPTH:
|
||||||
{
|
{
|
||||||
// -- stacksize
|
// -- stacksize
|
||||||
CBigNum bn(stack.size());
|
CScriptNum bn(stack.size());
|
||||||
stack.push_back(bn.getvch());
|
stack.push_back(bn.getvch());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -606,7 +596,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
|
||||||
// (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)
|
// (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)
|
||||||
if (stack.size() < 2)
|
if (stack.size() < 2)
|
||||||
return false;
|
return false;
|
||||||
int n = CastToBigNum(stacktop(-1)).getint();
|
int n = CScriptNum(stacktop(-1)).getint();
|
||||||
popstack(stack);
|
popstack(stack);
|
||||||
if (n < 0 || n >= (int)stack.size())
|
if (n < 0 || n >= (int)stack.size())
|
||||||
return false;
|
return false;
|
||||||
|
@ -654,7 +644,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
|
||||||
// (in -- in size)
|
// (in -- in size)
|
||||||
if (stack.size() < 1)
|
if (stack.size() < 1)
|
||||||
return false;
|
return false;
|
||||||
CBigNum bn(stacktop(-1).size());
|
CScriptNum bn(stacktop(-1).size());
|
||||||
stack.push_back(bn.getvch());
|
stack.push_back(bn.getvch());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -705,7 +695,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
|
||||||
// (in -- out)
|
// (in -- out)
|
||||||
if (stack.size() < 1)
|
if (stack.size() < 1)
|
||||||
return false;
|
return false;
|
||||||
CBigNum bn = CastToBigNum(stacktop(-1));
|
CScriptNum bn(stacktop(-1));
|
||||||
switch (opcode)
|
switch (opcode)
|
||||||
{
|
{
|
||||||
case OP_1ADD: bn += bnOne; break;
|
case OP_1ADD: bn += bnOne; break;
|
||||||
|
@ -738,9 +728,9 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
|
||||||
// (x1 x2 -- out)
|
// (x1 x2 -- out)
|
||||||
if (stack.size() < 2)
|
if (stack.size() < 2)
|
||||||
return false;
|
return false;
|
||||||
CBigNum bn1 = CastToBigNum(stacktop(-2));
|
CScriptNum bn1(stacktop(-2));
|
||||||
CBigNum bn2 = CastToBigNum(stacktop(-1));
|
CScriptNum bn2(stacktop(-1));
|
||||||
CBigNum bn;
|
CScriptNum bn(0);
|
||||||
switch (opcode)
|
switch (opcode)
|
||||||
{
|
{
|
||||||
case OP_ADD:
|
case OP_ADD:
|
||||||
|
@ -783,9 +773,9 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
|
||||||
// (x min max -- out)
|
// (x min max -- out)
|
||||||
if (stack.size() < 3)
|
if (stack.size() < 3)
|
||||||
return false;
|
return false;
|
||||||
CBigNum bn1 = CastToBigNum(stacktop(-3));
|
CScriptNum bn1(stacktop(-3));
|
||||||
CBigNum bn2 = CastToBigNum(stacktop(-2));
|
CScriptNum bn2(stacktop(-2));
|
||||||
CBigNum bn3 = CastToBigNum(stacktop(-1));
|
CScriptNum bn3(stacktop(-1));
|
||||||
bool fValue = (bn2 <= bn1 && bn1 < bn3);
|
bool fValue = (bn2 <= bn1 && bn1 < bn3);
|
||||||
popstack(stack);
|
popstack(stack);
|
||||||
popstack(stack);
|
popstack(stack);
|
||||||
|
@ -882,7 +872,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
|
||||||
if ((int)stack.size() < i)
|
if ((int)stack.size() < i)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
int nKeysCount = CastToBigNum(stacktop(-i)).getint();
|
int nKeysCount = CScriptNum(stacktop(-i)).getint();
|
||||||
if (nKeysCount < 0 || nKeysCount > 20)
|
if (nKeysCount < 0 || nKeysCount > 20)
|
||||||
return false;
|
return false;
|
||||||
nOpCount += nKeysCount;
|
nOpCount += nKeysCount;
|
||||||
|
@ -893,7 +883,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
|
||||||
if ((int)stack.size() < i)
|
if ((int)stack.size() < i)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
int nSigsCount = CastToBigNum(stacktop(-i)).getint();
|
int nSigsCount = CScriptNum(stacktop(-i)).getint();
|
||||||
if (nSigsCount < 0 || nSigsCount > nKeysCount)
|
if (nSigsCount < 0 || nSigsCount > nKeysCount)
|
||||||
return false;
|
return false;
|
||||||
int isig = ++i;
|
int isig = ++i;
|
||||||
|
|
51
src/script.h
51
src/script.h
|
@ -374,7 +374,7 @@ const char* GetOpName(opcodetype opcode);
|
||||||
inline std::string ValueString(const std::vector<unsigned char>& vch)
|
inline std::string ValueString(const std::vector<unsigned char>& vch)
|
||||||
{
|
{
|
||||||
if (vch.size() <= 4)
|
if (vch.size() <= 4)
|
||||||
return strprintf("%d", CBigNum(vch).getint());
|
return strprintf("%d", CScriptNum(vch).getint());
|
||||||
else
|
else
|
||||||
return HexStr(vch);
|
return HexStr(vch);
|
||||||
}
|
}
|
||||||
|
@ -410,26 +410,10 @@ protected:
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
CBigNum bn(n);
|
*this << CScriptNum::serialize(n);
|
||||||
*this << bn.getvch();
|
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
CScript& push_uint64(uint64_t n)
|
|
||||||
{
|
|
||||||
if (n >= 1 && n <= 16)
|
|
||||||
{
|
|
||||||
push_back(n + (OP_1 - 1));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CBigNum bn(n);
|
|
||||||
*this << bn.getvch();
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CScript() { }
|
CScript() { }
|
||||||
CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
|
CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
|
||||||
|
@ -452,35 +436,16 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//explicit CScript(char b) is not portable. Use 'signed char' or 'unsigned char'.
|
CScript(int64_t b) { operator<<(b); }
|
||||||
explicit CScript(signed char b) { operator<<(b); }
|
|
||||||
explicit CScript(short b) { operator<<(b); }
|
|
||||||
explicit CScript(int b) { operator<<(b); }
|
|
||||||
explicit CScript(long b) { operator<<(b); }
|
|
||||||
explicit CScript(long long b) { operator<<(b); }
|
|
||||||
explicit CScript(unsigned char b) { operator<<(b); }
|
|
||||||
explicit CScript(unsigned int b) { operator<<(b); }
|
|
||||||
explicit CScript(unsigned short b) { operator<<(b); }
|
|
||||||
explicit CScript(unsigned long b) { operator<<(b); }
|
|
||||||
explicit CScript(unsigned long long b) { operator<<(b); }
|
|
||||||
|
|
||||||
explicit CScript(opcodetype b) { operator<<(b); }
|
explicit CScript(opcodetype b) { operator<<(b); }
|
||||||
explicit CScript(const uint256& b) { operator<<(b); }
|
explicit CScript(const uint256& b) { operator<<(b); }
|
||||||
|
explicit CScript(const CScriptNum& b) { operator<<(b); }
|
||||||
explicit CScript(const CBigNum& b) { operator<<(b); }
|
explicit CScript(const CBigNum& b) { operator<<(b); }
|
||||||
explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
|
explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
|
||||||
|
|
||||||
|
|
||||||
//CScript& operator<<(char b) is not portable. Use 'signed char' or 'unsigned char'.
|
CScript& operator<<(int64_t b) { return push_int64(b); }
|
||||||
CScript& operator<<(signed char b) { return push_int64(b); }
|
|
||||||
CScript& operator<<(short b) { return push_int64(b); }
|
|
||||||
CScript& operator<<(int b) { return push_int64(b); }
|
|
||||||
CScript& operator<<(long b) { return push_int64(b); }
|
|
||||||
CScript& operator<<(long long b) { return push_int64(b); }
|
|
||||||
CScript& operator<<(unsigned char b) { return push_uint64(b); }
|
|
||||||
CScript& operator<<(unsigned int b) { return push_uint64(b); }
|
|
||||||
CScript& operator<<(unsigned short b) { return push_uint64(b); }
|
|
||||||
CScript& operator<<(unsigned long b) { return push_uint64(b); }
|
|
||||||
CScript& operator<<(unsigned long long b) { return push_uint64(b); }
|
|
||||||
|
|
||||||
CScript& operator<<(opcodetype opcode)
|
CScript& operator<<(opcodetype opcode)
|
||||||
{
|
{
|
||||||
|
@ -518,6 +483,12 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CScript& operator<<(const CScriptNum& b)
|
||||||
|
{
|
||||||
|
*this << b.getvch();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
CScript& operator<<(const std::vector<unsigned char>& b)
|
CScript& operator<<(const std::vector<unsigned char>& b)
|
||||||
{
|
{
|
||||||
if (b.size() < OP_PUSHDATA1)
|
if (b.size() < OP_PUSHDATA1)
|
||||||
|
|
Loading…
Reference in a new issue