Skip to content

Commit

Permalink
Shm 3053(2) (#10)
Browse files Browse the repository at this point in the history
* Add debugmiddleware to killroute endpoints

* we refactored killroutes to be behind debug middleware. Removed the allowKillRoute variable as its redundant now

---------

Co-authored-by: Sam Sweet <[email protected]>
  • Loading branch information
2 people authored and arhamj committed Apr 29, 2024
1 parent d688d6c commit 4cdea88
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/p2p/Lost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { RequestErrorEnum } from '../types/enum/RequestErrorEnum'
import { getStreamWithTypeCheck, requestErrorHandler } from '../types/Helpers'
import { TypeIdentifierEnum } from '../types/enum/TypeIdentifierEnum'
import { LostReportReq, deserializeLostReportReq, serializeLostReportReq } from '../types/LostReportReq'
import { isDebugModeMiddlewareHigh } from '../network/debugMiddleware'

/** TYPES */

Expand All @@ -65,9 +66,6 @@ type ScheduledRemoveNodeByApp = ScheduledRemoveByApp<P2P.NodeListTypes.Node>

/** STATE */

// [TODO] - This enables the /kill /killother debug route and should be set to false after testing
const allowKillRoute = false

let p2pLogger

let lostReported = new Map<string, P2P.LostTypes.LostReport>()
Expand All @@ -94,27 +92,33 @@ export declare type SignedPingMessage = PingMessage & SignedObject

/** ROUTES */

const killExternalRoute: P2P.P2PTypes.Route<Handler> = {
interface RouteWithAuthHandler {
authHandler: Handler
}

type RouteHandlerWithAuthHandler<T> = P2P.P2PTypes.Route<T> & RouteWithAuthHandler

/** ROUTES */

const killExternalRoute: RouteHandlerWithAuthHandler<Handler> = {
method: 'GET',
name: 'kill',
authHandler: isDebugModeMiddlewareHigh,
handler: (_req, res) => {
if (allowKillRoute) {
res.send(safeStringify({ status: 'left the network without telling any peers' }))
killSelf(
'Apoptosis being called killExternalRoute()->killSelf()->emitter.emit(`apoptosized`) at src/p2p/Lost.ts'
)
}
res.send(safeStringify({ status: 'left the network without telling any peers' }))
killSelf(
'Apoptosis being called killExternalRoute()->killSelf()->emitter.emit(`apoptosized`) at src/p2p/Lost.ts'
)
},
}

const killOtherExternalRoute: P2P.P2PTypes.Route<Handler> = {
const killOtherExternalRoute: RouteHandlerWithAuthHandler<Handler> = {
method: 'GET',
name: 'killother',
authHandler: isDebugModeMiddlewareHigh,
handler: (_req, res) => {
if (allowKillRoute) {
res.send(safeStringify({ status: 'killing another node' }))
killOther()
}
res.send(safeStringify({ status: 'killing another node' }))
killOther()
},
}

Expand Down Expand Up @@ -219,7 +223,7 @@ export function init() {
for (const route of routes.external) {
// [TODO] - Add Comms.registerExternalGet and Post that pass through to network.*
// so that we can always just use Comms.* instead of network.*
network._registerExternal(route.method, route.name, route.handler)
network._registerExternal(route.method, route.name, route.authHandler, route.handler)
}
for (const route of routes.internal) {
Comms.registerInternal(route.name, route.handler)
Expand Down Expand Up @@ -741,7 +745,7 @@ function reportLost(target, reason: string, requestId: string) {
cycle: currentCycle,
}
// [TODO] - remove the following line after testing killother
if (allowKillRoute && reason === 'killother') report.killother = true
if (reason === 'killother') report.killother = true
if (logFlags.lost) {
/* prettier-ignore */ info(`Sending investigate request. requestId: ${requestId}, reporter: ${Self.ip}:${Self.port} id: ${Self.id}`)
/* prettier-ignore */ info(`Sending investigate request. requestId: ${requestId}, checker: ${checker.internalIp}:${checker.internalPort} node details: ${logNode(checker)}`)
Expand Down Expand Up @@ -897,7 +901,7 @@ async function lostReportHandler(payload, response, sender) {
let result = await isDownCache(nodes.get(payload.target), requestId)
/* prettier-ignore */ if (logFlags.lost) console.log('lostReportHandler: result:', result)
/* prettier-ignore */ if (logFlags.lost) info(`isDownCache for requestId: ${requestId}, result ${result}`)
if (allowKillRoute && payload.killother) result = 'down'
if (payload.killother) result = 'down'
if (record.status === 'checking') record.status = result
/* prettier-ignore */ if (logFlags.lost) info(
`Status after checking for node ${payload.target} payload cycle: ${payload.cycle}, currentCycle: ${currentCycle} is ` +
Expand Down Expand Up @@ -978,7 +982,7 @@ const LostReportBinaryHandler: Route<InternalBinaryHandler<Buffer>> = {

let result = await isDownCache(nodes.get(req.target), requestId)
/* prettier-ignore */ if (logFlags.verbose) info(`isDownCache for requestId: ${requestId}, result ${result}`)
if (allowKillRoute && req.killother) result = 'down'
if (req.killother) result = 'down'
if (record.status === 'checking') record.status = result
/* prettier-ignore */ if (logFlags.verbose) info(`Status after checking for node ${req.target} payload cycle: ${req.cycle}, currentCycle: ${currentCycle} is ` + record.status)
if (!checkedLostRecordMap.has(key)) {
Expand Down

0 comments on commit 4cdea88

Please sign in to comment.