Skip to content

Commit

Permalink
fix: dont get old arweave block timestamps on read actions
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Dec 18, 2024
1 parent 3449e32 commit ef0e0ea
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/cli/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export const arnsPurchaseOptions = [
optionMap.fundFrom,
];

export const epochOptions = [optionMap.epochIndex, optionMap.timestamp];
export const epochOptions = [optionMap.epochIndex];

export const addressAndVaultIdOptions = [optionMap.address, optionMap.vaultId];

Expand Down
3 changes: 0 additions & 3 deletions src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,6 @@ export function epochInputFromOptions(options: EpochCLIOptions): EpochInput {
if (options.epochIndex !== undefined) {
return { epochIndex: +options.epochIndex };
}
if (options.timestamp !== undefined) {
return { timestamp: +options.timestamp };
}
return undefined;
}

Expand Down
61 changes: 9 additions & 52 deletions src/common/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Arweave from 'arweave';

import { ARIO_TESTNET_PROCESS_ID } from '../constants.js';
import {
AoArNSNameDataWithName,
Expand Down Expand Up @@ -72,12 +70,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 { defaultArweave } from './arweave.js';
import { paginationParamsToTags, pruneTags } from '../utils/arweave.js';
import { AOProcess } from './contracts/ao-process.js';
import { InvalidContractConfigurationError } from './error.js';

Expand Down Expand Up @@ -118,9 +111,8 @@ export class ARIO {

export class ARIOReadable implements AoARIORead {
protected process: AOProcess;
private arweave: Arweave;

constructor(config?: ProcessConfiguration, arweave = defaultArweave) {
constructor(config?: ProcessConfiguration) {
if (!config) {
this.process = new AOProcess({
processId: ARIO_TESTNET_PROCESS_ID,
Expand All @@ -134,7 +126,6 @@ export class ARIOReadable implements AoARIORead {
} else {
throw new InvalidContractConfigurationError();
}
this.arweave = arweave;
}

async getInfo(): Promise<{
Expand Down Expand Up @@ -170,7 +161,7 @@ export class ARIOReadable implements AoARIORead {
name: 'Timestamp',
value:
(params as { timestamp?: number })?.timestamp?.toString() ??
(await getCurrentBlockUnixTimestampMs(this.arweave)).toString(),
Date.now().toString(),
},
{
name: 'Epoch-Index',
Expand All @@ -189,7 +180,7 @@ export class ARIOReadable implements AoARIORead {
name: 'Timestamp',
value:
(epoch as { timestamp?: number })?.timestamp?.toString() ??
(await getCurrentBlockUnixTimestampMs(this.arweave)).toString(),
Date.now().toString(),
},
{
name: 'Epoch-Index',
Expand Down Expand Up @@ -349,15 +340,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 @@ -370,7 +353,7 @@ export class ARIOReadable implements AoARIORead {
name: 'Timestamp',
value:
(epoch as { timestamp?: number })?.timestamp?.toString() ??
(await getCurrentBlockUnixTimestampMs(this.arweave)).toString(),
Date.now().toString(),
},
{
name: 'Epoch-Index',
Expand All @@ -390,7 +373,7 @@ export class ARIOReadable implements AoARIORead {
name: 'Timestamp',
value:
(epoch as { timestamp?: number })?.timestamp?.toString() ??
(await getCurrentBlockUnixTimestampMs(this.arweave)).toString(),
Date.now().toString(),
},
{
name: 'Epoch-Index',
Expand All @@ -410,7 +393,7 @@ export class ARIOReadable implements AoARIORead {
name: 'Timestamp',
value:
(epoch as { timestamp?: number })?.timestamp?.toString() ??
(await getCurrentBlockUnixTimestampMs(this.arweave)).toString(),
Date.now().toString(),
},
{
name: 'Epoch-Index',
Expand All @@ -430,7 +413,7 @@ export class ARIOReadable implements AoARIORead {
name: 'Timestamp',
value:
(epoch as { timestamp?: number })?.timestamp?.toString() ??
(await getCurrentBlockUnixTimestampMs(this.arweave)).toString(),
Date.now().toString(),
},
{
name: 'Epoch-Index',
Expand Down Expand Up @@ -497,19 +480,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 +524,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
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 ef0e0ea

Please sign in to comment.