cacheMultiaddr method

MultiAddr cacheMultiaddr(
  1. MultiAddr? addr
)

Implementation

MultiAddr cacheMultiaddr(MultiAddr? addr) {
  if (addr == null) {
    return observedTWAddr;
  }

  final addrStr = String.fromCharCodes(addr.toBytes());
  if (_cachedMultiaddrs.containsKey(addrStr)) {
    return _cachedMultiaddrs[addrStr]!;
  }

  if (_cachedMultiaddrs.length == observerSetCacheSize) {
    // Remove one entry if we will go over the limit
    _cachedMultiaddrs.remove(_cachedMultiaddrs.keys.first);
  }

  // Extract the components from the addr parameter
  final addrComponents = addr.components.map((c) => "/${c.$1.name}/${c.$2}").join("");

  // Create a new Multiaddr by combining the observed thin waist address with the components from addr
  final result = MultiAddr(observedTWAddr.toString() + addrComponents);
  _cachedMultiaddrs[addrStr] = result;
  return result;
}