Skip to content

Commit fca41b3

Browse files
authored
fix: gas limit from estimation (#447)
* fix: use pigeon gas estimate as gas limit * fix: get rid of import cycle on tests * chore: update mocks * chore: revert gas-adjustment on paloma txs * fix: mock calls of ExecuteSmartContract
1 parent dbe9027 commit fca41b3

19 files changed

+68
-78
lines changed

chain/evm/client.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ type executeSmartContractIn struct {
245245
method string
246246
arguments []any
247247
opts callOptions
248+
249+
gasEstimate *big.Int
248250
}
249251

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

369376
if args.txType == 2 {
370377
txOpts.GasFeeCap = gasPrice
@@ -664,6 +671,7 @@ func (c *Client) ExecuteSmartContract(
664671
opts callOptions,
665672
method string,
666673
arguments []any,
674+
gasEstimate *big.Int,
667675
) (*etherumtypes.Transaction, error) {
668676
var mevClient mevClient = nil
669677
if opts.useMevRelay {
@@ -685,8 +693,9 @@ func (c *Client) ExecuteSmartContract(
685693
keystore: c.keystore,
686694
opts: opts,
687695

688-
method: method,
689-
arguments: arguments,
696+
method: method,
697+
arguments: arguments,
698+
gasEstimate: gasEstimate,
690699
},
691700
)
692701
}

chain/evm/compass.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var errValsetIDMismatch = errors.New("valset id mismatch")
5555
//go:generate mockery --name=evmClienter --inpackage --testonly
5656
type evmClienter interface {
5757
FilterLogs(ctx context.Context, fq ethereum.FilterQuery, currBlockHeight *big.Int, fn func(logs []ethtypes.Log) bool) (bool, error)
58-
ExecuteSmartContract(ctx context.Context, chainID *big.Int, contractAbi abi.ABI, addr common.Address, opts callOptions, method string, arguments []any) (*ethtypes.Transaction, error)
58+
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)
5959
DeployContract(ctx context.Context, chainID *big.Int, rawABI string, bytecode, constructorInput []byte) (contractAddr common.Address, tx *ethtypes.Transaction, err error)
6060
TransactionByHash(ctx context.Context, txHash common.Hash) (*ethtypes.Transaction, bool, error)
6161
TransactionReceipt(ctx context.Context, txHash common.Hash) (*ethtypes.Receipt, error)
@@ -181,12 +181,13 @@ func (t compass) updateValset(
181181
// TODO: Use generated contract code directly
182182
// compass 2.0.0
183183
// def update_valset(consensus: Consensus, new_valset: Valset, relayer: address, gas_estimate: uint256)
184-
tx, err := t.callCompass(ctx, opts, "update_valset", []any{
184+
args := []any{
185185
BuildCompassConsensus(currentValset, origMessage.Signatures),
186186
TransformValsetToCompassValset(newValset),
187187
ethSender,
188188
estimate,
189-
})
189+
}
190+
tx, err := t.callCompass(ctx, opts, "update_valset", args, estimate)
190191
if err != nil {
191192
return nil, 0, err
192193
}
@@ -268,7 +269,7 @@ func (t compass) submitLogicCall(
268269
opts.useMevRelay = true
269270
}
270271
logger.WithField("consensus", con).WithField("args", args).Debug("submitting logic call")
271-
tx, err := t.callCompass(ctx, opts, "submit_logic_call", args)
272+
tx, err := t.callCompass(ctx, opts, "submit_logic_call", args, nil)
272273
if err != nil {
273274
return nil, 0, err
274275
}
@@ -334,7 +335,7 @@ func (t compass) compass_handover(
334335
}
335336

336337
logger.WithField("consensus", con).WithField("args", args).Debug("compass handover")
337-
tx, err := t.callCompass(ctx, opts, "compass_update_batch", args)
338+
tx, err := t.callCompass(ctx, opts, "compass_update_batch", args, estimate)
338339
if err != nil {
339340
return nil, 0, err
340341
}
@@ -450,7 +451,7 @@ func (t compass) uploadUserSmartContract(
450451

451452
logger.WithField("consensus", con).WithField("args", args).
452453
Debug("deploying user smart contract")
453-
tx, err := t.callCompass(ctx, opts, "deploy_contract", args)
454+
tx, err := t.callCompass(ctx, opts, "deploy_contract", args, nil)
454455
if err != nil {
455456
return nil, 0, err
456457
}
@@ -1417,11 +1418,12 @@ func (c compass) callCompass(
14171418
opts callOptions,
14181419
method string,
14191420
arguments []any,
1421+
gasEstimate *big.Int,
14201422
) (*ethtypes.Transaction, error) {
14211423
if c.compassAbi == nil {
14221424
return nil, ErrABINotInitialized
14231425
}
1424-
return c.evm.ExecuteSmartContract(ctx, c.chainID, *c.compassAbi, c.smartContractAddr, opts, method, arguments)
1426+
return c.evm.ExecuteSmartContract(ctx, c.chainID, *c.compassAbi, c.smartContractAddr, opts, method, arguments, gasEstimate)
14251427
}
14261428

14271429
func (t compass) skywayRelayBatches(ctx context.Context, batches []chain.SkywayBatchWithSignatures) error {
@@ -1573,6 +1575,7 @@ func (t compass) skywayRelayBatch(
15731575
ethSender,
15741576
estimate,
15751577
},
1578+
estimate,
15761579
)
15771580
if err != nil {
15781581
logger.WithError(err).Error("failed to relay batch")

chain/evm/compass_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ func TestMessageProcessing(t *testing.T) {
443443
nil,
444444
)
445445

446-
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{}, "submit_logic_call", mock.Anything).Return(
446+
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{}, "submit_logic_call", mock.Anything, mock.Anything).Return(
447447
tx,
448448
nil,
449449
)
@@ -541,7 +541,7 @@ func TestMessageProcessing(t *testing.T) {
541541
nil,
542542
)
543543

544-
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{estimateOnly: true}, "submit_logic_call", mock.Anything).Return(
544+
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{estimateOnly: true}, "submit_logic_call", mock.Anything, mock.Anything).Return(
545545
tx,
546546
nil,
547547
)
@@ -805,7 +805,7 @@ func TestMessageProcessing(t *testing.T) {
805805
nil,
806806
)
807807

808-
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{useMevRelay: true}, "submit_logic_call", mock.Anything).Return(
808+
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{useMevRelay: true}, "submit_logic_call", mock.Anything, mock.Anything).Return(
809809
tx,
810810
nil,
811811
)
@@ -1013,7 +1013,7 @@ func TestMessageProcessing(t *testing.T) {
10131013
nil,
10141014
)
10151015

1016-
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{useMevRelay: true, estimateOnly: true}, "submit_logic_call", mock.Anything).Return(
1016+
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{useMevRelay: true, estimateOnly: true}, "submit_logic_call", mock.Anything, mock.Anything).Return(
10171017
tx,
10181018
nil,
10191019
)
@@ -1286,7 +1286,7 @@ func TestMessageProcessing(t *testing.T) {
12861286
nil,
12871287
)
12881288

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

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

1374-
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{estimateOnly: true}, "update_valset", mock.Anything).Return(tx, nil)
1374+
evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{estimateOnly: true}, "update_valset", mock.Anything, mock.Anything).Return(tx, nil)
13751375
return evm, paloma
13761376
},
13771377
},

