From e72e7f466483e29bda1968abd4cc401201d76d96 Mon Sep 17 00:00:00 2001 From: zoeyTM Date: Wed, 23 Oct 2024 06:49:14 -0400 Subject: [PATCH] read transactions in journal order --- .../execution/deployment-state-helpers.ts | 17 -- packages/core/src/list-transactions.ts | 166 +++++++++--------- 2 files changed, 87 insertions(+), 96 deletions(-) diff --git a/packages/core/src/internal/execution/deployment-state-helpers.ts b/packages/core/src/internal/execution/deployment-state-helpers.ts index 1b6f197cd..4b4fac442 100644 --- a/packages/core/src/internal/execution/deployment-state-helpers.ts +++ b/packages/core/src/internal/execution/deployment-state-helpers.ts @@ -89,20 +89,3 @@ export function shouldBeJournaled(message: JournalMessage): boolean { return true; } - -/** - * Reads the journal and returns an array of future IDs in the order they were executed. - */ -export async function getExecutionOrder( - deploymentLoader: DeploymentLoader -): Promise { - const futureIds: string[] = []; - - for await (const message of deploymentLoader.readFromJournal()) { - if ("futureId" in message && !futureIds.includes(message.futureId)) { - futureIds.push(message.futureId); - } - } - - return futureIds; -} diff --git a/packages/core/src/list-transactions.ts b/packages/core/src/list-transactions.ts index a2e3fe903..74b39154b 100644 --- a/packages/core/src/list-transactions.ts +++ b/packages/core/src/list-transactions.ts @@ -1,12 +1,11 @@ import type { ArtifactResolver } from "./types/artifact"; +import findLastIndex from "lodash/findLastIndex"; + import { IgnitionError } from "./errors"; import { FileDeploymentLoader } from "./internal/deployment-loader/file-deployment-loader"; import { ERRORS } from "./internal/errors-list"; -import { - getExecutionOrder, - loadDeploymentState, -} from "./internal/execution/deployment-state-helpers"; +import { loadDeploymentState } from "./internal/execution/deployment-state-helpers"; import { ExecutionResultType } from "./internal/execution/types/execution-result"; import { ExecutionSateType, @@ -19,6 +18,7 @@ import { type Transaction, TransactionReceiptStatus, } from "./internal/execution/types/jsonrpc"; +import { JournalMessageType } from "./internal/execution/types/messages"; import { assertIgnitionInvariant } from "./internal/utils/assertions"; import { type ListTransactionsResult, @@ -48,86 +48,94 @@ export async function listTransactions( }); } - const executionOrder = await getExecutionOrder(deploymentLoader); const transactions: ListTransactionsResult = []; - for (const futureId of executionOrder) { - const exState = deploymentState.executionStates[futureId]; - - if (!doesSendTransactions(exState)) { + for await (const message of deploymentLoader.readFromJournal()) { + if (message.type !== JournalMessageType.TRANSACTION_SEND) { continue; } - for (const networkInteraction of exState.networkInteractions) { - assertIgnitionInvariant( - networkInteraction.type === "ONCHAIN_INTERACTION", - "Expected network interaction to be an onchain interaction" - ); - - for (const [ - index, - transaction, - ] of networkInteraction.transactions.entries()) { - switch (exState.type) { - case ExecutionSateType.DEPLOYMENT_EXECUTION_STATE: { - transactions.push({ - type: exState.type, - from: exState.from, - txHash: transaction.hash, - status: getTransactionStatus( - transaction, - index === networkInteraction.transactions.length - 1 - ), - name: exState.contractName, - address: - transaction.receipt?.status === TransactionReceiptStatus.SUCCESS - ? exState.result?.type === ExecutionResultType.SUCCESS - ? exState.result.address - : undefined - : undefined, - params: exState.constructorArgs, - value: networkInteraction.value, - }); - - break; - } - case ExecutionSateType.CALL_EXECUTION_STATE: { - const artifact = await deploymentLoader.loadArtifact( - exState.artifactId - ); - - transactions.push({ - type: exState.type, - from: exState.from, - txHash: transaction.hash, - status: getTransactionStatus( - transaction, - index === networkInteraction.transactions.length - 1 - ), - name: `${artifact.contractName}#${exState.functionName}`, - to: networkInteraction.to, - params: exState.args, - value: networkInteraction.value, - }); - - break; - } - case ExecutionSateType.SEND_DATA_EXECUTION_STATE: { - transactions.push({ - type: exState.type, - from: exState.from, - txHash: transaction.hash, - status: getTransactionStatus( - transaction, - index === networkInteraction.transactions.length - 1 - ), - to: networkInteraction.to, - value: networkInteraction.value, - }); - - break; - } - } + const exState = deploymentState.executionStates[message.futureId]; + + assertIgnitionInvariant( + doesSendTransactions(exState), + "Expected execution state to be a type that sends transactions" + ); + + const networkInteraction = + exState.networkInteractions[message.networkInteractionId - 1]; + + assertIgnitionInvariant( + networkInteraction.type === "ONCHAIN_INTERACTION", + "Expected network interaction to be an onchain interaction" + ); + + // this seems redundant, but we use it later to determine pending vs dropped status + const lastTxIndex = findLastIndex( + networkInteraction.transactions, + (tx) => tx.hash === message.transaction.hash + ); + + const transaction = networkInteraction.transactions[lastTxIndex]; + + switch (exState.type) { + case ExecutionSateType.DEPLOYMENT_EXECUTION_STATE: { + transactions.push({ + type: exState.type, + from: exState.from, + txHash: transaction.hash, + status: getTransactionStatus( + transaction, + lastTxIndex === networkInteraction.transactions.length - 1 + ), + name: exState.contractName, + address: + transaction.receipt?.status === TransactionReceiptStatus.SUCCESS + ? exState.result?.type === ExecutionResultType.SUCCESS + ? exState.result.address + : undefined + : undefined, + params: exState.constructorArgs, + value: networkInteraction.value, + }); + + break; + } + case ExecutionSateType.CALL_EXECUTION_STATE: { + const artifact = await deploymentLoader.loadArtifact( + exState.artifactId + ); + + transactions.push({ + type: exState.type, + from: exState.from, + txHash: transaction.hash, + status: getTransactionStatus( + transaction, + lastTxIndex === networkInteraction.transactions.length - 1 + ), + name: `${artifact.contractName}#${exState.functionName}`, + to: networkInteraction.to, + params: exState.args, + value: networkInteraction.value, + }); + + break; + } + case ExecutionSateType.SEND_DATA_EXECUTION_STATE: { + transactions.push({ + type: exState.type, + from: exState.from, + txHash: transaction.hash, + status: getTransactionStatus( + transaction, + lastTxIndex === networkInteraction.transactions.length - 1 + ), + to: networkInteraction.to, + value: networkInteraction.value, + }); + + break; } } }