-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
225 lines (192 loc) · 6.58 KB
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package tests
import (
"os"
"path/filepath"
"testing"
"github.com/hyperledger/fabric/common/metrics/disabled"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage"
"github.com/hyperledger/fabric/common/ledger/util"
"github.com/hyperledger/fabric/core/common/privdata"
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/core/scc/lscc"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
type config map[string]interface{}
type rebuildable uint8
const (
rebuildableStatedb rebuildable = 1
rebuildableBlockIndex rebuildable = 2
rebuildableConfigHistory rebuildable = 4
rebuildableHistoryDB rebuildable = 8
rebuildableBookkeeper rebuildable = 16
)
var (
defaultConfig = config{
"peer.fileSystemPath": "/tmp/fabric/ledgertests",
"ledger.state.stateDatabase": "goleveldb",
}
)
type env struct {
assert *assert.Assertions
}
func newEnv(conf config, t *testing.T) *env {
setupConfigs(conf)
env := &env{assert.New(t)}
initLedgerMgmt()
return env
}
func (e *env) cleanup() {
closeLedgerMgmt()
e.assert.NoError(os.RemoveAll(getLedgerRootPath()))
}
func (e *env) closeAllLedgersAndDrop(flags rebuildable) {
closeLedgerMgmt()
defer initLedgerMgmt()
if flags&rebuildableBlockIndex == rebuildableBlockIndex {
indexPath := getBlockIndexDBPath()
logger.Infof("Deleting blockstore indexdb path [%s]", indexPath)
e.verifyNonEmptyDirExists(indexPath)
e.assert.NoError(os.RemoveAll(indexPath))
}
if flags&rebuildableStatedb == rebuildableStatedb {
statedbPath := getLevelstateDBPath()
logger.Infof("Deleting statedb path [%s]", statedbPath)
e.verifyNonEmptyDirExists(statedbPath)
e.assert.NoError(os.RemoveAll(statedbPath))
}
if flags&rebuildableConfigHistory == rebuildableConfigHistory {
configHistoryPath := getConfigHistoryDBPath()
logger.Infof("Deleting configHistory db path [%s]", configHistoryPath)
e.verifyNonEmptyDirExists(configHistoryPath)
e.assert.NoError(os.RemoveAll(configHistoryPath))
}
if flags&rebuildableBookkeeper == rebuildableBookkeeper {
bookkeeperPath := getBookkeeperDBPath()
logger.Infof("Deleting bookkeeper db path [%s]", bookkeeperPath)
e.verifyNonEmptyDirExists(bookkeeperPath)
e.assert.NoError(os.RemoveAll(bookkeeperPath))
}
if flags&rebuildableHistoryDB == rebuildableHistoryDB {
historyPath := getHistoryDBPath()
logger.Infof("Deleting history db path [%s]", historyPath)
e.verifyNonEmptyDirExists(historyPath)
e.assert.NoError(os.RemoveAll(historyPath))
}
e.verifyRebuilableDoesNotExist(flags)
}
func (e *env) verifyRebuilablesExist(flags rebuildable) {
if flags&rebuildableBlockIndex == rebuildableBlockIndex {
e.verifyNonEmptyDirExists(getBlockIndexDBPath())
}
if flags&rebuildableStatedb == rebuildableStatedb {
e.verifyNonEmptyDirExists(getLevelstateDBPath())
}
if flags&rebuildableConfigHistory == rebuildableConfigHistory {
e.verifyNonEmptyDirExists(getConfigHistoryDBPath())
}
if flags&rebuildableBookkeeper == rebuildableBookkeeper {
e.verifyNonEmptyDirExists(getBookkeeperDBPath())
}
if flags&rebuildableHistoryDB == rebuildableHistoryDB {
e.verifyNonEmptyDirExists(getHistoryDBPath())
}
}
func (e *env) verifyRebuilableDoesNotExist(flags rebuildable) {
if flags&rebuildableStatedb == rebuildableStatedb {
e.verifyDirDoesNotExist(getLevelstateDBPath())
}
if flags&rebuildableBlockIndex == rebuildableBlockIndex {
e.verifyDirDoesNotExist(getBlockIndexDBPath())
}
if flags&rebuildableConfigHistory == rebuildableConfigHistory {
e.verifyDirDoesNotExist(getConfigHistoryDBPath())
}
if flags&rebuildableBookkeeper == rebuildableBookkeeper {
e.verifyDirDoesNotExist(getBookkeeperDBPath())
}
if flags&rebuildableHistoryDB == rebuildableHistoryDB {
e.verifyDirDoesNotExist(getHistoryDBPath())
}
}
func (e *env) verifyNonEmptyDirExists(path string) {
empty, err := util.DirEmpty(path)
e.assert.NoError(err)
e.assert.False(empty)
}
func (e *env) verifyDirDoesNotExist(path string) {
exists, _, err := util.FileExists(path)
e.assert.NoError(err)
e.assert.False(exists)
}
// ########################### ledgermgmt and ledgerconfig related functions wrappers #############################
// In the current code, ledgermgmt and ledgerconfigs are packaged scope APIs and hence so are the following
// wrapper APIs. As a TODO, both the ledgermgmt and ledgerconfig can be refactored as separate objects and then
// the instances of these two would be wrapped inside the `env` struct above.
// #################################################################################################################
func setupConfigs(conf config) {
for c, v := range conf {
viper.Set(c, v)
}
}
func initLedgerMgmt() {
identityDeserializerFactory := func(chainID string) msp.IdentityDeserializer {
return mgmt.GetManagerForChain(chainID)
}
membershipInfoProvider := privdata.NewMembershipInfoProvider(createSelfSignedData(), identityDeserializerFactory)
ledgermgmt.InitializeExistingTestEnvWithInitializer(
&ledgermgmt.Initializer{
CustomTxProcessors: peer.ConfigTxProcessors,
DeployedChaincodeInfoProvider: &lscc.DeployedCCInfoProvider{},
MembershipInfoProvider: membershipInfoProvider,
MetricsProvider: &disabled.Provider{},
},
)
}
func createSelfSignedData() common.SignedData {
sID := mgmt.GetLocalSigningIdentityOrPanic()
msg := make([]byte, 32)
sig, err := sID.Sign(msg)
if err != nil {
logger.Panicf("Failed creating self signed data because message signing failed: %v", err)
}
peerIdentity, err := sID.Serialize()
if err != nil {
logger.Panicf("Failed creating self signed data because peer identity couldn't be serialized: %v", err)
}
return common.SignedData{
Data: msg,
Signature: sig,
Identity: peerIdentity,
}
}
func closeLedgerMgmt() {
ledgermgmt.Close()
}
func getLedgerRootPath() string {
return ledgerconfig.GetRootPath()
}
func getLevelstateDBPath() string {
return ledgerconfig.GetStateLevelDBPath()
}
func getBlockIndexDBPath() string {
return filepath.Join(ledgerconfig.GetBlockStorePath(), fsblkstorage.IndexDir)
}
func getConfigHistoryDBPath() string {
return ledgerconfig.GetConfigHistoryPath()
}
func getHistoryDBPath() string {
return ledgerconfig.GetHistoryLevelDBPath()
}
func getBookkeeperDBPath() string {
return ledgerconfig.GetInternalBookkeeperPath()
}