Skip to content

Commit 506fc95

Browse files
committed
refactor endpoint
1 parent 351fe67 commit 506fc95

File tree

1 file changed

+29
-39
lines changed

1 file changed

+29
-39
lines changed

api/api.go

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -326,63 +326,53 @@ func (m *ApiService) handleMemoryValidatorBlocks(w http.ResponseWriter, r *http.
326326
// Create a map to hold the ordered blocks, with the ValidatorIndex as the key
327327
orderedBlocks := make(map[uint64]*httpOkValBlocks)
328328

329-
// Add the ordered blocks to the map
329+
// Iterate over the ProposedBlocks and add them to the orderedBlocks map
330330
for _, block := range proposedBlocks {
331331
validatorIndex := block.ValidatorIndex
332-
if _, ok := orderedBlocks[validatorIndex]; !ok {
333-
orderedBlocks[validatorIndex] = &httpOkValBlocks{ValidatorIndex: validatorIndex}
332+
if valBlocks, ok := orderedBlocks[validatorIndex]; ok {
333+
valBlocks.ProposedBlocks = append(valBlocks.ProposedBlocks, block)
334+
} else {
335+
orderedBlocks[validatorIndex] = &httpOkValBlocks{
336+
ValidatorIndex: validatorIndex,
337+
ProposedBlocks: []oracle.Block{block},
338+
}
334339
}
335-
orderedBlocks[validatorIndex].ProposedBlocks = append(orderedBlocks[validatorIndex].ProposedBlocks, block)
336340
}
337341

342+
// Iterate over the MissedBlocks and add them to the orderedBlocks map
338343
for _, block := range missedBlocks {
339344
validatorIndex := block.ValidatorIndex
340-
if _, ok := orderedBlocks[validatorIndex]; !ok {
341-
orderedBlocks[validatorIndex] = &httpOkValBlocks{ValidatorIndex: validatorIndex}
345+
if valBlocks, ok := orderedBlocks[validatorIndex]; ok {
346+
valBlocks.MissedBlocks = append(valBlocks.MissedBlocks, block)
347+
} else {
348+
orderedBlocks[validatorIndex] = &httpOkValBlocks{
349+
ValidatorIndex: validatorIndex,
350+
MissedBlocks: []oracle.Block{block},
351+
}
342352
}
343-
orderedBlocks[validatorIndex].MissedBlocks = append(orderedBlocks[validatorIndex].MissedBlocks, block)
344353
}
345354

355+
// Iterate over the WrongFeeBlocks and add them to the orderedBlocks map
346356
for _, block := range wrongFeeBlocks {
347357
validatorIndex := block.ValidatorIndex
348-
if _, ok := orderedBlocks[validatorIndex]; !ok {
349-
orderedBlocks[validatorIndex] = &httpOkValBlocks{ValidatorIndex: validatorIndex}
358+
if valBlocks, ok := orderedBlocks[validatorIndex]; ok {
359+
valBlocks.WrongFeeBlocks = append(valBlocks.WrongFeeBlocks, block)
360+
} else {
361+
orderedBlocks[validatorIndex] = &httpOkValBlocks{
362+
ValidatorIndex: validatorIndex,
363+
WrongFeeBlocks: []oracle.Block{block},
364+
}
350365
}
351-
orderedBlocks[validatorIndex].WrongFeeBlocks = append(orderedBlocks[validatorIndex].WrongFeeBlocks, block)
352-
}
353-
354-
// Sort the blocks within each validator index by slot number
355-
for _, valBlocks := range orderedBlocks {
356-
sort.Slice(valBlocks.ProposedBlocks, func(i, j int) bool {
357-
return valBlocks.ProposedBlocks[i].Slot < valBlocks.ProposedBlocks[j].Slot
358-
})
359-
sort.Slice(valBlocks.MissedBlocks, func(i, j int) bool {
360-
return valBlocks.MissedBlocks[i].Slot < valBlocks.MissedBlocks[j].Slot
361-
})
362-
sort.Slice(valBlocks.WrongFeeBlocks, func(i, j int) bool {
363-
return valBlocks.WrongFeeBlocks[i].Slot < valBlocks.WrongFeeBlocks[j].Slot
364-
})
365-
}
366-
367-
// Create a slice to hold the ordered validator indices
368-
orderedIndices := make([]uint64, 0, len(orderedBlocks))
369-
370-
// Extract the validator indices from the map into the slice
371-
for index := range orderedBlocks {
372-
orderedIndices = append(orderedIndices, index)
373366
}
374367

375-
// Sort the validator indices in ascending order
376-
sort.Slice(orderedIndices, func(i, j int) bool {
377-
return orderedIndices[i] < orderedIndices[j]
378-
})
379-
380368
// Create a slice to hold the final JSON output
381369
finalOutput := make([]*httpOkValBlocks, 0, len(orderedBlocks))
382370

383-
// Add the blocks to the final JSON output in the ordered sequence
384-
for _, index := range orderedIndices {
385-
finalOutput = append(finalOutput, orderedBlocks[index])
371+
// Iterate over the orderedBlocks map using the validator indices in ascending order
372+
for index := uint64(0); index < uint64(len(orderedBlocks)); index++ {
373+
if valBlocks, ok := orderedBlocks[index]; ok {
374+
finalOutput = append(finalOutput, valBlocks)
375+
}
386376
}
387377

388378
m.respondOK(w, finalOutput)

0 commit comments

Comments
 (0)