Skip to content

Commit 71304c2

Browse files
Replace scrypt-shim with scrypt.js; add test (#128)
* Replace scrypt-shim with scrypt.js; add test * Add keystore tests * Travis CI: set nodejs version 16 * Update rollup version * [account] test: mock fetch value * [account] upgrade to 0.1.58 * e2e tests: mock results for Travis * src tests: adjust for new fetch-mock * package: fix tslib version * readme: update instructions for actual e2e tests mock is used in travis, but the actual e2e tests can be run by following the specified instructions Co-authored-by: MaxMustermann2 <[email protected]>
1 parent 3c70618 commit 71304c2

30 files changed

+25663
-3734
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: node_js
22
sudo: true
33
dist: trusty
44
node_js:
5-
- 10
5+
- 16
66
branches:
77
except:
88
- /^v[0-9]/

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,9 @@ yarn test:src
9393
```
9494
## e2e tests
9595

96-
1. `.env` file defines configuration, edit if you have custom settings
97-
98-
2. Run harmony node locally, follow the instructions: https://github.com/harmony-one/harmony
99-
100-
3. Wait for 1-2 mins, and run this:
101-
96+
1. Remove the `'cross-fetch': 'jest-fetch-mock'` line from `scripts/jest/jest.e2e.config.js`
97+
1. Run harmony node locally, follow the instructions: https://github.com/harmony-one/harmony
98+
1. Wait for 1-2 mins, and run this:
10299
```bash
103100
yarn build && yarn test:e2e
104101
```

e2e/fixtures/transactions.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,29 @@
110110
"gasPrice": "0x174876e800",
111111
"rawTransaction": "0xf8690985174876e8008252088080949d72989b68777a1f3ffd6f1db079f1928373ee52830f42408028a022efd1decb6e80e88fd33f5e189f48105acc72f7e302931221183f86ca8a7015a02001dee44471a5397a4579f733b3a13a86ba1d6c78ad3312187513d6a424f72b"
112112
}
113+
],
114+
"hashes": [
115+
"0x5aE91eCA3BF7CB97FCd1305bD6e030e24bda9bbfED3b8c741Efe808e8D4F95Ff",
116+
"0xf4CA9DdED0bC36B5AFBCf4fC6De082DFd0F7FCFad1c2aFC1aF52d16d9Dbd733f",
117+
"0x56C2408c5F58ac9aDFE824B7755f3b54CB0848c7A837Ea1CC5a0FdB521D4A1a2",
118+
"0xACFD09AD729E0C9f23EC9eEf27CC68bA37D7EE9D53aABace2dDD42616Eb8351A",
119+
"0xB8baee4EfB120254Afab3092c8Ccc458fF49eBda12ead9950CD39ac8Bb26D4C1",
120+
"0x412D8DBF0BC3dCAf91A0339EEadd1EEB44b44cD0cEBFaea0d35656d31F96Dde9",
121+
"0xbbeDcd6e4dCDef0c666C0C0c7914ba1E5617F8d57ddc40C32cCb31D4BC5CAfBF",
122+
"0x6E008bEbC8F2CE750d208dbF987edEf7476DcFDF8Eb8607f3B3c2E1ce8675ba9",
123+
"0x0Ff0AcBD45cdDf3A8A148eb2F4Fe6dBf74B422eabdD0766ba9284f0Fe8F6dc8a",
124+
"0x7FDCEE6B5EEf5C3F82B2Ffe8a088E0d3d485bEbBcdBCac16ECCb9d1cbfe50c91"
125+
],
126+
"blockHashes": [
127+
"0x9dD4e063d8cc7A6Fe74a0E3AC4feD5F84eB8Bdc4647Ba8DAd651FD1b4a3979cE",
128+
"0xEF5F7D7ca60bE76c1Cc5aADD8DC9e1bC0F7a3BCD0EEDA6e9Cca0EACAd99BE3Bb",
129+
"0x4261B409CCcDA3ab97aABCFe8dECCfd65D1aB9Bb536dD57Fdafd191FC6BbFbbc",
130+
"0x1fb05a17794cAd1ac025469eA1Bf5af4F33f9bdbD3c0e14bCeca323BC5DF0EC3",
131+
"0xEA405dB5dc0E7e2DB2CfdBf272DCeAf69E9CDa2DFD47229b38A3C5b7b9cc41fB",
132+
"0xCC1ADfef1bb2E496A3BbDb181ec5c8EA047AE340FaD6fF4FeF3CCE452c1975B6",
133+
"0x80dffAe15B6619Cc7aD74Bc132C1b29EFc38B8d8CAD1AfbDb9E37f7357E2EBe4",
134+
"0x6BcB9858d8cCE9de2a6ECd6392A5eC712be9b4A79F6Cf84De8aa5ae9C04f60A8",
135+
"0xB6De43A866dFcE1f7AA7DA0E8e9eb7Fe545cB6becF7cf5fdC564B22C4cBb72b9",
136+
"0xc4b4CeAEAEadADfa8CbCcDeFdcBaBeF999dBF42D14C9AFEDd1de2956B571CebD"
113137
]
114138
}

e2e/src/blockchain.e2e.ts

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,82 @@
1-
import { harmony } from './harmony';
1+
import fetch from 'jest-fetch-mock';
2+
import { harmony, checkCalledMethod } from './harmony';
23

34
import demoAccounts from '../fixtures/testAccount.json';
5+
import { RPCMethod } from '@harmony-js/network';
46

57
const bc = harmony.blockchain;
68

79
const testAccount = demoAccounts.Accounts[1];
810

911
describe('e2e test blockchain', () => {
12+
beforeEach(() => {
13+
fetch.resetMocks();
14+
});
1015
// net_*
1116
it('should test net_peerCount', async () => {
17+
fetch.mockResponseOnce(
18+
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x0"}),
19+
);
1220
const peerCount = await bc.net_peerCount();
21+
expect(checkCalledMethod(0, RPCMethod.PeerCount)).toEqual(true);
1322
expect(harmony.utils.isHex(peerCount.result)).toEqual(true);
1423
});
1524

1625
it('should test net_version', async () => {
26+
fetch.mockResponseOnce(
27+
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "5"}),
28+
);
1729
const netVersion = await bc.net_version();
1830
const versionNumber = parseInt(netVersion.result as string, 10);
1931
expect(netVersion.result).toEqual(`${versionNumber}`);
32+
expect(checkCalledMethod(0, RPCMethod.NetVersion)).toEqual(true);
2033
});
2134
it('should test hmy_protocolVersion', async () => {
35+
fetch.mockResponseOnce(
36+
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x10"}),
37+
);
2238
const protocolVersion = await bc.getProtocolVersion();
2339
expect(harmony.utils.isHex(protocolVersion.result)).toEqual(true);
40+
expect(checkCalledMethod(0, RPCMethod.ProtocolVersion)).toEqual(true);
2441
});
2542

2643
// block chain info
2744
it('should test hmy_blockNumber', async () => {
45+
fetch.mockResponseOnce(
46+
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x10"}),
47+
);
2848
const res = await bc.getBlockNumber();
2949
expect(res.responseType).toEqual('raw');
3050
expect(harmony.utils.isHex(res.result)).toEqual(true);
51+
expect(checkCalledMethod(0, RPCMethod.BlockNumber)).toEqual(true);
3152
});
53+
3254
it('should test hmy_getBlockByNumber', async () => {
55+
fetch.mockResponse(
56+
JSON.stringify({
57+
"jsonrpc": "2.0",
58+
"id": 1,
59+
"result": {
60+
"size": "0x1",
61+
"difficulty": 5,
62+
"extraData": "0x",
63+
"gasLimit": "0x80",
64+
"gasUsed": "0x40",
65+
"hash": "0x8a3390ab500Fbca6514eB326d2fcD9B3BFCFbA7DA392593cB4885b8e3399a2D8",
66+
"logsBloom": "0x0",
67+
"miner": "one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3",
68+
"mixHash": "0x3A7c1Ae14AfecFf55Da298F66b75F4FfB771c3EaBDeAa267FF33A77d4d0be220",
69+
"nonce": 1,
70+
"number": "0x1",
71+
"parentHash": "0x7CebC07e456F0bCD09dbc9A6f271074d93E27B40A4C67Dcc402e6513e12B9aF9",
72+
"receiptsRoot": "0x02B82e11eDC07775Dc6fCF706be2cdAF9165750Ea7bC1B3Eb48ea16Bb3072F4D",
73+
"stateRoot": "0xAaDc89C8bA4e3fCfC140cFcc8D3efD3BE7a49ab31534A5a3F0E1DEA09aae0f4a",
74+
"timestamp": "0x62c44c0a",
75+
"transactionsRoot": "0xc4bfa888fDCC8ca70E2b0CcdCEcc2fF545acCC2D655Ba33DaF4aBc31cFDBd9Ac",
76+
"uncles": []
77+
}
78+
}),
79+
);
3380
const res = await bc.getBlockByNumber({ blockNumber: 'latest' });
3481
const size = res.result.size;
3582
expect(res.responseType).toEqual('raw');
@@ -42,21 +89,55 @@ describe('e2e test blockchain', () => {
4289
const res3 = await bc.getBlockByNumber({ returnObject: true });
4390
expect(res3.responseType).toEqual('raw');
4491
expect(checkBlockData(res3.result)).toEqual(true);
92+
for(let i = 0; i < 3; i++) {
93+
expect(checkCalledMethod(i, RPCMethod.GetBlockByNumber)).toEqual(true);
94+
}
4595
});
4696

4797
it('should test hmy_getBlockByHash', async () => {
98+
fetch.mockResponse(
99+
JSON.stringify({
100+
"jsonrpc": "2.0",
101+
"id": 1,
102+
"result": {
103+
"size": "0x1",
104+
"difficulty": 5,
105+
"extraData": "0x",
106+
"gasLimit": "0x80",
107+
"gasUsed": "0x40",
108+
"hash": "0x8a3390ab500Fbca6514eB326d2fcD9B3BFCFbA7DA392593cB4885b8e3399a2D8",
109+
"logsBloom": "0x0",
110+
"miner": "one155jp2y76nazx8uw5sa94fr0m4s5aj8e5xm6fu3",
111+
"mixHash": "0x3A7c1Ae14AfecFf55Da298F66b75F4FfB771c3EaBDeAa267FF33A77d4d0be220",
112+
"nonce": 1,
113+
"number": "0x1",
114+
"parentHash": "0x7CebC07e456F0bCD09dbc9A6f271074d93E27B40A4C67Dcc402e6513e12B9aF9",
115+
"receiptsRoot": "0x02B82e11eDC07775Dc6fCF706be2cdAF9165750Ea7bC1B3Eb48ea16Bb3072F4D",
116+
"stateRoot": "0xAaDc89C8bA4e3fCfC140cFcc8D3efD3BE7a49ab31534A5a3F0E1DEA09aae0f4a",
117+
"timestamp": "0x62c44c0a",
118+
"transactionsRoot": "0xc4bfa888fDCC8ca70E2b0CcdCEcc2fF545acCC2D655Ba33DaF4aBc31cFDBd9Ac",
119+
"uncles": []
120+
}
121+
}),
122+
);
48123
const latestBlock = await bc.getBlockByNumber({ blockNumber: 'latest' });
49124
const res = await bc.getBlockByHash({ blockHash: latestBlock.result.hash });
50125
expect(res.responseType).toEqual('raw');
51126
expect(latestBlock.result.hash).toEqual(res.result.hash);
52127
expect(harmony.utils.isHex(res.result.size)).toEqual(true);
53128
expect(checkBlockData(res.result)).toEqual(true);
129+
expect(checkCalledMethod(0, RPCMethod.GetBlockByNumber)).toEqual(true);
130+
expect(checkCalledMethod(1, RPCMethod.GetBlockByHash)).toEqual(true);
54131
});
55132

56133
// account related
57134
it('should test hmy_getBalance', async () => {
135+
fetch.mockResponseOnce(
136+
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x10"}),
137+
);
58138
const balance = await bc.getBalance({ address: testAccount.Address });
59139
expect(harmony.utils.isHex(balance.result)).toEqual(true);
140+
expect(checkCalledMethod(0, RPCMethod.GetBalance)).toEqual(true);
60141
});
61142
});
62143

@@ -85,4 +166,4 @@ function checkBlockData(data: any) {
85166
},
86167
{ transactions: [harmony.utils.isArray] },
87168
);
88-
}
169+
}

e2e/src/harmony.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
import fetch from 'jest-fetch-mock';
12
// tslint:disable-next-line: no-implicit-dependencies
23
import { Harmony } from '@harmony-js/core';
34
// tslint:disable-next-line: no-implicit-dependencies
45
import { ChainType } from '@harmony-js/utils';
56
// tslint:disable-next-line: no-implicit-dependencies
67
import { Account } from '@harmony-js/account';
78

8-
const CHAIN_ID: number = parseInt(process.env.CHAIN_ID as string, 10);
9-
const CHAIN_TYPE: string = process.env.CHAIN_TYPE as string;
10-
const HTTP_PROVIDER: string = process.env.HTTP_PROVIDER as string;
11-
const GENESIS_PRIV_KEY: string = process.env.GENESIS_PRIV_KEY as string;
9+
const CHAIN_ID: number = 2;
10+
const CHAIN_TYPE: string = 'hmy';
11+
const HTTP_PROVIDER: string = 'http://localhost:9500';
12+
const GENESIS_PRIV_KEY: string = '45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e';
1213

1314
let chainType: ChainType = ChainType.Harmony;
1415

@@ -27,3 +28,12 @@ export const harmony: Harmony = new Harmony(HTTP_PROVIDER, {
2728
export const myAccount: Account = harmony.wallet.addByPrivateKey(
2829
GENESIS_PRIV_KEY,
2930
);
31+
32+
export function checkCalledMethod(i: number, s: string) {
33+
let params: (string | undefined) = fetch.mock.calls[i][1]?.body?.toString();
34+
if (params) {
35+
let method: string = JSON.parse(params).method;
36+
return method === s;
37+
}
38+
return false;
39+
}

e2e/src/transaction.e2e.ts

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
import { harmony } from './harmony';
1+
import fetch from 'jest-fetch-mock';
2+
import { harmony, checkCalledMethod } from './harmony';
23
// tslint:disable-next-line: no-implicit-dependencies
34
import { Transaction, TxStatus } from '@harmony-js/transaction';
45
// tslint:disable-next-line: no-implicit-dependencies
56
import { isHash, numberToHex } from '@harmony-js/utils';
67
import txnJsons from '../fixtures/transactions.json';
78
import demoAccounts from '../fixtures/testAccount.json';
9+
import { RPCMethod } from '@harmony-js/network';
810

911
const receiver = demoAccounts.Accounts[3];
1012
jest.useRealTimers();
1113

1214
describe('test Transaction using SDK', () => {
15+
beforeEach(() => {
16+
fetch.resetMocks();
17+
});
1318
let signed: Transaction;
1419
let sent: Transaction;
1520
let txId: string;
@@ -38,18 +43,56 @@ describe('test Transaction using SDK', () => {
3843
};
3944

4045
const txn = harmony.transactions.newTx(txnObject);
41-
signed = await harmony.wallet.signTransaction(txn);
46+
signed = await harmony.wallet.signTransaction(txn, undefined, undefined, false);
4247

4348
expect(signed.isSigned()).toEqual(true);
4449
});
4550
it('should send transaction', async () => {
51+
fetch.mockResponseOnce(
52+
JSON.stringify({
53+
"jsonrpc": "2.0", "id": 1,
54+
"result": "0x323A2B2C81d8948E5109FC32f9d0e4e6d178d14cC732C8E0a7Af74E81C7653eA"
55+
}),
56+
);
4657
const [sentTxn, id] = await signed.sendTransaction();
4758
expect(sentTxn.isPending()).toEqual(true);
4859
expect(harmony.utils.isHash(id)).toEqual(true);
60+
expect(checkCalledMethod(0, RPCMethod.SendRawTransaction)).toEqual(true);
4961
txId = id;
5062
sent = sentTxn;
5163
});
5264
it('should confirm a transaction', async () => {
65+
fetch.mockResponses(
66+
[
67+
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x1"}),
68+
{ status: 200 },
69+
],
70+
[
71+
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x2"}),
72+
{ status: 200 },
73+
],
74+
[
75+
JSON.stringify({
76+
"jsonrpc": "2.0",
77+
"id": 1,
78+
"result": {
79+
"contractAddress": null,
80+
"blockNumber": harmony.utils.numberToHex(2),
81+
"from": harmony.wallet.accounts[0],
82+
"gasUsed": harmony.utils.numberToHex(5),
83+
"cumulativeGasUsed": harmony.utils.numberToHex(5),
84+
"logs": [],
85+
"logsBloom": harmony.utils.numberToHex(5),
86+
"shardID": 0,
87+
"to": demoAccounts.Accounts[3].Address,
88+
"transactionHash": "0x8c26EFdb6e4cAC6F8BeACE59F52fd95beD4Bfbfa8fF30F4a7cEd511fE5f869d9",
89+
"transactionIndex": harmony.utils.numberToHex(10),
90+
"blockHash": "0xFECCCCBFd5AC71902BcfE65dDB0b88EEbbD15AD6cDAE7A9FAEb773bF827320fd",
91+
}
92+
}),
93+
{status: 200},
94+
]
95+
)
5396
const toConfirm = await sent.confirm(txId, 20, 1000);
5497
expect(toConfirm.receipt !== undefined).toEqual(true);
5598
expect(checkTransactionReceipt(toConfirm.receipt)).toEqual(true);
@@ -58,6 +101,9 @@ describe('test Transaction using SDK', () => {
58101
} else if (toConfirm.isRejected()) {
59102
expect(toConfirm.txStatus).toEqual(TxStatus.REJECTED);
60103
}
104+
expect(checkCalledMethod(0, RPCMethod.BlockNumber)).toEqual(true);
105+
expect(checkCalledMethod(1, RPCMethod.BlockNumber)).toEqual(true);
106+
expect(checkCalledMethod(2, RPCMethod.GetTransactionReceipt)).toEqual(true);
61107
});
62108
it('should test transaction observed events', async () => {
63109
const txnObject = {
@@ -84,10 +130,53 @@ describe('test Transaction using SDK', () => {
84130
.on('error', (error) => {
85131
expect(error).toBeTruthy();
86132
});
87-
const txnSigned = await harmony.wallet.signTransaction(txn);
133+
const txnSigned = await harmony.wallet.signTransaction(txn, undefined, undefined, false);
134+
fetch.mockResponseOnce(
135+
JSON.stringify({
136+
"jsonrpc": "2.0", "id": 1,
137+
"result": "0x323A2B2C81d8948E5109FC32f9d0e4e6d178d14cC732C8E0a7Af74E81C7653eA"
138+
}),
139+
);
88140
const [txnSent, id] = await txnSigned.sendTransaction();
89141
expect(txnSent.txStatus).toEqual(TxStatus.PENDING);
142+
expect(checkCalledMethod(0, RPCMethod.SendRawTransaction)).toEqual(true);
143+
console.log('Here');
144+
fetch.mockResponses(
145+
[
146+
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x1"}),
147+
{ status: 200 },
148+
],
149+
[
150+
JSON.stringify({"jsonrpc": "2.0", "id": 1, "result": "0x2"}),
151+
{ status: 200 },
152+
],
153+
[
154+
JSON.stringify({
155+
"jsonrpc": "2.0",
156+
"id": 1,
157+
"result": {
158+
"contractAddress": null,
159+
"blockNumber": harmony.utils.numberToHex(2),
160+
"from": harmony.wallet.accounts[0],
161+
"gasUsed": harmony.utils.numberToHex(5),
162+
"cumulativeGasUsed": harmony.utils.numberToHex(5),
163+
"logs": [],
164+
"logsBloom": harmony.utils.numberToHex(5),
165+
"shardID": 0,
166+
"to": demoAccounts.Accounts[3].Address,
167+
"transactionHash": "0x8c26EFdb6e4cAC6F8BeACE59F52fd95beD4Bfbfa8fF30F4a7cEd511fE5f869d9",
168+
"transactionIndex": harmony.utils.numberToHex(10),
169+
"blockHash": "0xFECCCCBFd5AC71902BcfE65dDB0b88EEbbD15AD6cDAE7A9FAEb773bF827320fd",
170+
"status": "0x1",
171+
}
172+
}),
173+
{status: 200},
174+
]
175+
);
90176
await txnSigned.confirm(id);
177+
expect(checkCalledMethod(1, RPCMethod.BlockNumber)).toEqual(true);
178+
expect(checkCalledMethod(2, RPCMethod.BlockNumber)).toEqual(true);
179+
expect(checkCalledMethod(3, RPCMethod.GetTransactionReceipt)).toEqual(true);
91180
});
92181
});
93182

0 commit comments

Comments
 (0)