Skip to content

Commit

Permalink
ALL-3446 - Fixed LoadBalancer throwing from setTimeout (#1018)
Browse files Browse the repository at this point in the history
* ALL-3446 - Fixed `LoadBalancer` throwing from setTimeout

* ALL-3446 - Fixed `LoadBalancer` throwing from setTimeout - cleanup

* ALL-3446 - Fixed `LoadBalancer` throwing from setTimeout - cleanup 2
  • Loading branch information
Smrecz authored Nov 12, 2023
1 parent 588446f commit 6531ad7
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 47 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## [4.1.25] - 2023.11.2
## [4.1.26] - 2023.11.12
### Fixed
- Fixed `LoadBalancer` throwing from setTimeout, it should now throw from actual method calls.

## [4.1.25] - 2023.11.10
### Added
- Added RPC support for the ALGORAND network. Users can now make RPC calls to these network using the `Network.ALGORAND_ALGOD`, `Network.ALGORAND_ALGOD_TESTNET`, `Network.ALGORAND_INDEXER`, `Network.ALGORAND_INDEXER_TESTNET` network.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tatumio/tatum",
"version": "4.1.25",
"version": "4.1.26",
"description": "Tatum JS SDK",
"author": "Tatum",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
2 changes: 1 addition & 1 deletion src/dto/rpc/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './EvmBasedRpcInterface'
export * from './SolanaRpcSuite'
export * from './TezosRpcSuite'
export * from './TronRpcSuite'
export * from './UtxoBasedRpcSuite'
export * from './XrpRpcSuite'
export * from './TezosRpcSuite'
2 changes: 1 addition & 1 deletion src/dto/shared.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ export interface TokenDetails {
export type QueryValue = string | number | boolean | string[] | number[] | boolean[]
export type QueryParams = Record<string, QueryValue>
export type SendGet = {
path: string,
path: string
queryParams?: QueryParams
}
8 changes: 4 additions & 4 deletions src/e2e/rpc/other/tatum.rpc.tezos.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ describe.each([false, true])(`Tezos`, (testnet: boolean) => {
chainId: 'main',
contractId: 'KT1Hkg5qeNhfwpKW4fXvq7HGZB9z2EnmCCA9',
block: '3000000',
normalizeTypes: true
})
await tatum.destroy()
expect(result).toBeDefined()
normalizeTypes: true,
})
await tatum.destroy()
expect(result).toBeDefined()
})

