Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db,pm: avoid redeeming expired tickets #1598

Merged
merged 2 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions common/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,15 @@ func InitDB(dbPath string) (*DB, error) {
d.insertWinningTicket = stmt

// Select earliest ticket
stmt, err = db.Prepare("SELECT sender, recipient, faceValue, winProb, senderNonce, recipientRand, recipientRandHash, sig, creationRound, creationRoundBlockHash, paramsExpirationBlock FROM ticketQueue WHERE sender=? AND redeemedAt IS NULL AND txHash IS NULL ORDER BY createdAt ASC LIMIT 1")
stmt, err = db.Prepare("SELECT sender, recipient, faceValue, winProb, senderNonce, recipientRand, recipientRandHash, sig, creationRound, creationRoundBlockHash, paramsExpirationBlock FROM ticketQueue WHERE sender=? AND creationRound >= ? AND redeemedAt IS NULL AND txHash IS NULL ORDER BY createdAt ASC LIMIT 1")
if err != nil {
glog.Error("Unable to prepare selectEarliestWinningTicket ", err)
d.Close()
return nil, err
}
d.selectEarliestWinningTicket = stmt

stmt, err = db.Prepare("SELECT count(sig) FROM ticketQueue WHERE sender=? AND redeemedAt IS NULL AND txHash IS NULL")
stmt, err = db.Prepare("SELECT count(sig) FROM ticketQueue WHERE sender=? AND creationRound >= ? AND redeemedAt IS NULL AND txHash IS NULL")
if err != nil {
glog.Error("Unable to prepare winningTicketCount ", err)
d.Close()
Expand Down Expand Up @@ -746,10 +746,10 @@ func (db *DB) RemoveWinningTicket(ticket *pm.SignedTicket) error {
return nil
}

// SelectEarliestWinningTicket selects the earliest stored winning ticket for a 'sender'
// which is not yet redeemed
func (db *DB) SelectEarliestWinningTicket(sender ethcommon.Address) (*pm.SignedTicket, error) {
row := db.selectEarliestWinningTicket.QueryRow(sender.Hex())
// SelectEarliestWinningTicket selects the earliest stored winning ticket for a 'sender' that is not expired and not yet redeemed
func (db *DB) SelectEarliestWinningTicket(sender ethcommon.Address, minCreationRound int64) (*pm.SignedTicket, error) {

row := db.selectEarliestWinningTicket.QueryRow(sender.Hex(), minCreationRound)
var (
senderString string
recipient string
Expand Down Expand Up @@ -789,8 +789,8 @@ func (db *DB) SelectEarliestWinningTicket(sender ethcommon.Address) (*pm.SignedT
}

// WinningTicketCount returns the amount of non-redeemed winning tickets for a 'sender'
func (db *DB) WinningTicketCount(sender ethcommon.Address) (int, error) {
row := db.winningTicketCount.QueryRow(sender.Hex())
func (db *DB) WinningTicketCount(sender ethcommon.Address, minCreationRound int64) (int, error) {
row := db.winningTicketCount.QueryRow(sender.Hex(), minCreationRound)
var count64 int64
if err := row.Scan(&count64); err != nil {
if err.Error() != "sql: no rows in result set" {
Expand Down
39 changes: 29 additions & 10 deletions common/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ func TestWinningTicketCount(t *testing.T) {

_, ticket, sig, recipientRand := defaultWinningTicket(t)
ticket.Sender = sender
count, err := dbh.WinningTicketCount(sender)
count, err := dbh.WinningTicketCount(sender, ticket.CreationRound)
assert.Nil(err)
assert.Equal(count, 0)

Expand All @@ -770,7 +770,7 @@ func TestWinningTicketCount(t *testing.T) {
})
require.Nil(err)

count, err = dbh.WinningTicketCount(sender)
count, err = dbh.WinningTicketCount(sender, ticket.CreationRound)
assert.Nil(err)
assert.Equal(count, 1)

Expand All @@ -789,9 +789,14 @@ func TestWinningTicketCount(t *testing.T) {
}, pm.RandHash())
require.Nil(err)

count, err = dbh.WinningTicketCount(sender)
count, err = dbh.WinningTicketCount(sender, ticket.CreationRound)
assert.Nil(err)
assert.Equal(count, 1)

// all tickets are expirted, should return 0
count, err = dbh.WinningTicketCount(sender, ticket.CreationRound+100)
assert.Nil(err)
assert.Equal(count, 0)
}

func TestInsertWinningTicket_GivenValidInputs_InsertsOneRowCorrectly(t *testing.T) {
Expand Down Expand Up @@ -947,6 +952,8 @@ func TestSelectEarliestWinningTicket(t *testing.T) {
_, ticket, sig, recipientRand := defaultWinningTicket(t)
ticket.Sender = ethcommon.HexToAddress("charizard")

defaultCreationRound := ticket.CreationRound

signedTicket0 := &pm.SignedTicket{
Ticket: ticket,
Sig: sig,
Expand All @@ -964,36 +971,48 @@ func TestSelectEarliestWinningTicket(t *testing.T) {
require.Nil(err)

// no tickets found
earliest, err := dbh.SelectEarliestWinningTicket(ethcommon.HexToAddress("charizard"))
earliest, err := dbh.SelectEarliestWinningTicket(ethcommon.HexToAddress("charizard"), defaultCreationRound)
assert.Nil(err)
assert.Nil(earliest)

err = dbh.StoreWinningTicket(signedTicket0)
require.Nil(err)
earliest, err = dbh.SelectEarliestWinningTicket(ethcommon.HexToAddress("charizard"))
earliest, err = dbh.SelectEarliestWinningTicket(ethcommon.HexToAddress("charizard"), defaultCreationRound)
assert.Nil(err)
assert.Equal(signedTicket0, earliest)

// test ticket expired
earliest, err = dbh.SelectEarliestWinningTicket(ethcommon.HexToAddress("charizard"), defaultCreationRound+100)
assert.Nil(err)
assert.Nil(earliest)

_, ticket, sig, recipientRand = defaultWinningTicket(t)
ticket.Sender = ethcommon.HexToAddress("charizard")
signedTicket2 := &pm.SignedTicket{
Ticket: ticket,
Sig: pm.RandBytes(32),
RecipientRand: new(big.Int).SetBytes(pm.RandBytes(32)),
}
signedTicket2.CreationRound = ticket.CreationRound + 100

err = dbh.StoreWinningTicket(signedTicket2)
require.Nil(err)

earliest, err = dbh.SelectEarliestWinningTicket(ethcommon.HexToAddress("charizard"))
earliest, err = dbh.SelectEarliestWinningTicket(ethcommon.HexToAddress("charizard"), defaultCreationRound)
assert.Nil(err)
assert.Equal(earliest, signedTicket0)

// Test excluding expired tickets
earliest, err = dbh.SelectEarliestWinningTicket(ethcommon.HexToAddress("charizard"), defaultCreationRound+100)
assert.Nil(err)
assert.Equal(earliest, signedTicket2)

// Test excluding submitted tickets
err = dbh.MarkWinningTicketRedeemed(signedTicket0, pm.RandHash())
require.Nil(err)
earliest, err = dbh.SelectEarliestWinningTicket(ethcommon.HexToAddress("charizard"))
earliest, err = dbh.SelectEarliestWinningTicket(ethcommon.HexToAddress("charizard"), defaultCreationRound)
assert.Equal(earliest, signedTicket2)

}

func TestMarkWinningTicketRedeemed_GivenNilTicket_ReturnsError(t *testing.T) {
Expand Down Expand Up @@ -1089,7 +1108,7 @@ func TestRemoveWinningTicket(t *testing.T) {
require.Nil(err)

// confirm ticket is added correctly
count, err := dbh.WinningTicketCount(ticket.Sender)
count, err := dbh.WinningTicketCount(ticket.Sender, ticket.CreationRound)
require.Nil(err)
require.Equal(count, 1)

Expand All @@ -1099,14 +1118,14 @@ func TestRemoveWinningTicket(t *testing.T) {
err = dbh.RemoveWinningTicket(&signedTicketDup)
assert.NoError(err)
// confirm ticket is not removed
count, err = dbh.WinningTicketCount(signedTicket.Sender)
count, err = dbh.WinningTicketCount(signedTicket.Sender, ticket.CreationRound)
require.Nil(err)
assert.Equal(count, 1)

err = dbh.RemoveWinningTicket(signedTicket)
assert.Nil(err)
// confirm ticket is removed correctly
count, _ = dbh.WinningTicketCount(ticket.Sender)
count, _ = dbh.WinningTicketCount(ticket.Sender, ticket.CreationRound)
require.Equal(count, 0)
}

Expand Down
24 changes: 13 additions & 11 deletions pm/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"math/big"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event"
"github.com/golang/glog"
)

const ticketValidityPeriod = 2

// RedeemableEmitter is an interface that describes methods for
// emitting redeemable tickets
type RedeemableEmitter interface {
Expand All @@ -32,9 +33,7 @@ type redemption struct {
//
// Based off of: https://github.com/lightningnetwork/lnd/blob/master/htlcswitch/queue.go
type ticketQueue struct {
// blockSub returns a subscription to receive the last seen block number
blockSub func(chan<- *big.Int) event.Subscription

tm TimeManager
// redeemable is a channel that a queue consumer will receive
// redeemable tickets on as a sender's max float becomes
// sufficient to cover the face value of tickets
Expand All @@ -46,11 +45,11 @@ type ticketQueue struct {
quit chan struct{}
}

func newTicketQueue(store TicketStore, sender ethcommon.Address, blockSub func(chan<- *big.Int) event.Subscription) *ticketQueue {
func newTicketQueue(sender ethcommon.Address, sm *LocalSenderMonitor) *ticketQueue {
return &ticketQueue{
blockSub: blockSub,
tm: sm.tm,
redeemable: make(chan *redemption),
store: store,
store: sm.ticketStore,
sender: sender,
quit: make(chan struct{}),
}
Expand Down Expand Up @@ -80,7 +79,7 @@ func (q *ticketQueue) Redeemable() chan *redemption {

// Length returns the current length of the queue
func (q *ticketQueue) Length() (int, error) {
return q.store.WinningTicketCount(q.sender)
return q.store.WinningTicketCount(q.sender, new(big.Int).Sub(q.tm.LastInitializedRound(), big.NewInt(ticketValidityPeriod)).Int64())
}

// startQueueLoop blocks until the ticket queue is non-empty. When the queue is non-empty
Expand All @@ -92,7 +91,7 @@ func (q *ticketQueue) Length() (int, error) {
// the ticket at the head of the queue and send it into q.redeemable which an external listener can use to receive redeemable tickets
func (q *ticketQueue) startQueueLoop() {
blockNums := make(chan *big.Int, 10)
sub := q.blockSub(blockNums)
sub := q.tm.SubscribeBlocks(blockNums)
defer sub.Unsubscribe()

ticketLoop:
Expand All @@ -109,7 +108,7 @@ ticketLoop:
continue
}
for i := 0; i < int(numTickets); i++ {
nextTicket, err := q.store.SelectEarliestWinningTicket(q.sender)
nextTicket, err := q.store.SelectEarliestWinningTicket(q.sender, new(big.Int).Sub(q.tm.LastInitializedRound(), big.NewInt(ticketValidityPeriod)).Int64())
if err != nil {
glog.Errorf("Unable select earliest winning ticket err=%v", err)
continue ticketLoop
Expand All @@ -129,9 +128,12 @@ ticketLoop:
case res := <-resCh:
// after receiving the response we can close the channel so it can be GC'd
close(resCh)
// If the ticket is used, we can mark it as redeemed
if res.err != nil {
glog.Errorf("Error redeeming err=%v", res.err)
continue
if res.err != errIsUsedTicket {
continue
}
}
err := q.store.MarkWinningTicketRedeemed(nextTicket, res.txHash)
if err != nil {
Expand Down
Loading