Skip to content

Commit

Permalink
Convert CDiskBlockIndex and CBlockLocator to SerParams
Browse files Browse the repository at this point in the history
  • Loading branch information
theuni committed Sep 7, 2023
1 parent 8e0d979 commit 3e3af45
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 20 deletions.
18 changes: 15 additions & 3 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,17 @@ const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex*
class CDiskBlockIndex : public CBlockIndex
{
public:

enum class hashing_type
{
no,
yes
};
struct SerParams {
const hashing_type value;
bool is_hashing() const { return value == hashing_type::yes; }
};

uint256 hashPrev;

CDiskBlockIndex()
Expand All @@ -401,11 +412,12 @@ class CDiskBlockIndex : public CBlockIndex
hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
}

SERIALIZE_METHODS(CDiskBlockIndex, obj)
SERIALIZE_METHODS_PARAMS(CDiskBlockIndex, obj, SerParams, params)
{
LOCK(::cs_main);
int _nVersion = s.GetVersion();
if (!(s.GetType() & SER_GETHASH)) READWRITE(VARINT_MODE(_nVersion, VarIntMode::NONNEGATIVE_SIGNED));
int _nVersion = 0;
if (!params.is_hashing())
READWRITE(VARINT_MODE(_nVersion, VarIntMode::NONNEGATIVE_SIGNED));

READWRITE(VARINT_MODE(obj.nHeight, VarIntMode::NONNEGATIVE_SIGNED));
READWRITE(VARINT(obj.nStatus));
Expand Down
4 changes: 2 additions & 2 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,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 @@ -229,7 +229,7 @@ class CDBWrapper
return false;
}
try {
CDataStream ssValue{MakeByteSpan(*strValue), SER_DISK, CLIENT_VERSION};
DataStream ssValue{MakeByteSpan(*strValue)};
ssValue.Xor(obfuscate_key);
ssValue >> value;
} catch (const std::exception&) {
Expand Down
7 changes: 5 additions & 2 deletions src/index/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool

bool BaseIndex::DB::ReadBestBlock(CBlockLocator& locator) const
{
bool success = Read(DB_BEST_BLOCK, locator);
const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no};
auto wrapper = WithParams(ser_params, locator);
bool success = Read(DB_BEST_BLOCK, wrapper);
if (!success) {
locator.SetNull();
}
Expand All @@ -65,7 +67,8 @@ bool BaseIndex::DB::ReadBestBlock(CBlockLocator& locator) const

void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator)
{
batch.Write(DB_BEST_BLOCK, locator);
const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no};
batch.Write(DB_BEST_BLOCK, WithParams(ser_params, locator));
}

BaseIndex::BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name)
Expand Down
9 changes: 6 additions & 3 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2627,7 +2627,8 @@ bool PeerManagerImpl::MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& loc
// Only allow a new getheaders message to go out if we don't have a recent
// one already in-flight
if (current_time - peer.m_last_getheaders_timestamp > HEADERS_RESPONSE_TIME) {
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, locator, uint256()));
const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no};
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, WithParams(ser_params, locator), uint256()));
peer.m_last_getheaders_timestamp = current_time;
return true;
}
Expand Down Expand Up @@ -3886,7 +3887,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
if (msg_type == NetMsgType::GETBLOCKS) {
CBlockLocator locator;
uint256 hashStop;
vRecv >> locator >> hashStop;
const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no};
vRecv >> WithParams(ser_params, locator) >> hashStop;

