Skip to content

Commit 56774a1

Browse files
committed
convert ChunkFault to an error type
1 parent efc5407 commit 56774a1

File tree

9 files changed

+152
-125
lines changed

9 files changed

+152
-125
lines changed

engine/execution/computation/execution_verification_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,9 +851,8 @@ func executeBlockAndVerifyWithParameters(t *testing.T,
851851
require.Len(t, vcds, len(txs)+1) // +1 for system chunk
852852

853853
for _, vcd := range vcds {
854-
_, fault, err := verifier.Verify(vcd)
854+
_, err := verifier.Verify(vcd)
855855
assert.NoError(t, err)
856-
assert.Nil(t, fault)
857856
}
858857

859858
return computationResult

engine/verification/verifier/engine.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,47 +188,46 @@ func (e *Engine) verify(ctx context.Context, originID flow.Identifier,
188188
// execute the assigned chunk
189189
span, _ := e.tracer.StartSpanFromContext(ctx, trace.VERVerChunkVerify)
190190

191-
spockSecret, chFault, err := e.chVerif.Verify(vc)
191+
spockSecret, err := e.chVerif.Verify(vc)
192192
span.End()
193-
// Any err means that something went wrong when verify the chunk
194-
// the outcome of the verification is captured inside the chFault and not the err
193+
195194
if err != nil {
196-
return fmt.Errorf("cannot verify chunk: %w", err)
197-
}
195+
if !chmodels.IsChunkFaultError(err) {
196+
return fmt.Errorf("cannot verify chunk: %w", err)
197+
}
198198

199-
// if any fault found with the chunk
200-
if chFault != nil {
201-
switch chFault.(type) {
199+
// if any fault found with the chunk
200+
switch chFault := err.(type) {
202201
case *chmodels.CFMissingRegisterTouch:
203202
e.log.Warn().
204203
Str("chunk_fault_type", "missing_register_touch").
205-
Str("chunk_fault", chFault.String()).
204+
Str("chunk_fault", chFault.Error()).
206205
Msg("chunk fault found, could not verify chunk")
207206
// still create approvals for this case
208207
case *chmodels.CFNonMatchingFinalState:
209208
// TODO raise challenge
210209
e.log.Warn().
211210
Str("chunk_fault_type", "final_state_mismatch").
212-
Str("chunk_fault", chFault.String()).
211+
Str("chunk_fault", chFault.Error()).
213212
Msg("chunk fault found, could not verify chunk")
214213
return nil
215214
case *chmodels.CFInvalidVerifiableChunk:
216215
// TODO raise challenge
217216
e.log.Error().
218217
Str("chunk_fault_type", "invalid_verifiable_chunk").
219-
Str("chunk_fault", chFault.String()).
218+
Str("chunk_fault", chFault.Error()).
220219
Msg("chunk fault found, could not verify chunk")
221220
return nil
222221
case *chmodels.CFInvalidEventsCollection:
223222
// TODO raise challenge
224223
e.log.Error().
225224
Str("chunk_fault_type", "invalid_event_collection").
226-
Str("chunk_fault", chFault.String()).
225+
Str("chunk_fault", chFault.Error()).
227226
Msg("chunk fault found, could not verify chunk")
228227
return nil
229228
default:
230229
return engine.NewInvalidInputErrorf("unknown type of chunk fault is received (type: %T) : %v",
231-
chFault, chFault.String())
230+
chFault, chFault.Error())
232231
}
233232
}
234233

engine/verification/verifier/engine_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,40 +211,40 @@ func (suite *VerifierEngineTestSuite) TestVerifyUnhappyPaths() {
211211
type ChunkVerifierMock struct {
212212
}
213213

214-
func (v ChunkVerifierMock) Verify(vc *verification.VerifiableChunkData) ([]byte, chmodel.ChunkFault, error) {
214+
func (v ChunkVerifierMock) Verify(vc *verification.VerifiableChunkData) ([]byte, error) {
215215
if vc.IsSystemChunk {
216-
return nil, nil, nil
216+
return nil, nil
217217
}
218218

219219
switch vc.Chunk.Index {
220220
case 0:
221-
return []byte{}, nil, nil
221+
return []byte{}, nil
222222
// return error
223223
case 1:
224224
return nil, chmodel.NewCFMissingRegisterTouch(
225225
[]string{"test missing register touch"},
226226
vc.Chunk.Index,
227227
vc.Result.ID(),
228-
unittest.TransactionFixture().ID()), nil
228+
unittest.TransactionFixture().ID())
229229

230230
case 2:
231231
return nil, chmodel.NewCFInvalidVerifiableChunk(
232232
"test",
233233
errors.New("test invalid verifiable chunk"),
234234
vc.Chunk.Index,
235-
vc.Result.ID()), nil
235+
vc.Result.ID())
236236

237237
case 3:
238238
return nil, chmodel.NewCFNonMatchingFinalState(
239239
unittest.StateCommitmentFixture(),
240240
unittest.StateCommitmentFixture(),
241241
vc.Chunk.Index,
242-
vc.Result.ID()), nil
242+
vc.Result.ID())
243243

244244
// TODO add cases for challenges
245245
// return successful by default
246246
default:
247-
return nil, nil, nil
247+
return nil, nil
248248
}
249249

250250
}

model/chunks/chunkFaults.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@ package chunks
22

33
import (
44
"encoding/hex"
5+
"errors"
56
"fmt"
67

78
"github.com/onflow/flow-go/model/flow"
89
)
910

10-
// ChunkFault holds information about a fault that is found while
11+
// ChunkFaultError holds information about a fault that is found while
1112
// verifying a chunk
12-
type ChunkFault interface {
13+
type ChunkFaultError interface {
14+
error
1315
ChunkIndex() uint64
1416
ExecutionResultID() flow.Identifier
1517
String() string
1618
}
1719

20+
func IsChunkFaultError(err error) bool {
21+
var cfErr ChunkFaultError
22+
return errors.As(err, &cfErr)
23+
}
24+
1825
// CFMissingRegisterTouch is returned when a register touch is missing (read or update)
1926
type CFMissingRegisterTouch struct {
2027
regsterIDs []string
@@ -23,6 +30,8 @@ type CFMissingRegisterTouch struct {
2330
txID flow.Identifier // very first transaction inside the chunk that required this register
2431
}
2532

33+
var _ ChunkFaultError = (*CFMissingRegisterTouch)(nil)
34+
2635
func (cf CFMissingRegisterTouch) String() string {
2736
hexStrings := make([]string, len(cf.regsterIDs))
2837
for i, s := range cf.regsterIDs {
@@ -32,6 +41,10 @@ func (cf CFMissingRegisterTouch) String() string {
3241
return fmt.Sprintf("at least one register touch was missing inside the chunk data package that was needed while running transactions of chunk %d of result %s (tx hash of one of them: %s), hex-encoded register ids: %s", cf.chunkIndex, cf.execResID.String(), cf.txID.String(), hexStrings)
3342
}
3443

44+
func (cf CFMissingRegisterTouch) Error() string {
45+
return cf.String()
46+
}
47+
3548
// ChunkIndex returns chunk index of the faulty chunk
3649
func (cf CFMissingRegisterTouch) ChunkIndex() uint64 {
3750
return cf.chunkIndex
@@ -59,10 +72,16 @@ type CFNonMatchingFinalState struct {
5972
execResID flow.Identifier
6073
}
6174

75+
var _ ChunkFaultError = (*CFNonMatchingFinalState)(nil)
76+
6277
func (cf CFNonMatchingFinalState) String() string {
6378
return fmt.Sprintf("final state commitment doesn't match, expected [%x] but computed [%x]", cf.expected, cf.computed)
6479
}
6580

81+
func (cf CFNonMatchingFinalState) Error() string {
82+
return cf.String()
83+
}
84+
6685
// ChunkIndex returns chunk index of the faulty chunk
6786
func (cf CFNonMatchingFinalState) ChunkIndex() uint64 {
6887
return cf.chunkIndex
@@ -90,6 +109,8 @@ type CFInvalidEventsCollection struct {
90109
eventIDs flow.IdentifierList
91110
}
92111

112+
var _ ChunkFaultError = (*CFInvalidEventsCollection)(nil)
113+
93114
func NewCFInvalidEventsCollection(expected flow.Identifier, computed flow.Identifier, chInx uint64, execResID flow.Identifier, events flow.EventsList) *CFInvalidEventsCollection {
94115
return &CFInvalidEventsCollection{
95116
expected: expected,
@@ -113,6 +134,10 @@ func (c *CFInvalidEventsCollection) String() string {
113134
c.chunkIndex, c.resultID, c.eventIDs)
114135
}
115136

137+
func (cf CFInvalidEventsCollection) Error() string {
138+
return cf.String()
139+
}
140+
116141
// CFInvalidServiceEventsEmitted is returned when service events are different from the chunk's one
117142
type CFInvalidServiceEventsEmitted struct {
118143
expected flow.ServiceEventList
@@ -121,6 +146,8 @@ type CFInvalidServiceEventsEmitted struct {
121146
resultID flow.Identifier
122147
}
123148

149+
var _ ChunkFaultError = (*CFInvalidServiceEventsEmitted)(nil)
150+
124151
func CFInvalidServiceSystemEventsEmitted(expected flow.ServiceEventList, computed flow.ServiceEventList, chInx uint64, execResID flow.Identifier) *CFInvalidServiceEventsEmitted {
125152
return &CFInvalidServiceEventsEmitted{
126153
expected: expected,
@@ -142,6 +169,10 @@ func (c *CFInvalidServiceEventsEmitted) String() string {
142169
return fmt.Sprintf("service events differs, got [%s] expected [%s] for chunk %d with result ID %s", c.computed, c.expected, c.chunkIndex, c.resultID)
143170
}
144171

172+
func (cf CFInvalidServiceEventsEmitted) Error() string {
173+
return cf.String()
174+
}
175+
145176
// CFInvalidVerifiableChunk is returned when a verifiable chunk is invalid
146177
// this includes cases that code fails to construct a partial trie,
147178
// collection hashes doesn't match
@@ -153,10 +184,16 @@ type CFInvalidVerifiableChunk struct {
153184
execResID flow.Identifier
154185
}
155186

187+
var _ ChunkFaultError = (*CFInvalidVerifiableChunk)(nil)
188+
156189
func (cf CFInvalidVerifiableChunk) String() string {
157190
return fmt.Sprint("invalid verifiable chunk due to ", cf.reason, cf.details.Error())
158191
}
159192

193+
func (cf CFInvalidVerifiableChunk) Error() string {
194+
return cf.String()
195+
}
196+
160197
// ChunkIndex returns chunk index of the faulty chunk
161198
func (cf CFInvalidVerifiableChunk) ChunkIndex() uint64 {
162199
return cf.chunkIndex

model/chunks/executionDataFaults.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"github.com/onflow/flow-go/model/flow"
99
)
1010

11-
var _ ChunkFault = (*CFExecutionDataBlockIDMismatch)(nil)
12-
1311
// CFExecutionDataBlockIDMismatch is returned when the block ID contained in the execution data
1412
// root is different from chunk's block ID
1513
type CFExecutionDataBlockIDMismatch struct {
@@ -19,11 +17,17 @@ type CFExecutionDataBlockIDMismatch struct {
1917
chunkBlockID flow.Identifier // chunk's blockID
2018
}
2119

20+
var _ ChunkFaultError = (*CFExecutionDataBlockIDMismatch)(nil)
21+
2222
func (cf CFExecutionDataBlockIDMismatch) String() string {
2323
return fmt.Sprintf("execution data root's block ID (%s) is different than chunk's block ID (%s) for chunk %d with result ID %s",
2424
cf.executionDataRootBlockID, cf.chunkBlockID, cf.chunkIndex, cf.execResID.String())
2525
}
2626

27+
func (cf CFExecutionDataBlockIDMismatch) Error() string {
28+
return cf.String()
29+
}
30+
2731
// ChunkIndex returns chunk index of the faulty chunk
2832
func (cf CFExecutionDataBlockIDMismatch) ChunkIndex() uint64 {
2933
return cf.chunkIndex
@@ -49,8 +53,6 @@ func NewCFExecutionDataBlockIDMismatch(
4953
}
5054
}
5155

52-
var _ ChunkFault = (*CFExecutionDataChunksLengthMismatch)(nil)
53-
5456
// CFExecutionDataChunksLengthMismatch is returned when execution data chunks list has different length than number of chunks for a block
5557
type CFExecutionDataChunksLengthMismatch struct {
5658
chunkIndex uint64 // chunk's index
@@ -59,11 +61,17 @@ type CFExecutionDataChunksLengthMismatch struct {
5961
executionResultChunkListLength int // number of chunks in ExecutionResult
6062
}
6163

64+
var _ ChunkFaultError = (*CFExecutionDataChunksLengthMismatch)(nil)
65+
6266
func (cf CFExecutionDataChunksLengthMismatch) String() string {
6367
return fmt.Sprintf("execution data root chunk length (%d) is different then execution result chunk list length (%d) for chunk %d with result ID %s",
6468
cf.executionDataRootChunkLength, cf.executionResultChunkListLength, cf.chunkIndex, cf.execResID.String())
6569
}
6670

71+
func (cf CFExecutionDataChunksLengthMismatch) Error() string {
72+
return cf.String()
73+
}
74+
6775
// ChunkIndex returns chunk index of the faulty chunk
6876
func (cf CFExecutionDataChunksLengthMismatch) ChunkIndex() uint64 {
6977
return cf.chunkIndex
@@ -89,8 +97,6 @@ func NewCFExecutionDataChunksLengthMismatch(
8997
}
9098
}
9199

92-
var _ ChunkFault = (*CFExecutionDataInvalidChunkCID)(nil)
93-
94100
// CFExecutionDataInvalidChunkCID is returned when execution data chunk's CID is different from computed
95101
type CFExecutionDataInvalidChunkCID struct {
96102
chunkIndex uint64 // chunk's index
@@ -99,11 +105,17 @@ type CFExecutionDataInvalidChunkCID struct {
99105
computedChunkCID cid.Cid // computed CID for the chunk
100106
}
101107

108+
var _ ChunkFaultError = (*CFExecutionDataInvalidChunkCID)(nil)
109+
102110
func (cf CFExecutionDataInvalidChunkCID) String() string {
103111
return fmt.Sprintf("execution data chunk CID (%s) is different then computed (%s) for chunk %d with result ID %s",
104112
cf.executionDataRootChunkCID, cf.computedChunkCID, cf.chunkIndex, cf.execResID.String())
105113
}
106114

115+
func (cf CFExecutionDataInvalidChunkCID) Error() string {
116+
return cf.String()
117+
}
118+
107119
// ChunkIndex returns chunk index of the faulty chunk
108120
func (cf CFExecutionDataInvalidChunkCID) ChunkIndex() uint64 {
109121
return cf.chunkIndex
@@ -129,8 +141,6 @@ func NewCFExecutionDataInvalidChunkCID(
129141
}
130142
}
131143

132-
var _ ChunkFault = (*CFInvalidExecutionDataID)(nil)
133-
134144
// CFInvalidExecutionDataID is returned when ExecutionResult's ExecutionDataID is different from computed
135145
type CFInvalidExecutionDataID struct {
136146
chunkIndex uint64 // chunk's index
@@ -139,11 +149,17 @@ type CFInvalidExecutionDataID struct {
139149
computedExecutionDataID flow.Identifier // computed ExecutionDataID
140150
}
141151

152+
var _ ChunkFaultError = (*CFInvalidExecutionDataID)(nil)
153+
142154
func (cf CFInvalidExecutionDataID) String() string {
143155
return fmt.Sprintf("execution data ID (%s) is different then computed (%s) for chunk %d with result ID %s",
144156
cf.erExecutionDataID, cf.computedExecutionDataID, cf.chunkIndex, cf.execResID.String())
145157
}
146158

159+
func (cf CFInvalidExecutionDataID) Error() string {
160+
return cf.String()
161+
}
162+
147163
// ChunkIndex returns chunk index of the faulty chunk
148164
func (cf CFInvalidExecutionDataID) ChunkIndex() uint64 {
149165
return cf.chunkIndex

module/chunks.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ type ChunkVerifier interface {
2626
ch *verification.VerifiableChunkData,
2727
) (
2828
[]byte,
29-
chmodels.ChunkFault,
3029
error,
3130
)
3231
}

0 commit comments

Comments
 (0)