-
Notifications
You must be signed in to change notification settings - Fork 892
/
testing.go
76 lines (66 loc) · 2.09 KB
/
testing.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
package getters
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
"github.com/celestiaorg/celestia-app/pkg/da"
"github.com/celestiaorg/rsmt2d"
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/header/headertest"
"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/eds/edstest"
)
// TestGetter provides a testing SingleEDSGetter and the root of the EDS it holds.
func TestGetter(t *testing.T) (share.Getter, *header.ExtendedHeader) {
eds := edstest.RandEDS(t, 8)
dah, err := share.NewRoot(eds)
eh := headertest.RandExtendedHeaderWithRoot(t, dah)
require.NoError(t, err)
return &SingleEDSGetter{
EDS: eds,
}, eh
}
// SingleEDSGetter contains a single EDS where data is retrieved from.
// Its primary use is testing, and GetSharesByNamespace is not supported.
type SingleEDSGetter struct {
EDS *rsmt2d.ExtendedDataSquare
}
// GetShare gets a share from a kept EDS if exist and if the correct root is given.
func (seg *SingleEDSGetter) GetShare(
_ context.Context,
header *header.ExtendedHeader,
row, col int,
) (share.Share, error) {
err := seg.checkRoot(header.DAH)
if err != nil {
return nil, err
}
return seg.EDS.GetCell(uint(row), uint(col)), nil
}
// GetEDS returns a kept EDS if the correct root is given.
func (seg *SingleEDSGetter) GetEDS(
_ context.Context,
header *header.ExtendedHeader,
) (*rsmt2d.ExtendedDataSquare, error) {
err := seg.checkRoot(header.DAH)
if err != nil {
return nil, err
}
return seg.EDS, nil
}
// GetSharesByNamespace returns NamespacedShares from a kept EDS if the correct root is given.
func (seg *SingleEDSGetter) GetSharesByNamespace(context.Context, *header.ExtendedHeader, share.Namespace,
) (share.NamespacedShares, error) {
panic("SingleEDSGetter: GetSharesByNamespace is not implemented")
}
func (seg *SingleEDSGetter) checkRoot(root *share.Root) error {
dah, err := da.NewDataAvailabilityHeader(seg.EDS)
if err != nil {
return err
}
if !root.Equals(&dah) {
return fmt.Errorf("unknown EDS: have %s, asked %s", dah.String(), root.String())
}
return nil
}