forked from evmos/evmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
285 lines (244 loc) · 9.95 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package backend
import (
"context"
"testing"
"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/bytes"
tmrpcclient "github.com/cometbft/cometbft/rpc/client"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/cometbft/cometbft/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common"
"github.com/evmos/evmos/v16/rpc/backend/mocks"
rpc "github.com/evmos/evmos/v16/rpc/types"
evmtypes "github.com/evmos/evmos/v16/x/evm/types"
mock "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// Client defines a mocked object that implements the Tendermint JSON-RPC Client
// interface. It allows for performing Client queries without having to run a
// Tendermint RPC Client server.
//
// To use a mock method it has to be registered in a given test.
var _ tmrpcclient.Client = &mocks.Client{}
// Tx Search
func RegisterTxSearch(client *mocks.Client, query string, txBz []byte) {
resulTxs := []*tmrpctypes.ResultTx{{Tx: txBz}}
client.On("TxSearch", rpc.ContextWithHeight(1), query, false, (*int)(nil), (*int)(nil), "").
Return(&tmrpctypes.ResultTxSearch{Txs: resulTxs, TotalCount: 1}, nil)
}
func RegisterTxSearchEmpty(client *mocks.Client, query string) {
client.On("TxSearch", rpc.ContextWithHeight(1), query, false, (*int)(nil), (*int)(nil), "").
Return(&tmrpctypes.ResultTxSearch{}, nil)
}
func RegisterTxSearchError(client *mocks.Client, query string) {
client.On("TxSearch", rpc.ContextWithHeight(1), query, false, (*int)(nil), (*int)(nil), "").
Return(nil, errortypes.ErrInvalidRequest)
}
// Broadcast Tx
func RegisterBroadcastTx(client *mocks.Client, tx types.Tx) {
client.On("BroadcastTxSync", context.Background(), tx).
Return(&tmrpctypes.ResultBroadcastTx{}, nil)
}
func RegisterBroadcastTxError(client *mocks.Client, tx types.Tx) {
client.On("BroadcastTxSync", context.Background(), tx).
Return(nil, errortypes.ErrInvalidRequest)
}
// Unconfirmed Transactions
func RegisterUnconfirmedTxs(client *mocks.Client, limit *int, txs []types.Tx) {
client.On("UnconfirmedTxs", rpc.ContextWithHeight(1), limit).
Return(&tmrpctypes.ResultUnconfirmedTxs{Txs: txs}, nil)
}
func RegisterUnconfirmedTxsEmpty(client *mocks.Client, limit *int) {
client.On("UnconfirmedTxs", rpc.ContextWithHeight(1), limit).
Return(&tmrpctypes.ResultUnconfirmedTxs{
Txs: make([]types.Tx, 2),
}, nil)
}
func RegisterUnconfirmedTxsError(client *mocks.Client, limit *int) {
client.On("UnconfirmedTxs", rpc.ContextWithHeight(1), limit).
Return(nil, errortypes.ErrInvalidRequest)
}
// Status
func RegisterStatus(client *mocks.Client) {
client.On("Status", rpc.ContextWithHeight(1)).
Return(&tmrpctypes.ResultStatus{}, nil)
}
func RegisterStatusError(client *mocks.Client) {
client.On("Status", rpc.ContextWithHeight(1)).
Return(nil, errortypes.ErrInvalidRequest)
}
// Block
func RegisterBlockMultipleTxs(
client *mocks.Client,
height int64,
txs []types.Tx,
) (*tmrpctypes.ResultBlock, error) {
block := types.MakeBlock(height, txs, nil, nil)
block.ChainID = ChainID
resBlock := &tmrpctypes.ResultBlock{Block: block}
client.On("Block", rpc.ContextWithHeight(height), mock.AnythingOfType("*int64")).Return(resBlock, nil)
return resBlock, nil
}
func RegisterBlock(
client *mocks.Client,
height int64,
tx []byte,
) (*tmrpctypes.ResultBlock, error) {
// without tx
if tx == nil {
emptyBlock := types.MakeBlock(height, []types.Tx{}, nil, nil)
emptyBlock.ChainID = ChainID
resBlock := &tmrpctypes.ResultBlock{Block: emptyBlock}
client.On("Block", rpc.ContextWithHeight(height), mock.AnythingOfType("*int64")).Return(resBlock, nil)
return resBlock, nil
}
// with tx
block := types.MakeBlock(height, []types.Tx{tx}, nil, nil)
block.ChainID = ChainID
resBlock := &tmrpctypes.ResultBlock{Block: block}
client.On("Block", rpc.ContextWithHeight(height), mock.AnythingOfType("*int64")).Return(resBlock, nil)
return resBlock, nil
}
// Block returns error
func RegisterBlockError(client *mocks.Client, height int64) {
client.On("Block", rpc.ContextWithHeight(height), mock.AnythingOfType("*int64")).
Return(nil, errortypes.ErrInvalidRequest)
}
// Block not found
func RegisterBlockNotFound(
client *mocks.Client,
height int64,
) (*tmrpctypes.ResultBlock, error) {
client.On("Block", rpc.ContextWithHeight(height), mock.AnythingOfType("*int64")).
Return(&tmrpctypes.ResultBlock{Block: nil}, nil)
return &tmrpctypes.ResultBlock{Block: nil}, nil
}
func TestRegisterBlock(t *testing.T) {
client := mocks.NewClient(t)
height := rpc.BlockNumber(1).Int64()
_, err := RegisterBlock(client, height, nil)
require.NoError(t, err)
res, err := client.Block(rpc.ContextWithHeight(height), &height)
emptyBlock := types.MakeBlock(height, []types.Tx{}, nil, nil)
emptyBlock.ChainID = ChainID
resBlock := &tmrpctypes.ResultBlock{Block: emptyBlock}
require.Equal(t, resBlock, res)
require.NoError(t, err)
}
// ConsensusParams
func RegisterConsensusParams(client *mocks.Client, height int64) {
consensusParams := types.DefaultConsensusParams()
client.On("ConsensusParams", rpc.ContextWithHeight(height), mock.AnythingOfType("*int64")).
Return(&tmrpctypes.ResultConsensusParams{ConsensusParams: *consensusParams}, nil)
}
func RegisterConsensusParamsError(client *mocks.Client, height int64) {
client.On("ConsensusParams", rpc.ContextWithHeight(height), mock.AnythingOfType("*int64")).
Return(nil, errortypes.ErrInvalidRequest)
}
func TestRegisterConsensusParams(t *testing.T) {
client := mocks.NewClient(t)
height := int64(1)
RegisterConsensusParams(client, height)
res, err := client.ConsensusParams(rpc.ContextWithHeight(height), &height)
consensusParams := types.DefaultConsensusParams()
require.Equal(t, &tmrpctypes.ResultConsensusParams{ConsensusParams: *consensusParams}, res)
require.NoError(t, err)
}
// BlockResults
func RegisterBlockResultsWithEventLog(client *mocks.Client, height int64) (*tmrpctypes.ResultBlockResults, error) {
res := &tmrpctypes.ResultBlockResults{
Height: height,
TxsResults: []*abci.ResponseDeliverTx{
{Code: 0, GasUsed: 0, Events: []abci.Event{{
Type: evmtypes.EventTypeTxLog,
Attributes: []abci.EventAttribute{{
Key: evmtypes.AttributeKeyTxLog,
Value: "{\"test\": \"hello\"}", // TODO refactor the value to unmarshall to a evmtypes.Log struct successfully
Index: true,
}},
}}},
},
}
client.On("BlockResults", rpc.ContextWithHeight(height), mock.AnythingOfType("*int64")).
Return(res, nil)
return res, nil
}
func RegisterBlockResults(
client *mocks.Client,
height int64,
) (*tmrpctypes.ResultBlockResults, error) {
res := &tmrpctypes.ResultBlockResults{
Height: height,
TxsResults: []*abci.ResponseDeliverTx{{Code: 0, GasUsed: 0}},
}
client.On("BlockResults", rpc.ContextWithHeight(height), mock.AnythingOfType("*int64")).
Return(res, nil)
return res, nil
}
func RegisterBlockResultsError(client *mocks.Client, height int64) {
client.On("BlockResults", rpc.ContextWithHeight(height), mock.AnythingOfType("*int64")).
Return(nil, errortypes.ErrInvalidRequest)
}
func TestRegisterBlockResults(t *testing.T) {
client := mocks.NewClient(t)
height := int64(1)
_, err := RegisterBlockResults(client, height)
require.NoError(t, err)
res, err := client.BlockResults(rpc.ContextWithHeight(height), &height)
expRes := &tmrpctypes.ResultBlockResults{
Height: height,
TxsResults: []*abci.ResponseDeliverTx{{Code: 0, GasUsed: 0}},
}
require.Equal(t, expRes, res)
require.NoError(t, err)
}
// BlockByHash
func RegisterBlockByHash(
client *mocks.Client,
_ common.Hash,
tx []byte,
) (*tmrpctypes.ResultBlock, error) {
block := types.MakeBlock(1, []types.Tx{tx}, nil, nil)
resBlock := &tmrpctypes.ResultBlock{Block: block}
client.On("BlockByHash", rpc.ContextWithHeight(1), []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}).
Return(resBlock, nil)
return resBlock, nil
}
func RegisterBlockByHashError(client *mocks.Client, _ common.Hash, _ []byte) {
client.On("BlockByHash", rpc.ContextWithHeight(1), []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}).
Return(nil, errortypes.ErrInvalidRequest)
}
func RegisterBlockByHashNotFound(client *mocks.Client, _ common.Hash, _ []byte) {
client.On("BlockByHash", rpc.ContextWithHeight(1), []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}).
Return(nil, nil)
}
func RegisterABCIQueryWithOptions(client *mocks.Client, height int64, path string, data bytes.HexBytes, opts tmrpcclient.ABCIQueryOptions) {
client.On("ABCIQueryWithOptions", context.Background(), path, data, opts).
Return(&tmrpctypes.ResultABCIQuery{
Response: abci.ResponseQuery{
Value: []byte{2}, // TODO replace with data.Bytes(),
Height: height,
},
}, nil)
}
func RegisterABCIQueryWithOptionsError(clients *mocks.Client, path string, data bytes.HexBytes, opts tmrpcclient.ABCIQueryOptions) {
clients.On("ABCIQueryWithOptions", context.Background(), path, data, opts).
Return(nil, errortypes.ErrInvalidRequest)
}
func RegisterABCIQueryAccount(clients *mocks.Client, data bytes.HexBytes, opts tmrpcclient.ABCIQueryOptions, acc client.Account) {
baseAccount := authtypes.NewBaseAccount(acc.GetAddress(), acc.GetPubKey(), acc.GetAccountNumber(), acc.GetSequence())
accAny, _ := codectypes.NewAnyWithValue(baseAccount)
accResponse := authtypes.QueryAccountResponse{Account: accAny}
respBz, _ := accResponse.Marshal()
clients.On("ABCIQueryWithOptions", context.Background(), "/cosmos.auth.v1beta1.Query/Account", data, opts).
Return(&tmrpctypes.ResultABCIQuery{
Response: abci.ResponseQuery{
Value: respBz,
Height: 1,
},
}, nil)
}