Skip to content

Commit

Permalink
Merge pull request #25 from DIG-Network/release/v0.0.1-alpha.25
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.25
  • Loading branch information
MichaelTaylor3D authored Sep 18, 2024
2 parents 6cad4c5 + ea17310 commit 82d2eb4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 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.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)


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.24",
"version": "0.0.1-alpha.25",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
33 changes: 23 additions & 10 deletions src/blockchain/ServerCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -221,7 +221,7 @@ export class ServerCoin {
public async getActiveEpochPeers(
blacklist: string[] = []
): Promise<string[]> {
const epoch = ServerCoin.getCurrentEpoch();
const { epoch } = ServerCoin.getCurrentEpoch();
return this.getAllEpochPeers(epoch, blacklist);
}

Expand All @@ -230,7 +230,7 @@ export class ServerCoin {
sampleSize: number = 5,
blacklist: string[] = []
): Promise<string[]> {
const epoch = ServerCoin.getCurrentEpoch();
const { epoch } = ServerCoin.getCurrentEpoch();
return this.sampleServerCoinsByEpoch(epoch, sampleSize, blacklist);
}

Expand All @@ -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<void> {
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
Expand Down Expand Up @@ -301,7 +301,7 @@ export class ServerCoin {
// Melt outdated server coins
public async meltOutdatedEpochs(peerIp: string): Promise<void> {
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
Expand Down Expand Up @@ -433,21 +433,34 @@ 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();

// 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 };
}
}

0 comments on commit 82d2eb4

Please sign in to comment.