Skip to content

Commit

Permalink
Refactored code around MirrorNodeClient calls to GET_ACCOUNTS_BY_ID_E…
Browse files Browse the repository at this point in the history
…NDPOINT, so it does not return null, but instead throws the corresponding error and the methods that consume that endpoint handle the errors as they see fit.

Signed-off-by: Alfredo Gutierrez <alfredo@swirldslabs.com>
  • Loading branch information
AlfredoG87 committed Jun 21, 2023
1 parent 2e7ec94 commit 64f4f3e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/relay/src/lib/clients/mirrorNodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class MirrorNodeClient {
private static CONTRACT_RESULT_LOGS_PROPERTY = 'logs';

static acceptedErrorStatusesResponsePerRequestPathMap: Map<string, Array<number>> = new Map([
[MirrorNodeClient.GET_ACCOUNTS_BY_ID_ENDPOINT, [400, 404]],
[MirrorNodeClient.GET_ACCOUNTS_BY_ID_ENDPOINT, []],
[MirrorNodeClient.GET_BALANCE_ENDPOINT, [400, 404]],
[MirrorNodeClient.GET_BLOCK_ENDPOINT, [400, 404]],
[MirrorNodeClient.GET_BLOCKS_ENDPOINT, [400, 404]],
Expand Down Expand Up @@ -325,7 +325,8 @@ export class MirrorNodeClient {
this.logger.error(new Error(error.message), `${requestIdPrefix} [${method}] ${path} ${effectiveStatusCode} status`);

// we only need contract revert errors here as it's not the same as not supported
if (mirrorError.isContractReverted() && !mirrorError.isNotSupported() && !mirrorError.isNotSupportedSystemContractOperaton()) {
// Contract Revert error is valid only for contract calls
if (pathLabel == MirrorNodeClient.CONTRACT_CALL_ENDPOINT && mirrorError.isContractReverted() && !mirrorError.isNotSupported() && !mirrorError.isNotSupportedSystemContractOperaton()) {
throw predefined.CONTRACT_REVERT(mirrorError.errorMessage);
}

Expand Down
37 changes: 33 additions & 4 deletions packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ export class EthImpl implements Eth {
const accountCacheKey = `${constants.CACHE_KEY.ACCOUNT}_${transaction.to}`;
let toAccount: object | null = this.cache.get(accountCacheKey);
if (!toAccount) {
toAccount = await this.mirrorNodeClient.getAccount(transaction.to, requestId);
toAccount = await this.getAccountOrNull(transaction.to, false, requestId);
}

// when account exists return default base gas, otherwise return the minimum amount of gas to create an account entity
Expand All @@ -520,6 +520,35 @@ export class EthImpl implements Eth {
return gas;
}

/**
* Gets the account data using the mirror node and handles the potential 404 error and returns null instead.
* */
async getAccountOrNull(address: string, usePageLimit: boolean, requestId?: string) {
let account;
try {
if(usePageLimit){
account = await this.mirrorNodeClient.getAccountPageLimit(address, requestId);
} else {
account = await this.mirrorNodeClient.getAccount(address, requestId);
}
} catch (error: any) {
if(error instanceof MirrorNodeClientError) {
if(error.statusCode == 404){
return null;

} else if(error.statusCode == 400){
this.logger.debug(`${formatRequestIdMessage(requestId)} Got Invalid Parameter when trying to fetch account from mirror node: ${JSON.stringify(error)}`);
throw predefined.INVALID_PARAMETER(address, `Invalid 'address' field in transaction param. Address must be a valid 20 bytes hex string`);
}
} else {
this.logger.error(`${formatRequestIdMessage(requestId)} Unexpected error raised while fetching account from mirror-node: ${JSON.stringify(error)}`);
throw error;
}
}

return account;
}

/**
* Gets the current gas price of the network.
*/
Expand Down Expand Up @@ -760,7 +789,7 @@ export class EthImpl implements Eth {
let blockNumber = null;
let balanceFound = false;
let weibars: BigInt = BigInt(0);
const mirrorAccount = await this.mirrorNodeClient.getAccountPageLimit(account, requestId);
const mirrorAccount = await this.getAccountOrNull(account, true, requestId);

try {
if (!EthImpl.blockTagIsLatestOrPending(blockNumberOrTag)) {
Expand Down Expand Up @@ -1398,7 +1427,7 @@ export class EthImpl implements Eth {
const accountCacheKey = `${constants.CACHE_KEY.ACCOUNT}_${fromAddress}`;
let accountResult: any | null = this.cache.get(accountCacheKey);
if (!accountResult) {
accountResult = await this.mirrorNodeClient.getAccount(fromAddress, requestId);
accountResult = await this.getAccountOrNull(fromAddress, false, requestId);
if (accountResult) {
this.logger.trace(`${requestIdPrefix} caching ${accountCacheKey}:${JSON.stringify(accountResult)} for ${constants.CACHE_TTL.ONE_HOUR} ms`);
this.cache.set(accountCacheKey, accountResult);
Expand Down Expand Up @@ -1889,7 +1918,7 @@ export class EthImpl implements Eth {
}

// get latest ethereumNonce from mirror node account API
const mirrorAccount = await this.mirrorNodeClient.getAccount(address, requestId);
const mirrorAccount = await this.getAccountOrNull(address, false, requestId);
if (mirrorAccount?.ethereum_nonce) {
return EthImpl.numberTo0x(mirrorAccount.ethereum_nonce);
}
Expand Down

0 comments on commit 64f4f3e

Please sign in to comment.