Skip to content

Commit

Permalink
worker: avoid fetching contract set contracts separately
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan authored and ChrisSchinnerl committed Jul 29, 2024
1 parent 67cf299 commit d163099
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
10 changes: 10 additions & 0 deletions api/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,13 @@ func (c Contract) RemainingCollateral() types.Currency {
}
return c.Revision.MissedHostPayout().Sub(c.ContractPrice)
}

// InSet returns whether the contract is in the given set.
func (cm ContractMetadata) InSet(set string) bool {
for _, s := range cm.ContractSets {
if s == set {
return true
}
}
return false
}
17 changes: 7 additions & 10 deletions autopilot/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,14 @@ func (a *accounts) refillWorkerAccounts(ctx context.Context, w Worker) {
return
}

// fetch all contract set contracts
contractSetContracts, err := a.c.Contracts(ctx, api.ContractsOpts{ContractSet: cfg.Config.Contracts.Set})
if err != nil {
a.l.Errorw(fmt.Sprintf("failed to fetch contract set contracts: %v", err))
return
}

// build a map of contract set contracts
// filter all contract set contracts
var contractSetContracts []api.ContractMetadata
inContractSet := make(map[types.FileContractID]struct{})
for _, contract := range contractSetContracts {
inContractSet[contract.ID] = struct{}{}
for _, c := range contracts {
if c.InSet(cfg.Config.Contracts.Set) {
contractSetContracts = append(contractSetContracts, c)
inContractSet[c.ID] = struct{}{}
}
}

// refill accounts in separate goroutines
Expand Down
10 changes: 6 additions & 4 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,10 +786,12 @@ func (w *worker) slabMigrateHandler(jc jape.Context) {
return
}

// fetch upload contracts
ulContracts, err := w.bus.Contracts(ctx, api.ContractsOpts{ContractSet: up.ContractSet})
if jc.Check("couldn't fetch contracts from bus", err) != nil {
return
// filter upload contracts
var ulContracts []api.ContractMetadata
for _, c := range dlContracts {
if c.InSet(up.ContractSet) {
ulContracts = append(ulContracts, c)
}
}

// migrate the slab
Expand Down

0 comments on commit d163099

Please sign in to comment.