Skip to content

Commit

Permalink
Add shardeum version check for seed node in restore network
Browse files Browse the repository at this point in the history
  • Loading branch information
tanuj-shardeum committed Aug 13, 2024
1 parent d1b989b commit e1259ab
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,17 @@ export function registerRoutes(server: FastifyInstance<Server, IncomingMessage,
* CZ adds AZ's join reqeuest to cycle zero and sets AZ as cycleRecipient
*/
type NodeListRequest = FastifyRequest<{
Body: P2P.FirstNodeInfo & Crypto.SignedMessage
Body: {
nodeInfo: P2P.FirstNodeInfo & Crypto.SignedMessage
appJoinData: {
shardeumVersion: string
minVersion: string
activeVersion: string
latestVersion: string
operatorCLIVersion: string
operatorGUIVersion: string
}
}
}>

server.get('/myip', function (request, reply) {
Expand All @@ -69,9 +79,10 @@ export function registerRoutes(server: FastifyInstance<Server, IncomingMessage,
})

server.post('/nodelist', (request: NodeListRequest, reply) => {
console.log(':>> nodeList request', request.body)

Check warning

Code scanning / CodeQL

Log injection Medium

Log entry depends on a
user-provided value
.
profilerInstance.profileSectionStart('POST_nodelist')
nestedCountersInstance.countEvent('consensor', 'POST_nodelist', 1)
const signedFirstNodeInfo = request.body
const signedFirstNodeInfo = request.body.nodeInfo

if (State.isFirst && NodeList.isEmpty() && !NodeList.foundFirstNode) {
try {
Expand Down Expand Up @@ -102,6 +113,12 @@ export function registerRoutes(server: FastifyInstance<Server, IncomingMessage,
publicKey,
}

// const networkAccount: AccountDB.AccountCopy | string = getGlobalNetworkAccount(false)
// if(typeof networkAccount !== 'string' && !NodeList.isValidVersion(networkAccount?.data?.minVersion, networkAccount?.data?.latestVersion, signedFirstNodeInfo.nodeInfo.version)) {
// Logger.mainLogger.error('Invalid version', signedFirstNodeInfo.nodeInfo.version)
// reply.send({ success: false, error: 'Invalid version' })
// }

Data.initSocketClient(firstNode)

// Add first node to NodeList
Expand Down
2 changes: 1 addition & 1 deletion src/GlobalAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface GlobalAccountsHashAndTimestamp {
export const globalAccountsMap = new Map<string, GlobalAccountsHashAndTimestamp>()
const appliedConfigChanges = new Set<string>()

export function getGlobalNetworkAccount(hash: boolean): object | string {
export function getGlobalNetworkAccount(hash: boolean): AccountDB.AccountCopy | string {
if (hash) {
return cachedGlobalNetworkAccountHash
}
Expand Down
28 changes: 28 additions & 0 deletions src/NodeList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,31 @@ export function toggleFirstNode(): void {
foundFirstNode = !foundFirstNode
Logger.mainLogger.debug('foundFirstNode', foundFirstNode)
}

export function isEqualOrNewerVersion(minimumVersion: string, testVersion: string): boolean {
if (minimumVersion === testVersion) {
return true
}

const minVerParts = minimumVersion.split('.')
const testVerParts = testVersion.split('.')
/* eslint-disable security/detect-object-injection */
for (let i = 0; i < testVerParts.length; i++) {
const testV = ~~testVerParts[i] // parse int
const minV = ~~minVerParts[i] // parse int
if (testV > minV) return true
if (testV < minV) return false
}
/* eslint-enable security/detect-object-injection */
return false
}

export function isEqualOrOlderVersion(maximumVersion: string, testVersion: string): boolean {
return isEqualOrNewerVersion(testVersion, maximumVersion)
}

export function isValidVersion(minimumVersion: string, latestVersion: string, testVersion: string): boolean {
const equalOrNewer = isEqualOrNewerVersion(minimumVersion, testVersion)
const equalOrOlder = isEqualOrOlderVersion(latestVersion, testVersion)
return equalOrNewer && equalOrOlder
}
8 changes: 8 additions & 0 deletions src/P2P.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ export interface FirstNodeInfo {
externalPort: number
publicKey: string
}
appJoinData: {
shardeumVersion: string
minVersion: string
activeVersion: string
latestVersion: string
operatorCLIVersion: string
operatorGUIVersion: string
}
}
export interface FirstNodeResponse {
nodeList: NodeList.ConsensusNodeInfo[]
Expand Down

0 comments on commit e1259ab

Please sign in to comment.