Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(header): cache hash #3975

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"sync"
"time"

tmjson "github.com/tendermint/tendermint/libs/json"
Expand Down Expand Up @@ -38,6 +39,9 @@ type ExtendedHeader struct {
Commit *core.Commit `json:"commit"`
ValidatorSet *core.ValidatorSet `json:"validator_set"`
DAH *da.DataAvailabilityHeader `json:"dah"`

hashOnce sync.Once
hash []byte
}

// MakeExtendedHeader assembles new ExtendedHeader.
Expand Down Expand Up @@ -90,11 +94,13 @@ func (eh *ExtendedHeader) Time() time.Time {
return eh.RawHeader.Time
}

// Hash returns Hash of the wrapped RawHeader.
// NOTE: It purposely overrides Hash method of RawHeader to get it directly from Commit without
// recomputing.
// Hash returns Hash of the wrapped [RawHeader].
// Value is cached to not be recomuted every call.
func (eh *ExtendedHeader) Hash() libhead.Hash {
return eh.Commit.BlockID.Hash.Bytes()
eh.hashOnce.Do(func() {
eh.hash = eh.RawHeader.Hash()
})
return eh.hash
}

// LastHeader returns the Hash of the last wrapped RawHeader.
Expand Down Expand Up @@ -147,7 +153,7 @@ func (eh *ExtendedHeader) Validate() error {
if eh.Commit.Height != eh.RawHeader.Height {
return fmt.Errorf("header and commit height mismatch: %d vs %d", eh.RawHeader.Height, eh.Commit.Height)
}
if hhash, chash := eh.RawHeader.Hash(), eh.Commit.BlockID.Hash; !bytes.Equal(hhash, chash) {
if hhash, chash := eh.Hash(), eh.Commit.BlockID.Hash; !bytes.Equal(hhash, chash) {
return fmt.Errorf("commit signs block %X, header is block %X", chash, hhash)
}

Expand Down Expand Up @@ -223,7 +229,12 @@ func (eh *ExtendedHeader) UnmarshalBinary(data []byte) error {
return err
}

*eh = *out
*eh = ExtendedHeader{
RawHeader: out.RawHeader,
Commit: out.Commit,
ValidatorSet: out.ValidatorSet,
DAH: out.DAH,
}
return nil
}

Expand Down
21 changes: 18 additions & 3 deletions header/headertest/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,38 @@ func TestVerify(t *testing.T) {
},
{
prepare: func() *header.ExtendedHeader {
untrusted := *untrustedAdj
untrusted := header.ExtendedHeader{
RawHeader: untrustedAdj.RawHeader,
Commit: untrustedAdj.Commit,
ValidatorSet: untrustedAdj.ValidatorSet,
DAH: untrustedAdj.DAH,
}
untrusted.ValidatorsHash = tmrand.Bytes(32)
return &untrusted
},
err: header.ErrValidatorHashMismatch,
},
{
prepare: func() *header.ExtendedHeader {
untrusted := *untrustedAdj
untrusted := header.ExtendedHeader{
RawHeader: untrustedAdj.RawHeader,
Commit: untrustedAdj.Commit,
ValidatorSet: untrustedAdj.ValidatorSet,
DAH: untrustedAdj.DAH,
}
untrusted.RawHeader.LastBlockID.Hash = tmrand.Bytes(32)
return &untrusted
},
err: header.ErrLastHeaderHashMismatch,
},
{
prepare: func() *header.ExtendedHeader {
untrusted := *untrustedNonAdj
untrusted := header.ExtendedHeader{
RawHeader: untrustedNonAdj.RawHeader,
Commit: untrustedNonAdj.Commit,
ValidatorSet: untrustedNonAdj.ValidatorSet,
DAH: untrustedNonAdj.DAH,
}
untrusted.Commit = NewTestSuite(t, 2, 0).Commit(RandRawHeader(t))
return &untrusted
},
Expand Down
Loading