-
Notifications
You must be signed in to change notification settings - Fork 962
/
Copy pathcore_exchange.go
90 lines (71 loc) · 2.22 KB
/
core_exchange.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
package header
import (
"bytes"
"context"
"fmt"
format "github.com/ipfs/go-ipld-format"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
"github.com/celestiaorg/celestia-node/core"
)
type CoreExchange struct {
fetcher *core.BlockFetcher
shareStore format.DAGService
}
func NewCoreExchange(fetcher *core.BlockFetcher, dag format.DAGService) *CoreExchange {
return &CoreExchange{
fetcher: fetcher,
shareStore: dag,
}
}
func (ce *CoreExchange) RequestHeader(ctx context.Context, height uint64) (*ExtendedHeader, error) {
log.Debugw("core: requesting header", "height", height)
intHeight := int64(height)
return ce.getExtendedHeaderByHeight(ctx, &intHeight)
}
func (ce *CoreExchange) RequestHeaders(ctx context.Context, from, amount uint64) ([]*ExtendedHeader, error) {
log.Debugw("core: requesting headers", "from", from, "to", from+amount)
headers := make([]*ExtendedHeader, amount)
for i := range headers {
extHeader, err := ce.RequestHeader(ctx, from+uint64(i))
if err != nil {
return nil, err
}
headers[i] = extHeader
}
return headers, nil
}
func (ce *CoreExchange) RequestByHash(ctx context.Context, hash tmbytes.HexBytes) (*ExtendedHeader, error) {
log.Debugw("core: requesting header", "hash", hash.String())
block, err := ce.fetcher.GetBlockByHash(ctx, hash)
if err != nil {
return nil, err
}
comm, vals, err := ce.fetcher.GetBlockInfo(ctx, &block.Height)
if err != nil {
return nil, err
}
eh, err := MakeExtendedHeader(ctx, block, comm, vals, ce.shareStore)
if err != nil {
return nil, err
}
// verify hashes match
if !bytes.Equal(hash, eh.Hash()) {
return nil, fmt.Errorf("incorrect hash in header: expected %x, got %x", hash, eh.Hash())
}
return eh, nil
}
func (ce *CoreExchange) RequestHead(ctx context.Context) (*ExtendedHeader, error) {
log.Debug("core: requesting head")
return ce.getExtendedHeaderByHeight(ctx, nil)
}
func (ce *CoreExchange) getExtendedHeaderByHeight(ctx context.Context, height *int64) (*ExtendedHeader, error) {
b, err := ce.fetcher.GetBlock(ctx, height)
if err != nil {
return nil, err
}
comm, vals, err := ce.fetcher.GetBlockInfo(ctx, &b.Height)
if err != nil {
return nil, err
}
return MakeExtendedHeader(ctx, b, comm, vals, ce.shareStore)
}