Skip to content

Commit

Permalink
fix: add gaspremium check when less than maxfee (#5383)
Browse files Browse the repository at this point in the history
* fix: add gaspremium check when less than maxfee

* test: add test for CapGasFee
  • Loading branch information
hunjixin authored Oct 17, 2022
1 parent 374f8bf commit 8e4d2b6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
7 changes: 4 additions & 3 deletions pkg/messagepool/messagepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func CapGasFee(mff DefaultMaxFeeFunc, msg *types.Message, sendSepc *types.Messag
totalFee := types.BigMul(msg.GasFeeCap, gl)

if totalFee.LessThanEqual(maxFee) {
msg.GasPremium = big.Min(msg.GasFeeCap, msg.GasPremium)
return
}

Expand Down Expand Up @@ -1092,9 +1093,9 @@ func (mp *MessagePool) getStateBalance(ctx context.Context, addr address.Address

// this method is provided for the gateway to push messages.
// differences from Push:
// - strict checks are enabled
// - extra strict add checks are used when adding the messages to the msgSet
// that means: no nonce gaps, at most 10 pending messages for the actor
// - strict checks are enabled
// - extra strict add checks are used when adding the messages to the msgSet
// that means: no nonce gaps, at most 10 pending messages for the actor
func (mp *MessagePool) PushUntrusted(ctx context.Context, m *types.SignedMessage) (cid.Cid, error) {
err := mp.checkMessage(m)
if err != nil {
Expand Down
40 changes: 39 additions & 1 deletion pkg/messagepool/messagepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"sort"
"testing"

_ "github.com/filecoin-project/venus/pkg/crypto/secp"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
tbig "github.com/filecoin-project/go-state-types/big"
Expand All @@ -19,7 +21,6 @@ import (
"github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/pkg/crypto"
_ "github.com/filecoin-project/venus/pkg/crypto/secp"
"github.com/filecoin-project/venus/pkg/messagepool/gasguess"
"github.com/filecoin-project/venus/pkg/repo"
tf "github.com/filecoin-project/venus/pkg/testhelpers/testflags"
Expand Down Expand Up @@ -808,3 +809,40 @@ func TestUpdates(t *testing.T) {
t.Fatal("expected closed channel, but got an update instead")
}
}

func TestCapGasFee(t *testing.T) {
t.Run("use default maxfee", func(t *testing.T) {
msg := &types.Message{
GasLimit: 100_000_000,
GasFeeCap: abi.NewTokenAmount(100_000_000),
GasPremium: abi.NewTokenAmount(100_000),
}
CapGasFee(func() (abi.TokenAmount, error) {
return abi.NewTokenAmount(100_000_000_000), nil
}, msg, nil)
assert.Equal(t, msg.GasFeeCap.Int64(), int64(1000))
assert.Equal(t, msg.GasPremium.Int.Int64(), int64(1000))
})

t.Run("use spec maxfee", func(t *testing.T) {
msg := &types.Message{
GasLimit: 100_000_000,
GasFeeCap: abi.NewTokenAmount(100_000_000),
GasPremium: abi.NewTokenAmount(100_000),
}
CapGasFee(nil, msg, &types.MessageSendSpec{MaxFee: abi.NewTokenAmount(100_000_000_000)})
assert.Equal(t, msg.GasFeeCap.Int64(), int64(1000))
assert.Equal(t, msg.GasPremium.Int.Int64(), int64(1000))
})

t.Run("use smaller feecap value when fee is enough", func(t *testing.T) {
msg := &types.Message{
GasLimit: 100_000_000,
GasFeeCap: abi.NewTokenAmount(100_000),
GasPremium: abi.NewTokenAmount(100_000_000),
}
CapGasFee(nil, msg, &types.MessageSendSpec{MaxFee: abi.NewTokenAmount(100_000_000_000_000)})
assert.Equal(t, msg.GasFeeCap.Int64(), int64(100_000))
assert.Equal(t, msg.GasPremium.Int.Int64(), int64(100_000))
})
}

0 comments on commit 8e4d2b6

Please sign in to comment.