OSDN Git Service

tmp save for branch merge
[bytom/bytom.git] / config / config.go
1 package config
2
3 import (
4         "fmt"
5         "path/filepath"
6         "time"
7
8         //"github.com/bytom/types"
9 )
10
11 type Config struct {
12         // Top level options use an anonymous struct
13         BaseConfig `mapstructure:",squash"`
14
15         // Options for services
16         RPC       *RPCConfig       `mapstructure:"rpc"`
17         P2P       *P2PConfig       `mapstructure:"p2p"`
18 }
19
20 func DefaultConfig() *Config {
21         return &Config{
22                 BaseConfig: DefaultBaseConfig(),
23                 RPC:        DefaultRPCConfig(),
24                 P2P:        DefaultP2PConfig(),
25         }
26 }
27
28 func TestConfig() *Config {
29         return &Config{
30                 BaseConfig: TestBaseConfig(),
31                 RPC:        TestRPCConfig(),
32                 P2P:        TestP2PConfig(),
33         }
34 }
35
36 // Set the RootDir for all Config structs
37 func (cfg *Config) SetRoot(root string) *Config {
38         cfg.BaseConfig.RootDir = root
39         cfg.RPC.RootDir = root
40         cfg.P2P.RootDir = root
41         return cfg
42 }
43
44 //-----------------------------------------------------------------------------
45 // BaseConfig
46
47 type BaseConfig struct {
48         // The root directory for all data.
49         // This should be set in viper so it can unmarshal into this struct
50         RootDir string `mapstructure:"home"`
51
52         // The ID of the chain to join (should be signed with every transaction and vote)
53         ChainID string `mapstructure:"chain_id"`
54
55         // A JSON file containing the initial validator set and other meta data
56         Genesis string `mapstructure:"genesis_file"`
57
58         // A JSON file containing the private key to use as a validator in the consensus protocol
59         PrivateKey string `mapstructure:"private_key"`
60
61         // A custom human readable name for this node
62         Moniker string `mapstructure:"moniker"`
63
64         // Output level for logging
65         LogLevel string `mapstructure:"log_level"`
66
67         // TCP or UNIX socket address for the profiling server to listen on
68         ProfListenAddress string `mapstructure:"prof_laddr"`
69
70         // If this node is many blocks behind the tip of the chain, FastSync
71         // allows them to catchup quickly by downloading blocks in parallel
72         // and verifying their commits
73         FastSync bool `mapstructure:"fast_sync"`
74
75         FilterPeers bool `mapstructure:"filter_peers"` // false
76
77         // What indexer to use for transactions
78         TxIndex string `mapstructure:"tx_index"`
79
80         // Database backend: leveldb | memdb
81         DBBackend string `mapstructure:"db_backend"`
82
83         // Database directory
84         DBPath string `mapstructure:"db_dir"`
85
86         ApiAddress string `mapstructure:"api_addr"`
87
88         Time time.Time
89 }
90
91 func DefaultBaseConfig() BaseConfig {
92         return BaseConfig{
93                 Genesis:           "genesis.json",
94                 Moniker:           "anonymous",
95                 LogLevel:          DefaultPackageLogLevels(),
96                 ProfListenAddress: "",
97                 FastSync:          true,
98                 FilterPeers:       false,
99                 TxIndex:           "kv",
100                 DBBackend:         "leveldb",
101                 DBPath:            "data",
102         }
103 }
104
105 func TestBaseConfig() BaseConfig {
106         conf := DefaultBaseConfig()
107         conf.ChainID = "bytom_test"
108         conf.FastSync = false
109         conf.DBBackend = "memdb"
110         return conf
111 }
112
113 func (b BaseConfig) GenesisFile() string {
114         return rootify(b.Genesis, b.RootDir)
115 }
116
117 func (b BaseConfig) DBDir() string {
118         return rootify(b.DBPath, b.RootDir)
119 }
120
121 func DefaultLogLevel() string {
122         return "info"
123 }
124
125 func DefaultPackageLogLevels() string {
126         return fmt.Sprintf("state:info,*:%s", DefaultLogLevel())
127 }
128
129 //-----------------------------------------------------------------------------
130 // RPCConfig
131
132 type RPCConfig struct {
133         RootDir string `mapstructure:"home"`
134
135         // TCP or UNIX socket address for the RPC server to listen on
136         ListenAddress string `mapstructure:"laddr"`
137
138         // TCP or UNIX socket address for the gRPC server to listen on
139         // NOTE: This server only supports /broadcast_tx_commit
140         GRPCListenAddress string `mapstructure:"grpc_laddr"`
141
142         // Activate unsafe RPC commands like /dial_seeds and /unsafe_flush_mempool
143         Unsafe bool `mapstructure:"unsafe"`
144 }
145
146 func DefaultRPCConfig() *RPCConfig {
147         return &RPCConfig{
148                 ListenAddress:     "tcp://0.0.0.0:46657",
149                 GRPCListenAddress: "",
150                 Unsafe:            false,
151         }
152 }
153
154 func TestRPCConfig() *RPCConfig {
155         conf := DefaultRPCConfig()
156         conf.ListenAddress = "tcp://0.0.0.0:36657"
157         conf.GRPCListenAddress = "tcp://0.0.0.0:36658"
158         conf.Unsafe = true
159         return conf
160 }
161
162 //-----------------------------------------------------------------------------
163 // P2PConfig
164
165 type P2PConfig struct {
166         RootDir        string `mapstructure:"home"`
167         ListenAddress  string `mapstructure:"laddr"`
168         Seeds          string `mapstructure:"seeds"`
169         SkipUPNP       bool   `mapstructure:"skip_upnp"`
170         AddrBook       string `mapstructure:"addr_book_file"`
171         AddrBookStrict bool   `mapstructure:"addr_book_strict"`
172         PexReactor     bool   `mapstructure:"pex"`
173         MaxNumPeers    int    `mapstructure:"max_num_peers"`
174 }
175
176 func DefaultP2PConfig() *P2PConfig {
177         return &P2PConfig{
178                 ListenAddress:  "tcp://0.0.0.0:46656",
179                 AddrBook:       "addrbook.json",
180                 AddrBookStrict: true,
181                 MaxNumPeers:    50,
182         }
183 }
184
185 func TestP2PConfig() *P2PConfig {
186         conf := DefaultP2PConfig()
187         conf.ListenAddress = "tcp://0.0.0.0:36656"
188         conf.SkipUPNP = true
189         return conf
190 }
191
192 func (p *P2PConfig) AddrBookFile() string {
193         return rootify(p.AddrBook, p.RootDir)
194 }
195
196 //-----------------------------------------------------------------------------
197 // Utils
198
199 // helper function to make config creation independent of root dir
200 func rootify(path, root string) string {
201         if filepath.IsAbs(path) {
202                 return path
203         }
204         return filepath.Join(root, path)
205 }