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

Improve contract recompilation #475

Merged
merged 8 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 10 additions & 11 deletions packages/cli/cli_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@
import { checkFullNodeVersion, codegen, getConfigFile, getSdkFullNodeVersion, isNetworkLive, loadConfig } from './src'
import { Project } from './src/project'

function getConfig(options: any): Configuration {

Check warning on line 29 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected any. Specify a different type
const configFile = options.config ? (options.config as string) : getConfigFile()
console.log(`Loading alephium config file: ${configFile}`)
const config = loadConfig(configFile)
if (config.forceRecompile && config.skipRecompileIfDeployedOnMainnet) {
throw new Error(`The forceRecompile and skipRecompileIfDeployedOnMainnet flags cannot be enabled at the same time`)
}

if (config.forceRecompile && (config.skipRecompileContracts ?? []).length > 0) {
throw new Error(`The skipRecompileContracts cannot be specified when forceRecompile is enabled`)
}

const isDebugModeEnabled = config.enableDebugMode || options.debug
if (isDebugModeEnabled) enableDebugMode()
return { ...config, enableDebugMode: isDebugModeEnabled }
Expand Down Expand Up @@ -91,21 +99,11 @@
try {
const config = getConfig(options)
const networkId = checkAndGetNetworkId(options.network)
const nodeUrl = config.networks[networkId].nodeUrl

Check warning on line 102 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (20)

Generic Object Injection Sink
if (!(await isNetworkLive(nodeUrl))) {
throw new Error(`${networkId} is not live`)
}

if (config.forceRecompile && config.skipRecompileIfDeployedOnMainnet) {
throw new Error(
`The forceRecompile and skipRecompileIfDeployedOnMainnet flags cannot be enabled at the same time`
)
}

if (config.forceRecompile && (config.skipRecompileContracts ?? []).length > 0) {
throw new Error(`The skipRecompileContracts cannot be specified when forceRecompile is enabled`)
}

web3.setCurrentNodeProvider(nodeUrl)
const connectedFullNodeVersion = (await web3.getCurrentNodeProvider().infos.getInfosVersion()).version
const sdkFullNodeVersion = getSdkFullNodeVersion()
Expand All @@ -121,7 +119,8 @@
connectedFullNodeVersion,
config.forceRecompile,
config.skipRecompileIfDeployedOnMainnet,
config.skipRecompileContracts ?? []
config.skipRecompileContracts ?? [],
config.networks['mainnet'].nodeUrl
)
console.log('✅ Compilation completed!')
if (options.skipGenerate) {
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
}

getInstance<I extends ContractInstance>(
contract: ContractFactory<I, any>,

Check warning on line 170 in packages/cli/src/deployment.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected any. Specify a different type
group?: number,
taskId?: string
): I | undefined {
Expand Down Expand Up @@ -218,7 +218,7 @@
return this.contracts.size === 0 && this.scripts.size === 0 && this.migrations.size === 0
}

marshal(): any {

Check warning on line 221 in packages/cli/src/deployment.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected any. Specify a different type
return {
deployerAddress: this.deployerAddress,
contracts: Object.fromEntries(this.contracts),
Expand All @@ -227,7 +227,7 @@
}
}

static unmarshal(json: any): DeploymentsPerAddress {

Check warning on line 230 in packages/cli/src/deployment.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected any. Specify a different type
const deployerAddress = json.deployerAddress as string
const contracts = new Map(Object.entries<DeployContractExecutionResult>(json.contracts))
const scripts = new Map(Object.entries<RunScriptResult>(json.scripts))
Expand Down Expand Up @@ -288,7 +288,7 @@
return true
}
// previous !== undefined if retry is false
return previous!.issueTokenAmount !== issueTokenAmount

Check warning on line 291 in packages/cli/src/deployment.ts

View workflow job for this annotation

GitHub Actions / build (20)

Forbidden non-null assertion
}

async function needToRunScript(
Expand Down Expand Up @@ -602,7 +602,10 @@
configuration.sourceDir ?? DEFAULT_CONFIGURATION_VALUES.sourceDir,
artifactDir,
undefined,
configuration.forceRecompile
configuration.forceRecompile,
configuration.skipRecompileIfDeployedOnMainnet,
configuration.skipRecompileContracts ?? [],
configuration.networks['mainnet'].nodeUrl
)
}

Expand Down
32 changes: 22 additions & 10 deletions packages/cli/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { promises as fsPromises } from 'fs'
import { parseError } from './error'

