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

feat: improve gas estimation #432

Merged
merged 2 commits into from
Sep 5, 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
8 changes: 6 additions & 2 deletions chain/evm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type PalomaClienter interface {
QueryLastObservedSkywayNonceByAddr(ctx context.Context, chainReferenceID string, orchestrator string) (uint64, error)
QueryBatchRequestByNonce(ctx context.Context, nonce uint64, contract string) (skywaytypes.OutgoingTxBatch, error)
QueryGetLatestPublishedSnapshot(ctx context.Context, chainReferenceID string) (*valset.Snapshot, error)
QueryGetSnapshotByID(ctx context.Context, id uint64) (*valset.Snapshot, error)
}

type Client struct {
Expand Down Expand Up @@ -351,16 +352,19 @@ func callSmartContract(
logger.WithFields(log.Fields{
"gas-limit": gasLimit,
}).Debug("estimated gas limit")
txOpts.GasLimit = uint64(float64(gasLimit) * 1.5)

// In case we only want to estimate, now is the time to return.
if args.opts.estimateOnly {
return ethtypes.NewTx(
&ethtypes.LegacyTx{
Gas: txOpts.GasLimit,
Gas: gasLimit,
})
}

// 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.txType == 2 {
txOpts.GasFeeCap = gasPrice
txOpts.GasTipCap = gasTipCap
Expand Down
2 changes: 1 addition & 1 deletion chain/evm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestExecutingSmartContract(t *testing.T) {
args.ethClient = ethMock
args.opts.estimateOnly = true
},
expectedEstimated: &([]uint64{333})[0], // afford for 1.5 multiplier
expectedEstimated: &([]uint64{222})[0], // will not be with the 1.5 multiplier safety margin
},
} {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions chain/evm/compass.go
Original file line number Diff line number Diff line change
Expand Up @@ -1601,12 +1601,12 @@ func (t compass) performValsetIDCrosscheck(ctx context.Context, chainReferenceID
func (t compass) findAssigneeEthAddress(ctx context.Context,
palomaAddress string,
) (common.Address, error) {
valset, err := t.paloma.QueryGetLatestPublishedSnapshot(ctx, t.ChainReferenceID)
snapshot, err := t.paloma.QueryGetSnapshotByID(ctx, 0)
if err != nil {
return common.Address{}, err
}

for _, v := range valset.Validators {
for _, v := range snapshot.Validators {
if v.Address.String() == palomaAddress {
for _, ci := range v.ExternalChainInfos {
if ci.ChainReferenceID == t.ChainReferenceID {
Expand Down
208 changes: 177 additions & 31 deletions chain/evm/compass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,23 +320,21 @@ func TestMessageProcessing(t *testing.T) {
nil,
)

paloma.On("QueryGetLatestPublishedSnapshot", mock.Anything, "internal-chain-id").Return(
&valsettypes.Snapshot{
Id: 55,
Validators: []valsettypes.Validator{
{
ExternalChainInfos: []*valsettypes.ExternalChainInfo{
{
ChainReferenceID: "internal-chain-id",
Address: "0xDEADBEEF0ba39494ce839613fffba74279579268",
},
sn := &valsettypes.Snapshot{
Id: 55,
Validators: []valsettypes.Validator{
{
ExternalChainInfos: []*valsettypes.ExternalChainInfo{
{
ChainReferenceID: "internal-chain-id",
Address: "0xDEADBEEF0ba39494ce839613fffba74279579268",
},
Address: sdk.ValAddress("validator-1").Bytes(),
},
Address: sdk.ValAddress("validator-1").Bytes(),
},
},
nil,
)
}
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(sn, nil)

return evm, paloma
},
Expand Down Expand Up @@ -421,6 +419,23 @@ func TestMessageProcessing(t *testing.T) {
},
nil,
)
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(
&valsettypes.Snapshot{
Id: 55,
Validators: []valsettypes.Validator{
{
ExternalChainInfos: []*valsettypes.ExternalChainInfo{
{
ChainReferenceID: "internal-chain-id",
Address: "0xDEADBEEF0ba39494ce839613fffba74279579268",
},
},
Address: sdk.ValAddress("validator-1").Bytes(),
},
},
},
nil,
)

evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{}, "submit_logic_call", mock.Anything).Return(
tx,
Expand Down Expand Up @@ -502,6 +517,23 @@ func TestMessageProcessing(t *testing.T) {
},
nil,
)
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(
&valsettypes.Snapshot{
Id: 55,
Validators: []valsettypes.Validator{
{
ExternalChainInfos: []*valsettypes.ExternalChainInfo{
{
ChainReferenceID: "internal-chain-id",
Address: "0xDEADBEEF0ba39494ce839613fffba74279579268",
},
},
Address: sdk.ValAddress("validator-1").Bytes(),
},
},
},
nil,
)

evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{estimateOnly: true}, "submit_logic_call", mock.Anything).Return(
tx,
Expand Down Expand Up @@ -569,6 +601,23 @@ func TestMessageProcessing(t *testing.T) {
},
nil,
)
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(
&valsettypes.Snapshot{
Id: 56,
Validators: []valsettypes.Validator{
{
ExternalChainInfos: []*valsettypes.ExternalChainInfo{
{
ChainReferenceID: "internal-chain-id",
Address: "0xDEADBEEF0ba39494ce839613fffba74279579268",
},
},
Address: sdk.ValAddress("validator-1").Bytes(),
},
},
},
nil,
)
paloma.On("NewStatus").Return(&StatusUpdater{})
return evm, paloma
},
Expand Down Expand Up @@ -629,6 +678,23 @@ func TestMessageProcessing(t *testing.T) {
},
nil,
)
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(
&valsettypes.Snapshot{
Id: 56,
Validators: []valsettypes.Validator{
{
ExternalChainInfos: []*valsettypes.ExternalChainInfo{
{
ChainReferenceID: "internal-chain-id",
Address: "0xDEADBEEF0ba39494ce839613fffba74279579268",
},
},
Address: sdk.ValAddress("validator-1").Bytes(),
},
},
},
nil,
)
paloma.On("NewStatus").Return(&StatusUpdater{})
return evm, paloma
},
Expand Down Expand Up @@ -715,6 +781,23 @@ func TestMessageProcessing(t *testing.T) {
},
nil,
)
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(
&valsettypes.Snapshot{
Id: 55,
Validators: []valsettypes.Validator{
{
ExternalChainInfos: []*valsettypes.ExternalChainInfo{
{
ChainReferenceID: "internal-chain-id",
Address: "0xDEADBEEF0ba39494ce839613fffba74279579268",
},
},
Address: sdk.ValAddress("validator-1").Bytes(),
},
},
},
nil,
)

evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{useMevRelay: true}, "submit_logic_call", mock.Anything).Return(
tx,
Expand Down Expand Up @@ -810,6 +893,23 @@ func TestMessageProcessing(t *testing.T) {
},
nil,
)
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(
&valsettypes.Snapshot{
Id: 55,
Validators: []valsettypes.Validator{
{
ExternalChainInfos: []*valsettypes.ExternalChainInfo{
{
ChainReferenceID: "internal-chain-id",
Address: "0xDEADBEEF0ba39494ce839613fffba74279579268",
},
},
Address: sdk.ValAddress("validator-1").Bytes(),
},
},
},
nil,
)
evm.On("FindCurrentBlockNumber", mock.Anything).Return(
big.NewInt(0),
nil,
Expand Down Expand Up @@ -889,6 +989,23 @@ func TestMessageProcessing(t *testing.T) {
},
nil,
)
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(
&valsettypes.Snapshot{
Id: 55,
Validators: []valsettypes.Validator{
{
ExternalChainInfos: []*valsettypes.ExternalChainInfo{
{
ChainReferenceID: "internal-chain-id",
Address: "0xDEADBEEF0ba39494ce839613fffba74279579268",
},
},
Address: sdk.ValAddress("validator-1").Bytes(),
},
},
},
nil,
)

evm.On("ExecuteSmartContract", mock.Anything, chainID, mock.Anything, smartContractAddr, callOptions{useMevRelay: true, estimateOnly: true}, "submit_logic_call", mock.Anything).Return(
tx,
Expand Down Expand Up @@ -975,6 +1092,23 @@ func TestMessageProcessing(t *testing.T) {
},
nil,
)
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(
&valsettypes.Snapshot{
Id: 55,
Validators: []valsettypes.Validator{
{
ExternalChainInfos: []*valsettypes.ExternalChainInfo{
{
ChainReferenceID: "internal-chain-id",
Address: "0xDEADBEEF0ba39494ce839613fffba74279579268",
},
},
Address: sdk.ValAddress("validator-1").Bytes(),
},
},
},
nil,
)

return evm, paloma
},
Expand Down Expand Up @@ -1030,6 +1164,23 @@ func TestMessageProcessing(t *testing.T) {
},
nil,
)
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(
&valsettypes.Snapshot{
Id: 55,
Validators: []valsettypes.Validator{
{
ExternalChainInfos: []*valsettypes.ExternalChainInfo{
{
ChainReferenceID: "internal-chain-id",
Address: "0xDEADBEEF0ba39494ce839613fffba74279579268",
},
},
Address: sdk.ValAddress("validator-1").Bytes(),
},
},
},
nil,
)
paloma.On("QueryGetLatestPublishedSnapshot", mock.Anything, "internal-chain-id").Return(
&valsettypes.Snapshot{
Id: 55,
Expand Down Expand Up @@ -1112,7 +1263,7 @@ func TestMessageProcessing(t *testing.T) {
},
nil,
)
paloma.On("QueryGetLatestPublishedSnapshot", mock.Anything, "internal-chain-id").Return(
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(
&valsettypes.Snapshot{
Validators: []valsettypes.Validator{
{
Expand Down Expand Up @@ -1197,7 +1348,7 @@ func TestMessageProcessing(t *testing.T) {
},
nil,
)
paloma.On("QueryGetLatestPublishedSnapshot", mock.Anything, "internal-chain-id").Return(
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(
&valsettypes.Snapshot{
Validators: []valsettypes.Validator{
{
Expand Down Expand Up @@ -1279,7 +1430,7 @@ func TestMessageProcessing(t *testing.T) {
},
nil,
)
paloma.On("QueryGetLatestPublishedSnapshot", mock.Anything, "internal-chain-id").Return(
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(
&valsettypes.Snapshot{
Validators: []valsettypes.Validator{
{
Expand Down Expand Up @@ -1358,25 +1509,20 @@ func TestMessageProcessing(t *testing.T) {
powerFromPercentage(0.4),
},
ValsetID: uint64(currentValsetID),
},
nil,
)
paloma.On("QueryGetLatestPublishedSnapshot", mock.Anything, "internal-chain-id").Return(
&valsettypes.Snapshot{
Validators: []valsettypes.Validator{
{
ExternalChainInfos: []*valsettypes.ExternalChainInfo{
{
ChainReferenceID: "internal-chain-id",
Address: "0xDEADBEEF0ba39494ce839613fffba74279579268",
},
}, nil)
paloma.On("QueryGetSnapshotByID", mock.Anything, uint64(0)).Return(&valsettypes.Snapshot{
Validators: []valsettypes.Validator{
{
ExternalChainInfos: []*valsettypes.ExternalChainInfo{
{
ChainReferenceID: "internal-chain-id",
Address: "0xDEADBEEF0ba39494ce839613fffba74279579268",
},
Address: sdk.ValAddress("validator-1").Bytes(),
},
Address: sdk.ValAddress("validator-1").Bytes(),
},
},
nil,
)
}, nil)
return evm, paloma
},
},
Expand Down
Loading
Loading