Skip to content

Commit

Permalink
Port block assembler
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Jul 29, 2024
1 parent a3bfc16 commit f2b7199
Show file tree
Hide file tree
Showing 5 changed files with 670 additions and 49 deletions.
6 changes: 4 additions & 2 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <kernel/notifications_interface.h>
#include <logging.h>
#include <pow.h>
#include <pos.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <reverse_iterator.h>
Expand Down Expand Up @@ -168,8 +169,8 @@ bool BlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, s
pindexNew->prevoutStake = diskindex.prevoutStake;
pindexNew->vchBlockSigDlgt = diskindex.vchBlockSigDlgt; // qtum

if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams)) {
return error("%s: CheckProofOfWork failed: %s", __func__, pindexNew->ToString());
if (!CheckIndexProof(*pindexNew, consensusParams)) {
return error("%s: CheckIndexProof failed: %s", __func__, pindexNew->ToString());
}

// NovaCoin: build setStakeSeen
Expand Down Expand Up @@ -648,6 +649,7 @@ CBlockIndex* BlockManager::AddToBlockIndex(const CBlockHeader& block, CBlockInde
}
pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime);
pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
pindexNew->nStakeModifier = ComputeStakeModifier(pindexNew->pprev, block.IsProofOfWork() ? hash : block.prevoutStake.hash.ToUint256());
pindexNew->RaiseValidity(BLOCK_VALID_TREE);
if (best_header == nullptr || best_header->nChainWork < pindexNew->nChainWork) {
best_header = pindexNew;
Expand Down
65 changes: 65 additions & 0 deletions src/node/chainstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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 @@ -41,6 +43,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 @@ -142,6 +147,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 @@ -252,6 +313,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
46 changes: 36 additions & 10 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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 All @@ -64,6 +66,13 @@

#include <boost/signals2/signal.hpp>

#ifdef ENABLE_WALLET
#include <wallet/stake.h>
#include <node/miner.h>
#include <wallet/rpc/contract.h>
#include <wallet/rpc/mining.h>
#endif

using interfaces::BlockTip;
using interfaces::Chain;
using interfaces::FoundBlock;
Expand Down Expand Up @@ -362,6 +371,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 @@ -395,11 +411,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 @@ -408,7 +426,7 @@ class NodeImpl : public Node
}
double getPoSKernelPS() override
{
return {};
return GetPoSKernelPS(chainman());
}
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
{
Expand Down Expand Up @@ -613,7 +631,8 @@ class ChainImpl : public Chain
}
std::map<COutPoint, uint32_t> getImmatureStakes() override
{
return {};
LOCK(cs_main);
return GetImmatureStakes(chainman());
}
std::optional<int> findLocatorFork(const CBlockLocator& locator) override
{
Expand Down Expand Up @@ -917,38 +936,45 @@ class ChainImpl : public Chain
}
CAmount getTxGasFee(const CMutableTransaction& tx) override
{
return {};
return GetTxGasFee(tx, mempool(), chainman().ActiveChainstate());
}
#ifdef ENABLE_WALLET
void startStake(wallet::CWallet& wallet) override
{
if (node::CanStake())
{
StartStake(wallet);
}
}
void stopStake(wallet::CWallet& wallet) override
{
StopStake(wallet);
}
uint64_t getStakeWeight(const wallet::CWallet& wallet, uint64_t* pStakerWeight, uint64_t* pDelegateWeight) override
{
return {};
return GetStakeWeight(wallet, pStakerWeight, pDelegateWeight);
}
void refreshDelegates(wallet::CWallet *pwallet, bool myDelegates, bool stakerDelegates) override
{
RefreshDelegates(pwallet, myDelegates, stakerDelegates);
}
Span<const CRPCCommand> getContractRPCCommands() override
{
return {};
return wallet::GetContractRPCCommands();
}
Span<const CRPCCommand> getMiningRPCCommands() override
{
return {};
return wallet::GetMiningRPCCommands();
}
#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
Loading

0 comments on commit f2b7199

Please sign in to comment.