github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/test/e2e/tests/block_test.go (about)

     1  package e2e_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	e2e "github.com/lazyledger/lazyledger-core/test/e2e/pkg"
    10  )
    11  
    12  // Tests that block headers are identical across nodes where present.
    13  func TestBlock_Header(t *testing.T) {
    14  	blocks := fetchBlockChain(t)
    15  	testNode(t, func(t *testing.T, node e2e.Node) {
    16  		if node.Mode == e2e.ModeSeed {
    17  			return
    18  		}
    19  
    20  		client, err := node.Client()
    21  		require.NoError(t, err)
    22  		status, err := client.Status(ctx)
    23  		require.NoError(t, err)
    24  
    25  		first := status.SyncInfo.EarliestBlockHeight
    26  		last := status.SyncInfo.LatestBlockHeight
    27  		if node.RetainBlocks > 0 {
    28  			first++ // avoid race conditions with block pruning
    29  		}
    30  
    31  		for _, block := range blocks {
    32  			if block.Header.Height < first {
    33  				continue
    34  			}
    35  			if block.Header.Height > last {
    36  				break
    37  			}
    38  			resp, err := client.Block(ctx, &block.Header.Height)
    39  			require.NoError(t, err)
    40  			require.Equal(t, block, resp.Block,
    41  				"block mismatch for height %v", block.Header.Height)
    42  		}
    43  	})
    44  }
    45  
    46  // Tests that the node contains the expected block range.
    47  func TestBlock_Range(t *testing.T) {
    48  	testNode(t, func(t *testing.T, node e2e.Node) {
    49  		if node.Mode == e2e.ModeSeed {
    50  			return
    51  		}
    52  
    53  		client, err := node.Client()
    54  		require.NoError(t, err)
    55  		status, err := client.Status(ctx)
    56  		require.NoError(t, err)
    57  
    58  		first := status.SyncInfo.EarliestBlockHeight
    59  		last := status.SyncInfo.LatestBlockHeight
    60  
    61  		switch {
    62  		case node.StateSync:
    63  			assert.Greater(t, first, node.Testnet.InitialHeight,
    64  				"state synced nodes should not contain network's initial height")
    65  
    66  		case node.RetainBlocks > 0 && int64(node.RetainBlocks) < (last-node.Testnet.InitialHeight+1):
    67  			// Delta handles race conditions in reading first/last heights.
    68  			assert.InDelta(t, node.RetainBlocks, last-first+1, 1,
    69  				"node not pruning expected blocks")
    70  
    71  		default:
    72  			assert.Equal(t, node.Testnet.InitialHeight, first,
    73  				"node's first block should be network's initial height")
    74  		}
    75  
    76  		for h := first; h <= last; h++ {
    77  			resp, err := client.Block(ctx, &(h))
    78  			if err != nil && node.RetainBlocks > 0 && h == first {
    79  				// Ignore errors in first block if node is pruning blocks due to race conditions.
    80  				continue
    81  			}
    82  			require.NoError(t, err)
    83  			assert.Equal(t, h, resp.Block.Height)
    84  		}
    85  
    86  		for h := node.Testnet.InitialHeight; h < first; h++ {
    87  			_, err := client.Block(ctx, &(h))
    88  			require.Error(t, err)
    89  		}
    90  	})
    91  }