forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress_book_query.go
189 lines (153 loc) · 3.99 KB
/
address_book_query.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
package hedera
/*-
*
* Hedera Go SDK
*
* Copyright (C) 2020 - 2022 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import (
"context"
"io"
"math"
"time"
"github.com/hashgraph/hedera-protobufs-go/services"
"github.com/hashgraph/hedera-protobufs-go/mirror"
"google.golang.org/grpc/status"
)
// AddressBookQuery query an address book for its list of nodes
type AddressBookQuery struct {
attempt uint64
maxAttempts uint64
fileID *FileID
limit int32
}
func NewAddressBookQuery() *AddressBookQuery {
return &AddressBookQuery{
fileID: nil,
limit: 0,
}
}
// SetFileID set the ID of the address book file on the network. Can be either 0.0.101 or 0.0.102.
func (query *AddressBookQuery) SetFileID(id FileID) *AddressBookQuery {
query.fileID = &id
return query
}
func (query *AddressBookQuery) GetFileID() FileID {
if query.fileID == nil {
return FileID{}
}
return *query.fileID
}
// SetLimit
// Set the maximum number of node addresses to receive before stopping.
// If not set or set to zero it will return all node addresses in the database.
func (query *AddressBookQuery) SetLimit(limit int32) *AddressBookQuery {
query.limit = limit
return query
}
func (query *AddressBookQuery) GetLimit() int32 {
return query.limit
}
func (query *AddressBookQuery) SetMaxAttempts(maxAttempts uint64) *AddressBookQuery {
query.maxAttempts = maxAttempts
return query
}
func (query *AddressBookQuery) GetMaxAttempts() uint64 {
return query.maxAttempts
}
func (query *AddressBookQuery) _ValidateNetworkOnIDs(client *Client) error {
if client == nil || !client.autoValidateChecksums {
return nil
}
if query.fileID != nil {
if err := query.fileID.ValidateChecksum(client); err != nil {
return err
}
}
return nil
}
func (query *AddressBookQuery) _Build() *mirror.AddressBookQuery {
body := &mirror.AddressBookQuery{
Limit: query.limit,
}
if query.fileID != nil {
body.FileId = query.fileID._ToProtobuf()
}
return body
}
func (query *AddressBookQuery) Execute(client *Client) (NodeAddressBook, error) {
var cancel func()
var ctx context.Context
err := query._ValidateNetworkOnIDs(client)
if err != nil {
return NodeAddressBook{}, err
}
pb := query._Build()
messages := make([]*services.NodeAddress, 0)
channel, err := client.mirrorNetwork._GetNextMirrorNode()._GetNetworkServiceClient()
if err != nil {
return NodeAddressBook{}, err
}
ch := make(chan byte, 1)
go func() {
var subClient mirror.NetworkService_GetNodesClient
var err error
for {
if err != nil {
cancel()
if grpcErr, ok := status.FromError(err); ok { // nolint
if query.attempt < query.maxAttempts {
subClient = nil
delay := math.Min(250.0*math.Pow(2.0, float64(query.attempt)), 8000)
time.Sleep(time.Duration(delay) * time.Millisecond)
query.attempt++
} else {
panic(grpcErr.Err())
}
} else if err == io.EOF {
break
} else {
panic(err)
}
}
if subClient == nil {
ctx, cancel = context.WithCancel(context.TODO())
subClient, err = (*channel).GetNodes(ctx, pb)
if err != nil {
continue
}
}
var resp *services.NodeAddress
resp, err = subClient.Recv()
if err != nil {
continue
}
if pb.Limit > 0 {
pb.Limit--
}
messages = append(messages, resp)
}
ch <- 1
}()
<-ch
result := make([]NodeAddress, 0)
for _, k := range messages {
result = append(result, _NodeAddressFromProtobuf(k))
}
return NodeAddressBook{
NodeAddresses: result,
}, nil
}