Repository: shardeum/shardeum
File: src/index.ts (TypeScript)

Code before:
      if (errors !== null) {
        nestedCountersInstance.countEvent('external', 'ajv-failed-query-certificate')
        res.json({
          success: false,
          reason: 'Invalid request body',
          details: isDebugMode() ? errors : null,
          status: 400,
        })
        return
      }
      // if timestamp is a float, round it down to nearest millisecond
      tx.timestamp = Math.floor(tx.timestamp)
      const appData = null
      const id = shardus.getNodeId()
      const isInRotationBonds = shardus.isNodeInRotationBounds(id)
      if (isInRotationBonds) {
        res.json({
          success: false,
          reason: `Node is too close to rotation edges. Inject to another node`,
          status: 500,
        })
        return
      }

      // Find IP of request sender
      let ipAddress: string | undefined = req.headers['x-forwarded-for'] as string || req.socket.remoteAddress;
      if (ipAddress && ipAddress.includes(',')) {
        ipAddress = ipAddress.split(',')[0].trim(); // Take the first IP in case of multiple
      }

      await handleInject(tx, appData, res, ipAddress)
    } catch (error) {
      /* prettier-ignore */ if (logFlags.error) console.error('Error in inject endpoint:', error)
      res.json({ error: 'Internal Server Error' })
    }
  })

  async function handleInject(tx, appData, res, ipAddress?: string): Promise<void> {
    if (ShardeumFlags.VerboseLogs) console.log('Transaction injected:', new Date(), tx)

    const nodeId = shardus.getNodeId()
    const node = shardus.getNode(nodeId)

    if (!node) {
      nestedCountersInstance.countEvent('shardeum', `txRejectedDueToNodeNotFound`)
      res.json({
        success: false,
        reason: `Node not found. Rejecting inject`,
        status: 500,
      })
      return

Review comment (suggestion):
please change this to : 
`let ip = req.ip || req.socket.remoteAddress;`
and remove the logic below : 

```
      if (ipAddress && ipAddress.includes(',')) {
        ipAddress = ipAddress.split(',')[0].trim(); // Take the first IP in case of multiple
      }
```

Code after:
      if (errors !== null) {
        nestedCountersInstance.countEvent('external', 'ajv-failed-query-certificate')
        res.json({
          success: false,
          reason: 'Invalid request body',
          details: isDebugMode() ? errors : null,
          status: 400,
        })
        return
      }
      // if timestamp is a float, round it down to nearest millisecond
      tx.timestamp = Math.floor(tx.timestamp)
      const appData = null
      const id = shardus.getNodeId()
      const isInRotationBonds = shardus.isNodeInRotationBounds(id)
      if (isInRotationBonds) {
        res.json({
          success: false,
          reason: `Node is too close to rotation edges. Inject to another node`,
          status: 500,
        })
        return
      }

      // Find IP of request sender
      const ipAddress: string | undefined = req.ip || req.socket.remoteAddress;

      await handleInject(tx, appData, res, ipAddress)
    } catch (error) {
      /* prettier-ignore */ if (logFlags.error) console.error('Error in inject endpoint:', error)
      res.json({ error: 'Internal Server Error' })
    }
  })

  async function handleInject(tx, appData, res, ipAddress?: string): Promise<void> {
    if (ShardeumFlags.VerboseLogs) console.log('Transaction injected:', new Date(), tx)

    const nodeId = shardus.getNodeId()
    const node = shardus.getNode(nodeId)

    if (!node) {
      nestedCountersInstance.countEvent('shardeum', `txRejectedDueToNodeNotFound`)
      res.json({
        success: false,
        reason: `Node not found. Rejecting inject`,
        status: 500,
      })
      return
    }
    if (node.status !== P2P.P2PTypes.NodeStatus.ACTIVE) {
      res.json({