danicoin/tests/unit_tests/TestBlockchainGenerator.cpp

206 lines
5.8 KiB
C++
Raw Normal View History

// Copyright (c) 2011-2015 The Cryptonote developers
2014-06-25 17:21:42 +00:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "TestBlockchainGenerator.h"
#include <time.h>
#include <unordered_set>
#include "cryptonote_core/cryptonote_format_utils.h"
2014-06-25 17:21:42 +00:00
#include "../performance_tests/multi_tx_test_base.h"
class TransactionForAddressCreator : public multi_tx_test_base<5>
{
typedef multi_tx_test_base<5> base_class;
public:
TransactionForAddressCreator() {}
bool init()
{
return base_class::init();
}
2014-08-13 10:51:37 +00:00
void generate(const cryptonote::AccountPublicAddress& address, cryptonote::Transaction& tx)
2014-06-25 17:21:42 +00:00
{
std::vector<cryptonote::tx_destination_entry> destinations;
cryptonote::decompose_amount_into_digits(this->m_source_amount, 0,
[&](uint64_t chunk) { destinations.push_back(cryptonote::tx_destination_entry(chunk, address)); },
[&](uint64_t a_dust) { destinations.push_back(cryptonote::tx_destination_entry(a_dust, address)); });
cryptonote::construct_tx(this->m_miners[this->real_source_idx].get_keys(), this->m_sources, destinations, std::vector<uint8_t>(), tx, 0);
}
void generateSingleOutputTx(const cryptonote::AccountPublicAddress& address, uint64_t amount, cryptonote::Transaction& tx) {
std::vector<cryptonote::tx_destination_entry> destinations;
destinations.push_back(cryptonote::tx_destination_entry(amount, address));
2014-06-25 17:21:42 +00:00
cryptonote::construct_tx(this->m_miners[this->real_source_idx].get_keys(), this->m_sources, destinations, std::vector<uint8_t>(), tx, 0);
}
};
2014-08-13 10:51:37 +00:00
TestBlockchainGenerator::TestBlockchainGenerator(const cryptonote::Currency& currency) :
m_currency(currency),
generator(currency)
2014-06-25 17:21:42 +00:00
{
miner_acc.generate();
addGenesisBlock();
addMiningBlock();
2014-06-25 17:21:42 +00:00
}
2014-08-13 10:51:37 +00:00
std::vector<cryptonote::Block>& TestBlockchainGenerator::getBlockchain()
2014-06-25 17:21:42 +00:00
{
return m_blockchain;
}
2014-08-13 10:51:37 +00:00
bool TestBlockchainGenerator::getTransactionByHash(const crypto::hash& hash, cryptonote::Transaction& tx)
2014-06-25 17:21:42 +00:00
{
auto it = m_txs.find(hash);
if (it == m_txs.end())
return false;
tx = it->second;
return true;
}
void TestBlockchainGenerator::addGenesisBlock() {
std::vector<size_t> bsizes;
generator.addBlock(m_currency.genesisBlock(), 0, 0, bsizes, 0);
m_blockchain.push_back(m_currency.genesisBlock());
}
2014-06-25 17:21:42 +00:00
void TestBlockchainGenerator::addMiningBlock() {
cryptonote::Block block;
uint64_t timestamp = time(NULL);
generator.constructBlock(block, miner_acc, timestamp);
m_blockchain.push_back(block);
2014-06-25 17:21:42 +00:00
}
void TestBlockchainGenerator::generateEmptyBlocks(size_t count)
{
for (size_t i = 0; i < count; ++i)
{
2014-08-13 10:51:37 +00:00
cryptonote::Block& prev_block = m_blockchain.back();
cryptonote::Block block;
generator.constructBlock(block, prev_block, miner_acc);
2014-06-25 17:21:42 +00:00
m_blockchain.push_back(block);
}
}
2014-08-13 10:51:37 +00:00
void TestBlockchainGenerator::addTxToBlockchain(const cryptonote::Transaction& transaction)
2014-06-25 17:21:42 +00:00
{
crypto::hash txHash = cryptonote::get_transaction_hash(transaction);
m_txs[txHash] = transaction;
2014-08-13 10:51:37 +00:00
std::list<cryptonote::Transaction> txs;
2014-06-25 17:21:42 +00:00
txs.push_back(transaction);
2014-08-13 10:51:37 +00:00
cryptonote::Block& prev_block = m_blockchain.back();
cryptonote::Block block;
2014-06-25 17:21:42 +00:00
2014-08-13 10:51:37 +00:00
generator.constructBlock(block, prev_block, miner_acc, txs);
2014-06-25 17:21:42 +00:00
m_blockchain.push_back(block);
}
2014-08-13 10:51:37 +00:00
bool TestBlockchainGenerator::getBlockRewardForAddress(const cryptonote::AccountPublicAddress& address)
2014-06-25 17:21:42 +00:00
{
TransactionForAddressCreator creator;
if (!creator.init())
return false;
2014-08-13 10:51:37 +00:00
cryptonote::Transaction tx;
2014-06-25 17:21:42 +00:00
creator.generate(address, tx);
2015-04-08 15:08:54 +00:00
tx.unlockTime = 10; //default unlock time for coinbase transactions
2014-06-25 17:21:42 +00:00
addToBlockchain(tx);
return true;
}
bool TestBlockchainGenerator::getSingleOutputTransaction(const cryptonote::AccountPublicAddress& address, uint64_t amount) {
TransactionForAddressCreator creator;
if (!creator.init())
return false;
cryptonote::Transaction tx;
creator.generateSingleOutputTx(address, amount, tx);
addToBlockchain(tx);
return true;
}
void TestBlockchainGenerator::addToBlockchain(const cryptonote::Transaction& tx) {
crypto::hash txHash = get_transaction_hash(tx);
2014-06-25 17:21:42 +00:00
m_txs[txHash] = tx;
2014-08-13 10:51:37 +00:00
std::list<cryptonote::Transaction> txs;
2014-06-25 17:21:42 +00:00
txs.push_back(tx);
2014-08-13 10:51:37 +00:00
cryptonote::Block& prev_block = m_blockchain.back();
cryptonote::Block block;
2014-06-25 17:21:42 +00:00
2014-08-13 10:51:37 +00:00
generator.constructBlock(block, prev_block, miner_acc, txs);
2014-06-25 17:21:42 +00:00
m_blockchain.push_back(block);
}
2014-06-25 17:21:42 +00:00
void TestBlockchainGenerator::getPoolSymmetricDifference(std::vector<crypto::hash>&& known_pool_tx_ids, crypto::hash known_block_id, bool& is_bc_actual,
std::vector<cryptonote::Transaction>& new_txs, std::vector<crypto::hash>& deleted_tx_ids)
{
if (known_block_id != cryptonote::get_block_hash(m_blockchain.back())) {
is_bc_actual = false;
return;
}
is_bc_actual = true;
std::unordered_set<crypto::hash> txIds;
for (const auto& kv : m_txPool) {
txIds.insert(kv.first);
}
std::unordered_set<crypto::hash> known_set(known_pool_tx_ids.begin(), known_pool_tx_ids.end());
for (auto it = txIds.begin(), e = txIds.end(); it != e;) {
auto known_it = known_set.find(*it);
if (known_it != known_set.end()) {
known_set.erase(known_it);
it = txIds.erase(it);
}
else {
new_txs.push_back(m_txPool[*it]);
++it;
}
}
deleted_tx_ids.assign(known_set.begin(), known_set.end());
2014-06-25 17:21:42 +00:00
}
void TestBlockchainGenerator::putTxToPool(const cryptonote::Transaction& tx) {
crypto::hash txHash = cryptonote::get_transaction_hash(tx);
m_txPool[txHash] = tx;
}
void TestBlockchainGenerator::putTxPoolToBlockchain() {
std::list<cryptonote::Transaction> txs;
for (const auto& kv: m_txPool) {
m_txs[kv.first] = kv.second;
txs.push_back(kv.second);
}
cryptonote::Block& prev_block = m_blockchain.back();
cryptonote::Block block;
generator.constructBlock(block, prev_block, miner_acc, txs);
m_blockchain.push_back(block);
m_txPool.clear();
}
void TestBlockchainGenerator::clearTxPool() {
m_txPool.clear();
2014-06-25 17:21:42 +00:00
}