From 60d7d29fc9153c5ec1e6e4c36f1873b8d2265df7 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 16 Aug 2022 08:10:24 +0200 Subject: [PATCH 1/4] core/state, trie, light: Add a DeleteAccount method --- core/state/database.go | 3 +++ core/state/statedb.go | 2 +- light/trie.go | 7 +++++++ trie/secure_trie.go | 6 ++++++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/core/state/database.go b/core/state/database.go index edbf78ae311..2fcade3f1b3 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -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 + // 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 diff --git a/core/state/statedb.go b/core/state/statedb.go index cd388d6a364..1254e464d0c 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -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)) } } diff --git a/light/trie.go b/light/trie.go index 5755e2cc195..6ffab36be6b 100644 --- a/light/trie.go +++ b/light/trie.go @@ -153,6 +153,13 @@ func (t *odrTrie) TryDelete(key []byte) error { }) } +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 diff --git a/trie/secure_trie.go b/trie/secure_trie.go index 28b3473c011..fad61b497ac 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -189,6 +189,12 @@ func (t *StateTrie) TryDelete(key []byte) error { return t.trie.TryDelete(hk) } +func (t *StateTrie) DeleteAccount(key []byte) error { + 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 { From ba590386e53cfdd2beef9accb5f3e1b9e7358e4c Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 16 Aug 2022 09:47:28 +0200 Subject: [PATCH 2/4] review feedback --- light/trie.go | 1 + trie/secure_trie.go | 1 + 2 files changed, 2 insertions(+) diff --git a/light/trie.go b/light/trie.go index 6ffab36be6b..33aa30f4fd8 100644 --- a/light/trie.go +++ b/light/trie.go @@ -153,6 +153,7 @@ 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 { diff --git a/trie/secure_trie.go b/trie/secure_trie.go index fad61b497ac..162a7fca913 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -189,6 +189,7 @@ 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 { hk := t.hashKey(key) delete(t.getSecKeyCache(), string(hk)) From c09ab630f6d14266cde86c10b78d0c27a734daf5 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Tue, 16 Aug 2022 18:29:55 +0800 Subject: [PATCH 3/4] Update database.go --- core/state/database.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/state/database.go b/core/state/database.go index 2fcade3f1b3..7ba671fbb69 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -87,7 +87,7 @@ 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 abstracts an account deletion from the trie. DeleteAccount(key []byte) error // Hash returns the root hash of the trie. It does not write to the database and From cdda614ee908e3a9575c571a6ca9acf8b33e5225 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 16 Aug 2022 14:48:04 +0200 Subject: [PATCH 4/4] pr triage feedback --- core/state/database.go | 4 ++-- core/state/statedb.go | 2 +- light/trie.go | 4 ++-- trie/secure_trie.go | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/state/database.go b/core/state/database.go index 7ba671fbb69..96b6bcfe655 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -87,8 +87,8 @@ 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 + // TryDeleteAccount abstracts an account deletion from the trie. + TryDeleteAccount(key []byte) error // 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. diff --git a/core/state/statedb.go b/core/state/statedb.go index 1254e464d0c..5c97dd94ade 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -484,7 +484,7 @@ func (s *StateDB) deleteStateObject(obj *stateObject) { } // Delete the account from the trie addr := obj.Address() - if err := s.trie.DeleteAccount(addr[:]); err != nil { + if err := s.trie.TryDeleteAccount(addr[:]); err != nil { s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err)) } } diff --git a/light/trie.go b/light/trie.go index 33aa30f4fd8..f60edaa3b17 100644 --- a/light/trie.go +++ b/light/trie.go @@ -153,8 +153,8 @@ func (t *odrTrie) TryDelete(key []byte) error { }) } -// DeleteACcount abstracts an account deletion from the trie. -func (t *odrTrie) DeleteAccount(key []byte) error { +// TryDeleteACcount abstracts an account deletion from the trie. +func (t *odrTrie) TryDeleteAccount(key []byte) error { key = crypto.Keccak256(key) return t.do(key, func() error { return t.trie.TryDelete(key) diff --git a/trie/secure_trie.go b/trie/secure_trie.go index 162a7fca913..3d468f56ee0 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -189,8 +189,8 @@ 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 { +// TryDeleteACcount abstracts an account deletion from the trie. +func (t *StateTrie) TryDeleteAccount(key []byte) error { hk := t.hashKey(key) delete(t.getSecKeyCache(), string(hk)) return t.trie.TryDelete(hk)