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

CU-86drpne9j - Setup Neo-go on neon-dappkit tests #32

Merged
merged 4 commits into from
Mar 25, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ common/autoinstallers/*/.npmrc
# Mocha Report
mocha-results.json
**/reporter/
packages/neon-dappkit/neogo/
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/neon-dappkit",
"comment": "",
"type": "none"
}
],
"packageName": "@cityofzion/neon-dappkit"
}
54 changes: 54 additions & 0 deletions packages/neon-dappkit/data/protocol.unit_testnet.single.yml
Original file line number Diff line number Diff line change
@@ -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"
55 changes: 55 additions & 0 deletions packages/neon-dappkit/data/wallet1_solo.json
Original file line number Diff line number Diff line change
@@ -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
}
}
6 changes: 5 additions & 1 deletion packages/neon-dappkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -31,15 +31,19 @@
"@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",
"ts-node": "^10.9.2",
"typescript": "^4.3.2"
},
"repository": {
Expand Down
40 changes: 40 additions & 0 deletions packages/neon-dappkit/src/NeonEventListener.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
90 changes: 90 additions & 0 deletions packages/neon-dappkit/test/setup-neo-go.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import * as fs from 'fs';
import * as path from 'path';
import { https } from 'follow-redirects';
import { execSync } from "child_process";

async function installNeoGo(): Promise<string | undefined> {
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);
}
}

fs.chmodSync(goCompilerExecutablePath, '755')
execSync(`${goCompilerExecutablePath} node -h`);
return goCompilerExecutablePath;
}


async function downloadAndVerify(downloadUrl: string, downloadPath: string) {
try {
await new Promise<void>((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();
Loading