Skip to content

Commit

Permalink
Merge pull request #9 from DIG-Network/release/v0.0.1-alpha.11
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.11
  • Loading branch information
MichaelTaylor3D authored Sep 18, 2024
2 parents c13a498 + 1926157 commit 41245d0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
24 changes: 22 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion 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.10",
"version": "0.0.1-alpha.11",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down Expand Up @@ -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": {
Expand Down
34 changes: 26 additions & 8 deletions src/controllers/merkleTreeController.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 (
Expand Down Expand Up @@ -245,16 +252,27 @@ export const putStore = async (req: Request, res: Response): Promise<void> => {

// 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<boolean>(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.");

Expand Down

0 comments on commit 41245d0

Please sign in to comment.