|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/ethereum/go-ethereum/common" |
| 10 | + "github.com/ethereum/go-ethereum/core/types" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/mock" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_getErrorReasonFromTx(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + var ( |
| 20 | + tx = types.NewTransaction( |
| 21 | + 1, // nonce |
| 22 | + common.HexToAddress("0xabc123"), // to address |
| 23 | + big.NewInt(1000000000000000000), // value: 1 ETH |
| 24 | + 21000, // gas limit |
| 25 | + big.NewInt(20000000000), // gas price: 20 Gwei |
| 26 | + []byte{0xde, 0xad, 0xbe, 0xef}, // data |
| 27 | + ) |
| 28 | + ) |
| 29 | + |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + beforeFunc func(caller *MockContractCaller) |
| 33 | + giveTx *types.Transaction |
| 34 | + giveReceipt *types.Receipt |
| 35 | + wantReason string |
| 36 | + wantErr string |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "no transaction error", |
| 40 | + beforeFunc: func(caller *MockContractCaller) { |
| 41 | + caller.EXPECT().CallContract( |
| 42 | + mock.Anything, |
| 43 | + mock.AnythingOfType("ethereum.CallMsg"), |
| 44 | + mock.AnythingOfType("*big.Int"), |
| 45 | + ).Return([]byte{}, nil) |
| 46 | + }, |
| 47 | + giveTx: tx, |
| 48 | + giveReceipt: &types.Receipt{}, |
| 49 | + wantErr: "reverted with no reason", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "transaction error with reason", |
| 53 | + beforeFunc: func(caller *MockContractCaller) { |
| 54 | + caller.EXPECT().CallContract( |
| 55 | + mock.Anything, |
| 56 | + mock.AnythingOfType("ethereum.CallMsg"), |
| 57 | + mock.AnythingOfType("*big.Int"), |
| 58 | + ).Return(nil, &jsonError{ |
| 59 | + Code: 100, |
| 60 | + Message: "test error message", |
| 61 | + Data: []byte("test error data"), |
| 62 | + }) |
| 63 | + }, |
| 64 | + giveTx: tx, |
| 65 | + giveReceipt: &types.Receipt{}, |
| 66 | + wantReason: "test error data", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "transaction error with no reason (non json error)", |
| 70 | + beforeFunc: func(caller *MockContractCaller) { |
| 71 | + caller.EXPECT().CallContract( |
| 72 | + mock.Anything, |
| 73 | + mock.AnythingOfType("ethereum.CallMsg"), |
| 74 | + mock.AnythingOfType("*big.Int"), |
| 75 | + ).Return(nil, errors.New("error message")) |
| 76 | + }, |
| 77 | + giveTx: tx, |
| 78 | + giveReceipt: &types.Receipt{}, |
| 79 | + wantReason: "error message", |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + |
| 87 | + caller := NewMockContractCaller(t) |
| 88 | + if tt.beforeFunc != nil { |
| 89 | + tt.beforeFunc(caller) |
| 90 | + } |
| 91 | + |
| 92 | + got, err := getErrorReasonFromTx( |
| 93 | + t.Context(), caller, common.HexToAddress("0x123"), tt.giveTx, tt.giveReceipt, |
| 94 | + ) |
| 95 | + |
| 96 | + if tt.wantErr != "" { |
| 97 | + require.ErrorContains(t, err, tt.wantErr) |
| 98 | + } else { |
| 99 | + require.NoError(t, err) |
| 100 | + assert.Equal(t, tt.wantReason, got) |
| 101 | + } |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func Test_parseError(t *testing.T) { |
| 107 | + t.Parallel() |
| 108 | + |
| 109 | + tests := []struct { |
| 110 | + name string |
| 111 | + give error |
| 112 | + want string |
| 113 | + wantErr string |
| 114 | + }{ |
| 115 | + { |
| 116 | + name: "valid error", |
| 117 | + give: &jsonError{ |
| 118 | + Code: 100, |
| 119 | + Message: "execution reverted", |
| 120 | + Data: "0x12345678", |
| 121 | + }, |
| 122 | + want: "0x12345678", |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "nil error", |
| 126 | + give: nil, |
| 127 | + wantErr: "cannot parse nil error", |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "invalid error type", |
| 131 | + give: errors.New("invalid"), |
| 132 | + wantErr: "error must be of type jsonError", |
| 133 | + }, |
| 134 | + { |
| 135 | + name: "trie error", |
| 136 | + give: &jsonError{ |
| 137 | + Code: -32000, |
| 138 | + Message: "missing trie node", |
| 139 | + Data: []byte{}, |
| 140 | + }, |
| 141 | + wantErr: "missing trie node", |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + for _, tt := range tests { |
| 146 | + t.Run(tt.name, func(t *testing.T) { |
| 147 | + t.Parallel() |
| 148 | + |
| 149 | + got, err := getJSONErrorData(tt.give) |
| 150 | + |
| 151 | + if tt.wantErr != "" { |
| 152 | + require.ErrorContains(t, err, tt.wantErr) |
| 153 | + } else { |
| 154 | + require.NoError(t, err) |
| 155 | + assert.Equal(t, tt.want, got) |
| 156 | + } |
| 157 | + }) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// Dummy implementation of jsonError to satisfy the interface |
| 162 | +type jsonError struct { |
| 163 | + Code int `json:"code"` |
| 164 | + Message string `json:"message"` |
| 165 | + Data any `json:"data,omitempty"` |
| 166 | +} |
| 167 | + |
| 168 | +func (err *jsonError) Error() string { |
| 169 | + if err.Message == "" { |
| 170 | + return fmt.Sprintf("json-rpc error %d", err.Code) |
| 171 | + } |
| 172 | + |
| 173 | + return err.Message |
| 174 | +} |
| 175 | + |
| 176 | +func (err *jsonError) ErrorCode() int { |
| 177 | + return err.Code |
| 178 | +} |
| 179 | + |
| 180 | +func (err *jsonError) ErrorData() interface{} { |
| 181 | + return err.Data |
| 182 | +} |
0 commit comments