Skip to content

Commit f07554a

Browse files
contractor: address comments
1 parent c8ab1bd commit f07554a

File tree

7 files changed

+94
-119
lines changed

7 files changed

+94
-119
lines changed

api/autopilot.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,10 @@ func (c AutopilotConfig) Validate() error {
138138
return nil
139139
}
140140

141-
func (c ContractsConfig) IsContractInSet(contract Contract) bool {
142-
for _, set := range contract.ContractSets {
143-
if set == c.Set {
144-
return true
145-
}
146-
}
147-
return false
148-
}
149-
150141
func (c ContractsConfig) SortContractsForMaintenance(contracts []Contract) {
151142
sort.SliceStable(contracts, func(i, j int) bool {
152-
iInSet := c.IsContractInSet(contracts[i])
153-
jInSet := c.IsContractInSet(contracts[j])
143+
iInSet := contracts[i].InSet(c.Set)
144+
jInSet := contracts[j].InSet(c.Set)
154145
if iInSet != jInSet {
155146
return iInSet
156147
}

autopilot/contractor/alerts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func newContractRenewalFailedAlert(contract api.ContractMetadata, ourFault bool,
3333
Message: "Contract renewal failed",
3434
Data: map[string]interface{}{
3535
"error": err.Error(),
36-
"ourFault": ourFault,
36+
"hostError": !ourFault,
3737
"contractID": contract.ID.String(),
3838
"hostKey": contract.HostKey.String(),
3939
},

autopilot/contractor/contract_spending.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package contractor
22

33
import (
4+
"context"
5+
46
"go.sia.tech/core/types"
57
"go.sia.tech/renterd/api"
68
)
@@ -27,7 +29,12 @@ func currentPeriodSpending(contracts []api.ContractMetadata, currentPeriod uint6
2729
return totalAllocated
2830
}
2931

30-
func remainingFunds(contracts []api.ContractMetadata, state *MaintenanceState) types.Currency {
32+
func remainingAllowance(ctx context.Context, bus Bus, state *MaintenanceState) (types.Currency, error) {
33+
contracts, err := bus.Contracts(ctx, api.ContractsOpts{})
34+
if err != nil {
35+
return types.Currency{}, err
36+
}
37+
3138
// find out how much we spent in the current period
3239
spent := currentPeriodSpending(contracts, state.Period())
3340

@@ -36,5 +43,5 @@ func remainingFunds(contracts []api.ContractMetadata, state *MaintenanceState) t
3643
if state.Allowance().Cmp(spent) > 0 {
3744
remaining = state.Allowance().Sub(spent)
3845
}
39-
return remaining
46+
return remaining, nil
4047
}

autopilot/contractor/contractor.go

Lines changed: 72 additions & 95 deletions
Large diffs are not rendered by default.

autopilot/contractor/hostfilter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (u *unusableHostsBreakdown) keysAndValues() []interface{} {
102102
// - recoverable -> can be usable in the contract set if it is refreshed/renewed
103103
// - refresh -> should be refreshed
104104
// - renew -> should be renewed
105-
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) {
105+
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) {
106106
usable = true
107107
if bh > contract.EndHeight() {
108108
reasons = append(reasons, errContractExpired.Error())

autopilot/contractor/ipfilter.go renamed to autopilot/contractor/hostset.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ var (
1212
)
1313

1414
type (
15-
ipFilter struct {
15+
hostSet struct {
1616
subnetToHostKey map[string]string
1717

1818
logger *zap.SugaredLogger
1919
}
2020
)
2121

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

3232
// check if we know about this subnet
3333
var knownHost string
3434
for _, subnet := range host.Subnets {
35-
if knownHost = f.subnetToHostKey[subnet]; knownHost != "" {
35+
if knownHost = hs.subnetToHostKey[subnet]; knownHost != "" {
3636
break
3737
}
3838
}
@@ -44,8 +44,8 @@ func (f *ipFilter) HasRedundantIP(host api.Host) bool {
4444
return false
4545
}
4646

47-
func (f *ipFilter) Add(host api.Host) {
47+
func (hs *hostSet) Add(host api.Host) {
4848
for _, subnet := range host.Subnets {
49-
f.subnetToHostKey[subnet] = host.PublicKey.String()
49+
hs.subnetToHostKey[subnet] = host.PublicKey.String()
5050
}
5151
}

autopilot/contractor/state.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ func (ctx *mCtx) WantedContracts() uint64 {
107107
return ctx.state.AP.Config.Contracts.Amount
108108
}
109109

110-
func (ctx *mCtx) IsContractInSet(contract api.Contract) bool {
111-
return ctx.state.ContractsConfig().IsContractInSet(contract)
110+
func (ctx *mCtx) Set() string {
111+
return ctx.state.ContractsConfig().Set
112112
}
113113

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

0 commit comments

Comments
 (0)