Skip to content

Commit 9df7b0e

Browse files
committed
FAB-1505 and FAB-1337 Refactor CouchDB code
FAB-1505 refactors CouchDB code to be aligned with LevelDB structure. FAB-1337 applies all ledger tests written for LevelDB against CouchDB. In order to test the CouchDB refactor, had to implement FAB-1337 in same changeset to test. Like most refactors, this changeset touches a lot of files and code, since it is not feasible to do a partial refactor. Tests related to versioning and deleting have been commented out, these will be re-enabled in subsequent changeset when CouchDB has the support, in order to keep this changeset size more reasonable. Change-Id: I0d2d6cca89bd0252f6e84317457a9140b3a0d7a5 Signed-off-by: denyeart <[email protected]>
1 parent 6d8f919 commit 9df7b0e

29 files changed

+1112
-1619
lines changed

core/ledger/history/couchdb_histmgr.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ var compositeKeySep = []byte{0x00}
4646
// CouchDBHistMgr a simple implementation of interface `histmgmt.HistMgr'.
4747
// TODO This implementation does not currently use a lock but may need one to ensure query's are consistent
4848
type CouchDBHistMgr struct {
49-
couchDB *couchdb.CouchDBConnectionDef // COUCHDB new properties for CouchDB
49+
couchDB *couchdb.CouchDatabase // COUCHDB new properties for CouchDB
5050
}
5151

