2021-06-01 14:33:45 +00:00
|
|
|
package morphconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-07-18 13:41:35 +00:00
|
|
|
"strconv"
|
2021-06-01 14:33:45 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
|
2022-07-18 13:41:35 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2021-06-01 14:33:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-08-24 14:33:18 +00:00
|
|
|
subsection = "morph"
|
|
|
|
notarySubsection = "notary"
|
2021-06-01 14:33:45 +00:00
|
|
|
|
|
|
|
// DialTimeoutDefault is a default dial timeout of morph chain client connection.
|
|
|
|
DialTimeoutDefault = 5 * time.Second
|
2021-08-24 14:33:18 +00:00
|
|
|
|
2022-08-02 11:31:21 +00:00
|
|
|
// PriorityDefault is a default endpoint priority for the morph client.
|
|
|
|
PriorityDefault = 1
|
2022-08-16 11:59:30 +00:00
|
|
|
|
2022-10-06 15:30:55 +00:00
|
|
|
// CacheTTLDefault is a default value for cached values TTL.
|
|
|
|
// It is 0, because actual default depends on block time.
|
|
|
|
CacheTTLDefault = time.Duration(0)
|
2022-10-12 16:47:33 +00:00
|
|
|
|
|
|
|
// SwitchIntervalDefault is a default Neo RPCs switch interval.
|
|
|
|
SwitchIntervalDefault = 2 * time.Minute
|
2021-06-01 14:33:45 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// RPCEndpoint returns list of the values of "rpc_endpoint" config parameter
|
2021-06-01 14:33:45 +00:00
|
|
|
// from "morph" section.
|
|
|
|
//
|
|
|
|
// Throws panic if list is empty.
|
2022-07-18 13:41:35 +00:00
|
|
|
func RPCEndpoint(c *config.Config) []client.Endpoint {
|
|
|
|
var es []client.Endpoint
|
|
|
|
|
|
|
|
sub := c.Sub(subsection).Sub("rpc_endpoint")
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
s := sub.Sub(strconv.FormatInt(int64(i), 10))
|
|
|
|
addr := config.StringSafe(s, "address")
|
|
|
|
if addr == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-08-02 11:31:21 +00:00
|
|
|
priority := int(config.IntSafe(s, "priority"))
|
|
|
|
if priority <= 0 {
|
|
|
|
priority = PriorityDefault
|
|
|
|
}
|
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
es = append(es, client.Endpoint{
|
|
|
|
Address: addr,
|
2022-08-02 11:31:21 +00:00
|
|
|
Priority: priority,
|
2022-07-18 13:41:35 +00:00
|
|
|
})
|
2021-06-01 14:33:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
if len(es) == 0 {
|
|
|
|
panic(fmt.Errorf("no morph chain RPC endpoints, see `morph.rpc_endpoint` section"))
|
|
|
|
}
|
|
|
|
return es
|
2021-06-01 14:33:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// DialTimeout returns the value of "dial_timeout" config parameter
|
2021-06-01 14:33:45 +00:00
|
|
|
// from "morph" section.
|
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns DialTimeoutDefault if the value is not positive duration.
|
2021-06-01 14:33:45 +00:00
|
|
|
func DialTimeout(c *config.Config) time.Duration {
|
|
|
|
v := config.DurationSafe(c.Sub(subsection), "dial_timeout")
|
2021-06-02 12:44:41 +00:00
|
|
|
if v > 0 {
|
2021-06-01 14:33:45 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
return DialTimeoutDefault
|
|
|
|
}
|
2021-07-29 14:57:17 +00:00
|
|
|
|
2022-08-16 11:59:30 +00:00
|
|
|
// CacheTTL returns the value of "cache_ttl" config parameter
|
2021-07-29 14:57:17 +00:00
|
|
|
// from "morph" section.
|
2022-08-16 11:59:30 +00:00
|
|
|
//
|
|
|
|
// Returns CacheTTLDefault if value is zero or invalid. Supports negative durations.
|
|
|
|
func CacheTTL(c *config.Config) time.Duration {
|
|
|
|
res := config.DurationSafe(c.Sub(subsection), "cache_ttl")
|
|
|
|
if res != 0 {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
return CacheTTLDefault
|
2021-07-29 14:57:17 +00:00
|
|
|
}
|
2022-10-12 16:47:33 +00:00
|
|
|
|
|
|
|
// SwitchInterval returns the value of "switch_interval" config parameter
|
|
|
|
// from "morph" section.
|
|
|
|
//
|
|
|
|
// Returns SwitchIntervalDefault if value is not positive duration.
|
|
|
|
func SwitchInterval(c *config.Config) time.Duration {
|
|
|
|
res := config.DurationSafe(c.Sub(subsection), "switch_interval")
|
|
|
|
if res != 0 {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
return SwitchIntervalDefault
|
|
|
|
}
|