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