@@ -46,17 +46,34 @@ static bool GetDataFromUnlockTx(const CTransaction& tx, CAmount& toUnlock, uint6
4646}
4747
4848namespace {
49- struct UnlockDataPerBlock {
49+ struct CreditPoolDataPerBlock {
5050 CAmount credit_pool{0 };
5151 CAmount unlocked{0 };
5252 std::unordered_set<uint64_t > indexes;
5353 };
5454} // anonymous namespace
5555
5656// it throws exception if anything went wrong
57- static UnlockDataPerBlock GetDataFromUnlockTxes (const CBlock& block)
57+ static std::optional<CreditPoolDataPerBlock> GetCreditDataFromBlock (const gsl::not_null<const CBlockIndex*> block_index,
58+ const Consensus::Params& consensusParams)
59+
5860{
59- UnlockDataPerBlock blockData;
61+ // There's no CbTx before DIP0003 activation
62+ if (!DeploymentActiveAt (*block_index, Params ().GetConsensus (), Consensus::DEPLOYMENT_DIP0003)) {
63+ return std::nullopt ;
64+ }
65+
66+ CBlock block;
67+ if (!ReadBlockFromDisk (block, block_index, consensusParams)) {
68+ throw std::runtime_error (" failed-getcbforblock-read" );
69+ }
70+
71+ if (block.vtx .empty () || block.vtx [0 ]->vExtraPayload .empty () || !block.vtx [0 ]->IsSpecialTxVersion ()) {
72+ LogPrintf (" %s: ERROR: empty CbTx for CreditPool at height=%d\n " , __func__, block_index->nHeight );
73+ return std::nullopt ;
74+ }
75+
76+ CreditPoolDataPerBlock blockData;
6077
6178 const auto opt_cbTx = GetTxPayload<CCbTx>(block.vtx [0 ]->vExtraPayload );
6279 if (!opt_cbTx) {
@@ -70,7 +87,7 @@ static UnlockDataPerBlock GetDataFromUnlockTxes(const CBlock& block)
7087 TxValidationState tx_state;
7188 uint64_t index{0 };
7289 if (!GetDataFromUnlockTx (*tx, unlocked, index, tx_state)) {
73- throw std::runtime_error (strprintf (" %s: CCreditPoolManager::GetDataFromUnlockTxes failed : %s" , __func__, tx_state.ToString ()));
90+ throw std::runtime_error (strprintf (" %s: GetDataFromUnlockTxfailed : %s" , __func__, tx_state.ToString ()));
7491 }
7592 blockData.unlocked += unlocked;
7693 blockData.indexes .insert (index);
@@ -117,32 +134,11 @@ void CCreditPoolManager::AddToCache(const uint256& block_hash, int height, const
117134 }
118135}
119136
120- static std::optional<CBlock> GetBlockForCreditPool (const gsl::not_null<const CBlockIndex*> block_index,
121- const Consensus::Params& consensusParams)
122- {
123- // There's no CbTx before DIP0003 activation
124- if (!DeploymentActiveAt (*block_index, Params ().GetConsensus (), Consensus::DEPLOYMENT_DIP0003)) {
125- return std::nullopt ;
126- }
127-
128- CBlock block;
129- if (!ReadBlockFromDisk (block, block_index, consensusParams)) {
130- throw std::runtime_error (" failed-getcbforblock-read" );
131- }
132-
133- if (block.vtx .empty () || block.vtx [0 ]->vExtraPayload .empty () || !block.vtx [0 ]->IsSpecialTxVersion ()) {
134- LogPrintf (" %s: ERROR: empty CbTx for CreditPool at height=%d\n " , __func__, block_index->nHeight );
135- return std::nullopt ;
136- }
137-
138- return block;
139- }
140-
141137CCreditPool CCreditPoolManager::ConstructCreditPool (const gsl::not_null<const CBlockIndex*> block_index,
142138 CCreditPool prev, const Consensus::Params& consensusParams)
143139{
144- std::optional<CBlock> block = GetBlockForCreditPool (block_index, consensusParams);
145- if (!block ) {
140+ std::optional<CreditPoolDataPerBlock> opt_block_data = GetCreditDataFromBlock (block_index, consensusParams);
141+ if (!opt_block_data ) {
146142 // If reading of previous block is not successfully, but
147143 // prev contains credit pool related data, something strange happened
148144 if (prev.locked != 0 ) {
@@ -156,12 +152,12 @@ CCreditPool CCreditPoolManager::ConstructCreditPool(const gsl::not_null<const CB
156152 AddToCache (block_index->GetBlockHash (), block_index->nHeight , emptyPool);
157153 return emptyPool;
158154 }
155+ const CreditPoolDataPerBlock& blockData{*opt_block_data};
159156
160157 // We use here sliding window with Params().CreditPoolPeriodBlocks to determine
161158 // current limits for asset unlock transactions.
162159 // Indexes should not be duplicated since genesis block, but the Unlock Amount
163160 // of withdrawal transaction is limited only by this window
164- const UnlockDataPerBlock blockData = GetDataFromUnlockTxes (*block);
165161 CRangesSet indexes{std::move (prev.indexes )};
166162 if (std::any_of (blockData.indexes .begin (), blockData.indexes .end (), [&](const uint64_t index) { return !indexes.Add (index); })) {
167163 throw std::runtime_error (strprintf (" %s: failed-getcreditpool-index-duplicated" , __func__));
@@ -170,8 +166,8 @@ CCreditPool CCreditPoolManager::ConstructCreditPool(const gsl::not_null<const CB
170166 const CBlockIndex* distant_block_index{block_index->GetAncestor (block_index->nHeight - Params ().CreditPoolPeriodBlocks ())};
171167 CAmount distantUnlocked{0 };
172168 if (distant_block_index) {
173- if (std::optional<CBlock > distant_block = GetBlockForCreditPool (distant_block_index, consensusParams); distant_block) {
174- distantUnlocked = GetDataFromUnlockTxes (* distant_block). unlocked ;
169+ if (std::optional<CreditPoolDataPerBlock > distant_block{ GetCreditDataFromBlock (distant_block_index, consensusParams)} ; distant_block) {
170+ distantUnlocked = distant_block-> unlocked ;
175171 }
176172 }
177173
0 commit comments