-
Notifications
You must be signed in to change notification settings - Fork 2
/
tx_operation_components.go
450 lines (406 loc) · 16.2 KB
/
tx_operation_components.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package services
import (
"fmt"
"math/big"
common2 "github.com/PositionExchange/posichain/internal/common"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/PositionExchange/posichain/rosetta/common"
"github.com/pkg/errors"
)
const (
// maxNumOfConstructionOps ..
maxNumOfConstructionOps = 2
// transferOperationCount ..
transferOperationCount = 2
)
// OperationComponents are components from a set of operations to construct a valid transaction
type OperationComponents struct {
Type string `json:"type"`
From *types.AccountIdentifier `json:"from"`
To *types.AccountIdentifier `json:"to"`
Amount *big.Int `json:"amount"`
StakingMessage interface{} `json:"staking_message,omitempty"`
}
// IsStaking ..
func (s *OperationComponents) IsStaking() bool {
return s.StakingMessage != nil
}
// GetOperationComponents ensures the provided operations creates a valid transaction and returns
// the OperationComponents of the resulting transaction.
//
// Providing a gas expenditure operation is INVALID.
// All staking & cross-shard operations require metadata matching the operation type to be a valid.
// All other operations do not require metadata.
func GetOperationComponents(
operations []*types.Operation,
) (*OperationComponents, *types.Error) {
if operations == nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "nil operations",
})
}
if len(operations) > maxNumOfConstructionOps || len(operations) == 0 {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": fmt.Sprintf("invalid number of operations, must <= %v & > 0", maxNumOfConstructionOps),
})
}
if len(operations) == transferOperationCount {
return getTransferOperationComponents(operations)
}
switch operations[0].Type {
case common.NativeCrossShardTransferOperation:
return getCrossShardOperationComponents(operations[0])
case common.ContractCreationOperation:
return getContractCreationOperationComponents(operations[0])
case common.CreateValidatorOperation:
return getCreateValidatorOperationComponents(operations[0])
case common.EditValidatorOperation:
return getEditValidatorOperationComponents(operations[0])
case common.DelegateOperation:
return getDelegateOperationComponents(operations[0])
case common.UndelegateOperation:
return getUndelegateOperationComponents(operations[0])
case common.CollectRewardsOperation:
return getCollectRewardsOperationComponents(operations[0])
default:
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": fmt.Sprintf("%v is unsupported or invalid operation type", operations[0].Type),
})
}
}
// getTransferOperationComponents ..
func getTransferOperationComponents(
operations []*types.Operation,
) (*OperationComponents, *types.Error) {
if len(operations) != transferOperationCount {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "require exactly 2 operations",
})
}
op0, op1 := operations[0], operations[1]
if op0.Type != common.NativeTransferOperation || op1.Type != common.NativeTransferOperation {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "invalid operation type(s) for same shard transfer",
})
}
val0, err := types.AmountValue(op0.Amount)
if err != nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": err.Error(),
})
}
val1, err := types.AmountValue(op1.Amount)
if err != nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": err.Error(),
})
}
if new(big.Int).Add(val0, val1).Cmp(big.NewInt(0)) != 0 {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "amount taken from sender is not exactly paid out to receiver for same shard transfer",
})
}
if types.Hash(op0.Amount.Currency) != common.NativeCurrencyHash ||
types.Hash(op1.Amount.Currency) != common.NativeCurrencyHash {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "invalid currency for provided amounts",
})
}
if len(op1.RelatedOperations) == 1 &&
op1.RelatedOperations[0].Index != op0.OperationIdentifier.Index {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "second operation is not related to the first operation for same shard transfer",
})
} else if len(op0.RelatedOperations) == 1 &&
op0.RelatedOperations[0].Index != op1.OperationIdentifier.Index {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "first operation is not related to the second operation for same shard transfer",
})
} else if len(op0.RelatedOperations) > 1 || len(op1.RelatedOperations) > 1 ||
len(op0.RelatedOperations)^len(op1.RelatedOperations) != 1 {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "operations must only relate to one another in one direction for same shard transfers",
})
}
components := &OperationComponents{
Type: op0.Type,
Amount: new(big.Int).Abs(val0),
}
if val0.Sign() != 1 {
components.From = op0.Account
components.To = op1.Account
} else {
components.From = op1.Account
components.To = op0.Account
}
if components.From == nil || components.To == nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "both operations must have account identifiers for same shard transfer",
})
}
return components, nil
}
// getCrossShardOperationComponents ..
func getCrossShardOperationComponents(
operation *types.Operation,
) (*OperationComponents, *types.Error) {
if operation == nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "nil operation",
})
}
metadata := common.CrossShardTransactionOperationMetadata{}
if err := metadata.UnmarshalFromInterface(operation.Metadata); err != nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": errors.WithMessage(err, "invalid metadata").Error(),
})
}
amount, err := types.AmountValue(operation.Amount)
if err != nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": err.Error(),
})
}
if amount.Sign() == 1 {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "sender amount must not be positive for cross shard transfer",
})
}
if types.Hash(operation.Amount.Currency) != common.NativeCurrencyHash {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "invalid currency for provided amounts",
})
}
components := &OperationComponents{
Type: operation.Type,
To: metadata.To,
From: metadata.From,
Amount: new(big.Int).Abs(amount),
}
if components.From == nil || components.To == nil || operation.Account == nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "operation must have account sender/from & receiver/to identifiers for cross shard transfer",
})
}
if operation.Account.Address != components.From.Address {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "operation account identifier does not match sender/from identifiers for cross shard transfer",
})
}
return components, nil
}
// getContractCreationOperationComponents ..
func getContractCreationOperationComponents(
operation *types.Operation,
) (*OperationComponents, *types.Error) {
if operation == nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "nil operation",
})
}
amount, err := types.AmountValue(operation.Amount)
if err != nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": err.Error(),
})
}
if amount.Sign() == 1 {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "sender amount must not be positive for contract creation",
})
}
if types.Hash(operation.Amount.Currency) != common.NativeCurrencyHash {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "invalid currency for provided amounts",
})
}
components := &OperationComponents{
Type: operation.Type,
From: operation.Account,
Amount: new(big.Int).Abs(amount),
}
if components.From == nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "operation must have account sender/from identifier for contract creation",
})
}
return components, nil
}
func getCreateValidatorOperationComponents(
operation *types.Operation,
) (*OperationComponents, *types.Error) {
if operation == nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "nil operation",
})
}
metadata := common.CreateValidatorOperationMetadata{}
if err := metadata.UnmarshalFromInterface(operation.Metadata); err != nil {
return nil, common.NewError(common.InvalidStakingConstructionError, map[string]interface{}{
"message": errors.WithMessage(err, "invalid metadata").Error(),
})
}
if metadata.ValidatorAddress == "" || !common2.IsBech32Address(metadata.ValidatorAddress) {
return nil, common.NewError(common.InvalidStakingConstructionError, map[string]interface{}{
"message": "validator address must not be empty or wrong format",
})
}
if metadata.CommissionRate == nil || metadata.MaxCommissionRate == nil || metadata.MaxChangeRate == nil {
return nil, common.NewError(common.InvalidStakingConstructionError, map[string]interface{}{
"message": "commission rate & max commission rate & max change rate must not be nil",
})
}
if metadata.MinSelfDelegation == nil || metadata.MaxTotalDelegation == nil {
return nil, common.NewError(common.InvalidStakingConstructionError, map[string]interface{}{
"message": "min self delegation & max total delegation much not be nil",
})
}
if metadata.Amount == nil {
return nil, common.NewError(common.InvalidStakingConstructionError, map[string]interface{}{
"message": "amount must not be nil",
})
}
if metadata.Name == "" || metadata.Website == "" || metadata.Identity == "" ||
metadata.SecurityContact == "" || metadata.Details == "" {
return nil, common.NewError(common.InvalidStakingConstructionError, map[string]interface{}{
"message": "name & website & identity & security contract & details must no be empty",
})
}
// slot public key would be add into
// https://github.com/PositionExchange/posichain/blob/3a8125666817149eaf9cea7870735e26cfe49c87/rosetta/services/tx_construction.go#L16
// see https://github.com/PositionExchange/posichain/issues/3431
components := &OperationComponents{
Type: operation.Type,
From: operation.Account,
StakingMessage: metadata,
}
if components.From == nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "operation must have account sender/from identifier for creating validator",
})
}
return components, nil
}
func getEditValidatorOperationComponents(
operation *types.Operation,
) (*OperationComponents, *types.Error) {
if operation == nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "nil operation",
})
}
metadata := common.EditValidatorOperationMetadata{}
if err := metadata.UnmarshalFromInterface(operation.Metadata); err != nil {
return nil, common.NewError(common.InvalidStakingConstructionError, map[string]interface{}{
"message": errors.WithMessage(err, "invalid metadata").Error(),
})
}
if metadata.ValidatorAddress == "" || !common2.IsBech32Address(metadata.ValidatorAddress) {
return nil, common.NewError(common.InvalidStakingConstructionError, map[string]interface{}{
"message": "validator address must not be empty or wrong format",
})
}
if metadata.CommissionRate == nil || metadata.MinSelfDelegation == nil || metadata.MaxTotalDelegation == nil {
return nil, common.NewError(common.InvalidStakingConstructionError, map[string]interface{}{
"message": "commission rate & max commission rate & max change rate must not be nil",
})
}
if metadata.Name == "" || metadata.Website == "" || metadata.Identity == "" ||
metadata.SecurityContact == "" || metadata.Details == "" {
return nil, common.NewError(common.InvalidStakingConstructionError, map[string]interface{}{
"message": "name & website & identity & security contract & details must no be empty",
})
}
components := &OperationComponents{
Type: operation.Type,
From: operation.Account,
StakingMessage: metadata,
}
if components.From == nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "operation must have account sender/from identifier for editing validator",
})
}
return components, nil
}
func getDelegateOperationComponents(
operation *types.Operation,
) (*OperationComponents, *types.Error) {
if operation == nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "nil operation",
})
}
metadata := common.DelegateOperationMetadata{}
if err := metadata.UnmarshalFromInterface(operation.Metadata); err != nil {
return nil, common.NewError(common.InvalidStakingConstructionError, map[string]interface{}{
"message": errors.WithMessage(err, "invalid metadata").Error(),
})
}
// validator and delegator and amount already got checked inside UnmarshalFromInterface
components := &OperationComponents{
Type: operation.Type,
From: operation.Account,
StakingMessage: metadata,
}
if components.From == nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "operation must have account sender/from identifier for delegating",
})
}
return components, nil
}
func getUndelegateOperationComponents(
operation *types.Operation,
) (*OperationComponents, *types.Error) {
if operation == nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "nil operation",
})
}
metadata := common.UndelegateOperationMetadata{}
if err := metadata.UnmarshalFromInterface(operation.Metadata); err != nil {
return nil, common.NewError(common.InvalidStakingConstructionError, map[string]interface{}{
"message": errors.WithMessage(err, "invalid metadata").Error(),
})
}
// validator and delegator and amount already got checked inside UnmarshalFromInterface
components := &OperationComponents{
Type: operation.Type,
From: operation.Account,
StakingMessage: metadata,
}
if components.From == nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "operation must have account sender/from identifier for undelegating",
})
}
return components, nil
}
func getCollectRewardsOperationComponents(
operation *types.Operation,
) (*OperationComponents, *types.Error) {
if operation == nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": "nil operation",
})
}
metadata := common.CollectRewardsMetadata{}
if err := metadata.UnmarshalFromInterface(operation.Metadata); err != nil {
return nil, common.NewError(common.InvalidStakingConstructionError, map[string]interface{}{
"message": errors.WithMessage(err, "invalid metadata").Error(),
})
}
//delegator already got checked inside UnmarshalFromInterface
components := &OperationComponents{
Type: operation.Type,
From: operation.Account,
StakingMessage: metadata,
}
if components.From == nil {
return nil, common.NewError(common.InvalidTransactionConstructionError, map[string]interface{}{
"message": "operation must have account sender/from identifier for collecting rewards",
})
}
return components, nil
}