It seems that the IPC mining interface needs to provide some way to cancel BlockTemplate::waitNext calls. This should not be hard to implement, but there are a few approaches that could be taken and it would be useful to know which would work best for callers. Context:
Originally posted by @plebhash in #33554:
the thing is, I still need to cater for incoming Sv2
CoinbaseOutputConstraintsmessages, and whenever that arrives, I need to make sure theBlockTemplate(orTemplateIpcClientas I’m aliasing on the rust code) that’s used to kickstart the nextwaitNextloops has the correctblock_reserved_weightandmax_additional_sigopsas defined on the Sv2 message.
Different ways I could think of doing this:
- Have
BlockTemplatedestructor cancel thewaitNextcall and have it return null right away. - Add a new
BlockTemplate::interruptWait()to be able to interruptwaitNext()without destroying the template. - Make
Mining::createNewBlock()cancel any pendingwaitNext()calls. - Use Cap’n Proto’s cancellation mechanism. Rust code could just drop the waitNext() promise. C++
waitNext()method could have a newinterfaces::Cancelargument that would let it ask if the request was cancelled and/or register a cancellation callback.
If main use-cases for cancelling waitNext methods are when needing to specify changed BlockCreateOptions, and when shutting down, approach (1) could be sufficient, because in both of these cases the client will be discarding the BlockTemplate anyway.
But maybe a new BlockTemplate::interruptWait() method (2) could be useful for other reasons and we should add that method too?
Approach (3) making other IPC calls implicitly cancel pending waitNext() calls, seems like it could make mining interface more annoying to use, so I think it is probably not a good idea, but wanted to mention it as an alternative.
Approach (4) could be interesting, and would be the most general approach but would be more work than other approaches to implement. (The implementation would need to modify the mp.Context PassField implementation to call Promise::attach on the promise it returns to be notified when the promise is discarded, as described https://capnproto.org/cxxrpc.html#cancellation and https://github.com/capnproto/capnproto/blob/v2/kjdoc/tour.md#cancellation)