forked from Layr-Labs/eigenda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment_test.go
198 lines (155 loc) · 4.83 KB
/
assignment_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
package core_test
import (
"context"
"fmt"
"math/rand"
"testing"
"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigenda/core/mock"
"github.com/Layr-Labs/eigenda/encoding"
"github.com/stretchr/testify/assert"
)
func TestOperatorAssignments(t *testing.T) {
state := dat.GetTotalOperatorState(context.Background(), 0)
operatorState := state.OperatorState
coordinator := &core.StdAssignmentCoordinator{}
quorumInfo := &core.BlobQuorumInfo{
SecurityParam: core.SecurityParam{
QuorumID: 0,
AdversaryThreshold: 50,
ConfirmationThreshold: 100,
},
ChunkLength: 10,
}
blobLength := uint(100)
assignments, info, err := coordinator.GetAssignments(operatorState, blobLength, quorumInfo)
assert.NoError(t, err)
expectedAssignments := map[core.OperatorID]core.Assignment{
mock.MakeOperatorId(0): {
StartIndex: 0,
NumChunks: 1,
},
mock.MakeOperatorId(1): {
StartIndex: 1,
NumChunks: 2,
},
mock.MakeOperatorId(2): {
StartIndex: 3,
NumChunks: 3,
},
mock.MakeOperatorId(3): {
StartIndex: 6,
NumChunks: 4,
},
mock.MakeOperatorId(4): {
StartIndex: 10,
NumChunks: 5,
},
mock.MakeOperatorId(5): {
StartIndex: 15,
NumChunks: 6,
},
mock.MakeOperatorId(6): {
StartIndex: 21,
NumChunks: 3,
},
mock.MakeOperatorId(7): {
StartIndex: 14,
NumChunks: 3,
},
mock.MakeOperatorId(8): {
StartIndex: 17,
NumChunks: 4,
},
mock.MakeOperatorId(9): {
StartIndex: 21,
NumChunks: 4,
},
}
expectedInfo := core.AssignmentInfo{
TotalChunks: 21,
}
assert.Equal(t, expectedInfo, info)
for operatorID, assignment := range assignments {
assert.Equal(t, assignment, expectedAssignments[operatorID])
header := &core.BlobHeader{
BlobCommitments: encoding.BlobCommitments{
Length: blobLength,
},
QuorumInfos: []*core.BlobQuorumInfo{quorumInfo},
}
assignment, info, err := coordinator.GetOperatorAssignment(operatorState, header, 0, operatorID)
assert.NoError(t, err)
assert.Equal(t, assignment, expectedAssignments[operatorID])
assert.Equal(t, expectedInfo, info)
}
}
func FuzzOperatorAssignments(f *testing.F) {
// Add distributions to fuzz
asn := &core.StdAssignmentCoordinator{}
for i := 1; i < 100; i++ {
f.Add(i, true)
}
for i := 1; i < 100; i++ {
f.Add(i, false)
}
for i := 0; i < 100; i++ {
f.Add(rand.Intn(254)+1, rand.Intn(2) == 0)
}
f.Fuzz(func(t *testing.T, numOperators int, useTargetNumChunks bool) {
// Generate a random slice of integers of length n
stakes := map[core.QuorumID]map[core.OperatorID]int{
0: {},
}
for i := 0; i < numOperators; i++ {
stakes[0][mock.MakeOperatorId(i)] = rand.Intn(100) + 1
}
advThreshold := rand.Intn(99)
quorumThreshold := rand.Intn(100-advThreshold) + advThreshold + 1
param := &core.SecurityParam{
QuorumID: 0,
AdversaryThreshold: uint8(advThreshold),
ConfirmationThreshold: uint8(quorumThreshold),
}
dat, err := mock.NewChainDataMock(stakes)
if err != nil {
t.Fatal(err)
}
state := dat.GetTotalOperatorState(context.Background(), 0)
blobLength := uint(rand.Intn(100000))
targetNumChunks := uint(0)
if useTargetNumChunks {
targetNumChunks = uint(rand.Intn(1000))
}
fmt.Println("advThreshold", advThreshold, "quorumThreshold", quorumThreshold, "numOperators", numOperators, "blobLength", blobLength)
chunkLength, err := asn.CalculateChunkLength(state.OperatorState, blobLength, targetNumChunks, param)
assert.NoError(t, err)
quorumInfo := &core.BlobQuorumInfo{
SecurityParam: *param,
ChunkLength: chunkLength,
}
ok, err := asn.ValidateChunkLength(state.OperatorState, blobLength, quorumInfo)
assert.NoError(t, err)
assert.True(t, ok)
assignments, info, err := asn.GetAssignments(state.OperatorState, blobLength, quorumInfo)
assert.NoError(t, err)
// fmt.Println("advThreshold", advThreshold, "quorumThreshold", quorumThreshold, "numOperators", numOperators, "chunkLength", chunkLength, "blobLength", blobLength)
if useTargetNumChunks {
quorumInfo.ChunkLength = chunkLength * 2
ok, err := asn.ValidateChunkLength(state.OperatorState, blobLength, quorumInfo)
// Make sure that the number of chunks is less than the target
// TODO: Make sure that the number of chunks is no less than half the target (this currently fails in some rare cases
// but it isn't a critical problem)
if ok && err == nil {
assert.GreaterOrEqual(t, targetNumChunks, info.TotalChunks)
}
}
// Check that each operator's assignment satisfies the security requirement
for operatorID, assignment := range assignments {
totalStake := state.Totals[0].Stake
myStake := state.Operators[0][operatorID].Stake
valid := assignment.NumChunks*uint((quorumThreshold-advThreshold))*chunkLength*uint(totalStake.Uint64()) >= blobLength*uint(myStake.Uint64())
assert.True(t, valid)
}
})
}