From 2ef37087245ac5480bfce0b4f814a79190dc123d Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 3 Oct 2024 17:05:46 -0400 Subject: [PATCH 1/4] chore: update sdk --- src/blockchain/StoreInfoCacheUpdater.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/blockchain/StoreInfoCacheUpdater.ts b/src/blockchain/StoreInfoCacheUpdater.ts index f0b8321..ddd54a1 100644 --- a/src/blockchain/StoreInfoCacheUpdater.ts +++ b/src/blockchain/StoreInfoCacheUpdater.ts @@ -24,13 +24,16 @@ export class StoreInfoCacheUpdater { private isMonitoring: boolean = true; private constructor() { + console.log("Constructor: Initializing StoreInfoCacheUpdater"); this.storeCoinCache = new FileCache(`stores`, USER_DIR_PATH); // Construct lock file path using the path module this.lockFilePath = path.join(USER_DIR_PATH, "store-info-cache.lock"); + console.log("Lock file path:", this.lockFilePath); const lockDir = path.dirname(this.lockFilePath); if (!fs.existsSync(lockDir)) { + console.log(`Creating lock directory: ${lockDir}`); fs.mkdirSync(lockDir, { recursive: true }); } @@ -48,6 +51,7 @@ export class StoreInfoCacheUpdater { private async startMonitors() { try { + console.log("Attempting to check if lockfile is held..."); // Check if the lockfile is already held const isLocked = await lockfile.check(this.lockFilePath, { realpath: false, @@ -61,6 +65,7 @@ export class StoreInfoCacheUpdater { } // Attempt to acquire the lock + console.log("Attempting to acquire lock..."); this.releaseLock = await lockfile.lock(this.lockFilePath, { retries: { retries: 0, // No retries since we only need one lock @@ -69,14 +74,20 @@ export class StoreInfoCacheUpdater { realpath: false, // Ensure lockfile uses the exact path }); + console.log("Lock acquired, starting monitors..."); + const storeIds = this.storeCoinCache.getCachedKeys(); + console.log(`Found ${storeIds.length} store IDs in cache:`, storeIds); for (const storeId of storeIds) { // Check if a monitor is already running for this storeId if (!this.monitors.has(storeId)) { + console.log(`Starting monitor for storeId: ${storeId}`); // Start monitoring in the background const monitorPromise = this.monitorStore(storeId); this.monitors.set(storeId, monitorPromise); + } else { + console.log(`Monitor already exists for storeId: ${storeId}`); } } @@ -84,7 +95,7 @@ export class StoreInfoCacheUpdater { // Wait for all monitors to settle const monitorPromises = Array.from(this.monitors.values()); - + console.log("Waiting for all monitor promises to settle..."); await Promise.all(monitorPromises); } catch (error: any) { console.error("Monitor system encountered an error:", error); @@ -92,6 +103,7 @@ export class StoreInfoCacheUpdater { // Release the lock if (this.releaseLock) { try { + console.log("Releasing lock..."); await this.releaseLock(); console.log("Lock released successfully."); } catch (releaseError) { @@ -138,7 +150,7 @@ export class StoreInfoCacheUpdater { // Get the coinId associated with the store const coinId = getCoinId(latestStore.coin); - console.log(`!!! Waiting for coin to be spent: ${coinId.toString("hex")}`); + console.log(`Waiting for coin to be spent: ${coinId.toString("hex")}`); // Wait for the coin to be spent await peer.waitForCoinToBeSpent( @@ -153,7 +165,6 @@ export class StoreInfoCacheUpdater { try { // When resolved, sync the store - //const { latestStore: updatedStore, latestHeight: newHeight } = await withTimeout( const storeInfo = await withTimeout( peer.syncStore( latestStore, @@ -225,8 +236,6 @@ export class StoreInfoCacheUpdater { private isUnrecoverableError(error: any): boolean { // Determine whether the error is unrecoverable - // For this example, we'll treat any unexpected error as unrecoverable - // You can customize this logic based on your application's needs return true; } } From e1f456ad5edfaf8331d6a613e6862adfef0d1894 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 3 Oct 2024 17:06:44 -0400 Subject: [PATCH 2/4] chore(release): 0.0.1-alpha.117 --- CHANGELOG.md | 2 ++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac46ad6..7897e84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ 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.117](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.116...v0.0.1-alpha.117) (2024-10-03) + ### [0.0.1-alpha.116](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.115...v0.0.1-alpha.116) (2024-10-03) ### [0.0.1-alpha.115](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.114...v0.0.1-alpha.115) (2024-10-03) diff --git a/package-lock.json b/package-lock.json index 49a88dd..c565168 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.116", + "version": "0.0.1-alpha.117", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.116", + "version": "0.0.1-alpha.117", "license": "ISC", "dependencies": { "@dignetwork/datalayer-driver": "^0.1.29", diff --git a/package.json b/package.json index 8a15615..4e53d6a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.116", + "version": "0.0.1-alpha.117", "description": "", "type": "commonjs", "main": "./dist/index.js", From 7b198d5ac46b28c9f0aa4f20ebbec7ffb6a14d5b Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 3 Oct 2024 17:15:37 -0400 Subject: [PATCH 3/4] chore: update sdk --- src/blockchain/StoreInfoCacheUpdater.ts | 67 +++++++++++++++++++------ 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/src/blockchain/StoreInfoCacheUpdater.ts b/src/blockchain/StoreInfoCacheUpdater.ts index ddd54a1..1c2c62e 100644 --- a/src/blockchain/StoreInfoCacheUpdater.ts +++ b/src/blockchain/StoreInfoCacheUpdater.ts @@ -17,15 +17,15 @@ export class StoreInfoCacheUpdater { latestStore: ReturnType; latestHeight: number; latestHash: string; - }>; + }> = new FileCache(`stores`, USER_DIR_PATH); private monitors: Map> = new Map(); private lockFilePath: string; private releaseLock: (() => Promise) | null = null; private isMonitoring: boolean = true; + private lockRenewalInterval: NodeJS.Timeout | null = null; private constructor() { console.log("Constructor: Initializing StoreInfoCacheUpdater"); - this.storeCoinCache = new FileCache(`stores`, USER_DIR_PATH); // Construct lock file path using the path module this.lockFilePath = path.join(USER_DIR_PATH, "store-info-cache.lock"); @@ -51,17 +51,22 @@ export class StoreInfoCacheUpdater { private async startMonitors() { try { - console.log("Attempting to check if lockfile is held..."); - // Check if the lockfile is already held - const isLocked = await lockfile.check(this.lockFilePath, { - realpath: false, - }); - if (isLocked) { - // Another process is already running the monitors; skip starting monitors - console.log( - "Another process is already running the StoreInfoCacheUpdater." - ); - return; + console.log("Checking if lockfile exists..."); + + // Check if the lock file exists + if (!fs.existsSync(this.lockFilePath)) { + console.log("Lockfile does not exist. Proceeding without lock."); + } else { + // Check if the lockfile is already held + const isLocked = await lockfile.check(this.lockFilePath, { + realpath: false, + }); + if (isLocked) { + console.log( + "Another process is already running the StoreInfoCacheUpdater." + ); + return; + } } // Attempt to acquire the lock @@ -70,11 +75,14 @@ export class StoreInfoCacheUpdater { retries: { retries: 0, // No retries since we only need one lock }, - stale: 60000, // Lock expires after 1 minute (adjust as needed) + stale: 60000, // Lock expires after 1 minute realpath: false, // Ensure lockfile uses the exact path }); console.log("Lock acquired, starting monitors..."); + + // Renew the lock every minute by reacquiring it + this.renewLock(); const storeIds = this.storeCoinCache.getCachedKeys(); console.log(`Found ${storeIds.length} store IDs in cache:`, storeIds); @@ -110,9 +118,39 @@ export class StoreInfoCacheUpdater { console.error("Error releasing the lock:", releaseError); } } + // Clear the lock renewal interval + if (this.lockRenewalInterval) { + clearInterval(this.lockRenewalInterval); + this.lockRenewalInterval = null; + } } } + private renewLock() { + // Set up a renewal process that releases and reacquires the lock every minute + this.lockRenewalInterval = setInterval(async () => { + try { + if (this.releaseLock) { + console.log("Releasing the lock for renewal..."); + await this.releaseLock(); + console.log("Lock released for renewal."); + } + + // Reacquire the lock + this.releaseLock = await lockfile.lock(this.lockFilePath, { + retries: { + retries: 0, // No retries since we only need one lock + }, + stale: 60000, // Lock expires after 1 minute + realpath: false, // Ensure lockfile uses the exact path + }); + console.log("Lock reacquired for renewal."); + } catch (error) { + console.error("Error renewing the lock:", error); + } + }, 60000); // Renew the lock every 60 seconds + } + // Monitor a single store's coin private async monitorStore(storeId: string): Promise { while (this.isMonitoring) { @@ -235,7 +273,6 @@ export class StoreInfoCacheUpdater { } private isUnrecoverableError(error: any): boolean { - // Determine whether the error is unrecoverable return true; } } From 09c17a7fabaeb2fb4d3dfa337c47b7aac9b6948c Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 3 Oct 2024 17:16:12 -0400 Subject: [PATCH 4/4] chore(release): 0.0.1-alpha.118 --- CHANGELOG.md | 2 ++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7897e84..8242630 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ 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.118](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.117...v0.0.1-alpha.118) (2024-10-03) + ### [0.0.1-alpha.117](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.116...v0.0.1-alpha.117) (2024-10-03) ### [0.0.1-alpha.116](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.115...v0.0.1-alpha.116) (2024-10-03) diff --git a/package-lock.json b/package-lock.json index c565168..4737dc1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.117", + "version": "0.0.1-alpha.118", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.117", + "version": "0.0.1-alpha.118", "license": "ISC", "dependencies": { "@dignetwork/datalayer-driver": "^0.1.29", diff --git a/package.json b/package.json index 4e53d6a..33ee872 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.117", + "version": "0.0.1-alpha.118", "description": "", "type": "commonjs", "main": "./dist/index.js",