Skip to content
Permalink
Browse files

tests: fix HF12 chaingen - construct bc object from events

  • Loading branch information...
ph4r05 committed Nov 8, 2019
1 parent a48ef0a commit fa7fc2517a7da3d5595634bec0502043b2a3cee3
Showing with 126 additions and 8 deletions.
  1. +120 −3 tests/core_tests/chaingen.cpp
  2. +6 −2 tests/core_tests/chaingen.h
  3. +0 −3 tests/core_tests/wallet_tools.cpp
@@ -47,6 +47,12 @@
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "cryptonote_basic/miner.h"

#include "blockchain_db/blockchain_db.h"
#include "cryptonote_core/cryptonote_core.h"
#include "cryptonote_core/tx_pool.h"
#include "cryptonote_core/blockchain.h"
#include "blockchain_db/testdb.h"

#include "chaingen.h"
#include "device/device.hpp"
using namespace std;
@@ -55,6 +61,110 @@ using namespace epee;
using namespace crypto;
using namespace cryptonote;

namespace
{

class TestDB: public cryptonote::BaseTestDB
{
private:
struct block_t
{
cryptonote::block bl;
crypto::hash hash;
};

public:
TestDB() { m_open = true; }

virtual void add_block( const cryptonote::block& blk
, size_t block_weight
, uint64_t long_term_block_weight
, const cryptonote::difficulty_type& cumulative_difficulty
, const uint64_t& coins_generated
, uint64_t num_rct_outs
, const crypto::hash& blk_hash
) override {
blocks.push_back({blk, blk_hash});
}

virtual uint64_t height() const override { return blocks.size(); }

// Required for randomx
virtual crypto::hash get_block_hash_from_height(const uint64_t &height) const override {
if (height < blocks.size()){
LOG_PRINT_L0("Get hash for block height: " << height << " hash: " << blocks[height].hash);
return blocks[height].hash;
}

LOG_PRINT_L0("Get hash for block height: " << height << " zero-hash");
crypto::hash hash = crypto::null_hash;
*(uint64_t*)&hash = height;
return hash;
}

virtual crypto::hash top_block_hash(uint64_t *block_height = NULL) const override {
uint64_t h = height();
if (h > 0) h -= 1;
if (block_height != nullptr){
*block_height = h;
}

return get_block_hash_from_height(h);
}

virtual cryptonote::block get_top_block() const override {
if (blocks.empty()){
cryptonote::block b;
return b;
}

return blocks[blocks.size()-1].bl;
}

virtual void pop_block(cryptonote::block &blk, std::vector<cryptonote::transaction> &txs) override { if (!blocks.empty()) blocks.pop_back(); }
virtual void set_hard_fork_version(uint64_t height, uint8_t version) override { if (height >= hf.size()) hf.resize(height + 1); hf[height] = version; }
virtual uint8_t get_hard_fork_version(uint64_t height) const override { if (height >= hf.size()) return 255; return hf[height]; }

private:
std::vector<block_t> blocks;
std::vector<uint8_t> hf;
};

}

static std::unique_ptr<cryptonote::Blockchain> init_blockchain(const std::vector<test_event_entry> & events, cryptonote::network_type nettype){
std::unique_ptr<cryptonote::Blockchain> bc;
v_hardforks_t hardforks;
cryptonote::test_options test_options_tmp{nullptr, 0};
const cryptonote::test_options * test_options_ = &test_options_tmp;
if (extract_hard_forks(events, hardforks)){
hardforks.push_back(std::make_pair((uint8_t)0, (uint64_t)0)); // terminator
test_options_tmp.hard_forks = hardforks.data();
test_options_ = &test_options_tmp;
} else {
return bc;
}

cryptonote::tx_memory_pool txpool(*bc);
bc.reset(new cryptonote::Blockchain(txpool));

cryptonote::Blockchain *blockchain = bc.get();
auto bdb = new TestDB();

BOOST_FOREACH(const test_event_entry &ev, events) {
if (typeid(block) == ev.type()) {
const block *blk2 = &boost::get<block>(ev);
auto blk_hash = get_block_hash(*blk2);
cryptonote::difficulty_type cumulative_difficulty = 1;
uint64_t coins_generated = 0;

bdb->add_block(*blk2, 1, 1, cumulative_difficulty, coins_generated, 0, blk_hash);
}
}

bool r = blockchain->init(bdb, nettype, true, test_options_, 2, nullptr);
return bc;
}

void test_generator::get_block_chain(std::vector<block_info>& blockchain, const crypto::hash& head, size_t n) const
{
@@ -184,12 +294,19 @@ bool test_generator::construct_block(cryptonote::block& blk, uint64_t height, co

//blk.tree_root_hash = get_tx_tree_hash(blk);

std::unique_ptr<cryptonote::Blockchain> bc;
if (m_events != nullptr && blk.major_version >= RX_BLOCK_VERSION){
bc = init_blockchain(*m_events, m_nettype);
}
cryptonote::Blockchain *blockchain = bc.get();

// Nonce search...
blk.nonce = 0;
while (!miner::find_nonce_for_given_block([](const cryptonote::block &b, uint64_t height, unsigned int threads, crypto::hash &hash){
return cryptonote::get_block_longhash(NULL, b, hash, height, threads);
}, blk, get_test_difficulty(hf_ver), height))
while (!miner::find_nonce_for_given_block([blockchain](const cryptonote::block &b, uint64_t height, unsigned int threads, crypto::hash &hash){
return cryptonote::get_block_longhash(blockchain, b, hash, height, threads);
}, blk, get_test_difficulty(hf_ver), height)) {
blk.timestamp++;
}

add_block(blk, txs_weight, block_weights, already_generated_coins, hf_ver ? hf_ver.get() : 1);

@@ -227,8 +227,8 @@ class test_generator
bf_hf_version= 1 << 8
};

test_generator() {}
test_generator(const test_generator &other): m_blocks_info(other.m_blocks_info) {}
test_generator(): m_events(nullptr) {}
test_generator(const test_generator &other): m_blocks_info(other.m_blocks_info), m_events(nullptr) {}
void get_block_chain(std::vector<block_info>& blockchain, const crypto::hash& head, size_t n) const;
void get_last_n_block_weights(std::vector<size_t>& block_weights, const crypto::hash& head, size_t n) const;
uint64_t get_already_generated_coins(const crypto::hash& blk_id) const;
@@ -253,9 +253,13 @@ class test_generator
uint8_t hf_version = 1);
bool construct_block_manually_tx(cryptonote::block& blk, const cryptonote::block& prev_block,
const cryptonote::account_base& miner_acc, const std::vector<crypto::hash>& tx_hashes, size_t txs_size);
void set_events(const std::vector<test_event_entry> * events) { m_events = events; }
void set_network_type(const cryptonote::network_type nettype) { m_nettype = nettype; }

private:
std::unordered_map<crypto::hash, block_info> m_blocks_info;
const std::vector<test_event_entry> * m_events;
cryptonote::network_type m_nettype;

friend class boost::serialization::access;

@@ -10,9 +10,6 @@ using namespace epee;
using namespace crypto;
using namespace cryptonote;

// Shared random generator
static std::default_random_engine RND(crypto::rand<unsigned>());

void wallet_accessor_test::set_account(tools::wallet2 * wallet, cryptonote::account_base& account)
{
wallet->clear();

0 comments on commit fa7fc25

Please sign in to comment.
You can’t perform that action at this time.