mpgen: Allow wrapped interfaces to inherit from unwrapped interfaces #377

pull ryanofsky wants to merge 1 commits into bitcoin-core:master from ryanofsky:pr/methodkey changing 8 files +69 −24
  1. ryanofsky commented at 1:45 AM on October 15, 2026: collaborator

    Problem: Currently, interface inheritance in libmultiprocess only works if capnproto base and derived interfaces both use $Proxy.wrap annotations and have C++ base and derived classes implementing methods of both interfaces. It is currently not possible to have derived capnproto interface that uses $Proxy.wrap inherit from a base interface that is unwrapped. This is needed for the stable/unstable Chain interface split proposed in bitcoin/bitcoin#34981. This limitation exists because ProxyMethod template class which is responsible for mapping capnproto method types to c++ method pointers currently assumes that that every capnproto method will be implemented by the wrapped base interface, and doesn't allow them to be implemented be derived classes.

    Solution: Pass an interface type template parameter to ProxyMethod and to the ProxyMethodTraits, ProxyClientMethodTraits and ProxyServerMethodTraits traits classes, and have mpgen generate ProxyMethod specializations for every method an interface handles, including inherited ones. Small change to proxy.h, proxy-types.h and gen.cpp, plus a test of an interface inheriting from a base interface with no C++ class.

    This is a potentially breaking change for code that specializes ProxyClientMethodTraits or ProxyServerMethodTraits, which now takes the interface type as its first template argument. But Bitcoin Core master does not have any of these specializations currently, so it needs no changes. Generated code is otherwise equivalent and there are no wire protocol changes.

  2. mpgen: Key ProxyMethod and method traits by interface type
    Add an Interface template parameter to ProxyMethod, ProxyMethodTraits,
    ProxyClientMethodTraits and ProxyServerMethodTraits, and generate ProxyMethod
    specializations for inherited methods as well as each interface's own methods.
    
    This lets a wrapped capnp interface inherit methods from a base interface
    that is wrapped by a different C++ class, or not wrapped at all. Previously
    the mapping from an inherited method to a C++ method came from the base
    interface's generated ProxyMethod<Params> specialization, so it only worked
    when the base interface was wrapped by a C++ base class of the derived
    interface's class. Each method could only have one mapping, so two wrapped
    interfaces inheriting from the same unwrapped base could not each supply one.
    
    Bitcoin Core needs this to split its Chain interface into stable Chain and
    UnstableChain capnp interfaces wrapping the same interfaces::Chain class
    (bitcoin/bitcoin#34981).
    
    This is a breaking change for code specializing ProxyClientMethodTraits or
    ProxyServerMethodTraits, which now takes the interface type as its first
    template argument.
    
    This change was written with Claude Opus 5.5 (1M context).
    08f72b0a2b
  3. DrahtBot commented at 1:45 AM on October 15, 2026: none

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK enochaz, xyzconstant

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #342 (Allow request cancellation for wrapped C++ methods by xyzconstant)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. in include/mp/proxy.h:30 in 08f72b0a2b
      25 | @@ -26,8 +26,11 @@ template <typename Interface> struct ProxyClient; // IWYU pragma: export
      26 |  //! Mapping from capnp interface type to proxy server implementation (specializations are generated by
      27 |  //! proxy-codegen.cpp).
      28 |  template <typename Interface> struct ProxyServer; // IWYU pragma: export
      29 | -//! Mapping from capnp method params type to method traits (specializations are generated by proxy-codegen.cpp).
      30 | -template <typename Params> struct ProxyMethod; // IWYU pragma: export
      31 | +//! Mapping from capnp interface and method params types to method traits
      32 | +//! (specializations are generated by proxy-codegen.cpp). The interface type is
    


    xyzconstant commented at 1:40 AM on October 28, 2026:

    Nit. Feel free to ignore:

    proxy-codegen.cpp is stale, and it appears multiple times outside the changes this PR touches, so we may need a separate commit to replace every instance with either mpgen or gen.cpp.

  5. in src/mp/gen.cpp:550 in 08f72b0a2b
     546 | +                // Generate a ProxyMethod specialization for every method,
     547 | +                // including inherited methods, so each wrapped interface maps
     548 | +                // the methods it handles to its own C++ class. The interface
     549 | +                // declaring an inherited method may be wrapped by a different
     550 | +                // C++ class, or not be wrapped at all.
     551 | +                if (!is_construct && !is_destroy) {
    


    enochaz commented at 12:05 PM on November 6, 2026:

    In commit https://github.com/bitcoin-core/libmultiprocess/pull/377/changes/08f72b0a2b2cb0a9e6640a2fc0d505df33e51443: mpgen: Key ProxyMethod and method traits by interface type

    I was wondering what happens if the c++ subclass adds an overload of an inherited method.

    FooCallback already declares call(int). I added these two lines to ExtendedCallback in test/foo.h

    using FooCallback::call;
    int call(double arg) { return static_cast<int>(arg); }
    

    This makes both FooCallback::call(int) and ExtendedCallback::call(double) visible through ExtendedCallback.

    Previously, the inherited method used &FooCallback::call, which identifies call(int). The new mapping uses &ExtendedCallback::call, where both overloads are visible.

    With these lines added mptest fails to build with:

    error: unable to deduce ‘const auto’ from
    ‘& mp::test::ExtendedCallback::call’
    

    Could we select the c++ class before taking the method pointer? I worked up this possible solution in gen.cpp

    index e27b1d2..bb5f5ef 100644
    --- i/src/mp/gen.cpp
    +++ w/src/mp/gen.cpp
    @@ -551,7 +551,19 @@ static void Generate(kj::StringPtr src_prefix,
                         methods << "template<>\n";
                         methods << "struct ProxyMethod<" << message_namespace << "::" << node_name << ", " << method_prefix << "Params>\n";
                         methods << "{\n";
    -                    methods << "    static constexpr auto impl = &" << proxied_class_type
    +                    kj::StringPtr method_class_type = proxied_class_type;
    +                    kj::StringPtr declaring_class_type;
    +                    if (&method_interface != &interface &&
    +                        GetAnnotationText(method_interface.getProto(), WRAP_ANNOTATION_ID, &declaring_class_type) &&
    +                        declaring_class_type.size()) {
    +                        // Preserve the declaring C++ base's lookup when possible,
    +                        // so overloads added by the derived class do not make it ambiguous.
    +                        methods << "    using Impl = std::conditional_t<std::is_convertible_v<"
    +                                << proxied_class_type << "*, " << declaring_class_type << "*>, "
    +                                << declaring_class_type << ", " << proxied_class_type << ">;\n";
    +                        method_class_type = "Impl";
    +                    }
    +                    methods << "    static constexpr auto impl = &" << method_class_type
                                 << "::" << proxied_method_name << ";\n";
                         methods << "};\n\n";
    

    There might be a better solution to this, but the reasoning here is that, where the receiving c++ class derives publicly and unambiguously from the declaring class, we can preserve the old base lookup. Here, Impl becomes FooCallback, avoiding the additional overload.

    For unrelated c++ classes, the condition is false and we retain the receiving class’s lookup. This selection would only be emitted for inherited methods with a wrapped base and unwrapped bases keep the direct lookup.

    With the proposed fix, mptest builds with the added overload. I haven’t found a current Bitcoin Core interface affected by this, and the ambiguity doesn’t arise when the inherited and added methods have distinct c++ names. But the PR does break a previously working libmultiprocess arrangement


    ryanofsky commented at 2:29 PM on November 6, 2026:

    re: #377 (review)

    FooCallback already declares call(int). I added these two lines to ExtendedCallback in test/foo.h

    using FooCallback::call;
    int call(double arg) { return static_cast<int>(arg); }
    

    Wow this is an interesting thing to test. My first instinct is that it's good for this to trigger an error because interfaces wrapped by libmultiprocess already have a bunch of restrictions and one listed there is "Interface methods should not be overloaded" (because capnproto doesn't support method overloading, and this helps it be more language-independent).

    But maybe overloading could be good to support in a derived class, and I"m thinking in particular of cases where base class may be wrapped and exposed over IPC and derived class is an unwrapped normal c++ class inheriting from some implementation of the interface. This type of class should be free to have its own methods without the same restrictions. I'm assuming this does not work today, so would not want to add support for that here, but I think the change you suggested could make sense as a followup especially if there is a motivating use-case.


    enochaz commented at 3:42 PM on November 6, 2026:

    Thanks for pointing out that restriction. I hadn’t accounted for it when testing the wrapped ExtendedCallback case. Agreed that this doesn’t need to expand the scope of this PR.

    I also tried the unwrapped subclass case you mentioned; a normal c++ subclass inheriting from an implementation, adding a local overload, and being passed through the wrapped base interface. That builds and passes the ipc test, so that particular arrangement already appears to work.

  6. enochaz commented at 1:45 PM on November 6, 2026: contributor

    crACK 08f72b0

    Allowing wrapped interfaces to inherit from unwrapped interfaces is a useful improvement that enables the stable/unstable Chain split in https://github.com/bitcoin/bitcoin/pull/29409/changes/06588f78312dd2ab2ff87f62c6f55cf2aa37affd. The stable Chain interface can stand on its own as a protocol definition, while UnstableChain wraps the existing c++ interfaces::Chain class and handles the inherited methods, without requiring matching c++ inheritance.

    Left a suggestion below

  7. xyzconstant commented at 11:21 PM on November 16, 2026: contributor

    LGTM ACK 08f72b0


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/libmultiprocess. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-17 18:30 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me