Skip to content

Commit 32ee497

Browse files
authored
Fix acceptance test inconsistency (#802)
* fix: inconsistent block assertion logic Signed-off-by: Ivo Yankov <[email protected]> * fix: refactor failing test Signed-off-by: Ivo Yankov <[email protected]> * fix: remove inter-test dependencies for eth_getBalance Signed-off-by: Ivo Yankov <[email protected]> Signed-off-by: Ivo Yankov <[email protected]>
1 parent 832cda0 commit 32ee497

File tree

2 files changed

+109
-93
lines changed

2 files changed

+109
-93
lines changed

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

Lines changed: 107 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -864,119 +864,134 @@ describe('@api RPC Server Acceptance Tests', function () {
864864
});
865865
});
866866

867-
it('@release should execute "eth_estimateGas"', async function () {
868-
const res = await relay.call('eth_estimateGas', [{}], requestId);
869-
expect(res).to.contain('0x');
870-
expect(res).to.not.be.equal('0x');
871-
expect(res).to.not.be.equal('0x0');
867+
describe('eth_estimateGas', async function () {
868+
it('@release should execute "eth_estimateGas"', async function () {
869+
const res = await relay.call('eth_estimateGas', [{}], requestId);
870+
expect(res).to.contain('0x');
871+
expect(res).to.not.be.equal('0x');
872+
expect(res).to.not.be.equal('0x0');
873+
});
872874
});
873875

874-
it('@release should call eth_gasPrice', async function () {
875-
const res = await relay.call('eth_gasPrice', [], requestId);
876-
expect(res).to.exist;
877-
if (process.env.LOCAL_NODE && process.env.LOCAL_NODE !== 'false') {
878-
expect(res).be.equal(ethers.utils.hexValue(Assertions.defaultGasPrice));
879-
}
880-
else {
881-
expect(Number(res)).to.be.gt(0);
882-
}
883-
});
876+
describe('eth_gasPrice', async function () {
877+
it('@release should call eth_gasPrice', async function () {
878+
const res = await relay.call('eth_gasPrice', [], requestId);
879+
expect(res).to.exist;
880+
if (process.env.LOCAL_NODE && process.env.LOCAL_NODE !== 'false') {
881+
expect(res).be.equal(ethers.utils.hexValue(Assertions.defaultGasPrice));
882+
}
883+
else {
884+
expect(Number(res)).to.be.gt(0);
885+
}
886+
});
887+
})
884888

885-
it('@release should execute "eth_getBalance" for newly created account with 10 HBAR', async function () {
886-
const account = await servicesNode.createAliasAccount(10, null, requestId);
887-
const mirrorAccount = await mirrorNode.get(`/accounts/${account.accountId}`, requestId);
889+
describe('eth_getBalance', async function () {
890+
let getBalanceContractId;
891+
before(async function () {
892+
getBalanceContractId = await accounts[0].client.createParentContract(parentContractJson, requestId);
893+
})
888894

889-
const res = await relay.call('eth_getBalance', ['0x' + account.address, 'latest'], requestId);
890-
const balanceInWeiBars = BigNumber.from(mirrorAccount.balance.balance.toString()).mul(constants.TINYBAR_TO_WEIBAR_COEF);
891-
// balance for tests changes as accounts are in use. Ensure non zero value
892-
expect(res).to.not.be.eq(EthImpl.zeroHex);
893-
});
895+
it('@release should execute "eth_getBalance" for newly created account with 10 HBAR', async function () {
896+
const account = await servicesNode.createAliasAccount(10, null, requestId);
897+
const mirrorAccount = await mirrorNode.get(`/accounts/${account.accountId}`, requestId);
894898

895-
it('should execute "eth_getBalance" for non-existing address', async function () {
896-
const res = await relay.call('eth_getBalance', [NON_EXISTING_ADDRESS, 'latest'], requestId);
897-
expect(res).to.eq('0x0');
898-
});
899+
const res = await relay.call('eth_getBalance', ['0x' + account.address, 'latest'], requestId);
900+
const balanceInWeiBars = BigNumber.from(mirrorAccount.balance.balance.toString()).mul(constants.TINYBAR_TO_WEIBAR_COEF);
901+
// balance for tests changes as accounts are in use. Ensure non zero value
902+
expect(res).to.not.be.eq(EthImpl.zeroHex);
903+
});
899904

900-
it('@release should execute "eth_getBalance" for contract', async function () {
901-
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(contractId.toString()), 'latest'], requestId);
902-
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
903-
});
905+
it('should execute "eth_getBalance" for non-existing address', async function () {
906+
const res = await relay.call('eth_getBalance', [NON_EXISTING_ADDRESS, 'latest'], requestId);
907+
expect(res).to.eq('0x0');
908+
});
904909

905-
it('@release should execute "eth_getBalance" for contract with id converted to evm_address', async function () {
906-
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(contractId.toString()), 'latest'], requestId);
907-
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
908-
});
910+
it('@release should execute "eth_getBalance" for contract', async function () {
911+
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(getBalanceContractId.toString()), 'latest'], requestId);
912+
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
913+
});
909914