chain/evm/mock_ethClientConn_test.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain/evm/mock_ethClientToFilterLogs_test.go

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain/evm/mock_ethClienter_test.go

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain/evm/mock_evmClienter_test.go

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain/evm/mock_mevClient_test.go

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain/evm/mocks/CompassBinding.go

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain/evm/mocks/PalomaClienter.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain/mocks/Processor.go

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain/paloma/client.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,14 @@ import (
1919
"github.com/palomachain/pigeon/util/slice"
2020
)
2121

22-
type (
23-
ResultStatus = coretypes.ResultStatus
24-
Unpacker = codectypes.AnyUnpacker
25-
)
26-
2722
//go:generate mockery --name=MessageSender
2823
type MessageSender interface {
2924
SendMsg(ctx context.Context, msg sdk.Msg, memo string, opts ...ion.SendMsgOption) (*sdk.TxResponse, error)
3025
}
3126

3227
//go:generate mockery --name=IonClient
3328
type IonClient interface {
34-
Status(context.Context) (*ResultStatus, error)
29+
Status(context.Context) (*coretypes.ResultStatus, error)
3530
DecodeBech32ValAddr(string) (sdk.ValAddress, error)
3631
GetKeybase() keyring.Keyring
3732
SetSDKContext() func()
@@ -43,7 +38,7 @@ type Client struct {
4338
GRPCClient grpc.ClientConn
4439

4540
ic IonClient
46-
unpacker Unpacker
41+
unpacker codectypes.AnyUnpacker
4742
messageSender MessageSender
4843
sendingOpts []ion.SendMsgOption
4944

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

217-
func (c *Client) Status(ctx context.Context) (*ResultStatus, error) {
212+
func (c *Client) Status(ctx context.Context) (*coretypes.ResultStatus, error) {
218213
res, err := c.ic.Status(ctx)
219214
if err != nil {
220215
return nil, err

chain/paloma/mocks/IonClient.go

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)