-
Notifications
You must be signed in to change notification settings - Fork 211
/
activation_service.go
101 lines (90 loc) · 2.91 KB
/
activation_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
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
91
92
93
94
95
96
97
98
99
100
101
package grpcserver
import (
"context"
"errors"
"fmt"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/sql"
)
type activationService struct {
goldenAtx types.ATXID
atxProvider atxProvider
}
func NewActivationService(atxProvider atxProvider, goldenAtx types.ATXID) *activationService {
return &activationService{
goldenAtx: goldenAtx,
atxProvider: atxProvider,
}
}
// RegisterService implements ServiceAPI.
func (s *activationService) RegisterService(server *grpc.Server) {
pb.RegisterActivationServiceServer(server, s)
}
func (s *activationService) RegisterHandlerService(mux *runtime.ServeMux) error {
return pb.RegisterActivationServiceHandlerServer(context.Background(), mux, s)
}
// String returns the service name.
func (s *activationService) String() string {
return "ActivationService"
}
// Get implements v1.ActivationServiceServer.
func (s *activationService) Get(ctx context.Context, request *pb.GetRequest) (*pb.GetResponse, error) {
if l := len(request.Id); l != types.ATXIDSize {
return nil, status.Error(
codes.InvalidArgument,
fmt.Sprintf("invalid ATX ID length (%d), expected (%d)", l, types.ATXIDSize),
)
}
atxId := types.ATXID(types.BytesToHash(request.Id))
atx, err := s.atxProvider.GetFullAtx(atxId)
if err != nil || atx == nil {
ctxzap.Debug(ctx, "failed to get ATX",
zap.Stringer("id", atxId),
zap.Error(err),
)
return nil, status.Error(codes.NotFound, "id was not found")
}
proof, err := s.atxProvider.GetMalfeasanceProof(atx.SmesherID)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
ctxzap.Error(ctx, "failed to get malfeasance proof",
zap.Stringer("smesher", atx.SmesherID),
zap.Stringer("smesher", atx.SmesherID),
zap.Stringer("id", atxId),
zap.Error(err),
)
return nil, status.Error(codes.NotFound, "id was not found")
}
resp := &pb.GetResponse{
Atx: convertActivation(atx),
}
if proof != nil {
resp.MalfeasanceProof = events.ToMalfeasancePB(atx.SmesherID, proof, false)
}
return resp, nil
}
func (s *activationService) Highest(ctx context.Context, req *emptypb.Empty) (*pb.HighestResponse, error) {
highest, err := s.atxProvider.MaxHeightAtx()
if err != nil {
return &pb.HighestResponse{
Atx: &pb.Activation{
Id: &pb.ActivationId{Id: s.goldenAtx.Bytes()},
},
}, nil
}
atx, err := s.atxProvider.GetFullAtx(highest)
if err != nil || atx == nil {
return nil, status.Error(codes.NotFound, fmt.Sprintf("atx id %v not found: %v", highest, err.Error()))
}
return &pb.HighestResponse{
Atx: convertActivation(atx),
}, nil
}