From 0eba8123d51c0f992adefadf856796947f867f40 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Wed, 18 Sep 2024 18:25:56 -0400 Subject: [PATCH 1/2] feat: calculate epoch and round --- src/blockchain/ServerCoin.ts | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/blockchain/ServerCoin.ts b/src/blockchain/ServerCoin.ts index 80523a1..986b404 100644 --- a/src/blockchain/ServerCoin.ts +++ b/src/blockchain/ServerCoin.ts @@ -39,7 +39,7 @@ export class ServerCoin { BigInt(1000000) ); - const currentEpoch = ServerCoin.getCurrentEpoch(); + const { epoch: currentEpoch} = ServerCoin.getCurrentEpoch(); const epochBasedHint = morphLauncherId( Buffer.from(this.storeId, "hex"), BigInt(currentEpoch) @@ -221,7 +221,7 @@ export class ServerCoin { public async getActiveEpochPeers( blacklist: string[] = [] ): Promise { - const epoch = ServerCoin.getCurrentEpoch(); + const { epoch } = ServerCoin.getCurrentEpoch(); return this.getAllEpochPeers(epoch, blacklist); } @@ -230,7 +230,7 @@ export class ServerCoin { sampleSize: number = 5, blacklist: string[] = [] ): Promise { - const epoch = ServerCoin.getCurrentEpoch(); + const { epoch } = ServerCoin.getCurrentEpoch(); return this.sampleServerCoinsByEpoch(epoch, sampleSize, blacklist); } @@ -245,15 +245,15 @@ export class ServerCoin { } // Get the current epoch based on the current timestamp - public static getCurrentEpoch(): number { - return ServerCoin.calculateEpoch(new Date()); + public static getCurrentEpoch(): { epoch: number; round: number } { + return ServerCoin.calculateEpochAndRound(new Date()); } // Ensure server coin exists for the current epoch public async ensureServerCoinExists(peerIp: string): Promise { try { console.log(`Ensuring server coin exists for store ${this.storeId}...`); - const currentEpoch = ServerCoin.getCurrentEpoch(); + const { epoch: currentEpoch } = ServerCoin.getCurrentEpoch(); const serverCoins = await this.getServerCoinsForStore(peerIp); // Check if a server coin already exists for the current epoch @@ -301,7 +301,7 @@ export class ServerCoin { // Melt outdated server coins public async meltOutdatedEpochs(peerIp: string): Promise { try { - const currentEpoch = ServerCoin.getCurrentEpoch(); + const { epoch: currentEpoch } = ServerCoin.getCurrentEpoch(); let serverCoins = await this.getServerCoinsForStore(peerIp); // Filter out coins that are not in the current epoch @@ -433,14 +433,17 @@ export class ServerCoin { } // Static method to calculate the current epoch - public static calculateEpoch(currentTimestampUTC: Date): number { + public static calculateEpochAndRound(currentTimestampUTC: Date): { + epoch: number; + round: number; + } { const firstEpochStart = new Date(Date.UTC(2024, 8, 3, 0, 0)); // Sept 3, 2024, 00:00 UTC // Convert the current timestamp to milliseconds const currentTimestampMillis = currentTimestampUTC.getTime(); // Calculate the number of milliseconds in one epoch (7 days) - const millisecondsInEpoch = 7 * 24 * 60 * 60 * 1000; + const millisecondsInEpoch = 7 * 24 * 60 * 60 * 1000; // 7 days in milliseconds // Calculate the difference in milliseconds between the current timestamp and the first epoch start const differenceMillis = currentTimestampMillis - firstEpochStart.getTime(); @@ -448,6 +451,16 @@ export class ServerCoin { // Calculate the current epoch number const epochNumber = Math.floor(differenceMillis / millisecondsInEpoch) + 1; - return epochNumber; + // Calculate the milliseconds elapsed since the start of the current epoch + const elapsedMillisInCurrentEpoch = differenceMillis % millisecondsInEpoch; + + // Calculate the number of milliseconds in a round (10 minutes) + const millisecondsInRound = 10 * 60 * 1000; // 10 minutes in milliseconds + + // Calculate the current round number + const roundNumber = + Math.floor(elapsedMillisInCurrentEpoch / millisecondsInRound) + 1; + + return { epoch: epochNumber, round: roundNumber }; } } From ea17310d280265a394e23a9aca44e7936ae2b44f Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Wed, 18 Sep 2024 18:27:40 -0400 Subject: [PATCH 2/2] chore(release): 0.0.1-alpha.25 --- CHANGELOG.md | 7 +++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94f8430..74b923b 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.25](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.24...v0.0.1-alpha.25) (2024-09-18) + + +### Features + +* calculate epoch and round ([0eba812](https://github.com/DIG-Network/dig-chia-sdk/commit/0eba8123d51c0f992adefadf856796947f867f40)) + ### [0.0.1-alpha.24](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.23...v0.0.1-alpha.24) (2024-09-17) diff --git a/package-lock.json b/package-lock.json index 3ad89d7..7d80e13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.24", + "version": "0.0.1-alpha.25", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.24", + "version": "0.0.1-alpha.25", "license": "ISC", "dependencies": { "bip39": "^3.1.0", diff --git a/package.json b/package.json index f1d7145..14a0ee9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.24", + "version": "0.0.1-alpha.25", "description": "", "type": "commonjs", "main": "./dist/index.js",