Skip to content

Commit

Permalink
first batch of reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Oct 25, 2024
1 parent 7243c80 commit 95c9237
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
6 changes: 6 additions & 0 deletions precompiles/types/address.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
bank "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -12,6 +14,10 @@ import (
// If contract.CallerAddress != evm.Origin is true, it means the call was made through a contract,
// on which case there is a need to set the caller to the evm.Origin.
func GetEVMCallerAddress(evm *vm.EVM, contract *vm.Contract) (common.Address, error) {
if evm == nil || contract == nil {
return common.Address{}, errors.New("invalid input: evm or contract is nil")
}

caller := contract.CallerAddress
if contract.CallerAddress != evm.Origin {
caller = evm.Origin
Expand Down
45 changes: 34 additions & 11 deletions precompiles/types/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,41 @@ import (
)

func Test_GetEVMCallerAddress(t *testing.T) {
t.Run("should raise error when evm is nil", func(t *testing.T) {
_, mockVMContract := setupMockEVMAndContract(common.Address{})
caller, err := GetEVMCallerAddress(nil, &mockVMContract)
require.Error(t, err)
require.Equal(t, common.Address{}, caller, "address should be zeroed")
})

t.Run("should raise error when contract is nil", func(t *testing.T) {
mockEVM, _ := setupMockEVMAndContract(common.Address{})
caller, err := GetEVMCallerAddress(&mockEVM, nil)
require.Error(t, err)
require.Equal(t, common.Address{}, caller, "address should be zeroed")
})

// When contract.CallerAddress == evm.Origin, caller is set to contract.CallerAddress.
t.Run("when caller address equals origin", func(t *testing.T) {
mockEVM, mockVMContract := setupMockEVMAndContract(common.Address{})
caller, err := GetEVMCallerAddress(&mockEVM, &mockVMContract)
require.NoError(t, err)
require.Equal(t, common.Address{}, caller, "address should be the same")
})

// When contract.CallerAddress != evm.Origin, caller should be set to evm.Origin.
t.Run("when caller address equals origin", func(t *testing.T) {
mockEVM, mockVMContract := setupMockEVMAndContract(sample.EthAddress())
caller, err := GetEVMCallerAddress(&mockEVM, &mockVMContract)
require.NoError(t, err)
require.Equal(t, mockEVM.Origin, caller, "address should be evm.Origin")
})
}

func setupMockEVMAndContract(address common.Address) (vm.EVM, vm.Contract) {
mockEVM := vm.EVM{
TxContext: vm.TxContext{
Origin: common.Address{},
Origin: address,
},
}

Expand All @@ -24,16 +56,7 @@ func Test_GetEVMCallerAddress(t *testing.T) {
0,
)

// When contract.CallerAddress == evm.Origin, caller is set to contract.CallerAddress.
caller, err := GetEVMCallerAddress(&mockEVM, mockVMContract)
require.NoError(t, err)
require.Equal(t, common.Address{}, caller, "address shouldn be the same")

// When contract.CallerAddress != evm.Origin, caller should be set to evm.Origin.
mockEVM.Origin = sample.EthAddress()
caller, err = GetEVMCallerAddress(&mockEVM, mockVMContract)
require.NoError(t, err)
require.Equal(t, mockEVM.Origin, caller, "address should be evm.Origin")
return mockEVM, *mockVMContract
}

type contractRef struct {
Expand Down
8 changes: 7 additions & 1 deletion precompiles/types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ import (
const ZRC20DenomPrefix = "zrc20/"

// ZRC20ToCosmosDenom returns the cosmos coin address for a given ZRC20 address.
// This is converted to "zevm/{ZRC20Address}".
// This is converted to "zrc20/{ZRC20Address}".
func ZRC20ToCosmosDenom(ZRC20Address common.Address) string {
return ZRC20DenomPrefix + ZRC20Address.String()
}

func CreateCoinSet(tokenDenom string, amount *big.Int) (sdk.Coins, error) {
defer func() {
if r := recover(); r != nil {
return
}

Check warning on line 23 in precompiles/types/coin.go

View check run for this annotation

Codecov / codecov/patch

precompiles/types/coin.go#L22-L23

Added lines #L22 - L23 were not covered by tests
}()

coin := sdk.NewCoin(tokenDenom, math.NewIntFromBigInt(amount))
if !coin.IsValid() {
return nil, &ErrInvalidCoin{

Check warning on line 28 in precompiles/types/coin.go

View check run for this annotation

Codecov / codecov/patch

precompiles/types/coin.go#L28

Added line #L28 was not covered by tests
Expand Down

0 comments on commit 95c9237

Please sign in to comment.