Skip to content

Commit

Permalink
feat: Add error message parsing for evm transactions in plan module (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sheldonleedev authored Aug 16, 2024
1 parent 2691e83 commit d35349d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/release_binary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ jobs:
runs-on: ubuntu-latest
environment: release
steps:
- name: Free Disk Space (Ubuntu)
uses: jlumbroso/free-disk-space@main
with:
# this might remove tools that are actually needed,
# if set to "true" but frees about 6 GB
tool-cache: false

# all of these default to true, but feel free to set to
# "false" if necessary for your workflow
android: true
dotnet: true
haskell: true
large-packages: true
docker-images: true
swap-storage: true
- uses: actions/checkout@v4
with:
submodules: true
Expand Down
2 changes: 1 addition & 1 deletion x/btcstaking/keeper/btc_staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (suite *KeeperTestSuite) TestDelegate() {
suite.Require().NoError(err)
btcStakingRecord := suite.keeper.GetBTCStakingRecord(suite.ctx, *txHash)
suite.Require().NotNil(btcStakingRecord)
suite.Require().Contains(btcStakingRecord.MintYatResult, "execution reverted")
suite.Require().Contains(btcStakingRecord.MintYatResult, "contract call failed")
},
expectErr: false,
},
Expand Down
43 changes: 42 additions & 1 deletion x/plan/keeper/evm.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package keeper

import (
"bytes"
"encoding/json"
"errors"
"math/big"

"github.com/ethereum/go-ethereum/crypto"
Expand All @@ -20,6 +22,13 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
)

var (
RevertSelectorInvalidParams = crypto.Keccak256([]byte("InvalidParams()"))[:4]
RevertSelectorEmptyMerkleRoot = crypto.Keccak256([]byte("EmptyMerkleRoot()"))[:4]
RevertSelectorAlreadyClaimed = crypto.Keccak256([]byte("AlreadyClaimed()"))[:4]
RevertSelectorInvalidMerkleProof = crypto.Keccak256([]byte("InvalidMerkleProof()"))[:4]
)

// CallEVM performs a smart contract method call using given args
func (k Keeper) CallEVM(
ctx sdk.Context,
Expand Down Expand Up @@ -68,7 +77,21 @@ func (k Keeper) CallEVMWithData(
return nil, errorsmod.Wrapf(errortypes.ErrJSONMarshal, "failed to marshal tx args: %s", err.Error())
}

// k.evmKeeper.
// k.evmKeeper.EthCall
callRes, err := k.evmKeeper.EthCall(sdk.WrapSDKContext(ctx), &evmtypes.EthCallRequest{
Args: args,
GasCap: config.DefaultGasCap,
ChainId: k.evmKeeper.ChainID().Int64(),
})
if err != nil {
return nil, err
}
if len(callRes.VmError) != 0 {
revertRetErr := UnpackRevert(callRes.Ret)
return nil, revertRetErr
}

// k.evmKeeper.EstimateGas
gasRes, err := k.evmKeeper.EstimateGas(sdk.WrapSDKContext(ctx), &evmtypes.EthCallRequest{
Args: args,
GasCap: config.DefaultGasCap,
Expand Down Expand Up @@ -152,3 +175,21 @@ func (k Keeper) DeployContract(

return contractAddr, nil
}

func UnpackRevert(data []byte) error {
if len(data) < 4 {
return errors.New("invalid data for unpacking")
}
switch {
case bytes.Equal(data[:4], RevertSelectorInvalidParams):
return errors.New("InvalidParams")
case bytes.Equal(data[:4], RevertSelectorEmptyMerkleRoot):
return errors.New("EmptyMerkleRoot")
case bytes.Equal(data[:4], RevertSelectorAlreadyClaimed):
return errors.New("AlreadyClaimed")
case bytes.Equal(data[:4], RevertSelectorInvalidMerkleProof):
return errors.New("InvalidMerkleProof")
}

return evmtypes.NewExecErrorWithReason(data)
}
1 change: 1 addition & 0 deletions x/plan/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type EVMKeeper interface {
GetParams(ctx sdk.Context) evmtypes.Params
GetAccountWithoutBalance(ctx sdk.Context, addr common.Address) *statedb.Account
EstimateGas(c context.Context, req *evmtypes.EthCallRequest) (*evmtypes.EstimateGasResponse, error)
EthCall(c context.Context, req *evmtypes.EthCallRequest) (*evmtypes.MsgEthereumTxResponse, error)
ApplyMessage(ctx sdk.Context, msg core.Message, tracer vm.EVMLogger, commit bool) (*evmtypes.MsgEthereumTxResponse, error)
ChainID() *big.Int
}
Expand Down

0 comments on commit d35349d

Please sign in to comment.