Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/v0.0.1 alpha.11 #9

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading