forked from dymensionxyz/dymint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor_test.go
267 lines (232 loc) · 8.72 KB
/
executor_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
package state
import (
"context"
"crypto/rand"
"testing"
"time"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/libs/pubsub/query"
"github.com/tendermint/tendermint/proxy"
tmtypes "github.com/tendermint/tendermint/types"
abciconv "github.com/furyaxyz/furyint/conv/abci"
"github.com/furyaxyz/furyint/mempool"
mempoolv1 "github.com/furyaxyz/furyint/mempool/v1"
"github.com/furyaxyz/furyint/mocks"
"github.com/furyaxyz/furyint/types"
)
func TestCreateBlock(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
logger := log.TestingLogger()
app := &mocks.Application{}
app.On("CheckTx", mock.Anything).Return(abci.ResponseCheckTx{})
clientCreator := proxy.NewLocalClientCreator(app)
abciClient, err := clientCreator.NewABCIClient()
require.NoError(err)
require.NotNil(clientCreator)
require.NotNil(abciClient)
nsID := [8]byte{1, 2, 3, 4, 5, 6, 7, 8}
mpool := mempoolv1.NewTxMempool(logger, cfg.DefaultMempoolConfig(), proxy.NewAppConnMempool(abciClient), 0)
executor := NewBlockExecutor([]byte("test address"), nsID, "test", mpool, proxy.NewAppConns(clientCreator), nil, logger)
state := types.State{}
state.ConsensusParams.Block.MaxBytes = 100
state.ConsensusParams.Block.MaxGas = 100000
state.Validators = tmtypes.NewValidatorSet(nil)
// empty block
block := executor.CreateBlock(1, &types.Commit{}, [32]byte{}, state)
require.NotNil(block)
assert.Empty(block.Data.Txs)
assert.Equal(uint64(1), block.Header.Height)
// one small Tx
err = mpool.CheckTx([]byte{1, 2, 3, 4}, func(r *abci.Response) {}, mempool.TxInfo{})
require.NoError(err)
block = executor.CreateBlock(2, &types.Commit{}, [32]byte{}, state)
require.NotNil(block)
assert.Equal(uint64(2), block.Header.Height)
assert.Len(block.Data.Txs, 1)
// now there are 3 Txs, and only two can fit into single block
err = mpool.CheckTx([]byte{4, 5, 6, 7}, func(r *abci.Response) {}, mempool.TxInfo{})
require.NoError(err)
err = mpool.CheckTx(make([]byte, 100), func(r *abci.Response) {}, mempool.TxInfo{})
require.NoError(err)
block = executor.CreateBlock(3, &types.Commit{}, [32]byte{}, state)
require.NotNil(block)
assert.Len(block.Data.Txs, 2)
}
func TestApplyBlock(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
logger := log.TestingLogger()
// Mock ABCI app
app := &mocks.Application{}
app.On("CheckTx", mock.Anything).Return(abci.ResponseCheckTx{})
app.On("BeginBlock", mock.Anything).Return(abci.ResponseBeginBlock{})
app.On("DeliverTx", mock.Anything).Return(abci.ResponseDeliverTx{})
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{})
var mockAppHash [32]byte
_, err := rand.Read(mockAppHash[:])
require.NoError(err)
app.On("Commit", mock.Anything).Return(abci.ResponseCommit{
Data: mockAppHash[:],
})
app.On("Info", mock.Anything).Return(abci.ResponseInfo{
LastBlockHeight: 0,
LastBlockAppHash: []byte{0},
})
// Mock ABCI client
clientCreator := proxy.NewLocalClientCreator(app)
abciClient, err := clientCreator.NewABCIClient()
require.NoError(err)
require.NotNil(clientCreator)
require.NotNil(abciClient)
nsID := [8]byte{1, 2, 3, 4, 5, 6, 7, 8}
chainID := "test"
// Init mempool
mpool := mempoolv1.NewTxMempool(logger, cfg.DefaultMempoolConfig(), proxy.NewAppConnMempool(abciClient), 0)
eventBus := tmtypes.NewEventBus()
require.NoError(eventBus.Start())
// Mock app connections
appConns := &mocks.AppConns{}
appConns.On("Consensus").Return(abciClient)
appConns.On("Query").Return(abciClient)
executor := NewBlockExecutor([]byte("test address"), nsID, chainID, mpool, appConns, eventBus, logger)
// Subscribe to tx events
txQuery, err := query.New("tm.event='Tx'")
require.NoError(err)
txSub, err := eventBus.Subscribe(context.Background(), "test", txQuery, 1000)
require.NoError(err)
require.NotNil(txSub)
// Subscribe to block header events
headerQuery, err := query.New("tm.event='NewBlockHeader'")
require.NoError(err)
headerSub, err := eventBus.Subscribe(context.Background(), "test", headerQuery, 100)
require.NoError(err)
require.NotNil(headerSub)
// Init state
state := types.State{
NextValidators: tmtypes.NewValidatorSet(nil),
Validators: tmtypes.NewValidatorSet(nil),
LastValidators: tmtypes.NewValidatorSet(nil),
}
state.InitialHeight = 1
state.LastBlockHeight = 0
state.ConsensusParams.Block.MaxBytes = 100
state.ConsensusParams.Block.MaxGas = 100000
// Create first block with one Tx from mempool
_ = mpool.CheckTx([]byte{1, 2, 3, 4}, func(r *abci.Response) {}, mempool.TxInfo{})
require.NoError(err)
block := executor.CreateBlock(1, &types.Commit{Height: 0}, [32]byte{}, state)
require.NotNil(block)
assert.Equal(uint64(1), block.Header.Height)
assert.Len(block.Data.Txs, 1)
// Create proposer for the block
proposerKey := ed25519.GenPrivKey()
proposer := &types.Sequencer{
PublicKey: proposerKey.PubKey(),
}
// Create commit for the block
abciHeaderPb := abciconv.ToABCIHeaderPB(&block.Header)
abciHeaderBytes, err := abciHeaderPb.Marshal()
require.NoError(err)
signature, err := proposerKey.Sign(abciHeaderBytes)
require.NoError(err)
commit := &types.Commit{
Height: block.Header.Height,
HeaderHash: block.Header.Hash(),
Signatures: []types.Signature{signature},
}
// Apply the block
err = executor.Validate(state, block, commit, proposer)
require.NoError(err)
resp, err := executor.Execute(context.Background(), state, block)
require.NoError(err)
require.NotNil(resp)
newState, err := executor.UpdateStateFromResponses(resp, state, block)
require.NoError(err)
require.NotNil(newState)
assert.Equal(int64(1), newState.LastBlockHeight)
err = executor.Commit(context.Background(), &newState, block, resp)
require.NoError(err)
assert.Equal(mockAppHash, newState.AppHash)
newState.LastStoreHeight = uint64(newState.LastBlockHeight)
// Create another block with multiple Tx from mempool
require.NoError(mpool.CheckTx([]byte{0, 1, 2, 3, 4}, func(r *abci.Response) {}, mempool.TxInfo{}))
require.NoError(mpool.CheckTx([]byte{5, 6, 7, 8, 9}, func(r *abci.Response) {}, mempool.TxInfo{}))
require.NoError(mpool.CheckTx([]byte{1, 2, 3, 4, 5}, func(r *abci.Response) {}, mempool.TxInfo{}))
require.NoError(mpool.CheckTx(make([]byte, 90), func(r *abci.Response) {}, mempool.TxInfo{}))
block = executor.CreateBlock(2, commit, [32]byte{}, newState)
require.NotNil(block)
assert.Equal(uint64(2), block.Header.Height)
assert.Len(block.Data.Txs, 3)
// Get the header bytes
abciHeaderPb = abciconv.ToABCIHeaderPB(&block.Header)
abciHeaderBytes, err = abciHeaderPb.Marshal()
require.NoError(err)
// Create invalid commit for the block with wrong signature
invalidProposerKey := ed25519.GenPrivKey()
invalidSignature, err := invalidProposerKey.Sign(abciHeaderBytes)
require.NoError(err)
invalidCommit := &types.Commit{
Height: block.Header.Height,
HeaderHash: block.Header.Hash(),
Signatures: []types.Signature{invalidSignature},
}
// Apply the block with an invalid commit
err = executor.Validate(state, block, invalidCommit, proposer)
require.Error(types.ErrInvalidSignature)
// Create a valid commit for the block
signature, err = proposerKey.Sign(abciHeaderBytes)
require.NoError(err)
commit = &types.Commit{
Height: block.Header.Height,
HeaderHash: block.Header.Hash(),
Signatures: []types.Signature{signature},
}
// Apply the block
err = executor.Validate(newState, block, commit, proposer)
require.NoError(err)
resp, err = executor.Execute(context.Background(), state, block)
require.NoError(err)
require.NotNil(resp)
newState, err = executor.UpdateStateFromResponses(resp, state, block)
require.NoError(err)
require.NotNil(newState)
assert.Equal(int64(2), newState.LastBlockHeight)
err = executor.Commit(context.Background(), &newState, block, resp)
require.NoError(err)
// wait for at least 4 Tx events, for up to 3 second.
// 3 seconds is a fail-scenario only
timer := time.NewTimer(3 * time.Second)
txs := make(map[int64]int)
cnt := 0
for cnt != 4 {
select {
case evt := <-txSub.Out():
cnt++
data, ok := evt.Data().(tmtypes.EventDataTx)
assert.True(ok)
assert.NotEmpty(data.Tx)
txs[data.Height]++
case <-timer.C:
t.FailNow()
}
}
assert.Zero(len(txSub.Out())) // expected exactly 4 Txs - channel should be empty
assert.EqualValues(1, txs[1])
assert.EqualValues(3, txs[2])
require.EqualValues(2, len(headerSub.Out()))
for h := 1; h <= 2; h++ {
evt := <-headerSub.Out()
data, ok := evt.Data().(tmtypes.EventDataNewBlockHeader)
assert.True(ok)
if data.Header.Height == 2 {
assert.EqualValues(3, data.NumTxs)
}
}
}