refactor: pre calculate the empty hash to save cost (#25359)

Co-authored-by: Alex | Interchain Labs <alex@cosmoslabs.io>
This commit is contained in:
cui 2025-10-07 02:06:35 +08:00 committed by GitHub
parent 9c3b19ae4c
commit 0911d742fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import (
"crypto/sha256"
"hash"
"math/bits"
"slices"
)
var (
@ -17,10 +18,13 @@ func HashFromByteSlices(items [][]byte) []byte {
return hashFromByteSlices(sha256.New(), items)
}
// emptyHash is the sha256 hash of the empty string.
var emptyHash = [32]byte{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}
func hashFromByteSlices(sha hash.Hash, items [][]byte) []byte {
switch len(items) {
case 0:
return emptyHash()
return slices.Clone(emptyHash[:])
case 1:
return leafHashOpt(sha, items[0])
default:
@ -47,12 +51,6 @@ func innerHashOpt(s hash.Hash, left, right []byte) []byte {
return s.Sum(nil)
}
// emptyHash returns tmhash(<empty>)
func emptyHash() []byte {
h := sha256.Sum256([]byte{})
return h[:]
}
// getSplitPoint returns the largest power of 2 less than length
func getSplitPoint(length int64) int64 {
if length < 1 {

View File

@ -0,0 +1,20 @@
package tree
import (
"crypto/sha256"
"testing"
)
func emptyHashF() []byte {
h := sha256.Sum256([]byte{})
return h[:]
}
func TestEmptyHashEqual(t *testing.T) {
var emptyHashBytes [32]byte
copy(emptyHashBytes[:], emptyHashF())
expected := emptyHash
if emptyHashBytes != expected {
t.Fatalf("empty hash mismatch: got=%#v expected=%#v", emptyHashBytes, expected)
}
}