Skip to content

Commit

Permalink
Port init
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Mar 6, 2024
1 parent 118db1f commit dd2b435
Show file tree
Hide file tree
Showing 15 changed files with 687 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ BITCOIN_CORE_H = \
qtum/qtumDGP.h \
qtum/storageresults.h \
qtum/qtumutils.h \
qtum/qtumdelegation.h \
qtum/qtumtoken.h \
qtum/qtumledger.h \
qtum/delegationutils.h
Expand Down Expand Up @@ -740,6 +741,7 @@ libbitcoin_common_a_SOURCES = \
qtum/qtumutils.cpp \
qtum/qtumDGP.cpp \
qtum/qtumtoken.cpp \
qtum/qtumdelegation.cpp \
qtum/delegationutils.cpp \
util/contractabi.cpp \
libff/libff/algebra/curves/public_params.hpp \
Expand Down
5 changes: 5 additions & 0 deletions src/addresstype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,8 @@ CScript GetScriptForDestination(const CTxDestination& dest)
bool IsValidDestination(const CTxDestination& dest) {
return std::visit(ValidDestinationVisitor(), dest);
}

PKHash ExtractPublicKeyHash(const CScript& scriptPubKey, bool* OK)
{
return {};
}
2 changes: 2 additions & 0 deletions src/addresstype.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,6 @@ bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
*/
CScript GetScriptForDestination(const CTxDestination& dest);

PKHash ExtractPublicKeyHash(const CScript& scriptPubKey, bool* OK = nullptr);

#endif // BITCOIN_ADDRESSTYPE_H
348 changes: 342 additions & 6 deletions src/init.cpp

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,7 @@ void SetupServerArgs(ArgsManager& argsman);
/** Validates requirements to run the indexes and spawns each index initial sync thread */
bool StartIndexBackgroundSync(node::NodeContext& node);

/** Unlock the data directory */
void UnlockDataDirectory();

#endif // BITCOIN_INIT_H
22 changes: 22 additions & 0 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ class PeerManagerImpl final : public PeerManager
PeerManagerImpl(CConnman& connman, AddrMan& addrman,
BanMan* banman, ChainstateManager& chainman,
CTxMemPool& pool, Options opts);
~PeerManagerImpl();

/** Overridden from CValidationInterface. */
void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override
Expand Down Expand Up @@ -518,6 +519,8 @@ class PeerManagerImpl final : public PeerManager
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_recent_confirmed_transactions_mutex, !m_most_recent_block_mutex, !m_headers_presync_mutex, g_msgproc_mutex);
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) override;
void InitCleanBlockIndex() override;
void StopCleanBlockIndex() override;

private:
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
Expand Down Expand Up @@ -1912,6 +1915,12 @@ void PeerManagerImpl::StartScheduledTasks(CScheduler& scheduler)
scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
}

PeerManagerImpl::~PeerManagerImpl()
{
// Stop clean block index thread
StopCleanBlockIndex();
}

/**
* Evict orphan txn pool entries based on a newly connected
* block, remember the recently confirmed transactions, and delete tracked
Expand Down Expand Up @@ -5995,3 +6004,16 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
MaybeSendFeefilter(*pto, *peer, current_time);
return true;
}

void PeerManagerImpl::InitCleanBlockIndex()
{
}

void PeerManagerImpl::StopCleanBlockIndex()
{
}

unsigned int GefaultHeaderSpamFilterMaxSize()
{
return Params().GetConsensus().MaxCheckpointSpan();
}
25 changes: 25 additions & 0 deletions src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ static const int DISCOURAGEMENT_THRESHOLD{100};
/** Maximum number of outstanding CMPCTBLOCK requests for the same block. */
static const unsigned int MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK = 3;

/** Default maximum orphan blocks */
static const unsigned int DEFAULT_MAX_ORPHAN_BLOCKS = 40;
/** Default for -headerspamfilter, use header spam filter */
static const bool DEFAULT_HEADER_SPAM_FILTER = true;
/** Default for -headerspamfiltermaxsize, maximum size of the list of indexes in the header spam filter */
static const unsigned int DEFAULT_HEADER_SPAM_FILTER_MAX_SIZE = 2000;
/** Default for -headerspamfiltermaxavg, maximum average size of an index occurrence in the header spam filter */
static const unsigned int DEFAULT_HEADER_SPAM_FILTER_MAX_AVG = 10;
/** Default for -headerspamfilterignoreport, ignore the port in the ip address when looking for header spam,
multiple nodes on the same ip will be treated as the one when computing the filter*/
static const unsigned int DEFAULT_HEADER_SPAM_FILTER_IGNORE_PORT = true;
/** Default for -cleanblockindex. */
static const bool DEFAULT_CLEANBLOCKINDEX = true;
/** Default for -cleanblockindextimeout. */
static const unsigned int DEFAULT_CLEANBLOCKINDEXTIMEOUT = 600;

