github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/chunks/chunkFaults_test.go (about)

     1  package chunks_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/ipfs/go-cid"
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/onflow/flow-go/model/chunks"
    11  	"github.com/onflow/flow-go/utils/unittest"
    12  )
    13  
    14  // TestIsChunkFaultError tests the IsChunkFaultError function returns true for known chunk fault errors
    15  // and false for any other error.
    16  func TestIsChunkFaultError(t *testing.T) {
    17  	t.Run("CFMissingRegisterTouch", func(t *testing.T) {
    18  		cf := chunks.NewCFMissingRegisterTouch(nil, 0, unittest.IdentifierFixture(), unittest.IdentifierFixture())
    19  		assert.Error(t, cf)
    20  		assert.True(t, chunks.IsChunkFaultError(cf))
    21  
    22  		var errType *chunks.CFMissingRegisterTouch
    23  		assert.ErrorAs(t, cf, &errType)
    24  	})
    25  
    26  	t.Run("CFNonMatchingFinalState", func(t *testing.T) {
    27  		cf := chunks.NewCFNonMatchingFinalState(unittest.StateCommitmentFixture(), unittest.StateCommitmentFixture(), 0, unittest.IdentifierFixture())
    28  		assert.Error(t, cf)
    29  		assert.True(t, chunks.IsChunkFaultError(cf))
    30  
    31  		var errType *chunks.CFNonMatchingFinalState
    32  		assert.ErrorAs(t, cf, &errType)
    33  	})
    34  
    35  	t.Run("CFInvalidEventsCollection", func(t *testing.T) {
    36  		cf := chunks.NewCFInvalidEventsCollection(unittest.IdentifierFixture(), unittest.IdentifierFixture(), 0, unittest.IdentifierFixture(), nil)
    37  		assert.Error(t, cf)
    38  		assert.True(t, chunks.IsChunkFaultError(cf))
    39  
    40  		var errType *chunks.CFInvalidEventsCollection
    41  		assert.ErrorAs(t, cf, &errType)
    42  	})
    43  
    44  	t.Run("CFInvalidVerifiableChunk", func(t *testing.T) {
    45  		cf := chunks.NewCFInvalidVerifiableChunk("", nil, 0, unittest.IdentifierFixture())
    46  		assert.Error(t, cf)
    47  		assert.True(t, chunks.IsChunkFaultError(cf))
    48  
    49  		var errType *chunks.CFInvalidVerifiableChunk
    50  		assert.ErrorAs(t, cf, &errType)
    51  	})
    52  
    53  	t.Run("CFSystemChunkIncludedCollection", func(t *testing.T) {
    54  		cf := chunks.NewCFSystemChunkIncludedCollection(0, unittest.IdentifierFixture())
    55  		assert.Error(t, cf)
    56  		assert.True(t, chunks.IsChunkFaultError(cf))
    57  
    58  		var errType *chunks.CFSystemChunkIncludedCollection
    59  		assert.ErrorAs(t, cf, &errType)
    60  	})
    61  
    62  	t.Run("CFExecutionDataBlockIDMismatch", func(t *testing.T) {
    63  		cf := chunks.NewCFExecutionDataBlockIDMismatch(unittest.IdentifierFixture(), unittest.IdentifierFixture(), 0, unittest.IdentifierFixture())
    64  		assert.Error(t, cf)
    65  		assert.True(t, chunks.IsChunkFaultError(cf))
    66  
    67  		var errType *chunks.CFExecutionDataBlockIDMismatch
    68  		assert.ErrorAs(t, cf, &errType)
    69  	})
    70  
    71  	t.Run("CFExecutionDataChunksLengthMismatch", func(t *testing.T) {
    72  		cf := chunks.NewCFExecutionDataChunksLengthMismatch(0, 0, 0, unittest.IdentifierFixture())
    73  		assert.Error(t, cf)
    74  		assert.True(t, chunks.IsChunkFaultError(cf))
    75  
    76  		var errType *chunks.CFExecutionDataChunksLengthMismatch
    77  		assert.ErrorAs(t, cf, &errType)
    78  	})
    79  
    80  	t.Run("CFExecutionDataInvalidChunkCID", func(t *testing.T) {
    81  		cf := chunks.NewCFExecutionDataInvalidChunkCID(cid.Cid{}, cid.Cid{}, 0, unittest.IdentifierFixture())
    82  		assert.Error(t, cf)
    83  		assert.True(t, chunks.IsChunkFaultError(cf))
    84  
    85  		var errType *chunks.CFExecutionDataInvalidChunkCID
    86  		assert.ErrorAs(t, cf, &errType)
    87  	})
    88  
    89  	t.Run("CFInvalidExecutionDataID", func(t *testing.T) {
    90  		cf := chunks.NewCFInvalidExecutionDataID(unittest.IdentifierFixture(), unittest.IdentifierFixture(), 0, unittest.IdentifierFixture())
    91  		assert.Error(t, cf)
    92  		assert.True(t, chunks.IsChunkFaultError(cf))
    93  
    94  		var errType *chunks.CFInvalidExecutionDataID
    95  		assert.ErrorAs(t, cf, &errType)
    96  	})
    97  
    98  	t.Run("Non ChunkFaultError", func(t *testing.T) {
    99  		err := fmt.Errorf("some error")
   100  		assert.False(t, chunks.IsChunkFaultError(err))
   101  	})
   102  }