Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
interpreter.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Copyright (c) 2017-2020 The Bitcoin developers
4// Distributed under the MIT software license, see the accompanying
5// file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7#ifndef BITCOIN_SCRIPT_INTERPRETER_H
8#define BITCOIN_SCRIPT_INTERPRETER_H
9
12#include <script/script.h>
13#include <script/script_error.h>
14#include <script/script_flags.h>
16#include <script/sighashtype.h>
17
18#include <cstdint>
19#include <vector>
20
21class CPubKey;
22class CTransaction;
23class uint256;
24
25bool CastToBool(const std::vector<uint8_t> &vch);
26
27template <class T>
28uint256 SignatureHash(const CScript &scriptCode, const T &txTo,
29 unsigned int nIn, SigHashType sigHashType,
30 const Amount amount,
31 const PrecomputedTransactionData *cache = nullptr,
33
35public:
36 virtual bool VerifySignature(const std::vector<uint8_t> &vchSig,
37 const CPubKey &vchPubKey,
38 const uint256 &sighash) const;
39
40 virtual bool CheckSig(const std::vector<uint8_t> &vchSigIn,
41 const std::vector<uint8_t> &vchPubKey,
42 const CScript &scriptCode, uint32_t flags) const {
43 return false;
44 }
45
46 virtual bool CheckLockTime(const CScriptNum &nLockTime) const {
47 return false;
48 }
49
50 virtual bool CheckSequence(const CScriptNum &nSequence) const {
51 return false;
52 }
53
55};
56
57template <class T>
59private:
60 const T *txTo;
61 unsigned int nIn;
64
65public:
73
74 // The overridden functions are now final.
75 bool CheckSig(const std::vector<uint8_t> &vchSigIn,
76 const std::vector<uint8_t> &vchPubKey,
77 const CScript &scriptCode,
78 uint32_t flags) const final override;
79 bool CheckLockTime(const CScriptNum &nLockTime) const final override;
80 bool CheckSequence(const CScriptNum &nSequence) const final override;
81};
82
87
89private:
90 // stack (reference so we can modify an outside stack)
91 std::vector<std::vector<uint8_t>> &stack;
92 // altstack (owned)
93 std::vector<std::vector<uint8_t>> altstack;
94
95 // Script being executed
97
98 // Current position (program counter) in the executed Script
100 // End iterator of the executed Script
102 // Position of the last executed OP_CODESEPARATOR
104
105 // Number of executed non-push ops
106 size_t nOpCount = 0;
107
108 // true/false stack of nested OP_IF/OP_ELSE/OP_ENDIF
110
111 // Script flag of this execution
113
114 // Signature checker (reference)
116
117 // Execution metrics (e.g. for counting sigchecks)
119
120 // Last script error
122
123public:
124 ScriptInterpreter(std::vector<std::vector<uint8_t>> &stack,
128
129 // Whether the interpreter program counter reached the end of the Script.
130 // This does not reflect whether an error occured or not.
131 bool IsAtEnd();
132
133 // Do checks before running the first opcode, and set script_error on errror
134 bool CheckPreConditions();
135
136 // Do checks after running the last opcode, and set script_error on error
137 bool CheckPostConditions();
138
139 // Return the next opcode (with attached pushdata, if present)
140 bool GetNextOp(opcodetype &opcodeRet, std::vector<uint8_t> &vchRet) const;
141
142 // Run all opcodes to completion, setting script_error on any error
143 bool RunUntilEnd();
144
145 // Execute the next op of the script, return `false` if it failed
146 // May throw an exception if there's e.g. an integer overflow
147 bool RunNextOp();
148
149 // Get the condition stack, determining if opcodes are currently executed
151
152 // Current error of the Script. During execution, this will be UNKNOWN,
153 // upon successful execution it will be OK, and if RunNextOp returns false,
154 // this will be the error of the Script.
156
157 // Get the stack of the interpreter
158 const std::vector<std::vector<uint8_t>> &GetStack() const { return stack; }
159
160 // Get the altstack of the interpreter
161 const std::vector<std::vector<uint8_t>> &GetAltStack() const {
162 return altstack;
163 }
164};
165
166bool EvalScript(std::vector<std::vector<uint8_t>> &stack, const CScript &script,
167 uint32_t flags, const BaseSignatureChecker &checker,
168 ScriptExecutionMetrics &metrics, ScriptError *error = nullptr);
169static inline bool EvalScript(std::vector<std::vector<uint8_t>> &stack,
170 const CScript &script, uint32_t flags,
171 const BaseSignatureChecker &checker,
172 ScriptError *error = nullptr) {
174 return EvalScript(stack, script, flags, checker, dummymetrics, error);
175}
176
183bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey,
184 uint32_t flags, const BaseSignatureChecker &checker,
186 ScriptError *serror = nullptr);
187static inline bool VerifyScript(const CScript &scriptSig,
188 const CScript &scriptPubKey, uint32_t flags,
189 const BaseSignatureChecker &checker,
190 ScriptError *serror = nullptr) {
192 return VerifyScript(scriptSig, scriptPubKey, flags, checker, dummymetrics,
193 serror);
194}
195
196int FindAndDelete(CScript &script, const CScript &b);
197
198#endif // BITCOIN_SCRIPT_INTERPRETER_H
int flags
virtual bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
Definition interpreter.h:46
virtual bool CheckSequence(const CScriptNum &nSequence) const
Definition interpreter.h:50
virtual bool CheckSig(const std::vector< uint8_t > &vchSigIn, const std::vector< uint8_t > &vchPubKey, const CScript &scriptCode, uint32_t flags) const
Definition interpreter.h:40
virtual ~BaseSignatureChecker()
Definition interpreter.h:54
An encapsulated public key.
Definition pubkey.h:31
Serialized script, used inside transaction inputs and outputs.
Definition script.h:424
The basic transaction that is broadcasted on the network and contained in blocks.
A data type to abstract out the condition stack during script execution.
bool CheckSig(const std::vector< uint8_t > &vchSigIn, const std::vector< uint8_t > &vchPubKey, const CScript &scriptCode, uint32_t flags) const final override
const PrecomputedTransactionData * txdata
Definition interpreter.h:63
bool CheckSequence(const CScriptNum &nSequence) const final override
GenericTransactionSignatureChecker(const T *txToIn, unsigned int nInIn, const Amount &amountIn)
Definition interpreter.h:66
GenericTransactionSignatureChecker(const T *txToIn, unsigned int nInIn, const Amount &amountIn, const PrecomputedTransactionData &txdataIn)
Definition interpreter.h:69
bool CheckLockTime(const CScriptNum &nLockTime) const final override
CScript::const_iterator pend
ConditionStack vfExec
ScriptError script_error
const CScript & script
Definition interpreter.h:96
const std::vector< std::vector< uint8_t > > & GetStack() const
const std::vector< std::vector< uint8_t > > & GetAltStack() const
std::vector< std::vector< uint8_t > > altstack
Definition interpreter.h:93
CScript::const_iterator pbegincodehash
ScriptError GetScriptError()
const ConditionStack & GetConditionStack()
const BaseSignatureChecker & checker
CScript::const_iterator pc
Definition interpreter.h:99
std::vector< std::vector< uint8_t > > & stack
Definition interpreter.h:91
bool GetNextOp(opcodetype &opcodeRet, std::vector< uint8_t > &vchRet) const
ScriptExecutionMetrics & metrics
Signature hash type wrapper class.
Definition sighashtype.h:37
256-bit opaque blob.
Definition uint256.h:129
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metricsOut, ScriptError *serror=nullptr)
Execute an unlocking and locking script together.
int FindAndDelete(CScript &script, const CScript &b)
bool CastToBool(const std::vector< uint8_t > &vch)
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, SigHashType sigHashType, const Amount amount, const PrecomputedTransactionData *cache=nullptr, uint32_t flags=SCRIPT_ENABLE_SIGHASH_FORKID)
bool EvalScript(std::vector< std::vector< uint8_t > > &stack, const CScript &script, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metrics, ScriptError *error=nullptr)
bool error(const char *fmt, const Args &...args)
Definition logging.h:263
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
opcodetype
Script opcodes.
Definition script.h:47
ScriptError
@ SCRIPT_ENABLE_SIGHASH_FORKID
Precompute sighash midstate to avoid quadratic hashing.
Struct for holding cumulative results from executing a script or a sequence of scripts.