Skip to content

Commit

Permalink
Merge pull request #135 from cculianu/cashtokens
Browse files Browse the repository at this point in the history
Implement correct address indexing for outputs containing CashTokens (BCH) + Code cleanup
  • Loading branch information
cculianu authored Sep 14, 2022
2 parents e45914c + f7305d4 commit 15a67c6
Show file tree
Hide file tree
Showing 66 changed files with 4,384 additions and 2,013 deletions.
8 changes: 5 additions & 3 deletions Fulcrum.pro
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ SOURCES += \
bitcoin/crypto/sha256.cpp \
bitcoin/crypto/sha256_sse4.cpp \
bitcoin/crypto/sha512.cpp \
bitcoin/feerate.cpp \
bitcoin/hash.cpp \
bitcoin/interpreter.cpp \
bitcoin/pubkey.cpp \
Expand All @@ -407,9 +406,11 @@ SOURCES += \
bitcoin/script_standard.cpp \
bitcoin/sigencoding.cpp \
bitcoin/test.cpp \
bitcoin/token.cpp \
bitcoin/transaction.cpp \
bitcoin/uint256.cpp \
bitcoin/utilstrencodings.cpp
bitcoin/utilstrencodings.cpp \
bitcoin/utilstring.cpp

HEADERS += \
bitcoin/amount.h \
Expand All @@ -431,7 +432,6 @@ HEADERS += \
bitcoin/crypto/sha1.h \
bitcoin/crypto/sha256.h \
bitcoin/crypto/sha512.h \
bitcoin/feerate.h \
bitcoin/hash.h \
bitcoin/interpreter.h \
bitcoin/litecoin_bits.h \
Expand All @@ -450,10 +450,12 @@ HEADERS += \
bitcoin/support/cleanse.h \
bitcoin/support/zeroafterfree.h \
bitcoin/tinyformat.h \
bitcoin/token.h \
bitcoin/transaction.h \
bitcoin/txid.h \
bitcoin/uint256.h \
bitcoin/utilstrencodings.h \
bitcoin/utilstring.h \
bitcoin/version.h

# Enable secp256k1 compilation on x86_64 only -- we don't actually use this lib
Expand Down
2 changes: 1 addition & 1 deletion contrib/rpm/fulcrum.spec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: {{{ git_repo_name name="fulcrum" }}}
Version: 1.7.0
Version: 1.8.0
Release: {{{ git_repo_version }}}%{?dist}
Summary: A fast & nimble SPV server for Bitcoin Cash & Bitcoin BTC

Expand Down
4 changes: 2 additions & 2 deletions doc/unix-man-page.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% FULCRUM(1) Version 1.7.0 | Fulcrum Manual
% FULCRUM(1) Version 1.8.0 | Fulcrum Manual
% Fulcrum is written by Calin Culianu (cculianu)
% June 05, 2022
% September 11, 2022

# NAME

Expand Down
14 changes: 0 additions & 14 deletions src/BTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,6 @@ namespace BTC
}


/// specialization for CTransaction which works differently
template <> bitcoin::CTransaction Deserialize(const QByteArray &bytes, int pos, bool allowSegWit, bool allowMW,
bool noJunkAtEnd)
{
int version = bitcoin::PROTOCOL_VERSION;
if (allowSegWit) version |= bitcoin::SERIALIZE_TRANSACTION_USE_WITNESS;
if (allowMW) version |= bitcoin::SERIALIZE_TRANSACTION_USE_MWEB;
bitcoin::GenericVectorReader<QByteArray> vr(bitcoin::SER_NETWORK, version, bytes, pos);
auto ret = bitcoin::CTransaction(bitcoin::deserialize, vr);
if (noJunkAtEnd && !vr.empty())
throw std::ios_base::failure("Got unprocessed bytes at the end when deserializeing a bitcoin object");
return ret;
}

