-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathvalidate.go
84 lines (70 loc) · 1.97 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
package blockhashstore
import (
"time"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"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.NewV4(),
}
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.BlockhashStore {
return jb, errors.Errorf("unsupported type %s", jb.Type)
}
var spec job.BlockhashStoreSpec
err = tree.Unmarshal(&spec)
if err != nil {
return jb, errors.Wrap(err, "unmarshalling toml job")
}
// Required fields
if spec.CoordinatorV1Address == nil && spec.CoordinatorV2Address == nil {
return jb, errors.New(
`at least one of "coordinatorV1Address" and "coordinatorV2Address" must be set`)
}
if spec.BlockhashStoreAddress == "" {
return jb, notSet("blockhashStoreAddress")
}
if spec.EVMChainID == nil {
return jb, notSet("evmChainID")
}
// Defaults
if spec.WaitBlocks == 0 {
spec.WaitBlocks = 100
}
if spec.LookbackBlocks == 0 {
spec.LookbackBlocks = 200
}
if spec.PollPeriod == 0 {
spec.PollPeriod = 30 * time.Second
}
if spec.RunTimeout == 0 {
spec.RunTimeout = 30 * time.Second
}
// Validation
if spec.WaitBlocks >= spec.LookbackBlocks {
return jb, errors.New(`"waitBlocks" must be less than "lookbackBlocks"`)
}
if spec.WaitBlocks >= 256 {
return jb, errors.New(`"waitBlocks" must be less than 256`)
}
if spec.LookbackBlocks >= 256 {
return jb, errors.New(`"lookbackBlocks" must be less than 256`)
}
jb.BlockhashStoreSpec = &spec
return jb, nil
}
func notSet(field string) error {
return errors.Errorf("%q must be set", field)
}