Files

221 lines
7.8 KiB
C++
Raw Permalink Normal View History

2020-04-17 19:33:06 +02:00
/*
* This file is part of the Flowee project
2026-04-09 18:56:13 +02:00
* Copyright (C) 2020-2026 Tom Zander <tom@flowee.org>
2020-04-17 19:33:06 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-01-22 19:32:15 +01:00
#ifndef FLOWEE_BLOCKCHAIN_H
#define FLOWEE_BLOCKCHAIN_H
2020-04-17 19:33:06 +02:00
#include "BlockHeader.h"
2020-10-29 21:47:53 +01:00
#include "P2PNet.h"
2020-04-17 19:33:06 +02:00
#include <Message.h>
#include <arith_uint256.h>
2026-04-09 18:56:13 +02:00
#include <boost/asio/steady_timer.hpp>
2020-04-17 19:33:06 +02:00
#include <uint256.h>
#include <boost/filesystem.hpp>
2020-04-17 19:33:06 +02:00
#include <mutex>
2021-02-03 16:31:46 +01:00
#include <unordered_map>
2020-04-17 19:33:06 +02:00
class DownloadManager;
2020-05-09 19:58:44 +02:00
namespace Streaming {
class P2PBuilder;
}
2020-04-17 19:33:06 +02:00
class Blockchain
{
public:
2020-10-29 21:47:53 +01:00
Blockchain(DownloadManager *downloadManager, const boost::filesystem::path &basedir, P2PNet::Chain chain);
/**
* The p2p message 'getheaders' asks the remote peer to identify and append information
* on blockchain information. This call generates a current message requesting extra
* headers we don't know about yet.
*
* Notice that if we are pretty sure we are at the tip, this request is legally answerable
* with a one-byte message. Namely stating that it knows no headers we know.
* As a result it may be useful to provide skipHeaders with a low number that will then
* trigger the responding peer to actually provide that number of headers since we pretend
* we don't know them.
*
* @param skipHeaders if non-zero, effectively ask the
* peer to provide that number of headers.
*/
Message createGetHeadersRequest(int skipHeaders = 0);
2020-04-17 19:33:06 +02:00
void processBlockHeaders(Message message, int peerId);
2021-05-27 18:40:58 +02:00
/**
2021-05-28 12:42:30 +02:00
* Set a raw mapped blockchain data, a simple list of headers.
* Block headers, as they are stored in the blockchain, at 80 bytes each can be provided
* here as a lost in order to avoid downloading them from peers.
* We require that the blocks are in-order (will not be checked), starting with the
* geneesis block (which is checked).
2021-05-27 18:40:58 +02:00
*
2021-05-28 12:42:30 +02:00
* This setter should be used BEFORE the creation of the Blockchain instance, which means
* before the creation of the DownloadManager instance.
*
2022-01-12 19:45:51 +01:00
* Notice that after setting this, the save() will skip saving any headers provided by
2021-05-28 12:42:30 +02:00
* the static data.
2021-05-27 18:40:58 +02:00
*/
2022-01-12 19:45:51 +01:00
static void setStaticChain(const unsigned char *data, int64_t size, const std::string &infoFile);
/**
* In order to promote a standard blockheaders file to be useful as a static chain,
* we need to create an extra metadata file by doing the expensive operations on them
* and saving the results.
* This method is meant to create the file 'meta' as a result of the input 'blockchain' file.
*/
static bool createStaticHeaders(const std::string &blockchain, const std::string &meta);
2021-05-27 18:40:58 +02:00
/**
* This is a specialized function that allows a checkpoint based static chain (so
* not starting at genesis) to be extended back in time. To an earlier checkpoint.
*/
void replaceStaticChain(const unsigned char *data, int64_t size, const std::string &infoFile);
2021-01-05 21:29:14 +01:00
/**
* Return the chain-height that we actually are at, based on validated headers.
*/
int height() const;
2021-01-05 21:29:14 +01:00
/**
* Return the chain-height that based on the date/time we expect to be at.
*/
2020-04-17 19:33:06 +02:00
int expectedBlockHeight() const;
/**
* Returns true if the block-id is part of the main chain (so far).
*/
2020-04-17 19:33:06 +02:00
bool isKnown(const uint256 &blockId) const;
/**
* Returns the block height for a certain block.
* Please notice the isKnown which is a cheaper version if all you need is a yes/no.
*/
2020-04-17 19:33:06 +02:00
int blockHeightFor(const uint256 &blockId) const;
/**
* This returns the block that was created just after the requested timestamp.
*
* If the timestamp is further in the future then 'Tip' then tip's height + 1 is given.
* If the timestamp is before oldestKnownBlockHeight's time, we return that height.
*/
int blockHeightAtTime(uint32_t timestamp) const;
/// In case of starting from a checkpoint, this returns the checkpoint height. Otherwise 0
int oldestKnownBlockHeight() const;
2021-05-27 18:40:58 +02:00
/**
* Return the block header for a block at a certain height.
* Height 0 is the genesis block.
*/
2020-04-17 19:33:06 +02:00
BlockHeader block(int height) const;
/// re-load the chain. Also called from the constructor.
void load();
/// save the chain
void save();
2024-05-31 13:05:38 +02:00
/// The origin of header data.
2024-05-28 10:20:42 +02:00
enum DataSourceType {
2024-05-31 13:05:38 +02:00
StaticHeadersSource, ///< From a Flowee provided file.
DataFileSource ///< From the Internet.
2024-05-28 10:20:42 +02:00
};
struct DataSource {
DataSourceType source;
int startBlock = 0;
int endBlock = 0;
};
2024-05-31 13:05:38 +02:00
/**
* Return the specific sources of our blockchain-data.
* This exports the explicit amount of block-headers available and their source.
*/
2024-05-28 10:20:42 +02:00
std::vector<DataSource> dataSources() const;
2024-05-31 13:05:38 +02:00
/**
* A checkpoint.
* This is a historically significant point in the blockchain that has happened
* far enough in the past to simply ship this in the sources of Flowee and
* that way have certainty that the publicly available data matches what we
* expect.
*/
struct CheckPoint {
int height = 0;
uint256 hash;
arith_uint256 chainWork;
};
/**
* Return the known checkpoints.
*/
std::vector<CheckPoint> checkpoints() const;
2020-04-17 19:33:06 +02:00
private:
2020-10-29 21:47:53 +01:00
void createMainchainGenesis();
void loadMainchainCheckpoints();
void createTestnet4Genesis();
void loadTestnet4Checkpoints();
2022-01-12 19:45:51 +01:00
void loadStaticChain(const unsigned char *data, int64_t dataSize, std::ifstream &infoFile);
2020-10-29 21:47:53 +01:00
void createGenericGenesis(BlockHeader genesis);
/// replace all the saved files with one that has all the data.
void compressSaveFiles(const boost::system::error_code &error);
void startSaveTimer();
2020-10-29 21:47:53 +01:00
2023-10-04 19:22:17 +02:00
/**
* Load a file that has no index (meta file) by actually parsing and doing math
* on each individual header.
* It also automatically creates a meta file for faster load next time, if the
* \a 'meta' is not empty.
*/
void loadSlow(std::ifstream &in, const std::string &meta);
2023-10-04 19:22:17 +02:00
// helper method to access m_longestChain
BlockHeader blockAt_priv(size_t i) const;
// helper method to access m_longestChain
int blockHeight_priv() const;
// private helper function to determine how many block-headers we should skip loading for a file starting with \a bh
int skipBlocksOfFile(const BlockHeader &bh) const;
2023-10-04 19:22:17 +02:00
mutable std::recursive_mutex m_lock;
const boost::filesystem::path m_basedir;
2020-04-17 19:33:06 +02:00
std::vector<BlockHeader> m_longestChain;
struct ChainTip {
uint256 tip;
int height;
arith_uint256 chainWork;
};
ChainTip m_tip;
2021-02-03 16:31:46 +01:00
typedef std::unordered_map<uint256, int, HashShortener, HashComparison> BlockHeightMap;
2020-04-17 19:33:06 +02:00
BlockHeightMap m_blockHeight;
DownloadManager *m_dlmanager;
2026-04-09 18:56:13 +02:00
boost::asio::steady_timer m_saveTimer;
bool m_saveTimerStarted = false;
2020-04-17 19:33:06 +02:00
// consensus
const uint256 powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
2024-05-31 13:05:38 +02:00
std::vector<CheckPoint> m_checkpoints;
2021-05-27 18:40:58 +02:00
int m_chainStartPoint = 0;
2021-05-27 18:40:58 +02:00
const unsigned char *m_staticChain = nullptr;
int m_numStaticHeaders = 0; // including genesis, so this is height + 1
int m_fileCount = 0; // number of 'blockchainN' files we own
int m_lastSavedHeader = 0;
2020-04-17 19:33:06 +02:00
};
#endif