Skip to content

Commit

Permalink
Move CDiskBlockPos from chain to flatfile.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimpo authored and furszy committed May 15, 2021
1 parent 7fa47bb commit 472857a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
42 changes: 1 addition & 41 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define BITCOIN_CHAIN_H

#include "chainparams.h"
#include "flatfile.h"
#include "optional.h"
#include "pow.h"
#include "primitives/block.h"
Expand Down Expand Up @@ -87,47 +88,6 @@ class CBlockFileInfo
}
};

struct CDiskBlockPos {
int nFile;
unsigned int nPos;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(VARINT(nFile, VarIntMode::NONNEGATIVE_SIGNED));
READWRITE(VARINT(nPos));
}

CDiskBlockPos()
{
SetNull();
}

CDiskBlockPos(int nFileIn, unsigned int nPosIn)
{
nFile = nFileIn;
nPos = nPosIn;
}

friend bool operator==(const CDiskBlockPos& a, const CDiskBlockPos& b)
{
return (a.nFile == b.nFile && a.nPos == b.nPos);
}

friend bool operator!=(const CDiskBlockPos& a, const CDiskBlockPos& b)
{
return !(a == b);
}

void SetNull()
{
nFile = -1;
nPos = 0;
}
bool IsNull() const { return (nFile == -1); }
};

enum BlockStatus {
//! Unused.
BLOCK_VALID_UNKNOWN = 0,
Expand Down
8 changes: 7 additions & 1 deletion src/flatfile.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand All @@ -19,6 +20,11 @@ FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) :
}
}

std::string CDiskBlockPos::ToString() const
{
return strprintf("CDiskBlockPos(nFile=%i, nPos=%i)", nFile, nPos);
}

fs::path FlatFileSeq::FileName(const CDiskBlockPos& pos) const
{
return m_dir / strprintf("%s%05u.dat", m_prefix, pos.nFile);
Expand Down
43 changes: 41 additions & 2 deletions src/flatfile.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_FLATFILE_H
#define BITCOIN_FLATFILE_H

#include "chain.h"
#include <string>

#include "fs.h"
#include "serialize.h"

struct CDiskBlockPos
{
int nFile;
unsigned int nPos;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(VARINT(nFile, VarIntMode::NONNEGATIVE_SIGNED));
READWRITE(VARINT(nPos));
}

CDiskBlockPos() {
SetNull();
}

CDiskBlockPos(int nFileIn, unsigned int nPosIn) {
nFile = nFileIn;
nPos = nPosIn;
}

friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) {
return (a.nFile == b.nFile && a.nPos == b.nPos);
}

friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) {
return !(a == b);
}

void SetNull() { nFile = -1; nPos = 0; }
bool IsNull() const { return (nFile == -1); }

std::string ToString() const;
};

/**
* FlatFileSeq represents a sequence of numbered files storing raw data. This class facilitates
Expand Down

0 comments on commit 472857a

Please sign in to comment.