-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
36 lines (29 loc) · 985 Bytes
/
reader.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
// Package sentinenhead enables to read head value from sentinel.
package sentinelhead
import (
"context"
"fmt"
"github.com/ipfs/go-cid"
"github.com/areknoster/public-distributed-commit-log/pdcl"
"github.com/areknoster/public-distributed-commit-log/sentinel/sentinelpb"
)
// Reader implements head.Reader based on sentinel client. It should be initialized with New function
type Reader struct {
client sentinelpb.SentinelClient
}
// New initializes sentinel head.Reader
func New(client sentinelpb.SentinelClient) *Reader {
return &Reader{client: client}
}
// ReadHead fetches head cid from sentinel
func (s *Reader) ReadHead(ctx context.Context) (cid.Cid, error) {
resp, err := s.client.GetHeadCID(ctx, &sentinelpb.GetHeadCIDRequest{})
if err != nil {
return cid.Cid{}, fmt.Errorf("get head cid from sentinel: %w", err)
}
decodedCID, err := pdcl.ParseCID(resp.Cid)
if err != nil {
return cid.Cid{}, fmt.Errorf("decode CID: %w", err)
}
return decodedCID, nil
}