Skip to content

Commit b966981

Browse files
committed
[FAB-6553] Ledger bookkeeping provider
This CR introduces a bookkeeping provider. This is intented to be used in maintaining different aspects of ledger data management. The initial use would be to maintain the expiry schedule of pvt data items in the pvt statedb Change-Id: I21f5eef5034cc413c39fdf4bee798fefafbcdf27 Signed-off-by: manish <manish.sethi@gmail.com>
1 parent 3ac6358 commit b966981

File tree

4 files changed

+139
-5
lines changed

4 files changed

+139
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package bookkeeping
8+
9+
import (
10+
"fmt"
11+
12+
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
13+
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
14+
)
15+
16+
// Category is an enum type for representing the bookkeeping of different type
17+
type Category int
18+
19+
const (
20+
// PvtdataExpiry repersents the bookkeeping related to expiry of pvtdata because of BTL policy
21+
PvtdataExpiry Category = iota
22+
)
23+
24+
// Provider provides handle to different bookkeepers for the given ledger
25+
type Provider interface {
26+
// GetDBHandle returns a db handle that can be used for maintaining the bookkeeping of a given category
27+
GetDBHandle(ledgerID string, cat Category) *leveldbhelper.DBHandle
28+
// Close closes the BookkeeperProvider
29+
Close()
30+
}
31+
32+
type provider struct {
33+
dbProvider *leveldbhelper.Provider
34+
}
35+
36+
// NewProvider instantiates a new provider
37+
func NewProvider() Provider {
38+
dbProvider := leveldbhelper.NewProvider(&leveldbhelper.Conf{DBPath: getInternalBookkeeperPath()})
39+
return &provider{dbProvider: dbProvider}
40+
}
41+
42+
// GetDBHandle implements the function in the interface 'BookkeeperProvider'
43+
func (provider *provider) GetDBHandle(ledgerID string, cat Category) *leveldbhelper.DBHandle {
44+
return provider.dbProvider.GetDBHandle(fmt.Sprintf(ledgerID+"/%d", cat))
45+
}
46+
47+
// Close implements the function in the interface 'BookKeeperProvider'
48+
func (provider *provider) Close() {
49+
provider.dbProvider.Close()
50+
}
51+
52+
func getInternalBookkeeperPath() string {
53+
return ledgerconfig.GetInternalBookkeeperPath()
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package bookkeeping
8+
9+
import (
10+
"os"
11+
"testing"
12+
13+
"github.com/spf13/viper"
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func TestMain(m *testing.M) {
18+
viper.Set("peer.fileSystemPath", "/tmp/fabric/ledgertests/kvledger/bookkeeping")
19+
os.Exit(m.Run())
20+
}
21+
22+
func TestProvider(t *testing.T) {
23+
testEnv := NewTestEnv(t)
24+
defer testEnv.Cleanup()
25+
p := testEnv.TestProvider
26+
db := p.GetDBHandle("TestLedger", PvtdataExpiry)
27+
assert.NoError(t, db.Put([]byte("key"), []byte("value"), true))
28+
val, err := db.Get([]byte("key"))
29+
assert.NoError(t, err)
30+
assert.Equal(t, []byte("value"), val)
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright IBM Corp. 2017 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 bookkeeping
18+
19+
import (
20+
"os"
21+
"testing"
22+
)
23+
24+
// TestEnv provides the bookkeeper provider env for testing
25+
type TestEnv struct {
26+
t testing.TB
27+
TestProvider Provider
28+
}
29+
30+
// NewTestEnv construct a TestEnv for testing
31+
func NewTestEnv(t *testing.T) *TestEnv {
32+
removePath(t)
33+
provider := NewProvider()
34+
return &TestEnv{t, provider}
35+
}
36+
37+
// Cleanup cleansup the store env after testing
38+
func (env *TestEnv) Cleanup() {
39+
env.TestProvider.Close()
40+
removePath(env.t)
41+
}
42+
43+
func removePath(t testing.TB) {
44+
dbPath := getInternalBookkeeperPath()
45+
if err := os.RemoveAll(dbPath); err != nil {
46+
t.Fatalf("Err: %s", err)
47+
t.FailNow()
48+
}
49+
}

core/ledger/ledgerconfig/ledger_config.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ func GetHistoryLevelDBPath() string {
6868
return filepath.Join(GetRootPath(), confHistoryLeveldb)
6969
}
7070

71-
// GetPvtWritesetStorePath returns the filesystem path that is used for permanent storage of privare write-sets
72-
func GetPvtWritesetStorePath() string {
73-
return filepath.Join(GetRootPath(), confPvtWritesetStore)
74-
}
75-
7671
// GetBlockStorePath returns the filesystem path that is used for the chain block stores
7772
func GetBlockStorePath() string {
7873
return filepath.Join(GetRootPath(), confChains)
@@ -83,6 +78,11 @@ func GetPvtdataStorePath() string {
8378
return filepath.Join(GetRootPath(), confPvtdataStore)
8479
}
8580

81+
// GetInternalBookkeeperPath returns the filesystem path that is used for bookkeeping the internal stuff by by KVledger (such as expiration time for pvt)
82+
func GetInternalBookkeeperPath() string {
83+
return filepath.Join(GetRootPath(), "bookkeeper")
84+
}
85+
8686
// GetMaxBlockfileSize returns maximum size of the block file
8787
func GetMaxBlockfileSize() int {
8888
return 64 * 1024 * 1024

0 commit comments

Comments
 (0)