github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/p2p/ipld/validate_test.go (about)

     1  package ipld
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	mdutils "github.com/ipfs/go-merkledag/test"
     9  	"github.com/lazyledger/nmt/namespace"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/lazyledger/lazyledger-core/ipfs"
    14  	"github.com/lazyledger/lazyledger-core/libs/log"
    15  	"github.com/lazyledger/lazyledger-core/types"
    16  	"github.com/lazyledger/lazyledger-core/types/consts"
    17  )
    18  
    19  // TODO(@Wondertan): Add test to simulate ErrValidationFailed
    20  
    21  func TestValidateAvailability(t *testing.T) {
    22  	const (
    23  		shares          = 15
    24  		squareSize      = 8
    25  		adjustedMsgSize = consts.MsgShareSize - 2
    26  	)
    27  
    28  	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    29  	defer cancel()
    30  
    31  	blockData := generateRandomBlockData(squareSize*squareSize, adjustedMsgSize)
    32  	block := &types.Block{
    33  		Data:       blockData,
    34  		LastCommit: &types.Commit{},
    35  	}
    36  	block.Hash()
    37  
    38  	dag := mdutils.Mock()
    39  	err := PutBlock(ctx, dag, block, ipfs.MockRouting(), log.TestingLogger())
    40  	require.NoError(t, err)
    41  
    42  	calls := 0
    43  	err = ValidateAvailability(ctx, dag, &block.DataAvailabilityHeader, shares, func(data namespace.PrefixedData8) {
    44  		calls++
    45  	})
    46  	assert.NoError(t, err)
    47  	assert.Equal(t, shares, calls)
    48  }