This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
/
checker.go
237 lines (206 loc) · 5.7 KB
/
checker.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
// TO BE MOVED TO "github.com/ava-labs/avalanchego/vms/platformvm"
import (
"context"
"errors"
"time"
"github.com/ava-labs/avalanchego/api/info"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/platformvm"
pstatus "github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/subnet-cli/internal/poll"
"go.uber.org/zap"
)
var (
ErrInvalidCheckerOpOption = errors.New("invalid checker OpOption")
ErrEmptyID = errors.New("empty ID")
ErrAbortedDropped = errors.New("aborted/dropped")
)
type Checker interface {
PollTx(ctx context.Context, txID ids.ID, s pstatus.Status) (time.Duration, error)
PollSubnet(ctx context.Context, subnetID ids.ID) (time.Duration, error)
PollBlockchain(ctx context.Context, opts ...OpOption) (time.Duration, error)
}
var _ Checker = &checker{}
type checker struct {
poller poll.Poller
cli platformvm.Client
}
func NewChecker(poller poll.Poller, cli platformvm.Client) Checker {
return &checker{
poller: poller,
cli: cli,
}
}
func (c *checker) PollTx(ctx context.Context, txID ids.ID, s pstatus.Status) (time.Duration, error) {
zap.L().Info("polling P-Chain tx",
zap.String("txId", txID.String()),
zap.String("expectedStatus", s.String()),
)
return c.poller.Poll(ctx, func() (done bool, err error) {
status, err := c.cli.GetTxStatus(ctx, txID)
if err != nil {
return false, err
}
zap.L().Debug("tx",
zap.String("status", status.Status.String()),
zap.String("reason", status.Reason),
)
if s == pstatus.Committed &&
(status.Status == pstatus.Aborted || status.Status == pstatus.Dropped) {
return true, ErrAbortedDropped
}
return status.Status == s, nil
})
}
func (c *checker) PollSubnet(ctx context.Context, subnetID ids.ID) (took time.Duration, err error) {
if subnetID == ids.Empty {
return took, ErrEmptyID
}
zap.L().Info("polling subnet",
zap.String("subnetId", subnetID.String()),
)
took, err = c.PollTx(ctx, subnetID, pstatus.Committed)
if err != nil {
return took, err
}
prev := took
took, err = c.findSubnet(ctx, subnetID)
took += prev
return took, err
}
func (c *checker) findSubnet(ctx context.Context, subnetID ids.ID) (took time.Duration, err error) {
zap.L().Info("finding subnets",
zap.String("subnetId", subnetID.String()),
)
took, err = c.poller.Poll(ctx, func() (done bool, err error) {
ss, err := c.cli.GetSubnets(ctx, []ids.ID{subnetID})
if err != nil {
return false, err
}
if len(ss) != 1 {
return false, nil
}
return ss[0].ID == subnetID, nil
})
return took, err
}
func (c *checker) PollBlockchain(ctx context.Context, opts ...OpOption) (took time.Duration, err error) {
ret := &Op{}
ret.applyOpts(opts)
if ret.subnetID == ids.Empty &&
ret.blockchainID == ids.Empty {
return took, ErrEmptyID
}
if ret.blockchainID == ids.Empty {
ret.blockchainID, took, err = c.findBlockchain(ctx, ret.subnetID)
if err != nil {
return took, err
}
}
if ret.blockchainID == ids.Empty {
return took, ErrEmptyID
}
if ret.checkBlockchainBootstrapped && ret.info == nil {
return took, ErrInvalidCheckerOpOption
}
zap.L().Info("polling blockchain",
zap.String("blockchainId", ret.blockchainID.String()),
zap.String("expectedBlockchainStatus", ret.blockchainStatus.String()),
)
prev := took
took, err = c.PollTx(ctx, ret.blockchainID, pstatus.Committed)
took += prev
if err != nil {
return took, err
}
statusPolled := false
prev = took
took, err = c.poller.Poll(ctx, func() (done bool, err error) {
if !statusPolled {
status, err := c.cli.GetBlockchainStatus(ctx, ret.blockchainID.String())
if err != nil {
return false, err
}
if status != ret.blockchainStatus {
zap.L().Info("waiting for blockchain status",
zap.String("current", status.String()),
)
return false, nil
}
statusPolled = true
if !ret.checkBlockchainBootstrapped {
return true, nil
}
}
bootstrapped, err := ret.info.IsBootstrapped(ctx, ret.blockchainID.String())
if err != nil {
return false, err
}
if !bootstrapped {
zap.L().Debug("blockchain not bootstrapped yet; retrying")
return false, nil
}
return true, nil
})
took += prev
return took, err
}
func (c *checker) findBlockchain(ctx context.Context, subnetID ids.ID) (bchID ids.ID, took time.Duration, err error) {
zap.L().Info("finding blockchains",
zap.String("subnetId", subnetID.String()),
)
took, err = c.poller.Poll(ctx, func() (done bool, err error) {
bcs, err := c.cli.GetBlockchains(ctx)
if err != nil {
return false, err
}
bchID = ids.Empty
for _, blockchain := range bcs {
if blockchain.SubnetID == subnetID {
bchID = blockchain.ID
break
}
}
return bchID == ids.Empty, nil
})
return bchID, took, err
}
type Op struct {
subnetID ids.ID
blockchainID ids.ID
blockchainStatus pstatus.BlockchainStatus
info info.Client
checkBlockchainBootstrapped bool
}
type OpOption func(*Op)
func (op *Op) applyOpts(opts []OpOption) {
for _, opt := range opts {
opt(op)
}
}
func WithSubnetID(subnetID ids.ID) OpOption {
return func(op *Op) {
op.subnetID = subnetID
}
}
func WithBlockchainID(bch ids.ID) OpOption {
return func(op *Op) {
op.blockchainID = bch
}
}
func WithBlockchainStatus(s pstatus.BlockchainStatus) OpOption {
return func(op *Op) {
op.blockchainStatus = s
}
}
// TODO: avalanchego "GetBlockchainStatusReply" should have "Bootstrapped".
// e.g., "service.vm.Chains.IsBootstrapped" in "GetBlockchainStatus".
func WithCheckBlockchainBootstrapped(info info.Client) OpOption {
return func(op *Op) {
op.info = info
op.checkBlockchainBootstrapped = true
}
}