Skip to content

Commit 75625e1

Browse files
Refactor acceptance tests structure (#211)
* refactor: overall refactor of acceptance structure Signed-off-by: Ivo Yankov <[email protected]> * fix: correctly load env for integration tests Signed-off-by: Ivo Yankov <[email protected]> * chore: test refactoring Signed-off-by: Daniel Ivanov <[email protected]> * chore: test refactoring Signed-off-by: Daniel Ivanov <[email protected]> * chore: enable part of the acceptance tests Signed-off-by: Daniel Ivanov <[email protected]> * chore: remove invalid child logger Signed-off-by: Daniel Ivanov <[email protected]> * chore: test-cicd Signed-off-by: Daniel Ivanov <[email protected]> * feat: partially enable acceptance tests Signed-off-by: Daniel Ivanov <[email protected]> * feat: fix more tests Signed-off-by: Daniel Ivanov <[email protected]> * chore: remove unused methods Signed-off-by: Daniel Ivanov <[email protected]> * chore: enable more tests Signed-off-by: Daniel Ivanov <[email protected]> * chore: enable more tests Signed-off-by: Daniel Ivanov <[email protected]> * feat: fix all tests Signed-off-by: Daniel Ivanov <[email protected]> * feat: resolve comments Signed-off-by: Daniel Ivanov <[email protected]> * feat: resolve last comment Signed-off-by: Daniel Ivanov <[email protected]> Co-authored-by: Daniel Ivanov <[email protected]>
1 parent 94328ad commit 75625e1

File tree

12 files changed

+1091
-1004
lines changed

12 files changed

+1091
-1004
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
]
2121
},
2222
"scripts": {
23-
"acceptancetest": "ts-mocha ./packages/server/tests/acceptance.spec.ts --exit",
23+
"acceptancetest": "ts-mocha packages/server/tests/acceptance/*.spec.ts --exit",
2424
"build": "npx lerna run build",
2525
"build-and-test": "npx lerna run build && npx lerna run test",
2626
"build:docker": "docker build . -t ${npm_package_name}",

packages/server/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
"build": "pnpm clean && pnpm compile",
4242
"clean": "rm -rf ./dist && rm -rf tsconfig.tsbuildinfo",
4343
"compile": "tsc -b tsconfig.json",
44-
"acceptancetest": "ts-mocha ./tests/acceptance.spec.ts",
44+
"acceptancetest": "ts-mocha tests/acceptance/*.spec.ts",
4545
"start": "node dist/index.js",
46-
"test": "nyc ts-mocha --recursive ./tests/server.spec.ts"
46+
"test": "nyc ts-mocha --recursive ./tests/integration/server.spec.ts"
4747
},
4848
"nyc": {
4949
"check-coverage": false,

packages/server/tests/acceptance.spec.ts

Lines changed: 0 additions & 566 deletions
This file was deleted.

packages/server/tests/acceptance/rpc.spec.ts

Lines changed: 504 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*-
2+
*
3+
* Hedera JSON RPC Relay
4+
*
5+
* Copyright (C) 2022 Hedera Hashgraph, LLC
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
import Axios, { AxiosInstance } from 'axios';
22+
import axiosRetry from 'axios-retry';
23+
import { Logger } from 'pino';
24+
25+
export default class MirrorClient {
26+
27+
private readonly logger: Logger;
28+
private readonly client: AxiosInstance;
29+
30+
constructor(mirrorNodeUrl: string, logger: Logger) {
31+
this.logger = logger;
32+
33+
const mirrorNodeClient = Axios.create({
34+
baseURL: `${mirrorNodeUrl}/api/v1`,
35+
responseType: 'json' as const,
36+
headers: {
37+
'Content-Type': 'application/json'
38+
},
39+
method: 'GET',
40+
timeout: 5 * 1000
41+
});
42+
43+
// allow retries given mirror node waits for consensus, record stream serialization, export and import before parsing and exposing
44+
axiosRetry(mirrorNodeClient, {
45+
retries: 5,
46+
retryDelay: (retryCount) => {
47+
this.logger.info(`Retry delay ${retryCount * 1000} s`);
48+
return retryCount * 1000;
49+
},
50+
retryCondition: (error) => {
51+
this.logger.error(error, `Request failed`);
52+
53+
// if retry condition is not specified, by default idempotent requests are retried
54+
return error?.response?.status === 400 || error?.response?.status === 404;
55+
}
56+
});
57+
58+
this.client = mirrorNodeClient;
59+
}
60+
61+
async get(path: string) {
62+
this.logger.debug(`[GET] MirrorNode ${path} endpoint`);
63+
return (await this.client.get(path)).data;
64+
};
65+
66+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*-
2+
*
3+
* Hedera JSON RPC Relay
4+
*
5+
* Copyright (C) 2022 Hedera Hashgraph, LLC
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
import { ethers, providers } from 'ethers';
22+
import { Logger } from 'pino';
23+
import Assertions from '../helpers/assertions';
24+
25+
export default class RelayClient {
26+
27+
private readonly provider: providers.JsonRpcProvider;
28+
private readonly logger: Logger;
29+
30+
constructor(relayUrl: string, logger: Logger) {
31+
this.logger = logger;
32+
this.provider = new ethers.providers.JsonRpcProvider(relayUrl);
33+
}
34+
35+
/**
36+
* Calls the specified methodName with the provided params
37+
* @param methodName
38+
* @param params
39+
*/
40+
async call(methodName: string, params: any[]) {
41+
const result = await this.provider.send(methodName, params);
42+
this.logger.trace(`[POST] to relay '${methodName}' with params [${params}] returned ${JSON.stringify(result)}`);
43+
return result;
44+
};
45+
46+
/**
47+
* Calls the specified methodName with the provided params and asserts that it fails
48+
* @param methodName
49+
* @param params
50+
*/
51+
async callFailing(methodName: string, params: any[]) {
52+
try {
53+
const res = await this.call(methodName, params);
54+
this.logger.trace(`[POST] to relay '${methodName}' with params [${params}] returned ${JSON.stringify(res)}`);
55+
Assertions.expectedError();
56+
} catch (err) {
57+
Assertions.unknownResponse(err);
58+
}
59+
}
60+
61+
/**
62+
* Calls the specified methodName and asserts that it is not supported
63+
* @param methodName
64+
* @param params
65+
*/
66+
async callUnsupported(methodName: string, params: any[]) {
67+
try {
68+
const res = await this.call(methodName, params);
69+
this.logger.trace(`[POST] to relay '${methodName}' with params [${params}] returned ${JSON.stringify(res)}`);
70+
Assertions.expectedError();
71+
} catch (err) {
72+
Assertions.unsupportedResponse(err);
73+
return err;
74+
}
75+
};
76+
77+
/**
78+
* Gets the account balance by executing `eth_getBalance`
79+
* @param address
80+
* @param block
81+
*/
82+
async getBalance(address, block = 'latest') {
83+
this.logger.debug(`[POST] to relay eth_getBalance for address ${address}]`);
84+
return this.provider.getBalance(address, block);
85+
};
86+
87+
/**
88+
* @param evmAddress
89+
*
90+
* Returns: The nonce of the account with the provided `evmAddress`
91+
*/
92+
async getAccountNonce(evmAddress): Promise<number> {
93+
this.logger.debug(`[POST] to relay for eth_getTransactionCount for address ${evmAddress}`);
94+
const nonce = await this.provider.send('eth_getTransactionCount', [evmAddress, 'latest']);
95+
return Number(nonce);
96+
};
97+
98+
/**
99+
* This invokes the relay logic from eth.ts/sendRawTransaction.
100+
*
101+
* Returns: Transaction hash
102+
* @param signedTx
103+
*/
104+
async sendRawTransaction(signedTx): Promise<string> {
105+
this.logger.debug(`[POST] to relay for eth_sendRawTransaction`);
106+
return this.provider.send('eth_sendRawTransaction', [signedTx]);
107+
};
108+
109+
}

0 commit comments

Comments
 (0)