forked from 0xPolygon/polygon-edge
/
config.go
150 lines (131 loc) · 5.39 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
138
139
140
141
142
143
144
145
146
147
148
149
150
package config
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/Gabulhas/polygon-external-consensus/network"
"github.com/hashicorp/hcl"
"gopkg.in/yaml.v3"
)
// Config defines the server configuration params
type Config struct {
GenesisPath string `json:"chain_config" yaml:"chain_config"`
SecretsConfigPath string `json:"secrets_config" yaml:"secrets_config"`
DataDir string `json:"data_dir" yaml:"data_dir"`
BlockGasTarget string `json:"block_gas_target" yaml:"block_gas_target"`
GRPCAddr string `json:"grpc_addr" yaml:"grpc_addr"`
JSONRPCAddr string `json:"jsonrpc_addr" yaml:"jsonrpc_addr"`
Telemetry *Telemetry `json:"telemetry" yaml:"telemetry"`
Network *Network `json:"network" yaml:"network"`
ShouldSeal bool `json:"seal" yaml:"seal"`
TxPool *TxPool `json:"tx_pool" yaml:"tx_pool"`
LogLevel string `json:"log_level" yaml:"log_level"`
RestoreFile string `json:"restore_file" yaml:"restore_file"`
BlockTime uint64 `json:"block_time_s" yaml:"block_time_s"`
Headers *Headers `json:"headers" yaml:"headers"`
LogFilePath string `json:"log_to" yaml:"log_to"`
JSONRPCBatchRequestLimit uint64 `json:"json_rpc_batch_request_limit" yaml:"json_rpc_batch_request_limit"`
JSONRPCBlockRangeLimit uint64 `json:"json_rpc_block_range_limit" yaml:"json_rpc_block_range_limit"`
}
// Telemetry holds the config details for metric services.
type Telemetry struct {
PrometheusAddr string `json:"prometheus_addr" yaml:"prometheus_addr"`
}
// Network defines the network configuration params
type Network struct {
NoDiscover bool `json:"no_discover" yaml:"no_discover"`
Libp2pAddr string `json:"libp2p_addr" yaml:"libp2p_addr"`
NatAddr string `json:"nat_addr" yaml:"nat_addr"`
DNSAddr string `json:"dns_addr" yaml:"dns_addr"`
MaxPeers int64 `json:"max_peers,omitempty" yaml:"max_peers,omitempty"`
MaxOutboundPeers int64 `json:"max_outbound_peers,omitempty" yaml:"max_outbound_peers,omitempty"`
MaxInboundPeers int64 `json:"max_inbound_peers,omitempty" yaml:"max_inbound_peers,omitempty"`
}
// TxPool defines the TxPool configuration params
type TxPool struct {
PriceLimit uint64 `json:"price_limit" yaml:"price_limit"`
MaxSlots uint64 `json:"max_slots" yaml:"max_slots"`
MaxAccountEnqueued uint64 `json:"max_account_enqueued" yaml:"max_account_enqueued"`
}
// Headers defines the HTTP response headers required to enable CORS.
type Headers struct {
AccessControlAllowOrigins []string `json:"access_control_allow_origins" yaml:"access_control_allow_origins"`
}
const (
// DefaultBlockTime minimum block generation time in seconds
DefaultBlockTime uint64 = 2
// BlockTimeMultiplierForTimeout Multiplier to get IBFT timeout from block time
// timeout is calculated when IBFT timeout is not specified
BlockTimeMultiplierForTimeout uint64 = 5
// DefaultJSONRPCBatchRequestLimit maximum length allowed for json_rpc batch requests
DefaultJSONRPCBatchRequestLimit uint64 = 20
// DefaultJSONRPCBlockRangeLimit maximum block range allowed for json_rpc
// requests with fromBlock/toBlock values (e.g. eth_getLogs)
DefaultJSONRPCBlockRangeLimit uint64 = 1000
)
// DefaultConfig returns the default server configuration
func DefaultConfig() *Config {
defaultNetworkConfig := network.DefaultConfig()
return &Config{
GenesisPath: "./genesis.json",
DataDir: "",
BlockGasTarget: "0x0", // Special value signaling the parent gas limit should be applied
Network: &Network{
NoDiscover: defaultNetworkConfig.NoDiscover,
MaxPeers: defaultNetworkConfig.MaxPeers,
MaxOutboundPeers: defaultNetworkConfig.MaxOutboundPeers,
MaxInboundPeers: defaultNetworkConfig.MaxInboundPeers,
Libp2pAddr: fmt.Sprintf("%s:%d",
defaultNetworkConfig.Addr.IP,
defaultNetworkConfig.Addr.Port,
),
},
Telemetry: &Telemetry{},
ShouldSeal: true,
TxPool: &TxPool{
PriceLimit: 0,
MaxSlots: 4096,
MaxAccountEnqueued: 128,
},
LogLevel: "INFO",
RestoreFile: "",
BlockTime: DefaultBlockTime,
Headers: &Headers{
AccessControlAllowOrigins: []string{"*"},
},
LogFilePath: "",
JSONRPCBatchRequestLimit: DefaultJSONRPCBatchRequestLimit,
JSONRPCBlockRangeLimit: DefaultJSONRPCBlockRangeLimit,
}
}
// ReadConfigFile reads the config file from the specified path, builds a Config object
// and returns it.
//
// Supported file types: .json, .hcl, .yaml, .yml
func ReadConfigFile(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var unmarshalFunc func([]byte, interface{}) error
switch {
case strings.HasSuffix(path, ".hcl"):
unmarshalFunc = hcl.Unmarshal
case strings.HasSuffix(path, ".json"):
unmarshalFunc = json.Unmarshal
case strings.HasSuffix(path, ".yaml"), strings.HasSuffix(path, ".yml"):
unmarshalFunc = yaml.Unmarshal
default:
return nil, fmt.Errorf("suffix of %s is neither hcl, json, yaml nor yml", path)
}
config := DefaultConfig()
config.Network = new(Network)
config.Network.MaxPeers = -1
config.Network.MaxInboundPeers = -1
config.Network.MaxOutboundPeers = -1
if err := unmarshalFunc(data, config); err != nil {
return nil, err
}
return config, nil
}