|
| 1 | +package megapool |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/rocket-pool/smartnode/shared/types/api" |
| 10 | +) |
| 11 | + |
| 12 | +// makeValidators builds a slice of MegapoolValidatorDetails with only the |
| 13 | +// fields that sortExitingValidatorsBySweep cares about populated. ValidatorId |
| 14 | +// is set to a distinct value derived from the index so we can also assert that |
| 15 | +// the original elements (not just their indices) are preserved. |
| 16 | +func makeValidators(indices ...uint64) []api.MegapoolValidatorDetails { |
| 17 | + out := make([]api.MegapoolValidatorDetails, 0, len(indices)) |
| 18 | + for i, idx := range indices { |
| 19 | + out = append(out, api.MegapoolValidatorDetails{ |
| 20 | + ValidatorId: uint32(1000 + i), |
| 21 | + ValidatorIndex: idx, |
| 22 | + }) |
| 23 | + } |
| 24 | + return out |
| 25 | +} |
| 26 | + |
| 27 | +func indexesOf(validators []api.MegapoolValidatorDetails) []uint64 { |
| 28 | + out := make([]uint64, len(validators)) |
| 29 | + for i, v := range validators { |
| 30 | + out[i] = v.ValidatorIndex |
| 31 | + } |
| 32 | + return out |
| 33 | +} |
| 34 | + |
| 35 | +func TestSortExitingValidatorsBySweep(t *testing.T) { |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + input []uint64 |
| 39 | + lastWithdrawnIndex uint64 |
| 40 | + hasLastWithdrawnIndex bool |
| 41 | + want []uint64 |
| 42 | + }{ |
| 43 | + { |
| 44 | + name: "empty slice does not panic", |
| 45 | + input: []uint64{}, |
| 46 | + lastWithdrawnIndex: 100, |
| 47 | + hasLastWithdrawnIndex: true, |
| 48 | + want: []uint64{}, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "single validator above pivot", |
| 52 | + input: []uint64{200}, |
| 53 | + lastWithdrawnIndex: 100, |
| 54 | + hasLastWithdrawnIndex: true, |
| 55 | + want: []uint64{200}, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "single validator below pivot", |
| 59 | + input: []uint64{50}, |
| 60 | + lastWithdrawnIndex: 100, |
| 61 | + hasLastWithdrawnIndex: true, |
| 62 | + want: []uint64{50}, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "no pivot falls back to ascending order", |
| 66 | + input: []uint64{300, 50, 200, 100}, |
| 67 | + hasLastWithdrawnIndex: false, |
| 68 | + want: []uint64{50, 100, 200, 300}, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "pivot in the middle splits and sorts each half", |
| 72 | + input: []uint64{300, 50, 200, 100, 400, 25}, |
| 73 | + lastWithdrawnIndex: 150, |
| 74 | + hasLastWithdrawnIndex: true, |
| 75 | + want: []uint64{200, 300, 400, 25, 50, 100}, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "pivot equal to a validator index puts that validator after the wrap", |
| 79 | + input: []uint64{100, 50, 150, 200}, |
| 80 | + lastWithdrawnIndex: 100, |
| 81 | + hasLastWithdrawnIndex: true, |
| 82 | + want: []uint64{150, 200, 50, 100}, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "pivot above all validators wraps everyone (plain ascending)", |
| 86 | + input: []uint64{50, 30, 10, 20}, |
| 87 | + lastWithdrawnIndex: 1000, |
| 88 | + hasLastWithdrawnIndex: true, |
| 89 | + want: []uint64{10, 20, 30, 50}, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "pivot below all validators leaves them all 'after' (plain ascending)", |
| 93 | + input: []uint64{50, 30, 10, 20}, |
| 94 | + lastWithdrawnIndex: 0, |
| 95 | + hasLastWithdrawnIndex: true, |
| 96 | + want: []uint64{10, 20, 30, 50}, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "already in sweep order is unchanged", |
| 100 | + input: []uint64{200, 300, 400, 25, 50, 100}, |
| 101 | + lastWithdrawnIndex: 150, |
| 102 | + hasLastWithdrawnIndex: true, |
| 103 | + want: []uint64{200, 300, 400, 25, 50, 100}, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + for _, tt := range tests { |
| 108 | + t.Run(tt.name, func(t *testing.T) { |
| 109 | + validators := makeValidators(tt.input...) |
| 110 | + |
| 111 | + sortExitingValidatorsBySweep(validators, tt.lastWithdrawnIndex, tt.hasLastWithdrawnIndex) |
| 112 | + |
| 113 | + got := indexesOf(validators) |
| 114 | + // Full struct is unreadable in %v; print index order and compact id@index chain. |
| 115 | + t.Logf("final sweep order (validator indices): %v", got) |
| 116 | + parts := make([]string, len(validators)) |
| 117 | + for i, v := range validators { |
| 118 | + parts[i] = fmt.Sprintf("%d@%d", v.ValidatorId, v.ValidatorIndex) |
| 119 | + } |
| 120 | + idIndexLine := strings.Join(parts, " → ") |
| 121 | + if idIndexLine == "" { |
| 122 | + idIndexLine = "(empty)" |
| 123 | + } |
| 124 | + t.Logf("final sweep order (id@index): %s", idIndexLine) |
| 125 | + if !reflect.DeepEqual(got, tt.want) { |
| 126 | + t.Fatalf("unexpected order: got %v, want %v", got, tt.want) |
| 127 | + } |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// TestSortExitingValidatorsBySweepPreservesElements confirms that the helper |
| 133 | +// reorders the original elements (preserving fields like ValidatorId) rather |
| 134 | +// than producing copies that drop unrelated data. |
| 135 | +func TestSortExitingValidatorsBySweepPreservesElements(t *testing.T) { |
| 136 | + validators := []api.MegapoolValidatorDetails{ |
| 137 | + {ValidatorId: 11, ValidatorIndex: 300}, |
| 138 | + {ValidatorId: 22, ValidatorIndex: 50}, |
| 139 | + {ValidatorId: 33, ValidatorIndex: 200}, |
| 140 | + {ValidatorId: 44, ValidatorIndex: 100}, |
| 141 | + } |
| 142 | + |
| 143 | + sortExitingValidatorsBySweep(validators, 150, true) |
| 144 | + |
| 145 | + wantIndexes := []uint64{200, 300, 50, 100} |
| 146 | + wantIDs := []uint32{33, 11, 22, 44} |
| 147 | + for i, v := range validators { |
| 148 | + if v.ValidatorIndex != wantIndexes[i] || v.ValidatorId != wantIDs[i] { |
| 149 | + t.Fatalf("position %d: got (id=%d, index=%d), want (id=%d, index=%d)", |
| 150 | + i, v.ValidatorId, v.ValidatorIndex, wantIDs[i], wantIndexes[i]) |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments