Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lo6165 price bump cap #62

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

dai1975
Copy link
Contributor

@dai1975 dai1975 commented Jan 30, 2025

  • fix price bump bug to set opts.Nonce before GasFeeCalculator#Apply
  • check latest(replacing) transaction's gas fee is already high enough
  • make IETHClient interface and use it in gas.go and txpool.go for testing

pkg/client/client.go Outdated Show resolved Hide resolved
gasFeeCap := targetTx.GasFeeCap.ToInt()
gasTipCap := targetTx.GasTipCap.ToInt()
gasFeeCap := new(big.Int).Set(targetTx.GasFeeCap.ToInt())
gasTipCap := new(big.Int).Set(targetTx.GasTipCap.ToInt())
Copy link
Contributor Author

@dai1975 dai1975 Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hexuti.Big.ToInt() returns same pointer of receiver instance and following inclByPercent() overwrite it. This make problems when targetTx object is stored and reused such as testing mock.

@siburu
Copy link
Contributor

siburu commented Feb 3, 2025

@dai1975 You have replaced the ETHClient struct with the IETHClient interface to mock it for testing. They are all defined in the pkg/client package.
However, actual tests that want to mock the ethereum client are not in pkg/client but in pkg/relay/ethereum (gas_test.go).
Therefore, I think, you didn't need to modify the pkg/client package and should only have modified the type of the client argument of NewGasFeeCalculator.

@siburu
Copy link
Contributor

siburu commented Feb 3, 2025

@dai1975 To easily mock all the functions of the pkg/client package and the pkg/client/txpool package, you may need to move the functions defined in the pkg/client/txpool package to the pkg/client and convert them into methods of the ETHClient struct.

Specifically, GetMinimumRequiredFee must be converted into a method of the ETHClient, since it is called from GasFeeCalculator.
However, I suggest to convert all the other functions (ContentFrom and PendingTransactions), too.

Daisuke Kanda added 4 commits February 4, 2025 00:15
Signed-off-by: Daisuke Kanda <[email protected]>
Signed-off-by: Daisuke Kanda <[email protected]>
if err := NewGasFeeCalculator(chain.client, &chain.config).Apply(ctx, txOpts); err != nil {
return nil, err
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

statement move to fix bug

@dai1975
Copy link
Contributor Author

dai1975 commented Feb 5, 2025

@siburu moved interface definition to ethereum package.

Comment on lines 63 to 71
func (cl *ChainClient) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
return cl.ETHClient.Client.SuggestGasPrice(ctx)
}
func (cl *ChainClient) HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error) {
return cl.ETHClient.Client.HeaderByNumber(ctx, number)
}
func (cl *ChainClient) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error) {
return cl.ETHClient.Client.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles)
}
Copy link
Contributor

@siburu siburu Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dai1975 You need not redefine these 3 functions. In the Go language, if an embedded struct implements an interface, the embedding struct also inherits the interface. Specifically, ChainClient inherits all the methods implemented by ethclient.Client.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, these codes are remains of try of struct version. Removed.

Comment on lines 51 to 56
type IChainClient interface {
SuggestGasPrice(ctx context.Context) (*big.Int, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*gethtypes.Header, error);
FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error)
GetMinimumRequiredFee(ctx context.Context, address common.Address, nonce uint64, priceBump uint64) (*txpool.RPCTransaction, *big.Int, *big.Int, error);
}
Copy link
Contributor

@siburu siburu Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dai1975
Do you know that go-ethereum defines ETH JSON RPC client interfaces in interfaces.go?
ethclient.Client implements all these interfaces, and so client.ETHClient and ChainClient inherit them.
I think it is better to pick up some or all of these interfaces and embed them into IChainClient like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know it. fixed.

@@ -61,6 +66,9 @@ func (m *GasFeeCalculator) Apply(ctx context.Context, txOpts *bind.TransactOpts)
// GasFeeCap = min(LimitFeePerGas, GasTipCap + BaseFee * BaseFeeRate)
m.config.DynamicTxGasConfig.BaseFeeRate.Mul(gasFeeCap)
gasFeeCap.Add(gasFeeCap, gasTipCap)
if oldTx != nil && oldTx.GasFeeCap != nil && oldTx.GasFeeCap.ToInt().Cmp(gasFeeCap) > 0 {
return fmt.Errorf("old tx's gasFeeCap(%v) is higher than suggestion(%v)", oldTx.GasFeeCap.ToInt(), gasFeeCap)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dai1975 I think that we should give up tx replacement only if both the tip and fee caps of the pending tx are higher than the new tx.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely. fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add error case when new caps are equals to old caps(Cmp returns zero)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants