Skip to content

Commit

Permalink
Update Precompile Input Method + Evm Balance
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbhat1 committed Mar 28, 2024
1 parent 6e0c934 commit 74da22f
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 13 deletions.
5 changes: 4 additions & 1 deletion precompiles/addr/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ func NewPrecompile(evmKeeper pcommon.EVMKeeper) (*Precompile, error) {

// RequiredGas returns the required bare minimum gas to execute the precompile.
func (p Precompile) RequiredGas(input []byte) uint64 {
methodID := input[:4]
methodID, err := pcommon.ExtractMethodID(input)
if err != nil {
return 0
}

method, err := p.ABI.MethodById(methodID)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions precompiles/bank/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ func NewPrecompile(bankKeeper pcommon.BankKeeper, evmKeeper pcommon.EVMKeeper) (

// RequiredGas returns the required bare minimum gas to execute the precompile.
func (p Precompile) RequiredGas(input []byte) uint64 {
methodID := input[:4]
methodID, err := pcommon.ExtractMethodID(input)
if err != nil {
return 0
}

method, err := p.ABI.MethodById(methodID)
if err != nil {
Expand Down Expand Up @@ -127,7 +130,6 @@ func (p Precompile) Run(evm *vm.EVM, caller common.Address, input []byte, value
}
return p.send(ctx, method, args, value)
case SendNativeMethod:
// TODO: Add validation on caller separate from validation above
return p.sendNative(ctx, method, args, caller, value)
case BalanceMethod:
return p.balance(ctx, method, args, value)
Expand Down Expand Up @@ -255,7 +257,7 @@ func (p Precompile) symbol(ctx sdk.Context, method *abi.Method, args []interface

func (p Precompile) decimals(_ sdk.Context, method *abi.Method, _ []interface{}, value *big.Int) ([]byte, error) {
pcommon.AssertNonPayable(value)
// all native tokens are integer-based
// all native tokens are integer-based, returns decimals for microdenom (usei)
return method.Outputs.Pack(uint8(0))
}

Expand Down
13 changes: 12 additions & 1 deletion precompiles/common/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func (p Precompile) Prepare(evm *vm.EVM, input []byte) (sdk.Context, *abi.Method
if !ok {
return sdk.Context{}, nil, nil, errors.New("cannot get context from EVM")
}
methodID := input[:4]
methodID, err := ExtractMethodID(input)
if err != nil {
return sdk.Context{}, nil, nil, err
}
method, err := p.ABI.MethodById(methodID)
if err != nil {
return sdk.Context{}, nil, nil, err
Expand Down Expand Up @@ -102,3 +105,11 @@ func ValidateCaller(ctx sdk.Context, evmKeeper EVMKeeper, caller common.Address,
}
return fmt.Errorf("calling contract %s with code hash %s is not whitelisted for delegate calls", callingContract.Hex(), codeHash.Hex())
}

func ExtractMethodID(input []byte) ([]byte, error) {
// Check if the input has at least the length needed for methodID
if len(input) < 4 {
return nil, errors.New("input too short to extract method ID")
}
return input[:4], nil
}
5 changes: 4 additions & 1 deletion precompiles/distribution/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ func NewPrecompile(distrKeeper pcommon.DistributionKeeper, evmKeeper pcommon.EVM

// RequiredGas returns the required bare minimum gas to execute the precompile.
func (p Precompile) RequiredGas(input []byte) uint64 {
methodID := input[:4]
methodID, err := pcommon.ExtractMethodID(input)
if err != nil {
return 0
}

if bytes.Equal(methodID, p.SetWithdrawAddrID) {
return 30000
Expand Down
5 changes: 4 additions & 1 deletion precompiles/gov/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ func NewPrecompile(govKeeper pcommon.GovKeeper, evmKeeper pcommon.EVMKeeper, ban

// RequiredGas returns the required bare minimum gas to execute the precompile.
func (p Precompile) RequiredGas(input []byte) uint64 {
methodID := input[:4]
methodID, err := pcommon.ExtractMethodID(input)
if err != nil {
return 0
}

if bytes.Equal(methodID, p.VoteID) {
return 30000
Expand Down
5 changes: 4 additions & 1 deletion precompiles/ibc/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ func NewPrecompile(transferKeeper pcommon.TransferKeeper, evmKeeper pcommon.EVMK

// RequiredGas returns the required bare minimum gas to execute the precompile.
func (p Precompile) RequiredGas(input []byte) uint64 {
methodID := input[:4]
methodID, err := pcommon.ExtractMethodID(input)
if err != nil {
return 0
}

method, err := p.ABI.MethodById(methodID)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions precompiles/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"embed"
gjson "encoding/json"
"errors"
"fmt"
"math/big"
"strings"
Expand Down Expand Up @@ -73,6 +74,9 @@ func NewPrecompile() (*Precompile, error) {

// RequiredGas returns the required bare minimum gas to execute the precompile.
func (p Precompile) RequiredGas(input []byte) uint64 {
if len(input) < 4 {
return 0
}
return uint64(GasCostPerByte * (len(input) - 4))
}

Expand Down Expand Up @@ -101,6 +105,11 @@ func (p Precompile) Run(evm *vm.EVM, _ common.Address, input []byte, value *big.
if err != nil {
return nil, err
}

if uint_.BitLen() > 256 {
return nil, errors.New("value does not fit in 32 bytes")
}

uint_.FillBytes(byteArr)
return byteArr, nil
}
Expand Down
5 changes: 4 additions & 1 deletion precompiles/oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ func NewPrecompile(oracleKeeper pcommon.OracleKeeper, evmKeeper pcommon.EVMKeepe

// RequiredGas returns the required bare minimum gas to execute the precompile.
func (p Precompile) RequiredGas(input []byte) uint64 {
methodID := input[:4]
methodID, err := pcommon.ExtractMethodID(input)
if err != nil {
return 0
}

method, err := p.ABI.MethodById(methodID)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion precompiles/staking/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ func NewPrecompile(stakingKeeper pcommon.StakingKeeper, evmKeeper pcommon.EVMKee

// RequiredGas returns the required bare minimum gas to execute the precompile.
func (p Precompile) RequiredGas(input []byte) uint64 {
methodID := input[:4]
methodID, err := pcommon.ExtractMethodID(input)
if err != nil {
return 0
}

if bytes.Equal(methodID, p.DelegateID) {
return 50000
Expand Down
5 changes: 4 additions & 1 deletion precompiles/wasmd/wasmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ func NewPrecompile(evmKeeper pcommon.EVMKeeper, wasmdKeeper pcommon.WasmdKeeper,

// RequiredGas returns the required bare minimum gas to execute the precompile.
func (p Precompile) RequiredGas(input []byte) uint64 {
methodID := input[:4]
methodID, err := pcommon.ExtractMethodID(input)
if err != nil {
return 0
}

method, err := p.ABI.MethodById(methodID)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions x/evm/state/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func (s *DBImpl) Exist(addr common.Address) bool {
return true
}

// check if account has a balance
if s.GetBalance(addr).Cmp(utils.Big0) > 0 {
return true
}

// go-ethereum impl considers just-deleted accounts as "exist" as well
return s.HasSelfDestructed(addr)
}
Expand Down
9 changes: 7 additions & 2 deletions x/evm/state/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ func TestExist(t *testing.T) {
statedb.SetCode(addr2, []byte{3})
require.True(t, statedb.Exist(addr2))

// destructed
// has balance
_, addr3 := testkeeper.MockAddressPair()
statedb.SelfDestruct(addr3)
statedb.AddBalance(addr3, big.NewInt(1000000000000))
require.True(t, statedb.Exist(addr3))

// destructed
_, addr4 := testkeeper.MockAddressPair()
statedb.SelfDestruct(addr4)
require.True(t, statedb.Exist(addr4))
}

func TestEmpty(t *testing.T) {
Expand Down

0 comments on commit 74da22f

Please sign in to comment.