-
Notifications
You must be signed in to change notification settings - Fork 7
/
context.go
275 lines (237 loc) · 8.21 KB
/
context.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
package testtx
import (
"context"
"fmt"
"testing"
"cosmossdk.io/depinject"
abci "github.com/cometbft/cometbft/abci/types"
cometbytes "github.com/cometbft/cometbft/libs/bytes"
cometrpctypes "github.com/cometbft/cometbft/rpc/core/types"
comettypes "github.com/cometbft/cometbft/types"
cosmosclient "github.com/cosmos/cosmos-sdk/client"
cosmostx "github.com/cosmos/cosmos-sdk/client/tx"
cosmoskeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
cosmostypes "github.com/cosmos/cosmos-sdk/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/pokt-network/poktroll/pkg/client"
"github.com/pokt-network/poktroll/pkg/client/tx"
txtypes "github.com/pokt-network/poktroll/pkg/client/tx/types"
"github.com/pokt-network/poktroll/testutil/mockclient"
"github.com/pokt-network/poktroll/testutil/testclient"
)
// NewLocalnetContext creates and returns a new transaction context configured
// for use with the LocalNet validator.
func NewLocalnetContext(t *testing.T) client.TxContext {
t.Helper()
flagSet := testclient.NewLocalnetFlagSet(t)
clientCtx := testclient.NewLocalnetClientCtx(t, flagSet)
txFactory, err := cosmostx.NewFactoryCLI(*clientCtx, flagSet)
require.NoError(t, err)
require.NotEmpty(t, txFactory)
deps := depinject.Supply(
txtypes.Context(*clientCtx),
txFactory,
)
txCtx, err := tx.NewTxContext(deps)
require.NoError(t, err)
return txCtx
}
// TODO_IMPROVE: these mock constructor helpers could include parameters for the
// "times" (e.g. exact, min, max) values which are passed to their respective
// gomock.EXPECT() method calls (i.e. Times(), MinTimes(), MaxTimes()).
// When implementing such a pattern, be careful about making assumptions about
// correlations between these "times" values and the contexts in which the expected
// methods may be called.
// NewOneTimeErrTxTimeoutTxContext creates a mock transaction context designed to
// simulate a specific timeout error scenario during transaction broadcasting.
// expectedErrMsg is populated with the same error message which is presented in
// the result from the QueryTx method so that it can be asserted against.
func NewOneTimeErrTxTimeoutTxContext(
t *testing.T,
keyring cosmoskeyring.Keyring,
signingKeyName string,
expectedErrMsg *string,
) *mockclient.MockTxContext {
t.Helper()
signerKey, err := keyring.Key(signingKeyName)
require.NoError(t, err)
signerAddr, err := signerKey.GetAddress()
require.NoError(t, err)
*expectedErrMsg = fmt.Sprintf(
"fee payer address: %s does not exist: unknown address",
signerAddr.String(),
)
var expectedTx cometbytes.HexBytes
txCtxMock := NewBaseTxContext(
t, signingKeyName,
keyring,
&expectedTx,
)
// intercept #BroadcastTx() call to mock response and prevent actual broadcast
txCtxMock.EXPECT().BroadcastTx(gomock.Any()).
DoAndReturn(
func(txBytes []byte) (*cosmostypes.TxResponse, error) {
var expectedTxHash cometbytes.HexBytes = comettypes.Tx(txBytes).Hash()
return &cosmostypes.TxResponse{
Height: 1,
TxHash: expectedTxHash.String(),
}, nil
},
).Times(1)
txCtxMock.EXPECT().QueryTx(
gomock.AssignableToTypeOf(context.Background()),
gomock.AssignableToTypeOf([]byte{}),
gomock.AssignableToTypeOf(false),
).DoAndReturn(
func(
ctx context.Context,
txHash []byte,
_ bool,
) (*cometrpctypes.ResultTx, error) {
return &cometrpctypes.ResultTx{
Hash: txHash,
Height: 1,
TxResult: abci.ExecTxResult{
Code: 1,
Log: *expectedErrMsg,
Codespace: "test_codespace",
},
Tx: expectedTx.Bytes(),
}, nil
},
)
return txCtxMock
}
// NewOneTimeErrCheckTxTxContext creates a mock transaction context to simulate
// a specific error scenario during the ABCI check-tx phase (i.e., during initial
// validation before the transaction is included in the block).
// expectedErrMsg is populated with the same error message which is presented in
// the result from the QueryTx method so that it can be asserted against.
func NewOneTimeErrCheckTxTxContext(
t *testing.T,
keyring cosmoskeyring.Keyring,
signingKeyName string,
expectedErrMsg *string,
) *mockclient.MockTxContext {
t.Helper()
signerKey, err := keyring.Key(signingKeyName)
require.NoError(t, err)
signerAddr, err := signerKey.GetAddress()
require.NoError(t, err)
*expectedErrMsg = fmt.Sprintf(
"fee payer address: %s does not exist: unknown address",
signerAddr.String(),
)
var expectedTx cometbytes.HexBytes
txCtxMock := NewBaseTxContext(
t, signingKeyName,
keyring,
&expectedTx,
)
// intercept #BroadcastTx() call to mock response and prevent actual broadcast
txCtxMock.EXPECT().BroadcastTx(gomock.Any()).
DoAndReturn(
func(txBytes []byte) (*cosmostypes.TxResponse, error) {
var expectedTxHash cometbytes.HexBytes = comettypes.Tx(txBytes).Hash()
return &cosmostypes.TxResponse{
Height: 1,
TxHash: expectedTxHash.String(),
RawLog: *expectedErrMsg,
Code: 1,
Codespace: "test_codespace",
}, nil
},
).Times(1)
return txCtxMock
}
// NewOneTimeTxTxContext creates a mock transaction context primed to respond with
// a single successful transaction response.
func NewOneTimeTxTxContext(
t *testing.T,
keyring cosmoskeyring.Keyring,
signingKeyName string,
expectedTx *cometbytes.HexBytes,
) *mockclient.MockTxContext {
t.Helper()
txCtxMock := NewBaseTxContext(
t, signingKeyName,
keyring,
expectedTx,
)
// intercept #BroadcastTx() call to mock response and prevent actual broadcast
txCtxMock.EXPECT().BroadcastTx(gomock.Any()).
DoAndReturn(
func(txBytes []byte) (*cosmostypes.TxResponse, error) {
var expectedTxHash cometbytes.HexBytes = comettypes.Tx(txBytes).Hash()
return &cosmostypes.TxResponse{
Height: 1,
TxHash: expectedTxHash.String(),
}, nil
},
).Times(1)
return txCtxMock
}
// NewBaseTxContext creates a mock transaction context that's configured to expect
// calls to NewTxBuilder, SignTx, and EncodeTx methods, any number of times.
// EncodeTx is used to intercept the encoded transaction bytes and store them in
// the expectedTx output parameter. Each of these methods proxies to the corresponding
// method on a real transaction context.
func NewBaseTxContext(
t *testing.T,
signingKeyName string,
keyring cosmoskeyring.Keyring,
expectedTx *cometbytes.HexBytes,
) *mockclient.MockTxContext {
t.Helper()
txCtxMock, txCtx := NewAnyTimesTxTxContext(t, keyring)
txCtxMock.EXPECT().NewTxBuilder().
DoAndReturn(txCtx.NewTxBuilder).
AnyTimes()
txCtxMock.EXPECT().SignTx(
gomock.Eq(signingKeyName),
gomock.AssignableToTypeOf(txCtx.NewTxBuilder()),
gomock.Eq(false), gomock.Eq(false),
).DoAndReturn(txCtx.SignTx).AnyTimes()
txCtxMock.EXPECT().EncodeTx(gomock.Any()).
DoAndReturn(
func(txBuilder cosmosclient.TxBuilder) (_ []byte, err error) {
// intercept cosmosTxContext#EncodeTx to get the encoded tx cometbytes
*expectedTx, err = txCtx.EncodeTx(txBuilder)
require.NoError(t, err)
return expectedTx.Bytes(), nil
},
).AnyTimes()
return txCtxMock
}
// NewAnyTimesTxTxContext initializes a mock transaction context that's configured to allow
// arbitrary calls to certain predefined interactions, primarily concerning the retrieval
// of account numbers and sequences.
func NewAnyTimesTxTxContext(
t *testing.T,
keyring cosmoskeyring.Keyring,
) (*mockclient.MockTxContext, client.TxContext) {
t.Helper()
var (
ctrl = gomock.NewController(t)
flagSet = testclient.NewLocalnetFlagSet(t)
)
// intercept #GetAccountNumberSequence() call to mock response and prevent actual query
accountRetrieverMock := mockclient.NewMockAccountRetriever(ctrl)
accountRetrieverMock.EXPECT().GetAccountNumberSequence(gomock.Any(), gomock.Any()).
Return(uint64(1), uint64(1), nil).
AnyTimes()
clientCtx := testclient.NewLocalnetClientCtx(t, flagSet).
WithKeyring(keyring).
WithAccountRetriever(accountRetrieverMock)
txFactory, err := cosmostx.NewFactoryCLI(clientCtx, flagSet)
require.NoError(t, err)
require.NotEmpty(t, txFactory)
txClientCtx := txtypes.Context(clientCtx)
txCtxDeps := depinject.Supply(txFactory, txClientCtx)
txCtx, err := tx.NewTxContext(txCtxDeps)
require.NoError(t, err)
txCtxMock := mockclient.NewMockTxContext(ctrl)
txCtxMock.EXPECT().GetKeyring().Return(keyring).AnyTimes()
return txCtxMock, txCtx
}