mergeAddrInfos static method

AddrInfo? mergeAddrInfos(
  1. AddrInfo prevAi,
  2. AddrInfo newAi
)

Creates a new AddrInfo by merging the addresses of two AddrInfo objects Returns null if no new addresses were added

Implementation

static AddrInfo? mergeAddrInfos(AddrInfo prevAi, AddrInfo newAi) {
  if (prevAi.id != newAi.id) {
    throw ArgumentError('Cannot merge AddrInfo with different peer IDs');
  }

  final seen = <String>{};
  final combinedAddrs = <MultiAddr>[];

  void addAddrs(List<MultiAddr> addrs) {
    for (final addr in addrs) {
      final addrStr = addr.toString();
      if (seen.contains(addrStr)) {
        continue;
      }
      seen.add(addrStr);
      combinedAddrs.add(addr);
    }
  }

  addAddrs(prevAi.addrs);
  addAddrs(newAi.addrs);

  if (combinedAddrs.length > prevAi.addrs.length) {
    return AddrInfo(prevAi.id, combinedAddrs);
  }
  return null;
}