it('getContractBalanceAndFrozenBonds', async () => {
const tatum = await getTezosRpc(testnet)
Expand Down
37 changes: 19 additions & 18 deletions src/service/rpc/evm/AbstractBeaconV1EvmRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import {
BlockQuery,
EvmBeaconResponse,
EvmBeaconV1Interface,
QueryParams,
StateCommitteesQuery,
StateQuery,
StateSyncCommitteesQuery,
ValidatorBalancesQuery,
ValidatorQuery,
ValidatorsQuery, QueryParams,
ValidatorsQuery,
} from '../../../dto'
import { GetI } from '../../../dto/GetI'
import { Constant, Utils } from '../../../util'
Expand All @@ -17,16 +18,16 @@ export abstract class AbstractBeaconV1EvmRpc implements EvmBeaconV1Interface {
protected abstract get<T>(get: GetI): Promise<T>

private sendGet<T>(path: string, params: QueryParams): Promise<T> {
const fullPath = Utils.addQueryParams(`${Constant.BEACON_PREFIX}/${path}`, Utils.camelToSnakeCase, params);
return this.get({ path: fullPath });
const fullPath = Utils.addQueryParams(`${Constant.BEACON_PREFIX}/${path}`, Utils.camelToSnakeCase, params)
return this.get({ path: fullPath })
}

getBlockAttestations({ blockId, ...rest }: BlockQuery): Promise<EvmBeaconResponse<any>> {
return this.sendGet(`blocks/${blockId}/attestations`, rest);
return this.sendGet(`blocks/${blockId}/attestations`, rest)
}

getBlockHeader({ blockId, ...rest }: BlockQuery): Promise<EvmBeaconResponse<any>> {
return this.sendGet(`blocks/${blockId}/header`, rest);
return this.sendGet(`blocks/${blockId}/header`, rest)
}

getBlockHeaders({ slot, parentRoot, ...rest }: { slot?: string; parentRoot?: string } = {}): Promise<
Expand All @@ -35,48 +36,48 @@ export abstract class AbstractBeaconV1EvmRpc implements EvmBeaconV1Interface {
const queryParams = {
...(slot ? { slot } : {}),
...(parentRoot ? { parentRoot } : {}),
...rest
};
return this.sendGet(`headers`, queryParams);
...rest,
}
return this.sendGet(`headers`, queryParams)
}

getBlockRoot({ blockId, ...rest }: BlockQuery): Promise<EvmBeaconResponse<any>> {
return this.sendGet(`blocks/${blockId}/root`, rest);
return this.sendGet(`blocks/${blockId}/root`, rest)
}

getGenesis(): Promise<EvmBeaconResponse<any>> {
return this.sendGet('genesis', {});
return this.sendGet('genesis', {})
}

getStateCommittees({ stateId, ...rest }: StateCommitteesQuery): Promise<EvmBeaconResponse<any>> {
return this.sendGet(`states/${stateId}/committees`, rest);
return this.sendGet(`states/${stateId}/committees`, rest)
}

getStateFinalityCheckpoints({ stateId, ...rest }: StateQuery): Promise<EvmBeaconResponse<any>> {
return this.sendGet(`states/${stateId}/finality_checkpoints`, rest);
return this.sendGet(`states/${stateId}/finality_checkpoints`, rest)
}

getStateFork({ stateId, ...rest }: StateQuery): Promise<EvmBeaconResponse<any>> {
return this.sendGet(`states/${stateId}/fork`, rest);
return this.sendGet(`states/${stateId}/fork`, rest)
}

getStateRoot({ stateId, ...rest }: StateQuery): Promise<EvmBeaconResponse<any>> {
return this.sendGet(`states/${stateId}/root`, rest);
return this.sendGet(`states/${stateId}/root`, rest)
}

getStateSyncCommittees({ stateId, ...rest }: StateSyncCommitteesQuery): Promise<EvmBeaconResponse<any>> {
return this.sendGet(`states/${stateId}/sync_committees`, rest);
return this.sendGet(`states/${stateId}/sync_committees`, rest)
}

getStateValidator({ stateId, validatorId, ...rest }: ValidatorQuery): Promise<EvmBeaconResponse<any>> {
return this.sendGet(`states/${stateId}/validators/${validatorId}`, rest);
return this.sendGet(`states/${stateId}/validators/${validatorId}`, rest)
}

getStateValidatorBalances({ stateId, ...rest }: ValidatorBalancesQuery): Promise<EvmBeaconResponse<any>> {
return this.sendGet(`states/${stateId}/validator_balances`, rest);
return this.sendGet(`states/${stateId}/validator_balances`, rest)
}

getStateValidators({ stateId, ...rest }: ValidatorsQuery): Promise<EvmBeaconResponse<any>> {
return this.sendGet(`states/${stateId}/validators`, rest);
return this.sendGet(`states/${stateId}/validators`, rest)
}
}
23 changes: 19 additions & 4 deletions src/service/rpc/generic/LoadBalancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class LoadBalancer implements AbstractRpcInterface {
}
private timeout: unknown
private network: Network
private noActiveNode = false

constructor(private readonly id: string) {
this.connector = Container.of(this.id).get(TatumConnector)
Expand Down Expand Up @@ -140,10 +141,7 @@ export class LoadBalancer implements AbstractRpcInterface {
private async checkStatuses() {
await this.checkStatus(RpcNodeType.NORMAL)
await this.checkStatus(RpcNodeType.ARCHIVE)
if (!this.activeUrl[RpcNodeType.NORMAL].url && !this.activeUrl[RpcNodeType.ARCHIVE].url) {
Utils.log({ id: this.id, message: 'No active node found, please set node urls manually.' })
throw new Error('No active node found, please set node urls manually.')
}
this.checkIfNoActiveNodes()

const { rpc } = Container.of(this.id).get(CONFIG)
if (!rpc?.oneTimeLoadBalancing) {
Expand All @@ -154,6 +152,15 @@ export class LoadBalancer implements AbstractRpcInterface {
}
}

private checkIfNoActiveNodes() {
if (!this.activeUrl[RpcNodeType.NORMAL].url && !this.activeUrl[RpcNodeType.ARCHIVE].url) {
Utils.log({ id: this.id, message: 'No active node found, please set node urls manually.' })
this.noActiveNode = true
} else {
this.noActiveNode = false
}
}

private async checkStatus(nodeType: RpcNodeType) {
const { rpc, network } = Container.of(this.id).get(CONFIG)
const all = []
Expand Down Expand Up @@ -268,6 +275,10 @@ export class LoadBalancer implements AbstractRpcInterface {
return { url: this.getActiveUrl(RpcNodeType.NORMAL).url, type: RpcNodeType.NORMAL }
}

if (this.noActiveNode) {
throw new Error('No active node found, please set node urls manually.')
}

throw new Error('No active node found.')
}

Expand All @@ -281,6 +292,10 @@ export class LoadBalancer implements AbstractRpcInterface {
return { url: this.getActiveUrl(RpcNodeType.ARCHIVE).url, type: RpcNodeType.ARCHIVE }
}

if (this.noActiveNode) {
throw new Error('No active node found, please set node urls manually.')
}

throw new Error('No active node found.')
}

Expand Down
14 changes: 4 additions & 10 deletions src/service/rpc/other/AbstractTezosRpc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { Service } from 'typedi'
import { QueryParams } from '../../../dto'
import { GetI } from '../../../dto/GetI'
import { PostI } from '../../../dto/PostI'
import {
GetBlock,
GetBlockHashes,
Expand All @@ -18,23 +15,20 @@ import {
InjectOperation,
InjectProtocol,
PreapplyOperations,
QueryParams,
SimulateOperation,
TezosRpcInterface,
} from '../../../dto'
import { GetI } from '../../../dto/GetI'
import { PostI } from '../../../dto/PostI'
import { Utils } from '../../../util'

@Service()
export abstract class AbstractTezosRpc implements TezosRpcInterface {
protected abstract get<T>(post: GetI): Promise<T>
protected abstract post<T>(post: PostI): Promise<T>

private async sendGet<T>({
path,
queryParams,
}: {
path: string
queryParams?: QueryParams
}): Promise<T> {
private async sendGet<T>({ path, queryParams }: { path: string; queryParams?: QueryParams }): Promise<T> {
if (queryParams && Object.keys(queryParams).length > 0) {
return this.get({ path: Utils.addQueryParams(path, Utils.camelToSnakeCase, queryParams) })
}
Expand Down
2 changes: 1 addition & 1 deletion src/service/rpc/other/AlgorandAlgodLoadBalancerRpc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Container, Service } from 'typedi'
import { LoadBalancer } from '../generic/LoadBalancer'
import { GetI } from '../../../dto/GetI'
import { PostI } from '../../../dto/PostI'
import { LoadBalancer } from '../generic'
import { AbstractAlgorandAlgodRpc } from './AbstractAlgorandAlgodRpc'

@Service({
Expand Down
2 changes: 1 addition & 1 deletion src/service/rpc/other/AlgorandIndexerLoadBalancerRpc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Container, Service } from 'typedi'
import { LoadBalancer } from '../generic/LoadBalancer'
import { GetI } from '../../../dto/GetI'
import { LoadBalancer } from '../generic'
import { AbstractAlgorandIndexerRpc } from './AbstractAlgorandIndexerRpc'

@Service({
Expand Down
9 changes: 4 additions & 5 deletions src/service/tatum/tatum.other.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Container } from 'typedi'
import { SolanaRpcSuite, TronRpcSuite, XrpRpcInterface } from '../../dto'
import { SolanaRpcSuite, TezosRpcInterface, TronRpcSuite, XrpRpcInterface } from '../../dto'
import { AlgorandAlgodRpcSuite } from '../../dto/rpc/AlgorandAlgodRpcSuite'
import { AlgorandIndexerRpcSuite } from '../../dto/rpc/AlgorandIndexerRpcSuite'
import { BnbRpcSuite } from '../../dto/rpc/BnbRpcSuite'
import { EosRpcSuite } from '../../dto/rpc/EosRpcSuite'
import { CONFIG, Utils } from '../../util'
import { Address, AddressTezos, AddressTron } from '../address'
Expand All @@ -10,10 +13,6 @@ import { Notification } from '../notification'
import { Rates } from '../rate'
import { Token } from '../token'
import { TatumSdkChain } from './tatum'
import { BnbRpcSuite } from '../../dto/rpc/BnbRpcSuite'
import { TezosRpcInterface } from '../../dto/rpc/TezosRpcSuite'
import { AlgorandAlgodRpcSuite } from '../../dto/rpc/AlgorandAlgodRpcSuite'
import { AlgorandIndexerRpcSuite } from '../../dto/rpc/AlgorandIndexerRpcSuite'

export abstract class BaseOther extends TatumSdkChain {
ipfs: Ipfs
Expand Down

0 comments on commit 6531ad7

Please sign in to comment.