github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/txs/tx_test.go (about) 1 package txs 2 3 import ( 4 "fmt" 5 "math/big" 6 "reflect" 7 "testing" 8 "time" 9 10 ethcmn "github.com/ethereum/go-ethereum/common" 11 "github.com/fibonacci-chain/fbc/app/crypto/ethsecp256k1" 12 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 13 authexported "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/exported" 14 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto" 15 "github.com/fibonacci-chain/fbc/x/evm/txs/base" 16 "github.com/fibonacci-chain/fbc/x/evm/types" 17 ) 18 19 var sdkResult sdk.Result 20 21 type EmptyAccount struct{} 22 23 func (e EmptyAccount) Copy() sdk.Account { return nil } 24 func (e EmptyAccount) GetAddress() sdk.AccAddress { return sdk.AccAddress{} } 25 func (e EmptyAccount) SetAddress(address sdk.AccAddress) error { return nil } 26 func (e EmptyAccount) GetPubKey() crypto.PubKey { return nil } 27 func (e EmptyAccount) SetPubKey(key crypto.PubKey) error { return nil } 28 func (e EmptyAccount) GetAccountNumber() uint64 { return 0 } 29 func (e EmptyAccount) SetAccountNumber(u uint64) error { return nil } 30 func (e EmptyAccount) GetSequence() uint64 { return 0 } 31 func (e EmptyAccount) SetSequence(u uint64) error { return nil } 32 func (e EmptyAccount) GetCoins() sdk.Coins { return sdk.Coins{} } 33 func (e EmptyAccount) SetCoins(coins sdk.Coins) error { return nil } 34 func (e EmptyAccount) SpendableCoins(blockTime time.Time) sdk.Coins { return sdk.Coins{} } 35 func (e EmptyAccount) String() string { return "ut" } 36 37 type EmptyTx struct { 38 PrepareFail bool 39 GetChainConfigFail bool 40 TransitionFail bool 41 DecorateResultFail bool 42 } 43 44 func (e EmptyTx) Dispose() {} 45 46 func (e EmptyTx) Prepare(msg *types.MsgEthereumTx) (err error) { 47 if e.PrepareFail { 48 return fmt.Errorf("prepare error") 49 } 50 return nil 51 } 52 func (e EmptyTx) SaveTx(msg *types.MsgEthereumTx) {} 53 func (e EmptyTx) GetChainConfig() (types.ChainConfig, bool) { 54 if e.GetChainConfigFail { 55 return types.ChainConfig{}, false 56 } 57 return types.ChainConfig{}, true 58 } 59 func (e EmptyTx) GetSenderAccount() authexported.Account { return EmptyAccount{} } 60 func (e EmptyTx) Transition(config types.ChainConfig) (result base.Result, err error) { 61 if e.TransitionFail { 62 err = fmt.Errorf("transition error") 63 return 64 } 65 var execResult types.ExecutionResult 66 execResult.Result = &sdkResult 67 result.ExecResult = &execResult 68 return 69 } 70 71 func (e EmptyTx) DecorateResult(inResult *base.Result, inErr error) (result *sdk.Result, err error) { 72 if e.DecorateResultFail { 73 return nil, fmt.Errorf("decorate result error") 74 } 75 if inErr != nil { 76 return nil, inErr 77 } 78 79 return &sdkResult, nil 80 } 81 82 func (e EmptyTx) Commit(msg *types.MsgEthereumTx, result *base.Result) {} 83 func (e EmptyTx) EmitEvent(msg *types.MsgEthereumTx, result *base.Result) {} 84 func (e EmptyTx) FinalizeWatcher(msg *types.MsgEthereumTx, err error, panic bool) {} 85 func (e EmptyTx) AnalyzeStart(tag string) {} 86 func (e EmptyTx) AnalyzeStop(tag string) {} 87 88 func TestTransitionEvmTx(t *testing.T) { 89 privateKey, _ := ethsecp256k1.GenerateKey() 90 sender := ethcmn.HexToAddress(privateKey.PubKey().Address().String()) 91 msg := types.NewMsgEthereumTx(0, &sender, big.NewInt(100), 3000000, big.NewInt(1), nil) 92 type args struct { 93 tx Tx 94 msg *types.MsgEthereumTx 95 } 96 tests := []struct { 97 name string 98 args args 99 wantResult *sdk.Result 100 wantErr bool 101 }{ 102 {"1. none error", args{tx: EmptyTx{}, msg: msg}, &sdkResult, false}, 103 {"2. prepare error", args{tx: EmptyTx{PrepareFail: true}, msg: msg}, nil, true}, 104 {"3. transition error", args{tx: EmptyTx{TransitionFail: true}, msg: msg}, nil, true}, 105 {"4. decorate result error", args{tx: EmptyTx{DecorateResultFail: true}, msg: msg}, nil, true}, 106 } 107 for _, tt := range tests { 108 t.Run(tt.name, func(t *testing.T) { 109 gotResult, err := TransitionEvmTx(tt.args.tx, tt.args.msg) 110 if (err != nil) != tt.wantErr { 111 t.Errorf("TransitionEvmTx() error = %v, wantErr %v", err, tt.wantErr) 112 return 113 } 114 if !reflect.DeepEqual(gotResult, tt.wantResult) { 115 t.Errorf("TransitionEvmTx() gotResult = %v, want %v", gotResult, tt.wantResult) 116 } 117 }) 118 } 119 }