forked from bitcoinfees/feesim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
137 lines (123 loc) · 3.48 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"io/ioutil"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
col "github.com/bitcoinfees/feesim/collect"
"github.com/bitcoinfees/feesim/collect/corerpc"
est "github.com/bitcoinfees/feesim/estimate"
"github.com/bitcoinfees/feesim/predict"
"github.com/bitcoinfees/feesim/sim"
)
const (
defaultConfigFileName = "config.yml"
configFileEnv = "FEESIM_CONFIG"
dataDirEnv = "FEESIM_DATADIR"
)
var (
defaultFeeSimConfig = FeeSimConfig{
Collect: col.Config{
PollPeriod: 10,
},
Transient: sim.TransientConfig{
MaxBlockConfirms: 12,
MinSuccessPct: 0.9,
NumIters: 10000,
},
Predict: predict.Config{
MaxBlockConfirms: 6,
Halflife: 1008, // 1 week
},
SimPeriod: 60,
TxMaxAge: 10800, // 3 hours
TxGapTol: 3600, // 1 hour
}
defaultConfig = config{
FeeSimConfig: defaultFeeSimConfig,
UniTx: est.UniTxSourceConfig{
MinWindow: 600, // 10 minutes
MaxWindow: 10800, // 3 hours
Halflife: 3600, // 1 hour
},
IndBlock: est.IndBlockSourceConfig{
Window: 2016,
MinCov: 0.5,
GuardInterval: 300,
TailPct: 0.1,
},
BitcoinRPC: corerpc.Config{
Host: "localhost",
Port: "8332",
Timeout: 30,
},
AppRPC: AppRPCConfig{
Host: "localhost",
Port: "8350",
},
DataDir: AppDataDir("feesim", false),
}
defaultConfigFile = filepath.Join(defaultConfig.DataDir, defaultConfigFileName)
defaultLogFileName = "feesim.log"
)
type config struct {
FeeSimConfig `yaml:",inline"`
UniTx est.UniTxSourceConfig `yaml:"unitx" json:"unitx"`
IndBlock est.IndBlockSourceConfig `yaml:"indblock" json:"indblock"`
BitcoinRPC corerpc.Config `yaml:"bitcoinrpc" json:"bitcoinrpc"`
AppRPC AppRPCConfig `yaml:"apprpc" json:"apprpc"`
DataDir string `yaml:"datadir" json:"datadir"`
LogFile string `yaml:"logfile" json:"logfile"`
}
type AppRPCConfig struct {
Host string `json:"host" yaml:"host"`
Port string `json:"port" yaml:"port"`
}
// loadConfig loads the config. The input arguments specify the path to the
// config file / data directory.
// They can also be specified through env variables (configFileEnv / dataDirEnv),
// with lower precedence.
// If not specified, they are set to default values.
func loadConfig(configFile, dataDir string) (config, error) {
cfg := defaultConfig
if configFile == "" {
configFile = os.Getenv(configFileEnv)
}
if dataDir == "" {
dataDir = os.Getenv(dataDirEnv)
}
if configFile != "" {
// Config file was specified explicitly, so return an error if it
// couldn't be read.
if c, err := ioutil.ReadFile(configFile); err != nil {
return cfg, err
} else if err := yaml.Unmarshal(c, &cfg); err != nil {
return cfg, err
}
} else {
// Check the default config file location. No error if it couldn't be
// read, but error if the yaml could not be unmarshaled.
if dataDir == "" {
configFile = defaultConfigFile
} else {
configFile = filepath.Join(dataDir, defaultConfigFileName)
}
if c, err := ioutil.ReadFile(configFile); err == nil {
if err := yaml.Unmarshal(c, &cfg); err != nil {
return cfg, err
}
}
}
// dataDir specified by env or input argument takes precedence
if dataDir != "" {
cfg.DataDir = dataDir
}
if cfg.LogFile == "" {
cfg.LogFile = filepath.Join(cfg.DataDir, defaultLogFileName)
}
// Create the datadir if not exists
if err := os.MkdirAll(cfg.DataDir, 0700); err != nil {
return cfg, err
}
return cfg, nil
}