Skip to content

Commit

Permalink
add correct []string convertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Guitarheroua authored and illia-malachyn committed Feb 7, 2025
1 parent a09d085 commit 570377e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 25 deletions.
57 changes: 39 additions & 18 deletions admin/commands/storage/backfill_tx_error_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,33 +167,54 @@ func (b *BackfillTxErrorMessagesCommand) Handler(ctx context.Context, request *a
return nil, nil
}

// convertToExecutionStringList converts a given input to a slice of strings containing execution node IDs.
// Returns an error if the input is not a slice of strings or if the slice is empty.
func convertToExecutionStringList(executionNodeIdsIn interface{}) ([]string, error) {
var executionNodeIds []string
switch executionNodeIdsConverted := executionNodeIdsIn.(type) {
case []interface{}:
for _, executionNodeIdIn := range executionNodeIdsConverted {
executionNodeId, ok := executionNodeIdIn.(string)
if !ok {
return nil, admin.NewInvalidAdminReqParameterError("execution-node-ids", "must be a list of strings", executionNodeIdsIn)
}
executionNodeIds = append(executionNodeIds, executionNodeId)
}
default:
return nil, admin.NewInvalidAdminReqParameterError("execution-node-ids", "must be a list of strings", executionNodeIdsIn)
}

if len(executionNodeIds) == 0 {
return nil, admin.NewInvalidAdminReqParameterError("execution-node-ids", "must be a non empty list of strings", executionNodeIdsIn)
}

return executionNodeIds, nil
}

// parseExecutionNodeIds converts a list of node IDs from input to flow.IdentitySkeletonList.
// Returns an error if the IDs are invalid or empty.
//
// Expected errors during normal operation:
// - admin.InvalidAdminReqParameterError - if execution-node-ids is empty or has an invalid format.
func (b *BackfillTxErrorMessagesCommand) parseExecutionNodeIds(executionNodeIdsIn interface{}, allIdentities flow.IdentityList) (flow.IdentityList, error) {
var ids flow.IdentityList
executionNodeIds, err := convertToExecutionStringList(executionNodeIdsIn)
if err != nil {
return nil, err
}

switch executionNodeIds := executionNodeIdsIn.(type) {
case []string:
if len(executionNodeIds) == 0 {
return nil, admin.NewInvalidAdminReqParameterError("execution-node-ids", "must be a non empty list of strings", executionNodeIdsIn)
}
requestedENIdentifiers, err := commonrpc.IdentifierList(executionNodeIds)
if err != nil {
return nil, admin.NewInvalidAdminReqParameterError("execution-node-ids", err.Error(), executionNodeIdsIn)
}
requestedENIdentifiers, err := commonrpc.IdentifierList(executionNodeIds)
if err != nil {
return nil, admin.NewInvalidAdminReqParameterError("execution-node-ids", err.Error(), executionNodeIdsIn)
}

for _, enId := range requestedENIdentifiers {
id, exists := allIdentities.ByNodeID(enId)
if !exists {
return nil, admin.NewInvalidAdminReqParameterError("execution-node-ids", "could not find execution node by provided id", enId)
}
ids = append(ids, id)
var ids flow.IdentityList

for _, enId := range requestedENIdentifiers {
id, exists := allIdentities.ByNodeID(enId)
if !exists {
return nil, admin.NewInvalidAdminReqParameterError("execution-node-ids", "could not find execution node by provided id", enId)
}
default:
return nil, admin.NewInvalidAdminReqParameterError("execution-node-ids", "must be a list of strings", executionNodeIdsIn)
ids = append(ids, id)
}

return ids, nil
Expand Down
26 changes: 19 additions & 7 deletions admin/commands/storage/backfill_tx_error_messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,13 @@ func (suite *BackfillTxErrorMessagesSuite) TestValidateInvalidFormat() {

// invalid execution-node-ids param
suite.Run("invalid execution-node-ids field", func() {
executorIDsList, err := commands.ConvertToInterfaceList([]int{1, 2, 3})
suite.Require().NoError(err)

// invalid type
err := suite.command.Validator(&admin.CommandRequest{
err = suite.command.Validator(&admin.CommandRequest{
Data: map[string]interface{}{
"execution-node-ids": []int{1, 2, 3},
"execution-node-ids": executorIDsList,
},
})
suite.Error(err)
Expand All @@ -275,11 +278,15 @@ func (suite *BackfillTxErrorMessagesSuite) TestValidateInvalidFormat() {

// invalid execution node id
invalidENID := unittest.IdentifierFixture()

executorIDsList, err = commands.ConvertToInterfaceList([]string{invalidENID.String()})
suite.Require().NoError(err)

err = suite.command.Validator(&admin.CommandRequest{
Data: map[string]interface{}{
"start-height": float64(1), // raw json parses to float64
"end-height": float64(4), // raw json parses to float64
"execution-node-ids": []string{invalidENID.String()},
"execution-node-ids": executorIDsList,
},
})
suite.Error(err)
Expand All @@ -306,11 +313,13 @@ func (suite *BackfillTxErrorMessagesSuite) TestValidateValidFormat() {

// all parameters are provided
suite.Run("happy case, all parameters are provided", func() {
err := suite.command.Validator(&admin.CommandRequest{
executorIDsList, err := commands.ConvertToInterfaceList([]string{suite.allENIDs[0].ID().String()})
suite.Require().NoError(err)
err = suite.command.Validator(&admin.CommandRequest{
Data: map[string]interface{}{
"start-height": float64(1), // raw json parses to float64
"end-height": float64(3), // raw json parses to float64
"execution-node-ids": []string{suite.allENIDs[0].ID().String()},
"execution-node-ids": executorIDsList,
},
})
suite.NoError(err)
Expand Down Expand Up @@ -374,11 +383,14 @@ func (suite *BackfillTxErrorMessagesSuite) TestHandleBackfillTxErrorMessages() {
suite.allENIDs = unittest.IdentityListFixture(3, unittest.WithRole(flow.RoleExecution))

executorID := suite.allENIDs[1].ID()
executorIDsList, err := commands.ConvertToInterfaceList([]string{executorID.String()})
suite.Require().NoError(err)

req = &admin.CommandRequest{
Data: map[string]interface{}{
"start-height": float64(startHeight), // raw json parses to float64
"end-height": float64(endHeight), // raw json parses to float64
"execution-node-ids": []string{executorID.String()},
"execution-node-ids": executorIDsList,
},
}
suite.Require().NoError(suite.command.Validator(req))
Expand All @@ -401,7 +413,7 @@ func (suite *BackfillTxErrorMessagesSuite) TestHandleBackfillTxErrorMessages() {
suite.mockStoreTxErrorMessages(blockId, results, executorID)
}

_, err := suite.command.Handler(ctx, req)
_, err = suite.command.Handler(ctx, req)
suite.Require().NoError(err)
suite.assertAllExpectations()
})
Expand Down

0 comments on commit 570377e

Please sign in to comment.