Skip to content

Commit

Permalink
refactor: CHashWriter, VectorWriter, CAutoFile, BufferedFile without …
Browse files Browse the repository at this point in the history
…nVersion
  • Loading branch information
lateminer committed May 16, 2024
1 parent 036566a commit af2480d
Show file tree
Hide file tree
Showing 54 changed files with 150 additions and 208 deletions.
4 changes: 2 additions & 2 deletions src/bench/checkblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

static void DeserializeBlockTest(benchmark::Bench& bench)
{
CDataStream stream(benchmark::data::block413567, SER_NETWORK, PROTOCOL_VERSION);
CDataStream stream(benchmark::data::block413567, SER_NETWORK);
std::byte a{0};
stream.write({&a, 1}); // Prevent compaction

Expand All @@ -33,7 +33,7 @@ static void DeserializeBlockTest(benchmark::Bench& bench)

static void DeserializeAndCheckBlockTest(benchmark::Bench& bench)
{
CDataStream stream(benchmark::data::block413567, SER_NETWORK, PROTOCOL_VERSION);
CDataStream stream(benchmark::data::block413567, SER_NETWORK);
std::byte a{0};
stream.write({&a, 1}); // Prevent compaction

Expand Down
2 changes: 1 addition & 1 deletion src/bench/load_external.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static void LoadExternalBlockFile(benchmark::Bench& bench)
bench.run([&] {
// "rb" is "binary, O_RDONLY", positioned to the start of the file.
// The file will be closed by LoadExternalBlockFile().
CAutoFile file{fsbridge::fopen(blkfile, "rb"), CLIENT_VERSION};
CAutoFile file{fsbridge::fopen(blkfile, "rb")};
testing_setup->m_node.chainman->LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);
});
fs::remove(blkfile);
Expand Down
2 changes: 1 addition & 1 deletion src/bench/rpc_blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct TestBlockAndIndex {

TestBlockAndIndex()
{
CDataStream stream(benchmark::data::block413567, SER_NETWORK, PROTOCOL_VERSION);
CDataStream stream(benchmark::data::block413567, SER_NETWORK);
std::byte a{0};
stream.write({&a, 1}); // Prevent compaction

Expand Down
1 change: 0 additions & 1 deletion src/bench/streams_findbyte.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <streams.h>
#include <util/fs.h>
#include <version.h> // for PROTOCOL_VERSION

#include <cstddef>
#include <cstdint>
Expand Down
2 changes: 1 addition & 1 deletion src/bench/verify_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static void VerifyScriptBench(benchmark::Bench& bench)
assert(success);

#if defined(HAVE_CONSENSUS_LIB)
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
CDataStream stream(SER_NETWORK);
stream << TX_WITH_WITNESS(txSpend);
int csuccess = bitcoinconsensus_verify_script_with_amount(
txCredit.vout[0].scriptPubKey.data(),
Expand Down
2 changes: 1 addition & 1 deletion src/bitcoin-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ static int Grind(const std::vector<std::string>& args, std::string& strPrint)
return EXIT_FAILURE;
}

CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
CDataStream ss(SER_NETWORK);
ss << header;
strPrint = HexStr(ss);
return EXIT_SUCCESS;
Expand Down
2 changes: 1 addition & 1 deletion src/blockencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CBlockHeaderAndShortTxIDs::CBlockHeaderAndShortTxIDs(const CBlock& block) :
}

void CBlockHeaderAndShortTxIDs::FillShortTxIDSelector() const {
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
CDataStream stream(SER_NETWORK);
stream << header << nonce;
CSHA256 hasher;
hasher.Write((unsigned char*)&(*stream.begin()), stream.end() - stream.begin());
Expand Down
4 changes: 2 additions & 2 deletions src/blockfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ GCSFilter::GCSFilter(const Params& params, const ElementSet& elements)
}
m_F = static_cast<uint64_t>(m_N) * static_cast<uint64_t>(m_params.m_M);

CVectorWriter stream(SER_NETWORK, GCS_SER_VERSION, m_encoded, 0);
VectorWriter stream(SER_NETWORK, m_encoded, 0);

WriteCompactSize(stream, m_N);

if (elements.empty()) {
return;
}

BitStreamWriter<CVectorWriter> bitwriter(stream);
BitStreamWriter<VectorWriter> bitwriter(stream);

uint64_t last_value = 0;
for (uint64_t value : BuildHashedSet(elements)) {
Expand Down
4 changes: 2 additions & 2 deletions src/core_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
if (!IsHex(hex_header)) return false;

const std::vector<unsigned char> header_data{ParseHex(hex_header)};
CDataStream ser_header(header_data, SER_NETWORK, PROTOCOL_VERSION);
CDataStream ser_header(header_data, SER_NETWORK);
try {
ser_header >> header;
} catch (const std::exception&) {
Expand All @@ -222,7 +222,7 @@ bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
return false;

std::vector<unsigned char> blockData(ParseHex(strHexBlk));
CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
CDataStream ssBlock(blockData, SER_NETWORK);
try {
ssBlock >> TX_WITH_WITNESS(block);
}
Expand Down
2 changes: 1 addition & 1 deletion src/external_signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ UniValue ExternalSigner::GetDescriptors(const int account)
bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::string& error)
{
// Serialize the PSBT
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
CDataStream ssTx(SER_NETWORK);
ssTx << psbtx;
// parse ExternalSigner master fingerprint
std::vector<unsigned char> parsed_m_fingerprint = ParseHex(m_fingerprint);
Expand Down
5 changes: 1 addition & 4 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,11 @@ class CHashWriter : public HashWriter
{
private:
const int nType;
const int nVersion;

public:
CHashWriter(int nVersionIn) : nType(SER_GETHASH), nVersion(nVersionIn) {}
CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
CHashWriter() : nType(SER_GETHASH) {}

int GetType() const { return nType; }
int GetVersion() const { return nVersion; }

template<typename T>
CHashWriter& operator<<(const T& obj) {
Expand Down
20 changes: 10 additions & 10 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,8 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
return true;
}

V1Transport::V1Transport(const NodeId node_id, int nTypeIn, int nVersionIn) noexcept :
m_node_id(node_id), hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn)
V1Transport::V1Transport(const NodeId node_id, int nTypeIn) noexcept :
m_node_id(node_id), hdrbuf(nTypeIn), vRecv(nTypeIn)
{
assert(std::size(Params().MessageStart()) == std::size(m_magic_bytes));
m_magic_bytes = Params().MessageStart();
Expand Down Expand Up @@ -816,7 +816,7 @@ bool V1Transport::SetMessageToSend(CSerializedNetMsg& msg) noexcept

// serialize header
m_header_to_send.clear();
CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, m_header_to_send, 0, hdr};
VectorWriter{SER_NETWORK, m_header_to_send, 0, hdr};

// update state
m_message_to_send = std::move(msg);
Expand Down Expand Up @@ -966,9 +966,9 @@ void V2Transport::StartSendingHandshake() noexcept
// We cannot wipe m_send_garbage as it will still be used as AAD later in the handshake.
}

V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, int version_in, const CKey& key, Span<const std::byte> ent32, std::vector<uint8_t> garbage) noexcept :
V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, const CKey& key, Span<const std::byte> ent32, std::vector<uint8_t> garbage) noexcept :
m_cipher{key, ent32}, m_initiating{initiating}, m_nodeid{nodeid},
m_v1_fallback{nodeid, type_in, version_in}, m_recv_type{type_in}, m_recv_version{version_in},
m_v1_fallback{nodeid, type_in}, m_recv_type{type_in},
m_recv_state{initiating ? RecvState::KEY : RecvState::KEY_MAYBE_V1},
m_send_garbage{std::move(garbage)},
m_send_state{initiating ? SendState::AWAITING_KEY : SendState::MAYBE_V1}
Expand All @@ -981,8 +981,8 @@ V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, int versio
}
}

