|
| 1 | +package parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/coinbase/rosetta-sdk-go/types" |
| 5 | +) |
| 6 | + |
| 7 | +// OperationGroup is a group of related operations |
| 8 | +// If all operations in a group have the same operation.Type, |
| 9 | +// the Type is also populated. |
| 10 | +type OperationGroup struct { |
| 11 | + Type string |
| 12 | + Operations []*types.Operation |
| 13 | +} |
| 14 | + |
| 15 | +func containsInt(valid []int, value int) bool { |
| 16 | + for _, v := range valid { |
| 17 | + if v == value { |
| 18 | + return true |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + return false |
| 23 | +} |
| 24 | + |
| 25 | +// GroupOperations parses all of a transaction's opertations and returns a slice |
| 26 | +// of each group of related operations. This should ONLY be called on operations |
| 27 | +// that have already been asserted for correctness. Assertion ensures there are |
| 28 | +// no duplicate operation indexes, operations are sorted, and that operations |
| 29 | +// only reference operations with an index less than theirs. |
| 30 | +func GroupOperations(transaction *types.Transaction) []*OperationGroup { |
| 31 | + ops := transaction.Operations |
| 32 | + opGroups := map[int]*OperationGroup{} // using a map makes group merges much easier |
| 33 | + opAssignments := make([]int, len(ops)) |
| 34 | + for i, op := range ops { |
| 35 | + if len(op.RelatedOperations) == 0 { |
| 36 | + opGroups[len(opGroups)] = &OperationGroup{ |
| 37 | + Type: op.Type, |
| 38 | + Operations: []*types.Operation{op}, |
| 39 | + } |
| 40 | + opAssignments[i] = len(opGroups) |
| 41 | + continue |
| 42 | + } |
| 43 | + |
| 44 | + // Find groups to merge |
| 45 | + groupsToMerge := []int{} |
| 46 | + for _, relatedOp := range op.RelatedOperations { |
| 47 | + if !containsInt(groupsToMerge, opAssignments[relatedOp.Index]) { |
| 48 | + groupsToMerge = append(groupsToMerge, opAssignments[relatedOp.Index]) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Merge Groups |
| 53 | + mergedGroupIndex := groupsToMerge[0] |
| 54 | + mergedGroup := opGroups[mergedGroupIndex] |
| 55 | + for _, otherGroupIndex := range groupsToMerge[1:] { |
| 56 | + otherGroup := opGroups[otherGroupIndex] |
| 57 | + |
| 58 | + // Add otherGroup ops to mergedGroup |
| 59 | + for _, otherOp := range otherGroup.Operations { |
| 60 | + if otherOp.Type != mergedGroup.Type && mergedGroup.Type != "" { |
| 61 | + mergedGroup.Type = "" |
| 62 | + } |
| 63 | + |
| 64 | + mergedGroup.Operations = append(mergedGroup.Operations, otherOp) |
| 65 | + opAssignments[otherOp.OperationIdentifier.Index] = mergedGroupIndex |
| 66 | + } |
| 67 | + |
| 68 | + // Delete otherGroup |
| 69 | + delete(opGroups, otherGroupIndex) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return func() []*OperationGroup { |
| 74 | + sliceGroups := []*OperationGroup{} |
| 75 | + for _, v := range opGroups { |
| 76 | + sliceGroups = append(sliceGroups, v) |
| 77 | + } |
| 78 | + |
| 79 | + return sliceGroups |
| 80 | + }() |
| 81 | +} |
0 commit comments