private ImmutableRangeMap<Integer, Capability> calculateAgreedCapabilities(
      final List<Capability> a, final List<Capability> b) {
    final List<Capability> caps = new ArrayList<>(a);
    caps.sort(CAPABILITY_COMPARATOR);
    caps.retainAll(b);

    final ImmutableRangeMap.Builder<Integer, Capability> builder = ImmutableRangeMap.builder();
    // Reserve some messages for WireProtocol
    int offset = WIRE_PROTOCOL_MESSAGE_SPACE;
    String prevProtocol = null;
    for (final Iterator<Capability> itr = caps.iterator(); itr.hasNext(); ) {
      final Capability cap = itr.next();
      final String curProtocol = cap.getName();
      if (curProtocol.equalsIgnoreCase(prevProtocol)) {
        // A later version of this protocol is already being used, so ignore this version
        continue;
      }
      prevProtocol = curProtocol;
      final SubProtocol subProtocol = subProtocols.get(cap.getName());
      final int messageSpace = subProtocol == null ? 0 : subProtocol.messageSpace(cap.getVersion());
      if (messageSpace > 0) {
        builder.put(Range.closedOpen(offset, offset + messageSpace), cap);
      }
      offset += messageSpace;
    }

    return builder.build();
  }
Given a message from a peer, determine which capability the message corresponds to and maps the
message code to the appropriate value.

@param receivedMessage The message received from a peer.
@return The interpreted message.
ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/wire/CapabilityMultiplexer.java