QByteArray Hash(const QByteArray &b, bool once)
{
bitcoin::CHash256 h(once);
Expand Down
35 changes: 23 additions & 12 deletions src/BTC.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ namespace BTC
/// Specify from_pos=-1 for appending at the end. Returns a reference to the passed-in buffer. This is very fast
/// and done in-place.
template <typename BitcoinObject>
QByteArray & Serialize(QByteArray &buf, const BitcoinObject &thing, int from_pos = -1, bool allowSegWit = false, bool allowMW = false)
QByteArray & Serialize(QByteArray &buf, const BitcoinObject &thing, int from_pos = -1, bool allowSegWit = false,
bool allowMW = false)
{
if (from_pos < 0) from_pos = buf.size();
int version = bitcoin::PROTOCOL_VERSION;
Expand All @@ -82,16 +83,19 @@ namespace BTC
Serialize(ret, thing, -1, allowSegWit, allowMW);
return ret;
}
/// Deserialize to a pre-allocated bitcoin object such as bitcoin::CBlock, bitcoin::CBlockHeader, bitcoin::CMutableTransaction, etc
/// Deserialize to a pre-allocated bitcoin object such as bitcoin::CBlock, bitcoin::CBlockHeader,
/// bitcoin::CMutableTransaction, etc
template <typename BitcoinObject,
/// NB: This in-place Deserialization does *NOT* work with CTransaction because if has const-fields. (use the non-in-place specialization instead)
/// NB: This in-place Deserialization does *NOT* work with CTransaction because if has const-fields.
/// (use the non-in-place specialization instead)
std::enable_if_t<!std::is_same_v<BitcoinObject, bitcoin::CTransaction>, int> = 0 >
void Deserialize(BitcoinObject &thing, const QByteArray &bytes, int pos = 0, bool allowSegWit = false, bool allowMW = false,
bool throwIfJunkAtEnd = false)
void Deserialize(BitcoinObject &thing, const QByteArray &bytes, int pos = 0, bool allowSegWit = false,
bool allowMW = false, bool allowCashTokens = true, bool throwIfJunkAtEnd = false)
{
int version = bitcoin::PROTOCOL_VERSION;
if (allowSegWit) version |= bitcoin::SERIALIZE_TRANSACTION_USE_WITNESS;
if (allowMW) version |= bitcoin::SERIALIZE_TRANSACTION_USE_MWEB;
if (allowCashTokens) version |= bitcoin::SERIALIZE_TRANSACTION_USE_CASHTOKENS;
bitcoin::GenericVectorReader<QByteArray> vr(bitcoin::SER_NETWORK, version, bytes, pos);
thing.Unserialize(vr);
if (throwIfJunkAtEnd && !vr.empty())
Expand All @@ -100,31 +104,38 @@ namespace BTC
/// Convenience for above. Create an instance of object and deserialize to it
template <typename BitcoinObject>
BitcoinObject Deserialize(const QByteArray &bytes, int pos = 0, bool allowSegWit = false, bool allowMW = false,
bool noJunkAtEnd = false)
bool allowCashTokens = true, bool noJunkAtEnd = false)
{
BitcoinObject ret;
Deserialize(ret, bytes, pos, allowSegWit, allowMW, noJunkAtEnd);
Deserialize(ret, bytes, pos, allowSegWit, allowMW, allowCashTokens, noJunkAtEnd);
return ret;
}

template <typename BitcoinObject>
struct is_block_or_tx {
using BO = std::decay_t<BitcoinObject>;
static constexpr bool value = std::is_base_of_v<bitcoin::CBlock, BO> || std::is_same_v<bitcoin::CTransaction, BO>
|| std::is_same_v<bitcoin::CMutableTransaction, BO>;
static constexpr bool value = std::is_base_of_v<bitcoin::CBlock, BO>
|| std::is_same_v<bitcoin::CTransaction, BO>
|| std::is_same_v<bitcoin::CMutableTransaction, BO>;
};

template <typename BitcoinObject>
inline constexpr bool is_block_or_tx_v = is_block_or_tx<BitcoinObject>::value;

/// Template specialization for CTransaction which has const fields and works a little differently (impl. in BTC.cpp)
template <> bitcoin::CTransaction Deserialize(const QByteArray &, int pos, bool allowSegWit, bool allowMW, bool noJunkAtEnd);
/// Template specialization for CTransaction which has const fields and works a little differently
template <> inline bitcoin::CTransaction Deserialize(const QByteArray &ba, int pos, bool allowSegWit, bool allowMW,
bool allowCashTokens, bool noJunkAtEnd)
{
// This *does* move the vectors from CMutableTransaction -> CTransaction
return bitcoin::CTransaction{Deserialize<bitcoin::CMutableTransaction>(ba, pos, allowSegWit, allowMW,
allowCashTokens, noJunkAtEnd)};
}

/// Convenience to deserialize segwit object (block or tx) (Core only)
template <typename BitcoinObject>
std::enable_if_t<is_block_or_tx_v<BitcoinObject>, BitcoinObject>
/* BitcoinObject */ DeserializeSegWit(const QByteArray &ba, int pos = 0) {
return Deserialize<BitcoinObject>(ba, pos, true, false);
return Deserialize<BitcoinObject>(ba, pos, /* segwit= */ true, /* mw= */ false, /* cashtokens= */false);
}

/// Convenience to serialize segwit object (block or tx) (Core only)
Expand Down
Loading

0 comments on commit 15a67c6

Please sign in to comment.