-
Notifications
You must be signed in to change notification settings - Fork 29
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
Improve contract recompilation #475
Changes from 6 commits
4517d45
708c0cf
05ba0a7
d0aa6fb
bb531ff
d51b0bd
53828bf
35cb7d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,14 +30,16 @@ import { | |
CompilerOptions, | ||
Constant, | ||
Enum, | ||
TraceableError | ||
TraceableError, | ||
getContractCodeByCodeHash | ||
} from '@alephium/web3' | ||
import * as path from 'path' | ||
import fs from 'fs' | ||
import { promises as fsPromises } from 'fs' | ||
import { parseError } from './error' | ||
|
||
const crypto = new WebCrypto() | ||
const defaultMainnetNodeUrl = 'https://node.mainnet.alephium.org' | ||
|
||
class TypedMatcher<T extends SourceKind> { | ||
matcher: RegExp | ||
|
@@ -390,18 +392,18 @@ export class Project { | |
contracts: Map<string, Compiled<Contract>>, | ||
scripts: Map<string, Compiled<Script>>, | ||
globalWarnings: string[], | ||
changedSources: string[], | ||
changedContracts: string[], | ||
forceRecompile: boolean, | ||
errorOnWarnings: boolean | ||
): void { | ||
const warnings: string[] = forceRecompile ? globalWarnings : [] | ||
contracts.forEach((contract) => { | ||
if (Project.needToUpdate(forceRecompile, changedSources, contract.sourceInfo.name)) { | ||
if (Project.needToUpdate(forceRecompile, changedContracts, contract.sourceInfo.name)) { | ||
warnings.push(...contract.warnings) | ||
} | ||
}) | ||
scripts.forEach((script) => { | ||
if (Project.needToUpdate(forceRecompile, changedSources, script.sourceInfo.name)) { | ||
if (Project.needToUpdate(forceRecompile, changedContracts, script.sourceInfo.name)) { | ||
warnings.push(...script.warnings) | ||
} | ||
}) | ||
|
@@ -475,8 +477,8 @@ export class Project { | |
return fsPromises.writeFile(filePath, JSON.stringify(object, null, 2)) | ||
} | ||
|
||
private static needToUpdate(forceRecompile: boolean, changedSources: string[], name: string): boolean { | ||
return forceRecompile || changedSources.includes(name) | ||
private static needToUpdate(forceRecompile: boolean, changedContracts: string[], name: string): boolean { | ||
return forceRecompile || changedContracts.includes(name) | ||
} | ||
|
||
private async checkMethodIndex(newArtifact: Compiled<Contract>) { | ||
|
@@ -500,7 +502,7 @@ export class Project { | |
private async saveArtifactsToFile( | ||
projectRootDir: string, | ||
forceRecompile: boolean, | ||
changedSources: string[] | ||
changedContracts: string[] | ||
): Promise<void> { | ||
const artifactsRootDir = this.artifactsRootDir | ||
const saveToFile = async function (compiled: Compiled<Artifact>): Promise<void> { | ||
|
@@ -512,29 +514,29 @@ export class Project { | |
return fsPromises.writeFile(artifactPath, compiled.artifact.toString()) | ||
} | ||
for (const [_, contract] of this.contracts) { | ||
if (Project.needToUpdate(forceRecompile, changedSources, contract.sourceInfo.name)) { | ||
if (Project.needToUpdate(forceRecompile, changedContracts, contract.sourceInfo.name)) { | ||
await saveToFile(contract) | ||
} else { | ||
await this.checkMethodIndex(contract) | ||
} | ||
} | ||
for (const [_, script] of this.scripts) { | ||
if (Project.needToUpdate(forceRecompile, changedSources, script.sourceInfo.name)) { | ||
if (Project.needToUpdate(forceRecompile, changedContracts, script.sourceInfo.name)) { | ||
await saveToFile(script) | ||
} | ||
} | ||
await this.saveStructsToFile() | ||
await this.saveConstantsToFile() | ||
await this.saveProjectArtifact(projectRootDir, forceRecompile, changedSources) | ||
await this.saveProjectArtifact(projectRootDir, forceRecompile, changedContracts) | ||
} | ||
|
||
private async saveProjectArtifact(projectRootDir: string, forceRecompile: boolean, changedSources: string[]) { | ||
private async saveProjectArtifact(projectRootDir: string, forceRecompile: boolean, changedContracts: string[]) { | ||
if (!forceRecompile) { | ||
// we should not update the `codeHashDebug` if the `forceRecompile` is disable | ||
const prevProjectArtifact = await ProjectArtifact.from(projectRootDir) | ||
if (prevProjectArtifact !== undefined) { | ||
for (const [name, info] of this.projectArtifact.infos) { | ||
if (!changedSources.includes(name)) { | ||
if (!changedContracts.includes(name)) { | ||
const prevInfo = prevProjectArtifact.infos.get(name) | ||
info.bytecodeDebugPatch = prevInfo?.bytecodeDebugPatch ?? info.bytecodeDebugPatch | ||
info.codeHashDebug = prevInfo?.codeHashDebug ?? info.codeHashDebug | ||
|
@@ -588,6 +590,67 @@ export class Project { | |
} | ||
} | ||
|
||
private static async getDeployedContracts( | ||
mainnetNodeUrl: string | undefined, | ||
sourceInfos: SourceInfo[], | ||
artifactsRootDir: string | ||
): Promise<string[]> { | ||
const nodeProvider = new NodeProvider(mainnetNodeUrl ?? defaultMainnetNodeUrl) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We support both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay |
||
const result: string[] = [] | ||
for (const sourceInfo of sourceInfos) { | ||
const artifactPath = sourceInfo.getArtifactPath(artifactsRootDir) | ||
if (sourceInfo.type === SourceKind.Contract) { | ||
try { | ||
const content = await fsPromises.readFile(artifactPath) | ||
const artifact = JSON.parse(content.toString()) | ||
const codeHash = artifact['codeHash'] | ||
const contractCode = await getContractCodeByCodeHash(nodeProvider, codeHash) | ||
if (contractCode !== undefined) result.push(artifact.name) | ||
} catch (error) { | ||
console.error(`Failed to check the contract deployment: ${sourceInfo.name}, error: ${error}`) | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
|
||
private static async filterChangedContracts( | ||
mainnetNodeUrl: string | undefined, | ||
sourceInfos: SourceInfo[], | ||
artifactsRootDir: string, | ||
changedContracts: string[], | ||
skipRecompileIfDeployedOnMainnet: boolean, | ||
skipRecompileContracts: string[] | ||
): Promise<string[]> { | ||
let deployedContracts: string[] | ||
if (skipRecompileIfDeployedOnMainnet) { | ||
const remainSourceInfo = sourceInfos.filter( | ||
(s) => s.type === SourceKind.Contract && !skipRecompileContracts.includes(s.name) | ||
) | ||
deployedContracts = await this.getDeployedContracts(mainnetNodeUrl, remainSourceInfo, artifactsRootDir) | ||
} else { | ||
deployedContracts = [] | ||
} | ||
|
||
const filteredChangedContracts: string[] = [] | ||
changedContracts.forEach((c) => { | ||
if (skipRecompileContracts.includes(c)) { | ||
console.warn( | ||
`The contract ${c} is in the skipRecompileContracts list. Even if the contract is updated, the code will not be regenerated. ` + | ||
`To regenerate the bytecode, please remove the contract from the skipRecompileContracts list.` | ||
) | ||
} else if (deployedContracts.includes(c)) { | ||
console.warn( | ||
`The contract ${c} has already been deployed to the mainnet. Even if the contract is updated, the bytecode will not be regenerated. ` + | ||
`To regenerate the bytecode, please enable the forceCompile flag.` | ||
) | ||
} else { | ||
filteredChangedContracts.push(c) | ||
} | ||
}) | ||
return filteredChangedContracts | ||
} | ||
|
||
private static async compile_( | ||
fullNodeVersion: string, | ||
provider: NodeProvider, | ||
|
@@ -597,8 +660,11 @@ export class Project { | |
artifactsRootDir: string, | ||
errorOnWarnings: boolean, | ||
compilerOptions: node.CompilerOptions, | ||
changedSources: string[], | ||
forceRecompile: boolean | ||
changedContracts: string[], | ||
forceRecompile: boolean, | ||
skipRecompileIfDeployedOnMainnet: boolean, | ||
skipRecompileContracts: string[], | ||
mainnetNodeUrl: string | undefined | ||
): Promise<Project> { | ||
const removeDuplicates = sourceInfos.reduce((acc: SourceInfo[], sourceInfo: SourceInfo) => { | ||
if (acc.find((info) => info.sourceCodeHash === sourceInfo.sourceCodeHash) === undefined) { | ||
|
@@ -644,7 +710,7 @@ export class Project { | |
contracts, | ||
scripts, | ||
result.warnings ?? [], | ||
changedSources, | ||
changedContracts, | ||
forceRecompile, | ||
errorOnWarnings | ||
) | ||
|
@@ -659,7 +725,15 @@ export class Project { | |
result.enums ?? [], | ||
projectArtifact | ||
) | ||
await project.saveArtifactsToFile(projectRootDir, forceRecompile, changedSources) | ||
const filteredChangedContracts = await Project.filterChangedContracts( | ||
mainnetNodeUrl, | ||
sourceInfos, | ||
artifactsRootDir, | ||
changedContracts, | ||
skipRecompileIfDeployedOnMainnet, | ||
skipRecompileContracts | ||
) | ||
await project.saveArtifactsToFile(projectRootDir, forceRecompile, filteredChangedContracts) | ||
return project | ||
} | ||
|
||
|
@@ -671,8 +745,11 @@ export class Project { | |
artifactsRootDir: string, | ||
errorOnWarnings: boolean, | ||
compilerOptions: node.CompilerOptions, | ||
changedSources: string[], | ||
forceRecompile: boolean | ||
changedContracts: string[], | ||
forceRecompile: boolean, | ||
skipRecompileIfDeployedOnMainnet: boolean, | ||
skipRecompileContracts: string[], | ||
mainnetNodeUrl: string | undefined | ||
): Promise<Project> { | ||
const projectArtifact = await ProjectArtifact.from(projectRootDir) | ||
if (projectArtifact === undefined) { | ||
|
@@ -725,8 +802,11 @@ export class Project { | |
artifactsRootDir, | ||
errorOnWarnings, | ||
compilerOptions, | ||
changedSources, | ||
forceRecompile | ||
changedContracts, | ||
forceRecompile, | ||
skipRecompileIfDeployedOnMainnet, | ||
skipRecompileContracts, | ||
mainnetNodeUrl | ||
) | ||
} | ||
} | ||
|
@@ -856,19 +936,22 @@ export class Project { | |
contractsRootDir = Project.DEFAULT_CONTRACTS_DIR, | ||
artifactsRootDir = Project.DEFAULT_ARTIFACTS_DIR, | ||
defaultFullNodeVersion: string | undefined = undefined, | ||
forceRecompile = false | ||
forceRecompile = false, | ||
skipRecompileIfDeployedOnMainnet = false, | ||
skipRecompileContracts: string[] = [], | ||
mainnetNodeUrl: string | undefined = undefined | ||
): Promise<Project> { | ||
const provider = web3.getCurrentNodeProvider() | ||
const fullNodeVersion = defaultFullNodeVersion ?? (await provider.infos.getInfosVersion()).version | ||
const sourceFiles = await Project.loadSourceFiles(projectRootDir, contractsRootDir) | ||
const { errorOnWarnings, ...nodeCompilerOptions } = { ...DEFAULT_COMPILER_OPTIONS, ...compilerOptionsPartial } | ||
const projectArtifact = await ProjectArtifact.from(projectRootDir) | ||
const changedSources = projectArtifact?.getChangedSources(sourceFiles) ?? sourceFiles.map((s) => s.name) | ||
const changedContracts = projectArtifact?.getChangedSources(sourceFiles) ?? sourceFiles.map((s) => s.name) | ||
if ( | ||
forceRecompile || | ||
projectArtifact === undefined || | ||
projectArtifact.needToReCompile(nodeCompilerOptions, fullNodeVersion) || | ||
changedSources.length > 0 | ||
changedContracts.length > 0 | ||
) { | ||
if (fs.existsSync(artifactsRootDir)) { | ||
removeOldArtifacts(artifactsRootDir, sourceFiles) | ||
|
@@ -883,8 +966,11 @@ export class Project { | |
artifactsRootDir, | ||
errorOnWarnings, | ||
nodeCompilerOptions, | ||
changedSources, | ||
forceRecompile | ||
changedContracts, | ||
forceRecompile, | ||
skipRecompileIfDeployedOnMainnet, | ||
skipRecompileContracts, | ||
mainnetNodeUrl | ||
) | ||
} | ||
// we need to reload those contracts that did not regenerate bytecode | ||
|
@@ -896,8 +982,11 @@ export class Project { | |
artifactsRootDir, | ||
errorOnWarnings, | ||
nodeCompilerOptions, | ||
changedSources, | ||
forceRecompile | ||
changedContracts, | ||
forceRecompile, | ||
skipRecompileIfDeployedOnMainnet, | ||
skipRecompileContracts, | ||
mainnetNodeUrl | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,12 +53,14 @@ export interface Configuration<Settings = unknown> { | |
deploymentScriptDir?: string | ||
deploymentsDir?: string | ||
compilerOptions?: CompilerOptions | ||
skipRecompile?: boolean | ||
skipRecompileOnDeployment?: boolean | ||
|
||
networks: Record<NetworkId, Network<Settings>> | ||
|
||
enableDebugMode?: boolean | ||
forceRecompile?: boolean | ||
skipRecompileIfDeployedOnMainnet?: boolean | ||
skipRecompileContracts?: string[] | ||
} | ||
|
||
export const DEFAULT_CONFIGURATION_VALUES = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we enable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit hesitant about this because most of the contract compilation occurs during the development phase. Once the contract is deployed to the mainnet, the number of compilations should be much less. If it's enabled by default, unnecessary checks could also be made on the mainnet during the development phase. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Let's not enable it by default. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's check NetworkID to ensure that it's mainnet. It might be other networks by accident.