Skip to content

Commit

Permalink
bus: better gouging checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Aug 30, 2024
1 parent aa2e912 commit 67594c2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
12 changes: 4 additions & 8 deletions bus/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,15 @@ func (b *Bus) accountsFundHandler(jc jape.Context) {
return
}

// check price table for gouging
gp, err := b.gougingParams(jc.Request.Context())
if jc.Check("failed to fetch gouging params", err) != nil {
return
}
gc := gouging.NewChecker(gp.GougingSettings, gp.ConsensusState, gp.TransactionFee, nil, nil)
if jc.Check("gouging check failed", gc.CheckUnusedDefaults(pt.HostPriceTable)) != nil {
// check only the FundAccountCost
if types.NewCurrency64(1).Cmp(pt.FundAccountCost) < 0 {
jc.Error(fmt.Errorf("%w: host is gouging on FundAccountCost", gouging.ErrPriceTableGouging), http.StatusServiceUnavailable)
return
}

// cap the deposit by what's left in the contract
deposit := req.Amount
cost := types.NewCurrency64(1)
cost := pt.FundAccountCost
availableFunds := rev.ValidRenterPayout().Sub(cost)
if deposit.Cmp(availableFunds) > 0 {
deposit = availableFunds
Expand Down
6 changes: 3 additions & 3 deletions internal/rhp/v3/rhp.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ func (c *Client) Renew(ctx context.Context, gc gouging.Checker, rev types.FileCo
return
}

func (c *Client) SyncAccount(ctx context.Context, rev *types.FileContractRevision, hk types.PublicKey, siamuxAddr string, accID rhpv3.Account, pt rhpv3.SettingsID, rk types.PrivateKey) (balance types.Currency, _ error) {
func (c *Client) SyncAccount(ctx context.Context, rev *types.FileContractRevision, hk types.PublicKey, siamuxAddr string, accID rhpv3.Account, pt rhpv3.HostPriceTable, rk types.PrivateKey) (balance types.Currency, _ error) {
return balance, c.tpool.withTransport(ctx, hk, siamuxAddr, func(ctx context.Context, t *transportV3) error {
payment, err := payByContract(rev, types.NewCurrency64(1), accID, rk)
payment, err := payByContract(rev, pt.AccountBalanceCost, accID, rk)
if err != nil {
return err
}
balance, err = rpcAccountBalance(ctx, t, &payment, accID, pt)
balance, err = rpcAccountBalance(ctx, t, &payment, accID, pt.UID)
return err
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/test/e2e/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,7 @@ func TestEphemeralAccountSync(t *testing.T) {
if len(accounts) != 1 || accounts[0].ID != acc.ID {
t.Fatal("account should exist")
} else if accounts[0].CleanShutdown || !accounts[0].RequiresSync {
t.Fatalf("account shouldn't be marked as clean shutdown or not require a sync, got %v", accounts[0].CleanShutdown, accounts[0].RequiresSync)
t.Fatal("account shouldn't be marked as clean shutdown or not require a sync, got", accounts[0].CleanShutdown, accounts[0].RequiresSync)
}

// assert account was funded
Expand Down
11 changes: 4 additions & 7 deletions worker/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,13 @@ func (h *host) SyncAccount(ctx context.Context, rev *types.FileContractRevision)
return err
}

// check only the unused defaults
gc, err := GougingCheckerFromContext(ctx, false)
if err != nil {
return err
} else if err := gc.CheckUnusedDefaults(pt.HostPriceTable); err != nil {
return fmt.Errorf("%w: %v", gouging.ErrPriceTableGouging, err)
// check only the AccountBalanceCost
if types.NewCurrency64(1).Cmp(pt.AccountBalanceCost) < 0 {
return fmt.Errorf("%w: host is gouging on AccountBalanceCost", gouging.ErrPriceTableGouging)
}

return h.acc.WithSync(func() (types.Currency, error) {
return h.client.SyncAccount(ctx, rev, h.hk, h.siamuxAddr, h.acc.ID(), pt.UID, h.renterKey)
return h.client.SyncAccount(ctx, rev, h.hk, h.siamuxAddr, h.acc.ID(), pt.HostPriceTable, h.renterKey)
})
}

Expand Down

0 comments on commit 67594c2

Please sign in to comment.