Skip to content

Commit

Permalink
contractor: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Sep 5, 2024
1 parent 072b34e commit 78c0743
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
50 changes: 33 additions & 17 deletions autopilot/contractor/contractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1335,26 +1335,14 @@ func performContractMaintenance(ctx *mCtx, alerter alerts.Alerter, bus Bus, chur
return false, fmt.Errorf("failed to fetch old contract set: %w", err)
}

// STEP 4: update contract set
// merge kept and formed contracts into new set
newSet := make([]api.ContractMetadata, 0, len(keptContracts)+len(formedContracts))
newSet = append(newSet, keptContracts...)
newSet = append(newSet, formedContracts...)
var newSetIDs []types.FileContractID
for _, contract := range newSet {
newSetIDs = append(newSetIDs, contract.ID)
}
inNewSet := make(map[types.FileContractID]struct{})
for _, c := range newSet {
inNewSet[c.ID] = struct{}{}
}
var toRemove []types.FileContractID
for _, c := range oldSet {
if _, exists := inNewSet[c.ID]; !exists {
toRemove = append(toRemove, c.ID)
}
}
if err := bus.UpdateContractSet(ctx, ctx.ContractSet(), newSetIDs, toRemove); err != nil {
return false, fmt.Errorf("failed to update contract set: %w", err)

// STEP 4: update contract set
if err := updateContractSet(ctx, bus, oldSet, newSet); err != nil {
return false, err
}

// STEP 5: perform minor maintenance such as cleanups and broadcasting
Expand All @@ -1366,3 +1354,31 @@ func performContractMaintenance(ctx *mCtx, alerter alerts.Alerter, bus Bus, chur
// STEP 6: log changes and register alerts
return computeContractSetChanged(ctx, alerter, bus, churn, logger, oldSet, newSet, churnReasons)
}

func updateContractSet(ctx *mCtx, bus Bus, oldSet, newSet []api.ContractMetadata) error {
var newSetIDs []types.FileContractID
for _, contract := range newSet {
newSetIDs = append(newSetIDs, contract.ID)
}
inOldSet := make(map[types.FileContractID]struct{})
for _, c := range oldSet {
inOldSet[c.ID] = struct{}{}
}
var toAdd []types.FileContractID
for _, c := range newSet {
if _, ok := inOldSet[c.ID]; !ok {
toAdd = append(toAdd, c.ID)
}
// only keep contracts that are in the old but not the new set
delete(inOldSet, c.ID)
}

var toRemove []types.FileContractID
for id := range inOldSet {
toRemove = append(toRemove, id)
}
if err := bus.UpdateContractSet(ctx, ctx.ContractSet(), newSetIDs, toRemove); err != nil {
return fmt.Errorf("failed to update contract set: %w", err)
}
return nil
}
2 changes: 1 addition & 1 deletion bus/client/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (c *Client) ReleaseContract(ctx context.Context, contractID types.FileContr
return
}

// UpdateContractSet adds the given contracts to the given set.
// UpdateContractSet adds/removes the given contracts to/from the given set.
func (c *Client) UpdateContractSet(ctx context.Context, set string, toAdd, toRemove []types.FileContractID) (err error) {
err = c.c.WithContext(ctx).POST(fmt.Sprintf("/contracts/set/%s", set), api.ContractSetUpdateRequest{
ToAdd: toAdd,
Expand Down
2 changes: 1 addition & 1 deletion stores/sql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ type (

// UpdateContractSet adds/removes the provided contract ids to/from
// the contract set. The contract set is created in the process if
// it doesn't eist already.
// it doesn't exist already.
UpdateContractSet(ctx context.Context, name string, toAdd, toRemove []types.FileContractID) error

// Setting returns the setting with the given key from the database.
Expand Down

0 comments on commit 78c0743

Please sign in to comment.