From 0fcc7e3e024f13406bcddeab20a6a27ccc9b1ed4 Mon Sep 17 00:00:00 2001 From: jejolare Date: Tue, 21 Oct 2025 19:30:21 +0700 Subject: [PATCH 1/4] fix alias upsert --- server/schemes/Alias.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/schemes/Alias.ts b/server/schemes/Alias.ts index 84a3ab8..6ef2b8a 100644 --- a/server/schemes/Alias.ts +++ b/server/schemes/Alias.ts @@ -18,12 +18,12 @@ class Alias extends Model { Alias.init( { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, - alias: { type: DataTypes.STRING, allowNull: true }, - address: { type: DataTypes.STRING, allowNull: true }, + alias: { type: DataTypes.STRING, allowNull: false, unique: true }, + address: { type: DataTypes.STRING, allowNull: false }, comment: { type: DataTypes.STRING, allowNull: true }, tracking_key: { type: DataTypes.STRING, allowNull: true }, - block: { type: DataTypes.STRING, allowNull: true }, - transaction: { type: DataTypes.STRING, allowNull: true }, + block: { type: DataTypes.STRING, allowNull: false }, + transaction: { type: DataTypes.STRING, allowNull: false }, enabled: { type: DataTypes.BOOLEAN, allowNull: true } }, { From a2842c71d648754241a28657b0d7a29411ea81bb Mon Sep 17 00:00:00 2001 From: jejolare Date: Tue, 21 Oct 2025 19:37:11 +0700 Subject: [PATCH 2/4] debug --- server/schemes/Alias.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/schemes/Alias.ts b/server/schemes/Alias.ts index 6ef2b8a..0b47b39 100644 --- a/server/schemes/Alias.ts +++ b/server/schemes/Alias.ts @@ -22,8 +22,8 @@ Alias.init( address: { type: DataTypes.STRING, allowNull: false }, comment: { type: DataTypes.STRING, allowNull: true }, tracking_key: { type: DataTypes.STRING, allowNull: true }, - block: { type: DataTypes.STRING, allowNull: false }, - transaction: { type: DataTypes.STRING, allowNull: false }, + block: { type: DataTypes.STRING, allowNull: true }, + transaction: { type: DataTypes.STRING, allowNull: true }, enabled: { type: DataTypes.BOOLEAN, allowNull: true } }, { From d440ba74049fd98fc5aec3038165c253c7692cb7 Mon Sep 17 00:00:00 2001 From: jejolare Date: Tue, 21 Oct 2025 20:02:36 +0700 Subject: [PATCH 3/4] ass services --- server/server.ts | 62 ++++++++++++++++++--------------- server/services/rpc.service.ts | 46 ++++++++++++++++++++++++ server/services/sync.service.ts | 28 +++++++++++++++ 3 files changed, 108 insertions(+), 28 deletions(-) create mode 100644 server/services/rpc.service.ts create mode 100644 server/services/sync.service.ts diff --git a/server/server.ts b/server/server.ts index f3b9c0d..f8e9bfb 100644 --- a/server/server.ts +++ b/server/server.ts @@ -28,6 +28,7 @@ import bodyParser from 'body-parser'; import { syncHistoricalPrice, syncLatestPrice } from "./services/zanoPrice.service"; import ZanoPrice from "./schemes/ZanoPrice"; import cron from "node-cron"; +import syncService from "./services/sync.service.ts"; // @ts-ignore const __dirname = import.meta.dirname; @@ -1464,36 +1465,36 @@ async function waitForDb() { let tx_info = response.data.result.tx_info; - for (let item of tx_info.extra) { - if (item.type === 'alias_info') { - let arr = item.short_view.split('-->'); - let aliasName = arr[0]; - let aliasAddress = arr[1]; - let aliasComment = parseComment(item.datails_view || item.details_view); - let aliasTrackingKey = parseTrackingKey(item.datails_view || item.details_view); - let aliasBlock = bl.height; - let aliasTransaction = localTr.id; + // for (let item of tx_info.extra) { + // if (item.type === 'alias_info') { + // let arr = item.short_view.split('-->'); + // let aliasName = arr[0]; + // let aliasAddress = arr[1]; + // let aliasComment = parseComment(item.datails_view || item.details_view); + // let aliasTrackingKey = parseTrackingKey(item.datails_view || item.details_view); + // let aliasBlock = bl.height; + // let aliasTransaction = localTr.id; - await Alias.update( - { enabled: 0 }, - { where: { alias: aliasName } } - ); + // await Alias.update( + // { enabled: 0 }, + // { where: { alias: aliasName } } + // ); - try { - await Alias.upsert({ - alias: decodeString(aliasName), - address: aliasAddress, - comment: decodeString(aliasComment), - tracking_key: decodeString(aliasTrackingKey), - block: aliasBlock, - transact: aliasTransaction, - enabled: 1, - }); - } catch (error) { - log(`SyncTransactions() Insert into aliases ERROR: ${error}`); - } - } - } + // try { + // await Alias.upsert({ + // alias: decodeString(aliasName), + // address: aliasAddress, + // comment: decodeString(aliasComment), + // tracking_key: decodeString(aliasTrackingKey), + // block: aliasBlock, + // transact: aliasTransaction, + // enabled: 1, + // }); + // } catch (error) { + // log(`SyncTransactions() Insert into aliases ERROR: ${error}`); + // } + // } + // } for (let item of tx_info.ins) { if (item.global_indexes) { @@ -2112,6 +2113,11 @@ cron.schedule("0 */4 * * *", async () => { await waitForDb(); + + await syncService.syncAliases(); + + await new Promise(resolve => setTimeout(resolve, 5000000)); + if (process.env.RESYNC_ASSETS === "true") { console.log('Resyncing assets'); diff --git a/server/services/rpc.service.ts b/server/services/rpc.service.ts new file mode 100644 index 0000000..35129c8 --- /dev/null +++ b/server/services/rpc.service.ts @@ -0,0 +1,46 @@ +import { config } from "../utils/utils"; + +export interface AliasDetails { + address: string; + alias: string; + comment: string; + tracking_key: string; +} + +class RPCService { + + private async fetchDaemon(method: string, params: object) { + const result = await fetch(config.api, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + "id": 0, + "jsonrpc": "2.0", + "method": method, + "params": params + }) + }).then(res => res.json()).catch(err => { + console.log(err); + return null; + }); + + return result?.result; + } + + async getAllAliasesDetails() { + const allAliases = await this.fetchDaemon("get_all_alias_details", {}); + + if (!allAliases?.aliases || !Array.isArray(allAliases.aliases)) { + throw new Error("Failed to fetch aliases from daemon"); + } + + return allAliases.aliases as AliasDetails[]; + + } +} + +const rpcService = new RPCService(); + +export default rpcService; \ No newline at end of file diff --git a/server/services/sync.service.ts b/server/services/sync.service.ts new file mode 100644 index 0000000..6dd83b2 --- /dev/null +++ b/server/services/sync.service.ts @@ -0,0 +1,28 @@ +import Alias from "../schemes/Alias"; +import rpcService from "./rpc.service"; + +class SyncService { + async syncAliases() { + console.log('fetching all alias details from daemon...'); + const allAliases = await rpcService.getAllAliasesDetails(); + console.log(`Fetched ${allAliases.length} aliases from daemon.`); + + const preparedData = allAliases.map(e => ({ + alias: e.alias, + address: e.address, + comment: e.comment, + tracking_key: e.tracking_key, + block: null, + transaction: null, + enabled: 1 + })); + + Alias.bulkCreate(preparedData, { + ignoreDuplicates: true, + }) + } +} + +const syncService = new SyncService(); + +export default syncService; \ No newline at end of file From 862341ec87097944e7143c81600759e9eb0bed60 Mon Sep 17 00:00:00 2001 From: jejolare Date: Tue, 21 Oct 2025 20:14:03 +0700 Subject: [PATCH 4/4] update alias system --- server/server.ts | 4 +--- server/services/sync.service.ts | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/server/server.ts b/server/server.ts index f8e9bfb..457802b 100644 --- a/server/server.ts +++ b/server/server.ts @@ -2114,9 +2114,7 @@ cron.schedule("0 */4 * * *", async () => { await waitForDb(); - await syncService.syncAliases(); - - await new Promise(resolve => setTimeout(resolve, 5000000)); + await syncService.startSyncDaemon(); if (process.env.RESYNC_ASSETS === "true") { console.log('Resyncing assets'); diff --git a/server/services/sync.service.ts b/server/services/sync.service.ts index 6dd83b2..1f631ba 100644 --- a/server/services/sync.service.ts +++ b/server/services/sync.service.ts @@ -1,3 +1,4 @@ +import sequelize from "../database/sequelize"; import Alias from "../schemes/Alias"; import rpcService from "./rpc.service"; @@ -17,9 +18,22 @@ class SyncService { enabled: 1 })); - Alias.bulkCreate(preparedData, { - ignoreDuplicates: true, - }) + await sequelize.transaction(async (t) => { + await Alias.destroy({ where: {}, transaction: t }); + await Alias.bulkCreate(preparedData, { transaction: t }); + }); + } + + async startSyncDaemon() { + while (true) { + try { + await this.syncAliases(); + } catch (error) { + console.log(error); + } + + await new Promise(resolve => setTimeout(resolve, 20000)); + } } }