Skip to content

Commit

Permalink
Change CTxDestination into a class to support serialization (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Dec 26, 2023
1 parent baec26d commit 3bc1ba4
Showing 1 changed file with 69 additions and 2 deletions.
71 changes: 69 additions & 2 deletions src/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "keystore.h"
#include "prevector.h"
#include <util/hash_type.h>
#include "serialize.h"
#include "wallet/ismine.h"

typedef std::vector<unsigned char> valtype;
Expand All @@ -30,7 +31,14 @@ class CScriptID : public BaseHash<uint160>
CScriptID() : BaseHash() {}
explicit CScriptID(const CScript& in);
explicit CScriptID(const uint160& in) : BaseHash(in) {}
// explicit CScriptID(const ScriptHash& in);

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(m_hash);
}
};

static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520; // bytes
Expand Down Expand Up @@ -93,7 +101,66 @@ class CNoDestination {
* * CScriptID: TX_SCRIPTHASH destination
* A CTxDestination is the internal data type encoded in a CBitcoinAddress
*/
typedef std::variant<CNoDestination, CKeyID, CScriptID> CTxDestination;
typedef std::variant<CNoDestination, CKeyID, CScriptID> CTxDestinationVariant;

class CTxDestination : public CTxDestinationVariant
{
public:
CTxDestination() : CTxDestinationVariant()
{}

CTxDestination(CTxDestination& dest) : CTxDestinationVariant(dest)
{}

CTxDestination(const CTxDestination& dest) : CTxDestinationVariant(dest)
{}

CTxDestination(CNoDestination dest) : CTxDestinationVariant(dest)
{}

CTxDestination(CKeyID dest) : CTxDestinationVariant(dest)
{}

CTxDestination(CScriptID dest) : CTxDestinationVariant(dest)
{}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
// The implicit cast here is to reduce the size of the variant index for (de)serialization. We will never need more than
// 256 different types in CTxDestinationVariant.
uint8_t variant_idx = this->index();

READWRITE(variant_idx);

// There is no (de)serialization necessary for CNoDestination.
// The indexes are in order of the typedef variant types specified above in CTxDestinationVariant.
// If new destination types are added to CTxDestinationVariant, then the below will need to be
// extended.

if (variant_idx == 0) {
return;
}

if (variant_idx == 1) {
CKeyID dest = std::get<1>(*this);

READWRITE(dest);

this->emplace<1>(dest);
}

if (variant_idx == 2) {
CScriptID dest = std::get<2>(*this);

READWRITE(dest);

this->emplace<2>(dest);
}
}
};

const char* GetTxnOutputType(txnouttype t);

Expand Down

0 comments on commit 3bc1ba4

Please sign in to comment.