forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchange_test.go
58 lines (44 loc) · 1.5 KB
/
exchange_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
package core
import (
"context"
"testing"
"time"
ds "github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/celestiaorg/celestia-app/testutil/testnode"
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/share/eds"
)
func TestCoreExchange_RequestHeaders(t *testing.T) {
fetcher, _ := createCoreFetcher(t, DefaultTestConfig())
// generate 10 blocks
generateBlocks(t, fetcher)
store := createStore(t)
ce := NewExchange(fetcher, store, header.MakeExtendedHeader)
headers, err := ce.GetRangeByHeight(context.Background(), 1, 10)
require.NoError(t, err)
assert.Equal(t, 10, len(headers))
}
func createCoreFetcher(t *testing.T, cfg *TestConfig) (*BlockFetcher, testnode.Context) {
cctx := StartTestNodeWithConfig(t, cfg)
// wait for height 2 in order to be able to start submitting txs (this prevents
// flakiness with accessing account state)
_, err := cctx.WaitForHeightWithTimeout(2, time.Second*2) // TODO @renaynay: configure?
require.NoError(t, err)
return NewBlockFetcher(cctx.Client), cctx
}
func createStore(t *testing.T) *eds.Store {
t.Helper()
store, err := eds.NewStore(t.TempDir(), ds_sync.MutexWrap(ds.NewMapDatastore()))
require.NoError(t, err)
return store
}
func generateBlocks(t *testing.T, fetcher *BlockFetcher) {
sub, err := fetcher.SubscribeNewBlockEvent(context.Background())
require.NoError(t, err)
for i := 0; i < 10; i++ {
<-sub
}
}