service.go raw
1 package main
2
3 import (
4 "context"
5
6 orlynitsv1 "next.orly.dev/pkg/proto/orlynits/v1"
7 )
8
9 // NitsService implements the gRPC NitsService interface.
10 type NitsService struct {
11 orlynitsv1.UnimplementedNitsServiceServer
12 node *Bitcoind
13 }
14
15 func NewNitsService(node *Bitcoind) *NitsService {
16 return &NitsService{node: node}
17 }
18
19 func (s *NitsService) Ready(_ context.Context, _ *orlynitsv1.Empty) (*orlynitsv1.ReadyResponse, error) {
20 if !s.node.IsResponsive() {
21 return &orlynitsv1.ReadyResponse{
22 Ready: false,
23 Status: "starting",
24 }, nil
25 }
26
27 info := s.node.GetBlockchainInfo()
28 if info == nil {
29 return &orlynitsv1.ReadyResponse{
30 Ready: false,
31 Status: "starting",
32 }, nil
33 }
34
35 progress := int32(info.VerificationProgress * 100)
36
37 if info.InitialBlockDownload {
38 return &orlynitsv1.ReadyResponse{
39 Ready: false,
40 Status: "syncing",
41 SyncProgressPercent: progress,
42 }, nil
43 }
44
45 return &orlynitsv1.ReadyResponse{
46 Ready: true,
47 Status: "ready",
48 SyncProgressPercent: progress,
49 }, nil
50 }
51
52 func (s *NitsService) GetBlockchainInfo(_ context.Context, _ *orlynitsv1.Empty) (*orlynitsv1.BlockchainInfoResponse, error) {
53 info := s.node.GetBlockchainInfo()
54 if info == nil {
55 return &orlynitsv1.BlockchainInfoResponse{}, nil
56 }
57
58 return &orlynitsv1.BlockchainInfoResponse{
59 Chain: info.Chain,
60 Blocks: info.Blocks,
61 Headers: info.Headers,
62 VerificationProgress: info.VerificationProgress,
63 InitialBlockDownload: info.InitialBlockDownload,
64 Pruned: info.Pruned,
65 PruneHeight: info.PruneHeight,
66 SizeOnDisk: info.SizeOnDisk,
67 }, nil
68 }
69
70 func (s *NitsService) EstimateFee(_ context.Context, req *orlynitsv1.EstimateFeeRequest) (*orlynitsv1.EstimateFeeResponse, error) {
71 if !s.node.IsResponsive() {
72 return &orlynitsv1.EstimateFeeResponse{
73 Errors: []string{"bitcoind is not responsive"},
74 }, nil
75 }
76
77 confTarget := int(req.ConfTarget)
78 if confTarget < 1 {
79 confTarget = 6
80 }
81
82 result, err := s.node.EstimateFee(confTarget)
83 if err != nil {
84 return &orlynitsv1.EstimateFeeResponse{
85 Errors: []string{err.Error()},
86 }, nil
87 }
88
89 return &orlynitsv1.EstimateFeeResponse{
90 FeeRate: result.FeeRate,
91 Errors: result.Errors,
92 }, nil
93 }
94