Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor files #298

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e2c0a76
refactor AcceptToMempool arguments:
Naviabheeman Feb 6, 2024
0f29808
added Package test accept to mempool and submit package
Naviabheeman Jan 1, 2024
5a8010a
Add packages rpcs testmempoolaccept and submit package to tapyrus core
Naviabheeman Jan 21, 2024
8e237c1
fix buid - remove conflict markers
Naviabheeman Feb 7, 2024
b890086
fix build - changed overwritted by rebase are corrected
Naviabheeman Feb 7, 2024
3232f76
fix build - add code to add package transactions to temp list in co…
Naviabheeman Mar 13, 2024
90592d0
remove extra univalue in test package acceptance result
Naviabheeman Mar 13, 2024
7a64d8f
fix syntax of messing input error in der signature test
Naviabheeman Mar 14, 2024
b74cef7
fix rpc help - add packages to rpc categories
Naviabheeman Mar 14, 2024
f0562e4
fix result of debug mode script - feature_cltv
Naviabheeman Mar 14, 2024
fd8114f
refactor AcceptToMempool arguments:
Naviabheeman Dec 31, 2023
d7b07ec
make smaller function calls from AcceptToMemoryPoolWorker to simplify it
Naviabheeman Jan 24, 2024
bdbd807
make smaller function calls from AcceptToMemoryPoolWorker to simplify it
Naviabheeman Jan 24, 2024
76b6cba
fix test failure - use state and missing inputs from opt instead of s…
Naviabheeman Jan 26, 2024
10153f8
review feedback - put back currentBlockScriptVerifyFlags
Naviabheeman Feb 1, 2024
9df08cd
refactor AcceptToMempool arguments:
Naviabheeman Feb 1, 2024
966e431
added Package test accept to mempool and submit package
Naviabheeman Jan 1, 2024
4316f2d
add colored coin tests for package accept to mempool
Naviabheeman Jan 2, 2024
59f1f7b
fix package acceptance after refactoring.
Naviabheeman Jan 3, 2024
75ba8a6
Fix mempool lock causing delay in package acceptance
Naviabheeman Jan 8, 2024
e9b6855
add submit package tests
Naviabheeman Jan 9, 2024
c57de8d
remove submit package test as it causes segmentation fault
Naviabheeman Jan 9, 2024
471d5e2
split validation.cpp into multiple files:
Naviabheeman Mar 14, 2024
b39110b
add deque header to fix builds
Naviabheeman May 7, 2024
d066254
change include guards to fix lint error
Naviabheeman May 7, 2024
196c195
remove duplicate config copy.ini added by mistake
Naviabheeman Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ add_library(server
policy/fees.cpp
policy/feerate.cpp
policy/policy.cpp
policy/packages.cpp
policy/rbf.cpp
rest.cpp
rpc/blockchain.cpp
Expand Down
16 changes: 15 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ BITCOIN_CORE_H = \
bech32.h \
bloom.h \
blockencodings.h \
blockprune.h \
chain.h \
chainparams.h \
chainparamsseeds.h \
chainstate.h \
checkpoints.h \
checkqueue.h \
clientversion.h \
Expand All @@ -106,12 +108,15 @@ BITCOIN_CORE_H = \
compat/endian.h \
compat/sanity.h \
compressor.h \
connecttrace.h \
consensus/consensus.h \
consensus/tx_verify.h \
core_io.h \
core_memusage.h \
cs_main.h \
cuckoocache.h \
federationparams.h \
file_io.h \
fs.h \
httprpc.h \
httpserver.h \
Expand Down Expand Up @@ -140,6 +145,7 @@ BITCOIN_CORE_H = \
outputtype.h \
policy/feerate.h \
policy/fees.h \
policy/packages.h \
policy/policy.h \
policy/rbf.h \
protocol.h \
Expand All @@ -156,6 +162,7 @@ BITCOIN_CORE_H = \
rpc/register.h \
rpc/util.h \
scheduler.h \
scriptcheck.h \
script/descriptor.h \
script/ismine.h \
script/sigcache.h \
Expand All @@ -174,6 +181,7 @@ BITCOIN_CORE_H = \
threadinterrupt.h \
timedata.h \
torcontrol.h \
trace.h \
txdb.h \
txmempool.h \
ui_interface.h \
Expand All @@ -196,8 +204,8 @@ BITCOIN_CORE_H = \
wallet/walletutil.h \
wallet/coinselection.h \
warnings.h \
verifydb.h \
xfieldhistory.h \
trace.h \
zmq/zmqabstractnotifier.h \
zmq/zmqconfig.h\
zmq/zmqnotificationinterface.h \
Expand All @@ -219,9 +227,13 @@ libtapyrus_server_a_SOURCES = \
addrman.cpp \
bloom.cpp \
blockencodings.cpp \
blockprune.cpp \
chain.cpp \
chainstate.cpp \
checkpoints.cpp \
consensus/tx_verify.cpp \
cs_main.cpp \
file_io.cpp \
httprpc.cpp \
httpserver.cpp \
index/base.cpp \
Expand All @@ -236,6 +248,7 @@ libtapyrus_server_a_SOURCES = \
outputtype.cpp \
policy/fees.cpp \
policy/policy.cpp \
policy/packages.cpp \
policy/rbf.cpp \
rest.cpp \
rpc/blockchain.cpp \
Expand All @@ -253,6 +266,7 @@ libtapyrus_server_a_SOURCES = \
txmempool.cpp \
ui_interface.cpp \
validation.cpp \
verifydb.cpp \
validationinterface.cpp \
$(BITCOIN_CORE_H)

Expand Down
167 changes: 167 additions & 0 deletions src/blockprune.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// Copyright (c) 2024 Chaintope Inc.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <validation.h>
#include <cs_main.h>
#include <blockprune.h>
#include <file_io.h>


/**
* BLOCK PRUNING CODE
*/

/* Calculate the amount of disk space the block & undo files currently use */
uint64_t CalculateCurrentUsage()
{
LOCK(cs_LastBlockFile);

uint64_t retval = 0;
for (const CBlockFileInfo &file : vinfoBlockFile) {
retval += file.nSize + file.nUndoSize;
}
return retval;
}

/* Prune a block file (modify associated database entries)*/
void PruneOneBlockFile(const int fileNumber)
{
LOCK(cs_LastBlockFile);

for (const auto& entry : mapBlockIndex) {
CBlockIndex* pindex = entry.second;
if (pindex->nFile == fileNumber) {
pindex->nStatus &= ~BLOCK_HAVE_DATA;
pindex->nStatus &= ~BLOCK_HAVE_UNDO;
pindex->nFile = 0;
pindex->nDataPos = 0;
pindex->nUndoPos = 0;
setDirtyBlockIndex.insert(pindex);

// Prune from mapBlocksUnlinked -- any block we prune would have
// to be downloaded again in order to consider its chain, at which
// point it would be considered as a candidate for
// mapBlocksUnlinked or setBlockIndexCandidates.
std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex->pprev);
while (range.first != range.second) {
std::multimap<CBlockIndex *, CBlockIndex *>::iterator _it = range.first;
range.first++;
if (_it->second == pindex) {
mapBlocksUnlinked.erase(_it);
}
}
}
}

vinfoBlockFile[fileNumber].SetNull();
setDirtyFileInfo.insert(fileNumber);
}


