Skip to content

Commit

Permalink
Merge pull request #902 from threefoldtech/development_misleading_cha…
Browse files Browse the repository at this point in the history
…in_errors

update chain go client version
  • Loading branch information
rawdaGastan authored Mar 4, 2024
2 parents cae6d45 + e868b6b commit c65525b
Show file tree
Hide file tree
Showing 30 changed files with 775 additions and 35 deletions.
2 changes: 1 addition & 1 deletion activation-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/rs/zerolog v1.32.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240116163757-68c63d80a9e0
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240227171040-f2a20ee3e965
gopkg.in/validator.v2 v2.0.1
)

Expand Down
4 changes: 2 additions & 2 deletions activation-service/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240116163757-68c63d80a9e0 h1:0ZMm/xYPgYv3vICNRw3vXTiJ/jVg8LkBLe8TPBEO0MQ=
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240116163757-68c63d80a9e0/go.mod h1:dtDKAPiUDxAwIkfHV7xcAFZcOm+xwNIuOI1MLFS+MeQ=
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240227171040-f2a20ee3e965 h1:A4EF0webCGCQPym/uWrhhL6H3j/FKn60G99tR+Zl93o=
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240227171040-f2a20ee3e965/go.mod h1:dtDKAPiUDxAwIkfHV7xcAFZcOm+xwNIuOI1MLFS+MeQ=
github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM=
github.com/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7AmcWpGR8lSZfqI=
github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms=
Expand Down
2 changes: 1 addition & 1 deletion farmerbot/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Farmerbot

<a href='https://github.com/jpoles1/gopherbadger' target='_blank'>![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-78%25-brightgreen.svg?longCache=true&style=flat)</a>
<a href='https://github.com/jpoles1/gopherbadger' target='_blank'>![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-75%25-brightgreen.svg?longCache=true&style=flat)</a>

Farmerbot is a service that farmers can run allowing them to automatically manage power of the nodes of their farm.

Expand Down
2 changes: 1 addition & 1 deletion farmerbot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/rs/zerolog v1.32.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240116163757-68c63d80a9e0
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240227171040-f2a20ee3e965
github.com/threefoldtech/tfgrid-sdk-go/grid-proxy v0.14.0
github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go v0.11.10
github.com/threefoldtech/zos v0.5.6-0.20240226114056-364e04acbed3
Expand Down
4 changes: 2 additions & 2 deletions farmerbot/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240116163757-68c63d80a9e0 h1:0ZMm/xYPgYv3vICNRw3vXTiJ/jVg8LkBLe8TPBEO0MQ=
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240116163757-68c63d80a9e0/go.mod h1:dtDKAPiUDxAwIkfHV7xcAFZcOm+xwNIuOI1MLFS+MeQ=
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240227171040-f2a20ee3e965 h1:A4EF0webCGCQPym/uWrhhL6H3j/FKn60G99tR+Zl93o=
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240227171040-f2a20ee3e965/go.mod h1:dtDKAPiUDxAwIkfHV7xcAFZcOm+xwNIuOI1MLFS+MeQ=
github.com/threefoldtech/tfgrid-sdk-go/grid-proxy v0.14.0 h1:ywAcTdB4pfxKnh0MzVMWUMR4d+tHua9NxIdLcsfDUfI=
github.com/threefoldtech/tfgrid-sdk-go/grid-proxy v0.14.0/go.mod h1:ptD6Oj0HoW63ezb8wWXL1y9b3wuJhE+eCRgKBTuGO88=
github.com/threefoldtech/zbus v1.0.1 h1:3KaEpyOiDYAw+lrAyoQUGIvY9BcjVRXlQ1beBRqhRNk=
Expand Down
4 changes: 2 additions & 2 deletions farmerbot/internal/farmerbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (f *FarmerBot) iterateOnNodes(ctx context.Context, subConn Substrate) error
}
}

