This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow evm to call native modules through logs
Closes #416 comment add txHash parameter review suggestions add hooks test
- Loading branch information
Showing
7 changed files
with
155 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package keeper | ||
|
||
import ( | ||
"reflect" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
ethcmn "github.com/ethereum/go-ethereum/common" | ||
ethtypes "github.com/ethereum/go-ethereum/core/types" | ||
"github.com/tharsis/ethermint/x/evm/types" | ||
) | ||
|
||
var ( | ||
_ types.EvmHooks = MultiEvmHooks{} | ||
) | ||
|
||
// MultiEvmHooks combine multiple evm hooks, all hook functions are run in array sequence | ||
type MultiEvmHooks []types.EvmHooks | ||
|
||
// NewMultiEvmHooks combine multiple evm hooks | ||
func NewMultiEvmHooks(hooks ...types.EvmHooks) MultiEvmHooks { | ||
return hooks | ||
} | ||
|
||
// PostTxProcessing delegate the call to underlying hooks | ||
func (mh MultiEvmHooks) PostTxProcessing(ctx sdk.Context, txHash ethcmn.Hash, logs []*ethtypes.Log) error { | ||
for i := range mh { | ||
if err := mh[i].PostTxProcessing(ctx, txHash, logs); err != nil { | ||
return sdkerrors.Wrapf(err, "EVM hook %s failed", reflect.TypeOf(mh[i])) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"errors" | ||
"math/big" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/ethereum/go-ethereum/common" | ||
ethtypes "github.com/ethereum/go-ethereum/core/types" | ||
|
||
ethermint "github.com/tharsis/ethermint/types" | ||
"github.com/tharsis/ethermint/x/evm/keeper" | ||
"github.com/tharsis/ethermint/x/evm/types" | ||
) | ||
|
||
// LogRecordHook records all the logs | ||
type LogRecordHook struct { | ||
Logs []*ethtypes.Log | ||
} | ||
|
||
func (dh *LogRecordHook) PostTxProcessing(ctx sdk.Context, txHash common.Hash, logs []*ethtypes.Log) error { | ||
dh.Logs = logs | ||
return nil | ||
} | ||
|
||
// FailureHook always fail | ||
type FailureHook struct{} | ||
|
||
func (dh FailureHook) PostTxProcessing(ctx sdk.Context, txHash common.Hash, logs []*ethtypes.Log) error { | ||
return errors.New("post tx processing failed") | ||
} | ||
func (suite *KeeperTestSuite) TestEvmHooks() { | ||
suite.SetupTest() | ||
suite.Commit() | ||
|
||
logRecordHook := LogRecordHook{} | ||
suite.app.EvmKeeper.SetHooks(keeper.NewMultiEvmHooks(&logRecordHook)) | ||
|
||
k := suite.app.EvmKeeper | ||
|
||
txHash := common.BigToHash(big.NewInt(1)) | ||
|
||
amt := sdk.Coins{ethermint.NewPhotonCoinInt64(100)} | ||
err := suite.app.BankKeeper.MintCoins(suite.ctx, types.ModuleName, amt) | ||
suite.Require().NoError(err) | ||
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, suite.address.Bytes(), amt) | ||
suite.Require().NoError(err) | ||
|
||
k.SetTxHashTransient(txHash) | ||
k.AddLog(ðtypes.Log{ | ||
Topics: []common.Hash{}, | ||
Address: suite.address, | ||
}) | ||
|
||
logs := k.GetTxLogs(txHash) | ||
suite.Require().Equal(1, len(logs)) | ||
|
||
err = k.PostTxProcessing(txHash, logs) | ||
suite.Require().NoError(err) | ||
|
||
suite.Require().Equal(1, len(logRecordHook.Logs)) | ||
} | ||
|
||
func (suite *KeeperTestSuite) TestHookFailure() { | ||
suite.SetupTest() | ||
k := suite.app.EvmKeeper | ||
|
||
// Test failure hook | ||
suite.app.EvmKeeper.SetHooks(keeper.NewMultiEvmHooks(FailureHook{})) | ||
err := k.PostTxProcessing(common.Hash{}, nil) | ||
suite.Require().Error(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters