ChainSafe / discv5

A Typescript implementation of the Discv5 protocol
Apache License 2.0
28 stars 15 forks source link

ENR API is error-prone #240

Closed wemeetagain closed 1 year ago

wemeetagain commented 1 year ago

ENR API and types are error prone since it handles both mutable and immutable ENRs, and mixing these behaviors makes the types lax and adds confusing behavior. Eg: encode takes an optional private key. Encoding can fail if the ENR has been mutated and not resigned. Etc.

A better design would prevent error cases that currently have to be handled.

eg:

class ImmutableENR {
  set(..) {
    throw
  }
  delete(..) {
    throw
  }
  encode(): Uint8Array {
    // can't throw
  }
}

class MutableENR {
  privateKey: Uint8Array;
  encode(): Uint8Array {
    // can't throw (except in truly exceptional case)
    sign(..., this.privateKey);
    ...
  }