You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.9 KiB

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