Skip to content

Commit

Permalink
contractor: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Jul 30, 2024
1 parent c8ab1bd commit f07554a
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 119 deletions.
13 changes: 2 additions & 11 deletions api/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,10 @@ func (c AutopilotConfig) Validate() error {
return nil
}

func (c ContractsConfig) IsContractInSet(contract Contract) bool {
for _, set := range contract.ContractSets {
if set == c.Set {
return true
}
}
return false
}

func (c ContractsConfig) SortContractsForMaintenance(contracts []Contract) {
sort.SliceStable(contracts, func(i, j int) bool {
iInSet := c.IsContractInSet(contracts[i])
jInSet := c.IsContractInSet(contracts[j])
iInSet := contracts[i].InSet(c.Set)
jInSet := contracts[j].InSet(c.Set)
if iInSet != jInSet {
return iInSet
}
Expand Down
2 changes: 1 addition & 1 deletion autopilot/contractor/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newContractRenewalFailedAlert(contract api.ContractMetadata, ourFault bool,
Message: "Contract renewal failed",
Data: map[string]interface{}{
"error": err.Error(),
"ourFault": ourFault,
"hostError": !ourFault,
"contractID": contract.ID.String(),
"hostKey": contract.HostKey.String(),
},
Expand Down
11 changes: 9 additions & 2 deletions autopilot/contractor/contract_spending.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package contractor

import (
"context"

"go.sia.tech/core/types"
"go.sia.tech/renterd/api"
)
Expand All @@ -27,7 +29,12 @@ func currentPeriodSpending(contracts []api.ContractMetadata, currentPeriod uint6
return totalAllocated
}

func remainingFunds(contracts []api.ContractMetadata, state *MaintenanceState) types.Currency {
func remainingAllowance(ctx context.Context, bus Bus, state *MaintenanceState) (types.Currency, error) {
contracts, err := bus.Contracts(ctx, api.ContractsOpts{})
if err != nil {
return types.Currency{}, err
}

// find out how much we spent in the current period
spent := currentPeriodSpending(contracts, state.Period())

Expand All @@ -36,5 +43,5 @@ func remainingFunds(contracts []api.ContractMetadata, state *MaintenanceState) t
if state.Allowance().Cmp(spent) > 0 {
remaining = state.Allowance().Sub(spent)
}
return remaining
return remaining, nil
}
167 changes: 72 additions & 95 deletions autopilot/contractor/contractor.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion autopilot/contractor/hostfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (u *unusableHostsBreakdown) keysAndValues() []interface{} {
// - recoverable -> can be usable in the contract set if it is refreshed/renewed
// - refresh -> should be refreshed
// - renew -> should be renewed
func (c *Contractor) isUsableContract(cfg api.AutopilotConfig, s rhpv2.HostSettings, pt rhpv3.HostPriceTable, rs api.RedundancySettings, contract api.Contract, inSet bool, bh uint64, f *ipFilter) (usable, refresh, renew bool, reasons []string) {
func (c *Contractor) isUsableContract(cfg api.AutopilotConfig, s rhpv2.HostSettings, pt rhpv3.HostPriceTable, rs api.RedundancySettings, contract api.Contract, inSet bool, bh uint64, f *hostSet) (usable, refresh, renew bool, reasons []string) {
usable = true
if bh > contract.EndHeight() {
reasons = append(reasons, errContractExpired.Error())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ var (
)

type (
ipFilter struct {
hostSet struct {
subnetToHostKey map[string]string

logger *zap.SugaredLogger
}
)

func (f *ipFilter) HasRedundantIP(host api.Host) bool {
func (hs *hostSet) HasRedundantIP(host api.Host) bool {
// validate host subnets
if len(host.Subnets) == 0 {
f.logger.Errorf("host %v has no subnet, treating its IP %v as redundant", host.PublicKey, host.NetAddress)
hs.logger.Errorf("host %v has no subnet, treating its IP %v as redundant", host.PublicKey, host.NetAddress)
return true
} else if len(host.Subnets) > 2 {
f.logger.Errorf("host %v has more than 2 subnets, treating its IP %v as redundant", host.PublicKey, errHostTooManySubnets)
hs.logger.Errorf("host %v has more than 2 subnets, treating its IP %v as redundant", host.PublicKey, errHostTooManySubnets)
return true
}

// check if we know about this subnet
var knownHost string
for _, subnet := range host.Subnets {
if knownHost = f.subnetToHostKey[subnet]; knownHost != "" {
if knownHost = hs.subnetToHostKey[subnet]; knownHost != "" {
break
}
}
Expand All @@ -44,8 +44,8 @@ func (f *ipFilter) HasRedundantIP(host api.Host) bool {
return false
}

func (f *ipFilter) Add(host api.Host) {
func (hs *hostSet) Add(host api.Host) {
for _, subnet := range host.Subnets {
f.subnetToHostKey[subnet] = host.PublicKey.String()
hs.subnetToHostKey[subnet] = host.PublicKey.String()
}
}
4 changes: 2 additions & 2 deletions autopilot/contractor/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func (ctx *mCtx) WantedContracts() uint64 {
return ctx.state.AP.Config.Contracts.Amount
}

func (ctx *mCtx) IsContractInSet(contract api.Contract) bool {
return ctx.state.ContractsConfig().IsContractInSet(contract)
func (ctx *mCtx) Set() string {
return ctx.state.ContractsConfig().Set
}

func (ctx *mCtx) SortContractsForMaintenance(contracts []api.Contract) {
Expand Down

0 comments on commit f07554a

Please sign in to comment.