Skip to content

Commit

Permalink
Minor refactor in joinstatus check
Browse files Browse the repository at this point in the history
  • Loading branch information
jairajdev committed Apr 30, 2024
1 parent bfda849 commit a9c1159
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 46 deletions.
1 change: 1 addition & 0 deletions scripts/api_tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
2 changes: 2 additions & 0 deletions scripts/get_tx_receipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
Expand All @@ -26,6 +27,7 @@ const runProgram = async (): Promise<void> => {
for (const node of nodeList) {
const data: any = {
txId,
timestamp,
full_receipt,
}
crypto.signObj(data, devAccount.secretKey, devAccount.publicKey)
Expand Down
7 changes: 6 additions & 1 deletion src/Data/Collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand Down
52 changes: 9 additions & 43 deletions src/Data/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
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
}
Expand Down Expand Up @@ -907,31 +904,24 @@ export async function getCycleDuration(): Promise<number> {

/*
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<boolean> {
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<JoinStatus> => {
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) {
Expand Down Expand Up @@ -974,30 +964,6 @@ export async function getTotalDataFromArchivers(): Promise<ArchiverTotalDataResp
)) as ArchiverTotalDataResponse | null
}

export async function checkJoinStatusFromConsensor(nodeList: NodeList.ConsensusNodeInfo[]): Promise<boolean> {
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<void> {
let complete = false
let startAccount = 0
Expand Down Expand Up @@ -1632,7 +1598,7 @@ export const syncOriginalTxsByCycle = async (
return
}
totalCycles = response.totalCycles
totalOriginalTxs = response.totalReceipts
totalOriginalTxs = response.totalOriginalTxs
}
const complete = false
let startCycle = lastStoredOriginalTxCycle
Expand Down
3 changes: 1 addition & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,13 @@ async function start(): Promise<void> {
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)
Expand Down

0 comments on commit a9c1159

Please sign in to comment.