OSDN Git Service

feat: add node discovery and status check (#374)
[bytom/vapor.git] / toolbar / precognitive / config / config.go
1 package config
2
3 import (
4         "encoding/json"
5         "os"
6
7         log "github.com/sirupsen/logrus"
8         "github.com/vapor/crypto/ed25519/chainkd"
9
10         "github.com/vapor/toolbar/common"
11 )
12
13 func NewConfig() *Config {
14         if len(os.Args) <= 1 {
15                 log.Fatal("Please setup the config file path")
16         }
17
18         return NewConfigWithPath(os.Args[1])
19 }
20
21 func NewConfigWithPath(path string) *Config {
22         configFile, err := os.Open(path)
23         if err != nil {
24                 log.WithFields(log.Fields{"err": err, "file_path": os.Args[1]}).Fatal("fail to open config file")
25         }
26         defer configFile.Close()
27
28         cfg := &Config{}
29         if err := json.NewDecoder(configFile).Decode(cfg); err != nil {
30                 log.WithField("err", err).Fatal("fail to decode config file")
31         }
32
33         return cfg
34 }
35
36 type Config struct {
37         NetworkID        uint64             `json:"network_id"`
38         MySQLConfig      common.MySQLConfig `json:"mysql"`
39         CheckFreqMinutes uint64             `json:"check_frequency_minutes"`
40         Policy           Policy             `json:"policy"`
41         Nodes            []Node             `json:"seeds"`
42         API              API                `json:"api"`
43 }
44
45 type Policy struct {
46         Confirmations uint64 `json:"confirmations"`
47         RequiredRttMS uint64 `json:"required_rtt_ms"`
48 }
49
50 type Node struct {
51         XPub      *chainkd.XPub `json:"xpub"`
52         PublicKey string        `json:"public_key"`
53         IP        string        `json:"ip"`
54         Port      uint16        `json:"port"`
55 }
56
57 type API struct {
58         ListeningPort uint64 `json:"listening_port"`
59         AccessToken   string `json:"access_token"`
60         IsReleaseMode bool   `json:"is_release_mode"`
61 }