-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathmirror_node.go
More file actions
316 lines (259 loc) · 8.3 KB
/
Copy pathmirror_node.go
File metadata and controls
316 lines (259 loc) · 8.3 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package hiero
// SPDX-License-Identifier: Apache-2.0
import (
"context"
"crypto/tls"
"fmt"
"io"
"math"
"time"
"google.golang.org/grpc/credentials/insecure"
"github.com/hiero-ledger/hiero-sdk-go/v2/proto/mirror"
"github.com/pkg/errors"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc"
)
type _MirrorNode struct {
*_ManagedNode
consensusServiceClient mirror.ConsensusServiceClient
networkServiceClient mirror.NetworkServiceClient
client *grpc.ClientConn
}
func (node *_MirrorNode) _SetVerifyCertificate(_ bool) {
}
func (node *_MirrorNode) _GetVerifyCertificate() bool {
return false
}
func (node *_MirrorNode) getScheme() (string, error) {
if node.address.address == nil {
return "", errors.New("mirror node address is not set")
}
port := node.address.port
// Standard HTTPS ports
if port == 443 {
return mirrorNodeSchemeHTTPS, nil
}
// Standard HTTP ports
if port == 80 {
return mirrorNodeSchemeHTTP, nil
}
// For other ports, assume HTTPS for security
return mirrorNodeSchemeHTTPS, nil
}
func (node *_MirrorNode) getBaseRestUrl() (string, error) {
scheme, err := node.getScheme()
if err != nil {
return "", err
}
host := node.address.address
port := node.address.port
if host == nil && port == 0 {
return "", errors.New("mirror node address is not set")
}
hostStr := *host
if hostStr == "localhost" || hostStr == "127.0.0.1" {
return fmt.Sprintf("http://%s:38081/api/v1", hostStr), nil
}
return fmt.Sprintf("%s://%s:%d/api/v1", scheme, hostStr, port), nil
}
func _NewMirrorNode(address string) (node *_MirrorNode, err error) {
node = &_MirrorNode{}
node._ManagedNode, err = _NewManagedNode(address, 250*time.Millisecond)
return node, err
}
func (node *_MirrorNode) _GetKey() string {
return node.address._String()
}
func (node *_MirrorNode) _SetMinBackoff(waitTime time.Duration) {
node._ManagedNode._SetMinBackoff(waitTime)
}
func (node *_MirrorNode) _GetMinBackoff() time.Duration {
return node._ManagedNode._GetMinBackoff()
}
func (node *_MirrorNode) _SetMaxBackoff(waitTime time.Duration) {
node._ManagedNode._SetMaxBackoff(waitTime)
}
func (node *_MirrorNode) _GetMaxBackoff() time.Duration {
return node._ManagedNode._GetMaxBackoff()
}
func (node *_MirrorNode) _InUse() {
node._ManagedNode._InUse()
}
func (node *_MirrorNode) _IsHealthy() bool {
return node._ManagedNode._IsHealthy()
}
func (node *_MirrorNode) _IncreaseBackoff() {
node._ManagedNode._IncreaseBackoff()
}
func (node *_MirrorNode) _DecreaseBackoff() {
node._ManagedNode._DecreaseBackoff()
}
func (node *_MirrorNode) _Wait() time.Duration {
return node._ManagedNode._Wait()
}
func (node *_MirrorNode) _GetUseCount() int64 {
return node._ManagedNode._GetUseCount()
}
func (node *_MirrorNode) _GetLastUsed() time.Time {
return node._ManagedNode._GetLastUsed()
}
func (node *_MirrorNode) _GetManagedNode() *_ManagedNode {
return node._ManagedNode
}
func (node *_MirrorNode) _GetAttempts() int64 {
return node._ManagedNode._GetAttempts()
}
func (node *_MirrorNode) _GetAddress() string {
return node._ManagedNode._GetAddress()
}
func (node *_MirrorNode) _GetReadmitTime() *time.Time {
return node._ManagedNode._GetReadmitTime()
}
func (node *_MirrorNode) _GetConsensusServiceClient() (mirror.ConsensusServiceClient, error) {
if node.consensusServiceClient != nil {
return node.consensusServiceClient, nil
} else if node.client != nil {
channel := mirror.NewConsensusServiceClient(node.client)
node.consensusServiceClient = channel
return node.consensusServiceClient, nil
}
var kacp = keepalive.ClientParameters{
Time: 10 * time.Second,
Timeout: time.Second,
PermitWithoutStream: true,
}
var security grpc.DialOption
if node._ManagedNode.address._IsTransportSecurity() {
security = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{MinVersion: tls.VersionTLS12}))
} else {
security = grpc.WithTransportCredentials(insecure.NewCredentials())
}
conn, err := grpc.NewClient(node._ManagedNode.address._String(), security, grpc.WithKeepaliveParams(kacp))
if err != nil {
return nil, errors.Wrapf(err, "error connecting to mirror at %s", node._ManagedNode.address._String())
}
channel := mirror.NewConsensusServiceClient(conn)
node.consensusServiceClient = channel
node.client = conn
return node.consensusServiceClient, nil
}
func (node *_MirrorNode) _GetNetworkServiceClient() (mirror.NetworkServiceClient, error) {
if node.networkServiceClient != nil {
return node.networkServiceClient, nil
} else if node.client != nil {
channel := mirror.NewNetworkServiceClient(node.client)
node.networkServiceClient = channel
return node.networkServiceClient, nil
}
var kacp = keepalive.ClientParameters{
Time: time.Minute,
Timeout: 20 * time.Second,
PermitWithoutStream: true,
}
var security grpc.DialOption
if node._ManagedNode.address._IsTransportSecurity() {
security = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{MinVersion: tls.VersionTLS12}))
} else {
security = grpc.WithTransportCredentials(insecure.NewCredentials())
}
conn, err := grpc.NewClient(node._ManagedNode.address._String(), security, grpc.WithKeepaliveParams(kacp))
if err != nil {
return nil, errors.Wrapf(err, "error connecting to mirror at %s", node._ManagedNode.address._String())
}
channel := mirror.NewNetworkServiceClient(conn)
node.networkServiceClient = channel
node.client = conn
return node.networkServiceClient, nil
}
func (node *_MirrorNode) _ToSecure() _IManagedNode {
managed := _ManagedNode{
address: node.address._ToSecure(),
currentBackoff: node.currentBackoff,
lastUsed: node.lastUsed,
readmitTime: node.readmitTime,
useCount: node.useCount,
minBackoff: node.minBackoff,
badGrpcStatusCount: node.badGrpcStatusCount,
}
return &_MirrorNode{
_ManagedNode: &managed,
consensusServiceClient: node.consensusServiceClient,
client: node.client,
}
}
func (node *_MirrorNode) _ToInsecure() _IManagedNode {
managed := _ManagedNode{
address: node.address._ToInsecure(),
currentBackoff: node.currentBackoff,
lastUsed: node.lastUsed,
readmitTime: node.readmitTime,
useCount: node.useCount,
minBackoff: node.minBackoff,
badGrpcStatusCount: node.badGrpcStatusCount,
}
return &_MirrorNode{
_ManagedNode: &managed,
consensusServiceClient: node.consensusServiceClient,
client: node.client,
}
}
func (node *_MirrorNode) _Close() error {
if node.consensusServiceClient != nil {
return node.client.Close()
}
return nil
}
// RecvStream is a generic interface for any gRPC client stream that has a Recv() method.
// This allows processProtoMessageStream to work with any stream type, not just specific ones.
type recvStream[T any] interface {
Recv() (T, error)
}
// processProtoMessageStream is a generic method that works with any gRPC client stream
// that implements the RecvStream interface. This allows it to work with any stream type
// such as mirror.NetworkService_GetNodesClient, mirror.ConsensusService_SubscribeTopicClient, etc.
func processProtoMessageStream[T any](ctx context.Context, stream recvStream[T], attempt uint64, maxAttempts uint64, retryHandler func(err error) bool) <-chan streamResult[T] {
resultStream := make(chan streamResult[T])
go func() {
var zero T
var err error
defer close(resultStream)
for {
select {
case <-ctx.Done():
resultStream <- streamResult[T]{zero, ctx.Err()}
return
default:
}
if err != nil {
if err == io.EOF {
return
}
if !retryHandler(err) { // should we retry?
resultStream <- streamResult[T]{zero, err}
return
}
if attempt < maxAttempts { // do we have attempts left?
delay := math.Min(250.0*math.Pow(2.0, float64(attempt)), 8000)
time.Sleep(time.Duration(delay) * time.Millisecond)
attempt++
} else { // max attempts reached
resultStream <- streamResult[T]{zero, errors.Wrap(err, "max attempts reached")}
return
}
}
resp, e := stream.Recv()
if e != nil {
err = e
continue
}
resultStream <- streamResult[T]{resp, nil}
}
}()
return resultStream
}
// streamResult is a generic type for stream results
type streamResult[T any] struct {
data T
err error
}