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

Add Support for Price Pinning #316

Merged
merged 20 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
138 changes: 69 additions & 69 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"go.sia.tech/hostd/host/contracts"
"go.sia.tech/hostd/host/metrics"
"go.sia.tech/hostd/host/settings"
"go.sia.tech/hostd/host/settings/pin"
"go.sia.tech/hostd/host/storage"
"go.sia.tech/hostd/rhp"
"go.sia.tech/hostd/wallet"
Expand Down Expand Up @@ -46,6 +47,12 @@ type (
UpdateDDNS(force bool) error
}

// PinnedSettings updates and retrieves the host's pinned settings
PinnedSettings interface {
Update(context.Context, pin.PinnedSettings) error
Pinned(context.Context) pin.PinnedSettings
}

// A MetricManager retrieves metrics related to the host
MetricManager interface {
// PeriodMetrics returns metrics for n periods starting at start.
Expand Down Expand Up @@ -149,6 +156,7 @@ type (
wallet Wallet
metrics MetricManager
settings Settings
pinned PinnedSettings
sessions RHPSessionReporter

volumeJobs volumeJobs
Expand All @@ -157,92 +165,84 @@ type (
)

// NewServer initializes the API
func NewServer(name string, hostKey types.PublicKey, a Alerts, wh WebHooks, g Syncer, chain ChainManager, tp TPool, cm ContractManager, am AccountManager, vm VolumeManager, rsr RHPSessionReporter, m MetricManager, s Settings, w Wallet, log *zap.Logger) http.Handler {
api := &api{
func NewServer(name string, hostKey types.PublicKey, opts ...ServerOption) http.Handler {
a := &api{
hostKey: hostKey,
name: name,

alerts: a,
webhooks: wh,
syncer: g,
chain: chain,
tpool: tp,
contracts: cm,
accounts: am,
volumes: vm,
metrics: m,
settings: s,
wallet: w,
sessions: rsr,
log: log,

checks: integrityCheckJobs{
contracts: cm,
checks: make(map[types.FileContractID]IntegrityCheckResult),
},
volumeJobs: volumeJobs{
volumes: vm,
jobs: make(map[int64]context.CancelFunc),
},
log: zap.NewNop(),
}
for _, opt := range opts {
opt(a)
}
a.checks = integrityCheckJobs{
contracts: a.contracts,
checks: make(map[types.FileContractID]IntegrityCheckResult),
}
a.volumeJobs = volumeJobs{
volumes: a.volumes,
jobs: make(map[int64]context.CancelFunc),
}

return jape.Mux(map[string]jape.Handler{
// state endpoints
"GET /state/host": api.handleGETHostState,
"GET /state/consensus": api.handleGETConsensusState,
"GET /state/host": a.handleGETHostState,
"GET /state/consensus": a.handleGETConsensusState,
// gateway endpoints
"GET /syncer/address": api.handleGETSyncerAddr,
"GET /syncer/peers": api.handleGETSyncerPeers,
"PUT /syncer/peers": api.handlePUTSyncerPeer,
"DELETE /syncer/peers/:address": api.handleDeleteSyncerPeer,
"GET /syncer/address": a.handleGETSyncerAddr,
"GET /syncer/peers": a.handleGETSyncerPeers,
"PUT /syncer/peers": a.handlePUTSyncerPeer,
"DELETE /syncer/peers/:address": a.handleDeleteSyncerPeer,
// alerts endpoints
"GET /alerts": api.handleGETAlerts,
"POST /alerts/dismiss": api.handlePOSTAlertsDismiss,
"GET /alerts": a.handleGETAlerts,
"POST /alerts/dismiss": a.handlePOSTAlertsDismiss,
// settings endpoints
"GET /settings": api.handleGETSettings,
"PATCH /settings": api.handlePATCHSettings,
"POST /settings/announce": api.handlePOSTAnnounce,
"PUT /settings/ddns/update": api.handlePUTDDNSUpdate,
"GET /settings": a.handleGETSettings,
"PATCH /settings": a.handlePATCHSettings,
"POST /settings/announce": a.handlePOSTAnnounce,
"PUT /settings/ddns/update": a.handlePUTDDNSUpdate,
"GET /settings/pinned": a.handleGETPinnedSettings,
"PUT /settings/pinned": a.handlePUTPinnedSettings,
// metrics endpoints
"GET /metrics": api.handleGETMetrics,
"GET /metrics/:period": api.handleGETPeriodMetrics,
"GET /metrics": a.handleGETMetrics,
"GET /metrics/:period": a.handleGETPeriodMetrics,
// contract endpoints
"POST /contracts": api.handlePostContracts,
"GET /contracts/:id": api.handleGETContract,
"GET /contracts/:id/integrity": api.handleGETContractCheck,
"PUT /contracts/:id/integrity": api.handlePUTContractCheck,
"DELETE /contracts/:id/integrity": api.handleDeleteContractCheck,
"POST /contracts": a.handlePostContracts,
"GET /contracts/:id": a.handleGETContract,
"GET /contracts/:id/integrity": a.handleGETContractCheck,
"PUT /contracts/:id/integrity": a.handlePUTContractCheck,
"DELETE /contracts/:id/integrity": a.handleDeleteContractCheck,
// account endpoints
"GET /accounts": api.handleGETAccounts,
"GET /accounts/:account/funding": api.handleGETAccountFunding,
"GET /accounts": a.handleGETAccounts,
"GET /accounts/:account/funding": a.handleGETAccountFunding,
// sector endpoints
"DELETE /sectors/:root": api.handleDeleteSector,
"GET /sectors/:root/verify": api.handleGETVerifySector,
"DELETE /sectors/:root": a.handleDeleteSector,
"GET /sectors/:root/verify": a.handleGETVerifySector,
// volume endpoints
"GET /volumes": api.handleGETVolumes,
"POST /volumes": api.handlePOSTVolume,
"GET /volumes/:id": api.handleGETVolume,
"PUT /volumes/:id": api.handlePUTVolume,
"DELETE /volumes/:id": api.handleDeleteVolume,
"DELETE /volumes/:id/cancel": api.handleDELETEVolumeCancelOp,
"PUT /volumes/:id/resize": api.handlePUTVolumeResize,
"GET /volumes": a.handleGETVolumes,
"POST /volumes": a.handlePOSTVolume,
"GET /volumes/:id": a.handleGETVolume,
"PUT /volumes/:id": a.handlePUTVolume,
"DELETE /volumes/:id": a.handleDeleteVolume,
"DELETE /volumes/:id/cancel": a.handleDELETEVolumeCancelOp,
"PUT /volumes/:id/resize": a.handlePUTVolumeResize,
// session endpoints
"GET /sessions": api.handleGETSessions,
"GET /sessions/subscribe": api.handleGETSessionsSubscribe,
"GET /sessions": a.handleGETSessions,
"GET /sessions/subscribe": a.handleGETSessionsSubscribe,
// tpool endpoints
"GET /tpool/fee": api.handleGETTPoolFee,
"GET /tpool/fee": a.handleGETTPoolFee,
// wallet endpoints
"GET /wallet": api.handleGETWallet,
"GET /wallet/transactions": api.handleGETWalletTransactions,
"GET /wallet/pending": api.handleGETWalletPending,
"POST /wallet/send": api.handlePOSTWalletSend,
"GET /wallet": a.handleGETWallet,
"GET /wallet/transactions": a.handleGETWalletTransactions,
"GET /wallet/pending": a.handleGETWalletPending,
"POST /wallet/send": a.handlePOSTWalletSend,
// system endpoints
"GET /system/dir": api.handleGETSystemDir,
"PUT /system/dir": api.handlePUTSystemDir,
"GET /system/dir": a.handleGETSystemDir,
"PUT /system/dir": a.handlePUTSystemDir,
// webhook endpoints
"GET /webhooks": api.handleGETWebhooks,
"POST /webhooks": api.handlePOSTWebhooks,
"PUT /webhooks/:id": api.handlePUTWebhooks,
"POST /webhooks/:id/test": api.handlePOSTWebhooksTest,
"DELETE /webhooks/:id": api.handleDELETEWebhooks,
"GET /webhooks": a.handleGETWebhooks,
"POST /webhooks": a.handlePOSTWebhooks,
"PUT /webhooks/:id": a.handlePUTWebhooks,
"POST /webhooks/:id/test": a.handlePOSTWebhooksTest,
"DELETE /webhooks/:id": a.handleDELETEWebhooks,
})
}
47 changes: 35 additions & 12 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"go.sia.tech/hostd/host/contracts"
"go.sia.tech/hostd/host/metrics"
"go.sia.tech/hostd/host/settings"
"go.sia.tech/hostd/host/settings/pin"
"go.sia.tech/hostd/host/storage"
"go.sia.tech/hostd/internal/disk"
"go.sia.tech/hostd/internal/prometheus"
Expand All @@ -39,7 +40,7 @@ func (a *api) checkServerError(c jape.Context, context string, err error) bool {
return err == nil
}

func (a *api) writeResponse(c jape.Context, code int, resp any) {
func (a *api) writeResponse(c jape.Context, resp any) {
var responseFormat string
if err := c.DecodeForm("response", &responseFormat); err != nil {
return
Expand Down Expand Up @@ -74,7 +75,7 @@ func (a *api) handleGETHostState(c jape.Context) {
return
}

a.writeResponse(c, http.StatusOK, HostState{
a.writeResponse(c, HostState{
Name: a.name,
PublicKey: a.hostKey,
WalletAddress: a.wallet.Address(),
Expand All @@ -91,14 +92,14 @@ func (a *api) handleGETHostState(c jape.Context) {
}

func (a *api) handleGETConsensusState(c jape.Context) {
a.writeResponse(c, http.StatusOK, ConsensusState{
a.writeResponse(c, ConsensusState{
Synced: a.chain.Synced(),
ChainIndex: a.chain.TipState().Index,
})
}

func (a *api) handleGETSyncerAddr(c jape.Context) {
a.writeResponse(c, http.StatusOK, SyncerAddrResp(a.syncer.Address()))
a.writeResponse(c, SyncerAddrResp(a.syncer.Address()))
}

func (a *api) handleGETSyncerPeers(c jape.Context) {
Expand All @@ -110,7 +111,7 @@ func (a *api) handleGETSyncerPeers(c jape.Context) {
Version: peer.Version,
}
}
a.writeResponse(c, http.StatusOK, PeerResp(peers))
a.writeResponse(c, PeerResp(peers))
}

func (a *api) handlePUTSyncerPeer(c jape.Context) {
Expand All @@ -132,7 +133,7 @@ func (a *api) handleDeleteSyncerPeer(c jape.Context) {
}

func (a *api) handleGETAlerts(c jape.Context) {
a.writeResponse(c, http.StatusOK, AlertResp(a.alerts.Active()))
a.writeResponse(c, AlertResp(a.alerts.Active()))
}

func (a *api) handlePOSTAlertsDismiss(c jape.Context) {
Expand All @@ -153,7 +154,7 @@ func (a *api) handlePOSTAnnounce(c jape.Context) {

func (a *api) handleGETSettings(c jape.Context) {
hs := HostSettings(a.settings.Settings())
a.writeResponse(c, http.StatusOK, hs)
a.writeResponse(c, hs)
}

func (a *api) handlePATCHSettings(c jape.Context) {
Expand Down Expand Up @@ -199,6 +200,28 @@ func (a *api) handlePATCHSettings(c jape.Context) {
c.Encode(a.settings.Settings())
}

func (a *api) handleGETPinnedSettings(c jape.Context) {
if a.pinned == nil {
c.Error(errors.New("pinned settings disabled"), http.StatusNotFound)
return
}
c.Encode(a.pinned.Pinned(c.Request.Context()))
}

func (a *api) handlePUTPinnedSettings(c jape.Context) {
if a.pinned == nil {
c.Error(errors.New("pinned settings disabled"), http.StatusNotFound)
return
}

var req pin.PinnedSettings
if err := c.Decode(&req); err != nil {
return
}

a.checkServerError(c, "failed to update pinned settings", a.pinned.Update(c.Request.Context(), req))
}

func (a *api) handlePUTDDNSUpdate(c jape.Context) {
err := a.settings.UpdateDDNS(true)
a.checkServerError(c, "failed to update dynamic DNS", err)
Expand All @@ -217,7 +240,7 @@ func (a *api) handleGETMetrics(c jape.Context) {
return
}

a.writeResponse(c, http.StatusOK, Metrics(metrics))
a.writeResponse(c, Metrics(metrics))
}

func (a *api) handleGETPeriodMetrics(c jape.Context) {
Expand Down Expand Up @@ -370,7 +393,7 @@ func (a *api) handleGETWallet(c jape.Context) {
if !a.checkServerError(c, "failed to get wallet", err) {
return
}
a.writeResponse(c, http.StatusOK, WalletResponse{
a.writeResponse(c, WalletResponse{
ScanHeight: a.wallet.ScanHeight(),
Address: a.wallet.Address(),
Spendable: spendable,
Expand All @@ -387,15 +410,15 @@ func (a *api) handleGETWalletTransactions(c jape.Context) {
return
}

a.writeResponse(c, http.StatusOK, WalletTransactionsResp(transactions))
a.writeResponse(c, WalletTransactionsResp(transactions))
}

func (a *api) handleGETWalletPending(c jape.Context) {
pending, err := a.wallet.UnconfirmedTransactions()
if !a.checkServerError(c, "failed to get wallet pending", err) {
return
}
a.writeResponse(c, http.StatusOK, WalletPendingResp(pending))
a.writeResponse(c, WalletPendingResp(pending))
}

func (a *api) handlePOSTWalletSend(c jape.Context) {
Expand Down Expand Up @@ -534,7 +557,7 @@ func (a *api) handlePUTSystemDir(c jape.Context) {
}

func (a *api) handleGETTPoolFee(c jape.Context) {
a.writeResponse(c, http.StatusOK, TPoolResp(a.tpool.RecommendedFee()))
a.writeResponse(c, TPoolResp(a.tpool.RecommendedFee()))
}

func (a *api) handleGETAccounts(c jape.Context) {
Expand Down
Loading
Loading