-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathsparse_shares_test.go
172 lines (143 loc) · 5.27 KB
/
sparse_shares_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
package shares
import (
"bytes"
"fmt"
"testing"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/nmt/namespace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
coretypes "github.com/tendermint/tendermint/types"
)
func Test_parseSparseShares(t *testing.T) {
// exactMsgShareSize is the length of message that will fit exactly into a
// single share, accounting for namespace id and the length delimiter
// prepended to each message. Note that the length delimiter can be 1 to 10
// bytes (varint) but this test assumes it is 2 bytes.
const exactMsgShareSize = appconsts.SparseShareContentSize - 2
type test struct {
name string
msgSize int
msgCount int
}
// each test is ran twice, once using msgSize as an exact size, and again
// using it as a cap for randomly sized leaves
tests := []test{
{"single small msg", appconsts.SparseShareContentSize / 2, 1},
{"many small msgs", appconsts.SparseShareContentSize / 2, 10},
{"single big msg", appconsts.SparseShareContentSize * 4, 1},
{"many big msgs", appconsts.SparseShareContentSize * 4, 10},
{"single exact size msg", exactMsgShareSize, 1},
{"many exact size msgs", appconsts.SparseShareContentSize, 10},
}
for _, tc := range tests {
tc := tc
// run the tests with identically sized messagses
t.Run(fmt.Sprintf("%s identically sized ", tc.name), func(t *testing.T) {
rawmsgs := make([]coretypes.Message, tc.msgCount)
for i := 0; i < tc.msgCount; i++ {
rawmsgs[i] = generateRandomMessage(tc.msgSize)
}
msgs := coretypes.Messages{MessagesList: rawmsgs}
msgs.SortMessages()
shares, _ := SplitMessages(0, nil, msgs.MessagesList, false)
rawShares := ToBytes(shares)
parsedMsgs, err := parseSparseShares(rawShares, appconsts.SupportedShareVersions)
if err != nil {
t.Error(err)
}
// check that the namespaces and data are the same
for i := 0; i < len(msgs.MessagesList); i++ {
assert.Equal(t, msgs.MessagesList[i].NamespaceID, parsedMsgs[i].NamespaceID)
assert.Equal(t, msgs.MessagesList[i].Data, parsedMsgs[i].Data)
}
})
// run the same tests using randomly sized messages with caps of tc.msgSize
t.Run(fmt.Sprintf("%s randomly sized", tc.name), func(t *testing.T) {
msgs := generateRandomlySizedMessages(tc.msgCount, tc.msgSize)
shares, _ := SplitMessages(0, nil, msgs.MessagesList, false)
rawShares := make([][]byte, len(shares))
for i, share := range shares {
rawShares[i] = []byte(share)
}
parsedMsgs, err := parseSparseShares(rawShares, appconsts.SupportedShareVersions)
if err != nil {
t.Error(err)
}
// check that the namespaces and data are the same
for i := 0; i < len(msgs.MessagesList); i++ {
assert.Equal(t, msgs.MessagesList[i].NamespaceID, parsedMsgs[i].NamespaceID)
assert.Equal(t, msgs.MessagesList[i].Data, parsedMsgs[i].Data)
}
})
}
}
func Test_parseSparseSharesErrors(t *testing.T) {
type testCase struct {
name string
rawShares [][]byte
}
unsupportedShareVersion := 5
infoByte, _ := NewInfoByte(uint8(unsupportedShareVersion), true)
rawShare := []byte{}
rawShare = append(rawShare, namespace.ID{1, 1, 1, 1, 1, 1, 1, 1}...)
rawShare = append(rawShare, byte(infoByte))
rawShare = append(rawShare, bytes.Repeat([]byte{0}, appconsts.ShareSize-len(rawShare))...)
tests := []testCase{
{
"share with unsupported share version",
[][]byte{rawShare},
},
}
for _, tt := range tests {
t.Run(tt.name, func(*testing.T) {
_, err := parseSparseShares(tt.rawShares, appconsts.SupportedShareVersions)
assert.Error(t, err)
})
}
}
func TestParsePaddedMsg(t *testing.T) {
msgWr := NewSparseShareSplitter()
randomSmallMsg := generateRandomMessage(appconsts.SparseShareContentSize / 2)
randomLargeMsg := generateRandomMessage(appconsts.SparseShareContentSize * 4)
msgs := coretypes.Messages{
MessagesList: []coretypes.Message{
randomSmallMsg,
randomLargeMsg,
},
}
msgs.SortMessages()
msgWr.Write(msgs.MessagesList[0])
msgWr.WriteNamespacedPaddedShares(4)
msgWr.Write(msgs.MessagesList[1])
msgWr.WriteNamespacedPaddedShares(10)
shares := msgWr.Export()
rawShares := ToBytes(shares)
pmsgs, err := parseSparseShares(rawShares, appconsts.SupportedShareVersions)
require.NoError(t, err)
require.Equal(t, msgs.MessagesList, pmsgs)
}
func TestMsgShareContainsInfoByte(t *testing.T) {
sss := NewSparseShareSplitter()
smallMsg := generateRandomMessage(appconsts.SparseShareContentSize / 2)
sss.Write(smallMsg)
shares := sss.Export()
got := shares[0][appconsts.NamespaceSize : appconsts.NamespaceSize+appconsts.ShareInfoBytes][0]
isMessageStart := true
want, err := NewInfoByte(appconsts.ShareVersion, isMessageStart)
require.NoError(t, err)
assert.Equal(t, byte(want), got)
}
func TestContiguousMsgShareContainsInfoByte(t *testing.T) {
sss := NewSparseShareSplitter()
longMsg := generateRandomMessage(appconsts.SparseShareContentSize * 4)
sss.Write(longMsg)
shares := sss.Export()
// we expect longMsg to occupy more than one share
assert.Condition(t, func() bool { return len(shares) > 1 })
got := shares[1][appconsts.NamespaceSize : appconsts.NamespaceSize+appconsts.ShareInfoBytes][0]
isMessageStart := false
want, err := NewInfoByte(appconsts.ShareVersion, isMessageStart)
require.NoError(t, err)
assert.Equal(t, byte(want), got)
}