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

Add CommitHeader to TestChain struct #5435

Merged
merged 9 commits into from
Jan 24, 2024
50 changes: 31 additions & 19 deletions testing/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,35 @@ func (chain *TestChain) CurrentTMClientHeader() *ibctm.Header {
)
}

// CommitHeader takes in a proposed header and returns a signed cometbft header.
// The signers passed in must match the validator set provided. The signers will
// be used to sign over the proposed header.
func CommitHeader(proposedHeader cmttypes.Header, valSet *cmttypes.ValidatorSet, signers map[string]cmttypes.PrivValidator) (*cmtproto.SignedHeader, error) {
hhash := proposedHeader.Hash()
blockID := MakeBlockID(hhash, 3, tmhash.Sum([]byte("part_set")))
voteSet := cmttypes.NewVoteSet(proposedHeader.ChainID, proposedHeader.Height, 1, cmtproto.PrecommitType, valSet)

// MakeExtCommit expects a signer array in the same order as the validator array.
// Thus we iterate over the ordered validator set and construct a signer array
// from the signer map in the same order.
signerArr := make([]cmttypes.PrivValidator, len(valSet.Validators))
for i, v := range valSet.Validators { //nolint:staticcheck // need to check for nil validator set
signerArr[i] = signers[v.Address.String()]
}

extCommit, err := cmttypes.MakeExtCommit(blockID, proposedHeader.Height, 1, voteSet, signerArr, proposedHeader.Time, false)
if err != nil {
return nil, err
}

signedHeader := &cmtproto.SignedHeader{
Header: proposedHeader.ToProto(),
Commit: extCommit.ToCommit().ToProto(),
}

return signedHeader, nil
}

// CreateTMClientHeader creates a TM header to update the TM client. Args are passed in to allow
// caller flexibility to use params that differ from the chain.
func (chain *TestChain) CreateTMClientHeader(chainID string, blockHeight int64, trustedHeight clienttypes.Height, timestamp time.Time, cmtValSet, nextVals, cmtTrustedVals *cmttypes.ValidatorSet, signers map[string]cmttypes.PrivValidator) *ibctm.Header {
Expand All @@ -513,7 +542,7 @@ func (chain *TestChain) CreateTMClientHeader(chainID string, blockHeight int64,
)
require.NotNil(chain.TB, cmtValSet)

cmtHeader := cmttypes.Header{
proposedHeader := cmttypes.Header{
Version: cmtprotoversion.Consensus{Block: cmtversion.BlockProtocol, App: 2},
ChainID: chainID,
Height: blockHeight,
Expand All @@ -530,26 +559,9 @@ func (chain *TestChain) CreateTMClientHeader(chainID string, blockHeight int64,
ProposerAddress: cmtValSet.Proposer.Address, //nolint:staticcheck
}

hhash := cmtHeader.Hash()
blockID := MakeBlockID(hhash, 3, tmhash.Sum([]byte("part_set")))
voteSet := cmttypes.NewVoteSet(chainID, blockHeight, 1, cmtproto.PrecommitType, cmtValSet)

// MakeCommit expects a signer array in the same order as the validator array.
// Thus we iterate over the ordered validator set and construct a signer array
// from the signer map in the same order.
var signerArr []cmttypes.PrivValidator //nolint:prealloc // using prealloc here would be needlessly complex
for _, v := range cmtValSet.Validators { //nolint:staticcheck // need to check for nil validator set
signerArr = append(signerArr, signers[v.Address.String()])
}

extCommit, err := cmttypes.MakeExtCommit(blockID, blockHeight, 1, voteSet, signerArr, timestamp, false)
signedHeader, err := CommitHeader(proposedHeader, cmtValSet, signers)
require.NoError(chain.TB, err)

signedHeader := &cmtproto.SignedHeader{
Header: cmtHeader.ToProto(),
Commit: extCommit.ToCommit().ToProto(),
}

if cmtValSet != nil { //nolint:staticcheck
valSet, err = cmtValSet.ToProto()
require.NoError(chain.TB, err)
Expand Down
Loading