mirror of
https://github.com/EvernodeXRPL/sashimono.git
synced 2026-04-29 15:38:00 +00:00
Single reputation account for multiple host support (#393)
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
namespace sqlite
|
||||
{
|
||||
constexpr const char *COLUMN_DATA_TYPES[]{"INT", "TEXT", "BLOB"};
|
||||
constexpr const char *ALTER_TABLE = "ALTER TABLE ";
|
||||
constexpr const char *CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ";
|
||||
constexpr const char *CREATE_INDEX = "CREATE INDEX ";
|
||||
constexpr const char *CREATE_UNIQUE_INDEX = "CREATE UNIQUE INDEX ";
|
||||
@@ -29,7 +30,7 @@ namespace sqlite
|
||||
"instances WHERE status == ? AND user_port NOT IN"
|
||||
"(SELECT user_port FROM instances WHERE status != ?)";
|
||||
|
||||
constexpr const char *GET_MAX_PORTS_FROM_HP = "SELECT max(peer_port), max(user_port), max(init_gp_tcp_port), max(init_gp_udp_port) FROM instances WHERE status != ?";
|
||||
constexpr const char *GET_MAX_PORTS_FROM_HP = "SELECT peer_port, user_port, init_gp_tcp_port, init_gp_udp_port FROM instances WHERE status != ? ORDER BY peer_port DESC LIMIT 1";
|
||||
|
||||
constexpr const char *UPDATE_STATUS_IN_HP = "UPDATE instances SET status = ? WHERE name = ?";
|
||||
|
||||
@@ -157,6 +158,48 @@ namespace sqlite
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alter a table with given table info.
|
||||
* @param db Pointer to the db.
|
||||
* @param table_name Table name to be altered.
|
||||
* @returns returns 0 on success, or -1 on error.
|
||||
*/
|
||||
int alter_table(sqlite3 *db, std::string_view table_name, const std::vector<table_column_info> &column_info)
|
||||
{
|
||||
std::string sql;
|
||||
|
||||
for (auto itr = column_info.begin(); itr != column_info.end(); ++itr)
|
||||
{
|
||||
sql.append(ALTER_TABLE).append(table_name).append(" ADD COLUMN ");
|
||||
sql.append(itr->name);
|
||||
sql.append(" ");
|
||||
sql.append(COLUMN_DATA_TYPES[itr->column_type]);
|
||||
|
||||
if (itr->is_key)
|
||||
{
|
||||
sql.append(" ");
|
||||
sql.append(PRIMARY_KEY);
|
||||
}
|
||||
|
||||
if (!itr->is_null)
|
||||
{
|
||||
sql.append(" ");
|
||||
sql.append(NOT_NULL);
|
||||
}
|
||||
|
||||
if (itr != column_info.end() - 1)
|
||||
sql.append("; ");
|
||||
}
|
||||
|
||||
const int ret = exec_sql(db, sql);
|
||||
if (ret == -1)
|
||||
{
|
||||
LOG_ERROR << "Error when altering sqlite table " << table_name;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int create_index(sqlite3 *db, std::string_view table_name, std::string_view column_names, const bool is_unique)
|
||||
{
|
||||
std::string index_name = std::string("idx_").append(table_name).append("_").append(column_names);
|
||||
@@ -265,6 +308,28 @@ namespace sqlite
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether column exist in the database.
|
||||
* @param db Pointer to the db.
|
||||
* @param table_name Table name to be checked.
|
||||
* @param column_name Column name to be checked.
|
||||
* @returns returns true is exist, otherwise false.
|
||||
*/
|
||||
bool is_column_exists(sqlite3 *db, std::string_view table_name, std::string_view column_name)
|
||||
{
|
||||
std::string sql;
|
||||
// Reserving the space for the query before construction.
|
||||
sql.reserve(21 + table_name.size() + column_name.size());
|
||||
|
||||
sql.append("SELECT ");
|
||||
sql.append(column_name);
|
||||
sql.append(" FROM ");
|
||||
sql.append(table_name);
|
||||
sql.append(" LIMIT 1");
|
||||
|
||||
return exec_sql(db, sql) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes a connection to a given databse.
|
||||
* @param db Pointer to the db.
|
||||
@@ -314,6 +379,15 @@ namespace sqlite
|
||||
create_index(db, INSTANCE_TABLE, "owner_pubkey", false) == -1) // one user can have multiple instances running.
|
||||
return -1;
|
||||
}
|
||||
else if (!is_column_exists(db, INSTANCE_TABLE, "init_gp_tcp_port")) // TODO: Added because v0.8.4 does not have gp ports.
|
||||
{
|
||||
const std::vector<table_column_info> columns{
|
||||
table_column_info("init_gp_tcp_port", COLUMN_DATA_TYPE::INT),
|
||||
table_column_info("init_gp_udp_port", COLUMN_DATA_TYPE::INT)};
|
||||
|
||||
if (alter_table(db, INSTANCE_TABLE, columns) == -1)
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -426,10 +500,17 @@ namespace sqlite
|
||||
max_ports = {peer_port, user_port, gp_tcp_port_start, gp_udp_port_start};
|
||||
}
|
||||
// Initialize with default config values if either of the ports are zero.
|
||||
if (max_ports.peer_port == 0 || max_ports.user_port == 0 || max_ports.gp_tcp_port_start == 0 || max_ports.gp_udp_port_start == 0)
|
||||
if (max_ports.peer_port == 0 || max_ports.user_port == 0)
|
||||
{
|
||||
max_ports = {(uint16_t)(conf::cfg.hp.init_peer_port - 1), (uint16_t)(conf::cfg.hp.init_user_port - 1), (uint16_t)(conf::cfg.hp.init_gp_tcp_port - 2), (uint16_t)(conf::cfg.hp.init_gp_udp_port - 2)};
|
||||
}
|
||||
else if (max_ports.gp_tcp_port_start == 0 || max_ports.gp_udp_port_start == 0)
|
||||
{
|
||||
const uint16_t increment = ((max_ports.peer_port - conf::cfg.hp.init_peer_port) * 2);
|
||||
const uint16_t gp_tcp_port_start = conf::cfg.hp.init_gp_tcp_port + increment;
|
||||
const uint16_t gp_udp_port_start = conf::cfg.hp.init_gp_udp_port + increment;
|
||||
max_ports = {max_ports.user_port, max_ports.peer_port, gp_tcp_port_start, gp_udp_port_start};
|
||||
}
|
||||
|
||||
// Finalize and distroys the statement.
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
Reference in New Issue
Block a user