From 06fc32f52083ac38aa2fafcfc564a02753295a7a Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Mon, 5 Aug 2024 11:52:27 +0800 Subject: [PATCH] eth/protocol/snap: improve delete function --- eth/protocols/snap/gentrie.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/eth/protocols/snap/gentrie.go b/eth/protocols/snap/gentrie.go index 882eb2befb..5126d26777 100644 --- a/eth/protocols/snap/gentrie.go +++ b/eth/protocols/snap/gentrie.go @@ -212,19 +212,24 @@ func (t *pathTrie) update(key, value []byte) error { // delete implements genTrie interface, deleting the item from the stack trie. func (t *pathTrie) delete(key []byte) error { - // reset the trie along with the trackers. Be aware that the last - // inserted item (potentially along with some internal trie nodes) - // will be silently discarded by trie resetting. + // Commit the trie since the right boundary is incomplete because + // of the deleted item. This will implicitly discard the last inserted + // item and clean some ancestor trie nodes of the last committed + // item in the database. + t.commit(false) + + // Reset the trie and all the internal trackers t.first = nil t.last = nil t.tr.Reset() - // explicitly mark the left boundary as incomplete, as the left-side - // item of the next one has been removed. Be aware that the next item - // to be inserted to will be ignored from committing as well. + // Explicitly mark the left boundary as incomplete, as the left-side + // item of the next one has been deleted. Be aware that the next item + // to be inserted will be ignored from committing as well as it's on + // the left boundary. t.skipLeftBoundary = true - // explicitly delete the potential leftover nodes on the specific + // Explicitly delete the potential leftover nodes on the specific // path from the database. tkey := t.tr.TrieKey(key) for i := 0; i <= len(tkey); i++ {