Skip to content

Commit

Permalink
Merge pull request #142 from DIG-Network/release/v0.0.1-alpha.158
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.158
  • Loading branch information
MichaelTaylor3D authored Oct 7, 2024
2 parents 1735d9e + 05e917c commit 6c54d12
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

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.158](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.157...v0.0.1-alpha.158) (2024-10-07)


### Features

* additional caching measure to optimize requests ([1e501c9](https://github.com/DIG-Network/dig-chia-sdk/commit/1e501c939b60eea08d9b157c287e80b860c8a89d))

### [0.0.1-alpha.157](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.156...v0.0.1-alpha.157) (2024-10-07)


### Features

* additional caching measure to optimize requests ([e198a97](https://github.com/DIG-Network/dig-chia-sdk/commit/e198a9722def99788dfe02f13e199aae22835d74))

### [0.0.1-alpha.156](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.155...v0.0.1-alpha.156) (2024-10-07)


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

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

2 changes: 1 addition & 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.156",
"version": "0.0.1-alpha.158",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
18 changes: 16 additions & 2 deletions src/DigNetwork/PropagationServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import NodeCache from "node-cache";

// Initialize cache with a TTL of 1 week (604800 seconds)
const storeExistsCache = new NodeCache({ stdTTL: 86400 });
const pingUpdatecache = new NodeCache({ stdTTL: 86400 });

// Helper function to trim long filenames with ellipsis and ensure consistent padding
function formatFilename(filename: string | undefined, maxLength = 30): string {
Expand Down Expand Up @@ -119,9 +120,17 @@ export class PropagationServer {

/**
* Ping the current peer about an update to the store, passing rootHash.
* If the combination of ipAddress-storeId-rootHash is cached as "successfully synced", skip the request.
* @param rootHash - The root hash for the store update.
*/
async pingUpdate(rootHash: string): Promise<void> {
const cacheKey = `${this.ipAddress}-${this.storeId}-${rootHash}`;

// Check if response for this combination is already cached
if (pingUpdatecache.get(cacheKey) === "successfully synced") {
return;
}

try {
const httpsAgent = this.createHttpsAgent();
const url = `https://${formatHost(this.ipAddress)}:${
Expand All @@ -135,7 +144,6 @@ export class PropagationServer {
},
};

// Data to send in the request (storeId and rootHash)
const data = {
storeId: this.storeId,
rootHash: rootHash,
Expand All @@ -146,8 +154,14 @@ export class PropagationServer {
const response = await axios.post(url, data, config);
console.log(
green(`✔ Successfully pinged peer: ${this.ipAddress}`),
response
response.data
);

if (response.data.message === "Already Synced") {
// Cache the response as "successfully synced" to avoid redundant requests
pingUpdatecache.set(cacheKey, "successfully synced");
}

return response.data;
} catch (error: any) {
console.error(red(`✖ Failed to ping peer: ${this.ipAddress}`));
Expand Down
1 change: 1 addition & 0 deletions src/blockchain/DataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { StoreMonitorRegistry } from "./StoreMonitorRegistry";

// Initialize the cache with a TTL of 180 seconds (3 minutes)
const rootHistoryCache = new NodeCache({ stdTTL: 180 });
const allStoresCache = new NodeCache({ stdTTL: 15 });

const stat = promisify(fs.stat);
const readdir = promisify(fs.readdir);
Expand Down

0 comments on commit 6c54d12

Please sign in to comment.