-
Notifications
You must be signed in to change notification settings - Fork 57
/
block_test.go
108 lines (88 loc) · 2.85 KB
/
block_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package e2e_test
import (
"context"
"testing"
"github.com/omni-network/omni/lib/netconf"
e2e "github.com/cometbft/cometbft/test/e2e/pkg"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Tests that block headers are identical across nodes where present.
func TestBlock_Header(t *testing.T) {
t.Parallel()
ctx := context.Background()
blocks := fetchBlockChain(ctx, t)
testNode(t, func(t *testing.T, _ netconf.Network, node *e2e.Node, _ []Portal) {
t.Helper()
if node.Mode == e2e.ModeSeed {
return
}
client, err := node.Client()
require.NoError(t, err)
status, err := client.Status(ctx)
require.NoError(t, err)
first := status.SyncInfo.EarliestBlockHeight
last := status.SyncInfo.LatestBlockHeight
if node.RetainBlocks > 0 {
first++ // avoid race conditions with block pruning
}
for _, block := range blocks {
if block.Header.Height < first {
continue
}
if block.Header.Height > last {
break
}
resp, err := client.Block(ctx, &block.Header.Height)
require.NoError(t, err)
require.Equal(t, block, resp.Block,
"block mismatch for height %d", block.Header.Height)
require.NoError(t, resp.Block.ValidateBasic(),
"block at height %d is invalid", block.Header.Height)
}
})
}
// Tests that the node contains the expected block range.
func TestBlock_Range(t *testing.T) {
t.Parallel()
ctx := context.Background()
testNode(t, func(t *testing.T, _ netconf.Network, node *e2e.Node, _ []Portal) {
t.Helper()
if node.Mode == e2e.ModeSeed {
return
}
client, err := node.Client()
require.NoError(t, err)
status, err := client.Status(ctx)
require.NoError(t, err)
first := status.SyncInfo.EarliestBlockHeight
last := status.SyncInfo.LatestBlockHeight
switch {
case node.StateSync:
assert.Greater(t, first, node.Testnet.InitialHeight,
"state synced nodes should not contain network's initial height")
case node.RetainBlocks > 0 && int64(node.RetainBlocks) < (last-node.Testnet.InitialHeight+1):
// Delta handles race conditions in reading first/last heights.
assert.InDelta(t, node.RetainBlocks, last-first+1, 1,
"node not pruning expected blocks")
default:
assert.Equal(t, node.Testnet.InitialHeight, first,
"node's first block should be network's initial height")
}
for h := first; h <= last; h++ {
resp, err := client.Block(ctx, &(h))
if err != nil && node.RetainBlocks > 0 && h == first {
// Ignore errors in first block if node is pruning blocks due to race conditions.
continue
}
require.NoError(t, ctx.Err(), "Timeout fetching block range: %d", h)
require.NoError(t, err)
assert.Equal(t, h, resp.Block.Height)
}
for h := node.Testnet.InitialHeight; h < first; h++ {
_, err := client.Block(ctx, &(h))
require.NoError(t, ctx.Err(), "Timeout fetching initial range: %d", h)
require.Error(t, err)
}
})
}