Skip to content

Commit 3731447

Browse files
committed
FAB-1087 Add config option in core.yaml for history
Add config option in core.yaml for history database to store the transaction history. The history database will be a queryable database such as CouchDB. The default will be false. If set to false, the transaction history will not be persisted in the history database. Move the ledger config directory up one level to ledger to be the config for state and history databases as well as any other ledger functionality that needs a config option. Change-Id: If4607047082b686dd903d8b3805362d0c05e1c64 Signed-off-by: Mari Wade <[email protected]>
1 parent d148c38 commit 3731447

File tree

7 files changed

+131
-18
lines changed

7 files changed

+131
-18
lines changed

core/ledger/kvledger/kv_ledger.go

+3-3
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

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

79-
if kvledgerconfig.IsCouchDBEnabled() == true {
79+
if ledgerconfig.IsCouchDBEnabled() == true {
8080
//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
8181
logger.Debugf("===COUCHDB=== NewKVLedger() Using CouchDB instead of RocksDB...hardcoding and passing connection config for now")
8282

83-
couchDBDef := kvledgerconfig.GetCouchDBDefinition()
83+
couchDBDef := ledgerconfig.GetCouchDBDefinition()
8484

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

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

+11-10
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

+3-3
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 core/ledger/ledgerconfig/ledger_config.go

+14-1
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+
}
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

+8-1
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

+6
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,12 @@ ledger:
413413
couchDBAddress: 127.0.0.1:5984
414414
username:
415415
password:
416+
# historyDatabase - options are true or false
417+
# Indicates if the transaction history should be stored in
418+
# a querable database such as "CouchDB".
419+
# The stateDatabase must be also stored in CouchDB for
420+
# history to be enabled.
421+
historyDatabase: false
416422

417423
###############################################################################
418424
#

0 commit comments

Comments
 (0)