github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/keeper/evm_hooks_test.go (about) 1 package keeper_test 2 3 import ( 4 "errors" 5 "math/big" 6 7 "github.com/ethereum/go-ethereum/common" 8 ethtypes "github.com/ethereum/go-ethereum/core/types" 9 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 10 "github.com/fibonacci-chain/fbc/x/evm/keeper" 11 "github.com/fibonacci-chain/fbc/x/evm/types" 12 ) 13 14 // LogRecordHook records all the logs 15 type LogRecordHook struct { 16 Logs []*ethtypes.Log 17 } 18 19 func (dh *LogRecordHook) PostTxProcessing(ctx sdk.Context, st *types.StateTransition, receipt *ethtypes.Receipt) error { 20 dh.Logs = receipt.Logs 21 return nil 22 } 23 24 // FailureHook always fail 25 type FailureHook struct{} 26 27 func (dh FailureHook) PostTxProcessing(ctx sdk.Context, st *types.StateTransition, receipt *ethtypes.Receipt) error { 28 return errors.New("post tx processing failed") 29 } 30 31 func (suite *KeeperTestSuite) TestEvmHooks() { 32 testCases := []struct { 33 msg string 34 setupHook func() types.EvmHooks 35 expFunc func(hook types.EvmHooks, result error) 36 }{ 37 { 38 "log collect hook", 39 func() types.EvmHooks { 40 return &LogRecordHook{} 41 }, 42 func(hook types.EvmHooks, result error) { 43 suite.Require().NoError(result) 44 suite.Require().Equal(1, len((hook.(*LogRecordHook).Logs))) 45 }, 46 }, 47 { 48 "always fail hook", 49 func() types.EvmHooks { 50 return &FailureHook{} 51 }, 52 func(hook types.EvmHooks, result error) { 53 suite.Require().Error(result) 54 }, 55 }, 56 } 57 58 for _, tc := range testCases { 59 suite.SetupTest() 60 hook := tc.setupHook() 61 suite.app.EvmKeeper.SetHooks(keeper.NewMultiEvmHooks(hook)) 62 63 k := suite.app.EvmKeeper 64 ctx := suite.ctx 65 txHash := common.BigToHash(big.NewInt(1)) 66 vmdb := types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx) 67 vmdb.Prepare(txHash, txHash, 0) 68 vmdb.AddLog(ðtypes.Log{ 69 Topics: []common.Hash{}, 70 Address: suite.address, 71 }) 72 logs, err := vmdb.GetLogs(txHash) 73 suite.Require().Nil(err) 74 receipt := ðtypes.Receipt{ 75 TxHash: txHash, 76 Logs: logs, 77 } 78 result := k.CallEvmHooks(ctx, nil, receipt) 79 80 tc.expFunc(hook, result) 81 } 82 }