Repository navigation
Description
Seeking feedback on whether abstracting away ChainstateManager (and Chainstate) in net_processing is worthwhile. This was previously discussed in #27502. Using a wrapper class, here's where I am currently:
net_processing.hdefines an abstract classNetValidationInterface(name TODO)net_processing.cppdefines a derived classChainManAdapterthat takesChainstateManager&and wraps calls to it. This hides members likem_blockman,m_best_header, etc.PeerManagerImplstores astd::unique_ptr<NetValidationInterface>that is nevernullptrand replaces all previous calls tom_chainman.
With this, it becomes possible to reach most (all?) branches gated on m_chainman in net_processing when fuzzing without a TestingSetup. For example, this lets us toggle IsPruneMode, mock reorgs easier, and report the full set of BlockValidationState / MempoolAcceptResult. This should give speed and coverage wins (determinism too?). The same approach could be applied to m_mempool, though I want to see if the general approach is acceptable.
Questions:
- My rough draft diff to
net_processing.his ~+81, tonet_processing.cppis ~+268,-130. Is the benefit worth the cost? - Is there a clearer design pattern that works?
- Could the interface be somewhere else and more general so other callers may use it?
- Is there ongoing work in kernel that can address the same problem?
- Is there a way to also reduce
cs_mainlocking at the same time? - Abstracting away
m_chainmanbut notm_mempoolis a bit weird for a fuzz test since the mempool will always be empty yet net_processing may think that a transaction should exist in the mempool based onProcessTransaction. Should we abstract both?
Rough branch that does this (helped by Claude, though I re-wrote it). The first commit introduces + uses the interface in net_processing, the second is a copy of the process_messages harness that uses FuzzedNetValidation instead of TestingSetup. The interface is a bit weird currently: it has a GetMutex function and it hacks around ProcessNewPackage being a free function accepting a Chainstate. The fuzz test is sparse because I don't know all invariants that need to hold while mocking to avoid a crash.
Inspired by #27502 (comment).
Activity
sedited commentedon Jun 3, 2026
I'm very much in favour of having some form of interface between net processing and validation. I briefly checked what the overlap is between the
ChainstateMaanger's public member functions and the member functions you define in theNetValidationInterface. It seems like most functions not called by the interface fall in roughly two categories: Initializers for the chainstate, and functions that allow aChainstateto reach back to theChainstateManager. For your goal of mocking validation the former can just be ignored, but the latter may be a bit annoying to deal with. I've been working on eliminating this circular dependency between theChainstateandChainstateManagerhere (didn't quite finish it, and needs a bit of rebasing again). I think this would already get us a good ways closer to having this interface be more or less the public member functions of theChainstateManager.Instead of having an adapter/proxy class, could the chainman just inherit from this interface class and override its methods? I could see that making it a bit easier down the line to also intercept and schedule calls to the chainman, i.e. to prioritize data processing over data queries. Thinking even further this could also make splitting up the net processing and validation responsibilities of cs_main much easier by just having to deal with and take into account a subset of validation functions.
Crypt-iQ commentedon Jun 4, 2026
Thanks for the feedback @sedited
The circular dependency definitely threw me off when looking at the code, it's not immediately clear to me how it affects mocking? Can you explain why -- is it because the goal should be to have the public member functions be the interface? If so, that makes sense and then this work would be a prerequisite for an interface.
This also works and I think makes more sense to me than having net_processing coerce chainman into a specific interface. My questions here are:
validation.hor innode/interfaces?Just curious, do you think this would be done by modifying one of the chainman's overridden methods or something else?
This seems like a nice win independent of mocking.
sedited commentedon Jun 4, 2026
Yes, that was what I was getting at. I'm not sure if this really makes sense as a true prerequisite, but I think that is what we should strive for architecturally.
I think if this is baked into the chainman, it could make sense to put it in the kernel/ subdirectory for now.
I think besides just stubbing out all the unwanted fnuctions, we don't have a clean solution for that anyway. An example of thi is the
CCoinsViewEmptyclass that just contains stubs. My impression is that while this might be a bit clumsy to deal with sometimes, it is also not a particularly evil or dangerous pattern.I was more thinking of implementing a dispatcher proxy that also inherits from the same interface and then gets wired to the chainman. In this case, net processing would call this dispatcher that then calls to the actual chainman.
Crypt-iQ commentedon Jun 5, 2026
I might have been unclear, by this I meant that removing the circular dependency seemed to me to be a prerequisite, and not that the public member functions needed to match the interface exactly. The interface could be written right now, but I think it's probably premature -- I think once the circular dependency is removed, then hopefully we'd have a better idea of some of the other public member functions that could be pruned away. There is some weirdness in my code where I was forced to call chainstate & blockman functions through chainman, but it's not clear to me how to fix that.
This was the only downside I can think of, which doesn't seem like a big deal.
Gotcha, makes sense. @ajtowns mentioned to me out-of-band his comment that reminds me of this. I haven't thought too much about it. It would be cool if the interface allowed more customization for net_processing.
More generally, I like having the chainman inherit from this interface, pruning away the member functions if possible, and I like removing the circular dependency between chainman and chainstate. I'd like to get broader buy-in from other folks, but this seems like a good approach and I can't think of any downsides (other than the review burden perhaps)?
Olorunshogo commentedon Aug 10, 2026
I've been following this discussion and I'm interested in the fuzzing aspect. How do you see the interface boundary being determined in practice?
Would the intention be to start from all of the current
m_chainmancalls innet_processingand then progressively shrink that set as theChainstate/ChainstateManagerseparation improves, or is there a higher-level validation interface already being considered?I'm interested in digging into this and potentially helping with the dependency analysis.
Crypt-iQ commentedon Aug 10, 2026
I haven't taken a stab at implementing this, so there may some hidden gotchas that aren't obvious from reading the code. I think it makes sense to start with reducing the circular dependency between
Chainstate&ChainstateManagerand once that's done, makeChainstateinherit from this kernel/ interface. It would be nice (as @sedited mentioned above) if theChainstatemember functions basically matched this interface. Then them_chainmancalls innet_processingcan simply be replaced by an interface that we can mock when fuzzing. I think this also means that calls to read the block index throughm_chainmanwould need to be part of the interface and perhaps a special carve-out forProcessNewPackageas that function is also odd for this kind of design.Edit: It is also totally possible to start with the interface and then iterate to get just what
net_processing(and other components) need