Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: manish <manish.sethi@gmail.com>
  • Loading branch information
manish-sethi authored and denyeart committed Nov 4, 2020
1 parent a37ea5c commit c15e44e
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 94 deletions.
4 changes: 2 additions & 2 deletions core/ledger/kvledger/tests/customtx_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestReadWriteCustomTxProcessor(t *testing.T) {
defer env.cleanup()
env.initLedgerMgmt()

l := env.createTestLedger("ledger1", t)
l := env.createTestLedger("ledger1")
l.simulateDataTx("tx0", func(s *simulator) {
s.setState("ns", "key1", "value1")
s.setState("ns", "key2", "value2")
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestRangeReadAndWriteCustomTxProcessor(t *testing.T) {
defer env.cleanup()
env.initLedgerMgmt()

l := env.createTestLedger("ledger1", t)
l := env.createTestLedger("ledger1")
l.simulateDataTx("tx0", func(s *simulator) {
s.setState("ns", "key1", "value1")
s.setState("ns", "key2", "value2")
Expand Down
22 changes: 11 additions & 11 deletions core/ledger/kvledger/tests/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const (
)

type env struct {
assert *require.Assertions
t *testing.T
initializer *ledgermgmt.Initializer
ledgerMgr *ledgermgmt.LedgerMgr
}
Expand All @@ -65,7 +65,7 @@ func newEnvWithInitializer(t *testing.T, initializer *ledgermgmt.Initializer) *e
populateMissingsWithTestDefaults(t, initializer)

return &env{
assert: require.New(t),
t: t,
initializer: initializer,
}
}
Expand All @@ -90,35 +90,35 @@ func (e *env) closeAllLedgersAndRemoveDirContents(flags rebuildable) {
indexPath := e.getBlockIndexDBPath()
logger.Infof("Deleting blockstore indexdb path [%s]", indexPath)
e.verifyNonEmptyDirExists(indexPath)
e.assert.NoError(fileutil.RemoveContents(indexPath))
require.NoError(e.t, fileutil.RemoveContents(indexPath))
}

if flags&rebuildableStatedb == rebuildableStatedb {
statedbPath := e.getLevelstateDBPath()
logger.Infof("Deleting statedb path [%s]", statedbPath)
e.verifyNonEmptyDirExists(statedbPath)
e.assert.NoError(fileutil.RemoveContents(statedbPath))
require.NoError(e.t, fileutil.RemoveContents(statedbPath))
}

if flags&rebuildableConfigHistory == rebuildableConfigHistory {
configHistoryPath := e.getConfigHistoryDBPath()
logger.Infof("Deleting configHistory db path [%s]", configHistoryPath)
e.verifyNonEmptyDirExists(configHistoryPath)
e.assert.NoError(fileutil.RemoveContents(configHistoryPath))
require.NoError(e.t, fileutil.RemoveContents(configHistoryPath))
}

if flags&rebuildableBookkeeper == rebuildableBookkeeper {
bookkeeperPath := e.getBookkeeperDBPath()
logger.Infof("Deleting bookkeeper db path [%s]", bookkeeperPath)
e.verifyNonEmptyDirExists(bookkeeperPath)
e.assert.NoError(fileutil.RemoveContents(bookkeeperPath))
require.NoError(e.t, fileutil.RemoveContents(bookkeeperPath))
}

if flags&rebuildableHistoryDB == rebuildableHistoryDB {
historyPath := e.getHistoryDBPath()
logger.Infof("Deleting history db path [%s]", historyPath)
e.verifyNonEmptyDirExists(historyPath)
e.assert.NoError(fileutil.RemoveContents(historyPath))
require.NoError(e.t, fileutil.RemoveContents(historyPath))
}

e.verifyRebuilableDirEmpty(flags)
Expand Down Expand Up @@ -162,14 +162,14 @@ func (e *env) verifyRebuilableDirEmpty(flags rebuildable) {

func (e *env) verifyNonEmptyDirExists(path string) {
empty, err := fileutil.DirEmpty(path)
e.assert.NoError(err)
e.assert.False(empty)
require.NoError(e.t, err)
require.False(e.t, empty)
}

func (e *env) verifyDirEmpty(path string) {
empty, err := fileutil.DirEmpty(path)
e.assert.NoError(err)
e.assert.True(empty)
require.NoError(e.t, err)
require.True(e.t, empty)
}

func (e *env) initLedgerMgmt() {
Expand Down
4 changes: 2 additions & 2 deletions core/ledger/kvledger/tests/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func TestLedgerAPIs(t *testing.T) {
env.initLedgerMgmt()

// create two ledgers
testLedger1 := env.createTestLedger("ledger1", t)
testLedger2 := env.createTestLedger("ledger2", t)
testLedger1 := env.createTestLedger("ledger1")
testLedger2 := env.createTestLedger("ledger2")

// populate ledgers with sample data
dataHelper := newSampleDataHelper(t)
Expand Down
20 changes: 13 additions & 7 deletions core/ledger/kvledger/tests/missing_pvtdata_retrieval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ func TestGetMissingPvtData(t *testing.T) {
blk2 := l.cutBlockAndCommitLegacy()

l.verifyPvtState("cc1", "coll1", "key2", "value2") // key2 should have been committed

l.simulateDataTx("", func(s *simulator) {
l.assertError(s.GetPrivateData("cc1", "coll1", "key1")) // key1 would be stale with respect to hashed version
// key1 would be stale with respect to hashed version
_, err := s.GetPrivateData("cc1", "coll1", "key1")
require.EqualError(t, err, "private data matching public hash version is not available. Public hash version = {BlockNum: 2, TxNum: 0}, Private data version = <nil>")
})

l.simulateDataTx("", func(s *simulator) {
l.assertError(s.GetPrivateData("cc1", "coll1", "key3")) // key3 would be stale with respect to hashed version
// key3 would be stale with respect to hashed version
_, err := s.GetPrivateData("cc1", "coll1", "key3")
require.EqualError(t, err, "private data matching public hash version is not available. Public hash version = {BlockNum: 2, TxNum: 2}, Private data version = <nil>")
})

// verify missing pvtdata info
Expand All @@ -69,7 +75,7 @@ func TestGetMissingPvtData(t *testing.T) {
env := newEnv(t)
defer env.cleanup()
env.initLedgerMgmt()
l := env.createTestLedger("ledger1", t)
l := env.createTestLedger("ledger1")

blk, expectedMissingPvtDataInfo := setup(l)

Expand All @@ -95,7 +101,7 @@ func TestGetMissingPvtData(t *testing.T) {
require.NoError(t, err)
env.initLedgerMgmt()

l = env.openTestLedger("ledger1", t)
l = env.openTestLedger("ledger1")
l.verifyLedgerHeight(3)

// verify block & pvtdata
Expand Down Expand Up @@ -138,7 +144,7 @@ func TestGetMissingPvtData(t *testing.T) {
env := newEnvWithInitializer(t, initializer)
defer env.cleanup()
env.initLedgerMgmt()
l := env.createTestLedger("ledger1", t)
l := env.createTestLedger("ledger1")

_, expectedMissingPvtDataInfo := setup(l)

Expand All @@ -152,7 +158,7 @@ func TestGetMissingPvtData(t *testing.T) {
env.initializer.Config.PrivateDataConfig.DeprioritizedDataReconcilerInterval = 0 * time.Second
env.initLedgerMgmt()

l = env.openTestLedger("ledger1", t)
l = env.openTestLedger("ledger1")
for i := 0; i < 5; i++ {
l.verifyMissingPvtDataSameAs(2, expectedMissingPvtDataInfo)
}
Expand All @@ -161,7 +167,7 @@ func TestGetMissingPvtData(t *testing.T) {
env.initializer.Config.PrivateDataConfig.DeprioritizedDataReconcilerInterval = 120 * time.Minute
env.initLedgerMgmt()

l = env.openTestLedger("ledger1", t)
l = env.openTestLedger("ledger1")
for i := 0; i < 5; i++ {
l.verifyMissingPvtDataSameAs(2, ledger.MissingPvtDataInfo{})
}
Expand Down
48 changes: 34 additions & 14 deletions core/ledger/kvledger/tests/pvtdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
"testing"

"github.com/hyperledger/fabric/core/ledger"
"github.com/stretchr/testify/require"
)

func TestMissingCollConfig(t *testing.T) {
env := newEnv(t)
defer env.cleanup()
env.initLedgerMgmt()
l := env.createTestLedger("ledger1", t)
l := env.createTestLedger("ledger1")

collConf := []*collConf{{name: "coll1", btl: 5}}

Expand All @@ -26,9 +27,15 @@ func TestMissingCollConfig(t *testing.T) {

// pvt data operations should give error as no collection config defined
l.simulateDataTx("", func(s *simulator) {
l.assertError(s.GetPrivateData("cc1", "coll1", "key"))
l.assertError(s.SetPrivateData("cc1", "coll1", "key", []byte("value")))
l.assertError(s.DeletePrivateData("cc1", "coll1", "key"))
expectedErr := "collection config not defined for chaincode [cc1], pass the collection configuration upon chaincode definition/instantiation"
_, err := s.GetPrivateData("cc1", "coll1", "key")
require.EqualError(t, err, expectedErr)

err = s.SetPrivateData("cc1", "coll1", "key", []byte("value"))
require.EqualError(t, err, expectedErr)

err = s.DeletePrivateData("cc1", "coll1", "key")
require.EqualError(t, err, expectedErr)
})

// upgrade cc1 (add collConf)
Expand All @@ -38,20 +45,32 @@ func TestMissingCollConfig(t *testing.T) {
// operations on coll1 should not give error
// operations on coll2 should give error (because, only coll1 is defined in collConf)
l.simulateDataTx("", func(s *simulator) {
l.assertNoError(s.GetPrivateData("cc1", "coll1", "key1"))
l.assertNoError(s.SetPrivateData("cc1", "coll1", "key2", []byte("value")))
l.assertNoError(s.DeletePrivateData("cc1", "coll1", "key3"))
l.assertError(s.GetPrivateData("cc1", "coll2", "key"))
l.assertError(s.SetPrivateData("cc1", "coll2", "key", []byte("value")))
l.assertError(s.DeletePrivateData("cc1", "coll2", "key"))
_, err := s.GetPrivateData("cc1", "coll1", "key1")
require.NoError(t, err)

err = s.SetPrivateData("cc1", "coll1", "key2", []byte("value"))
require.NoError(t, err)

err = s.DeletePrivateData("cc1", "coll1", "key3")
require.NoError(t, err)

expectedErr := "collection [coll2] not defined in the collection config for chaincode [cc1]"
_, err = s.GetPrivateData("cc1", "coll2", "key")
require.EqualError(t, err, expectedErr)

err = s.SetPrivateData("cc1", "coll2", "key", []byte("value"))
require.EqualError(t, err, expectedErr)

err = s.DeletePrivateData("cc1", "coll2", "key")
require.EqualError(t, err, expectedErr)
})
}

func TestTxWithMissingPvtdata(t *testing.T) {
env := newEnv(t)
defer env.cleanup()
env.initLedgerMgmt()
l := env.createTestLedger("ledger1", t)
l := env.createTestLedger("ledger1")

collConf := []*collConf{{name: "coll1", btl: 5}}

Expand All @@ -73,7 +92,8 @@ func TestTxWithMissingPvtdata(t *testing.T) {

l.verifyPvtState("cc1", "coll1", "key2", "value2") // key2 should have been committed
l.simulateDataTx("", func(s *simulator) {
l.assertError(s.GetPrivateData("cc1", "coll1", "key1")) // key1 would be stale with respect to hashed version
_, err := s.GetPrivateData("cc1", "coll1", "key1") // key1 would be stale with respect to hashed version
require.EqualError(t, err, "private data matching public hash version is not available. Public hash version = {BlockNum: 2, TxNum: 0}, Private data version = <nil>")
})

// verify missing pvtdata info
Expand All @@ -96,7 +116,7 @@ func TestTxWithWrongPvtdata(t *testing.T) {
env := newEnv(t)
defer env.cleanup()
env.initLedgerMgmt()
l := env.createTestLedger("ledger1", t)
l := env.createTestLedger("ledger1")

collConf := []*collConf{{name: "coll1", btl: 5}}

Expand All @@ -122,7 +142,7 @@ func TestBTL(t *testing.T) {
env := newEnv(t)
defer env.cleanup()
env.initLedgerMgmt()
l := env.createTestLedger("ledger1", t)
l := env.createTestLedger("ledger1")
collConf := []*collConf{{name: "coll1", btl: 0}, {name: "coll2", btl: 5}}

// deploy cc1 with 'collConf'
Expand Down
14 changes: 7 additions & 7 deletions core/ledger/kvledger/tests/rebuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestRebuildComponents(t *testing.T) {
defer env.cleanup()
env.initLedgerMgmt()

l1, l2 := env.createTestLedger("ledger1", t), env.createTestLedger("ledger2", t)
l1, l2 := env.createTestLedger("ledger1"), env.createTestLedger("ledger2")
dataHelper := newSampleDataHelper(t)

dataHelper.populateLedger(l1)
Expand All @@ -27,7 +27,7 @@ func TestRebuildComponents(t *testing.T) {
t.Run("rebuild only statedb",
func(t *testing.T) {
env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb)
l1, l2 := env.openTestLedger("ledger1", t), env.openTestLedger("ledger2", t)
l1, l2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2")
dataHelper.verifyLedgerContent(l1)
dataHelper.verifyLedgerContent(l2)
},
Expand All @@ -36,7 +36,7 @@ func TestRebuildComponents(t *testing.T) {
t.Run("rebuild statedb and config history",
func(t *testing.T) {
env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb | rebuildableConfigHistory)
l1, l2 := env.openTestLedger("ledger1", t), env.openTestLedger("ledger2", t)
l1, l2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2")
dataHelper.verifyLedgerContent(l1)
dataHelper.verifyLedgerContent(l2)
},
Expand All @@ -45,7 +45,7 @@ func TestRebuildComponents(t *testing.T) {
t.Run("rebuild statedb and block index",
func(t *testing.T) {
env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb | rebuildableBlockIndex)
l1, l2 := env.openTestLedger("ledger1", t), env.openTestLedger("ledger2", t)
l1, l2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2")
dataHelper.verifyLedgerContent(l1)
dataHelper.verifyLedgerContent(l2)
},
Expand All @@ -54,7 +54,7 @@ func TestRebuildComponents(t *testing.T) {
t.Run("rebuild statedb and historydb",
func(t *testing.T) {
env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb | rebuildableHistoryDB)
l1, l2 := env.openTestLedger("ledger1", t), env.openTestLedger("ledger2", t)
l1, l2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2")
dataHelper.verifyLedgerContent(l1)
dataHelper.verifyLedgerContent(l2)
},
Expand All @@ -65,7 +65,7 @@ func TestRebuildComponentsWithBTL(t *testing.T) {
env := newEnv(t)
defer env.cleanup()
env.initLedgerMgmt()
l := env.createTestLedger("ledger1", t)
l := env.createTestLedger("ledger1")
collConf := []*collConf{{name: "coll1", btl: 0}, {name: "coll2", btl: 1}}

// deploy cc1 with 'collConf'
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestRebuildComponentsWithBTL(t *testing.T) {
// rebuild statedb and bookkeeper
env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb | rebuildableBookkeeper)

l = env.openTestLedger("ledger1", t)
l = env.openTestLedger("ledger1")
l.verifyPvtState("cc1", "coll1", "key1", "value1") // key1 should still exist in the state
l.verifyPvtState("cc1", "coll2", "key2", "") // key2 should have been purged from the state
l.verifyBlockAndPvtData(2, nil, func(r *retrievedBlockAndPvtdata) { // retrieve the pvtdata for block 2 from pvtdata storage
Expand Down
11 changes: 5 additions & 6 deletions core/ledger/kvledger/tests/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestResetAllLedgers(t *testing.T) {
ledgerIDs := make([]string, numLedgers)
for i := 0; i < numLedgers; i++ {
ledgerIDs[i] = fmt.Sprintf("ledger-%d", i)
l := env.createTestLedger(ledgerIDs[i], t)
l := env.createTestLedger(ledgerIDs[i])
dataHelper.populateLedger(l)
dataHelper.verifyLedgerContent(l)
gb, err := l.lgr.GetBlockByNumber(0)
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestResetAllLedgers(t *testing.T) {
// and final blockchainInfo and ledger state same as before reset
for i := 0; i < 10; i++ {
ledgerID := fmt.Sprintf("ledger-%d", i)
l := env.openTestLedger(ledgerID, t)
l := env.openTestLedger(ledgerID)
l.verifyLedgerHeight(1)
require.Equal(t, blockchainsInfo[i].Height, preResetHt[ledgerID])
gb, err := l.lgr.GetBlockByNumber(0)
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestResetAllLedgersWithBTL(t *testing.T) {
env := newEnv(t)
defer env.cleanup()
env.initLedgerMgmt()
l := env.createTestLedger("ledger1", t)
l := env.createTestLedger("ledger1")
collConf := []*collConf{{name: "coll1", btl: 0}, {name: "coll2", btl: 1}}

// deploy cc1 with 'collConf'
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestResetAllLedgersWithBTL(t *testing.T) {
require.NoError(t, err)
t.Logf("preResetHt = %#v", preResetHt)
require.Equal(t, uint64(5), preResetHt["ledger1"])
l = env.openTestLedger("ledger1", t)
l = env.openTestLedger("ledger1")
l.verifyLedgerHeight(1)

// recommit blocks
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestResetLedgerWithoutDroppingDBs(t *testing.T) {
dataHelper := newSampleDataHelper(t)

// create ledgers and pouplate with sample data
l := env.createTestLedger("ledger-1", t)
l := env.createTestLedger("ledger-1")
dataHelper.populateLedger(l)
dataHelper.verifyLedgerContent(l)
env.closeLedgerMgmt()
Expand All @@ -211,7 +211,6 @@ func TestResetLedgerWithoutDroppingDBs(t *testing.T) {
require.NoError(t, err)
require.Equal(t, uint64(9), preResetHt["ledger-1"])
_, err = env.ledgerMgr.OpenLedger("ledger-1")
require.Error(t, err)
// populateLedger() stores 8 block in total
require.EqualError(t, err, "the state database [height=9] is ahead of the block store [height=1]. "+
"This is possible when the state database is not dropped after a ledger reset/rollback. "+
Expand Down
Loading

0 comments on commit c15e44e

Please sign in to comment.