if (locator.vHave.size() > MAX_LOCATOR_SZ) {
LogPrint(BCLog::NET, "getblocks locator size %lld > %d, disconnect peer=%d\n", locator.vHave.size(), MAX_LOCATOR_SZ, pfrom.GetId());
Expand Down Expand Up @@ -4001,7 +4003,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
if (msg_type == NetMsgType::GETHEADERS) {
CBlockLocator locator;
uint256 hashStop;
vRecv >> locator >> hashStop;
const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no};
vRecv >> WithParams(ser_params, locator) >> hashStop;

if (locator.vHave.size() > MAX_LOCATOR_SZ) {
LogPrint(BCLog::NET, "getheaders locator size %lld > %d, disconnect peer=%d\n", locator.vHave.size(), MAX_LOCATOR_SZ, pfrom.GetId());
Expand Down
8 changes: 6 additions & 2 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ bool BlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFi
batch.Write(std::make_pair(DB_BLOCK_FILES, file), *info);
}
batch.Write(DB_LAST_BLOCK, nLastFile);
const CDiskBlockIndex::SerParams ser_params{CDiskBlockIndex::hashing_type::no};
for (const CBlockIndex* bi : blockinfo) {
batch.Write(std::make_pair(DB_BLOCK_INDEX, bi->GetBlockHash()), CDiskBlockIndex{bi});
auto wrapper = WithParams(ser_params, CDiskBlockIndex{bi});
batch.Write(std::make_pair(DB_BLOCK_INDEX, bi->GetBlockHash()), wrapper);
}
return WriteBatch(batch, true);
}
Expand Down Expand Up @@ -102,7 +104,9 @@ bool BlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, s
std::pair<uint8_t, uint256> key;
if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
CDiskBlockIndex diskindex;
if (pcursor->GetValue(diskindex)) {
const CDiskBlockIndex::SerParams ser_params{CDiskBlockIndex::hashing_type::no};
auto wrapper = WithParams(ser_params, diskindex);
if (pcursor->GetValue(wrapper)) {
// Construct block index object
CBlockIndex* pindexNew = insertBlockIndex(diskindex.ConstructBlockHash());
pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
Expand Down
16 changes: 13 additions & 3 deletions src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,26 @@ class CBlock : public CBlockHeader
*/
struct CBlockLocator
{
enum class hashing_type
{
no,
yes
};
struct SerParams {
const hashing_type value;
bool is_hashing() const { return value == hashing_type::yes; }
};

std::vector<uint256> vHave;

CBlockLocator() {}

explicit CBlockLocator(std::vector<uint256>&& have) : vHave(std::move(have)) {}

SERIALIZE_METHODS(CBlockLocator, obj)
SERIALIZE_METHODS_PARAMS(CBlockLocator, obj, SerParams, params)
{
int nVersion = s.GetVersion();
if (!(s.GetType() & SER_GETHASH))
int nVersion = 0;
if (!params.is_hashing())
READWRITE(nVersion);
READWRITE(obj.vHave);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/fuzz/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ template <typename T>
[[nodiscard]] inline std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
{
const std::vector<uint8_t> buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
CDataStream ds{buffer, SER_NETWORK, INIT_PROTO_VERSION};
DataStream ds{buffer};
T obj;
try {
ds >> obj;
Expand Down
11 changes: 7 additions & 4 deletions src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,17 @@ bool WalletBatch::EraseWatchOnly(const CScript &dest)

bool WalletBatch::WriteBestBlock(const CBlockLocator& locator)
{
WriteIC(DBKeys::BESTBLOCK, CBlockLocator()); // Write empty block locator so versions that require a merkle branch automatically rescan
return WriteIC(DBKeys::BESTBLOCK_NOMERKLE, locator);
const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no};
WriteIC(DBKeys::BESTBLOCK, WithParams(ser_params, CBlockLocator())); // Write empty block locator so versions that require a merkle branch automatically rescan
return WriteIC(DBKeys::BESTBLOCK_NOMERKLE, WithParams(ser_params, locator));
}

bool WalletBatch::ReadBestBlock(CBlockLocator& locator)
{
if (m_batch->Read(DBKeys::BESTBLOCK, locator) && !locator.vHave.empty()) return true;
return m_batch->Read(DBKeys::BESTBLOCK_NOMERKLE, locator);
const CBlockLocator::SerParams ser_params{CBlockLocator::hashing_type::no};
auto wrapper = WithParams(ser_params, locator);
if (m_batch->Read(DBKeys::BESTBLOCK, wrapper) && !locator.vHave.empty()) return true;
return m_batch->Read(DBKeys::BESTBLOCK_NOMERKLE, wrapper);
}

bool WalletBatch::WriteOrderPosNext(int64_t nOrderPosNext)
Expand Down

0 comments on commit 3e3af45

Please sign in to comment.