fix: Improve performance in priority extraction [BLO-686] (#347)

* change DefaultTxPriority calculation

* benchmarks

* linting

* move to new file

* nits

* nits + testing

* explicit validity

* compareCoins -> Greater
This commit is contained in:
Nikhil Vasan
2024-01-02 09:57:52 -08:00
committed by GitHub
parent 4c05d56d2c
commit 47fc466129
5 changed files with 398 additions and 130 deletions
-77
View File
@@ -9,84 +9,7 @@ import (
testutils "github.com/skip-mev/block-sdk/testutils"
)
func (s *BaseTestSuite) TestGetTxPriority() {
txPriority := base.DefaultTxPriority()
s.Run("should be able to get the priority off a normal transaction with fees", func() {
tx, err := testutils.CreateRandomTx(
s.encodingConfig.TxConfig,
s.accounts[0],
0,
0,
0,
0,
sdk.NewCoin(s.gasTokenDenom, math.NewInt(100)),
)
s.Require().NoError(err)
priority := txPriority.GetTxPriority(sdk.Context{}, tx)
s.Require().Equal(sdk.NewCoin(s.gasTokenDenom, math.NewInt(100)).String(), priority)
})
s.Run("should not get a priority when the transaction does not have a fee", func() {
tx, err := testutils.CreateRandomTx(
s.encodingConfig.TxConfig,
s.accounts[0],
0,
0,
0,
0,
)
s.Require().NoError(err)
priority := txPriority.GetTxPriority(sdk.Context{}, tx)
s.Require().Equal("", priority)
})
s.Run("should get a priority when the gas token is different", func() {
tx, err := testutils.CreateRandomTx(
s.encodingConfig.TxConfig,
s.accounts[0],
0,
0,
0,
0,
sdk.NewCoin("random", math.NewInt(100)),
)
s.Require().NoError(err)
priority := txPriority.GetTxPriority(sdk.Context{}, tx)
s.Require().Equal(sdk.NewCoin("random", math.NewInt(100)).String(), priority)
})
}
func (s *BaseTestSuite) TestCompareTxPriority() {
txPriority := base.DefaultTxPriority()
s.Run("should return 0 when both priorities are nil", func() {
a := sdk.NewCoin(s.gasTokenDenom, math.NewInt(0)).String()
b := sdk.NewCoin(s.gasTokenDenom, math.NewInt(0)).String()
s.Require().Equal(0, txPriority.Compare(a, b))
})
s.Run("should return 1 when the first priority is greater", func() {
a := sdk.NewCoin(s.gasTokenDenom, math.NewInt(100)).String()
b := sdk.NewCoin(s.gasTokenDenom, math.NewInt(1)).String()
s.Require().Equal(1, txPriority.Compare(a, b))
})
s.Run("should return -1 when the second priority is greater", func() {
a := sdk.NewCoin(s.gasTokenDenom, math.NewInt(1)).String()
b := sdk.NewCoin(s.gasTokenDenom, math.NewInt(100)).String()
s.Require().Equal(-1, txPriority.Compare(a, b))
})
s.Run("should return 0 when both priorities are equal", func() {
a := sdk.NewCoin(s.gasTokenDenom, math.NewInt(100)).String()
b := sdk.NewCoin(s.gasTokenDenom, math.NewInt(100)).String()
s.Require().Equal(0, txPriority.Compare(a, b))
})
lane := s.initLane(math.LegacyOneDec(), nil)
s.Run("should return -1 when signers are the same but the first tx has a higher sequence", func() {
-10
View File
@@ -39,11 +39,6 @@ func (s *BaseTestSuite) TestGetTxInfo() {
s.Require().Equal(signer.Address.String(), txInfo.Signers[0].Signer.String())
s.Require().Equal(nonce, txInfo.Signers[0].Sequence)
// Verify the priority
actualfee, err := sdk.ParseCoinsNormalized(txInfo.Priority.(string))
s.Require().NoError(err)
s.Require().Equal(fee, actualfee)
// Verify the gas limit
s.Require().Equal(gasLimit, txInfo.GasLimit)
@@ -82,11 +77,6 @@ func (s *BaseTestSuite) TestGetTxInfo() {
s.Require().Equal(signer.Address.String(), txInfo.Signers[0].Signer.String())
s.Require().Equal(nonce, txInfo.Signers[0].Sequence)
// Verify the priority
actualfee, err := sdk.ParseCoinsNormalized(txInfo.Priority.(string))
s.Require().NoError(err)
s.Require().Equal(fee, actualfee)
// Verify the bytes
txBz, err := s.encodingConfig.TxConfig.TxEncoder()(tx)
s.Require().NoError(err)