add in expiration logic (#31)
This commit is contained in:
parent
702aef3ce6
commit
80e7983d4e
@ -17,6 +17,7 @@ type Ticket struct {
|
|||||||
BlockHeight int64 `json:"blockheight"`
|
BlockHeight int64 `json:"blockheight"`
|
||||||
VoteBits uint16 `json:"votebits"`
|
VoteBits uint16 `json:"votebits"`
|
||||||
VotingKey string `json:"votingkey"`
|
VotingKey string `json:"votingkey"`
|
||||||
|
VSPFee float64 `json:"vspfee"`
|
||||||
Expiration int64 `json:"expiration"`
|
Expiration int64 `json:"expiration"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,3 +148,70 @@ func (vdb *VspDatabase) GetTicketByHash(hash string) (Ticket, error) {
|
|||||||
|
|
||||||
return ticket, err
|
return ticket, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (vdb *VspDatabase) UpdateVoteBits(hash string, voteBits uint16) error {
|
||||||
|
return vdb.db.View(func(tx *bolt.Tx) error {
|
||||||
|
ticketBkt := tx.Bucket(vspBktK).Bucket(ticketBktK)
|
||||||
|
key := []byte(hash)
|
||||||
|
|
||||||
|
ticketBytes := ticketBkt.Get(key)
|
||||||
|
if ticketBytes == nil {
|
||||||
|
return ErrNoTicketFound
|
||||||
|
}
|
||||||
|
|
||||||
|
var ticket Ticket
|
||||||
|
err := json.Unmarshal(ticketBytes, &ticket)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not unmarshal ticket: %v", err)
|
||||||
|
}
|
||||||
|
ticket.VoteBits = voteBits
|
||||||
|
|
||||||
|
ticketBytes, err = json.Marshal(ticket)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not marshal ticket: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete existing ticket
|
||||||
|
err = ticketBkt.Delete(key)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to delete ticket: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add updated ticket
|
||||||
|
return ticketBkt.Put(key, ticketBytes)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (vdb *VspDatabase) UpdateExpireAndFee(hash string, expiration int64, vspFee float64) error {
|
||||||
|
return vdb.db.View(func(tx *bolt.Tx) error {
|
||||||
|
ticketBkt := tx.Bucket(vspBktK).Bucket(ticketBktK)
|
||||||
|
key := []byte(hash)
|
||||||
|
|
||||||
|
ticketBytes := ticketBkt.Get(key)
|
||||||
|
if ticketBytes == nil {
|
||||||
|
return ErrNoTicketFound
|
||||||
|
}
|
||||||
|
|
||||||
|
var ticket Ticket
|
||||||
|
err := json.Unmarshal(ticketBytes, &ticket)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not unmarshal ticket: %v", err)
|
||||||
|
}
|
||||||
|
ticket.Expiration = expiration
|
||||||
|
ticket.VSPFee = vspFee
|
||||||
|
|
||||||
|
ticketBytes, err = json.Marshal(ticket)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not marshal ticket: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete existing ticket
|
||||||
|
err = ticketBkt.Delete(key)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to delete ticket: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add updated ticket
|
||||||
|
return ticketBkt.Put(key, ticketBytes)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@ -85,28 +85,41 @@ func feeAddress(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Check for existing response
|
||||||
// TODO - DB - deal with cached responses
|
ticket, err := db.GetTicketByHash(ticketHashStr)
|
||||||
ticket, err := db.GetTicketByHash(ticketHashStr)
|
if err != nil && !errors.Is(err, database.ErrNoTicketFound) {
|
||||||
if err != nil && !errors.Is(err, database.ErrNoTicketFound) {
|
c.AbortWithError(http.StatusInternalServerError, errors.New("database error"))
|
||||||
c.AbortWithError(http.StatusInternalServerError, errors.New("database error"))
|
return
|
||||||
return
|
}
|
||||||
}
|
if err == nil {
|
||||||
if err == nil {
|
// Ticket already exists
|
||||||
// TODO - deal with expiration
|
if signature == ticket.CommitmentSignature {
|
||||||
if signature == ticket.CommitmentSignature {
|
now := time.Now()
|
||||||
sendJSONResponse(feeAddressResponse{
|
expire := ticket.Expiration
|
||||||
Timestamp: time.Now().Unix(),
|
VSPFee := ticket.VSPFee
|
||||||
CommitmentSignature: ticket.CommitmentSignature,
|
if now.After(time.Unix(ticket.Expiration, 0)) {
|
||||||
FeeAddress: ticket.FeeAddress,
|
expire = now.Add(defaultFeeAddressExpiration).Unix()
|
||||||
Expiration: ticket.Expiration,
|
VSPFee = cfg.VSPFee
|
||||||
}, http.StatusOK, c)
|
|
||||||
return
|
err = db.UpdateExpireAndFee(ticketHashStr, expire, VSPFee)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, errors.New("database error"))
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c.AbortWithError(http.StatusBadRequest, errors.New("invalid signature"))
|
sendJSONResponse(feeAddressResponse{
|
||||||
|
Timestamp: now.Unix(),
|
||||||
|
Request: feeAddressRequest,
|
||||||
|
FeeAddress: ticket.FeeAddress,
|
||||||
|
Fee: VSPFee,
|
||||||
|
Expiration: expire,
|
||||||
|
}, c)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
*/
|
c.AbortWithError(http.StatusBadRequest, errors.New("invalid signature"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
walletClient, err := walletRPC()
|
walletClient, err := walletRPC()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -206,6 +219,7 @@ func feeAddress(c *gin.Context) {
|
|||||||
SDiff: blockHeader.SBits,
|
SDiff: blockHeader.SBits,
|
||||||
BlockHeight: int64(blockHeader.Height),
|
BlockHeight: int64(blockHeader.Height),
|
||||||
VoteBits: dcrutil.BlockValid,
|
VoteBits: dcrutil.BlockValid,
|
||||||
|
VSPFee: cfg.VSPFee,
|
||||||
Expiration: expire,
|
Expiration: expire,
|
||||||
// VotingKey: set during payfee
|
// VotingKey: set during payfee
|
||||||
}
|
}
|
||||||
@ -221,6 +235,7 @@ func feeAddress(c *gin.Context) {
|
|||||||
Timestamp: now.Unix(),
|
Timestamp: now.Unix(),
|
||||||
Request: feeAddressRequest,
|
Request: feeAddressRequest,
|
||||||
FeeAddress: newAddress,
|
FeeAddress: newAddress,
|
||||||
|
Fee: cfg.VSPFee,
|
||||||
Expiration: expire,
|
Expiration: expire,
|
||||||
}, c)
|
}, c)
|
||||||
}
|
}
|
||||||
@ -448,6 +463,12 @@ func setVoteBits(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = db.UpdateVoteBits(txHash.String(), voteBits)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, errors.New("database error"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: DB - error if given timestamp is older than any previous requests
|
// TODO: DB - error if given timestamp is older than any previous requests
|
||||||
|
|
||||||
// TODO: DB - store setvotebits receipt in log
|
// TODO: DB - store setvotebits receipt in log
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user