Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nodes validation in farmerbot #1135

Merged
merged 17 commits into from
Aug 4, 2024

Conversation

SalmaElsoly
Copy link
Contributor

@SalmaElsoly SalmaElsoly commented Jul 29, 2024

Description

Describe the changes introduced by this PR and what does it affect

Changes

List of changes this PR includes

  • farmer bot
    • add validation for included, excluded and priority nodes
      ensuring that each of them is included in the farm, no overlapping between included and excluded nodes

Related Issues

List of related issues

Checklist

  • Tests included
  • Build pass
  • Documentation
  • Code format and docstring

@SalmaElsoly SalmaElsoly marked this pull request as draft July 29, 2024 10:00
@SalmaElsoly SalmaElsoly marked this pull request as ready for review July 30, 2024 07:28
@SalmaElsoly SalmaElsoly changed the title feat: add nodes validation Add nodes validation in farmerbot Jul 30, 2024
if _, ok := nodesMap[excludedNode]; !ok {
return fmt.Errorf("excluded node with id %d doesn't exist in the farm", excludedNode)
}
index := slices.Index(input.IncludedNodes, excludedNode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use slices.Contains

if err != nil {
return err
}
defer sub.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no connection was opened here so no need to close the manager

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Substrate( ) by default returns a connection :
image
doesn't it make connection to db?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahaaa, that's kinda confusing, please create a manager then a connection, or at least rename to subConn, I couldn't see your're using a connection

)

// ValidateInput validates that included, excluded and priority nodes are in the farm
func ValidateInput(input *internal.Config, sub internal.Substrate) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why using the address, why not just passing the value of the config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, there is no need for it

@SalmaElsoly SalmaElsoly requested a review from Eslam-Nawara July 31, 2024 07:26
@@ -41,6 +41,11 @@ var runCmd = &cobra.Command{
return err
}

err = parser.ValidateInput(config, network)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

		if err := parser.ValidateConfig(config, network); err != nil {
			return fmt.Errorf("invalid configuration: %w", err)
		}

nodesMap[node] = true
}
if len(input.IncludedNodes) != 0 {
//validate included nodes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract the logic into another function and call it here, same for the rest

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about the testing, split it too?

ctrl := gomock.NewController(t)
mockGetNodes := mocks.NewMockSubstrate(ctrl)
mockGetNodes.EXPECT().GetNodes(config.FarmID).Times(13).Return([]uint32{20, 21, 22, 23, 24, 30, 31, 32, 34, 40, 41}, nil)
t.Run("test valid include, exclude, priority nodes and never shutdown nodes", func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's refactor to use e.g Table Driven Tests instead e.g
image

@SalmaElsoly SalmaElsoly requested a review from xmonader August 1, 2024 11:59
nodesMap[node] = true
}
if len(input.IncludedNodes) != 0 {
if err := validateWithIncludedNodes(input, nodesMap); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think what thabet meant was to create a validator function for every set of nodes eg (validateIncludedNodes, validateExcludedNodes,...)

return fmt.Errorf("%s node with id %d doesn't exist in the included nodes ", typeOfValidation, node)
}
if slices.Contains(excluded, node) {
return fmt.Errorf("cannot %s and exclude the same node %d", typeOfValidation, node)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("cannot %s and exclude the same node %d", typeOfValidation, node)
return fmt.Errorf("node %d can not be both %s and excluded", node, typeOfValidation)

priorityNodes []uint32
excludedNodes []uint32
neverShutdownNodes []uint32
shouldError bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should fail?

Comment on lines 72 to 74
if slices.Contains(excluded, node) {
return fmt.Errorf("node %d can not be both %s and excluded", node, typeOfValidation)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at this point if the node is in the included nodes, it won't be in the excluded nodes, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it will not be in the excluded, this check is added for the case of all nodes included because in that case there is no validation for included nodes so priority and nevershutdown nodes need to be validated against excluded nodes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then you'd need to remove the excluded nodes from the list of included nodes, check line 41, included nodes should NOT be a map that include excluded nodes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after that there is no need for this check right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

return nil
}

func validatePriorityOrNeverShutdown(typeOfValidation string, toBeValidated, excluded []uint32, nodes map[uint32]bool) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename nodes to includedNodes

Comment on lines 33 to 47
if len(input.IncludedNodes) != 0 {
if err := validateIncludedNodes(input.IncludedNodes, input.ExcludedNodes, nodesMap); err != nil {
return err
}
for _, includedNode := range input.IncludedNodes {
includedNodes[includedNode] = true
}
} else {
for key, value := range nodesMap {
if slices.Contains(input.ExcludedNodes, key) {
continue
}
includedNodes[key] = value
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(input.IncludedNodes) != 0 {
if err := validateIncludedNodes(input.IncludedNodes, input.ExcludedNodes, nodesMap); err != nil {
return err
}
for _, includedNode := range input.IncludedNodes {
includedNodes[includedNode] = true
}
} else {
for key, value := range nodesMap {
if slices.Contains(input.ExcludedNodes, key) {
continue
}
includedNodes[key] = value
}
}
for _, includedNode := range input.IncludedNodes {
includedNodes[includedNode] = true
}
if len(icludedNodes == 0){
for key, value := range nodesMap {
if !slices.Contains(input.ExcludedNodes, key) {
includedNodes[key] = value
}
}
}
if err := validateIncludedNodes(input.IncludedNodes, input.ExcludedNodes, nodesMap); err != nil {
return err
}

if err != nil {
return fmt.Errorf("couldn't retrieve node for %d : %v", input.FarmID, err)
}
nodesMap := make(map[uint32]bool)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to farm nodes

@Eslam-Nawara Eslam-Nawara merged commit 938013a into development Aug 4, 2024
56 checks passed
@Eslam-Nawara Eslam-Nawara deleted the development_farmerbot_input_validation branch August 4, 2024 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants