discv5 icon indicating copy to clipboard operation
discv5 copied to clipboard

`discv5.getKadValue` returns incomprehensible error when looking up own ENR

Open acolytec3 opened this issue 10 months ago • 1 comments

After having seen this sort of issue on and off when in various edge case scenarios in Ultralight where a node mistakenly queries its own routing table for its nodeId, I've finally figured out the source of this error and want to ask if there's a "best practice" solution for it. The issue is:

    discv5.getKadValue(discv5.enr.nodeId)

produces this very hard to understand error

     TypeError: Cannot read properties of undefined (reading 'getValue')
      at KademliaRoutingTable.getValue (file:///home/jim/development/discv5/packages/discv5/src/kademlia/kademlia.ts:156:23)
      at Discv5.getKadValue (file:///home/jim/development/discv5/packages/discv5/src/service/service.ts:214:30)
      at Context.<anonymous> (file:///home/jim/development/discv5/packages/discv5/test/e2e/connect.test.ts:69:21)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

The reason this error is produced is because when you compute the log2Distance for your own nodeId, the distance is 0 and so RoutingTable.bucketForId computes a bucketId of -1 and there is no bucket at that index.

It would be nice if we could either return undefined (since the ENR isn't in the routing table) or else throw an error that makes more sense like "Don't look for your own ENR in your routing table" (which might be cleaner since it really doesn't make sense to look for your own ENR in your own routing table).

Let me know if there's a preference and I'll raise a PR accordingly.

acolytec3 avatar Jan 27 '25 16:01 acolytec3

@wemeetagain thoughts here? Would love to get a bug fix for this in the next discv5 release if possible.

acolytec3 avatar Jan 27 '25 17:01 acolytec3