From 3307f507d8a00b68f36f141468db571679fb503c Mon Sep 17 00:00:00 2001 From: Mirella de Medeiros Date: Thu, 21 Mar 2024 13:30:32 -0300 Subject: [PATCH] CU-86drpne9j - Setup Neo-go on neon-dappkit tests --- .gitignore | 1 + .../data/protocol.unit_testnet.single.yml | 54 +++++++++++ packages/neon-dappkit/data/wallet1_solo.json | 55 ++++++++++++ packages/neon-dappkit/package.json | 5 +- .../src/NeonEventListener.spec.ts | 40 +++++++++ packages/neon-dappkit/test/setup-neo-go.ts | 89 +++++++++++++++++++ 6 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 packages/neon-dappkit/data/protocol.unit_testnet.single.yml create mode 100644 packages/neon-dappkit/data/wallet1_solo.json create mode 100644 packages/neon-dappkit/src/NeonEventListener.spec.ts create mode 100644 packages/neon-dappkit/test/setup-neo-go.ts diff --git a/.gitignore b/.gitignore index 5fbd966..3b34a2d 100644 --- a/.gitignore +++ b/.gitignore @@ -79,3 +79,4 @@ common/autoinstallers/*/.npmrc # Mocha Report mocha-results.json **/reporter/ +packages/neon-dappkit/neogo/ diff --git a/packages/neon-dappkit/data/protocol.unit_testnet.single.yml b/packages/neon-dappkit/data/protocol.unit_testnet.single.yml new file mode 100644 index 0000000..1ebda60 --- /dev/null +++ b/packages/neon-dappkit/data/protocol.unit_testnet.single.yml @@ -0,0 +1,54 @@ +ProtocolConfiguration: + Magic: 42 + MaxTraceableBlocks: 200000 + TimePerBlock: 100ms + MemPoolSize: 100 + StandbyCommittee: + - 02b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc2 + ValidatorsCount: 1 + VerifyTransactions: true + P2PSigExtensions: true + Hardforks: + Aspidochelone: 25 + +ApplicationConfiguration: + SkipBlockVerification: false + # LogPath could be set up in case you need stdout logs to some proper file. + # LogPath: "./log/neogo.log" + DBConfiguration: + Type: "inmemory" #other options: 'inmemory','boltdb' + # DB type options. Uncomment those you need in case you want to switch DB type. + # LevelDBOptions: + # DataDirectoryPath: "./chains/unit_testnet" + # BoltDBOptions: + # FilePath: "./chains/unit_testnet.bolt" + P2P: + Addresses: + - ":0" # in form of "[host]:[port][:announcedPort]" + DialTimeout: 3s + ProtoTickInterval: 2s + PingInterval: 30s + PingTimeout: 90s + MinPeers: 0 + MaxPeers: 10 + AttemptConnPeers: 5 + Relay: true + Consensus: + Enabled: true + UnlockWallet: + Path: "wallet1_solo.json" + Password: "one" + RPC: + MaxGasInvoke: 15 + Enabled: true + Addresses: + - "127.0.0.1:0" # let the system choose port dynamically + EnableCORSWorkaround: false + Prometheus: + Enabled: false #since it's not useful for unit tests. + Addresses: + - ":2112" + Pprof: + Enabled: false #since it's not useful for unit tests. + Addresses: + - ":2113" diff --git a/packages/neon-dappkit/data/wallet1_solo.json b/packages/neon-dappkit/data/wallet1_solo.json new file mode 100644 index 0000000..4a284a2 --- /dev/null +++ b/packages/neon-dappkit/data/wallet1_solo.json @@ -0,0 +1,55 @@ +{ + "version": "1.0", + "accounts": [ + { + "address": "Nhfg3TbpwogLvDGVvAvqyThbsHgoSUKwtn", + "key": "6PYM8VdX3hY4B51UJxmm8D41RQMbpJT8aYHibyQ67gjkUPmvQgu51Y5UQR", + "label": "", + "contract": { + "script": "DCECs2Ir9AF73+MXxYrtX0x1PyBrfbiWBG+n13S7xL9/jcJBVuezJw==", + "parameters": [ + { + "name": "parameter0", + "type": "Signature" + } + ], + "deployed": false + }, + "lock": false, + "isDefault": false + }, + { + "address": "NVTiAjNgagDkTr5HTzDmQP9kPwPHN5BgVq", + "key": "6PYM8VdX3hY4B51UJxmm8D41RQMbpJT8aYHibyQ67gjkUPmvQgu51Y5UQR", + "label": "", + "contract": { + "script": "EwwhAhA6f33QFlWFl/eWDSfFFqQ5T9loueZRVetLAT5AQEBuDCECp7xV/oaE4BGXaNEEujB5W9zIZhnoZK3SYVZyPtGFzWIMIQKzYiv0AXvf4xfFiu1fTHU/IGt9uJYEb6fXdLvEv3+NwgwhA9kMB99j5pDOd5EuEKtRrMlEtmhgI3tgjE+PgwnnHuaZFEGe0Nw6", + "parameters": [ + { + "name": "parameter0", + "type": "Signature" + }, + { + "name": "parameter1", + "type": "Signature" + }, + { + "name": "parameter2", + "type": "Signature" + } + ], + "deployed": false + }, + "lock": false, + "isDefault": false + } + ], + "scrypt": { + "n": 2, + "r": 1, + "p": 1 + }, + "extra": { + "Tokens": null + } +} diff --git a/packages/neon-dappkit/package.json b/packages/neon-dappkit/package.json index bfb6ab8..bebcae5 100644 --- a/packages/neon-dappkit/package.json +++ b/packages/neon-dappkit/package.json @@ -11,7 +11,7 @@ "build": "tsc", "lint": "eslint .", "format": "eslint --fix", - "test": "ts-mocha --reporter json > ../../mocha-results.json src/**/*.spec.ts", + "test": "ts-node test/setup-neo-go.ts && ts-mocha --reporter json > ../../mocha-results.json src/**/*.spec.ts", "test-print": "ts-mocha src/**/*.spec.ts", "coverage": "nyc pnpm test" }, @@ -31,12 +31,15 @@ "@istanbuljs/nyc-config-typescript": "^1.0.2", "@types/elliptic": "6.4.14", "@types/expect": "^24.3.0", + "@types/follow-redirects": "^1.14.4", "@types/mocha": "10.0.0", "@types/node": "^18.14.6", "@typescript-eslint/eslint-plugin": "^6.5.0", "@typescript-eslint/parser": "^6.5.0", + "adm-zip": "^0.5.10", "chai": "~4.3.7", "eslint": "^8.48.0", + "follow-redirects": "^1.14.4", "mocha": "^10.0.0", "nyc": "^15.1.0", "ts-mocha": "^10.0.0", diff --git a/packages/neon-dappkit/src/NeonEventListener.spec.ts b/packages/neon-dappkit/src/NeonEventListener.spec.ts new file mode 100644 index 0000000..84ced27 --- /dev/null +++ b/packages/neon-dappkit/src/NeonEventListener.spec.ts @@ -0,0 +1,40 @@ +import { ChildProcess, spawn } from 'child_process' +import * as path from 'path'; +import assert from 'assert' + +function wait(ms: number) { + return new Promise((resolve) => { + setTimeout(resolve, ms) + }) +} + +function neoGoPath() { + return path.resolve(path.join(__dirname, '..', 'neogo', 'neogo')) +} + +function getDataDir() { + return path.resolve(path.join(__dirname, '..', 'data')) +} + +describe('NeonEventListener', function () { + this.timeout(60000) + let childProcess: ChildProcess; + + beforeEach(async function () { + const neoGo = neoGoPath(); + const dataDir = getDataDir(); + + childProcess = spawn(neoGo, ['node', '--config-file', `${dataDir}/protocol.unit_testnet.single.yml`, '--relative-path', dataDir], {}) + await wait(1200) + + return true + }) + + afterEach('Tear down', async function () { + return childProcess.kill(); + }) + + it('does execute neoGo', async () => { + assert(childProcess !== undefined, 'child process running neo-go is set') + }) +}) diff --git a/packages/neon-dappkit/test/setup-neo-go.ts b/packages/neon-dappkit/test/setup-neo-go.ts new file mode 100644 index 0000000..3c84796 --- /dev/null +++ b/packages/neon-dappkit/test/setup-neo-go.ts @@ -0,0 +1,89 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import { https } from 'follow-redirects'; +import { execSync } from "child_process"; + +async function installNeoGo(): Promise { + const toolsDir = path.resolve(path.join(__dirname, '..', 'neogo')); + + const platform = process.platform; + let osType = platform.toString(); + let fileExtension = ''; + if (platform == "win32") { + osType = "windows"; + fileExtension = '.exe'; + } + + const goCompilerExecutablePath = path.resolve(path.join(toolsDir, `neogo${fileExtension}`)) + if (fs.existsSync(goCompilerExecutablePath)) { + return goCompilerExecutablePath; + } + + const version = '0.105.1'; + const arch = process.arch; + + let archType = 'arm64'; + if (arch == 'x64') { + archType = 'amd64'; + } + + if (osType == "windows" && archType == "arm64") { + throw new Error(`Unsupported architecture: ${osType}-${arch}`); + } + + + if (!fs.existsSync(toolsDir)) { + fs.mkdirSync(toolsDir, { recursive: true }) + } + + if (!fs.existsSync(goCompilerExecutablePath)) { + if (osType == "darwin" && archType == "arm64") { + const neoGoArchivePage = "https://github.com/nspcc-dev/neo-go/archive/refs/tags"; + const downloadUrl = `${neoGoArchivePage}/v${version}.zip`; + const zipPath = path.join(toolsDir, 'neogo.zip'); + + await downloadAndVerify(downloadUrl, zipPath); + + const AdmZip = require('adm-zip'); + const zip = new AdmZip(zipPath); + + zip.extractAllTo(toolsDir, true); + const extractedFolderPath = path.join(toolsDir, "neo-go-" + version); + console.log(extractedFolderPath); + execSync(`make -C ${extractedFolderPath}`); + + } else { + const fileName = `neo-go-${osType}-${archType}${fileExtension}`; + const neoGoReleasePage = "https://github.com/nspcc-dev/neo-go/releases"; + const downloadUrl = `${neoGoReleasePage}/download/v${version}/${fileName}`; + + await downloadAndVerify(downloadUrl, goCompilerExecutablePath); + } + } + + execSync(`${goCompilerExecutablePath} node -h`); + return goCompilerExecutablePath; +} + + +async function downloadAndVerify(downloadUrl: string, downloadPath: string) { + try { + await new Promise((resolve, reject) => { + const file = fs.createWriteStream(downloadPath); + https.get(downloadUrl, (response: { pipe: (stream: fs.WriteStream) => void; }) => { + response.pipe(file); + file.on('finish', () => { + file.close(); + resolve(); + }); + }).on('error', (err: { message: any; }) => { + fs.unlink(downloadPath, () => { }); // Delete the file async on error + reject(err.message); + }); + }); + } catch (error) { + console.error('Error:', error); + } +} + +installNeoGo();