From ec30390853532a504413e300f39de6b413c17473 Mon Sep 17 00:00:00 2001 From: code0xff Date: Wed, 30 Mar 2022 14:14:15 +0800 Subject: [PATCH 1/4] refactor: separate detail from rpc to api --- src/noir/eth/rpc/api.cpp | 4 +- src/noir/eth/rpc/api.h | 4 +- src/noir/eth/rpc/rpc.cpp | 38 +++++++------- src/noir/eth/rpc/rpc.h | 2 +- src/noir/rpc/jsonrpc.cpp | 2 +- src/noir/rpc/jsonrpc.h | 2 +- src/noir/tendermint/rpc/CMakeLists.txt | 1 + src/noir/tendermint/rpc/api.cpp | 69 ++++++++++++++++++++++++++ src/noir/tendermint/rpc/api.h | 30 +++++++++++ src/noir/tendermint/rpc/mempool.cpp | 12 ++--- src/noir/tendermint/rpc/mempool.h | 4 +- src/noir/tendermint/rpc/responses.h | 1 + src/noir/tendermint/rpc/rpc.cpp | 61 +++-------------------- src/noir/tendermint/rpc/rpc.h | 6 +-- 14 files changed, 146 insertions(+), 90 deletions(-) create mode 100644 src/noir/tendermint/rpc/api.cpp create mode 100644 src/noir/tendermint/rpc/api.h diff --git a/src/noir/eth/rpc/api.cpp b/src/noir/eth/rpc/api.cpp index 79c08f91..c2bdc1c1 100644 --- a/src/noir/eth/rpc/api.cpp +++ b/src/noir/eth/rpc/api.cpp @@ -11,7 +11,7 @@ #include #include -namespace noir::eth::api { +namespace noir::eth::rpc { using namespace noir::codec; @@ -197,4 +197,4 @@ fc::variant api::call(const fc::variant& req) { return fc::variant("0x0"); } -} // namespace noir::eth::api +} // namespace noir::eth::rpc diff --git a/src/noir/eth/rpc/api.h b/src/noir/eth/rpc/api.h index 54e0b60f..51951dfa 100644 --- a/src/noir/eth/rpc/api.h +++ b/src/noir/eth/rpc/api.h @@ -8,7 +8,7 @@ #include #include -namespace noir::eth::api { +namespace noir::eth::rpc { class api { public: @@ -55,4 +55,4 @@ class api { std::shared_ptr block_store_ptr; }; -} // namespace noir::eth::api +} // namespace noir::eth::rpc diff --git a/src/noir/eth/rpc/rpc.cpp b/src/noir/eth/rpc/rpc.cpp index 3c73e256..6bf637dd 100644 --- a/src/noir/eth/rpc/rpc.cpp +++ b/src/noir/eth/rpc/rpc.cpp @@ -11,7 +11,7 @@ namespace noir::eth::rpc { using namespace appbase; -rpc::rpc(appbase::application& app): plugin(app), api(std::make_unique()){}; +rpc::rpc(appbase::application& app): plugin(app), api_(std::make_unique()){}; void rpc::set_program_options(CLI::App& config) { auto eth_options = config.add_section("eth", "Ethereum Configuration"); @@ -31,34 +31,34 @@ void rpc::plugin_initialize(const CLI::App& config) { auto tx_fee_cap = eth_options->get_option("--rpc-tx-fee-cap")->as(); auto allow_unprotected_txs = eth_options->get_option("--rpc-allow-unprotected-txs")->as(); - api->set_tx_fee_cap(tx_fee_cap); - api->set_allow_unprotected_txs(allow_unprotected_txs); + api_->set_tx_fee_cap(tx_fee_cap); + api_->set_allow_unprotected_txs(allow_unprotected_txs); auto tx_poor_ptr = app.find_plugin(); - api->set_tx_pool_ptr(tx_poor_ptr); + api_->set_tx_pool_ptr(tx_poor_ptr); auto& block_store_ptr = app.get_plugin().node_->block_store_; - api->set_block_store(block_store_ptr); + api_->set_block_store(block_store_ptr); } void rpc::plugin_startup() { ilog("starting ethereum rpc"); auto& endpoint = app.get_plugin().get_or_create_endpoint("/eth"); - endpoint.add_handler("eth_sendRawTransaction", [&](auto& req) { return api->send_raw_tx(req); }); - endpoint.add_handler("eth_chainId", [&](auto& req) { return api->chain_id(req); }); - endpoint.add_handler("net_version", [&](auto& req) { return api->net_version(req); }); - endpoint.add_handler("net_listening", [&](auto& req) { return api->net_listening(req); }); - endpoint.add_handler("eth_getBalance", [&](auto& req) { return api->get_balance(req); }); - endpoint.add_handler("eth_getTransactionCount", [&](auto& req) { return api->get_tx_count(req); }); - endpoint.add_handler("eth_blockNumber", [&](auto& req) { return api->block_number(req); }); - endpoint.add_handler("eth_gasPrice", [&](auto& req) { return api->gas_price(req); }); - endpoint.add_handler("eth_estimateGas", [&](auto& req) { return api->estimate_gas(req); }); - endpoint.add_handler("eth_getTransactionByHash", [&](auto& req) { return api->get_tx_by_hash(req); }); - endpoint.add_handler("eth_getBlockByNumber", [&](auto& req) { return api->get_block_by_number(req); }); - endpoint.add_handler("eth_getBlockByHash", [&](auto& req) { return api->get_block_by_hash(req); }); - endpoint.add_handler("eth_getTransactionReceipt", [&](auto& req) { return api->get_tx_receipt(req); }); - endpoint.add_handler("eth_call", [&](auto& req) { return api->call(req); }); + endpoint.add_handler("eth_sendRawTransaction", [&](auto& req) { return api_->send_raw_tx(req); }); + endpoint.add_handler("eth_chainId", [&](auto& req) { return api_->chain_id(req); }); + endpoint.add_handler("net_version", [&](auto& req) { return api_->net_version(req); }); + endpoint.add_handler("net_listening", [&](auto& req) { return api_->net_listening(req); }); + endpoint.add_handler("eth_getBalance", [&](auto& req) { return api_->get_balance(req); }); + endpoint.add_handler("eth_getTransactionCount", [&](auto& req) { return api_->get_tx_count(req); }); + endpoint.add_handler("eth_blockNumber", [&](auto& req) { return api_->block_number(req); }); + endpoint.add_handler("eth_gasPrice", [&](auto& req) { return api_->gas_price(req); }); + endpoint.add_handler("eth_estimateGas", [&](auto& req) { return api_->estimate_gas(req); }); + endpoint.add_handler("eth_getTransactionByHash", [&](auto& req) { return api_->get_tx_by_hash(req); }); + endpoint.add_handler("eth_getBlockByNumber", [&](auto& req) { return api_->get_block_by_number(req); }); + endpoint.add_handler("eth_getBlockByHash", [&](auto& req) { return api_->get_block_by_hash(req); }); + endpoint.add_handler("eth_getTransactionReceipt", [&](auto& req) { return api_->get_tx_receipt(req); }); + endpoint.add_handler("eth_call", [&](auto& req) { return api_->call(req); }); } void rpc::plugin_shutdown() {} diff --git a/src/noir/eth/rpc/rpc.h b/src/noir/eth/rpc/rpc.h index 6d5a8a8e..850ecc7c 100644 --- a/src/noir/eth/rpc/rpc.h +++ b/src/noir/eth/rpc/rpc.h @@ -24,7 +24,7 @@ class rpc : public appbase::plugin { void plugin_shutdown(); private: - std::unique_ptr api; + std::unique_ptr api_; }; } // namespace noir::eth::rpc diff --git a/src/noir/rpc/jsonrpc.cpp b/src/noir/rpc/jsonrpc.cpp index 754c99e7..e1b2a14e 100644 --- a/src/noir/rpc/jsonrpc.cpp +++ b/src/noir/rpc/jsonrpc.cpp @@ -13,7 +13,7 @@ using namespace noir::jsonrpc; class jsonrpc_impl : std::enable_shared_from_this { public: - jsonrpc_impl(appbase::application& app): app(app) {} + explicit jsonrpc_impl(appbase::application& app): app(app) {} endpoint& get_or_create_endpoint(const std::string& url) { if (endpoints.find(url) != endpoints.end()) diff --git a/src/noir/rpc/jsonrpc.h b/src/noir/rpc/jsonrpc.h index d8c58742..4af019ad 100644 --- a/src/noir/rpc/jsonrpc.h +++ b/src/noir/rpc/jsonrpc.h @@ -18,7 +18,7 @@ namespace noir::rpc { */ class jsonrpc : public appbase::plugin { public: - jsonrpc(appbase::application&); + explicit jsonrpc(appbase::application&); APPBASE_PLUGIN_REQUIRES((rpc)) void set_program_options(CLI::App& config) override {} diff --git a/src/noir/tendermint/rpc/CMakeLists.txt b/src/noir/tendermint/rpc/CMakeLists.txt index bc919678..950fa44a 100644 --- a/src/noir/tendermint/rpc/CMakeLists.txt +++ b/src/noir/tendermint/rpc/CMakeLists.txt @@ -2,4 +2,5 @@ target_sources(noir PRIVATE mempool.cpp rpc.cpp + api.cpp ) diff --git a/src/noir/tendermint/rpc/api.cpp b/src/noir/tendermint/rpc/api.cpp new file mode 100644 index 00000000..f68a737d --- /dev/null +++ b/src/noir/tendermint/rpc/api.cpp @@ -0,0 +1,69 @@ +// This file is part of NOIR. +// +// Copyright (c) 2022 Haderech Pte. Ltd. +// SPDX-License-Identifier: AGPL-3.0-or-later +// +#include + +namespace noir::tendermint::rpc { + +using namespace fc; + +fc::variant api::broadcast_tx_async(const variant& req) { + auto tx = req.get_object()["tx"].as_string(); + auto d = base64_decode(tx); + std::vector raw_tx(d.begin(), d.end()); + auto result = mempool_->broadcast_tx_async(raw_tx); + variant res; + to_variant(result, res); + return res; +} + +fc::variant api::broadcast_tx_sync(const variant& req) { + auto enc_tx = req.get_object()["tx"].as_string(); + auto d = base64_decode(enc_tx); + std::vector raw_tx(d.begin(), d.end()); + auto result = mempool_->broadcast_tx_sync(raw_tx); + variant res; + to_variant(result, res); + return res; +} + +fc::variant api::broadcast_tx_commit(const variant& req) { + // auto enc_tx = req.get_object()["tx"].as_string(); + // auto d = fc::base64_decode(enc_tx); + // std::vector raw_tx(d.begin(), d.end()); + // auto result = mempool_->broadcast_tx_commit(raw_tx); + // variant res; + // to_variant(result, res); + // return res; + check(false, "not implemented yet"); + return fc::variant(nullptr); +} + +fc::variant api::unconfirmed_txs(const variant& req) { + auto limit = req.get_object()["limit"].as(); + auto result = mempool_->unconfirmed_txs(limit); + variant res; + to_variant(result, res); + return res; +} + +fc::variant api::num_unconfirmed_txs(const variant& req) { + auto result = mempool_->num_unconfirmed_txs(); + variant res; + to_variant(result, res); + return res; +} + +fc::variant api::check_tx(const variant& req) { + auto enc_tx = req.get_object()["tx"].as_string(); + auto d = base64_decode(enc_tx); + std::vector raw_tx(d.begin(), d.end()); + auto result = mempool_->check_tx(raw_tx); + variant res; + to_variant(result, res); + return res; +} + +} // namespace noir::tendermint::rpc diff --git a/src/noir/tendermint/rpc/api.h b/src/noir/tendermint/rpc/api.h new file mode 100644 index 00000000..9a463ecc --- /dev/null +++ b/src/noir/tendermint/rpc/api.h @@ -0,0 +1,30 @@ +// This file is part of NOIR. +// +// Copyright (c) 2022 Haderech Pte. Ltd. +// SPDX-License-Identifier: AGPL-3.0-or-later +// +#pragma once +#include +#include +#include + +namespace noir::tendermint::rpc { +class api { +public: + api(): mempool_(std::make_unique()) {} + + fc::variant broadcast_tx_async(const fc::variant& req); + fc::variant broadcast_tx_sync(const fc::variant& req); + fc::variant broadcast_tx_commit(const fc::variant& req); + fc::variant unconfirmed_txs(const fc::variant& req); + fc::variant num_unconfirmed_txs(const fc::variant& req); + fc::variant check_tx(const fc::variant& req); + + void set_tx_pool_ptr(noir::tx_pool::tx_pool* tx_pool_ptr) { + mempool_->set_tx_pool_ptr(tx_pool_ptr); + } + +private: + std::unique_ptr mempool_; +}; +} // namespace noir::tendermint::rpc diff --git a/src/noir/tendermint/rpc/mempool.cpp b/src/noir/tendermint/rpc/mempool.cpp index ee91a75b..47e14e4a 100644 --- a/src/noir/tendermint/rpc/mempool.cpp +++ b/src/noir/tendermint/rpc/mempool.cpp @@ -16,13 +16,13 @@ using namespace noir::consensus; using tx = bytes; result_broadcast_tx mempool::broadcast_tx_async(const tx& t) { - tx_pool_ptr->check_tx_async(t); + tx_poor_->check_tx_async(t); auto tx_hash = get_tx_hash(t); return result_broadcast_tx{.hash = tx_hash}; } result_broadcast_tx mempool::broadcast_tx_sync(const tx& t) { - auto r = tx_pool_ptr->check_tx_sync(t); + auto r = tx_poor_->check_tx_sync(t); return result_broadcast_tx{.code = r.code, .data = r.data, .log = r.log, @@ -37,17 +37,17 @@ result_broadcast_tx mempool::broadcast_tx_sync(const tx& t) { //} result_unconfirmed_txs mempool::unconfirmed_txs(const uint32_t& limit_ptr) { - auto txs = tx_pool_ptr->reap_max_txs(limit_ptr); + auto txs = tx_poor_->reap_max_txs(limit_ptr); return result_unconfirmed_txs{ - .count = txs.size(), .total = tx_pool_ptr->size(), .total_bytes = tx_pool_ptr->size_bytes(), .txs = txs}; + .count = txs.size(), .total = tx_poor_->size(), .total_bytes = tx_poor_->size_bytes(), .txs = txs}; } result_unconfirmed_txs mempool::num_unconfirmed_txs() { return result_unconfirmed_txs{ - .count = tx_pool_ptr->size(), .total = tx_pool_ptr->size(), .total_bytes = tx_pool_ptr->size_bytes()}; + .count = tx_poor_->size(), .total = tx_poor_->size(), .total_bytes = tx_poor_->size_bytes()}; } response_check_tx& mempool::check_tx(const tx& t) { - return tx_pool_ptr->proxy_app_->check_tx_sync(consensus::request_check_tx{.tx = t}); + return tx_poor_->proxy_app_->check_tx_sync(consensus::request_check_tx{.tx = t}); } } // namespace noir::tendermint::rpc diff --git a/src/noir/tendermint/rpc/mempool.h b/src/noir/tendermint/rpc/mempool.h index fd5f91fb..d410145f 100644 --- a/src/noir/tendermint/rpc/mempool.h +++ b/src/noir/tendermint/rpc/mempool.h @@ -19,11 +19,11 @@ class mempool { noir::consensus::response_check_tx& check_tx(const bytes& tx); void set_tx_pool_ptr(noir::tx_pool::tx_pool* tx_pool_ptr) { - this->tx_pool_ptr = tx_pool_ptr; + tx_poor_ = tx_pool_ptr; } private: - noir::tx_pool::tx_pool* tx_pool_ptr; + noir::tx_pool::tx_pool* tx_poor_; }; } // namespace noir::tendermint::rpc diff --git a/src/noir/tendermint/rpc/responses.h b/src/noir/tendermint/rpc/responses.h index cffe1d7c..935acec0 100644 --- a/src/noir/tendermint/rpc/responses.h +++ b/src/noir/tendermint/rpc/responses.h @@ -3,6 +3,7 @@ // Copyright (c) 2022 Haderech Pte. Ltd. // SPDX-License-Identifier: AGPL-3.0-or-later // +#pragma once #include #include #include diff --git a/src/noir/tendermint/rpc/rpc.cpp b/src/noir/tendermint/rpc/rpc.cpp index 29d93c23..6881903f 100644 --- a/src/noir/tendermint/rpc/rpc.cpp +++ b/src/noir/tendermint/rpc/rpc.cpp @@ -12,7 +12,7 @@ using namespace fc; using namespace noir; using namespace noir::rpc; -rpc::rpc(appbase::application& app): plugin(app), mempool_(std::make_shared()) {} +rpc::rpc(appbase::application& app): plugin(app), api_(std::make_unique()) {} void rpc::set_program_options(CLI::App& config) {} @@ -20,64 +20,19 @@ void rpc::plugin_initialize(const CLI::App& config) { ilog("initializing tendermint rpc"); auto tx_poor_ptr = app.find_plugin(); - mempool_->set_tx_pool_ptr(tx_poor_ptr); + api_->set_tx_pool_ptr(tx_poor_ptr); } void rpc::plugin_startup() { ilog("starting tendermint rpc"); auto& endpoint = app.get_plugin().get_or_create_endpoint("/tendermint"); - endpoint.add_handler("broadcast_tx_async", [&](auto& req) { - auto tx = req.get_object()["tx"].as_string(); - auto d = base64_decode(tx); - std::vector raw_tx(d.begin(), d.end()); - auto result = mempool_->broadcast_tx_async(raw_tx); - variant res; - to_variant(result, res); - return res; - }); - endpoint.add_handler("broadcast_tx_sync", [&](auto& req) { - auto enc_tx = req.get_object()["tx"].as_string(); - auto d = base64_decode(enc_tx); - std::vector raw_tx(d.begin(), d.end()); - auto result = mempool_->broadcast_tx_sync(raw_tx); - variant res; - to_variant(result, res); - return res; - }); - endpoint.add_handler("broadcast_tx_commit", [&](auto& req) { - // auto enc_tx = req.get_object()["tx"].as_string(); - // auto d = fc::base64_decode(enc_tx); - // std::vector raw_tx(d.begin(), d.end()); - // auto result = mempool_->broadcast_tx_commit(raw_tx); - // variant res; - // to_variant(result, res); - // return res; - check(false, "not implemented yet"); - return fc::variant(nullptr); - }); - endpoint.add_handler("unconfirmed_txs", [&](const fc::variant& req) { - auto limit = req.get_object()["limit"].as(); - auto result = mempool_->unconfirmed_txs(limit); - variant res; - to_variant(result, res); - return fc::variant(res); - }); - endpoint.add_handler("num_unconfirmed_txs", [&](auto& req) { - auto result = mempool_->num_unconfirmed_txs(); - variant res; - to_variant(result, res); - return res; - }); - endpoint.add_handler("check_tx", [&](auto& req) { - auto enc_tx = req.get_object()["tx"].as_string(); - auto d = base64_decode(enc_tx); - std::vector raw_tx(d.begin(), d.end()); - auto result = mempool_->check_tx(raw_tx); - variant res; - to_variant(result, res); - return res; - }); + endpoint.add_handler("broadcast_tx_async", [&](auto& req) { return api_->broadcast_tx_async(req); }); + endpoint.add_handler("broadcast_tx_sync", [&](auto& req) { return api_->broadcast_tx_sync(req); }); + endpoint.add_handler("broadcast_tx_commit", [&](auto& req) { return api_->broadcast_tx_commit(req); }); + endpoint.add_handler("unconfirmed_txs", [&](auto& req) { return api_->unconfirmed_txs(req); }); + endpoint.add_handler("num_unconfirmed_txs", [&](auto& req) { return api_->num_unconfirmed_txs(req); }); + endpoint.add_handler("check_tx", [&](auto& req) { return api_->check_tx(req); }); } void rpc::plugin_shutdown() {} diff --git a/src/noir/tendermint/rpc/rpc.h b/src/noir/tendermint/rpc/rpc.h index ea17d5a1..f909c58f 100644 --- a/src/noir/tendermint/rpc/rpc.h +++ b/src/noir/tendermint/rpc/rpc.h @@ -5,7 +5,7 @@ // #pragma once #include -#include +#include #include namespace noir::tendermint::rpc { @@ -14,7 +14,7 @@ class rpc : public appbase::plugin { public: explicit rpc(appbase::application& app); - APPBASE_PLUGIN_REQUIRES((noir::rpc::rpc)(noir::rpc::jsonrpc)) + APPBASE_PLUGIN_REQUIRES((noir::rpc::jsonrpc)(noir::tx_pool::tx_pool)) void set_program_options(CLI::App& config) override; void plugin_initialize(const CLI::App& config); @@ -22,7 +22,7 @@ class rpc : public appbase::plugin { void plugin_shutdown(); private: - std::shared_ptr mempool_; + std::unique_ptr api_; }; } // namespace noir::tendermint::rpc From 363150a1604cf2741a2ceb18cc2994da7220195b Mon Sep 17 00:00:00 2001 From: code0xff Date: Wed, 30 Mar 2022 17:21:37 +0800 Subject: [PATCH 2/4] fix: add url param on websocket message_handler --- src/noir/rpc/jsonrpc.cpp | 2 +- src/noir/rpc/websocket/websocket.cpp | 15 ++++++++------- src/noir/rpc/websocket/websocket.h | 6 +++--- src/noir/tendermint/rpc/rpc.h | 3 ++- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/noir/rpc/jsonrpc.cpp b/src/noir/rpc/jsonrpc.cpp index e1b2a14e..b5eaf5ce 100644 --- a/src/noir/rpc/jsonrpc.cpp +++ b/src/noir/rpc/jsonrpc.cpp @@ -45,7 +45,7 @@ class jsonrpc_impl : std::enable_shared_from_this { app.get_plugin().add_ws_api({ {url, - [&](std::string payload, message_sender sender) mutable { + [&](std::string url, std::string payload, message_sender sender) mutable { try { if (payload.empty()) payload = "{}"; diff --git a/src/noir/rpc/websocket/websocket.cpp b/src/noir/rpc/websocket/websocket.cpp index cf5297fe..1fbf17e8 100644 --- a/src/noir/rpc/websocket/websocket.cpp +++ b/src/noir/rpc/websocket/websocket.cpp @@ -26,18 +26,19 @@ void websocket::add_message_handler(const std::string& path, message_handler& ha internal_message_handler websocket::make_app_thread_message_handler( appbase::application& app, message_handler next, int priority) { auto next_ptr = std::make_shared(std::move(next)); - return - [&app, priority, next_ptr = std::move(next_ptr)](connection_ptr conn, const string& payload, message_sender then) { - message_sender wrapped_then = [then = std::move(then)](std::optional msg) { then(std::move(msg)); }; + return [&app, priority, next_ptr = std::move(next_ptr)]( + connection_ptr conn, const string& url, const string& payload, message_sender then) { + message_sender wrapped_then = [then = std::move(then)](std::optional msg) { then(std::move(msg)); }; - app.post(priority, [next_ptr, conn = std::move(conn), payload, wrapped_then = std::move(wrapped_then)]() mutable { + app.post( + priority, [next_ptr, conn = std::move(conn), url, payload, wrapped_then = std::move(wrapped_then)]() mutable { try { - (*next_ptr)(payload, std::move(wrapped_then)); + (*next_ptr)(url, payload, std::move(wrapped_then)); } catch (...) { conn->send("Internal Server Error"); } }); - }; + }; } message_sender websocket::make_message_sender(appbase::application& app, connection_ptr conn, int priority) { @@ -61,7 +62,7 @@ void websocket::handle_message( websocketpp::server::connection_ptr conn, ws_server_type::message_ptr msg) { std::string resource = conn->get_uri()->get_resource(); if (message_handlers.contains(resource)) { - message_handlers[resource](conn, msg->get_payload(), make_message_sender(app, conn)); + message_handlers[resource](conn, resource, msg->get_payload(), make_message_sender(app, conn)); } else { conn->send("Unknown Endpoint"); } diff --git a/src/noir/rpc/websocket/websocket.h b/src/noir/rpc/websocket/websocket.h index fc15071c..e1cb2b85 100644 --- a/src/noir/rpc/websocket/websocket.h +++ b/src/noir/rpc/websocket/websocket.h @@ -13,9 +13,9 @@ namespace noir::rpc { using message_sender = std::function)>; -using message_handler = std::function; -using internal_message_handler = - std::function::connection_ptr, std::string, message_sender)>; +using message_handler = std::function; +using internal_message_handler = std::function::connection_ptr, std::string, std::string, message_sender)>; using ws_server_type = websocketpp::server; diff --git a/src/noir/tendermint/rpc/rpc.h b/src/noir/tendermint/rpc/rpc.h index f909c58f..752154c9 100644 --- a/src/noir/tendermint/rpc/rpc.h +++ b/src/noir/tendermint/rpc/rpc.h @@ -4,8 +4,9 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // #pragma once -#include +#include #include +#include #include namespace noir::tendermint::rpc { From e4d0ced6e228ebe6f6b69df8528f959a0d5cee00 Mon Sep 17 00:00:00 2001 From: code0xff Date: Wed, 30 Mar 2022 17:22:06 +0800 Subject: [PATCH 3/4] feat: add tendermint ws api --- src/noir/tendermint/rpc/rpc.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/noir/tendermint/rpc/rpc.cpp b/src/noir/tendermint/rpc/rpc.cpp index 6881903f..51663d96 100644 --- a/src/noir/tendermint/rpc/rpc.cpp +++ b/src/noir/tendermint/rpc/rpc.cpp @@ -3,7 +3,6 @@ // Copyright (c) 2022 Haderech Pte. Ltd. // SPDX-License-Identifier: AGPL-3.0-or-later // -#include #include namespace noir::tendermint::rpc { @@ -33,6 +32,14 @@ void rpc::plugin_startup() { endpoint.add_handler("unconfirmed_txs", [&](auto& req) { return api_->unconfirmed_txs(req); }); endpoint.add_handler("num_unconfirmed_txs", [&](auto& req) { return api_->num_unconfirmed_txs(req); }); endpoint.add_handler("check_tx", [&](auto& req) { return api_->check_tx(req); }); + + auto& ws_endpoint = app.get_plugin().get_or_create_ws_endpoint("/tendermint"); + ws_endpoint.add_handler("broadcast_tx_async", [&](auto& req) { return api_->broadcast_tx_async(req); }); + ws_endpoint.add_handler("broadcast_tx_sync", [&](auto& req) { return api_->broadcast_tx_sync(req); }); + ws_endpoint.add_handler("broadcast_tx_commit", [&](auto& req) { return api_->broadcast_tx_commit(req); }); + ws_endpoint.add_handler("unconfirmed_txs", [&](auto& req) { return api_->unconfirmed_txs(req); }); + ws_endpoint.add_handler("num_unconfirmed_txs", [&](auto& req) { return api_->num_unconfirmed_txs(req); }); + ws_endpoint.add_handler("check_tx", [&](auto& req) { return api_->check_tx(req); }); } void rpc::plugin_shutdown() {} From f51733155962a39598e28d90fba535a7a528b65e Mon Sep 17 00:00:00 2001 From: code0xff Date: Wed, 30 Mar 2022 18:09:19 +0800 Subject: [PATCH 4/4] test: modify eth api namespace --- src/noir/eth/rpc/test/api_test.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/noir/eth/rpc/test/api_test.cpp b/src/noir/eth/rpc/test/api_test.cpp index e6af8173..593cb4e0 100644 --- a/src/noir/eth/rpc/test/api_test.cpp +++ b/src/noir/eth/rpc/test/api_test.cpp @@ -7,7 +7,7 @@ #include using namespace std; -using namespace noir::eth::api; +using namespace noir::eth::rpc; TEST_CASE("eth:params: check_params_size", "[eth][api]") { fc::variants vs; @@ -22,22 +22,22 @@ TEST_CASE("eth:params: check_params_size", "[eth][api]") { TEST_CASE("eth:params: send_raw_tx", "[eth][api]") { fc::variant params; - api a; + api api_; SECTION("check params fail") { params = fc::variant(1); - CHECK_THROWS_WITH(a.send_raw_tx(params), "invalid json request"); + CHECK_THROWS_WITH(api_.send_raw_tx(params), "invalid json request"); vector v; params = fc::variant(v); - CHECK_THROWS_WITH(a.send_raw_tx(params), "missing value for required argument 0"); + CHECK_THROWS_WITH(api_.send_raw_tx(params), "missing value for required argument 0"); v = {"0x1", "0x2"}; params = fc::variant(v); - CHECK_THROWS_WITH(a.send_raw_tx(params), "too many arguments, want at most 1"); + CHECK_THROWS_WITH(api_.send_raw_tx(params), "too many arguments, want at most 1"); } SECTION("check param fail") { vector v = {0}; params = fc::variant(v); - CHECK_THROWS_WITH(a.send_raw_tx(params), "invalid parameters: json: cannot unmarshal"); + CHECK_THROWS_WITH(api_.send_raw_tx(params), "invalid parameters: json: cannot unmarshal"); } }