-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(epochs): move epoch to class that fetches epoch information from …
…contract Since we are not computing epochs ourselves, we need to fetch the epoch information from the contract when we run a new report. This moves that logic to a class that fetches epoch information on every execution of running a report. It will cache the result until the epoch is over, based on network time rather than local system time.
- Loading branch information
dtfiedler
committed
Jun 11, 2024
1 parent
2ffb3e2
commit 4a51a7a
Showing
6 changed files
with
104 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* AR.IO Observer | ||
* Copyright (C) 2023 Permanent Data Solutions, Inc. All Rights Reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { AoIORead, IO } from '@ar.io/sdk'; | ||
|
||
import * as config from '../config.js'; | ||
import { BlockSource, EpochTimestampParams, HeightSource } from '../types.js'; | ||
import { EpochTimestampSource as IEpochTimestampSource } from '../types.js'; | ||
|
||
export class ContractEpochSource implements IEpochTimestampSource { | ||
private contract: AoIORead; | ||
private blockSource: BlockSource; | ||
private heightSource: HeightSource; | ||
private epochParams: EpochTimestampParams | undefined; | ||
|
||
constructor({ | ||
contract = IO.init({ processId: config.IO_PROCESS_ID }), | ||
blockSource, | ||
heightSource, | ||
}: { | ||
contract?: AoIORead; | ||
blockSource: BlockSource; | ||
heightSource: HeightSource; | ||
}) { | ||
this.contract = contract; | ||
this.blockSource = blockSource; | ||
this.heightSource = heightSource; | ||
} | ||
|
||
async getEpochParams(): Promise<EpochTimestampParams> { | ||
// cache the epoch params for the duration of the epoch to avoid unnecessary contract calls | ||
const height = await this.heightSource.getHeight(); | ||
const block = await this.blockSource.getBlockByHeight(height); | ||
const networkTimestamp = block.timestamp * 1000; | ||
if ( | ||
this.epochParams !== undefined && | ||
this.epochParams.epochEndTimestamp > networkTimestamp | ||
) { | ||
return this.epochParams; | ||
} | ||
const { startTimestamp, startHeight, endTimestamp, epochIndex } = | ||
await this.contract.getCurrentEpoch(); | ||
return { | ||
epochStartTimestamp: startTimestamp, | ||
epochStartHeight: startHeight, | ||
epochEndTimestamp: endTimestamp, | ||
epochIndex, | ||
}; | ||
} | ||
|
||
async getEpochStartTimestamp(): Promise<number> { | ||
return this.getEpochParams().then((params) => params.epochStartTimestamp); | ||
} | ||
|
||
async getEpochEndTimestamp(): Promise<number> { | ||
return this.getEpochParams().then((params) => params.epochEndTimestamp); | ||
} | ||
|
||
async getEpochStartHeight(): Promise<number> { | ||
return this.getEpochParams().then((params) => params.epochStartHeight); | ||
} | ||
|
||
async getEpochIndex(): Promise<number> { | ||
return this.getEpochParams().then((params) => params.epochIndex); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters