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

fix: gas limit from estimation #447

Merged
merged 5 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions chain/evm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ type executeSmartContractIn struct {
method string
arguments []any
opts callOptions

gasEstimate *big.Int
}

func callSmartContract(
Expand Down Expand Up @@ -365,6 +367,11 @@ func callSmartContract(
// Once estimation is finished, we adjust the gas limit
// to be sure that the tx will be included in the next block.
txOpts.GasLimit = uint64(float64(gasLimit) * 1.5)
if args.gasEstimate != nil && args.gasEstimate.Uint64() > txOpts.GasLimit {
// If we have a gas estimate from pigeons, and it is greater than
// what we just got, we use it
txOpts.GasLimit = args.gasEstimate.Uint64()
}

if args.txType == 2 {
txOpts.GasFeeCap = gasPrice
Expand Down Expand Up @@ -664,6 +671,7 @@ func (c *Client) ExecuteSmartContract(
opts callOptions,
method string,
arguments []any,
gasEstimate *big.Int,
) (*etherumtypes.Transaction, error) {
var mevClient mevClient = nil
if opts.useMevRelay {
Expand All @@ -685,8 +693,9 @@ func (c *Client) ExecuteSmartContract(
keystore: c.keystore,
opts: opts,

method: method,
arguments: arguments,
method: method,
arguments: arguments,
gasEstimate: gasEstimate,
},
)
}
Expand Down
17 changes: 10 additions & 7 deletions chain/evm/compass.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var errValsetIDMismatch = errors.New("valset id mismatch")
//go:generate mockery --name=evmClienter --inpackage --testonly
type evmClienter interface {
FilterLogs(ctx context.Context, fq ethereum.FilterQuery, currBlockHeight *big.Int, fn func(logs []ethtypes.Log) bool) (bool, error)
ExecuteSmartContract(ctx context.Context, chainID *big.Int, contractAbi abi.ABI, addr common.Address, opts callOptions, method string, arguments []any) (*ethtypes.Transaction, error)
ExecuteSmartContract(ctx context.Context, chainID *big.Int, contractAbi abi.ABI, addr common.Address, opts callOptions, method string, arguments []any, gasEstimate *big.Int) (*ethtypes.Transaction, error)
DeployContract(ctx context.Context, chainID *big.Int, rawABI string, bytecode, constructorInput []byte) (contractAddr common.Address, tx *ethtypes.Transaction, err error)
TransactionByHash(ctx context.Context, txHash common.Hash) (*ethtypes.Transaction, bool, error)
TransactionReceipt(ctx context.Context, txHash common.Hash) (*ethtypes.Receipt, error)
Expand Down Expand Up @@ -181,12 +181,13 @@ func (t compass) updateValset(
// TODO: Use generated contract code directly
// compass 2.0.0
// def update_valset(consensus: Consensus, new_valset: Valset, relayer: address, gas_estimate: uint256)
tx, err := t.callCompass(ctx, opts, "update_valset", []any{
args := []any{
BuildCompassConsensus(currentValset, origMessage.Signatures),
TransformValsetToCompassValset(newValset),
ethSender,
estimate,
})
}
tx, err := t.callCompass(ctx, opts, "update_valset", args, estimate)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -268,7 +269,7 @@ func (t compass) submitLogicCall(
opts.useMevRelay = true
}
logger.WithField("consensus", con).WithField("args", args).Debug("submitting logic call")
tx, err := t.callCompass(ctx, opts, "submit_logic_call", args)
tx, err := t.callCompass(ctx, opts, "submit_logic_call", args, nil)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -334,7 +335,7 @@ func (t compass) compass_handover(
}

logger.WithField("consensus", con).WithField("args", args).Debug("compass handover")
tx, err := t.callCompass(ctx, opts, "compass_update_batch", args)
tx, err := t.callCompass(ctx, opts, "compass_update_batch", args, estimate)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -450,7 +451,7 @@ func (t compass) uploadUserSmartContract(

logger.WithField("consensus", con).WithField("args", args).
Debug("deploying user smart contract")
tx, err := t.callCompass(ctx, opts, "deploy_contract", args)
tx, err := t.callCompass(ctx, opts, "deploy_contract", args, nil)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -1417,11 +1418,12 @@ func (c compass) callCompass(
opts callOptions,
method string,
arguments []any,
gasEstimate *big.Int,
) (*ethtypes.Transaction, error) {
if c.compassAbi == nil {
return nil, ErrABINotInitialized
}
return c.evm.ExecuteSmartContract(ctx, c.chainID, *c.compassAbi, c.smartContractAddr, opts, method, arguments)
return c.evm.ExecuteSmartContract(ctx, c.chainID, *c.compassAbi, c.smartContractAddr, opts, method, arguments, gasEstimate)
}

func (t compass) skywayRelayBatches(ctx context.Context, batches []chain.SkywayBatchWithSignatures) error {
Expand Down Expand Up @@ -1573,6 +1575,7 @@ func (t compass) skywayRelayBatch(
ethSender,
estimate,
},
estimate,
)
if err != nil {
logger.WithError(err).Error("failed to relay batch")
Expand Down
12 changes: 6 additions & 6 deletions chain/evm/compass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func TestMessageProcessing(t *testing.T) {
nil,
)

evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{}, "submit_logic_call", mock.Anything).Return(
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{}, "submit_logic_call", mock.Anything, mock.Anything).Return(
tx,
nil,
)
Expand Down Expand Up @@ -541,7 +541,7 @@ func TestMessageProcessing(t *testing.T) {
nil,
)

evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{estimateOnly: true}, "submit_logic_call", mock.Anything).Return(
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{estimateOnly: true}, "submit_logic_call", mock.Anything, mock.Anything).Return(
tx,
nil,
)
Expand Down Expand Up @@ -805,7 +805,7 @@ func TestMessageProcessing(t *testing.T) {
nil,
)

evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{useMevRelay: true}, "submit_logic_call", mock.Anything).Return(
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{useMevRelay: true}, "submit_logic_call", mock.Anything, mock.Anything).Return(
tx,
nil,
)
Expand Down Expand Up @@ -1013,7 +1013,7 @@ func TestMessageProcessing(t *testing.T) {
nil,
)

evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{useMevRelay: true, estimateOnly: true}, "submit_logic_call", mock.Anything).Return(
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{useMevRelay: true, estimateOnly: true}, "submit_logic_call", mock.Anything, mock.Anything).Return(
tx,
nil,
)
Expand Down Expand Up @@ -1286,7 +1286,7 @@ func TestMessageProcessing(t *testing.T) {
nil,
)

evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{}, "update_valset", mock.Anything).Return(tx, nil)
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{}, "update_valset", mock.Anything, mock.Anything).Return(tx, nil)

paloma.On("SetPublicAccessData", mock.Anything, "queue-name", uint64(555), uint64(55), tx.Hash().Bytes()).Return(nil)
return evm, paloma
Expand Down Expand Up @@ -1371,7 +1371,7 @@ func TestMessageProcessing(t *testing.T) {
nil,
)

evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{estimateOnly: true}, "update_valset", mock.Anything).Return(tx, nil)
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{estimateOnly: true}, "update_valset", mock.Anything, mock.Anything).Return(tx, nil)
return evm, paloma
},
},
Expand Down
2 changes: 1 addition & 1 deletion chain/evm/mock_ethClientConn_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions chain/evm/mock_ethClientToFilterLogs_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions chain/evm/mock_ethClienter_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions chain/evm/mock_evmClienter_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions chain/evm/mock_mevClient_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions chain/evm/mocks/CompassBinding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion chain/evm/mocks/PalomaClienter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions chain/mocks/Processor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions chain/paloma/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@ import (
"github.com/palomachain/pigeon/util/slice"
)

type (
ResultStatus = coretypes.ResultStatus
Unpacker = codectypes.AnyUnpacker
)

//go:generate mockery --name=MessageSender
type MessageSender interface {
SendMsg(ctx context.Context, msg sdk.Msg, memo string, opts ...ion.SendMsgOption) (*sdk.TxResponse, error)
}

//go:generate mockery --name=IonClient
type IonClient interface {
Status(context.Context) (*ResultStatus, error)
Status(context.Context) (*coretypes.ResultStatus, error)
DecodeBech32ValAddr(string) (sdk.ValAddress, error)
GetKeybase() keyring.Keyring
SetSDKContext() func()
Expand All @@ -43,7 +38,7 @@ type Client struct {
GRPCClient grpc.ClientConn

ic IonClient
unpacker Unpacker
unpacker codectypes.AnyUnpacker
messageSender MessageSender
sendingOpts []ion.SendMsgOption

Expand Down Expand Up @@ -214,7 +209,7 @@ func (c *Client) KeepValidatorAlive(ctx context.Context, appVersion string) erro
return err
}

func (c *Client) Status(ctx context.Context) (*ResultStatus, error) {
func (c *Client) Status(ctx context.Context) (*coretypes.ResultStatus, error) {
res, err := c.ic.Status(ctx)
if err != nil {
return nil, err
Expand Down
5 changes: 2 additions & 3 deletions chain/paloma/mocks/IonClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading