Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bls/bls.h
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ class CBLSLazyWrapper
hash.SetNull();
}
if (hash.IsNull()) {
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
HashWriter ss{};
ss.write(MakeByteSpan(vecBytes));
hash = ss.GetHash();
}
Expand Down
12 changes: 10 additions & 2 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex*
/** Used to marshal pointers into hashes for db storage. */
class CDiskBlockIndex : public CBlockIndex
{
/** Historically CBlockLocator's version field has been written to disk
* streams as the client version, but the value has never been used.
*
* Hard-code to the highest client version ever written.
* SerParams can be used if the field requires any meaning in the future.
**/
static constexpr int DUMMY_VERSION = 240100;

public:
uint256 hash;
uint256 hashPrev;
Expand All @@ -391,8 +399,8 @@ class CDiskBlockIndex : public CBlockIndex
SERIALIZE_METHODS(CDiskBlockIndex, obj)
{
LOCK(::cs_main);
int _nVersion = s.GetVersion();
if (!(s.GetType() & SER_GETHASH)) READWRITE(VARINT_MODE(_nVersion, VarIntMode::NONNEGATIVE_SIGNED));
int _nVersion = DUMMY_VERSION;
READWRITE(VARINT_MODE(_nVersion, VarIntMode::NONNEGATIVE_SIGNED));

READWRITE(VARINT_MODE(obj.nHeight, VarIntMode::NONNEGATIVE_SIGNED));
READWRITE(VARINT(obj.nStatus));
Expand Down
2 changes: 1 addition & 1 deletion src/chainlock/clsig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class DecodedSignatureCache
// process-wide scheme flag, including its opposite-scheme rejection. The
// flag is part of the key so a transient value cannot leak into later calls.
const bool legacy = bls::bls_legacy_scheme.load();
const uint256 key = (CHashWriter(SER_GETHASH, 0) << Span{bytes} << legacy).GetHash();
const uint256 key = (HashWriter{} << Span{bytes} << legacy).GetHash();
CBLSSignature sig;
{
LOCK(m_mutex);
Expand Down
4 changes: 2 additions & 2 deletions src/coinjoin/coinjoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool CCoinJoinEntry::AddScriptSig(const CTxIn& txin)

uint256 CCoinJoinQueue::GetSignatureHash() const
{
return SerializeHash(*this, SER_GETHASH, PROTOCOL_VERSION);
return SerializeSigHash(*this);
}
uint256 CCoinJoinQueue::GetHash() const { return SerializeHash(*this, SER_NETWORK, PROTOCOL_VERSION); }

Expand Down Expand Up @@ -76,7 +76,7 @@ bool CCoinJoinQueue::IsTimeOutOfBounds() const

uint256 CCoinJoinBroadcastTx::GetSignatureHash() const
{
return SerializeHash(*this, SER_GETHASH, PROTOCOL_VERSION);
return SerializeSigHash(*this);
}

bool CCoinJoinBroadcastTx::CheckSignature(const CBLSPublicKey& blsPubKey) const
Expand Down
5 changes: 3 additions & 2 deletions src/coinjoin/coinjoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class CBLSPublicKey;
class CBlockIndex;
class ChainstateManager;
class CTxMemPool;
class SigHashWriter;

namespace chainlock {
class Chainlocks;
Expand Down Expand Up @@ -270,7 +271,7 @@ class CCoinJoinQueue
SERIALIZE_METHODS(CCoinJoinQueue, obj)
{
READWRITE(obj.nDenom, obj.m_protxHash, obj.nTime, obj.fReady);
if (!(s.GetType() & SER_GETHASH)) {
if (!std::is_same_v<Stream, SigHashWriter>) {
READWRITE(obj.vchSig);
}
}
Expand Down Expand Up @@ -325,7 +326,7 @@ class CCoinJoinBroadcastTx
{
READWRITE(obj.tx, obj.m_protxHash);

if (!(s.GetType() & SER_GETHASH)) {
if (!std::is_same_v<Stream, SigHashWriter>) {
READWRITE(obj.vchSig);
}
READWRITE(obj.sigTime);
Expand Down
9 changes: 4 additions & 5 deletions src/dbwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <dbwrapper.h>

#include <clientversion.h>
#include <common/system.h>
#include <logging.h>
#include <random.h>
Expand Down Expand Up @@ -157,9 +156,9 @@ struct CDBBatch::WriteBatchImpl {
leveldb::WriteBatch batch;
};

CDBBatch::CDBBatch(const CDBWrapper& _parent) : parent(_parent),
m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()},
ssValue(SER_DISK, CLIENT_VERSION){};
CDBBatch::CDBBatch(const CDBWrapper& _parent)
: parent{_parent},
m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()} {};

CDBBatch::~CDBBatch() = default;

Expand All @@ -169,7 +168,7 @@ void CDBBatch::Clear()
size_estimate = 0;
}

void CDBBatch::WriteImpl(Span<const std::byte> key, CDataStream& ssValue)
void CDBBatch::WriteImpl(Span<const std::byte> key, DataStream& ssValue)
{
leveldb::Slice slKey(CharCast(key.data()), key.size());
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
Expand Down
14 changes: 7 additions & 7 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ class CDBBatch
const std::unique_ptr<WriteBatchImpl> m_impl_batch;

DataStream ssKey{};
CDataStream ssValue;
DataStream ssValue{};

size_t size_estimate{0};

void WriteImpl(Span<const std::byte> key, CDataStream& ssValue);
void WriteImpl(Span<const std::byte> key, DataStream& ssValue);
void EraseImpl(Span<const std::byte> key);

public:
Expand Down Expand Up @@ -199,7 +199,7 @@ class CDBIterator

template<typename V> bool GetValue(V& value) {
try {
CDataStream ssValue{GetValueImpl(), SER_DISK, CLIENT_VERSION};
DataStream ssValue{GetValueImpl()};
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
ssValue >> value;
} catch (const std::exception&) {
Expand Down Expand Up @@ -252,21 +252,21 @@ class CDBWrapper
CDBWrapper& operator=(const CDBWrapper&) = delete;

template <typename K>
bool ReadDataStream(const K& key, CDataStream& ssValue) const
bool ReadDataStream(const K& key, DataStream& ssValue) const
{
DataStream ssKey{};
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
return ReadDataStream(ssKey, ssValue);
}

bool ReadDataStream(const DataStream& ssKey, CDataStream& ssValue) const
bool ReadDataStream(const DataStream& ssKey, DataStream& ssValue) const
{
std::optional<std::string> strValue{ReadImpl(ssKey)};
if (!strValue) {
return false;
}
CDataStream ssValueTmp{MakeByteSpan(*strValue), SER_DISK, CLIENT_VERSION};
DataStream ssValueTmp{MakeByteSpan(*strValue)};
ssValueTmp.Xor(obfuscate_key);
ssValue = std::move(ssValueTmp);
return true;
Expand All @@ -284,7 +284,7 @@ class CDBWrapper
template <typename V>
bool Read(const DataStream& ssKey, V& value) const
{
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
DataStream ssValue{};
if (!ReadDataStream(ssKey, ssValue)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ bool CDeterministicMNManager::MigrateLegacyDiffs(const CBlockIndex* const tip_in
}

// Use legacy-aware deserialization for DB_LIST_DIFF_LEGACY entries
CDataStream s(SER_DISK, CLIENT_VERSION);
DataStream s{};
if (!m_evoDb.GetRawDB().ReadDataStream(key, s)) {
break;
}
Expand Down
13 changes: 3 additions & 10 deletions src/evo/deterministicmns.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,7 @@ class CDeterministicMN
READWRITE(obj.collateralOutpoint);
READWRITE(obj.nOperatorReward);
READWRITE(obj.pdmnState);
// We can't know if we are serialising for the Disk or for the Network here (s.GetType() is not accessible)
// Therefore if s.GetVersion() == CLIENT_VERSION -> Then we know we are serialising for the Disk
// Otherwise, we can safely check with protocol versioning logic so we won't break old clients
if (s.GetVersion() == CLIENT_VERSION || s.GetVersion() >= DMN_TYPE_PROTO_VERSION) {
READWRITE(obj.nType);
} else {
SER_READ(obj, obj.nType = MnType::Regular);
}
READWRITE(obj.nType);
}

[[nodiscard]] bool HasInternalId() const { return internalId != std::numeric_limits<uint64_t>::max(); }
Expand Down Expand Up @@ -544,9 +537,9 @@ class CDeterministicMNList
// an invalid entry and subsequent attempts at registering ADDRv2 entries get blocked. We cannot
// apply this treatment ADDRv1 compatible addresses for backwards compatibility with the existing map.
const auto& ser_params{v.IsAddrV1Compatible() ? CNetAddr::V1 : CNetAddr::V2};
return ::SerializeHash(ser_params(v), /*nType=*/SER_GETHASH, /*nVersion=*/PROTOCOL_VERSION);
return ::SerializeHash(ser_params(v));
} else {
return ::SerializeHash(v, /*nType=*/SER_GETHASH, /*nVersion=*/PROTOCOL_VERSION);
return ::SerializeHash(v);
}
}
template <typename T>
Expand Down
6 changes: 3 additions & 3 deletions src/evo/providertx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ uint256 CProRegTx::MakeSharedRegConsentHash(const CTransaction& tx) const
// signatures. Covering the sequences matters: BIP68 gives them consensus meaning on
// version >= 2 transactions, so an uncovered sequence rewrite could impose a months-long
// relative timelock on a fully consented registration.
CHashWriter hw(SER_GETHASH, CLIENT_VERSION);
HashWriter hw{};
hw << std::string("DashSharedMNReg");
hw << nVersion;
hw << tx.nVersion;
Expand Down Expand Up @@ -434,7 +434,7 @@ std::string CProRegTx::MakeSignString() const
s += EncodeDestination(PKHash(keyIDVoting)) + "|";

// ... and also the full hash of the payload as a protection against malleability and replays
s += ::SerializeHash(*this).ToString();
s += ::SerializeSigHash(*this).ToString();

return s;
}
Expand Down Expand Up @@ -551,7 +551,7 @@ uint256 CProDisTx::MakeSignHash(const CTransaction& tx, uint8_t sig_count) const
// unanimous dissolution as a unilateral one (or vice versa) by dropping/adding signatures.
// Together with the empty-scriptSig rule and the low-S requirement this pins every free byte of
// a ProDisTx, so its txid is non-malleable by third parties.
CHashWriter hw(SER_GETHASH, CLIENT_VERSION);
HashWriter hw{};
hw << std::string("DashSharedMNDissolve");
hw << nVersion;
hw << tx.nVersion;
Expand Down
12 changes: 6 additions & 6 deletions src/evo/providertx.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class CProRegTx
obj.platformHTTPPort);
}
}
if (!(s.GetType() & SER_GETHASH)) {
if (!std::is_same_v<Stream, SigHashWriter>) {
READWRITE(obj.vchSig);
}
}
Expand Down Expand Up @@ -304,7 +304,7 @@ class CProUpServTx
obj.platformHTTPPort);
}
}
if (!(s.GetType() & SER_GETHASH)) {
if (!std::is_same_v<Stream, SigHashWriter>) {
READWRITE(
CBLSSignatureVersionWrapper(const_cast<CBLSSignature&>(obj.sig), (obj.nVersion == ProTxVersion::LegacyBLS))
);
Expand Down Expand Up @@ -368,7 +368,7 @@ class CProUpRegTx
READWRITE(
obj.inputsHash
);
if (!(s.GetType() & SER_GETHASH)) {
if (!std::is_same_v<Stream, SigHashWriter>) {
READWRITE(
obj.vchSig
);
Expand Down Expand Up @@ -422,7 +422,7 @@ class CProUpRevTx
obj.nReason,
obj.inputsHash
);
if (!(s.GetType() & SER_GETHASH)) {
if (!std::is_same_v<Stream, SigHashWriter>) {
READWRITE(
CBLSSignatureVersionWrapper(const_cast<CBLSSignature&>(obj.sig), (obj.nVersion == ProTxVersion::LegacyBLS))
);
Expand Down Expand Up @@ -505,7 +505,7 @@ class CProUpShareTx
SERIALIZE_METHODS(CProUpShareTx, obj)
{
READWRITE(obj.nVersion, obj.proTxHash, obj.shareIndex, obj.scriptReward, obj.inputsHash);
if (!(s.GetType() & SER_GETHASH)) {
if (!std::is_same_v<Stream, SigHashWriter>) {
READWRITE(obj.vchSig);
}
}
Expand Down Expand Up @@ -541,7 +541,7 @@ class CProUpSharedRegTx
READWRITE(obj.nVersion, obj.proTxHash,
CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.pubKeyOperator), /*legacy=*/false),
obj.keyIDVoting, obj.inputsHash);
if (!(s.GetType() & SER_GETHASH)) {
if (!std::is_same_v<Stream, SigHashWriter>) {
uint8_t sig_count{0};
SER_WRITE(obj, sig_count = static_cast<uint8_t>(obj.vchSigs.size()));
READWRITE(sig_count);
Expand Down
8 changes: 4 additions & 4 deletions src/evo/providertx_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ std::optional<ProviderTxError> SignPayload(const CMutableTransaction& tx, Payloa
{
UpdateInputsHash(tx, payload);
payload.vchSig.clear();
if (!wallet.signSpecialTxPayload(::SerializeHash(payload), key_id, payload.vchSig)) {
if (!wallet.signSpecialTxPayload(::SerializeSigHash(payload), key_id, payload.vchSig)) {
return Error(ProviderTxErrorCode::INTERNAL_ERROR, "failed to sign special tx");
}
return std::nullopt;
Expand All @@ -347,7 +347,7 @@ template <typename Payload>
void SignPayload(const CMutableTransaction& tx, Payload& payload, const CBLSSecretKey& key, bool legacy)
{
UpdateInputsHash(tx, payload);
payload.sig = key.Sign(::SerializeHash(payload), legacy);
payload.sig = key.Sign(::SerializeSigHash(payload), legacy);
}

using MutableTxResult = std::variant<CMutableTransaction, ProviderTxError>;
Expand Down Expand Up @@ -1282,7 +1282,7 @@ ProviderTxResult<SharedSignResult> SignShared(node::NodeContext& node, Wallet& w
const auto dmn{GetSharedMn(node, payload->proTxHash)};
if (!dmn) return SharedMnNotFound();
shares = dmn->pdmnState->shares;
sign_hash = ::SerializeHash(*payload);
sign_hash = ::SerializeSigHash(*payload);
result.kind = SharedSignResult::Kind::RegistrarUpdate;
} else {
return Error(ProviderTxErrorCode::INVALID_PARAMETER, "transaction is not a shared masternode transaction");
Expand Down Expand Up @@ -1500,7 +1500,7 @@ ProviderTxResult<PreparedSharedConsent> PrepareSharedRegistrarUpdate(node::NodeC
tx = std::get<CMutableTransaction>(std::move(funded_result));
UpdateInputsHash(tx, payload);
SetTxPayload(tx, payload);
return PreparedSharedConsent{MakeTransactionRef(std::move(tx)), ::SerializeHash(payload)};
return PreparedSharedConsent{MakeTransactionRef(std::move(tx)), ::SerializeSigHash(payload)};
}

} // namespace evo::provider
4 changes: 2 additions & 2 deletions src/evo/specialtx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

uint256 CalcTxInputsHash(const CTransaction& tx)
{
CHashWriter hw(SER_GETHASH, CLIENT_VERSION);
HashWriter hw{};
for (const auto& in : tx.vin) {
hw << in.prevout;
}
Expand All @@ -18,7 +18,7 @@ uint256 CalcTxInputsHash(const CTransaction& tx)

uint256 CalcTxOutputsHash(const CTransaction& tx)
{
CHashWriter hw(SER_GETHASH, CLIENT_VERSION);
HashWriter hw{};
for (const auto& out : tx.vout) {
hw << out;
}
Expand Down
8 changes: 4 additions & 4 deletions src/evo/specialtxman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ bool CSpecialTxProcessor::CheckCreditPoolDiffForBlock(const CBlock& block, const
template <typename ProTx>
static bool CheckHashSig(const ProTx& proTx, const PKHash& pkhash, TxValidationState& state)
{
if (std::string strError; !CHashSigner::VerifyHash(::SerializeHash(proTx), ToKeyID(pkhash), proTx.vchSig, strError)) {
if (std::string strError; !CHashSigner::VerifyHash(::SerializeSigHash(proTx), ToKeyID(pkhash), proTx.vchSig, strError)) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-sig");
}
return true;
Expand All @@ -1200,7 +1200,7 @@ static bool CheckStringSig(const ProTx& proTx, const PKHash& pkhash, TxValidatio
template <typename ProTx>
static bool CheckHashSig(const ProTx& proTx, const CBLSPublicKey& pubKey, TxValidationState& state)
{
if (!proTx.sig.VerifyInsecure(pubKey, ::SerializeHash(proTx))) {
if (!proTx.sig.VerifyInsecure(pubKey, ::SerializeSigHash(proTx))) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-sig");
}
return true;
Expand Down Expand Up @@ -1720,7 +1720,7 @@ bool CheckProUpShareTx(const CTransaction& tx, gsl::not_null<const CBlockIndex*>
}
if (check_sigs) {
if (std::string strError;
!CHashSigner::VerifyHashCanonical(::SerializeHash(*opt_ptx),
!CHashSigner::VerifyHashCanonical(::SerializeSigHash(*opt_ptx),
mnState.shares[opt_ptx->shareIndex].keyIDOwner, opt_ptx->vchSig,
strError)) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-proupshare-sig");
Expand Down Expand Up @@ -1775,7 +1775,7 @@ bool CheckProUpSharedRegTx(const CTransaction& tx, gsl::not_null<const CBlockInd
return false;
}
if (check_sigs) {
const uint256 payload_hash = ::SerializeHash(*opt_ptx);
const uint256 payload_hash = ::SerializeSigHash(*opt_ptx);
for (size_t i = 0; i < mnState.shares.size(); i++) {
if (std::string strError; !CHashSigner::VerifyHashCanonical(payload_hash, mnState.shares[i].keyIDOwner,
opt_ptx->vchSigs[i], strError)) {
Expand Down
2 changes: 1 addition & 1 deletion src/governance/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ uint256 Object::GetHash() const

// CREATE HASH OF ALL IMPORTANT PIECES OF DATA

CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
HashWriter ss{};
ss << hashParent;
ss << revision;
ss << time;
Expand Down
Loading
Loading