Skip to content

Commit fd40b00

Browse files
committed
Prevent negative gas price (iotexproject#1059)
1 parent c3ef825 commit fd40b00

File tree

8 files changed

+40
-2
lines changed

8 files changed

+40
-2
lines changed

action/const.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ var (
2525
ErrNonce = errors.New("invalid nonce")
2626
// ErrBalance indicates the error of balance
2727
ErrBalance = errors.New("invalid balance")
28+
// ErrGasPrice indicates the error of gas price
29+
ErrGasPrice = errors.New("invalid gas price")
2830
// ErrVotee indicates the error of votee
2931
ErrVotee = errors.New("votee is not a candidate")
3032
// ErrHash indicates the error of action's hash

action/protocol/account/transfer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ func (p *Protocol) validateTransfer(_ context.Context, act action.Action) error
173173
if tsf.Amount().Sign() < 0 {
174174
return errors.Wrap(action.ErrBalance, "negative value")
175175
}
176+
// Reject transfer of negative gas price
177+
if tsf.GasPrice().Sign() < 0 {
178+
return errors.Wrap(action.ErrGasPrice, "negative value")
179+
}
176180
// check if recipient's address is valid
177181
if _, err := address.FromString(tsf.Recipient()); err != nil {
178182
return errors.Wrapf(err, "error when validating recipient's address %s", tsf.Recipient())

action/protocol/account/transfer_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,10 @@ func TestProtocol_ValidateTransfer(t *testing.T) {
149149
err = protocol.Validate(context.Background(), tsf)
150150
require.Error(err)
151151
require.True(strings.Contains(err.Error(), "error when validating recipient's address"))
152+
// Case IV: Negative gas fee
153+
tsf, err = action.NewTransfer(uint64(1), big.NewInt(100), "2", nil,
154+
uint64(100000), big.NewInt(-1))
155+
require.NoError(err)
156+
err = protocol.Validate(context.Background(), tsf)
157+
require.Equal(action.ErrGasPrice, errors.Cause(err))
152158
}

action/protocol/execution/protocol.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ func (p *Protocol) Validate(_ context.Context, act action.Action) error {
7373
if exec.Amount().Sign() < 0 {
7474
return errors.Wrap(action.ErrBalance, "negative value")
7575
}
76+
// Reject execution of negative gas price
77+
if exec.GasPrice().Sign() < 0 {
78+
return errors.Wrap(action.ErrGasPrice, "negative value")
79+
}
7680
// check if contract's address is valid
7781
if exec.Contract() != action.EmptyAddress {
7882
if _, err := address.FromString(exec.Contract()); err != nil {

action/protocol/execution/protocol_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,4 +717,9 @@ func TestProtocol_Validate(t *testing.T) {
717717
err = protocol.Validate(context.Background(), ex)
718718
require.Error(err)
719719
require.True(strings.Contains(err.Error(), "error when validating contract's address"))
720+
// Case V: Negative gas price
721+
ex, err = action.NewExecution("2", uint64(1), big.NewInt(100), uint64(0), big.NewInt(-1), []byte{})
722+
require.NoError(err)
723+
err = protocol.Validate(context.Background(), ex)
724+
require.Equal(action.ErrGasPrice, errors.Cause(err))
720725
}

action/protocol/rewarding/admin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (p *Protocol) assertAmount(amount *big.Int) error {
236236
if amount.Cmp(big.NewInt(0)) >= 0 {
237237
return nil
238238
}
239-
return errors.Errorf("reward amount %s shouldn't be negative", amount.String())
239+
return errors.Errorf("amount %s shouldn't be negative", amount.String())
240240
}
241241

242242
func (p *Protocol) assertZeroBlockHeight(height uint64) error {

action/protocol/rewarding/fund.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func (p *Protocol) Deposit(
6262
amount *big.Int,
6363
) error {
6464
raCtx := protocol.MustGetRunActionsCtx(ctx)
65+
if err := p.assertAmount(amount); err != nil {
66+
return err
67+
}
6568
if err := p.assertEnoughBalance(raCtx, sm, amount); err != nil {
6669
return err
6770
}
@@ -71,7 +74,9 @@ func (p *Protocol) Deposit(
7174
return err
7275
}
7376
acc.Balance = big.NewInt(0).Sub(acc.Balance, amount)
74-
accountutil.StoreAccount(sm, raCtx.Caller.String(), acc)
77+
if err := accountutil.StoreAccount(sm, raCtx.Caller.String(), acc); err != nil {
78+
return err
79+
}
7580
// Add balance to fund
7681
f := fund{}
7782
if err := p.state(sm, fundKey, &f); err != nil {

action/protocol/rewarding/fund_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,15 @@ func TestProtocol_Fund(t *testing.T) {
5050
}, false)
5151

5252
}
53+
54+
func TestDepositNegativeGasFee(t *testing.T) {
55+
testProtocol(t, func(t *testing.T, ctx context.Context, stateDB factory.Factory, p *Protocol) {
56+
57+
r := protocol.Registry{}
58+
r.Register(ProtocolID, p)
59+
60+
ws, err := stateDB.NewWorkingSet()
61+
require.NoError(t, err)
62+
require.Error(t, DepositGas(ctx, ws, big.NewInt(-1), &r))
63+
}, false)
64+
}

0 commit comments

Comments
 (0)