Skip to content

Commit

Permalink
accounts: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Aug 26, 2024
1 parent 0ca6c9c commit f6a04a7
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
4 changes: 4 additions & 0 deletions api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type (
// an account and the balance reported by a host.
Drift *big.Int `json:"drift"`

// Owner is the owner of the account which is responsible for funding
// it.
Owner string `json:"owner"`

// RequiresSync indicates whether an account needs to be synced with the
// host before it can be used again.
RequiresSync bool `json:"requiresSync"`
Expand Down
3 changes: 0 additions & 3 deletions bus/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1714,9 +1714,6 @@ func (b *Bus) accountsHandlerGET(jc jape.Context) {
var owner string
if jc.DecodeForm("owner", &owner) != nil {
return
} else if owner == "" {
jc.Error(errors.New("owner is required"), http.StatusBadRequest)
return
}
accounts, err := b.accounts.Accounts(jc.Request.Context(), owner)
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion cmd/renterd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func defaultConfig() config.Config {
Worker: config.Worker{
Enabled: true,

ID: "worker",
ID: "",
AccountsRefillInterval: defaultAccountRefillInterval,
ContractLockTimeout: 30 * time.Second,
BusFlushInterval: 5 * time.Second,
Expand Down Expand Up @@ -132,6 +132,15 @@ func defaultConfig() config.Config {
}
}

func assertWorkerID(cfg *config.Config) error {
if cfg.Bus.RemoteAddr != "" && cfg.Worker.ID == "" {
return errors.New("a unique worker ID must be set in a cluster setup")
} else if cfg.Worker.ID == "" {
cfg.Worker.ID = "worker"
}
return nil
}

// loadConfig creates a default config and overrides it with the contents of the
// YAML file (specified by the RENTERD_CONFIG_FILE), CLI flags, and environment
// variables, in that order.
Expand All @@ -141,6 +150,11 @@ func loadConfig() (cfg config.Config, network *consensus.Network, genesis types.
parseCLIFlags(&cfg)
parseEnvironmentVariables(&cfg)

// check worker id
if err = assertWorkerID(&cfg); err != nil {
return
}

// check network
switch cfg.Network {
case "anagami":
Expand Down
2 changes: 2 additions & 0 deletions internal/test/e2e/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,8 @@ func TestEphemeralAccounts(t *testing.T) {
t.Fatal("wrong host")
} else if !acc.CleanShutdown {
t.Fatal("account should indicate a clean shutdown")
} else if acc.Owner != testWorkerCfg().ID {
t.Fatalf("wrong owner %v", acc.Owner)
}

// Check that the spending was recorded for the contract. The recorded
Expand Down
9 changes: 6 additions & 3 deletions internal/worker/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type (

type (
AccountMgr struct {
alerts alerts.Alerter
w AccountMgrWorker
dc DownloadContracts
cs ConsensusState
Expand Down Expand Up @@ -180,6 +181,7 @@ func (a *AccountMgr) account(hk types.PublicKey) *Account {
HostKey: hk,
Balance: big.NewInt(0),
Drift: big.NewInt(0),
Owner: a.owner,
RequiresSync: true, // force sync on new account
},
}
Expand Down Expand Up @@ -352,16 +354,16 @@ func (a *AccountMgr) refillAccount(ctx context.Context, contract api.ContractMet
// negative because we don't care if we have more money than
// expected.
if account.Drift.Cmp(maxNegDrift) < 0 {
// TODO: register alert
_ = newAccountRefillAlert(account.ID, contract, errMaxDriftExceeded,
alert := newAccountRefillAlert(account.ID, contract, errMaxDriftExceeded,
"accountID", account.ID.String(),
"hostKey", contract.HostKey.String(),
"balance", account.Balance.String(),
"drift", account.Drift.String(),
)
_ = a.alerts.RegisterAlert(a.shutdownCtx, alert)
return fmt.Errorf("not refilling account since host is potentially cheating: %w", errMaxDriftExceeded)
} else {
// TODO: dismiss alert on success
_ = a.alerts.DismissAlerts(a.shutdownCtx, alerts.IDForAccount(alertAccountRefillID, account.ID))
}

// check if a resync is needed
Expand Down Expand Up @@ -498,6 +500,7 @@ func (a *Account) convert() api.Account {
HostKey: a.acc.HostKey,
Balance: new(big.Int).Set(a.acc.Balance),
Drift: new(big.Int).Set(a.acc.Drift),
Owner: a.acc.Owner,
RequiresSync: a.acc.RequiresSync,
}
}
Expand Down
11 changes: 9 additions & 2 deletions stores/sql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ func AbortMultipartUpload(ctx context.Context, tx sql.Tx, bucket, key string, up
}

func Accounts(ctx context.Context, tx sql.Tx, owner string) ([]api.Account, error) {
rows, err := tx.Query(ctx, "SELECT account_id, clean_shutdown, host, balance, drift, requires_sync FROM ephemeral_accounts WHERE owner = ?", owner)
var whereExpr string
var args []any
if owner != "" {
whereExpr = "WHERE owner = ?"
args = append(args, owner)
}
rows, err := tx.Query(ctx, fmt.Sprintf("SELECT account_id, clean_shutdown, host, balance, drift, requires_sync, owner FROM ephemeral_accounts %s", whereExpr),
args...)
if err != nil {
return nil, fmt.Errorf("failed to fetch accounts: %w", err)
}
Expand All @@ -107,7 +114,7 @@ func Accounts(ctx context.Context, tx sql.Tx, owner string) ([]api.Account, erro
var accounts []api.Account
for rows.Next() {
a := api.Account{Balance: new(big.Int), Drift: new(big.Int)} // init big.Int
if err := rows.Scan((*PublicKey)(&a.ID), &a.CleanShutdown, (*PublicKey)(&a.HostKey), (*BigInt)(a.Balance), (*BigInt)(a.Drift), &a.RequiresSync); err != nil {
if err := rows.Scan((*PublicKey)(&a.ID), &a.CleanShutdown, (*PublicKey)(&a.HostKey), (*BigInt)(a.Balance), (*BigInt)(a.Drift), &a.RequiresSync, &a.Owner); err != nil {
return nil, fmt.Errorf("failed to scan account: %w", err)
}
accounts = append(accounts, a)
Expand Down
4 changes: 4 additions & 0 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,10 @@ func (w *Worker) stateHandlerGET(jc jape.Context) {

// New returns an HTTP handler that serves the worker API.
func New(cfg config.Worker, masterKey [32]byte, b Bus, l *zap.Logger) (*Worker, error) {
if cfg.ID == "" {
return nil, errors.New("worker ID cannot be empty")
}

l = l.Named("worker").Named(cfg.ID)

if cfg.ContractLockTimeout == 0 {
Expand Down

0 comments on commit f6a04a7

Please sign in to comment.