add setvotebits api (#18)

This commit is contained in:
David Hill 2020-05-15 10:33:20 -05:00 committed by GitHub
parent 7c5ab7ebae
commit 72ba2ce3b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 0 deletions

View File

@ -347,6 +347,70 @@ func PayFee2(ctx context.Context, ticketHash *chainhash.Hash, votingWIF *dcrutil
return res, nil return res, nil
} }
func setVoteBits(c *gin.Context) {
dec := json.NewDecoder(c.Request.Body)
var setVoteBitsRequest SetVoteBitsRequest
err := dec.Decode(&setVoteBitsRequest)
if err != nil {
c.AbortWithError(http.StatusBadRequest, errors.New("invalid json"))
return
}
// ticketHash
ticketHashStr := setVoteBitsRequest.TicketHash
if len(ticketHashStr) != chainhash.MaxHashStringSize {
c.AbortWithError(http.StatusBadRequest, errors.New("invalid ticket hash"))
return
}
txHash, err := chainhash.NewHashFromStr(ticketHashStr)
if err != nil {
c.AbortWithError(http.StatusBadRequest, errors.New("invalid ticket hash"))
return
}
// signature - sanity check signature is in base64 encoding
signature := setVoteBitsRequest.Signature
if _, err = base64.StdEncoding.DecodeString(signature); err != nil {
c.AbortWithError(http.StatusBadRequest, errors.New("invalid signature"))
return
}
// votebits
voteBits := setVoteBitsRequest.VoteBits
if !isValidVoteBits(cfg.netParams.Params, currentVoteVersion(cfg.netParams.Params), voteBits) {
c.AbortWithError(http.StatusBadRequest, errors.New("invalid votebits"))
return
}
// TODO: DB - get commitment address from db (stored from feeaddress)
var addr string
// verify message
ctx := c.Request.Context()
message := fmt.Sprintf("vsp v3 setvotebits %d %s %d", setVoteBitsRequest.Timestamp, txHash, voteBits)
var valid bool
err = nodeConnection.Call(ctx, "verifymessage", &valid, addr, signature, message)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, errors.New("RPC server error"))
return
}
if !valid {
c.AbortWithError(http.StatusBadRequest, errors.New("message did not pass verification"))
return
}
// TODO: DB - error if given timestamp is older than any previous requests
// TODO: DB - store setvotebits receipt in log
sendJSONResponse(setVoteBitsResponse{
Timestamp: time.Now().Unix(),
Request: setVoteBitsRequest,
VoteBits: voteBits,
}, c)
}
func ticketStatus(c *gin.Context) { func ticketStatus(c *gin.Context) {
dec := json.NewDecoder(c.Request.Body) dec := json.NewDecoder(c.Request.Body)

View File

@ -37,6 +37,19 @@ type payFeeResponse struct {
Request PayFeeRequest `json:"request"` Request PayFeeRequest `json:"request"`
} }
type SetVoteBitsRequest struct {
Timestamp int64 `json:"timestamp"`
TicketHash string `json:"ticketHash"`
Signature string `json:"commitmentSignature"`
VoteBits uint16 `json:"voteBits"`
}
type setVoteBitsResponse struct {
Timestamp int64 `json:"timestamp"`
Request SetVoteBitsRequest `json:"request"`
VoteBits uint16 `json:"voteBits"`
}
type TicketStatusRequest struct { type TicketStatusRequest struct {
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
TicketHash string `json:"ticketHash"` TicketHash string `json:"ticketHash"`

View File

@ -38,6 +38,7 @@ func newRouter(releaseMode bool) *gin.Engine {
api.POST("/feeaddress", feeAddress) api.POST("/feeaddress", feeAddress)
api.GET("/pubkey", pubKey) api.GET("/pubkey", pubKey)
api.POST("/payfee", payFee) api.POST("/payfee", payFee)
api.POST("/setvotebits", setVoteBits)
api.POST("/ticketstatus", ticketStatus) api.POST("/ticketstatus", ticketStatus)
} }