Skip to content

Commit

Permalink
Add gas test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Jun 28, 2024
1 parent 84df3ca commit c7cfb73
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 3 deletions.
12 changes: 10 additions & 2 deletions zetaclient/chains/evm/signer/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
// https://eips.ethereum.org/EIPS/eip-1559
// https://www.blocknative.com/blog/eip-1559-fees
// https://github.com/bnb-chain/BEPs/blob/master/BEPs/BEP226.md (tl;dr: baseFee is always zero)
//
// However, this doesn't affect tx creation nor broadcasting
type Gas struct {
Limit uint64

Expand Down Expand Up @@ -46,10 +48,11 @@ func (g Gas) validate() error {
//
// Returns true if priority fee is <= 0.
func (g Gas) isLegacy() bool {
return g.PriorityFeePerUnit.Sign() <= 1
return g.PriorityFeePerUnit.Sign() < 1
}

func determineGas(cctx *types.CrossChainTx, priorityFee *big.Int, logger zerolog.Logger) (Gas, error) {
// makeGasFromCCTX creates Gas struct based from CCTX and priorityFee.
func makeGasFromCCTX(cctx *types.CrossChainTx, priorityFee *big.Int, logger zerolog.Logger) (Gas, error) {
if priorityFee == nil {
return Gas{}, errors.New("priorityFee is nil")
}
Expand Down Expand Up @@ -79,6 +82,11 @@ func determineGas(cctx *types.CrossChainTx, priorityFee *big.Int, logger zerolog
return Gas{}, errors.New("unable to parse gasPrice from " + outboundParams.GasPrice)
}

// is maxFee < priorityFee
if maxFee.Cmp(priorityFee) == -1 {
return Gas{}, errors.Errorf("maxFee (%d) is less than priorityFee (%d)", maxFee.Int64(), priorityFee.Int64())
}

return Gas{
Limit: limit,
MaxFeePerUnit: maxFee,
Expand Down
121 changes: 121 additions & 0 deletions zetaclient/chains/evm/signer/gas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package signer

import (
"math/big"
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/zeta-chain/zetacore/x/crosschain/types"
)

func Test_makeGasFromCCTX(t *testing.T) {
logger := zerolog.Nop()

cctx1 := getCCTX(t)
cctx1.GetOutboundParams()[0].GasLimit = MinGasLimit - 200
cctx1.GetOutboundParams()[0].GasPrice = gwei(2).String()

cctx2 := getCCTX(t)
cctx2.GetOutboundParams()[0].GasLimit = 200_000
cctx2.GetOutboundParams()[0].GasPrice = gwei(3).String()

cctx3 := getCCTX(t)
cctx3.GetOutboundParams()[0].GasLimit = 2_000_000
cctx3.GetOutboundParams()[0].GasPrice = gwei(3).String()

for _, tt := range []struct {
name string
cctx *types.CrossChainTx
priorityFee *big.Int
errorContains string
assert func(t *testing.T, g Gas)
}{
{
name: "priority fee is nil",
cctx: cctx1,
priorityFee: nil,
errorContains: "priorityFee is nil",
},
{
name: "gas is too low",
cctx: cctx1,
priorityFee: gwei(1),
assert: func(t *testing.T, g Gas) {
assert.False(t, g.isLegacy())
assertGasEquals(t, Gas{
Limit: MinGasLimit,
PriorityFeePerUnit: gwei(1),
MaxFeePerUnit: gwei(2),
}, g)
},
},
{
name: "as is, no surprises",
cctx: cctx2,
priorityFee: gwei(2),
assert: func(t *testing.T, g Gas) {
assert.False(t, g.isLegacy())
assertGasEquals(t, Gas{
Limit: 200_000,
PriorityFeePerUnit: gwei(2),
MaxFeePerUnit: gwei(3),
}, g)
},
},
{
name: "pre London gas logic",
cctx: cctx2,
priorityFee: gwei(0),
assert: func(t *testing.T, g Gas) {
assert.True(t, g.isLegacy())
assertGasEquals(t, Gas{
Limit: 200_000,
PriorityFeePerUnit: gwei(0),
MaxFeePerUnit: gwei(3),
}, g)
},
},
{
name: "gas is too high, force to the ceiling",
cctx: cctx3,
priorityFee: gwei(2),
assert: func(t *testing.T, g Gas) {
assert.False(t, g.isLegacy())
assertGasEquals(t, Gas{
Limit: MaxGasLimit,
PriorityFeePerUnit: gwei(2),
MaxFeePerUnit: gwei(3),
}, g)
},
},
{
name: "priority fee is too high",
cctx: cctx1,
priorityFee: gwei(200),
errorContains: "less than priorityFee",
},
} {
t.Run(tt.name, func(t *testing.T) {
g, err := makeGasFromCCTX(tt.cctx, tt.priorityFee, logger)
if tt.errorContains != "" {
assert.ErrorContains(t, err, tt.errorContains)
return
}

assert.NoError(t, err)
tt.assert(t, g)
})
}
}

func assertGasEquals(t *testing.T, expected, actual Gas) {
assert.Equal(t, int64(expected.Limit), int64(actual.Limit), "gas limit")
assert.Equal(t, expected.MaxFeePerUnit.Int64(), actual.MaxFeePerUnit.Int64(), "max fee per unit")
assert.Equal(t, expected.PriorityFeePerUnit.Int64(), actual.PriorityFeePerUnit.Int64(), "priority fee per unit")
}

func gwei(i int64) *big.Int {
const g = 1_000_000_000
return big.NewInt(i * g)
}
2 changes: 1 addition & 1 deletion zetaclient/chains/evm/signer/outbound_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewOutboundData(
}

// Determine gas fees
gas, err := determineGas(cctx, evmObserver.GetPriorityGasFee(), logger)
gas, err := makeGasFromCCTX(cctx, evmObserver.GetPriorityGasFee(), logger)
if err != nil {
return nil, false, errors.Wrap(err, "failed to determine gas fees")
}
Expand Down

0 comments on commit c7cfb73

Please sign in to comment.