Bitcoin ABC 0.32.4
P2P Digital Currency
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
10#include <hash.h>
13#include <script/script.h>
14#include <script/script_error.h>
15#include <script/script_flags.h>
17#include <script/sighashtype.h>
18
19#include <cstdint>
20#include <optional>
21#include <vector>
22
23class CPubKey;
24class CTransaction;
25class uint256;
26
27bool CastToBool(const std::vector<uint8_t> &vch);
28
51 std::optional<std::pair<CScript, HashWriter>> m_cache_entries[12];
52
54 int CacheIndex(const SigHashType &hash_type) const noexcept;
55
56public:
58 [[nodiscard]] bool Load(const SigHashType &hash_type,
59 const CScript &script_code,
60 HashWriter &writer) const noexcept;
62 void Store(const SigHashType &hash_type, const CScript &script_code,
63 const HashWriter &writer) noexcept;
64};
65
66template <class T>
67uint256 SignatureHash(const CScript &scriptCode, const T &txTo,
68 unsigned int nIn, SigHashType sigHashType,
69 const Amount amount,
70 const PrecomputedTransactionData *cache = nullptr,
72 SigHashCache *sighash_cache = nullptr);
73
75public:
76 virtual bool VerifySignature(const std::vector<uint8_t> &vchSig,
77 const CPubKey &vchPubKey,
78 const uint256 &sighash) const;
79
80 virtual bool CheckSig(const std::vector<uint8_t> &vchSigIn,
81 const std::vector<uint8_t> &vchPubKey,
82 const CScript &scriptCode, uint32_t flags) const {
83 return false;
84 }
85
86 virtual bool CheckLockTime(const CScriptNum &nLockTime) const {
87 return false;
88 }
89
90 virtual bool CheckSequence(const CScriptNum &nSequence) const {
91 return false;
92 }
93
95};
96
97template <class T>
99private:
100 const T *txTo;
101 unsigned int nIn;
105
106public:
107 GenericTransactionSignatureChecker(const T *txToIn, unsigned int nInIn,
108 const Amount &amountIn)
109 : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(nullptr) {}
111 const T *txToIn, unsigned int nInIn, const Amount &amountIn,
112 const PrecomputedTransactionData &txdataIn)
113 : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(&txdataIn) {}
114
115 // The overridden functions are now final.
116 bool CheckSig(const std::vector<uint8_t> &vchSigIn,
117 const std::vector<uint8_t> &vchPubKey,
118 const CScript &scriptCode,
119 uint32_t flags) const final override;
120 bool CheckLockTime(const CScriptNum &nLockTime) const final override;
121 bool CheckSequence(const CScriptNum &nSequence) const final override;
122};
123
128
130private:
131 // stack (reference so we can modify an outside stack)
132 std::vector<std::vector<uint8_t>> &stack;
133 // altstack (owned)
134 std::vector<std::vector<uint8_t>> altstack;
135
136 // Script being executed
138
139 // Current position (program counter) in the executed Script
141 // End iterator of the executed Script
143 // Position of the last executed OP_CODESEPARATOR
145
146 // Number of executed non-push ops
147 size_t nOpCount = 0;
148
149 // true/false stack of nested OP_IF/OP_ELSE/OP_ENDIF
151
152 // Script flag of this execution
153 uint32_t flags;
154
155 // Signature checker (reference)
157
158 // Execution metrics (e.g. for counting sigchecks)
160
161 // Last script error
163
164public:
165 ScriptInterpreter(std::vector<std::vector<uint8_t>> &stack,
166 const CScript &script, uint32_t flags,
169
170 // Whether the interpreter program counter reached the end of the Script.
171 // This does not reflect whether an error occured or not.
172 bool IsAtEnd();
173
174 // Do checks before running the first opcode, and set script_error on errror
175 bool CheckPreConditions();
176
177 // Do checks after running the last opcode, and set script_error on error
178 bool CheckPostConditions();
179
180 // Return the next opcode (with attached pushdata, if present)
181 bool GetNextOp(opcodetype &opcodeRet, std::vector<uint8_t> &vchRet) const;
182
183 // Run all opcodes to completion, setting script_error on any error
184 bool RunUntilEnd();
185
186 // Execute the next op of the script, return `false` if it failed.
187 // The corresponding error can be obtained via GetScriptError
188 bool RunNextOp() noexcept;
189
190 // Get the condition stack, determining if opcodes are currently executed
192
193 // Current error of the Script. During execution, this will be UNKNOWN,
194 // upon successful execution it will be OK, and if RunNextOp returns false,
195 // this will be the error of the Script.
197
198 // Get the stack of the interpreter
199 const std::vector<std::vector<uint8_t>> &GetStack() const { return stack; }
200
201 // Get the altstack of the interpreter
202 const std::vector<std::vector<uint8_t>> &GetAltStack() const {
203 return altstack;
204 }
205
206private:
207 // Inner private opcode execution function, called by RunNextOp.
208 // Execute the next op of the script, return `false` if it failed.
209 // May throw an exception if there's e.g. an integer overflow.
210 bool RunNextOpInner();
211};
212
213bool EvalScript(std::vector<std::vector<uint8_t>> &stack, const CScript &script,
214 uint32_t flags, const BaseSignatureChecker &checker,
215 ScriptExecutionMetrics &metrics, ScriptError *error = nullptr);
216static inline bool EvalScript(std::vector<std::vector<uint8_t>> &stack,
217 const CScript &script, uint32_t flags,
218 const BaseSignatureChecker &checker,
219 ScriptError *error = nullptr) {
220 ScriptExecutionMetrics dummymetrics;
221 return EvalScript(stack, script, flags, checker, dummymetrics, error);
222}
223
230bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey,
231 uint32_t flags, const BaseSignatureChecker &checker,
232 ScriptExecutionMetrics &metricsOut,
233 ScriptError *serror = nullptr);
234static inline bool VerifyScript(const CScript &scriptSig,
235 const CScript &scriptPubKey, uint32_t flags,
236 const BaseSignatureChecker &checker,
237 ScriptError *serror = nullptr) {
238 ScriptExecutionMetrics dummymetrics;
239 return VerifyScript(scriptSig, scriptPubKey, flags, checker, dummymetrics,
240 serror);
241}
242
243int FindAndDelete(CScript &script, const CScript &b);
244
245#endif // BITCOIN_SCRIPT_INTERPRETER_H
int flags
Definition: bitcoin-tx.cpp:542
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:86
virtual bool CheckSequence(const CScriptNum &nSequence) const
Definition: interpreter.h:90
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:80
virtual ~BaseSignatureChecker()
Definition: interpreter.h:94
An encapsulated public key.
Definition: pubkey.h:31
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:432
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
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:103
bool CheckSequence(const CScriptNum &nSequence) const final override
GenericTransactionSignatureChecker(const T *txToIn, unsigned int nInIn, const Amount &amountIn)
Definition: interpreter.h:107
GenericTransactionSignatureChecker(const T *txToIn, unsigned int nInIn, const Amount &amountIn, const PrecomputedTransactionData &txdataIn)
Definition: interpreter.h:110
bool CheckLockTime(const CScriptNum &nLockTime) const final override
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:100
CScript::const_iterator pend
Definition: interpreter.h:142
ConditionStack vfExec
Definition: interpreter.h:150
ScriptError script_error
Definition: interpreter.h:162
bool CheckPostConditions()
const CScript & script
Definition: interpreter.h:137
const std::vector< std::vector< uint8_t > > & GetStack() const
Definition: interpreter.h:199
const std::vector< std::vector< uint8_t > > & GetAltStack() const
Definition: interpreter.h:202
std::vector< std::vector< uint8_t > > altstack
Definition: interpreter.h:134
CScript::const_iterator pbegincodehash
Definition: interpreter.h:144
ScriptError GetScriptError()
Definition: interpreter.h:196
bool RunNextOp() noexcept
const ConditionStack & GetConditionStack()
Definition: interpreter.h:191
const BaseSignatureChecker & checker
Definition: interpreter.h:156
CScript::const_iterator pc
Definition: interpreter.h:140
ScriptInterpreter(std::vector< std::vector< uint8_t > > &stack, const CScript &script, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metrics)
std::vector< std::vector< uint8_t > > & stack
Definition: interpreter.h:132
bool GetNextOp(opcodetype &opcodeRet, std::vector< uint8_t > &vchRet) const
ScriptExecutionMetrics & metrics
Definition: interpreter.h:159
Data structure to cache SHA256 midstates for the ECDSA sighash calculations (bare,...
Definition: interpreter.h:33
void Store(const SigHashType &hash_type, const CScript &script_code, const HashWriter &writer) noexcept
Store into this cache object the provided SHA256 midstate.
bool Load(const SigHashType &hash_type, const CScript &script_code, HashWriter &writer) const noexcept
Load into writer the SHA256 midstate if found in this cache.
std::optional< std::pair< CScript, HashWriter > > m_cache_entries[12]
For each sighash mode: [0] ALL [1] NONE [2] SINGLE [3] ALL|ANYONE [4] NONE|ANYONE [5] SINGLE|ANYONE [...
Definition: interpreter.h:51
int CacheIndex(const SigHashType &hash_type) const noexcept
Given a hash_type, find which of the 12 cache entries is to be used.
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)
Definition: interpreter.cpp:45
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, SigHashCache *sighash_cache=nullptr)
bool CastToBool(const std::vector< uint8_t > &vch)
Definition: interpreter.cpp:19
bool EvalScript(std::vector< std::vector< uint8_t > > &stack, const CScript &script, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metrics, ScriptError *error=nullptr)
opcodetype
Script opcodes.
Definition: script.h:51
ScriptError
Definition: script_error.h:11
@ SCRIPT_ENABLE_SIGHASH_FORKID
Definition: script_flags.h:85
Definition: amount.h:19
Precompute sighash midstate to avoid quadratic hashing.
Definition: transaction.h:325
Struct for holding cumulative results from executing a script or a sequence of scripts.