danicoin/tests/core_tests/TestGenerator.h

124 lines
3.9 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 "chaingen.h"
#include "cryptonote_core/Currency.h"
#include "TransactionBuilder.h"
2015-05-27 12:08:46 +00:00
#include <Logging/LoggerGroup.h>
2014-08-13 10:51:37 +00:00
class TestGenerator {
public:
2015-05-27 12:08:46 +00:00
TestGenerator(
const CryptoNote::Currency& currency,
std::vector<test_event_entry>& eventsRef) :
2014-08-13 10:51:37 +00:00
generator(currency),
events(eventsRef) {
minerAccount.generate();
generator.constructBlock(genesisBlock, minerAccount, 1338224400);
events.push_back(genesisBlock);
lastBlock = genesisBlock;
}
2015-05-27 12:08:46 +00:00
const CryptoNote::Currency& currency() const { return generator.currency(); }
2014-08-13 10:51:37 +00:00
2015-05-27 12:08:46 +00:00
void makeNextBlock(const std::list<CryptoNote::Transaction>& txs = std::list<CryptoNote::Transaction>()) {
CryptoNote::Block block;
2014-08-13 10:51:37 +00:00
generator.constructBlock(block, lastBlock, minerAccount, txs);
events.push_back(block);
lastBlock = block;
}
2015-05-27 12:08:46 +00:00
void makeNextBlock(const CryptoNote::Transaction& tx) {
std::list<CryptoNote::Transaction> txs;
2014-08-13 10:51:37 +00:00
txs.push_back(tx);
makeNextBlock(txs);
}
void generateBlocks() {
generateBlocks(currency().minedMoneyUnlockWindow());
}
2015-05-27 12:08:46 +00:00
void generateBlocks(size_t count, uint8_t majorVersion = CryptoNote::BLOCK_MAJOR_VERSION_1) {
2014-08-13 10:51:37 +00:00
while (count--) {
2015-05-27 12:08:46 +00:00
CryptoNote::Block next;
2014-08-13 10:51:37 +00:00
generator.constructBlockManually(next, lastBlock, minerAccount, test_generator::bf_major_ver, majorVersion);
lastBlock = next;
events.push_back(next);
}
}
2015-05-27 12:08:46 +00:00
TransactionBuilder createTxBuilder(const CryptoNote::account_base& from, const CryptoNote::account_base& to, uint64_t amount, uint64_t fee) {
2014-08-13 10:51:37 +00:00
2015-05-27 12:08:46 +00:00
std::vector<CryptoNote::tx_source_entry> sources;
std::vector<CryptoNote::tx_destination_entry> destinations;
2014-08-13 10:51:37 +00:00
fillTxSourcesAndDestinations(sources, destinations, from, to, amount, fee);
TransactionBuilder builder(generator.currency());
builder.setInput(sources, from.get_keys());
builder.setOutput(destinations);
return builder;
}
void fillTxSourcesAndDestinations(
2015-05-27 12:08:46 +00:00
std::vector<CryptoNote::tx_source_entry>& sources,
std::vector<CryptoNote::tx_destination_entry>& destinations,
const CryptoNote::account_base& from, const CryptoNote::account_base& to, uint64_t amount, uint64_t fee, size_t nmix = 0) {
2014-08-13 10:51:37 +00:00
fill_tx_sources_and_destinations(events, lastBlock, from, to, amount, fee, nmix, sources, destinations);
}
void constructTxToKey(
2015-05-27 12:08:46 +00:00
CryptoNote::Transaction& tx,
const CryptoNote::account_base& from,
const CryptoNote::account_base& to,
2014-08-13 10:51:37 +00:00
uint64_t amount,
uint64_t fee,
size_t nmix = 0) {
2015-05-27 12:08:46 +00:00
construct_tx_to_key(logger, events, tx, lastBlock, from, to, amount, fee, nmix);
2014-08-13 10:51:37 +00:00
}
void addEvent(const test_event_entry& e) {
events.push_back(e);
}
void addCallback(const std::string& name) {
callback_entry cb;
cb.callback_name = name;
events.push_back(cb);
}
void addCheckAccepted() {
addCallback("check_block_accepted");
}
void addCheckPurged() {
addCallback("check_block_purged");
}
2015-05-27 12:08:46 +00:00
Logging::LoggerGroup logger;
2014-08-13 10:51:37 +00:00
test_generator generator;
2015-05-27 12:08:46 +00:00
CryptoNote::Block genesisBlock;
CryptoNote::Block lastBlock;
CryptoNote::account_base minerAccount;
2014-08-13 10:51:37 +00:00
std::vector<test_event_entry>& events;
};