2021-03-05 19:10:51 +01:00
|
|
|
/*
|
|
|
|
|
* 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>
|
|
|
|
|
|
2021-11-02 10:23:33 +01:00
|
|
|
#include <primitives/Block.h>
|
2021-03-05 19:10:51 +01:00
|
|
|
|
2021-03-08 13:47:33 +01:00
|
|
|
#include <vector>
|
|
|
|
|
#include <deque>
|
|
|
|
|
#include <memory>
|
2021-03-05 19:10:51 +01:00
|
|
|
|
|
|
|
|
class BlockMetaData
|
|
|
|
|
{
|
|
|
|
|
public:
|
2021-03-12 09:35:27 +01:00
|
|
|
/**
|
|
|
|
|
* Constructor that loads the data from a buffer.
|
|
|
|
|
* \see the static creation method parseBlock()
|
|
|
|
|
*/
|
2021-03-05 19:10:51 +01:00
|
|
|
BlockMetaData(const Streaming::ConstBuffer &buffer);
|
|
|
|
|
|
2021-03-19 14:20:40 +01:00
|
|
|
/// invalid block
|
|
|
|
|
BlockMetaData();
|
|
|
|
|
|
|
|
|
|
/// copy constructor
|
|
|
|
|
BlockMetaData(const BlockMetaData &other) = default;
|
|
|
|
|
|
2021-03-12 09:35:27 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2021-11-02 10:18:24 +01:00
|
|
|
static BlockMetaData parseBlock(int blockHeight, const Block &block,
|
2021-03-08 13:47:33 +01:00
|
|
|
const std::vector<std::unique_ptr<std::deque<std::int32_t> > > &perTxFees,
|
|
|
|
|
Streaming::BufferPool &pool);
|
2021-03-05 19:10:51 +01:00
|
|
|
|
2021-03-12 09:35:27 +01:00
|
|
|
/**
|
|
|
|
|
* The per-transaction data.
|
|
|
|
|
*/
|
2021-03-05 19:10:51 +01:00
|
|
|
struct TransactionData {
|
|
|
|
|
char txid[32];
|
|
|
|
|
uint32_t offsetInBlock;
|
|
|
|
|
uint32_t fees : 24;
|
|
|
|
|
uint32_t scriptTags : 8;
|
|
|
|
|
|
|
|
|
|
const TransactionData *next() const {
|
|
|
|
|
return this + 1;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-10 18:04:57 +01:00
|
|
|
/**
|
|
|
|
|
* Find a transaction by txid.
|
|
|
|
|
* This will walk over the list of transactions and find a match with /a txid.
|
|
|
|
|
* We return a nullptr if no match was found.
|
|
|
|
|
*/
|
|
|
|
|
const TransactionData *findTransaction(const uint256 &txid) const;
|
2021-03-12 09:06:06 +01:00
|
|
|
/**
|
|
|
|
|
* 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;
|
2021-03-10 18:04:57 +01:00
|
|
|
|
2021-03-12 09:35:27 +01:00
|
|
|
/**
|
|
|
|
|
* Return the first transaction data (aka the coinbase).
|
|
|
|
|
*/
|
2021-03-05 19:10:51 +01:00
|
|
|
const TransactionData *first() const;
|
|
|
|
|
|
2021-03-12 09:35:27 +01:00
|
|
|
/**
|
|
|
|
|
* Returns the amount of transactions that are in this block.
|
|
|
|
|
*/
|
2021-03-05 19:10:51 +01:00
|
|
|
int txCount() const;
|
2021-03-12 09:35:27 +01:00
|
|
|
/**
|
|
|
|
|
* Return the TransactionData by index.
|
|
|
|
|
* Coinbase is transaction zero.
|
|
|
|
|
*/
|
2021-03-05 19:10:51 +01:00
|
|
|
const TransactionData* tx(int index) const;
|
|
|
|
|
|
2021-03-12 09:35:27 +01:00
|
|
|
/**
|
|
|
|
|
* \internal
|
|
|
|
|
* Returns the hard data.
|
|
|
|
|
*/
|
2021-03-05 19:10:51 +01:00
|
|
|
Streaming::ConstBuffer data() const;
|
|
|
|
|
|
2021-03-12 09:35:27 +01:00
|
|
|
/**
|
|
|
|
|
* Returns the blockheight this block was mined at.
|
|
|
|
|
*/
|
2021-03-05 19:10:51 +01:00
|
|
|
int blockHeight() const;
|
|
|
|
|
|
2021-03-12 09:35:27 +01:00
|
|
|
/**
|
|
|
|
|
* Returns true if the transactions are sorted by txid.
|
|
|
|
|
*/
|
2021-03-05 19:10:51 +01:00
|
|
|
bool ctorSorted() const;
|
|
|
|
|
|
2021-03-12 09:35:27 +01:00
|
|
|
/**
|
|
|
|
|
* Return the block id (or blockheader-hash) for this block.
|
|
|
|
|
*/
|
2021-03-09 13:36:48 +01:00
|
|
|
uint256 blockId() const;
|
|
|
|
|
|
2021-03-19 14:20:40 +01:00
|
|
|
BlockMetaData &operator=(const BlockMetaData &other) = default;
|
2021-03-12 09:35:27 +01:00
|
|
|
|
2021-03-05 19:10:51 +01:00
|
|
|
private:
|
|
|
|
|
bool m_ctorSorted = false;
|
|
|
|
|
int m_blockHeight = 0;
|
2021-03-09 13:36:48 +01:00
|
|
|
uint256 m_blockId;
|
2021-03-19 14:20:40 +01:00
|
|
|
Streaming::ConstBuffer m_data;
|
2021-03-05 19:10:51 +01:00
|
|
|
Streaming::ConstBuffer m_transactions;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|