core/types: add length check in CalcRequestsHash (#30829)

The existing implementation is correct when building and verifying
blocks, since we will only collect non-empty requests into the block
requests list.

But it isn't correct for cases where a requests list containing empty
items is sent by the consensus layer on the engine API. We want to
ensure that empty requests do not cause a difference in validation
there, so the commitment computation should explicitly skip them.
This commit is contained in:
Felix Lange 2024-11-28 18:43:39 +01:00 committed by GitHub
parent 53f66c1b03
commit c7a8bcecbe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 3 deletions

View File

@ -463,9 +463,11 @@ func CalcRequestsHash(requests [][]byte) common.Hash {
h1, h2 := sha256.New(), sha256.New()
var buf common.Hash
for _, item := range requests {
h1.Reset()
h1.Write(item)
h2.Write(h1.Sum(buf[:0]))
if len(item) > 1 { // skip items with only requestType and no data.
h1.Reset()
h1.Write(item)
h2.Write(h1.Sum(buf[:0]))
}
}
h2.Sum(buf[:0])
return buf