github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/test/e2e/tests/block_test.go (about)

     1  package e2e_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	e2e "github.com/badrootd/celestia-core/test/e2e/pkg"
    12  )
    13  
    14  // Tests that block headers are identical across nodes where present.
    15  func TestBlock_Header(t *testing.T) {
    16  	blocks := fetchBlockChain(t)
    17  	testNode(t, func(t *testing.T, node e2e.Node) {
    18  		if node.Mode == e2e.ModeSeed {
    19  			return
    20  		}
    21  
    22  		client, err := node.Client()
    23  		require.NoError(t, err)
    24  		status, err := client.Status(ctx)
    25  		require.NoError(t, err)
    26  
    27  		first := status.SyncInfo.EarliestBlockHeight
    28  		last := status.SyncInfo.LatestBlockHeight
    29  		if node.RetainBlocks > 0 {
    30  			first++ // avoid race conditions with block pruning
    31  		}
    32  
    33  		for _, block := range blocks {
    34  			if block.Header.Height < first {
    35  				continue
    36  			}
    37  			if block.Header.Height > last {
    38  				break
    39  			}
    40  			resp, err := client.Block(ctx, &block.Header.Height)
    41  			require.NoError(t, err)
    42  
    43  			require.Equal(t, block, resp.Block,
    44  				"block mismatch for height %d", block.Header.Height)
    45  
    46  			require.NoError(t, resp.Block.ValidateBasic(),
    47  				"block at height %d is invalid", block.Header.Height)
    48  		}
    49  	})
    50  }
    51  
    52  // Tests that the node contains the expected block range.
    53  func TestBlock_Range(t *testing.T) {
    54  	testNode(t, func(t *testing.T, node e2e.Node) {
    55  		if node.Mode == e2e.ModeSeed {
    56  			return
    57  		}
    58  
    59  		client, err := node.Client()
    60  		require.NoError(t, err)
    61  		status, err := client.Status(ctx)
    62  		require.NoError(t, err)
    63  
    64  		first := status.SyncInfo.EarliestBlockHeight
    65  		last := status.SyncInfo.LatestBlockHeight
    66  
    67  		switch {
    68  		case node.StateSync:
    69  			assert.Greater(t, first, node.Testnet.InitialHeight,
    70  				"state synced nodes should not contain network's initial height")
    71  
    72  		case node.RetainBlocks > 0 && int64(node.RetainBlocks) < (last-node.Testnet.InitialHeight+1):
    73  			// Delta handles race conditions in reading first/last heights.
    74  			assert.InDelta(t, node.RetainBlocks, last-first+1, 1,
    75  				"node not pruning expected blocks")
    76  
    77  		default:
    78  			assert.Equal(t, node.Testnet.InitialHeight, first,
    79  				"node's first block should be network's initial height")
    80  		}
    81  
    82  		for h := first; h <= last; h++ {
    83  			resp, err := client.Block(ctx, &(h))
    84  			if err != nil && node.RetainBlocks > 0 && h == first {
    85  				// Ignore errors in first block if node is pruning blocks due to race conditions.
    86  				continue
    87  			}
    88  			require.NoError(t, err)
    89  			assert.Equal(t, h, resp.Block.Height)
    90  		}
    91  
    92  		for h := node.Testnet.InitialHeight; h < first; h++ {
    93  			_, err := client.Block(ctx, &(h))
    94  			require.Error(t, err)
    95  		}
    96  	})
    97  }
    98  
    99  func TestBlock_SignedData(t *testing.T) {
   100  	testNode(t, func(t *testing.T, node e2e.Node) {
   101  		client, err := node.Client()
   102  		require.NoError(t, err)
   103  
   104  		resp, err := client.SignedBlock(context.Background(), nil)
   105  		require.NoError(t, err)
   106  		require.Equal(t, resp.Header.Height, resp.Commit.Height)
   107  
   108  		err = resp.ValidatorSet.VerifyCommit(resp.Header.ChainID, resp.Commit.BlockID, resp.Header.Height, &resp.Commit)
   109  		require.NoError(t, err)
   110  
   111  		if !bytes.Equal(resp.Commit.BlockID.Hash, resp.Header.Hash()) {
   112  			t.Fatal("commit is for a different block")
   113  		}
   114  
   115  		if !bytes.Equal(resp.Header.DataHash, resp.Data.Hash()) {
   116  			t.Fatal("data does not match header	data hash")
   117  		}
   118  	})
   119  }