- Fee tx status is now tracked using a dedicated field, with values none/received/broadcast/confirmed/error. - Fee tx hex and hash are now both set in /payfee. The absense of txhash is no longer used to determine if a fee tx has been broadcast or not. - setvotechoices can no longer be called before a fee is received. - Remove `binding:required` from response types. It has no effect on responses, it is only needed on request types which are validated by gin.
156 lines
4.2 KiB
Go
156 lines
4.2 KiB
Go
package database
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func exampleTicket() Ticket {
|
|
return Ticket{
|
|
Hash: "Hash",
|
|
CommitmentAddress: "Address",
|
|
FeeAddressIndex: 12345,
|
|
FeeAddress: "FeeAddress",
|
|
FeeAmount: 10000000,
|
|
FeeExpiration: 4,
|
|
Confirmed: false,
|
|
VoteChoices: map[string]string{"AgendaID": "Choice"},
|
|
VotingWIF: "VotingKey",
|
|
FeeTxHex: "FeeTransction",
|
|
FeeTxHash: "",
|
|
FeeTxStatus: FeeBroadcast,
|
|
}
|
|
}
|
|
|
|
func testInsertNewTicket(t *testing.T) {
|
|
// Insert a ticket into the database.
|
|
ticket := exampleTicket()
|
|
err := db.InsertNewTicket(ticket)
|
|
if err != nil {
|
|
t.Fatalf("error storing ticket in database: %v", err)
|
|
}
|
|
|
|
// Inserting a ticket with different fee address but same hash should fail.
|
|
ticket2 := exampleTicket()
|
|
ticket2.FeeAddress = ticket.FeeAddress + "2"
|
|
err = db.InsertNewTicket(ticket)
|
|
if err == nil {
|
|
t.Fatal("expected an error inserting ticket with duplicate hash")
|
|
}
|
|
|
|
// Inserting a ticket with different hash but same fee address should fail.
|
|
ticket3 := exampleTicket()
|
|
ticket3.FeeAddress = ticket.Hash + "2"
|
|
err = db.InsertNewTicket(ticket)
|
|
if err == nil {
|
|
t.Fatal("expected an error inserting ticket with duplicate fee addr")
|
|
}
|
|
|
|
// Inserting a ticket with empty hash should fail.
|
|
ticket.Hash = ""
|
|
err = db.InsertNewTicket(ticket)
|
|
if err == nil {
|
|
t.Fatal("expected an error inserting ticket with no hash")
|
|
}
|
|
}
|
|
|
|
func testGetTicketByHash(t *testing.T) {
|
|
ticket := exampleTicket()
|
|
// Insert a ticket into the database.
|
|
err := db.InsertNewTicket(ticket)
|
|
if err != nil {
|
|
t.Fatalf("error storing ticket in database: %v", err)
|
|
}
|
|
|
|
// Retrieve ticket from database.
|
|
retrieved, found, err := db.GetTicketByHash(ticket.Hash)
|
|
if err != nil {
|
|
t.Fatalf("error retrieving ticket by ticket hash: %v", err)
|
|
}
|
|
if !found {
|
|
t.Fatal("expected found==true")
|
|
}
|
|
|
|
// Check ticket fields match expected.
|
|
if retrieved.Hash != ticket.Hash ||
|
|
retrieved.CommitmentAddress != ticket.CommitmentAddress ||
|
|
retrieved.FeeAddressIndex != ticket.FeeAddressIndex ||
|
|
retrieved.FeeAddress != ticket.FeeAddress ||
|
|
retrieved.FeeAmount != ticket.FeeAmount ||
|
|
retrieved.FeeExpiration != ticket.FeeExpiration ||
|
|
retrieved.Confirmed != ticket.Confirmed ||
|
|
!reflect.DeepEqual(retrieved.VoteChoices, ticket.VoteChoices) ||
|
|
retrieved.VotingWIF != ticket.VotingWIF ||
|
|
retrieved.FeeTxHex != ticket.FeeTxHex ||
|
|
retrieved.FeeTxHash != ticket.FeeTxHash ||
|
|
retrieved.FeeTxStatus != ticket.FeeTxStatus {
|
|
t.Fatal("retrieved ticket value didnt match expected")
|
|
}
|
|
|
|
// Check found==false when requesting a non-existent ticket.
|
|
_, found, err = db.GetTicketByHash("Not a real ticket hash")
|
|
if err != nil {
|
|
t.Fatalf("error retrieving ticket by ticket hash: %v", err)
|
|
}
|
|
if found {
|
|
t.Fatal("expected found==false")
|
|
}
|
|
}
|
|
|
|
func testUpdateTicket(t *testing.T) {
|
|
ticket := exampleTicket()
|
|
// Insert a ticket into the database.
|
|
err := db.InsertNewTicket(ticket)
|
|
if err != nil {
|
|
t.Fatalf("error storing ticket in database: %v", err)
|
|
}
|
|
|
|
// Update ticket with new values
|
|
ticket.FeeAmount = ticket.FeeAmount + 1
|
|
ticket.FeeExpiration = ticket.FeeExpiration + 1
|
|
err = db.UpdateTicket(ticket)
|
|
if err != nil {
|
|
t.Fatalf("error updating ticket: %v", err)
|
|
}
|
|
|
|
// Retrieve ticket from database.
|
|
retrieved, found, err := db.GetTicketByHash(ticket.Hash)
|
|
if err != nil {
|
|
t.Fatalf("error retrieving ticket by ticket hash: %v", err)
|
|
}
|
|
if !found {
|
|
t.Fatal("expected found==true")
|
|
}
|
|
|
|
if ticket.FeeAmount != retrieved.FeeAmount ||
|
|
ticket.FeeExpiration != retrieved.FeeExpiration {
|
|
t.Fatal("retrieved ticket value didnt match expected")
|
|
}
|
|
|
|
// Updating a non-existent ticket should fail.
|
|
ticket.Hash = "doesnt exist"
|
|
err = db.UpdateTicket(ticket)
|
|
if err == nil {
|
|
t.Fatal("expected an error updating a ticket with non-existent hash")
|
|
}
|
|
}
|
|
|
|
func testTicketFeeExpired(t *testing.T) {
|
|
ticket := exampleTicket()
|
|
|
|
now := time.Now()
|
|
hourBefore := now.Add(-time.Hour).Unix()
|
|
hourAfter := now.Add(time.Hour).Unix()
|
|
|
|
ticket.FeeExpiration = hourAfter
|
|
if ticket.FeeExpired() {
|
|
t.Fatal("expected ticket not to be expired")
|
|
}
|
|
|
|
ticket.FeeExpiration = hourBefore
|
|
if !ticket.FeeExpired() {
|
|
t.Fatal("expected ticket to be expired")
|
|
}
|
|
}
|