Add registered ons entry counts to get_info RPC

This commit is contained in:
Jason Rhinelander 2021-06-09 11:36:56 -03:00
parent e32cbcbccd
commit 5de150a475
5 changed files with 51 additions and 17 deletions

View File

@ -51,6 +51,7 @@ enum struct ons_sql_type
get_mappings,
get_mappings_by_owner,
get_mappings_by_owners,
get_mapping_counts,
get_owner,
get_setting,
get_sentinel_end,
@ -455,27 +456,32 @@ bool sql_run_statement(ons_sql_type type, sql_compiled_statement& statement, voi
}
break;
case ons_sql_type::get_mappings_by_owners: /* FALLTHRU */
case ons_sql_type::get_mappings_by_owner: /* FALLTHRU */
case ons_sql_type::get_mappings: /* FALLTHRU */
case ons_sql_type::get_mappings_by_owners: [[fallthrough]];
case ons_sql_type::get_mappings_by_owner: [[fallthrough]];
case ons_sql_type::get_mappings: [[fallthrough]];
case ons_sql_type::get_mapping:
{
if (mapping_record tmp_entry = sql_get_mapping_from_statement(statement))
{
data_loaded = true;
if (type == ons_sql_type::get_mapping)
{
auto *entry = reinterpret_cast<mapping_record *>(context);
*entry = std::move(tmp_entry);
}
*static_cast<mapping_record *>(context) = std::move(tmp_entry);
else
{
auto *records = reinterpret_cast<std::vector<mapping_record> *>(context);
records->emplace_back(std::move(tmp_entry));
}
static_cast<std::vector<mapping_record>*>(context)->push_back(std::move(tmp_entry));
}
}
break;
case ons_sql_type::get_mapping_counts:
{
auto& counts = *static_cast<std::map<mapping_type, int>*>(context);
std::underlying_type_t<mapping_type> type_val;
int count;
get(statement, 0, type_val);
get(statement, 1, count);
counts.emplace(static_cast<mapping_type>(type_val), count);
data_loaded = true;
}
}
}
break;
@ -1578,6 +1584,7 @@ CREATE TABLE IF NOT EXISTS "settings" (
CREATE TABLE IF NOT EXISTS "mappings" ()" + mappings_columns + R"();
CREATE INDEX IF NOT EXISTS "owner_id_index" ON mappings("owner_id");
CREATE INDEX IF NOT EXISTS "backup_owner_id_index" ON mappings("backup_owner_index");
CREATE INDEX IF NOT EXISTS "mapping_type_name_exp" ON "mappings" ("type", "name_hash", "expiration_height" DESC);
)";
char *table_err_msg = nullptr;
@ -1626,6 +1633,7 @@ DROP TABLE "mappings_old";
CREATE UNIQUE INDEX "name_type_update" ON "mappings" ("name_hash", "type", "update_height" DESC);
CREATE INDEX "owner_id_index" ON mappings("owner_id");
CREATE INDEX "backup_owner_id_index" ON mappings("backup_owner_index");
CREATE INDEX "mapping_type_name_exp" ON "mappings" ("type", "name_hash", "expiration_height" DESC);
COMMIT TRANSACTION;
)";
@ -1723,13 +1731,19 @@ bool name_system_db::init(cryptonote::Blockchain const *blockchain, cryptonote::
this->db = db;
this->nettype = nettype;
std::string const get_mappings_by_owner_str = sql_select_mappings_and_owners_prefix
std::string const GET_MAPPINGS_BY_OWNER_STR = sql_select_mappings_and_owners_prefix
+ R"(WHERE ? IN ("o1"."address", "o2"."address"))"
+ sql_select_mappings_and_owners_suffix;
std::string const get_mapping_str = sql_select_mappings_and_owners_prefix
std::string const GET_MAPPING_STR = sql_select_mappings_and_owners_prefix
+ R"(WHERE "type" = ? AND "name_hash" = ?)"
+ sql_select_mappings_and_owners_suffix;
const std::string GET_MAPPING_COUNTS_STR = R"(
SELECT type, COUNT(*) FROM (
SELECT DISTINCT type, name_hash FROM mappings WHERE )" + std::string{EXPIRATION} + R"(
)
GROUP BY type)";
std::string const RESOLVE_STR =
R"(SELECT "encrypted_value", MAX("update_height")
FROM "mappings"
@ -1834,8 +1848,9 @@ AND NOT EXISTS (SELECT * FROM "mappings" WHERE "owner"."id" = "mappings"."back
// Prepare commonly executed sql statements
//
// ---------------------------------------------------------------------------
if (!get_mappings_by_owner_sql.compile(get_mappings_by_owner_str) ||
!get_mapping_sql.compile(get_mapping_str) ||
if (!get_mappings_by_owner_sql.compile(GET_MAPPINGS_BY_OWNER_STR) ||
!get_mapping_sql.compile(GET_MAPPING_STR) ||
!get_mapping_counts_sql.compile(GET_MAPPING_COUNTS_STR) ||
!resolve_sql.compile(RESOLVE_STR) ||
!get_owner_by_id_sql.compile(GET_OWNER_BY_ID_STR) ||
!get_owner_by_key_sql.compile(GET_OWNER_BY_KEY_STR) ||
@ -2395,6 +2410,12 @@ std::vector<mapping_record> name_system_db::get_mappings_by_owner(generic_owner
return result;
}
std::map<mapping_type, int> name_system_db::get_mapping_counts(uint64_t blockchain_height) {
std::map<mapping_type, int> result;
bind_and_run(ons_sql_type::get_mapping_counts, get_mapping_counts_sql, &result, blockchain_height);
return result;
}
settings_record name_system_db::get_settings()
{
settings_record result = {};

View File

@ -305,6 +305,9 @@ struct name_system_db
std::vector<mapping_record> get_mappings_by_owners(std::vector<generic_owner> const &keys, std::optional<uint64_t> blockchain_height = std::nullopt);
settings_record get_settings ();
// Returns the count of each type of ONS registration that is currently active.
std::map<mapping_type, int> get_mapping_counts(uint64_t blockchain_height);
// Resolves a mapping of the given type and name hash. Returns a null optional if the value was
// not found or expired, otherwise returns the encrypted value.
std::optional<mapping_value> resolve(mapping_type type, std::string_view name_hash_b64, uint64_t blockchain_height);
@ -333,6 +336,7 @@ private:
sql_compiled_statement prune_mappings_sql{*this};
sql_compiled_statement prune_owners_sql{*this};
sql_compiled_statement get_mappings_by_owner_sql{*this};
sql_compiled_statement get_mapping_counts_sql{*this};
sql_compiled_statement get_mappings_on_height_and_newer_sql{*this};
};

View File

@ -417,6 +417,13 @@ namespace cryptonote { namespace rpc {
res.block_size_limit = res.block_weight_limit = m_core.get_blockchain_storage().get_current_cumulative_block_weight_limit();
res.block_size_median = res.block_weight_median = m_core.get_blockchain_storage().get_current_cumulative_block_weight_median();
auto ons_counts = m_core.get_blockchain_storage().name_system_db().get_mapping_counts(res.height);
res.ons_counts = {
ons_counts[ons::mapping_type::session],
ons_counts[ons::mapping_type::wallet],
ons_counts[ons::mapping_type::lokinet]};
if (context.admin)
{
res.service_node = m_core.service_node();

View File

@ -314,9 +314,10 @@ KV_SERIALIZE_MAP_CODE_BEGIN(GET_INFO::response)
KV_SERIALIZE(immutable_block_hash)
KV_SERIALIZE(cumulative_difficulty)
KV_SERIALIZE(block_size_limit)
KV_SERIALIZE_OPT(block_weight_limit, (uint64_t)0)
KV_SERIALIZE(block_weight_limit)
KV_SERIALIZE(block_size_median)
KV_SERIALIZE_OPT(block_weight_median, (uint64_t)0)
KV_SERIALIZE(block_weight_median)
KV_SERIALIZE(ons_counts)
KV_SERIALIZE(start_time)
KV_SERIALIZE(service_node)
KV_SERIALIZE(last_storage_server_ping)

View File

@ -640,6 +640,7 @@ namespace rpc {
uint64_t block_weight_limit; // Maximum allowed block weight.
uint64_t block_size_median; // Median block size of latest 100 blocks.
uint64_t block_weight_median; // Median block weight of latest 100 blocks.
std::array<int, 3> ons_counts; // ONS registration counts, [session, wallet, lokinet]
std::optional<bool> service_node; // Will be true if the node is running in --service-node mode.
std::optional<uint64_t> start_time; // Start time of the daemon, as UNIX time.
std::optional<uint64_t> last_storage_server_ping; // Last ping time of the storage server (0 if never or not running as a service node)