forked from Layr-Labs/eigenda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
229 lines (191 loc) · 7.22 KB
/
validator.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
package core
import (
"errors"
"fmt"
"github.com/Layr-Labs/eigenda/common"
"github.com/Layr-Labs/eigenda/encoding"
)
var (
ErrChunkLengthMismatch = errors.New("chunk length mismatch")
ErrBlobQuorumSkip = errors.New("blob skipped for a quorum before verification")
)
type ShardValidator interface {
ValidateBatch(*BatchHeader, []*BlobMessage, *OperatorState, common.WorkerPool) error
ValidateBlobs(blobs []*BlobMessage, operatorState *OperatorState, pool common.WorkerPool) error
UpdateOperatorID(OperatorID)
}
// shardValidator implements the validation logic that a DA node should apply to its received data
type shardValidator struct {
verifier encoding.Verifier
assignment AssignmentCoordinator
chainState ChainState
operatorID OperatorID
}
func NewShardValidator(v encoding.Verifier, asgn AssignmentCoordinator, cst ChainState, operatorID OperatorID) ShardValidator {
return &shardValidator{
verifier: v,
assignment: asgn,
chainState: cst,
operatorID: operatorID,
}
}
func (v *shardValidator) validateBlobQuorum(quorumHeader *BlobQuorumInfo, blob *BlobMessage, operatorState *OperatorState) ([]*encoding.Frame, *Assignment, *encoding.EncodingParams, error) {
if err := ValidateSecurityParam(uint32(quorumHeader.ConfirmationThreshold), uint32(quorumHeader.AdversaryThreshold)); err != nil {
return nil, nil, nil, err
}
// Check if the operator is a member of the quorum
if _, ok := operatorState.Operators[quorumHeader.QuorumID]; !ok {
return nil, nil, nil, fmt.Errorf("%w: operator %s is not a member of quorum %d", ErrBlobQuorumSkip, v.operatorID.Hex(), quorumHeader.QuorumID)
}
// Get the assignments for the quorum
assignment, info, err := v.assignment.GetOperatorAssignment(operatorState, blob.BlobHeader, quorumHeader.QuorumID, v.operatorID)
if err != nil {
return nil, nil, nil, err
}
// Validate the number of chunks
if assignment.NumChunks == 0 {
return nil, nil, nil, fmt.Errorf("%w: operator %s has no chunks in quorum %d", ErrBlobQuorumSkip, v.operatorID.Hex(), quorumHeader.QuorumID)
}
if assignment.NumChunks != uint(len(blob.Bundles[quorumHeader.QuorumID])) {
return nil, nil, nil, fmt.Errorf("number of chunks (%d) does not match assignment (%d) for quorum %d", len(blob.Bundles[quorumHeader.QuorumID]), assignment.NumChunks, quorumHeader.QuorumID)
}
// Validate the chunkLength against the confirmation and adversary threshold parameters
ok, err := v.assignment.ValidateChunkLength(operatorState, blob.BlobHeader.Length, quorumHeader)
if err != nil || !ok {
return nil, nil, nil, fmt.Errorf("invalid chunk length: %w", err)
}
// Get the chunk length
chunks := blob.Bundles[quorumHeader.QuorumID]
for _, chunk := range chunks {
if uint(chunk.Length()) != quorumHeader.ChunkLength {
return nil, nil, nil, fmt.Errorf("%w: chunk length (%d) does not match quorum header (%d) for quorum %d", ErrChunkLengthMismatch, chunk.Length(), quorumHeader.ChunkLength, quorumHeader.QuorumID)
}
}
// Check the received chunks against the commitment
params := encoding.ParamsFromMins(quorumHeader.ChunkLength, info.TotalChunks)
if params.ChunkLength != uint64(quorumHeader.ChunkLength) {
return nil, nil, nil, fmt.Errorf("%w: chunk length from encoding parameters (%d) does not match quorum header (%d)", ErrChunkLengthMismatch, params.ChunkLength, quorumHeader.ChunkLength)
}
return chunks, &assignment, ¶ms, nil
}
func (v *shardValidator) UpdateOperatorID(operatorID OperatorID) {
v.operatorID = operatorID
}
func (v *shardValidator) ValidateBatch(batchHeader *BatchHeader, blobs []*BlobMessage, operatorState *OperatorState, pool common.WorkerPool) error {
headers := make([]*BlobHeader, len(blobs))
for i, blob := range blobs {
headers[i] = blob.BlobHeader
}
err := ValidateBatchHeaderRoot(batchHeader, headers)
if err != nil {
return err
}
return v.ValidateBlobs(blobs, operatorState, pool)
}
func (v *shardValidator) ValidateBlobs(blobs []*BlobMessage, operatorState *OperatorState, pool common.WorkerPool) error {
var err error
subBatchMap := make(map[encoding.EncodingParams]*encoding.SubBatch)
blobCommitmentList := make([]encoding.BlobCommitments, len(blobs))
for k, blob := range blobs {
if len(blob.Bundles) != len(blob.BlobHeader.QuorumInfos) {
return fmt.Errorf("number of bundles (%d) does not match number of quorums (%d)", len(blob.Bundles), len(blob.BlobHeader.QuorumInfos))
}
// Saved for the blob length validation
blobCommitmentList[k] = blob.BlobHeader.BlobCommitments
// for each quorum
for _, quorumHeader := range blob.BlobHeader.QuorumInfos {
chunks, assignment, params, err := v.validateBlobQuorum(quorumHeader, blob, operatorState)
if errors.Is(err, ErrBlobQuorumSkip) {
continue
} else if err != nil {
return err
} else {
// Check the received chunks against the commitment
blobIndex := 0
subBatch, ok := subBatchMap[*params]
if ok {
blobIndex = subBatch.NumBlobs
}
indices := assignment.GetIndices()
samples := make([]encoding.Sample, len(chunks))
for ind := range chunks {
samples[ind] = encoding.Sample{
Commitment: blob.BlobHeader.BlobCommitments.Commitment,
Chunk: chunks[ind],
AssignmentIndex: uint(indices[ind]),
BlobIndex: blobIndex,
}
}
// update subBatch
if !ok {
subBatchMap[*params] = &encoding.SubBatch{
Samples: samples,
NumBlobs: 1,
}
} else {
subBatch.Samples = append(subBatch.Samples, samples...)
subBatch.NumBlobs += 1
}
}
}
}
// Parallelize the universal verification for each subBatch
numResult := len(subBatchMap) + len(blobCommitmentList)
// create a channel to accept results, we don't use stop
out := make(chan error, numResult)
// parallelize subBatch verification
for params, subBatch := range subBatchMap {
params := params
subBatch := subBatch
pool.Submit(func() {
v.universalVerifyWorker(params, subBatch, out)
})
}
// parallelize length proof verification
for _, blobCommitments := range blobCommitmentList {
blobCommitments := blobCommitments
pool.Submit(func() {
v.VerifyBlobLengthWorker(blobCommitments, out)
})
}
// check if commitments are equivalent
err = v.verifier.VerifyCommitEquivalenceBatch(blobCommitmentList)
if err != nil {
return err
}
for i := 0; i < numResult; i++ {
err := <-out
if err != nil {
return err
}
}
return nil
}
func (v *shardValidator) universalVerifyWorker(params encoding.EncodingParams, subBatch *encoding.SubBatch, out chan error) {
err := v.verifier.UniversalVerifySubBatch(params, subBatch.Samples, subBatch.NumBlobs)
if err != nil {
out <- err
return
}
out <- nil
}
func (v *shardValidator) VerifyBlobLengthWorker(blobCommitments encoding.BlobCommitments, out chan error) {
err := v.verifier.VerifyBlobLength(blobCommitments)
if err != nil {
out <- err
return
}
out <- nil
}
func ValidateBatchHeaderRoot(batchHeader *BatchHeader, blobHeaders []*BlobHeader) error {
// Check the batch header root
derivedHeader := &BatchHeader{}
_, err := derivedHeader.SetBatchRoot(blobHeaders)
if err != nil {
return fmt.Errorf("failed to compute batch header root: %w", err)
}
if batchHeader.BatchRoot != derivedHeader.BatchRoot {
return fmt.Errorf("batch header root does not match computed root")
}
return nil
}