From 524d3ceb889772178d9d4ec5876931671937a7fc Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 1 Apr 2022 11:12:12 -0400 Subject: [PATCH] e2e: Fix hashing for app + Fix logic of TestApp_Hash (backport #8229) (#8236) --- .github/workflows/lint.yml | 2 +- crypto/xchacha20poly1305/xchachapoly_test.go | 8 +++--- test/e2e/app/snapshots.go | 2 +- test/e2e/app/state.go | 12 ++++++--- test/e2e/tests/app_test.go | 27 ++++++++++---------- 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index b808d9f77..c1d51b8d0 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -23,7 +23,7 @@ jobs: - uses: golangci/golangci-lint-action@v2.5.2 with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. - version: v1.42.1 + version: v1.45.2 args: --timeout 10m github-token: ${{ secrets.github_token }} if: env.GIT_DIFF diff --git a/crypto/xchacha20poly1305/xchachapoly_test.go b/crypto/xchacha20poly1305/xchachapoly_test.go index 75953d72d..99903e9f9 100644 --- a/crypto/xchacha20poly1305/xchachapoly_test.go +++ b/crypto/xchacha20poly1305/xchachapoly_test.go @@ -25,19 +25,19 @@ func TestRandom(t *testing.T) { plaintext := make([]byte, pl) _, err := crand.Read(key[:]) if err != nil { - t.Errorf("error on read: %w", err) + t.Errorf("error on read: %v", err) } _, err = crand.Read(nonce[:]) if err != nil { - t.Errorf("error on read: %w", err) + t.Errorf("error on read: %v", err) } _, err = crand.Read(ad) if err != nil { - t.Errorf("error on read: %w", err) + t.Errorf("error on read: %v", err) } _, err = crand.Read(plaintext) if err != nil { - t.Errorf("error on read: %w", err) + t.Errorf("error on read: %v", err) } aead, err := New(key[:]) diff --git a/test/e2e/app/snapshots.go b/test/e2e/app/snapshots.go index 6a9c0e0dc..bda71eb1e 100644 --- a/test/e2e/app/snapshots.go +++ b/test/e2e/app/snapshots.go @@ -93,7 +93,7 @@ func (s *SnapshotStore) Create(state *State) (abci.Snapshot, error) { snapshot := abci.Snapshot{ Height: state.Height, Format: 1, - Hash: hashItems(state.Values), + Hash: hashItems(state.Values, state.Height), Chunks: byteChunks(bz), } err = ioutil.WriteFile(filepath.Join(s.dir, fmt.Sprintf("%v.json", state.Height)), bz, 0644) diff --git a/test/e2e/app/state.go b/test/e2e/app/state.go index e82a22539..17d8cd75f 100644 --- a/test/e2e/app/state.go +++ b/test/e2e/app/state.go @@ -3,6 +3,7 @@ package app import ( "crypto/sha256" + "encoding/binary" "encoding/json" "errors" "fmt" @@ -38,7 +39,7 @@ func NewState(dir string, persistInterval uint64) (*State, error) { previousFile: filepath.Join(dir, prevStateFileName), persistInterval: persistInterval, } - state.Hash = hashItems(state.Values) + state.Hash = hashItems(state.Values, state.Height) err := state.load() switch { case errors.Is(err, os.ErrNotExist): @@ -114,7 +115,7 @@ func (s *State) Import(height uint64, jsonBytes []byte) error { } s.Height = height s.Values = values - s.Hash = hashItems(values) + s.Hash = hashItems(values, height) return s.save() } @@ -140,7 +141,6 @@ func (s *State) Set(key, value string) { func (s *State) Commit() (uint64, []byte, error) { s.Lock() defer s.Unlock() - s.Hash = hashItems(s.Values) switch { case s.Height > 0: s.Height++ @@ -149,6 +149,7 @@ func (s *State) Commit() (uint64, []byte, error) { default: s.Height = 1 } + s.Hash = hashItems(s.Values, s.Height) if s.persistInterval > 0 && s.Height%s.persistInterval == 0 { err := s.save() if err != nil { @@ -171,7 +172,7 @@ func (s *State) Rollback() error { } // hashItems hashes a set of key/value items. -func hashItems(items map[string]string) []byte { +func hashItems(items map[string]string, height uint64) []byte { keys := make([]string, 0, len(items)) for key := range items { keys = append(keys, key) @@ -179,6 +180,9 @@ func hashItems(items map[string]string) []byte { sort.Strings(keys) hasher := sha256.New() + var b [8]byte + binary.BigEndian.PutUint64(b[:], height) + _, _ = hasher.Write(b[:]) for _, key := range keys { _, _ = hasher.Write([]byte(key)) _, _ = hasher.Write([]byte{0}) diff --git a/test/e2e/tests/app_test.go b/test/e2e/tests/app_test.go index ba82e0e89..3ec8671ef 100644 --- a/test/e2e/tests/app_test.go +++ b/test/e2e/tests/app_test.go @@ -48,23 +48,24 @@ func TestApp_Hash(t *testing.T) { info, err := client.ABCIInfo(ctx) require.NoError(t, err) require.NotEmpty(t, info.Response.LastBlockAppHash, "expected app to return app hash") + // In next-block execution, the app hash is stored in the next block + blockHeight := info.Response.LastBlockHeight + 1 - status, err := client.Status(ctx) - require.NoError(t, err) - require.NotZero(t, status.SyncInfo.LatestBlockHeight) + require.Eventually(t, func() bool { + status, err := client.Status(ctx) + require.NoError(t, err) + require.NotZero(t, status.SyncInfo.LatestBlockHeight) + return status.SyncInfo.LatestBlockHeight >= blockHeight + }, 60*time.Second, 500*time.Millisecond) - block, err := client.Block(ctx, &info.Response.LastBlockHeight) + block, err := client.Block(ctx, &blockHeight) require.NoError(t, err) - if info.Response.LastBlockHeight == block.Block.Height { - require.Equal(t, - fmt.Sprintf("%x", info.Response.LastBlockAppHash), - fmt.Sprintf("%x", block.Block.AppHash.Bytes()), - "app hash does not match last block's app hash") - } - - require.True(t, status.SyncInfo.LatestBlockHeight >= info.Response.LastBlockHeight, - "status out of sync with application") + require.Equal(t, blockHeight, block.Block.Height) + require.Equal(t, + fmt.Sprintf("%x", info.Response.LastBlockAppHash), + fmt.Sprintf("%x", block.Block.AppHash.Bytes()), + "app hash does not match last block's app hash") }) }