Skip to content

Commit

Permalink
Refactor robustQuery used in different places
Browse files Browse the repository at this point in the history
  • Loading branch information
jairajdev committed Apr 22, 2024
1 parent 383aaea commit b8318cb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 50 deletions.
21 changes: 7 additions & 14 deletions src/Data/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ import * as StateMetaData from '../archivedCycle/StateMetaData'
import fetch from 'node-fetch'
import { syncV2 } from '../sync-v2'
import { queryFromArchivers, RequestDataType } from '../API'
// Socket modules
import ioclient = require('socket.io-client')
import { Transaction } from '../dbstore/transactions'
import { AccountCopy } from '../dbstore/accounts'
import { robustQuery } from '../Utils'
export let socketServer: SocketIO.Server

export const socketClients: Map<string, SocketIOClientStatic['Socket']> = new Map()
// let socketConnectionsTracker: Map<string, string> = new Map()
export let combineAccountsData = {
accounts: [],
receipts: [],
Expand Down Expand Up @@ -557,25 +555,21 @@ async function getConsensusRadius(): Promise<number> {
// Define the equality function to compare two responses
const equalityFn = (responseA, responseB): boolean => {
return (
responseA.config.sharding.nodesPerConsensusGroup === responseB.config.sharding.nodesPerConsensusGroup
responseA?.config?.sharding?.nodesPerConsensusGroup ===
responseB?.config?.sharding?.nodesPerConsensusGroup
)
}

// Get the list of active nodes or the first node if no active nodes are available
// Get the list of 10 max random active nodes or the first node if no active nodes are available
const nodes =
NodeList.getActiveNodeCount() > 0
? NodeList.getRandomActiveNodes(NodeList.getActiveNodeCount())
: NodeList.getList().slice(0, 1)
NodeList.getActiveNodeCount() > 0 ? NodeList.getRandomActiveNodes(10) : NodeList.getList().slice(0, 1)

// Use robustQuery to get the consensusRadius from multiple nodes
const tallyItem = await robustQuery(
nodes,
queryFn,
equalityFn,
3, // Redundancy
true, // Shuffle nodes
0, // No delay
false // Disable fail log
3 // Redundancy (minimum 3 nodes should return the same result to reach consensus)
)

// Check if a consensus was reached
Expand All @@ -598,10 +592,9 @@ async function getConsensusRadius(): Promise<number> {
'nodesPerEdge',
nodesPerEdge
)
if (config.VERBOSE) console.log('consensusRadius', consensusRadius)
return consensusRadius
}

Logger.mainLogger.error('Failed to get consensusRadius from the network')
// If no consensus was reached, return the existing currentConsensusRadius
return currentConsensusRadius
}
Expand Down
4 changes: 2 additions & 2 deletions src/GlobalAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const syncGlobalAccount = async (): Promise<void> => {
return equivalent
}

const globalAccsResponse = await robustQuery(filteredArchivers, queryFn, equalFn)
const globalAccsResponse = await robustQuery(filteredArchivers, queryFn, equalFn, 3, true)
Logger.mainLogger.debug('syncGlobalAccount() - globalAccsResponse', globalAccsResponse)
if (!globalAccsResponse) {
Logger.mainLogger.warn('() - robustResponse is null')
Expand All @@ -96,7 +96,7 @@ export const syncGlobalAccount = async (): Promise<void> => {
const queryFn = async (node: Node): Promise<any> => {
return await getJson(`http://${node.ip}:${node.port}/get-network-account?hash=false`)
}
const networkAccResponse = await robustQuery(filteredArchivers, queryFn)
const networkAccResponse = await robustQuery(filteredArchivers, queryFn, equalFn, 3, true)
Logger.mainLogger.debug('syncGlobalAccount() - networkAccResponse', networkAccResponse)
if (!networkAccResponse) {
Logger.mainLogger.warn('get-network-account() - robustResponse is null')
Expand Down
33 changes: 0 additions & 33 deletions src/NodeList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,39 +419,6 @@ export async function getActiveNodeListFromArchiver(
return []
}

// We can't get the same node list from all archivers; Each archiver would response with its max 30 random nodes
export async function getActiveListFromArchivers(
activeArchivers: State.ArchiverNodeInfo[]
): Promise<ConsensusNodeInfo> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isSameCycleInfo(info1: any, info2: any): boolean {
const cm1 = Utils.deepCopy(info1)
const cm2 = Utils.deepCopy(info2)
delete cm1.currentTime
delete cm2.currentTime
const equivalent = isDeepStrictEqual(cm1, cm2)
return equivalent
}

const queryFn = async (node: ConsensusNodeInfo): Promise<ConsensusNodeInfo[]> => {
const response = (await P2P.getJson(
`http://${node.ip}:${node.port}/nodelist`
)) as ConsensusNodeListResponse

if (response && response.nodeList) {
return response.nodeList.sort((a: ConsensusNodeInfo, b: ConsensusNodeInfo) =>
a.publicKey > b.publicKey ? 1 : -1
)
}
return null
}
const nodeList = await Utils.robustQuery(activeArchivers, queryFn, isSameCycleInfo)
if (nodeList && nodeList.count > 0) {
return nodeList.value[0]
}
return null
}

export function getRandomActiveNodes(node_count = 1): ConsensusNodeInfo[] {
const nodeList = getActiveList()
if (node_count <= 1 || node_count > nodeList.length)
Expand Down
2 changes: 1 addition & 1 deletion src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export async function robustQuery<Node = unknown, Response = unknown>(
queryFn: QueryFunction<Node, Response>,
equalityFn: EqualityFunction<Response> = util.isDeepStrictEqual,
redundancy = 3,
shuffleNodes = true,
shuffleNodes = false, // Set to true if nodes need to be shuffled before querying
delayTimeInMS = 0, // Add a delay after we have queried half of the nodes
disableFailLog = false
): Promise<TallyItem<Node, Response>> {
Expand Down

0 comments on commit b8318cb

Please sign in to comment.