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

SYS-380: Implement verifyNodeUnknown test #248

Draft
wants to merge 10 commits into
base: dev
Choose a base branch
from
388 changes: 44 additions & 344 deletions src/p2p/Join/index.ts

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions src/p2p/Join/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Logger } from 'log4js'
import { logger } from '../Context'

export function initLogging(): void {
p2pLogger = logger.getLogger('p2p')
}

let p2pLogger: Logger
export function getP2PLogger(): Logger {
return p2pLogger
}

export function info(...msg: string[]): void {
const entry = `Join: ${msg.join(' ')}`
getP2PLogger().info(entry)
}

export function warn(...msg: string[]): void {
const entry = `Join: ${msg.join(' ')}`
getP2PLogger().warn(entry)
}

export function error(...msg: string[]): void {
const entry = `Join: ${msg.join(' ')}`
getP2PLogger().error(entry)
}
46 changes: 29 additions & 17 deletions src/p2p/Join/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@
import {
addJoinRequest,
computeSelectionNum,
getAllowBogon,
setAllowBogon,
validateJoinRequest,
verifyJoinRequestSignature,
warn,
queueStandbyRefreshRequest,
queueJoinRequest,
queueUnjoinRequest,
verifyJoinRequestTypes,
nodeListFromStates
nodeListFromStates,
} from '.'
import { config } from '../Context'
import { isBogonIP } from '../../utils/functions/checkIP'
Expand All @@ -43,7 +38,9 @@
import { addStandbyRefresh } from './v2/standbyRefresh'
import { Utils } from '@shardus/types'
import { testFailChance } from '../../utils'
import { shardusGetTime } from '../../network'
import { validateJoinRequest } from './validate'
import { warn } from './logging'
import { getAllowBogon, setAllowBogon } from './state'

const cycleMarkerRoute: P2P.P2PTypes.Route<Handler> = {
method: 'GET',
Expand Down Expand Up @@ -341,7 +338,7 @@
const acceptedRoute: P2P.P2PTypes.Route<Handler> = {
method: 'POST',
name: 'accepted',
handler: async (req, res) => {

Check warning on line 341 in src/p2p/Join/routes.ts

View workflow job for this annotation

GitHub Actions / ci / QA merge checks

'req' is defined but never used

Check warning on line 341 in src/p2p/Join/routes.ts

View workflow job for this annotation

GitHub Actions / ci / QA merge checks

'res' is defined but never used
// Turns out the cycle check is unnecessary because the joining node will robust query for its node ID
// The joinNetwork fn in startupV2 will handle acceptance
acceptance.getEventEmitter().emit('accepted')
Expand Down Expand Up @@ -444,14 +441,14 @@
* Part of Join Protocol v2. Gossips all valid join requests.
*/
const gossipValidJoinRequests: P2P.P2PTypes.GossipHandler<
{
joinRequest: P2P.JoinTypes.JoinRequest,
sign: P2P.P2PTypes.Signature
},
{
joinRequest: P2P.JoinTypes.JoinRequest
sign: P2P.P2PTypes.Signature
},
P2P.NodeListTypes.Node['id']
> = (
payload: {
joinRequest: P2P.JoinTypes.JoinRequest,
joinRequest: P2P.JoinTypes.JoinRequest
sign: P2P.P2PTypes.Signature
},
sender: P2P.NodeListTypes.Node['id'],
Expand Down Expand Up @@ -541,10 +538,17 @@
sender: P2P.NodeListTypes.Node['id'],
tracker: string
) => {
if(!checkGossipPayload(payload, {
publicKey: 's',
sign: 'o',
}, 'gossip-unjoin', sender)) {
if (
!checkGossipPayload(
payload,
{
publicKey: 's',
sign: 'o',
},
'gossip-unjoin',
sender
)
) {
return
}

Expand Down Expand Up @@ -724,7 +728,15 @@
}

export const routes = {
external: [cycleMarkerRoute, joinRoute, joinedRoute, joinedV2Route, acceptedRoute, unjoinRoute, standbyRefreshRoute],
external: [
cycleMarkerRoute,
joinRoute,
joinedRoute,
joinedV2Route,
acceptedRoute,
unjoinRoute,
standbyRefreshRoute,
],
gossip: {
'gossip-join': gossipJoinRoute,
'gossip-valid-join-requests': gossipValidJoinRequests,
Expand Down
17 changes: 17 additions & 0 deletions src/p2p/Join/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { P2P } from '@shardus/types'

let seen: Set<P2P.P2PTypes.Node['publicKey']>
export function getSeen(): Set<string> {
return seen
}
export function resetSeen(): void {
seen = new Set()
}

let allowBogon = false
export function setAllowBogon(value: boolean): void {
allowBogon = value
}
export function getAllowBogon(): boolean {
return allowBogon
}
10 changes: 10 additions & 0 deletions src/p2p/Join/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface JoinRequestResponse {
/** Whether the join request was accepted. TODO: consider renaming to `accepted`? */
success: boolean

/** A message explaining the result of the join request. */
reason: string

/** Whether the join request could not be accepted due to some error, usually in validating a join request. TODO: consider renaming to `invalid`? */
fatal: boolean
}
Empty file added src/p2p/Join/utils.ts
Empty file.
Loading
Loading