-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
chain_set.go
166 lines (141 loc) · 4.39 KB
/
chain_set.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package chains
import (
"context"
"fmt"
"math/big"
"github.com/pkg/errors"
"go.uber.org/multierr"
"golang.org/x/exp/maps"
"github.com/smartcontractkit/chainlink-relay/pkg/logger"
"github.com/smartcontractkit/chainlink-relay/pkg/types"
"github.com/smartcontractkit/chainlink/v2/core/services"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
var (
// ErrChainIDEmpty is returned when chain is required but was empty.
ErrChainIDEmpty = errors.New("chain id empty")
ErrNotFound = errors.New("not found")
)
// ChainStatuser is a generic interface for chain configuration.
type ChainStatuser interface {
// must return [ErrNotFound] if the id is not found
ChainStatus(ctx context.Context, id string) (types.ChainStatus, error)
ChainStatuses(ctx context.Context, offset, limit int) ([]types.ChainStatus, int, error)
}
// NodesStatuser is an interface for node configuration and state.
// TODO BCF2440, BCF-2511 may need Node(ctx,name) to get a node status by name
type NodesStatuser interface {
NodeStatuses(ctx context.Context, offset, limit int, chainIDs ...string) (nodes []types.NodeStatus, count int, err error)
}
// ChainService is a live, runtime chain instance, with supporting services.
type ChainService interface {
services.ServiceCtx
SendTx(ctx context.Context, from, to string, amount *big.Int, balanceCheck bool) error
}
// ChainSetOpts holds options for configuring a ChainSet via NewChainSet.
type ChainSetOpts[I ID, N Node] interface {
Validate() error
ConfigsAndLogger() (Configs[I, N], logger.Logger)
}
type chainSet[N Node, S ChainService] struct {
utils.StartStopOnce
opts ChainSetOpts[string, N]
configs Configs[string, N]
lggr logger.Logger
chains map[string]S
}
// NewChainSet returns a new immutable ChainSet for the given ChainSetOpts.
func NewChainSet[N Node, S ChainService](
chains map[string]S,
opts ChainSetOpts[string, N],
) (types.ChainSet[string, S], error) {
if err := opts.Validate(); err != nil {
return nil, err
}
cfgs, lggr := opts.ConfigsAndLogger()
cs := chainSet[N, S]{
opts: opts,
configs: cfgs,
lggr: logger.Named(lggr, "ChainSet"),
chains: chains,
}
return &cs, nil
}
func (c *chainSet[N, S]) Chain(ctx context.Context, id string) (s S, err error) {
if err = c.StartStopOnce.Ready(); err != nil {
return
}
ch, ok := c.chains[id]
if !ok {
err = fmt.Errorf("chain %s: %w", id, ErrNotFound)
return
}
return ch, nil
}
func (c *chainSet[N, S]) ChainStatus(ctx context.Context, id string) (cfg types.ChainStatus, err error) {
var cs []types.ChainStatus
cs, _, err = c.configs.Chains(0, -1, id)
if err != nil {
return
}
l := len(cs)
if l == 0 {
err = fmt.Errorf("chain %s: %w", id, ErrNotFound)
return
}
if l > 1 {
err = fmt.Errorf("multiple chains found: %d", len(cs))
return
}
cfg = cs[0]
return
}
func (c *chainSet[N, S]) ChainStatuses(ctx context.Context, offset, limit int) ([]types.ChainStatus, int, error) {
return c.configs.Chains(offset, limit)
}
func (c *chainSet[N, S]) NodeStatuses(ctx context.Context, offset, limit int, chainIDs ...string) (nodes []types.NodeStatus, count int, err error) {
return c.configs.NodeStatusesPaged(offset, limit, chainIDs...)
}
func (c *chainSet[N, S]) SendTx(ctx context.Context, chainID, from, to string, amount *big.Int, balanceCheck bool) error {
chain, err := c.Chain(ctx, chainID)
if err != nil {
return err
}
return chain.SendTx(ctx, from, to, amount, balanceCheck)
}
func (c *chainSet[N, S]) Start(ctx context.Context) error {
return c.StartOnce("ChainSet", func() error {
c.lggr.Debug("Starting")
var ms services.MultiStart
for id, ch := range c.chains {
if err := ms.Start(ctx, ch); err != nil {
return errors.Wrapf(err, "failed to start chain %q", id)
}
}
c.lggr.Info(fmt.Sprintf("Started %d chains", len(c.chains)))
return nil
})
}
func (c *chainSet[N, S]) Close() error {
return c.StopOnce("ChainSet", func() error {
c.lggr.Debug("Stopping")
return services.MultiCloser(maps.Values(c.chains)).Close()
})
}
func (c *chainSet[N, S]) Ready() (err error) {
err = c.StartStopOnce.Ready()
for _, c := range c.chains {
err = multierr.Combine(err, c.Ready())
}
return
}
func (c *chainSet[N, S]) Name() string {
return c.lggr.Name()
}
func (c *chainSet[N, S]) HealthReport() map[string]error {
report := map[string]error{c.Name(): c.StartStopOnce.Healthy()}
for _, c := range c.chains {
maps.Copy(report, c.HealthReport())
}
return report
}