Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #194

Merged
merged 3 commits into from
May 6, 2024
Merged

Dev #194

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/balance/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ import { Balances } from "./types"

/**
* @name getBalances
* @summary Get the balances of an account including free & reserved balances as well as the total.
* @summary Get the balances of an account including free & reserved balances as well as the total.
* Currently Mainnet also returns miscFrozen & feeFrozen while alphanet returns frozen and flags. After next Mainnet runtime upgrade both miscFrozen & feeFrozen will be removed.
* @param address Public address of the account to get balances.
* @returns The balances of the account.
*/
export const getBalances = async (
address: string,
): Promise<Balances> => {
const balances: Balances = (
(await query(txPallets.system, chainQuery.account, [address])) as any
).data
export const getBalances = async (address: string): Promise<Balances> => {
const balances: Balances = ((await query(txPallets.system, chainQuery.account, [address])) as any).data
return balances
}

Expand Down
12 changes: 6 additions & 6 deletions src/balance/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import BN from "bn.js"

export type Balances = {
free: BN,
reserved: BN,
frozen?: BN,
flags?: BN,
miscFrozen?: BN,
feeFrozen?: BN,
free: BN
reserved: BN
frozen?: BN
flags?: BN
miscFrozen?: BN
feeFrozen?: BN
}
2 changes: 1 addition & 1 deletion src/helpers/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class HttpClient {
constructor(baseURL: string, timeout?: number) {
this.client = axios.create({
baseURL,
...(timeout && { timeout })
...(timeout && { timeout }),
})
}

Expand Down
30 changes: 20 additions & 10 deletions src/helpers/tee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
ClusterDataType,
} from "../tee/types"
import { isValidAddress, query } from "../blockchain"
import { getBalances } from "../balance"
import { BN } from "bn.js"

export const SSSA_NUMSHARES = 5
export const SSSA_THRESHOLD = 3
Expand Down Expand Up @@ -94,15 +96,24 @@ export const combineKeyShares = (shares: string[]): string => {
*/
export const getEnclaveHealthStatus = async (clusterId = 0, timeout = 10000) => {
const teeEnclaves = await getTeeEnclavesBaseUrl(clusterId)
const lastBlock = await getLastBlock()
const clusterHealthCheck = await Promise.all(
teeEnclaves.map(async (enclaveUrl, idx) => {
const http = new HttpClient(ensureHttps(enclaveUrl), timeout)
const enclaveData: EnclaveHealthType = await http.getRaw(TEE_HEALTH_ENDPOINT)
const isError = enclaveData.status !== 200
if (isError || !enclaveData.sync_state.length || enclaveData.sync_state == "setup")
throw new Error(
`${Errors.TEE_ENCLAVE_NOT_AVAILBLE} - ID ${idx}, URL: ${enclaveUrl}. ${enclaveData.description}`,
)
if (isError || !enclaveData.sync_state.length || enclaveData.sync_state == "setup") throw new Error(
`${Errors.TEE_ENCLAVE_NOT_AVAILBLE} - ID ${idx}, URL: ${enclaveUrl}. ${enclaveData.description}`,
)
// ADDITIONAL CHECKS
if ((lastBlock - enclaveData.block_number) > 4) throw new Error(
`${Errors.TEE_ENCLAVE_NOT_AVAILBLE} - ID ${idx}, URL: ${enclaveUrl}. Enclave blocks not synchornized with chain`,
)
const { free } = await getBalances(enclaveData.enclave_address)
const ONE_CAPS = new BN("1000000000000000000")
if (free.lt(ONE_CAPS)) throw new Error(
`${Errors.TEE_ENCLAVE_NOT_AVAILBLE} - ID ${idx}, URL: ${enclaveUrl}. Enclave balance too low`,
)
return enclaveData
}),
)
Expand Down Expand Up @@ -223,16 +234,15 @@ export const getPublicsClusters = async () => {
*/
export const getFirstPublicClusterAvailable = async (timeout = 10000) => {
const publicClusters = await getPublicsClusters()
if (publicClusters.length === 0) return undefined;
if (publicClusters.length === 0) return undefined

for (const cluster of publicClusters) {
try {
const healthData = await timeoutTrigger<EnclaveDataAndHealthType[]>(() =>
getEnclaveDataAndHealth(cluster, timeout),
const healthData = await timeoutTrigger<EnclaveHealthType[]>(() =>
getEnclaveHealthStatus(cluster, timeout),
timeout + 1000
)
const filteredData = healthData.filter((c) => c.status === 200)
if (filteredData.length === ENCLAVES_IN_CLUSTER) {
if (healthData.length === ENCLAVES_IN_CLUSTER) {
return cluster
}
} catch (error) {
Expand Down Expand Up @@ -630,4 +640,4 @@ export const teeNFTReconciliation = async (
if (!nftList) throw new Error(Errors.NFT_RECONCILIATION_FAILED)
nftList = nftList.filter((x) => x !== undefined)
return [...nftList, ...errors]
}
}
17 changes: 8 additions & 9 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,16 @@ export const ensureHttps = (url: string) => {
export const timeoutTrigger = <T>(fn: () => Promise<T>, duration = 10000): Promise<T> => {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(new Error('Error: Function timed out'));
}, duration);
reject(new Error("Error: Function timed out"))
}, duration)

try {
const data = fn();
clearTimeout(timer);
resolve(data);
const data = fn()
clearTimeout(timer)
resolve(data)
} catch (error) {
clearTimeout(timer);
reject(error);
clearTimeout(timer)
reject(error)
}
});
})
}

Loading