forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher_test.go
84 lines (74 loc) · 2.45 KB
/
fetcher_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
package core
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/types"
"github.com/tendermint/tendermint/libs/bytes"
)
func TestBlockFetcher_GetBlock_and_SubscribeNewBlockEvent(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
t.Cleanup(cancel)
_, client := StartTestCoreWithApp(t)
fetcher := NewBlockFetcher(client)
// generate some blocks
newBlockChan, err := fetcher.SubscribeNewBlockEvent(ctx)
require.NoError(t, err)
for i := 1; i < 3; i++ {
select {
case newBlockFromChan := <-newBlockChan:
h := newBlockFromChan.Height
block, err := fetcher.GetBlock(ctx, &h)
require.NoError(t, err)
assert.Equal(t, newBlockFromChan, block)
require.GreaterOrEqual(t, block.Height, int64(i))
case <-ctx.Done():
require.NoError(t, ctx.Err())
}
}
require.NoError(t, fetcher.UnsubscribeNewBlockEvent(ctx))
}
// TestBlockFetcherHeaderValues tests that both the Commit and ValidatorSet
// endpoints are working as intended.
func TestBlockFetcherHeaderValues(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
t.Cleanup(cancel)
_, client := StartTestCoreWithApp(t)
fetcher := NewBlockFetcher(client)
// generate some blocks
newBlockChan, err := fetcher.SubscribeNewBlockEvent(ctx)
require.NoError(t, err)
// read once from channel to generate next block
var h int64
select {
case evt := <-newBlockChan:
h = evt.Header.Height
case <-ctx.Done():
require.NoError(t, ctx.Err())
}
// get Commit from current height
commit, err := fetcher.Commit(ctx, &h)
require.NoError(t, err)
// get ValidatorSet from current height
valSet, err := fetcher.ValidatorSet(ctx, &h)
require.NoError(t, err)
// get next block
var nextBlock *types.Block
select {
case nextBlock = <-newBlockChan:
case <-ctx.Done():
require.NoError(t, ctx.Err())
}
// compare LastCommit from next block to Commit from first block height
assert.Equal(t, nextBlock.LastCommit.Hash(), commit.Hash())
assert.Equal(t, nextBlock.LastCommit.Height, commit.Height)
assert.Equal(t, nextBlock.LastCommit.Signatures, commit.Signatures)
// compare ValidatorSet hash to the ValidatorsHash from first block height
hexBytes := bytes.HexBytes{}
err = hexBytes.Unmarshal(valSet.Hash())
require.NoError(t, err)
assert.Equal(t, nextBlock.ValidatorsHash, hexBytes)
require.NoError(t, fetcher.UnsubscribeNewBlockEvent(ctx))
}