Skip to content

Commit

Permalink
Abstract string to const
Browse files Browse the repository at this point in the history
Avoid typo same string in multiple places, extract to const instead.

Change-Id: Ic2a92fe1e75827883f7f0f4e707fdd2bc1f8f265
Signed-off-by: grapebaba <[email protected]>
  • Loading branch information
GrapeBaBa committed Aug 24, 2016
1 parent 981a222 commit 2e3211f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions core/ledger/statemgmt/raw/state_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type StateImpl struct {
stateDelta *statemgmt.StateDelta
}

// NewRawState constructs new instance of raw state
func NewRawState() *StateImpl {
// NewStateImpl constructs new instance of raw state
func NewStateImpl() *StateImpl {
return &StateImpl{}
}

Expand Down
6 changes: 3 additions & 3 deletions core/ledger/statemgmt/state/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

var loadConfigOnce sync.Once

var stateImplName string
var stateImplName stateImplType
var stateImplConfigs map[string]interface{}
var deltaHistorySize int

Expand All @@ -35,7 +35,7 @@ func initConfig() {

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

Expand Down
18 changes: 13 additions & 5 deletions core/ledger/statemgmt/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ const defaultStateImpl = "buckettree"

var stateImpl statemgmt.HashableState

type stateImplType string

const (
buckettreeType stateImplType = "buckettree"
trieType stateImplType = "trie"
rawType stateImplType = "raw"
)

// State structure for maintaining world state.
// This encapsulates a particular implementation for managing the state persistence
// This is not thread safe
Expand All @@ -53,12 +61,12 @@ func NewState() *State {
initConfig()
logger.Infof("Initializing state implementation [%s]", stateImplName)
switch stateImplName {
case "buckettree":
case buckettreeType:
stateImpl = buckettree.NewStateImpl()
case "trie":
stateImpl = trie.NewStateTrie()
case "raw":
stateImpl = raw.NewRawState()
case trieType:
stateImpl = trie.NewStateImpl()
case rawType:
stateImpl = raw.NewStateImpl()
default:
panic("Should not reach here. Configs should have checked for the stateImplName being a valid names ")
}
Expand Down
2 changes: 1 addition & 1 deletion core/ledger/statemgmt/trie/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type stateTrieTestWrapper struct {
}

func newStateTrieTestWrapper(t *testing.T) *stateTrieTestWrapper {
return &stateTrieTestWrapper{NewStateTrie(), t}
return &stateTrieTestWrapper{NewStateImpl(), t}
}

func (stateTrieTestWrapper *stateTrieTestWrapper) Get(chaincodeID string, key string) []byte {
Expand Down
4 changes: 2 additions & 2 deletions core/ledger/statemgmt/trie/state_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type StateTrie struct {
recomputeCryptoHash bool
}

// NewStateTrie contructs a new empty StateTrie
func NewStateTrie() *StateTrie {
// NewStateImpl contructs a new empty StateTrie
func NewStateImpl() *StateTrie {
return &StateTrie{}
}

Expand Down
10 changes: 5 additions & 5 deletions core/ledger/statemgmt/trie/state_trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import (

func TestStateTrie_ComputeHash_AllInMemory_NoContents(t *testing.T) {
testDBWrapper.CleanDB(t)
stateTrie := NewStateTrie()
stateTrie := NewStateImpl()
stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t}
hash := stateTrieTestWrapper.PrepareWorkingSetAndComputeCryptoHash(statemgmt.NewStateDelta())
testutil.AssertEquals(t, hash, nil)
}

func TestStateTrie_ComputeHash_AllInMemory(t *testing.T) {
testDBWrapper.CleanDB(t)
stateTrie := NewStateTrie()
stateTrie := NewStateImpl()
stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t}
stateDelta := statemgmt.NewStateDelta()

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

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

func TestStateTrie_ComputeHash_WithDB_Spread_Keys(t *testing.T) {
testDBWrapper.CleanDB(t)
stateTrie := NewStateTrie()
stateTrie := NewStateImpl()
stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t}

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

func TestStateTrie_ComputeHash_WithDB_Staggered_Keys(t *testing.T) {
testDBWrapper.CleanDB(t)
stateTrie := NewStateTrie()
stateTrie := NewStateImpl()
stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t}

/////////////////////////////////////////////////////////
Expand Down

0 comments on commit 2e3211f

Please sign in to comment.