-
Notifications
You must be signed in to change notification settings - Fork 206
/
chain_tracker_service.go
34 lines (29 loc) · 1.12 KB
/
chain_tracker_service.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
package chaintracker
import (
"context"
empty "github.com/golang/protobuf/ptypes/empty"
)
type ChainTrackerService struct {
UnimplementedChainTrackerServiceServer
ChainTracker *ChainTracker
}
func (cts *ChainTrackerService) GetLatestBlockNum(context.Context, *empty.Empty) (*GetLatestBlockNumResponse, error) {
latestBlockNum, changeTime := cts.ChainTracker.GetLatestBlockNum()
if latestBlockNum <= 0 {
return nil, InvalidLatestBlockNumValue
}
return &GetLatestBlockNumResponse{Block: uint64(latestBlockNum), Timestamp: changeTime.Unix()}, nil
}
func (cts *ChainTrackerService) GetLatestBlockData(ctx context.Context, latestBlockData *LatestBlockData) (*LatestBlockDataResponse, error) {
latestBlockNum, requestedHashes, _, err := cts.ChainTracker.GetLatestBlockData(latestBlockData.FromBlock, latestBlockData.ToBlock, latestBlockData.SpecificBlock)
if err != nil {
return nil, err
}
if latestBlockNum <= 0 {
return nil, InvalidLatestBlockNumValue
}
if len(requestedHashes) == 0 {
return nil, InvalidReturnedHashes
}
return &LatestBlockDataResponse{LatestBlock: latestBlockNum, RequestedHashes: requestedHashes}, nil
}