-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathstreaming_test.go
333 lines (285 loc) · 11.5 KB
/
streaming_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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package baseapp_test
import (
"context"
"fmt"
"testing"
abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
tmproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
"github.com/stretchr/testify/require"
"cosmossdk.io/schema/appdata"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/baseapp"
baseapptestutil "github.com/cosmos/cosmos-sdk/baseapp/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var _ storetypes.ABCIListener = (*MockABCIListener)(nil)
type MockABCIListener struct {
name string
ChangeSet []*storetypes.StoreKVPair
}
func NewMockABCIListener(name string) MockABCIListener {
return MockABCIListener{
name: name,
ChangeSet: make([]*storetypes.StoreKVPair, 0),
}
}
func (m MockABCIListener) ListenFinalizeBlock(_ context.Context, _ abci.FinalizeBlockRequest, _ abci.FinalizeBlockResponse) error {
return nil
}
func (m *MockABCIListener) ListenCommit(_ context.Context, _ abci.CommitResponse, cs []*storetypes.StoreKVPair) error {
m.ChangeSet = cs
return nil
}
var distKey1 = storetypes.NewKVStoreKey("distKey1")
func TestABCI_MultiListener_StateChanges(t *testing.T) {
anteKey := []byte("ante-key")
anteOpt := func(bapp *baseapp.BaseApp) { bapp.SetAnteHandler(anteHandlerTxTest(t, capKey1, anteKey)) }
distOpt := func(bapp *baseapp.BaseApp) { bapp.MountStores(distKey1) }
mockListener1 := NewMockABCIListener("lis_1")
mockListener2 := NewMockABCIListener("lis_2")
streamingManager := storetypes.StreamingManager{ABCIListeners: []storetypes.ABCIListener{&mockListener1, &mockListener2}}
streamingManagerOpt := func(bapp *baseapp.BaseApp) { bapp.SetStreamingManager(streamingManager) }
addListenerOpt := func(bapp *baseapp.BaseApp) { bapp.CommitMultiStore().AddListeners([]storetypes.StoreKey{distKey1}) }
suite := NewBaseAppSuite(t, anteOpt, distOpt, streamingManagerOpt, addListenerOpt)
_, err := suite.baseApp.InitChain(
&abci.InitChainRequest{
ConsensusParams: &tmproto.ConsensusParams{},
},
)
require.NoError(t, err)
deliverKey := []byte("deliver-key")
baseapptestutil.RegisterCounterServer(suite.baseApp.MsgServiceRouter(), CounterServerImpl{t, capKey1, deliverKey})
nBlocks := 3
txPerHeight := 5
for blockN := 0; blockN < nBlocks; blockN++ {
txs := [][]byte{}
var expectedChangeSet []*storetypes.StoreKVPair
// create final block context state
_, err := suite.baseApp.FinalizeBlock(&abci.FinalizeBlockRequest{Height: int64(blockN) + 1, Txs: txs})
require.NoError(t, err)
for i := 0; i < txPerHeight; i++ {
counter := int64(blockN*txPerHeight + i)
tx := newTxCounter(t, suite.txConfig, suite.ac, counter, counter)
txBytes, err := suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)
sKey := []byte(fmt.Sprintf("distKey%d", i))
sVal := []byte(fmt.Sprintf("distVal%d", i))
store := getFinalizeBlockStateCtx(suite.baseApp).KVStore(distKey1)
store.Set(sKey, sVal)
expectedChangeSet = append(expectedChangeSet, &storetypes.StoreKVPair{
StoreKey: distKey1.Name(),
Delete: false,
Key: sKey,
Value: sVal,
})
txs = append(txs, txBytes)
}
res, err := suite.baseApp.FinalizeBlock(&abci.FinalizeBlockRequest{Height: int64(blockN) + 1, Txs: txs})
require.NoError(t, err)
for _, tx := range res.TxResults {
events := tx.GetEvents()
require.Len(t, events, 3, "should contain ante handler, message type and counter events respectively")
// require.Equal(t, sdk.MarkEventsToIndex(counterEvent("ante_handler", counter).ToABCIEvents(), map[string]struct{}{})[0], events[0], "ante handler event")
// require.Equal(t, sdk.MarkEventsToIndex(counterEvent(sdk.EventTypeMessage, counter).ToABCIEvents(), map[string]struct{}{})[0], events[2], "msg handler update counter event")
}
_, err = suite.baseApp.Commit()
require.NoError(t, err)
require.Equal(t, expectedChangeSet, mockListener1.ChangeSet, "should contain the same changeSet")
require.Equal(t, expectedChangeSet, mockListener2.ChangeSet, "should contain the same changeSet")
}
}
func Test_Ctx_with_StreamingManager(t *testing.T) {
mockListener1 := NewMockABCIListener("lis_1")
mockListener2 := NewMockABCIListener("lis_2")
listeners := []storetypes.ABCIListener{&mockListener1, &mockListener2}
streamingManager := storetypes.StreamingManager{ABCIListeners: listeners, StopNodeOnErr: true}
streamingManagerOpt := func(bapp *baseapp.BaseApp) { bapp.SetStreamingManager(streamingManager) }
addListenerOpt := func(bapp *baseapp.BaseApp) { bapp.CommitMultiStore().AddListeners([]storetypes.StoreKey{distKey1}) }
suite := NewBaseAppSuite(t, streamingManagerOpt, addListenerOpt)
_, err := suite.baseApp.InitChain(&abci.InitChainRequest{
ConsensusParams: &tmproto.ConsensusParams{},
})
require.NoError(t, err)
ctx := getFinalizeBlockStateCtx(suite.baseApp)
sm := ctx.StreamingManager()
require.NotNil(t, sm, fmt.Sprintf("nil StreamingManager: %v", sm))
require.Equal(t, listeners, sm.ABCIListeners, fmt.Sprintf("should contain same listeners: %v", listeners))
require.Equal(t, true, sm.StopNodeOnErr, "should contain StopNodeOnErr = true")
nBlocks := 2
for blockN := 0; blockN < nBlocks; blockN++ {
_, err = suite.baseApp.FinalizeBlock(&abci.FinalizeBlockRequest{Height: int64(blockN) + 1})
require.NoError(t, err)
ctx := getFinalizeBlockStateCtx(suite.baseApp)
sm := ctx.StreamingManager()
require.NotNil(t, sm, fmt.Sprintf("nil StreamingManager: %v", sm))
require.Equal(t, listeners, sm.ABCIListeners, fmt.Sprintf("should contain same listeners: %v", listeners))
require.Equal(t, true, sm.StopNodeOnErr, "should contain StopNodeOnErr = true")
_, err = suite.baseApp.Commit()
require.NoError(t, err)
}
}
type mockAppDataListener struct {
appdata.Listener
startBlockData []appdata.StartBlockData
txData []appdata.TxData
eventData []appdata.EventData
kvPairData []appdata.KVPairData
commitData []appdata.CommitData
}
func newMockAppDataListener() *mockAppDataListener {
listener := &mockAppDataListener{}
// Initialize the Listener with custom behavior to store data
listener.Listener = appdata.Listener{
StartBlock: func(data appdata.StartBlockData) error {
listener.startBlockData = append(listener.startBlockData, data) // Store StartBlockData
return nil
},
OnTx: func(data appdata.TxData) error {
listener.txData = append(listener.txData, data) // Store TxData
return nil
},
OnEvent: func(data appdata.EventData) error {
listener.eventData = append(listener.eventData, data) // Store EventData
return nil
},
OnKVPair: func(data appdata.KVPairData) error {
listener.kvPairData = append(listener.kvPairData, data) // Store KVPairData
return nil
},
Commit: func(data appdata.CommitData) (func() error, error) {
listener.commitData = append(listener.commitData, data) // Store CommitData
return nil, nil
},
}
return listener
}
func TestAppDataListener(t *testing.T) {
anteKey := []byte("ante-key")
anteOpt := func(bapp *baseapp.BaseApp) { bapp.SetAnteHandler(anteHandlerTxTest(t, capKey1, anteKey)) }
distOpt := func(bapp *baseapp.BaseApp) { bapp.MountStores(distKey1) }
mockListener := newMockAppDataListener()
streamingManager := storetypes.StreamingManager{ABCIListeners: []storetypes.ABCIListener{baseapp.NewListenerWrapper(mockListener.Listener)}}
streamingManagerOpt := func(bapp *baseapp.BaseApp) { bapp.SetStreamingManager(streamingManager) }
addListenerOpt := func(bapp *baseapp.BaseApp) { bapp.CommitMultiStore().AddListeners([]storetypes.StoreKey{distKey1}) }
// for event tests
baseappOpts := func(app *baseapp.BaseApp) {
app.SetPreBlocker(func(ctx sdk.Context, req *abci.FinalizeBlockRequest) error {
ctx.EventManager().EmitEvent(sdk.NewEvent("pre-block"))
return nil
})
app.SetBeginBlocker(func(_ sdk.Context) (sdk.BeginBlock, error) {
return sdk.BeginBlock{
Events: []abci.Event{
{Type: "begin-block"},
},
}, nil
})
app.SetEndBlocker(func(_ sdk.Context) (sdk.EndBlock, error) {
return sdk.EndBlock{
Events: []abci.Event{
{Type: "end-block"},
},
}, nil
})
}
suite := NewBaseAppSuite(t, anteOpt, distOpt, streamingManagerOpt, addListenerOpt, baseappOpts)
_, err := suite.baseApp.InitChain(
&abci.InitChainRequest{
ConsensusParams: &tmproto.ConsensusParams{},
},
)
require.NoError(t, err)
deliverKey := []byte("deliver-key")
baseapptestutil.RegisterCounterServer(suite.baseApp.MsgServiceRouter(), CounterServerImpl{t, capKey1, deliverKey})
txCount := 5
txs := make([][]byte, txCount)
for i := 0; i < txCount; i++ {
tx := newTxCounter(t, suite.txConfig, suite.ac, int64(i), int64(i))
txBytes, err := suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)
sKey := []byte(fmt.Sprintf("distKey%d", i))
sVal := []byte(fmt.Sprintf("distVal%d", i))
store := getFinalizeBlockStateCtx(suite.baseApp).KVStore(distKey1)
store.Set(sKey, sVal)
txs[i] = txBytes
}
_, err = suite.baseApp.FinalizeBlock(&abci.FinalizeBlockRequest{Height: 1, Txs: txs})
require.NoError(t, err)
_, err = suite.baseApp.Commit()
require.NoError(t, err)
// StartBlockData
require.Len(t, mockListener.startBlockData, 1)
require.Equal(t, uint64(1), mockListener.startBlockData[0].Height)
// TxData
txData := mockListener.txData
require.Len(t, txData, len(txs))
for i := 0; i < txCount; i++ {
require.Equal(t, int32(i), txData[i].TxIndex)
txBytes, err := txData[i].Bytes()
require.NoError(t, err)
require.Equal(t, txs[i], txBytes)
}
// KVPairData
require.Len(t, mockListener.kvPairData, 1)
updates := mockListener.kvPairData[0].Updates
for i := 0; i < txCount; i++ {
require.Equal(t, []byte(distKey1.Name()), updates[i].Actor)
require.Len(t, updates[i].StateChanges, 1)
sKey := []byte(fmt.Sprintf("distKey%d", i))
sVal := []byte(fmt.Sprintf("distVal%d", i))
require.Equal(t, sKey, updates[i].StateChanges[0].Key)
require.Equal(t, sVal, updates[i].StateChanges[0].Value)
}
// CommitData
require.Len(t, mockListener.commitData, 1)
// EventData
require.Len(t, mockListener.eventData, 1)
events := mockListener.eventData[0].Events
require.Len(t, events, 3+txCount*3)
for i := 0; i < 3; i++ {
require.Equal(t, int32(0), events[i].TxIndex)
require.Equal(t, int32(0), events[i].MsgIndex)
require.Equal(t, int32(1), events[i].EventIndex)
attrs, err := events[i].Attributes()
require.NoError(t, err)
require.Len(t, attrs, 2)
switch i {
case 0:
require.Equal(t, appdata.PreBlockStage, events[i].BlockStage)
require.Equal(t, "pre-block", events[i].Type)
case 1:
require.Equal(t, appdata.BeginBlockStage, events[i].BlockStage)
require.Equal(t, "begin-block", events[i].Type)
case 2:
require.Equal(t, appdata.EndBlockStage, events[i].BlockStage)
require.Equal(t, "end-block", events[i].Type)
}
}
for i := 3; i < 3+txCount*3; i++ {
require.Equal(t, appdata.TxProcessingStage, events[i].BlockStage)
require.Equal(t, int32(i/3), events[i].TxIndex)
switch i % 3 {
case 0:
require.Equal(t, "ante_handler", events[i].Type)
require.Equal(t, int32(0), events[i].MsgIndex)
require.Equal(t, int32(0), events[i].EventIndex)
attrs, err := events[i].Attributes()
require.NoError(t, err)
require.Len(t, attrs, 2)
case 1:
require.Equal(t, "message", events[i].Type)
require.Equal(t, int32(1), events[i].MsgIndex)
require.Equal(t, int32(1), events[i].EventIndex)
attrs, err := events[i].Attributes()
require.NoError(t, err)
require.Len(t, attrs, 5)
case 2:
require.Equal(t, "message", events[i].Type)
require.Equal(t, int32(1), events[i].MsgIndex)
require.Equal(t, int32(2), events[i].EventIndex)
attrs, err := events[i].Attributes()
require.NoError(t, err)
require.Len(t, attrs, 4)
}
}
}