Skip to content

Commit

Permalink
Recompute signature in signature header tests to make tests throw the…
Browse files Browse the repository at this point in the history
… right errors (cosmos#1162)

<!--
Please read and fill out this form before submitting your PR.

Please make sure you have reviewed our contributors guide before
submitting your
first PR.
-->

## Overview
Closes: cosmos#1163 

<!-- 
Please provide an explanation of the PR, including the appropriate
context,
background, goal, and rationale. If there is an issue with this
information,
please provide a tl;dr and link the issue. 
-->

## Checklist

<!-- 
Please complete the checklist to ensure that the PR is ready to be
reviewed.

IMPORTANT:
PRs should be left in Draft until the below checklist is completed.
-->

- [x] New and updated code has appropriate documentation
- [x] New and updated code has new and/or updated testing
- [x] Required CI checks are passing
- [ ] Visual proof for any user facing features like CLI or
documentation updates
- [x] Linked issues closed with keywords
  • Loading branch information
Manav-Aggarwal authored Sep 1, 2023
1 parent 8981fd6 commit 8063321
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (e *BlockExecutor) CreateBlock(height uint64, lastCommit *types.Commit, las
BaseHeader: types.BaseHeader{
ChainID: e.chainID,
Height: height,
Time: uint64(time.Now().UnixNano()), // TODO(tzdybal): how to get TAI64?
Time: uint64(time.Now().UnixNano()),
},
//LastHeaderHash: lastHeaderHash,
//LastCommitHash: lastCommitHash,
Expand Down
2 changes: 1 addition & 1 deletion types/hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ func (h *Header) Hash() Hash {

// Hash returns ABCI-compatible hash of a block.
func (b *Block) Hash() Hash {
return b.SignedHeader.Header.Hash()
return b.SignedHeader.Hash()
}
69 changes: 41 additions & 28 deletions types/signed_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,84 +16,97 @@ func TestVerify(t *testing.T) {
untrustedAdj, err := GetNextRandomHeader(trusted, privKey)
require.NoError(t, err)
tests := []struct {
prepare func() *SignedHeader
prepare func() (*SignedHeader, bool)
err bool
}{
{
prepare: func() *SignedHeader { return untrustedAdj },
prepare: func() (*SignedHeader, bool) { return untrustedAdj, false },
err: false,
},
{
prepare: func() *SignedHeader {
prepare: func() (*SignedHeader, bool) {
untrusted := *untrustedAdj
untrusted.AggregatorsHash = GetRandomBytes(32)
return &untrusted
return &untrusted, true
},
err: true,
},
{
prepare: func() *SignedHeader {
prepare: func() (*SignedHeader, bool) {
untrusted := *untrustedAdj
untrusted.LastHeaderHash = GetRandomBytes(32)
return &untrusted
return &untrusted, true
},
err: true,
},
{
prepare: func() *SignedHeader {
prepare: func() (*SignedHeader, bool) {
untrusted := *untrustedAdj
untrusted.LastCommitHash = GetRandomBytes(32)
return &untrusted
return &untrusted, true
},
err: true,
},
{
prepare: func() *SignedHeader {
untrustedAdj.Header.BaseHeader.Height++
return untrustedAdj
prepare: func() (*SignedHeader, bool) {
// Checks for non-adjacency
untrusted := *untrustedAdj
untrusted.Header.BaseHeader.Height++
return &untrusted, true
},
err: true,
err: false, // Accepts non-adjacent headers
},
{
prepare: func() *SignedHeader {
untrustedAdj.Header.BaseHeader.Time = uint64(untrustedAdj.Header.Time().Truncate(time.Hour).UnixNano())
return untrustedAdj
prepare: func() (*SignedHeader, bool) {
untrusted := *untrustedAdj
untrusted.Header.BaseHeader.Time = uint64(untrusted.Header.Time().Truncate(time.Hour).UnixNano())
return &untrusted, true
},
err: true,
},
{
prepare: func() *SignedHeader {
untrustedAdj.Header.BaseHeader.Time = uint64(untrustedAdj.Header.Time().Add(time.Minute).UnixNano())
return untrustedAdj
prepare: func() (*SignedHeader, bool) {
untrusted := *untrustedAdj
untrusted.Header.BaseHeader.Time = uint64(untrusted.Header.Time().Add(time.Minute).UnixNano())
return &untrusted, true
},
err: true,
},
{
prepare: func() *SignedHeader {
untrustedAdj.BaseHeader.ChainID = "toaster"
return untrustedAdj
prepare: func() (*SignedHeader, bool) {
untrusted := *untrustedAdj
untrusted.BaseHeader.ChainID = "toaster"
return &untrusted, false // Signature verification should fail
},
err: true,
},
{
prepare: func() *SignedHeader {
untrustedAdj.Version.App = untrustedAdj.Version.App + 1
return untrustedAdj
prepare: func() (*SignedHeader, bool) {
untrusted := *untrustedAdj
untrusted.Version.App = untrusted.Version.App + 1
return &untrusted, false // Signature verification should fail
},
err: true,
},
{
prepare: func() *SignedHeader {
untrustedAdj.ProposerAddress = nil
return untrustedAdj
prepare: func() (*SignedHeader, bool) {
untrusted := *untrustedAdj
untrusted.ProposerAddress = nil
return &untrusted, true
},
err: true,
},
}

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
err := trusted.Verify(test.prepare())
preparedHeader, recomputeCommit := test.prepare()
if recomputeCommit {
commit, err := getCommit(preparedHeader.Header, privKey)
require.NoError(t, err)
preparedHeader.Commit = *commit
}
err = trusted.Verify(preparedHeader)
if test.err {
assert.Error(t, err)
} else {
Expand Down

0 comments on commit 8063321

Please sign in to comment.