-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
validate.go
109 lines (93 loc) · 2.79 KB
/
validate.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
package blockheaderfeeder
import (
"time"
"github.com/google/uuid"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
)
// ValidatedSpec validates and converts the given toml string to a job.Job.
func ValidatedSpec(tomlString string) (job.Job, error) {
jb := job.Job{
// Default to generating a UUID, can be overwritten by the specified one in tomlString.
ExternalJobID: uuid.New(),
}
tree, err := toml.Load(tomlString)
if err != nil {
return jb, errors.Wrap(err, "loading toml")
}
err = tree.Unmarshal(&jb)
if err != nil {
return jb, errors.Wrap(err, "unmarshalling toml spec")
}
if jb.Type != job.BlockHeaderFeeder {
return jb, errors.Errorf("unsupported type %s", jb.Type)
}
var spec job.BlockHeaderFeederSpec
err = tree.Unmarshal(&spec)
if err != nil {
return jb, errors.Wrap(err, "unmarshalling toml job")
}
// Required fields
if spec.CoordinatorV1Address == nil && spec.CoordinatorV2Address == nil && spec.CoordinatorV2PlusAddress == nil {
return jb, errors.New(
`at least one of "coordinatorV1Address", "coordinatorV2Address" and "coordinatorV2PlusAddress" must be set`)
}
if spec.BlockhashStoreAddress == "" {
return jb, notSet("blockhashStoreAddress")
}
if spec.BatchBlockhashStoreAddress == "" {
return jb, notSet("batchBlockhashStoreAddress")
}
if spec.EVMChainID == nil {
return jb, notSet("evmChainID")
}
err = validateChainID(spec.EVMChainID.Int64())
if err != nil {
return jb, err
}
// Defaults
if spec.WaitBlocks == 0 {
spec.WaitBlocks = 256
}
if spec.LookbackBlocks == 0 {
spec.LookbackBlocks = 1000
}
if spec.PollPeriod == 0 {
spec.PollPeriod = 15 * time.Second
}
if spec.RunTimeout == 0 {
spec.RunTimeout = 30 * time.Second
}
if spec.StoreBlockhashesBatchSize == 0 {
spec.StoreBlockhashesBatchSize = 10
}
if spec.GetBlockhashesBatchSize == 0 {
spec.GetBlockhashesBatchSize = 100
}
if spec.WaitBlocks < 256 {
return jb, errors.New(`"waitBlocks" must be greater than or equal to 256`)
}
if spec.LookbackBlocks <= 256 {
return jb, errors.New(`"lookbackBlocks" must be greater than 256`)
}
if spec.WaitBlocks >= spec.LookbackBlocks {
return jb, errors.New(`"lookbackBlocks" must be greater than "waitBlocks"`)
}
jb.BlockHeaderFeederSpec = &spec
return jb, nil
}
func notSet(field string) error {
return errors.Errorf("%q must be set", field)
}
// validateChainID validates whether the given chain is supported
// Avax chain is not supported because block header format
// is different from go-ethereum types.Header.
// Special handling for Avax chains is not yet supported
func validateChainID(evmChainID int64) error {
if evmChainID == 43114 || // C-chain mainnet
evmChainID == 43113 { // Fuji testnet
return errors.Errorf("unsupported chain")
}
return nil
}