Files

133 lines
3.5 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2009-2010 Satoshi Nakamoto
* Copyright (C) 2009-2015 The Bitcoin Core developers
2021-11-02 09:28:35 +01:00
* Copyright (C) 2021 Tom Zander <tom@flowee.org>
2017-11-09 19:34:51 +01:00
*
* 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/>.
*/
2013-04-13 00:13:08 -05:00
2021-11-02 09:28:35 +01:00
#ifndef FLOWEE_PRIMITIVES_MUTABLEBLOCK_H
#define FLOWEE_PRIMITIVES_MUTABLEBLOCK_H
#include "primitives/Block.h"
2014-11-18 21:03:02 +00:00
#include "primitives/transaction.h"
2013-04-13 00:13:08 -05:00
#include "serialize.h"
#include "uint256.h"
/** Nodes collect new transactions into a block, hash them into a hash tree,
* and scan through nonce values to make the block's hash satisfy proof-of-work
* requirements. When they solve the proof-of-work, they broadcast the block
* to everyone and the block is added to the block chain. The first transaction
* in the block is a special one that creates a new coin owned by the creator
* of the block.
*/
class BlockHeaderFields
{
public:
// header
int32_t nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
uint32_t nTime;
uint32_t nBits;
uint32_t nNonce;
inline BlockHeaderFields() {
2021-11-02 09:28:35 +01:00
setNull();
}
2017-01-19 21:40:34 +01:00
ADD_SERIALIZE_METHODS
2014-08-20 08:42:31 +02:00
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(this->nVersion);
nVersion = this->nVersion;
READWRITE(hashPrevBlock);
READWRITE(hashMerkleRoot);
READWRITE(nTime);
READWRITE(nBits);
READWRITE(nNonce);
2014-08-20 08:42:31 +02:00
}
2021-11-02 09:28:35 +01:00
inline void setNull() {
2016-02-15 05:13:27 +01:00
nVersion = 0;
hashPrevBlock.SetNull();
hashMerkleRoot.SetNull();
nTime = 0;
nBits = 0;
nNonce = 0;
}
2021-11-02 09:28:35 +01:00
inline bool isNull() const {
return (nBits == 0);
}
2021-11-02 09:28:35 +01:00
uint256 createHash() const;
2021-11-02 09:28:35 +01:00
inline int64_t blockTime() const {
2013-04-13 00:13:08 -05:00
return (int64_t)nTime;
}
};
2013-06-23 19:37:03 -07:00
class MutableBlock : public BlockHeaderFields
2013-06-23 19:37:03 -07:00
{
public:
// network and disk
std::vector<CTransaction> vtx;
2021-11-02 09:36:09 +01:00
MutableBlock()
2013-06-23 19:37:03 -07:00
{
2021-11-02 09:28:35 +01:00
setNull();
2013-06-23 19:37:03 -07:00
}
MutableBlock(const BlockHeaderFields &header)
2013-06-23 19:37:03 -07:00
{
2021-11-02 09:36:09 +01:00
setNull();
*((BlockHeaderFields*)this) = header;
2013-06-23 19:37:03 -07:00
}
2017-01-19 21:40:34 +01:00
ADD_SERIALIZE_METHODS
2014-08-20 08:42:31 +02:00
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(*(BlockHeaderFields*)this);
2013-06-23 19:37:03 -07:00
READWRITE(vtx);
2014-08-20 08:42:31 +02:00
}
2013-06-23 19:37:03 -07:00
2021-11-02 09:36:09 +01:00
void setNull()
2013-06-23 19:37:03 -07:00
{
BlockHeaderFields::setNull();
2013-06-23 19:37:03 -07:00
vtx.clear();
}
/**
* Helper method to convert to the new style (non serializable) class.
*/
BlockHeader blockHeader() const
2013-06-23 19:37:03 -07:00
{
BlockHeader block;
2013-06-23 19:37:03 -07:00
block.nVersion = nVersion;
block.hashPrevBlock = hashPrevBlock;
block.hashMerkleRoot = hashMerkleRoot;
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
return block;
}
};
2018-01-16 10:47:52 +00:00
#endif