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
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions farmerbot/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/spf13/cobra"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/internal"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/parser"
)
Expand Down Expand Up @@ -40,6 +41,15 @@ var runCmd = &cobra.Command{
if err != nil {
return err
}
sub, err := substrate.NewManager(internal.SubstrateURLs[network]...).Substrate()
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

err = parser.ValidateInput(&config, sub)
if err != nil {
return err
}

config.ContinueOnPoweringOnErr = continueOnPoweringOnErr

Expand Down
38 changes: 38 additions & 0 deletions farmerbot/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,41 @@ func ParseEnv(content string) (network string, mnemonicOrSeed string, keyType st

return
}

// ValidateInput validates that included, excluded and priority nodes are in the farm
Eslam-Nawara marked this conversation as resolved.
Show resolved Hide resolved
func ValidateInput(input *internal.Config, sub internal.Substrate) error {
nodes, err := sub.GetNodes(input.FarmID)
if err != nil {
return fmt.Errorf("couldn't retrieve node for %d : %v", input.FarmID, err)
}
nodesMap := make(map[uint32]bool)
for _, node := range nodes {
nodesMap[node] = true
}

//validate included nodes
for _, includedNode := range input.IncludedNodes {
if _, ok := nodesMap[includedNode]; !ok {
return fmt.Errorf("included node with id %d doesn't exist in the farm", includedNode)
}
}
//validate excluded nodes
for _, excludedNode := range input.ExcludedNodes {
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 index >= 0 {
return fmt.Errorf("cannot include and exclude the same node %d", excludedNode)
}
}

//validate priority nodes
for _, priorityNode := range input.PriorityNodes {
index := slices.Index(input.IncludedNodes, priorityNode)
if index < 0 {
return fmt.Errorf("priority node with id %d doesn't exist included nodes", priorityNode)
}
}
return nil
}
46 changes: 46 additions & 0 deletions farmerbot/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/internal"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/mocks"
)

func TestFileReader(t *testing.T) {
Expand Down Expand Up @@ -148,3 +150,47 @@ MNEMONIC_OR_SEED=//alice`
assert.Error(t, err)
})
}

func TestValidateInput(t *testing.T) {
config := &internal.Config{FarmID: uint32(25)}
ctrl := gomock.NewController(t)
mockGetNodes := mocks.NewMockSubstrate(ctrl)
mockGetNodes.EXPECT().GetNodes(config.FarmID).Times(6).Return([]uint32{20, 21, 22, 23, 24, 30, 31, 32, 34, 40, 41}, nil)
t.Run("test valid include, exclude and priority nodes", func(t *testing.T) {
config.IncludedNodes = []uint32{20, 21, 22, 30, 31, 32, 40, 41}
config.ExcludedNodes = []uint32{23, 24, 34}
config.PriorityNodes = []uint32{20, 21}
got := ValidateInput(config, mockGetNodes)
assert.NoError(t, got)
})
t.Run("test invalid include", func(t *testing.T) {
config.IncludedNodes = []uint32{26, 27}
got := ValidateInput(config, mockGetNodes)
assert.Error(t, got)
})
t.Run("test invalid exclude", func(t *testing.T) {
config.ExcludedNodes = []uint32{26, 27}
got := ValidateInput(config, mockGetNodes)
assert.Error(t, got)
})
t.Run("test invalid priority", func(t *testing.T) {
config.IncludedNodes = []uint32{21}
config.PriorityNodes = []uint32{20, 21}
got := ValidateInput(config, mockGetNodes)
assert.Error(t, got)
})
t.Run("test overlapping nodes in include and exclude", func(t *testing.T) {
config.IncludedNodes = []uint32{21}
config.ExcludedNodes = []uint32{20, 21}
got := ValidateInput(config, mockGetNodes)
assert.Error(t, got)
})
t.Run("test overlapping nodes in priority and exclude", func(t *testing.T) {
config.IncludedNodes = []uint32{21}
config.PriorityNodes = []uint32{21}
config.ExcludedNodes = []uint32{20, 21}
got := ValidateInput(config, mockGetNodes)
assert.Error(t, got)
})

}
Loading