diff --git a/CHANGELOG.md b/CHANGELOG.md index fd54330..35971aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ 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.11](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.10...v0.0.1-alpha.11) (2024-09-18) + + +### Features + +* add cache to store ownership check ([b54bc13](https://github.com/DIG-Network/dig-propagation-server/commit/b54bc13a988997261f185c07b2dcc427e64cae95)) + ### [0.0.1-alpha.10](https://github.com/DIG-Network/dig-propagation-server/compare/v0.0.1-alpha.9...v0.0.1-alpha.10) (2024-09-17) diff --git a/package-lock.json b/package-lock.json index e3edcbe..6b604b1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,18 +1,19 @@ { "name": "dig-propagation-server", - "version": "0.0.1-alpha.10", + "version": "0.0.1-alpha.11", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dig-propagation-server", - "version": "0.0.1-alpha.10", + "version": "0.0.1-alpha.11", "license": "ISC", "dependencies": { "@dignetwork/dig-sdk": "^0.0.1-alpha.18", "async-mutex": "^0.5.0", "chia-server-coin": "^0.0.5", "express": "^4.19.2", + "node-cache": "^5.1.2", "toad-scheduler": "^3.0.1" }, "bin": { @@ -1314,6 +1315,14 @@ "url": "https://github.com/chalk/wrap-ansi?sponsor=1" } }, + "node_modules/clone": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz", + "integrity": "sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==", + "engines": { + "node": ">=0.8" + } + }, "node_modules/color-convert": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", @@ -3653,6 +3662,17 @@ "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==", "dev": true }, + "node_modules/node-cache": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/node-cache/-/node-cache-5.1.2.tgz", + "integrity": "sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==", + "dependencies": { + "clone": "2.x" + }, + "engines": { + "node": ">= 8.0.0" + } + }, "node_modules/noms": { "version": "0.0.0", "resolved": "https://registry.npmjs.org/noms/-/noms-0.0.0.tgz", diff --git a/package.json b/package.json index e229275..2fc8aa0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dig-propagation-server", - "version": "0.0.1-alpha.10", + "version": "0.0.1-alpha.11", "description": "", "type": "commonjs", "main": "./dist/index.js", @@ -28,6 +28,7 @@ "async-mutex": "^0.5.0", "chia-server-coin": "^0.0.5", "express": "^4.19.2", + "node-cache": "^5.1.2", "toad-scheduler": "^3.0.1" }, "devDependencies": { diff --git a/src/controllers/merkleTreeController.ts b/src/controllers/merkleTreeController.ts index a19c1e8..4521091 100644 --- a/src/controllers/merkleTreeController.ts +++ b/src/controllers/merkleTreeController.ts @@ -1,6 +1,7 @@ import fs from "fs"; import path from "path"; import { Request, Response } from "express"; +import NodeCache from "node-cache"; import { getCredentials } from "../utils/authUtils"; import { HttpError } from "../utils/HttpError"; import { generateNonce } from "../utils/nonce"; @@ -13,6 +14,12 @@ import { getStorageLocation } from "../utils/storage"; const streamPipeline = promisify(pipeline); +// Create a cache instance with 1 minute TTL (time-to-live) +const storeOwnerCache = new NodeCache({ stdTTL: 60 }); +const generateCacheKey = (publicKey: string, storeId: string): string => { + return `${publicKey}_${storeId}`; +}; + const digFolderPath = getStorageLocation(); export const storeStatus = async ( @@ -245,16 +252,27 @@ export const putStore = async (req: Request, res: Response): Promise => { // Check store ownership console.log("Checking store ownership..."); - const dataStore = DataStore.from(storeId); - //const isOwner = await dataStore.hasMetaWritePermissions( - // Buffer.from(publicKey, "hex") - //); + const cacheKey = generateCacheKey(publicKey, storeId); + let isOwner = storeOwnerCache.get(cacheKey); + + if (isOwner === undefined) { + // If not in cache, check ownership and cache the result + const dataStore = DataStore.from(storeId); + isOwner = await dataStore.hasMetaWritePermissions( + Buffer.from(publicKey, "hex") + ); - // if (!isOwner) { - // console.log("User does not have write access to this store."); - // throw new HttpError(403, "You do not have write access to this store."); - //} + // Cache the result for 1 minute (60 seconds) + storeOwnerCache.set(cacheKey, isOwner); + } else { + console.log("Using cached isOwner value for publicKey and storeId:", cacheKey); + } + + if (!isOwner) { + console.log("User does not have write access to this store."); + throw new HttpError(403, "You do not have write access to this store."); + } console.log("User has write access to the store.");