-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendkeypiece.go
79 lines (70 loc) · 2.22 KB
/
sendkeypiece.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
package pnetclient
import (
"context"
"errors"
"fmt"
"log"
"github.com/cpssd-students/paranoid/cmd/pfsd/globals"
"github.com/cpssd-students/paranoid/cmd/pfsd/keyman"
pb "github.com/cpssd-students/paranoid/proto/paranoid/paranoidnetwork/v1"
raftpb "github.com/cpssd-students/paranoid/proto/paranoid/raft/v1"
)
// SendKeyPiece to the node specified by the UUID
func SendKeyPiece(uuid string, generation int64, piece *keyman.KeyPiece, addElement bool) error {
node, err := globals.Nodes.GetNode(uuid)
if err != nil {
return errors.New("could not find node details")
}
conn, err := Dial(node)
if err != nil {
log.Printf("SendKeyPiece: failed to dial %s: %v", node, err)
return fmt.Errorf("failed to dial: %s: %w", node, err)
}
defer conn.Close()
client := pb.NewParanoidNetworkServiceClient(conn)
thisNodeProto := &pb.Node{
Ip: globals.ThisNode.IP,
Port: globals.ThisNode.Port,
CommonName: globals.ThisNode.CommonName,
Uuid: globals.ThisNode.UUID,
}
keyProto := &pb.KeyPiece{
Data: piece.Data,
ParentFingerprint: piece.ParentFingerprint[:],
Prime: piece.Prime.Bytes(),
Seq: piece.Seq,
Generation: generation,
OwnerNode: thisNodeProto,
}
resp, err := client.SendKeyPiece(context.Background(), &pb.SendKeyPieceRequest{
Key: keyProto,
AddElement: addElement,
})
if err != nil {
log.Printf("Failed sending KeyPiece to %s: %v", node, err)
return fmt.Errorf("Failed sending key piece to %s: %w", node, err)
}
if resp.ClientMustCommit && addElement {
raftThisNodeProto := &raftpb.Node{
Ip: globals.ThisNode.IP,
Port: globals.ThisNode.Port,
CommonName: globals.ThisNode.CommonName,
NodeId: globals.ThisNode.UUID,
}
raftOwnerNode := &raftpb.Node{
Ip: keyProto.GetOwnerNode().Ip,
Port: keyProto.GetOwnerNode().Port,
CommonName: keyProto.GetOwnerNode().CommonName,
NodeId: keyProto.GetOwnerNode().Uuid,
}
if err := globals.RaftNetworkServer.RequestKeyStateUpdate(
raftThisNodeProto,
raftOwnerNode,
generation,
); err != nil {
log.Printf("failed to commit to Raft: %v", err)
return fmt.Errorf("failed to commit to Raft: %w", err)
}
}
return nil
}