Skip to content

Commit

Permalink
add /alltickets admin api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
itswisdomagain committed Aug 23, 2019
1 parent 5f42c09 commit f98ce53
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
57 changes: 57 additions & 0 deletions controllers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ func (controller *MainController) API(c web.C, r *http.Request) *system.APIRespo
switch r.Method {
case "GET":
switch command {
case "alltickets":
data, code, response, err = controller.APITickets(c, r)
case "getpurchaseinfo":
data, code, response, err = controller.APIPurchaseInfo(c, r)
case "stats":
Expand Down Expand Up @@ -522,6 +524,61 @@ func (controller *MainController) APIPurchaseInfo(c web.C,
return purchaseInfo, codes.OK, "purchaseinfo successfully retrieved", nil
}

// APIPurchaseInfo fetches and returns the user's info or an error
func (controller *MainController) APITickets(c web.C,
r *http.Request) ([]*poolapi.Ticket, codes.Code, string, error) {
// todo: should probably restrict this endpoint to admin IPs only

dbMap := controller.GetDbMap(c)
msaUsers, err := models.GetAllCurrentMultiSigScripts(dbMap)
if err != nil {
log.Errorf("models.GetAllCurrentMultiSigScripts error: %v", err)
return nil, codes.Internal, "get tickets error", errors.New("unexpected db error")
}

start := time.Now()
tickets := make([]*poolapi.Ticket, 0)

for _, user := range msaUsers {
// Get P2SH Address
multisig, err := dcrutil.DecodeAddress(user.MultiSigAddress)
if err != nil {
log.Warnf("Invalid address %v in database: %v", user.MultiSigAddress, err)
continue
}

spui, err := controller.StakepooldServers.StakePoolUserInfo(multisig.String())
if err != nil {
log.Errorf("RPC StakePoolUserInfo failed: %v", err)
return nil, codes.Internal, "get tickets error", errors.New("unexpected rpc error")
}

// If the user has tickets, get their info
if spui != nil && len(spui.Tickets) > 0 {
for _, ticket := range spui.Tickets {
tickets = append(tickets, &poolapi.Ticket{
UserId: user.Id,
MultiSigAddress: user.MultiSigAddress,
Hash: ticket.Ticket,
BlockHeight: ticket.TicketHeight,
Status: ticket.Status,
})
}
} else {
tickets = append(tickets, &poolapi.Ticket{
UserId: user.Id,
MultiSigAddress: user.MultiSigAddress,
Hash: "no ticket for user",
})
}
}

log.Debugf(":: StakePoolUserInfo (for %d users) execution time: %v",
len(msaUsers), time.Since(start))

return tickets, codes.OK, "tickets successfully retrieved", nil
}

// APIStats is an API version of the stats page
func (controller *MainController) APIStats(c web.C,
r *http.Request) (*poolapi.Stats, codes.Code, string, error) {
Expand Down
2 changes: 1 addition & 1 deletion models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func UpdateUserByID(dbMap *gorp.DbMap, id int64, multiSigAddr string,

func GetAllCurrentMultiSigScripts(dbMap *gorp.DbMap) ([]User, error) {
var multiSigs []User
_, err := dbMap.Select(&multiSigs, "SELECT MultiSigScript, HeightRegistered FROM Users WHERE MultiSigAddress <> ''")
_, err := dbMap.Select(&multiSigs, "SELECT UserId, MultiSigAddress, MultiSigScript, HeightRegistered FROM Users WHERE MultiSigAddress <> ''")
if err != nil {
return nil, err
}
Expand Down
8 changes: 8 additions & 0 deletions poolapi/apijson.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ type PurchaseInfo struct {
VoteBitsVersion uint32 `json:"VoteBitsVersion"`
}

type Ticket struct {
UserId int64 `json:"UserId"`
MultiSigAddress string `json:"MultiSigAddress"`
Hash string `json:"Hash"`
Status string `json:"Status"`
BlockHeight uint32 `json:"BlockHeight"`
}

type Stats struct {
AllMempoolTix uint32 `json:"AllMempoolTix"`
APIVersionsSupported []int `json:"APIVersionsSupported"`
Expand Down

0 comments on commit f98ce53

Please sign in to comment.