Skip to content

Commit

Permalink
webapi: Don't export funcs/vars unnecessarily.
Browse files Browse the repository at this point in the history
  • Loading branch information
jholdstock committed Sep 13, 2023
1 parent b5ffecd commit 3967d9e
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 56 deletions.
4 changes: 2 additions & 2 deletions internal/webapi/addressgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func newAddressGenerator(xPub string, netParams *chaincfg.Params, lastUsedIdx ui
}, nil
}

// NextAddress increments the last used address counter and returns a new
// nextAddress increments the last used address counter and returns a new
// address. It will skip any address index which causes an ErrInvalidChild.
// Not safe for concurrent access.
func (m *addressGenerator) NextAddress() (string, uint32, error) {
func (m *addressGenerator) nextAddress() (string, uint32, error) {
var key *hdkeychain.ExtendedKey
var err error

Expand Down
40 changes: 20 additions & 20 deletions internal/webapi/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"github.com/gorilla/sessions"
)

// WalletStatus describes the current status of a single voting wallet. This is
// walletStatus describes the current status of a single voting wallet. This is
// used by the admin.html template, and also serialized to JSON for the
// /admin/status endpoint.
type WalletStatus struct {
type walletStatus struct {
Connected bool `json:"connected"`
InfoError bool `json:"infoerror"`
DaemonConnected bool `json:"daemonconnected"`
Expand All @@ -28,10 +28,10 @@ type WalletStatus struct {
BestBlockHeight int64 `json:"bestblockheight"`
}

// DcrdStatus describes the current status of the local instance of dcrd used by
// dcrdStatus describes the current status of the local instance of dcrd used by
// vspd. This is used by the admin.html template, and also serialized to JSON
// for the /admin/status endpoint.
type DcrdStatus struct {
type dcrdStatus struct {
Host string `json:"host"`
Connected bool `json:"connected"`
BestBlockError bool `json:"bestblockerror"`
Expand All @@ -48,9 +48,9 @@ type searchResult struct {
MaxVoteChanges int
}

func (s *Server) dcrdStatus(c *gin.Context) DcrdStatus {
func (s *server) dcrdStatus(c *gin.Context) dcrdStatus {
hostname := c.MustGet(dcrdHostKey).(string)
status := DcrdStatus{Host: hostname}
status := dcrdStatus{Host: hostname}

dcrdClient := c.MustGet(dcrdKey).(*rpc.DcrdRPC)
dcrdErr := c.MustGet(dcrdErrorKey)
Expand All @@ -73,13 +73,13 @@ func (s *Server) dcrdStatus(c *gin.Context) DcrdStatus {
return status
}

func (s *Server) walletStatus(c *gin.Context) map[string]WalletStatus {
func (s *server) walletStatus(c *gin.Context) map[string]walletStatus {
walletClients := c.MustGet(walletsKey).([]*rpc.WalletRPC)
failedWalletClients := c.MustGet(failedWalletsKey).([]string)

walletStatus := make(map[string]WalletStatus)
status := make(map[string]walletStatus)
for _, v := range walletClients {
ws := WalletStatus{Connected: true}
ws := walletStatus{Connected: true}

walletInfo, err := v.WalletInfo()
if err != nil {
Expand All @@ -100,18 +100,18 @@ func (s *Server) walletStatus(c *gin.Context) map[string]WalletStatus {
ws.BestBlockHeight = height
}

walletStatus[v.String()] = ws
status[v.String()] = ws
}
for _, v := range failedWalletClients {
ws := WalletStatus{Connected: false}
walletStatus[v] = ws
ws := walletStatus{Connected: false}
status[v] = ws
}
return walletStatus
return status
}

// statusJSON is the handler for "GET /admin/status". It returns a JSON object
// describing the current status of voting wallets.
func (s *Server) statusJSON(c *gin.Context) {
func (s *server) statusJSON(c *gin.Context) {
httpStatus := http.StatusOK

wallets := s.walletStatus(c)
Expand Down Expand Up @@ -143,7 +143,7 @@ func (s *Server) statusJSON(c *gin.Context) {
}

// adminPage is the handler for "GET /admin".
func (s *Server) adminPage(c *gin.Context) {
func (s *server) adminPage(c *gin.Context) {
c.HTML(http.StatusOK, "admin.html", gin.H{
"WebApiCache": s.cache.getData(),
"WebApiCfg": s.cfg,
Expand All @@ -154,7 +154,7 @@ func (s *Server) adminPage(c *gin.Context) {

// ticketSearch is the handler for "POST /admin/ticket". The hash param will be
// used to retrieve a ticket from the database.
func (s *Server) ticketSearch(c *gin.Context) {
func (s *server) ticketSearch(c *gin.Context) {
hash := c.PostForm("hash")

ticket, found, err := s.db.GetTicketByHash(hash)
Expand Down Expand Up @@ -227,7 +227,7 @@ func (s *Server) ticketSearch(c *gin.Context) {

// adminLogin is the handler for "POST /admin". If a valid password is provided,
// the current session will be authenticated as an admin.
func (s *Server) adminLogin(c *gin.Context) {
func (s *server) adminLogin(c *gin.Context) {
password := c.PostForm("password")

if password != s.cfg.AdminPass {
Expand All @@ -245,13 +245,13 @@ func (s *Server) adminLogin(c *gin.Context) {

// adminLogout is the handler for "POST /admin/logout". The current session will
// have its admin authentication removed.
func (s *Server) adminLogout(c *gin.Context) {
func (s *server) adminLogout(c *gin.Context) {
s.setAdminStatus(nil, c)
}

// downloadDatabaseBackup is the handler for "GET /backup". A binary
// representation of the whole database is generated and returned to the client.
func (s *Server) downloadDatabaseBackup(c *gin.Context) {
func (s *server) downloadDatabaseBackup(c *gin.Context) {
err := s.db.BackupDB(c.Writer)
if err != nil {
s.log.Errorf("Error backing up database: %v", err)
Expand All @@ -263,7 +263,7 @@ func (s *Server) downloadDatabaseBackup(c *gin.Context) {

// setAdminStatus stores the authentication status of the current session and
// redirects the client to GET /admin.
func (s *Server) setAdminStatus(admin any, c *gin.Context) {
func (s *server) setAdminStatus(admin any, c *gin.Context) {
session := c.MustGet(sessionKey).(*sessions.Session)
session.Values["admin"] = admin
err := session.Save(c.Request, c.Writer)
Expand Down
8 changes: 4 additions & 4 deletions internal/webapi/getfeeaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ var addrMtx sync.Mutex
// the last used address index in the database. In order to maintain consistency
// between the internal counter of address generator and the database, this func
// uses a mutex to ensure it is not run concurrently.
func (s *Server) getNewFeeAddress() (string, uint32, error) {
func (s *server) getNewFeeAddress() (string, uint32, error) {
addrMtx.Lock()
defer addrMtx.Unlock()

addr, idx, err := s.addrGen.NextAddress()
addr, idx, err := s.addrGen.nextAddress()
if err != nil {
return "", 0, err
}
Expand All @@ -43,7 +43,7 @@ func (s *Server) getNewFeeAddress() (string, uint32, error) {

// getCurrentFee returns the minimum fee amount a client should pay in order to
// register a ticket with the VSP at the current block height.
func (s *Server) getCurrentFee(dcrdClient *rpc.DcrdRPC) (dcrutil.Amount, error) {
func (s *server) getCurrentFee(dcrdClient *rpc.DcrdRPC) (dcrutil.Amount, error) {
bestBlock, err := dcrdClient.GetBestBlockHeader()
if err != nil {
return 0, err
Expand All @@ -70,7 +70,7 @@ func (s *Server) getCurrentFee(dcrdClient *rpc.DcrdRPC) (dcrutil.Amount, error)
}

// feeAddress is the handler for "POST /api/v3/feeaddress".
func (s *Server) feeAddress(c *gin.Context) {
func (s *server) feeAddress(c *gin.Context) {

const funcName = "feeAddress"

Expand Down
2 changes: 1 addition & 1 deletion internal/webapi/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func validateTicketHash(hash string) error {
// canTicketVote checks determines whether a ticket is able to vote at some
// point in the future by checking that it is currently either in the mempool,
// immature or live.
func canTicketVote(rawTx *dcrdtypes.TxRawResult, dcrdClient Node, netParams *chaincfg.Params) (bool, error) {
func canTicketVote(rawTx *dcrdtypes.TxRawResult, dcrdClient node, netParams *chaincfg.Params) (bool, error) {

// Tickets which have more than (TicketMaturity+TicketExpiry+1)
// confirmations are too old to vote.
Expand Down
2 changes: 1 addition & 1 deletion internal/webapi/homepage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/gin-gonic/gin"
)

func (s *Server) homepage(c *gin.Context) {
func (s *server) homepage(c *gin.Context) {
c.HTML(http.StatusOK, "homepage.html", gin.H{
"WebApiCache": s.cache.getData(),
"WebApiCfg": s.cfg,
Expand Down
14 changes: 7 additions & 7 deletions internal/webapi/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func rateLimit(limit rate.Limit, limitExceeded gin.HandlerFunc) gin.HandlerFunc
// withSession middleware adds a gorilla session to the request context for
// downstream handlers to make use of. Sessions are used by admin pages to
// maintain authentication status.
func (s *Server) withSession(store *sessions.CookieStore) gin.HandlerFunc {
func (s *server) withSession(store *sessions.CookieStore) gin.HandlerFunc {
return func(c *gin.Context) {
session, err := store.Get(c.Request, "vspd-session")
if err != nil {
Expand Down Expand Up @@ -95,7 +95,7 @@ func (s *Server) withSession(store *sessions.CookieStore) gin.HandlerFunc {

// requireAdmin will only allow the request to proceed if the current session is
// authenticated as an admin, otherwise it will render the login template.
func (s *Server) requireAdmin(c *gin.Context) {
func (s *server) requireAdmin(c *gin.Context) {
session := c.MustGet(sessionKey).(*sessions.Session)
admin := session.Values["admin"]

Expand All @@ -111,7 +111,7 @@ func (s *Server) requireAdmin(c *gin.Context) {

// withDcrdClient middleware adds a dcrd client to the request context for
// downstream handlers to make use of.
func (s *Server) withDcrdClient(dcrd rpc.DcrdConnect) gin.HandlerFunc {
func (s *server) withDcrdClient(dcrd rpc.DcrdConnect) gin.HandlerFunc {
return func(c *gin.Context) {
client, hostname, err := dcrd.Client()
// Don't handle the error here, add it to the context and let downstream
Expand All @@ -125,7 +125,7 @@ func (s *Server) withDcrdClient(dcrd rpc.DcrdConnect) gin.HandlerFunc {
// withWalletClients middleware attempts to add voting wallet clients to the
// request context for downstream handlers to make use of. Downstream handlers
// must handle the case where no wallet clients are connected.
func (s *Server) withWalletClients(wallets rpc.WalletConnect) gin.HandlerFunc {
func (s *server) withWalletClients(wallets rpc.WalletConnect) gin.HandlerFunc {
return func(c *gin.Context) {
clients, failedConnections := wallets.Clients()
if len(clients) == 0 {
Expand All @@ -151,7 +151,7 @@ func drainAndReplaceBody(req *http.Request) ([]byte, error) {
return reqBytes, nil
}

func (s *Server) vspMustBeOpen(c *gin.Context) {
func (s *server) vspMustBeOpen(c *gin.Context) {
if s.cfg.VspClosed {
s.sendError(types.ErrVspClosed, c)
return
Expand All @@ -163,7 +163,7 @@ func (s *Server) vspMustBeOpen(c *gin.Context) {
// Ticket hash, ticket hex, and parent hex are parsed from the request body and
// validated. They are broadcast to the network using SendRawTransaction if dcrd
// is not aware of them.
func (s *Server) broadcastTicket(c *gin.Context) {
func (s *server) broadcastTicket(c *gin.Context) {
const funcName = "broadcastTicket"

// Read request bytes.
Expand Down Expand Up @@ -304,7 +304,7 @@ func (s *Server) broadcastTicket(c *gin.Context) {
// does not contain the request body signed with the commitment address.
// Ticket information is added to the request context for downstream handlers to
// use.
func (s *Server) vspAuth(c *gin.Context) {
func (s *server) vspAuth(c *gin.Context) {
const funcName = "vspAuth"

// Read request bytes.
Expand Down
2 changes: 1 addition & 1 deletion internal/webapi/payfee.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// payFee is the handler for "POST /api/v3/payfee".
func (s *Server) payFee(c *gin.Context) {
func (s *server) payFee(c *gin.Context) {
const funcName = "payFee"

// Get values which have been added to context by middleware.
Expand Down
4 changes: 2 additions & 2 deletions internal/webapi/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ var (
slash = []byte("/")
)

// Recovery returns a middleware that recovers from any panics which occur in
// recovery returns a middleware that recovers from any panics which occur in
// request handlers. It logs the panic, a stack trace, and the full request
// details. It also ensure the client receives a 500 response rather than no
// response at all.
func Recovery(log slog.Logger) gin.HandlerFunc {
func recovery(log slog.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions internal/webapi/setaltsignaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ import (
)

// Ensure that Node is satisfied by *rpc.DcrdRPC.
var _ Node = (*rpc.DcrdRPC)(nil)
var _ node = (*rpc.DcrdRPC)(nil)

// Node is satisfied by *rpc.DcrdRPC and retrieves data from the blockchain.
type Node interface {
// node is satisfied by *rpc.DcrdRPC and retrieves data from the blockchain.
type node interface {
ExistsLiveTicket(ticketHash string) (bool, error)
GetRawTransaction(txHash string) (*dcrdtypes.TxRawResult, error)
}

// setAltSignAddr is the handler for "POST /api/v3/setaltsignaddr".
func (s *Server) setAltSignAddr(c *gin.Context) {
func (s *server) setAltSignAddr(c *gin.Context) {

const funcName = "setAltSignAddr"

// Get values which have been added to context by middleware.
dcrdClient := c.MustGet(dcrdKey).(Node)
dcrdClient := c.MustGet(dcrdKey).(node)
dcrdErr := c.MustGet(dcrdErrorKey)
if dcrdErr != nil {
s.log.Errorf("%s: Could not get dcrd client: %v", funcName, dcrdErr.(error))
Expand Down
6 changes: 3 additions & 3 deletions internal/webapi/setaltsignaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (
seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
feeXPub = "feexpub"
maxVoteChangeRecords = 3
api *Server
api *server
)

// randBytes returns a byte slice of size n filled with random bytes.
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestMain(m *testing.M) {
panic(fmt.Errorf("error opening test database: %w", err))
}

api = &Server{
api = &server{
cfg: cfg,
signPrivKey: signPrivKey,
db: db,
Expand Down Expand Up @@ -112,7 +112,7 @@ func randString(length int, charset string) string {
}

// Ensure that testNode satisfies Node.
var _ Node = (*testNode)(nil)
var _ node = (*testNode)(nil)

type testNode struct {
getRawTransaction *dcrdtypes.TxRawResult
Expand Down
2 changes: 1 addition & 1 deletion internal/webapi/setvotechoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// setVoteChoices is the handler for "POST /api/v3/setvotechoices".
func (s *Server) setVoteChoices(c *gin.Context) {
func (s *server) setVoteChoices(c *gin.Context) {
const funcName = "setVoteChoices"

// Get values which have been added to context by middleware.
Expand Down
2 changes: 1 addition & 1 deletion internal/webapi/ticketstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// ticketStatus is the handler for "POST /api/v3/ticketstatus".
func (s *Server) ticketStatus(c *gin.Context) {
func (s *server) ticketStatus(c *gin.Context) {
const funcName = "ticketStatus"

// Get values which have been added to context by middleware.
Expand Down
2 changes: 1 addition & 1 deletion internal/webapi/vspinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// vspInfo is the handler for "GET /api/v3/vspinfo".
func (s *Server) vspInfo(c *gin.Context) {
func (s *server) vspInfo(c *gin.Context) {
cachedStats := s.cache.getData()
s.sendJSONResponse(types.VspInfoResponse{
APIVersions: []int64{3},
Expand Down
Loading

0 comments on commit 3967d9e

Please sign in to comment.