danicoin/tests/TestGenerator/TestGenerator.h

116 lines
5.1 KiB
C
Raw Normal View History

2015-05-27 12:08:46 +00:00
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
2014-08-13 10:51:37 +00:00
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <cstdint>
#include <list>
#include <vector>
#include <unordered_map>
#include "crypto/hash.h"
2015-07-30 15:22:07 +00:00
#include "CryptoNoteCore/CryptoNoteBasic.h"
#include "CryptoNoteCore/CryptoNoteBasicImpl.h"
#include "CryptoNoteCore/CryptoNoteFormatUtils.h"
#include "CryptoNoteCore/Currency.h"
#include "CryptoNoteCore/Difficulty.h"
2014-08-13 10:51:37 +00:00
class test_generator
{
public:
struct BlockInfo {
BlockInfo()
2015-07-30 15:22:07 +00:00
: previousBlockHash()
2014-08-13 10:51:37 +00:00
, alreadyGeneratedCoins(0)
, blockSize(0) {
}
2015-07-30 15:22:07 +00:00
BlockInfo(Crypto::Hash aPrevId, uint64_t anAlreadyGeneratedCoins, size_t aBlockSize)
: previousBlockHash(aPrevId)
2014-08-13 10:51:37 +00:00
, alreadyGeneratedCoins(anAlreadyGeneratedCoins)
, blockSize(aBlockSize) {
}
2015-07-30 15:22:07 +00:00
Crypto::Hash previousBlockHash;
2014-08-13 10:51:37 +00:00
uint64_t alreadyGeneratedCoins;
size_t blockSize;
};
enum BlockFields {
bf_none = 0,
bf_major_ver = 1 << 0,
bf_minor_ver = 1 << 1,
bf_timestamp = 1 << 2,
bf_prev_id = 1 << 3,
bf_miner_tx = 1 << 4,
bf_tx_hashes = 1 << 5,
bf_diffic = 1 << 6
};
2015-05-27 12:08:46 +00:00
test_generator(const CryptoNote::Currency& currency, uint8_t majorVersion = CryptoNote::BLOCK_MAJOR_VERSION_1,
uint8_t minorVersion = CryptoNote::BLOCK_MINOR_VERSION_0)
2014-08-13 10:51:37 +00:00
: m_currency(currency), defaultMajorVersion(majorVersion), defaultMinorVersion(minorVersion) {
}
uint8_t defaultMajorVersion;
uint8_t defaultMinorVersion;
2015-05-27 12:08:46 +00:00
const CryptoNote::Currency& currency() const { return m_currency; }
2014-08-13 10:51:37 +00:00
2015-07-30 15:22:07 +00:00
void getBlockchain(std::vector<BlockInfo>& blockchain, const Crypto::Hash& head, size_t n) const;
void getLastNBlockSizes(std::vector<size_t>& blockSizes, const Crypto::Hash& head, size_t n) const;
uint64_t getAlreadyGeneratedCoins(const Crypto::Hash& blockId) const;
2015-05-27 12:08:46 +00:00
uint64_t getAlreadyGeneratedCoins(const CryptoNote::Block& blk) const;
2014-08-13 10:51:37 +00:00
2015-05-27 12:08:46 +00:00
void addBlock(const CryptoNote::Block& blk, size_t tsxSize, uint64_t fee, std::vector<size_t>& blockSizes,
2014-08-13 10:51:37 +00:00
uint64_t alreadyGeneratedCoins);
2015-07-30 15:22:07 +00:00
bool constructBlock(CryptoNote::Block& blk, uint32_t height, const Crypto::Hash& previousBlockHash,
const CryptoNote::AccountBase& minerAcc, uint64_t timestamp, uint64_t alreadyGeneratedCoins,
2015-05-27 12:08:46 +00:00
std::vector<size_t>& blockSizes, const std::list<CryptoNote::Transaction>& txList);
2015-07-30 15:22:07 +00:00
bool constructBlock(CryptoNote::Block& blk, const CryptoNote::AccountBase& minerAcc, uint64_t timestamp);
bool constructBlock(CryptoNote::Block& blk, const CryptoNote::Block& blkPrev, const CryptoNote::AccountBase& minerAcc,
2015-05-27 12:08:46 +00:00
const std::list<CryptoNote::Transaction>& txList = std::list<CryptoNote::Transaction>());
bool constructBlockManually(CryptoNote::Block& blk, const CryptoNote::Block& prevBlock,
2015-07-30 15:22:07 +00:00
const CryptoNote::AccountBase& minerAcc, int actualParams = bf_none, uint8_t majorVer = 0,
uint8_t minorVer = 0, uint64_t timestamp = 0, const Crypto::Hash& previousBlockHash = Crypto::Hash(),
const CryptoNote::difficulty_type& diffic = 1, const CryptoNote::Transaction& baseTransaction = CryptoNote::Transaction(),
const std::vector<Crypto::Hash>& transactionHashes = std::vector<Crypto::Hash>(), size_t txsSizes = 0, uint64_t fee = 0);
2015-05-27 12:08:46 +00:00
bool constructBlockManuallyTx(CryptoNote::Block& blk, const CryptoNote::Block& prevBlock,
2015-07-30 15:22:07 +00:00
const CryptoNote::AccountBase& minerAcc, const std::vector<Crypto::Hash>& transactionHashes, size_t txsSize);
2015-05-27 12:08:46 +00:00
bool constructMaxSizeBlock(CryptoNote::Block& blk, const CryptoNote::Block& blkPrev,
2015-07-30 15:22:07 +00:00
const CryptoNote::AccountBase& minerAccount, size_t medianBlockCount = 0,
2015-05-27 12:08:46 +00:00
const std::list<CryptoNote::Transaction>& txList = std::list<CryptoNote::Transaction>());
2014-08-13 10:51:37 +00:00
private:
2015-05-27 12:08:46 +00:00
const CryptoNote::Currency& m_currency;
2015-07-30 15:22:07 +00:00
std::unordered_map<Crypto::Hash, BlockInfo> m_blocksInfo;
2014-08-13 10:51:37 +00:00
};
2015-05-27 12:08:46 +00:00
inline CryptoNote::difficulty_type getTestDifficulty() { return 1; }
void fillNonce(CryptoNote::Block& blk, const CryptoNote::difficulty_type& diffic);
2014-08-13 10:51:37 +00:00
2015-05-27 12:08:46 +00:00
bool constructMinerTxManually(const CryptoNote::Currency& currency, uint32_t height, uint64_t alreadyGeneratedCoins,
const CryptoNote::AccountPublicAddress& minerAddress, CryptoNote::Transaction& tx, uint64_t fee,
CryptoNote::KeyPair* pTxKey = 0);
2015-07-30 15:22:07 +00:00
bool constructMinerTxBySize(const CryptoNote::Currency& currency, CryptoNote::Transaction& baseTransaction, uint32_t height,
2015-05-27 12:08:46 +00:00
uint64_t alreadyGeneratedCoins, const CryptoNote::AccountPublicAddress& minerAddress,
2014-08-13 10:51:37 +00:00
std::vector<size_t>& blockSizes, size_t targetTxSize, size_t targetBlockSize, uint64_t fee = 0,
bool penalizeFee = false);