From a9c1159f684cac6e6eedfcfa79692017d11314df Mon Sep 17 00:00:00 2001 From: jairajdev Date: Tue, 30 Apr 2024 22:03:32 +0545 Subject: [PATCH] Minor refactor in joinstatus check --- scripts/api_tester.ts | 1 + scripts/get_tx_receipt.ts | 2 ++ src/Data/Collector.ts | 7 +++++- src/Data/Data.ts | 52 +++++++-------------------------------- src/server.ts | 3 +-- 5 files changed, 19 insertions(+), 46 deletions(-) diff --git a/scripts/api_tester.ts b/scripts/api_tester.ts index 756ff678..9a3f57e7 100644 --- a/scripts/api_tester.ts +++ b/scripts/api_tester.ts @@ -25,6 +25,7 @@ crypto.signObj(data, devAccount.secretKey, devAccount.publicKey) fetch(`${ARCHIVER_URL}/totalData`, { // fetch(`${ARCHIVER_URL}/cycleinfo`, { // fetch(`${ARCHIVER_URL}/receipt`, { + // fetch(`${ARCHIVER_URL}/account`, { method: 'post', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json' }, diff --git a/scripts/get_tx_receipt.ts b/scripts/get_tx_receipt.ts index 478f17a5..8c9e555c 100644 --- a/scripts/get_tx_receipt.ts +++ b/scripts/get_tx_receipt.ts @@ -16,6 +16,7 @@ const devAccount = { const ARCHIVER_URL = `http://127.0.0.1:4000` const txId = '' +const timestamp = 0 const full_receipt = false const runProgram = async (): Promise => { @@ -26,6 +27,7 @@ const runProgram = async (): Promise => { for (const node of nodeList) { const data: any = { txId, + timestamp, full_receipt, } crypto.signObj(data, devAccount.secretKey, devAccount.publicKey) diff --git a/src/Data/Collector.ts b/src/Data/Collector.ts index 78ea3a4f..52090b6f 100644 --- a/src/Data/Collector.ts +++ b/src/Data/Collector.ts @@ -181,7 +181,12 @@ const isReceiptRobust = async ( `'fullReceiptResult ${receipt.tx.txId} , ${receipt.cycle}, ${receipt.tx.timestamp}`, fullReceiptResult ) - if (!fullReceiptResult || !fullReceiptResult.receipt) continue + if (!fullReceiptResult || !fullReceiptResult.receipt) { + Logger.mainLogger.error( + `Got fullReceiptResult null from robustQuery node for ${receipt.tx.txId} , ${receipt.cycle}, ${receipt.tx.timestamp}` + ) + continue + } const fullReceipt = fullReceiptResult.receipt as Receipt.ArchiverReceipt if ( isReceiptEqual(fullReceipt.appliedReceipt, robustQueryReceipt) && diff --git a/src/Data/Data.ts b/src/Data/Data.ts index 58ef4b0c..a8543931 100644 --- a/src/Data/Data.ts +++ b/src/Data/Data.ts @@ -756,14 +756,11 @@ export function calcIncomingTimes(record: P2PTypes.CycleCreatorTypes.CycleRecord export async function joinNetwork( nodeList: NodeList.ConsensusNodeInfo[], - isFirstTime: boolean, - checkFromConsensor = false + isFirstTime: boolean ): Promise { Logger.mainLogger.debug('Is firstTime', isFirstTime) if (!isFirstTime) { - let isJoined: boolean - if (checkFromConsensor) isJoined = await checkJoinStatusFromConsensor(nodeList) - else isJoined = await checkJoinStatus(nodeList) + const isJoined: boolean = await checkJoinStatus(nodeList) if (isJoined) { return isJoined } @@ -907,31 +904,24 @@ export async function getCycleDuration(): Promise { /* checkJoinStatus checks if the current archiver node is joined to a network. - It randomly selects up to 5 active nodes to query about the join status, - then queries /joinedArchiver endpoint on those nodes and returns joining status - based on majority response + This queries by the /joinedArchiver endpoint on the nodes and returns joining status based on majority response. */ export async function checkJoinStatus(activeNodes: NodeList.ConsensusNodeInfo[]): Promise { Logger.mainLogger.debug('checkJoinStatus: Checking join status') const ourNodeInfo = State.getNodeInfo() - // Get 5 random nodes or all activeNodes if activeNodes count is less than 5 - const selectedNodes = Utils.getRandom(activeNodes, Math.min(activeNodes.length, 5)) - - Logger.mainLogger.debug('checkJoinStatus: selectedNodes: ', selectedNodes) - const queryFn = async (node: NodeList.ConsensusNodeInfo): Promise => { - const url = `http://${node.ip}:${node.port}/joinedArchiver/${ourNodeInfo.publicKey}`; + const url = `http://${node.ip}:${node.port}/joinedArchiver/${ourNodeInfo.publicKey}` try { - return await getJson(url) as JoinStatus; + return (await getJson(url)) as JoinStatus } catch (e) { - Logger.mainLogger.error(`Error querying node ${node.ip}:${node.port}: ${e}`); - throw e; + Logger.mainLogger.error(`Error querying node ${node.ip}:${node.port}: ${e}`) + throw e } } try { - const joinStatus = await robustQuery(selectedNodes, queryFn) + const joinStatus = await robustQuery(activeNodes, queryFn) Logger.mainLogger.debug(`checkJoinStatus: Join status: ${joinStatus.value.isJoined}`) return joinStatus.value.isJoined } catch (e) { @@ -974,30 +964,6 @@ export async function getTotalDataFromArchivers(): Promise { - Logger.mainLogger.debug('Checking join status from consenosr') - try { - const latestCycle = await getNewestCycleFromConsensors(nodeList) - if (latestCycle && latestCycle.joinedArchivers && latestCycle.refreshedArchivers) { - const joinedArchivers = latestCycle.joinedArchivers - const refreshedArchivers = latestCycle.refreshedArchivers - Logger.mainLogger.debug('cycle counter', latestCycle.counter) - Logger.mainLogger.debug('Joined archivers', joinedArchivers) - - const isJoined: boolean = [...joinedArchivers, ...refreshedArchivers].some( - (a) => a.publicKey === State.getNodeInfo().publicKey - ) - Logger.mainLogger.debug('isJoined', isJoined) - return !!isJoined - } else { - return false - } - } catch (e) { - Logger.mainLogger.error(e) - return false - } -} - export async function syncGenesisAccountsFromArchiver(): Promise { let complete = false let startAccount = 0 @@ -1632,7 +1598,7 @@ export const syncOriginalTxsByCycle = async ( return } totalCycles = response.totalCycles - totalOriginalTxs = response.totalReceipts + totalOriginalTxs = response.totalOriginalTxs } const complete = false let startCycle = lastStoredOriginalTxCycle diff --git a/src/server.ts b/src/server.ts index dd762eb3..7072edc5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -121,14 +121,13 @@ async function start(): Promise { let isJoined = false let firstTime = true const cycleDuration = Cycles.currentCycleDuration - const checkFromConsensor = true do { try { // Get active nodes from Archiver const nodeList = NodeList.getActiveList() // try to join the network - isJoined = await Data.joinNetwork(nodeList, firstTime, checkFromConsensor) + isJoined = await Data.joinNetwork(nodeList, firstTime) } catch (err: unknown) { Logger.mainLogger.error('Error while joining network:') Logger.mainLogger.error(err as Error)