Skip to content

Commit

Permalink
Replace TxInputStream with RustDataStream in C++ zcash_script lib TEM…
Browse files Browse the repository at this point in the history
…P: change in zcashd
  • Loading branch information
teor2345 committed Apr 24, 2023
1 parent 014637a commit c9ebf22
Showing 1 changed file with 10 additions and 49 deletions.
59 changes: 10 additions & 49 deletions depend/zcash/src/script/zcash_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,6 @@
#include "version.h"

namespace {

/** A class that deserializes a single CTransaction one time. */
class TxInputStream
{
public:
TxInputStream(int nTypeIn, int nVersionIn, const unsigned char *txTo, size_t txToLen) :
m_type(nTypeIn),
m_version(nVersionIn),
m_data(txTo),
m_remaining(txToLen)
{}

void read(char* pch, size_t nSize)
{
if (nSize > m_remaining)
throw std::ios_base::failure(std::string(__func__) + ": end of data");

if (pch == NULL)
throw std::ios_base::failure(std::string(__func__) + ": bad destination buffer");

if (m_data == NULL)
throw std::ios_base::failure(std::string(__func__) + ": bad source buffer");

memcpy(pch, m_data, nSize);
m_remaining -= nSize;
m_data += nSize;
}

template<typename T>
TxInputStream& operator>>(T& obj)
{
::Unserialize(*this, obj);
return *this;
}

int GetVersion() const { return m_version; }
int GetType() const { return m_type; }
private:
const int m_type;
const int m_version;
const unsigned char* m_data;
size_t m_remaining;
};

inline int set_error(zcash_script_error* ret, zcash_script_error serror)
{
if (ret)
Expand Down Expand Up @@ -104,7 +60,8 @@ void* zcash_script_new_precomputed_tx(
zcash_script_error* err)
{
try {
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
const char* txToEnd = (const char *)(txTo + txToLen);
RustDataStream stream((const char *)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
CTransaction tx;
stream >> tx;
if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen) {
Expand Down Expand Up @@ -137,7 +94,8 @@ void* zcash_script_new_precomputed_tx_v5(
{
CTransaction tx;
try {
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
const char* txToEnd = (const char *)(txTo + txToLen);
RustDataStream stream((const char *)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
stream >> tx;
if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen) {
set_error(err, zcash_script_ERR_TX_SIZE_MISMATCH);
Expand Down Expand Up @@ -202,7 +160,8 @@ int zcash_script_verify(
zcash_script_error* err)
{
try {
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
const char* txToEnd = (const char *)(txTo + txToLen);
RustDataStream stream((const char *)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
CTransaction tx;
stream >> tx;
if (nIn >= tx.vin.size())
Expand Down Expand Up @@ -242,7 +201,8 @@ int zcash_script_verify_v5(
{
CTransaction tx;
try {
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
const char* txToEnd = (const char *)(txTo + txToLen);
RustDataStream stream((const char *)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
stream >> tx;
if (nIn >= tx.vin.size())
return set_error(err, zcash_script_ERR_TX_INDEX);
Expand Down Expand Up @@ -305,7 +265,8 @@ unsigned int zcash_script_legacy_sigop_count(
zcash_script_error* err)
{
try {
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
const char* txToEnd = (const char *)(txTo + txToLen);
RustDataStream stream((const char *)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
CTransaction tx;
stream >> tx;
if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen) {
Expand Down

0 comments on commit c9ebf22

Please sign in to comment.