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

Add started_elections topic to websockets #3851

Merged
merged 37 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
732bf2e
add started_election websocket topic and core_test
Jul 8, 2022
88175db
Merge branch 'nanocurrency:develop' into develop_started_elections
gr0vity-dev Jul 9, 2022
02988c4
clang format
Jul 9, 2022
8bc810a
Merge branch 'develop_started_elections' of https://github.com/gr0vit…
Jul 9, 2022
761b649
initial centos cleanup
Mar 2, 2022
9869810
Fixes to let it run in the CI
May 4, 2022
753142f
Changes the repo to be passed as argument
May 16, 2022
234e15d
Removes the changelog generation from the RPM workflow
May 20, 2022
2cc4f03
Sets the workflow scripts to use its ref/repo
May 23, 2022
3fd31ee
Better name for the RPM release variable
May 23, 2022
d277eb5
Improve maksrc build-centos.sh scripts
Jun 6, 2022
423f536
Missing S3_BUILD_DIRECTORY var definition
Jun 9, 2022
7c07474
Fix the checksum basenames
Jun 13, 2022
a21caaf
confirmation_height_clear cli account param (#3836)
JerzyStanislawski Jun 24, 2022
30ebf6c
Removes references to Travis CI from nano-node (#3755)
thsfs Jun 27, 2022
e17545d
Bound the unchecked store after the initial bootstrap amount has been…
clemahieu Apr 11, 2022
ac999f2
If multiple handshake queries are received then close the socket
dsiganos Apr 26, 2022
0bc1cf8
Only allow 2 entries to have the same dependency.
clemahieu May 14, 2022
88a107c
Disable vote_processor flushing within the request loop which can blo…
clemahieu May 19, 2022
8567a2d
Set inactive votes cache size to 0 as it causes excessive vote relaying.
clemahieu May 19, 2022
86c63f8
Bugfix: correctly check for magic bytes and network in message header
dsiganos May 22, 2022
b9bd38e
Remove a debug assert that crashes the node on receipt of zero node ID
dsiganos May 27, 2022
b0b4250
Improves the vote processor class and limits its flush() (#142)
thsfs May 27, 2022
d7a5828
Vote hinting reenable (#152)
pwojcikdev Jun 9, 2022
d22ac4c
Removing unchecked.for_each_par as it's effectively unused.
clemahieu Jun 8, 2022
ce9e261
Converting functions on unchecked_map that return iterators to instea…
clemahieu Jun 8, 2022
71ec1d7
Reimplementing nano::unchecked_store::get in terms of unchecked_store…
clemahieu Jun 8, 2022
17f281b
Adding a memory container for blocks once the initial bootstrap thres…
clemahieu Jun 8, 2022
bf54411
Commenting the transition from disk to memory.
clemahieu Jun 9, 2022
cfe3357
Cleanup of the leftover code
pwojcikdev Jun 10, 2022
769019f
Fix use after delete in cleanup_election()
dsiganos Jun 8, 2022
94b913f
Formatting.
clemahieu Jun 27, 2022
5111bfc
Block constructors to builders conversion in unit tests (#3841)
JerzyStanislawski Jul 8, 2022
288baeb
Adds a ledger_context class and ledger_empty function which can be us…
clemahieu Jul 8, 2022
5e0a46e
clang format
Jul 9, 2022
438a5c2
Merge branch 'develop_started_elections' of https://github.com/gr0vit…
Jul 9, 2022
34cbd37
use block_builder and standard initialisation syntax
Jul 9, 2022
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
48 changes: 48 additions & 0 deletions nano/core_test/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,54 @@ TEST (websocket, confirmation)
ASSERT_TIMELY (5s, future.wait_for (0s) == std::future_status::ready);
}

// Tests getting notification of a started election
TEST (websocket, started_election)
{
nano::system system;
nano::node_config config (nano::get_available_port (), system.logging);
config.websocket_config.enabled = true;
config.websocket_config.port = nano::get_available_port ();
auto node1 = system.add_node (config);

std::atomic<bool> ack_ready{ false };
auto task = ([&ack_ready, config, &node1] () {
fake_websocket_client client (node1->websocket_server->listening_port ());
client.send_message (R"json({"action": "subscribe", "topic": "started_election", "ack": "true"})json");
client.await_ack ();
ack_ready = true;
EXPECT_EQ (1, node1->websocket_server->subscriber_count (nano::websocket::topic::started_election));
return client.get_response ();
});
auto future = std::async (std::launch::async, task);

ASSERT_TIMELY (5s, ack_ready);

// Create election, causing a websocket message to be emitted
nano::keypair key1;
nano::block_builder builder;
auto send1 = builder
.send ()
.previous (nano::dev::genesis->hash ())
.destination (key1.pub)
.balance (0)
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (*system.work.generate (nano::dev::genesis->hash ()))
.build_shared ();
nano::publish publish1{ nano::dev::network_params.network, send1 };
auto channel1 = node1->network.udp_channels.create (node1->network.endpoint ());
clemahieu marked this conversation as resolved.
Show resolved Hide resolved
node1->network.inbound (publish1, channel1);
ASSERT_TIMELY (1s, node1->active.election (send1->qualified_root ()));
ASSERT_TIMELY (5s, future.wait_for (0s) == std::future_status::ready);

auto response = future.get ();
ASSERT_TRUE (response);
boost::property_tree::ptree event;
std::stringstream stream;
stream << response.get ();
boost::property_tree::read_json (stream, event);
ASSERT_EQ (event.get<std::string> ("topic"), "started_election");
}

// Tests getting notification of an erased election
TEST (websocket, stopped_election)
{
Expand Down
1 change: 1 addition & 0 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ nano::election_insertion_result nano::active_transactions::insert_impl (nano::un
auto const cache = find_inactive_votes_cache_impl (hash);
lock_a.unlock ();
result.election->insert_inactive_votes_cache (cache);
node.observers.active_started.notify (hash);
node.stats.inc (nano::stat::type::election, nano::stat::detail::election_start);
vacancy_update ();
}
Expand Down
8 changes: 8 additions & 0 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ nano::node::node (boost::asio::io_context & io_ctx_a, boost::filesystem::path co
}
});

observers.active_started.add ([this] (nano::block_hash const & hash_a) {
if (this->websocket_server->any_subscriber (nano::websocket::topic::started_election))
{
nano::websocket::message_builder builder;
this->websocket_server->broadcast (builder.started_election (hash_a));
}
});

observers.active_stopped.add ([this] (nano::block_hash const & hash_a) {
if (this->websocket_server->any_subscriber (nano::websocket::topic::stopped_election))
{
Expand Down
1 change: 1 addition & 0 deletions nano/node/node_observers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ std::unique_ptr<nano::container_info_component> nano::collect_container_info (na
composite->add_component (collect_container_info (node_observers.blocks, "blocks"));
composite->add_component (collect_container_info (node_observers.wallet, "wallet"));
composite->add_component (collect_container_info (node_observers.vote, "vote"));
composite->add_component (collect_container_info (node_observers.active_started, "active_started"));
composite->add_component (collect_container_info (node_observers.active_stopped, "active_stopped"));
composite->add_component (collect_container_info (node_observers.account_balance, "account_balance"));
composite->add_component (collect_container_info (node_observers.endpoint, "endpoint"));
Expand Down
1 change: 1 addition & 0 deletions nano/node/node_observers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class node_observers final
blocks_t blocks;
nano::observer_set<bool> wallet;
nano::observer_set<std::shared_ptr<nano::vote>, std::shared_ptr<nano::transport::channel>, nano::vote_code> vote;
nano::observer_set<nano::block_hash const &> active_started;
nano::observer_set<nano::block_hash const &> active_stopped;
nano::observer_set<nano::account const &, bool> account_balance;
nano::observer_set<std::shared_ptr<nano::transport::channel>> endpoint;
Expand Down
20 changes: 20 additions & 0 deletions nano/node/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ nano::websocket::topic to_topic (std::string const & topic_a)
{
topic = nano::websocket::topic::confirmation;
}
else if (topic_a == "started_election")
{
topic = nano::websocket::topic::started_election;
}
else if (topic_a == "stopped_election")
{
topic = nano::websocket::topic::stopped_election;
Expand Down Expand Up @@ -414,6 +418,10 @@ std::string from_topic (nano::websocket::topic topic_a)
{
topic = "confirmation";
}
else if (topic_a == nano::websocket::topic::started_election)
{
topic = "started_election";
}
else if (topic_a == nano::websocket::topic::stopped_election)
{
topic = "stopped_election";
Expand Down Expand Up @@ -689,6 +697,18 @@ void nano::websocket::listener::decrease_subscriber_count (nano::websocket::topi
count -= 1;
}

nano::websocket::message nano::websocket::message_builder::started_election (nano::block_hash const & hash_a)
{
nano::websocket::message message_l (nano::websocket::topic::started_election);
set_common_fields (message_l);

boost::property_tree::ptree message_node_l;
message_node_l.add ("hash", hash_a.to_string ());
message_l.contents.add_child ("message", message_node_l);

return message_l;
}

nano::websocket::message nano::websocket::message_builder::stopped_election (nano::block_hash const & hash_a)
{
nano::websocket::message message_l (nano::websocket::topic::stopped_election);
Expand Down
3 changes: 3 additions & 0 deletions nano/node/websocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace websocket
ack,
/** A confirmation message */
confirmation,
/** Started election message*/
started_election,
/** Stopped election message (dropped elections due to bounding or block lost the elections) */
stopped_election,
/** A vote message **/
Expand Down Expand Up @@ -81,6 +83,7 @@ namespace websocket
{
public:
message block_confirmed (std::shared_ptr<nano::block> const & block_a, nano::account const & account_a, nano::amount const & amount_a, std::string subtype, bool include_block, nano::election_status const & election_status_a, std::vector<nano::vote_with_weight_info> const & election_votes_a, nano::websocket::confirmation_options const & options_a);
message started_election (nano::block_hash const & hash_a);
message stopped_election (nano::block_hash const & hash_a);
message vote_received (std::shared_ptr<nano::vote> const & vote_a, nano::vote_code code_a);
message work_generation (nano::work_version const version_a, nano::block_hash const & root_a, uint64_t const work_a, uint64_t const difficulty_a, uint64_t const publish_threshold_a, std::chrono::milliseconds const & duration_a, std::string const & peer_a, std::vector<std::string> const & bad_peers_a, bool const completed_a = true, bool const cancelled_a = false);
Expand Down