Skip to content

Commit 351fe67

Browse files
committed
add sorting
1 parent 89ba4fd commit 351fe67

File tree

2 files changed

+90
-30
lines changed

2 files changed

+90
-30
lines changed

api/api.go

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ type httpOkProofs struct {
180180
}
181181

182182
type httpOkValBlocks struct {
183+
ValidatorIndex uint64 `json:"validator_index"`
183184
ProposedBlocks []oracle.Block `json:"proposed_blocks"`
184185
MissedBlocks []oracle.Block `json:"missed_blocks"`
185186
WrongFeeBlocks []oracle.Block `json:"wrong_fee_blocks"`
@@ -322,46 +323,69 @@ func (m *ApiService) handleMemoryValidatorBlocks(w http.ResponseWriter, r *http.
322323
missedBlocks := oracleState.MissedBlocks
323324
wrongFeeBlocks := oracleState.WrongFeeBlocks
324325

325-
// Sort the blocks by ValidatorKey
326-
sort.Slice(proposedBlocks, func(i, j int) bool {
327-
return proposedBlocks[i].ValidatorKey < proposedBlocks[j].ValidatorKey
328-
})
329-
sort.Slice(missedBlocks, func(i, j int) bool {
330-
return missedBlocks[i].ValidatorKey < missedBlocks[j].ValidatorKey
331-
})
332-
sort.Slice(wrongFeeBlocks, func(i, j int) bool {
333-
return wrongFeeBlocks[i].ValidatorKey < wrongFeeBlocks[j].ValidatorKey
334-
})
335-
336-
// Create a map to hold the ordered blocks, with the ValidatorKey as the key
337-
orderedBlocks := make(map[string]*httpOkValBlocks)
326+
// Create a map to hold the ordered blocks, with the ValidatorIndex as the key
327+
orderedBlocks := make(map[uint64]*httpOkValBlocks)
338328

339329
// Add the ordered blocks to the map
340330
for _, block := range proposedBlocks {
341-
validatorKey := block.ValidatorKey
342-
if _, ok := orderedBlocks[validatorKey]; !ok {
343-
orderedBlocks[validatorKey] = &httpOkValBlocks{}
331+
validatorIndex := block.ValidatorIndex
332+
if _, ok := orderedBlocks[validatorIndex]; !ok {
333+
orderedBlocks[validatorIndex] = &httpOkValBlocks{ValidatorIndex: validatorIndex}
344334
}
345-
orderedBlocks[validatorKey].ProposedBlocks = append(orderedBlocks[validatorKey].ProposedBlocks, block)
335+
orderedBlocks[validatorIndex].ProposedBlocks = append(orderedBlocks[validatorIndex].ProposedBlocks, block)
346336
}
347337

348338
for _, block := range missedBlocks {
349-
validatorKey := block.ValidatorKey
350-
if _, ok := orderedBlocks[validatorKey]; !ok {
351-
orderedBlocks[validatorKey] = &httpOkValBlocks{}
339+
validatorIndex := block.ValidatorIndex
340+
if _, ok := orderedBlocks[validatorIndex]; !ok {
341+
orderedBlocks[validatorIndex] = &httpOkValBlocks{ValidatorIndex: validatorIndex}
352342
}
353-
orderedBlocks[validatorKey].MissedBlocks = append(orderedBlocks[validatorKey].MissedBlocks, block)
343+
orderedBlocks[validatorIndex].MissedBlocks = append(orderedBlocks[validatorIndex].MissedBlocks, block)
354344
}
355345

356346
for _, block := range wrongFeeBlocks {
357-
validatorKey := block.ValidatorKey
358-
if _, ok := orderedBlocks[validatorKey]; !ok {
359-
orderedBlocks[validatorKey] = &httpOkValBlocks{}
347+
validatorIndex := block.ValidatorIndex
348+
if _, ok := orderedBlocks[validatorIndex]; !ok {
349+
orderedBlocks[validatorIndex] = &httpOkValBlocks{ValidatorIndex: validatorIndex}
360350
}
361-
orderedBlocks[validatorKey].WrongFeeBlocks = append(orderedBlocks[validatorKey].WrongFeeBlocks, block)
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)
373+
}
374+
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+
380+
// Create a slice to hold the final JSON output
381+
finalOutput := make([]*httpOkValBlocks, 0, len(orderedBlocks))
382+
383+
// Add the blocks to the final JSON output in the ordered sequence
384+
for _, index := range orderedIndices {
385+
finalOutput = append(finalOutput, orderedBlocks[index])
362386
}
363387

364-
m.respondOK(w, orderedBlocks)
388+
m.respondOK(w, finalOutput)
365389
}
366390

367391
func (m *ApiService) handleMemoryStatistics(w http.ResponseWriter, req *http.Request) {

api/api_test.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,18 +342,36 @@ func TestHandleGetOrderedBlocksByValidatorKey(t *testing.T) {
342342
// Create a mock Oracle using the NewOracle function
343343
mockOracle := oracle.NewOracle(mockConfig)
344344
oracle.NewOracleState(mockConfig)
345-
346345
numBlocks := 100
347346
for i := 0; i < numBlocks; i++ {
347+
// Generate a random ValidatorIndex between 0 and 99
348+
validatorIndex := rand.Intn(100)
349+
348350
// Create mock blocks and add them to the OracleState's ProposedBlocks
349351
mockBlock := blockOkProposal(
350-
0,
351-
420,
352-
fmt.Sprintf("%d", i+1),
352+
rand.Uint64()%101,
353+
uint64(validatorIndex),
354+
"PUBKEY",
353355
big.NewInt(int64(rand.Intn(1000)+10000)),
354356
"0xaaa0000000000000000000000000000000000000",
355357
)
356358
mockOracle.State().HandleCorrectBlockProposal(mockBlock)
359+
360+
// Create mock missed blocks and add them to the OracleState's MissedBlocks
361+
mockMissedBlock := MissedBlock(
362+
rand.Uint64()%101,
363+
uint64(validatorIndex),
364+
"PUBKEY",
365+
)
366+
mockOracle.State().HandleMissedBlock(mockMissedBlock)
367+
368+
// Create mock wrong fee blocks and add them to the OracleState's WrongFeeBlocks
369+
mockWrongFeeBlock := WrongFeeBlock(
370+
rand.Uint64()%101,
371+
uint64(validatorIndex),
372+
"PUBKEY",
373+
)
374+
mockOracle.State().HandleBanValidator(mockWrongFeeBlock)
357375
}
358376
// Create an instance of your ApiService with the mock Oracle
359377
apiService := &ApiService{
@@ -388,6 +406,24 @@ func TestHandleGetOrderedBlocksByValidatorKey(t *testing.T) {
388406
// }
389407
}
390408

409+
func MissedBlock(slot uint64, valIndex uint64, pubKey string) oracle.Block {
410+
return oracle.Block{
411+
Slot: slot,
412+
ValidatorIndex: valIndex,
413+
ValidatorKey: pubKey,
414+
BlockType: oracle.MissedProposal,
415+
}
416+
}
417+
418+
func WrongFeeBlock(slot uint64, valIndex uint64, pubKey string) oracle.Block {
419+
return oracle.Block{
420+
Slot: slot,
421+
ValidatorIndex: valIndex,
422+
ValidatorKey: pubKey,
423+
BlockType: oracle.WrongFeeRecipient,
424+
}
425+
}
426+
391427
func blockOkProposal(slot uint64, valIndex uint64, pubKey string, reward *big.Int, withAddress string) oracle.Block {
392428
return oracle.Block{
393429
Slot: slot,

0 commit comments

Comments
 (0)