-
Notifications
You must be signed in to change notification settings - Fork 773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mpt: Added delayed pruning for state, due to issue #3828 #3829
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool idea, though we should think through whether we are able to split the delete ops list into a set of deletes for each state transition rather than just one single list. Otherwise, we are only able to roll back to one previous state root (the one before the delete operations began).
@acolytec3 Thank you! Yes, sure. We can split the list. As I demonstrated in the last example, we can delete only some keys, not the whole list. This will give us the ability to rollback N steps back, but not more. Useful to smoothly get rid of super old state. |
Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>
packages/mpt/src/mpt.ts
Outdated
@@ -218,9 +219,10 @@ export class MerklePatriciaTrie { | |||
|
|||
// If value is empty, delete | |||
if (value === null || value.length === 0) { | |||
return this.del(key) | |||
await this.del(key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This initially returned from this method, but now it does not, so I suspect this will yield some bugs (?)
UPD: Fixed the problem with deletion of necessary nodesThanks @acolytec3 @jochem-brouwer So guys, after a series of tests I actually encountered the problem that with naive deletion we can accidentally delete nodes that will still be needed in the last states. Here's why it went unnoticed:
SolutionSo, after spending more time on research, I found 2 useful articles that helped with this:
In simple terms - by modifying the methods What was changed in the commit
Using this method - keys with a counter value of
|
Hi folk x2 👋
It's been a while since I opened issue #3828
As it turns out from the details of the Discord discussion, the current MPT implementation does not have the ability to delayed pruning of the state. Thus, all old state modifications remain in the storage.
The only option to remove (as @jochem-brouwer stated) was to walk the tree and purge nodes that are not associated with the current root. However, this is a very slow process that would not work well with a large storage.
Problem statement
We need a mechanism for delayed state pruning that:
useNodePruning=false
)For example, if we have a sequence of state changes:
S1 -> S2 -> S3 -> S4 -> S5
Then, we need functionality that:
S5
toS1
backS1
andS2
, to be deleted. At the same time, the network retains the ability to rollback fromS5
toS4
andS3
, BUT NOT TOS1
andS2
What was added/changed in the pull request
put
anddel
functions - added the functionality for tracking operations for delete batch. Simply pass an additional parametertrackPruningOps=true
to track storage pruning operations as ifuseNodePruning=true
BatchDBOp[]
like:You can save these keys somewhere (in some 3rd party KV database) and use them later for delayed pruning.
The
del()
andput()
function signatures now look like this:which takes an array of keys to delete as input. This function can be used to prune the database
Demo
Let's see how it works:
1. Adding accounts for Alice, Bob, Charlie
Output:
2. Check the ops for pruning (to use it later)
As you have seen, from the
put()
function we get delete operations. So, currently our DB (withuseNodePruning=false
) contains:Total keys: 11
If we make
useNodePruning=true
, then:Total keys: 5
3. Make pruning
Let's delete it using the
delPrevStatesData()
function:Output:
Also, if we check the total keys in state we'll see:
Summary
4. What about access to some previous states, not to the latest
full pruning
. That is, there is access only to the latest stateIn our example, let's try deleting data that relates to the state when the storage only contained data about Alice and Bob.
That is:
S1
- empty dbS2
- there is Alice's accountS3
- there is Bob's accountS4
- there is Charlie's accountS5
- Charlie's account has been updatedLet's delete
S1
,S2
,S3
. This way, we can still rollback to stateS4
and get Charlie's old data.Output:
As you see - we still has access to states
S5
andS4
but not to statesS1-S3
If we check the total keys number in db:
Total keys: 8
Indeed, because, we deleted only 3 values. In case we also want to delete data related to S4, we can delete extra 3 keys and get 8-3=5 total keys - as expected.