Skip to content

Commit

Permalink
pm: Require max float > tx cost
Browse files Browse the repository at this point in the history
  • Loading branch information
yondonfu committed Feb 2, 2021
1 parent 44a940a commit 6b1fca4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pm/recipient.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ func (r *recipient) faceValue(sender ethcommon.Address) (*big.Int, error) {
}

if faceValue.Cmp(maxFloat) > 0 {
if maxFloat.Cmp(r.cfg.EV) < 0 {
// If maxFloat < EV, then there is no
if maxFloat.Cmp(r.cfg.EV) < 0 || maxFloat.Cmp(r.txCost()) <= 0 {
// If maxFloat < EV or <= tx cost, then there is no
// acceptable faceValue
return nil, errInsufficientSenderReserve
}
Expand Down
21 changes: 16 additions & 5 deletions pm/recipient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,26 +488,38 @@ func TestTicketParams(t *testing.T) {
assert.LessOrEqual(new(big.Int).Abs(new(big.Int).Sub(cfg.EV, expEV)).Int64(), int64(1))

// Test correct params returned when default faceValue > maxFloat
sm.maxFloat = big.NewInt(10000)
sm.maxFloat = big.NewInt(10001)
gm.gasPrice = big.NewInt(1)

params4, err := r.TicketParams(sender, big.NewRat(1, 1))
require.Nil(err)

faceValue = sm.maxFloat
assert.Equal(faceValue, params4.FaceValue)

winProb, _ = new(big.Int).SetString("57896044618658097711785492504343953926634992332820282019728792003956564820", 10)
winProb, _ = new(big.Int).SetString("57890255593098787833002192285115442382396752657554526567072084795477017120", 10)
assert.Equal(winProb, params4.WinProb)

// Might be slightly off due to truncation
expEV = new(big.Int).Div(new(big.Int).Mul(faceValue, winProb), maxWinProb)
assert.LessOrEqual(new(big.Int).Abs(new(big.Int).Sub(cfg.EV, expEV)).Int64(), int64(1))

// Test insufficient sender reserve error
// Test insufficient sender reserve error due to maxFloat < EV
sm.maxFloat = new(big.Int).Sub(cfg.EV, big.NewInt(1))
_, err = r.TicketParams(sender, big.NewRat(1, 1))
assert.EqualError(err, errInsufficientSenderReserve.Error())

// Test insufficient sender reserve error due to maxFloat < txCost
sm.maxFloat = new(big.Int).Add(cfg.EV, big.NewInt(1))
_, err = r.TicketParams(sender, big.NewRat(1, 1))
assert.EqualError(err, errInsufficientSenderReserve.Error())

// Test insufficient sender reserve error due to maxFloat = txCost
txCost := new(big.Int).Mul(big.NewInt(int64(cfg.RedeemGas)), gm.gasPrice)
sm.maxFloat = txCost
_, err = r.TicketParams(sender, big.NewRat(1, 1))
assert.EqualError(err, errInsufficientSenderReserve.Error())

// Test default faceValue < EV and maxFloat > EV
// Set gas price = 0 to set default faceValue = 0
gm.gasPrice = big.NewInt(0)
Expand Down Expand Up @@ -543,9 +555,8 @@ func TestTxCostMultiplier_UsingMaxFloat_ReturnsScaledMultiplier(t *testing.T) {
secret := [32]byte{3}
r := NewRecipientWithSecret(recipient, b, v, gm, sm, tm, secret, cfg)

sm.maxFloat = big.NewInt(500000)

txCost := new(big.Int).Mul(gm.gasPrice, big.NewInt(int64(cfg.RedeemGas)))
sm.maxFloat = new(big.Int).Add(txCost, big.NewInt(1))
expMul := new(big.Rat).SetFrac(sm.maxFloat, txCost)

mul, err := r.TxCostMultiplier(sender)
Expand Down

0 comments on commit 6b1fca4

Please sign in to comment.