Skip to content

Commit

Permalink
Merge PR: return the minimal GP if block is uncongested (#2892)
Browse files Browse the repository at this point in the history
* bug fix

* rewrite ut

* enrich ut

* rewrite ut
  • Loading branch information
LeoGuo621 authored Dec 28, 2022
1 parent 9834981 commit a8d4ecf
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 16 deletions.
8 changes: 8 additions & 0 deletions app/gasprice/gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ func (gpo *Oracle) RecommendGP() *big.Int {
txPrices := gpo.BlockGPQueue.ExecuteSamplingBy(gpo.lastPrice, adoptHigherGp)

price := new(big.Int).Set(gpo.lastPrice)

// If the block is not congested, return the minimal GP
if !isCongested {
price.Set(MinPrice)
gpo.lastPrice.Set(price)
return price
}

if len(txPrices) > 0 {
sort.Sort(types.BigIntArray(txPrices))
price.Set(txPrices[(len(txPrices)-1)*gpo.weight/100])
Expand Down
119 changes: 104 additions & 15 deletions app/gasprice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import (
)

var (
txCoin100000 = cosmossdk.NewInt64Coin(cosmossdk.DefaultBondDenom, 100000)
nonce = uint64(0)
txNumPerBlock = 200
txCoin100000 = cosmossdk.NewInt64Coin(cosmossdk.DefaultBondDenom, 100000)
nonce = uint64(0)
)

type FakeBlockRecommendGPTestSuite struct {
Expand Down Expand Up @@ -121,17 +120,92 @@ func (suite *FakeBlockRecommendGPTestSuite) TestRecommendGP() {

needMultiple bool
gpDecrease bool
tpb []int
}{
{
title: "5/5 empty block, higher gp mode",
buildTxs: generateEvmTxs,
gpMaxTxNum: 300,
gpMaxGasUsed: 1000000,
gpMode: 0,
expectedTotalGU: []int64{0, 0, 0, 0, 0},
expectedRecommendGp: []string{"100000000", "100000000", "100000000", "100000000", "100000000"},
blocks: 5,
needMultiple: false,
tpb: []int{0, 0, 0, 0, 0},
},
{
title: "4/6 empty block, gp increase, higher gp mode",
buildTxs: generateEvmTxs,
gpMaxTxNum: 300,
gpMaxGasUsed: 40000000,
gpMode: 0,
expectedTotalGU: []int64{46329800, 0, 0, 0, 0, 46329800},
expectedRecommendGp: []string{"100200099", "100000000", "100000000", "100000000", "100000000", "100200099"},
blocks: 6,
needMultiple: false,
tpb: []int{200, 0, 0, 0, 0, 200},
},
{
title: "4/6 uncongested block, gp increase, higher gp mode",
buildTxs: generateEvmTxs,
gpMaxTxNum: 300,
gpMaxGasUsed: 40000000,
gpMode: 0,
expectedTotalGU: []int64{46329800, 23164900, 23164900, 23164900, 23164900, 46329800},
expectedRecommendGp: []string{"100200099", "100000000", "100000000", "100000000", "100000000", "100200500"},
blocks: 6,
needMultiple: false,
tpb: []int{200, 100, 100, 100, 100, 200},
},
{
title: "2/5 empty block, congestion, gp increase, higher gp mode",
buildTxs: generateEvmTxs,
gpMaxTxNum: 300,
gpMaxGasUsed: 1000000,
gpMode: 0,
expectedTotalGU: []int64{46329800, 0, 46329800, 0, 46329800},
expectedRecommendGp: []string{"100200099", "100000000", "100200099", "100000000", "100200299"},
blocks: 5,
needMultiple: false,
tpb: []int{200, 0, 200, 0, 200},
},
{
title: "congestion, gp increase, higher gp mode",
buildTxs: generateEvmTxs,
gpMaxTxNum: 300,
gpMaxGasUsed: 1000000,
gpMode: 0,
expectedTotalGU: []int64{46329800, 46329800, 46329800, 46329800, 46329800},
expectedRecommendGp: []string{"100200099", "100200299", "100200499", "100200699", "100200899"},
expectedRecommendGp: []string{"100200099", "100200099", "100200299", "100200499", "100200699"},
blocks: 5,
needMultiple: false,
tpb: []int{200, 200, 200, 200, 200},
},
{
title: "2/5 empty block, uncongestion, gp increase, higher gp mode",
buildTxs: generateEvmTxs,
gpMaxTxNum: 300,
gpMaxGasUsed: 60000000,
gpMode: 0,
expectedTotalGU: []int64{46329800, 0, 46329800, 0, 46329800},
expectedRecommendGp: []string{"100000000", "100000000", "100000000", "100000000", "100000000"},
blocks: 5,
needMultiple: false,
tpb: []int{200, 0, 200, 0, 200},
},
{
title: "congestion, gp decrease, normal gp mode",
buildTxs: generateEvmTxs,
gpMaxTxNum: 300,
gpMaxGasUsed: 1000000,
gpMode: 1,
expectedTotalGU: []int64{46329800, 46329800, 46329800, 46329800, 46329800},
expectedRecommendGp: []string{"100199802", "100199802", "100199801", "100199603", "100199603"},
blocks: 5,
needMultiple: false,
gpDecrease: true,
tpb: []int{200, 200, 200, 200, 200},
},
{
title: "congestion, gp decrease, higher gp mode",
Expand All @@ -140,10 +214,11 @@ func (suite *FakeBlockRecommendGPTestSuite) TestRecommendGP() {
gpMaxGasUsed: 1000000,
gpMode: 0,
expectedTotalGU: []int64{46329800, 46329800, 46329800, 46329800, 46329800},
expectedRecommendGp: []string{"100199900", "100199700", "100199500", "100199300", "100199100"},
expectedRecommendGp: []string{"100199900", "100199700", "100199700", "100199700", "100199700"},
blocks: 5,
needMultiple: false,
gpDecrease: true,
tpb: []int{200, 200, 200, 200, 200},
},
{
title: "congestion, gp increase, normal mode",
Expand All @@ -152,9 +227,10 @@ func (suite *FakeBlockRecommendGPTestSuite) TestRecommendGP() {
gpMaxGasUsed: 1000000,
gpMode: 1,
expectedTotalGU: []int64{46329800, 46329800, 46329800, 46329800, 46329800},
expectedRecommendGp: []string{"100200000", "100200200", "100200400", "100200600", "100200800"},
expectedRecommendGp: []string{"100200001", "100200201", "100200400", "100200402", "100200602"},
blocks: 5,
needMultiple: false,
tpb: []int{200, 200, 200, 200, 200},
},
{
title: "no congestion, gp increase, higher gp mode",
Expand All @@ -163,9 +239,10 @@ func (suite *FakeBlockRecommendGPTestSuite) TestRecommendGP() {
gpMaxGasUsed: 60000000,
gpMode: 0,
expectedTotalGU: []int64{46329800, 46329800, 46329800, 46329800, 46329800},
expectedRecommendGp: []string{"100200000", "100200200", "100200400", "100200600", "100200800"},
expectedRecommendGp: []string{"100000000", "100000000", "100000000", "100000000", "100000000"},
blocks: 5,
needMultiple: false,
tpb: []int{200, 200, 200, 200, 200},
},
{
title: "no congestion, gp increase, gp multiple, higher gp mode",
Expand All @@ -174,9 +251,10 @@ func (suite *FakeBlockRecommendGPTestSuite) TestRecommendGP() {
gpMaxGasUsed: 60000000,
gpMode: 0,
expectedTotalGU: []int64{46329800, 46329800, 46329800, 46329800, 46329800},
expectedRecommendGp: []string{"100200000", "100200200", "100200400", "100200600", "100200800"},
expectedRecommendGp: []string{"100000000", "100000000", "100000000", "100000000", "100000000"},
blocks: 5,
needMultiple: true,
tpb: []int{200, 200, 200, 200, 200},
},
{
title: "congestion, gp increase, gp multiple, higher gp mode",
Expand All @@ -185,9 +263,10 @@ func (suite *FakeBlockRecommendGPTestSuite) TestRecommendGP() {
gpMaxGasUsed: 1000000,
gpMode: 0,
expectedTotalGU: []int64{46329800, 46329800, 46329800, 46329800, 46329800},
expectedRecommendGp: []string{"5060109900", "5060120000", "5060130100", "5060140200", "5060150300"},
expectedRecommendGp: []string{"5060109900", "5060109900", "5060120000", "5060130100", "5060140200"},
blocks: 5,
needMultiple: true,
tpb: []int{200, 200, 200, 200, 200},
},
{
title: "congestion, gp decrease, gp multiple, higher gp mode",
Expand All @@ -196,10 +275,11 @@ func (suite *FakeBlockRecommendGPTestSuite) TestRecommendGP() {
gpMaxGasUsed: 1000000,
gpMode: 0,
expectedTotalGU: []int64{46329800, 46329800, 46329800, 46329800, 46329800},
expectedRecommendGp: []string{"5060094901", "5060084801", "5060074701", "5060064601", "5060054501"},
expectedRecommendGp: []string{"5060094901", "5060084801", "5060084801", "5060084801", "5060084801"},
blocks: 5,
needMultiple: true,
gpDecrease: true,
tpb: []int{200, 200, 200, 200, 200},
},
{
title: "congestion, gp increase, gp multiple, normal mode",
Expand All @@ -208,12 +288,15 @@ func (suite *FakeBlockRecommendGPTestSuite) TestRecommendGP() {
gpMaxGasUsed: 1000000,
gpMode: 1,
expectedTotalGU: []int64{46329800, 46329800, 46329800, 46329800, 46329800},
expectedRecommendGp: []string{"100200000", "100200200", "100200400", "100200600", "100200800"},
expectedRecommendGp: []string{"100200001", "100200201", "100200400", "100200402", "100200602"},
blocks: 5,
needMultiple: true,
tpb: []int{200, 200, 200, 200, 200},
},
}

appconfig.GetOecConfig().SetDynamicGpWeight(80)
appconfig.GetOecConfig().SetDynamicGpCheckBlocks(5)
suite.SetupTest()
for _, tc := range testCases {

Expand All @@ -229,7 +312,7 @@ func (suite *FakeBlockRecommendGPTestSuite) TestRecommendGP() {
totalGasUsed := int64(0)
suite.beginFakeBlock(height)
suite.Run(tc.title+", tx serial", func() {
txs := tc.buildTxs(txNumPerBlock, baseGP, &gpOffset, tc.gpDecrease, tc.needMultiple)
txs := tc.buildTxs(tc.tpb[i], baseGP, &gpOffset, tc.gpDecrease, tc.needMultiple)
for _, tx := range txs {
tx.Sign(evmChainID, suite.evmSenderPrivKey.ToECDSA())
txBytes, err := authclient.GetTxEncoder(nil, authclient.WithEthereumTx())(tx)
Expand All @@ -241,10 +324,13 @@ func (suite *FakeBlockRecommendGPTestSuite) TestRecommendGP() {
}
})
//fmt.Println("totalGasUsed: ", totalGasUsed)
suite.Require().True(totalGasUsed == tc.expectedTotalGU[i], "block gas expect %d, but %d ", tc.expectedTotalGU, totalGasUsed)
suite.Require().True(totalGasUsed == tc.expectedTotalGU[i], "block gas expect %d, but %d ", tc.expectedTotalGU[i], totalGasUsed)
suite.endFakeBlock(tc.expectedRecommendGp[i])
height++
}
for !suite.app.gpo.BlockGPQueue.IsEmpty() {
suite.app.gpo.BlockGPQueue.Pop()
}

// tx parallel
gpOffset = int64(200000)
Expand All @@ -254,7 +340,7 @@ func (suite *FakeBlockRecommendGPTestSuite) TestRecommendGP() {
totalGasUsed := int64(0)
suite.beginFakeBlock(height)
suite.Run(tc.title+", tx parallel", func() {
txs := tc.buildTxs(txNumPerBlock, baseGP, &gpOffset, tc.gpDecrease, tc.needMultiple)
txs := tc.buildTxs(tc.tpb[i], baseGP, &gpOffset, tc.gpDecrease, tc.needMultiple)
txsBytes := make([][]byte, 0)
for _, tx := range txs {
tx.Sign(evmChainID, suite.evmSenderPrivKey.ToECDSA())
Expand All @@ -268,10 +354,13 @@ func (suite *FakeBlockRecommendGPTestSuite) TestRecommendGP() {
}
})
//fmt.Println("totalGasUsed: ", totalGasUsed)
suite.Require().True(totalGasUsed == tc.expectedTotalGU[i], "block gas expect %d, but %d ", tc.expectedTotalGU, totalGasUsed)
suite.Require().True(totalGasUsed == tc.expectedTotalGU[i], "block gas expect %d, but %d ", tc.expectedTotalGU[i], totalGasUsed)
suite.endFakeBlock(tc.expectedRecommendGp[i])
height++
}
for !suite.app.gpo.BlockGPQueue.IsEmpty() {
suite.app.gpo.BlockGPQueue.Pop()
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion app/test_helpers.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package app

import (
"github.com/spf13/viper"

"github.com/okex/exchain/libs/cosmos-sdk/codec"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
abci "github.com/okex/exchain/libs/tendermint/abci/types"
"github.com/okex/exchain/libs/tendermint/libs/log"
"github.com/okex/exchain/libs/tendermint/types"
dbm "github.com/okex/exchain/libs/tm-db"
"github.com/spf13/viper"
)

type Option func(option *SetupOption)
Expand Down

0 comments on commit a8d4ecf

Please sign in to comment.