danicoin/tests/CoreTests/TestGenerator.h

111 lines
3.3 KiB
C
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2014-08-13 10:51:37 +00:00
#pragma once
2015-07-30 15:22:07 +00:00
#include "Chaingen.h"
2014-08-13 10:51:37 +00:00
2015-07-30 15:22:07 +00:00
#include "CryptoNoteCore/Currency.h"
2014-08-13 10:51:37 +00:00
#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-07-30 15:22:07 +00:00
TransactionBuilder createTxBuilder(const CryptoNote::AccountBase& from, const CryptoNote::AccountBase& to, uint64_t amount, uint64_t fee) {
2014-08-13 10:51:37 +00:00
2015-07-30 15:22:07 +00:00
std::vector<CryptoNote::TransactionSourceEntry> sources;
std::vector<CryptoNote::TransactionDestinationEntry> destinations;
2014-08-13 10:51:37 +00:00
fillTxSourcesAndDestinations(sources, destinations, from, to, amount, fee);
TransactionBuilder builder(generator.currency());
2015-07-30 15:22:07 +00:00
builder.setInput(sources, from.getAccountKeys());
2014-08-13 10:51:37 +00:00
builder.setOutput(destinations);
return builder;
}
void fillTxSourcesAndDestinations(
2015-07-30 15:22:07 +00:00
std::vector<CryptoNote::TransactionSourceEntry>& sources,
std::vector<CryptoNote::TransactionDestinationEntry>& destinations,
const CryptoNote::AccountBase& from, const CryptoNote::AccountBase& 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,
2015-07-30 15:22:07 +00:00
const CryptoNote::AccountBase& from,
const CryptoNote::AccountBase& 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;
2015-07-30 15:22:07 +00:00
CryptoNote::AccountBase minerAccount;
2014-08-13 10:51:37 +00:00
std::vector<test_event_entry>& events;
};