Skip to content

Commit

Permalink
fix(transcation):#195 remove global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi committed Aug 10, 2021
1 parent d4d05f4 commit 739b482
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 45 deletions.
43 changes: 9 additions & 34 deletions zboxcore/conf/conf.go → core/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ import (
"github.com/spf13/viper"
)

var (
// DefaultConfigFileName default config file in ~/.zcn
DefaultConfigFileName = "config.yaml"

// Config current config instance for zbox
Config ZConfig
)

var (
// ErrMssingConfig config file is missing
ErrMssingConfig = errors.New("[conf]missing config file")
Expand All @@ -31,43 +23,28 @@ var (
ErrBadFormat = errors.New("[conf]bad format")
)

func init() {
LoadDefault()
}

// LoadDefault load and parse config from ~/.zcn/config.yaml
func LoadDefault() error {
return Load(DefaultConfigFileName)
func LoadDefault() (Config, error) {
return Load("config.yaml")
}

// Load load and parse config file in ~/.zcn folder. it is ~/.zcn/config.yaml if file is invalid.
// Example:
// conf.Load("stream.yaml"), it will load settings from ~/.zcn/stream.yaml
func Load(fileName string) error {
func Load(fileName string) (Config, error) {

var cfg Config

file := path.Join(getConfigDir(), fileName)
_, err := os.Stat(file)

if err != nil {
if errors.Is(err, os.ErrNotExist) {
return thrown.Throw(ErrMssingConfig, err.Error())
return cfg, thrown.Throw(ErrMssingConfig, err.Error())
}
return err
return cfg, err
}

cfg, err := loadConfigFile(file)

if err != nil {
return err
}

Config = cfg
return nil
}

func loadConfigFile(file string) (ZConfig, error) {

var cfg ZConfig

v := viper.New()

v.SetConfigFile(file)
Expand All @@ -85,9 +62,7 @@ func loadConfigFile(file string) (ZConfig, error) {
minSubmit := v.GetInt("min_submit")

if minSubmit < 1 {
minSubmit = 50
} else if minSubmit > 100 {
minSubmit = 100
minSubmit = 3
}

minCfm := v.GetInt("min_confirmation")
Expand Down
6 changes: 3 additions & 3 deletions zboxcore/conf/conf_test.go → core/conf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestMissingConfig(t *testing.T) {

configFile := "missing_config.yaml"

err := Load(configFile)
_, err := Load(configFile)

require.ErrorIs(t, err, ErrMssingConfig)
}
Expand All @@ -36,7 +36,7 @@ func TestBadFormat(t *testing.T) {
`)
defer tearDownConfig("bad_format.yaml")

err := Load("bad_format.yaml")
_, err := Load("bad_format.yaml")

require.ErrorIs(t, err, ErrBadFormat)
}
Expand All @@ -48,7 +48,7 @@ block_worker: 127.0.0.1:9091
`)
defer tearDownConfig("invalid_blockworker.yaml")

err := Load("invalid_blockworker.yaml")
_, err := Load("invalid_blockworker.yaml")

require.ErrorIs(t, err, ErrInvalidValue)
}
4 changes: 2 additions & 2 deletions zboxcore/conf/config.go → core/conf/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package conf

// ZConfig settings from ~/.zcn/config.yaml
// Config settings from ~/.zcn/config.yaml
// block_worker: http://198.18.0.98:9091
// signature_scheme: bls0chain
// min_submit: 50
Expand All @@ -13,7 +13,7 @@ package conf
// # - http://one.devnet-0chain.net:31051
// # - http://one.devnet-0chain.net:31052
// # - http://one.devnet-0chain.net:31053
type ZConfig struct {
type Config struct {
// BlockWorker the url of 0dns's network api
BlockWorker string
// PreferredBlobbers preferred blobbers on new allocation
Expand Down
4 changes: 2 additions & 2 deletions zboxcore/conf/network.go → core/conf/network.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package conf

// ZNetwork settings from ~/.zcn/network.yaml
type ZNetwork struct {
// Network settings from ~/.zcn/network.yaml
type Network struct {
// Sharders sharder list of blockchain
Sharders []string
// Miners miner list of blockchain
Expand Down
8 changes: 4 additions & 4 deletions core/transaction/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

"github.com/0chain/errors"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/util"
"github.com/0chain/gosdk/zboxcore/conf"
)

const TXN_SUBMIT_URL = "v1/transaction/put"
Expand Down Expand Up @@ -206,7 +206,7 @@ func sendTransactionToURL(url string, txn *Transaction, wg *sync.WaitGroup) ([]b
}

// VerifyTransaction query transaction status from sharders, and verify it by mininal confirmation
func VerifyTransaction(txnHash string, sharders []string) (*Transaction, error) {
func VerifyTransaction(txnHash string, sharders []string, cfg conf.Config) (*Transaction, error) {
numSharders := len(sharders)

if numSharders == 0 {
Expand Down Expand Up @@ -274,7 +274,7 @@ func VerifyTransaction(txnHash string, sharders []string) (*Transaction, error)

consensus := int(float64(numSuccess) / float64(numSharders) * 100)

if consensus > 0 && consensus >= conf.Config.MinConfirmation {
if consensus > 0 && consensus >= cfg.MinConfirmation {

if retTxn == nil {
return nil, errors.Throw(ErrNoTxnDetail, strings.Join(msgList, "\r\n"))
Expand All @@ -283,7 +283,7 @@ func VerifyTransaction(txnHash string, sharders []string) (*Transaction, error)
return retTxn, nil
}

msgList[0] = fmt.Sprintf("want %v, but got %v ", conf.Config.MinConfirmation, consensus)
msgList[0] = fmt.Sprintf("want %v, but got %v ", cfg.MinConfirmation, consensus)

return nil, errors.Throw(ErrTooLessConfirmation, strings.Join(msgList, "\r\n"))

Expand Down

0 comments on commit 739b482

Please sign in to comment.