2020-04-17 19:33:06 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
2021-02-03 16:31:46 +01:00
|
|
|
* Copyright (C) 2020-2021 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/>.
|
|
|
|
|
*/
|
|
|
|
|
#ifndef BLOCKCHAIN_H
|
|
|
|
|
#define BLOCKCHAIN_H
|
|
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
#include <uint256.h>
|
|
|
|
|
|
2020-05-11 15:16:59 +02:00
|
|
|
#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);
|
2020-04-17 19:33:06 +02:00
|
|
|
|
|
|
|
|
Message createGetHeadersRequest(Streaming::P2PBuilder &builder);
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*
|
|
|
|
|
* Notice that after setting this, the save() will skip saving any heders provided by
|
|
|
|
|
* the static data.
|
2021-05-27 18:40:58 +02:00
|
|
|
*/
|
|
|
|
|
static void setStaticChain(const unsigned char *data, int64_t size);
|
|
|
|
|
|
2021-01-05 21:29:14 +01:00
|
|
|
/**
|
|
|
|
|
* Return the chain-height that we actually are at, based on validated headers.
|
|
|
|
|
*/
|
2020-05-11 18:07:10 +02:00
|
|
|
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;
|
|
|
|
|
|
2021-05-28 14:52:27 +02:00
|
|
|
/**
|
|
|
|
|
* 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;
|
2021-05-28 14:52:27 +02:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
|
2021-05-28 14:52:27 +02:00
|
|
|
/**
|
|
|
|
|
* This returns the block that was created just after the requested timestamp.
|
|
|
|
|
*
|
|
|
|
|
* If the timestamp is further in the future than 'Tip' then tip's height + 1 is given.
|
|
|
|
|
*/
|
|
|
|
|
int blockHeightAtTime(uint32_t timestamp) 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;
|
|
|
|
|
|
2020-05-11 15:16:59 +02:00
|
|
|
/// re-load the chain. Also called from the constructor.
|
|
|
|
|
void load();
|
|
|
|
|
/// save the chain
|
|
|
|
|
void save();
|
|
|
|
|
|
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();
|
2021-05-27 18:40:58 +02:00
|
|
|
void loadStaticChain(const unsigned char *data, int64_t dataSize);
|
2020-10-29 21:47:53 +01:00
|
|
|
|
|
|
|
|
void createGenericGenesis(BlockHeader genesis);
|
|
|
|
|
|
2020-04-17 19:33:06 +02:00
|
|
|
mutable std::mutex m_lock;
|
2020-05-11 15:16:59 +02:00
|
|
|
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;
|
2021-05-27 18:48:31 +02:00
|
|
|
bool m_needsSaving = false;
|
2020-04-17 19:33:06 +02:00
|
|
|
|
|
|
|
|
// consensus
|
|
|
|
|
const uint256 powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
|
|
|
std::map<int, uint256> checkpoints;
|
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
|
2020-04-17 19:33:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|