forked from strangelove-ventures/horcrux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cosigner_grpc_server.go
139 lines (127 loc) · 3.63 KB
/
cosigner_grpc_server.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package signer
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/hashicorp/raft"
"github.com/strangelove-ventures/horcrux/signer/proto"
)
var _ proto.CosignerServer = &CosignerGRPCServer{}
type CosignerGRPCServer struct {
cosigner *LocalCosigner
thresholdValidator *ThresholdValidator
raftStore *RaftStore
proto.UnimplementedCosignerServer
}
func NewCosignerGRPCServer(
cosigner *LocalCosigner,
thresholdValidator *ThresholdValidator,
raftStore *RaftStore,
) *CosignerGRPCServer {
return &CosignerGRPCServer{
cosigner: cosigner,
thresholdValidator: thresholdValidator,
raftStore: raftStore,
}
}
func (rpc *CosignerGRPCServer) SignBlock(
ctx context.Context,
req *proto.SignBlockRequest,
) (*proto.SignBlockResponse, error) {
res, _, err := rpc.thresholdValidator.Sign(ctx, req.ChainID, BlockFromProto(req.Block))
if err != nil {
return nil, err
}
return &proto.SignBlockResponse{
Signature: res,
}, nil
}
func (rpc *CosignerGRPCServer) SetNoncesAndSign(
ctx context.Context,
req *proto.SetNoncesAndSignRequest,
) (*proto.SetNoncesAndSignResponse, error) {
res, err := rpc.cosigner.SetNoncesAndSign(ctx, CosignerSetNoncesAndSignRequest{
ChainID: req.ChainID,
Nonces: &CosignerUUIDNonces{
UUID: uuid.UUID(req.Uuid),
Nonces: CosignerNoncesFromProto(req.GetNonces()),
},
HRST: HRSTKeyFromProto(req.GetHrst()),
SignBytes: req.GetSignBytes(),
})
if err != nil {
rpc.raftStore.logger.Error(
"Failed to sign with shard",
"chain_id", req.ChainID,
"height", req.Hrst.Height,
"round", req.Hrst.Round,
"step", req.Hrst.Step,
"error", err,
)
return nil, err
}
rpc.raftStore.logger.Info(
"Signed with shard",
"chain_id", req.ChainID,
"height", req.Hrst.Height,
"round", req.Hrst.Round,
"step", req.Hrst.Step,
)
return &proto.SetNoncesAndSignResponse{
NoncePublic: res.NoncePublic,
Timestamp: res.Timestamp.UnixNano(),
Signature: res.Signature,
}, nil
}
func (rpc *CosignerGRPCServer) GetNonces(
ctx context.Context,
req *proto.GetNoncesRequest,
) (*proto.GetNoncesResponse, error) {
uuids := make([]uuid.UUID, len(req.Uuids))
for i, uuidBytes := range req.Uuids {
uuids[i] = uuid.UUID(uuidBytes)
}
res, err := rpc.cosigner.GetNonces(
ctx,
uuids,
)
if err != nil {
return nil, err
}
return &proto.GetNoncesResponse{
Nonces: res.toProto(),
}, nil
}
func (rpc *CosignerGRPCServer) TransferLeadership(
_ context.Context,
req *proto.TransferLeadershipRequest,
) (*proto.TransferLeadershipResponse, error) {
if rpc.raftStore.raft.State() != raft.Leader {
return &proto.TransferLeadershipResponse{}, nil
}
leaderID := req.GetLeaderID()
if leaderID != "" {
for _, c := range rpc.raftStore.Cosigners {
shardID := fmt.Sprint(c.GetID())
if shardID == leaderID {
raftAddress := p2pURLToRaftAddress(c.GetAddress())
fmt.Printf("Transferring leadership to ID: %s - Address: %s\n", shardID, raftAddress)
rpc.raftStore.raft.LeadershipTransferToServer(raft.ServerID(shardID), raft.ServerAddress(raftAddress))
return &proto.TransferLeadershipResponse{LeaderID: shardID, LeaderAddress: raftAddress}, nil
}
}
}
fmt.Printf("Transferring leadership to next candidate\n")
rpc.raftStore.raft.LeadershipTransfer()
return &proto.TransferLeadershipResponse{}, nil
}
func (rpc *CosignerGRPCServer) GetLeader(
context.Context,
*proto.GetLeaderRequest,
) (*proto.GetLeaderResponse, error) {
leader := rpc.raftStore.GetLeader()
return &proto.GetLeaderResponse{Leader: int32(leader)}, nil
}
func (rpc *CosignerGRPCServer) Ping(context.Context, *proto.PingRequest) (*proto.PingResponse, error) {
return &proto.PingResponse{}, nil
}