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
Changes from 1 commit
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
20 changes: 19 additions & 1 deletion header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/celestiaorg/celestia-app/v3/pkg/appconsts"
"github.com/celestiaorg/celestia-app/v3/pkg/da"
libhead "github.com/celestiaorg/go-header"
"github.com/celestiaorg/go-square/merkle"
"github.com/celestiaorg/rsmt2d"
)

Expand All @@ -38,6 +39,8 @@ type ExtendedHeader struct {
Commit *core.Commit `json:"commit"`
ValidatorSet *core.ValidatorSet `json:"validator_set"`
DAH *da.DataAvailabilityHeader `json:"dah"`

hash []byte
}

// MakeExtendedHeader assembles new ExtendedHeader.
Expand Down Expand Up @@ -94,7 +97,22 @@ func (eh *ExtendedHeader) Time() time.Time {
// NOTE: It purposely overrides Hash method of RawHeader to get it directly from Commit without
// recomputing.
func (eh *ExtendedHeader) Hash() libhead.Hash {
return eh.Commit.BlockID.Hash.Bytes()
if len(eh.hash) == 0 {
bs := make([][]byte, len(eh.Commit.Signatures))
for i, commitSig := range eh.Commit.Signatures {
pbcs := commitSig.ToProto()

bz, err := pbcs.Marshal()
if err != nil {
panic(err)
}

bs[i] = bz
}

eh.hash = merkle.HashFromByteSlices(bs)
}
return eh.hash
}

// LastHeader returns the Hash of the last wrapped RawHeader.
Expand Down
Loading