fix(chat): commit global names with signed node and member profiles
This commit is contained in:
1 parent
0656f24a7c
commit
c73090759f
8 files changed
+278
-45
No files matched your search
@@ -1260,18 +1260,23 @@ void topo_node_sqlite_update_rtt(sqlite3* db, uint64_t node_id, uint16_t rtt) {
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t topo_node_sqlite_snapshot_timestamp(sqlite3* db, uint64_t node_id) {
|
||||
int topo_node_sqlite_snapshot_timestamp(sqlite3* db, uint64_t node_id, uint64_t* out) {
|
||||
if (!db || !node_id || !out) { DEBUG_ERROR(DEBUG_CATEGORY_BGP, "snapshot timestamp: invalid arguments"); return -1; }
|
||||
sqlite3_stmt* stmt = NULL;
|
||||
uint64_t timestamp = 0;
|
||||
if (sqlite3_prepare_v2(db, "SELECT timestamp FROM node_snapshots WHERE node_id=?", -1, &stmt, NULL) != SQLITE_OK) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "snapshot timestamp: %s", sqlite3_errmsg(db)); return 0;
|
||||
int rc = sqlite3_prepare_v2(db, "SELECT timestamp FROM node_snapshots WHERE node_id=?", -1, &stmt, NULL);
|
||||
if (rc == SQLITE_OK) rc = sqlite3_bind_int64(stmt, 1, (sqlite3_int64)node_id);
|
||||
if (rc == SQLITE_OK) rc = sqlite3_step(stmt);
|
||||
if (rc == SQLITE_ROW) {
|
||||
if (sqlite3_column_type(stmt, 0) != SQLITE_INTEGER || sqlite3_column_int64(stmt, 0) <= 0) rc = SQLITE_CORRUPT;
|
||||
else timestamp = (uint64_t)sqlite3_column_int64(stmt, 0);
|
||||
}
|
||||
sqlite3_bind_int64(stmt, 1, (sqlite3_int64)node_id);
|
||||
int rc = sqlite3_step(stmt);
|
||||
if (rc == SQLITE_ROW) timestamp = (uint64_t)sqlite3_column_int64(stmt, 0);
|
||||
else if (rc != SQLITE_DONE) DEBUG_ERROR(DEBUG_CATEGORY_BGP, "snapshot timestamp read: %s", sqlite3_errmsg(db));
|
||||
sqlite3_finalize(stmt);
|
||||
return timestamp;
|
||||
int final = sqlite3_finalize(stmt);
|
||||
if ((rc != SQLITE_ROW && rc != SQLITE_DONE) || final != SQLITE_OK) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "snapshot timestamp: node=%016llx rc=%d finalize=%d: %s",
|
||||
(unsigned long long)node_id, rc, final, sqlite3_errmsg(db)); return -1;
|
||||
}
|
||||
*out = timestamp; return 0;
|
||||
}
|
||||
|
||||
int topo_node_sqlite_snapshot_put(sqlite3* db, struct TOPO_NODE* ni) {
|
||||
@@ -1280,20 +1285,35 @@ int topo_node_sqlite_snapshot_put(sqlite3* db, struct TOPO_NODE* ni) {
|
||||
struct TOPO_GROUP_NODE nq = {0};
|
||||
int len = topo_node_serialize(ni, &nq, 0, 0, record, sizeof(record), 0);
|
||||
if (len < 0) { DEBUG_ERROR(DEBUG_CATEGORY_BGP, "snapshot serialization failed"); return -1; }
|
||||
int own = sqlite3_get_autocommit(db);
|
||||
int rc = sqlite3_exec(db, "SAVEPOINT node_snapshot", NULL, NULL, NULL);
|
||||
if (rc != SQLITE_OK) { DEBUG_ERROR(DEBUG_CATEGORY_BGP, "snapshot begin rc=%d: %s", rc, sqlite3_errmsg(db)); return -1; }
|
||||
sqlite3_stmt* stmt = NULL;
|
||||
const char* sql = "INSERT INTO node_snapshots(node_id,timestamp,record) VALUES(?,?,?) "
|
||||
"ON CONFLICT(node_id) DO UPDATE SET timestamp=excluded.timestamp,record=excluded.record "
|
||||
"WHERE excluded.timestamp > node_snapshots.timestamp";
|
||||
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_int64(stmt, 1, (sqlite3_int64)ni->node_id);
|
||||
sqlite3_bind_int64(stmt, 2, (sqlite3_int64)ni->timestamp);
|
||||
sqlite3_bind_blob(stmt, 3, record, len, SQLITE_STATIC);
|
||||
rc = sqlite3_step(stmt);
|
||||
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
|
||||
if (rc == SQLITE_OK) rc = sqlite3_bind_int64(stmt, 1, (sqlite3_int64)ni->node_id);
|
||||
if (rc == SQLITE_OK) rc = sqlite3_bind_int64(stmt, 2, (sqlite3_int64)ni->timestamp);
|
||||
if (rc == SQLITE_OK) rc = sqlite3_bind_blob(stmt, 3, record, len, SQLITE_STATIC);
|
||||
if (rc == SQLITE_OK) rc = sqlite3_step(stmt);
|
||||
int final = sqlite3_finalize(stmt);
|
||||
if (rc == SQLITE_DONE && final == SQLITE_OK) {
|
||||
rc = sqlite3_exec(db, "RELEASE node_snapshot", NULL, NULL, NULL);
|
||||
if (rc == SQLITE_OK) return 0;
|
||||
}
|
||||
if (rc != SQLITE_DONE) DEBUG_ERROR(DEBUG_CATEGORY_BGP, "snapshot write: %s", sqlite3_errmsg(db));
|
||||
sqlite3_finalize(stmt);
|
||||
return rc == SQLITE_DONE ? 0 : -1;
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "snapshot write: node=%016llx rc=%d finalize=%d: %s",
|
||||
(unsigned long long)ni->node_id, rc, final, sqlite3_errmsg(db));
|
||||
if (!sqlite3_get_autocommit(db)) {
|
||||
int rollback = sqlite3_exec(db, "ROLLBACK TO node_snapshot", NULL, NULL, NULL);
|
||||
if (rollback == SQLITE_OK) rollback = sqlite3_exec(db, "RELEASE node_snapshot", NULL, NULL, NULL);
|
||||
if (rollback != SQLITE_OK) DEBUG_ERROR(DEBUG_CATEGORY_BGP, "snapshot rollback rc=%d own=%d: %s", rollback, own, sqlite3_errmsg(db));
|
||||
if (own && !sqlite3_get_autocommit(db)) {
|
||||
rollback = sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL);
|
||||
if (rollback != SQLITE_OK) DEBUG_ERROR(DEBUG_CATEGORY_BGP, "snapshot full rollback rc=%d: %s", rollback, sqlite3_errmsg(db));
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct TOPO_NODE* topo_node_sqlite_snapshot_load(sqlite3* db, struct TOPO_GROUPS* groups, uint64_t node_id) {
|
||||
|
||||
Reference in new issue
Block a user