void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
{
for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
CDiskBlockPos pos(*it, 0);
fs::remove(GetBlockPosFilename(pos, "blk"));
fs::remove(GetBlockPosFilename(pos, "rev"));
LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
}
}

void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight)
{
assert(fPruneMode && nManualPruneHeight > 0);

LOCK2(cs_main, cs_LastBlockFile);
if (chainActive.Tip() == nullptr)
return;

// last block to prune is the lesser of (user-specified height, MIN_BLOCKS_TO_KEEP from the tip)
unsigned int nLastBlockWeCanPrune = std::min((unsigned)nManualPruneHeight, chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP);
int count=0;
for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
if (vinfoBlockFile[fileNumber].nSize == 0 || vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
continue;
PruneOneBlockFile(fileNumber);
setFilesToPrune.insert(fileNumber);
count++;
}
LogPrintf("Prune (Manual): prune_height=%d removed %d blk/rev pairs\n", nLastBlockWeCanPrune, count);
}

/* This function is called from the RPC code for pruneblockchain */
void PruneBlockFilesManual(int nManualPruneHeight)
{
CValidationState state;
if (!FlushStateToDisk(state, FlushStateMode::NONE, nManualPruneHeight)) {
LogPrintf("%s: failed to flush state (%s)\n", __func__, FormatStateMessage(state));
}
}

