substrate-api-client icon indicating copy to clipboard operation
substrate-api-client copied to clipboard

Software Design: Introduce nonce caching

Open haerdib opened this issue 2 years ago • 4 comments

Currently, every compose_extrinsic! call, calls the substrate storage. There's already a workaround for this by using ccompose_extrinsic_offline!, but it sure would be nice, to have a nonce cache that caches the nonce, and updates it with a storage request in case of a failure (e.g. mark it as dirty or something).

Complexity: When to revert nonce? What about future?

haerdib avatar Nov 14 '22 14:11 haerdib

Maybe in a first version we can introduce a NonceProvider trait that abstracts over getting the nonce and provide two simple implementations:

  • LocalNonceProvider: Fetches the nonce only once and increases it automatically, but does not implement additional logic (fetching nonce again on error, ...). This one could also be useful for no_std where I don't have an Api instance.
  • OnlineNonceProvider: The approach currently used that always fetches the nonce from the node

This will already be some work to think about how the trait should look, how to configure this and how to pass the NonceProvider around. We could then implement fancier nonce caching later on.

Niederb avatar Apr 20 '23 06:04 Niederb

I agree in general, but before doing all this work, we should probably make sure that a local nonce cache does make sense. At least theoretically. E.g. find out if such a cache could ever be robust enough to be applied in the field. What do you think?

haerdib avatar Apr 20 '23 06:04 haerdib

Yes, I agree and I think even this "simple" approach is more involved that it seems at first glance.

Niederb avatar Apr 20 '23 07:04 Niederb

Yes. I was just thinking about the separation of creating and sending extriniscs. The creation is done without the api, while the sending is done with it. For a cache, those two elements would need to be combined somehow. Otherwise, the cache could never be reverted / updated in case something goes wrong during sending.

haerdib avatar Apr 20 '23 08:04 haerdib