diff --git a/core/ledger/pvtdatastorage/kv_encoding_test.go b/core/ledger/pvtdatastorage/kv_encoding_test.go index d2ec44316d9..46c49f97aba 100644 --- a/core/ledger/pvtdatastorage/kv_encoding_test.go +++ b/core/ledger/pvtdatastorage/kv_encoding_test.go @@ -11,14 +11,14 @@ import ( math "math" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestDataKeyEncoding(t *testing.T) { dataKey1 := &dataKey{nsCollBlk: nsCollBlk{ns: "ns1", coll: "coll1", blkNum: 2}, txNum: 5} datakey2, err := decodeDatakey(encodeDataKey(dataKey1)) - assert.NoError(t, err) - assert.Equal(t, dataKey1, datakey2) + require.NoError(t, err) + require.Equal(t, dataKey1, datakey2) } func TestDatakeyRange(t *testing.T) { @@ -44,10 +44,10 @@ func TestDatakeyRange(t *testing.T) { txNum: txNum, }, ) - assert.Equal(t, bytes.Compare(keyOfPreviousBlock, startKey), -1) - assert.Equal(t, bytes.Compare(keyOfBlock, startKey), 1) - assert.Equal(t, bytes.Compare(keyOfBlock, endKey), -1) - assert.Equal(t, bytes.Compare(keyOfNextBlock, endKey), 1) + require.Equal(t, bytes.Compare(keyOfPreviousBlock, startKey), -1) + require.Equal(t, bytes.Compare(keyOfBlock, startKey), 1) + require.Equal(t, bytes.Compare(keyOfBlock, endKey), -1) + require.Equal(t, bytes.Compare(keyOfNextBlock, endKey), 1) } } @@ -74,10 +74,10 @@ func TestEligibleMissingdataRange(t *testing.T) { isEligible: true, }, ) - assert.Equal(t, bytes.Compare(keyOfNextBlock, startKey), -1) - assert.Equal(t, bytes.Compare(keyOfBlock, startKey), 1) - assert.Equal(t, bytes.Compare(keyOfBlock, endKey), -1) - assert.Equal(t, bytes.Compare(keyOfPreviousBlock, endKey), 1) + require.Equal(t, bytes.Compare(keyOfNextBlock, startKey), -1) + require.Equal(t, bytes.Compare(keyOfBlock, startKey), 1) + require.Equal(t, bytes.Compare(keyOfBlock, endKey), -1) + require.Equal(t, bytes.Compare(keyOfPreviousBlock, endKey), 1) } } @@ -103,7 +103,7 @@ func testEncodeDecodeMissingdataKey(t *testing.T, blkNum uint64) { decodedKey := decodeMissingDataKey( encodeMissingDataKey(key), ) - assert.Equal(t, key, decodedKey) + require.Equal(t, key, decodedKey) }, ) @@ -113,7 +113,7 @@ func testEncodeDecodeMissingdataKey(t *testing.T, blkNum uint64) { decodedKey := decodeMissingDataKey( encodeMissingDataKey(key), ) - assert.Equal(t, key, decodedKey) + require.Equal(t, key, decodedKey) }, ) } diff --git a/core/ledger/pvtdatastorage/store_test.go b/core/ledger/pvtdatastorage/store_test.go index 194e331af2e..65051623694 100644 --- a/core/ledger/pvtdatastorage/store_test.go +++ b/core/ledger/pvtdatastorage/store_test.go @@ -20,7 +20,7 @@ import ( "github.com/hyperledger/fabric/core/ledger" "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/rwsetutil" btltestutil "github.com/hyperledger/fabric/core/ledger/pvtdatapolicy/testutil" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestMain(m *testing.M) { @@ -31,9 +31,9 @@ func TestMain(m *testing.M) { func TestEmptyStore(t *testing.T) { env := NewTestStoreEnv(t, "TestEmptyStore", nil, pvtDataConf()) defer env.Cleanup() - assert := assert.New(t) + require := require.New(t) store := env.TestStore - assert.True(store.isEmpty) + require.True(store.isEmpty) } func TestStoreBasicCommitAndRetrieval(t *testing.T) { @@ -51,7 +51,7 @@ func TestStoreBasicCommitAndRetrieval(t *testing.T) { env := NewTestStoreEnv(t, "TestStoreBasicCommitAndRetrieval", btlPolicy, pvtDataConf()) defer env.Cleanup() - assert := assert.New(t) + require := require.New(t) store := env.TestStore testData := []*ledger.TxPvtData{ produceSamplePvtdata(t, 2, []string{"ns-1:coll-1", "ns-1:coll-2", "ns-2:coll-1", "ns-2:coll-2"}), @@ -81,23 +81,23 @@ func TestStoreBasicCommitAndRetrieval(t *testing.T) { blk2MissingData.Add(3, "ns-1", "coll-1", true) // no pvt data with block 0 - assert.NoError(store.Commit(0, nil, nil)) + require.NoError(store.Commit(0, nil, nil)) // pvt data with block 1 - commit - assert.NoError(store.Commit(1, testData, blk1MissingData)) + require.NoError(store.Commit(1, testData, blk1MissingData)) // pvt data retrieval for block 0 should return nil var nilFilter ledger.PvtNsCollFilter retrievedData, err := store.GetPvtDataByBlockNum(0, nilFilter) - assert.NoError(err) - assert.Nil(retrievedData) + require.NoError(err) + require.Nil(retrievedData) // pvt data retrieval for block 1 should return full pvtdata retrievedData, err = store.GetPvtDataByBlockNum(1, nilFilter) - assert.NoError(err) + require.NoError(err) for i, data := range retrievedData { - assert.Equal(data.SeqInBlock, testData[i].SeqInBlock) - assert.True(proto.Equal(data.WriteSet, testData[i].WriteSet)) + require.Equal(data.SeqInBlock, testData[i].SeqInBlock) + require.True(proto.Equal(data.WriteSet, testData[i].WriteSet)) } // pvt data retrieval for block 1 with filter should return filtered pvtdata @@ -105,24 +105,24 @@ func TestStoreBasicCommitAndRetrieval(t *testing.T) { filter.Add("ns-1", "coll-1") filter.Add("ns-2", "coll-2") retrievedData, err = store.GetPvtDataByBlockNum(1, filter) - assert.NoError(err) + require.NoError(err) expectedRetrievedData := []*ledger.TxPvtData{ produceSamplePvtdata(t, 2, []string{"ns-1:coll-1", "ns-2:coll-2"}), produceSamplePvtdata(t, 4, []string{"ns-1:coll-1", "ns-2:coll-2"}), } for i, data := range retrievedData { - assert.Equal(data.SeqInBlock, expectedRetrievedData[i].SeqInBlock) - assert.True(proto.Equal(data.WriteSet, expectedRetrievedData[i].WriteSet)) + require.Equal(data.SeqInBlock, expectedRetrievedData[i].SeqInBlock) + require.True(proto.Equal(data.WriteSet, expectedRetrievedData[i].WriteSet)) } // pvt data retrieval for block 2 should return ErrOutOfRange retrievedData, err = store.GetPvtDataByBlockNum(2, nilFilter) _, ok := err.(*ErrOutOfRange) - assert.True(ok) - assert.Nil(retrievedData) + require.True(ok) + require.Nil(retrievedData) // pvt data with block 2 - commit - assert.NoError(store.Commit(2, testData, blk2MissingData)) + require.NoError(store.Commit(2, testData, blk2MissingData)) // retrieve the stored missing entries using GetMissingPvtDataInfoForMostRecentBlocks // Only the code path of eligible entries would be covered in this unit-test. For @@ -135,8 +135,8 @@ func TestStoreBasicCommitAndRetrieval(t *testing.T) { expectedMissingPvtDataInfo.Add(2, 3, "ns-1", "coll-1") missingPvtDataInfo, err := store.GetMissingPvtDataInfoForMostRecentBlocks(1) - assert.NoError(err) - assert.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) + require.NoError(err) + require.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) // missing data in block1, tx1 expectedMissingPvtDataInfo.Add(1, 1, "ns-1", "coll-1") @@ -148,12 +148,12 @@ func TestStoreBasicCommitAndRetrieval(t *testing.T) { expectedMissingPvtDataInfo.Add(1, 2, "ns-3", "coll-1") missingPvtDataInfo, err = store.GetMissingPvtDataInfoForMostRecentBlocks(2) - assert.NoError(err) - assert.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) + require.NoError(err) + require.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) missingPvtDataInfo, err = store.GetMissingPvtDataInfoForMostRecentBlocks(10) - assert.NoError(err) - assert.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) + require.NoError(err) + require.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) } func TestCommitPvtDataOfOldBlocks(t *testing.T) { @@ -171,7 +171,7 @@ func TestCommitPvtDataOfOldBlocks(t *testing.T) { ) env := NewTestStoreEnv(t, "TestCommitPvtDataOfOldBlocks", btlPolicy, pvtDataConf()) defer env.Cleanup() - assert := assert.New(t) + require := require.New(t) store := env.TestStore testData := []*ledger.TxPvtData{ @@ -202,13 +202,13 @@ func TestCommitPvtDataOfOldBlocks(t *testing.T) { blk2MissingData.Add(3, "ns-1", "coll-1", true) // COMMIT BLOCK 0 WITH NO DATA - assert.NoError(store.Commit(0, nil, nil)) + require.NoError(store.Commit(0, nil, nil)) // COMMIT BLOCK 1 WITH PVTDATA AND MISSINGDATA - assert.NoError(store.Commit(1, testData, blk1MissingData)) + require.NoError(store.Commit(1, testData, blk1MissingData)) // COMMIT BLOCK 2 WITH PVTDATA AND MISSINGDATA - assert.NoError(store.Commit(2, nil, blk2MissingData)) + require.NoError(store.Commit(2, nil, blk2MissingData)) // CHECK MISSINGDATA ENTRIES ARE CORRECTLY STORED expectedMissingPvtDataInfo := make(ledger.MissingPvtDataInfo) @@ -231,8 +231,8 @@ func TestCommitPvtDataOfOldBlocks(t *testing.T) { expectedMissingPvtDataInfo.Add(2, 3, "ns-1", "coll-1") missingPvtDataInfo, err := store.GetMissingPvtDataInfoForMostRecentBlocks(2) - assert.NoError(err) - assert.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) + require.NoError(err) + require.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) // COMMIT THE MISSINGDATA IN BLOCK 1 AND BLOCK 2 oldBlocksPvtData := make(map[uint64][]*ledger.TxPvtData) @@ -245,7 +245,7 @@ func TestCommitPvtDataOfOldBlocks(t *testing.T) { } err = store.CommitPvtDataOfOldBlocks(oldBlocksPvtData) - assert.NoError(err) + require.NoError(err) // ENSURE THAT THE PREVIOUSLY MISSING PVTDATA OF BLOCK 1 & 2 EXIST IN THE STORE ns1Coll1Blk1Tx1 := &dataKey{nsCollBlk: nsCollBlk{ns: "ns-1", coll: "coll-1", blkNum: 1}, txNum: 1} @@ -254,19 +254,19 @@ func TestCommitPvtDataOfOldBlocks(t *testing.T) { ns3Coll1Blk1Tx2 := &dataKey{nsCollBlk: nsCollBlk{ns: "ns-3", coll: "coll-1", blkNum: 1}, txNum: 2} ns1Coll1Blk2Tx3 := &dataKey{nsCollBlk: nsCollBlk{ns: "ns-1", coll: "coll-1", blkNum: 2}, txNum: 3} - assert.True(testDataKeyExists(t, store, ns1Coll1Blk1Tx1)) - assert.True(testDataKeyExists(t, store, ns2Coll1Blk1Tx1)) - assert.True(testDataKeyExists(t, store, ns1Coll1Blk1Tx2)) - assert.True(testDataKeyExists(t, store, ns3Coll1Blk1Tx2)) - assert.True(testDataKeyExists(t, store, ns1Coll1Blk2Tx3)) + require.True(testDataKeyExists(t, store, ns1Coll1Blk1Tx1)) + require.True(testDataKeyExists(t, store, ns2Coll1Blk1Tx1)) + require.True(testDataKeyExists(t, store, ns1Coll1Blk1Tx2)) + require.True(testDataKeyExists(t, store, ns3Coll1Blk1Tx2)) + require.True(testDataKeyExists(t, store, ns1Coll1Blk2Tx3)) // pvt data retrieval for block 2 should return the just committed pvtdata var nilFilter ledger.PvtNsCollFilter retrievedData, err := store.GetPvtDataByBlockNum(2, nilFilter) - assert.NoError(err) + require.NoError(err) for i, data := range retrievedData { - assert.Equal(data.SeqInBlock, oldBlocksPvtData[2][i].SeqInBlock) - assert.True(proto.Equal(data.WriteSet, oldBlocksPvtData[2][i].WriteSet)) + require.Equal(data.SeqInBlock, oldBlocksPvtData[2][i].SeqInBlock) + require.True(proto.Equal(data.WriteSet, oldBlocksPvtData[2][i].WriteSet)) } expectedMissingPvtDataInfo = make(ledger.MissingPvtDataInfo) @@ -283,11 +283,11 @@ func TestCommitPvtDataOfOldBlocks(t *testing.T) { expectedMissingPvtDataInfo.Add(2, 1, "ns-1", "coll-2") missingPvtDataInfo, err = store.GetMissingPvtDataInfoForMostRecentBlocks(2) - assert.NoError(err) - assert.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) + require.NoError(err) + require.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) // COMMIT BLOCK 3 WITH NO PVTDATA - assert.NoError(store.Commit(3, nil, nil)) + require.NoError(store.Commit(3, nil, nil)) // IN BLOCK 1, NS-1:COLL-2 AND NS-2:COLL-2 SHOULD HAVE EXPIRED BUT NOT PURGED // HENCE, THE FOLLOWING COMMIT SHOULD CREATE ENTRIES IN THE STORE @@ -299,7 +299,7 @@ func TestCommitPvtDataOfOldBlocks(t *testing.T) { } err = store.CommitPvtDataOfOldBlocks(oldBlocksPvtData) - assert.NoError(err) + require.NoError(err) ns1Coll2Blk1Tx1 := &dataKey{nsCollBlk: nsCollBlk{ns: "ns-1", coll: "coll-2", blkNum: 1}, txNum: 1} ns2Coll2Blk1Tx1 := &dataKey{nsCollBlk: nsCollBlk{ns: "ns-2", coll: "coll-2", blkNum: 1}, txNum: 1} @@ -309,13 +309,13 @@ func TestCommitPvtDataOfOldBlocks(t *testing.T) { // though the pvtdata are expired but not purged yet, we do // commit the data and hence the entries would exist in the // store - assert.True(testDataKeyExists(t, store, ns1Coll2Blk1Tx1)) // expired but committed - assert.False(testDataKeyExists(t, store, ns2Coll2Blk1Tx1)) // expired but still missing - assert.False(testDataKeyExists(t, store, ns1Coll2Blk1Tx2)) // expired still missing - assert.True(testDataKeyExists(t, store, ns3Coll2Blk1Tx2)) // never expires + require.True(testDataKeyExists(t, store, ns1Coll2Blk1Tx1)) // expired but committed + require.False(testDataKeyExists(t, store, ns2Coll2Blk1Tx1)) // expired but still missing + require.False(testDataKeyExists(t, store, ns1Coll2Blk1Tx2)) // expired still missing + require.True(testDataKeyExists(t, store, ns3Coll2Blk1Tx2)) // never expires // COMMIT BLOCK 4 WITH NO PVTDATA - assert.NoError(store.Commit(4, nil, nil)) + require.NoError(store.Commit(4, nil, nil)) testWaitForPurgerRoutineToFinish(store) @@ -330,17 +330,17 @@ func TestCommitPvtDataOfOldBlocks(t *testing.T) { } err = store.CommitPvtDataOfOldBlocks(oldBlocksPvtData) - assert.NoError(err) + require.NoError(err) ns1Coll2Blk1Tx1 = &dataKey{nsCollBlk: nsCollBlk{ns: "ns-1", coll: "coll-2", blkNum: 1}, txNum: 1} ns2Coll2Blk1Tx1 = &dataKey{nsCollBlk: nsCollBlk{ns: "ns-2", coll: "coll-2", blkNum: 1}, txNum: 1} ns1Coll2Blk1Tx2 = &dataKey{nsCollBlk: nsCollBlk{ns: "ns-1", coll: "coll-2", blkNum: 1}, txNum: 2} ns3Coll2Blk1Tx2 = &dataKey{nsCollBlk: nsCollBlk{ns: "ns-3", coll: "coll-2", blkNum: 1}, txNum: 2} - assert.False(testDataKeyExists(t, store, ns1Coll2Blk1Tx1)) // purged - assert.False(testDataKeyExists(t, store, ns2Coll2Blk1Tx1)) // purged - assert.False(testDataKeyExists(t, store, ns1Coll2Blk1Tx2)) // purged - assert.True(testDataKeyExists(t, store, ns3Coll2Blk1Tx2)) // never expires + require.False(testDataKeyExists(t, store, ns1Coll2Blk1Tx1)) // purged + require.False(testDataKeyExists(t, store, ns2Coll2Blk1Tx1)) // purged + require.False(testDataKeyExists(t, store, ns1Coll2Blk1Tx2)) // purged + require.True(testDataKeyExists(t, store, ns3Coll2Blk1Tx2)) // never expires } func TestExpiryDataNotIncluded(t *testing.T) { @@ -357,7 +357,7 @@ func TestExpiryDataNotIncluded(t *testing.T) { ) env := NewTestStoreEnv(t, ledgerid, btlPolicy, pvtDataConf()) defer env.Cleanup() - assert := assert.New(t) + require := require.New(t) store := env.TestStore // construct missing data for block 1 @@ -376,27 +376,27 @@ func TestExpiryDataNotIncluded(t *testing.T) { blk2MissingData.Add(1, "ns-1", "coll-2", true) // no pvt data with block 0 - assert.NoError(store.Commit(0, nil, nil)) + require.NoError(store.Commit(0, nil, nil)) // write pvt data for block 1 testDataForBlk1 := []*ledger.TxPvtData{ produceSamplePvtdata(t, 2, []string{"ns-1:coll-1", "ns-1:coll-2", "ns-2:coll-1", "ns-2:coll-2"}), produceSamplePvtdata(t, 4, []string{"ns-1:coll-1", "ns-1:coll-2", "ns-2:coll-1", "ns-2:coll-2"}), } - assert.NoError(store.Commit(1, testDataForBlk1, blk1MissingData)) + require.NoError(store.Commit(1, testDataForBlk1, blk1MissingData)) // write pvt data for block 2 testDataForBlk2 := []*ledger.TxPvtData{ produceSamplePvtdata(t, 3, []string{"ns-1:coll-1", "ns-1:coll-2", "ns-2:coll-1", "ns-2:coll-2"}), produceSamplePvtdata(t, 5, []string{"ns-1:coll-1", "ns-1:coll-2", "ns-2:coll-1", "ns-2:coll-2"}), } - assert.NoError(store.Commit(2, testDataForBlk2, blk2MissingData)) + require.NoError(store.Commit(2, testDataForBlk2, blk2MissingData)) retrievedData, _ := store.GetPvtDataByBlockNum(1, nil) // block 1 data should still be not expired for i, data := range retrievedData { - assert.Equal(data.SeqInBlock, testDataForBlk1[i].SeqInBlock) - assert.True(proto.Equal(data.WriteSet, testDataForBlk1[i].WriteSet)) + require.Equal(data.SeqInBlock, testDataForBlk1[i].SeqInBlock) + require.True(proto.Equal(data.WriteSet, testDataForBlk1[i].WriteSet)) } // none of the missing data entries would have expired @@ -410,11 +410,11 @@ func TestExpiryDataNotIncluded(t *testing.T) { expectedMissingPvtDataInfo.Add(1, 1, "ns-1", "coll-2") missingPvtDataInfo, err := store.GetMissingPvtDataInfoForMostRecentBlocks(10) - assert.NoError(err) - assert.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) + require.NoError(err) + require.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) // Commit block 3 with no pvtdata - assert.NoError(store.Commit(3, nil, nil)) + require.NoError(store.Commit(3, nil, nil)) // After committing block 3, the data for "ns-1:coll1" of block 1 should have expired and should not be returned by the store expectedPvtdataFromBlock1 := []*ledger.TxPvtData{ @@ -422,7 +422,7 @@ func TestExpiryDataNotIncluded(t *testing.T) { produceSamplePvtdata(t, 4, []string{"ns-1:coll-2", "ns-2:coll-1", "ns-2:coll-2"}), } retrievedData, _ = store.GetPvtDataByBlockNum(1, nil) - assert.Equal(expectedPvtdataFromBlock1, retrievedData) + require.Equal(expectedPvtdataFromBlock1, retrievedData) // After committing block 3, the missing data of "ns1-coll1" in block1-tx1 should have expired expectedMissingPvtDataInfo = make(ledger.MissingPvtDataInfo) @@ -433,11 +433,11 @@ func TestExpiryDataNotIncluded(t *testing.T) { expectedMissingPvtDataInfo.Add(1, 1, "ns-1", "coll-2") missingPvtDataInfo, err = store.GetMissingPvtDataInfoForMostRecentBlocks(10) - assert.NoError(err) - assert.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) + require.NoError(err) + require.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) // Commit block 4 with no pvtdata - assert.NoError(store.Commit(4, nil, nil)) + require.NoError(store.Commit(4, nil, nil)) // After committing block 4, the data for "ns-2:coll2" of block 1 should also have expired and should not be returned by the store expectedPvtdataFromBlock1 = []*ledger.TxPvtData{ @@ -445,7 +445,7 @@ func TestExpiryDataNotIncluded(t *testing.T) { produceSamplePvtdata(t, 4, []string{"ns-1:coll-2", "ns-2:coll-1"}), } retrievedData, _ = store.GetPvtDataByBlockNum(1, nil) - assert.Equal(expectedPvtdataFromBlock1, retrievedData) + require.Equal(expectedPvtdataFromBlock1, retrievedData) // Now, for block 2, "ns-1:coll1" should also have expired expectedPvtdataFromBlock2 := []*ledger.TxPvtData{ @@ -453,7 +453,7 @@ func TestExpiryDataNotIncluded(t *testing.T) { produceSamplePvtdata(t, 5, []string{"ns-1:coll-2", "ns-2:coll-1", "ns-2:coll-2"}), } retrievedData, _ = store.GetPvtDataByBlockNum(2, nil) - assert.Equal(expectedPvtdataFromBlock2, retrievedData) + require.Equal(expectedPvtdataFromBlock2, retrievedData) // After committing block 4, the missing data of "ns1-coll1" in block2-tx1 should have expired expectedMissingPvtDataInfo = make(ledger.MissingPvtDataInfo) @@ -464,8 +464,8 @@ func TestExpiryDataNotIncluded(t *testing.T) { expectedMissingPvtDataInfo.Add(1, 1, "ns-1", "coll-2") missingPvtDataInfo, err = store.GetMissingPvtDataInfoForMostRecentBlocks(10) - assert.NoError(err) - assert.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) + require.NoError(err) + require.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) } @@ -483,11 +483,11 @@ func TestStorePurge(t *testing.T) { ) env := NewTestStoreEnv(t, ledgerid, btlPolicy, pvtDataConf()) defer env.Cleanup() - assert := assert.New(t) + require := require.New(t) s := env.TestStore // no pvt data with block 0 - assert.NoError(s.Commit(0, nil, nil)) + require.NoError(s.Commit(0, nil, nil)) // construct missing data for block 1 blk1MissingData := make(ledger.TxMissingPvtDataMap) @@ -503,10 +503,10 @@ func TestStorePurge(t *testing.T) { produceSamplePvtdata(t, 2, []string{"ns-1:coll-1", "ns-1:coll-2", "ns-2:coll-1", "ns-2:coll-2"}), produceSamplePvtdata(t, 4, []string{"ns-1:coll-1", "ns-1:coll-2", "ns-2:coll-1", "ns-2:coll-2"}), } - assert.NoError(s.Commit(1, testDataForBlk1, blk1MissingData)) + require.NoError(s.Commit(1, testDataForBlk1, blk1MissingData)) // write pvt data for block 2 - assert.NoError(s.Commit(2, nil, nil)) + require.NoError(s.Commit(2, nil, nil)) // data for ns-1:coll-1 and ns-2:coll-2 should exist in store ns1Coll1 := &dataKey{nsCollBlk: nsCollBlk{ns: "ns-1", coll: "coll-1", blkNum: 1}, txNum: 2} ns2Coll2 := &dataKey{nsCollBlk: nsCollBlk{ns: "ns-2", coll: "coll-2", blkNum: 1}, txNum: 2} @@ -520,58 +520,58 @@ func TestStorePurge(t *testing.T) { ns3Coll2inelgMD := &missingDataKey{nsCollBlk: nsCollBlk{ns: "ns-3", coll: "coll-2", blkNum: 1}, isEligible: false} testWaitForPurgerRoutineToFinish(s) - assert.True(testDataKeyExists(t, s, ns1Coll1)) - assert.True(testDataKeyExists(t, s, ns2Coll2)) + require.True(testDataKeyExists(t, s, ns1Coll1)) + require.True(testDataKeyExists(t, s, ns2Coll2)) - assert.True(testMissingDataKeyExists(t, s, ns1Coll1elgMD)) - assert.True(testMissingDataKeyExists(t, s, ns1Coll2elgMD)) + require.True(testMissingDataKeyExists(t, s, ns1Coll1elgMD)) + require.True(testMissingDataKeyExists(t, s, ns1Coll2elgMD)) - assert.True(testMissingDataKeyExists(t, s, ns3Coll1inelgMD)) - assert.True(testMissingDataKeyExists(t, s, ns3Coll2inelgMD)) + require.True(testMissingDataKeyExists(t, s, ns3Coll1inelgMD)) + require.True(testMissingDataKeyExists(t, s, ns3Coll2inelgMD)) // write pvt data for block 3 - assert.NoError(s.Commit(3, nil, nil)) + require.NoError(s.Commit(3, nil, nil)) // data for ns-1:coll-1 and ns-2:coll-2 should exist in store (because purger should not be launched at block 3) testWaitForPurgerRoutineToFinish(s) - assert.True(testDataKeyExists(t, s, ns1Coll1)) - assert.True(testDataKeyExists(t, s, ns2Coll2)) + require.True(testDataKeyExists(t, s, ns1Coll1)) + require.True(testDataKeyExists(t, s, ns2Coll2)) // eligible missingData entries for ns-1:coll-1, ns-1:coll-2 (neverExpires) should exist in store - assert.True(testMissingDataKeyExists(t, s, ns1Coll1elgMD)) - assert.True(testMissingDataKeyExists(t, s, ns1Coll2elgMD)) + require.True(testMissingDataKeyExists(t, s, ns1Coll1elgMD)) + require.True(testMissingDataKeyExists(t, s, ns1Coll2elgMD)) // ineligible missingData entries for ns-3:col-1, ns-3:coll-2 (neverExpires) should exist in store - assert.True(testMissingDataKeyExists(t, s, ns3Coll1inelgMD)) - assert.True(testMissingDataKeyExists(t, s, ns3Coll2inelgMD)) + require.True(testMissingDataKeyExists(t, s, ns3Coll1inelgMD)) + require.True(testMissingDataKeyExists(t, s, ns3Coll2inelgMD)) // write pvt data for block 4 - assert.NoError(s.Commit(4, nil, nil)) + require.NoError(s.Commit(4, nil, nil)) // data for ns-1:coll-1 should not exist in store (because purger should be launched at block 4) // but ns-2:coll-2 should exist because it expires at block 5 testWaitForPurgerRoutineToFinish(s) - assert.False(testDataKeyExists(t, s, ns1Coll1)) - assert.True(testDataKeyExists(t, s, ns2Coll2)) + require.False(testDataKeyExists(t, s, ns1Coll1)) + require.True(testDataKeyExists(t, s, ns2Coll2)) // eligible missingData entries for ns-1:coll-1 should have expired and ns-1:coll-2 (neverExpires) should exist in store - assert.False(testMissingDataKeyExists(t, s, ns1Coll1elgMD)) - assert.True(testMissingDataKeyExists(t, s, ns1Coll2elgMD)) + require.False(testMissingDataKeyExists(t, s, ns1Coll1elgMD)) + require.True(testMissingDataKeyExists(t, s, ns1Coll2elgMD)) // ineligible missingData entries for ns-3:col-1 should have expired and ns-3:coll-2 (neverExpires) should exist in store - assert.False(testMissingDataKeyExists(t, s, ns3Coll1inelgMD)) - assert.True(testMissingDataKeyExists(t, s, ns3Coll2inelgMD)) + require.False(testMissingDataKeyExists(t, s, ns3Coll1inelgMD)) + require.True(testMissingDataKeyExists(t, s, ns3Coll2inelgMD)) // write pvt data for block 5 - assert.NoError(s.Commit(5, nil, nil)) + require.NoError(s.Commit(5, nil, nil)) // ns-2:coll-2 should exist because though the data expires at block 5 but purger is launched every second block testWaitForPurgerRoutineToFinish(s) - assert.False(testDataKeyExists(t, s, ns1Coll1)) - assert.True(testDataKeyExists(t, s, ns2Coll2)) + require.False(testDataKeyExists(t, s, ns1Coll1)) + require.True(testDataKeyExists(t, s, ns2Coll2)) // write pvt data for block 6 - assert.NoError(s.Commit(6, nil, nil)) + require.NoError(s.Commit(6, nil, nil)) // ns-2:coll-2 should not exists now (because purger should be launched at block 6) testWaitForPurgerRoutineToFinish(s) - assert.False(testDataKeyExists(t, s, ns1Coll1)) - assert.False(testDataKeyExists(t, s, ns2Coll2)) + require.False(testDataKeyExists(t, s, ns1Coll1)) + require.False(testDataKeyExists(t, s, ns2Coll2)) // "ns-2:coll-1" should never have been purged (because, it was no btl was declared for this) - assert.True(testDataKeyExists(t, s, &dataKey{nsCollBlk: nsCollBlk{ns: "ns-1", coll: "coll-2", blkNum: 1}, txNum: 2})) + require.True(testDataKeyExists(t, s, &dataKey{nsCollBlk: nsCollBlk{ns: "ns-1", coll: "coll-2", blkNum: 1}, txNum: 2})) } func TestStoreState(t *testing.T) { @@ -583,13 +583,13 @@ func TestStoreState(t *testing.T) { ) env := NewTestStoreEnv(t, "TestStoreState", btlPolicy, pvtDataConf()) defer env.Cleanup() - assert := assert.New(t) + require := require.New(t) store := env.TestStore testData := []*ledger.TxPvtData{ produceSamplePvtdata(t, 0, []string{"ns-1:coll-1", "ns-1:coll-2"}), } _, ok := store.Commit(1, testData, nil).(*ErrIllegalArgs) - assert.True(ok) + require.True(ok) } func TestPendingBatch(t *testing.T) { @@ -601,15 +601,15 @@ func TestPendingBatch(t *testing.T) { ) env := NewTestStoreEnv(t, "TestPendingBatch", btlPolicy, pvtDataConf()) defer env.Cleanup() - assert := assert.New(t) + require := require.New(t) s := env.TestStore existingLastBlockNum := uint64(25) batch := leveldbhelper.NewUpdateBatch() batch.Put(lastCommittedBlkkey, encodeLastCommittedBlockVal(existingLastBlockNum)) - assert.NoError(s.db.WriteBatch(batch, true)) + require.NoError(s.db.WriteBatch(batch, true)) s.lastCommittedBlock = existingLastBlockNum s.isEmpty = false - testLastCommittedBlockHeight(existingLastBlockNum+1, assert, s) + testLastCommittedBlockHeight(existingLastBlockNum+1, require, s) // assume that a block has been prepared in v142 and the peer was // killed for upgrade. When the pvtdataStore is opened again with @@ -621,33 +621,33 @@ func TestPendingBatch(t *testing.T) { dataValue := &rwset.CollectionPvtReadWriteSet{CollectionName: "coll-1", Rwset: []byte("pvtdata")} keyBytes := encodeDataKey(dataKey) valueBytes, err := encodeDataValue(dataValue) - assert.NoError(err) + require.NoError(err) batch.Put(keyBytes, valueBytes) // store pendingBatch marker batch.Put(pendingCommitKey, emptyValue) // write to the store - assert.NoError(s.db.WriteBatch(batch, true)) - testLastCommittedBlockHeight(existingLastBlockNum+1, assert, s) + require.NoError(s.db.WriteBatch(batch, true)) + testLastCommittedBlockHeight(existingLastBlockNum+1, require, s) // as the block commit is pending, we cannot read the pvtData hasPendingBatch, err := s.hasPendingCommit() - assert.NoError(err) - assert.Equal(true, hasPendingBatch) + require.NoError(err) + require.Equal(true, hasPendingBatch) pvtData, err := s.GetPvtDataByBlockNum(26, nil) _, ok := err.(*ErrOutOfRange) - assert.True(ok) - assert.Nil(pvtData) + require.True(ok) + require.Nil(pvtData) // emulate a version upgrade env.CloseAndReopen() s = env.TestStore - testLastCommittedBlockHeight(existingLastBlockNum+2, assert, s) + testLastCommittedBlockHeight(existingLastBlockNum+2, require, s) hasPendingBatch, err = s.hasPendingCommit() - assert.NoError(err) - assert.Equal(false, hasPendingBatch) + require.NoError(err) + require.Equal(false, hasPendingBatch) testDataKeyExists(t, s, dataKey) expectedPvtData := &rwset.TxPvtReadWriteSet{ @@ -661,10 +661,10 @@ func TestPendingBatch(t *testing.T) { }, } pvtData, err = s.GetPvtDataByBlockNum(26, nil) - assert.NoError(err) - assert.Equal(1, len(pvtData)) - assert.Equal(uint64(1), pvtData[0].SeqInBlock) - assert.True(proto.Equal(expectedPvtData, pvtData[0].WriteSet)) + require.NoError(err) + require.Equal(1, len(pvtData)) + require.Equal(uint64(1), pvtData[0].SeqInBlock) + require.True(proto.Equal(expectedPvtData, pvtData[0].WriteSet)) } func TestCollElgEnabled(t *testing.T) { @@ -687,13 +687,13 @@ func testCollElgEnabled(t *testing.T, conf *PrivateDataConfig) { ) env := NewTestStoreEnv(t, ledgerid, btlPolicy, conf) defer env.Cleanup() - assert := assert.New(t) + require := require.New(t) testStore := env.TestStore // Initial state: eligible for {ns-1:coll-1 and ns-2:coll-1 } // no pvt data with block 0 - assert.NoError(testStore.Commit(0, nil, nil)) + require.NoError(testStore.Commit(0, nil, nil)) // construct and commit block 1 blk1MissingData := make(ledger.TxMissingPvtDataMap) @@ -704,7 +704,7 @@ func testCollElgEnabled(t *testing.T, conf *PrivateDataConfig) { testDataForBlk1 := []*ledger.TxPvtData{ produceSamplePvtdata(t, 2, []string{"ns-1:coll-1"}), } - assert.NoError(testStore.Commit(1, testDataForBlk1, blk1MissingData)) + require.NoError(testStore.Commit(1, testDataForBlk1, blk1MissingData)) // construct and commit block 2 blk2MissingData := make(ledger.TxMissingPvtDataMap) @@ -714,7 +714,7 @@ func testCollElgEnabled(t *testing.T, conf *PrivateDataConfig) { testDataForBlk2 := []*ledger.TxPvtData{ produceSamplePvtdata(t, 3, []string{"ns-1:coll-1"}), } - assert.NoError(testStore.Commit(2, testDataForBlk2, blk2MissingData)) + require.NoError(testStore.Commit(2, testDataForBlk2, blk2MissingData)) // Retrieve and verify missing data reported // Expected missing data should be only blk1-tx1 (because, the other missing data is marked as ineliigible) @@ -722,8 +722,8 @@ func testCollElgEnabled(t *testing.T, conf *PrivateDataConfig) { expectedMissingPvtDataInfo.Add(1, 1, "ns-1", "coll-1") expectedMissingPvtDataInfo.Add(1, 1, "ns-2", "coll-1") missingPvtDataInfo, err := testStore.GetMissingPvtDataInfoForMostRecentBlocks(10) - assert.NoError(err) - assert.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) + require.NoError(err) + require.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) // Enable eligibility for {ns-1:coll2} testStore.ProcessCollsEligibilityEnabled( @@ -739,8 +739,8 @@ func testCollElgEnabled(t *testing.T, conf *PrivateDataConfig) { expectedMissingPvtDataInfo.Add(1, 4, "ns-1", "coll-2") expectedMissingPvtDataInfo.Add(2, 1, "ns-1", "coll-2") missingPvtDataInfo, err = testStore.GetMissingPvtDataInfoForMostRecentBlocks(10) - assert.NoError(err) - assert.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) + require.NoError(err) + require.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) // Enable eligibility for {ns-2:coll2} testStore.ProcessCollsEligibilityEnabled(6, @@ -755,27 +755,27 @@ func testCollElgEnabled(t *testing.T, conf *PrivateDataConfig) { expectedMissingPvtDataInfo.Add(1, 4, "ns-2", "coll-2") expectedMissingPvtDataInfo.Add(2, 1, "ns-2", "coll-2") missingPvtDataInfo, err = testStore.GetMissingPvtDataInfoForMostRecentBlocks(10) - assert.NoError(err) - assert.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) + require.NoError(err) + require.Equal(expectedMissingPvtDataInfo, missingPvtDataInfo) } -func testLastCommittedBlockHeight(expectedBlockHt uint64, assert *assert.Assertions, store *Store) { +func testLastCommittedBlockHeight(expectedBlockHt uint64, require *require.Assertions, store *Store) { blkHt, err := store.LastCommittedBlockHeight() - assert.NoError(err) - assert.Equal(expectedBlockHt, blkHt) + require.NoError(err) + require.Equal(expectedBlockHt, blkHt) } func testDataKeyExists(t *testing.T, s *Store, dataKey *dataKey) bool { dataKeyBytes := encodeDataKey(dataKey) val, err := s.db.Get(dataKeyBytes) - assert.NoError(t, err) + require.NoError(t, err) return len(val) != 0 } func testMissingDataKeyExists(t *testing.T, s *Store, missingDataKey *missingDataKey) bool { dataKeyBytes := encodeMissingDataKey(missingDataKey) val, err := s.db.Get(dataKeyBytes) - assert.NoError(t, err) + require.NoError(t, err) return len(val) != 0 } @@ -798,6 +798,6 @@ func produceSamplePvtdata(t *testing.T, txNum uint64, nsColls []string) *ledger. builder.AddToPvtAndHashedWriteSet(ns, coll, fmt.Sprintf("key-%s-%s", ns, coll), []byte(fmt.Sprintf("value-%s-%s", ns, coll))) } simRes, err := builder.GetTxSimulationResults() - assert.NoError(t, err) + require.NoError(t, err) return &ledger.TxPvtData{SeqInBlock: txNum, WriteSet: simRes.PvtSimulationResults} } diff --git a/core/ledger/pvtdatastorage/v11_V12_test.go b/core/ledger/pvtdatastorage/v11_V12_test.go index a8373e557e3..840c0857de8 100644 --- a/core/ledger/pvtdatastorage/v11_V12_test.go +++ b/core/ledger/pvtdatastorage/v11_V12_test.go @@ -17,7 +17,7 @@ import ( "github.com/hyperledger/fabric/common/ledger/testutil" "github.com/hyperledger/fabric/core/ledger" btltestutil "github.com/hyperledger/fabric/core/ledger/pvtdatapolicy/testutil" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // TestV11v12 test that we are able to read the mixed format data (for v11 and v12) @@ -49,10 +49,10 @@ func TestV11v12(t *testing.T) { StorePath: filepath.Join(testWorkingDir, "pvtdataStore"), } p, err := NewProvider(conf) - assert.NoError(t, err) + require.NoError(t, err) defer p.Close() s, err := p.OpenStore(ledgerid) - assert.NoError(t, err) + require.NoError(t, err) s.Init(btlPolicy) for blk := 0; blk < 10; blk++ { @@ -66,18 +66,18 @@ func TestV11v12(t *testing.T) { _, err = s.GetPvtDataByBlockNum(uint64(15), nil) _, ok := err.(*ErrOutOfRange) - assert.True(t, ok) + require.True(t, ok) } func checkDataNotExists(t *testing.T, s *Store, blkNum int) { data, err := s.GetPvtDataByBlockNum(uint64(blkNum), nil) - assert.NoError(t, err) - assert.Nil(t, data) + require.NoError(t, err) + require.Nil(t, data) } func checkDataExists(t *testing.T, s *Store, blkNum int) { data, err := s.GetPvtDataByBlockNum(uint64(blkNum), nil) - assert.NoError(t, err) - assert.NotNil(t, data) + require.NoError(t, err) + require.NotNil(t, data) t.Logf("pvtdata = %s\n", spew.Sdump(data)) }