From 2e3211f3f50e904416197cdd57bc881345392cc9 Mon Sep 17 00:00:00 2001 From: grapebaba <281165273@qq.com> Date: Mon, 22 Aug 2016 15:58:25 +0800 Subject: [PATCH] Abstract string to const Avoid typo same string in multiple places, extract to const instead. Change-Id: Ic2a92fe1e75827883f7f0f4e707fdd2bc1f8f265 Signed-off-by: grapebaba <281165273@qq.com> --- core/ledger/statemgmt/raw/state_impl.go | 4 ++-- core/ledger/statemgmt/state/config.go | 6 +++--- core/ledger/statemgmt/state/state.go | 18 +++++++++++++----- core/ledger/statemgmt/trie/pkg_test.go | 2 +- core/ledger/statemgmt/trie/state_trie.go | 4 ++-- core/ledger/statemgmt/trie/state_trie_test.go | 10 +++++----- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/core/ledger/statemgmt/raw/state_impl.go b/core/ledger/statemgmt/raw/state_impl.go index 6885ec6f9f8..5229c226ee4 100644 --- a/core/ledger/statemgmt/raw/state_impl.go +++ b/core/ledger/statemgmt/raw/state_impl.go @@ -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{} } diff --git a/core/ledger/statemgmt/state/config.go b/core/ledger/statemgmt/state/config.go index ab0a0279037..11fdd06f032 100644 --- a/core/ledger/statemgmt/state/config.go +++ b/core/ledger/statemgmt/state/config.go @@ -25,7 +25,7 @@ import ( var loadConfigOnce sync.Once -var stateImplName string +var stateImplName stateImplType var stateImplConfigs map[string]interface{} var deltaHistorySize int @@ -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]", @@ -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)) } diff --git a/core/ledger/statemgmt/state/state.go b/core/ledger/statemgmt/state/state.go index 293a37e5fb4..7556aa4949d 100644 --- a/core/ledger/statemgmt/state/state.go +++ b/core/ledger/statemgmt/state/state.go @@ -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 @@ -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 ") } diff --git a/core/ledger/statemgmt/trie/pkg_test.go b/core/ledger/statemgmt/trie/pkg_test.go index 5f29b59b66f..a9763bcc0a6 100644 --- a/core/ledger/statemgmt/trie/pkg_test.go +++ b/core/ledger/statemgmt/trie/pkg_test.go @@ -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 { diff --git a/core/ledger/statemgmt/trie/state_trie.go b/core/ledger/statemgmt/trie/state_trie.go index ad200e8006d..e73d293952b 100644 --- a/core/ledger/statemgmt/trie/state_trie.go +++ b/core/ledger/statemgmt/trie/state_trie.go @@ -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{} } diff --git a/core/ledger/statemgmt/trie/state_trie_test.go b/core/ledger/statemgmt/trie/state_trie_test.go index a9c2f21102f..3be8b39d6c9 100644 --- a/core/ledger/statemgmt/trie/state_trie_test.go +++ b/core/ledger/statemgmt/trie/state_trie_test.go @@ -25,7 +25,7 @@ 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) @@ -33,7 +33,7 @@ func TestStateTrie_ComputeHash_AllInMemory_NoContents(t *testing.T) { func TestStateTrie_ComputeHash_AllInMemory(t *testing.T) { testDBWrapper.CleanDB(t) - stateTrie := NewStateTrie() + stateTrie := NewStateImpl() stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t} stateDelta := statemgmt.NewStateDelta() @@ -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) @@ -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 @@ -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} /////////////////////////////////////////////////////////