Skip to content

Commit

Permalink
perf(state): Cache the block hash cometbft#2924 (#30) (#34)
Browse files Browse the repository at this point in the history
* perf(state): Cache the block hash (backport cometbft#2924) (cometbft#2932)

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 <ValarDragon@users.noreply.github.com>
Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>

* delete md

* changelog

* changelog

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
(cherry picked from commit 7cbbf31)

Co-authored-by: Adam Tucker <adam@osmosis.team>
  • Loading branch information
mergify[bot] and czarcas7ic authored Apr 30, 2024
1 parent a9f5161 commit 2007939
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

## Unreleased

## v0.37.4-v24-osmo-4

* [#27](https://github.com/osmosis-labs/cometbft/pull/27) Lower allocation overhead of txIndex matchRange
* [#28](https://github.com/osmosis-labs/cometbft/pull/28) Significantly speedup bitArray.PickRandom
* [#29](https://github.com/osmosis-labs/cometbft/pull/29) Lower heap overhead of JSON encoding
* [#30](https://github.com/osmosis-labs/cometbft/pull/30) Cache the block hash

## v0.37.4-v24-osmo-3

Expand Down
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 2007939

Please sign in to comment.