-
Notifications
You must be signed in to change notification settings - Fork 29
/
config.go
87 lines (76 loc) · 2.5 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
package tendermint
import (
"errors"
"fmt"
"strings"
"time"
"github.com/hyperledger-labs/yui-relayer/core"
)
var _ core.ChainConfig = (*ChainConfig)(nil)
func (c ChainConfig) Build() (core.Chain, error) {
return &Chain{
config: c,
}, nil
}
func (c ChainConfig) Validate() error {
isEmpty := func(s string) bool {
return strings.TrimSpace(s) == ""
}
var errs []error
if isEmpty(c.Key) {
errs = append(errs, fmt.Errorf("config attribute \"key\" is empty"))
}
if isEmpty(c.ChainId) {
errs = append(errs, fmt.Errorf("config attribute \"chain_id\" is empty"))
}
if isEmpty(c.RpcAddr) {
errs = append(errs, fmt.Errorf("config attribute \"rpc_addr\" is empty"))
}
if isEmpty(c.AccountPrefix) {
errs = append(errs, fmt.Errorf("config attribute \"account_prefix\" is empty"))
}
if c.GasAdjustment <= 0 {
errs = append(errs, fmt.Errorf("config attribute \"gas_adjustment\" is too small: %v", c.GasAdjustment))
}
if isEmpty(c.GasPrices) {
errs = append(errs, fmt.Errorf("config attribute \"gas_prices\" is empty"))
}
if c.AverageBlockTimeMsec == 0 {
errs = append(errs, fmt.Errorf("config attribute \"average_block_time_msec\" is zero"))
}
if c.MaxRetryForCommit == 0 {
errs = append(errs, fmt.Errorf("config attribute \"max_retry_for_commit\" is zero"))
}
// errors.Join returns nil if len(errs) == 0
return errors.Join(errs...)
}
var _ core.ProverConfig = (*ProverConfig)(nil)
func (c ProverConfig) Build(chain core.Chain) (core.Prover, error) {
chain_, ok := chain.(*Chain)
if !ok {
return nil, fmt.Errorf("chain type must be %T, not %T", &Chain{}, chain)
}
return NewProver(chain_, c), nil
}
func (c ProverConfig) Validate() error {
if _, err := time.ParseDuration(c.TrustingPeriod); err != nil {
return fmt.Errorf("config attribute \"trusting_period\" is invalid: %v", err)
}
if c.RefreshThresholdRate.Denominator == 0 {
return fmt.Errorf("config attribute \"refresh_threshold_rate.denominator\" must not be zero")
}
if c.RefreshThresholdRate.Numerator == 0 {
return fmt.Errorf("config attribute \"refresh_threshold_rate.numerator\" must not be zero")
}
if c.RefreshThresholdRate.Numerator > c.RefreshThresholdRate.Denominator {
return fmt.Errorf("config attribute \"refresh_threshold_rate\" must be less than or equal to 1.0: actual=%v/%v", c.RefreshThresholdRate.Numerator, c.RefreshThresholdRate.Denominator)
}
return nil
}
func (c ProverConfig) GetTrustingPeriod() time.Duration {
if d, err := time.ParseDuration(c.TrustingPeriod); err != nil {
panic(err)
} else {
return d
}
}