910-
it('@release should execute "eth_getBalance" with latest block number', async function () {
911-
const latestBlock = (await mirrorNode.get(`/blocks?limit=1&order=desc`, requestId)).blocks[0];
912-
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(contractId.toString()), EthImpl.numberTo0x(latestBlock.number)], requestId);
913-
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
914-
});
915+
it('@release should execute "eth_getBalance" for contract with id converted to evm_address', async function () {
916+
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(getBalanceContractId.toString()), 'latest'], requestId);
917+
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
918+
});
915919

916-
it('@release should execute "eth_getBalance" with one block behind latest block number', async function () {
917-
const latestBlock = (await mirrorNode.get(`/blocks?limit=1&order=desc`, requestId)).blocks[0];
918-
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(contractId.toString()), EthImpl.numberTo0x(latestBlock.number - 1)], requestId);
919-
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
920-
});
920+
it('@release should execute "eth_getBalance" with latest block number', async function () {
921+
const latestBlock = (await mirrorNode.get(`/blocks?limit=1&order=desc`, requestId)).blocks[0];
922+
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(getBalanceContractId.toString()), EthImpl.numberTo0x(latestBlock.number)], requestId);
923+
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
924+
});
921925

922-
it('@release should execute "eth_getBalance" with pending', async function () {
923-
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(contractId.toString()), 'pending'], requestId);
924-
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
925-
});
926+
it('@release should execute "eth_getBalance" with one block behind latest block number', async function () {
927+
const latestBlock = (await mirrorNode.get(`/blocks?limit=1&order=desc`, requestId)).blocks[0];
928+
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(getBalanceContractId.toString()), EthImpl.numberTo0x(latestBlock.number - 1)], requestId);
929+
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
930+
});
926931

927-
it('@release should execute "eth_getBalance" with block number in the last 15 minutes', async function () {
928-
const latestBlock = (await mirrorNode.get(`/blocks?limit=1&order=desc`, requestId)).blocks[0];
929-
const earlierBlockNumber = latestBlock.number - 2;
930-
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(contractId.toString()), EthImpl.numberTo0x(earlierBlockNumber)], requestId);
931-
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
932-
});
932+
it('@release should execute "eth_getBalance" with pending', async function () {
933+
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(getBalanceContractId.toString()), 'pending'], requestId);
934+
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
935+
});
933936

