-
Notifications
You must be signed in to change notification settings - Fork 99
Description
I think we can simplify our ntx component by adding a Sqlite database to store the current state. This is desired in the long term to reduce store IO in any case, but I actually think this will simplify our state management dramatically.
Our data use already follows a relational pattern which will map naturally to sql database queries.
Proposal
The database will hold the latest committed network account states and all unconsumed network notes. In addition, the database will also contain all uncommitted network account deltas and notes/nullifiers aka it will also track inflight network stuff.
Whether committed and inflight state should be in separate tables is tbd, and depends largely on how complex queries/joins become.
We should also store the latest chain tip header.
Start up
On startup the ntx syncs its local database from the canonical state in the store. This would comprise of:
- Purging all local inflight state left-over from the last run.
- Sync from local chain tip to store chain tip.
- We can now start the mempool subscription (and ensure our chain tip matches the mempool one).
Run time
The database would have a single writer only - the main co-ordinator thread. Each mempool event received it writes the changes to the database.
- Transaction received: add new inflight state (account delta, note nullified, note created)
- Transaction reverted: revert the delta
- Block committed: mark data as committed. Nullified notes can be pruned (or kept if we want to have a bit of history), update chain tip
Instead of passing the mempool event on, we now just need to notify actor accounts if they should wake up (if they're asleep).
Actors
Actors have a read-only database connection. They can query for available notes and decide on behaviour based on that.
When a transaction is emitted successfully, the actor should sleep until the transaction event was received (actor will be notified by writer and can check the database for the transaction/account update).
Schema
This will require a bit of design. We have committed notes & account state. And then inflight transactions linked to notes and account deltas.