Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Resolve #6: Get P2P working
Browse files Browse the repository at this point in the history
Yeah, P2P works now, as far as I can tell. Nodes connect, sync, and stay
in sync. Not rigorously tested yet, but the basics are there.
  • Loading branch information
nathanielhourt committed Apr 19, 2017
1 parent b0d596b commit bdd2c9a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion libraries/appbase
8 changes: 4 additions & 4 deletions libraries/net/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@

//#define ENABLE_DEBUG_ULOGS

//#ifdef DEFAULT_LOGGER
//# undef DEFAULT_LOGGER
//#endif
//#define DEFAULT_LOGGER "p2p"
#ifdef DEFAULT_LOGGER
# undef DEFAULT_LOGGER
#endif
#define DEFAULT_LOGGER "p2p"

#define P2P_IN_DEDICATED_THREAD 1

Expand Down
2 changes: 2 additions & 0 deletions plugins/p2p_plugin/include/eos/p2p_plugin/p2p_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class p2p_plugin : public appbase::plugin<p2p_plugin> {
virtual void plugin_startup();
virtual void plugin_shutdown();

void broadcast_block(const chain::signed_block& block);

private:
std::unique_ptr<class p2p_plugin_impl> my;
};
Expand Down
15 changes: 12 additions & 3 deletions plugins/p2p_plugin/p2p_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class p2p_plugin_impl {

// node_delegate interface
virtual bool has_item(const net::item_id& id) override;
virtual bool handle_block(const net::block_message& blk_msg, bool sync_mode, std::vector<fc::uint160_t>& contained_transaction_message_ids) override;
virtual bool handle_block(const net::block_message& blk_msg, bool sync_mode, std::vector<fc::uint160_t>&) override;
virtual void handle_transaction(const net::trx_message& trx_msg) override;
virtual void handle_message(const net::message& message_to_process) override;
virtual std::vector<net::item_hash_t> get_block_ids(const std::vector<net::item_hash_t>& blockchain_synopsis, uint32_t& remaining_item_count, uint32_t limit) override;
Expand Down Expand Up @@ -127,10 +127,16 @@ void p2p_plugin::plugin_startup() {
}

void p2p_plugin::plugin_shutdown() {
ilog("Shutting down P2P Plugin");
my->node->close();
my->node.reset();
}

void p2p_plugin::broadcast_block(const chain::signed_block& block) {
ilog("Broadcasting block #${n}", ("n", block.block_num()));
my->node->broadcast(eos::net::block_message(block));
}

////////////////////////////// Begin node_delegate Implementation //////////////////////////////
bool p2p_plugin_impl::node_delegate::has_item(const net::item_id& id) {
switch (id.item_type) {
Expand All @@ -146,8 +152,9 @@ bool p2p_plugin_impl::node_delegate::has_item(const net::item_id& id) {
}

bool p2p_plugin_impl::node_delegate::handle_block(const net::block_message& blk_msg, bool sync_mode,
std::vector<fc::uint160_t>& contained_transaction_message_ids) {
std::vector<fc::uint160_t>&) {
try {
if (!sync_mode) ilog("Received block ${num} from peer", ("num", blk_msg.block.block_num()));
return chain.accept_block(blk_msg.block, sync_mode);
} catch (eos::chain::unlinkable_block_exception& e) {
FC_THROW_EXCEPTION(net::unlinkable_block_exception, "Unlinkable block: ${e}", ("e", e.to_detail_string()));
Expand Down Expand Up @@ -185,8 +192,10 @@ vector<net::item_hash_t> p2p_plugin_impl::node_delegate::get_block_ids(const std
vector<net::item_hash_t> ids;
ids.reserve(last_block_num_to_return - last_known_block_num);
for (unsigned num = last_known_block_num; num <= last_block_num_to_return; ++num)
ids.emplace_back(chain.db().get_block_id_for_num(num));
if (BOOST_LIKELY(num > 0))
ids.emplace_back(chain.db().get_block_id_for_num(num));

ilog("Exiting ${f} with ${r}", ("f", __FUNCTION__)("r", ids));
return ids;
} FC_CAPTURE_AND_RETHROW((blockchain_synopsis)) }

Expand Down
3 changes: 2 additions & 1 deletion plugins/producer_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ file(GLOB HEADERS "include/eos/producer_plugin/*.hpp")

add_library( producer_plugin
producer_plugin.cpp
${HEADERS}
)

target_link_libraries( producer_plugin chain_plugin appbase eos_chain eos_utilities )
target_link_libraries( producer_plugin p2p_plugin chain_plugin appbase eos_chain eos_utilities )
target_include_directories( producer_plugin
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" )

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#pragma once

#include <eos/chain_plugin/chain_plugin.hpp>
#include <eos/p2p_plugin/p2p_plugin.hpp>

#include <appbase/application.hpp>

Expand All @@ -46,7 +47,7 @@ namespace block_production_condition {

class producer_plugin : public appbase::plugin<producer_plugin> {
public:
APPBASE_PLUGIN_REQUIRES((chain_plugin))
APPBASE_PLUGIN_REQUIRES((chain_plugin)(p2p_plugin))

producer_plugin();
virtual ~producer_plugin();
Expand Down
4 changes: 1 addition & 3 deletions plugins/producer_plugin/producer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,7 @@ block_production_condition::block_production_condition_enum producer_plugin_impl
);
capture("n", block.block_num())("t", block.timestamp)("c", now);

// fc::async( [this,block](){ p2p_node().broadcast(net::block_message(block)); } );
#warning TODO: broadcast the new block

app().get_plugin<p2p_plugin>().broadcast_block(block);
return block_production_condition::produced;
}

Expand Down

0 comments on commit bdd2c9a

Please sign in to comment.