Skip to content

Commit f8f055f

Browse files
Zhijie Shenlizhefeng
Zhijie Shen
authored andcommitted
Refactor config module
1 parent 3676653 commit f8f055f

22 files changed

+695
-420
lines changed

Gopkg.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ run:
131131
$(ECHO_V)rm -f ./e2etest/chain*.db
132132
$(GOBUILD) -o ./bin/$(BUILD_TARGET_SERVER) -v ./$(BUILD_TARGET_SERVER)
133133
export LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(PWD)/crypto/lib
134-
./bin/$(BUILD_TARGET_SERVER) -config=e2etest/config_local_delegate.yaml -log-level=debug
134+
./bin/$(BUILD_TARGET_SERVER) -config-path=e2etest/config_local_delegate.yaml -log-level=debug
135135

136136
.PHONY: docker
137137
docker:

blockchain/blockchain_test.go

+34-27
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,16 @@ func TestCreateBlockchain(t *testing.T) {
134134
assert := assert.New(t)
135135
ctx := context.Background()
136136

137-
config, err := config.LoadConfigWithPathWithoutValidation(testingConfigPath)
137+
config.Path = testingConfigPath
138+
cfg, err := config.New()
138139
assert.Nil(err)
139140
// disable account-based testing
140-
config.Chain.TrieDBPath = ""
141+
cfg.Chain.TrieDBPath = ""
141142
// Disable block reward to make bookkeeping easier
142143
Gen.BlockReward = uint64(0)
143144

144145
// create chain
145-
bc := NewBlockchain(config, InMemDaoOption())
146+
bc := NewBlockchain(cfg, InMemDaoOption())
146147
assert.NotNil(bc)
147148
height, err := bc.TipHeight()
148149
assert.Nil(err)
@@ -187,7 +188,8 @@ func TestCreateBlockchain(t *testing.T) {
187188
func TestLoadBlockchainfromDB(t *testing.T) {
188189
require := require.New(t)
189190

190-
config, err := config.LoadConfigWithPathWithoutValidation(testingConfigPath)
191+
config.Path = testingConfigPath
192+
cfg, err := config.New()
191193
require.Nil(err)
192194
util.CleanupPath(t, testTriePath)
193195
defer util.CleanupPath(t, testTriePath)
@@ -197,15 +199,15 @@ func TestLoadBlockchainfromDB(t *testing.T) {
197199
// Disable block reward to make bookkeeping easier
198200
Gen.BlockReward = uint64(0)
199201

200-
config.Chain.TrieDBPath = testTriePath
201-
config.Chain.ChainDBPath = testDBPath
202+
cfg.Chain.TrieDBPath = testTriePath
203+
cfg.Chain.ChainDBPath = testDBPath
202204

203-
sf, err := state.NewFactory(config, state.DefaultTrieOption())
205+
sf, err := state.NewFactory(cfg, state.DefaultTrieOption())
204206
require.Nil(err)
205207
sf.CreateState(ta.Addrinfo["miner"].RawAddress, Gen.TotalSupply)
206208

207209
// Create a blockchain from scratch
208-
bc := NewBlockchain(config, PrecreatedStateFactoryOption(sf), BoltDBDaoOption())
210+
bc := NewBlockchain(cfg, PrecreatedStateFactoryOption(sf), BoltDBDaoOption())
209211
require.NotNil(bc)
210212
height, err := bc.TipHeight()
211213
require.Nil(err)
@@ -215,7 +217,7 @@ func TestLoadBlockchainfromDB(t *testing.T) {
215217
bc.Stop(ctx)
216218

217219
// Load a blockchain from DB
218-
bc = NewBlockchain(config, PrecreatedStateFactoryOption(sf), BoltDBDaoOption())
220+
bc = NewBlockchain(cfg, PrecreatedStateFactoryOption(sf), BoltDBDaoOption())
219221
defer bc.Stop(ctx)
220222
require.NotNil(bc)
221223

@@ -377,13 +379,14 @@ func TestLoadBlockchainfromDB(t *testing.T) {
377379
}
378380

379381
func TestBlockchain_Validator(t *testing.T) {
380-
config, err := config.LoadConfigWithPathWithoutValidation(testingConfigPath)
382+
config.Path = testingConfigPath
383+
cfg, err := config.New()
381384
assert.Nil(t, err)
382385
// disable account-based testing
383-
config.Chain.TrieDBPath = ""
386+
cfg.Chain.TrieDBPath = ""
384387

385388
ctx := context.Background()
386-
bc := NewBlockchain(config, InMemDaoOption(), InMemStateFactoryOption())
389+
bc := NewBlockchain(cfg, InMemDaoOption(), InMemStateFactoryOption())
387390
defer bc.Stop(ctx)
388391
assert.NotNil(t, bc)
389392

@@ -394,13 +397,14 @@ func TestBlockchain_Validator(t *testing.T) {
394397
}
395398

396399
func TestBlockchain_MintNewDummyBlock(t *testing.T) {
397-
config, err := config.LoadConfigWithPathWithoutValidation(testingConfigPath)
400+
config.Path = testingConfigPath
401+
cfg, err := config.New()
398402
assert.Nil(t, err)
399403
// disable account-based testing
400-
config.Chain.TrieDBPath = ""
404+
cfg.Chain.TrieDBPath = ""
401405

402406
ctx := context.Background()
403-
bc := NewBlockchain(config, InMemDaoOption(), InMemStateFactoryOption())
407+
bc := NewBlockchain(cfg, InMemDaoOption(), InMemStateFactoryOption())
404408
defer bc.Stop(ctx)
405409
assert.NotNil(t, bc)
406410

@@ -412,25 +416,26 @@ func TestBlockchain_MintNewDummyBlock(t *testing.T) {
412416
func TestBlockchainInitialCandidate(t *testing.T) {
413417
require := require.New(t)
414418

415-
config, err := config.LoadConfigWithPathWithoutValidation(testingConfigPath)
419+
config.Path = testingConfigPath
420+
cfg, err := config.New()
416421
require.Nil(err)
417422
util.CleanupPath(t, testTriePath)
418423
defer util.CleanupPath(t, testTriePath)
419424
util.CleanupPath(t, testDBPath)
420425
defer util.CleanupPath(t, testDBPath)
421426

422-
config.Chain.TrieDBPath = testTriePath
423-
config.Chain.ChainDBPath = testDBPath
427+
cfg.Chain.TrieDBPath = testTriePath
428+
cfg.Chain.ChainDBPath = testDBPath
424429
// Disable block reward to make bookkeeping easier
425430
Gen.BlockReward = uint64(0)
426431

427-
sf, err := state.NewFactory(config, state.DefaultTrieOption())
432+
sf, err := state.NewFactory(cfg, state.DefaultTrieOption())
428433
require.Nil(err)
429434

430435
height, candidate := sf.Candidates()
431436
require.True(height == 0)
432437
require.True(len(candidate) == 0)
433-
bc := NewBlockchain(config, PrecreatedStateFactoryOption(sf), BoltDBDaoOption())
438+
bc := NewBlockchain(cfg, PrecreatedStateFactoryOption(sf), BoltDBDaoOption())
434439
require.NotNil(t, bc)
435440
// TODO: change the value when Candidates size is changed
436441
height, candidate = sf.Candidates()
@@ -440,23 +445,24 @@ func TestBlockchainInitialCandidate(t *testing.T) {
440445

441446
func TestCoinbaseTransfer(t *testing.T) {
442447
require := require.New(t)
443-
config, err := config.LoadConfigWithPathWithoutValidation(testingConfigPath)
448+
config.Path = testingConfigPath
449+
cfg, err := config.New()
444450
require.Nil(err)
445451
util.CleanupPath(t, testTriePath)
446452
defer util.CleanupPath(t, testTriePath)
447453
util.CleanupPath(t, testDBPath)
448454
defer util.CleanupPath(t, testDBPath)
449455

450-
config.Chain.TrieDBPath = testTriePath
451-
config.Chain.ChainDBPath = testDBPath
456+
cfg.Chain.TrieDBPath = testTriePath
457+
cfg.Chain.ChainDBPath = testDBPath
452458

453-
sf, err := state.NewFactory(config, state.DefaultTrieOption())
459+
sf, err := state.NewFactory(cfg, state.DefaultTrieOption())
454460
require.Nil(err)
455461
sf.CreateState(ta.Addrinfo["miner"].RawAddress, Gen.TotalSupply)
456462

457463
Gen.BlockReward = uint64(10)
458464

459-
bc := NewBlockchain(config, PrecreatedStateFactoryOption(sf), BoltDBDaoOption())
465+
bc := NewBlockchain(cfg, PrecreatedStateFactoryOption(sf), BoltDBDaoOption())
460466
require.NotNil(bc)
461467
height, err := bc.TipHeight()
462468
require.Nil(err)
@@ -484,11 +490,12 @@ func TestCoinbaseTransfer(t *testing.T) {
484490
func TestBlockchain_StateByAddr(t *testing.T) {
485491
require := require.New(t)
486492

487-
config, err := config.LoadConfigWithPathWithoutValidation(testingConfigPath)
493+
config.Path = testingConfigPath
494+
cfg, err := config.New()
488495
require.Nil(err)
489496
// disable account-based testing
490497
// create chain
491-
bc := NewBlockchain(config, InMemDaoOption(), InMemStateFactoryOption())
498+
bc := NewBlockchain(cfg, InMemDaoOption(), InMemStateFactoryOption())
492499
require.NotNil(bc)
493500

494501
s, _ := bc.StateByAddr(Gen.CreatorAddr)

cli/iotc/cmd/iotc_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import (
2020
var configFile = "../../../e2etest/config_local_delegate.yaml"
2121

2222
func Test_All(t *testing.T) {
23-
cfg, err := config.LoadConfigWithPath(configFile)
23+
config.Path = configFile
24+
cfg, err := config.New()
2425
require.Nil(t, err)
2526
httpPort := cfg.Explorer.Addr
2627
explorer.StartJSONServer(nil, nil, nil, nil, true, httpPort, 0)

cli/iotc/cmd/root.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ func getClient() (eidl.Explorer, error) {
5656
logger.Error().Msg("please set GOPATH environment variable")
5757
gopath = build.Default.GOPATH
5858
}
59-
configFile := gopath + yamlPath
60-
cfg, err := config.LoadConfigWithPath(configFile)
59+
config.Path = gopath + yamlPath
60+
cfg, err := config.New()
6161
if err != nil {
6262
logger.Error().Err(err).Msg("cannot access config file")
6363
return nil, err
@@ -76,8 +76,8 @@ func getCfg() (*config.Config, error) {
7676
logger.Error().Msg("please set GOPATH environment variable")
7777
gopath = build.Default.GOPATH
7878
}
79-
configFile := gopath + yamlPath
80-
cfg, err := config.LoadConfigWithPath(configFile)
79+
config.Path = gopath + yamlPath
80+
cfg, err := config.New()
8181
if err != nil {
8282
logger.Error().Err(err).Msg("cannot access config file")
8383
return nil, err

0 commit comments

Comments
 (0)