Skip to content

Commit

Permalink
feat(header/headertest): Enable block time intervals for header gener…
Browse files Browse the repository at this point in the history
…ation (celestiaorg#3139)

Header test suite can now generate headers with the given block time
interval if it is specified.

Needed for testing pruner service.
  • Loading branch information
renaynay authored Mar 5, 2024
1 parent a487efd commit f843183
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
24 changes: 17 additions & 7 deletions header/headertest/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,24 @@ type TestSuite struct {
valPntr int

head *header.ExtendedHeader

// blockTime is optional - if set, the test suite will generate
// blocks timestamped at the specified interval
blockTime time.Duration
}

func NewStore(t *testing.T) libhead.Store[*header.ExtendedHeader] {
return headertest.NewStore[*header.ExtendedHeader](t, NewTestSuite(t, 3), 10)
return headertest.NewStore[*header.ExtendedHeader](t, NewTestSuite(t, 3, 0), 10)
}

// NewTestSuite setups a new test suite with a given number of validators.
func NewTestSuite(t *testing.T, num int) *TestSuite {
valSet, vals := RandValidatorSet(num, 10)
func NewTestSuite(t *testing.T, numValidators int, blockTime time.Duration) *TestSuite {
valSet, vals := RandValidatorSet(numValidators, 10)
return &TestSuite{
t: t,
vals: vals,
valSet: valSet,
t: t,
vals: vals,
valSet: valSet,
blockTime: blockTime,
}
}

Expand Down Expand Up @@ -155,13 +160,18 @@ func (s *TestSuite) GenRawHeader(
height uint64, lastHeader, lastCommit, dataHash libhead.Hash) *header.RawHeader {
rh := RandRawHeader(s.t)
rh.Height = int64(height)
rh.Time = time.Now()
rh.LastBlockID = types.BlockID{Hash: bytes.HexBytes(lastHeader)}
rh.LastCommitHash = bytes.HexBytes(lastCommit)
rh.DataHash = bytes.HexBytes(dataHash)
rh.ValidatorsHash = s.valSet.Hash()
rh.NextValidatorsHash = s.valSet.Hash()
rh.ProposerAddress = s.nextProposer().Address

rh.Time = time.Now()
if s.blockTime > 0 {
rh.Time = s.Head().Time().Add(s.blockTime)
}

return rh
}

Expand Down
4 changes: 2 additions & 2 deletions header/headertest/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestVerify(t *testing.T) {
h := NewTestSuite(t, 2).GenExtendedHeaders(3)
h := NewTestSuite(t, 2, 0).GenExtendedHeaders(3)
trusted, untrustedAdj, untrustedNonAdj := h[0], h[1], h[2]
tests := []struct {
prepare func() *header.ExtendedHeader
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestVerify(t *testing.T) {
{
prepare: func() *header.ExtendedHeader {
untrusted := *untrustedNonAdj
untrusted.Commit = NewTestSuite(t, 2).Commit(RandRawHeader(t))
untrusted.Commit = NewTestSuite(t, 2, 0).Commit(RandRawHeader(t))
return &untrusted
},
err: header.ErrVerifyCommitLightTrustingFailed,
Expand Down

0 comments on commit f843183

Please sign in to comment.