Skip to content

Commit

Permalink
fixup! pm: isUsedTicket check when redeeming ticket
Browse files Browse the repository at this point in the history
  • Loading branch information
kyriediculous committed Oct 7, 2020
1 parent e5cf270 commit 8e00158
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
6 changes: 4 additions & 2 deletions pm/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ ticketLoop:
// 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 && res.err != errisUsedTicket {
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
43 changes: 40 additions & 3 deletions pm/queue_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pm

import (
"fmt"
"math/big"
"sync"
"testing"
Expand Down Expand Up @@ -30,8 +31,9 @@ func defaultSignedTicket(sender ethcommon.Address, senderNonce uint32) *SignedTi
}

type queueConsumer struct {
redeemable []*redemption
mu sync.Mutex
redeemable []*redemption
mu sync.Mutex
redemptionErr error
}

// Redeemable returns the consumed redeemable tickets from a ticket queue
Expand All @@ -56,7 +58,7 @@ func (qc *queueConsumer) Wait(num int, e RedeemableEmitter, done chan struct{})
ticket.resCh <- struct {
txHash ethcommon.Hash
err error
}{RandHash(), nil}
}{ticket.SignedTicket.Hash(), qc.redemptionErr}
}
}
done <- struct{}{}
Expand Down Expand Up @@ -121,9 +123,44 @@ func TestTicketQueueLoop(t *testing.T) {
redeemable := qc.Redeemable()
for i := 0; i < numTickets; i++ {
assert.Equal(uint32(i), redeemable[i].SignedTicket.SenderNonce)
assert.True(ts.submitted[fmt.Sprintf("%x", redeemable[i].SignedTicket.Sig)])
}
}

func TestTicketQueueLoop_IsUsedTicket_MarkAsRedeemed(t *testing.T) {
assert := assert.New(t)

sender := RandAddress()
ts := newStubTicketStore()
tm := &stubTimeManager{round: big.NewInt(100)}
sm := &LocalSenderMonitor{
ticketStore: ts,
tm: tm,
}

q := newTicketQueue(sender, sm)
q.Start()
defer q.Stop()

ticket := defaultSignedTicket(sender, 0)
q.Add(ticket)

qlen, err := q.Length()
assert.Nil(err)
assert.Equal(1, qlen)

qc := &queueConsumer{redemptionErr: errIsUsedTicket}
done := make(chan struct{})
go qc.Wait(1, q, done)
time.Sleep(20 * time.Millisecond)

tm.blockNumSink <- big.NewInt(1)
<-done
time.Sleep(20 * time.Millisecond)

assert.True(ts.submitted[fmt.Sprintf("%x", ticket.Sig)])
}

func TestTicketQueueLoopConcurrent(t *testing.T) {
assert := assert.New(t)

Expand Down
2 changes: 1 addition & 1 deletion pm/sendermonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (sm *LocalSenderMonitor) redeemWinningTicket(ticket *SignedTicket) (*types.
if monitor.Enabled {
monitor.TicketRedemptionError(ticket.Ticket.Sender.String())
}
return nil, errisUsedTicket
return nil, errIsUsedTicket
}

ctx, cancel := context.WithTimeout(context.Background(), sm.cfg.RPCTimeout)
Expand Down
2 changes: 1 addition & 1 deletion pm/sendermonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ func TestRedeemWinningTicket_IsUsedTicket(t *testing.T) {
b.usedTickets[signedT.Hash()] = true
tx, err = sm.redeemWinningTicket(signedT)
assert.Nil(tx)
assert.EqualError(err, errisUsedTicket.Error())
assert.EqualError(err, errIsUsedTicket.Error())

// test not used
b.usedTickets[signedT.Hash()] = false
Expand Down
2 changes: 1 addition & 1 deletion pm/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
errInvalidTicketSignature = errors.New("invalid ticket signature")
errInvalidCreationRound = errors.New("invalid ticket creation round")
errInvalidCreationRoundBlockHash = errors.New("invalid ticket creation round block hash")
errisUsedTicket = errors.New("ticket already used")
errIsUsedTicket = errors.New("ticket already used")
)

// Validator is an interface which describes an object capable
Expand Down

0 comments on commit 8e00158

Please sign in to comment.