danicoin/tests/unit_tests/TestBlockchainGenerator.cpp

128 lines
3.5 KiB
C++
Raw Normal View History

2014-08-13 10:38:35 +00:00
// Copyright (c) 2012-2014, The CryptoNote developers, The Bytecoin developers
//
// 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/>.
2014-06-25 17:21:42 +00:00
#include <time.h>
#include "TestBlockchainGenerator.h"
#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
{
cryptonote::tx_destination_entry destination(this->m_source_amount, address);
std::vector<cryptonote::tx_destination_entry> destinations;
destinations.push_back(destination);
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();
}
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()
{
2014-08-13 10:51:37 +00:00
cryptonote::Block genesis;
2014-06-25 17:21:42 +00:00
uint64_t timestamp = time(NULL);
2014-08-13 10:51:37 +00:00
generator.constructBlock(genesis, miner_acc, timestamp);
2014-06-25 17:21:42 +00:00
m_blockchain.push_back(genesis);
}
void TestBlockchainGenerator::generateEmptyBlocks(size_t count)
{
addGenesisBlock();
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);
crypto::hash txHash = cryptonote::get_transaction_hash(tx);
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);
return true;
}