refactor: introduce a python-like enumerate() method, and use it in block_reward_reallocation_tests.cpp

This commit is contained in:
PastaPastaPasta 2022-10-23 16:00:17 -05:00 committed by GitHub
parent 32a328cae3
commit 88adf2402f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 6 deletions

View File

@ -293,6 +293,7 @@ BITCOIN_CORE_H = \
util/bip32.h \
util/bytevectorhash.h \
util/check.h \
util/enumerate.h \
util/error.h \
util/fees.h \
util/golombrice.h \

View File

@ -23,6 +23,7 @@
#include <llmq/blockprocessor.h>
#include <llmq/chainlocks.h>
#include <llmq/instantsend.h>
#include <util/enumerate.h>
#include <boost/test/unit_test.hpp>
@ -48,10 +49,9 @@ struct TestChainBRRBeforeActivationSetup : public TestChainSetup
static SimpleUTXOMap BuildSimpleUtxoMap(const std::vector<CTransactionRef>& txs)
{
SimpleUTXOMap utxos;
for (size_t i = 0; i < txs.size(); i++) {
auto& tx = txs[i];
for (size_t j = 0; j < tx->vout.size(); j++) {
utxos.try_emplace(COutPoint(tx->GetHash(), j), std::make_pair((int)i + 1, tx->vout[j].nValue));
for (auto [i, tx] : enumerate(txs)) {
for (auto [j, output] : enumerate(tx->vout)) {
utxos.try_emplace(COutPoint(tx->GetHash(), j), std::make_pair((int)i + 1, output.nValue));
}
}
return utxos;
@ -104,9 +104,9 @@ static void SignTransaction(const CTxMemPool& mempool, CMutableTransaction& tx,
FillableSigningProvider tempKeystore;
tempKeystore.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
for (size_t i = 0; i < tx.vin.size(); i++) {
for (auto [i, input] : enumerate(tx.vin)) {
uint256 hashBlock;
CTransactionRef txFrom = GetTransaction(/* block_index */ nullptr, &mempool, tx.vin[i].prevout.hash, Params().GetConsensus(), hashBlock);
CTransactionRef txFrom = GetTransaction(/* block_index */ nullptr, &mempool, input.prevout.hash, Params().GetConsensus(), hashBlock);
BOOST_ASSERT(txFrom);
BOOST_ASSERT(SignSignature(tempKeystore, *txFrom, tx, i, SIGHASH_ALL));
}

40
src/util/enumerate.h Normal file
View File

@ -0,0 +1,40 @@
// Copyright (c) 2022 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_UTIL_ENUMERATE_H
#define BITCOIN_UTIL_ENUMERATE_H
#include <tuple>
#include <iterator>
/**
* similar to python's enumerate(iterable).
* @tparam T type of iterable, automatically deduced
* @tparam TIter
* @param iterable an iterable object, can be a temporary
* @return struct containing a size_t index, and it's element in iterable
*/
template <typename T,
typename TIter = decltype(std::begin(std::declval<T>())),
typename = decltype(std::end(std::declval<T>()))>
constexpr auto enumerate(T && iterable)
{
struct iterator
{
size_t i;
TIter iter;
bool operator != (const iterator & other) const { return iter != other.iter; }
void operator ++ () { ++i; ++iter; }
auto operator * () const { return std::tie(i, *iter); }
};
struct iterable_wrapper
{
T iterable;
auto begin() { return iterator{ 0, std::begin(iterable) }; }
auto end() { return iterator{ 0, std::end(iterable) }; }
};
return iterable_wrapper{ std::forward<T>(iterable) };
}
#endif // BITCOIN_UTIL_ENUMERATE_H