From 85424403c24c26263bd033060e885bebf4727856 Mon Sep 17 00:00:00 2001 From: neeboo Date: Mon, 15 Jul 2019 17:29:37 +0800 Subject: [PATCH] wip(e2e): add e2e tests --- .env | 5 ++ e2e/fixtures/testAccount.json | 79 +++++++++++++++++ e2e/src/blockchain.e2e.ts | 88 +++++++++++++++++++ e2e/src/harmony.ts | 29 ++++++ e2e/tsconfig.json | 17 ++++ package.json | 2 + packages/harmony-core/src/blockchain.ts | 14 +++ packages/harmony-network/src/rpcMethod/rpc.ts | 5 ++ scripts/jest/jest.e2e.config.js | 10 +++ scripts/jest/jest.setup.js | 6 +- scripts/jest/jest.src.config.js | 2 +- tsconfig.e2e.json | 4 + 12 files changed, 258 insertions(+), 3 deletions(-) create mode 100644 .env create mode 100644 e2e/fixtures/testAccount.json create mode 100644 e2e/src/blockchain.e2e.ts create mode 100644 e2e/src/harmony.ts create mode 100644 e2e/tsconfig.json create mode 100644 scripts/jest/jest.e2e.config.js create mode 100644 tsconfig.e2e.json diff --git a/.env b/.env new file mode 100644 index 0000000..d925ad3 --- /dev/null +++ b/.env @@ -0,0 +1,5 @@ +GENESIS_PRIV_KEY=27978f895b11d9c737e1ab1623fde722c04b4f9ccb4ab776bf15932cc72d7c66 +HTTP_PROVIDER=http://localhost:9500 +CHAIN_TYPE=hmy +CHAIN_ID=0 + diff --git a/e2e/fixtures/testAccount.json b/e2e/fixtures/testAccount.json new file mode 100644 index 0000000..caf2ea2 --- /dev/null +++ b/e2e/fixtures/testAccount.json @@ -0,0 +1,79 @@ +{ + "Accounts": [ + { + "Address": "0x1a3e7a44ee21101d7D64FBf29B0F6F1fc295F723", + "Private": "27978f895b11d9c737e1ab1623fde722c04b4f9ccb4ab776bf15932cc72d7c66", + "Public": "0x1a3e7a44ee21101d7D64FBf29B0F6F1fc295F723" + }, + { + "Address": "0x10A02A0a6e95a676AE23e2db04BEa3D1B8b7ca2E", + "Private": "371cb68abe6a6101ac88603fc847e0c013a834253acee5315884d2c4e387ebca", + "Public": "0x10A02A0a6e95a676AE23e2db04BEa3D1B8b7ca2E" + }, + { + "Address": "0x3e881F6C36A3A14a2D1816b0A5471d1caBB16F33", + "Private": "3f8af52063c6648be37d4b33559f784feb16d8e5ffaccf082b3657ea35b05977", + "Public": "0x3e881F6C36A3A14a2D1816b0A5471d1caBB16F33" + }, + { + "Address": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52", + "Private": "df77927961152e6a080ac299e7af2135fc0fb02eb044d0d7bbb1e8c5ad523809", + "Public": "0x9d72989b68777a1f3FfD6F1DB079f1928373eE52" + }, + { + "Address": "0x67957240b6eB045E17B47dcE98102f09aaC03435", + "Private": "fcff43741ad2dd0b232efb159dc47736bbb16f11a79aaeec39b388d06f91116d", + "Public": "0x67957240b6eB045E17B47dcE98102f09aaC03435" + }, + { + "Address": "0xf70fBDB1AD002baDF19024785b1a4bf6F841F558", + "Private": "916d3d78b7f413452434e89f9c1f1d136995ef02d7dc8038e84cc9cef4a02b96", + "Public": "0xf70fBDB1AD002baDF19024785b1a4bf6F841F558" + }, + { + "Address": "0x3f1A559be93C9456Ca75712535Fd522f5EC22c6B", + "Private": "f5967bd87fd2b9dbf51855a2a75ef0a811c84953b3b300ffe90c430a5c856303", + "Public": "0x3f1A559be93C9456Ca75712535Fd522f5EC22c6B" + }, + { + "Address": "0xedD257B4e0F5e7d632c737f4277e93b64DC268FC", + "Private": "f02f7b3bb5aa03aa97f9e030020dd9ca306b209742fafe018104a3207a70a3c9", + "Public": "0xedD257B4e0F5e7d632c737f4277e93b64DC268FC" + }, + { + "Address": "0x66A74477FC1dd0F4924ed943C1d2F1Dece3Ab138", + "Private": "0436864cc15772448f88dd40554592ff6c91a6c1a389d965ad26ee143db1234d", + "Public": "0x66A74477FC1dd0F4924ed943C1d2F1Dece3Ab138" + }, + { + "Address": "0x04178CdbCe3a9Ff9Ea385777aFc4b78B3E745281", + "Private": "dea956e530073ab23d9cae704f5d068482b1977c3173c9efd697c48a7fd3ce83", + "Public": "0x04178CdbCe3a9Ff9Ea385777aFc4b78B3E745281" + }, + { + "Address": "0x46C61d50874A7A06D29FF89a710AbBD0856265be", + "Private": "af539d4ace07a9f601a8d3a6ca6f914d5a9fabe09cfe7d62ebc2348fc95f03a4", + "Public": "0x46C61d50874A7A06D29FF89a710AbBD0856265be" + }, + { + "Address": "0xfE9BABE6904C28E31971337738FBCBAF8c72873e", + "Private": "7d24797eeba0cdac9bf943f0d82c4b18eb206108d6e1b7f610471594c0c94306", + "Public": "0xfE9BABE6904C28E31971337738FBCBAF8c72873e" + }, + { + "Address": "0x3f78622de8D8f87EAa0E8b28C2851e2450E91250", + "Private": "4fa2fecce1becfaf7e5fba5394caacb318333b04071462b5ca850ee5a406dcfe", + "Public": "0x3f78622de8D8f87EAa0E8b28C2851e2450E91250" + }, + { + "Address": "0xd2Cb501B40D3a9a013A38267a4d2A4Cf6bD2CAa8", + "Private": "3c8642f7188e05acc4467d9e2aa7fd539e82aa90a5497257cf0ecbb98ed3b88f", + "Public": "0xd2Cb501B40D3a9a013A38267a4d2A4Cf6bD2CAa8" + }, + { + "Address": "0x2676e6dd2d7618be14cb4c18a355c81bf7aac647", + "Private": "bf29f6a33b2c24a8b5182ef44cc35ce87534ef827c8dfbc1e6bb536aa52f8563", + "Public": "0x2676e6dd2d7618be14cb4c18a355c81bf7aac647" + } + ] +} diff --git a/e2e/src/blockchain.e2e.ts b/e2e/src/blockchain.e2e.ts new file mode 100644 index 0000000..a4747d4 --- /dev/null +++ b/e2e/src/blockchain.e2e.ts @@ -0,0 +1,88 @@ +import { harmony } from './harmony'; + +import demoAccounts from '../fixtures/testAccount.json'; + +const bc = harmony.blockchain; + +const testAccount = demoAccounts.Accounts[1]; + +describe('e2e test blockchain', () => { + // net_* + it('should test net_peerCount', async () => { + const peerCount = await bc.net_peerCount(); + expect(harmony.utils.isHex(peerCount.result)).toEqual(true); + }); + + it('should test net_version', async () => { + const netVersion = await bc.net_version(); + const versionNumber = parseInt(netVersion.result as string, 10); + expect(netVersion.result).toEqual(`${versionNumber}`); + }); + it('should test hmy_protocalVersion', async () => { + const protocalVersion = await bc.getProtocalVersion(); + expect(harmony.utils.isHex(protocalVersion.result)).toEqual(true); + }); + + // block chain info + it('should test hmy_blockNumber', async () => { + const res = await bc.getBlockNumber(); + expect(res.responseType).toEqual('raw'); + expect(harmony.utils.isHex(res.result)).toEqual(true); + }); + it('should test hmy_getBlockByNumber', async () => { + const res = await bc.getBlockByNumber({ blockNumber: 'latest' }); + const size = res.result.size; + expect(res.responseType).toEqual('raw'); + expect(harmony.utils.isHex(size)).toEqual(true); + expect(checkBlockData(res.result)).toEqual(true); + const res2 = await bc.getBlockByNumber({ blockNumber: '0x1' }); + expect(res2.responseType).toEqual('raw'); + expect(harmony.utils.isHex(res2.result.size)).toEqual(true); + expect(checkBlockData(res2.result)).toEqual(true); + const res3 = await bc.getBlockByNumber({ returnObject: true }); + expect(res3.responseType).toEqual('raw'); + expect(checkBlockData(res3.result)).toEqual(true); + expect(typeof res3.result.timestamp).toEqual('number'); + }); + it('should test hmy_getBlockByHash', async () => { + const latestBlock = await bc.getBlockByNumber({ blockNumber: 'latest' }); + const res = await bc.getBlockByHash({ blockHash: latestBlock.result.hash }); + expect(res.responseType).toEqual('raw'); + expect(latestBlock.result.hash).toEqual(res.result.hash); + expect(harmony.utils.isHex(res.result.size)).toEqual(true); + expect(checkBlockData(res.result)).toEqual(true); + }); + + // account related + it('should test hmy_getBalance', async () => { + const balance = await bc.getBalance({ address: testAccount.Address }); + expect(harmony.utils.isHex(balance.result)).toEqual(true); + }); +}); + +function checkBlockData(data: any) { + return harmony.utils.validateArgs( + data, + { + difficulty: [harmony.utils.isNumber], + // tslint:disable-next-line: no-shadowed-variable + extraData: [(data: any) => data === '0x' || harmony.utils.isHex(data)], + gasLimit: [harmony.utils.isHex], + gasUsed: [harmony.utils.isHex], + hash: [harmony.utils.isHash], + logsBloom: [harmony.utils.isHex], + miner: [harmony.utils.isAddress], + mixHash: [harmony.utils.isHash], + nonce: [harmony.utils.isNumber], + number: [harmony.utils.isHex], + parentHash: [harmony.utils.isHash], + receiptsRoot: [harmony.utils.isHash], + size: [harmony.utils.isHex], + stateRoot: [harmony.utils.isHash], + timestamp: [harmony.utils.isNumber], + transactionsRoot: [harmony.utils.isHash], + uncles: [harmony.utils.isArray], + }, + { transactions: [harmony.utils.isArray] }, + ); +} diff --git a/e2e/src/harmony.ts b/e2e/src/harmony.ts new file mode 100644 index 0000000..a379af7 --- /dev/null +++ b/e2e/src/harmony.ts @@ -0,0 +1,29 @@ +// tslint:disable-next-line: no-implicit-dependencies +import { Harmony } from '@harmony-js/core'; +// tslint:disable-next-line: no-implicit-dependencies +import { ChainType } from '@harmony-js/utils'; +// tslint:disable-next-line: no-implicit-dependencies +import { Account } from '@harmony-js/account'; + +const CHAIN_ID: number = parseInt(process.env.CHAIN_ID as string, 10); +const CHAIN_TYPE: string = process.env.CHAIN_TYPE as string; +const HTTP_PROVIDER: string = process.env.HTTP_PROVIDER as string; +const GENESIS_PRIV_KEY: string = process.env.GENESIS_PRIV_KEY as string; + +let chainType: ChainType = ChainType.Harmony; + +if (CHAIN_TYPE === 'hmy') { + chainType = ChainType.Harmony; +} else if (CHAIN_TYPE === 'eth') { + chainType = ChainType.Ethereum; +} + +export const harmony: Harmony = new Harmony(HTTP_PROVIDER, { + chainId: CHAIN_ID, + chainType, + chainUrl: HTTP_PROVIDER, +}); + +export const myAccount: Account = harmony.wallet.addByPrivateKey( + GENESIS_PRIV_KEY, +); diff --git a/e2e/tsconfig.json b/e2e/tsconfig.json new file mode 100644 index 0000000..92e3120 --- /dev/null +++ b/e2e/tsconfig.json @@ -0,0 +1,17 @@ +{ + "extends": "../tsconfig.base.json", + "compilerOptions": { + "outDir": "dist", + "rootDir": "src", + "declarationDir": "dist" + }, + "include": ["src", "../typings/**/*.d.ts"], + "references": [ + { "path": "../packages/harmony-account" }, + { "path": "../packages/harmony-crypto" }, + { "path": "../packages/harmony-utils" }, + { "path": "../packages/harmony-network" }, + { "path": "../packages/harmony-transaction" }, + { "path": "../packages/harmony-contract" } + ] +} diff --git a/package.json b/package.json index 23f51b5..4117cb9 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "build": "yarn build:proto && yarn build:ts", "build:ts": "tsc -b tsconfig.json", "build:test":"tsc -b tsconfig.test.json", + "build:e2e":"tsc -b tsconfig.e2e.json", "bundle": "ts-node -P scripts/tsconfig.json scripts/bundle.ts umd,esm", "clean": "lerna clean --yes && lerna run clean && rimraf includes", "schema": "ts-node -P scripts/tsconfig.json scripts/typings/schema.ts core", @@ -21,6 +22,7 @@ "test:src": "cross-env NODE_ENV=test jest --config ./scripts/jest/jest.src.config.js --silent --runInBand --no-cache", "test:build": "cross-env TEST_ENV=unit jest -c jest.build.config.js", "test:integration": "cross-env TEST_ENV=integration jest -c jest.iconfig.js --runInBand --verbose --collectCoverage=false", + "test:e2e": "cross-env TEST_ENV=e2e jest -c ./scripts/jest/jest.e2e.config.js --runInBand --verbose --collectCoverage=false", "release": "yarn bootstrap && yarn bundle && lerna publish --exact", "format": "prettier --write '**/*.{ts,tsx,js}' --config .prettierrc", "formatSol": "prettier --list-different **/*.sol", diff --git a/packages/harmony-core/src/blockchain.ts b/packages/harmony-core/src/blockchain.ts index 082012e..b249382 100644 --- a/packages/harmony-core/src/blockchain.ts +++ b/packages/harmony-core/src/blockchain.ts @@ -234,6 +234,20 @@ class Blockchain extends HarmonyCore { return this.getRpcResult(result); } + async net_version() { + const result = await this.messenger.send(RPCMethod.NetVersion, [], 'net'); + + return this.getRpcResult(result); + } + + async getProtocalVersion() { + const result = await this.messenger.send( + RPCMethod.ProtocalVersion, + [], + this.chainPrefix, + ); + return this.getRpcResult(result); + } @assertObject({ address: ['isValidAddress', AssertType.required], diff --git a/packages/harmony-network/src/rpcMethod/rpc.ts b/packages/harmony-network/src/rpcMethod/rpc.ts index d74d084..8686329 100644 --- a/packages/harmony-network/src/rpcMethod/rpc.ts +++ b/packages/harmony-network/src/rpcMethod/rpc.ts @@ -21,6 +21,7 @@ export const enum RPCMethod { Syncing = 'hmy_syncing', // 10. net_peerCount PeerCount = 'net_peerCount', + // 11. hmy_getBalance GetBalance = 'hmy_getBalance', // 12. hmy_getStorageAt @@ -59,6 +60,10 @@ export const enum RPCMethod { BlockNumber = 'hmy_blockNumber', // 29. hmy_unsubscribe UnSubscribe = 'hmy_unsubscribe', + // 30. net_version + NetVersion = 'net_version', + // 31. hmy_protocolVersion + ProtocalVersion = 'hmy_protocolVersion', } export const enum RPCErrorCode { diff --git a/scripts/jest/jest.e2e.config.js b/scripts/jest/jest.e2e.config.js new file mode 100644 index 0000000..9bd9847 --- /dev/null +++ b/scripts/jest/jest.e2e.config.js @@ -0,0 +1,10 @@ +const baseConfig = require('./jest.src.config'); +module.exports = { + ...baseConfig, + moduleNameMapper: { + '^@harmony-js/(.*)$': '/packages/harmony-$1/src/index.ts', + }, + setupTestFrameworkScriptFile: + '/scripts/jest/jest.framework-setup.js', + testMatch: ['/e2e/src/?(*.)+(spec|test|e2e).ts'], +}; diff --git a/scripts/jest/jest.setup.js b/scripts/jest/jest.setup.js index b123445..910c9d2 100644 --- a/scripts/jest/jest.setup.js +++ b/scripts/jest/jest.setup.js @@ -1,3 +1,5 @@ -global.fetch = require('jest-fetch-mock') +global.fetch = require('jest-fetch-mock'); -window.fetch = global.fetch +window.fetch = global.fetch; + +require('dotenv').config('../../.env'); diff --git a/scripts/jest/jest.src.config.js b/scripts/jest/jest.src.config.js index d6b3462..cb8e3b4 100644 --- a/scripts/jest/jest.src.config.js +++ b/scripts/jest/jest.src.config.js @@ -42,7 +42,7 @@ const config = { }, }, rootDir: process.cwd(), - roots: ['/packages', '/scripts'], + roots: ['/packages', '/scripts', '/e2e'], collectCoverageFrom: [ // 'packages/!(harmony-core)/src/**/*.ts', 'packages/harmony-core/src/**/*.ts', diff --git a/tsconfig.e2e.json b/tsconfig.e2e.json new file mode 100644 index 0000000..705ab16 --- /dev/null +++ b/tsconfig.e2e.json @@ -0,0 +1,4 @@ +{ + "files": [], + "references": [{ "path": "e2e" }] +}