const crypto = new WebCrypto()
const mainnetNodeUrl = 'https://node.mainnet.alephium.org'
const defaultMainnetNodeUrl = 'https://node.mainnet.alephium.org'

class TypedMatcher<T extends SourceKind> {
matcher: RegExp
Expand Down Expand Up @@ -590,8 +590,12 @@ export class Project {
}
}

private static async getDeployedContracts(sourceInfos: SourceInfo[], artifactsRootDir: string): Promise<string[]> {
const nodeProvider = new NodeProvider(mainnetNodeUrl)
private static async getDeployedContracts(
mainnetNodeUrl: string | undefined,
sourceInfos: SourceInfo[],
artifactsRootDir: string
): Promise<string[]> {
const nodeProvider = new NodeProvider(mainnetNodeUrl ?? defaultMainnetNodeUrl)
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems mainetNodeUrl won't be undefined, so this check mainnetNodeUrl ?? defaultMainnetNodeUrl may not work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We support both alephium.config.ts and alephium.config.js configurations. When using alephium.config.ts, the compiler will ensure that the node url is not undefined. But when using alephium.config.js, the node url may be undefined.

Copy link
Member

Choose a reason for hiding this comment

The 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)
Expand All @@ -611,6 +615,7 @@ export class Project {
}

private static async filterChangedContracts(
mainnetNodeUrl: string | undefined,
sourceInfos: SourceInfo[],
artifactsRootDir: string,
changedContracts: string[],
Expand All @@ -622,7 +627,7 @@ export class Project {
const remainSourceInfo = sourceInfos.filter(
(s) => s.type === SourceKind.Contract && !skipRecompileContracts.includes(s.name)
)
deployedContracts = await this.getDeployedContracts(remainSourceInfo, artifactsRootDir)
deployedContracts = await this.getDeployedContracts(mainnetNodeUrl, remainSourceInfo, artifactsRootDir)
} else {
deployedContracts = []
}
Expand Down Expand Up @@ -658,7 +663,8 @@ export class Project {
changedContracts: string[],
forceRecompile: boolean,
skipRecompileIfDeployedOnMainnet: boolean,
skipRecompileContracts: string[]
skipRecompileContracts: string[],
mainnetNodeUrl: string | undefined
): Promise<Project> {
const removeDuplicates = sourceInfos.reduce((acc: SourceInfo[], sourceInfo: SourceInfo) => {
if (acc.find((info) => info.sourceCodeHash === sourceInfo.sourceCodeHash) === undefined) {
Expand Down Expand Up @@ -720,6 +726,7 @@ export class Project {
projectArtifact
)
const filteredChangedContracts = await Project.filterChangedContracts(
mainnetNodeUrl,
sourceInfos,
artifactsRootDir,
changedContracts,
Expand All @@ -741,7 +748,8 @@ export class Project {
changedContracts: string[],
forceRecompile: boolean,
skipRecompileIfDeployedOnMainnet: boolean,
skipRecompileContracts: string[]
skipRecompileContracts: string[],
mainnetNodeUrl: string | undefined
): Promise<Project> {
const projectArtifact = await ProjectArtifact.from(projectRootDir)
if (projectArtifact === undefined) {
Expand Down Expand Up @@ -797,7 +805,8 @@ export class Project {
changedContracts,
forceRecompile,
skipRecompileIfDeployedOnMainnet,
skipRecompileContracts
skipRecompileContracts,
mainnetNodeUrl
)
}
}
Expand Down Expand Up @@ -929,7 +938,8 @@ export class Project {
defaultFullNodeVersion: string | undefined = undefined,
forceRecompile = false,
skipRecompileIfDeployedOnMainnet = false,
skipRecompileContracts: string[] = []
skipRecompileContracts: string[] = [],
mainnetNodeUrl: string | undefined = undefined
): Promise<Project> {
const provider = web3.getCurrentNodeProvider()
const fullNodeVersion = defaultFullNodeVersion ?? (await provider.infos.getInfosVersion()).version
Expand Down Expand Up @@ -959,7 +969,8 @@ export class Project {
changedContracts,
forceRecompile,
skipRecompileIfDeployedOnMainnet,
skipRecompileContracts
skipRecompileContracts,
mainnetNodeUrl
)
}
// we need to reload those contracts that did not regenerate bytecode
Expand All @@ -974,7 +985,8 @@ export class Project {
changedContracts,
forceRecompile,
skipRecompileIfDeployedOnMainnet,
skipRecompileContracts
skipRecompileContracts,
mainnetNodeUrl
)
}
}
Loading