Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
add CheckCaller
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Feb 3, 2023
1 parent 056cd11 commit f834a85
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
26 changes: 5 additions & 21 deletions x/evm/keeper/precompiles/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package precompiles
import (
"errors"
"math/big"
"runtime"
"strings"

sdkmath "cosmossdk.io/math"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -90,23 +88,9 @@ func (bc *BankContract) RequiredGas(input []byte) uint64 {
}

func (bc *BankContract) Run(input []byte) ([]byte, error) {
layer := 2
pc, _, _, _ := runtime.Caller(layer)
prefix := "github.com/ethereum/go-ethereum/core/vm.(*EVM)."
caller := runtime.FuncForPC(pc).Name()
readonly := false
if strings.Index(caller, prefix) == 0 {
fn := caller[len(prefix):]
switch fn {
case "Call":
readonly = false
case "CallCode":
case "DelegateCall":
case "StaticCall":
readonly = true
default:
return nil, errors.New("unknown caller")
}
readonly, err := CheckCaller()
if err != nil {
return nil, err
}
// parse input
methodID := input[:4]
Expand Down Expand Up @@ -147,8 +131,8 @@ func (bc *BankContract) Run(input []byte) ([]byte, error) {
token := args[0].(common.Address)
addr := args[1].(common.Address)
// query from storage
amount := bc.bankKeeper.GetBalance(bc.ctx, sdk.AccAddress(addr.Bytes()), EVMDenom(token)).Amount.BigInt()
return BalanceOfMethod.Outputs.Pack(amount)
balance := bc.bankKeeper.GetBalance(bc.ctx, sdk.AccAddress(addr.Bytes()), EVMDenom(token)).Amount.BigInt()
return BalanceOfMethod.Outputs.Pack(balance)
default:
return nil, errors.New("unknown method")
}
Expand Down
29 changes: 29 additions & 0 deletions x/evm/keeper/precompiles/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package precompiles

import (
"errors"
"runtime"
"strings"
)

func CheckCaller() (bool, error) {
layer := 2
pc, _, _, _ := runtime.Caller(layer)
prefix := "github.com/ethereum/go-ethereum/core/vm.(*EVM)."
caller := runtime.FuncForPC(pc).Name()
readonly := false
if strings.Index(caller, prefix) == 0 {
fn := caller[len(prefix):]
switch fn {
case "Call":
readonly = false
case "CallCode":
case "DelegateCall":
case "StaticCall":
readonly = true
default:
return readonly, errors.New("unknown caller")
}
}
return readonly, nil
}

0 comments on commit f834a85

Please sign in to comment.