github.com/0chain/gosdk@v1.17.11/core/conf/config.go (about)

     1  // Provides the data structures and methods to work with the configuration data structure.
     2  // This includes parsing, loading, and saving the configuration data structure.
     3  // It uses the viper library to parse and manage the configuration data structure.
     4  package conf
     5  
     6  import (
     7  	"errors"
     8  	"net/url"
     9  	"os"
    10  	"strings"
    11  
    12  	thrown "github.com/0chain/errors"
    13  	"github.com/0chain/gosdk/core/sys"
    14  	"github.com/spf13/viper"
    15  )
    16  
    17  const (
    18  	// DefaultMinSubmit default value for min_submit
    19  	DefaultMinSubmit = 10
    20  	// DefaultMinConfirmation default value for min_confirmation
    21  	DefaultMinConfirmation = 10
    22  	// DefaultMaxTxnQuery default value for max_txn_query
    23  	DefaultMaxTxnQuery = 5
    24  	// DefaultConfirmationChainLength default value for confirmation_chain_length
    25  	DefaultConfirmationChainLength = 3
    26  	// DefaultQuerySleepTime default value for query_sleep_time
    27  	DefaultQuerySleepTime = 5
    28  	// DefaultSharderConsensous default consensous to take make SCRestAPI calls
    29  	DefaultSharderConsensous = 3
    30  )
    31  
    32  // Config settings from ~/.zcn/config.yaml
    33  //
    34  //	block_worker: http://198.18.0.98:9091
    35  //	signature_scheme: bls0chain
    36  //	min_submit: 50
    37  //	min_confirmation: 50
    38  //	confirmation_chain_length: 3
    39  //	max_txn_query: 5
    40  //	query_sleep_time: 5
    41  //	# # OPTIONAL - Uncomment to use/ Add more if you want
    42  //	# preferred_blobbers:
    43  //	#   - http://one.devnet-0chain.net:31051
    44  //	#   - http://one.devnet-0chain.net:31052
    45  //	#   - http://one.devnet-0chain.net:31053
    46  type Config struct {
    47  	// BlockWorker the url of 0dns's network api
    48  	BlockWorker string `json:"block_worker,omitempty"`
    49  	// PreferredBlobbers preferred blobbers on new allocation
    50  	PreferredBlobbers []string `json:"preferred_blobbers,omitempty"`
    51  
    52  	// MinSubmit mininal submit from blobber
    53  	MinSubmit int `json:"min_submit,omitempty"`
    54  	// MinConfirmation mininal confirmation from sharders
    55  	MinConfirmation int `json:"min_confirmation,omitempty"`
    56  	// CconfirmationChainLength minial confirmation chain length
    57  	ConfirmationChainLength int `json:"confirmation_chain_length,omitempty"`
    58  
    59  	// additional settings depending network latency
    60  	// MaxTxnQuery maximum transcation query from sharders
    61  	MaxTxnQuery int `json:"max_txn_query,omitempty"`
    62  	// QuerySleepTime sleep time before transcation query
    63  	QuerySleepTime int `json:"query_sleep_time,omitempty"`
    64  
    65  	// SignatureScheme signature scheme
    66  	SignatureScheme string `json:"signature_scheme,omitempty"`
    67  	// ChainID which blockchain it is working
    68  	ChainID string `json:"chain_id,omitempty"`
    69  
    70  	VerifyOptimistic bool
    71  
    72  	// Ethereum node: "https://ropsten.infura.io/v3/xxxxxxxxxxxxxxx"
    73  	EthereumNode string `json:"ethereum_node,omitempty"`
    74  
    75  	// ZboxHost 0box api host host: "https://0box.dev.0chain.net"
    76  	ZboxHost string `json:"zbox_host"`
    77  	// ZboxAppType app type name
    78  	ZboxAppType string `json:"zbox_app_type"`
    79  	// SharderConsensous is consensous for when quering for SCRestAPI calls
    80  	SharderConsensous int          `json:"sharder_consensous"`
    81  	ZauthServer       string       `json:"zauth_server"`
    82  	V                 *viper.Viper `json:"-"`
    83  }
    84  
    85  // LoadConfigFile load and parse SDK Config from file
    86  //   - file: config file path (full path)
    87  func LoadConfigFile(file string) (Config, error) {
    88  
    89  	var cfg Config
    90  	var err error
    91  
    92  	_, err = sys.Files.Stat(file)
    93  
    94  	if err != nil {
    95  		if errors.Is(err, os.ErrNotExist) {
    96  			return cfg, thrown.Throw(ErrMssingConfig, file)
    97  		}
    98  		return cfg, err
    99  	}
   100  
   101  	v := viper.New()
   102  
   103  	v.SetConfigFile(file)
   104  
   105  	if err := v.ReadInConfig(); err != nil {
   106  		return cfg, thrown.Throw(ErrBadParsing, err.Error())
   107  	}
   108  
   109  	cfg, err = LoadConfig(v)
   110  	if err != nil {
   111  		return cfg, err
   112  	}
   113  
   114  	cfg.V = v
   115  
   116  	return cfg, nil
   117  }
   118  
   119  // LoadConfig load and parse config
   120  func LoadConfig(v Reader) (Config, error) {
   121  
   122  	var cfg Config
   123  
   124  	blockWorker := strings.TrimSpace(v.GetString("block_worker"))
   125  
   126  	if !isURL(blockWorker) {
   127  		return cfg, thrown.Throw(ErrInvalidValue, "block_worker="+blockWorker)
   128  	}
   129  
   130  	minSubmit := v.GetInt("min_submit")
   131  	if minSubmit < 1 {
   132  		minSubmit = DefaultMinSubmit
   133  	} else if minSubmit > 100 {
   134  		minSubmit = 100
   135  	}
   136  
   137  	minCfm := v.GetInt("min_confirmation")
   138  
   139  	if minCfm < 1 {
   140  		minCfm = DefaultMinConfirmation
   141  	} else if minCfm > 100 {
   142  		minCfm = 100
   143  	}
   144  
   145  	CfmChainLength := v.GetInt("confirmation_chain_length")
   146  
   147  	if CfmChainLength < 1 {
   148  		CfmChainLength = DefaultConfirmationChainLength
   149  	}
   150  
   151  	// additional settings depending network latency
   152  	maxTxnQuery := v.GetInt("max_txn_query")
   153  	if maxTxnQuery < 1 {
   154  		maxTxnQuery = DefaultMaxTxnQuery
   155  	}
   156  
   157  	querySleepTime := v.GetInt("query_sleep_time")
   158  	if querySleepTime < 1 {
   159  		querySleepTime = DefaultQuerySleepTime
   160  	}
   161  	VerifyOptimisticString := v.GetString("verify_optimistic")
   162  	if VerifyOptimisticString == "true" {
   163  		cfg.VerifyOptimistic = true
   164  	}
   165  
   166  	sharderConsensous := v.GetInt("sharder_consensous")
   167  	if sharderConsensous < 1 {
   168  		sharderConsensous = DefaultSharderConsensous
   169  	}
   170  
   171  	cfg.BlockWorker = blockWorker
   172  	cfg.PreferredBlobbers = v.GetStringSlice("preferred_blobbers")
   173  	cfg.MinSubmit = minSubmit
   174  	cfg.MinConfirmation = minCfm
   175  	cfg.ConfirmationChainLength = CfmChainLength
   176  	cfg.MaxTxnQuery = maxTxnQuery
   177  	cfg.QuerySleepTime = querySleepTime
   178  	cfg.SharderConsensous = sharderConsensous
   179  
   180  	cfg.SignatureScheme = v.GetString("signature_scheme")
   181  	cfg.ChainID = v.GetString("chain_id")
   182  	cfg.ZauthServer = v.GetString("zauth.server")
   183  
   184  	return cfg, nil
   185  }
   186  
   187  func isURL(s string) bool {
   188  	u, err := url.Parse(s)
   189  	return err == nil && u.Scheme != "" && u.Host != ""
   190  }