-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
config_builder.go
116 lines (104 loc) · 3.37 KB
/
config_builder.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
package client
import (
"fmt"
"net/url"
"time"
"go.uber.org/multierr"
commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config"
"github.com/smartcontractkit/chainlink/v2/common/config"
commonclient "github.com/smartcontractkit/chainlink/v2/common/client"
evmconfig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml"
)
type NodeConfig struct {
Name *string
WSURL *string
HTTPURL *string
SendOnly *bool
Order *int32
}
// Build the configs needed to initialize the chain client
// Parameters should only be basic go types to make it accessible for external users
// Configs can be stored in a variety of ways
func NewClientConfigs(
selectionMode *string,
leaseDuration time.Duration,
chainType string,
nodeCfgs []NodeConfig,
pollFailureThreshold *uint32,
pollInterval time.Duration,
syncThreshold *uint32,
nodeIsSyncingEnabled *bool,
noNewHeadsThreshold time.Duration,
finalityDepth *uint32,
finalityTagEnabled *bool,
) (commonclient.ChainConfig, evmconfig.NodePool, []*toml.Node, error) {
nodes, err := parseNodeConfigs(nodeCfgs)
if err != nil {
return nil, nil, nil, err
}
nodePool := toml.NodePool{
SelectionMode: selectionMode,
LeaseDuration: commonconfig.MustNewDuration(leaseDuration),
PollFailureThreshold: pollFailureThreshold,
PollInterval: commonconfig.MustNewDuration(pollInterval),
SyncThreshold: syncThreshold,
NodeIsSyncingEnabled: nodeIsSyncingEnabled,
}
nodePoolCfg := &evmconfig.NodePoolConfig{C: nodePool}
chainConfig := &evmconfig.EVMConfig{
C: &toml.EVMConfig{
Chain: toml.Chain{
ChainType: config.NewChainTypeConfig(chainType),
FinalityDepth: finalityDepth,
FinalityTagEnabled: finalityTagEnabled,
NoNewHeadsThreshold: commonconfig.MustNewDuration(noNewHeadsThreshold),
},
},
}
return chainConfig, nodePoolCfg, nodes, nil
}
func parseNodeConfigs(nodeCfgs []NodeConfig) ([]*toml.Node, error) {
nodes := make([]*toml.Node, len(nodeCfgs))
for i, nodeCfg := range nodeCfgs {
if nodeCfg.WSURL == nil || nodeCfg.HTTPURL == nil {
return nil, fmt.Errorf("node config [%d]: missing WS or HTTP URL", i)
}
wsUrl := commonconfig.MustParseURL(*nodeCfg.WSURL)
httpUrl := commonconfig.MustParseURL(*nodeCfg.HTTPURL)
node := &toml.Node{
Name: nodeCfg.Name,
WSURL: wsUrl,
HTTPURL: httpUrl,
SendOnly: nodeCfg.SendOnly,
Order: nodeCfg.Order,
}
nodes[i] = node
}
if err := validateNodeConfigs(nodes); err != nil {
return nil, err
}
return nodes, nil
}
func validateNodeConfigs(nodes []*toml.Node) (err error) {
names := commonconfig.UniqueStrings{}
wsURLs := commonconfig.UniqueStrings{}
httpURLs := commonconfig.UniqueStrings{}
for i, node := range nodes {
if nodeErr := node.ValidateConfig(); nodeErr != nil {
err = multierr.Append(err, nodeErr)
}
if names.IsDupe(node.Name) {
err = multierr.Append(err, commonconfig.NewErrDuplicate(fmt.Sprintf("Nodes.%d.Name", i), *node.Name))
}
u := (*url.URL)(node.WSURL)
if wsURLs.IsDupeFmt(u) {
err = multierr.Append(err, commonconfig.NewErrDuplicate(fmt.Sprintf("Nodes.%d.WSURL", i), u.String()))
}
u = (*url.URL)(node.HTTPURL)
if httpURLs.IsDupeFmt(u) {
err = multierr.Append(err, commonconfig.NewErrDuplicate(fmt.Sprintf("Nodes.%d.HTTPURL", i), u.String()))
}
}
return err
}