Skip to content

Commit

Permalink
chore: update script to accept file
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Sep 30, 2024
1 parent 783c46a commit b3c932a
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 2,136 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules
/lua
/lua_modules
/.luarocks
*.csv
4 changes: 4 additions & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
To spawn ants for existing records do the following:

- `node spawn-ants-for-records.mjs --testnet --dry-run --file <path-to-csv-file>` - confirm the total number of process ids that will be created
- `node spawn-ants-for-records.mjs --testnet --file <path-to-csv-file>` - spawn the ants, this will update the file. If errors occur, re-run and it will resume from where it left off.
2,123 changes: 0 additions & 2,123 deletions tools/arns-processid-mapping.csv

This file was deleted.

27 changes: 16 additions & 11 deletions tools/create-csv-from-arns-records.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
import { AOProcess, IO, IO_TESTNET_PROCESS_ID } from '@ar.io/sdk';
import { AOProcess, IO, IO_DEVNET_PROCESS_ID, IO_TESTNET_PROCESS_ID } from '@ar.io/sdk';
import { connect } from '@permaweb/aoconnect';
import path from 'path';
import fs from 'fs';

const __dirname = path.dirname(new URL(import.meta.url).pathname);
const outputFilePath = path.join(__dirname, 'arns-processid-mapping.csv');

