Skip to content

Commit

Permalink
Merge pull request #45 from DIG-Network/release/v0.0.1-alpha.49
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.49
  • Loading branch information
MichaelTaylor3D authored Sep 20, 2024
2 parents c9c6764 + 9d446fd commit 9fa4657
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 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.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)


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": "@dignetwork/dig-sdk",
"version": "0.0.1-alpha.48",
"version": "0.0.1-alpha.49",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down Expand Up @@ -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"
},
Expand Down
34 changes: 21 additions & 13 deletions src/blockchain/ServerCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -179,22 +183,21 @@ export class ServerCoin {
);
}

public async getAllEpochPeers(
epoch: number,
blacklist: string[] = []
): Promise<string[]> {
const epochBasedHint = morphLauncherId(
Buffer.from(this.storeId, "hex"),
BigInt(epoch)
);
public async getAllEpochPeers(epoch: number, blacklist: string[] = []): Promise<string[]> {
const cacheKey = `serverCoinPeers-${this.storeId}-${epoch}`;

// Check if the result is already cached
const cachedPeers = serverCoinPeersCache.get<string[]>(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
Expand All @@ -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(
Expand Down

0 comments on commit 9fa4657

Please sign in to comment.