934-
it('@release should execute "eth_getBalance" with block number in the last 15 minutes for account that has performed contract deploys/calls"', async function () {
935-
const res = await relay.call('eth_getBalance', ['0x' + accounts[0].address, EthImpl.numberTo0x(blockNumberAtStartOfTests)], requestId);
936-
const balanceAtBlock = mirrorAccount0AtStartOfTests.balance.balance * constants.TINYBAR_TO_WEIBAR_COEF;
937-
expect(res).to.eq(`0x${balanceAtBlock.toString(16)}`);
938-
});
937+
it('@release should execute "eth_getBalance" with block number in the last 15 minutes', async function () {
938+
const latestBlock = (await mirrorNode.get(`/blocks?limit=1&order=desc`, requestId)).blocks[0];
939+
const earlierBlockNumber = latestBlock.number - 2;
940+
const res = await relay.call('eth_getBalance', [Utils.idToEvmAddress(getBalanceContractId.toString()), EthImpl.numberTo0x(earlierBlockNumber)], requestId);
941+
expect(res).to.eq(ethers.utils.hexValue(ONE_WEIBAR));
942+
});
939943

940-
it('@release should correctly execute "eth_getBalance" with block number in the last 15 minutes with several txs around that time', async function () {
941-
const initialBalance = await relay.call('eth_getBalance', ['0x' + accounts[0].address, 'latest'], requestId);
944+
it('@release should execute "eth_getBalance" with block number in the last 15 minutes for account that has performed contract deploys/calls"', async function () {
945+
const res = await relay.call('eth_getBalance', ['0x' + accounts[0].address, EthImpl.numberTo0x(blockNumberAtStartOfTests)], requestId);
946+
const balanceAtBlock = mirrorAccount0AtStartOfTests.balance.balance * constants.TINYBAR_TO_WEIBAR_COEF;
947+
expect(res).to.eq(`0x${balanceAtBlock.toString(16)}`);
948+
});
942949

943-
const acc1Nonce = await relay.getAccountNonce('0x' + accounts[1].address);
944-
const gasPrice = await relay.gasPrice();
950+
it('@release should correctly execute "eth_getBalance" with block number in the last 15 minutes with several txs around that time', async function () {
951+
const initialBalance = await relay.call('eth_getBalance', ['0x' + accounts[0].address, 'latest'], requestId);
945952

946-
const transaction = {
947-
value: ONE_TINYBAR,
948-
gasLimit: 50000,
949-
chainId: Number(CHAIN_ID),
950-
to: accounts[0].wallet.address,
951-
nonce: acc1Nonce,
952-
gasPrice: gasPrice,
953-
};
953+
const acc3Nonce = await relay.getAccountNonce('0x' + accounts[3].address);
954+
const gasPrice = await relay.gasPrice();
955+
956+
const transaction = {
957+
value: ONE_TINYBAR,
958+
gasLimit: 50000,
959+
chainId: Number(CHAIN_ID),
960+
to: accounts[0].wallet.address,
961+
nonce: acc3Nonce,
962+
gasPrice: gasPrice,
963+
};
954964

955-
const signedTx1 = await accounts[3].wallet.signTransaction(transaction);
956-
const txHash1 = await relay.call('eth_sendRawTransaction', [signedTx1]);
957-
await mirrorNode.get(`/contracts/results/${txHash1}`, requestId);
958-
const tx1 = await relay.call('eth_getTransactionByHash', [txHash1]);
965+
const signedTx1 = await accounts[3].wallet.signTransaction(transaction);
966+
const txHash1 = await relay.call('eth_sendRawTransaction', [signedTx1]);
967+
await mirrorNode.get(`/contracts/results/${txHash1}`, requestId);
968+
const tx1 = await relay.call('eth_getTransactionByHash', [txHash1]);
969+
await new Promise(r => setTimeout(r, 4000));
959970

960-
const signedTx2 = await accounts[3].wallet.signTransaction({...transaction, nonce: acc1Nonce + 1});
961-
const txHash2 = await relay.call('eth_sendRawTransaction', [signedTx2]);
962-
await mirrorNode.get(`/contracts/results/${txHash2}`, requestId);
963-
const tx2 = await relay.call('eth_getTransactionByHash', [txHash2]);
971+
const signedTx2 = await accounts[3].wallet.signTransaction({...transaction, nonce: acc3Nonce + 1});
972+
const txHash2 = await relay.call('eth_sendRawTransaction', [signedTx2]);
973+
await mirrorNode.get(`/contracts/results/${txHash2}`, requestId);
974+
const tx2 = await relay.call('eth_getTransactionByHash', [txHash2]);
975+
await new Promise(r => setTimeout(r, 4000));
964976

965-
const signedTx3 = await accounts[3].wallet.signTransaction({...transaction, nonce: acc1Nonce + 2});
966-
const txHash3 = await relay.call('eth_sendRawTransaction', [signedTx3]);
967-
await mirrorNode.get(`/contracts/results/${txHash3}`, requestId);
968-
const tx3 = await relay.call('eth_getTransactionByHash', [txHash3]);
977+
const signedTx3 = await accounts[3].wallet.signTransaction({...transaction, nonce: acc3Nonce + 2});
978+
const txHash3 = await relay.call('eth_sendRawTransaction', [signedTx3]);
979+
await mirrorNode.get(`/contracts/results/${txHash3}`, requestId);
980+
const tx3 = await relay.call('eth_getTransactionByHash', [txHash3]);
981+
await new Promise(r => setTimeout(r, 4000));
969982

970-
const endBalance = await relay.call('eth_getBalance', ['0x' + accounts[0].address, 'latest'], requestId);
983+
const endBalance = await relay.call('eth_getBalance', ['0x' + accounts[0].address, 'latest'], requestId);
971984

972-
// initialBalance + sum of value of all transactions
973-
const manuallyCalculatedBalance = BigNumber.from(initialBalance).add(BigNumber.from(ONE_TINYBAR).mul(3));
974-
expect(BigNumber.from(endBalance).toString()).to.eq(manuallyCalculatedBalance.toString());
985+
// initialBalance + sum of value of all transactions
986+
const manuallyCalculatedBalance = BigNumber.from(initialBalance).add(BigNumber.from(ONE_TINYBAR).mul(3));
987+
expect(BigNumber.from(endBalance).toString()).to.eq(manuallyCalculatedBalance.toString());
988+
989+
// Balance at the block number of tx1 should be initialBalance + the value of tx1
990+
const balanceAtTx1Block = await relay.call('eth_getBalance', ['0x' + accounts[0].address, tx1.blockNumber], requestId);
991+
const manuallyCalculatedBalanceAtTx1Block = BigNumber.from(initialBalance).add(BigNumber.from(ONE_TINYBAR));
992+
expect(BigNumber.from(balanceAtTx1Block).toString()).to.eq(manuallyCalculatedBalanceAtTx1Block.toString());
993+
});
975994

976-
// Balance at the block number of tx1 should be initialBalance + the value of tx1
977-
const balanceAtTx1Block = await relay.call('eth_getBalance', ['0x' + accounts[0].address, tx1.blockNumber], requestId);
978-
const manuallyCalculatedBalanceAtTx1Block = BigNumber.from(initialBalance).add(BigNumber.from(ONE_TINYBAR));
979-
expect(BigNumber.from(balanceAtTx1Block).toString()).to.eq(manuallyCalculatedBalanceAtTx1Block.toString());
980995
});
981996

982997
describe('@release Hardcoded RPC Endpoints', () => {

packages/server/tests/helpers/assertions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ export default class Assertions {
101101
// Assert transactions
102102
for (const i in relayResponse.transactions) {
103103
const tx = relayResponse.transactions[i];
104-
const mirrorTx = mirrorTransactions[i];
105104
if ( hydratedTransactions ) {
105+
const mirrorTx = mirrorTransactions.find(mTx => mTx.hash.slice(0,66) === tx.hash);
106106
Assertions.transaction(tx, mirrorTx);
107107
}
108108
else {
109+
const mirrorTx = mirrorTransactions.find(mTx => mTx.hash.slice(0,66) === tx);
109110
expect(tx).to.eq(mirrorTx.hash.slice(0, 66));
110111
}
111112
}

0 commit comments

Comments
 (0)