diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b660b7..45ea369 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.49](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.48...v0.0.1-alpha.49) (2024-09-20) + + +### Features + +* add cache to getAllEpochPeers ([2ba45ef](https://github.com/DIG-Network/dig-chia-sdk/commit/2ba45effadb6efc6babdef37e82edc9fa2f0bfec)) + ### [0.0.1-alpha.48](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.47...v0.0.1-alpha.48) (2024-09-20) diff --git a/package-lock.json b/package-lock.json index 9ffd264..acbce3d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.48", + "version": "0.0.1-alpha.49", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.48", + "version": "0.0.1-alpha.49", "license": "ISC", "dependencies": { "@dignetwork/datalayer-driver": "^0.1.24", @@ -25,6 +25,7 @@ "merkletreejs": "^0.4.0", "nanospinner": "^1.1.0", "nconf": "^0.12.1", + "node-cache": "^5.1.2", "p-limit": "^6.1.0", "superagent": "^10.0.0" }, @@ -1411,6 +1412,14 @@ "node": ">= 12" } }, + "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", @@ -3665,6 +3674,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 21e1da7..a7587d5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.48", + "version": "0.0.1-alpha.49", "description": "", "type": "commonjs", "main": "./dist/index.js", @@ -41,6 +41,7 @@ "merkletreejs": "^0.4.0", "nanospinner": "^1.1.0", "nconf": "^0.12.1", + "node-cache": "^5.1.2", "p-limit": "^6.1.0", "superagent": "^10.0.0" }, diff --git a/src/blockchain/ServerCoin.ts b/src/blockchain/ServerCoin.ts index 0fd05e2..22fa845 100644 --- a/src/blockchain/ServerCoin.ts +++ b/src/blockchain/ServerCoin.ts @@ -14,9 +14,13 @@ import { Wallet } from "./Wallet"; import { NconfManager } from "../utils/NconfManager"; import { CoinData, ServerCoinData } from "../types"; import { DataStore } from "./DataStore"; +import NodeCache from "node-cache"; const serverCoinCollateral = 300_000_000; +// Initialize the cache with a TTL of 300 seconds (5 minutes) +const serverCoinPeersCache = new NodeCache({ stdTTL: 300 }); + export class ServerCoin { private storeId: string; public static readonly serverCoinManager = new NconfManager( @@ -179,22 +183,21 @@ export class ServerCoin { ); } - public async getAllEpochPeers( - epoch: number, - blacklist: string[] = [] - ): Promise { - const epochBasedHint = morphLauncherId( - Buffer.from(this.storeId, "hex"), - BigInt(epoch) - ); + public async getAllEpochPeers(epoch: number, blacklist: string[] = []): Promise { + const cacheKey = `serverCoinPeers-${this.storeId}-${epoch}`; + + // Check if the result is already cached + const cachedPeers = serverCoinPeersCache.get(cacheKey); + if (cachedPeers) { + return cachedPeers; + } + + const epochBasedHint = morphLauncherId(Buffer.from(this.storeId, "hex"), BigInt(epoch)); const peer = await FullNodePeer.connect(); const maxClvmCost = BigInt(11_000_000_000); - const hintedCoinStates = await peer.getHintedCoinStates( - epochBasedHint, - false - ); + const hintedCoinStates = await peer.getHintedCoinStates(epochBasedHint, false); const filteredCoinStates = hintedCoinStates.filter( (coinState) => coinState.coin.amount >= serverCoinCollateral @@ -215,7 +218,12 @@ export class ServerCoin { console.log("Server Coin Peers: ", serverCoinPeers); } - return Array.from(serverCoinPeers); + const peerList = Array.from(serverCoinPeers); + + // Cache the result + serverCoinPeersCache.set(cacheKey, peerList); + + return peerList; } public async getActiveEpochPeers(