diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -2198,6 +2198,12 @@ do_mediantxsize || loop_inputs || SetHasKeys(stats, "total_size", "avgtxsize", "mintxsize", "maxtxsize"); + if (loop_inputs && !g_txindex) { + throw JSONRPCError( + RPC_INVALID_PARAMETER, + "One or more of the selected stats requires -txindex enabled"); + } + const int64_t blockMaxSize = config.GetMaxBlockSize(); Amount maxfee = Amount::zero(); Amount maxfeerate = Amount::zero(); @@ -2249,12 +2255,6 @@ } if (loop_inputs) { - if (!g_txindex) { - throw JSONRPCError(RPC_INVALID_PARAMETER, - "One or more of the selected stats requires " - "-txindex enabled"); - } - Amount tx_total_in = Amount::zero(); for (const CTxIn &in : tx->vin) { CTransactionRef tx_in; diff --git a/test/functional/rpc_getblockstats.py b/test/functional/rpc_getblockstats.py --- a/test/functional/rpc_getblockstats.py +++ b/test/functional/rpc_getblockstats.py @@ -194,6 +194,8 @@ assert_raises_rpc_error(-8, 'Invalid selected statistic aaa{}'.format(inv_sel_stat), self.nodes[0].getblockstats, hash_or_height=1, stats=['minfee', 'aaa{}'.format(inv_sel_stat)]) + assert_raises_rpc_error(-8, 'One or more of the selected stats requires -txindex enabled', + self.nodes[1].getblockstats, hash_or_height=1) assert_raises_rpc_error(-8, 'One or more of the selected stats requires -txindex enabled', self.nodes[1].getblockstats, hash_or_height=self.start_height + self.max_stat_pos)