Skip to content

Commit

Permalink
Add CommitHeader to TestChain struct (cosmos#5435)
Browse files Browse the repository at this point in the history
* add CommitHeader to TestChain struct

* imp: add godoc and minor imp

* Update testing/chain.go

---------

Co-authored-by: Lặc <67097720+expertdicer@users.noreply.github.com>
Co-authored-by: Colin Axnér <25233464+colin-axner@users.noreply.github.com>
Co-authored-by: Damian Nolan <damiannolan@gmail.com>
  • Loading branch information
4 people committed Jan 24, 2024
1 parent 87dc82e commit 2f3fa4b
Showing 1 changed file with 31 additions and 19 deletions.
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

0 comments on commit 2f3fa4b

Please sign in to comment.