void FindFilesToPrune(std::set<int>& setFilesToPrune, uint32_t nPruneAfterHeight)
{
LOCK2(cs_main, cs_LastBlockFile);
if (chainActive.Tip() == nullptr || nPruneTarget == 0) {
return;
}
if (chainActive.Tip()->nHeight <= nPruneAfterHeight) {
return;
}

unsigned int nLastBlockWeCanPrune = chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP;
uint64_t nCurrentUsage = CalculateCurrentUsage();
// We don't check to prune until after we've allocated new space for files
// So we should leave a buffer under our target to account for another allocation
// before the next pruning.
uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
uint64_t nBytesToPrune;
int count=0;

if (nCurrentUsage + nBuffer >= nPruneTarget) {
// On a prune event, the chainstate DB is flushed.
// To avoid excessive prune events negating the benefit of high dbcache
// values, we should not prune too rapidly.
// So when pruning in IBD, increase the buffer a bit to avoid a re-prune too soon.
if (IsInitialBlockDownload()) {
// Since this is only relevant during IBD, we use a fixed 10%
nBuffer += nPruneTarget / 10;
}

for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize;

if (vinfoBlockFile[fileNumber].nSize == 0)
continue;

if (nCurrentUsage + nBuffer < nPruneTarget) // are we below our target?
break;

// don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the prod chain's tip but keep scanning
if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
continue;

PruneOneBlockFile(fileNumber);
// Queue up the files for removal
setFilesToPrune.insert(fileNumber);
nCurrentUsage -= nBytesToPrune;
count++;
}
}

LogPrint(BCLog::PRUNE, "Prune: target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n",
nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
nLastBlockWeCanPrune, count);
}

bool CheckDiskSpace(uint64_t nAdditionalBytes, bool blocks_dir)
{
uint64_t nFreeBytesAvailable = fs::space(blocks_dir ? GetBlocksDir() : GetDataDir()).available;

// Check for nMinDiskSpace bytes (currently 50MB)
if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
return AbortNode("Disk space is low!", _("Error: Disk space is low!"));

return true;
}
48 changes: 48 additions & 0 deletions src/blockprune.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2023 The Bitcoin Core developers
// Copyright (c) 2024 Chaintope Inc.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef TAPYRUS_BLOCKPRUNE_H
#define TAPYRUS_BLOCKPRUNE_H

/** Minimum disk space required - used in CheckDiskSpace() */
static const uint64_t nMinDiskSpace = 52428800;

/** Check whether enough disk space is available for an incoming block */
bool CheckDiskSpace(uint64_t nAdditionalBytes = 0, bool blocks_dir = false);

/** Calculate the amount of disk space the block & undo files currently use */
uint64_t CalculateCurrentUsage();

/**
* Mark one block file as pruned.
*/
void PruneOneBlockFile(const int fileNumber);

/**
* Actually unlink the specified files
*/
void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune);

/* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight);

/**
* Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target.
* The user sets the target (in MB) on the command line or in config file. This will be run on startup and whenever new
* space is allocated in a block or undo file, staying below the target. Changing back to unpruned requires a reindex
* (which in this case means the blockchain must be re-downloaded.)
*
* Pruning functions are called from FlushStateToDisk when the global fCheckForPruning flag has been set.
* Block and undo files are deleted in lock-step (when blk00003.dat is deleted, so is rev00003.dat.)
* Pruning cannot take place until the longest chain is at least a certain length (100000 on mainnet, 1000 on testnet, 1000 on dev).
* Pruning will never delete a block within a defined distance (currently 288) from the active chain's tip.
* The block index is updated by unsetting HAVE_DATA and HAVE_UNDO for any blocks that were stored in the deleted files.
* A db flag records the fact that at least some block files have been pruned.
*
* @param[out] setFilesToPrune The set of file indices that can be unlinked will be returned
*/
void FindFilesToPrune(std::set<int>& setFilesToPrune, uint32_t nPruneAfterHeight);

#endif // TAPYRUS_BLOCKPRUNE_H
Loading