Skip to content

Commit

Permalink
Check full node version
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Feb 27, 2024
1 parent 193a542 commit a443634
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/cli/cli_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { deployAndSaveProgress } from './scripts/deploy'
import { Configuration, DEFAULT_CONFIGURATION_VALUES } from './src/types'
import { createProject } from './scripts/create-project'
import { generateImagesWithOpenAI, uploadImagesAndMetadataToIPFS } from './scripts/pre-designed-nft'
import { codegen, getConfigFile, isNetworkLive, loadConfig } from './src'
import { checkFullNodeVersion, codegen, getConfigFile, getSdkFullNodeVersion, isNetworkLive, loadConfig } from './src'

function getConfig(options: any): Configuration {

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

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected any. Specify a different type

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

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected any. Specify a different type
const configFile = options.config ? (options.config as string) : getConfigFile()
Expand Down Expand Up @@ -95,11 +95,15 @@ program
if (!(await isNetworkLive(nodeUrl))) {
throw new Error(`${networkId} is not live`)
}

web3.setCurrentNodeProvider(nodeUrl)
const fullNodeVersion = (await web3.getCurrentNodeProvider().infos.getInfosVersion()).version
console.log(`Full node version: ${fullNodeVersion}`)
const connectedFullNodeVersion = (await web3.getCurrentNodeProvider().infos.getInfosVersion()).version
const sdkFullNodeVersion = getSdkFullNodeVersion()
checkFullNodeVersion(connectedFullNodeVersion.slice(1), sdkFullNodeVersion)
console.log(`Full node version: ${connectedFullNodeVersion}`)

const cwd = path.resolve(process.cwd())
await Project.build(config.compilerOptions, cwd, config.sourceDir, config.artifactDir, fullNodeVersion)
await Project.build(config.compilerOptions, cwd, config.sourceDir, config.artifactDir, connectedFullNodeVersion)
console.log('✅ Compilation completed!')
if (options.skipGenerate) {
return
Expand Down
45 changes: 45 additions & 0 deletions packages/cli/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2018 - 2022 The Alephium Authors
This file is part of the alephium project.
The library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { checkFullNodeVersion } from './utils'

describe('utils', () => {
it('should check full node version', () => {
expect(() => checkFullNodeVersion('2.9.0', '2.9.0')).not.toThrow()
expect(() => checkFullNodeVersion('2.9.0', '2.9.1')).not.toThrow()
expect(() => checkFullNodeVersion('2.9.1', '2.9.0')).not.toThrow()
expect(() => checkFullNodeVersion('3.0.0', '2.9.0')).not.toThrow()

expect(() => checkFullNodeVersion('1.8.0', '2.9.0')).toThrow(
'Connected full node version is 1.8.0, the minimum required version is 2.9.0'
)
expect(() => checkFullNodeVersion('1.9.0', '2.9.0')).toThrow(
'Connected full node version is 1.9.0, the minimum required version is 2.9.0'
)
expect(() => checkFullNodeVersion('1.9.1', '2.9.1')).toThrow(
'Connected full node version is 1.9.1, the minimum required version is 2.9.0'
)

expect(() => checkFullNodeVersion('2.8.0', '2.9.0')).toThrow(
'Connected full node version is 2.8.0, the minimum required version is 2.9.0'
)
expect(() => checkFullNodeVersion('2.8.1', '2.9.1')).toThrow(
'Connected full node version is 2.8.1, the minimum required version is 2.9.0'
)
})
})
35 changes: 35 additions & 0 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,41 @@ export async function isNetworkLive(url: string): Promise<boolean> {
}
}

export function getSdkFullNodeVersion() {
/* eslint-disable @typescript-eslint/no-var-requires */
const web3Path = require.resolve('@alephium/web3')
const packageJsonPath = path.join(web3Path, '..', '..', '..', 'package.json')
return require(packageJsonPath).config.alephium_version
/* eslint-enable @typescript-eslint/no-var-requires */
}

export function checkFullNodeVersion(connectedFullNodeVersion: string, sdkFullNodeVersion: string) {
const connectedVersions = connectedFullNodeVersion.split('.')
const sdkVersions = sdkFullNodeVersion.split('.')
const connectedMajorVersion = Number(connectedVersions[0])
const sdkMajorVersion = Number(sdkVersions[0])
if (connectedMajorVersion > sdkMajorVersion) {
return
}
const minimumRequiredVersion = `${sdkVersions[0]}.${sdkVersions[1]}.0`
if (connectedMajorVersion < sdkMajorVersion) {
throw new Error(
`Connected full node version is ${connectedFullNodeVersion}, the minimum required version is ${minimumRequiredVersion}`
)
}

const connectedMinorVersion = Number(connectedVersions[1])
const sdkMinorVersion = Number(sdkVersions[1])
if (connectedMinorVersion > sdkMinorVersion) {
return
}
if (connectedMinorVersion < sdkMinorVersion) {
throw new Error(
`Connected full node version is ${connectedFullNodeVersion}, the minimum required version is ${minimumRequiredVersion}`
)
}
}

export async function isDevnetLive(): Promise<boolean> {
return await isNetworkLive('http://127.0.0.1:22973')
}
Expand Down

0 comments on commit a443634

Please sign in to comment.