forked from bazil/bazil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peerAdd.go
35 lines (31 loc) · 960 Bytes
/
peerAdd.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
package control
import (
"bytes"
"log"
"bazil.org/bazil/db"
"bazil.org/bazil/peer"
"bazil.org/bazil/server/control/wire"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
func (c controlRPC) PeerAdd(ctx context.Context, req *wire.PeerAddRequest) (*wire.PeerAddResponse, error) {
var pub peer.PublicKey
if err := pub.UnmarshalBinary(req.Pub); err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "bad peer public key: %v", err)
}
if bytes.Equal(pub[:], c.app.Keys.Sign.Pub[:]) {
return nil, grpc.Errorf(codes.InvalidArgument, "cannot add self as peer")
}
makePeer := func(tx *db.Tx) error {
if _, err := tx.Peers().Make(&pub); err != nil {
return err
}
return nil
}
if err := c.app.DB.Update(makePeer); err != nil {
log.Printf("db update error: put public key %x: %v", pub[:], err)
return nil, grpc.Errorf(codes.Internal, "database error")
}
return &wire.PeerAddResponse{}, nil
}