Raven Core  3.0.0
P2P Digital Currency
ravenconsensus.cpp
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-2019 The Raven Core 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 #include "ravenconsensus.h"
8 
10 #include "pubkey.h"
11 #include "script/interpreter.h"
12 #include "version.h"
13 
14 namespace {
15 
17 class TxInputStream
18 {
19 public:
20  TxInputStream(int nTypeIn, int nVersionIn, const unsigned char *txTo, size_t txToLen) :
21  m_type(nTypeIn),
22  m_version(nVersionIn),
23  m_data(txTo),
24  m_remaining(txToLen)
25  {}
26 
27  void read(char* pch, size_t nSize)
28  {
29  if (nSize > m_remaining)
30  throw std::ios_base::failure(std::string(__func__) + ": end of data");
31 
32  if (pch == nullptr)
33  throw std::ios_base::failure(std::string(__func__) + ": bad destination buffer");
34 
35  if (m_data == nullptr)
36  throw std::ios_base::failure(std::string(__func__) + ": bad source buffer");
37 
38  memcpy(pch, m_data, nSize);
39  m_remaining -= nSize;
40  m_data += nSize;
41  }
42 
43  template<typename T>
44  TxInputStream& operator>>(T& obj)
45  {
46  ::Unserialize(*this, obj);
47  return *this;
48  }
49 
50  int GetVersion() const { return m_version; }
51  int GetType() const { return m_type; }
52 private:
53  const int m_type;
54  const int m_version;
55  const unsigned char* m_data;
56  size_t m_remaining;
57 };
58 
59 inline int set_error(ravenconsensus_error* ret, ravenconsensus_error serror)
60 {
61  if (ret)
62  *ret = serror;
63  return 0;
64 }
65 
66 struct ECCryptoClosure
67 {
68  ECCVerifyHandle handle;
69 };
70 
71 ECCryptoClosure instance_of_eccryptoclosure;
72 } // namespace
73 
75 static bool verify_flags(unsigned int flags)
76 {
77  return (flags & ~(ravenconsensus_SCRIPT_FLAGS_VERIFY_ALL)) == 0;
78 }
79 
80 static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, CAmount amount,
81  const unsigned char *txTo , unsigned int txToLen,
82  unsigned int nIn, unsigned int flags, ravenconsensus_error* err)
83 {
84  if (!verify_flags(flags)) {
86  }
87  try {
88  TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
89  CTransaction tx(deserialize, stream);
90  if (nIn >= tx.vin.size())
91  return set_error(err, ravenconsensus_ERR_TX_INDEX);
92  if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen)
93  return set_error(err, ravenconsensus_ERR_TX_SIZE_MISMATCH);
94 
95  // Regardless of the verification result, the tx did not error.
96  set_error(err, ravenconsensus_ERR_OK);
97 
98  PrecomputedTransactionData txdata(tx);
99  return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), &tx.vin[nIn].scriptWitness, flags, TransactionSignatureChecker(&tx, nIn, amount, txdata), nullptr);
100  } catch (const std::exception&) {
101  return set_error(err, ravenconsensus_ERR_TX_DESERIALIZE); // Error deserializing
102  }
103 }
104 
105 int ravenconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
106  const unsigned char *txTo , unsigned int txToLen,
107  unsigned int nIn, unsigned int flags, ravenconsensus_error* err)
108 {
109  CAmount am(amount);
110  return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
111 }
112 
113 
114 int ravenconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen,
115  const unsigned char *txTo , unsigned int txToLen,
116  unsigned int nIn, unsigned int flags, ravenconsensus_error* err)
117 {
119  return set_error(err, ravenconsensus_ERR_AMOUNT_REQUIRED);
120  }
121 
122  CAmount am(0);
123  return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
124 }
125 
127 {
128  // Just use the API version for now
129  return RAVENCONSENSUS_API_VER;
130 }
int ravenconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, const unsigned char *txTo, unsigned int txToLen, unsigned int nIn, unsigned int flags, ravenconsensus_error *err)
Returns 1 if the input nIn of the serialized transaction pointed to by txTo correctly spends the scri...
int ravenconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount, const unsigned char *txTo, unsigned int txToLen, unsigned int nIn, unsigned int flags, ravenconsensus_error *err)
int flags
Definition: raven-tx.cpp:500
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:967
constexpr deserialize_type deserialize
Definition: serialize.h:41
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:242
unsigned int ravenconsensus_version()
enum ravenconsensus_error_t ravenconsensus_error
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
void * memcpy(void *a, const void *b, size_t c)
void Unserialize(Stream &s, char &a)
Definition: serialize.h:194
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
#define RAVENCONSENSUS_API_VER