Files
thehub/testing/hub/MetaBlock_tests.cpp
tomFlowee bc47a700a4 Refactor; wrap BufferPool in shared_ptr
As we moved most of the creation of a BufferPool to be via the
Streaming::pool() method, which uses a thread-local, it makes sense
to start cleaning up the design and make it more modern C++.
The above mentioned method would return a reference and you'd see
loads of places use `auto &pool =` which is less than ideal.

As the number of places where we actually instantiate a BufferPool
goes down, the usage of some sort of smart pointer makes more sense.

This now makes all APIs use BufferPool be wrapped in a shared_ptr.
2023-12-21 15:23:23 +01:00

83 lines
2.8 KiB
C++

/*
* This file is part of the Flowee project
* Copyright (C) 2021 Tom Zander <tom@flowee.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MetaBlock_tests.h"
#include <BlockMetaData.h>
#include <streaming/BufferPool.h>
#include <primitives/Block.h>
TestMetaBlock::TestMetaBlock()
{
}
void TestMetaBlock::testCreation()
{
QFile input(":/blockdata");
auto pool = std::make_shared<Streaming::BufferPool>();
bool ok = input.open(QIODevice::ReadOnly);
QVERIFY(ok);
auto size = input.read(pool->begin(), pool->capacity());
Block block(pool->commit(size));
QVERIFY(block.createHash()
== uint256S("0x00000000000000000560372e0caadc38c56cde6c4aaae03287a6898e643e5b8a"));
std::vector<std::unique_ptr<std::deque<std::int32_t> > > feesVector;
BlockMetaData noFeesMD = BlockMetaData::parseBlock(13451, block, feesVector, pool);
auto coinbase = noFeesMD.first();
// no fees vector sets the coinbase to a non-zero value.
QCOMPARE(coinbase->fees, 0xFFFFFF);
QCOMPARE(coinbase->next()->fees, 0);
feesVector.resize(1);
feesVector[0].reset(new std::deque<std::int32_t>());
feesVector[0]->push_back(8475); // first one should go to the first real transaction (not coinbase);
BlockMetaData md = BlockMetaData::parseBlock(13451, block, feesVector, pool);
QCOMPARE(md.blockHeight(), 13451);
QCOMPARE(md.txCount(), 94);
coinbase = md.first();
QCOMPARE(coinbase->offsetInBlock, 81);
QCOMPARE(coinbase->fees, 0);
auto nextTx = coinbase->next();
QCOMPARE(nextTx->offsetInBlock, 248);
QCOMPARE(nextTx->fees, 8475);
QCOMPARE(nextTx->next()->fees, 0);
BlockMetaData md2(md.data());
QCOMPARE(md2.blockHeight(), 13451);
QCOMPARE(md2.txCount(), 94);
coinbase = md2.first();
QCOMPARE(coinbase->offsetInBlock, 81);
QCOMPARE(coinbase->fees, 0);
nextTx = coinbase->next();
QCOMPARE(nextTx->offsetInBlock, 248);
QCOMPARE(nextTx->fees, 8475);
QCOMPARE(nextTx->next()->fees, 0);
try {
md2.tx(0);
md2.tx(50);
md2.tx(93);
} catch (...) {
QFAIL("Should not throw");
}
try {
md2.tx(94);
QFAIL("Out of bounds, should have thrown");
} catch (...) { }
}