Skip to content

Commit 89ba4fd

Browse files
committed
finish ValidatorBlocks endpoint
1 parent 5873cfe commit 89ba4fd

File tree

2 files changed

+39
-68
lines changed

2 files changed

+39
-68
lines changed

api/api.go

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,10 @@ type httpOkProofs struct {
179179
PendingRewardsWei string `json:"pending_rewards_wei"`
180180
}
181181

182-
type SimplifiedValidatorInfo struct {
183-
Status oracle.ValidatorStatus `json:"status"`
184-
AccumulatedRewards *big.Int `json:"accumulated_rewards_wei"`
185-
PendingRewards *big.Int `json:"pending_rewards_wei"`
186-
Collateral *big.Int `json:"collateral_wei"`
187-
WithdrawalAddress string `json:"withdrawal_address"`
188-
ValidatorIndex uint64 `json:"validator_index"`
189-
ValidatorKey string `json:"validator_key"`
190-
ProposedBlocks []oracle.Block `json:"proposed_block"`
191-
MissedBlocks []oracle.Block `json:"missed_blocks"`
192-
WrongFeeBlocks []oracle.Block `json:"wrong_fee_blocks"`
182+
type httpOkValBlocks struct {
183+
ProposedBlocks []oracle.Block `json:"proposed_blocks"`
184+
MissedBlocks []oracle.Block `json:"missed_blocks"`
185+
WrongFeeBlocks []oracle.Block `json:"wrong_fee_blocks"`
193186
}
194187

195188
type ApiService struct {
@@ -341,39 +334,34 @@ func (m *ApiService) handleMemoryValidatorBlocks(w http.ResponseWriter, r *http.
341334
})
342335

343336
// Create a map to hold the ordered blocks, with the ValidatorKey as the key
344-
orderedBlocks := make(map[string]interface{})
337+
orderedBlocks := make(map[string]*httpOkValBlocks)
345338

346339
// Add the ordered blocks to the map
347340
for _, block := range proposedBlocks {
348341
validatorKey := block.ValidatorKey
349-
if orderedBlocks[validatorKey] == nil {
350-
orderedBlocks[validatorKey] = make(map[string]interface{})
342+
if _, ok := orderedBlocks[validatorKey]; !ok {
343+
orderedBlocks[validatorKey] = &httpOkValBlocks{}
351344
}
352-
orderedBlocks[validatorKey].(map[string]interface{})["proposed_block"] = append(orderedBlocks[validatorKey].(map[string]interface{})["proposed_block"].([]oracle.Block), block)
345+
orderedBlocks[validatorKey].ProposedBlocks = append(orderedBlocks[validatorKey].ProposedBlocks, block)
353346
}
347+
354348
for _, block := range missedBlocks {
355349
validatorKey := block.ValidatorKey
356-
if orderedBlocks[validatorKey] == nil {
357-
orderedBlocks[validatorKey] = make(map[string]interface{})
350+
if _, ok := orderedBlocks[validatorKey]; !ok {
351+
orderedBlocks[validatorKey] = &httpOkValBlocks{}
358352
}
359-
orderedBlocks[validatorKey].(map[string]interface{})["missed_blocks"] = append(orderedBlocks[validatorKey].(map[string]interface{})["missed_blocks"].([]oracle.Block), block)
353+
orderedBlocks[validatorKey].MissedBlocks = append(orderedBlocks[validatorKey].MissedBlocks, block)
360354
}
355+
361356
for _, block := range wrongFeeBlocks {
362357
validatorKey := block.ValidatorKey
363-
if orderedBlocks[validatorKey] == nil {
364-
orderedBlocks[validatorKey] = make(map[string]interface{})
358+
if _, ok := orderedBlocks[validatorKey]; !ok {
359+
orderedBlocks[validatorKey] = &httpOkValBlocks{}
365360
}
366-
orderedBlocks[validatorKey].(map[string]interface{})["wrong_fee_blocks"] = append(orderedBlocks[validatorKey].(map[string]interface{})["wrong_fee_blocks"].([]oracle.Block), block)
361+
orderedBlocks[validatorKey].WrongFeeBlocks = append(orderedBlocks[validatorKey].WrongFeeBlocks, block)
367362
}
368363

369-
// Convert the map to JSON
370-
responseJSON, err := json.Marshal(orderedBlocks)
371-
if err != nil {
372-
// Handle error
373-
w.WriteHeader(http.StatusInternalServerError)
374-
return
375-
}
376-
m.respondOK(w, string(responseJSON))
364+
m.respondOK(w, orderedBlocks)
377365
}
378366

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

api/api_test.go

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -343,34 +343,18 @@ func TestHandleGetOrderedBlocksByValidatorKey(t *testing.T) {
343343
mockOracle := oracle.NewOracle(mockConfig)
344344
oracle.NewOracleState(mockConfig)
345345

346-
// blockPool := oracle.Block{
347-
// // Set the desired values for the blockPool struct
348-
// Slot: 0,
349-
// Block: 0,
350-
// ValidatorIndex: 420,
351-
// BlockType: 3,
352-
// }
353-
blockSubs := []oracle.Subscription{
354-
// Create mock subscription objects as needed
355-
}
356-
blockUnsubs := []oracle.Unsubscription{
357-
// Create mock unsubscription objects as needed
346+
numBlocks := 100
347+
for i := 0; i < numBlocks; i++ {
348+
// Create mock blocks and add them to the OracleState's ProposedBlocks
349+
mockBlock := blockOkProposal(
350+
0,
351+
420,
352+
fmt.Sprintf("%d", i+1),
353+
big.NewInt(int64(rand.Intn(1000)+10000)),
354+
"0xaaa0000000000000000000000000000000000000",
355+
)
356+
mockOracle.State().HandleCorrectBlockProposal(mockBlock)
358357
}
359-
blockDonations := []oracle.Donation{
360-
// Create mock donation objects as needed
361-
}
362-
mevReward := big.NewInt(int64(rand.Intn(1000) + 10000))
363-
364-
// Call the function and capture the returned values
365-
processedSlot, err := mockOracle.AdvanceStateToNextSlot(blockOkProposal(
366-
0,
367-
420,
368-
"1",
369-
mevReward,
370-
"0xaaa0000000000000000000000000000000000000"), blockSubs, blockUnsubs, blockDonations)
371-
fmt.Print(mockOracle.State().ProposedBlocks)
372-
373-
_ = processedSlot
374358
// Create an instance of your ApiService with the mock Oracle
375359
apiService := &ApiService{
376360
oracle: mockOracle,
@@ -384,21 +368,20 @@ func TestHandleGetOrderedBlocksByValidatorKey(t *testing.T) {
384368
// Call the handler function directly, passing in the ResponseRecorder and the Request
385369
handler := http.HandlerFunc(apiService.handleMemoryValidatorBlocks)
386370
handler.ServeHTTP(rr, req)
387-
fmt.Print(rr.Body.String())
388-
389-
handler2 := http.HandlerFunc(apiService.handleMemoryAllBlocks)
390-
handler2.ServeHTTP(rr, req)
391-
fmt.Print(rr.Body.String())
371+
fmt.Println(rr.Body)
392372

393-
// // Perform assertions on the response
394-
// if status := rr.Code; status != http.StatusOK {
395-
// t.Errorf("handler returned wrong status code: got %v, want %v", status, http.StatusOK)
396-
// }
373+
// handler2 := http.HandlerFunc(apiService.handleMemoryAllBlocks)
374+
// handler2.ServeHTTP(rr, req)
397375
// fmt.Print(rr.Body.String())
398-
// // Perform additional assertions on the response body or headers if needed
399-
// // For example, you can check the response body for expected JSON data
400376

401-
// // Example assertion for JSON response
377+
// Perform assertions on the response
378+
if status := rr.Code; status != http.StatusOK {
379+
t.Errorf("handler returned wrong status code: got %v, want %v", status, http.StatusOK)
380+
}
381+
// Perform additional assertions on the response body or headers if needed
382+
// For example, you can check the response body for expected JSON data
383+
384+
// Example assertion for JSON response
402385
// expectedResponse := `{"message":"success"}`
403386
// if rr.Body.String() != expectedResponse {
404387
// t.Errorf("handler returned unexpected body: got %v, want %v", rr.Body.String(), expectedResponse)

0 commit comments

Comments
 (0)