Skip to content

Commit 2e3211f

Browse files
committed
Abstract string to const
Avoid typo same string in multiple places, extract to const instead. Change-Id: Ic2a92fe1e75827883f7f0f4e707fdd2bc1f8f265 Signed-off-by: grapebaba <[email protected]>
1 parent 981a222 commit 2e3211f

File tree

6 files changed

+26
-18
lines changed

6 files changed

+26
-18
lines changed

core/ledger/statemgmt/raw/state_impl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ type StateImpl struct {
2828
stateDelta *statemgmt.StateDelta
2929
}
3030

31-
// NewRawState constructs new instance of raw state
32-
func NewRawState() *StateImpl {
31+
// NewStateImpl constructs new instance of raw state
32+
func NewStateImpl() *StateImpl {
3333
return &StateImpl{}
3434
}
3535

core/ledger/statemgmt/state/config.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
var loadConfigOnce sync.Once
2727

28-
var stateImplName string
28+
var stateImplName stateImplType
2929
var stateImplConfigs map[string]interface{}
3030
var deltaHistorySize int
3131

@@ -35,7 +35,7 @@ func initConfig() {
3535

3636
func loadConfig() {
3737
logger.Info("Loading configurations...")
38-
stateImplName = viper.GetString("ledger.state.dataStructure.name")
38+
stateImplName = stateImplType(viper.GetString("ledger.state.dataStructure.name"))
3939
stateImplConfigs = viper.GetStringMap("ledger.state.dataStructure.configs")
4040
deltaHistorySize = viper.GetInt("ledger.state.deltaHistorySize")
4141
logger.Infof("Configurations loaded. stateImplName=[%s], stateImplConfigs=%s, deltaHistorySize=[%d]",
@@ -44,7 +44,7 @@ func loadConfig() {
4444
if len(stateImplName) == 0 {
4545
stateImplName = defaultStateImpl
4646
stateImplConfigs = nil
47-
} else if stateImplName != "buckettree" && stateImplName != "trie" && stateImplName != "raw" {
47+
} else if stateImplName != buckettreeType && stateImplName != trieType && stateImplName != rawType {
4848
panic(fmt.Errorf("Error during initialization of state implementation. State data structure '%s' is not valid.", stateImplName))
4949
}
5050

core/ledger/statemgmt/state/state.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ const defaultStateImpl = "buckettree"
3535

3636
var stateImpl statemgmt.HashableState
3737

38+
type stateImplType string
39+
40+
const (
41+
buckettreeType stateImplType = "buckettree"
42+
trieType stateImplType = "trie"
43+
rawType stateImplType = "raw"
44+
)
45+
3846
// State structure for maintaining world state.
3947
// This encapsulates a particular implementation for managing the state persistence
4048
// This is not thread safe
@@ -53,12 +61,12 @@ func NewState() *State {
5361
initConfig()
5462
logger.Infof("Initializing state implementation [%s]", stateImplName)
5563
switch stateImplName {
56-
case "buckettree":
64+
case buckettreeType:
5765
stateImpl = buckettree.NewStateImpl()
58-
case "trie":
59-
stateImpl = trie.NewStateTrie()
60-
case "raw":
61-
stateImpl = raw.NewRawState()
66+
case trieType:
67+
stateImpl = trie.NewStateImpl()
68+
case rawType:
69+
stateImpl = raw.NewStateImpl()
6270
default:
6371
panic("Should not reach here. Configs should have checked for the stateImplName being a valid names ")
6472
}

core/ledger/statemgmt/trie/pkg_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type stateTrieTestWrapper struct {
3636
}
3737

3838
func newStateTrieTestWrapper(t *testing.T) *stateTrieTestWrapper {
39-
return &stateTrieTestWrapper{NewStateTrie(), t}
39+
return &stateTrieTestWrapper{NewStateImpl(), t}
4040
}
4141

4242
func (stateTrieTestWrapper *stateTrieTestWrapper) Get(chaincodeID string, key string) []byte {

core/ledger/statemgmt/trie/state_trie.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ type StateTrie struct {
3737
recomputeCryptoHash bool
3838
}
3939

40-
// NewStateTrie contructs a new empty StateTrie
41-
func NewStateTrie() *StateTrie {
40+
// NewStateImpl contructs a new empty StateTrie
41+
func NewStateImpl() *StateTrie {
4242
return &StateTrie{}
4343
}
4444

core/ledger/statemgmt/trie/state_trie_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ import (
2525

2626
func TestStateTrie_ComputeHash_AllInMemory_NoContents(t *testing.T) {
2727
testDBWrapper.CleanDB(t)
28-
stateTrie := NewStateTrie()
28+
stateTrie := NewStateImpl()
2929
stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t}
3030
hash := stateTrieTestWrapper.PrepareWorkingSetAndComputeCryptoHash(statemgmt.NewStateDelta())
3131
testutil.AssertEquals(t, hash, nil)
3232
}
3333

3434
func TestStateTrie_ComputeHash_AllInMemory(t *testing.T) {
3535
testDBWrapper.CleanDB(t)
36-
stateTrie := NewStateTrie()
36+
stateTrie := NewStateImpl()
3737
stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t}
3838
stateDelta := statemgmt.NewStateDelta()
3939

@@ -76,7 +76,7 @@ func TestStateTrie_ComputeHash_AllInMemory(t *testing.T) {
7676

7777
func TestStateTrie_GetSet_WithDB(t *testing.T) {
7878
testDBWrapper.CleanDB(t)
79-
stateTrie := NewStateTrie()
79+
stateTrie := NewStateImpl()
8080
stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t}
8181
stateDelta := statemgmt.NewStateDelta()
8282
stateDelta.Set("chaincodeID1", "key1", []byte("value1"), nil)
@@ -100,7 +100,7 @@ func TestStateTrie_GetSet_WithDB(t *testing.T) {
100100

101101
func TestStateTrie_ComputeHash_WithDB_Spread_Keys(t *testing.T) {
102102
testDBWrapper.CleanDB(t)
103-
stateTrie := NewStateTrie()
103+
stateTrie := NewStateImpl()
104104
stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t}
105105

106106
// Add a few keys and write to DB
@@ -180,7 +180,7 @@ func TestStateTrie_ComputeHash_WithDB_Spread_Keys(t *testing.T) {
180180

181181
func TestStateTrie_ComputeHash_WithDB_Staggered_Keys(t *testing.T) {
182182
testDBWrapper.CleanDB(t)
183-
stateTrie := NewStateTrie()
183+
stateTrie := NewStateImpl()
184184
stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t}
185185

186186
/////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)