Skip to content

Commit

Permalink
Adds deployment stamp modification
Browse files Browse the repository at this point in the history
  • Loading branch information
3commascapital committed Aug 30, 2024
1 parent c1ccf67 commit 736b1f3
Show file tree
Hide file tree
Showing 26 changed files with 234 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { DeploymentStamp } from "../journal/types/deployment-stamp";

import { Artifact, ArtifactResolver, BuildInfo } from "../../types/artifact";
import { ExecutionEventListener } from "../../types/execution-events";
import { JournalMessage } from "../execution/types/messages";
Expand All @@ -16,6 +18,10 @@ export class EphemeralDeploymentLoader implements DeploymentLoader {
private _journal: Journal;

private _deployedAddresses: { [key: string]: string };

private _deploymentStamps: {
[key: string]: DeploymentStamp;
};
private _savedArtifacts: {
[key: string]:
| { _kind: "artifact"; artifact: Artifact }
Expand All @@ -28,6 +34,7 @@ export class EphemeralDeploymentLoader implements DeploymentLoader {
) {
this._journal = new MemoryJournal(this._executionEventListener);
this._deployedAddresses = {};
this._deploymentStamps = {};
this._savedArtifacts = {};
}

Expand All @@ -41,9 +48,13 @@ export class EphemeralDeploymentLoader implements DeploymentLoader {

public async recordDeployedAddress(
futureId: string,
contractAddress: string
address: string,
deploymentStamp?: DeploymentStamp
): Promise<void> {
this._deployedAddresses[futureId] = contractAddress;
this._deployedAddresses[futureId] = address;
if (deploymentStamp !== undefined) {
this._deploymentStamps[futureId] = { ...deploymentStamp };
}
}

public async storeBuildInfo(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { DeploymentStamp } from "../journal/types/deployment-stamp";

import { ensureDir, pathExists, readFile, writeFile } from "fs-extra";
import path from "path";

Expand All @@ -9,6 +11,27 @@ import { Journal } from "../journal/types";

import { DeploymentLoader } from "./types";

const updateJsonFile = async <T>(
pathToFile: string,
updater: (j: Record<string, T>) => Record<string, T>
) => {
let fileContents: { [key: string]: T };
if (await pathExists(pathToFile)) {
const json = (await readFile(pathToFile)).toString();

fileContents = JSON.parse(json);
} else {
fileContents = {};
}

fileContents = updater(fileContents);

await writeFile(
pathToFile,
`${JSON.stringify(fileContents, undefined, 2)}\n`
);
};

export class FileDeploymentLoader implements DeploymentLoader {
private _journal: Journal;
private _deploymentDirsEnsured: boolean;
Expand All @@ -19,6 +42,7 @@ export class FileDeploymentLoader implements DeploymentLoader {
buildInfoDir: string;
journalPath: string;
deployedAddressesPath: string;
deployedDeploymentStampPath: string;
};

constructor(
Expand All @@ -32,6 +56,10 @@ export class FileDeploymentLoader implements DeploymentLoader {
this._deploymentDirPath,
"deployed_addresses.json"
);
const deployedDeploymentStampPath = path.join(
this._deploymentDirPath,
"deployment_stamps.json"
);

this._journal = new FileJournal(journalPath, this._executionEventListener);

Expand All @@ -41,6 +69,7 @@ export class FileDeploymentLoader implements DeploymentLoader {
buildInfoDir,
journalPath,
deployedAddressesPath,
deployedDeploymentStampPath,
};

this._deploymentDirsEnsured = false;
Expand Down Expand Up @@ -151,27 +180,27 @@ export class FileDeploymentLoader implements DeploymentLoader {

public async recordDeployedAddress(
futureId: string,
contractAddress: string
address: string,
deploymentStamp?: DeploymentStamp
): Promise<void> {
await this._initialize();

let deployedAddresses: { [key: string]: string };
if (await pathExists(this._paths.deployedAddressesPath)) {
const json = (
await readFile(this._paths.deployedAddressesPath)
).toString();

deployedAddresses = JSON.parse(json);
} else {
deployedAddresses = {};
}

deployedAddresses[futureId] = contractAddress;

await writeFile(
await updateJsonFile<string>(
this._paths.deployedAddressesPath,
`${JSON.stringify(deployedAddresses, undefined, 2)}\n`
(contents) => ({
...contents,
[futureId]: address,
})
);
if (deploymentStamp !== undefined) {
await updateJsonFile<DeploymentStamp>(
this._paths.deployedDeploymentStampPath,
(contents) => ({
...contents,
[futureId]: { ...deploymentStamp },
})
);
}
}

private async _initialize(): Promise<void> {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/internal/deployment-loader/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { DeploymentStamp } from "../journal/types/deployment-stamp";

import { Artifact, BuildInfo } from "../../types/artifact";
import { JournalMessage } from "../execution/types/messages";

Expand All @@ -22,6 +24,7 @@ export interface DeploymentLoader {
storeBuildInfo(futureId: string, buildInfo: BuildInfo): Promise<void>;
recordDeployedAddress(
futureId: string,
contractAddress: string
contractAddress: string,
deploymentStamp?: DeploymentStamp
): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
StaticCallExecutionState,
} from "../types/execution-state";
import { ExecutionStrategy } from "../types/execution-strategy";
import { TransactionReceiptStatus } from "../types/jsonrpc";
import { JournalMessage, JournalMessageType } from "../types/messages";

import { monitorOnchainInteraction } from "./handlers/monitor-onchain-interaction";
Expand Down Expand Up @@ -144,13 +145,31 @@ export class FutureProcessor {
lastAppliedMessage: JournalMessage
) {
if (
lastAppliedMessage.type === JournalMessageType.TRANSACTION_CONFIRM &&
lastAppliedMessage.receipt.status === TransactionReceiptStatus.SUCCESS
) {
const { receipt, futureId } = lastAppliedMessage;
const { contractAddress, blockNumber, transactionHash } = receipt;
if (contractAddress !== null && contractAddress !== undefined) {
await this._deploymentLoader.recordDeployedAddress(
futureId,
contractAddress,
{
address: contractAddress,
blockNumber: Number(blockNumber),
transactionHash,
}
);
}
} else if (
lastAppliedMessage.type ===
JournalMessageType.DEPLOYMENT_EXECUTION_STATE_COMPLETE &&
lastAppliedMessage.result.type === ExecutionResultType.SUCCESS
) {
await this._deploymentLoader.recordDeployedAddress(
lastAppliedMessage.futureId,
lastAppliedMessage.result.address
lastAppliedMessage.result.address,
lastAppliedMessage.result.deploymentStamp
);
} else if (
lastAppliedMessage.type ===
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/internal/execution/jsonrpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,10 @@ export class EIP1193JsonRpcClient implements JsonRpcClient {
): Promise<TransactionReceipt | undefined> {
const method = "eth_getTransactionReceipt";

const response = await this._provider.request({
const response = (await this._provider.request({
method,
params: [txHash],
});
})) as TransactionReceipt | null;

if (response === null) {
return undefined;
Expand Down Expand Up @@ -622,6 +622,7 @@ export class EIP1193JsonRpcClient implements JsonRpcClient {
: toChecksumFormat(contractAddress),
status,
logs: formatReceiptLogs(method, response),
transactionHash: response.transactionHash,
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SolidityParameterType } from "../../../types/module";
import { DeploymentStamp } from "../../journal/types/deployment-stamp";

import { FailedEvmExecutionResult } from "./evm-execution";

Expand Down Expand Up @@ -76,6 +77,7 @@ export interface StrategyHeldExecutionResult {
export interface SuccessfulDeploymentExecutionResult {
type: ExecutionResultType.SUCCESS;
address: string;
deploymentStamp?: DeploymentStamp;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/internal/execution/types/jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface TransactionReceipt {
contractAddress?: string;
status: TransactionReceiptStatus;
logs: TransactionLog[];
transactionHash: string;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/internal/journal/types/deployment-stamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface DeploymentStamp {
address: string;
blockNumber: number;
transactionHash: string;
}

export const zeroHash =
"0x0000000000000000000000000000000000000000000000000000000000000000";
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ExecutionResultType,
SendDataExecutionResult,
StaticCallExecutionResult,
SuccessfulDeploymentExecutionResult,
} from "../../execution/types/execution-result";
import {
JournalMessage,
Expand Down Expand Up @@ -203,9 +204,10 @@ function convertExecutionResultToEventResult(
): ExecutionEventResult {
switch (result.type) {
case ExecutionResultType.SUCCESS: {
const res = result as SuccessfulDeploymentExecutionResult;
return {
type: ExecutionEventResultType.SUCCESS,
result: "address" in result ? result.address : undefined,
result: res.address,
};
}
case ExecutionResultType.STATIC_CALL_ERROR:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { DeploymentState } from "../execution/types/deployment-state";
import { ExecutionResultType } from "../execution/types/execution-result";
import {
ExecutionResultType,
SuccessfulDeploymentExecutionResult,
} from "../execution/types/execution-result";
import { ExecutionSateType } from "../execution/types/execution-state";
import { assertIgnitionInvariant } from "../utils/assertions";

Expand Down Expand Up @@ -45,5 +48,6 @@ export function findAddressForContractFuture(
`Cannot access the result of ${futureId}, it was not a deployment success`
);

return exState.result.address;
const result = exState.result as SuccessfulDeploymentExecutionResult;
return result.address;
}
16 changes: 14 additions & 2 deletions packages/core/src/internal/views/find-deployed-contracts.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { DeployedContract } from "../../types/deploy";
import { DeploymentState } from "../execution/types/deployment-state";
import { ExecutionResultType } from "../execution/types/execution-result";
import {
ExecutionResultType,
SuccessfulDeploymentExecutionResult,
} from "../execution/types/execution-result";
import {
ContractAtExecutionState,
DeploymentExecutionState,
ExecutionSateType,
ExecutionStatus,
} from "../execution/types/execution-state";
import { zeroHash } from "../journal/types/deployment-stamp";
import { assertIgnitionInvariant } from "../utils/assertions";

export function findDeployedContracts(deploymentState: DeploymentState): {
Expand Down Expand Up @@ -39,17 +43,25 @@ function _toDeployedContract(
`Deployment execution state ${des.id} should have a successful result to retrieve address`
);

const res = des.result as SuccessfulDeploymentExecutionResult;
return {
id: des.id,
contractName: des.contractName,
address: des.result.address,
address: res.address,
transactionHash: zeroHash,
blockNumber: 0,
...(res.deploymentStamp !== null && res.deploymentStamp !== undefined
? res.deploymentStamp
: {}),
};
}
case ExecutionSateType.CONTRACT_AT_EXECUTION_STATE: {
return {
id: des.id,
contractName: des.contractName,
address: des.contractAddress,
transactionHash: zeroHash,
blockNumber: 0,
};
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export interface DeployedContract {
id: string;
contractName: string;
address: string;
transactionHash: string;
blockNumber: number;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ import {
describe("buildInitializeMessageFor", () => {
const differentAddress = "0xBA12222222228d8Ba445958a75a0704d566BF2C8";
const libraryAddress = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
const successfulTxHash =
"0x0011223344556677889900112233445566778899001122334455667788990000";
const basicStrategy = { name: "basic", config: {} } as any;

let namedContractDeployment: NamedArtifactContractDeploymentFuture<string>;
Expand Down Expand Up @@ -319,6 +321,7 @@ describe("buildInitializeMessageFor", () => {
],
},
],
transactionHash: successfulTxHash,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,14 +762,15 @@ class MockGetTransactionJsonRpcClient extends StubJsonRpcClient {
}

public async getTransactionReceipt(
_txHash: string
transactionHash: string
): Promise<TransactionReceipt | undefined> {
return {
blockHash: "0xblockhash",
blockNumber: 34,
contractAddress: "0xcontractaddress",
logs: [],
status: TransactionReceiptStatus.SUCCESS,
transactionHash,
};
}
}
Expand Down
Loading

0 comments on commit 736b1f3

Please sign in to comment.