forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
186 lines (156 loc) · 5.1 KB
/
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
package hedera
import (
protobuf "github.com/golang/protobuf/proto"
"github.com/hashgraph/hedera-sdk-go/v2/proto"
"github.com/pkg/errors"
)
type Query struct {
pb *proto.Query
pbHeader *proto.QueryHeader
paymentTransactionID TransactionID
nodeIDs []AccountID
maxQueryPayment Hbar
queryPayment Hbar
nextPaymentTransactionIndex int
nextTransactionIndex int
maxRetry int
paymentTransactions []*proto.Transaction
isPaymentRequired bool
}
func newQuery(isPaymentRequired bool, queryHeader *proto.QueryHeader) Query {
return Query{
pb: &proto.Query{},
pbHeader: queryHeader,
paymentTransactionID: TransactionID{},
nextTransactionIndex: 0,
maxRetry: 10,
paymentTransactions: make([]*proto.Transaction, 0),
isPaymentRequired: isPaymentRequired,
maxQueryPayment: NewHbar(0),
queryPayment: NewHbar(0),
}
}
func (query *Query) SetNodeAccountIDs(accountID []AccountID) *Query {
query.nodeIDs = append(query.nodeIDs, accountID...)
return query
}
func (query *Query) GetNodeAccountIDs() []AccountID {
return query.nodeIDs
}
func query_getNodeAccountID(request request) AccountID {
if len(request.query.nodeIDs) > 0 {
return request.query.nodeIDs[request.query.nextPaymentTransactionIndex]
} else {
panic("Query node AccountID's not set before executing")
}
}
func costQuery_getNodeAccountID(request request) AccountID {
return request.query.nodeIDs[request.query.nextPaymentTransactionIndex]
}
// SetMaxQueryPayment sets the maximum payment allowed for this Query.
func (query *Query) SetMaxQueryPayment(maxPayment Hbar) *Query {
query.maxQueryPayment = maxPayment
return query
}
// SetQueryPayment sets the payment amount for this Query.
func (query *Query) SetQueryPayment(paymentAmount Hbar) *Query {
query.queryPayment = paymentAmount
return query
}
func (query *Query) GetMaxRetryCount() int {
return query.maxRetry
}
func (query *Query) SetMaxRetry(count int) *Query {
query.maxRetry = count
return query
}
func (query *Query) getTransactionID(paymentAmount Hbar) TransactionID {
return query.paymentTransactionID
}
func (query *Query) getIsPaymentRequired() bool {
return true
}
func query_shouldRetry(status Status, _ response) bool {
return status == StatusBusy
}
func query_makeRequest(request request) protoRequest {
if request.query.isPaymentRequired && len(request.query.paymentTransactions) > 0 {
request.query.pbHeader.Payment = request.query.paymentTransactions[request.query.nextPaymentTransactionIndex]
}
request.query.pbHeader.ResponseType = proto.ResponseType_ANSWER_ONLY
return protoRequest{
query: request.query.pb,
}
}
func costQuery_makeRequest(request request) protoRequest {
return protoRequest{
query: request.query.pb,
}
}
func query_advanceRequest(request request) {
if request.query.isPaymentRequired && len(request.query.paymentTransactions) > 0 {
request.query.nextPaymentTransactionIndex = (request.query.nextPaymentTransactionIndex + 1) % len(request.query.paymentTransactions)
}
}
func costQuery_advanceRequest(request request) {
request.query.nextPaymentTransactionIndex = (request.query.nextPaymentTransactionIndex + 1) % len(request.query.nodeIDs)
}
func query_mapResponse(request request, response response, _ AccountID, protoRequest protoRequest) (intermediateResponse, error) {
return intermediateResponse{
query: response.query,
}, nil
}
func query_generatePayments(query *Query, client *Client, cost Hbar) error {
for _, nodeID := range query.nodeIDs {
transaction, err := query_makePaymentTransaction(
query.paymentTransactionID,
nodeID,
client.operator,
cost,
)
if err != nil {
return err
}
query.paymentTransactions = append(query.paymentTransactions, transaction)
}
return nil
}
func query_makePaymentTransaction(transactionID TransactionID, nodeAccountID AccountID, operator *operator, cost Hbar) (*proto.Transaction, error) {
accountAmounts := make([]*proto.AccountAmount, 0)
accountAmounts = append(accountAmounts, &proto.AccountAmount{
AccountID: nodeAccountID.toProtobuf(),
Amount: cost.tinybar,
})
accountAmounts = append(accountAmounts, &proto.AccountAmount{
AccountID: operator.accountID.toProtobuf(),
Amount: -cost.tinybar,
})
body := proto.TransactionBody{
TransactionID: transactionID.toProtobuf(),
NodeAccountID: nodeAccountID.toProtobuf(),
TransactionFee: uint64(NewHbar(1).tinybar),
TransactionValidDuration: &proto.Duration{
Seconds: 120,
},
Data: &proto.TransactionBody_CryptoTransfer{
CryptoTransfer: &proto.CryptoTransferTransactionBody{
Transfers: &proto.TransferList{
AccountAmounts: accountAmounts,
},
},
},
}
bodyBytes, err := protobuf.Marshal(&body)
if err != nil {
return nil, errors.Wrap(err, "error serializing query body")
}
signature := operator.signer(bodyBytes)
sigPairs := make([]*proto.SignaturePair, 0)
sigPairs = append(sigPairs, operator.publicKey.toSignaturePairProtobuf(signature))
return &proto.Transaction{
BodyBytes: bodyBytes,
SigMap: &proto.SignatureMap{
SigPair: sigPairs,
},
}, nil
}