const restart = process.argv.includes('--restart');
const testnet = process.argv.includes('--testnet');
async function main() {
const io = IO.init({
process: new AOProcess({
processId: IO_TESTNET_PROCESS_ID,
processId: testnet ? IO_TESTNET_PROCESS_ID : IO_DEVNET_PROCESS_ID,
ao: connect({
CU_URL: 'https://cu.ar-io.dev',
}),
}),
});

const outputFilePath = path.join(__dirname, `arns-processid-mapping-${testnet ? 'testnet' : 'devnet'}.csv`);

const arnsRecords = await io.getArNSRecords({
limit: 100000,
sortBy: 'startTimestamp',
sortOrder: 'asc',
});
// create csv if not exists
fs.writeFileSync(outputFilePath, 'domain,processId\n', { flag: 'w' });

arnsRecords.items.forEach((record) => {
// append column to CSV
// column 1: domain name
// column 2: process ID
// recreate the file if restart is true
if (!fs.existsSync(outputFilePath) || restart) {
fs.writeFileSync(outputFilePath, 'domain,oldProcessId\n', { flag: 'w' });
}

console.log(`Found ${arnsRecords.items.length} ARNS records for process, mapping to CSV for processing`);
arnsRecords.items.forEach((record) => {
fs.writeFileSync(outputFilePath, `${record.name},${record.processId}\n`, {
flag: 'a',
});
});
console.log(`Wrote ${arnsRecords.items.length} ARNS records to CSV`);
}

main();
main();
Empty file.
1 change: 0 additions & 1 deletion tools/new-arns-processid-mapping.csv

This file was deleted.

3 changes: 2 additions & 1 deletion tools/spawn-ants-for-records.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const __dirname = path.dirname(new URL(import.meta.url).pathname);
const restart = process.argv.includes('--restart');
const dryRun = process.argv.includes('--dry-run');
const testnet = process.argv.includes('--testnet');
const inputFilePath = process.argv.includes('--file') ? process.argv[process.argv.indexOf('--file') + 1] : null;
const wallet = JSON.parse(
fs.readFileSync(path.join(__dirname, 'key.json'), 'utf8'),
);
Expand All @@ -30,7 +31,7 @@ const { spawn } = aoClient;

async function main() {
const csv = fs.readFileSync(
path.join(__dirname, `arns-processid-mapping-${testnet ? 'testnet' : 'devnet'}.csv`),
path.join(__dirname, inputFilePath),
'utf8',
);

Expand Down
138 changes: 138 additions & 0 deletions tools/update-code-for-new-processes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import {
ArweaveSigner,
createAoSigner,
} from '@ar.io/sdk';
import Arweave from 'arweave';
import { connect } from '@permaweb/aoconnect';
import path from 'path';
import fs from 'fs';

const __dirname = path.dirname(new URL(import.meta.url).pathname);
const restart = process.argv.includes('--restart');
const dryRun = process.argv.includes('--dry-run');
const wallet = JSON.parse(
fs.readFileSync(path.join(__dirname, 'key.json'), 'utf8'),
);
const signer = new ArweaveSigner(wallet);
const testnet = process.argv.includes('--testnet');
const arweave = Arweave.init({
host: 'arweave.net',
port: 443,
protocol: 'https',
});
const aoClient = connect({
CU_URL: 'https://cu.ar-io.dev',
});
const { message, result } = aoClient;
const bundledCode = fs.readFileSync(path.join(__dirname, 'aos-bundled.lua'), 'utf8');

async function main() {
const csv = fs.readFileSync(
path.join(__dirname, `new-arns-processid-mapping-${testnet ? 'testnet' : 'devnet'}.csv`),
'utf8',
);

const outputFilePath = path.join(__dirname, `new-evaluated-processids-${testnet ? 'testnet' : 'devnet'}.csv`);

// print out address of wallet being used
const address = await arweave.wallets.jwkToAddress(wallet);
console.log(`Using wallet ${address} to evaluate ants`);

const newlyCreatedProcessIds = csv
.split('\n')
.slice(1) // skip header
.map((line) => line.split(','))
.filter(([domain, oldProcessId, newProcessId]) => domain && oldProcessId && newProcessId && oldProcessId !== newProcessId);

// create output csv if not exists including eval result
if (!fs.existsSync(outputFilePath) || restart) {
fs.writeFileSync(outputFilePath, 'domain,oldProcessId,newProcessId,evalResult\n', { flag: 'w' });
}

const antsToEvaluate = [];
const processMap = new Map();
let lastEvalProcessId;

// if any failed previously, we want to retry so add them our list
if (!restart) {
const existingRecords = fs.readFileSync(outputFilePath, 'utf8')
.split('\n')
.slice(1) // Skip header
.filter(line => line.trim() !== '')
.map(line => line.split(','));

for (const [domain, oldProcessId, newProcessId, evalResult] of existingRecords) {
lastEvalProcessId = newProcessId;
if(processMap.has(newProcessId) && processMap.get(newProcessId)) {
continue;
}
processMap.set(newProcessId, evalResult === 'true');
}

console.log(`Loaded ${antsToEvaluate.length} ants to evaluate.`);
}

if (lastEvalProcessId) {
console.log(`Last eval process id: ${lastEvalProcessId}`);
}

const indexOfAntToEval = newlyCreatedProcessIds.findIndex(([domain, oldProcessId, newProcessId]) => newProcessId === lastEvalProcessId);
const processIdsToEval = [...antsToEvaluate, ...newlyCreatedProcessIds.slice(indexOfAntToEval + 1)];

console.log(`Evaluating ${processIdsToEval.length} unique ants`);

// process map - don't re-evaluate ants that have already been evaluated

for (const [domain, oldProcessId, newProcessId] of processIdsToEval) {
console.log(`Evaluating ant ${newProcessId}`);

// don't eval if we already have on the process map
if(processMap.has(newProcessId)) {
console.log(`Skipping ${newProcessId} as it has already been evaluated`);
continue;
}

if (dryRun) {
console.log(`Dry run, skipping actual evaluation of ant ${newProcessId}`);
processMap.set(newProcessId, true);
fs.writeFileSync(outputFilePath, `${domain},${oldProcessId},${newProcessId},true\n`, {
flag: 'a',
});
continue;
}

const evalMessageId = await message({
signer: createAoSigner(signer),
process: newProcessId,
tags: [
{
name: 'Action',
value: 'Eval'
},
{
name: 'For-Domain',
value: domain
},
{
name: 'Old-Process-Id',
value: oldProcessId
}
],
data: bundledCode
})

// crank the MU to ensure eval is processed
await result({
message: evalMessageId,
process: newProcessId,
});

fs.writeFileSync(outputFilePath, `${domain},${oldProcessId},${newProcessId},true\n`, {
flag: 'a',
});

processMap.set(newProcessId, true);
}
}

main();

0 comments on commit b3c932a

Please sign in to comment.