7 #ifndef RAVEN_SCRIPT_SCRIPT_H 8 #define RAVEN_SCRIPT_SCRIPT_H 26 static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520;
29 static const int MAX_OPS_PER_SCRIPT = 201;
32 static const int MAX_PUBKEYS_PER_MULTISIG = 20;
35 static const int MAX_SCRIPT_SIZE = 10000;
38 static const int MAX_STACK_SIZE = 1000;
42 static const unsigned int LOCKTIME_THRESHOLD = 500000000;
47 return std::vector<unsigned char>(in.begin(), in.end());
202 static const unsigned int MAX_OPCODE =
OP_NOP10;
229 static const size_t nDefaultMaxNumSize = 4;
231 explicit CScriptNum(
const std::vector<unsigned char>& vch,
bool fRequireMinimal,
232 const size_t nMaxNumSize = nDefaultMaxNumSize)
234 if (vch.size() > nMaxNumSize) {
237 if (fRequireMinimal && vch.size() > 0) {
244 if ((vch.back() & 0x7f) == 0) {
250 if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
255 m_value = set_vch(vch);
258 inline bool operator==(
const int64_t& rhs)
const {
return m_value == rhs; }
259 inline bool operator!=(
const int64_t& rhs)
const {
return m_value != rhs; }
260 inline bool operator<=(
const int64_t& rhs)
const {
return m_value <= rhs; }
261 inline bool operator< (
const int64_t& rhs)
const {
return m_value < rhs; }
262 inline bool operator>=(
const int64_t& rhs)
const {
return m_value >= rhs; }
263 inline bool operator> (
const int64_t& rhs)
const {
return m_value > rhs; }
287 assert(m_value != std::numeric_limits<int64_t>::min());
299 assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
300 (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
307 assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
308 (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
321 if (m_value > std::numeric_limits<int>::max())
322 return std::numeric_limits<int>::max();
323 else if (m_value < std::numeric_limits<int>::min())
324 return std::numeric_limits<int>::min();
328 std::vector<unsigned char>
getvch()
const 330 return serialize(m_value);
333 static std::vector<unsigned char>
serialize(
const int64_t& value)
336 return std::vector<unsigned char>();
338 std::vector<unsigned char> result;
339 const bool neg = value < 0;
340 uint64_t absvalue = neg ? -value : value;
344 result.push_back(absvalue & 0xff);
358 if (result.back() & 0x80)
359 result.push_back(neg ? 0x80 : 0);
361 result.back() |= 0x80;
367 static int64_t
set_vch(
const std::vector<unsigned char>& vch)
373 for (
size_t i = 0; i != vch.size(); ++i)
374 result |= static_cast<int64_t>(vch[i]) << 8*i;
378 if (vch.back() & 0x80)
379 return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
401 if (n == -1 || (n >= 1 && n <= 16))
403 push_back(n + (
OP_1 - 1));
418 CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) :
CScriptBase(pbegin, pend) { }
423 template <
typename Stream,
typename Operation>
425 READWRITE(static_cast<CScriptBase&>(*
this));
430 reserve(size() + b.
size());
446 explicit CScript(
const std::vector<unsigned char>& b) { operator<<(b); }
453 if (opcode < 0 || opcode > 0xff)
454 throw std::runtime_error(
"CScript::operator<<(): invalid opcode");
455 insert(end(), (
unsigned char)opcode);
465 CScript& operator<<(const std::vector<unsigned char>& b)
469 insert(end(), (
unsigned char)b.size());
471 else if (b.size() <= 0xff)
474 insert(end(), (
unsigned char)b.size());
476 else if (b.size() <= 0xffff)
480 WriteLE16(_data, b.size());
481 insert(end(), _data, _data +
sizeof(_data));
487 WriteLE32(_data, b.size());
488 insert(end(), _data, _data +
sizeof(_data));
490 insert(end(), b.begin(), b.end());
498 assert(!
"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
507 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
508 pc = begin() + (pc2 - begin());
515 bool fRet = GetOp2(pc2, opcodeRet,
nullptr);
516 pc = begin() + (pc2 - begin());
522 return GetOp2(pc, opcodeRet, &vchRet);
527 return GetOp2(pc, opcodeRet,
nullptr);
541 unsigned int opcode = *pc++;
546 unsigned int nSize = 0;
561 nSize = ReadLE16(&pc[0]);
568 nSize = ReadLE32(&pc[0]);
571 if (end() - pc < 0 || (
unsigned int)(end() - pc) < nSize)
574 pvchRet->assign(pc, pc + nSize);
587 assert(opcode >=
OP_1 && opcode <=
OP_16);
588 return (
int)opcode - (int)(
OP_1 - 1);
592 assert(n >= 0 && n <= 16);
604 iterator pc = begin(), pc2 = begin();
609 while (static_cast<size_t>(end() - pc) >= b.
size() && std::equal(b.
begin(), b.
end(), pc))
616 while (GetOp(pc, opcode));
629 for (
const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
642 unsigned int GetSigOpCount(
bool fAccurate)
const;
648 unsigned int GetSigOpCount(
const CScript& scriptSig)
const;
650 bool IsPayToPublicKeyHash()
const;
652 bool IsPayToScriptHash()
const;
653 bool IsPayToWitnessScriptHash()
const;
654 bool IsWitnessProgram(
int& version, std::vector<unsigned char>& program)
const;
658 bool IsAssetScript(
int& nType,
bool& fIsOwner,
int& nStartingIndex)
const;
659 bool IsAssetScript(
int& nType,
bool& fIsOwner)
const;
660 bool IsAssetScript()
const;
661 bool IsNewAsset()
const;
662 bool IsOwnerAsset()
const;
663 bool IsReissueAsset()
const;
664 bool IsTransferAsset()
const;
665 bool IsAsset()
const;
666 bool IsNullAsset()
const;
667 bool IsNullAssetTxDataScript()
const;
668 bool IsNullAssetVerifierTxDataScript()
const;
669 bool IsNullGlobalRestrictionAssetTxDataScript()
const;
673 bool IsPayToPublicKey()
const;
676 bool IsPushOnly()
const;
679 bool HasValidOps()
const;
686 bool IsUnspendable()
const;
701 std::vector<std::vector<unsigned char> >
stack;
706 bool IsNull()
const {
return stack.empty(); }
708 void SetNull() { stack.clear(); stack.shrink_to_fit(); }
710 std::string ToString()
const;
733 #endif // RAVEN_SCRIPT_SCRIPT_H
static int64_t set_vch(const std::vector< unsigned char > &vch)
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
iterator insert(iterator pos, const T &value)
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
virtual ~CReserveScript()
CScriptNum(const int64_t &n)
Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
CScriptNum & operator-=(const int64_t &rhs)
CScript & push_int64(int64_t n)
std::vector< std::vector< unsigned char > > stack
bool ScriptTransferAsset(const CScript &scriptPubKey, int &nStartingIndex)
bool ScriptNewAsset(const CScript &scriptPubKey, int &nStartingIndex)
friend CScript operator+(const CScript &a, const CScript &b)
bool operator<=(const int64_t &rhs) const
CScriptNum & operator=(const int64_t &rhs)
CScript(const std::vector< unsigned char > &b)
static std::vector< unsigned char > serialize(const int64_t &value)
const char * GetOpName(opcodetype opcode)
CScriptNum & operator+=(const CScriptNum &rhs)
int64_t CAmount
Amount in corbies (Can be negative)
CScript & operator<<(const CScriptNum &b)
CScript & operator<<(opcodetype opcode)
opcodetype
Script opcodes.
CScript(const CScriptNum &b)
bool AmountFromTransferScript(const CScript &scriptPubKey, CAmount &nAmount)
bool operator==(const CScriptNum &rhs) const
CScript & operator<<(const CScript &b)
CScriptNum operator+(const int64_t &rhs) const
bool operator!=(const int64_t &rhs) const
CScriptNum operator-() const
prevector< 28, unsigned char > CScriptBase
We use a prevector for the script to reduce the considerable memory overhead of vectors in cases wher...
bool operator>=(const CScriptNum &rhs) const
CScriptNum operator+(const CScriptNum &rhs) const
CScript(const unsigned char *pbegin, const unsigned char *pend)
bool ScriptReissueAsset(const CScript &scriptPubKey, int &nStartingIndex)
std::vector< unsigned char > getvch() const
scriptnum_error(const std::string &str)
CScriptNum(const std::vector< unsigned char > &vch, bool fRequireMinimal, const size_t nMaxNumSize=nDefaultMaxNumSize)
bool GetOp(iterator &pc, opcodetype &opcodeRet)
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
bool operator<(const CNetAddr &a, const CNetAddr &b)
CScriptNum & operator-=(const CScriptNum &rhs)
CScriptNum operator-(const CScriptNum &rhs) const
CScriptNum operator-(const int64_t &rhs) const
CScript(const_iterator pbegin, const_iterator pend)
static opcodetype EncodeOP_N(int n)
Serialized script, used inside transaction inputs and outputs.
CScript & operator+=(const CScript &b)
bool AmountFromNewAssetScript(const CScript &scriptPubKey, CAmount &nAmount)
bool GetOp(const_iterator &pc, opcodetype &opcodeRet) const
int FindAndDelete(const CScript &b)
CScript & operator<<(int64_t b)
virtual void KeepScript()
CScriptNum & operator+=(const int64_t &rhs)
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
void SerializationOp(Stream &s, Operation ser_action)
int Find(opcodetype op) const
bool AmountFromReissueScript(const CScript &scriptPubKey, CAmount &nAmount)
CScript(std::vector< unsigned char >::const_iterator pbegin, std::vector< unsigned char >::const_iterator pend)
bool operator==(const int64_t &rhs) const
bool operator>=(const int64_t &rhs) const
bool GetAssetAmountFromScript(const CScript &script, CAmount &nAmount)
These are needed because script.h and script.cpp do not have access to asset.h and asset...
bool operator!=(const CScriptNum &rhs) const
std::vector< unsigned char > ToByteVector(const T &in)
bool GetOp2(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet) const
bool operator<=(const CScriptNum &rhs) const