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

core/state, trie, light: Add a DeleteAccount method #25531

Merged
merged 4 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ type Trie interface {
// found in the database, a trie.MissingNodeError is returned.
TryDelete(key []byte) error

// DeleteACcount abstracts an account deletion from the trie.
DeleteAccount(key []byte) error
rjl493456442 marked this conversation as resolved.
Show resolved Hide resolved

// Hash returns the root hash of the trie. It does not write to the database and
// can be used even if the trie doesn't have one.
Hash() common.Hash
Expand Down
2 changes: 1 addition & 1 deletion core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ func (s *StateDB) deleteStateObject(obj *stateObject) {
}
// Delete the account from the trie
addr := obj.Address()
if err := s.trie.TryDelete(addr[:]); err != nil {
if err := s.trie.DeleteAccount(addr[:]); err != nil {
s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err))
}
}
Expand Down
8 changes: 8 additions & 0 deletions light/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ func (t *odrTrie) TryDelete(key []byte) error {
})
}

// DeleteACcount abstracts an account deletion from the trie.
func (t *odrTrie) DeleteAccount(key []byte) error {
key = crypto.Keccak256(key)
return t.do(key, func() error {
return t.trie.TryDelete(key)
})
}

func (t *odrTrie) Commit(collectLeaf bool) (common.Hash, *trie.NodeSet, error) {
if t.trie == nil {
return t.id.Root, nil, nil
Expand Down
7 changes: 7 additions & 0 deletions trie/secure_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ func (t *StateTrie) TryDelete(key []byte) error {
return t.trie.TryDelete(hk)
}

// DeleteACcount abstracts an account deletion from the trie.
func (t *StateTrie) DeleteAccount(key []byte) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+docstring

hk := t.hashKey(key)
delete(t.getSecKeyCache(), string(hk))
return t.trie.TryDelete(hk)
}

// GetKey returns the sha3 preimage of a hashed key that was
// previously used to store a value.
func (t *StateTrie) GetKey(shaKey []byte) []byte {
Expand Down