Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/noir/eth/rpc/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <noir/eth/rpc/api.h>
#include <fmt/core.h>

namespace noir::eth::api {
namespace noir::eth::rpc {

using namespace noir::codec;

Expand Down Expand Up @@ -197,4 +197,4 @@ fc::variant api::call(const fc::variant& req) {
return fc::variant("0x0");
}

} // namespace noir::eth::api
} // namespace noir::eth::rpc
4 changes: 2 additions & 2 deletions src/noir/eth/rpc/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <noir/tx_pool/tx_pool.h>
#include <fc/variant.hpp>

namespace noir::eth::api {
namespace noir::eth::rpc {

class api {
public:
Expand Down Expand Up @@ -55,4 +55,4 @@ class api {
std::shared_ptr<noir::consensus::block_store> block_store_ptr;
};

} // namespace noir::eth::api
} // namespace noir::eth::rpc
38 changes: 19 additions & 19 deletions src/noir/eth/rpc/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace noir::eth::rpc {

using namespace appbase;

rpc::rpc(appbase::application& app): plugin(app), api(std::make_unique<api::api>()){};
rpc::rpc(appbase::application& app): plugin(app), api_(std::make_unique<api>()){};

void rpc::set_program_options(CLI::App& config) {
auto eth_options = config.add_section("eth", "Ethereum Configuration");
Expand All @@ -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<uint256_t>();
auto allow_unprotected_txs = eth_options->get_option("--rpc-allow-unprotected-txs")->as<bool>();

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<tx_pool::tx_pool>();
api->set_tx_pool_ptr(tx_poor_ptr);
api_->set_tx_pool_ptr(tx_poor_ptr);

auto& block_store_ptr = app.get_plugin<consensus::abci>().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<noir::rpc::jsonrpc>().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() {}
Expand Down
2 changes: 1 addition & 1 deletion src/noir/eth/rpc/rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class rpc : public appbase::plugin<rpc> {
void plugin_shutdown();

private:
std::unique_ptr<api::api> api;
std::unique_ptr<api> api_;
};

} // namespace noir::eth::rpc
12 changes: 6 additions & 6 deletions src/noir/eth/rpc/test/api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <noir/eth/rpc/api.h>

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;
Expand All @@ -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<string> 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<uint32_t> 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");
}
}
4 changes: 2 additions & 2 deletions src/noir/rpc/jsonrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using namespace noir::jsonrpc;

class jsonrpc_impl : std::enable_shared_from_this<jsonrpc_impl> {
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())
Expand Down Expand Up @@ -45,7 +45,7 @@ class jsonrpc_impl : std::enable_shared_from_this<jsonrpc_impl> {

app.get_plugin<rpc>().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 = "{}";
Expand Down
2 changes: 1 addition & 1 deletion src/noir/rpc/jsonrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace noir::rpc {
*/
class jsonrpc : public appbase::plugin<jsonrpc> {
public:
jsonrpc(appbase::application&);
explicit jsonrpc(appbase::application&);

APPBASE_PLUGIN_REQUIRES((rpc))
void set_program_options(CLI::App& config) override {}
Expand Down
15 changes: 8 additions & 7 deletions src/noir/rpc/websocket/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<message_handler>(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<fc::variant> 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<fc::variant> 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) {
Expand All @@ -61,7 +62,7 @@ void websocket::handle_message(
websocketpp::server<websocketpp::config::asio>::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");
}
Expand Down
6 changes: 3 additions & 3 deletions src/noir/rpc/websocket/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
namespace noir::rpc {

using message_sender = std::function<void(std::optional<fc::variant>)>;
using message_handler = std::function<void(std::string, message_sender)>;
using internal_message_handler =
std::function<void(websocketpp::server<websocketpp::config::asio>::connection_ptr, std::string, message_sender)>;
using message_handler = std::function<void(std::string, std::string, message_sender)>;
using internal_message_handler = std::function<void(
websocketpp::server<websocketpp::config::asio>::connection_ptr, std::string, std::string, message_sender)>;

using ws_server_type = websocketpp::server<websocketpp::config::asio>;

Expand Down
1 change: 1 addition & 0 deletions src/noir/tendermint/rpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ target_sources(noir
PRIVATE
mempool.cpp
rpc.cpp
api.cpp
)
69 changes: 69 additions & 0 deletions src/noir/tendermint/rpc/api.cpp
Original file line number Diff line number Diff line change
@@ -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 <noir/tendermint/rpc/api.h>

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<char> 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<char> 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<char> 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<uint32_t>();
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<char> 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
30 changes: 30 additions & 0 deletions src/noir/tendermint/rpc/api.h
Original file line number Diff line number Diff line change
@@ -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 <noir/tendermint/rpc/mempool.h>
#include <noir/tendermint/rpc/responses.h>
#include <fc/variant.hpp>

namespace noir::tendermint::rpc {
class api {
public:
api(): mempool_(std::make_unique<rpc::mempool>()) {}

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<rpc::mempool> mempool_;
};
} // namespace noir::tendermint::rpc
12 changes: 6 additions & 6 deletions src/noir/tendermint/rpc/mempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
4 changes: 2 additions & 2 deletions src/noir/tendermint/rpc/mempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/noir/tendermint/rpc/responses.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright (c) 2022 Haderech Pte. Ltd.
// SPDX-License-Identifier: AGPL-3.0-or-later
//
#pragma once
#include <noir/common/helper/variant.h>
#include <noir/common/refl.h>
#include <tendermint/abci/types.pb.h>
Expand Down
Loading