Raven Core  3.0.0
P2P Digital Currency
core_read.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "core_io.h"
7 
8 #include "primitives/block.h"
10 #include "script/script.h"
11 #include "serialize.h"
12 #include "streams.h"
13 #include <univalue.h>
14 #include "util.h"
15 #include "utilstrencodings.h"
16 #include "version.h"
17 
18 #include <boost/algorithm/string/classification.hpp>
19 #include <boost/algorithm/string/predicate.hpp>
20 #include <boost/algorithm/string/replace.hpp>
21 #include <boost/algorithm/string/split.hpp>
22 
23 CScript ParseScript(const std::string& s)
24 {
25  CScript result;
26 
27  static std::map<std::string, opcodetype> mapOpNames;
28 
29  if (mapOpNames.empty())
30  {
31  for (unsigned int op = 0; op <= MAX_OPCODE; op++)
32  {
33  // Allow OP_RESERVED to get into mapOpNames
34  if (op < OP_NOP && op != OP_RESERVED)
35  continue;
36 
37  const char* name = GetOpName((opcodetype)op);
38  if (strcmp(name, "OP_UNKNOWN") == 0)
39  continue;
40  std::string strName(name);
41  mapOpNames[strName] = (opcodetype)op;
42  // Convenience: OP_ADD and just ADD are both recognized:
43  boost::algorithm::replace_first(strName, "OP_", "");
44  mapOpNames[strName] = (opcodetype)op;
45  }
46  }
47 
48  std::vector<std::string> words;
49  boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
50 
51  for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w)
52  {
53  if (w->empty())
54  {
55  // Empty string, ignore. (boost::split given '' will return one word)
56  }
57  else if (all(*w, boost::algorithm::is_digit()) ||
58  (boost::algorithm::starts_with(*w, "-") && all(std::string(w->begin()+1, w->end()), boost::algorithm::is_digit())))
59  {
60  // Number
61  int64_t n = atoi64(*w);
62  result << n;
63  }
64  else if (boost::algorithm::starts_with(*w, "0x") && (w->begin()+2 != w->end()) && IsHex(std::string(w->begin()+2, w->end())))
65  {
66  // Raw hex data, inserted NOT pushed onto stack:
67  std::vector<unsigned char> raw = ParseHex(std::string(w->begin()+2, w->end()));
68  result.insert(result.end(), raw.begin(), raw.end());
69  }
70  else if (w->size() >= 2 && boost::algorithm::starts_with(*w, "'") && boost::algorithm::ends_with(*w, "'"))
71  {
72  // Single-quoted string, pushed as data. NOTE: this is poor-man's
73  // parsing, spaces/tabs/newlines in single-quoted strings won't work.
74  std::vector<unsigned char> value(w->begin()+1, w->end()-1);
75  result << value;
76  }
77  else if (mapOpNames.count(*w))
78  {
79  // opcode, e.g. OP_ADD or ADD:
80  result << mapOpNames[*w];
81  }
82  else
83  {
84  throw std::runtime_error("script parse error");
85  }
86  }
87 
88  return result;
89 }
90 
91 // Check that all of the input and output scripts of a transaction contains valid opcodes
93 {
94  // Check input scripts for non-coinbase txs
95  if (!CTransaction(tx).IsCoinBase()) {
96  for (unsigned int i = 0; i < tx.vin.size(); i++) {
97  if (!tx.vin[i].scriptSig.HasValidOps() || tx.vin[i].scriptSig.size() > MAX_SCRIPT_SIZE) {
98  return false;
99  }
100  }
101  }
102  // Check output scripts
103  for (unsigned int i = 0; i < tx.vout.size(); i++) {
104  if (!tx.vout[i].scriptPubKey.HasValidOps() || tx.vout[i].scriptPubKey.size() > MAX_SCRIPT_SIZE) {
105  return false;
106  }
107  }
108 
109  return true;
110 }
111 
112 bool DecodeHexTx(CMutableTransaction& tx, const std::string& strHexTx, bool fTryNoWitness)
113 {
114  if (!IsHex(strHexTx)) {
115  return false;
116  }
117 
118  std::vector<unsigned char> txData(ParseHex(strHexTx));
119 
120  if (fTryNoWitness) {
121  CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
122  try {
123  ssData >> tx;
124  if (ssData.eof() && CheckTxScriptsSanity(tx)) {
125  return true;
126  }
127  }
128  catch (const std::exception&) {
129  // Fall through.
130  }
131  }
132 
133  CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
134  try {
135  ssData >> tx;
136  if (!ssData.empty()) {
137  return false;
138  }
139  }
140  catch (const std::exception&) {
141  return false;
142  }
143 
144  return true;
145 }
146 
147 bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
148 {
149  if (!IsHex(strHexBlk))
150  return false;
151 
152  std::vector<unsigned char> blockData(ParseHex(strHexBlk));
153  CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
154  try {
155  ssBlock >> block;
156  }
157  catch (const std::exception&) {
158  return false;
159  }
160 
161  return true;
162 }
163 
164 uint256 ParseHashUV(const UniValue& v, const std::string& strName)
165 {
166  std::string strHex;
167  if (v.isStr())
168  strHex = v.getValStr();
169  return ParseHashStr(strHex, strName); // Note: ParseHashStr("") throws a runtime_error
170 }
171 
172 uint256 ParseHashStr(const std::string& strHex, const std::string& strName)
173 {
174  if (!IsHex(strHex)) // Note: IsHex("") is false
175  throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
176 
177  uint256 result;
178  result.SetHex(strHex);
179  return result;
180 }
181 
182 std::vector<unsigned char> ParseHexUV(const UniValue& v, const std::string& strName)
183 {
184  std::string strHex;
185  if (v.isStr())
186  strHex = v.getValStr();
187  if (!IsHex(strHex))
188  throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')");
189  return ParseHex(strHex);
190 }
std::vector< unsigned char > ParseHexUV(const UniValue &v, const std::string &strName)
Definition: core_read.cpp:182
iterator insert(iterator pos, const T &value)
Definition: prevector.h:344
Definition: block.h:73
std::vector< CTxIn > vin
Definition: transaction.h:391
bool isStr() const
Definition: univalue.h:83
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
bool empty() const
Definition: streams.h:239
const std::string & getValStr() const
Definition: univalue.h:67
iterator end()
Definition: prevector.h:293
opcodetype
Script opcodes.
Definition: script.h:51
bool IsHex(const std::string &str)
Definition: script.h:80
uint256 ParseHashStr(const std::string &strHex, const std::string &strName)
Definition: core_read.cpp:172
bool CheckTxScriptsSanity(const CMutableTransaction &tx)
Definition: core_read.cpp:92
std::vector< CTxOut > vout
Definition: transaction.h:392
const char * GetOpName(opcodetype opcode)
Definition: script.cpp:15
CScript ParseScript(const std::string &s)
Definition: core_read.cpp:23
256-bit opaque blob.
Definition: uint256.h:123
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
int64_t atoi64(const char *psz)
A mutable version of CTransaction.
Definition: transaction.h:389
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
void SetHex(const char *psz)
Definition: uint256.cpp:28
bool eof() const
Definition: streams.h:334
uint256 ParseHashUV(const UniValue &v, const std::string &strName)
Definition: core_read.cpp:164
bool DecodeHexTx(CMutableTransaction &tx, const std::string &strHexTx, bool fTryNoWitness)
Definition: core_read.cpp:112
bool DecodeHexBlk(CBlock &block, const std::string &strHexBlk)
Definition: core_read.cpp:147
std::vector< unsigned char > ParseHex(const char *psz)