From cd407af1aac9f614c3176c5c87cd7bc3c555768b Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Mon, 4 Aug 2025 14:42:13 +0800 Subject: [PATCH] core/rawdb: add HasCode, HasTrieNode and use them where possible #24454 (#1211) --- core/rawdb/accessors_state.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/core/rawdb/accessors_state.go b/core/rawdb/accessors_state.go index bfadc1e2c6..e1d9aa469f 100644 --- a/core/rawdb/accessors_state.go +++ b/core/rawdb/accessors_state.go @@ -47,7 +47,7 @@ func ReadCode(db ethdb.KeyValueReader, hash common.Hash) []byte { if len(data) != 0 { return data } - data, _ = db.Get(hash[:]) + data, _ = db.Get(hash.Bytes()) return data } @@ -59,6 +59,18 @@ func ReadCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) []byte { return data } +// HasCode checks if the contract code corresponding to the +// provided code hash is present in the db. +func HasCode(db ethdb.KeyValueReader, hash common.Hash) bool { + // Try with the prefixed code scheme first, if not then try with legacy + // scheme. + if ok := HasCodeWithPrefix(db, hash); ok { + return true + } + ok, _ := db.Has(hash.Bytes()) + return ok +} + // HasCodeWithPrefix checks if the contract code corresponding to the // provided code hash is present in the db. This function will only check // presence using the prefix-scheme.