start hooking up database (#17)

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

View File

@ -9,13 +9,14 @@ var (
testDb = "test.db" testDb = "test.db"
ticket = Ticket{ ticket = Ticket{
Hash: "Hash", Hash: "Hash",
CommitmentAddress: "Address",
CommitmentSignature: "CommitmentSignature", CommitmentSignature: "CommitmentSignature",
FeeAddress: "FeeAddress", FeeAddress: "FeeAddress",
Address: "Address",
SDiff: 1, SDiff: 1,
BlockHeight: 2, BlockHeight: 2,
VoteBits: 3, VoteBits: 3,
VotingKey: "VotingKey", VotingKey: "VotingKey",
Expiration: 4,
} }
db *VspDatabase db *VspDatabase
) )
@ -80,13 +81,14 @@ func testGetFeeAddressByTicketHash(t *testing.T) {
// Check ticket fields match expected. // Check ticket fields match expected.
if retrieved.Hash != ticket.Hash || if retrieved.Hash != ticket.Hash ||
retrieved.CommitmentAddress != ticket.CommitmentAddress ||
retrieved.CommitmentSignature != ticket.CommitmentSignature || retrieved.CommitmentSignature != ticket.CommitmentSignature ||
retrieved.FeeAddress != ticket.FeeAddress || retrieved.FeeAddress != ticket.FeeAddress ||
retrieved.Address != ticket.Address ||
retrieved.SDiff != ticket.SDiff || retrieved.SDiff != ticket.SDiff ||
retrieved.BlockHeight != ticket.BlockHeight || retrieved.BlockHeight != ticket.BlockHeight ||
retrieved.VoteBits != ticket.VoteBits || retrieved.VoteBits != ticket.VoteBits ||
retrieved.VotingKey != ticket.VotingKey { retrieved.VotingKey != ticket.VotingKey ||
retrieved.Expiration != ticket.Expiration {
t.Fatal("retrieved ticket value didnt match expected") t.Fatal("retrieved ticket value didnt match expected")
} }
@ -145,7 +147,7 @@ func testInsertFeeAddressVotingKey(t *testing.T) {
// Update values. // Update values.
newVotingKey := ticket.VotingKey + "2" newVotingKey := ticket.VotingKey + "2"
newVoteBits := ticket.VoteBits + 2 newVoteBits := ticket.VoteBits + 2
err = db.InsertFeeAddressVotingKey(ticket.Address, newVotingKey, newVoteBits) err = db.InsertFeeAddressVotingKey(ticket.CommitmentAddress, newVotingKey, newVoteBits)
if err != nil { if err != nil {
t.Fatalf("error updating votingkey and votebits: %v", err) t.Fatalf("error updating votingkey and votebits: %v", err)
} }

View File

@ -2,6 +2,7 @@ package database
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
bolt "go.etcd.io/bbolt" bolt "go.etcd.io/bbolt"
@ -10,14 +11,19 @@ import (
type Ticket struct { type Ticket struct {
Hash string `json:"hash"` Hash string `json:"hash"`
CommitmentSignature string `json:"commitmentsignature"` CommitmentSignature string `json:"commitmentsignature"`
CommitmentAddress string `json:"commitmentaddress"`
FeeAddress string `json:"feeaddress"` FeeAddress string `json:"feeaddress"`
Address string `json:"address"` SDiff float64 `json:"sdiff"`
SDiff int64 `json:"sdiff"`
BlockHeight int64 `json:"blockheight"` BlockHeight int64 `json:"blockheight"`
VoteBits uint16 `json:"votebits"` VoteBits uint16 `json:"votebits"`
VotingKey string `json:"votingkey"` VotingKey string `json:"votingkey"`
Expiration int64 `json:"expiration"`
} }
var (
ErrNoTicketFound = errors.New("no ticket found")
)
func (vdb *VspDatabase) InsertFeeAddress(ticket Ticket) error { func (vdb *VspDatabase) InsertFeeAddress(ticket Ticket) error {
return vdb.db.Update(func(tx *bolt.Tx) error { return vdb.db.Update(func(tx *bolt.Tx) error {
ticketBkt := tx.Bucket(vspBktK).Bucket(ticketBktK) ticketBkt := tx.Bucket(vspBktK).Bucket(ticketBktK)
@ -47,7 +53,7 @@ func (vdb *VspDatabase) InsertFeeAddressVotingKey(address, votingKey string, vot
return fmt.Errorf("could not unmarshal ticket: %v", err) return fmt.Errorf("could not unmarshal ticket: %v", err)
} }
if ticket.Address == address { if ticket.CommitmentAddress == address {
ticket.VotingKey = votingKey ticket.VotingKey = votingKey
ticket.VoteBits = voteBits ticket.VoteBits = voteBits
ticketBytes, err := json.Marshal(ticket) ticketBytes, err := json.Marshal(ticket)
@ -128,7 +134,7 @@ func (vdb *VspDatabase) GetFeeAddressByTicketHash(ticketHash string) (Ticket, er
ticketBytes := ticketBkt.Get([]byte(ticketHash)) ticketBytes := ticketBkt.Get([]byte(ticketHash))
if ticketBytes == nil { if ticketBytes == nil {
return fmt.Errorf("no ticket found with hash %s", ticketHash) return ErrNoTicketFound
} }
err := json.Unmarshal(ticketBytes, &ticket) err := json.Unmarshal(ticketBytes, &ticket)

View File

@ -21,6 +21,7 @@ import (
"github.com/decred/dcrd/txscript/v3" "github.com/decred/dcrd/txscript/v3"
"github.com/decred/dcrd/wire" "github.com/decred/dcrd/wire"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jholdstock/dcrvsp/database"
) )
const ( const (
@ -85,8 +86,28 @@ func feeAddress(c *gin.Context) {
return return
} }
// TODO: check db for cache response - if expired, reset expiration, but still /*
// use same feeaddress // TODO - DB - deal with cached responses
ticket, err := db.GetFeeAddressByTicketHash(ticketHashStr)
if err != nil && !errors.Is(err, database.ErrNoTicketFound) {
c.AbortWithError(http.StatusInternalServerError, errors.New("database error"))
return
}
if err == nil {
// TODO - deal with expiration
if signature == ticket.CommitmentSignature {
sendJSONResponse(feeAddressResponse{
Timestamp: time.Now().Unix(),
CommitmentSignature: ticket.CommitmentSignature,
FeeAddress: ticket.FeeAddress,
Expiration: ticket.Expiration,
}, http.StatusOK, c)
return
}
c.AbortWithError(http.StatusBadRequest, errors.New("invalid signature"))
return
}
*/
ctx := c.Request.Context() ctx := c.Request.Context()
@ -154,7 +175,6 @@ func feeAddress(c *gin.Context) {
c.AbortWithError(http.StatusInternalServerError, errors.New("RPC server error")) c.AbortWithError(http.StatusInternalServerError, errors.New("RPC server error"))
return return
} }
sDiff := blockHeader.SBits
var newAddress string var newAddress string
err = nodeConnection.Call(ctx, "getnewaddress", &newAddress, "fees") err = nodeConnection.Call(ctx, "getnewaddress", &newAddress, "fees")
@ -163,15 +183,33 @@ func feeAddress(c *gin.Context) {
return return
} }
// TODO: Insert into DB
_ = sDiff
now := time.Now() now := time.Now()
expire := now.Add(defaultFeeAddressExpiration).Unix()
dbTicket := database.Ticket{
Hash: txHash.String(),
CommitmentSignature: signature,
CommitmentAddress: addr.Address(),
FeeAddress: newAddress,
SDiff: blockHeader.SBits,
BlockHeight: int64(blockHeader.Height),
VoteBits: dcrutil.BlockValid,
Expiration: expire,
// VotingKey: set during payfee
}
// TODO: Insert into DB
err = db.InsertFeeAddress(dbTicket)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, errors.New("database error"))
return
}
sendJSONResponse(feeAddressResponse{ sendJSONResponse(feeAddressResponse{
Timestamp: now.Unix(), Timestamp: now.Unix(),
Request: feeAddressRequest, Request: feeAddressRequest,
FeeAddress: newAddress, FeeAddress: newAddress,
Expiration: now.Add(defaultFeeAddressExpiration).Unix(), Expiration: expire,
}, c) }, c)
} }
@ -246,7 +284,7 @@ findAddress:
c.AbortWithError(http.StatusInternalServerError, errors.New("database error")) c.AbortWithError(http.StatusInternalServerError, errors.New("database error"))
return return
} }
voteAddr, err := dcrutil.DecodeAddress(feeEntry.Address, cfg.netParams) voteAddr, err := dcrutil.DecodeAddress(feeEntry.CommitmentAddress, cfg.netParams)
if err != nil { if err != nil {
fmt.Printf("PayFee: DecodeAddress: %v", err) fmt.Printf("PayFee: DecodeAddress: %v", err)
c.AbortWithError(http.StatusInternalServerError, errors.New("database error")) c.AbortWithError(http.StatusInternalServerError, errors.New("database error"))