|
| 1 | +package evmd_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "testing" |
| 6 | + |
| 7 | + codectypes "github.com/cosmos/cosmos-sdk/codec/types" |
| 8 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 9 | + mempool "github.com/cosmos/cosmos-sdk/types/mempool" |
| 10 | + "github.com/cosmos/evm/evmd" |
| 11 | + "github.com/cosmos/evm/x/vm/types" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + protov2 "google.golang.org/protobuf/proto" |
| 14 | +) |
| 15 | + |
| 16 | +type mockFallback struct { |
| 17 | + called bool |
| 18 | +} |
| 19 | + |
| 20 | +func (m *mockFallback) GetSigners(tx sdk.Tx) ([]mempool.SignerData, error) { |
| 21 | + m.called = true |
| 22 | + return []mempool.SignerData{mempool.NewSignerData(sdk.AccAddress("fallback"), 1)}, nil |
| 23 | +} |
| 24 | + |
| 25 | +type mockHasExtOptions struct { |
| 26 | + msg sdk.Msg |
| 27 | +} |
| 28 | + |
| 29 | +func (m *mockHasExtOptions) GetMsgs() []sdk.Msg { return []sdk.Msg{m.msg} } |
| 30 | +func (m *mockHasExtOptions) GetMsgsV2() ([]protov2.Message, error) { |
| 31 | + return []protov2.Message{}, nil |
| 32 | +} |
| 33 | +func (m *mockHasExtOptions) GetExtensionOptions() []*codectypes.Any { |
| 34 | + return []*codectypes.Any{ |
| 35 | + { |
| 36 | + TypeUrl: "/cosmos.evm.vm.v1.ExtensionOptionsEthereumTx", |
| 37 | + Value: []byte{}, |
| 38 | + }, |
| 39 | + } |
| 40 | +} |
| 41 | +func (m *mockHasExtOptions) GetNonCriticalExtensionOptions() []*codectypes.Any { return nil } |
| 42 | + |
| 43 | +func TestGetSigners(t *testing.T) { |
| 44 | + ethAddr := sdk.AccAddress("ethsigner") |
| 45 | + evmTx := &types.EvmTxArgs{ |
| 46 | + ChainID: big.NewInt(100), |
| 47 | + Nonce: 1, |
| 48 | + Amount: big.NewInt(10), |
| 49 | + GasLimit: 100000, |
| 50 | + GasPrice: big.NewInt(150), |
| 51 | + GasFeeCap: big.NewInt(200), |
| 52 | + } |
| 53 | + ethMsg := types.NewTx(evmTx) |
| 54 | + ethMsg.From = ethAddr.String() |
| 55 | + txWithEth := &mockHasExtOptions{ |
| 56 | + msg: ethMsg, |
| 57 | + } |
| 58 | + fallback := &mockFallback{} |
| 59 | + adapter := evmd.NewEthSignerExtractionAdapter(fallback) |
| 60 | + signers, err := adapter.GetSigners(txWithEth) |
| 61 | + require.NoError(t, err) |
| 62 | + require.Equal(t, []mempool.SignerData{{Signer: signers[0].Signer, Sequence: signers[0].Sequence}}, signers) |
| 63 | + require.False(t, fallback.called) |
| 64 | + |
| 65 | + fallback = &mockFallback{} |
| 66 | + txWithEth = &mockHasExtOptions{} |
| 67 | + adapter = evmd.NewEthSignerExtractionAdapter(fallback) |
| 68 | + signers, err = adapter.GetSigners(txWithEth) |
| 69 | + require.NoError(t, err) |
| 70 | + fallbackSigners, _ := new(mockFallback).GetSigners(txWithEth) |
| 71 | + require.Equal(t, fallbackSigners, signers) |
| 72 | + require.True(t, fallback.called) |
| 73 | +} |
0 commit comments