Skip to content

Commit de00eaa

Browse files
Srinivasan MuralidharanGerrit Code Review
authored andcommitted
Merge "FAB-1087 Add config option in core.yaml for history"
2 parents 6bb5043 + 3731447 commit de00eaa

File tree

7 files changed

+131
-18
lines changed

7 files changed

+131
-18
lines changed

core/ledger/kvledger/kv_ledger.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424
"github.com/hyperledger/fabric/core/ledger"
2525
"github.com/hyperledger/fabric/core/ledger/blkstorage"
2626
"github.com/hyperledger/fabric/core/ledger/blkstorage/fsblkstorage"
27-
"github.com/hyperledger/fabric/core/ledger/kvledger/kvledgerconfig"
2827
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt"
2928
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/couchdbtxmgmt"
3029
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/lockbasedtxmgmt"
30+
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
3131

3232
logging "github.com/op/go-logging"
3333

@@ -77,11 +77,11 @@ func NewKVLedger(conf *Conf) (*KVLedger, error) {
7777
blockStorageConf := fsblkstorage.NewConf(conf.blockStorageDir, conf.maxBlockfileSize)
7878
blockStore := fsblkstorage.NewFsBlockStore(blockStorageConf, indexConfig)
7979

80-
if kvledgerconfig.IsCouchDBEnabled() == true {
80+
if ledgerconfig.IsCouchDBEnabled() == true {
8181
//By default we can talk to CouchDB with empty id and pw (""), or you can add your own id and password to talk to a secured CouchDB
8282
logger.Debugf("===COUCHDB=== NewKVLedger() Using CouchDB instead of RocksDB...hardcoding and passing connection config for now")
8383

84-
couchDBDef := kvledgerconfig.GetCouchDBDefinition()
84+
couchDBDef := ledgerconfig.GetCouchDBDefinition()
8585

8686
//create new transaction manager based on couchDB
8787
txmgmt := couchdbtxmgmt.NewCouchDBTxMgr(&couchdbtxmgmt.Conf{DBPath: conf.txMgrDBPath},

core/ledger/kvledger/txmgmt/couchdbtxmgmt/couchdb/couchdb_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"fmt"
2222
"testing"
2323

24-
"github.com/hyperledger/fabric/core/ledger/kvledger/kvledgerconfig"
24+
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
2525
"github.com/hyperledger/fabric/core/ledger/testutil"
2626
)
2727

@@ -63,7 +63,7 @@ func TestDBBadConnectionDef(t *testing.T) {
6363

6464
func TestDBCreateSaveWithoutRevision(t *testing.T) {
6565

66-
if kvledgerconfig.IsCouchDBEnabled() == true {
66+
if ledgerconfig.IsCouchDBEnabled() == true {
6767

6868
cleanup()
6969
defer cleanup()
@@ -85,7 +85,7 @@ func TestDBCreateSaveWithoutRevision(t *testing.T) {
8585

8686
func TestDBBadConnection(t *testing.T) {
8787

88-
if kvledgerconfig.IsCouchDBEnabled() == true {
88+
if ledgerconfig.IsCouchDBEnabled() == true {
8989

9090
//create a new connection
9191
db, err := CreateConnectionDefinition(badConnectURL, database, username, password)
@@ -108,7 +108,7 @@ func TestDBBadConnection(t *testing.T) {
108108

109109
func TestDBCreateDatabaseAndPersist(t *testing.T) {
110110

111-
if kvledgerconfig.IsCouchDBEnabled() == true {
111+
if ledgerconfig.IsCouchDBEnabled() == true {
112112

113113
cleanup()
114114

@@ -175,9 +175,10 @@ func TestDBCreateDatabaseAndPersist(t *testing.T) {
175175

176176
func TestDBBadJSON(t *testing.T) {
177177

178-
if kvledgerconfig.IsCouchDBEnabled() == true {
178+
if ledgerconfig.IsCouchDBEnabled() == true {
179179

180180
cleanup()
181+
defer cleanup()
181182

182183
//create a new connection
183184
db, err := CreateConnectionDefinition(connectURL, database, username, password)
@@ -204,7 +205,7 @@ func TestDBBadJSON(t *testing.T) {
204205

205206
func TestDBSaveAttachment(t *testing.T) {
206207

207-
if kvledgerconfig.IsCouchDBEnabled() == true {
208+
if ledgerconfig.IsCouchDBEnabled() == true {
208209

209210
cleanup()
210211
defer cleanup()
@@ -239,7 +240,7 @@ func TestDBSaveAttachment(t *testing.T) {
239240

240241
func TestDBRetrieveNonExistingDocument(t *testing.T) {
241242

242-
if kvledgerconfig.IsCouchDBEnabled() == true {
243+
if ledgerconfig.IsCouchDBEnabled() == true {
243244

244245
cleanup()
245246
defer cleanup()
@@ -261,7 +262,7 @@ func TestDBRetrieveNonExistingDocument(t *testing.T) {
261262

262263
func TestDBTestExistingDB(t *testing.T) {
263264

264-
if kvledgerconfig.IsCouchDBEnabled() == true {
265+
if ledgerconfig.IsCouchDBEnabled() == true {
265266

266267
cleanup()
267268
defer cleanup()
@@ -283,7 +284,7 @@ func TestDBTestExistingDB(t *testing.T) {
283284

284285
func TestDBTestDropNonExistDatabase(t *testing.T) {
285286

286-
if kvledgerconfig.IsCouchDBEnabled() == true {
287+
if ledgerconfig.IsCouchDBEnabled() == true {
287288

288289
cleanup()
289290
defer cleanup()
@@ -301,7 +302,7 @@ func TestDBTestDropNonExistDatabase(t *testing.T) {
301302

302303
func TestDBTestDropDatabaseBadConnection(t *testing.T) {
303304

304-
if kvledgerconfig.IsCouchDBEnabled() == true {
305+
if ledgerconfig.IsCouchDBEnabled() == true {
305306

306307
cleanup()
307308
defer cleanup()

core/ledger/kvledger/txmgmt/couchdbtxmgmt/couchdb_txmgmt_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
"os"
2222
"testing"
2323

24-
"github.com/hyperledger/fabric/core/ledger/kvledger/kvledgerconfig"
2524
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/couchdbtxmgmt/couchdb"
25+
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
2626
"github.com/hyperledger/fabric/core/ledger/testutil"
2727
)
2828

@@ -39,7 +39,7 @@ func newTestEnv(t testing.TB) *testEnv {
3939
//call a helper method to load the core.yaml
4040
testutil.SetupCoreYAMLConfig("./../../../../../peer")
4141

42-
couchDBDef := kvledgerconfig.GetCouchDBDefinition()
42+
couchDBDef := ledgerconfig.GetCouchDBDefinition()
4343

4444
conf := &Conf{"/tmp/tests/ledger/kvledger/txmgmt/couchdbtxmgmt"}
4545
os.RemoveAll(conf.DBPath)
@@ -69,7 +69,7 @@ func TestDatabaseAutoCreate(t *testing.T) {
6969
//Only run the tests if CouchDB is explitily enabled in the code,
7070
//otherwise CouchDB may not be installed and all the tests would fail
7171
//TODO replace this with external config property rather than config within the code
72-
if kvledgerconfig.IsCouchDBEnabled() == true {
72+
if ledgerconfig.IsCouchDBEnabled() == true {
7373

7474
env := newTestEnv(t)
7575
env.Cleanup() //cleanup at the beginning to ensure the database doesn't exist already

core/ledger/kvledger/kvledgerconfig/kv_ledger_config.go renamed to core/ledger/ledgerconfig/ledger_config.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package kvledgerconfig
17+
package ledgerconfig
1818

1919
import "github.com/spf13/viper"
2020

2121
var stateDatabase = "goleveldb"
2222
var couchDBAddress = "127.0.0.1:5984"
2323
var username = ""
2424
var password = ""
25+
var historyDatabase = true
2526

2627
// CouchDBDef contains parameters
2728
type CouchDBDef struct {
@@ -48,3 +49,15 @@ func GetCouchDBDefinition() *CouchDBDef {
4849

4950
return &CouchDBDef{couchDBAddress, username, password}
5051
}
52+
53+
//IsHistoryDBEnabled exposes the historyDatabase variable
54+
//History database can only be enabled if couchDb is enabled
55+
//as it the history stored in the same couchDB instance.
56+
//TODO put History DB in it's own instance
57+
func IsHistoryDBEnabled() bool {
58+
historyDatabase = viper.GetBool("ledger.state.historyDatabase")
59+
if IsCouchDBEnabled() && historyDatabase {
60+
return historyDatabase
61+
}
62+
return false
63+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package ledgerconfig
18+
19+
import (
20+
"testing"
21+
22+
"github.com/hyperledger/fabric/core/ledger/testutil"
23+
"github.com/spf13/viper"
24+
)
25+
26+
func TestIsCouchDBEnabledDefault(t *testing.T) {
27+
setUpCoreYAMLConfig()
28+
defaultValue := IsCouchDBEnabled()
29+
testutil.AssertEquals(t, defaultValue, false) //test default config is false
30+
}
31+
32+
func TestIsCouchDBEnabled(t *testing.T) {
33+
setUpCoreYAMLConfig()
34+
defer testutil.ResetConfigToDefaultValues()
35+
viper.Set("ledger.state.stateDatabase", "CouchDB")
36+
updatedValue := IsCouchDBEnabled()
37+
testutil.AssertEquals(t, updatedValue, true) //test config returns true
38+
}
39+
40+
func TestGetCouchDBDefinition(t *testing.T) {
41+
setUpCoreYAMLConfig()
42+
defer testutil.ResetConfigToDefaultValues()
43+
viper.Set("ledger.state.stateDatabase", "CouchDB")
44+
couchDBDef := GetCouchDBDefinition()
45+
testutil.AssertEquals(t, couchDBDef.URL, "127.0.0.1:5984")
46+
testutil.AssertEquals(t, couchDBDef.Username, "")
47+
testutil.AssertEquals(t, couchDBDef.Password, "")
48+
}
49+
50+
func TestIsHistoryDBEnabledDefault(t *testing.T) {
51+
setUpCoreYAMLConfig()
52+
defaultValue := IsHistoryDBEnabled()
53+
testutil.AssertEquals(t, defaultValue, false) //test default config is false
54+
}
55+
56+
func TestIsHistoryDBEnabledWhenCouchDBIsDisabled(t *testing.T) {
57+
setUpCoreYAMLConfig()
58+
defer testutil.ResetConfigToDefaultValues()
59+
viper.Set("ledger.state.stateDatabase", "goleveldb")
60+
viper.Set("ledger.state.historyDatabase", true)
61+
updatedValue := IsHistoryDBEnabled()
62+
testutil.AssertEquals(t, updatedValue, false) //test config is false
63+
}
64+
65+
func TestIsHistoryDBEnabledWhenOnlyCouchDBEnabled(t *testing.T) {
66+
setUpCoreYAMLConfig()
67+
defer testutil.ResetConfigToDefaultValues()
68+
viper.Set("ledger.state.stateDatabase", "CouchDB")
69+
viper.Set("ledger.state.historyDatabase", false)
70+
updatedValue := IsHistoryDBEnabled()
71+
testutil.AssertEquals(t, updatedValue, false) //test config is false
72+
}
73+
74+
func TestIsHistoryDBEnabled(t *testing.T) {
75+
setUpCoreYAMLConfig()
76+
defer testutil.ResetConfigToDefaultValues()
77+
viper.Set("ledger.state.stateDatabase", "CouchDB")
78+
viper.Set("ledger.state.historyDatabase", true)
79+
updatedValue := IsHistoryDBEnabled()
80+
testutil.AssertEquals(t, updatedValue, true) //test config returns true
81+
}
82+
83+
func setUpCoreYAMLConfig() {
84+
//call a helper method to load the core.yaml
85+
testutil.SetupCoreYAMLConfig("./../../../peer")
86+
}

core/ledger/testutil/test_util.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func SetupTestConfig() {
7272
logging.SetFormatter(formatter)
7373
}
7474

75-
// SetupCoreYAMLConfig sets up configurations for tetsing
75+
// SetupCoreYAMLConfig sets up configurations for testing
7676
func SetupCoreYAMLConfig(coreYamlPath string) {
7777
viper.SetConfigName("core")
7878
viper.SetEnvPrefix("CORE")
@@ -85,6 +85,13 @@ func SetupCoreYAMLConfig(coreYamlPath string) {
8585
}
8686
}
8787

88+
// ResetConfigToDefaultValues resets configurations optins back to defaults
89+
func ResetConfigToDefaultValues() {
90+
//reset to defaults
91+
viper.Set("ledger.state.stateDatabase", "goleveldb")
92+
viper.Set("ledger.state.historyDatabase", false)
93+
}
94+
8895
// SetLogLevel sets up log level
8996
func SetLogLevel(level logging.Level, module string) {
9097
logging.SetLevel(level, module)

peer/core.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,12 @@ ledger:
408408
couchDBAddress: 127.0.0.1:5984
409409
username:
410410
password:
411+
# historyDatabase - options are true or false
412+
# Indicates if the transaction history should be stored in
413+
# a querable database such as "CouchDB".
414+
# The stateDatabase must be also stored in CouchDB for
415+
# history to be enabled.
416+
historyDatabase: false
411417

412418
###############################################################################
413419
#

0 commit comments

Comments
 (0)