Skip to content

Commit

Permalink
perf(state): Cache the block hash (backport cometbft#2924) (cometbft#…
Browse files Browse the repository at this point in the history
…2931)

Closes cometbft#2923 

Caches the block hash to ensure we only compute it once in consensus
execution.

---

#### PR checklist

- [x] Tests written/updated - not sure what test I should write, any
suggestions?
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
<hr>This is an automatic backport of pull request cometbft#2924 done by
[Mergify](https://mergify.com).

Co-authored-by: Dev Ojha <[email protected]>
  • Loading branch information
mergify[bot] and ValarDragon authored Apr 29, 2024
1 parent cdf8c54 commit 203bf33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[state/execution]` Cache the block hash computation inside of the Block Type, so we only compute it once.
([\#2924](https://github.com/cometbft/cometbft/pull/2924))
16 changes: 11 additions & 5 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ const (
type Block struct {
mtx cmtsync.Mutex

Header `json:"header"`
Data `json:"data"`
Evidence EvidenceData `json:"evidence"`
LastCommit *Commit `json:"last_commit"`
verifiedHash cmtbytes.HexBytes // Verified block hash (not included in the struct hash)
Header `json:"header"`
Data `json:"data"`
Evidence EvidenceData `json:"evidence"`
LastCommit *Commit `json:"last_commit"`
}

// ValidateBasic performs basic validation that doesn't involve state data.
Expand Down Expand Up @@ -130,8 +131,13 @@ func (b *Block) Hash() cmtbytes.HexBytes {
if b.LastCommit == nil {
return nil
}
if b.verifiedHash != nil {
return b.verifiedHash
}
b.fillHeader()
return b.Header.Hash()
hash := b.Header.Hash()
b.verifiedHash = hash
return hash
}

// MakePartSet returns a PartSet containing parts of a serialized block.
Expand Down

0 comments on commit 203bf33

Please sign in to comment.