forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.go
278 lines (241 loc) · 7.42 KB
/
testing.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
// TODO(@Wondertan): Ideally, we should move that into subpackage, so this does not get included
// into binary of production code, but that does not matter at the moment.
package header
import (
"context"
"github.com/celestiaorg/celestia-node/share"
mrand "math/rand"
"testing"
"time"
"github.com/ipfs/go-blockservice"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/libs/bytes"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/proto/tendermint/version"
"github.com/tendermint/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
"github.com/celestiaorg/celestia-app/pkg/da"
"github.com/celestiaorg/celestia-node/core"
)
// TestSuite provides everything you need to test chain of Headers.
// If not, please don't hesitate to extend it for your case.
type TestSuite struct {
t *testing.T
vals []types.PrivValidator
valSet *types.ValidatorSet
valPntr int
head *ExtendedHeader
}
// NewTestSuite setups a new test suite with a given number of validators.
func NewTestSuite(t *testing.T, num int) *TestSuite {
valSet, vals := core.RandValidatorSet(num, 10)
return &TestSuite{
t: t,
vals: vals,
valSet: valSet,
}
}
func (s *TestSuite) genesis() *ExtendedHeader {
dah := EmptyDAH()
gen := RandRawHeader(s.t)
gen.DataHash = dah.Hash()
gen.ValidatorsHash = s.valSet.Hash()
gen.NextValidatorsHash = s.valSet.Hash()
gen.Height = 1
voteSet := types.NewVoteSet(gen.ChainID, gen.Height, 0, tmproto.PrecommitType, s.valSet)
commit, err := core.MakeCommit(RandBlockID(s.t), gen.Height, 0, voteSet, s.vals, time.Now())
require.NoError(s.t, err)
eh := &ExtendedHeader{
RawHeader: *gen,
Commit: commit,
ValidatorSet: s.valSet,
DAH: &dah,
}
require.NoError(s.t, eh.ValidateBasic())
return eh
}
func (s *TestSuite) Head() *ExtendedHeader {
if s.head == nil {
s.head = s.genesis()
}
return s.head
}
func (s *TestSuite) GenExtendedHeaders(num int) []*ExtendedHeader {
headers := make([]*ExtendedHeader, num)
for i := range headers {
headers[i] = s.GenExtendedHeader()
}
return headers
}
func (s *TestSuite) GenExtendedHeader() *ExtendedHeader {
if s.head == nil {
s.head = s.genesis()
return s.head
}
dah := da.MinDataAvailabilityHeader()
height := s.Head().Height + 1
rh := s.GenRawHeader(height, s.Head().Hash(), s.Head().Commit.Hash(), dah.Hash())
s.head = &ExtendedHeader{
RawHeader: *rh,
Commit: s.Commit(rh),
ValidatorSet: s.valSet,
DAH: &dah,
}
require.NoError(s.t, s.head.ValidateBasic())
return s.head
}
func (s *TestSuite) GenRawHeader(
height int64, lastHeader, lastCommit, dataHash bytes.HexBytes) *RawHeader {
rh := RandRawHeader(s.t)
rh.Height = height
rh.Time = time.Now()
rh.LastBlockID = types.BlockID{Hash: lastHeader}
rh.LastCommitHash = lastCommit
rh.DataHash = dataHash
rh.ValidatorsHash = s.valSet.Hash()
rh.NextValidatorsHash = s.valSet.Hash()
rh.ProposerAddress = s.nextProposer().Address
return rh
}
func (s *TestSuite) Commit(h *RawHeader) *types.Commit {
bid := types.BlockID{
Hash: h.Hash(),
// Unfortunately, we still have to commit PartSetHeader even we don't need it in Celestia
PartSetHeader: types.PartSetHeader{Total: 1, Hash: tmrand.Bytes(32)},
}
round := int32(0)
comms := make([]types.CommitSig, len(s.vals))
for i, val := range s.vals {
v := &types.Vote{
ValidatorAddress: s.valSet.Validators[i].Address,
ValidatorIndex: int32(i),
Height: h.Height,
Round: round,
Timestamp: tmtime.Now(),
Type: tmproto.PrecommitType,
BlockID: bid,
}
sgntr, err := val.(types.MockPV).PrivKey.Sign(types.VoteSignBytes(h.ChainID, v.ToProto()))
require.Nil(s.t, err)
v.Signature = sgntr
comms[i] = v.CommitSig()
}
return types.NewCommit(h.Height, round, bid, comms)
}
func (s *TestSuite) nextProposer() *types.Validator {
if s.valPntr == len(s.valSet.Validators)-1 {
s.valPntr = 0
} else {
s.valPntr++
}
val := s.valSet.Validators[s.valPntr]
return val
}
// RandExtendedHeader provides an ExtendedHeader fixture.
func RandExtendedHeader(t *testing.T) *ExtendedHeader {
dah := EmptyDAH()
rh := RandRawHeader(t)
rh.DataHash = dah.Hash()
valSet, vals := core.RandValidatorSet(3, 1)
rh.ValidatorsHash = valSet.Hash()
voteSet := types.NewVoteSet(rh.ChainID, rh.Height, 0, tmproto.PrecommitType, valSet)
commit, err := core.MakeCommit(RandBlockID(t), rh.Height, 0, voteSet, vals, time.Now())
require.NoError(t, err)
return &ExtendedHeader{
RawHeader: *rh,
Commit: commit,
ValidatorSet: valSet,
DAH: &dah,
}
}
// RandRawHeader provides a RawHeader fixture.
func RandRawHeader(t *testing.T) *RawHeader {
return &RawHeader{
Version: version.Consensus{Block: 11, App: 1},
ChainID: "test",
Height: mrand.Int63(),
Time: time.Now(),
LastBlockID: RandBlockID(t),
LastCommitHash: tmrand.Bytes(32),
DataHash: tmrand.Bytes(32),
ValidatorsHash: tmrand.Bytes(32),
NextValidatorsHash: tmrand.Bytes(32),
ConsensusHash: tmrand.Bytes(32),
AppHash: tmrand.Bytes(32),
LastResultsHash: tmrand.Bytes(32),
EvidenceHash: tmhash.Sum([]byte{}),
ProposerAddress: tmrand.Bytes(20),
}
}
// RandBlockID provides a BlockID fixture.
func RandBlockID(t *testing.T) types.BlockID {
bid := types.BlockID{
Hash: make([]byte, 32),
PartSetHeader: types.PartSetHeader{
Total: 123,
Hash: make([]byte, 32),
},
}
mrand.Read(bid.Hash)
mrand.Read(bid.PartSetHeader.Hash)
return bid
}
// FraudMaker creates a custom ConstructFn that breaks the block at the given height.
func FraudMaker(t *testing.T, faultHeight int64) ConstructFn {
log.Warn("Corrupting block...", "height", faultHeight)
return func(ctx context.Context,
b *types.Block,
comm *types.Commit,
vals *types.ValidatorSet,
bServ blockservice.BlockService) (*ExtendedHeader, error) {
if b.Height == faultHeight {
eh := &ExtendedHeader{
RawHeader: b.Header,
Commit: comm,
ValidatorSet: vals,
}
eh = CreateFraudExtHeader(t, eh, bServ)
return eh, nil
}
return MakeExtendedHeader(ctx, b, comm, vals, bServ)
}
}
func CreateFraudExtHeader(t *testing.T, eh *ExtendedHeader, dag blockservice.BlockService) *ExtendedHeader {
extended := share.RandEDS(t, 2)
shares := share.ExtractEDS(extended)
copy(shares[0][share.NamespaceSize:], shares[1][share.NamespaceSize:])
extended, err := share.ImportShares(context.Background(), shares, dag)
require.NoError(t, err)
dah := da.NewDataAvailabilityHeader(extended)
eh.DAH = &dah
eh.RawHeader.DataHash = dah.Hash()
return eh
}
type DummySubscriber struct {
Headers []*ExtendedHeader
}
func (mhs *DummySubscriber) AddValidator(Validator) error {
return nil
}
func (mhs *DummySubscriber) Subscribe() (Subscription, error) {
return mhs, nil
}
func (mhs *DummySubscriber) NextHeader(ctx context.Context) (*ExtendedHeader, error) {
defer func() {
if len(mhs.Headers) > 1 {
// pop the already-returned header
cp := mhs.Headers
mhs.Headers = cp[1:]
} else {
mhs.Headers = make([]*ExtendedHeader, 0)
}
}()
if len(mhs.Headers) == 0 {
return nil, context.Canceled
}
return mhs.Headers[0], nil
}
func (mhs *DummySubscriber) Stop(context.Context) error { return nil }
func (mhs *DummySubscriber) Cancel() {}