2021-09-02 12:36:33 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-04-04 18:11:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2021-09-02 12:36:33 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
|
|
|
|
"github.com/tharsis/ethermint/x/evm/keeper"
|
2022-01-05 07:28:27 +00:00
|
|
|
"github.com/tharsis/ethermint/x/evm/statedb"
|
2021-09-02 12:36:33 +00:00
|
|
|
"github.com/tharsis/ethermint/x/evm/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LogRecordHook records all the logs
|
|
|
|
type LogRecordHook struct {
|
|
|
|
Logs []*ethtypes.Log
|
|
|
|
}
|
|
|
|
|
2022-04-04 18:11:46 +00:00
|
|
|
func (dh *LogRecordHook) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error {
|
2022-01-03 16:18:13 +00:00
|
|
|
dh.Logs = receipt.Logs
|
2021-09-02 12:36:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FailureHook always fail
|
|
|
|
type FailureHook struct{}
|
|
|
|
|
2022-04-04 18:11:46 +00:00
|
|
|
func (dh FailureHook) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error {
|
2021-09-02 12:36:33 +00:00
|
|
|
return errors.New("post tx processing failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestEvmHooks() {
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
setupHook func() types.EvmHooks
|
|
|
|
expFunc func(hook types.EvmHooks, result error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"log collect hook",
|
|
|
|
func() types.EvmHooks {
|
|
|
|
return &LogRecordHook{}
|
|
|
|
},
|
|
|
|
func(hook types.EvmHooks, result error) {
|
|
|
|
suite.Require().NoError(result)
|
|
|
|
suite.Require().Equal(1, len((hook.(*LogRecordHook).Logs)))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"always fail hook",
|
|
|
|
func() types.EvmHooks {
|
|
|
|
return &FailureHook{}
|
|
|
|
},
|
|
|
|
func(hook types.EvmHooks, result error) {
|
|
|
|
suite.Require().Error(result)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.SetupTest()
|
|
|
|
hook := tc.setupHook()
|
|
|
|
suite.app.EvmKeeper.SetHooks(keeper.NewMultiEvmHooks(hook))
|
|
|
|
|
|
|
|
k := suite.app.EvmKeeper
|
2022-01-05 07:28:27 +00:00
|
|
|
ctx := suite.ctx
|
2021-09-02 12:36:33 +00:00
|
|
|
txHash := common.BigToHash(big.NewInt(1))
|
2022-01-05 07:28:27 +00:00
|
|
|
vmdb := statedb.New(ctx, k, statedb.NewTxConfig(
|
|
|
|
common.BytesToHash(ctx.HeaderHash().Bytes()),
|
|
|
|
txHash,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
))
|
|
|
|
|
|
|
|
vmdb.AddLog(ðtypes.Log{
|
2021-09-02 12:36:33 +00:00
|
|
|
Topics: []common.Hash{},
|
|
|
|
Address: suite.address,
|
|
|
|
})
|
2022-01-05 07:28:27 +00:00
|
|
|
logs := vmdb.Logs()
|
2022-01-03 16:18:13 +00:00
|
|
|
receipt := ðtypes.Receipt{
|
|
|
|
TxHash: txHash,
|
|
|
|
Logs: logs,
|
|
|
|
}
|
2022-04-04 18:11:46 +00:00
|
|
|
result := k.PostTxProcessing(ctx, ethtypes.Message{}, receipt)
|
2021-09-02 12:36:33 +00:00
|
|
|
|
|
|
|
tc.expFunc(hook, result)
|
|
|
|
}
|
|
|
|
}
|