Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cyb417 test bp over fn #26

Merged
merged 5 commits into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions simulator/include/log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <iostream>

class logger_type:
public std::streambuf,
public std::ostream
{
public:
static logger_type& instance() {
static logger_type inst;
return inst;
}

void enabled(bool set) {
_enabled = set;
}

bool enabled() const {
return _enabled;
}

std::streambuf::int_type overflow(std::streambuf::int_type c) override {
if (_enabled) {
std::cout << static_cast<std::streambuf::char_type>(c);
}
return 0;
}

private:
logger_type():
std::ostream(this)
{ }

private:
bool _enabled { true };
};

static auto& logger = logger_type::instance();
9 changes: 5 additions & 4 deletions simulator/include/randpa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "database.hpp"
#include "simulator.hpp"
#include "log.hpp"

#define SYNC_RANDPA //SYNC mode
#include <eosio/randpa_plugin/randpa.hpp>
Expand Down Expand Up @@ -38,7 +39,7 @@ class RandpaNode: public Node {
}

virtual void restart() override {
std::cout << "[Node] #" << id << " restarted " << std::endl;
logger << "[Node] #" << id << " restarted " << std::endl;
init();
randpa_impl->start(copy_fork_db());
auto runner = get_runner();
Expand Down Expand Up @@ -75,20 +76,20 @@ class RandpaNode: public Node {
}

void on_receive(uint32_t from, void* msg) override {
std::cout << "[Node] #" << this->id << " on_receive " << std::endl;
logger << "[Node] #" << this->id << " on_receive " << std::endl;
auto data = *static_cast<randpa_net_msg*>(msg);
data.ses_id = from;
data.receive_time = fc::time_point::now();
in_net_ch->send(data);
}

void on_new_peer_event(uint32_t id) override {
std::cout << "[Node] #" << this->id << " on_new_peer_event " << std::endl;
logger << "[Node] #" << this->id << " on_new_peer_event " << std::endl;
ev_ch->send(randpa_event { ::on_new_peer_event { id } });
}

void on_accepted_block_event(pair<block_id_type, public_key_type> block) override {
std::cout << "[Node] #" << this->id << " on_accepted_block_event " << std::endl;
logger << "[Node] #" << this->id << " on_accepted_block_event " << std::endl;
ev_ch->send(randpa_event { ::on_accepted_block_event { block.first, db.fetch_prev_block_id(block.first),
block.second, get_active_bp_keys()
} });
Expand Down
65 changes: 34 additions & 31 deletions simulator/include/simulator.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "database.hpp"
#include "log.hpp"

#include <fc/bitutil.hpp>
#include <fc/crypto/sha256.hpp>
Expand Down Expand Up @@ -138,23 +139,23 @@ class Node {
std::stringstream ss;
ss << "[Node] #" << id << " ";
auto node_id = ss.str();
std::cout << node_id << "Received " << chain.blocks.size() << " blocks " << std::endl;
std::cout << node_id << chain << std::endl;
logger << node_id << "Received " << chain.blocks.size() << " blocks " << std::endl;
logger << node_id << chain << std::endl;

if (db.find(chain.blocks.back().first)) {
std::cout << node_id << "Already got chain head. Skipping chain " << std::endl;
logger << node_id << "Already got chain head. Skipping chain " << std::endl;
return false;
}

if (get_block_height(chain.blocks.back().first) <= get_block_height(db.get_master_block_id())) {
std::cout << node_id << "Current master is not smaller than chain head. Skipping chain";
logger << node_id << "Current master is not smaller than chain head. Skipping chain";
return false;
}

try {
db.insert(chain);
} catch (const ForkDbInsertException&) {
std::cout << node_id << "Failed to apply chain" << std::endl;
logger << node_id << "Failed to apply chain" << std::endl;
pending_chains.push(chain);
return false;
}
Expand All @@ -169,15 +170,15 @@ class Node {
inline std::set<public_key_type> get_active_bp_keys() const;

virtual void on_receive(uint32_t from, void *) {
std::cout << "Received from " << from << std::endl;
logger << "Received from " << from << std::endl;
}

virtual void on_new_peer_event(uint32_t from) {
std::cout << "On new peer event handled by " << id << " at " << get_clock().now() << std::endl;
logger << "On new peer event handled by " << id << " at " << get_clock().now() << std::endl;
}

virtual void on_accepted_block_event(pair<block_id_type, public_key_type> block) {
std::cout << "On accepted block event handled by " << this->id << " at " << get_clock().now() << std::endl;
logger << "On accepted block event handled by " << this->id << " at " << get_clock().now() << std::endl;
}

virtual void restart() {}
Expand Down Expand Up @@ -270,14 +271,14 @@ class TestRunner {
std::stringstream ss;
ss << "[Node] #" << node->id << " ";
auto node_id = ss.str();
std::cout << node_id << "Generating block" << " at " << clock.now() << std::endl;
std::cout << node_id << "LIB " << db.last_irreversible_block_id() << std::endl;
logger << node_id << "Generating block" << " at " << clock.now() << std::endl;
logger << node_id << "LIB " << db.last_irreversible_block_id() << std::endl;
auto head = db.get_master_head();
auto head_block_height = fc::endian_reverse_u32(head->block_id._hash[0]);
std::cout << node_id << "Head block height: " << head_block_height << std::endl;
std::cout << node_id << "Building on top of " << head->block_id << std::endl;
logger << node_id << "Head block height: " << head_block_height << std::endl;
logger << node_id << "Building on top of " << head->block_id << std::endl;
auto new_block_id = generate_block(head_block_height + 1);
std::cout << node_id << "New block: " << new_block_id << std::endl;
logger << node_id << "New block: " << new_block_id << std::endl;
return {head->block_id, {{new_block_id, node->private_key.get_public_key()}}};
}

Expand All @@ -289,9 +290,11 @@ class TestRunner {

vector<int> get_bp_list() {
vector<int> bp_list;
for(int i = 0; i<nodetypes.size(); ++i)
if(nodetypes[i] == node_type::BP)
for(int i = 0; i < nodetypes.size(); ++i) {
if(nodetypes[i] == node_type::BP) {
bp_list.push_back(i);
}
}
return bp_list;
}

Expand Down Expand Up @@ -340,13 +343,13 @@ class TestRunner {
}

void schedule_producers() {
std::cout << "[TaskRunner] Scheduling PRODUCERS " << std::endl;
std::cout << "[TaskRunner] Ordering: " << "[ " ;
logger << "[TaskRunner] Scheduling PRODUCERS " << std::endl;
logger << "[TaskRunner] Ordering: " << "[ " ;
auto ordering = get_ordering();
for (auto x : ordering) {
std::cout << x << " ";
logger << x << " ";
}
std::cout << "]" << std::endl;
logger << "]" << std::endl;
auto now = clock.now();
int instances = ordering.size();

Expand Down Expand Up @@ -387,11 +390,11 @@ class TestRunner {
}
task.at = clock.now() + dist_matrix[node->id][best_peer->id];
task.cb = [best_peer](NodePtr node) {
std::cout << "[Node #" << node->id << "]" " Executing sync " << std::endl;
logger << "[Node #" << node->id << "]" " Executing sync " << std::endl;
const auto& peer_db = best_peer->db;
auto& node_db = node->db;
// sync done
std::cout << "[Node #" << node->id << "]" " best_peer=" << best_peer->id << std::endl;
logger << "[Node #" << node->id << "]" " best_peer=" << best_peer->id << std::endl;

// Copy fork_db and restart
node_db.set_root(deep_copy(peer_db.get_root()));
Expand All @@ -401,7 +404,7 @@ class TestRunner {
auto& pending_chains = node->pending_chains;
while (!pending_chains.empty()) {
auto chain = pending_chains.front();
std::cout << "[Node #" << node->id << "]" " Applying chain " << chain << std::endl;
logger << "[Node #" << node->id << "]" " Applying chain " << chain << std::endl;
pending_chains.pop();
if (!node->apply_chain(chain)) {
break;
Expand All @@ -416,33 +419,33 @@ class TestRunner {
void run() {
init_nodes<TNode>(get_instances());
init_connections();
if (get_bp_list().size() != 0)
add_schedule_task(schedule_time);
assert(get_bp_list().size() != 0);
add_schedule_task(schedule_time);
run_loop();
}

void run_loop() {
std::cout << "[TaskRunner] " << "Run loop " << std::endl;
logger << "[TaskRunner] " << "Run loop " << std::endl;
should_stop = false;
while (!should_stop) {
auto task = timeline.top();
std::cout << "[TaskRunner] " << "current_time=" << task.at << " schedule_time=" << schedule_time << std::endl;
logger << "[TaskRunner] " << "current_time=" << task.at << " schedule_time=" << schedule_time << std::endl;
timeline.pop();
clock.set(task.at);
if (task.to == RUNNER_ID) {
std::cout << "[TaskRunner] Executing task for " << "TaskRunner" << std::endl;
logger << "[TaskRunner] Executing task for " << "TaskRunner" << std::endl;
task.cb(nullptr);
} else {
std::cout << "[TaskRunner] Gotta task for " << task.to << std::endl;
logger << "[TaskRunner] Gotta task for " << task.to << std::endl;
auto node = nodes[task.to];
if (node->should_sync() && task.type != Task::SYNC) {
std::cout << "[TaskRunner] Skipping task cause node is not synchronized" << std::endl;
logger << "[TaskRunner] Skipping task cause node is not synchronized" << std::endl;
} else {
std::cout << "[TaskRunner] Executing task " << std::endl;
logger << "[TaskRunner] Executing task " << std::endl;
task.cb(node);
}
if (node->should_sync()) {
std::cout << "[TaskRunner] Scheduling sync for node " << node->id << std::endl;
logger << "[TaskRunner] Scheduling sync for node " << node->id << std::endl;
schedule_sync(node);
}
}
Expand Down
24 changes: 21 additions & 3 deletions simulator/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
#include <gtest/gtest.h>
#include "log.hpp"
#include <fc/log/logger.hpp>

using namespace std;

constexpr uint32_t random_seed = 42;

int main(int argc, char **argv) {
srand(random_seed);
auto seed = time(NULL);
std::srand(seed);
std::cout << "Random number generator seeded to " << seed << std::endl;

bool is_verbose = false;
std::string verbose_arg = "--verbose";
for (int i = 0; i < argc; i++) {
if (verbose_arg == argv[i]) {
is_verbose = true;
break;
}
}

if (!is_verbose) {
std::cout << "Logging disabled, use --verbose to enable" << std::endl;
logger.enabled(false);
fc::logger::get().set_log_level(fc::log_level::off);
}

::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Loading