Skip to content

Commit

Permalink
test for commit hash computation's backward compatibility (#1240)
Browse files Browse the repository at this point in the history
We might change the code that computes the block commit hash
during refactoring or to improve the performance from time to time.
When changing the hash computation code, we need to ensure that
we are not changing the way commit hash has been computed in previous
releases. Hence, we add a test to ensure that commit hash
computation is always compatible with the first code added in v1.1

FAB-17878

Signed-off-by: senthil <cendhu@gmail.com>
  • Loading branch information
cendhu authored May 12, 2020
1 parent 42d6581 commit cc92b8d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/ledger/kvledger/tests/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ func (c *client) currentHeight() uint64 {
return bcInfo.Height
}

func (c *client) currentCommitHash() []byte {
block, err := c.lgr.GetBlockByNumber(c.currentHeight() - 1)
c.assert.NoError(err)
if len(block.Metadata.Metadata) < int(common.BlockMetadataIndex_COMMIT_HASH+1) {
return nil
}
commitHash := &common.Metadata{}
err = proto.Unmarshal(block.Metadata.Metadata[common.BlockMetadataIndex_COMMIT_HASH], commitHash)
c.assert.NoError(err)
return commitHash.Value
}

/////////////////////// simulator wrapper functions ///////////////////////
type simulator struct {
ledger.TxSimulator
Expand Down
7 changes: 7 additions & 0 deletions core/ledger/kvledger/tests/v1x_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ func testV11CommitHashes(t *testing.T,
env.initLedgerMgmt()
h := env.newTestHelperOpenLgr("ledger1", t)
blocksAndPvtData := h.retrieveCommittedBlocksAndPvtdata(0, h.currentHeight()-1)

var commitHashPreReset []byte
if preResetCommitHashExists {
commitHashPreReset = h.currentCommitHash()
h.verifyCommitHashExists()
} else {
h.verifyCommitHashNotExists()
Expand All @@ -199,6 +202,10 @@ func testV11CommitHashes(t *testing.T,
require.NoError(t, h.lgr.CommitLegacy(d, &ledger.CommitOptions{FetchPvtDataFromLedger: true}))
}

if preResetCommitHashExists {
commitHashPostReset := h.currentCommitHash()
require.Equal(t, commitHashPreReset, commitHashPostReset)
}
if postResetCommitHashExists {
h.verifyCommitHashExists()
} else {
Expand Down

0 comments on commit cc92b8d

Please sign in to comment.