Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for downloading from hosts which are gouging as long as they are not download gouging #1170

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions internal/test/e2e/gouging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io"
"testing"
"time"

Expand Down Expand Up @@ -142,3 +143,64 @@ func TestHostMinVersion(t *testing.T) {
return nil
})
}

func TestDownloadGouging(t *testing.T) {
if testing.Short() {
t.SkipNow()
}

// create a new test cluster
cluster := newTestCluster(t, testClusterOptions{
hosts: int(test.AutopilotConfig.Contracts.Amount),
logger: newTestLoggerCustom(zapcore.ErrorLevel),
})
defer cluster.Shutdown()

cfg := test.AutopilotConfig.Contracts
b := cluster.Bus
w := cluster.Worker
tt := cluster.tt

// build a hosts map
hostsMap := make(map[string]*Host)
for _, h := range cluster.hosts {
hostsMap[h.PublicKey().String()] = h
}

// upload and download some data, asserting we have a working contract set
data := make([]byte, rhpv2.SectorSize/12)
tt.OKAll(frand.Read(data))

// upload some data
path := fmt.Sprintf("data_%v", len(data))
tt.OKAll(w.UploadObject(context.Background(), bytes.NewReader(data), api.DefaultBucketName, path, api.UploadObjectOptions{}))

// update the gouging settings to cause hosts to be gouging but not download gouging
gs := test.GougingSettings
gs.MaxContractPrice = types.NewCurrency64(1)
if err := b.UpdateSetting(context.Background(), api.SettingGouging, gs); err != nil {
t.Fatal(err)
}

// wait for hosts to drop out of the set
tt.Retry(100, 100*time.Millisecond, func() error {
contracts, err := b.Contracts(context.Background(), api.ContractsOpts{ContractSet: cfg.Set})
tt.OK(err)
if len(contracts) > 0 {
return fmt.Errorf("still got contracts in set")
}
return nil
})

// download the data
tt.OK(w.DownloadObject(context.Background(), io.Discard, api.DefaultBucketName, path, api.DownloadObjectOptions{}))

// update the gouging settings to cause hosts to be download gouging
gs.MaxDownloadPrice = types.NewCurrency64(1)
if err := b.UpdateSetting(context.Background(), api.SettingGouging, gs); err != nil {
t.Fatal(err)
}

// downloading should fail now
tt.FailAll(w.DownloadObject(context.Background(), io.Discard, api.DefaultBucketName, path, api.DownloadObjectOptions{}))
}
17 changes: 9 additions & 8 deletions worker/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func (h *host) DownloadSector(ctx context.Context, w io.Writer, root types.Hash2
if err != nil {
return err
}
if breakdown := gc.Check(nil, &hpt); breakdown.Gouging() {
return fmt.Errorf("%w: %v", errPriceTableGouging, breakdown)
if breakdown := gc.Check(nil, &hpt); breakdown.DownloadErr != "" {
return fmt.Errorf("%w: %v", errPriceTableGouging, breakdown.DownloadErr)
}

// return errBalanceInsufficient if balance insufficient
Expand Down Expand Up @@ -235,26 +235,27 @@ func (h *host) FundAccount(ctx context.Context, balance types.Currency, rev *typ
}

// check whether we have money left in the contract
if pt.FundAccountCost.Cmp(rev.ValidRenterPayout()) >= 0 {
return fmt.Errorf("insufficient funds to fund account: %v <= %v", rev.ValidRenterPayout(), pt.FundAccountCost)
cost := types.NewCurrency64(1)
ChrisSchinnerl marked this conversation as resolved.
Show resolved Hide resolved
if cost.Cmp(rev.ValidRenterPayout()) >= 0 {
return fmt.Errorf("insufficient funds to fund account: %v <= %v", rev.ValidRenterPayout(), cost)
}
availableFunds := rev.ValidRenterPayout().Sub(pt.FundAccountCost)
availableFunds := rev.ValidRenterPayout().Sub(cost)

// cap the deposit amount by the money that's left in the contract
if deposit.Cmp(availableFunds) > 0 {
deposit = availableFunds
}

// create the payment
amount := deposit.Add(pt.FundAccountCost)
amount := deposit.Add(cost)
payment, err := payByContract(rev, amount, rhpv3.Account{}, h.renterKey) // no account needed for funding
if err != nil {
return err
}

// fund the account
if err := RPCFundAccount(ctx, t, &payment, h.acc.id, pt.UID); err != nil {
return fmt.Errorf("failed to fund account with %v (excluding cost %v);%w", deposit, pt.FundAccountCost, err)
return fmt.Errorf("failed to fund account with %v (excluding cost %v);%w", deposit, cost, err)
}

// record the spend
Expand All @@ -277,7 +278,7 @@ func (h *host) SyncAccount(ctx context.Context, rev *types.FileContractRevision)
return h.acc.WithSync(ctx, func() (types.Currency, error) {
var balance types.Currency
err := h.transportPool.withTransportV3(ctx, h.hk, h.siamuxAddr, func(ctx context.Context, t *transportV3) error {
payment, err := payByContract(rev, pt.AccountBalanceCost, h.acc.id, h.renterKey)
payment, err := payByContract(rev, types.NewCurrency64(1), h.acc.id, h.renterKey)
if err != nil {
return err
}
Expand Down
Loading