struct CNodeStateStats {
int nSyncHeight = -1;
int nCommonHeight = -1;
Expand Down Expand Up @@ -110,6 +126,15 @@ class PeerManager : public CValidationInterface, public NetEventsInterface

/** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */
virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0;

/** Initialize clean block index */
virtual void InitCleanBlockIndex() = 0;

/** Stop clean block index thread */
virtual void StopCleanBlockIndex() = 0;
};

/** Default for -headerspamfiltermaxsize, maximum size of the list of indexes in the header spam filter */
unsigned int GefaultHeaderSpamFilterMaxSize();

#endif // BITCOIN_NET_PROCESSING_H
65 changes: 65 additions & 0 deletions src/node/chainstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <util/time.h>
#include <util/translation.h>
#include <validation.h>
#include <chainparams.h>
#include <common/args.h>

#include <algorithm>
#include <atomic>
Expand All @@ -40,6 +42,9 @@ static ChainstateLoadResult CompleteChainstateInitialization(
// new BlockTreeDB tries to delete the existing file, which
// fails if it's still open from the previous loop. Close it first:
pblocktree.reset();
pstorageresult.reset();
globalState.reset();
globalSealEngine.reset();
pblocktree = std::make_unique<BlockTreeDB>(DBParams{
.path = chainman.m_options.datadir / "blocks" / "index",
.cache_bytes = static_cast<size_t>(cache_sizes.block_tree_db),
Expand Down Expand Up @@ -141,6 +146,62 @@ static ChainstateLoadResult CompleteChainstateInitialization(
}
}

/////////////////////////////////////////////////////////// qtum
fGettingValuesDGP = options.getting_values_dgp;

dev::eth::NoProof::init();
fs::path qtumStateDir = gArgs.GetDataDirNet() / "stateQtum";
bool fStatus = fs::exists(qtumStateDir);
const std::string dirQtum = PathToString(qtumStateDir);
const dev::h256 hashDB(dev::sha3(dev::rlp("")));
dev::eth::BaseState existsQtumstate = fStatus ? dev::eth::BaseState::PreExisting : dev::eth::BaseState::Empty;
globalState = std::unique_ptr<QtumState>(new QtumState(dev::u256(0), QtumState::openDB(dirQtum, hashDB, dev::WithExisting::Trust), dirQtum, existsQtumstate));
const CChainParams& chainparams = Params();
dev::eth::ChainParams cp(chainparams.EVMGenesisInfo());
globalSealEngine = std::unique_ptr<dev::eth::SealEngineFace>(cp.createSealEngine());

pstorageresult.reset(new StorageResults(PathToString(qtumStateDir)));
if (options.reindex) {
pstorageresult->wipeResults();
}

{
LOCK(cs_main);
CChain& active_chain = chainman.ActiveChain();
if(active_chain.Tip() != nullptr){
globalState->setRoot(uintToh256(active_chain.Tip()->hashStateRoot));
globalState->setRootUTXO(uintToh256(active_chain.Tip()->hashUTXORoot));
} else {
globalState->setRoot(dev::sha3(dev::rlp("")));
globalState->setRootUTXO(uintToh256(chainparams.GenesisBlock().hashUTXORoot));
globalState->populateFrom(cp.genesisState);
}
globalState->db().commit();
globalState->dbUtxo().commit();
}

fRecordLogOpcodes = options.record_log_opcodes;
fIsVMlogFile = fs::exists(gArgs.GetDataDirNet() / "vmExecLogs.json");
///////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////// // qtum
if (fAddressIndex != options.addrindex) {
return {ChainstateLoadStatus::FAILURE, _("You need to rebuild the database using -reindex to change -addrindex")};
}
///////////////////////////////////////////////////////////////
// Check for changed -logevents state
if (fLogEvents != options.logevents && !fLogEvents) {
return {ChainstateLoadStatus::FAILURE, _("You need to rebuild the database using -reindex to enable -logevents")};
}

if (!options.logevents)
{
pstorageresult->wipeResults();
pblocktree->WipeHeightIndex();
fLogEvents = false;
pblocktree->WriteFlag("logevents", fLogEvents);
}

if (!options.reindex) {
auto chainstates{chainman.GetAll()};
if (std::any_of(chainstates.begin(), chainstates.end(),
Expand Down Expand Up @@ -251,6 +312,10 @@ ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager& chainman, const C

LOCK(cs_main);

CChain& active_chain = chainman.ActiveChain();
QtumDGP qtumDGP(globalState.get(), chainman.ActiveChainstate(), fGettingValuesDGP);
globalSealEngine->setQtumSchedule(qtumDGP.getGasSchedule(active_chain.Height() + (active_chain.Height()+1 >= chainman.GetConsensus().QIP7Height ? 0 : 1) ));

for (Chainstate* chainstate : chainman.GetAll()) {
if (!is_coinsview_empty(chainstate)) {
const CBlockIndex* tip = chainstate->m_chain.Tip();
Expand Down
22 changes: 17 additions & 5 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
#include <validation.h>
#include <validationinterface.h>
#include <warnings.h>
#include <qtum/qtumdelegation.h>
#include <qtum/qtumDGP.h>

#if defined(HAVE_CONFIG_H)
#include <config/bitcoin-config.h>
Expand Down Expand Up @@ -361,6 +363,13 @@ class NodeImpl : public Node
}
void getGasInfo(uint64_t& blockGasLimit, uint64_t& minGasPrice, uint64_t& nGasPrice) override
{
LOCK(::cs_main);

QtumDGP qtumDGP(globalState.get(), chainman().ActiveChainstate(), fGettingValuesDGP);
int numBlocks = chainman().ActiveChain().Height();
blockGasLimit = qtumDGP.getBlockGasLimit(numBlocks);
minGasPrice = CAmount(qtumDGP.getMinGasPrice(numBlocks));
nGasPrice = (minGasPrice>DEFAULT_GAS_PRICE)?minGasPrice:DEFAULT_GAS_PRICE;
}
void getSyncInfo(int& numBlocks, bool& isSyncing) override
{
Expand Down Expand Up @@ -394,11 +403,13 @@ class NodeImpl : public Node
}
uint64_t getNetworkStakeWeight() override
{
return {};
LOCK(::cs_main);
return GetPoSKernelPS(chainman());
}
double getEstimatedAnnualROI() override
{
return {};
LOCK(::cs_main);
return GetEstimatedAnnualROI(chainman());
}
int64_t getMoneySupply() override
{
Expand All @@ -407,7 +418,7 @@ class NodeImpl : public Node
}
double getPoSKernelPS() override
{
return {};
return GetPoSKernelPS(chainman());
}
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
{
Expand Down Expand Up @@ -945,11 +956,12 @@ class ChainImpl : public Chain
#endif
bool getDelegation(const uint160& address, Delegation& delegation) override
{
return {};
QtumDelegation qtumDelegation;
return qtumDelegation.ExistDelegationContract() ? qtumDelegation.GetDelegation(address, delegation, chainman().ActiveChainstate()) : false;
}
bool verifyDelegation(const uint160& address, const Delegation& delegation) override
{
return {};
return QtumDelegation::VerifyDelegation(address, delegation);
}

NodeContext& m_node;
Expand Down
1 change: 1 addition & 0 deletions src/qtum/qtumdelegation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <util/signstr.h>
#include <util/strencodings.h>
#include <libdevcore/Common.h>
#include <logging.h>

const std::string strDelegationsABI = "[{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"_staker\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"_delegate\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint8\",\"name\":\"fee\",\"type\":\"uint8\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"blockHeight\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"bytes\",\"name\":\"PoD\",\"type\":\"bytes\"}],\"name\":\"AddDelegation\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"_staker\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"_delegate\",\"type\":\"address\"}],\"name\":\"RemoveDelegation\",\"type\":\"event\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"_staker\",\"type\":\"address\"},{\"internalType\":\"uint8\",\"name\":\"_fee\",\"type\":\"uint8\"},{\"internalType\":\"bytes\",\"name\":\"_PoD\",\"type\":\"bytes\"}],\"name\":\"addDelegation\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"name\":\"delegations\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"staker\",\"type\":\"address\"},{\"internalType\":\"uint8\",\"name\":\"fee\",\"type\":\"uint8\"},{\"internalType\":\"uint256\",\"name\":\"blockHeight\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"PoD\",\"type\":\"bytes\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"removeDelegation\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]";
const ContractABI contractDelegationABI = strDelegationsABI;
Expand Down
Loading

0 comments on commit dd2b435

Please sign in to comment.