Skip to content

Commit

Permalink
Merge pull request #89 from DIG-Network/release/v0.0.1-alpha.95
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.95
  • Loading branch information
MichaelTaylor3D authored Oct 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 14277ee + 8763784 commit f9a2f76
Showing 5 changed files with 58 additions and 52 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,20 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.0.1-alpha.95](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.94...v0.0.1-alpha.95) (2024-10-04)


### Features

* sync store optimizations ([2e9d32e](https://github.com/DIG-Network/dig-propagation-server/commit/2e9d32e9d3a4289d24e5f39acbee1af86defaa3c))

### [0.0.1-alpha.94](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.93...v0.0.1-alpha.94) (2024-10-04)


### Features

* sync store optimizations ([4dea8bc](https://github.com/DIG-Network/dig-propagation-server/commit/4dea8bc647ad02d856f4a4bb3f2fe9884dfb88a7))

### [0.0.1-alpha.93](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.92...v0.0.1-alpha.93) (2024-10-04)


12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dig-propagation-server",
"version": "0.0.1-alpha.93",
"version": "0.0.1-alpha.95",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
@@ -26,7 +26,7 @@
],
"dependencies": {
"@dignetwork/datalayer-driver": "^0.1.28",
"@dignetwork/dig-sdk": "^0.0.1-alpha.129",
"@dignetwork/dig-sdk": "^0.0.1-alpha.130",
"async-mutex": "^0.5.0",
"busboy": "^1.6.0",
"express": "^4.19.2",
17 changes: 16 additions & 1 deletion src/controllers/storeController.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,13 @@ import path from "path";
import fs from "fs";

// @ts-ignore
import { DigNetwork, DataStore, DigPeer } from "@dignetwork/dig-sdk";
import {
DigNetwork,
DataStore,
DigPeer,
ServerCoin,
NconfManager,
} from "@dignetwork/dig-sdk";
import { getStorageLocation } from "../utils/storage";

export const subscribeToStore = async (
@@ -31,6 +37,15 @@ export const subscribeToStore = async (
const digNetwork = new DigNetwork(storeId);
await digNetwork.syncStoreFromPeers();

const nconfManager = new NconfManager("config.json");
const publicIp: string | null | undefined =
await nconfManager.getConfigValue("publicIp");

if (publicIp) {
const serverCoin = new ServerCoin(storeId);
await serverCoin.ensureServerCoinExists(publicIp);
}

res.status(200).json({ message: `Subscribing to store ${storeId}` });
} catch (error) {
console.error(
63 changes: 20 additions & 43 deletions src/tasks/sync_stores.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import fs from "fs";
import path from "path";
import { SimpleIntervalJob, Task } from "toad-scheduler";
import {
getStoresList,
Wallet,
DataStore,
DigNetwork,
NconfManager,
ServerCoin,
StoreMonitorRegistry,
} from "@dignetwork/dig-sdk";
import { Mutex } from "async-mutex";
import { getStorageLocation } from "../utils/storage";

const STORE_PATH = path.join(getStorageLocation(), "stores");

const mutex = new Mutex();

const PUBLIC_IP_KEY = "publicIp";
@@ -24,32 +17,20 @@ const nconfManager = new NconfManager("config.json");
// Helper Functions
// -------------------------

/**
* Synchronizes a specific store.
* @param storeId - The ID of the store to synchronize.
*/
const syncStore = async (storeId: string): Promise<void> => {
console.log(`Starting sync process for store ${storeId}...`);

try {
console.log(`Store ${storeId} is out of date. Syncing...`);
await syncStoreFromNetwork(storeId);
} catch (error: any) {
console.trace(`Error processing store ${storeId}: ${error.message}`);
} finally {
await finalizeStoreSync(storeId);
}
};

/**
* Attempts to synchronize a store from the network.
* @param storeId - The ID of the store to synchronize.
*/
const syncStoreFromNetwork = async (storeId: string): Promise<void> => {
try {
console.log(`Attempting to sync store ${storeId} from the network...`);

const digNetwork = new DigNetwork(storeId);
await digNetwork.syncStoreFromPeers();

const dataStore = await DataStore.from(storeId);
await dataStore.fetchCoinInfo();

console.log(`Store ${storeId} synchronized successfully.`);
} catch (error: any) {
console.warn(
@@ -62,20 +43,6 @@ const syncStoreFromNetwork = async (storeId: string): Promise<void> => {
}
};

/**
* Finalizes the synchronization process for a store.
* @param storeId - The ID of the store to finalize.
*/
const finalizeStoreSync = async (storeId: string): Promise<void> => {
try {
console.log(`Finalizing sync for store ${storeId}...`);
const dataStore = await DataStore.from(storeId);
await dataStore.fetchCoinInfo();
console.log(`Finalization complete for store ${storeId}.`);
} catch (error: any) {
console.error(`Error in finalizing store ${storeId}: ${error.message}`);
}
};

/**
* Ensures that the server coin exists and is valid for a specific store.
@@ -90,7 +57,6 @@ const ensureServerCoin = async (
const serverCoin = new ServerCoin(storeId);
await serverCoin.ensureServerCoinExists(publicIp);
await serverCoin.meltOutdatedEpochs(publicIp);
console.log(`Server coin ensured for store ${storeId}.`);
} catch (error: any) {
console.error(
`Failed to ensure server coin for store ${storeId}: ${error.message}`
@@ -112,17 +78,29 @@ const initializeStoreMonitor = async (): Promise<void> => {

const storeList = getStoresList();

const publicIp: string | null | undefined =
await nconfManager.getConfigValue(PUBLIC_IP_KEY);

// Register each store with the store monitor
storeList.forEach((storeId) => {
const serverCoin = new ServerCoin(storeId);
storeMonitor.registerStore(storeId, async () => {
if (publicIp) {
await serverCoin.ensureServerCoinExists(publicIp);
}

console.log(`Store update detected for ${storeId}. Syncing...`);
await syncStore(storeId);
await syncStoreFromNetwork(storeId);

if (publicIp) {
await serverCoin.ensureServerCoinExists(publicIp);
}
});
});

// Attempt to sync each store initially
for (const storeId of storeList) {
await syncStore(storeId);
await syncStoreFromNetwork(storeId);
}

console.log("All stores have been initialized and synchronized.");
@@ -141,7 +119,6 @@ const initializeStoreMonitor = async (): Promise<void> => {
const syncStoresTask = new Task("sync-stores", async () => {
if (!mutex.isLocked()) {
const releaseMutex = await mutex.acquire();
let mnemonic: string | undefined;

try {
console.log("Starting sync-stores task...");
@@ -197,7 +174,7 @@ const syncStoresTask = new Task("sync-stores", async () => {

const job = new SimpleIntervalJob(
{
seconds: 60,
minutes: 5,
runImmediately: true,
},
syncStoresTask,

0 comments on commit f9a2f76

Please sign in to comment.