Skip to content

Commit

Permalink
Send node version while joining as a first node during restart
Browse files Browse the repository at this point in the history
  • Loading branch information
tanuj-shardeum committed Aug 29, 2024
1 parent 959d7d5 commit 77f4d03
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions src/p2p/Self.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ let cyclesElapsedSinceRefresh = 0

const idErrorMessage = `id did not match the cycle record info`

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const nodeMatch = (node) =>
node.externalIp === network.ipInfo.externalIp && node.externalPort === network.ipInfo.externalPort

Expand Down Expand Up @@ -276,7 +277,10 @@ export function startupV2(): Promise<boolean> {
const resp = await Join.fetchJoinedV2(activeNodes)

info(`startupV2: resp ${Utils.safeStringify(resp)}`)
nestedCountersInstance.countEvent('p2p', `fetchJoinedV2: isOnStandbyList: ${resp.isOnStandbyList} id: ${resp.id}`)
nestedCountersInstance.countEvent(
'p2p',
`fetchJoinedV2: isOnStandbyList: ${resp.isOnStandbyList} id: ${resp.id}`
)

// note the list below is in priority order of what operation is the most important
// mainly this matters on something like our node being selected to join but also on the
Expand All @@ -293,7 +297,8 @@ export function startupV2(): Promise<boolean> {

/* prettier-ignore */ nestedCountersInstance.countEvent('p2p', `detected self as zombie ${latestCycle.counter} waiting ${Context.config.p2p.delayZombieRestartSec} before exiting`, 1)

utils.sleep(Context.config.p2p.delayZombieRestartSec * 1000 /*sec to ms*/).then(() => { //give the network a chance to see we are not active
utils.sleep(Context.config.p2p.delayZombieRestartSec * 1000 /*sec to ms*/).then(() => {
//give the network a chance to see we are not active
//TODO in the future if we are more confident in our ablility to shut down the node from functioning
// we could have a shutdown wait. (but there is a lot of surface area)
// the other method would be to request to be shut down but that is tricky and may not be possible
Expand Down Expand Up @@ -625,7 +630,7 @@ async function joinNetworkV2(activeNodes): Promise<void> {
info(`joinNetworkV2: got latest cycle :${mode}`)
const publicKey = Context.crypto.getPublicKey()

try{
try {
const isReadyToJoin = await Context.shardus.app.isReadyToJoin(latestCycle, publicKey, activeNodes, mode)
if (!isReadyToJoin) {
/* prettier-ignore */ nestedCountersInstance.countEvent('p2p', `joinNetworkV2:isReadyToJoin:false`)
Expand All @@ -634,7 +639,7 @@ async function joinNetworkV2(activeNodes): Promise<void> {
} else {
/* prettier-ignore */ nestedCountersInstance.countEvent('p2p', `joinNetworkV2:isReadyToJoin:true`)
}
} catch(ex){
} catch (ex) {
/* prettier-ignore */ nestedCountersInstance.countEvent('p2p', `joinNetworkV2:isReadyToJoin:crashed: ${ex?.message}`)
warn(`joinNetworkV2: isReadyToJoin crashed :${utils.formatErrorMessage(ex)}`)
return
Expand Down Expand Up @@ -844,7 +849,7 @@ async function checkNodeId(nodeMatch: (node: any) => boolean, selfId: string): P
if (logFlags.p2pNonFatal) info('Node passed id check')
}

async function contactArchiver(dbgContex:string): Promise<P2P.P2PTypes.Node[]> {
async function contactArchiver(dbgContex: string): Promise<P2P.P2PTypes.Node[]> {
const maxRetries = 10
let retry = maxRetries
const failArchivers: string[] = []
Expand All @@ -859,7 +864,7 @@ async function contactArchiver(dbgContex:string): Promise<P2P.P2PTypes.Node[]> {
archiver = getRandomAvailableArchiver()
info(`contactArchiver: communicate with:${archiver?.ip}`)

if (!failArchivers.includes(archiver.ip + ':' + archiver.port)){
if (!failArchivers.includes(archiver.ip + ':' + archiver.port)) {
failArchivers.push(archiver.ip + ':' + archiver.port)
}

Expand All @@ -870,14 +875,16 @@ async function contactArchiver(dbgContex:string): Promise<P2P.P2PTypes.Node[]> {
activeNodesSigned.nodeList.length === 0
) {
/* prettier-ignore */ nestedCountersInstance.countEvent('p2p', `contactArchiver: no nodes in nodelist yet. ${dbgContex}`, 1)
info(`contactArchiver: no nodes in nodelist yet, or seedlist null ${Utils.safeStringify(activeNodesSigned)}`)
info(
`contactArchiver: no nodes in nodelist yet, or seedlist null ${Utils.safeStringify(
activeNodesSigned
)}`
)
await utils.sleep(1000) // no nodes in nodelist yet so please take a breather. would be smarter to ask each archiver only once but
// but do not want to refactor that much right now
// but do not want to refactor that much right now
if (retry === 1) {
/* prettier-ignore */ nestedCountersInstance.countEvent('p2p', `contactArchiver: no nodes in nodelist yet out of retries. ${dbgContex}`, 1)
throw Error(
`contactArchiver: nodelist null or empty after ${maxRetries} retries:`
)
throw Error(`contactArchiver: nodelist null or empty after ${maxRetries} retries:`)
}
continue
}
Expand All @@ -904,7 +911,11 @@ async function contactArchiver(dbgContex:string): Promise<P2P.P2PTypes.Node[]> {

info(`contactArchiver: passed ${archiver.ip} retry:${retry}`)

info(`contactArchiver: activeNodesSigned:${Utils.safeStringify(activeNodesSigned?.joinRequest)} restartCycleRecord:${Utils.safeStringify(activeNodesSigned?.restartCycleRecord)}`)
info(
`contactArchiver: activeNodesSigned:${Utils.safeStringify(
activeNodesSigned?.joinRequest
)} restartCycleRecord:${Utils.safeStringify(activeNodesSigned?.restartCycleRecord)}`
)

const joinRequest: P2P.ArchiversTypes.Request | undefined = activeNodesSigned.joinRequest as
| P2P.ArchiversTypes.Request
Expand Down Expand Up @@ -995,14 +1006,18 @@ async function getActiveNodesFromArchiver(
archiver: ActiveNode
): Promise<P2P.P2PTypes.SignedObject<SeedNodesList>> {
const nodeInfo = getPublicNodeInfo()
const joinRequest = {
nodeInfo: nodeInfo,
appJoinData: Context.shardus.app.getNodeInfoAppData(),
}
const seedListResult: Result<
P2P.P2PTypes.SignedObject<SeedNodesList>,
Error
> = await Archivers.postToArchiver(
archiver,
'nodelist',
Context.crypto.sign({
nodeInfo,
joinRequest,
}),
10000
)
Expand Down Expand Up @@ -1157,12 +1172,12 @@ function acceptedTrigger(): Promise<void> {
* @returns
*/
export function waitForQ1SendRequests(): Promise<void> {
return new Promise(resolve => {
return new Promise((resolve) => {
const intervalId = setInterval(() => {
if (currentQuarter === 1 && q1SendRequests === true) {
clearInterval(intervalId);
resolve();
clearInterval(intervalId)
resolve()
}
}, Context.config.p2p.secondsToCheckForQ1);
});
}, Context.config.p2p.secondsToCheckForQ1)
})
}

0 comments on commit 77f4d03

Please sign in to comment.