From d163099f6cf901ae4ff56d7e6e39091d6e7dce09 Mon Sep 17 00:00:00 2001 From: PJ Date: Sun, 21 Jul 2024 14:22:27 +0200 Subject: [PATCH] worker: avoid fetching contract set contracts separately --- api/contract.go | 10 ++++++++++ autopilot/accounts.go | 17 +++++++---------- worker/worker.go | 10 ++++++---- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/api/contract.go b/api/contract.go index 92775b268..66fdd81d3 100644 --- a/api/contract.go +++ b/api/contract.go @@ -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 +} diff --git a/autopilot/accounts.go b/autopilot/accounts.go index 63f77bf88..a1422d69a 100644 --- a/autopilot/accounts.go +++ b/autopilot/accounts.go @@ -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 diff --git a/worker/worker.go b/worker/worker.go index 392cbd6c2..a847af894 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -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