Skip to content

Commit d20d756

Browse files
committed
rpc: faster getblockstats using BlockUndo data
Using undo data for a block (rev?????.dat) we can retrieve value information about prevouts and calculate the final transaction fee (rate). This approach is about 80x faster, drops the requirement for -txindex, and works for all non-pruned blocks.
1 parent 08bd21a commit d20d756

File tree

4 files changed

+174
-187
lines changed

4 files changed

+174
-187
lines changed

doc/release-notes-14802.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RPC changes
2+
-----------
3+
The `getblockstats` RPC is faster for fee calculation by using BlockUndo data. Also, `-txindex` is no longer required and `getblockstats` works for all non-pruned blocks.

src/rpc/blockchain.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <sync.h>
2929
#include <txdb.h>
3030
#include <txmempool.h>
31+
#include <undo.h>
3132
#include <util/strencodings.h>
3233
#include <util/system.h>
3334
#include <util/validation.h>
@@ -822,6 +823,20 @@ static CBlock GetBlockChecked(const CBlockIndex* pblockindex)
822823
return block;
823824
}
824825

826+
static CBlockUndo GetUndoChecked(const CBlockIndex* pblockindex)
827+
{
828+
CBlockUndo blockUndo;
829+
if (IsBlockPruned(pblockindex)) {
830+
throw JSONRPCError(RPC_MISC_ERROR, "Undo data not available (pruned data)");
831+
}
832+
833+
if (!UndoReadFromDisk(blockUndo, pblockindex)) {
834+
throw JSONRPCError(RPC_MISC_ERROR, "Can't read undo data from disk");
835+
}
836+
837+
return blockUndo;
838+
}
839+
825840
static UniValue getblock(const JSONRPCRequest& request)
826841
{
827842
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
@@ -1783,8 +1798,7 @@ static UniValue getblockstats(const JSONRPCRequest& request)
17831798
{
17841799
const RPCHelpMan help{"getblockstats",
17851800
"\nCompute per block statistics for a given window. All amounts are in satoshis.\n"
1786-
"It won't work for some heights with pruning.\n"
1787-
"It won't work without -txindex for utxo_size_inc, *fee or *feerate stats.\n",
1801+
"It won't work for some heights with pruning.\n",
17881802
{
17891803
{"hash_or_height", RPCArg::Type::NUM, RPCArg::Optional::NO, "The block hash or height of the target block", "", {"", "string or numeric"}},
17901804
{"stats", RPCArg::Type::ARR, /* default */ "all values", "Values to plot (see result below)",
@@ -1879,6 +1893,7 @@ static UniValue getblockstats(const JSONRPCRequest& request)
18791893
}
18801894

18811895
const CBlock block = GetBlockChecked(pindex);
1896+
const CBlockUndo blockUndo = GetUndoChecked(pindex);
18821897

18831898
const bool do_all = stats.size() == 0; // Calculate everything if nothing selected (default)
18841899
const bool do_mediantxsize = do_all || stats.count("mediantxsize") != 0;
@@ -1892,10 +1907,6 @@ static UniValue getblockstats(const JSONRPCRequest& request)
18921907
const bool do_calculate_weight = do_all || SetHasKeys(stats, "total_weight", "avgfeerate", "swtotal_weight", "avgfeerate", "feerate_percentiles", "minfeerate", "maxfeerate");
18931908
const bool do_calculate_sw = do_all || SetHasKeys(stats, "swtxs", "swtotal_size", "swtotal_weight");
18941909

1895-
if (loop_inputs && !g_txindex) {
1896-
throw JSONRPCError(RPC_INVALID_PARAMETER, "One or more of the selected stats requires -txindex enabled");
1897-
}
1898-
18991910
CAmount maxfee = 0;
19001911
CAmount maxfeerate = 0;
19011912
CAmount minfee = MAX_MONEY;
@@ -1916,7 +1927,8 @@ static UniValue getblockstats(const JSONRPCRequest& request)
19161927
std::vector<std::pair<CAmount, int64_t>> feerate_array;
19171928
std::vector<int64_t> txsize_array;
19181929

1919-
for (const auto& tx : block.vtx) {
1930+
for (size_t i = 0; i < block.vtx.size(); ++i) {
1931+
const auto& tx = block.vtx.at(i);
19201932
outputs += tx->vout.size();
19211933

19221934
CAmount tx_total_out = 0;
@@ -1960,14 +1972,9 @@ static UniValue getblockstats(const JSONRPCRequest& request)
19601972

19611973
if (loop_inputs) {
19621974
CAmount tx_total_in = 0;
1963-
for (const CTxIn& in : tx->vin) {
1964-
CTransactionRef tx_in;
1965-
uint256 hashBlock;
1966-
if (!GetTransaction(in.prevout.hash, tx_in, Params().GetConsensus(), hashBlock)) {
1967-
throw JSONRPCError(RPC_INTERNAL_ERROR, std::string("Unexpected internal error (tx index seems corrupt)"));
1968-
}
1969-
1970-
CTxOut prevoutput = tx_in->vout[in.prevout.n];
1975+
const auto& txundo = blockUndo.vtxundo.at(i - 1);
1976+
for (const Coin& coin: txundo.vprevout) {
1977+
const CTxOut& prevoutput = coin.out;
19711978

19721979
tx_total_in += prevoutput.nValue;
19731980
utxo_size_inc -= GetSerializeSize(prevoutput, PROTOCOL_VERSION) + PER_UTXO_OVERHEAD;

0 commit comments

Comments
 (0)