From b572f496d96fb4d9692f1faca75800c8e3da5616 Mon Sep 17 00:00:00 2001 From: Jack Frain Date: Tue, 7 Jan 2025 15:42:50 -0500 Subject: [PATCH] fix(cu): in findresult, check for exact eval by message id --- servers/cu/src/domain/dal.js | 4 +--- servers/cu/src/domain/lib/chainEvaluation.js | 4 +--- servers/cu/src/domain/lib/loadProcess.js | 4 +--- servers/cu/src/effects/ao-evaluation.js | 19 ++++++++++++++----- servers/cu/src/effects/ao-evaluation.test.js | 10 +++------- servers/cu/src/effects/sqlite.js | 7 +++++++ 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/servers/cu/src/domain/dal.js b/servers/cu/src/domain/dal.js index 606a8edaf..c4f9d5967 100644 --- a/servers/cu/src/domain/dal.js +++ b/servers/cu/src/domain/dal.js @@ -99,9 +99,7 @@ export const evaluatorSchema = z.function() export const findEvaluationSchema = z.function() .args(z.object({ processId: z.string(), - to: z.coerce.number().nullish(), - ordinate: z.coerce.string().nullish(), - cron: z.string().nullish() + messageId: z.string() })) .returns(z.promise(evaluationSchema)) diff --git a/servers/cu/src/domain/lib/chainEvaluation.js b/servers/cu/src/domain/lib/chainEvaluation.js index 803b439fe..ec08b3e32 100644 --- a/servers/cu/src/domain/lib/chainEvaluation.js +++ b/servers/cu/src/domain/lib/chainEvaluation.js @@ -21,9 +21,7 @@ function loadLatestEvaluationWith ({ findEvaluation, findLatestProcessMemory, lo return findEvaluation({ processId: ctx.id, - to: ctx.to, - ordinate: ctx.ordinate, - cron: ctx.cron + messageId: ctx.messageId }) .map((evaluation) => { logger( diff --git a/servers/cu/src/domain/lib/loadProcess.js b/servers/cu/src/domain/lib/loadProcess.js index 099c5a13c..8009303ca 100644 --- a/servers/cu/src/domain/lib/loadProcess.js +++ b/servers/cu/src/domain/lib/loadProcess.js @@ -65,9 +65,7 @@ function loadLatestEvaluationWith ({ findEvaluation, findLatestProcessMemory, sa return findEvaluation({ processId: ctx.id, - to: ctx.to, - ordinate: ctx.ordinate, - cron: ctx.cron + messageId: ctx.messageId }) .map((evaluation) => { logger( diff --git a/servers/cu/src/effects/ao-evaluation.js b/servers/cu/src/effects/ao-evaluation.js index d6ef1eec7..6d90c5391 100644 --- a/servers/cu/src/effects/ao-evaluation.js +++ b/servers/cu/src/effects/ao-evaluation.js @@ -70,8 +70,9 @@ const fromEvaluationDoc = pipe( toEvaluation ) +// TODO: Change the query to use process Id, message Id, order by id export function findEvaluationWith ({ db }) { - function createQuery ({ processId, timestamp, ordinate, cron }) { + function createQuery ({ processId, messageId }) { return { sql: ` SELECT @@ -79,14 +80,22 @@ export function findEvaluationWith ({ db }) { ordinate, "blockHeight", cron, "evaluatedAt", output FROM ${EVALUATIONS_TABLE} WHERE - id = ?; + id > ? AND id <= ? + messageId = ? + ORDER BY + id ASC + LIMIT 1; `, - parameters: [createEvaluationId({ processId, timestamp, ordinate, cron })] + parameters: [ + createEvaluationId({ processId, timestamp: '' }), + createEvaluationId({ processId, timestamp: COLLATION_SEQUENCE_MAX_CHAR }), + messageId + ] } } - return ({ processId, to, ordinate, cron }) => { - return of({ processId, timestamp: to, ordinate, cron }) + return ({ processId, messageId }) => { + return of({ processId, messageId }) .chain(fromPromise((params) => db.query(createQuery(params)))) .map(defaultTo([])) .map(head) diff --git a/servers/cu/src/effects/ao-evaluation.test.js b/servers/cu/src/effects/ao-evaluation.test.js index 925c24e34..3e2d95e7b 100644 --- a/servers/cu/src/effects/ao-evaluation.test.js +++ b/servers/cu/src/effects/ao-evaluation.test.js @@ -18,7 +18,7 @@ describe('ao-evaluation', () => { findEvaluationWith({ db: { query: async ({ parameters }) => { - assert.deepStrictEqual(parameters, ['process-123,1702677252111,1']) + assert.deepStrictEqual(parameters, ['process-123,', 'process-123,􏿿', 'message-123']) return [{ id: 'process-123,1702677252111,1', @@ -41,9 +41,7 @@ describe('ao-evaluation', () => { const res = await findEvaluation({ processId: 'process-123', - to: 1702677252111, - ordinate: '1', - cron: undefined + messageId: 'message-123' }) assert.deepStrictEqual(res, { @@ -73,9 +71,7 @@ describe('ao-evaluation', () => { const res = await findEvaluation({ processId: 'process-123', - to: 1702677252111, - ordinate: '1', - cron: undefined + messageId: 'message-123' }) .catch(err => { assert.equal(err.status, 404) diff --git a/servers/cu/src/effects/sqlite.js b/servers/cu/src/effects/sqlite.js index 95f968afb..9c7acb6db 100644 --- a/servers/cu/src/effects/sqlite.js +++ b/servers/cu/src/effects/sqlite.js @@ -109,6 +109,12 @@ const createCheckpointFilesIndexes = async (db) => db.prepare( (processId, timestamp);` ).run() +const createEvaluationsIndexes = async (db) => db.prepare( + `CREATE INDEX IF NOT EXISTS idx_${EVALUATIONS_TABLE}_id_messageId + ON ${EVALUATIONS_TABLE} + (id, messageId);` +).run() + let internalSqliteDb export async function createSqliteClient ({ url, bootstrap = false, walLimit = bytes.parse('100mb') }) { if (internalSqliteDb) return internalSqliteDb @@ -138,6 +144,7 @@ export async function createSqliteClient ({ url, bootstrap = false, walLimit = b .then(() => createMessagesIndexes(db)) .then(() => createCheckpointsIndexes(db)) .then(() => createCheckpointFilesIndexes(db)) + .then(() => createEvaluationsIndexes(db)) } return {