diff --git a/state/executor.go b/state/executor.go index b4581b2431f..c5bca36dbbd 100644 --- a/state/executor.go +++ b/state/executor.go @@ -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, diff --git a/types/hashing.go b/types/hashing.go index 6d084ee2fe5..328a7884268 100644 --- a/types/hashing.go +++ b/types/hashing.go @@ -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() } diff --git a/types/signed_header_test.go b/types/signed_header_test.go index 5f701219517..1242a308968 100644 --- a/types/signed_header_test.go +++ b/types/signed_header_test.go @@ -16,76 +16,83 @@ 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, }, @@ -93,7 +100,13 @@ func TestVerify(t *testing.T) { 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 {