Skip to content

Commit

Permalink
read transactions in journal order
Browse files Browse the repository at this point in the history
  • Loading branch information
zoeyTM committed Oct 24, 2024
1 parent 7aa47f3 commit e72e7f4
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 96 deletions.
17 changes: 0 additions & 17 deletions packages/core/src/internal/execution/deployment-state-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> {
const futureIds: string[] = [];

for await (const message of deploymentLoader.readFromJournal()) {
if ("futureId" in message && !futureIds.includes(message.futureId)) {
futureIds.push(message.futureId);
}
}

return futureIds;
}
166 changes: 87 additions & 79 deletions packages/core/src/list-transactions.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
}
}
Expand Down

0 comments on commit e72e7f4

Please sign in to comment.