Skip to content

Commit

Permalink
Added Test + cleanup based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yongkangc committed Aug 14, 2023
1 parent 1d90bf9 commit c504799
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/cosmos/client/gas_price_estimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (gpe *FixedGasPriceEstimator) CalculateGasPrice(

// BumpGasPrice calculates a new gas price by bumping the current gas price by a percentage.
// Parameters:
// - currentGasPrice: current gas price (could be after bumping)
// - originalGasPrice: original gas price (before bumping)
// - currentGasPrice: The current gas price before bumping in the current round. May have already been bumped previously.
// - originalGasPrice: The original base gas price before any bumping.
// - maxGasPrice: max gas price
// - maxBumpPrice: max gas price that can be bumped to
// - bumpMin: min gas price that can be bumped by
Expand Down
44 changes: 44 additions & 0 deletions pkg/cosmos/client/gas_price_estimator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"go.uber.org/zap"

"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -146,6 +147,49 @@ func TestFixedPriceGasEstimator(t *testing.T) {
}
})

t.Run("Set Gas Price", func(t *testing.T) {
tests := []struct {
name string
gasToSet sdk.DecCoin
gpeFixed *FixedGasPriceEstimator
want sdk.DecCoin
}{
{
name: "Empty map adds the gas price",
gasToSet: sdk.NewDecCoinFromDec("ucosm", types.MustNewDecFromStr("0.001")),
gpeFixed: NewFixedGasPriceEstimator(map[string]sdk.DecCoin{}, nil),
want: sdk.NewDecCoinFromDec("ucosm", sdk.MustNewDecFromStr("0.001")),
},
{
name: "Map with same key updates the gas price",
gasToSet: sdk.NewDecCoinFromDec("ucosm", sdk.MustNewDecFromStr("0.002")),
gpeFixed: NewFixedGasPriceEstimator(map[string]sdk.DecCoin{
"ucosm": sdk.NewDecCoinFromDec("ucosm", sdk.MustNewDecFromStr("0.001")),
}, nil),
want: sdk.NewDecCoinFromDec("ucosm", sdk.MustNewDecFromStr("0.002")),
},
{name: "Map with different key adds the gas price",
gasToSet: sdk.NewDecCoinFromDec("uatom", sdk.MustNewDecFromStr("0.001")),
gpeFixed: NewFixedGasPriceEstimator(map[string]sdk.DecCoin{
"ucosm": sdk.NewDecCoinFromDec("ucosm", sdk.MustNewDecFromStr("0.001")),
}, nil),
want: sdk.NewDecCoinFromDec("uatom", sdk.MustNewDecFromStr("0.001")),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set gas price
tt.gpeFixed.SetGasPrice("ucosm", tt.gasToSet)

// Check the value
got, ok := tt.gpeFixed.gasPrices["ucosm"]
assert.True(t, ok, "coin type not found in gas prices map")
assert.Equal(t, tt.want, got, "calculated gas price does not match expected")
})
}
})

t.Run("bump gas price", func(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit c504799

Please sign in to comment.