Skip to content

Commit

Permalink
fix: remove Timestamp from epoch inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Dec 18, 2024
1 parent 3449e32 commit 01d9e47
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 112 deletions.
78 changes: 2 additions & 76 deletions src/common/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ import {
} from '../types/io.js';
import { AoSigner, mARIOToken } from '../types/token.js';
import { createAoSigner } from '../utils/ao.js';
import {
getCurrentBlockUnixTimestampMs,
paginationParamsToTags,
pruneTags,
} from '../utils/arweave.js';
import { paginationParamsToTags, pruneTags } from '../utils/arweave.js';
import { defaultArweave } from './arweave.js';
import { AOProcess } from './contracts/ao-process.js';
import { InvalidContractConfigurationError } from './error.js';
Expand Down Expand Up @@ -166,12 +162,6 @@ export class ARIOReadable implements AoARIORead {
async getEpochSettings(params?: EpochInput): Promise<AoEpochSettings> {
const allTags = [
{ name: 'Action', value: 'Epoch-Settings' },
{
name: 'Timestamp',
value:
(params as { timestamp?: number })?.timestamp?.toString() ??
(await getCurrentBlockUnixTimestampMs(this.arweave)).toString(),
},
{
name: 'Epoch-Index',
value: (params as { epochIndex?: number })?.epochIndex?.toString(),
Expand All @@ -185,12 +175,6 @@ export class ARIOReadable implements AoARIORead {
async getEpoch(epoch?: EpochInput): Promise<AoEpochData> {
const allTags = [
{ name: 'Action', value: 'Epoch' },
{
name: 'Timestamp',
value:
(epoch as { timestamp?: number })?.timestamp?.toString() ??
(await getCurrentBlockUnixTimestampMs(this.arweave)).toString(),
},
{
name: 'Epoch-Index',
value: (epoch as { epochIndex?: number })?.epochIndex?.toString(),
Expand Down Expand Up @@ -349,15 +333,7 @@ export class ARIOReadable implements AoARIORead {

async getCurrentEpoch(): Promise<AoEpochData> {
return this.process.read<AoEpochData>({
tags: [
{ name: 'Action', value: 'Epoch' },
{
name: 'Timestamp',
value: (
await getCurrentBlockUnixTimestampMs(this.arweave)
).toString(),
},
],
tags: [{ name: 'Action', value: 'Epoch' }],
});
}

Expand All @@ -366,12 +342,6 @@ export class ARIOReadable implements AoARIORead {
): Promise<AoWeightedObserver[]> {
const allTags = [
{ name: 'Action', value: 'Epoch-Prescribed-Observers' },
{
name: 'Timestamp',
value:
(epoch as { timestamp?: number })?.timestamp?.toString() ??
(await getCurrentBlockUnixTimestampMs(this.arweave)).toString(),
},
{
name: 'Epoch-Index',
value: (epoch as { epochIndex?: number })?.epochIndex?.toString(),
Expand All @@ -386,12 +356,6 @@ export class ARIOReadable implements AoARIORead {
async getPrescribedNames(epoch?: EpochInput): Promise<string[]> {
const allTags = [
{ name: 'Action', value: 'Epoch-Prescribed-Names' },
{
name: 'Timestamp',
value:
(epoch as { timestamp?: number })?.timestamp?.toString() ??
(await getCurrentBlockUnixTimestampMs(this.arweave)).toString(),
},
{
name: 'Epoch-Index',
value: (epoch as { epochIndex?: number })?.epochIndex?.toString(),
Expand All @@ -406,12 +370,6 @@ export class ARIOReadable implements AoARIORead {
async getObservations(epoch?: EpochInput): Promise<AoEpochObservationData> {
const allTags = [
{ name: 'Action', value: 'Epoch-Observations' },
{
name: 'Timestamp',
value:
(epoch as { timestamp?: number })?.timestamp?.toString() ??
(await getCurrentBlockUnixTimestampMs(this.arweave)).toString(),
},
{
name: 'Epoch-Index',
value: (epoch as { epochIndex?: number })?.epochIndex?.toString(),
Expand All @@ -426,12 +384,6 @@ export class ARIOReadable implements AoARIORead {
async getDistributions(epoch?: EpochInput): Promise<AoEpochDistributionData> {
const allTags = [
{ name: 'Action', value: 'Epoch-Distributions' },
{
name: 'Timestamp',
value:
(epoch as { timestamp?: number })?.timestamp?.toString() ??
(await getCurrentBlockUnixTimestampMs(this.arweave)).toString(),
},
{
name: 'Epoch-Index',
value: (epoch as { epochIndex?: number })?.epochIndex?.toString(),
Expand Down Expand Up @@ -497,19 +449,6 @@ export class ARIOReadable implements AoARIORead {
name: 'Purchase-Type',
value: type,
},
{
name: 'Timestamp',
value: (
await this.arweave.blocks
.getCurrent()
.then((block) => {
return { timestamp: block.timestamp * 1000 };
})
.catch(() => {
return { timestamp: Date.now() }; // fallback to current time
})
).timestamp.toString(),
},
];

return this.process.read<number>({
Expand Down Expand Up @@ -554,19 +493,6 @@ export class ARIOReadable implements AoARIORead {
name: 'Fund-From',
value: fundFrom,
},
{
name: 'Timestamp',
value: (
await this.arweave.blocks
.getCurrent()
.then((block) => {
return { timestamp: block.timestamp * 1000 };
})
.catch(() => {
return { timestamp: Date.now() }; // fallback to current time
})
).timestamp.toString(),
},
];

return this.process.read<CostDetailsResult>({
Expand Down
3 changes: 0 additions & 3 deletions src/types/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ export type EpochInput =
| {
epochIndex: AoEpochIndex;
}
| {
timestamp: Timestamp;
}
| undefined;

// AO/ARIO Contract
Expand Down
17 changes: 1 addition & 16 deletions src/utils/arweave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Arweave from 'arweave';

import { ARWEAVE_TX_REGEX } from '../constants.js';
import { BlockHeight, Timestamp } from '../types/common.js';
import { BlockHeight } from '../types/common.js';
import { PaginationParams } from '../types/io.js';

export const validateArweaveId = (id: string): boolean => {
Expand Down Expand Up @@ -44,19 +42,6 @@ export const pruneTags = (
);
};

export const getCurrentBlockUnixTimestampMs = async (
arweave: Arweave,
): Promise<Timestamp> => {
return await arweave.blocks
.getCurrent()
.then((block) => {
return block.timestamp * 1000;
})
.catch(() => {
return Date.now(); // fallback to current time
});
};

export const paginationParamsToTags = <T>(
params?: PaginationParams<T>,
): { name: string; value: string }[] => {
Expand Down
18 changes: 1 addition & 17 deletions tests/unit/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import Arweave from 'arweave';
import { strict as assert } from 'node:assert';
import { describe, it } from 'node:test';

import {
getCurrentBlockUnixTimestampMs,
pruneTags,
} from '../../src/utils/arweave.js';
import { pruneTags } from '../../src/utils/arweave.js';

describe('pruneTags', () => {
it('should remove tags with undefined values', () => {
Expand Down Expand Up @@ -52,15 +48,3 @@ describe('pruneTags', () => {
assert.deepEqual(prunedTags, []);
});
});

describe('getCurrentBlockUnixTimestamp', () => {
it('should return the current block timestamp', async () => {
const arweave = Arweave.init({});
// cheap way to check the returned timestamp is within the boundaries of the async call
const minTimestamp = Date.now();
const timestamp = await getCurrentBlockUnixTimestampMs(arweave);
const maxTimestamp = Date.now();
assert.ok(timestamp >= minTimestamp);
assert.ok(timestamp <= maxTimestamp);
});
});

0 comments on commit 01d9e47

Please sign in to comment.