Skip to content

Commit

Permalink
Merge pull request #153 from BitHaru/master
Browse files Browse the repository at this point in the history
add alias
  • Loading branch information
BitHaru authored Sep 28, 2021
2 parents 24d6cac + 6c8a848 commit 78174ea
Show file tree
Hide file tree
Showing 22 changed files with 684 additions and 45 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ add_library(xxhash
xxhash/xxhash.c
xxhash/xxhash.h)


add_subdirectory(rai/app)
add_subdirectory(rai/custom)
add_subdirectory(rai/secure)
add_subdirectory(rai/common)
Expand All @@ -234,6 +234,7 @@ add_subdirectory(rai/rai_node)
add_subdirectory(rai/wallet)
add_subdirectory(rai/rai_airdrop)
add_subdirectory(rai/rai_wallet_rpc)
add_subdirectory(rai/rai_alias)

if (RAI_GUI)
if (WIN32)
Expand Down
11 changes: 5 additions & 6 deletions rai/app/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ rai::AppTrace::AppTrace()
{
}

rai::App::App(rai::ErrorCode& error_code, rai::Alarm& alarm,
boost::asio::io_service& service,
const boost::filesystem::path& data_path,
rai::App::App(rai::ErrorCode& error_code, boost::asio::io_service& service,
const boost::filesystem::path& db_path, rai::Alarm& alarm,
const rai::AppConfig& config, rai::AppSubscriptions& subscribe,
const std::vector<rai::BlockType>& account_types,
const rai::Provider::Info& provider_info)
: alarm_(alarm),
service_(service),
: service_(service),
alarm_(alarm),
config_(config),
subscribe_(subscribe),
store_(error_code, data_path / "app_data.ldb"),
store_(error_code, db_path),
ledger_(error_code, store_, false),
account_types_(account_types),
service_runner_(service_gateway_),
Expand Down
6 changes: 3 additions & 3 deletions rai/app/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ enum class AppActionPri : uint32_t
class App : public std::enable_shared_from_this<rai::App>
{
public:
App(rai::ErrorCode&, rai::Alarm&, boost::asio::io_service&,
const boost::filesystem::path&, const rai::AppConfig&,
App(rai::ErrorCode&, boost::asio::io_service&,
const boost::filesystem::path&, rai::Alarm&, const rai::AppConfig&,
rai::AppSubscriptions&, const std::vector<rai::BlockType>&,
const rai::Provider::Info&);
virtual ~App() = default;
Expand Down Expand Up @@ -119,8 +119,8 @@ class App : public std::enable_shared_from_this<rai::App>
static size_t constexpr MAX_BLOCK_CACHE_SIZE = 100 * 1024;
static uint64_t constexpr BLOCKS_QUERY_COUNT = 100;

rai::Alarm& alarm_;
boost::asio::io_service& service_;
rai::Alarm& alarm_;
const rai::AppConfig& config_;
rai::AppSubscriptions& subscribe_;
rai::Store store_;
Expand Down
26 changes: 18 additions & 8 deletions rai/app/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ std::string DefaultNodeGateway()

rai::AppConfig::AppConfig(uint16_t ws_port)
: node_gateway_("wss"),
ws_enable_(true),
ws_ip_(boost::asio::ip::address_v4::loopback()),
ws_port_(ws_port)
{
Expand All @@ -41,7 +42,7 @@ rai::AppConfig::AppConfig(uint16_t ws_port)
}

rai::ErrorCode rai::AppConfig::DeserializeJson(bool& update,
rai::Ptree& ptree)
rai::Ptree& ptree)
{
rai::ErrorCode error_code = rai::ErrorCode::UNEXPECTED;
try
Expand All @@ -55,24 +56,30 @@ rai::ErrorCode rai::AppConfig::DeserializeJson(bool& update,
error_code = UpgradeJson(update, version, ptree);
IF_NOT_SUCCESS_RETURN(error_code);

error_code = rai::ErrorCode::JSON_CONFIG_NODE_GATEWAY;
error_code = rai::ErrorCode::JSON_CONFIG_APP_NODE_GATEWAY;
auto node_gateway = ptree.get_optional<std::string>("node_gateway");
if (!node_gateway || node_gateway_.Parse(*node_gateway))
{
return error_code;
}

error_code = rai::ErrorCode::JSON_CONFIG_WS_ADDRESS;
std::string address = ptree.get<std::string>("websocket_address");
error_code = rai::ErrorCode::JSON_CONFIG_APP_WS;
const rai::Ptree& ws_ptree = ptree.get_child("websocket");

error_code = rai::ErrorCode::JSON_CONFIG_APP_WS_ENABLE;
ws_enable_ = ws_ptree.get<bool>("enable");

error_code = rai::ErrorCode::JSON_CONFIG_APP_WS_ADDRESS;
std::string address = ws_ptree.get<std::string>("address");
boost::system::error_code ec;
ws_ip_ = boost::asio::ip::make_address_v4(address, ec);
if (ec)
{
return error_code;
}

error_code = rai::ErrorCode::JSON_CONFIG_WS_PORT;
std::string ws_port_str = ptree.get<std::string>("websocket_port");
error_code = rai::ErrorCode::JSON_CONFIG_APP_WS_PORT;
std::string ws_port_str = ws_ptree.get<std::string>("port");
if (ws_port_str.empty() || rai::StringToUint(ws_port_str, ws_port_))
{
return error_code;
Expand All @@ -89,8 +96,11 @@ void rai::AppConfig::SerializeJson(rai::Ptree& ptree) const
{
ptree.put("version", "1");
ptree.put("node_gateway", node_gateway_.String());
ptree.put("websocket_address", ws_ip_.to_string());
ptree.put("websocket_port", std::to_string(ws_port_));
rai::Ptree ws_ptree;
ws_ptree.put("enable", ws_enable_);
ws_ptree.put("address", ws_ip_);
ws_ptree.put("port", std::to_string(ws_port_));
ptree.add_child("websocket", ws_ptree);
}

rai::ErrorCode rai::AppConfig::UpgradeJson(bool& update, uint32_t version,
Expand Down
1 change: 1 addition & 0 deletions rai/app/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AppConfig
rai::ErrorCode UpgradeJson(bool&, uint32_t, rai::Ptree&) const;

rai::Url node_gateway_;
bool ws_enable_;
rai::IP ws_ip_;
uint16_t ws_port_;
};
Expand Down
76 changes: 76 additions & 0 deletions rai/app/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ void rai::AppRpcHandler::ProcessImpl()
{
BlockCount();
}
else if (action == "clients")
{
Clients();
}
else if (action == "subscription")
{
Subscription();
}
else if (action == "subscription_count")
{
SubscriptionCount();
}
else if (action == "subscription_account_count")
{
SubscriptionAccountCount();
}
else if (action == "subscriptions")
{
Subscriptions();
}
else
{
error_code_ = rai::ErrorCode::RPC_UNKNOWN_ACTION;
Expand Down Expand Up @@ -473,3 +493,59 @@ void rai::AppRpcHandler::BootstrapStatus()
{
app_.bootstrap_.Status(response_);
}

void rai::AppRpcHandler::Subscription()
{
rai::Account account;
bool error = GetAccount_(account);
IF_ERROR_RETURN_VOID(error);

app_.subscribe_.JsonByAccount(account, response_);
}

void rai::AppRpcHandler::Subscriptions()
{
auto client_id_o = request_.get_optional<std::string>("client_id");
if (client_id_o)
{
rai::UniqueId client_id;
bool error = client_id.DecodeHex(*client_id_o);
if (error)
{
error_code_ = rai::ErrorCode::RPC_INVALID_FIELD_CLIENT_ID;
return;
}

app_.subscribe_.JsonByUid(client_id, response_);
}
else
{
app_.subscribe_.Json(response_);
}
}

void rai::AppRpcHandler::SubscriptionCount()
{
auto client_id_o = request_.get_optional<std::string>("client_id");
if (client_id_o)
{
rai::UniqueId client_id;
bool error = client_id.DecodeHex(*client_id_o);
if (error)
{
error_code_ = rai::ErrorCode::RPC_INVALID_FIELD_CLIENT_ID;
return;
}

response_.put("count", std::to_string(app_.subscribe_.Size(client_id)));
}
else
{
response_.put("count", std::to_string(app_.subscribe_.Size()));
}
}

void rai::AppRpcHandler::SubscriptionAccountCount()
{
response_.put("count", std::to_string(app_.subscribe_.AccountSize()));
}
5 changes: 5 additions & 0 deletions rai/app/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class AppRpcHandler : public RpcHandler
void BlockQueryByHash();
void BlockQueryByHeight();
void BootstrapStatus();
void Clients();
void Subscription();
void Subscriptions();
void SubscriptionCount();
void SubscriptionAccountCount();

rai::App& app_;
rai::UniqueId uid_;
Expand Down
103 changes: 103 additions & 0 deletions rai/app/subscribe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ rai::AppSubscriptionData::AppSubscriptionData(): synced_(false)
{
}

void rai::AppSubscriptionData::Json(rai::Ptree& ptree)
{
ptree.put("synchronized", rai::BoolToString(synced_));
SerializeJson(ptree);
}

rai::AppSubscriptions::AppSubscriptions(rai::App& app) : app_(app)
{
}
Expand Down Expand Up @@ -208,3 +214,100 @@ void rai::AppSubscriptions::Unsubscribe(const rai::UniqueId& uid)
}
}
}

size_t rai::AppSubscriptions::Size() const
{
std::lock_guard<std::mutex> lock(mutex_);
return subscriptions_.size();
}

size_t rai::AppSubscriptions::Size(const rai::UniqueId& uid) const
{
std::lock_guard<std::mutex> lock(mutex_);
return subscriptions_.get<rai::AppSubscriptionByUid>().count(uid);
}

size_t rai::AppSubscriptions::AccountSize() const
{
std::lock_guard<std::mutex> lock(mutex_);
return accounts_.size();
}

void rai::AppSubscriptions::Json(rai::Ptree& ptree) const
{
std::unique_lock<std::mutex> lock(mutex_);
rai::Ptree subscriptions;
for (auto i = accounts_.begin(), n = accounts_.end(); i != n; ++i)
{
rai::Ptree entry;
Json_(lock, i->first, entry);
if (entry.empty())
{
continue;
}
subscriptions.push_back(std::make_pair("", entry));
}
ptree.put_child("subscriptions", subscriptions);
}

void rai::AppSubscriptions::JsonByUid(const rai::UniqueId& uid,
rai::Ptree& ptree) const
{
std::unique_lock<std::mutex> lock(mutex_);
rai::Ptree subscriptions;
auto begin = subscriptions_.get<rai::AppSubscriptionByUid>().lower_bound(uid);
auto end = subscriptions_.get<rai::AppSubscriptionByUid>().upper_bound(uid);
for (auto& i = begin; i != end; ++i)
{
rai::Ptree entry;
Json_(lock, i->account_, entry);
if (entry.empty())
{
continue;
}
subscriptions.push_back(std::make_pair("", entry));
}
ptree.put_child("subscriptions", subscriptions);
}

void rai::AppSubscriptions::JsonByAccount(const rai::Account& account,
rai::Ptree& ptree) const
{
std::unique_lock<std::mutex> lock(mutex_);
Json_(lock, account, ptree);
}

void rai::AppSubscriptions::Json_(std::unique_lock<std::mutex>& lock,
const rai::Account& account,
rai::Ptree& ptree) const
{
auto it = accounts_.find(account);
if (it == accounts_.end())
{
return;
}

ptree.put("account", it->first.StringAccount());

rai::Ptree data;
it->second->Json(data);
ptree.put_child("data", data);

rai::Ptree clients;
auto now = std::chrono::steady_clock::now();
std::pair<rai::AppSubscriptionContainer::iterator,
rai::AppSubscriptionContainer::iterator>
it_pair = subscriptions_.equal_range(boost::make_tuple(account));
for (auto& i = it_pair.first; i != it_pair.second; ++i)
{
rai::Ptree entry;
entry.put("uid", i->uid_.StringHex());
auto subscribe_at =
std::chrono::duration_cast<std::chrono::seconds>(now - i->time_)
.count();
entry.put("subscribe_at",
std::to_string(subscribe_at) + " seconds ago");
clients.push_back(std::make_pair("", entry));
}
ptree.put_child("clients", clients);
}
12 changes: 12 additions & 0 deletions rai/app/subscribe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class AppSubscriptionData
{
public:
AppSubscriptionData();
void Json(rai::Ptree&);

virtual void SerializeJson(rai::Ptree&) = 0;

bool synced_;
};
Expand Down Expand Up @@ -73,13 +76,22 @@ class AppSubscriptions
void NotifyAccountSynced(const rai::Account&);
rai::ErrorCode Subscribe(const rai::Account&, const rai::UniqueId&);
void Unsubscribe(const rai::UniqueId&);
size_t Size() const;
size_t Size(const rai::UniqueId&) const;
size_t AccountSize() const;
void Json(rai::Ptree&) const;
void JsonByUid(const rai::UniqueId&, rai::Ptree&) const;
void JsonByAccount(const rai::Account&, rai::Ptree&) const;

rai::App& app_;

static std::chrono::seconds constexpr CUTOFF_TIME =
std::chrono::seconds(900);

private:
void Json_(std::unique_lock<std::mutex>&, const rai::Account&,
rai::Ptree&) const;

mutable std::mutex mutex_;
rai::AppSubscriptionContainer subscriptions_;
std::unordered_map<rai::Account, std::shared_ptr<rai::AppSubscriptionData>>
Expand Down
Loading

0 comments on commit 78174ea

Please sign in to comment.