5252
// NewCouchDBHistMgr constructs a new `CouchDB HistMgr`
5353
func NewCouchDBHistMgr(couchDBConnectURL string, dbName string, id string, pw string) *CouchDBHistMgr {
5454

5555
//TODO locking has not been implemented but may need some sort of locking to insure queries are valid data.
5656

57-
couchDB, err := couchdb.CreateCouchDBConnectionAndDB(couchDBConnectURL, dbName, id, pw)
57+
couchInstance, err := couchdb.CreateCouchInstance(couchDBConnectURL, id, pw)
58+
couchDB, err := couchdb.CreateCouchDatabase(*couchInstance, dbName)
5859
if err != nil {
5960
logger.Errorf("===HISTORYDB=== Error during NewCouchDBHistMgr(): %s\n", err.Error())
6061
return nil

core/ledger/history/pkg_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ func newTestEnvHistoryCouchDB(t testing.TB, dbName string) *testEnvHistoryCouchD
4646
func (env *testEnvHistoryCouchDB) cleanup() {
4747

4848
//create a new connection
49-
couchDB, err := couchdb.CreateConnectionDefinition(env.couchDBAddress, env.couchDatabaseName, env.couchUsername, env.couchPassword)
49+
couchInstance, err := couchdb.CreateCouchInstance(env.couchDBAddress, env.couchUsername, env.couchPassword)
50+
couchDB, err := couchdb.CreateCouchDatabase(*couchInstance, env.couchDatabaseName)
5051
if err == nil {
5152
//drop the test database if it already existed
5253
couchDB.DropDatabase()

core/ledger/kvledger/example/main/example.go

+15
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ import (
2727
"github.com/hyperledger/fabric/core/ledger/testutil"
2828
"github.com/hyperledger/fabric/core/ledger/util"
2929
"github.com/hyperledger/fabric/protos/common"
30+
logging "github.com/op/go-logging"
3031
)
3132

33+
var logger = logging.MustGetLogger("main")
34+
3235
const (
3336
ledgerID = "Default"
3437
)
@@ -91,6 +94,7 @@ func main() {
9194
}
9295

9396
func initApp() {
97+
logger.Debug("Entering initApp()")
9498
tx, err := app.Init(map[string]int{
9599
accounts[0]: 100,
96100
accounts[1]: 100,
@@ -101,9 +105,11 @@ func initApp() {
101105
err = committer.CommitBlock(rawBlock)
102106
handleError(err, true)
103107
printBlocksInfo(rawBlock)
108+
logger.Debug("Exiting initApp()")
104109
}
105110

106111
func transferFunds() {
112+
logger.Debug("Entering transferFunds()")
107113
tx1, err := app.TransferFunds("account1", "account2", 50)
108114
handleError(err, true)
109115
tx2, err := app.TransferFunds("account3", "account4", 50)
@@ -116,14 +122,18 @@ func transferFunds() {
116122
err = committer.CommitBlock(rawBlock)
117123
handleError(err, true)
118124
printBlocksInfo(rawBlock)
125+
logger.Debug("Exiting transferFunds")
119126
}
120127

121128
func tryInvalidTransfer() {
129+
logger.Debug("Entering tryInvalidTransfer()")
122130
_, err := app.TransferFunds("account1", "account2", 60)
123131
handleError(err, false)
132+
logger.Debug("Exiting tryInvalidTransfer()")
124133
}
125134

126135
func tryDoubleSpend() {
136+
logger.Debug("Entering tryDoubleSpend()")
127137
tx1, err := app.TransferFunds("account1", "account2", 50)
128138
handleError(err, true)
129139
tx2, err := app.TransferFunds("account1", "account4", 50)
@@ -132,9 +142,11 @@ func tryDoubleSpend() {
132142
err = committer.CommitBlock(rawBlock)
133143
handleError(err, true)
134144
printBlocksInfo(rawBlock)
145+
logger.Debug("Exiting tryDoubleSpend()")
135146
}
136147

137148
func printBlocksInfo(block *common.Block) {
149+
logger.Debug("Entering printBlocksInfo()")
138150
// Read invalid transactions filter
139151
txsFltr := util.NewFilterBitArrayFromBytes(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER])
140152
numOfInvalid := 0
@@ -146,14 +158,17 @@ func printBlocksInfo(block *common.Block) {
146158
}
147159
fmt.Printf("Num txs in rawBlock = [%d], num invalidTxs = [%d]\n",
148160
len(block.Data.Data), numOfInvalid)
161+
logger.Debug("Exiting printBlocksInfo()")
149162
}
150163

151164
func printBalances() {
165+
logger.Debug("Entering printBalances()")
152166
balances, err := app.QueryBalances(accounts)
153167
handleError(err, true)
154168
for i := 0; i < len(accounts); i++ {
155169
fmt.Printf("[%s] = [%d]\n", accounts[i], balances[i])
156170
}
171+
logger.Debug("Exiting printBalances()")
157172
}
158173

159174
func handleError(err error, quit bool) {

core/ledger/kvledger/example/marble_app.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type Marble struct {
5757
func (marbleApp *MarbleApp) CreateMarble(args []string) (*common.Envelope, error) {
5858
// 0 1 2 3
5959
// "asdf", "blue", "35", "bob"
60-
logger.Debugf("===COUCHDB=== Entering ----------CreateMarble()----------")
60+
logger.Debugf("Entering ----------CreateMarble()----------")
6161
marbleName := args[0]
6262
marbleJsonBytes, err := init_marble(args)
6363
if err != nil {
@@ -76,9 +76,9 @@ func (marbleApp *MarbleApp) CreateMarble(args []string) (*common.Envelope, error
7676
if txSimulationResults, err = txSimulator.GetTxSimulationResults(); err != nil {
7777
return nil, err
7878
}
79-
logger.Debugf("===COUCHDB=== CreateMarble() simulation done, packaging into a transaction...")
79+
logger.Debugf("CreateMarble() simulation done, packaging into a transaction...")
8080
tx := constructTransaction(txSimulationResults)
81-
logger.Debugf("===COUCHDB=== Exiting CreateMarble()")
81+
logger.Debugf("Exiting CreateMarble()")
8282
return tx, nil
8383
}
8484

@@ -94,7 +94,7 @@ func init_marble(args []string) ([]byte, error) {
9494
return nil, errors.New("Incorrect number of arguments. Expecting 4")
9595
}
9696

97-
logger.Debugf("===COUCHDB=== Entering init marble")
97+
logger.Debugf("Entering init marble")
9898
if len(args[0]) <= 0 {
9999
return nil, errors.New("1st argument must be a non-empty string")
100100
}
@@ -120,7 +120,7 @@ func init_marble(args []string) ([]byte, error) {
120120
marbleJson := `{"txid": "` + tx + `", "asset_name": "` + args[0] + `", "color": "` + color + `", "size": ` + strconv.Itoa(size) + `, "owner": "` + user + `"}`
121121
marbleBytes := []byte(marbleJson)
122122

123-
logger.Debugf("===COUCHDB=== Exiting init marble")
123+
logger.Debugf("Exiting init marble")
124124
return marbleBytes, nil
125125
}
126126

@@ -134,7 +134,7 @@ func (marbleApp *MarbleApp) TransferMarble(args []string) (*common.Envelope, err
134134
marbleName := args[0]
135135
marbleNewOwner := args[1]
136136

137-
logger.Debugf("===COUCHDB=== Entering ----------TransferMarble----------")
137+
logger.Debugf("Entering ----------TransferMarble----------")
138138
var txSimulator ledger.TxSimulator
139139
var err error
140140
if txSimulator, err = marbleApp.ledger.NewTxSimulator(); err != nil {
@@ -143,25 +143,25 @@ func (marbleApp *MarbleApp) TransferMarble(args []string) (*common.Envelope, err
143143
defer txSimulator.Done()
144144

145145
marbleBytes, err := txSimulator.GetState(marbleApp.name, marbleName)
146-
logger.Debugf("===COUCHDB=== marbleBytes is: %v", marbleBytes)
146+
logger.Debugf("marbleBytes is: %v", marbleBytes)
147147
if marbleBytes != nil {
148148
jsonString := string(marbleBytes[:])
149-
logger.Debugf("===COUCHDB=== TransferMarble() Retrieved jsonString: \n %s", jsonString)
149+
logger.Debugf("TransferMarble() Retrieved jsonString: \n %s", jsonString)
150150
}
151151

152152
theMarble := Marble{}
153153
json.Unmarshal(marbleBytes, &theMarble) //Unmarshal JSON bytes into a Marble struct
154154

155-
logger.Debugf("===COUCHDB=== theMarble after unmarshal: %v", theMarble)
155+
logger.Debugf(" theMarble after unmarshal: %v", theMarble)
156156

157-
logger.Debugf("===COUCHDB=== Setting the owner to: %s", marbleNewOwner)
157+
logger.Debugf(" Setting the owner to: %s", marbleNewOwner)
158158
theMarble.User = marbleNewOwner //change the user
159159
theMarble.Txid = "tx000000000000002" // COUCHDB hardcode a txid for now for demo purpose
160160

161161
updatedMarbleBytes, _ := json.Marshal(theMarble)
162162
if updatedMarbleBytes != nil {
163163
updatedJsonString := string(updatedMarbleBytes[:])
164-
logger.Debugf("===COUCHDB=== updatedJsonString:\n %s", updatedJsonString)
164+
logger.Debugf("updatedJsonString:\n %s", updatedJsonString)
165165
}
166166
err = txSimulator.SetState(marbleApp.name, marbleName, updatedMarbleBytes)
167167
if err != nil {
@@ -172,7 +172,7 @@ func (marbleApp *MarbleApp) TransferMarble(args []string) (*common.Envelope, err
172172
if txSimulationResults, err = txSimulator.GetTxSimulationResults(); err != nil {
173173
return nil, err
174174
}
175-
logger.Debugf("===COUCHDB=== TransferMarble() simulation done, packaging into a transaction...")
175+
logger.Debugf("TransferMarble() simulation done, packaging into a transaction...")
176176
tx := constructTransaction(txSimulationResults)
177177
return tx, nil
178178
}

core/ledger/kvledger/kv_ledger.go

+4-17
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/hyperledger/fabric/core/ledger/blkstorage"
2525
"github.com/hyperledger/fabric/core/ledger/blkstorage/fsblkstorage"
2626
"github.com/hyperledger/fabric/core/ledger/history"
27-
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/couchdbtxmgmt"
2827
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
2928
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/txmgr"
3029
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr"
@@ -66,23 +65,11 @@ func NewKVLedger(versionedDBProvider statedb.VersionedDBProvider, ledgerID strin
6665
var txmgmt txmgr.TxMgr
6766
var historymgmt history.HistMgr
6867

69-
if ledgerconfig.IsCouchDBEnabled() == true {
70-
//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
71-
logger.Debugf("===COUCHDB=== NewKVLedger() Using CouchDB instead of RocksDB...hardcoding and passing connection config for now")
72-
73-
couchDBDef := ledgerconfig.GetCouchDBDefinition()
74-
75-
//create new transaction manager based on couchDB
76-
txmgmt = couchdbtxmgmt.NewCouchDBTxMgr(&couchdbtxmgmt.Conf{DBPath: ""},
77-
couchDBDef.URL, //couchDB connection URL
78-
"system", //couchDB db name matches ledger name, TODO for now use system ledger, eventually allow passing in subledger name
79-
couchDBDef.Username, //enter couchDB id here
80-
couchDBDef.Password) //enter couchDB pw here
81-
} else {
82-
// Fall back to using goleveldb lockbased transaction manager
83-
db := versionedDBProvider.GetDBHandle(ledgerID)
84-
txmgmt = lockbasedtxmgr.NewLockBasedTxMgr(db)
68+
db, err := versionedDBProvider.GetDBHandle(ledgerID)
69+
if err != nil {
70+
return nil, err
8571
}
72+
txmgmt = lockbasedtxmgr.NewLockBasedTxMgr(db)
8673

8774
if ledgerconfig.IsHistoryDBEnabled() == true {
8875
logger.Debugf("===HISTORYDB=== NewKVLedger() Using CouchDB for transaction history database")

core/ledger/kvledger/kv_ledger_provider.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/hyperledger/fabric/core/ledger"
2323
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
24+
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb"
2425
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/stateleveldb"
2526
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
2627
"github.com/hyperledger/fabric/core/ledger/util/db"
@@ -50,7 +51,12 @@ func NewProvider() (ledger.ValidatedLedgerProvider, error) {
5051
logger.Debugf("Constructing leveldb VersionedDBProvider")
5152
vdbProvider = stateleveldb.NewVersionedDBProvider()
5253
} else {
53-
//TODO same for couchDB after refactoring of couchdb code
54+
logger.Debugf("Constructing CouchDB VersionedDBProvider")
55+
var err error
56+
vdbProvider, err = statecouchdb.NewVersionedDBProvider()
57+
if err != nil {
58+
return nil, err
59+
}
5460
}
5561
ledgerMgmtPath := ledgerconfig.GetLedgerProviderPath()
5662
idStore := openIDStore(ledgerMgmtPath)

core/ledger/kvledger/marble_example/main/marble_example.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func init() {
4747
// Initialization will get a handle to the ledger at the specified path
4848
// Note, if subledgers are supported in the future,
4949
// the various ledgers could be created/managed at this level
50-
logger.Debugf("===COUCHDB=== Marble Example main init()")
50+
logger.Debugf("Marble Example main init()")
5151

5252
//call a helper method to load the core.yaml
5353
testutil.SetupCoreYAMLConfig("./../../../../../peer")
@@ -77,7 +77,7 @@ func main() {
7777
}
7878

7979
func initApp() {
80-
logger.Debugf("===COUCHDB=== Marble Example initApp() to create a marble")
80+
logger.Debugf("Marble Example initApp() to create a marble")
8181
marble := []string{"marble1", "blue", "35", "tom"}
8282
tx, err := marbleApp.CreateMarble(marble)
8383
handleError(err, true)
@@ -88,7 +88,7 @@ func initApp() {
8888
}
8989

9090
func transferMarble() {
91-
logger.Debugf("===COUCHDB=== Marble Example transferMarble()")
91+
logger.Debugf("Marble Example transferMarble()")
9292
tx1, err := marbleApp.TransferMarble([]string{"marble1", "jerry"})
9393
handleError(err, true)
9494
rawBlock := consenter.ConstructBlock(tx1)

0 commit comments

Comments
 (0)