Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove Timestamp from epoch inputs #324

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' }],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work on devnet without Date.now()? if so, sweet.

Copy link
Contributor Author

@fedellen fedellen Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes 👍

{"level":"debug","message":"Evaluating read interaction on contract","name":"ar-io-sdk","tags":[{"name":"Action","value":"Epoch"}],"timestamp":"2024-12-18T22:36:42.882Z","version":"3.1.0-alpha.6"}
{"level":"debug","message":"Read interaction result","name":"ar-io-sdk","result":{"Assignments":[],"GasUsed":2131764549,"Messages":[{"Anchor":"00000000000000000000000000003903","Data":"{\"epochIndex\":176,\"startHeight\":1570443,\"distributions\":{\"totalEligibleObserverReward\":162453809,\"totalEligibleRewards\":22743533280,\"rewards\":{\"eligible\":{\"rweM0QUKbLVHADi2jyAvtOYgQv7lhp61NtX2ftXpANY\":{\"delegateRewards\":{\"Rlz7oEBtntc0EXkiK2p_L981kY_MYnp_LG7SUESo5_Q\":1543311186},\"operatorReward\":81226905},\"J40R1BgFSI1_7p25QW49T7P46BePJJnlDrsFGY1YWbM\":{\"delegateRewards\":[],\"operatorReward\":1624538091},\"Rlz7oEBtntc0EXkiK2p_L981kY_MYnp_LG7SUESo5_Q\":{\"delegateRewards\":[],\"operatorReward\":1624538091},\"Rc80LG6h27Y3p9TN6J5hwDeG5M51cu671YwZpU9uAVE\":{\"delegateRewards\":{\"rweM0QUKbLVHADi2jyAvtOYgQv7lhp61NtX2ftXpANY\":1066536594,\"jaxl_dxqJ00gEgQazGASFXVRvO4h-Q0_vnaLtuOUoWU\":476774591},\"operatorReward\":81226905},\"E-QVU3dta36Wia2uQw6tQLjQk7Qw5uN0Z6fUzsoqzUc\":{\"delegateRewards\":{\"rweM0QUKbLVHADi2jyAvtOYgQv7lhp61NtX2ftXpANY\":1543311186},\"operatorReward\":81226905},\"QGWqtJdLLgm2ehFWiiPzMaoFLD50CnGuzZIPEdoDRGQ\":{\"delegateRewards\":{\"N4h8M9A9hasa3tF47qQyNvcKjm4APBKuFs7vqUVm-SI\":82443443,\"PZ5vIhHf8VY969TxBPQN-rYY9CNFP9ggNsMBqlWUzWM\":32004145,\"JcC4ZLUY76vmWha5y6RwKsFqYTrMZhbockl8iM9p5lQ\":48006219},\"operatorReward\":1462084282},\"M0WP8KSzCvKpzC-HPF1WcddLgGaL9J4DGi76iMnhrN4\":{\"delegateRewards\":{\"N4h8M9A9hasa3tF47qQyNvcKjm4APBKuFs7vqUVm-SI\":324907618},\"operatorReward\":1299630473},\"iKryOeZQMONi2965nKz528htMMN_sBcjlhc-VncoRjA\":{\"delegateRewards\":[],\"operatorReward\":1624538091},\"q6zIf3KQRMCW9fytR0YlKG4oqw6Cox4r_bk7mq6JZBM\":{\"delegateRewards\":[],\"operatorReward\":1624538091},\"LtV0aSqgK3YI7c5FmfvZd-wG95TJ9sezj_a4syaLMS8\":{\"delegateRewards\":{\"rweM0QUKbLVHADi2jyAvtOYgQv7lhp61NtX2ftXpANY\":1462084281},\"operatorReward\":162453810},\"1H7WZIWhzwTH9FIcnuMqYkTsoyv1OTfGa_amvuYwrgo\":{\"delegateRewards\":[],\"operatorReward\":1624538091},\"jaxl_dxqJ00gEgQazGASFXVRvO4h-Q0_vnaLtuOUoWU\":{\"delegateRewards\":{\"J40R1BgFSI1_7p25QW49T7P46BePJJnlDrsFGY1YWbM\":162453809},\"operatorReward\":1462084282},\"wlcEhTQY_qjDKTvTDZsb53aX8wivbOJZKnhLswdueZw\":{\"delegateRewards\":[],\"operatorReward\":1624538091},\"xN_aVln30LmoCffwmk5_kRkcyQZyZWy1o_TNtM_CTm0\":{\"delegateRewards\":[],\"operatorReward\":1624538091}}},\"totalEligibleGateways\":14,\"totalEligibleGatewayReward\":1462084282},\"endTimestamp\":1734588000000,\"prescribedObservers\":[{\"gatewayRewardRatioWeight\":1,\"tenureWeight\":0.9777777777777777457,\"stakeWeight\":38.724997919400003354,\"compositeWeight\":6.2746790279561146519,\"observerAddress\":\"1H7WZIWhzwTH9FIcnuMqYkTsoyv1OTfGa_amvuYwrgo\",\"gatewayAddress\":\"1H7WZIWhzwTH9FIcnuMqYkTsoyv1OTfGa_amvuYwrgo\",\"startTimestamp\":1719295200000,\"normalizedCompositeWeight\":0.8715690509236552197,\"stake\":387249979194,\"observerRewardRatioWeight\":0.16571428571428570287},{\"gatewayRewardRatioWeight\":1,\"tenureWeight\":0.9777777777777777457,\"stakeWeight\":26.255397969699998839,\"compositeWeight\":0.1466968267513396873,\"observerAddress\":\"QGWqtJdLLgm2ehFWiiPzMaoFLD50CnGuzZIPEdoDRGQ\",\"gatewayAddress\":\"QGWqtJdLLgm2ehFWiiPzMaoFLD50CnGuzZIPEdoDRGQ\",\"startTimestamp\":1719295200000,\"normalizedCompositeWeight\":0.020376566434000431549,\"stake\":250000000000,\"observerRewardRatioWeight\":0.0057142857142857142808},{\"gatewayRewardRatioWeight\":1,\"tenureWeight\":0.9777777777777777457,\"stakeWeight\":25,\"compositeWeight\":0.13968253968253968589,\"observerAddress\":\"wlcEhTQY_qjDKTvTDZsb53aX8wivbOJZKnhLswdueZw\",\"gatewayAddress\":\"wlcEhTQY_qjDKTvTDZsb53aX8wivbOJZKnhLswdueZw\",\"startTimestamp\":1719295200000,\"normalizedCompositeWeight\":0.019402263924466096878,\"stake\":250000000000,\"observerRewardRatioWeight\":0.0057142857142857142808},{\"gatewayRewardRatioWeight\":0.85795454545454541417,\"tenureWeight\":0.9777777777777777457,\"stakeWeight\":25.736584157599999401,\"compositeWeight\":0.12337219707293968496,\"observerAddress\":\"q6zIf3KQRMCW9fytR0YlKG4oqw6Cox4r_bk7mq6JZBM\",\"gatewayAddress\":\"q6zIf3KQRMCW9fytR0YlKG4oqw6Cox4r_bk7mq6JZBM\",\"startTimestamp\":1719295200000,\"normalizedCompositeWeight\":0.017136715397576867542,\"stake\":257365841576,\"observerRewardRatioWeight\":0.0057142857142857142808},{\"gatewayRewardRatioWeight\":0.85875706214689262463,\"tenureWeight\":0.9777777777777777457,\"stakeWeight\":25.746584157600000964,\"compositeWeight\":0.12283367206387947601,\"observerAddress\":\"M0WP8KSzCvKpzC-HPF1WcddLgGaL9J4DGi76iMnhrN4\",\"gatewayAddress\":\"M0WP8KSzCvKpzC-HPF1WcddLgGaL9J4DGi76iMnhrN4\",\"startTimestamp\":1719295200000,\"normalizedCompositeWeight\":0.017061912889121205739,\"stake\":249326055188,\"observerRewardRatioWeight\":0.0056818181818181819759},{\"gatewayRewardRatioWeight\":0.86931818181818176772,\"tenureWeight\":0.9777777777777777457,\"stakeWeight\":5,\"compositeWeight\":0.097142857142857128028,\"observerAddress\":\"iKryOeZQMONi2965nKz528htMMN_sBcjlhc-VncoRjA\",\"gatewayAddress\":\"iKryOeZQMONi2965nKz528htMMN_sBcjlhc-VncoRjA\",\"startTimestamp\":1719295200000,\"normalizedCompositeWeight\":0.013493392638378693543,\"stake\":50000000000,\"observerRewardRatioWeight\":0.022857142857142857123},{\"gatewayRewardRatioWeight\":0.85795454545454541417,\"tenureWeight\":0.97407515624999996273,\"stakeWeight\":18.890942695100001458,\"compositeWeight\":0.090213665310822685472,\"observerAddress\":\"rweM0QUKbLVHADi2jyAvtOYgQv7lhp61NtX2ftXpANY\",\"gatewayAddress\":\"rweM0QUKbLVHADi2jyAvtOYgQv7lhp61NtX2ftXpANY\",\"startTimestamp\":1719352783170,\"normalizedCompositeWeight\":0.012530910076034559864,\"stake\":182347848195,\"observerRewardRatioWeight\":0.0057142857142857142808},{\"gatewayRewardRatioWeight\":0.93333333333333334814,\"tenureWeight\":0.16462944039351851133,\"stakeWeight\":12.088611636599999599,\"compositeWeight\":0.064050526814143624033,\"observerAddress\":\"E-QVU3dta36Wia2uQw6tQLjQk7Qw5uN0Z6fUzsoqzUc\",\"gatewayAddress\":\"E-QVU3dta36Wia2uQw6tQLjQk7Qw5uN0Z6fUzsoqzUc\",\"startTimestamp\":1731941282943,\"normalizedCompositeWeight\":0.0088967828661583817779,\"stake\":111510748770,\"observerRewardRatioWeight\":0.034482758620689654694},{\"gatewayRewardRatioWeight\":0.93103448275862066286,\"tenureWeight\":0.15769376286008229826,\"stakeWeight\":8.306565332099999921,\"compositeWeight\":0.043555573496148851376,\"observerAddress\":\"jaxl_dxqJ00gEgQazGASFXVRvO4h-Q0_vnaLtuOUoWU\",\"gatewayAddress\":\"jaxl_dxqJ00gEgQazGASFXVRvO4h-Q0_vnaLtuOUoWU\",\"startTimestamp\":1732049146600,\"normalizedCompositeWeight\":0.0060499811520780587618,\"stake\":80172623276,\"observerRewardRatioWeight\":0.035714285714285712303},{\"gatewayRewardRatioWeight\":1,\"tenureWeight\":0.11371482233796295636,\"stakeWeight\":6.0401961938999999546,\"compositeWeight\":0.034342991853788931711,\"observerAddress\":\"Rc80LG6h27Y3p9TN6J5hwDeG5M51cu671YwZpU9uAVE\",\"gatewayAddress\":\"Rc80LG6h27Y3p9TN6J5hwDeG5M51cu671YwZpU9uAVE\",\"startTimestamp\":1732733107083,\"normalizedCompositeWeight\":0.0047703298738510377733,\"stake\":32213778936,\"observerRewardRatioWeight\":0.050000000000000002776},{\"gatewayRewardRatioWeight\":0.89772727272727270709,\"tenureWeight\":0.9777777777777777457,\"stakeWeight\":5,\"compositeWeight\":0.025079365079365076308,\"observerAddress\":\"xN_aVln30LmoCffwmk5_kRkcyQZyZWy1o_TNtM_CTm0\",\"gatewayAddress\":\"xN_aVln30LmoCffwmk5_kRkcyQZyZWy1o_TNtM_CTm0\",\"startTimestamp\":1719295200000,\"normalizedCompositeWeight\":0.0034835882955291399242,\"stake\":50000000000,\"observerRewardRatioWeight\":0.0057142857142857142808},{\"gatewayRewardRatioWeight\":1,\"tenureWeight\":0.11811964313271604288,\"stakeWeight\":3.9284300591000000935,\"compositeWeight\":0.022096416983463169209,\"observerAddress\":\"LtV0aSqgK3YI7c5FmfvZd-wG95TJ9sezj_a4syaLMS8\",\"gatewayAddress\":\"LtV0aSqgK3YI7c5FmfvZd-wG95TJ9sezj_a4syaLMS8\",\"startTimestamp\":1732664603310,\"normalizedCompositeWeight\":0.0030692491350212580854,\"stake\":26885195728,\"observerRewardRatioWeight\":0.047619047619047616404},{\"gatewayRewardRatioWeight\":1,\"tenureWeight\":0.03099603530092592471,\"stakeWeight\":1.5854293432999999602,\"compositeWeight\":0.0098284047784101217365,\"observerAddress\":\"Rlz7oEBtntc0EXkiK2p_L981kY_MYnp_LG7SUESo5_Q\",\"gatewayAddress\":\"Rlz7oEBtntc0EXkiK2p_L981kY_MYnp_LG7SUESo5_Q\",\"startTimestamp\":1734019549659,\"normalizedCompositeWeight\":0.0013651906952765234873,\"stake\":15854293433,\"observerRewardRatioWeight\":0.2000000000000000111},{\"gatewayRewardRatioWeight\":1,\"tenureWeight\":0.11898008629115226775,\"stakeWeight\":1.008999999999999897,\"compositeWeight\":0.0057167098603701244769,\"observerAddress\":\"J40R1BgFSI1_7p25QW49T7P46BePJJnlDrsFGY1YWbM\",\"gatewayAddress\":\"J40R1BgFSI1_7p25QW49T7P46BePJJnlDrsFGY1YWbM\",\"startTimestamp\":1732651221698,\"normalizedCompositeWeight\":0.00079406569885243527985,\"stake\":10090000000,\"observerRewardRatioWeight\":0.047619047619047616404}],\"prescribedNames\":[\"dwedwedwedddd\",\"helpmeeeeddededede\"],\"observations\":{\"failureSummaries\":[],\"reports\":[]},\"startTimestamp\":1734501600000,\"distributionTimestamp\":1734589800000}","Tags":[{"name":"Data-Protocol","value":"ao"},{"name":"Variant","value":"ao.TN.1"},{"name":"Type","value":"Message"},{"name":"Reference","value":"3903"},{"name":"Action","value":"Epoch-Notice"},{"name":"X-Origin","value":""},{"name":"X-Reference","value":""}],"Target":"1234"}],"Output":{"data":"Pruning state at timestamp: 1734561391338\n{\"Type\":\"Message\",\"Timestamp\":1734561391338,\"Variant\":\"ao.TN.1\",\"Cast\":false,\"Cron\":false,\"From\":\"1234\",\"Action\":\"Epoch\",\"_e\":1,\"From-Formatted\":\"1234\",\"Epoch-Index\":176,\"Data-Protocol\":\"ao\",\"Message-Id\":\"1234\"}","print":true,"prompt":"\u001b[32mDevnet ARIO\u001b[90m@\u001b[34maos-2.0.1\u001b[90m[Inbox:\u001b[31m267\u001b[90m]\u001b[0m> "},"Spawns":[]},"timestamp":"2024-12-18T22:37:04.324Z","version":"3.1.0-alpha.6"}```

});
}

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);
});
});
Loading