/* * This file is part of the Flowee project * Copyright (C) 2009-2010 Satoshi Nakamoto * Copyright (C) 2009-2015 The Bitcoin Core developers * Copyright (C) 2021 Tom Zander * * 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 . */ #ifndef FLOWEE_PRIMITIVES_MUTABLEBLOCK_H #define FLOWEE_PRIMITIVES_MUTABLEBLOCK_H #include "primitives/Block.h" #include "primitives/transaction.h" #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() { setNull(); } ADD_SERIALIZE_METHODS template 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); } inline void setNull() { nVersion = 0; hashPrevBlock.SetNull(); hashMerkleRoot.SetNull(); nTime = 0; nBits = 0; nNonce = 0; } inline bool isNull() const { return (nBits == 0); } uint256 createHash() const; inline int64_t blockTime() const { return (int64_t)nTime; } }; class MutableBlock : public BlockHeaderFields { public: // network and disk std::vector vtx; MutableBlock() { setNull(); } MutableBlock(const BlockHeaderFields &header) { setNull(); *((BlockHeaderFields*)this) = header; } ADD_SERIALIZE_METHODS template inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { READWRITE(*(BlockHeaderFields*)this); READWRITE(vtx); } void setNull() { BlockHeaderFields::setNull(); vtx.clear(); } /** * Helper method to convert to the new style (non serializable) class. */ BlockHeader blockHeader() const { BlockHeader block; block.nVersion = nVersion; block.hashPrevBlock = hashPrevBlock; block.hashMerkleRoot = hashMerkleRoot; block.nTime = nTime; block.nBits = nBits; block.nNonce = nNonce; return block; } }; #endif