Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This simple double ended iterator based design of UsageTracker has served us pretty well for a long time. We would push to the front a new usage hour and pop old ones off the back.
Sadly this format has proven to not be very robust to errors, if for any reason the usage or payments list was to become out of order invalid duplicate data would exist leading to confusing questions about which one to select.
We saw this most recently with payments where the make_payments_v2 change caused duplicate payments and incorrect income data to pollute the databases of many routers. That issue was patched with a duplicate check but in this new structure duplicates of payments in the payment list and duplicates of data usage in the data usage list are simply not possible.
By storing the usage data as a hashmap indexed by unix timestamp we will always retrieve a copy of the usage data for a given hour if we request it and only one version of any hour can exist.
Likewise for payments we've moved to a flat HashSet where the Hash value for the payment struct is the txid, this means that duplicate payments with the same txid can not co-exist in the hashset.
This new more robust format will be consumed by operator tools as well making it easier to manage usage data for many routers in the database there.