add setvotebits api (#18)
This commit is contained in:
parent
7c5ab7ebae
commit
72ba2ce3b1
64
methods.go
64
methods.go
@ -347,6 +347,70 @@ func PayFee2(ctx context.Context, ticketHash *chainhash.Hash, votingWIF *dcrutil
|
||||
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) {
|
||||
dec := json.NewDecoder(c.Request.Body)
|
||||
|
||||
|
||||
13
responses.go
13
responses.go
@ -37,6 +37,19 @@ type payFeeResponse struct {
|
||||
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 {
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
TicketHash string `json:"ticketHash"`
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user