Skip to content

Commit

Permalink
whitelist erc20 wrapper to ibchook whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Oct 25, 2024
1 parent 63688ba commit 34e53b3
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 25 deletions.
8 changes: 8 additions & 0 deletions app/ibc-hooks/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func _createTestInput(
banktypes.RegisterQueryServer(queryRouter, &bankKeeper)

communityPoolKeeper := &MockCommunityPoolKeeper{}
ibcHookKeeper := &MockIBCHookKeeper{}
gasPriceKeeper := &MockGasPriceKeeper{GasPrices: map[string]math.LegacyDec{}}
evmKeeper := evmkeeper.NewKeeper(
ac,
Expand All @@ -312,6 +313,7 @@ func _createTestInput(
accountKeeper,
bankKeeper,
communityPoolKeeper,
ibcHookKeeper,
gasPriceKeeper,
msgRouter,
queryRouter,
Expand Down Expand Up @@ -439,3 +441,9 @@ func (k *MockGasPriceKeeper) GasPrice(ctx context.Context, denom string) (math.L

return gasPrice, nil
}

type MockIBCHookKeeper struct{}

func (k *MockIBCHookKeeper) SetAllowed(ctx context.Context, addr sdk.AccAddress, allowed bool) error {
return nil
}
1 change: 1 addition & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ func NewAppKeeper(
accountKeeper,
bankKeeper,
communityPoolKeeper,
appKeepers.IBCHooksKeeper,
gasPriceKeeper,
bApp.MsgServiceRouter(),
bApp.GRPCQueryRouter(),
Expand Down
4 changes: 2 additions & 2 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ can do so safely.
*/
func orderInitBlockers() []string {
return []string{
capabilitytypes.ModuleName, authtypes.ModuleName, evmtypes.ModuleName, banktypes.ModuleName,
capabilitytypes.ModuleName, authtypes.ModuleName, ibchookstypes.ModuleName, evmtypes.ModuleName, banktypes.ModuleName,
opchildtypes.ModuleName, genutiltypes.ModuleName, authz.ModuleName, group.ModuleName, crisistypes.ModuleName,
upgradetypes.ModuleName, feegrant.ModuleName, consensusparamtypes.ModuleName, ibcexported.ModuleName,
ibctransfertypes.ModuleName, ibcnfttransfertypes.ModuleName, icatypes.ModuleName, icaauthtypes.ModuleName,
ibcfeetypes.ModuleName, auctiontypes.ModuleName, oracletypes.ModuleName, marketmaptypes.ModuleName,
packetforwardtypes.ModuleName, forwardingtypes.ModuleName, ibchookstypes.ModuleName,
packetforwardtypes.ModuleName, forwardingtypes.ModuleName,
}
}
2 changes: 1 addition & 1 deletion jsonrpc/backend/feehistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (b *JSONRPCBackend) FeeHistory(blocks uint64, unresolvedLastBlock rpc.Block
} else {
fees.header, fees.err = b.GetHeaderByNumber(rpc.BlockNumber(blockNumber))
if len(rewardPercentiles) != 0 && fees.err == nil {
fees.receipts, fees.err = b.getBLockReceipts(blockNumber)
fees.receipts, fees.err = b.getBlockReceipts(blockNumber)

Check warning on line 131 in jsonrpc/backend/feehistory.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/feehistory.go#L131

Added line #L131 was not covered by tests
if fees.err == nil {
var txs []*rpctypes.RPCTransaction
txs, fees.err = b.getBlockTransactions(blockNumber)
Expand Down
8 changes: 1 addition & 7 deletions jsonrpc/backend/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package backend

import (
coretypes "github.com/ethereum/go-ethereum/core/types"

rpctypes "github.com/initia-labs/minievm/jsonrpc/types"
)

// GetLogsByHeight returns all the logs from all the ethereum transactions in a block.
Expand All @@ -20,11 +18,7 @@ func (b *JSONRPCBackend) GetLogsByHeight(height uint64) ([]*coretypes.Log, error
return nil, err
}

txs := []*rpctypes.RPCTransaction{}
err = b.app.EVMIndexer().IterateBlockTxs(queryCtx, height, func(tx *rpctypes.RPCTransaction) (bool, error) {
txs = append(txs, tx)
return false, nil
})
txs, err := b.getBlockTransactions(height)

Check warning on line 21 in jsonrpc/backend/filters.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/filters.go#L21

Added line #L21 was not covered by tests
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/backend/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func (b *JSONRPCBackend) getBlockTransactions(blockNumber uint64) ([]*rpctypes.R
return txs, nil
}

func (b *JSONRPCBackend) getBLockReceipts(blockNumber uint64) ([]*coretypes.Receipt, error) {
func (b *JSONRPCBackend) getBlockReceipts(blockNumber uint64) ([]*coretypes.Receipt, error) {

Check warning on line 388 in jsonrpc/backend/tx.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/tx.go#L388

Added line #L388 was not covered by tests
queryCtx, err := b.getQueryCtx()
if err != nil {
return nil, err
Expand Down Expand Up @@ -414,7 +414,7 @@ func (b *JSONRPCBackend) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc
return nil, err
}

receipts, err := b.getBLockReceipts(blockNumber)
receipts, err := b.getBlockReceipts(blockNumber)

Check warning on line 417 in jsonrpc/backend/tx.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/backend/tx.go#L417

Added line #L417 was not covered by tests
if err != nil {
return nil, err
}
Expand Down
15 changes: 3 additions & 12 deletions jsonrpc/namespaces/eth/filters/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/big"
"slices"

"cosmossdk.io/log"

Expand Down Expand Up @@ -213,7 +214,7 @@ func filterLogs(logs []*coretypes.Log, fromBlock, toBlock *big.Int, addresses []
if toBlock != nil && toBlock.Int64() >= 0 && toBlock.Uint64() < log.BlockNumber {
return false
}
if len(addresses) > 0 && !includes(addresses, log.Address) {
if len(addresses) > 0 && !slices.Contains(addresses, log.Address) {

Check warning on line 217 in jsonrpc/namespaces/eth/filters/filter.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/filter.go#L217

Added line #L217 was not covered by tests
return false
}
// If the to filtered topics is greater than the amount of topics in logs, skip.
Expand All @@ -224,7 +225,7 @@ func filterLogs(logs []*coretypes.Log, fromBlock, toBlock *big.Int, addresses []
if len(sub) == 0 {
continue // empty rule set == wildcard
}
if !includes(sub, log.Topics[i]) {
if !slices.Contains(sub, log.Topics[i]) {

Check warning on line 228 in jsonrpc/namespaces/eth/filters/filter.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/filter.go#L228

Added line #L228 was not covered by tests
return false
}
}
Expand All @@ -239,16 +240,6 @@ func filterLogs(logs []*coretypes.Log, fromBlock, toBlock *big.Int, addresses []
return ret
}

// includes returns true if the element is present in the list.
func includes[T comparable](things []T, element T) bool {
for _, thing := range things {
if thing == element {
return true
}
}
return false
}

func bloomFilter(bloom coretypes.Bloom, addresses []common.Address, topics [][]common.Hash) bool {
if len(addresses) > 0 {
var included bool
Expand Down
8 changes: 8 additions & 0 deletions x/bank/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ func _createTestInput(
banktypes.RegisterQueryServer(queryRouter, &bankKeeper)

communityPoolKeeper := &MockCommunityPoolKeeper{}
ibcHookKeeper := &MockIBCHookKeeper{}
gasPriceKeeper := &MockGasPriceKeeper{GasPrices: map[string]math.LegacyDec{}}
evmKeeper := evmkeeper.NewKeeper(
ac,
Expand All @@ -304,6 +305,7 @@ func _createTestInput(
accountKeeper,
bankKeeper,
communityPoolKeeper,
ibcHookKeeper,
gasPriceKeeper,
msgRouter,
queryRouter,
Expand Down Expand Up @@ -356,3 +358,9 @@ func (k *MockGasPriceKeeper) GasPrice(ctx context.Context, denom string) (math.L

return gasPrice, nil
}

type MockIBCHookKeeper struct{}

func (k *MockIBCHookKeeper) SetAllowed(ctx context.Context, addr sdk.AccAddress, allowed bool) error {
return nil
}
8 changes: 8 additions & 0 deletions x/evm/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func _createTestInput(
banktypes.RegisterQueryServer(queryRouter, &bankKeeper)

communityPoolKeeper := &MockCommunityPoolKeeper{}
ibcHookKeeper := &MockIBCHookKeeper{}
gasPriceKeeper := &MockGasPriceKeeper{GasPrices: map[string]math.LegacyDec{}}
evmKeeper := evmkeeper.NewKeeper(
ac,
Expand All @@ -265,6 +266,7 @@ func _createTestInput(
accountKeeper,
bankKeeper,
communityPoolKeeper,
ibcHookKeeper,
gasPriceKeeper,
msgRouter,
queryRouter,
Expand Down Expand Up @@ -331,3 +333,9 @@ func (k *MockGasPriceKeeper) GasPrice(ctx context.Context, denom string) (math.L

return gasPrice, nil
}

type MockIBCHookKeeper struct{}

func (k *MockIBCHookKeeper) SetAllowed(ctx context.Context, addr sdk.AccAddress, allowed bool) error {
return nil
}
10 changes: 9 additions & 1 deletion x/evm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,16 @@ func (k Keeper) DeployERC20Wrapper(ctx context.Context) error {
if err != nil {
return err
}
if err = k.ERC20WrapperAddr.Set(ctx, wrapperAddr.Bytes()); err != nil {
return err
}

Check warning on line 91 in x/evm/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/genesis.go#L90-L91

Added lines #L90 - L91 were not covered by tests

// whitelist the wrapper contract for IBC hook
if err = k.ibcHookKeeper.SetAllowed(ctx, wrapperAddr.Bytes(), true); err != nil {
return err
}

Check warning on line 96 in x/evm/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/genesis.go#L95-L96

Added lines #L95 - L96 were not covered by tests

return k.ERC20WrapperAddr.Set(ctx, wrapperAddr.Bytes())
return nil
}

func (k Keeper) InitGenesis(ctx context.Context, genState types.GenesisState) error {
Expand Down
3 changes: 3 additions & 0 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Keeper struct {
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
communityPoolKeeper types.CommunityPoolKeeper
ibcHookKeeper types.IBCHookKeeper
gasPriceKeeper types.GasPriceKeeper
erc20Keeper types.IERC20Keeper
erc20StoresKeeper types.IERC20StoresKeeper
Expand Down Expand Up @@ -86,6 +87,7 @@ func NewKeeper(
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
communityPoolKeeper types.CommunityPoolKeeper,
ibcHookKeeper types.IBCHookKeeper,
gasPriceKeeper types.GasPriceKeeper,
msgRouter baseapp.MessageRouter,
grpcRouter *baseapp.GRPCQueryRouter,
Expand All @@ -109,6 +111,7 @@ func NewKeeper(
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
communityPoolKeeper: communityPoolKeeper,
ibcHookKeeper: ibcHookKeeper,
gasPriceKeeper: gasPriceKeeper,
authority: authority,

Expand Down
8 changes: 8 additions & 0 deletions x/evm/state/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func _createTestInput(
banktypes.RegisterQueryServer(queryRouter, &bankKeeper)

communityPoolKeeper := &MockCommunityPoolKeeper{}
ibcHookKeeper := &MockIBCHookKeeper{}
gasPriceKeeper := &MockGasPriceKeeper{GasPrices: make(map[string]math.LegacyDec)}
evmKeeper := evmkeeper.NewKeeper(
ac,
Expand All @@ -264,6 +265,7 @@ func _createTestInput(
accountKeeper,
bankKeeper,
communityPoolKeeper,
ibcHookKeeper,
gasPriceKeeper,
msgRouter,
queryRouter,
Expand Down Expand Up @@ -329,3 +331,9 @@ func (k *MockGasPriceKeeper) GasPrice(ctx context.Context, denom string) (math.L

return gasPrice, nil
}

type MockIBCHookKeeper struct{}

func (k *MockIBCHookKeeper) SetAllowed(ctx context.Context, addr sdk.AccAddress, allowed bool) error {
return nil
}
4 changes: 4 additions & 0 deletions x/evm/types/expected_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@ type TxUtils interface {
ConvertEthereumTxToCosmosTx(ctx context.Context, ethTx *coretypes.Transaction) (sdk.Tx, error)
IsEthereumTx(ctx context.Context, sdkTx sdk.Tx) (bool, error)
}

type IBCHookKeeper interface {
SetAllowed(ctx context.Context, addr sdk.AccAddress, allowed bool) error
}

0 comments on commit 34e53b3

Please sign in to comment.