Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify source code input for verification #712

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 1 addition & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
},
"dependencies": {
"@ethersproject/address": "5.6.1",
"@nomicfoundation/solidity-analyzer": "^0.1.1",
"cbor": "^9.0.0",
"debug": "^4.3.2",
"ethers": "^6.7.0",
Expand Down
58 changes: 50 additions & 8 deletions packages/core/src/verify.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { analyze } from "@nomicfoundation/solidity-analyzer";
import path from "path";

import { IgnitionError } from "./errors";
import { builtinChains } from "./internal/chain-config";
import { FileDeploymentLoader } from "./internal/deployment-loader/file-deployment-loader";
Expand All @@ -13,7 +16,7 @@ import {
} from "./internal/execution/types/execution-state";
import { assertIgnitionInvariant } from "./internal/utils/assertions";
import { findExecutionStatesByType } from "./internal/views/find-execution-states-by-type";
import { Artifact, BuildInfo } from "./types/artifact";
import { Artifact, BuildInfo, CompilerInput } from "./types/artifact";
import {
ChainConfig,
SourceToLibraryToAddress,
Expand All @@ -30,7 +33,8 @@ import {
*/
export async function* getVerificationInformation(
deploymentDir: string,
customChains: ChainConfig[] = []
customChains: ChainConfig[] = [],
includeUnrelatedContracts = false
): AsyncGenerator<VerifyResult> {
const deploymentLoader = new FileDeploymentLoader(deploymentDir);

Expand Down Expand Up @@ -58,7 +62,8 @@ export async function* getVerificationInformation(
for (const exState of deploymentExStates) {
const verifyInfo = await convertExStateToVerifyInfo(
exState,
deploymentLoader
deploymentLoader,
includeUnrelatedContracts
);

const verifyResult: VerifyResult = [chainConfig, verifyInfo];
Expand Down Expand Up @@ -87,9 +92,31 @@ function resolveChainConfig(
return chainConfig;
}

function getImportSourceNames(
sourceName: string,
buildInfo: BuildInfo
): string[] {
const contractSource = buildInfo.input.sources[sourceName].content;
const { imports } = analyze(contractSource);

const importSources = imports.map((i) => {
if (/^\.\.?[\/|\\]/.test(i)) {
return path.join(path.dirname(sourceName), i).replaceAll("\\", "/");
}

return i;
});

return [
...importSources,
...importSources.flatMap((i) => getImportSourceNames(i, buildInfo)),
];
}

async function convertExStateToVerifyInfo(
exState: DeploymentExecutionState,
deploymentLoader: FileDeploymentLoader
deploymentLoader: FileDeploymentLoader,
includeUnrelatedContracts: boolean = false
) {
const [buildInfo, artifact] = await Promise.all([
deploymentLoader.readBuildInfo(exState.artifactId),
Expand All @@ -104,12 +131,27 @@ async function convertExStateToVerifyInfo(
`Deployment execution state ${exState.id} should have a successful result to retrieve address`
);

const sourceCode = prepareInputBasedOn(buildInfo, artifact, libraries);

if (!includeUnrelatedContracts) {
const sourceNames = [
artifact.sourceName,
...getImportSourceNames(artifact.sourceName, buildInfo),
];

for (const source of Object.keys(sourceCode.sources)) {
if (!sourceNames.includes(source)) {
delete sourceCode.sources[source];
}
}
}

const verifyInfo = {
address: exState.result.address,
compilerVersion: buildInfo.solcLongVersion.startsWith("v")
? buildInfo.solcLongVersion
: `v${buildInfo.solcLongVersion}`,
sourceCode: prepareInputBasedOn(buildInfo, artifact, libraries),
sourceCode: JSON.stringify(sourceCode),
name: `${artifact.sourceName}:${contractName}`,
args: encodeDeploymentArguments(artifact, constructorArgs),
};
Expand All @@ -121,20 +163,20 @@ function prepareInputBasedOn(
buildInfo: BuildInfo,
artifact: Artifact,
libraries: Record<string, string>
): string {
): CompilerInput {
const sourceToLibraryAddresses = resolveLibraryInfoForArtifact(
artifact,
libraries
);

if (sourceToLibraryAddresses === null) {
return JSON.stringify(buildInfo.input);
return buildInfo.input;
}

const { input } = buildInfo;
input.settings.libraries = sourceToLibraryAddresses;

return JSON.stringify(input);
zoeyTM marked this conversation as resolved.
Show resolved Hide resolved
return input;
}

function resolveLibraryInfoForArtifact(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../build-info/5a5467b3a2cddf6ce0f79584095e02d2.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "TestA",
"sourceName": "contracts/TestA.sol",
"abi": [
{
"inputs": [
{
"internalType": "uint256",
"name": "_a",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_b",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "sum",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
],
"bytecode": "0x608060405234801561001057600080fd5b5060405161025c38038061025c833981810160405281019061003291906100f6565b8173__$9fa3a4b36eea03935ac7879fed7cda438e$__63cad0899b9091836040518363ffffffff1660e01b815260040161006d929190610145565b602060405180830381865af415801561008a573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100ae919061016e565b600081905550505061019b565b600080fd5b6000819050919050565b6100d3816100c0565b81146100de57600080fd5b50565b6000815190506100f0816100ca565b92915050565b6000806040838503121561010d5761010c6100bb565b5b600061011b858286016100e1565b925050602061012c858286016100e1565b9150509250929050565b61013f816100c0565b82525050565b600060408201905061015a6000830185610136565b6101676020830184610136565b9392505050565b600060208284031215610184576101836100bb565b5b6000610192848285016100e1565b91505092915050565b60b3806101a96000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063853255cc14602d575b600080fd5b60336047565b604051603e91906064565b60405180910390f35b60005481565b6000819050919050565b605e81604d565b82525050565b6000602082019050607760008301846057565b9291505056fea2646970667358221220ed8950fa3d7184d2692eb6136b1815845d0f7c330e78b5948e12f2e35fd9b24564736f6c63430008130033",
"deployedBytecode": "0x6080604052348015600f57600080fd5b506004361060285760003560e01c8063853255cc14602d575b600080fd5b60336047565b604051603e91906064565b60405180910390f35b60005481565b6000819050919050565b605e81604d565b82525050565b6000602082019050607760008301846057565b9291505056fea2646970667358221220ed8950fa3d7184d2692eb6136b1815845d0f7c330e78b5948e12f2e35fd9b24564736f6c63430008130033",
"linkReferences": {
"contracts/TestB.sol": {
"TestB": [
{
"length": 20,
"start": 53
}
]
}
},
"deployedLinkReferences": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../build-info/5a5467b3a2cddf6ce0f79584095e02d2.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "TestB",
"sourceName": "contracts/TestB.sol",
"abi": [
{
"inputs": [
{
"internalType": "uint256",
"name": "a",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "b",
"type": "uint256"
}
],
"name": "sum",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "pure",
"type": "function"
}
],
"bytecode": "0x6102ad610053600b82828239805160001a607314610046577f4e487b7100000000000000000000000000000000000000000000000000000000600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600436106100355760003560e01c8063cad0899b1461003a575b600080fd5b610054600480360381019061004f91906101a2565b61006a565b60405161006191906101f1565b60405180910390f35b600073__$a77b74ebd34224ac3c3a582e4bc24622f4$__63cad0899b8473__$3fa06892d414312f70326060dfc06e0087$__63cad0899b87876040518363ffffffff1660e01b81526004016100c092919061020c565b602060405180830381865af41580156100dd573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610101919061024a565b6040518363ffffffff1660e01b815260040161011e92919061020c565b602060405180830381865af415801561013b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061015f919061024a565b905092915050565b600080fd5b6000819050919050565b61017f8161016c565b811461018a57600080fd5b50565b60008135905061019c81610176565b92915050565b600080604083850312156101b9576101b8610167565b5b60006101c78582860161018d565b92505060206101d88582860161018d565b9150509250929050565b6101eb8161016c565b82525050565b600060208201905061020660008301846101e2565b92915050565b600060408201905061022160008301856101e2565b61022e60208301846101e2565b9392505050565b60008151905061024481610176565b92915050565b6000602082840312156102605761025f610167565b5b600061026e84828501610235565b9150509291505056fea264697066735822122056e067cbff0f6f4b44115fa7c682e899221992ed439b30cd21e26a9ad4faac2664736f6c63430008130033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600436106100355760003560e01c8063cad0899b1461003a575b600080fd5b610054600480360381019061004f91906101a2565b61006a565b60405161006191906101f1565b60405180910390f35b600073__$a77b74ebd34224ac3c3a582e4bc24622f4$__63cad0899b8473__$3fa06892d414312f70326060dfc06e0087$__63cad0899b87876040518363ffffffff1660e01b81526004016100c092919061020c565b602060405180830381865af41580156100dd573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610101919061024a565b6040518363ffffffff1660e01b815260040161011e92919061020c565b602060405180830381865af415801561013b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061015f919061024a565b905092915050565b600080fd5b6000819050919050565b61017f8161016c565b811461018a57600080fd5b50565b60008135905061019c81610176565b92915050565b600080604083850312156101b9576101b8610167565b5b60006101c78582860161018d565b92505060206101d88582860161018d565b9150509250929050565b6101eb8161016c565b82525050565b600060208201905061020660008301846101e2565b92915050565b600060408201905061022160008301856101e2565b61022e60208301846101e2565b9392505050565b60008151905061024481610176565b92915050565b6000602082840312156102605761025f610167565b5b600061026e84828501610235565b9150509291505056fea264697066735822122056e067cbff0f6f4b44115fa7c682e899221992ed439b30cd21e26a9ad4faac2664736f6c63430008130033",
"linkReferences": {
"contracts/TestC.sol": {
"TestC": [
{
"length": 20,
"start": 193
}
]
},
"contracts/TestD.sol": {
"TestD": [
{
"length": 20,
"start": 220
}
]
}
},
"deployedLinkReferences": {
"contracts/TestC.sol": {
"TestC": [
{
"length": 20,
"start": 110
}
]
},
"contracts/TestD.sol": {
"TestD": [
{
"length": 20,
"start": 137
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../build-info/5a5467b3a2cddf6ce0f79584095e02d2.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "TestC",
"sourceName": "contracts/TestC.sol",
"abi": [
{
"inputs": [
{
"internalType": "uint256",
"name": "a",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "b",
"type": "uint256"
}
],
"name": "sum",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "pure",
"type": "function"
}
],
"bytecode": "0x610234610053600b82828239805160001a607314610046577f4e487b7100000000000000000000000000000000000000000000000000000000600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600436106100355760003560e01c8063cad0899b1461003a575b600080fd5b610054600480360381019061004f9190610129565b61006a565b6040516100619190610178565b60405180910390f35b600073__$3fa06892d414312f70326060dfc06e0087$__63cad0899b84846040518363ffffffff1660e01b81526004016100a5929190610193565b602060405180830381865af41580156100c2573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100e691906101d1565b905092915050565b600080fd5b6000819050919050565b610106816100f3565b811461011157600080fd5b50565b600081359050610123816100fd565b92915050565b600080604083850312156101405761013f6100ee565b5b600061014e85828601610114565b925050602061015f85828601610114565b9150509250929050565b610172816100f3565b82525050565b600060208201905061018d6000830184610169565b92915050565b60006040820190506101a86000830185610169565b6101b56020830184610169565b9392505050565b6000815190506101cb816100fd565b92915050565b6000602082840312156101e7576101e66100ee565b5b60006101f5848285016101bc565b9150509291505056fea264697066735822122024415ac821281eba4a04ac4443e09eab95a2381de7e62a3a62fe726c54e8a3d764736f6c63430008130033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600436106100355760003560e01c8063cad0899b1461003a575b600080fd5b610054600480360381019061004f9190610129565b61006a565b6040516100619190610178565b60405180910390f35b600073__$3fa06892d414312f70326060dfc06e0087$__63cad0899b84846040518363ffffffff1660e01b81526004016100a5929190610193565b602060405180830381865af41580156100c2573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100e691906101d1565b905092915050565b600080fd5b6000819050919050565b610106816100f3565b811461011157600080fd5b50565b600081359050610123816100fd565b92915050565b600080604083850312156101405761013f6100ee565b5b600061014e85828601610114565b925050602061015f85828601610114565b9150509250929050565b610172816100f3565b82525050565b600060208201905061018d6000830184610169565b92915050565b60006040820190506101a86000830185610169565b6101b56020830184610169565b9392505050565b6000815190506101cb816100fd565b92915050565b6000602082840312156101e7576101e66100ee565b5b60006101f5848285016101bc565b9150509291505056fea264697066735822122024415ac821281eba4a04ac4443e09eab95a2381de7e62a3a62fe726c54e8a3d764736f6c63430008130033",
"linkReferences": {
"contracts/TestD.sol": {
"TestD": [
{
"length": 20,
"start": 193
}
]
}
},
"deployedLinkReferences": {
"contracts/TestD.sol": {
"TestD": [
{
"length": 20,
"start": 110
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../build-info/5a5467b3a2cddf6ce0f79584095e02d2.json"
}
Loading
Loading