Skip to content

Commit b01b0a8

Browse files
Acceptance tests different references - getCode, getBalance, getTransactionCount (#225)
* test: Add acceptance tests for eth_getCode Signed-off-by: Ivo Yankov <[email protected]> * fix: getCode tests and implementation Signed-off-by: Ivo Yankov <[email protected]> * test: add more test cases for eth_getBalance and eth_getCode Signed-off-by: Ivo Yankov <[email protected]> * chore: fix failing tests Signed-off-by: Ivo Yankov <[email protected]> * fix: properly end test process Signed-off-by: Ivo Yankov <[email protected]> * nit: reduced all Hbar usage Signed-off-by: Ivo Yankov <[email protected]> * nit: tweak test account balance Signed-off-by: Ivo Yankov <[email protected]> * chore: debug failing test Signed-off-by: Ivo Yankov <[email protected]> * chore: attempt to fix failing test Signed-off-by: Ivo Yankov <[email protected]> * chore: resolve conflicts Signed-off-by: Ivo Yankov <[email protected]> * feat: resolve comments Signed-off-by: Daniel Ivanov <[email protected]> Co-authored-by: Daniel Ivanov <[email protected]>
1 parent d61a89b commit b01b0a8

File tree

7 files changed

+108
-12
lines changed

7 files changed

+108
-12
lines changed

packages/relay/src/lib/eth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,12 @@ export class EthImpl implements Eth {
392392
return EthImpl.prepend0x(Buffer.from(bytecode).toString('hex'));
393393
} catch (e: any) {
394394
// handle INVALID_CONTRACT_ID
395-
if (e?.status?._code === Status.InvalidContractId._code) {
396-
this.logger.debug('Unable to find code for contract %s in block "%s", returning 0x0', address, blockNumber);
395+
if (e?.status?._code === Status.InvalidContractId._code || e?.message?.includes(Status.InvalidContractId.toString())) {
396+
this.logger.debug('Unable to find code for contract %s in block "%s", returning 0x0, err code: %s', address, blockNumber, e?.status?._code);
397397
return '0x0';
398398
}
399399

400-
this.logger.error(e, 'Error raised during getCode for address %s', address);
400+
this.logger.error(e, 'Error raised during getCode for address %s, err code: %s', address, e?.status?._code);
401401
throw e;
402402
}
403403
}

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

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ const testLogger = pino({
4848
const logger = testLogger.child({ name: 'rpc-acceptance-test' });
4949

5050
// local resources
51-
const contractJson = require('../parentContract/Parent.json');
51+
import parentContractJson from '../contracts/Parent.json';
52+
import basicContractJson from '../contracts/Basic.json';
5253

5354
dotenv.config({ path: path.resolve(__dirname, '../../../.env') });
5455

@@ -69,7 +70,10 @@ const OPERATOR_ID = process.env.OPERATOR_ID_MAIN || '';
6970
const MIRROR_NODE_URL = process.env.MIRROR_NODE_URL || '';
7071
const LOCAL_RELAY_URL = 'http://localhost:7546';
7172
const RELAY_URL = process.env.E2E_RELAY_HOST || LOCAL_RELAY_URL;
73+
7274
const ONE_TINYBAR = ethers.utils.parseUnits('1', 10);
75+
const ONE_WEIBAR = ethers.utils.parseUnits('1', 18);
76+
7377
const NON_EXISTING_ADDRESS = '0x5555555555555555555555555555555555555555';
7478

7579
describe('RPC Server Acceptance Tests', function() {
@@ -94,8 +98,8 @@ describe('RPC Server Acceptance Tests', function() {
9498

9599
accounts[0] = await servicesNode.createAliasAccount();
96100
accounts[1] = await servicesNode.createAliasAccount();
97-
accounts[2] = await servicesNode.createAliasAccount();
98-
contractId = await accounts[0].client.createParentContract(contractJson);
101+
accounts[2] = await servicesNode.createAliasAccount(20);
102+
contractId = await accounts[0].client.createParentContract(parentContractJson);
99103

100104
const params = new ContractFunctionParameters().addUint256(1);
101105
contractExecuteTimestamp = (await accounts[0].client
@@ -349,8 +353,8 @@ describe('RPC Server Acceptance Tests', function() {
349353
expect(res).to.be.equal('0xa7a3582000');
350354
});
351355

352-
it('should execute "eth_getBalance" for newly created account with 100 HBAR', async function() {
353-
const account = await servicesNode.createAliasAccount(100);
356+
it('should execute "eth_getBalance" for newly created account with 10 HBAR', async function() {
357+
const account = await servicesNode.createAliasAccount(10);
354358
// Wait for account creation to propagate
355359
await mirrorNode.get(`/accounts/${account.accountId}`);
356360

@@ -368,12 +372,12 @@ describe('RPC Server Acceptance Tests', function() {
368372

369373
it('should execute "eth_getBalance" for contract', async function() {
370374
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(contractId.toString()), 'latest']);
371-
expect(res).to.eq('0x56bc75e2d63100000');
375+
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
372376
});
373377

374378
it('should execute "eth_getBalance" for contract with id converted to evm_address', async function() {
375379
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(contractId.toString()), 'latest']);
376-
expect(res).to.eq('0x56bc75e2d63100000');
380+
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
377381
});
378382

379383
describe('Hardcoded RPC Endpoints', () => {
@@ -476,6 +480,46 @@ describe('RPC Server Acceptance Tests', function() {
476480
});
477481
});
478482

483+
describe('eth_getCode', () => {
484+
485+
let basicContract;
486+
487+
before(async () => {
488+
basicContract = await servicesNode.deployContract(basicContractJson);
489+
// Wait for creation to propagate
490+
await mirrorNode.get(`/contracts/${basicContract.contractId}`);
491+
});
492+
493+
it('should execute "eth_getCode" for contract evm_address', async function () {
494+
const evmAddress = basicContract.contractId.toSolidityAddress();
495+
const res = await relay.call('eth_getCode', [evmAddress]);
496+
expect(res).to.eq(basicContractJson.deployedBytecode);
497+
});
498+
499+
it('should execute "eth_getCode" for contract with id converted to evm_address', async function () {
500+
const evmAddress = Utils.idToEvmAddress(basicContract.contractId.toString());
501+
const res = await relay.call('eth_getCode', [evmAddress]);
502+
expect(res).to.eq(basicContractJson.deployedBytecode);
503+
});
504+
505+
it('should return 0x for non-existing contract on eth_getCode', async function () {
506+
const res = await relay.call('eth_getCode', [NON_EXISTING_ADDRESS]);
507+
expect(res).to.eq('0x0');
508+
});
509+
510+
it('should return 0x for account evm_address on eth_getCode', async function () {
511+
const evmAddress = Utils.idToEvmAddress(accounts[2].accountId.toString());
512+
const res = await relay.call('eth_getCode', [evmAddress]);
513+
expect(res).to.eq('0x0');
514+
});
515+
516+
it('should return 0x for account alias on eth_getCode', async function () {
517+
const alias = Utils.idToEvmAddress(accounts[2].accountId.toString());
518+
const res = await relay.call('eth_getCode', [alias]);
519+
expect(res).to.eq('0x0');
520+
});
521+
});
522+
479523
after(function() {
480524
if (USE_LOCAL_NODE === 'true') {
481525
// stop local-node

packages/server/tests/clients/servicesClient.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export default class ServicesClient {
164164
new ContractFunctionParameters()
165165
)
166166
.setGas(75000)
167-
.setInitialBalance(100)
167+
.setInitialBalance(1)
168168
.setBytecodeFileId(fileId || '')
169169
.setAdminKey(this.client.operatorPublicKey || this.DEFAULT_KEY));
170170

@@ -196,7 +196,7 @@ export default class ServicesClient {
196196
return { contractExecuteTimestamp, contractExecutedTransactionId };
197197
};
198198

199-
async createAliasAccount(initialBalance = 1000): Promise<AliasAccount> {
199+
async createAliasAccount(initialBalance = 10): Promise<AliasAccount> {
200200
const privateKey = PrivateKey.generateECDSA();
201201
const publicKey = privateKey.publicKey;
202202
const aliasAccountId = publicKey.toAccountId(0, 0);
@@ -236,6 +236,20 @@ export default class ServicesClient {
236236
);
237237
};
238238

239+
async deployContract(contract, gas = 100_000) {
240+
const fileCreateTx = await (new FileCreateTransaction()
241+
.setContents(contract.bytecode)
242+
.setKeys([this.client.operatorPublicKey])
243+
.execute(this.client));
244+
const fileCreateRx = await fileCreateTx.getReceipt(this.client);
245+
const bytecodeFileId = fileCreateRx.fileId;
246+
const contractInstantiateTx = new ContractCreateTransaction()
247+
.setBytecodeFileId(bytecodeFileId)
248+
.setGas(gas);
249+
const contractInstantiateSubmit = await contractInstantiateTx.execute(this.client);
250+
return contractInstantiateSubmit.getReceipt(this.client);
251+
};
252+
239253
_thisAccountId() {
240254
return this.client.operatorAccountId || AccountId.fromString('0.0.0');
241255
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"_format": "hh-sol-artifact-1",
3+
"contractName": "Basic",
4+
"sourceName": "contracts/Basic.sol",
5+
"abi": [
6+
{
7+
"inputs": [],
8+
"stateMutability": "nonpayable",
9+
"type": "constructor"
10+
},
11+
{
12+
"inputs": [],
13+
"name": "ping",
14+
"outputs": [
15+
{
16+
"internalType": "int256",
17+
"name": "",
18+
"type": "int256"
19+
}
20+
],
21+
"stateMutability": "pure",
22+
"type": "function"
23+
}
24+
],
25+
"bytecode": "0x608060405234801561001057600080fd5b5060b68061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80635c36b18614602d575b600080fd5b60336047565b604051603e9190605d565b60405180910390f35b60006001905090565b6057816076565b82525050565b6000602082019050607060008301846050565b92915050565b600081905091905056fea26469706673582212206ad62fc0dbb8a1f3b6389e460d953f0f7fb0ce2eb71250d4057992f62dfc56d264736f6c63430008040033",
26+
"deployedBytecode": "0x6080604052348015600f57600080fd5b506004361060285760003560e01c80635c36b18614602d575b600080fd5b60336047565b604051603e9190605d565b60405180910390f35b60006001905090565b6057816076565b82525050565b6000602082019050607060008301846050565b92915050565b600081905091905056fea26469706673582212206ad62fc0dbb8a1f3b6389e460d953f0f7fb0ce2eb71250d4057992f62dfc56d264736f6c63430008040033",
27+
"linkReferences": {},
28+
"deployedLinkReferences": {}
29+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//SPDX-License-Identifier: Unlicense
2+
pragma solidity ^0.8.0;
3+
contract Basic {
4+
constructor() {}
5+
6+
function ping() public pure returns (int) {
7+
return 1;
8+
}
9+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)