45
45
#include < memory>
46
46
#include < stdint.h>
47
47
48
- using node::BlockAssembler;
49
- using node::CBlockTemplate;
48
+ using interfaces::BlockTemplate;
50
49
using interfaces::Mining;
50
+ using node::BlockAssembler;
51
51
using node::NodeContext;
52
52
using node::RegenerateCommitments;
53
53
using node::UpdateTime;
@@ -130,7 +130,7 @@ static RPCHelpMan getnetworkhashps()
130
130
};
131
131
}
132
132
133
- static bool GenerateBlock (ChainstateManager& chainman, Mining& miner, CBlock& block, uint64_t & max_tries, std::shared_ptr<const CBlock>& block_out, bool process_new_block)
133
+ static bool GenerateBlock (ChainstateManager& chainman, Mining& miner, CBlock&& block, uint64_t & max_tries, std::shared_ptr<const CBlock>& block_out, bool process_new_block)
134
134
{
135
135
block_out.reset ();
136
136
block.hashMerkleRoot = BlockMerkleRoot (block);
@@ -146,7 +146,7 @@ static bool GenerateBlock(ChainstateManager& chainman, Mining& miner, CBlock& bl
146
146
return true ;
147
147
}
148
148
149
- block_out = std::make_shared<const CBlock>(block);
149
+ block_out = std::make_shared<const CBlock>(std::move ( block) );
150
150
151
151
if (!process_new_block) return true ;
152
152
@@ -161,12 +161,11 @@ static UniValue generateBlocks(ChainstateManager& chainman, Mining& miner, const
161
161
{
162
162
UniValue blockHashes (UniValue::VARR);
163
163
while (nGenerate > 0 && !chainman.m_interrupt ) {
164
- std::unique_ptr<CBlockTemplate> pblocktemplate (miner.createNewBlock (coinbase_script));
165
- if (!pblocktemplate.get ())
166
- throw JSONRPCError (RPC_INTERNAL_ERROR, " Couldn't create new block" );
164
+ std::unique_ptr<BlockTemplate> block_template (miner.createNewBlock (coinbase_script));
165
+ CHECK_NONFATAL (block_template);
167
166
168
167
std::shared_ptr<const CBlock> block_out;
169
- if (!GenerateBlock (chainman, miner, pblocktemplate-> block , nMaxTries, block_out, /* process_new_block=*/ true )) {
168
+ if (!GenerateBlock (chainman, miner, block_template-> getBlock () , nMaxTries, block_out, /* process_new_block=*/ true )) {
170
169
break ;
171
170
}
172
171
@@ -371,11 +370,10 @@ static RPCHelpMan generateblock()
371
370
372
371
ChainstateManager& chainman = EnsureChainman (node);
373
372
{
374
- std::unique_ptr<CBlockTemplate> blocktemplate{miner.createNewBlock (coinbase_script, {.use_mempool = false })};
375
- if (!blocktemplate) {
376
- throw JSONRPCError (RPC_INTERNAL_ERROR, " Couldn't create new block" );
377
- }
378
- block = blocktemplate->block ;
373
+ std::unique_ptr<BlockTemplate> block_template{miner.createNewBlock (coinbase_script, {.use_mempool = false })};
374
+ CHECK_NONFATAL (block_template);
375
+
376
+ block = block_template->getBlock ();
379
377
}
380
378
381
379
CHECK_NONFATAL (block.vtx .size () == 1 );
@@ -394,7 +392,7 @@ static RPCHelpMan generateblock()
394
392
std::shared_ptr<const CBlock> block_out;
395
393
uint64_t max_tries{DEFAULT_MAX_TRIES};
396
394
397
- if (!GenerateBlock (chainman, miner, block, max_tries, block_out, process_new_block) || !block_out) {
395
+ if (!GenerateBlock (chainman, miner, std::move ( block) , max_tries, block_out, process_new_block) || !block_out) {
398
396
throw JSONRPCError (RPC_MISC_ERROR, " Failed to make block." );
399
397
}
400
398
@@ -800,7 +798,7 @@ static RPCHelpMan getblocktemplate()
800
798
// Update block
801
799
static CBlockIndex* pindexPrev;
802
800
static int64_t time_start;
803
- static std::unique_ptr<CBlockTemplate> pblocktemplate ;
801
+ static std::unique_ptr<BlockTemplate> block_template ;
804
802
if (!pindexPrev || pindexPrev->GetBlockHash () != tip ||
805
803
(miner.getTransactionsUpdated () != nTransactionsUpdatedLast && GetTime () - time_start > 5 ))
806
804
{
@@ -814,20 +812,19 @@ static RPCHelpMan getblocktemplate()
814
812
815
813
// Create new block
816
814
CScript scriptDummy = CScript () << OP_TRUE;
817
- pblocktemplate = miner.createNewBlock (scriptDummy);
818
- if (!pblocktemplate) {
819
- throw JSONRPCError (RPC_OUT_OF_MEMORY, " Out of memory" );
820
- }
815
+ block_template = miner.createNewBlock (scriptDummy);
816
+ CHECK_NONFATAL (block_template);
817
+
821
818
822
819
// Need to update only after we know createNewBlock succeeded
823
820
pindexPrev = pindexPrevNew;
824
821
}
825
822
CHECK_NONFATAL (pindexPrev);
826
- CBlock* pblock = &pblocktemplate-> block ; // pointer for convenience
823
+ CBlock block{block_template-> getBlock ()};
827
824
828
825
// Update nTime
829
- UpdateTime (pblock , consensusParams, pindexPrev);
830
- pblock-> nNonce = 0 ;
826
+ UpdateTime (&block , consensusParams, pindexPrev);
827
+ block. nNonce = 0 ;
831
828
832
829
// NOTE: If at some point we support pre-segwit miners post-segwit-activation, this needs to take segwit support into consideration
833
830
const bool fPreSegWit = !DeploymentActiveAfter (pindexPrev, chainman, Consensus::DEPLOYMENT_SEGWIT);
@@ -836,8 +833,11 @@ static RPCHelpMan getblocktemplate()
836
833
837
834
UniValue transactions (UniValue::VARR);
838
835
std::map<uint256, int64_t > setTxIndex;
836
+ std::vector<CAmount> tx_fees{block_template->getTxFees ()};
837
+ std::vector<CAmount> tx_sigops{block_template->getTxSigops ()};
838
+
839
839
int i = 0 ;
840
- for (const auto & it : pblock-> vtx ) {
840
+ for (const auto & it : block. vtx ) {
841
841
const CTransaction& tx = *it;
842
842
uint256 txHash = tx.GetHash ();
843
843
setTxIndex[txHash] = i++;
@@ -860,8 +860,8 @@ static RPCHelpMan getblocktemplate()
860
860
entry.pushKV (" depends" , std::move (deps));
861
861
862
862
int index_in_template = i - 1 ;
863
- entry.pushKV (" fee" , pblocktemplate-> vTxFees [ index_in_template] );
864
- int64_t nTxSigOps = pblocktemplate-> vTxSigOpsCost [ index_in_template] ;
863
+ entry.pushKV (" fee" , tx_fees. at ( index_in_template) );
864
+ int64_t nTxSigOps{tx_sigops. at ( index_in_template)} ;
865
865
if (fPreSegWit ) {
866
866
CHECK_NONFATAL (nTxSigOps % WITNESS_SCALE_FACTOR == 0 );
867
867
nTxSigOps /= WITNESS_SCALE_FACTOR;
@@ -874,7 +874,7 @@ static RPCHelpMan getblocktemplate()
874
874
875
875
UniValue aux (UniValue::VOBJ);
876
876
877
- arith_uint256 hashTarget = arith_uint256 ().SetCompact (pblock-> nBits );
877
+ arith_uint256 hashTarget = arith_uint256 ().SetCompact (block. nBits );
878
878
879
879
UniValue aMutable (UniValue::VARR);
880
880
aMutable.push_back (" time" );
@@ -904,7 +904,7 @@ static RPCHelpMan getblocktemplate()
904
904
break ;
905
905
case ThresholdState::LOCKED_IN:
906
906
// Ensure bit is set in block version
907
- pblock-> nVersion |= chainman.m_versionbitscache .Mask (consensusParams, pos);
907
+ block. nVersion |= chainman.m_versionbitscache .Mask (consensusParams, pos);
908
908
[[fallthrough]];
909
909
case ThresholdState::STARTED:
910
910
{
@@ -913,7 +913,7 @@ static RPCHelpMan getblocktemplate()
913
913
if (setClientRules.find (vbinfo.name ) == setClientRules.end ()) {
914
914
if (!vbinfo.gbt_force ) {
915
915
// If the client doesn't support this, don't indicate it in the [default] version
916
- pblock-> nVersion &= ~chainman.m_versionbitscache .Mask (consensusParams, pos);
916
+ block. nVersion &= ~chainman.m_versionbitscache .Mask (consensusParams, pos);
917
917
}
918
918
}
919
919
break ;
@@ -933,15 +933,15 @@ static RPCHelpMan getblocktemplate()
933
933
}
934
934
}
935
935
}
936
- result.pushKV (" version" , pblock-> nVersion );
936
+ result.pushKV (" version" , block. nVersion );
937
937
result.pushKV (" rules" , std::move (aRules));
938
938
result.pushKV (" vbavailable" , std::move (vbavailable));
939
939
result.pushKV (" vbrequired" , int (0 ));
940
940
941
- result.pushKV (" previousblockhash" , pblock-> hashPrevBlock .GetHex ());
941
+ result.pushKV (" previousblockhash" , block. hashPrevBlock .GetHex ());
942
942
result.pushKV (" transactions" , std::move (transactions));
943
943
result.pushKV (" coinbaseaux" , std::move (aux));
944
- result.pushKV (" coinbasevalue" , (int64_t )pblock-> vtx [0 ]->vout [0 ].nValue );
944
+ result.pushKV (" coinbasevalue" , (int64_t )block. vtx [0 ]->vout [0 ].nValue );
945
945
result.pushKV (" longpollid" , tip.GetHex () + ToString (nTransactionsUpdatedLast));
946
946
result.pushKV (" target" , hashTarget.GetHex ());
947
947
result.pushKV (" mintime" , (int64_t )pindexPrev->GetMedianTimePast ()+1 );
@@ -960,16 +960,16 @@ static RPCHelpMan getblocktemplate()
960
960
if (!fPreSegWit ) {
961
961
result.pushKV (" weightlimit" , (int64_t )MAX_BLOCK_WEIGHT);
962
962
}
963
- result.pushKV (" curtime" , pblock-> GetBlockTime ());
964
- result.pushKV (" bits" , strprintf (" %08x" , pblock-> nBits ));
963
+ result.pushKV (" curtime" , block. GetBlockTime ());
964
+ result.pushKV (" bits" , strprintf (" %08x" , block. nBits ));
965
965
result.pushKV (" height" , (int64_t )(pindexPrev->nHeight +1 ));
966
966
967
967
if (consensusParams.signet_blocks ) {
968
968
result.pushKV (" signet_challenge" , HexStr (consensusParams.signet_challenge ));
969
969
}
970
970
971
- if (!pblocktemplate-> vchCoinbaseCommitment .empty ()) {
972
- result.pushKV (" default_witness_commitment" , HexStr (pblocktemplate-> vchCoinbaseCommitment ));
971
+ if (!block_template-> getCoinbaseCommitment () .empty ()) {
972
+ result.pushKV (" default_witness_commitment" , HexStr (block_template-> getCoinbaseCommitment () ));
973
973
}
974
974
975
975
return result;
0 commit comments