diff --git a/stores/sql/main.go b/stores/sql/main.go index 9de379b74..221cda24c 100644 --- a/stores/sql/main.go +++ b/stores/sql/main.go @@ -1853,47 +1853,50 @@ func Slab(ctx context.Context, tx sql.Tx, key object.EncryptionKey) (object.Slab defer hostStmt.Close() for i, sectorID := range sectorIDs { - // contracts - rows, err := stmt.Query(ctx, sectorID) + slab.Shards[i].Contracts = make(map[types.PublicKey][]types.FileContractID) + + // hosts + rows, err = hostStmt.Query(ctx, sectorID) if err != nil { - return object.Slab{}, fmt.Errorf("failed to fetch contracts: %w", err) + return object.Slab{}, fmt.Errorf("failed to fetch hosts: %w", err) } if err := func() error { defer rows.Close() - slab.Shards[i].Contracts = make(map[types.PublicKey][]types.FileContractID) for rows.Next() { var pk types.PublicKey - var fcid types.FileContractID - if err := rows.Scan((*PublicKey)(&pk), (*FileContractID)(&fcid)); err != nil { - return fmt.Errorf("failed to scan contract: %w", err) + if err := rows.Scan((*PublicKey)(&pk)); err != nil { + return fmt.Errorf("failed to scan host: %w", err) } if _, exists := slab.Shards[i].Contracts[pk]; !exists { slab.Shards[i].Contracts[pk] = []types.FileContractID{} } - if fcid != (types.FileContractID{}) { - slab.Shards[i].Contracts[pk] = append(slab.Shards[i].Contracts[pk], fcid) - } } return nil }(); err != nil { return object.Slab{}, err } - // hosts - rows, err = hostStmt.Query(ctx, sectorID) + // contracts + rows, err := stmt.Query(ctx, sectorID) if err != nil { - return object.Slab{}, fmt.Errorf("failed to fetch hosts: %w", err) + return object.Slab{}, fmt.Errorf("failed to fetch contracts: %w", err) } if err := func() error { defer rows.Close() for rows.Next() { var pk types.PublicKey - if err := rows.Scan((*PublicKey)(&pk)); err != nil { - return fmt.Errorf("failed to scan host: %w", err) + var fcid types.FileContractID + if err := rows.Scan((*PublicKey)(&pk), (*FileContractID)(&fcid)); err != nil { + return fmt.Errorf("failed to scan contract: %w", err) + } + if _, exists := slab.Shards[i].Contracts[pk]; !exists { + slab.Shards[i].Contracts[pk] = []types.FileContractID{} + } + if fcid != (types.FileContractID{}) { + slab.Shards[i].Contracts[pk] = append(slab.Shards[i].Contracts[pk], fcid) } - slab.Shards[i].Contracts[pk] = []types.FileContractID{} } return nil }(); err != nil {