6 #ifndef BITCOIN_SCRIPT_SCRIPT_H 7 #define BITCOIN_SCRIPT_SCRIPT_H 61 return std::vector<unsigned char>(in.begin(), in.end());
235 static const size_t nDefaultMaxNumSize = 4;
237 explicit CScriptNum(
const std::vector<unsigned char>& vch,
bool fRequireMinimal,
238 const size_t nMaxNumSize = nDefaultMaxNumSize)
240 if (vch.size() > nMaxNumSize) {
243 if (fRequireMinimal && vch.size() > 0) {
250 if ((vch.back() & 0x7f) == 0) {
256 if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
261 m_value = set_vch(vch);
264 inline bool operator==(
const int64_t& rhs)
const {
return m_value == rhs; }
265 inline bool operator!=(
const int64_t& rhs)
const {
return m_value != rhs; }
266 inline bool operator<=(
const int64_t& rhs)
const {
return m_value <= rhs; }
267 inline bool operator< (
const int64_t& rhs)
const {
return m_value < rhs; }
268 inline bool operator>=(
const int64_t& rhs)
const {
return m_value >= rhs; }
269 inline bool operator> (
const int64_t& rhs)
const {
return m_value > rhs; }
293 assert(m_value != std::numeric_limits<int64_t>::min());
305 assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
306 (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
313 assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
314 (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
327 if (m_value > std::numeric_limits<int>::max())
328 return std::numeric_limits<int>::max();
329 else if (m_value < std::numeric_limits<int>::min())
330 return std::numeric_limits<int>::min();
334 std::vector<unsigned char>
getvch()
const 336 return serialize(m_value);
339 static std::vector<unsigned char>
serialize(
const int64_t& value)
342 return std::vector<unsigned char>();
344 std::vector<unsigned char> result;
345 const bool neg = value < 0;
346 uint64_t absvalue = neg ? ~static_cast<uint64_t>(value) + 1 : static_cast<uint64_t>(value);
350 result.push_back(absvalue & 0xff);
364 if (result.back() & 0x80)
365 result.push_back(neg ? 0x80 : 0);
367 result.back() |= 0x80;
373 static int64_t
set_vch(
const std::vector<unsigned char>& vch)
379 for (
size_t i = 0; i != vch.size(); ++i)
380 result |= static_cast<int64_t>(vch[i]) << 8*i;
384 if (vch.back() & 0x80)
385 return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
409 if (n == -1 || (n >= 1 && n <= 16))
411 push_back(n + (
OP_1 - 1));
426 CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) :
CScriptBase(pbegin, pend) { }
436 explicit CScript(
const std::vector<unsigned char>& b) =
delete;
445 if (opcode < 0 || opcode > 0xff)
446 throw std::runtime_error(
"CScript::operator<<(): invalid opcode");
447 insert(end(), (
unsigned char)opcode);
457 CScript& operator<<(const std::vector<unsigned char>& b)
463 else if (b.
size() <= 0xff)
468 else if (b.
size() <= 0xffff)
473 insert(end(), _data, _data +
sizeof(_data));
480 insert(end(), _data, _data +
sizeof(_data));
501 assert(opcode >=
OP_1 && opcode <=
OP_16);
502 return (
int)opcode - (int)(
OP_1 - 1);
506 assert(n >= 0 && n <= 16);
519 unsigned int GetSigOpCount(
bool fAccurate)
const;
525 unsigned int GetSigOpCount(
const CScript& scriptSig)
const;
527 bool IsPayToScriptHash()
const;
528 bool IsPayToWitnessScriptHash()
const;
529 bool IsWitnessProgram(
int& version, std::vector<unsigned char>& program)
const;
533 bool IsPushOnly()
const;
536 bool HasValidOps()
const;
560 std::vector<std::vector<unsigned char> >
stack;
565 bool IsNull()
const {
return stack.empty(); }
567 void SetNull() { stack.clear(); stack.shrink_to_fit(); }
575 #endif // BITCOIN_SCRIPT_SCRIPT_H
bool operator!=(const int64_t &rhs) const
static const int MAX_PUBKEYS_PER_MULTISIG
static int64_t set_vch(const std::vector< unsigned char > &vch)
SERIALIZE_METHODS(CScript, obj)
bool IsOpSuccess(const opcodetype &opcode)
Test for OP_SUCCESSx opcodes as defined by BIP342.
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
CScriptNum operator-() const
static const unsigned int MAX_OPCODE
static void WriteLE16(unsigned char *ptr, uint16_t x)
CScriptNum(const int64_t &n)
Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
CScriptNum & operator-=(const int64_t &rhs)
CScriptNum operator&(const CScriptNum &rhs) const
static void WriteLE32(unsigned char *ptr, uint32_t x)
CScript & push_int64(int64_t n)
std::vector< std::vector< unsigned char > > stack
std::string GetOpName(opcodetype opcode)
void insert(Tdst &dst, const Tsrc &src)
Simplification of std insertion.
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
CScriptNum operator-(const CScriptNum &rhs) const
#define READWRITEAS(type, obj)
CScriptNum & operator=(const int64_t &rhs)
std::ostream & operator<<(std::ostream &os, BigO const &bigO)
bool GetOp(const_iterator &pc, opcodetype &opcodeRet) const
static std::vector< unsigned char > serialize(const int64_t &value)
CScriptNum operator+(const CScriptNum &rhs) const
static const int MAX_OPS_PER_SCRIPT
CScriptNum & operator+=(const CScriptNum &rhs)
bool operator==(const CScriptNum &rhs) const
bool operator<=(const int64_t &rhs) const
CScriptNum & operator&=(const CScriptNum &rhs)
CScript & operator<<(const CScriptNum &b)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
CScript & operator<<(opcodetype opcode)
opcodetype
Script opcodes.
CScript(const CScriptNum &b)
prevector< 28, unsigned char > CScriptBase
We use a prevector for the script to reduce the considerable memory overhead of vectors in cases wher...
CScriptNum operator-(const int64_t &rhs) const
CScript(const unsigned char *pbegin, const unsigned char *pend)
bool operator==(const int64_t &rhs) const
bool operator>=(const int64_t &rhs) const
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
scriptnum_error(const std::string &str)
CScriptNum(const std::vector< unsigned char > &vch, bool fRequireMinimal, const size_t nMaxNumSize=nDefaultMaxNumSize)
static const int MAX_SCRIPT_SIZE
static const int MAX_STACK_SIZE
static constexpr uint64_t VALIDATION_WEIGHT_PER_SIGOP_PASSED
CScriptNum & operator&=(const int64_t &rhs)
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
CScriptNum operator&(const int64_t &rhs) const
bool GetScriptOp(CScriptBase::const_iterator &pc, CScriptBase::const_iterator end, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet)
bool operator<(const CNetAddr &a, const CNetAddr &b)
CScriptNum & operator-=(const CScriptNum &rhs)
CScript(const_iterator pbegin, const_iterator pend)
static opcodetype EncodeOP_N(int n)
bool operator<=(const CScriptNum &rhs) const
Serialized script, used inside transaction inputs and outputs.
static constexpr unsigned int ANNEX_TAG
CScriptNum operator+(const int64_t &rhs) const
std::vector< unsigned char > getvch() const
CScript & operator<<(int64_t b)
CScriptNum & operator+=(const int64_t &rhs)
static constexpr uint64_t VALIDATION_WEIGHT_OFFSET
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
static const unsigned int LOCKTIME_THRESHOLD
bool operator!=(const CScriptNum &rhs) const
bool operator>=(const CScriptNum &rhs) const
static const uint32_t LOCKTIME_MAX
CScript(std::vector< unsigned char >::const_iterator pbegin, std::vector< unsigned char >::const_iterator pend)
std::vector< unsigned char > ToByteVector(const T &in)