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

SHARD-1223: Fix nodes not being able to submit unjoin requests #354

Merged
merged 2 commits into from
Dec 30, 2024
Merged
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
32 changes: 16 additions & 16 deletions src/p2p/Join/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,22 +254,22 @@ const unjoinRoute: P2P.P2PTypes.Route<Handler> = {
method: 'POST',
name: 'unjoin',
handler: (req, res) => {
const unjoinRequest = req.body
const processResult = processNewUnjoinRequest(unjoinRequest)
if (processResult.isErr()) {
res.status(500).json({ error: processResult.error })
return
}

// we need to remove the unjoin request this cycle since we are waiting until next cycle to gossip it to the network
// processNewUnjoinRequest automatically adds the unjoin request to newUnjoinRequests it will be added back to our list
// once we process the queued request next cycle
removeUnjoinRequest(unjoinRequest.publicKey)

queueUnjoinRequest(unjoinRequest)

res.status(200).json()
return
try {
const unjoinRequest = req.body

const processResult = processNewUnjoinRequest(unjoinRequest)
if (processResult.isErr()) {
res.status(500).json({ error: processResult.error.message })
return
}

removeUnjoinRequest(unjoinRequest.publicKey)
queueUnjoinRequest(unjoinRequest)

res.status(200).json({ message: 'Unjoin request processed successfully' })
} catch (error) {
res.status(500).json({ error: error.message || 'An unknown error occurred' })
}
},
}

Expand Down
28 changes: 16 additions & 12 deletions src/p2p/Join/v2/unjoin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { crypto } from '../../Context'
import { err, ok, Result } from 'neverthrow'
import { hexstring } from '@shardus/types'
import { hexstring, P2P } from '@shardus/types'
import * as utils from '../../../utils'
import * as http from '../../../http'
import * as NodeList from '../../NodeList'
Expand All @@ -9,6 +9,7 @@ import { getActiveNodesFromArchiver, getRandomAvailableArchiver } from '../../Ut
import { logFlags } from '../../../logger'
import * as CycleChain from '../../CycleChain'
import { SignedUnjoinRequest } from '@shardus/types/build/src/p2p/JoinTypes'
import { getPublicNodeInfo } from '../../Self'

/** A Set of new public keys of nodes that have submitted unjoin requests. */
const newUnjoinRequests: Set<SignedUnjoinRequest> = new Set()
Expand All @@ -19,20 +20,10 @@ const newUnjoinRequests: Set<SignedUnjoinRequest> = new Set()
export async function submitUnjoin(): Promise<Result<void, Error>> {
const publicKey = crypto.keypair.publicKey

const foundInStandbyNodes = getStandbyNodesInfoMap().has(publicKey)
if (!foundInStandbyNodes) {
if (getPublicNodeInfo(true).status !== P2P.P2PTypes.NodeStatus.STANDBY) {
return err(new Error('node is not in standby. Do not send unjoin request'))
}

if(!CycleChain.getNewest()) {
return err(new Error('No cycle chain found. Do not send unjoin request'))
}

const unjoinRequest = crypto.sign({
publicKey: publicKey,
cycleNumber: CycleChain.getNewest().counter,
})

const archiver = getRandomAvailableArchiver()
try {
const activeNodesResult = await getActiveNodesFromArchiver(archiver)
Expand All @@ -47,6 +38,19 @@ export async function submitUnjoin(): Promise<Result<void, Error>> {
// not being properly removed isn't that big a deal. Standby refresh will take care of them anyways.
// If we really want to solve this, can do so by sending request to numRotatedOut + 1 nodes.
const node = utils.getRandom(activeNodes.nodeList, 1)[0]
const cycleRecord: P2P.CycleCreatorTypes.CycleRecord = await http.get(
`${node.ip}:${node.port}/newest-cycle-record`
)

if (!cycleRecord?.counter) {
return err(new Error('No cycle counter found. Do not send unjoin request'))
}

const unjoinRequest = crypto.sign({
publicKey: publicKey,
cycleNumber: cycleRecord.counter,
})

await http.post(`${node.ip}:${node.port}/unjoin`, unjoinRequest)
return ok(void 0)
} catch (e) {
Expand Down
Loading