Files
thehub/hub/server/BlockMetaData.h
T
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

128 lines
3.7 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/>.
*/
#ifndef BLOCKMETADATA_H
#define BLOCKMETADATA_H
#include <streaming/BufferPool.h>
#include <streaming/ConstBuffer.h>
#include <primitives/Block.h>
#include <vector>
#include <deque>
#include <memory>
class BlockMetaData
{
public:
/**
* Constructor that loads the data from a buffer.
* \see the static creation method parseBlock()
*/
BlockMetaData(const Streaming::ConstBuffer &buffer);
/// invalid block
BlockMetaData();
/// copy constructor
BlockMetaData(const BlockMetaData &other) = default;
/**
* Return true if this metadata object has proper fee data.
* Since it is legal to construct a block meta data with no fees info and that simply
* makes all the fees be set to zero (except for the coinbase one) it is helpful to
* ask if fees are present.
*/
bool hasFeesData() const;
/**
* The proper way to create a new BlockMetaData and fill it with data.
*
* The perTxFees are required to have a list of lists, which when we put them all together make one
* list of fee items per transaction, skipping the coinbase. So total fee objects is one less than all the
* transactions in a block since the coinbase is not represented here.
* The perTxFees can be an empty vector if no fees are present.
*/
static BlockMetaData parseBlock(int blockHeight, const Block &block,
const std::vector<std::unique_ptr<std::deque<std::int32_t> > > &perTxFees,
const std::shared_ptr<Streaming::BufferPool> &pool);
/**
* The per-transaction data.
*/
struct TransactionData {
uint32_t offsetInBlock;
uint32_t fees : 24;
uint32_t scriptTags : 8;
const TransactionData *next() const {
return this + 1;
}
};
/**
* Find a transaction by offset in block.
* This will walk over the list of transactions and find a match with /a offsetInBlock.
* We return a nullptr if no match was found.
*/
const TransactionData *findTransaction(int offsetInBlock) const;
/**
* Return the first transaction data (aka the coinbase).
*/
const TransactionData *first() const;
/**
* Returns the amount of transactions that are in this block.
*/
int txCount() const;
/**
* Return the TransactionData by index.
* Coinbase is transaction zero.
*/
const TransactionData* tx(int index) const;
/**
* \internal
* Returns the hard data.
*/
Streaming::ConstBuffer data() const;
/**
* Returns the blockheight this block was mined at.
*/
int blockHeight() const;
/**
* Return the block id (or blockheader-hash) for this block.
*/
uint256 blockId() const;
BlockMetaData &operator=(const BlockMetaData &other) = default;
private:
int m_blockHeight = 0;
uint256 m_blockId;
Streaming::ConstBuffer m_data;
Streaming::ConstBuffer m_transactions;
};
#endif