Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions packages/mpt/src/mpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ export class MerklePatriciaTrie {
key: Uint8Array,
value: Uint8Array | null,
skipKeyTransform: boolean = false,
): Promise<void> {
trackPruningOps: boolean = false,
): Promise<BatchDBOp[]> {
this.DEBUG && this.debug(`Key: ${bytesToHex(key)}`, ['put'])
this.DEBUG && this.debug(`Value: ${value === null ? 'null' : bytesToHex(key)}`, ['put'])
if (this._opts.useRootPersistence && equalsBytes(key, ROOT_DB_KEY) === true) {
Expand All @@ -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)
Copy link
Member

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 (?)

}

let ops: BatchDBOp[] = []
await this._lock.acquire()
const appliedKey = skipKeyTransform ? key : this.appliedKey(key)
if (equalsBytes(this.root(), this.EMPTY_TRIE_ROOT) === true) {
Expand All @@ -229,8 +231,8 @@ export class MerklePatriciaTrie {
} else {
// First try to find the given key or its nearest node
const { remaining, stack } = await this.findPath(appliedKey)
let ops: BatchDBOp[] = []
if (this._opts.useNodePruning) {
let forceFindPruningOps = this._opts.useNodePruning || trackPruningOps
if (forceFindPruningOps) {
const val = await this.get(key)
// Only delete keys if it either does not exist, or if it gets updated
// (The update will update the hash of the node, thus we can delete the original leaf node)
Expand Down Expand Up @@ -264,6 +266,7 @@ export class MerklePatriciaTrie {
}
await this.persistRoot()
this._lock.release()
return ops
}

/**
Expand All @@ -272,15 +275,16 @@ export class MerklePatriciaTrie {
* @param key
* @returns A Promise that resolves once value is deleted.
*/
async del(key: Uint8Array, skipKeyTransform: boolean = false): Promise<void> {
async del(key: Uint8Array, skipKeyTransform: boolean = false, trackPruningOps: boolean = false): Promise<BatchDBOp[]> {
this.DEBUG && this.debug(`Key: ${bytesToHex(key)}`, ['del'])
await this._lock.acquire()
const appliedKey = skipKeyTransform ? key : this.appliedKey(key)
const { node, stack } = await this.findPath(appliedKey)

let ops: BatchDBOp[] = []
// Only delete if the `key` currently has any value
if (this._opts.useNodePruning && node !== null) {
let forceFindPruningOps = this._opts.useNodePruning || trackPruningOps
if (forceFindPruningOps && node !== null) {
const deleteHashes = stack.map((e) => this.hash(e.serialize()))
// Just as with `put`, the stack items all will have their keyHashes updated
// So after deleting the node, one can safely delete these from the DB
Expand All @@ -307,8 +311,22 @@ export class MerklePatriciaTrie {
}
await this.persistRoot()
this._lock.release()
return ops
}

/**
* Deletes data related to previous states from db value given a `key` from the trie
* @param ops
* @returns A Promise that resolves once values are deleted.
*/
async delPrevStatesData(ops: BatchDBOp[]): Promise<void> {
await this._lock.acquire()
await this._db.batch(ops)
await this.persistRoot()
this._lock.release()
}


/**
* Tries to find a path to the node for the given key.
* It returns a `stack` of nodes to the closest node.
Expand Down