V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in, int version_in) noexcept :
V2Transport{nodeid, initiating, type_in, version_in, GenerateRandomKey(),
V2Transport::V2Transport(NodeId nodeid, bool initiating, int type_in) noexcept :
V2Transport{nodeid, initiating, type_in, GenerateRandomKey(),
MakeByteSpan(GetRandHash()), GenerateRandomGarbage()} { }

void V2Transport::SetReceiveState(RecvState recv_state) noexcept
Expand Down Expand Up @@ -1427,7 +1427,7 @@ CNetMessage V2Transport::GetReceivedMessage(std::chrono::microseconds time, bool
Assume(m_recv_state == RecvState::APP_READY);
Span<const uint8_t> contents{m_recv_decode_buffer};
auto msg_type = GetMessageType(contents);
CDataStream ret(m_recv_type, m_recv_version);
CDataStream ret(m_recv_type);
CNetMessage msg{std::move(ret)};
// Note that BIP324Cipher::EXPANSION also includes the length descriptor size.
msg.m_raw_message_size = m_recv_decode_buffer.size() + BIP324Cipher::EXPANSION;
Expand Down Expand Up @@ -3660,9 +3660,9 @@ ServiceFlags CConnman::GetLocalServices() const
static std::unique_ptr<Transport> MakeTransport(NodeId id, bool use_v2transport, bool inbound) noexcept
{
if (use_v2transport) {
return std::make_unique<V2Transport>(id, /*initiating=*/!inbound, SER_NETWORK, INIT_PROTO_VERSION);
return std::make_unique<V2Transport>(id, /*initiating=*/!inbound, SER_NETWORK);
} else {
return std::make_unique<V1Transport>(id, SER_NETWORK, INIT_PROTO_VERSION);
return std::make_unique<V1Transport>(id, SER_NETWORK);
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ class V1Transport final : public Transport
size_t m_bytes_sent GUARDED_BY(m_send_mutex) {0};

public:
V1Transport(const NodeId node_id, int nTypeIn, int nVersionIn) noexcept;
V1Transport(const NodeId node_id, int nTypeIn) noexcept;

bool ReceivedMessageComplete() const override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
{
Expand Down Expand Up @@ -599,8 +599,6 @@ class V2Transport final : public Transport
std::vector<uint8_t> m_recv_decode_buffer GUARDED_BY(m_recv_mutex);
/** Deserialization type. */
const int m_recv_type;
/** Deserialization version number. */
const int m_recv_version;
/** Current receiver state. */
RecvState m_recv_state GUARDED_BY(m_recv_mutex);

Expand Down Expand Up @@ -649,10 +647,10 @@ class V2Transport final : public Transport
* @param[in] type_in the serialization type of returned CNetMessages.
* @param[in] version_in the serialization version of returned CNetMessages.
*/
V2Transport(NodeId nodeid, bool initiating, int type_in, int version_in) noexcept;
V2Transport(NodeId nodeid, bool initiating, int type_in) noexcept;

/** Construct a V2 transport with specified keys and garbage (test use only). */
V2Transport(NodeId nodeid, bool initiating, int type_in, int version_in, const CKey& key, Span<const std::byte> ent32, std::vector<uint8_t> garbage) noexcept;
V2Transport(NodeId nodeid, bool initiating, int type_in, const CKey& key, Span<const std::byte> ent32, std::vector<uint8_t> garbage) noexcept;

// Receive side functions.
bool ReceivedMessageComplete() const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex);
Expand Down
2 changes: 1 addition & 1 deletion src/netmessagemaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CNetMsgMaker
msg.m_type = std::move(msg_type);
// Blackcoin ToDo: revert after nodes upgrade to current version
int32_t serModes = nVersion <= OLD_VERSION ? SER_NETWORK : SER_NETWORK | SER_POSMARKER;
CVectorWriter{serModes, nFlags | nVersion, msg.data, 0, std::forward<Args>(args)...};
VectorWriter{serModes, msg.data, 0, std::forward<Args>(args)...};
return msg;
}

Expand Down
6 changes: 3 additions & 3 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,13 +636,13 @@ FlatFileSeq BlockManager::UndoFileSeq() const

CAutoFile BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
{
return CAutoFile{BlockFileSeq().Open(pos, fReadOnly), CLIENT_VERSION};
return CAutoFile{BlockFileSeq().Open(pos, fReadOnly)};
}

/** Open an undo file (rev?????.dat) */
CAutoFile BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
{
return CAutoFile{UndoFileSeq().Open(pos, fReadOnly), CLIENT_VERSION};
return CAutoFile{UndoFileSeq().Open(pos, fReadOnly)};
}

fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
Expand Down Expand Up @@ -991,7 +991,7 @@ void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFile

// -loadblock=
for (const fs::path& path : vImportFiles) {
CAutoFile file{fsbridge::fopen(path, "rb"), CLIENT_VERSION};
CAutoFile file{fsbridge::fopen(path, "rb")};
if (!file.IsNull()) {
LogPrintf("Importing blocks file %s...\n", fs::PathToString(path));
chainman.LoadExternalBlockFile(file);
Expand Down
4 changes: 2 additions & 2 deletions src/pos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ uint256 ComputeStakeModifier(const CBlockIndex* pindexPrev, const uint256& kerne
if (!pindexPrev)
return uint256(); // genesis block's modifier is 0

CHashWriter ss(SER_GETHASH, 0);
CHashWriter ss{};
ss << kernel << pindexPrev->nStakeModifier;
return ss.GetHash();
}
Expand Down Expand Up @@ -93,7 +93,7 @@ bool CheckStakeKernelHash(const CBlockIndex* pindexPrev, unsigned int nBits, uin
uint256 nStakeModifier = pindexPrev->nStakeModifier;

// Calculate hash
CHashWriter ss(SER_GETHASH, 0);
CHashWriter ss{};
ss << nStakeModifier;
ss << blockFromTime << prevout.hash << prevout.n << nTimeTx;

Expand Down
2 changes: 1 addition & 1 deletion src/primitives/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
uint256 CBlockHeader::GetHash() const
{
if (nVersion > 6)
return (CHashWriter{PROTOCOL_VERSION} << *this).GetHash();
return (CHashWriter{} << *this).GetHash();
return GetPoWHash();
}

Expand Down
2 changes: 1 addition & 1 deletion src/psbt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base6

bool DecodeRawPSBT(PartiallySignedTransaction& psbt, Span<const std::byte> tx_data, std::string& error)
{
CDataStream ss_data(tx_data, SER_NETWORK, PROTOCOL_VERSION);
CDataStream ss_data(tx_data, SER_NETWORK);
try {
ss_data >> psbt;
if (!ss_data.empty()) {
Expand Down
6 changes: 3 additions & 3 deletions src/psbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ struct PSBTInput
const auto& [leaf_hashes, origin] = leaf_origin;
SerializeToVector(s, PSBT_IN_TAP_BIP32_DERIVATION, xonly);
std::vector<unsigned char> value;
CVectorWriter s_value{s.GetType(), s.GetVersion(), value, 0};
VectorWriter s_value{s.GetType(), value, 0};
s_value << leaf_hashes;
SerializeKeyOrigin(s_value, origin);
s << value;
Expand Down Expand Up @@ -757,7 +757,7 @@ struct PSBTOutput
if (!m_tap_tree.empty()) {
SerializeToVector(s, PSBT_OUT_TAP_TREE);
std::vector<unsigned char> value;
CVectorWriter s_value{s.GetType(), s.GetVersion(), value, 0};
VectorWriter s_value{s.GetType(), value, 0};
for (const auto& [depth, leaf_ver, script] : m_tap_tree) {
s_value << depth;
s_value << leaf_ver;
Expand All @@ -771,7 +771,7 @@ struct PSBTOutput
const auto& [leaf_hashes, origin] = leaf;
SerializeToVector(s, PSBT_OUT_TAP_BIP32_DERIVATION, xonly);
std::vector<unsigned char> value;
CVectorWriter s_value{s.GetType(), s.GetVersion(), value, 0};
VectorWriter s_value{s.GetType(), value, 0};
s_value << leaf_hashes;
SerializeKeyOrigin(s_value, origin);
s << value;
Expand Down
4 changes: 2 additions & 2 deletions src/qt/psbtoperationsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ void PSBTOperationsDialog::broadcastTransaction()
}

void PSBTOperationsDialog::copyToClipboard() {
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
CDataStream ssTx(SER_NETWORK);
ssTx << m_transaction_data;
GUIUtil::setClipboard(EncodeBase64(ssTx.str()).c_str());
showStatus(tr("PSBT copied to clipboard."), StatusLevel::INFO);
}

void PSBTOperationsDialog::saveTransaction() {
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
CDataStream ssTx(SER_NETWORK);
ssTx << m_transaction_data;

QString selected_filter;
Expand Down
2 changes: 1 addition & 1 deletion src/qt/sendcoinsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ bool SendCoinsDialog::PrepareSendText(QString& question_string, QString& informa
void SendCoinsDialog::presentPSBT(PartiallySignedTransaction& psbtx)
{
// Serialize the PSBT
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
CDataStream ssTx(SER_NETWORK);
ssTx << psbtx;
GUIUtil::setClipboard(EncodeBase64(ssTx.str()).c_str());
QMessageBox msgBox(this);
Expand Down
Loading

0 comments on commit af2480d

Please sign in to comment.