bitbucket.org/number571/tendermint@v0.8.14/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 "bitbucket.org/number571/tendermint/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  			// the first blocks after state sync come from the backfill process
    36  			// and are therefore not complete
    37  			if node.StateSync && block.Header.Height <= first+e2e.EvidenceAgeHeight+1 {
    38  				continue
    39  			}
    40  			if block.Header.Height > last {
    41  				break
    42  			}
    43  			resp, err := client.Block(ctx, &block.Header.Height)
    44  			require.NoError(t, err)
    45  
    46  			require.Equal(t, block, resp.Block,
    47  				"block mismatch for height %d", block.Header.Height)
    48  
    49  			require.NoError(t, resp.Block.ValidateBasic(),
    50  				"block at height %d is invalid", block.Header.Height)
    51  		}
    52  	})
    53  }
    54  
    55  // Tests that the node contains the expected block range.
    56  func TestBlock_Range(t *testing.T) {
    57  	testNode(t, func(t *testing.T, node e2e.Node) {
    58  		if node.Mode == e2e.ModeSeed {
    59  			return
    60  		}
    61  
    62  		client, err := node.Client()
    63  		require.NoError(t, err)
    64  		status, err := client.Status(ctx)
    65  		require.NoError(t, err)
    66  
    67  		first := status.SyncInfo.EarliestBlockHeight
    68  		last := status.SyncInfo.LatestBlockHeight
    69  
    70  		switch {
    71  		// if the node state synced we ignore any assertions because it's hard to know how far back
    72  		// the node ran reverse sync for
    73  		case node.StateSync:
    74  			break
    75  		case node.RetainBlocks > 0 && int64(node.RetainBlocks) < (last-node.Testnet.InitialHeight+1):
    76  			// Delta handles race conditions in reading first/last heights.
    77  			assert.InDelta(t, node.RetainBlocks, last-first+1, 1,
    78  				"node not pruning expected blocks")
    79  
    80  		default:
    81  			assert.Equal(t, node.Testnet.InitialHeight, first,
    82  				"node's first block should be network's initial height")
    83  		}
    84  
    85  		for h := first; h <= last; h++ {
    86  			if node.StateSync && h <= first+e2e.EvidenceAgeHeight+1 {
    87  				continue
    88  			}
    89  			resp, err := client.Block(ctx, &(h))
    90  			if err != nil && node.RetainBlocks > 0 && h == first {
    91  				// Ignore errors in first block if node is pruning blocks due to race conditions.
    92  				continue
    93  			}
    94  			require.NoError(t, err)
    95  			require.NotNil(t, resp.Block)
    96  			assert.Equal(t, h, resp.Block.Height)
    97  		}
    98  
    99  		for h := node.Testnet.InitialHeight; h < first; h++ {
   100  			_, err := client.Block(ctx, &(h))
   101  			require.Error(t, err)
   102  		}
   103  	})
   104  }