if f.shouldWakeUp(ctx, subConn, &node, roundStart, wakeUpCalls) {
if f.shouldWakeUp(ctx, &node, roundStart, wakeUpCalls) {
err = f.state.updateNode(node)
if err != nil {
log.Error().Err(err).Send()
Expand Down Expand Up @@ -375,7 +375,7 @@ func (f *FarmerBot) addOrUpdateNode(ctx context.Context, subConn Substrate, node
return nil
}

func (f *FarmerBot) shouldWakeUp(ctx context.Context, sub Substrate, node *node, roundStart time.Time, wakeUpCalls uint8) bool {
func (f *FarmerBot) shouldWakeUp(ctx context.Context, node *node, roundStart time.Time, wakeUpCalls uint8) bool {
if node.powerState != off ||
wakeUpCalls >= f.config.Power.PeriodicWakeUpLimit {
return false
Expand Down
4 changes: 4 additions & 0 deletions farmerbot/internal/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (f *FarmerBot) powerOff(sub Substrate, nodeID uint32) error {
return fmt.Errorf("cannot power off node '%d', node has a rent contract", nodeID)
}

if node.hasActiveContracts {
return fmt.Errorf("cannot power off node '%d', node has active contracts", nodeID)
}

if !node.isUnused() {
return fmt.Errorf("cannot power off node '%d', node is used", nodeID)
}
Expand Down
11 changes: 11 additions & 0 deletions farmerbot/internal/power_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ func TestPower(t *testing.T) {
state.addNode(oldNode1)
})

t.Run("test invalid power off: node has active contracts", func(t *testing.T) {
testNode := state.nodes[1]
testNode.hasActiveContracts = true
state.addNode(testNode)

err = farmerbot.powerOff(sub, uint32(state.nodes[1].ID))
assert.Error(t, err)

state.addNode(oldNode1)
})

t.Run("test invalid power off: node power changed", func(t *testing.T) {
testNode := state.nodes[1]
testNode.lastTimePowerStateChanged = time.Now()
Expand Down
7 changes: 7 additions & 0 deletions farmerbot/internal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ func getNode(

configNode.hasActiveRentContract = rentContract != 0

activeContracts, err := sub.GetNodeContracts(nodeID)
if err != nil {
return node{}, fmt.Errorf("failed to get node %d active contracts from substrate with error: %w", nodeID, err)
}

configNode.hasActiveContracts = len(activeContracts) > 0

powerTarget, err := sub.GetPowerTarget(nodeID)
if err != nil {
return node{}, fmt.Errorf("failed to get node %d power target from substrate with error: %w", nodeID, err)
Expand Down
13 changes: 11 additions & 2 deletions farmerbot/internal/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func mockRMBAndSubstrateCalls(
inputs Config, on bool, noFarm bool,
resources gridtypes.Capacity, errs []string, emptyNode, emptyTwin bool,
) {
farmErr, nodesErr, nodeErr, dedicatedErr, rentErr, powerErr, statsErr, poolsErr, gpusErr := mocksErr(errs)
farmErr, nodesErr, nodeErr, dedicatedErr, rentErr, contractsErr, powerErr, statsErr, poolsErr, gpusErr := mocksErr(errs)

// farm calls
if !noFarm {
Expand Down Expand Up @@ -69,6 +69,11 @@ func mockRMBAndSubstrateCalls(
return
}

sub.EXPECT().GetNodeContracts(nodeID).Return([]types.U64{}, contractsErr)
if contractsErr != nil {
return
}

sub.EXPECT().GetPowerTarget(nodeID).Return(substrate.NodePower{
State: substrate.PowerState{
IsUp: on,
Expand Down Expand Up @@ -319,7 +324,7 @@ func TestStateModel(t *testing.T) {
})
}

func mocksErr(errs []string) (farmErr, nodesErr, nodeErr, dedicatedErr, rentErr, powerErr, statsErr, poolsErr, gpusErr error) {
func mocksErr(errs []string) (farmErr, nodesErr, nodeErr, dedicatedErr, rentErr, contractsErr, powerErr, statsErr, poolsErr, gpusErr error) {
// errors
if slices.Contains(errs, "farm") {
farmErr = fmt.Errorf("error")
Expand All @@ -341,6 +346,10 @@ func mocksErr(errs []string) (farmErr, nodesErr, nodeErr, dedicatedErr, rentErr,
rentErr = fmt.Errorf("error")
}

if slices.Contains(errs, "contracts") {
contractsErr = fmt.Errorf("error")
}

if slices.Contains(errs, "rentNotExist") {
rentErr = substrate.ErrNotFound
}
Expand Down
1 change: 1 addition & 0 deletions farmerbot/internal/substrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Substrate interface {
GetPowerTarget(nodeID uint32) (power substrate.NodePower, err error)

GetNodeRentContract(nodeID uint32) (uint64, error)
GetNodeContracts(nodeID uint32) ([]types.U64, error)
GetNode(nodeID uint32) (*substrate.Node, error)
GetFarm(id uint32) (*substrate.Farm, error)
GetNodes(farmID uint32) ([]uint32, error)
Expand Down
9 changes: 9 additions & 0 deletions farmerbot/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type node struct {
pools []pkg.PoolMetrics
gpus []zos.GPU
hasActiveRentContract bool
hasActiveContracts bool
dedicated bool
neverShutDown bool

Expand Down Expand Up @@ -115,6 +116,13 @@ func (n *node) update(

n.hasActiveRentContract = rentContract != 0

activeContracts, err := sub.GetNodeContracts(nodeID)
if err != nil {
return fmt.Errorf("failed to get node %d active contracts from substrate with error: %w", nodeID, err)
}

n.hasActiveContracts = len(activeContracts) > 0

powerTarget, err := sub.GetPowerTarget(nodeID)
if err != nil {
return fmt.Errorf("failed to get node %d power target from substrate with error: %w", nodeID, err)
Expand Down Expand Up @@ -219,6 +227,7 @@ func (n *node) canShutDown() bool {
n.PublicConfig.HasValue ||
n.neverShutDown ||
n.hasActiveRentContract ||
n.hasActiveContracts ||
n.timeoutClaimedResources.After(time.Now()) ||
time.Since(n.lastTimePowerStateChanged) < periodicWakeUpDuration {
return false
Expand Down
16 changes: 16 additions & 0 deletions farmerbot/mocks/substrate_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c65525b

Please sign in to comment.